mdio_bus.c 16 KB

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