phy_device.c 51 KB

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