phy_device.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. /*
  2. * drivers/net/phy/phy_device.c
  3. *
  4. * Framework for finding and configuring PHYs.
  5. * Also contains generic PHY driver
  6. *
  7. * Author: Andy Fleming
  8. *
  9. * Copyright (c) 2004 Freescale Semiconductor, Inc.
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the
  13. * Free Software Foundation; either version 2 of the License, or (at your
  14. * option) any later version.
  15. *
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/string.h>
  19. #include <linux/errno.h>
  20. #include <linux/unistd.h>
  21. #include <linux/slab.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/init.h>
  24. #include <linux/delay.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/etherdevice.h>
  27. #include <linux/skbuff.h>
  28. #include <linux/mm.h>
  29. #include <linux/module.h>
  30. #include <linux/mii.h>
  31. #include <linux/ethtool.h>
  32. #include <linux/phy.h>
  33. #include <asm/io.h>
  34. #include <asm/irq.h>
  35. #include <asm/uaccess.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. kfree(phydev);
  42. }
  43. EXPORT_SYMBOL(phy_device_free);
  44. static void phy_device_release(struct device *dev)
  45. {
  46. phy_device_free(to_phy_device(dev));
  47. }
  48. static struct phy_driver genphy_driver;
  49. extern int mdio_bus_init(void);
  50. extern void mdio_bus_exit(void);
  51. static LIST_HEAD(phy_fixup_list);
  52. static DEFINE_MUTEX(phy_fixup_lock);
  53. /*
  54. * Creates a new phy_fixup and adds it to the list
  55. * @bus_id: A string which matches phydev->dev.bus_id (or PHY_ANY_ID)
  56. * @phy_uid: Used to match against phydev->phy_id (the UID of the PHY)
  57. * It can also be PHY_ANY_UID
  58. * @phy_uid_mask: Applied to phydev->phy_id and fixup->phy_uid before
  59. * comparison
  60. * @run: The actual code to be run when a matching PHY is found
  61. */
  62. int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
  63. int (*run)(struct phy_device *))
  64. {
  65. struct phy_fixup *fixup;
  66. fixup = kzalloc(sizeof(struct phy_fixup), GFP_KERNEL);
  67. if (!fixup)
  68. return -ENOMEM;
  69. strlcpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id));
  70. fixup->phy_uid = phy_uid;
  71. fixup->phy_uid_mask = phy_uid_mask;
  72. fixup->run = run;
  73. mutex_lock(&phy_fixup_lock);
  74. list_add_tail(&fixup->list, &phy_fixup_list);
  75. mutex_unlock(&phy_fixup_lock);
  76. return 0;
  77. }
  78. EXPORT_SYMBOL(phy_register_fixup);
  79. /* Registers a fixup to be run on any PHY with the UID in phy_uid */
  80. int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
  81. int (*run)(struct phy_device *))
  82. {
  83. return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run);
  84. }
  85. EXPORT_SYMBOL(phy_register_fixup_for_uid);
  86. /* Registers a fixup to be run on the PHY with id string bus_id */
  87. int phy_register_fixup_for_id(const char *bus_id,
  88. int (*run)(struct phy_device *))
  89. {
  90. return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run);
  91. }
  92. EXPORT_SYMBOL(phy_register_fixup_for_id);
  93. /*
  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. 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;
  116. err = fixup->run(phydev);
  117. if (err < 0) {
  118. mutex_unlock(&phy_fixup_lock);
  119. return err;
  120. }
  121. }
  122. }
  123. mutex_unlock(&phy_fixup_lock);
  124. return 0;
  125. }
  126. EXPORT_SYMBOL(phy_scan_fixups);
  127. struct phy_device* phy_device_create(struct mii_bus *bus, int addr, int phy_id)
  128. {
  129. struct phy_device *dev;
  130. /* We allocate the device, and initialize the
  131. * default values */
  132. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  133. if (NULL == dev)
  134. return (struct phy_device*) PTR_ERR((void*)-ENOMEM);
  135. dev->dev.release = phy_device_release;
  136. dev->speed = 0;
  137. dev->duplex = -1;
  138. dev->pause = dev->asym_pause = 0;
  139. dev->link = 1;
  140. dev->interface = PHY_INTERFACE_MODE_GMII;
  141. dev->autoneg = AUTONEG_ENABLE;
  142. dev->addr = addr;
  143. dev->phy_id = phy_id;
  144. dev->bus = bus;
  145. dev->dev.parent = bus->parent;
  146. dev->dev.bus = &mdio_bus_type;
  147. dev->irq = bus->irq != NULL ? bus->irq[addr] : PHY_POLL;
  148. dev_set_name(&dev->dev, PHY_ID_FMT, bus->id, addr);
  149. dev->state = PHY_DOWN;
  150. mutex_init(&dev->lock);
  151. return dev;
  152. }
  153. EXPORT_SYMBOL(phy_device_create);
  154. /**
  155. * get_phy_id - reads the specified addr for its ID.
  156. * @bus: the target MII bus
  157. * @addr: PHY address on the MII bus
  158. * @phy_id: where to store the ID retrieved.
  159. *
  160. * Description: Reads the ID registers of the PHY at @addr on the
  161. * @bus, stores it in @phy_id and returns zero on success.
  162. */
  163. int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id)
  164. {
  165. int phy_reg;
  166. /* Grab the bits from PHYIR1, and put them
  167. * in the upper half */
  168. phy_reg = bus->read(bus, addr, MII_PHYSID1);
  169. if (phy_reg < 0)
  170. return -EIO;
  171. *phy_id = (phy_reg & 0xffff) << 16;
  172. /* Grab the bits from PHYIR2, and put them in the lower half */
  173. phy_reg = bus->read(bus, addr, MII_PHYSID2);
  174. if (phy_reg < 0)
  175. return -EIO;
  176. *phy_id |= (phy_reg & 0xffff);
  177. return 0;
  178. }
  179. EXPORT_SYMBOL(get_phy_id);
  180. /**
  181. * get_phy_device - reads the specified PHY device and returns its @phy_device struct
  182. * @bus: the target MII bus
  183. * @addr: PHY address on the MII bus
  184. *
  185. * Description: Reads the ID registers of the PHY at @addr on the
  186. * @bus, then allocates and returns the phy_device to represent it.
  187. */
  188. struct phy_device * get_phy_device(struct mii_bus *bus, int addr)
  189. {
  190. struct phy_device *dev = NULL;
  191. u32 phy_id;
  192. int r;
  193. r = get_phy_id(bus, addr, &phy_id);
  194. if (r)
  195. return ERR_PTR(r);
  196. /* If the phy_id is mostly Fs, there is no device there */
  197. if ((phy_id & 0x1fffffff) == 0x1fffffff)
  198. return NULL;
  199. dev = phy_device_create(bus, addr, phy_id);
  200. return dev;
  201. }
  202. EXPORT_SYMBOL(get_phy_device);
  203. /**
  204. * phy_device_register - Register the phy device on the MDIO bus
  205. * @phydev: phy_device structure to be added to the MDIO bus
  206. */
  207. int phy_device_register(struct phy_device *phydev)
  208. {
  209. int err;
  210. /* Don't register a phy if one is already registered at this
  211. * address */
  212. if (phydev->bus->phy_map[phydev->addr])
  213. return -EINVAL;
  214. phydev->bus->phy_map[phydev->addr] = phydev;
  215. /* Run all of the fixups for this PHY */
  216. phy_scan_fixups(phydev);
  217. err = device_register(&phydev->dev);
  218. if (err) {
  219. pr_err("phy %d failed to register\n", phydev->addr);
  220. goto out;
  221. }
  222. return 0;
  223. out:
  224. phydev->bus->phy_map[phydev->addr] = NULL;
  225. return err;
  226. }
  227. EXPORT_SYMBOL(phy_device_register);
  228. /**
  229. * phy_prepare_link - prepares the PHY layer to monitor link status
  230. * @phydev: target phy_device struct
  231. * @handler: callback function for link status change notifications
  232. *
  233. * Description: Tells the PHY infrastructure to handle the
  234. * gory details on monitoring link status (whether through
  235. * polling or an interrupt), and to call back to the
  236. * connected device driver when the link status changes.
  237. * If you want to monitor your own link state, don't call
  238. * this function.
  239. */
  240. void phy_prepare_link(struct phy_device *phydev,
  241. void (*handler)(struct net_device *))
  242. {
  243. phydev->adjust_link = handler;
  244. }
  245. /**
  246. * phy_connect_direct - connect an ethernet device to a specific phy_device
  247. * @dev: the network device to connect
  248. * @phydev: the pointer to the phy device
  249. * @handler: callback function for state change notifications
  250. * @flags: PHY device's dev_flags
  251. * @interface: PHY device's interface
  252. */
  253. int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
  254. void (*handler)(struct net_device *), u32 flags,
  255. phy_interface_t interface)
  256. {
  257. int rc;
  258. rc = phy_attach_direct(dev, phydev, flags, interface);
  259. if (rc)
  260. return rc;
  261. phy_prepare_link(phydev, handler);
  262. phy_start_machine(phydev, NULL);
  263. if (phydev->irq > 0)
  264. phy_start_interrupts(phydev);
  265. return 0;
  266. }
  267. EXPORT_SYMBOL(phy_connect_direct);
  268. /**
  269. * phy_connect - connect an ethernet device to a PHY device
  270. * @dev: the network device to connect
  271. * @bus_id: the id string of the PHY device to connect
  272. * @handler: callback function for state change notifications
  273. * @flags: PHY device's dev_flags
  274. * @interface: PHY device's interface
  275. *
  276. * Description: Convenience function for connecting ethernet
  277. * devices to PHY devices. The default behavior is for
  278. * the PHY infrastructure to handle everything, and only notify
  279. * the connected driver when the link status changes. If you
  280. * don't want, or can't use the provided functionality, you may
  281. * choose to call only the subset of functions which provide
  282. * the desired functionality.
  283. */
  284. struct phy_device * phy_connect(struct net_device *dev, const char *bus_id,
  285. void (*handler)(struct net_device *), u32 flags,
  286. phy_interface_t interface)
  287. {
  288. struct phy_device *phydev;
  289. struct device *d;
  290. int rc;
  291. /* Search the list of PHY devices on the mdio bus for the
  292. * PHY with the requested name */
  293. d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id);
  294. if (!d) {
  295. pr_err("PHY %s not found\n", bus_id);
  296. return ERR_PTR(-ENODEV);
  297. }
  298. phydev = to_phy_device(d);
  299. rc = phy_connect_direct(dev, phydev, handler, flags, interface);
  300. if (rc)
  301. return ERR_PTR(rc);
  302. return phydev;
  303. }
  304. EXPORT_SYMBOL(phy_connect);
  305. /**
  306. * phy_disconnect - disable interrupts, stop state machine, and detach a PHY device
  307. * @phydev: target phy_device struct
  308. */
  309. void phy_disconnect(struct phy_device *phydev)
  310. {
  311. if (phydev->irq > 0)
  312. phy_stop_interrupts(phydev);
  313. phy_stop_machine(phydev);
  314. phydev->adjust_link = NULL;
  315. phy_detach(phydev);
  316. }
  317. EXPORT_SYMBOL(phy_disconnect);
  318. int phy_init_hw(struct phy_device *phydev)
  319. {
  320. int ret;
  321. if (!phydev->drv || !phydev->drv->config_init)
  322. return 0;
  323. ret = phy_scan_fixups(phydev);
  324. if (ret < 0)
  325. return ret;
  326. return phydev->drv->config_init(phydev);
  327. }
  328. /**
  329. * phy_attach_direct - attach a network device to a given PHY device pointer
  330. * @dev: network device to attach
  331. * @phydev: Pointer to phy_device to attach
  332. * @flags: PHY device's dev_flags
  333. * @interface: PHY device's interface
  334. *
  335. * Description: Called by drivers to attach to a particular PHY
  336. * device. The phy_device is found, and properly hooked up
  337. * to the phy_driver. If no driver is attached, then the
  338. * genphy_driver is used. The phy_device is given a ptr to
  339. * the attaching device, and given a callback for link status
  340. * change. The phy_device is returned to the attaching driver.
  341. */
  342. int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
  343. u32 flags, phy_interface_t interface)
  344. {
  345. struct device *d = &phydev->dev;
  346. /* Assume that if there is no driver, that it doesn't
  347. * exist, and we should use the genphy driver. */
  348. if (NULL == d->driver) {
  349. int err;
  350. d->driver = &genphy_driver.driver;
  351. err = d->driver->probe(d);
  352. if (err >= 0)
  353. err = device_bind_driver(d);
  354. if (err)
  355. return err;
  356. }
  357. if (phydev->attached_dev) {
  358. dev_err(&dev->dev, "PHY already attached\n");
  359. return -EBUSY;
  360. }
  361. phydev->attached_dev = dev;
  362. phydev->dev_flags = flags;
  363. phydev->interface = interface;
  364. /* Do initial configuration here, now that
  365. * we have certain key parameters
  366. * (dev_flags and interface) */
  367. return phy_init_hw(phydev);
  368. }
  369. EXPORT_SYMBOL(phy_attach_direct);
  370. /**
  371. * phy_attach - attach a network device to a particular PHY device
  372. * @dev: network device to attach
  373. * @bus_id: Bus ID of PHY device to attach
  374. * @flags: PHY device's dev_flags
  375. * @interface: PHY device's interface
  376. *
  377. * Description: Same as phy_attach_direct() except that a PHY bus_id
  378. * string is passed instead of a pointer to a struct phy_device.
  379. */
  380. struct phy_device *phy_attach(struct net_device *dev,
  381. const char *bus_id, u32 flags, phy_interface_t interface)
  382. {
  383. struct bus_type *bus = &mdio_bus_type;
  384. struct phy_device *phydev;
  385. struct device *d;
  386. int rc;
  387. /* Search the list of PHY devices on the mdio bus for the
  388. * PHY with the requested name */
  389. d = bus_find_device_by_name(bus, NULL, bus_id);
  390. if (!d) {
  391. pr_err("PHY %s not found\n", bus_id);
  392. return ERR_PTR(-ENODEV);
  393. }
  394. phydev = to_phy_device(d);
  395. rc = phy_attach_direct(dev, phydev, flags, interface);
  396. if (rc)
  397. return ERR_PTR(rc);
  398. return phydev;
  399. }
  400. EXPORT_SYMBOL(phy_attach);
  401. /**
  402. * phy_detach - detach a PHY device from its network device
  403. * @phydev: target phy_device struct
  404. */
  405. void phy_detach(struct phy_device *phydev)
  406. {
  407. phydev->attached_dev = NULL;
  408. /* If the device had no specific driver before (i.e. - it
  409. * was using the generic driver), we unbind the device
  410. * from the generic driver so that there's a chance a
  411. * real driver could be loaded */
  412. if (phydev->dev.driver == &genphy_driver.driver)
  413. device_release_driver(&phydev->dev);
  414. }
  415. EXPORT_SYMBOL(phy_detach);
  416. /* Generic PHY support and helper functions */
  417. /**
  418. * genphy_config_advert - sanitize and advertise auto-negotation parameters
  419. * @phydev: target phy_device struct
  420. *
  421. * Description: Writes MII_ADVERTISE with the appropriate values,
  422. * after sanitizing the values to make sure we only advertise
  423. * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
  424. * hasn't changed, and > 0 if it has changed.
  425. */
  426. int genphy_config_advert(struct phy_device *phydev)
  427. {
  428. u32 advertise;
  429. int oldadv, adv;
  430. int err, changed = 0;
  431. /* Only allow advertising what
  432. * this PHY supports */
  433. phydev->advertising &= phydev->supported;
  434. advertise = phydev->advertising;
  435. /* Setup standard advertisement */
  436. oldadv = adv = phy_read(phydev, MII_ADVERTISE);
  437. if (adv < 0)
  438. return adv;
  439. adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
  440. ADVERTISE_PAUSE_ASYM);
  441. if (advertise & ADVERTISED_10baseT_Half)
  442. adv |= ADVERTISE_10HALF;
  443. if (advertise & ADVERTISED_10baseT_Full)
  444. adv |= ADVERTISE_10FULL;
  445. if (advertise & ADVERTISED_100baseT_Half)
  446. adv |= ADVERTISE_100HALF;
  447. if (advertise & ADVERTISED_100baseT_Full)
  448. adv |= ADVERTISE_100FULL;
  449. if (advertise & ADVERTISED_Pause)
  450. adv |= ADVERTISE_PAUSE_CAP;
  451. if (advertise & ADVERTISED_Asym_Pause)
  452. adv |= ADVERTISE_PAUSE_ASYM;
  453. if (adv != oldadv) {
  454. err = phy_write(phydev, MII_ADVERTISE, adv);
  455. if (err < 0)
  456. return err;
  457. changed = 1;
  458. }
  459. /* Configure gigabit if it's supported */
  460. if (phydev->supported & (SUPPORTED_1000baseT_Half |
  461. SUPPORTED_1000baseT_Full)) {
  462. oldadv = adv = phy_read(phydev, MII_CTRL1000);
  463. if (adv < 0)
  464. return adv;
  465. adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
  466. if (advertise & SUPPORTED_1000baseT_Half)
  467. adv |= ADVERTISE_1000HALF;
  468. if (advertise & SUPPORTED_1000baseT_Full)
  469. adv |= ADVERTISE_1000FULL;
  470. if (adv != oldadv) {
  471. err = phy_write(phydev, MII_CTRL1000, adv);
  472. if (err < 0)
  473. return err;
  474. changed = 1;
  475. }
  476. }
  477. return changed;
  478. }
  479. EXPORT_SYMBOL(genphy_config_advert);
  480. /**
  481. * genphy_setup_forced - configures/forces speed/duplex from @phydev
  482. * @phydev: target phy_device struct
  483. *
  484. * Description: Configures MII_BMCR to force speed/duplex
  485. * to the values in phydev. Assumes that the values are valid.
  486. * Please see phy_sanitize_settings().
  487. */
  488. int genphy_setup_forced(struct phy_device *phydev)
  489. {
  490. int err;
  491. int ctl = 0;
  492. phydev->pause = phydev->asym_pause = 0;
  493. if (SPEED_1000 == phydev->speed)
  494. ctl |= BMCR_SPEED1000;
  495. else if (SPEED_100 == phydev->speed)
  496. ctl |= BMCR_SPEED100;
  497. if (DUPLEX_FULL == phydev->duplex)
  498. ctl |= BMCR_FULLDPLX;
  499. err = phy_write(phydev, MII_BMCR, ctl);
  500. return err;
  501. }
  502. /**
  503. * genphy_restart_aneg - Enable and Restart Autonegotiation
  504. * @phydev: target phy_device struct
  505. */
  506. int genphy_restart_aneg(struct phy_device *phydev)
  507. {
  508. int ctl;
  509. ctl = phy_read(phydev, MII_BMCR);
  510. if (ctl < 0)
  511. return ctl;
  512. ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
  513. /* Don't isolate the PHY if we're negotiating */
  514. ctl &= ~(BMCR_ISOLATE);
  515. ctl = phy_write(phydev, MII_BMCR, ctl);
  516. return ctl;
  517. }
  518. EXPORT_SYMBOL(genphy_restart_aneg);
  519. /**
  520. * genphy_config_aneg - restart auto-negotiation or write BMCR
  521. * @phydev: target phy_device struct
  522. *
  523. * Description: If auto-negotiation is enabled, we configure the
  524. * advertising, and then restart auto-negotiation. If it is not
  525. * enabled, then we write the BMCR.
  526. */
  527. int genphy_config_aneg(struct phy_device *phydev)
  528. {
  529. int result;
  530. if (AUTONEG_ENABLE != phydev->autoneg)
  531. return genphy_setup_forced(phydev);
  532. result = genphy_config_advert(phydev);
  533. if (result < 0) /* error */
  534. return result;
  535. if (result == 0) {
  536. /* Advertisment hasn't changed, but maybe aneg was never on to
  537. * begin with? Or maybe phy was isolated? */
  538. int ctl = phy_read(phydev, MII_BMCR);
  539. if (ctl < 0)
  540. return ctl;
  541. if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
  542. result = 1; /* do restart aneg */
  543. }
  544. /* Only restart aneg if we are advertising something different
  545. * than we were before. */
  546. if (result > 0)
  547. result = genphy_restart_aneg(phydev);
  548. return result;
  549. }
  550. EXPORT_SYMBOL(genphy_config_aneg);
  551. /**
  552. * genphy_update_link - update link status in @phydev
  553. * @phydev: target phy_device struct
  554. *
  555. * Description: Update the value in phydev->link to reflect the
  556. * current link value. In order to do this, we need to read
  557. * the status register twice, keeping the second value.
  558. */
  559. int genphy_update_link(struct phy_device *phydev)
  560. {
  561. int status;
  562. /* Do a fake read */
  563. status = phy_read(phydev, MII_BMSR);
  564. if (status < 0)
  565. return status;
  566. /* Read link and autonegotiation status */
  567. status = phy_read(phydev, MII_BMSR);
  568. if (status < 0)
  569. return status;
  570. if ((status & BMSR_LSTATUS) == 0)
  571. phydev->link = 0;
  572. else
  573. phydev->link = 1;
  574. return 0;
  575. }
  576. EXPORT_SYMBOL(genphy_update_link);
  577. /**
  578. * genphy_read_status - check the link status and update current link state
  579. * @phydev: target phy_device struct
  580. *
  581. * Description: Check the link, then figure out the current state
  582. * by comparing what we advertise with what the link partner
  583. * advertises. Start by checking the gigabit possibilities,
  584. * then move on to 10/100.
  585. */
  586. int genphy_read_status(struct phy_device *phydev)
  587. {
  588. int adv;
  589. int err;
  590. int lpa;
  591. int lpagb = 0;
  592. /* Update the link, but return if there
  593. * was an error */
  594. err = genphy_update_link(phydev);
  595. if (err)
  596. return err;
  597. if (AUTONEG_ENABLE == phydev->autoneg) {
  598. if (phydev->supported & (SUPPORTED_1000baseT_Half
  599. | SUPPORTED_1000baseT_Full)) {
  600. lpagb = phy_read(phydev, MII_STAT1000);
  601. if (lpagb < 0)
  602. return lpagb;
  603. adv = phy_read(phydev, MII_CTRL1000);
  604. if (adv < 0)
  605. return adv;
  606. lpagb &= adv << 2;
  607. }
  608. lpa = phy_read(phydev, MII_LPA);
  609. if (lpa < 0)
  610. return lpa;
  611. adv = phy_read(phydev, MII_ADVERTISE);
  612. if (adv < 0)
  613. return adv;
  614. lpa &= adv;
  615. phydev->speed = SPEED_10;
  616. phydev->duplex = DUPLEX_HALF;
  617. phydev->pause = phydev->asym_pause = 0;
  618. if (lpagb & (LPA_1000FULL | LPA_1000HALF)) {
  619. phydev->speed = SPEED_1000;
  620. if (lpagb & LPA_1000FULL)
  621. phydev->duplex = DUPLEX_FULL;
  622. } else if (lpa & (LPA_100FULL | LPA_100HALF)) {
  623. phydev->speed = SPEED_100;
  624. if (lpa & LPA_100FULL)
  625. phydev->duplex = DUPLEX_FULL;
  626. } else
  627. if (lpa & LPA_10FULL)
  628. phydev->duplex = DUPLEX_FULL;
  629. if (phydev->duplex == DUPLEX_FULL){
  630. phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
  631. phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
  632. }
  633. } else {
  634. int bmcr = phy_read(phydev, MII_BMCR);
  635. if (bmcr < 0)
  636. return bmcr;
  637. if (bmcr & BMCR_FULLDPLX)
  638. phydev->duplex = DUPLEX_FULL;
  639. else
  640. phydev->duplex = DUPLEX_HALF;
  641. if (bmcr & BMCR_SPEED1000)
  642. phydev->speed = SPEED_1000;
  643. else if (bmcr & BMCR_SPEED100)
  644. phydev->speed = SPEED_100;
  645. else
  646. phydev->speed = SPEED_10;
  647. phydev->pause = phydev->asym_pause = 0;
  648. }
  649. return 0;
  650. }
  651. EXPORT_SYMBOL(genphy_read_status);
  652. static int genphy_config_init(struct phy_device *phydev)
  653. {
  654. int val;
  655. u32 features;
  656. /* For now, I'll claim that the generic driver supports
  657. * all possible port types */
  658. features = (SUPPORTED_TP | SUPPORTED_MII
  659. | SUPPORTED_AUI | SUPPORTED_FIBRE |
  660. SUPPORTED_BNC);
  661. /* Do we support autonegotiation? */
  662. val = phy_read(phydev, MII_BMSR);
  663. if (val < 0)
  664. return val;
  665. if (val & BMSR_ANEGCAPABLE)
  666. features |= SUPPORTED_Autoneg;
  667. if (val & BMSR_100FULL)
  668. features |= SUPPORTED_100baseT_Full;
  669. if (val & BMSR_100HALF)
  670. features |= SUPPORTED_100baseT_Half;
  671. if (val & BMSR_10FULL)
  672. features |= SUPPORTED_10baseT_Full;
  673. if (val & BMSR_10HALF)
  674. features |= SUPPORTED_10baseT_Half;
  675. if (val & BMSR_ESTATEN) {
  676. val = phy_read(phydev, MII_ESTATUS);
  677. if (val < 0)
  678. return val;
  679. if (val & ESTATUS_1000_TFULL)
  680. features |= SUPPORTED_1000baseT_Full;
  681. if (val & ESTATUS_1000_THALF)
  682. features |= SUPPORTED_1000baseT_Half;
  683. }
  684. phydev->supported = features;
  685. phydev->advertising = features;
  686. return 0;
  687. }
  688. int genphy_suspend(struct phy_device *phydev)
  689. {
  690. int value;
  691. mutex_lock(&phydev->lock);
  692. value = phy_read(phydev, MII_BMCR);
  693. phy_write(phydev, MII_BMCR, (value | BMCR_PDOWN));
  694. mutex_unlock(&phydev->lock);
  695. return 0;
  696. }
  697. EXPORT_SYMBOL(genphy_suspend);
  698. int genphy_resume(struct phy_device *phydev)
  699. {
  700. int value;
  701. mutex_lock(&phydev->lock);
  702. value = phy_read(phydev, MII_BMCR);
  703. phy_write(phydev, MII_BMCR, (value & ~BMCR_PDOWN));
  704. mutex_unlock(&phydev->lock);
  705. return 0;
  706. }
  707. EXPORT_SYMBOL(genphy_resume);
  708. /**
  709. * phy_probe - probe and init a PHY device
  710. * @dev: device to probe and init
  711. *
  712. * Description: Take care of setting up the phy_device structure,
  713. * set the state to READY (the driver's init function should
  714. * set it to STARTING if needed).
  715. */
  716. static int phy_probe(struct device *dev)
  717. {
  718. struct phy_device *phydev;
  719. struct phy_driver *phydrv;
  720. struct device_driver *drv;
  721. int err = 0;
  722. phydev = to_phy_device(dev);
  723. /* Make sure the driver is held.
  724. * XXX -- Is this correct? */
  725. drv = get_driver(phydev->dev.driver);
  726. phydrv = to_phy_driver(drv);
  727. phydev->drv = phydrv;
  728. /* Disable the interrupt if the PHY doesn't support it */
  729. if (!(phydrv->flags & PHY_HAS_INTERRUPT))
  730. phydev->irq = PHY_POLL;
  731. mutex_lock(&phydev->lock);
  732. /* Start out supporting everything. Eventually,
  733. * a controller will attach, and may modify one
  734. * or both of these values */
  735. phydev->supported = phydrv->features;
  736. phydev->advertising = phydrv->features;
  737. /* Set the state to READY by default */
  738. phydev->state = PHY_READY;
  739. if (phydev->drv->probe)
  740. err = phydev->drv->probe(phydev);
  741. mutex_unlock(&phydev->lock);
  742. return err;
  743. }
  744. static int phy_remove(struct device *dev)
  745. {
  746. struct phy_device *phydev;
  747. phydev = to_phy_device(dev);
  748. mutex_lock(&phydev->lock);
  749. phydev->state = PHY_DOWN;
  750. mutex_unlock(&phydev->lock);
  751. if (phydev->drv->remove)
  752. phydev->drv->remove(phydev);
  753. put_driver(dev->driver);
  754. phydev->drv = NULL;
  755. return 0;
  756. }
  757. /**
  758. * phy_driver_register - register a phy_driver with the PHY layer
  759. * @new_driver: new phy_driver to register
  760. */
  761. int phy_driver_register(struct phy_driver *new_driver)
  762. {
  763. int retval;
  764. new_driver->driver.name = new_driver->name;
  765. new_driver->driver.bus = &mdio_bus_type;
  766. new_driver->driver.probe = phy_probe;
  767. new_driver->driver.remove = phy_remove;
  768. retval = driver_register(&new_driver->driver);
  769. if (retval) {
  770. printk(KERN_ERR "%s: Error %d in registering driver\n",
  771. new_driver->name, retval);
  772. return retval;
  773. }
  774. pr_debug("%s: Registered new driver\n", new_driver->name);
  775. return 0;
  776. }
  777. EXPORT_SYMBOL(phy_driver_register);
  778. void phy_driver_unregister(struct phy_driver *drv)
  779. {
  780. driver_unregister(&drv->driver);
  781. }
  782. EXPORT_SYMBOL(phy_driver_unregister);
  783. static struct phy_driver genphy_driver = {
  784. .phy_id = 0xffffffff,
  785. .phy_id_mask = 0xffffffff,
  786. .name = "Generic PHY",
  787. .config_init = genphy_config_init,
  788. .features = 0,
  789. .config_aneg = genphy_config_aneg,
  790. .read_status = genphy_read_status,
  791. .suspend = genphy_suspend,
  792. .resume = genphy_resume,
  793. .driver = {.owner= THIS_MODULE, },
  794. };
  795. static int __init phy_init(void)
  796. {
  797. int rc;
  798. rc = mdio_bus_init();
  799. if (rc)
  800. return rc;
  801. rc = phy_driver_register(&genphy_driver);
  802. if (rc)
  803. mdio_bus_exit();
  804. return rc;
  805. }
  806. static void __exit phy_exit(void)
  807. {
  808. phy_driver_unregister(&genphy_driver);
  809. mdio_bus_exit();
  810. }
  811. subsys_initcall(phy_init);
  812. module_exit(phy_exit);