phy_device.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  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/mm.h>
  29. #include <linux/module.h>
  30. #include <linux/mii.h>
  31. #include <linux/ethtool.h>
  32. #include <linux/phy.h>
  33. #include <asm/io.h>
  34. #include <asm/irq.h>
  35. #include <asm/uaccess.h>
  36. MODULE_DESCRIPTION("PHY library");
  37. MODULE_AUTHOR("Andy Fleming");
  38. MODULE_LICENSE("GPL");
  39. void phy_device_free(struct phy_device *phydev)
  40. {
  41. kfree(phydev);
  42. }
  43. EXPORT_SYMBOL(phy_device_free);
  44. static void phy_device_release(struct device *dev)
  45. {
  46. phy_device_free(to_phy_device(dev));
  47. }
  48. static struct phy_driver genphy_driver;
  49. extern int mdio_bus_init(void);
  50. extern void mdio_bus_exit(void);
  51. static LIST_HEAD(phy_fixup_list);
  52. static DEFINE_MUTEX(phy_fixup_lock);
  53. static int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
  54. u32 flags, phy_interface_t interface);
  55. /*
  56. * Creates a new phy_fixup and adds it to the list
  57. * @bus_id: A string which matches phydev->dev.bus_id (or PHY_ANY_ID)
  58. * @phy_uid: Used to match against phydev->phy_id (the UID of the PHY)
  59. * It can also be PHY_ANY_UID
  60. * @phy_uid_mask: Applied to phydev->phy_id and fixup->phy_uid before
  61. * comparison
  62. * @run: The actual code to be run when a matching PHY is found
  63. */
  64. int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
  65. int (*run)(struct phy_device *))
  66. {
  67. struct phy_fixup *fixup;
  68. fixup = kzalloc(sizeof(struct phy_fixup), GFP_KERNEL);
  69. if (!fixup)
  70. return -ENOMEM;
  71. strlcpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id));
  72. fixup->phy_uid = phy_uid;
  73. fixup->phy_uid_mask = phy_uid_mask;
  74. fixup->run = run;
  75. mutex_lock(&phy_fixup_lock);
  76. list_add_tail(&fixup->list, &phy_fixup_list);
  77. mutex_unlock(&phy_fixup_lock);
  78. return 0;
  79. }
  80. EXPORT_SYMBOL(phy_register_fixup);
  81. /* Registers a fixup to be run on any PHY with the UID in phy_uid */
  82. int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
  83. int (*run)(struct phy_device *))
  84. {
  85. return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run);
  86. }
  87. EXPORT_SYMBOL(phy_register_fixup_for_uid);
  88. /* Registers a fixup to be run on the PHY with id string bus_id */
  89. int phy_register_fixup_for_id(const char *bus_id,
  90. int (*run)(struct phy_device *))
  91. {
  92. return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run);
  93. }
  94. EXPORT_SYMBOL(phy_register_fixup_for_id);
  95. /*
  96. * Returns 1 if fixup matches phydev in bus_id and phy_uid.
  97. * Fixups can be set to match any in one or more fields.
  98. */
  99. static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup)
  100. {
  101. if (strcmp(fixup->bus_id, dev_name(&phydev->dev)) != 0)
  102. if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0)
  103. return 0;
  104. if ((fixup->phy_uid & fixup->phy_uid_mask) !=
  105. (phydev->phy_id & fixup->phy_uid_mask))
  106. if (fixup->phy_uid != PHY_ANY_UID)
  107. return 0;
  108. return 1;
  109. }
  110. /* Runs any matching fixups for this phydev */
  111. int phy_scan_fixups(struct phy_device *phydev)
  112. {
  113. struct phy_fixup *fixup;
  114. mutex_lock(&phy_fixup_lock);
  115. list_for_each_entry(fixup, &phy_fixup_list, list) {
  116. if (phy_needs_fixup(phydev, fixup)) {
  117. int err;
  118. err = fixup->run(phydev);
  119. if (err < 0) {
  120. mutex_unlock(&phy_fixup_lock);
  121. return err;
  122. }
  123. }
  124. }
  125. mutex_unlock(&phy_fixup_lock);
  126. return 0;
  127. }
  128. EXPORT_SYMBOL(phy_scan_fixups);
  129. static struct phy_device* phy_device_create(struct mii_bus *bus,
  130. int addr, int phy_id)
  131. {
  132. struct phy_device *dev;
  133. /* We allocate the device, and initialize the
  134. * default values */
  135. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  136. if (NULL == dev)
  137. return (struct phy_device*) PTR_ERR((void*)-ENOMEM);
  138. dev->dev.release = phy_device_release;
  139. dev->speed = 0;
  140. dev->duplex = -1;
  141. dev->pause = dev->asym_pause = 0;
  142. dev->link = 1;
  143. dev->interface = PHY_INTERFACE_MODE_GMII;
  144. dev->autoneg = AUTONEG_ENABLE;
  145. dev->addr = addr;
  146. dev->phy_id = phy_id;
  147. dev->bus = bus;
  148. dev->dev.parent = bus->parent;
  149. dev->dev.bus = &mdio_bus_type;
  150. dev->irq = bus->irq != NULL ? bus->irq[addr] : PHY_POLL;
  151. dev_set_name(&dev->dev, PHY_ID_FMT, bus->id, addr);
  152. dev->state = PHY_DOWN;
  153. mutex_init(&dev->lock);
  154. INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine);
  155. /* Request the appropriate module unconditionally; don't
  156. bother trying to do so only if it isn't already loaded,
  157. because that gets complicated. A hotplug event would have
  158. done an unconditional modprobe anyway.
  159. We don't do normal hotplug because it won't work for MDIO
  160. -- because it relies on the device staying around for long
  161. enough for the driver to get loaded. With MDIO, the NIC
  162. driver will get bored and give up as soon as it finds that
  163. there's no driver _already_ loaded. */
  164. request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT, MDIO_ID_ARGS(phy_id));
  165. return dev;
  166. }
  167. /**
  168. * get_phy_id - reads the specified addr for its ID.
  169. * @bus: the target MII bus
  170. * @addr: PHY address on the MII bus
  171. * @phy_id: where to store the ID retrieved.
  172. *
  173. * Description: Reads the ID registers of the PHY at @addr on the
  174. * @bus, stores it in @phy_id and returns zero on success.
  175. */
  176. int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id)
  177. {
  178. int phy_reg;
  179. /* Grab the bits from PHYIR1, and put them
  180. * in the upper half */
  181. phy_reg = mdiobus_read(bus, addr, MII_PHYSID1);
  182. if (phy_reg < 0)
  183. return -EIO;
  184. *phy_id = (phy_reg & 0xffff) << 16;
  185. /* Grab the bits from PHYIR2, and put them in the lower half */
  186. phy_reg = mdiobus_read(bus, addr, MII_PHYSID2);
  187. if (phy_reg < 0)
  188. return -EIO;
  189. *phy_id |= (phy_reg & 0xffff);
  190. return 0;
  191. }
  192. EXPORT_SYMBOL(get_phy_id);
  193. /**
  194. * get_phy_device - reads the specified PHY device and returns its @phy_device struct
  195. * @bus: the target MII bus
  196. * @addr: PHY address on the MII bus
  197. *
  198. * Description: Reads the ID registers of the PHY at @addr on the
  199. * @bus, then allocates and returns the phy_device to represent it.
  200. */
  201. struct phy_device * get_phy_device(struct mii_bus *bus, int addr)
  202. {
  203. struct phy_device *dev = NULL;
  204. u32 phy_id;
  205. int r;
  206. r = get_phy_id(bus, addr, &phy_id);
  207. if (r)
  208. return ERR_PTR(r);
  209. /* If the phy_id is mostly Fs, there is no device there */
  210. if ((phy_id & 0x1fffffff) == 0x1fffffff)
  211. return NULL;
  212. dev = phy_device_create(bus, addr, phy_id);
  213. return dev;
  214. }
  215. EXPORT_SYMBOL(get_phy_device);
  216. /**
  217. * phy_device_register - Register the phy device on the MDIO bus
  218. * @phydev: phy_device structure to be added to the MDIO bus
  219. */
  220. int phy_device_register(struct phy_device *phydev)
  221. {
  222. int err;
  223. /* Don't register a phy if one is already registered at this
  224. * address */
  225. if (phydev->bus->phy_map[phydev->addr])
  226. return -EINVAL;
  227. phydev->bus->phy_map[phydev->addr] = phydev;
  228. /* Run all of the fixups for this PHY */
  229. phy_scan_fixups(phydev);
  230. err = device_register(&phydev->dev);
  231. if (err) {
  232. pr_err("phy %d failed to register\n", phydev->addr);
  233. goto out;
  234. }
  235. return 0;
  236. out:
  237. phydev->bus->phy_map[phydev->addr] = NULL;
  238. return err;
  239. }
  240. EXPORT_SYMBOL(phy_device_register);
  241. /**
  242. * phy_find_first - finds the first PHY device on the bus
  243. * @bus: the target MII bus
  244. */
  245. struct phy_device *phy_find_first(struct mii_bus *bus)
  246. {
  247. int addr;
  248. for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
  249. if (bus->phy_map[addr])
  250. return bus->phy_map[addr];
  251. }
  252. return NULL;
  253. }
  254. EXPORT_SYMBOL(phy_find_first);
  255. /**
  256. * phy_prepare_link - prepares the PHY layer to monitor link status
  257. * @phydev: target phy_device struct
  258. * @handler: callback function for link status change notifications
  259. *
  260. * Description: Tells the PHY infrastructure to handle the
  261. * gory details on monitoring link status (whether through
  262. * polling or an interrupt), and to call back to the
  263. * connected device driver when the link status changes.
  264. * If you want to monitor your own link state, don't call
  265. * this function.
  266. */
  267. static void phy_prepare_link(struct phy_device *phydev,
  268. void (*handler)(struct net_device *))
  269. {
  270. phydev->adjust_link = handler;
  271. }
  272. /**
  273. * phy_connect_direct - connect an ethernet device to a specific phy_device
  274. * @dev: the network device to connect
  275. * @phydev: the pointer to the phy device
  276. * @handler: callback function for state change notifications
  277. * @flags: PHY device's dev_flags
  278. * @interface: PHY device's interface
  279. */
  280. int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
  281. void (*handler)(struct net_device *), u32 flags,
  282. phy_interface_t interface)
  283. {
  284. int rc;
  285. if (!dev)
  286. return -EINVAL;
  287. rc = phy_attach_direct(dev, phydev, flags, interface);
  288. if (rc)
  289. return rc;
  290. phy_prepare_link(phydev, handler);
  291. phy_start_machine(phydev, NULL);
  292. if (phydev->irq > 0)
  293. phy_start_interrupts(phydev);
  294. return 0;
  295. }
  296. EXPORT_SYMBOL(phy_connect_direct);
  297. /**
  298. * phy_connect - connect an ethernet device to a PHY device
  299. * @dev: the network device to connect
  300. * @bus_id: the id string of the PHY device to connect
  301. * @handler: callback function for state change notifications
  302. * @flags: PHY device's dev_flags
  303. * @interface: PHY device's interface
  304. *
  305. * Description: Convenience function for connecting ethernet
  306. * devices to PHY devices. The default behavior is for
  307. * the PHY infrastructure to handle everything, and only notify
  308. * the connected driver when the link status changes. If you
  309. * don't want, or can't use the provided functionality, you may
  310. * choose to call only the subset of functions which provide
  311. * the desired functionality.
  312. */
  313. struct phy_device * phy_connect(struct net_device *dev, const char *bus_id,
  314. void (*handler)(struct net_device *), u32 flags,
  315. phy_interface_t interface)
  316. {
  317. struct phy_device *phydev;
  318. struct device *d;
  319. int rc;
  320. /* Search the list of PHY devices on the mdio bus for the
  321. * PHY with the requested name */
  322. d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id);
  323. if (!d) {
  324. pr_err("PHY %s not found\n", bus_id);
  325. return ERR_PTR(-ENODEV);
  326. }
  327. phydev = to_phy_device(d);
  328. rc = phy_connect_direct(dev, phydev, handler, flags, interface);
  329. if (rc)
  330. return ERR_PTR(rc);
  331. return phydev;
  332. }
  333. EXPORT_SYMBOL(phy_connect);
  334. /**
  335. * phy_disconnect - disable interrupts, stop state machine, and detach a PHY device
  336. * @phydev: target phy_device struct
  337. */
  338. void phy_disconnect(struct phy_device *phydev)
  339. {
  340. if (phydev->irq > 0)
  341. phy_stop_interrupts(phydev);
  342. phy_stop_machine(phydev);
  343. phydev->adjust_link = NULL;
  344. phy_detach(phydev);
  345. }
  346. EXPORT_SYMBOL(phy_disconnect);
  347. int phy_init_hw(struct phy_device *phydev)
  348. {
  349. int ret;
  350. if (!phydev->drv || !phydev->drv->config_init)
  351. return 0;
  352. ret = phy_scan_fixups(phydev);
  353. if (ret < 0)
  354. return ret;
  355. return phydev->drv->config_init(phydev);
  356. }
  357. /**
  358. * phy_attach_direct - attach a network device to a given PHY device pointer
  359. * @dev: network device to attach
  360. * @phydev: Pointer to phy_device to attach
  361. * @flags: PHY device's dev_flags
  362. * @interface: PHY device's interface
  363. *
  364. * Description: Called by drivers to attach to a particular PHY
  365. * device. The phy_device is found, and properly hooked up
  366. * to the phy_driver. If no driver is attached, then the
  367. * genphy_driver is used. The phy_device is given a ptr to
  368. * the attaching device, and given a callback for link status
  369. * change. The phy_device is returned to the attaching driver.
  370. */
  371. static int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
  372. u32 flags, phy_interface_t interface)
  373. {
  374. struct device *d = &phydev->dev;
  375. int err;
  376. /* Assume that if there is no driver, that it doesn't
  377. * exist, and we should use the genphy driver. */
  378. if (NULL == d->driver) {
  379. d->driver = &genphy_driver.driver;
  380. err = d->driver->probe(d);
  381. if (err >= 0)
  382. err = device_bind_driver(d);
  383. if (err)
  384. return err;
  385. }
  386. if (phydev->attached_dev) {
  387. dev_err(&dev->dev, "PHY already attached\n");
  388. return -EBUSY;
  389. }
  390. phydev->attached_dev = dev;
  391. dev->phydev = phydev;
  392. phydev->dev_flags = flags;
  393. phydev->interface = interface;
  394. phydev->state = PHY_READY;
  395. /* Do initial configuration here, now that
  396. * we have certain key parameters
  397. * (dev_flags and interface) */
  398. err = phy_init_hw(phydev);
  399. if (err)
  400. phy_detach(phydev);
  401. return err;
  402. }
  403. /**
  404. * phy_attach - attach a network device to a particular PHY device
  405. * @dev: network device to attach
  406. * @bus_id: Bus ID of PHY device to attach
  407. * @flags: PHY device's dev_flags
  408. * @interface: PHY device's interface
  409. *
  410. * Description: Same as phy_attach_direct() except that a PHY bus_id
  411. * string is passed instead of a pointer to a struct phy_device.
  412. */
  413. struct phy_device *phy_attach(struct net_device *dev,
  414. const char *bus_id, u32 flags, phy_interface_t interface)
  415. {
  416. struct bus_type *bus = &mdio_bus_type;
  417. struct phy_device *phydev;
  418. struct device *d;
  419. int rc;
  420. if (!dev)
  421. return ERR_PTR(-EINVAL);
  422. /* Search the list of PHY devices on the mdio bus for the
  423. * PHY with the requested name */
  424. d = bus_find_device_by_name(bus, NULL, bus_id);
  425. if (!d) {
  426. pr_err("PHY %s not found\n", bus_id);
  427. return ERR_PTR(-ENODEV);
  428. }
  429. phydev = to_phy_device(d);
  430. rc = phy_attach_direct(dev, phydev, flags, interface);
  431. if (rc)
  432. return ERR_PTR(rc);
  433. return phydev;
  434. }
  435. EXPORT_SYMBOL(phy_attach);
  436. /**
  437. * phy_detach - detach a PHY device from its network device
  438. * @phydev: target phy_device struct
  439. */
  440. void phy_detach(struct phy_device *phydev)
  441. {
  442. phydev->attached_dev->phydev = NULL;
  443. phydev->attached_dev = NULL;
  444. /* If the device had no specific driver before (i.e. - it
  445. * was using the generic driver), we unbind the device
  446. * from the generic driver so that there's a chance a
  447. * real driver could be loaded */
  448. if (phydev->dev.driver == &genphy_driver.driver)
  449. device_release_driver(&phydev->dev);
  450. }
  451. EXPORT_SYMBOL(phy_detach);
  452. /* Generic PHY support and helper functions */
  453. /**
  454. * genphy_config_advert - sanitize and advertise auto-negotiation parameters
  455. * @phydev: target phy_device struct
  456. *
  457. * Description: Writes MII_ADVERTISE with the appropriate values,
  458. * after sanitizing the values to make sure we only advertise
  459. * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
  460. * hasn't changed, and > 0 if it has changed.
  461. */
  462. static int genphy_config_advert(struct phy_device *phydev)
  463. {
  464. u32 advertise;
  465. int oldadv, adv;
  466. int err, changed = 0;
  467. /* Only allow advertising what
  468. * this PHY supports */
  469. phydev->advertising &= phydev->supported;
  470. advertise = phydev->advertising;
  471. /* Setup standard advertisement */
  472. oldadv = adv = phy_read(phydev, MII_ADVERTISE);
  473. if (adv < 0)
  474. return adv;
  475. adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
  476. ADVERTISE_PAUSE_ASYM);
  477. adv |= ethtool_adv_to_mii_adv_t(advertise);
  478. if (adv != oldadv) {
  479. err = phy_write(phydev, MII_ADVERTISE, adv);
  480. if (err < 0)
  481. return err;
  482. changed = 1;
  483. }
  484. /* Configure gigabit if it's supported */
  485. if (phydev->supported & (SUPPORTED_1000baseT_Half |
  486. SUPPORTED_1000baseT_Full)) {
  487. oldadv = adv = phy_read(phydev, MII_CTRL1000);
  488. if (adv < 0)
  489. return adv;
  490. adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
  491. adv |= ethtool_adv_to_mii_ctrl1000_t(advertise);
  492. if (adv != oldadv) {
  493. err = phy_write(phydev, MII_CTRL1000, adv);
  494. if (err < 0)
  495. return err;
  496. changed = 1;
  497. }
  498. }
  499. return changed;
  500. }
  501. /**
  502. * genphy_setup_forced - configures/forces speed/duplex from @phydev
  503. * @phydev: target phy_device struct
  504. *
  505. * Description: Configures MII_BMCR to force speed/duplex
  506. * to the values in phydev. Assumes that the values are valid.
  507. * Please see phy_sanitize_settings().
  508. */
  509. static int genphy_setup_forced(struct phy_device *phydev)
  510. {
  511. int err;
  512. int ctl = 0;
  513. phydev->pause = phydev->asym_pause = 0;
  514. if (SPEED_1000 == phydev->speed)
  515. ctl |= BMCR_SPEED1000;
  516. else if (SPEED_100 == phydev->speed)
  517. ctl |= BMCR_SPEED100;
  518. if (DUPLEX_FULL == phydev->duplex)
  519. ctl |= BMCR_FULLDPLX;
  520. err = phy_write(phydev, MII_BMCR, ctl);
  521. return err;
  522. }
  523. /**
  524. * genphy_restart_aneg - Enable and Restart Autonegotiation
  525. * @phydev: target phy_device struct
  526. */
  527. int genphy_restart_aneg(struct phy_device *phydev)
  528. {
  529. int ctl;
  530. ctl = phy_read(phydev, MII_BMCR);
  531. if (ctl < 0)
  532. return ctl;
  533. ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
  534. /* Don't isolate the PHY if we're negotiating */
  535. ctl &= ~(BMCR_ISOLATE);
  536. ctl = phy_write(phydev, MII_BMCR, ctl);
  537. return ctl;
  538. }
  539. EXPORT_SYMBOL(genphy_restart_aneg);
  540. /**
  541. * genphy_config_aneg - restart auto-negotiation or write BMCR
  542. * @phydev: target phy_device struct
  543. *
  544. * Description: If auto-negotiation is enabled, we configure the
  545. * advertising, and then restart auto-negotiation. If it is not
  546. * enabled, then we write the BMCR.
  547. */
  548. int genphy_config_aneg(struct phy_device *phydev)
  549. {
  550. int result;
  551. if (AUTONEG_ENABLE != phydev->autoneg)
  552. return genphy_setup_forced(phydev);
  553. result = genphy_config_advert(phydev);
  554. if (result < 0) /* error */
  555. return result;
  556. if (result == 0) {
  557. /* Advertisement hasn't changed, but maybe aneg was never on to
  558. * begin with? Or maybe phy was isolated? */
  559. int ctl = phy_read(phydev, MII_BMCR);
  560. if (ctl < 0)
  561. return ctl;
  562. if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
  563. result = 1; /* do restart aneg */
  564. }
  565. /* Only restart aneg if we are advertising something different
  566. * than we were before. */
  567. if (result > 0)
  568. result = genphy_restart_aneg(phydev);
  569. return result;
  570. }
  571. EXPORT_SYMBOL(genphy_config_aneg);
  572. /**
  573. * genphy_update_link - update link status in @phydev
  574. * @phydev: target phy_device struct
  575. *
  576. * Description: Update the value in phydev->link to reflect the
  577. * current link value. In order to do this, we need to read
  578. * the status register twice, keeping the second value.
  579. */
  580. int genphy_update_link(struct phy_device *phydev)
  581. {
  582. int status;
  583. /* Do a fake read */
  584. status = phy_read(phydev, MII_BMSR);
  585. if (status < 0)
  586. return status;
  587. /* Read link and autonegotiation status */
  588. status = phy_read(phydev, MII_BMSR);
  589. if (status < 0)
  590. return status;
  591. if ((status & BMSR_LSTATUS) == 0)
  592. phydev->link = 0;
  593. else
  594. phydev->link = 1;
  595. return 0;
  596. }
  597. EXPORT_SYMBOL(genphy_update_link);
  598. /**
  599. * genphy_read_status - check the link status and update current link state
  600. * @phydev: target phy_device struct
  601. *
  602. * Description: Check the link, then figure out the current state
  603. * by comparing what we advertise with what the link partner
  604. * advertises. Start by checking the gigabit possibilities,
  605. * then move on to 10/100.
  606. */
  607. int genphy_read_status(struct phy_device *phydev)
  608. {
  609. int adv;
  610. int err;
  611. int lpa;
  612. int lpagb = 0;
  613. /* Update the link, but return if there
  614. * was an error */
  615. err = genphy_update_link(phydev);
  616. if (err)
  617. return err;
  618. if (AUTONEG_ENABLE == phydev->autoneg) {
  619. if (phydev->supported & (SUPPORTED_1000baseT_Half
  620. | SUPPORTED_1000baseT_Full)) {
  621. lpagb = phy_read(phydev, MII_STAT1000);
  622. if (lpagb < 0)
  623. return lpagb;
  624. adv = phy_read(phydev, MII_CTRL1000);
  625. if (adv < 0)
  626. return adv;
  627. lpagb &= adv << 2;
  628. }
  629. lpa = phy_read(phydev, MII_LPA);
  630. if (lpa < 0)
  631. return lpa;
  632. adv = phy_read(phydev, MII_ADVERTISE);
  633. if (adv < 0)
  634. return adv;
  635. lpa &= adv;
  636. phydev->speed = SPEED_10;
  637. phydev->duplex = DUPLEX_HALF;
  638. phydev->pause = phydev->asym_pause = 0;
  639. if (lpagb & (LPA_1000FULL | LPA_1000HALF)) {
  640. phydev->speed = SPEED_1000;
  641. if (lpagb & LPA_1000FULL)
  642. phydev->duplex = DUPLEX_FULL;
  643. } else if (lpa & (LPA_100FULL | LPA_100HALF)) {
  644. phydev->speed = SPEED_100;
  645. if (lpa & LPA_100FULL)
  646. phydev->duplex = DUPLEX_FULL;
  647. } else
  648. if (lpa & LPA_10FULL)
  649. phydev->duplex = DUPLEX_FULL;
  650. if (phydev->duplex == DUPLEX_FULL){
  651. phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
  652. phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
  653. }
  654. } else {
  655. int bmcr = phy_read(phydev, MII_BMCR);
  656. if (bmcr < 0)
  657. return bmcr;
  658. if (bmcr & BMCR_FULLDPLX)
  659. phydev->duplex = DUPLEX_FULL;
  660. else
  661. phydev->duplex = DUPLEX_HALF;
  662. if (bmcr & BMCR_SPEED1000)
  663. phydev->speed = SPEED_1000;
  664. else if (bmcr & BMCR_SPEED100)
  665. phydev->speed = SPEED_100;
  666. else
  667. phydev->speed = SPEED_10;
  668. phydev->pause = phydev->asym_pause = 0;
  669. }
  670. return 0;
  671. }
  672. EXPORT_SYMBOL(genphy_read_status);
  673. static int genphy_config_init(struct phy_device *phydev)
  674. {
  675. int val;
  676. u32 features;
  677. /* For now, I'll claim that the generic driver supports
  678. * all possible port types */
  679. features = (SUPPORTED_TP | SUPPORTED_MII
  680. | SUPPORTED_AUI | SUPPORTED_FIBRE |
  681. SUPPORTED_BNC);
  682. /* Do we support autonegotiation? */
  683. val = phy_read(phydev, MII_BMSR);
  684. if (val < 0)
  685. return val;
  686. if (val & BMSR_ANEGCAPABLE)
  687. features |= SUPPORTED_Autoneg;
  688. if (val & BMSR_100FULL)
  689. features |= SUPPORTED_100baseT_Full;
  690. if (val & BMSR_100HALF)
  691. features |= SUPPORTED_100baseT_Half;
  692. if (val & BMSR_10FULL)
  693. features |= SUPPORTED_10baseT_Full;
  694. if (val & BMSR_10HALF)
  695. features |= SUPPORTED_10baseT_Half;
  696. if (val & BMSR_ESTATEN) {
  697. val = phy_read(phydev, MII_ESTATUS);
  698. if (val < 0)
  699. return val;
  700. if (val & ESTATUS_1000_TFULL)
  701. features |= SUPPORTED_1000baseT_Full;
  702. if (val & ESTATUS_1000_THALF)
  703. features |= SUPPORTED_1000baseT_Half;
  704. }
  705. phydev->supported = features;
  706. phydev->advertising = features;
  707. return 0;
  708. }
  709. int genphy_suspend(struct phy_device *phydev)
  710. {
  711. int value;
  712. mutex_lock(&phydev->lock);
  713. value = phy_read(phydev, MII_BMCR);
  714. phy_write(phydev, MII_BMCR, (value | BMCR_PDOWN));
  715. mutex_unlock(&phydev->lock);
  716. return 0;
  717. }
  718. EXPORT_SYMBOL(genphy_suspend);
  719. int genphy_resume(struct phy_device *phydev)
  720. {
  721. int value;
  722. mutex_lock(&phydev->lock);
  723. value = phy_read(phydev, MII_BMCR);
  724. phy_write(phydev, MII_BMCR, (value & ~BMCR_PDOWN));
  725. mutex_unlock(&phydev->lock);
  726. return 0;
  727. }
  728. EXPORT_SYMBOL(genphy_resume);
  729. /**
  730. * phy_probe - probe and init a PHY device
  731. * @dev: device to probe and init
  732. *
  733. * Description: Take care of setting up the phy_device structure,
  734. * set the state to READY (the driver's init function should
  735. * set it to STARTING if needed).
  736. */
  737. static int phy_probe(struct device *dev)
  738. {
  739. struct phy_device *phydev;
  740. struct phy_driver *phydrv;
  741. struct device_driver *drv;
  742. int err = 0;
  743. phydev = to_phy_device(dev);
  744. drv = phydev->dev.driver;
  745. phydrv = to_phy_driver(drv);
  746. phydev->drv = phydrv;
  747. /* Disable the interrupt if the PHY doesn't support it */
  748. if (!(phydrv->flags & PHY_HAS_INTERRUPT))
  749. phydev->irq = PHY_POLL;
  750. mutex_lock(&phydev->lock);
  751. /* Start out supporting everything. Eventually,
  752. * a controller will attach, and may modify one
  753. * or both of these values */
  754. phydev->supported = phydrv->features;
  755. phydev->advertising = phydrv->features;
  756. /* Set the state to READY by default */
  757. phydev->state = PHY_READY;
  758. if (phydev->drv->probe)
  759. err = phydev->drv->probe(phydev);
  760. mutex_unlock(&phydev->lock);
  761. return err;
  762. }
  763. static int phy_remove(struct device *dev)
  764. {
  765. struct phy_device *phydev;
  766. phydev = to_phy_device(dev);
  767. mutex_lock(&phydev->lock);
  768. phydev->state = PHY_DOWN;
  769. mutex_unlock(&phydev->lock);
  770. if (phydev->drv->remove)
  771. phydev->drv->remove(phydev);
  772. phydev->drv = NULL;
  773. return 0;
  774. }
  775. /**
  776. * phy_driver_register - register a phy_driver with the PHY layer
  777. * @new_driver: new phy_driver to register
  778. */
  779. int phy_driver_register(struct phy_driver *new_driver)
  780. {
  781. int retval;
  782. new_driver->driver.name = new_driver->name;
  783. new_driver->driver.bus = &mdio_bus_type;
  784. new_driver->driver.probe = phy_probe;
  785. new_driver->driver.remove = phy_remove;
  786. retval = driver_register(&new_driver->driver);
  787. if (retval) {
  788. printk(KERN_ERR "%s: Error %d in registering driver\n",
  789. new_driver->name, retval);
  790. return retval;
  791. }
  792. pr_debug("%s: Registered new driver\n", new_driver->name);
  793. return 0;
  794. }
  795. EXPORT_SYMBOL(phy_driver_register);
  796. void phy_driver_unregister(struct phy_driver *drv)
  797. {
  798. driver_unregister(&drv->driver);
  799. }
  800. EXPORT_SYMBOL(phy_driver_unregister);
  801. static struct phy_driver genphy_driver = {
  802. .phy_id = 0xffffffff,
  803. .phy_id_mask = 0xffffffff,
  804. .name = "Generic PHY",
  805. .config_init = genphy_config_init,
  806. .features = 0,
  807. .config_aneg = genphy_config_aneg,
  808. .read_status = genphy_read_status,
  809. .suspend = genphy_suspend,
  810. .resume = genphy_resume,
  811. .driver = {.owner= THIS_MODULE, },
  812. };
  813. static int __init phy_init(void)
  814. {
  815. int rc;
  816. rc = mdio_bus_init();
  817. if (rc)
  818. return rc;
  819. rc = phy_driver_register(&genphy_driver);
  820. if (rc)
  821. mdio_bus_exit();
  822. return rc;
  823. }
  824. static void __exit phy_exit(void)
  825. {
  826. phy_driver_unregister(&genphy_driver);
  827. mdio_bus_exit();
  828. }
  829. subsys_initcall(phy_init);
  830. module_exit(phy_exit);