mdio_bus.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. /* MDIO Bus interface
  2. *
  3. * Author: Andy Fleming
  4. *
  5. * Copyright (c) 2004 Freescale Semiconductor, Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. *
  12. */
  13. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  14. #include <linux/kernel.h>
  15. #include <linux/string.h>
  16. #include <linux/errno.h>
  17. #include <linux/unistd.h>
  18. #include <linux/slab.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/init.h>
  21. #include <linux/delay.h>
  22. #include <linux/device.h>
  23. #include <linux/gpio.h>
  24. #include <linux/gpio/consumer.h>
  25. #include <linux/of_device.h>
  26. #include <linux/of_mdio.h>
  27. #include <linux/of_gpio.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/etherdevice.h>
  30. #include <linux/skbuff.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/mm.h>
  33. #include <linux/module.h>
  34. #include <linux/mii.h>
  35. #include <linux/ethtool.h>
  36. #include <linux/phy.h>
  37. #include <linux/io.h>
  38. #include <linux/uaccess.h>
  39. #include <linux/gpio/consumer.h>
  40. #include <asm/irq.h>
  41. #define CREATE_TRACE_POINTS
  42. #include <trace/events/mdio.h>
  43. #include "mdio-boardinfo.h"
  44. static int mdiobus_register_gpiod(struct mdio_device *mdiodev)
  45. {
  46. struct gpio_desc *gpiod = NULL;
  47. /* Deassert the optional reset signal */
  48. if (mdiodev->dev.of_node)
  49. gpiod = fwnode_get_named_gpiod(&mdiodev->dev.of_node->fwnode,
  50. "reset-gpios", 0, GPIOD_OUT_LOW,
  51. "PHY reset");
  52. if (PTR_ERR(gpiod) == -ENOENT ||
  53. PTR_ERR(gpiod) == -ENOSYS)
  54. gpiod = NULL;
  55. else if (IS_ERR(gpiod))
  56. return PTR_ERR(gpiod);
  57. mdiodev->reset = gpiod;
  58. /* Assert the reset signal again */
  59. mdio_device_reset(mdiodev, 1);
  60. return 0;
  61. }
  62. int mdiobus_register_device(struct mdio_device *mdiodev)
  63. {
  64. int err;
  65. if (mdiodev->bus->mdio_map[mdiodev->addr])
  66. return -EBUSY;
  67. if (mdiodev->flags & MDIO_DEVICE_FLAG_PHY) {
  68. err = mdiobus_register_gpiod(mdiodev);
  69. if (err)
  70. return err;
  71. }
  72. mdiodev->bus->mdio_map[mdiodev->addr] = mdiodev;
  73. return 0;
  74. }
  75. EXPORT_SYMBOL(mdiobus_register_device);
  76. int mdiobus_unregister_device(struct mdio_device *mdiodev)
  77. {
  78. if (mdiodev->bus->mdio_map[mdiodev->addr] != mdiodev)
  79. return -EINVAL;
  80. mdiodev->bus->mdio_map[mdiodev->addr] = NULL;
  81. return 0;
  82. }
  83. EXPORT_SYMBOL(mdiobus_unregister_device);
  84. struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr)
  85. {
  86. struct mdio_device *mdiodev = bus->mdio_map[addr];
  87. if (!mdiodev)
  88. return NULL;
  89. if (!(mdiodev->flags & MDIO_DEVICE_FLAG_PHY))
  90. return NULL;
  91. return container_of(mdiodev, struct phy_device, mdio);
  92. }
  93. EXPORT_SYMBOL(mdiobus_get_phy);
  94. bool mdiobus_is_registered_device(struct mii_bus *bus, int addr)
  95. {
  96. return bus->mdio_map[addr];
  97. }
  98. EXPORT_SYMBOL(mdiobus_is_registered_device);
  99. /**
  100. * mdiobus_alloc_size - allocate a mii_bus structure
  101. * @size: extra amount of memory to allocate for private storage.
  102. * If non-zero, then bus->priv is points to that memory.
  103. *
  104. * Description: called by a bus driver to allocate an mii_bus
  105. * structure to fill in.
  106. */
  107. struct mii_bus *mdiobus_alloc_size(size_t size)
  108. {
  109. struct mii_bus *bus;
  110. size_t aligned_size = ALIGN(sizeof(*bus), NETDEV_ALIGN);
  111. size_t alloc_size;
  112. int i;
  113. /* If we alloc extra space, it should be aligned */
  114. if (size)
  115. alloc_size = aligned_size + size;
  116. else
  117. alloc_size = sizeof(*bus);
  118. bus = kzalloc(alloc_size, GFP_KERNEL);
  119. if (!bus)
  120. return NULL;
  121. bus->state = MDIOBUS_ALLOCATED;
  122. if (size)
  123. bus->priv = (void *)bus + aligned_size;
  124. /* Initialise the interrupts to polling */
  125. for (i = 0; i < PHY_MAX_ADDR; i++)
  126. bus->irq[i] = PHY_POLL;
  127. return bus;
  128. }
  129. EXPORT_SYMBOL(mdiobus_alloc_size);
  130. static void _devm_mdiobus_free(struct device *dev, void *res)
  131. {
  132. mdiobus_free(*(struct mii_bus **)res);
  133. }
  134. static int devm_mdiobus_match(struct device *dev, void *res, void *data)
  135. {
  136. struct mii_bus **r = res;
  137. if (WARN_ON(!r || !*r))
  138. return 0;
  139. return *r == data;
  140. }
  141. /**
  142. * devm_mdiobus_alloc_size - Resource-managed mdiobus_alloc_size()
  143. * @dev: Device to allocate mii_bus for
  144. * @sizeof_priv: Space to allocate for private structure.
  145. *
  146. * Managed mdiobus_alloc_size. mii_bus allocated with this function is
  147. * automatically freed on driver detach.
  148. *
  149. * If an mii_bus allocated with this function needs to be freed separately,
  150. * devm_mdiobus_free() must be used.
  151. *
  152. * RETURNS:
  153. * Pointer to allocated mii_bus on success, NULL on failure.
  154. */
  155. struct mii_bus *devm_mdiobus_alloc_size(struct device *dev, int sizeof_priv)
  156. {
  157. struct mii_bus **ptr, *bus;
  158. ptr = devres_alloc(_devm_mdiobus_free, sizeof(*ptr), GFP_KERNEL);
  159. if (!ptr)
  160. return NULL;
  161. /* use raw alloc_dr for kmalloc caller tracing */
  162. bus = mdiobus_alloc_size(sizeof_priv);
  163. if (bus) {
  164. *ptr = bus;
  165. devres_add(dev, ptr);
  166. } else {
  167. devres_free(ptr);
  168. }
  169. return bus;
  170. }
  171. EXPORT_SYMBOL_GPL(devm_mdiobus_alloc_size);
  172. /**
  173. * devm_mdiobus_free - Resource-managed mdiobus_free()
  174. * @dev: Device this mii_bus belongs to
  175. * @bus: the mii_bus associated with the device
  176. *
  177. * Free mii_bus allocated with devm_mdiobus_alloc_size().
  178. */
  179. void devm_mdiobus_free(struct device *dev, struct mii_bus *bus)
  180. {
  181. int rc;
  182. rc = devres_release(dev, _devm_mdiobus_free,
  183. devm_mdiobus_match, bus);
  184. WARN_ON(rc);
  185. }
  186. EXPORT_SYMBOL_GPL(devm_mdiobus_free);
  187. /**
  188. * mdiobus_release - mii_bus device release callback
  189. * @d: the target struct device that contains the mii_bus
  190. *
  191. * Description: called when the last reference to an mii_bus is
  192. * dropped, to free the underlying memory.
  193. */
  194. static void mdiobus_release(struct device *d)
  195. {
  196. struct mii_bus *bus = to_mii_bus(d);
  197. BUG_ON(bus->state != MDIOBUS_RELEASED &&
  198. /* for compatibility with error handling in drivers */
  199. bus->state != MDIOBUS_ALLOCATED);
  200. kfree(bus);
  201. }
  202. static struct class mdio_bus_class = {
  203. .name = "mdio_bus",
  204. .dev_release = mdiobus_release,
  205. };
  206. #if IS_ENABLED(CONFIG_OF_MDIO)
  207. /* Helper function for of_mdio_find_bus */
  208. static int of_mdio_bus_match(struct device *dev, const void *mdio_bus_np)
  209. {
  210. return dev->of_node == mdio_bus_np;
  211. }
  212. /**
  213. * of_mdio_find_bus - Given an mii_bus node, find the mii_bus.
  214. * @mdio_bus_np: Pointer to the mii_bus.
  215. *
  216. * Returns a reference to the mii_bus, or NULL if none found. The
  217. * embedded struct device will have its reference count incremented,
  218. * and this must be put once the bus is finished with.
  219. *
  220. * Because the association of a device_node and mii_bus is made via
  221. * of_mdiobus_register(), the mii_bus cannot be found before it is
  222. * registered with of_mdiobus_register().
  223. *
  224. */
  225. struct mii_bus *of_mdio_find_bus(struct device_node *mdio_bus_np)
  226. {
  227. struct device *d;
  228. if (!mdio_bus_np)
  229. return NULL;
  230. d = class_find_device(&mdio_bus_class, NULL, mdio_bus_np,
  231. of_mdio_bus_match);
  232. return d ? to_mii_bus(d) : NULL;
  233. }
  234. EXPORT_SYMBOL(of_mdio_find_bus);
  235. /* Walk the list of subnodes of a mdio bus and look for a node that
  236. * matches the mdio device's address with its 'reg' property. If
  237. * found, set the of_node pointer for the mdio device. This allows
  238. * auto-probed phy devices to be supplied with information passed in
  239. * via DT.
  240. */
  241. static void of_mdiobus_link_mdiodev(struct mii_bus *bus,
  242. struct mdio_device *mdiodev)
  243. {
  244. struct device *dev = &mdiodev->dev;
  245. struct device_node *child;
  246. if (dev->of_node || !bus->dev.of_node)
  247. return;
  248. for_each_available_child_of_node(bus->dev.of_node, child) {
  249. int addr;
  250. addr = of_mdio_parse_addr(dev, child);
  251. if (addr < 0)
  252. continue;
  253. if (addr == mdiodev->addr) {
  254. dev->of_node = child;
  255. dev->fwnode = of_fwnode_handle(child);
  256. return;
  257. }
  258. }
  259. }
  260. #else /* !IS_ENABLED(CONFIG_OF_MDIO) */
  261. static inline void of_mdiobus_link_mdiodev(struct mii_bus *mdio,
  262. struct mdio_device *mdiodev)
  263. {
  264. }
  265. #endif
  266. /**
  267. * mdiobus_create_device_from_board_info - create a full MDIO device given
  268. * a mdio_board_info structure
  269. * @bus: MDIO bus to create the devices on
  270. * @bi: mdio_board_info structure describing the devices
  271. *
  272. * Returns 0 on success or < 0 on error.
  273. */
  274. static int mdiobus_create_device(struct mii_bus *bus,
  275. struct mdio_board_info *bi)
  276. {
  277. struct mdio_device *mdiodev;
  278. int ret = 0;
  279. mdiodev = mdio_device_create(bus, bi->mdio_addr);
  280. if (IS_ERR(mdiodev))
  281. return -ENODEV;
  282. strncpy(mdiodev->modalias, bi->modalias,
  283. sizeof(mdiodev->modalias));
  284. mdiodev->bus_match = mdio_device_bus_match;
  285. mdiodev->dev.platform_data = (void *)bi->platform_data;
  286. ret = mdio_device_register(mdiodev);
  287. if (ret)
  288. mdio_device_free(mdiodev);
  289. return ret;
  290. }
  291. /**
  292. * __mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
  293. * @bus: target mii_bus
  294. * @owner: module containing bus accessor functions
  295. *
  296. * Description: Called by a bus driver to bring up all the PHYs
  297. * on a given bus, and attach them to the bus. Drivers should use
  298. * mdiobus_register() rather than __mdiobus_register() unless they
  299. * need to pass a specific owner module. MDIO devices which are not
  300. * PHYs will not be brought up by this function. They are expected to
  301. * to be explicitly listed in DT and instantiated by of_mdiobus_register().
  302. *
  303. * Returns 0 on success or < 0 on error.
  304. */
  305. int __mdiobus_register(struct mii_bus *bus, struct module *owner)
  306. {
  307. struct mdio_device *mdiodev;
  308. int i, err;
  309. struct gpio_desc *gpiod;
  310. if (NULL == bus || NULL == bus->name ||
  311. NULL == bus->read || NULL == bus->write)
  312. return -EINVAL;
  313. BUG_ON(bus->state != MDIOBUS_ALLOCATED &&
  314. bus->state != MDIOBUS_UNREGISTERED);
  315. bus->owner = owner;
  316. bus->dev.parent = bus->parent;
  317. bus->dev.class = &mdio_bus_class;
  318. bus->dev.groups = NULL;
  319. dev_set_name(&bus->dev, "%s", bus->id);
  320. err = device_register(&bus->dev);
  321. if (err) {
  322. pr_err("mii_bus %s failed to register\n", bus->id);
  323. return -EINVAL;
  324. }
  325. mutex_init(&bus->mdio_lock);
  326. /* de-assert bus level PHY GPIO reset */
  327. for (i = 0; i < PHY_MAX_ADDR; i++) {
  328. gpiod = devm_gpiod_get_index_optional(&bus->dev, "reset",
  329. i, GPIOD_OUT_LOW);
  330. if (IS_ERR(gpiod)) {
  331. dev_err(&bus->dev, "mii_bus %s couldn't get reset GPIO\n",
  332. bus->id);
  333. return PTR_ERR(gpiod);
  334. } else if (gpiod) {
  335. bus->reset_gpiod[i] = gpiod;
  336. gpiod_set_value_cansleep(gpiod, 1);
  337. udelay(bus->reset_delay_us);
  338. gpiod_set_value_cansleep(gpiod, 0);
  339. } else {
  340. break;
  341. }
  342. }
  343. bus->num_resets = i;
  344. if (bus->reset)
  345. bus->reset(bus);
  346. for (i = 0; i < PHY_MAX_ADDR; i++) {
  347. if ((bus->phy_mask & (1 << i)) == 0) {
  348. struct phy_device *phydev;
  349. phydev = mdiobus_scan(bus, i);
  350. if (IS_ERR(phydev) && (PTR_ERR(phydev) != -ENODEV)) {
  351. err = PTR_ERR(phydev);
  352. goto error;
  353. }
  354. }
  355. }
  356. mdiobus_setup_mdiodev_from_board_info(bus, mdiobus_create_device);
  357. bus->state = MDIOBUS_REGISTERED;
  358. pr_info("%s: probed\n", bus->name);
  359. return 0;
  360. error:
  361. while (--i >= 0) {
  362. mdiodev = bus->mdio_map[i];
  363. if (!mdiodev)
  364. continue;
  365. mdiodev->device_remove(mdiodev);
  366. mdiodev->device_free(mdiodev);
  367. }
  368. /* Put PHYs in RESET to save power */
  369. for (i = 0; i < bus->num_resets; i++) {
  370. if (bus->reset_gpiod[i])
  371. gpiod_set_value_cansleep(bus->reset_gpiod[i], 1);
  372. }
  373. device_del(&bus->dev);
  374. return err;
  375. }
  376. EXPORT_SYMBOL(__mdiobus_register);
  377. void mdiobus_unregister(struct mii_bus *bus)
  378. {
  379. struct mdio_device *mdiodev;
  380. int i;
  381. BUG_ON(bus->state != MDIOBUS_REGISTERED);
  382. bus->state = MDIOBUS_UNREGISTERED;
  383. for (i = 0; i < PHY_MAX_ADDR; i++) {
  384. mdiodev = bus->mdio_map[i];
  385. if (!mdiodev)
  386. continue;
  387. if (mdiodev->reset)
  388. gpiod_put(mdiodev->reset);
  389. mdiodev->device_remove(mdiodev);
  390. mdiodev->device_free(mdiodev);
  391. }
  392. /* Put PHYs in RESET to save power */
  393. for (i = 0; i < bus->num_resets; i++) {
  394. if (bus->reset_gpiod[i])
  395. gpiod_set_value_cansleep(bus->reset_gpiod[i], 1);
  396. }
  397. device_del(&bus->dev);
  398. }
  399. EXPORT_SYMBOL(mdiobus_unregister);
  400. /**
  401. * mdiobus_free - free a struct mii_bus
  402. * @bus: mii_bus to free
  403. *
  404. * This function releases the reference to the underlying device
  405. * object in the mii_bus. If this is the last reference, the mii_bus
  406. * will be freed.
  407. */
  408. void mdiobus_free(struct mii_bus *bus)
  409. {
  410. /* For compatibility with error handling in drivers. */
  411. if (bus->state == MDIOBUS_ALLOCATED) {
  412. kfree(bus);
  413. return;
  414. }
  415. BUG_ON(bus->state != MDIOBUS_UNREGISTERED);
  416. bus->state = MDIOBUS_RELEASED;
  417. put_device(&bus->dev);
  418. }
  419. EXPORT_SYMBOL(mdiobus_free);
  420. /**
  421. * mdiobus_scan - scan a bus for MDIO devices.
  422. * @bus: mii_bus to scan
  423. * @addr: address on bus to scan
  424. *
  425. * This function scans the MDIO bus, looking for devices which can be
  426. * identified using a vendor/product ID in registers 2 and 3. Not all
  427. * MDIO devices have such registers, but PHY devices typically
  428. * do. Hence this function assumes anything found is a PHY, or can be
  429. * treated as a PHY. Other MDIO devices, such as switches, will
  430. * probably not be found during the scan.
  431. */
  432. struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
  433. {
  434. struct phy_device *phydev;
  435. int err;
  436. phydev = get_phy_device(bus, addr, false);
  437. if (IS_ERR(phydev))
  438. return phydev;
  439. /*
  440. * For DT, see if the auto-probed phy has a correspoding child
  441. * in the bus node, and set the of_node pointer in this case.
  442. */
  443. of_mdiobus_link_mdiodev(bus, &phydev->mdio);
  444. err = phy_device_register(phydev);
  445. if (err) {
  446. phy_device_free(phydev);
  447. return ERR_PTR(-ENODEV);
  448. }
  449. return phydev;
  450. }
  451. EXPORT_SYMBOL(mdiobus_scan);
  452. /**
  453. * __mdiobus_read - Unlocked version of the mdiobus_read function
  454. * @bus: the mii_bus struct
  455. * @addr: the phy address
  456. * @regnum: register number to read
  457. *
  458. * Read a MDIO bus register. Caller must hold the mdio bus lock.
  459. *
  460. * NOTE: MUST NOT be called from interrupt context.
  461. */
  462. int __mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
  463. {
  464. int retval;
  465. WARN_ON_ONCE(!mutex_is_locked(&bus->mdio_lock));
  466. retval = bus->read(bus, addr, regnum);
  467. trace_mdio_access(bus, 1, addr, regnum, retval, retval);
  468. return retval;
  469. }
  470. EXPORT_SYMBOL(__mdiobus_read);
  471. /**
  472. * __mdiobus_write - Unlocked version of the mdiobus_write function
  473. * @bus: the mii_bus struct
  474. * @addr: the phy address
  475. * @regnum: register number to write
  476. * @val: value to write to @regnum
  477. *
  478. * Write a MDIO bus register. Caller must hold the mdio bus lock.
  479. *
  480. * NOTE: MUST NOT be called from interrupt context.
  481. */
  482. int __mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val)
  483. {
  484. int err;
  485. WARN_ON_ONCE(!mutex_is_locked(&bus->mdio_lock));
  486. err = bus->write(bus, addr, regnum, val);
  487. trace_mdio_access(bus, 0, addr, regnum, val, err);
  488. return err;
  489. }
  490. EXPORT_SYMBOL(__mdiobus_write);
  491. /**
  492. * mdiobus_read_nested - Nested version of the mdiobus_read function
  493. * @bus: the mii_bus struct
  494. * @addr: the phy address
  495. * @regnum: register number to read
  496. *
  497. * In case of nested MDIO bus access avoid lockdep false positives by
  498. * using mutex_lock_nested().
  499. *
  500. * NOTE: MUST NOT be called from interrupt context,
  501. * because the bus read/write functions may wait for an interrupt
  502. * to conclude the operation.
  503. */
  504. int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum)
  505. {
  506. int retval;
  507. BUG_ON(in_interrupt());
  508. mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
  509. retval = __mdiobus_read(bus, addr, regnum);
  510. mutex_unlock(&bus->mdio_lock);
  511. return retval;
  512. }
  513. EXPORT_SYMBOL(mdiobus_read_nested);
  514. /**
  515. * mdiobus_read - Convenience function for reading a given MII mgmt register
  516. * @bus: the mii_bus struct
  517. * @addr: the phy address
  518. * @regnum: register number to read
  519. *
  520. * NOTE: MUST NOT be called from interrupt context,
  521. * because the bus read/write functions may wait for an interrupt
  522. * to conclude the operation.
  523. */
  524. int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
  525. {
  526. int retval;
  527. BUG_ON(in_interrupt());
  528. mutex_lock(&bus->mdio_lock);
  529. retval = __mdiobus_read(bus, addr, regnum);
  530. mutex_unlock(&bus->mdio_lock);
  531. return retval;
  532. }
  533. EXPORT_SYMBOL(mdiobus_read);
  534. /**
  535. * mdiobus_write_nested - Nested version of the mdiobus_write function
  536. * @bus: the mii_bus struct
  537. * @addr: the phy address
  538. * @regnum: register number to write
  539. * @val: value to write to @regnum
  540. *
  541. * In case of nested MDIO bus access avoid lockdep false positives by
  542. * using mutex_lock_nested().
  543. *
  544. * NOTE: MUST NOT be called from interrupt context,
  545. * because the bus read/write functions may wait for an interrupt
  546. * to conclude the operation.
  547. */
  548. int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val)
  549. {
  550. int err;
  551. BUG_ON(in_interrupt());
  552. mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
  553. err = __mdiobus_write(bus, addr, regnum, val);
  554. mutex_unlock(&bus->mdio_lock);
  555. return err;
  556. }
  557. EXPORT_SYMBOL(mdiobus_write_nested);
  558. /**
  559. * mdiobus_write - Convenience function for writing a given MII mgmt register
  560. * @bus: the mii_bus struct
  561. * @addr: the phy address
  562. * @regnum: register number to write
  563. * @val: value to write to @regnum
  564. *
  565. * NOTE: MUST NOT be called from interrupt context,
  566. * because the bus read/write functions may wait for an interrupt
  567. * to conclude the operation.
  568. */
  569. int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val)
  570. {
  571. int err;
  572. BUG_ON(in_interrupt());
  573. mutex_lock(&bus->mdio_lock);
  574. err = __mdiobus_write(bus, addr, regnum, val);
  575. mutex_unlock(&bus->mdio_lock);
  576. return err;
  577. }
  578. EXPORT_SYMBOL(mdiobus_write);
  579. /**
  580. * mdio_bus_match - determine if given MDIO driver supports the given
  581. * MDIO device
  582. * @dev: target MDIO device
  583. * @drv: given MDIO driver
  584. *
  585. * Description: Given a MDIO device, and a MDIO driver, return 1 if
  586. * the driver supports the device. Otherwise, return 0. This may
  587. * require calling the devices own match function, since different classes
  588. * of MDIO devices have different match criteria.
  589. */
  590. static int mdio_bus_match(struct device *dev, struct device_driver *drv)
  591. {
  592. struct mdio_device *mdio = to_mdio_device(dev);
  593. if (of_driver_match_device(dev, drv))
  594. return 1;
  595. if (mdio->bus_match)
  596. return mdio->bus_match(dev, drv);
  597. return 0;
  598. }
  599. static int mdio_uevent(struct device *dev, struct kobj_uevent_env *env)
  600. {
  601. int rc;
  602. /* Some devices have extra OF data and an OF-style MODALIAS */
  603. rc = of_device_uevent_modalias(dev, env);
  604. if (rc != -ENODEV)
  605. return rc;
  606. return 0;
  607. }
  608. struct bus_type mdio_bus_type = {
  609. .name = "mdio_bus",
  610. .match = mdio_bus_match,
  611. .uevent = mdio_uevent,
  612. };
  613. EXPORT_SYMBOL(mdio_bus_type);
  614. int __init mdio_bus_init(void)
  615. {
  616. int ret;
  617. ret = class_register(&mdio_bus_class);
  618. if (!ret) {
  619. ret = bus_register(&mdio_bus_type);
  620. if (ret)
  621. class_unregister(&mdio_bus_class);
  622. }
  623. return ret;
  624. }
  625. EXPORT_SYMBOL_GPL(mdio_bus_init);
  626. #if IS_ENABLED(CONFIG_PHYLIB)
  627. void mdio_bus_exit(void)
  628. {
  629. class_unregister(&mdio_bus_class);
  630. bus_unregister(&mdio_bus_type);
  631. }
  632. EXPORT_SYMBOL_GPL(mdio_bus_exit);
  633. #else
  634. module_init(mdio_bus_init);
  635. /* no module_exit, intentional */
  636. MODULE_LICENSE("GPL");
  637. MODULE_DESCRIPTION("MDIO bus/device layer");
  638. #endif