fsl_mdio.c 2.5 KB

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