phy_device.c 31 KB

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