phy-comphy-common.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (C) 2018-2021 Marvell International Ltd.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. * https://spdx.org/licenses
  6. */
  7. /* Marvell CP110 ana A3700 common */
  8. #ifndef PHY_COMPHY_COMMON_H
  9. #define PHY_COMPHY_COMMON_H
  10. /* #define DEBUG_COMPHY */
  11. #ifdef DEBUG_COMPHY
  12. #define debug(format...) printf(format)
  13. #else
  14. #define debug(format, arg...)
  15. #endif
  16. /* A lane is described by 4 fields:
  17. * - bit 1~0 represent comphy polarity invert
  18. * - bit 7~2 represent comphy speed
  19. * - bit 11~8 represent unit index
  20. * - bit 16~12 represent mode
  21. * - bit 17 represent comphy indication of clock source
  22. * - bit 20~18 represents pcie width (in case of pcie comphy config.)
  23. * - bit 21 represents the source of the request (Linux/Bootloader),
  24. * (reguired only for PCIe!)
  25. * - bit 31~22 reserved
  26. */
  27. #define COMPHY_INVERT_OFFSET 0
  28. #define COMPHY_INVERT_LEN 2
  29. #define COMPHY_INVERT_MASK COMPHY_MASK(COMPHY_INVERT_OFFSET, \
  30. COMPHY_INVERT_LEN)
  31. #define COMPHY_SPEED_OFFSET (COMPHY_INVERT_OFFSET + COMPHY_INVERT_LEN)
  32. #define COMPHY_SPEED_LEN 6
  33. #define COMPHY_SPEED_MASK COMPHY_MASK(COMPHY_SPEED_OFFSET, \
  34. COMPHY_SPEED_LEN)
  35. #define COMPHY_UNIT_ID_OFFSET (COMPHY_SPEED_OFFSET + COMPHY_SPEED_LEN)
  36. #define COMPHY_UNIT_ID_LEN 4
  37. #define COMPHY_UNIT_ID_MASK COMPHY_MASK(COMPHY_UNIT_ID_OFFSET, \
  38. COMPHY_UNIT_ID_LEN)
  39. #define COMPHY_MODE_OFFSET (COMPHY_UNIT_ID_OFFSET + COMPHY_UNIT_ID_LEN)
  40. #define COMPHY_MODE_LEN 5
  41. #define COMPHY_MODE_MASK COMPHY_MASK(COMPHY_MODE_OFFSET, COMPHY_MODE_LEN)
  42. #define COMPHY_CLK_SRC_OFFSET (COMPHY_MODE_OFFSET + COMPHY_MODE_LEN)
  43. #define COMPHY_CLK_SRC_LEN 1
  44. #define COMPHY_CLK_SRC_MASK COMPHY_MASK(COMPHY_CLK_SRC_OFFSET, \
  45. COMPHY_CLK_SRC_LEN)
  46. #define COMPHY_PCI_WIDTH_OFFSET (COMPHY_CLK_SRC_OFFSET + COMPHY_CLK_SRC_LEN)
  47. #define COMPHY_PCI_WIDTH_LEN 3
  48. #define COMPHY_PCI_WIDTH_MASK COMPHY_MASK(COMPHY_PCI_WIDTH_OFFSET, \
  49. COMPHY_PCI_WIDTH_LEN)
  50. #define COMPHY_PCI_CALLER_OFFSET \
  51. (COMPHY_PCI_WIDTH_OFFSET + COMPHY_PCI_WIDTH_LEN)
  52. #define COMPHY_PCI_CALLER_LEN 1
  53. #define COMPHY_PCI_CALLER_MASK COMPHY_MASK(COMPHY_PCI_CALLER_OFFSET, \
  54. COMPHY_PCI_CALLER_LEN)
  55. #define COMPHY_MASK(offset, len) (((1 << (len)) - 1) << (offset))
  56. /* Macro which extracts mode from lane description */
  57. #define COMPHY_GET_MODE(x) (((x) & COMPHY_MODE_MASK) >> \
  58. COMPHY_MODE_OFFSET)
  59. /* Macro which extracts unit index from lane description */
  60. #define COMPHY_GET_ID(x) (((x) & COMPHY_UNIT_ID_MASK) >> \
  61. COMPHY_UNIT_ID_OFFSET)
  62. /* Macro which extracts speed from lane description */
  63. #define COMPHY_GET_SPEED(x) (((x) & COMPHY_SPEED_MASK) >> \
  64. COMPHY_SPEED_OFFSET)
  65. /* Macro which extracts clock source indication from lane description */
  66. #define COMPHY_GET_CLK_SRC(x) (((x) & COMPHY_CLK_SRC_MASK) >> \
  67. COMPHY_CLK_SRC_OFFSET)
  68. /* Macro which extracts pcie width indication from lane description */
  69. #define COMPHY_GET_PCIE_WIDTH(x) (((x) & COMPHY_PCI_WIDTH_MASK) >> \
  70. COMPHY_PCI_WIDTH_OFFSET)
  71. /* Macro which extracts the caller for pcie power on from lane description */
  72. #define COMPHY_GET_CALLER(x) (((x) & COMPHY_PCI_CALLER_MASK) >> \
  73. COMPHY_PCI_CALLER_OFFSET)
  74. /* Macro which extracts the polarity invert from lane description */
  75. #define COMPHY_GET_POLARITY_INVERT(x) (((x) & COMPHY_INVERT_MASK) >> \
  76. COMPHY_INVERT_OFFSET)
  77. #define COMPHY_SATA_MODE 0x1
  78. #define COMPHY_SGMII_MODE 0x2 /* SGMII 1G */
  79. #define COMPHY_2500BASEX_MODE 0x3 /* 2500Base-X */
  80. #define COMPHY_USB3H_MODE 0x4
  81. #define COMPHY_USB3D_MODE 0x5
  82. #define COMPHY_PCIE_MODE 0x6
  83. #define COMPHY_RXAUI_MODE 0x7
  84. #define COMPHY_XFI_MODE 0x8
  85. #define COMPHY_SFI_MODE 0x9
  86. #define COMPHY_USB3_MODE 0xa
  87. #define COMPHY_AP_MODE 0xb
  88. #define COMPHY_UNUSED 0xFFFFFFFF
  89. /* Polarity invert macro */
  90. #define COMPHY_POLARITY_NO_INVERT 0
  91. #define COMPHY_POLARITY_TXD_INVERT 1
  92. #define COMPHY_POLARITY_RXD_INVERT 2
  93. #define COMPHY_POLARITY_ALL_INVERT (COMPHY_POLARITY_TXD_INVERT | \
  94. COMPHY_POLARITY_RXD_INVERT)
  95. enum reg_width_type {
  96. REG_16BIT = 0,
  97. REG_32BIT,
  98. };
  99. enum {
  100. COMPHY_LANE0 = 0,
  101. COMPHY_LANE1,
  102. COMPHY_LANE2,
  103. COMPHY_LANE3,
  104. COMPHY_LANE4,
  105. COMPHY_LANE5,
  106. COMPHY_LANE_MAX,
  107. };
  108. static inline uint32_t polling_with_timeout(uintptr_t addr, uint32_t val,
  109. uint32_t mask,
  110. uint32_t usec_timeout,
  111. enum reg_width_type type)
  112. {
  113. uint32_t data;
  114. do {
  115. udelay(1);
  116. if (type == REG_16BIT)
  117. data = mmio_read_16(addr) & mask;
  118. else
  119. data = mmio_read_32(addr) & mask;
  120. } while (data != val && --usec_timeout > 0);
  121. if (usec_timeout == 0)
  122. return data;
  123. return 0;
  124. }
  125. static inline void reg_set(uintptr_t addr, uint32_t data, uint32_t mask)
  126. {
  127. debug("<atf>: WR to addr = 0x%lx, data = 0x%x (mask = 0x%x) - ",
  128. addr, data, mask);
  129. debug("old value = 0x%x ==> ", mmio_read_32(addr));
  130. mmio_clrsetbits_32(addr, mask, data & mask);
  131. debug("new val 0x%x\n", mmio_read_32(addr));
  132. }
  133. static inline void __unused reg_set16(uintptr_t addr, uint16_t data,
  134. uint16_t mask)
  135. {
  136. debug("<atf>: WR to addr = 0x%lx, data = 0x%x (mask = 0x%x) - ",
  137. addr, data, mask);
  138. debug("old value = 0x%x ==> ", mmio_read_16(addr));
  139. mmio_clrsetbits_16(addr, mask, data & mask);
  140. debug("new val 0x%x\n", mmio_read_16(addr));
  141. }
  142. #endif /* PHY_COMPHY_COMMON_H */