1
2
3
4
5
6
7
8
9
10
11
12#include <linux/slab.h>
13#include <linux/gpio.h>
14#include <linux/init.h>
15#include <linux/module.h>
16#include <linux/interrupt.h>
17#include <linux/mtd/mtd.h>
18#include <linux/mtd/nand.h>
19#include <linux/mtd/partitions.h>
20#include <asm/io.h>
21
22#include <asm/mach-au1x00/au1xxx.h>
23#include <asm/mach-db1x00/bcsr.h>
24
25
26
27
28static struct mtd_info *au1550_mtd = NULL;
29static void __iomem *p_nand;
30static int nand_width = 1;
31static void (*au1550_write_byte)(struct mtd_info *, u_char);
32
33
34
35
36static const struct mtd_partition partition_info[] = {
37 {
38 .name = "NAND FS 0",
39 .offset = 0,
40 .size = 8 * 1024 * 1024},
41 {
42 .name = "NAND FS 1",
43 .offset = MTDPART_OFS_APPEND,
44 .size = MTDPART_SIZ_FULL}
45};
46
47
48
49
50
51
52
53static u_char au_read_byte(struct mtd_info *mtd)
54{
55 struct nand_chip *this = mtd->priv;
56 u_char ret = readb(this->IO_ADDR_R);
57 au_sync();
58 return ret;
59}
60
61
62
63
64
65
66
67
68static void au_write_byte(struct mtd_info *mtd, u_char byte)
69{
70 struct nand_chip *this = mtd->priv;
71 writeb(byte, this->IO_ADDR_W);
72 au_sync();
73}
74
75
76
77
78
79
80
81
82static u_char au_read_byte16(struct mtd_info *mtd)
83{
84 struct nand_chip *this = mtd->priv;
85 u_char ret = (u_char) cpu_to_le16(readw(this->IO_ADDR_R));
86 au_sync();
87 return ret;
88}
89
90
91
92
93
94
95
96
97
98static void au_write_byte16(struct mtd_info *mtd, u_char byte)
99{
100 struct nand_chip *this = mtd->priv;
101 writew(le16_to_cpu((u16) byte), this->IO_ADDR_W);
102 au_sync();
103}
104
105
106
107
108
109
110
111
112static u16 au_read_word(struct mtd_info *mtd)
113{
114 struct nand_chip *this = mtd->priv;
115 u16 ret = readw(this->IO_ADDR_R);
116 au_sync();
117 return ret;
118}
119
120
121
122
123
124
125
126
127
128static void au_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
129{
130 int i;
131 struct nand_chip *this = mtd->priv;
132
133 for (i = 0; i < len; i++) {
134 writeb(buf[i], this->IO_ADDR_W);
135 au_sync();
136 }
137}
138
139
140
141
142
143
144
145
146
147static void au_read_buf(struct mtd_info *mtd, u_char *buf, int len)
148{
149 int i;
150 struct nand_chip *this = mtd->priv;
151
152 for (i = 0; i < len; i++) {
153 buf[i] = readb(this->IO_ADDR_R);
154 au_sync();
155 }
156}
157
158
159
160
161
162
163
164
165
166static int au_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
167{
168 int i;
169 struct nand_chip *this = mtd->priv;
170
171 for (i = 0; i < len; i++) {
172 if (buf[i] != readb(this->IO_ADDR_R))
173 return -EFAULT;
174 au_sync();
175 }
176
177 return 0;
178}
179
180
181
182
183
184
185
186
187
188static void au_write_buf16(struct mtd_info *mtd, const u_char *buf, int len)
189{
190 int i;
191 struct nand_chip *this = mtd->priv;
192 u16 *p = (u16 *) buf;
193 len >>= 1;
194
195 for (i = 0; i < len; i++) {
196 writew(p[i], this->IO_ADDR_W);
197 au_sync();
198 }
199
200}
201
202
203
204
205
206
207
208
209
210static void au_read_buf16(struct mtd_info *mtd, u_char *buf, int len)
211{
212 int i;
213 struct nand_chip *this = mtd->priv;
214 u16 *p = (u16 *) buf;
215 len >>= 1;
216
217 for (i = 0; i < len; i++) {
218 p[i] = readw(this->IO_ADDR_R);
219 au_sync();
220 }
221}
222
223
224
225
226
227
228
229
230
231static int au_verify_buf16(struct mtd_info *mtd, const u_char *buf, int len)
232{
233 int i;
234 struct nand_chip *this = mtd->priv;
235 u16 *p = (u16 *) buf;
236 len >>= 1;
237
238 for (i = 0; i < len; i++) {
239 if (p[i] != readw(this->IO_ADDR_R))
240 return -EFAULT;
241 au_sync();
242 }
243 return 0;
244}
245
246
247#define NAND_CTL_SETNCE 1
248
249#define NAND_CTL_CLRNCE 2
250
251#define NAND_CTL_SETCLE 3
252
253#define NAND_CTL_CLRCLE 4
254
255#define NAND_CTL_SETALE 5
256
257#define NAND_CTL_CLRALE 6
258
259static void au1550_hwcontrol(struct mtd_info *mtd, int cmd)
260{
261 register struct nand_chip *this = mtd->priv;
262
263 switch (cmd) {
264
265 case NAND_CTL_SETCLE:
266 this->IO_ADDR_W = p_nand + MEM_STNAND_CMD;
267 break;
268
269 case NAND_CTL_CLRCLE:
270 this->IO_ADDR_W = p_nand + MEM_STNAND_DATA;
271 break;
272
273 case NAND_CTL_SETALE:
274 this->IO_ADDR_W = p_nand + MEM_STNAND_ADDR;
275 break;
276
277 case NAND_CTL_CLRALE:
278 this->IO_ADDR_W = p_nand + MEM_STNAND_DATA;
279
280
281 udelay(1);
282 break;
283
284 case NAND_CTL_SETNCE:
285
286 au_writel((1 << (4 + NAND_CS)), MEM_STNDCTL);
287 break;
288
289 case NAND_CTL_CLRNCE:
290
291 au_writel(0, MEM_STNDCTL);
292 break;
293 }
294
295 this->IO_ADDR_R = this->IO_ADDR_W;
296
297
298 au_sync();
299}
300
301int au1550_device_ready(struct mtd_info *mtd)
302{
303 int ret = (au_readl(MEM_STSTAT) & 0x1) ? 1 : 0;
304 au_sync();
305 return ret;
306}
307
308
309
310
311
312
313
314
315
316
317
318
319
320static void au1550_select_chip(struct mtd_info *mtd, int chip)
321{
322}
323
324
325
326
327
328
329
330
331static void au1550_command(struct mtd_info *mtd, unsigned command, int column, int page_addr)
332{
333 register struct nand_chip *this = mtd->priv;
334 int ce_override = 0, i;
335 ulong flags;
336
337
338 au1550_hwcontrol(mtd, NAND_CTL_SETCLE);
339
340
341
342 if (command == NAND_CMD_SEQIN) {
343 int readcmd;
344
345 if (column >= mtd->writesize) {
346
347 column -= mtd->writesize;
348 readcmd = NAND_CMD_READOOB;
349 } else if (column < 256) {
350
351 readcmd = NAND_CMD_READ0;
352 } else {
353 column -= 256;
354 readcmd = NAND_CMD_READ1;
355 }
356 au1550_write_byte(mtd, readcmd);
357 }
358 au1550_write_byte(mtd, command);
359
360
361 au1550_hwcontrol(mtd, NAND_CTL_CLRCLE);
362
363 if (column != -1 || page_addr != -1) {
364 au1550_hwcontrol(mtd, NAND_CTL_SETALE);
365
366
367 if (column != -1) {
368
369 if (this->options & NAND_BUSWIDTH_16)
370 column >>= 1;
371 au1550_write_byte(mtd, column);
372 }
373 if (page_addr != -1) {
374 au1550_write_byte(mtd, (u8)(page_addr & 0xff));
375
376 if (command == NAND_CMD_READ0 ||
377 command == NAND_CMD_READ1 ||
378 command == NAND_CMD_READOOB) {
379
380
381
382
383
384
385
386
387 ce_override = 1;
388 local_irq_save(flags);
389 au1550_hwcontrol(mtd, NAND_CTL_SETNCE);
390 }
391
392 au1550_write_byte(mtd, (u8)(page_addr >> 8));
393
394
395 if (this->chipsize > (32 << 20))
396 au1550_write_byte(mtd, (u8)((page_addr >> 16) & 0x0f));
397 }
398
399 au1550_hwcontrol(mtd, NAND_CTL_CLRALE);
400 }
401
402
403
404
405
406 switch (command) {
407
408 case NAND_CMD_PAGEPROG:
409 case NAND_CMD_ERASE1:
410 case NAND_CMD_ERASE2:
411 case NAND_CMD_SEQIN:
412 case NAND_CMD_STATUS:
413 return;
414
415 case NAND_CMD_RESET:
416 break;
417
418 case NAND_CMD_READ0:
419 case NAND_CMD_READ1:
420 case NAND_CMD_READOOB:
421
422 if (unlikely(!ce_override))
423 break;
424
425
426 ndelay(100);
427
428 for (i = this->chip_delay; !this->dev_ready(mtd) && i > 0; --i)
429 udelay(1);
430
431
432 au1550_hwcontrol(mtd, NAND_CTL_CLRNCE);
433 local_irq_restore(flags);
434 return;
435 }
436
437 ndelay(100);
438
439 while(!this->dev_ready(mtd));
440}
441
442
443
444
445
446static int __init au1xxx_nand_init(void)
447{
448 struct nand_chip *this;
449 u16 boot_swapboot = 0;
450 int retval;
451 u32 mem_staddr;
452 u32 nand_phys;
453
454
455 au1550_mtd = kzalloc(sizeof(struct mtd_info) + sizeof(struct nand_chip), GFP_KERNEL);
456 if (!au1550_mtd) {
457 printk("Unable to allocate NAND MTD dev structure.\n");
458 return -ENOMEM;
459 }
460
461
462 this = (struct nand_chip *)(&au1550_mtd[1]);
463
464
465 au1550_mtd->priv = this;
466 au1550_mtd->owner = THIS_MODULE;
467
468
469
470 au_writel(0, MEM_STNDCTL);
471
472#ifdef CONFIG_MIPS_PB1550
473
474 gpio_direction_input(206);
475
476 boot_swapboot = (au_readl(MEM_STSTAT) & (0x7 << 1)) | ((bcsr_read(BCSR_STATUS) >> 6) & 0x1);
477
478 switch (boot_swapboot) {
479 case 0:
480 case 2:
481 case 8:
482 case 0xC:
483 case 0xD:
484
485 nand_width = 0;
486 break;
487 case 1:
488 case 9:
489 case 3:
490 case 0xE:
491 case 0xF:
492
493 nand_width = 1;
494 break;
495 default:
496 printk("Pb1550 NAND: bad boot:swap\n");
497 retval = -EINVAL;
498 goto outmem;
499 }
500#endif
501
502
503#ifdef NAND_STCFG
504 if (NAND_CS == 0) {
505 au_writel(NAND_STCFG, MEM_STCFG0);
506 au_writel(NAND_STTIME, MEM_STTIME0);
507 au_writel(NAND_STADDR, MEM_STADDR0);
508 }
509 if (NAND_CS == 1) {
510 au_writel(NAND_STCFG, MEM_STCFG1);
511 au_writel(NAND_STTIME, MEM_STTIME1);
512 au_writel(NAND_STADDR, MEM_STADDR1);
513 }
514 if (NAND_CS == 2) {
515 au_writel(NAND_STCFG, MEM_STCFG2);
516 au_writel(NAND_STTIME, MEM_STTIME2);
517 au_writel(NAND_STADDR, MEM_STADDR2);
518 }
519 if (NAND_CS == 3) {
520 au_writel(NAND_STCFG, MEM_STCFG3);
521 au_writel(NAND_STTIME, MEM_STTIME3);
522 au_writel(NAND_STADDR, MEM_STADDR3);
523 }
524#endif
525
526
527 mem_staddr = 0x00000000;
528 if (((au_readl(MEM_STCFG0) & 0x7) == 0x5) && (NAND_CS == 0))
529 mem_staddr = au_readl(MEM_STADDR0);
530 else if (((au_readl(MEM_STCFG1) & 0x7) == 0x5) && (NAND_CS == 1))
531 mem_staddr = au_readl(MEM_STADDR1);
532 else if (((au_readl(MEM_STCFG2) & 0x7) == 0x5) && (NAND_CS == 2))
533 mem_staddr = au_readl(MEM_STADDR2);
534 else if (((au_readl(MEM_STCFG3) & 0x7) == 0x5) && (NAND_CS == 3))
535 mem_staddr = au_readl(MEM_STADDR3);
536
537 if (mem_staddr == 0x00000000) {
538 printk("Au1xxx NAND: ERROR WITH NAND CHIP-SELECT\n");
539 kfree(au1550_mtd);
540 return 1;
541 }
542 nand_phys = (mem_staddr << 4) & 0xFFFC0000;
543
544 p_nand = ioremap(nand_phys, 0x1000);
545
546
547 if (NAND_CS == 0)
548 nand_width = au_readl(MEM_STCFG0) & (1 << 22);
549 if (NAND_CS == 1)
550 nand_width = au_readl(MEM_STCFG1) & (1 << 22);
551 if (NAND_CS == 2)
552 nand_width = au_readl(MEM_STCFG2) & (1 << 22);
553 if (NAND_CS == 3)
554 nand_width = au_readl(MEM_STCFG3) & (1 << 22);
555
556
557 this->dev_ready = au1550_device_ready;
558 this->select_chip = au1550_select_chip;
559 this->cmdfunc = au1550_command;
560
561
562 this->chip_delay = 30;
563 this->ecc.mode = NAND_ECC_SOFT;
564
565 this->options = NAND_NO_AUTOINCR;
566
567 if (!nand_width)
568 this->options |= NAND_BUSWIDTH_16;
569
570 this->read_byte = (!nand_width) ? au_read_byte16 : au_read_byte;
571 au1550_write_byte = (!nand_width) ? au_write_byte16 : au_write_byte;
572 this->read_word = au_read_word;
573 this->write_buf = (!nand_width) ? au_write_buf16 : au_write_buf;
574 this->read_buf = (!nand_width) ? au_read_buf16 : au_read_buf;
575 this->verify_buf = (!nand_width) ? au_verify_buf16 : au_verify_buf;
576
577
578 if (nand_scan(au1550_mtd, 1)) {
579 retval = -ENXIO;
580 goto outio;
581 }
582
583
584 mtd_device_register(au1550_mtd, partition_info,
585 ARRAY_SIZE(partition_info));
586
587 return 0;
588
589 outio:
590 iounmap(p_nand);
591
592 outmem:
593 kfree(au1550_mtd);
594 return retval;
595}
596
597module_init(au1xxx_nand_init);
598
599
600
601
602static void __exit au1550_cleanup(void)
603{
604
605 nand_release(au1550_mtd);
606
607
608 kfree(au1550_mtd);
609
610
611 iounmap(p_nand);
612}
613
614module_exit(au1550_cleanup);
615
616MODULE_LICENSE("GPL");
617MODULE_AUTHOR("Embedded Edge, LLC");
618MODULE_DESCRIPTION("Board-specific glue layer for NAND flash on Pb1550 board");
619