phy_device.c 15 KB

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