mdio_bus.c 18 KB

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