blob: be33b0f4634dcc754fe3713c169735d1f39f6c1d [file] [log] [blame]
Mike Rapoport54d33c42007-04-22 08:53:21 +03001/*
2 * linux/drivers/mtd/nand/cmx270-nand.c
3 *
4 * Copyright (C) 2006 Compulab, Ltd.
5 * Mike Rapoport <mike@compulab.co.il>
6 *
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)
10 *
11 *
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.
15 *
16 * Overview:
17 * This is a device driver for the NAND flash device found on the
18 * CM-X270 board.
19 */
20
21#include <linux/mtd/nand.h>
22#include <linux/mtd/partitions.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Mike Rapoport70eb33d2008-06-17 09:48:46 +010024#include <linux/gpio.h>
Paul Gortmakera0e5cc52011-07-03 15:17:31 -040025#include <linux/module.h>
Mike Rapoport54d33c42007-04-22 08:53:21 +030026
27#include <asm/io.h>
28#include <asm/irq.h>
Mike Rapoport70eb33d2008-06-17 09:48:46 +010029#include <asm/mach-types.h>
Mike Rapoport54d33c42007-04-22 08:53:21 +030030
Eric Miaob74d1962009-01-20 10:31:55 +080031#include <mach/pxa2xx-regs.h>
Mike Rapoport54d33c42007-04-22 08:53:21 +030032
33#define GPIO_NAND_CS (11)
34#define GPIO_NAND_RB (89)
35
Mike Rapoport54d33c42007-04-22 08:53:21 +030036/* MTD structure for CM-X270 board */
37static struct mtd_info *cmx270_nand_mtd;
38
39/* remaped IO address of the device */
40static void __iomem *cmx270_nand_io;
41
42/*
43 * Define static partitions for flash device
44 */
45static struct mtd_partition partition_info[] = {
46 [0] = {
47 .name = "cmx270-0",
48 .offset = 0,
49 .size = MTDPART_SIZ_FULL
50 }
51};
52#define NUM_PARTITIONS (ARRAY_SIZE(partition_info))
53
54const char *part_probes[] = { "cmdlinepart", NULL };
55
56static u_char cmx270_read_byte(struct mtd_info *mtd)
57{
58 struct nand_chip *this = mtd->priv;
59
60 return (readl(this->IO_ADDR_R) >> 16);
61}
62
63static void cmx270_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
64{
65 int i;
66 struct nand_chip *this = mtd->priv;
67
68 for (i=0; i<len; i++)
69 writel((*buf++ << 16), this->IO_ADDR_W);
70}
71
72static void cmx270_read_buf(struct mtd_info *mtd, u_char *buf, int len)
73{
74 int i;
75 struct nand_chip *this = mtd->priv;
76
77 for (i=0; i<len; i++)
78 *buf++ = readl(this->IO_ADDR_R) >> 16;
79}
80
81static int cmx270_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
82{
83 int i;
84 struct nand_chip *this = mtd->priv;
85
86 for (i=0; i<len; i++)
87 if (buf[i] != (u_char)(readl(this->IO_ADDR_R) >> 16))
88 return -EFAULT;
89
90 return 0;
91}
92
93static inline void nand_cs_on(void)
94{
Mike Rapoport70eb33d2008-06-17 09:48:46 +010095 gpio_set_value(GPIO_NAND_CS, 0);
Mike Rapoport54d33c42007-04-22 08:53:21 +030096}
97
98static void nand_cs_off(void)
99{
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100100 dsb();
Mike Rapoport54d33c42007-04-22 08:53:21 +0300101
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100102 gpio_set_value(GPIO_NAND_CS, 1);
Mike Rapoport54d33c42007-04-22 08:53:21 +0300103}
104
105/*
106 * hardware specific access to control-lines
107 */
108static void cmx270_hwcontrol(struct mtd_info *mtd, int dat,
109 unsigned int ctrl)
110{
111 struct nand_chip* this = mtd->priv;
112 unsigned int nandaddr = (unsigned int)this->IO_ADDR_W;
113
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100114 dsb();
Mike Rapoport54d33c42007-04-22 08:53:21 +0300115
116 if (ctrl & NAND_CTRL_CHANGE) {
117 if ( ctrl & NAND_ALE )
118 nandaddr |= (1 << 3);
119 else
120 nandaddr &= ~(1 << 3);
121 if ( ctrl & NAND_CLE )
122 nandaddr |= (1 << 2);
123 else
124 nandaddr &= ~(1 << 2);
125 if ( ctrl & NAND_NCE )
126 nand_cs_on();
127 else
128 nand_cs_off();
129 }
130
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100131 dsb();
Mike Rapoport54d33c42007-04-22 08:53:21 +0300132 this->IO_ADDR_W = (void __iomem*)nandaddr;
133 if (dat != NAND_CMD_NONE)
134 writel((dat << 16), this->IO_ADDR_W);
135
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100136 dsb();
Mike Rapoport54d33c42007-04-22 08:53:21 +0300137}
138
139/*
140 * read device ready pin
141 */
142static int cmx270_device_ready(struct mtd_info *mtd)
143{
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100144 dsb();
Mike Rapoport54d33c42007-04-22 08:53:21 +0300145
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100146 return (gpio_get_value(GPIO_NAND_RB));
Mike Rapoport54d33c42007-04-22 08:53:21 +0300147}
148
149/*
150 * Main initialization routine
151 */
Peter Huewe627df232009-06-11 02:23:33 +0200152static int __init cmx270_init(void)
Mike Rapoport54d33c42007-04-22 08:53:21 +0300153{
154 struct nand_chip *this;
155 const char *part_type;
156 struct mtd_partition *mtd_parts;
157 int mtd_parts_nb = 0;
158 int ret;
159
Mike Rapoporta7f3f032008-10-05 10:26:55 +0100160 if (!(machine_is_armcore() && cpu_is_pxa27x()))
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100161 return -ENODEV;
162
163 ret = gpio_request(GPIO_NAND_CS, "NAND CS");
164 if (ret) {
165 pr_warning("CM-X270: failed to request NAND CS gpio\n");
166 return ret;
167 }
168
169 gpio_direction_output(GPIO_NAND_CS, 1);
170
171 ret = gpio_request(GPIO_NAND_RB, "NAND R/B");
172 if (ret) {
173 pr_warning("CM-X270: failed to request NAND R/B gpio\n");
174 goto err_gpio_request;
175 }
176
177 gpio_direction_input(GPIO_NAND_RB);
178
Mike Rapoport54d33c42007-04-22 08:53:21 +0300179 /* Allocate memory for MTD device structure and private data */
180 cmx270_nand_mtd = kzalloc(sizeof(struct mtd_info) +
181 sizeof(struct nand_chip),
182 GFP_KERNEL);
183 if (!cmx270_nand_mtd) {
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100184 pr_debug("Unable to allocate CM-X270 NAND MTD device structure.\n");
185 ret = -ENOMEM;
186 goto err_kzalloc;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300187 }
188
189 cmx270_nand_io = ioremap(PXA_CS1_PHYS, 12);
190 if (!cmx270_nand_io) {
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100191 pr_debug("Unable to ioremap NAND device\n");
Mike Rapoport54d33c42007-04-22 08:53:21 +0300192 ret = -EINVAL;
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100193 goto err_ioremap;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300194 }
195
196 /* Get pointer to private data */
197 this = (struct nand_chip *)(&cmx270_nand_mtd[1]);
198
199 /* Link the private data with the MTD structure */
200 cmx270_nand_mtd->owner = THIS_MODULE;
201 cmx270_nand_mtd->priv = this;
202
203 /* insert callbacks */
204 this->IO_ADDR_R = cmx270_nand_io;
205 this->IO_ADDR_W = cmx270_nand_io;
206 this->cmd_ctrl = cmx270_hwcontrol;
207 this->dev_ready = cmx270_device_ready;
208
209 /* 15 us command delay time */
210 this->chip_delay = 20;
211 this->ecc.mode = NAND_ECC_SOFT;
212
213 /* read/write functions */
214 this->read_byte = cmx270_read_byte;
215 this->read_buf = cmx270_read_buf;
216 this->write_buf = cmx270_write_buf;
217 this->verify_buf = cmx270_verify_buf;
218
219 /* Scan to find existence of the device */
220 if (nand_scan (cmx270_nand_mtd, 1)) {
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100221 pr_notice("No NAND device\n");
Mike Rapoport54d33c42007-04-22 08:53:21 +0300222 ret = -ENXIO;
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100223 goto err_scan;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300224 }
225
226#ifdef CONFIG_MTD_CMDLINE_PARTS
227 mtd_parts_nb = parse_mtd_partitions(cmx270_nand_mtd, part_probes,
228 &mtd_parts, 0);
229 if (mtd_parts_nb > 0)
230 part_type = "command line";
231 else
232 mtd_parts_nb = 0;
233#endif
234 if (!mtd_parts_nb) {
235 mtd_parts = partition_info;
236 mtd_parts_nb = NUM_PARTITIONS;
237 part_type = "static";
238 }
239
240 /* Register the partitions */
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100241 pr_notice("Using %s partition definition\n", part_type);
Jamie Ilesee0e87b2011-05-23 10:23:40 +0100242 ret = mtd_device_register(cmx270_nand_mtd, mtd_parts, mtd_parts_nb);
Mike Rapoport54d33c42007-04-22 08:53:21 +0300243 if (ret)
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100244 goto err_scan;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300245
246 /* Return happy */
247 return 0;
248
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100249err_scan:
Mike Rapoport54d33c42007-04-22 08:53:21 +0300250 iounmap(cmx270_nand_io);
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100251err_ioremap:
Mike Rapoport54d33c42007-04-22 08:53:21 +0300252 kfree(cmx270_nand_mtd);
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100253err_kzalloc:
254 gpio_free(GPIO_NAND_RB);
255err_gpio_request:
256 gpio_free(GPIO_NAND_CS);
Mike Rapoport54d33c42007-04-22 08:53:21 +0300257
258 return ret;
259
260}
261module_init(cmx270_init);
262
263/*
264 * Clean up routine
265 */
Peter Huewe627df232009-06-11 02:23:33 +0200266static void __exit cmx270_cleanup(void)
Mike Rapoport54d33c42007-04-22 08:53:21 +0300267{
268 /* Release resources, unregister device */
269 nand_release(cmx270_nand_mtd);
270
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100271 gpio_free(GPIO_NAND_RB);
272 gpio_free(GPIO_NAND_CS);
273
Mike Rapoport54d33c42007-04-22 08:53:21 +0300274 iounmap(cmx270_nand_io);
275
276 /* Free the MTD device structure */
277 kfree (cmx270_nand_mtd);
278}
279module_exit(cmx270_cleanup);
280
281MODULE_LICENSE("GPL");
282MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
283MODULE_DESCRIPTION("NAND flash driver for Compulab CM-X270 Module");