phy_device.c 15 KB

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