mii-bitbang.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. #ifdef CONFIG_PPC_CPM_NEW_BINDING
  25. #include <linux/of_platform.h>
  26. #endif
  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. #ifdef CONFIG_PPC_CPM_NEW_BINDING
  95. static int __devinit fs_mii_bitbang_init(struct mii_bus *bus,
  96. struct device_node *np)
  97. {
  98. struct resource res;
  99. const u32 *data;
  100. int mdio_pin, mdc_pin, len;
  101. struct bb_info *bitbang = bus->priv;
  102. int ret = of_address_to_resource(np, 0, &res);
  103. if (ret)
  104. return ret;
  105. if (res.end - res.start < 13)
  106. return -ENODEV;
  107. /* This should really encode the pin number as well, but all
  108. * we get is an int, and the odds of multiple bitbang mdio buses
  109. * is low enough that it's not worth going too crazy.
  110. */
  111. bus->id = res.start;
  112. data = of_get_property(np, "fsl,mdio-pin", &len);
  113. if (!data || len != 4)
  114. return -ENODEV;
  115. mdio_pin = *data;
  116. data = of_get_property(np, "fsl,mdc-pin", &len);
  117. if (!data || len != 4)
  118. return -ENODEV;
  119. mdc_pin = *data;
  120. bitbang->dir = ioremap(res.start, res.end - res.start + 1);
  121. if (!bitbang->dir)
  122. return -ENOMEM;
  123. bitbang->dat = bitbang->dir + 4;
  124. bitbang->mdio_msk = 1 << (31 - mdio_pin);
  125. bitbang->mdc_msk = 1 << (31 - mdc_pin);
  126. return 0;
  127. }
  128. static void __devinit add_phy(struct mii_bus *bus, struct device_node *np)
  129. {
  130. const u32 *data;
  131. int len, id, irq;
  132. data = of_get_property(np, "reg", &len);
  133. if (!data || len != 4)
  134. return;
  135. id = *data;
  136. bus->phy_mask &= ~(1 << id);
  137. irq = of_irq_to_resource(np, 0, NULL);
  138. if (irq != NO_IRQ)
  139. bus->irq[id] = irq;
  140. }
  141. static int __devinit fs_enet_mdio_probe(struct of_device *ofdev,
  142. const struct of_device_id *match)
  143. {
  144. struct device_node *np = NULL;
  145. struct mii_bus *new_bus;
  146. struct bb_info *bitbang;
  147. int ret = -ENOMEM;
  148. int i;
  149. bitbang = kzalloc(sizeof(struct bb_info), GFP_KERNEL);
  150. if (!bitbang)
  151. goto out;
  152. bitbang->ctrl.ops = &bb_ops;
  153. new_bus = alloc_mdio_bitbang(&bitbang->ctrl);
  154. if (!new_bus)
  155. goto out_free_priv;
  156. new_bus->name = "CPM2 Bitbanged MII",
  157. ret = fs_mii_bitbang_init(new_bus, ofdev->node);
  158. if (ret)
  159. goto out_free_bus;
  160. new_bus->phy_mask = ~0;
  161. new_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
  162. if (!new_bus->irq)
  163. goto out_unmap_regs;
  164. for (i = 0; i < PHY_MAX_ADDR; i++)
  165. new_bus->irq[i] = -1;
  166. while ((np = of_get_next_child(ofdev->node, np)))
  167. if (!strcmp(np->type, "ethernet-phy"))
  168. add_phy(new_bus, np);
  169. new_bus->dev = &ofdev->dev;
  170. dev_set_drvdata(&ofdev->dev, new_bus);
  171. ret = mdiobus_register(new_bus);
  172. if (ret)
  173. goto out_free_irqs;
  174. return 0;
  175. out_free_irqs:
  176. dev_set_drvdata(&ofdev->dev, NULL);
  177. kfree(new_bus->irq);
  178. out_unmap_regs:
  179. iounmap(bitbang->dir);
  180. out_free_bus:
  181. kfree(new_bus);
  182. out_free_priv:
  183. free_mdio_bitbang(new_bus);
  184. out:
  185. return ret;
  186. }
  187. static int fs_enet_mdio_remove(struct of_device *ofdev)
  188. {
  189. struct mii_bus *bus = dev_get_drvdata(&ofdev->dev);
  190. struct bb_info *bitbang = bus->priv;
  191. mdiobus_unregister(bus);
  192. free_mdio_bitbang(bus);
  193. dev_set_drvdata(&ofdev->dev, NULL);
  194. kfree(bus->irq);
  195. iounmap(bitbang->dir);
  196. kfree(bitbang);
  197. kfree(bus);
  198. return 0;
  199. }
  200. static struct of_device_id fs_enet_mdio_bb_match[] = {
  201. {
  202. .compatible = "fsl,cpm2-mdio-bitbang",
  203. },
  204. {},
  205. };
  206. static struct of_platform_driver fs_enet_bb_mdio_driver = {
  207. .name = "fsl-bb-mdio",
  208. .match_table = fs_enet_mdio_bb_match,
  209. .probe = fs_enet_mdio_probe,
  210. .remove = fs_enet_mdio_remove,
  211. };
  212. static int fs_enet_mdio_bb_init(void)
  213. {
  214. return of_register_platform_driver(&fs_enet_bb_mdio_driver);
  215. }
  216. static void fs_enet_mdio_bb_exit(void)
  217. {
  218. of_unregister_platform_driver(&fs_enet_bb_mdio_driver);
  219. }
  220. module_init(fs_enet_mdio_bb_init);
  221. module_exit(fs_enet_mdio_bb_exit);
  222. #else
  223. static int __devinit fs_mii_bitbang_init(struct bb_info *bitbang,
  224. struct fs_mii_bb_platform_info *fmpi)
  225. {
  226. bitbang->dir = (u32 __iomem *)fmpi->mdio_dir.offset;
  227. bitbang->dat = (u32 __iomem *)fmpi->mdio_dat.offset;
  228. bitbang->mdio_msk = 1U << (31 - fmpi->mdio_dat.bit);
  229. bitbang->mdc_msk = 1U << (31 - fmpi->mdc_dat.bit);
  230. return 0;
  231. }
  232. static int __devinit fs_enet_mdio_probe(struct device *dev)
  233. {
  234. struct platform_device *pdev = to_platform_device(dev);
  235. struct fs_mii_bb_platform_info *pdata;
  236. struct mii_bus *new_bus;
  237. struct bb_info *bitbang;
  238. int err = 0;
  239. if (NULL == dev)
  240. return -EINVAL;
  241. bitbang = kzalloc(sizeof(struct bb_info), GFP_KERNEL);
  242. if (NULL == bitbang)
  243. return -ENOMEM;
  244. bitbang->ctrl.ops = &bb_ops;
  245. new_bus = alloc_mdio_bitbang(&bitbang->ctrl);
  246. if (NULL == new_bus)
  247. return -ENOMEM;
  248. new_bus->name = "BB MII Bus",
  249. new_bus->id = pdev->id;
  250. new_bus->phy_mask = ~0x9;
  251. pdata = (struct fs_mii_bb_platform_info *)pdev->dev.platform_data;
  252. if (NULL == pdata) {
  253. printk(KERN_ERR "gfar mdio %d: Missing platform data!\n", pdev->id);
  254. return -ENODEV;
  255. }
  256. /*set up workspace*/
  257. fs_mii_bitbang_init(bitbang, pdata);
  258. new_bus->priv = bitbang;
  259. new_bus->irq = pdata->irq;
  260. new_bus->dev = dev;
  261. dev_set_drvdata(dev, new_bus);
  262. err = mdiobus_register(new_bus);
  263. if (0 != err) {
  264. printk (KERN_ERR "%s: Cannot register as MDIO bus\n",
  265. new_bus->name);
  266. goto bus_register_fail;
  267. }
  268. return 0;
  269. bus_register_fail:
  270. free_mdio_bitbang(new_bus);
  271. kfree(bitbang);
  272. return err;
  273. }
  274. static int fs_enet_mdio_remove(struct device *dev)
  275. {
  276. struct mii_bus *bus = dev_get_drvdata(dev);
  277. mdiobus_unregister(bus);
  278. dev_set_drvdata(dev, NULL);
  279. free_mdio_bitbang(bus);
  280. return 0;
  281. }
  282. static struct device_driver fs_enet_bb_mdio_driver = {
  283. .name = "fsl-bb-mdio",
  284. .bus = &platform_bus_type,
  285. .probe = fs_enet_mdio_probe,
  286. .remove = fs_enet_mdio_remove,
  287. };
  288. int fs_enet_mdio_bb_init(void)
  289. {
  290. return driver_register(&fs_enet_bb_mdio_driver);
  291. }
  292. void fs_enet_mdio_bb_exit(void)
  293. {
  294. driver_unregister(&fs_enet_bb_mdio_driver);
  295. }
  296. #endif