fixed.c 5.6 KB

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