phy_device.c 28 KB

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