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