phy_device.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241
  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. err = phy_init_hw(phydev);
  308. if (err) {
  309. pr_err("PHY %d failed to initialize\n", phydev->addr);
  310. goto out;
  311. }
  312. err = device_add(&phydev->dev);
  313. if (err) {
  314. pr_err("PHY %d failed to add\n", phydev->addr);
  315. goto out;
  316. }
  317. return 0;
  318. out:
  319. phydev->bus->phy_map[phydev->addr] = NULL;
  320. return err;
  321. }
  322. EXPORT_SYMBOL(phy_device_register);
  323. /**
  324. * phy_find_first - finds the first PHY device on the bus
  325. * @bus: the target MII bus
  326. */
  327. struct phy_device *phy_find_first(struct mii_bus *bus)
  328. {
  329. int addr;
  330. for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
  331. if (bus->phy_map[addr])
  332. return bus->phy_map[addr];
  333. }
  334. return NULL;
  335. }
  336. EXPORT_SYMBOL(phy_find_first);
  337. /**
  338. * phy_prepare_link - prepares the PHY layer to monitor link status
  339. * @phydev: target phy_device struct
  340. * @handler: callback function for link status change notifications
  341. *
  342. * Description: Tells the PHY infrastructure to handle the
  343. * gory details on monitoring link status (whether through
  344. * polling or an interrupt), and to call back to the
  345. * connected device driver when the link status changes.
  346. * If you want to monitor your own link state, don't call
  347. * this function.
  348. */
  349. static void phy_prepare_link(struct phy_device *phydev,
  350. void (*handler)(struct net_device *))
  351. {
  352. phydev->adjust_link = handler;
  353. }
  354. /**
  355. * phy_connect_direct - connect an ethernet device to a specific phy_device
  356. * @dev: the network device to connect
  357. * @phydev: the pointer to the phy device
  358. * @handler: callback function for state change notifications
  359. * @interface: PHY device's interface
  360. */
  361. int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
  362. void (*handler)(struct net_device *),
  363. phy_interface_t interface)
  364. {
  365. int rc;
  366. rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
  367. if (rc)
  368. return rc;
  369. phy_prepare_link(phydev, handler);
  370. phy_start_machine(phydev, NULL);
  371. if (phydev->irq > 0)
  372. phy_start_interrupts(phydev);
  373. return 0;
  374. }
  375. EXPORT_SYMBOL(phy_connect_direct);
  376. /**
  377. * phy_connect - connect an ethernet device to a PHY device
  378. * @dev: the network device to connect
  379. * @bus_id: the id string of the PHY device to connect
  380. * @handler: callback function for state change notifications
  381. * @interface: PHY device's interface
  382. *
  383. * Description: Convenience function for connecting ethernet
  384. * devices to PHY devices. The default behavior is for
  385. * the PHY infrastructure to handle everything, and only notify
  386. * the connected driver when the link status changes. If you
  387. * don't want, or can't use the provided functionality, you may
  388. * choose to call only the subset of functions which provide
  389. * the desired functionality.
  390. */
  391. struct phy_device * phy_connect(struct net_device *dev, const char *bus_id,
  392. void (*handler)(struct net_device *),
  393. phy_interface_t interface)
  394. {
  395. struct phy_device *phydev;
  396. struct device *d;
  397. int rc;
  398. /* Search the list of PHY devices on the mdio bus for the
  399. * PHY with the requested name */
  400. d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id);
  401. if (!d) {
  402. pr_err("PHY %s not found\n", bus_id);
  403. return ERR_PTR(-ENODEV);
  404. }
  405. phydev = to_phy_device(d);
  406. rc = phy_connect_direct(dev, phydev, handler, interface);
  407. if (rc)
  408. return ERR_PTR(rc);
  409. return phydev;
  410. }
  411. EXPORT_SYMBOL(phy_connect);
  412. /**
  413. * phy_disconnect - disable interrupts, stop state machine, and detach a PHY device
  414. * @phydev: target phy_device struct
  415. */
  416. void phy_disconnect(struct phy_device *phydev)
  417. {
  418. if (phydev->irq > 0)
  419. phy_stop_interrupts(phydev);
  420. phy_stop_machine(phydev);
  421. phydev->adjust_link = NULL;
  422. phy_detach(phydev);
  423. }
  424. EXPORT_SYMBOL(phy_disconnect);
  425. /**
  426. * phy_poll_reset - Safely wait until a PHY reset has properly completed
  427. * @phydev: The PHY device to poll
  428. *
  429. * Description: According to IEEE 802.3, Section 2, Subsection 22.2.4.1.1, as
  430. * published in 2008, a PHY reset may take up to 0.5 seconds. The MII BMCR
  431. * register must be polled until the BMCR_RESET bit clears.
  432. *
  433. * Furthermore, any attempts to write to PHY registers may have no effect
  434. * or even generate MDIO bus errors until this is complete.
  435. *
  436. * Some PHYs (such as the Marvell 88E1111) don't entirely conform to the
  437. * standard and do not fully reset after the BMCR_RESET bit is set, and may
  438. * even *REQUIRE* a soft-reset to properly restart autonegotiation. In an
  439. * effort to support such broken PHYs, this function is separate from the
  440. * standard phy_init_hw() which will zero all the other bits in the BMCR
  441. * and reapply all driver-specific and board-specific fixups.
  442. */
  443. static int phy_poll_reset(struct phy_device *phydev)
  444. {
  445. /* Poll until the reset bit clears (50ms per retry == 0.6 sec) */
  446. unsigned int retries = 12;
  447. int ret;
  448. do {
  449. msleep(50);
  450. ret = phy_read(phydev, MII_BMCR);
  451. if (ret < 0)
  452. return ret;
  453. } while (ret & BMCR_RESET && --retries);
  454. if (ret & BMCR_RESET)
  455. return -ETIMEDOUT;
  456. /*
  457. * Some chips (smsc911x) may still need up to another 1ms after the
  458. * BMCR_RESET bit is cleared before they are usable.
  459. */
  460. msleep(1);
  461. return 0;
  462. }
  463. int phy_init_hw(struct phy_device *phydev)
  464. {
  465. int ret;
  466. if (!phydev->drv || !phydev->drv->config_init)
  467. return 0;
  468. ret = phy_write(phydev, MII_BMCR, BMCR_RESET);
  469. if (ret < 0)
  470. return ret;
  471. ret = phy_poll_reset(phydev);
  472. if (ret < 0)
  473. return ret;
  474. ret = phy_scan_fixups(phydev);
  475. if (ret < 0)
  476. return ret;
  477. return phydev->drv->config_init(phydev);
  478. }
  479. EXPORT_SYMBOL(phy_init_hw);
  480. /**
  481. * phy_attach_direct - attach a network device to a given PHY device pointer
  482. * @dev: network device to attach
  483. * @phydev: Pointer to phy_device to attach
  484. * @flags: PHY device's dev_flags
  485. * @interface: PHY device's interface
  486. *
  487. * Description: Called by drivers to attach to a particular PHY
  488. * device. The phy_device is found, and properly hooked up
  489. * to the phy_driver. If no driver is attached, then the
  490. * genphy_driver is used. The phy_device is given a ptr to
  491. * the attaching device, and given a callback for link status
  492. * change. The phy_device is returned to the attaching driver.
  493. */
  494. static int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
  495. u32 flags, phy_interface_t interface)
  496. {
  497. struct device *d = &phydev->dev;
  498. int err;
  499. /* Assume that if there is no driver, that it doesn't
  500. * exist, and we should use the genphy driver. */
  501. if (NULL == d->driver) {
  502. if (phydev->is_c45) {
  503. pr_err("No driver for phy %x\n", phydev->phy_id);
  504. return -ENODEV;
  505. }
  506. d->driver = &genphy_driver.driver;
  507. err = d->driver->probe(d);
  508. if (err >= 0)
  509. err = device_bind_driver(d);
  510. if (err)
  511. return err;
  512. }
  513. if (phydev->attached_dev) {
  514. dev_err(&dev->dev, "PHY already attached\n");
  515. return -EBUSY;
  516. }
  517. phydev->attached_dev = dev;
  518. dev->phydev = phydev;
  519. phydev->dev_flags = flags;
  520. phydev->interface = interface;
  521. phydev->state = PHY_READY;
  522. /* Do initial configuration here, now that
  523. * we have certain key parameters
  524. * (dev_flags and interface) */
  525. err = phy_init_hw(phydev);
  526. if (err)
  527. phy_detach(phydev);
  528. phy_resume(phydev);
  529. return err;
  530. }
  531. /**
  532. * phy_attach - attach a network device to a particular PHY device
  533. * @dev: network device to attach
  534. * @bus_id: Bus ID of PHY device to attach
  535. * @interface: PHY device's interface
  536. *
  537. * Description: Same as phy_attach_direct() except that a PHY bus_id
  538. * string is passed instead of a pointer to a struct phy_device.
  539. */
  540. struct phy_device *phy_attach(struct net_device *dev,
  541. const char *bus_id, phy_interface_t interface)
  542. {
  543. struct bus_type *bus = &mdio_bus_type;
  544. struct phy_device *phydev;
  545. struct device *d;
  546. int rc;
  547. /* Search the list of PHY devices on the mdio bus for the
  548. * PHY with the requested name */
  549. d = bus_find_device_by_name(bus, NULL, bus_id);
  550. if (!d) {
  551. pr_err("PHY %s not found\n", bus_id);
  552. return ERR_PTR(-ENODEV);
  553. }
  554. phydev = to_phy_device(d);
  555. rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
  556. if (rc)
  557. return ERR_PTR(rc);
  558. return phydev;
  559. }
  560. EXPORT_SYMBOL(phy_attach);
  561. /**
  562. * phy_detach - detach a PHY device from its network device
  563. * @phydev: target phy_device struct
  564. */
  565. void phy_detach(struct phy_device *phydev)
  566. {
  567. phydev->attached_dev->phydev = NULL;
  568. phydev->attached_dev = NULL;
  569. phy_suspend(phydev);
  570. /* If the device had no specific driver before (i.e. - it
  571. * was using the generic driver), we unbind the device
  572. * from the generic driver so that there's a chance a
  573. * real driver could be loaded */
  574. if (phydev->dev.driver == &genphy_driver.driver)
  575. device_release_driver(&phydev->dev);
  576. }
  577. EXPORT_SYMBOL(phy_detach);
  578. int phy_suspend(struct phy_device *phydev)
  579. {
  580. struct phy_driver *phydrv = to_phy_driver(phydev->dev.driver);
  581. struct ethtool_wolinfo wol;
  582. /* If the device has WOL enabled, we cannot suspend the PHY */
  583. wol.cmd = ETHTOOL_GWOL;
  584. phy_ethtool_get_wol(phydev, &wol);
  585. if (wol.wolopts)
  586. return -EBUSY;
  587. if (phydrv->suspend)
  588. return phydrv->suspend(phydev);
  589. return 0;
  590. }
  591. int phy_resume(struct phy_device *phydev)
  592. {
  593. struct phy_driver *phydrv = to_phy_driver(phydev->dev.driver);
  594. if (phydrv->resume)
  595. return phydrv->resume(phydev);
  596. return 0;
  597. }
  598. /* Generic PHY support and helper functions */
  599. /**
  600. * genphy_config_advert - sanitize and advertise auto-negotiation parameters
  601. * @phydev: target phy_device struct
  602. *
  603. * Description: Writes MII_ADVERTISE with the appropriate values,
  604. * after sanitizing the values to make sure we only advertise
  605. * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
  606. * hasn't changed, and > 0 if it has changed.
  607. */
  608. static int genphy_config_advert(struct phy_device *phydev)
  609. {
  610. u32 advertise;
  611. int oldadv, adv;
  612. int err, changed = 0;
  613. /* Only allow advertising what
  614. * this PHY supports */
  615. phydev->advertising &= phydev->supported;
  616. advertise = phydev->advertising;
  617. /* Setup standard advertisement */
  618. oldadv = adv = phy_read(phydev, MII_ADVERTISE);
  619. if (adv < 0)
  620. return adv;
  621. adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
  622. ADVERTISE_PAUSE_ASYM);
  623. adv |= ethtool_adv_to_mii_adv_t(advertise);
  624. if (adv != oldadv) {
  625. err = phy_write(phydev, MII_ADVERTISE, adv);
  626. if (err < 0)
  627. return err;
  628. changed = 1;
  629. }
  630. /* Configure gigabit if it's supported */
  631. if (phydev->supported & (SUPPORTED_1000baseT_Half |
  632. SUPPORTED_1000baseT_Full)) {
  633. oldadv = adv = phy_read(phydev, MII_CTRL1000);
  634. if (adv < 0)
  635. return adv;
  636. adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
  637. adv |= ethtool_adv_to_mii_ctrl1000_t(advertise);
  638. if (adv != oldadv) {
  639. err = phy_write(phydev, MII_CTRL1000, adv);
  640. if (err < 0)
  641. return err;
  642. changed = 1;
  643. }
  644. }
  645. return changed;
  646. }
  647. /**
  648. * genphy_setup_forced - configures/forces speed/duplex from @phydev
  649. * @phydev: target phy_device struct
  650. *
  651. * Description: Configures MII_BMCR to force speed/duplex
  652. * to the values in phydev. Assumes that the values are valid.
  653. * Please see phy_sanitize_settings().
  654. */
  655. int genphy_setup_forced(struct phy_device *phydev)
  656. {
  657. int err;
  658. int ctl = 0;
  659. phydev->pause = phydev->asym_pause = 0;
  660. if (SPEED_1000 == phydev->speed)
  661. ctl |= BMCR_SPEED1000;
  662. else if (SPEED_100 == phydev->speed)
  663. ctl |= BMCR_SPEED100;
  664. if (DUPLEX_FULL == phydev->duplex)
  665. ctl |= BMCR_FULLDPLX;
  666. err = phy_write(phydev, MII_BMCR, ctl);
  667. return err;
  668. }
  669. EXPORT_SYMBOL(genphy_setup_forced);
  670. /**
  671. * genphy_restart_aneg - Enable and Restart Autonegotiation
  672. * @phydev: target phy_device struct
  673. */
  674. int genphy_restart_aneg(struct phy_device *phydev)
  675. {
  676. int ctl;
  677. ctl = phy_read(phydev, MII_BMCR);
  678. if (ctl < 0)
  679. return ctl;
  680. ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
  681. /* Don't isolate the PHY if we're negotiating */
  682. ctl &= ~(BMCR_ISOLATE);
  683. ctl = phy_write(phydev, MII_BMCR, ctl);
  684. return ctl;
  685. }
  686. EXPORT_SYMBOL(genphy_restart_aneg);
  687. /**
  688. * genphy_config_aneg - restart auto-negotiation or write BMCR
  689. * @phydev: target phy_device struct
  690. *
  691. * Description: If auto-negotiation is enabled, we configure the
  692. * advertising, and then restart auto-negotiation. If it is not
  693. * enabled, then we write the BMCR.
  694. */
  695. int genphy_config_aneg(struct phy_device *phydev)
  696. {
  697. int result;
  698. if (AUTONEG_ENABLE != phydev->autoneg)
  699. return genphy_setup_forced(phydev);
  700. result = genphy_config_advert(phydev);
  701. if (result < 0) /* error */
  702. return result;
  703. if (result == 0) {
  704. /* Advertisement hasn't changed, but maybe aneg was never on to
  705. * begin with? Or maybe phy was isolated? */
  706. int ctl = phy_read(phydev, MII_BMCR);
  707. if (ctl < 0)
  708. return ctl;
  709. if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
  710. result = 1; /* do restart aneg */
  711. }
  712. /* Only restart aneg if we are advertising something different
  713. * than we were before. */
  714. if (result > 0)
  715. result = genphy_restart_aneg(phydev);
  716. return result;
  717. }
  718. EXPORT_SYMBOL(genphy_config_aneg);
  719. /**
  720. * genphy_update_link - update link status in @phydev
  721. * @phydev: target phy_device struct
  722. *
  723. * Description: Update the value in phydev->link to reflect the
  724. * current link value. In order to do this, we need to read
  725. * the status register twice, keeping the second value.
  726. */
  727. int genphy_update_link(struct phy_device *phydev)
  728. {
  729. int status;
  730. /* Do a fake read */
  731. status = phy_read(phydev, MII_BMSR);
  732. if (status < 0)
  733. return status;
  734. /* Read link and autonegotiation status */
  735. status = phy_read(phydev, MII_BMSR);
  736. if (status < 0)
  737. return status;
  738. if ((status & BMSR_LSTATUS) == 0)
  739. phydev->link = 0;
  740. else
  741. phydev->link = 1;
  742. return 0;
  743. }
  744. EXPORT_SYMBOL(genphy_update_link);
  745. /**
  746. * genphy_read_status - check the link status and update current link state
  747. * @phydev: target phy_device struct
  748. *
  749. * Description: Check the link, then figure out the current state
  750. * by comparing what we advertise with what the link partner
  751. * advertises. Start by checking the gigabit possibilities,
  752. * then move on to 10/100.
  753. */
  754. int genphy_read_status(struct phy_device *phydev)
  755. {
  756. int adv;
  757. int err;
  758. int lpa;
  759. int lpagb = 0;
  760. /* Update the link, but return if there
  761. * was an error */
  762. err = genphy_update_link(phydev);
  763. if (err)
  764. return err;
  765. phydev->lp_advertising = 0;
  766. if (AUTONEG_ENABLE == phydev->autoneg) {
  767. if (phydev->supported & (SUPPORTED_1000baseT_Half
  768. | SUPPORTED_1000baseT_Full)) {
  769. lpagb = phy_read(phydev, MII_STAT1000);
  770. if (lpagb < 0)
  771. return lpagb;
  772. adv = phy_read(phydev, MII_CTRL1000);
  773. if (adv < 0)
  774. return adv;
  775. phydev->lp_advertising =
  776. mii_stat1000_to_ethtool_lpa_t(lpagb);
  777. lpagb &= adv << 2;
  778. }
  779. lpa = phy_read(phydev, MII_LPA);
  780. if (lpa < 0)
  781. return lpa;
  782. phydev->lp_advertising |= mii_lpa_to_ethtool_lpa_t(lpa);
  783. adv = phy_read(phydev, MII_ADVERTISE);
  784. if (adv < 0)
  785. return adv;
  786. lpa &= adv;
  787. phydev->speed = SPEED_10;
  788. phydev->duplex = DUPLEX_HALF;
  789. phydev->pause = phydev->asym_pause = 0;
  790. if (lpagb & (LPA_1000FULL | LPA_1000HALF)) {
  791. phydev->speed = SPEED_1000;
  792. if (lpagb & LPA_1000FULL)
  793. phydev->duplex = DUPLEX_FULL;
  794. } else if (lpa & (LPA_100FULL | LPA_100HALF)) {
  795. phydev->speed = SPEED_100;
  796. if (lpa & LPA_100FULL)
  797. phydev->duplex = DUPLEX_FULL;
  798. } else
  799. if (lpa & LPA_10FULL)
  800. phydev->duplex = DUPLEX_FULL;
  801. if (phydev->duplex == DUPLEX_FULL){
  802. phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
  803. phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
  804. }
  805. } else {
  806. int bmcr = phy_read(phydev, MII_BMCR);
  807. if (bmcr < 0)
  808. return bmcr;
  809. if (bmcr & BMCR_FULLDPLX)
  810. phydev->duplex = DUPLEX_FULL;
  811. else
  812. phydev->duplex = DUPLEX_HALF;
  813. if (bmcr & BMCR_SPEED1000)
  814. phydev->speed = SPEED_1000;
  815. else if (bmcr & BMCR_SPEED100)
  816. phydev->speed = SPEED_100;
  817. else
  818. phydev->speed = SPEED_10;
  819. phydev->pause = phydev->asym_pause = 0;
  820. }
  821. return 0;
  822. }
  823. EXPORT_SYMBOL(genphy_read_status);
  824. static int genphy_config_init(struct phy_device *phydev)
  825. {
  826. int val;
  827. u32 features;
  828. /* For now, I'll claim that the generic driver supports
  829. * all possible port types */
  830. features = (SUPPORTED_TP | SUPPORTED_MII
  831. | SUPPORTED_AUI | SUPPORTED_FIBRE |
  832. SUPPORTED_BNC);
  833. /* Do we support autonegotiation? */
  834. val = phy_read(phydev, MII_BMSR);
  835. if (val < 0)
  836. return val;
  837. if (val & BMSR_ANEGCAPABLE)
  838. features |= SUPPORTED_Autoneg;
  839. if (val & BMSR_100FULL)
  840. features |= SUPPORTED_100baseT_Full;
  841. if (val & BMSR_100HALF)
  842. features |= SUPPORTED_100baseT_Half;
  843. if (val & BMSR_10FULL)
  844. features |= SUPPORTED_10baseT_Full;
  845. if (val & BMSR_10HALF)
  846. features |= SUPPORTED_10baseT_Half;
  847. if (val & BMSR_ESTATEN) {
  848. val = phy_read(phydev, MII_ESTATUS);
  849. if (val < 0)
  850. return val;
  851. if (val & ESTATUS_1000_TFULL)
  852. features |= SUPPORTED_1000baseT_Full;
  853. if (val & ESTATUS_1000_THALF)
  854. features |= SUPPORTED_1000baseT_Half;
  855. }
  856. phydev->supported = features;
  857. phydev->advertising = features;
  858. return 0;
  859. }
  860. int genphy_suspend(struct phy_device *phydev)
  861. {
  862. int value;
  863. mutex_lock(&phydev->lock);
  864. value = phy_read(phydev, MII_BMCR);
  865. phy_write(phydev, MII_BMCR, (value | BMCR_PDOWN));
  866. mutex_unlock(&phydev->lock);
  867. return 0;
  868. }
  869. EXPORT_SYMBOL(genphy_suspend);
  870. int genphy_resume(struct phy_device *phydev)
  871. {
  872. int value;
  873. mutex_lock(&phydev->lock);
  874. value = phy_read(phydev, MII_BMCR);
  875. phy_write(phydev, MII_BMCR, (value & ~BMCR_PDOWN));
  876. mutex_unlock(&phydev->lock);
  877. return 0;
  878. }
  879. EXPORT_SYMBOL(genphy_resume);
  880. /**
  881. * phy_probe - probe and init a PHY device
  882. * @dev: device to probe and init
  883. *
  884. * Description: Take care of setting up the phy_device structure,
  885. * set the state to READY (the driver's init function should
  886. * set it to STARTING if needed).
  887. */
  888. static int phy_probe(struct device *dev)
  889. {
  890. struct phy_device *phydev;
  891. struct phy_driver *phydrv;
  892. struct device_driver *drv;
  893. int err = 0;
  894. phydev = to_phy_device(dev);
  895. drv = phydev->dev.driver;
  896. phydrv = to_phy_driver(drv);
  897. phydev->drv = phydrv;
  898. /* Disable the interrupt if the PHY doesn't support it
  899. * but the interrupt is still a valid one
  900. */
  901. if (!(phydrv->flags & PHY_HAS_INTERRUPT) &&
  902. phy_interrupt_is_valid(phydev))
  903. phydev->irq = PHY_POLL;
  904. if (phydrv->flags & PHY_IS_INTERNAL)
  905. phydev->is_internal = true;
  906. mutex_lock(&phydev->lock);
  907. /* Start out supporting everything. Eventually,
  908. * a controller will attach, and may modify one
  909. * or both of these values */
  910. phydev->supported = phydrv->features;
  911. phydev->advertising = phydrv->features;
  912. /* Set the state to READY by default */
  913. phydev->state = PHY_READY;
  914. if (phydev->drv->probe)
  915. err = phydev->drv->probe(phydev);
  916. mutex_unlock(&phydev->lock);
  917. return err;
  918. }
  919. static int phy_remove(struct device *dev)
  920. {
  921. struct phy_device *phydev;
  922. phydev = to_phy_device(dev);
  923. mutex_lock(&phydev->lock);
  924. phydev->state = PHY_DOWN;
  925. mutex_unlock(&phydev->lock);
  926. if (phydev->drv->remove)
  927. phydev->drv->remove(phydev);
  928. phydev->drv = NULL;
  929. return 0;
  930. }
  931. /**
  932. * phy_driver_register - register a phy_driver with the PHY layer
  933. * @new_driver: new phy_driver to register
  934. */
  935. int phy_driver_register(struct phy_driver *new_driver)
  936. {
  937. int retval;
  938. new_driver->driver.name = new_driver->name;
  939. new_driver->driver.bus = &mdio_bus_type;
  940. new_driver->driver.probe = phy_probe;
  941. new_driver->driver.remove = phy_remove;
  942. retval = driver_register(&new_driver->driver);
  943. if (retval) {
  944. pr_err("%s: Error %d in registering driver\n",
  945. new_driver->name, retval);
  946. return retval;
  947. }
  948. pr_debug("%s: Registered new driver\n", new_driver->name);
  949. return 0;
  950. }
  951. EXPORT_SYMBOL(phy_driver_register);
  952. int phy_drivers_register(struct phy_driver *new_driver, int n)
  953. {
  954. int i, ret = 0;
  955. for (i = 0; i < n; i++) {
  956. ret = phy_driver_register(new_driver + i);
  957. if (ret) {
  958. while (i-- > 0)
  959. phy_driver_unregister(new_driver + i);
  960. break;
  961. }
  962. }
  963. return ret;
  964. }
  965. EXPORT_SYMBOL(phy_drivers_register);
  966. void phy_driver_unregister(struct phy_driver *drv)
  967. {
  968. driver_unregister(&drv->driver);
  969. }
  970. EXPORT_SYMBOL(phy_driver_unregister);
  971. void phy_drivers_unregister(struct phy_driver *drv, int n)
  972. {
  973. int i;
  974. for (i = 0; i < n; i++) {
  975. phy_driver_unregister(drv + i);
  976. }
  977. }
  978. EXPORT_SYMBOL(phy_drivers_unregister);
  979. static struct phy_driver genphy_driver = {
  980. .phy_id = 0xffffffff,
  981. .phy_id_mask = 0xffffffff,
  982. .name = "Generic PHY",
  983. .config_init = genphy_config_init,
  984. .features = 0,
  985. .config_aneg = genphy_config_aneg,
  986. .read_status = genphy_read_status,
  987. .suspend = genphy_suspend,
  988. .resume = genphy_resume,
  989. .driver = {.owner= THIS_MODULE, },
  990. };
  991. static int __init phy_init(void)
  992. {
  993. int rc;
  994. rc = mdio_bus_init();
  995. if (rc)
  996. return rc;
  997. rc = phy_driver_register(&genphy_driver);
  998. if (rc)
  999. mdio_bus_exit();
  1000. return rc;
  1001. }
  1002. static void __exit phy_exit(void)
  1003. {
  1004. phy_driver_unregister(&genphy_driver);
  1005. mdio_bus_exit();
  1006. }
  1007. subsys_initcall(phy_init);
  1008. module_exit(phy_exit);