fec_mpc52xx_phy.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 <linux/of_mdio.h>
  17. #include <asm/io.h>
  18. #include <asm/mpc52xx.h>
  19. #include "fec_mpc52xx.h"
  20. struct mpc52xx_fec_mdio_priv {
  21. struct mpc52xx_fec __iomem *regs;
  22. int mdio_irqs[PHY_MAX_ADDR];
  23. };
  24. static int mpc52xx_fec_mdio_transfer(struct mii_bus *bus, int phy_id,
  25. int reg, u32 value)
  26. {
  27. struct mpc52xx_fec_mdio_priv *priv = bus->priv;
  28. struct mpc52xx_fec __iomem *fec;
  29. int tries = 3;
  30. value |= (phy_id << FEC_MII_DATA_PA_SHIFT) & FEC_MII_DATA_PA_MSK;
  31. value |= (reg << FEC_MII_DATA_RA_SHIFT) & FEC_MII_DATA_RA_MSK;
  32. fec = priv->regs;
  33. out_be32(&fec->ievent, FEC_IEVENT_MII);
  34. out_be32(&priv->regs->mii_data, value);
  35. /* wait for it to finish, this takes about 23 us on lite5200b */
  36. while (!(in_be32(&fec->ievent) & FEC_IEVENT_MII) && --tries)
  37. msleep(1);
  38. if (!tries)
  39. return -ETIMEDOUT;
  40. return value & FEC_MII_DATA_OP_RD ?
  41. in_be32(&priv->regs->mii_data) & FEC_MII_DATA_DATAMSK : 0;
  42. }
  43. static int mpc52xx_fec_mdio_read(struct mii_bus *bus, int phy_id, int reg)
  44. {
  45. return mpc52xx_fec_mdio_transfer(bus, phy_id, reg, FEC_MII_READ_FRAME);
  46. }
  47. static int mpc52xx_fec_mdio_write(struct mii_bus *bus, int phy_id, int reg,
  48. u16 data)
  49. {
  50. return mpc52xx_fec_mdio_transfer(bus, phy_id, reg,
  51. data | FEC_MII_WRITE_FRAME);
  52. }
  53. static int mpc52xx_fec_mdio_probe(struct of_device *of,
  54. const struct of_device_id *match)
  55. {
  56. struct device *dev = &of->dev;
  57. struct device_node *np = of->node;
  58. struct mii_bus *bus;
  59. struct mpc52xx_fec_mdio_priv *priv;
  60. struct resource res = {};
  61. int err;
  62. int i;
  63. bus = mdiobus_alloc();
  64. if (bus == NULL)
  65. return -ENOMEM;
  66. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  67. if (priv == NULL) {
  68. err = -ENOMEM;
  69. goto out_free;
  70. }
  71. bus->name = "mpc52xx MII bus";
  72. bus->read = mpc52xx_fec_mdio_read;
  73. bus->write = mpc52xx_fec_mdio_write;
  74. /* setup irqs */
  75. bus->irq = priv->mdio_irqs;
  76. /* setup registers */
  77. err = of_address_to_resource(np, 0, &res);
  78. if (err)
  79. goto out_free;
  80. priv->regs = ioremap(res.start, res.end - res.start + 1);
  81. if (priv->regs == NULL) {
  82. err = -ENOMEM;
  83. goto out_free;
  84. }
  85. snprintf(bus->id, MII_BUS_ID_SIZE, "%x", res.start);
  86. bus->priv = priv;
  87. bus->parent = dev;
  88. dev_set_drvdata(dev, bus);
  89. /* set MII speed */
  90. out_be32(&priv->regs->mii_speed,
  91. ((mpc5xxx_get_bus_frequency(of->node) >> 20) / 5) << 1);
  92. err = of_mdiobus_register(bus, np);
  93. if (err)
  94. goto out_unmap;
  95. return 0;
  96. out_unmap:
  97. iounmap(priv->regs);
  98. out_free:
  99. for (i=0; i<PHY_MAX_ADDR; i++)
  100. if (bus->irq[i] != PHY_POLL)
  101. irq_dispose_mapping(bus->irq[i]);
  102. kfree(bus->irq);
  103. kfree(priv);
  104. mdiobus_free(bus);
  105. return err;
  106. }
  107. static int mpc52xx_fec_mdio_remove(struct of_device *of)
  108. {
  109. struct device *dev = &of->dev;
  110. struct mii_bus *bus = dev_get_drvdata(dev);
  111. struct mpc52xx_fec_mdio_priv *priv = bus->priv;
  112. int i;
  113. mdiobus_unregister(bus);
  114. dev_set_drvdata(dev, NULL);
  115. iounmap(priv->regs);
  116. for (i=0; i<PHY_MAX_ADDR; i++)
  117. if (bus->irq[i] != PHY_POLL)
  118. irq_dispose_mapping(bus->irq[i]);
  119. kfree(priv);
  120. kfree(bus->irq);
  121. mdiobus_free(bus);
  122. return 0;
  123. }
  124. static struct of_device_id mpc52xx_fec_mdio_match[] = {
  125. { .compatible = "fsl,mpc5200b-mdio", },
  126. { .compatible = "fsl,mpc5200-mdio", },
  127. { .compatible = "mpc5200b-fec-phy", },
  128. {}
  129. };
  130. struct of_platform_driver mpc52xx_fec_mdio_driver = {
  131. .name = "mpc5200b-fec-phy",
  132. .probe = mpc52xx_fec_mdio_probe,
  133. .remove = mpc52xx_fec_mdio_remove,
  134. .match_table = mpc52xx_fec_mdio_match,
  135. };
  136. /* let fec driver call it, since this has to be registered before it */
  137. EXPORT_SYMBOL_GPL(mpc52xx_fec_mdio_driver);
  138. MODULE_LICENSE("Dual BSD/GPL");