ctrl_pex.c 9.4 KB

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