netcp_sgmii.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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_REG_STATUS_LOCK BIT(4)
  20. #define SGMII_REG_STATUS_LINK BIT(0)
  21. #define SGMII_REG_STATUS_AUTONEG BIT(2)
  22. #define SGMII_REG_CONTROL_AUTONEG BIT(0)
  23. #define SGMII23_OFFSET(x) ((x - 2) * 0x100)
  24. #define SGMII_OFFSET(x) ((x <= 1) ? (x * 0x100) : (SGMII23_OFFSET(x)))
  25. /* SGMII registers */
  26. #define SGMII_SRESET_REG(x) (SGMII_OFFSET(x) + 0x004)
  27. #define SGMII_CTL_REG(x) (SGMII_OFFSET(x) + 0x010)
  28. #define SGMII_STATUS_REG(x) (SGMII_OFFSET(x) + 0x014)
  29. #define SGMII_MRADV_REG(x) (SGMII_OFFSET(x) + 0x018)
  30. static void sgmii_write_reg(void __iomem *base, int reg, u32 val)
  31. {
  32. writel(val, base + reg);
  33. }
  34. static u32 sgmii_read_reg(void __iomem *base, int reg)
  35. {
  36. return readl(base + reg);
  37. }
  38. static void sgmii_write_reg_bit(void __iomem *base, int reg, u32 val)
  39. {
  40. writel((readl(base + reg) | val), base + reg);
  41. }
  42. /* port is 0 based */
  43. int netcp_sgmii_reset(void __iomem *sgmii_ofs, int port)
  44. {
  45. /* Soft reset */
  46. sgmii_write_reg_bit(sgmii_ofs, SGMII_SRESET_REG(port), 0x1);
  47. while (sgmii_read_reg(sgmii_ofs, SGMII_SRESET_REG(port)) != 0x0)
  48. ;
  49. return 0;
  50. }
  51. int netcp_sgmii_get_port_link(void __iomem *sgmii_ofs, int port)
  52. {
  53. u32 status = 0, link = 0;
  54. status = sgmii_read_reg(sgmii_ofs, SGMII_STATUS_REG(port));
  55. if ((status & SGMII_REG_STATUS_LINK) != 0)
  56. link = 1;
  57. return link;
  58. }
  59. int netcp_sgmii_config(void __iomem *sgmii_ofs, int port, u32 interface)
  60. {
  61. unsigned int i, status, mask;
  62. u32 mr_adv_ability;
  63. u32 control;
  64. switch (interface) {
  65. case SGMII_LINK_MAC_MAC_AUTONEG:
  66. mr_adv_ability = 0x9801;
  67. control = 0x21;
  68. break;
  69. case SGMII_LINK_MAC_PHY:
  70. case SGMII_LINK_MAC_PHY_NO_MDIO:
  71. mr_adv_ability = 1;
  72. control = 1;
  73. break;
  74. case SGMII_LINK_MAC_MAC_FORCED:
  75. mr_adv_ability = 0x9801;
  76. control = 0x20;
  77. break;
  78. case SGMII_LINK_MAC_FIBER:
  79. mr_adv_ability = 0x20;
  80. control = 0x1;
  81. break;
  82. default:
  83. WARN_ONCE(1, "Invalid sgmii interface: %d\n", interface);
  84. return -EINVAL;
  85. }
  86. sgmii_write_reg(sgmii_ofs, SGMII_CTL_REG(port), 0);
  87. /* Wait for the SerDes pll to lock */
  88. for (i = 0; i < 1000; i++) {
  89. usleep_range(1000, 2000);
  90. status = sgmii_read_reg(sgmii_ofs, SGMII_STATUS_REG(port));
  91. if ((status & SGMII_REG_STATUS_LOCK) != 0)
  92. break;
  93. }
  94. if ((status & SGMII_REG_STATUS_LOCK) == 0)
  95. pr_err("serdes PLL not locked\n");
  96. sgmii_write_reg(sgmii_ofs, SGMII_MRADV_REG(port), mr_adv_ability);
  97. sgmii_write_reg(sgmii_ofs, SGMII_CTL_REG(port), control);
  98. mask = SGMII_REG_STATUS_LINK;
  99. if (control & SGMII_REG_CONTROL_AUTONEG)
  100. mask |= SGMII_REG_STATUS_AUTONEG;
  101. for (i = 0; i < 1000; i++) {
  102. usleep_range(200, 500);
  103. status = sgmii_read_reg(sgmii_ofs, SGMII_STATUS_REG(port));
  104. if ((status & mask) == mask)
  105. break;
  106. }
  107. return 0;
  108. }