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