mdio_bus.c 15 KB

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