mdio_bus.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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 pointer to the mii_bus, or NULL if none found.
  151. *
  152. * Because the association of a device_node and mii_bus is made via
  153. * of_mdiobus_register(), the mii_bus cannot be found before it is
  154. * registered with of_mdiobus_register().
  155. *
  156. */
  157. struct mii_bus *of_mdio_find_bus(struct device_node *mdio_bus_np)
  158. {
  159. struct device *d;
  160. if (!mdio_bus_np)
  161. return NULL;
  162. d = class_find_device(&mdio_bus_class, NULL, mdio_bus_np,
  163. of_mdio_bus_match);
  164. return d ? to_mii_bus(d) : NULL;
  165. }
  166. EXPORT_SYMBOL(of_mdio_find_bus);
  167. /* Walk the list of subnodes of a mdio bus and look for a node that matches the
  168. * phy's address with its 'reg' property. If found, set the of_node pointer for
  169. * the phy. This allows auto-probed pyh devices to be supplied with information
  170. * passed in via DT.
  171. */
  172. static void of_mdiobus_link_phydev(struct mii_bus *mdio,
  173. struct phy_device *phydev)
  174. {
  175. struct device *dev = &phydev->dev;
  176. struct device_node *child;
  177. if (dev->of_node || !mdio->dev.of_node)
  178. return;
  179. for_each_available_child_of_node(mdio->dev.of_node, child) {
  180. int addr;
  181. int ret;
  182. ret = of_property_read_u32(child, "reg", &addr);
  183. if (ret < 0) {
  184. dev_err(dev, "%s has invalid PHY address\n",
  185. child->full_name);
  186. continue;
  187. }
  188. /* A PHY must have a reg property in the range [0-31] */
  189. if (addr >= PHY_MAX_ADDR) {
  190. dev_err(dev, "%s PHY address %i is too large\n",
  191. child->full_name, addr);
  192. continue;
  193. }
  194. if (addr == phydev->addr) {
  195. dev->of_node = child;
  196. return;
  197. }
  198. }
  199. }
  200. #else /* !IS_ENABLED(CONFIG_OF_MDIO) */
  201. static inline void of_mdiobus_link_phydev(struct mii_bus *mdio,
  202. struct phy_device *phydev)
  203. {
  204. }
  205. #endif
  206. /**
  207. * mdiobus_register - bring up all the PHYs on a given bus and attach them to bus
  208. * @bus: target mii_bus
  209. *
  210. * Description: Called by a bus driver to bring up all the PHYs
  211. * on a given bus, and attach them to the bus.
  212. *
  213. * Returns 0 on success or < 0 on error.
  214. */
  215. int mdiobus_register(struct mii_bus *bus)
  216. {
  217. int i, err;
  218. if (NULL == bus || NULL == bus->name ||
  219. NULL == bus->read || NULL == bus->write)
  220. return -EINVAL;
  221. BUG_ON(bus->state != MDIOBUS_ALLOCATED &&
  222. bus->state != MDIOBUS_UNREGISTERED);
  223. bus->dev.parent = bus->parent;
  224. bus->dev.class = &mdio_bus_class;
  225. bus->dev.groups = NULL;
  226. dev_set_name(&bus->dev, "%s", bus->id);
  227. err = device_register(&bus->dev);
  228. if (err) {
  229. pr_err("mii_bus %s failed to register\n", bus->id);
  230. put_device(&bus->dev);
  231. return -EINVAL;
  232. }
  233. mutex_init(&bus->mdio_lock);
  234. if (bus->reset)
  235. bus->reset(bus);
  236. for (i = 0; i < PHY_MAX_ADDR; i++) {
  237. if ((bus->phy_mask & (1 << i)) == 0) {
  238. struct phy_device *phydev;
  239. phydev = mdiobus_scan(bus, i);
  240. if (IS_ERR(phydev)) {
  241. err = PTR_ERR(phydev);
  242. goto error;
  243. }
  244. }
  245. }
  246. bus->state = MDIOBUS_REGISTERED;
  247. pr_info("%s: probed\n", bus->name);
  248. return 0;
  249. error:
  250. while (--i >= 0) {
  251. if (bus->phy_map[i])
  252. device_unregister(&bus->phy_map[i]->dev);
  253. }
  254. device_del(&bus->dev);
  255. return err;
  256. }
  257. EXPORT_SYMBOL(mdiobus_register);
  258. void mdiobus_unregister(struct mii_bus *bus)
  259. {
  260. int i;
  261. BUG_ON(bus->state != MDIOBUS_REGISTERED);
  262. bus->state = MDIOBUS_UNREGISTERED;
  263. device_del(&bus->dev);
  264. for (i = 0; i < PHY_MAX_ADDR; i++) {
  265. if (bus->phy_map[i])
  266. device_unregister(&bus->phy_map[i]->dev);
  267. bus->phy_map[i] = NULL;
  268. }
  269. }
  270. EXPORT_SYMBOL(mdiobus_unregister);
  271. /**
  272. * mdiobus_free - free a struct mii_bus
  273. * @bus: mii_bus to free
  274. *
  275. * This function releases the reference to the underlying device
  276. * object in the mii_bus. If this is the last reference, the mii_bus
  277. * will be freed.
  278. */
  279. void mdiobus_free(struct mii_bus *bus)
  280. {
  281. /* For compatibility with error handling in drivers. */
  282. if (bus->state == MDIOBUS_ALLOCATED) {
  283. kfree(bus);
  284. return;
  285. }
  286. BUG_ON(bus->state != MDIOBUS_UNREGISTERED);
  287. bus->state = MDIOBUS_RELEASED;
  288. put_device(&bus->dev);
  289. }
  290. EXPORT_SYMBOL(mdiobus_free);
  291. struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
  292. {
  293. struct phy_device *phydev;
  294. int err;
  295. phydev = get_phy_device(bus, addr, false);
  296. if (IS_ERR(phydev) || phydev == NULL)
  297. return phydev;
  298. /*
  299. * For DT, see if the auto-probed phy has a correspoding child
  300. * in the bus node, and set the of_node pointer in this case.
  301. */
  302. of_mdiobus_link_phydev(bus, phydev);
  303. err = phy_device_register(phydev);
  304. if (err) {
  305. phy_device_free(phydev);
  306. return NULL;
  307. }
  308. return phydev;
  309. }
  310. EXPORT_SYMBOL(mdiobus_scan);
  311. /**
  312. * mdiobus_read - Convenience function for reading a given MII mgmt register
  313. * @bus: the mii_bus struct
  314. * @addr: the phy address
  315. * @regnum: register number to read
  316. *
  317. * NOTE: MUST NOT be called from interrupt context,
  318. * because the bus read/write functions may wait for an interrupt
  319. * to conclude the operation.
  320. */
  321. int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
  322. {
  323. int retval;
  324. BUG_ON(in_interrupt());
  325. mutex_lock(&bus->mdio_lock);
  326. retval = bus->read(bus, addr, regnum);
  327. mutex_unlock(&bus->mdio_lock);
  328. return retval;
  329. }
  330. EXPORT_SYMBOL(mdiobus_read);
  331. /**
  332. * mdiobus_write - Convenience function for writing a given MII mgmt register
  333. * @bus: the mii_bus struct
  334. * @addr: the phy address
  335. * @regnum: register number to write
  336. * @val: value to write to @regnum
  337. *
  338. * NOTE: MUST NOT be called from interrupt context,
  339. * because the bus read/write functions may wait for an interrupt
  340. * to conclude the operation.
  341. */
  342. int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val)
  343. {
  344. int err;
  345. BUG_ON(in_interrupt());
  346. mutex_lock(&bus->mdio_lock);
  347. err = bus->write(bus, addr, regnum, val);
  348. mutex_unlock(&bus->mdio_lock);
  349. return err;
  350. }
  351. EXPORT_SYMBOL(mdiobus_write);
  352. /**
  353. * mdio_bus_match - determine if given PHY driver supports the given PHY device
  354. * @dev: target PHY device
  355. * @drv: given PHY driver
  356. *
  357. * Description: Given a PHY device, and a PHY driver, return 1 if
  358. * the driver supports the device. Otherwise, return 0.
  359. */
  360. static int mdio_bus_match(struct device *dev, struct device_driver *drv)
  361. {
  362. struct phy_device *phydev = to_phy_device(dev);
  363. struct phy_driver *phydrv = to_phy_driver(drv);
  364. if (of_driver_match_device(dev, drv))
  365. return 1;
  366. if (phydrv->match_phy_device)
  367. return phydrv->match_phy_device(phydev);
  368. return (phydrv->phy_id & phydrv->phy_id_mask) ==
  369. (phydev->phy_id & phydrv->phy_id_mask);
  370. }
  371. #ifdef CONFIG_PM
  372. static bool mdio_bus_phy_may_suspend(struct phy_device *phydev)
  373. {
  374. struct device_driver *drv = phydev->dev.driver;
  375. struct phy_driver *phydrv = to_phy_driver(drv);
  376. struct net_device *netdev = phydev->attached_dev;
  377. if (!drv || !phydrv->suspend)
  378. return false;
  379. /* PHY not attached? May suspend if the PHY has not already been
  380. * suspended as part of a prior call to phy_disconnect() ->
  381. * phy_detach() -> phy_suspend() because the parent netdev might be the
  382. * MDIO bus driver and clock gated at this point.
  383. */
  384. if (!netdev)
  385. return !phydev->suspended;
  386. /* Don't suspend PHY if the attched netdev parent may wakeup.
  387. * The parent may point to a PCI device, as in tg3 driver.
  388. */
  389. if (netdev->dev.parent && device_may_wakeup(netdev->dev.parent))
  390. return false;
  391. /* Also don't suspend PHY if the netdev itself may wakeup. This
  392. * is the case for devices w/o underlaying pwr. mgmt. aware bus,
  393. * e.g. SoC devices.
  394. */
  395. if (device_may_wakeup(&netdev->dev))
  396. return false;
  397. return true;
  398. }
  399. static int mdio_bus_suspend(struct device *dev)
  400. {
  401. struct phy_device *phydev = to_phy_device(dev);
  402. /* We must stop the state machine manually, otherwise it stops out of
  403. * control, possibly with the phydev->lock held. Upon resume, netdev
  404. * may call phy routines that try to grab the same lock, and that may
  405. * lead to a deadlock.
  406. */
  407. if (phydev->attached_dev && phydev->adjust_link)
  408. phy_stop_machine(phydev);
  409. if (!mdio_bus_phy_may_suspend(phydev))
  410. return 0;
  411. return phy_suspend(phydev);
  412. }
  413. static int mdio_bus_resume(struct device *dev)
  414. {
  415. struct phy_device *phydev = to_phy_device(dev);
  416. int ret;
  417. if (!mdio_bus_phy_may_suspend(phydev))
  418. goto no_resume;
  419. ret = phy_resume(phydev);
  420. if (ret < 0)
  421. return ret;
  422. no_resume:
  423. if (phydev->attached_dev && phydev->adjust_link)
  424. phy_start_machine(phydev);
  425. return 0;
  426. }
  427. static int mdio_bus_restore(struct device *dev)
  428. {
  429. struct phy_device *phydev = to_phy_device(dev);
  430. struct net_device *netdev = phydev->attached_dev;
  431. int ret;
  432. if (!netdev)
  433. return 0;
  434. ret = phy_init_hw(phydev);
  435. if (ret < 0)
  436. return ret;
  437. /* The PHY needs to renegotiate. */
  438. phydev->link = 0;
  439. phydev->state = PHY_UP;
  440. phy_start_machine(phydev);
  441. return 0;
  442. }
  443. static const struct dev_pm_ops mdio_bus_pm_ops = {
  444. .suspend = mdio_bus_suspend,
  445. .resume = mdio_bus_resume,
  446. .freeze = mdio_bus_suspend,
  447. .thaw = mdio_bus_resume,
  448. .restore = mdio_bus_restore,
  449. };
  450. #define MDIO_BUS_PM_OPS (&mdio_bus_pm_ops)
  451. #else
  452. #define MDIO_BUS_PM_OPS NULL
  453. #endif /* CONFIG_PM */
  454. static ssize_t
  455. phy_id_show(struct device *dev, struct device_attribute *attr, char *buf)
  456. {
  457. struct phy_device *phydev = to_phy_device(dev);
  458. return sprintf(buf, "0x%.8lx\n", (unsigned long)phydev->phy_id);
  459. }
  460. static DEVICE_ATTR_RO(phy_id);
  461. static ssize_t
  462. phy_interface_show(struct device *dev, struct device_attribute *attr, char *buf)
  463. {
  464. struct phy_device *phydev = to_phy_device(dev);
  465. const char *mode = NULL;
  466. if (phy_is_internal(phydev))
  467. mode = "internal";
  468. else
  469. mode = phy_modes(phydev->interface);
  470. return sprintf(buf, "%s\n", mode);
  471. }
  472. static DEVICE_ATTR_RO(phy_interface);
  473. static ssize_t
  474. phy_has_fixups_show(struct device *dev, struct device_attribute *attr, char *buf)
  475. {
  476. struct phy_device *phydev = to_phy_device(dev);
  477. return sprintf(buf, "%d\n", phydev->has_fixups);
  478. }
  479. static DEVICE_ATTR_RO(phy_has_fixups);
  480. static struct attribute *mdio_dev_attrs[] = {
  481. &dev_attr_phy_id.attr,
  482. &dev_attr_phy_interface.attr,
  483. &dev_attr_phy_has_fixups.attr,
  484. NULL,
  485. };
  486. ATTRIBUTE_GROUPS(mdio_dev);
  487. struct bus_type mdio_bus_type = {
  488. .name = "mdio_bus",
  489. .match = mdio_bus_match,
  490. .pm = MDIO_BUS_PM_OPS,
  491. .dev_groups = mdio_dev_groups,
  492. };
  493. EXPORT_SYMBOL(mdio_bus_type);
  494. int __init mdio_bus_init(void)
  495. {
  496. int ret;
  497. ret = class_register(&mdio_bus_class);
  498. if (!ret) {
  499. ret = bus_register(&mdio_bus_type);
  500. if (ret)
  501. class_unregister(&mdio_bus_class);
  502. }
  503. return ret;
  504. }
  505. void mdio_bus_exit(void)
  506. {
  507. class_unregister(&mdio_bus_class);
  508. bus_unregister(&mdio_bus_type);
  509. }