phy_device.c 16 KB

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