mii-fec.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 <asm/mpc5xxx.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 fs_enet_fec_mii_read(struct mii_bus *bus , int phy_id, int location)
  47. {
  48. struct fec_info* fec = bus->priv;
  49. struct fec __iomem *fecp = fec->fecp;
  50. int i, ret = -1;
  51. BUG_ON((in_be32(&fecp->fec_r_cntrl) & FEC_RCNTRL_MII_MODE) == 0);
  52. /* Add PHY address to register command. */
  53. out_be32(&fecp->fec_mii_data, (phy_id << 23) | mk_mii_read(location));
  54. for (i = 0; i < FEC_MII_LOOPS; i++)
  55. if ((in_be32(&fecp->fec_ievent) & FEC_ENET_MII) != 0)
  56. break;
  57. if (i < FEC_MII_LOOPS) {
  58. out_be32(&fecp->fec_ievent, FEC_ENET_MII);
  59. ret = in_be32(&fecp->fec_mii_data) & 0xffff;
  60. }
  61. return ret;
  62. }
  63. static int fs_enet_fec_mii_write(struct mii_bus *bus, int phy_id, int location, u16 val)
  64. {
  65. struct fec_info* fec = bus->priv;
  66. struct fec __iomem *fecp = fec->fecp;
  67. int i;
  68. /* this must never happen */
  69. BUG_ON((in_be32(&fecp->fec_r_cntrl) & FEC_RCNTRL_MII_MODE) == 0);
  70. /* Add PHY address to register command. */
  71. out_be32(&fecp->fec_mii_data, (phy_id << 23) | mk_mii_write(location, val));
  72. for (i = 0; i < FEC_MII_LOOPS; i++)
  73. if ((in_be32(&fecp->fec_ievent) & FEC_ENET_MII) != 0)
  74. break;
  75. if (i < FEC_MII_LOOPS)
  76. out_be32(&fecp->fec_ievent, FEC_ENET_MII);
  77. return 0;
  78. }
  79. static int fs_enet_fec_mii_reset(struct mii_bus *bus)
  80. {
  81. /* nothing here - for now */
  82. return 0;
  83. }
  84. static struct of_device_id fs_enet_mdio_fec_match[];
  85. static int __devinit fs_enet_mdio_probe(struct platform_device *ofdev)
  86. {
  87. const struct of_device_id *match;
  88. struct resource res;
  89. struct mii_bus *new_bus;
  90. struct fec_info *fec;
  91. int (*get_bus_freq)(struct device_node *);
  92. int ret = -ENOMEM, clock, speed;
  93. match = of_match_device(fs_enet_mdio_fec_match, &ofdev->dev);
  94. if (!match)
  95. return -EINVAL;
  96. get_bus_freq = match->data;
  97. new_bus = mdiobus_alloc();
  98. if (!new_bus)
  99. goto out;
  100. fec = kzalloc(sizeof(struct fec_info), GFP_KERNEL);
  101. if (!fec)
  102. goto out_mii;
  103. new_bus->priv = fec;
  104. new_bus->name = "FEC MII Bus";
  105. new_bus->read = &fs_enet_fec_mii_read;
  106. new_bus->write = &fs_enet_fec_mii_write;
  107. new_bus->reset = &fs_enet_fec_mii_reset;
  108. ret = of_address_to_resource(ofdev->dev.of_node, 0, &res);
  109. if (ret)
  110. goto out_res;
  111. snprintf(new_bus->id, MII_BUS_ID_SIZE, "%x", res.start);
  112. fec->fecp = ioremap(res.start, res.end - res.start + 1);
  113. if (!fec->fecp)
  114. goto out_fec;
  115. if (get_bus_freq) {
  116. clock = get_bus_freq(ofdev->dev.of_node);
  117. if (!clock) {
  118. /* Use maximum divider if clock is unknown */
  119. dev_warn(&ofdev->dev, "could not determine IPS clock\n");
  120. clock = 0x3F * 5000000;
  121. }
  122. } else
  123. clock = ppc_proc_freq;
  124. /*
  125. * Scale for a MII clock <= 2.5 MHz
  126. * Note that only 6 bits (25:30) are available for MII speed.
  127. */
  128. speed = (clock + 4999999) / 5000000;
  129. if (speed > 0x3F) {
  130. speed = 0x3F;
  131. dev_err(&ofdev->dev,
  132. "MII clock (%d Hz) exceeds max (2.5 MHz)\n",
  133. clock / speed);
  134. }
  135. fec->mii_speed = speed << 1;
  136. setbits32(&fec->fecp->fec_r_cntrl, FEC_RCNTRL_MII_MODE);
  137. setbits32(&fec->fecp->fec_ecntrl, FEC_ECNTRL_PINMUX |
  138. FEC_ECNTRL_ETHER_EN);
  139. out_be32(&fec->fecp->fec_ievent, FEC_ENET_MII);
  140. clrsetbits_be32(&fec->fecp->fec_mii_speed, 0x7E, fec->mii_speed);
  141. new_bus->phy_mask = ~0;
  142. new_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
  143. if (!new_bus->irq)
  144. goto out_unmap_regs;
  145. new_bus->parent = &ofdev->dev;
  146. dev_set_drvdata(&ofdev->dev, new_bus);
  147. ret = of_mdiobus_register(new_bus, ofdev->dev.of_node);
  148. if (ret)
  149. goto out_free_irqs;
  150. return 0;
  151. out_free_irqs:
  152. dev_set_drvdata(&ofdev->dev, NULL);
  153. kfree(new_bus->irq);
  154. out_unmap_regs:
  155. iounmap(fec->fecp);
  156. out_res:
  157. out_fec:
  158. kfree(fec);
  159. out_mii:
  160. mdiobus_free(new_bus);
  161. out:
  162. return ret;
  163. }
  164. static int fs_enet_mdio_remove(struct platform_device *ofdev)
  165. {
  166. struct mii_bus *bus = dev_get_drvdata(&ofdev->dev);
  167. struct fec_info *fec = bus->priv;
  168. mdiobus_unregister(bus);
  169. dev_set_drvdata(&ofdev->dev, NULL);
  170. kfree(bus->irq);
  171. iounmap(fec->fecp);
  172. kfree(fec);
  173. mdiobus_free(bus);
  174. return 0;
  175. }
  176. static struct of_device_id fs_enet_mdio_fec_match[] = {
  177. {
  178. .compatible = "fsl,pq1-fec-mdio",
  179. },
  180. #if defined(CONFIG_PPC_MPC512x)
  181. {
  182. .compatible = "fsl,mpc5121-fec-mdio",
  183. .data = mpc5xxx_get_bus_frequency,
  184. },
  185. #endif
  186. {},
  187. };
  188. MODULE_DEVICE_TABLE(of, fs_enet_mdio_fec_match);
  189. static struct platform_driver fs_enet_fec_mdio_driver = {
  190. .driver = {
  191. .name = "fsl-fec-mdio",
  192. .owner = THIS_MODULE,
  193. .of_match_table = fs_enet_mdio_fec_match,
  194. },
  195. .probe = fs_enet_mdio_probe,
  196. .remove = fs_enet_mdio_remove,
  197. };
  198. static int fs_enet_mdio_fec_init(void)
  199. {
  200. return platform_driver_register(&fs_enet_fec_mdio_driver);
  201. }
  202. static void fs_enet_mdio_fec_exit(void)
  203. {
  204. platform_driver_unregister(&fs_enet_fec_mdio_driver);
  205. }
  206. module_init(fs_enet_mdio_fec_init);
  207. module_exit(fs_enet_mdio_fec_exit);