fixed_phy.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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_update_state(struct phy_device *phydev,
  162. const struct fixed_phy_status *status,
  163. const struct fixed_phy_status *changed)
  164. {
  165. struct fixed_mdio_bus *fmb = &platform_fmb;
  166. struct fixed_phy *fp;
  167. if (!phydev || !phydev->bus)
  168. return -EINVAL;
  169. list_for_each_entry(fp, &fmb->phys, node) {
  170. if (fp->addr == phydev->addr) {
  171. #define _UPD(x) if (changed->x) \
  172. fp->status.x = status->x
  173. _UPD(link);
  174. _UPD(speed);
  175. _UPD(duplex);
  176. _UPD(pause);
  177. _UPD(asym_pause);
  178. #undef _UPD
  179. fixed_phy_update_regs(fp);
  180. return 0;
  181. }
  182. }
  183. return -ENOENT;
  184. }
  185. EXPORT_SYMBOL(fixed_phy_update_state);
  186. int fixed_phy_add(unsigned int irq, int phy_addr,
  187. struct fixed_phy_status *status)
  188. {
  189. int ret;
  190. struct fixed_mdio_bus *fmb = &platform_fmb;
  191. struct fixed_phy *fp;
  192. fp = kzalloc(sizeof(*fp), GFP_KERNEL);
  193. if (!fp)
  194. return -ENOMEM;
  195. memset(fp->regs, 0xFF, sizeof(fp->regs[0]) * MII_REGS_NUM);
  196. fmb->irqs[phy_addr] = irq;
  197. fp->addr = phy_addr;
  198. fp->status = *status;
  199. ret = fixed_phy_update_regs(fp);
  200. if (ret)
  201. goto err_regs;
  202. list_add_tail(&fp->node, &fmb->phys);
  203. return 0;
  204. err_regs:
  205. kfree(fp);
  206. return ret;
  207. }
  208. EXPORT_SYMBOL_GPL(fixed_phy_add);
  209. void fixed_phy_del(int phy_addr)
  210. {
  211. struct fixed_mdio_bus *fmb = &platform_fmb;
  212. struct fixed_phy *fp, *tmp;
  213. list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
  214. if (fp->addr == phy_addr) {
  215. list_del(&fp->node);
  216. kfree(fp);
  217. return;
  218. }
  219. }
  220. }
  221. EXPORT_SYMBOL_GPL(fixed_phy_del);
  222. static int phy_fixed_addr;
  223. static DEFINE_SPINLOCK(phy_fixed_addr_lock);
  224. struct phy_device *fixed_phy_register(unsigned int irq,
  225. struct fixed_phy_status *status,
  226. struct device_node *np)
  227. {
  228. struct fixed_mdio_bus *fmb = &platform_fmb;
  229. struct phy_device *phy;
  230. int phy_addr;
  231. int ret;
  232. /* Get the next available PHY address, up to PHY_MAX_ADDR */
  233. spin_lock(&phy_fixed_addr_lock);
  234. if (phy_fixed_addr == PHY_MAX_ADDR) {
  235. spin_unlock(&phy_fixed_addr_lock);
  236. return ERR_PTR(-ENOSPC);
  237. }
  238. phy_addr = phy_fixed_addr++;
  239. spin_unlock(&phy_fixed_addr_lock);
  240. ret = fixed_phy_add(PHY_POLL, phy_addr, status);
  241. if (ret < 0)
  242. return ERR_PTR(ret);
  243. phy = get_phy_device(fmb->mii_bus, phy_addr, false);
  244. if (!phy || IS_ERR(phy)) {
  245. fixed_phy_del(phy_addr);
  246. return ERR_PTR(-EINVAL);
  247. }
  248. of_node_get(np);
  249. phy->dev.of_node = np;
  250. ret = phy_device_register(phy);
  251. if (ret) {
  252. phy_device_free(phy);
  253. of_node_put(np);
  254. fixed_phy_del(phy_addr);
  255. return ERR_PTR(ret);
  256. }
  257. return phy;
  258. }
  259. EXPORT_SYMBOL_GPL(fixed_phy_register);
  260. static int __init fixed_mdio_bus_init(void)
  261. {
  262. struct fixed_mdio_bus *fmb = &platform_fmb;
  263. int ret;
  264. pdev = platform_device_register_simple("Fixed MDIO bus", 0, NULL, 0);
  265. if (IS_ERR(pdev)) {
  266. ret = PTR_ERR(pdev);
  267. goto err_pdev;
  268. }
  269. fmb->mii_bus = mdiobus_alloc();
  270. if (fmb->mii_bus == NULL) {
  271. ret = -ENOMEM;
  272. goto err_mdiobus_reg;
  273. }
  274. snprintf(fmb->mii_bus->id, MII_BUS_ID_SIZE, "fixed-0");
  275. fmb->mii_bus->name = "Fixed MDIO Bus";
  276. fmb->mii_bus->priv = fmb;
  277. fmb->mii_bus->parent = &pdev->dev;
  278. fmb->mii_bus->read = &fixed_mdio_read;
  279. fmb->mii_bus->write = &fixed_mdio_write;
  280. fmb->mii_bus->irq = fmb->irqs;
  281. ret = mdiobus_register(fmb->mii_bus);
  282. if (ret)
  283. goto err_mdiobus_alloc;
  284. return 0;
  285. err_mdiobus_alloc:
  286. mdiobus_free(fmb->mii_bus);
  287. err_mdiobus_reg:
  288. platform_device_unregister(pdev);
  289. err_pdev:
  290. return ret;
  291. }
  292. module_init(fixed_mdio_bus_init);
  293. static void __exit fixed_mdio_bus_exit(void)
  294. {
  295. struct fixed_mdio_bus *fmb = &platform_fmb;
  296. struct fixed_phy *fp, *tmp;
  297. mdiobus_unregister(fmb->mii_bus);
  298. mdiobus_free(fmb->mii_bus);
  299. platform_device_unregister(pdev);
  300. list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
  301. list_del(&fp->node);
  302. kfree(fp);
  303. }
  304. }
  305. module_exit(fixed_mdio_bus_exit);
  306. MODULE_DESCRIPTION("Fixed MDIO bus (MDIO bus emulation with fixed PHYs)");
  307. MODULE_AUTHOR("Vitaly Bordug");
  308. MODULE_LICENSE("GPL");