2 * linux/drivers/mtd/nand/cmx270-nand.c
4 * Copyright (C) 2006 Compulab, Ltd.
7 * Derived from drivers/mtd/nand/h1910.c
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 static u_char cmx270_read_byte(struct mtd_info *mtd)
56 struct nand_chip *this = mtd_to_nand(mtd);
58 return (readl(this->IO_ADDR_R) >> 16);
61 static void cmx270_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
64 struct nand_chip *this = mtd_to_nand(mtd);
67 writel((*buf++ << 16), this->IO_ADDR_W);
70 static void cmx270_read_buf(struct mtd_info *mtd, u_char *buf, int len)
73 struct nand_chip *this = mtd_to_nand(mtd);
76 *buf++ = readl(this->IO_ADDR_R) >> 16;
79 static inline void nand_cs_on(void)
81 gpio_set_value(GPIO_NAND_CS, 0);
84 static void nand_cs_off(void)
88 gpio_set_value(GPIO_NAND_CS, 1);
92 * hardware specific access to control-lines
94 static void cmx270_hwcontrol(struct mtd_info *mtd, int dat,
97 struct nand_chip *this = mtd_to_nand(mtd);
98 unsigned int nandaddr = (unsigned int)this->IO_ADDR_W;
102 if (ctrl & NAND_CTRL_CHANGE) {
103 if ( ctrl & NAND_ALE )
104 nandaddr |= (1 << 3);
106 nandaddr &= ~(1 << 3);
107 if ( ctrl & NAND_CLE )
108 nandaddr |= (1 << 2);
110 nandaddr &= ~(1 << 2);
111 if ( ctrl & NAND_NCE )
118 this->IO_ADDR_W = (void __iomem*)nandaddr;
119 if (dat != NAND_CMD_NONE)
120 writel((dat << 16), this->IO_ADDR_W);
126 * read device ready pin
128 static int cmx270_device_ready(struct mtd_info *mtd)
132 return (gpio_get_value(GPIO_NAND_RB));
136 * Main initialization routine
138 static int __init cmx270_init(void)
140 struct nand_chip *this;
143 if (!(machine_is_armcore() && cpu_is_pxa27x()))
146 ret = gpio_request(GPIO_NAND_CS, "NAND CS");
148 pr_warning("CM-X270: failed to request NAND CS gpio\n");
152 gpio_direction_output(GPIO_NAND_CS, 1);
154 ret = gpio_request(GPIO_NAND_RB, "NAND R/B");
156 pr_warning("CM-X270: failed to request NAND R/B gpio\n");
157 goto err_gpio_request;
160 gpio_direction_input(GPIO_NAND_RB);
162 /* Allocate memory for MTD device structure and private data */
163 this = kzalloc(sizeof(struct nand_chip), GFP_KERNEL);
169 cmx270_nand_io = ioremap(PXA_CS1_PHYS, 12);
170 if (!cmx270_nand_io) {
171 pr_debug("Unable to ioremap NAND device\n");
176 cmx270_nand_mtd = nand_to_mtd(this);
178 /* Link the private data with the MTD structure */
179 cmx270_nand_mtd->owner = THIS_MODULE;
181 /* insert callbacks */
182 this->IO_ADDR_R = cmx270_nand_io;
183 this->IO_ADDR_W = cmx270_nand_io;
184 this->cmd_ctrl = cmx270_hwcontrol;
185 this->dev_ready = cmx270_device_ready;
187 /* 15 us command delay time */
188 this->chip_delay = 20;
189 this->ecc.mode = NAND_ECC_SOFT;
190 this->ecc.algo = NAND_ECC_HAMMING;
192 /* read/write functions */
193 this->read_byte = cmx270_read_byte;
194 this->read_buf = cmx270_read_buf;
195 this->write_buf = cmx270_write_buf;
197 /* Scan to find existence of the device */
198 if (nand_scan (cmx270_nand_mtd, 1)) {
199 pr_notice("No NAND device\n");
204 /* Register the partitions */
205 ret = mtd_device_parse_register(cmx270_nand_mtd, NULL, NULL,
206 partition_info, NUM_PARTITIONS);
214 iounmap(cmx270_nand_io);
218 gpio_free(GPIO_NAND_RB);
220 gpio_free(GPIO_NAND_CS);
225 module_init(cmx270_init);
230 static void __exit cmx270_cleanup(void)
232 /* Release resources, unregister device */
233 nand_release(cmx270_nand_mtd);
235 gpio_free(GPIO_NAND_RB);
236 gpio_free(GPIO_NAND_CS);
238 iounmap(cmx270_nand_io);
240 kfree(mtd_to_nand(cmx270_nand_mtd));
242 module_exit(cmx270_cleanup);
244 MODULE_LICENSE("GPL");
246 MODULE_DESCRIPTION("NAND flash driver for Compulab CM-X270 Module");