fixed.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 = bus->priv;
  104. struct fixed_phy *fp;
  105. if (reg_num >= MII_REGS_NUM)
  106. return -1;
  107. list_for_each_entry(fp, &fmb->phys, node) {
  108. if (fp->id == phy_id) {
  109. /* Issue callback if user registered it. */
  110. if (fp->link_update) {
  111. fp->link_update(fp->phydev->attached_dev,
  112. &fp->status);
  113. fixed_phy_update_regs(fp);
  114. }
  115. return fp->regs[reg_num];
  116. }
  117. }
  118. return 0xFFFF;
  119. }
  120. static int fixed_mdio_write(struct mii_bus *bus, int phy_id, int reg_num,
  121. u16 val)
  122. {
  123. return 0;
  124. }
  125. /*
  126. * If something weird is required to be done with link/speed,
  127. * network driver is able to assign a function to implement this.
  128. * May be useful for PHY's that need to be software-driven.
  129. */
  130. int fixed_phy_set_link_update(struct phy_device *phydev,
  131. int (*link_update)(struct net_device *,
  132. struct fixed_phy_status *))
  133. {
  134. struct fixed_mdio_bus *fmb = &platform_fmb;
  135. struct fixed_phy *fp;
  136. if (!link_update || !phydev || !phydev->bus)
  137. return -EINVAL;
  138. list_for_each_entry(fp, &fmb->phys, node) {
  139. if (fp->id == phydev->phy_id) {
  140. fp->link_update = link_update;
  141. fp->phydev = phydev;
  142. return 0;
  143. }
  144. }
  145. return -ENOENT;
  146. }
  147. EXPORT_SYMBOL_GPL(fixed_phy_set_link_update);
  148. int fixed_phy_add(unsigned int irq, int phy_id,
  149. struct fixed_phy_status *status)
  150. {
  151. int ret;
  152. struct fixed_mdio_bus *fmb = &platform_fmb;
  153. struct fixed_phy *fp;
  154. fp = kzalloc(sizeof(*fp), GFP_KERNEL);
  155. if (!fp)
  156. return -ENOMEM;
  157. memset(fp->regs, 0xFF, sizeof(fp->regs[0]) * MII_REGS_NUM);
  158. fmb->irqs[phy_id] = irq;
  159. fp->id = phy_id;
  160. fp->status = *status;
  161. ret = fixed_phy_update_regs(fp);
  162. if (ret)
  163. goto err_regs;
  164. list_add_tail(&fp->node, &fmb->phys);
  165. return 0;
  166. err_regs:
  167. kfree(fp);
  168. return ret;
  169. }
  170. EXPORT_SYMBOL_GPL(fixed_phy_add);
  171. static int __init fixed_mdio_bus_init(void)
  172. {
  173. struct fixed_mdio_bus *fmb = &platform_fmb;
  174. int ret;
  175. pdev = platform_device_register_simple("Fixed MDIO bus", 0, NULL, 0);
  176. if (!pdev) {
  177. ret = -ENOMEM;
  178. goto err_pdev;
  179. }
  180. fmb->mii_bus = mdiobus_alloc();
  181. if (fmb->mii_bus == NULL) {
  182. ret = -ENOMEM;
  183. goto err_mdiobus_reg;
  184. }
  185. snprintf(fmb->mii_bus->id, MII_BUS_ID_SIZE, "0");
  186. fmb->mii_bus->name = "Fixed MDIO Bus";
  187. fmb->mii_bus->priv = fmb;
  188. fmb->mii_bus->parent = &pdev->dev;
  189. fmb->mii_bus->read = &fixed_mdio_read;
  190. fmb->mii_bus->write = &fixed_mdio_write;
  191. fmb->mii_bus->irq = fmb->irqs;
  192. ret = mdiobus_register(fmb->mii_bus);
  193. if (ret)
  194. goto err_mdiobus_alloc;
  195. return 0;
  196. err_mdiobus_alloc:
  197. mdiobus_free(fmb->mii_bus);
  198. err_mdiobus_reg:
  199. platform_device_unregister(pdev);
  200. err_pdev:
  201. return ret;
  202. }
  203. module_init(fixed_mdio_bus_init);
  204. static void __exit fixed_mdio_bus_exit(void)
  205. {
  206. struct fixed_mdio_bus *fmb = &platform_fmb;
  207. struct fixed_phy *fp, *tmp;
  208. mdiobus_unregister(fmb->mii_bus);
  209. mdiobus_free(fmb->mii_bus);
  210. platform_device_unregister(pdev);
  211. list_for_each_entry_safe(fp, tmp, &fmb->phys, node) {
  212. list_del(&fp->node);
  213. kfree(fp);
  214. }
  215. }
  216. module_exit(fixed_mdio_bus_exit);
  217. MODULE_DESCRIPTION("Fixed MDIO bus (MDIO bus emulation with fixed PHYs)");
  218. MODULE_AUTHOR("Vitaly Bordug");
  219. MODULE_LICENSE("GPL");