phy_device.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  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. err = device_bind_driver(d);
  177. up_write(&d->bus->subsys.rwsem);
  178. if (err)
  179. return ERR_PTR(err);
  180. }
  181. if (phydev->attached_dev) {
  182. printk(KERN_ERR "%s: %s already attached\n",
  183. dev->name, phy_id);
  184. return ERR_PTR(-EBUSY);
  185. }
  186. phydev->attached_dev = dev;
  187. phydev->dev_flags = flags;
  188. return phydev;
  189. }
  190. EXPORT_SYMBOL(phy_attach);
  191. void phy_detach(struct phy_device *phydev)
  192. {
  193. phydev->attached_dev = NULL;
  194. /* If the device had no specific driver before (i.e. - it
  195. * was using the generic driver), we unbind the device
  196. * from the generic driver so that there's a chance a
  197. * real driver could be loaded */
  198. if (phydev->dev.driver == &genphy_driver.driver) {
  199. down_write(&phydev->dev.bus->subsys.rwsem);
  200. device_release_driver(&phydev->dev);
  201. up_write(&phydev->dev.bus->subsys.rwsem);
  202. }
  203. }
  204. EXPORT_SYMBOL(phy_detach);
  205. /* Generic PHY support and helper functions */
  206. /* genphy_config_advert
  207. *
  208. * description: Writes MII_ADVERTISE with the appropriate values,
  209. * after sanitizing the values to make sure we only advertise
  210. * what is supported
  211. */
  212. int genphy_config_advert(struct phy_device *phydev)
  213. {
  214. u32 advertise;
  215. int adv;
  216. int err;
  217. /* Only allow advertising what
  218. * this PHY supports */
  219. phydev->advertising &= phydev->supported;
  220. advertise = phydev->advertising;
  221. /* Setup standard advertisement */
  222. adv = phy_read(phydev, MII_ADVERTISE);
  223. if (adv < 0)
  224. return adv;
  225. adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
  226. ADVERTISE_PAUSE_ASYM);
  227. if (advertise & ADVERTISED_10baseT_Half)
  228. adv |= ADVERTISE_10HALF;
  229. if (advertise & ADVERTISED_10baseT_Full)
  230. adv |= ADVERTISE_10FULL;
  231. if (advertise & ADVERTISED_100baseT_Half)
  232. adv |= ADVERTISE_100HALF;
  233. if (advertise & ADVERTISED_100baseT_Full)
  234. adv |= ADVERTISE_100FULL;
  235. if (advertise & ADVERTISED_Pause)
  236. adv |= ADVERTISE_PAUSE_CAP;
  237. if (advertise & ADVERTISED_Asym_Pause)
  238. adv |= ADVERTISE_PAUSE_ASYM;
  239. err = phy_write(phydev, MII_ADVERTISE, adv);
  240. if (err < 0)
  241. return err;
  242. /* Configure gigabit if it's supported */
  243. if (phydev->supported & (SUPPORTED_1000baseT_Half |
  244. SUPPORTED_1000baseT_Full)) {
  245. adv = phy_read(phydev, MII_CTRL1000);
  246. if (adv < 0)
  247. return adv;
  248. adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
  249. if (advertise & SUPPORTED_1000baseT_Half)
  250. adv |= ADVERTISE_1000HALF;
  251. if (advertise & SUPPORTED_1000baseT_Full)
  252. adv |= ADVERTISE_1000FULL;
  253. err = phy_write(phydev, MII_CTRL1000, adv);
  254. if (err < 0)
  255. return err;
  256. }
  257. return adv;
  258. }
  259. EXPORT_SYMBOL(genphy_config_advert);
  260. /* genphy_setup_forced
  261. *
  262. * description: Configures MII_BMCR to force speed/duplex
  263. * to the values in phydev. Assumes that the values are valid.
  264. * Please see phy_sanitize_settings() */
  265. int genphy_setup_forced(struct phy_device *phydev)
  266. {
  267. int ctl = BMCR_RESET;
  268. phydev->pause = phydev->asym_pause = 0;
  269. if (SPEED_1000 == phydev->speed)
  270. ctl |= BMCR_SPEED1000;
  271. else if (SPEED_100 == phydev->speed)
  272. ctl |= BMCR_SPEED100;
  273. if (DUPLEX_FULL == phydev->duplex)
  274. ctl |= BMCR_FULLDPLX;
  275. ctl = phy_write(phydev, MII_BMCR, ctl);
  276. if (ctl < 0)
  277. return ctl;
  278. /* We just reset the device, so we'd better configure any
  279. * settings the PHY requires to operate */
  280. if (phydev->drv->config_init)
  281. ctl = phydev->drv->config_init(phydev);
  282. return ctl;
  283. }
  284. /* Enable and Restart Autonegotiation */
  285. int genphy_restart_aneg(struct phy_device *phydev)
  286. {
  287. int ctl;
  288. ctl = phy_read(phydev, MII_BMCR);
  289. if (ctl < 0)
  290. return ctl;
  291. ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
  292. /* Don't isolate the PHY if we're negotiating */
  293. ctl &= ~(BMCR_ISOLATE);
  294. ctl = phy_write(phydev, MII_BMCR, ctl);
  295. return ctl;
  296. }
  297. /* genphy_config_aneg
  298. *
  299. * description: If auto-negotiation is enabled, we configure the
  300. * advertising, and then restart auto-negotiation. If it is not
  301. * enabled, then we write the BMCR
  302. */
  303. int genphy_config_aneg(struct phy_device *phydev)
  304. {
  305. int err = 0;
  306. if (AUTONEG_ENABLE == phydev->autoneg) {
  307. err = genphy_config_advert(phydev);
  308. if (err < 0)
  309. return err;
  310. err = genphy_restart_aneg(phydev);
  311. } else
  312. err = genphy_setup_forced(phydev);
  313. return err;
  314. }
  315. EXPORT_SYMBOL(genphy_config_aneg);
  316. /* genphy_update_link
  317. *
  318. * description: Update the value in phydev->link to reflect the
  319. * current link value. In order to do this, we need to read
  320. * the status register twice, keeping the second value
  321. */
  322. int genphy_update_link(struct phy_device *phydev)
  323. {
  324. int status;
  325. /* Do a fake read */
  326. status = phy_read(phydev, MII_BMSR);
  327. if (status < 0)
  328. return status;
  329. /* Read link and autonegotiation status */
  330. status = phy_read(phydev, MII_BMSR);
  331. if (status < 0)
  332. return status;
  333. if ((status & BMSR_LSTATUS) == 0)
  334. phydev->link = 0;
  335. else
  336. phydev->link = 1;
  337. return 0;
  338. }
  339. /* genphy_read_status
  340. *
  341. * description: Check the link, then figure out the current state
  342. * by comparing what we advertise with what the link partner
  343. * advertises. Start by checking the gigabit possibilities,
  344. * then move on to 10/100.
  345. */
  346. int genphy_read_status(struct phy_device *phydev)
  347. {
  348. int adv;
  349. int err;
  350. int lpa;
  351. int lpagb = 0;
  352. /* Update the link, but return if there
  353. * was an error */
  354. err = genphy_update_link(phydev);
  355. if (err)
  356. return err;
  357. if (AUTONEG_ENABLE == phydev->autoneg) {
  358. if (phydev->supported & (SUPPORTED_1000baseT_Half
  359. | SUPPORTED_1000baseT_Full)) {
  360. lpagb = phy_read(phydev, MII_STAT1000);
  361. if (lpagb < 0)
  362. return lpagb;
  363. adv = phy_read(phydev, MII_CTRL1000);
  364. if (adv < 0)
  365. return adv;
  366. lpagb &= adv << 2;
  367. }
  368. lpa = phy_read(phydev, MII_LPA);
  369. if (lpa < 0)
  370. return lpa;
  371. adv = phy_read(phydev, MII_ADVERTISE);
  372. if (adv < 0)
  373. return adv;
  374. lpa &= adv;
  375. phydev->speed = SPEED_10;
  376. phydev->duplex = DUPLEX_HALF;
  377. phydev->pause = phydev->asym_pause = 0;
  378. if (lpagb & (LPA_1000FULL | LPA_1000HALF)) {
  379. phydev->speed = SPEED_1000;
  380. if (lpagb & LPA_1000FULL)
  381. phydev->duplex = DUPLEX_FULL;
  382. } else if (lpa & (LPA_100FULL | LPA_100HALF)) {
  383. phydev->speed = SPEED_100;
  384. if (lpa & LPA_100FULL)
  385. phydev->duplex = DUPLEX_FULL;
  386. } else
  387. if (lpa & LPA_10FULL)
  388. phydev->duplex = DUPLEX_FULL;
  389. if (phydev->duplex == DUPLEX_FULL){
  390. phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
  391. phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
  392. }
  393. } else {
  394. int bmcr = phy_read(phydev, MII_BMCR);
  395. if (bmcr < 0)
  396. return bmcr;
  397. if (bmcr & BMCR_FULLDPLX)
  398. phydev->duplex = DUPLEX_FULL;
  399. else
  400. phydev->duplex = DUPLEX_HALF;
  401. if (bmcr & BMCR_SPEED1000)
  402. phydev->speed = SPEED_1000;
  403. else if (bmcr & BMCR_SPEED100)
  404. phydev->speed = SPEED_100;
  405. else
  406. phydev->speed = SPEED_10;
  407. phydev->pause = phydev->asym_pause = 0;
  408. }
  409. return 0;
  410. }
  411. EXPORT_SYMBOL(genphy_read_status);
  412. static int genphy_config_init(struct phy_device *phydev)
  413. {
  414. int val;
  415. u32 features;
  416. /* For now, I'll claim that the generic driver supports
  417. * all possible port types */
  418. features = (SUPPORTED_TP | SUPPORTED_MII
  419. | SUPPORTED_AUI | SUPPORTED_FIBRE |
  420. SUPPORTED_BNC);
  421. /* Do we support autonegotiation? */
  422. val = phy_read(phydev, MII_BMSR);
  423. if (val < 0)
  424. return val;
  425. if (val & BMSR_ANEGCAPABLE)
  426. features |= SUPPORTED_Autoneg;
  427. if (val & BMSR_100FULL)
  428. features |= SUPPORTED_100baseT_Full;
  429. if (val & BMSR_100HALF)
  430. features |= SUPPORTED_100baseT_Half;
  431. if (val & BMSR_10FULL)
  432. features |= SUPPORTED_10baseT_Full;
  433. if (val & BMSR_10HALF)
  434. features |= SUPPORTED_10baseT_Half;
  435. if (val & BMSR_ESTATEN) {
  436. val = phy_read(phydev, MII_ESTATUS);
  437. if (val < 0)
  438. return val;
  439. if (val & ESTATUS_1000_TFULL)
  440. features |= SUPPORTED_1000baseT_Full;
  441. if (val & ESTATUS_1000_THALF)
  442. features |= SUPPORTED_1000baseT_Half;
  443. }
  444. phydev->supported = features;
  445. phydev->advertising = features;
  446. return 0;
  447. }
  448. /* phy_probe
  449. *
  450. * description: Take care of setting up the phy_device structure,
  451. * set the state to READY (the driver's init function should
  452. * set it to STARTING if needed).
  453. */
  454. static int phy_probe(struct device *dev)
  455. {
  456. struct phy_device *phydev;
  457. struct phy_driver *phydrv;
  458. struct device_driver *drv;
  459. int err = 0;
  460. phydev = to_phy_device(dev);
  461. /* Make sure the driver is held.
  462. * XXX -- Is this correct? */
  463. drv = get_driver(phydev->dev.driver);
  464. phydrv = to_phy_driver(drv);
  465. phydev->drv = phydrv;
  466. /* Disable the interrupt if the PHY doesn't support it */
  467. if (!(phydrv->flags & PHY_HAS_INTERRUPT))
  468. phydev->irq = PHY_POLL;
  469. spin_lock(&phydev->lock);
  470. /* Start out supporting everything. Eventually,
  471. * a controller will attach, and may modify one
  472. * or both of these values */
  473. phydev->supported = phydrv->features;
  474. phydev->advertising = phydrv->features;
  475. /* Set the state to READY by default */
  476. phydev->state = PHY_READY;
  477. if (phydev->drv->probe)
  478. err = phydev->drv->probe(phydev);
  479. spin_unlock(&phydev->lock);
  480. if (err < 0)
  481. return err;
  482. if (phydev->drv->config_init)
  483. err = phydev->drv->config_init(phydev);
  484. return err;
  485. }
  486. static int phy_remove(struct device *dev)
  487. {
  488. struct phy_device *phydev;
  489. phydev = to_phy_device(dev);
  490. spin_lock(&phydev->lock);
  491. phydev->state = PHY_DOWN;
  492. spin_unlock(&phydev->lock);
  493. if (phydev->drv->remove)
  494. phydev->drv->remove(phydev);
  495. put_driver(dev->driver);
  496. phydev->drv = NULL;
  497. return 0;
  498. }
  499. int phy_driver_register(struct phy_driver *new_driver)
  500. {
  501. int retval;
  502. memset(&new_driver->driver, 0, sizeof(new_driver->driver));
  503. new_driver->driver.name = new_driver->name;
  504. new_driver->driver.bus = &mdio_bus_type;
  505. new_driver->driver.probe = phy_probe;
  506. new_driver->driver.remove = phy_remove;
  507. retval = driver_register(&new_driver->driver);
  508. if (retval) {
  509. printk(KERN_ERR "%s: Error %d in registering driver\n",
  510. new_driver->name, retval);
  511. return retval;
  512. }
  513. pr_info("%s: Registered new driver\n", new_driver->name);
  514. return 0;
  515. }
  516. EXPORT_SYMBOL(phy_driver_register);
  517. void phy_driver_unregister(struct phy_driver *drv)
  518. {
  519. driver_unregister(&drv->driver);
  520. }
  521. EXPORT_SYMBOL(phy_driver_unregister);
  522. static struct phy_driver genphy_driver = {
  523. .phy_id = 0xffffffff,
  524. .phy_id_mask = 0xffffffff,
  525. .name = "Generic PHY",
  526. .config_init = genphy_config_init,
  527. .features = 0,
  528. .config_aneg = genphy_config_aneg,
  529. .read_status = genphy_read_status,
  530. .driver = {.owner= THIS_MODULE, },
  531. };
  532. static int __init phy_init(void)
  533. {
  534. int rc;
  535. rc = mdio_bus_init();
  536. if (rc)
  537. return rc;
  538. rc = phy_driver_register(&genphy_driver);
  539. if (rc)
  540. mdio_bus_exit();
  541. return rc;
  542. }
  543. static void __exit phy_exit(void)
  544. {
  545. phy_driver_unregister(&genphy_driver);
  546. mdio_bus_exit();
  547. }
  548. subsys_initcall(phy_init);
  549. module_exit(phy_exit);