/drivers/net/ethernet/freescale/fs_enet/mii-fec.c

http://github.com/mirrors/linux · C · 226 lines · 162 code · 41 blank · 23 comment · 19 complexity · 9d670a3599ce734c34cc14b36b68c5b1 MD5 · raw file

  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/delay.h>
  24. #include <linux/netdevice.h>
  25. #include <linux/etherdevice.h>
  26. #include <linux/skbuff.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/mii.h>
  29. #include <linux/ethtool.h>
  30. #include <linux/bitops.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/of_address.h>
  33. #include <linux/of_platform.h>
  34. #include <asm/pgtable.h>
  35. #include <asm/irq.h>
  36. #include <linux/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 const struct of_device_id fs_enet_mdio_fec_match[];
  80. static int fs_enet_mdio_probe(struct platform_device *ofdev)
  81. {
  82. const struct of_device_id *match;
  83. struct resource res;
  84. struct mii_bus *new_bus;
  85. struct fec_info *fec;
  86. int (*get_bus_freq)(struct device_node *);
  87. int ret = -ENOMEM, clock, speed;
  88. match = of_match_device(fs_enet_mdio_fec_match, &ofdev->dev);
  89. if (!match)
  90. return -EINVAL;
  91. get_bus_freq = match->data;
  92. new_bus = mdiobus_alloc();
  93. if (!new_bus)
  94. goto out;
  95. fec = kzalloc(sizeof(struct fec_info), GFP_KERNEL);
  96. if (!fec)
  97. goto out_mii;
  98. new_bus->priv = fec;
  99. new_bus->name = "FEC MII Bus";
  100. new_bus->read = &fs_enet_fec_mii_read;
  101. new_bus->write = &fs_enet_fec_mii_write;
  102. ret = of_address_to_resource(ofdev->dev.of_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, resource_size(&res));
  107. if (!fec->fecp) {
  108. ret = -ENOMEM;
  109. goto out_fec;
  110. }
  111. if (get_bus_freq) {
  112. clock = get_bus_freq(ofdev->dev.of_node);
  113. if (!clock) {
  114. /* Use maximum divider if clock is unknown */
  115. dev_warn(&ofdev->dev, "could not determine IPS clock\n");
  116. clock = 0x3F * 5000000;
  117. }
  118. } else
  119. clock = ppc_proc_freq;
  120. /*
  121. * Scale for a MII clock <= 2.5 MHz
  122. * Note that only 6 bits (25:30) are available for MII speed.
  123. */
  124. speed = (clock + 4999999) / 5000000;
  125. if (speed > 0x3F) {
  126. speed = 0x3F;
  127. dev_err(&ofdev->dev,
  128. "MII clock (%d Hz) exceeds max (2.5 MHz)\n",
  129. clock / speed);
  130. }
  131. fec->mii_speed = speed << 1;
  132. setbits32(&fec->fecp->fec_r_cntrl, FEC_RCNTRL_MII_MODE);
  133. setbits32(&fec->fecp->fec_ecntrl, FEC_ECNTRL_PINMUX |
  134. FEC_ECNTRL_ETHER_EN);
  135. out_be32(&fec->fecp->fec_ievent, FEC_ENET_MII);
  136. clrsetbits_be32(&fec->fecp->fec_mii_speed, 0x7E, fec->mii_speed);
  137. new_bus->phy_mask = ~0;
  138. new_bus->parent = &ofdev->dev;
  139. platform_set_drvdata(ofdev, new_bus);
  140. ret = of_mdiobus_register(new_bus, ofdev->dev.of_node);
  141. if (ret)
  142. goto out_unmap_regs;
  143. return 0;
  144. out_unmap_regs:
  145. iounmap(fec->fecp);
  146. out_res:
  147. out_fec:
  148. kfree(fec);
  149. out_mii:
  150. mdiobus_free(new_bus);
  151. out:
  152. return ret;
  153. }
  154. static int fs_enet_mdio_remove(struct platform_device *ofdev)
  155. {
  156. struct mii_bus *bus = platform_get_drvdata(ofdev);
  157. struct fec_info *fec = bus->priv;
  158. mdiobus_unregister(bus);
  159. iounmap(fec->fecp);
  160. kfree(fec);
  161. mdiobus_free(bus);
  162. return 0;
  163. }
  164. static const struct of_device_id fs_enet_mdio_fec_match[] = {
  165. {
  166. .compatible = "fsl,pq1-fec-mdio",
  167. },
  168. #if defined(CONFIG_PPC_MPC512x)
  169. {
  170. .compatible = "fsl,mpc5121-fec-mdio",
  171. .data = mpc5xxx_get_bus_frequency,
  172. },
  173. #endif
  174. {},
  175. };
  176. MODULE_DEVICE_TABLE(of, fs_enet_mdio_fec_match);
  177. static struct platform_driver fs_enet_fec_mdio_driver = {
  178. .driver = {
  179. .name = "fsl-fec-mdio",
  180. .of_match_table = fs_enet_mdio_fec_match,
  181. },
  182. .probe = fs_enet_mdio_probe,
  183. .remove = fs_enet_mdio_remove,
  184. };
  185. module_platform_driver(fs_enet_fec_mdio_driver);