ctrl_pex.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) Marvell International Ltd. and its affiliates
  4. */
  5. #include <common.h>
  6. #include <spl.h>
  7. #include <asm/io.h>
  8. #include <asm/arch/cpu.h>
  9. #include <asm/arch/soc.h>
  10. #include "ctrl_pex.h"
  11. #include "sys_env_lib.h"
  12. __weak void board_pex_config(void)
  13. {
  14. /* nothing in this weak default implementation */
  15. }
  16. int hws_pex_config(const struct serdes_map *serdes_map, u8 count)
  17. {
  18. u32 pex_idx, tmp, next_busno, first_busno, temp_pex_reg,
  19. temp_reg, addr, dev_id, ctrl_mode;
  20. enum serdes_type serdes_type;
  21. u32 idx;
  22. DEBUG_INIT_FULL_S("\n### hws_pex_config ###\n");
  23. for (idx = 0; idx < count; idx++) {
  24. serdes_type = serdes_map[idx].serdes_type;
  25. /* configuration for PEX only */
  26. if ((serdes_type != PEX0) && (serdes_type != PEX1) &&
  27. (serdes_type != PEX2) && (serdes_type != PEX3))
  28. continue;
  29. if ((serdes_type != PEX0) &&
  30. ((serdes_map[idx].serdes_mode == PEX_ROOT_COMPLEX_X4) ||
  31. (serdes_map[idx].serdes_mode == PEX_END_POINT_X4))) {
  32. /* for PEX by4 - relevant for the first port only */
  33. continue;
  34. }
  35. pex_idx = serdes_type - PEX0;
  36. tmp = reg_read(PEX_CAPABILITIES_REG(pex_idx));
  37. tmp &= ~(0xf << 20);
  38. tmp |= (0x4 << 20);
  39. reg_write(PEX_CAPABILITIES_REG(pex_idx), tmp);
  40. }
  41. tmp = reg_read(SOC_CTRL_REG);
  42. tmp &= ~0x03;
  43. for (idx = 0; idx < count; idx++) {
  44. serdes_type = serdes_map[idx].serdes_type;
  45. if ((serdes_type != PEX0) &&
  46. ((serdes_map[idx].serdes_mode == PEX_ROOT_COMPLEX_X4) ||
  47. (serdes_map[idx].serdes_mode == PEX_END_POINT_X4))) {
  48. /* for PEX by4 - relevant for the first port only */
  49. continue;
  50. }
  51. switch (serdes_type) {
  52. case PEX0:
  53. tmp |= 0x1 << PCIE0_ENABLE_OFFS;
  54. break;
  55. case PEX1:
  56. tmp |= 0x1 << PCIE1_ENABLE_OFFS;
  57. break;
  58. case PEX2:
  59. tmp |= 0x1 << PCIE2_ENABLE_OFFS;
  60. break;
  61. case PEX3:
  62. tmp |= 0x1 << PCIE3_ENABLE_OFFS;
  63. break;
  64. default:
  65. break;
  66. }
  67. }
  68. reg_write(SOC_CTRL_REG, tmp);
  69. /* Support gen1/gen2 */
  70. DEBUG_INIT_FULL_S("Support gen1/gen2\n");
  71. board_pex_config();
  72. next_busno = 0;
  73. mdelay(150);
  74. for (idx = 0; idx < count; idx++) {
  75. serdes_type = serdes_map[idx].serdes_type;
  76. DEBUG_INIT_FULL_S(" serdes_type=0x");
  77. DEBUG_INIT_FULL_D(serdes_type, 8);
  78. DEBUG_INIT_FULL_S("\n");
  79. DEBUG_INIT_FULL_S(" idx=0x");
  80. DEBUG_INIT_FULL_D(idx, 8);
  81. DEBUG_INIT_FULL_S("\n");
  82. /* Configuration for PEX only */
  83. if ((serdes_type != PEX0) && (serdes_type != PEX1) &&
  84. (serdes_type != PEX2) && (serdes_type != PEX3))
  85. continue;
  86. if ((serdes_type != PEX0) &&
  87. ((serdes_map[idx].serdes_mode == PEX_ROOT_COMPLEX_X4) ||
  88. (serdes_map[idx].serdes_mode == PEX_END_POINT_X4))) {
  89. /* for PEX by4 - relevant for the first port only */
  90. continue;
  91. }
  92. pex_idx = serdes_type - PEX0;
  93. tmp = reg_read(PEX_DBG_STATUS_REG(pex_idx));
  94. first_busno = next_busno;
  95. if ((tmp & 0x7f) != 0x7e) {
  96. DEBUG_INIT_S("PCIe, Idx ");
  97. DEBUG_INIT_D(pex_idx, 1);
  98. DEBUG_INIT_S(": detected no link\n");
  99. continue;
  100. }
  101. next_busno++;
  102. temp_pex_reg = reg_read((PEX_CFG_DIRECT_ACCESS
  103. (pex_idx, PEX_LINK_CAPABILITY_REG)));
  104. temp_pex_reg &= 0xf;
  105. if (temp_pex_reg != 0x2)
  106. continue;
  107. temp_reg = (reg_read(PEX_CFG_DIRECT_ACCESS(
  108. pex_idx,
  109. PEX_LINK_CTRL_STAT_REG)) &
  110. 0xf0000) >> 16;
  111. /* Check if the link established is GEN1 */
  112. DEBUG_INIT_FULL_S
  113. ("Checking if the link established is gen1\n");
  114. if (temp_reg != 0x1)
  115. continue;
  116. pex_local_bus_num_set(pex_idx, first_busno);
  117. pex_local_dev_num_set(pex_idx, 1);
  118. DEBUG_INIT_FULL_S("PCIe, Idx ");
  119. DEBUG_INIT_FULL_D(pex_idx, 1);
  120. DEBUG_INIT_S(":** Link is Gen1, check the EP capability\n");
  121. /* link is Gen1, check the EP capability */
  122. addr = pex_config_read(pex_idx, first_busno, 0, 0, 0x34) & 0xff;
  123. DEBUG_INIT_FULL_C("pex_config_read: return addr=0x%x", addr, 4);
  124. if (addr == 0xff) {
  125. DEBUG_INIT_FULL_C
  126. ("pex_config_read: return 0xff -->PCIe (%d): Detected No Link.",
  127. pex_idx, 1);
  128. continue;
  129. }
  130. while ((pex_config_read(pex_idx, first_busno, 0, 0, addr)
  131. & 0xff) != 0x10) {
  132. addr = (pex_config_read(pex_idx, first_busno, 0,
  133. 0, addr) & 0xff00) >> 8;
  134. }
  135. /* Check for Gen2 and above */
  136. if ((pex_config_read(pex_idx, first_busno, 0, 0,
  137. addr + 0xc) & 0xf) < 0x2) {
  138. DEBUG_INIT_S("PCIe, Idx ");
  139. DEBUG_INIT_D(pex_idx, 1);
  140. DEBUG_INIT_S(": remains Gen1\n");
  141. continue;
  142. }
  143. tmp = reg_read(PEX_LINK_CTRL_STATUS2_REG(pex_idx));
  144. DEBUG_RD_REG(PEX_LINK_CTRL_STATUS2_REG(pex_idx), tmp);
  145. tmp &= ~(BIT(0) | BIT(1));
  146. tmp |= BIT(1);
  147. tmp |= BIT(6); /* Select Deemphasize (-3.5d_b) */
  148. reg_write(PEX_LINK_CTRL_STATUS2_REG(pex_idx), tmp);
  149. DEBUG_WR_REG(PEX_LINK_CTRL_STATUS2_REG(pex_idx), tmp);
  150. tmp = reg_read(PEX_CTRL_REG(pex_idx));
  151. DEBUG_RD_REG(PEX_CTRL_REG(pex_idx), tmp);
  152. tmp |= BIT(10);
  153. reg_write(PEX_CTRL_REG(pex_idx), tmp);
  154. DEBUG_WR_REG(PEX_CTRL_REG(pex_idx), tmp);
  155. /*
  156. * We need to wait 10ms before reading the PEX_DBG_STATUS_REG
  157. * in order not to read the status of the former state
  158. */
  159. mdelay(10);
  160. DEBUG_INIT_S("PCIe, Idx ");
  161. DEBUG_INIT_D(pex_idx, 1);
  162. DEBUG_INIT_S
  163. (": Link upgraded to Gen2 based on client capabilities\n");
  164. }
  165. /* Update pex DEVICE ID */
  166. ctrl_mode = sys_env_model_get();
  167. for (idx = 0; idx < count; idx++) {
  168. serdes_type = serdes_map[idx].serdes_type;
  169. /* configuration for PEX only */
  170. if ((serdes_type != PEX0) && (serdes_type != PEX1) &&
  171. (serdes_type != PEX2) && (serdes_type != PEX3))
  172. continue;
  173. if ((serdes_type != PEX0) &&
  174. ((serdes_map[idx].serdes_mode == PEX_ROOT_COMPLEX_X4) ||
  175. (serdes_map[idx].serdes_mode == PEX_END_POINT_X4))) {
  176. /* for PEX by4 - relevant for the first port only */
  177. continue;
  178. }
  179. pex_idx = serdes_type - PEX0;
  180. dev_id = reg_read(PEX_CFG_DIRECT_ACCESS
  181. (pex_idx, PEX_DEVICE_AND_VENDOR_ID));
  182. dev_id &= 0xffff;
  183. dev_id |= ((ctrl_mode << 16) & 0xffff0000);
  184. reg_write(PEX_CFG_DIRECT_ACCESS
  185. (pex_idx, PEX_DEVICE_AND_VENDOR_ID), dev_id);
  186. }
  187. DEBUG_INIT_FULL_C("Update PEX Device ID ", ctrl_mode, 4);
  188. return MV_OK;
  189. }
  190. int pex_local_bus_num_set(u32 pex_if, u32 bus_num)
  191. {
  192. u32 pex_status;
  193. DEBUG_INIT_FULL_S("\n### pex_local_bus_num_set ###\n");
  194. if (bus_num >= MAX_PEX_BUSSES) {
  195. DEBUG_INIT_C("pex_local_bus_num_set: Illegal bus number %d\n",
  196. bus_num, 4);
  197. return MV_BAD_PARAM;
  198. }
  199. pex_status = reg_read(PEX_STATUS_REG(pex_if));
  200. pex_status &= ~PXSR_PEX_BUS_NUM_MASK;
  201. pex_status |=
  202. (bus_num << PXSR_PEX_BUS_NUM_OFFS) & PXSR_PEX_BUS_NUM_MASK;
  203. reg_write(PEX_STATUS_REG(pex_if), pex_status);
  204. return MV_OK;
  205. }
  206. int pex_local_dev_num_set(u32 pex_if, u32 dev_num)
  207. {
  208. u32 pex_status;
  209. DEBUG_INIT_FULL_S("\n### pex_local_dev_num_set ###\n");
  210. pex_status = reg_read(PEX_STATUS_REG(pex_if));
  211. pex_status &= ~PXSR_PEX_DEV_NUM_MASK;
  212. pex_status |=
  213. (dev_num << PXSR_PEX_DEV_NUM_OFFS) & PXSR_PEX_DEV_NUM_MASK;
  214. reg_write(PEX_STATUS_REG(pex_if), pex_status);
  215. return MV_OK;
  216. }
  217. /*
  218. * pex_config_read - Read from configuration space
  219. *
  220. * DESCRIPTION:
  221. * This function performs a 32 bit read from PEX configuration space.
  222. * It supports both type 0 and type 1 of Configuration Transactions
  223. * (local and over bridge). In order to read from local bus segment, use
  224. * bus number retrieved from pex_local_bus_num_get(). Other bus numbers
  225. * will result configuration transaction of type 1 (over bridge).
  226. *
  227. * INPUT:
  228. * pex_if - PEX interface number.
  229. * bus - PEX segment bus number.
  230. * dev - PEX device number.
  231. * func - Function number.
  232. * reg_offs - Register offset.
  233. *
  234. * OUTPUT:
  235. * None.
  236. *
  237. * RETURN:
  238. * 32bit register data, 0xffffffff on error
  239. */
  240. u32 pex_config_read(u32 pex_if, u32 bus, u32 dev, u32 func, u32 reg_off)
  241. {
  242. u32 pex_data = 0;
  243. u32 local_dev, local_bus;
  244. u32 pex_status;
  245. pex_status = reg_read(PEX_STATUS_REG(pex_if));
  246. local_dev =
  247. ((pex_status & PXSR_PEX_DEV_NUM_MASK) >> PXSR_PEX_DEV_NUM_OFFS);
  248. local_bus =
  249. ((pex_status & PXSR_PEX_BUS_NUM_MASK) >> PXSR_PEX_BUS_NUM_OFFS);
  250. /*
  251. * In PCI Express we have only one device number
  252. * and this number is the first number we encounter
  253. * else that the local_dev
  254. * spec pex define return on config read/write on any device
  255. */
  256. if (bus == local_bus) {
  257. if (local_dev == 0) {
  258. /*
  259. * if local dev is 0 then the first number we encounter
  260. * after 0 is 1
  261. */
  262. if ((dev != 1) && (dev != local_dev))
  263. return MV_ERROR;
  264. } else {
  265. /*
  266. * if local dev is not 0 then the first number we
  267. * encounter is 0
  268. */
  269. if ((dev != 0) && (dev != local_dev))
  270. return MV_ERROR;
  271. }
  272. }
  273. /* Creating PEX address to be passed */
  274. pex_data = (bus << PXCAR_BUS_NUM_OFFS);
  275. pex_data |= (dev << PXCAR_DEVICE_NUM_OFFS);
  276. pex_data |= (func << PXCAR_FUNC_NUM_OFFS);
  277. /* Legacy register space */
  278. pex_data |= (reg_off & PXCAR_REG_NUM_MASK);
  279. /* Extended register space */
  280. pex_data |= (((reg_off & PXCAR_REAL_EXT_REG_NUM_MASK) >>
  281. PXCAR_REAL_EXT_REG_NUM_OFFS) << PXCAR_EXT_REG_NUM_OFFS);
  282. pex_data |= PXCAR_CONFIG_EN;
  283. /* Write the address to the PEX configuration address register */
  284. reg_write(PEX_CFG_ADDR_REG(pex_if), pex_data);
  285. /*
  286. * In order to let the PEX controller absorbed the address
  287. * of the read transaction we perform a validity check that
  288. * the address was written
  289. */
  290. if (pex_data != reg_read(PEX_CFG_ADDR_REG(pex_if)))
  291. return MV_ERROR;
  292. /* Cleaning Master Abort */
  293. reg_bit_set(PEX_CFG_DIRECT_ACCESS(pex_if, PEX_STATUS_AND_COMMAND),
  294. PXSAC_MABORT);
  295. /* Read the Data returned in the PEX Data register */
  296. pex_data = reg_read(PEX_CFG_DATA_REG(pex_if));
  297. DEBUG_INIT_FULL_C(" --> ", pex_data, 4);
  298. return pex_data;
  299. }