fixed_phy.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * Fixed MDIO bus (MDIO bus emulation with fixed PHYs)
  3. *
  4. * Author: Vitaly Bordug <vbordug@ru.mvista.com>
  5. * Anton Vorontsov <avorontsov@ru.mvista.com>
  6. *
  7. * Copyright (c) 2006-2007 MontaVista Software, Inc.
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/list.h>
  18. #include <linux/mii.h>
  19. #include <linux/phy.h>
  20. #include <linux/phy_fixed.h>
  21. #include <linux/err.h>
  22. #include <linux/slab.h>
  23. #include <linux/of.h>
  24. #include <linux/gpio.h>
  25. #include <linux/seqlock.h>
  26. #include <linux/idr.h>
  27. #include "swphy.h"
  28. struct fixed_mdio_bus {
  29. struct mii_bus *mii_bus;
  30. struct list_head phys;
  31. };
  32. struct fixed_phy {
  33. int addr;
  34. struct phy_device *phydev;
  35. seqcount_t seqcount;
  36. struct fixed_phy_status status;
  37. int (*link_update)(struct net_device *, struct fixed_phy_status *);
  38. struct list_head node;
  39. int link_gpio;
  40. };
  41. static struct platform_device *pdev;
  42. static struct fixed_mdio_bus platform_fmb = {
  43. .phys = LIST_HEAD_INIT(platform_fmb.phys),
  44. };
  45. static void fixed_phy_update(struct fixed_phy *fp)
  46. {
  47. if (gpio_is_valid(fp->link_gpio))
  48. fp->status.link = !!gpio_get_value_cansleep(fp->link_gpio);
  49. }
  50. static int fixed_mdio_read(struct mii_bus *bus, int phy_addr, int reg_num)
  51. {
  52. struct fixed_mdio_bus *fmb = bus->priv;
  53. struct fixed_phy *fp;
  54. list_for_each_entry(fp, &fmb->phys, node) {
  55. if (fp->addr == phy_addr) {
  56. struct fixed_phy_status state;
  57. int s;
  58. do {
  59. s = read_seqcount_begin(&fp->seqcount);
  60. /* Issue callback if user registered it. */
  61. if (fp->link_update)
  62. fp->link_update(fp->phydev->attached_dev,
  63. &fp->status);
  64. /* Check the GPIO for change in status */
  65. fixed_phy_update(fp);
  66. state = fp->status;
  67. } while (read_seqcount_retry(&fp->seqcount, s));
  68. return swphy_read_reg(reg_num, &state);
  69. }
  70. }
  71. return 0xFFFF;
  72. }
  73. static int fixed_mdio_write(struct mii_bus *bus, int phy_addr, int reg_num,
  74. u16 val)
  75. {
  76. return 0;
  77. }
  78. /*
  79. * If something weird is required to be done with link/speed,
  80. * network driver is able to assign a function to implement this.
  81. * May be useful for PHY's that need to be software-driven.
  82. */
  83. int fixed_phy_set_link_update(struct phy_device *phydev,
  84. int (*link_update)(struct net_device *,
  85. struct fixed_phy_status *))
  86. {
  87. struct fixed_mdio_bus *fmb = &platform_fmb;
  88. struct fixed_phy *fp;
  89. if (!phydev || !phydev->mdio.bus)
  90. return -EINVAL;
  91. list_for_each_entry(fp, &fmb->phys, node) {
  92. if (fp->addr == phydev->mdio.addr) {
  93. fp->link_update = link_update;
  94. fp->phydev = phydev;
  95. return 0;
  96. }
  97. }
  98. return -ENOENT;
  99. }
  100. EXPORT_SYMBOL_GPL(fixed_phy_set_link_update);
  101. int fixed_phy_add(unsigned int irq, int phy_addr,
  102. struct fixed_phy_status *status,
  103. int link_gpio)
  104. {
  105. int ret;
  106. struct fixed_mdio_bus *fmb = &platform_fmb;
  107. struct fixed_phy *fp;
  108. ret = swphy_validate_state(status);
  109. if (ret < 0)
  110. return ret;
  111. fp = kzalloc(sizeof(*fp), GFP_KERNEL);
  112. if (!fp)
  113. return -ENOMEM;
  114. seqcount_init(&fp->seqcount);
  115. if (irq != PHY_POLL)
  116. fmb->mii_bus->irq[phy_addr] = irq;
  117. fp->addr = phy_addr;
  118. fp->status = *status;
  119. fp->link_gpio = link_gpio;
  120. if (gpio_is_valid(fp->link_gpio)) {
  121. ret = gpio_request_one(fp->link_gpio, GPIOF_DIR_IN,
  122. "fixed-link-gpio-link");
  123. if (ret)
  124. goto err_regs;
  125. }
  126. fixed_phy_update(fp);
  127. list_add_tail(&fp->node, &fmb->phys);
  128. return 0;
  129. err_regs:
  130. kfree(fp);
  131. return ret;
  132. }
  133. EXPORT_SYMBOL_GPL(fixed_phy_add);
  134. static DEFINE_IDA(phy_fixed_ida);
  135. static void fixed_phy_del(int phy_addr)
  136. {
  137. struct fixed_mdio_bus *fmb = &platform_fmb;
  138. struct fixed_phy *fp, *tmp;
  139. list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
  140. if (fp->addr == phy_addr) {
  141. list_del(&fp->node);
  142. if (gpio_is_valid(fp->link_gpio))
  143. gpio_free(fp->link_gpio);
  144. kfree(fp);
  145. ida_simple_remove(&phy_fixed_ida, phy_addr);
  146. return;
  147. }
  148. }
  149. }
  150. struct phy_device *fixed_phy_register(unsigned int irq,
  151. struct fixed_phy_status *status,
  152. int link_gpio,
  153. struct device_node *np)
  154. {
  155. struct fixed_mdio_bus *fmb = &platform_fmb;
  156. struct phy_device *phy;
  157. int phy_addr;
  158. int ret;
  159. if (!fmb->mii_bus || fmb->mii_bus->state != MDIOBUS_REGISTERED)
  160. return ERR_PTR(-EPROBE_DEFER);
  161. /* Get the next available PHY address, up to PHY_MAX_ADDR */
  162. phy_addr = ida_simple_get(&phy_fixed_ida, 0, PHY_MAX_ADDR, GFP_KERNEL);
  163. if (phy_addr < 0)
  164. return ERR_PTR(phy_addr);
  165. ret = fixed_phy_add(irq, phy_addr, status, link_gpio);
  166. if (ret < 0) {
  167. ida_simple_remove(&phy_fixed_ida, phy_addr);
  168. return ERR_PTR(ret);
  169. }
  170. phy = get_phy_device(fmb->mii_bus, phy_addr, false);
  171. if (IS_ERR(phy)) {
  172. fixed_phy_del(phy_addr);
  173. return ERR_PTR(-EINVAL);
  174. }
  175. /* propagate the fixed link values to struct phy_device */
  176. phy->link = status->link;
  177. if (status->link) {
  178. phy->speed = status->speed;
  179. phy->duplex = status->duplex;
  180. phy->pause = status->pause;
  181. phy->asym_pause = status->asym_pause;
  182. }
  183. of_node_get(np);
  184. phy->mdio.dev.of_node = np;
  185. phy->is_pseudo_fixed_link = true;
  186. switch (status->speed) {
  187. case SPEED_1000:
  188. phy->supported = PHY_1000BT_FEATURES;
  189. break;
  190. case SPEED_100:
  191. phy->supported = PHY_100BT_FEATURES;
  192. break;
  193. case SPEED_10:
  194. default:
  195. phy->supported = PHY_10BT_FEATURES;
  196. }
  197. ret = phy_device_register(phy);
  198. if (ret) {
  199. phy_device_free(phy);
  200. of_node_put(np);
  201. fixed_phy_del(phy_addr);
  202. return ERR_PTR(ret);
  203. }
  204. return phy;
  205. }
  206. EXPORT_SYMBOL_GPL(fixed_phy_register);
  207. void fixed_phy_unregister(struct phy_device *phy)
  208. {
  209. phy_device_remove(phy);
  210. of_node_put(phy->mdio.dev.of_node);
  211. fixed_phy_del(phy->mdio.addr);
  212. }
  213. EXPORT_SYMBOL_GPL(fixed_phy_unregister);
  214. static int __init fixed_mdio_bus_init(void)
  215. {
  216. struct fixed_mdio_bus *fmb = &platform_fmb;
  217. int ret;
  218. pdev = platform_device_register_simple("Fixed MDIO bus", 0, NULL, 0);
  219. if (IS_ERR(pdev))
  220. return PTR_ERR(pdev);
  221. fmb->mii_bus = mdiobus_alloc();
  222. if (fmb->mii_bus == NULL) {
  223. ret = -ENOMEM;
  224. goto err_mdiobus_reg;
  225. }
  226. snprintf(fmb->mii_bus->id, MII_BUS_ID_SIZE, "fixed-0");
  227. fmb->mii_bus->name = "Fixed MDIO Bus";
  228. fmb->mii_bus->priv = fmb;
  229. fmb->mii_bus->parent = &pdev->dev;
  230. fmb->mii_bus->read = &fixed_mdio_read;
  231. fmb->mii_bus->write = &fixed_mdio_write;
  232. ret = mdiobus_register(fmb->mii_bus);
  233. if (ret)
  234. goto err_mdiobus_alloc;
  235. return 0;
  236. err_mdiobus_alloc:
  237. mdiobus_free(fmb->mii_bus);
  238. err_mdiobus_reg:
  239. platform_device_unregister(pdev);
  240. return ret;
  241. }
  242. module_init(fixed_mdio_bus_init);
  243. static void __exit fixed_mdio_bus_exit(void)
  244. {
  245. struct fixed_mdio_bus *fmb = &platform_fmb;
  246. struct fixed_phy *fp, *tmp;
  247. mdiobus_unregister(fmb->mii_bus);
  248. mdiobus_free(fmb->mii_bus);
  249. platform_device_unregister(pdev);
  250. list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
  251. list_del(&fp->node);
  252. kfree(fp);
  253. }
  254. ida_destroy(&phy_fixed_ida);
  255. }
  256. module_exit(fixed_mdio_bus_exit);
  257. MODULE_DESCRIPTION("Fixed MDIO bus (MDIO bus emulation with fixed PHYs)");
  258. MODULE_AUTHOR("Vitaly Bordug");
  259. MODULE_LICENSE("GPL");