1 From bed297dad6283a5926962c1c59f95ad641824630 Mon Sep 17 00:00:00 2001
2 From: =?utf-8?q?Arve=20Hj=C3=B8nnev=C3=A5g?= <arve@google.com>
3 Date: Fri, 29 Jun 2007 22:20:07 -0700
4 Subject: [PATCH 125/134] [ARM] goldfish: NAND: Add nand driver for goldfish.
6 Content-Type: text/plain; charset=utf-8
7 Content-Transfer-Encoding: 8bit
9 Signed-off-by: Mike A. Chan <mikechan@google.com>
10 Signed-off-by: Arve Hjønnevåg <arve@android.com>
12 drivers/mtd/devices/Kconfig | 5 +
13 drivers/mtd/devices/Makefile | 1 +
14 drivers/mtd/devices/goldfish_nand.c | 418 +++++++++++++++++++++++++++++++
15 drivers/mtd/devices/goldfish_nand_reg.h | 58 +++++
16 4 files changed, 482 insertions(+), 0 deletions(-)
17 create mode 100644 drivers/mtd/devices/goldfish_nand.c
18 create mode 100644 drivers/mtd/devices/goldfish_nand_reg.h
20 diff --git a/drivers/mtd/devices/Kconfig b/drivers/mtd/devices/Kconfig
21 index 6fde0a2..0e9bdd7 100644
22 --- a/drivers/mtd/devices/Kconfig
23 +++ b/drivers/mtd/devices/Kconfig
24 @@ -297,5 +297,10 @@ config MTD_DOCPROBE_55AA
25 LinuxBIOS or if you need to recover a DiskOnChip Millennium on which
26 you have managed to wipe the first block.
28 +config MTD_GOLDFISH_NAND
29 + tristate "Goldfish NAND device"
35 diff --git a/drivers/mtd/devices/Makefile b/drivers/mtd/devices/Makefile
36 index 0993d5c..46cf8a8 100644
37 --- a/drivers/mtd/devices/Makefile
38 +++ b/drivers/mtd/devices/Makefile
39 @@ -16,3 +16,4 @@ obj-$(CONFIG_MTD_LART) += lart.o
40 obj-$(CONFIG_MTD_BLOCK2MTD) += block2mtd.o
41 obj-$(CONFIG_MTD_DATAFLASH) += mtd_dataflash.o
42 obj-$(CONFIG_MTD_M25P80) += m25p80.o
43 +obj-$(CONFIG_MTD_GOLDFISH_NAND) += goldfish_nand.o
44 diff --git a/drivers/mtd/devices/goldfish_nand.c b/drivers/mtd/devices/goldfish_nand.c
46 index 0000000..6b4b8b1
48 +++ b/drivers/mtd/devices/goldfish_nand.c
50 +/* drivers/mtd/devices/goldfish_nand.c
52 +** Copyright (C) 2007 Google, Inc.
54 +** This software is licensed under the terms of the GNU General Public
55 +** License version 2, as published by the Free Software Foundation, and
56 +** may be copied, distributed, and modified under those terms.
58 +** This program is distributed in the hope that it will be useful,
59 +** but WITHOUT ANY WARRANTY; without even the implied warranty of
60 +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
61 +** GNU General Public License for more details.
65 +#include <asm/div64.h>
67 +#include <linux/module.h>
68 +#include <linux/slab.h>
69 +#include <linux/ioport.h>
70 +#include <linux/vmalloc.h>
71 +#include <linux/init.h>
72 +#include <linux/mtd/compatmac.h>
73 +#include <linux/mtd/mtd.h>
74 +#include <linux/platform_device.h>
76 +#include "goldfish_nand_reg.h"
78 +struct goldfish_nand {
80 + unsigned char __iomem *base;
82 + struct mtd_info mtd[0];
85 +static uint32_t goldfish_nand_cmd(struct mtd_info *mtd, enum nand_cmd cmd,
86 + uint64_t addr, uint32_t len, void *ptr)
88 + struct goldfish_nand *nand = mtd->priv;
90 + unsigned long irq_flags;
91 + unsigned char __iomem *base = nand->base;
93 + spin_lock_irqsave(&nand->lock, irq_flags);
94 + writel(mtd - nand->mtd, base + NAND_DEV);
95 + writel((uint32_t)(addr >> 32), base + NAND_ADDR_HIGH);
96 + writel((uint32_t)addr, base + NAND_ADDR_LOW);
97 + writel(len, base + NAND_TRANSFER_SIZE);
98 + writel(ptr, base + NAND_DATA);
99 + writel(cmd, base + NAND_COMMAND);
100 + rv = readl(base + NAND_RESULT);
101 + spin_unlock_irqrestore(&nand->lock, irq_flags);
105 +static int goldfish_nand_erase(struct mtd_info *mtd, struct erase_info *instr)
107 + loff_t ofs = instr->addr;
108 + uint32_t len = instr->len;
111 + if (ofs + len > mtd->size)
113 + rem = do_div(ofs, mtd->writesize);
116 + ofs *= (mtd->writesize + mtd->oobsize);
118 + if(len % mtd->writesize)
120 + len = len / mtd->writesize * (mtd->writesize + mtd->oobsize);
122 + if(goldfish_nand_cmd(mtd, NAND_CMD_ERASE, ofs, len, NULL) != len) {
123 + printk("goldfish_nand_erase: erase failed, start %llx, len %x, dev_size "
124 + "%llx, erase_size %x\n", ofs, len, mtd->size, mtd->erasesize);
128 + instr->state = MTD_ERASE_DONE;
129 + mtd_erase_callback(instr);
134 + printk("goldfish_nand_erase: invalid erase, start %llx, len %x, dev_size "
135 + "%llx, erase_size %x\n", ofs, len, mtd->size, mtd->erasesize);
139 +static int goldfish_nand_read_oob(struct mtd_info *mtd, loff_t ofs,
140 + struct mtd_oob_ops *ops)
144 + if(ofs + ops->len > mtd->size)
146 + if(ops->datbuf && ops->len && ops->len != mtd->writesize)
148 + if(ops->ooblen + ops->ooboffs > mtd->oobsize)
151 + rem = do_div(ofs, mtd->writesize);
154 + ofs *= (mtd->writesize + mtd->oobsize);
157 + ops->retlen = goldfish_nand_cmd(mtd, NAND_CMD_READ, ofs,
158 + ops->len, ops->datbuf);
159 + ofs += mtd->writesize + ops->ooboffs;
161 + ops->oobretlen = goldfish_nand_cmd(mtd, NAND_CMD_READ, ofs,
162 + ops->ooblen, ops->oobbuf);
166 + printk("goldfish_nand_read_oob: invalid read, start %llx, len %x, "
167 + "ooblen %x, dev_size %llx, write_size %x\n",
168 + ofs, ops->len, ops->ooblen, mtd->size, mtd->writesize);
172 +static int goldfish_nand_write_oob(struct mtd_info *mtd, loff_t ofs,
173 + struct mtd_oob_ops *ops)
177 + if(ofs + ops->len > mtd->size)
179 + if(ops->len && ops->len != mtd->writesize)
181 + if(ops->ooblen + ops->ooboffs > mtd->oobsize)
184 + rem = do_div(ofs, mtd->writesize);
187 + ofs *= (mtd->writesize + mtd->oobsize);
190 + ops->retlen = goldfish_nand_cmd(mtd, NAND_CMD_WRITE, ofs,
191 + ops->len, ops->datbuf);
192 + ofs += mtd->writesize + ops->ooboffs;
194 + ops->oobretlen = goldfish_nand_cmd(mtd, NAND_CMD_WRITE, ofs,
195 + ops->ooblen, ops->oobbuf);
199 + printk("goldfish_nand_write_oob: invalid write, start %llx, len %x, "
200 + "ooblen %x, dev_size %llx, write_size %x\n",
201 + ofs, ops->len, ops->ooblen, mtd->size, mtd->writesize);
205 +static int goldfish_nand_read(struct mtd_info *mtd, loff_t from, size_t len,
206 + size_t *retlen, u_char *buf)
210 + if(from + len > mtd->size)
212 + if(len != mtd->writesize)
215 + rem = do_div(from, mtd->writesize);
218 + from *= (mtd->writesize + mtd->oobsize);
220 + *retlen = goldfish_nand_cmd(mtd, NAND_CMD_READ, from, len, buf);
224 + printk("goldfish_nand_read: invalid read, start %llx, len %x, dev_size %llx"
225 + ", write_size %x\n", from, len, mtd->size, mtd->writesize);
229 +static int goldfish_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
230 + size_t *retlen, const u_char *buf)
234 + if(to + len > mtd->size)
236 + if(len != mtd->writesize)
239 + rem = do_div(to, mtd->writesize);
242 + to *= (mtd->writesize + mtd->oobsize);
244 + *retlen = goldfish_nand_cmd(mtd, NAND_CMD_WRITE, to, len, (void *)buf);
248 + printk("goldfish_nand_write: invalid write, start %llx, len %x, dev_size %llx"
249 + ", write_size %x\n", to, len, mtd->size, mtd->writesize);
253 +static int goldfish_nand_block_isbad(struct mtd_info *mtd, loff_t ofs)
257 + if(ofs >= mtd->size)
260 + rem = do_div(ofs, mtd->erasesize);
263 + ofs *= mtd->erasesize / mtd->writesize;
264 + ofs *= (mtd->writesize + mtd->oobsize);
266 + return goldfish_nand_cmd(mtd, NAND_CMD_BLOCK_BAD_GET, ofs, 0, NULL);
269 + printk("goldfish_nand_block_isbad: invalid arg, ofs %llx, dev_size %llx, "
270 + "write_size %x\n", ofs, mtd->size, mtd->writesize);
274 +static int goldfish_nand_block_markbad(struct mtd_info *mtd, loff_t ofs)
278 + if(ofs >= mtd->size)
281 + rem = do_div(ofs, mtd->erasesize);
284 + ofs *= mtd->erasesize / mtd->writesize;
285 + ofs *= (mtd->writesize + mtd->oobsize);
287 + if(goldfish_nand_cmd(mtd, NAND_CMD_BLOCK_BAD_SET, ofs, 0, NULL) != 1)
292 + printk("goldfish_nand_block_markbad: invalid arg, ofs %llx, dev_size %llx, "
293 + "write_size %x\n", ofs, mtd->size, mtd->writesize);
297 +static int goldfish_nand_init_device(struct goldfish_nand *nand, int id)
302 + unsigned long irq_flags;
303 + unsigned char __iomem *base = nand->base;
304 + struct mtd_info *mtd = &nand->mtd[id];
307 + spin_lock_irqsave(&nand->lock, irq_flags);
308 + writel(id, base + NAND_DEV);
309 + flags = readl(base + NAND_DEV_FLAGS);
310 + name_len = readl(base + NAND_DEV_NAME_LEN);
311 + mtd->writesize = readl(base + NAND_DEV_PAGE_SIZE);
312 + mtd->size = readl(base + NAND_DEV_SIZE_LOW);
313 + mtd->size |= (uint64_t)readl(base + NAND_DEV_SIZE_HIGH) << 32;
314 + mtd->oobsize = readl(base + NAND_DEV_EXTRA_SIZE);
315 + mtd->oobavail = mtd->oobsize;
316 + mtd->erasesize = readl(base + NAND_DEV_ERASE_SIZE) /
317 + (mtd->writesize + mtd->oobsize) * mtd->writesize;
318 + do_div(mtd->size, mtd->writesize + mtd->oobsize);
319 + mtd->size *= mtd->writesize;
320 + printk("goldfish nand dev%d: size %llx, page %d, extra %d, erase %d\n",
321 + id, mtd->size, mtd->writesize, mtd->oobsize, mtd->erasesize);
322 + spin_unlock_irqrestore(&nand->lock, irq_flags);
326 + mtd->name = name = kmalloc(name_len + 1, GFP_KERNEL);
330 + result = goldfish_nand_cmd(mtd, NAND_CMD_GET_DEV_NAME, 0, name_len, name);
331 + if(result != name_len) {
334 + printk("goldfish_nand_init_device failed to get dev name %d != %d\n",
338 + ((char *) mtd->name)[name_len] = '\0';
340 + /* Setup the MTD structure */
341 + mtd->type = MTD_NANDFLASH;
342 + mtd->flags = MTD_CAP_NANDFLASH;
343 + if(flags & NAND_DEV_FLAG_READ_ONLY)
344 + mtd->flags &= ~MTD_WRITEABLE;
346 + mtd->owner = THIS_MODULE;
347 + mtd->erase = goldfish_nand_erase;
348 + mtd->read = goldfish_nand_read;
349 + mtd->write = goldfish_nand_write;
350 + mtd->read_oob = goldfish_nand_read_oob;
351 + mtd->write_oob = goldfish_nand_write_oob;
352 + mtd->block_isbad = goldfish_nand_block_isbad;
353 + mtd->block_markbad = goldfish_nand_block_markbad;
355 + if (add_mtd_device(mtd)) {
364 +static int goldfish_nand_probe(struct platform_device *pdev)
369 + uint32_t num_dev_working;
371 + struct resource *r;
372 + struct goldfish_nand *nand;
373 + unsigned char __iomem *base;
375 + r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
378 + goto err_no_io_base;
381 + base = ioremap(r->start, PAGE_SIZE);
386 + version = readl(base + NAND_VERSION);
387 + if(version != NAND_VERSION_CURRENT) {
388 + printk("goldfish_nand_init: version mismatch, got %d, expected %d\n",
389 + version, NAND_VERSION_CURRENT);
393 + num_dev = readl(base + NAND_NUM_DEV);
399 + nand = kzalloc(sizeof(*nand) + sizeof(struct mtd_info) * num_dev, GFP_KERNEL);
402 + goto err_nand_alloc_failed;
404 + spin_lock_init(&nand->lock);
406 + nand->mtd_count = num_dev;
407 + platform_set_drvdata(pdev, nand);
409 + num_dev_working = 0;
410 + for(i = 0; i < num_dev; i++) {
411 + err = goldfish_nand_init_device(nand, i);
415 + if(num_dev_working == 0) {
417 + goto err_no_working_dev;
423 +err_nand_alloc_failed:
431 +static int goldfish_nand_remove(struct platform_device *pdev)
433 + struct goldfish_nand *nand = platform_get_drvdata(pdev);
435 + for(i = 0; i < nand->mtd_count; i++) {
436 + if(nand->mtd[i].name) {
437 + del_mtd_device(&nand->mtd[i]);
438 + kfree(nand->mtd[i].name);
441 + iounmap(nand->base);
446 +static struct platform_driver goldfish_nand_driver = {
447 + .probe = goldfish_nand_probe,
448 + .remove = goldfish_nand_remove,
450 + .name = "goldfish_nand"
454 +static int __init goldfish_nand_init(void)
456 + return platform_driver_register(&goldfish_nand_driver);
459 +static void __exit goldfish_nand_exit(void)
461 + platform_driver_unregister(&goldfish_nand_driver);
465 +module_init(goldfish_nand_init);
466 +module_exit(goldfish_nand_exit);
468 diff --git a/drivers/mtd/devices/goldfish_nand_reg.h b/drivers/mtd/devices/goldfish_nand_reg.h
470 index 0000000..7c17a44
472 +++ b/drivers/mtd/devices/goldfish_nand_reg.h
474 +/* drivers/mtd/devices/goldfish_nand_reg.h
476 +** Copyright (C) 2007 Google, Inc.
478 +** This software is licensed under the terms of the GNU General Public
479 +** License version 2, as published by the Free Software Foundation, and
480 +** may be copied, distributed, and modified under those terms.
482 +** This program is distributed in the hope that it will be useful,
483 +** but WITHOUT ANY WARRANTY; without even the implied warranty of
484 +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
485 +** GNU General Public License for more details.
489 +#ifndef GOLDFISH_NAND_REG_H
490 +#define GOLDFISH_NAND_REG_H
493 + NAND_CMD_GET_DEV_NAME, // Write device name for NAND_DEV to NAND_DATA (vaddr)
497 + NAND_CMD_BLOCK_BAD_GET, // NAND_RESULT is 1 if block is bad, 0 if it is not
498 + NAND_CMD_BLOCK_BAD_SET
501 +enum nand_dev_flags {
502 + NAND_DEV_FLAG_READ_ONLY = 0x00000001
505 +#define NAND_VERSION_CURRENT (1)
509 + NAND_VERSION = 0x000,
510 + NAND_NUM_DEV = 0x004,
514 + NAND_DEV_FLAGS = 0x010,
515 + NAND_DEV_NAME_LEN = 0x014,
516 + NAND_DEV_PAGE_SIZE = 0x018,
517 + NAND_DEV_EXTRA_SIZE = 0x01c,
518 + NAND_DEV_ERASE_SIZE = 0x020,
519 + NAND_DEV_SIZE_LOW = 0x028,
520 + NAND_DEV_SIZE_HIGH = 0x02c,
523 + NAND_RESULT = 0x040,
524 + NAND_COMMAND = 0x044,
526 + NAND_TRANSFER_SIZE = 0x04c,
527 + NAND_ADDR_LOW = 0x050,
528 + NAND_ADDR_HIGH = 0x054,