mii-fec.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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/types.h>
  16. #include <linux/kernel.h>
  17. #include <linux/string.h>
  18. #include <linux/ptrace.h>
  19. #include <linux/errno.h>
  20. #include <linux/ioport.h>
  21. #include <linux/slab.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/init.h>
  24. #include <linux/delay.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/etherdevice.h>
  27. #include <linux/skbuff.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/mii.h>
  30. #include <linux/ethtool.h>
  31. #include <linux/bitops.h>
  32. #include <linux/platform_device.h>
  33. #include <asm/pgtable.h>
  34. #include <asm/irq.h>
  35. #include <asm/uaccess.h>
  36. #ifdef CONFIG_PPC_CPM_NEW_BINDING
  37. #include <asm/of_platform.h>
  38. #endif
  39. #include "fs_enet.h"
  40. #include "fec.h"
  41. /* Make MII read/write commands for the FEC.
  42. */
  43. #define mk_mii_read(REG) (0x60020000 | ((REG & 0x1f) << 18))
  44. #define mk_mii_write(REG, VAL) (0x50020000 | ((REG & 0x1f) << 18) | (VAL & 0xffff))
  45. #define mk_mii_end 0
  46. #define FEC_MII_LOOPS 10000
  47. #ifndef CONFIG_PPC_CPM_NEW_BINDING
  48. static int match_has_phy (struct device *dev, void* data)
  49. {
  50. struct platform_device* pdev = container_of(dev, struct platform_device, dev);
  51. struct fs_platform_info* fpi;
  52. if(strcmp(pdev->name, (char*)data))
  53. {
  54. return 0;
  55. }
  56. fpi = pdev->dev.platform_data;
  57. if((fpi)&&(fpi->has_phy))
  58. return 1;
  59. return 0;
  60. }
  61. static int fs_mii_fec_init(struct fec_info* fec, struct fs_mii_fec_platform_info *fmpi)
  62. {
  63. struct resource *r;
  64. fec_t __iomem *fecp;
  65. char* name = "fsl-cpm-fec";
  66. /* we need fec in order to be useful */
  67. struct platform_device *fec_pdev =
  68. container_of(bus_find_device(&platform_bus_type, NULL, name, match_has_phy),
  69. struct platform_device, dev);
  70. if(fec_pdev == NULL) {
  71. printk(KERN_ERR"Unable to find PHY for %s", name);
  72. return -ENODEV;
  73. }
  74. r = platform_get_resource_byname(fec_pdev, IORESOURCE_MEM, "regs");
  75. fec->fecp = fecp = ioremap(r->start,sizeof(fec_t));
  76. fec->mii_speed = fmpi->mii_speed;
  77. setbits32(&fecp->fec_r_cntrl, FEC_RCNTRL_MII_MODE); /* MII enable */
  78. setbits32(&fecp->fec_ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN);
  79. out_be32(&fecp->fec_ievent, FEC_ENET_MII);
  80. out_be32(&fecp->fec_mii_speed, fec->mii_speed);
  81. return 0;
  82. }
  83. #endif
  84. static int fs_enet_fec_mii_read(struct mii_bus *bus , int phy_id, int location)
  85. {
  86. struct fec_info* fec = bus->priv;
  87. fec_t __iomem *fecp = fec->fecp;
  88. int i, ret = -1;
  89. if ((in_be32(&fecp->fec_r_cntrl) & FEC_RCNTRL_MII_MODE) == 0)
  90. BUG();
  91. /* Add PHY address to register command. */
  92. out_be32(&fecp->fec_mii_data, (phy_id << 23) | mk_mii_read(location));
  93. for (i = 0; i < FEC_MII_LOOPS; i++)
  94. if ((in_be32(&fecp->fec_ievent) & FEC_ENET_MII) != 0)
  95. break;
  96. if (i < FEC_MII_LOOPS) {
  97. out_be32(&fecp->fec_ievent, FEC_ENET_MII);
  98. ret = in_be32(&fecp->fec_mii_data) & 0xffff;
  99. }
  100. return ret;
  101. }
  102. static int fs_enet_fec_mii_write(struct mii_bus *bus, int phy_id, int location, u16 val)
  103. {
  104. struct fec_info* fec = bus->priv;
  105. fec_t __iomem *fecp = fec->fecp;
  106. int i;
  107. /* this must never happen */
  108. if ((in_be32(&fecp->fec_r_cntrl) & FEC_RCNTRL_MII_MODE) == 0)
  109. BUG();
  110. /* Add PHY address to register command. */
  111. out_be32(&fecp->fec_mii_data, (phy_id << 23) | mk_mii_write(location, val));
  112. for (i = 0; i < FEC_MII_LOOPS; i++)
  113. if ((in_be32(&fecp->fec_ievent) & FEC_ENET_MII) != 0)
  114. break;
  115. if (i < FEC_MII_LOOPS)
  116. out_be32(&fecp->fec_ievent, FEC_ENET_MII);
  117. return 0;
  118. }
  119. static int fs_enet_fec_mii_reset(struct mii_bus *bus)
  120. {
  121. /* nothing here - for now */
  122. return 0;
  123. }
  124. #ifdef CONFIG_PPC_CPM_NEW_BINDING
  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 resource res;
  143. struct mii_bus *new_bus;
  144. struct fec_info *fec;
  145. int ret = -ENOMEM, i;
  146. new_bus = kzalloc(sizeof(struct mii_bus), GFP_KERNEL);
  147. if (!new_bus)
  148. goto out;
  149. fec = kzalloc(sizeof(struct fec_info), GFP_KERNEL);
  150. if (!fec)
  151. goto out_mii;
  152. new_bus->priv = fec;
  153. new_bus->name = "FEC MII Bus";
  154. new_bus->read = &fs_enet_fec_mii_read;
  155. new_bus->write = &fs_enet_fec_mii_write;
  156. new_bus->reset = &fs_enet_fec_mii_reset;
  157. ret = of_address_to_resource(ofdev->node, 0, &res);
  158. if (ret)
  159. return ret;
  160. new_bus->id = res.start;
  161. fec->fecp = ioremap(res.start, res.end - res.start + 1);
  162. if (!fec->fecp)
  163. goto out_fec;
  164. fec->mii_speed = ((ppc_proc_freq + 4999999) / 5000000) << 1;
  165. setbits32(&fec->fecp->fec_r_cntrl, FEC_RCNTRL_MII_MODE);
  166. setbits32(&fec->fecp->fec_ecntrl, FEC_ECNTRL_PINMUX |
  167. FEC_ECNTRL_ETHER_EN);
  168. out_be32(&fec->fecp->fec_ievent, FEC_ENET_MII);
  169. out_be32(&fec->fecp->fec_mii_speed, fec->mii_speed);
  170. new_bus->phy_mask = ~0;
  171. new_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
  172. if (!new_bus->irq)
  173. goto out_unmap_regs;
  174. for (i = 0; i < PHY_MAX_ADDR; i++)
  175. new_bus->irq[i] = -1;
  176. while ((np = of_get_next_child(ofdev->node, np)))
  177. if (!strcmp(np->type, "ethernet-phy"))
  178. add_phy(new_bus, np);
  179. new_bus->dev = &ofdev->dev;
  180. dev_set_drvdata(&ofdev->dev, new_bus);
  181. ret = mdiobus_register(new_bus);
  182. if (ret)
  183. goto out_free_irqs;
  184. return 0;
  185. out_free_irqs:
  186. dev_set_drvdata(&ofdev->dev, NULL);
  187. kfree(new_bus->irq);
  188. out_unmap_regs:
  189. iounmap(fec->fecp);
  190. out_fec:
  191. kfree(fec);
  192. out_mii:
  193. kfree(new_bus);
  194. out:
  195. return ret;
  196. }
  197. static int fs_enet_mdio_remove(struct of_device *ofdev)
  198. {
  199. struct mii_bus *bus = dev_get_drvdata(&ofdev->dev);
  200. struct fec_info *fec = bus->priv;
  201. mdiobus_unregister(bus);
  202. dev_set_drvdata(&ofdev->dev, NULL);
  203. kfree(bus->irq);
  204. iounmap(fec->fecp);
  205. kfree(fec);
  206. kfree(bus);
  207. return 0;
  208. }
  209. static struct of_device_id fs_enet_mdio_fec_match[] = {
  210. {
  211. .compatible = "fsl,pq1-fec-mdio",
  212. },
  213. {},
  214. };
  215. static struct of_platform_driver fs_enet_fec_mdio_driver = {
  216. .name = "fsl-fec-mdio",
  217. .match_table = fs_enet_mdio_fec_match,
  218. .probe = fs_enet_mdio_probe,
  219. .remove = fs_enet_mdio_remove,
  220. };
  221. static int fs_enet_mdio_fec_init(void)
  222. {
  223. return of_register_platform_driver(&fs_enet_fec_mdio_driver);
  224. }
  225. static void fs_enet_mdio_fec_exit(void)
  226. {
  227. of_unregister_platform_driver(&fs_enet_fec_mdio_driver);
  228. }
  229. module_init(fs_enet_mdio_fec_init);
  230. module_exit(fs_enet_mdio_fec_exit);
  231. #else
  232. static int __devinit fs_enet_fec_mdio_probe(struct device *dev)
  233. {
  234. struct platform_device *pdev = to_platform_device(dev);
  235. struct fs_mii_fec_platform_info *pdata;
  236. struct mii_bus *new_bus;
  237. struct fec_info *fec;
  238. int err = 0;
  239. if (NULL == dev)
  240. return -EINVAL;
  241. new_bus = kzalloc(sizeof(struct mii_bus), GFP_KERNEL);
  242. if (NULL == new_bus)
  243. return -ENOMEM;
  244. fec = kzalloc(sizeof(struct fec_info), GFP_KERNEL);
  245. if (NULL == fec)
  246. return -ENOMEM;
  247. new_bus->name = "FEC MII Bus",
  248. new_bus->read = &fs_enet_fec_mii_read,
  249. new_bus->write = &fs_enet_fec_mii_write,
  250. new_bus->reset = &fs_enet_fec_mii_reset,
  251. new_bus->id = pdev->id;
  252. pdata = (struct fs_mii_fec_platform_info *)pdev->dev.platform_data;
  253. if (NULL == pdata) {
  254. printk(KERN_ERR "fs_enet FEC mdio %d: Missing platform data!\n", pdev->id);
  255. return -ENODEV;
  256. }
  257. /*set up workspace*/
  258. fs_mii_fec_init(fec, pdata);
  259. new_bus->priv = fec;
  260. new_bus->irq = pdata->irq;
  261. new_bus->dev = dev;
  262. dev_set_drvdata(dev, new_bus);
  263. err = mdiobus_register(new_bus);
  264. if (0 != err) {
  265. printk (KERN_ERR "%s: Cannot register as MDIO bus\n",
  266. new_bus->name);
  267. goto bus_register_fail;
  268. }
  269. return 0;
  270. bus_register_fail:
  271. kfree(new_bus);
  272. return err;
  273. }
  274. static int fs_enet_fec_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. kfree(bus->priv);
  280. bus->priv = NULL;
  281. kfree(bus);
  282. return 0;
  283. }
  284. static struct device_driver fs_enet_fec_mdio_driver = {
  285. .name = "fsl-cpm-fec-mdio",
  286. .bus = &platform_bus_type,
  287. .probe = fs_enet_fec_mdio_probe,
  288. .remove = fs_enet_fec_mdio_remove,
  289. };
  290. int fs_enet_mdio_fec_init(void)
  291. {
  292. return driver_register(&fs_enet_fec_mdio_driver);
  293. }
  294. void fs_enet_mdio_fec_exit(void)
  295. {
  296. driver_unregister(&fs_enet_fec_mdio_driver);
  297. }
  298. #endif