fec_mpc52xx_phy.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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/slab.h>
  17. #include <linux/of_mdio.h>
  18. #include <asm/io.h>
  19. #include <asm/mpc52xx.h>
  20. #include "fec_mpc52xx.h"
  21. struct mpc52xx_fec_mdio_priv {
  22. struct mpc52xx_fec __iomem *regs;
  23. int mdio_irqs[PHY_MAX_ADDR];
  24. };
  25. static int mpc52xx_fec_mdio_transfer(struct mii_bus *bus, int phy_id,
  26. int reg, u32 value)
  27. {
  28. struct mpc52xx_fec_mdio_priv *priv = bus->priv;
  29. struct mpc52xx_fec __iomem *fec = priv->regs;
  30. int tries = 3;
  31. value |= (phy_id << FEC_MII_DATA_PA_SHIFT) & FEC_MII_DATA_PA_MSK;
  32. value |= (reg << FEC_MII_DATA_RA_SHIFT) & FEC_MII_DATA_RA_MSK;
  33. out_be32(&fec->ievent, FEC_IEVENT_MII);
  34. out_be32(&fec->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(&fec->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 platform_device *of,
  54. const struct of_device_id *match)
  55. {
  56. struct device *dev = &of->dev;
  57. struct device_node *np = of->dev.of_node;
  58. struct mii_bus *bus;
  59. struct mpc52xx_fec_mdio_priv *priv;
  60. struct resource res;
  61. int err;
  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 = priv->mdio_irqs;
  75. /* setup registers */
  76. err = of_address_to_resource(np, 0, &res);
  77. if (err)
  78. goto out_free;
  79. priv->regs = ioremap(res.start, resource_size(&res));
  80. if (priv->regs == NULL) {
  81. err = -ENOMEM;
  82. goto out_free;
  83. }
  84. snprintf(bus->id, MII_BUS_ID_SIZE, "%x", res.start);
  85. bus->priv = priv;
  86. bus->parent = dev;
  87. dev_set_drvdata(dev, bus);
  88. /* set MII speed */
  89. out_be32(&priv->regs->mii_speed,
  90. ((mpc5xxx_get_bus_frequency(of->dev.of_node) >> 20) / 5) << 1);
  91. err = of_mdiobus_register(bus, np);
  92. if (err)
  93. goto out_unmap;
  94. return 0;
  95. out_unmap:
  96. iounmap(priv->regs);
  97. out_free:
  98. kfree(priv);
  99. mdiobus_free(bus);
  100. return err;
  101. }
  102. static int mpc52xx_fec_mdio_remove(struct platform_device *of)
  103. {
  104. struct device *dev = &of->dev;
  105. struct mii_bus *bus = dev_get_drvdata(dev);
  106. struct mpc52xx_fec_mdio_priv *priv = bus->priv;
  107. mdiobus_unregister(bus);
  108. dev_set_drvdata(dev, NULL);
  109. iounmap(priv->regs);
  110. kfree(priv);
  111. mdiobus_free(bus);
  112. return 0;
  113. }
  114. static struct of_device_id mpc52xx_fec_mdio_match[] = {
  115. { .compatible = "fsl,mpc5200b-mdio", },
  116. { .compatible = "fsl,mpc5200-mdio", },
  117. { .compatible = "mpc5200b-fec-phy", },
  118. {}
  119. };
  120. MODULE_DEVICE_TABLE(of, mpc52xx_fec_mdio_match);
  121. struct of_platform_driver mpc52xx_fec_mdio_driver = {
  122. .driver = {
  123. .name = "mpc5200b-fec-phy",
  124. .owner = THIS_MODULE,
  125. .of_match_table = mpc52xx_fec_mdio_match,
  126. },
  127. .probe = mpc52xx_fec_mdio_probe,
  128. .remove = mpc52xx_fec_mdio_remove,
  129. };
  130. /* let fec driver call it, since this has to be registered before it */
  131. EXPORT_SYMBOL_GPL(mpc52xx_fec_mdio_driver);
  132. MODULE_LICENSE("Dual BSD/GPL");