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