mdio_bus.c 18 KB

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