phy_device.c 46 KB

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