2 * Copyright (C) 2004 Embedded Edge, LLC
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
10 #include <linux/slab.h>
11 #include <linux/module.h>
12 #include <linux/interrupt.h>
13 #include <linux/mtd/mtd.h>
14 #include <linux/mtd/rawnand.h>
15 #include <linux/mtd/partitions.h>
16 #include <linux/platform_device.h>
18 #include <asm/mach-au1x00/au1000.h>
19 #include <asm/mach-au1x00/au1550nd.h>
23 struct nand_chip chip
;
27 void (*write_byte
)(struct mtd_info
*, u_char
);
31 * au_read_byte - read one byte from the chip
32 * @mtd: MTD device structure
34 * read function for 8bit buswidth
36 static u_char
au_read_byte(struct mtd_info
*mtd
)
38 struct nand_chip
*this = mtd_to_nand(mtd
);
39 u_char ret
= readb(this->IO_ADDR_R
);
40 wmb(); /* drain writebuffer */
45 * au_write_byte - write one byte to the chip
46 * @mtd: MTD device structure
47 * @byte: pointer to data byte to write
49 * write function for 8it buswidth
51 static void au_write_byte(struct mtd_info
*mtd
, u_char byte
)
53 struct nand_chip
*this = mtd_to_nand(mtd
);
54 writeb(byte
, this->IO_ADDR_W
);
55 wmb(); /* drain writebuffer */
59 * au_read_byte16 - read one byte endianness aware from the chip
60 * @mtd: MTD device structure
62 * read function for 16bit buswidth with endianness conversion
64 static u_char
au_read_byte16(struct mtd_info
*mtd
)
66 struct nand_chip
*this = mtd_to_nand(mtd
);
67 u_char ret
= (u_char
) cpu_to_le16(readw(this->IO_ADDR_R
));
68 wmb(); /* drain writebuffer */
73 * au_write_byte16 - write one byte endianness aware to the chip
74 * @mtd: MTD device structure
75 * @byte: pointer to data byte to write
77 * write function for 16bit buswidth with endianness conversion
79 static void au_write_byte16(struct mtd_info
*mtd
, u_char byte
)
81 struct nand_chip
*this = mtd_to_nand(mtd
);
82 writew(le16_to_cpu((u16
) byte
), this->IO_ADDR_W
);
83 wmb(); /* drain writebuffer */
87 * au_read_word - read one word from the chip
88 * @mtd: MTD device structure
90 * read function for 16bit buswidth without endianness conversion
92 static u16
au_read_word(struct mtd_info
*mtd
)
94 struct nand_chip
*this = mtd_to_nand(mtd
);
95 u16 ret
= readw(this->IO_ADDR_R
);
96 wmb(); /* drain writebuffer */
101 * au_write_buf - write buffer to chip
102 * @mtd: MTD device structure
104 * @len: number of bytes to write
106 * write function for 8bit buswidth
108 static void au_write_buf(struct mtd_info
*mtd
, const u_char
*buf
, int len
)
111 struct nand_chip
*this = mtd_to_nand(mtd
);
113 for (i
= 0; i
< len
; i
++) {
114 writeb(buf
[i
], this->IO_ADDR_W
);
115 wmb(); /* drain writebuffer */
120 * au_read_buf - read chip data into buffer
121 * @mtd: MTD device structure
122 * @buf: buffer to store date
123 * @len: number of bytes to read
125 * read function for 8bit buswidth
127 static void au_read_buf(struct mtd_info
*mtd
, u_char
*buf
, int len
)
130 struct nand_chip
*this = mtd_to_nand(mtd
);
132 for (i
= 0; i
< len
; i
++) {
133 buf
[i
] = readb(this->IO_ADDR_R
);
134 wmb(); /* drain writebuffer */
139 * au_write_buf16 - write buffer to chip
140 * @mtd: MTD device structure
142 * @len: number of bytes to write
144 * write function for 16bit buswidth
146 static void au_write_buf16(struct mtd_info
*mtd
, const u_char
*buf
, int len
)
149 struct nand_chip
*this = mtd_to_nand(mtd
);
150 u16
*p
= (u16
*) buf
;
153 for (i
= 0; i
< len
; i
++) {
154 writew(p
[i
], this->IO_ADDR_W
);
155 wmb(); /* drain writebuffer */
161 * au_read_buf16 - read chip data into buffer
162 * @mtd: MTD device structure
163 * @buf: buffer to store date
164 * @len: number of bytes to read
166 * read function for 16bit buswidth
168 static void au_read_buf16(struct mtd_info
*mtd
, u_char
*buf
, int len
)
171 struct nand_chip
*this = mtd_to_nand(mtd
);
172 u16
*p
= (u16
*) buf
;
175 for (i
= 0; i
< len
; i
++) {
176 p
[i
] = readw(this->IO_ADDR_R
);
177 wmb(); /* drain writebuffer */
181 /* Select the chip by setting nCE to low */
182 #define NAND_CTL_SETNCE 1
183 /* Deselect the chip by setting nCE to high */
184 #define NAND_CTL_CLRNCE 2
185 /* Select the command latch by setting CLE to high */
186 #define NAND_CTL_SETCLE 3
187 /* Deselect the command latch by setting CLE to low */
188 #define NAND_CTL_CLRCLE 4
189 /* Select the address latch by setting ALE to high */
190 #define NAND_CTL_SETALE 5
191 /* Deselect the address latch by setting ALE to low */
192 #define NAND_CTL_CLRALE 6
194 static void au1550_hwcontrol(struct mtd_info
*mtd
, int cmd
)
196 struct nand_chip
*this = mtd_to_nand(mtd
);
197 struct au1550nd_ctx
*ctx
= container_of(this, struct au1550nd_ctx
,
202 case NAND_CTL_SETCLE
:
203 this->IO_ADDR_W
= ctx
->base
+ MEM_STNAND_CMD
;
206 case NAND_CTL_CLRCLE
:
207 this->IO_ADDR_W
= ctx
->base
+ MEM_STNAND_DATA
;
210 case NAND_CTL_SETALE
:
211 this->IO_ADDR_W
= ctx
->base
+ MEM_STNAND_ADDR
;
214 case NAND_CTL_CLRALE
:
215 this->IO_ADDR_W
= ctx
->base
+ MEM_STNAND_DATA
;
216 /* FIXME: Nobody knows why this is necessary,
217 * but it works only that way */
221 case NAND_CTL_SETNCE
:
222 /* assert (force assert) chip enable */
223 alchemy_wrsmem((1 << (4 + ctx
->cs
)), AU1000_MEM_STNDCTL
);
226 case NAND_CTL_CLRNCE
:
227 /* deassert chip enable */
228 alchemy_wrsmem(0, AU1000_MEM_STNDCTL
);
232 this->IO_ADDR_R
= this->IO_ADDR_W
;
234 wmb(); /* Drain the writebuffer */
237 int au1550_device_ready(struct mtd_info
*mtd
)
239 return (alchemy_rdsmem(AU1000_MEM_STSTAT
) & 0x1) ? 1 : 0;
243 * au1550_select_chip - control -CE line
244 * Forbid driving -CE manually permitting the NAND controller to do this.
245 * Keeping -CE asserted during the whole sector reads interferes with the
246 * NOR flash and PCMCIA drivers as it causes contention on the static bus.
247 * We only have to hold -CE low for the NAND read commands since the flash
248 * chip needs it to be asserted during chip not ready time but the NAND
249 * controller keeps it released.
251 * @mtd: MTD device structure
252 * @chip: chipnumber to select, -1 for deselect
254 static void au1550_select_chip(struct mtd_info
*mtd
, int chip
)
259 * au1550_command - Send command to NAND device
260 * @mtd: MTD device structure
261 * @command: the command to be sent
262 * @column: the column address for this command, -1 if none
263 * @page_addr: the page address for this command, -1 if none
265 static void au1550_command(struct mtd_info
*mtd
, unsigned command
, int column
, int page_addr
)
267 struct nand_chip
*this = mtd_to_nand(mtd
);
268 struct au1550nd_ctx
*ctx
= container_of(this, struct au1550nd_ctx
,
270 int ce_override
= 0, i
;
271 unsigned long flags
= 0;
273 /* Begin command latch cycle */
274 au1550_hwcontrol(mtd
, NAND_CTL_SETCLE
);
276 * Write out the command to the device.
278 if (command
== NAND_CMD_SEQIN
) {
281 if (column
>= mtd
->writesize
) {
283 column
-= mtd
->writesize
;
284 readcmd
= NAND_CMD_READOOB
;
285 } else if (column
< 256) {
286 /* First 256 bytes --> READ0 */
287 readcmd
= NAND_CMD_READ0
;
290 readcmd
= NAND_CMD_READ1
;
292 ctx
->write_byte(mtd
, readcmd
);
294 ctx
->write_byte(mtd
, command
);
296 /* Set ALE and clear CLE to start address cycle */
297 au1550_hwcontrol(mtd
, NAND_CTL_CLRCLE
);
299 if (column
!= -1 || page_addr
!= -1) {
300 au1550_hwcontrol(mtd
, NAND_CTL_SETALE
);
302 /* Serially input address */
304 /* Adjust columns for 16 bit buswidth */
305 if (this->options
& NAND_BUSWIDTH_16
&&
306 !nand_opcode_8bits(command
))
308 ctx
->write_byte(mtd
, column
);
310 if (page_addr
!= -1) {
311 ctx
->write_byte(mtd
, (u8
)(page_addr
& 0xff));
313 if (command
== NAND_CMD_READ0
||
314 command
== NAND_CMD_READ1
||
315 command
== NAND_CMD_READOOB
) {
317 * NAND controller will release -CE after
318 * the last address byte is written, so we'll
319 * have to forcibly assert it. No interrupts
320 * are allowed while we do this as we don't
321 * want the NOR flash or PCMCIA drivers to
322 * steal our precious bytes of data...
325 local_irq_save(flags
);
326 au1550_hwcontrol(mtd
, NAND_CTL_SETNCE
);
329 ctx
->write_byte(mtd
, (u8
)(page_addr
>> 8));
331 if (this->options
& NAND_ROW_ADDR_3
)
333 ((page_addr
>> 16) & 0x0f));
335 /* Latch in address */
336 au1550_hwcontrol(mtd
, NAND_CTL_CLRALE
);
340 * Program and erase have their own busy handlers.
341 * Status and sequential in need no delay.
345 case NAND_CMD_PAGEPROG
:
346 case NAND_CMD_ERASE1
:
347 case NAND_CMD_ERASE2
:
349 case NAND_CMD_STATUS
:
357 case NAND_CMD_READOOB
:
358 /* Check if we're really driving -CE low (just in case) */
359 if (unlikely(!ce_override
))
362 /* Apply a short delay always to ensure that we do wait tWB. */
364 /* Wait for a chip to become ready... */
365 for (i
= this->chip_delay
; !this->dev_ready(mtd
) && i
> 0; --i
)
368 /* Release -CE and re-enable interrupts. */
369 au1550_hwcontrol(mtd
, NAND_CTL_CLRNCE
);
370 local_irq_restore(flags
);
373 /* Apply this short delay always to ensure that we do wait tWB. */
376 while(!this->dev_ready(mtd
));
379 static int find_nand_cs(unsigned long nand_base
)
382 (void __iomem
*)KSEG1ADDR(AU1000_STATIC_MEM_PHYS_ADDR
);
383 unsigned long addr
, staddr
, start
, mask
, end
;
386 for (i
= 0; i
< 4; i
++) {
387 addr
= 0x1000 + (i
* 0x10); /* CSx */
388 staddr
= __raw_readl(base
+ addr
+ 0x08); /* STADDRx */
389 /* figure out the decoded range of this CS */
390 start
= (staddr
<< 4) & 0xfffc0000;
391 mask
= (staddr
<< 18) & 0xfffc0000;
392 end
= (start
| (start
- 1)) & ~(start
^ mask
);
393 if ((nand_base
>= start
) && (nand_base
< end
))
400 static int au1550nd_probe(struct platform_device
*pdev
)
402 struct au1550nd_platdata
*pd
;
403 struct au1550nd_ctx
*ctx
;
404 struct nand_chip
*this;
405 struct mtd_info
*mtd
;
409 pd
= dev_get_platdata(&pdev
->dev
);
411 dev_err(&pdev
->dev
, "missing platform data\n");
415 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
419 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
421 dev_err(&pdev
->dev
, "no NAND memory resource\n");
425 if (request_mem_region(r
->start
, resource_size(r
), "au1550-nand")) {
426 dev_err(&pdev
->dev
, "cannot claim NAND memory area\n");
431 ctx
->base
= ioremap_nocache(r
->start
, 0x1000);
433 dev_err(&pdev
->dev
, "cannot remap NAND memory area\n");
439 mtd
= nand_to_mtd(this);
440 mtd
->dev
.parent
= &pdev
->dev
;
442 /* figure out which CS# r->start belongs to */
443 cs
= find_nand_cs(r
->start
);
445 dev_err(&pdev
->dev
, "cannot detect NAND chipselect\n");
451 this->dev_ready
= au1550_device_ready
;
452 this->select_chip
= au1550_select_chip
;
453 this->cmdfunc
= au1550_command
;
455 /* 30 us command delay time */
456 this->chip_delay
= 30;
457 this->ecc
.mode
= NAND_ECC_SOFT
;
458 this->ecc
.algo
= NAND_ECC_HAMMING
;
461 this->options
|= NAND_BUSWIDTH_16
;
463 this->read_byte
= (pd
->devwidth
) ? au_read_byte16
: au_read_byte
;
464 ctx
->write_byte
= (pd
->devwidth
) ? au_write_byte16
: au_write_byte
;
465 this->read_word
= au_read_word
;
466 this->write_buf
= (pd
->devwidth
) ? au_write_buf16
: au_write_buf
;
467 this->read_buf
= (pd
->devwidth
) ? au_read_buf16
: au_read_buf
;
469 ret
= nand_scan(this, 1);
471 dev_err(&pdev
->dev
, "NAND scan failed with %d\n", ret
);
475 mtd_device_register(mtd
, pd
->parts
, pd
->num_parts
);
477 platform_set_drvdata(pdev
, ctx
);
484 release_mem_region(r
->start
, resource_size(r
));
490 static int au1550nd_remove(struct platform_device
*pdev
)
492 struct au1550nd_ctx
*ctx
= platform_get_drvdata(pdev
);
493 struct resource
*r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
495 nand_release(&ctx
->chip
);
497 release_mem_region(r
->start
, 0x1000);
502 static struct platform_driver au1550nd_driver
= {
504 .name
= "au1550-nand",
506 .probe
= au1550nd_probe
,
507 .remove
= au1550nd_remove
,
510 module_platform_driver(au1550nd_driver
);
512 MODULE_LICENSE("GPL");
513 MODULE_AUTHOR("Embedded Edge, LLC");
514 MODULE_DESCRIPTION("Board-specific glue layer for NAND flash on Pb1550 board");