lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* drivers/mtd/mtdadapt.c |
| 2 | * |
| 3 | * Mtd api adapt driver for nand&nor. |
| 4 | * |
| 5 | * Copyright (c) 2018 ZTE Ltd. |
| 6 | * |
| 7 | * This software is licensed under the terms of the GNU General Public |
| 8 | * License version 2, as published by the Free Software Foundation, and |
| 9 | * may be copied, distributed, and modified under those terms. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/mtd/mtd.h> |
| 21 | #include <linux/module.h> |
xf.li | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame] | 22 | #include <linux/soc/zte/otp/otp_zx.h> |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 23 | |
| 24 | #define READ_ZLOADER_FLAG_SIZE 0x800 |
| 25 | #define WRITE_ZLOADER_FLAG_SIZE 0x3000 |
| 26 | |
| 27 | |
| 28 | extern struct mtd_info *mtd_fota; |
| 29 | extern int g_zload_read_only_flag; |
xf.li | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame] | 30 | extern char *nor_cmdline; |
| 31 | unsigned char nor_flag = 0; |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 32 | |
| 33 | #ifndef USE_CPPS_KO |
| 34 | extern unsigned int zOss_NvItemRead(unsigned int NvItemID, unsigned char *NvItemData, unsigned int NvItemLen); |
| 35 | extern unsigned int zOss_NvItemWrite(unsigned int NvItemID, unsigned char *NvItemData, unsigned int NvItemLen); |
| 36 | #endif |
| 37 | |
| 38 | /*zdd need modify*/ |
| 39 | int zDrvNand_ChangeNvrAttr(unsigned int rw ) |
| 40 | { |
| 41 | int ret = 0; |
| 42 | // ret = nand_nv_mid_r_set_to_rw(rw); |
| 43 | return ret; |
| 44 | } |
| 45 | EXPORT_SYMBOL(zDrvNand_ChangeNvrAttr); |
| 46 | |
| 47 | /* |
| 48 | * nv read interface. |
| 49 | * dwstart: nv addr to read |
| 50 | * dwLen: length of nv to read |
| 51 | * to: memory addr |
| 52 | */ |
| 53 | int zDrvNand_Read(unsigned int dwStart, unsigned int dwLen, unsigned char* to) |
| 54 | { |
| 55 | int ret = 0; |
| 56 | |
| 57 | #ifdef USE_CPPS_KO |
| 58 | ret = cpps_callbacks.zOss_NvItemRead(dwStart, to, dwLen); |
| 59 | #else |
| 60 | ret = zOss_NvItemRead(dwStart, to, dwLen); |
| 61 | #endif |
| 62 | |
| 63 | return ret; |
| 64 | } |
| 65 | EXPORT_SYMBOL(zDrvNand_Read); |
| 66 | |
| 67 | /* |
| 68 | * nv write interface. |
| 69 | * dwstart: nv addr to program |
| 70 | * dwLen: length of nv to program |
| 71 | * from: memory addr |
| 72 | */ |
| 73 | int zDrvNand_Program(unsigned int dwStart, unsigned int dwLen, unsigned char* from) |
| 74 | { |
| 75 | int ret = 0; |
| 76 | |
| 77 | #ifdef USE_CPPS_KO |
| 78 | ret = cpps_callbacks.zOss_NvItemWrite(dwStart,from, dwLen); |
| 79 | #else |
| 80 | ret = zOss_NvItemWrite(dwStart,from, dwLen); |
| 81 | #endif |
| 82 | |
| 83 | return ret; |
| 84 | } |
| 85 | EXPORT_SYMBOL(zDrvNand_Program); |
| 86 | |
| 87 | /*zdd need modify*/ |
| 88 | int zDrvNand_SmsRead(unsigned int dwStart, unsigned int dwLen, unsigned char* to) |
| 89 | { |
| 90 | int ret = 0; |
| 91 | |
| 92 | //ret = zftl_wrapper_read((char *)"sms", dwStart, dwLen, to); |
| 93 | |
| 94 | return ret; |
| 95 | } |
| 96 | |
| 97 | /*zdd need modify*/ |
| 98 | int zDrvNand_SmsProgram(unsigned int dwStart, unsigned int dwLen, unsigned char* from) |
| 99 | { |
| 100 | int ret = 0; |
| 101 | |
| 102 | //ret = zftl_wrapper_write((unsigned char *)"sms", dwStart, dwLen, from); |
| 103 | |
| 104 | return ret; |
| 105 | } |
| 106 | |
| 107 | /*zdd need modify*/ |
| 108 | int zDrvNand_SimNvRead(unsigned int dwStart, unsigned int dwLen, unsigned char* to) |
| 109 | { |
| 110 | int ret = 0; |
| 111 | #if 0//fyi |
| 112 | NAND_LOCK |
| 113 | nand_clk_gate(SYSCLK_ENABLE); |
| 114 | ret = nand_simnv_read(dwStart, dwLen, to); |
| 115 | nand_clk_gate(SYSCLK_DISABLE); |
| 116 | NAND_UNLOCK |
| 117 | |
| 118 | if(ret == -1) |
| 119 | { |
| 120 | zftl_res = 1; |
| 121 | zDrvNand_SetSimNvFlag(dwStart);/*ECC REBOOT*/ |
| 122 | zftl_res = 0; |
| 123 | } |
| 124 | #endif |
| 125 | //ret = zftl_wrapper_read((unsigned char *)"simnv", dwStart, dwLen, to); |
| 126 | |
| 127 | return ret; |
| 128 | } |
| 129 | |
| 130 | /*zdd need modify*/ |
| 131 | int zDrvNand_SimNvProgram(unsigned int dwStart, unsigned int dwLen, unsigned char* from) |
| 132 | { |
| 133 | int ret = 0; |
| 134 | #if 0//fyi |
| 135 | NAND_LOCK |
| 136 | nand_clk_gate(SYSCLK_ENABLE); |
| 137 | ret = nand_simnv_program(dwStart, dwLen, from); |
| 138 | nand_clk_gate(SYSCLK_DISABLE); |
| 139 | NAND_UNLOCK |
| 140 | if(ret == -1) |
| 141 | { |
| 142 | zftl_res = 1; |
| 143 | zDrvNand_SetSimNvFlag(dwStart);/*ECC REBOOT*/ |
| 144 | zftl_res = 0; |
| 145 | } |
| 146 | #endif |
| 147 | //ret = zftl_wrapper_write((unsigned char *)"simnv", dwStart, dwLen, from); |
| 148 | |
| 149 | return ret; |
| 150 | } |
| 151 | |
| 152 | /*zdd need modify*/ |
| 153 | int zDrvNand_SimNvFacRead(unsigned int dwStart, unsigned int dwLen, unsigned char* to) |
| 154 | { |
| 155 | int ret = 0; |
| 156 | #if 0//fyi |
| 157 | NAND_LOCK |
| 158 | nand_clk_gate(SYSCLK_ENABLE); |
| 159 | ret = nand_simnvfac_read(dwStart, dwLen, to); |
| 160 | nand_clk_gate(SYSCLK_DISABLE); |
| 161 | NAND_UNLOCK |
| 162 | #endif |
| 163 | //ret = zftl_wrapper_read((unsigned char *)"simnvfac", dwStart, dwLen, to); |
| 164 | |
| 165 | return ret; |
| 166 | } |
| 167 | |
| 168 | /*zdd need modify*/ |
| 169 | int zDrvNand_SimNvFacProgram(unsigned int dwStart, unsigned int dwLen, unsigned char* from) |
| 170 | { |
| 171 | int ret = 0; |
| 172 | #if 0//fyi |
| 173 | NAND_LOCK |
| 174 | nand_clk_gate(SYSCLK_ENABLE); |
| 175 | ret = nand_simnvfac_program(dwStart, dwLen, from); |
| 176 | nand_clk_gate(SYSCLK_DISABLE); |
| 177 | NAND_UNLOCK |
| 178 | #endif |
| 179 | //ret = zftl_wrapper_write((unsigned char *)"simnvfac", dwStart, dwLen, from); |
| 180 | |
| 181 | return ret; |
| 182 | } |
| 183 | |
| 184 | /*zdd need modify*/ |
| 185 | int zDrvNand_NvRwEccMake(unsigned int dwStart, unsigned int dwLen) |
| 186 | { |
| 187 | char buffer[2048]; |
| 188 | |
| 189 | //if( dwStart >= NVRW_SIZE||dwStart < 0 || (dwStart +dwLen)> NVRW_SIZE|| dwLen > 2048) |
| 190 | // return -1; |
| 191 | //fyi nand_nvrw_mid_ecc_make(dwStart,dwLen,buffer); |
| 192 | return 0; |
| 193 | } |
| 194 | EXPORT_SYMBOL(zDrvNand_NvRwEccMake); |
| 195 | |
| 196 | /*zdd need modify*/ |
| 197 | int zDrvNand_EccMake(unsigned char* partName,unsigned int dwStart, unsigned int dwLen) |
| 198 | { |
| 199 | return 0; |
| 200 | } |
| 201 | EXPORT_SYMBOL(zDrvNand_EccMake); |
| 202 | |
| 203 | /* |
| 204 | * get the bootflag from flash. |
| 205 | * return: 0, dl off 1, dl on |
| 206 | */ |
| 207 | unsigned int zDrvNand_ReadBootflag( void ) |
| 208 | { |
| 209 | unsigned int bootflag = 0; |
| 210 | |
| 211 | char value = 0; |
| 212 | int retlen = 0; |
| 213 | |
| 214 | unsigned char *buffer = kzalloc(READ_ZLOADER_FLAG_SIZE,GFP_KERNEL); |
| 215 | if( buffer == NULL ) |
| 216 | return -1; |
| 217 | |
xf.li | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame] | 218 | if(nor_cmdline != NULL) |
| 219 | { |
| 220 | if (!strcmp(nor_cmdline, "1")) |
| 221 | { |
| 222 | nor_flag = 1; |
| 223 | printk("----------EnhancedSecurity---------\n"); |
| 224 | } |
| 225 | } |
| 226 | if(1 == nor_flag) |
| 227 | { |
| 228 | #ifndef CONFIG_SPI_ZXIC_NOR |
| 229 | |
| 230 | if(nor_read(0, 256, buffer)) |
| 231 | { |
| 232 | kfree(buffer); |
| 233 | return 1; |
| 234 | } |
| 235 | memcpy(&value, buffer+2, 1); |
| 236 | bootflag = value; |
| 237 | |
| 238 | if( bootflag == 0x5a) |
| 239 | { |
| 240 | bootflag = 1; |
| 241 | } |
| 242 | else |
| 243 | { |
| 244 | bootflag = 0; |
| 245 | } |
| 246 | kfree(buffer); |
| 247 | #endif |
| 248 | } |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 249 | else |
| 250 | { |
xf.li | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame] | 251 | if(mtd_read(mtd_fota,0,READ_ZLOADER_FLAG_SIZE,&retlen,buffer))/* BOOTFLAGÔÚnandµÄµÚ20~27×Ö½Ú */ |
| 252 | { |
| 253 | kfree(buffer); |
| 254 | return 1; |
| 255 | } |
| 256 | memcpy(&value, buffer+2, 1); |
| 257 | bootflag = value; |
| 258 | |
| 259 | if( bootflag == 0x5a) |
| 260 | { |
| 261 | bootflag = 1; |
| 262 | } |
| 263 | else |
| 264 | { |
| 265 | bootflag = 0; |
| 266 | } |
| 267 | kfree(buffer); |
| 268 | |
| 269 | } |
| 270 | |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 271 | |
| 272 | return bootflag; |
| 273 | } |
| 274 | EXPORT_SYMBOL(zDrvNand_ReadBootflag); |
| 275 | |
| 276 | /* |
| 277 | * write the bootflag to flash. |
| 278 | * flag: 0,dl off else dl on |
| 279 | */ |
| 280 | unsigned int zDrvNand_WriteBootflag(unsigned int flag) |
| 281 | { |
| 282 | |
| 283 | unsigned int ret = 0; |
| 284 | char value = 0; |
| 285 | int bootflag = 0; |
| 286 | int retlen = 0; |
| 287 | struct erase_info ei; |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 288 | |
xf.li | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame] | 289 | if(nor_cmdline != NULL) |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 290 | { |
xf.li | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame] | 291 | if (!strcmp(nor_cmdline, "1")) |
| 292 | { |
| 293 | nor_flag = 1; |
| 294 | printk("----------EnhancedSecurity---------\n"); |
| 295 | }else{ |
| 296 | printk("----------normal---------\n"); |
| 297 | } |
| 298 | }else{ |
| 299 | printk("----------nor cmdline is null!---------\n"); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 300 | } |
xf.li | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame] | 301 | |
| 302 | if(1 == nor_flag) |
| 303 | { |
| 304 | #ifndef CONFIG_SPI_ZXIC_NOR |
| 305 | |
| 306 | unsigned char *buffer = kmalloc(0x10000, GFP_KERNEL); |
| 307 | |
| 308 | if( buffer == NULL ) |
| 309 | return -1; |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 310 | |
xf.li | ce87319 | 2023-11-08 17:10:35 -0800 | [diff] [blame] | 311 | if(nor_read(0,0x10000,buffer)) |
| 312 | { |
| 313 | kfree(buffer); |
| 314 | return -1; |
| 315 | } |
| 316 | if(flag == 0 ) |
| 317 | { |
| 318 | bootflag = 0x00; |
| 319 | memset(&value, bootflag, 1); |
| 320 | } |
| 321 | else |
| 322 | { |
| 323 | bootflag = 0x5a; |
| 324 | memset(&value, bootflag, 1); |
| 325 | } |
| 326 | |
| 327 | memcpy(buffer+2, &value, 1); |
| 328 | |
| 329 | ret = nor_erase(0); |
| 330 | ret = nor_write(0,0x10000,buffer); |
| 331 | kfree(buffer); |
| 332 | #endif |
| 333 | } |
| 334 | else |
| 335 | { |
| 336 | unsigned char *buffer = kzalloc(WRITE_ZLOADER_FLAG_SIZE,GFP_KERNEL); |
| 337 | |
| 338 | if( buffer == NULL ) |
| 339 | return -1; |
| 340 | |
| 341 | if(mtd_read(mtd_fota,0,WRITE_ZLOADER_FLAG_SIZE,&retlen,buffer))/* BOOTFLAGÔÚnandµÄµÚ20~27×Ö½Ú */ |
| 342 | { |
| 343 | kfree(buffer); |
| 344 | return -1; |
| 345 | } |
| 346 | if(flag == 0 ) |
| 347 | { |
| 348 | bootflag = 0x00; |
| 349 | memset(&value, bootflag, 1); |
| 350 | } |
| 351 | else |
| 352 | { |
| 353 | bootflag = 0x5a; |
| 354 | memset(&value, bootflag, 1); |
| 355 | } |
| 356 | |
| 357 | memcpy(buffer+2, &value, 1); |
| 358 | memset(&ei, 0, sizeof(struct erase_info)); |
| 359 | ei.mtd = mtd_fota; |
| 360 | ei.addr = 0; |
| 361 | ei.len = mtd_fota->erasesize; |
| 362 | g_zload_read_only_flag = 1; |
| 363 | ret = mtd_erase(mtd_fota, &ei); /*²Á³ýµÚÒ»¿é*/ |
| 364 | ret = mtd_write(mtd_fota,0,WRITE_ZLOADER_FLAG_SIZE,&retlen,buffer); |
| 365 | g_zload_read_only_flag = 0; |
| 366 | kfree(buffer); |
| 367 | |
| 368 | } |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 369 | |
| 370 | return 0; |
| 371 | |
| 372 | } |
| 373 | EXPORT_SYMBOL(zDrvNand_WriteBootflag); |
| 374 | |
| 375 | /* |
| 376 | * get the usbtimeout flag from flash. |
| 377 | * return: should be 1~10 |
| 378 | */ |
| 379 | unsigned int zDrvNand_ReadUsbtimeout( void ) |
| 380 | { |
| 381 | |
| 382 | unsigned int usbtimeout = 0; |
| 383 | char value = 0; |
| 384 | int retlen = 0; |
| 385 | |
| 386 | unsigned char *buffer = kzalloc(READ_ZLOADER_FLAG_SIZE,GFP_KERNEL); |
| 387 | if( buffer == NULL ) |
| 388 | return -1; |
| 389 | |
| 390 | if(mtd_read(mtd_fota,0,READ_ZLOADER_FLAG_SIZE,&retlen,buffer))/* BOOTFLAGÔÚnandµÄµÚ20~27×Ö½Ú */ |
| 391 | { |
| 392 | kfree(buffer); |
| 393 | return -1; |
| 394 | } |
| 395 | memcpy(&value, buffer+3, 1); |
| 396 | usbtimeout = value; |
| 397 | |
| 398 | kfree(buffer); |
| 399 | return usbtimeout; |
| 400 | |
| 401 | } |
| 402 | EXPORT_SYMBOL(zDrvNand_ReadUsbtimeout); |
| 403 | |
| 404 | /* |
| 405 | * write the usbtimeout flag to flash. |
| 406 | * flag: should be 1~10 |
| 407 | */ |
| 408 | unsigned int zDrvNand_WriteUsbtimeout( unsigned int flag ) |
| 409 | { |
| 410 | |
| 411 | unsigned int ret = 0; |
| 412 | uint8_t oob[256]; |
| 413 | int i =0; |
| 414 | int times = 0; |
| 415 | int retlen = 0; |
| 416 | char value = 0; |
| 417 | struct erase_info ei; |
| 418 | |
| 419 | unsigned char *buffer = kzalloc(WRITE_ZLOADER_FLAG_SIZE,GFP_KERNEL); /* ZLOADERºÍ·ÖÇø±í¹²8192BYTES */ |
| 420 | |
| 421 | if( buffer == NULL ) |
| 422 | return -1; |
| 423 | |
| 424 | if(mtd_read(mtd_fota,0,WRITE_ZLOADER_FLAG_SIZE,&retlen,buffer))/* BOOTFLAGÔÚnandµÄµÚ20~27×Ö½Ú */ |
| 425 | { |
| 426 | kfree(buffer); |
| 427 | return -1; |
| 428 | } |
| 429 | |
| 430 | memset(&value, flag, 1); |
| 431 | memcpy(buffer+3, &value, 1); |
| 432 | |
| 433 | memset(&ei, 0, sizeof(struct erase_info)); |
| 434 | ei.mtd = mtd_fota; |
| 435 | ei.addr = 0; |
| 436 | ei.len = mtd_fota->erasesize; |
| 437 | g_zload_read_only_flag = 1; |
| 438 | ret = mtd_erase(mtd_fota, &ei); /*²Á³ýµÚÒ»¿é*/ |
| 439 | ret = mtd_write(mtd_fota,0,WRITE_ZLOADER_FLAG_SIZE,&retlen,buffer); |
| 440 | g_zload_read_only_flag = 0; |
| 441 | kfree(buffer); |
| 442 | |
| 443 | return ret; |
| 444 | } |
| 445 | EXPORT_SYMBOL(zDrvNand_WriteUsbtimeout); |
| 446 | |