fixed_phy.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Fixed MDIO bus (MDIO bus emulation with fixed PHYs)
  4. *
  5. * Author: Vitaly Bordug <[email protected]>
  6. * Anton Vorontsov <[email protected]>
  7. *
  8. * Copyright (c) 2006-2007 MontaVista Software, Inc.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/list.h>
  14. #include <linux/mii.h>
  15. #include <linux/phy.h>
  16. #include <linux/phy_fixed.h>
  17. #include <linux/err.h>
  18. #include <linux/slab.h>
  19. #include <linux/of.h>
  20. #include <linux/gpio/consumer.h>
  21. #include <linux/idr.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/linkmode.h>
  24. #include "swphy.h"
  25. struct fixed_mdio_bus {
  26. struct mii_bus *mii_bus;
  27. struct list_head phys;
  28. };
  29. struct fixed_phy {
  30. int addr;
  31. struct phy_device *phydev;
  32. struct fixed_phy_status status;
  33. bool no_carrier;
  34. int (*link_update)(struct net_device *, struct fixed_phy_status *);
  35. struct list_head node;
  36. struct gpio_desc *link_gpiod;
  37. };
  38. static struct platform_device *pdev;
  39. static struct fixed_mdio_bus platform_fmb = {
  40. .phys = LIST_HEAD_INIT(platform_fmb.phys),
  41. };
  42. int fixed_phy_change_carrier(struct net_device *dev, bool new_carrier)
  43. {
  44. struct fixed_mdio_bus *fmb = &platform_fmb;
  45. struct phy_device *phydev = dev->phydev;
  46. struct fixed_phy *fp;
  47. if (!phydev || !phydev->mdio.bus)
  48. return -EINVAL;
  49. list_for_each_entry(fp, &fmb->phys, node) {
  50. if (fp->addr == phydev->mdio.addr) {
  51. fp->no_carrier = !new_carrier;
  52. return 0;
  53. }
  54. }
  55. return -EINVAL;
  56. }
  57. EXPORT_SYMBOL_GPL(fixed_phy_change_carrier);
  58. static void fixed_phy_update(struct fixed_phy *fp)
  59. {
  60. if (!fp->no_carrier && fp->link_gpiod)
  61. fp->status.link = !!gpiod_get_value_cansleep(fp->link_gpiod);
  62. }
  63. static int fixed_mdio_read(struct mii_bus *bus, int phy_addr, int reg_num)
  64. {
  65. struct fixed_mdio_bus *fmb = bus->priv;
  66. struct fixed_phy *fp;
  67. list_for_each_entry(fp, &fmb->phys, node) {
  68. if (fp->addr == phy_addr) {
  69. struct fixed_phy_status state;
  70. fp->status.link = !fp->no_carrier;
  71. /* Issue callback if user registered it. */
  72. if (fp->link_update)
  73. fp->link_update(fp->phydev->attached_dev,
  74. &fp->status);
  75. /* Check the GPIO for change in status */
  76. fixed_phy_update(fp);
  77. state = fp->status;
  78. return swphy_read_reg(reg_num, &state);
  79. }
  80. }
  81. return 0xFFFF;
  82. }
  83. static int fixed_mdio_write(struct mii_bus *bus, int phy_addr, int reg_num,
  84. u16 val)
  85. {
  86. return 0;
  87. }
  88. /*
  89. * If something weird is required to be done with link/speed,
  90. * network driver is able to assign a function to implement this.
  91. * May be useful for PHY's that need to be software-driven.
  92. */
  93. int fixed_phy_set_link_update(struct phy_device *phydev,
  94. int (*link_update)(struct net_device *,
  95. struct fixed_phy_status *))
  96. {
  97. struct fixed_mdio_bus *fmb = &platform_fmb;
  98. struct fixed_phy *fp;
  99. if (!phydev || !phydev->mdio.bus)
  100. return -EINVAL;
  101. list_for_each_entry(fp, &fmb->phys, node) {
  102. if (fp->addr == phydev->mdio.addr) {
  103. fp->link_update = link_update;
  104. fp->phydev = phydev;
  105. return 0;
  106. }
  107. }
  108. return -ENOENT;
  109. }
  110. EXPORT_SYMBOL_GPL(fixed_phy_set_link_update);
  111. static int fixed_phy_add_gpiod(unsigned int irq, int phy_addr,
  112. struct fixed_phy_status *status,
  113. struct gpio_desc *gpiod)
  114. {
  115. int ret;
  116. struct fixed_mdio_bus *fmb = &platform_fmb;
  117. struct fixed_phy *fp;
  118. ret = swphy_validate_state(status);
  119. if (ret < 0)
  120. return ret;
  121. fp = kzalloc(sizeof(*fp), GFP_KERNEL);
  122. if (!fp)
  123. return -ENOMEM;
  124. if (irq != PHY_POLL)
  125. fmb->mii_bus->irq[phy_addr] = irq;
  126. fp->addr = phy_addr;
  127. fp->status = *status;
  128. fp->link_gpiod = gpiod;
  129. fixed_phy_update(fp);
  130. list_add_tail(&fp->node, &fmb->phys);
  131. return 0;
  132. }
  133. int fixed_phy_add(unsigned int irq, int phy_addr,
  134. struct fixed_phy_status *status)
  135. {
  136. return fixed_phy_add_gpiod(irq, phy_addr, status, NULL);
  137. }
  138. EXPORT_SYMBOL_GPL(fixed_phy_add);
  139. static DEFINE_IDA(phy_fixed_ida);
  140. static void fixed_phy_del(int phy_addr)
  141. {
  142. struct fixed_mdio_bus *fmb = &platform_fmb;
  143. struct fixed_phy *fp, *tmp;
  144. list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
  145. if (fp->addr == phy_addr) {
  146. list_del(&fp->node);
  147. if (fp->link_gpiod)
  148. gpiod_put(fp->link_gpiod);
  149. kfree(fp);
  150. ida_free(&phy_fixed_ida, phy_addr);
  151. return;
  152. }
  153. }
  154. }
  155. #ifdef CONFIG_OF_GPIO
  156. static struct gpio_desc *fixed_phy_get_gpiod(struct device_node *np)
  157. {
  158. struct device_node *fixed_link_node;
  159. struct gpio_desc *gpiod;
  160. if (!np)
  161. return NULL;
  162. fixed_link_node = of_get_child_by_name(np, "fixed-link");
  163. if (!fixed_link_node)
  164. return NULL;
  165. /*
  166. * As the fixed link is just a device tree node without any
  167. * Linux device associated with it, we simply have obtain
  168. * the GPIO descriptor from the device tree like this.
  169. */
  170. gpiod = fwnode_gpiod_get_index(of_fwnode_handle(fixed_link_node),
  171. "link", 0, GPIOD_IN, "mdio");
  172. if (IS_ERR(gpiod) && PTR_ERR(gpiod) != -EPROBE_DEFER) {
  173. if (PTR_ERR(gpiod) != -ENOENT)
  174. pr_err("error getting GPIO for fixed link %pOF, proceed without\n",
  175. fixed_link_node);
  176. gpiod = NULL;
  177. }
  178. of_node_put(fixed_link_node);
  179. return gpiod;
  180. }
  181. #else
  182. static struct gpio_desc *fixed_phy_get_gpiod(struct device_node *np)
  183. {
  184. return NULL;
  185. }
  186. #endif
  187. static struct phy_device *__fixed_phy_register(unsigned int irq,
  188. struct fixed_phy_status *status,
  189. struct device_node *np,
  190. struct gpio_desc *gpiod)
  191. {
  192. struct fixed_mdio_bus *fmb = &platform_fmb;
  193. struct phy_device *phy;
  194. int phy_addr;
  195. int ret;
  196. if (!fmb->mii_bus || fmb->mii_bus->state != MDIOBUS_REGISTERED)
  197. return ERR_PTR(-EPROBE_DEFER);
  198. /* Check if we have a GPIO associated with this fixed phy */
  199. if (!gpiod) {
  200. gpiod = fixed_phy_get_gpiod(np);
  201. if (IS_ERR(gpiod))
  202. return ERR_CAST(gpiod);
  203. }
  204. /* Get the next available PHY address, up to PHY_MAX_ADDR */
  205. phy_addr = ida_alloc_max(&phy_fixed_ida, PHY_MAX_ADDR - 1, GFP_KERNEL);
  206. if (phy_addr < 0)
  207. return ERR_PTR(phy_addr);
  208. ret = fixed_phy_add_gpiod(irq, phy_addr, status, gpiod);
  209. if (ret < 0) {
  210. ida_free(&phy_fixed_ida, phy_addr);
  211. return ERR_PTR(ret);
  212. }
  213. phy = get_phy_device(fmb->mii_bus, phy_addr, false);
  214. if (IS_ERR(phy)) {
  215. fixed_phy_del(phy_addr);
  216. return ERR_PTR(-EINVAL);
  217. }
  218. /* propagate the fixed link values to struct phy_device */
  219. phy->link = status->link;
  220. if (status->link) {
  221. phy->speed = status->speed;
  222. phy->duplex = status->duplex;
  223. phy->pause = status->pause;
  224. phy->asym_pause = status->asym_pause;
  225. }
  226. of_node_get(np);
  227. phy->mdio.dev.of_node = np;
  228. phy->is_pseudo_fixed_link = true;
  229. switch (status->speed) {
  230. case SPEED_1000:
  231. linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
  232. phy->supported);
  233. linkmode_set_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
  234. phy->supported);
  235. fallthrough;
  236. case SPEED_100:
  237. linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Half_BIT,
  238. phy->supported);
  239. linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
  240. phy->supported);
  241. fallthrough;
  242. case SPEED_10:
  243. default:
  244. linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Half_BIT,
  245. phy->supported);
  246. linkmode_set_bit(ETHTOOL_LINK_MODE_10baseT_Full_BIT,
  247. phy->supported);
  248. }
  249. phy_advertise_supported(phy);
  250. ret = phy_device_register(phy);
  251. if (ret) {
  252. phy_device_free(phy);
  253. of_node_put(np);
  254. fixed_phy_del(phy_addr);
  255. return ERR_PTR(ret);
  256. }
  257. return phy;
  258. }
  259. struct phy_device *fixed_phy_register(unsigned int irq,
  260. struct fixed_phy_status *status,
  261. struct device_node *np)
  262. {
  263. return __fixed_phy_register(irq, status, np, NULL);
  264. }
  265. EXPORT_SYMBOL_GPL(fixed_phy_register);
  266. struct phy_device *
  267. fixed_phy_register_with_gpiod(unsigned int irq,
  268. struct fixed_phy_status *status,
  269. struct gpio_desc *gpiod)
  270. {
  271. return __fixed_phy_register(irq, status, NULL, gpiod);
  272. }
  273. EXPORT_SYMBOL_GPL(fixed_phy_register_with_gpiod);
  274. void fixed_phy_unregister(struct phy_device *phy)
  275. {
  276. phy_device_remove(phy);
  277. of_node_put(phy->mdio.dev.of_node);
  278. fixed_phy_del(phy->mdio.addr);
  279. }
  280. EXPORT_SYMBOL_GPL(fixed_phy_unregister);
  281. static int __init fixed_mdio_bus_init(void)
  282. {
  283. struct fixed_mdio_bus *fmb = &platform_fmb;
  284. int ret;
  285. pdev = platform_device_register_simple("Fixed MDIO bus", 0, NULL, 0);
  286. if (IS_ERR(pdev))
  287. return PTR_ERR(pdev);
  288. fmb->mii_bus = mdiobus_alloc();
  289. if (fmb->mii_bus == NULL) {
  290. ret = -ENOMEM;
  291. goto err_mdiobus_reg;
  292. }
  293. snprintf(fmb->mii_bus->id, MII_BUS_ID_SIZE, "fixed-0");
  294. fmb->mii_bus->name = "Fixed MDIO Bus";
  295. fmb->mii_bus->priv = fmb;
  296. fmb->mii_bus->parent = &pdev->dev;
  297. fmb->mii_bus->read = &fixed_mdio_read;
  298. fmb->mii_bus->write = &fixed_mdio_write;
  299. fmb->mii_bus->phy_mask = ~0;
  300. ret = mdiobus_register(fmb->mii_bus);
  301. if (ret)
  302. goto err_mdiobus_alloc;
  303. return 0;
  304. err_mdiobus_alloc:
  305. mdiobus_free(fmb->mii_bus);
  306. err_mdiobus_reg:
  307. platform_device_unregister(pdev);
  308. return ret;
  309. }
  310. module_init(fixed_mdio_bus_init);
  311. static void __exit fixed_mdio_bus_exit(void)
  312. {
  313. struct fixed_mdio_bus *fmb = &platform_fmb;
  314. struct fixed_phy *fp, *tmp;
  315. mdiobus_unregister(fmb->mii_bus);
  316. mdiobus_free(fmb->mii_bus);
  317. platform_device_unregister(pdev);
  318. list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
  319. list_del(&fp->node);
  320. kfree(fp);
  321. }
  322. ida_destroy(&phy_fixed_ida);
  323. }
  324. module_exit(fixed_mdio_bus_exit);
  325. MODULE_DESCRIPTION("Fixed MDIO bus (MDIO bus emulation with fixed PHYs)");
  326. MODULE_AUTHOR("Vitaly Bordug");
  327. MODULE_LICENSE("GPL");