blob: 31308e694bd3055d4d0d83cedd4b4f5b29920144 [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>
Mike Rapoport54d33c42007-04-22 08:53:21 +030025
26#include <asm/io.h>
27#include <asm/irq.h>
Mike Rapoport70eb33d2008-06-17 09:48:46 +010028#include <asm/mach-types.h>
Mike Rapoport54d33c42007-04-22 08:53:21 +030029
Eric Miaob74d1962009-01-20 10:31:55 +080030#include <mach/pxa2xx-regs.h>
Mike Rapoport54d33c42007-04-22 08:53:21 +030031
32#define GPIO_NAND_CS (11)
33#define GPIO_NAND_RB (89)
34
Mike Rapoport54d33c42007-04-22 08:53:21 +030035/* MTD structure for CM-X270 board */
36static struct mtd_info *cmx270_nand_mtd;
37
38/* remaped IO address of the device */
39static void __iomem *cmx270_nand_io;
40
41/*
42 * Define static partitions for flash device
43 */
44static struct mtd_partition partition_info[] = {
45 [0] = {
46 .name = "cmx270-0",
47 .offset = 0,
48 .size = MTDPART_SIZ_FULL
49 }
50};
51#define NUM_PARTITIONS (ARRAY_SIZE(partition_info))
52
Mike Rapoport54d33c42007-04-22 08:53:21 +030053static u_char cmx270_read_byte(struct mtd_info *mtd)
54{
55 struct nand_chip *this = mtd->priv;
56
57 return (readl(this->IO_ADDR_R) >> 16);
58}
59
60static void cmx270_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
61{
62 int i;
63 struct nand_chip *this = mtd->priv;
64
65 for (i=0; i<len; i++)
66 writel((*buf++ << 16), this->IO_ADDR_W);
67}
68
69static void cmx270_read_buf(struct mtd_info *mtd, u_char *buf, int len)
70{
71 int i;
72 struct nand_chip *this = mtd->priv;
73
74 for (i=0; i<len; i++)
75 *buf++ = readl(this->IO_ADDR_R) >> 16;
76}
77
78static int cmx270_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
79{
80 int i;
81 struct nand_chip *this = mtd->priv;
82
83 for (i=0; i<len; i++)
84 if (buf[i] != (u_char)(readl(this->IO_ADDR_R) >> 16))
85 return -EFAULT;
86
87 return 0;
88}
89
90static inline void nand_cs_on(void)
91{
Mike Rapoport70eb33d2008-06-17 09:48:46 +010092 gpio_set_value(GPIO_NAND_CS, 0);
Mike Rapoport54d33c42007-04-22 08:53:21 +030093}
94
95static void nand_cs_off(void)
96{
Mike Rapoport70eb33d2008-06-17 09:48:46 +010097 dsb();
Mike Rapoport54d33c42007-04-22 08:53:21 +030098
Mike Rapoport70eb33d2008-06-17 09:48:46 +010099 gpio_set_value(GPIO_NAND_CS, 1);
Mike Rapoport54d33c42007-04-22 08:53:21 +0300100}
101
102/*
103 * hardware specific access to control-lines
104 */
105static void cmx270_hwcontrol(struct mtd_info *mtd, int dat,
106 unsigned int ctrl)
107{
108 struct nand_chip* this = mtd->priv;
109 unsigned int nandaddr = (unsigned int)this->IO_ADDR_W;
110
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100111 dsb();
Mike Rapoport54d33c42007-04-22 08:53:21 +0300112
113 if (ctrl & NAND_CTRL_CHANGE) {
114 if ( ctrl & NAND_ALE )
115 nandaddr |= (1 << 3);
116 else
117 nandaddr &= ~(1 << 3);
118 if ( ctrl & NAND_CLE )
119 nandaddr |= (1 << 2);
120 else
121 nandaddr &= ~(1 << 2);
122 if ( ctrl & NAND_NCE )
123 nand_cs_on();
124 else
125 nand_cs_off();
126 }
127
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100128 dsb();
Mike Rapoport54d33c42007-04-22 08:53:21 +0300129 this->IO_ADDR_W = (void __iomem*)nandaddr;
130 if (dat != NAND_CMD_NONE)
131 writel((dat << 16), this->IO_ADDR_W);
132
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100133 dsb();
Mike Rapoport54d33c42007-04-22 08:53:21 +0300134}
135
136/*
137 * read device ready pin
138 */
139static int cmx270_device_ready(struct mtd_info *mtd)
140{
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100141 dsb();
Mike Rapoport54d33c42007-04-22 08:53:21 +0300142
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100143 return (gpio_get_value(GPIO_NAND_RB));
Mike Rapoport54d33c42007-04-22 08:53:21 +0300144}
145
146/*
147 * Main initialization routine
148 */
Peter Huewe627df232009-06-11 02:23:33 +0200149static int __init cmx270_init(void)
Mike Rapoport54d33c42007-04-22 08:53:21 +0300150{
151 struct nand_chip *this;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300152 int ret;
153
Mike Rapoporta7f3f032008-10-05 10:26:55 +0100154 if (!(machine_is_armcore() && cpu_is_pxa27x()))
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100155 return -ENODEV;
156
157 ret = gpio_request(GPIO_NAND_CS, "NAND CS");
158 if (ret) {
159 pr_warning("CM-X270: failed to request NAND CS gpio\n");
160 return ret;
161 }
162
163 gpio_direction_output(GPIO_NAND_CS, 1);
164
165 ret = gpio_request(GPIO_NAND_RB, "NAND R/B");
166 if (ret) {
167 pr_warning("CM-X270: failed to request NAND R/B gpio\n");
168 goto err_gpio_request;
169 }
170
171 gpio_direction_input(GPIO_NAND_RB);
172
Mike Rapoport54d33c42007-04-22 08:53:21 +0300173 /* Allocate memory for MTD device structure and private data */
174 cmx270_nand_mtd = kzalloc(sizeof(struct mtd_info) +
175 sizeof(struct nand_chip),
176 GFP_KERNEL);
177 if (!cmx270_nand_mtd) {
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100178 pr_debug("Unable to allocate CM-X270 NAND MTD device structure.\n");
179 ret = -ENOMEM;
180 goto err_kzalloc;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300181 }
182
183 cmx270_nand_io = ioremap(PXA_CS1_PHYS, 12);
184 if (!cmx270_nand_io) {
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100185 pr_debug("Unable to ioremap NAND device\n");
Mike Rapoport54d33c42007-04-22 08:53:21 +0300186 ret = -EINVAL;
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100187 goto err_ioremap;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300188 }
189
190 /* Get pointer to private data */
191 this = (struct nand_chip *)(&cmx270_nand_mtd[1]);
192
193 /* Link the private data with the MTD structure */
194 cmx270_nand_mtd->owner = THIS_MODULE;
195 cmx270_nand_mtd->priv = this;
196
197 /* insert callbacks */
198 this->IO_ADDR_R = cmx270_nand_io;
199 this->IO_ADDR_W = cmx270_nand_io;
200 this->cmd_ctrl = cmx270_hwcontrol;
201 this->dev_ready = cmx270_device_ready;
202
203 /* 15 us command delay time */
204 this->chip_delay = 20;
205 this->ecc.mode = NAND_ECC_SOFT;
206
207 /* read/write functions */
208 this->read_byte = cmx270_read_byte;
209 this->read_buf = cmx270_read_buf;
210 this->write_buf = cmx270_write_buf;
211 this->verify_buf = cmx270_verify_buf;
212
213 /* Scan to find existence of the device */
214 if (nand_scan (cmx270_nand_mtd, 1)) {
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100215 pr_notice("No NAND device\n");
Mike Rapoport54d33c42007-04-22 08:53:21 +0300216 ret = -ENXIO;
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100217 goto err_scan;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300218 }
219
Mike Rapoport54d33c42007-04-22 08:53:21 +0300220 /* Register the partitions */
Dmitry Eremin-Solenikov0b118f02011-06-02 18:00:30 +0400221 ret = mtd_device_parse_register(cmx270_nand_mtd, NULL, 0,
222 partition_info, NUM_PARTITIONS);
Mike Rapoport54d33c42007-04-22 08:53:21 +0300223 if (ret)
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100224 goto err_scan;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300225
226 /* Return happy */
227 return 0;
228
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100229err_scan:
Mike Rapoport54d33c42007-04-22 08:53:21 +0300230 iounmap(cmx270_nand_io);
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100231err_ioremap:
Mike Rapoport54d33c42007-04-22 08:53:21 +0300232 kfree(cmx270_nand_mtd);
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100233err_kzalloc:
234 gpio_free(GPIO_NAND_RB);
235err_gpio_request:
236 gpio_free(GPIO_NAND_CS);
Mike Rapoport54d33c42007-04-22 08:53:21 +0300237
238 return ret;
239
240}
241module_init(cmx270_init);
242
243/*
244 * Clean up routine
245 */
Peter Huewe627df232009-06-11 02:23:33 +0200246static void __exit cmx270_cleanup(void)
Mike Rapoport54d33c42007-04-22 08:53:21 +0300247{
248 /* Release resources, unregister device */
249 nand_release(cmx270_nand_mtd);
250
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100251 gpio_free(GPIO_NAND_RB);
252 gpio_free(GPIO_NAND_CS);
253
Mike Rapoport54d33c42007-04-22 08:53:21 +0300254 iounmap(cmx270_nand_io);
255
256 /* Free the MTD device structure */
257 kfree (cmx270_nand_mtd);
258}
259module_exit(cmx270_cleanup);
260
261MODULE_LICENSE("GPL");
262MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
263MODULE_DESCRIPTION("NAND flash driver for Compulab CM-X270 Module");