phy_device.c 21 KB

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