fsl_mdio.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. void tsec_local_mdio_write(struct tsec_mii_mng __iomem *phyregs, int port_addr,
  15. int dev_addr, int regnum, int value)
  16. {
  17. int timeout = 1000000;
  18. out_be32(&phyregs->miimadd, (port_addr << 8) | (regnum & 0x1f));
  19. out_be32(&phyregs->miimcon, value);
  20. /* Memory barrier */
  21. mb();
  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. /* Memory barrier */
  36. mb();
  37. /* Initiate a read command, and wait */
  38. out_be32(&phyregs->miimcom, MIIMCOM_READ_CYCLE);
  39. /* Memory barrier */
  40. mb();
  41. /* Wait for the the indication that the read is done */
  42. while ((in_be32(&phyregs->miimind) & (MIIMIND_NOTVALID | MIIMIND_BUSY))
  43. && timeout--)
  44. ;
  45. /* Grab the value read from the PHY */
  46. value = in_be32(&phyregs->miimstat);
  47. return value;
  48. }
  49. static int fsl_pq_mdio_reset(struct mii_dev *bus)
  50. {
  51. struct tsec_mii_mng __iomem *regs =
  52. (struct tsec_mii_mng __iomem *)bus->priv;
  53. /* Reset MII (due to new addresses) */
  54. out_be32(&regs->miimcfg, MIIMCFG_RESET_MGMT);
  55. out_be32(&regs->miimcfg, MIIMCFG_INIT_VALUE);
  56. while (in_be32(&regs->miimind) & MIIMIND_BUSY)
  57. ;
  58. return 0;
  59. }
  60. int tsec_phy_read(struct mii_dev *bus, int addr, int dev_addr, int regnum)
  61. {
  62. struct tsec_mii_mng __iomem *phyregs =
  63. (struct tsec_mii_mng __iomem *)bus->priv;
  64. return tsec_local_mdio_read(phyregs, addr, dev_addr, regnum);
  65. }
  66. int tsec_phy_write(struct mii_dev *bus, int addr, int dev_addr, int regnum,
  67. u16 value)
  68. {
  69. struct tsec_mii_mng __iomem *phyregs =
  70. (struct tsec_mii_mng __iomem *)bus->priv;
  71. tsec_local_mdio_write(phyregs, addr, dev_addr, regnum, value);
  72. return 0;
  73. }
  74. int fsl_pq_mdio_init(bd_t *bis, struct fsl_pq_mdio_info *info)
  75. {
  76. struct mii_dev *bus = mdio_alloc();
  77. if (!bus) {
  78. printf("Failed to allocate FSL MDIO bus\n");
  79. return -1;
  80. }
  81. bus->read = tsec_phy_read;
  82. bus->write = tsec_phy_write;
  83. bus->reset = fsl_pq_mdio_reset;
  84. sprintf(bus->name, info->name);
  85. bus->priv = (void *)info->regs;
  86. return mdio_register(bus);
  87. }