blob: 31228334da12425dbfe18f2cc3155894aaaf34a5 [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 */
Thomas Gleixnercad74f22006-05-23 23:28:48 +020043static void (*au1550_write_byte)(struct mtd_info *, u_char);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/*
46 * Define partitions for flash device
47 */
Jesper Juhl3c6bee12006-01-09 20:54:01 -080048static const struct mtd_partition partition_info[] = {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000049 {
David Woodhousee0c7d762006-05-13 18:07:53 +010050 .name = "NAND FS 0",
51 .offset = 0,
52 .size = 8 * 1024 * 1024},
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000053 {
David Woodhousee0c7d762006-05-13 18:07:53 +010054 .name = "NAND FS 1",
55 .offset = MTDPART_OFS_APPEND,
56 .size = MTDPART_SIZ_FULL}
Linus Torvalds1da177e2005-04-16 15:20:36 -070057};
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59/**
60 * au_read_byte - read one byte from the chip
61 * @mtd: MTD device structure
62 *
63 * read function for 8bit buswith
64 */
65static u_char au_read_byte(struct mtd_info *mtd)
66{
67 struct nand_chip *this = mtd->priv;
68 u_char ret = readb(this->IO_ADDR_R);
69 au_sync();
70 return ret;
71}
72
73/**
74 * au_write_byte - write one byte to the chip
75 * @mtd: MTD device structure
76 * @byte: pointer to data byte to write
77 *
78 * write function for 8it buswith
79 */
80static void au_write_byte(struct mtd_info *mtd, u_char byte)
81{
82 struct nand_chip *this = mtd->priv;
83 writeb(byte, this->IO_ADDR_W);
84 au_sync();
85}
86
87/**
88 * au_read_byte16 - read one byte endianess aware from the chip
89 * @mtd: MTD device structure
90 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000091 * read function for 16bit buswith with
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 * endianess conversion
93 */
94static u_char au_read_byte16(struct mtd_info *mtd)
95{
96 struct nand_chip *this = mtd->priv;
97 u_char ret = (u_char) cpu_to_le16(readw(this->IO_ADDR_R));
98 au_sync();
99 return ret;
100}
101
102/**
103 * au_write_byte16 - write one byte endianess aware to the chip
104 * @mtd: MTD device structure
105 * @byte: pointer to data byte to write
106 *
107 * write function for 16bit buswith with
108 * endianess conversion
109 */
110static void au_write_byte16(struct mtd_info *mtd, u_char byte)
111{
112 struct nand_chip *this = mtd->priv;
113 writew(le16_to_cpu((u16) byte), this->IO_ADDR_W);
114 au_sync();
115}
116
117/**
118 * au_read_word - read one word from the chip
119 * @mtd: MTD device structure
120 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000121 * read function for 16bit buswith without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 * endianess conversion
123 */
124static u16 au_read_word(struct mtd_info *mtd)
125{
126 struct nand_chip *this = mtd->priv;
127 u16 ret = readw(this->IO_ADDR_R);
128 au_sync();
129 return ret;
130}
131
132/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 * au_write_buf - write buffer to chip
134 * @mtd: MTD device structure
135 * @buf: data buffer
136 * @len: number of bytes to write
137 *
138 * write function for 8bit buswith
139 */
140static void au_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
141{
142 int i;
143 struct nand_chip *this = mtd->priv;
144
David Woodhousee0c7d762006-05-13 18:07:53 +0100145 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 writeb(buf[i], this->IO_ADDR_W);
147 au_sync();
148 }
149}
150
151/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000152 * au_read_buf - read chip data into buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 * @mtd: MTD device structure
154 * @buf: buffer to store date
155 * @len: number of bytes to read
156 *
157 * read function for 8bit buswith
158 */
159static void au_read_buf(struct mtd_info *mtd, u_char *buf, int len)
160{
161 int i;
162 struct nand_chip *this = mtd->priv;
163
David Woodhousee0c7d762006-05-13 18:07:53 +0100164 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 buf[i] = readb(this->IO_ADDR_R);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000166 au_sync();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
168}
169
170/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000171 * au_verify_buf - Verify chip data against buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 * @mtd: MTD device structure
173 * @buf: buffer containing the data to compare
174 * @len: number of bytes to compare
175 *
176 * verify function for 8bit buswith
177 */
178static int au_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
179{
180 int i;
181 struct nand_chip *this = mtd->priv;
182
David Woodhousee0c7d762006-05-13 18:07:53 +0100183 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 if (buf[i] != readb(this->IO_ADDR_R))
185 return -EFAULT;
186 au_sync();
187 }
188
189 return 0;
190}
191
192/**
193 * au_write_buf16 - write buffer to chip
194 * @mtd: MTD device structure
195 * @buf: data buffer
196 * @len: number of bytes to write
197 *
198 * write function for 16bit buswith
199 */
200static void au_write_buf16(struct mtd_info *mtd, const u_char *buf, int len)
201{
202 int i;
203 struct nand_chip *this = mtd->priv;
204 u16 *p = (u16 *) buf;
205 len >>= 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000206
David Woodhousee0c7d762006-05-13 18:07:53 +0100207 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 writew(p[i], this->IO_ADDR_W);
209 au_sync();
210 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
214/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000215 * au_read_buf16 - read chip data into buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 * @mtd: MTD device structure
217 * @buf: buffer to store date
218 * @len: number of bytes to read
219 *
220 * read function for 16bit buswith
221 */
222static void au_read_buf16(struct mtd_info *mtd, u_char *buf, int len)
223{
224 int i;
225 struct nand_chip *this = mtd->priv;
226 u16 *p = (u16 *) buf;
227 len >>= 1;
228
David Woodhousee0c7d762006-05-13 18:07:53 +0100229 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 p[i] = readw(this->IO_ADDR_R);
231 au_sync();
232 }
233}
234
235/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000236 * au_verify_buf16 - Verify chip data against buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 * @mtd: MTD device structure
238 * @buf: buffer containing the data to compare
239 * @len: number of bytes to compare
240 *
241 * verify function for 16bit buswith
242 */
243static int au_verify_buf16(struct mtd_info *mtd, const u_char *buf, int len)
244{
245 int i;
246 struct nand_chip *this = mtd->priv;
247 u16 *p = (u16 *) buf;
248 len >>= 1;
249
David Woodhousee0c7d762006-05-13 18:07:53 +0100250 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 if (p[i] != readw(this->IO_ADDR_R))
252 return -EFAULT;
253 au_sync();
254 }
255 return 0;
256}
257
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200258/* Select the chip by setting nCE to low */
259#define NAND_CTL_SETNCE 1
260/* Deselect the chip by setting nCE to high */
261#define NAND_CTL_CLRNCE 2
262/* Select the command latch by setting CLE to high */
263#define NAND_CTL_SETCLE 3
264/* Deselect the command latch by setting CLE to low */
265#define NAND_CTL_CLRCLE 4
266/* Select the address latch by setting ALE to high */
267#define NAND_CTL_SETALE 5
268/* Deselect the address latch by setting ALE to low */
269#define NAND_CTL_CLRALE 6
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271static void au1550_hwcontrol(struct mtd_info *mtd, int cmd)
272{
273 register struct nand_chip *this = mtd->priv;
274
David Woodhousee0c7d762006-05-13 18:07:53 +0100275 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
David Woodhousee0c7d762006-05-13 18:07:53 +0100277 case NAND_CTL_SETCLE:
278 this->IO_ADDR_W = p_nand + MEM_STNAND_CMD;
279 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
David Woodhousee0c7d762006-05-13 18:07:53 +0100281 case NAND_CTL_CLRCLE:
282 this->IO_ADDR_W = p_nand + MEM_STNAND_DATA;
283 break;
284
285 case NAND_CTL_SETALE:
286 this->IO_ADDR_W = p_nand + MEM_STNAND_ADDR;
287 break;
288
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000289 case NAND_CTL_CLRALE:
290 this->IO_ADDR_W = p_nand + MEM_STNAND_DATA;
David Woodhousee0c7d762006-05-13 18:07:53 +0100291 /* FIXME: Nobody knows why this is necessary,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 * but it works only that way */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000293 udelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 break;
295
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000296 case NAND_CTL_SETNCE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 /* assert (force assert) chip enable */
David Woodhousee0c7d762006-05-13 18:07:53 +0100298 au_writel((1 << (4 + NAND_CS)), MEM_STNDCTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 break;
300
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000301 case NAND_CTL_CLRNCE:
David Woodhousee0c7d762006-05-13 18:07:53 +0100302 /* deassert chip enable */
303 au_writel(0, MEM_STNDCTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 break;
305 }
306
307 this->IO_ADDR_R = this->IO_ADDR_W;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 /* Drain the writebuffer */
310 au_sync();
311}
312
313int au1550_device_ready(struct mtd_info *mtd)
314{
315 int ret = (au_readl(MEM_STSTAT) & 0x1) ? 1 : 0;
316 au_sync();
317 return ret;
318}
319
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400320/**
321 * au1550_select_chip - control -CE line
322 * Forbid driving -CE manually permitting the NAND controller to do this.
323 * Keeping -CE asserted during the whole sector reads interferes with the
324 * NOR flash and PCMCIA drivers as it causes contention on the static bus.
325 * We only have to hold -CE low for the NAND read commands since the flash
326 * chip needs it to be asserted during chip not ready time but the NAND
327 * controller keeps it released.
328 *
329 * @mtd: MTD device structure
330 * @chip: chipnumber to select, -1 for deselect
331 */
332static void au1550_select_chip(struct mtd_info *mtd, int chip)
333{
334}
335
336/**
337 * au1550_command - Send command to NAND device
338 * @mtd: MTD device structure
339 * @command: the command to be sent
340 * @column: the column address for this command, -1 if none
341 * @page_addr: the page address for this command, -1 if none
342 */
343static void au1550_command(struct mtd_info *mtd, unsigned command, int column, int page_addr)
344{
345 register struct nand_chip *this = mtd->priv;
346 int ce_override = 0, i;
347 ulong flags;
348
349 /* Begin command latch cycle */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200350 au1550_hwcontrol(mtd, NAND_CTL_SETCLE);
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400351 /*
352 * Write out the command to the device.
353 */
354 if (command == NAND_CMD_SEQIN) {
355 int readcmd;
356
Joern Engel28318772006-05-22 23:18:05 +0200357 if (column >= mtd->writesize) {
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400358 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200359 column -= mtd->writesize;
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400360 readcmd = NAND_CMD_READOOB;
361 } else if (column < 256) {
362 /* First 256 bytes --> READ0 */
363 readcmd = NAND_CMD_READ0;
364 } else {
365 column -= 256;
366 readcmd = NAND_CMD_READ1;
367 }
Thomas Gleixnercad74f22006-05-23 23:28:48 +0200368 au1550_write_byte(mtd, readcmd);
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400369 }
Thomas Gleixnercad74f22006-05-23 23:28:48 +0200370 au1550_write_byte(mtd, command);
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400371
372 /* Set ALE and clear CLE to start address cycle */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200373 au1550_hwcontrol(mtd, NAND_CTL_CLRCLE);
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400374
375 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200376 au1550_hwcontrol(mtd, NAND_CTL_SETALE);
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400377
378 /* Serially input address */
379 if (column != -1) {
380 /* Adjust columns for 16 bit buswidth */
381 if (this->options & NAND_BUSWIDTH_16)
382 column >>= 1;
Thomas Gleixnercad74f22006-05-23 23:28:48 +0200383 au1550_write_byte(mtd, column);
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400384 }
385 if (page_addr != -1) {
Thomas Gleixnercad74f22006-05-23 23:28:48 +0200386 au1550_write_byte(mtd, (u8)(page_addr & 0xff));
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400387
388 if (command == NAND_CMD_READ0 ||
389 command == NAND_CMD_READ1 ||
390 command == NAND_CMD_READOOB) {
391 /*
392 * NAND controller will release -CE after
393 * the last address byte is written, so we'll
394 * have to forcibly assert it. No interrupts
395 * are allowed while we do this as we don't
396 * want the NOR flash or PCMCIA drivers to
397 * steal our precious bytes of data...
398 */
399 ce_override = 1;
400 local_irq_save(flags);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200401 au1550_hwcontrol(mtd, NAND_CTL_SETNCE);
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400402 }
403
Thomas Gleixnercad74f22006-05-23 23:28:48 +0200404 au1550_write_byte(mtd, (u8)(page_addr >> 8));
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400405
406 /* One more address cycle for devices > 32MiB */
407 if (this->chipsize > (32 << 20))
Thomas Gleixnercad74f22006-05-23 23:28:48 +0200408 au1550_write_byte(mtd, (u8)((page_addr >> 16) & 0x0f));
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400409 }
410 /* Latch in address */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200411 au1550_hwcontrol(mtd, NAND_CTL_CLRALE);
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400412 }
413
414 /*
415 * Program and erase have their own busy handlers.
416 * Status and sequential in need no delay.
417 */
418 switch (command) {
419
420 case NAND_CMD_PAGEPROG:
421 case NAND_CMD_ERASE1:
422 case NAND_CMD_ERASE2:
423 case NAND_CMD_SEQIN:
424 case NAND_CMD_STATUS:
425 return;
426
427 case NAND_CMD_RESET:
428 break;
429
430 case NAND_CMD_READ0:
431 case NAND_CMD_READ1:
432 case NAND_CMD_READOOB:
433 /* Check if we're really driving -CE low (just in case) */
434 if (unlikely(!ce_override))
435 break;
436
437 /* Apply a short delay always to ensure that we do wait tWB. */
438 ndelay(100);
439 /* Wait for a chip to become ready... */
440 for (i = this->chip_delay; !this->dev_ready(mtd) && i > 0; --i)
441 udelay(1);
442
443 /* Release -CE and re-enable interrupts. */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200444 au1550_hwcontrol(mtd, NAND_CTL_CLRNCE);
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400445 local_irq_restore(flags);
446 return;
447 }
448 /* Apply this short delay always to ensure that we do wait tWB. */
449 ndelay(100);
450
451 while(!this->dev_ready(mtd));
452}
453
454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455/*
456 * Main initialization routine
457 */
David Woodhousecead4db2006-05-16 13:54:50 +0100458static int __init au1xxx_nand_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
460 struct nand_chip *this;
David Woodhousee0c7d762006-05-13 18:07:53 +0100461 u16 boot_swapboot = 0; /* default value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 int retval;
Pete Popovef6f0d12005-09-23 02:44:58 +0100463 u32 mem_staddr;
464 u32 nand_phys;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 /* Allocate memory for MTD device structure and private data */
David Woodhousee0c7d762006-05-13 18:07:53 +0100467 au1550_mtd = kmalloc(sizeof(struct mtd_info) + sizeof(struct nand_chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (!au1550_mtd) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100469 printk("Unable to allocate NAND MTD dev structure.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return -ENOMEM;
471 }
472
473 /* Get pointer to private data */
David Woodhousee0c7d762006-05-13 18:07:53 +0100474 this = (struct nand_chip *)(&au1550_mtd[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 /* Initialize structures */
David Woodhousee0c7d762006-05-13 18:07:53 +0100477 memset(au1550_mtd, 0, sizeof(struct mtd_info));
478 memset(this, 0, sizeof(struct nand_chip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 /* Link the private data with the MTD structure */
481 au1550_mtd->priv = this;
David Woodhouse552d9202006-05-14 01:20:46 +0100482 au1550_mtd->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000484
Sergei Shtylyov155285c2006-05-16 20:16:41 +0400485 /* MEM_STNDCTL: disable ints, disable nand boot */
486 au_writel(0, MEM_STNDCTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488#ifdef CONFIG_MIPS_PB1550
489 /* set gpio206 high */
David Woodhousee0c7d762006-05-13 18:07:53 +0100490 au_writel(au_readl(GPIO2_DIR) & ~(1 << 6), GPIO2_DIR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
David Woodhousee0c7d762006-05-13 18:07:53 +0100492 boot_swapboot = (au_readl(MEM_STSTAT) & (0x7 << 1)) | ((bcsr->status >> 6) & 0x1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 switch (boot_swapboot) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100494 case 0:
495 case 2:
496 case 8:
497 case 0xC:
498 case 0xD:
499 /* x16 NAND Flash */
500 nand_width = 0;
501 break;
502 case 1:
503 case 9:
504 case 3:
505 case 0xE:
506 case 0xF:
507 /* x8 NAND Flash */
508 nand_width = 1;
509 break;
510 default:
511 printk("Pb1550 NAND: bad boot:swap\n");
512 retval = -EINVAL;
513 goto outmem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
515#endif
516
Pete Popovef6f0d12005-09-23 02:44:58 +0100517 /* Configure chip-select; normally done by boot code, e.g. YAMON */
518#ifdef NAND_STCFG
519 if (NAND_CS == 0) {
520 au_writel(NAND_STCFG, MEM_STCFG0);
521 au_writel(NAND_STTIME, MEM_STTIME0);
522 au_writel(NAND_STADDR, MEM_STADDR0);
523 }
524 if (NAND_CS == 1) {
525 au_writel(NAND_STCFG, MEM_STCFG1);
526 au_writel(NAND_STTIME, MEM_STTIME1);
527 au_writel(NAND_STADDR, MEM_STADDR1);
528 }
529 if (NAND_CS == 2) {
530 au_writel(NAND_STCFG, MEM_STCFG2);
531 au_writel(NAND_STTIME, MEM_STTIME2);
532 au_writel(NAND_STADDR, MEM_STADDR2);
533 }
534 if (NAND_CS == 3) {
535 au_writel(NAND_STCFG, MEM_STCFG3);
536 au_writel(NAND_STTIME, MEM_STTIME3);
537 au_writel(NAND_STADDR, MEM_STADDR3);
538 }
539#endif
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000540
Pete Popovef6f0d12005-09-23 02:44:58 +0100541 /* Locate NAND chip-select in order to determine NAND phys address */
542 mem_staddr = 0x00000000;
543 if (((au_readl(MEM_STCFG0) & 0x7) == 0x5) && (NAND_CS == 0))
544 mem_staddr = au_readl(MEM_STADDR0);
545 else if (((au_readl(MEM_STCFG1) & 0x7) == 0x5) && (NAND_CS == 1))
546 mem_staddr = au_readl(MEM_STADDR1);
547 else if (((au_readl(MEM_STCFG2) & 0x7) == 0x5) && (NAND_CS == 2))
548 mem_staddr = au_readl(MEM_STADDR2);
549 else if (((au_readl(MEM_STCFG3) & 0x7) == 0x5) && (NAND_CS == 3))
550 mem_staddr = au_readl(MEM_STADDR3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Pete Popovef6f0d12005-09-23 02:44:58 +0100552 if (mem_staddr == 0x00000000) {
553 printk("Au1xxx NAND: ERROR WITH NAND CHIP-SELECT\n");
554 kfree(au1550_mtd);
555 return 1;
556 }
557 nand_phys = (mem_staddr << 4) & 0xFFFC0000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Pete Popovef6f0d12005-09-23 02:44:58 +0100559 p_nand = (void __iomem *)ioremap(nand_phys, 0x1000);
560
561 /* make controller and MTD agree */
562 if (NAND_CS == 0)
David Woodhousee0c7d762006-05-13 18:07:53 +0100563 nand_width = au_readl(MEM_STCFG0) & (1 << 22);
Pete Popovef6f0d12005-09-23 02:44:58 +0100564 if (NAND_CS == 1)
David Woodhousee0c7d762006-05-13 18:07:53 +0100565 nand_width = au_readl(MEM_STCFG1) & (1 << 22);
Pete Popovef6f0d12005-09-23 02:44:58 +0100566 if (NAND_CS == 2)
David Woodhousee0c7d762006-05-13 18:07:53 +0100567 nand_width = au_readl(MEM_STCFG2) & (1 << 22);
Pete Popovef6f0d12005-09-23 02:44:58 +0100568 if (NAND_CS == 3)
David Woodhousee0c7d762006-05-13 18:07:53 +0100569 nand_width = au_readl(MEM_STCFG3) & (1 << 22);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 /* Set address of hardware control function */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 this->dev_ready = au1550_device_ready;
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400573 this->select_chip = au1550_select_chip;
574 this->cmdfunc = au1550_command;
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 /* 30 us command delay time */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000577 this->chip_delay = 30;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +0200578 this->ecc.mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 this->options = NAND_NO_AUTOINCR;
581
582 if (!nand_width)
583 this->options |= NAND_BUSWIDTH_16;
584
585 this->read_byte = (!nand_width) ? au_read_byte16 : au_read_byte;
Thomas Gleixnercad74f22006-05-23 23:28:48 +0200586 au1550_write_byte = (!nand_width) ? au_write_byte16 : au_write_byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 this->read_word = au_read_word;
588 this->write_buf = (!nand_width) ? au_write_buf16 : au_write_buf;
589 this->read_buf = (!nand_width) ? au_read_buf16 : au_read_buf;
590 this->verify_buf = (!nand_width) ? au_verify_buf16 : au_verify_buf;
591
592 /* Scan to find existence of the device */
David Woodhousee0c7d762006-05-13 18:07:53 +0100593 if (nand_scan(au1550_mtd, 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 retval = -ENXIO;
595 goto outio;
596 }
597
598 /* Register the partitions */
Tobias Klauser87d10f32006-03-31 02:29:45 -0800599 add_mtd_partitions(au1550_mtd, partition_info, ARRAY_SIZE(partition_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 return 0;
602
603 outio:
David Woodhousee0c7d762006-05-13 18:07:53 +0100604 iounmap((void *)p_nand);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 outmem:
David Woodhousee0c7d762006-05-13 18:07:53 +0100607 kfree(au1550_mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 return retval;
609}
610
Pete Popovef6f0d12005-09-23 02:44:58 +0100611module_init(au1xxx_nand_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613/*
614 * Clean up routine
615 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100616static void __exit au1550_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
David Woodhousee0c7d762006-05-13 18:07:53 +0100618 struct nand_chip *this = (struct nand_chip *)&au1550_mtd[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 /* Release resources, unregister device */
David Woodhousee0c7d762006-05-13 18:07:53 +0100621 nand_release(au1550_mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 /* Free the MTD device structure */
David Woodhousee0c7d762006-05-13 18:07:53 +0100624 kfree(au1550_mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 /* Unmap */
David Woodhousee0c7d762006-05-13 18:07:53 +0100627 iounmap((void *)p_nand);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
David Woodhousee0c7d762006-05-13 18:07:53 +0100629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630module_exit(au1550_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632MODULE_LICENSE("GPL");
633MODULE_AUTHOR("Embedded Edge, LLC");
634MODULE_DESCRIPTION("Board-specific glue layer for NAND flash on Pb1550 board");