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