include: replace linux/module.h with "struct module" wherever possible
[linux-2.6/next.git] / drivers / mtd / nand / cmx270_nand.c
blobbe33b0f4634dcc754fe3713c169735d1f39f6c1d
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>
25 #include <linux/module.h>
27 #include <asm/io.h>
28 #include <asm/irq.h>
29 #include <asm/mach-types.h>
31 #include <mach/pxa2xx-regs.h>
33 #define GPIO_NAND_CS (11)
34 #define GPIO_NAND_RB (89)
36 /* MTD structure for CM-X270 board */
37 static struct mtd_info *cmx270_nand_mtd;
39 /* remaped IO address of the device */
40 static void __iomem *cmx270_nand_io;
43 * Define static partitions for flash device
45 static struct mtd_partition partition_info[] = {
46 [0] = {
47 .name = "cmx270-0",
48 .offset = 0,
49 .size = MTDPART_SIZ_FULL
52 #define NUM_PARTITIONS (ARRAY_SIZE(partition_info))
54 const char *part_probes[] = { "cmdlinepart", NULL };
56 static u_char cmx270_read_byte(struct mtd_info *mtd)
58 struct nand_chip *this = mtd->priv;
60 return (readl(this->IO_ADDR_R) >> 16);
63 static void cmx270_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
65 int i;
66 struct nand_chip *this = mtd->priv;
68 for (i=0; i<len; i++)
69 writel((*buf++ << 16), this->IO_ADDR_W);
72 static void cmx270_read_buf(struct mtd_info *mtd, u_char *buf, int len)
74 int i;
75 struct nand_chip *this = mtd->priv;
77 for (i=0; i<len; i++)
78 *buf++ = readl(this->IO_ADDR_R) >> 16;
81 static int cmx270_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
83 int i;
84 struct nand_chip *this = mtd->priv;
86 for (i=0; i<len; i++)
87 if (buf[i] != (u_char)(readl(this->IO_ADDR_R) >> 16))
88 return -EFAULT;
90 return 0;
93 static inline void nand_cs_on(void)
95 gpio_set_value(GPIO_NAND_CS, 0);
98 static void nand_cs_off(void)
100 dsb();
102 gpio_set_value(GPIO_NAND_CS, 1);
106 * hardware specific access to control-lines
108 static void cmx270_hwcontrol(struct mtd_info *mtd, int dat,
109 unsigned int ctrl)
111 struct nand_chip* this = mtd->priv;
112 unsigned int nandaddr = (unsigned int)this->IO_ADDR_W;
114 dsb();
116 if (ctrl & NAND_CTRL_CHANGE) {
117 if ( ctrl & NAND_ALE )
118 nandaddr |= (1 << 3);
119 else
120 nandaddr &= ~(1 << 3);
121 if ( ctrl & NAND_CLE )
122 nandaddr |= (1 << 2);
123 else
124 nandaddr &= ~(1 << 2);
125 if ( ctrl & NAND_NCE )
126 nand_cs_on();
127 else
128 nand_cs_off();
131 dsb();
132 this->IO_ADDR_W = (void __iomem*)nandaddr;
133 if (dat != NAND_CMD_NONE)
134 writel((dat << 16), this->IO_ADDR_W);
136 dsb();
140 * read device ready pin
142 static int cmx270_device_ready(struct mtd_info *mtd)
144 dsb();
146 return (gpio_get_value(GPIO_NAND_RB));
150 * Main initialization routine
152 static int __init cmx270_init(void)
154 struct nand_chip *this;
155 const char *part_type;
156 struct mtd_partition *mtd_parts;
157 int mtd_parts_nb = 0;
158 int ret;
160 if (!(machine_is_armcore() && cpu_is_pxa27x()))
161 return -ENODEV;
163 ret = gpio_request(GPIO_NAND_CS, "NAND CS");
164 if (ret) {
165 pr_warning("CM-X270: failed to request NAND CS gpio\n");
166 return ret;
169 gpio_direction_output(GPIO_NAND_CS, 1);
171 ret = gpio_request(GPIO_NAND_RB, "NAND R/B");
172 if (ret) {
173 pr_warning("CM-X270: failed to request NAND R/B gpio\n");
174 goto err_gpio_request;
177 gpio_direction_input(GPIO_NAND_RB);
179 /* Allocate memory for MTD device structure and private data */
180 cmx270_nand_mtd = kzalloc(sizeof(struct mtd_info) +
181 sizeof(struct nand_chip),
182 GFP_KERNEL);
183 if (!cmx270_nand_mtd) {
184 pr_debug("Unable to allocate CM-X270 NAND MTD device structure.\n");
185 ret = -ENOMEM;
186 goto err_kzalloc;
189 cmx270_nand_io = ioremap(PXA_CS1_PHYS, 12);
190 if (!cmx270_nand_io) {
191 pr_debug("Unable to ioremap NAND device\n");
192 ret = -EINVAL;
193 goto err_ioremap;
196 /* Get pointer to private data */
197 this = (struct nand_chip *)(&cmx270_nand_mtd[1]);
199 /* Link the private data with the MTD structure */
200 cmx270_nand_mtd->owner = THIS_MODULE;
201 cmx270_nand_mtd->priv = this;
203 /* insert callbacks */
204 this->IO_ADDR_R = cmx270_nand_io;
205 this->IO_ADDR_W = cmx270_nand_io;
206 this->cmd_ctrl = cmx270_hwcontrol;
207 this->dev_ready = cmx270_device_ready;
209 /* 15 us command delay time */
210 this->chip_delay = 20;
211 this->ecc.mode = NAND_ECC_SOFT;
213 /* read/write functions */
214 this->read_byte = cmx270_read_byte;
215 this->read_buf = cmx270_read_buf;
216 this->write_buf = cmx270_write_buf;
217 this->verify_buf = cmx270_verify_buf;
219 /* Scan to find existence of the device */
220 if (nand_scan (cmx270_nand_mtd, 1)) {
221 pr_notice("No NAND device\n");
222 ret = -ENXIO;
223 goto err_scan;
226 #ifdef CONFIG_MTD_CMDLINE_PARTS
227 mtd_parts_nb = parse_mtd_partitions(cmx270_nand_mtd, part_probes,
228 &mtd_parts, 0);
229 if (mtd_parts_nb > 0)
230 part_type = "command line";
231 else
232 mtd_parts_nb = 0;
233 #endif
234 if (!mtd_parts_nb) {
235 mtd_parts = partition_info;
236 mtd_parts_nb = NUM_PARTITIONS;
237 part_type = "static";
240 /* Register the partitions */
241 pr_notice("Using %s partition definition\n", part_type);
242 ret = mtd_device_register(cmx270_nand_mtd, mtd_parts, mtd_parts_nb);
243 if (ret)
244 goto err_scan;
246 /* Return happy */
247 return 0;
249 err_scan:
250 iounmap(cmx270_nand_io);
251 err_ioremap:
252 kfree(cmx270_nand_mtd);
253 err_kzalloc:
254 gpio_free(GPIO_NAND_RB);
255 err_gpio_request:
256 gpio_free(GPIO_NAND_CS);
258 return ret;
261 module_init(cmx270_init);
264 * Clean up routine
266 static void __exit cmx270_cleanup(void)
268 /* Release resources, unregister device */
269 nand_release(cmx270_nand_mtd);
271 gpio_free(GPIO_NAND_RB);
272 gpio_free(GPIO_NAND_CS);
274 iounmap(cmx270_nand_io);
276 /* Free the MTD device structure */
277 kfree (cmx270_nand_mtd);
279 module_exit(cmx270_cleanup);
281 MODULE_LICENSE("GPL");
282 MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
283 MODULE_DESCRIPTION("NAND flash driver for Compulab CM-X270 Module");