phy_device.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390
  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->dev);
  42. }
  43. EXPORT_SYMBOL(phy_device_free);
  44. static void phy_device_release(struct device *dev)
  45. {
  46. kfree(to_phy_device(dev));
  47. }
  48. enum genphy_driver {
  49. GENPHY_DRV_1G,
  50. GENPHY_DRV_10G,
  51. GENPHY_DRV_MAX
  52. };
  53. static struct phy_driver genphy_driver[GENPHY_DRV_MAX];
  54. static LIST_HEAD(phy_fixup_list);
  55. static DEFINE_MUTEX(phy_fixup_lock);
  56. /**
  57. * phy_register_fixup - creates a new phy_fixup and adds it to the list
  58. * @bus_id: A string which matches phydev->dev.bus_id (or PHY_ANY_ID)
  59. * @phy_uid: Used to match against phydev->phy_id (the UID of the PHY)
  60. * It can also be PHY_ANY_UID
  61. * @phy_uid_mask: Applied to phydev->phy_id and fixup->phy_uid before
  62. * comparison
  63. * @run: The actual code to be run when a matching PHY is found
  64. */
  65. int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
  66. int (*run)(struct phy_device *))
  67. {
  68. struct phy_fixup *fixup = kzalloc(sizeof(*fixup), GFP_KERNEL);
  69. if (!fixup)
  70. return -ENOMEM;
  71. strlcpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id));
  72. fixup->phy_uid = phy_uid;
  73. fixup->phy_uid_mask = phy_uid_mask;
  74. fixup->run = run;
  75. mutex_lock(&phy_fixup_lock);
  76. list_add_tail(&fixup->list, &phy_fixup_list);
  77. mutex_unlock(&phy_fixup_lock);
  78. return 0;
  79. }
  80. EXPORT_SYMBOL(phy_register_fixup);
  81. /* Registers a fixup to be run on any PHY with the UID in phy_uid */
  82. int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
  83. int (*run)(struct phy_device *))
  84. {
  85. return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run);
  86. }
  87. EXPORT_SYMBOL(phy_register_fixup_for_uid);
  88. /* Registers a fixup to be run on the PHY with id string bus_id */
  89. int phy_register_fixup_for_id(const char *bus_id,
  90. int (*run)(struct phy_device *))
  91. {
  92. return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run);
  93. }
  94. EXPORT_SYMBOL(phy_register_fixup_for_id);
  95. /* Returns 1 if fixup matches phydev in bus_id and phy_uid.
  96. * Fixups can be set to match any in one or more fields.
  97. */
  98. static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup)
  99. {
  100. if (strcmp(fixup->bus_id, dev_name(&phydev->dev)) != 0)
  101. if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0)
  102. return 0;
  103. if ((fixup->phy_uid & fixup->phy_uid_mask) !=
  104. (phydev->phy_id & fixup->phy_uid_mask))
  105. if (fixup->phy_uid != PHY_ANY_UID)
  106. return 0;
  107. return 1;
  108. }
  109. /* Runs any matching fixups for this phydev */
  110. static int phy_scan_fixups(struct phy_device *phydev)
  111. {
  112. struct phy_fixup *fixup;
  113. mutex_lock(&phy_fixup_lock);
  114. list_for_each_entry(fixup, &phy_fixup_list, list) {
  115. if (phy_needs_fixup(phydev, fixup)) {
  116. int err = fixup->run(phydev);
  117. if (err < 0) {
  118. mutex_unlock(&phy_fixup_lock);
  119. return err;
  120. }
  121. phydev->has_fixups = true;
  122. }
  123. }
  124. mutex_unlock(&phy_fixup_lock);
  125. return 0;
  126. }
  127. struct phy_device *phy_device_create(struct mii_bus *bus, int addr, int phy_id,
  128. bool is_c45,
  129. struct phy_c45_device_ids *c45_ids)
  130. {
  131. struct phy_device *dev;
  132. /* We allocate the device, and initialize the default values */
  133. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  134. if (NULL == dev)
  135. return (struct phy_device *)PTR_ERR((void *)-ENOMEM);
  136. dev->dev.release = phy_device_release;
  137. dev->speed = 0;
  138. dev->duplex = -1;
  139. dev->pause = 0;
  140. dev->asym_pause = 0;
  141. dev->link = 1;
  142. dev->interface = PHY_INTERFACE_MODE_GMII;
  143. dev->autoneg = AUTONEG_ENABLE;
  144. dev->is_c45 = is_c45;
  145. dev->addr = addr;
  146. dev->phy_id = phy_id;
  147. if (c45_ids)
  148. dev->c45_ids = *c45_ids;
  149. dev->bus = bus;
  150. dev->dev.parent = bus->parent;
  151. dev->dev.bus = &mdio_bus_type;
  152. dev->irq = bus->irq != NULL ? bus->irq[addr] : PHY_POLL;
  153. dev_set_name(&dev->dev, PHY_ID_FMT, bus->id, addr);
  154. dev->state = PHY_DOWN;
  155. mutex_init(&dev->lock);
  156. INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine);
  157. INIT_WORK(&dev->phy_queue, phy_change);
  158. /* Request the appropriate module unconditionally; don't
  159. * bother trying to do so only if it isn't already loaded,
  160. * because that gets complicated. A hotplug event would have
  161. * done an unconditional modprobe anyway.
  162. * We don't do normal hotplug because it won't work for MDIO
  163. * -- because it relies on the device staying around for long
  164. * enough for the driver to get loaded. With MDIO, the NIC
  165. * driver will get bored and give up as soon as it finds that
  166. * there's no driver _already_ loaded.
  167. */
  168. request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT, MDIO_ID_ARGS(phy_id));
  169. device_initialize(&dev->dev);
  170. return dev;
  171. }
  172. EXPORT_SYMBOL(phy_device_create);
  173. /**
  174. * get_phy_c45_ids - reads the specified addr for its 802.3-c45 IDs.
  175. * @bus: the target MII bus
  176. * @addr: PHY address on the MII bus
  177. * @phy_id: where to store the ID retrieved.
  178. * @c45_ids: where to store the c45 ID information.
  179. *
  180. * If the PHY devices-in-package appears to be valid, it and the
  181. * corresponding identifiers are stored in @c45_ids, zero is stored
  182. * in @phy_id. Otherwise 0xffffffff is stored in @phy_id. Returns
  183. * zero on success.
  184. *
  185. */
  186. static int get_phy_c45_ids(struct mii_bus *bus, int addr, u32 *phy_id,
  187. struct phy_c45_device_ids *c45_ids) {
  188. int phy_reg;
  189. int i, reg_addr;
  190. const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
  191. /* Find first non-zero Devices In package. Device
  192. * zero is reserved, so don't probe it.
  193. */
  194. for (i = 1;
  195. i < num_ids && c45_ids->devices_in_package == 0;
  196. i++) {
  197. reg_addr = MII_ADDR_C45 | i << 16 | 6;
  198. phy_reg = mdiobus_read(bus, addr, reg_addr);
  199. if (phy_reg < 0)
  200. return -EIO;
  201. c45_ids->devices_in_package = (phy_reg & 0xffff) << 16;
  202. reg_addr = MII_ADDR_C45 | i << 16 | 5;
  203. phy_reg = mdiobus_read(bus, addr, reg_addr);
  204. if (phy_reg < 0)
  205. return -EIO;
  206. c45_ids->devices_in_package |= (phy_reg & 0xffff);
  207. /* If mostly Fs, there is no device there,
  208. * let's get out of here.
  209. */
  210. if ((c45_ids->devices_in_package & 0x1fffffff) == 0x1fffffff) {
  211. *phy_id = 0xffffffff;
  212. return 0;
  213. }
  214. }
  215. /* Now probe Device Identifiers for each device present. */
  216. for (i = 1; i < num_ids; i++) {
  217. if (!(c45_ids->devices_in_package & (1 << i)))
  218. continue;
  219. reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID1;
  220. phy_reg = mdiobus_read(bus, addr, reg_addr);
  221. if (phy_reg < 0)
  222. return -EIO;
  223. c45_ids->device_ids[i] = (phy_reg & 0xffff) << 16;
  224. reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID2;
  225. phy_reg = mdiobus_read(bus, addr, reg_addr);
  226. if (phy_reg < 0)
  227. return -EIO;
  228. c45_ids->device_ids[i] |= (phy_reg & 0xffff);
  229. }
  230. *phy_id = 0;
  231. return 0;
  232. }
  233. /**
  234. * get_phy_id - reads the specified addr for its ID.
  235. * @bus: the target MII bus
  236. * @addr: PHY address on the MII bus
  237. * @phy_id: where to store the ID retrieved.
  238. * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
  239. * @c45_ids: where to store the c45 ID information.
  240. *
  241. * Description: In the case of a 802.3-c22 PHY, reads the ID registers
  242. * of the PHY at @addr on the @bus, stores it in @phy_id and returns
  243. * zero on success.
  244. *
  245. * In the case of a 802.3-c45 PHY, get_phy_c45_ids() is invoked, and
  246. * its return value is in turn returned.
  247. *
  248. */
  249. static int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id,
  250. bool is_c45, struct phy_c45_device_ids *c45_ids)
  251. {
  252. int phy_reg;
  253. if (is_c45)
  254. return get_phy_c45_ids(bus, addr, phy_id, c45_ids);
  255. /* Grab the bits from PHYIR1, and put them in the upper half */
  256. phy_reg = mdiobus_read(bus, addr, MII_PHYSID1);
  257. if (phy_reg < 0)
  258. return -EIO;
  259. *phy_id = (phy_reg & 0xffff) << 16;
  260. /* Grab the bits from PHYIR2, and put them in the lower half */
  261. phy_reg = mdiobus_read(bus, addr, MII_PHYSID2);
  262. if (phy_reg < 0)
  263. return -EIO;
  264. *phy_id |= (phy_reg & 0xffff);
  265. return 0;
  266. }
  267. /**
  268. * get_phy_device - reads the specified PHY device and returns its @phy_device
  269. * struct
  270. * @bus: the target MII bus
  271. * @addr: PHY address on the MII bus
  272. * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
  273. *
  274. * Description: Reads the ID registers of the PHY at @addr on the
  275. * @bus, then allocates and returns the phy_device to represent it.
  276. */
  277. struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45)
  278. {
  279. struct phy_c45_device_ids c45_ids = {0};
  280. u32 phy_id = 0;
  281. int r;
  282. r = get_phy_id(bus, addr, &phy_id, is_c45, &c45_ids);
  283. if (r)
  284. return ERR_PTR(r);
  285. /* If the phy_id is mostly Fs, there is no device there */
  286. if ((phy_id & 0x1fffffff) == 0x1fffffff)
  287. return NULL;
  288. return phy_device_create(bus, addr, phy_id, is_c45, &c45_ids);
  289. }
  290. EXPORT_SYMBOL(get_phy_device);
  291. /**
  292. * phy_device_register - Register the phy device on the MDIO bus
  293. * @phydev: phy_device structure to be added to the MDIO bus
  294. */
  295. int phy_device_register(struct phy_device *phydev)
  296. {
  297. int err;
  298. /* Don't register a phy if one is already registered at this address */
  299. if (phydev->bus->phy_map[phydev->addr])
  300. return -EINVAL;
  301. phydev->bus->phy_map[phydev->addr] = phydev;
  302. /* Run all of the fixups for this PHY */
  303. err = phy_scan_fixups(phydev);
  304. if (err) {
  305. pr_err("PHY %d failed to initialize\n", phydev->addr);
  306. goto out;
  307. }
  308. err = device_add(&phydev->dev);
  309. if (err) {
  310. pr_err("PHY %d failed to add\n", phydev->addr);
  311. goto out;
  312. }
  313. return 0;
  314. out:
  315. phydev->bus->phy_map[phydev->addr] = NULL;
  316. return err;
  317. }
  318. EXPORT_SYMBOL(phy_device_register);
  319. /**
  320. * phy_find_first - finds the first PHY device on the bus
  321. * @bus: the target MII bus
  322. */
  323. struct phy_device *phy_find_first(struct mii_bus *bus)
  324. {
  325. int addr;
  326. for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
  327. if (bus->phy_map[addr])
  328. return bus->phy_map[addr];
  329. }
  330. return NULL;
  331. }
  332. EXPORT_SYMBOL(phy_find_first);
  333. /**
  334. * phy_prepare_link - prepares the PHY layer to monitor link status
  335. * @phydev: target phy_device struct
  336. * @handler: callback function for link status change notifications
  337. *
  338. * Description: Tells the PHY infrastructure to handle the
  339. * gory details on monitoring link status (whether through
  340. * polling or an interrupt), and to call back to the
  341. * connected device driver when the link status changes.
  342. * If you want to monitor your own link state, don't call
  343. * this function.
  344. */
  345. static void phy_prepare_link(struct phy_device *phydev,
  346. void (*handler)(struct net_device *))
  347. {
  348. phydev->adjust_link = handler;
  349. }
  350. /**
  351. * phy_connect_direct - connect an ethernet device to a specific phy_device
  352. * @dev: the network device to connect
  353. * @phydev: the pointer to the phy device
  354. * @handler: callback function for state change notifications
  355. * @interface: PHY device's interface
  356. */
  357. int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
  358. void (*handler)(struct net_device *),
  359. phy_interface_t interface)
  360. {
  361. int rc;
  362. rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
  363. if (rc)
  364. return rc;
  365. phy_prepare_link(phydev, handler);
  366. phy_start_machine(phydev);
  367. if (phydev->irq > 0)
  368. phy_start_interrupts(phydev);
  369. return 0;
  370. }
  371. EXPORT_SYMBOL(phy_connect_direct);
  372. /**
  373. * phy_connect - connect an ethernet device to a PHY device
  374. * @dev: the network device to connect
  375. * @bus_id: the id string of the PHY device to connect
  376. * @handler: callback function for state change notifications
  377. * @interface: PHY device's interface
  378. *
  379. * Description: Convenience function for connecting ethernet
  380. * devices to PHY devices. The default behavior is for
  381. * the PHY infrastructure to handle everything, and only notify
  382. * the connected driver when the link status changes. If you
  383. * don't want, or can't use the provided functionality, you may
  384. * choose to call only the subset of functions which provide
  385. * the desired functionality.
  386. */
  387. struct phy_device *phy_connect(struct net_device *dev, const char *bus_id,
  388. void (*handler)(struct net_device *),
  389. phy_interface_t interface)
  390. {
  391. struct phy_device *phydev;
  392. struct device *d;
  393. int rc;
  394. /* Search the list of PHY devices on the mdio bus for the
  395. * PHY with the requested name
  396. */
  397. d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id);
  398. if (!d) {
  399. pr_err("PHY %s not found\n", bus_id);
  400. return ERR_PTR(-ENODEV);
  401. }
  402. phydev = to_phy_device(d);
  403. rc = phy_connect_direct(dev, phydev, handler, interface);
  404. if (rc)
  405. return ERR_PTR(rc);
  406. return phydev;
  407. }
  408. EXPORT_SYMBOL(phy_connect);
  409. /**
  410. * phy_disconnect - disable interrupts, stop state machine, and detach a PHY
  411. * device
  412. * @phydev: target phy_device struct
  413. */
  414. void phy_disconnect(struct phy_device *phydev)
  415. {
  416. if (phydev->irq > 0)
  417. phy_stop_interrupts(phydev);
  418. phy_stop_machine(phydev);
  419. phydev->adjust_link = NULL;
  420. phy_detach(phydev);
  421. }
  422. EXPORT_SYMBOL(phy_disconnect);
  423. /**
  424. * phy_poll_reset - Safely wait until a PHY reset has properly completed
  425. * @phydev: The PHY device to poll
  426. *
  427. * Description: According to IEEE 802.3, Section 2, Subsection 22.2.4.1.1, as
  428. * published in 2008, a PHY reset may take up to 0.5 seconds. The MII BMCR
  429. * register must be polled until the BMCR_RESET bit clears.
  430. *
  431. * Furthermore, any attempts to write to PHY registers may have no effect
  432. * or even generate MDIO bus errors until this is complete.
  433. *
  434. * Some PHYs (such as the Marvell 88E1111) don't entirely conform to the
  435. * standard and do not fully reset after the BMCR_RESET bit is set, and may
  436. * even *REQUIRE* a soft-reset to properly restart autonegotiation. In an
  437. * effort to support such broken PHYs, this function is separate from the
  438. * standard phy_init_hw() which will zero all the other bits in the BMCR
  439. * and reapply all driver-specific and board-specific fixups.
  440. */
  441. static int phy_poll_reset(struct phy_device *phydev)
  442. {
  443. /* Poll until the reset bit clears (50ms per retry == 0.6 sec) */
  444. unsigned int retries = 12;
  445. int ret;
  446. do {
  447. msleep(50);
  448. ret = phy_read(phydev, MII_BMCR);
  449. if (ret < 0)
  450. return ret;
  451. } while (ret & BMCR_RESET && --retries);
  452. if (ret & BMCR_RESET)
  453. return -ETIMEDOUT;
  454. /* Some chips (smsc911x) may still need up to another 1ms after the
  455. * BMCR_RESET bit is cleared before they are usable.
  456. */
  457. msleep(1);
  458. return 0;
  459. }
  460. int phy_init_hw(struct phy_device *phydev)
  461. {
  462. int ret = 0;
  463. if (!phydev->drv || !phydev->drv->config_init)
  464. return 0;
  465. if (phydev->drv->soft_reset)
  466. ret = phydev->drv->soft_reset(phydev);
  467. else
  468. ret = genphy_soft_reset(phydev);
  469. if (ret < 0)
  470. return ret;
  471. ret = phy_scan_fixups(phydev);
  472. if (ret < 0)
  473. return ret;
  474. return phydev->drv->config_init(phydev);
  475. }
  476. EXPORT_SYMBOL(phy_init_hw);
  477. /**
  478. * phy_attach_direct - attach a network device to a given PHY device pointer
  479. * @dev: network device to attach
  480. * @phydev: Pointer to phy_device to attach
  481. * @flags: PHY device's dev_flags
  482. * @interface: PHY device's interface
  483. *
  484. * Description: Called by drivers to attach to a particular PHY
  485. * device. The phy_device is found, and properly hooked up
  486. * to the phy_driver. If no driver is attached, then a
  487. * generic driver is used. The phy_device is given a ptr to
  488. * the attaching device, and given a callback for link status
  489. * change. The phy_device is returned to the attaching driver.
  490. */
  491. int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
  492. u32 flags, phy_interface_t interface)
  493. {
  494. struct device *d = &phydev->dev;
  495. struct module *bus_module;
  496. int err;
  497. /* Assume that if there is no driver, that it doesn't
  498. * exist, and we should use the genphy driver.
  499. */
  500. if (NULL == d->driver) {
  501. if (phydev->is_c45)
  502. d->driver = &genphy_driver[GENPHY_DRV_10G].driver;
  503. else
  504. d->driver = &genphy_driver[GENPHY_DRV_1G].driver;
  505. err = d->driver->probe(d);
  506. if (err >= 0)
  507. err = device_bind_driver(d);
  508. if (err)
  509. return err;
  510. }
  511. if (phydev->attached_dev) {
  512. dev_err(&dev->dev, "PHY already attached\n");
  513. return -EBUSY;
  514. }
  515. /* Increment the bus module reference count */
  516. bus_module = phydev->bus->dev.driver ?
  517. phydev->bus->dev.driver->owner : NULL;
  518. if (!try_module_get(bus_module)) {
  519. dev_err(&dev->dev, "failed to get the bus module\n");
  520. return -EIO;
  521. }
  522. phydev->attached_dev = dev;
  523. dev->phydev = phydev;
  524. phydev->dev_flags = flags;
  525. phydev->interface = interface;
  526. phydev->state = PHY_READY;
  527. /* Do initial configuration here, now that
  528. * we have certain key parameters
  529. * (dev_flags and interface)
  530. */
  531. err = phy_init_hw(phydev);
  532. if (err)
  533. phy_detach(phydev);
  534. else
  535. phy_resume(phydev);
  536. return err;
  537. }
  538. EXPORT_SYMBOL(phy_attach_direct);
  539. /**
  540. * phy_attach - attach a network device to a particular PHY device
  541. * @dev: network device to attach
  542. * @bus_id: Bus ID of PHY device to attach
  543. * @interface: PHY device's interface
  544. *
  545. * Description: Same as phy_attach_direct() except that a PHY bus_id
  546. * string is passed instead of a pointer to a struct phy_device.
  547. */
  548. struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
  549. phy_interface_t interface)
  550. {
  551. struct bus_type *bus = &mdio_bus_type;
  552. struct phy_device *phydev;
  553. struct device *d;
  554. int rc;
  555. /* Search the list of PHY devices on the mdio bus for the
  556. * PHY with the requested name
  557. */
  558. d = bus_find_device_by_name(bus, NULL, bus_id);
  559. if (!d) {
  560. pr_err("PHY %s not found\n", bus_id);
  561. return ERR_PTR(-ENODEV);
  562. }
  563. phydev = to_phy_device(d);
  564. rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
  565. if (rc)
  566. return ERR_PTR(rc);
  567. return phydev;
  568. }
  569. EXPORT_SYMBOL(phy_attach);
  570. /**
  571. * phy_detach - detach a PHY device from its network device
  572. * @phydev: target phy_device struct
  573. */
  574. void phy_detach(struct phy_device *phydev)
  575. {
  576. int i;
  577. if (phydev->bus->dev.driver)
  578. module_put(phydev->bus->dev.driver->owner);
  579. phydev->attached_dev->phydev = NULL;
  580. phydev->attached_dev = NULL;
  581. phy_suspend(phydev);
  582. /* If the device had no specific driver before (i.e. - it
  583. * was using the generic driver), we unbind the device
  584. * from the generic driver so that there's a chance a
  585. * real driver could be loaded
  586. */
  587. for (i = 0; i < ARRAY_SIZE(genphy_driver); i++) {
  588. if (phydev->dev.driver == &genphy_driver[i].driver) {
  589. device_release_driver(&phydev->dev);
  590. break;
  591. }
  592. }
  593. }
  594. EXPORT_SYMBOL(phy_detach);
  595. int phy_suspend(struct phy_device *phydev)
  596. {
  597. struct phy_driver *phydrv = to_phy_driver(phydev->dev.driver);
  598. struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
  599. /* If the device has WOL enabled, we cannot suspend the PHY */
  600. phy_ethtool_get_wol(phydev, &wol);
  601. if (wol.wolopts)
  602. return -EBUSY;
  603. if (phydrv->suspend)
  604. return phydrv->suspend(phydev);
  605. return 0;
  606. }
  607. int phy_resume(struct phy_device *phydev)
  608. {
  609. struct phy_driver *phydrv = to_phy_driver(phydev->dev.driver);
  610. if (phydrv->resume)
  611. return phydrv->resume(phydev);
  612. return 0;
  613. }
  614. /* Generic PHY support and helper functions */
  615. /**
  616. * genphy_config_advert - sanitize and advertise auto-negotiation parameters
  617. * @phydev: target phy_device struct
  618. *
  619. * Description: Writes MII_ADVERTISE with the appropriate values,
  620. * after sanitizing the values to make sure we only advertise
  621. * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
  622. * hasn't changed, and > 0 if it has changed.
  623. */
  624. static int genphy_config_advert(struct phy_device *phydev)
  625. {
  626. u32 advertise;
  627. int oldadv, adv, bmsr;
  628. int err, changed = 0;
  629. /* Only allow advertising what this PHY supports */
  630. phydev->advertising &= phydev->supported;
  631. advertise = phydev->advertising;
  632. /* Setup standard advertisement */
  633. adv = phy_read(phydev, MII_ADVERTISE);
  634. if (adv < 0)
  635. return adv;
  636. oldadv = adv;
  637. adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
  638. ADVERTISE_PAUSE_ASYM);
  639. adv |= ethtool_adv_to_mii_adv_t(advertise);
  640. if (adv != oldadv) {
  641. err = phy_write(phydev, MII_ADVERTISE, adv);
  642. if (err < 0)
  643. return err;
  644. changed = 1;
  645. }
  646. bmsr = phy_read(phydev, MII_BMSR);
  647. if (bmsr < 0)
  648. return bmsr;
  649. /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all
  650. * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a
  651. * logical 1.
  652. */
  653. if (!(bmsr & BMSR_ESTATEN))
  654. return changed;
  655. /* Configure gigabit if it's supported */
  656. adv = phy_read(phydev, MII_CTRL1000);
  657. if (adv < 0)
  658. return adv;
  659. oldadv = adv;
  660. adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
  661. if (phydev->supported & (SUPPORTED_1000baseT_Half |
  662. SUPPORTED_1000baseT_Full)) {
  663. adv |= ethtool_adv_to_mii_ctrl1000_t(advertise);
  664. if (adv != oldadv)
  665. changed = 1;
  666. }
  667. err = phy_write(phydev, MII_CTRL1000, adv);
  668. if (err < 0)
  669. return err;
  670. return changed;
  671. }
  672. /**
  673. * genphy_setup_forced - configures/forces speed/duplex from @phydev
  674. * @phydev: target phy_device struct
  675. *
  676. * Description: Configures MII_BMCR to force speed/duplex
  677. * to the values in phydev. Assumes that the values are valid.
  678. * Please see phy_sanitize_settings().
  679. */
  680. int genphy_setup_forced(struct phy_device *phydev)
  681. {
  682. int ctl = 0;
  683. phydev->pause = 0;
  684. phydev->asym_pause = 0;
  685. if (SPEED_1000 == phydev->speed)
  686. ctl |= BMCR_SPEED1000;
  687. else if (SPEED_100 == phydev->speed)
  688. ctl |= BMCR_SPEED100;
  689. if (DUPLEX_FULL == phydev->duplex)
  690. ctl |= BMCR_FULLDPLX;
  691. return phy_write(phydev, MII_BMCR, ctl);
  692. }
  693. EXPORT_SYMBOL(genphy_setup_forced);
  694. /**
  695. * genphy_restart_aneg - Enable and Restart Autonegotiation
  696. * @phydev: target phy_device struct
  697. */
  698. int genphy_restart_aneg(struct phy_device *phydev)
  699. {
  700. int ctl = phy_read(phydev, MII_BMCR);
  701. if (ctl < 0)
  702. return ctl;
  703. ctl |= BMCR_ANENABLE | BMCR_ANRESTART;
  704. /* Don't isolate the PHY if we're negotiating */
  705. ctl &= ~BMCR_ISOLATE;
  706. return phy_write(phydev, MII_BMCR, ctl);
  707. }
  708. EXPORT_SYMBOL(genphy_restart_aneg);
  709. /**
  710. * genphy_config_aneg - restart auto-negotiation or write BMCR
  711. * @phydev: target phy_device struct
  712. *
  713. * Description: If auto-negotiation is enabled, we configure the
  714. * advertising, and then restart auto-negotiation. If it is not
  715. * enabled, then we write the BMCR.
  716. */
  717. int genphy_config_aneg(struct phy_device *phydev)
  718. {
  719. int result;
  720. if (AUTONEG_ENABLE != phydev->autoneg)
  721. return genphy_setup_forced(phydev);
  722. result = genphy_config_advert(phydev);
  723. if (result < 0) /* error */
  724. return result;
  725. if (result == 0) {
  726. /* Advertisement hasn't changed, but maybe aneg was never on to
  727. * begin with? Or maybe phy was isolated?
  728. */
  729. int ctl = phy_read(phydev, MII_BMCR);
  730. if (ctl < 0)
  731. return ctl;
  732. if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
  733. result = 1; /* do restart aneg */
  734. }
  735. /* Only restart aneg if we are advertising something different
  736. * than we were before.
  737. */
  738. if (result > 0)
  739. result = genphy_restart_aneg(phydev);
  740. return result;
  741. }
  742. EXPORT_SYMBOL(genphy_config_aneg);
  743. /**
  744. * genphy_aneg_done - return auto-negotiation status
  745. * @phydev: target phy_device struct
  746. *
  747. * Description: Reads the status register and returns 0 either if
  748. * auto-negotiation is incomplete, or if there was an error.
  749. * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
  750. */
  751. int genphy_aneg_done(struct phy_device *phydev)
  752. {
  753. int retval = phy_read(phydev, MII_BMSR);
  754. return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
  755. }
  756. EXPORT_SYMBOL(genphy_aneg_done);
  757. static int gen10g_config_aneg(struct phy_device *phydev)
  758. {
  759. return 0;
  760. }
  761. /**
  762. * genphy_update_link - update link status in @phydev
  763. * @phydev: target phy_device struct
  764. *
  765. * Description: Update the value in phydev->link to reflect the
  766. * current link value. In order to do this, we need to read
  767. * the status register twice, keeping the second value.
  768. */
  769. int genphy_update_link(struct phy_device *phydev)
  770. {
  771. int status;
  772. /* Do a fake read */
  773. status = phy_read(phydev, MII_BMSR);
  774. if (status < 0)
  775. return status;
  776. /* Read link and autonegotiation status */
  777. status = phy_read(phydev, MII_BMSR);
  778. if (status < 0)
  779. return status;
  780. if ((status & BMSR_LSTATUS) == 0)
  781. phydev->link = 0;
  782. else
  783. phydev->link = 1;
  784. return 0;
  785. }
  786. EXPORT_SYMBOL(genphy_update_link);
  787. /**
  788. * genphy_read_status - check the link status and update current link state
  789. * @phydev: target phy_device struct
  790. *
  791. * Description: Check the link, then figure out the current state
  792. * by comparing what we advertise with what the link partner
  793. * advertises. Start by checking the gigabit possibilities,
  794. * then move on to 10/100.
  795. */
  796. int genphy_read_status(struct phy_device *phydev)
  797. {
  798. int adv;
  799. int err;
  800. int lpa;
  801. int lpagb = 0;
  802. int common_adv;
  803. int common_adv_gb = 0;
  804. /* Update the link, but return if there was an error */
  805. err = genphy_update_link(phydev);
  806. if (err)
  807. return err;
  808. phydev->lp_advertising = 0;
  809. if (AUTONEG_ENABLE == phydev->autoneg) {
  810. if (phydev->supported & (SUPPORTED_1000baseT_Half
  811. | SUPPORTED_1000baseT_Full)) {
  812. lpagb = phy_read(phydev, MII_STAT1000);
  813. if (lpagb < 0)
  814. return lpagb;
  815. adv = phy_read(phydev, MII_CTRL1000);
  816. if (adv < 0)
  817. return adv;
  818. phydev->lp_advertising =
  819. mii_stat1000_to_ethtool_lpa_t(lpagb);
  820. common_adv_gb = lpagb & adv << 2;
  821. }
  822. lpa = phy_read(phydev, MII_LPA);
  823. if (lpa < 0)
  824. return lpa;
  825. phydev->lp_advertising |= mii_lpa_to_ethtool_lpa_t(lpa);
  826. adv = phy_read(phydev, MII_ADVERTISE);
  827. if (adv < 0)
  828. return adv;
  829. common_adv = lpa & adv;
  830. phydev->speed = SPEED_10;
  831. phydev->duplex = DUPLEX_HALF;
  832. phydev->pause = 0;
  833. phydev->asym_pause = 0;
  834. if (common_adv_gb & (LPA_1000FULL | LPA_1000HALF)) {
  835. phydev->speed = SPEED_1000;
  836. if (common_adv_gb & LPA_1000FULL)
  837. phydev->duplex = DUPLEX_FULL;
  838. } else if (common_adv & (LPA_100FULL | LPA_100HALF)) {
  839. phydev->speed = SPEED_100;
  840. if (common_adv & LPA_100FULL)
  841. phydev->duplex = DUPLEX_FULL;
  842. } else
  843. if (common_adv & LPA_10FULL)
  844. phydev->duplex = DUPLEX_FULL;
  845. if (phydev->duplex == DUPLEX_FULL) {
  846. phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
  847. phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
  848. }
  849. } else {
  850. int bmcr = phy_read(phydev, MII_BMCR);
  851. if (bmcr < 0)
  852. return bmcr;
  853. if (bmcr & BMCR_FULLDPLX)
  854. phydev->duplex = DUPLEX_FULL;
  855. else
  856. phydev->duplex = DUPLEX_HALF;
  857. if (bmcr & BMCR_SPEED1000)
  858. phydev->speed = SPEED_1000;
  859. else if (bmcr & BMCR_SPEED100)
  860. phydev->speed = SPEED_100;
  861. else
  862. phydev->speed = SPEED_10;
  863. phydev->pause = 0;
  864. phydev->asym_pause = 0;
  865. }
  866. return 0;
  867. }
  868. EXPORT_SYMBOL(genphy_read_status);
  869. static int gen10g_read_status(struct phy_device *phydev)
  870. {
  871. int devad, reg;
  872. u32 mmd_mask = phydev->c45_ids.devices_in_package;
  873. phydev->link = 1;
  874. /* For now just lie and say it's 10G all the time */
  875. phydev->speed = SPEED_10000;
  876. phydev->duplex = DUPLEX_FULL;
  877. for (devad = 0; mmd_mask; devad++, mmd_mask = mmd_mask >> 1) {
  878. if (!(mmd_mask & 1))
  879. continue;
  880. /* Read twice because link state is latched and a
  881. * read moves the current state into the register
  882. */
  883. phy_read_mmd(phydev, devad, MDIO_STAT1);
  884. reg = phy_read_mmd(phydev, devad, MDIO_STAT1);
  885. if (reg < 0 || !(reg & MDIO_STAT1_LSTATUS))
  886. phydev->link = 0;
  887. }
  888. return 0;
  889. }
  890. /**
  891. * genphy_soft_reset - software reset the PHY via BMCR_RESET bit
  892. * @phydev: target phy_device struct
  893. *
  894. * Description: Perform a software PHY reset using the standard
  895. * BMCR_RESET bit and poll for the reset bit to be cleared.
  896. *
  897. * Returns: 0 on success, < 0 on failure
  898. */
  899. int genphy_soft_reset(struct phy_device *phydev)
  900. {
  901. int ret;
  902. ret = phy_write(phydev, MII_BMCR, BMCR_RESET);
  903. if (ret < 0)
  904. return ret;
  905. return phy_poll_reset(phydev);
  906. }
  907. EXPORT_SYMBOL(genphy_soft_reset);
  908. int genphy_config_init(struct phy_device *phydev)
  909. {
  910. int val;
  911. u32 features;
  912. features = (SUPPORTED_TP | SUPPORTED_MII
  913. | SUPPORTED_AUI | SUPPORTED_FIBRE |
  914. SUPPORTED_BNC);
  915. /* Do we support autonegotiation? */
  916. val = phy_read(phydev, MII_BMSR);
  917. if (val < 0)
  918. return val;
  919. if (val & BMSR_ANEGCAPABLE)
  920. features |= SUPPORTED_Autoneg;
  921. if (val & BMSR_100FULL)
  922. features |= SUPPORTED_100baseT_Full;
  923. if (val & BMSR_100HALF)
  924. features |= SUPPORTED_100baseT_Half;
  925. if (val & BMSR_10FULL)
  926. features |= SUPPORTED_10baseT_Full;
  927. if (val & BMSR_10HALF)
  928. features |= SUPPORTED_10baseT_Half;
  929. if (val & BMSR_ESTATEN) {
  930. val = phy_read(phydev, MII_ESTATUS);
  931. if (val < 0)
  932. return val;
  933. if (val & ESTATUS_1000_TFULL)
  934. features |= SUPPORTED_1000baseT_Full;
  935. if (val & ESTATUS_1000_THALF)
  936. features |= SUPPORTED_1000baseT_Half;
  937. }
  938. phydev->supported &= features;
  939. phydev->advertising &= features;
  940. return 0;
  941. }
  942. static int gen10g_soft_reset(struct phy_device *phydev)
  943. {
  944. /* Do nothing for now */
  945. return 0;
  946. }
  947. EXPORT_SYMBOL(genphy_config_init);
  948. static int gen10g_config_init(struct phy_device *phydev)
  949. {
  950. /* Temporarily just say we support everything */
  951. phydev->supported = SUPPORTED_10000baseT_Full;
  952. phydev->advertising = SUPPORTED_10000baseT_Full;
  953. return 0;
  954. }
  955. int genphy_suspend(struct phy_device *phydev)
  956. {
  957. int value;
  958. mutex_lock(&phydev->lock);
  959. value = phy_read(phydev, MII_BMCR);
  960. phy_write(phydev, MII_BMCR, value | BMCR_PDOWN);
  961. mutex_unlock(&phydev->lock);
  962. return 0;
  963. }
  964. EXPORT_SYMBOL(genphy_suspend);
  965. static int gen10g_suspend(struct phy_device *phydev)
  966. {
  967. return 0;
  968. }
  969. int genphy_resume(struct phy_device *phydev)
  970. {
  971. int value;
  972. mutex_lock(&phydev->lock);
  973. value = phy_read(phydev, MII_BMCR);
  974. phy_write(phydev, MII_BMCR, value & ~BMCR_PDOWN);
  975. mutex_unlock(&phydev->lock);
  976. return 0;
  977. }
  978. EXPORT_SYMBOL(genphy_resume);
  979. static int gen10g_resume(struct phy_device *phydev)
  980. {
  981. return 0;
  982. }
  983. static void of_set_phy_supported(struct phy_device *phydev)
  984. {
  985. struct device_node *node = phydev->dev.of_node;
  986. u32 max_speed;
  987. if (!IS_ENABLED(CONFIG_OF_MDIO))
  988. return;
  989. if (!node)
  990. return;
  991. if (!of_property_read_u32(node, "max-speed", &max_speed)) {
  992. /* The default values for phydev->supported are provided by the PHY
  993. * driver "features" member, we want to reset to sane defaults fist
  994. * before supporting higher speeds.
  995. */
  996. phydev->supported &= PHY_DEFAULT_FEATURES;
  997. switch (max_speed) {
  998. default:
  999. return;
  1000. case SPEED_1000:
  1001. phydev->supported |= PHY_1000BT_FEATURES;
  1002. case SPEED_100:
  1003. phydev->supported |= PHY_100BT_FEATURES;
  1004. case SPEED_10:
  1005. phydev->supported |= PHY_10BT_FEATURES;
  1006. }
  1007. }
  1008. }
  1009. /**
  1010. * phy_probe - probe and init a PHY device
  1011. * @dev: device to probe and init
  1012. *
  1013. * Description: Take care of setting up the phy_device structure,
  1014. * set the state to READY (the driver's init function should
  1015. * set it to STARTING if needed).
  1016. */
  1017. static int phy_probe(struct device *dev)
  1018. {
  1019. struct phy_device *phydev = to_phy_device(dev);
  1020. struct device_driver *drv = phydev->dev.driver;
  1021. struct phy_driver *phydrv = to_phy_driver(drv);
  1022. int err = 0;
  1023. phydev->drv = phydrv;
  1024. /* Disable the interrupt if the PHY doesn't support it
  1025. * but the interrupt is still a valid one
  1026. */
  1027. if (!(phydrv->flags & PHY_HAS_INTERRUPT) &&
  1028. phy_interrupt_is_valid(phydev))
  1029. phydev->irq = PHY_POLL;
  1030. if (phydrv->flags & PHY_IS_INTERNAL)
  1031. phydev->is_internal = true;
  1032. mutex_lock(&phydev->lock);
  1033. /* Start out supporting everything. Eventually,
  1034. * a controller will attach, and may modify one
  1035. * or both of these values
  1036. */
  1037. phydev->supported = phydrv->features;
  1038. of_set_phy_supported(phydev);
  1039. phydev->advertising = phydev->supported;
  1040. /* Set the state to READY by default */
  1041. phydev->state = PHY_READY;
  1042. if (phydev->drv->probe)
  1043. err = phydev->drv->probe(phydev);
  1044. mutex_unlock(&phydev->lock);
  1045. return err;
  1046. }
  1047. static int phy_remove(struct device *dev)
  1048. {
  1049. struct phy_device *phydev = to_phy_device(dev);
  1050. mutex_lock(&phydev->lock);
  1051. phydev->state = PHY_DOWN;
  1052. mutex_unlock(&phydev->lock);
  1053. if (phydev->drv->remove)
  1054. phydev->drv->remove(phydev);
  1055. phydev->drv = NULL;
  1056. return 0;
  1057. }
  1058. /**
  1059. * phy_driver_register - register a phy_driver with the PHY layer
  1060. * @new_driver: new phy_driver to register
  1061. */
  1062. int phy_driver_register(struct phy_driver *new_driver)
  1063. {
  1064. int retval;
  1065. new_driver->driver.name = new_driver->name;
  1066. new_driver->driver.bus = &mdio_bus_type;
  1067. new_driver->driver.probe = phy_probe;
  1068. new_driver->driver.remove = phy_remove;
  1069. retval = driver_register(&new_driver->driver);
  1070. if (retval) {
  1071. pr_err("%s: Error %d in registering driver\n",
  1072. new_driver->name, retval);
  1073. return retval;
  1074. }
  1075. pr_debug("%s: Registered new driver\n", new_driver->name);
  1076. return 0;
  1077. }
  1078. EXPORT_SYMBOL(phy_driver_register);
  1079. int phy_drivers_register(struct phy_driver *new_driver, int n)
  1080. {
  1081. int i, ret = 0;
  1082. for (i = 0; i < n; i++) {
  1083. ret = phy_driver_register(new_driver + i);
  1084. if (ret) {
  1085. while (i-- > 0)
  1086. phy_driver_unregister(new_driver + i);
  1087. break;
  1088. }
  1089. }
  1090. return ret;
  1091. }
  1092. EXPORT_SYMBOL(phy_drivers_register);
  1093. void phy_driver_unregister(struct phy_driver *drv)
  1094. {
  1095. driver_unregister(&drv->driver);
  1096. }
  1097. EXPORT_SYMBOL(phy_driver_unregister);
  1098. void phy_drivers_unregister(struct phy_driver *drv, int n)
  1099. {
  1100. int i;
  1101. for (i = 0; i < n; i++)
  1102. phy_driver_unregister(drv + i);
  1103. }
  1104. EXPORT_SYMBOL(phy_drivers_unregister);
  1105. static struct phy_driver genphy_driver[] = {
  1106. {
  1107. .phy_id = 0xffffffff,
  1108. .phy_id_mask = 0xffffffff,
  1109. .name = "Generic PHY",
  1110. .soft_reset = genphy_soft_reset,
  1111. .config_init = genphy_config_init,
  1112. .features = PHY_GBIT_FEATURES | SUPPORTED_MII |
  1113. SUPPORTED_AUI | SUPPORTED_FIBRE |
  1114. SUPPORTED_BNC,
  1115. .config_aneg = genphy_config_aneg,
  1116. .aneg_done = genphy_aneg_done,
  1117. .read_status = genphy_read_status,
  1118. .suspend = genphy_suspend,
  1119. .resume = genphy_resume,
  1120. .driver = { .owner = THIS_MODULE, },
  1121. }, {
  1122. .phy_id = 0xffffffff,
  1123. .phy_id_mask = 0xffffffff,
  1124. .name = "Generic 10G PHY",
  1125. .soft_reset = gen10g_soft_reset,
  1126. .config_init = gen10g_config_init,
  1127. .features = 0,
  1128. .config_aneg = gen10g_config_aneg,
  1129. .read_status = gen10g_read_status,
  1130. .suspend = gen10g_suspend,
  1131. .resume = gen10g_resume,
  1132. .driver = {.owner = THIS_MODULE, },
  1133. } };
  1134. static int __init phy_init(void)
  1135. {
  1136. int rc;
  1137. rc = mdio_bus_init();
  1138. if (rc)
  1139. return rc;
  1140. rc = phy_drivers_register(genphy_driver,
  1141. ARRAY_SIZE(genphy_driver));
  1142. if (rc)
  1143. mdio_bus_exit();
  1144. return rc;
  1145. }
  1146. static void __exit phy_exit(void)
  1147. {
  1148. phy_drivers_unregister(genphy_driver,
  1149. ARRAY_SIZE(genphy_driver));
  1150. mdio_bus_exit();
  1151. }
  1152. subsys_initcall(phy_init);
  1153. module_exit(phy_exit);