Merge branch 'parisc-4.20-1' of git://git.kernel.org/pub/scm/linux/kernel/git/deller...
[sfrench/cifs-2.6.git] / drivers / mtd / nand / raw / cmx270_nand.c
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
52 static u_char cmx270_read_byte(struct nand_chip *this)
53 {
54         return (readl(this->legacy.IO_ADDR_R) >> 16);
55 }
56
57 static void cmx270_write_buf(struct nand_chip *this, const u_char *buf,
58                              int len)
59 {
60         int i;
61
62         for (i=0; i<len; i++)
63                 writel((*buf++ << 16), this->legacy.IO_ADDR_W);
64 }
65
66 static void cmx270_read_buf(struct nand_chip *this, u_char *buf, int len)
67 {
68         int i;
69
70         for (i=0; i<len; i++)
71                 *buf++ = readl(this->legacy.IO_ADDR_R) >> 16;
72 }
73
74 static inline void nand_cs_on(void)
75 {
76         gpio_set_value(GPIO_NAND_CS, 0);
77 }
78
79 static void nand_cs_off(void)
80 {
81         dsb();
82
83         gpio_set_value(GPIO_NAND_CS, 1);
84 }
85
86 /*
87  *      hardware specific access to control-lines
88  */
89 static void cmx270_hwcontrol(struct nand_chip *this, int dat,
90                              unsigned int ctrl)
91 {
92         unsigned int nandaddr = (unsigned int)this->legacy.IO_ADDR_W;
93
94         dsb();
95
96         if (ctrl & NAND_CTRL_CHANGE) {
97                 if ( ctrl & NAND_ALE )
98                         nandaddr |=  (1 << 3);
99                 else
100                         nandaddr &= ~(1 << 3);
101                 if ( ctrl & NAND_CLE )
102                         nandaddr |=  (1 << 2);
103                 else
104                         nandaddr &= ~(1 << 2);
105                 if ( ctrl & NAND_NCE )
106                         nand_cs_on();
107                 else
108                         nand_cs_off();
109         }
110
111         dsb();
112         this->legacy.IO_ADDR_W = (void __iomem*)nandaddr;
113         if (dat != NAND_CMD_NONE)
114                 writel((dat << 16), this->legacy.IO_ADDR_W);
115
116         dsb();
117 }
118
119 /*
120  *      read device ready pin
121  */
122 static int cmx270_device_ready(struct nand_chip *this)
123 {
124         dsb();
125
126         return (gpio_get_value(GPIO_NAND_RB));
127 }
128
129 /*
130  * Main initialization routine
131  */
132 static int __init cmx270_init(void)
133 {
134         struct nand_chip *this;
135         int ret;
136
137         if (!(machine_is_armcore() && cpu_is_pxa27x()))
138                 return -ENODEV;
139
140         ret = gpio_request(GPIO_NAND_CS, "NAND CS");
141         if (ret) {
142                 pr_warn("CM-X270: failed to request NAND CS gpio\n");
143                 return ret;
144         }
145
146         gpio_direction_output(GPIO_NAND_CS, 1);
147
148         ret = gpio_request(GPIO_NAND_RB, "NAND R/B");
149         if (ret) {
150                 pr_warn("CM-X270: failed to request NAND R/B gpio\n");
151                 goto err_gpio_request;
152         }
153
154         gpio_direction_input(GPIO_NAND_RB);
155
156         /* Allocate memory for MTD device structure and private data */
157         this = kzalloc(sizeof(struct nand_chip), GFP_KERNEL);
158         if (!this) {
159                 ret = -ENOMEM;
160                 goto err_kzalloc;
161         }
162
163         cmx270_nand_io = ioremap(PXA_CS1_PHYS, 12);
164         if (!cmx270_nand_io) {
165                 pr_debug("Unable to ioremap NAND device\n");
166                 ret = -EINVAL;
167                 goto err_ioremap;
168         }
169
170         cmx270_nand_mtd = nand_to_mtd(this);
171
172         /* Link the private data with the MTD structure */
173         cmx270_nand_mtd->owner = THIS_MODULE;
174
175         /* insert callbacks */
176         this->legacy.IO_ADDR_R = cmx270_nand_io;
177         this->legacy.IO_ADDR_W = cmx270_nand_io;
178         this->legacy.cmd_ctrl = cmx270_hwcontrol;
179         this->legacy.dev_ready = cmx270_device_ready;
180
181         /* 15 us command delay time */
182         this->legacy.chip_delay = 20;
183         this->ecc.mode = NAND_ECC_SOFT;
184         this->ecc.algo = NAND_ECC_HAMMING;
185
186         /* read/write functions */
187         this->legacy.read_byte = cmx270_read_byte;
188         this->legacy.read_buf = cmx270_read_buf;
189         this->legacy.write_buf = cmx270_write_buf;
190
191         /* Scan to find existence of the device */
192         ret = nand_scan(this, 1);
193         if (ret) {
194                 pr_notice("No NAND device\n");
195                 goto err_scan;
196         }
197
198         /* Register the partitions */
199         ret = mtd_device_register(cmx270_nand_mtd, partition_info,
200                                   NUM_PARTITIONS);
201         if (ret)
202                 goto err_scan;
203
204         /* Return happy */
205         return 0;
206
207 err_scan:
208         iounmap(cmx270_nand_io);
209 err_ioremap:
210         kfree(this);
211 err_kzalloc:
212         gpio_free(GPIO_NAND_RB);
213 err_gpio_request:
214         gpio_free(GPIO_NAND_CS);
215
216         return ret;
217
218 }
219 module_init(cmx270_init);
220
221 /*
222  * Clean up routine
223  */
224 static void __exit cmx270_cleanup(void)
225 {
226         /* Release resources, unregister device */
227         nand_release(mtd_to_nand(cmx270_nand_mtd));
228
229         gpio_free(GPIO_NAND_RB);
230         gpio_free(GPIO_NAND_CS);
231
232         iounmap(cmx270_nand_io);
233
234         kfree(mtd_to_nand(cmx270_nand_mtd));
235 }
236 module_exit(cmx270_cleanup);
237
238 MODULE_LICENSE("GPL");
239 MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
240 MODULE_DESCRIPTION("NAND flash driver for Compulab CM-X270 Module");