fixed_phy.c 9.3 KB

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