xref: /rk3399_rockchip-uboot/drivers/rkflash/rkflash_debug.c (revision bc04a3dd9a41813372820ba50655022a6a28bfbf)
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;
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_lba = 0x800;
60 	else
61 		test_lba = 0;
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 < 10; loop++) {
68 		printf("---------Test loop = %d---------\n", loop);
69 		printf("---------Test ftl write---------\n");
70 		test_sec_count = 1;
71 		printf("test_end_lba = %x\n", test_end_lba);
72 		printf("test_lba = %x\n", test_lba);
73 		for (; (test_lba + test_sec_count) < test_end_lba;) {
74 			pwrite32[0] = test_lba;
75 			blk_dwrite(block_dev, test_lba, test_sec_count, pwrite);
76 			blk_dread(block_dev, test_lba, test_sec_count, pread);
77 			for (j = 0; j < test_sec_count * 512; j++) {
78 				if (pwrite[j] != pread[j]) {
79 					rkflash_print_hex("w:",
80 							  pwrite,
81 							  4,
82 							  test_sec_count * 128);
83 					rkflash_print_hex("r:",
84 							  pread,
85 							  4,
86 							  test_sec_count * 128);
87 					printf("r=%x, n=%x, w=%x, r=%x\n",
88 					       test_lba,
89 					       j,
90 					       pwrite[j],
91 					       pread[j]);
92 					while (1)
93 						;
94 					break;
95 				}
96 			}
97 			print_flag = test_lba & 0x1FF;
98 			if (print_flag < test_sec_count)
99 				printf("test_lba = %x\n", test_lba);
100 			test_lba += test_sec_count;
101 			test_sec_count++;
102 			if (test_sec_count > max_test_sector)
103 				test_sec_count = 1;
104 		}
105 		printf("---------Test ftl check---------\n");
106 
107 		test_sec_count = 1;
108 		for (; (test_lba + test_sec_count) < test_end_lba;) {
109 			pwrite32[0] = test_lba;
110 			blk_dread(block_dev, test_lba, test_sec_count, pread);
111 			print_flag = test_lba & 0x7FF;
112 			if (print_flag < test_sec_count)
113 				printf("test_lba = %x\n", test_lba);
114 
115 			for (j = 0; j < test_sec_count * 512; j++) {
116 				if (pwrite[j] != pread[j]) {
117 					printf("r=%x, n=%x, w=%x, r=%x\n",
118 					       test_lba,
119 					       j,
120 					       pwrite[j],
121 					       pread[j]);
122 					/* while(1); */
123 					break;
124 				}
125 			}
126 			test_lba += test_sec_count;
127 			test_sec_count++;
128 			if (test_sec_count > max_test_sector)
129 				test_sec_count = 1;
130 		}
131 	}
132 	printf("---------Test end---------\n");
133 	/* while(1); */
134 }
135 #endif
136 
137 void rkflash_test(struct udevice *udev)
138 {
139 #if (BLK_STRESS_TEST_EN)
140 	blk_stress_test(udev);
141 #endif
142 }
143 
144