fec_mpc52xx_phy.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. };
  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 = priv->regs;
  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. out_be32(&fec->ievent, FEC_IEVENT_MII);
  33. out_be32(&fec->mii_data, value);
  34. /* wait for it to finish, this takes about 23 us on lite5200b */
  35. while (!(in_be32(&fec->ievent) & FEC_IEVENT_MII) && --tries)
  36. msleep(1);
  37. if (!tries)
  38. return -ETIMEDOUT;
  39. return value & FEC_MII_DATA_OP_RD ?
  40. in_be32(&fec->mii_data) & FEC_MII_DATA_DATAMSK : 0;
  41. }
  42. static int mpc52xx_fec_mdio_read(struct mii_bus *bus, int phy_id, int reg)
  43. {
  44. return mpc52xx_fec_mdio_transfer(bus, phy_id, reg, FEC_MII_READ_FRAME);
  45. }
  46. static int mpc52xx_fec_mdio_write(struct mii_bus *bus, int phy_id, int reg,
  47. u16 data)
  48. {
  49. return mpc52xx_fec_mdio_transfer(bus, phy_id, reg,
  50. data | FEC_MII_WRITE_FRAME);
  51. }
  52. static int mpc52xx_fec_mdio_probe(struct platform_device *of)
  53. {
  54. struct device *dev = &of->dev;
  55. struct device_node *np = of->dev.of_node;
  56. struct mii_bus *bus;
  57. struct mpc52xx_fec_mdio_priv *priv;
  58. struct resource res;
  59. int err;
  60. bus = mdiobus_alloc();
  61. if (bus == NULL)
  62. return -ENOMEM;
  63. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  64. if (priv == NULL) {
  65. err = -ENOMEM;
  66. goto out_free;
  67. }
  68. bus->name = "mpc52xx MII bus";
  69. bus->read = mpc52xx_fec_mdio_read;
  70. bus->write = mpc52xx_fec_mdio_write;
  71. /* setup registers */
  72. err = of_address_to_resource(np, 0, &res);
  73. if (err)
  74. goto out_free;
  75. priv->regs = ioremap(res.start, resource_size(&res));
  76. if (priv->regs == NULL) {
  77. err = -ENOMEM;
  78. goto out_free;
  79. }
  80. snprintf(bus->id, MII_BUS_ID_SIZE, "%x", res.start);
  81. bus->priv = priv;
  82. bus->parent = dev;
  83. dev_set_drvdata(dev, bus);
  84. /* set MII speed */
  85. out_be32(&priv->regs->mii_speed,
  86. ((mpc5xxx_get_bus_frequency(of->dev.of_node) >> 20) / 5) << 1);
  87. err = of_mdiobus_register(bus, np);
  88. if (err)
  89. goto out_unmap;
  90. return 0;
  91. out_unmap:
  92. iounmap(priv->regs);
  93. out_free:
  94. kfree(priv);
  95. mdiobus_free(bus);
  96. return err;
  97. }
  98. static int mpc52xx_fec_mdio_remove(struct platform_device *of)
  99. {
  100. struct mii_bus *bus = platform_get_drvdata(of);
  101. struct mpc52xx_fec_mdio_priv *priv = bus->priv;
  102. mdiobus_unregister(bus);
  103. iounmap(priv->regs);
  104. kfree(priv);
  105. mdiobus_free(bus);
  106. return 0;
  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");