2 * drivers/mtd/nand/au1550nd.c
4 * Copyright (C) 2004 Embedded Edge, LLC
6 * 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.
12 #include <linux/slab.h>
13 #include <linux/gpio.h>
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/interrupt.h>
17 #include <linux/mtd/mtd.h>
18 #include <linux/mtd/nand.h>
19 #include <linux/mtd/partitions.h>
20 #include <linux/platform_device.h>
22 #include <asm/mach-au1x00/au1000.h>
23 #include <asm/mach-au1x00/au1550nd.h>
28 struct nand_chip chip
;
32 void (*write_byte
)(struct mtd_info
*, u_char
);
36 * au_read_byte - read one byte from the chip
37 * @mtd: MTD device structure
39 * read function for 8bit buswidth
41 static u_char
au_read_byte(struct mtd_info
*mtd
)
43 struct nand_chip
*this = mtd
->priv
;
44 u_char ret
= readb(this->IO_ADDR_R
);
50 * au_write_byte - write one byte to the chip
51 * @mtd: MTD device structure
52 * @byte: pointer to data byte to write
54 * write function for 8it buswidth
56 static void au_write_byte(struct mtd_info
*mtd
, u_char byte
)
58 struct nand_chip
*this = mtd
->priv
;
59 writeb(byte
, this->IO_ADDR_W
);
64 * au_read_byte16 - read one byte endianness aware from the chip
65 * @mtd: MTD device structure
67 * read function for 16bit buswidth with endianness conversion
69 static u_char
au_read_byte16(struct mtd_info
*mtd
)
71 struct nand_chip
*this = mtd
->priv
;
72 u_char ret
= (u_char
) cpu_to_le16(readw(this->IO_ADDR_R
));
78 * au_write_byte16 - write one byte endianness aware to the chip
79 * @mtd: MTD device structure
80 * @byte: pointer to data byte to write
82 * write function for 16bit buswidth with endianness conversion
84 static void au_write_byte16(struct mtd_info
*mtd
, u_char byte
)
86 struct nand_chip
*this = mtd
->priv
;
87 writew(le16_to_cpu((u16
) byte
), this->IO_ADDR_W
);
92 * au_read_word - read one word from the chip
93 * @mtd: MTD device structure
95 * read function for 16bit buswidth without endianness conversion
97 static u16
au_read_word(struct mtd_info
*mtd
)
99 struct nand_chip
*this = mtd
->priv
;
100 u16 ret
= readw(this->IO_ADDR_R
);
106 * au_write_buf - write buffer to chip
107 * @mtd: MTD device structure
109 * @len: number of bytes to write
111 * write function for 8bit buswidth
113 static void au_write_buf(struct mtd_info
*mtd
, const u_char
*buf
, int len
)
116 struct nand_chip
*this = mtd
->priv
;
118 for (i
= 0; i
< len
; i
++) {
119 writeb(buf
[i
], this->IO_ADDR_W
);
125 * au_read_buf - read chip data into buffer
126 * @mtd: MTD device structure
127 * @buf: buffer to store date
128 * @len: number of bytes to read
130 * read function for 8bit buswidth
132 static void au_read_buf(struct mtd_info
*mtd
, u_char
*buf
, int len
)
135 struct nand_chip
*this = mtd
->priv
;
137 for (i
= 0; i
< len
; i
++) {
138 buf
[i
] = readb(this->IO_ADDR_R
);
144 * au_verify_buf - Verify chip data against buffer
145 * @mtd: MTD device structure
146 * @buf: buffer containing the data to compare
147 * @len: number of bytes to compare
149 * verify function for 8bit buswidth
151 static int au_verify_buf(struct mtd_info
*mtd
, const u_char
*buf
, int len
)
154 struct nand_chip
*this = mtd
->priv
;
156 for (i
= 0; i
< len
; i
++) {
157 if (buf
[i
] != readb(this->IO_ADDR_R
))
166 * au_write_buf16 - write buffer to chip
167 * @mtd: MTD device structure
169 * @len: number of bytes to write
171 * write function for 16bit buswidth
173 static void au_write_buf16(struct mtd_info
*mtd
, const u_char
*buf
, int len
)
176 struct nand_chip
*this = mtd
->priv
;
177 u16
*p
= (u16
*) buf
;
180 for (i
= 0; i
< len
; i
++) {
181 writew(p
[i
], this->IO_ADDR_W
);
188 * au_read_buf16 - read chip data into buffer
189 * @mtd: MTD device structure
190 * @buf: buffer to store date
191 * @len: number of bytes to read
193 * read function for 16bit buswidth
195 static void au_read_buf16(struct mtd_info
*mtd
, u_char
*buf
, int len
)
198 struct nand_chip
*this = mtd
->priv
;
199 u16
*p
= (u16
*) buf
;
202 for (i
= 0; i
< len
; i
++) {
203 p
[i
] = readw(this->IO_ADDR_R
);
209 * au_verify_buf16 - Verify chip data against buffer
210 * @mtd: MTD device structure
211 * @buf: buffer containing the data to compare
212 * @len: number of bytes to compare
214 * verify function for 16bit buswidth
216 static int au_verify_buf16(struct mtd_info
*mtd
, const u_char
*buf
, int len
)
219 struct nand_chip
*this = mtd
->priv
;
220 u16
*p
= (u16
*) buf
;
223 for (i
= 0; i
< len
; i
++) {
224 if (p
[i
] != readw(this->IO_ADDR_R
))
231 /* Select the chip by setting nCE to low */
232 #define NAND_CTL_SETNCE 1
233 /* Deselect the chip by setting nCE to high */
234 #define NAND_CTL_CLRNCE 2
235 /* Select the command latch by setting CLE to high */
236 #define NAND_CTL_SETCLE 3
237 /* Deselect the command latch by setting CLE to low */
238 #define NAND_CTL_CLRCLE 4
239 /* Select the address latch by setting ALE to high */
240 #define NAND_CTL_SETALE 5
241 /* Deselect the address latch by setting ALE to low */
242 #define NAND_CTL_CLRALE 6
244 static void au1550_hwcontrol(struct mtd_info
*mtd
, int cmd
)
246 struct au1550nd_ctx
*ctx
= container_of(mtd
, struct au1550nd_ctx
, info
);
247 struct nand_chip
*this = mtd
->priv
;
251 case NAND_CTL_SETCLE
:
252 this->IO_ADDR_W
= ctx
->base
+ MEM_STNAND_CMD
;
255 case NAND_CTL_CLRCLE
:
256 this->IO_ADDR_W
= ctx
->base
+ MEM_STNAND_DATA
;
259 case NAND_CTL_SETALE
:
260 this->IO_ADDR_W
= ctx
->base
+ MEM_STNAND_ADDR
;
263 case NAND_CTL_CLRALE
:
264 this->IO_ADDR_W
= ctx
->base
+ MEM_STNAND_DATA
;
265 /* FIXME: Nobody knows why this is necessary,
266 * but it works only that way */
270 case NAND_CTL_SETNCE
:
271 /* assert (force assert) chip enable */
272 au_writel((1 << (4 + ctx
->cs
)), MEM_STNDCTL
);
275 case NAND_CTL_CLRNCE
:
276 /* deassert chip enable */
277 au_writel(0, MEM_STNDCTL
);
281 this->IO_ADDR_R
= this->IO_ADDR_W
;
283 /* Drain the writebuffer */
287 int au1550_device_ready(struct mtd_info
*mtd
)
289 int ret
= (au_readl(MEM_STSTAT
) & 0x1) ? 1 : 0;
295 * au1550_select_chip - control -CE line
296 * Forbid driving -CE manually permitting the NAND controller to do this.
297 * Keeping -CE asserted during the whole sector reads interferes with the
298 * NOR flash and PCMCIA drivers as it causes contention on the static bus.
299 * We only have to hold -CE low for the NAND read commands since the flash
300 * chip needs it to be asserted during chip not ready time but the NAND
301 * controller keeps it released.
303 * @mtd: MTD device structure
304 * @chip: chipnumber to select, -1 for deselect
306 static void au1550_select_chip(struct mtd_info
*mtd
, int chip
)
311 * au1550_command - Send command to NAND device
312 * @mtd: MTD device structure
313 * @command: the command to be sent
314 * @column: the column address for this command, -1 if none
315 * @page_addr: the page address for this command, -1 if none
317 static void au1550_command(struct mtd_info
*mtd
, unsigned command
, int column
, int page_addr
)
319 struct au1550nd_ctx
*ctx
= container_of(mtd
, struct au1550nd_ctx
, info
);
320 struct nand_chip
*this = mtd
->priv
;
321 int ce_override
= 0, i
;
322 unsigned long flags
= 0;
324 /* Begin command latch cycle */
325 au1550_hwcontrol(mtd
, NAND_CTL_SETCLE
);
327 * Write out the command to the device.
329 if (command
== NAND_CMD_SEQIN
) {
332 if (column
>= mtd
->writesize
) {
334 column
-= mtd
->writesize
;
335 readcmd
= NAND_CMD_READOOB
;
336 } else if (column
< 256) {
337 /* First 256 bytes --> READ0 */
338 readcmd
= NAND_CMD_READ0
;
341 readcmd
= NAND_CMD_READ1
;
343 ctx
->write_byte(mtd
, readcmd
);
345 ctx
->write_byte(mtd
, command
);
347 /* Set ALE and clear CLE to start address cycle */
348 au1550_hwcontrol(mtd
, NAND_CTL_CLRCLE
);
350 if (column
!= -1 || page_addr
!= -1) {
351 au1550_hwcontrol(mtd
, NAND_CTL_SETALE
);
353 /* Serially input address */
355 /* Adjust columns for 16 bit buswidth */
356 if (this->options
& NAND_BUSWIDTH_16
)
358 ctx
->write_byte(mtd
, column
);
360 if (page_addr
!= -1) {
361 ctx
->write_byte(mtd
, (u8
)(page_addr
& 0xff));
363 if (command
== NAND_CMD_READ0
||
364 command
== NAND_CMD_READ1
||
365 command
== NAND_CMD_READOOB
) {
367 * NAND controller will release -CE after
368 * the last address byte is written, so we'll
369 * have to forcibly assert it. No interrupts
370 * are allowed while we do this as we don't
371 * want the NOR flash or PCMCIA drivers to
372 * steal our precious bytes of data...
375 local_irq_save(flags
);
376 au1550_hwcontrol(mtd
, NAND_CTL_SETNCE
);
379 ctx
->write_byte(mtd
, (u8
)(page_addr
>> 8));
381 /* One more address cycle for devices > 32MiB */
382 if (this->chipsize
> (32 << 20))
384 ((page_addr
>> 16) & 0x0f));
386 /* Latch in address */
387 au1550_hwcontrol(mtd
, NAND_CTL_CLRALE
);
391 * Program and erase have their own busy handlers.
392 * Status and sequential in need no delay.
396 case NAND_CMD_PAGEPROG
:
397 case NAND_CMD_ERASE1
:
398 case NAND_CMD_ERASE2
:
400 case NAND_CMD_STATUS
:
408 case NAND_CMD_READOOB
:
409 /* Check if we're really driving -CE low (just in case) */
410 if (unlikely(!ce_override
))
413 /* Apply a short delay always to ensure that we do wait tWB. */
415 /* Wait for a chip to become ready... */
416 for (i
= this->chip_delay
; !this->dev_ready(mtd
) && i
> 0; --i
)
419 /* Release -CE and re-enable interrupts. */
420 au1550_hwcontrol(mtd
, NAND_CTL_CLRNCE
);
421 local_irq_restore(flags
);
424 /* Apply this short delay always to ensure that we do wait tWB. */
427 while(!this->dev_ready(mtd
));
430 static int __devinit
find_nand_cs(unsigned long nand_base
)
433 (void __iomem
*)KSEG1ADDR(AU1000_STATIC_MEM_PHYS_ADDR
);
434 unsigned long addr
, staddr
, start
, mask
, end
;
437 for (i
= 0; i
< 4; i
++) {
438 addr
= 0x1000 + (i
* 0x10); /* CSx */
439 staddr
= __raw_readl(base
+ addr
+ 0x08); /* STADDRx */
440 /* figure out the decoded range of this CS */
441 start
= (staddr
<< 4) & 0xfffc0000;
442 mask
= (staddr
<< 18) & 0xfffc0000;
443 end
= (start
| (start
- 1)) & ~(start
^ mask
);
444 if ((nand_base
>= start
) && (nand_base
< end
))
451 static int __devinit
au1550nd_probe(struct platform_device
*pdev
)
453 struct au1550nd_platdata
*pd
;
454 struct au1550nd_ctx
*ctx
;
455 struct nand_chip
*this;
459 pd
= pdev
->dev
.platform_data
;
461 dev_err(&pdev
->dev
, "missing platform data\n");
465 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
467 dev_err(&pdev
->dev
, "no memory for NAND context\n");
471 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
473 dev_err(&pdev
->dev
, "no NAND memory resource\n");
477 if (request_mem_region(r
->start
, resource_size(r
), "au1550-nand")) {
478 dev_err(&pdev
->dev
, "cannot claim NAND memory area\n");
483 ctx
->base
= ioremap_nocache(r
->start
, 0x1000);
485 dev_err(&pdev
->dev
, "cannot remap NAND memory area\n");
491 ctx
->info
.priv
= this;
492 ctx
->info
.owner
= THIS_MODULE
;
494 /* figure out which CS# r->start belongs to */
495 cs
= find_nand_cs(r
->start
);
497 dev_err(&pdev
->dev
, "cannot detect NAND chipselect\n");
503 this->dev_ready
= au1550_device_ready
;
504 this->select_chip
= au1550_select_chip
;
505 this->cmdfunc
= au1550_command
;
507 /* 30 us command delay time */
508 this->chip_delay
= 30;
509 this->ecc
.mode
= NAND_ECC_SOFT
;
511 this->options
= NAND_NO_AUTOINCR
;
514 this->options
|= NAND_BUSWIDTH_16
;
516 this->read_byte
= (pd
->devwidth
) ? au_read_byte16
: au_read_byte
;
517 ctx
->write_byte
= (pd
->devwidth
) ? au_write_byte16
: au_write_byte
;
518 this->read_word
= au_read_word
;
519 this->write_buf
= (pd
->devwidth
) ? au_write_buf16
: au_write_buf
;
520 this->read_buf
= (pd
->devwidth
) ? au_read_buf16
: au_read_buf
;
521 this->verify_buf
= (pd
->devwidth
) ? au_verify_buf16
: au_verify_buf
;
523 ret
= nand_scan(&ctx
->info
, 1);
525 dev_err(&pdev
->dev
, "NAND scan failed with %d\n", ret
);
529 mtd_device_register(&ctx
->info
, pd
->parts
, pd
->num_parts
);
536 release_mem_region(r
->start
, resource_size(r
));
542 static int __devexit
au1550nd_remove(struct platform_device
*pdev
)
544 struct au1550nd_ctx
*ctx
= platform_get_drvdata(pdev
);
545 struct resource
*r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
547 nand_release(&ctx
->info
);
549 release_mem_region(r
->start
, 0x1000);
554 static struct platform_driver au1550nd_driver
= {
556 .name
= "au1550-nand",
557 .owner
= THIS_MODULE
,
559 .probe
= au1550nd_probe
,
560 .remove
= __devexit_p(au1550nd_remove
),
563 module_platform_driver(au1550nd_driver
);
565 MODULE_LICENSE("GPL");
566 MODULE_AUTHOR("Embedded Edge, LLC");
567 MODULE_DESCRIPTION("Board-specific glue layer for NAND flash on Pb1550 board");