fixed_phy.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. /* We do not support emulating Clause 45 over Clause 22 register reads
  111. * return an error instead of bogus data.
  112. */
  113. switch (reg_num) {
  114. case MII_MMD_CTRL:
  115. case MII_MMD_DATA:
  116. return -1;
  117. default:
  118. break;
  119. }
  120. list_for_each_entry(fp, &fmb->phys, node) {
  121. if (fp->addr == phy_addr) {
  122. /* Issue callback if user registered it. */
  123. if (fp->link_update) {
  124. fp->link_update(fp->phydev->attached_dev,
  125. &fp->status);
  126. fixed_phy_update_regs(fp);
  127. }
  128. return fp->regs[reg_num];
  129. }
  130. }
  131. return 0xFFFF;
  132. }
  133. static int fixed_mdio_write(struct mii_bus *bus, int phy_addr, int reg_num,
  134. u16 val)
  135. {
  136. return 0;
  137. }
  138. /*
  139. * If something weird is required to be done with link/speed,
  140. * network driver is able to assign a function to implement this.
  141. * May be useful for PHY's that need to be software-driven.
  142. */
  143. int fixed_phy_set_link_update(struct phy_device *phydev,
  144. int (*link_update)(struct net_device *,
  145. struct fixed_phy_status *))
  146. {
  147. struct fixed_mdio_bus *fmb = &platform_fmb;
  148. struct fixed_phy *fp;
  149. if (!phydev || !phydev->bus)
  150. return -EINVAL;
  151. list_for_each_entry(fp, &fmb->phys, node) {
  152. if (fp->addr == phydev->addr) {
  153. fp->link_update = link_update;
  154. fp->phydev = phydev;
  155. return 0;
  156. }
  157. }
  158. return -ENOENT;
  159. }
  160. EXPORT_SYMBOL_GPL(fixed_phy_set_link_update);
  161. int fixed_phy_add(unsigned int irq, int phy_addr,
  162. struct fixed_phy_status *status)
  163. {
  164. int ret;
  165. struct fixed_mdio_bus *fmb = &platform_fmb;
  166. struct fixed_phy *fp;
  167. fp = kzalloc(sizeof(*fp), GFP_KERNEL);
  168. if (!fp)
  169. return -ENOMEM;
  170. memset(fp->regs, 0xFF, sizeof(fp->regs[0]) * MII_REGS_NUM);
  171. fmb->irqs[phy_addr] = irq;
  172. fp->addr = phy_addr;
  173. fp->status = *status;
  174. ret = fixed_phy_update_regs(fp);
  175. if (ret)
  176. goto err_regs;
  177. list_add_tail(&fp->node, &fmb->phys);
  178. return 0;
  179. err_regs:
  180. kfree(fp);
  181. return ret;
  182. }
  183. EXPORT_SYMBOL_GPL(fixed_phy_add);
  184. void fixed_phy_del(int phy_addr)
  185. {
  186. struct fixed_mdio_bus *fmb = &platform_fmb;
  187. struct fixed_phy *fp, *tmp;
  188. list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
  189. if (fp->addr == phy_addr) {
  190. list_del(&fp->node);
  191. kfree(fp);
  192. return;
  193. }
  194. }
  195. }
  196. EXPORT_SYMBOL_GPL(fixed_phy_del);
  197. static int phy_fixed_addr;
  198. static DEFINE_SPINLOCK(phy_fixed_addr_lock);
  199. struct phy_device *fixed_phy_register(unsigned int irq,
  200. struct fixed_phy_status *status,
  201. struct device_node *np)
  202. {
  203. struct fixed_mdio_bus *fmb = &platform_fmb;
  204. struct phy_device *phy;
  205. int phy_addr;
  206. int ret;
  207. /* Get the next available PHY address, up to PHY_MAX_ADDR */
  208. spin_lock(&phy_fixed_addr_lock);
  209. if (phy_fixed_addr == PHY_MAX_ADDR) {
  210. spin_unlock(&phy_fixed_addr_lock);
  211. return ERR_PTR(-ENOSPC);
  212. }
  213. phy_addr = phy_fixed_addr++;
  214. spin_unlock(&phy_fixed_addr_lock);
  215. ret = fixed_phy_add(PHY_POLL, phy_addr, status);
  216. if (ret < 0)
  217. return ERR_PTR(ret);
  218. phy = get_phy_device(fmb->mii_bus, phy_addr, false);
  219. if (!phy || IS_ERR(phy)) {
  220. fixed_phy_del(phy_addr);
  221. return ERR_PTR(-EINVAL);
  222. }
  223. of_node_get(np);
  224. phy->dev.of_node = np;
  225. ret = phy_device_register(phy);
  226. if (ret) {
  227. phy_device_free(phy);
  228. of_node_put(np);
  229. fixed_phy_del(phy_addr);
  230. return ERR_PTR(ret);
  231. }
  232. return phy;
  233. }
  234. EXPORT_SYMBOL_GPL(fixed_phy_register);
  235. static int __init fixed_mdio_bus_init(void)
  236. {
  237. struct fixed_mdio_bus *fmb = &platform_fmb;
  238. int ret;
  239. pdev = platform_device_register_simple("Fixed MDIO bus", 0, NULL, 0);
  240. if (IS_ERR(pdev)) {
  241. ret = PTR_ERR(pdev);
  242. goto err_pdev;
  243. }
  244. fmb->mii_bus = mdiobus_alloc();
  245. if (fmb->mii_bus == NULL) {
  246. ret = -ENOMEM;
  247. goto err_mdiobus_reg;
  248. }
  249. snprintf(fmb->mii_bus->id, MII_BUS_ID_SIZE, "fixed-0");
  250. fmb->mii_bus->name = "Fixed MDIO Bus";
  251. fmb->mii_bus->priv = fmb;
  252. fmb->mii_bus->parent = &pdev->dev;
  253. fmb->mii_bus->read = &fixed_mdio_read;
  254. fmb->mii_bus->write = &fixed_mdio_write;
  255. fmb->mii_bus->irq = fmb->irqs;
  256. ret = mdiobus_register(fmb->mii_bus);
  257. if (ret)
  258. goto err_mdiobus_alloc;
  259. return 0;
  260. err_mdiobus_alloc:
  261. mdiobus_free(fmb->mii_bus);
  262. err_mdiobus_reg:
  263. platform_device_unregister(pdev);
  264. err_pdev:
  265. return ret;
  266. }
  267. module_init(fixed_mdio_bus_init);
  268. static void __exit fixed_mdio_bus_exit(void)
  269. {
  270. struct fixed_mdio_bus *fmb = &platform_fmb;
  271. struct fixed_phy *fp, *tmp;
  272. mdiobus_unregister(fmb->mii_bus);
  273. mdiobus_free(fmb->mii_bus);
  274. platform_device_unregister(pdev);
  275. list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
  276. list_del(&fp->node);
  277. kfree(fp);
  278. }
  279. }
  280. module_exit(fixed_mdio_bus_exit);
  281. MODULE_DESCRIPTION("Fixed MDIO bus (MDIO bus emulation with fixed PHYs)");
  282. MODULE_AUTHOR("Vitaly Bordug");
  283. MODULE_LICENSE("GPL");