mdio_bus.c 15 KB

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