forked from lede-project/source
-
Notifications
You must be signed in to change notification settings - Fork 4
/
0001-add-support-for-GL-AR300M-NAND-flash-support.patch
1536 lines (1522 loc) · 44 KB
/
0001-add-support-for-GL-AR300M-NAND-flash-support.patch
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
From 0e62cff2713e11e1aa89f95fce01f5a9cc1c80c0 Mon Sep 17 00:00:00 2001
From: Alfie Zhao <alzhao@gmail.com>
Date: Sat, 7 Jan 2017 10:03:41 +0800
Subject: [PATCH 1/1] add support for GL-AR300M NAND flash support Leds doesn't
work properly
---
.../ar71xx/base-files/lib/upgrade/platform.sh | 45 +-
target/linux/ar71xx/config-4.4 | 7 +-
.../ar71xx/files/drivers/mtd/nand/ath79_spinand.c | 1173 ++++++++++++++++++++
target/linux/ar71xx/image/legacy.mk | 34 +
target/linux/ar71xx/image/nand.mk | 19 +
target/linux/ar71xx/image/ubinize-gl-ar300m.ini | 26 +
target/linux/ar71xx/nand/config-default | 2 +
.../patches-4.4/911-add-ar300m-nand-support.patch | 82 ++
8 files changed, 1386 insertions(+), 2 deletions(-)
create mode 100644 target/linux/ar71xx/files/drivers/mtd/nand/ath79_spinand.c
create mode 100644 target/linux/ar71xx/image/ubinize-gl-ar300m.ini
create mode 100644 target/linux/ar71xx/patches-4.4/911-add-ar300m-nand-support.patch
diff --git a/target/linux/ar71xx/base-files/lib/upgrade/platform.sh b/target/linux/ar71xx/base-files/lib/upgrade/platform.sh
index 6341a31..d76c756 100755
--- a/target/linux/ar71xx/base-files/lib/upgrade/platform.sh
+++ b/target/linux/ar71xx/base-files/lib/upgrade/platform.sh
@@ -163,6 +163,36 @@ alfa_check_image() {
return 0
}
+gl_ar300m_is_nand() {
+ local size="$(mtd_get_part_size 'ubi')"
+ case "$size" in
+ 132120576)
+ return 0
+ ;;
+ *)
+ return 1
+ ;;
+ esac
+}
+
+# $(1) image file
+# $(2) board name
+# $(3) magic
+platform_check_image_gl_ar300m() {
+ local board=$2
+ local magic=$3
+
+ if gl_ar300m_is_nand; then
+ nand_do_platform_check $board $1
+ return $?
+ else
+ [ "$magic" != "2705" ] && {
+ echo "Invalid image type."
+ return 1
+ }
+ return 0
+ fi
+}
platform_check_image() {
local board=$(ar71xx_board_name)
@@ -209,7 +239,6 @@ platform_check_image() {
ew-dorin-router|\
ew-dorin|\
gl-ar150|\
- gl-ar300m|\
gl-ar300|\
gl-domino|\
gl-mifi|\
@@ -259,6 +288,10 @@ platform_check_image() {
return 0
;;
+ gl-ar300m)
+ platform_check_image_gl_ar300m "$1" "$board" "$magic" && return 0
+ return 1
+ ;;
alfa-ap96|\
alfa-nx|\
ap121-mini|\
@@ -566,10 +599,20 @@ platform_check_image() {
return 1
}
+platform_pre_upgrade_gl_ar300m() {
+ if gl_ar300m_is_nand; then
+ nand_do_upgrade "$1"
+ fi
+}
+
+
platform_pre_upgrade() {
local board=$(ar71xx_board_name)
case "$board" in
+ gl-ar300m)
+ platform_pre_upgrade_gl_ar300m "$1"
+ ;;
c-60|\
nbg6716|\
r6100|\
diff --git a/target/linux/ar71xx/config-4.4 b/target/linux/ar71xx/config-4.4
index 4b2f736..f727ca5 100644
--- a/target/linux/ar71xx/config-4.4
+++ b/target/linux/ar71xx/config-4.4
@@ -187,9 +187,9 @@ CONFIG_ATH79_MACH_TL_WR810N=y
CONFIG_ATH79_MACH_TL_WR841N_V1=y
CONFIG_ATH79_MACH_TL_WR841N_V8=y
CONFIG_ATH79_MACH_TL_WR841N_V9=y
+CONFIG_ATH79_MACH_TL_WR940N_V4=y
CONFIG_ATH79_MACH_TL_WR941ND=y
CONFIG_ATH79_MACH_TL_WR941ND_V6=y
-CONFIG_ATH79_MACH_TL_WR940N_V4=y
CONFIG_ATH79_MACH_TUBE2H=y
CONFIG_ATH79_MACH_UBNT=y
CONFIG_ATH79_MACH_UBNT_UNIFIAC=y
@@ -359,9 +359,14 @@ CONFIG_MTD_M25P80=y
# CONFIG_MTD_MAP_BANK_WIDTH_1 is not set
# CONFIG_MTD_MAP_BANK_WIDTH_4 is not set
CONFIG_MTD_MYLOADER_PARTS=y
+CONFIG_MTD_NAND=y
+CONFIG_MTD_NAND_ECC=y
+CONFIG_MTD_NAND_ECC_SMC=y
CONFIG_MTD_PHYSMAP=y
CONFIG_MTD_REDBOOT_DIRECTORY_BLOCK=-2
CONFIG_MTD_REDBOOT_PARTS=y
+CONFIG_MTD_NAND_ATH79=y
+CONFIG_MTD_SPINAND_ONDIEECC=y
CONFIG_MTD_SPI_NOR=y
CONFIG_MTD_SPLIT_FIRMWARE=y
CONFIG_MTD_SPLIT_LZMA_FW=y
diff --git a/target/linux/ar71xx/files/drivers/mtd/nand/ath79_spinand.c b/target/linux/ar71xx/files/drivers/mtd/nand/ath79_spinand.c
new file mode 100644
index 0000000..e621b0f
--- /dev/null
+++ b/target/linux/ar71xx/files/drivers/mtd/nand/ath79_spinand.c
@@ -0,0 +1,1173 @@
+/*
+ * Copyright (c) 2015 The Linux Foundation. All rights reserved.
+ *
+ * Copyright (c) 2003-2013 Broadcom Corporation
+ *
+ * Copyright (c) 2009-2010 Micron Technology, Inc.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#include <linux/module.h>
+#include <linux/delay.h>
+#include <linux/mtd/mtd.h>
+#include <linux/mtd/partitions.h>
+#include <linux/mtd/nand.h>
+#include <linux/spi/spi.h>
+#include <linux/wait.h>
+#include <linux/spinlock.h>
+#include <linux/mtd/mtd.h>
+#include <linux/sizes.h>
+
+/* cmd */
+#define ATH79_SPINAND_CMD_READ 0x13
+#define ATH79_SPINAND_CMD_READ_RDM 0x03
+#define ATH79_SPINAND_CMD_PROG_PAGE_LOAD 0x02
+#define ATH79_SPINAND_CMD_PROG_PAGE 0x84
+#define ATH79_SPINAND_CMD_PROG_PAGE_EXC 0x10
+#define ATH79_SPINAND_CMD_ERASE_BLK 0xd8
+#define ATH79_SPINAND_CMD_WR_ENABLE 0x06
+#define ATH79_SPINAND_CMD_WR_DISABLE 0x04
+#define ATH79_SPINAND_CMD_READ_ID 0x9f
+#define ATH79_SPINAND_CMD_RESET 0xff
+#define ATH79_SPINAND_CMD_READ_REG 0x0f
+#define ATH79_SPINAND_CMD_WRITE_REG 0x1f
+
+/* feature/ status reg */
+#define ATH79_SPINAND_REG_BLOCK_LOCK 0xa0
+#define ATH79_SPINAND_REG_OTP 0xb0
+#define ATH79_SPINAND_REG_STATUS 0xc0
+
+/* status */
+#define ATH79_SPINAND_STATUS_OIP_MASK 0x01
+#define ATH79_SPINAND_STATUS_READY 0
+#define ATH79_SPINAND_STATUS_BUSY BIT(1)
+
+#define ATH79_SPINAND_STATUS_E_FAIL_MASK 0x04
+#define ATH79_SPINAND_STATUS_E_FAIL BIT(2)
+
+#define ATH79_SPINAND_STATUS_P_FAIL_MASK 0x08
+#define ATH79_SPINAND_STATUS_P_FAIL BIT(3)
+
+/* ECC/OTP enable defines */
+#define ATH79_SPINAND_REG_ECC_MASK 0x10
+#define ATH79_SPINAND_REG_ECC_OFF 0
+#define ATH79_SPINAND_REG_ECC_ON BIT(4)
+
+#define ATH79_SPINAND_REG_OTP_EN BIT(6)
+#define ATH79_SPINAND_REG_OTP_PRT BIT(7)
+
+/* block lock */
+#define ATH79_SPINAND_BL_ALL_UNLOCKED 0
+
+#define ATH79_SPINAND_PAGE_TO_BLOCK(p) ((p) >> 6)
+#define ATH79_SPINAND_OFS_TO_PAGE(ofs) (((ofs) >> 17) << 6)
+#define ATH79_SPINAND_BUF_SIZE (2048 * 64)
+
+#define ATH79_SPINAND_BITS_PER_WORD(len) ((len) & 0x02 ? 16 : 32)
+
+struct ath79_spinand_priv {
+ u8 mfr;
+ u8 ecc_error;
+ int ecc_size;
+ int ecc_bytes;
+ int ecc_strength;
+ struct nand_ecclayout *ecc_layout;
+ struct nand_bbt_descr *badblock_pattern;
+ u8 (*ecc_status)(u8 status);
+ void (*read_rdm_addr)(u32 offset, u8 *addr);
+ int (*program_load)(struct spi_device *spi,
+ u32 offset, u32 len, u8 *wbuf);
+ int (*erase_block)(struct spi_device *spi, u32 page);
+ int (*page_read_to_cache)(struct spi_device *spi, u32 page);
+ int (*program_execute)(struct spi_device *spi, u32 page);
+};
+
+struct ath79_spinand_state {
+ uint32_t col;
+ uint32_t row;
+ int buf_ptr;
+ u8 *buf;
+};
+
+struct ath79_spinand_info {
+ struct spi_device *spi;
+ struct ath79_spinand_state *state;
+ void *priv;
+};
+
+struct ath79_spinand_command {
+ u8 cmd;
+ u32 n_addr; /* Number of address */
+ u8 addr[3]; /* Reg Offset */
+ u32 n_dummy; /* Dummy use */
+ u32 n_tx; /* Number of tx bytes */
+ u8 *tx_buf; /* Tx buf */
+ u32 n_rx; /* Number of rx bytes */
+ u8 *rx_buf; /* Rx buf */
+};
+
+static u8 badblock_pattern[] = { 0xff, };
+
+static struct nand_bbt_descr ath79_badblock_pattern_default = {
+ .options = 0,
+ .offs = 0,
+ .len = 1,
+ .pattern = badblock_pattern,
+};
+
+static struct nand_ecclayout ath79_spinand_oob_128_gd = {
+ .eccbytes = 64,
+ .eccpos = {
+ 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},
+ .oobfree = {
+ {.offset = 16, .length = 48},
+ }
+};
+
+/* ECC parity code stored in the additional hidden spare area */
+static struct nand_ecclayout ath79_spinand_oob_64_mx = {
+ .eccbytes = 0,
+ .eccpos = {},
+ .oobfree = {
+ {.offset = 4, .length = 4},
+ {.offset = 20, .length = 4},
+ {.offset = 36, .length = 4},
+ {.offset = 52, .length = 4},
+ }
+};
+
+static struct nand_ecclayout ath79_spinand_oob_64_win = {
+ .eccbytes = 40,
+ .eccpos = {
+ 8, 9, 10, 11, 12, 13, 14, 15,
+ 24, 25, 26, 27, 28, 29, 30, 31,
+ 40, 41, 42, 43, 44, 45, 46, 47,
+ 56, 57, 58, 59, 60, 61, 62, 63,
+ 72, 73, 74, 75, 76, 77, 78, 79},
+ .oobfree = {
+ {.offset = 4, .length = 4},
+ {.offset = 20, .length = 4},
+ {.offset = 36, .length = 4},
+ {.offset = 52, .length = 4},
+ }
+};
+
+static inline struct ath79_spinand_state *mtd_to_state(struct mtd_info *mtd)
+{
+ struct nand_chip *chip = (struct nand_chip *)mtd->priv;
+ struct ath79_spinand_info *info = (struct ath79_spinand_info *)chip->priv;
+
+ return info->state;
+}
+
+static inline struct ath79_spinand_priv *spi_to_priv(struct spi_device *spi)
+{
+ struct mtd_info *mtd = dev_get_drvdata(&spi->dev);
+ struct nand_chip *chip = (struct nand_chip *)mtd->priv;
+ struct ath79_spinand_info *info = (struct ath79_spinand_info *)chip->priv;
+
+ return info->priv;
+}
+
+static inline u8 ath79_spinand_ecc_status(struct spi_device *spi, u8 status)
+{
+ struct ath79_spinand_priv *priv = spi_to_priv(spi);
+
+ return priv->ecc_status(status);
+}
+
+static inline u8 ath79_spinand_ecc_error(struct spi_device *spi)
+{
+ struct ath79_spinand_priv *priv = spi_to_priv(spi);
+
+ return priv->ecc_error;
+}
+
+static inline void ath79_spinand_read_rdm_addr(struct spi_device *spi, u32 offset, u8 *addr)
+{
+ struct ath79_spinand_priv *priv = spi_to_priv(spi);
+
+ priv->read_rdm_addr(offset, addr);
+}
+
+static inline int ath79_spinand_program_load(struct spi_device *spi, u32 offset,
+ u32 len, u8 *wbuf)
+{
+ struct ath79_spinand_priv *priv = spi_to_priv(spi);
+
+ return priv->program_load(spi, offset, len, wbuf);
+}
+
+static inline int ath79_spinand_erase_block_erase(struct spi_device *spi, u32 page)
+{
+ struct ath79_spinand_priv *priv = spi_to_priv(spi);
+
+ return priv->erase_block(spi, page);
+}
+
+static inline int ath79_spinand_read_page_to_cache(struct spi_device *spi, u32 page)
+{
+ struct ath79_spinand_priv *priv = spi_to_priv(spi);
+
+ return priv->page_read_to_cache(spi, page);
+}
+
+static inline int ath79_spinand_program_execute(struct spi_device *spi, u32 page)
+{
+ struct ath79_spinand_priv *priv = spi_to_priv(spi);
+
+ return priv->program_execute(spi, page);
+}
+
+static int ath79_spinand_cmd(struct spi_device *spi, struct ath79_spinand_command *cmd)
+{
+ struct spi_message message;
+ struct spi_transfer x[4];
+ u8 dummy = 0xff;
+
+ spi_message_init(&message);
+ memset(x, 0, sizeof(x));
+
+ x[0].len = 1;
+ x[0].tx_buf = &cmd->cmd;
+ spi_message_add_tail(&x[0], &message);
+
+ if (cmd->n_addr) {
+ x[1].len = cmd->n_addr;
+ x[1].tx_buf = cmd->addr;
+ spi_message_add_tail(&x[1], &message);
+ }
+
+ if (cmd->n_dummy) {
+ x[2].len = cmd->n_dummy;
+ x[2].tx_buf = &dummy;
+ spi_message_add_tail(&x[2], &message);
+ }
+
+ if (cmd->n_tx) {
+ x[3].len = cmd->n_tx;
+ x[3].tx_buf = cmd->tx_buf;
+ if (!(cmd->n_tx & 1))
+ x[3].bits_per_word = ATH79_SPINAND_BITS_PER_WORD(cmd->n_tx);
+ spi_message_add_tail(&x[3], &message);
+ } else if (cmd->n_rx) {
+ x[3].len = cmd->n_rx;
+ x[3].rx_buf = cmd->rx_buf;
+ if (!(cmd->n_rx & 1))
+ x[3].bits_per_word = ATH79_SPINAND_BITS_PER_WORD(cmd->n_rx);
+ spi_message_add_tail(&x[3], &message);
+ }
+
+ return spi_sync(spi, &message);
+}
+
+static int ath79_spinand_read_id(struct spi_device *spi_nand, u8 *id)
+{
+ int retval;
+ u8 nand_id[3];
+ struct ath79_spinand_command cmd = {0};
+
+ cmd.cmd = ATH79_SPINAND_CMD_READ_ID;
+ cmd.n_rx = 3;
+ cmd.rx_buf = nand_id;
+
+ retval = ath79_spinand_cmd(spi_nand, &cmd);
+ if (retval < 0) {
+ dev_err(&spi_nand->dev, "error %d reading id\n", retval);
+ return retval;
+ }
+
+ if (nand_id[0] == NAND_MFR_GIGADEVICE || nand_id[0] == NAND_MFR_HEYANGTEK) {
+ id[0] = nand_id[0];
+ id[1] = nand_id[1];
+ } else { /* Macronix, Micron */
+ id[0] = nand_id[1];
+ id[1] = nand_id[2];
+ }
+
+ return retval;
+}
+
+static int ath79_spinand_read_status(struct spi_device *spi_nand, uint8_t *status)
+{
+ struct ath79_spinand_command cmd = {0};
+ int ret;
+
+ cmd.cmd = ATH79_SPINAND_CMD_READ_REG;
+ cmd.n_addr = 1;
+ cmd.addr[0] = ATH79_SPINAND_REG_STATUS;
+ cmd.n_rx = 1;
+ cmd.rx_buf = status;
+
+ ret = ath79_spinand_cmd(spi_nand, &cmd);
+ if (ret < 0)
+ dev_err(&spi_nand->dev, "err: %d read status register\n", ret);
+
+ return ret;
+}
+
+#define ATH79_SPINAND_MAX_WAIT_JIFFIES (40 * HZ)
+static int __ath79_wait_till_ready(struct spi_device *spi_nand, u8 *status)
+{
+ unsigned long deadline;
+
+ deadline = jiffies + ATH79_SPINAND_MAX_WAIT_JIFFIES;
+ do {
+ if (ath79_spinand_read_status(spi_nand, status))
+ return -1;
+ else if ((*status & ATH79_SPINAND_STATUS_OIP_MASK) == ATH79_SPINAND_STATUS_READY)
+ break;
+
+ cond_resched();
+ } while (!time_after_eq(jiffies, deadline));
+
+ return 0;
+}
+
+static int ath79_wait_till_ready(struct spi_device *spi_nand)
+{
+ u8 stat = 0;
+
+ if (__ath79_wait_till_ready(spi_nand, &stat))
+ return -1;
+
+ if ((stat & ATH79_SPINAND_STATUS_OIP_MASK) == ATH79_SPINAND_STATUS_READY)
+ return 0;
+
+ return -1;
+}
+
+static int ath79_spinand_get_otp(struct spi_device *spi_nand, u8 *otp)
+{
+ struct ath79_spinand_command cmd = {0};
+ int retval;
+
+ cmd.cmd = ATH79_SPINAND_CMD_READ_REG;
+ cmd.n_addr = 1;
+ cmd.addr[0] = ATH79_SPINAND_REG_OTP;
+ cmd.n_rx = 1;
+ cmd.rx_buf = otp;
+
+ retval = ath79_spinand_cmd(spi_nand, &cmd);
+ if (retval < 0)
+ dev_err(&spi_nand->dev, "error %d get otp\n", retval);
+
+ return retval;
+}
+
+static int ath79_spinand_set_otp(struct spi_device *spi_nand, u8 *otp)
+{
+ int retval;
+ struct ath79_spinand_command cmd = {0};
+
+ cmd.cmd = ATH79_SPINAND_CMD_WRITE_REG,
+ cmd.n_addr = 1,
+ cmd.addr[0] = ATH79_SPINAND_REG_OTP,
+ cmd.n_tx = 1,
+ cmd.tx_buf = otp,
+
+ retval = ath79_spinand_cmd(spi_nand, &cmd);
+ if (retval < 0)
+ dev_err(&spi_nand->dev, "error %d set otp\n", retval);
+
+ return retval;
+}
+
+static int ath79_spinand_enable_ecc(struct spi_device *spi_nand)
+{
+ u8 otp = 0;
+
+ if (ath79_spinand_get_otp(spi_nand, &otp))
+ return -1;
+
+ if ((otp & ATH79_SPINAND_REG_ECC_MASK) == ATH79_SPINAND_REG_ECC_ON)
+ return 0;
+
+ otp |= ATH79_SPINAND_REG_ECC_ON;
+ if (ath79_spinand_set_otp(spi_nand, &otp))
+ return -1;
+
+ return ath79_spinand_get_otp(spi_nand, &otp);
+}
+
+static int ath79_spinand_disable_ecc(struct spi_device *spi_nand)
+{
+ u8 otp = 0;
+
+ if (ath79_spinand_get_otp(spi_nand, &otp))
+ return -1;
+
+ if ((otp & ATH79_SPINAND_REG_ECC_MASK) == ATH79_SPINAND_REG_ECC_OFF)
+ return 0;
+
+ otp &= ~ATH79_SPINAND_REG_ECC_MASK;
+ if (ath79_spinand_set_otp(spi_nand, &otp))
+ return -1;
+
+ return ath79_spinand_get_otp(spi_nand, &otp);
+}
+
+static int ath79_spinand_write_enable(struct spi_device *spi_nand)
+{
+ struct ath79_spinand_command cmd = {0};
+
+ cmd.cmd = ATH79_SPINAND_CMD_WR_ENABLE;
+
+ return ath79_spinand_cmd(spi_nand, &cmd);
+}
+
+static int ath79_spinand_read_from_cache(struct spi_device *spi_nand,
+ u32 offset, u32 len, u8 *rbuf)
+{
+ struct ath79_spinand_command cmd = {0};
+
+ ath79_spinand_read_rdm_addr(spi_nand, offset, cmd.addr);
+
+ cmd.cmd = ATH79_SPINAND_CMD_READ_RDM;
+ cmd.n_addr = 3;
+ cmd.n_dummy = 0;
+ cmd.n_rx = len;
+ cmd.rx_buf = rbuf;
+
+ return ath79_spinand_cmd(spi_nand, &cmd);
+}
+
+static int ath79_spinand_read_page(struct spi_device *spi_nand, u32 page_id,
+ u32 offset, u32 len, u8 *rbuf)
+{
+ int ret;
+ u8 status = 0;
+
+ ret = ath79_spinand_read_page_to_cache(spi_nand, page_id);
+ if (ret < 0) {
+ dev_err(&spi_nand->dev, "Read page to cache failed!\n");
+ return ret;
+ }
+
+ if (__ath79_wait_till_ready(spi_nand, &status)) {
+ dev_err(&spi_nand->dev, "WAIT timedout!\n");
+ return -EBUSY;
+ }
+
+ if ((status & ATH79_SPINAND_STATUS_OIP_MASK) != ATH79_SPINAND_STATUS_READY)
+ return -EBUSY;
+
+ status = ath79_spinand_ecc_status(spi_nand, status);
+ if (ath79_spinand_ecc_error(spi_nand) == status) {
+ dev_err(&spi_nand->dev,
+ "ecc error, page=%d\n", page_id);
+
+ return -1;
+ }
+
+ ret = ath79_spinand_read_from_cache(spi_nand, offset, len, rbuf);
+ if (ret < 0) {
+ dev_err(&spi_nand->dev, "read from cache failed!!\n");
+ return ret;
+ }
+
+ return ret;
+}
+
+static int ath79_spinand_program_data_to_cache(struct spi_device *spi_nand,
+ u32 offset, u32 len, u8 *wbuf)
+{
+ struct ath79_spinand_command cmd = {0};
+
+ cmd.cmd = ATH79_SPINAND_CMD_PROG_PAGE_LOAD;
+ cmd.n_addr = 2;
+ cmd.addr[0] = (u8)(offset >> 8);
+ cmd.addr[1] = (u8)(offset >> 0);
+ cmd.n_tx = len;
+ cmd.tx_buf = wbuf;
+
+ return ath79_spinand_cmd(spi_nand, &cmd);
+}
+
+static int ath79_spinand_program_page(struct spi_device *spi_nand,
+ u32 page_id, u32 offset, u32 len, u8 *buf, u32 cache_size)
+{
+ int retval;
+ u8 status = 0;
+
+ if (unlikely(offset)) {
+ uint8_t *wbuf;
+ unsigned int i, j;
+
+ wbuf = devm_kzalloc(&spi_nand->dev, cache_size, GFP_KERNEL);
+ if (!wbuf) {
+ dev_err(&spi_nand->dev, "No memory\n");
+ return -ENOMEM;
+ }
+
+ if (ath79_spinand_read_page(spi_nand, page_id, 0, cache_size, wbuf)) {
+ devm_kfree(&spi_nand->dev, wbuf);
+ return -1;
+ }
+
+ for (i = offset, j = 0; i < len; i++, j++)
+ wbuf[i] &= buf[j];
+
+ retval = ath79_spinand_program_load(spi_nand, offset, len, wbuf);
+
+ devm_kfree(&spi_nand->dev, wbuf);
+ } else {
+ retval = ath79_spinand_program_load(spi_nand, offset, len, buf);
+ }
+
+ if (retval < 0)
+ return retval;
+
+ retval = ath79_spinand_program_execute(spi_nand, page_id);
+ if (retval < 0) {
+ dev_err(&spi_nand->dev, "program execute failed\n");
+ return retval;
+ }
+
+ if (__ath79_wait_till_ready(spi_nand, &status)) {
+ dev_err(&spi_nand->dev, "wait timedout!!!\n");
+ return -EBUSY;
+ }
+
+ if ((status & ATH79_SPINAND_STATUS_OIP_MASK) != ATH79_SPINAND_STATUS_READY)
+ return -EBUSY;
+
+ if ((status & ATH79_SPINAND_STATUS_P_FAIL_MASK) == ATH79_SPINAND_STATUS_P_FAIL) {
+ dev_err(&spi_nand->dev,
+ "program error, page %d\n", page_id);
+ return -1;
+ }
+
+ return 0;
+}
+
+static int ath79_spinand_erase_block(struct mtd_info *mtd,
+ struct spi_device *spi_nand, u32 page)
+{
+ int retval;
+ u8 status = 0;
+
+ retval = ath79_spinand_write_enable(spi_nand);
+ if (retval < 0) {
+ dev_err(&spi_nand->dev, "write enable failed!\n");
+ return retval;
+ }
+
+ if (ath79_wait_till_ready(spi_nand)) {
+ dev_err(&spi_nand->dev, "wait timedout!\n");
+ return -EBUSY;
+ }
+
+ retval = ath79_spinand_erase_block_erase(spi_nand, page);
+ if (retval < 0) {
+ dev_err(&spi_nand->dev, "erase block failed!\n");
+ return retval;
+ }
+
+ if (__ath79_wait_till_ready(spi_nand, &status)) {
+ dev_err(&spi_nand->dev, "wait timedout!\n");
+ return -EBUSY;
+ }
+
+ if ((status & ATH79_SPINAND_STATUS_OIP_MASK) != ATH79_SPINAND_STATUS_READY)
+ return -EBUSY;
+
+ if ((status & ATH79_SPINAND_STATUS_E_FAIL_MASK) == ATH79_SPINAND_STATUS_E_FAIL) {
+ dev_err(&spi_nand->dev,
+ "erase error, block %d\n", ATH79_SPINAND_PAGE_TO_BLOCK(page));
+ return -1;
+ }
+
+ return 0;
+}
+
+static int ath79_spinand_write_page_hwecc(struct mtd_info *mtd,
+ struct nand_chip *chip,
+ const uint8_t *buf,
+ int oob_required)
+{
+ chip->write_buf(mtd, buf, chip->ecc.size * chip->ecc.steps);
+
+ return 0;
+}
+
+static int ath79_spinand_read_page_hwecc(struct mtd_info *mtd,
+ struct nand_chip *chip,
+ uint8_t *buf,
+ int oob_required,
+ int page)
+{
+ u8 status;
+ uint8_t *p = buf;
+ struct ath79_spinand_info *info =
+ (struct ath79_spinand_info *)chip->priv;
+ struct spi_device *spi_nand = info->spi;
+
+ chip->read_buf(mtd, p, chip->ecc.size * chip->ecc.steps);
+
+ if (oob_required)
+ chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
+
+ if (__ath79_wait_till_ready(spi_nand, &status)) {
+ dev_err(&spi_nand->dev, "wait timedout!\n");
+ return -EBUSY;
+ }
+
+ if ((status & ATH79_SPINAND_STATUS_OIP_MASK) != ATH79_SPINAND_STATUS_READY)
+ return -EBUSY;
+
+ status = ath79_spinand_ecc_status(spi_nand, status);
+ if (ath79_spinand_ecc_error(spi_nand) == status) {
+ pr_info("%s: Internal ECC error\n", __func__);
+ mtd->ecc_stats.failed++;
+ } else if (status) {
+ pr_debug("%s: Internal ECC error corrected\n", __func__);
+ mtd->ecc_stats.corrected++;
+ }
+
+ return 0;
+}
+
+static void ath79_spinand_select_chip(struct mtd_info *mtd, int dev)
+{
+}
+
+/*
+ * Mark bad block: the 1st page of the bad block set to zero, include
+ * main area and spare area.
+ * NOTE, irreversible and erase is not recommanded
+ */
+static int ath79_spinand_block_markbad(struct mtd_info *mtd, loff_t ofs)
+{
+ u8 status;
+ struct nand_chip *chip = (struct nand_chip *)mtd->priv;
+ struct ath79_spinand_info *info = (struct ath79_spinand_info *)chip->priv;
+ struct spi_device *spi_nand = info->spi;
+ struct ath79_spinand_state *state = info->state;
+ int page_len = mtd->writesize + mtd->oobsize;
+ int retval, page_id = ATH79_SPINAND_OFS_TO_PAGE(ofs);;
+
+ memset(state->buf, 0, page_len);
+
+ retval = ath79_spinand_program_load(spi_nand, 0, page_len, state->buf);
+ if (retval < 0)
+ return retval;
+
+ retval = ath79_spinand_program_execute(spi_nand, page_id);
+ if (retval < 0) {
+ dev_err(&spi_nand->dev, "program execute failed\n");
+ return retval;
+ }
+
+ if (__ath79_wait_till_ready(spi_nand, &status)) {
+ dev_err(&spi_nand->dev, "wait timedout!!!\n");
+ return -EBUSY;
+ }
+
+ if ((status & ATH79_SPINAND_STATUS_OIP_MASK) != ATH79_SPINAND_STATUS_READY)
+ return -EBUSY;
+
+ if ((status & ATH79_SPINAND_STATUS_P_FAIL_MASK) == ATH79_SPINAND_STATUS_P_FAIL) {
+ dev_err(&spi_nand->dev,
+ "program error, page %d\n", page_id);
+ return -1;
+ }
+
+ return 0;
+}
+
+static uint8_t ath79_spinand_read_byte(struct mtd_info *mtd)
+{
+ struct ath79_spinand_state *state = mtd_to_state(mtd);
+
+ return state->buf[state->buf_ptr++];
+}
+
+static int ath79_spinand_wait(struct mtd_info *mtd, struct nand_chip *chip)
+{
+ struct ath79_spinand_info *info = (struct ath79_spinand_info *)chip->priv;
+ unsigned long timeo = jiffies;
+ u8 status;
+
+ if (chip->state == FL_ERASING)
+ timeo += (HZ * 400) / 1000;
+ else
+ timeo += (HZ * 20) / 1000;
+
+ while (time_before(jiffies, timeo)) {
+ if (ath79_spinand_read_status(info->spi, &status))
+ return NAND_STATUS_FAIL;
+
+ if ((status & ATH79_SPINAND_STATUS_OIP_MASK) == ATH79_SPINAND_STATUS_READY) {
+ status = ath79_spinand_ecc_status(info->spi, status);
+ return (ath79_spinand_ecc_error(info->spi) == status) ?
+ NAND_STATUS_FAIL : NAND_STATUS_READY;
+ }
+
+ cond_resched();
+ }
+ return NAND_STATUS_FAIL;
+}
+
+static void ath79_spinand_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
+{
+ struct ath79_spinand_state *state = mtd_to_state(mtd);
+
+ BUG_ON(ATH79_SPINAND_BUF_SIZE - state->buf_ptr < len);
+
+ memcpy(state->buf + state->buf_ptr, buf, len);
+ state->buf_ptr += len;
+}
+
+static void ath79_spinand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
+{
+ struct ath79_spinand_state *state = mtd_to_state(mtd);
+
+ memcpy(buf, state->buf + state->buf_ptr, len);
+ state->buf_ptr += len;
+}
+
+static void ath79_spinand_reset(struct spi_device *spi_nand)
+{
+ struct ath79_spinand_command cmd = {0};
+
+ cmd.cmd = ATH79_SPINAND_CMD_RESET;
+
+ if (ath79_spinand_cmd(spi_nand, &cmd) < 0)
+ pr_info("ath79_spinand reset failed!\n");
+
+ /* elapse 1ms before issuing any other command */
+ udelay(1000);
+
+ if (ath79_wait_till_ready(spi_nand))
+ dev_err(&spi_nand->dev, "wait timedout!\n");
+}
+
+static void ath79_spinand_cmdfunc(struct mtd_info *mtd, unsigned int command,
+ int column, int page)
+{
+ struct nand_chip *chip = (struct nand_chip *)mtd->priv;
+ struct ath79_spinand_info *info = (struct ath79_spinand_info *)chip->priv;
+ struct ath79_spinand_state *state = info->state;
+
+ switch (command) {
+ case NAND_CMD_READ1:
+ case NAND_CMD_READ0:
+ state->buf_ptr = 0;
+ ath79_spinand_read_page(info->spi, page, 0x0,
+ mtd->writesize + mtd->oobsize,
+ state->buf);
+ break;
+ case NAND_CMD_READOOB:
+ state->buf_ptr = 0;
+ ath79_spinand_read_page(info->spi, page, mtd->writesize,
+ mtd->oobsize, state->buf);
+ break;
+ case NAND_CMD_RNDOUT:
+ state->buf_ptr = column;
+ break;
+ case NAND_CMD_READID:
+ state->buf_ptr = 0;
+ ath79_spinand_read_id(info->spi, (u8 *)state->buf);
+ break;
+ case NAND_CMD_PARAM:
+ state->buf_ptr = 0;
+ break;
+ /* ERASE1 stores the block and page address */
+ case NAND_CMD_ERASE1:
+ ath79_spinand_erase_block(mtd, info->spi, page);
+ break;
+ /* ERASE2 uses the block and page address from ERASE1 */
+ case NAND_CMD_ERASE2:
+ break;
+ /* SEQIN sets up the addr buffer and all registers except the length */
+ case NAND_CMD_SEQIN:
+ state->col = column;
+ state->row = page;
+ state->buf_ptr = 0;
+ break;
+ /* PAGEPROG reuses all of the setup from SEQIN and adds the length */
+ case NAND_CMD_PAGEPROG:
+ ath79_spinand_program_page(info->spi, state->row, state->col,
+ state->buf_ptr, state->buf,
+ mtd->writesize + mtd->oobsize);
+ break;
+ case NAND_CMD_STATUS:
+ ath79_spinand_get_otp(info->spi, state->buf);
+ if (!(state->buf[0] & ATH79_SPINAND_REG_OTP_PRT))
+ state->buf[0] = ATH79_SPINAND_REG_OTP_PRT;
+ state->buf_ptr = 0;
+ break;
+ /* RESET command */
+ case NAND_CMD_RESET:
+ if (ath79_wait_till_ready(info->spi))
+ dev_err(&info->spi->dev, "WAIT timedout!!!\n");
+
+ /* a minimum of 250us must elapse before issuing RESET cmd*/
+ udelay(250);
+ ath79_spinand_reset(info->spi);
+ break;
+ default:
+ dev_err(&mtd->dev, "Unknown CMD: 0x%x\n", command);
+ }
+}
+
+static int ath79_spinand_lock_block(struct spi_device *spi_nand, u8 lock)
+{
+ struct ath79_spinand_command cmd = {0};
+ int ret;
+
+ cmd.cmd = ATH79_SPINAND_CMD_WRITE_REG;
+ cmd.n_addr = 1;
+ cmd.addr[0] = ATH79_SPINAND_REG_BLOCK_LOCK;
+ cmd.n_tx = 1;
+ cmd.tx_buf = &lock;
+
+ ret = ath79_spinand_cmd(spi_nand, &cmd);
+ if (ret < 0)
+ dev_err(&spi_nand->dev, "error %d lock block\n", ret);
+
+ return ret;
+}
+
+/*
+ * ECCSR[2:0] ECC Status
+ * -------------------------------------------
+ * 000 no bit errors were detected
+ * 001 bit errors(<3) corrected
+ * 010 bit errors(=4) corrected
+ * 011 bit errors(=5) corrected
+ * 100 bit errors(=6) corrected
+ * 101 bit errors(=7) corrected
+ * 110 bit errors(=8) corrected
+ * 111 uncorrectable
+ */
+static inline u8 ath79_spinand_eccsr_gd(u8 status)
+{
+ return status >> 4 & 0x7;