mii-bitbang.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Combined Ethernet driver for Motorola MPC8xx and MPC82xx.
  3. *
  4. * Copyright (c) 2003 Intracom S.A.
  5. * by Pantelis Antoniou <panto@intracom.gr>
  6. *
  7. * 2005 (c) MontaVista Software, Inc.
  8. * Vitaly Bordug <vbordug@ru.mvista.com>
  9. *
  10. * This file is licensed under the terms of the GNU General Public License
  11. * version 2. This program is licensed "as is" without any warranty of any
  12. * kind, whether express or implied.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/ioport.h>
  16. #include <linux/slab.h>
  17. #include <linux/init.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/netdevice.h>
  20. #include <linux/etherdevice.h>
  21. #include <linux/mii.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/mdio-bitbang.h>
  24. #include <linux/of_mdio.h>
  25. #include <linux/of_platform.h>
  26. #include "fs_enet.h"
  27. struct bb_info {
  28. struct mdiobb_ctrl ctrl;
  29. __be32 __iomem *dir;
  30. __be32 __iomem *dat;
  31. u32 mdio_msk;
  32. u32 mdc_msk;
  33. };
  34. /* FIXME: If any other users of GPIO crop up, then these will have to
  35. * have some sort of global synchronization to avoid races with other
  36. * pins on the same port. The ideal solution would probably be to
  37. * bind the ports to a GPIO driver, and have this be a client of it.
  38. */
  39. static inline void bb_set(u32 __iomem *p, u32 m)
  40. {
  41. out_be32(p, in_be32(p) | m);
  42. }
  43. static inline void bb_clr(u32 __iomem *p, u32 m)
  44. {
  45. out_be32(p, in_be32(p) & ~m);
  46. }
  47. static inline int bb_read(u32 __iomem *p, u32 m)
  48. {
  49. return (in_be32(p) & m) != 0;
  50. }
  51. static inline void mdio_dir(struct mdiobb_ctrl *ctrl, int dir)
  52. {
  53. struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
  54. if (dir)
  55. bb_set(bitbang->dir, bitbang->mdio_msk);
  56. else
  57. bb_clr(bitbang->dir, bitbang->mdio_msk);
  58. /* Read back to flush the write. */
  59. in_be32(bitbang->dir);
  60. }
  61. static inline int mdio_read(struct mdiobb_ctrl *ctrl)
  62. {
  63. struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
  64. return bb_read(bitbang->dat, bitbang->mdio_msk);
  65. }
  66. static inline void mdio(struct mdiobb_ctrl *ctrl, int what)
  67. {
  68. struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
  69. if (what)
  70. bb_set(bitbang->dat, bitbang->mdio_msk);
  71. else
  72. bb_clr(bitbang->dat, bitbang->mdio_msk);
  73. /* Read back to flush the write. */
  74. in_be32(bitbang->dat);
  75. }
  76. static inline void mdc(struct mdiobb_ctrl *ctrl, int what)
  77. {
  78. struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
  79. if (what)
  80. bb_set(bitbang->dat, bitbang->mdc_msk);
  81. else
  82. bb_clr(bitbang->dat, bitbang->mdc_msk);
  83. /* Read back to flush the write. */
  84. in_be32(bitbang->dat);
  85. }
  86. static struct mdiobb_ops bb_ops = {
  87. .owner = THIS_MODULE,
  88. .set_mdc = mdc,
  89. .set_mdio_dir = mdio_dir,
  90. .set_mdio_data = mdio,
  91. .get_mdio_data = mdio_read,
  92. };
  93. static int __devinit fs_mii_bitbang_init(struct mii_bus *bus,
  94. struct device_node *np)
  95. {
  96. struct resource res;
  97. const u32 *data;
  98. int mdio_pin, mdc_pin, len;
  99. struct bb_info *bitbang = bus->priv;
  100. int ret = of_address_to_resource(np, 0, &res);
  101. if (ret)
  102. return ret;
  103. if (res.end - res.start < 13)
  104. return -ENODEV;
  105. /* This should really encode the pin number as well, but all
  106. * we get is an int, and the odds of multiple bitbang mdio buses
  107. * is low enough that it's not worth going too crazy.
  108. */
  109. snprintf(bus->id, MII_BUS_ID_SIZE, "%x", res.start);
  110. data = of_get_property(np, "fsl,mdio-pin", &len);
  111. if (!data || len != 4)
  112. return -ENODEV;
  113. mdio_pin = *data;
  114. data = of_get_property(np, "fsl,mdc-pin", &len);
  115. if (!data || len != 4)
  116. return -ENODEV;
  117. mdc_pin = *data;
  118. bitbang->dir = ioremap(res.start, res.end - res.start + 1);
  119. if (!bitbang->dir)
  120. return -ENOMEM;
  121. bitbang->dat = bitbang->dir + 4;
  122. bitbang->mdio_msk = 1 << (31 - mdio_pin);
  123. bitbang->mdc_msk = 1 << (31 - mdc_pin);
  124. return 0;
  125. }
  126. static int __devinit fs_enet_mdio_probe(struct of_device *ofdev,
  127. const struct of_device_id *match)
  128. {
  129. struct mii_bus *new_bus;
  130. struct bb_info *bitbang;
  131. int ret = -ENOMEM;
  132. bitbang = kzalloc(sizeof(struct bb_info), GFP_KERNEL);
  133. if (!bitbang)
  134. goto out;
  135. bitbang->ctrl.ops = &bb_ops;
  136. new_bus = alloc_mdio_bitbang(&bitbang->ctrl);
  137. if (!new_bus)
  138. goto out_free_priv;
  139. new_bus->name = "CPM2 Bitbanged MII",
  140. ret = fs_mii_bitbang_init(new_bus, ofdev->node);
  141. if (ret)
  142. goto out_free_bus;
  143. new_bus->phy_mask = ~0;
  144. new_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
  145. if (!new_bus->irq)
  146. goto out_unmap_regs;
  147. new_bus->parent = &ofdev->dev;
  148. dev_set_drvdata(&ofdev->dev, new_bus);
  149. ret = of_mdiobus_register(new_bus, ofdev->node);
  150. if (ret)
  151. goto out_free_irqs;
  152. return 0;
  153. out_free_irqs:
  154. dev_set_drvdata(&ofdev->dev, NULL);
  155. kfree(new_bus->irq);
  156. out_unmap_regs:
  157. iounmap(bitbang->dir);
  158. out_free_bus:
  159. free_mdio_bitbang(new_bus);
  160. out_free_priv:
  161. kfree(bitbang);
  162. out:
  163. return ret;
  164. }
  165. static int fs_enet_mdio_remove(struct of_device *ofdev)
  166. {
  167. struct mii_bus *bus = dev_get_drvdata(&ofdev->dev);
  168. struct bb_info *bitbang = bus->priv;
  169. mdiobus_unregister(bus);
  170. dev_set_drvdata(&ofdev->dev, NULL);
  171. kfree(bus->irq);
  172. free_mdio_bitbang(bus);
  173. iounmap(bitbang->dir);
  174. kfree(bitbang);
  175. return 0;
  176. }
  177. static struct of_device_id fs_enet_mdio_bb_match[] = {
  178. {
  179. .compatible = "fsl,cpm2-mdio-bitbang",
  180. },
  181. {},
  182. };
  183. static struct of_platform_driver fs_enet_bb_mdio_driver = {
  184. .name = "fsl-bb-mdio",
  185. .match_table = fs_enet_mdio_bb_match,
  186. .probe = fs_enet_mdio_probe,
  187. .remove = fs_enet_mdio_remove,
  188. };
  189. static int fs_enet_mdio_bb_init(void)
  190. {
  191. return of_register_platform_driver(&fs_enet_bb_mdio_driver);
  192. }
  193. static void fs_enet_mdio_bb_exit(void)
  194. {
  195. of_unregister_platform_driver(&fs_enet_bb_mdio_driver);
  196. }
  197. module_init(fs_enet_mdio_bb_init);
  198. module_exit(fs_enet_mdio_bb_exit);