phy_device.c 16 KB

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