phy_device.c 24 KB

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