fixed_phy.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. fixed_phy_update(fp);
  65. }
  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_update_state(struct phy_device *phydev,
  102. const struct fixed_phy_status *status,
  103. const struct fixed_phy_status *changed)
  104. {
  105. struct fixed_mdio_bus *fmb = &platform_fmb;
  106. struct fixed_phy *fp;
  107. if (!phydev || phydev->mdio.bus != fmb->mii_bus)
  108. return -EINVAL;
  109. list_for_each_entry(fp, &fmb->phys, node) {
  110. if (fp->addr == phydev->mdio.addr) {
  111. write_seqcount_begin(&fp->seqcount);
  112. #define _UPD(x) if (changed->x) \
  113. fp->status.x = status->x
  114. _UPD(link);
  115. _UPD(speed);
  116. _UPD(duplex);
  117. _UPD(pause);
  118. _UPD(asym_pause);
  119. #undef _UPD
  120. fixed_phy_update(fp);
  121. write_seqcount_end(&fp->seqcount);
  122. return 0;
  123. }
  124. }
  125. return -ENOENT;
  126. }
  127. EXPORT_SYMBOL(fixed_phy_update_state);
  128. int fixed_phy_add(unsigned int irq, int phy_addr,
  129. struct fixed_phy_status *status,
  130. int link_gpio)
  131. {
  132. int ret;
  133. struct fixed_mdio_bus *fmb = &platform_fmb;
  134. struct fixed_phy *fp;
  135. ret = swphy_validate_state(status);
  136. if (ret < 0)
  137. return ret;
  138. fp = kzalloc(sizeof(*fp), GFP_KERNEL);
  139. if (!fp)
  140. return -ENOMEM;
  141. seqcount_init(&fp->seqcount);
  142. if (irq != PHY_POLL)
  143. fmb->mii_bus->irq[phy_addr] = irq;
  144. fp->addr = phy_addr;
  145. fp->status = *status;
  146. fp->link_gpio = link_gpio;
  147. if (gpio_is_valid(fp->link_gpio)) {
  148. ret = gpio_request_one(fp->link_gpio, GPIOF_DIR_IN,
  149. "fixed-link-gpio-link");
  150. if (ret)
  151. goto err_regs;
  152. }
  153. fixed_phy_update(fp);
  154. list_add_tail(&fp->node, &fmb->phys);
  155. return 0;
  156. err_regs:
  157. kfree(fp);
  158. return ret;
  159. }
  160. EXPORT_SYMBOL_GPL(fixed_phy_add);
  161. static DEFINE_IDA(phy_fixed_ida);
  162. static void fixed_phy_del(int phy_addr)
  163. {
  164. struct fixed_mdio_bus *fmb = &platform_fmb;
  165. struct fixed_phy *fp, *tmp;
  166. list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
  167. if (fp->addr == phy_addr) {
  168. list_del(&fp->node);
  169. if (gpio_is_valid(fp->link_gpio))
  170. gpio_free(fp->link_gpio);
  171. kfree(fp);
  172. ida_simple_remove(&phy_fixed_ida, phy_addr);
  173. return;
  174. }
  175. }
  176. }
  177. struct phy_device *fixed_phy_register(unsigned int irq,
  178. struct fixed_phy_status *status,
  179. int link_gpio,
  180. struct device_node *np)
  181. {
  182. struct fixed_mdio_bus *fmb = &platform_fmb;
  183. struct phy_device *phy;
  184. int phy_addr;
  185. int ret;
  186. if (!fmb->mii_bus || fmb->mii_bus->state != MDIOBUS_REGISTERED)
  187. return ERR_PTR(-EPROBE_DEFER);
  188. /* Get the next available PHY address, up to PHY_MAX_ADDR */
  189. phy_addr = ida_simple_get(&phy_fixed_ida, 0, PHY_MAX_ADDR, GFP_KERNEL);
  190. if (phy_addr < 0)
  191. return ERR_PTR(phy_addr);
  192. ret = fixed_phy_add(irq, phy_addr, status, link_gpio);
  193. if (ret < 0) {
  194. ida_simple_remove(&phy_fixed_ida, phy_addr);
  195. return ERR_PTR(ret);
  196. }
  197. phy = get_phy_device(fmb->mii_bus, phy_addr, false);
  198. if (IS_ERR(phy)) {
  199. fixed_phy_del(phy_addr);
  200. return ERR_PTR(-EINVAL);
  201. }
  202. /* propagate the fixed link values to struct phy_device */
  203. phy->link = status->link;
  204. if (status->link) {
  205. phy->speed = status->speed;
  206. phy->duplex = status->duplex;
  207. phy->pause = status->pause;
  208. phy->asym_pause = status->asym_pause;
  209. }
  210. of_node_get(np);
  211. phy->mdio.dev.of_node = np;
  212. phy->is_pseudo_fixed_link = true;
  213. switch (status->speed) {
  214. case SPEED_1000:
  215. phy->supported = PHY_1000BT_FEATURES;
  216. break;
  217. case SPEED_100:
  218. phy->supported = PHY_100BT_FEATURES;
  219. break;
  220. case SPEED_10:
  221. default:
  222. phy->supported = PHY_10BT_FEATURES;
  223. }
  224. ret = phy_device_register(phy);
  225. if (ret) {
  226. phy_device_free(phy);
  227. of_node_put(np);
  228. fixed_phy_del(phy_addr);
  229. return ERR_PTR(ret);
  230. }
  231. return phy;
  232. }
  233. EXPORT_SYMBOL_GPL(fixed_phy_register);
  234. void fixed_phy_unregister(struct phy_device *phy)
  235. {
  236. phy_device_remove(phy);
  237. of_node_put(phy->mdio.dev.of_node);
  238. fixed_phy_del(phy->mdio.addr);
  239. }
  240. EXPORT_SYMBOL_GPL(fixed_phy_unregister);
  241. static int __init fixed_mdio_bus_init(void)
  242. {
  243. struct fixed_mdio_bus *fmb = &platform_fmb;
  244. int ret;
  245. pdev = platform_device_register_simple("Fixed MDIO bus", 0, NULL, 0);
  246. if (IS_ERR(pdev)) {
  247. ret = PTR_ERR(pdev);
  248. goto err_pdev;
  249. }
  250. fmb->mii_bus = mdiobus_alloc();
  251. if (fmb->mii_bus == NULL) {
  252. ret = -ENOMEM;
  253. goto err_mdiobus_reg;
  254. }
  255. snprintf(fmb->mii_bus->id, MII_BUS_ID_SIZE, "fixed-0");
  256. fmb->mii_bus->name = "Fixed MDIO Bus";
  257. fmb->mii_bus->priv = fmb;
  258. fmb->mii_bus->parent = &pdev->dev;
  259. fmb->mii_bus->read = &fixed_mdio_read;
  260. fmb->mii_bus->write = &fixed_mdio_write;
  261. ret = mdiobus_register(fmb->mii_bus);
  262. if (ret)
  263. goto err_mdiobus_alloc;
  264. return 0;
  265. err_mdiobus_alloc:
  266. mdiobus_free(fmb->mii_bus);
  267. err_mdiobus_reg:
  268. platform_device_unregister(pdev);
  269. err_pdev:
  270. return ret;
  271. }
  272. module_init(fixed_mdio_bus_init);
  273. static void __exit fixed_mdio_bus_exit(void)
  274. {
  275. struct fixed_mdio_bus *fmb = &platform_fmb;
  276. struct fixed_phy *fp, *tmp;
  277. mdiobus_unregister(fmb->mii_bus);
  278. mdiobus_free(fmb->mii_bus);
  279. platform_device_unregister(pdev);
  280. list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
  281. list_del(&fp->node);
  282. kfree(fp);
  283. }
  284. ida_destroy(&phy_fixed_ida);
  285. }
  286. module_exit(fixed_mdio_bus_exit);
  287. MODULE_DESCRIPTION("Fixed MDIO bus (MDIO bus emulation with fixed PHYs)");
  288. MODULE_AUTHOR("Vitaly Bordug");
  289. MODULE_LICENSE("GPL");