fixed.c 5.6 KB

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