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 <asm/io.h>
25
26#if defined(CONFIG_CMD_NAND)
27
28#include <nand.h>
29#include <asm/arch/pxa-regs.h>
30
31#ifdef CONFIG_SYS_DFC_DEBUG1
32# define DFC_DEBUG1(fmt, args...) printf(fmt, ##args)
33#else
34# define DFC_DEBUG1(fmt, args...)
35#endif
36
37#ifdef CONFIG_SYS_DFC_DEBUG2
38# define DFC_DEBUG2(fmt, args...) printf(fmt, ##args)
39#else
40# define DFC_DEBUG2(fmt, args...)
41#endif
42
43#ifdef CONFIG_SYS_DFC_DEBUG3
44# define DFC_DEBUG3(fmt, args...) printf(fmt, ##args)
45#else
46# define DFC_DEBUG3(fmt, args...)
47#endif
48
49
50static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
51
52static struct nand_bbt_descr delta_bbt_descr = {
53 .options = 0,
54 .offs = 0,
55 .len = 2,
56 .pattern = scan_ff_pattern
57};
58
59static struct nand_ecclayout delta_oob = {
60 .eccbytes = 6,
61 .eccpos = {2, 3, 4, 5, 6, 7},
62 .oobfree = { {8, 2}, {12, 4} }
63};
64
65
66
67
68static void dfc_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
69{
70 return;
71}
72
73#if 0
74
75static int dfc_device_ready(struct mtd_info *mtdinfo)
76{
77 if(NDSR & NDSR_RDY)
78 return 1;
79 else
80 return 0;
81 return 0;
82}
83#endif
84
85
86
87
88static void dfc_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
89{
90 unsigned long bytes_multi = len & 0xfffffffc;
91 unsigned long rest = len & 0x3;
92 unsigned long *long_buf;
93 int i;
94
95 DFC_DEBUG2("dfc_write_buf: writing %d bytes starting with 0x%x.\n", len, *((unsigned long*) buf));
96 if(bytes_multi) {
97 for(i=0; i<bytes_multi; i+=4) {
98 long_buf = (unsigned long*) &buf[i];
99 writel(*long_buf, NDDB);
100 }
101 }
102 if(rest) {
103 printf("dfc_write_buf: ERROR, writing non 4-byte aligned data.\n");
104 }
105 return;
106}
107
108
109
110
111
112
113
114static void dfc_read_buf(struct mtd_info *mtd, u_char* const buf, int len)
115{
116 int i=0, j;
117
118
119
120 unsigned long bytes_multi = len & 0xfffffffc;
121 unsigned long rest = len & 0x3;
122 unsigned long *long_buf;
123
124 DFC_DEBUG3("dfc_read_buf: reading %d bytes.\n", len);
125
126 if(bytes_multi) {
127 for(i=0; i<bytes_multi; i+=4) {
128 long_buf = (unsigned long*) &buf[i];
129 *long_buf = readl(NDDB);
130 }
131 }
132
133
134 if(rest) {
135 unsigned long rest_data = NDDB;
136 for(j=0;j<rest; j++)
137 buf[i+j] = (u_char) ((rest_data>>j) & 0xff);
138 }
139
140 return;
141}
142
143
144
145
146static u16 dfc_read_word(struct mtd_info *mtd)
147{
148 printf("dfc_read_word: UNIMPLEMENTED.\n");
149 return 0;
150}
151
152
153static unsigned long read_buf = 0;
154static int bytes_read = -1;
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169static u_char dfc_read_byte(struct mtd_info *mtd)
170{
171 unsigned char byte;
172 unsigned long dummy;
173
174 if(bytes_read < 0) {
175 read_buf = readl(NDDB);
176 dummy = readl(NDDB);
177 bytes_read = 0;
178 }
179 byte = (unsigned char) (read_buf>>(8 * bytes_read++));
180 if(bytes_read >= 4)
181 bytes_read = -1;
182
183 DFC_DEBUG2("dfc_read_byte: byte %u: 0x%x of (0x%x).\n", bytes_read - 1, byte, read_buf);
184 return byte;
185}
186
187
188static unsigned long get_delta(unsigned long start)
189{
190 unsigned long cur = readl(OSCR);
191
192 if(cur < start)
193 return (cur + (start^0xffffffff));
194 else
195 return (cur - start);
196}
197
198
199static void wait_us(unsigned long us)
200{
201 unsigned long start = readl(OSCR);
202 us = DIV_ROUND_UP(us * OSCR_CLK_FREQ, 1000);
203
204 while (get_delta(start) < us) {
205
206 }
207}
208
209static void dfc_clear_nddb(void)
210{
211 writel(readl(NDCR) & ~NDCR_ND_RUN, NDCR);
212 wait_us(CONFIG_SYS_NAND_OTHER_TO);
213}
214
215
216static unsigned long dfc_wait_event(unsigned long event)
217{
218 unsigned long ndsr, timeout, start = readl(OSCR);
219
220 if(!event)
221 return 0xff000000;
222 else if(event & (NDSR_CS0_CMDD | NDSR_CS0_BBD))
223 timeout = DIV_ROUND_UP(CONFIG_SYS_NAND_PROG_ERASE_TO
224 * OSCR_CLK_FREQ, 1000);
225 else
226 timeout = DIV_ROUND_UP(CONFIG_SYS_NAND_OTHER_TO
227 * OSCR_CLK_FREQ, 1000);
228
229 while(1) {
230 ndsr = readl(NDSR);
231 if(ndsr & event) {
232 writel(readl(NDSR) | event, NDSR);
233 break;
234 }
235 if(get_delta(start) > timeout) {
236 DFC_DEBUG1("dfc_wait_event: TIMEOUT waiting for event: 0x%lx.\n", event);
237 return 0xff000000;
238 }
239
240 }
241 return ndsr;
242}
243
244
245static void dfc_new_cmd(void)
246{
247 int retry = 0;
248 unsigned long status;
249
250 while(retry++ <= CONFIG_SYS_NAND_SENDCMD_RETRY) {
251
252 writel(0xFFF, NDSR);
253
254
255 if (!(readl(NDCR) & NDCR_ND_RUN))
256 writel(readl(NDCR) | NDCR_ND_RUN, NDCR);
257
258 status = dfc_wait_event(NDSR_WRCMDREQ);
259
260 if(status & NDSR_WRCMDREQ)
261 return;
262
263 DFC_DEBUG2("dfc_new_cmd: FAILED to get WRITECMDREQ, retry: %d.\n", retry);
264 dfc_clear_nddb();
265 }
266 DFC_DEBUG1("dfc_new_cmd: giving up after %d retries.\n", retry);
267}
268
269
270
271static int dfc_wait(struct mtd_info *mtd, struct nand_chip *this)
272{
273 unsigned long ndsr=0, event=0;
274 int state = this->state;
275
276 if(state == FL_WRITING) {
277 event = NDSR_CS0_CMDD | NDSR_CS0_BBD;
278 } else if(state == FL_ERASING) {
279 event = NDSR_CS0_CMDD | NDSR_CS0_BBD;
280 }
281
282 ndsr = dfc_wait_event(event);
283
284 if((ndsr & NDSR_CS0_BBD) || (ndsr & 0xff000000))
285 return(0x1);
286 return 0;
287}
288
289
290static void dfc_cmdfunc(struct mtd_info *mtd, unsigned command,
291 int column, int page_addr)
292{
293
294 unsigned long ndcb0=0, ndcb1=0, ndcb2=0, event=0;
295
296
297 bytes_read = -1;
298 read_buf = 0;
299
300 switch (command) {
301 case NAND_CMD_READ0:
302 DFC_DEBUG3("dfc_cmdfunc: NAND_CMD_READ0, page_addr: 0x%x, column: 0x%x.\n", page_addr, (column>>1));
303 dfc_new_cmd();
304 ndcb0 = (NAND_CMD_READ0 | (4<<16));
305 column >>= 1;
306 ndcb1 = (((column>>1) & 0xff) |
307 ((page_addr<<8) & 0xff00) |
308 ((page_addr<<8) & 0xff0000) |
309 ((page_addr<<8) & 0xff000000));
310 event = NDSR_RDDREQ;
311 goto write_cmd;
312 case NAND_CMD_READ1:
313 DFC_DEBUG2("dfc_cmdfunc: NAND_CMD_READ1 unimplemented!\n");
314 goto end;
315 case NAND_CMD_READOOB:
316 DFC_DEBUG1("dfc_cmdfunc: NAND_CMD_READOOB unimplemented!\n");
317 goto end;
318 case NAND_CMD_READID:
319 dfc_new_cmd();
320 DFC_DEBUG2("dfc_cmdfunc: NAND_CMD_READID.\n");
321 ndcb0 = (NAND_CMD_READID | (3 << 21) | (1 << 16));
322 event = NDSR_RDDREQ;
323 goto write_cmd;
324 case NAND_CMD_PAGEPROG:
325
326 DFC_DEBUG2("dfc_cmdfunc: NAND_CMD_PAGEPROG empty due to multicmd.\n");
327 goto end;
328 case NAND_CMD_ERASE1:
329 DFC_DEBUG2("dfc_cmdfunc: NAND_CMD_ERASE1, page_addr: 0x%x, column: 0x%x.\n", page_addr, (column>>1));
330 dfc_new_cmd();
331 ndcb0 = (0xd060 | (1<<25) | (2<<21) | (1<<19) | (3<<16));
332 ndcb1 = (page_addr & 0x00ffffff);
333 goto write_cmd;
334 case NAND_CMD_ERASE2:
335 DFC_DEBUG2("dfc_cmdfunc: NAND_CMD_ERASE2 empty due to multicmd.\n");
336 goto end;
337 case NAND_CMD_SEQIN:
338
339 dfc_new_cmd();
340 DFC_DEBUG2("dfc_cmdfunc: NAND_CMD_SEQIN/PAGE_PROG, page_addr: 0x%x, column: 0x%x.\n", page_addr, (column>>1));
341 ndcb0 = (0x1080 | (1<<25) | (1<<21) | (1<<19) | (4<<16));
342 column >>= 1;
343 ndcb1 = (((column>>1) & 0xff) |
344 ((page_addr<<8) & 0xff00) |
345 ((page_addr<<8) & 0xff0000) |
346 ((page_addr<<8) & 0xff000000));
347 event = NDSR_WRDREQ;
348 goto write_cmd;
349 case NAND_CMD_STATUS:
350 DFC_DEBUG2("dfc_cmdfunc: NAND_CMD_STATUS.\n");
351 dfc_new_cmd();
352 ndcb0 = NAND_CMD_STATUS | (4<<21);
353 event = NDSR_RDDREQ;
354 goto write_cmd;
355 case NAND_CMD_RESET:
356 DFC_DEBUG2("dfc_cmdfunc: NAND_CMD_RESET.\n");
357 ndcb0 = NAND_CMD_RESET | (5<<21);
358 event = NDSR_CS0_CMDD;
359 goto write_cmd;
360 default:
361 printk("dfc_cmdfunc: error, unsupported command.\n");
362 goto end;
363 }
364
365 write_cmd:
366 writel(ndcb0, NDCB0);
367 writel(ndcb1, NDCB0);
368 writel(ndcb2, NDCB0);
369
370
371 dfc_wait_event(event);
372 end:
373 return;
374}
375
376static void dfc_gpio_init(void)
377{
378 DFC_DEBUG2("Setting up DFC GPIO's.\n");
379
380
381 writel(0x1, GPIO4);
382
383 writel(0x00000001, DF_ALE_nWE1);
384 writel(0x00000001, DF_ALE_nWE2);
385 writel(0x00000001, DF_nCS0);
386 writel(0x00000001, DF_nCS1);
387 writel(0x00000001, DF_nWE);
388 writel(0x00000001, DF_nRE);
389 writel(0x00000001, DF_IO0);
390 writel(0x00000001, DF_IO8);
391 writel(0x00000001, DF_IO1);
392 writel(0x00000001, DF_IO9);
393 writel(0x00000001, DF_IO2);
394 writel(0x00000001, DF_IO10);
395 writel(0x00000001, DF_IO3);
396 writel(0x00000001, DF_IO11);
397 writel(0x00000001, DF_IO4);
398 writel(0x00000001, DF_IO12);
399 writel(0x00000001, DF_IO5);
400 writel(0x00000001, DF_IO13);
401 writel(0x00000001, DF_IO6);
402 writel(0x00000001, DF_IO14);
403 writel(0x00000001, DF_IO7);
404 writel(0x00000001, DF_IO15);
405
406 writel(0x1901, DF_nWE);
407 writel(0x1901, DF_nRE);
408 writel(0x1900, DF_CLE_nOE);
409 writel(0x1901, DF_ALE_nWE1);
410 writel(0x1900, DF_INT_RnB);
411}
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431int board_nand_init(struct nand_chip *nand)
432{
433 unsigned long tCH, tCS, tWH, tWP, tRH, tRP, tRP_high, tR, tWHR, tAR;
434
435
436 dfc_gpio_init();
437
438
439 writel(readl(CKENA) | (CKENA_4_NAND | CKENA_9_SMC), CKENA);
440
441#undef CONFIG_SYS_TIMING_TIGHT
442#ifndef CONFIG_SYS_TIMING_TIGHT
443 tCH = MIN(((unsigned long) (NAND_TIMING_tCH * DFC_CLK_PER_US) + 1),
444 DFC_MAX_tCH);
445 tCS = MIN(((unsigned long) (NAND_TIMING_tCS * DFC_CLK_PER_US) + 1),
446 DFC_MAX_tCS);
447 tWH = MIN(((unsigned long) (NAND_TIMING_tWH * DFC_CLK_PER_US) + 1),
448 DFC_MAX_tWH);
449 tWP = MIN(((unsigned long) (NAND_TIMING_tWP * DFC_CLK_PER_US) + 1),
450 DFC_MAX_tWP);
451 tRH = MIN(((unsigned long) (NAND_TIMING_tRH * DFC_CLK_PER_US) + 1),
452 DFC_MAX_tRH);
453 tRP = MIN(((unsigned long) (NAND_TIMING_tRP * DFC_CLK_PER_US) + 1),
454 DFC_MAX_tRP);
455 tR = MIN(((unsigned long) (NAND_TIMING_tR * DFC_CLK_PER_US) + 1),
456 DFC_MAX_tR);
457 tWHR = MIN(((unsigned long) (NAND_TIMING_tWHR * DFC_CLK_PER_US) + 1),
458 DFC_MAX_tWHR);
459 tAR = MIN(((unsigned long) (NAND_TIMING_tAR * DFC_CLK_PER_US) + 1),
460 DFC_MAX_tAR);
461#else
462
463 tCH = MIN(((unsigned long) (NAND_TIMING_tCH * DFC_CLK_PER_US)),
464 DFC_MAX_tCH);
465 tCS = MIN(((unsigned long) (NAND_TIMING_tCS * DFC_CLK_PER_US)),
466 DFC_MAX_tCS);
467 tWH = MIN(((unsigned long) (NAND_TIMING_tWH * DFC_CLK_PER_US)),
468 DFC_MAX_tWH);
469 tWP = MIN(((unsigned long) (NAND_TIMING_tWP * DFC_CLK_PER_US)),
470 DFC_MAX_tWP);
471 tRH = MIN(((unsigned long) (NAND_TIMING_tRH * DFC_CLK_PER_US)),
472 DFC_MAX_tRH);
473 tRP = MIN(((unsigned long) (NAND_TIMING_tRP * DFC_CLK_PER_US)),
474 DFC_MAX_tRP);
475 tR = MIN(((unsigned long) (NAND_TIMING_tR * DFC_CLK_PER_US) - tCH - 2),
476 DFC_MAX_tR);
477 tWHR = MIN(((unsigned long) (NAND_TIMING_tWHR * DFC_CLK_PER_US) - tCH - 2),
478 DFC_MAX_tWHR);
479 tAR = MIN(((unsigned long) (NAND_TIMING_tAR * DFC_CLK_PER_US) - 2),
480 DFC_MAX_tAR);
481#endif
482
483
484 DFC_DEBUG2("tCH=%u, tCS=%u, tWH=%u, tWP=%u, tRH=%u, tRP=%u, tR=%u, tWHR=%u, tAR=%u.\n", tCH, tCS, tWH, tWP, tRH, tRP, tR, tWHR, tAR);
485
486
487 if(tRP & (1 << 4)) {
488 tRP_high = 1;
489 tRP &= ~(1 << 4);
490 } else {
491 tRP_high = 0;
492 }
493
494 writel((tCH << 19) |
495 (tCS << 16) |
496 (tWH << 11) |
497 (tWP << 8) |
498 (tRP_high << 6) |
499 (tRH << 3) |
500 (tRP << 0),
501 NDTR0CS0);
502
503 writel((tR << 16) |
504 (tWHR << 4) |
505 (tAR << 0),
506 NDTR1CS0);
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523 writel(NDCR_SPARE_EN |
524 NDCR_DWIDTH_C |
525 NDCR_DWIDTH_M |
526 (2 << 16) |
527 NDCR_ND_ARB_EN |
528 NDCR_RDYM |
529 NDCR_CS0_PAGEDM |
530 NDCR_CS1_PAGEDM |
531 NDCR_CS0_CMDDM |
532 NDCR_CS1_CMDDM |
533 NDCR_CS0_BBDM |
534 NDCR_CS1_BBDM |
535 NDCR_DBERRM |
536 NDCR_SBERRM |
537 NDCR_WRDREQM |
538 NDCR_RDDREQM |
539 NDCR_WRCMDREQM,
540 NDCR);
541
542
543
544
545
546 nand->cmd_ctrl = dfc_hwcontrol;
547
548 nand->ecc.mode = NAND_ECC_SOFT;
549 nand->ecc.layout = &delta_oob;
550 nand->options = NAND_BUSWIDTH_16;
551 nand->waitfunc = dfc_wait;
552 nand->read_byte = dfc_read_byte;
553 nand->read_word = dfc_read_word;
554 nand->read_buf = dfc_read_buf;
555 nand->write_buf = dfc_write_buf;
556
557 nand->cmdfunc = dfc_cmdfunc;
558 nand->badblock_pattern = &delta_bbt_descr;
559 return 0;
560}
561
562#endif
563