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>
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
[] = {
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
)
63 struct nand_chip
*this = mtd
->priv
;
66 writel((*buf
++ << 16), this->IO_ADDR_W
);
69 static void cmx270_read_buf(struct mtd_info
*mtd
, u_char
*buf
, int len
)
72 struct nand_chip
*this = mtd
->priv
;
75 *buf
++ = readl(this->IO_ADDR_R
) >> 16;
78 static int cmx270_verify_buf(struct mtd_info
*mtd
, const u_char
*buf
, int len
)
81 struct nand_chip
*this = mtd
->priv
;
84 if (buf
[i
] != (u_char
)(readl(this->IO_ADDR_R
) >> 16))
90 static inline void nand_cs_on(void)
92 gpio_set_value(GPIO_NAND_CS
, 0);
95 static void nand_cs_off(void)
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
,
108 struct nand_chip
* this = mtd
->priv
;
109 unsigned int nandaddr
= (unsigned int)this->IO_ADDR_W
;
113 if (ctrl
& NAND_CTRL_CHANGE
) {
114 if ( ctrl
& NAND_ALE
)
115 nandaddr
|= (1 << 3);
117 nandaddr
&= ~(1 << 3);
118 if ( ctrl
& NAND_CLE
)
119 nandaddr
|= (1 << 2);
121 nandaddr
&= ~(1 << 2);
122 if ( ctrl
& NAND_NCE
)
129 this->IO_ADDR_W
= (void __iomem
*)nandaddr
;
130 if (dat
!= NAND_CMD_NONE
)
131 writel((dat
<< 16), this->IO_ADDR_W
);
137 * read device ready pin
139 static int cmx270_device_ready(struct mtd_info
*mtd
)
143 return (gpio_get_value(GPIO_NAND_RB
));
147 * Main initialization routine
149 static int __init
cmx270_init(void)
151 struct nand_chip
*this;
154 if (!(machine_is_armcore() && cpu_is_pxa27x()))
157 ret
= gpio_request(GPIO_NAND_CS
, "NAND CS");
159 pr_warning("CM-X270: failed to request NAND CS gpio\n");
163 gpio_direction_output(GPIO_NAND_CS
, 1);
165 ret
= gpio_request(GPIO_NAND_RB
, "NAND R/B");
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
),
177 if (!cmx270_nand_mtd
) {
178 pr_debug("Unable to allocate CM-X270 NAND MTD device structure.\n");
183 cmx270_nand_io
= ioremap(PXA_CS1_PHYS
, 12);
184 if (!cmx270_nand_io
) {
185 pr_debug("Unable to ioremap NAND device\n");
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");
220 /* Register the partitions */
221 ret
= mtd_device_parse_register(cmx270_nand_mtd
, NULL
, 0,
222 partition_info
, NUM_PARTITIONS
);
230 iounmap(cmx270_nand_io
);
232 kfree(cmx270_nand_mtd
);
234 gpio_free(GPIO_NAND_RB
);
236 gpio_free(GPIO_NAND_CS
);
241 module_init(cmx270_init
);
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");