mii-fec.c 5.5 KB

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