2 * Copyright (C) 2006 Compulab, Ltd.
3 * Mike Rapoport <mike@compulab.co.il>
5 * Derived from drivers/mtd/nand/h1910.c (removed in v3.10)
6 * Copyright (C) 2002 Marius Gröger (mag@sysgo.de)
7 * Copyright (c) 2001 Thomas Gleixner (gleixner@autronix.de)
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
15 * This is a device driver for the NAND flash device found on the
19 #include <linux/mtd/rawnand.h>
20 #include <linux/mtd/partitions.h>
21 #include <linux/slab.h>
22 #include <linux/gpio.h>
23 #include <linux/module.h>
27 #include <asm/mach-types.h>
29 #include <mach/pxa2xx-regs.h>
31 #define GPIO_NAND_CS (11)
32 #define GPIO_NAND_RB (89)
34 /* MTD structure for CM-X270 board */
35 static struct mtd_info *cmx270_nand_mtd;
37 /* remaped IO address of the device */
38 static void __iomem *cmx270_nand_io;
41 * Define static partitions for flash device
43 static const struct mtd_partition partition_info[] = {
47 .size = MTDPART_SIZ_FULL
50 #define NUM_PARTITIONS (ARRAY_SIZE(partition_info))
52 static u_char cmx270_read_byte(struct nand_chip *this)
54 return (readl(this->legacy.IO_ADDR_R) >> 16);
57 static void cmx270_write_buf(struct nand_chip *this, const u_char *buf,
63 writel((*buf++ << 16), this->legacy.IO_ADDR_W);
66 static void cmx270_read_buf(struct nand_chip *this, u_char *buf, int len)
71 *buf++ = readl(this->legacy.IO_ADDR_R) >> 16;
74 static inline void nand_cs_on(void)
76 gpio_set_value(GPIO_NAND_CS, 0);
79 static void nand_cs_off(void)
83 gpio_set_value(GPIO_NAND_CS, 1);
87 * hardware specific access to control-lines
89 static void cmx270_hwcontrol(struct nand_chip *this, int dat,
92 unsigned int nandaddr = (unsigned int)this->legacy.IO_ADDR_W;
96 if (ctrl & NAND_CTRL_CHANGE) {
97 if ( ctrl & NAND_ALE )
100 nandaddr &= ~(1 << 3);
101 if ( ctrl & NAND_CLE )
102 nandaddr |= (1 << 2);
104 nandaddr &= ~(1 << 2);
105 if ( ctrl & NAND_NCE )
112 this->legacy.IO_ADDR_W = (void __iomem*)nandaddr;
113 if (dat != NAND_CMD_NONE)
114 writel((dat << 16), this->legacy.IO_ADDR_W);
120 * read device ready pin
122 static int cmx270_device_ready(struct nand_chip *this)
126 return (gpio_get_value(GPIO_NAND_RB));
130 * Main initialization routine
132 static int __init cmx270_init(void)
134 struct nand_chip *this;
137 if (!(machine_is_armcore() && cpu_is_pxa27x()))
140 ret = gpio_request(GPIO_NAND_CS, "NAND CS");
142 pr_warn("CM-X270: failed to request NAND CS gpio\n");
146 gpio_direction_output(GPIO_NAND_CS, 1);
148 ret = gpio_request(GPIO_NAND_RB, "NAND R/B");
150 pr_warn("CM-X270: failed to request NAND R/B gpio\n");
151 goto err_gpio_request;
154 gpio_direction_input(GPIO_NAND_RB);
156 /* Allocate memory for MTD device structure and private data */
157 this = kzalloc(sizeof(struct nand_chip), GFP_KERNEL);
163 cmx270_nand_io = ioremap(PXA_CS1_PHYS, 12);
164 if (!cmx270_nand_io) {
165 pr_debug("Unable to ioremap NAND device\n");
170 cmx270_nand_mtd = nand_to_mtd(this);
172 /* Link the private data with the MTD structure */
173 cmx270_nand_mtd->owner = THIS_MODULE;
175 /* insert callbacks */
176 this->legacy.IO_ADDR_R = cmx270_nand_io;
177 this->legacy.IO_ADDR_W = cmx270_nand_io;
178 this->legacy.cmd_ctrl = cmx270_hwcontrol;
179 this->legacy.dev_ready = cmx270_device_ready;
181 /* 15 us command delay time */
182 this->legacy.chip_delay = 20;
183 this->ecc.mode = NAND_ECC_SOFT;
184 this->ecc.algo = NAND_ECC_HAMMING;
186 /* read/write functions */
187 this->legacy.read_byte = cmx270_read_byte;
188 this->legacy.read_buf = cmx270_read_buf;
189 this->legacy.write_buf = cmx270_write_buf;
191 /* Scan to find existence of the device */
192 ret = nand_scan(this, 1);
194 pr_notice("No NAND device\n");
198 /* Register the partitions */
199 ret = mtd_device_register(cmx270_nand_mtd, partition_info,
208 iounmap(cmx270_nand_io);
212 gpio_free(GPIO_NAND_RB);
214 gpio_free(GPIO_NAND_CS);
219 module_init(cmx270_init);
224 static void __exit cmx270_cleanup(void)
226 /* Release resources, unregister device */
227 nand_release(mtd_to_nand(cmx270_nand_mtd));
229 gpio_free(GPIO_NAND_RB);
230 gpio_free(GPIO_NAND_CS);
232 iounmap(cmx270_nand_io);
234 kfree(mtd_to_nand(cmx270_nand_mtd));
236 module_exit(cmx270_cleanup);
238 MODULE_LICENSE("GPL");
239 MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
240 MODULE_DESCRIPTION("NAND flash driver for Compulab CM-X270 Module");