mii-fec.c 5.4 KB

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