mii-bitbang.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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_platform.h>
  25. #include "fs_enet.h"
  26. struct bb_info {
  27. struct mdiobb_ctrl ctrl;
  28. __be32 __iomem *dir;
  29. __be32 __iomem *dat;
  30. u32 mdio_msk;
  31. u32 mdc_msk;
  32. };
  33. /* FIXME: If any other users of GPIO crop up, then these will have to
  34. * have some sort of global synchronization to avoid races with other
  35. * pins on the same port. The ideal solution would probably be to
  36. * bind the ports to a GPIO driver, and have this be a client of it.
  37. */
  38. static inline void bb_set(u32 __iomem *p, u32 m)
  39. {
  40. out_be32(p, in_be32(p) | m);
  41. }
  42. static inline void bb_clr(u32 __iomem *p, u32 m)
  43. {
  44. out_be32(p, in_be32(p) & ~m);
  45. }
  46. static inline int bb_read(u32 __iomem *p, u32 m)
  47. {
  48. return (in_be32(p) & m) != 0;
  49. }
  50. static inline void mdio_dir(struct mdiobb_ctrl *ctrl, int dir)
  51. {
  52. struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
  53. if (dir)
  54. bb_set(bitbang->dir, bitbang->mdio_msk);
  55. else
  56. bb_clr(bitbang->dir, bitbang->mdio_msk);
  57. /* Read back to flush the write. */
  58. in_be32(bitbang->dir);
  59. }
  60. static inline int mdio_read(struct mdiobb_ctrl *ctrl)
  61. {
  62. struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
  63. return bb_read(bitbang->dat, bitbang->mdio_msk);
  64. }
  65. static inline void mdio(struct mdiobb_ctrl *ctrl, int what)
  66. {
  67. struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
  68. if (what)
  69. bb_set(bitbang->dat, bitbang->mdio_msk);
  70. else
  71. bb_clr(bitbang->dat, bitbang->mdio_msk);
  72. /* Read back to flush the write. */
  73. in_be32(bitbang->dat);
  74. }
  75. static inline void mdc(struct mdiobb_ctrl *ctrl, int what)
  76. {
  77. struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
  78. if (what)
  79. bb_set(bitbang->dat, bitbang->mdc_msk);
  80. else
  81. bb_clr(bitbang->dat, bitbang->mdc_msk);
  82. /* Read back to flush the write. */
  83. in_be32(bitbang->dat);
  84. }
  85. static struct mdiobb_ops bb_ops = {
  86. .owner = THIS_MODULE,
  87. .set_mdc = mdc,
  88. .set_mdio_dir = mdio_dir,
  89. .set_mdio_data = mdio,
  90. .get_mdio_data = mdio_read,
  91. };
  92. static int __devinit fs_mii_bitbang_init(struct mii_bus *bus,
  93. struct device_node *np)
  94. {
  95. struct resource res;
  96. const u32 *data;
  97. int mdio_pin, mdc_pin, len;
  98. struct bb_info *bitbang = bus->priv;
  99. int ret = of_address_to_resource(np, 0, &res);
  100. if (ret)
  101. return ret;
  102. if (res.end - res.start < 13)
  103. return -ENODEV;
  104. /* This should really encode the pin number as well, but all
  105. * we get is an int, and the odds of multiple bitbang mdio buses
  106. * is low enough that it's not worth going too crazy.
  107. */
  108. snprintf(bus->id, MII_BUS_ID_SIZE, "%x", res.start);
  109. data = of_get_property(np, "fsl,mdio-pin", &len);
  110. if (!data || len != 4)
  111. return -ENODEV;
  112. mdio_pin = *data;
  113. data = of_get_property(np, "fsl,mdc-pin", &len);
  114. if (!data || len != 4)
  115. return -ENODEV;
  116. mdc_pin = *data;
  117. bitbang->dir = ioremap(res.start, res.end - res.start + 1);
  118. if (!bitbang->dir)
  119. return -ENOMEM;
  120. bitbang->dat = bitbang->dir + 4;
  121. bitbang->mdio_msk = 1 << (31 - mdio_pin);
  122. bitbang->mdc_msk = 1 << (31 - mdc_pin);
  123. return 0;
  124. }
  125. static void __devinit add_phy(struct mii_bus *bus, struct device_node *np)
  126. {
  127. const u32 *data;
  128. int len, id, irq;
  129. data = of_get_property(np, "reg", &len);
  130. if (!data || len != 4)
  131. return;
  132. id = *data;
  133. bus->phy_mask &= ~(1 << id);
  134. irq = of_irq_to_resource(np, 0, NULL);
  135. if (irq != NO_IRQ)
  136. bus->irq[id] = irq;
  137. }
  138. static int __devinit fs_enet_mdio_probe(struct of_device *ofdev,
  139. const struct of_device_id *match)
  140. {
  141. struct device_node *np = NULL;
  142. struct mii_bus *new_bus;
  143. struct bb_info *bitbang;
  144. int ret = -ENOMEM;
  145. int i;
  146. bitbang = kzalloc(sizeof(struct bb_info), GFP_KERNEL);
  147. if (!bitbang)
  148. goto out;
  149. bitbang->ctrl.ops = &bb_ops;
  150. new_bus = alloc_mdio_bitbang(&bitbang->ctrl);
  151. if (!new_bus)
  152. goto out_free_priv;
  153. new_bus->name = "CPM2 Bitbanged MII",
  154. ret = fs_mii_bitbang_init(new_bus, ofdev->node);
  155. if (ret)
  156. goto out_free_bus;
  157. new_bus->phy_mask = ~0;
  158. new_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
  159. if (!new_bus->irq)
  160. goto out_unmap_regs;
  161. for (i = 0; i < PHY_MAX_ADDR; i++)
  162. new_bus->irq[i] = -1;
  163. while ((np = of_get_next_child(ofdev->node, np)))
  164. if (!strcmp(np->type, "ethernet-phy"))
  165. add_phy(new_bus, np);
  166. new_bus->parent = &ofdev->dev;
  167. dev_set_drvdata(&ofdev->dev, new_bus);
  168. ret = mdiobus_register(new_bus);
  169. if (ret)
  170. goto out_free_irqs;
  171. return 0;
  172. out_free_irqs:
  173. dev_set_drvdata(&ofdev->dev, NULL);
  174. kfree(new_bus->irq);
  175. out_unmap_regs:
  176. iounmap(bitbang->dir);
  177. out_free_bus:
  178. free_mdio_bitbang(new_bus);
  179. out_free_priv:
  180. kfree(bitbang);
  181. out:
  182. return ret;
  183. }
  184. static int fs_enet_mdio_remove(struct of_device *ofdev)
  185. {
  186. struct mii_bus *bus = dev_get_drvdata(&ofdev->dev);
  187. struct bb_info *bitbang = bus->priv;
  188. mdiobus_unregister(bus);
  189. dev_set_drvdata(&ofdev->dev, NULL);
  190. kfree(bus->irq);
  191. free_mdio_bitbang(bus);
  192. iounmap(bitbang->dir);
  193. kfree(bitbang);
  194. return 0;
  195. }
  196. static struct of_device_id fs_enet_mdio_bb_match[] = {
  197. {
  198. .compatible = "fsl,cpm2-mdio-bitbang",
  199. },
  200. {},
  201. };
  202. static struct of_platform_driver fs_enet_bb_mdio_driver = {
  203. .name = "fsl-bb-mdio",
  204. .match_table = fs_enet_mdio_bb_match,
  205. .probe = fs_enet_mdio_probe,
  206. .remove = fs_enet_mdio_remove,
  207. };
  208. static int fs_enet_mdio_bb_init(void)
  209. {
  210. return of_register_platform_driver(&fs_enet_bb_mdio_driver);
  211. }
  212. static void fs_enet_mdio_bb_exit(void)
  213. {
  214. of_unregister_platform_driver(&fs_enet_bb_mdio_driver);
  215. }
  216. module_init(fs_enet_mdio_bb_init);
  217. module_exit(fs_enet_mdio_bb_exit);