phy_device.c 15 KB

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