netcp_sgmii.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * SGMI module initialisation
  3. *
  4. * Copyright (C) 2014 Texas Instruments Incorporated
  5. * Authors: Sandeep Nair <sandeep_n@ti.com>
  6. * Sandeep Paulraj <s-paulraj@ti.com>
  7. * Wingman Kwok <w-kwok2@ti.com>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation version 2.
  12. *
  13. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  14. * kind, whether express or implied; without even the implied warranty
  15. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include "netcp.h"
  19. #define SGMII_SRESET_RESET BIT(0)
  20. #define SGMII_SRESET_RTRESET BIT(1)
  21. #define SGMII_REG_STATUS_LOCK BIT(4)
  22. #define SGMII_REG_STATUS_LINK BIT(0)
  23. #define SGMII_REG_STATUS_AUTONEG BIT(2)
  24. #define SGMII_REG_CONTROL_AUTONEG BIT(0)
  25. #define SGMII23_OFFSET(x) ((x - 2) * 0x100)
  26. #define SGMII_OFFSET(x) ((x <= 1) ? (x * 0x100) : (SGMII23_OFFSET(x)))
  27. /* SGMII registers */
  28. #define SGMII_SRESET_REG(x) (SGMII_OFFSET(x) + 0x004)
  29. #define SGMII_CTL_REG(x) (SGMII_OFFSET(x) + 0x010)
  30. #define SGMII_STATUS_REG(x) (SGMII_OFFSET(x) + 0x014)
  31. #define SGMII_MRADV_REG(x) (SGMII_OFFSET(x) + 0x018)
  32. static void sgmii_write_reg(void __iomem *base, int reg, u32 val)
  33. {
  34. writel(val, base + reg);
  35. }
  36. static u32 sgmii_read_reg(void __iomem *base, int reg)
  37. {
  38. return readl(base + reg);
  39. }
  40. static void sgmii_write_reg_bit(void __iomem *base, int reg, u32 val)
  41. {
  42. writel((readl(base + reg) | val), base + reg);
  43. }
  44. /* port is 0 based */
  45. int netcp_sgmii_reset(void __iomem *sgmii_ofs, int port)
  46. {
  47. /* Soft reset */
  48. sgmii_write_reg_bit(sgmii_ofs, SGMII_SRESET_REG(port),
  49. SGMII_SRESET_RESET);
  50. while ((sgmii_read_reg(sgmii_ofs, SGMII_SRESET_REG(port)) &
  51. SGMII_SRESET_RESET) != 0x0)
  52. ;
  53. return 0;
  54. }
  55. /* port is 0 based */
  56. bool netcp_sgmii_rtreset(void __iomem *sgmii_ofs, int port, bool set)
  57. {
  58. u32 reg;
  59. bool oldval;
  60. /* Initiate a soft reset */
  61. reg = sgmii_read_reg(sgmii_ofs, SGMII_SRESET_REG(port));
  62. oldval = (reg & SGMII_SRESET_RTRESET) != 0x0;
  63. if (set)
  64. reg |= SGMII_SRESET_RTRESET;
  65. else
  66. reg &= ~SGMII_SRESET_RTRESET;
  67. sgmii_write_reg(sgmii_ofs, SGMII_SRESET_REG(port), reg);
  68. wmb();
  69. return oldval;
  70. }
  71. int netcp_sgmii_get_port_link(void __iomem *sgmii_ofs, int port)
  72. {
  73. u32 status = 0, link = 0;
  74. status = sgmii_read_reg(sgmii_ofs, SGMII_STATUS_REG(port));
  75. if ((status & SGMII_REG_STATUS_LINK) != 0)
  76. link = 1;
  77. return link;
  78. }
  79. int netcp_sgmii_config(void __iomem *sgmii_ofs, int port, u32 interface)
  80. {
  81. unsigned int i, status, mask;
  82. u32 mr_adv_ability;
  83. u32 control;
  84. switch (interface) {
  85. case SGMII_LINK_MAC_MAC_AUTONEG:
  86. mr_adv_ability = 0x9801;
  87. control = 0x21;
  88. break;
  89. case SGMII_LINK_MAC_PHY:
  90. case SGMII_LINK_MAC_PHY_NO_MDIO:
  91. mr_adv_ability = 1;
  92. control = 1;
  93. break;
  94. case SGMII_LINK_MAC_MAC_FORCED:
  95. mr_adv_ability = 0x9801;
  96. control = 0x20;
  97. break;
  98. case SGMII_LINK_MAC_FIBER:
  99. mr_adv_ability = 0x20;
  100. control = 0x1;
  101. break;
  102. default:
  103. WARN_ONCE(1, "Invalid sgmii interface: %d\n", interface);
  104. return -EINVAL;
  105. }
  106. sgmii_write_reg(sgmii_ofs, SGMII_CTL_REG(port), 0);
  107. /* Wait for the SerDes pll to lock */
  108. for (i = 0; i < 1000; i++) {
  109. usleep_range(1000, 2000);
  110. status = sgmii_read_reg(sgmii_ofs, SGMII_STATUS_REG(port));
  111. if ((status & SGMII_REG_STATUS_LOCK) != 0)
  112. break;
  113. }
  114. if ((status & SGMII_REG_STATUS_LOCK) == 0)
  115. pr_err("serdes PLL not locked\n");
  116. sgmii_write_reg(sgmii_ofs, SGMII_MRADV_REG(port), mr_adv_ability);
  117. sgmii_write_reg(sgmii_ofs, SGMII_CTL_REG(port), control);
  118. mask = SGMII_REG_STATUS_LINK;
  119. if (control & SGMII_REG_CONTROL_AUTONEG)
  120. mask |= SGMII_REG_STATUS_AUTONEG;
  121. for (i = 0; i < 1000; i++) {
  122. usleep_range(200, 500);
  123. status = sgmii_read_reg(sgmii_ofs, SGMII_STATUS_REG(port));
  124. if ((status & mask) == mask)
  125. break;
  126. }
  127. return 0;
  128. }