fixed.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. #define MII_REGS_NUM 29
  25. struct fixed_mdio_bus {
  26. int irqs[PHY_MAX_ADDR];
  27. struct mii_bus *mii_bus;
  28. struct list_head phys;
  29. };
  30. struct fixed_phy {
  31. int addr;
  32. u16 regs[MII_REGS_NUM];
  33. struct phy_device *phydev;
  34. struct fixed_phy_status status;
  35. int (*link_update)(struct net_device *, struct fixed_phy_status *);
  36. struct list_head node;
  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. static int fixed_phy_update_regs(struct fixed_phy *fp)
  43. {
  44. u16 bmsr = BMSR_ANEGCAPABLE;
  45. u16 bmcr = 0;
  46. u16 lpagb = 0;
  47. u16 lpa = 0;
  48. if (fp->status.duplex) {
  49. bmcr |= BMCR_FULLDPLX;
  50. switch (fp->status.speed) {
  51. case 1000:
  52. bmsr |= BMSR_ESTATEN;
  53. bmcr |= BMCR_SPEED1000;
  54. lpagb |= LPA_1000FULL;
  55. break;
  56. case 100:
  57. bmsr |= BMSR_100FULL;
  58. bmcr |= BMCR_SPEED100;
  59. lpa |= LPA_100FULL;
  60. break;
  61. case 10:
  62. bmsr |= BMSR_10FULL;
  63. lpa |= LPA_10FULL;
  64. break;
  65. default:
  66. pr_warn("fixed phy: unknown speed\n");
  67. return -EINVAL;
  68. }
  69. } else {
  70. switch (fp->status.speed) {
  71. case 1000:
  72. bmsr |= BMSR_ESTATEN;
  73. bmcr |= BMCR_SPEED1000;
  74. lpagb |= LPA_1000HALF;
  75. break;
  76. case 100:
  77. bmsr |= BMSR_100HALF;
  78. bmcr |= BMCR_SPEED100;
  79. lpa |= LPA_100HALF;
  80. break;
  81. case 10:
  82. bmsr |= BMSR_10HALF;
  83. lpa |= LPA_10HALF;
  84. break;
  85. default:
  86. pr_warn("fixed phy: unknown speed\n");
  87. return -EINVAL;
  88. }
  89. }
  90. if (fp->status.link)
  91. bmsr |= BMSR_LSTATUS | BMSR_ANEGCOMPLETE;
  92. if (fp->status.pause)
  93. lpa |= LPA_PAUSE_CAP;
  94. if (fp->status.asym_pause)
  95. lpa |= LPA_PAUSE_ASYM;
  96. fp->regs[MII_PHYSID1] = 0;
  97. fp->regs[MII_PHYSID2] = 0;
  98. fp->regs[MII_BMSR] = bmsr;
  99. fp->regs[MII_BMCR] = bmcr;
  100. fp->regs[MII_LPA] = lpa;
  101. fp->regs[MII_STAT1000] = lpagb;
  102. return 0;
  103. }
  104. static int fixed_mdio_read(struct mii_bus *bus, int phy_addr, int reg_num)
  105. {
  106. struct fixed_mdio_bus *fmb = bus->priv;
  107. struct fixed_phy *fp;
  108. if (reg_num >= MII_REGS_NUM)
  109. return -1;
  110. list_for_each_entry(fp, &fmb->phys, node) {
  111. if (fp->addr == phy_addr) {
  112. /* Issue callback if user registered it. */
  113. if (fp->link_update) {
  114. fp->link_update(fp->phydev->attached_dev,
  115. &fp->status);
  116. fixed_phy_update_regs(fp);
  117. }
  118. return fp->regs[reg_num];
  119. }
  120. }
  121. return 0xFFFF;
  122. }
  123. static int fixed_mdio_write(struct mii_bus *bus, int phy_addr, int reg_num,
  124. u16 val)
  125. {
  126. return 0;
  127. }
  128. /*
  129. * If something weird is required to be done with link/speed,
  130. * network driver is able to assign a function to implement this.
  131. * May be useful for PHY's that need to be software-driven.
  132. */
  133. int fixed_phy_set_link_update(struct phy_device *phydev,
  134. int (*link_update)(struct net_device *,
  135. struct fixed_phy_status *))
  136. {
  137. struct fixed_mdio_bus *fmb = &platform_fmb;
  138. struct fixed_phy *fp;
  139. if (!link_update || !phydev || !phydev->bus)
  140. return -EINVAL;
  141. list_for_each_entry(fp, &fmb->phys, node) {
  142. if (fp->addr == phydev->addr) {
  143. fp->link_update = link_update;
  144. fp->phydev = phydev;
  145. return 0;
  146. }
  147. }
  148. return -ENOENT;
  149. }
  150. EXPORT_SYMBOL_GPL(fixed_phy_set_link_update);
  151. int fixed_phy_add(unsigned int irq, int phy_addr,
  152. struct fixed_phy_status *status)
  153. {
  154. int ret;
  155. struct fixed_mdio_bus *fmb = &platform_fmb;
  156. struct fixed_phy *fp;
  157. fp = kzalloc(sizeof(*fp), GFP_KERNEL);
  158. if (!fp)
  159. return -ENOMEM;
  160. memset(fp->regs, 0xFF, sizeof(fp->regs[0]) * MII_REGS_NUM);
  161. fmb->irqs[phy_addr] = irq;
  162. fp->addr = phy_addr;
  163. fp->status = *status;
  164. ret = fixed_phy_update_regs(fp);
  165. if (ret)
  166. goto err_regs;
  167. list_add_tail(&fp->node, &fmb->phys);
  168. return 0;
  169. err_regs:
  170. kfree(fp);
  171. return ret;
  172. }
  173. EXPORT_SYMBOL_GPL(fixed_phy_add);
  174. void fixed_phy_del(int phy_addr)
  175. {
  176. struct fixed_mdio_bus *fmb = &platform_fmb;
  177. struct fixed_phy *fp, *tmp;
  178. list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
  179. if (fp->addr == phy_addr) {
  180. list_del(&fp->node);
  181. kfree(fp);
  182. return;
  183. }
  184. }
  185. }
  186. EXPORT_SYMBOL_GPL(fixed_phy_del);
  187. static int phy_fixed_addr;
  188. static DEFINE_SPINLOCK(phy_fixed_addr_lock);
  189. int fixed_phy_register(unsigned int irq,
  190. struct fixed_phy_status *status,
  191. struct device_node *np)
  192. {
  193. struct fixed_mdio_bus *fmb = &platform_fmb;
  194. struct phy_device *phy;
  195. int phy_addr;
  196. int ret;
  197. /* Get the next available PHY address, up to PHY_MAX_ADDR */
  198. spin_lock(&phy_fixed_addr_lock);
  199. if (phy_fixed_addr == PHY_MAX_ADDR) {
  200. spin_unlock(&phy_fixed_addr_lock);
  201. return -ENOSPC;
  202. }
  203. phy_addr = phy_fixed_addr++;
  204. spin_unlock(&phy_fixed_addr_lock);
  205. ret = fixed_phy_add(PHY_POLL, phy_addr, status);
  206. if (ret < 0)
  207. return ret;
  208. phy = get_phy_device(fmb->mii_bus, phy_addr, false);
  209. if (!phy || IS_ERR(phy)) {
  210. fixed_phy_del(phy_addr);
  211. return -EINVAL;
  212. }
  213. of_node_get(np);
  214. phy->dev.of_node = np;
  215. ret = phy_device_register(phy);
  216. if (ret) {
  217. phy_device_free(phy);
  218. of_node_put(np);
  219. fixed_phy_del(phy_addr);
  220. return ret;
  221. }
  222. return 0;
  223. }
  224. static int __init fixed_mdio_bus_init(void)
  225. {
  226. struct fixed_mdio_bus *fmb = &platform_fmb;
  227. int ret;
  228. pdev = platform_device_register_simple("Fixed MDIO bus", 0, NULL, 0);
  229. if (IS_ERR(pdev)) {
  230. ret = PTR_ERR(pdev);
  231. goto err_pdev;
  232. }
  233. fmb->mii_bus = mdiobus_alloc();
  234. if (fmb->mii_bus == NULL) {
  235. ret = -ENOMEM;
  236. goto err_mdiobus_reg;
  237. }
  238. snprintf(fmb->mii_bus->id, MII_BUS_ID_SIZE, "fixed-0");
  239. fmb->mii_bus->name = "Fixed MDIO Bus";
  240. fmb->mii_bus->priv = fmb;
  241. fmb->mii_bus->parent = &pdev->dev;
  242. fmb->mii_bus->read = &fixed_mdio_read;
  243. fmb->mii_bus->write = &fixed_mdio_write;
  244. fmb->mii_bus->irq = fmb->irqs;
  245. ret = mdiobus_register(fmb->mii_bus);
  246. if (ret)
  247. goto err_mdiobus_alloc;
  248. return 0;
  249. err_mdiobus_alloc:
  250. mdiobus_free(fmb->mii_bus);
  251. err_mdiobus_reg:
  252. platform_device_unregister(pdev);
  253. err_pdev:
  254. return ret;
  255. }
  256. module_init(fixed_mdio_bus_init);
  257. static void __exit fixed_mdio_bus_exit(void)
  258. {
  259. struct fixed_mdio_bus *fmb = &platform_fmb;
  260. struct fixed_phy *fp, *tmp;
  261. mdiobus_unregister(fmb->mii_bus);
  262. mdiobus_free(fmb->mii_bus);
  263. platform_device_unregister(pdev);
  264. list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
  265. list_del(&fp->node);
  266. kfree(fp);
  267. }
  268. }
  269. module_exit(fixed_mdio_bus_exit);
  270. MODULE_DESCRIPTION("Fixed MDIO bus (MDIO bus emulation with fixed PHYs)");
  271. MODULE_AUTHOR("Vitaly Bordug");
  272. MODULE_LICENSE("GPL");