phy_device.c 24 KB

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