phy_device.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490
  1. /* Framework for finding and configuring PHYs.
  2. * Also contains generic PHY driver
  3. *
  4. * Author: Andy Fleming
  5. *
  6. * Copyright (c) 2004 Freescale Semiconductor, Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. */
  14. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  15. #include <linux/kernel.h>
  16. #include <linux/string.h>
  17. #include <linux/errno.h>
  18. #include <linux/unistd.h>
  19. #include <linux/slab.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/init.h>
  22. #include <linux/delay.h>
  23. #include <linux/netdevice.h>
  24. #include <linux/etherdevice.h>
  25. #include <linux/skbuff.h>
  26. #include <linux/mm.h>
  27. #include <linux/module.h>
  28. #include <linux/mii.h>
  29. #include <linux/ethtool.h>
  30. #include <linux/phy.h>
  31. #include <linux/mdio.h>
  32. #include <linux/io.h>
  33. #include <linux/uaccess.h>
  34. #include <linux/of.h>
  35. #include <asm/irq.h>
  36. MODULE_DESCRIPTION("PHY library");
  37. MODULE_AUTHOR("Andy Fleming");
  38. MODULE_LICENSE("GPL");
  39. void phy_device_free(struct phy_device *phydev)
  40. {
  41. put_device(&phydev->dev);
  42. }
  43. EXPORT_SYMBOL(phy_device_free);
  44. static void phy_device_release(struct device *dev)
  45. {
  46. kfree(to_phy_device(dev));
  47. }
  48. enum genphy_driver {
  49. GENPHY_DRV_1G,
  50. GENPHY_DRV_10G,
  51. GENPHY_DRV_MAX
  52. };
  53. static struct phy_driver genphy_driver[GENPHY_DRV_MAX];
  54. static LIST_HEAD(phy_fixup_list);
  55. static DEFINE_MUTEX(phy_fixup_lock);
  56. /**
  57. * phy_register_fixup - 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 = kzalloc(sizeof(*fixup), GFP_KERNEL);
  69. if (!fixup)
  70. return -ENOMEM;
  71. strlcpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id));
  72. fixup->phy_uid = phy_uid;
  73. fixup->phy_uid_mask = phy_uid_mask;
  74. fixup->run = run;
  75. mutex_lock(&phy_fixup_lock);
  76. list_add_tail(&fixup->list, &phy_fixup_list);
  77. mutex_unlock(&phy_fixup_lock);
  78. return 0;
  79. }
  80. EXPORT_SYMBOL(phy_register_fixup);
  81. /* Registers a fixup to be run on any PHY with the UID in phy_uid */
  82. int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
  83. int (*run)(struct phy_device *))
  84. {
  85. return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run);
  86. }
  87. EXPORT_SYMBOL(phy_register_fixup_for_uid);
  88. /* Registers a fixup to be run on the PHY with id string bus_id */
  89. int phy_register_fixup_for_id(const char *bus_id,
  90. int (*run)(struct phy_device *))
  91. {
  92. return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run);
  93. }
  94. EXPORT_SYMBOL(phy_register_fixup_for_id);
  95. /* Returns 1 if fixup matches phydev in bus_id and phy_uid.
  96. * Fixups can be set to match any in one or more fields.
  97. */
  98. static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup)
  99. {
  100. if (strcmp(fixup->bus_id, dev_name(&phydev->dev)) != 0)
  101. if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0)
  102. return 0;
  103. if ((fixup->phy_uid & fixup->phy_uid_mask) !=
  104. (phydev->phy_id & fixup->phy_uid_mask))
  105. if (fixup->phy_uid != PHY_ANY_UID)
  106. return 0;
  107. return 1;
  108. }
  109. /* Runs any matching fixups for this phydev */
  110. static int phy_scan_fixups(struct phy_device *phydev)
  111. {
  112. struct phy_fixup *fixup;
  113. mutex_lock(&phy_fixup_lock);
  114. list_for_each_entry(fixup, &phy_fixup_list, list) {
  115. if (phy_needs_fixup(phydev, fixup)) {
  116. int err = fixup->run(phydev);
  117. if (err < 0) {
  118. mutex_unlock(&phy_fixup_lock);
  119. return err;
  120. }
  121. phydev->has_fixups = true;
  122. }
  123. }
  124. mutex_unlock(&phy_fixup_lock);
  125. return 0;
  126. }
  127. struct phy_device *phy_device_create(struct mii_bus *bus, int addr, int phy_id,
  128. bool is_c45,
  129. struct phy_c45_device_ids *c45_ids)
  130. {
  131. struct phy_device *dev;
  132. /* We allocate the device, and initialize the default values */
  133. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  134. if (!dev)
  135. return ERR_PTR(-ENOMEM);
  136. dev->dev.release = phy_device_release;
  137. dev->speed = 0;
  138. dev->duplex = -1;
  139. dev->pause = 0;
  140. dev->asym_pause = 0;
  141. dev->link = 1;
  142. dev->interface = PHY_INTERFACE_MODE_GMII;
  143. dev->autoneg = AUTONEG_ENABLE;
  144. dev->is_c45 = is_c45;
  145. dev->addr = addr;
  146. dev->phy_id = phy_id;
  147. if (c45_ids)
  148. dev->c45_ids = *c45_ids;
  149. dev->bus = bus;
  150. dev->dev.parent = &bus->dev;
  151. dev->dev.bus = &mdio_bus_type;
  152. dev->irq = bus->irq ? bus->irq[addr] : PHY_POLL;
  153. dev_set_name(&dev->dev, PHY_ID_FMT, bus->id, addr);
  154. dev->state = PHY_DOWN;
  155. mutex_init(&dev->lock);
  156. INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine);
  157. INIT_WORK(&dev->phy_queue, phy_change);
  158. /* Request the appropriate module unconditionally; don't
  159. * bother trying to do so only if it isn't already loaded,
  160. * because that gets complicated. A hotplug event would have
  161. * done an unconditional modprobe anyway.
  162. * We don't do normal hotplug because it won't work for MDIO
  163. * -- because it relies on the device staying around for long
  164. * enough for the driver to get loaded. With MDIO, the NIC
  165. * driver will get bored and give up as soon as it finds that
  166. * there's no driver _already_ loaded.
  167. */
  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. /* get_phy_c45_devs_in_pkg - reads a MMD's devices in package registers.
  174. * @bus: the target MII bus
  175. * @addr: PHY address on the MII bus
  176. * @dev_addr: MMD address in the PHY.
  177. * @devices_in_package: where to store the devices in package information.
  178. *
  179. * Description: reads devices in package registers of a MMD at @dev_addr
  180. * from PHY at @addr on @bus.
  181. *
  182. * Returns: 0 on success, -EIO on failure.
  183. */
  184. static int get_phy_c45_devs_in_pkg(struct mii_bus *bus, int addr, int dev_addr,
  185. u32 *devices_in_package)
  186. {
  187. int phy_reg, reg_addr;
  188. reg_addr = MII_ADDR_C45 | dev_addr << 16 | MDIO_DEVS2;
  189. phy_reg = mdiobus_read(bus, addr, reg_addr);
  190. if (phy_reg < 0)
  191. return -EIO;
  192. *devices_in_package = (phy_reg & 0xffff) << 16;
  193. reg_addr = MII_ADDR_C45 | dev_addr << 16 | MDIO_DEVS1;
  194. phy_reg = mdiobus_read(bus, addr, reg_addr);
  195. if (phy_reg < 0)
  196. return -EIO;
  197. *devices_in_package |= (phy_reg & 0xffff);
  198. return 0;
  199. }
  200. /**
  201. * get_phy_c45_ids - reads the specified addr for its 802.3-c45 IDs.
  202. * @bus: the target MII bus
  203. * @addr: PHY address on the MII bus
  204. * @phy_id: where to store the ID retrieved.
  205. * @c45_ids: where to store the c45 ID information.
  206. *
  207. * If the PHY devices-in-package appears to be valid, it and the
  208. * corresponding identifiers are stored in @c45_ids, zero is stored
  209. * in @phy_id. Otherwise 0xffffffff is stored in @phy_id. Returns
  210. * zero on success.
  211. *
  212. */
  213. static int get_phy_c45_ids(struct mii_bus *bus, int addr, u32 *phy_id,
  214. struct phy_c45_device_ids *c45_ids) {
  215. int phy_reg;
  216. int i, reg_addr;
  217. const int num_ids = ARRAY_SIZE(c45_ids->device_ids);
  218. u32 *devs = &c45_ids->devices_in_package;
  219. /* Find first non-zero Devices In package. Device zero is reserved
  220. * for 802.3 c45 complied PHYs, so don't probe it at first.
  221. */
  222. for (i = 1; i < num_ids && *devs == 0; i++) {
  223. phy_reg = get_phy_c45_devs_in_pkg(bus, addr, i, devs);
  224. if (phy_reg < 0)
  225. return -EIO;
  226. if ((*devs & 0x1fffffff) == 0x1fffffff) {
  227. /* If mostly Fs, there is no device there,
  228. * then let's continue to probe more, as some
  229. * 10G PHYs have zero Devices In package,
  230. * e.g. Cortina CS4315/CS4340 PHY.
  231. */
  232. phy_reg = get_phy_c45_devs_in_pkg(bus, addr, 0, devs);
  233. if (phy_reg < 0)
  234. return -EIO;
  235. /* no device there, let's get out of here */
  236. if ((*devs & 0x1fffffff) == 0x1fffffff) {
  237. *phy_id = 0xffffffff;
  238. return 0;
  239. } else {
  240. break;
  241. }
  242. }
  243. }
  244. /* Now probe Device Identifiers for each device present. */
  245. for (i = 1; i < num_ids; i++) {
  246. if (!(c45_ids->devices_in_package & (1 << i)))
  247. continue;
  248. reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID1;
  249. phy_reg = mdiobus_read(bus, addr, reg_addr);
  250. if (phy_reg < 0)
  251. return -EIO;
  252. c45_ids->device_ids[i] = (phy_reg & 0xffff) << 16;
  253. reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID2;
  254. phy_reg = mdiobus_read(bus, addr, reg_addr);
  255. if (phy_reg < 0)
  256. return -EIO;
  257. c45_ids->device_ids[i] |= (phy_reg & 0xffff);
  258. }
  259. *phy_id = 0;
  260. return 0;
  261. }
  262. /**
  263. * get_phy_id - reads the specified addr for its ID.
  264. * @bus: the target MII bus
  265. * @addr: PHY address on the MII bus
  266. * @phy_id: where to store the ID retrieved.
  267. * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
  268. * @c45_ids: where to store the c45 ID information.
  269. *
  270. * Description: In the case of a 802.3-c22 PHY, reads the ID registers
  271. * of the PHY at @addr on the @bus, stores it in @phy_id and returns
  272. * zero on success.
  273. *
  274. * In the case of a 802.3-c45 PHY, get_phy_c45_ids() is invoked, and
  275. * its return value is in turn returned.
  276. *
  277. */
  278. static int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id,
  279. bool is_c45, struct phy_c45_device_ids *c45_ids)
  280. {
  281. int phy_reg;
  282. if (is_c45)
  283. return get_phy_c45_ids(bus, addr, phy_id, c45_ids);
  284. /* Grab the bits from PHYIR1, and put them in the upper half */
  285. phy_reg = mdiobus_read(bus, addr, MII_PHYSID1);
  286. if (phy_reg < 0)
  287. return -EIO;
  288. *phy_id = (phy_reg & 0xffff) << 16;
  289. /* Grab the bits from PHYIR2, and put them in the lower half */
  290. phy_reg = mdiobus_read(bus, addr, MII_PHYSID2);
  291. if (phy_reg < 0)
  292. return -EIO;
  293. *phy_id |= (phy_reg & 0xffff);
  294. return 0;
  295. }
  296. /**
  297. * get_phy_device - reads the specified PHY device and returns its @phy_device
  298. * struct
  299. * @bus: the target MII bus
  300. * @addr: PHY address on the MII bus
  301. * @is_c45: If true the PHY uses the 802.3 clause 45 protocol
  302. *
  303. * Description: Reads the ID registers of the PHY at @addr on the
  304. * @bus, then allocates and returns the phy_device to represent it.
  305. */
  306. struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45)
  307. {
  308. struct phy_c45_device_ids c45_ids = {0};
  309. u32 phy_id = 0;
  310. int r;
  311. r = get_phy_id(bus, addr, &phy_id, is_c45, &c45_ids);
  312. if (r)
  313. return ERR_PTR(r);
  314. /* If the phy_id is mostly Fs, there is no device there */
  315. if ((phy_id & 0x1fffffff) == 0x1fffffff)
  316. return NULL;
  317. return phy_device_create(bus, addr, phy_id, is_c45, &c45_ids);
  318. }
  319. EXPORT_SYMBOL(get_phy_device);
  320. /**
  321. * phy_device_register - Register the phy device on the MDIO bus
  322. * @phydev: phy_device structure to be added to the MDIO bus
  323. */
  324. int phy_device_register(struct phy_device *phydev)
  325. {
  326. int err;
  327. /* Don't register a phy if one is already registered at this address */
  328. if (phydev->bus->phy_map[phydev->addr])
  329. return -EINVAL;
  330. phydev->bus->phy_map[phydev->addr] = phydev;
  331. /* Run all of the fixups for this PHY */
  332. err = phy_scan_fixups(phydev);
  333. if (err) {
  334. pr_err("PHY %d failed to initialize\n", phydev->addr);
  335. goto out;
  336. }
  337. err = device_add(&phydev->dev);
  338. if (err) {
  339. pr_err("PHY %d failed to add\n", phydev->addr);
  340. goto out;
  341. }
  342. return 0;
  343. out:
  344. phydev->bus->phy_map[phydev->addr] = NULL;
  345. return err;
  346. }
  347. EXPORT_SYMBOL(phy_device_register);
  348. /**
  349. * phy_device_remove - Remove a previously registered phy device from the MDIO bus
  350. * @phydev: phy_device structure to remove
  351. *
  352. * This doesn't free the phy_device itself, it merely reverses the effects
  353. * of phy_device_register(). Use phy_device_free() to free the device
  354. * after calling this function.
  355. */
  356. void phy_device_remove(struct phy_device *phydev)
  357. {
  358. struct mii_bus *bus = phydev->bus;
  359. int addr = phydev->addr;
  360. device_del(&phydev->dev);
  361. bus->phy_map[addr] = NULL;
  362. }
  363. EXPORT_SYMBOL(phy_device_remove);
  364. /**
  365. * phy_find_first - finds the first PHY device on the bus
  366. * @bus: the target MII bus
  367. */
  368. struct phy_device *phy_find_first(struct mii_bus *bus)
  369. {
  370. int addr;
  371. for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
  372. if (bus->phy_map[addr])
  373. return bus->phy_map[addr];
  374. }
  375. return NULL;
  376. }
  377. EXPORT_SYMBOL(phy_find_first);
  378. /**
  379. * phy_prepare_link - prepares the PHY layer to monitor link status
  380. * @phydev: target phy_device struct
  381. * @handler: callback function for link status change notifications
  382. *
  383. * Description: Tells the PHY infrastructure to handle the
  384. * gory details on monitoring link status (whether through
  385. * polling or an interrupt), and to call back to the
  386. * connected device driver when the link status changes.
  387. * If you want to monitor your own link state, don't call
  388. * this function.
  389. */
  390. static void phy_prepare_link(struct phy_device *phydev,
  391. void (*handler)(struct net_device *))
  392. {
  393. phydev->adjust_link = handler;
  394. }
  395. /**
  396. * phy_connect_direct - connect an ethernet device to a specific phy_device
  397. * @dev: the network device to connect
  398. * @phydev: the pointer to the phy device
  399. * @handler: callback function for state change notifications
  400. * @interface: PHY device's interface
  401. */
  402. int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
  403. void (*handler)(struct net_device *),
  404. phy_interface_t interface)
  405. {
  406. int rc;
  407. rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
  408. if (rc)
  409. return rc;
  410. phy_prepare_link(phydev, handler);
  411. phy_start_machine(phydev);
  412. if (phydev->irq > 0)
  413. phy_start_interrupts(phydev);
  414. return 0;
  415. }
  416. EXPORT_SYMBOL(phy_connect_direct);
  417. /**
  418. * phy_connect - connect an ethernet device to a PHY device
  419. * @dev: the network device to connect
  420. * @bus_id: the id string of the PHY device to connect
  421. * @handler: callback function for state change notifications
  422. * @interface: PHY device's interface
  423. *
  424. * Description: Convenience function for connecting ethernet
  425. * devices to PHY devices. The default behavior is for
  426. * the PHY infrastructure to handle everything, and only notify
  427. * the connected driver when the link status changes. If you
  428. * don't want, or can't use the provided functionality, you may
  429. * choose to call only the subset of functions which provide
  430. * the desired functionality.
  431. */
  432. struct phy_device *phy_connect(struct net_device *dev, const char *bus_id,
  433. void (*handler)(struct net_device *),
  434. phy_interface_t interface)
  435. {
  436. struct phy_device *phydev;
  437. struct device *d;
  438. int rc;
  439. /* Search the list of PHY devices on the mdio bus for the
  440. * PHY with the requested name
  441. */
  442. d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id);
  443. if (!d) {
  444. pr_err("PHY %s not found\n", bus_id);
  445. return ERR_PTR(-ENODEV);
  446. }
  447. phydev = to_phy_device(d);
  448. rc = phy_connect_direct(dev, phydev, handler, interface);
  449. if (rc)
  450. return ERR_PTR(rc);
  451. return phydev;
  452. }
  453. EXPORT_SYMBOL(phy_connect);
  454. /**
  455. * phy_disconnect - disable interrupts, stop state machine, and detach a PHY
  456. * device
  457. * @phydev: target phy_device struct
  458. */
  459. void phy_disconnect(struct phy_device *phydev)
  460. {
  461. if (phydev->irq > 0)
  462. phy_stop_interrupts(phydev);
  463. phy_stop_machine(phydev);
  464. phydev->adjust_link = NULL;
  465. phy_detach(phydev);
  466. }
  467. EXPORT_SYMBOL(phy_disconnect);
  468. /**
  469. * phy_poll_reset - Safely wait until a PHY reset has properly completed
  470. * @phydev: The PHY device to poll
  471. *
  472. * Description: According to IEEE 802.3, Section 2, Subsection 22.2.4.1.1, as
  473. * published in 2008, a PHY reset may take up to 0.5 seconds. The MII BMCR
  474. * register must be polled until the BMCR_RESET bit clears.
  475. *
  476. * Furthermore, any attempts to write to PHY registers may have no effect
  477. * or even generate MDIO bus errors until this is complete.
  478. *
  479. * Some PHYs (such as the Marvell 88E1111) don't entirely conform to the
  480. * standard and do not fully reset after the BMCR_RESET bit is set, and may
  481. * even *REQUIRE* a soft-reset to properly restart autonegotiation. In an
  482. * effort to support such broken PHYs, this function is separate from the
  483. * standard phy_init_hw() which will zero all the other bits in the BMCR
  484. * and reapply all driver-specific and board-specific fixups.
  485. */
  486. static int phy_poll_reset(struct phy_device *phydev)
  487. {
  488. /* Poll until the reset bit clears (50ms per retry == 0.6 sec) */
  489. unsigned int retries = 12;
  490. int ret;
  491. do {
  492. msleep(50);
  493. ret = phy_read(phydev, MII_BMCR);
  494. if (ret < 0)
  495. return ret;
  496. } while (ret & BMCR_RESET && --retries);
  497. if (ret & BMCR_RESET)
  498. return -ETIMEDOUT;
  499. /* Some chips (smsc911x) may still need up to another 1ms after the
  500. * BMCR_RESET bit is cleared before they are usable.
  501. */
  502. msleep(1);
  503. return 0;
  504. }
  505. int phy_init_hw(struct phy_device *phydev)
  506. {
  507. int ret = 0;
  508. if (!phydev->drv || !phydev->drv->config_init)
  509. return 0;
  510. if (phydev->drv->soft_reset)
  511. ret = phydev->drv->soft_reset(phydev);
  512. else
  513. ret = genphy_soft_reset(phydev);
  514. if (ret < 0)
  515. return ret;
  516. ret = phy_scan_fixups(phydev);
  517. if (ret < 0)
  518. return ret;
  519. return phydev->drv->config_init(phydev);
  520. }
  521. EXPORT_SYMBOL(phy_init_hw);
  522. /**
  523. * phy_attach_direct - attach a network device to a given PHY device pointer
  524. * @dev: network device to attach
  525. * @phydev: Pointer to phy_device to attach
  526. * @flags: PHY device's dev_flags
  527. * @interface: PHY device's interface
  528. *
  529. * Description: Called by drivers to attach to a particular PHY
  530. * device. The phy_device is found, and properly hooked up
  531. * to the phy_driver. If no driver is attached, then a
  532. * generic driver is used. The phy_device is given a ptr to
  533. * the attaching device, and given a callback for link status
  534. * change. The phy_device is returned to the attaching driver.
  535. * This function takes a reference on the phy device.
  536. */
  537. int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
  538. u32 flags, phy_interface_t interface)
  539. {
  540. struct mii_bus *bus = phydev->bus;
  541. struct device *d = &phydev->dev;
  542. int err;
  543. if (!try_module_get(bus->owner)) {
  544. dev_err(&dev->dev, "failed to get the bus module\n");
  545. return -EIO;
  546. }
  547. get_device(d);
  548. /* Assume that if there is no driver, that it doesn't
  549. * exist, and we should use the genphy driver.
  550. */
  551. if (!d->driver) {
  552. if (phydev->is_c45)
  553. d->driver = &genphy_driver[GENPHY_DRV_10G].driver;
  554. else
  555. d->driver = &genphy_driver[GENPHY_DRV_1G].driver;
  556. err = d->driver->probe(d);
  557. if (err >= 0)
  558. err = device_bind_driver(d);
  559. if (err)
  560. goto error;
  561. }
  562. if (phydev->attached_dev) {
  563. dev_err(&dev->dev, "PHY already attached\n");
  564. err = -EBUSY;
  565. goto error;
  566. }
  567. phydev->attached_dev = dev;
  568. dev->phydev = phydev;
  569. phydev->dev_flags = flags;
  570. phydev->interface = interface;
  571. phydev->state = PHY_READY;
  572. /* Do initial configuration here, now that
  573. * we have certain key parameters
  574. * (dev_flags and interface)
  575. */
  576. err = phy_init_hw(phydev);
  577. if (err)
  578. phy_detach(phydev);
  579. else
  580. phy_resume(phydev);
  581. return err;
  582. error:
  583. put_device(d);
  584. module_put(bus->owner);
  585. return err;
  586. }
  587. EXPORT_SYMBOL(phy_attach_direct);
  588. /**
  589. * phy_attach - attach a network device to a particular PHY device
  590. * @dev: network device to attach
  591. * @bus_id: Bus ID of PHY device to attach
  592. * @interface: PHY device's interface
  593. *
  594. * Description: Same as phy_attach_direct() except that a PHY bus_id
  595. * string is passed instead of a pointer to a struct phy_device.
  596. */
  597. struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
  598. phy_interface_t interface)
  599. {
  600. struct bus_type *bus = &mdio_bus_type;
  601. struct phy_device *phydev;
  602. struct device *d;
  603. int rc;
  604. /* Search the list of PHY devices on the mdio bus for the
  605. * PHY with the requested name
  606. */
  607. d = bus_find_device_by_name(bus, NULL, bus_id);
  608. if (!d) {
  609. pr_err("PHY %s not found\n", bus_id);
  610. return ERR_PTR(-ENODEV);
  611. }
  612. phydev = to_phy_device(d);
  613. rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface);
  614. if (rc)
  615. return ERR_PTR(rc);
  616. return phydev;
  617. }
  618. EXPORT_SYMBOL(phy_attach);
  619. /**
  620. * phy_detach - detach a PHY device from its network device
  621. * @phydev: target phy_device struct
  622. *
  623. * This detaches the phy device from its network device and the phy
  624. * driver, and drops the reference count taken in phy_attach_direct().
  625. */
  626. void phy_detach(struct phy_device *phydev)
  627. {
  628. struct mii_bus *bus;
  629. int i;
  630. phydev->attached_dev->phydev = NULL;
  631. phydev->attached_dev = NULL;
  632. phy_suspend(phydev);
  633. /* If the device had no specific driver before (i.e. - it
  634. * was using the generic driver), we unbind the device
  635. * from the generic driver so that there's a chance a
  636. * real driver could be loaded
  637. */
  638. for (i = 0; i < ARRAY_SIZE(genphy_driver); i++) {
  639. if (phydev->dev.driver == &genphy_driver[i].driver) {
  640. device_release_driver(&phydev->dev);
  641. break;
  642. }
  643. }
  644. /*
  645. * The phydev might go away on the put_device() below, so avoid
  646. * a use-after-free bug by reading the underlying bus first.
  647. */
  648. bus = phydev->bus;
  649. put_device(&phydev->dev);
  650. module_put(bus->owner);
  651. }
  652. EXPORT_SYMBOL(phy_detach);
  653. int phy_suspend(struct phy_device *phydev)
  654. {
  655. struct phy_driver *phydrv = to_phy_driver(phydev->dev.driver);
  656. struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
  657. int ret = 0;
  658. /* If the device has WOL enabled, we cannot suspend the PHY */
  659. phy_ethtool_get_wol(phydev, &wol);
  660. if (wol.wolopts)
  661. return -EBUSY;
  662. if (phydrv->suspend)
  663. ret = phydrv->suspend(phydev);
  664. if (ret)
  665. return ret;
  666. phydev->suspended = true;
  667. return ret;
  668. }
  669. EXPORT_SYMBOL(phy_suspend);
  670. int phy_resume(struct phy_device *phydev)
  671. {
  672. struct phy_driver *phydrv = to_phy_driver(phydev->dev.driver);
  673. int ret = 0;
  674. if (phydrv->resume)
  675. ret = phydrv->resume(phydev);
  676. if (ret)
  677. return ret;
  678. phydev->suspended = false;
  679. return ret;
  680. }
  681. EXPORT_SYMBOL(phy_resume);
  682. /* Generic PHY support and helper functions */
  683. /**
  684. * genphy_config_advert - sanitize and advertise auto-negotiation parameters
  685. * @phydev: target phy_device struct
  686. *
  687. * Description: Writes MII_ADVERTISE with the appropriate values,
  688. * after sanitizing the values to make sure we only advertise
  689. * what is supported. Returns < 0 on error, 0 if the PHY's advertisement
  690. * hasn't changed, and > 0 if it has changed.
  691. */
  692. static int genphy_config_advert(struct phy_device *phydev)
  693. {
  694. u32 advertise;
  695. int oldadv, adv, bmsr;
  696. int err, changed = 0;
  697. /* Only allow advertising what this PHY supports */
  698. phydev->advertising &= phydev->supported;
  699. advertise = phydev->advertising;
  700. /* Setup standard advertisement */
  701. adv = phy_read(phydev, MII_ADVERTISE);
  702. if (adv < 0)
  703. return adv;
  704. oldadv = adv;
  705. adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
  706. ADVERTISE_PAUSE_ASYM);
  707. adv |= ethtool_adv_to_mii_adv_t(advertise);
  708. if (adv != oldadv) {
  709. err = phy_write(phydev, MII_ADVERTISE, adv);
  710. if (err < 0)
  711. return err;
  712. changed = 1;
  713. }
  714. bmsr = phy_read(phydev, MII_BMSR);
  715. if (bmsr < 0)
  716. return bmsr;
  717. /* Per 802.3-2008, Section 22.2.4.2.16 Extended status all
  718. * 1000Mbits/sec capable PHYs shall have the BMSR_ESTATEN bit set to a
  719. * logical 1.
  720. */
  721. if (!(bmsr & BMSR_ESTATEN))
  722. return changed;
  723. /* Configure gigabit if it's supported */
  724. adv = phy_read(phydev, MII_CTRL1000);
  725. if (adv < 0)
  726. return adv;
  727. oldadv = adv;
  728. adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
  729. if (phydev->supported & (SUPPORTED_1000baseT_Half |
  730. SUPPORTED_1000baseT_Full)) {
  731. adv |= ethtool_adv_to_mii_ctrl1000_t(advertise);
  732. }
  733. if (adv != oldadv)
  734. changed = 1;
  735. err = phy_write(phydev, MII_CTRL1000, adv);
  736. if (err < 0)
  737. return err;
  738. return changed;
  739. }
  740. /**
  741. * genphy_setup_forced - configures/forces speed/duplex from @phydev
  742. * @phydev: target phy_device struct
  743. *
  744. * Description: Configures MII_BMCR to force speed/duplex
  745. * to the values in phydev. Assumes that the values are valid.
  746. * Please see phy_sanitize_settings().
  747. */
  748. int genphy_setup_forced(struct phy_device *phydev)
  749. {
  750. int ctl = 0;
  751. phydev->pause = 0;
  752. phydev->asym_pause = 0;
  753. if (SPEED_1000 == phydev->speed)
  754. ctl |= BMCR_SPEED1000;
  755. else if (SPEED_100 == phydev->speed)
  756. ctl |= BMCR_SPEED100;
  757. if (DUPLEX_FULL == phydev->duplex)
  758. ctl |= BMCR_FULLDPLX;
  759. return phy_write(phydev, MII_BMCR, ctl);
  760. }
  761. EXPORT_SYMBOL(genphy_setup_forced);
  762. /**
  763. * genphy_restart_aneg - Enable and Restart Autonegotiation
  764. * @phydev: target phy_device struct
  765. */
  766. int genphy_restart_aneg(struct phy_device *phydev)
  767. {
  768. int ctl = phy_read(phydev, MII_BMCR);
  769. if (ctl < 0)
  770. return ctl;
  771. ctl |= BMCR_ANENABLE | BMCR_ANRESTART;
  772. /* Don't isolate the PHY if we're negotiating */
  773. ctl &= ~BMCR_ISOLATE;
  774. return phy_write(phydev, MII_BMCR, ctl);
  775. }
  776. EXPORT_SYMBOL(genphy_restart_aneg);
  777. /**
  778. * genphy_config_aneg - restart auto-negotiation or write BMCR
  779. * @phydev: target phy_device struct
  780. *
  781. * Description: If auto-negotiation is enabled, we configure the
  782. * advertising, and then restart auto-negotiation. If it is not
  783. * enabled, then we write the BMCR.
  784. */
  785. int genphy_config_aneg(struct phy_device *phydev)
  786. {
  787. int result;
  788. if (AUTONEG_ENABLE != phydev->autoneg)
  789. return genphy_setup_forced(phydev);
  790. result = genphy_config_advert(phydev);
  791. if (result < 0) /* error */
  792. return result;
  793. if (result == 0) {
  794. /* Advertisement hasn't changed, but maybe aneg was never on to
  795. * begin with? Or maybe phy was isolated?
  796. */
  797. int ctl = phy_read(phydev, MII_BMCR);
  798. if (ctl < 0)
  799. return ctl;
  800. if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
  801. result = 1; /* do restart aneg */
  802. }
  803. /* Only restart aneg if we are advertising something different
  804. * than we were before.
  805. */
  806. if (result > 0)
  807. result = genphy_restart_aneg(phydev);
  808. return result;
  809. }
  810. EXPORT_SYMBOL(genphy_config_aneg);
  811. /**
  812. * genphy_aneg_done - return auto-negotiation status
  813. * @phydev: target phy_device struct
  814. *
  815. * Description: Reads the status register and returns 0 either if
  816. * auto-negotiation is incomplete, or if there was an error.
  817. * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
  818. */
  819. int genphy_aneg_done(struct phy_device *phydev)
  820. {
  821. int retval = phy_read(phydev, MII_BMSR);
  822. return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
  823. }
  824. EXPORT_SYMBOL(genphy_aneg_done);
  825. static int gen10g_config_aneg(struct phy_device *phydev)
  826. {
  827. return 0;
  828. }
  829. /**
  830. * genphy_update_link - update link status in @phydev
  831. * @phydev: target phy_device struct
  832. *
  833. * Description: Update the value in phydev->link to reflect the
  834. * current link value. In order to do this, we need to read
  835. * the status register twice, keeping the second value.
  836. */
  837. int genphy_update_link(struct phy_device *phydev)
  838. {
  839. int status;
  840. /* Do a fake read */
  841. status = phy_read(phydev, MII_BMSR);
  842. if (status < 0)
  843. return status;
  844. /* Read link and autonegotiation status */
  845. status = phy_read(phydev, MII_BMSR);
  846. if (status < 0)
  847. return status;
  848. if ((status & BMSR_LSTATUS) == 0)
  849. phydev->link = 0;
  850. else
  851. phydev->link = 1;
  852. return 0;
  853. }
  854. EXPORT_SYMBOL(genphy_update_link);
  855. /**
  856. * genphy_read_status - check the link status and update current link state
  857. * @phydev: target phy_device struct
  858. *
  859. * Description: Check the link, then figure out the current state
  860. * by comparing what we advertise with what the link partner
  861. * advertises. Start by checking the gigabit possibilities,
  862. * then move on to 10/100.
  863. */
  864. int genphy_read_status(struct phy_device *phydev)
  865. {
  866. int adv;
  867. int err;
  868. int lpa;
  869. int lpagb = 0;
  870. int common_adv;
  871. int common_adv_gb = 0;
  872. /* Update the link, but return if there was an error */
  873. err = genphy_update_link(phydev);
  874. if (err)
  875. return err;
  876. phydev->lp_advertising = 0;
  877. if (AUTONEG_ENABLE == phydev->autoneg) {
  878. if (phydev->supported & (SUPPORTED_1000baseT_Half
  879. | SUPPORTED_1000baseT_Full)) {
  880. lpagb = phy_read(phydev, MII_STAT1000);
  881. if (lpagb < 0)
  882. return lpagb;
  883. adv = phy_read(phydev, MII_CTRL1000);
  884. if (adv < 0)
  885. return adv;
  886. phydev->lp_advertising =
  887. mii_stat1000_to_ethtool_lpa_t(lpagb);
  888. common_adv_gb = lpagb & adv << 2;
  889. }
  890. lpa = phy_read(phydev, MII_LPA);
  891. if (lpa < 0)
  892. return lpa;
  893. phydev->lp_advertising |= mii_lpa_to_ethtool_lpa_t(lpa);
  894. adv = phy_read(phydev, MII_ADVERTISE);
  895. if (adv < 0)
  896. return adv;
  897. common_adv = lpa & adv;
  898. phydev->speed = SPEED_10;
  899. phydev->duplex = DUPLEX_HALF;
  900. phydev->pause = 0;
  901. phydev->asym_pause = 0;
  902. if (common_adv_gb & (LPA_1000FULL | LPA_1000HALF)) {
  903. phydev->speed = SPEED_1000;
  904. if (common_adv_gb & LPA_1000FULL)
  905. phydev->duplex = DUPLEX_FULL;
  906. } else if (common_adv & (LPA_100FULL | LPA_100HALF)) {
  907. phydev->speed = SPEED_100;
  908. if (common_adv & LPA_100FULL)
  909. phydev->duplex = DUPLEX_FULL;
  910. } else
  911. if (common_adv & LPA_10FULL)
  912. phydev->duplex = DUPLEX_FULL;
  913. if (phydev->duplex == DUPLEX_FULL) {
  914. phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
  915. phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
  916. }
  917. } else {
  918. int bmcr = phy_read(phydev, MII_BMCR);
  919. if (bmcr < 0)
  920. return bmcr;
  921. if (bmcr & BMCR_FULLDPLX)
  922. phydev->duplex = DUPLEX_FULL;
  923. else
  924. phydev->duplex = DUPLEX_HALF;
  925. if (bmcr & BMCR_SPEED1000)
  926. phydev->speed = SPEED_1000;
  927. else if (bmcr & BMCR_SPEED100)
  928. phydev->speed = SPEED_100;
  929. else
  930. phydev->speed = SPEED_10;
  931. phydev->pause = 0;
  932. phydev->asym_pause = 0;
  933. }
  934. return 0;
  935. }
  936. EXPORT_SYMBOL(genphy_read_status);
  937. static int gen10g_read_status(struct phy_device *phydev)
  938. {
  939. int devad, reg;
  940. u32 mmd_mask = phydev->c45_ids.devices_in_package;
  941. phydev->link = 1;
  942. /* For now just lie and say it's 10G all the time */
  943. phydev->speed = SPEED_10000;
  944. phydev->duplex = DUPLEX_FULL;
  945. for (devad = 0; mmd_mask; devad++, mmd_mask = mmd_mask >> 1) {
  946. if (!(mmd_mask & 1))
  947. continue;
  948. /* Read twice because link state is latched and a
  949. * read moves the current state into the register
  950. */
  951. phy_read_mmd(phydev, devad, MDIO_STAT1);
  952. reg = phy_read_mmd(phydev, devad, MDIO_STAT1);
  953. if (reg < 0 || !(reg & MDIO_STAT1_LSTATUS))
  954. phydev->link = 0;
  955. }
  956. return 0;
  957. }
  958. /**
  959. * genphy_soft_reset - software reset the PHY via BMCR_RESET bit
  960. * @phydev: target phy_device struct
  961. *
  962. * Description: Perform a software PHY reset using the standard
  963. * BMCR_RESET bit and poll for the reset bit to be cleared.
  964. *
  965. * Returns: 0 on success, < 0 on failure
  966. */
  967. int genphy_soft_reset(struct phy_device *phydev)
  968. {
  969. int ret;
  970. ret = phy_write(phydev, MII_BMCR, BMCR_RESET);
  971. if (ret < 0)
  972. return ret;
  973. return phy_poll_reset(phydev);
  974. }
  975. EXPORT_SYMBOL(genphy_soft_reset);
  976. int genphy_config_init(struct phy_device *phydev)
  977. {
  978. int val;
  979. u32 features;
  980. features = (SUPPORTED_TP | SUPPORTED_MII
  981. | SUPPORTED_AUI | SUPPORTED_FIBRE |
  982. SUPPORTED_BNC);
  983. /* Do we support autonegotiation? */
  984. val = phy_read(phydev, MII_BMSR);
  985. if (val < 0)
  986. return val;
  987. if (val & BMSR_ANEGCAPABLE)
  988. features |= SUPPORTED_Autoneg;
  989. if (val & BMSR_100FULL)
  990. features |= SUPPORTED_100baseT_Full;
  991. if (val & BMSR_100HALF)
  992. features |= SUPPORTED_100baseT_Half;
  993. if (val & BMSR_10FULL)
  994. features |= SUPPORTED_10baseT_Full;
  995. if (val & BMSR_10HALF)
  996. features |= SUPPORTED_10baseT_Half;
  997. if (val & BMSR_ESTATEN) {
  998. val = phy_read(phydev, MII_ESTATUS);
  999. if (val < 0)
  1000. return val;
  1001. if (val & ESTATUS_1000_TFULL)
  1002. features |= SUPPORTED_1000baseT_Full;
  1003. if (val & ESTATUS_1000_THALF)
  1004. features |= SUPPORTED_1000baseT_Half;
  1005. }
  1006. phydev->supported &= features;
  1007. phydev->advertising &= features;
  1008. return 0;
  1009. }
  1010. static int gen10g_soft_reset(struct phy_device *phydev)
  1011. {
  1012. /* Do nothing for now */
  1013. return 0;
  1014. }
  1015. EXPORT_SYMBOL(genphy_config_init);
  1016. static int gen10g_config_init(struct phy_device *phydev)
  1017. {
  1018. /* Temporarily just say we support everything */
  1019. phydev->supported = SUPPORTED_10000baseT_Full;
  1020. phydev->advertising = SUPPORTED_10000baseT_Full;
  1021. return 0;
  1022. }
  1023. int genphy_suspend(struct phy_device *phydev)
  1024. {
  1025. int value;
  1026. mutex_lock(&phydev->lock);
  1027. value = phy_read(phydev, MII_BMCR);
  1028. phy_write(phydev, MII_BMCR, value | BMCR_PDOWN);
  1029. mutex_unlock(&phydev->lock);
  1030. return 0;
  1031. }
  1032. EXPORT_SYMBOL(genphy_suspend);
  1033. static int gen10g_suspend(struct phy_device *phydev)
  1034. {
  1035. return 0;
  1036. }
  1037. int genphy_resume(struct phy_device *phydev)
  1038. {
  1039. int value;
  1040. mutex_lock(&phydev->lock);
  1041. value = phy_read(phydev, MII_BMCR);
  1042. phy_write(phydev, MII_BMCR, value & ~BMCR_PDOWN);
  1043. mutex_unlock(&phydev->lock);
  1044. return 0;
  1045. }
  1046. EXPORT_SYMBOL(genphy_resume);
  1047. static int gen10g_resume(struct phy_device *phydev)
  1048. {
  1049. return 0;
  1050. }
  1051. static int __set_phy_supported(struct phy_device *phydev, u32 max_speed)
  1052. {
  1053. switch (max_speed) {
  1054. case SPEED_10:
  1055. phydev->supported &= ~PHY_100BT_FEATURES;
  1056. /* fall through */
  1057. case SPEED_100:
  1058. phydev->supported &= ~PHY_1000BT_FEATURES;
  1059. break;
  1060. case SPEED_1000:
  1061. break;
  1062. default:
  1063. return -ENOTSUPP;
  1064. }
  1065. return 0;
  1066. }
  1067. int phy_set_max_speed(struct phy_device *phydev, u32 max_speed)
  1068. {
  1069. int err;
  1070. err = __set_phy_supported(phydev, max_speed);
  1071. if (err)
  1072. return err;
  1073. phydev->advertising = phydev->supported;
  1074. return 0;
  1075. }
  1076. EXPORT_SYMBOL(phy_set_max_speed);
  1077. static void of_set_phy_supported(struct phy_device *phydev)
  1078. {
  1079. struct device_node *node = phydev->dev.of_node;
  1080. u32 max_speed;
  1081. if (!IS_ENABLED(CONFIG_OF_MDIO))
  1082. return;
  1083. if (!node)
  1084. return;
  1085. if (!of_property_read_u32(node, "max-speed", &max_speed))
  1086. __set_phy_supported(phydev, max_speed);
  1087. }
  1088. /**
  1089. * phy_probe - probe and init a PHY device
  1090. * @dev: device to probe and init
  1091. *
  1092. * Description: Take care of setting up the phy_device structure,
  1093. * set the state to READY (the driver's init function should
  1094. * set it to STARTING if needed).
  1095. */
  1096. static int phy_probe(struct device *dev)
  1097. {
  1098. struct phy_device *phydev = to_phy_device(dev);
  1099. struct device_driver *drv = phydev->dev.driver;
  1100. struct phy_driver *phydrv = to_phy_driver(drv);
  1101. int err = 0;
  1102. phydev->drv = phydrv;
  1103. /* Disable the interrupt if the PHY doesn't support it
  1104. * but the interrupt is still a valid one
  1105. */
  1106. if (!(phydrv->flags & PHY_HAS_INTERRUPT) &&
  1107. phy_interrupt_is_valid(phydev))
  1108. phydev->irq = PHY_POLL;
  1109. if (phydrv->flags & PHY_IS_INTERNAL)
  1110. phydev->is_internal = true;
  1111. mutex_lock(&phydev->lock);
  1112. /* Start out supporting everything. Eventually,
  1113. * a controller will attach, and may modify one
  1114. * or both of these values
  1115. */
  1116. phydev->supported = phydrv->features;
  1117. of_set_phy_supported(phydev);
  1118. phydev->advertising = phydev->supported;
  1119. /* Set the state to READY by default */
  1120. phydev->state = PHY_READY;
  1121. if (phydev->drv->probe)
  1122. err = phydev->drv->probe(phydev);
  1123. mutex_unlock(&phydev->lock);
  1124. return err;
  1125. }
  1126. static int phy_remove(struct device *dev)
  1127. {
  1128. struct phy_device *phydev = to_phy_device(dev);
  1129. cancel_delayed_work_sync(&phydev->state_queue);
  1130. mutex_lock(&phydev->lock);
  1131. phydev->state = PHY_DOWN;
  1132. mutex_unlock(&phydev->lock);
  1133. if (phydev->drv->remove)
  1134. phydev->drv->remove(phydev);
  1135. phydev->drv = NULL;
  1136. return 0;
  1137. }
  1138. /**
  1139. * phy_driver_register - register a phy_driver with the PHY layer
  1140. * @new_driver: new phy_driver to register
  1141. */
  1142. int phy_driver_register(struct phy_driver *new_driver)
  1143. {
  1144. int retval;
  1145. new_driver->driver.name = new_driver->name;
  1146. new_driver->driver.bus = &mdio_bus_type;
  1147. new_driver->driver.probe = phy_probe;
  1148. new_driver->driver.remove = phy_remove;
  1149. retval = driver_register(&new_driver->driver);
  1150. if (retval) {
  1151. pr_err("%s: Error %d in registering driver\n",
  1152. new_driver->name, retval);
  1153. return retval;
  1154. }
  1155. pr_debug("%s: Registered new driver\n", new_driver->name);
  1156. return 0;
  1157. }
  1158. EXPORT_SYMBOL(phy_driver_register);
  1159. int phy_drivers_register(struct phy_driver *new_driver, int n)
  1160. {
  1161. int i, ret = 0;
  1162. for (i = 0; i < n; i++) {
  1163. ret = phy_driver_register(new_driver + i);
  1164. if (ret) {
  1165. while (i-- > 0)
  1166. phy_driver_unregister(new_driver + i);
  1167. break;
  1168. }
  1169. }
  1170. return ret;
  1171. }
  1172. EXPORT_SYMBOL(phy_drivers_register);
  1173. void phy_driver_unregister(struct phy_driver *drv)
  1174. {
  1175. driver_unregister(&drv->driver);
  1176. }
  1177. EXPORT_SYMBOL(phy_driver_unregister);
  1178. void phy_drivers_unregister(struct phy_driver *drv, int n)
  1179. {
  1180. int i;
  1181. for (i = 0; i < n; i++)
  1182. phy_driver_unregister(drv + i);
  1183. }
  1184. EXPORT_SYMBOL(phy_drivers_unregister);
  1185. static struct phy_driver genphy_driver[] = {
  1186. {
  1187. .phy_id = 0xffffffff,
  1188. .phy_id_mask = 0xffffffff,
  1189. .name = "Generic PHY",
  1190. .soft_reset = genphy_no_soft_reset,
  1191. .config_init = genphy_config_init,
  1192. .features = PHY_GBIT_FEATURES | SUPPORTED_MII |
  1193. SUPPORTED_AUI | SUPPORTED_FIBRE |
  1194. SUPPORTED_BNC,
  1195. .config_aneg = genphy_config_aneg,
  1196. .aneg_done = genphy_aneg_done,
  1197. .read_status = genphy_read_status,
  1198. .suspend = genphy_suspend,
  1199. .resume = genphy_resume,
  1200. .driver = { .owner = THIS_MODULE, },
  1201. }, {
  1202. .phy_id = 0xffffffff,
  1203. .phy_id_mask = 0xffffffff,
  1204. .name = "Generic 10G PHY",
  1205. .soft_reset = gen10g_soft_reset,
  1206. .config_init = gen10g_config_init,
  1207. .features = 0,
  1208. .config_aneg = gen10g_config_aneg,
  1209. .read_status = gen10g_read_status,
  1210. .suspend = gen10g_suspend,
  1211. .resume = gen10g_resume,
  1212. .driver = {.owner = THIS_MODULE, },
  1213. } };
  1214. static int __init phy_init(void)
  1215. {
  1216. int rc;
  1217. rc = mdio_bus_init();
  1218. if (rc)
  1219. return rc;
  1220. rc = phy_drivers_register(genphy_driver,
  1221. ARRAY_SIZE(genphy_driver));
  1222. if (rc)
  1223. mdio_bus_exit();
  1224. return rc;
  1225. }
  1226. static void __exit phy_exit(void)
  1227. {
  1228. phy_drivers_unregister(genphy_driver,
  1229. ARRAY_SIZE(genphy_driver));
  1230. mdio_bus_exit();
  1231. }
  1232. subsys_initcall(phy_init);
  1233. module_exit(phy_exit);