ctrl_pex.c 9.4 KB

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