phy_device.c 59 KB

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