Loading...
   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
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
/*
 * Copyright (C) 2019, STMicroelectronics - All Rights Reserved
 */

#define LOG_CATEGORY UCLASS_RAM

#include <console.h>
#include <init.h>
#include <log.h>
#include <rand.h>
#include <watchdog.h>
#include <asm/io.h>
#include <linux/log2.h>
#include "stm32mp1_tests.h"

#define ADDR_INVALID	0xFFFFFFFF

#define PATTERN_DEFAULT	"-"

static int get_bufsize(char *string, int argc, char *argv[], int arg_nb,
		       size_t *bufsize, size_t default_size, size_t min_size)
{
	unsigned long value;

	if (argc > arg_nb) {
		if (strict_strtoul(argv[arg_nb], 0, &value) < 0) {
			sprintf(string, "invalid %d parameter %s",
				arg_nb, argv[arg_nb]);
			return -1;
		}
		if (value > STM32_DDR_SIZE || value < min_size) {
			sprintf(string, "invalid size %s (min=%d)",
				argv[arg_nb], min_size);
			return -1;
		}
		if (value & (min_size - 1)) {
			sprintf(string, "unaligned size %s (min=%d)",
				argv[arg_nb], min_size);
			return -1;
		}
		*bufsize = value;
	} else {
		if (default_size != STM32_DDR_SIZE)
			*bufsize = default_size;
		else
			*bufsize = get_ram_size((long *)STM32_DDR_BASE,
						STM32_DDR_SIZE);
	}
	return 0;
}

static int get_nb_loop(char *string, int argc, char *argv[], int arg_nb,
		       u32 *nb_loop, u32 default_nb_loop)
{
	unsigned long value;

	if (argc > arg_nb) {
		if (strict_strtoul(argv[arg_nb], 0, &value) < 0) {
			sprintf(string, "invalid %d parameter %s",
				arg_nb, argv[arg_nb]);
			return -1;
		}
		if (value == 0)
			printf("WARNING: infinite loop requested\n");
		*nb_loop = value;
	} else {
		*nb_loop = default_nb_loop;
	}

	return 0;
}

static int get_addr(char *string, int argc, char *argv[], int arg_nb,
		    u32 *addr)
{
	unsigned long value;

	if (argc > arg_nb) {
		if (strict_strtoul(argv[arg_nb], 16, &value) < 0) {
			sprintf(string, "invalid %d parameter %s",
				arg_nb, argv[arg_nb]);
			return -1;
		}
		if (value < STM32_DDR_BASE) {
			sprintf(string, "too low address %s", argv[arg_nb]);
			return -1;
		}
		if (value & 0x3 && value != ADDR_INVALID) {
			sprintf(string, "unaligned address %s",
				argv[arg_nb]);
			return -1;
		}
		*addr = value;
	} else {
		*addr = STM32_DDR_BASE;
	}

	return 0;
}

static int get_pattern(char *string, int argc, char *argv[], int arg_nb,
		       u32 *pattern, u32 default_pattern)
{
	unsigned long value;

	if (argc > arg_nb) {
		if (!strcmp(argv[arg_nb], PATTERN_DEFAULT)) {
			*pattern = default_pattern;
			return 0;
		}
		if (strict_strtoul(argv[arg_nb], 16, &value) < 0) {
			sprintf(string, "invalid %d parameter %s",
				arg_nb, argv[arg_nb]);
			return -1;
		}
		*pattern = value;
	} else {
		*pattern = default_pattern;
	}

	return 0;
}

static u32 check_addr(u32 addr, u32 value)
{
	u32 data = readl(addr);

	if (value !=  data) {
		printf("0x%08x: 0x%08x <=> 0x%08x", addr, data, value);
		data = readl(addr);
		printf("(2nd read: 0x%08x)", data);
		if (value == data)
			printf("- read error");
		else
			printf("- write error");
		printf("\n");
		return -1;
	}
	return 0;
}

static int progress(u32 offset)
{
	if (!(offset & 0xFFFFFF)) {
		putc('.');
		if (ctrlc()) {
			printf("\ntest interrupted!\n");
			return 1;
		}
	}
	return 0;
}

static int test_loop_end(u32 *loop, u32 nb_loop, u32 progress)
{
	(*loop)++;
	if (nb_loop && *loop >= nb_loop)
		return 1;
	if ((*loop) % progress)
		return 0;
	/* allow to interrupt the test only for progress step */
	if (ctrlc()) {
		printf("test interrupted!\n");
		return 1;
	}
	printf("loop #%d\n", *loop);
	schedule();

	return 0;
}

/**********************************************************************
 *
 * Function:    memTestDataBus()
 *
 * Description: Test the data bus wiring in a memory region by
 *              performing a walking 1's test at a fixed address
 *              within that region.  The address is selected
 *              by the caller.
 *
 * Notes:
 *
 * Returns:     0 if the test succeeds.
 *              A non-zero result is the first pattern that failed.
 *
 **********************************************************************/
static u32 databus(u32 *address)
{
	u32 pattern;
	u32 read_value;

	/* Perform a walking 1's test at the given address. */
	for (pattern = 1; pattern != 0; pattern <<= 1) {
		/* Write the test pattern. */
		writel(pattern, address);

		/* Read it back (immediately is okay for this test). */
		read_value = readl(address);
		log_debug("%x: %x <=> %x\n",
			  (u32)address, read_value, pattern);

		if (read_value != pattern)
			return pattern;
	}

	return 0;
}

/**********************************************************************
 *
 * Function:    memTestAddressBus()
 *
 * Description: Test the address bus wiring in a memory region by
 *              performing a walking 1's test on the relevant bits
 *              of the address and checking for aliasing. This test
 *              will find single-bit address failures such as stuck
 *              -high, stuck-low, and shorted pins.  The base address
 *              and size of the region are selected by the caller.
 *
 * Notes:       For best results, the selected base address should
 *              have enough LSB 0's to guarantee single address bit
 *              changes.  For example, to test a 64-Kbyte region,
 *              select a base address on a 64-Kbyte boundary.  Also,
 *              select the region size as a power-of-two--if at all
 *              possible.
 *
 * Returns:     NULL if the test succeeds.
 *              A non-zero result is the first address at which an
 *              aliasing problem was uncovered.  By examining the
 *              contents of memory, it may be possible to gather
 *              additional information about the problem.
 *
 **********************************************************************/
static u32 *addressbus(u32 *address, u32 nb_bytes)
{
	u32 mask = (nb_bytes / sizeof(u32) - 1);
	u32 offset;
	u32 test_offset;
	u32 read_value;

	u32 pattern     = 0xAAAAAAAA;
	u32 antipattern = 0x55555555;

	/* Write the default pattern at each of the power-of-two offsets. */
	for (offset = 1; (offset & mask) != 0; offset <<= 1)
		writel(pattern, &address[offset]);

	/* Check for address bits stuck high. */
	test_offset = 0;
	writel(antipattern, &address[test_offset]);

	for (offset = 1; (offset & mask) != 0; offset <<= 1) {
		read_value = readl(&address[offset]);
		log_debug("%x: %x <=> %x\n",
			  (u32)&address[offset], read_value, pattern);
		if (read_value != pattern)
			return &address[offset];
	}

	writel(pattern, &address[test_offset]);

	/* Check for address bits stuck low or shorted. */
	for (test_offset = 1; (test_offset & mask) != 0; test_offset <<= 1) {
		writel(antipattern, &address[test_offset]);
		if (readl(&address[0]) != pattern)
			return &address[test_offset];

		for (offset = 1; (offset & mask) != 0; offset <<= 1) {
			if (readl(&address[offset]) != pattern &&
			    offset != test_offset)
				return &address[test_offset];
		}
		writel(pattern, &address[test_offset]);
	}

	return NULL;
}

/**********************************************************************
 *
 * Function:    memTestDevice()
 *
 * Description: Test the integrity of a physical memory device by
 *              performing an increment/decrement test over the
 *              entire region.  In the process every storage bit
 *              in the device is tested as a zero and a one.  The
 *              base address and the size of the region are
 *              selected by the caller.
 *
 * Notes:
 *
 * Returns:     NULL if the test succeeds.
 *
 *              A non-zero result is the first address at which an
 *              incorrect value was read back.  By examining the
 *              contents of memory, it may be possible to gather
 *              additional information about the problem.
 *
 **********************************************************************/
static u32 *memdevice(u32 *address, u32 nb_bytes)
{
	u32 offset;
	u32 nb_words = nb_bytes / sizeof(u32);

	u32 pattern;
	u32 antipattern;

	puts("Fill with pattern");
	/* Fill memory with a known pattern. */
	for (pattern = 1, offset = 0; offset < nb_words; pattern++, offset++) {
		writel(pattern, &address[offset]);
		if (progress(offset))
			return NULL;
	}

	puts("\nCheck and invert pattern");
	/* Check each location and invert it for the second pass. */
	for (pattern = 1, offset = 0; offset < nb_words; pattern++, offset++) {
		if (readl(&address[offset]) != pattern)
			return &address[offset];

		antipattern = ~pattern;
		writel(antipattern, &address[offset]);
		if (progress(offset))
			return NULL;
	}

	puts("\nCheck inverted pattern");
	/* Check each location for the inverted pattern and zero it. */
	for (pattern = 1, offset = 0; offset < nb_words; pattern++, offset++) {
		antipattern = ~pattern;
		if (readl(&address[offset]) != antipattern)
			return &address[offset];
		if (progress(offset))
			return NULL;
	}
	printf("\n");

	return NULL;
}

static enum test_result databuswalk0(struct stm32mp1_ddrctl *ctl,
				     struct stm32mp1_ddrphy *phy,
				     char *string, int argc, char *argv[])
{
	int i;
	u32 loop = 0, nb_loop;
	u32 addr;
	u32 error = 0;
	u32 data;

	if (get_nb_loop(string, argc, argv, 0, &nb_loop, 100))
		return TEST_ERROR;
	if (get_addr(string, argc, argv, 1, &addr))
		return TEST_ERROR;

	printf("running %d loops at 0x%x\n", nb_loop, addr);
	while (!error) {
		for (i = 0; i < 32; i++)
			writel(~(1 << i), addr + 4 * i);
		for (i = 0; i < 32; i++) {
			data = readl(addr + 4 * i);
			if (~(1 << i) !=  data) {
				error |= 1 << i;
				log_debug("%x: error %x expected %x => error:%x\n",
					  addr + 4 * i, data, ~(1 << i), error);
			}
		}
		if (test_loop_end(&loop, nb_loop, 1000))
			break;
		for (i = 0; i < 32; i++)
			writel(0, addr + 4 * i);
	}
	if (error) {
		sprintf(string, "loop %d: error for bits 0x%x",
			loop, error);
		return TEST_FAILED;
	}
	sprintf(string, "no error for %d loops", loop);
	return TEST_PASSED;
}

static enum test_result databuswalk1(struct stm32mp1_ddrctl *ctl,
				     struct stm32mp1_ddrphy *phy,
				     char *string, int argc, char *argv[])
{
	int i;
	u32 loop = 0, nb_loop;
	u32 addr;
	u32 error = 0;
	u32 data;

	if (get_nb_loop(string, argc, argv, 0, &nb_loop, 100))
		return TEST_ERROR;
	if (get_addr(string, argc, argv, 1, &addr))
		return TEST_ERROR;
	printf("running %d loops at 0x%x\n", nb_loop, addr);
	while (!error) {
		for (i = 0; i < 32; i++)
			writel(1 << i, addr + 4 * i);
		for (i = 0; i < 32; i++) {
			data = readl(addr + 4 * i);
			if ((1 << i) !=  data) {
				error |= 1 << i;
				log_debug("%x: error %x expected %x => error:%x\n",
					  addr + 4 * i, data, (1 << i), error);
			}
		}
		if (test_loop_end(&loop, nb_loop, 1000))
			break;
		for (i = 0; i < 32; i++)
			writel(0, addr + 4 * i);
	}
	if (error) {
		sprintf(string, "loop %d: error for bits 0x%x",
			loop, error);
		return TEST_FAILED;
	}
	sprintf(string, "no error for %d loops", loop);
	return TEST_PASSED;
}

static enum test_result test_databus(struct stm32mp1_ddrctl *ctl,
				     struct stm32mp1_ddrphy *phy,
				     char *string, int argc, char *argv[])
{
	u32 addr;
	u32 error;

	if (get_addr(string, argc, argv, 0, &addr))
		return TEST_ERROR;
	error = databus((u32 *)addr);
	if (error) {
		sprintf(string, "0x%x: error for bits 0x%x",
			addr, error);
		return TEST_FAILED;
	}
	sprintf(string, "address 0x%x", addr);
	return TEST_PASSED;
}

static enum test_result test_addressbus(struct stm32mp1_ddrctl *ctl,
					struct stm32mp1_ddrphy *phy,
					char *string, int argc, char *argv[])
{
	u32 addr;
	u32 bufsize;
	u32 error;

	if (get_bufsize(string, argc, argv, 0, &bufsize, STM32_DDR_SIZE, 4))
		return TEST_ERROR;
	if (!is_power_of_2(bufsize)) {
		sprintf(string, "size 0x%x is not a power of 2",
			(u32)bufsize);
		return TEST_ERROR;
	}
	if (get_addr(string, argc, argv, 1, &addr))
		return TEST_ERROR;

	printf("running at 0x%08x length 0x%x\n", addr, bufsize);
	error = (u32)addressbus((u32 *)addr, bufsize);
	if (error) {
		sprintf(string, "0x%x: error for address 0x%x",
			addr, error);
		return TEST_FAILED;
	}
	sprintf(string, "address 0x%x, size 0x%x",
		addr, bufsize);
	return TEST_PASSED;
}

static enum test_result test_memdevice(struct stm32mp1_ddrctl *ctl,
				       struct stm32mp1_ddrphy *phy,
				       char *string, int argc, char *argv[])
{
	u32 addr;
	size_t bufsize;
	u32 error;

	if (get_bufsize(string, argc, argv, 0, &bufsize, 4 * 1024, 4))
		return TEST_ERROR;
	if (get_addr(string, argc, argv, 1, &addr))
		return TEST_ERROR;
	error = (u32)memdevice((u32 *)addr, (unsigned long)bufsize);
	if (error) {
		sprintf(string, "0x%x: error for address 0x%x",
			addr, error);
		return TEST_FAILED;
	}
	sprintf(string, "address 0x%x, size 0x%x",
		addr, bufsize);
	return TEST_PASSED;
}

/**********************************************************************
 *
 * Function:    sso
 *
 * Description: Test the Simultaneous Switching Output.
 *              Verifies succes sive reads and writes to the same memory word,
 *              holding one bit constant while toggling all other data bits
 *              simultaneously
 *              => stress the data bus over an address range
 *
 *              The CPU writes to each address in the given range.
 *              For each bit, first the CPU holds the bit at 1 while
 *              toggling the other bits, and then the CPU holds the bit at 0
 *              while toggling the other bits.
 *              After each write, the CPU reads the address that was written
 *              to verify that it contains the correct data
 *
 **********************************************************************/
static enum test_result test_sso(struct stm32mp1_ddrctl *ctl,
				 struct stm32mp1_ddrphy *phy,
				 char *string, int argc, char *argv[])
{
	int i, j;
	u32 addr, bufsize, remaining, offset;
	u32 error = 0;
	u32 data;

	if (get_bufsize(string, argc, argv, 0, &bufsize, 4 * 1024, 4))
		return TEST_ERROR;
	if (get_addr(string, argc, argv, 1, &addr))
		return TEST_ERROR;

	printf("running sso at 0x%x length 0x%x", addr, bufsize);
	offset = addr;
	remaining = bufsize;
	while (remaining) {
		for (i = 0; i < 32; i++) {
			/* write pattern. */
			for (j = 0; j < 6; j++) {
				switch (j) {
				case 0:
				case 2:
					data = 1 << i;
					break;
				case 3:
				case 5:
					data = ~(1 << i);
					break;
				case 1:
					data = ~0x0;
					break;
				case 4:
					data = 0x0;
					break;
				}

				writel(data, offset);
				error = check_addr(offset, data);
				if (error)
					goto end;
			}
		}
		offset += 4;
		remaining -= 4;
		if (progress(offset << 7))
			goto end;
	}
	puts("\n");

end:
	if (error) {
		sprintf(string, "error for pattern 0x%x @0x%x",
			data, offset);
		return TEST_FAILED;
	}
	sprintf(string, "no error for sso at 0x%x length 0x%x", addr, bufsize);
	return TEST_PASSED;
}

/**********************************************************************
 *
 * Function:    Random
 *
 * Description: Verifies r/w with pseudo-ramdom value on one region
 *              + write the region (individual access)
 *              + memcopy to the 2nd region (try to use burst)
 *              + verify the 2 regions
 *
 **********************************************************************/
static enum test_result test_random(struct stm32mp1_ddrctl *ctl,
				    struct stm32mp1_ddrphy *phy,
				    char *string, int argc, char *argv[])
{
	u32 addr, offset, value = 0;
	size_t bufsize;
	u32 loop = 0, nb_loop;
	u32 error = 0;
	unsigned int seed;

	if (get_bufsize(string, argc, argv, 0, &bufsize, 4 * 1024, 8))
		return TEST_ERROR;
	if (get_nb_loop(string, argc, argv, 1, &nb_loop, 1))
		return TEST_ERROR;
	if (get_addr(string, argc, argv, 2, &addr))
		return TEST_ERROR;

	bufsize /= 2;
	printf("running %d loops copy from 0x%x to 0x%x (buffer size=0x%x)\n",
	       nb_loop, addr, addr + bufsize, bufsize);
	while (!error) {
		seed = rand();
		for (offset = 0; offset < bufsize; offset += 4)
			writel(rand(), addr + offset);

		memcpy((void *)addr + bufsize, (void *)addr, bufsize);

		srand(seed);
		for (offset = 0; offset < 2 * bufsize; offset += 4) {
			if (offset == bufsize)
				srand(seed);
			value = rand();
			error = check_addr(addr + offset, value);
			if (error)
				break;
			if (progress(offset))
				return TEST_FAILED;
		}
		if (test_loop_end(&loop, nb_loop, 100))
			break;
	}
	putc('\n');

	if (error) {
		sprintf(string,
			"loop %d: error for address 0x%x: 0x%x expected 0x%x",
			loop, offset, readl(offset), value);
		return TEST_FAILED;
	}
	sprintf(string, "no error for %d loops, size 0x%x",
		loop, bufsize);
	return TEST_PASSED;
}

/**********************************************************************
 *
 * Function:    noise
 *
 * Description: Verifies r/w while forcing switching of all data bus lines.
 *              optimised 4 iteration write/read/write/read cycles...
 *              for pattern and inversed pattern
 *
 **********************************************************************/
void do_noise(u32 addr, u32 pattern, u32 *result)
{
	__asm__("push {R0-R11}");
	__asm__("mov r0, %0" : : "r" (addr));
	__asm__("mov r1, %0" : : "r" (pattern));
	__asm__("mov r11, %0" : : "r" (result));

	__asm__("mvn r2, r1");

	__asm__("str r1, [r0]");
	__asm__("ldr r3, [r0]");
	__asm__("str r2, [r0]");
	__asm__("ldr r4, [r0]");

	__asm__("str r1, [r0]");
	__asm__("ldr r5, [r0]");
	__asm__("str r2, [r0]");
	__asm__("ldr r6, [r0]");

	__asm__("str r1, [r0]");
	__asm__("ldr r7, [r0]");
	__asm__("str r2, [r0]");
	__asm__("ldr r8, [r0]");

	__asm__("str r1, [r0]");
	__asm__("ldr r9, [r0]");
	__asm__("str r2, [r0]");
	__asm__("ldr r10, [r0]");

	__asm__("stmia R11!, {R3-R10}");

	__asm__("pop {R0-R11}");
}

static enum test_result test_noise(struct stm32mp1_ddrctl *ctl,
				   struct stm32mp1_ddrphy *phy,
				   char *string, int argc, char *argv[])
{
	u32 addr, pattern;
	u32 result[8];
	int i;
	enum test_result res = TEST_PASSED;

	if (get_pattern(string, argc, argv, 0, &pattern, 0xFFFFFFFF))
		return TEST_ERROR;
	if (get_addr(string, argc, argv, 1, &addr))
		return TEST_ERROR;

	printf("running noise for 0x%x at 0x%x\n", pattern, addr);

	do_noise(addr, pattern, result);

	for (i = 0; i < 0x8;) {
		if (check_addr((u32)&result[i++], pattern))
			res = TEST_FAILED;
		if (check_addr((u32)&result[i++], ~pattern))
			res = TEST_FAILED;
	}

	return res;
}

/**********************************************************************
 *
 * Function:    noise_burst
 *
 * Description: Verifies r/w while forcing switching of all data bus lines.
 *              optimised write loop witrh store multiple to use burst
 *              for pattern and inversed pattern
 *
 **********************************************************************/
void do_noise_burst(u32 addr, u32 pattern, size_t bufsize)
{
	__asm__("push {R0-R9}");
	__asm__("mov r0, %0" : : "r" (addr));
	__asm__("mov r1, %0" : : "r" (pattern));
	__asm__("mov r9, %0" : : "r" (bufsize));

	__asm__("mvn r2, r1");
	__asm__("mov r3, r1");
	__asm__("mov r4, r2");
	__asm__("mov r5, r1");
	__asm__("mov r6, r2");
	__asm__("mov r7, r1");
	__asm__("mov r8, r2");

	__asm__("loop1:");
	__asm__("stmia R0!, {R1-R8}");
	__asm__("stmia R0!, {R1-R8}");
	__asm__("stmia R0!, {R1-R8}");
	__asm__("stmia R0!, {R1-R8}");
	__asm__("subs r9, r9, #128");
	__asm__("bge loop1");
	__asm__("pop {R0-R9}");
}

/* chunk size enough to allow interruption with Ctrl-C*/
#define CHUNK_SIZE	0x8000000
static enum test_result test_noise_burst(struct stm32mp1_ddrctl *ctl,
					 struct stm32mp1_ddrphy *phy,
					 char *string, int argc, char *argv[])
{
	u32 addr, offset, pattern;
	size_t bufsize, remaining, size;
	int i;
	enum test_result res = TEST_PASSED;

	if (get_bufsize(string, argc, argv, 0, &bufsize, 4 * 1024, 128))
		return TEST_ERROR;
	if (get_pattern(string, argc, argv, 1, &pattern, 0xFFFFFFFF))
		return TEST_ERROR;
	if (get_addr(string, argc, argv, 2, &addr))
		return TEST_ERROR;

	printf("running noise burst for 0x%x at 0x%x + 0x%x",
	       pattern, addr, bufsize);

	offset = addr;
	remaining = bufsize;
	size = CHUNK_SIZE;
	while (remaining) {
		if (remaining < size)
			size = remaining;
		do_noise_burst(offset, pattern, size);
		remaining -= size;
		offset += size;
		if (progress(offset)) {
			res = TEST_FAILED;
			goto end;
		}
	}
	puts("\ncheck buffer");
	for (i = 0; i < bufsize;) {
		if (check_addr(addr + i, pattern))
			res = TEST_FAILED;
		i += 4;
		if (check_addr(addr + i, ~pattern))
			res = TEST_FAILED;
		i += 4;
		if (progress(i)) {
			res = TEST_FAILED;
			goto end;
		}
	}
end:
	puts("\n");
	return res;
}

/**********************************************************************
 *
 * Function:    pattern test
 *
 * Description: optimized loop for read/write pattern (array of 8 u32)
 *
 **********************************************************************/
#define PATTERN_SIZE	8
static enum test_result test_loop(const u32 *pattern, u32 *address,
				  const u32 bufsize)
{
	int i;
	int j;
	enum test_result res = TEST_PASSED;
	u32 offset, testsize, remaining;

	offset = (u32)address;
	remaining = bufsize;
	while (remaining) {
		testsize = bufsize > 0x1000000 ? 0x1000000 : bufsize;

		__asm__("push {R0-R10}");
		__asm__("mov r0, %0" : : "r" (pattern));
		__asm__("mov r1, %0" : : "r" (offset));
		__asm__("mov r2, %0" : : "r" (testsize));
		__asm__("ldmia r0!, {R3-R10}");

		__asm__("loop2:");
		__asm__("stmia r1!, {R3-R10}");
		__asm__("stmia r1!, {R3-R10}");
		__asm__("stmia r1!, {R3-R10}");
		__asm__("stmia r1!, {R3-R10}");
		__asm__("subs r2, r2, #128");
		__asm__("bge loop2");
		__asm__("pop {R0-R10}");

		offset += testsize;
		remaining -= testsize;
		if (progress((u32)offset)) {
			res = TEST_FAILED;
			goto end;
		}
	}

	puts("\ncheck buffer");
	for (i = 0; i < bufsize; i += PATTERN_SIZE * 4) {
		for (j = 0; j < PATTERN_SIZE; j++, address++)
			if (check_addr((u32)address, pattern[j])) {
				res = TEST_FAILED;
				goto end;
			}
		if (progress(i)) {
			res = TEST_FAILED;
			goto end;
		}
	}

end:
	puts("\n");
	return res;
}

const u32 pattern_div1_x16[PATTERN_SIZE] = {
	0x0000FFFF, 0x0000FFFF, 0x0000FFFF, 0x0000FFFF,
	0x0000FFFF, 0x0000FFFF, 0x0000FFFF, 0x0000FFFF
};

const u32 pattern_div2_x16[PATTERN_SIZE] = {
	0xFFFFFFFF, 0x00000000, 0xFFFFFFFF, 0x00000000,
	0xFFFFFFFF, 0x00000000, 0xFFFFFFFF, 0x00000000
};

const u32 pattern_div4_x16[PATTERN_SIZE] = {
	0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000,
	0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000
};

const u32 pattern_div4_x32[PATTERN_SIZE] = {
	0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
	0x00000000, 0x00000000, 0x00000000, 0x00000000
};

const u32 pattern_mostly_zero_x16[PATTERN_SIZE] = {
	0x00000000, 0x00000000, 0x00000000, 0x0000FFFF,
	0x00000000, 0x00000000, 0x00000000, 0x00000000
};

const u32 pattern_mostly_zero_x32[PATTERN_SIZE] = {
	0x00000000, 0x00000000, 0x00000000, 0xFFFFFFFF,
	0x00000000, 0x00000000, 0x00000000, 0x00000000
};

const u32 pattern_mostly_one_x16[PATTERN_SIZE] = {
	0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x0000FFFF,
	0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF
};

const u32 pattern_mostly_one_x32[PATTERN_SIZE] = {
	0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000,
	0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF
};

#define NB_PATTERN	5
static enum test_result test_freq_pattern(struct stm32mp1_ddrctl *ctl,
					  struct stm32mp1_ddrphy *phy,
					  char *string, int argc, char *argv[])
{
	const u32 * const patterns_x16[NB_PATTERN] = {
		pattern_div1_x16,
		pattern_div2_x16,
		pattern_div4_x16,
		pattern_mostly_zero_x16,
		pattern_mostly_one_x16,
	};
	const u32 * const patterns_x32[NB_PATTERN] = {
		pattern_div2_x16,
		pattern_div4_x16,
		pattern_div4_x32,
		pattern_mostly_zero_x32,
		pattern_mostly_one_x32
	};
	const char *patterns_comments[NB_PATTERN] = {
		"switching at frequency F/1",
		"switching at frequency F/2",
		"switching at frequency F/4",
		"mostly zero",
		"mostly one"
	};

	enum test_result res = TEST_PASSED, pattern_res;
	int i, bus_width;
	const u32 **patterns;
	u32 bufsize, addr;

	if (get_bufsize(string, argc, argv, 0, &bufsize, 4 * 1024, 128))
		return TEST_ERROR;
	if (get_addr(string, argc, argv, 1, &addr))
		return TEST_ERROR;

	switch (readl(&ctl->mstr) & DDRCTRL_MSTR_DATA_BUS_WIDTH_MASK) {
	case DDRCTRL_MSTR_DATA_BUS_WIDTH_HALF:
	case DDRCTRL_MSTR_DATA_BUS_WIDTH_QUARTER:
		bus_width = 16;
		break;
	default:
		bus_width = 32;
		break;
	}

	printf("running test pattern at 0x%08x length 0x%x width = %d\n",
	       addr, bufsize, bus_width);

	patterns =
		(const u32 **)(bus_width == 16 ? patterns_x16 : patterns_x32);

	for (i = 0; i < NB_PATTERN; i++) {
		printf("test data pattern %s:", patterns_comments[i]);
		pattern_res = test_loop(patterns[i], (u32 *)addr, bufsize);
		if (pattern_res != TEST_PASSED)	{
			printf("Failed\n");
			return pattern_res;
		}
		printf("Passed\n");
	}

	return res;
}

/**********************************************************************
 *
 * Function:    pattern test with size
 *
 * Description: loop for write pattern
 *
 **********************************************************************/

static enum test_result test_loop_size(const u32 *pattern, u32 size,
				       u32 *address,
				       const u32 bufsize)
{
	int i, j;
	enum test_result res = TEST_PASSED;
	u32 *p = address;

	for (i = 0; i < bufsize; i += size * 4) {
		for (j = 0; j < size ; j++, p++)
			*p = pattern[j];
		if (progress(i)) {
			res = TEST_FAILED;
			goto end;
		}
	}

	puts("\ncheck buffer");
	p = address;
	for (i = 0; i < bufsize; i += size * 4) {
		for (j = 0; j < size; j++, p++)
			if (check_addr((u32)p, pattern[j])) {
				res = TEST_FAILED;
				goto end;
			}
		if (progress(i)) {
			res = TEST_FAILED;
			goto end;
		}
	}

end:
	puts("\n");
	return res;
}

static enum test_result test_checkboard(struct stm32mp1_ddrctl *ctl,
					struct stm32mp1_ddrphy *phy,
					char *string, int argc, char *argv[])
{
	enum test_result res = TEST_PASSED;
	u32 bufsize, nb_loop, loop = 0, addr;
	int i;

	u32 checkboard[2] = {0x55555555, 0xAAAAAAAA};

	if (get_bufsize(string, argc, argv, 0, &bufsize, 4 * 1024, 8))
		return TEST_ERROR;
	if (get_nb_loop(string, argc, argv, 1, &nb_loop, 1))
		return TEST_ERROR;
	if (get_addr(string, argc, argv, 2, &addr))
		return TEST_ERROR;

	printf("running %d loops at 0x%08x length 0x%x\n",
	       nb_loop, addr, bufsize);
	while (1) {
		for (i = 0; i < 2; i++) {
			res = test_loop_size(checkboard, 2, (u32 *)addr,
					     bufsize);
			if (res)
				return res;
			checkboard[0] = ~checkboard[0];
			checkboard[1] = ~checkboard[1];
		}
		if (test_loop_end(&loop, nb_loop, 1))
			break;
	}
	sprintf(string, "no error for %d loops at 0x%08x length 0x%x",
		loop, addr, bufsize);

	return res;
}

static enum test_result test_blockseq(struct stm32mp1_ddrctl *ctl,
				      struct stm32mp1_ddrphy *phy,
				      char *string, int argc, char *argv[])
{
	enum test_result res = TEST_PASSED;
	u32 bufsize, nb_loop, loop = 0, addr, value;
	int i;

	if (get_bufsize(string, argc, argv, 0, &bufsize, 4 * 1024, 4))
		return TEST_ERROR;
	if (get_nb_loop(string, argc, argv, 1, &nb_loop, 1))
		return TEST_ERROR;
	if (get_addr(string, argc, argv, 2, &addr))
		return TEST_ERROR;

	printf("running %d loops at 0x%08x length 0x%x\n",
	       nb_loop, addr, bufsize);
	while (1) {
		for (i = 0; i < 256; i++) {
			value = i | i << 8 | i << 16 | i << 24;
			printf("pattern = %08x", value);
			res = test_loop_size(&value, 1, (u32 *)addr, bufsize);
			if (res != TEST_PASSED)
				return res;
		}
		if (test_loop_end(&loop, nb_loop, 1))
			break;
	}
	sprintf(string, "no error for %d loops at 0x%08x length 0x%x",
		loop, addr, bufsize);

	return res;
}

static enum test_result test_walkbit0(struct stm32mp1_ddrctl *ctl,
				      struct stm32mp1_ddrphy *phy,
				      char *string, int argc, char *argv[])
{
	enum test_result res = TEST_PASSED;
	u32 bufsize, nb_loop, loop = 0, addr, value;
	int i;

	if (get_bufsize(string, argc, argv, 0, &bufsize, 4 * 1024, 4))
		return TEST_ERROR;
	if (get_nb_loop(string, argc, argv, 1, &nb_loop, 1))
		return TEST_ERROR;
	if (get_addr(string, argc, argv, 2, &addr))
		return TEST_ERROR;

	printf("running %d loops at 0x%08x length 0x%x\n",
	       nb_loop, addr, bufsize);
	while (1) {
		for (i = 0; i < 64; i++) {
			if (i < 32)
				value = 1 << i;
			else
				value = 1 << (63 - i);

			printf("pattern = %08x", value);
			res = test_loop_size(&value, 1, (u32 *)addr, bufsize);
			if (res != TEST_PASSED)
				return res;
		}
		if (test_loop_end(&loop, nb_loop, 1))
			break;
	}
	sprintf(string, "no error for %d loops at 0x%08x length 0x%x",
		loop, addr, bufsize);

	return res;
}

static enum test_result test_walkbit1(struct stm32mp1_ddrctl *ctl,
				      struct stm32mp1_ddrphy *phy,
				      char *string, int argc, char *argv[])
{
	enum test_result res = TEST_PASSED;
	u32 bufsize, nb_loop, loop = 0, addr, value;
	int i;

	if (get_bufsize(string, argc, argv, 0, &bufsize, 4 * 1024, 4))
		return TEST_ERROR;
	if (get_nb_loop(string, argc, argv, 1, &nb_loop, 1))
		return TEST_ERROR;
	if (get_addr(string, argc, argv, 2, &addr))
		return TEST_ERROR;

	printf("running %d loops at 0x%08x length 0x%x\n",
	       nb_loop, addr, bufsize);
	while (1) {
		for (i = 0; i < 64; i++) {
			if (i < 32)
				value = ~(1 << i);
			else
				value = ~(1 << (63 - i));

			printf("pattern = %08x", value);
			res = test_loop_size(&value, 1, (u32 *)addr, bufsize);
			if (res != TEST_PASSED)
				return res;
		}
		if (test_loop_end(&loop, nb_loop, 1))
			break;
	}
	sprintf(string, "no error for %d loops at 0x%08x length 0x%x",
		loop, addr, bufsize);

	return res;
}

/*
 * try to catch bad bits which are dependent on the current values of
 * surrounding bits in either the same word32
 */
static enum test_result test_bitspread(struct stm32mp1_ddrctl *ctl,
				       struct stm32mp1_ddrphy *phy,
				       char *string, int argc, char *argv[])
{
	enum test_result res = TEST_PASSED;
	u32 bufsize, nb_loop, loop = 0, addr, bitspread[4];
	int i, j;

	if (get_bufsize(string, argc, argv, 0, &bufsize, 4 * 1024, 32))
		return TEST_ERROR;
	if (get_nb_loop(string, argc, argv, 1, &nb_loop, 1))
		return TEST_ERROR;
	if (get_addr(string, argc, argv, 2, &addr))
		return TEST_ERROR;

	printf("running %d loops at 0x%08x length 0x%x\n",
	       nb_loop, addr, bufsize);
	while (1) {
		for (i = 1; i < 32; i++) {
			for (j = 0; j < i; j++) {
				if (i < 32)
					bitspread[0] = (1 << i) | (1 << j);
				else
					bitspread[0] = (1 << (63 - i)) |
						       (1 << (63 - j));
				bitspread[1] = bitspread[0];
				bitspread[2] = ~bitspread[0];
				bitspread[3] = ~bitspread[0];
				printf("pattern = %08x", bitspread[0]);

				res = test_loop_size(bitspread, 4, (u32 *)addr,
						     bufsize);
				if (res != TEST_PASSED)
					return res;
			}
		}
		if (test_loop_end(&loop, nb_loop, 1))
			break;
	}
	sprintf(string, "no error for %d loops at 0x%08x length 0x%x",
		loop, addr, bufsize);

	return res;
}

static enum test_result test_bitflip(struct stm32mp1_ddrctl *ctl,
				     struct stm32mp1_ddrphy *phy,
				     char *string, int argc, char *argv[])
{
	enum test_result res = TEST_PASSED;
	u32 bufsize, nb_loop, loop = 0, addr;
	int i;

	u32 bitflip[4];

	if (get_bufsize(string, argc, argv, 0, &bufsize, 4 * 1024, 32))
		return TEST_ERROR;
	if (get_nb_loop(string, argc, argv, 1, &nb_loop, 1))
		return TEST_ERROR;
	if (get_addr(string, argc, argv, 2, &addr))
		return TEST_ERROR;

	printf("running %d loops at 0x%08x length 0x%x\n",
	       nb_loop, addr, bufsize);
	while (1) {
		for (i = 0; i < 32; i++) {
			bitflip[0] = 1 << i;
			bitflip[1] = bitflip[0];
			bitflip[2] = ~bitflip[0];
			bitflip[3] = bitflip[2];
			printf("pattern = %08x", bitflip[0]);

			res = test_loop_size(bitflip, 4, (u32 *)addr, bufsize);
			if (res != TEST_PASSED)
				return res;
		}
		if (test_loop_end(&loop, nb_loop, 1))
			break;
	}
	sprintf(string, "no error for %d loops at 0x%08x length 0x%x",
		loop, addr, bufsize);

	return res;
}

/**********************************************************************
 *
 * Function: infinite read access to DDR
 *
 * Description: continuous read the same pattern at the same address
 *
 **********************************************************************/
static enum test_result test_read(struct stm32mp1_ddrctl *ctl,
				  struct stm32mp1_ddrphy *phy,
				  char *string, int argc, char *argv[])
{
	u32 *addr;
	u32 data;
	u32 loop = 0;
	int i, size = 1024 * 1024;
	bool random = false;

	if (get_addr(string, argc, argv, 0, (u32 *)&addr))
		return TEST_ERROR;

	if (get_pattern(string, argc, argv, 1, &data, 0xA5A5AA55))
		return TEST_ERROR;

	if ((u32)addr == ADDR_INVALID) {
		printf("running random\n");
		random = true;
	} else {
		printf("running at 0x%08x with pattern=0x%08x\n",
		       (u32)addr, data);
		writel(data, addr);
	}

	while (1) {
		for (i = 0; i < size; i++) {
			if (random)
				addr = (u32 *)(STM32_DDR_BASE +
				       (rand() & (STM32_DDR_SIZE - 1) & ~0x3));
			data = readl(addr);
		}
		if (test_loop_end(&loop, 0, 1))
			break;
	}
	if (random)
		sprintf(string, "%d loops random", loop);
	else
		sprintf(string, "%d loops at 0x%x: %x", loop, (u32)addr, data);

	return TEST_PASSED;
}

/**********************************************************************
 *
 * Function: infinite write access to DDR
 *
 * Description: continuous write the same pattern at the same address
 *
 **********************************************************************/
static enum test_result test_write(struct stm32mp1_ddrctl *ctl,
				   struct stm32mp1_ddrphy *phy,
				   char *string, int argc, char *argv[])
{
	u32 *addr;
	u32 data;
	u32 loop = 0;
	int i, size = 1024 * 1024;
	bool random = false;

	if (get_addr(string, argc, argv, 0, (u32 *)&addr))
		return TEST_ERROR;

	if (get_pattern(string, argc, argv, 1, &data, 0xA5A5AA55))
		return TEST_ERROR;

	if ((u32)addr == ADDR_INVALID) {
		printf("running random\n");
		random = true;
	} else {
		printf("running at 0x%08x with pattern 0x%08x\n",
		       (u32)addr, data);
	}

	while (1) {
		for (i = 0; i < size; i++) {
			if (random) {
				addr = (u32 *)(STM32_DDR_BASE +
				       (rand() & (STM32_DDR_SIZE - 1) & ~0x3));
				data = rand();
			}
			writel(data, addr);
		}
		if (test_loop_end(&loop, 0, 1))
			break;
	}
	if (random)
		sprintf(string, "%d loops random", loop);
	else
		sprintf(string, "%d loops at 0x%x: %x", loop, (u32)addr, data);

	return TEST_PASSED;
}

#define NB_TEST_INFINITE 2
static enum test_result test_all(struct stm32mp1_ddrctl *ctl,
				 struct stm32mp1_ddrphy *phy,
				 char *string, int argc, char *argv[])
{
	enum test_result res = TEST_PASSED, result;
	int i, j, nb_error = 0, len;
	u32 loop = 0, nb_loop;
	int argc_test;
	char *argv_test[4];
	char loop_string[] = "1";
	char pattern_string[] = PATTERN_DEFAULT;
	u32 *addr;

	if (get_nb_loop(string, argc, argv, 0, &nb_loop, 1))
		return TEST_ERROR;

	if (get_addr(string, argc, argv, 2, (u32 *)&addr))
		return TEST_ERROR;

	while (!nb_error) {
		/* execute all the test except the lasts which are infinite */
		for (i = 1; i < test_nb - NB_TEST_INFINITE; i++) {
			argc_test = 0;
			j = 0;
			len = strlen(test[i].usage);
			if (argc > 1 && j < len &&
			    !strncmp("[size]", &test[i].usage[j], 6)) {
				argv_test[argc_test++] = argv[1];
				j += 7;
			}
			if (argc > 2) {
				if (j < len &&
				    !strncmp("[loop]", &test[i].usage[j], 6)) {
					argv_test[argc_test++] = loop_string;
					j += 7;
				}
				if (j < len &&
				    !strncmp("[pattern]", &test[i].usage[j],
					     9)) {
					argv_test[argc_test++] = pattern_string;
					j += 10;
				}
				if (j < len &&
				    !strncmp("[addr]", &test[i].usage[j], 6)) {
					argv_test[argc_test++] = argv[2];
					j += 7;
				}
			}
			printf("execute %d:%s\n", (int)i, test[i].name);
			result = test[i].fct(ctl, phy, string,
					     argc_test, argv_test);
			printf("result %d:%s = ", (int)i, test[i].name);
			if (result != TEST_PASSED) {
				nb_error++;
				res = TEST_FAILED;
				puts("Failed");
			} else {
				puts("Passed");
			}
			puts("\n\n");
		}
		printf("loop %d: %d/%d test failed\n\n\n",
		       loop + 1, nb_error, test_nb - NB_TEST_INFINITE);
		if (test_loop_end(&loop, nb_loop, 1))
			break;
	}
	if (res != TEST_PASSED) {
		sprintf(string, "loop %d: %d/%d test failed", loop, nb_error,
			test_nb - NB_TEST_INFINITE);
	} else {
		sprintf(string, "loop %d: %d tests passed", loop,
			test_nb - NB_TEST_INFINITE);
	}
	return res;
}

/****************************************************************
 * TEST Description
 ****************************************************************/

const struct test_desc test[] = {
	{test_all, "All", "[loop] [size] [addr]", "Execute all tests", 3 },
	{test_databus, "Simple DataBus", "[addr]",
	 "Verifies each data line by walking 1 on fixed address",
	 1
	 },
	{databuswalk0, "DataBusWalking0", "[loop] [addr]",
	 "Verifies each data bus signal can be driven low (32 word burst)",
	 2
	},
	{databuswalk1, "DataBusWalking1", "[loop] [addr]",
	 "Verifies each data bus signal can be driven high (32 word burst)",
	 2
	},
	{test_addressbus, "AddressBus", "[size] [addr]",
	 "Verifies each relevant bits of the address and checking for aliasing",
	 2
	 },
	{test_memdevice, "MemDevice", "[size] [addr]",
	 "Test the integrity of a physical memory (test every storage bit in the region)",
	 2
	 },
	{test_sso, "SimultaneousSwitchingOutput", "[size] [addr] ",
	 "Stress the data bus over an address range",
	 2
	},
	{test_noise, "Noise", "[pattern] [addr]",
	 "Verifies r/w while forcing switching of all data bus lines.",
	 3
	},
	{test_noise_burst, "NoiseBurst", "[size] [pattern] [addr]",
	 "burst transfers while forcing switching of the data bus lines",
	 3
	},
	{test_random, "Random", "[size] [loop] [addr]",
	 "Verifies r/w and memcopy(burst for pseudo random value.",
	 3
	},
	{test_freq_pattern, "FrequencySelectivePattern", "[size] [addr]",
	 "write & test patterns: Mostly Zero, Mostly One and F/n",
	 2
	},
	{test_blockseq, "BlockSequential", "[size] [loop] [addr]",
	 "test incremental pattern",
	 3
	},
	{test_checkboard, "Checkerboard", "[size] [loop] [addr]",
	 "test checker pattern",
	 3
	},
	{test_bitspread, "BitSpread", "[size] [loop] [addr]",
	 "test Bit Spread pattern",
	 3
	},
	{test_bitflip, "BitFlip", "[size] [loop] [addr]",
	 "test Bit Flip pattern",
	 3
	},
	{test_walkbit0, "WalkingOnes", "[size] [loop] [addr]",
	 "test Walking Ones pattern",
	 3
	},
	{test_walkbit1, "WalkingZeroes", "[size] [loop] [addr]",
	 "test Walking Zeroes pattern",
	 3
	},
	/* need to the the 2 last one (infinite) : skipped for test all */
	{test_read, "infinite read", "[addr] [pattern]",
	 "basic test : infinite read access (random: addr=0xFFFFFFFF)", 2},
	{test_write, "infinite write", "[addr] [pattern]",
	 "basic test : infinite write access (random: addr=0xFFFFFFFF)", 2},
};

const int test_nb = ARRAY_SIZE(test);