2 * Copyright (C) 2006 Compulab, Ltd.
3 * Mike Rapoport <mike@compulab.co.il>
5 * Derived from drivers/mtd/nand/h1910.c (removed in v3.10)
6 * Copyright (C) 2002 Marius Gröger (mag@sysgo.de)
7 * Copyright (c) 2001 Thomas Gleixner (gleixner@autronix.de)
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
15 * This is a device driver for the NAND flash device found on the
19 #include <linux/mtd/rawnand.h>
20 #include <linux/mtd/partitions.h>
21 #include <linux/slab.h>
22 #include <linux/gpio.h>
23 #include <linux/module.h>
27 #include <asm/mach-types.h>
29 #include <mach/pxa2xx-regs.h>
31 #define GPIO_NAND_CS (11)
32 #define GPIO_NAND_RB (89)
34 /* MTD structure for CM-X270 board */
35 static struct mtd_info *cmx270_nand_mtd;
37 /* remaped IO address of the device */
38 static void __iomem *cmx270_nand_io;
41 * Define static partitions for flash device
43 static const struct mtd_partition partition_info[] = {
47 .size = MTDPART_SIZ_FULL
50 #define NUM_PARTITIONS (ARRAY_SIZE(partition_info))
52 static u_char cmx270_read_byte(struct mtd_info *mtd)
54 struct nand_chip *this = mtd_to_nand(mtd);
56 return (readl(this->IO_ADDR_R) >> 16);
59 static void cmx270_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
62 struct nand_chip *this = mtd_to_nand(mtd);
65 writel((*buf++ << 16), this->IO_ADDR_W);
68 static void cmx270_read_buf(struct mtd_info *mtd, u_char *buf, int len)
71 struct nand_chip *this = mtd_to_nand(mtd);
74 *buf++ = readl(this->IO_ADDR_R) >> 16;
77 static inline void nand_cs_on(void)
79 gpio_set_value(GPIO_NAND_CS, 0);
82 static void nand_cs_off(void)
86 gpio_set_value(GPIO_NAND_CS, 1);
90 * hardware specific access to control-lines
92 static void cmx270_hwcontrol(struct mtd_info *mtd, int dat,
95 struct nand_chip *this = mtd_to_nand(mtd);
96 unsigned int nandaddr = (unsigned int)this->IO_ADDR_W;
100 if (ctrl & NAND_CTRL_CHANGE) {
101 if ( ctrl & NAND_ALE )
102 nandaddr |= (1 << 3);
104 nandaddr &= ~(1 << 3);
105 if ( ctrl & NAND_CLE )
106 nandaddr |= (1 << 2);
108 nandaddr &= ~(1 << 2);
109 if ( ctrl & NAND_NCE )
116 this->IO_ADDR_W = (void __iomem*)nandaddr;
117 if (dat != NAND_CMD_NONE)
118 writel((dat << 16), this->IO_ADDR_W);
124 * read device ready pin
126 static int cmx270_device_ready(struct mtd_info *mtd)
130 return (gpio_get_value(GPIO_NAND_RB));
134 * Main initialization routine
136 static int __init cmx270_init(void)
138 struct nand_chip *this;
141 if (!(machine_is_armcore() && cpu_is_pxa27x()))
144 ret = gpio_request(GPIO_NAND_CS, "NAND CS");
146 pr_warn("CM-X270: failed to request NAND CS gpio\n");
150 gpio_direction_output(GPIO_NAND_CS, 1);
152 ret = gpio_request(GPIO_NAND_RB, "NAND R/B");
154 pr_warn("CM-X270: failed to request NAND R/B gpio\n");
155 goto err_gpio_request;
158 gpio_direction_input(GPIO_NAND_RB);
160 /* Allocate memory for MTD device structure and private data */
161 this = kzalloc(sizeof(struct nand_chip), GFP_KERNEL);
167 cmx270_nand_io = ioremap(PXA_CS1_PHYS, 12);
168 if (!cmx270_nand_io) {
169 pr_debug("Unable to ioremap NAND device\n");
174 cmx270_nand_mtd = nand_to_mtd(this);
176 /* Link the private data with the MTD structure */
177 cmx270_nand_mtd->owner = THIS_MODULE;
179 /* insert callbacks */
180 this->IO_ADDR_R = cmx270_nand_io;
181 this->IO_ADDR_W = cmx270_nand_io;
182 this->cmd_ctrl = cmx270_hwcontrol;
183 this->dev_ready = cmx270_device_ready;
185 /* 15 us command delay time */
186 this->chip_delay = 20;
187 this->ecc.mode = NAND_ECC_SOFT;
188 this->ecc.algo = NAND_ECC_HAMMING;
190 /* read/write functions */
191 this->read_byte = cmx270_read_byte;
192 this->read_buf = cmx270_read_buf;
193 this->write_buf = cmx270_write_buf;
195 /* Scan to find existence of the device */
196 ret = nand_scan(this, 1);
198 pr_notice("No NAND device\n");
202 /* Register the partitions */
203 ret = mtd_device_register(cmx270_nand_mtd, partition_info,
212 iounmap(cmx270_nand_io);
216 gpio_free(GPIO_NAND_RB);
218 gpio_free(GPIO_NAND_CS);
223 module_init(cmx270_init);
228 static void __exit cmx270_cleanup(void)
230 /* Release resources, unregister device */
231 nand_release(mtd_to_nand(cmx270_nand_mtd));
233 gpio_free(GPIO_NAND_RB);
234 gpio_free(GPIO_NAND_CS);
236 iounmap(cmx270_nand_io);
238 kfree(mtd_to_nand(cmx270_nand_mtd));
240 module_exit(cmx270_cleanup);
242 MODULE_LICENSE("GPL");
243 MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
244 MODULE_DESCRIPTION("NAND flash driver for Compulab CM-X270 Module");