1
2
3
4
5
6
7
8
9
10#include <common.h>
11#include <asm/io.h>
12#include <memalign.h>
13#include <nand.h>
14#include <asm/arch/clock.h>
15#include <asm/arch/funcmux.h>
16#include <asm/arch-tegra/clk_rst.h>
17#include <linux/errno.h>
18#include <asm/gpio.h>
19#include <fdtdec.h>
20#include <bouncebuf.h>
21#include "tegra_nand.h"
22
23DECLARE_GLOBAL_DATA_PTR;
24
25#define NAND_CMD_TIMEOUT_MS 10
26
27#define SKIPPED_SPARE_BYTES 4
28
29
30#define TAG_ECC_BYTES 4
31
32
33
34
35
36
37
38
39
40
41
42static struct nand_ecclayout eccoob = {
43 .eccbytes = 36,
44 .eccpos = {
45 4, 5, 6, 7, 8, 9, 10, 11, 12,
46 13, 14, 15, 16, 17, 18, 19, 20, 21,
47 22, 23, 24, 25, 26, 27, 28, 29, 30,
48 31, 32, 33, 34, 35, 36, 37, 38, 39,
49 },
50 .oobavail = 20,
51 .oobfree = {
52 {
53 .offset = 40,
54 .length = 20,
55 },
56 }
57};
58
59enum {
60 ECC_OK,
61 ECC_TAG_ERROR = 1 << 0,
62 ECC_DATA_ERROR = 1 << 1
63};
64
65
66enum {
67 FDT_NAND_MAX_TRP_TREA,
68 FDT_NAND_TWB,
69 FDT_NAND_MAX_TCR_TAR_TRR,
70 FDT_NAND_TWHR,
71 FDT_NAND_MAX_TCS_TCH_TALS_TALH,
72 FDT_NAND_TWH,
73 FDT_NAND_TWP,
74 FDT_NAND_TRH,
75 FDT_NAND_TADL,
76
77 FDT_NAND_TIMING_COUNT
78};
79
80
81struct fdt_nand {
82 struct nand_ctlr *reg;
83 int enabled;
84 struct gpio_desc wp_gpio;
85 s32 width;
86 u32 timing[FDT_NAND_TIMING_COUNT];
87};
88
89struct nand_drv {
90 struct nand_ctlr *reg;
91 struct fdt_nand config;
92};
93
94static struct nand_drv nand_ctrl;
95static struct mtd_info *our_mtd;
96static struct nand_chip nand_chip[CONFIG_SYS_MAX_NAND_DEVICE];
97
98
99
100
101
102
103
104
105
106static int nand_waitfor_cmd_completion(struct nand_ctlr *reg)
107{
108 u32 reg_val;
109 int running;
110 int i;
111
112 for (i = 0; i < NAND_CMD_TIMEOUT_MS * 1000; i++) {
113 if ((readl(®->command) & CMD_GO) ||
114 !(readl(®->status) & STATUS_RBSY0) ||
115 !(readl(®->isr) & ISR_IS_CMD_DONE)) {
116 udelay(1);
117 continue;
118 }
119 reg_val = readl(®->dma_mst_ctrl);
120
121
122
123
124
125
126
127 running = reg_val & (DMA_MST_CTRL_EN_A_ENABLE |
128 DMA_MST_CTRL_EN_B_ENABLE);
129 if (!running || (reg_val & DMA_MST_CTRL_IS_DMA_DONE))
130 return 1;
131 udelay(1);
132 }
133 return 0;
134}
135
136
137
138
139
140
141
142
143
144static uint8_t read_byte(struct mtd_info *mtd)
145{
146 struct nand_chip *chip = mtd_to_nand(mtd);
147 struct nand_drv *info;
148
149 info = (struct nand_drv *)nand_get_controller_data(chip);
150
151 writel(CMD_GO | CMD_PIO | CMD_RX | CMD_CE0 | CMD_A_VALID,
152 &info->reg->command);
153 if (!nand_waitfor_cmd_completion(info->reg))
154 printf("Command timeout\n");
155
156 return (uint8_t)readl(&info->reg->resp);
157}
158
159
160
161
162
163
164
165
166
167
168static void read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
169{
170 int i, s;
171 unsigned int reg;
172 struct nand_chip *chip = mtd_to_nand(mtd);
173 struct nand_drv *info = (struct nand_drv *)nand_get_controller_data(chip);
174
175 for (i = 0; i < len; i += 4) {
176 s = (len - i) > 4 ? 4 : len - i;
177 writel(CMD_PIO | CMD_RX | CMD_A_VALID | CMD_CE0 |
178 ((s - 1) << CMD_TRANS_SIZE_SHIFT) | CMD_GO,
179 &info->reg->command);
180 if (!nand_waitfor_cmd_completion(info->reg))
181 puts("Command timeout during read_buf\n");
182 reg = readl(&info->reg->resp);
183 memcpy(buf + i, ®, s);
184 }
185}
186
187
188
189
190
191
192
193
194
195static int nand_dev_ready(struct mtd_info *mtd)
196{
197 struct nand_chip *chip = mtd_to_nand(mtd);
198 int reg_val;
199 struct nand_drv *info;
200
201 info = (struct nand_drv *)nand_get_controller_data(chip);
202
203 reg_val = readl(&info->reg->status);
204 if (reg_val & STATUS_RBSY0)
205 return 1;
206 else
207 return 0;
208}
209
210
211static void nand_select_chip(struct mtd_info *mtd, int chipnr)
212{
213 switch (chipnr) {
214 case -1:
215 case 0:
216 break;
217
218 default:
219 BUG();
220 }
221}
222
223
224
225
226
227
228static void nand_clear_interrupt_status(struct nand_ctlr *reg)
229{
230 u32 reg_val;
231
232
233 reg_val = readl(®->isr);
234 writel(reg_val, ®->isr);
235}
236
237
238
239
240
241
242
243
244
245static void nand_command(struct mtd_info *mtd, unsigned int command,
246 int column, int page_addr)
247{
248 struct nand_chip *chip = mtd_to_nand(mtd);
249 struct nand_drv *info;
250
251 info = (struct nand_drv *)nand_get_controller_data(chip);
252
253
254
255
256
257
258
259
260
261 if (command == NAND_CMD_READOOB) {
262 assert(mtd->writesize != 0);
263 column += mtd->writesize;
264 command = NAND_CMD_READ0;
265 }
266
267
268 if (column != -1 && (chip->options & NAND_BUSWIDTH_16))
269 column >>= 1;
270
271 nand_clear_interrupt_status(info->reg);
272
273
274 writel(DMA_MST_CTRL_EN_A_DISABLE
275 | DMA_MST_CTRL_EN_B_DISABLE
276 | DMA_MST_CTRL_IS_DMA_DONE,
277 &info->reg->dma_mst_ctrl);
278
279
280
281
282
283 switch (command) {
284 case NAND_CMD_READID:
285 writel(NAND_CMD_READID, &info->reg->cmd_reg1);
286 writel(column & 0xFF, &info->reg->addr_reg1);
287 writel(CMD_GO | CMD_CLE | CMD_ALE | CMD_CE0,
288 &info->reg->command);
289 break;
290 case NAND_CMD_PARAM:
291 writel(NAND_CMD_PARAM, &info->reg->cmd_reg1);
292 writel(column & 0xFF, &info->reg->addr_reg1);
293 writel(CMD_GO | CMD_CLE | CMD_ALE | CMD_CE0,
294 &info->reg->command);
295 break;
296 case NAND_CMD_READ0:
297 writel(NAND_CMD_READ0, &info->reg->cmd_reg1);
298 writel(NAND_CMD_READSTART, &info->reg->cmd_reg2);
299 writel((page_addr << 16) | (column & 0xFFFF),
300 &info->reg->addr_reg1);
301 writel(page_addr >> 16, &info->reg->addr_reg2);
302 return;
303 case NAND_CMD_SEQIN:
304 writel(NAND_CMD_SEQIN, &info->reg->cmd_reg1);
305 writel(NAND_CMD_PAGEPROG, &info->reg->cmd_reg2);
306 writel((page_addr << 16) | (column & 0xFFFF),
307 &info->reg->addr_reg1);
308 writel(page_addr >> 16,
309 &info->reg->addr_reg2);
310 return;
311 case NAND_CMD_PAGEPROG:
312 return;
313 case NAND_CMD_ERASE1:
314 writel(NAND_CMD_ERASE1, &info->reg->cmd_reg1);
315 writel(NAND_CMD_ERASE2, &info->reg->cmd_reg2);
316 writel(page_addr, &info->reg->addr_reg1);
317 writel(CMD_GO | CMD_CLE | CMD_ALE |
318 CMD_SEC_CMD | CMD_CE0 | CMD_ALE_BYTES3,
319 &info->reg->command);
320 break;
321 case NAND_CMD_ERASE2:
322 return;
323 case NAND_CMD_STATUS:
324 writel(NAND_CMD_STATUS, &info->reg->cmd_reg1);
325 writel(CMD_GO | CMD_CLE | CMD_PIO | CMD_RX
326 | ((1 - 0) << CMD_TRANS_SIZE_SHIFT)
327 | CMD_CE0,
328 &info->reg->command);
329 break;
330 case NAND_CMD_RESET:
331 writel(NAND_CMD_RESET, &info->reg->cmd_reg1);
332 writel(CMD_GO | CMD_CLE | CMD_CE0,
333 &info->reg->command);
334 break;
335 case NAND_CMD_RNDOUT:
336 default:
337 printf("%s: Unsupported command %d\n", __func__, command);
338 return;
339 }
340 if (!nand_waitfor_cmd_completion(info->reg))
341 printf("Command 0x%02X timeout\n", command);
342}
343
344
345
346
347
348
349
350
351
352
353static int blank_check(u8 *buf, int len)
354{
355 int i;
356
357 for (i = 0; i < len; i++)
358 if (buf[i] != 0xFF)
359 return 0;
360 return 1;
361}
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378static int check_ecc_error(struct nand_ctlr *reg, u8 *databuf,
379 int a_len, u8 *oobbuf, int b_len)
380{
381 int return_val = ECC_OK;
382 u32 reg_val;
383
384 if (!(readl(®->isr) & ISR_IS_ECC_ERR))
385 return ECC_OK;
386
387
388
389
390
391 reg_val = readl(®->dec_status);
392 if ((reg_val & DEC_STATUS_A_ECC_FAIL) && databuf) {
393 reg_val = readl(®->bch_dec_status_buf);
394
395
396
397
398
399 if ((reg_val & BCH_DEC_STATUS_FAIL_SEC_FLAG_MASK) &&
400 !blank_check(databuf, a_len))
401 return_val |= ECC_DATA_ERROR;
402 }
403
404 if ((reg_val & DEC_STATUS_B_ECC_FAIL) && oobbuf) {
405 reg_val = readl(®->bch_dec_status_buf);
406
407
408
409
410
411 if ((reg_val & BCH_DEC_STATUS_FAIL_TAG_MASK) &&
412 !blank_check(oobbuf, b_len))
413 return_val |= ECC_TAG_ERROR;
414 }
415
416 return return_val;
417}
418
419
420
421
422
423
424static void start_command(struct nand_ctlr *reg)
425{
426 u32 reg_val;
427
428 reg_val = readl(®->command);
429 reg_val |= CMD_GO;
430 writel(reg_val, ®->command);
431}
432
433
434
435
436
437
438static void stop_command(struct nand_ctlr *reg)
439{
440
441 writel(0, ®->command);
442
443
444 writel(DMA_MST_CTRL_GO_DISABLE
445 | DMA_MST_CTRL_IS_DMA_DONE,
446 ®->dma_mst_ctrl);
447}
448
449
450
451
452
453
454
455
456static int set_bus_width_page_size(struct fdt_nand *config,
457 u32 *reg_val)
458{
459 if (config->width == 8)
460 *reg_val = CFG_BUS_WIDTH_8BIT;
461 else if (config->width == 16)
462 *reg_val = CFG_BUS_WIDTH_16BIT;
463 else {
464 debug("%s: Unsupported bus width %d\n", __func__,
465 config->width);
466 return -1;
467 }
468
469 if (our_mtd->writesize == 512)
470 *reg_val |= CFG_PAGE_SIZE_512;
471 else if (our_mtd->writesize == 2048)
472 *reg_val |= CFG_PAGE_SIZE_2048;
473 else if (our_mtd->writesize == 4096)
474 *reg_val |= CFG_PAGE_SIZE_4096;
475 else {
476 debug("%s: Unsupported page size %d\n", __func__,
477 our_mtd->writesize);
478 return -1;
479 }
480
481 return 0;
482}
483
484
485
486
487
488
489
490
491
492
493
494
495
496static int nand_rw_page(struct mtd_info *mtd, struct nand_chip *chip,
497 uint8_t *buf, int page, int with_ecc, int is_writing)
498{
499 u32 reg_val;
500 int tag_size;
501 struct nand_oobfree *free = chip->ecc.layout->oobfree;
502
503 ALLOC_CACHE_ALIGN_BUFFER(u32, tag_buf, 128);
504 char *tag_ptr;
505 struct nand_drv *info;
506 struct fdt_nand *config;
507 unsigned int bbflags;
508 struct bounce_buffer bbstate, bbstate_oob;
509
510 if ((uintptr_t)buf & 0x03) {
511 printf("buf %p has to be 4-byte aligned\n", buf);
512 return -EINVAL;
513 }
514
515 info = (struct nand_drv *)nand_get_controller_data(chip);
516 config = &info->config;
517 if (set_bus_width_page_size(config, ®_val))
518 return -EINVAL;
519
520
521 tag_ptr = (char *)tag_buf;
522
523 stop_command(info->reg);
524
525 if (is_writing)
526 bbflags = GEN_BB_READ;
527 else
528 bbflags = GEN_BB_WRITE;
529
530 bounce_buffer_start(&bbstate, (void *)buf, 1 << chip->page_shift,
531 bbflags);
532 writel((1 << chip->page_shift) - 1, &info->reg->dma_cfg_a);
533 writel(virt_to_phys(bbstate.bounce_buffer), &info->reg->data_block_ptr);
534
535
536 if (with_ecc) {
537 if (is_writing)
538 memcpy(tag_ptr, chip->oob_poi + free->offset,
539 chip->ecc.layout->oobavail + TAG_ECC_BYTES);
540 tag_size = chip->ecc.layout->oobavail + TAG_ECC_BYTES;
541 reg_val |= (CFG_SKIP_SPARE_SEL_4
542 | CFG_SKIP_SPARE_ENABLE
543 | CFG_HW_ECC_CORRECTION_ENABLE
544 | CFG_ECC_EN_TAG_DISABLE
545 | CFG_HW_ECC_SEL_RS
546 | CFG_HW_ECC_ENABLE
547 | CFG_TVAL4
548 | (tag_size - 1));
549
550 if (!is_writing)
551 tag_size += SKIPPED_SPARE_BYTES;
552 bounce_buffer_start(&bbstate_oob, (void *)tag_ptr, tag_size,
553 bbflags);
554 } else {
555 tag_size = mtd->oobsize;
556 reg_val |= (CFG_SKIP_SPARE_DISABLE
557 | CFG_HW_ECC_CORRECTION_DISABLE
558 | CFG_ECC_EN_TAG_DISABLE
559 | CFG_HW_ECC_DISABLE
560 | (tag_size - 1));
561 bounce_buffer_start(&bbstate_oob, (void *)chip->oob_poi,
562 tag_size, bbflags);
563 }
564 writel(reg_val, &info->reg->config);
565 writel(virt_to_phys(bbstate_oob.bounce_buffer), &info->reg->tag_ptr);
566 writel(BCH_CONFIG_BCH_ECC_DISABLE, &info->reg->bch_config);
567 writel(tag_size - 1, &info->reg->dma_cfg_b);
568
569 nand_clear_interrupt_status(info->reg);
570
571 reg_val = CMD_CLE | CMD_ALE
572 | CMD_SEC_CMD
573 | (CMD_ALE_BYTES5 << CMD_ALE_BYTE_SIZE_SHIFT)
574 | CMD_A_VALID
575 | CMD_B_VALID
576 | (CMD_TRANS_SIZE_PAGE << CMD_TRANS_SIZE_SHIFT)
577 | CMD_CE0;
578 if (!is_writing)
579 reg_val |= (CMD_AFT_DAT_DISABLE | CMD_RX);
580 else
581 reg_val |= (CMD_AFT_DAT_ENABLE | CMD_TX);
582 writel(reg_val, &info->reg->command);
583
584
585 reg_val = DMA_MST_CTRL_GO_ENABLE
586 | DMA_MST_CTRL_BURST_8WORDS
587 | DMA_MST_CTRL_EN_A_ENABLE
588 | DMA_MST_CTRL_EN_B_ENABLE;
589
590 if (!is_writing)
591 reg_val |= DMA_MST_CTRL_DIR_READ;
592 else
593 reg_val |= DMA_MST_CTRL_DIR_WRITE;
594
595 writel(reg_val, &info->reg->dma_mst_ctrl);
596
597 start_command(info->reg);
598
599 if (!nand_waitfor_cmd_completion(info->reg)) {
600 if (!is_writing)
601 printf("Read Page 0x%X timeout ", page);
602 else
603 printf("Write Page 0x%X timeout ", page);
604 if (with_ecc)
605 printf("with ECC");
606 else
607 printf("without ECC");
608 printf("\n");
609 return -EIO;
610 }
611
612 bounce_buffer_stop(&bbstate_oob);
613 bounce_buffer_stop(&bbstate);
614
615 if (with_ecc && !is_writing) {
616 memcpy(chip->oob_poi, tag_ptr,
617 SKIPPED_SPARE_BYTES);
618 memcpy(chip->oob_poi + free->offset,
619 tag_ptr + SKIPPED_SPARE_BYTES,
620 chip->ecc.layout->oobavail);
621 reg_val = (u32)check_ecc_error(info->reg, (u8 *)buf,
622 1 << chip->page_shift,
623 (u8 *)(tag_ptr + SKIPPED_SPARE_BYTES),
624 chip->ecc.layout->oobavail);
625 if (reg_val & ECC_TAG_ERROR)
626 printf("Read Page 0x%X tag ECC error\n", page);
627 if (reg_val & ECC_DATA_ERROR)
628 printf("Read Page 0x%X data ECC error\n",
629 page);
630 if (reg_val & (ECC_DATA_ERROR | ECC_TAG_ERROR))
631 return -EIO;
632 }
633 return 0;
634}
635
636
637
638
639
640
641
642
643
644
645
646static int nand_read_page_hwecc(struct mtd_info *mtd,
647 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
648{
649 return nand_rw_page(mtd, chip, buf, page, 1, 0);
650}
651
652
653
654
655
656
657
658
659static int nand_write_page_hwecc(struct mtd_info *mtd,
660 struct nand_chip *chip, const uint8_t *buf, int oob_required,
661 int page)
662{
663 nand_rw_page(mtd, chip, (uint8_t *)buf, page, 1, 1);
664 return 0;
665}
666
667
668
669
670
671
672
673
674
675
676
677
678
679static int nand_read_page_raw(struct mtd_info *mtd,
680 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
681{
682 return nand_rw_page(mtd, chip, buf, page, 0, 0);
683}
684
685
686
687
688
689
690
691
692static int nand_write_page_raw(struct mtd_info *mtd,
693 struct nand_chip *chip, const uint8_t *buf,
694 int oob_required, int page)
695{
696 nand_rw_page(mtd, chip, (uint8_t *)buf, page, 0, 1);
697 return 0;
698}
699
700
701
702
703
704
705
706
707
708
709
710
711
712static int nand_rw_oob(struct mtd_info *mtd, struct nand_chip *chip,
713 int page, int with_ecc, int is_writing)
714{
715 u32 reg_val;
716 int tag_size;
717 struct nand_oobfree *free = chip->ecc.layout->oobfree;
718 struct nand_drv *info;
719 unsigned int bbflags;
720 struct bounce_buffer bbstate_oob;
721
722 if (((int)chip->oob_poi) & 0x03)
723 return -EINVAL;
724 info = (struct nand_drv *)nand_get_controller_data(chip);
725 if (set_bus_width_page_size(&info->config, ®_val))
726 return -EINVAL;
727
728 stop_command(info->reg);
729
730
731 tag_size = mtd->oobsize;
732 if (with_ecc)
733 reg_val |= CFG_ECC_EN_TAG_ENABLE;
734 else
735 reg_val |= (CFG_ECC_EN_TAG_DISABLE);
736
737 reg_val |= ((tag_size - 1) |
738 CFG_SKIP_SPARE_DISABLE |
739 CFG_HW_ECC_CORRECTION_DISABLE |
740 CFG_HW_ECC_DISABLE);
741 writel(reg_val, &info->reg->config);
742
743 if (is_writing && with_ecc)
744 tag_size -= TAG_ECC_BYTES;
745
746 if (is_writing)
747 bbflags = GEN_BB_READ;
748 else
749 bbflags = GEN_BB_WRITE;
750
751 bounce_buffer_start(&bbstate_oob, (void *)chip->oob_poi, tag_size,
752 bbflags);
753 writel(virt_to_phys(bbstate_oob.bounce_buffer), &info->reg->tag_ptr);
754
755 writel(BCH_CONFIG_BCH_ECC_DISABLE, &info->reg->bch_config);
756
757 writel(tag_size - 1, &info->reg->dma_cfg_b);
758
759 nand_clear_interrupt_status(info->reg);
760
761 reg_val = CMD_CLE | CMD_ALE
762 | CMD_SEC_CMD
763 | (CMD_ALE_BYTES5 << CMD_ALE_BYTE_SIZE_SHIFT)
764 | CMD_B_VALID
765 | CMD_CE0;
766 if (!is_writing)
767 reg_val |= (CMD_AFT_DAT_DISABLE | CMD_RX);
768 else
769 reg_val |= (CMD_AFT_DAT_ENABLE | CMD_TX);
770 writel(reg_val, &info->reg->command);
771
772
773 reg_val = DMA_MST_CTRL_GO_ENABLE
774 | DMA_MST_CTRL_BURST_8WORDS
775 | DMA_MST_CTRL_EN_B_ENABLE;
776 if (!is_writing)
777 reg_val |= DMA_MST_CTRL_DIR_READ;
778 else
779 reg_val |= DMA_MST_CTRL_DIR_WRITE;
780
781 writel(reg_val, &info->reg->dma_mst_ctrl);
782
783 start_command(info->reg);
784
785 if (!nand_waitfor_cmd_completion(info->reg)) {
786 if (!is_writing)
787 printf("Read OOB of Page 0x%X timeout\n", page);
788 else
789 printf("Write OOB of Page 0x%X timeout\n", page);
790 return -EIO;
791 }
792
793 bounce_buffer_stop(&bbstate_oob);
794
795 if (with_ecc && !is_writing) {
796 reg_val = (u32)check_ecc_error(info->reg, 0, 0,
797 (u8 *)(chip->oob_poi + free->offset),
798 chip->ecc.layout->oobavail);
799 if (reg_val & ECC_TAG_ERROR)
800 printf("Read OOB of Page 0x%X tag ECC error\n", page);
801 }
802 return 0;
803}
804
805
806
807
808
809
810
811
812static int nand_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
813 int page)
814{
815 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
816 nand_rw_oob(mtd, chip, page, 0, 0);
817 return 0;
818}
819
820
821
822
823
824
825
826
827
828
829
830static int nand_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
831 int page)
832{
833 chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
834
835 return nand_rw_oob(mtd, chip, page, 0, 1);
836}
837
838
839
840
841
842
843
844static void setup_timing(unsigned timing[FDT_NAND_TIMING_COUNT],
845 struct nand_ctlr *reg)
846{
847 u32 reg_val, clk_rate, clk_period, time_val;
848
849 clk_rate = (u32)clock_get_periph_rate(PERIPH_ID_NDFLASH,
850 CLOCK_ID_PERIPH) / 1000000;
851 clk_period = 1000 / clk_rate;
852 reg_val = ((timing[FDT_NAND_MAX_TRP_TREA] / clk_period) <<
853 TIMING_TRP_RESP_CNT_SHIFT) & TIMING_TRP_RESP_CNT_MASK;
854 reg_val |= ((timing[FDT_NAND_TWB] / clk_period) <<
855 TIMING_TWB_CNT_SHIFT) & TIMING_TWB_CNT_MASK;
856 time_val = timing[FDT_NAND_MAX_TCR_TAR_TRR] / clk_period;
857 if (time_val > 2)
858 reg_val |= ((time_val - 2) << TIMING_TCR_TAR_TRR_CNT_SHIFT) &
859 TIMING_TCR_TAR_TRR_CNT_MASK;
860 reg_val |= ((timing[FDT_NAND_TWHR] / clk_period) <<
861 TIMING_TWHR_CNT_SHIFT) & TIMING_TWHR_CNT_MASK;
862 time_val = timing[FDT_NAND_MAX_TCS_TCH_TALS_TALH] / clk_period;
863 if (time_val > 1)
864 reg_val |= ((time_val - 1) << TIMING_TCS_CNT_SHIFT) &
865 TIMING_TCS_CNT_MASK;
866 reg_val |= ((timing[FDT_NAND_TWH] / clk_period) <<
867 TIMING_TWH_CNT_SHIFT) & TIMING_TWH_CNT_MASK;
868 reg_val |= ((timing[FDT_NAND_TWP] / clk_period) <<
869 TIMING_TWP_CNT_SHIFT) & TIMING_TWP_CNT_MASK;
870 reg_val |= ((timing[FDT_NAND_TRH] / clk_period) <<
871 TIMING_TRH_CNT_SHIFT) & TIMING_TRH_CNT_MASK;
872 reg_val |= ((timing[FDT_NAND_MAX_TRP_TREA] / clk_period) <<
873 TIMING_TRP_CNT_SHIFT) & TIMING_TRP_CNT_MASK;
874 writel(reg_val, ®->timing);
875
876 reg_val = 0;
877 time_val = timing[FDT_NAND_TADL] / clk_period;
878 if (time_val > 2)
879 reg_val = (time_val - 2) & TIMING2_TADL_CNT_MASK;
880 writel(reg_val, ®->timing2);
881}
882
883
884
885
886
887
888
889
890static int fdt_decode_nand(const void *blob, int node, struct fdt_nand *config)
891{
892 int err;
893
894 config->reg = (struct nand_ctlr *)fdtdec_get_addr(blob, node, "reg");
895 config->enabled = fdtdec_get_is_enabled(blob, node);
896 config->width = fdtdec_get_int(blob, node, "nvidia,nand-width", 8);
897 err = gpio_request_by_name_nodev(offset_to_ofnode(node),
898 "nvidia,wp-gpios", 0, &config->wp_gpio, GPIOD_IS_OUT);
899 if (err)
900 return err;
901 err = fdtdec_get_int_array(blob, node, "nvidia,timing",
902 config->timing, FDT_NAND_TIMING_COUNT);
903 if (err < 0)
904 return err;
905
906
907 node = fdt_next_node(blob, node, NULL);
908 if (node < 0)
909 return node;
910
911 return 0;
912}
913
914
915
916
917
918
919
920int tegra_nand_init(struct nand_chip *nand, int devnum)
921{
922 struct nand_drv *info = &nand_ctrl;
923 struct fdt_nand *config = &info->config;
924 int node, ret;
925
926 node = fdtdec_next_compatible(gd->fdt_blob, 0,
927 COMPAT_NVIDIA_TEGRA20_NAND);
928 if (node < 0)
929 return -1;
930 if (fdt_decode_nand(gd->fdt_blob, node, config)) {
931 printf("Could not decode nand-flash in device tree\n");
932 return -1;
933 }
934 if (!config->enabled)
935 return -1;
936 info->reg = config->reg;
937 nand->ecc.mode = NAND_ECC_HW;
938 nand->ecc.layout = &eccoob;
939
940 nand->options = LP_OPTIONS;
941 nand->cmdfunc = nand_command;
942 nand->read_byte = read_byte;
943 nand->read_buf = read_buf;
944 nand->ecc.read_page = nand_read_page_hwecc;
945 nand->ecc.write_page = nand_write_page_hwecc;
946 nand->ecc.read_page_raw = nand_read_page_raw;
947 nand->ecc.write_page_raw = nand_write_page_raw;
948 nand->ecc.read_oob = nand_read_oob;
949 nand->ecc.write_oob = nand_write_oob;
950 nand->ecc.strength = 1;
951 nand->select_chip = nand_select_chip;
952 nand->dev_ready = nand_dev_ready;
953 nand_set_controller_data(nand, &nand_ctrl);
954
955
956 nand->options |= NAND_NO_SUBPAGE_WRITE;
957
958
959 clock_start_periph_pll(PERIPH_ID_NDFLASH, CLOCK_ID_PERIPH, 52000000);
960
961
962 setup_timing(config->timing, info->reg);
963
964 dm_gpio_set_value(&config->wp_gpio, 1);
965
966 our_mtd = nand_to_mtd(nand);
967 ret = nand_scan_ident(our_mtd, CONFIG_SYS_NAND_MAX_CHIPS, NULL);
968 if (ret)
969 return ret;
970
971 nand->ecc.size = our_mtd->writesize;
972 nand->ecc.bytes = our_mtd->oobsize;
973
974 ret = nand_scan_tail(our_mtd);
975 if (ret)
976 return ret;
977
978 ret = nand_register(devnum, our_mtd);
979 if (ret)
980 return ret;
981
982 return 0;
983}
984
985void board_nand_init(void)
986{
987 struct nand_chip *nand = &nand_chip[0];
988
989 if (tegra_nand_init(nand, 0))
990 puts("Tegra NAND init failed\n");
991}
992