blob: 4e5c8b7721abfdc0295718bcb8b4c163b936bdb8 [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
Boris Brezillon7e534322018-09-06 14:05:22 +020052static u_char cmx270_read_byte(struct nand_chip *this)
Mike Rapoport54d33c42007-04-22 08:53:21 +030053{
Mike Rapoport54d33c42007-04-22 08:53:21 +030054 return (readl(this->IO_ADDR_R) >> 16);
55}
56
Boris Brezillonc0739d82018-09-06 14:05:23 +020057static void cmx270_write_buf(struct nand_chip *this, const u_char *buf,
58 int len)
Mike Rapoport54d33c42007-04-22 08:53:21 +030059{
60 int i;
Mike Rapoport54d33c42007-04-22 08:53:21 +030061
62 for (i=0; i<len; i++)
63 writel((*buf++ << 16), this->IO_ADDR_W);
64}
65
Boris Brezillon7e534322018-09-06 14:05:22 +020066static void cmx270_read_buf(struct nand_chip *this, u_char *buf, int len)
Mike Rapoport54d33c42007-04-22 08:53:21 +030067{
68 int i;
Mike Rapoport54d33c42007-04-22 08:53:21 +030069
70 for (i=0; i<len; i++)
71 *buf++ = readl(this->IO_ADDR_R) >> 16;
72}
73
Mike Rapoport54d33c42007-04-22 08:53:21 +030074static inline void nand_cs_on(void)
75{
Mike Rapoport70eb33d2008-06-17 09:48:46 +010076 gpio_set_value(GPIO_NAND_CS, 0);
Mike Rapoport54d33c42007-04-22 08:53:21 +030077}
78
79static void nand_cs_off(void)
80{
Mike Rapoport70eb33d2008-06-17 09:48:46 +010081 dsb();
Mike Rapoport54d33c42007-04-22 08:53:21 +030082
Mike Rapoport70eb33d2008-06-17 09:48:46 +010083 gpio_set_value(GPIO_NAND_CS, 1);
Mike Rapoport54d33c42007-04-22 08:53:21 +030084}
85
86/*
87 * hardware specific access to control-lines
88 */
89static void cmx270_hwcontrol(struct mtd_info *mtd, int dat,
90 unsigned int ctrl)
91{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +010092 struct nand_chip *this = mtd_to_nand(mtd);
Mike Rapoport54d33c42007-04-22 08:53:21 +030093 unsigned int nandaddr = (unsigned int)this->IO_ADDR_W;
94
Mike Rapoport70eb33d2008-06-17 09:48:46 +010095 dsb();
Mike Rapoport54d33c42007-04-22 08:53:21 +030096
97 if (ctrl & NAND_CTRL_CHANGE) {
98 if ( ctrl & NAND_ALE )
99 nandaddr |= (1 << 3);
100 else
101 nandaddr &= ~(1 << 3);
102 if ( ctrl & NAND_CLE )
103 nandaddr |= (1 << 2);
104 else
105 nandaddr &= ~(1 << 2);
106 if ( ctrl & NAND_NCE )
107 nand_cs_on();
108 else
109 nand_cs_off();
110 }
111
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100112 dsb();
Mike Rapoport54d33c42007-04-22 08:53:21 +0300113 this->IO_ADDR_W = (void __iomem*)nandaddr;
114 if (dat != NAND_CMD_NONE)
115 writel((dat << 16), this->IO_ADDR_W);
116
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100117 dsb();
Mike Rapoport54d33c42007-04-22 08:53:21 +0300118}
119
120/*
121 * read device ready pin
122 */
123static int cmx270_device_ready(struct mtd_info *mtd)
124{
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100125 dsb();
Mike Rapoport54d33c42007-04-22 08:53:21 +0300126
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100127 return (gpio_get_value(GPIO_NAND_RB));
Mike Rapoport54d33c42007-04-22 08:53:21 +0300128}
129
130/*
131 * Main initialization routine
132 */
Peter Huewe627df232009-06-11 02:23:33 +0200133static int __init cmx270_init(void)
Mike Rapoport54d33c42007-04-22 08:53:21 +0300134{
135 struct nand_chip *this;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300136 int ret;
137
Mike Rapoporta7f3f032008-10-05 10:26:55 +0100138 if (!(machine_is_armcore() && cpu_is_pxa27x()))
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100139 return -ENODEV;
140
141 ret = gpio_request(GPIO_NAND_CS, "NAND CS");
142 if (ret) {
Joe Perchese8348dc2017-02-16 23:11:37 -0800143 pr_warn("CM-X270: failed to request NAND CS gpio\n");
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100144 return ret;
145 }
146
147 gpio_direction_output(GPIO_NAND_CS, 1);
148
149 ret = gpio_request(GPIO_NAND_RB, "NAND R/B");
150 if (ret) {
Joe Perchese8348dc2017-02-16 23:11:37 -0800151 pr_warn("CM-X270: failed to request NAND R/B gpio\n");
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100152 goto err_gpio_request;
153 }
154
155 gpio_direction_input(GPIO_NAND_RB);
156
Mike Rapoport54d33c42007-04-22 08:53:21 +0300157 /* Allocate memory for MTD device structure and private data */
Boris BREZILLON2afd14f2015-12-10 08:59:56 +0100158 this = kzalloc(sizeof(struct nand_chip), GFP_KERNEL);
159 if (!this) {
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100160 ret = -ENOMEM;
161 goto err_kzalloc;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300162 }
163
164 cmx270_nand_io = ioremap(PXA_CS1_PHYS, 12);
165 if (!cmx270_nand_io) {
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100166 pr_debug("Unable to ioremap NAND device\n");
Mike Rapoport54d33c42007-04-22 08:53:21 +0300167 ret = -EINVAL;
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100168 goto err_ioremap;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300169 }
170
Boris BREZILLON2afd14f2015-12-10 08:59:56 +0100171 cmx270_nand_mtd = nand_to_mtd(this);
Mike Rapoport54d33c42007-04-22 08:53:21 +0300172
173 /* Link the private data with the MTD structure */
174 cmx270_nand_mtd->owner = THIS_MODULE;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300175
176 /* insert callbacks */
177 this->IO_ADDR_R = cmx270_nand_io;
178 this->IO_ADDR_W = cmx270_nand_io;
179 this->cmd_ctrl = cmx270_hwcontrol;
180 this->dev_ready = cmx270_device_ready;
181
182 /* 15 us command delay time */
183 this->chip_delay = 20;
184 this->ecc.mode = NAND_ECC_SOFT;
Rafał Miłeckid9944e12016-04-13 14:06:59 +0200185 this->ecc.algo = NAND_ECC_HAMMING;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300186
187 /* read/write functions */
188 this->read_byte = cmx270_read_byte;
189 this->read_buf = cmx270_read_buf;
190 this->write_buf = cmx270_write_buf;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300191
192 /* Scan to find existence of the device */
Boris Brezillon00ad3782018-09-06 14:05:14 +0200193 ret = nand_scan(this, 1);
Masahiro Yamada546fe032016-11-04 19:42:50 +0900194 if (ret) {
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100195 pr_notice("No NAND device\n");
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100196 goto err_scan;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300197 }
198
Mike Rapoport54d33c42007-04-22 08:53:21 +0300199 /* Register the partitions */
Rafał Miłecki29597ca2018-07-13 11:27:31 +0200200 ret = mtd_device_register(cmx270_nand_mtd, partition_info,
201 NUM_PARTITIONS);
Mike Rapoport54d33c42007-04-22 08:53:21 +0300202 if (ret)
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100203 goto err_scan;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300204
205 /* Return happy */
206 return 0;
207
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100208err_scan:
Mike Rapoport54d33c42007-04-22 08:53:21 +0300209 iounmap(cmx270_nand_io);
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100210err_ioremap:
Boris BREZILLON2afd14f2015-12-10 08:59:56 +0100211 kfree(this);
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100212err_kzalloc:
213 gpio_free(GPIO_NAND_RB);
214err_gpio_request:
215 gpio_free(GPIO_NAND_CS);
Mike Rapoport54d33c42007-04-22 08:53:21 +0300216
217 return ret;
218
219}
220module_init(cmx270_init);
221
222/*
223 * Clean up routine
224 */
Peter Huewe627df232009-06-11 02:23:33 +0200225static void __exit cmx270_cleanup(void)
Mike Rapoport54d33c42007-04-22 08:53:21 +0300226{
227 /* Release resources, unregister device */
Boris Brezillon59ac2762018-09-06 14:05:15 +0200228 nand_release(mtd_to_nand(cmx270_nand_mtd));
Mike Rapoport54d33c42007-04-22 08:53:21 +0300229
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100230 gpio_free(GPIO_NAND_RB);
231 gpio_free(GPIO_NAND_CS);
232
Mike Rapoport54d33c42007-04-22 08:53:21 +0300233 iounmap(cmx270_nand_io);
234
Boris BREZILLON2afd14f2015-12-10 08:59:56 +0100235 kfree(mtd_to_nand(cmx270_nand_mtd));
Mike Rapoport54d33c42007-04-22 08:53:21 +0300236}
237module_exit(cmx270_cleanup);
238
239MODULE_LICENSE("GPL");
240MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
241MODULE_DESCRIPTION("NAND flash driver for Compulab CM-X270 Module");