blob: 3cca6bd4c227b67e47eb46e9c6f9c18831c734b3 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: Intel
Bin Menge159cdf2015-02-05 23:42:23 +08002/*
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
Bin Menge159cdf2015-02-05 23:42:23 +08008 */
9
Bin Menge159cdf2015-02-05 23:42:23 +080010#include <asm/arch/mrc.h>
11#include <asm/arch/msg_port.h>
12#include "mrc_util.h"
13#include "hte.h"
14
15/**
16 * Enable HTE to detect all possible errors for the given training parameters
17 * (per-bit or full byte lane).
18 */
19static void hte_enable_all_errors(void)
20{
Bin Meng15e3f282015-03-10 18:31:20 +080021 msg_port_write(HTE, 0x000200a2, 0xffffffff);
22 msg_port_write(HTE, 0x000200a3, 0x000000ff);
23 msg_port_write(HTE, 0x000200a4, 0x00000000);
Bin Menge159cdf2015-02-05 23:42:23 +080024}
25
26/**
27 * Go and read the HTE register in order to find any error
28 *
29 * @return: The errors detected in the HTE status register
30 */
31static u32 hte_check_errors(void)
32{
Bin Meng15e3f282015-03-10 18:31:20 +080033 return msg_port_read(HTE, 0x000200a7);
Bin Menge159cdf2015-02-05 23:42:23 +080034}
35
36/**
37 * Wait until HTE finishes
38 */
39static void hte_wait_for_complete(void)
40{
41 u32 tmp;
42
43 ENTERFN();
44
Bin Meng15e3f282015-03-10 18:31:20 +080045 do {} while ((msg_port_read(HTE, 0x00020012) & (1 << 30)) != 0);
Bin Menge159cdf2015-02-05 23:42:23 +080046
47 tmp = msg_port_read(HTE, 0x00020011);
Bin Meng15e3f282015-03-10 18:31:20 +080048 tmp |= (1 << 9);
49 tmp &= ~((1 << 12) | (1 << 13));
Bin Menge159cdf2015-02-05 23:42:23 +080050 msg_port_write(HTE, 0x00020011, tmp);
51
52 LEAVEFN();
53}
54
55/**
56 * Clear registers related with errors in the HTE
57 */
58static void hte_clear_error_regs(void)
59{
60 u32 tmp;
61
62 /*
63 * Clear all HTE errors and enable error checking
64 * for burst and chunk.
65 */
Bin Meng15e3f282015-03-10 18:31:20 +080066 tmp = msg_port_read(HTE, 0x000200a1);
67 tmp |= (1 << 8);
68 msg_port_write(HTE, 0x000200a1, tmp);
Bin Menge159cdf2015-02-05 23:42:23 +080069}
70
71/**
72 * Execute a basic single-cache-line memory write/read/verify test using simple
73 * constant pattern, different for READ_TRAIN and WRITE_TRAIN modes.
74 *
75 * See hte_basic_write_read() which is the external visible wrapper.
76 *
77 * @mrc_params: host structure for all MRC global data
78 * @addr: memory adress being tested (must hit specific channel/rank)
79 * @first_run: if set then the HTE registers are configured, otherwise it is
80 * assumed configuration is done and we just re-run the test
81 * @mode: READ_TRAIN or WRITE_TRAIN (the difference is in the pattern)
82 *
83 * @return: byte lane failure on each bit (for Quark only bit0 and bit1)
84 */
85static u16 hte_basic_data_cmp(struct mrc_params *mrc_params, u32 addr,
86 u8 first_run, u8 mode)
87{
88 u32 pattern;
89 u32 offset;
90
91 if (first_run) {
Bin Meng15e3f282015-03-10 18:31:20 +080092 msg_port_write(HTE, 0x00020020, 0x01b10021);
Bin Menge159cdf2015-02-05 23:42:23 +080093 msg_port_write(HTE, 0x00020021, 0x06000000);
94 msg_port_write(HTE, 0x00020022, addr >> 6);
95 msg_port_write(HTE, 0x00020062, 0x00800015);
Bin Meng15e3f282015-03-10 18:31:20 +080096 msg_port_write(HTE, 0x00020063, 0xaaaaaaaa);
97 msg_port_write(HTE, 0x00020064, 0xcccccccc);
98 msg_port_write(HTE, 0x00020065, 0xf0f0f0f0);
Bin Menge159cdf2015-02-05 23:42:23 +080099 msg_port_write(HTE, 0x00020061, 0x00030008);
100
101 if (mode == WRITE_TRAIN)
Bin Meng15e3f282015-03-10 18:31:20 +0800102 pattern = 0xc33c0000;
Bin Menge159cdf2015-02-05 23:42:23 +0800103 else /* READ_TRAIN */
Bin Meng15e3f282015-03-10 18:31:20 +0800104 pattern = 0xaa5555aa;
Bin Menge159cdf2015-02-05 23:42:23 +0800105
Bin Meng15e3f282015-03-10 18:31:20 +0800106 for (offset = 0x80; offset <= 0x8f; offset++)
Bin Menge159cdf2015-02-05 23:42:23 +0800107 msg_port_write(HTE, offset, pattern);
108 }
109
Bin Meng15e3f282015-03-10 18:31:20 +0800110 msg_port_write(HTE, 0x000200a1, 0xffff1000);
Bin Menge159cdf2015-02-05 23:42:23 +0800111 msg_port_write(HTE, 0x00020011, 0x00011000);
112 msg_port_write(HTE, 0x00020011, 0x00011100);
113
114 hte_wait_for_complete();
115
116 /*
117 * Return bits 15:8 of HTE_CH0_ERR_XSTAT to check for
118 * any bytelane errors.
119 */
Bin Meng15e3f282015-03-10 18:31:20 +0800120 return (hte_check_errors() >> 8) & 0xff;
Bin Menge159cdf2015-02-05 23:42:23 +0800121}
122
123/**
124 * Examine a single-cache-line memory with write/read/verify test using multiple
125 * data patterns (victim-aggressor algorithm).
126 *
127 * See hte_write_stress_bit_lanes() which is the external visible wrapper.
128 *
129 * @mrc_params: host structure for all MRC global data
130 * @addr: memory adress being tested (must hit specific channel/rank)
131 * @loop_cnt: number of test iterations
132 * @seed_victim: victim data pattern seed
133 * @seed_aggressor: aggressor data pattern seed
134 * @victim_bit: should be 0 as auto-rotate feature is in use
135 * @first_run: if set then the HTE registers are configured, otherwise it is
136 * assumed configuration is done and we just re-run the test
137 *
138 * @return: byte lane failure on each bit (for Quark only bit0 and bit1)
139 */
140static u16 hte_rw_data_cmp(struct mrc_params *mrc_params, u32 addr,
141 u8 loop_cnt, u32 seed_victim, u32 seed_aggressor,
142 u8 victim_bit, u8 first_run)
143{
144 u32 offset;
145 u32 tmp;
146
147 if (first_run) {
148 msg_port_write(HTE, 0x00020020, 0x00910024);
149 msg_port_write(HTE, 0x00020023, 0x00810024);
150 msg_port_write(HTE, 0x00020021, 0x06070000);
151 msg_port_write(HTE, 0x00020024, 0x06070000);
152 msg_port_write(HTE, 0x00020022, addr >> 6);
153 msg_port_write(HTE, 0x00020025, addr >> 6);
Bin Meng15e3f282015-03-10 18:31:20 +0800154 msg_port_write(HTE, 0x00020062, 0x0000002a);
Bin Menge159cdf2015-02-05 23:42:23 +0800155 msg_port_write(HTE, 0x00020063, seed_victim);
156 msg_port_write(HTE, 0x00020064, seed_aggressor);
157 msg_port_write(HTE, 0x00020065, seed_victim);
158
159 /*
160 * Write the pattern buffers to select the victim bit
161 *
162 * Start with bit0
163 */
Bin Meng15e3f282015-03-10 18:31:20 +0800164 for (offset = 0x80; offset <= 0x8f; offset++) {
Bin Menge159cdf2015-02-05 23:42:23 +0800165 if ((offset % 8) == victim_bit)
166 msg_port_write(HTE, offset, 0x55555555);
167 else
Bin Meng15e3f282015-03-10 18:31:20 +0800168 msg_port_write(HTE, offset, 0xcccccccc);
Bin Menge159cdf2015-02-05 23:42:23 +0800169 }
170
171 msg_port_write(HTE, 0x00020061, 0x00000000);
172 msg_port_write(HTE, 0x00020066, 0x03440000);
Bin Meng15e3f282015-03-10 18:31:20 +0800173 msg_port_write(HTE, 0x000200a1, 0xffff1000);
Bin Menge159cdf2015-02-05 23:42:23 +0800174 }
175
176 tmp = 0x10001000 | (loop_cnt << 16);
177 msg_port_write(HTE, 0x00020011, tmp);
Bin Meng15e3f282015-03-10 18:31:20 +0800178 msg_port_write(HTE, 0x00020011, tmp | (1 << 8));
Bin Menge159cdf2015-02-05 23:42:23 +0800179
180 hte_wait_for_complete();
181
182 /*
183 * Return bits 15:8 of HTE_CH0_ERR_XSTAT to check for
184 * any bytelane errors.
185 */
Bin Meng15e3f282015-03-10 18:31:20 +0800186 return (hte_check_errors() >> 8) & 0xff;
Bin Menge159cdf2015-02-05 23:42:23 +0800187}
188
189/**
190 * Use HW HTE engine to initialize or test all memory attached to a given DUNIT.
191 * If flag is MRC_MEM_INIT, this routine writes 0s to all memory locations to
192 * initialize ECC. If flag is MRC_MEM_TEST, this routine will send an 5AA55AA5
193 * pattern to all memory locations on the RankMask and then read it back.
194 * Then it sends an A55AA55A pattern to all memory locations on the RankMask
195 * and reads it back.
196 *
197 * @mrc_params: host structure for all MRC global data
198 * @flag: MRC_MEM_INIT or MRC_MEM_TEST
199 *
200 * @return: errors register showing HTE failures. Also prints out which rank
201 * failed the HTE test if failure occurs. For rank detection to work,
202 * the address map must be left in its default state. If MRC changes
203 * the address map, this function must be modified to change it back
204 * to default at the beginning, then restore it at the end.
205 */
206u32 hte_mem_init(struct mrc_params *mrc_params, u8 flag)
207{
208 u32 offset;
209 int test_num;
210 int i;
211
212 /*
213 * Clear out the error registers at the start of each memory
214 * init or memory test run.
215 */
216 hte_clear_error_regs();
217
218 msg_port_write(HTE, 0x00020062, 0x00000015);
219
Bin Meng15e3f282015-03-10 18:31:20 +0800220 for (offset = 0x80; offset <= 0x8f; offset++)
221 msg_port_write(HTE, offset, ((offset & 1) ? 0xa55a : 0x5aa5));
Bin Menge159cdf2015-02-05 23:42:23 +0800222
223 msg_port_write(HTE, 0x00020021, 0x00000000);
224 msg_port_write(HTE, 0x00020022, (mrc_params->mem_size >> 6) - 1);
Bin Meng15e3f282015-03-10 18:31:20 +0800225 msg_port_write(HTE, 0x00020063, 0xaaaaaaaa);
226 msg_port_write(HTE, 0x00020064, 0xcccccccc);
227 msg_port_write(HTE, 0x00020065, 0xf0f0f0f0);
Bin Menge159cdf2015-02-05 23:42:23 +0800228 msg_port_write(HTE, 0x00020066, 0x03000000);
229
230 switch (flag) {
231 case MRC_MEM_INIT:
232 /*
233 * Only 1 write pass through memory is needed
234 * to initialize ECC
235 */
236 test_num = 1;
237 break;
238 case MRC_MEM_TEST:
239 /* Write/read then write/read with inverted pattern */
240 test_num = 4;
241 break;
242 default:
243 DPF(D_INFO, "Unknown parameter for flag: %d\n", flag);
Bin Meng15e3f282015-03-10 18:31:20 +0800244 return 0xffffffff;
Bin Menge159cdf2015-02-05 23:42:23 +0800245 }
246
247 DPF(D_INFO, "hte_mem_init");
248
249 for (i = 0; i < test_num; i++) {
250 DPF(D_INFO, ".");
251
252 if (i == 0) {
253 msg_port_write(HTE, 0x00020061, 0x00000000);
254 msg_port_write(HTE, 0x00020020, 0x00110010);
255 } else if (i == 1) {
256 msg_port_write(HTE, 0x00020061, 0x00000000);
257 msg_port_write(HTE, 0x00020020, 0x00010010);
258 } else if (i == 2) {
259 msg_port_write(HTE, 0x00020061, 0x00010100);
260 msg_port_write(HTE, 0x00020020, 0x00110010);
261 } else {
262 msg_port_write(HTE, 0x00020061, 0x00010100);
263 msg_port_write(HTE, 0x00020020, 0x00010010);
264 }
265
266 msg_port_write(HTE, 0x00020011, 0x00111000);
267 msg_port_write(HTE, 0x00020011, 0x00111100);
268
269 hte_wait_for_complete();
270
271 /* If this is a READ pass, check for errors at the end */
272 if ((i % 2) == 1) {
273 /* Return immediately if error */
274 if (hte_check_errors())
275 break;
276 }
277 }
278
279 DPF(D_INFO, "done\n");
280
281 return hte_check_errors();
282}
283
284/**
285 * Execute a basic single-cache-line memory write/read/verify test using simple
286 * constant pattern, different for READ_TRAIN and WRITE_TRAIN modes.
287 *
288 * @mrc_params: host structure for all MRC global data
289 * @addr: memory adress being tested (must hit specific channel/rank)
290 * @first_run: if set then the HTE registers are configured, otherwise it is
291 * assumed configuration is done and we just re-run the test
292 * @mode: READ_TRAIN or WRITE_TRAIN (the difference is in the pattern)
293 *
294 * @return: byte lane failure on each bit (for Quark only bit0 and bit1)
295 */
296u16 hte_basic_write_read(struct mrc_params *mrc_params, u32 addr,
297 u8 first_run, u8 mode)
298{
299 u16 errors;
300
301 ENTERFN();
302
303 /* Enable all error reporting in preparation for HTE test */
304 hte_enable_all_errors();
305 hte_clear_error_regs();
306
307 errors = hte_basic_data_cmp(mrc_params, addr, first_run, mode);
308
309 LEAVEFN();
310
311 return errors;
312}
313
314/**
315 * Examine a single-cache-line memory with write/read/verify test using multiple
316 * data patterns (victim-aggressor algorithm).
317 *
318 * @mrc_params: host structure for all MRC global data
319 * @addr: memory adress being tested (must hit specific channel/rank)
320 * @first_run: if set then the HTE registers are configured, otherwise it is
321 * assumed configuration is done and we just re-run the test
322 *
323 * @return: byte lane failure on each bit (for Quark only bit0 and bit1)
324 */
325u16 hte_write_stress_bit_lanes(struct mrc_params *mrc_params,
326 u32 addr, u8 first_run)
327{
328 u16 errors;
329 u8 victim_bit = 0;
330
331 ENTERFN();
332
333 /* Enable all error reporting in preparation for HTE test */
334 hte_enable_all_errors();
335 hte_clear_error_regs();
336
337 /*
338 * Loop through each bit in the bytelane.
339 *
340 * Each pass creates a victim bit while keeping all other bits the same
341 * as aggressors. AVN HTE adds an auto-rotate feature which allows us
342 * to program the entire victim/aggressor sequence in 1 step.
343 *
344 * The victim bit rotates on each pass so no need to have software
345 * implement a victim bit loop like on VLV.
346 */
347 errors = hte_rw_data_cmp(mrc_params, addr, HTE_LOOP_CNT,
348 HTE_LFSR_VICTIM_SEED, HTE_LFSR_AGRESSOR_SEED,
349 victim_bit, first_run);
350
351 LEAVEFN();
352
353 return errors;
354}
355
356/**
357 * Execute a basic single-cache-line memory write or read.
358 * This is just for receive enable / fine write-levelling purpose.
359 *
360 * @addr: memory adress being tested (must hit specific channel/rank)
361 * @first_run: if set then the HTE registers are configured, otherwise it is
362 * assumed configuration is done and we just re-run the test
363 * @is_write: when non-zero memory write operation executed, otherwise read
364 */
365void hte_mem_op(u32 addr, u8 first_run, u8 is_write)
366{
367 u32 offset;
368 u32 tmp;
369
370 hte_enable_all_errors();
371 hte_clear_error_regs();
372
373 if (first_run) {
374 tmp = is_write ? 0x01110021 : 0x01010021;
375 msg_port_write(HTE, 0x00020020, tmp);
376
377 msg_port_write(HTE, 0x00020021, 0x06000000);
378 msg_port_write(HTE, 0x00020022, addr >> 6);
379 msg_port_write(HTE, 0x00020062, 0x00800015);
Bin Meng15e3f282015-03-10 18:31:20 +0800380 msg_port_write(HTE, 0x00020063, 0xaaaaaaaa);
381 msg_port_write(HTE, 0x00020064, 0xcccccccc);
382 msg_port_write(HTE, 0x00020065, 0xf0f0f0f0);
Bin Menge159cdf2015-02-05 23:42:23 +0800383 msg_port_write(HTE, 0x00020061, 0x00030008);
384
Bin Meng15e3f282015-03-10 18:31:20 +0800385 for (offset = 0x80; offset <= 0x8f; offset++)
386 msg_port_write(HTE, offset, 0xc33c0000);
Bin Menge159cdf2015-02-05 23:42:23 +0800387 }
388
Bin Meng15e3f282015-03-10 18:31:20 +0800389 msg_port_write(HTE, 0x000200a1, 0xffff1000);
Bin Menge159cdf2015-02-05 23:42:23 +0800390 msg_port_write(HTE, 0x00020011, 0x00011000);
391 msg_port_write(HTE, 0x00020011, 0x00011100);
392
393 hte_wait_for_complete();
394}