fsl_mdio.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2009-2010, 2013 Freescale Semiconductor, Inc.
  4. * Jun-jie Zhang <b18070@freescale.com>
  5. * Mingkai Hu <Mingkai.hu@freescale.com>
  6. */
  7. #include <common.h>
  8. #include <miiphy.h>
  9. #include <phy.h>
  10. #include <fsl_mdio.h>
  11. #include <asm/io.h>
  12. #include <linux/errno.h>
  13. void tsec_local_mdio_write(struct tsec_mii_mng __iomem *phyregs, int port_addr,
  14. int dev_addr, int regnum, int value)
  15. {
  16. int timeout = 1000000;
  17. out_be32(&phyregs->miimadd, (port_addr << 8) | (regnum & 0x1f));
  18. out_be32(&phyregs->miimcon, value);
  19. /* Memory barrier */
  20. mb();
  21. while ((in_be32(&phyregs->miimind) & MIIMIND_BUSY) && timeout--)
  22. ;
  23. }
  24. int tsec_local_mdio_read(struct tsec_mii_mng __iomem *phyregs, int port_addr,
  25. int dev_addr, int regnum)
  26. {
  27. int value;
  28. int timeout = 1000000;
  29. /* Put the address of the phy, and the register number into MIIMADD */
  30. out_be32(&phyregs->miimadd, (port_addr << 8) | (regnum & 0x1f));
  31. /* Clear the command register, and wait */
  32. out_be32(&phyregs->miimcom, 0);
  33. /* Memory barrier */
  34. mb();
  35. /* Initiate a read command, and wait */
  36. out_be32(&phyregs->miimcom, MIIMCOM_READ_CYCLE);
  37. /* Memory barrier */
  38. mb();
  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. strcpy(bus->name, info->name);
  83. bus->priv = (void *)info->regs;
  84. return mdio_register(bus);
  85. }