Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / drivers / mtd / nand / cmx270_nand.c
blobcb663ef245d53cb5ec8e2c7212b76c0be7a156ab
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>
24 #include <asm/io.h>
25 #include <asm/irq.h>
27 #include <asm/arch/hardware.h>
28 #include <asm/arch/pxa-regs.h>
30 #define GPIO_NAND_CS (11)
31 #define GPIO_NAND_RB (89)
33 /* This macro needed to ensure in-order operation of GPIO and local
34 * bus. Without both asm command and dummy uncached read there're
35 * states when NAND access is broken. I've looked for such macro(s) in
36 * include/asm-arm but found nothing approptiate.
37 * dmac_clean_range is close, but is makes cache invalidation
38 * unnecessary here and it cannot be used in module
40 #define DRAIN_WB() \
41 do { \
42 unsigned char dummy; \
43 asm volatile ("mcr p15, 0, r0, c7, c10, 4":::"r0"); \
44 dummy=*((unsigned char*)UNCACHED_ADDR); \
45 } while(0)
47 /* MTD structure for CM-X270 board */
48 static struct mtd_info *cmx270_nand_mtd;
50 /* remaped IO address of the device */
51 static void __iomem *cmx270_nand_io;
54 * Define static partitions for flash device
56 static struct mtd_partition partition_info[] = {
57 [0] = {
58 .name = "cmx270-0",
59 .offset = 0,
60 .size = MTDPART_SIZ_FULL
63 #define NUM_PARTITIONS (ARRAY_SIZE(partition_info))
65 const char *part_probes[] = { "cmdlinepart", NULL };
67 static u_char cmx270_read_byte(struct mtd_info *mtd)
69 struct nand_chip *this = mtd->priv;
71 return (readl(this->IO_ADDR_R) >> 16);
74 static void cmx270_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
76 int i;
77 struct nand_chip *this = mtd->priv;
79 for (i=0; i<len; i++)
80 writel((*buf++ << 16), this->IO_ADDR_W);
83 static void cmx270_read_buf(struct mtd_info *mtd, u_char *buf, int len)
85 int i;
86 struct nand_chip *this = mtd->priv;
88 for (i=0; i<len; i++)
89 *buf++ = readl(this->IO_ADDR_R) >> 16;
92 static int cmx270_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
94 int i;
95 struct nand_chip *this = mtd->priv;
97 for (i=0; i<len; i++)
98 if (buf[i] != (u_char)(readl(this->IO_ADDR_R) >> 16))
99 return -EFAULT;
101 return 0;
104 static inline void nand_cs_on(void)
106 GPCR(GPIO_NAND_CS) = GPIO_bit(GPIO_NAND_CS);
109 static void nand_cs_off(void)
111 DRAIN_WB();
113 GPSR(GPIO_NAND_CS) = GPIO_bit(GPIO_NAND_CS);
117 * hardware specific access to control-lines
119 static void cmx270_hwcontrol(struct mtd_info *mtd, int dat,
120 unsigned int ctrl)
122 struct nand_chip* this = mtd->priv;
123 unsigned int nandaddr = (unsigned int)this->IO_ADDR_W;
125 DRAIN_WB();
127 if (ctrl & NAND_CTRL_CHANGE) {
128 if ( ctrl & NAND_ALE )
129 nandaddr |= (1 << 3);
130 else
131 nandaddr &= ~(1 << 3);
132 if ( ctrl & NAND_CLE )
133 nandaddr |= (1 << 2);
134 else
135 nandaddr &= ~(1 << 2);
136 if ( ctrl & NAND_NCE )
137 nand_cs_on();
138 else
139 nand_cs_off();
142 DRAIN_WB();
143 this->IO_ADDR_W = (void __iomem*)nandaddr;
144 if (dat != NAND_CMD_NONE)
145 writel((dat << 16), this->IO_ADDR_W);
147 DRAIN_WB();
151 * read device ready pin
153 static int cmx270_device_ready(struct mtd_info *mtd)
155 DRAIN_WB();
157 return (GPLR(GPIO_NAND_RB) & GPIO_bit(GPIO_NAND_RB));
161 * Main initialization routine
163 static int cmx270_init(void)
165 struct nand_chip *this;
166 const char *part_type;
167 struct mtd_partition *mtd_parts;
168 int mtd_parts_nb = 0;
169 int ret;
171 /* Allocate memory for MTD device structure and private data */
172 cmx270_nand_mtd = kzalloc(sizeof(struct mtd_info) +
173 sizeof(struct nand_chip),
174 GFP_KERNEL);
175 if (!cmx270_nand_mtd) {
176 printk("Unable to allocate CM-X270 NAND MTD device structure.\n");
177 return -ENOMEM;
180 cmx270_nand_io = ioremap(PXA_CS1_PHYS, 12);
181 if (!cmx270_nand_io) {
182 printk("Unable to ioremap NAND device\n");
183 ret = -EINVAL;
184 goto err1;
187 /* Get pointer to private data */
188 this = (struct nand_chip *)(&cmx270_nand_mtd[1]);
190 /* Link the private data with the MTD structure */
191 cmx270_nand_mtd->owner = THIS_MODULE;
192 cmx270_nand_mtd->priv = this;
194 /* insert callbacks */
195 this->IO_ADDR_R = cmx270_nand_io;
196 this->IO_ADDR_W = cmx270_nand_io;
197 this->cmd_ctrl = cmx270_hwcontrol;
198 this->dev_ready = cmx270_device_ready;
200 /* 15 us command delay time */
201 this->chip_delay = 20;
202 this->ecc.mode = NAND_ECC_SOFT;
204 /* read/write functions */
205 this->read_byte = cmx270_read_byte;
206 this->read_buf = cmx270_read_buf;
207 this->write_buf = cmx270_write_buf;
208 this->verify_buf = cmx270_verify_buf;
210 /* Scan to find existence of the device */
211 if (nand_scan (cmx270_nand_mtd, 1)) {
212 printk(KERN_NOTICE "No NAND device\n");
213 ret = -ENXIO;
214 goto err2;
217 #ifdef CONFIG_MTD_CMDLINE_PARTS
218 mtd_parts_nb = parse_mtd_partitions(cmx270_nand_mtd, part_probes,
219 &mtd_parts, 0);
220 if (mtd_parts_nb > 0)
221 part_type = "command line";
222 else
223 mtd_parts_nb = 0;
224 #endif
225 if (!mtd_parts_nb) {
226 mtd_parts = partition_info;
227 mtd_parts_nb = NUM_PARTITIONS;
228 part_type = "static";
231 /* Register the partitions */
232 printk(KERN_NOTICE "Using %s partition definition\n", part_type);
233 ret = add_mtd_partitions(cmx270_nand_mtd, mtd_parts, mtd_parts_nb);
234 if (ret)
235 goto err2;
237 /* Return happy */
238 return 0;
240 err2:
241 iounmap(cmx270_nand_io);
242 err1:
243 kfree(cmx270_nand_mtd);
245 return ret;
248 module_init(cmx270_init);
251 * Clean up routine
253 static void cmx270_cleanup(void)
255 /* Release resources, unregister device */
256 nand_release(cmx270_nand_mtd);
258 iounmap(cmx270_nand_io);
260 /* Free the MTD device structure */
261 kfree (cmx270_nand_mtd);
263 module_exit(cmx270_cleanup);
265 MODULE_LICENSE("GPL");
266 MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
267 MODULE_DESCRIPTION("NAND flash driver for Compulab CM-X270 Module");