phy_device.c 50 KB

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