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>
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
42 unsigned char dummy; \
43 asm volatile ("mcr p15, 0, r0, c7, c10, 4":::"r0"); \
44 dummy=*((unsigned char*)UNCACHED_ADDR); \
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[] = {
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)
77 struct nand_chip *this = mtd->priv;
80 writel((*buf++ << 16), this->IO_ADDR_W);
83 static void cmx270_read_buf(struct mtd_info *mtd, u_char *buf, int len)
86 struct nand_chip *this = mtd->priv;
89 *buf++ = readl(this->IO_ADDR_R) >> 16;
92 static int cmx270_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
95 struct nand_chip *this = mtd->priv;
98 if (buf[i] != (u_char)(readl(this->IO_ADDR_R) >> 16))
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)
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,
122 struct nand_chip* this = mtd->priv;
123 unsigned int nandaddr = (unsigned int)this->IO_ADDR_W;
127 if (ctrl & NAND_CTRL_CHANGE) {
128 if ( ctrl & NAND_ALE )
129 nandaddr |= (1 << 3);
131 nandaddr &= ~(1 << 3);
132 if ( ctrl & NAND_CLE )
133 nandaddr |= (1 << 2);
135 nandaddr &= ~(1 << 2);
136 if ( ctrl & NAND_NCE )
143 this->IO_ADDR_W = (void __iomem*)nandaddr;
144 if (dat != NAND_CMD_NONE)
145 writel((dat << 16), this->IO_ADDR_W);
151 * read device ready pin
153 static int cmx270_device_ready(struct mtd_info *mtd)
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;
171 /* Allocate memory for MTD device structure and private data */
172 cmx270_nand_mtd = kzalloc(sizeof(struct mtd_info) +
173 sizeof(struct nand_chip),
175 if (!cmx270_nand_mtd) {
176 printk("Unable to allocate CM-X270 NAND MTD device structure.\n");
180 cmx270_nand_io = ioremap(PXA_CS1_PHYS, 12);
181 if (!cmx270_nand_io) {
182 printk("Unable to ioremap NAND device\n");
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");
217 #ifdef CONFIG_MTD_CMDLINE_PARTS
218 mtd_parts_nb = parse_mtd_partitions(cmx270_nand_mtd, part_probes,
220 if (mtd_parts_nb > 0)
221 part_type = "command line";
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);
241 iounmap(cmx270_nand_io);
243 kfree(cmx270_nand_mtd);
248 module_init(cmx270_init);
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");