aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/mediatek/patches-4.19/0306-spi-spi-mem-MediaTek-Add-SPI-NAND-Flash-interface-dr.patch
blob: 2370925372f69aed0566339a4808056580e88837 (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
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
From 1ecb38eabd90efe93957d0a822a167560c39308a Mon Sep 17 00:00:00 2001
From: Xiangsheng Hou <xiangsheng.hou@mediatek.com>
Date: Wed, 20 Mar 2019 16:19:51 +0800
Subject: [PATCH 6/6] spi: spi-mem: MediaTek: Add SPI NAND Flash interface
 driver for MediaTek MT7622

Change-Id: I3e78406bb9b46b0049d3988a5c71c7069e4f809c
Signed-off-by: Xiangsheng Hou <xiangsheng.hou@mediatek.com>
---
 drivers/spi/Kconfig        |    9 +
 drivers/spi/Makefile       |    1 +
 drivers/spi/spi-mtk-snfi.c | 1183 ++++++++++++++++++++++++++++++++++++
 3 files changed, 1193 insertions(+)
 create mode 100644 drivers/spi/spi-mtk-snfi.c

--- /dev/null
+++ b/drivers/spi/spi-mtk-snfi.c
@@ -0,0 +1,1183 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Driver for MediaTek SPI Nand interface
+ *
+ * Copyright (C) 2018 MediaTek Inc.
+ * Authors:	Xiangsheng Hou	<xiangsheng.hou@mediatek.com>
+ *
+ */
+
+#include <linux/clk.h>
+#include <linux/delay.h>
+#include <linux/dma-mapping.h>
+#include <linux/interrupt.h>
+#include <linux/iopoll.h>
+#include <linux/mtd/mtd.h>
+#include <linux/mtd/mtk_ecc.h>
+#include <linux/mtd/spinand.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/of_device.h>
+#include <linux/platform_device.h>
+#include <linux/spi/spi.h>
+#include <linux/spi/spi-mem.h>
+
+/* NAND controller register definition */
+/* NFI control */
+#define NFI_CNFG		0x00
+#define		CNFG_DMA		BIT(0)
+#define		CNFG_READ_EN		BIT(1)
+#define		CNFG_DMA_BURST_EN	BIT(2)
+#define		CNFG_BYTE_RW		BIT(6)
+#define		CNFG_HW_ECC_EN		BIT(8)
+#define		CNFG_AUTO_FMT_EN	BIT(9)
+#define		CNFG_OP_PROGRAM		(3UL << 12)
+#define		CNFG_OP_CUST		(6UL << 12)
+#define NFI_PAGEFMT		0x04
+#define		PAGEFMT_512		0
+#define		PAGEFMT_2K		1
+#define		PAGEFMT_4K		2
+#define		PAGEFMT_FDM_SHIFT	8
+#define		PAGEFMT_FDM_ECC_SHIFT	12
+#define NFI_CON			0x08
+#define		CON_FIFO_FLUSH		BIT(0)
+#define		CON_NFI_RST		BIT(1)
+#define		CON_BRD			BIT(8)
+#define		CON_BWR			BIT(9)
+#define		CON_SEC_SHIFT		12
+#define NFI_INTR_EN		0x10
+#define		INTR_AHB_DONE_EN	BIT(6)
+#define NFI_INTR_STA		0x14
+#define NFI_CMD			0x20
+#define NFI_STA			0x60
+#define		STA_EMP_PAGE		BIT(12)
+#define		NAND_FSM_MASK		(0x1f << 24)
+#define		NFI_FSM_MASK		(0xf << 16)
+#define NFI_ADDRCNTR		0x70
+#define		CNTR_MASK		GENMASK(16, 12)
+#define		ADDRCNTR_SEC_SHIFT	12
+#define		ADDRCNTR_SEC(val) \
+		(((val) & CNTR_MASK) >> ADDRCNTR_SEC_SHIFT)
+#define NFI_STRADDR		0x80
+#define NFI_BYTELEN		0x84
+#define NFI_CSEL		0x90
+#define NFI_FDML(x)		(0xa0 + (x) * sizeof(u32) * 2)
+#define NFI_FDMM(x)		(0xa4 + (x) * sizeof(u32) * 2)
+#define NFI_MASTER_STA		0x224
+#define		MASTER_STA_MASK		0x0fff
+/* NFI_SPI control */
+#define SNFI_MAC_OUTL		0x504
+#define SNFI_MAC_INL		0x508
+#define SNFI_RD_CTL2		0x510
+#define		RD_CMD_MASK		0x00ff
+#define		RD_DUMMY_SHIFT		8
+#define SNFI_RD_CTL3		0x514
+#define		RD_ADDR_MASK		0xffff
+#define SNFI_MISC_CTL		0x538
+#define		RD_MODE_X2		BIT(16)
+#define		RD_MODE_X4		(2UL << 16)
+#define		RD_QDUAL_IO		(4UL << 16)
+#define		RD_MODE_MASK		(7UL << 16)
+#define		RD_CUSTOM_EN		BIT(6)
+#define		WR_CUSTOM_EN		BIT(7)
+#define		WR_X4_EN		BIT(20)
+#define		SW_RST			BIT(28)
+#define SNFI_MISC_CTL2		0x53c
+#define		WR_LEN_SHIFT		16
+#define SNFI_PG_CTL1		0x524
+#define		WR_LOAD_CMD_SHIFT	8
+#define SNFI_PG_CTL2		0x528
+#define		WR_LOAD_ADDR_MASK	0xffff
+#define SNFI_MAC_CTL		0x500
+#define		MAC_WIP			BIT(0)
+#define		MAC_WIP_READY		BIT(1)
+#define		MAC_TRIG		BIT(2)
+#define		MAC_EN			BIT(3)
+#define		MAC_SIO_SEL		BIT(4)
+#define SNFI_STA_CTL1		0x550
+#define		SPI_STATE_IDLE		0xf
+#define SNFI_CNFG		0x55c
+#define		SNFI_MODE_EN		BIT(0)
+#define SNFI_GPRAM_DATA		0x800
+#define		SNFI_GPRAM_MAX_LEN	16
+
+/* Dummy command trigger NFI to spi mode */
+#define NAND_CMD_DUMMYREAD	0x00
+#define NAND_CMD_DUMMYPROG	0x80
+
+#define MTK_TIMEOUT		500000
+#define MTK_RESET_TIMEOUT	1000000
+#define MTK_SNFC_MIN_SPARE	16
+#define KB(x)			((x) * 1024UL)
+
+/*
+ * supported spare size of each IP.
+ * order should be the same with the spare size bitfiled defination of
+ * register NFI_PAGEFMT.
+ */
+static const u8 spare_size_mt7622[] = {
+	16, 26, 27, 28
+};
+
+struct mtk_snfi_caps {
+	const u8 *spare_size;
+	u8 num_spare_size;
+	u32 nand_sec_size;
+	u8 nand_fdm_size;
+	u8 nand_fdm_ecc_size;
+	u8 ecc_parity_bits;
+	u8 pageformat_spare_shift;
+	u8 bad_mark_swap;
+};
+
+struct mtk_snfi_bad_mark_ctl {
+	void (*bm_swap)(struct spi_mem *mem, u8 *buf, int raw);
+	u32 sec;
+	u32 pos;
+};
+
+struct mtk_snfi_nand_chip {
+	struct mtk_snfi_bad_mark_ctl bad_mark;
+	u32 spare_per_sector;
+};
+
+struct mtk_snfi_clk {
+	struct clk *nfi_clk;
+	struct clk *spi_clk;
+};
+
+struct mtk_snfi {
+	const struct mtk_snfi_caps *caps;
+	struct mtk_snfi_nand_chip snfi_nand;
+	struct mtk_snfi_clk clk;
+	struct mtk_ecc_config ecc_cfg;
+	struct mtk_ecc *ecc;
+	struct completion done;
+	struct device *dev;
+
+	void __iomem *regs;
+
+	u8 *buffer;
+};
+
+static inline u8 *oob_ptr(struct spi_mem *mem, int i)
+{
+	struct spinand_device *spinand = spi_mem_get_drvdata(mem);
+	struct mtk_snfi *snfi = spi_controller_get_devdata(mem->spi->master);
+	struct mtk_snfi_nand_chip *snfi_nand = &snfi->snfi_nand;
+	u8 *poi;
+
+	/* map the sector's FDM data to free oob:
+	 * the beginning of the oob area stores the FDM data of bad mark
+	 */
+
+	if (i < snfi_nand->bad_mark.sec)
+		poi = spinand->oobbuf + (i + 1) * snfi->caps->nand_fdm_size;
+	else if (i == snfi_nand->bad_mark.sec)
+		poi = spinand->oobbuf;
+	else
+		poi = spinand->oobbuf + i * snfi->caps->nand_fdm_size;
+
+	return poi;
+}
+
+static inline int mtk_data_len(struct spi_mem *mem)
+{
+	struct mtk_snfi *snfi = spi_controller_get_devdata(mem->spi->master);
+	struct mtk_snfi_nand_chip *snfi_nand = &snfi->snfi_nand;
+
+	return snfi->caps->nand_sec_size + snfi_nand->spare_per_sector;
+}
+
+static inline u8 *mtk_oob_ptr(struct spi_mem *mem,
+			      const u8 *p, int i)
+{
+	struct mtk_snfi *snfi = spi_controller_get_devdata(mem->spi->master);
+
+	return (u8 *)p + i * mtk_data_len(mem) + snfi->caps->nand_sec_size;
+}
+
+static void mtk_snfi_bad_mark_swap(struct spi_mem *mem,
+				   u8 *buf, int raw)
+{
+	struct spinand_device *spinand = spi_mem_get_drvdata(mem);
+	struct mtk_snfi *snfi = spi_controller_get_devdata(mem->spi->master);
+	struct mtk_snfi_nand_chip *snfi_nand = &snfi->snfi_nand;
+	u32 bad_pos = snfi_nand->bad_mark.pos;
+
+	if (raw)
+		bad_pos += snfi_nand->bad_mark.sec * mtk_data_len(mem);
+	else
+		bad_pos += snfi_nand->bad_mark.sec * snfi->caps->nand_sec_size;
+
+	swap(spinand->oobbuf[0], buf[bad_pos]);
+}
+
+static void mtk_snfi_set_bad_mark_ctl(struct mtk_snfi_bad_mark_ctl *bm_ctl,
+				      struct spi_mem *mem)
+{
+	struct spinand_device *spinand = spi_mem_get_drvdata(mem);
+	struct mtd_info *mtd = spinand_to_mtd(spinand);
+
+	bm_ctl->bm_swap = mtk_snfi_bad_mark_swap;
+	bm_ctl->sec = mtd->writesize / mtk_data_len(mem);
+	bm_ctl->pos = mtd->writesize % mtk_data_len(mem);
+}
+
+static void mtk_snfi_mac_enable(struct mtk_snfi *snfi)
+{
+	u32 mac;
+
+	mac = readl(snfi->regs + SNFI_MAC_CTL);
+	mac &= ~MAC_SIO_SEL;
+	mac |= MAC_EN;
+
+	writel(mac, snfi->regs + SNFI_MAC_CTL);
+}
+
+static int mtk_snfi_mac_trigger(struct mtk_snfi *snfi)
+{
+	u32 mac, reg;
+	int ret = 0;
+
+	mac = readl(snfi->regs + SNFI_MAC_CTL);
+	mac |= MAC_TRIG;
+	writel(mac, snfi->regs + SNFI_MAC_CTL);
+
+	ret = readl_poll_timeout_atomic(snfi->regs + SNFI_MAC_CTL, reg,
+					reg & MAC_WIP_READY, 10,
+					MTK_TIMEOUT);
+	if (ret < 0) {
+		dev_err(snfi->dev, "polling wip ready for read timeout\n");
+		return -EIO;
+	}
+
+	ret = readl_poll_timeout_atomic(snfi->regs + SNFI_MAC_CTL, reg,
+					!(reg & MAC_WIP), 10,
+					MTK_TIMEOUT);
+	if (ret < 0) {
+		dev_err(snfi->dev, "polling flash update timeout\n");
+		return -EIO;
+	}
+
+	return ret;
+}
+
+static void mtk_snfi_mac_leave(struct mtk_snfi *snfi)
+{
+	u32 mac;
+
+	mac = readl(snfi->regs + SNFI_MAC_CTL);
+	mac &= ~(MAC_TRIG | MAC_EN | MAC_SIO_SEL);
+	writel(mac, snfi->regs + SNFI_MAC_CTL);
+}
+
+static int mtk_snfi_mac_op(struct mtk_snfi *snfi)
+{
+	int ret = 0;
+
+	mtk_snfi_mac_enable(snfi);
+
+	ret = mtk_snfi_mac_trigger(snfi);
+	if (ret)
+		return ret;
+
+	mtk_snfi_mac_leave(snfi);
+
+	return ret;
+}
+
+static irqreturn_t mtk_snfi_irq(int irq, void *id)
+{
+	struct mtk_snfi *snfi = id;
+	u16 sta, ien;
+
+	sta = readw(snfi->regs + NFI_INTR_STA);
+	ien = readw(snfi->regs + NFI_INTR_EN);
+
+	if (!(sta & ien))
+		return IRQ_NONE;
+
+	writew(~sta & ien, snfi->regs + NFI_INTR_EN);
+	complete(&snfi->done);
+
+	return IRQ_HANDLED;
+}
+
+static int mtk_snfi_enable_clk(struct device *dev, struct mtk_snfi_clk *clk)
+{
+	int ret;
+
+	ret = clk_prepare_enable(clk->nfi_clk);
+	if (ret) {
+		dev_err(dev, "failed to enable nfi clk\n");
+		return ret;
+	}
+
+	ret = clk_prepare_enable(clk->spi_clk);
+	if (ret) {
+		dev_err(dev, "failed to enable spi clk\n");
+		clk_disable_unprepare(clk->nfi_clk);
+		return ret;
+	}
+
+	return 0;
+}
+
+static void mtk_snfi_disable_clk(struct mtk_snfi_clk *clk)
+{
+	clk_disable_unprepare(clk->nfi_clk);
+	clk_disable_unprepare(clk->spi_clk);
+}
+
+static int mtk_snfi_reset(struct mtk_snfi *snfi)
+{
+	u32 val;
+	int ret;
+
+	/* SW reset controller */
+	val = readl(snfi->regs + SNFI_MISC_CTL) | SW_RST;
+	writel(val, snfi->regs + SNFI_MISC_CTL);
+
+	ret = readw_poll_timeout(snfi->regs + SNFI_STA_CTL1, val,
+				 !(val & SPI_STATE_IDLE), 50,
+				 MTK_RESET_TIMEOUT);
+	if (ret) {
+		dev_warn(snfi->dev, "spi state active in reset [0x%x] = 0x%x\n",
+			 SNFI_STA_CTL1, val);
+		return ret;
+	}
+
+	val = readl(snfi->regs + SNFI_MISC_CTL);
+	val &= ~SW_RST;
+	writel(val, snfi->regs + SNFI_MISC_CTL);
+
+	/* reset all registers and force the NFI master to terminate */
+	writew(CON_FIFO_FLUSH | CON_NFI_RST, snfi->regs + NFI_CON);
+	ret = readw_poll_timeout(snfi->regs + NFI_STA, val,
+				 !(val & (NFI_FSM_MASK | NAND_FSM_MASK)), 50,
+				 MTK_RESET_TIMEOUT);
+	if (ret) {
+		dev_warn(snfi->dev, "nfi active in reset [0x%x] = 0x%x\n",
+			 NFI_STA, val);
+		return ret;
+	}
+
+	return 0;
+}
+
+static int mtk_snfi_set_spare_per_sector(struct spinand_device *spinand,
+					 const struct mtk_snfi_caps *caps,
+					 u32 *sps)
+{
+	struct mtd_info *mtd = spinand_to_mtd(spinand);
+	const u8 *spare = caps->spare_size;
+	u32 sectors, i, closest_spare = 0;
+
+	sectors = mtd->writesize / caps->nand_sec_size;
+	*sps = mtd->oobsize / sectors;
+
+	if (*sps < MTK_SNFC_MIN_SPARE)
+		return -EINVAL;
+
+	for (i = 0; i < caps->num_spare_size; i++) {
+		if (*sps >= spare[i] && spare[i] >= spare[closest_spare]) {
+			closest_spare = i;
+			if (*sps == spare[i])
+				break;
+		}
+	}
+
+	*sps = spare[closest_spare];
+
+	return 0;
+}
+
+static void mtk_snfi_read_fdm_data(struct spi_mem *mem,
+				   u32 sectors)
+{
+	struct mtk_snfi *snfi = spi_controller_get_devdata(mem->spi->master);
+	const struct mtk_snfi_caps *caps = snfi->caps;
+	u32 vall, valm;
+	int i, j;
+	u8 *oobptr;
+
+	for (i = 0; i < sectors; i++) {
+		oobptr = oob_ptr(mem, i);
+		vall = readl(snfi->regs + NFI_FDML(i));
+		valm = readl(snfi->regs + NFI_FDMM(i));
+
+		for (j = 0; j < caps->nand_fdm_size; j++)
+			oobptr[j] = (j >= 4 ? valm : vall) >> ((j % 4) * 8);
+	}
+}
+
+static void mtk_snfi_write_fdm_data(struct spi_mem *mem,
+				    u32 sectors)
+{
+	struct mtk_snfi *snfi = spi_controller_get_devdata(mem->spi->master);
+	const struct mtk_snfi_caps *caps = snfi->caps;
+	u32 vall, valm;
+	int i, j;
+	u8 *oobptr;
+
+	for (i = 0; i < sectors; i++) {
+		oobptr = oob_ptr(mem, i);
+		vall = 0;
+		valm = 0;
+		for (j = 0; j < 8; j++) {
+			if (j < 4)
+				vall |= (j < caps->nand_fdm_size ? oobptr[j] :
+					 0xff) << (j * 8);
+			else
+				valm |= (j < caps->nand_fdm_size ? oobptr[j] :
+					 0xff) << ((j - 4) * 8);
+		}
+		writel(vall, snfi->regs + NFI_FDML(i));
+		writel(valm, snfi->regs + NFI_FDMM(i));
+	}
+}
+
+static int mtk_snfi_update_ecc_stats(struct spi_mem *mem,
+				     u8 *buf, u32 sectors)
+{
+	struct spinand_device *spinand = spi_mem_get_drvdata(mem);
+	struct mtd_info *mtd = spinand_to_mtd(spinand);
+	struct mtk_snfi *snfi = spi_controller_get_devdata(mem->spi->master);
+	struct mtk_ecc_stats stats;
+	int rc, i;
+
+	rc = readl(snfi->regs + NFI_STA) & STA_EMP_PAGE;
+	if (rc) {
+		memset(buf, 0xff, sectors * snfi->caps->nand_sec_size);
+		for (i = 0; i < sectors; i++)
+			memset(spinand->oobbuf, 0xff,
+			       snfi->caps->nand_fdm_size);
+		return 0;
+	}
+
+	mtk_ecc_get_stats(snfi->ecc, &stats, sectors);
+	mtd->ecc_stats.corrected += stats.corrected;
+	mtd->ecc_stats.failed += stats.failed;
+
+	return 0;
+}
+
+static int mtk_snfi_hw_runtime_config(struct spi_mem *mem)
+{
+	struct spinand_device *spinand = spi_mem_get_drvdata(mem);
+	struct mtd_info *mtd = spinand_to_mtd(spinand);
+	struct nand_device *nand = mtd_to_nanddev(mtd);
+	struct mtk_snfi *snfi = spi_controller_get_devdata(mem->spi->master);
+	const struct mtk_snfi_caps *caps = snfi->caps;
+	struct mtk_snfi_nand_chip *snfi_nand = &snfi->snfi_nand;
+	u32 fmt, spare, i = 0;
+	int ret;
+
+	ret = mtk_snfi_set_spare_per_sector(spinand, caps, &spare);
+	if (ret)
+		return ret;
+
+	/* calculate usable oob bytes for ecc parity data */
+	snfi_nand->spare_per_sector = spare;
+	spare -= caps->nand_fdm_size;
+
+	nand->memorg.oobsize = snfi_nand->spare_per_sector
+		* (mtd->writesize / caps->nand_sec_size);
+	mtd->oobsize = nanddev_per_page_oobsize(nand);
+
+	snfi->ecc_cfg.strength = (spare << 3) / caps->ecc_parity_bits;
+	mtk_ecc_adjust_strength(snfi->ecc, &snfi->ecc_cfg.strength);
+
+	switch (mtd->writesize) {
+	case 512:
+		fmt = PAGEFMT_512;
+		break;
+	case KB(2):
+		fmt = PAGEFMT_2K;
+		break;
+	case KB(4):
+		fmt = PAGEFMT_4K;
+		break;
+	default:
+		dev_err(snfi->dev, "invalid page len: %d\n", mtd->writesize);
+		return -EINVAL;
+	}
+
+	/* Setup PageFormat */
+	while (caps->spare_size[i] != snfi_nand->spare_per_sector) {
+		i++;
+		if (i == (caps->num_spare_size - 1)) {
+			dev_err(snfi->dev, "invalid spare size %d\n",
+				snfi_nand->spare_per_sector);
+			return -EINVAL;
+		}
+	}
+
+	fmt |= i << caps->pageformat_spare_shift;
+	fmt |= caps->nand_fdm_size << PAGEFMT_FDM_SHIFT;
+	fmt |= caps->nand_fdm_ecc_size << PAGEFMT_FDM_ECC_SHIFT;
+	writel(fmt, snfi->regs + NFI_PAGEFMT);
+
+	snfi->ecc_cfg.len = caps->nand_sec_size + caps->nand_fdm_ecc_size;
+
+	mtk_snfi_set_bad_mark_ctl(&snfi_nand->bad_mark, mem);
+
+	return 0;
+}
+
+static int mtk_snfi_read_from_cache(struct spi_mem *mem,
+				    const struct spi_mem_op *op, int oob_on)
+{
+	struct mtk_snfi *snfi = spi_controller_get_devdata(mem->spi->master);
+	struct spinand_device *spinand = spi_mem_get_drvdata(mem);
+	struct mtd_info *mtd = spinand_to_mtd(spinand);
+	u32 sectors = mtd->writesize / snfi->caps->nand_sec_size;
+	struct mtk_snfi_nand_chip *snfi_nand = &snfi->snfi_nand;
+	u32 reg, len, col_addr = 0;
+	int dummy_cycle, ret;
+	dma_addr_t dma_addr;
+
+	len = sectors * (snfi->caps->nand_sec_size
+	      + snfi_nand->spare_per_sector);
+
+	dma_addr = dma_map_single(snfi->dev, snfi->buffer,
+				  len, DMA_FROM_DEVICE);
+	ret = dma_mapping_error(snfi->dev, dma_addr);
+	if (ret) {
+		dev_err(snfi->dev, "dma mapping error\n");
+		return -EINVAL;
+	}
+
+	/* set Read cache command and dummy cycle */
+	dummy_cycle = (op->dummy.nbytes << 3) >> (ffs(op->dummy.buswidth) - 1);
+	reg = ((op->cmd.opcode & RD_CMD_MASK) |
+	       (dummy_cycle << RD_DUMMY_SHIFT));
+	writel(reg, snfi->regs + SNFI_RD_CTL2);
+
+	writel((col_addr & RD_ADDR_MASK), snfi->regs + SNFI_RD_CTL3);
+
+	reg = readl(snfi->regs + SNFI_MISC_CTL);
+	reg |= RD_CUSTOM_EN;
+	reg &= ~(RD_MODE_MASK | WR_X4_EN);
+
+	/* set data and addr buswidth */
+	if (op->data.buswidth == 4)
+		reg |= RD_MODE_X4;
+	else if (op->data.buswidth == 2)
+		reg |= RD_MODE_X2;
+
+	if (op->addr.buswidth == 4 || op->addr.buswidth == 2)
+		reg |= RD_QDUAL_IO;
+	writel(reg, snfi->regs + SNFI_MISC_CTL);
+
+	writel(len, snfi->regs + SNFI_MISC_CTL2);
+	writew(sectors << CON_SEC_SHIFT, snfi->regs + NFI_CON);
+	reg = readw(snfi->regs + NFI_CNFG);
+	reg |= CNFG_READ_EN | CNFG_DMA_BURST_EN | CNFG_DMA | CNFG_OP_CUST;
+
+	if (!oob_on) {
+		reg |= CNFG_AUTO_FMT_EN | CNFG_HW_ECC_EN;
+		writew(reg, snfi->regs + NFI_CNFG);
+
+		snfi->ecc_cfg.mode = ECC_NFI_MODE;
+		snfi->ecc_cfg.sectors = sectors;
+		snfi->ecc_cfg.op = ECC_DECODE;
+		ret = mtk_ecc_enable(snfi->ecc, &snfi->ecc_cfg);
+		if (ret) {
+			dev_err(snfi->dev, "ecc enable failed\n");
+			/* clear NFI_CNFG */
+			reg &= ~(CNFG_READ_EN | CNFG_DMA_BURST_EN | CNFG_DMA |
+				CNFG_AUTO_FMT_EN | CNFG_HW_ECC_EN);
+			writew(reg, snfi->regs + NFI_CNFG);
+			goto out;
+		}
+	} else {
+		writew(reg, snfi->regs + NFI_CNFG);
+	}
+
+	writel(lower_32_bits(dma_addr), snfi->regs + NFI_STRADDR);
+	readw(snfi->regs + NFI_INTR_STA);
+	writew(INTR_AHB_DONE_EN, snfi->regs + NFI_INTR_EN);
+
+	init_completion(&snfi->done);
+
+	/* set dummy command to trigger NFI enter SPI mode */
+	writew(NAND_CMD_DUMMYREAD, snfi->regs + NFI_CMD);
+	reg = readl(snfi->regs + NFI_CON) | CON_BRD;
+	writew(reg, snfi->regs + NFI_CON);
+
+	ret = wait_for_completion_timeout(&snfi->done, msecs_to_jiffies(500));
+	if (!ret) {
+		dev_err(snfi->dev, "read ahb done timeout\n");
+		writew(0, snfi->regs + NFI_INTR_EN);
+		ret = -ETIMEDOUT;
+		goto out;
+	}
+
+	ret = readl_poll_timeout_atomic(snfi->regs + NFI_BYTELEN, reg,
+					ADDRCNTR_SEC(reg) >= sectors, 10,
+					MTK_TIMEOUT);
+	if (ret < 0) {
+		dev_err(snfi->dev, "polling read byte len timeout\n");
+		ret = -EIO;
+	} else {
+		if (!oob_on) {
+			ret = mtk_ecc_wait_done(snfi->ecc, ECC_DECODE);
+			if (ret) {
+				dev_warn(snfi->dev, "wait ecc done timeout\n");
+			} else {
+				mtk_snfi_update_ecc_stats(mem, snfi->buffer,
+							  sectors);
+				mtk_snfi_read_fdm_data(mem, sectors);
+			}
+		}
+	}
+
+	if (oob_on)
+		goto out;
+
+	mtk_ecc_disable(snfi->ecc);
+out:
+	dma_unmap_single(snfi->dev, dma_addr, len, DMA_FROM_DEVICE);
+	writel(0, snfi->regs + NFI_CON);
+	writel(0, snfi->regs + NFI_CNFG);
+	reg = readl(snfi->regs + SNFI_MISC_CTL);
+	reg &= ~RD_CUSTOM_EN;
+	writel(reg, snfi->regs + SNFI_MISC_CTL);
+
+	return ret;
+}
+
+static int mtk_snfi_write_to_cache(struct spi_mem *mem,
+				   const struct spi_mem_op *op,
+				   int oob_on)
+{
+	struct mtk_snfi *snfi = spi_controller_get_devdata(mem->spi->master);
+	struct spinand_device *spinand = spi_mem_get_drvdata(mem);
+	struct mtd_info *mtd = spinand_to_mtd(spinand);
+	u32 sectors = mtd->writesize / snfi->caps->nand_sec_size;
+	struct mtk_snfi_nand_chip *snfi_nand = &snfi->snfi_nand;
+	u32 reg, len, col_addr = 0;
+	dma_addr_t dma_addr;
+	int ret;
+
+	len = sectors * (snfi->caps->nand_sec_size
+	      + snfi_nand->spare_per_sector);
+
+	dma_addr = dma_map_single(snfi->dev, snfi->buffer, len,
+				  DMA_TO_DEVICE);
+	ret = dma_mapping_error(snfi->dev, dma_addr);
+	if (ret) {
+		dev_err(snfi->dev, "dma mapping error\n");
+		return -EINVAL;
+	}
+
+	/* set program load cmd and address */
+	reg = (op->cmd.opcode << WR_LOAD_CMD_SHIFT);
+	writel(reg, snfi->regs + SNFI_PG_CTL1);
+	writel(col_addr & WR_LOAD_ADDR_MASK, snfi->regs + SNFI_PG_CTL2);
+
+	reg = readl(snfi->regs + SNFI_MISC_CTL);
+	reg |= WR_CUSTOM_EN;
+	reg &= ~(RD_MODE_MASK | WR_X4_EN);
+
+	if (op->data.buswidth == 4)
+		reg |= WR_X4_EN;
+	writel(reg, snfi->regs + SNFI_MISC_CTL);
+
+	writel(len << WR_LEN_SHIFT, snfi->regs + SNFI_MISC_CTL2);
+	writew(sectors << CON_SEC_SHIFT, snfi->regs + NFI_CON);
+
+	reg = readw(snfi->regs + NFI_CNFG);
+	reg &= ~(CNFG_READ_EN | CNFG_BYTE_RW);
+	reg |= CNFG_DMA | CNFG_DMA_BURST_EN | CNFG_OP_PROGRAM;
+
+	if (!oob_on) {
+		reg |= CNFG_AUTO_FMT_EN | CNFG_HW_ECC_EN;
+		writew(reg, snfi->regs + NFI_CNFG);
+
+		snfi->ecc_cfg.mode = ECC_NFI_MODE;
+		snfi->ecc_cfg.op = ECC_ENCODE;
+		ret = mtk_ecc_enable(snfi->ecc, &snfi->ecc_cfg);
+		if (ret) {
+			dev_err(snfi->dev, "ecc enable failed\n");
+			/* clear NFI_CNFG */
+			reg &= ~(CNFG_DMA_BURST_EN | CNFG_DMA |
+					CNFG_AUTO_FMT_EN | CNFG_HW_ECC_EN);
+			writew(reg, snfi->regs + NFI_CNFG);
+			dma_unmap_single(snfi->dev, dma_addr, len,
+					 DMA_FROM_DEVICE);
+			goto out;
+		}
+		/* write OOB into the FDM registers (OOB area in MTK NAND) */
+		mtk_snfi_write_fdm_data(mem, sectors);
+	} else {
+		writew(reg, snfi->regs + NFI_CNFG);
+	}
+	writel(lower_32_bits(dma_addr), snfi->regs + NFI_STRADDR);
+	readw(snfi->regs + NFI_INTR_STA);
+	writew(INTR_AHB_DONE_EN, snfi->regs + NFI_INTR_EN);
+
+	init_completion(&snfi->done);
+
+	/* set dummy command to trigger NFI enter SPI mode */
+	writew(NAND_CMD_DUMMYPROG, snfi->regs + NFI_CMD);
+	reg = readl(snfi->regs + NFI_CON) | CON_BWR;
+	writew(reg, snfi->regs + NFI_CON);
+
+	ret = wait_for_completion_timeout(&snfi->done, msecs_to_jiffies(500));
+	if (!ret) {
+		dev_err(snfi->dev, "custom program done timeout\n");
+		writew(0, snfi->regs + NFI_INTR_EN);
+		ret = -ETIMEDOUT;
+		goto ecc_disable;
+	}
+
+	ret = readl_poll_timeout_atomic(snfi->regs + NFI_ADDRCNTR, reg,
+					ADDRCNTR_SEC(reg) >= sectors,
+					10, MTK_TIMEOUT);
+	if (ret)
+		dev_err(snfi->dev, "hwecc write timeout\n");
+
+ecc_disable:
+	mtk_ecc_disable(snfi->ecc);
+
+out:
+	dma_unmap_single(snfi->dev, dma_addr, len, DMA_TO_DEVICE);
+	writel(0, snfi->regs + NFI_CON);
+	writel(0, snfi->regs + NFI_CNFG);
+	reg = readl(snfi->regs + SNFI_MISC_CTL);
+	reg &= ~WR_CUSTOM_EN;
+	writel(reg, snfi->regs + SNFI_MISC_CTL);
+
+	return ret;
+}
+
+static int mtk_snfi_read(struct spi_mem *mem,
+			 const struct spi_mem_op *op)
+{
+	struct spinand_device *spinand = spi_mem_get_drvdata(mem);
+	struct mtk_snfi *snfi = spi_controller_get_devdata(mem->spi->master);
+	struct mtd_info *mtd = spinand_to_mtd(spinand);
+	struct mtk_snfi_nand_chip *snfi_nand = &snfi->snfi_nand;
+	u32 col_addr = op->addr.val;
+	int i, ret, sectors, oob_on = false;
+
+	if (col_addr == mtd->writesize)
+		oob_on = true;
+
+	ret = mtk_snfi_read_from_cache(mem, op, oob_on);
+	if (ret) {
+		dev_warn(snfi->dev, "read from cache fail\n");
+		return ret;
+	}
+
+	sectors = mtd->writesize / snfi->caps->nand_sec_size;
+	for (i = 0; i < sectors; i++) {
+		if (oob_on)
+			memcpy(oob_ptr(mem, i),
+			       mtk_oob_ptr(mem, snfi->buffer, i),
+			       snfi->caps->nand_fdm_size);
+
+		if (i == snfi_nand->bad_mark.sec && snfi->caps->bad_mark_swap)
+			snfi_nand->bad_mark.bm_swap(mem, snfi->buffer,
+							oob_on);
+	}
+
+	if (!oob_on)
+		memcpy(spinand->databuf, snfi->buffer, mtd->writesize);
+
+	return ret;
+}
+
+static int mtk_snfi_write(struct spi_mem *mem,
+			  const struct spi_mem_op *op)
+{
+	struct spinand_device *spinand = spi_mem_get_drvdata(mem);
+	struct mtk_snfi *snfi = spi_controller_get_devdata(mem->spi->master);
+	struct mtd_info *mtd = spinand_to_mtd(spinand);
+	struct mtk_snfi_nand_chip *snfi_nand = &snfi->snfi_nand;
+	u32 ret, i, sectors, col_addr = op->addr.val;
+	int oob_on = false;
+
+	if (col_addr == mtd->writesize)
+		oob_on = true;
+
+	sectors = mtd->writesize / snfi->caps->nand_sec_size;
+	memset(snfi->buffer, 0xff, mtd->writesize + mtd->oobsize);
+
+	if (!oob_on)
+		memcpy(snfi->buffer, spinand->databuf, mtd->writesize);
+
+	for (i = 0; i < sectors; i++) {
+		if (i == snfi_nand->bad_mark.sec && snfi->caps->bad_mark_swap)
+			snfi_nand->bad_mark.bm_swap(mem, snfi->buffer, oob_on);
+
+		if (oob_on)
+			memcpy(mtk_oob_ptr(mem, snfi->buffer, i),
+			       oob_ptr(mem, i),
+			       snfi->caps->nand_fdm_size);
+	}
+
+	ret = mtk_snfi_write_to_cache(mem, op, oob_on);
+	if (ret)
+		dev_warn(snfi->dev, "write to cache fail\n");
+
+	return ret;
+}
+
+static int mtk_snfi_command_exec(struct mtk_snfi *snfi,
+				 const u8 *txbuf, u8 *rxbuf,
+				 const u32 txlen, const u32 rxlen)
+{
+	u32 tmp, i, j, reg, m;
+	u8 *p_tmp = (u8 *)(&tmp);
+	int ret = 0;
+
+	/* Moving tx data to NFI_SPI GPRAM */
+	for (i = 0, m = 0; i < txlen; ) {
+		for (j = 0, tmp = 0; i < txlen && j < 4; i++, j++)
+			p_tmp[j] = txbuf[i];
+
+		writel(tmp, snfi->regs + SNFI_GPRAM_DATA + m);
+		m += 4;
+	}
+
+	writel(txlen, snfi->regs + SNFI_MAC_OUTL);
+	writel(rxlen, snfi->regs + SNFI_MAC_INL);
+	ret = mtk_snfi_mac_op(snfi);
+	if (ret)
+		return ret;
+
+	/* For NULL input data, this loop will be skipped */
+	if (rxlen)
+		for (i = 0, m = 0; i < rxlen; ) {
+			reg = readl(snfi->regs +
+					    SNFI_GPRAM_DATA + m);
+			for (j = 0; i < rxlen && j < 4; i++, j++, rxbuf++) {
+				if (m == 0 && i == 0)
+					j = i + txlen;
+				*rxbuf = (reg >> (j * 8)) & 0xFF;
+			}
+			m += 4;
+		}
+
+	return ret;
+}
+
+/*
+ * mtk_snfi_exec_op - to process command/data to send to the
+ * SPI NAND by mtk controller
+ */
+static int mtk_snfi_exec_op(struct spi_mem *mem,
+			    const struct spi_mem_op *op)
+
+{
+	struct mtk_snfi *snfi = spi_controller_get_devdata(mem->spi->master);
+	struct spinand_device *spinand = spi_mem_get_drvdata(mem);
+	struct mtd_info *mtd = spinand_to_mtd(spinand);
+	struct nand_device *nand = mtd_to_nanddev(mtd);
+	const struct spi_mem_op *read_cache;
+	const struct spi_mem_op *write_cache;
+	u32 tmpbufsize, txlen = 0, rxlen = 0;
+	u8 *txbuf, *rxbuf = NULL, *buf;
+	int i, ret = 0;
+
+	ret = mtk_snfi_reset(snfi);
+	if (ret) {
+		dev_warn(snfi->dev, "reset spi memory controller fail\n");
+		return ret;
+	}
+
+	/*if bbt initial, framework have detect nand information */
+	if (nand->bbt.cache) {
+		read_cache = spinand->op_templates.read_cache;
+		write_cache = spinand->op_templates.write_cache;
+
+		ret = mtk_snfi_hw_runtime_config(mem);
+		if (ret)
+			return ret;
+
+		/* For Read/Write with cache, Erase use framework flow */
+		if (op->cmd.opcode == read_cache->cmd.opcode) {
+			ret = mtk_snfi_read(mem, op);
+			if (ret)
+				dev_warn(snfi->dev, "snfi read fail\n");
+			return ret;
+		} else if (op->cmd.opcode == write_cache->cmd.opcode) {
+			ret = mtk_snfi_write(mem, op);
+			if (ret)
+				dev_warn(snfi->dev, "snfi write fail\n");
+			return ret;
+		}
+	}
+
+	tmpbufsize = sizeof(op->cmd.opcode) + op->addr.nbytes +
+		     op->dummy.nbytes + op->data.nbytes;
+
+	txbuf = kzalloc(tmpbufsize, GFP_KERNEL);
+	if (!txbuf)
+		return -ENOMEM;
+
+	txbuf[txlen++] = op->cmd.opcode;
+
+	if (op->addr.nbytes)
+		for (i = 0; i < op->addr.nbytes; i++)
+			txbuf[txlen++] = op->addr.val >>
+					(8 * (op->addr.nbytes - i - 1));
+
+	txlen += op->dummy.nbytes;
+
+	if (op->data.dir == SPI_MEM_DATA_OUT)
+		for (i = 0; i < op->data.nbytes; i++) {
+			buf = (u8 *)op->data.buf.out;
+			txbuf[txlen++] = buf[i];
+		}
+
+	if (op->data.dir == SPI_MEM_DATA_IN) {
+		rxbuf = (u8 *)op->data.buf.in;
+		rxlen += op->data.nbytes;
+	}
+
+	ret = mtk_snfi_command_exec(snfi, txbuf, rxbuf, txlen, rxlen);
+	kfree(txbuf);
+
+	return ret;
+}
+
+static int mtk_snfi_init(struct mtk_snfi *snfi)
+{
+	int ret;
+
+	/* Reset the state machine and data FIFO */
+	ret = mtk_snfi_reset(snfi);
+	if (ret) {
+		dev_warn(snfi->dev, "MTK reset controller fail\n");
+		return ret;
+	}
+
+	snfi->buffer = devm_kzalloc(snfi->dev, 4096 + 256, GFP_KERNEL);
+	if (!snfi->buffer)
+		return  -ENOMEM;
+
+	/* Clear interrupt, read clear. */
+	readw(snfi->regs + NFI_INTR_STA);
+	writew(0, snfi->regs + NFI_INTR_EN);
+
+	writel(0, snfi->regs + NFI_CON);
+	writel(0, snfi->regs + NFI_CNFG);
+
+	/* Change to NFI_SPI mode. */
+	writel(SNFI_MODE_EN, snfi->regs + SNFI_CNFG);
+
+	return 0;
+}
+
+static int mtk_snfi_check_buswidth(u8 width)
+{
+	switch (width) {
+	case 1:
+	case 2:
+	case 4:
+		return 0;
+
+	default:
+		break;
+	}
+
+	return -ENOTSUPP;
+}
+
+static bool mtk_snfi_supports_op(struct spi_mem *mem,
+				 const struct spi_mem_op *op)
+{
+	int ret = 0;
+
+	/* For MTK Spi Nand controller, cmd buswidth just support 1 bit*/
+	if (op->cmd.buswidth != 1)
+		ret = -ENOTSUPP;
+
+	if (op->addr.nbytes)
+		ret |= mtk_snfi_check_buswidth(op->addr.buswidth);
+
+	if (op->dummy.nbytes)
+		ret |= mtk_snfi_check_buswidth(op->dummy.buswidth);
+
+	if (op->data.nbytes)
+		ret |= mtk_snfi_check_buswidth(op->data.buswidth);
+
+	if (ret)
+		return false;
+
+	return true;
+}
+
+static const struct spi_controller_mem_ops mtk_snfi_ops = {
+	.supports_op = mtk_snfi_supports_op,
+	.exec_op = mtk_snfi_exec_op,
+};
+
+static const struct mtk_snfi_caps snfi_mt7622 = {
+	.spare_size = spare_size_mt7622,
+	.num_spare_size = 4,
+	.nand_sec_size = 512,
+	.nand_fdm_size = 8,
+	.nand_fdm_ecc_size = 1,
+	.ecc_parity_bits = 13,
+	.pageformat_spare_shift = 4,
+	.bad_mark_swap = 0,
+};
+
+static const struct of_device_id mtk_snfi_id_table[] = {
+	{ .compatible = "mediatek,mt7622-snfi", .data = &snfi_mt7622, },
+	{  /* sentinel */ }
+};
+
+static int mtk_snfi_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct device_node *np = dev->of_node;
+	struct spi_controller *ctlr;
+	struct mtk_snfi *snfi;
+	struct resource *res;
+	int ret = 0, irq;
+
+	ctlr = spi_alloc_master(&pdev->dev, sizeof(*snfi));
+	if (!ctlr)
+		return -ENOMEM;
+
+	snfi = spi_controller_get_devdata(ctlr);
+	snfi->caps = of_device_get_match_data(dev);
+	snfi->dev = dev;
+
+	snfi->ecc = of_mtk_ecc_get(np);
+	if (IS_ERR_OR_NULL(snfi->ecc))
+		goto err_put_master;
+
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	snfi->regs = devm_ioremap_resource(dev, res);
+	if (IS_ERR(snfi->regs)) {
+		ret = PTR_ERR(snfi->regs);
+		goto release_ecc;
+	}
+
+	/* find the clocks */
+	snfi->clk.nfi_clk = devm_clk_get(dev, "nfi_clk");
+	if (IS_ERR(snfi->clk.nfi_clk)) {
+		dev_err(dev, "no nfi clk\n");
+		ret = PTR_ERR(snfi->clk.nfi_clk);
+		goto release_ecc;
+	}
+
+	snfi->clk.spi_clk = devm_clk_get(dev, "spi_clk");
+	if (IS_ERR(snfi->clk.spi_clk)) {
+		dev_err(dev, "no spi clk\n");
+		ret = PTR_ERR(snfi->clk.spi_clk);
+		goto release_ecc;
+	}
+
+	ret = mtk_snfi_enable_clk(dev, &snfi->clk);
+	if (ret)
+		goto release_ecc;
+
+	/* find the irq */
+	irq = platform_get_irq(pdev, 0);
+	if (irq < 0) {
+		dev_err(dev, "no snfi irq resource\n");
+		ret = -EINVAL;
+		goto clk_disable;
+	}
+
+	ret = devm_request_irq(dev, irq, mtk_snfi_irq, 0, "mtk-snfi", snfi);
+	if (ret) {
+		dev_err(dev, "failed to request snfi irq\n");
+		goto clk_disable;
+	}
+
+	ret = dma_set_mask(dev, DMA_BIT_MASK(32));
+	if (ret) {
+		dev_err(dev, "failed to set dma mask\n");
+		goto clk_disable;
+	}
+
+	ctlr->dev.of_node = np;
+	ctlr->mem_ops = &mtk_snfi_ops;
+
+	platform_set_drvdata(pdev, snfi);
+	ret = mtk_snfi_init(snfi);
+	if (ret) {
+		dev_err(dev, "failed to init snfi\n");
+		goto clk_disable;
+	}
+
+	ret = devm_spi_register_master(dev, ctlr);
+	if (ret)
+		goto clk_disable;
+
+	return 0;
+
+clk_disable:
+	mtk_snfi_disable_clk(&snfi->clk);
+
+release_ecc:
+	mtk_ecc_release(snfi->ecc);
+
+err_put_master:
+	spi_master_put(ctlr);
+
+	dev_err(dev, "MediaTek SPI NAND interface probe failed %d\n", ret);
+	return ret;
+}
+
+static int mtk_snfi_remove(struct platform_device *pdev)
+{
+	struct mtk_snfi *snfi = platform_get_drvdata(pdev);
+
+	mtk_snfi_disable_clk(&snfi->clk);
+
+	return 0;
+}
+
+static int mtk_snfi_suspend(struct platform_device *pdev, pm_message_t state)
+{
+	struct mtk_snfi *snfi = platform_get_drvdata(pdev);
+
+	mtk_snfi_disable_clk(&snfi->clk);
+
+	return 0;
+}
+
+static int mtk_snfi_resume(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct mtk_snfi *snfi = dev_get_drvdata(dev);
+	int ret;
+
+	ret = mtk_snfi_enable_clk(dev, &snfi->clk);
+	if (ret)
+		return ret;
+
+	ret = mtk_snfi_init(snfi);
+	if (ret)
+		dev_err(dev, "failed to init snfi controller\n");
+
+	return ret;
+}
+
+static struct platform_driver mtk_snfi_driver = {
+	.driver = {
+		.name	= "mtk-snfi",
+		.of_match_table = mtk_snfi_id_table,
+	},
+	.probe		= mtk_snfi_probe,
+	.remove		= mtk_snfi_remove,
+	.suspend	= mtk_snfi_suspend,
+	.resume		= mtk_snfi_resume,
+};
+
+module_platform_driver(mtk_snfi_driver);
+
+MODULE_LICENSE("GPL v2");
+MODULE_AUTHOR("Xiangsheng Hou <xiangsheng.hou@mediatek.com>");
+MODULE_DESCRIPTION("Mediatek SPI Memory Interface Driver");
--- a/drivers/spi/Kconfig
+++ b/drivers/spi/Kconfig
@@ -389,6 +389,15 @@ config SPI_MT65XX
 	  say Y or M here.If you are not sure, say N.
 	  SPI drivers for Mediatek MT65XX and MT81XX series ARM SoCs.
 
+config SPI_MTK_SNFI
+       tristate "MediaTek SPI NAND interface"
+       select MTD_SPI_NAND
+       help
+         This selects the SPI NAND FLASH interface(SNFI),
+         which could be found on MediaTek Soc.
+         Say Y or M here.If you are not sure, say N.
+         Note Parallel Nand and SPI NAND is alternative on MediaTek SoCs.
+
 config SPI_NUC900
 	tristate "Nuvoton NUC900 series SPI"
 	depends on ARCH_W90X900
--- a/drivers/spi/Makefile
+++ b/drivers/spi/Makefile
@@ -57,6 +57,7 @@ obj-$(CONFIG_SPI_MPC512x_PSC)		+= spi-mp
 obj-$(CONFIG_SPI_MPC52xx_PSC)		+= spi-mpc52xx-psc.o
 obj-$(CONFIG_SPI_MPC52xx)		+= spi-mpc52xx.o
 obj-$(CONFIG_SPI_MT65XX)                += spi-mt65xx.o
+obj-$(CONFIG_SPI_MTK_SNFI)              += spi-mtk-snfi.o
 obj-$(CONFIG_SPI_MXS)			+= spi-mxs.o
 obj-$(CONFIG_SPI_NUC900)		+= spi-nuc900.o
 obj-$(CONFIG_SPI_OC_TINY)		+= spi-oc-tiny.o