blob: e7767eef4505665279142ba57e13a43e6ac4b35b [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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 */
11
12#include <linux/slab.h>
Manuel Laussb7f720d2011-05-08 10:42:20 +020013#include <linux/gpio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/init.h>
15#include <linux/module.h>
Sergei Shtylyov35af68b2006-05-16 20:52:06 +040016#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/mtd/mtd.h>
18#include <linux/mtd/nand.h>
19#include <linux/mtd/partitions.h>
20#include <asm/io.h>
21
Pete Popovef6f0d12005-09-23 02:44:58 +010022#include <asm/mach-au1x00/au1xxx.h>
Manuel Lauss9bdcf332009-10-04 14:55:24 +020023#include <asm/mach-db1x00/bcsr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25/*
26 * MTD structure for NAND controller
27 */
28static struct mtd_info *au1550_mtd = NULL;
29static void __iomem *p_nand;
David Woodhousee0c7d762006-05-13 18:07:53 +010030static int nand_width = 1; /* default x8 */
Thomas Gleixnercad74f22006-05-23 23:28:48 +020031static void (*au1550_write_byte)(struct mtd_info *, u_char);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/*
34 * Define partitions for flash device
35 */
Jesper Juhl3c6bee12006-01-09 20:54:01 -080036static const struct mtd_partition partition_info[] = {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000037 {
David Woodhousee0c7d762006-05-13 18:07:53 +010038 .name = "NAND FS 0",
39 .offset = 0,
40 .size = 8 * 1024 * 1024},
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000041 {
David Woodhousee0c7d762006-05-13 18:07:53 +010042 .name = "NAND FS 1",
43 .offset = MTDPART_OFS_APPEND,
44 .size = MTDPART_SIZ_FULL}
Linus Torvalds1da177e2005-04-16 15:20:36 -070045};
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47/**
48 * au_read_byte - read one byte from the chip
49 * @mtd: MTD device structure
50 *
51 * read function for 8bit buswith
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 * au_write_byte - write one byte to the chip
63 * @mtd: MTD device structure
64 * @byte: pointer to data byte to write
65 *
66 * write function for 8it buswith
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 * au_read_byte16 - read one byte endianess aware from the chip
77 * @mtd: MTD device structure
78 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000079 * read function for 16bit buswith with
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 * endianess conversion
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 * au_write_byte16 - write one byte endianess aware to the chip
92 * @mtd: MTD device structure
93 * @byte: pointer to data byte to write
94 *
95 * write function for 16bit buswith with
96 * endianess conversion
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 * au_read_word - read one word from the chip
107 * @mtd: MTD device structure
108 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000109 * read function for 16bit buswith without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 * endianess conversion
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/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 * au_write_buf - write buffer to chip
122 * @mtd: MTD device structure
123 * @buf: data buffer
124 * @len: number of bytes to write
125 *
126 * write function for 8bit buswith
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
David Woodhousee0c7d762006-05-13 18:07:53 +0100133 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 writeb(buf[i], this->IO_ADDR_W);
135 au_sync();
136 }
137}
138
139/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000140 * au_read_buf - read chip data into buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 * @mtd: MTD device structure
142 * @buf: buffer to store date
143 * @len: number of bytes to read
144 *
145 * read function for 8bit buswith
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
David Woodhousee0c7d762006-05-13 18:07:53 +0100152 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 buf[i] = readb(this->IO_ADDR_R);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000154 au_sync();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 }
156}
157
158/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000159 * au_verify_buf - Verify chip data against buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 * @mtd: MTD device structure
161 * @buf: buffer containing the data to compare
162 * @len: number of bytes to compare
163 *
164 * verify function for 8bit buswith
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
David Woodhousee0c7d762006-05-13 18:07:53 +0100171 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (buf[i] != readb(this->IO_ADDR_R))
173 return -EFAULT;
174 au_sync();
175 }
176
177 return 0;
178}
179
180/**
181 * au_write_buf16 - write buffer to chip
182 * @mtd: MTD device structure
183 * @buf: data buffer
184 * @len: number of bytes to write
185 *
186 * write function for 16bit buswith
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;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000194
David Woodhousee0c7d762006-05-13 18:07:53 +0100195 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 writew(p[i], this->IO_ADDR_W);
197 au_sync();
198 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
202/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000203 * au_read_buf16 - read chip data into buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 * @mtd: MTD device structure
205 * @buf: buffer to store date
206 * @len: number of bytes to read
207 *
208 * read function for 16bit buswith
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
David Woodhousee0c7d762006-05-13 18:07:53 +0100217 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 p[i] = readw(this->IO_ADDR_R);
219 au_sync();
220 }
221}
222
223/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000224 * au_verify_buf16 - Verify chip data against buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 * @mtd: MTD device structure
226 * @buf: buffer containing the data to compare
227 * @len: number of bytes to compare
228 *
229 * verify function for 16bit buswith
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
David Woodhousee0c7d762006-05-13 18:07:53 +0100238 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 if (p[i] != readw(this->IO_ADDR_R))
240 return -EFAULT;
241 au_sync();
242 }
243 return 0;
244}
245
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200246/* Select the chip by setting nCE to low */
247#define NAND_CTL_SETNCE 1
248/* Deselect the chip by setting nCE to high */
249#define NAND_CTL_CLRNCE 2
250/* Select the command latch by setting CLE to high */
251#define NAND_CTL_SETCLE 3
252/* Deselect the command latch by setting CLE to low */
253#define NAND_CTL_CLRCLE 4
254/* Select the address latch by setting ALE to high */
255#define NAND_CTL_SETALE 5
256/* Deselect the address latch by setting ALE to low */
257#define NAND_CTL_CLRALE 6
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259static void au1550_hwcontrol(struct mtd_info *mtd, int cmd)
260{
261 register struct nand_chip *this = mtd->priv;
262
David Woodhousee0c7d762006-05-13 18:07:53 +0100263 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
David Woodhousee0c7d762006-05-13 18:07:53 +0100265 case NAND_CTL_SETCLE:
266 this->IO_ADDR_W = p_nand + MEM_STNAND_CMD;
267 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
David Woodhousee0c7d762006-05-13 18:07:53 +0100269 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
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000277 case NAND_CTL_CLRALE:
278 this->IO_ADDR_W = p_nand + MEM_STNAND_DATA;
David Woodhousee0c7d762006-05-13 18:07:53 +0100279 /* FIXME: Nobody knows why this is necessary,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 * but it works only that way */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000281 udelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 break;
283
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000284 case NAND_CTL_SETNCE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 /* assert (force assert) chip enable */
David Woodhousee0c7d762006-05-13 18:07:53 +0100286 au_writel((1 << (4 + NAND_CS)), MEM_STNDCTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 break;
288
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000289 case NAND_CTL_CLRNCE:
David Woodhousee0c7d762006-05-13 18:07:53 +0100290 /* deassert chip enable */
291 au_writel(0, MEM_STNDCTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 break;
293 }
294
295 this->IO_ADDR_R = this->IO_ADDR_W;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 /* Drain the writebuffer */
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
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400308/**
309 * au1550_select_chip - control -CE line
310 * Forbid driving -CE manually permitting the NAND controller to do this.
311 * Keeping -CE asserted during the whole sector reads interferes with the
312 * NOR flash and PCMCIA drivers as it causes contention on the static bus.
313 * We only have to hold -CE low for the NAND read commands since the flash
314 * chip needs it to be asserted during chip not ready time but the NAND
315 * controller keeps it released.
316 *
317 * @mtd: MTD device structure
318 * @chip: chipnumber to select, -1 for deselect
319 */
320static void au1550_select_chip(struct mtd_info *mtd, int chip)
321{
322}
323
324/**
325 * au1550_command - Send command to NAND device
326 * @mtd: MTD device structure
327 * @command: the command to be sent
328 * @column: the column address for this command, -1 if none
329 * @page_addr: the page address for this command, -1 if none
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 /* Begin command latch cycle */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200338 au1550_hwcontrol(mtd, NAND_CTL_SETCLE);
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400339 /*
340 * Write out the command to the device.
341 */
342 if (command == NAND_CMD_SEQIN) {
343 int readcmd;
344
Joern Engel28318772006-05-22 23:18:05 +0200345 if (column >= mtd->writesize) {
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400346 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200347 column -= mtd->writesize;
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400348 readcmd = NAND_CMD_READOOB;
349 } else if (column < 256) {
350 /* First 256 bytes --> READ0 */
351 readcmd = NAND_CMD_READ0;
352 } else {
353 column -= 256;
354 readcmd = NAND_CMD_READ1;
355 }
Thomas Gleixnercad74f22006-05-23 23:28:48 +0200356 au1550_write_byte(mtd, readcmd);
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400357 }
Thomas Gleixnercad74f22006-05-23 23:28:48 +0200358 au1550_write_byte(mtd, command);
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400359
360 /* Set ALE and clear CLE to start address cycle */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200361 au1550_hwcontrol(mtd, NAND_CTL_CLRCLE);
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400362
363 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200364 au1550_hwcontrol(mtd, NAND_CTL_SETALE);
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400365
366 /* Serially input address */
367 if (column != -1) {
368 /* Adjust columns for 16 bit buswidth */
369 if (this->options & NAND_BUSWIDTH_16)
370 column >>= 1;
Thomas Gleixnercad74f22006-05-23 23:28:48 +0200371 au1550_write_byte(mtd, column);
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400372 }
373 if (page_addr != -1) {
Thomas Gleixnercad74f22006-05-23 23:28:48 +0200374 au1550_write_byte(mtd, (u8)(page_addr & 0xff));
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400375
376 if (command == NAND_CMD_READ0 ||
377 command == NAND_CMD_READ1 ||
378 command == NAND_CMD_READOOB) {
379 /*
380 * NAND controller will release -CE after
381 * the last address byte is written, so we'll
382 * have to forcibly assert it. No interrupts
383 * are allowed while we do this as we don't
384 * want the NOR flash or PCMCIA drivers to
385 * steal our precious bytes of data...
386 */
387 ce_override = 1;
388 local_irq_save(flags);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200389 au1550_hwcontrol(mtd, NAND_CTL_SETNCE);
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400390 }
391
Thomas Gleixnercad74f22006-05-23 23:28:48 +0200392 au1550_write_byte(mtd, (u8)(page_addr >> 8));
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400393
394 /* One more address cycle for devices > 32MiB */
395 if (this->chipsize > (32 << 20))
Thomas Gleixnercad74f22006-05-23 23:28:48 +0200396 au1550_write_byte(mtd, (u8)((page_addr >> 16) & 0x0f));
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400397 }
398 /* Latch in address */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200399 au1550_hwcontrol(mtd, NAND_CTL_CLRALE);
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400400 }
401
402 /*
403 * Program and erase have their own busy handlers.
404 * Status and sequential in need no delay.
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 /* Check if we're really driving -CE low (just in case) */
422 if (unlikely(!ce_override))
423 break;
424
425 /* Apply a short delay always to ensure that we do wait tWB. */
426 ndelay(100);
427 /* Wait for a chip to become ready... */
428 for (i = this->chip_delay; !this->dev_ready(mtd) && i > 0; --i)
429 udelay(1);
430
431 /* Release -CE and re-enable interrupts. */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200432 au1550_hwcontrol(mtd, NAND_CTL_CLRNCE);
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400433 local_irq_restore(flags);
434 return;
435 }
436 /* Apply this short delay always to ensure that we do wait tWB. */
437 ndelay(100);
438
439 while(!this->dev_ready(mtd));
440}
441
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443/*
444 * Main initialization routine
445 */
David Woodhousecead4db2006-05-16 13:54:50 +0100446static int __init au1xxx_nand_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
448 struct nand_chip *this;
David Woodhousee0c7d762006-05-13 18:07:53 +0100449 u16 boot_swapboot = 0; /* default value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 int retval;
Pete Popovef6f0d12005-09-23 02:44:58 +0100451 u32 mem_staddr;
452 u32 nand_phys;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 /* Allocate memory for MTD device structure and private data */
H Hartley Sweeten34970a72009-12-14 16:04:07 -0500455 au1550_mtd = kzalloc(sizeof(struct mtd_info) + sizeof(struct nand_chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 if (!au1550_mtd) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100457 printk("Unable to allocate NAND MTD dev structure.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return -ENOMEM;
459 }
460
461 /* Get pointer to private data */
David Woodhousee0c7d762006-05-13 18:07:53 +0100462 this = (struct nand_chip *)(&au1550_mtd[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 /* Link the private data with the MTD structure */
465 au1550_mtd->priv = this;
David Woodhouse552d9202006-05-14 01:20:46 +0100466 au1550_mtd->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000468
Sergei Shtylyov155285c2006-05-16 20:16:41 +0400469 /* MEM_STNDCTL: disable ints, disable nand boot */
470 au_writel(0, MEM_STNDCTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472#ifdef CONFIG_MIPS_PB1550
473 /* set gpio206 high */
Manuel Laussb7f720d2011-05-08 10:42:20 +0200474 gpio_direction_input(206);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Manuel Lauss9bdcf332009-10-04 14:55:24 +0200476 boot_swapboot = (au_readl(MEM_STSTAT) & (0x7 << 1)) | ((bcsr_read(BCSR_STATUS) >> 6) & 0x1);
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 switch (boot_swapboot) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100479 case 0:
480 case 2:
481 case 8:
482 case 0xC:
483 case 0xD:
484 /* x16 NAND Flash */
485 nand_width = 0;
486 break;
487 case 1:
488 case 9:
489 case 3:
490 case 0xE:
491 case 0xF:
492 /* x8 NAND Flash */
493 nand_width = 1;
494 break;
495 default:
496 printk("Pb1550 NAND: bad boot:swap\n");
497 retval = -EINVAL;
498 goto outmem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
500#endif
501
Pete Popovef6f0d12005-09-23 02:44:58 +0100502 /* Configure chip-select; normally done by boot code, e.g. YAMON */
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
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000525
Pete Popovef6f0d12005-09-23 02:44:58 +0100526 /* Locate NAND chip-select in order to determine NAND phys address */
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Pete Popovef6f0d12005-09-23 02:44:58 +0100537 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
H Hartley Sweeten440d4f92009-12-14 16:08:37 -0500544 p_nand = ioremap(nand_phys, 0x1000);
Pete Popovef6f0d12005-09-23 02:44:58 +0100545
546 /* make controller and MTD agree */
547 if (NAND_CS == 0)
David Woodhousee0c7d762006-05-13 18:07:53 +0100548 nand_width = au_readl(MEM_STCFG0) & (1 << 22);
Pete Popovef6f0d12005-09-23 02:44:58 +0100549 if (NAND_CS == 1)
David Woodhousee0c7d762006-05-13 18:07:53 +0100550 nand_width = au_readl(MEM_STCFG1) & (1 << 22);
Pete Popovef6f0d12005-09-23 02:44:58 +0100551 if (NAND_CS == 2)
David Woodhousee0c7d762006-05-13 18:07:53 +0100552 nand_width = au_readl(MEM_STCFG2) & (1 << 22);
Pete Popovef6f0d12005-09-23 02:44:58 +0100553 if (NAND_CS == 3)
David Woodhousee0c7d762006-05-13 18:07:53 +0100554 nand_width = au_readl(MEM_STCFG3) & (1 << 22);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 /* Set address of hardware control function */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 this->dev_ready = au1550_device_ready;
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400558 this->select_chip = au1550_select_chip;
559 this->cmdfunc = au1550_command;
560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 /* 30 us command delay time */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000562 this->chip_delay = 30;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +0200563 this->ecc.mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
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;
Thomas Gleixnercad74f22006-05-23 23:28:48 +0200571 au1550_write_byte = (!nand_width) ? au_write_byte16 : au_write_byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 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 /* Scan to find existence of the device */
David Woodhousee0c7d762006-05-13 18:07:53 +0100578 if (nand_scan(au1550_mtd, 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 retval = -ENXIO;
580 goto outio;
581 }
582
583 /* Register the partitions */
Jamie Ilesee0e87b2011-05-23 10:23:40 +0100584 mtd_device_register(au1550_mtd, partition_info,
585 ARRAY_SIZE(partition_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 return 0;
588
589 outio:
H Hartley Sweeten440d4f92009-12-14 16:08:37 -0500590 iounmap(p_nand);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 outmem:
David Woodhousee0c7d762006-05-13 18:07:53 +0100593 kfree(au1550_mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return retval;
595}
596
Pete Popovef6f0d12005-09-23 02:44:58 +0100597module_init(au1xxx_nand_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599/*
600 * Clean up routine
601 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100602static void __exit au1550_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 /* Release resources, unregister device */
David Woodhousee0c7d762006-05-13 18:07:53 +0100605 nand_release(au1550_mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 /* Free the MTD device structure */
David Woodhousee0c7d762006-05-13 18:07:53 +0100608 kfree(au1550_mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 /* Unmap */
H Hartley Sweeten440d4f92009-12-14 16:08:37 -0500611 iounmap(p_nand);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
David Woodhousee0c7d762006-05-13 18:07:53 +0100613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614module_exit(au1550_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616MODULE_LICENSE("GPL");
617MODULE_AUTHOR("Embedded Edge, LLC");
618MODULE_DESCRIPTION("Board-specific glue layer for NAND flash on Pb1550 board");