memac_phy.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2012 Freescale Semiconductor, Inc.
  4. * Andy Fleming <afleming@gmail.com>
  5. * Roy Zang <tie-fei.zang@freescale.com>
  6. * Some part is taken from tsec.c
  7. */
  8. #include <common.h>
  9. #include <miiphy.h>
  10. #include <phy.h>
  11. #include <asm/io.h>
  12. #include <fsl_memac.h>
  13. #include <fm_eth.h>
  14. #ifdef CONFIG_SYS_MEMAC_LITTLE_ENDIAN
  15. #define memac_out_32(a, v) out_le32(a, v)
  16. #define memac_clrbits_32(a, v) clrbits_le32(a, v)
  17. #define memac_setbits_32(a, v) setbits_le32(a, v)
  18. #else
  19. #define memac_out_32(a, v) out_be32(a, v)
  20. #define memac_clrbits_32(a, v) clrbits_be32(a, v)
  21. #define memac_setbits_32(a, v) setbits_be32(a, v)
  22. #endif
  23. static u32 memac_in_32(u32 *reg)
  24. {
  25. #ifdef CONFIG_SYS_MEMAC_LITTLE_ENDIAN
  26. return in_le32(reg);
  27. #else
  28. return in_be32(reg);
  29. #endif
  30. }
  31. /*
  32. * Write value to the PHY for this device to the register at regnum, waiting
  33. * until the write is done before it returns. All PHY configuration has to be
  34. * done through the TSEC1 MIIM regs
  35. */
  36. int memac_mdio_write(struct mii_dev *bus, int port_addr, int dev_addr,
  37. int regnum, u16 value)
  38. {
  39. u32 mdio_ctl;
  40. struct memac_mdio_controller *regs = bus->priv;
  41. u32 c45 = 1; /* Default to 10G interface */
  42. if (dev_addr == MDIO_DEVAD_NONE) {
  43. c45 = 0; /* clause 22 */
  44. dev_addr = regnum & 0x1f;
  45. memac_clrbits_32(&regs->mdio_stat, MDIO_STAT_ENC);
  46. } else
  47. memac_setbits_32(&regs->mdio_stat, MDIO_STAT_ENC);
  48. /* Wait till the bus is free */
  49. while ((memac_in_32(&regs->mdio_stat)) & MDIO_STAT_BSY)
  50. ;
  51. /* Set the port and dev addr */
  52. mdio_ctl = MDIO_CTL_PORT_ADDR(port_addr) | MDIO_CTL_DEV_ADDR(dev_addr);
  53. memac_out_32(&regs->mdio_ctl, mdio_ctl);
  54. /* Set the register address */
  55. if (c45)
  56. memac_out_32(&regs->mdio_addr, regnum & 0xffff);
  57. /* Wait till the bus is free */
  58. while ((memac_in_32(&regs->mdio_stat)) & MDIO_STAT_BSY)
  59. ;
  60. /* Write the value to the register */
  61. memac_out_32(&regs->mdio_data, MDIO_DATA(value));
  62. /* Wait till the MDIO write is complete */
  63. while ((memac_in_32(&regs->mdio_data)) & MDIO_DATA_BSY)
  64. ;
  65. return 0;
  66. }
  67. /*
  68. * Reads from register regnum in the PHY for device dev, returning the value.
  69. * Clears miimcom first. All PHY configuration has to be done through the
  70. * TSEC1 MIIM regs
  71. */
  72. int memac_mdio_read(struct mii_dev *bus, int port_addr, int dev_addr,
  73. int regnum)
  74. {
  75. u32 mdio_ctl;
  76. struct memac_mdio_controller *regs = bus->priv;
  77. u32 c45 = 1;
  78. if (dev_addr == MDIO_DEVAD_NONE) {
  79. if (!strcmp(bus->name, DEFAULT_FM_TGEC_MDIO_NAME))
  80. return 0xffff;
  81. c45 = 0; /* clause 22 */
  82. dev_addr = regnum & 0x1f;
  83. memac_clrbits_32(&regs->mdio_stat, MDIO_STAT_ENC);
  84. } else
  85. memac_setbits_32(&regs->mdio_stat, MDIO_STAT_ENC);
  86. /* Wait till the bus is free */
  87. while ((memac_in_32(&regs->mdio_stat)) & MDIO_STAT_BSY)
  88. ;
  89. /* Set the Port and Device Addrs */
  90. mdio_ctl = MDIO_CTL_PORT_ADDR(port_addr) | MDIO_CTL_DEV_ADDR(dev_addr);
  91. memac_out_32(&regs->mdio_ctl, mdio_ctl);
  92. /* Set the register address */
  93. if (c45)
  94. memac_out_32(&regs->mdio_addr, regnum & 0xffff);
  95. /* Wait till the bus is free */
  96. while ((memac_in_32(&regs->mdio_stat)) & MDIO_STAT_BSY)
  97. ;
  98. /* Initiate the read */
  99. mdio_ctl |= MDIO_CTL_READ;
  100. memac_out_32(&regs->mdio_ctl, mdio_ctl);
  101. /* Wait till the MDIO write is complete */
  102. while ((memac_in_32(&regs->mdio_data)) & MDIO_DATA_BSY)
  103. ;
  104. /* Return all Fs if nothing was there */
  105. if (memac_in_32(&regs->mdio_stat) & MDIO_STAT_RD_ER)
  106. return 0xffff;
  107. return memac_in_32(&regs->mdio_data) & 0xffff;
  108. }
  109. int memac_mdio_reset(struct mii_dev *bus)
  110. {
  111. return 0;
  112. }
  113. int fm_memac_mdio_init(bd_t *bis, struct memac_mdio_info *info)
  114. {
  115. struct mii_dev *bus = mdio_alloc();
  116. if (!bus) {
  117. printf("Failed to allocate FM TGEC MDIO bus\n");
  118. return -1;
  119. }
  120. bus->read = memac_mdio_read;
  121. bus->write = memac_mdio_write;
  122. bus->reset = memac_mdio_reset;
  123. strcpy(bus->name, info->name);
  124. bus->priv = info->regs;
  125. /*
  126. * On some platforms like B4860, default value of MDIO_CLK_DIV bits
  127. * in mdio_stat(mdio_cfg) register generates MDIO clock too high
  128. * (much higher than 2.5MHz), violating the IEEE specs.
  129. * On other platforms like T1040, default value of MDIO_CLK_DIV bits
  130. * is zero, so MDIO clock is disabled.
  131. * So, for proper functioning of MDIO, MDIO_CLK_DIV bits needs to
  132. * be properly initialized.
  133. * NEG bit default should be '1' as per FMAN-v3 RM, but on platform
  134. * like T2080QDS, this bit default is '0', which leads to MDIO failure
  135. * on XAUI PHY, so set this bit definitely.
  136. */
  137. memac_setbits_32(
  138. &((struct memac_mdio_controller *)info->regs)->mdio_stat,
  139. MDIO_STAT_CLKDIV(258) | MDIO_STAT_NEG);
  140. return mdio_register(bus);
  141. }