blob: c7b418a820bfea894a3762a52516acf1ace8e663 [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>
22
23#define READ_ZLOADER_FLAG_SIZE 0x800
24#define WRITE_ZLOADER_FLAG_SIZE 0x3000
25
26
27extern struct mtd_info *mtd_fota;
28extern int g_zload_read_only_flag;
29
30#ifndef USE_CPPS_KO
31extern unsigned int zOss_NvItemRead(unsigned int NvItemID, unsigned char *NvItemData, unsigned int NvItemLen);
32extern unsigned int zOss_NvItemWrite(unsigned int NvItemID, unsigned char *NvItemData, unsigned int NvItemLen);
33#endif
34
35/*zdd need modify*/
36int zDrvNand_ChangeNvrAttr(unsigned int rw )
37{
38 int ret = 0;
39 // ret = nand_nv_mid_r_set_to_rw(rw);
40 return ret;
41}
42EXPORT_SYMBOL(zDrvNand_ChangeNvrAttr);
43
44/*
45* nv read interface.
46* dwstart: nv addr to read
47* dwLen: length of nv to read
48* to: memory addr
49*/
50int zDrvNand_Read(unsigned int dwStart, unsigned int dwLen, unsigned char* to)
51{
52 int ret = 0;
53
54 #ifdef USE_CPPS_KO
55 ret = cpps_callbacks.zOss_NvItemRead(dwStart, to, dwLen);
56 #else
57 ret = zOss_NvItemRead(dwStart, to, dwLen);
58 #endif
59
60 return ret;
61}
62EXPORT_SYMBOL(zDrvNand_Read);
63
64/*
65* nv write interface.
66* dwstart: nv addr to program
67* dwLen: length of nv to program
68* from: memory addr
69*/
70int zDrvNand_Program(unsigned int dwStart, unsigned int dwLen, unsigned char* from)
71{
72 int ret = 0;
73
74 #ifdef USE_CPPS_KO
75 ret = cpps_callbacks.zOss_NvItemWrite(dwStart,from, dwLen);
76 #else
77 ret = zOss_NvItemWrite(dwStart,from, dwLen);
78 #endif
79
80 return ret;
81}
82EXPORT_SYMBOL(zDrvNand_Program);
83
84/*zdd need modify*/
85int zDrvNand_SmsRead(unsigned int dwStart, unsigned int dwLen, unsigned char* to)
86{
87 int ret = 0;
88
89 //ret = zftl_wrapper_read((char *)"sms", dwStart, dwLen, to);
90
91 return ret;
92}
93
94/*zdd need modify*/
95int zDrvNand_SmsProgram(unsigned int dwStart, unsigned int dwLen, unsigned char* from)
96{
97 int ret = 0;
98
99 //ret = zftl_wrapper_write((unsigned char *)"sms", dwStart, dwLen, from);
100
101 return ret;
102}
103
104/*zdd need modify*/
105int zDrvNand_SimNvRead(unsigned int dwStart, unsigned int dwLen, unsigned char* to)
106{
107 int ret = 0;
108#if 0//fyi
109 NAND_LOCK
110 nand_clk_gate(SYSCLK_ENABLE);
111 ret = nand_simnv_read(dwStart, dwLen, to);
112 nand_clk_gate(SYSCLK_DISABLE);
113 NAND_UNLOCK
114
115 if(ret == -1)
116 {
117 zftl_res = 1;
118 zDrvNand_SetSimNvFlag(dwStart);/*ECC REBOOT*/
119 zftl_res = 0;
120 }
121#endif
122 //ret = zftl_wrapper_read((unsigned char *)"simnv", dwStart, dwLen, to);
123
124 return ret;
125}
126
127/*zdd need modify*/
128int zDrvNand_SimNvProgram(unsigned int dwStart, unsigned int dwLen, unsigned char* from)
129{
130 int ret = 0;
131#if 0//fyi
132 NAND_LOCK
133 nand_clk_gate(SYSCLK_ENABLE);
134 ret = nand_simnv_program(dwStart, dwLen, from);
135 nand_clk_gate(SYSCLK_DISABLE);
136 NAND_UNLOCK
137 if(ret == -1)
138 {
139 zftl_res = 1;
140 zDrvNand_SetSimNvFlag(dwStart);/*ECC REBOOT*/
141 zftl_res = 0;
142 }
143#endif
144 //ret = zftl_wrapper_write((unsigned char *)"simnv", dwStart, dwLen, from);
145
146 return ret;
147}
148
149/*zdd need modify*/
150int zDrvNand_SimNvFacRead(unsigned int dwStart, unsigned int dwLen, unsigned char* to)
151{
152 int ret = 0;
153#if 0//fyi
154 NAND_LOCK
155 nand_clk_gate(SYSCLK_ENABLE);
156 ret = nand_simnvfac_read(dwStart, dwLen, to);
157 nand_clk_gate(SYSCLK_DISABLE);
158 NAND_UNLOCK
159#endif
160 //ret = zftl_wrapper_read((unsigned char *)"simnvfac", dwStart, dwLen, to);
161
162 return ret;
163}
164
165/*zdd need modify*/
166int zDrvNand_SimNvFacProgram(unsigned int dwStart, unsigned int dwLen, unsigned char* from)
167{
168 int ret = 0;
169#if 0//fyi
170 NAND_LOCK
171 nand_clk_gate(SYSCLK_ENABLE);
172 ret = nand_simnvfac_program(dwStart, dwLen, from);
173 nand_clk_gate(SYSCLK_DISABLE);
174 NAND_UNLOCK
175#endif
176 //ret = zftl_wrapper_write((unsigned char *)"simnvfac", dwStart, dwLen, from);
177
178 return ret;
179}
180
181/*zdd need modify*/
182int zDrvNand_NvRwEccMake(unsigned int dwStart, unsigned int dwLen)
183{
184 char buffer[2048];
185
186 //if( dwStart >= NVRW_SIZE||dwStart < 0 || (dwStart +dwLen)> NVRW_SIZE|| dwLen > 2048)
187 // return -1;
188//fyi nand_nvrw_mid_ecc_make(dwStart,dwLen,buffer);
189 return 0;
190}
191EXPORT_SYMBOL(zDrvNand_NvRwEccMake);
192
193/*zdd need modify*/
194int zDrvNand_EccMake(unsigned char* partName,unsigned int dwStart, unsigned int dwLen)
195{
196 return 0;
197}
198EXPORT_SYMBOL(zDrvNand_EccMake);
199
200/*
201* get the bootflag from flash.
202* return: 0, dl off 1, dl on
203*/
204unsigned int zDrvNand_ReadBootflag( void )
205{
206 unsigned int bootflag = 0;
207
208 char value = 0;
209 int retlen = 0;
210
211 unsigned char *buffer = kzalloc(READ_ZLOADER_FLAG_SIZE,GFP_KERNEL);
212 if( buffer == NULL )
213 return -1;
214
215 if(mtd_read(mtd_fota,0,READ_ZLOADER_FLAG_SIZE,&retlen,buffer))/* BOOTFLAGÔÚnandµÄµÚ20~27×Ö½Ú */
216 {
217 kfree(buffer);
218 return 1;
219 }
220 memcpy(&value, buffer+2, 1);
221 bootflag = value;
222
223 if( bootflag == 0x5a)
224 {
225 bootflag = 1;
226 }
227 else
228 {
229 bootflag = 0;
230 }
231 kfree(buffer);
232
233 return bootflag;
234}
235EXPORT_SYMBOL(zDrvNand_ReadBootflag);
236
237/*
238* write the bootflag to flash.
239* flag: 0,dl off else dl on
240*/
241unsigned int zDrvNand_WriteBootflag(unsigned int flag)
242{
243
244 unsigned int ret = 0;
245 char value = 0;
246 int bootflag = 0;
247 int retlen = 0;
248 struct erase_info ei;
249
250 unsigned char *buffer = kzalloc(WRITE_ZLOADER_FLAG_SIZE,GFP_KERNEL);
251
252 if( buffer == NULL )
253 return -1;
254
255 if(mtd_read(mtd_fota,0,WRITE_ZLOADER_FLAG_SIZE,&retlen,buffer))/* BOOTFLAGÔÚnandµÄµÚ20~27×Ö½Ú */
256 {
257 kfree(buffer);
258 return -1;
259 }
260 if(flag == 0 )
261 {
262 bootflag = 0x00;
263 memset(&value, bootflag, 1);
264 }
265 else
266 {
267 bootflag = 0x5a;
268 memset(&value, bootflag, 1);
269 }
270
271 memcpy(buffer+2, &value, 1);
272 memset(&ei, 0, sizeof(struct erase_info));
273 ei.mtd = mtd_fota;
274 ei.addr = 0;
275 ei.len = mtd_fota->erasesize;
276 g_zload_read_only_flag = 1;
277 ret = mtd_erase(mtd_fota, &ei); /*²Á³ýµÚÒ»¿é*/
278 ret = mtd_write(mtd_fota,0,WRITE_ZLOADER_FLAG_SIZE,&retlen,buffer);
279 g_zload_read_only_flag = 0;
280 kfree(buffer);
281
282 return 0;
283
284}
285EXPORT_SYMBOL(zDrvNand_WriteBootflag);
286
287/*
288* get the usbtimeout flag from flash.
289* return: should be 1~10
290*/
291unsigned int zDrvNand_ReadUsbtimeout( void )
292{
293
294 unsigned int usbtimeout = 0;
295 char value = 0;
296 int retlen = 0;
297
298 unsigned char *buffer = kzalloc(READ_ZLOADER_FLAG_SIZE,GFP_KERNEL);
299 if( buffer == NULL )
300 return -1;
301
302 if(mtd_read(mtd_fota,0,READ_ZLOADER_FLAG_SIZE,&retlen,buffer))/* BOOTFLAGÔÚnandµÄµÚ20~27×Ö½Ú */
303 {
304 kfree(buffer);
305 return -1;
306 }
307 memcpy(&value, buffer+3, 1);
308 usbtimeout = value;
309
310 kfree(buffer);
311 return usbtimeout;
312
313}
314EXPORT_SYMBOL(zDrvNand_ReadUsbtimeout);
315
316/*
317* write the usbtimeout flag to flash.
318* flag: should be 1~10
319*/
320unsigned int zDrvNand_WriteUsbtimeout( unsigned int flag )
321{
322
323 unsigned int ret = 0;
324 uint8_t oob[256];
325 int i =0;
326 int times = 0;
327 int retlen = 0;
328 char value = 0;
329 struct erase_info ei;
330
331 unsigned char *buffer = kzalloc(WRITE_ZLOADER_FLAG_SIZE,GFP_KERNEL); /* ZLOADERºÍ·ÖÇø±í¹²8192BYTES */
332
333 if( buffer == NULL )
334 return -1;
335
336 if(mtd_read(mtd_fota,0,WRITE_ZLOADER_FLAG_SIZE,&retlen,buffer))/* BOOTFLAGÔÚnandµÄµÚ20~27×Ö½Ú */
337 {
338 kfree(buffer);
339 return -1;
340 }
341
342 memset(&value, flag, 1);
343 memcpy(buffer+3, &value, 1);
344
345 memset(&ei, 0, sizeof(struct erase_info));
346 ei.mtd = mtd_fota;
347 ei.addr = 0;
348 ei.len = mtd_fota->erasesize;
349 g_zload_read_only_flag = 1;
350 ret = mtd_erase(mtd_fota, &ei); /*²Á³ýµÚÒ»¿é*/
351 ret = mtd_write(mtd_fota,0,WRITE_ZLOADER_FLAG_SIZE,&retlen,buffer);
352 g_zload_read_only_flag = 0;
353 kfree(buffer);
354
355 return ret;
356}
357EXPORT_SYMBOL(zDrvNand_WriteUsbtimeout);
358