1 /*
2 * Copyright (C) 2006 Compulab, Ltd.
3 * Mike Rapoport <mike@compulab.co.il>
4 *
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)
8 *
9 *
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.
13 *
14 * Overview:
15 * This is a device driver for the NAND flash device found on the
16 * CM-X270 board.
17 */
18
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>
24
25 #include <asm/io.h>
26 #include <asm/irq.h>
27 #include <asm/mach-types.h>
28
29 #include <mach/pxa2xx-regs.h>
30
31 #define GPIO_NAND_CS (11)
32 #define GPIO_NAND_RB (89)
33
34 /* MTD structure for CM-X270 board */
35 static struct mtd_info *cmx270_nand_mtd;
36
37 /* remaped IO address of the device */
38 static void __iomem *cmx270_nand_io;
39
40 /*
41 * Define static partitions for flash device
42 */
43 static const struct mtd_partition partition_info[] = {
44 [0] = {
45 .name = "cmx270-0",
46 .offset = 0,
47 .size = MTDPART_SIZ_FULL
48 }
49 };
50 #define NUM_PARTITIONS (ARRAY_SIZE(partition_info))
51
cmx270_read_byte(struct mtd_info * mtd)52 static u_char cmx270_read_byte(struct mtd_info *mtd)
53 {
54 struct nand_chip *this = mtd_to_nand(mtd);
55
56 return (readl(this->IO_ADDR_R) >> 16);
57 }
58
cmx270_write_buf(struct mtd_info * mtd,const u_char * buf,int len)59 static void cmx270_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
60 {
61 int i;
62 struct nand_chip *this = mtd_to_nand(mtd);
63
64 for (i=0; i<len; i++)
65 writel((*buf++ << 16), this->IO_ADDR_W);
66 }
67
cmx270_read_buf(struct mtd_info * mtd,u_char * buf,int len)68 static void cmx270_read_buf(struct mtd_info *mtd, u_char *buf, int len)
69 {
70 int i;
71 struct nand_chip *this = mtd_to_nand(mtd);
72
73 for (i=0; i<len; i++)
74 *buf++ = readl(this->IO_ADDR_R) >> 16;
75 }
76
nand_cs_on(void)77 static inline void nand_cs_on(void)
78 {
79 gpio_set_value(GPIO_NAND_CS, 0);
80 }
81
nand_cs_off(void)82 static void nand_cs_off(void)
83 {
84 dsb();
85
86 gpio_set_value(GPIO_NAND_CS, 1);
87 }
88
89 /*
90 * hardware specific access to control-lines
91 */
cmx270_hwcontrol(struct mtd_info * mtd,int dat,unsigned int ctrl)92 static void cmx270_hwcontrol(struct mtd_info *mtd, int dat,
93 unsigned int ctrl)
94 {
95 struct nand_chip *this = mtd_to_nand(mtd);
96 unsigned int nandaddr = (unsigned int)this->IO_ADDR_W;
97
98 dsb();
99
100 if (ctrl & NAND_CTRL_CHANGE) {
101 if ( ctrl & NAND_ALE )
102 nandaddr |= (1 << 3);
103 else
104 nandaddr &= ~(1 << 3);
105 if ( ctrl & NAND_CLE )
106 nandaddr |= (1 << 2);
107 else
108 nandaddr &= ~(1 << 2);
109 if ( ctrl & NAND_NCE )
110 nand_cs_on();
111 else
112 nand_cs_off();
113 }
114
115 dsb();
116 this->IO_ADDR_W = (void __iomem*)nandaddr;
117 if (dat != NAND_CMD_NONE)
118 writel((dat << 16), this->IO_ADDR_W);
119
120 dsb();
121 }
122
123 /*
124 * read device ready pin
125 */
cmx270_device_ready(struct mtd_info * mtd)126 static int cmx270_device_ready(struct mtd_info *mtd)
127 {
128 dsb();
129
130 return (gpio_get_value(GPIO_NAND_RB));
131 }
132
133 /*
134 * Main initialization routine
135 */
cmx270_init(void)136 static int __init cmx270_init(void)
137 {
138 struct nand_chip *this;
139 int ret;
140
141 if (!(machine_is_armcore() && cpu_is_pxa27x()))
142 return -ENODEV;
143
144 ret = gpio_request(GPIO_NAND_CS, "NAND CS");
145 if (ret) {
146 pr_warn("CM-X270: failed to request NAND CS gpio\n");
147 return ret;
148 }
149
150 gpio_direction_output(GPIO_NAND_CS, 1);
151
152 ret = gpio_request(GPIO_NAND_RB, "NAND R/B");
153 if (ret) {
154 pr_warn("CM-X270: failed to request NAND R/B gpio\n");
155 goto err_gpio_request;
156 }
157
158 gpio_direction_input(GPIO_NAND_RB);
159
160 /* Allocate memory for MTD device structure and private data */
161 this = kzalloc(sizeof(struct nand_chip), GFP_KERNEL);
162 if (!this) {
163 ret = -ENOMEM;
164 goto err_kzalloc;
165 }
166
167 cmx270_nand_io = ioremap(PXA_CS1_PHYS, 12);
168 if (!cmx270_nand_io) {
169 pr_debug("Unable to ioremap NAND device\n");
170 ret = -EINVAL;
171 goto err_ioremap;
172 }
173
174 cmx270_nand_mtd = nand_to_mtd(this);
175
176 /* Link the private data with the MTD structure */
177 cmx270_nand_mtd->owner = THIS_MODULE;
178
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;
184
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;
189
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;
194
195 /* Scan to find existence of the device */
196 ret = nand_scan(this, 1);
197 if (ret) {
198 pr_notice("No NAND device\n");
199 goto err_scan;
200 }
201
202 /* Register the partitions */
203 ret = mtd_device_register(cmx270_nand_mtd, partition_info,
204 NUM_PARTITIONS);
205 if (ret)
206 goto err_scan;
207
208 /* Return happy */
209 return 0;
210
211 err_scan:
212 iounmap(cmx270_nand_io);
213 err_ioremap:
214 kfree(this);
215 err_kzalloc:
216 gpio_free(GPIO_NAND_RB);
217 err_gpio_request:
218 gpio_free(GPIO_NAND_CS);
219
220 return ret;
221
222 }
223 module_init(cmx270_init);
224
225 /*
226 * Clean up routine
227 */
cmx270_cleanup(void)228 static void __exit cmx270_cleanup(void)
229 {
230 /* Release resources, unregister device */
231 nand_release(mtd_to_nand(cmx270_nand_mtd));
232
233 gpio_free(GPIO_NAND_RB);
234 gpio_free(GPIO_NAND_CS);
235
236 iounmap(cmx270_nand_io);
237
238 kfree(mtd_to_nand(cmx270_nand_mtd));
239 }
240 module_exit(cmx270_cleanup);
241
242 MODULE_LICENSE("GPL");
243 MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
244 MODULE_DESCRIPTION("NAND flash driver for Compulab CM-X270 Module");
245