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. /**
  319. * phy_attach_direct - attach a network device to a given PHY device pointer
  320. * @dev: network device to attach
  321. * @phydev: Pointer to phy_device to attach
  322. * @flags: PHY device's dev_flags
  323. * @interface: PHY device's interface
  324. *
  325. * Description: Called by drivers to attach to a particular PHY
  326. * device. The phy_device is found, and properly hooked up
  327. * to the phy_driver. If no driver is attached, then the
  328. * genphy_driver is used. The phy_device is given a ptr to
  329. * the attaching device, and given a callback for link status
  330. * change. The phy_device is returned to the attaching driver.
  331. */
  332. int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
  333. u32 flags, phy_interface_t interface)
  334. {
  335. struct device *d = &phydev->dev;
  336. /* Assume that if there is no driver, that it doesn't
  337. * exist, and we should use the genphy driver. */
  338. if (NULL == d->driver) {
  339. int err;
  340. d->driver = &genphy_driver.driver;
  341. err = d->driver->probe(d);
  342. if (err >= 0)
  343. err = device_bind_driver(d);
  344. if (err)
  345. return err;
  346. }
  347. if (phydev->attached_dev) {
  348. dev_err(&dev->dev, "PHY already attached\n");
  349. return -EBUSY;
  350. }
  351. phydev->attached_dev = dev;
  352. phydev->dev_flags = flags;
  353. phydev->interface = interface;
  354. /* Do initial configuration here, now that
  355. * we have certain key parameters
  356. * (dev_flags and interface) */
  357. if (phydev->drv->config_init) {
  358. int err;
  359. err = phy_scan_fixups(phydev);
  360. if (err < 0)
  361. return err;
  362. err = phydev->drv->config_init(phydev);
  363. if (err < 0)
  364. return err;
  365. }
  366. return 0;
  367. }
  368. EXPORT_SYMBOL(phy_attach_direct);
  369. /**
  370. * phy_attach - attach a network device to a particular PHY device
  371. * @dev: network device to attach
  372. * @bus_id: Bus ID of PHY device to attach
  373. * @flags: PHY device's dev_flags
  374. * @interface: PHY device's interface
  375. *
  376. * Description: Same as phy_attach_direct() except that a PHY bus_id
  377. * string is passed instead of a pointer to a struct phy_device.
  378. */
  379. struct phy_device *phy_attach(struct net_device *dev,
  380. const char *bus_id, u32 flags, phy_interface_t interface)
  381. {
  382. struct bus_type *bus = &mdio_bus_type;
  383. struct phy_device *phydev;
  384. struct device *d;
  385. int rc;
  386. /* Search the list of PHY devices on the mdio bus for the
  387. * PHY with the requested name */
  388. d = bus_find_device_by_name(bus, NULL, bus_id);
  389. if (!d) {
  390. pr_err("PHY %s not found\n", bus_id);
  391. return ERR_PTR(-ENODEV);
  392. }
  393. phydev = to_phy_device(d);
  394. rc = phy_attach_direct(dev, phydev, flags, interface);
  395. if (rc)
  396. return ERR_PTR(rc);
  397. return phydev;
  398. }
  399. EXPORT_SYMBOL(phy_attach);
  400. /**
  401. * phy_detach - detach a PHY device from its network device
  402. * @phydev: target phy_device struct
  403. */
  404. void phy_detach(struct phy_device *phydev)
  405. {
  406. phydev->attached_dev = NULL;
  407. /* If the device had no specific driver before (i.e. - it
  408. * was using the generic driver), we unbind the device
  409. * from the generic driver so that there's a chance a
  410. * real driver could be loaded */
  411. if (phydev->dev.driver == &genphy_driver.driver)
  412. device_release_driver(&phydev->dev);
  413. }
  414. EXPORT_SYMBOL(phy_detach);
  415. /* Generic PHY support and helper functions */
  416. /**
  417. * genphy_config_advert - sanitize and advertise auto-negotation parameters
  418. * @phydev: target phy_device struct
  419. *
  420. * Description: Writes MII_ADVERTISE with the appropriate values,
  421. * after sanitizing the values to make sure we only advertise
  422. * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
  423. * hasn't changed, and > 0 if it has changed.
  424. */
  425. int genphy_config_advert(struct phy_device *phydev)
  426. {
  427. u32 advertise;
  428. int oldadv, adv;
  429. int err, changed = 0;
  430. /* Only allow advertising what
  431. * this PHY supports */
  432. phydev->advertising &= phydev->supported;
  433. advertise = phydev->advertising;
  434. /* Setup standard advertisement */
  435. oldadv = adv = phy_read(phydev, MII_ADVERTISE);
  436. if (adv < 0)
  437. return adv;
  438. adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
  439. ADVERTISE_PAUSE_ASYM);
  440. if (advertise & ADVERTISED_10baseT_Half)
  441. adv |= ADVERTISE_10HALF;
  442. if (advertise & ADVERTISED_10baseT_Full)
  443. adv |= ADVERTISE_10FULL;
  444. if (advertise & ADVERTISED_100baseT_Half)
  445. adv |= ADVERTISE_100HALF;
  446. if (advertise & ADVERTISED_100baseT_Full)
  447. adv |= ADVERTISE_100FULL;
  448. if (advertise & ADVERTISED_Pause)
  449. adv |= ADVERTISE_PAUSE_CAP;
  450. if (advertise & ADVERTISED_Asym_Pause)
  451. adv |= ADVERTISE_PAUSE_ASYM;
  452. if (adv != oldadv) {
  453. err = phy_write(phydev, MII_ADVERTISE, adv);
  454. if (err < 0)
  455. return err;
  456. changed = 1;
  457. }
  458. /* Configure gigabit if it's supported */
  459. if (phydev->supported & (SUPPORTED_1000baseT_Half |
  460. SUPPORTED_1000baseT_Full)) {
  461. oldadv = adv = phy_read(phydev, MII_CTRL1000);
  462. if (adv < 0)
  463. return adv;
  464. adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
  465. if (advertise & SUPPORTED_1000baseT_Half)
  466. adv |= ADVERTISE_1000HALF;
  467. if (advertise & SUPPORTED_1000baseT_Full)
  468. adv |= ADVERTISE_1000FULL;
  469. if (adv != oldadv) {
  470. err = phy_write(phydev, MII_CTRL1000, adv);
  471. if (err < 0)
  472. return err;
  473. changed = 1;
  474. }
  475. }
  476. return changed;
  477. }
  478. EXPORT_SYMBOL(genphy_config_advert);
  479. /**
  480. * genphy_setup_forced - configures/forces speed/duplex from @phydev
  481. * @phydev: target phy_device struct
  482. *
  483. * Description: Configures MII_BMCR to force speed/duplex
  484. * to the values in phydev. Assumes that the values are valid.
  485. * Please see phy_sanitize_settings().
  486. */
  487. int genphy_setup_forced(struct phy_device *phydev)
  488. {
  489. int err;
  490. int ctl = 0;
  491. phydev->pause = phydev->asym_pause = 0;
  492. if (SPEED_1000 == phydev->speed)
  493. ctl |= BMCR_SPEED1000;
  494. else if (SPEED_100 == phydev->speed)
  495. ctl |= BMCR_SPEED100;
  496. if (DUPLEX_FULL == phydev->duplex)
  497. ctl |= BMCR_FULLDPLX;
  498. err = phy_write(phydev, MII_BMCR, ctl);
  499. return err;
  500. }
  501. /**
  502. * genphy_restart_aneg - Enable and Restart Autonegotiation
  503. * @phydev: target phy_device struct
  504. */
  505. int genphy_restart_aneg(struct phy_device *phydev)
  506. {
  507. int ctl;
  508. ctl = phy_read(phydev, MII_BMCR);
  509. if (ctl < 0)
  510. return ctl;
  511. ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
  512. /* Don't isolate the PHY if we're negotiating */
  513. ctl &= ~(BMCR_ISOLATE);
  514. ctl = phy_write(phydev, MII_BMCR, ctl);
  515. return ctl;
  516. }
  517. EXPORT_SYMBOL(genphy_restart_aneg);
  518. /**
  519. * genphy_config_aneg - restart auto-negotiation or write BMCR
  520. * @phydev: target phy_device struct
  521. *
  522. * Description: If auto-negotiation is enabled, we configure the
  523. * advertising, and then restart auto-negotiation. If it is not
  524. * enabled, then we write the BMCR.
  525. */
  526. int genphy_config_aneg(struct phy_device *phydev)
  527. {
  528. int result;
  529. if (AUTONEG_ENABLE != phydev->autoneg)
  530. return genphy_setup_forced(phydev);
  531. result = genphy_config_advert(phydev);
  532. if (result < 0) /* error */
  533. return result;
  534. if (result == 0) {
  535. /* Advertisment hasn't changed, but maybe aneg was never on to
  536. * begin with? Or maybe phy was isolated? */
  537. int ctl = phy_read(phydev, MII_BMCR);
  538. if (ctl < 0)
  539. return ctl;
  540. if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
  541. result = 1; /* do restart aneg */
  542. }
  543. /* Only restart aneg if we are advertising something different
  544. * than we were before. */
  545. if (result > 0)
  546. result = genphy_restart_aneg(phydev);
  547. return result;
  548. }
  549. EXPORT_SYMBOL(genphy_config_aneg);
  550. /**
  551. * genphy_update_link - update link status in @phydev
  552. * @phydev: target phy_device struct
  553. *
  554. * Description: Update the value in phydev->link to reflect the
  555. * current link value. In order to do this, we need to read
  556. * the status register twice, keeping the second value.
  557. */
  558. int genphy_update_link(struct phy_device *phydev)
  559. {
  560. int status;
  561. /* Do a fake read */
  562. status = phy_read(phydev, MII_BMSR);
  563. if (status < 0)
  564. return status;
  565. /* Read link and autonegotiation status */
  566. status = phy_read(phydev, MII_BMSR);
  567. if (status < 0)
  568. return status;
  569. if ((status & BMSR_LSTATUS) == 0)
  570. phydev->link = 0;
  571. else
  572. phydev->link = 1;
  573. return 0;
  574. }
  575. EXPORT_SYMBOL(genphy_update_link);
  576. /**
  577. * genphy_read_status - check the link status and update current link state
  578. * @phydev: target phy_device struct
  579. *
  580. * Description: Check the link, then figure out the current state
  581. * by comparing what we advertise with what the link partner
  582. * advertises. Start by checking the gigabit possibilities,
  583. * then move on to 10/100.
  584. */
  585. int genphy_read_status(struct phy_device *phydev)
  586. {
  587. int adv;
  588. int err;
  589. int lpa;
  590. int lpagb = 0;
  591. /* Update the link, but return if there
  592. * was an error */
  593. err = genphy_update_link(phydev);
  594. if (err)
  595. return err;
  596. if (AUTONEG_ENABLE == phydev->autoneg) {
  597. if (phydev->supported & (SUPPORTED_1000baseT_Half
  598. | SUPPORTED_1000baseT_Full)) {
  599. lpagb = phy_read(phydev, MII_STAT1000);
  600. if (lpagb < 0)
  601. return lpagb;
  602. adv = phy_read(phydev, MII_CTRL1000);
  603. if (adv < 0)
  604. return adv;
  605. lpagb &= adv << 2;
  606. }
  607. lpa = phy_read(phydev, MII_LPA);
  608. if (lpa < 0)
  609. return lpa;
  610. adv = phy_read(phydev, MII_ADVERTISE);
  611. if (adv < 0)
  612. return adv;
  613. lpa &= adv;
  614. phydev->speed = SPEED_10;
  615. phydev->duplex = DUPLEX_HALF;
  616. phydev->pause = phydev->asym_pause = 0;
  617. if (lpagb & (LPA_1000FULL | LPA_1000HALF)) {
  618. phydev->speed = SPEED_1000;
  619. if (lpagb & LPA_1000FULL)
  620. phydev->duplex = DUPLEX_FULL;
  621. } else if (lpa & (LPA_100FULL | LPA_100HALF)) {
  622. phydev->speed = SPEED_100;
  623. if (lpa & LPA_100FULL)
  624. phydev->duplex = DUPLEX_FULL;
  625. } else
  626. if (lpa & LPA_10FULL)
  627. phydev->duplex = DUPLEX_FULL;
  628. if (phydev->duplex == DUPLEX_FULL){
  629. phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
  630. phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
  631. }
  632. } else {
  633. int bmcr = phy_read(phydev, MII_BMCR);
  634. if (bmcr < 0)
  635. return bmcr;
  636. if (bmcr & BMCR_FULLDPLX)
  637. phydev->duplex = DUPLEX_FULL;
  638. else
  639. phydev->duplex = DUPLEX_HALF;
  640. if (bmcr & BMCR_SPEED1000)
  641. phydev->speed = SPEED_1000;
  642. else if (bmcr & BMCR_SPEED100)
  643. phydev->speed = SPEED_100;
  644. else
  645. phydev->speed = SPEED_10;
  646. phydev->pause = phydev->asym_pause = 0;
  647. }
  648. return 0;
  649. }
  650. EXPORT_SYMBOL(genphy_read_status);
  651. static int genphy_config_init(struct phy_device *phydev)
  652. {
  653. int val;
  654. u32 features;
  655. /* For now, I'll claim that the generic driver supports
  656. * all possible port types */
  657. features = (SUPPORTED_TP | SUPPORTED_MII
  658. | SUPPORTED_AUI | SUPPORTED_FIBRE |
  659. SUPPORTED_BNC);
  660. /* Do we support autonegotiation? */
  661. val = phy_read(phydev, MII_BMSR);
  662. if (val < 0)
  663. return val;
  664. if (val & BMSR_ANEGCAPABLE)
  665. features |= SUPPORTED_Autoneg;
  666. if (val & BMSR_100FULL)
  667. features |= SUPPORTED_100baseT_Full;
  668. if (val & BMSR_100HALF)
  669. features |= SUPPORTED_100baseT_Half;
  670. if (val & BMSR_10FULL)
  671. features |= SUPPORTED_10baseT_Full;
  672. if (val & BMSR_10HALF)
  673. features |= SUPPORTED_10baseT_Half;
  674. if (val & BMSR_ESTATEN) {
  675. val = phy_read(phydev, MII_ESTATUS);
  676. if (val < 0)
  677. return val;
  678. if (val & ESTATUS_1000_TFULL)
  679. features |= SUPPORTED_1000baseT_Full;
  680. if (val & ESTATUS_1000_THALF)
  681. features |= SUPPORTED_1000baseT_Half;
  682. }
  683. phydev->supported = features;
  684. phydev->advertising = features;
  685. return 0;
  686. }
  687. int genphy_suspend(struct phy_device *phydev)
  688. {
  689. int value;
  690. mutex_lock(&phydev->lock);
  691. value = phy_read(phydev, MII_BMCR);
  692. phy_write(phydev, MII_BMCR, (value | BMCR_PDOWN));
  693. mutex_unlock(&phydev->lock);
  694. return 0;
  695. }
  696. EXPORT_SYMBOL(genphy_suspend);
  697. int genphy_resume(struct phy_device *phydev)
  698. {
  699. int value;
  700. mutex_lock(&phydev->lock);
  701. value = phy_read(phydev, MII_BMCR);
  702. phy_write(phydev, MII_BMCR, (value & ~BMCR_PDOWN));
  703. mutex_unlock(&phydev->lock);
  704. return 0;
  705. }
  706. EXPORT_SYMBOL(genphy_resume);
  707. /**
  708. * phy_probe - probe and init a PHY device
  709. * @dev: device to probe and init
  710. *
  711. * Description: Take care of setting up the phy_device structure,
  712. * set the state to READY (the driver's init function should
  713. * set it to STARTING if needed).
  714. */
  715. static int phy_probe(struct device *dev)
  716. {
  717. struct phy_device *phydev;
  718. struct phy_driver *phydrv;
  719. struct device_driver *drv;
  720. int err = 0;
  721. phydev = to_phy_device(dev);
  722. /* Make sure the driver is held.
  723. * XXX -- Is this correct? */
  724. drv = get_driver(phydev->dev.driver);
  725. phydrv = to_phy_driver(drv);
  726. phydev->drv = phydrv;
  727. /* Disable the interrupt if the PHY doesn't support it */
  728. if (!(phydrv->flags & PHY_HAS_INTERRUPT))
  729. phydev->irq = PHY_POLL;
  730. mutex_lock(&phydev->lock);
  731. /* Start out supporting everything. Eventually,
  732. * a controller will attach, and may modify one
  733. * or both of these values */
  734. phydev->supported = phydrv->features;
  735. phydev->advertising = phydrv->features;
  736. /* Set the state to READY by default */
  737. phydev->state = PHY_READY;
  738. if (phydev->drv->probe)
  739. err = phydev->drv->probe(phydev);
  740. mutex_unlock(&phydev->lock);
  741. return err;
  742. }
  743. static int phy_remove(struct device *dev)
  744. {
  745. struct phy_device *phydev;
  746. phydev = to_phy_device(dev);
  747. mutex_lock(&phydev->lock);
  748. phydev->state = PHY_DOWN;
  749. mutex_unlock(&phydev->lock);
  750. if (phydev->drv->remove)
  751. phydev->drv->remove(phydev);
  752. put_driver(dev->driver);
  753. phydev->drv = NULL;
  754. return 0;
  755. }
  756. /**
  757. * phy_driver_register - register a phy_driver with the PHY layer
  758. * @new_driver: new phy_driver to register
  759. */
  760. int phy_driver_register(struct phy_driver *new_driver)
  761. {
  762. int retval;
  763. new_driver->driver.name = new_driver->name;
  764. new_driver->driver.bus = &mdio_bus_type;
  765. new_driver->driver.probe = phy_probe;
  766. new_driver->driver.remove = phy_remove;
  767. retval = driver_register(&new_driver->driver);
  768. if (retval) {
  769. printk(KERN_ERR "%s: Error %d in registering driver\n",
  770. new_driver->name, retval);
  771. return retval;
  772. }
  773. pr_debug("%s: Registered new driver\n", new_driver->name);
  774. return 0;
  775. }
  776. EXPORT_SYMBOL(phy_driver_register);
  777. void phy_driver_unregister(struct phy_driver *drv)
  778. {
  779. driver_unregister(&drv->driver);
  780. }
  781. EXPORT_SYMBOL(phy_driver_unregister);
  782. static struct phy_driver genphy_driver = {
  783. .phy_id = 0xffffffff,
  784. .phy_id_mask = 0xffffffff,
  785. .name = "Generic PHY",
  786. .config_init = genphy_config_init,
  787. .features = 0,
  788. .config_aneg = genphy_config_aneg,
  789. .read_status = genphy_read_status,
  790. .suspend = genphy_suspend,
  791. .resume = genphy_resume,
  792. .driver = {.owner= THIS_MODULE, },
  793. };
  794. static int __init phy_init(void)
  795. {
  796. int rc;
  797. rc = mdio_bus_init();
  798. if (rc)
  799. return rc;
  800. rc = phy_driver_register(&genphy_driver);
  801. if (rc)
  802. mdio_bus_exit();
  803. return rc;
  804. }
  805. static void __exit phy_exit(void)
  806. {
  807. phy_driver_unregister(&genphy_driver);
  808. mdio_bus_exit();
  809. }
  810. subsys_initcall(phy_init);
  811. module_exit(phy_exit);