phy_device.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  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. strncpy(fixup->bus_id, bus_id, BUS_ID_SIZE);
  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, phydev->dev.bus_id) != 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. /*
  193. * Broken hardware is sometimes missing the pull down resistor on the
  194. * MDIO line, which results in reads to non-existent devices returning
  195. * 0 rather than 0xffff. Catch this here and treat 0 as a non-existent
  196. * device as well.
  197. */
  198. if (phy_id == 0)
  199. return NULL;
  200. dev = phy_device_create(bus, addr, phy_id);
  201. return dev;
  202. }
  203. /**
  204. * phy_prepare_link - prepares the PHY layer to monitor link status
  205. * @phydev: target phy_device struct
  206. * @handler: callback function for link status change notifications
  207. *
  208. * Description: Tells the PHY infrastructure to handle the
  209. * gory details on monitoring link status (whether through
  210. * polling or an interrupt), and to call back to the
  211. * connected device driver when the link status changes.
  212. * If you want to monitor your own link state, don't call
  213. * this function.
  214. */
  215. void phy_prepare_link(struct phy_device *phydev,
  216. void (*handler)(struct net_device *))
  217. {
  218. phydev->adjust_link = handler;
  219. }
  220. /**
  221. * phy_connect - connect an ethernet device to a PHY device
  222. * @dev: the network device to connect
  223. * @bus_id: the id string of the PHY device to connect
  224. * @handler: callback function for state change notifications
  225. * @flags: PHY device's dev_flags
  226. * @interface: PHY device's interface
  227. *
  228. * Description: Convenience function for connecting ethernet
  229. * devices to PHY devices. The default behavior is for
  230. * the PHY infrastructure to handle everything, and only notify
  231. * the connected driver when the link status changes. If you
  232. * don't want, or can't use the provided functionality, you may
  233. * choose to call only the subset of functions which provide
  234. * the desired functionality.
  235. */
  236. struct phy_device * phy_connect(struct net_device *dev, const char *bus_id,
  237. void (*handler)(struct net_device *), u32 flags,
  238. phy_interface_t interface)
  239. {
  240. struct phy_device *phydev;
  241. phydev = phy_attach(dev, bus_id, flags, interface);
  242. if (IS_ERR(phydev))
  243. return phydev;
  244. phy_prepare_link(phydev, handler);
  245. phy_start_machine(phydev, NULL);
  246. if (phydev->irq > 0)
  247. phy_start_interrupts(phydev);
  248. return phydev;
  249. }
  250. EXPORT_SYMBOL(phy_connect);
  251. /**
  252. * phy_disconnect - disable interrupts, stop state machine, and detach a PHY device
  253. * @phydev: target phy_device struct
  254. */
  255. void phy_disconnect(struct phy_device *phydev)
  256. {
  257. if (phydev->irq > 0)
  258. phy_stop_interrupts(phydev);
  259. phy_stop_machine(phydev);
  260. phydev->adjust_link = NULL;
  261. phy_detach(phydev);
  262. }
  263. EXPORT_SYMBOL(phy_disconnect);
  264. /**
  265. * phy_attach - attach a network device to a particular PHY device
  266. * @dev: network device to attach
  267. * @bus_id: PHY device to attach
  268. * @flags: PHY device's dev_flags
  269. * @interface: PHY device's interface
  270. *
  271. * Description: Called by drivers to attach to a particular PHY
  272. * device. The phy_device is found, and properly hooked up
  273. * to the phy_driver. If no driver is attached, then the
  274. * genphy_driver is used. The phy_device is given a ptr to
  275. * the attaching device, and given a callback for link status
  276. * change. The phy_device is returned to the attaching driver.
  277. */
  278. struct phy_device *phy_attach(struct net_device *dev,
  279. const char *bus_id, u32 flags, phy_interface_t interface)
  280. {
  281. struct bus_type *bus = &mdio_bus_type;
  282. struct phy_device *phydev;
  283. struct device *d;
  284. /* Search the list of PHY devices on the mdio bus for the
  285. * PHY with the requested name */
  286. d = bus_find_device_by_name(bus, NULL, bus_id);
  287. if (d) {
  288. phydev = to_phy_device(d);
  289. } else {
  290. printk(KERN_ERR "%s not found\n", bus_id);
  291. return ERR_PTR(-ENODEV);
  292. }
  293. /* Assume that if there is no driver, that it doesn't
  294. * exist, and we should use the genphy driver. */
  295. if (NULL == d->driver) {
  296. int err;
  297. d->driver = &genphy_driver.driver;
  298. err = d->driver->probe(d);
  299. if (err >= 0)
  300. err = device_bind_driver(d);
  301. if (err)
  302. return ERR_PTR(err);
  303. }
  304. if (phydev->attached_dev) {
  305. printk(KERN_ERR "%s: %s already attached\n",
  306. dev->name, bus_id);
  307. return ERR_PTR(-EBUSY);
  308. }
  309. phydev->attached_dev = dev;
  310. phydev->dev_flags = flags;
  311. phydev->interface = interface;
  312. /* Do initial configuration here, now that
  313. * we have certain key parameters
  314. * (dev_flags and interface) */
  315. if (phydev->drv->config_init) {
  316. int err;
  317. err = phy_scan_fixups(phydev);
  318. if (err < 0)
  319. return ERR_PTR(err);
  320. err = phydev->drv->config_init(phydev);
  321. if (err < 0)
  322. return ERR_PTR(err);
  323. }
  324. return phydev;
  325. }
  326. EXPORT_SYMBOL(phy_attach);
  327. /**
  328. * phy_detach - detach a PHY device from its network device
  329. * @phydev: target phy_device struct
  330. */
  331. void phy_detach(struct phy_device *phydev)
  332. {
  333. phydev->attached_dev = NULL;
  334. /* If the device had no specific driver before (i.e. - it
  335. * was using the generic driver), we unbind the device
  336. * from the generic driver so that there's a chance a
  337. * real driver could be loaded */
  338. if (phydev->dev.driver == &genphy_driver.driver)
  339. device_release_driver(&phydev->dev);
  340. }
  341. EXPORT_SYMBOL(phy_detach);
  342. /* Generic PHY support and helper functions */
  343. /**
  344. * genphy_config_advert - sanitize and advertise auto-negotation parameters
  345. * @phydev: target phy_device struct
  346. *
  347. * Description: Writes MII_ADVERTISE with the appropriate values,
  348. * after sanitizing the values to make sure we only advertise
  349. * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
  350. * hasn't changed, and > 0 if it has changed.
  351. */
  352. int genphy_config_advert(struct phy_device *phydev)
  353. {
  354. u32 advertise;
  355. int oldadv, adv;
  356. int err, changed = 0;
  357. /* Only allow advertising what
  358. * this PHY supports */
  359. phydev->advertising &= phydev->supported;
  360. advertise = phydev->advertising;
  361. /* Setup standard advertisement */
  362. oldadv = adv = phy_read(phydev, MII_ADVERTISE);
  363. if (adv < 0)
  364. return adv;
  365. adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
  366. ADVERTISE_PAUSE_ASYM);
  367. if (advertise & ADVERTISED_10baseT_Half)
  368. adv |= ADVERTISE_10HALF;
  369. if (advertise & ADVERTISED_10baseT_Full)
  370. adv |= ADVERTISE_10FULL;
  371. if (advertise & ADVERTISED_100baseT_Half)
  372. adv |= ADVERTISE_100HALF;
  373. if (advertise & ADVERTISED_100baseT_Full)
  374. adv |= ADVERTISE_100FULL;
  375. if (advertise & ADVERTISED_Pause)
  376. adv |= ADVERTISE_PAUSE_CAP;
  377. if (advertise & ADVERTISED_Asym_Pause)
  378. adv |= ADVERTISE_PAUSE_ASYM;
  379. if (adv != oldadv) {
  380. err = phy_write(phydev, MII_ADVERTISE, adv);
  381. if (err < 0)
  382. return err;
  383. changed = 1;
  384. }
  385. /* Configure gigabit if it's supported */
  386. if (phydev->supported & (SUPPORTED_1000baseT_Half |
  387. SUPPORTED_1000baseT_Full)) {
  388. oldadv = adv = phy_read(phydev, MII_CTRL1000);
  389. if (adv < 0)
  390. return adv;
  391. adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
  392. if (advertise & SUPPORTED_1000baseT_Half)
  393. adv |= ADVERTISE_1000HALF;
  394. if (advertise & SUPPORTED_1000baseT_Full)
  395. adv |= ADVERTISE_1000FULL;
  396. if (adv != oldadv) {
  397. err = phy_write(phydev, MII_CTRL1000, adv);
  398. if (err < 0)
  399. return err;
  400. changed = 1;
  401. }
  402. }
  403. return changed;
  404. }
  405. EXPORT_SYMBOL(genphy_config_advert);
  406. /**
  407. * genphy_setup_forced - configures/forces speed/duplex from @phydev
  408. * @phydev: target phy_device struct
  409. *
  410. * Description: Configures MII_BMCR to force speed/duplex
  411. * to the values in phydev. Assumes that the values are valid.
  412. * Please see phy_sanitize_settings().
  413. */
  414. int genphy_setup_forced(struct phy_device *phydev)
  415. {
  416. int err;
  417. int ctl = 0;
  418. phydev->pause = phydev->asym_pause = 0;
  419. if (SPEED_1000 == phydev->speed)
  420. ctl |= BMCR_SPEED1000;
  421. else if (SPEED_100 == phydev->speed)
  422. ctl |= BMCR_SPEED100;
  423. if (DUPLEX_FULL == phydev->duplex)
  424. ctl |= BMCR_FULLDPLX;
  425. err = phy_write(phydev, MII_BMCR, ctl);
  426. if (err < 0)
  427. return err;
  428. /*
  429. * Run the fixups on this PHY, just in case the
  430. * board code needs to change something after a reset
  431. */
  432. err = phy_scan_fixups(phydev);
  433. if (err < 0)
  434. return err;
  435. /* We just reset the device, so we'd better configure any
  436. * settings the PHY requires to operate */
  437. if (phydev->drv->config_init)
  438. err = phydev->drv->config_init(phydev);
  439. return err;
  440. }
  441. /**
  442. * genphy_restart_aneg - Enable and Restart Autonegotiation
  443. * @phydev: target phy_device struct
  444. */
  445. int genphy_restart_aneg(struct phy_device *phydev)
  446. {
  447. int ctl;
  448. ctl = phy_read(phydev, MII_BMCR);
  449. if (ctl < 0)
  450. return ctl;
  451. ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
  452. /* Don't isolate the PHY if we're negotiating */
  453. ctl &= ~(BMCR_ISOLATE);
  454. ctl = phy_write(phydev, MII_BMCR, ctl);
  455. return ctl;
  456. }
  457. EXPORT_SYMBOL(genphy_restart_aneg);
  458. /**
  459. * genphy_config_aneg - restart auto-negotiation or write BMCR
  460. * @phydev: target phy_device struct
  461. *
  462. * Description: If auto-negotiation is enabled, we configure the
  463. * advertising, and then restart auto-negotiation. If it is not
  464. * enabled, then we write the BMCR.
  465. */
  466. int genphy_config_aneg(struct phy_device *phydev)
  467. {
  468. int result;
  469. if (AUTONEG_ENABLE != phydev->autoneg)
  470. return genphy_setup_forced(phydev);
  471. result = genphy_config_advert(phydev);
  472. if (result < 0) /* error */
  473. return result;
  474. if (result == 0) {
  475. /* Advertisment hasn't changed, but maybe aneg was never on to
  476. * begin with? Or maybe phy was isolated? */
  477. int ctl = phy_read(phydev, MII_BMCR);
  478. if (ctl < 0)
  479. return ctl;
  480. if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
  481. result = 1; /* do restart aneg */
  482. }
  483. /* Only restart aneg if we are advertising something different
  484. * than we were before. */
  485. if (result > 0)
  486. result = genphy_restart_aneg(phydev);
  487. return result;
  488. }
  489. EXPORT_SYMBOL(genphy_config_aneg);
  490. /**
  491. * genphy_update_link - update link status in @phydev
  492. * @phydev: target phy_device struct
  493. *
  494. * Description: Update the value in phydev->link to reflect the
  495. * current link value. In order to do this, we need to read
  496. * the status register twice, keeping the second value.
  497. */
  498. int genphy_update_link(struct phy_device *phydev)
  499. {
  500. int status;
  501. /* Do a fake read */
  502. status = phy_read(phydev, MII_BMSR);
  503. if (status < 0)
  504. return status;
  505. /* Read link and autonegotiation status */
  506. status = phy_read(phydev, MII_BMSR);
  507. if (status < 0)
  508. return status;
  509. if ((status & BMSR_LSTATUS) == 0)
  510. phydev->link = 0;
  511. else
  512. phydev->link = 1;
  513. return 0;
  514. }
  515. EXPORT_SYMBOL(genphy_update_link);
  516. /**
  517. * genphy_read_status - check the link status and update current link state
  518. * @phydev: target phy_device struct
  519. *
  520. * Description: Check the link, then figure out the current state
  521. * by comparing what we advertise with what the link partner
  522. * advertises. Start by checking the gigabit possibilities,
  523. * then move on to 10/100.
  524. */
  525. int genphy_read_status(struct phy_device *phydev)
  526. {
  527. int adv;
  528. int err;
  529. int lpa;
  530. int lpagb = 0;
  531. /* Update the link, but return if there
  532. * was an error */
  533. err = genphy_update_link(phydev);
  534. if (err)
  535. return err;
  536. if (AUTONEG_ENABLE == phydev->autoneg) {
  537. if (phydev->supported & (SUPPORTED_1000baseT_Half
  538. | SUPPORTED_1000baseT_Full)) {
  539. lpagb = phy_read(phydev, MII_STAT1000);
  540. if (lpagb < 0)
  541. return lpagb;
  542. adv = phy_read(phydev, MII_CTRL1000);
  543. if (adv < 0)
  544. return adv;
  545. lpagb &= adv << 2;
  546. }
  547. lpa = phy_read(phydev, MII_LPA);
  548. if (lpa < 0)
  549. return lpa;
  550. adv = phy_read(phydev, MII_ADVERTISE);
  551. if (adv < 0)
  552. return adv;
  553. lpa &= adv;
  554. phydev->speed = SPEED_10;
  555. phydev->duplex = DUPLEX_HALF;
  556. phydev->pause = phydev->asym_pause = 0;
  557. if (lpagb & (LPA_1000FULL | LPA_1000HALF)) {
  558. phydev->speed = SPEED_1000;
  559. if (lpagb & LPA_1000FULL)
  560. phydev->duplex = DUPLEX_FULL;
  561. } else if (lpa & (LPA_100FULL | LPA_100HALF)) {
  562. phydev->speed = SPEED_100;
  563. if (lpa & LPA_100FULL)
  564. phydev->duplex = DUPLEX_FULL;
  565. } else
  566. if (lpa & LPA_10FULL)
  567. phydev->duplex = DUPLEX_FULL;
  568. if (phydev->duplex == DUPLEX_FULL){
  569. phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
  570. phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
  571. }
  572. } else {
  573. int bmcr = phy_read(phydev, MII_BMCR);
  574. if (bmcr < 0)
  575. return bmcr;
  576. if (bmcr & BMCR_FULLDPLX)
  577. phydev->duplex = DUPLEX_FULL;
  578. else
  579. phydev->duplex = DUPLEX_HALF;
  580. if (bmcr & BMCR_SPEED1000)
  581. phydev->speed = SPEED_1000;
  582. else if (bmcr & BMCR_SPEED100)
  583. phydev->speed = SPEED_100;
  584. else
  585. phydev->speed = SPEED_10;
  586. phydev->pause = phydev->asym_pause = 0;
  587. }
  588. return 0;
  589. }
  590. EXPORT_SYMBOL(genphy_read_status);
  591. static int genphy_config_init(struct phy_device *phydev)
  592. {
  593. int val;
  594. u32 features;
  595. /* For now, I'll claim that the generic driver supports
  596. * all possible port types */
  597. features = (SUPPORTED_TP | SUPPORTED_MII
  598. | SUPPORTED_AUI | SUPPORTED_FIBRE |
  599. SUPPORTED_BNC);
  600. /* Do we support autonegotiation? */
  601. val = phy_read(phydev, MII_BMSR);
  602. if (val < 0)
  603. return val;
  604. if (val & BMSR_ANEGCAPABLE)
  605. features |= SUPPORTED_Autoneg;
  606. if (val & BMSR_100FULL)
  607. features |= SUPPORTED_100baseT_Full;
  608. if (val & BMSR_100HALF)
  609. features |= SUPPORTED_100baseT_Half;
  610. if (val & BMSR_10FULL)
  611. features |= SUPPORTED_10baseT_Full;
  612. if (val & BMSR_10HALF)
  613. features |= SUPPORTED_10baseT_Half;
  614. if (val & BMSR_ESTATEN) {
  615. val = phy_read(phydev, MII_ESTATUS);
  616. if (val < 0)
  617. return val;
  618. if (val & ESTATUS_1000_TFULL)
  619. features |= SUPPORTED_1000baseT_Full;
  620. if (val & ESTATUS_1000_THALF)
  621. features |= SUPPORTED_1000baseT_Half;
  622. }
  623. phydev->supported = features;
  624. phydev->advertising = features;
  625. return 0;
  626. }
  627. /**
  628. * phy_probe - probe and init a PHY device
  629. * @dev: device to probe and init
  630. *
  631. * Description: Take care of setting up the phy_device structure,
  632. * set the state to READY (the driver's init function should
  633. * set it to STARTING if needed).
  634. */
  635. static int phy_probe(struct device *dev)
  636. {
  637. struct phy_device *phydev;
  638. struct phy_driver *phydrv;
  639. struct device_driver *drv;
  640. int err = 0;
  641. phydev = to_phy_device(dev);
  642. /* Make sure the driver is held.
  643. * XXX -- Is this correct? */
  644. drv = get_driver(phydev->dev.driver);
  645. phydrv = to_phy_driver(drv);
  646. phydev->drv = phydrv;
  647. /* Disable the interrupt if the PHY doesn't support it */
  648. if (!(phydrv->flags & PHY_HAS_INTERRUPT))
  649. phydev->irq = PHY_POLL;
  650. mutex_lock(&phydev->lock);
  651. /* Start out supporting everything. Eventually,
  652. * a controller will attach, and may modify one
  653. * or both of these values */
  654. phydev->supported = phydrv->features;
  655. phydev->advertising = phydrv->features;
  656. /* Set the state to READY by default */
  657. phydev->state = PHY_READY;
  658. if (phydev->drv->probe)
  659. err = phydev->drv->probe(phydev);
  660. mutex_unlock(&phydev->lock);
  661. return err;
  662. }
  663. static int phy_remove(struct device *dev)
  664. {
  665. struct phy_device *phydev;
  666. phydev = to_phy_device(dev);
  667. mutex_lock(&phydev->lock);
  668. phydev->state = PHY_DOWN;
  669. mutex_unlock(&phydev->lock);
  670. if (phydev->drv->remove)
  671. phydev->drv->remove(phydev);
  672. put_driver(dev->driver);
  673. phydev->drv = NULL;
  674. return 0;
  675. }
  676. /**
  677. * phy_driver_register - register a phy_driver with the PHY layer
  678. * @new_driver: new phy_driver to register
  679. */
  680. int phy_driver_register(struct phy_driver *new_driver)
  681. {
  682. int retval;
  683. memset(&new_driver->driver, 0, sizeof(new_driver->driver));
  684. new_driver->driver.name = new_driver->name;
  685. new_driver->driver.bus = &mdio_bus_type;
  686. new_driver->driver.probe = phy_probe;
  687. new_driver->driver.remove = phy_remove;
  688. retval = driver_register(&new_driver->driver);
  689. if (retval) {
  690. printk(KERN_ERR "%s: Error %d in registering driver\n",
  691. new_driver->name, retval);
  692. return retval;
  693. }
  694. pr_debug("%s: Registered new driver\n", new_driver->name);
  695. return 0;
  696. }
  697. EXPORT_SYMBOL(phy_driver_register);
  698. void phy_driver_unregister(struct phy_driver *drv)
  699. {
  700. driver_unregister(&drv->driver);
  701. }
  702. EXPORT_SYMBOL(phy_driver_unregister);
  703. static struct phy_driver genphy_driver = {
  704. .phy_id = 0xffffffff,
  705. .phy_id_mask = 0xffffffff,
  706. .name = "Generic PHY",
  707. .config_init = genphy_config_init,
  708. .features = 0,
  709. .config_aneg = genphy_config_aneg,
  710. .read_status = genphy_read_status,
  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);