blob: 226ac0bcafc6539ba6389cf1170f05ee05a2acc6 [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
Mike Rapoport54d33c42007-04-22 08:53:21 +030054static u_char cmx270_read_byte(struct mtd_info *mtd)
55{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +010056 struct nand_chip *this = mtd_to_nand(mtd);
Mike Rapoport54d33c42007-04-22 08:53:21 +030057
58 return (readl(this->IO_ADDR_R) >> 16);
59}
60
61static void cmx270_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
62{
63 int i;
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +010064 struct nand_chip *this = mtd_to_nand(mtd);
Mike Rapoport54d33c42007-04-22 08:53:21 +030065
66 for (i=0; i<len; i++)
67 writel((*buf++ << 16), this->IO_ADDR_W);
68}
69
70static void cmx270_read_buf(struct mtd_info *mtd, u_char *buf, int len)
71{
72 int i;
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +010073 struct nand_chip *this = mtd_to_nand(mtd);
Mike Rapoport54d33c42007-04-22 08:53:21 +030074
75 for (i=0; i<len; i++)
76 *buf++ = readl(this->IO_ADDR_R) >> 16;
77}
78
Mike Rapoport54d33c42007-04-22 08:53:21 +030079static inline void nand_cs_on(void)
80{
Mike Rapoport70eb33d2008-06-17 09:48:46 +010081 gpio_set_value(GPIO_NAND_CS, 0);
Mike Rapoport54d33c42007-04-22 08:53:21 +030082}
83
84static void nand_cs_off(void)
85{
Mike Rapoport70eb33d2008-06-17 09:48:46 +010086 dsb();
Mike Rapoport54d33c42007-04-22 08:53:21 +030087
Mike Rapoport70eb33d2008-06-17 09:48:46 +010088 gpio_set_value(GPIO_NAND_CS, 1);
Mike Rapoport54d33c42007-04-22 08:53:21 +030089}
90
91/*
92 * hardware specific access to control-lines
93 */
94static void cmx270_hwcontrol(struct mtd_info *mtd, int dat,
95 unsigned int ctrl)
96{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +010097 struct nand_chip *this = mtd_to_nand(mtd);
Mike Rapoport54d33c42007-04-22 08:53:21 +030098 unsigned int nandaddr = (unsigned int)this->IO_ADDR_W;
99
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100100 dsb();
Mike Rapoport54d33c42007-04-22 08:53:21 +0300101
102 if (ctrl & NAND_CTRL_CHANGE) {
103 if ( ctrl & NAND_ALE )
104 nandaddr |= (1 << 3);
105 else
106 nandaddr &= ~(1 << 3);
107 if ( ctrl & NAND_CLE )
108 nandaddr |= (1 << 2);
109 else
110 nandaddr &= ~(1 << 2);
111 if ( ctrl & NAND_NCE )
112 nand_cs_on();
113 else
114 nand_cs_off();
115 }
116
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100117 dsb();
Mike Rapoport54d33c42007-04-22 08:53:21 +0300118 this->IO_ADDR_W = (void __iomem*)nandaddr;
119 if (dat != NAND_CMD_NONE)
120 writel((dat << 16), this->IO_ADDR_W);
121
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100122 dsb();
Mike Rapoport54d33c42007-04-22 08:53:21 +0300123}
124
125/*
126 * read device ready pin
127 */
128static int cmx270_device_ready(struct mtd_info *mtd)
129{
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100130 dsb();
Mike Rapoport54d33c42007-04-22 08:53:21 +0300131
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100132 return (gpio_get_value(GPIO_NAND_RB));
Mike Rapoport54d33c42007-04-22 08:53:21 +0300133}
134
135/*
136 * Main initialization routine
137 */
Peter Huewe627df232009-06-11 02:23:33 +0200138static int __init cmx270_init(void)
Mike Rapoport54d33c42007-04-22 08:53:21 +0300139{
140 struct nand_chip *this;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300141 int ret;
142
Mike Rapoporta7f3f032008-10-05 10:26:55 +0100143 if (!(machine_is_armcore() && cpu_is_pxa27x()))
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100144 return -ENODEV;
145
146 ret = gpio_request(GPIO_NAND_CS, "NAND CS");
147 if (ret) {
148 pr_warning("CM-X270: failed to request NAND CS gpio\n");
149 return ret;
150 }
151
152 gpio_direction_output(GPIO_NAND_CS, 1);
153
154 ret = gpio_request(GPIO_NAND_RB, "NAND R/B");
155 if (ret) {
156 pr_warning("CM-X270: failed to request NAND R/B gpio\n");
157 goto err_gpio_request;
158 }
159
160 gpio_direction_input(GPIO_NAND_RB);
161
Mike Rapoport54d33c42007-04-22 08:53:21 +0300162 /* Allocate memory for MTD device structure and private data */
Boris BREZILLON2afd14f2015-12-10 08:59:56 +0100163 this = kzalloc(sizeof(struct nand_chip), GFP_KERNEL);
164 if (!this) {
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100165 ret = -ENOMEM;
166 goto err_kzalloc;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300167 }
168
169 cmx270_nand_io = ioremap(PXA_CS1_PHYS, 12);
170 if (!cmx270_nand_io) {
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100171 pr_debug("Unable to ioremap NAND device\n");
Mike Rapoport54d33c42007-04-22 08:53:21 +0300172 ret = -EINVAL;
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100173 goto err_ioremap;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300174 }
175
Boris BREZILLON2afd14f2015-12-10 08:59:56 +0100176 cmx270_nand_mtd = nand_to_mtd(this);
Mike Rapoport54d33c42007-04-22 08:53:21 +0300177
178 /* Link the private data with the MTD structure */
179 cmx270_nand_mtd->owner = THIS_MODULE;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300180
181 /* insert callbacks */
182 this->IO_ADDR_R = cmx270_nand_io;
183 this->IO_ADDR_W = cmx270_nand_io;
184 this->cmd_ctrl = cmx270_hwcontrol;
185 this->dev_ready = cmx270_device_ready;
186
187 /* 15 us command delay time */
188 this->chip_delay = 20;
189 this->ecc.mode = NAND_ECC_SOFT;
Rafał Miłeckid9944e12016-04-13 14:06:59 +0200190 this->ecc.algo = NAND_ECC_HAMMING;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300191
192 /* read/write functions */
193 this->read_byte = cmx270_read_byte;
194 this->read_buf = cmx270_read_buf;
195 this->write_buf = cmx270_write_buf;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300196
197 /* Scan to find existence of the device */
Masahiro Yamada546fe032016-11-04 19:42:50 +0900198 ret = nand_scan(cmx270_nand_mtd, 1);
199 if (ret) {
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100200 pr_notice("No NAND device\n");
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100201 goto err_scan;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300202 }
203
Mike Rapoport54d33c42007-04-22 08:53:21 +0300204 /* Register the partitions */
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +0200205 ret = mtd_device_parse_register(cmx270_nand_mtd, NULL, NULL,
Dmitry Eremin-Solenikov0b118f02011-06-02 18:00:30 +0400206 partition_info, NUM_PARTITIONS);
Mike Rapoport54d33c42007-04-22 08:53:21 +0300207 if (ret)
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100208 goto err_scan;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300209
210 /* Return happy */
211 return 0;
212
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100213err_scan:
Mike Rapoport54d33c42007-04-22 08:53:21 +0300214 iounmap(cmx270_nand_io);
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100215err_ioremap:
Boris BREZILLON2afd14f2015-12-10 08:59:56 +0100216 kfree(this);
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100217err_kzalloc:
218 gpio_free(GPIO_NAND_RB);
219err_gpio_request:
220 gpio_free(GPIO_NAND_CS);
Mike Rapoport54d33c42007-04-22 08:53:21 +0300221
222 return ret;
223
224}
225module_init(cmx270_init);
226
227/*
228 * Clean up routine
229 */
Peter Huewe627df232009-06-11 02:23:33 +0200230static void __exit cmx270_cleanup(void)
Mike Rapoport54d33c42007-04-22 08:53:21 +0300231{
232 /* Release resources, unregister device */
233 nand_release(cmx270_nand_mtd);
234
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100235 gpio_free(GPIO_NAND_RB);
236 gpio_free(GPIO_NAND_CS);
237
Mike Rapoport54d33c42007-04-22 08:53:21 +0300238 iounmap(cmx270_nand_io);
239
Boris BREZILLON2afd14f2015-12-10 08:59:56 +0100240 kfree(mtd_to_nand(cmx270_nand_mtd));
Mike Rapoport54d33c42007-04-22 08:53:21 +0300241}
242module_exit(cmx270_cleanup);
243
244MODULE_LICENSE("GPL");
245MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
246MODULE_DESCRIPTION("NAND flash driver for Compulab CM-X270 Module");