phy_device.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  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/sched.h>
  19. #include <linux/string.h>
  20. #include <linux/errno.h>
  21. #include <linux/unistd.h>
  22. #include <linux/slab.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/init.h>
  25. #include <linux/delay.h>
  26. #include <linux/netdevice.h>
  27. #include <linux/etherdevice.h>
  28. #include <linux/skbuff.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/mm.h>
  31. #include <linux/module.h>
  32. #include <linux/mii.h>
  33. #include <linux/ethtool.h>
  34. #include <linux/phy.h>
  35. #include <asm/io.h>
  36. #include <asm/irq.h>
  37. #include <asm/uaccess.h>
  38. MODULE_DESCRIPTION("PHY library");
  39. MODULE_AUTHOR("Andy Fleming");
  40. MODULE_LICENSE("GPL");
  41. static struct phy_driver genphy_driver;
  42. extern int mdio_bus_init(void);
  43. extern void mdio_bus_exit(void);
  44. /* get_phy_device
  45. *
  46. * description: Reads the ID registers of the PHY at addr on the
  47. * bus, then allocates and returns the phy_device to
  48. * represent it.
  49. */
  50. struct phy_device * get_phy_device(struct mii_bus *bus, int addr)
  51. {
  52. int phy_reg;
  53. u32 phy_id;
  54. struct phy_device *dev = NULL;
  55. /* Grab the bits from PHYIR1, and put them
  56. * in the upper half */
  57. phy_reg = bus->read(bus, addr, MII_PHYSID1);
  58. if (phy_reg < 0)
  59. return ERR_PTR(phy_reg);
  60. phy_id = (phy_reg & 0xffff) << 16;
  61. /* Grab the bits from PHYIR2, and put them in the lower half */
  62. phy_reg = bus->read(bus, addr, MII_PHYSID2);
  63. if (phy_reg < 0)
  64. return ERR_PTR(phy_reg);
  65. phy_id |= (phy_reg & 0xffff);
  66. /* If the phy_id is all Fs, there is no device there */
  67. if (0xffffffff == phy_id)
  68. return NULL;
  69. /* Otherwise, we allocate the device, and initialize the
  70. * default values */
  71. dev = kcalloc(1, sizeof(*dev), GFP_KERNEL);
  72. if (NULL == dev)
  73. return ERR_PTR(-ENOMEM);
  74. dev->speed = 0;
  75. dev->duplex = -1;
  76. dev->pause = dev->asym_pause = 0;
  77. dev->link = 1;
  78. dev->autoneg = AUTONEG_ENABLE;
  79. dev->addr = addr;
  80. dev->phy_id = phy_id;
  81. dev->bus = bus;
  82. dev->state = PHY_DOWN;
  83. spin_lock_init(&dev->lock);
  84. return dev;
  85. }
  86. /* phy_prepare_link:
  87. *
  88. * description: Tells the PHY infrastructure to handle the
  89. * gory details on monitoring link status (whether through
  90. * polling or an interrupt), and to call back to the
  91. * connected device driver when the link status changes.
  92. * If you want to monitor your own link state, don't call
  93. * this function */
  94. void phy_prepare_link(struct phy_device *phydev,
  95. void (*handler)(struct net_device *))
  96. {
  97. phydev->adjust_link = handler;
  98. }
  99. /* phy_connect:
  100. *
  101. * description: Convenience function for connecting ethernet
  102. * devices to PHY devices. The default behavior is for
  103. * the PHY infrastructure to handle everything, and only notify
  104. * the connected driver when the link status changes. If you
  105. * don't want, or can't use the provided functionality, you may
  106. * choose to call only the subset of functions which provide
  107. * the desired functionality.
  108. */
  109. struct phy_device * phy_connect(struct net_device *dev, const char *phy_id,
  110. void (*handler)(struct net_device *), u32 flags)
  111. {
  112. struct phy_device *phydev;
  113. phydev = phy_attach(dev, phy_id, flags);
  114. if (IS_ERR(phydev))
  115. return phydev;
  116. phy_prepare_link(phydev, handler);
  117. phy_start_machine(phydev, NULL);
  118. if (phydev->irq > 0)
  119. phy_start_interrupts(phydev);
  120. return phydev;
  121. }
  122. EXPORT_SYMBOL(phy_connect);
  123. void phy_disconnect(struct phy_device *phydev)
  124. {
  125. if (phydev->irq > 0)
  126. phy_stop_interrupts(phydev);
  127. phy_stop_machine(phydev);
  128. phydev->adjust_link = NULL;
  129. phy_detach(phydev);
  130. }
  131. EXPORT_SYMBOL(phy_disconnect);
  132. /* phy_attach:
  133. *
  134. * description: Called by drivers to attach to a particular PHY
  135. * device. The phy_device is found, and properly hooked up
  136. * to the phy_driver. If no driver is attached, then the
  137. * genphy_driver is used. The phy_device is given a ptr to
  138. * the attaching device, and given a callback for link status
  139. * change. The phy_device is returned to the attaching
  140. * driver.
  141. */
  142. static int phy_compare_id(struct device *dev, void *data)
  143. {
  144. return strcmp((char *)data, dev->bus_id) ? 0 : 1;
  145. }
  146. struct phy_device *phy_attach(struct net_device *dev,
  147. const char *phy_id, u32 flags)
  148. {
  149. struct bus_type *bus = &mdio_bus_type;
  150. struct phy_device *phydev;
  151. struct device *d;
  152. /* Search the list of PHY devices on the mdio bus for the
  153. * PHY with the requested name */
  154. d = bus_find_device(bus, NULL, (void *)phy_id, phy_compare_id);
  155. if (d) {
  156. phydev = to_phy_device(d);
  157. } else {
  158. printk(KERN_ERR "%s not found\n", phy_id);
  159. return ERR_PTR(-ENODEV);
  160. }
  161. /* Assume that if there is no driver, that it doesn't
  162. * exist, and we should use the genphy driver. */
  163. if (NULL == d->driver) {
  164. int err;
  165. down_write(&d->bus->subsys.rwsem);
  166. d->driver = &genphy_driver.driver;
  167. err = d->driver->probe(d);
  168. if (err < 0)
  169. return ERR_PTR(err);
  170. device_bind_driver(d);
  171. up_write(&d->bus->subsys.rwsem);
  172. }
  173. if (phydev->attached_dev) {
  174. printk(KERN_ERR "%s: %s already attached\n",
  175. dev->name, phy_id);
  176. return ERR_PTR(-EBUSY);
  177. }
  178. phydev->attached_dev = dev;
  179. phydev->dev_flags = flags;
  180. return phydev;
  181. }
  182. EXPORT_SYMBOL(phy_attach);
  183. void phy_detach(struct phy_device *phydev)
  184. {
  185. phydev->attached_dev = NULL;
  186. /* If the device had no specific driver before (i.e. - it
  187. * was using the generic driver), we unbind the device
  188. * from the generic driver so that there's a chance a
  189. * real driver could be loaded */
  190. if (phydev->dev.driver == &genphy_driver.driver) {
  191. down_write(&phydev->dev.bus->subsys.rwsem);
  192. device_release_driver(&phydev->dev);
  193. up_write(&phydev->dev.bus->subsys.rwsem);
  194. }
  195. }
  196. EXPORT_SYMBOL(phy_detach);
  197. /* Generic PHY support and helper functions */
  198. /* genphy_config_advert
  199. *
  200. * description: Writes MII_ADVERTISE with the appropriate values,
  201. * after sanitizing the values to make sure we only advertise
  202. * what is supported
  203. */
  204. int genphy_config_advert(struct phy_device *phydev)
  205. {
  206. u32 advertise;
  207. int adv;
  208. int err;
  209. /* Only allow advertising what
  210. * this PHY supports */
  211. phydev->advertising &= phydev->supported;
  212. advertise = phydev->advertising;
  213. /* Setup standard advertisement */
  214. adv = phy_read(phydev, MII_ADVERTISE);
  215. if (adv < 0)
  216. return adv;
  217. adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
  218. ADVERTISE_PAUSE_ASYM);
  219. if (advertise & ADVERTISED_10baseT_Half)
  220. adv |= ADVERTISE_10HALF;
  221. if (advertise & ADVERTISED_10baseT_Full)
  222. adv |= ADVERTISE_10FULL;
  223. if (advertise & ADVERTISED_100baseT_Half)
  224. adv |= ADVERTISE_100HALF;
  225. if (advertise & ADVERTISED_100baseT_Full)
  226. adv |= ADVERTISE_100FULL;
  227. if (advertise & ADVERTISED_Pause)
  228. adv |= ADVERTISE_PAUSE_CAP;
  229. if (advertise & ADVERTISED_Asym_Pause)
  230. adv |= ADVERTISE_PAUSE_ASYM;
  231. err = phy_write(phydev, MII_ADVERTISE, adv);
  232. if (err < 0)
  233. return err;
  234. /* Configure gigabit if it's supported */
  235. if (phydev->supported & (SUPPORTED_1000baseT_Half |
  236. SUPPORTED_1000baseT_Full)) {
  237. adv = phy_read(phydev, MII_CTRL1000);
  238. if (adv < 0)
  239. return adv;
  240. adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
  241. if (advertise & SUPPORTED_1000baseT_Half)
  242. adv |= ADVERTISE_1000HALF;
  243. if (advertise & SUPPORTED_1000baseT_Full)
  244. adv |= ADVERTISE_1000FULL;
  245. err = phy_write(phydev, MII_CTRL1000, adv);
  246. if (err < 0)
  247. return err;
  248. }
  249. return adv;
  250. }
  251. EXPORT_SYMBOL(genphy_config_advert);
  252. /* genphy_setup_forced
  253. *
  254. * description: Configures MII_BMCR to force speed/duplex
  255. * to the values in phydev. Assumes that the values are valid.
  256. * Please see phy_sanitize_settings() */
  257. int genphy_setup_forced(struct phy_device *phydev)
  258. {
  259. int ctl = BMCR_RESET;
  260. phydev->pause = phydev->asym_pause = 0;
  261. if (SPEED_1000 == phydev->speed)
  262. ctl |= BMCR_SPEED1000;
  263. else if (SPEED_100 == phydev->speed)
  264. ctl |= BMCR_SPEED100;
  265. if (DUPLEX_FULL == phydev->duplex)
  266. ctl |= BMCR_FULLDPLX;
  267. ctl = phy_write(phydev, MII_BMCR, ctl);
  268. if (ctl < 0)
  269. return ctl;
  270. /* We just reset the device, so we'd better configure any
  271. * settings the PHY requires to operate */
  272. if (phydev->drv->config_init)
  273. ctl = phydev->drv->config_init(phydev);
  274. return ctl;
  275. }
  276. /* Enable and Restart Autonegotiation */
  277. int genphy_restart_aneg(struct phy_device *phydev)
  278. {
  279. int ctl;
  280. ctl = phy_read(phydev, MII_BMCR);
  281. if (ctl < 0)
  282. return ctl;
  283. ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
  284. /* Don't isolate the PHY if we're negotiating */
  285. ctl &= ~(BMCR_ISOLATE);
  286. ctl = phy_write(phydev, MII_BMCR, ctl);
  287. return ctl;
  288. }
  289. /* genphy_config_aneg
  290. *
  291. * description: If auto-negotiation is enabled, we configure the
  292. * advertising, and then restart auto-negotiation. If it is not
  293. * enabled, then we write the BMCR
  294. */
  295. int genphy_config_aneg(struct phy_device *phydev)
  296. {
  297. int err = 0;
  298. if (AUTONEG_ENABLE == phydev->autoneg) {
  299. err = genphy_config_advert(phydev);
  300. if (err < 0)
  301. return err;
  302. err = genphy_restart_aneg(phydev);
  303. } else
  304. err = genphy_setup_forced(phydev);
  305. return err;
  306. }
  307. EXPORT_SYMBOL(genphy_config_aneg);
  308. /* genphy_update_link
  309. *
  310. * description: Update the value in phydev->link to reflect the
  311. * current link value. In order to do this, we need to read
  312. * the status register twice, keeping the second value
  313. */
  314. int genphy_update_link(struct phy_device *phydev)
  315. {
  316. int status;
  317. /* Do a fake read */
  318. status = phy_read(phydev, MII_BMSR);
  319. if (status < 0)
  320. return status;
  321. /* Read link and autonegotiation status */
  322. status = phy_read(phydev, MII_BMSR);
  323. if (status < 0)
  324. return status;
  325. if ((status & BMSR_LSTATUS) == 0)
  326. phydev->link = 0;
  327. else
  328. phydev->link = 1;
  329. return 0;
  330. }
  331. /* genphy_read_status
  332. *
  333. * description: Check the link, then figure out the current state
  334. * by comparing what we advertise with what the link partner
  335. * advertises. Start by checking the gigabit possibilities,
  336. * then move on to 10/100.
  337. */
  338. int genphy_read_status(struct phy_device *phydev)
  339. {
  340. int adv;
  341. int err;
  342. int lpa;
  343. int lpagb = 0;
  344. /* Update the link, but return if there
  345. * was an error */
  346. err = genphy_update_link(phydev);
  347. if (err)
  348. return err;
  349. if (AUTONEG_ENABLE == phydev->autoneg) {
  350. if (phydev->supported & (SUPPORTED_1000baseT_Half
  351. | SUPPORTED_1000baseT_Full)) {
  352. lpagb = phy_read(phydev, MII_STAT1000);
  353. if (lpagb < 0)
  354. return lpagb;
  355. adv = phy_read(phydev, MII_CTRL1000);
  356. if (adv < 0)
  357. return adv;
  358. lpagb &= adv << 2;
  359. }
  360. lpa = phy_read(phydev, MII_LPA);
  361. if (lpa < 0)
  362. return lpa;
  363. adv = phy_read(phydev, MII_ADVERTISE);
  364. if (adv < 0)
  365. return adv;
  366. lpa &= adv;
  367. phydev->speed = SPEED_10;
  368. phydev->duplex = DUPLEX_HALF;
  369. phydev->pause = phydev->asym_pause = 0;
  370. if (lpagb & (LPA_1000FULL | LPA_1000HALF)) {
  371. phydev->speed = SPEED_1000;
  372. if (lpagb & LPA_1000FULL)
  373. phydev->duplex = DUPLEX_FULL;
  374. } else if (lpa & (LPA_100FULL | LPA_100HALF)) {
  375. phydev->speed = SPEED_100;
  376. if (lpa & LPA_100FULL)
  377. phydev->duplex = DUPLEX_FULL;
  378. } else
  379. if (lpa & LPA_10FULL)
  380. phydev->duplex = DUPLEX_FULL;
  381. if (phydev->duplex == DUPLEX_FULL){
  382. phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
  383. phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
  384. }
  385. } else {
  386. int bmcr = phy_read(phydev, MII_BMCR);
  387. if (bmcr < 0)
  388. return bmcr;
  389. if (bmcr & BMCR_FULLDPLX)
  390. phydev->duplex = DUPLEX_FULL;
  391. else
  392. phydev->duplex = DUPLEX_HALF;
  393. if (bmcr & BMCR_SPEED1000)
  394. phydev->speed = SPEED_1000;
  395. else if (bmcr & BMCR_SPEED100)
  396. phydev->speed = SPEED_100;
  397. else
  398. phydev->speed = SPEED_10;
  399. phydev->pause = phydev->asym_pause = 0;
  400. }
  401. return 0;
  402. }
  403. EXPORT_SYMBOL(genphy_read_status);
  404. static int genphy_config_init(struct phy_device *phydev)
  405. {
  406. u32 val;
  407. u32 features;
  408. /* For now, I'll claim that the generic driver supports
  409. * all possible port types */
  410. features = (SUPPORTED_TP | SUPPORTED_MII
  411. | SUPPORTED_AUI | SUPPORTED_FIBRE |
  412. SUPPORTED_BNC);
  413. /* Do we support autonegotiation? */
  414. val = phy_read(phydev, MII_BMSR);
  415. if (val < 0)
  416. return val;
  417. if (val & BMSR_ANEGCAPABLE)
  418. features |= SUPPORTED_Autoneg;
  419. if (val & BMSR_100FULL)
  420. features |= SUPPORTED_100baseT_Full;
  421. if (val & BMSR_100HALF)
  422. features |= SUPPORTED_100baseT_Half;
  423. if (val & BMSR_10FULL)
  424. features |= SUPPORTED_10baseT_Full;
  425. if (val & BMSR_10HALF)
  426. features |= SUPPORTED_10baseT_Half;
  427. if (val & BMSR_ESTATEN) {
  428. val = phy_read(phydev, MII_ESTATUS);
  429. if (val < 0)
  430. return val;
  431. if (val & ESTATUS_1000_TFULL)
  432. features |= SUPPORTED_1000baseT_Full;
  433. if (val & ESTATUS_1000_THALF)
  434. features |= SUPPORTED_1000baseT_Half;
  435. }
  436. phydev->supported = features;
  437. phydev->advertising = features;
  438. return 0;
  439. }
  440. /* phy_probe
  441. *
  442. * description: Take care of setting up the phy_device structure,
  443. * set the state to READY (the driver's init function should
  444. * set it to STARTING if needed).
  445. */
  446. static int phy_probe(struct device *dev)
  447. {
  448. struct phy_device *phydev;
  449. struct phy_driver *phydrv;
  450. struct device_driver *drv;
  451. int err = 0;
  452. phydev = to_phy_device(dev);
  453. /* Make sure the driver is held.
  454. * XXX -- Is this correct? */
  455. drv = get_driver(phydev->dev.driver);
  456. phydrv = to_phy_driver(drv);
  457. phydev->drv = phydrv;
  458. /* Disable the interrupt if the PHY doesn't support it */
  459. if (!(phydrv->flags & PHY_HAS_INTERRUPT))
  460. phydev->irq = PHY_POLL;
  461. spin_lock(&phydev->lock);
  462. /* Start out supporting everything. Eventually,
  463. * a controller will attach, and may modify one
  464. * or both of these values */
  465. phydev->supported = phydrv->features;
  466. phydev->advertising = phydrv->features;
  467. /* Set the state to READY by default */
  468. phydev->state = PHY_READY;
  469. if (phydev->drv->probe)
  470. err = phydev->drv->probe(phydev);
  471. spin_unlock(&phydev->lock);
  472. if (err < 0)
  473. return err;
  474. if (phydev->drv->config_init)
  475. err = phydev->drv->config_init(phydev);
  476. return err;
  477. }
  478. static int phy_remove(struct device *dev)
  479. {
  480. struct phy_device *phydev;
  481. phydev = to_phy_device(dev);
  482. spin_lock(&phydev->lock);
  483. phydev->state = PHY_DOWN;
  484. spin_unlock(&phydev->lock);
  485. if (phydev->drv->remove)
  486. phydev->drv->remove(phydev);
  487. put_driver(dev->driver);
  488. phydev->drv = NULL;
  489. return 0;
  490. }
  491. int phy_driver_register(struct phy_driver *new_driver)
  492. {
  493. int retval;
  494. memset(&new_driver->driver, 0, sizeof(new_driver->driver));
  495. new_driver->driver.name = new_driver->name;
  496. new_driver->driver.bus = &mdio_bus_type;
  497. new_driver->driver.probe = phy_probe;
  498. new_driver->driver.remove = phy_remove;
  499. retval = driver_register(&new_driver->driver);
  500. if (retval) {
  501. printk(KERN_ERR "%s: Error %d in registering driver\n",
  502. new_driver->name, retval);
  503. return retval;
  504. }
  505. pr_info("%s: Registered new driver\n", new_driver->name);
  506. return 0;
  507. }
  508. EXPORT_SYMBOL(phy_driver_register);
  509. void phy_driver_unregister(struct phy_driver *drv)
  510. {
  511. driver_unregister(&drv->driver);
  512. }
  513. EXPORT_SYMBOL(phy_driver_unregister);
  514. static struct phy_driver genphy_driver = {
  515. .phy_id = 0xffffffff,
  516. .phy_id_mask = 0xffffffff,
  517. .name = "Generic PHY",
  518. .config_init = genphy_config_init,
  519. .features = 0,
  520. .config_aneg = genphy_config_aneg,
  521. .read_status = genphy_read_status,
  522. .driver = {.owner= THIS_MODULE, },
  523. };
  524. static int __init phy_init(void)
  525. {
  526. int rc;
  527. rc = mdio_bus_init();
  528. if (rc)
  529. return rc;
  530. rc = phy_driver_register(&genphy_driver);
  531. if (rc)
  532. mdio_bus_exit();
  533. return rc;
  534. }
  535. static void __exit phy_exit(void)
  536. {
  537. phy_driver_unregister(&genphy_driver);
  538. mdio_bus_exit();
  539. }
  540. subsys_initcall(phy_init);
  541. module_exit(phy_exit);