phy_device.c 16 KB

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