blob: fa5736b9286c2060bd6af631f704dd146b192566 [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
Manuel Lauss50d56762011-08-12 11:39:43 +020022#ifdef CONFIG_MIPS_PB1550
23#include <asm/mach-pb1x00/pb1550.h>
24#elif defined(CONFIG_MIPS_DB1550)
25#include <asm/mach-db1x00/db1x00.h>
26#endif
Manuel Lauss9bdcf332009-10-04 14:55:24 +020027#include <asm/mach-db1x00/bcsr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29/*
30 * MTD structure for NAND controller
31 */
32static struct mtd_info *au1550_mtd = NULL;
33static void __iomem *p_nand;
David Woodhousee0c7d762006-05-13 18:07:53 +010034static int nand_width = 1; /* default x8 */
Thomas Gleixnercad74f22006-05-23 23:28:48 +020035static void (*au1550_write_byte)(struct mtd_info *, u_char);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/*
38 * Define partitions for flash device
39 */
Jesper Juhl3c6bee12006-01-09 20:54:01 -080040static const struct mtd_partition partition_info[] = {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000041 {
David Woodhousee0c7d762006-05-13 18:07:53 +010042 .name = "NAND FS 0",
43 .offset = 0,
44 .size = 8 * 1024 * 1024},
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000045 {
David Woodhousee0c7d762006-05-13 18:07:53 +010046 .name = "NAND FS 1",
47 .offset = MTDPART_OFS_APPEND,
48 .size = MTDPART_SIZ_FULL}
Linus Torvalds1da177e2005-04-16 15:20:36 -070049};
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51/**
52 * au_read_byte - read one byte from the chip
53 * @mtd: MTD device structure
54 *
55 * read function for 8bit buswith
56 */
57static u_char au_read_byte(struct mtd_info *mtd)
58{
59 struct nand_chip *this = mtd->priv;
60 u_char ret = readb(this->IO_ADDR_R);
61 au_sync();
62 return ret;
63}
64
65/**
66 * au_write_byte - write one byte to the chip
67 * @mtd: MTD device structure
68 * @byte: pointer to data byte to write
69 *
70 * write function for 8it buswith
71 */
72static void au_write_byte(struct mtd_info *mtd, u_char byte)
73{
74 struct nand_chip *this = mtd->priv;
75 writeb(byte, this->IO_ADDR_W);
76 au_sync();
77}
78
79/**
80 * au_read_byte16 - read one byte endianess aware from the chip
81 * @mtd: MTD device structure
82 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000083 * read function for 16bit buswith with
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 * endianess conversion
85 */
86static u_char au_read_byte16(struct mtd_info *mtd)
87{
88 struct nand_chip *this = mtd->priv;
89 u_char ret = (u_char) cpu_to_le16(readw(this->IO_ADDR_R));
90 au_sync();
91 return ret;
92}
93
94/**
95 * au_write_byte16 - write one byte endianess aware to the chip
96 * @mtd: MTD device structure
97 * @byte: pointer to data byte to write
98 *
99 * write function for 16bit buswith with
100 * endianess conversion
101 */
102static void au_write_byte16(struct mtd_info *mtd, u_char byte)
103{
104 struct nand_chip *this = mtd->priv;
105 writew(le16_to_cpu((u16) byte), this->IO_ADDR_W);
106 au_sync();
107}
108
109/**
110 * au_read_word - read one word from the chip
111 * @mtd: MTD device structure
112 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000113 * read function for 16bit buswith without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 * endianess conversion
115 */
116static u16 au_read_word(struct mtd_info *mtd)
117{
118 struct nand_chip *this = mtd->priv;
119 u16 ret = readw(this->IO_ADDR_R);
120 au_sync();
121 return ret;
122}
123
124/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 * au_write_buf - write buffer to chip
126 * @mtd: MTD device structure
127 * @buf: data buffer
128 * @len: number of bytes to write
129 *
130 * write function for 8bit buswith
131 */
132static void au_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
133{
134 int i;
135 struct nand_chip *this = mtd->priv;
136
David Woodhousee0c7d762006-05-13 18:07:53 +0100137 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 writeb(buf[i], this->IO_ADDR_W);
139 au_sync();
140 }
141}
142
143/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000144 * au_read_buf - read chip data into buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 * @mtd: MTD device structure
146 * @buf: buffer to store date
147 * @len: number of bytes to read
148 *
149 * read function for 8bit buswith
150 */
151static void au_read_buf(struct mtd_info *mtd, u_char *buf, int len)
152{
153 int i;
154 struct nand_chip *this = mtd->priv;
155
David Woodhousee0c7d762006-05-13 18:07:53 +0100156 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 buf[i] = readb(this->IO_ADDR_R);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000158 au_sync();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 }
160}
161
162/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000163 * au_verify_buf - Verify chip data against buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 * @mtd: MTD device structure
165 * @buf: buffer containing the data to compare
166 * @len: number of bytes to compare
167 *
168 * verify function for 8bit buswith
169 */
170static int au_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
171{
172 int i;
173 struct nand_chip *this = mtd->priv;
174
David Woodhousee0c7d762006-05-13 18:07:53 +0100175 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 if (buf[i] != readb(this->IO_ADDR_R))
177 return -EFAULT;
178 au_sync();
179 }
180
181 return 0;
182}
183
184/**
185 * au_write_buf16 - write buffer to chip
186 * @mtd: MTD device structure
187 * @buf: data buffer
188 * @len: number of bytes to write
189 *
190 * write function for 16bit buswith
191 */
192static void au_write_buf16(struct mtd_info *mtd, const u_char *buf, int len)
193{
194 int i;
195 struct nand_chip *this = mtd->priv;
196 u16 *p = (u16 *) buf;
197 len >>= 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000198
David Woodhousee0c7d762006-05-13 18:07:53 +0100199 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 writew(p[i], this->IO_ADDR_W);
201 au_sync();
202 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
206/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000207 * au_read_buf16 - read chip data into buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 * @mtd: MTD device structure
209 * @buf: buffer to store date
210 * @len: number of bytes to read
211 *
212 * read function for 16bit buswith
213 */
214static void au_read_buf16(struct mtd_info *mtd, u_char *buf, int len)
215{
216 int i;
217 struct nand_chip *this = mtd->priv;
218 u16 *p = (u16 *) buf;
219 len >>= 1;
220
David Woodhousee0c7d762006-05-13 18:07:53 +0100221 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 p[i] = readw(this->IO_ADDR_R);
223 au_sync();
224 }
225}
226
227/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000228 * au_verify_buf16 - Verify chip data against buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 * @mtd: MTD device structure
230 * @buf: buffer containing the data to compare
231 * @len: number of bytes to compare
232 *
233 * verify function for 16bit buswith
234 */
235static int au_verify_buf16(struct mtd_info *mtd, const u_char *buf, int len)
236{
237 int i;
238 struct nand_chip *this = mtd->priv;
239 u16 *p = (u16 *) buf;
240 len >>= 1;
241
David Woodhousee0c7d762006-05-13 18:07:53 +0100242 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 if (p[i] != readw(this->IO_ADDR_R))
244 return -EFAULT;
245 au_sync();
246 }
247 return 0;
248}
249
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200250/* Select the chip by setting nCE to low */
251#define NAND_CTL_SETNCE 1
252/* Deselect the chip by setting nCE to high */
253#define NAND_CTL_CLRNCE 2
254/* Select the command latch by setting CLE to high */
255#define NAND_CTL_SETCLE 3
256/* Deselect the command latch by setting CLE to low */
257#define NAND_CTL_CLRCLE 4
258/* Select the address latch by setting ALE to high */
259#define NAND_CTL_SETALE 5
260/* Deselect the address latch by setting ALE to low */
261#define NAND_CTL_CLRALE 6
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263static void au1550_hwcontrol(struct mtd_info *mtd, int cmd)
264{
265 register struct nand_chip *this = mtd->priv;
266
David Woodhousee0c7d762006-05-13 18:07:53 +0100267 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
David Woodhousee0c7d762006-05-13 18:07:53 +0100269 case NAND_CTL_SETCLE:
270 this->IO_ADDR_W = p_nand + MEM_STNAND_CMD;
271 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
David Woodhousee0c7d762006-05-13 18:07:53 +0100273 case NAND_CTL_CLRCLE:
274 this->IO_ADDR_W = p_nand + MEM_STNAND_DATA;
275 break;
276
277 case NAND_CTL_SETALE:
278 this->IO_ADDR_W = p_nand + MEM_STNAND_ADDR;
279 break;
280
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000281 case NAND_CTL_CLRALE:
282 this->IO_ADDR_W = p_nand + MEM_STNAND_DATA;
David Woodhousee0c7d762006-05-13 18:07:53 +0100283 /* FIXME: Nobody knows why this is necessary,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 * but it works only that way */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000285 udelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 break;
287
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000288 case NAND_CTL_SETNCE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 /* assert (force assert) chip enable */
David Woodhousee0c7d762006-05-13 18:07:53 +0100290 au_writel((1 << (4 + NAND_CS)), MEM_STNDCTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 break;
292
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000293 case NAND_CTL_CLRNCE:
David Woodhousee0c7d762006-05-13 18:07:53 +0100294 /* deassert chip enable */
295 au_writel(0, MEM_STNDCTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 break;
297 }
298
299 this->IO_ADDR_R = this->IO_ADDR_W;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 /* Drain the writebuffer */
302 au_sync();
303}
304
305int au1550_device_ready(struct mtd_info *mtd)
306{
307 int ret = (au_readl(MEM_STSTAT) & 0x1) ? 1 : 0;
308 au_sync();
309 return ret;
310}
311
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400312/**
313 * au1550_select_chip - control -CE line
314 * Forbid driving -CE manually permitting the NAND controller to do this.
315 * Keeping -CE asserted during the whole sector reads interferes with the
316 * NOR flash and PCMCIA drivers as it causes contention on the static bus.
317 * We only have to hold -CE low for the NAND read commands since the flash
318 * chip needs it to be asserted during chip not ready time but the NAND
319 * controller keeps it released.
320 *
321 * @mtd: MTD device structure
322 * @chip: chipnumber to select, -1 for deselect
323 */
324static void au1550_select_chip(struct mtd_info *mtd, int chip)
325{
326}
327
328/**
329 * au1550_command - Send command to NAND device
330 * @mtd: MTD device structure
331 * @command: the command to be sent
332 * @column: the column address for this command, -1 if none
333 * @page_addr: the page address for this command, -1 if none
334 */
335static void au1550_command(struct mtd_info *mtd, unsigned command, int column, int page_addr)
336{
337 register struct nand_chip *this = mtd->priv;
338 int ce_override = 0, i;
339 ulong flags;
340
341 /* Begin command latch cycle */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200342 au1550_hwcontrol(mtd, NAND_CTL_SETCLE);
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400343 /*
344 * Write out the command to the device.
345 */
346 if (command == NAND_CMD_SEQIN) {
347 int readcmd;
348
Joern Engel28318772006-05-22 23:18:05 +0200349 if (column >= mtd->writesize) {
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400350 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200351 column -= mtd->writesize;
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400352 readcmd = NAND_CMD_READOOB;
353 } else if (column < 256) {
354 /* First 256 bytes --> READ0 */
355 readcmd = NAND_CMD_READ0;
356 } else {
357 column -= 256;
358 readcmd = NAND_CMD_READ1;
359 }
Thomas Gleixnercad74f22006-05-23 23:28:48 +0200360 au1550_write_byte(mtd, readcmd);
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400361 }
Thomas Gleixnercad74f22006-05-23 23:28:48 +0200362 au1550_write_byte(mtd, command);
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400363
364 /* Set ALE and clear CLE to start address cycle */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200365 au1550_hwcontrol(mtd, NAND_CTL_CLRCLE);
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400366
367 if (column != -1 || page_addr != -1) {
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200368 au1550_hwcontrol(mtd, NAND_CTL_SETALE);
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400369
370 /* Serially input address */
371 if (column != -1) {
372 /* Adjust columns for 16 bit buswidth */
373 if (this->options & NAND_BUSWIDTH_16)
374 column >>= 1;
Thomas Gleixnercad74f22006-05-23 23:28:48 +0200375 au1550_write_byte(mtd, column);
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400376 }
377 if (page_addr != -1) {
Thomas Gleixnercad74f22006-05-23 23:28:48 +0200378 au1550_write_byte(mtd, (u8)(page_addr & 0xff));
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400379
380 if (command == NAND_CMD_READ0 ||
381 command == NAND_CMD_READ1 ||
382 command == NAND_CMD_READOOB) {
383 /*
384 * NAND controller will release -CE after
385 * the last address byte is written, so we'll
386 * have to forcibly assert it. No interrupts
387 * are allowed while we do this as we don't
388 * want the NOR flash or PCMCIA drivers to
389 * steal our precious bytes of data...
390 */
391 ce_override = 1;
392 local_irq_save(flags);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200393 au1550_hwcontrol(mtd, NAND_CTL_SETNCE);
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400394 }
395
Thomas Gleixnercad74f22006-05-23 23:28:48 +0200396 au1550_write_byte(mtd, (u8)(page_addr >> 8));
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400397
398 /* One more address cycle for devices > 32MiB */
399 if (this->chipsize > (32 << 20))
Thomas Gleixnercad74f22006-05-23 23:28:48 +0200400 au1550_write_byte(mtd, (u8)((page_addr >> 16) & 0x0f));
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400401 }
402 /* Latch in address */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200403 au1550_hwcontrol(mtd, NAND_CTL_CLRALE);
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400404 }
405
406 /*
407 * Program and erase have their own busy handlers.
408 * Status and sequential in need no delay.
409 */
410 switch (command) {
411
412 case NAND_CMD_PAGEPROG:
413 case NAND_CMD_ERASE1:
414 case NAND_CMD_ERASE2:
415 case NAND_CMD_SEQIN:
416 case NAND_CMD_STATUS:
417 return;
418
419 case NAND_CMD_RESET:
420 break;
421
422 case NAND_CMD_READ0:
423 case NAND_CMD_READ1:
424 case NAND_CMD_READOOB:
425 /* Check if we're really driving -CE low (just in case) */
426 if (unlikely(!ce_override))
427 break;
428
429 /* Apply a short delay always to ensure that we do wait tWB. */
430 ndelay(100);
431 /* Wait for a chip to become ready... */
432 for (i = this->chip_delay; !this->dev_ready(mtd) && i > 0; --i)
433 udelay(1);
434
435 /* Release -CE and re-enable interrupts. */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200436 au1550_hwcontrol(mtd, NAND_CTL_CLRNCE);
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400437 local_irq_restore(flags);
438 return;
439 }
440 /* Apply this short delay always to ensure that we do wait tWB. */
441 ndelay(100);
442
443 while(!this->dev_ready(mtd));
444}
445
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447/*
448 * Main initialization routine
449 */
David Woodhousecead4db2006-05-16 13:54:50 +0100450static int __init au1xxx_nand_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 struct nand_chip *this;
David Woodhousee0c7d762006-05-13 18:07:53 +0100453 u16 boot_swapboot = 0; /* default value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 int retval;
Pete Popovef6f0d12005-09-23 02:44:58 +0100455 u32 mem_staddr;
456 u32 nand_phys;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 /* Allocate memory for MTD device structure and private data */
H Hartley Sweeten34970a72009-12-14 16:04:07 -0500459 au1550_mtd = kzalloc(sizeof(struct mtd_info) + sizeof(struct nand_chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (!au1550_mtd) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100461 printk("Unable to allocate NAND MTD dev structure.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return -ENOMEM;
463 }
464
465 /* Get pointer to private data */
David Woodhousee0c7d762006-05-13 18:07:53 +0100466 this = (struct nand_chip *)(&au1550_mtd[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 /* Link the private data with the MTD structure */
469 au1550_mtd->priv = this;
David Woodhouse552d9202006-05-14 01:20:46 +0100470 au1550_mtd->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000472
Sergei Shtylyov155285c2006-05-16 20:16:41 +0400473 /* MEM_STNDCTL: disable ints, disable nand boot */
474 au_writel(0, MEM_STNDCTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476#ifdef CONFIG_MIPS_PB1550
477 /* set gpio206 high */
Manuel Laussb7f720d2011-05-08 10:42:20 +0200478 gpio_direction_input(206);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Manuel Lauss9bdcf332009-10-04 14:55:24 +0200480 boot_swapboot = (au_readl(MEM_STSTAT) & (0x7 << 1)) | ((bcsr_read(BCSR_STATUS) >> 6) & 0x1);
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 switch (boot_swapboot) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100483 case 0:
484 case 2:
485 case 8:
486 case 0xC:
487 case 0xD:
488 /* x16 NAND Flash */
489 nand_width = 0;
490 break;
491 case 1:
492 case 9:
493 case 3:
494 case 0xE:
495 case 0xF:
496 /* x8 NAND Flash */
497 nand_width = 1;
498 break;
499 default:
500 printk("Pb1550 NAND: bad boot:swap\n");
501 retval = -EINVAL;
502 goto outmem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
504#endif
505
Pete Popovef6f0d12005-09-23 02:44:58 +0100506 /* Configure chip-select; normally done by boot code, e.g. YAMON */
507#ifdef NAND_STCFG
508 if (NAND_CS == 0) {
509 au_writel(NAND_STCFG, MEM_STCFG0);
510 au_writel(NAND_STTIME, MEM_STTIME0);
511 au_writel(NAND_STADDR, MEM_STADDR0);
512 }
513 if (NAND_CS == 1) {
514 au_writel(NAND_STCFG, MEM_STCFG1);
515 au_writel(NAND_STTIME, MEM_STTIME1);
516 au_writel(NAND_STADDR, MEM_STADDR1);
517 }
518 if (NAND_CS == 2) {
519 au_writel(NAND_STCFG, MEM_STCFG2);
520 au_writel(NAND_STTIME, MEM_STTIME2);
521 au_writel(NAND_STADDR, MEM_STADDR2);
522 }
523 if (NAND_CS == 3) {
524 au_writel(NAND_STCFG, MEM_STCFG3);
525 au_writel(NAND_STTIME, MEM_STTIME3);
526 au_writel(NAND_STADDR, MEM_STADDR3);
527 }
528#endif
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000529
Pete Popovef6f0d12005-09-23 02:44:58 +0100530 /* Locate NAND chip-select in order to determine NAND phys address */
531 mem_staddr = 0x00000000;
532 if (((au_readl(MEM_STCFG0) & 0x7) == 0x5) && (NAND_CS == 0))
533 mem_staddr = au_readl(MEM_STADDR0);
534 else if (((au_readl(MEM_STCFG1) & 0x7) == 0x5) && (NAND_CS == 1))
535 mem_staddr = au_readl(MEM_STADDR1);
536 else if (((au_readl(MEM_STCFG2) & 0x7) == 0x5) && (NAND_CS == 2))
537 mem_staddr = au_readl(MEM_STADDR2);
538 else if (((au_readl(MEM_STCFG3) & 0x7) == 0x5) && (NAND_CS == 3))
539 mem_staddr = au_readl(MEM_STADDR3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Pete Popovef6f0d12005-09-23 02:44:58 +0100541 if (mem_staddr == 0x00000000) {
542 printk("Au1xxx NAND: ERROR WITH NAND CHIP-SELECT\n");
543 kfree(au1550_mtd);
544 return 1;
545 }
546 nand_phys = (mem_staddr << 4) & 0xFFFC0000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
H Hartley Sweeten440d4f92009-12-14 16:08:37 -0500548 p_nand = ioremap(nand_phys, 0x1000);
Pete Popovef6f0d12005-09-23 02:44:58 +0100549
550 /* make controller and MTD agree */
551 if (NAND_CS == 0)
David Woodhousee0c7d762006-05-13 18:07:53 +0100552 nand_width = au_readl(MEM_STCFG0) & (1 << 22);
Pete Popovef6f0d12005-09-23 02:44:58 +0100553 if (NAND_CS == 1)
David Woodhousee0c7d762006-05-13 18:07:53 +0100554 nand_width = au_readl(MEM_STCFG1) & (1 << 22);
Pete Popovef6f0d12005-09-23 02:44:58 +0100555 if (NAND_CS == 2)
David Woodhousee0c7d762006-05-13 18:07:53 +0100556 nand_width = au_readl(MEM_STCFG2) & (1 << 22);
Pete Popovef6f0d12005-09-23 02:44:58 +0100557 if (NAND_CS == 3)
David Woodhousee0c7d762006-05-13 18:07:53 +0100558 nand_width = au_readl(MEM_STCFG3) & (1 << 22);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 /* Set address of hardware control function */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 this->dev_ready = au1550_device_ready;
Sergei Shtylyov35af68b2006-05-16 20:52:06 +0400562 this->select_chip = au1550_select_chip;
563 this->cmdfunc = au1550_command;
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 /* 30 us command delay time */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000566 this->chip_delay = 30;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +0200567 this->ecc.mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 this->options = NAND_NO_AUTOINCR;
570
571 if (!nand_width)
572 this->options |= NAND_BUSWIDTH_16;
573
574 this->read_byte = (!nand_width) ? au_read_byte16 : au_read_byte;
Thomas Gleixnercad74f22006-05-23 23:28:48 +0200575 au1550_write_byte = (!nand_width) ? au_write_byte16 : au_write_byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 this->read_word = au_read_word;
577 this->write_buf = (!nand_width) ? au_write_buf16 : au_write_buf;
578 this->read_buf = (!nand_width) ? au_read_buf16 : au_read_buf;
579 this->verify_buf = (!nand_width) ? au_verify_buf16 : au_verify_buf;
580
581 /* Scan to find existence of the device */
David Woodhousee0c7d762006-05-13 18:07:53 +0100582 if (nand_scan(au1550_mtd, 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 retval = -ENXIO;
584 goto outio;
585 }
586
587 /* Register the partitions */
Jamie Ilesee0e87b2011-05-23 10:23:40 +0100588 mtd_device_register(au1550_mtd, partition_info,
589 ARRAY_SIZE(partition_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 return 0;
592
593 outio:
H Hartley Sweeten440d4f92009-12-14 16:08:37 -0500594 iounmap(p_nand);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 outmem:
David Woodhousee0c7d762006-05-13 18:07:53 +0100597 kfree(au1550_mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return retval;
599}
600
Pete Popovef6f0d12005-09-23 02:44:58 +0100601module_init(au1xxx_nand_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603/*
604 * Clean up routine
605 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100606static void __exit au1550_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 /* Release resources, unregister device */
David Woodhousee0c7d762006-05-13 18:07:53 +0100609 nand_release(au1550_mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 /* Free the MTD device structure */
David Woodhousee0c7d762006-05-13 18:07:53 +0100612 kfree(au1550_mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 /* Unmap */
H Hartley Sweeten440d4f92009-12-14 16:08:37 -0500615 iounmap(p_nand);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
David Woodhousee0c7d762006-05-13 18:07:53 +0100617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618module_exit(au1550_cleanup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620MODULE_LICENSE("GPL");
621MODULE_AUTHOR("Embedded Edge, LLC");
622MODULE_DESCRIPTION("Board-specific glue layer for NAND flash on Pb1550 board");