blob: d9a0143e1d3a51cb766a6576e5b15829fb0f8a9b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/mtd/nand/au1550nd.c
3 *
4 * Copyright (C) 2004 Embedded Edge, LLC
5 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006 * $Id: au1550nd.c,v 1.13 2005/11/07 11:14:30 gleixner Exp $
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 */
13
14#include <linux/slab.h>
15#include <linux/init.h>
16#include <linux/module.h>
Sergei Shtylyov35af68b2006-05-16 20:52:06 +040017#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/mtd/mtd.h>
19#include <linux/mtd/nand.h>
20#include <linux/mtd/partitions.h>
Olaf Hering733482e2005-11-08 21:34:55 -080021#include <linux/version.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/io.h>
23
24/* fixme: this is ugly */
25#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 0)
Pete Popovef6f0d12005-09-23 02:44:58 +010026#include <asm/mach-au1x00/au1xxx.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#else
28#include <asm/au1000.h>
29#ifdef CONFIG_MIPS_PB1550
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000030#include <asm/pb1550.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#endif
32#ifdef CONFIG_MIPS_DB1550
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000033#include <asm/db1x00.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#endif
35#endif
36
37/*
38 * MTD structure for NAND controller
39 */
40static struct mtd_info *au1550_mtd = NULL;
41static void __iomem *p_nand;
David Woodhousee0c7d762006-05-13 18:07:53 +010042static int nand_width = 1; /* default x8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/*
45 * Define partitions for flash device
46 */
Jesper Juhl3c6bee12006-01-09 20:54:01 -080047static const struct mtd_partition partition_info[] = {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000048 {
David Woodhousee0c7d762006-05-13 18:07:53 +010049 .name = "NAND FS 0",
50 .offset = 0,
51 .size = 8 * 1024 * 1024},
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000052 {
David Woodhousee0c7d762006-05-13 18:07:53 +010053 .name = "NAND FS 1",
54 .offset = MTDPART_OFS_APPEND,
55 .size = MTDPART_SIZ_FULL}
Linus Torvalds1da177e2005-04-16 15:20:36 -070056};
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58/**
59 * au_read_byte - read one byte from the chip
60 * @mtd: MTD device structure
61 *
62 * read function for 8bit buswith
63 */
64static u_char au_read_byte(struct mtd_info *mtd)
65{
66 struct nand_chip *this = mtd->priv;
67 u_char ret = readb(this->IO_ADDR_R);
68 au_sync();
69 return ret;
70}
71
72/**
73 * au_write_byte - write one byte to the chip
74 * @mtd: MTD device structure
75 * @byte: pointer to data byte to write
76 *
77 * write function for 8it buswith
78 */
79static void au_write_byte(struct mtd_info *mtd, u_char byte)
80{
81 struct nand_chip *this = mtd->priv;
82 writeb(byte, this->IO_ADDR_W);
83 au_sync();
84}
85
86/**
87 * au_read_byte16 - read one byte endianess aware from the chip
88 * @mtd: MTD device structure
89 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000090 * read function for 16bit buswith with
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 * endianess conversion
92 */
93static u_char au_read_byte16(struct mtd_info *mtd)
94{
95 struct nand_chip *this = mtd->priv;
96 u_char ret = (u_char) cpu_to_le16(readw(this->IO_ADDR_R));
97 au_sync();
98 return ret;
99}
100
101/**
102 * au_write_byte16 - write one byte endianess aware to the chip
103 * @mtd: MTD device structure
104 * @byte: pointer to data byte to write
105 *
106 * write function for 16bit buswith with
107 * endianess conversion
108 */
109static void au_write_byte16(struct mtd_info *mtd, u_char byte)
110{
111 struct nand_chip *this = mtd->priv;
112 writew(le16_to_cpu((u16) byte), this->IO_ADDR_W);
113 au_sync();
114}
115
116/**
117 * au_read_word - read one word from the chip
118 * @mtd: MTD device structure
119 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000120 * read function for 16bit buswith without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 * endianess conversion
122 */
123static u16 au_read_word(struct mtd_info *mtd)
124{
125 struct nand_chip *this = mtd->priv;
126 u16 ret = readw(this->IO_ADDR_R);
127 au_sync();
128 return ret;
129}
130
131/**
132 * au_write_word - write one word to the chip
133 * @mtd: MTD device structure
134 * @word: data word to write
135 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000136 * write function for 16bit buswith without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 * endianess conversion
138 */
139static void au_write_word(struct mtd_info *mtd, u16 word)
140{
141 struct nand_chip *this = mtd->priv;
142 writew(word, this->IO_ADDR_W);
143 au_sync();
144}
145
146/**
147 * au_write_buf - write buffer to chip
148 * @mtd: MTD device structure
149 * @buf: data buffer
150 * @len: number of bytes to write
151 *
152 * write function for 8bit buswith
153 */
154static void au_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
155{
156 int i;
157 struct nand_chip *this = mtd->priv;
158
David Woodhousee0c7d762006-05-13 18:07:53 +0100159 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 writeb(buf[i], this->IO_ADDR_W);
161 au_sync();
162 }
163}
164
165/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000166 * au_read_buf - read chip data into buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 * @mtd: MTD device structure
168 * @buf: buffer to store date
169 * @len: number of bytes to read
170 *
171 * read function for 8bit buswith
172 */
173static void au_read_buf(struct mtd_info *mtd, u_char *buf, int len)
174{
175 int i;
176 struct nand_chip *this = mtd->priv;
177
David Woodhousee0c7d762006-05-13 18:07:53 +0100178 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 buf[i] = readb(this->IO_ADDR_R);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000180 au_sync();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
182}
183
184/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000185 * au_verify_buf - Verify chip data against buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 * @mtd: MTD device structure
187 * @buf: buffer containing the data to compare
188 * @len: number of bytes to compare
189 *
190 * verify function for 8bit buswith
191 */
192static int au_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
193{
194 int i;
195 struct nand_chip *this = mtd->priv;
196
David Woodhousee0c7d762006-05-13 18:07:53 +0100197 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (buf[i] != readb(this->IO_ADDR_R))
199 return -EFAULT;
200 au_sync();
201 }
202
203 return 0;
204}
205
206/**
207 * au_write_buf16 - write buffer to chip
208 * @mtd: MTD device structure
209 * @buf: data buffer
210 * @len: number of bytes to write
211 *
212 * write function for 16bit buswith
213 */
214static void au_write_buf16(struct mtd_info *mtd, const u_char *buf, int len)
215{
216 int i;
217 struct nand_chip *this = mtd->priv;
218 u16 *p = (u16 *) buf;
219 len >>= 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000220
David Woodhousee0c7d762006-05-13 18:07:53 +0100221 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 writew(p[i], this->IO_ADDR_W);
223 au_sync();
224 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
228/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000229 * au_read_buf16 - read chip data into buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 * @mtd: MTD device structure
231 * @buf: buffer to store date
232 * @len: number of bytes to read
233 *
234 * read function for 16bit buswith
235 */
236static void au_read_buf16(struct mtd_info *mtd, u_char *buf, int len)
237{
238 int i;
239 struct nand_chip *this = mtd->priv;
240 u16 *p = (u16 *) buf;
241 len >>= 1;
242
David Woodhousee0c7d762006-05-13 18:07:53 +0100243 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 p[i] = readw(this->IO_ADDR_R);
245 au_sync();
246 }
247}
248
249/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000250 * au_verify_buf16 - Verify chip data against buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 * @mtd: MTD device structure
252 * @buf: buffer containing the data to compare
253 * @len: number of bytes to compare
254 *
255 * verify function for 16bit buswith
256 */
257static int au_verify_buf16(struct mtd_info *mtd, const u_char *buf, int len)
258{
259 int i;
260 struct nand_chip *this = mtd->priv;
261 u16 *p = (u16 *) buf;
262 len >>= 1;
263
David Woodhousee0c7d762006-05-13 18:07:53 +0100264 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 if (p[i] != readw(this->IO_ADDR_R))
266 return -EFAULT;
267 au_sync();
268 }
269 return 0;
270}
271
272
273static void au1550_hwcontrol(struct mtd_info *mtd, int cmd)
274{
275 register struct nand_chip *this = mtd->priv;
276
David Woodhousee0c7d762006-05-13 18:07:53 +0100277 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
David Woodhousee0c7d762006-05-13 18:07:53 +0100279 case NAND_CTL_SETCLE:
280 this->IO_ADDR_W = p_nand + MEM_STNAND_CMD;
281 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
David Woodhousee0c7d762006-05-13 18:07:53 +0100283 case NAND_CTL_CLRCLE:
284 this->IO_ADDR_W = p_nand + MEM_STNAND_DATA;
285 break;
286
287 case NAND_CTL_SETALE:
288 this->IO_ADDR_W = p_nand + MEM_STNAND_ADDR;
289 break;
290
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000291 case NAND_CTL_CLRALE:
292 this->IO_ADDR_W = p_nand + MEM_STNAND_DATA;
David Woodhousee0c7d762006-05-13 18:07:53 +0100293 /* FIXME: Nobody knows why this is necessary,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 * but it works only that way */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000295 udelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 break;
297
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000298 case NAND_CTL_SETNCE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 /* assert (force assert) chip enable */
David Woodhousee0c7d762006-05-13 18:07:53 +0100300 au_writel((1 << (4 + NAND_CS)), MEM_STNDCTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 break;
302
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000303 case NAND_CTL_CLRNCE:
David Woodhousee0c7d762006-05-13 18:07:53 +0100304 /* deassert chip enable */
305 au_writel(0, MEM_STNDCTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 break;
307 }
308
309 this->IO_ADDR_R = this->IO_ADDR_W;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 /* Drain the writebuffer */
312 au_sync();
313}
314
315int au1550_device_ready(struct mtd_info *mtd)
316{
317 int ret = (au_readl(MEM_STSTAT) & 0x1) ? 1 : 0;
318 au_sync();
319 return ret;
320}
321
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400322/**
323 * au1550_select_chip - control -CE line
324 * Forbid driving -CE manually permitting the NAND controller to do this.
325 * Keeping -CE asserted during the whole sector reads interferes with the
326 * NOR flash and PCMCIA drivers as it causes contention on the static bus.
327 * We only have to hold -CE low for the NAND read commands since the flash
328 * chip needs it to be asserted during chip not ready time but the NAND
329 * controller keeps it released.
330 *
331 * @mtd: MTD device structure
332 * @chip: chipnumber to select, -1 for deselect
333 */
334static void au1550_select_chip(struct mtd_info *mtd, int chip)
335{
336}
337
338/**
339 * au1550_command - Send command to NAND device
340 * @mtd: MTD device structure
341 * @command: the command to be sent
342 * @column: the column address for this command, -1 if none
343 * @page_addr: the page address for this command, -1 if none
344 */
345static void au1550_command(struct mtd_info *mtd, unsigned command, int column, int page_addr)
346{
347 register struct nand_chip *this = mtd->priv;
348 int ce_override = 0, i;
349 ulong flags;
350
351 /* Begin command latch cycle */
352 this->hwcontrol(mtd, NAND_CTL_SETCLE);
353 /*
354 * Write out the command to the device.
355 */
356 if (command == NAND_CMD_SEQIN) {
357 int readcmd;
358
359 if (column >= mtd->oobblock) {
360 /* OOB area */
361 column -= mtd->oobblock;
362 readcmd = NAND_CMD_READOOB;
363 } else if (column < 256) {
364 /* First 256 bytes --> READ0 */
365 readcmd = NAND_CMD_READ0;
366 } else {
367 column -= 256;
368 readcmd = NAND_CMD_READ1;
369 }
370 this->write_byte(mtd, readcmd);
371 }
372 this->write_byte(mtd, command);
373
374 /* Set ALE and clear CLE to start address cycle */
375 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
376
377 if (column != -1 || page_addr != -1) {
378 this->hwcontrol(mtd, NAND_CTL_SETALE);
379
380 /* Serially input address */
381 if (column != -1) {
382 /* Adjust columns for 16 bit buswidth */
383 if (this->options & NAND_BUSWIDTH_16)
384 column >>= 1;
385 this->write_byte(mtd, column);
386 }
387 if (page_addr != -1) {
388 this->write_byte(mtd, (u8)(page_addr & 0xff));
389
390 if (command == NAND_CMD_READ0 ||
391 command == NAND_CMD_READ1 ||
392 command == NAND_CMD_READOOB) {
393 /*
394 * NAND controller will release -CE after
395 * the last address byte is written, so we'll
396 * have to forcibly assert it. No interrupts
397 * are allowed while we do this as we don't
398 * want the NOR flash or PCMCIA drivers to
399 * steal our precious bytes of data...
400 */
401 ce_override = 1;
402 local_irq_save(flags);
403 this->hwcontrol(mtd, NAND_CTL_SETNCE);
404 }
405
406 this->write_byte(mtd, (u8)(page_addr >> 8));
407
408 /* One more address cycle for devices > 32MiB */
409 if (this->chipsize > (32 << 20))
410 this->write_byte(mtd, (u8)((page_addr >> 16) & 0x0f));
411 }
412 /* Latch in address */
413 this->hwcontrol(mtd, NAND_CTL_CLRALE);
414 }
415
416 /*
417 * Program and erase have their own busy handlers.
418 * Status and sequential in need no delay.
419 */
420 switch (command) {
421
422 case NAND_CMD_PAGEPROG:
423 case NAND_CMD_ERASE1:
424 case NAND_CMD_ERASE2:
425 case NAND_CMD_SEQIN:
426 case NAND_CMD_STATUS:
427 return;
428
429 case NAND_CMD_RESET:
430 break;
431
432 case NAND_CMD_READ0:
433 case NAND_CMD_READ1:
434 case NAND_CMD_READOOB:
435 /* Check if we're really driving -CE low (just in case) */
436 if (unlikely(!ce_override))
437 break;
438
439 /* Apply a short delay always to ensure that we do wait tWB. */
440 ndelay(100);
441 /* Wait for a chip to become ready... */
442 for (i = this->chip_delay; !this->dev_ready(mtd) && i > 0; --i)
443 udelay(1);
444
445 /* Release -CE and re-enable interrupts. */
446 this->hwcontrol(mtd, NAND_CTL_CLRNCE);
447 local_irq_restore(flags);
448 return;
449 }
450 /* Apply this short delay always to ensure that we do wait tWB. */
451 ndelay(100);
452
453 while(!this->dev_ready(mtd));
454}
455
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457/*
458 * Main initialization routine
459 */
David Woodhousecead4db2006-05-16 13:54:50 +0100460static int __init au1xxx_nand_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
462 struct nand_chip *this;
David Woodhousee0c7d762006-05-13 18:07:53 +0100463 u16 boot_swapboot = 0; /* default value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 int retval;
Pete Popovef6f0d12005-09-23 02:44:58 +0100465 u32 mem_staddr;
466 u32 nand_phys;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 /* Allocate memory for MTD device structure and private data */
David Woodhousee0c7d762006-05-13 18:07:53 +0100469 au1550_mtd = kmalloc(sizeof(struct mtd_info) + sizeof(struct nand_chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 if (!au1550_mtd) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100471 printk("Unable to allocate NAND MTD dev structure.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return -ENOMEM;
473 }
474
475 /* Get pointer to private data */
David Woodhousee0c7d762006-05-13 18:07:53 +0100476 this = (struct nand_chip *)(&au1550_mtd[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 /* Initialize structures */
David Woodhousee0c7d762006-05-13 18:07:53 +0100479 memset(au1550_mtd, 0, sizeof(struct mtd_info));
480 memset(this, 0, sizeof(struct nand_chip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 /* Link the private data with the MTD structure */
483 au1550_mtd->priv = this;
David Woodhouse552d9202006-05-14 01:20:46 +0100484 au1550_mtd->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000486
Sergei Shtylyov155285c2006-05-16 20:16:41 +0400487 /* MEM_STNDCTL: disable ints, disable nand boot */
488 au_writel(0, MEM_STNDCTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490#ifdef CONFIG_MIPS_PB1550
491 /* set gpio206 high */
David Woodhousee0c7d762006-05-13 18:07:53 +0100492 au_writel(au_readl(GPIO2_DIR) & ~(1 << 6), GPIO2_DIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
David Woodhousee0c7d762006-05-13 18:07:53 +0100494 boot_swapboot = (au_readl(MEM_STSTAT) & (0x7 << 1)) | ((bcsr->status >> 6) & 0x1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 switch (boot_swapboot) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100496 case 0:
497 case 2:
498 case 8:
499 case 0xC:
500 case 0xD:
501 /* x16 NAND Flash */
502 nand_width = 0;
503 break;
504 case 1:
505 case 9:
506 case 3:
507 case 0xE:
508 case 0xF:
509 /* x8 NAND Flash */
510 nand_width = 1;
511 break;
512 default:
513 printk("Pb1550 NAND: bad boot:swap\n");
514 retval = -EINVAL;
515 goto outmem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
517#endif
518
Pete Popovef6f0d12005-09-23 02:44:58 +0100519 /* Configure chip-select; normally done by boot code, e.g. YAMON */
520#ifdef NAND_STCFG
521 if (NAND_CS == 0) {
522 au_writel(NAND_STCFG, MEM_STCFG0);
523 au_writel(NAND_STTIME, MEM_STTIME0);
524 au_writel(NAND_STADDR, MEM_STADDR0);
525 }
526 if (NAND_CS == 1) {
527 au_writel(NAND_STCFG, MEM_STCFG1);
528 au_writel(NAND_STTIME, MEM_STTIME1);
529 au_writel(NAND_STADDR, MEM_STADDR1);
530 }
531 if (NAND_CS == 2) {
532 au_writel(NAND_STCFG, MEM_STCFG2);
533 au_writel(NAND_STTIME, MEM_STTIME2);
534 au_writel(NAND_STADDR, MEM_STADDR2);
535 }
536 if (NAND_CS == 3) {
537 au_writel(NAND_STCFG, MEM_STCFG3);
538 au_writel(NAND_STTIME, MEM_STTIME3);
539 au_writel(NAND_STADDR, MEM_STADDR3);
540 }
541#endif
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000542
Pete Popovef6f0d12005-09-23 02:44:58 +0100543 /* Locate NAND chip-select in order to determine NAND phys address */
544 mem_staddr = 0x00000000;
545 if (((au_readl(MEM_STCFG0) & 0x7) == 0x5) && (NAND_CS == 0))
546 mem_staddr = au_readl(MEM_STADDR0);
547 else if (((au_readl(MEM_STCFG1) & 0x7) == 0x5) && (NAND_CS == 1))
548 mem_staddr = au_readl(MEM_STADDR1);
549 else if (((au_readl(MEM_STCFG2) & 0x7) == 0x5) && (NAND_CS == 2))
550 mem_staddr = au_readl(MEM_STADDR2);
551 else if (((au_readl(MEM_STCFG3) & 0x7) == 0x5) && (NAND_CS == 3))
552 mem_staddr = au_readl(MEM_STADDR3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Pete Popovef6f0d12005-09-23 02:44:58 +0100554 if (mem_staddr == 0x00000000) {
555 printk("Au1xxx NAND: ERROR WITH NAND CHIP-SELECT\n");
556 kfree(au1550_mtd);
557 return 1;
558 }
559 nand_phys = (mem_staddr << 4) & 0xFFFC0000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Pete Popovef6f0d12005-09-23 02:44:58 +0100561 p_nand = (void __iomem *)ioremap(nand_phys, 0x1000);
562
563 /* make controller and MTD agree */
564 if (NAND_CS == 0)
David Woodhousee0c7d762006-05-13 18:07:53 +0100565 nand_width = au_readl(MEM_STCFG0) & (1 << 22);
Pete Popovef6f0d12005-09-23 02:44:58 +0100566 if (NAND_CS == 1)
David Woodhousee0c7d762006-05-13 18:07:53 +0100567 nand_width = au_readl(MEM_STCFG1) & (1 << 22);
Pete Popovef6f0d12005-09-23 02:44:58 +0100568 if (NAND_CS == 2)
David Woodhousee0c7d762006-05-13 18:07:53 +0100569 nand_width = au_readl(MEM_STCFG2) & (1 << 22);
Pete Popovef6f0d12005-09-23 02:44:58 +0100570 if (NAND_CS == 3)
David Woodhousee0c7d762006-05-13 18:07:53 +0100571 nand_width = au_readl(MEM_STCFG3) & (1 << 22);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 /* Set address of hardware control function */
574 this->hwcontrol = au1550_hwcontrol;
575 this->dev_ready = au1550_device_ready;
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400576 this->select_chip = au1550_select_chip;
577 this->cmdfunc = au1550_command;
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 /* 30 us command delay time */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000580 this->chip_delay = 30;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 this->eccmode = NAND_ECC_SOFT;
582
583 this->options = NAND_NO_AUTOINCR;
584
585 if (!nand_width)
586 this->options |= NAND_BUSWIDTH_16;
587
588 this->read_byte = (!nand_width) ? au_read_byte16 : au_read_byte;
589 this->write_byte = (!nand_width) ? au_write_byte16 : au_write_byte;
590 this->write_word = au_write_word;
591 this->read_word = au_read_word;
592 this->write_buf = (!nand_width) ? au_write_buf16 : au_write_buf;
593 this->read_buf = (!nand_width) ? au_read_buf16 : au_read_buf;
594 this->verify_buf = (!nand_width) ? au_verify_buf16 : au_verify_buf;
595
596 /* Scan to find existence of the device */
David Woodhousee0c7d762006-05-13 18:07:53 +0100597 if (nand_scan(au1550_mtd, 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 retval = -ENXIO;
599 goto outio;
600 }
601
602 /* Register the partitions */
Tobias Klauser87d10f32006-03-31 02:29:45 -0800603 add_mtd_partitions(au1550_mtd, partition_info, ARRAY_SIZE(partition_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 return 0;
606
607 outio:
David Woodhousee0c7d762006-05-13 18:07:53 +0100608 iounmap((void *)p_nand);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 outmem:
David Woodhousee0c7d762006-05-13 18:07:53 +0100611 kfree(au1550_mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 return retval;
613}
614
Pete Popovef6f0d12005-09-23 02:44:58 +0100615module_init(au1xxx_nand_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617/*
618 * Clean up routine
619 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100620static void __exit au1550_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
David Woodhousee0c7d762006-05-13 18:07:53 +0100622 struct nand_chip *this = (struct nand_chip *)&au1550_mtd[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 /* Release resources, unregister device */
David Woodhousee0c7d762006-05-13 18:07:53 +0100625 nand_release(au1550_mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 /* Free the MTD device structure */
David Woodhousee0c7d762006-05-13 18:07:53 +0100628 kfree(au1550_mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630 /* Unmap */
David Woodhousee0c7d762006-05-13 18:07:53 +0100631 iounmap((void *)p_nand);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632}
David Woodhousee0c7d762006-05-13 18:07:53 +0100633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634module_exit(au1550_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636MODULE_LICENSE("GPL");
637MODULE_AUTHOR("Embedded Edge, LLC");
638MODULE_DESCRIPTION("Board-specific glue layer for NAND flash on Pb1550 board");