blob: 1cc4218f4c8d82418546455913a747d3a48b64fc [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/* 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.lice873192023-11-08 17:10:35 -080022#include <linux/soc/zte/otp/otp_zx.h>
lh9ed821d2023-04-07 01:36:19 -070023
24#define READ_ZLOADER_FLAG_SIZE 0x800
25#define WRITE_ZLOADER_FLAG_SIZE 0x3000
26
27
28extern struct mtd_info *mtd_fota;
29extern int g_zload_read_only_flag;
xf.lice873192023-11-08 17:10:35 -080030extern char *nor_cmdline;
31unsigned char nor_flag = 0;
lh9ed821d2023-04-07 01:36:19 -070032
33#ifndef USE_CPPS_KO
34extern unsigned int zOss_NvItemRead(unsigned int NvItemID, unsigned char *NvItemData, unsigned int NvItemLen);
35extern unsigned int zOss_NvItemWrite(unsigned int NvItemID, unsigned char *NvItemData, unsigned int NvItemLen);
36#endif
37
38/*zdd need modify*/
39int zDrvNand_ChangeNvrAttr(unsigned int rw )
40{
41 int ret = 0;
42 // ret = nand_nv_mid_r_set_to_rw(rw);
43 return ret;
44}
45EXPORT_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*/
53int 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}
65EXPORT_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*/
73int 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}
85EXPORT_SYMBOL(zDrvNand_Program);
86
87/*zdd need modify*/
88int 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*/
98int 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*/
108int 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*/
131int 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*/
153int 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*/
169int 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*/
185int 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}
194EXPORT_SYMBOL(zDrvNand_NvRwEccMake);
195
196/*zdd need modify*/
197int zDrvNand_EccMake(unsigned char* partName,unsigned int dwStart, unsigned int dwLen)
198{
199 return 0;
200}
201EXPORT_SYMBOL(zDrvNand_EccMake);
202
203/*
204* get the bootflag from flash.
205* return: 0, dl off 1, dl on
206*/
207unsigned 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.lice873192023-11-08 17:10:35 -0800218 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 }
lh9ed821d2023-04-07 01:36:19 -0700249 else
250 {
xf.lice873192023-11-08 17:10:35 -0800251 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
lh9ed821d2023-04-07 01:36:19 -0700271
272 return bootflag;
273}
274EXPORT_SYMBOL(zDrvNand_ReadBootflag);
275
276/*
277* write the bootflag to flash.
278* flag: 0,dl off else dl on
279*/
280unsigned 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;
lh9ed821d2023-04-07 01:36:19 -0700288
xf.lice873192023-11-08 17:10:35 -0800289 if(nor_cmdline != NULL)
lh9ed821d2023-04-07 01:36:19 -0700290 {
xf.lice873192023-11-08 17:10:35 -0800291 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");
lh9ed821d2023-04-07 01:36:19 -0700300 }
xf.lice873192023-11-08 17:10:35 -0800301
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;
lh9ed821d2023-04-07 01:36:19 -0700310
xf.lice873192023-11-08 17:10:35 -0800311 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 }
lh9ed821d2023-04-07 01:36:19 -0700369
370 return 0;
371
372}
373EXPORT_SYMBOL(zDrvNand_WriteBootflag);
374
375/*
376* get the usbtimeout flag from flash.
377* return: should be 1~10
378*/
379unsigned 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}
402EXPORT_SYMBOL(zDrvNand_ReadUsbtimeout);
403
404/*
405* write the usbtimeout flag to flash.
406* flag: should be 1~10
407*/
408unsigned 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}
445EXPORT_SYMBOL(zDrvNand_WriteUsbtimeout);
446