PageRenderTime 55ms CodeModel.GetById 24ms app.highlight 26ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/block/sunxi_nand/nfc/nfc_w.c

https://bitbucket.org/ndreys/linux-sunxi
C | 955 lines | 532 code | 204 blank | 219 comment | 55 complexity | 4e2149b98d93fc77c4aa5aab05edf5ab MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1/*
  2 * drivers/block/sunxi_nand/nfc/nfc_w.c
  3 *
  4 * (C) Copyright 2007-2012
  5 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
  6 *
  7 * This program is free software; you can redistribute it and/or
  8 * modify it under the terms of the GNU General Public License as
  9 * published by the Free Software Foundation; either version 2 of
 10 * the License, or (at your option) any later version.
 11 *
 12 * This program is distributed in the hope that it will be useful,
 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
 15 * GNU General Public License for more details.
 16 *
 17 * You should have received a copy of the GNU General Public License
 18 * along with this program; if not, write to the Free Software
 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 20 * MA 02111-1307 USA
 21 */
 22
 23#include "nfc_i.h"
 24
 25extern __u32 pagesize;
 26extern __s32 _wait_cmdfifo_free(void);
 27extern __s32 _wait_cmd_finish(void);
 28extern void _dma_config_start(__u8 rw, __u32 buff_addr, __u32 len);
 29extern __s32 _wait_dma_end(void);
 30extern __s32 _check_ecc(__u32 eblock_cnt);
 31extern void _disable_ecc(void);
 32extern void _enable_ecc(__u32 pipline);
 33extern __s32 _enter_nand_critical(void);
 34extern __s32 _exit_nand_critical(void);
 35extern void _set_addr(__u8 *addr, __u8 cnt);
 36extern __s32 nfc_set_cmd_register(NFC_CMD_LIST *cmd);
 37extern __s32 _read_in_page_mode(NFC_CMD_LIST  *rcmd,void *mainbuf,void *sparebuf,__u8 dma_wait_mode);
 38extern __s32 _read_in_normal_mode(NFC_CMD_LIST  *rcmd, __u8 *mainbuf, __u8 *sparebuf,__u8 dma_wait_mode);
 39
 40
 41extern __u8 read_retry_reg_adr[READ_RETRY_MAX_REG_NUM];
 42extern __u8 read_retry_default_val[8][READ_RETRY_MAX_REG_NUM];
 43extern __s16 read_retry_val[READ_RETRY_MAX_CYCLE][READ_RETRY_MAX_REG_NUM];
 44extern __u8 read_retry_mode;
 45extern __u8 read_retry_cycle;
 46extern __u8 read_retry_reg_num;
 47
 48extern __u8 lsb_mode_reg_adr[LSB_MODE_MAX_REG_NUM];
 49extern __u8 lsb_mode_default_val[LSB_MODE_MAX_REG_NUM];
 50extern __u8 lsb_mode_val[LSB_MODE_MAX_REG_NUM];
 51extern __u8 lsb_mode_reg_num;
 52
 53extern __s32 _vender_get_param(__u8 *para, __u8 *addr, __u32 count);
 54extern __s32 _vender_set_param(__u8 *para, __u8 *addr, __u32 count);
 55extern __s32 _vender_pre_condition(void);
 56
 57
 58/*after send write or erase command, must wait rb from ready to busy, then can send status command
 59	because nfc not do this, so software delay by xr, 2009-3-25*/
 60
 61void _wait_twb(void)
 62{
 63/*
 64	__u32 timeout = 800;
 65
 66	while ( (timeout--) && !(NFC_READ_REG(NFC_REG_ST) & NFC_CMD_FIFO_STATUS));
 67*/
 68}
 69
 70/*******************************************************************************
 71*								NFC_Write
 72*
 73* Description 	: write one page data into flash in single plane mode.
 74* Arguments	: *wcmd	-- the write command sequence list head?&#x20AC;&#x201A;
 75*			  *mainbuf	-- point to data buffer address, 	it must be four bytes align.
 76*                     *sparebuf	-- point to spare buffer address.
 77*                     dma_wait_mode	-- how to deal when dma start, 0 = wait till dma finish,
 78							    1 = dma interrupt was set and now sleep till interrupt occurs.
 79*			  rb_wait_mode -- 0 = do not care rb, 1 = set rb interrupt and do not wait rb ready.
 80*			  page_mode  -- 0 = common command, 1 = page command.
 81* Returns		: 0 = success.
 82			  -1 = fail.
 83* Notes		: the unit must be page, so if  page_mode is not 1, return fail,the function exits without checking status,
 84			  if the commands do not fetch data,ecc is not neccesary.
 85********************************************************************************/
 86__s32 NFC_Write( NFC_CMD_LIST  *wcmd, void *mainbuf, void *sparebuf,  __u8 dma_wait_mode, __u8 rb_wait_mode,
 87				    __u8 page_mode)
 88{
 89	__s32 ret;
 90	__s32 i;
 91	__u32 cfg;
 92	__u32 program_cmd,random_program_cmd;
 93	NFC_CMD_LIST *cur_cmd,*program_addr_cmd;
 94
 95	if (page_mode == 0){
 96		return -1;
 97	}
 98
 99	ret = 0;
100	_enter_nand_critical();
101
102	/*write in page_mode*/
103	program_addr_cmd = wcmd;
104	cur_cmd = wcmd;
105	cur_cmd = cur_cmd->next;
106	random_program_cmd = cur_cmd->value;
107	cur_cmd = cur_cmd->next;
108	program_cmd = cur_cmd->value;
109
110	//access NFC internal RAM by DMA bus
111	NFC_WRITE_REG(NFC_REG_CTL, (NFC_READ_REG(NFC_REG_CTL)) | NFC_RAM_METHOD);
112
113	/*set dma and run*/
114//	if (NFC_IS_SDRAM((__u32)mainbuf))
115//		attr = 0x2930281;
116//	else
117//		attr = 0x2930280;
118
119	_dma_config_start(1, (__u32)mainbuf, pagesize);
120
121	/*wait cmd fifo free*/
122	ret = _wait_cmdfifo_free();
123	if (ret){
124		_exit_nand_critical();
125		return ret;
126	}
127	/*set NFC_REG_CNT*/
128  NFC_WRITE_REG(NFC_REG_CNT,1024);
129
130	/*set NFC_REG_RCMD_SET*/
131	cfg = 0;
132	cfg |= (program_cmd & 0xff);
133	cfg |= ((random_program_cmd & 0xff) << 8);
134	NFC_WRITE_REG(NFC_REG_WCMD_SET, cfg);
135
136	/*set NFC_REG_SECTOR_NUM*/
137	NFC_WRITE_REG(NFC_REG_SECTOR_NUM, pagesize/1024);
138
139	/*set user data*/
140	for (i = 0; i < pagesize/1024;  i++){
141		NFC_WRITE_REG(NFC_REG_USER_DATA(i), *((__u32 *)sparebuf + i) );
142	}
143
144	/*set addr*/
145	_set_addr(program_addr_cmd->addr,program_addr_cmd->addr_cycle);
146
147	/*set NFC_REG_CMD*/
148	cfg  = 0;
149	 /*set sequence mode*/
150	//cfg |= 0x1<<25;
151	cfg |= program_addr_cmd->value;
152	cfg |= ( (program_addr_cmd->addr_cycle - 1) << 16);
153	//cfg |= (NFC_SEND_ADR | NFC_ACCESS_DIR | NFC_DATA_TRANS | NFC_SEND_CMD | NFC_WAIT_FLAG | NFC_DATA_SWAP_METHOD);
154	cfg |= (NFC_SEND_ADR | NFC_ACCESS_DIR | NFC_DATA_TRANS | NFC_SEND_CMD1 | NFC_SEND_CMD2 | NFC_DATA_SWAP_METHOD);
155	cfg |= ((__u32)0x2 << 30);//page command
156	if (pagesize/1024 == 1)
157		cfg |= NFC_SEQ;
158
159	/*enable ecc*/
160	_enable_ecc(1);
161	NFC_WRITE_REG(NFC_REG_CMD,cfg);
162
163    NAND_WaitDmaFinish();
164
165	_wait_twb();
166	_wait_cmdfifo_free();
167	_wait_cmd_finish();
168
169	/*start dma?*/
170	/*if dma mode is wait*/
171	if(0 == dma_wait_mode){
172		ret = _wait_dma_end();
173	}
174
175	/*disable ecc*/
176	_disable_ecc();
177
178	/*switch to ahb*/
179	NFC_WRITE_REG(NFC_REG_CTL, (NFC_READ_REG(NFC_REG_CTL)) & (~NFC_RAM_METHOD));
180
181	_exit_nand_critical();
182	return ret;
183}
184
185__s32 NFC_Write_Seq( NFC_CMD_LIST  *wcmd, void *mainbuf, void *sparebuf,  __u8 dma_wait_mode, __u8 rb_wait_mode,
186				    __u8 page_mode)
187{
188	__s32 ret;
189	__s32 i;
190	__u32 cfg;
191	__u32 ecc_mode_temp;
192	__u32 program_cmd,random_program_cmd;
193	NFC_CMD_LIST *cur_cmd,*program_addr_cmd;
194
195	if (page_mode == 0){
196		return -1;
197	}
198
199	ret = 0;
200	_enter_nand_critical();
201
202	/*write in page_mode*/
203	program_addr_cmd = wcmd;
204	cur_cmd = wcmd;
205	cur_cmd = cur_cmd->next;
206	random_program_cmd = cur_cmd->value;
207	cur_cmd = cur_cmd->next;
208	program_cmd = cur_cmd->value;
209
210	//access NFC internal RAM by DMA bus
211	NFC_WRITE_REG(NFC_REG_CTL, (NFC_READ_REG(NFC_REG_CTL)) | NFC_RAM_METHOD);
212
213//	/*set dma and run*/
214//	if (NFC_IS_SDRAM((__u32)mainbuf))
215//		attr = 0x2930281;
216//	else
217//		attr = 0x2930280;
218
219	_dma_config_start(1, (__u32)mainbuf, pagesize);
220
221	/*wait cmd fifo free*/
222	ret = _wait_cmdfifo_free();
223	if (ret){
224		_exit_nand_critical();
225		return ret;
226	}
227	/*set NFC_REG_CNT*/
228  NFC_WRITE_REG(NFC_REG_CNT,1024);
229
230	/*set NFC_REG_RCMD_SET*/
231	cfg = 0;
232	cfg |= (program_cmd & 0xff);
233	cfg |= ((random_program_cmd & 0xff) << 8);
234	NFC_WRITE_REG(NFC_REG_WCMD_SET, cfg);
235
236	/*set NFC_REG_SECTOR_NUM*/
237	NFC_WRITE_REG(NFC_REG_SECTOR_NUM, pagesize/1024);
238
239	/*set user data*/
240	for (i = 0; i < pagesize/1024;  i++){
241		NFC_WRITE_REG(NFC_REG_USER_DATA(i), *((__u32 *)sparebuf + i) );
242	}
243
244	/*set addr*/
245	_set_addr(program_addr_cmd->addr,program_addr_cmd->addr_cycle);
246
247	/*set NFC_REG_CMD*/
248	cfg  = 0;
249	 /*set sequence mode*/
250	cfg |= 0x1<<25;
251	cfg |= program_addr_cmd->value;
252	cfg |= ( (program_addr_cmd->addr_cycle - 1) << 16);
253	//cfg |= (NFC_SEND_ADR | NFC_ACCESS_DIR | NFC_DATA_TRANS | NFC_SEND_CMD | NFC_WAIT_FLAG | NFC_DATA_SWAP_METHOD);
254	cfg |= (NFC_SEND_ADR | NFC_ACCESS_DIR | NFC_DATA_TRANS | NFC_SEND_CMD1 | NFC_SEND_CMD2 | NFC_DATA_SWAP_METHOD);
255	cfg |= ((__u32)0x2 << 30);//page command
256	if (pagesize/1024 == 1)
257		cfg |= NFC_SEQ;
258
259	/*enable ecc*/
260	_enable_ecc(1);
261
262	/*set ecc to 24-bit ecc*/
263    ecc_mode_temp = NFC_READ_REG(NFC_REG_ECC_CTL) & 0xf000;
264	NFC_WRITE_REG(NFC_REG_ECC_CTL, ((NFC_READ_REG(NFC_REG_ECC_CTL) & (~NFC_ECC_MODE))|(0x1<<12) ));
265
266	NFC_WRITE_REG(NFC_REG_CMD,cfg);
267
268    NAND_WaitDmaFinish();
269
270	_wait_twb();
271	_wait_cmdfifo_free();
272	_wait_cmd_finish();
273
274	/*start dma?*/
275	/*if dma mode is wait*/
276	if(0 == dma_wait_mode){
277		ret = _wait_dma_end();
278	}
279
280	/*disable ecc*/
281	_disable_ecc();
282
283	/*set ecc to original value*/
284	NFC_WRITE_REG(NFC_REG_ECC_CTL, (NFC_READ_REG(NFC_REG_ECC_CTL) & (~NFC_ECC_MODE))|ecc_mode_temp);
285
286	/*switch to ahb*/
287	NFC_WRITE_REG(NFC_REG_CTL, (NFC_READ_REG(NFC_REG_CTL)) & (~NFC_RAM_METHOD));
288
289	_exit_nand_critical();
290	return ret;
291}
292
293__s32 NFC_Write_1K( NFC_CMD_LIST  *wcmd, void *mainbuf, void *sparebuf,  __u8 dma_wait_mode, __u8 rb_wait_mode,
294				    __u8 page_mode)
295{
296	__s32 ret;
297	__s32 i;
298	__u32 cfg;
299	__u32 page_size_temp, ecc_mode_temp;
300	__u32 program_cmd,random_program_cmd;
301	NFC_CMD_LIST *cur_cmd,*program_addr_cmd;
302
303	if (page_mode == 0){
304		return -1;
305	}
306
307	ret = 0;
308	_enter_nand_critical();
309
310	/*write in page_mode*/
311	program_addr_cmd = wcmd;
312	cur_cmd = wcmd;
313	cur_cmd = cur_cmd->next;
314	random_program_cmd = cur_cmd->value;
315	cur_cmd = cur_cmd->next;
316	program_cmd = cur_cmd->value;
317
318	//access NFC internal RAM by DMA bus
319	NFC_WRITE_REG(NFC_REG_CTL, (NFC_READ_REG(NFC_REG_CTL)) | NFC_RAM_METHOD);
320
321	//set pagesize to 1K
322      page_size_temp = (NFC_READ_REG(NFC_REG_CTL) & 0xf00)>>8;
323	NFC_WRITE_REG(NFC_REG_CTL, (NFC_READ_REG(NFC_REG_CTL)) | (0x3<<8));
324
325//	/*set dma and run*/
326//	if (NFC_IS_SDRAM((__u32)mainbuf))
327//		attr = 0x2930281;
328//	else
329//		attr = 0x2930280;
330
331	_dma_config_start(1, (__u32)mainbuf, 1024);
332
333	/*wait cmd fifo free*/
334	ret = _wait_cmdfifo_free();
335	if (ret){
336		_exit_nand_critical();
337		return ret;
338	}
339	/*set NFC_REG_CNT*/
340  NFC_WRITE_REG(NFC_REG_CNT,1024);
341
342	/*set NFC_REG_RCMD_SET*/
343	cfg = 0;
344	cfg |= (program_cmd & 0xff);
345	cfg |= ((random_program_cmd & 0xff) << 8);
346	NFC_WRITE_REG(NFC_REG_WCMD_SET, cfg);
347
348	/*set NFC_REG_SECTOR_NUM*/
349	NFC_WRITE_REG(NFC_REG_SECTOR_NUM, 1024/1024);
350
351	/*set user data*/
352	for (i = 0; i < 1024/1024;  i++){
353		NFC_WRITE_REG(NFC_REG_USER_DATA(i), *((__u32 *)sparebuf + i) );
354	}
355
356	/*set addr*/
357	_set_addr(program_addr_cmd->addr,program_addr_cmd->addr_cycle);
358
359	/*set NFC_REG_CMD*/
360	cfg  = 0;
361	 /*set sequence mode*/
362	cfg |= 0x1<<25;
363	cfg |= program_addr_cmd->value;
364	cfg |= ( (program_addr_cmd->addr_cycle - 1) << 16);
365	//cfg |= (NFC_SEND_ADR | NFC_ACCESS_DIR | NFC_DATA_TRANS | NFC_SEND_CMD | NFC_WAIT_FLAG | NFC_DATA_SWAP_METHOD);
366	cfg |= (NFC_SEND_ADR | NFC_ACCESS_DIR | NFC_DATA_TRANS | NFC_SEND_CMD1 | NFC_SEND_CMD2 | NFC_DATA_SWAP_METHOD);
367	cfg |= ((__u32)0x2 << 30);//page command
368	if (pagesize/1024 == 1)
369		cfg |= NFC_SEQ;
370
371	/*enable ecc*/
372	_enable_ecc(1);
373
374	/*set ecc to 64-bit ecc*/
375    ecc_mode_temp = NFC_READ_REG(NFC_REG_ECC_CTL) & 0xf000;
376	NFC_WRITE_REG(NFC_REG_ECC_CTL, ((NFC_READ_REG(NFC_REG_ECC_CTL) & (~NFC_ECC_MODE))|(0x8<<12) ));
377	NFC_WRITE_REG(NFC_REG_CMD,cfg);
378
379    NAND_WaitDmaFinish();
380
381	_wait_twb();
382	_wait_cmdfifo_free();
383	_wait_cmd_finish();
384
385	/*start dma?*/
386	/*if dma mode is wait*/
387	if(0 == dma_wait_mode){
388		ret = _wait_dma_end();
389	}
390
391
392
393	/*disable ecc*/
394	_disable_ecc();
395
396	/*set ecc to original value*/
397	NFC_WRITE_REG(NFC_REG_ECC_CTL, (NFC_READ_REG(NFC_REG_ECC_CTL) & (~NFC_ECC_MODE))|ecc_mode_temp);
398
399      /*set pagesize to original value*/
400      NFC_WRITE_REG(NFC_REG_CTL, ((NFC_READ_REG(NFC_REG_CTL)) & (~NFC_PAGE_SIZE)) | (page_size_temp<<8));
401
402	/*switch to ahb*/
403	NFC_WRITE_REG(NFC_REG_CTL, (NFC_READ_REG(NFC_REG_CTL)) & (~NFC_RAM_METHOD));
404
405	_exit_nand_critical();
406	return ret;
407}
408
409/*******************************************************************************
410*								NFC_Erase
411*
412* Description 	: erase one block in signle plane mode or multi plane mode.
413* Arguments	: *ecmd	-- the erase command sequence list head
414*			  rb_wait_mode  -- 0 = do not care rb, 1 = set rb interrupt and do not wait rb ready.
415* Returns		: 0 = success.
416			  -1 = fail.
417* Notes		: the unit must be page, so if  page_mode is not 1, return fail,the function exits without checking status,
418			  if the commands do not fetch data,ecc is not neccesary.
419********************************************************************************/
420__s32 NFC_Erase(NFC_CMD_LIST  *ecmd, __u8 rb_wait_mode)
421{
422
423	__s32 ret;
424
425	_enter_nand_critical();
426
427	ret = nfc_set_cmd_register(ecmd);
428	if (ret){
429		_exit_nand_critical();
430		return ret;
431	}
432	_wait_twb();
433	ret = _wait_cmdfifo_free();
434	ret |= _wait_cmd_finish();
435
436	_exit_nand_critical();
437
438	return ret;
439}
440
441/*******************************************************************************
442*								NFC_CopyBackRead
443*
444* Description 	: copyback read one page data inside flash in single plane mode or multi plane mode
445* Arguments	: *crcmd	-- the copyback read command sequence list head.
446* Returns		: 0 = success.
447			  -1 = fail.
448* Notes		: the unit must be page.
449********************************************************************************/
450__s32 NFC_CopyBackRead(NFC_CMD_LIST  *crcmd)
451{
452
453	__s32 ret;
454
455	_enter_nand_critical();
456	ret = nfc_set_cmd_register(crcmd);
457	if (ret){
458		_exit_nand_critical();
459		return ret;
460	}
461
462	ret = _wait_cmdfifo_free();
463	ret |= _wait_cmd_finish();
464
465	_exit_nand_critical();
466
467	return ret;
468}
469//#pragma arm section code="NFC_CopyBackWrite"
470/*******************************************************************************
471*								NFC_CopyBackWrite
472*
473* Description 	: copyback write one page data inside flash in single plane mode or multi plane mode
474* Arguments	: *cwcmd	-- the copyback read command sequence list head.
475 			  rb_wait_mode  -- 0 = do not care rb, 1 = set rb interrupt and do not wait rb ready.
476* Returns		: 0 = success.
477			  -1 = fail.
478* Notes		: the unit must be page.
479********************************************************************************/
480__s32 NFC_CopyBackWrite(NFC_CMD_LIST  *cwcmd, __u8 rb_wait_mode)
481{
482	__s32 ret;
483
484	_enter_nand_critical();
485
486	ret = nfc_set_cmd_register(cwcmd);
487	if (ret){
488		_exit_nand_critical();
489		return ret;
490	}
491	_wait_twb();
492	ret = _wait_cmdfifo_free();
493	ret |= _wait_cmd_finish();
494
495	_exit_nand_critical();
496
497	return ret;
498}
499
500__s32 _read_in_page_mode_seq(NFC_CMD_LIST  *rcmd,void *mainbuf,void *sparebuf,__u8 dma_wait_mode)
501{
502	__s32 ret,ret1;
503	__s32 i;
504	__u32 cfg;
505	NFC_CMD_LIST *cur_cmd,*read_addr_cmd;
506	__u32 read_data_cmd,random_read_cmd0,random_read_cmd1;
507	__u32 ecc_mode_temp;
508
509	ret = 0;
510	read_addr_cmd = rcmd;
511	cur_cmd = rcmd;
512	cur_cmd = cur_cmd->next;
513	random_read_cmd0 = cur_cmd->value;
514	cur_cmd = cur_cmd->next;
515	random_read_cmd1 = cur_cmd->value;
516	cur_cmd = cur_cmd->next;
517	read_data_cmd = cur_cmd->value;
518
519	//access NFC internal RAM by DMA bus
520	NFC_WRITE_REG(NFC_REG_CTL, (NFC_READ_REG(NFC_REG_CTL)) | NFC_RAM_METHOD);
521
522//	/*set dma and run*/
523//	/*sdram*/
524//	if (NFC_IS_SDRAM((__u32)mainbuf))
525//		attr = 0x2810293;
526//	/*sram*/
527//	else
528//		attr = 0x2800293;
529	_dma_config_start(0, (__u32)mainbuf, pagesize);
530
531	/*wait cmd fifo free*/
532	ret = _wait_cmdfifo_free();
533	if (ret)
534		return ret;
535
536	/*set NFC_REG_CNT*/
537	NFC_WRITE_REG(NFC_REG_CNT,1024);
538
539	/*set NFC_REG_RCMD_SET*/
540	cfg = 0;
541	cfg |= (read_data_cmd & 0xff);
542	cfg |= ((random_read_cmd0 & 0xff) << 8);
543	cfg |= ((random_read_cmd1 & 0xff) << 16);
544	NFC_WRITE_REG(NFC_REG_RCMD_SET, cfg);
545
546	/*set NFC_REG_SECTOR_NUM*/
547	NFC_WRITE_REG(NFC_REG_SECTOR_NUM, pagesize/1024);
548
549	/*set addr*/
550	_set_addr(read_addr_cmd->addr,read_addr_cmd->addr_cycle);
551
552	/*set NFC_REG_CMD*/
553	cfg  = 0;
554	cfg |= read_addr_cmd->value;
555	/*set sequence mode*/
556	cfg |= 0x1<<25;
557	cfg |= ( (read_addr_cmd->addr_cycle - 1) << 16);
558	cfg |= (NFC_SEND_ADR | NFC_DATA_TRANS | NFC_SEND_CMD1 | NFC_SEND_CMD2 | NFC_WAIT_FLAG | NFC_DATA_SWAP_METHOD);
559	cfg |= ((__u32)0x2 << 30);//page command
560
561	if (pagesize/1024 == 1)
562		cfg |= NFC_SEQ;
563
564	/*enable ecc*/
565	_enable_ecc(1);
566
567	/*set ecc to 24-bit ecc*/
568    ecc_mode_temp = NFC_READ_REG(NFC_REG_ECC_CTL) & 0xf000;
569	NFC_WRITE_REG(NFC_REG_ECC_CTL, ((NFC_READ_REG(NFC_REG_ECC_CTL) & (~NFC_ECC_MODE))|(0x1<<12)));
570
571	NFC_WRITE_REG(NFC_REG_CMD,cfg);
572
573    NAND_WaitDmaFinish();
574
575	/*wait cmd fifo free and cmd finish*/
576	ret = _wait_cmdfifo_free();
577	ret |= _wait_cmd_finish();
578	if (ret){
579		_disable_ecc();
580		return ret;
581	}
582	/*get user data*/
583	for (i = 0; i < pagesize/1024;  i++){
584		*(((__u32*) sparebuf)+i) = NFC_READ_REG(NFC_REG_USER_DATA(i));
585	}
586
587	/*ecc check and disable ecc*/
588	ret = _check_ecc(pagesize/1024);
589	_disable_ecc();
590
591    /*set ecc to original value*/
592	NFC_WRITE_REG(NFC_REG_ECC_CTL, (NFC_READ_REG(NFC_REG_ECC_CTL) & (~NFC_ECC_MODE))|ecc_mode_temp);
593
594	/*if dma mode is wait*/
595	if(0 == dma_wait_mode){
596		ret1 = _wait_dma_end();
597		if (ret1)
598			return ret1;
599	}
600
601	return ret;
602}
603
604
605__s32 _read_in_page_mode_1K(NFC_CMD_LIST  *rcmd,void *mainbuf,void *sparebuf,__u8 dma_wait_mode)
606{
607	__s32 ret,ret1;
608	__s32 i;
609	__u32 cfg;
610	NFC_CMD_LIST *cur_cmd,*read_addr_cmd;
611	__u32 read_data_cmd,random_read_cmd0,random_read_cmd1;
612	__u32 page_size_temp, ecc_mode_temp;
613
614	ret = 0;
615	read_addr_cmd = rcmd;
616	cur_cmd = rcmd;
617	cur_cmd = cur_cmd->next;
618	random_read_cmd0 = cur_cmd->value;
619	cur_cmd = cur_cmd->next;
620	random_read_cmd1 = cur_cmd->value;
621	cur_cmd = cur_cmd->next;
622	read_data_cmd = cur_cmd->value;
623
624	//access NFC internal RAM by DMA bus
625	NFC_WRITE_REG(NFC_REG_CTL, (NFC_READ_REG(NFC_REG_CTL)) | NFC_RAM_METHOD);
626
627	//set pagesize to 1K
628      page_size_temp = (NFC_READ_REG(NFC_REG_CTL) & 0xf00)>>8;
629	NFC_WRITE_REG(NFC_REG_CTL, (NFC_READ_REG(NFC_REG_CTL)) | (0x3<<8));
630
631	///*set dma and run*/
632	///*sdram*/
633	//if (NFC_IS_SDRAM((__u32)mainbuf))
634	//	attr = 0x2810293;
635	///*sram*/
636	//else
637	//	attr = 0x2800293;
638	_dma_config_start(0, (__u32)mainbuf, 1024);
639
640	/*wait cmd fifo free*/
641	ret = _wait_cmdfifo_free();
642	if (ret)
643		return ret;
644
645	/*set NFC_REG_CNT*/
646	NFC_WRITE_REG(NFC_REG_CNT,1024);
647
648	/*set NFC_REG_RCMD_SET*/
649	cfg = 0;
650	cfg |= (read_data_cmd & 0xff);
651	cfg |= ((random_read_cmd0 & 0xff) << 8);
652	cfg |= ((random_read_cmd1 & 0xff) << 16);
653	NFC_WRITE_REG(NFC_REG_RCMD_SET, cfg);
654
655	/*set NFC_REG_SECTOR_NUM*/
656	NFC_WRITE_REG(NFC_REG_SECTOR_NUM, 1024/1024);
657
658	/*set addr*/
659	_set_addr(read_addr_cmd->addr,read_addr_cmd->addr_cycle);
660
661	/*set NFC_REG_CMD*/
662	cfg  = 0;
663	cfg |= read_addr_cmd->value;
664	/*set sequence mode*/
665	cfg |= 0x1<<25;
666	cfg |= ( (read_addr_cmd->addr_cycle - 1) << 16);
667	cfg |= (NFC_SEND_ADR | NFC_DATA_TRANS | NFC_SEND_CMD1 | NFC_SEND_CMD2 | NFC_WAIT_FLAG | NFC_DATA_SWAP_METHOD);
668	cfg |= ((__u32)0x2 << 30);//page command
669
670	if (1024/1024 == 1)
671		cfg |= NFC_SEQ;
672
673	/*enable ecc*/
674	_enable_ecc(1);
675
676	/*set ecc to 64-bit ecc*/
677    ecc_mode_temp = NFC_READ_REG(NFC_REG_ECC_CTL) & 0xf000;
678	NFC_WRITE_REG(NFC_REG_ECC_CTL, ((NFC_READ_REG(NFC_REG_ECC_CTL) & (~NFC_ECC_MODE))|(0x8<<12) ));
679
680	NFC_WRITE_REG(NFC_REG_CMD,cfg);
681
682    NAND_WaitDmaFinish();
683
684	/*wait cmd fifo free and cmd finish*/
685	ret = _wait_cmdfifo_free();
686	ret |= _wait_cmd_finish();
687	if (ret){
688		_disable_ecc();
689		return ret;
690	}
691	/*get user data*/
692	for (i = 0; i < 1024/1024;  i++){
693		*(((__u32*) sparebuf)+i) = NFC_READ_REG(NFC_REG_USER_DATA(i));
694	}
695
696	/*ecc check and disable ecc*/
697	ret = _check_ecc(pagesize/1024);
698	_disable_ecc();
699
700	/*if dma mode is wait*/
701	if(0 == dma_wait_mode){
702		ret1 = _wait_dma_end();
703		if (ret1)
704			return ret1;
705	}
706
707	/*set ecc to original value*/
708	NFC_WRITE_REG(NFC_REG_ECC_CTL, (NFC_READ_REG(NFC_REG_ECC_CTL) & (~NFC_ECC_MODE))|ecc_mode_temp);
709
710    /*set pagesize to original value*/
711    NFC_WRITE_REG(NFC_REG_CTL, ((NFC_READ_REG(NFC_REG_CTL)) & (~NFC_PAGE_SIZE)) | (page_size_temp<<8));
712
713	return ret;
714}
715
716
717__s32 _read_in_page_mode_spare(NFC_CMD_LIST  *rcmd,void *mainbuf,void *sparebuf,__u8 dma_wait_mode)
718{
719	__s32 ret,ret1;
720	__s32 i;
721	__u32 cfg;
722	NFC_CMD_LIST *cur_cmd,*read_addr_cmd;
723	__u32 read_data_cmd,random_read_cmd0,random_read_cmd1;
724
725	ret = 0;
726	read_addr_cmd = rcmd;
727	cur_cmd = rcmd;
728	cur_cmd = cur_cmd->next;
729	random_read_cmd0 = cur_cmd->value;
730	cur_cmd = cur_cmd->next;
731	random_read_cmd1 = cur_cmd->value;
732	cur_cmd = cur_cmd->next;
733	read_data_cmd = cur_cmd->value;
734
735	//access NFC internal RAM by DMA bus
736	NFC_WRITE_REG(NFC_REG_CTL, (NFC_READ_REG(NFC_REG_CTL)) | NFC_RAM_METHOD);
737
738
739	///*set dma and run*/
740	///*sdram*/
741	//if (NFC_IS_SDRAM((__u32)mainbuf))
742	//	attr = 0x2810293;
743	///*sram*/
744	//else
745	//	attr = 0x2800293;
746	_dma_config_start(0, (__u32)mainbuf, 2048);
747
748	/*wait cmd fifo free*/
749	ret = _wait_cmdfifo_free();
750	if (ret)
751		return ret;
752
753	/*set NFC_REG_CNT*/
754	NFC_WRITE_REG(NFC_REG_CNT,1024);
755
756	/*set NFC_REG_RCMD_SET*/
757	cfg = 0;
758	cfg |= (read_data_cmd & 0xff);
759	cfg |= ((random_read_cmd0 & 0xff) << 8);
760	cfg |= ((random_read_cmd1 & 0xff) << 16);
761	NFC_WRITE_REG(NFC_REG_RCMD_SET, cfg);
762
763	/*set NFC_REG_SECTOR_NUM*/
764	NFC_WRITE_REG(NFC_REG_SECTOR_NUM, 2048/1024);
765
766	/*set addr*/
767	_set_addr(read_addr_cmd->addr,read_addr_cmd->addr_cycle);
768
769	/*set NFC_REG_CMD*/
770	cfg  = 0;
771	cfg |= read_addr_cmd->value;
772	/*set sequence mode*/
773	//cfg |= 0x1<<25;
774	cfg |= ( (read_addr_cmd->addr_cycle - 1) << 16);
775	cfg |= (NFC_SEND_ADR | NFC_DATA_TRANS | NFC_SEND_CMD1 | NFC_SEND_CMD2 | NFC_WAIT_FLAG | NFC_DATA_SWAP_METHOD);
776	cfg |= ((__u32)0x2 << 30);//page command
777
778	if (pagesize/1024 == 1)
779		cfg |= NFC_SEQ;
780
781	/*enable ecc*/
782	_enable_ecc(1);
783	NFC_WRITE_REG(NFC_REG_CMD,cfg);
784
785    NAND_WaitDmaFinish();//
786
787	/*wait cmd fifo free and cmd finish*/
788	ret = _wait_cmdfifo_free();
789	ret |= _wait_cmd_finish();
790	if (ret){
791		_disable_ecc();
792		return ret;
793	}
794	/*get user data*/
795	for (i = 0; i < 2048/1024;  i++){
796		*(((__u32*) sparebuf)+i) = NFC_READ_REG(NFC_REG_USER_DATA(i));
797	}
798
799	/*ecc check and disable ecc*/
800	ret = _check_ecc(2048/1024);
801	_disable_ecc();
802
803	/*if dma mode is wait*/
804	if(0 == dma_wait_mode){
805		ret1 = _wait_dma_end();
806		if (ret1)
807			return ret1;
808	}
809
810	return ret;
811}
812
813
814
815__s32 NFC_Read_Seq(NFC_CMD_LIST  *rcmd, void *mainbuf, void *sparebuf, __u8 dma_wait_mode,__u8 page_mode )
816{
817
818	__s32 ret ;
819
820	_enter_nand_critical();
821
822	ret = _read_in_page_mode_seq(rcmd, mainbuf,sparebuf, dma_wait_mode);
823
824	/*switch to ahb*/
825	NFC_WRITE_REG(NFC_REG_CTL, (NFC_READ_REG(NFC_REG_CTL)) & (~NFC_RAM_METHOD));
826
827	_exit_nand_critical();
828
829
830	return ret;
831}
832
833__s32 NFC_Read_1K(NFC_CMD_LIST  *rcmd, void *mainbuf, void *sparebuf, __u8 dma_wait_mode,__u8 page_mode )
834{
835
836	__s32 ret ;
837
838	_enter_nand_critical();
839
840	ret = _read_in_page_mode_1K(rcmd, mainbuf,sparebuf, dma_wait_mode);
841
842	/*switch to ahb*/
843	NFC_WRITE_REG(NFC_REG_CTL, (NFC_READ_REG(NFC_REG_CTL)) & (~NFC_RAM_METHOD));
844
845	_exit_nand_critical();
846
847
848	return ret;
849}
850
851__s32 NFC_Read_Spare(NFC_CMD_LIST  *rcmd, void *mainbuf, void *sparebuf, __u8 dma_wait_mode,__u8 page_mode )
852{
853
854	__s32 ret ;
855
856	_enter_nand_critical();
857
858	ret = _read_in_page_mode_spare(rcmd, mainbuf,sparebuf, dma_wait_mode);
859
860	/*switch to ahb*/
861	NFC_WRITE_REG(NFC_REG_CTL, (NFC_READ_REG(NFC_REG_CTL)) & (~NFC_RAM_METHOD));
862
863	_exit_nand_critical();
864
865
866	return ret;
867}
868
869__s32 NFC_LSBInit(__u32 read_retry_type)
870{
871	//init
872	read_retry_mode = (read_retry_type>>16)&0xff;
873	read_retry_cycle =(read_retry_type>>8)&0xff;
874	read_retry_reg_num = (read_retry_type>>0)&0xff;
875
876	if(read_retry_mode == 1) //mode1
877	{
878		//set lsb mode
879		lsb_mode_reg_num = 5;
880
881		lsb_mode_reg_adr[0] = 0xa4;
882		lsb_mode_reg_adr[1] = 0xa5;
883		lsb_mode_reg_adr[2] = 0xb0;
884		lsb_mode_reg_adr[3] = 0xb1;
885		lsb_mode_reg_adr[4] = 0xc9;
886
887		lsb_mode_val[0] = 0x25;
888		lsb_mode_val[1] = 0x25;
889		lsb_mode_val[2] = 0x25;
890		lsb_mode_val[3] = 0x25;
891		lsb_mode_val[4] = 0x1;
892	}
893	else
894	{
895	    return -1;
896	}
897
898	return 0;
899}
900
901__s32 LSB_GetDefaultParam(__u32 chip,__u8* default_value, __u32 read_retry_type)
902{
903    __s32 ret;
904    __u32 i;
905
906    ret =_vender_get_param(&lsb_mode_default_val[0], &lsb_mode_reg_adr[0], lsb_mode_reg_num);
907    for(i=0; i<lsb_mode_reg_num; i++)
908    {
909        default_value[i] = lsb_mode_default_val[i];
910    }
911
912	return ret;
913
914}
915
916__s32 LSB_SetDefaultParam(__u32 chip,__u8* default_value, __u32 read_retry_type)
917{
918    __s32 ret;
919
920    ret =_vender_set_param(&lsb_mode_default_val[0], &lsb_mode_reg_adr[0], lsb_mode_reg_num);
921
922	return ret;
923}
924
925__s32 NFC_LSBEnable(__u32 chip, __u32 read_retry_type)
926{
927    __u8 value[LSB_MODE_MAX_REG_NUM];
928    __u32 i;
929
930    //fix chip 0
931    LSB_GetDefaultParam(0,value,read_retry_type);
932    for(i=0;i<lsb_mode_reg_num;i++)
933        value[i] += lsb_mode_val[i];
934
935    _vender_set_param(value, &lsb_mode_reg_adr[0], lsb_mode_reg_num);
936
937    return 0;
938}
939
940__s32 NFC_LSBDisable(__u32 chip, __u32 read_retry_type)
941{
942    __u8 value[LSB_MODE_MAX_REG_NUM];
943
944    //fix chip 0
945    LSB_SetDefaultParam(0,value, read_retry_type);
946
947    return 0;
948}
949
950__s32 NFC_LSBExit(__u32 read_retry_type)
951{
952
953	return 0;
954}
955