blob: 02d6751e9efea6ea8cfcf650a6e2c748b87a43a0 [file] [log] [blame]
Mike Rapoport54d33c42007-04-22 08:53:21 +03001/*
Mike Rapoport54d33c42007-04-22 08:53:21 +03002 * Copyright (C) 2006 Compulab, Ltd.
3 * Mike Rapoport <mike@compulab.co.il>
4 *
Boris Brezillon187c54482018-02-05 23:02:02 +01005 * Derived from drivers/mtd/nand/h1910.c (removed in v3.10)
Mike Rapoport54d33c42007-04-22 08:53:21 +03006 * 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
Boris Brezillond4092d72017-08-04 17:29:10 +020019#include <linux/mtd/rawnand.h>
Mike Rapoport54d33c42007-04-22 08:53:21 +030020#include <linux/mtd/partitions.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Mike Rapoport70eb33d2008-06-17 09:48:46 +010022#include <linux/gpio.h>
Paul Gortmakera0e5cc52011-07-03 15:17:31 -040023#include <linux/module.h>
Mike Rapoport54d33c42007-04-22 08:53:21 +030024
25#include <asm/io.h>
26#include <asm/irq.h>
Mike Rapoport70eb33d2008-06-17 09:48:46 +010027#include <asm/mach-types.h>
Mike Rapoport54d33c42007-04-22 08:53:21 +030028
Eric Miaob74d1962009-01-20 10:31:55 +080029#include <mach/pxa2xx-regs.h>
Mike Rapoport54d33c42007-04-22 08:53:21 +030030
31#define GPIO_NAND_CS (11)
32#define GPIO_NAND_RB (89)
33
Mike Rapoport54d33c42007-04-22 08:53:21 +030034/* MTD structure for CM-X270 board */
35static struct mtd_info *cmx270_nand_mtd;
36
37/* remaped IO address of the device */
38static void __iomem *cmx270_nand_io;
39
40/*
41 * Define static partitions for flash device
42 */
Arvind Yadavd4906682017-08-28 13:54:57 +053043static const struct mtd_partition partition_info[] = {
Mike Rapoport54d33c42007-04-22 08:53:21 +030044 [0] = {
45 .name = "cmx270-0",
46 .offset = 0,
47 .size = MTDPART_SIZ_FULL
48 }
49};
50#define NUM_PARTITIONS (ARRAY_SIZE(partition_info))
51
Mike Rapoport54d33c42007-04-22 08:53:21 +030052static u_char cmx270_read_byte(struct mtd_info *mtd)
53{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +010054 struct nand_chip *this = mtd_to_nand(mtd);
Mike Rapoport54d33c42007-04-22 08:53:21 +030055
56 return (readl(this->IO_ADDR_R) >> 16);
57}
58
59static void cmx270_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
60{
61 int i;
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +010062 struct nand_chip *this = mtd_to_nand(mtd);
Mike Rapoport54d33c42007-04-22 08:53:21 +030063
64 for (i=0; i<len; i++)
65 writel((*buf++ << 16), this->IO_ADDR_W);
66}
67
68static void cmx270_read_buf(struct mtd_info *mtd, u_char *buf, int len)
69{
70 int i;
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +010071 struct nand_chip *this = mtd_to_nand(mtd);
Mike Rapoport54d33c42007-04-22 08:53:21 +030072
73 for (i=0; i<len; i++)
74 *buf++ = readl(this->IO_ADDR_R) >> 16;
75}
76
Mike Rapoport54d33c42007-04-22 08:53:21 +030077static inline void nand_cs_on(void)
78{
Mike Rapoport70eb33d2008-06-17 09:48:46 +010079 gpio_set_value(GPIO_NAND_CS, 0);
Mike Rapoport54d33c42007-04-22 08:53:21 +030080}
81
82static void nand_cs_off(void)
83{
Mike Rapoport70eb33d2008-06-17 09:48:46 +010084 dsb();
Mike Rapoport54d33c42007-04-22 08:53:21 +030085
Mike Rapoport70eb33d2008-06-17 09:48:46 +010086 gpio_set_value(GPIO_NAND_CS, 1);
Mike Rapoport54d33c42007-04-22 08:53:21 +030087}
88
89/*
90 * hardware specific access to control-lines
91 */
92static void cmx270_hwcontrol(struct mtd_info *mtd, int dat,
93 unsigned int ctrl)
94{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +010095 struct nand_chip *this = mtd_to_nand(mtd);
Mike Rapoport54d33c42007-04-22 08:53:21 +030096 unsigned int nandaddr = (unsigned int)this->IO_ADDR_W;
97
Mike Rapoport70eb33d2008-06-17 09:48:46 +010098 dsb();
Mike Rapoport54d33c42007-04-22 08:53:21 +030099
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
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100115 dsb();
Mike Rapoport54d33c42007-04-22 08:53:21 +0300116 this->IO_ADDR_W = (void __iomem*)nandaddr;
117 if (dat != NAND_CMD_NONE)
118 writel((dat << 16), this->IO_ADDR_W);
119
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100120 dsb();
Mike Rapoport54d33c42007-04-22 08:53:21 +0300121}
122
123/*
124 * read device ready pin
125 */
126static int cmx270_device_ready(struct mtd_info *mtd)
127{
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100128 dsb();
Mike Rapoport54d33c42007-04-22 08:53:21 +0300129
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100130 return (gpio_get_value(GPIO_NAND_RB));
Mike Rapoport54d33c42007-04-22 08:53:21 +0300131}
132
133/*
134 * Main initialization routine
135 */
Peter Huewe627df232009-06-11 02:23:33 +0200136static int __init cmx270_init(void)
Mike Rapoport54d33c42007-04-22 08:53:21 +0300137{
138 struct nand_chip *this;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300139 int ret;
140
Mike Rapoporta7f3f032008-10-05 10:26:55 +0100141 if (!(machine_is_armcore() && cpu_is_pxa27x()))
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100142 return -ENODEV;
143
144 ret = gpio_request(GPIO_NAND_CS, "NAND CS");
145 if (ret) {
Joe Perchese8348dc2017-02-16 23:11:37 -0800146 pr_warn("CM-X270: failed to request NAND CS gpio\n");
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100147 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) {
Joe Perchese8348dc2017-02-16 23:11:37 -0800154 pr_warn("CM-X270: failed to request NAND R/B gpio\n");
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100155 goto err_gpio_request;
156 }
157
158 gpio_direction_input(GPIO_NAND_RB);
159
Mike Rapoport54d33c42007-04-22 08:53:21 +0300160 /* Allocate memory for MTD device structure and private data */
Boris BREZILLON2afd14f2015-12-10 08:59:56 +0100161 this = kzalloc(sizeof(struct nand_chip), GFP_KERNEL);
162 if (!this) {
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100163 ret = -ENOMEM;
164 goto err_kzalloc;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300165 }
166
167 cmx270_nand_io = ioremap(PXA_CS1_PHYS, 12);
168 if (!cmx270_nand_io) {
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100169 pr_debug("Unable to ioremap NAND device\n");
Mike Rapoport54d33c42007-04-22 08:53:21 +0300170 ret = -EINVAL;
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100171 goto err_ioremap;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300172 }
173
Boris BREZILLON2afd14f2015-12-10 08:59:56 +0100174 cmx270_nand_mtd = nand_to_mtd(this);
Mike Rapoport54d33c42007-04-22 08:53:21 +0300175
176 /* Link the private data with the MTD structure */
177 cmx270_nand_mtd->owner = THIS_MODULE;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300178
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;
Rafał Miłeckid9944e12016-04-13 14:06:59 +0200188 this->ecc.algo = NAND_ECC_HAMMING;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300189
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;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300194
195 /* Scan to find existence of the device */
Masahiro Yamada546fe032016-11-04 19:42:50 +0900196 ret = nand_scan(cmx270_nand_mtd, 1);
197 if (ret) {
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100198 pr_notice("No NAND device\n");
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100199 goto err_scan;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300200 }
201
Mike Rapoport54d33c42007-04-22 08:53:21 +0300202 /* Register the partitions */
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +0200203 ret = mtd_device_parse_register(cmx270_nand_mtd, NULL, NULL,
Dmitry Eremin-Solenikov0b118f02011-06-02 18:00:30 +0400204 partition_info, NUM_PARTITIONS);
Mike Rapoport54d33c42007-04-22 08:53:21 +0300205 if (ret)
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100206 goto err_scan;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300207
208 /* Return happy */
209 return 0;
210
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100211err_scan:
Mike Rapoport54d33c42007-04-22 08:53:21 +0300212 iounmap(cmx270_nand_io);
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100213err_ioremap:
Boris BREZILLON2afd14f2015-12-10 08:59:56 +0100214 kfree(this);
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100215err_kzalloc:
216 gpio_free(GPIO_NAND_RB);
217err_gpio_request:
218 gpio_free(GPIO_NAND_CS);
Mike Rapoport54d33c42007-04-22 08:53:21 +0300219
220 return ret;
221
222}
223module_init(cmx270_init);
224
225/*
226 * Clean up routine
227 */
Peter Huewe627df232009-06-11 02:23:33 +0200228static void __exit cmx270_cleanup(void)
Mike Rapoport54d33c42007-04-22 08:53:21 +0300229{
230 /* Release resources, unregister device */
231 nand_release(cmx270_nand_mtd);
232
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100233 gpio_free(GPIO_NAND_RB);
234 gpio_free(GPIO_NAND_CS);
235
Mike Rapoport54d33c42007-04-22 08:53:21 +0300236 iounmap(cmx270_nand_io);
237
Boris BREZILLON2afd14f2015-12-10 08:59:56 +0100238 kfree(mtd_to_nand(cmx270_nand_mtd));
Mike Rapoport54d33c42007-04-22 08:53:21 +0300239}
240module_exit(cmx270_cleanup);
241
242MODULE_LICENSE("GPL");
243MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
244MODULE_DESCRIPTION("NAND flash driver for Compulab CM-X270 Module");