phy_device.c 27 KB

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