hte.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. // SPDX-License-Identifier: Intel
  2. /*
  3. * Copyright (C) 2013, Intel Corporation
  4. * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com>
  5. *
  6. * Ported from Intel released Quark UEFI BIOS
  7. * QuarkSocPkg/QuarkNorthCluster/MemoryInit/Pei
  8. */
  9. #include <common.h>
  10. #include <asm/arch/mrc.h>
  11. #include <asm/arch/msg_port.h>
  12. #include "mrc_util.h"
  13. #include "hte.h"
  14. /**
  15. * Enable HTE to detect all possible errors for the given training parameters
  16. * (per-bit or full byte lane).
  17. */
  18. static void hte_enable_all_errors(void)
  19. {
  20. msg_port_write(HTE, 0x000200a2, 0xffffffff);
  21. msg_port_write(HTE, 0x000200a3, 0x000000ff);
  22. msg_port_write(HTE, 0x000200a4, 0x00000000);
  23. }
  24. /**
  25. * Go and read the HTE register in order to find any error
  26. *
  27. * @return: The errors detected in the HTE status register
  28. */
  29. static u32 hte_check_errors(void)
  30. {
  31. return msg_port_read(HTE, 0x000200a7);
  32. }
  33. /**
  34. * Wait until HTE finishes
  35. */
  36. static void hte_wait_for_complete(void)
  37. {
  38. u32 tmp;
  39. ENTERFN();
  40. do {} while ((msg_port_read(HTE, 0x00020012) & (1 << 30)) != 0);
  41. tmp = msg_port_read(HTE, 0x00020011);
  42. tmp |= (1 << 9);
  43. tmp &= ~((1 << 12) | (1 << 13));
  44. msg_port_write(HTE, 0x00020011, tmp);
  45. LEAVEFN();
  46. }
  47. /**
  48. * Clear registers related with errors in the HTE
  49. */
  50. static void hte_clear_error_regs(void)
  51. {
  52. u32 tmp;
  53. /*
  54. * Clear all HTE errors and enable error checking
  55. * for burst and chunk.
  56. */
  57. tmp = msg_port_read(HTE, 0x000200a1);
  58. tmp |= (1 << 8);
  59. msg_port_write(HTE, 0x000200a1, tmp);
  60. }
  61. /**
  62. * Execute a basic single-cache-line memory write/read/verify test using simple
  63. * constant pattern, different for READ_TRAIN and WRITE_TRAIN modes.
  64. *
  65. * See hte_basic_write_read() which is the external visible wrapper.
  66. *
  67. * @mrc_params: host structure for all MRC global data
  68. * @addr: memory adress being tested (must hit specific channel/rank)
  69. * @first_run: if set then the HTE registers are configured, otherwise it is
  70. * assumed configuration is done and we just re-run the test
  71. * @mode: READ_TRAIN or WRITE_TRAIN (the difference is in the pattern)
  72. *
  73. * @return: byte lane failure on each bit (for Quark only bit0 and bit1)
  74. */
  75. static u16 hte_basic_data_cmp(struct mrc_params *mrc_params, u32 addr,
  76. u8 first_run, u8 mode)
  77. {
  78. u32 pattern;
  79. u32 offset;
  80. if (first_run) {
  81. msg_port_write(HTE, 0x00020020, 0x01b10021);
  82. msg_port_write(HTE, 0x00020021, 0x06000000);
  83. msg_port_write(HTE, 0x00020022, addr >> 6);
  84. msg_port_write(HTE, 0x00020062, 0x00800015);
  85. msg_port_write(HTE, 0x00020063, 0xaaaaaaaa);
  86. msg_port_write(HTE, 0x00020064, 0xcccccccc);
  87. msg_port_write(HTE, 0x00020065, 0xf0f0f0f0);
  88. msg_port_write(HTE, 0x00020061, 0x00030008);
  89. if (mode == WRITE_TRAIN)
  90. pattern = 0xc33c0000;
  91. else /* READ_TRAIN */
  92. pattern = 0xaa5555aa;
  93. for (offset = 0x80; offset <= 0x8f; offset++)
  94. msg_port_write(HTE, offset, pattern);
  95. }
  96. msg_port_write(HTE, 0x000200a1, 0xffff1000);
  97. msg_port_write(HTE, 0x00020011, 0x00011000);
  98. msg_port_write(HTE, 0x00020011, 0x00011100);
  99. hte_wait_for_complete();
  100. /*
  101. * Return bits 15:8 of HTE_CH0_ERR_XSTAT to check for
  102. * any bytelane errors.
  103. */
  104. return (hte_check_errors() >> 8) & 0xff;
  105. }
  106. /**
  107. * Examine a single-cache-line memory with write/read/verify test using multiple
  108. * data patterns (victim-aggressor algorithm).
  109. *
  110. * See hte_write_stress_bit_lanes() which is the external visible wrapper.
  111. *
  112. * @mrc_params: host structure for all MRC global data
  113. * @addr: memory adress being tested (must hit specific channel/rank)
  114. * @loop_cnt: number of test iterations
  115. * @seed_victim: victim data pattern seed
  116. * @seed_aggressor: aggressor data pattern seed
  117. * @victim_bit: should be 0 as auto-rotate feature is in use
  118. * @first_run: if set then the HTE registers are configured, otherwise it is
  119. * assumed configuration is done and we just re-run the test
  120. *
  121. * @return: byte lane failure on each bit (for Quark only bit0 and bit1)
  122. */
  123. static u16 hte_rw_data_cmp(struct mrc_params *mrc_params, u32 addr,
  124. u8 loop_cnt, u32 seed_victim, u32 seed_aggressor,
  125. u8 victim_bit, u8 first_run)
  126. {
  127. u32 offset;
  128. u32 tmp;
  129. if (first_run) {
  130. msg_port_write(HTE, 0x00020020, 0x00910024);
  131. msg_port_write(HTE, 0x00020023, 0x00810024);
  132. msg_port_write(HTE, 0x00020021, 0x06070000);
  133. msg_port_write(HTE, 0x00020024, 0x06070000);
  134. msg_port_write(HTE, 0x00020022, addr >> 6);
  135. msg_port_write(HTE, 0x00020025, addr >> 6);
  136. msg_port_write(HTE, 0x00020062, 0x0000002a);
  137. msg_port_write(HTE, 0x00020063, seed_victim);
  138. msg_port_write(HTE, 0x00020064, seed_aggressor);
  139. msg_port_write(HTE, 0x00020065, seed_victim);
  140. /*
  141. * Write the pattern buffers to select the victim bit
  142. *
  143. * Start with bit0
  144. */
  145. for (offset = 0x80; offset <= 0x8f; offset++) {
  146. if ((offset % 8) == victim_bit)
  147. msg_port_write(HTE, offset, 0x55555555);
  148. else
  149. msg_port_write(HTE, offset, 0xcccccccc);
  150. }
  151. msg_port_write(HTE, 0x00020061, 0x00000000);
  152. msg_port_write(HTE, 0x00020066, 0x03440000);
  153. msg_port_write(HTE, 0x000200a1, 0xffff1000);
  154. }
  155. tmp = 0x10001000 | (loop_cnt << 16);
  156. msg_port_write(HTE, 0x00020011, tmp);
  157. msg_port_write(HTE, 0x00020011, tmp | (1 << 8));
  158. hte_wait_for_complete();
  159. /*
  160. * Return bits 15:8 of HTE_CH0_ERR_XSTAT to check for
  161. * any bytelane errors.
  162. */
  163. return (hte_check_errors() >> 8) & 0xff;
  164. }
  165. /**
  166. * Use HW HTE engine to initialize or test all memory attached to a given DUNIT.
  167. * If flag is MRC_MEM_INIT, this routine writes 0s to all memory locations to
  168. * initialize ECC. If flag is MRC_MEM_TEST, this routine will send an 5AA55AA5
  169. * pattern to all memory locations on the RankMask and then read it back.
  170. * Then it sends an A55AA55A pattern to all memory locations on the RankMask
  171. * and reads it back.
  172. *
  173. * @mrc_params: host structure for all MRC global data
  174. * @flag: MRC_MEM_INIT or MRC_MEM_TEST
  175. *
  176. * @return: errors register showing HTE failures. Also prints out which rank
  177. * failed the HTE test if failure occurs. For rank detection to work,
  178. * the address map must be left in its default state. If MRC changes
  179. * the address map, this function must be modified to change it back
  180. * to default at the beginning, then restore it at the end.
  181. */
  182. u32 hte_mem_init(struct mrc_params *mrc_params, u8 flag)
  183. {
  184. u32 offset;
  185. int test_num;
  186. int i;
  187. /*
  188. * Clear out the error registers at the start of each memory
  189. * init or memory test run.
  190. */
  191. hte_clear_error_regs();
  192. msg_port_write(HTE, 0x00020062, 0x00000015);
  193. for (offset = 0x80; offset <= 0x8f; offset++)
  194. msg_port_write(HTE, offset, ((offset & 1) ? 0xa55a : 0x5aa5));
  195. msg_port_write(HTE, 0x00020021, 0x00000000);
  196. msg_port_write(HTE, 0x00020022, (mrc_params->mem_size >> 6) - 1);
  197. msg_port_write(HTE, 0x00020063, 0xaaaaaaaa);
  198. msg_port_write(HTE, 0x00020064, 0xcccccccc);
  199. msg_port_write(HTE, 0x00020065, 0xf0f0f0f0);
  200. msg_port_write(HTE, 0x00020066, 0x03000000);
  201. switch (flag) {
  202. case MRC_MEM_INIT:
  203. /*
  204. * Only 1 write pass through memory is needed
  205. * to initialize ECC
  206. */
  207. test_num = 1;
  208. break;
  209. case MRC_MEM_TEST:
  210. /* Write/read then write/read with inverted pattern */
  211. test_num = 4;
  212. break;
  213. default:
  214. DPF(D_INFO, "Unknown parameter for flag: %d\n", flag);
  215. return 0xffffffff;
  216. }
  217. DPF(D_INFO, "hte_mem_init");
  218. for (i = 0; i < test_num; i++) {
  219. DPF(D_INFO, ".");
  220. if (i == 0) {
  221. msg_port_write(HTE, 0x00020061, 0x00000000);
  222. msg_port_write(HTE, 0x00020020, 0x00110010);
  223. } else if (i == 1) {
  224. msg_port_write(HTE, 0x00020061, 0x00000000);
  225. msg_port_write(HTE, 0x00020020, 0x00010010);
  226. } else if (i == 2) {
  227. msg_port_write(HTE, 0x00020061, 0x00010100);
  228. msg_port_write(HTE, 0x00020020, 0x00110010);
  229. } else {
  230. msg_port_write(HTE, 0x00020061, 0x00010100);
  231. msg_port_write(HTE, 0x00020020, 0x00010010);
  232. }
  233. msg_port_write(HTE, 0x00020011, 0x00111000);
  234. msg_port_write(HTE, 0x00020011, 0x00111100);
  235. hte_wait_for_complete();
  236. /* If this is a READ pass, check for errors at the end */
  237. if ((i % 2) == 1) {
  238. /* Return immediately if error */
  239. if (hte_check_errors())
  240. break;
  241. }
  242. }
  243. DPF(D_INFO, "done\n");
  244. return hte_check_errors();
  245. }
  246. /**
  247. * Execute a basic single-cache-line memory write/read/verify test using simple
  248. * constant pattern, different for READ_TRAIN and WRITE_TRAIN modes.
  249. *
  250. * @mrc_params: host structure for all MRC global data
  251. * @addr: memory adress being tested (must hit specific channel/rank)
  252. * @first_run: if set then the HTE registers are configured, otherwise it is
  253. * assumed configuration is done and we just re-run the test
  254. * @mode: READ_TRAIN or WRITE_TRAIN (the difference is in the pattern)
  255. *
  256. * @return: byte lane failure on each bit (for Quark only bit0 and bit1)
  257. */
  258. u16 hte_basic_write_read(struct mrc_params *mrc_params, u32 addr,
  259. u8 first_run, u8 mode)
  260. {
  261. u16 errors;
  262. ENTERFN();
  263. /* Enable all error reporting in preparation for HTE test */
  264. hte_enable_all_errors();
  265. hte_clear_error_regs();
  266. errors = hte_basic_data_cmp(mrc_params, addr, first_run, mode);
  267. LEAVEFN();
  268. return errors;
  269. }
  270. /**
  271. * Examine a single-cache-line memory with write/read/verify test using multiple
  272. * data patterns (victim-aggressor algorithm).
  273. *
  274. * @mrc_params: host structure for all MRC global data
  275. * @addr: memory adress being tested (must hit specific channel/rank)
  276. * @first_run: if set then the HTE registers are configured, otherwise it is
  277. * assumed configuration is done and we just re-run the test
  278. *
  279. * @return: byte lane failure on each bit (for Quark only bit0 and bit1)
  280. */
  281. u16 hte_write_stress_bit_lanes(struct mrc_params *mrc_params,
  282. u32 addr, u8 first_run)
  283. {
  284. u16 errors;
  285. u8 victim_bit = 0;
  286. ENTERFN();
  287. /* Enable all error reporting in preparation for HTE test */
  288. hte_enable_all_errors();
  289. hte_clear_error_regs();
  290. /*
  291. * Loop through each bit in the bytelane.
  292. *
  293. * Each pass creates a victim bit while keeping all other bits the same
  294. * as aggressors. AVN HTE adds an auto-rotate feature which allows us
  295. * to program the entire victim/aggressor sequence in 1 step.
  296. *
  297. * The victim bit rotates on each pass so no need to have software
  298. * implement a victim bit loop like on VLV.
  299. */
  300. errors = hte_rw_data_cmp(mrc_params, addr, HTE_LOOP_CNT,
  301. HTE_LFSR_VICTIM_SEED, HTE_LFSR_AGRESSOR_SEED,
  302. victim_bit, first_run);
  303. LEAVEFN();
  304. return errors;
  305. }
  306. /**
  307. * Execute a basic single-cache-line memory write or read.
  308. * This is just for receive enable / fine write-levelling purpose.
  309. *
  310. * @addr: memory adress being tested (must hit specific channel/rank)
  311. * @first_run: if set then the HTE registers are configured, otherwise it is
  312. * assumed configuration is done and we just re-run the test
  313. * @is_write: when non-zero memory write operation executed, otherwise read
  314. */
  315. void hte_mem_op(u32 addr, u8 first_run, u8 is_write)
  316. {
  317. u32 offset;
  318. u32 tmp;
  319. hte_enable_all_errors();
  320. hte_clear_error_regs();
  321. if (first_run) {
  322. tmp = is_write ? 0x01110021 : 0x01010021;
  323. msg_port_write(HTE, 0x00020020, tmp);
  324. msg_port_write(HTE, 0x00020021, 0x06000000);
  325. msg_port_write(HTE, 0x00020022, addr >> 6);
  326. msg_port_write(HTE, 0x00020062, 0x00800015);
  327. msg_port_write(HTE, 0x00020063, 0xaaaaaaaa);
  328. msg_port_write(HTE, 0x00020064, 0xcccccccc);
  329. msg_port_write(HTE, 0x00020065, 0xf0f0f0f0);
  330. msg_port_write(HTE, 0x00020061, 0x00030008);
  331. for (offset = 0x80; offset <= 0x8f; offset++)
  332. msg_port_write(HTE, offset, 0xc33c0000);
  333. }
  334. msg_port_write(HTE, 0x000200a1, 0xffff1000);
  335. msg_port_write(HTE, 0x00020011, 0x00011000);
  336. msg_port_write(HTE, 0x00020011, 0x00011100);
  337. hte_wait_for_complete();
  338. }