tgec_phy.c 3.6 KB

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