xref: /rk3399_rockchip-uboot/drivers/rkflash/rkflash_debug.c (revision 6aa65bb1ee0951865e27da81dde1de76c6d4687e)
1 /*
2  * Copyright (c) 2018 Fuzhou Rockchip Electronics Co., Ltd
3  *
4  * SPDX-License-Identifier:	GPL-2.0
5  */
6 
7 #include <blk.h>
8 
9 #include "rkflash_debug.h"
10 #include "rkflash_blk.h"
11 #include "boot_rkimg.h"
12 
13 void rkflash_print_hex(char *s, void *buf, u32 width, u32 len)
14 {
15 	u32 i, j;
16 	char *p8 = (char *)buf;
17 	short *p16 = (short *)buf;
18 	u32 *p32 = (u32 *)buf;
19 
20 	j = 0;
21 	for (i = 0; i < len; i++) {
22 		if (j == 0)
23 			printf("%s 0x%x:", s, i * width);
24 
25 		if (width == 4)
26 			printf("%x ", p32[i]);
27 		else if (width == 2)
28 			printf("%x ", p16[i]);
29 		else
30 			printf("%02x ", p8[i]);
31 		if (++j >= 16) {
32 			j = 0;
33 			printf("\n");
34 		}
35 	}
36 	printf("\n");
37 }
38 
39 #if (BLK_STRESS_TEST_EN)
40 #define max_test_sector 64
41 static u8 pwrite[max_test_sector * 512];
42 static u8 pread[max_test_sector * 512];
43 static u32 *pwrite32;
44 void blk_stress_test(struct udevice *udev)
45 {
46 	struct blk_desc *block_dev = rockchip_get_bootdev();
47 	u16 i, j, loop = 0;
48 	u32 test_end_lba, test_begin_lba;
49 	u32 test_lba;
50 	u16 test_sec_count = 1;
51 	u16 print_flag;
52 
53 	if (!block_dev) {
54 		printf("device unknown\n");
55 		return;
56 	}
57 
58 	if (block_dev->if_type == IF_TYPE_SPINOR)
59 		test_begin_lba = 0x4000;
60 	else
61 		test_begin_lba = 0x8000;
62 
63 	test_end_lba = block_dev->lba;
64 	pwrite32 = (u32 *)pwrite;
65 	for (i = 0; i < (max_test_sector * 512); i++)
66 		pwrite[i] = i;
67 	for (loop = 0; loop < 100; loop++) {
68 		test_lba = test_begin_lba;
69 		printf("---------Test loop = %d---------\n", loop);
70 		printf("---------Test ftl write---------\n");
71 		test_sec_count = 1;
72 		printf("test_end_lba = %x\n", test_end_lba);
73 		printf("test_lba = %x\n", test_lba);
74 		for (; (test_lba + test_sec_count) < test_end_lba;) {
75 			pwrite32[0] = test_lba;
76 			blk_dwrite(block_dev, test_lba, test_sec_count, pwrite);
77 			blk_dread(block_dev, test_lba, test_sec_count, pread);
78 			for (j = 0; j < test_sec_count * 512; j++) {
79 				if (pwrite[j] != pread[j]) {
80 					printf("rkflash stress test fail\n");
81 					rkflash_print_hex("w:",
82 							  pwrite,
83 							  4,
84 							  test_sec_count * 128);
85 					rkflash_print_hex("r:",
86 							  pread,
87 							  4,
88 							  test_sec_count * 128);
89 					printf("r=%x, n=%x, w=%x, r=%x\n",
90 					       test_lba,
91 					       j,
92 					       pwrite[j],
93 					       pread[j]);
94 					while (1)
95 						;
96 				}
97 			}
98 			print_flag = test_lba & 0x1FF;
99 			if (print_flag < test_sec_count)
100 				printf("test_lba = %x\n", test_lba);
101 			test_lba += test_sec_count;
102 			test_sec_count++;
103 			if (test_sec_count > max_test_sector)
104 				test_sec_count = 1;
105 		}
106 		printf("---------Test ftl check---------\n");
107 
108 		test_lba = test_begin_lba;
109 		test_sec_count = 1;
110 		for (; (test_lba + test_sec_count) < test_end_lba;) {
111 			pwrite32[0] = test_lba;
112 			blk_dread(block_dev, test_lba, test_sec_count, pread);
113 			print_flag = test_lba & 0x7FF;
114 			if (print_flag < test_sec_count)
115 				printf("test_lba = %x\n", test_lba);
116 
117 			for (j = 0; j < test_sec_count * 512; j++) {
118 				if (pwrite[j] != pread[j]) {
119 					printf("rkflash stress test fail\n");
120 					printf("r=%x, n=%x, w=%x, r=%x\n",
121 					       test_lba,
122 					       j,
123 					       pwrite[j],
124 					       pread[j]);
125 					while (1)
126 						;
127 				}
128 			}
129 			test_lba += test_sec_count;
130 			test_sec_count++;
131 			if (test_sec_count > max_test_sector)
132 				test_sec_count = 1;
133 		}
134 	}
135 	printf("---------Test end---------\n");
136 	/* while(1); */
137 }
138 #endif
139 
140 void rkflash_test(struct udevice *udev)
141 {
142 #if (BLK_STRESS_TEST_EN)
143 	blk_stress_test(udev);
144 #endif
145 }
146 
147