mii-bitbang.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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_address.h>
  25. #include <linux/of_mdio.h>
  26. #include <linux/of_platform.h>
  27. #include "fs_enet.h"
  28. struct bb_info {
  29. struct mdiobb_ctrl ctrl;
  30. __be32 __iomem *dir;
  31. __be32 __iomem *dat;
  32. u32 mdio_msk;
  33. u32 mdc_msk;
  34. };
  35. /* FIXME: If any other users of GPIO crop up, then these will have to
  36. * have some sort of global synchronization to avoid races with other
  37. * pins on the same port. The ideal solution would probably be to
  38. * bind the ports to a GPIO driver, and have this be a client of it.
  39. */
  40. static inline void bb_set(u32 __iomem *p, u32 m)
  41. {
  42. out_be32(p, in_be32(p) | m);
  43. }
  44. static inline void bb_clr(u32 __iomem *p, u32 m)
  45. {
  46. out_be32(p, in_be32(p) & ~m);
  47. }
  48. static inline int bb_read(u32 __iomem *p, u32 m)
  49. {
  50. return (in_be32(p) & m) != 0;
  51. }
  52. static inline void mdio_dir(struct mdiobb_ctrl *ctrl, int dir)
  53. {
  54. struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
  55. if (dir)
  56. bb_set(bitbang->dir, bitbang->mdio_msk);
  57. else
  58. bb_clr(bitbang->dir, bitbang->mdio_msk);
  59. /* Read back to flush the write. */
  60. in_be32(bitbang->dir);
  61. }
  62. static inline int mdio_read(struct mdiobb_ctrl *ctrl)
  63. {
  64. struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
  65. return bb_read(bitbang->dat, bitbang->mdio_msk);
  66. }
  67. static inline void mdio(struct mdiobb_ctrl *ctrl, int what)
  68. {
  69. struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
  70. if (what)
  71. bb_set(bitbang->dat, bitbang->mdio_msk);
  72. else
  73. bb_clr(bitbang->dat, bitbang->mdio_msk);
  74. /* Read back to flush the write. */
  75. in_be32(bitbang->dat);
  76. }
  77. static inline void mdc(struct mdiobb_ctrl *ctrl, int what)
  78. {
  79. struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
  80. if (what)
  81. bb_set(bitbang->dat, bitbang->mdc_msk);
  82. else
  83. bb_clr(bitbang->dat, bitbang->mdc_msk);
  84. /* Read back to flush the write. */
  85. in_be32(bitbang->dat);
  86. }
  87. static struct mdiobb_ops bb_ops = {
  88. .owner = THIS_MODULE,
  89. .set_mdc = mdc,
  90. .set_mdio_dir = mdio_dir,
  91. .set_mdio_data = mdio,
  92. .get_mdio_data = mdio_read,
  93. };
  94. static int fs_mii_bitbang_init(struct mii_bus *bus, 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 (resource_size(&res) <= 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, resource_size(&res));
  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 fs_enet_mdio_probe(struct platform_device *ofdev)
  127. {
  128. struct mii_bus *new_bus;
  129. struct bb_info *bitbang;
  130. int ret = -ENOMEM;
  131. bitbang = kzalloc(sizeof(struct bb_info), GFP_KERNEL);
  132. if (!bitbang)
  133. goto out;
  134. bitbang->ctrl.ops = &bb_ops;
  135. new_bus = alloc_mdio_bitbang(&bitbang->ctrl);
  136. if (!new_bus)
  137. goto out_free_priv;
  138. new_bus->name = "CPM2 Bitbanged MII",
  139. ret = fs_mii_bitbang_init(new_bus, ofdev->dev.of_node);
  140. if (ret)
  141. goto out_free_bus;
  142. new_bus->phy_mask = ~0;
  143. new_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
  144. if (!new_bus->irq) {
  145. ret = -ENOMEM;
  146. goto out_unmap_regs;
  147. }
  148. new_bus->parent = &ofdev->dev;
  149. platform_set_drvdata(ofdev, new_bus);
  150. ret = of_mdiobus_register(new_bus, ofdev->dev.of_node);
  151. if (ret)
  152. goto out_free_irqs;
  153. return 0;
  154. out_free_irqs:
  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 platform_device *ofdev)
  166. {
  167. struct mii_bus *bus = platform_get_drvdata(ofdev);
  168. struct bb_info *bitbang = bus->priv;
  169. mdiobus_unregister(bus);
  170. kfree(bus->irq);
  171. free_mdio_bitbang(bus);
  172. iounmap(bitbang->dir);
  173. kfree(bitbang);
  174. return 0;
  175. }
  176. static struct of_device_id fs_enet_mdio_bb_match[] = {
  177. {
  178. .compatible = "fsl,cpm2-mdio-bitbang",
  179. },
  180. {},
  181. };
  182. MODULE_DEVICE_TABLE(of, fs_enet_mdio_bb_match);
  183. static struct platform_driver fs_enet_bb_mdio_driver = {
  184. .driver = {
  185. .name = "fsl-bb-mdio",
  186. .owner = THIS_MODULE,
  187. .of_match_table = fs_enet_mdio_bb_match,
  188. },
  189. .probe = fs_enet_mdio_probe,
  190. .remove = fs_enet_mdio_remove,
  191. };
  192. module_platform_driver(fs_enet_bb_mdio_driver);