blob: 3013b7b6676ce8868272d04029a82ad4897a042e [file] [log] [blame]
Patrick Delaunay2f91e612019-04-10 14:09:29 +02001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
2/*
3 * Copyright (C) 2019, STMicroelectronics - All Rights Reserved
4 */
5#include <common.h>
6#include <console.h>
7#include <clk.h>
8#include <ram.h>
9#include <reset.h>
10#include <asm/io.h>
Patrick Delaunay131ec952020-03-06 11:14:08 +010011#include <linux/bitops.h>
Patrick Delaunay0cd380b2020-03-06 11:14:06 +010012#include <linux/iopoll.h>
Patrick Delaunay2f91e612019-04-10 14:09:29 +020013
14#include "stm32mp1_ddr_regs.h"
15#include "stm32mp1_ddr.h"
16#include "stm32mp1_tests.h"
17
18#define MAX_DQS_PHASE_IDX _144deg
19#define MAX_DQS_UNIT_IDX 7
20#define MAX_GSL_IDX 5
21#define MAX_GPS_IDX 3
22
23/* Number of bytes used in this SW. ( min 1--> max 4). */
24#define NUM_BYTES 4
25
26enum dqs_phase_enum {
27 _36deg = 0,
28 _54deg = 1,
29 _72deg = 2,
30 _90deg = 3,
31 _108deg = 4,
32 _126deg = 5,
33 _144deg = 6
34};
35
36/* BIST Result struct */
37struct BIST_result {
38 /* Overall test result:
39 * 0 Fail (any bit failed) ,
40 * 1 Success (All bits success)
41 */
42 bool test_result;
43 /* 1: true, all fail / 0: False, not all bits fail */
44 bool all_bits_fail;
45 bool bit_i_test_result[8]; /* 0 fail / 1 success */
46};
47
48/* a struct that defines tuning parameters of a byte. */
49struct tuning_position {
50 u8 phase; /* DQS phase */
51 u8 unit; /* DQS unit delay */
52 u32 bits_delay; /* Bits deskew in this byte */
53};
54
55/* 36deg, 54deg, 72deg, 90deg, 108deg, 126deg, 144deg */
56const u8 dx_dll_phase[7] = {3, 2, 1, 0, 14, 13, 12};
57
58static u8 BIST_error_max = 1;
59static u32 BIST_seed = 0x1234ABCD;
60
61static u8 get_nb_bytes(struct stm32mp1_ddrctl *ctl)
62{
63 u32 data_bus = readl(&ctl->mstr) & DDRCTRL_MSTR_DATA_BUS_WIDTH_MASK;
64 u8 nb_bytes = NUM_BYTES;
65
66 switch (data_bus) {
67 case DDRCTRL_MSTR_DATA_BUS_WIDTH_HALF:
68 nb_bytes /= 2;
69 break;
70 case DDRCTRL_MSTR_DATA_BUS_WIDTH_QUARTER:
71 nb_bytes /= 4;
72 break;
73 default:
74 break;
75 }
76
77 return nb_bytes;
78}
79
Patrick Delaunay131ec952020-03-06 11:14:08 +010080static u8 get_nb_bank(struct stm32mp1_ddrctl *ctl)
81{
82 /* Count bank address bits */
83 u8 bits = 0;
84 u32 reg, val;
85
86 reg = readl(&ctl->addrmap1);
87 /* addrmap1.addrmap_bank_b1 */
88 val = (reg & GENMASK(5, 0)) >> 0;
89 if (val <= 31)
90 bits++;
91 /* addrmap1.addrmap_bank_b2 */
92 val = (reg & GENMASK(13, 8)) >> 8;
93 if (val <= 31)
94 bits++;
95 /* addrmap1.addrmap_bank_b3 */
96 val = (reg & GENMASK(21, 16)) >> 16;
97 if (val <= 31)
98 bits++;
99
100 return bits;
101}
102
103static u8 get_nb_col(struct stm32mp1_ddrctl *ctl)
104{
105 u8 bits;
106 u32 reg, val;
107
108 /* Count column address bits, start at 2 for b0 and b1 (fixed) */
109 bits = 2;
110
111 reg = readl(&ctl->addrmap2);
112 /* addrmap2.addrmap_col_b2 */
113 val = (reg & GENMASK(3, 0)) >> 0;
114 if (val <= 7)
115 bits++;
116 /* addrmap2.addrmap_col_b3 */
117 val = (reg & GENMASK(11, 8)) >> 8;
118 if (val <= 7)
119 bits++;
120 /* addrmap2.addrmap_col_b4 */
121 val = (reg & GENMASK(19, 16)) >> 16;
122 if (val <= 7)
123 bits++;
124 /* addrmap2.addrmap_col_b5 */
125 val = (reg & GENMASK(27, 24)) >> 24;
126 if (val <= 7)
127 bits++;
128
129 reg = readl(&ctl->addrmap3);
130 /* addrmap3.addrmap_col_b6 */
131 val = (reg & GENMASK(3, 0)) >> 0;
132 if (val <= 7)
133 bits++;
134 /* addrmap3.addrmap_col_b7 */
135 val = (reg & GENMASK(11, 8)) >> 8;
136 if (val <= 7)
137 bits++;
138 /* addrmap3.addrmap_col_b8 */
139 val = (reg & GENMASK(19, 16)) >> 16;
140 if (val <= 7)
141 bits++;
142 /* addrmap3.addrmap_col_b9 */
143 val = (reg & GENMASK(27, 24)) >> 24;
144 if (val <= 7)
145 bits++;
146
147 reg = readl(&ctl->addrmap4);
148 /* addrmap4.addrmap_col_b10 */
149 val = (reg & GENMASK(3, 0)) >> 0;
150 if (val <= 7)
151 bits++;
152 /* addrmap4.addrmap_col_b11 */
153 val = (reg & GENMASK(11, 8)) >> 8;
154 if (val <= 7)
155 bits++;
156
157 return bits;
158}
159
160static u8 get_nb_row(struct stm32mp1_ddrctl *ctl)
161{
162 /* Count row address bits */
163 u8 bits = 0;
164 u32 reg, val;
165
166 reg = readl(&ctl->addrmap5);
167 /* addrmap5.addrmap_row_b0 */
168 val = (reg & GENMASK(3, 0)) >> 0;
169 if (val <= 11)
170 bits++;
171 /* addrmap5.addrmap_row_b1 */
172 val = (reg & GENMASK(11, 8)) >> 8;
173 if (val <= 11)
174 bits++;
175 /* addrmap5.addrmap_row_b2_10 */
176 val = (reg & GENMASK(19, 16)) >> 16;
177 if (val <= 11)
178 bits += 9;
179 else
180 printf("warning: addrmap5.addrmap_row_b2_10 not supported\n");
181 /* addrmap5.addrmap_row_b11 */
182 val = (reg & GENMASK(27, 24)) >> 24;
183 if (val <= 11)
184 bits++;
185
186 reg = readl(&ctl->addrmap6);
187 /* addrmap6.addrmap_row_b12 */
188 val = (reg & GENMASK(3, 0)) >> 0;
189 if (val <= 7)
190 bits++;
191 /* addrmap6.addrmap_row_b13 */
192 val = (reg & GENMASK(11, 8)) >> 8;
193 if (val <= 7)
194 bits++;
195 /* addrmap6.addrmap_row_b14 */
196 val = (reg & GENMASK(19, 16)) >> 16;
197 if (val <= 7)
198 bits++;
199 /* addrmap6.addrmap_row_b15 */
200 val = (reg & GENMASK(27, 24)) >> 24;
201 if (val <= 7)
202 bits++;
203
204 return bits;
205}
206
Patrick Delaunay2f91e612019-04-10 14:09:29 +0200207static void itm_soft_reset(struct stm32mp1_ddrphy *phy)
208{
209 stm32mp1_ddrphy_init(phy, DDRPHYC_PIR_ITMSRST);
210}
211
212/* Read DQ unit delay register and provides the retrieved value for DQS
213 * We are assuming that we have the same delay when clocking
214 * by DQS and when clocking by DQSN
215 */
216static u8 DQ_unit_index(struct stm32mp1_ddrphy *phy, u8 byte, u8 bit)
217{
218 u32 index;
219 u32 addr = DXNDQTR(phy, byte);
220
221 /* We are assuming that we have the same delay when clocking by DQS
222 * and when clocking by DQSN : use only the low bits
223 */
224 index = (readl(addr) >> DDRPHYC_DXNDQTR_DQDLY_SHIFT(bit))
225 & DDRPHYC_DXNDQTR_DQDLY_LOW_MASK;
226
227 pr_debug("%s: [%x]: %x => DQ unit index = %x\n",
228 __func__, addr, readl(addr), index);
229
230 return index;
231}
232
233/* Sets the DQS phase delay for a byte lane.
234 *phase delay is specified by giving the index of the desired delay
235 * in the dx_dll_phase array.
236 */
237static void DQS_phase_delay(struct stm32mp1_ddrphy *phy, u8 byte, u8 phase_idx)
238{
239 u8 sdphase_val = 0;
240
241 /* Write DXNDLLCR.SDPHASE = dx_dll_phase(phase_index); */
242 sdphase_val = dx_dll_phase[phase_idx];
243 clrsetbits_le32(DXNDLLCR(phy, byte),
244 DDRPHYC_DXNDLLCR_SDPHASE_MASK,
245 sdphase_val << DDRPHYC_DXNDLLCR_SDPHASE_SHIFT);
246}
247
248/* Sets the DQS unit delay for a byte lane.
249 * unit delay is specified by giving the index of the desired delay
250 * for dgsdly and dqsndly (same value).
251 */
252static void DQS_unit_delay(struct stm32mp1_ddrphy *phy,
253 u8 byte, u8 unit_dly_idx)
254{
255 /* Write the same value in DXNDQSTR.DQSDLY and DXNDQSTR.DQSNDLY */
256 clrsetbits_le32(DXNDQSTR(phy, byte),
257 DDRPHYC_DXNDQSTR_DQSDLY_MASK |
258 DDRPHYC_DXNDQSTR_DQSNDLY_MASK,
259 (unit_dly_idx << DDRPHYC_DXNDQSTR_DQSDLY_SHIFT) |
260 (unit_dly_idx << DDRPHYC_DXNDQSTR_DQSNDLY_SHIFT));
261
262 /* After changing this value, an ITM soft reset (PIR.ITMSRST=1,
263 * plus PIR.INIT=1) must be issued.
264 */
265 stm32mp1_ddrphy_init(phy, DDRPHYC_PIR_ITMSRST);
266}
267
268/* Sets the DQ unit delay for a bit line in particular byte lane.
269 * unit delay is specified by giving the desired delay
270 */
271static void set_DQ_unit_delay(struct stm32mp1_ddrphy *phy,
272 u8 byte, u8 bit,
273 u8 dq_delay_index)
274{
275 u8 dq_bit_delay_val = dq_delay_index | (dq_delay_index << 2);
276
277 /* same value on delay for clock DQ an DQS_b */
278 clrsetbits_le32(DXNDQTR(phy, byte),
279 DDRPHYC_DXNDQTR_DQDLY_MASK
280 << DDRPHYC_DXNDQTR_DQDLY_SHIFT(bit),
281 dq_bit_delay_val << DDRPHYC_DXNDQTR_DQDLY_SHIFT(bit));
282}
283
284static void set_r0dgsl_delay(struct stm32mp1_ddrphy *phy,
285 u8 byte, u8 r0dgsl_idx)
286{
287 clrsetbits_le32(DXNDQSTR(phy, byte),
288 DDRPHYC_DXNDQSTR_R0DGSL_MASK,
289 r0dgsl_idx << DDRPHYC_DXNDQSTR_R0DGSL_SHIFT);
290}
291
292static void set_r0dgps_delay(struct stm32mp1_ddrphy *phy,
293 u8 byte, u8 r0dgps_idx)
294{
295 clrsetbits_le32(DXNDQSTR(phy, byte),
296 DDRPHYC_DXNDQSTR_R0DGPS_MASK,
297 r0dgps_idx << DDRPHYC_DXNDQSTR_R0DGPS_SHIFT);
298}
299
300/* Basic BIST configuration for data lane tests. */
Patrick Delaunay131ec952020-03-06 11:14:08 +0100301static void config_BIST(struct stm32mp1_ddrctl *ctl,
302 struct stm32mp1_ddrphy *phy)
Patrick Delaunay2f91e612019-04-10 14:09:29 +0200303{
Patrick Delaunay131ec952020-03-06 11:14:08 +0100304 u8 nb_bank = get_nb_bank(ctl);
305 u8 nb_row = get_nb_row(ctl);
306 u8 nb_col = get_nb_col(ctl);
307
Patrick Delaunay2f91e612019-04-10 14:09:29 +0200308 /* Selects the SDRAM bank address to be used during BIST. */
309 u32 bbank = 0;
310 /* Selects the SDRAM row address to be used during BIST. */
311 u32 brow = 0;
312 /* Selects the SDRAM column address to be used during BIST. */
313 u32 bcol = 0;
314 /* Selects the value by which the SDRAM address is incremented
315 * for each write/read access.
316 */
317 u32 bainc = 0x00000008;
318 /* Specifies the maximum SDRAM rank to be used during BIST.
319 * The default value is set to maximum ranks minus 1.
320 * must be 0 with single rank
321 */
322 u32 bmrank = 0;
323 /* Selects the SDRAM rank to be used during BIST.
324 * must be 0 with single rank
325 */
326 u32 brank = 0;
Patrick Delaunay131ec952020-03-06 11:14:08 +0100327
Patrick Delaunay2f91e612019-04-10 14:09:29 +0200328 /* Specifies the maximum SDRAM bank address to be used during
329 * BIST before the address & increments to the next rank.
330 */
Patrick Delaunay131ec952020-03-06 11:14:08 +0100331 u32 bmbank = (1 << nb_bank) - 1;
Patrick Delaunay2f91e612019-04-10 14:09:29 +0200332 /* Specifies the maximum SDRAM row address to be used during
333 * BIST before the address & increments to the next bank.
334 */
Patrick Delaunay131ec952020-03-06 11:14:08 +0100335 u32 bmrow = (1 << nb_row) - 1;
Patrick Delaunay2f91e612019-04-10 14:09:29 +0200336 /* Specifies the maximum SDRAM column address to be used during
337 * BIST before the address & increments to the next row.
338 */
Patrick Delaunay131ec952020-03-06 11:14:08 +0100339 u32 bmcol = (1 << nb_col) - 1;
340
Patrick Delaunay2f91e612019-04-10 14:09:29 +0200341 u32 bmode_conf = 0x00000001; /* DRam mode */
342 u32 bdxen_conf = 0x00000001; /* BIST on Data byte */
343 u32 bdpat_conf = 0x00000002; /* Select LFSR pattern */
344
345 /*Setup BIST for DRAM mode, and LFSR-random data pattern.*/
346 /*Write BISTRR.BMODE = 1?b1;*/
347 /*Write BISTRR.BDXEN = 1?b1;*/
348 /*Write BISTRR.BDPAT = 2?b10;*/
349
350 /* reset BIST */
351 writel(0x3, &phy->bistrr);
352
353 writel((bmode_conf << 3) | (bdxen_conf << 14) | (bdpat_conf << 17),
354 &phy->bistrr);
355
356 /*Setup BIST Word Count*/
357 /*Write BISTWCR.BWCNT = 16?b0008;*/
358 writel(0x00000200, &phy->bistwcr); /* A multiple of BL/2 */
359
360 writel(bcol | (brow << 12) | (bbank << 28), &phy->bistar0);
361 writel(brank | (bmrank << 2) | (bainc << 4), &phy->bistar1);
Patrick Delaunay2f91e612019-04-10 14:09:29 +0200362 writel(bmcol | (bmrow << 12) | (bmbank << 28), &phy->bistar2);
363}
364
365/* Select the Byte lane to be tested by BIST. */
366static void BIST_datx8_sel(struct stm32mp1_ddrphy *phy, u8 datx8)
367{
368 clrsetbits_le32(&phy->bistrr,
369 DDRPHYC_BISTRR_BDXSEL_MASK,
370 datx8 << DDRPHYC_BISTRR_BDXSEL_SHIFT);
371
372 /*(For example, selecting Byte Lane 3, BISTRR.BDXSEL = 4?b0011)*/
373 /* Write BISTRR.BDXSEL = datx8; */
374}
375
376/* Perform BIST Write_Read test on a byte lane and return test result. */
377static void BIST_test(struct stm32mp1_ddrphy *phy, u8 byte,
378 struct BIST_result *bist)
379{
380 bool result = true; /* BIST_SUCCESS */
381 u32 cnt = 0;
382 u32 error = 0;
Patrick Delaunay0cd380b2020-03-06 11:14:06 +0100383 u32 val;
384 int ret;
Patrick Delaunay2f91e612019-04-10 14:09:29 +0200385
386 bist->test_result = true;
387
388run:
389 itm_soft_reset(phy);
390
391 /*Perform BIST Reset*/
392 /* Write BISTRR.BINST = 3?b011; */
393 clrsetbits_le32(&phy->bistrr,
394 0x00000007,
395 0x00000003);
396
397 /*Re-seed LFSR*/
398 /* Write BISTLSR.SEED = 32'h1234ABCD; */
399 if (BIST_seed)
400 writel(BIST_seed, &phy->bistlsr);
401 else
402 writel(rand(), &phy->bistlsr);
403
404 /* some delay to reset BIST */
Patrick Delaunay6cdc1202020-03-06 11:14:10 +0100405 udelay(10);
Patrick Delaunay2f91e612019-04-10 14:09:29 +0200406
407 /*Perform BIST Run*/
408 clrsetbits_le32(&phy->bistrr,
409 0x00000007,
410 0x00000001);
411 /* Write BISTRR.BINST = 3?b001; */
412
Patrick Delaunay0cd380b2020-03-06 11:14:06 +0100413 /* poll on BISTGSR.BDONE and wait max 1000 us */
414 ret = readl_poll_timeout(&phy->bistgsr, val,
415 val & DDRPHYC_BISTGSR_BDDONE, 1000);
Patrick Delaunay2f91e612019-04-10 14:09:29 +0200416
Patrick Delaunay0cd380b2020-03-06 11:14:06 +0100417 if (ret < 0) {
418 printf("warning: BIST timeout\n");
Patrick Delaunay2f91e612019-04-10 14:09:29 +0200419 result = false; /* BIST_FAIL; */
Patrick Delaunay0cd380b2020-03-06 11:14:06 +0100420 /*Perform BIST Stop */
421 clrsetbits_le32(&phy->bistrr, 0x00000007, 0x00000002);
422 } else {
423 /*Check if received correct number of words*/
424 /* if (Read BISTWCSR.DXWCNT = Read BISTWCR.BWCNT) */
425 if (((readl(&phy->bistwcsr)) >> DDRPHYC_BISTWCSR_DXWCNT_SHIFT)
426 == readl(&phy->bistwcr)) {
427 /*Determine if there is a data comparison error*/
428 /* if (Read BISTGSR.BDXERR = 1?b0) */
429 if (readl(&phy->bistgsr) & DDRPHYC_BISTGSR_BDXERR)
430 result = false; /* BIST_FAIL; */
431 else
432 result = true; /* BIST_SUCCESS; */
433 } else {
434 result = false; /* BIST_FAIL; */
435 }
Patrick Delaunay2f91e612019-04-10 14:09:29 +0200436 }
437
438 /* loop while success */
439 cnt++;
440 if (result && cnt != 1000)
441 goto run;
442
443 if (!result)
444 error++;
445
446 if (error < BIST_error_max) {
447 if (cnt != 1000)
448 goto run;
449 bist->test_result = true;
450 } else {
451 bist->test_result = false;
452 }
453}
454
455/* After running the deskew algo, this function applies the new DQ delays
456 * by reading them from the array "deskew_delay"and writing in PHY registers.
457 * The bits that are not deskewed parfectly (too much skew on them,
458 * or data eye very wide) are marked in the array deskew_non_converge.
459 */
460static void apply_deskew_results(struct stm32mp1_ddrphy *phy, u8 byte,
461 u8 deskew_delay[NUM_BYTES][8],
462 u8 deskew_non_converge[NUM_BYTES][8])
463{
464 u8 bit_i;
465 u8 index;
466
467 for (bit_i = 0; bit_i < 8; bit_i++) {
468 set_DQ_unit_delay(phy, byte, bit_i, deskew_delay[byte][bit_i]);
469 index = DQ_unit_index(phy, byte, bit_i);
470 pr_debug("Byte %d ; bit %d : The new DQ delay (%d) index=%d [delta=%d, 3 is the default]",
471 byte, bit_i, deskew_delay[byte][bit_i],
472 index, index - 3);
473 printf("Byte %d, bit %d, DQ delay = %d",
474 byte, bit_i, deskew_delay[byte][bit_i]);
475 if (deskew_non_converge[byte][bit_i] == 1)
476 pr_debug(" - not converged : still more skew");
477 printf("\n");
478 }
479}
480
481/* DQ Bit de-skew algorithm.
482 * Deskews data lines as much as possible.
483 * 1. Add delay to DQS line until finding the failure
484 * (normally a hold time violation)
485 * 2. Reduce DQS line by small steps until finding the very first time
486 * we go back to "Pass" condition.
487 * 3. For each DQ line, Reduce DQ delay until finding the very first failure
488 * (normally a hold time fail)
489 * 4. When all bits are at their first failure delay, we can consider them
490 * aligned.
491 * Handle conrer situation (Can't find Pass-fail, or fail-pass transitions
492 * at any step)
493 * TODO Provide a return Status. Improve doc
494 */
495static enum test_result bit_deskew(struct stm32mp1_ddrctl *ctl,
496 struct stm32mp1_ddrphy *phy, char *string)
497{
498 /* New DQ delay value (index), set during Deskew algo */
499 u8 deskew_delay[NUM_BYTES][8];
500 /*If there is still skew on a bit, mark this bit. */
501 u8 deskew_non_converge[NUM_BYTES][8];
502 struct BIST_result result;
503 s8 dqs_unit_delay_index = 0;
504 u8 datx8 = 0;
505 u8 bit_i = 0;
506 s8 phase_idx = 0;
507 s8 bit_i_delay_index = 0;
508 u8 success = 0;
509 struct tuning_position last_right_ok;
510 u8 force_stop = 0;
511 u8 fail_found;
512 u8 error = 0;
513 u8 nb_bytes = get_nb_bytes(ctl);
514 /* u8 last_pass_dqs_unit = 0; */
515
516 memset(deskew_delay, 0, sizeof(deskew_delay));
517 memset(deskew_non_converge, 0, sizeof(deskew_non_converge));
518
519 /*Disable DQS Drift Compensation*/
520 clrbits_le32(&phy->pgcr, DDRPHYC_PGCR_DFTCMP);
521 /*Disable all bytes*/
522 /* Disable automatic power down of DLL and IOs when disabling
523 * a byte (To avoid having to add programming and delay
524 * for a DLL re-lock when later re-enabling a disabled Byte Lane)
525 */
526 clrbits_le32(&phy->pgcr, DDRPHYC_PGCR_PDDISDX);
527
528 /* Disable all data bytes */
529 clrbits_le32(&phy->dx0gcr, DDRPHYC_DXNGCR_DXEN);
530 clrbits_le32(&phy->dx1gcr, DDRPHYC_DXNGCR_DXEN);
531 clrbits_le32(&phy->dx2gcr, DDRPHYC_DXNGCR_DXEN);
532 clrbits_le32(&phy->dx3gcr, DDRPHYC_DXNGCR_DXEN);
533
534 /* Config the BIST block */
Patrick Delaunay131ec952020-03-06 11:14:08 +0100535 config_BIST(ctl, phy);
Patrick Delaunay2f91e612019-04-10 14:09:29 +0200536 pr_debug("BIST Config done.\n");
537
538 /* Train each byte */
539 for (datx8 = 0; datx8 < nb_bytes; datx8++) {
540 if (ctrlc()) {
541 sprintf(string, "interrupted at byte %d/%d, error=%d",
542 datx8 + 1, nb_bytes, error);
543 return TEST_FAILED;
544 }
545 pr_debug("\n======================\n");
546 pr_debug("Start deskew byte %d .\n", datx8);
547 pr_debug("======================\n");
548 /* Enable Byte (DXNGCR, bit DXEN) */
549 setbits_le32(DXNGCR(phy, datx8), DDRPHYC_DXNGCR_DXEN);
550
551 /* Select the byte lane for comparison of read data */
552 BIST_datx8_sel(phy, datx8);
553
554 /* Set all DQDLYn to maximum value. All bits within the byte
555 * will be delayed with DQSTR = 2 instead of max = 3
556 * to avoid inter bits fail influence
557 */
558 writel(0xAAAAAAAA, DXNDQTR(phy, datx8));
559
560 /* Set the DQS phase delay to 90 DEG (default).
561 * What is defined here is the index of the desired config
562 * in the PHASE array.
563 */
564 phase_idx = _90deg;
565
566 /* Set DQS unit delay to the max value. */
567 dqs_unit_delay_index = MAX_DQS_UNIT_IDX;
568 DQS_unit_delay(phy, datx8, dqs_unit_delay_index);
569 DQS_phase_delay(phy, datx8, phase_idx);
570
571 /* Issue a DLL soft reset */
572 clrbits_le32(DXNDLLCR(phy, datx8), DDRPHYC_DXNDLLCR_DLLSRST);
573 setbits_le32(DXNDLLCR(phy, datx8), DDRPHYC_DXNDLLCR_DLLSRST);
574
575 /* Test this typical init condition */
576 BIST_test(phy, datx8, &result);
577 success = result.test_result;
578
579 /* If the test pass in this typical condition,
580 * start the algo with it.
581 * Else, look for Pass init condition
582 */
583 if (!success) {
584 pr_debug("Fail at init condtion. Let's look for a good init condition.\n");
585 success = 0; /* init */
586 /* Make sure we start with a PASS condition before
587 * looking for a fail condition.
588 * Find the first PASS PHASE condition
589 */
590
591 /* escape if we find a PASS */
592 pr_debug("increase Phase idx\n");
593 while (!success && (phase_idx <= MAX_DQS_PHASE_IDX)) {
594 DQS_phase_delay(phy, datx8, phase_idx);
595 BIST_test(phy, datx8, &result);
596 success = result.test_result;
597 phase_idx++;
598 }
599 /* if ended with success
600 * ==>> Restore the fist success condition
601 */
602 if (success)
603 phase_idx--; /* because it ended with ++ */
604 }
605 if (ctrlc()) {
606 sprintf(string, "interrupted at byte %d/%d, error=%d",
607 datx8 + 1, nb_bytes, error);
608 return TEST_FAILED;
609 }
610 /* We couldn't find a successful condition, its seems
611 * we have hold violation, lets try reduce DQS_unit Delay
612 */
613 if (!success) {
614 /* We couldn't find a successful condition, its seems
615 * we have hold violation, lets try reduce DQS_unit
616 * Delay
617 */
618 pr_debug("Still fail. Try decrease DQS Unit delay\n");
619
620 phase_idx = 0;
621 dqs_unit_delay_index = 0;
622 DQS_phase_delay(phy, datx8, phase_idx);
623
624 /* escape if we find a PASS */
625 while (!success &&
626 (dqs_unit_delay_index <=
627 MAX_DQS_UNIT_IDX)) {
628 DQS_unit_delay(phy, datx8,
629 dqs_unit_delay_index);
630 BIST_test(phy, datx8, &result);
631 success = result.test_result;
632 dqs_unit_delay_index++;
633 }
634 if (success) {
635 /* Restore the first success condition*/
636 dqs_unit_delay_index--;
637 /* last_pass_dqs_unit = dqs_unit_delay_index;*/
638 DQS_unit_delay(phy, datx8,
639 dqs_unit_delay_index);
640 } else {
641 /* No need to continue,
642 * there is no pass region.
643 */
644 force_stop = 1;
645 }
646 }
647
648 /* There is an initial PASS condition
649 * Look for the first failing condition by PHASE stepping.
650 * This part of the algo can finish without converging.
651 */
652 if (force_stop) {
653 printf("Result: Failed ");
654 printf("[Cannot Deskew lines, ");
655 printf("there is no PASS region]\n");
656 error++;
657 continue;
658 }
659 if (ctrlc()) {
660 sprintf(string, "interrupted at byte %d/%d, error=%d",
661 datx8 + 1, nb_bytes, error);
662 return TEST_FAILED;
663 }
664
665 pr_debug("there is a pass region for phase idx %d\n",
666 phase_idx);
667 pr_debug("Step1: Find the first failing condition\n");
668 /* Look for the first failing condition by PHASE stepping.
669 * This part of the algo can finish without converging.
670 */
671
672 /* escape if we find a fail (hold time violation)
673 * condition at any bit or if out of delay range.
674 */
675 while (success && (phase_idx <= MAX_DQS_PHASE_IDX)) {
676 DQS_phase_delay(phy, datx8, phase_idx);
677 BIST_test(phy, datx8, &result);
678 success = result.test_result;
679 phase_idx++;
680 }
681 if (ctrlc()) {
682 sprintf(string, "interrupted at byte %d/%d, error=%d",
683 datx8 + 1, nb_bytes, error);
684 return TEST_FAILED;
685 }
686
687 /* if the loop ended with a failing condition at any bit,
688 * lets look for the first previous success condition by unit
689 * stepping (minimal delay)
690 */
691 if (!success) {
692 pr_debug("Fail region (PHASE) found phase idx %d\n",
693 phase_idx);
694 pr_debug("Let's look for first success by DQS Unit steps\n");
695 /* This part, the algo always converge */
696 phase_idx--;
697
698 /* escape if we find a success condition
699 * or if out of delay range.
700 */
701 while (!success && dqs_unit_delay_index >= 0) {
702 DQS_unit_delay(phy, datx8,
703 dqs_unit_delay_index);
704 BIST_test(phy, datx8, &result);
705 success = result.test_result;
706 dqs_unit_delay_index--;
707 }
708 /* if the loop ended with a success condition,
709 * the last delay Right OK (before hold violation)
710 * condition is then defined as following:
711 */
712 if (success) {
713 /* Hold the dely parameters of the the last
714 * delay Right OK condition.
715 * -1 to get back to current condition
716 */
717 last_right_ok.phase = phase_idx;
718 /*+1 to get back to current condition */
719 last_right_ok.unit = dqs_unit_delay_index + 1;
720 last_right_ok.bits_delay = 0xFFFFFFFF;
721 pr_debug("Found %d\n", dqs_unit_delay_index);
722 } else {
723 /* the last OK condition is then with the
724 * previous phase_idx.
725 * -2 instead of -1 because at the last
726 * iteration of the while(),
727 * we incremented phase_idx
728 */
729 last_right_ok.phase = phase_idx - 1;
730 /* Nominal+1. Because we want the previous
731 * delay after reducing the phase delay.
732 */
733 last_right_ok.unit = 1;
734 last_right_ok.bits_delay = 0xFFFFFFFF;
735 pr_debug("Not Found : try previous phase %d\n",
736 phase_idx - 1);
737
738 DQS_phase_delay(phy, datx8, phase_idx - 1);
739 dqs_unit_delay_index = 0;
740 success = true;
741 while (success &&
742 (dqs_unit_delay_index <
743 MAX_DQS_UNIT_IDX)) {
744 DQS_unit_delay(phy, datx8,
745 dqs_unit_delay_index);
746 BIST_test(phy, datx8, &result);
747 success = result.test_result;
748 dqs_unit_delay_index++;
749 pr_debug("dqs_unit_delay_index = %d, result = %d\n",
750 dqs_unit_delay_index, success);
751 }
752
753 if (!success) {
754 last_right_ok.unit =
755 dqs_unit_delay_index - 1;
756 } else {
757 last_right_ok.unit = 0;
758 pr_debug("ERROR: failed region not FOUND");
759 }
760 }
761 } else {
762 /* we can't find a failing condition at all bits
763 * ==> Just hold the last test condition
764 * (the max DQS delay)
765 * which is the most likely,
766 * the closest to a hold violation
767 * If we can't find a Fail condition after
768 * the Pass region, stick at this position
769 * In order to have max chances to find a fail
770 * when reducing DQ delays.
771 */
772 last_right_ok.phase = MAX_DQS_PHASE_IDX;
773 last_right_ok.unit = MAX_DQS_UNIT_IDX;
774 last_right_ok.bits_delay = 0xFFFFFFFF;
775 pr_debug("Can't find the a fail condition\n");
776 }
777
778 /* step 2:
779 * if we arrive at this stage, it means that we found the last
780 * Right OK condition (by tweeking the DQS delay). Or we simply
781 * pushed DQS delay to the max
782 * This means that by reducing the delay on some DQ bits,
783 * we should find a failing condition.
784 */
785 printf("Byte %d, DQS unit = %d, phase = %d\n",
786 datx8, last_right_ok.unit, last_right_ok.phase);
787 pr_debug("Step2, unit = %d, phase = %d, bits delay=%x\n",
788 last_right_ok.unit, last_right_ok.phase,
789 last_right_ok.bits_delay);
790
791 /* Restore the last_right_ok condtion. */
792 DQS_unit_delay(phy, datx8, last_right_ok.unit);
793 DQS_phase_delay(phy, datx8, last_right_ok.phase);
794 writel(last_right_ok.bits_delay, DXNDQTR(phy, datx8));
795
796 /* train each bit
797 * reduce delay on each bit, and perform a write/read test
798 * and stop at the very first time it fails.
799 * the goal is the find the first failing condition
800 * for each bit.
801 * When we achieve this condition< for all the bits,
802 * we are sure they are aligned (+/- step resolution)
803 */
804 fail_found = 0;
805 for (bit_i = 0; bit_i < 8; bit_i++) {
806 if (ctrlc()) {
807 sprintf(string,
808 "interrupted at byte %d/%d, error=%d",
809 datx8 + 1, nb_bytes, error);
810 return error;
811 }
812 pr_debug("deskewing bit %d:\n", bit_i);
813 success = 1; /* init */
814 /* Set all DQDLYn to maximum value.
815 * Only bit_i will be down-delayed
816 * ==> if we have a fail, it will be definitely
817 * from bit_i
818 */
819 writel(0xFFFFFFFF, DXNDQTR(phy, datx8));
820 /* Arriving at this stage,
821 * we have a success condition with delay = 3;
822 */
823 bit_i_delay_index = 3;
824
825 /* escape if bit delay is out of range or
826 * if a fatil occurs
827 */
828 while ((bit_i_delay_index >= 0) && success) {
829 set_DQ_unit_delay(phy, datx8,
830 bit_i,
831 bit_i_delay_index);
832 BIST_test(phy, datx8, &result);
833 success = result.test_result;
834 bit_i_delay_index--;
835 }
836
837 /* if escape with a fail condition
838 * ==> save this position for bit_i
839 */
840 if (!success) {
841 /* save the delay position.
842 * Add 1 because the while loop ended with a --,
843 * and that we need to hold the last success
844 * delay
845 */
846 deskew_delay[datx8][bit_i] =
847 bit_i_delay_index + 2;
848 if (deskew_delay[datx8][bit_i] > 3)
849 deskew_delay[datx8][bit_i] = 3;
850
851 /* A flag that states we found at least a fail
852 * at one bit.
853 */
854 fail_found = 1;
855 pr_debug("Fail found on bit %d, for delay = %d => deskew[%d][%d] = %d\n",
856 bit_i, bit_i_delay_index + 1,
857 datx8, bit_i,
858 deskew_delay[datx8][bit_i]);
859 } else {
860 /* if we can find a success condition by
861 * back-delaying this bit, just set the delay
862 * to 0 (the best deskew
863 * possible) and mark the bit.
864 */
865 deskew_delay[datx8][bit_i] = 0;
866 /* set a flag that will be used later
867 * in the report.
868 */
869 deskew_non_converge[datx8][bit_i] = 1;
870 pr_debug("Fail not found on bit %d => deskew[%d][%d] = %d\n",
871 bit_i, datx8, bit_i,
872 deskew_delay[datx8][bit_i]);
873 }
874 }
875 pr_debug("**********byte %d tuning complete************\n",
876 datx8);
877 /* If we can't find any failure by back delaying DQ lines,
878 * hold the default values
879 */
880 if (!fail_found) {
881 for (bit_i = 0; bit_i < 8; bit_i++)
882 deskew_delay[datx8][bit_i] = 0;
883 pr_debug("The Deskew algorithm can't converge, there is too much margin in your design. Good job!\n");
884 }
885
886 apply_deskew_results(phy, datx8, deskew_delay,
887 deskew_non_converge);
888 /* Restore nominal value for DQS delay */
889 DQS_phase_delay(phy, datx8, 3);
890 DQS_unit_delay(phy, datx8, 3);
891 /* disable byte after byte bits deskew */
892 clrbits_le32(DXNGCR(phy, datx8), DDRPHYC_DXNGCR_DXEN);
893 } /* end of byte deskew */
894
895 /* re-enable all data bytes */
896 setbits_le32(&phy->dx0gcr, DDRPHYC_DXNGCR_DXEN);
897 setbits_le32(&phy->dx1gcr, DDRPHYC_DXNGCR_DXEN);
898 setbits_le32(&phy->dx2gcr, DDRPHYC_DXNGCR_DXEN);
899 setbits_le32(&phy->dx3gcr, DDRPHYC_DXNGCR_DXEN);
900
901 if (error) {
902 sprintf(string, "error = %d", error);
903 return TEST_FAILED;
904 }
905
906 return TEST_PASSED;
907} /* end function */
908
909/* Trim DQS timings and set it in the centre of data eye.
910 * Look for a PPPPF region, then look for a FPPP region and finally select
911 * the mid of the FPPPPPF region
912 */
913static enum test_result eye_training(struct stm32mp1_ddrctl *ctl,
914 struct stm32mp1_ddrphy *phy, char *string)
915{
916 /*Stores the DQS trim values (PHASE index, unit index) */
917 u8 eye_training_val[NUM_BYTES][2];
918 u8 byte = 0;
919 struct BIST_result result;
920 s8 dqs_unit_delay_index = 0;
921 s8 phase_idx = 0;
922 s8 dqs_unit_delay_index_pass = 0;
923 s8 phase_idx_pass = 0;
924 u8 success = 0;
925 u8 left_phase_bound_found, right_phase_bound_found;
926 u8 left_unit_bound_found, right_unit_bound_found;
927 u8 left_bound_found, right_bound_found;
928 struct tuning_position left_bound, right_bound;
929 u8 error = 0;
930 u8 nb_bytes = get_nb_bytes(ctl);
931
932 /*Disable DQS Drift Compensation*/
933 clrbits_le32(&phy->pgcr, DDRPHYC_PGCR_DFTCMP);
934 /*Disable all bytes*/
935 /* Disable automatic power down of DLL and IOs when disabling a byte
936 * (To avoid having to add programming and delay
937 * for a DLL re-lock when later re-enabling a disabled Byte Lane)
938 */
939 clrbits_le32(&phy->pgcr, DDRPHYC_PGCR_PDDISDX);
940
941 /*Disable all data bytes */
942 clrbits_le32(&phy->dx0gcr, DDRPHYC_DXNGCR_DXEN);
943 clrbits_le32(&phy->dx1gcr, DDRPHYC_DXNGCR_DXEN);
944 clrbits_le32(&phy->dx2gcr, DDRPHYC_DXNGCR_DXEN);
945 clrbits_le32(&phy->dx3gcr, DDRPHYC_DXNGCR_DXEN);
946
947 /* Config the BIST block */
Patrick Delaunay131ec952020-03-06 11:14:08 +0100948 config_BIST(ctl, phy);
Patrick Delaunay2f91e612019-04-10 14:09:29 +0200949
950 for (byte = 0; byte < nb_bytes; byte++) {
951 if (ctrlc()) {
952 sprintf(string, "interrupted at byte %d/%d, error=%d",
953 byte + 1, nb_bytes, error);
954 return TEST_FAILED;
955 }
956 right_bound.phase = 0;
957 right_bound.unit = 0;
958
959 left_bound.phase = 0;
960 left_bound.unit = 0;
961
962 left_phase_bound_found = 0;
963 right_phase_bound_found = 0;
964
965 left_unit_bound_found = 0;
966 right_unit_bound_found = 0;
967
968 left_bound_found = 0;
969 right_bound_found = 0;
970
971 /* Enable Byte (DXNGCR, bit DXEN) */
972 setbits_le32(DXNGCR(phy, byte), DDRPHYC_DXNGCR_DXEN);
973
974 /* Select the byte lane for comparison of read data */
975 BIST_datx8_sel(phy, byte);
976
977 /* Set DQS phase delay to the nominal value. */
978 phase_idx = _90deg;
979 phase_idx_pass = phase_idx;
980
981 /* Set DQS unit delay to the nominal value. */
982 dqs_unit_delay_index = 3;
983 dqs_unit_delay_index_pass = dqs_unit_delay_index;
984 success = 0;
985
986 pr_debug("STEP0: Find Init delay\n");
987 /* STEP0: Find Init delay: a delay that put the system
988 * in a "Pass" condition then (TODO) update
989 * dqs_unit_delay_index_pass & phase_idx_pass
990 */
991 DQS_unit_delay(phy, byte, dqs_unit_delay_index);
992 DQS_phase_delay(phy, byte, phase_idx);
993 BIST_test(phy, byte, &result);
994 success = result.test_result;
995 /* If we have a fail in the nominal condition */
996 if (!success) {
997 /* Look at the left */
998 while (phase_idx >= 0 && !success) {
999 phase_idx--;
1000 DQS_phase_delay(phy, byte, phase_idx);
1001 BIST_test(phy, byte, &result);
1002 success = result.test_result;
1003 }
1004 }
1005 if (!success) {
1006 /* if we can't find pass condition,
1007 * then look at the right
1008 */
1009 phase_idx = _90deg;
1010 while (phase_idx <= MAX_DQS_PHASE_IDX &&
1011 !success) {
1012 phase_idx++;
1013 DQS_phase_delay(phy, byte,
1014 phase_idx);
1015 BIST_test(phy, byte, &result);
1016 success = result.test_result;
1017 }
1018 }
1019 /* save the pass condition */
1020 if (success) {
1021 phase_idx_pass = phase_idx;
1022 } else {
1023 printf("Result: Failed ");
1024 printf("[Cannot DQS timings, ");
1025 printf("there is no PASS region]\n");
1026 error++;
1027 continue;
1028 }
1029
1030 if (ctrlc()) {
1031 sprintf(string, "interrupted at byte %d/%d, error=%d",
1032 byte + 1, nb_bytes, error);
1033 return TEST_FAILED;
1034 }
1035 pr_debug("STEP1: Find LEFT PHASE DQS Bound\n");
1036 /* STEP1: Find LEFT PHASE DQS Bound */
1037 while ((phase_idx >= 0) &&
1038 (phase_idx <= MAX_DQS_PHASE_IDX) &&
1039 !left_phase_bound_found) {
1040 DQS_unit_delay(phy, byte,
1041 dqs_unit_delay_index);
1042 DQS_phase_delay(phy, byte,
1043 phase_idx);
1044 BIST_test(phy, byte, &result);
1045 success = result.test_result;
1046
1047 /*TODO: Manage the case were at the beginning
1048 * there is already a fail
1049 */
1050 if (!success) {
1051 /* the last pass condition */
1052 left_bound.phase = ++phase_idx;
1053 left_phase_bound_found = 1;
1054 } else if (success) {
1055 phase_idx--;
1056 }
1057 }
1058 if (!left_phase_bound_found) {
1059 left_bound.phase = 0;
1060 phase_idx = 0;
1061 }
1062 /* If not found, lets take 0 */
1063
1064 if (ctrlc()) {
1065 sprintf(string, "interrupted at byte %d/%d, error=%d",
1066 byte + 1, nb_bytes, error);
1067 return TEST_FAILED;
1068 }
1069 pr_debug("STEP2: Find UNIT left bound\n");
1070 /* STEP2: Find UNIT left bound */
1071 while ((dqs_unit_delay_index >= 0) &&
1072 !left_unit_bound_found) {
1073 DQS_unit_delay(phy, byte,
1074 dqs_unit_delay_index);
1075 DQS_phase_delay(phy, byte, phase_idx);
1076 BIST_test(phy, byte, &result);
1077 success = result.test_result;
1078 if (!success) {
1079 left_bound.unit =
1080 ++dqs_unit_delay_index;
1081 left_unit_bound_found = 1;
1082 left_bound_found = 1;
1083 } else if (success) {
1084 dqs_unit_delay_index--;
1085 }
1086 }
1087
1088 /* If not found, lets take 0 */
1089 if (!left_unit_bound_found)
1090 left_bound.unit = 0;
1091
1092 if (ctrlc()) {
1093 sprintf(string, "interrupted at byte %d/%d, error=%d",
1094 byte + 1, nb_bytes, error);
1095 return TEST_FAILED;
1096 }
1097 pr_debug("STEP3: Find PHase right bound\n");
1098 /* STEP3: Find PHase right bound, start with "pass"
1099 * condition
1100 */
1101
1102 /* Set DQS phase delay to the pass value. */
1103 phase_idx = phase_idx_pass;
1104
1105 /* Set DQS unit delay to the pass value. */
1106 dqs_unit_delay_index = dqs_unit_delay_index_pass;
1107
1108 while ((phase_idx <= MAX_DQS_PHASE_IDX) &&
1109 !right_phase_bound_found) {
1110 DQS_unit_delay(phy, byte,
1111 dqs_unit_delay_index);
1112 DQS_phase_delay(phy, byte, phase_idx);
1113 BIST_test(phy, byte, &result);
1114 success = result.test_result;
1115 if (!success) {
1116 /* the last pass condition */
1117 right_bound.phase = --phase_idx;
1118 right_phase_bound_found = 1;
1119 } else if (success) {
1120 phase_idx++;
1121 }
1122 }
1123
1124 /* If not found, lets take the max value */
1125 if (!right_phase_bound_found) {
1126 right_bound.phase = MAX_DQS_PHASE_IDX;
1127 phase_idx = MAX_DQS_PHASE_IDX;
1128 }
1129
1130 if (ctrlc()) {
1131 sprintf(string, "interrupted at byte %d/%d, error=%d",
1132 byte + 1, nb_bytes, error);
1133 return TEST_FAILED;
1134 }
1135 pr_debug("STEP4: Find UNIT right bound\n");
1136 /* STEP4: Find UNIT right bound */
1137 while ((dqs_unit_delay_index <= MAX_DQS_UNIT_IDX) &&
1138 !right_unit_bound_found) {
1139 DQS_unit_delay(phy, byte,
1140 dqs_unit_delay_index);
1141 DQS_phase_delay(phy, byte, phase_idx);
1142 BIST_test(phy, byte, &result);
1143 success = result.test_result;
1144 if (!success) {
1145 right_bound.unit =
1146 --dqs_unit_delay_index;
1147 right_unit_bound_found = 1;
1148 right_bound_found = 1;
1149 } else if (success) {
1150 dqs_unit_delay_index++;
1151 }
1152 }
1153 /* If not found, lets take the max value */
1154 if (!right_unit_bound_found)
1155 right_bound.unit = MAX_DQS_UNIT_IDX;
1156
1157 /* If we found a regular FAil Pass FAil pattern
1158 * FFPPPPPPFF
1159 * OR PPPPPFF Or FFPPPPP
1160 */
1161
1162 if (left_bound_found || right_bound_found) {
1163 eye_training_val[byte][0] = (right_bound.phase +
1164 left_bound.phase) / 2;
1165 eye_training_val[byte][1] = (right_bound.unit +
1166 left_bound.unit) / 2;
1167
1168 /* If we already lost 1/2PHASE Tuning,
1169 * let's try to recover by ++ on unit
1170 */
1171 if (((right_bound.phase + left_bound.phase) % 2 == 1) &&
1172 eye_training_val[byte][1] != MAX_DQS_UNIT_IDX)
1173 eye_training_val[byte][1]++;
1174 pr_debug("** found phase : %d - %d & unit %d - %d\n",
1175 right_bound.phase, left_bound.phase,
1176 right_bound.unit, left_bound.unit);
1177 pr_debug("** calculating mid region: phase: %d unit: %d (nominal is 3)\n",
1178 eye_training_val[byte][0],
1179 eye_training_val[byte][1]);
1180 } else {
1181 /* PPPPPPPPPP, we're already good.
1182 * Set nominal values.
1183 */
1184 eye_training_val[byte][0] = 3;
1185 eye_training_val[byte][1] = 3;
1186 }
1187 DQS_phase_delay(phy, byte, eye_training_val[byte][0]);
1188 DQS_unit_delay(phy, byte, eye_training_val[byte][1]);
1189
1190 printf("Byte %d, DQS unit = %d, phase = %d\n",
1191 byte,
1192 eye_training_val[byte][1],
1193 eye_training_val[byte][0]);
1194 }
1195
1196 if (error) {
1197 sprintf(string, "error = %d", error);
1198 return TEST_FAILED;
1199 }
1200
1201 return TEST_PASSED;
1202}
1203
1204static void display_reg_results(struct stm32mp1_ddrphy *phy, u8 byte)
1205{
1206 u8 i = 0;
1207
1208 printf("Byte %d Dekew result, bit0 delay, bit1 delay...bit8 delay\n ",
1209 byte);
1210
1211 for (i = 0; i < 8; i++)
1212 printf("%d ", DQ_unit_index(phy, byte, i));
1213 printf("\n");
1214
1215 printf("dxndllcr: [%08x] val:%08x\n",
1216 DXNDLLCR(phy, byte),
1217 readl(DXNDLLCR(phy, byte)));
1218 printf("dxnqdstr: [%08x] val:%08x\n",
1219 DXNDQSTR(phy, byte),
1220 readl(DXNDQSTR(phy, byte)));
1221 printf("dxndqtr: [%08x] val:%08x\n",
1222 DXNDQTR(phy, byte),
1223 readl(DXNDQTR(phy, byte)));
1224}
1225
1226/* analyse the dgs gating log table, and determine the midpoint.*/
1227static u8 set_midpoint_read_dqs_gating(struct stm32mp1_ddrphy *phy, u8 byte,
1228 u8 dqs_gating[NUM_BYTES]
1229 [MAX_GSL_IDX + 1]
1230 [MAX_GPS_IDX + 1])
1231{
1232 /* stores the dqs gate values (gsl index, gps index) */
1233 u8 dqs_gate_values[NUM_BYTES][2];
1234 u8 gsl_idx, gps_idx = 0;
1235 u8 left_bound_idx[2] = {0, 0};
1236 u8 right_bound_idx[2] = {0, 0};
1237 u8 left_bound_found = 0;
1238 u8 right_bound_found = 0;
1239 u8 intermittent = 0;
1240 u8 value;
1241
1242 for (gsl_idx = 0; gsl_idx <= MAX_GSL_IDX; gsl_idx++) {
1243 for (gps_idx = 0; gps_idx <= MAX_GPS_IDX; gps_idx++) {
1244 value = dqs_gating[byte][gsl_idx][gps_idx];
1245 if (value == 1 && left_bound_found == 0) {
1246 left_bound_idx[0] = gsl_idx;
1247 left_bound_idx[1] = gps_idx;
1248 left_bound_found = 1;
1249 } else if (value == 0 &&
1250 left_bound_found == 1 &&
1251 !right_bound_found) {
1252 if (gps_idx == 0) {
1253 right_bound_idx[0] = gsl_idx - 1;
1254 right_bound_idx[1] = MAX_GPS_IDX;
1255 } else {
1256 right_bound_idx[0] = gsl_idx;
1257 right_bound_idx[1] = gps_idx - 1;
1258 }
1259 right_bound_found = 1;
1260 } else if (value == 1 &&
1261 right_bound_found == 1) {
1262 intermittent = 1;
1263 }
1264 }
1265 }
1266
1267 /* if only ppppppp is found, there is no mid region. */
1268 if (left_bound_idx[0] == 0 && left_bound_idx[1] == 0 &&
1269 right_bound_idx[0] == 0 && right_bound_idx[1] == 0)
1270 intermittent = 1;
1271
1272 /*if we found a regular fail pass fail pattern ffppppppff
1273 * or pppppff or ffppppp
1274 */
1275 if (!intermittent) {
1276 /*if we found a regular fail pass fail pattern ffppppppff
1277 * or pppppff or ffppppp
1278 */
1279 if (left_bound_found || right_bound_found) {
1280 pr_debug("idx0(%d): %d %d idx1(%d) : %d %d\n",
1281 left_bound_found,
1282 right_bound_idx[0], left_bound_idx[0],
1283 right_bound_found,
1284 right_bound_idx[1], left_bound_idx[1]);
1285 dqs_gate_values[byte][0] =
1286 (right_bound_idx[0] + left_bound_idx[0]) / 2;
1287 dqs_gate_values[byte][1] =
1288 (right_bound_idx[1] + left_bound_idx[1]) / 2;
1289 /* if we already lost 1/2gsl tuning,
1290 * let's try to recover by ++ on gps
1291 */
1292 if (((right_bound_idx[0] +
1293 left_bound_idx[0]) % 2 == 1) &&
1294 dqs_gate_values[byte][1] != MAX_GPS_IDX)
1295 dqs_gate_values[byte][1]++;
1296 /* if we already lost 1/2gsl tuning and gps is on max*/
1297 else if (((right_bound_idx[0] +
1298 left_bound_idx[0]) % 2 == 1) &&
1299 dqs_gate_values[byte][1] == MAX_GPS_IDX) {
1300 dqs_gate_values[byte][1] = 0;
1301 dqs_gate_values[byte][0]++;
1302 }
1303 /* if we have gsl left and write limit too close
1304 * (difference=1)
1305 */
1306 if (((right_bound_idx[0] - left_bound_idx[0]) == 1)) {
1307 dqs_gate_values[byte][1] = (left_bound_idx[1] +
1308 right_bound_idx[1] +
1309 4) / 2;
1310 if (dqs_gate_values[byte][1] >= 4) {
1311 dqs_gate_values[byte][0] =
1312 right_bound_idx[0];
1313 dqs_gate_values[byte][1] -= 4;
1314 } else {
1315 dqs_gate_values[byte][0] =
1316 left_bound_idx[0];
1317 }
1318 }
1319 pr_debug("*******calculating mid region: system latency: %d phase: %d********\n",
1320 dqs_gate_values[byte][0],
1321 dqs_gate_values[byte][1]);
1322 pr_debug("*******the nominal values were system latency: 0 phase: 2*******\n");
Patrick Delaunay2f91e612019-04-10 14:09:29 +02001323 }
1324 } else {
1325 /* if intermitant, restore defaut values */
1326 pr_debug("dqs gating:no regular fail/pass/fail found. defaults values restored.\n");
Patrick Delaunay030df3b2020-03-06 11:14:04 +01001327 dqs_gate_values[byte][0] = 0;
1328 dqs_gate_values[byte][1] = 2;
Patrick Delaunay2f91e612019-04-10 14:09:29 +02001329 }
Patrick Delaunay030df3b2020-03-06 11:14:04 +01001330 set_r0dgsl_delay(phy, byte, dqs_gate_values[byte][0]);
1331 set_r0dgps_delay(phy, byte, dqs_gate_values[byte][1]);
1332 printf("Byte %d, R0DGSL = %d, R0DGPS = %d\n",
1333 byte, dqs_gate_values[byte][0], dqs_gate_values[byte][1]);
Patrick Delaunay2f91e612019-04-10 14:09:29 +02001334
1335 /* return 0 if intermittent or if both left_bound
1336 * and right_bound are not found
1337 */
1338 return !(intermittent || (left_bound_found && right_bound_found));
1339}
1340
1341static enum test_result read_dqs_gating(struct stm32mp1_ddrctl *ctl,
1342 struct stm32mp1_ddrphy *phy,
1343 char *string)
1344{
1345 /* stores the log of pass/fail */
1346 u8 dqs_gating[NUM_BYTES][MAX_GSL_IDX + 1][MAX_GPS_IDX + 1];
1347 u8 byte, gsl_idx, gps_idx = 0;
1348 struct BIST_result result;
1349 u8 success = 0;
1350 u8 nb_bytes = get_nb_bytes(ctl);
1351
1352 memset(dqs_gating, 0x0, sizeof(dqs_gating));
1353
1354 /*disable dqs drift compensation*/
1355 clrbits_le32(&phy->pgcr, DDRPHYC_PGCR_DFTCMP);
1356 /*disable all bytes*/
1357 /* disable automatic power down of dll and ios when disabling a byte
1358 * (to avoid having to add programming and delay
1359 * for a dll re-lock when later re-enabling a disabled byte lane)
1360 */
1361 clrbits_le32(&phy->pgcr, DDRPHYC_PGCR_PDDISDX);
1362
1363 /* disable all data bytes */
1364 clrbits_le32(&phy->dx0gcr, DDRPHYC_DXNGCR_DXEN);
1365 clrbits_le32(&phy->dx1gcr, DDRPHYC_DXNGCR_DXEN);
1366 clrbits_le32(&phy->dx2gcr, DDRPHYC_DXNGCR_DXEN);
1367 clrbits_le32(&phy->dx3gcr, DDRPHYC_DXNGCR_DXEN);
1368
1369 /* config the bist block */
Patrick Delaunay131ec952020-03-06 11:14:08 +01001370 config_BIST(ctl, phy);
Patrick Delaunay2f91e612019-04-10 14:09:29 +02001371
1372 for (byte = 0; byte < nb_bytes; byte++) {
1373 if (ctrlc()) {
1374 sprintf(string, "interrupted at byte %d/%d",
1375 byte + 1, nb_bytes);
1376 return TEST_FAILED;
1377 }
1378 /* enable byte x (dxngcr, bit dxen) */
1379 setbits_le32(DXNGCR(phy, byte), DDRPHYC_DXNGCR_DXEN);
1380
1381 /* select the byte lane for comparison of read data */
1382 BIST_datx8_sel(phy, byte);
1383 for (gsl_idx = 0; gsl_idx <= MAX_GSL_IDX; gsl_idx++) {
1384 for (gps_idx = 0; gps_idx <= MAX_GPS_IDX; gps_idx++) {
1385 if (ctrlc()) {
1386 sprintf(string,
1387 "interrupted at byte %d/%d",
1388 byte + 1, nb_bytes);
1389 return TEST_FAILED;
1390 }
1391 /* write cfg to dxndqstr */
1392 set_r0dgsl_delay(phy, byte, gsl_idx);
1393 set_r0dgps_delay(phy, byte, gps_idx);
1394
1395 BIST_test(phy, byte, &result);
1396 success = result.test_result;
1397 if (success)
1398 dqs_gating[byte][gsl_idx][gps_idx] = 1;
1399 itm_soft_reset(phy);
1400 }
1401 }
1402 set_midpoint_read_dqs_gating(phy, byte, dqs_gating);
1403 /* dummy reads */
1404 readl(0xc0000000);
1405 readl(0xc0000000);
1406 }
1407
1408 /* re-enable drift compensation */
1409 /* setbits_le32(&phy->pgcr, DDRPHYC_PGCR_DFTCMP); */
1410 return TEST_PASSED;
1411}
1412
1413/****************************************************************
1414 * TEST
1415 ****************************************************************
1416 */
1417static enum test_result do_read_dqs_gating(struct stm32mp1_ddrctl *ctl,
1418 struct stm32mp1_ddrphy *phy,
1419 char *string, int argc,
1420 char *argv[])
1421{
1422 u32 rfshctl3 = readl(&ctl->rfshctl3);
1423 u32 pwrctl = readl(&ctl->pwrctl);
Patrick Delaunay656d2a92020-03-06 11:14:07 +01001424 u32 derateen = readl(&ctl->derateen);
Patrick Delaunay2f91e612019-04-10 14:09:29 +02001425 enum test_result res;
1426
Patrick Delaunay656d2a92020-03-06 11:14:07 +01001427 writel(0x0, &ctl->derateen);
Patrick Delaunay2f91e612019-04-10 14:09:29 +02001428 stm32mp1_refresh_disable(ctl);
Patrick Delaunay656d2a92020-03-06 11:14:07 +01001429
Patrick Delaunay2f91e612019-04-10 14:09:29 +02001430 res = read_dqs_gating(ctl, phy, string);
Patrick Delaunay656d2a92020-03-06 11:14:07 +01001431
Patrick Delaunay2f91e612019-04-10 14:09:29 +02001432 stm32mp1_refresh_restore(ctl, rfshctl3, pwrctl);
Patrick Delaunay656d2a92020-03-06 11:14:07 +01001433 writel(derateen, &ctl->derateen);
Patrick Delaunay2f91e612019-04-10 14:09:29 +02001434
1435 return res;
1436}
1437
1438static enum test_result do_bit_deskew(struct stm32mp1_ddrctl *ctl,
1439 struct stm32mp1_ddrphy *phy,
1440 char *string, int argc, char *argv[])
1441{
1442 u32 rfshctl3 = readl(&ctl->rfshctl3);
1443 u32 pwrctl = readl(&ctl->pwrctl);
Patrick Delaunay656d2a92020-03-06 11:14:07 +01001444 u32 derateen = readl(&ctl->derateen);
Patrick Delaunay2f91e612019-04-10 14:09:29 +02001445 enum test_result res;
1446
Patrick Delaunay656d2a92020-03-06 11:14:07 +01001447 writel(0x0, &ctl->derateen);
Patrick Delaunay2f91e612019-04-10 14:09:29 +02001448 stm32mp1_refresh_disable(ctl);
Patrick Delaunay656d2a92020-03-06 11:14:07 +01001449
Patrick Delaunay2f91e612019-04-10 14:09:29 +02001450 res = bit_deskew(ctl, phy, string);
Patrick Delaunay656d2a92020-03-06 11:14:07 +01001451
Patrick Delaunay2f91e612019-04-10 14:09:29 +02001452 stm32mp1_refresh_restore(ctl, rfshctl3, pwrctl);
Patrick Delaunay656d2a92020-03-06 11:14:07 +01001453 writel(derateen, &ctl->derateen);
Patrick Delaunay2f91e612019-04-10 14:09:29 +02001454
1455 return res;
1456}
1457
1458static enum test_result do_eye_training(struct stm32mp1_ddrctl *ctl,
1459 struct stm32mp1_ddrphy *phy,
1460 char *string, int argc, char *argv[])
1461{
1462 u32 rfshctl3 = readl(&ctl->rfshctl3);
1463 u32 pwrctl = readl(&ctl->pwrctl);
Patrick Delaunay656d2a92020-03-06 11:14:07 +01001464 u32 derateen = readl(&ctl->derateen);
Patrick Delaunay2f91e612019-04-10 14:09:29 +02001465 enum test_result res;
1466
Patrick Delaunay656d2a92020-03-06 11:14:07 +01001467 writel(0x0, &ctl->derateen);
Patrick Delaunay2f91e612019-04-10 14:09:29 +02001468 stm32mp1_refresh_disable(ctl);
Patrick Delaunay656d2a92020-03-06 11:14:07 +01001469
Patrick Delaunay2f91e612019-04-10 14:09:29 +02001470 res = eye_training(ctl, phy, string);
Patrick Delaunay656d2a92020-03-06 11:14:07 +01001471
Patrick Delaunay2f91e612019-04-10 14:09:29 +02001472 stm32mp1_refresh_restore(ctl, rfshctl3, pwrctl);
Patrick Delaunay656d2a92020-03-06 11:14:07 +01001473 writel(derateen, &ctl->derateen);
Patrick Delaunay2f91e612019-04-10 14:09:29 +02001474
1475 return res;
1476}
1477
1478static enum test_result do_display(struct stm32mp1_ddrctl *ctl,
1479 struct stm32mp1_ddrphy *phy,
1480 char *string, int argc, char *argv[])
1481{
1482 int byte;
1483 u8 nb_bytes = get_nb_bytes(ctl);
1484
1485 for (byte = 0; byte < nb_bytes; byte++)
1486 display_reg_results(phy, byte);
1487
1488 return TEST_PASSED;
1489}
1490
1491static enum test_result do_bist_config(struct stm32mp1_ddrctl *ctl,
1492 struct stm32mp1_ddrphy *phy,
1493 char *string, int argc, char *argv[])
1494{
1495 unsigned long value;
1496
1497 if (argc > 0) {
1498 if (strict_strtoul(argv[0], 0, &value) < 0) {
1499 sprintf(string, "invalid nbErr %s", argv[0]);
1500 return TEST_FAILED;
1501 }
1502 BIST_error_max = value;
1503 }
1504 if (argc > 1) {
1505 if (strict_strtoul(argv[1], 0, &value) < 0) {
1506 sprintf(string, "invalid Seed %s", argv[1]);
1507 return TEST_FAILED;
1508 }
1509 BIST_seed = value;
1510 }
1511 printf("Bist.nbErr = %d\n", BIST_error_max);
1512 if (BIST_seed)
1513 printf("Bist.Seed = 0x%x\n", BIST_seed);
1514 else
1515 printf("Bist.Seed = random\n");
1516
1517 return TEST_PASSED;
1518}
1519
1520/****************************************************************
1521 * TEST Description
1522 ****************************************************************
1523 */
1524
1525const struct test_desc tuning[] = {
1526 {do_read_dqs_gating, "Read DQS gating",
1527 "software read DQS Gating", "", 0 },
1528 {do_bit_deskew, "Bit de-skew", "", "", 0 },
1529 {do_eye_training, "Eye Training", "or DQS training", "", 0 },
1530 {do_display, "Display registers", "", "", 0 },
1531 {do_bist_config, "Bist config", "[nbErr] [seed]",
1532 "configure Bist test", 2},
1533};
1534
1535const int tuning_nb = ARRAY_SIZE(tuning);