phy_device.c 42 KB

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