memac_phy.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright 2012 Freescale Semiconductor, Inc.
  3. * Andy Fleming <afleming@freescale.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. setbits_be32(&regs->mdio_stat, MDIO_STAT_HOLD_15_CLK);
  33. }
  34. /* Wait till the bus is free */
  35. while ((in_be32(&regs->mdio_stat)) & MDIO_STAT_BSY)
  36. ;
  37. /* Set the port and dev addr */
  38. mdio_ctl = MDIO_CTL_PORT_ADDR(port_addr) | MDIO_CTL_DEV_ADDR(dev_addr);
  39. out_be32(&regs->mdio_ctl, mdio_ctl);
  40. /* Set the register address */
  41. if (c45)
  42. out_be32(&regs->mdio_addr, regnum & 0xffff);
  43. /* Wait till the bus is free */
  44. while ((in_be32(&regs->mdio_stat)) & MDIO_STAT_BSY)
  45. ;
  46. /* Write the value to the register */
  47. out_be32(&regs->mdio_data, MDIO_DATA(value));
  48. /* Wait till the MDIO write is complete */
  49. while ((in_be32(&regs->mdio_data)) & MDIO_DATA_BSY)
  50. ;
  51. return 0;
  52. }
  53. /*
  54. * Reads from register regnum in the PHY for device dev, returning the value.
  55. * Clears miimcom first. All PHY configuration has to be done through the
  56. * TSEC1 MIIM regs
  57. */
  58. int memac_mdio_read(struct mii_dev *bus, int port_addr, int dev_addr,
  59. int regnum)
  60. {
  61. u32 mdio_ctl;
  62. struct memac_mdio_controller *regs = bus->priv;
  63. u32 c45 = 1;
  64. if (dev_addr == MDIO_DEVAD_NONE) {
  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. setbits_be32(&regs->mdio_stat, MDIO_STAT_HOLD_15_CLK);
  71. }
  72. /* Wait till the bus is free */
  73. while ((in_be32(&regs->mdio_stat)) & MDIO_STAT_BSY)
  74. ;
  75. /* Set the Port and Device Addrs */
  76. mdio_ctl = MDIO_CTL_PORT_ADDR(port_addr) | MDIO_CTL_DEV_ADDR(dev_addr);
  77. out_be32(&regs->mdio_ctl, mdio_ctl);
  78. /* Set the register address */
  79. if (c45)
  80. out_be32(&regs->mdio_addr, regnum & 0xffff);
  81. /* Wait till the bus is free */
  82. while ((in_be32(&regs->mdio_stat)) & MDIO_STAT_BSY)
  83. ;
  84. /* Initiate the read */
  85. mdio_ctl |= MDIO_CTL_READ;
  86. out_be32(&regs->mdio_ctl, mdio_ctl);
  87. /* Wait till the MDIO write is complete */
  88. while ((in_be32(&regs->mdio_data)) & MDIO_DATA_BSY)
  89. ;
  90. /* Return all Fs if nothing was there */
  91. if (in_be32(&regs->mdio_stat) & MDIO_STAT_RD_ER)
  92. return 0xffff;
  93. return in_be32(&regs->mdio_data) & 0xffff;
  94. }
  95. int memac_mdio_reset(struct mii_dev *bus)
  96. {
  97. return 0;
  98. }
  99. int fm_memac_mdio_init(bd_t *bis, struct memac_mdio_info *info)
  100. {
  101. struct mii_dev *bus = mdio_alloc();
  102. if (!bus) {
  103. printf("Failed to allocate FM TGEC MDIO bus\n");
  104. return -1;
  105. }
  106. bus->read = memac_mdio_read;
  107. bus->write = memac_mdio_write;
  108. bus->reset = memac_mdio_reset;
  109. sprintf(bus->name, info->name);
  110. bus->priv = info->regs;
  111. /*
  112. * On some platforms like B4860, default value of MDIO_CLK_DIV bits
  113. * in mdio_stat(mdio_cfg) register generates MDIO clock too high
  114. * (much higher than 2.5MHz), violating the IEEE specs.
  115. * On other platforms like T1040, default value of MDIO_CLK_DIV bits
  116. * is zero, so MDIO clock is disabled.
  117. * So, for proper functioning of MDIO, MDIO_CLK_DIV bits needs to
  118. * be properly initialized.
  119. */
  120. setbits_be32(&((struct memac_mdio_controller *)info->regs)->mdio_stat,
  121. MDIO_STAT_CLKDIV(258));
  122. return mdio_register(bus);
  123. }