fsl_mdio.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright 2009-2010 Freescale Semiconductor, Inc.
  3. * Jun-jie Zhang <b18070@freescale.com>
  4. * Mingkai Hu <Mingkai.hu@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. */
  21. #include <common.h>
  22. #include <miiphy.h>
  23. #include <phy.h>
  24. #include <fsl_mdio.h>
  25. #include <asm/io.h>
  26. #include <asm/errno.h>
  27. #include <asm/fsl_enet.h>
  28. void tsec_local_mdio_write(struct tsec_mii_mng *phyregs, int port_addr,
  29. int dev_addr, int regnum, int value)
  30. {
  31. int timeout = 1000000;
  32. out_be32(&phyregs->miimadd, (port_addr << 8) | (regnum & 0x1f));
  33. out_be32(&phyregs->miimcon, value);
  34. asm("sync");
  35. while ((in_be32(&phyregs->miimind) & MIIMIND_BUSY) && timeout--)
  36. ;
  37. }
  38. int tsec_local_mdio_read(struct tsec_mii_mng *phyregs, int port_addr,
  39. int dev_addr, int regnum)
  40. {
  41. int value;
  42. int timeout = 1000000;
  43. /* Put the address of the phy, and the register
  44. * number into MIIMADD */
  45. out_be32(&phyregs->miimadd, (port_addr << 8) | (regnum & 0x1f));
  46. /* Clear the command register, and wait */
  47. out_be32(&phyregs->miimcom, 0);
  48. asm("sync");
  49. /* Initiate a read command, and wait */
  50. out_be32(&phyregs->miimcom, MIIMCOM_READ_CYCLE);
  51. asm("sync");
  52. /* Wait for the the indication that the read is done */
  53. while ((in_be32(&phyregs->miimind) & (MIIMIND_NOTVALID | MIIMIND_BUSY))
  54. && timeout--)
  55. ;
  56. /* Grab the value read from the PHY */
  57. value = in_be32(&phyregs->miimstat);
  58. return value;
  59. }
  60. static int fsl_pq_mdio_reset(struct mii_dev *bus)
  61. {
  62. struct tsec_mii_mng *regs = bus->priv;
  63. /* Reset MII (due to new addresses) */
  64. out_be32(&regs->miimcfg, MIIMCFG_RESET_MGMT);
  65. out_be32(&regs->miimcfg, MIIMCFG_INIT_VALUE);
  66. while (in_be32(&regs->miimind) & MIIMIND_BUSY)
  67. ;
  68. return 0;
  69. }
  70. int tsec_phy_read(struct mii_dev *bus, int addr, int dev_addr, int regnum)
  71. {
  72. struct tsec_mii_mng *phyregs = bus->priv;
  73. return tsec_local_mdio_read(phyregs, addr, dev_addr, regnum);
  74. }
  75. int tsec_phy_write(struct mii_dev *bus, int addr, int dev_addr, int regnum,
  76. u16 value)
  77. {
  78. struct tsec_mii_mng *phyregs = bus->priv;
  79. tsec_local_mdio_write(phyregs, addr, dev_addr, regnum, value);
  80. return 0;
  81. }
  82. int fsl_pq_mdio_init(bd_t *bis, struct fsl_pq_mdio_info *info)
  83. {
  84. struct mii_dev *bus = mdio_alloc();
  85. if (!bus) {
  86. printf("Failed to allocate FSL MDIO bus\n");
  87. return -1;
  88. }
  89. bus->read = tsec_phy_read;
  90. bus->write = tsec_phy_write;
  91. bus->reset = fsl_pq_mdio_reset;
  92. sprintf(bus->name, info->name);
  93. bus->priv = info->regs;
  94. return mdio_register(bus);
  95. }