phy_device.c 34 KB

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