phy_device.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  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 = bus->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 = bus->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. rc = phy_attach_direct(dev, phydev, flags, interface);
  286. if (rc)
  287. return rc;
  288. phy_prepare_link(phydev, handler);
  289. phy_start_machine(phydev, NULL);
  290. if (phydev->irq > 0)
  291. phy_start_interrupts(phydev);
  292. return 0;
  293. }
  294. EXPORT_SYMBOL(phy_connect_direct);
  295. /**
  296. * phy_connect - connect an ethernet device to a PHY device
  297. * @dev: the network device to connect
  298. * @bus_id: the id string of the PHY device to connect
  299. * @handler: callback function for state change notifications
  300. * @flags: PHY device's dev_flags
  301. * @interface: PHY device's interface
  302. *
  303. * Description: Convenience function for connecting ethernet
  304. * devices to PHY devices. The default behavior is for
  305. * the PHY infrastructure to handle everything, and only notify
  306. * the connected driver when the link status changes. If you
  307. * don't want, or can't use the provided functionality, you may
  308. * choose to call only the subset of functions which provide
  309. * the desired functionality.
  310. */
  311. struct phy_device * phy_connect(struct net_device *dev, const char *bus_id,
  312. void (*handler)(struct net_device *), u32 flags,
  313. phy_interface_t interface)
  314. {
  315. struct phy_device *phydev;
  316. struct device *d;
  317. int rc;
  318. /* Search the list of PHY devices on the mdio bus for the
  319. * PHY with the requested name */
  320. d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id);
  321. if (!d) {
  322. pr_err("PHY %s not found\n", bus_id);
  323. return ERR_PTR(-ENODEV);
  324. }
  325. phydev = to_phy_device(d);
  326. rc = phy_connect_direct(dev, phydev, handler, flags, interface);
  327. if (rc)
  328. return ERR_PTR(rc);
  329. return phydev;
  330. }
  331. EXPORT_SYMBOL(phy_connect);
  332. /**
  333. * phy_disconnect - disable interrupts, stop state machine, and detach a PHY device
  334. * @phydev: target phy_device struct
  335. */
  336. void phy_disconnect(struct phy_device *phydev)
  337. {
  338. if (phydev->irq > 0)
  339. phy_stop_interrupts(phydev);
  340. phy_stop_machine(phydev);
  341. phydev->adjust_link = NULL;
  342. phy_detach(phydev);
  343. }
  344. EXPORT_SYMBOL(phy_disconnect);
  345. int phy_init_hw(struct phy_device *phydev)
  346. {
  347. int ret;
  348. if (!phydev->drv || !phydev->drv->config_init)
  349. return 0;
  350. ret = phy_scan_fixups(phydev);
  351. if (ret < 0)
  352. return ret;
  353. return phydev->drv->config_init(phydev);
  354. }
  355. /**
  356. * phy_attach_direct - attach a network device to a given PHY device pointer
  357. * @dev: network device to attach
  358. * @phydev: Pointer to phy_device to attach
  359. * @flags: PHY device's dev_flags
  360. * @interface: PHY device's interface
  361. *
  362. * Description: Called by drivers to attach to a particular PHY
  363. * device. The phy_device is found, and properly hooked up
  364. * to the phy_driver. If no driver is attached, then the
  365. * genphy_driver is used. The phy_device is given a ptr to
  366. * the attaching device, and given a callback for link status
  367. * change. The phy_device is returned to the attaching driver.
  368. */
  369. static int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
  370. u32 flags, phy_interface_t interface)
  371. {
  372. struct device *d = &phydev->dev;
  373. int err;
  374. /* Assume that if there is no driver, that it doesn't
  375. * exist, and we should use the genphy driver. */
  376. if (NULL == d->driver) {
  377. d->driver = &genphy_driver.driver;
  378. err = d->driver->probe(d);
  379. if (err >= 0)
  380. err = device_bind_driver(d);
  381. if (err)
  382. return err;
  383. }
  384. if (phydev->attached_dev) {
  385. dev_err(&dev->dev, "PHY already attached\n");
  386. return -EBUSY;
  387. }
  388. phydev->attached_dev = dev;
  389. dev->phydev = phydev;
  390. phydev->dev_flags = flags;
  391. phydev->interface = interface;
  392. phydev->state = PHY_READY;
  393. /* Do initial configuration here, now that
  394. * we have certain key parameters
  395. * (dev_flags and interface) */
  396. err = phy_init_hw(phydev);
  397. if (err)
  398. phy_detach(phydev);
  399. return err;
  400. }
  401. /**
  402. * phy_attach - attach a network device to a particular PHY device
  403. * @dev: network device to attach
  404. * @bus_id: Bus ID of PHY device to attach
  405. * @flags: PHY device's dev_flags
  406. * @interface: PHY device's interface
  407. *
  408. * Description: Same as phy_attach_direct() except that a PHY bus_id
  409. * string is passed instead of a pointer to a struct phy_device.
  410. */
  411. struct phy_device *phy_attach(struct net_device *dev,
  412. const char *bus_id, u32 flags, phy_interface_t interface)
  413. {
  414. struct bus_type *bus = &mdio_bus_type;
  415. struct phy_device *phydev;
  416. struct device *d;
  417. int rc;
  418. /* Search the list of PHY devices on the mdio bus for the
  419. * PHY with the requested name */
  420. d = bus_find_device_by_name(bus, NULL, bus_id);
  421. if (!d) {
  422. pr_err("PHY %s not found\n", bus_id);
  423. return ERR_PTR(-ENODEV);
  424. }
  425. phydev = to_phy_device(d);
  426. rc = phy_attach_direct(dev, phydev, flags, interface);
  427. if (rc)
  428. return ERR_PTR(rc);
  429. return phydev;
  430. }
  431. EXPORT_SYMBOL(phy_attach);
  432. /**
  433. * phy_detach - detach a PHY device from its network device
  434. * @phydev: target phy_device struct
  435. */
  436. void phy_detach(struct phy_device *phydev)
  437. {
  438. phydev->attached_dev->phydev = NULL;
  439. phydev->attached_dev = NULL;
  440. /* If the device had no specific driver before (i.e. - it
  441. * was using the generic driver), we unbind the device
  442. * from the generic driver so that there's a chance a
  443. * real driver could be loaded */
  444. if (phydev->dev.driver == &genphy_driver.driver)
  445. device_release_driver(&phydev->dev);
  446. }
  447. EXPORT_SYMBOL(phy_detach);
  448. /* Generic PHY support and helper functions */
  449. /**
  450. * genphy_config_advert - sanitize and advertise auto-negotiation parameters
  451. * @phydev: target phy_device struct
  452. *
  453. * Description: Writes MII_ADVERTISE with the appropriate values,
  454. * after sanitizing the values to make sure we only advertise
  455. * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
  456. * hasn't changed, and > 0 if it has changed.
  457. */
  458. static int genphy_config_advert(struct phy_device *phydev)
  459. {
  460. u32 advertise;
  461. int oldadv, adv;
  462. int err, changed = 0;
  463. /* Only allow advertising what
  464. * this PHY supports */
  465. phydev->advertising &= phydev->supported;
  466. advertise = phydev->advertising;
  467. /* Setup standard advertisement */
  468. oldadv = adv = phy_read(phydev, MII_ADVERTISE);
  469. if (adv < 0)
  470. return adv;
  471. adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
  472. ADVERTISE_PAUSE_ASYM);
  473. if (advertise & ADVERTISED_10baseT_Half)
  474. adv |= ADVERTISE_10HALF;
  475. if (advertise & ADVERTISED_10baseT_Full)
  476. adv |= ADVERTISE_10FULL;
  477. if (advertise & ADVERTISED_100baseT_Half)
  478. adv |= ADVERTISE_100HALF;
  479. if (advertise & ADVERTISED_100baseT_Full)
  480. adv |= ADVERTISE_100FULL;
  481. if (advertise & ADVERTISED_Pause)
  482. adv |= ADVERTISE_PAUSE_CAP;
  483. if (advertise & ADVERTISED_Asym_Pause)
  484. adv |= ADVERTISE_PAUSE_ASYM;
  485. if (adv != oldadv) {
  486. err = phy_write(phydev, MII_ADVERTISE, adv);
  487. if (err < 0)
  488. return err;
  489. changed = 1;
  490. }
  491. /* Configure gigabit if it's supported */
  492. if (phydev->supported & (SUPPORTED_1000baseT_Half |
  493. SUPPORTED_1000baseT_Full)) {
  494. oldadv = adv = phy_read(phydev, MII_CTRL1000);
  495. if (adv < 0)
  496. return adv;
  497. adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
  498. if (advertise & SUPPORTED_1000baseT_Half)
  499. adv |= ADVERTISE_1000HALF;
  500. if (advertise & SUPPORTED_1000baseT_Full)
  501. adv |= ADVERTISE_1000FULL;
  502. if (adv != oldadv) {
  503. err = phy_write(phydev, MII_CTRL1000, adv);
  504. if (err < 0)
  505. return err;
  506. changed = 1;
  507. }
  508. }
  509. return changed;
  510. }
  511. /**
  512. * genphy_setup_forced - configures/forces speed/duplex from @phydev
  513. * @phydev: target phy_device struct
  514. *
  515. * Description: Configures MII_BMCR to force speed/duplex
  516. * to the values in phydev. Assumes that the values are valid.
  517. * Please see phy_sanitize_settings().
  518. */
  519. static int genphy_setup_forced(struct phy_device *phydev)
  520. {
  521. int err;
  522. int ctl = 0;
  523. phydev->pause = phydev->asym_pause = 0;
  524. if (SPEED_1000 == phydev->speed)
  525. ctl |= BMCR_SPEED1000;
  526. else if (SPEED_100 == phydev->speed)
  527. ctl |= BMCR_SPEED100;
  528. if (DUPLEX_FULL == phydev->duplex)
  529. ctl |= BMCR_FULLDPLX;
  530. err = phy_write(phydev, MII_BMCR, ctl);
  531. return err;
  532. }
  533. /**
  534. * genphy_restart_aneg - Enable and Restart Autonegotiation
  535. * @phydev: target phy_device struct
  536. */
  537. int genphy_restart_aneg(struct phy_device *phydev)
  538. {
  539. int ctl;
  540. ctl = phy_read(phydev, MII_BMCR);
  541. if (ctl < 0)
  542. return ctl;
  543. ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
  544. /* Don't isolate the PHY if we're negotiating */
  545. ctl &= ~(BMCR_ISOLATE);
  546. ctl = phy_write(phydev, MII_BMCR, ctl);
  547. return ctl;
  548. }
  549. EXPORT_SYMBOL(genphy_restart_aneg);
  550. /**
  551. * genphy_config_aneg - restart auto-negotiation or write BMCR
  552. * @phydev: target phy_device struct
  553. *
  554. * Description: If auto-negotiation is enabled, we configure the
  555. * advertising, and then restart auto-negotiation. If it is not
  556. * enabled, then we write the BMCR.
  557. */
  558. int genphy_config_aneg(struct phy_device *phydev)
  559. {
  560. int result;
  561. if (AUTONEG_ENABLE != phydev->autoneg)
  562. return genphy_setup_forced(phydev);
  563. result = genphy_config_advert(phydev);
  564. if (result < 0) /* error */
  565. return result;
  566. if (result == 0) {
  567. /* Advertisement hasn't changed, but maybe aneg was never on to
  568. * begin with? Or maybe phy was isolated? */
  569. int ctl = phy_read(phydev, MII_BMCR);
  570. if (ctl < 0)
  571. return ctl;
  572. if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
  573. result = 1; /* do restart aneg */
  574. }
  575. /* Only restart aneg if we are advertising something different
  576. * than we were before. */
  577. if (result > 0)
  578. result = genphy_restart_aneg(phydev);
  579. return result;
  580. }
  581. EXPORT_SYMBOL(genphy_config_aneg);
  582. /**
  583. * genphy_update_link - update link status in @phydev
  584. * @phydev: target phy_device struct
  585. *
  586. * Description: Update the value in phydev->link to reflect the
  587. * current link value. In order to do this, we need to read
  588. * the status register twice, keeping the second value.
  589. */
  590. int genphy_update_link(struct phy_device *phydev)
  591. {
  592. int status;
  593. /* Do a fake read */
  594. status = phy_read(phydev, MII_BMSR);
  595. if (status < 0)
  596. return status;
  597. /* Read link and autonegotiation status */
  598. status = phy_read(phydev, MII_BMSR);
  599. if (status < 0)
  600. return status;
  601. if ((status & BMSR_LSTATUS) == 0)
  602. phydev->link = 0;
  603. else
  604. phydev->link = 1;
  605. return 0;
  606. }
  607. EXPORT_SYMBOL(genphy_update_link);
  608. /**
  609. * genphy_read_status - check the link status and update current link state
  610. * @phydev: target phy_device struct
  611. *
  612. * Description: Check the link, then figure out the current state
  613. * by comparing what we advertise with what the link partner
  614. * advertises. Start by checking the gigabit possibilities,
  615. * then move on to 10/100.
  616. */
  617. int genphy_read_status(struct phy_device *phydev)
  618. {
  619. int adv;
  620. int err;
  621. int lpa;
  622. int lpagb = 0;
  623. /* Update the link, but return if there
  624. * was an error */
  625. err = genphy_update_link(phydev);
  626. if (err)
  627. return err;
  628. if (AUTONEG_ENABLE == phydev->autoneg) {
  629. if (phydev->supported & (SUPPORTED_1000baseT_Half
  630. | SUPPORTED_1000baseT_Full)) {
  631. lpagb = phy_read(phydev, MII_STAT1000);
  632. if (lpagb < 0)
  633. return lpagb;
  634. adv = phy_read(phydev, MII_CTRL1000);
  635. if (adv < 0)
  636. return adv;
  637. lpagb &= adv << 2;
  638. }
  639. lpa = phy_read(phydev, MII_LPA);
  640. if (lpa < 0)
  641. return lpa;
  642. adv = phy_read(phydev, MII_ADVERTISE);
  643. if (adv < 0)
  644. return adv;
  645. lpa &= adv;
  646. phydev->speed = SPEED_10;
  647. phydev->duplex = DUPLEX_HALF;
  648. phydev->pause = phydev->asym_pause = 0;
  649. if (lpagb & (LPA_1000FULL | LPA_1000HALF)) {
  650. phydev->speed = SPEED_1000;
  651. if (lpagb & LPA_1000FULL)
  652. phydev->duplex = DUPLEX_FULL;
  653. } else if (lpa & (LPA_100FULL | LPA_100HALF)) {
  654. phydev->speed = SPEED_100;
  655. if (lpa & LPA_100FULL)
  656. phydev->duplex = DUPLEX_FULL;
  657. } else
  658. if (lpa & LPA_10FULL)
  659. phydev->duplex = DUPLEX_FULL;
  660. if (phydev->duplex == DUPLEX_FULL){
  661. phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
  662. phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
  663. }
  664. } else {
  665. int bmcr = phy_read(phydev, MII_BMCR);
  666. if (bmcr < 0)
  667. return bmcr;
  668. if (bmcr & BMCR_FULLDPLX)
  669. phydev->duplex = DUPLEX_FULL;
  670. else
  671. phydev->duplex = DUPLEX_HALF;
  672. if (bmcr & BMCR_SPEED1000)
  673. phydev->speed = SPEED_1000;
  674. else if (bmcr & BMCR_SPEED100)
  675. phydev->speed = SPEED_100;
  676. else
  677. phydev->speed = SPEED_10;
  678. phydev->pause = phydev->asym_pause = 0;
  679. }
  680. return 0;
  681. }
  682. EXPORT_SYMBOL(genphy_read_status);
  683. static int genphy_config_init(struct phy_device *phydev)
  684. {
  685. int val;
  686. u32 features;
  687. /* For now, I'll claim that the generic driver supports
  688. * all possible port types */
  689. features = (SUPPORTED_TP | SUPPORTED_MII
  690. | SUPPORTED_AUI | SUPPORTED_FIBRE |
  691. SUPPORTED_BNC);
  692. /* Do we support autonegotiation? */
  693. val = phy_read(phydev, MII_BMSR);
  694. if (val < 0)
  695. return val;
  696. if (val & BMSR_ANEGCAPABLE)
  697. features |= SUPPORTED_Autoneg;
  698. if (val & BMSR_100FULL)
  699. features |= SUPPORTED_100baseT_Full;
  700. if (val & BMSR_100HALF)
  701. features |= SUPPORTED_100baseT_Half;
  702. if (val & BMSR_10FULL)
  703. features |= SUPPORTED_10baseT_Full;
  704. if (val & BMSR_10HALF)
  705. features |= SUPPORTED_10baseT_Half;
  706. if (val & BMSR_ESTATEN) {
  707. val = phy_read(phydev, MII_ESTATUS);
  708. if (val < 0)
  709. return val;
  710. if (val & ESTATUS_1000_TFULL)
  711. features |= SUPPORTED_1000baseT_Full;
  712. if (val & ESTATUS_1000_THALF)
  713. features |= SUPPORTED_1000baseT_Half;
  714. }
  715. phydev->supported = features;
  716. phydev->advertising = features;
  717. return 0;
  718. }
  719. int genphy_suspend(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_suspend);
  729. int genphy_resume(struct phy_device *phydev)
  730. {
  731. int value;
  732. mutex_lock(&phydev->lock);
  733. value = phy_read(phydev, MII_BMCR);
  734. phy_write(phydev, MII_BMCR, (value & ~BMCR_PDOWN));
  735. mutex_unlock(&phydev->lock);
  736. return 0;
  737. }
  738. EXPORT_SYMBOL(genphy_resume);
  739. /**
  740. * phy_probe - probe and init a PHY device
  741. * @dev: device to probe and init
  742. *
  743. * Description: Take care of setting up the phy_device structure,
  744. * set the state to READY (the driver's init function should
  745. * set it to STARTING if needed).
  746. */
  747. static int phy_probe(struct device *dev)
  748. {
  749. struct phy_device *phydev;
  750. struct phy_driver *phydrv;
  751. struct device_driver *drv;
  752. int err = 0;
  753. phydev = to_phy_device(dev);
  754. /* Make sure the driver is held.
  755. * XXX -- Is this correct? */
  756. drv = get_driver(phydev->dev.driver);
  757. phydrv = to_phy_driver(drv);
  758. phydev->drv = phydrv;
  759. /* Disable the interrupt if the PHY doesn't support it */
  760. if (!(phydrv->flags & PHY_HAS_INTERRUPT))
  761. phydev->irq = PHY_POLL;
  762. mutex_lock(&phydev->lock);
  763. /* Start out supporting everything. Eventually,
  764. * a controller will attach, and may modify one
  765. * or both of these values */
  766. phydev->supported = phydrv->features;
  767. phydev->advertising = phydrv->features;
  768. /* Set the state to READY by default */
  769. phydev->state = PHY_READY;
  770. if (phydev->drv->probe)
  771. err = phydev->drv->probe(phydev);
  772. mutex_unlock(&phydev->lock);
  773. return err;
  774. }
  775. static int phy_remove(struct device *dev)
  776. {
  777. struct phy_device *phydev;
  778. phydev = to_phy_device(dev);
  779. mutex_lock(&phydev->lock);
  780. phydev->state = PHY_DOWN;
  781. mutex_unlock(&phydev->lock);
  782. if (phydev->drv->remove)
  783. phydev->drv->remove(phydev);
  784. put_driver(dev->driver);
  785. phydev->drv = NULL;
  786. return 0;
  787. }
  788. /**
  789. * phy_driver_register - register a phy_driver with the PHY layer
  790. * @new_driver: new phy_driver to register
  791. */
  792. int phy_driver_register(struct phy_driver *new_driver)
  793. {
  794. int retval;
  795. new_driver->driver.name = new_driver->name;
  796. new_driver->driver.bus = &mdio_bus_type;
  797. new_driver->driver.probe = phy_probe;
  798. new_driver->driver.remove = phy_remove;
  799. retval = driver_register(&new_driver->driver);
  800. if (retval) {
  801. printk(KERN_ERR "%s: Error %d in registering driver\n",
  802. new_driver->name, retval);
  803. return retval;
  804. }
  805. pr_debug("%s: Registered new driver\n", new_driver->name);
  806. return 0;
  807. }
  808. EXPORT_SYMBOL(phy_driver_register);
  809. void phy_driver_unregister(struct phy_driver *drv)
  810. {
  811. driver_unregister(&drv->driver);
  812. }
  813. EXPORT_SYMBOL(phy_driver_unregister);
  814. static struct phy_driver genphy_driver = {
  815. .phy_id = 0xffffffff,
  816. .phy_id_mask = 0xffffffff,
  817. .name = "Generic PHY",
  818. .config_init = genphy_config_init,
  819. .features = 0,
  820. .config_aneg = genphy_config_aneg,
  821. .read_status = genphy_read_status,
  822. .suspend = genphy_suspend,
  823. .resume = genphy_resume,
  824. .driver = {.owner= THIS_MODULE, },
  825. };
  826. static int __init phy_init(void)
  827. {
  828. int rc;
  829. rc = mdio_bus_init();
  830. if (rc)
  831. return rc;
  832. rc = phy_driver_register(&genphy_driver);
  833. if (rc)
  834. mdio_bus_exit();
  835. return rc;
  836. }
  837. static void __exit phy_exit(void)
  838. {
  839. phy_driver_unregister(&genphy_driver);
  840. mdio_bus_exit();
  841. }
  842. subsys_initcall(phy_init);
  843. module_exit(phy_exit);