phy_device.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948
  1. /* Framework for finding and configuring PHYs.
  2. * Also contains generic PHY driver
  3. *
  4. * Author: Andy Fleming
  5. *
  6. * Copyright (c) 2004 Freescale Semiconductor, Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. */
  14. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  15. #include <linux/kernel.h>
  16. #include <linux/string.h>
  17. #include <linux/errno.h>
  18. #include <linux/unistd.h>
  19. #include <linux/slab.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/init.h>
  22. #include <linux/delay.h>
  23. #include <linux/netdevice.h>
  24. #include <linux/etherdevice.h>
  25. #include <linux/skbuff.h>
  26. #include <linux/mm.h>
  27. #include <linux/module.h>
  28. #include <linux/mii.h>
  29. #include <linux/ethtool.h>
  30. #include <linux/phy.h>
  31. #include <linux/phy_led_triggers.h>
  32. #include <linux/mdio.h>
  33. #include <linux/io.h>
  34. #include <linux/uaccess.h>
  35. #include <linux/of.h>
  36. #include <asm/irq.h>
  37. MODULE_DESCRIPTION("PHY library");
  38. MODULE_AUTHOR("Andy Fleming");
  39. MODULE_LICENSE("GPL");
  40. void phy_device_free(struct phy_device *phydev)
  41. {
  42. put_device(&phydev->mdio.dev);
  43. }
  44. EXPORT_SYMBOL(phy_device_free);
  45. static void phy_mdio_device_free(struct mdio_device *mdiodev)
  46. {
  47. struct phy_device *phydev;
  48. phydev = container_of(mdiodev, struct phy_device, mdio);
  49. phy_device_free(phydev);
  50. }
  51. static void phy_device_release(struct device *dev)
  52. {
  53. kfree(to_phy_device(dev));
  54. }
  55. static void phy_mdio_device_remove(struct mdio_device *mdiodev)
  56. {
  57. struct phy_device *phydev;
  58. phydev = container_of(mdiodev, struct phy_device, mdio);
  59. phy_device_remove(phydev);
  60. }
  61. static struct phy_driver genphy_driver;
  62. extern struct phy_driver genphy_10g_driver;
  63. static LIST_HEAD(phy_fixup_list);
  64. static DEFINE_MUTEX(phy_fixup_lock);
  65. #ifdef CONFIG_PM
  66. static bool mdio_bus_phy_may_suspend(struct phy_device *phydev)
  67. {
  68. struct device_driver *drv = phydev->mdio.dev.driver;
  69. struct phy_driver *phydrv = to_phy_driver(drv);
  70. struct net_device *netdev = phydev->attached_dev;
  71. if (!drv || !phydrv->suspend)
  72. return false;
  73. /* PHY not attached? May suspend if the PHY has not already been
  74. * suspended as part of a prior call to phy_disconnect() ->
  75. * phy_detach() -> phy_suspend() because the parent netdev might be the
  76. * MDIO bus driver and clock gated at this point.
  77. */
  78. if (!netdev)
  79. return !phydev->suspended;
  80. /* Don't suspend PHY if the attached netdev parent may wakeup.
  81. * The parent may point to a PCI device, as in tg3 driver.
  82. */
  83. if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent))
  84. return false;
  85. /* Also don't suspend PHY if the netdev itself may wakeup. This
  86. * is the case for devices w/o underlaying pwr. mgmt. aware bus,
  87. * e.g. SoC devices.
  88. */
  89. if (device_may_wakeup(&netdev->dev))
  90. return false;
  91. return true;
  92. }
  93. static int mdio_bus_phy_suspend(struct device *dev)
  94. {
  95. struct phy_device *phydev = to_phy_device(dev);
  96. /* We must stop the state machine manually, otherwise it stops out of
  97. * control, possibly with the phydev->lock held. Upon resume, netdev
  98. * may call phy routines that try to grab the same lock, and that may
  99. * lead to a deadlock.
  100. */
  101. if (phydev->attached_dev && phydev->adjust_link)
  102. phy_stop_machine(phydev);
  103. if (!mdio_bus_phy_may_suspend(phydev))
  104. return 0;
  105. return phy_suspend(phydev);
  106. }
  107. static int mdio_bus_phy_resume(struct device *dev)
  108. {
  109. struct phy_device *phydev = to_phy_device(dev);
  110. int ret;
  111. if (!mdio_bus_phy_may_suspend(phydev))
  112. goto no_resume;
  113. ret = phy_resume(phydev);
  114. if (ret < 0)
  115. return ret;
  116. no_resume:
  117. if (phydev->attached_dev && phydev->adjust_link)
  118. phy_start_machine(phydev);
  119. return 0;
  120. }
  121. static int mdio_bus_phy_restore(struct device *dev)
  122. {
  123. struct phy_device *phydev = to_phy_device(dev);
  124. struct net_device *netdev = phydev->attached_dev;
  125. int ret;
  126. if (!netdev)
  127. return 0;
  128. ret = phy_init_hw(phydev);
  129. if (ret < 0)
  130. return ret;
  131. /* The PHY needs to renegotiate. */
  132. phydev->link = 0;
  133. phydev->state = PHY_UP;
  134. phy_start_machine(phydev);
  135. return 0;
  136. }
  137. static const struct dev_pm_ops mdio_bus_phy_pm_ops = {
  138. .suspend = mdio_bus_phy_suspend,
  139. .resume = mdio_bus_phy_resume,
  140. .freeze = mdio_bus_phy_suspend,
  141. .thaw = mdio_bus_phy_resume,
  142. .restore = mdio_bus_phy_restore,
  143. };
  144. #define MDIO_BUS_PHY_PM_OPS (&mdio_bus_phy_pm_ops)
  145. #else
  146. #define MDIO_BUS_PHY_PM_OPS NULL
  147. #endif /* CONFIG_PM */
  148. /**
  149. * phy_register_fixup - creates a new phy_fixup and adds it to the list
  150. * @bus_id: A string which matches phydev->mdio.dev.bus_id (or PHY_ANY_ID)
  151. * @phy_uid: Used to match against phydev->phy_id (the UID of the PHY)
  152. * It can also be PHY_ANY_UID
  153. * @phy_uid_mask: Applied to phydev->phy_id and fixup->phy_uid before
  154. * comparison
  155. * @run: The actual code to be run when a matching PHY is found
  156. */
  157. int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
  158. int (*run)(struct phy_device *))
  159. {
  160. struct phy_fixup *fixup = kzalloc(sizeof(*fixup), GFP_KERNEL);
  161. if (!fixup)
  162. return -ENOMEM;
  163. strlcpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id));
  164. fixup->phy_uid = phy_uid;
  165. fixup->phy_uid_mask = phy_uid_mask;
  166. fixup->run = run;
  167. mutex_lock(&phy_fixup_lock);
  168. list_add_tail(&fixup->list, &phy_fixup_list);
  169. mutex_unlock(&phy_fixup_lock);
  170. return 0;
  171. }
  172. EXPORT_SYMBOL(phy_register_fixup);
  173. /* Registers a fixup to be run on any PHY with the UID in phy_uid */
  174. int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
  175. int (*run)(struct phy_device *))
  176. {
  177. return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run);
  178. }
  179. EXPORT_SYMBOL(phy_register_fixup_for_uid);
  180. /* Registers a fixup to be run on the PHY with id string bus_id */
  181. int phy_register_fixup_for_id(const char *bus_id,
  182. int (*run)(struct phy_device *))
  183. {
  184. return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run);
  185. }
  186. EXPORT_SYMBOL(phy_register_fixup_for_id);
  187. /**
  188. * phy_unregister_fixup - remove a phy_fixup from the list
  189. * @bus_id: A string matches fixup->bus_id (or PHY_ANY_ID) in phy_fixup_list
  190. * @phy_uid: A phy id matches fixup->phy_id (or PHY_ANY_UID) in phy_fixup_list
  191. * @phy_uid_mask: Applied to phy_uid and fixup->phy_uid before comparison
  192. */
  193. int phy_unregister_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask)
  194. {
  195. struct list_head *pos, *n;
  196. struct phy_fixup *fixup;
  197. int ret;
  198. ret = -ENODEV;
  199. mutex_lock(&phy_fixup_lock);
  200. list_for_each_safe(pos, n, &phy_fixup_list) {
  201. fixup = list_entry(pos, struct phy_fixup, list);
  202. if ((!strcmp(fixup->bus_id, bus_id)) &&
  203. ((fixup->phy_uid & phy_uid_mask) ==
  204. (phy_uid & phy_uid_mask))) {
  205. list_del(&fixup->list);
  206. kfree(fixup);
  207. ret = 0;
  208. break;
  209. }
  210. }
  211. mutex_unlock(&phy_fixup_lock);
  212. return ret;
  213. }
  214. EXPORT_SYMBOL(phy_unregister_fixup);
  215. /* Unregisters a fixup of any PHY with the UID in phy_uid */
  216. int phy_unregister_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask)
  217. {
  218. return phy_unregister_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask);
  219. }
  220. EXPORT_SYMBOL(phy_unregister_fixup_for_uid);
  221. /* Unregisters a fixup of the PHY with id string bus_id */
  222. int phy_unregister_fixup_for_id(const char *bus_id)
  223. {
  224. return phy_unregister_fixup(bus_id, PHY_ANY_UID, 0xffffffff);
  225. }
  226. EXPORT_SYMBOL(phy_unregister_fixup_for_id);
  227. /* Returns 1 if fixup matches phydev in bus_id and phy_uid.
  228. * Fixups can be set to match any in one or more fields.
  229. */
  230. static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup)
  231. {
  232. if (strcmp(fixup->bus_id, phydev_name(phydev)) != 0)
  233. if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0)
  234. return 0;
  235. if ((fixup->phy_uid & fixup->phy_uid_mask) !=
  236. (phydev->phy_id & fixup->phy_uid_mask))
  237. if (fixup->phy_uid != PHY_ANY_UID)
  238. return 0;
  239. return 1;
  240. }
  241. /* Runs any matching fixups for this phydev */
  242. static int phy_scan_fixups(struct phy_device *phydev)
  243. {
  244. struct phy_fixup *fixup;
  245. mutex_lock(&phy_fixup_lock);
  246. list_for_each_entry(fixup, &phy_fixup_list, list) {
  247. if (phy_needs_fixup(phydev, fixup)) {
  248. int err = fixup->run(phydev);
  249. if (err < 0) {
  250. mutex_unlock(&phy_fixup_lock);
  251. return err;
  252. }
  253. phydev->has_fixups = true;
  254. }
  255. }
  256. mutex_unlock(&phy_fixup_lock);
  257. return 0;
  258. }
  259. static int phy_bus_match(struct device *dev, struct device_driver *drv)
  260. {
  261. struct phy_device *phydev = to_phy_device(dev);
  262. struct phy_driver *phydrv = to_phy_driver(drv);
  263. const int num_ids = ARRAY_SIZE(phydev->c45_ids.device_ids);
  264. int i;
  265. if (!(phydrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY))
  266. return 0;
  267. if (phydrv->match_phy_device)
  268. return phydrv->match_phy_device(phydev);
  269. if (phydev->is_c45) {
  270. for (i = 1; i < num_ids; i++) {
  271. if (!(phydev->c45_ids.devices_in_package & (1 << i)))
  272. continue;
  273. if ((phydrv->phy_id & phydrv->phy_id_mask) ==
  274. (phydev->c45_ids.device_ids[i] &
  275. phydrv->phy_id_mask))
  276. return 1;
  277. }
  278. return 0;
  279. } else {
  280. return (phydrv->phy_id & phydrv->phy_id_mask) ==
  281. (phydev->phy_id & phydrv->phy_id_mask);
  282. }
  283. }
  284. struct phy_device *phy_device_create(struct mii_bus *bus, int addr, int phy_id,
  285. bool is_c45,
  286. struct phy_c45_device_ids *c45_ids)
  287. {
  288. struct phy_device *dev;
  289. struct mdio_device *mdiodev;
  290. /* We allocate the device, and initialize the default values */
  291. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  292. if (!dev)
  293. return ERR_PTR(-ENOMEM);
  294. mdiodev = &dev->mdio;
  295. mdiodev->dev.release = phy_device_release;
  296. mdiodev->dev.parent = &bus->dev;
  297. mdiodev->dev.bus = &mdio_bus_type;
  298. mdiodev->bus = bus;
  299. mdiodev->pm_ops = MDIO_BUS_PHY_PM_OPS;
  300. mdiodev->bus_match = phy_bus_match;
  301. mdiodev->addr = addr;
  302. mdiodev->flags = MDIO_DEVICE_FLAG_PHY;
  303. mdiodev->device_free = phy_mdio_device_free;
  304. mdiodev->device_remove = phy_mdio_device_remove;
  305. dev->speed = 0;
  306. dev->duplex = -1;
  307. dev->pause = 0;
  308. dev->asym_pause = 0;
  309. dev->link = 1;
  310. dev->interface = PHY_INTERFACE_MODE_GMII;
  311. dev->autoneg = AUTONEG_ENABLE;
  312. dev->is_c45 = is_c45;
  313. dev->phy_id = phy_id;
  314. if (c45_ids)
  315. dev->c45_ids = *c45_ids;
  316. dev->irq = bus->irq[addr];
  317. dev_set_name(&mdiodev->dev, PHY_ID_FMT, bus->id, addr);
  318. dev->state = PHY_DOWN;
  319. mutex_init(&dev->lock);
  320. INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine);
  321. INIT_WORK(&dev->phy_queue, phy_change_work);
  322. /* Request the appropriate module unconditionally; don't
  323. * bother trying to do so only if it isn't already loaded,
  324. * because that gets complicated. A hotplug event would have
  325. * done an unconditional modprobe anyway.
  326. * We don't do normal hotplug because it won't work for MDIO
  327. * -- because it relies on the device staying around for long
  328. * enough for the driver to get loaded. With MDIO, the NIC
  329. * driver will get bored and give up as soon as it finds that
  330. * there's no driver _already_ loaded.
  331. */
  332. request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT, MDIO_ID_ARGS(phy_id));
  333. device_initialize(&mdiodev->dev);
  334. return dev;
  335. }
  336. EXPORT_SYMBOL(phy_device_create);
  337. /* get_phy_c45_devs_in_pkg - reads a MMD's devices in package registers.
  338. * @bus: the target MII bus
  339. * @addr: PHY address on the MII bus
  340. * @dev_addr: MMD address in the PHY.
  341. * @devices_in_package: where to store the devices in package information.
  342. *
  343. * Description: reads devices in package registers of a MMD at @dev_addr
  344. * from PHY at @addr on @bus.
  345. *
  346. * Returns: 0 on success, -EIO on failure.
  347. */
  348. static int get_phy_c45_devs_in_pkg(struct mii_bus *bus, int addr, int dev_addr,
  349. u32 *devices_in_package)
  350. {
  351. int phy_reg, reg_addr;
  352. reg_addr = MII_ADDR_C45 | dev_addr << 16 | MDIO_DEVS2;
  353. phy_reg = mdiobus_read(bus, addr, reg_addr);
  354. if (phy_reg < 0)
  355. return -EIO;
  356. *devices_in_package = (phy_reg & 0xffff) << 16;
  357. reg_addr = MII_ADDR_C45 | dev_addr << 16 | MDIO_DEVS1;
  358. phy_reg = mdiobus_read(bus, addr, reg_addr);
  359. if (phy_reg < 0)
  360. return -EIO;
  361. *devices_in_package |= (phy_reg & 0xffff);
  362. return 0;
  363. }
  364. /**
  365. * get_phy_c45_ids - reads the specified addr for its 802.3-c45 IDs.
  366. * @bus: the target MII bus
  367. * @addr: PHY address on the MII bus
  368. * @phy_id: where to store the ID retrieved.
  369. * @c45_ids: where to store the c45 ID information.
  370. *
  371. * If the PHY devices-in-package appears to be valid, it and the
  372. * corresponding identifiers are stored in @c45_ids, zero is stored
  373. * in @phy_id. Otherwise 0xffffffff is stored in @phy_id. Returns
  374. * zero on success.
  375. *
  376. */
  377. static int get_phy_c45_ids(struct mii_bus *bus, int addr, u32 *phy_id,
  378. struct phy_c45_device_ids *c45_ids) {
  379. int phy_reg;
  380. int i, reg_addr;
  381. const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
  382. u32 *devs = &c45_ids->devices_in_package;
  383. /* Find first non-zero Devices In package. Device zero is reserved
  384. * for 802.3 c45 complied PHYs, so don't probe it at first.
  385. */
  386. for (i = 1; i < num_ids && *devs == 0; i++) {
  387. phy_reg = get_phy_c45_devs_in_pkg(bus, addr, i, devs);
  388. if (phy_reg < 0)
  389. return -EIO;
  390. if ((*devs & 0x1fffffff) == 0x1fffffff) {
  391. /* If mostly Fs, there is no device there,
  392. * then let's continue to probe more, as some
  393. * 10G PHYs have zero Devices In package,
  394. * e.g. Cortina CS4315/CS4340 PHY.
  395. */
  396. phy_reg = get_phy_c45_devs_in_pkg(bus, addr, 0, devs);
  397. if (phy_reg < 0)
  398. return -EIO;
  399. /* no device there, let's get out of here */
  400. if ((*devs & 0x1fffffff) == 0x1fffffff) {
  401. *phy_id = 0xffffffff;
  402. return 0;
  403. } else {
  404. break;
  405. }
  406. }
  407. }
  408. /* Now probe Device Identifiers for each device present. */
  409. for (i = 1; i < num_ids; i++) {
  410. if (!(c45_ids->devices_in_package & (1 << i)))
  411. continue;
  412. reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID1;
  413. phy_reg = mdiobus_read(bus, addr, reg_addr);
  414. if (phy_reg < 0)
  415. return -EIO;
  416. c45_ids->device_ids[i] = (phy_reg & 0xffff) << 16;
  417. reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID2;
  418. phy_reg = mdiobus_read(bus, addr, reg_addr);
  419. if (phy_reg < 0)
  420. return -EIO;
  421. c45_ids->device_ids[i] |= (phy_reg & 0xffff);
  422. }
  423. *phy_id = 0;
  424. return 0;
  425. }
  426. /**
  427. * get_phy_id - reads the specified addr for its ID.
  428. * @bus: the target MII bus
  429. * @addr: PHY address on the MII bus
  430. * @phy_id: where to store the ID retrieved.
  431. * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
  432. * @c45_ids: where to store the c45 ID information.
  433. *
  434. * Description: In the case of a 802.3-c22 PHY, reads the ID registers
  435. * of the PHY at @addr on the @bus, stores it in @phy_id and returns
  436. * zero on success.
  437. *
  438. * In the case of a 802.3-c45 PHY, get_phy_c45_ids() is invoked, and
  439. * its return value is in turn returned.
  440. *
  441. */
  442. static int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id,
  443. bool is_c45, struct phy_c45_device_ids *c45_ids)
  444. {
  445. int phy_reg;
  446. if (is_c45)
  447. return get_phy_c45_ids(bus, addr, phy_id, c45_ids);
  448. /* Grab the bits from PHYIR1, and put them in the upper half */
  449. phy_reg = mdiobus_read(bus, addr, MII_PHYSID1);
  450. if (phy_reg < 0)
  451. return -EIO;
  452. *phy_id = (phy_reg & 0xffff) << 16;
  453. /* Grab the bits from PHYIR2, and put them in the lower half */
  454. phy_reg = mdiobus_read(bus, addr, MII_PHYSID2);
  455. if (phy_reg < 0)
  456. return -EIO;
  457. *phy_id |= (phy_reg & 0xffff);
  458. return 0;
  459. }
  460. /**
  461. * get_phy_device - reads the specified PHY device and returns its @phy_device
  462. * struct
  463. * @bus: the target MII bus
  464. * @addr: PHY address on the MII bus
  465. * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
  466. *
  467. * Description: Reads the ID registers of the PHY at @addr on the
  468. * @bus, then allocates and returns the phy_device to represent it.
  469. */
  470. struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45)
  471. {
  472. struct phy_c45_device_ids c45_ids = {0};
  473. u32 phy_id = 0;
  474. int r;
  475. r = get_phy_id(bus, addr, &phy_id, is_c45, &c45_ids);
  476. if (r)
  477. return ERR_PTR(r);
  478. /* If the phy_id is mostly Fs, there is no device there */
  479. if ((phy_id & 0x1fffffff) == 0x1fffffff)
  480. return ERR_PTR(-ENODEV);
  481. return phy_device_create(bus, addr, phy_id, is_c45, &c45_ids);
  482. }
  483. EXPORT_SYMBOL(get_phy_device);
  484. static ssize_t
  485. phy_id_show(struct device *dev, struct device_attribute *attr, char *buf)
  486. {
  487. struct phy_device *phydev = to_phy_device(dev);
  488. return sprintf(buf, "0x%.8lx\n", (unsigned long)phydev->phy_id);
  489. }
  490. static DEVICE_ATTR_RO(phy_id);
  491. static ssize_t
  492. phy_interface_show(struct device *dev, struct device_attribute *attr, char *buf)
  493. {
  494. struct phy_device *phydev = to_phy_device(dev);
  495. const char *mode = NULL;
  496. if (phy_is_internal(phydev))
  497. mode = "internal";
  498. else
  499. mode = phy_modes(phydev->interface);
  500. return sprintf(buf, "%s\n", mode);
  501. }
  502. static DEVICE_ATTR_RO(phy_interface);
  503. static ssize_t
  504. phy_has_fixups_show(struct device *dev, struct device_attribute *attr,
  505. char *buf)
  506. {
  507. struct phy_device *phydev = to_phy_device(dev);
  508. return sprintf(buf, "%d\n", phydev->has_fixups);
  509. }
  510. static DEVICE_ATTR_RO(phy_has_fixups);
  511. static struct attribute *phy_dev_attrs[] = {
  512. &dev_attr_phy_id.attr,
  513. &dev_attr_phy_interface.attr,
  514. &dev_attr_phy_has_fixups.attr,
  515. NULL,
  516. };
  517. ATTRIBUTE_GROUPS(phy_dev);
  518. /**
  519. * phy_device_register - Register the phy device on the MDIO bus
  520. * @phydev: phy_device structure to be added to the MDIO bus
  521. */
  522. int phy_device_register(struct phy_device *phydev)
  523. {
  524. int err;
  525. err = mdiobus_register_device(&phydev->mdio);
  526. if (err)
  527. return err;
  528. /* Run all of the fixups for this PHY */
  529. err = phy_scan_fixups(phydev);
  530. if (err) {
  531. pr_err("PHY %d failed to initialize\n", phydev->mdio.addr);
  532. goto out;
  533. }
  534. phydev->mdio.dev.groups = phy_dev_groups;
  535. err = device_add(&phydev->mdio.dev);
  536. if (err) {
  537. pr_err("PHY %d failed to add\n", phydev->mdio.addr);
  538. goto out;
  539. }
  540. return 0;
  541. out:
  542. mdiobus_unregister_device(&phydev->mdio);
  543. return err;
  544. }
  545. EXPORT_SYMBOL(phy_device_register);
  546. /**
  547. * phy_device_remove - Remove a previously registered phy device from the MDIO bus
  548. * @phydev: phy_device structure to remove
  549. *
  550. * This doesn't free the phy_device itself, it merely reverses the effects
  551. * of phy_device_register(). Use phy_device_free() to free the device
  552. * after calling this function.
  553. */
  554. void phy_device_remove(struct phy_device *phydev)
  555. {
  556. device_del(&phydev->mdio.dev);
  557. mdiobus_unregister_device(&phydev->mdio);
  558. }
  559. EXPORT_SYMBOL(phy_device_remove);
  560. /**
  561. * phy_find_first - finds the first PHY device on the bus
  562. * @bus: the target MII bus
  563. */
  564. struct phy_device *phy_find_first(struct mii_bus *bus)
  565. {
  566. struct phy_device *phydev;
  567. int addr;
  568. for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
  569. phydev = mdiobus_get_phy(bus, addr);
  570. if (phydev)
  571. return phydev;
  572. }
  573. return NULL;
  574. }
  575. EXPORT_SYMBOL(phy_find_first);
  576. static void phy_link_change(struct phy_device *phydev, bool up, bool do_carrier)
  577. {
  578. struct net_device *netdev = phydev->attached_dev;
  579. if (do_carrier) {
  580. if (up)
  581. netif_carrier_on(netdev);
  582. else
  583. netif_carrier_off(netdev);
  584. }
  585. phydev->adjust_link(netdev);
  586. }
  587. /**
  588. * phy_prepare_link - prepares the PHY layer to monitor link status
  589. * @phydev: target phy_device struct
  590. * @handler: callback function for link status change notifications
  591. *
  592. * Description: Tells the PHY infrastructure to handle the
  593. * gory details on monitoring link status (whether through
  594. * polling or an interrupt), and to call back to the
  595. * connected device driver when the link status changes.
  596. * If you want to monitor your own link state, don't call
  597. * this function.
  598. */
  599. static void phy_prepare_link(struct phy_device *phydev,
  600. void (*handler)(struct net_device *))
  601. {
  602. phydev->adjust_link = handler;
  603. }
  604. /**
  605. * phy_connect_direct - connect an ethernet device to a specific phy_device
  606. * @dev: the network device to connect
  607. * @phydev: the pointer to the phy device
  608. * @handler: callback function for state change notifications
  609. * @interface: PHY device's interface
  610. */
  611. int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
  612. void (*handler)(struct net_device *),
  613. phy_interface_t interface)
  614. {
  615. int rc;
  616. rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
  617. if (rc)
  618. return rc;
  619. phy_prepare_link(phydev, handler);
  620. phy_start_machine(phydev);
  621. if (phydev->irq > 0)
  622. phy_start_interrupts(phydev);
  623. return 0;
  624. }
  625. EXPORT_SYMBOL(phy_connect_direct);
  626. /**
  627. * phy_connect - connect an ethernet device to a PHY device
  628. * @dev: the network device to connect
  629. * @bus_id: the id string of the PHY device to connect
  630. * @handler: callback function for state change notifications
  631. * @interface: PHY device's interface
  632. *
  633. * Description: Convenience function for connecting ethernet
  634. * devices to PHY devices. The default behavior is for
  635. * the PHY infrastructure to handle everything, and only notify
  636. * the connected driver when the link status changes. If you
  637. * don't want, or can't use the provided functionality, you may
  638. * choose to call only the subset of functions which provide
  639. * the desired functionality.
  640. */
  641. struct phy_device *phy_connect(struct net_device *dev, const char *bus_id,
  642. void (*handler)(struct net_device *),
  643. phy_interface_t interface)
  644. {
  645. struct phy_device *phydev;
  646. struct device *d;
  647. int rc;
  648. /* Search the list of PHY devices on the mdio bus for the
  649. * PHY with the requested name
  650. */
  651. d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id);
  652. if (!d) {
  653. pr_err("PHY %s not found\n", bus_id);
  654. return ERR_PTR(-ENODEV);
  655. }
  656. phydev = to_phy_device(d);
  657. rc = phy_connect_direct(dev, phydev, handler, interface);
  658. put_device(d);
  659. if (rc)
  660. return ERR_PTR(rc);
  661. return phydev;
  662. }
  663. EXPORT_SYMBOL(phy_connect);
  664. /**
  665. * phy_disconnect - disable interrupts, stop state machine, and detach a PHY
  666. * device
  667. * @phydev: target phy_device struct
  668. */
  669. void phy_disconnect(struct phy_device *phydev)
  670. {
  671. if (phydev->irq > 0)
  672. phy_stop_interrupts(phydev);
  673. phy_stop_machine(phydev);
  674. phydev->adjust_link = NULL;
  675. phy_detach(phydev);
  676. }
  677. EXPORT_SYMBOL(phy_disconnect);
  678. /**
  679. * phy_poll_reset - Safely wait until a PHY reset has properly completed
  680. * @phydev: The PHY device to poll
  681. *
  682. * Description: According to IEEE 802.3, Section 2, Subsection 22.2.4.1.1, as
  683. * published in 2008, a PHY reset may take up to 0.5 seconds. The MII BMCR
  684. * register must be polled until the BMCR_RESET bit clears.
  685. *
  686. * Furthermore, any attempts to write to PHY registers may have no effect
  687. * or even generate MDIO bus errors until this is complete.
  688. *
  689. * Some PHYs (such as the Marvell 88E1111) don't entirely conform to the
  690. * standard and do not fully reset after the BMCR_RESET bit is set, and may
  691. * even *REQUIRE* a soft-reset to properly restart autonegotiation. In an
  692. * effort to support such broken PHYs, this function is separate from the
  693. * standard phy_init_hw() which will zero all the other bits in the BMCR
  694. * and reapply all driver-specific and board-specific fixups.
  695. */
  696. static int phy_poll_reset(struct phy_device *phydev)
  697. {
  698. /* Poll until the reset bit clears (50ms per retry == 0.6 sec) */
  699. unsigned int retries = 12;
  700. int ret;
  701. do {
  702. msleep(50);
  703. ret = phy_read(phydev, MII_BMCR);
  704. if (ret < 0)
  705. return ret;
  706. } while (ret & BMCR_RESET && --retries);
  707. if (ret & BMCR_RESET)
  708. return -ETIMEDOUT;
  709. /* Some chips (smsc911x) may still need up to another 1ms after the
  710. * BMCR_RESET bit is cleared before they are usable.
  711. */
  712. msleep(1);
  713. return 0;
  714. }
  715. int phy_init_hw(struct phy_device *phydev)
  716. {
  717. int ret = 0;
  718. if (!phydev->drv || !phydev->drv->config_init)
  719. return 0;
  720. if (phydev->drv->soft_reset)
  721. ret = phydev->drv->soft_reset(phydev);
  722. else
  723. ret = genphy_soft_reset(phydev);
  724. if (ret < 0)
  725. return ret;
  726. ret = phy_scan_fixups(phydev);
  727. if (ret < 0)
  728. return ret;
  729. return phydev->drv->config_init(phydev);
  730. }
  731. EXPORT_SYMBOL(phy_init_hw);
  732. void phy_attached_info(struct phy_device *phydev)
  733. {
  734. phy_attached_print(phydev, NULL);
  735. }
  736. EXPORT_SYMBOL(phy_attached_info);
  737. #define ATTACHED_FMT "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%s)"
  738. void phy_attached_print(struct phy_device *phydev, const char *fmt, ...)
  739. {
  740. const char *drv_name = phydev->drv ? phydev->drv->name : "unbound";
  741. char *irq_str;
  742. char irq_num[8];
  743. switch(phydev->irq) {
  744. case PHY_POLL:
  745. irq_str = "POLL";
  746. break;
  747. case PHY_IGNORE_INTERRUPT:
  748. irq_str = "IGNORE";
  749. break;
  750. default:
  751. snprintf(irq_num, sizeof(irq_num), "%d", phydev->irq);
  752. irq_str = irq_num;
  753. break;
  754. }
  755. if (!fmt) {
  756. dev_info(&phydev->mdio.dev, ATTACHED_FMT "\n",
  757. drv_name, phydev_name(phydev),
  758. irq_str);
  759. } else {
  760. va_list ap;
  761. dev_info(&phydev->mdio.dev, ATTACHED_FMT,
  762. drv_name, phydev_name(phydev),
  763. irq_str);
  764. va_start(ap, fmt);
  765. vprintk(fmt, ap);
  766. va_end(ap);
  767. }
  768. }
  769. EXPORT_SYMBOL(phy_attached_print);
  770. /**
  771. * phy_attach_direct - attach a network device to a given PHY device pointer
  772. * @dev: network device to attach
  773. * @phydev: Pointer to phy_device to attach
  774. * @flags: PHY device's dev_flags
  775. * @interface: PHY device's interface
  776. *
  777. * Description: Called by drivers to attach to a particular PHY
  778. * device. The phy_device is found, and properly hooked up
  779. * to the phy_driver. If no driver is attached, then a
  780. * generic driver is used. The phy_device is given a ptr to
  781. * the attaching device, and given a callback for link status
  782. * change. The phy_device is returned to the attaching driver.
  783. * This function takes a reference on the phy device.
  784. */
  785. int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
  786. u32 flags, phy_interface_t interface)
  787. {
  788. struct module *ndev_owner = dev->dev.parent->driver->owner;
  789. struct mii_bus *bus = phydev->mdio.bus;
  790. struct device *d = &phydev->mdio.dev;
  791. bool using_genphy = false;
  792. int err;
  793. /* For Ethernet device drivers that register their own MDIO bus, we
  794. * will have bus->owner match ndev_mod, so we do not want to increment
  795. * our own module->refcnt here, otherwise we would not be able to
  796. * unload later on.
  797. */
  798. if (ndev_owner != bus->owner && !try_module_get(bus->owner)) {
  799. dev_err(&dev->dev, "failed to get the bus module\n");
  800. return -EIO;
  801. }
  802. get_device(d);
  803. /* Assume that if there is no driver, that it doesn't
  804. * exist, and we should use the genphy driver.
  805. */
  806. if (!d->driver) {
  807. if (phydev->is_c45)
  808. d->driver = &genphy_10g_driver.mdiodrv.driver;
  809. else
  810. d->driver = &genphy_driver.mdiodrv.driver;
  811. using_genphy = true;
  812. }
  813. if (!try_module_get(d->driver->owner)) {
  814. dev_err(&dev->dev, "failed to get the device driver module\n");
  815. err = -EIO;
  816. goto error_put_device;
  817. }
  818. if (using_genphy) {
  819. err = d->driver->probe(d);
  820. if (err >= 0)
  821. err = device_bind_driver(d);
  822. if (err)
  823. goto error_module_put;
  824. }
  825. if (phydev->attached_dev) {
  826. dev_err(&dev->dev, "PHY already attached\n");
  827. err = -EBUSY;
  828. goto error;
  829. }
  830. phydev->phy_link_change = phy_link_change;
  831. phydev->attached_dev = dev;
  832. dev->phydev = phydev;
  833. /* Some Ethernet drivers try to connect to a PHY device before
  834. * calling register_netdevice() -> netdev_register_kobject() and
  835. * does the dev->dev.kobj initialization. Here we only check for
  836. * success which indicates that the network device kobject is
  837. * ready. Once we do that we still need to keep track of whether
  838. * links were successfully set up or not for phy_detach() to
  839. * remove them accordingly.
  840. */
  841. phydev->sysfs_links = false;
  842. err = sysfs_create_link(&phydev->mdio.dev.kobj, &dev->dev.kobj,
  843. "attached_dev");
  844. if (!err) {
  845. err = sysfs_create_link(&dev->dev.kobj, &phydev->mdio.dev.kobj,
  846. "phydev");
  847. if (err)
  848. goto error;
  849. phydev->sysfs_links = true;
  850. }
  851. phydev->dev_flags = flags;
  852. phydev->interface = interface;
  853. phydev->state = PHY_READY;
  854. /* Initial carrier state is off as the phy is about to be
  855. * (re)initialized.
  856. */
  857. netif_carrier_off(phydev->attached_dev);
  858. /* Do initial configuration here, now that
  859. * we have certain key parameters
  860. * (dev_flags and interface)
  861. */
  862. err = phy_init_hw(phydev);
  863. if (err)
  864. goto error;
  865. phy_resume(phydev);
  866. phy_led_triggers_register(phydev);
  867. return err;
  868. error:
  869. /* phy_detach() does all of the cleanup below */
  870. phy_detach(phydev);
  871. return err;
  872. error_module_put:
  873. module_put(d->driver->owner);
  874. error_put_device:
  875. put_device(d);
  876. if (ndev_owner != bus->owner)
  877. module_put(bus->owner);
  878. return err;
  879. }
  880. EXPORT_SYMBOL(phy_attach_direct);
  881. /**
  882. * phy_attach - attach a network device to a particular PHY device
  883. * @dev: network device to attach
  884. * @bus_id: Bus ID of PHY device to attach
  885. * @interface: PHY device's interface
  886. *
  887. * Description: Same as phy_attach_direct() except that a PHY bus_id
  888. * string is passed instead of a pointer to a struct phy_device.
  889. */
  890. struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
  891. phy_interface_t interface)
  892. {
  893. struct bus_type *bus = &mdio_bus_type;
  894. struct phy_device *phydev;
  895. struct device *d;
  896. int rc;
  897. /* Search the list of PHY devices on the mdio bus for the
  898. * PHY with the requested name
  899. */
  900. d = bus_find_device_by_name(bus, NULL, bus_id);
  901. if (!d) {
  902. pr_err("PHY %s not found\n", bus_id);
  903. return ERR_PTR(-ENODEV);
  904. }
  905. phydev = to_phy_device(d);
  906. rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
  907. put_device(d);
  908. if (rc)
  909. return ERR_PTR(rc);
  910. return phydev;
  911. }
  912. EXPORT_SYMBOL(phy_attach);
  913. /**
  914. * phy_detach - detach a PHY device from its network device
  915. * @phydev: target phy_device struct
  916. *
  917. * This detaches the phy device from its network device and the phy
  918. * driver, and drops the reference count taken in phy_attach_direct().
  919. */
  920. void phy_detach(struct phy_device *phydev)
  921. {
  922. struct net_device *dev = phydev->attached_dev;
  923. struct module *ndev_owner = dev->dev.parent->driver->owner;
  924. struct mii_bus *bus;
  925. if (phydev->sysfs_links) {
  926. sysfs_remove_link(&dev->dev.kobj, "phydev");
  927. sysfs_remove_link(&phydev->mdio.dev.kobj, "attached_dev");
  928. }
  929. phydev->attached_dev->phydev = NULL;
  930. phydev->attached_dev = NULL;
  931. phy_suspend(phydev);
  932. phydev->phylink = NULL;
  933. phy_led_triggers_unregister(phydev);
  934. module_put(phydev->mdio.dev.driver->owner);
  935. /* If the device had no specific driver before (i.e. - it
  936. * was using the generic driver), we unbind the device
  937. * from the generic driver so that there's a chance a
  938. * real driver could be loaded
  939. */
  940. if (phydev->mdio.dev.driver == &genphy_10g_driver.mdiodrv.driver ||
  941. phydev->mdio.dev.driver == &genphy_driver.mdiodrv.driver)
  942. device_release_driver(&phydev->mdio.dev);
  943. /*
  944. * The phydev might go away on the put_device() below, so avoid
  945. * a use-after-free bug by reading the underlying bus first.
  946. */
  947. bus = phydev->mdio.bus;
  948. put_device(&phydev->mdio.dev);
  949. if (ndev_owner != bus->owner)
  950. module_put(bus->owner);
  951. }
  952. EXPORT_SYMBOL(phy_detach);
  953. int phy_suspend(struct phy_device *phydev)
  954. {
  955. struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
  956. struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
  957. int ret = 0;
  958. /* If the device has WOL enabled, we cannot suspend the PHY */
  959. phy_ethtool_get_wol(phydev, &wol);
  960. if (wol.wolopts)
  961. return -EBUSY;
  962. if (phydev->drv && phydrv->suspend)
  963. ret = phydrv->suspend(phydev);
  964. if (ret)
  965. return ret;
  966. phydev->suspended = true;
  967. return ret;
  968. }
  969. EXPORT_SYMBOL(phy_suspend);
  970. int phy_resume(struct phy_device *phydev)
  971. {
  972. struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
  973. int ret = 0;
  974. if (phydev->drv && phydrv->resume)
  975. ret = phydrv->resume(phydev);
  976. if (ret)
  977. return ret;
  978. phydev->suspended = false;
  979. return ret;
  980. }
  981. EXPORT_SYMBOL(phy_resume);
  982. int phy_loopback(struct phy_device *phydev, bool enable)
  983. {
  984. struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
  985. int ret = 0;
  986. mutex_lock(&phydev->lock);
  987. if (enable && phydev->loopback_enabled) {
  988. ret = -EBUSY;
  989. goto out;
  990. }
  991. if (!enable && !phydev->loopback_enabled) {
  992. ret = -EINVAL;
  993. goto out;
  994. }
  995. if (phydev->drv && phydrv->set_loopback)
  996. ret = phydrv->set_loopback(phydev, enable);
  997. else
  998. ret = -EOPNOTSUPP;
  999. if (ret)
  1000. goto out;
  1001. phydev->loopback_enabled = enable;
  1002. out:
  1003. mutex_unlock(&phydev->lock);
  1004. return ret;
  1005. }
  1006. EXPORT_SYMBOL(phy_loopback);
  1007. /* Generic PHY support and helper functions */
  1008. /**
  1009. * genphy_config_advert - sanitize and advertise auto-negotiation parameters
  1010. * @phydev: target phy_device struct
  1011. *
  1012. * Description: Writes MII_ADVERTISE with the appropriate values,
  1013. * after sanitizing the values to make sure we only advertise
  1014. * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
  1015. * hasn't changed, and > 0 if it has changed.
  1016. */
  1017. static int genphy_config_advert(struct phy_device *phydev)
  1018. {
  1019. u32 advertise;
  1020. int oldadv, adv, bmsr;
  1021. int err, changed = 0;
  1022. /* Only allow advertising what this PHY supports */
  1023. phydev->advertising &= phydev->supported;
  1024. advertise = phydev->advertising;
  1025. /* Setup standard advertisement */
  1026. adv = phy_read(phydev, MII_ADVERTISE);
  1027. if (adv < 0)
  1028. return adv;
  1029. oldadv = adv;
  1030. adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
  1031. ADVERTISE_PAUSE_ASYM);
  1032. adv |= ethtool_adv_to_mii_adv_t(advertise);
  1033. if (adv != oldadv) {
  1034. err = phy_write(phydev, MII_ADVERTISE, adv);
  1035. if (err < 0)
  1036. return err;
  1037. changed = 1;
  1038. }
  1039. bmsr = phy_read(phydev, MII_BMSR);
  1040. if (bmsr < 0)
  1041. return bmsr;
  1042. /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all
  1043. * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a
  1044. * logical 1.
  1045. */
  1046. if (!(bmsr & BMSR_ESTATEN))
  1047. return changed;
  1048. /* Configure gigabit if it's supported */
  1049. adv = phy_read(phydev, MII_CTRL1000);
  1050. if (adv < 0)
  1051. return adv;
  1052. oldadv = adv;
  1053. adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
  1054. if (phydev->supported & (SUPPORTED_1000baseT_Half |
  1055. SUPPORTED_1000baseT_Full)) {
  1056. adv |= ethtool_adv_to_mii_ctrl1000_t(advertise);
  1057. }
  1058. if (adv != oldadv)
  1059. changed = 1;
  1060. err = phy_write(phydev, MII_CTRL1000, adv);
  1061. if (err < 0)
  1062. return err;
  1063. return changed;
  1064. }
  1065. /**
  1066. * genphy_config_eee_advert - disable unwanted eee mode advertisement
  1067. * @phydev: target phy_device struct
  1068. *
  1069. * Description: Writes MDIO_AN_EEE_ADV after disabling unsupported energy
  1070. * efficent ethernet modes. Returns 0 if the PHY's advertisement hasn't
  1071. * changed, and 1 if it has changed.
  1072. */
  1073. static int genphy_config_eee_advert(struct phy_device *phydev)
  1074. {
  1075. int broken = phydev->eee_broken_modes;
  1076. int old_adv, adv;
  1077. /* Nothing to disable */
  1078. if (!broken)
  1079. return 0;
  1080. /* If the following call fails, we assume that EEE is not
  1081. * supported by the phy. If we read 0, EEE is not advertised
  1082. * In both case, we don't need to continue
  1083. */
  1084. adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
  1085. if (adv <= 0)
  1086. return 0;
  1087. old_adv = adv;
  1088. adv &= ~broken;
  1089. /* Advertising remains unchanged with the broken mask */
  1090. if (old_adv == adv)
  1091. return 0;
  1092. phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, adv);
  1093. return 1;
  1094. }
  1095. /**
  1096. * genphy_setup_forced - configures/forces speed/duplex from @phydev
  1097. * @phydev: target phy_device struct
  1098. *
  1099. * Description: Configures MII_BMCR to force speed/duplex
  1100. * to the values in phydev. Assumes that the values are valid.
  1101. * Please see phy_sanitize_settings().
  1102. */
  1103. int genphy_setup_forced(struct phy_device *phydev)
  1104. {
  1105. int ctl = phy_read(phydev, MII_BMCR);
  1106. ctl &= BMCR_LOOPBACK | BMCR_ISOLATE | BMCR_PDOWN;
  1107. phydev->pause = 0;
  1108. phydev->asym_pause = 0;
  1109. if (SPEED_1000 == phydev->speed)
  1110. ctl |= BMCR_SPEED1000;
  1111. else if (SPEED_100 == phydev->speed)
  1112. ctl |= BMCR_SPEED100;
  1113. if (DUPLEX_FULL == phydev->duplex)
  1114. ctl |= BMCR_FULLDPLX;
  1115. return phy_write(phydev, MII_BMCR, ctl);
  1116. }
  1117. EXPORT_SYMBOL(genphy_setup_forced);
  1118. /**
  1119. * genphy_restart_aneg - Enable and Restart Autonegotiation
  1120. * @phydev: target phy_device struct
  1121. */
  1122. int genphy_restart_aneg(struct phy_device *phydev)
  1123. {
  1124. int ctl = phy_read(phydev, MII_BMCR);
  1125. if (ctl < 0)
  1126. return ctl;
  1127. ctl |= BMCR_ANENABLE | BMCR_ANRESTART;
  1128. /* Don't isolate the PHY if we're negotiating */
  1129. ctl &= ~BMCR_ISOLATE;
  1130. return phy_write(phydev, MII_BMCR, ctl);
  1131. }
  1132. EXPORT_SYMBOL(genphy_restart_aneg);
  1133. /**
  1134. * genphy_config_aneg - restart auto-negotiation or write BMCR
  1135. * @phydev: target phy_device struct
  1136. *
  1137. * Description: If auto-negotiation is enabled, we configure the
  1138. * advertising, and then restart auto-negotiation. If it is not
  1139. * enabled, then we write the BMCR.
  1140. */
  1141. int genphy_config_aneg(struct phy_device *phydev)
  1142. {
  1143. int err, changed;
  1144. changed = genphy_config_eee_advert(phydev);
  1145. if (AUTONEG_ENABLE != phydev->autoneg)
  1146. return genphy_setup_forced(phydev);
  1147. err = genphy_config_advert(phydev);
  1148. if (err < 0) /* error */
  1149. return err;
  1150. changed |= err;
  1151. if (changed == 0) {
  1152. /* Advertisement hasn't changed, but maybe aneg was never on to
  1153. * begin with? Or maybe phy was isolated?
  1154. */
  1155. int ctl = phy_read(phydev, MII_BMCR);
  1156. if (ctl < 0)
  1157. return ctl;
  1158. if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
  1159. changed = 1; /* do restart aneg */
  1160. }
  1161. /* Only restart aneg if we are advertising something different
  1162. * than we were before.
  1163. */
  1164. if (changed > 0)
  1165. return genphy_restart_aneg(phydev);
  1166. return 0;
  1167. }
  1168. EXPORT_SYMBOL(genphy_config_aneg);
  1169. /**
  1170. * genphy_aneg_done - return auto-negotiation status
  1171. * @phydev: target phy_device struct
  1172. *
  1173. * Description: Reads the status register and returns 0 either if
  1174. * auto-negotiation is incomplete, or if there was an error.
  1175. * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
  1176. */
  1177. int genphy_aneg_done(struct phy_device *phydev)
  1178. {
  1179. int retval = phy_read(phydev, MII_BMSR);
  1180. return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
  1181. }
  1182. EXPORT_SYMBOL(genphy_aneg_done);
  1183. /**
  1184. * genphy_update_link - update link status in @phydev
  1185. * @phydev: target phy_device struct
  1186. *
  1187. * Description: Update the value in phydev->link to reflect the
  1188. * current link value. In order to do this, we need to read
  1189. * the status register twice, keeping the second value.
  1190. */
  1191. int genphy_update_link(struct phy_device *phydev)
  1192. {
  1193. int status;
  1194. /* Do a fake read */
  1195. status = phy_read(phydev, MII_BMSR);
  1196. if (status < 0)
  1197. return status;
  1198. /* Read link and autonegotiation status */
  1199. status = phy_read(phydev, MII_BMSR);
  1200. if (status < 0)
  1201. return status;
  1202. if ((status & BMSR_LSTATUS) == 0)
  1203. phydev->link = 0;
  1204. else
  1205. phydev->link = 1;
  1206. return 0;
  1207. }
  1208. EXPORT_SYMBOL(genphy_update_link);
  1209. /**
  1210. * genphy_read_status - check the link status and update current link state
  1211. * @phydev: target phy_device struct
  1212. *
  1213. * Description: Check the link, then figure out the current state
  1214. * by comparing what we advertise with what the link partner
  1215. * advertises. Start by checking the gigabit possibilities,
  1216. * then move on to 10/100.
  1217. */
  1218. int genphy_read_status(struct phy_device *phydev)
  1219. {
  1220. int adv;
  1221. int err;
  1222. int lpa;
  1223. int lpagb = 0;
  1224. int common_adv;
  1225. int common_adv_gb = 0;
  1226. /* Update the link, but return if there was an error */
  1227. err = genphy_update_link(phydev);
  1228. if (err)
  1229. return err;
  1230. phydev->lp_advertising = 0;
  1231. if (AUTONEG_ENABLE == phydev->autoneg) {
  1232. if (phydev->supported & (SUPPORTED_1000baseT_Half
  1233. | SUPPORTED_1000baseT_Full)) {
  1234. lpagb = phy_read(phydev, MII_STAT1000);
  1235. if (lpagb < 0)
  1236. return lpagb;
  1237. adv = phy_read(phydev, MII_CTRL1000);
  1238. if (adv < 0)
  1239. return adv;
  1240. phydev->lp_advertising =
  1241. mii_stat1000_to_ethtool_lpa_t(lpagb);
  1242. common_adv_gb = lpagb & adv << 2;
  1243. }
  1244. lpa = phy_read(phydev, MII_LPA);
  1245. if (lpa < 0)
  1246. return lpa;
  1247. phydev->lp_advertising |= mii_lpa_to_ethtool_lpa_t(lpa);
  1248. adv = phy_read(phydev, MII_ADVERTISE);
  1249. if (adv < 0)
  1250. return adv;
  1251. common_adv = lpa & adv;
  1252. phydev->speed = SPEED_10;
  1253. phydev->duplex = DUPLEX_HALF;
  1254. phydev->pause = 0;
  1255. phydev->asym_pause = 0;
  1256. if (common_adv_gb & (LPA_1000FULL | LPA_1000HALF)) {
  1257. phydev->speed = SPEED_1000;
  1258. if (common_adv_gb & LPA_1000FULL)
  1259. phydev->duplex = DUPLEX_FULL;
  1260. } else if (common_adv & (LPA_100FULL | LPA_100HALF)) {
  1261. phydev->speed = SPEED_100;
  1262. if (common_adv & LPA_100FULL)
  1263. phydev->duplex = DUPLEX_FULL;
  1264. } else
  1265. if (common_adv & LPA_10FULL)
  1266. phydev->duplex = DUPLEX_FULL;
  1267. if (phydev->duplex == DUPLEX_FULL) {
  1268. phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
  1269. phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
  1270. }
  1271. } else {
  1272. int bmcr = phy_read(phydev, MII_BMCR);
  1273. if (bmcr < 0)
  1274. return bmcr;
  1275. if (bmcr & BMCR_FULLDPLX)
  1276. phydev->duplex = DUPLEX_FULL;
  1277. else
  1278. phydev->duplex = DUPLEX_HALF;
  1279. if (bmcr & BMCR_SPEED1000)
  1280. phydev->speed = SPEED_1000;
  1281. else if (bmcr & BMCR_SPEED100)
  1282. phydev->speed = SPEED_100;
  1283. else
  1284. phydev->speed = SPEED_10;
  1285. phydev->pause = 0;
  1286. phydev->asym_pause = 0;
  1287. }
  1288. return 0;
  1289. }
  1290. EXPORT_SYMBOL(genphy_read_status);
  1291. /**
  1292. * genphy_soft_reset - software reset the PHY via BMCR_RESET bit
  1293. * @phydev: target phy_device struct
  1294. *
  1295. * Description: Perform a software PHY reset using the standard
  1296. * BMCR_RESET bit and poll for the reset bit to be cleared.
  1297. *
  1298. * Returns: 0 on success, < 0 on failure
  1299. */
  1300. int genphy_soft_reset(struct phy_device *phydev)
  1301. {
  1302. int ret;
  1303. ret = phy_write(phydev, MII_BMCR, BMCR_RESET);
  1304. if (ret < 0)
  1305. return ret;
  1306. return phy_poll_reset(phydev);
  1307. }
  1308. EXPORT_SYMBOL(genphy_soft_reset);
  1309. int genphy_config_init(struct phy_device *phydev)
  1310. {
  1311. int val;
  1312. u32 features;
  1313. features = (SUPPORTED_TP | SUPPORTED_MII
  1314. | SUPPORTED_AUI | SUPPORTED_FIBRE |
  1315. SUPPORTED_BNC | SUPPORTED_Pause | SUPPORTED_Asym_Pause);
  1316. /* Do we support autonegotiation? */
  1317. val = phy_read(phydev, MII_BMSR);
  1318. if (val < 0)
  1319. return val;
  1320. if (val & BMSR_ANEGCAPABLE)
  1321. features |= SUPPORTED_Autoneg;
  1322. if (val & BMSR_100FULL)
  1323. features |= SUPPORTED_100baseT_Full;
  1324. if (val & BMSR_100HALF)
  1325. features |= SUPPORTED_100baseT_Half;
  1326. if (val & BMSR_10FULL)
  1327. features |= SUPPORTED_10baseT_Full;
  1328. if (val & BMSR_10HALF)
  1329. features |= SUPPORTED_10baseT_Half;
  1330. if (val & BMSR_ESTATEN) {
  1331. val = phy_read(phydev, MII_ESTATUS);
  1332. if (val < 0)
  1333. return val;
  1334. if (val & ESTATUS_1000_TFULL)
  1335. features |= SUPPORTED_1000baseT_Full;
  1336. if (val & ESTATUS_1000_THALF)
  1337. features |= SUPPORTED_1000baseT_Half;
  1338. }
  1339. phydev->supported &= features;
  1340. phydev->advertising &= features;
  1341. return 0;
  1342. }
  1343. EXPORT_SYMBOL(genphy_config_init);
  1344. int genphy_suspend(struct phy_device *phydev)
  1345. {
  1346. int value;
  1347. mutex_lock(&phydev->lock);
  1348. value = phy_read(phydev, MII_BMCR);
  1349. phy_write(phydev, MII_BMCR, value | BMCR_PDOWN);
  1350. mutex_unlock(&phydev->lock);
  1351. return 0;
  1352. }
  1353. EXPORT_SYMBOL(genphy_suspend);
  1354. int genphy_resume(struct phy_device *phydev)
  1355. {
  1356. int value;
  1357. mutex_lock(&phydev->lock);
  1358. value = phy_read(phydev, MII_BMCR);
  1359. phy_write(phydev, MII_BMCR, value & ~BMCR_PDOWN);
  1360. mutex_unlock(&phydev->lock);
  1361. return 0;
  1362. }
  1363. EXPORT_SYMBOL(genphy_resume);
  1364. int genphy_loopback(struct phy_device *phydev, bool enable)
  1365. {
  1366. int value;
  1367. value = phy_read(phydev, MII_BMCR);
  1368. if (value < 0)
  1369. return value;
  1370. if (enable)
  1371. value |= BMCR_LOOPBACK;
  1372. else
  1373. value &= ~BMCR_LOOPBACK;
  1374. return phy_write(phydev, MII_BMCR, value);
  1375. }
  1376. EXPORT_SYMBOL(genphy_loopback);
  1377. static int __set_phy_supported(struct phy_device *phydev, u32 max_speed)
  1378. {
  1379. /* The default values for phydev->supported are provided by the PHY
  1380. * driver "features" member, we want to reset to sane defaults first
  1381. * before supporting higher speeds.
  1382. */
  1383. phydev->supported &= PHY_DEFAULT_FEATURES;
  1384. switch (max_speed) {
  1385. default:
  1386. return -ENOTSUPP;
  1387. case SPEED_1000:
  1388. phydev->supported |= PHY_1000BT_FEATURES;
  1389. /* fall through */
  1390. case SPEED_100:
  1391. phydev->supported |= PHY_100BT_FEATURES;
  1392. /* fall through */
  1393. case SPEED_10:
  1394. phydev->supported |= PHY_10BT_FEATURES;
  1395. }
  1396. return 0;
  1397. }
  1398. int phy_set_max_speed(struct phy_device *phydev, u32 max_speed)
  1399. {
  1400. int err;
  1401. err = __set_phy_supported(phydev, max_speed);
  1402. if (err)
  1403. return err;
  1404. phydev->advertising = phydev->supported;
  1405. return 0;
  1406. }
  1407. EXPORT_SYMBOL(phy_set_max_speed);
  1408. static void of_set_phy_supported(struct phy_device *phydev)
  1409. {
  1410. struct device_node *node = phydev->mdio.dev.of_node;
  1411. u32 max_speed;
  1412. if (!IS_ENABLED(CONFIG_OF_MDIO))
  1413. return;
  1414. if (!node)
  1415. return;
  1416. if (!of_property_read_u32(node, "max-speed", &max_speed))
  1417. __set_phy_supported(phydev, max_speed);
  1418. }
  1419. static void of_set_phy_eee_broken(struct phy_device *phydev)
  1420. {
  1421. struct device_node *node = phydev->mdio.dev.of_node;
  1422. u32 broken = 0;
  1423. if (!IS_ENABLED(CONFIG_OF_MDIO))
  1424. return;
  1425. if (!node)
  1426. return;
  1427. if (of_property_read_bool(node, "eee-broken-100tx"))
  1428. broken |= MDIO_EEE_100TX;
  1429. if (of_property_read_bool(node, "eee-broken-1000t"))
  1430. broken |= MDIO_EEE_1000T;
  1431. if (of_property_read_bool(node, "eee-broken-10gt"))
  1432. broken |= MDIO_EEE_10GT;
  1433. if (of_property_read_bool(node, "eee-broken-1000kx"))
  1434. broken |= MDIO_EEE_1000KX;
  1435. if (of_property_read_bool(node, "eee-broken-10gkx4"))
  1436. broken |= MDIO_EEE_10GKX4;
  1437. if (of_property_read_bool(node, "eee-broken-10gkr"))
  1438. broken |= MDIO_EEE_10GKR;
  1439. phydev->eee_broken_modes = broken;
  1440. }
  1441. /**
  1442. * phy_probe - probe and init a PHY device
  1443. * @dev: device to probe and init
  1444. *
  1445. * Description: Take care of setting up the phy_device structure,
  1446. * set the state to READY (the driver's init function should
  1447. * set it to STARTING if needed).
  1448. */
  1449. static int phy_probe(struct device *dev)
  1450. {
  1451. struct phy_device *phydev = to_phy_device(dev);
  1452. struct device_driver *drv = phydev->mdio.dev.driver;
  1453. struct phy_driver *phydrv = to_phy_driver(drv);
  1454. int err = 0;
  1455. phydev->drv = phydrv;
  1456. /* Disable the interrupt if the PHY doesn't support it
  1457. * but the interrupt is still a valid one
  1458. */
  1459. if (!(phydrv->flags & PHY_HAS_INTERRUPT) &&
  1460. phy_interrupt_is_valid(phydev))
  1461. phydev->irq = PHY_POLL;
  1462. if (phydrv->flags & PHY_IS_INTERNAL)
  1463. phydev->is_internal = true;
  1464. mutex_lock(&phydev->lock);
  1465. /* Start out supporting everything. Eventually,
  1466. * a controller will attach, and may modify one
  1467. * or both of these values
  1468. */
  1469. phydev->supported = phydrv->features;
  1470. of_set_phy_supported(phydev);
  1471. phydev->advertising = phydev->supported;
  1472. /* Get the EEE modes we want to prohibit. We will ask
  1473. * the PHY stop advertising these mode later on
  1474. */
  1475. of_set_phy_eee_broken(phydev);
  1476. /* The Pause Frame bits indicate that the PHY can support passing
  1477. * pause frames. During autonegotiation, the PHYs will determine if
  1478. * they should allow pause frames to pass. The MAC driver should then
  1479. * use that result to determine whether to enable flow control via
  1480. * pause frames.
  1481. *
  1482. * Normally, PHY drivers should not set the Pause bits, and instead
  1483. * allow phylib to do that. However, there may be some situations
  1484. * (e.g. hardware erratum) where the driver wants to set only one
  1485. * of these bits.
  1486. */
  1487. if (phydrv->features & (SUPPORTED_Pause | SUPPORTED_Asym_Pause)) {
  1488. phydev->supported &= ~(SUPPORTED_Pause | SUPPORTED_Asym_Pause);
  1489. phydev->supported |= phydrv->features &
  1490. (SUPPORTED_Pause | SUPPORTED_Asym_Pause);
  1491. } else {
  1492. phydev->supported |= SUPPORTED_Pause | SUPPORTED_Asym_Pause;
  1493. }
  1494. /* Set the state to READY by default */
  1495. phydev->state = PHY_READY;
  1496. if (phydev->drv->probe)
  1497. err = phydev->drv->probe(phydev);
  1498. mutex_unlock(&phydev->lock);
  1499. return err;
  1500. }
  1501. static int phy_remove(struct device *dev)
  1502. {
  1503. struct phy_device *phydev = to_phy_device(dev);
  1504. cancel_delayed_work_sync(&phydev->state_queue);
  1505. mutex_lock(&phydev->lock);
  1506. phydev->state = PHY_DOWN;
  1507. mutex_unlock(&phydev->lock);
  1508. if (phydev->drv && phydev->drv->remove)
  1509. phydev->drv->remove(phydev);
  1510. phydev->drv = NULL;
  1511. return 0;
  1512. }
  1513. /**
  1514. * phy_driver_register - register a phy_driver with the PHY layer
  1515. * @new_driver: new phy_driver to register
  1516. * @owner: module owning this PHY
  1517. */
  1518. int phy_driver_register(struct phy_driver *new_driver, struct module *owner)
  1519. {
  1520. int retval;
  1521. new_driver->mdiodrv.flags |= MDIO_DEVICE_IS_PHY;
  1522. new_driver->mdiodrv.driver.name = new_driver->name;
  1523. new_driver->mdiodrv.driver.bus = &mdio_bus_type;
  1524. new_driver->mdiodrv.driver.probe = phy_probe;
  1525. new_driver->mdiodrv.driver.remove = phy_remove;
  1526. new_driver->mdiodrv.driver.owner = owner;
  1527. retval = driver_register(&new_driver->mdiodrv.driver);
  1528. if (retval) {
  1529. pr_err("%s: Error %d in registering driver\n",
  1530. new_driver->name, retval);
  1531. return retval;
  1532. }
  1533. pr_debug("%s: Registered new driver\n", new_driver->name);
  1534. return 0;
  1535. }
  1536. EXPORT_SYMBOL(phy_driver_register);
  1537. int phy_drivers_register(struct phy_driver *new_driver, int n,
  1538. struct module *owner)
  1539. {
  1540. int i, ret = 0;
  1541. for (i = 0; i < n; i++) {
  1542. ret = phy_driver_register(new_driver + i, owner);
  1543. if (ret) {
  1544. while (i-- > 0)
  1545. phy_driver_unregister(new_driver + i);
  1546. break;
  1547. }
  1548. }
  1549. return ret;
  1550. }
  1551. EXPORT_SYMBOL(phy_drivers_register);
  1552. void phy_driver_unregister(struct phy_driver *drv)
  1553. {
  1554. driver_unregister(&drv->mdiodrv.driver);
  1555. }
  1556. EXPORT_SYMBOL(phy_driver_unregister);
  1557. void phy_drivers_unregister(struct phy_driver *drv, int n)
  1558. {
  1559. int i;
  1560. for (i = 0; i < n; i++)
  1561. phy_driver_unregister(drv + i);
  1562. }
  1563. EXPORT_SYMBOL(phy_drivers_unregister);
  1564. static struct phy_driver genphy_driver = {
  1565. .phy_id = 0xffffffff,
  1566. .phy_id_mask = 0xffffffff,
  1567. .name = "Generic PHY",
  1568. .soft_reset = genphy_no_soft_reset,
  1569. .config_init = genphy_config_init,
  1570. .features = PHY_GBIT_FEATURES | SUPPORTED_MII |
  1571. SUPPORTED_AUI | SUPPORTED_FIBRE |
  1572. SUPPORTED_BNC,
  1573. .config_aneg = genphy_config_aneg,
  1574. .aneg_done = genphy_aneg_done,
  1575. .read_status = genphy_read_status,
  1576. .suspend = genphy_suspend,
  1577. .resume = genphy_resume,
  1578. .set_loopback = genphy_loopback,
  1579. };
  1580. static int __init phy_init(void)
  1581. {
  1582. int rc;
  1583. rc = mdio_bus_init();
  1584. if (rc)
  1585. return rc;
  1586. rc = phy_driver_register(&genphy_10g_driver, THIS_MODULE);
  1587. if (rc)
  1588. goto err_10g;
  1589. rc = phy_driver_register(&genphy_driver, THIS_MODULE);
  1590. if (rc) {
  1591. phy_driver_unregister(&genphy_10g_driver);
  1592. err_10g:
  1593. mdio_bus_exit();
  1594. }
  1595. return rc;
  1596. }
  1597. static void __exit phy_exit(void)
  1598. {
  1599. phy_driver_unregister(&genphy_10g_driver);
  1600. phy_driver_unregister(&genphy_driver);
  1601. mdio_bus_exit();
  1602. }
  1603. subsys_initcall(phy_init);
  1604. module_exit(phy_exit);