1
2
3
4
5
6#define LOG_CATEGORY UCLASS_RAM
7
8#include <common.h>
9#include <console.h>
10#include <clk.h>
11#include <log.h>
12#include <ram.h>
13#include <rand.h>
14#include <reset.h>
15#include <asm/io.h>
16#include <linux/bitops.h>
17#include <linux/delay.h>
18#include <linux/iopoll.h>
19
20#include "stm32mp1_ddr_regs.h"
21#include "stm32mp1_ddr.h"
22#include "stm32mp1_tests.h"
23
24#define MAX_DQS_PHASE_IDX _144deg
25#define MAX_DQS_UNIT_IDX 7
26#define MAX_GSL_IDX 5
27#define MAX_GPS_IDX 3
28
29
30#define NUM_BYTES 4
31
32enum dqs_phase_enum {
33 _36deg = 0,
34 _54deg = 1,
35 _72deg = 2,
36 _90deg = 3,
37 _108deg = 4,
38 _126deg = 5,
39 _144deg = 6
40};
41
42
43struct BIST_result {
44
45
46
47
48 bool test_result;
49
50 bool all_bits_fail;
51 bool bit_i_test_result[8];
52};
53
54
55struct tuning_position {
56 u8 phase;
57 u8 unit;
58 u32 bits_delay;
59};
60
61
62const u8 dx_dll_phase[7] = {3, 2, 1, 0, 14, 13, 12};
63
64static u8 BIST_error_max = 1;
65static u32 BIST_seed = 0x1234ABCD;
66
67static u8 get_nb_bytes(struct stm32mp1_ddrctl *ctl)
68{
69 u32 data_bus = readl(&ctl->mstr) & DDRCTRL_MSTR_DATA_BUS_WIDTH_MASK;
70 u8 nb_bytes = NUM_BYTES;
71
72 switch (data_bus) {
73 case DDRCTRL_MSTR_DATA_BUS_WIDTH_HALF:
74 nb_bytes /= 2;
75 break;
76 case DDRCTRL_MSTR_DATA_BUS_WIDTH_QUARTER:
77 nb_bytes /= 4;
78 break;
79 default:
80 break;
81 }
82
83 return nb_bytes;
84}
85
86static u8 get_nb_bank(struct stm32mp1_ddrctl *ctl)
87{
88
89 u8 bits = 0;
90 u32 reg, val;
91
92 reg = readl(&ctl->addrmap1);
93
94 val = (reg & GENMASK(5, 0)) >> 0;
95 if (val <= 31)
96 bits++;
97
98 val = (reg & GENMASK(13, 8)) >> 8;
99 if (val <= 31)
100 bits++;
101
102 val = (reg & GENMASK(21, 16)) >> 16;
103 if (val <= 31)
104 bits++;
105
106 return bits;
107}
108
109static u8 get_nb_col(struct stm32mp1_ddrctl *ctl)
110{
111 u8 bits;
112 u32 reg, val;
113
114
115 bits = 2;
116
117 reg = readl(&ctl->addrmap2);
118
119 val = (reg & GENMASK(3, 0)) >> 0;
120 if (val <= 7)
121 bits++;
122
123 val = (reg & GENMASK(11, 8)) >> 8;
124 if (val <= 7)
125 bits++;
126
127 val = (reg & GENMASK(19, 16)) >> 16;
128 if (val <= 7)
129 bits++;
130
131 val = (reg & GENMASK(27, 24)) >> 24;
132 if (val <= 7)
133 bits++;
134
135 reg = readl(&ctl->addrmap3);
136
137 val = (reg & GENMASK(3, 0)) >> 0;
138 if (val <= 7)
139 bits++;
140
141 val = (reg & GENMASK(11, 8)) >> 8;
142 if (val <= 7)
143 bits++;
144
145 val = (reg & GENMASK(19, 16)) >> 16;
146 if (val <= 7)
147 bits++;
148
149 val = (reg & GENMASK(27, 24)) >> 24;
150 if (val <= 7)
151 bits++;
152
153 reg = readl(&ctl->addrmap4);
154
155 val = (reg & GENMASK(3, 0)) >> 0;
156 if (val <= 7)
157 bits++;
158
159 val = (reg & GENMASK(11, 8)) >> 8;
160 if (val <= 7)
161 bits++;
162
163 return bits;
164}
165
166static u8 get_nb_row(struct stm32mp1_ddrctl *ctl)
167{
168
169 u8 bits = 0;
170 u32 reg, val;
171
172 reg = readl(&ctl->addrmap5);
173
174 val = (reg & GENMASK(3, 0)) >> 0;
175 if (val <= 11)
176 bits++;
177
178 val = (reg & GENMASK(11, 8)) >> 8;
179 if (val <= 11)
180 bits++;
181
182 val = (reg & GENMASK(19, 16)) >> 16;
183 if (val <= 11)
184 bits += 9;
185 else
186 printf("warning: addrmap5.addrmap_row_b2_10 not supported\n");
187
188 val = (reg & GENMASK(27, 24)) >> 24;
189 if (val <= 11)
190 bits++;
191
192 reg = readl(&ctl->addrmap6);
193
194 val = (reg & GENMASK(3, 0)) >> 0;
195 if (val <= 7)
196 bits++;
197
198 val = (reg & GENMASK(11, 8)) >> 8;
199 if (val <= 7)
200 bits++;
201
202 val = (reg & GENMASK(19, 16)) >> 16;
203 if (val <= 7)
204 bits++;
205
206 val = (reg & GENMASK(27, 24)) >> 24;
207 if (val <= 7)
208 bits++;
209
210 return bits;
211}
212
213static void itm_soft_reset(struct stm32mp1_ddrphy *phy)
214{
215 stm32mp1_ddrphy_init(phy, DDRPHYC_PIR_ITMSRST);
216}
217
218
219
220
221
222static u8 DQ_unit_index(struct stm32mp1_ddrphy *phy, u8 byte, u8 bit)
223{
224 u32 index;
225 u32 addr = DXNDQTR(phy, byte);
226
227
228
229
230 index = (readl(addr) >> DDRPHYC_DXNDQTR_DQDLY_SHIFT(bit))
231 & DDRPHYC_DXNDQTR_DQDLY_LOW_MASK;
232
233 log_debug("[%x]: %x => DQ unit index = %x\n", addr, readl(addr), index);
234
235 return index;
236}
237
238
239
240
241
242static void DQS_phase_delay(struct stm32mp1_ddrphy *phy, u8 byte, u8 phase_idx)
243{
244 u8 sdphase_val = 0;
245
246
247 sdphase_val = dx_dll_phase[phase_idx];
248 clrsetbits_le32(DXNDLLCR(phy, byte),
249 DDRPHYC_DXNDLLCR_SDPHASE_MASK,
250 sdphase_val << DDRPHYC_DXNDLLCR_SDPHASE_SHIFT);
251}
252
253
254
255
256
257static void DQS_unit_delay(struct stm32mp1_ddrphy *phy,
258 u8 byte, u8 unit_dly_idx)
259{
260
261 clrsetbits_le32(DXNDQSTR(phy, byte),
262 DDRPHYC_DXNDQSTR_DQSDLY_MASK |
263 DDRPHYC_DXNDQSTR_DQSNDLY_MASK,
264 (unit_dly_idx << DDRPHYC_DXNDQSTR_DQSDLY_SHIFT) |
265 (unit_dly_idx << DDRPHYC_DXNDQSTR_DQSNDLY_SHIFT));
266
267
268
269
270 stm32mp1_ddrphy_init(phy, DDRPHYC_PIR_ITMSRST);
271}
272
273
274
275
276static void set_DQ_unit_delay(struct stm32mp1_ddrphy *phy,
277 u8 byte, u8 bit,
278 u8 dq_delay_index)
279{
280 u8 dq_bit_delay_val = dq_delay_index | (dq_delay_index << 2);
281
282
283 clrsetbits_le32(DXNDQTR(phy, byte),
284 DDRPHYC_DXNDQTR_DQDLY_MASK
285 << DDRPHYC_DXNDQTR_DQDLY_SHIFT(bit),
286 dq_bit_delay_val << DDRPHYC_DXNDQTR_DQDLY_SHIFT(bit));
287}
288
289static void set_r0dgsl_delay(struct stm32mp1_ddrphy *phy,
290 u8 byte, u8 r0dgsl_idx)
291{
292 clrsetbits_le32(DXNDQSTR(phy, byte),
293 DDRPHYC_DXNDQSTR_R0DGSL_MASK,
294 r0dgsl_idx << DDRPHYC_DXNDQSTR_R0DGSL_SHIFT);
295}
296
297static void set_r0dgps_delay(struct stm32mp1_ddrphy *phy,
298 u8 byte, u8 r0dgps_idx)
299{
300 clrsetbits_le32(DXNDQSTR(phy, byte),
301 DDRPHYC_DXNDQSTR_R0DGPS_MASK,
302 r0dgps_idx << DDRPHYC_DXNDQSTR_R0DGPS_SHIFT);
303}
304
305
306static void config_BIST(struct stm32mp1_ddrctl *ctl,
307 struct stm32mp1_ddrphy *phy)
308{
309 u8 nb_bank = get_nb_bank(ctl);
310 u8 nb_row = get_nb_row(ctl);
311 u8 nb_col = get_nb_col(ctl);
312
313
314 u32 bbank = 0;
315
316 u32 brow = 0;
317
318 u32 bcol = 0;
319
320
321
322 u32 bainc = 0x00000008;
323
324
325
326
327 u32 bmrank = 0;
328
329
330
331 u32 brank = 0;
332
333
334
335
336 u32 bmbank = (1 << nb_bank) - 1;
337
338
339
340 u32 bmrow = (1 << nb_row) - 1;
341
342
343
344 u32 bmcol = (1 << nb_col) - 1;
345
346 u32 bmode_conf = 0x00000001;
347 u32 bdxen_conf = 0x00000001;
348 u32 bdpat_conf = 0x00000002;
349
350
351
352
353
354
355
356 writel(0x3, &phy->bistrr);
357
358 writel((bmode_conf << 3) | (bdxen_conf << 14) | (bdpat_conf << 17),
359 &phy->bistrr);
360
361
362
363 writel(0x00000200, &phy->bistwcr);
364
365 writel(bcol | (brow << 12) | (bbank << 28), &phy->bistar0);
366 writel(brank | (bmrank << 2) | (bainc << 4), &phy->bistar1);
367 writel(bmcol | (bmrow << 12) | (bmbank << 28), &phy->bistar2);
368}
369
370
371static void BIST_datx8_sel(struct stm32mp1_ddrphy *phy, u8 datx8)
372{
373 clrsetbits_le32(&phy->bistrr,
374 DDRPHYC_BISTRR_BDXSEL_MASK,
375 datx8 << DDRPHYC_BISTRR_BDXSEL_SHIFT);
376
377
378
379}
380
381
382static void BIST_test(struct stm32mp1_ddrphy *phy, u8 byte,
383 struct BIST_result *bist)
384{
385 bool result = true;
386 u32 cnt = 0;
387 u32 error = 0;
388 u32 val;
389 int ret;
390
391 bist->test_result = true;
392
393run:
394 itm_soft_reset(phy);
395
396
397
398 clrsetbits_le32(&phy->bistrr,
399 0x00000007,
400 0x00000003);
401
402
403
404 if (BIST_seed)
405 writel(BIST_seed, &phy->bistlsr);
406 else
407 writel(rand(), &phy->bistlsr);
408
409
410 udelay(10);
411
412
413 clrsetbits_le32(&phy->bistrr,
414 0x00000007,
415 0x00000001);
416
417
418
419 ret = readl_poll_timeout(&phy->bistgsr, val,
420 val & DDRPHYC_BISTGSR_BDDONE, 1000);
421
422 if (ret < 0) {
423 printf("warning: BIST timeout\n");
424 result = false;
425
426 clrsetbits_le32(&phy->bistrr, 0x00000007, 0x00000002);
427 } else {
428
429
430 if (((readl(&phy->bistwcsr)) >> DDRPHYC_BISTWCSR_DXWCNT_SHIFT)
431 == readl(&phy->bistwcr)) {
432
433
434 if (readl(&phy->bistgsr) & DDRPHYC_BISTGSR_BDXERR)
435 result = false;
436 else
437 result = true;
438 } else {
439 result = false;
440 }
441 }
442
443
444 cnt++;
445 if (result && cnt != 1000)
446 goto run;
447
448 if (!result)
449 error++;
450
451 if (error < BIST_error_max) {
452 if (cnt != 1000)
453 goto run;
454 bist->test_result = true;
455 } else {
456 bist->test_result = false;
457 }
458}
459
460
461
462
463
464
465static void apply_deskew_results(struct stm32mp1_ddrphy *phy, u8 byte,
466 u8 deskew_delay[NUM_BYTES][8],
467 u8 deskew_non_converge[NUM_BYTES][8])
468{
469 u8 bit_i;
470 u8 index;
471
472 for (bit_i = 0; bit_i < 8; bit_i++) {
473 set_DQ_unit_delay(phy, byte, bit_i, deskew_delay[byte][bit_i]);
474 index = DQ_unit_index(phy, byte, bit_i);
475 log_debug("Byte %d ; bit %d : The new DQ delay (%d) index=%d [delta=%d, 3 is the default]",
476 byte, bit_i, deskew_delay[byte][bit_i],
477 index, index - 3);
478 printf("Byte %d, bit %d, DQ delay = %d",
479 byte, bit_i, deskew_delay[byte][bit_i]);
480 if (deskew_non_converge[byte][bit_i] == 1)
481 log_debug(" - not converged : still more skew");
482 printf("\n");
483 }
484}
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500static enum test_result bit_deskew(struct stm32mp1_ddrctl *ctl,
501 struct stm32mp1_ddrphy *phy, char *string)
502{
503
504 u8 deskew_delay[NUM_BYTES][8];
505
506 u8 deskew_non_converge[NUM_BYTES][8];
507 struct BIST_result result;
508 s8 dqs_unit_delay_index = 0;
509 u8 datx8 = 0;
510 u8 bit_i = 0;
511 s8 phase_idx = 0;
512 s8 bit_i_delay_index = 0;
513 u8 success = 0;
514 struct tuning_position last_right_ok;
515 u8 force_stop = 0;
516 u8 fail_found;
517 u8 error = 0;
518 u8 nb_bytes = get_nb_bytes(ctl);
519
520
521 memset(deskew_delay, 0, sizeof(deskew_delay));
522 memset(deskew_non_converge, 0, sizeof(deskew_non_converge));
523
524
525 clrbits_le32(&phy->pgcr, DDRPHYC_PGCR_DFTCMP);
526
527
528
529
530
531 clrbits_le32(&phy->pgcr, DDRPHYC_PGCR_PDDISDX);
532
533
534 clrbits_le32(&phy->dx0gcr, DDRPHYC_DXNGCR_DXEN);
535 clrbits_le32(&phy->dx1gcr, DDRPHYC_DXNGCR_DXEN);
536 clrbits_le32(&phy->dx2gcr, DDRPHYC_DXNGCR_DXEN);
537 clrbits_le32(&phy->dx3gcr, DDRPHYC_DXNGCR_DXEN);
538
539
540 config_BIST(ctl, phy);
541 log_debug("BIST Config done.\n");
542
543
544 for (datx8 = 0; datx8 < nb_bytes; datx8++) {
545 if (ctrlc()) {
546 sprintf(string, "interrupted at byte %d/%d, error=%d",
547 datx8 + 1, nb_bytes, error);
548 return TEST_FAILED;
549 }
550 log_debug("\n======================\n");
551 log_debug("Start deskew byte %d .\n", datx8);
552 log_debug("======================\n");
553
554 setbits_le32(DXNGCR(phy, datx8), DDRPHYC_DXNGCR_DXEN);
555
556
557 BIST_datx8_sel(phy, datx8);
558
559
560
561
562
563 writel(0xAAAAAAAA, DXNDQTR(phy, datx8));
564
565
566
567
568
569 phase_idx = _90deg;
570
571
572 dqs_unit_delay_index = MAX_DQS_UNIT_IDX;
573 DQS_unit_delay(phy, datx8, dqs_unit_delay_index);
574 DQS_phase_delay(phy, datx8, phase_idx);
575
576
577 clrbits_le32(DXNDLLCR(phy, datx8), DDRPHYC_DXNDLLCR_DLLSRST);
578 setbits_le32(DXNDLLCR(phy, datx8), DDRPHYC_DXNDLLCR_DLLSRST);
579
580
581 BIST_test(phy, datx8, &result);
582 success = result.test_result;
583
584
585
586
587
588 if (!success) {
589 log_debug("Fail at init condtion. Let's look for a good init condition.\n");
590 success = 0;
591
592
593
594
595
596
597 log_debug("increase Phase idx\n");
598 while (!success && (phase_idx <= MAX_DQS_PHASE_IDX)) {
599 DQS_phase_delay(phy, datx8, phase_idx);
600 BIST_test(phy, datx8, &result);
601 success = result.test_result;
602 phase_idx++;
603 }
604
605
606
607 if (success)
608 phase_idx--;
609 }
610 if (ctrlc()) {
611 sprintf(string, "interrupted at byte %d/%d, error=%d",
612 datx8 + 1, nb_bytes, error);
613 return TEST_FAILED;
614 }
615
616
617
618 if (!success) {
619
620
621
622
623 log_debug("Still fail. Try decrease DQS Unit delay\n");
624
625 phase_idx = 0;
626 dqs_unit_delay_index = 0;
627 DQS_phase_delay(phy, datx8, phase_idx);
628
629
630 while (!success &&
631 (dqs_unit_delay_index <=
632 MAX_DQS_UNIT_IDX)) {
633 DQS_unit_delay(phy, datx8,
634 dqs_unit_delay_index);
635 BIST_test(phy, datx8, &result);
636 success = result.test_result;
637 dqs_unit_delay_index++;
638 }
639 if (success) {
640
641 dqs_unit_delay_index--;
642
643 DQS_unit_delay(phy, datx8,
644 dqs_unit_delay_index);
645 } else {
646
647
648
649 force_stop = 1;
650 }
651 }
652
653
654
655
656
657 if (force_stop) {
658 printf("Result: Failed ");
659 printf("[Cannot Deskew lines, ");
660 printf("there is no PASS region]\n");
661 error++;
662 continue;
663 }
664 if (ctrlc()) {
665 sprintf(string, "interrupted at byte %d/%d, error=%d",
666 datx8 + 1, nb_bytes, error);
667 return TEST_FAILED;
668 }
669
670 log_debug("there is a pass region for phase idx %d\n",
671 phase_idx);
672 log_debug("Step1: Find the first failing condition\n");
673
674
675
676
677
678
679
680 while (success && (phase_idx <= MAX_DQS_PHASE_IDX)) {
681 DQS_phase_delay(phy, datx8, phase_idx);
682 BIST_test(phy, datx8, &result);
683 success = result.test_result;
684 phase_idx++;
685 }
686 if (ctrlc()) {
687 sprintf(string, "interrupted at byte %d/%d, error=%d",
688 datx8 + 1, nb_bytes, error);
689 return TEST_FAILED;
690 }
691
692
693
694
695
696 if (!success) {
697 log_debug("Fail region (PHASE) found phase idx %d\n",
698 phase_idx);
699 log_debug("Let's look for first success by DQS Unit steps\n");
700
701 phase_idx--;
702
703
704
705
706 while (!success && dqs_unit_delay_index >= 0) {
707 DQS_unit_delay(phy, datx8,
708 dqs_unit_delay_index);
709 BIST_test(phy, datx8, &result);
710 success = result.test_result;
711 dqs_unit_delay_index--;
712 }
713
714
715
716
717 if (success) {
718
719
720
721
722 last_right_ok.phase = phase_idx;
723
724 last_right_ok.unit = dqs_unit_delay_index + 1;
725 last_right_ok.bits_delay = 0xFFFFFFFF;
726 log_debug("Found %d\n", dqs_unit_delay_index);
727 } else {
728
729
730
731
732
733
734 last_right_ok.phase = phase_idx - 1;
735
736
737
738 last_right_ok.unit = 1;
739 last_right_ok.bits_delay = 0xFFFFFFFF;
740 log_debug("Not Found : try previous phase %d\n",
741 phase_idx - 1);
742
743 DQS_phase_delay(phy, datx8, phase_idx - 1);
744 dqs_unit_delay_index = 0;
745 success = true;
746 while (success &&
747 (dqs_unit_delay_index <
748 MAX_DQS_UNIT_IDX)) {
749 DQS_unit_delay(phy, datx8,
750 dqs_unit_delay_index);
751 BIST_test(phy, datx8, &result);
752 success = result.test_result;
753 dqs_unit_delay_index++;
754 log_debug("dqs_unit_delay_index = %d, result = %d\n",
755 dqs_unit_delay_index, success);
756 }
757
758 if (!success) {
759 last_right_ok.unit =
760 dqs_unit_delay_index - 1;
761 } else {
762 last_right_ok.unit = 0;
763 log_debug("ERROR: failed region not FOUND");
764 }
765 }
766 } else {
767
768
769
770
771
772
773
774
775
776
777 last_right_ok.phase = MAX_DQS_PHASE_IDX;
778 last_right_ok.unit = MAX_DQS_UNIT_IDX;
779 last_right_ok.bits_delay = 0xFFFFFFFF;
780 log_debug("Can't find the a fail condition\n");
781 }
782
783
784
785
786
787
788
789
790 printf("Byte %d, DQS unit = %d, phase = %d\n",
791 datx8, last_right_ok.unit, last_right_ok.phase);
792 log_debug("Step2, unit = %d, phase = %d, bits delay=%x\n",
793 last_right_ok.unit, last_right_ok.phase,
794 last_right_ok.bits_delay);
795
796
797 DQS_unit_delay(phy, datx8, last_right_ok.unit);
798 DQS_phase_delay(phy, datx8, last_right_ok.phase);
799 writel(last_right_ok.bits_delay, DXNDQTR(phy, datx8));
800
801
802
803
804
805
806
807
808
809 fail_found = 0;
810 for (bit_i = 0; bit_i < 8; bit_i++) {
811 if (ctrlc()) {
812 sprintf(string,
813 "interrupted at byte %d/%d, error=%d",
814 datx8 + 1, nb_bytes, error);
815 return error;
816 }
817 log_debug("deskewing bit %d:\n", bit_i);
818 success = 1;
819
820
821
822
823
824 writel(0xFFFFFFFF, DXNDQTR(phy, datx8));
825
826
827
828 bit_i_delay_index = 3;
829
830
831
832
833 while ((bit_i_delay_index >= 0) && success) {
834 set_DQ_unit_delay(phy, datx8,
835 bit_i,
836 bit_i_delay_index);
837 BIST_test(phy, datx8, &result);
838 success = result.test_result;
839 bit_i_delay_index--;
840 }
841
842
843
844
845 if (!success) {
846
847
848
849
850
851 deskew_delay[datx8][bit_i] =
852 bit_i_delay_index + 2;
853 if (deskew_delay[datx8][bit_i] > 3)
854 deskew_delay[datx8][bit_i] = 3;
855
856
857
858
859 fail_found = 1;
860 log_debug("Fail found on bit %d, for delay = %d => deskew[%d][%d] = %d\n",
861 bit_i, bit_i_delay_index + 1,
862 datx8, bit_i,
863 deskew_delay[datx8][bit_i]);
864 } else {
865
866
867
868
869
870 deskew_delay[datx8][bit_i] = 0;
871
872
873
874 deskew_non_converge[datx8][bit_i] = 1;
875 log_debug("Fail not found on bit %d => deskew[%d][%d] = %d\n",
876 bit_i, datx8, bit_i,
877 deskew_delay[datx8][bit_i]);
878 }
879 }
880 log_debug("**********byte %d tuning complete************\n",
881 datx8);
882
883
884
885 if (!fail_found) {
886 for (bit_i = 0; bit_i < 8; bit_i++)
887 deskew_delay[datx8][bit_i] = 0;
888 log_debug("The Deskew algorithm can't converge, there is too much margin in your design. Good job!\n");
889 }
890
891 apply_deskew_results(phy, datx8, deskew_delay,
892 deskew_non_converge);
893
894 DQS_phase_delay(phy, datx8, 3);
895 DQS_unit_delay(phy, datx8, 3);
896
897 clrbits_le32(DXNGCR(phy, datx8), DDRPHYC_DXNGCR_DXEN);
898 }
899
900
901 setbits_le32(&phy->dx0gcr, DDRPHYC_DXNGCR_DXEN);
902 setbits_le32(&phy->dx1gcr, DDRPHYC_DXNGCR_DXEN);
903 setbits_le32(&phy->dx2gcr, DDRPHYC_DXNGCR_DXEN);
904 setbits_le32(&phy->dx3gcr, DDRPHYC_DXNGCR_DXEN);
905
906 if (error) {
907 sprintf(string, "error = %d", error);
908 return TEST_FAILED;
909 }
910
911 return TEST_PASSED;
912}
913
914
915
916
917
918static enum test_result eye_training(struct stm32mp1_ddrctl *ctl,
919 struct stm32mp1_ddrphy *phy, char *string)
920{
921
922 u8 eye_training_val[NUM_BYTES][2];
923 u8 byte = 0;
924 struct BIST_result result;
925 s8 dqs_unit_delay_index = 0;
926 s8 phase_idx = 0;
927 s8 dqs_unit_delay_index_pass = 0;
928 s8 phase_idx_pass = 0;
929 u8 success = 0;
930 u8 left_phase_bound_found, right_phase_bound_found;
931 u8 left_unit_bound_found, right_unit_bound_found;
932 u8 left_bound_found, right_bound_found;
933 struct tuning_position left_bound, right_bound;
934 u8 error = 0;
935 u8 nb_bytes = get_nb_bytes(ctl);
936
937
938 clrbits_le32(&phy->pgcr, DDRPHYC_PGCR_DFTCMP);
939
940
941
942
943
944 clrbits_le32(&phy->pgcr, DDRPHYC_PGCR_PDDISDX);
945
946
947 clrbits_le32(&phy->dx0gcr, DDRPHYC_DXNGCR_DXEN);
948 clrbits_le32(&phy->dx1gcr, DDRPHYC_DXNGCR_DXEN);
949 clrbits_le32(&phy->dx2gcr, DDRPHYC_DXNGCR_DXEN);
950 clrbits_le32(&phy->dx3gcr, DDRPHYC_DXNGCR_DXEN);
951
952
953 config_BIST(ctl, phy);
954
955 for (byte = 0; byte < nb_bytes; byte++) {
956 if (ctrlc()) {
957 sprintf(string, "interrupted at byte %d/%d, error=%d",
958 byte + 1, nb_bytes, error);
959 return TEST_FAILED;
960 }
961 right_bound.phase = 0;
962 right_bound.unit = 0;
963
964 left_bound.phase = 0;
965 left_bound.unit = 0;
966
967 left_phase_bound_found = 0;
968 right_phase_bound_found = 0;
969
970 left_unit_bound_found = 0;
971 right_unit_bound_found = 0;
972
973 left_bound_found = 0;
974 right_bound_found = 0;
975
976
977 setbits_le32(DXNGCR(phy, byte), DDRPHYC_DXNGCR_DXEN);
978
979
980 BIST_datx8_sel(phy, byte);
981
982
983 phase_idx = _90deg;
984 phase_idx_pass = phase_idx;
985
986
987 dqs_unit_delay_index = 3;
988 dqs_unit_delay_index_pass = dqs_unit_delay_index;
989 success = 0;
990
991 log_debug("STEP0: Find Init delay\n");
992
993
994
995
996 DQS_unit_delay(phy, byte, dqs_unit_delay_index);
997 DQS_phase_delay(phy, byte, phase_idx);
998 BIST_test(phy, byte, &result);
999 success = result.test_result;
1000
1001 if (!success) {
1002
1003 while (phase_idx >= 0 && !success) {
1004 phase_idx--;
1005 DQS_phase_delay(phy, byte, phase_idx);
1006 BIST_test(phy, byte, &result);
1007 success = result.test_result;
1008 }
1009 }
1010 if (!success) {
1011
1012
1013
1014 phase_idx = _90deg;
1015 while (phase_idx <= MAX_DQS_PHASE_IDX &&
1016 !success) {
1017 phase_idx++;
1018 DQS_phase_delay(phy, byte,
1019 phase_idx);
1020 BIST_test(phy, byte, &result);
1021 success = result.test_result;
1022 }
1023 }
1024
1025 if (success) {
1026 phase_idx_pass = phase_idx;
1027 } else {
1028 printf("Result: Failed ");
1029 printf("[Cannot DQS timings, ");
1030 printf("there is no PASS region]\n");
1031 error++;
1032 continue;
1033 }
1034
1035 if (ctrlc()) {
1036 sprintf(string, "interrupted at byte %d/%d, error=%d",
1037 byte + 1, nb_bytes, error);
1038 return TEST_FAILED;
1039 }
1040 log_debug("STEP1: Find LEFT PHASE DQS Bound\n");
1041
1042 while ((phase_idx >= 0) &&
1043 (phase_idx <= MAX_DQS_PHASE_IDX) &&
1044 !left_phase_bound_found) {
1045 DQS_unit_delay(phy, byte,
1046 dqs_unit_delay_index);
1047 DQS_phase_delay(phy, byte,
1048 phase_idx);
1049 BIST_test(phy, byte, &result);
1050 success = result.test_result;
1051
1052
1053
1054
1055 if (!success) {
1056
1057 left_bound.phase = ++phase_idx;
1058 left_phase_bound_found = 1;
1059 } else if (success) {
1060 phase_idx--;
1061 }
1062 }
1063 if (!left_phase_bound_found) {
1064 left_bound.phase = 0;
1065 phase_idx = 0;
1066 }
1067
1068
1069 if (ctrlc()) {
1070 sprintf(string, "interrupted at byte %d/%d, error=%d",
1071 byte + 1, nb_bytes, error);
1072 return TEST_FAILED;
1073 }
1074 log_debug("STEP2: Find UNIT left bound\n");
1075
1076 while ((dqs_unit_delay_index >= 0) &&
1077 !left_unit_bound_found) {
1078 DQS_unit_delay(phy, byte,
1079 dqs_unit_delay_index);
1080 DQS_phase_delay(phy, byte, phase_idx);
1081 BIST_test(phy, byte, &result);
1082 success = result.test_result;
1083 if (!success) {
1084 left_bound.unit =
1085 ++dqs_unit_delay_index;
1086 left_unit_bound_found = 1;
1087 left_bound_found = 1;
1088 } else if (success) {
1089 dqs_unit_delay_index--;
1090 }
1091 }
1092
1093
1094 if (!left_unit_bound_found)
1095 left_bound.unit = 0;
1096
1097 if (ctrlc()) {
1098 sprintf(string, "interrupted at byte %d/%d, error=%d",
1099 byte + 1, nb_bytes, error);
1100 return TEST_FAILED;
1101 }
1102 log_debug("STEP3: Find PHase right bound\n");
1103
1104
1105
1106
1107
1108 phase_idx = phase_idx_pass;
1109
1110
1111 dqs_unit_delay_index = dqs_unit_delay_index_pass;
1112
1113 while ((phase_idx <= MAX_DQS_PHASE_IDX) &&
1114 !right_phase_bound_found) {
1115 DQS_unit_delay(phy, byte,
1116 dqs_unit_delay_index);
1117 DQS_phase_delay(phy, byte, phase_idx);
1118 BIST_test(phy, byte, &result);
1119 success = result.test_result;
1120 if (!success) {
1121
1122 right_bound.phase = --phase_idx;
1123 right_phase_bound_found = 1;
1124 } else if (success) {
1125 phase_idx++;
1126 }
1127 }
1128
1129
1130 if (!right_phase_bound_found) {
1131 right_bound.phase = MAX_DQS_PHASE_IDX;
1132 phase_idx = MAX_DQS_PHASE_IDX;
1133 }
1134
1135 if (ctrlc()) {
1136 sprintf(string, "interrupted at byte %d/%d, error=%d",
1137 byte + 1, nb_bytes, error);
1138 return TEST_FAILED;
1139 }
1140 log_debug("STEP4: Find UNIT right bound\n");
1141
1142 while ((dqs_unit_delay_index <= MAX_DQS_UNIT_IDX) &&
1143 !right_unit_bound_found) {
1144 DQS_unit_delay(phy, byte,
1145 dqs_unit_delay_index);
1146 DQS_phase_delay(phy, byte, phase_idx);
1147 BIST_test(phy, byte, &result);
1148 success = result.test_result;
1149 if (!success) {
1150 right_bound.unit =
1151 --dqs_unit_delay_index;
1152 right_unit_bound_found = 1;
1153 right_bound_found = 1;
1154 } else if (success) {
1155 dqs_unit_delay_index++;
1156 }
1157 }
1158
1159 if (!right_unit_bound_found)
1160 right_bound.unit = MAX_DQS_UNIT_IDX;
1161
1162
1163
1164
1165
1166
1167 if (left_bound_found || right_bound_found) {
1168 eye_training_val[byte][0] = (right_bound.phase +
1169 left_bound.phase) / 2;
1170 eye_training_val[byte][1] = (right_bound.unit +
1171 left_bound.unit) / 2;
1172
1173
1174
1175
1176 if (((right_bound.phase + left_bound.phase) % 2 == 1) &&
1177 eye_training_val[byte][1] != MAX_DQS_UNIT_IDX)
1178 eye_training_val[byte][1]++;
1179 log_debug("** found phase : %d - %d & unit %d - %d\n",
1180 right_bound.phase, left_bound.phase,
1181 right_bound.unit, left_bound.unit);
1182 log_debug("** calculating mid region: phase: %d unit: %d (nominal is 3)\n",
1183 eye_training_val[byte][0],
1184 eye_training_val[byte][1]);
1185 } else {
1186
1187
1188
1189 eye_training_val[byte][0] = 3;
1190 eye_training_val[byte][1] = 3;
1191 }
1192 DQS_phase_delay(phy, byte, eye_training_val[byte][0]);
1193 DQS_unit_delay(phy, byte, eye_training_val[byte][1]);
1194
1195 printf("Byte %d, DQS unit = %d, phase = %d\n",
1196 byte,
1197 eye_training_val[byte][1],
1198 eye_training_val[byte][0]);
1199 }
1200
1201 if (error) {
1202 sprintf(string, "error = %d", error);
1203 return TEST_FAILED;
1204 }
1205
1206 return TEST_PASSED;
1207}
1208
1209static void display_reg_results(struct stm32mp1_ddrphy *phy, u8 byte)
1210{
1211 u8 i = 0;
1212
1213 printf("Byte %d Dekew result, bit0 delay, bit1 delay...bit8 delay\n ",
1214 byte);
1215
1216 for (i = 0; i < 8; i++)
1217 printf("%d ", DQ_unit_index(phy, byte, i));
1218 printf("\n");
1219
1220 printf("dxndllcr: [%08x] val:%08x\n",
1221 DXNDLLCR(phy, byte),
1222 readl(DXNDLLCR(phy, byte)));
1223 printf("dxnqdstr: [%08x] val:%08x\n",
1224 DXNDQSTR(phy, byte),
1225 readl(DXNDQSTR(phy, byte)));
1226 printf("dxndqtr: [%08x] val:%08x\n",
1227 DXNDQTR(phy, byte),
1228 readl(DXNDQTR(phy, byte)));
1229}
1230
1231
1232static u8 set_midpoint_read_dqs_gating(struct stm32mp1_ddrphy *phy, u8 byte,
1233 u8 dqs_gating[NUM_BYTES]
1234 [MAX_GSL_IDX + 1]
1235 [MAX_GPS_IDX + 1])
1236{
1237
1238 u8 dqs_gate_values[NUM_BYTES][2];
1239 u8 gsl_idx, gps_idx = 0;
1240 u8 left_bound_idx[2] = {0, 0};
1241 u8 right_bound_idx[2] = {0, 0};
1242 u8 left_bound_found = 0;
1243 u8 right_bound_found = 0;
1244 u8 intermittent = 0;
1245 u8 value;
1246
1247 for (gsl_idx = 0; gsl_idx <= MAX_GSL_IDX; gsl_idx++) {
1248 for (gps_idx = 0; gps_idx <= MAX_GPS_IDX; gps_idx++) {
1249 value = dqs_gating[byte][gsl_idx][gps_idx];
1250 if (value == 1 && left_bound_found == 0) {
1251 left_bound_idx[0] = gsl_idx;
1252 left_bound_idx[1] = gps_idx;
1253 left_bound_found = 1;
1254 } else if (value == 0 &&
1255 left_bound_found == 1 &&
1256 !right_bound_found) {
1257 if (gps_idx == 0) {
1258 right_bound_idx[0] = gsl_idx - 1;
1259 right_bound_idx[1] = MAX_GPS_IDX;
1260 } else {
1261 right_bound_idx[0] = gsl_idx;
1262 right_bound_idx[1] = gps_idx - 1;
1263 }
1264 right_bound_found = 1;
1265 } else if (value == 1 &&
1266 right_bound_found == 1) {
1267 intermittent = 1;
1268 }
1269 }
1270 }
1271
1272
1273 if (left_bound_idx[0] == 0 && left_bound_idx[1] == 0 &&
1274 right_bound_idx[0] == 0 && right_bound_idx[1] == 0)
1275 intermittent = 1;
1276
1277
1278
1279
1280 if (!intermittent) {
1281
1282
1283
1284 if (left_bound_found || right_bound_found) {
1285 log_debug("idx0(%d): %d %d idx1(%d) : %d %d\n",
1286 left_bound_found,
1287 right_bound_idx[0], left_bound_idx[0],
1288 right_bound_found,
1289 right_bound_idx[1], left_bound_idx[1]);
1290 dqs_gate_values[byte][0] =
1291 (right_bound_idx[0] + left_bound_idx[0]) / 2;
1292 dqs_gate_values[byte][1] =
1293 (right_bound_idx[1] + left_bound_idx[1]) / 2;
1294
1295
1296
1297 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]++;
1301
1302 else if (((right_bound_idx[0] +
1303 left_bound_idx[0]) % 2 == 1) &&
1304 dqs_gate_values[byte][1] == MAX_GPS_IDX) {
1305 dqs_gate_values[byte][1] = 0;
1306 dqs_gate_values[byte][0]++;
1307 }
1308
1309
1310
1311 if (((right_bound_idx[0] - left_bound_idx[0]) == 1)) {
1312 dqs_gate_values[byte][1] = (left_bound_idx[1] +
1313 right_bound_idx[1] +
1314 4) / 2;
1315 if (dqs_gate_values[byte][1] >= 4) {
1316 dqs_gate_values[byte][0] =
1317 right_bound_idx[0];
1318 dqs_gate_values[byte][1] -= 4;
1319 } else {
1320 dqs_gate_values[byte][0] =
1321 left_bound_idx[0];
1322 }
1323 }
1324 log_debug("*******calculating mid region: system latency: %d phase: %d********\n",
1325 dqs_gate_values[byte][0],
1326 dqs_gate_values[byte][1]);
1327 log_debug("*******the nominal values were system latency: 0 phase: 2*******\n");
1328 }
1329 } else {
1330
1331 log_debug("dqs gating:no regular fail/pass/fail found. defaults values restored.\n");
1332 dqs_gate_values[byte][0] = 0;
1333 dqs_gate_values[byte][1] = 2;
1334 }
1335 set_r0dgsl_delay(phy, byte, dqs_gate_values[byte][0]);
1336 set_r0dgps_delay(phy, byte, dqs_gate_values[byte][1]);
1337 printf("Byte %d, R0DGSL = %d, R0DGPS = %d\n",
1338 byte, dqs_gate_values[byte][0], dqs_gate_values[byte][1]);
1339
1340
1341
1342
1343 return !(intermittent || (left_bound_found && right_bound_found));
1344}
1345
1346static enum test_result read_dqs_gating(struct stm32mp1_ddrctl *ctl,
1347 struct stm32mp1_ddrphy *phy,
1348 char *string)
1349{
1350
1351 u8 dqs_gating[NUM_BYTES][MAX_GSL_IDX + 1][MAX_GPS_IDX + 1];
1352 u8 byte, gsl_idx, gps_idx = 0;
1353 struct BIST_result result;
1354 u8 success = 0;
1355 u8 nb_bytes = get_nb_bytes(ctl);
1356
1357 memset(dqs_gating, 0x0, sizeof(dqs_gating));
1358
1359
1360 clrbits_le32(&phy->pgcr, DDRPHYC_PGCR_DFTCMP);
1361
1362
1363
1364
1365
1366 clrbits_le32(&phy->pgcr, DDRPHYC_PGCR_PDDISDX);
1367
1368
1369 clrbits_le32(&phy->dx0gcr, DDRPHYC_DXNGCR_DXEN);
1370 clrbits_le32(&phy->dx1gcr, DDRPHYC_DXNGCR_DXEN);
1371 clrbits_le32(&phy->dx2gcr, DDRPHYC_DXNGCR_DXEN);
1372 clrbits_le32(&phy->dx3gcr, DDRPHYC_DXNGCR_DXEN);
1373
1374
1375 config_BIST(ctl, phy);
1376
1377 for (byte = 0; byte < nb_bytes; byte++) {
1378 if (ctrlc()) {
1379 sprintf(string, "interrupted at byte %d/%d",
1380 byte + 1, nb_bytes);
1381 return TEST_FAILED;
1382 }
1383
1384 setbits_le32(DXNGCR(phy, byte), DDRPHYC_DXNGCR_DXEN);
1385
1386
1387 BIST_datx8_sel(phy, byte);
1388 for (gsl_idx = 0; gsl_idx <= MAX_GSL_IDX; gsl_idx++) {
1389 for (gps_idx = 0; gps_idx <= MAX_GPS_IDX; gps_idx++) {
1390 if (ctrlc()) {
1391 sprintf(string,
1392 "interrupted at byte %d/%d",
1393 byte + 1, nb_bytes);
1394 return TEST_FAILED;
1395 }
1396
1397 set_r0dgsl_delay(phy, byte, gsl_idx);
1398 set_r0dgps_delay(phy, byte, gps_idx);
1399
1400 BIST_test(phy, byte, &result);
1401 success = result.test_result;
1402 if (success)
1403 dqs_gating[byte][gsl_idx][gps_idx] = 1;
1404 itm_soft_reset(phy);
1405 }
1406 }
1407 set_midpoint_read_dqs_gating(phy, byte, dqs_gating);
1408
1409 readl(0xc0000000);
1410 readl(0xc0000000);
1411 }
1412
1413
1414
1415 return TEST_PASSED;
1416}
1417
1418
1419
1420
1421
1422static enum test_result do_read_dqs_gating(struct stm32mp1_ddrctl *ctl,
1423 struct stm32mp1_ddrphy *phy,
1424 char *string, int argc,
1425 char *argv[])
1426{
1427 u32 rfshctl3 = readl(&ctl->rfshctl3);
1428 u32 pwrctl = readl(&ctl->pwrctl);
1429 u32 derateen = readl(&ctl->derateen);
1430 enum test_result res;
1431
1432 writel(0x0, &ctl->derateen);
1433 stm32mp1_refresh_disable(ctl);
1434
1435 res = read_dqs_gating(ctl, phy, string);
1436
1437 stm32mp1_refresh_restore(ctl, rfshctl3, pwrctl);
1438 writel(derateen, &ctl->derateen);
1439
1440 return res;
1441}
1442
1443static enum test_result do_bit_deskew(struct stm32mp1_ddrctl *ctl,
1444 struct stm32mp1_ddrphy *phy,
1445 char *string, int argc, char *argv[])
1446{
1447 u32 rfshctl3 = readl(&ctl->rfshctl3);
1448 u32 pwrctl = readl(&ctl->pwrctl);
1449 u32 derateen = readl(&ctl->derateen);
1450 enum test_result res;
1451
1452 writel(0x0, &ctl->derateen);
1453 stm32mp1_refresh_disable(ctl);
1454
1455 res = bit_deskew(ctl, phy, string);
1456
1457 stm32mp1_refresh_restore(ctl, rfshctl3, pwrctl);
1458 writel(derateen, &ctl->derateen);
1459
1460 return res;
1461}
1462
1463static enum test_result do_eye_training(struct stm32mp1_ddrctl *ctl,
1464 struct stm32mp1_ddrphy *phy,
1465 char *string, int argc, char *argv[])
1466{
1467 u32 rfshctl3 = readl(&ctl->rfshctl3);
1468 u32 pwrctl = readl(&ctl->pwrctl);
1469 u32 derateen = readl(&ctl->derateen);
1470 enum test_result res;
1471
1472 writel(0x0, &ctl->derateen);
1473 stm32mp1_refresh_disable(ctl);
1474
1475 res = eye_training(ctl, phy, string);
1476
1477 stm32mp1_refresh_restore(ctl, rfshctl3, pwrctl);
1478 writel(derateen, &ctl->derateen);
1479
1480 return res;
1481}
1482
1483static enum test_result do_display(struct stm32mp1_ddrctl *ctl,
1484 struct stm32mp1_ddrphy *phy,
1485 char *string, int argc, char *argv[])
1486{
1487 int byte;
1488 u8 nb_bytes = get_nb_bytes(ctl);
1489
1490 for (byte = 0; byte < nb_bytes; byte++)
1491 display_reg_results(phy, byte);
1492
1493 return TEST_PASSED;
1494}
1495
1496static enum test_result do_bist_config(struct stm32mp1_ddrctl *ctl,
1497 struct stm32mp1_ddrphy *phy,
1498 char *string, int argc, char *argv[])
1499{
1500 unsigned long value;
1501
1502 if (argc > 0) {
1503 if (strict_strtoul(argv[0], 0, &value) < 0) {
1504 sprintf(string, "invalid nbErr %s", argv[0]);
1505 return TEST_FAILED;
1506 }
1507 BIST_error_max = value;
1508 }
1509 if (argc > 1) {
1510 if (strict_strtoul(argv[1], 0, &value) < 0) {
1511 sprintf(string, "invalid Seed %s", argv[1]);
1512 return TEST_FAILED;
1513 }
1514 BIST_seed = value;
1515 }
1516 printf("Bist.nbErr = %d\n", BIST_error_max);
1517 if (BIST_seed)
1518 printf("Bist.Seed = 0x%x\n", BIST_seed);
1519 else
1520 printf("Bist.Seed = random\n");
1521
1522 return TEST_PASSED;
1523}
1524
1525
1526
1527
1528
1529
1530const struct test_desc tuning[] = {
1531 {do_read_dqs_gating, "Read DQS gating",
1532 "software read DQS Gating", "", 0 },
1533 {do_bit_deskew, "Bit de-skew", "", "", 0 },
1534 {do_eye_training, "Eye Training", "or DQS training", "", 0 },
1535 {do_display, "Display registers", "", "", 0 },
1536 {do_bist_config, "Bist config", "[nbErr] [seed]",
1537 "configure Bist test", 2},
1538};
1539
1540const int tuning_nb = ARRAY_SIZE(tuning);
1541