memac_phy.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright 2012 Freescale Semiconductor, Inc.
  3. * Andy Fleming <afleming@freescale.com>
  4. * Roy Zang <tie-fei.zang@freescale.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  19. * MA 02111-1307 USA
  20. * Some part is taken from tsec.c
  21. */
  22. #include <common.h>
  23. #include <miiphy.h>
  24. #include <phy.h>
  25. #include <asm/io.h>
  26. #include <asm/fsl_memac.h>
  27. #include <fm_eth.h>
  28. /*
  29. * Write value to the PHY for this device to the register at regnum, waiting
  30. * until the write is done before it returns. All PHY configuration has to be
  31. * done through the TSEC1 MIIM regs
  32. */
  33. int memac_mdio_write(struct mii_dev *bus, int port_addr, int dev_addr,
  34. int regnum, u16 value)
  35. {
  36. u32 mdio_ctl;
  37. struct memac_mdio_controller *regs = bus->priv;
  38. u32 c45 = 1; /* Default to 10G interface */
  39. if (dev_addr == MDIO_DEVAD_NONE) {
  40. c45 = 0; /* clause 22 */
  41. dev_addr = regnum & 0x1f;
  42. clrbits_be32(&regs->mdio_stat, MDIO_STAT_ENC);
  43. } else {
  44. setbits_be32(&regs->mdio_stat, MDIO_STAT_ENC);
  45. setbits_be32(&regs->mdio_stat, MDIO_STAT_HOLD_15_CLK);
  46. }
  47. /* Wait till the bus is free */
  48. while ((in_be32(&regs->mdio_stat)) & MDIO_STAT_BSY)
  49. ;
  50. /* Set the port and dev addr */
  51. mdio_ctl = MDIO_CTL_PORT_ADDR(port_addr) | MDIO_CTL_DEV_ADDR(dev_addr);
  52. out_be32(&regs->mdio_ctl, mdio_ctl);
  53. /* Set the register address */
  54. if (c45)
  55. out_be32(&regs->mdio_addr, regnum & 0xffff);
  56. /* Wait till the bus is free */
  57. while ((in_be32(&regs->mdio_stat)) & MDIO_STAT_BSY)
  58. ;
  59. /* Write the value to the register */
  60. out_be32(&regs->mdio_data, MDIO_DATA(value));
  61. /* Wait till the MDIO write is complete */
  62. while ((in_be32(&regs->mdio_data)) & MDIO_DATA_BSY)
  63. ;
  64. return 0;
  65. }
  66. /*
  67. * Reads from register regnum in the PHY for device dev, returning the value.
  68. * Clears miimcom first. All PHY configuration has to be done through the
  69. * TSEC1 MIIM regs
  70. */
  71. int memac_mdio_read(struct mii_dev *bus, int port_addr, int dev_addr,
  72. int regnum)
  73. {
  74. u32 mdio_ctl;
  75. struct memac_mdio_controller *regs = bus->priv;
  76. u32 c45 = 1;
  77. if (dev_addr == MDIO_DEVAD_NONE) {
  78. c45 = 0; /* clause 22 */
  79. dev_addr = regnum & 0x1f;
  80. clrbits_be32(&regs->mdio_stat, MDIO_STAT_ENC);
  81. } else {
  82. setbits_be32(&regs->mdio_stat, MDIO_STAT_ENC);
  83. setbits_be32(&regs->mdio_stat, MDIO_STAT_HOLD_15_CLK);
  84. }
  85. /* Wait till the bus is free */
  86. while ((in_be32(&regs->mdio_stat)) & MDIO_STAT_BSY)
  87. ;
  88. /* Set the Port and Device Addrs */
  89. mdio_ctl = MDIO_CTL_PORT_ADDR(port_addr) | MDIO_CTL_DEV_ADDR(dev_addr);
  90. out_be32(&regs->mdio_ctl, mdio_ctl);
  91. /* Set the register address */
  92. if (c45)
  93. out_be32(&regs->mdio_addr, regnum & 0xffff);
  94. /* Wait till the bus is free */
  95. while ((in_be32(&regs->mdio_stat)) & MDIO_STAT_BSY)
  96. ;
  97. /* Initiate the read */
  98. mdio_ctl |= MDIO_CTL_READ;
  99. out_be32(&regs->mdio_ctl, mdio_ctl);
  100. /* Wait till the MDIO write is complete */
  101. while ((in_be32(&regs->mdio_data)) & MDIO_DATA_BSY)
  102. ;
  103. /* Return all Fs if nothing was there */
  104. if (in_be32(&regs->mdio_stat) & MDIO_STAT_RD_ER)
  105. return 0xffff;
  106. return in_be32(&regs->mdio_data) & 0xffff;
  107. }
  108. int memac_mdio_reset(struct mii_dev *bus)
  109. {
  110. return 0;
  111. }
  112. int fm_memac_mdio_init(bd_t *bis, struct memac_mdio_info *info)
  113. {
  114. struct mii_dev *bus = mdio_alloc();
  115. if (!bus) {
  116. printf("Failed to allocate FM TGEC MDIO bus\n");
  117. return -1;
  118. }
  119. bus->read = memac_mdio_read;
  120. bus->write = memac_mdio_write;
  121. bus->reset = memac_mdio_reset;
  122. sprintf(bus->name, info->name);
  123. bus->priv = info->regs;
  124. return mdio_register(bus);
  125. }