fsl_mdio.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2009-2010, 2013 Freescale Semiconductor, Inc.
  4. * Jun-jie Zhang <b18070@freescale.com>
  5. * Mingkai Hu <Mingkai.hu@freescale.com>
  6. */
  7. #include <common.h>
  8. #include <miiphy.h>
  9. #include <phy.h>
  10. #include <fsl_mdio.h>
  11. #include <asm/io.h>
  12. #include <linux/errno.h>
  13. #ifdef CONFIG_DM_MDIO
  14. struct tsec_mdio_priv {
  15. struct tsec_mii_mng __iomem *regs;
  16. };
  17. #endif
  18. void tsec_local_mdio_write(struct tsec_mii_mng __iomem *phyregs, int port_addr,
  19. int dev_addr, int regnum, int value)
  20. {
  21. int timeout = 1000000;
  22. out_be32(&phyregs->miimadd, (port_addr << 8) | (regnum & 0x1f));
  23. out_be32(&phyregs->miimcon, value);
  24. /* Memory barrier */
  25. mb();
  26. while ((in_be32(&phyregs->miimind) & MIIMIND_BUSY) && timeout--)
  27. ;
  28. }
  29. int tsec_local_mdio_read(struct tsec_mii_mng __iomem *phyregs, int port_addr,
  30. int dev_addr, int regnum)
  31. {
  32. int value;
  33. int timeout = 1000000;
  34. /* Put the address of the phy, and the register number into MIIMADD */
  35. out_be32(&phyregs->miimadd, (port_addr << 8) | (regnum & 0x1f));
  36. /* Clear the command register, and wait */
  37. out_be32(&phyregs->miimcom, 0);
  38. /* Memory barrier */
  39. mb();
  40. /* Initiate a read command, and wait */
  41. out_be32(&phyregs->miimcom, MIIMCOM_READ_CYCLE);
  42. /* Memory barrier */
  43. mb();
  44. /* Wait for the the indication that the read is done */
  45. while ((in_be32(&phyregs->miimind) & (MIIMIND_NOTVALID | MIIMIND_BUSY))
  46. && timeout--)
  47. ;
  48. /* Grab the value read from the PHY */
  49. value = in_be32(&phyregs->miimstat);
  50. return value;
  51. }
  52. #if defined(CONFIG_PHYLIB)
  53. static int fsl_pq_mdio_reset(struct mii_dev *bus)
  54. {
  55. struct tsec_mii_mng __iomem *regs;
  56. #ifndef CONFIG_DM_MDIO
  57. regs = (struct tsec_mii_mng __iomem *)bus->priv;
  58. #else
  59. struct tsec_mdio_priv *priv;
  60. if (!bus->priv)
  61. return -EINVAL;
  62. priv = dev_get_priv(bus->priv);
  63. regs = priv->regs;
  64. #endif
  65. /* Reset MII (due to new addresses) */
  66. out_be32(&regs->miimcfg, MIIMCFG_RESET_MGMT);
  67. out_be32(&regs->miimcfg, MIIMCFG_INIT_VALUE);
  68. while (in_be32(&regs->miimind) & MIIMIND_BUSY)
  69. ;
  70. return 0;
  71. }
  72. #endif
  73. int tsec_phy_read(struct mii_dev *bus, int addr, int dev_addr, int regnum)
  74. {
  75. struct tsec_mii_mng __iomem *phyregs;
  76. #ifndef CONFIG_DM_MDIO
  77. phyregs = (struct tsec_mii_mng __iomem *)bus->priv;
  78. #else
  79. struct tsec_mdio_priv *priv;
  80. if (!bus->priv)
  81. return -EINVAL;
  82. priv = dev_get_priv(bus->priv);
  83. phyregs = priv->regs;
  84. #endif
  85. return tsec_local_mdio_read(phyregs, addr, dev_addr, regnum);
  86. }
  87. int tsec_phy_write(struct mii_dev *bus, int addr, int dev_addr, int regnum,
  88. u16 value)
  89. {
  90. struct tsec_mii_mng __iomem *phyregs;
  91. #ifndef CONFIG_DM_MDIO
  92. phyregs = (struct tsec_mii_mng __iomem *)bus->priv;
  93. #else
  94. struct tsec_mdio_priv *priv;
  95. if (!bus->priv)
  96. return -EINVAL;
  97. priv = dev_get_priv(bus->priv);
  98. phyregs = priv->regs;
  99. #endif
  100. tsec_local_mdio_write(phyregs, addr, dev_addr, regnum, value);
  101. return 0;
  102. }
  103. #ifndef CONFIG_DM_MDIO
  104. int fsl_pq_mdio_init(bd_t *bis, struct fsl_pq_mdio_info *info)
  105. {
  106. struct mii_dev *bus = mdio_alloc();
  107. if (!bus) {
  108. printf("Failed to allocate FSL MDIO bus\n");
  109. return -1;
  110. }
  111. bus->read = tsec_phy_read;
  112. bus->write = tsec_phy_write;
  113. bus->reset = fsl_pq_mdio_reset;
  114. strcpy(bus->name, info->name);
  115. bus->priv = (void *)info->regs;
  116. return mdio_register(bus);
  117. }
  118. #else /* CONFIG_DM_MDIO */
  119. #if defined(CONFIG_PHYLIB)
  120. static int tsec_mdio_read(struct udevice *dev, int addr, int devad, int reg)
  121. {
  122. struct mdio_perdev_priv *pdata = (dev) ? dev_get_uclass_priv(dev) :
  123. NULL;
  124. if (pdata && pdata->mii_bus)
  125. return tsec_phy_read(pdata->mii_bus, addr, devad, reg);
  126. return -1;
  127. }
  128. static int tsec_mdio_write(struct udevice *dev, int addr, int devad, int reg,
  129. u16 val)
  130. {
  131. struct mdio_perdev_priv *pdata = (dev) ? dev_get_uclass_priv(dev) :
  132. NULL;
  133. if (pdata && pdata->mii_bus)
  134. return tsec_phy_write(pdata->mii_bus, addr, devad, reg, val);
  135. return -1;
  136. }
  137. static int tsec_mdio_reset(struct udevice *dev)
  138. {
  139. struct mdio_perdev_priv *pdata = (dev) ? dev_get_uclass_priv(dev) :
  140. NULL;
  141. if (pdata && pdata->mii_bus)
  142. return fsl_pq_mdio_reset(pdata->mii_bus);
  143. return -1;
  144. }
  145. static const struct mdio_ops tsec_mdio_ops = {
  146. .read = tsec_mdio_read,
  147. .write = tsec_mdio_write,
  148. .reset = tsec_mdio_reset,
  149. };
  150. static const struct udevice_id tsec_mdio_ids[] = {
  151. { .compatible = "fsl,gianfar-tbi" },
  152. { .compatible = "fsl,gianfar-mdio" },
  153. { .compatible = "fsl,etsec2-tbi" },
  154. { .compatible = "fsl,etsec2-mdio" },
  155. { .compatible = "fsl,fman-mdio" },
  156. {}
  157. };
  158. static int tsec_mdio_probe(struct udevice *dev)
  159. {
  160. struct tsec_mdio_priv *priv = (dev) ? dev_get_priv(dev) : NULL;
  161. struct mdio_perdev_priv *pdata = (dev) ? dev_get_uclass_priv(dev) :
  162. NULL;
  163. if (!dev) {
  164. printf("%s dev = NULL\n", __func__);
  165. return -1;
  166. }
  167. if (!priv) {
  168. printf("dev_get_priv(dev %p) = NULL\n", dev);
  169. return -1;
  170. }
  171. priv->regs = (void *)(uintptr_t)dev_read_addr(dev);
  172. debug("%s priv %p @ regs %p, pdata %p\n", __func__,
  173. priv, priv->regs, pdata);
  174. return 0;
  175. }
  176. static int tsec_mdio_remove(struct udevice *dev)
  177. {
  178. return 0;
  179. }
  180. U_BOOT_DRIVER(tsec_mdio) = {
  181. .name = "tsec_mdio",
  182. .id = UCLASS_MDIO,
  183. .of_match = tsec_mdio_ids,
  184. .probe = tsec_mdio_probe,
  185. .remove = tsec_mdio_remove,
  186. .ops = &tsec_mdio_ops,
  187. .priv_auto_alloc_size = sizeof(struct tsec_mdio_priv),
  188. .platdata_auto_alloc_size = sizeof(struct mdio_perdev_priv),
  189. };
  190. #endif /* CONFIG_PHYLIB */
  191. #endif /* CONFIG_DM_MDIO */