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 #include <common.h>
9
10 #include "rkflash_debug.h"
11 #include "rkflash_blk.h"
12 #include "boot_rkimg.h"
13
14 static unsigned int rkflash_debug;
15
rkflash_print_dio(const char * fmt,...)16 __printf(1, 2) int rkflash_print_dio(const char *fmt, ...)
17 {
18 int nret = 0;
19 #if PRINT_SWI_CON_IO
20 if (rkflash_debug & PRINT_BIT_CON_IO) {
21 va_list args;
22
23 if (!fmt)
24 return nret;
25
26 va_start(args, fmt);
27 nret = vprintf(fmt, args);
28 va_end(args);
29 }
30 #endif
31 return nret;
32 }
33
rkflash_print_bio(const char * fmt,...)34 __printf(1, 2) int rkflash_print_bio(const char *fmt, ...)
35 {
36 int nret = 0;
37 #if PRINT_SWI_BLK_IO
38 if (rkflash_debug & PRINT_BIT_BLK_IO) {
39 va_list args;
40
41 if (!fmt)
42 return nret;
43
44 va_start(args, fmt);
45 nret = vprintf(fmt, args);
46 va_end(args);
47 }
48 #endif
49 return nret;
50 }
51
rkflash_print_info(const char * fmt,...)52 __printf(1, 2) int rkflash_print_info(const char *fmt, ...)
53 {
54 int nret = 0;
55 #if PRINT_SWI_INFO
56 va_list args;
57
58 if (!fmt)
59 return nret;
60
61 va_start(args, fmt);
62 nret = vprintf(fmt, args);
63 va_end(args);
64 #endif
65 return nret;
66 }
67
rkflash_print_error(const char * fmt,...)68 __printf(1, 2) int rkflash_print_error(const char *fmt, ...)
69 {
70 int nret = 0;
71 #if PRINT_SWI_ERROR
72 va_list args;
73
74 if (!fmt)
75 return nret;
76
77 va_start(args, fmt);
78 nret = vprintf(fmt, args);
79 va_end(args);
80 #endif
81 return nret;
82 }
83
rkflash_print_hex(const char * s,const void * buf,int w,size_t len)84 void rkflash_print_hex(const char *s, const void *buf, int w, size_t len)
85 {
86 }
87
88 #if (BLK_STRESS_TEST_EN)
89 #define max_test_sector 64
90 static u8 pwrite[max_test_sector * 512];
91 static u8 pread[max_test_sector * 512];
92 static u32 *pwrite32;
blk_stress_test(struct udevice * udev)93 void blk_stress_test(struct udevice *udev)
94 {
95 struct blk_desc *block_dev = rockchip_get_bootdev();
96 u16 i, j, loop = 0;
97 u32 test_end_lba, test_begin_lba;
98 u32 test_lba;
99 u16 test_sec_count = 1;
100 u16 print_flag;
101
102 if (!block_dev) {
103 printf("device unknown\n");
104 return;
105 }
106
107 if (block_dev->if_type == IF_TYPE_SPINOR)
108 test_begin_lba = 0x4000;
109 else
110 test_begin_lba = 0x8000;
111
112 test_end_lba = block_dev->lba;
113 pwrite32 = (u32 *)pwrite;
114 for (i = 0; i < (max_test_sector * 512); i++)
115 pwrite[i] = i;
116 for (loop = 0; loop < 100; loop++) {
117 test_lba = test_begin_lba;
118 printf("---------Test loop = %d---------\n", loop);
119 printf("---------Test ftl write---------\n");
120 test_sec_count = 1;
121 printf("test_end_lba = %x\n", test_end_lba);
122 printf("test_lba = %x\n", test_lba);
123 for (; (test_lba + test_sec_count) < test_end_lba;) {
124 pwrite32[0] = test_lba;
125 blk_dwrite(block_dev, test_lba, test_sec_count, pwrite);
126 blk_dread(block_dev, test_lba, test_sec_count, pread);
127 for (j = 0; j < test_sec_count * 512; j++) {
128 if (pwrite[j] != pread[j]) {
129 printf("rkflash stress test fail\n");
130 rkflash_print_hex("w:",
131 pwrite,
132 4,
133 test_sec_count * 128);
134 rkflash_print_hex("r:",
135 pread,
136 4,
137 test_sec_count * 128);
138 printf("r=%x, n=%x, w=%x, r=%x\n",
139 test_lba,
140 j,
141 pwrite[j],
142 pread[j]);
143 while (1)
144 ;
145 }
146 }
147 print_flag = test_lba & 0x1FF;
148 if (print_flag < test_sec_count)
149 printf("test_lba = %x\n", test_lba);
150 test_lba += test_sec_count;
151 test_sec_count++;
152 if (test_sec_count > max_test_sector)
153 test_sec_count = 1;
154 }
155 printf("---------Test ftl check---------\n");
156
157 test_lba = test_begin_lba;
158 test_sec_count = 1;
159 for (; (test_lba + test_sec_count) < test_end_lba;) {
160 pwrite32[0] = test_lba;
161 blk_dread(block_dev, test_lba, test_sec_count, pread);
162 print_flag = test_lba & 0x7FF;
163 if (print_flag < test_sec_count)
164 printf("test_lba = %x\n", test_lba);
165
166 for (j = 0; j < test_sec_count * 512; j++) {
167 if (pwrite[j] != pread[j]) {
168 printf("rkflash stress test fail\n");
169 printf("r=%x, n=%x, w=%x, r=%x\n",
170 test_lba,
171 j,
172 pwrite[j],
173 pread[j]);
174 while (1)
175 ;
176 }
177 }
178 test_lba += test_sec_count;
179 test_sec_count++;
180 if (test_sec_count > max_test_sector)
181 test_sec_count = 1;
182 }
183 }
184 printf("---------Test end---------\n");
185 /* while(1); */
186 }
187 #endif
188
rkflash_test(struct udevice * udev)189 void rkflash_test(struct udevice *udev)
190 {
191 #if (BLK_STRESS_TEST_EN)
192 blk_stress_test(udev);
193 #endif
194 }
195
196