mii-fec.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/config.h>
  15. #include <linux/module.h>
  16. #include <linux/types.h>
  17. #include <linux/kernel.h>
  18. #include <linux/sched.h>
  19. #include <linux/string.h>
  20. #include <linux/ptrace.h>
  21. #include <linux/errno.h>
  22. #include <linux/ioport.h>
  23. #include <linux/slab.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/pci.h>
  26. #include <linux/init.h>
  27. #include <linux/delay.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/etherdevice.h>
  30. #include <linux/skbuff.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/mii.h>
  33. #include <linux/ethtool.h>
  34. #include <linux/bitops.h>
  35. #include <linux/platform_device.h>
  36. #include <asm/pgtable.h>
  37. #include <asm/irq.h>
  38. #include <asm/uaccess.h>
  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. static int match_has_phy (struct device *dev, void* data)
  48. {
  49. struct platform_device* pdev = container_of(dev, struct platform_device, dev);
  50. struct fs_platform_info* fpi;
  51. if(strcmp(pdev->name, (char*)data))
  52. {
  53. return 0;
  54. }
  55. fpi = pdev->dev.platform_data;
  56. if((fpi)&&(fpi->has_phy))
  57. return 1;
  58. return 0;
  59. }
  60. static int fs_mii_fec_init(struct fec_info* fec, struct fs_mii_fec_platform_info *fmpi)
  61. {
  62. struct resource *r;
  63. fec_t *fecp;
  64. char* name = "fsl-cpm-fec";
  65. /* we need fec in order to be useful */
  66. struct platform_device *fec_pdev =
  67. container_of(bus_find_device(&platform_bus_type, NULL, name, match_has_phy),
  68. struct platform_device, dev);
  69. if(fec_pdev == NULL) {
  70. printk(KERN_ERR"Unable to find PHY for %s", name);
  71. return -ENODEV;
  72. }
  73. r = platform_get_resource_byname(fec_pdev, IORESOURCE_MEM, "regs");
  74. fec->fecp = fecp = (fec_t*)ioremap(r->start,sizeof(fec_t));
  75. fec->mii_speed = fmpi->mii_speed;
  76. setbits32(&fecp->fec_r_cntrl, FEC_RCNTRL_MII_MODE); /* MII enable */
  77. setbits32(&fecp->fec_ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN);
  78. out_be32(&fecp->fec_ievent, FEC_ENET_MII);
  79. out_be32(&fecp->fec_mii_speed, fec->mii_speed);
  80. return 0;
  81. }
  82. static int fs_enet_fec_mii_read(struct mii_bus *bus , int phy_id, int location)
  83. {
  84. struct fec_info* fec = bus->priv;
  85. fec_t *fecp = fec->fecp;
  86. int i, ret = -1;
  87. if ((in_be32(&fecp->fec_r_cntrl) & FEC_RCNTRL_MII_MODE) == 0)
  88. BUG();
  89. /* Add PHY address to register command. */
  90. out_be32(&fecp->fec_mii_data, (phy_id << 23) | mk_mii_read(location));
  91. for (i = 0; i < FEC_MII_LOOPS; i++)
  92. if ((in_be32(&fecp->fec_ievent) & FEC_ENET_MII) != 0)
  93. break;
  94. if (i < FEC_MII_LOOPS) {
  95. out_be32(&fecp->fec_ievent, FEC_ENET_MII);
  96. ret = in_be32(&fecp->fec_mii_data) & 0xffff;
  97. }
  98. return ret;
  99. }
  100. static int fs_enet_fec_mii_write(struct mii_bus *bus, int phy_id, int location, u16 val)
  101. {
  102. struct fec_info* fec = bus->priv;
  103. fec_t *fecp = fec->fecp;
  104. int i;
  105. /* this must never happen */
  106. if ((in_be32(&fecp->fec_r_cntrl) & FEC_RCNTRL_MII_MODE) == 0)
  107. BUG();
  108. /* Add PHY address to register command. */
  109. out_be32(&fecp->fec_mii_data, (phy_id << 23) | mk_mii_write(location, val));
  110. for (i = 0; i < FEC_MII_LOOPS; i++)
  111. if ((in_be32(&fecp->fec_ievent) & FEC_ENET_MII) != 0)
  112. break;
  113. if (i < FEC_MII_LOOPS)
  114. out_be32(&fecp->fec_ievent, FEC_ENET_MII);
  115. return 0;
  116. }
  117. static int fs_enet_fec_mii_reset(struct mii_bus *bus)
  118. {
  119. /* nothing here - for now */
  120. return 0;
  121. }
  122. static int __devinit fs_enet_fec_mdio_probe(struct device *dev)
  123. {
  124. struct platform_device *pdev = to_platform_device(dev);
  125. struct fs_mii_fec_platform_info *pdata;
  126. struct mii_bus *new_bus;
  127. struct fec_info *fec;
  128. int err = 0;
  129. if (NULL == dev)
  130. return -EINVAL;
  131. new_bus = kzalloc(sizeof(struct mii_bus), GFP_KERNEL);
  132. if (NULL == new_bus)
  133. return -ENOMEM;
  134. fec = kzalloc(sizeof(struct fec_info), GFP_KERNEL);
  135. if (NULL == fec)
  136. return -ENOMEM;
  137. new_bus->name = "FEC MII Bus",
  138. new_bus->read = &fs_enet_fec_mii_read,
  139. new_bus->write = &fs_enet_fec_mii_write,
  140. new_bus->reset = &fs_enet_fec_mii_reset,
  141. new_bus->id = pdev->id;
  142. pdata = (struct fs_mii_fec_platform_info *)pdev->dev.platform_data;
  143. if (NULL == pdata) {
  144. printk(KERN_ERR "fs_enet FEC mdio %d: Missing platform data!\n", pdev->id);
  145. return -ENODEV;
  146. }
  147. /*set up workspace*/
  148. fs_mii_fec_init(fec, pdata);
  149. new_bus->priv = fec;
  150. new_bus->irq = pdata->irq;
  151. new_bus->dev = dev;
  152. dev_set_drvdata(dev, new_bus);
  153. err = mdiobus_register(new_bus);
  154. if (0 != err) {
  155. printk (KERN_ERR "%s: Cannot register as MDIO bus\n",
  156. new_bus->name);
  157. goto bus_register_fail;
  158. }
  159. return 0;
  160. bus_register_fail:
  161. kfree(new_bus);
  162. return err;
  163. }
  164. static int fs_enet_fec_mdio_remove(struct device *dev)
  165. {
  166. struct mii_bus *bus = dev_get_drvdata(dev);
  167. mdiobus_unregister(bus);
  168. dev_set_drvdata(dev, NULL);
  169. kfree(bus->priv);
  170. bus->priv = NULL;
  171. kfree(bus);
  172. return 0;
  173. }
  174. static struct device_driver fs_enet_fec_mdio_driver = {
  175. .name = "fsl-cpm-fec-mdio",
  176. .bus = &platform_bus_type,
  177. .probe = fs_enet_fec_mdio_probe,
  178. .remove = fs_enet_fec_mdio_remove,
  179. };
  180. int fs_enet_mdio_fec_init(void)
  181. {
  182. return driver_register(&fs_enet_fec_mdio_driver);
  183. }
  184. void fs_enet_mdio_fec_exit(void)
  185. {
  186. driver_unregister(&fs_enet_fec_mdio_driver);
  187. }