fec_mpc52xx_phy.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Driver for the MPC5200 Fast Ethernet Controller - MDIO bus driver
  3. *
  4. * Copyright (C) 2007 Domen Puncer, Telargo, Inc.
  5. * Copyright (C) 2008 Wolfram Sang, Pengutronix
  6. *
  7. * This file is licensed under the terms of the GNU General Public License
  8. * version 2. This program is licensed "as is" without any warranty of any
  9. * kind, whether express or implied.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/phy.h>
  15. #include <linux/of_platform.h>
  16. #include <asm/io.h>
  17. #include <asm/mpc52xx.h>
  18. #include "fec_mpc52xx.h"
  19. struct mpc52xx_fec_mdio_priv {
  20. struct mpc52xx_fec __iomem *regs;
  21. };
  22. static int mpc52xx_fec_mdio_transfer(struct mii_bus *bus, int phy_id,
  23. int reg, u32 value)
  24. {
  25. struct mpc52xx_fec_mdio_priv *priv = bus->priv;
  26. struct mpc52xx_fec __iomem *fec;
  27. int tries = 100;
  28. value |= (phy_id << FEC_MII_DATA_PA_SHIFT) & FEC_MII_DATA_PA_MSK;
  29. value |= (reg << FEC_MII_DATA_RA_SHIFT) & FEC_MII_DATA_RA_MSK;
  30. fec = priv->regs;
  31. out_be32(&fec->ievent, FEC_IEVENT_MII);
  32. out_be32(&priv->regs->mii_data, value);
  33. /* wait for it to finish, this takes about 23 us on lite5200b */
  34. while (!(in_be32(&fec->ievent) & FEC_IEVENT_MII) && --tries)
  35. udelay(5);
  36. if (!tries)
  37. return -ETIMEDOUT;
  38. return value & FEC_MII_DATA_OP_RD ?
  39. in_be32(&priv->regs->mii_data) & FEC_MII_DATA_DATAMSK : 0;
  40. }
  41. static int mpc52xx_fec_mdio_read(struct mii_bus *bus, int phy_id, int reg)
  42. {
  43. return mpc52xx_fec_mdio_transfer(bus, phy_id, reg, FEC_MII_READ_FRAME);
  44. }
  45. static int mpc52xx_fec_mdio_write(struct mii_bus *bus, int phy_id, int reg,
  46. u16 data)
  47. {
  48. return mpc52xx_fec_mdio_transfer(bus, phy_id, reg,
  49. data | FEC_MII_WRITE_FRAME);
  50. }
  51. static int mpc52xx_fec_mdio_probe(struct of_device *of,
  52. const struct of_device_id *match)
  53. {
  54. struct device *dev = &of->dev;
  55. struct device_node *np = of->node;
  56. struct device_node *child = NULL;
  57. struct mii_bus *bus;
  58. struct mpc52xx_fec_mdio_priv *priv;
  59. struct resource res = {};
  60. int err;
  61. int i;
  62. bus = mdiobus_alloc();
  63. if (bus == NULL)
  64. return -ENOMEM;
  65. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  66. if (priv == NULL) {
  67. err = -ENOMEM;
  68. goto out_free;
  69. }
  70. bus->name = "mpc52xx MII bus";
  71. bus->read = mpc52xx_fec_mdio_read;
  72. bus->write = mpc52xx_fec_mdio_write;
  73. /* setup irqs */
  74. bus->irq = kmalloc(sizeof(bus->irq[0]) * PHY_MAX_ADDR, GFP_KERNEL);
  75. if (bus->irq == NULL) {
  76. err = -ENOMEM;
  77. goto out_free;
  78. }
  79. for (i=0; i<PHY_MAX_ADDR; i++)
  80. bus->irq[i] = PHY_POLL;
  81. while ((child = of_get_next_child(np, child)) != NULL) {
  82. int irq = irq_of_parse_and_map(child, 0);
  83. if (irq != NO_IRQ) {
  84. const u32 *id = of_get_property(child, "reg", NULL);
  85. if (id)
  86. bus->irq[*id] = irq;
  87. }
  88. }
  89. /* setup registers */
  90. err = of_address_to_resource(np, 0, &res);
  91. if (err)
  92. goto out_free;
  93. priv->regs = ioremap(res.start, res.end - res.start + 1);
  94. if (priv->regs == NULL) {
  95. err = -ENOMEM;
  96. goto out_free;
  97. }
  98. snprintf(bus->id, MII_BUS_ID_SIZE, "%x", res.start);
  99. bus->priv = priv;
  100. bus->parent = dev;
  101. dev_set_drvdata(dev, bus);
  102. /* set MII speed */
  103. out_be32(&priv->regs->mii_speed,
  104. ((mpc52xx_find_ipb_freq(of->node) >> 20) / 5) << 1);
  105. err = mdiobus_register(bus);
  106. if (err)
  107. goto out_unmap;
  108. return 0;
  109. out_unmap:
  110. iounmap(priv->regs);
  111. out_free:
  112. for (i=0; i<PHY_MAX_ADDR; i++)
  113. if (bus->irq[i] != PHY_POLL)
  114. irq_dispose_mapping(bus->irq[i]);
  115. kfree(bus->irq);
  116. kfree(priv);
  117. mdiobus_free(bus);
  118. return err;
  119. }
  120. static int mpc52xx_fec_mdio_remove(struct of_device *of)
  121. {
  122. struct device *dev = &of->dev;
  123. struct mii_bus *bus = dev_get_drvdata(dev);
  124. struct mpc52xx_fec_mdio_priv *priv = bus->priv;
  125. int i;
  126. mdiobus_unregister(bus);
  127. dev_set_drvdata(dev, NULL);
  128. iounmap(priv->regs);
  129. for (i=0; i<PHY_MAX_ADDR; i++)
  130. if (bus->irq[i] != PHY_POLL)
  131. irq_dispose_mapping(bus->irq[i]);
  132. kfree(priv);
  133. kfree(bus->irq);
  134. mdiobus_free(bus);
  135. return 0;
  136. }
  137. static struct of_device_id mpc52xx_fec_mdio_match[] = {
  138. { .compatible = "fsl,mpc5200b-mdio", },
  139. { .compatible = "fsl,mpc5200-mdio", },
  140. { .compatible = "mpc5200b-fec-phy", },
  141. {}
  142. };
  143. struct of_platform_driver mpc52xx_fec_mdio_driver = {
  144. .name = "mpc5200b-fec-phy",
  145. .probe = mpc52xx_fec_mdio_probe,
  146. .remove = mpc52xx_fec_mdio_remove,
  147. .match_table = mpc52xx_fec_mdio_match,
  148. };
  149. /* let fec driver call it, since this has to be registered before it */
  150. EXPORT_SYMBOL_GPL(mpc52xx_fec_mdio_driver);
  151. MODULE_LICENSE("Dual BSD/GPL");