memac_phy.c 4.0 KB

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