summaryrefslogtreecommitdiffstats
path: root/arch/x86/cpu/quark/hte.c
blob: db601e4efb2d0b2b1168ef474a348d95624703ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
 * Copyright (C) 2013, Intel Corporation
 * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com>
 *
 * Ported from Intel released Quark UEFI BIOS
 * QuarkSocPkg/QuarkNorthCluster/MemoryInit/Pei
 *
 * SPDX-License-Identifier:	Intel
 */

#include <common.h>
#include <asm/arch/mrc.h>
#include <asm/arch/msg_port.h>
#include "mrc_util.h"
#include "hte.h"

/**
 * Enable HTE to detect all possible errors for the given training parameters
 * (per-bit or full byte lane).
 */
static void hte_enable_all_errors(void)
{
	msg_port_write(HTE, 0x000200a2, 0xffffffff);
	msg_port_write(HTE, 0x000200a3, 0x000000ff);
	msg_port_write(HTE, 0x000200a4, 0x00000000);
}

/**
 * Go and read the HTE register in order to find any error
 *
 * @return: The errors detected in the HTE status register
 */
static u32 hte_check_errors(void)
{
	return msg_port_read(HTE, 0x000200a7);
}

/**
 * Wait until HTE finishes
 */
static void hte_wait_for_complete(void)
{
	u32 tmp;

	ENTERFN();

	do {} while ((msg_port_read(HTE, 0x00020012) & (1 << 30)) != 0);

	tmp = msg_port_read(HTE, 0x00020011);
	tmp |= (1 << 9);
	tmp &= ~((1 << 12) | (1 << 13));
	msg_port_write(HTE, 0x00020011, tmp);

	LEAVEFN();
}

/**
 * Clear registers related with errors in the HTE
 */
static void hte_clear_error_regs(void)
{
	u32 tmp;

	/*
	 * Clear all HTE errors and enable error checking
	 * for burst and chunk.
	 */
	tmp = msg_port_read(HTE, 0x000200a1);
	tmp |= (1 << 8);
	msg_port_write(HTE, 0x000200a1, tmp);
}

/**
 * Execute a basic single-cache-line memory write/read/verify test using simple
 * constant pattern, different for READ_TRAIN and WRITE_TRAIN modes.
 *
 * See hte_basic_write_read() which is the external visible wrapper.
 *
 * @mrc_params: host structure for all MRC global data
 * @addr: memory adress being tested (must hit specific channel/rank)
 * @first_run: if set then the HTE registers are configured, otherwise it is
 *             assumed configuration is done and we just re-run the test
 * @mode: READ_TRAIN or WRITE_TRAIN (the difference is in the pattern)
 *
 * @return: byte lane failure on each bit (for Quark only bit0 and bit1)
 */
static u16 hte_basic_data_cmp(struct mrc_params *mrc_params, u32 addr,
			      u8 first_run, u8 mode)
{
	u32 pattern;
	u32 offset;

	if (first_run) {
		msg_port_write(HTE, 0x00020020, 0x01b10021);
		msg_port_write(HTE, 0x00020021, 0x06000000);
		msg_port_write(HTE, 0x00020022, addr >> 6);
		msg_port_write(HTE, 0x00020062, 0x00800015);
		msg_port_write(HTE, 0x00020063, 0xaaaaaaaa);
		msg_port_write(HTE, 0x00020064, 0xcccccccc);
		msg_port_write(HTE, 0x00020065, 0xf0f0f0f0);
		msg_port_write(HTE, 0x00020061, 0x00030008);

		if (mode == WRITE_TRAIN)
			pattern = 0xc33c0000;
		else /* READ_TRAIN */
			pattern = 0xaa5555aa;

		for (offset = 0x80; offset <= 0x8f; offset++)
			msg_port_write(HTE, offset, pattern);
	}

	msg_port_write(HTE, 0x000200a1, 0xffff1000);
	msg_port_write(HTE, 0x00020011, 0x00011000);
	msg_port_write(HTE, 0x00020011, 0x00011100);

	hte_wait_for_complete();

	/*
	 * Return bits 15:8 of HTE_CH0_ERR_XSTAT to check for
	 * any bytelane errors.
	 */
	return (hte_check_errors() >> 8) & 0xff;
}

/**
 * Examine a single-cache-line memory with write/read/verify test using multiple
 * data patterns (victim-aggressor algorithm).
 *
 * See hte_write_stress_bit_lanes() which is the external visible wrapper.
 *
 * @mrc_params: host structure for all MRC global data
 * @addr: memory adress being tested (must hit specific channel/rank)
 * @loop_cnt: number of test iterations
 * @seed_victim: victim data pattern seed
 * @seed_aggressor: aggressor data pattern seed
 * @victim_bit: should be 0 as auto-rotate feature is in use
 * @first_run: if set then the HTE registers are configured, otherwise it is
 *             assumed configuration is done and we just re-run the test
 *
 * @return: byte lane failure on each bit (for Quark only bit0 and bit1)
 */
static u16 hte_rw_data_cmp(struct mrc_params *mrc_params, u32 addr,
			   u8 loop_cnt, u32 seed_victim, u32 seed_aggressor,
			   u8 victim_bit, u8 first_run)
{
	u32 offset;
	u32 tmp;

	if (first_run) {
		msg_port_write(HTE, 0x00020020, 0x00910024);
		msg_port_write(HTE, 0x00020023, 0x00810024);
		msg_port_write(HTE, 0x00020021, 0x06070000);
		msg_port_write(HTE, 0x00020024, 0x06070000);
		msg_port_write(HTE, 0x00020022, addr >> 6);
		msg_port_write(HTE, 0x00020025, addr >> 6);
		msg_port_write(HTE, 0x00020062, 0x0000002a);
		msg_port_write(HTE, 0x00020063, seed_victim);
		msg_port_write(HTE, 0x00020064, seed_aggressor);
		msg_port_write(HTE, 0x00020065, seed_victim);

		/*
		 * Write the pattern buffers to select the victim bit
		 *
		 * Start with bit0
		 */
		for (offset = 0x80; offset <= 0x8f; offset++) {
			if ((offset % 8) == victim_bit)
				msg_port_write(HTE, offset, 0x55555555);
			else
				msg_port_write(HTE, offset, 0xcccccccc);
		}

		msg_port_write(HTE, 0x00020061, 0x00000000);
		msg_port_write(HTE, 0x00020066, 0x03440000);
		msg_port_write(HTE, 0x000200a1, 0xffff1000);
	}

	tmp = 0x10001000 | (loop_cnt << 16);
	msg_port_write(HTE, 0x00020011, tmp);
	msg_port_write(HTE, 0x00020011, tmp | (1 << 8));

	hte_wait_for_complete();

	/*
	 * Return bits 15:8 of HTE_CH0_ERR_XSTAT to check for
	 * any bytelane errors.
	 */
	return (hte_check_errors() >> 8) & 0xff;
}

/**
 * Use HW HTE engine to initialize or test all memory attached to a given DUNIT.
 * If flag is MRC_MEM_INIT, this routine writes 0s to all memory locations to
 * initialize ECC. If flag is MRC_MEM_TEST, this routine will send an 5AA55AA5
 * pattern to all memory locations on the RankMask and then read it back.
 * Then it sends an A55AA55A pattern to all memory locations on the RankMask
 * and reads it back.
 *
 * @mrc_params: host structure for all MRC global data
 * @flag: MRC_MEM_INIT or MRC_MEM_TEST
 *
 * @return: errors register showing HTE failures. Also prints out which rank
 *          failed the HTE test if failure occurs. For rank detection to work,
 *          the address map must be left in its default state. If MRC changes
 *          the address map, this function must be modified to change it back
 *          to default at the beginning, then restore it at the end.
 */
u32 hte_mem_init(struct mrc_params *mrc_params, u8 flag)
{
	u32 offset;
	int test_num;
	int i;

	/*
	 * Clear out the error registers at the start of each memory
	 * init or memory test run.
	 */
	hte_clear_error_regs();

	msg_port_write(HTE, 0x00020062, 0x00000015);

	for (offset = 0x80; offset <= 0x8f; offset++)
		msg_port_write(HTE, offset, ((offset & 1) ? 0xa55a : 0x5aa5));

	msg_port_write(HTE, 0x00020021, 0x00000000);
	msg_port_write(HTE, 0x00020022, (mrc_params->mem_size >> 6) - 1);
	msg_port_write(HTE, 0x00020063, 0xaaaaaaaa);
	msg_port_write(HTE, 0x00020064, 0xcccccccc);
	msg_port_write(HTE, 0x00020065, 0xf0f0f0f0);
	msg_port_write(HTE, 0x00020066, 0x03000000);

	switch (flag) {
	case MRC_MEM_INIT:
		/*
		 * Only 1 write pass through memory is needed
		 * to initialize ECC
		 */
		test_num = 1;
		break;
	case MRC_MEM_TEST:
		/* Write/read then write/read with inverted pattern */
		test_num = 4;
		break;
	default:
		DPF(D_INFO, "Unknown parameter for flag: %d\n", flag);
		return 0xffffffff;
	}

	DPF(D_INFO, "hte_mem_init");

	for (i = 0; i < test_num; i++) {
		DPF(D_INFO, ".");

		if (i == 0) {
			msg_port_write(HTE, 0x00020061, 0x00000000);
			msg_port_write(HTE, 0x00020020, 0x00110010);
		} else if (i == 1) {
			msg_port_write(HTE, 0x00020061, 0x00000000);
			msg_port_write(HTE, 0x00020020, 0x00010010);
		} else if (i == 2) {
			msg_port_write(HTE, 0x00020061, 0x00010100);
			msg_port_write(HTE, 0x00020020, 0x00110010);
		} else {
			msg_port_write(HTE, 0x00020061, 0x00010100);
			msg_port_write(HTE, 0x00020020, 0x00010010);
		}

		msg_port_write(HTE, 0x00020011, 0x00111000);
		msg_port_write(HTE, 0x00020011, 0x00111100);

		hte_wait_for_complete();

		/* If this is a READ pass, check for errors at the end */
		if ((i % 2) == 1) {
			/* Return immediately if error */
			if (hte_check_errors())
				break;
		}
	}

	DPF(D_INFO, "done\n");

	return hte_check_errors();
}

/**
 * Execute a basic single-cache-line memory write/read/verify test using simple
 * constant pattern, different for READ_TRAIN and WRITE_TRAIN modes.
 *
 * @mrc_params: host structure for all MRC global data
 * @addr: memory adress being tested (must hit specific channel/rank)
 * @first_run: if set then the HTE registers are configured, otherwise it is
 *             assumed configuration is done and we just re-run the test
 * @mode: READ_TRAIN or WRITE_TRAIN (the difference is in the pattern)
 *
 * @return: byte lane failure on each bit (for Quark only bit0 and bit1)
 */
u16 hte_basic_write_read(struct mrc_params *mrc_params, u32 addr,
			 u8 first_run, u8 mode)
{
	u16 errors;

	ENTERFN();

	/* Enable all error reporting in preparation for HTE test */
	hte_enable_all_errors();
	hte_clear_error_regs();

	errors = hte_basic_data_cmp(mrc_params, addr, first_run, mode);

	LEAVEFN();

	return errors;
}

/**
 * Examine a single-cache-line memory with write/read/verify test using multiple
 * data patterns (victim-aggressor algorithm).
 *
 * @mrc_params: host structure for all MRC global data
 * @addr: memory adress being tested (must hit specific channel/rank)
 * @first_run: if set then the HTE registers are configured, otherwise it is
 *             assumed configuration is done and we just re-run the test
 *
 * @return: byte lane failure on each bit (for Quark only bit0 and bit1)
 */
u16 hte_write_stress_bit_lanes(struct mrc_params *mrc_params,
			       u32 addr, u8 first_run)
{
	u16 errors;
	u8 victim_bit = 0;

	ENTERFN();

	/* Enable all error reporting in preparation for HTE test */
	hte_enable_all_errors();
	hte_clear_error_regs();

	/*
	 * Loop through each bit in the bytelane.
	 *
	 * Each pass creates a victim bit while keeping all other bits the same
	 * as aggressors. AVN HTE adds an auto-rotate feature which allows us
	 * to program the entire victim/aggressor sequence in 1 step.
	 *
	 * The victim bit rotates on each pass so no need to have software
	 * implement a victim bit loop like on VLV.
	 */
	errors = hte_rw_data_cmp(mrc_params, addr, HTE_LOOP_CNT,
				 HTE_LFSR_VICTIM_SEED, HTE_LFSR_AGRESSOR_SEED,
				 victim_bit, first_run);

	LEAVEFN();

	return errors;
}

/**
 * Execute a basic single-cache-line memory write or read.
 * This is just for receive enable / fine write-levelling purpose.
 *
 * @addr: memory adress being tested (must hit specific channel/rank)
 * @first_run: if set then the HTE registers are configured, otherwise it is
 *             assumed configuration is done and we just re-run the test
 * @is_write: when non-zero memory write operation executed, otherwise read
 */
void hte_mem_op(u32 addr, u8 first_run, u8 is_write)
{
	u32 offset;
	u32 tmp;

	hte_enable_all_errors();
	hte_clear_error_regs();

	if (first_run) {
		tmp = is_write ? 0x01110021 : 0x01010021;
		msg_port_write(HTE, 0x00020020, tmp);

		msg_port_write(HTE, 0x00020021, 0x06000000);
		msg_port_write(HTE, 0x00020022, addr >> 6);
		msg_port_write(HTE, 0x00020062, 0x00800015);
		msg_port_write(HTE, 0x00020063, 0xaaaaaaaa);
		msg_port_write(HTE, 0x00020064, 0xcccccccc);
		msg_port_write(HTE, 0x00020065, 0xf0f0f0f0);
		msg_port_write(HTE, 0x00020061, 0x00030008);

		for (offset = 0x80; offset <= 0x8f; offset++)
			msg_port_write(HTE, offset, 0xc33c0000);
	}

	msg_port_write(HTE, 0x000200a1, 0xffff1000);
	msg_port_write(HTE, 0x00020011, 0x00011000);
	msg_port_write(HTE, 0x00020011, 0x00011100);

	hte_wait_for_complete();
}
OpenPOWER on IntegriCloud