mii-fec.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 <linux/of_platform.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 fs_enet_fec_mii_read(struct mii_bus *bus , int phy_id, int location)
  46. {
  47. struct fec_info* fec = bus->priv;
  48. fec_t __iomem *fecp = fec->fecp;
  49. int i, ret = -1;
  50. BUG_ON((in_be32(&fecp->fec_r_cntrl) & FEC_RCNTRL_MII_MODE) == 0);
  51. /* Add PHY address to register command. */
  52. out_be32(&fecp->fec_mii_data, (phy_id << 23) | mk_mii_read(location));
  53. for (i = 0; i < FEC_MII_LOOPS; i++)
  54. if ((in_be32(&fecp->fec_ievent) & FEC_ENET_MII) != 0)
  55. break;
  56. if (i < FEC_MII_LOOPS) {
  57. out_be32(&fecp->fec_ievent, FEC_ENET_MII);
  58. ret = in_be32(&fecp->fec_mii_data) & 0xffff;
  59. }
  60. return ret;
  61. }
  62. static int fs_enet_fec_mii_write(struct mii_bus *bus, int phy_id, int location, u16 val)
  63. {
  64. struct fec_info* fec = bus->priv;
  65. fec_t __iomem *fecp = fec->fecp;
  66. int i;
  67. /* this must never happen */
  68. BUG_ON((in_be32(&fecp->fec_r_cntrl) & FEC_RCNTRL_MII_MODE) == 0);
  69. /* Add PHY address to register command. */
  70. out_be32(&fecp->fec_mii_data, (phy_id << 23) | mk_mii_write(location, val));
  71. for (i = 0; i < FEC_MII_LOOPS; i++)
  72. if ((in_be32(&fecp->fec_ievent) & FEC_ENET_MII) != 0)
  73. break;
  74. if (i < FEC_MII_LOOPS)
  75. out_be32(&fecp->fec_ievent, FEC_ENET_MII);
  76. return 0;
  77. }
  78. static int fs_enet_fec_mii_reset(struct mii_bus *bus)
  79. {
  80. /* nothing here - for now */
  81. return 0;
  82. }
  83. static int __devinit fs_enet_mdio_probe(struct of_device *ofdev,
  84. const struct of_device_id *match)
  85. {
  86. struct device_node *np = NULL;
  87. struct resource res;
  88. struct mii_bus *new_bus;
  89. struct fec_info *fec;
  90. int ret = -ENOMEM, i;
  91. new_bus = mdiobus_alloc();
  92. if (!new_bus)
  93. goto out;
  94. fec = kzalloc(sizeof(struct fec_info), GFP_KERNEL);
  95. if (!fec)
  96. goto out_mii;
  97. new_bus->priv = fec;
  98. new_bus->name = "FEC MII Bus";
  99. new_bus->read = &fs_enet_fec_mii_read;
  100. new_bus->write = &fs_enet_fec_mii_write;
  101. new_bus->reset = &fs_enet_fec_mii_reset;
  102. ret = of_address_to_resource(ofdev->node, 0, &res);
  103. if (ret)
  104. goto out_res;
  105. snprintf(new_bus->id, MII_BUS_ID_SIZE, "%x", res.start);
  106. fec->fecp = ioremap(res.start, res.end - res.start + 1);
  107. if (!fec->fecp)
  108. goto out_fec;
  109. fec->mii_speed = ((ppc_proc_freq + 4999999) / 5000000) << 1;
  110. setbits32(&fec->fecp->fec_r_cntrl, FEC_RCNTRL_MII_MODE);
  111. setbits32(&fec->fecp->fec_ecntrl, FEC_ECNTRL_PINMUX |
  112. FEC_ECNTRL_ETHER_EN);
  113. out_be32(&fec->fecp->fec_ievent, FEC_ENET_MII);
  114. out_be32(&fec->fecp->fec_mii_speed, fec->mii_speed);
  115. new_bus->phy_mask = ~0;
  116. new_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
  117. if (!new_bus->irq)
  118. goto out_unmap_regs;
  119. new_bus->parent = &ofdev->dev;
  120. dev_set_drvdata(&ofdev->dev, new_bus);
  121. ret = of_mdiobus_register(new_bus, ofdev->node);
  122. if (ret)
  123. goto out_free_irqs;
  124. return 0;
  125. out_free_irqs:
  126. dev_set_drvdata(&ofdev->dev, NULL);
  127. kfree(new_bus->irq);
  128. out_unmap_regs:
  129. iounmap(fec->fecp);
  130. out_res:
  131. out_fec:
  132. kfree(fec);
  133. out_mii:
  134. mdiobus_free(new_bus);
  135. out:
  136. return ret;
  137. }
  138. static int fs_enet_mdio_remove(struct of_device *ofdev)
  139. {
  140. struct mii_bus *bus = dev_get_drvdata(&ofdev->dev);
  141. struct fec_info *fec = bus->priv;
  142. mdiobus_unregister(bus);
  143. dev_set_drvdata(&ofdev->dev, NULL);
  144. kfree(bus->irq);
  145. iounmap(fec->fecp);
  146. kfree(fec);
  147. mdiobus_free(bus);
  148. return 0;
  149. }
  150. static struct of_device_id fs_enet_mdio_fec_match[] = {
  151. {
  152. .compatible = "fsl,pq1-fec-mdio",
  153. },
  154. {},
  155. };
  156. static struct of_platform_driver fs_enet_fec_mdio_driver = {
  157. .name = "fsl-fec-mdio",
  158. .match_table = fs_enet_mdio_fec_match,
  159. .probe = fs_enet_mdio_probe,
  160. .remove = fs_enet_mdio_remove,
  161. };
  162. static int fs_enet_mdio_fec_init(void)
  163. {
  164. return of_register_platform_driver(&fs_enet_fec_mdio_driver);
  165. }
  166. static void fs_enet_mdio_fec_exit(void)
  167. {
  168. of_unregister_platform_driver(&fs_enet_fec_mdio_driver);
  169. }
  170. module_init(fs_enet_mdio_fec_init);
  171. module_exit(fs_enet_mdio_fec_exit);