xilinx_ll_temac_mdio.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Xilinx xps_ll_temac ethernet driver for u-boot
  3. *
  4. * MDIO bus access
  5. *
  6. * Copyright (C) 2011 - 2012 Stephan Linz <linz@li-pro.net>
  7. * Copyright (C) 2008 - 2011 Michal Simek <monstr@monstr.eu>
  8. * Copyright (C) 2008 - 2011 PetaLogix
  9. *
  10. * Based on Yoshio Kashiwagi kashiwagi@co-nss.co.jp driver
  11. * Copyright (C) 2008 Nissin Systems Co.,Ltd.
  12. * March 2008 created
  13. *
  14. * CREDITS: tsec driver
  15. *
  16. * SPDX-License-Identifier: GPL-2.0+
  17. *
  18. * [0]: http://www.xilinx.com/support/documentation
  19. *
  20. * [S]: [0]/ip_documentation/xps_ll_temac.pdf
  21. * [A]: [0]/application_notes/xapp1041.pdf
  22. */
  23. #include <config.h>
  24. #include <common.h>
  25. #include <miiphy.h>
  26. #include <phy.h>
  27. #include <malloc.h>
  28. #include <asm/io.h>
  29. #include "xilinx_ll_temac.h"
  30. #include "xilinx_ll_temac_mdio.h"
  31. #if !defined(CONFIG_MII)
  32. # error "LL_TEMAC requires MII -- missing CONFIG_MII"
  33. #endif
  34. #if !defined(CONFIG_PHYLIB)
  35. # error "LL_TEMAC requires PHYLIB -- missing CONFIG_PHYLIB"
  36. #endif
  37. /*
  38. * Prior to PHY access, the MDIO clock must be setup. This driver will set a
  39. * safe default that should work with PLB bus speeds of up to 150 MHz and keep
  40. * the MDIO clock below 2.5 MHz. If the user wishes faster access to the PHY
  41. * then the clock divisor can be set to a different value by setting the
  42. * correct bus speed value with CONFIG_XILINX_LL_TEMAC_CLK.
  43. */
  44. #if !defined(CONFIG_XILINX_LL_TEMAC_CLK)
  45. #define MDIO_CLOCK_DIV MC_CLKDIV_10(150000000)
  46. #else
  47. #define MDIO_CLOCK_DIV MC_CLKDIV_25(CONFIG_XILINX_LL_TEMAC_CLK)
  48. #endif
  49. static int ll_temac_mdio_setup(struct mii_dev *bus)
  50. {
  51. struct temac_reg *regs = (struct temac_reg *)bus->priv;
  52. /* setup MDIO clock */
  53. ll_temac_indirect_set(regs, TEMAC_MC,
  54. MC_MDIOEN | (MDIO_CLOCK_DIV & MC_CLKDIV_MASK));
  55. return 0;
  56. }
  57. /*
  58. * Indirect MII PHY read via ll_temac.
  59. *
  60. * http://www.xilinx.com/support/documentation/ip_documentation/xps_ll_temac.pdf
  61. * page 67, Using the MII Management to Access PHY Registers
  62. */
  63. int ll_temac_local_mdio_read(struct temac_reg *regs, int addr, int devad,
  64. int regnum)
  65. {
  66. out_be32(&regs->lsw,
  67. ((addr << LSW_PHYAD_POS) & LSW_PHYAD_MASK) |
  68. (regnum & LSW_REGAD_MASK));
  69. out_be32(&regs->ctl, TEMAC_MIIMAI);
  70. ll_temac_check_status(regs, RSE_MIIM_RR);
  71. return in_be32(&regs->lsw) & LSW_REGDAT_MASK;
  72. }
  73. /*
  74. * Indirect MII PHY write via ll_temac.
  75. *
  76. * http://www.xilinx.com/support/documentation/ip_documentation/xps_ll_temac.pdf
  77. * page 67, Using the MII Management to Access PHY Registers
  78. */
  79. void ll_temac_local_mdio_write(struct temac_reg *regs, int addr, int devad,
  80. int regnum, u16 value)
  81. {
  82. out_be32(&regs->lsw, (value & LSW_REGDAT_MASK));
  83. out_be32(&regs->ctl, CTL_WEN | TEMAC_MIIMWD);
  84. out_be32(&regs->lsw,
  85. ((addr << LSW_PHYAD_POS) & LSW_PHYAD_MASK) |
  86. (regnum & LSW_REGAD_MASK));
  87. out_be32(&regs->ctl, CTL_WEN | TEMAC_MIIMAI);
  88. ll_temac_check_status(regs, RSE_MIIM_WR);
  89. }
  90. int ll_temac_phy_read(struct mii_dev *bus, int addr, int devad, int regnum)
  91. {
  92. struct temac_reg *regs = (struct temac_reg *)bus->priv;
  93. return ll_temac_local_mdio_read(regs, addr, devad, regnum);
  94. }
  95. int ll_temac_phy_write(struct mii_dev *bus, int addr, int devad, int regnum,
  96. u16 value)
  97. {
  98. struct temac_reg *regs = (struct temac_reg *)bus->priv;
  99. ll_temac_local_mdio_write(regs, addr, devad, regnum, value);
  100. return 0;
  101. }
  102. /*
  103. * Use MII register 1 (MII status register) to detect PHY
  104. *
  105. * A Mask used to verify certain PHY features (register content)
  106. * in the PHY detection register:
  107. * Auto-negotiation support, 10Mbps half/full duplex support
  108. */
  109. #define PHY_DETECT_REG MII_BMSR
  110. #define PHY_DETECT_MASK (BMSR_10FULL | BMSR_10HALF | BMSR_ANEGCAPABLE)
  111. /* Looking for a valid PHY address */
  112. int ll_temac_phy_addr(struct mii_dev *bus)
  113. {
  114. struct temac_reg *regs = (struct temac_reg *)bus->priv;
  115. unsigned short val;
  116. unsigned int phy;
  117. for (phy = PHY_MAX_ADDR; phy >= 0; phy--) {
  118. val = ll_temac_local_mdio_read(regs, phy, 0, PHY_DETECT_REG);
  119. if ((val != 0xFFFF) &&
  120. ((val & PHY_DETECT_MASK) == PHY_DETECT_MASK)) {
  121. /* Found a valid PHY address */
  122. return phy;
  123. }
  124. }
  125. return -1;
  126. }
  127. int xilinx_ll_temac_mdio_initialize(bd_t *bis, struct ll_temac_mdio_info *info)
  128. {
  129. struct mii_dev *bus = mdio_alloc();
  130. if (!bus) {
  131. printf("Failed to allocate LL_TEMAC MDIO bus: %s\n",
  132. info->name);
  133. return -1;
  134. }
  135. bus->read = ll_temac_phy_read;
  136. bus->write = ll_temac_phy_write;
  137. bus->reset = NULL;
  138. /* use given name or generate its own unique name */
  139. if (info->name) {
  140. strncpy(bus->name, info->name, MDIO_NAME_LEN);
  141. } else {
  142. snprintf(bus->name, MDIO_NAME_LEN, "lltemii.%p", info->regs);
  143. info->name = bus->name;
  144. }
  145. bus->priv = info->regs;
  146. ll_temac_mdio_setup(bus);
  147. return mdio_register(bus);
  148. }