Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / mtd / nand / au1550nd.c
blob8ab827edf94e2fbc97885032da26105eaeb2bdea
1 /*
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/module.h>
15 #include <linux/interrupt.h>
16 #include <linux/mtd/mtd.h>
17 #include <linux/mtd/rawnand.h>
18 #include <linux/mtd/partitions.h>
19 #include <linux/platform_device.h>
20 #include <asm/io.h>
21 #include <asm/mach-au1x00/au1000.h>
22 #include <asm/mach-au1x00/au1550nd.h>
25 struct au1550nd_ctx {
26 struct nand_chip chip;
28 int cs;
29 void __iomem *base;
30 void (*write_byte)(struct mtd_info *, u_char);
33 /**
34 * au_read_byte - read one byte from the chip
35 * @mtd: MTD device structure
37 * read function for 8bit buswidth
39 static u_char au_read_byte(struct mtd_info *mtd)
41 struct nand_chip *this = mtd_to_nand(mtd);
42 u_char ret = readb(this->IO_ADDR_R);
43 wmb(); /* drain writebuffer */
44 return ret;
47 /**
48 * au_write_byte - write one byte to the chip
49 * @mtd: MTD device structure
50 * @byte: pointer to data byte to write
52 * write function for 8it buswidth
54 static void au_write_byte(struct mtd_info *mtd, u_char byte)
56 struct nand_chip *this = mtd_to_nand(mtd);
57 writeb(byte, this->IO_ADDR_W);
58 wmb(); /* drain writebuffer */
61 /**
62 * au_read_byte16 - read one byte endianness aware from the chip
63 * @mtd: MTD device structure
65 * read function for 16bit buswidth with endianness conversion
67 static u_char au_read_byte16(struct mtd_info *mtd)
69 struct nand_chip *this = mtd_to_nand(mtd);
70 u_char ret = (u_char) cpu_to_le16(readw(this->IO_ADDR_R));
71 wmb(); /* drain writebuffer */
72 return ret;
75 /**
76 * au_write_byte16 - write one byte endianness aware to the chip
77 * @mtd: MTD device structure
78 * @byte: pointer to data byte to write
80 * write function for 16bit buswidth with endianness conversion
82 static void au_write_byte16(struct mtd_info *mtd, u_char byte)
84 struct nand_chip *this = mtd_to_nand(mtd);
85 writew(le16_to_cpu((u16) byte), this->IO_ADDR_W);
86 wmb(); /* drain writebuffer */
89 /**
90 * au_read_word - read one word from the chip
91 * @mtd: MTD device structure
93 * read function for 16bit buswidth without endianness conversion
95 static u16 au_read_word(struct mtd_info *mtd)
97 struct nand_chip *this = mtd_to_nand(mtd);
98 u16 ret = readw(this->IO_ADDR_R);
99 wmb(); /* drain writebuffer */
100 return ret;
104 * au_write_buf - write buffer to chip
105 * @mtd: MTD device structure
106 * @buf: data buffer
107 * @len: number of bytes to write
109 * write function for 8bit buswidth
111 static void au_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
113 int i;
114 struct nand_chip *this = mtd_to_nand(mtd);
116 for (i = 0; i < len; i++) {
117 writeb(buf[i], this->IO_ADDR_W);
118 wmb(); /* drain writebuffer */
123 * au_read_buf - read chip data into buffer
124 * @mtd: MTD device structure
125 * @buf: buffer to store date
126 * @len: number of bytes to read
128 * read function for 8bit buswidth
130 static void au_read_buf(struct mtd_info *mtd, u_char *buf, int len)
132 int i;
133 struct nand_chip *this = mtd_to_nand(mtd);
135 for (i = 0; i < len; i++) {
136 buf[i] = readb(this->IO_ADDR_R);
137 wmb(); /* drain writebuffer */
142 * au_write_buf16 - write buffer to chip
143 * @mtd: MTD device structure
144 * @buf: data buffer
145 * @len: number of bytes to write
147 * write function for 16bit buswidth
149 static void au_write_buf16(struct mtd_info *mtd, const u_char *buf, int len)
151 int i;
152 struct nand_chip *this = mtd_to_nand(mtd);
153 u16 *p = (u16 *) buf;
154 len >>= 1;
156 for (i = 0; i < len; i++) {
157 writew(p[i], this->IO_ADDR_W);
158 wmb(); /* drain writebuffer */
164 * au_read_buf16 - read chip data into buffer
165 * @mtd: MTD device structure
166 * @buf: buffer to store date
167 * @len: number of bytes to read
169 * read function for 16bit buswidth
171 static void au_read_buf16(struct mtd_info *mtd, u_char *buf, int len)
173 int i;
174 struct nand_chip *this = mtd_to_nand(mtd);
175 u16 *p = (u16 *) buf;
176 len >>= 1;
178 for (i = 0; i < len; i++) {
179 p[i] = readw(this->IO_ADDR_R);
180 wmb(); /* drain writebuffer */
184 /* Select the chip by setting nCE to low */
185 #define NAND_CTL_SETNCE 1
186 /* Deselect the chip by setting nCE to high */
187 #define NAND_CTL_CLRNCE 2
188 /* Select the command latch by setting CLE to high */
189 #define NAND_CTL_SETCLE 3
190 /* Deselect the command latch by setting CLE to low */
191 #define NAND_CTL_CLRCLE 4
192 /* Select the address latch by setting ALE to high */
193 #define NAND_CTL_SETALE 5
194 /* Deselect the address latch by setting ALE to low */
195 #define NAND_CTL_CLRALE 6
197 static void au1550_hwcontrol(struct mtd_info *mtd, int cmd)
199 struct nand_chip *this = mtd_to_nand(mtd);
200 struct au1550nd_ctx *ctx = container_of(this, struct au1550nd_ctx,
201 chip);
203 switch (cmd) {
205 case NAND_CTL_SETCLE:
206 this->IO_ADDR_W = ctx->base + MEM_STNAND_CMD;
207 break;
209 case NAND_CTL_CLRCLE:
210 this->IO_ADDR_W = ctx->base + MEM_STNAND_DATA;
211 break;
213 case NAND_CTL_SETALE:
214 this->IO_ADDR_W = ctx->base + MEM_STNAND_ADDR;
215 break;
217 case NAND_CTL_CLRALE:
218 this->IO_ADDR_W = ctx->base + MEM_STNAND_DATA;
219 /* FIXME: Nobody knows why this is necessary,
220 * but it works only that way */
221 udelay(1);
222 break;
224 case NAND_CTL_SETNCE:
225 /* assert (force assert) chip enable */
226 alchemy_wrsmem((1 << (4 + ctx->cs)), AU1000_MEM_STNDCTL);
227 break;
229 case NAND_CTL_CLRNCE:
230 /* deassert chip enable */
231 alchemy_wrsmem(0, AU1000_MEM_STNDCTL);
232 break;
235 this->IO_ADDR_R = this->IO_ADDR_W;
237 wmb(); /* Drain the writebuffer */
240 int au1550_device_ready(struct mtd_info *mtd)
242 return (alchemy_rdsmem(AU1000_MEM_STSTAT) & 0x1) ? 1 : 0;
246 * au1550_select_chip - control -CE line
247 * Forbid driving -CE manually permitting the NAND controller to do this.
248 * Keeping -CE asserted during the whole sector reads interferes with the
249 * NOR flash and PCMCIA drivers as it causes contention on the static bus.
250 * We only have to hold -CE low for the NAND read commands since the flash
251 * chip needs it to be asserted during chip not ready time but the NAND
252 * controller keeps it released.
254 * @mtd: MTD device structure
255 * @chip: chipnumber to select, -1 for deselect
257 static void au1550_select_chip(struct mtd_info *mtd, int chip)
262 * au1550_command - Send command to NAND device
263 * @mtd: MTD device structure
264 * @command: the command to be sent
265 * @column: the column address for this command, -1 if none
266 * @page_addr: the page address for this command, -1 if none
268 static void au1550_command(struct mtd_info *mtd, unsigned command, int column, int page_addr)
270 struct nand_chip *this = mtd_to_nand(mtd);
271 struct au1550nd_ctx *ctx = container_of(this, struct au1550nd_ctx,
272 chip);
273 int ce_override = 0, i;
274 unsigned long flags = 0;
276 /* Begin command latch cycle */
277 au1550_hwcontrol(mtd, NAND_CTL_SETCLE);
279 * Write out the command to the device.
281 if (command == NAND_CMD_SEQIN) {
282 int readcmd;
284 if (column >= mtd->writesize) {
285 /* OOB area */
286 column -= mtd->writesize;
287 readcmd = NAND_CMD_READOOB;
288 } else if (column < 256) {
289 /* First 256 bytes --> READ0 */
290 readcmd = NAND_CMD_READ0;
291 } else {
292 column -= 256;
293 readcmd = NAND_CMD_READ1;
295 ctx->write_byte(mtd, readcmd);
297 ctx->write_byte(mtd, command);
299 /* Set ALE and clear CLE to start address cycle */
300 au1550_hwcontrol(mtd, NAND_CTL_CLRCLE);
302 if (column != -1 || page_addr != -1) {
303 au1550_hwcontrol(mtd, NAND_CTL_SETALE);
305 /* Serially input address */
306 if (column != -1) {
307 /* Adjust columns for 16 bit buswidth */
308 if (this->options & NAND_BUSWIDTH_16 &&
309 !nand_opcode_8bits(command))
310 column >>= 1;
311 ctx->write_byte(mtd, column);
313 if (page_addr != -1) {
314 ctx->write_byte(mtd, (u8)(page_addr & 0xff));
316 if (command == NAND_CMD_READ0 ||
317 command == NAND_CMD_READ1 ||
318 command == NAND_CMD_READOOB) {
320 * NAND controller will release -CE after
321 * the last address byte is written, so we'll
322 * have to forcibly assert it. No interrupts
323 * are allowed while we do this as we don't
324 * want the NOR flash or PCMCIA drivers to
325 * steal our precious bytes of data...
327 ce_override = 1;
328 local_irq_save(flags);
329 au1550_hwcontrol(mtd, NAND_CTL_SETNCE);
332 ctx->write_byte(mtd, (u8)(page_addr >> 8));
334 if (this->options & NAND_ROW_ADDR_3)
335 ctx->write_byte(mtd,
336 ((page_addr >> 16) & 0x0f));
338 /* Latch in address */
339 au1550_hwcontrol(mtd, NAND_CTL_CLRALE);
343 * Program and erase have their own busy handlers.
344 * Status and sequential in need no delay.
346 switch (command) {
348 case NAND_CMD_PAGEPROG:
349 case NAND_CMD_ERASE1:
350 case NAND_CMD_ERASE2:
351 case NAND_CMD_SEQIN:
352 case NAND_CMD_STATUS:
353 return;
355 case NAND_CMD_RESET:
356 break;
358 case NAND_CMD_READ0:
359 case NAND_CMD_READ1:
360 case NAND_CMD_READOOB:
361 /* Check if we're really driving -CE low (just in case) */
362 if (unlikely(!ce_override))
363 break;
365 /* Apply a short delay always to ensure that we do wait tWB. */
366 ndelay(100);
367 /* Wait for a chip to become ready... */
368 for (i = this->chip_delay; !this->dev_ready(mtd) && i > 0; --i)
369 udelay(1);
371 /* Release -CE and re-enable interrupts. */
372 au1550_hwcontrol(mtd, NAND_CTL_CLRNCE);
373 local_irq_restore(flags);
374 return;
376 /* Apply this short delay always to ensure that we do wait tWB. */
377 ndelay(100);
379 while(!this->dev_ready(mtd));
382 static int find_nand_cs(unsigned long nand_base)
384 void __iomem *base =
385 (void __iomem *)KSEG1ADDR(AU1000_STATIC_MEM_PHYS_ADDR);
386 unsigned long addr, staddr, start, mask, end;
387 int i;
389 for (i = 0; i < 4; i++) {
390 addr = 0x1000 + (i * 0x10); /* CSx */
391 staddr = __raw_readl(base + addr + 0x08); /* STADDRx */
392 /* figure out the decoded range of this CS */
393 start = (staddr << 4) & 0xfffc0000;
394 mask = (staddr << 18) & 0xfffc0000;
395 end = (start | (start - 1)) & ~(start ^ mask);
396 if ((nand_base >= start) && (nand_base < end))
397 return i;
400 return -ENODEV;
403 static int au1550nd_probe(struct platform_device *pdev)
405 struct au1550nd_platdata *pd;
406 struct au1550nd_ctx *ctx;
407 struct nand_chip *this;
408 struct mtd_info *mtd;
409 struct resource *r;
410 int ret, cs;
412 pd = dev_get_platdata(&pdev->dev);
413 if (!pd) {
414 dev_err(&pdev->dev, "missing platform data\n");
415 return -ENODEV;
418 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
419 if (!ctx)
420 return -ENOMEM;
422 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
423 if (!r) {
424 dev_err(&pdev->dev, "no NAND memory resource\n");
425 ret = -ENODEV;
426 goto out1;
428 if (request_mem_region(r->start, resource_size(r), "au1550-nand")) {
429 dev_err(&pdev->dev, "cannot claim NAND memory area\n");
430 ret = -ENOMEM;
431 goto out1;
434 ctx->base = ioremap_nocache(r->start, 0x1000);
435 if (!ctx->base) {
436 dev_err(&pdev->dev, "cannot remap NAND memory area\n");
437 ret = -ENODEV;
438 goto out2;
441 this = &ctx->chip;
442 mtd = nand_to_mtd(this);
443 mtd->dev.parent = &pdev->dev;
445 /* figure out which CS# r->start belongs to */
446 cs = find_nand_cs(r->start);
447 if (cs < 0) {
448 dev_err(&pdev->dev, "cannot detect NAND chipselect\n");
449 ret = -ENODEV;
450 goto out3;
452 ctx->cs = cs;
454 this->dev_ready = au1550_device_ready;
455 this->select_chip = au1550_select_chip;
456 this->cmdfunc = au1550_command;
458 /* 30 us command delay time */
459 this->chip_delay = 30;
460 this->ecc.mode = NAND_ECC_SOFT;
461 this->ecc.algo = NAND_ECC_HAMMING;
463 if (pd->devwidth)
464 this->options |= NAND_BUSWIDTH_16;
466 this->read_byte = (pd->devwidth) ? au_read_byte16 : au_read_byte;
467 ctx->write_byte = (pd->devwidth) ? au_write_byte16 : au_write_byte;
468 this->read_word = au_read_word;
469 this->write_buf = (pd->devwidth) ? au_write_buf16 : au_write_buf;
470 this->read_buf = (pd->devwidth) ? au_read_buf16 : au_read_buf;
472 ret = nand_scan(mtd, 1);
473 if (ret) {
474 dev_err(&pdev->dev, "NAND scan failed with %d\n", ret);
475 goto out3;
478 mtd_device_register(mtd, pd->parts, pd->num_parts);
480 platform_set_drvdata(pdev, ctx);
482 return 0;
484 out3:
485 iounmap(ctx->base);
486 out2:
487 release_mem_region(r->start, resource_size(r));
488 out1:
489 kfree(ctx);
490 return ret;
493 static int au1550nd_remove(struct platform_device *pdev)
495 struct au1550nd_ctx *ctx = platform_get_drvdata(pdev);
496 struct resource *r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
498 nand_release(nand_to_mtd(&ctx->chip));
499 iounmap(ctx->base);
500 release_mem_region(r->start, 0x1000);
501 kfree(ctx);
502 return 0;
505 static struct platform_driver au1550nd_driver = {
506 .driver = {
507 .name = "au1550-nand",
509 .probe = au1550nd_probe,
510 .remove = au1550nd_remove,
513 module_platform_driver(au1550nd_driver);
515 MODULE_LICENSE("GPL");
516 MODULE_AUTHOR("Embedded Edge, LLC");
517 MODULE_DESCRIPTION("Board-specific glue layer for NAND flash on Pb1550 board");