phy_device.c 35 KB

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