xilinx_ll_temac_mdio.c 4.8 KB

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