Merge remote-tracking branch 'driver-core/driver-core-next'
[linux-2.6/next.git] / drivers / mtd / nand / cmx270_nand.c
blob31308e694bd3055d4d0d83cedd4b4f5b29920144
1 /*
2 * linux/drivers/mtd/nand/cmx270-nand.c
4 * Copyright (C) 2006 Compulab, Ltd.
5 * Mike Rapoport <mike@compulab.co.il>
7 * Derived from drivers/mtd/nand/h1910.c
8 * Copyright (C) 2002 Marius Gröger (mag@sysgo.de)
9 * Copyright (c) 2001 Thomas Gleixner (gleixner@autronix.de)
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
16 * Overview:
17 * This is a device driver for the NAND flash device found on the
18 * CM-X270 board.
21 #include <linux/mtd/nand.h>
22 #include <linux/mtd/partitions.h>
23 #include <linux/slab.h>
24 #include <linux/gpio.h>
26 #include <asm/io.h>
27 #include <asm/irq.h>
28 #include <asm/mach-types.h>
30 #include <mach/pxa2xx-regs.h>
32 #define GPIO_NAND_CS (11)
33 #define GPIO_NAND_RB (89)
35 /* MTD structure for CM-X270 board */
36 static struct mtd_info *cmx270_nand_mtd;
38 /* remaped IO address of the device */
39 static void __iomem *cmx270_nand_io;
42 * Define static partitions for flash device
44 static struct mtd_partition partition_info[] = {
45 [0] = {
46 .name = "cmx270-0",
47 .offset = 0,
48 .size = MTDPART_SIZ_FULL
51 #define NUM_PARTITIONS (ARRAY_SIZE(partition_info))
53 static u_char cmx270_read_byte(struct mtd_info *mtd)
55 struct nand_chip *this = mtd->priv;
57 return (readl(this->IO_ADDR_R) >> 16);
60 static void cmx270_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
62 int i;
63 struct nand_chip *this = mtd->priv;
65 for (i=0; i<len; i++)
66 writel((*buf++ << 16), this->IO_ADDR_W);
69 static void cmx270_read_buf(struct mtd_info *mtd, u_char *buf, int len)
71 int i;
72 struct nand_chip *this = mtd->priv;
74 for (i=0; i<len; i++)
75 *buf++ = readl(this->IO_ADDR_R) >> 16;
78 static int cmx270_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
80 int i;
81 struct nand_chip *this = mtd->priv;
83 for (i=0; i<len; i++)
84 if (buf[i] != (u_char)(readl(this->IO_ADDR_R) >> 16))
85 return -EFAULT;
87 return 0;
90 static inline void nand_cs_on(void)
92 gpio_set_value(GPIO_NAND_CS, 0);
95 static void nand_cs_off(void)
97 dsb();
99 gpio_set_value(GPIO_NAND_CS, 1);
103 * hardware specific access to control-lines
105 static void cmx270_hwcontrol(struct mtd_info *mtd, int dat,
106 unsigned int ctrl)
108 struct nand_chip* this = mtd->priv;
109 unsigned int nandaddr = (unsigned int)this->IO_ADDR_W;
111 dsb();
113 if (ctrl & NAND_CTRL_CHANGE) {
114 if ( ctrl & NAND_ALE )
115 nandaddr |= (1 << 3);
116 else
117 nandaddr &= ~(1 << 3);
118 if ( ctrl & NAND_CLE )
119 nandaddr |= (1 << 2);
120 else
121 nandaddr &= ~(1 << 2);
122 if ( ctrl & NAND_NCE )
123 nand_cs_on();
124 else
125 nand_cs_off();
128 dsb();
129 this->IO_ADDR_W = (void __iomem*)nandaddr;
130 if (dat != NAND_CMD_NONE)
131 writel((dat << 16), this->IO_ADDR_W);
133 dsb();
137 * read device ready pin
139 static int cmx270_device_ready(struct mtd_info *mtd)
141 dsb();
143 return (gpio_get_value(GPIO_NAND_RB));
147 * Main initialization routine
149 static int __init cmx270_init(void)
151 struct nand_chip *this;
152 int ret;
154 if (!(machine_is_armcore() && cpu_is_pxa27x()))
155 return -ENODEV;
157 ret = gpio_request(GPIO_NAND_CS, "NAND CS");
158 if (ret) {
159 pr_warning("CM-X270: failed to request NAND CS gpio\n");
160 return ret;
163 gpio_direction_output(GPIO_NAND_CS, 1);
165 ret = gpio_request(GPIO_NAND_RB, "NAND R/B");
166 if (ret) {
167 pr_warning("CM-X270: failed to request NAND R/B gpio\n");
168 goto err_gpio_request;
171 gpio_direction_input(GPIO_NAND_RB);
173 /* Allocate memory for MTD device structure and private data */
174 cmx270_nand_mtd = kzalloc(sizeof(struct mtd_info) +
175 sizeof(struct nand_chip),
176 GFP_KERNEL);
177 if (!cmx270_nand_mtd) {
178 pr_debug("Unable to allocate CM-X270 NAND MTD device structure.\n");
179 ret = -ENOMEM;
180 goto err_kzalloc;
183 cmx270_nand_io = ioremap(PXA_CS1_PHYS, 12);
184 if (!cmx270_nand_io) {
185 pr_debug("Unable to ioremap NAND device\n");
186 ret = -EINVAL;
187 goto err_ioremap;
190 /* Get pointer to private data */
191 this = (struct nand_chip *)(&cmx270_nand_mtd[1]);
193 /* Link the private data with the MTD structure */
194 cmx270_nand_mtd->owner = THIS_MODULE;
195 cmx270_nand_mtd->priv = this;
197 /* insert callbacks */
198 this->IO_ADDR_R = cmx270_nand_io;
199 this->IO_ADDR_W = cmx270_nand_io;
200 this->cmd_ctrl = cmx270_hwcontrol;
201 this->dev_ready = cmx270_device_ready;
203 /* 15 us command delay time */
204 this->chip_delay = 20;
205 this->ecc.mode = NAND_ECC_SOFT;
207 /* read/write functions */
208 this->read_byte = cmx270_read_byte;
209 this->read_buf = cmx270_read_buf;
210 this->write_buf = cmx270_write_buf;
211 this->verify_buf = cmx270_verify_buf;
213 /* Scan to find existence of the device */
214 if (nand_scan (cmx270_nand_mtd, 1)) {
215 pr_notice("No NAND device\n");
216 ret = -ENXIO;
217 goto err_scan;
220 /* Register the partitions */
221 ret = mtd_device_parse_register(cmx270_nand_mtd, NULL, 0,
222 partition_info, NUM_PARTITIONS);
223 if (ret)
224 goto err_scan;
226 /* Return happy */
227 return 0;
229 err_scan:
230 iounmap(cmx270_nand_io);
231 err_ioremap:
232 kfree(cmx270_nand_mtd);
233 err_kzalloc:
234 gpio_free(GPIO_NAND_RB);
235 err_gpio_request:
236 gpio_free(GPIO_NAND_CS);
238 return ret;
241 module_init(cmx270_init);
244 * Clean up routine
246 static void __exit cmx270_cleanup(void)
248 /* Release resources, unregister device */
249 nand_release(cmx270_nand_mtd);
251 gpio_free(GPIO_NAND_RB);
252 gpio_free(GPIO_NAND_CS);
254 iounmap(cmx270_nand_io);
256 /* Free the MTD device structure */
257 kfree (cmx270_nand_mtd);
259 module_exit(cmx270_cleanup);
261 MODULE_LICENSE("GPL");
262 MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
263 MODULE_DESCRIPTION("NAND flash driver for Compulab CM-X270 Module");