fec_mpc52xx_phy.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. {
  55. struct device *dev = &of->dev;
  56. struct device_node *np = of->dev.of_node;
  57. struct mii_bus *bus;
  58. struct mpc52xx_fec_mdio_priv *priv;
  59. struct resource res;
  60. int err;
  61. bus = mdiobus_alloc();
  62. if (bus == NULL)
  63. return -ENOMEM;
  64. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  65. if (priv == NULL) {
  66. err = -ENOMEM;
  67. goto out_free;
  68. }
  69. bus->name = "mpc52xx MII bus";
  70. bus->read = mpc52xx_fec_mdio_read;
  71. bus->write = mpc52xx_fec_mdio_write;
  72. /* setup irqs */
  73. bus->irq = priv->mdio_irqs;
  74. /* setup registers */
  75. err = of_address_to_resource(np, 0, &res);
  76. if (err)
  77. goto out_free;
  78. priv->regs = ioremap(res.start, resource_size(&res));
  79. if (priv->regs == NULL) {
  80. err = -ENOMEM;
  81. goto out_free;
  82. }
  83. snprintf(bus->id, MII_BUS_ID_SIZE, "%x", res.start);
  84. bus->priv = priv;
  85. bus->parent = dev;
  86. dev_set_drvdata(dev, bus);
  87. /* set MII speed */
  88. out_be32(&priv->regs->mii_speed,
  89. ((mpc5xxx_get_bus_frequency(of->dev.of_node) >> 20) / 5) << 1);
  90. err = of_mdiobus_register(bus, np);
  91. if (err)
  92. goto out_unmap;
  93. return 0;
  94. out_unmap:
  95. iounmap(priv->regs);
  96. out_free:
  97. kfree(priv);
  98. mdiobus_free(bus);
  99. return err;
  100. }
  101. static int mpc52xx_fec_mdio_remove(struct platform_device *of)
  102. {
  103. struct mii_bus *bus = platform_get_drvdata(of);
  104. struct mpc52xx_fec_mdio_priv *priv = bus->priv;
  105. mdiobus_unregister(bus);
  106. iounmap(priv->regs);
  107. kfree(priv);
  108. mdiobus_free(bus);
  109. return 0;
  110. }
  111. static const struct of_device_id mpc52xx_fec_mdio_match[] = {
  112. { .compatible = "fsl,mpc5200b-mdio", },
  113. { .compatible = "fsl,mpc5200-mdio", },
  114. { .compatible = "mpc5200b-fec-phy", },
  115. {}
  116. };
  117. MODULE_DEVICE_TABLE(of, mpc52xx_fec_mdio_match);
  118. struct platform_driver mpc52xx_fec_mdio_driver = {
  119. .driver = {
  120. .name = "mpc5200b-fec-phy",
  121. .owner = THIS_MODULE,
  122. .of_match_table = mpc52xx_fec_mdio_match,
  123. },
  124. .probe = mpc52xx_fec_mdio_probe,
  125. .remove = mpc52xx_fec_mdio_remove,
  126. };
  127. /* let fec driver call it, since this has to be registered before it */
  128. EXPORT_SYMBOL_GPL(mpc52xx_fec_mdio_driver);
  129. MODULE_LICENSE("Dual BSD/GPL");