mii-fec.c 5.4 KB

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