phy_device.c 15 KB

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