fixed.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. #define MII_REGS_NUM 29
  22. struct fixed_mdio_bus {
  23. int irqs[PHY_MAX_ADDR];
  24. struct mii_bus mii_bus;
  25. struct list_head phys;
  26. };
  27. struct fixed_phy {
  28. int id;
  29. u16 regs[MII_REGS_NUM];
  30. struct phy_device *phydev;
  31. struct fixed_phy_status status;
  32. int (*link_update)(struct net_device *, struct fixed_phy_status *);
  33. struct list_head node;
  34. };
  35. static struct platform_device *pdev;
  36. static struct fixed_mdio_bus platform_fmb = {
  37. .phys = LIST_HEAD_INIT(platform_fmb.phys),
  38. };
  39. static int fixed_phy_update_regs(struct fixed_phy *fp)
  40. {
  41. u16 bmsr = BMSR_ANEGCAPABLE;
  42. u16 bmcr = 0;
  43. u16 lpagb = 0;
  44. u16 lpa = 0;
  45. if (fp->status.duplex) {
  46. bmcr |= BMCR_FULLDPLX;
  47. switch (fp->status.speed) {
  48. case 1000:
  49. bmsr |= BMSR_ESTATEN;
  50. bmcr |= BMCR_SPEED1000;
  51. lpagb |= LPA_1000FULL;
  52. break;
  53. case 100:
  54. bmsr |= BMSR_100FULL;
  55. bmcr |= BMCR_SPEED100;
  56. lpa |= LPA_100FULL;
  57. break;
  58. case 10:
  59. bmsr |= BMSR_10FULL;
  60. lpa |= LPA_10FULL;
  61. break;
  62. default:
  63. printk(KERN_WARNING "fixed phy: unknown speed\n");
  64. return -EINVAL;
  65. }
  66. } else {
  67. switch (fp->status.speed) {
  68. case 1000:
  69. bmsr |= BMSR_ESTATEN;
  70. bmcr |= BMCR_SPEED1000;
  71. lpagb |= LPA_1000HALF;
  72. break;
  73. case 100:
  74. bmsr |= BMSR_100HALF;
  75. bmcr |= BMCR_SPEED100;
  76. lpa |= LPA_100HALF;
  77. break;
  78. case 10:
  79. bmsr |= BMSR_10HALF;
  80. lpa |= LPA_10HALF;
  81. break;
  82. default:
  83. printk(KERN_WARNING "fixed phy: unknown speed\n");
  84. return -EINVAL;
  85. }
  86. }
  87. if (fp->status.link)
  88. bmsr |= BMSR_LSTATUS | BMSR_ANEGCOMPLETE;
  89. if (fp->status.pause)
  90. lpa |= LPA_PAUSE_CAP;
  91. if (fp->status.asym_pause)
  92. lpa |= LPA_PAUSE_ASYM;
  93. fp->regs[MII_PHYSID1] = fp->id >> 16;
  94. fp->regs[MII_PHYSID2] = fp->id;
  95. fp->regs[MII_BMSR] = bmsr;
  96. fp->regs[MII_BMCR] = bmcr;
  97. fp->regs[MII_LPA] = lpa;
  98. fp->regs[MII_STAT1000] = lpagb;
  99. return 0;
  100. }
  101. static int fixed_mdio_read(struct mii_bus *bus, int phy_id, int reg_num)
  102. {
  103. struct fixed_mdio_bus *fmb = container_of(bus, struct fixed_mdio_bus,
  104. mii_bus);
  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 (!pdev) {
  178. ret = -ENOMEM;
  179. goto err_pdev;
  180. }
  181. snprintf(fmb->mii_bus.id, MII_BUS_ID_SIZE, "0");
  182. fmb->mii_bus.name = "Fixed MDIO Bus";
  183. fmb->mii_bus.dev = &pdev->dev;
  184. fmb->mii_bus.read = &fixed_mdio_read;
  185. fmb->mii_bus.write = &fixed_mdio_write;
  186. fmb->mii_bus.irq = fmb->irqs;
  187. ret = mdiobus_register(&fmb->mii_bus);
  188. if (ret)
  189. goto err_mdiobus_reg;
  190. return 0;
  191. err_mdiobus_reg:
  192. platform_device_unregister(pdev);
  193. err_pdev:
  194. return ret;
  195. }
  196. module_init(fixed_mdio_bus_init);
  197. static void __exit fixed_mdio_bus_exit(void)
  198. {
  199. struct fixed_mdio_bus *fmb = &platform_fmb;
  200. struct fixed_phy *fp, *tmp;
  201. mdiobus_unregister(&fmb->mii_bus);
  202. platform_device_unregister(pdev);
  203. list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
  204. list_del(&fp->node);
  205. kfree(fp);
  206. }
  207. }
  208. module_exit(fixed_mdio_bus_exit);
  209. MODULE_DESCRIPTION("Fixed MDIO bus (MDIO bus emulation with fixed PHYs)");
  210. MODULE_AUTHOR("Vitaly Bordug");
  211. MODULE_LICENSE("GPL");