phy_device.c 24 KB

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