1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <common.h>
23#include <nand.h>
24#include <linux/errno.h>
25#include <asm/io.h>
26#include <nand.h>
27#include <asm/arch/clk.h>
28#include <asm/arch/sys_proto.h>
29
30
31
32
33struct lpc32xx_nand_mlc_registers {
34 u8 buff[32768];
35 u8 data[32768];
36 u32 cmd;
37 u32 addr;
38 u32 ecc_enc_reg;
39 u32 ecc_dec_reg;
40 u32 ecc_auto_enc_reg;
41 u32 ecc_auto_dec_reg;
42 u32 rpr;
43 u32 wpr;
44 u32 rubp;
45 u32 robp;
46 u32 sw_wp_add_low;
47 u32 sw_wp_add_hig;
48 u32 icr;
49 u32 time_reg;
50 u32 irq_mr;
51 u32 irq_sr;
52 u32 lock_pr;
53 u32 isr;
54 u32 ceh;
55};
56
57
58#define LOCK_PR_UNLOCK_KEY 0x0000A25E
59
60
61#define ICR_LARGE_BLOCKS 0x00000004
62#define ICR_ADDR4 0x00000002
63
64
65#define CEH_NORMAL_CE 0x00000001
66
67
68#define ISR_NAND_READY 0x00000001
69#define ISR_CONTROLLER_READY 0x00000002
70#define ISR_ECC_READY 0x00000004
71#define ISR_DECODER_ERRORS(s) ((((s) >> 4) & 3)+1)
72#define ISR_DECODER_FAILURE 0x00000040
73#define ISR_DECODER_ERROR 0x00000008
74
75
76#define LPC32X_NAND_TIMEOUT 5000
77
78
79
80
81
82static struct lpc32xx_nand_mlc_registers __iomem *lpc32xx_nand_mlc_registers
83 = (struct lpc32xx_nand_mlc_registers __iomem *)MLC_NAND_BASE;
84
85#if !defined(CONFIG_SYS_MAX_NAND_CHIPS)
86#define CONFIG_SYS_MAX_NAND_CHIPS 1
87#endif
88
89#define clkdiv(v, w, o) (((1+(clk/v)) & w) << o)
90
91
92
93
94
95
96
97
98
99
100
101
102
103struct lpc32xx_oob {
104 struct {
105 uint8_t free_oob_bytes[6];
106 } free[4];
107 struct {
108 uint8_t ecc_oob_bytes[10];
109 } ecc[4];
110};
111
112
113
114
115
116static void lpc32xx_nand_init(void)
117{
118 unsigned int clk;
119
120
121
122
123
124 writel(LOCK_PR_UNLOCK_KEY,
125 &lpc32xx_nand_mlc_registers->lock_pr);
126
127
128 writel(ICR_LARGE_BLOCKS | ICR_ADDR4,
129 &lpc32xx_nand_mlc_registers->icr);
130
131
132 writel(0, &lpc32xx_nand_mlc_registers->irq_mr);
133
134
135 writel(CEH_NORMAL_CE,
136 &lpc32xx_nand_mlc_registers->ceh);
137
138
139 clk = get_hclk_clk_rate();
140
141 writel(
142 clkdiv(CONFIG_LPC32XX_NAND_MLC_TCEA_DELAY, 0x03, 24) |
143 clkdiv(CONFIG_LPC32XX_NAND_MLC_BUSY_DELAY, 0x1F, 19) |
144 clkdiv(CONFIG_LPC32XX_NAND_MLC_NAND_TA, 0x07, 16) |
145 clkdiv(CONFIG_LPC32XX_NAND_MLC_RD_HIGH, 0x0F, 12) |
146 clkdiv(CONFIG_LPC32XX_NAND_MLC_RD_LOW, 0x0F, 8) |
147 clkdiv(CONFIG_LPC32XX_NAND_MLC_WR_HIGH, 0x0F, 4) |
148 clkdiv(CONFIG_LPC32XX_NAND_MLC_WR_LOW, 0x0F, 0),
149 &lpc32xx_nand_mlc_registers->time_reg);
150}
151
152#if !defined(CONFIG_SPL_BUILD)
153
154
155
156
157
158static void lpc32xx_cmd_ctrl(struct mtd_info *mtd, int cmd,
159 unsigned int ctrl)
160{
161 if (cmd == NAND_CMD_NONE)
162 return;
163
164 if (ctrl & NAND_CLE)
165 writeb(cmd & 0Xff, &lpc32xx_nand_mlc_registers->cmd);
166 else if (ctrl & NAND_ALE)
167 writeb(cmd & 0Xff, &lpc32xx_nand_mlc_registers->addr);
168}
169
170
171
172
173
174
175static uint8_t lpc32xx_read_byte(struct mtd_info *mtd)
176{
177 return readb(&lpc32xx_nand_mlc_registers->data);
178}
179
180
181
182
183
184
185
186static int lpc32xx_dev_ready(struct mtd_info *mtd)
187{
188
189 int status = readl(&lpc32xx_nand_mlc_registers->isr);
190 return status & ISR_CONTROLLER_READY;
191}
192
193
194
195
196
197
198
199
200static struct nand_ecclayout lpc32xx_largepage_ecclayout = {
201 .eccbytes = 40,
202 .eccpos = {24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
203 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
204 44, 45, 46, 47, 48, 48, 50, 51, 52, 53,
205 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
206 },
207 .oobfree = {
208
209 {
210 .offset = 2,
211 .length = 22
212 },
213 }
214};
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233static int lpc32xx_read_page_hwecc(struct mtd_info *mtd,
234 struct nand_chip *chip, uint8_t *buf, int oob_required,
235 int page)
236{
237 unsigned int i, status, timeout, err, max_bitflips = 0;
238 struct lpc32xx_oob *oob = (struct lpc32xx_oob *)chip->oob_poi;
239
240
241 for (i = 0; i < 4; i++) {
242
243 writel(0, &lpc32xx_nand_mlc_registers->ecc_auto_dec_reg);
244
245 for (timeout = LPC32X_NAND_TIMEOUT; timeout; timeout--) {
246 status = readl(&lpc32xx_nand_mlc_registers->isr);
247 if (status & ISR_CONTROLLER_READY)
248 break;
249 udelay(1);
250 }
251
252 if (status & ISR_DECODER_FAILURE)
253 return -1;
254
255 if (status & ISR_DECODER_ERROR) {
256 err = ISR_DECODER_ERRORS(status);
257 if (err > max_bitflips)
258 max_bitflips = err;
259 }
260
261 memcpy(buf+512*i, lpc32xx_nand_mlc_registers->buff, 512);
262
263 memcpy(&oob->free[i], lpc32xx_nand_mlc_registers->buff, 6);
264
265 memcpy(&oob->ecc[i], lpc32xx_nand_mlc_registers->buff, 10);
266 }
267 return max_bitflips;
268}
269
270
271
272
273
274
275
276
277
278
279
280
281static int lpc32xx_read_page_raw(struct mtd_info *mtd,
282 struct nand_chip *chip, uint8_t *buf, int oob_required,
283 int page)
284{
285 unsigned int i, status, timeout;
286 struct lpc32xx_oob *oob = (struct lpc32xx_oob *)chip->oob_poi;
287
288
289
290
291 for (i = 0; i < 4; i++) {
292
293 for (timeout = LPC32X_NAND_TIMEOUT; timeout; timeout--) {
294 status = readl(&lpc32xx_nand_mlc_registers->isr);
295 if (status & ISR_NAND_READY)
296 break;
297 udelay(1);
298 }
299
300 if (!(status & ISR_NAND_READY))
301 return -1;
302
303 memcpy(buf+512*i, lpc32xx_nand_mlc_registers->data, 512);
304
305 memcpy(&oob->free[i], lpc32xx_nand_mlc_registers->data, 6);
306
307 memcpy(&oob->ecc[i], lpc32xx_nand_mlc_registers->data, 10);
308 }
309 return 0;
310}
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330static int lpc32xx_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
331 int page)
332{
333 unsigned int i, status, timeout, err, max_bitflips = 0;
334 struct lpc32xx_oob *oob = (struct lpc32xx_oob *)chip->oob_poi;
335
336
337
338 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
339
340
341 for (i = 0; i < 4; i++) {
342
343 writel(0, &lpc32xx_nand_mlc_registers->ecc_auto_dec_reg);
344
345 for (timeout = LPC32X_NAND_TIMEOUT; timeout; timeout--) {
346 status = readl(&lpc32xx_nand_mlc_registers->isr);
347 if (status & ISR_CONTROLLER_READY)
348 break;
349 udelay(1);
350 }
351
352 if (status & ISR_DECODER_FAILURE)
353 max_bitflips = 5;
354
355 if (status & ISR_DECODER_ERROR) {
356 err = ISR_DECODER_ERRORS(status);
357 if (err > max_bitflips)
358 max_bitflips = err;
359 }
360
361 writel(0, &lpc32xx_nand_mlc_registers->robp);
362
363 memcpy(&oob->free[i], lpc32xx_nand_mlc_registers->buff, 6);
364
365 memcpy(&oob->ecc[i], lpc32xx_nand_mlc_registers->buff, 10);
366 }
367 return max_bitflips;
368}
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383static int lpc32xx_write_page_hwecc(struct mtd_info *mtd,
384 struct nand_chip *chip, const uint8_t *buf, int oob_required,
385 int page)
386{
387 unsigned int i, status, timeout;
388 struct lpc32xx_oob *oob = (struct lpc32xx_oob *)chip->oob_poi;
389
390
391 for (i = 0; i < 4; i++) {
392
393 writel(0, &lpc32xx_nand_mlc_registers->ecc_enc_reg);
394
395 memcpy(&lpc32xx_nand_mlc_registers->buff, buf+512*i, 512);
396
397 memcpy(&lpc32xx_nand_mlc_registers->buff, &oob->free[i], 6);
398
399 for (timeout = LPC32X_NAND_TIMEOUT; timeout; timeout--) {
400 status = readl(&lpc32xx_nand_mlc_registers->isr);
401 if (status & ISR_ECC_READY)
402 break;
403 udelay(1);
404 }
405
406 if (!(status & ISR_ECC_READY))
407 return -1;
408
409 writel(0, &lpc32xx_nand_mlc_registers->ecc_auto_enc_reg);
410
411 for (timeout = LPC32X_NAND_TIMEOUT; timeout; timeout--) {
412 status = readl(&lpc32xx_nand_mlc_registers->isr);
413 if (status & ISR_CONTROLLER_READY)
414 break;
415 udelay(1);
416 }
417
418 if (!(status & ISR_CONTROLLER_READY))
419 return -1;
420 }
421 return 0;
422}
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441static int lpc32xx_write_page_raw(struct mtd_info *mtd,
442 struct nand_chip *chip, const uint8_t *buf, int oob_required,
443 int page)
444{
445 unsigned int i;
446 struct lpc32xx_oob *oob = (struct lpc32xx_oob *)chip->oob_poi;
447
448
449 for (i = 0; i < 4; i++) {
450
451 memcpy(lpc32xx_nand_mlc_registers->buff, buf+512*i, 512);
452
453 memcpy(lpc32xx_nand_mlc_registers->buff, &oob->free[i], 6);
454
455 memcpy(lpc32xx_nand_mlc_registers->buff, &oob->ecc[i], 10);
456 }
457 return 0;
458}
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477static int lpc32xx_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
478 int page)
479{
480
481 unsigned int i, status, timeout;
482 struct lpc32xx_oob *oob = (struct lpc32xx_oob *)chip->oob_poi;
483
484 for (i = 0; i < 4; i++) {
485
486 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x200+0x210*i, page);
487
488 memcpy(lpc32xx_nand_mlc_registers->data, &oob->free[i], 6);
489
490 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
491
492 for (timeout = LPC32X_NAND_TIMEOUT; timeout; timeout--) {
493 status = readl(&lpc32xx_nand_mlc_registers->isr);
494 if (status & ISR_NAND_READY)
495 break;
496 udelay(1);
497 }
498
499 if (!(status & ISR_NAND_READY))
500 return -1;
501 }
502 return 0;
503}
504
505
506
507
508
509
510
511
512
513static int lpc32xx_waitfunc(struct mtd_info *mtd, struct nand_chip *chip)
514{
515 int status;
516 unsigned int timeout;
517
518 for (timeout = LPC32X_NAND_TIMEOUT; timeout; timeout--) {
519 status = readl(&lpc32xx_nand_mlc_registers->isr);
520 if ((status & (ISR_CONTROLLER_READY || ISR_NAND_READY))
521 == (ISR_CONTROLLER_READY || ISR_NAND_READY))
522 break;
523 udelay(1);
524 }
525
526 if ((status & (ISR_CONTROLLER_READY || ISR_NAND_READY))
527 != (ISR_CONTROLLER_READY || ISR_NAND_READY))
528 return -1;
529
530 writel(NAND_CMD_STATUS, &lpc32xx_nand_mlc_registers->cmd);
531
532 return readb(&lpc32xx_nand_mlc_registers->data);
533}
534
535
536
537
538
539static struct nand_chip lpc32xx_chip;
540
541
542
543
544
545void board_nand_init(void)
546{
547 struct mtd_info *mtd = nand_to_mtd(&lpc32xx_chip);
548 int ret;
549
550
551
552 lpc32xx_chip.IO_ADDR_R = &lpc32xx_nand_mlc_registers->buff;
553 lpc32xx_chip.IO_ADDR_W = &lpc32xx_nand_mlc_registers->buff;
554 lpc32xx_chip.cmd_ctrl = lpc32xx_cmd_ctrl;
555
556 lpc32xx_chip.dev_ready = lpc32xx_dev_ready;
557
558
559 lpc32xx_chip.options |= NAND_NO_SUBPAGE_WRITE;
560
561
562
563 lpc32xx_chip.ecc.mode = NAND_ECC_HW;
564 lpc32xx_chip.ecc.layout = &lpc32xx_largepage_ecclayout;
565 lpc32xx_chip.ecc.size = 512;
566 lpc32xx_chip.ecc.bytes = 10;
567 lpc32xx_chip.ecc.strength = 4;
568 lpc32xx_chip.ecc.read_page = lpc32xx_read_page_hwecc;
569 lpc32xx_chip.ecc.read_page_raw = lpc32xx_read_page_raw;
570 lpc32xx_chip.ecc.write_page = lpc32xx_write_page_hwecc;
571 lpc32xx_chip.ecc.write_page_raw = lpc32xx_write_page_raw;
572 lpc32xx_chip.ecc.read_oob = lpc32xx_read_oob;
573 lpc32xx_chip.ecc.write_oob = lpc32xx_write_oob;
574 lpc32xx_chip.waitfunc = lpc32xx_waitfunc;
575
576 lpc32xx_chip.read_byte = lpc32xx_read_byte;
577
578
579 lpc32xx_chip.bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_LASTBLOCK
580 | NAND_BBT_SCANLASTPAGE | NAND_BBT_SCAN2NDPAGE
581 | NAND_BBT_WRITE;
582
583
584 lpc32xx_nand_init();
585
586
587 ret = nand_scan_ident(mtd, CONFIG_SYS_MAX_NAND_CHIPS, NULL);
588 if (ret) {
589 pr_err("nand_scan_ident returned %i", ret);
590 return;
591 }
592
593
594 ret = nand_scan_tail(mtd);
595 if (ret) {
596 pr_err("nand_scan_tail returned %i", ret);
597 return;
598 }
599
600
601 ret = nand_register(0, mtd);
602 if (ret)
603 pr_err("nand_register returned %i", ret);
604}
605
606#else
607
608void nand_init(void)
609{
610
611 lpc32xx_mlc_nand_init();
612
613 lpc32xx_nand_init();
614}
615
616void nand_deselect(void)
617{
618
619}
620
621static int read_single_page(uint8_t *dest, int page,
622 struct lpc32xx_oob *oob)
623{
624 int status, i, timeout, err, max_bitflips = 0;
625
626
627 writel(NAND_CMD_READ0, &lpc32xx_nand_mlc_registers->cmd);
628
629 writel(0, &lpc32xx_nand_mlc_registers->addr);
630 writel(0, &lpc32xx_nand_mlc_registers->addr);
631 writel(page & 0xff, &lpc32xx_nand_mlc_registers->addr);
632 writel((page>>8) & 0xff, &lpc32xx_nand_mlc_registers->addr);
633 writel((page>>16) & 0xff, &lpc32xx_nand_mlc_registers->addr);
634
635 writel(NAND_CMD_READSTART, &lpc32xx_nand_mlc_registers->cmd);
636
637
638 for (i = 0; i < 4; i++) {
639
640 writel(0, &lpc32xx_nand_mlc_registers->ecc_auto_dec_reg);
641
642 for (timeout = LPC32X_NAND_TIMEOUT; timeout; timeout--) {
643 status = readl(&lpc32xx_nand_mlc_registers->isr);
644 if (status & ISR_CONTROLLER_READY)
645 break;
646 udelay(1);
647 }
648
649 if (!(status & ISR_CONTROLLER_READY))
650 return -1;
651
652 if (status & ISR_DECODER_FAILURE)
653 return -1;
654
655 if (status & ISR_DECODER_ERROR) {
656 err = ISR_DECODER_ERRORS(status);
657 if (err > max_bitflips)
658 max_bitflips = err;
659 }
660
661 memcpy(dest+i*512, lpc32xx_nand_mlc_registers->buff, 512);
662
663 memcpy(&oob->free[i], lpc32xx_nand_mlc_registers->buff, 6);
664 }
665 return max_bitflips;
666}
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689#define BYTES_PER_PAGE 2048
690#define PAGES_PER_BLOCK 64
691#define BYTES_PER_BLOCK (BYTES_PER_PAGE * PAGES_PER_BLOCK)
692#define PAGES_PER_CHIP_MAX 524288
693
694int nand_spl_load_image(uint32_t offs, unsigned int size, void *dst)
695{
696 int bytes_left = size;
697 int pages_left = DIV_ROUND_UP(size, BYTES_PER_PAGE);
698 int blocks_left = DIV_ROUND_UP(size, BYTES_PER_BLOCK);
699 int block = 0;
700 int page = offs / BYTES_PER_PAGE;
701
702 while (blocks_left) {
703
704 void *block_page_dst = dst;
705
706 int block_bytes_left = bytes_left;
707 if (block_bytes_left > BYTES_PER_BLOCK)
708 block_bytes_left = BYTES_PER_BLOCK;
709
710 int block_pages_good = 0;
711 int block_pages_bad = 0;
712 int block_pages_err = 0;
713
714 int block_pages_left = pages_left;
715 if (block_pages_left > PAGES_PER_BLOCK)
716 block_pages_left = PAGES_PER_BLOCK;
717 int block_pages = block_pages_left;
718 int block_page = page;
719
720 while ((block_pages > 0) && (block_pages_bad == 0)) {
721
722 struct lpc32xx_oob oob;
723
724 int res = read_single_page(block_page_dst, block_page,
725 &oob);
726
727 if (res >= 0) {
728
729 block_pages_good++;
730
731 if ((oob.free[0].free_oob_bytes[0] != 0xff)
732 | (oob.free[0].free_oob_bytes[1] != 0xff))
733 block_pages_bad++;
734 } else
735
736 block_pages_err++;
737
738 block_page++;
739 block_page_dst += BYTES_PER_PAGE;
740 if (block_pages)
741 block_pages--;
742 }
743
744 if (block_pages_good == 0)
745 block_pages_bad = block_pages_err;
746
747 if ((block_pages_err > 0) && (block_pages_bad == 0))
748 return -1;
749
750 if (block_pages_bad == 0) {
751 dst += block_bytes_left;
752 bytes_left -= block_bytes_left;
753 pages_left -= block_pages_left;
754 blocks_left--;
755 }
756
757 block++;
758 page += PAGES_PER_BLOCK;
759 }
760
761
762 return 0;
763}
764
765#endif
766