summaryrefslogtreecommitdiffstats
path: root/drivers/nand/nand_imx_v2.c
blob: 6b47022130e1b21a4a68bf9293c9d6457d658831 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
/*
 * Copyright 2004-2008 Freescale Semiconductor, Inc. All Rights Reserved.
 */

/*
 * The code contained herein is licensed under the GNU General Public
 * License. You may obtain a copy of the GNU General Public License
 * Version 2 or later at the following locations:
 *
 * http://www.opensource.org/licenses/gpl-license.html
 * http://www.gnu.org/copyleft/gpl.html
 */

#include <common.h>
#include <driver.h>
#include <malloc.h>
#include <init.h>
#include <linux/mtd/mtd.h>
#include <linux/mtd/nand.h>
#include <asm/arch/imx-nand.h>
#include <asm/arch/imx-regs.h>
#include <asm/io.h>
#include <errno.h>

/*
 * Addresses for NFC registers
 */
#define MAIN_AREA0      		(host->regs + 0x000)
#define MAIN_AREA1      		(host->regs + 0x200)
#define SPARE_AREA0     		(host->regs + 0x1000)
#define NFC_BUF_SIZE            	(host->regs + 0x1E00)
#define NFC_BUF_ADDR            	(host->regs + 0x1E04)
#define NFC_FLASH_ADDR          	(host->regs + 0x1E06)
#define NFC_FLASH_CMD           	(host->regs + 0x1E08)
#define NFC_CONFIG              	(host->regs + 0x1E0A)
#define NFC_ECC_STATUS_RESULT		(host->regs + 0x1E0C)
#define NFC_ECC_STATUS_RESULT_1		(host->regs + 0x1E0C)
#define NFC_ECC_STATUS_RESULT_2		(host->regs + 0x1E0E)
#define NFC_SPAS			(host->regs + 0x1E10)
#define NFC_WRPROT              	(host->regs + 0x1E12)
#define NFC_UNLOCKSTART_BLKADDR  	(host->regs + 0x1E20)
#define NFC_UNLOCKEND_BLKADDR    	(host->regs + 0x1E22)
#define NFC_UNLOCKSTART_BLKADDR1 	(host->regs + 0x1E24)
#define NFC_UNLOCKEND_BLKADDR1   	(host->regs + 0x1E26)
#define NFC_UNLOCKSTART_BLKADDR2 	(host->regs + 0x1E28)
#define NFC_UNLOCKEND_BLKADDR2   	(host->regs + 0x1E2A)
#define NFC_UNLOCKSTART_BLKADDR3 	(host->regs + 0x1E2C)
#define NFC_UNLOCKEND_BLKADDR3   	(host->regs + 0x1E2E)
#define NFC_NF_WRPRST            	(host->regs + 0x1E18)
#define NFC_CONFIG1              	(host->regs + 0x1E1A)
#define NFC_CONFIG2              	(host->regs + 0x1E1C)

/*
 * Set INT to 0, Set 1 to specific operation bit, rest to 0 in LAUNCH_NFC Register for
 * Specific operation
 */
#define NFC_CMD            		0x1
#define NFC_ADDR           		0x2
#define NFC_INPUT          		0x4
#define NFC_OUTPUT         		0x8
#define NFC_ID             		0x10
#define NFC_STATUS         		0x20

/* Bit Definitions */
#define NFC_OPS_STAT			(1 << 15)
#define NFC_SP_EN           		(1 << 2)
#define NFC_ECC_EN          		(1 << 3)
#define NFC_INT_MSK         		(1 << 4)
#define NFC_BIG             		(1 << 5)
#define NFC_RST             		(1 << 6)
#define NFC_CE              		(1 << 7)
#define NFC_ONE_CYCLE       		(1 << 8)
#define NFC_BLS_LOCKED			0
#define NFC_BLS_LOCKED_DEFAULT		1
#define NFC_BLS_UNLCOKED		2
#define NFC_WPC_LOCK_TIGHT		1
#define NFC_WPC_LOCK			(1 << 1)
#define NFC_WPC_UNLOCK			(1 << 2)
#define NFC_FLASH_ADDR_SHIFT 		0
#define NFC_UNLOCK_END_ADDR_SHIFT	0

#define NFC_ECC_MODE_4    		 (1<<0)
#define NFC_ECC_MODE_8			 ~(1<<0)
#define NFC_SPAS_16			 8
#define NFC_SPAS_64			 32
#define NFC_SPAS_128			 64
#define NFC_SPAS_218			 109

static inline void raw_write(unsigned long val, void *reg)
{
	writew(val, reg);
	debug("wr: 0x%08x = 0x%08x\n", reg, val);
}

static inline unsigned long raw_read(void *reg)
{
	unsigned long val = readw(reg);

	debug("rd: 0x%08x = 0x%08x\n", reg, val);

	return val;
}

struct imx_nand_host {
	struct mtd_info mtd;
	struct nand_chip nand;
	struct device_d *dev;
	void __iomem *regs;

	int status_request;
	u16 col_addr;
};

/*
 * Define delays in microsec for NAND device operations
 */
#define TROP_US_DELAY   2000

static u8 *data_buf;
static u8 *oob_buf;

/*
 * OOB placement block for use with hardware ecc generation
 */
static struct nand_ecclayout nand_hw_eccoob_512 = {
	.eccbytes = 9,
	.eccpos = {7, 8, 9, 10, 11, 12, 13, 14, 15},
	.oobavail = 4,
	.oobfree = {{0, 4}}
};

static struct nand_ecclayout nand_hw_eccoob_2k = {
	.eccbytes = 9,
	.eccpos = {7, 8, 9, 10, 11, 12, 13, 14, 15},
	.oobavail = 4,
	.oobfree = {{2, 4}}
};

static struct nand_ecclayout nand_hw_eccoob_4k = {
	.eccbytes = 9,
	.eccpos = {7, 8, 9, 10, 11, 12, 13, 14, 15},
	.oobavail = 4,
	.oobfree = {{2, 4}}
};

/*
 * @defgroup NAND_MTD NAND Flash MTD Driver for MXC processors
 */

/*
 * @file mxc_nd2.c
 *
 * @brief This file contains the hardware specific layer for NAND Flash on
 * MXC processor
 *
 * @ingroup NAND_MTD
 */

static void memcpy32(void *trg, const void *src, int size)
{
	int i;
	unsigned int *t = trg;
	unsigned const int *s = src;

	if ((ulong)trg & 0x3 || (ulong)src & 0x3 || size & 0x3)
		while(1);

	for (i = 0; i < (size >> 2); i++)
		*t++ = *s++;
}

/*
 * Functions to transfer data to/from spare erea.
 */
static void
copy_spare(struct mtd_info *mtd, void *pbuf, void *pspare, int len, int bfrom)
{
	u16 i, j;
	u16 m = mtd->oobsize;
	u16 n = mtd->writesize >> 9;
	u8 *d = (u8 *) pbuf;
	u8 *s = (u8 *) pspare;

	j = (m / n >> 1) << 1;

	if (bfrom) {
		for (i = 0; i < n - 1; i++)
			memcpy32(&d[i * j], &s[i * 64], j);

		/* the last section */
		memcpy32(&d[i * j], &s[i * 64], len - i * j);
	} else {
		for (i = 0; i < n - 1; i++)
			memcpy32(&s[i * 64], &d[i * j], j);

		/* the last section */
		memcpy32(&s[i * 64], &d[i * j], len - i * j);
	}
}

/*
 * This function polls the NFC to wait for the basic operation to complete by
 * checking the INT bit of config2 register.
 *
 * @param       maxRetries     number of retry attempts (separated by 1 us)
 * @param       useirq         True if IRQ should be used rather than polling
 */
static void wait_op_done(struct imx_nand_host *host, int maxRetries)
{
	while (maxRetries > 0) {
		maxRetries--;
		if (raw_read(NFC_CONFIG2) & NFC_OPS_STAT) {
			raw_write((raw_read(NFC_CONFIG2) & ~NFC_OPS_STAT), NFC_CONFIG2);
			break;
		}
		udelay(1);
	}

	if (maxRetries <= 0)
		printf("%s timeout\n", __func__);
}

/*
 * This function issues the specified command to the NAND device and
 * waits for completion.
 *
 * @param       cmd     command for NAND Flash
 * @param       useirq  True if IRQ should be used rather than polling
 */
static void send_cmd(struct mtd_info *mtd, u16 cmd, int useirq)
{
	struct nand_chip *nand_chip = mtd->priv;
	struct imx_nand_host *host = nand_chip->priv;

	/* fill command */
	raw_write(cmd, NFC_FLASH_CMD);

	/* send out command */
	raw_write(NFC_CMD, NFC_CONFIG2);

	wait_op_done(host, TROP_US_DELAY);
}

/*
 * This function sends an address (or partial address) to the
 * NAND device.  The address is used to select the source/destination for
 * a NAND command.
 *
 * @param       addr    address to be written to NFC.
 * @param       useirq  True if IRQ should be used rather than polling
 */
static void send_addr(struct imx_nand_host *host, u16 addr, int useirq)
{
	debug("%s: 0x%04x\n", __func__, addr);

	/* fill address */
	raw_write((addr << NFC_FLASH_ADDR_SHIFT), NFC_FLASH_ADDR);

	/* send out address */
	raw_write(NFC_ADDR, NFC_CONFIG2);

	wait_op_done(host, TROP_US_DELAY);
}

/*
 * This function requests the NFC to perform a read of the
 * NAND device status and returns the current status.
 *
 * @return  device status
 */
static u16 get_dev_status(struct imx_nand_host *host)
{
	raw_write(1, NFC_BUF_ADDR);

	/* Read status into main buffer */
	raw_write(NFC_STATUS, NFC_CONFIG2);

	wait_op_done(host, TROP_US_DELAY);

	/* Status is placed in first word of main buffer */
	/* get status, then recovery area 1 data */
	return raw_read(MAIN_AREA1);
}

static void mxc_nand_enable_hwecc(struct mtd_info *mtd, int mode)
{
	struct nand_chip *nand_chip = mtd->priv;
	struct imx_nand_host *host = nand_chip->priv;

	raw_write((raw_read(NFC_CONFIG1) | NFC_ECC_EN), NFC_CONFIG1);
	return;
}

/*
 * Function to record the ECC corrected/uncorrected errors resulted
 * after a page read. This NFC detects and corrects upto to 4 symbols
 * of 9-bits each.
 */
static int mxc_check_ecc_status(struct mtd_info *mtd)
{
	struct nand_chip *nand_chip = mtd->priv;
	struct imx_nand_host *host = nand_chip->priv;
	u32 ecc_stat, err;
	int no_subpages = 1;
	int ret = 0;
	u8 ecc_bit_mask, err_limit;

	ecc_bit_mask = (raw_read(NFC_CONFIG1) & NFC_ECC_MODE_4) ? 0x7 : 0xf;
	err_limit = raw_read(NFC_CONFIG1) & NFC_ECC_MODE_4 ? 0x4 : 0x8;

	no_subpages = mtd->writesize >> 9;

	ecc_stat = raw_read(NFC_ECC_STATUS_RESULT);
	do {
		err = ecc_stat & ecc_bit_mask;
		if (err > err_limit) {
			mtd->ecc_stats.failed++;
			printk(KERN_WARNING "UnCorrectable RS-ECC Error\n");
			return -1;
		} else {
			ret += err;
		}
		ecc_stat >>= 4;
	} while (--no_subpages);

	mtd->ecc_stats.corrected += ret;
	pr_debug("%d Symbol Correctable RS-ECC Error\n", ret);

	return ret;
}

/*
 * Function to correct the detected errors. This NFC corrects all the errors
 * detected. So this function just return 0.
 */
static int mxc_nand_correct_data(struct mtd_info *mtd, u_char * dat,
				 u_char * read_ecc, u_char * calc_ecc)
{
	return 0;
}

/*
 * Function to calculate the ECC for the data to be stored in the Nand device.
 * This NFC has a hardware RS(511,503) ECC engine together with the RS ECC
 * CONTROL blocks are responsible for detection  and correction of up to
 * 8 symbols of 9 bits each in 528 byte page.
 * So this function is just return 0.
 */

static int mxc_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
				  u_char *ecc_code)
{
	return 0;
}

/*
 * This function id is used to read the data buffer from the NAND Flash. To
 * read the data from NAND Flash first the data output cycle is initiated by
 * the NFC, which copies the data to RAMbuffer. This data of length \b len is
 * then copied to buffer \b buf.
 *
 * @param       mtd     MTD structure for the NAND Flash
 * @param       buf     data to be read from NAND Flash
 * @param       len     number of bytes to be read
 */
static void mxc_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
{
	struct nand_chip *nand_chip = mtd->priv;
	struct imx_nand_host *host = nand_chip->priv;
	u16 col = host->col_addr;

	debug("%s: 0x%08x %d\n", __func__, buf, len);

	if (mtd->writesize) {
		int j = mtd->writesize - col;
		int n = mtd->oobsize + j;

		n = min(n, len);

		if (j > 0) {
			if (n > j) {
				memcpy(buf, &data_buf[col], j);
				memcpy(buf + j, &oob_buf[0], n - j);
			} else {
				memcpy(buf, &data_buf[col], n);
			}
		} else {
			col -= mtd->writesize;
			memcpy(buf, &oob_buf[col], len);
		}

		/* update */
		host->col_addr += n;

	} else {
		/* At flash identify phase,
		 * mtd->writesize has not been
		 * set correctly, it should
		 * be zero.And len will less 2
		 */
		memcpy(buf, &data_buf[col], len);

		/* update */
		host->col_addr += len;
	}
}

/*
 * This function reads byte from the NAND Flash
 *
 * @param       mtd     MTD structure for the NAND Flash
 *
 * @return    data read from the NAND Flash
 */
static uint8_t mxc_nand_read_byte(struct mtd_info *mtd)
{
	struct nand_chip *nand_chip = mtd->priv;
	struct imx_nand_host *host = nand_chip->priv;
	uint8_t ret;

	/* Check for status request */
	if (host->status_request)
		return get_dev_status(host) & 0xFF;

	mxc_nand_read_buf(mtd, &ret, 1);

	return ret;
}

/*
  * This function reads word from the NAND Flash
  *
  * @param     mtd     MTD structure for the NAND Flash
  *
  * @return    data read from the NAND Flash
  */
static u16 mxc_nand_read_word(struct mtd_info *mtd)
{
	u16 ret;

	mxc_nand_read_buf(mtd, (uint8_t *)&ret, sizeof(u16));

	return ret;
}

/*
 * This function reads byte from the NAND Flash
 *
 * @param     mtd     MTD structure for the NAND Flash
 *
 * @return    data read from the NAND Flash
 */
static u_char mxc_nand_read_byte16(struct mtd_info *mtd)
{
	struct nand_chip *nand_chip = mtd->priv;
	struct imx_nand_host *host = nand_chip->priv;

	/* Check for status request */
	if (host->status_request)
		return get_dev_status(host) & 0xFF;

	return mxc_nand_read_word(mtd) & 0xFF;
}

/*
 * This function writes data of length \b len from buffer \b buf to the NAND
 * internal RAM buffer's MAIN area 0.
 *
 * @param       mtd     MTD structure for the NAND Flash
 * @param       buf     data to be written to NAND Flash
 * @param       len     number of bytes to be written
 */
static void mxc_nand_write_buf(struct mtd_info *mtd,
			       const u_char *buf, int len)
{
	struct nand_chip *nand_chip = mtd->priv;
	struct imx_nand_host *host = nand_chip->priv;
	u16 col = host->col_addr;
	int j = mtd->writesize - col;
	int n = mtd->oobsize + j;

	n = min(n, len);

	if (j > 0) {
		if (n > j) {
			memcpy(&data_buf[col], buf, j);
			memcpy(&oob_buf[0], buf + j, n - j);
		} else {
			memcpy(&data_buf[col], buf, n);
		}
	} else {
		col -= mtd->writesize;
		memcpy(&oob_buf[col], buf, len);
	}

	/* update */
	host->col_addr += n;
}

/*
 * This function is used by the upper layer to verify the data in NAND Flash
 * with the data in the \b buf.
 *
 * @param       mtd     MTD structure for the NAND Flash
 * @param       buf     data to be verified
 * @param       len     length of the data to be verified
 *
 * @return      -EFAULT if error else 0
 *
 */
static int mxc_nand_verify_buf(struct mtd_info *mtd, const u_char *buf,
			       int len)
{
	u_char *s = data_buf;

	const u_char *p = buf;

	for (; len > 0; len--) {
		if (*p++ != *s++)
			return -EFAULT;
	}

	return 0;
}

/*
 * This function is used by upper layer for select and deselect of the NAND
 * chip
 *
 * @param       mtd     MTD structure for the NAND Flash
 * @param       chip    val indicating select or deselect
 */
static void mxc_nand_select_chip(struct mtd_info *mtd, int chip)
{
}

/*
 * Function to perform the address cycles.
 */
static void mxc_do_addr_cycle(struct mtd_info *mtd, int column, int page_addr)
{
	struct nand_chip *nand_chip = mtd->priv;
	struct imx_nand_host *host = nand_chip->priv;
	u32 page_mask = nand_chip->pagemask;

	debug("%s: %d %d\n", __func__, column, page_addr);

	if (column != -1) {
		send_addr(host, column & 0xFF, 0);
		if (mtd->writesize == 2048) {
			/* another col addr cycle for 2k page */
			send_addr(host, (column >> 8) & 0xF, 0);
		} else if (mtd->writesize == 4096) {
			/* another col addr cycle for 4k page */
			send_addr(host, (column >> 8) & 0x1F, 0);
		}
	}
	if (page_addr != -1) {
		do {
			send_addr(host, (page_addr & 0xff), 0);
			page_mask >>= 8;
			page_addr >>= 8;
		} while (page_mask != 0);
	}
}

/*
 * This function is used by the upper layer to write command to NAND Flash for
 * different operations to be carried out on NAND Flash
 *
 * @param       mtd             MTD structure for the NAND Flash
 * @param       command         command for NAND Flash
 * @param       column          column offset for the page read
 * @param       page_addr       page to be read from NAND Flash
 */
static void mxc_nand_command(struct mtd_info *mtd, unsigned command,
			     int column, int page_addr)
{
	struct nand_chip *nand_chip = mtd->priv;
	struct imx_nand_host *host = nand_chip->priv;

	debug("mxc_nand_command (cmd = 0x%x, col = 0x%x, page = 0x%x)\n",
		command, column, page_addr);

	/*
	 * Reset command state information
	 */
	host->status_request = 0;

	/*
	 * Command pre-processing step
	 */
	switch (command) {
	case NAND_CMD_STATUS:
		host->col_addr = 0;
		host->status_request = 1;
		break;

	case NAND_CMD_READ0:
		host->col_addr = column;
		break;

	case NAND_CMD_READOOB:
		host->col_addr = column;
		command = NAND_CMD_READ0;
		break;

	case NAND_CMD_SEQIN:
		if (column) {

			/* FIXME: before send SEQIN command for
			 * partial write,We need read one page out.
			 * FSL NFC does not support partial write
			 * It alway send out 512+ecc+512+ecc ...
			 * for large page nand flash. But for small
			 * page nand flash, it did support SPARE
			 * ONLY operation. But to make driver
			 * simple. We take the same as large page,read
			 * whole page out and update. As for MLC nand
			 * NOP(num of operation) = 1. Partial written
			 * on one programed page is not allowed! We
			 * can't limit it on the driver, it need the
			 * upper layer applicaiton take care it
			 */

			mxc_nand_command(mtd, NAND_CMD_READ0, 0, page_addr);
		}

		host->col_addr = column;
		break;

	case NAND_CMD_PAGEPROG:
		/* FIXME:the NFC interal buffer
		 * access has some limitation, it
		 * does not allow byte access. To
		 * make the code simple and ease use
		 * not every time check the address
		 * alignment.Use the temp buffer
		 * to accomadate the data.since We
		 * know data_buf will be at leat 4
		 * byte alignment, so we can use
		 * memcpy safely
		 */
		memcpy32(MAIN_AREA0, data_buf, mtd->writesize);
		copy_spare(mtd, oob_buf, SPARE_AREA0, mtd->oobsize, 0);

		/* set ram buffer id */
		raw_write(0, NFC_BUF_ADDR);

		/* transfer data from NFC ram to nand */
		raw_write(NFC_INPUT, NFC_CONFIG2);

		wait_op_done(host, TROP_US_DELAY);

		break;

	case NAND_CMD_ERASE1:
	case NAND_CMD_ERASE2:
		break;
	}

	send_cmd(mtd, command, 0);

	mxc_do_addr_cycle(mtd, column, page_addr);

	/*
	 * Command post-processing step
	 */
	switch (command) {
	case NAND_CMD_READOOB:
	case NAND_CMD_READ0:
		if (mtd->writesize > 512) {
			/* send read confirm command */
			send_cmd(mtd, NAND_CMD_READSTART, 1);
		}

		raw_write(0, NFC_BUF_ADDR);

		/* transfer data from nand to NFC ram */
		raw_write(NFC_OUTPUT, NFC_CONFIG2);

		wait_op_done(host, TROP_US_DELAY);

		/* FIXME, the NFC interal buffer
		 * access has some limitation, it
		 * does not allow byte access. To
		 * make the code simple and ease use
		 * not every time check the address
		 * alignment.Use the temp buffer
		 * to accomadate the data.since We
		 * know data_buf will be at leat 4
		 * byte alignment, so we can use
		 * memcpy safely
		 */
		memcpy32(data_buf, MAIN_AREA0, mtd->writesize);
		copy_spare(mtd, oob_buf, SPARE_AREA0, mtd->oobsize, 1);

		break;

	case NAND_CMD_READID:
		raw_write(0, NFC_BUF_ADDR);

		/* Read ID into main buffer */
		raw_write(NFC_ID, NFC_CONFIG2);

		wait_op_done(host, TROP_US_DELAY);

		host->col_addr = column;
		memcpy32(data_buf, MAIN_AREA0, 2048);

		break;
	}
}

static int mxc_nand_read_oob(struct mtd_info *mtd,
			     struct nand_chip *chip, int page, int sndcmd)
{
	if (sndcmd) {
		chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
		sndcmd = 0;
	}

	memcpy32(chip->oob_poi, oob_buf, mtd->oobsize);

	return sndcmd;
}

static int mxc_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
			      uint8_t *buf)
{
	debug("%s: 0x%08x\n", __func__, buf);

	mxc_check_ecc_status(mtd);

	memcpy32(buf, data_buf, mtd->writesize);
	memcpy32(chip->oob_poi, oob_buf, mtd->oobsize);

	return 0;
}

static void mxc_nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
				const uint8_t *buf)
{
	memcpy32(data_buf, buf, mtd->writesize);
	memcpy32(oob_buf, chip->oob_poi, mtd->oobsize);
}

/*
 * We must provide a private bbt decriptor, because the settings from
 * the generic one collide with our ECC hardware.
 */
static uint8_t bbt_pattern[] = { 'B', 'b', 't', '0' };
static uint8_t mirror_pattern[] = { '1', 't', 'b', 'B' };

static struct nand_bbt_descr bbt_main_descr = {
	.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
	    | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
	.offs = 0,
	.len = 4,
	.veroffs = 4,
	.maxblocks = 4,
	.pattern = bbt_pattern
};

static struct nand_bbt_descr bbt_mirror_descr = {
	.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
	    | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
	.offs = 0,
	.len = 4,
	.veroffs = 4,
	.maxblocks = 4,
	.pattern = mirror_pattern
};

#ifdef CONFIG_ARCH_IMX25

#define RCSR_NFC_FMS		(1 << 8)
#define RCSR_NFC_4K		(1 << 9)
#define RCSR_NFC_16BIT_SEL	(1 << 14)

static void __bare_init mxc_nand_set_writesize(struct mtd_info *mtd, int writesize)
{
	unsigned int rcsr;

	rcsr = readl(IMX_CCM_BASE + CCM_RCSR);
	rcsr &= ~(RCSR_NFC_FMS | RCSR_NFC_4K);

	if (writesize == 2048)
		rcsr |= RCSR_NFC_FMS;
	if (writesize == 4096)
		rcsr |= RCSR_NFC_FMS | RCSR_NFC_4K;

	writel(rcsr, IMX_CCM_BASE + CCM_RCSR);
}

static void __bare_init mxc_nand_set_datawidth(struct mtd_info *mtd, int datawidth)
{
	unsigned int rcsr;

	rcsr = readl(IMX_CCM_BASE + CCM_RCSR);

	if (datawidth == 16)
		rcsr |= RCSR_NFC_16BIT_SEL;
	else
		rcsr &= ~RCSR_NFC_16BIT_SEL;


	writel(rcsr, IMX_CCM_BASE + CCM_RCSR);
}
#endif

static void mxc_nfc_init(struct imx_nand_host *host)
{
	/* Disable interrupt */
	raw_write((raw_read(NFC_CONFIG1) | NFC_INT_MSK), NFC_CONFIG1);

	/* disable spare enable */
	raw_write(raw_read(NFC_CONFIG1) & ~NFC_SP_EN, NFC_CONFIG1);

	/* Unlock the internal RAM Buffer */
	raw_write(NFC_BLS_UNLCOKED, NFC_CONFIG);

	/* Blocks to be unlocked */
	raw_write(0x0, NFC_UNLOCKSTART_BLKADDR);
        raw_write(0xffff, NFC_UNLOCKEND_BLKADDR);

	/* Unlock Block Command for given address range */
	raw_write(NFC_WPC_UNLOCK, NFC_WRPROT);
}

static int __init imxnd_probe(struct device_d *dev)
{
	struct nand_chip *this;
	struct mtd_info *mtd;
	struct imx_nand_platform_data *pdata = dev->platform_data;
	struct imx_nand_host *host;
	u16 tmp;
	int err = 0;

	/* init data buf */
	data_buf = xzalloc(NAND_MAX_PAGESIZE);
	oob_buf = xzalloc(NAND_MAX_OOBSIZE);

	/* Allocate memory for MTD device structure and private data */
	host = kzalloc(sizeof(struct imx_nand_host), GFP_KERNEL);
	if (!host) {
		printk(KERN_ERR "%s: failed to allocate mtd_info\n",
		       __FUNCTION__);
		err = -ENOMEM;
		goto err_out;
	}

	host->dev = dev;
	/* structures must be linked */
	this = &host->nand;
	mtd = &host->mtd;
	mtd->priv = this;

	/* NAND bus width determines access funtions used by upper layer */
	if (pdata->width == 2) {
		this->read_byte = mxc_nand_read_byte16;
		this->options |= NAND_BUSWIDTH_16;
		mxc_nand_set_datawidth(mtd, 16);
	} else
		mxc_nand_set_datawidth(mtd, 8);

	/* 50 us command delay time */
	this->chip_delay = 5;

	this->priv = host;
	this->cmdfunc = mxc_nand_command;
	this->select_chip = mxc_nand_select_chip;
	this->read_byte = mxc_nand_read_byte;
	this->read_word = mxc_nand_read_word;
	this->write_buf = mxc_nand_write_buf;
	this->read_buf = mxc_nand_read_buf;
	this->verify_buf = mxc_nand_verify_buf;

	/* use flash based bbt */
	this->bbt_td = &bbt_main_descr;
	this->bbt_md = &bbt_mirror_descr;

	/* update flash based bbt */
	this->options |= NAND_USE_FLASH_BBT;

	host->regs = (void __iomem *)dev->map_base;

	mxc_nfc_init(host);

	tmp = readw(NFC_CONFIG1);
	tmp |= NFC_INT_MSK;
	writew(tmp, NFC_CONFIG1);

	if (pdata->hw_ecc) {
		this->ecc.read_page = mxc_nand_read_page;
		this->ecc.write_page = mxc_nand_write_page;
		this->ecc.read_oob = mxc_nand_read_oob;
		this->ecc.layout = &nand_hw_eccoob_512;
		this->ecc.calculate = mxc_nand_calculate_ecc;
		this->ecc.hwctl = mxc_nand_enable_hwecc;
		this->ecc.correct = mxc_nand_correct_data;
		this->ecc.mode = NAND_ECC_HW;
		this->ecc.size = 512;
		this->ecc.bytes = 9;
		raw_write(raw_read(NFC_CONFIG1) | NFC_ECC_EN, NFC_CONFIG1);
	} else {
		this->ecc.mode = NAND_ECC_SOFT;
		raw_write(raw_read(NFC_CONFIG1) & ~NFC_ECC_EN, NFC_CONFIG1);
	}

	/* Reset NAND */
	this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);

	/* Scan to find existence of the device */
	if (nand_scan_ident(mtd, 1)) {
		err = -ENXIO;
		dev_err(dev, "Unable to find any NAND device\n");
		goto err_out;
	}

	if (mtd->writesize == 2048) {
		mxc_nand_set_writesize(mtd, 2048);
		raw_write(((raw_read(NFC_SPAS) & 0xff00) | NFC_SPAS_64), NFC_SPAS);
		raw_write((raw_read(NFC_CONFIG1) | NFC_ECC_MODE_4), NFC_CONFIG1);
		this->ecc.layout = &nand_hw_eccoob_2k;
	} else if (mtd->writesize == 4096) {
		mxc_nand_set_writesize(mtd, 4096);
		raw_write(((raw_read(NFC_SPAS) & 0xff00) | NFC_SPAS_128), NFC_SPAS);
		raw_write((raw_read(NFC_CONFIG1) | NFC_ECC_MODE_4), NFC_CONFIG1);
		this->ecc.layout = &nand_hw_eccoob_4k;
	} else {
		mxc_nand_set_writesize(mtd, 512);
		this->ecc.layout = &nand_hw_eccoob_512;
	}

	if (nand_scan_tail(mtd)) {
		dev_err(dev, "Unable to find any NAND device.\n");
		err = -ENXIO;
		goto err_out;
	}

	add_mtd_device(mtd);

#ifdef CONFIG_MXC_NAND_LOW_LEVEL_ERASE
	/* Erase all the blocks of a NAND */
	imx_low_erase(mtd);
#endif

	dev->priv = host;

	return 0;
err_out:
	return err;
}

static struct driver_d imx_nand_driver = {
	.name  = "imx_nand",
	.probe = imxnd_probe,
};

/*
 * Main initialization routine
 * @return  0 if successful; non-zero otherwise
 */
static int __init imx_nand_init(void)
{
	return register_driver(&imx_nand_driver);
}

device_initcall(imx_nand_init);

#ifdef CONFIG_NAND_IMX_BOOT

static void __bare_init noinline boot_send_cmd(struct imx_nand_host *host, u16 cmd)
{
	/* fill command */
	raw_write(cmd, NFC_FLASH_CMD);

	/* send out command */
	raw_write(NFC_CMD, NFC_CONFIG2);

	wait_op_done(host, TROP_US_DELAY);
}

static void noinline __bare_init boot_send_addr(struct imx_nand_host *host, u16 addr)
{
	/* fill address */
	raw_write((addr << NFC_FLASH_ADDR_SHIFT), NFC_FLASH_ADDR);

	/* send out address */
	raw_write(NFC_ADDR, NFC_CONFIG2);

	wait_op_done(host, TROP_US_DELAY);
}

static void __bare_init boot_nfc_addr(struct imx_nand_host *host, u32 offs, int pagesize)
{
	switch (pagesize) {
	case 512:
		boot_send_addr(host, offs & 0xff);
		boot_send_addr(host, (offs >> 9) & 0xff);
		boot_send_addr(host, (offs >> 17) & 0xff);
		boot_send_addr(host, (offs >> 25) & 0xff);
		break;
	case 2048:
		boot_send_addr(host, 0);
		boot_send_addr(host, 0);
		boot_send_addr(host, (offs >> 11) & 0xff);
		boot_send_addr(host, (offs >> 19) & 0xff);
		boot_send_addr(host, (offs >> 27) & 0xff);
		break;
	case 4096:
		boot_send_addr(host, 0);
		boot_send_addr(host, 0);
		boot_send_addr(host, (offs >> 12) & 0xff);
		boot_send_addr(host, (offs >> 20) & 0xff);
		boot_send_addr(host, (offs >> 27) & 0xff);
		break;
	}

	if (pagesize > 512)
		boot_send_cmd(host, NAND_CMD_READSTART);
}

static void __bare_init noinline boot_send_read_page(struct imx_nand_host *host, u8 buf_id)
{
	DEBUG(MTD_DEBUG_LEVEL3, "%s(%d)\n", __FUNCTION__, buf_id);

	raw_write(buf_id, NFC_BUF_ADDR);

	/* transfer data from nand to NFC ram */
	raw_write(NFC_OUTPUT, NFC_CONFIG2);

	wait_op_done(host, TROP_US_DELAY);
}

static int __bare_init block_is_bad(struct imx_nand_host *host, u32 offs, int pagesize)
{
	boot_send_cmd(host, NAND_CMD_READ0);
	boot_nfc_addr(host, offs, pagesize);
	boot_send_read_page(host, 0);

	/* FIXME: copied from V1 driver. Is this correct? */
	return (raw_read(SPARE_AREA0) & 0xff) == 0xff ? 0 : 1;
}

void __bare_init imx_nand_load_image(void *dest, int size, int pagesize,
		int blocksize)
{
	struct imx_nand_host _host;
	struct imx_nand_host *host = &_host;
	int width = 1;
	u32 page, block;

	host->regs = (void *)IMX_NAND_BASE;

	debug("%s\n", __func__);

	/* disable spare enable */
	raw_write(raw_read(NFC_CONFIG1) & ~NFC_SP_EN, NFC_CONFIG1);

	/* Unlock the internal RAM Buffer */
	raw_write(NFC_BLS_UNLCOKED, NFC_CONFIG);

	/* Blocks to be unlocked */
	raw_write(0x0, NFC_UNLOCKSTART_BLKADDR);
        raw_write(0xffff, NFC_UNLOCKEND_BLKADDR);

	/* Unlock Block Command for given address range */
	raw_write(NFC_WPC_UNLOCK, NFC_WRPROT);

	if (readl(IMX_CCM_BASE + CCM_RCSR) & (1 << 14))
		width = 2;

	block = page = 0;

	while (1) {
		if (!block_is_bad(host, block * blocksize, pagesize)) {
			page = 0;
			while (page * pagesize < blocksize) {
				debug("page: %d block: %d dest: %p src "
						"0x%08x\n",
						page, block, dest,
						block * blocksize +
						page * pagesize);

				boot_send_cmd(host, NAND_CMD_READ0);
				boot_nfc_addr(host, block * blocksize +
						page * pagesize, pagesize);
				boot_send_read_page(host, 0);
				page++;
				memcpy32(dest, MAIN_AREA0, pagesize);
				dest += pagesize;
				size -= pagesize;
				if (size <= 0)
					return;
			}
		}
		block++;
	}
}

#ifdef IMX_NAND_BOOT_DEBUG
#include <command.h>
static int do_nand_boot_test(cmd_tbl_t *cmdtp, int argc, char *argv[])
{
	void *dest;
	int size, pagesize, blocksize;

	if (argc < 4)
		return COMMAND_ERROR_USAGE;

	dest = (void *)strtoul_suffix(argv[1], NULL, 0);
	size = strtoul_suffix(argv[2], NULL, 0);
	pagesize = strtoul_suffix(argv[3], NULL, 0);
	blocksize = strtoul_suffix(argv[4], NULL, 0);

	imx_nand_load_image(dest, size, pagesize, blocksize * 1024);

	return 0;
}

static const __maybe_unused char cmd_nand_boot_test_help[] =
"Usage: nand_boot_test <dest> <size> <pagesize> <blocksize in kiB>\n";

U_BOOT_CMD_START(nand_boot_test)
	.cmd		= do_nand_boot_test,
	.usage		= "load an image from NAND",
	U_BOOT_CMD_HELP(cmd_nand_boot_test_help)
U_BOOT_CMD_END

#endif
#endif