mdio_bus.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  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/of_device.h>
  24. #include <linux/of_mdio.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/etherdevice.h>
  27. #include <linux/skbuff.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/mm.h>
  30. #include <linux/module.h>
  31. #include <linux/mii.h>
  32. #include <linux/ethtool.h>
  33. #include <linux/phy.h>
  34. #include <linux/io.h>
  35. #include <linux/uaccess.h>
  36. #include <asm/irq.h>
  37. #define CREATE_TRACE_POINTS
  38. #include <trace/events/mdio.h>
  39. #include "mdio-boardinfo.h"
  40. int mdiobus_register_device(struct mdio_device *mdiodev)
  41. {
  42. if (mdiodev->bus->mdio_map[mdiodev->addr])
  43. return -EBUSY;
  44. mdiodev->bus->mdio_map[mdiodev->addr] = mdiodev;
  45. return 0;
  46. }
  47. EXPORT_SYMBOL(mdiobus_register_device);
  48. int mdiobus_unregister_device(struct mdio_device *mdiodev)
  49. {
  50. if (mdiodev->bus->mdio_map[mdiodev->addr] != mdiodev)
  51. return -EINVAL;
  52. mdiodev->bus->mdio_map[mdiodev->addr] = NULL;
  53. return 0;
  54. }
  55. EXPORT_SYMBOL(mdiobus_unregister_device);
  56. struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr)
  57. {
  58. struct mdio_device *mdiodev = bus->mdio_map[addr];
  59. if (!mdiodev)
  60. return NULL;
  61. if (!(mdiodev->flags & MDIO_DEVICE_FLAG_PHY))
  62. return NULL;
  63. return container_of(mdiodev, struct phy_device, mdio);
  64. }
  65. EXPORT_SYMBOL(mdiobus_get_phy);
  66. bool mdiobus_is_registered_device(struct mii_bus *bus, int addr)
  67. {
  68. return bus->mdio_map[addr];
  69. }
  70. EXPORT_SYMBOL(mdiobus_is_registered_device);
  71. /**
  72. * mdiobus_alloc_size - allocate a mii_bus structure
  73. * @size: extra amount of memory to allocate for private storage.
  74. * If non-zero, then bus->priv is points to that memory.
  75. *
  76. * Description: called by a bus driver to allocate an mii_bus
  77. * structure to fill in.
  78. */
  79. struct mii_bus *mdiobus_alloc_size(size_t size)
  80. {
  81. struct mii_bus *bus;
  82. size_t aligned_size = ALIGN(sizeof(*bus), NETDEV_ALIGN);
  83. size_t alloc_size;
  84. int i;
  85. /* If we alloc extra space, it should be aligned */
  86. if (size)
  87. alloc_size = aligned_size + size;
  88. else
  89. alloc_size = sizeof(*bus);
  90. bus = kzalloc(alloc_size, GFP_KERNEL);
  91. if (!bus)
  92. return NULL;
  93. bus->state = MDIOBUS_ALLOCATED;
  94. if (size)
  95. bus->priv = (void *)bus + aligned_size;
  96. /* Initialise the interrupts to polling */
  97. for (i = 0; i < PHY_MAX_ADDR; i++)
  98. bus->irq[i] = PHY_POLL;
  99. return bus;
  100. }
  101. EXPORT_SYMBOL(mdiobus_alloc_size);
  102. static void _devm_mdiobus_free(struct device *dev, void *res)
  103. {
  104. mdiobus_free(*(struct mii_bus **)res);
  105. }
  106. static int devm_mdiobus_match(struct device *dev, void *res, void *data)
  107. {
  108. struct mii_bus **r = res;
  109. if (WARN_ON(!r || !*r))
  110. return 0;
  111. return *r == data;
  112. }
  113. /**
  114. * devm_mdiobus_alloc_size - Resource-managed mdiobus_alloc_size()
  115. * @dev: Device to allocate mii_bus for
  116. * @sizeof_priv: Space to allocate for private structure.
  117. *
  118. * Managed mdiobus_alloc_size. mii_bus allocated with this function is
  119. * automatically freed on driver detach.
  120. *
  121. * If an mii_bus allocated with this function needs to be freed separately,
  122. * devm_mdiobus_free() must be used.
  123. *
  124. * RETURNS:
  125. * Pointer to allocated mii_bus on success, NULL on failure.
  126. */
  127. struct mii_bus *devm_mdiobus_alloc_size(struct device *dev, int sizeof_priv)
  128. {
  129. struct mii_bus **ptr, *bus;
  130. ptr = devres_alloc(_devm_mdiobus_free, sizeof(*ptr), GFP_KERNEL);
  131. if (!ptr)
  132. return NULL;
  133. /* use raw alloc_dr for kmalloc caller tracing */
  134. bus = mdiobus_alloc_size(sizeof_priv);
  135. if (bus) {
  136. *ptr = bus;
  137. devres_add(dev, ptr);
  138. } else {
  139. devres_free(ptr);
  140. }
  141. return bus;
  142. }
  143. EXPORT_SYMBOL_GPL(devm_mdiobus_alloc_size);
  144. /**
  145. * devm_mdiobus_free - Resource-managed mdiobus_free()
  146. * @dev: Device this mii_bus belongs to
  147. * @bus: the mii_bus associated with the device
  148. *
  149. * Free mii_bus allocated with devm_mdiobus_alloc_size().
  150. */
  151. void devm_mdiobus_free(struct device *dev, struct mii_bus *bus)
  152. {
  153. int rc;
  154. rc = devres_release(dev, _devm_mdiobus_free,
  155. devm_mdiobus_match, bus);
  156. WARN_ON(rc);
  157. }
  158. EXPORT_SYMBOL_GPL(devm_mdiobus_free);
  159. /**
  160. * mdiobus_release - mii_bus device release callback
  161. * @d: the target struct device that contains the mii_bus
  162. *
  163. * Description: called when the last reference to an mii_bus is
  164. * dropped, to free the underlying memory.
  165. */
  166. static void mdiobus_release(struct device *d)
  167. {
  168. struct mii_bus *bus = to_mii_bus(d);
  169. BUG_ON(bus->state != MDIOBUS_RELEASED &&
  170. /* for compatibility with error handling in drivers */
  171. bus->state != MDIOBUS_ALLOCATED);
  172. kfree(bus);
  173. }
  174. static struct class mdio_bus_class = {
  175. .name = "mdio_bus",
  176. .dev_release = mdiobus_release,
  177. };
  178. #if IS_ENABLED(CONFIG_OF_MDIO)
  179. /* Helper function for of_mdio_find_bus */
  180. static int of_mdio_bus_match(struct device *dev, const void *mdio_bus_np)
  181. {
  182. return dev->of_node == mdio_bus_np;
  183. }
  184. /**
  185. * of_mdio_find_bus - Given an mii_bus node, find the mii_bus.
  186. * @mdio_bus_np: Pointer to the mii_bus.
  187. *
  188. * Returns a reference to the mii_bus, or NULL if none found. The
  189. * embedded struct device will have its reference count incremented,
  190. * and this must be put once the bus is finished with.
  191. *
  192. * Because the association of a device_node and mii_bus is made via
  193. * of_mdiobus_register(), the mii_bus cannot be found before it is
  194. * registered with of_mdiobus_register().
  195. *
  196. */
  197. struct mii_bus *of_mdio_find_bus(struct device_node *mdio_bus_np)
  198. {
  199. struct device *d;
  200. if (!mdio_bus_np)
  201. return NULL;
  202. d = class_find_device(&mdio_bus_class, NULL, mdio_bus_np,
  203. of_mdio_bus_match);
  204. return d ? to_mii_bus(d) : NULL;
  205. }
  206. EXPORT_SYMBOL(of_mdio_find_bus);
  207. /* Walk the list of subnodes of a mdio bus and look for a node that
  208. * matches the mdio device's address with its 'reg' property. If
  209. * found, set the of_node pointer for the mdio device. This allows
  210. * auto-probed phy devices to be supplied with information passed in
  211. * via DT.
  212. */
  213. static void of_mdiobus_link_mdiodev(struct mii_bus *bus,
  214. struct mdio_device *mdiodev)
  215. {
  216. struct device *dev = &mdiodev->dev;
  217. struct device_node *child;
  218. if (dev->of_node || !bus->dev.of_node)
  219. return;
  220. for_each_available_child_of_node(bus->dev.of_node, child) {
  221. int addr;
  222. int ret;
  223. ret = of_property_read_u32(child, "reg", &addr);
  224. if (ret < 0) {
  225. dev_err(dev, "%s has invalid MDIO address\n",
  226. child->full_name);
  227. continue;
  228. }
  229. /* A MDIO device must have a reg property in the range [0-31] */
  230. if (addr >= PHY_MAX_ADDR) {
  231. dev_err(dev, "%s MDIO address %i is too large\n",
  232. child->full_name, addr);
  233. continue;
  234. }
  235. if (addr == mdiodev->addr) {
  236. dev->of_node = child;
  237. return;
  238. }
  239. }
  240. }
  241. #else /* !IS_ENABLED(CONFIG_OF_MDIO) */
  242. static inline void of_mdiobus_link_mdiodev(struct mii_bus *mdio,
  243. struct mdio_device *mdiodev)
  244. {
  245. }
  246. #endif
  247. /**
  248. * __mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
  249. * @bus: target mii_bus
  250. * @owner: module containing bus accessor functions
  251. *
  252. * Description: Called by a bus driver to bring up all the PHYs
  253. * on a given bus, and attach them to the bus. Drivers should use
  254. * mdiobus_register() rather than __mdiobus_register() unless they
  255. * need to pass a specific owner module. MDIO devices which are not
  256. * PHYs will not be brought up by this function. They are expected to
  257. * to be explicitly listed in DT and instantiated by of_mdiobus_register().
  258. *
  259. * Returns 0 on success or < 0 on error.
  260. */
  261. int __mdiobus_register(struct mii_bus *bus, struct module *owner)
  262. {
  263. struct mdio_device *mdiodev;
  264. int i, err;
  265. if (NULL == bus || NULL == bus->name ||
  266. NULL == bus->read || NULL == bus->write)
  267. return -EINVAL;
  268. BUG_ON(bus->state != MDIOBUS_ALLOCATED &&
  269. bus->state != MDIOBUS_UNREGISTERED);
  270. bus->owner = owner;
  271. bus->dev.parent = bus->parent;
  272. bus->dev.class = &mdio_bus_class;
  273. bus->dev.groups = NULL;
  274. dev_set_name(&bus->dev, "%s", bus->id);
  275. err = device_register(&bus->dev);
  276. if (err) {
  277. pr_err("mii_bus %s failed to register\n", bus->id);
  278. put_device(&bus->dev);
  279. return -EINVAL;
  280. }
  281. mutex_init(&bus->mdio_lock);
  282. if (bus->reset)
  283. bus->reset(bus);
  284. for (i = 0; i < PHY_MAX_ADDR; i++) {
  285. if ((bus->phy_mask & (1 << i)) == 0) {
  286. struct phy_device *phydev;
  287. phydev = mdiobus_scan(bus, i);
  288. if (IS_ERR(phydev) && (PTR_ERR(phydev) != -ENODEV)) {
  289. err = PTR_ERR(phydev);
  290. goto error;
  291. }
  292. }
  293. }
  294. mdiobus_setup_mdiodev_from_board_info(bus);
  295. bus->state = MDIOBUS_REGISTERED;
  296. pr_info("%s: probed\n", bus->name);
  297. return 0;
  298. error:
  299. while (--i >= 0) {
  300. mdiodev = bus->mdio_map[i];
  301. if (!mdiodev)
  302. continue;
  303. mdiodev->device_remove(mdiodev);
  304. mdiodev->device_free(mdiodev);
  305. }
  306. device_del(&bus->dev);
  307. return err;
  308. }
  309. EXPORT_SYMBOL(__mdiobus_register);
  310. void mdiobus_unregister(struct mii_bus *bus)
  311. {
  312. struct mdio_device *mdiodev;
  313. int i;
  314. BUG_ON(bus->state != MDIOBUS_REGISTERED);
  315. bus->state = MDIOBUS_UNREGISTERED;
  316. for (i = 0; i < PHY_MAX_ADDR; i++) {
  317. mdiodev = bus->mdio_map[i];
  318. if (!mdiodev)
  319. continue;
  320. mdiodev->device_remove(mdiodev);
  321. mdiodev->device_free(mdiodev);
  322. }
  323. device_del(&bus->dev);
  324. }
  325. EXPORT_SYMBOL(mdiobus_unregister);
  326. /**
  327. * mdiobus_free - free a struct mii_bus
  328. * @bus: mii_bus to free
  329. *
  330. * This function releases the reference to the underlying device
  331. * object in the mii_bus. If this is the last reference, the mii_bus
  332. * will be freed.
  333. */
  334. void mdiobus_free(struct mii_bus *bus)
  335. {
  336. /* For compatibility with error handling in drivers. */
  337. if (bus->state == MDIOBUS_ALLOCATED) {
  338. kfree(bus);
  339. return;
  340. }
  341. BUG_ON(bus->state != MDIOBUS_UNREGISTERED);
  342. bus->state = MDIOBUS_RELEASED;
  343. put_device(&bus->dev);
  344. }
  345. EXPORT_SYMBOL(mdiobus_free);
  346. /**
  347. * mdiobus_scan - scan a bus for MDIO devices.
  348. * @bus: mii_bus to scan
  349. * @addr: address on bus to scan
  350. *
  351. * This function scans the MDIO bus, looking for devices which can be
  352. * identified using a vendor/product ID in registers 2 and 3. Not all
  353. * MDIO devices have such registers, but PHY devices typically
  354. * do. Hence this function assumes anything found is a PHY, or can be
  355. * treated as a PHY. Other MDIO devices, such as switches, will
  356. * probably not be found during the scan.
  357. */
  358. struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
  359. {
  360. struct phy_device *phydev;
  361. int err;
  362. phydev = get_phy_device(bus, addr, false);
  363. if (IS_ERR(phydev))
  364. return phydev;
  365. /*
  366. * For DT, see if the auto-probed phy has a correspoding child
  367. * in the bus node, and set the of_node pointer in this case.
  368. */
  369. of_mdiobus_link_mdiodev(bus, &phydev->mdio);
  370. err = phy_device_register(phydev);
  371. if (err) {
  372. phy_device_free(phydev);
  373. return ERR_PTR(-ENODEV);
  374. }
  375. return phydev;
  376. }
  377. EXPORT_SYMBOL(mdiobus_scan);
  378. /**
  379. * mdiobus_read_nested - Nested version of the mdiobus_read function
  380. * @bus: the mii_bus struct
  381. * @addr: the phy address
  382. * @regnum: register number to read
  383. *
  384. * In case of nested MDIO bus access avoid lockdep false positives by
  385. * using mutex_lock_nested().
  386. *
  387. * NOTE: MUST NOT be called from interrupt context,
  388. * because the bus read/write functions may wait for an interrupt
  389. * to conclude the operation.
  390. */
  391. int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum)
  392. {
  393. int retval;
  394. BUG_ON(in_interrupt());
  395. mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
  396. retval = bus->read(bus, addr, regnum);
  397. mutex_unlock(&bus->mdio_lock);
  398. trace_mdio_access(bus, 1, addr, regnum, retval, retval);
  399. return retval;
  400. }
  401. EXPORT_SYMBOL(mdiobus_read_nested);
  402. /**
  403. * mdiobus_read - Convenience function for reading a given MII mgmt register
  404. * @bus: the mii_bus struct
  405. * @addr: the phy address
  406. * @regnum: register number to read
  407. *
  408. * NOTE: MUST NOT be called from interrupt context,
  409. * because the bus read/write functions may wait for an interrupt
  410. * to conclude the operation.
  411. */
  412. int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
  413. {
  414. int retval;
  415. BUG_ON(in_interrupt());
  416. mutex_lock(&bus->mdio_lock);
  417. retval = bus->read(bus, addr, regnum);
  418. mutex_unlock(&bus->mdio_lock);
  419. trace_mdio_access(bus, 1, addr, regnum, retval, retval);
  420. return retval;
  421. }
  422. EXPORT_SYMBOL(mdiobus_read);
  423. /**
  424. * mdiobus_write_nested - Nested version of the mdiobus_write function
  425. * @bus: the mii_bus struct
  426. * @addr: the phy address
  427. * @regnum: register number to write
  428. * @val: value to write to @regnum
  429. *
  430. * In case of nested MDIO bus access avoid lockdep false positives by
  431. * using mutex_lock_nested().
  432. *
  433. * NOTE: MUST NOT be called from interrupt context,
  434. * because the bus read/write functions may wait for an interrupt
  435. * to conclude the operation.
  436. */
  437. int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val)
  438. {
  439. int err;
  440. BUG_ON(in_interrupt());
  441. mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
  442. err = bus->write(bus, addr, regnum, val);
  443. mutex_unlock(&bus->mdio_lock);
  444. trace_mdio_access(bus, 0, addr, regnum, val, err);
  445. return err;
  446. }
  447. EXPORT_SYMBOL(mdiobus_write_nested);
  448. /**
  449. * mdiobus_write - Convenience function for writing a given MII mgmt register
  450. * @bus: the mii_bus struct
  451. * @addr: the phy address
  452. * @regnum: register number to write
  453. * @val: value to write to @regnum
  454. *
  455. * NOTE: MUST NOT be called from interrupt context,
  456. * because the bus read/write functions may wait for an interrupt
  457. * to conclude the operation.
  458. */
  459. int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val)
  460. {
  461. int err;
  462. BUG_ON(in_interrupt());
  463. mutex_lock(&bus->mdio_lock);
  464. err = bus->write(bus, addr, regnum, val);
  465. mutex_unlock(&bus->mdio_lock);
  466. trace_mdio_access(bus, 0, addr, regnum, val, err);
  467. return err;
  468. }
  469. EXPORT_SYMBOL(mdiobus_write);
  470. /**
  471. * mdio_bus_match - determine if given MDIO driver supports the given
  472. * MDIO device
  473. * @dev: target MDIO device
  474. * @drv: given MDIO driver
  475. *
  476. * Description: Given a MDIO device, and a MDIO driver, return 1 if
  477. * the driver supports the device. Otherwise, return 0. This may
  478. * require calling the devices own match function, since different classes
  479. * of MDIO devices have different match criteria.
  480. */
  481. static int mdio_bus_match(struct device *dev, struct device_driver *drv)
  482. {
  483. struct mdio_device *mdio = to_mdio_device(dev);
  484. if (of_driver_match_device(dev, drv))
  485. return 1;
  486. if (mdio->bus_match)
  487. return mdio->bus_match(dev, drv);
  488. return 0;
  489. }
  490. #ifdef CONFIG_PM
  491. static int mdio_bus_suspend(struct device *dev)
  492. {
  493. struct mdio_device *mdio = to_mdio_device(dev);
  494. if (mdio->pm_ops && mdio->pm_ops->suspend)
  495. return mdio->pm_ops->suspend(dev);
  496. return 0;
  497. }
  498. static int mdio_bus_resume(struct device *dev)
  499. {
  500. struct mdio_device *mdio = to_mdio_device(dev);
  501. if (mdio->pm_ops && mdio->pm_ops->resume)
  502. return mdio->pm_ops->resume(dev);
  503. return 0;
  504. }
  505. static int mdio_bus_restore(struct device *dev)
  506. {
  507. struct mdio_device *mdio = to_mdio_device(dev);
  508. if (mdio->pm_ops && mdio->pm_ops->restore)
  509. return mdio->pm_ops->restore(dev);
  510. return 0;
  511. }
  512. static const struct dev_pm_ops mdio_bus_pm_ops = {
  513. .suspend = mdio_bus_suspend,
  514. .resume = mdio_bus_resume,
  515. .freeze = mdio_bus_suspend,
  516. .thaw = mdio_bus_resume,
  517. .restore = mdio_bus_restore,
  518. };
  519. #define MDIO_BUS_PM_OPS (&mdio_bus_pm_ops)
  520. #else
  521. #define MDIO_BUS_PM_OPS NULL
  522. #endif /* CONFIG_PM */
  523. struct bus_type mdio_bus_type = {
  524. .name = "mdio_bus",
  525. .match = mdio_bus_match,
  526. .pm = MDIO_BUS_PM_OPS,
  527. };
  528. EXPORT_SYMBOL(mdio_bus_type);
  529. int __init mdio_bus_init(void)
  530. {
  531. int ret;
  532. ret = class_register(&mdio_bus_class);
  533. if (!ret) {
  534. ret = bus_register(&mdio_bus_type);
  535. if (ret)
  536. class_unregister(&mdio_bus_class);
  537. }
  538. return ret;
  539. }
  540. void mdio_bus_exit(void)
  541. {
  542. class_unregister(&mdio_bus_class);
  543. bus_unregister(&mdio_bus_type);
  544. }