fec_mpc52xx_phy.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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/slab.h>
  16. #include <linux/of.h>
  17. #include <linux/of_address.h>
  18. #include <linux/of_mdio.h>
  19. #include <linux/platform_device.h>
  20. #include <asm/io.h>
  21. #include <asm/mpc52xx.h>
  22. #include "fec_mpc52xx.h"
  23. struct mpc52xx_fec_mdio_priv {
  24. struct mpc52xx_fec __iomem *regs;
  25. };
  26. static int mpc52xx_fec_mdio_transfer(struct mii_bus *bus, int phy_id,
  27. int reg, u32 value)
  28. {
  29. struct mpc52xx_fec_mdio_priv *priv = bus->priv;
  30. struct mpc52xx_fec __iomem *fec = priv->regs;
  31. int tries = 3;
  32. value |= (phy_id << FEC_MII_DATA_PA_SHIFT) & FEC_MII_DATA_PA_MSK;
  33. value |= (reg << FEC_MII_DATA_RA_SHIFT) & FEC_MII_DATA_RA_MSK;
  34. out_be32(&fec->ievent, FEC_IEVENT_MII);
  35. out_be32(&fec->mii_data, value);
  36. /* wait for it to finish, this takes about 23 us on lite5200b */
  37. while (!(in_be32(&fec->ievent) & FEC_IEVENT_MII) && --tries)
  38. msleep(1);
  39. if (!tries)
  40. return -ETIMEDOUT;
  41. return value & FEC_MII_DATA_OP_RD ?
  42. in_be32(&fec->mii_data) & FEC_MII_DATA_DATAMSK : 0;
  43. }
  44. static int mpc52xx_fec_mdio_read(struct mii_bus *bus, int phy_id, int reg)
  45. {
  46. return mpc52xx_fec_mdio_transfer(bus, phy_id, reg, FEC_MII_READ_FRAME);
  47. }
  48. static int mpc52xx_fec_mdio_write(struct mii_bus *bus, int phy_id, int reg,
  49. u16 data)
  50. {
  51. return mpc52xx_fec_mdio_transfer(bus, phy_id, reg,
  52. data | FEC_MII_WRITE_FRAME);
  53. }
  54. static int mpc52xx_fec_mdio_probe(struct platform_device *of)
  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_obj(*priv);
  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 registers */
  74. err = of_address_to_resource(np, 0, &res);
  75. if (err)
  76. goto out_free;
  77. priv->regs = ioremap(res.start, resource_size(&res));
  78. if (priv->regs == NULL) {
  79. err = -ENOMEM;
  80. goto out_free;
  81. }
  82. snprintf(bus->id, MII_BUS_ID_SIZE, "%pa", &res.start);
  83. bus->priv = priv;
  84. bus->parent = dev;
  85. dev_set_drvdata(dev, bus);
  86. /* set MII speed */
  87. out_be32(&priv->regs->mii_speed, ((mpc5xxx_get_bus_frequency(dev) >> 20) / 5) << 1);
  88. err = of_mdiobus_register(bus, np);
  89. if (err)
  90. goto out_unmap;
  91. return 0;
  92. out_unmap:
  93. iounmap(priv->regs);
  94. out_free:
  95. kfree(priv);
  96. mdiobus_free(bus);
  97. return err;
  98. }
  99. static void mpc52xx_fec_mdio_remove(struct platform_device *of)
  100. {
  101. struct mii_bus *bus = platform_get_drvdata(of);
  102. struct mpc52xx_fec_mdio_priv *priv = bus->priv;
  103. mdiobus_unregister(bus);
  104. iounmap(priv->regs);
  105. kfree(priv);
  106. mdiobus_free(bus);
  107. }
  108. static const struct of_device_id mpc52xx_fec_mdio_match[] = {
  109. { .compatible = "fsl,mpc5200b-mdio", },
  110. { .compatible = "fsl,mpc5200-mdio", },
  111. { .compatible = "mpc5200b-fec-phy", },
  112. {}
  113. };
  114. MODULE_DEVICE_TABLE(of, mpc52xx_fec_mdio_match);
  115. struct platform_driver mpc52xx_fec_mdio_driver = {
  116. .driver = {
  117. .name = "mpc5200b-fec-phy",
  118. .owner = THIS_MODULE,
  119. .of_match_table = mpc52xx_fec_mdio_match,
  120. },
  121. .probe = mpc52xx_fec_mdio_probe,
  122. .remove = mpc52xx_fec_mdio_remove,
  123. };
  124. /* let fec driver call it, since this has to be registered before it */
  125. EXPORT_SYMBOL_GPL(mpc52xx_fec_mdio_driver);
  126. MODULE_LICENSE("Dual BSD/GPL");