phy_device.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738
  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 ERR_PTR(-ENODEV);
  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. put_device(d);
  611. if (rc)
  612. return ERR_PTR(rc);
  613. return phydev;
  614. }
  615. EXPORT_SYMBOL(phy_connect);
  616. /**
  617. * phy_disconnect - disable interrupts, stop state machine, and detach a PHY
  618. * device
  619. * @phydev: target phy_device struct
  620. */
  621. void phy_disconnect(struct phy_device *phydev)
  622. {
  623. if (phydev->irq > 0)
  624. phy_stop_interrupts(phydev);
  625. phy_stop_machine(phydev);
  626. phydev->adjust_link = NULL;
  627. phy_detach(phydev);
  628. }
  629. EXPORT_SYMBOL(phy_disconnect);
  630. /**
  631. * phy_poll_reset - Safely wait until a PHY reset has properly completed
  632. * @phydev: The PHY device to poll
  633. *
  634. * Description: According to IEEE 802.3, Section 2, Subsection 22.2.4.1.1, as
  635. * published in 2008, a PHY reset may take up to 0.5 seconds. The MII BMCR
  636. * register must be polled until the BMCR_RESET bit clears.
  637. *
  638. * Furthermore, any attempts to write to PHY registers may have no effect
  639. * or even generate MDIO bus errors until this is complete.
  640. *
  641. * Some PHYs (such as the Marvell 88E1111) don't entirely conform to the
  642. * standard and do not fully reset after the BMCR_RESET bit is set, and may
  643. * even *REQUIRE* a soft-reset to properly restart autonegotiation. In an
  644. * effort to support such broken PHYs, this function is separate from the
  645. * standard phy_init_hw() which will zero all the other bits in the BMCR
  646. * and reapply all driver-specific and board-specific fixups.
  647. */
  648. static int phy_poll_reset(struct phy_device *phydev)
  649. {
  650. /* Poll until the reset bit clears (50ms per retry == 0.6 sec) */
  651. unsigned int retries = 12;
  652. int ret;
  653. do {
  654. msleep(50);
  655. ret = phy_read(phydev, MII_BMCR);
  656. if (ret < 0)
  657. return ret;
  658. } while (ret & BMCR_RESET && --retries);
  659. if (ret & BMCR_RESET)
  660. return -ETIMEDOUT;
  661. /* Some chips (smsc911x) may still need up to another 1ms after the
  662. * BMCR_RESET bit is cleared before they are usable.
  663. */
  664. msleep(1);
  665. return 0;
  666. }
  667. int phy_init_hw(struct phy_device *phydev)
  668. {
  669. int ret = 0;
  670. if (!phydev->drv || !phydev->drv->config_init)
  671. return 0;
  672. if (phydev->drv->soft_reset)
  673. ret = phydev->drv->soft_reset(phydev);
  674. else
  675. ret = genphy_soft_reset(phydev);
  676. if (ret < 0)
  677. return ret;
  678. ret = phy_scan_fixups(phydev);
  679. if (ret < 0)
  680. return ret;
  681. return phydev->drv->config_init(phydev);
  682. }
  683. EXPORT_SYMBOL(phy_init_hw);
  684. void phy_attached_info(struct phy_device *phydev)
  685. {
  686. phy_attached_print(phydev, NULL);
  687. }
  688. EXPORT_SYMBOL(phy_attached_info);
  689. #define ATTACHED_FMT "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)"
  690. void phy_attached_print(struct phy_device *phydev, const char *fmt, ...)
  691. {
  692. if (!fmt) {
  693. dev_info(&phydev->mdio.dev, ATTACHED_FMT "\n",
  694. phydev->drv->name, phydev_name(phydev),
  695. phydev->irq);
  696. } else {
  697. va_list ap;
  698. dev_info(&phydev->mdio.dev, ATTACHED_FMT,
  699. phydev->drv->name, phydev_name(phydev),
  700. phydev->irq);
  701. va_start(ap, fmt);
  702. vprintk(fmt, ap);
  703. va_end(ap);
  704. }
  705. }
  706. EXPORT_SYMBOL(phy_attached_print);
  707. /**
  708. * phy_attach_direct - attach a network device to a given PHY device pointer
  709. * @dev: network device to attach
  710. * @phydev: Pointer to phy_device to attach
  711. * @flags: PHY device's dev_flags
  712. * @interface: PHY device's interface
  713. *
  714. * Description: Called by drivers to attach to a particular PHY
  715. * device. The phy_device is found, and properly hooked up
  716. * to the phy_driver. If no driver is attached, then a
  717. * generic driver is used. The phy_device is given a ptr to
  718. * the attaching device, and given a callback for link status
  719. * change. The phy_device is returned to the attaching driver.
  720. * This function takes a reference on the phy device.
  721. */
  722. int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
  723. u32 flags, phy_interface_t interface)
  724. {
  725. struct mii_bus *bus = phydev->mdio.bus;
  726. struct device *d = &phydev->mdio.dev;
  727. int err;
  728. if (!try_module_get(bus->owner)) {
  729. dev_err(&dev->dev, "failed to get the bus module\n");
  730. return -EIO;
  731. }
  732. get_device(d);
  733. /* Assume that if there is no driver, that it doesn't
  734. * exist, and we should use the genphy driver.
  735. */
  736. if (!d->driver) {
  737. if (phydev->is_c45)
  738. d->driver =
  739. &genphy_driver[GENPHY_DRV_10G].mdiodrv.driver;
  740. else
  741. d->driver =
  742. &genphy_driver[GENPHY_DRV_1G].mdiodrv.driver;
  743. err = d->driver->probe(d);
  744. if (err >= 0)
  745. err = device_bind_driver(d);
  746. if (err)
  747. goto error;
  748. }
  749. if (phydev->attached_dev) {
  750. dev_err(&dev->dev, "PHY already attached\n");
  751. err = -EBUSY;
  752. goto error;
  753. }
  754. phydev->attached_dev = dev;
  755. dev->phydev = phydev;
  756. phydev->dev_flags = flags;
  757. phydev->interface = interface;
  758. phydev->state = PHY_READY;
  759. /* Initial carrier state is off as the phy is about to be
  760. * (re)initialized.
  761. */
  762. netif_carrier_off(phydev->attached_dev);
  763. /* Do initial configuration here, now that
  764. * we have certain key parameters
  765. * (dev_flags and interface)
  766. */
  767. err = phy_init_hw(phydev);
  768. if (err)
  769. phy_detach(phydev);
  770. else
  771. phy_resume(phydev);
  772. return err;
  773. error:
  774. put_device(d);
  775. module_put(bus->owner);
  776. return err;
  777. }
  778. EXPORT_SYMBOL(phy_attach_direct);
  779. /**
  780. * phy_attach - attach a network device to a particular PHY device
  781. * @dev: network device to attach
  782. * @bus_id: Bus ID of PHY device to attach
  783. * @interface: PHY device's interface
  784. *
  785. * Description: Same as phy_attach_direct() except that a PHY bus_id
  786. * string is passed instead of a pointer to a struct phy_device.
  787. */
  788. struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
  789. phy_interface_t interface)
  790. {
  791. struct bus_type *bus = &mdio_bus_type;
  792. struct phy_device *phydev;
  793. struct device *d;
  794. int rc;
  795. /* Search the list of PHY devices on the mdio bus for the
  796. * PHY with the requested name
  797. */
  798. d = bus_find_device_by_name(bus, NULL, bus_id);
  799. if (!d) {
  800. pr_err("PHY %s not found\n", bus_id);
  801. return ERR_PTR(-ENODEV);
  802. }
  803. phydev = to_phy_device(d);
  804. rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
  805. put_device(d);
  806. if (rc)
  807. return ERR_PTR(rc);
  808. return phydev;
  809. }
  810. EXPORT_SYMBOL(phy_attach);
  811. /**
  812. * phy_detach - detach a PHY device from its network device
  813. * @phydev: target phy_device struct
  814. *
  815. * This detaches the phy device from its network device and the phy
  816. * driver, and drops the reference count taken in phy_attach_direct().
  817. */
  818. void phy_detach(struct phy_device *phydev)
  819. {
  820. struct mii_bus *bus;
  821. int i;
  822. phydev->attached_dev->phydev = NULL;
  823. phydev->attached_dev = NULL;
  824. phy_suspend(phydev);
  825. /* If the device had no specific driver before (i.e. - it
  826. * was using the generic driver), we unbind the device
  827. * from the generic driver so that there's a chance a
  828. * real driver could be loaded
  829. */
  830. for (i = 0; i < ARRAY_SIZE(genphy_driver); i++) {
  831. if (phydev->mdio.dev.driver ==
  832. &genphy_driver[i].mdiodrv.driver) {
  833. device_release_driver(&phydev->mdio.dev);
  834. break;
  835. }
  836. }
  837. /*
  838. * The phydev might go away on the put_device() below, so avoid
  839. * a use-after-free bug by reading the underlying bus first.
  840. */
  841. bus = phydev->mdio.bus;
  842. put_device(&phydev->mdio.dev);
  843. module_put(bus->owner);
  844. }
  845. EXPORT_SYMBOL(phy_detach);
  846. int phy_suspend(struct phy_device *phydev)
  847. {
  848. struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
  849. struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
  850. int ret = 0;
  851. /* If the device has WOL enabled, we cannot suspend the PHY */
  852. phy_ethtool_get_wol(phydev, &wol);
  853. if (wol.wolopts)
  854. return -EBUSY;
  855. if (phydrv->suspend)
  856. ret = phydrv->suspend(phydev);
  857. if (ret)
  858. return ret;
  859. phydev->suspended = true;
  860. return ret;
  861. }
  862. EXPORT_SYMBOL(phy_suspend);
  863. int phy_resume(struct phy_device *phydev)
  864. {
  865. struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
  866. int ret = 0;
  867. if (phydrv->resume)
  868. ret = phydrv->resume(phydev);
  869. if (ret)
  870. return ret;
  871. phydev->suspended = false;
  872. return ret;
  873. }
  874. EXPORT_SYMBOL(phy_resume);
  875. /* Generic PHY support and helper functions */
  876. /**
  877. * genphy_config_advert - sanitize and advertise auto-negotiation parameters
  878. * @phydev: target phy_device struct
  879. *
  880. * Description: Writes MII_ADVERTISE with the appropriate values,
  881. * after sanitizing the values to make sure we only advertise
  882. * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
  883. * hasn't changed, and > 0 if it has changed.
  884. */
  885. static int genphy_config_advert(struct phy_device *phydev)
  886. {
  887. u32 advertise;
  888. int oldadv, adv, bmsr;
  889. int err, changed = 0;
  890. /* Only allow advertising what this PHY supports */
  891. phydev->advertising &= phydev->supported;
  892. advertise = phydev->advertising;
  893. /* Setup standard advertisement */
  894. adv = phy_read(phydev, MII_ADVERTISE);
  895. if (adv < 0)
  896. return adv;
  897. oldadv = adv;
  898. adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
  899. ADVERTISE_PAUSE_ASYM);
  900. adv |= ethtool_adv_to_mii_adv_t(advertise);
  901. if (adv != oldadv) {
  902. err = phy_write(phydev, MII_ADVERTISE, adv);
  903. if (err < 0)
  904. return err;
  905. changed = 1;
  906. }
  907. bmsr = phy_read(phydev, MII_BMSR);
  908. if (bmsr < 0)
  909. return bmsr;
  910. /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all
  911. * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a
  912. * logical 1.
  913. */
  914. if (!(bmsr & BMSR_ESTATEN))
  915. return changed;
  916. /* Configure gigabit if it's supported */
  917. adv = phy_read(phydev, MII_CTRL1000);
  918. if (adv < 0)
  919. return adv;
  920. oldadv = adv;
  921. adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
  922. if (phydev->supported & (SUPPORTED_1000baseT_Half |
  923. SUPPORTED_1000baseT_Full)) {
  924. adv |= ethtool_adv_to_mii_ctrl1000_t(advertise);
  925. }
  926. if (adv != oldadv)
  927. changed = 1;
  928. err = phy_write(phydev, MII_CTRL1000, adv);
  929. if (err < 0)
  930. return err;
  931. return changed;
  932. }
  933. /**
  934. * genphy_setup_forced - configures/forces speed/duplex from @phydev
  935. * @phydev: target phy_device struct
  936. *
  937. * Description: Configures MII_BMCR to force speed/duplex
  938. * to the values in phydev. Assumes that the values are valid.
  939. * Please see phy_sanitize_settings().
  940. */
  941. int genphy_setup_forced(struct phy_device *phydev)
  942. {
  943. int ctl = phy_read(phydev, MII_BMCR);
  944. ctl &= BMCR_LOOPBACK | BMCR_ISOLATE | BMCR_PDOWN;
  945. phydev->pause = 0;
  946. phydev->asym_pause = 0;
  947. if (SPEED_1000 == phydev->speed)
  948. ctl |= BMCR_SPEED1000;
  949. else if (SPEED_100 == phydev->speed)
  950. ctl |= BMCR_SPEED100;
  951. if (DUPLEX_FULL == phydev->duplex)
  952. ctl |= BMCR_FULLDPLX;
  953. return phy_write(phydev, MII_BMCR, ctl);
  954. }
  955. EXPORT_SYMBOL(genphy_setup_forced);
  956. /**
  957. * genphy_restart_aneg - Enable and Restart Autonegotiation
  958. * @phydev: target phy_device struct
  959. */
  960. int genphy_restart_aneg(struct phy_device *phydev)
  961. {
  962. int ctl = phy_read(phydev, MII_BMCR);
  963. if (ctl < 0)
  964. return ctl;
  965. ctl |= BMCR_ANENABLE | BMCR_ANRESTART;
  966. /* Don't isolate the PHY if we're negotiating */
  967. ctl &= ~BMCR_ISOLATE;
  968. return phy_write(phydev, MII_BMCR, ctl);
  969. }
  970. EXPORT_SYMBOL(genphy_restart_aneg);
  971. /**
  972. * genphy_config_aneg - restart auto-negotiation or write BMCR
  973. * @phydev: target phy_device struct
  974. *
  975. * Description: If auto-negotiation is enabled, we configure the
  976. * advertising, and then restart auto-negotiation. If it is not
  977. * enabled, then we write the BMCR.
  978. */
  979. int genphy_config_aneg(struct phy_device *phydev)
  980. {
  981. int result;
  982. if (AUTONEG_ENABLE != phydev->autoneg)
  983. return genphy_setup_forced(phydev);
  984. result = genphy_config_advert(phydev);
  985. if (result < 0) /* error */
  986. return result;
  987. if (result == 0) {
  988. /* Advertisement hasn't changed, but maybe aneg was never on to
  989. * begin with? Or maybe phy was isolated?
  990. */
  991. int ctl = phy_read(phydev, MII_BMCR);
  992. if (ctl < 0)
  993. return ctl;
  994. if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
  995. result = 1; /* do restart aneg */
  996. }
  997. /* Only restart aneg if we are advertising something different
  998. * than we were before.
  999. */
  1000. if (result > 0)
  1001. result = genphy_restart_aneg(phydev);
  1002. return result;
  1003. }
  1004. EXPORT_SYMBOL(genphy_config_aneg);
  1005. /**
  1006. * genphy_aneg_done - return auto-negotiation status
  1007. * @phydev: target phy_device struct
  1008. *
  1009. * Description: Reads the status register and returns 0 either if
  1010. * auto-negotiation is incomplete, or if there was an error.
  1011. * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
  1012. */
  1013. int genphy_aneg_done(struct phy_device *phydev)
  1014. {
  1015. int retval = phy_read(phydev, MII_BMSR);
  1016. return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
  1017. }
  1018. EXPORT_SYMBOL(genphy_aneg_done);
  1019. static int gen10g_config_aneg(struct phy_device *phydev)
  1020. {
  1021. return 0;
  1022. }
  1023. /**
  1024. * genphy_update_link - update link status in @phydev
  1025. * @phydev: target phy_device struct
  1026. *
  1027. * Description: Update the value in phydev->link to reflect the
  1028. * current link value. In order to do this, we need to read
  1029. * the status register twice, keeping the second value.
  1030. */
  1031. int genphy_update_link(struct phy_device *phydev)
  1032. {
  1033. int status;
  1034. /* Do a fake read */
  1035. status = phy_read(phydev, MII_BMSR);
  1036. if (status < 0)
  1037. return status;
  1038. /* Read link and autonegotiation status */
  1039. status = phy_read(phydev, MII_BMSR);
  1040. if (status < 0)
  1041. return status;
  1042. if ((status & BMSR_LSTATUS) == 0)
  1043. phydev->link = 0;
  1044. else
  1045. phydev->link = 1;
  1046. return 0;
  1047. }
  1048. EXPORT_SYMBOL(genphy_update_link);
  1049. /**
  1050. * genphy_read_status - check the link status and update current link state
  1051. * @phydev: target phy_device struct
  1052. *
  1053. * Description: Check the link, then figure out the current state
  1054. * by comparing what we advertise with what the link partner
  1055. * advertises. Start by checking the gigabit possibilities,
  1056. * then move on to 10/100.
  1057. */
  1058. int genphy_read_status(struct phy_device *phydev)
  1059. {
  1060. int adv;
  1061. int err;
  1062. int lpa;
  1063. int lpagb = 0;
  1064. int common_adv;
  1065. int common_adv_gb = 0;
  1066. /* Update the link, but return if there was an error */
  1067. err = genphy_update_link(phydev);
  1068. if (err)
  1069. return err;
  1070. phydev->lp_advertising = 0;
  1071. if (AUTONEG_ENABLE == phydev->autoneg) {
  1072. if (phydev->supported & (SUPPORTED_1000baseT_Half
  1073. | SUPPORTED_1000baseT_Full)) {
  1074. lpagb = phy_read(phydev, MII_STAT1000);
  1075. if (lpagb < 0)
  1076. return lpagb;
  1077. adv = phy_read(phydev, MII_CTRL1000);
  1078. if (adv < 0)
  1079. return adv;
  1080. phydev->lp_advertising =
  1081. mii_stat1000_to_ethtool_lpa_t(lpagb);
  1082. common_adv_gb = lpagb & adv << 2;
  1083. }
  1084. lpa = phy_read(phydev, MII_LPA);
  1085. if (lpa < 0)
  1086. return lpa;
  1087. phydev->lp_advertising |= mii_lpa_to_ethtool_lpa_t(lpa);
  1088. adv = phy_read(phydev, MII_ADVERTISE);
  1089. if (adv < 0)
  1090. return adv;
  1091. common_adv = lpa & adv;
  1092. phydev->speed = SPEED_10;
  1093. phydev->duplex = DUPLEX_HALF;
  1094. phydev->pause = 0;
  1095. phydev->asym_pause = 0;
  1096. if (common_adv_gb & (LPA_1000FULL | LPA_1000HALF)) {
  1097. phydev->speed = SPEED_1000;
  1098. if (common_adv_gb & LPA_1000FULL)
  1099. phydev->duplex = DUPLEX_FULL;
  1100. } else if (common_adv & (LPA_100FULL | LPA_100HALF)) {
  1101. phydev->speed = SPEED_100;
  1102. if (common_adv & LPA_100FULL)
  1103. phydev->duplex = DUPLEX_FULL;
  1104. } else
  1105. if (common_adv & LPA_10FULL)
  1106. phydev->duplex = DUPLEX_FULL;
  1107. if (phydev->duplex == DUPLEX_FULL) {
  1108. phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
  1109. phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
  1110. }
  1111. } else {
  1112. int bmcr = phy_read(phydev, MII_BMCR);
  1113. if (bmcr < 0)
  1114. return bmcr;
  1115. if (bmcr & BMCR_FULLDPLX)
  1116. phydev->duplex = DUPLEX_FULL;
  1117. else
  1118. phydev->duplex = DUPLEX_HALF;
  1119. if (bmcr & BMCR_SPEED1000)
  1120. phydev->speed = SPEED_1000;
  1121. else if (bmcr & BMCR_SPEED100)
  1122. phydev->speed = SPEED_100;
  1123. else
  1124. phydev->speed = SPEED_10;
  1125. phydev->pause = 0;
  1126. phydev->asym_pause = 0;
  1127. }
  1128. return 0;
  1129. }
  1130. EXPORT_SYMBOL(genphy_read_status);
  1131. static int gen10g_read_status(struct phy_device *phydev)
  1132. {
  1133. int devad, reg;
  1134. u32 mmd_mask = phydev->c45_ids.devices_in_package;
  1135. phydev->link = 1;
  1136. /* For now just lie and say it's 10G all the time */
  1137. phydev->speed = SPEED_10000;
  1138. phydev->duplex = DUPLEX_FULL;
  1139. for (devad = 0; mmd_mask; devad++, mmd_mask = mmd_mask >> 1) {
  1140. if (!(mmd_mask & 1))
  1141. continue;
  1142. /* Read twice because link state is latched and a
  1143. * read moves the current state into the register
  1144. */
  1145. phy_read_mmd(phydev, devad, MDIO_STAT1);
  1146. reg = phy_read_mmd(phydev, devad, MDIO_STAT1);
  1147. if (reg < 0 || !(reg & MDIO_STAT1_LSTATUS))
  1148. phydev->link = 0;
  1149. }
  1150. return 0;
  1151. }
  1152. /**
  1153. * genphy_soft_reset - software reset the PHY via BMCR_RESET bit
  1154. * @phydev: target phy_device struct
  1155. *
  1156. * Description: Perform a software PHY reset using the standard
  1157. * BMCR_RESET bit and poll for the reset bit to be cleared.
  1158. *
  1159. * Returns: 0 on success, < 0 on failure
  1160. */
  1161. int genphy_soft_reset(struct phy_device *phydev)
  1162. {
  1163. int ret;
  1164. ret = phy_write(phydev, MII_BMCR, BMCR_RESET);
  1165. if (ret < 0)
  1166. return ret;
  1167. return phy_poll_reset(phydev);
  1168. }
  1169. EXPORT_SYMBOL(genphy_soft_reset);
  1170. int genphy_config_init(struct phy_device *phydev)
  1171. {
  1172. int val;
  1173. u32 features;
  1174. features = (SUPPORTED_TP | SUPPORTED_MII
  1175. | SUPPORTED_AUI | SUPPORTED_FIBRE |
  1176. SUPPORTED_BNC | SUPPORTED_Pause | SUPPORTED_Asym_Pause);
  1177. /* Do we support autonegotiation? */
  1178. val = phy_read(phydev, MII_BMSR);
  1179. if (val < 0)
  1180. return val;
  1181. if (val & BMSR_ANEGCAPABLE)
  1182. features |= SUPPORTED_Autoneg;
  1183. if (val & BMSR_100FULL)
  1184. features |= SUPPORTED_100baseT_Full;
  1185. if (val & BMSR_100HALF)
  1186. features |= SUPPORTED_100baseT_Half;
  1187. if (val & BMSR_10FULL)
  1188. features |= SUPPORTED_10baseT_Full;
  1189. if (val & BMSR_10HALF)
  1190. features |= SUPPORTED_10baseT_Half;
  1191. if (val & BMSR_ESTATEN) {
  1192. val = phy_read(phydev, MII_ESTATUS);
  1193. if (val < 0)
  1194. return val;
  1195. if (val & ESTATUS_1000_TFULL)
  1196. features |= SUPPORTED_1000baseT_Full;
  1197. if (val & ESTATUS_1000_THALF)
  1198. features |= SUPPORTED_1000baseT_Half;
  1199. }
  1200. phydev->supported &= features;
  1201. phydev->advertising &= features;
  1202. return 0;
  1203. }
  1204. static int gen10g_soft_reset(struct phy_device *phydev)
  1205. {
  1206. /* Do nothing for now */
  1207. return 0;
  1208. }
  1209. EXPORT_SYMBOL(genphy_config_init);
  1210. static int gen10g_config_init(struct phy_device *phydev)
  1211. {
  1212. /* Temporarily just say we support everything */
  1213. phydev->supported = SUPPORTED_10000baseT_Full;
  1214. phydev->advertising = SUPPORTED_10000baseT_Full;
  1215. return 0;
  1216. }
  1217. int genphy_suspend(struct phy_device *phydev)
  1218. {
  1219. int value;
  1220. mutex_lock(&phydev->lock);
  1221. value = phy_read(phydev, MII_BMCR);
  1222. phy_write(phydev, MII_BMCR, value | BMCR_PDOWN);
  1223. mutex_unlock(&phydev->lock);
  1224. return 0;
  1225. }
  1226. EXPORT_SYMBOL(genphy_suspend);
  1227. static int gen10g_suspend(struct phy_device *phydev)
  1228. {
  1229. return 0;
  1230. }
  1231. int genphy_resume(struct phy_device *phydev)
  1232. {
  1233. int value;
  1234. mutex_lock(&phydev->lock);
  1235. value = phy_read(phydev, MII_BMCR);
  1236. phy_write(phydev, MII_BMCR, value & ~BMCR_PDOWN);
  1237. mutex_unlock(&phydev->lock);
  1238. return 0;
  1239. }
  1240. EXPORT_SYMBOL(genphy_resume);
  1241. static int gen10g_resume(struct phy_device *phydev)
  1242. {
  1243. return 0;
  1244. }
  1245. static int __set_phy_supported(struct phy_device *phydev, u32 max_speed)
  1246. {
  1247. /* The default values for phydev->supported are provided by the PHY
  1248. * driver "features" member, we want to reset to sane defaults first
  1249. * before supporting higher speeds.
  1250. */
  1251. phydev->supported &= PHY_DEFAULT_FEATURES;
  1252. switch (max_speed) {
  1253. default:
  1254. return -ENOTSUPP;
  1255. case SPEED_1000:
  1256. phydev->supported |= PHY_1000BT_FEATURES;
  1257. /* fall through */
  1258. case SPEED_100:
  1259. phydev->supported |= PHY_100BT_FEATURES;
  1260. /* fall through */
  1261. case SPEED_10:
  1262. phydev->supported |= PHY_10BT_FEATURES;
  1263. }
  1264. return 0;
  1265. }
  1266. int phy_set_max_speed(struct phy_device *phydev, u32 max_speed)
  1267. {
  1268. int err;
  1269. err = __set_phy_supported(phydev, max_speed);
  1270. if (err)
  1271. return err;
  1272. phydev->advertising = phydev->supported;
  1273. return 0;
  1274. }
  1275. EXPORT_SYMBOL(phy_set_max_speed);
  1276. static void of_set_phy_supported(struct phy_device *phydev)
  1277. {
  1278. struct device_node *node = phydev->mdio.dev.of_node;
  1279. u32 max_speed;
  1280. if (!IS_ENABLED(CONFIG_OF_MDIO))
  1281. return;
  1282. if (!node)
  1283. return;
  1284. if (!of_property_read_u32(node, "max-speed", &max_speed))
  1285. __set_phy_supported(phydev, max_speed);
  1286. }
  1287. /**
  1288. * phy_probe - probe and init a PHY device
  1289. * @dev: device to probe and init
  1290. *
  1291. * Description: Take care of setting up the phy_device structure,
  1292. * set the state to READY (the driver's init function should
  1293. * set it to STARTING if needed).
  1294. */
  1295. static int phy_probe(struct device *dev)
  1296. {
  1297. struct phy_device *phydev = to_phy_device(dev);
  1298. struct device_driver *drv = phydev->mdio.dev.driver;
  1299. struct phy_driver *phydrv = to_phy_driver(drv);
  1300. int err = 0;
  1301. phydev->drv = phydrv;
  1302. /* Disable the interrupt if the PHY doesn't support it
  1303. * but the interrupt is still a valid one
  1304. */
  1305. if (!(phydrv->flags & PHY_HAS_INTERRUPT) &&
  1306. phy_interrupt_is_valid(phydev))
  1307. phydev->irq = PHY_POLL;
  1308. if (phydrv->flags & PHY_IS_INTERNAL)
  1309. phydev->is_internal = true;
  1310. mutex_lock(&phydev->lock);
  1311. /* Start out supporting everything. Eventually,
  1312. * a controller will attach, and may modify one
  1313. * or both of these values
  1314. */
  1315. phydev->supported = phydrv->features;
  1316. of_set_phy_supported(phydev);
  1317. phydev->advertising = phydev->supported;
  1318. /* Set the state to READY by default */
  1319. phydev->state = PHY_READY;
  1320. if (phydev->drv->probe)
  1321. err = phydev->drv->probe(phydev);
  1322. mutex_unlock(&phydev->lock);
  1323. return err;
  1324. }
  1325. static int phy_remove(struct device *dev)
  1326. {
  1327. struct phy_device *phydev = to_phy_device(dev);
  1328. mutex_lock(&phydev->lock);
  1329. phydev->state = PHY_DOWN;
  1330. mutex_unlock(&phydev->lock);
  1331. if (phydev->drv->remove)
  1332. phydev->drv->remove(phydev);
  1333. phydev->drv = NULL;
  1334. return 0;
  1335. }
  1336. /**
  1337. * phy_driver_register - register a phy_driver with the PHY layer
  1338. * @new_driver: new phy_driver to register
  1339. * @owner: module owning this PHY
  1340. */
  1341. int phy_driver_register(struct phy_driver *new_driver, struct module *owner)
  1342. {
  1343. int retval;
  1344. new_driver->mdiodrv.flags |= MDIO_DEVICE_IS_PHY;
  1345. new_driver->mdiodrv.driver.name = new_driver->name;
  1346. new_driver->mdiodrv.driver.bus = &mdio_bus_type;
  1347. new_driver->mdiodrv.driver.probe = phy_probe;
  1348. new_driver->mdiodrv.driver.remove = phy_remove;
  1349. new_driver->mdiodrv.driver.owner = owner;
  1350. retval = driver_register(&new_driver->mdiodrv.driver);
  1351. if (retval) {
  1352. pr_err("%s: Error %d in registering driver\n",
  1353. new_driver->name, retval);
  1354. return retval;
  1355. }
  1356. pr_debug("%s: Registered new driver\n", new_driver->name);
  1357. return 0;
  1358. }
  1359. EXPORT_SYMBOL(phy_driver_register);
  1360. int phy_drivers_register(struct phy_driver *new_driver, int n,
  1361. struct module *owner)
  1362. {
  1363. int i, ret = 0;
  1364. for (i = 0; i < n; i++) {
  1365. ret = phy_driver_register(new_driver + i, owner);
  1366. if (ret) {
  1367. while (i-- > 0)
  1368. phy_driver_unregister(new_driver + i);
  1369. break;
  1370. }
  1371. }
  1372. return ret;
  1373. }
  1374. EXPORT_SYMBOL(phy_drivers_register);
  1375. void phy_driver_unregister(struct phy_driver *drv)
  1376. {
  1377. driver_unregister(&drv->mdiodrv.driver);
  1378. }
  1379. EXPORT_SYMBOL(phy_driver_unregister);
  1380. void phy_drivers_unregister(struct phy_driver *drv, int n)
  1381. {
  1382. int i;
  1383. for (i = 0; i < n; i++)
  1384. phy_driver_unregister(drv + i);
  1385. }
  1386. EXPORT_SYMBOL(phy_drivers_unregister);
  1387. static struct phy_driver genphy_driver[] = {
  1388. {
  1389. .phy_id = 0xffffffff,
  1390. .phy_id_mask = 0xffffffff,
  1391. .name = "Generic PHY",
  1392. .soft_reset = genphy_soft_reset,
  1393. .config_init = genphy_config_init,
  1394. .features = PHY_GBIT_FEATURES | SUPPORTED_MII |
  1395. SUPPORTED_AUI | SUPPORTED_FIBRE |
  1396. SUPPORTED_BNC,
  1397. .config_aneg = genphy_config_aneg,
  1398. .aneg_done = genphy_aneg_done,
  1399. .read_status = genphy_read_status,
  1400. .suspend = genphy_suspend,
  1401. .resume = genphy_resume,
  1402. }, {
  1403. .phy_id = 0xffffffff,
  1404. .phy_id_mask = 0xffffffff,
  1405. .name = "Generic 10G PHY",
  1406. .soft_reset = gen10g_soft_reset,
  1407. .config_init = gen10g_config_init,
  1408. .features = 0,
  1409. .config_aneg = gen10g_config_aneg,
  1410. .read_status = gen10g_read_status,
  1411. .suspend = gen10g_suspend,
  1412. .resume = gen10g_resume,
  1413. } };
  1414. static int __init phy_init(void)
  1415. {
  1416. int rc;
  1417. rc = mdio_bus_init();
  1418. if (rc)
  1419. return rc;
  1420. rc = phy_drivers_register(genphy_driver,
  1421. ARRAY_SIZE(genphy_driver), THIS_MODULE);
  1422. if (rc)
  1423. mdio_bus_exit();
  1424. return rc;
  1425. }
  1426. static void __exit phy_exit(void)
  1427. {
  1428. phy_drivers_unregister(genphy_driver,
  1429. ARRAY_SIZE(genphy_driver));
  1430. mdio_bus_exit();
  1431. }
  1432. subsys_initcall(phy_init);
  1433. module_exit(phy_exit);