mdio-bcm-iproc.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright (C) 2015 Broadcom Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation version 2.
  7. *
  8. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  9. * kind, whether express or implied; without even the implied warranty
  10. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/delay.h>
  14. #include <linux/io.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/of_platform.h>
  19. #include <linux/of_mdio.h>
  20. #include <linux/phy.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/sched.h>
  23. #define IPROC_GPHY_MDCDIV 0x1a
  24. #define MII_CTRL_OFFSET 0x000
  25. #define MII_CTRL_DIV_SHIFT 0
  26. #define MII_CTRL_PRE_SHIFT 7
  27. #define MII_CTRL_BUSY_SHIFT 8
  28. #define MII_DATA_OFFSET 0x004
  29. #define MII_DATA_MASK 0xffff
  30. #define MII_DATA_TA_SHIFT 16
  31. #define MII_DATA_TA_VAL 2
  32. #define MII_DATA_RA_SHIFT 18
  33. #define MII_DATA_PA_SHIFT 23
  34. #define MII_DATA_OP_SHIFT 28
  35. #define MII_DATA_OP_WRITE 1
  36. #define MII_DATA_OP_READ 2
  37. #define MII_DATA_SB_SHIFT 30
  38. struct iproc_mdio_priv {
  39. struct mii_bus *mii_bus;
  40. void __iomem *base;
  41. };
  42. static inline int iproc_mdio_wait_for_idle(void __iomem *base)
  43. {
  44. u32 val;
  45. unsigned int timeout = 1000; /* loop for 1s */
  46. do {
  47. val = readl(base + MII_CTRL_OFFSET);
  48. if ((val & BIT(MII_CTRL_BUSY_SHIFT)) == 0)
  49. return 0;
  50. usleep_range(1000, 2000);
  51. } while (timeout--);
  52. return -ETIMEDOUT;
  53. }
  54. static inline void iproc_mdio_config_clk(void __iomem *base)
  55. {
  56. u32 val;
  57. val = (IPROC_GPHY_MDCDIV << MII_CTRL_DIV_SHIFT) |
  58. BIT(MII_CTRL_PRE_SHIFT);
  59. writel(val, base + MII_CTRL_OFFSET);
  60. }
  61. static int iproc_mdio_read(struct mii_bus *bus, int phy_id, int reg)
  62. {
  63. struct iproc_mdio_priv *priv = bus->priv;
  64. u32 cmd;
  65. int rc;
  66. rc = iproc_mdio_wait_for_idle(priv->base);
  67. if (rc)
  68. return rc;
  69. /* Prepare the read operation */
  70. cmd = (MII_DATA_TA_VAL << MII_DATA_TA_SHIFT) |
  71. (reg << MII_DATA_RA_SHIFT) |
  72. (phy_id << MII_DATA_PA_SHIFT) |
  73. BIT(MII_DATA_SB_SHIFT) |
  74. (MII_DATA_OP_READ << MII_DATA_OP_SHIFT);
  75. writel(cmd, priv->base + MII_DATA_OFFSET);
  76. rc = iproc_mdio_wait_for_idle(priv->base);
  77. if (rc)
  78. return rc;
  79. cmd = readl(priv->base + MII_DATA_OFFSET) & MII_DATA_MASK;
  80. return cmd;
  81. }
  82. static int iproc_mdio_write(struct mii_bus *bus, int phy_id,
  83. int reg, u16 val)
  84. {
  85. struct iproc_mdio_priv *priv = bus->priv;
  86. u32 cmd;
  87. int rc;
  88. rc = iproc_mdio_wait_for_idle(priv->base);
  89. if (rc)
  90. return rc;
  91. /* Prepare the write operation */
  92. cmd = (MII_DATA_TA_VAL << MII_DATA_TA_SHIFT) |
  93. (reg << MII_DATA_RA_SHIFT) |
  94. (phy_id << MII_DATA_PA_SHIFT) |
  95. BIT(MII_DATA_SB_SHIFT) |
  96. (MII_DATA_OP_WRITE << MII_DATA_OP_SHIFT) |
  97. ((u32)(val) & MII_DATA_MASK);
  98. writel(cmd, priv->base + MII_DATA_OFFSET);
  99. rc = iproc_mdio_wait_for_idle(priv->base);
  100. if (rc)
  101. return rc;
  102. return 0;
  103. }
  104. static int iproc_mdio_probe(struct platform_device *pdev)
  105. {
  106. struct iproc_mdio_priv *priv;
  107. struct mii_bus *bus;
  108. struct resource *res;
  109. int rc;
  110. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  111. if (!priv)
  112. return -ENOMEM;
  113. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  114. priv->base = devm_ioremap_resource(&pdev->dev, res);
  115. if (IS_ERR(priv->base)) {
  116. dev_err(&pdev->dev, "failed to ioremap register\n");
  117. return PTR_ERR(priv->base);
  118. }
  119. priv->mii_bus = mdiobus_alloc();
  120. if (!priv->mii_bus) {
  121. dev_err(&pdev->dev, "MDIO bus alloc failed\n");
  122. return -ENOMEM;
  123. }
  124. bus = priv->mii_bus;
  125. bus->priv = priv;
  126. bus->name = "iProc MDIO bus";
  127. snprintf(bus->id, MII_BUS_ID_SIZE, "%s-%d", pdev->name, pdev->id);
  128. bus->parent = &pdev->dev;
  129. bus->read = iproc_mdio_read;
  130. bus->write = iproc_mdio_write;
  131. iproc_mdio_config_clk(priv->base);
  132. rc = of_mdiobus_register(bus, pdev->dev.of_node);
  133. if (rc) {
  134. dev_err(&pdev->dev, "MDIO bus registration failed\n");
  135. goto err_iproc_mdio;
  136. }
  137. platform_set_drvdata(pdev, priv);
  138. dev_info(&pdev->dev, "Broadcom iProc MDIO bus at 0x%p\n", priv->base);
  139. return 0;
  140. err_iproc_mdio:
  141. mdiobus_free(bus);
  142. return rc;
  143. }
  144. static int iproc_mdio_remove(struct platform_device *pdev)
  145. {
  146. struct iproc_mdio_priv *priv = platform_get_drvdata(pdev);
  147. mdiobus_unregister(priv->mii_bus);
  148. mdiobus_free(priv->mii_bus);
  149. return 0;
  150. }
  151. static const struct of_device_id iproc_mdio_of_match[] = {
  152. { .compatible = "brcm,iproc-mdio", },
  153. { /* sentinel */ },
  154. };
  155. MODULE_DEVICE_TABLE(of, iproc_mdio_of_match);
  156. static struct platform_driver iproc_mdio_driver = {
  157. .driver = {
  158. .name = "iproc-mdio",
  159. .of_match_table = iproc_mdio_of_match,
  160. },
  161. .probe = iproc_mdio_probe,
  162. .remove = iproc_mdio_remove,
  163. };
  164. module_platform_driver(iproc_mdio_driver);
  165. MODULE_AUTHOR("Broadcom Corporation");
  166. MODULE_DESCRIPTION("Broadcom iProc MDIO bus controller");
  167. MODULE_LICENSE("GPL v2");
  168. MODULE_ALIAS("platform:iproc-mdio");