phy_device.c 45 KB

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