hte.c 11 KB

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