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.
17 * This is a device driver for the NAND flash device found on the
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>
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
[] = {
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
)
66 struct nand_chip
*this = mtd
->priv
;
69 writel((*buf
++ << 16), this->IO_ADDR_W
);
72 static void cmx270_read_buf(struct mtd_info
*mtd
, u_char
*buf
, int len
)
75 struct nand_chip
*this = mtd
->priv
;
78 *buf
++ = readl(this->IO_ADDR_R
) >> 16;
81 static int cmx270_verify_buf(struct mtd_info
*mtd
, const u_char
*buf
, int len
)
84 struct nand_chip
*this = mtd
->priv
;
87 if (buf
[i
] != (u_char
)(readl(this->IO_ADDR_R
) >> 16))
93 static inline void nand_cs_on(void)
95 gpio_set_value(GPIO_NAND_CS
, 0);
98 static void nand_cs_off(void)
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
,
111 struct nand_chip
* this = mtd
->priv
;
112 unsigned int nandaddr
= (unsigned int)this->IO_ADDR_W
;
116 if (ctrl
& NAND_CTRL_CHANGE
) {
117 if ( ctrl
& NAND_ALE
)
118 nandaddr
|= (1 << 3);
120 nandaddr
&= ~(1 << 3);
121 if ( ctrl
& NAND_CLE
)
122 nandaddr
|= (1 << 2);
124 nandaddr
&= ~(1 << 2);
125 if ( ctrl
& NAND_NCE
)
132 this->IO_ADDR_W
= (void __iomem
*)nandaddr
;
133 if (dat
!= NAND_CMD_NONE
)
134 writel((dat
<< 16), this->IO_ADDR_W
);
140 * read device ready pin
142 static int cmx270_device_ready(struct mtd_info
*mtd
)
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;
160 if (!(machine_is_armcore() && cpu_is_pxa27x()))
163 ret
= gpio_request(GPIO_NAND_CS
, "NAND CS");
165 pr_warning("CM-X270: failed to request NAND CS gpio\n");
169 gpio_direction_output(GPIO_NAND_CS
, 1);
171 ret
= gpio_request(GPIO_NAND_RB
, "NAND R/B");
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
),
183 if (!cmx270_nand_mtd
) {
184 pr_debug("Unable to allocate CM-X270 NAND MTD device structure.\n");
189 cmx270_nand_io
= ioremap(PXA_CS1_PHYS
, 12);
190 if (!cmx270_nand_io
) {
191 pr_debug("Unable to ioremap NAND device\n");
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");
226 #ifdef CONFIG_MTD_CMDLINE_PARTS
227 mtd_parts_nb
= parse_mtd_partitions(cmx270_nand_mtd
, part_probes
,
229 if (mtd_parts_nb
> 0)
230 part_type
= "command line";
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
);
250 iounmap(cmx270_nand_io
);
252 kfree(cmx270_nand_mtd
);
254 gpio_free(GPIO_NAND_RB
);
256 gpio_free(GPIO_NAND_CS
);
261 module_init(cmx270_init
);
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");