blob: 045b6175ae79c6c539277489c26609e920874984 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Mike Rapoport54d33c42007-04-22 08:53:21 +03002/*
Mike Rapoport54d33c42007-04-22 08:53:21 +03003 * Copyright (C) 2006 Compulab, Ltd.
4 * Mike Rapoport <mike@compulab.co.il>
5 *
Boris Brezillon187c54482018-02-05 23:02:02 +01006 * Derived from drivers/mtd/nand/h1910.c (removed in v3.10)
Mike Rapoport54d33c42007-04-22 08:53:21 +03007 * Copyright (C) 2002 Marius Gröger (mag@sysgo.de)
8 * Copyright (c) 2001 Thomas Gleixner (gleixner@autronix.de)
9 *
Mike Rapoport54d33c42007-04-22 08:53:21 +030010 * Overview:
11 * This is a device driver for the NAND flash device found on the
12 * CM-X270 board.
13 */
14
Boris Brezillond4092d72017-08-04 17:29:10 +020015#include <linux/mtd/rawnand.h>
Mike Rapoport54d33c42007-04-22 08:53:21 +030016#include <linux/mtd/partitions.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Mike Rapoport70eb33d2008-06-17 09:48:46 +010018#include <linux/gpio.h>
Paul Gortmakera0e5cc52011-07-03 15:17:31 -040019#include <linux/module.h>
Mike Rapoport54d33c42007-04-22 08:53:21 +030020
21#include <asm/io.h>
22#include <asm/irq.h>
Mike Rapoport70eb33d2008-06-17 09:48:46 +010023#include <asm/mach-types.h>
Mike Rapoport54d33c42007-04-22 08:53:21 +030024
Eric Miaob74d1962009-01-20 10:31:55 +080025#include <mach/pxa2xx-regs.h>
Mike Rapoport54d33c42007-04-22 08:53:21 +030026
27#define GPIO_NAND_CS (11)
28#define GPIO_NAND_RB (89)
29
Mike Rapoport54d33c42007-04-22 08:53:21 +030030/* MTD structure for CM-X270 board */
31static struct mtd_info *cmx270_nand_mtd;
32
33/* remaped IO address of the device */
34static void __iomem *cmx270_nand_io;
35
36/*
37 * Define static partitions for flash device
38 */
Arvind Yadavd4906682017-08-28 13:54:57 +053039static const struct mtd_partition partition_info[] = {
Mike Rapoport54d33c42007-04-22 08:53:21 +030040 [0] = {
41 .name = "cmx270-0",
42 .offset = 0,
43 .size = MTDPART_SIZ_FULL
44 }
45};
46#define NUM_PARTITIONS (ARRAY_SIZE(partition_info))
47
Boris Brezillon7e534322018-09-06 14:05:22 +020048static u_char cmx270_read_byte(struct nand_chip *this)
Mike Rapoport54d33c42007-04-22 08:53:21 +030049{
Boris Brezillon82fc5092018-09-07 00:38:34 +020050 return (readl(this->legacy.IO_ADDR_R) >> 16);
Mike Rapoport54d33c42007-04-22 08:53:21 +030051}
52
Boris Brezillonc0739d82018-09-06 14:05:23 +020053static void cmx270_write_buf(struct nand_chip *this, const u_char *buf,
54 int len)
Mike Rapoport54d33c42007-04-22 08:53:21 +030055{
56 int i;
Mike Rapoport54d33c42007-04-22 08:53:21 +030057
58 for (i=0; i<len; i++)
Boris Brezillon82fc5092018-09-07 00:38:34 +020059 writel((*buf++ << 16), this->legacy.IO_ADDR_W);
Mike Rapoport54d33c42007-04-22 08:53:21 +030060}
61
Boris Brezillon7e534322018-09-06 14:05:22 +020062static void cmx270_read_buf(struct nand_chip *this, u_char *buf, int len)
Mike Rapoport54d33c42007-04-22 08:53:21 +030063{
64 int i;
Mike Rapoport54d33c42007-04-22 08:53:21 +030065
66 for (i=0; i<len; i++)
Boris Brezillon82fc5092018-09-07 00:38:34 +020067 *buf++ = readl(this->legacy.IO_ADDR_R) >> 16;
Mike Rapoport54d33c42007-04-22 08:53:21 +030068}
69
Mike Rapoport54d33c42007-04-22 08:53:21 +030070static inline void nand_cs_on(void)
71{
Mike Rapoport70eb33d2008-06-17 09:48:46 +010072 gpio_set_value(GPIO_NAND_CS, 0);
Mike Rapoport54d33c42007-04-22 08:53:21 +030073}
74
75static void nand_cs_off(void)
76{
Mike Rapoport70eb33d2008-06-17 09:48:46 +010077 dsb();
Mike Rapoport54d33c42007-04-22 08:53:21 +030078
Mike Rapoport70eb33d2008-06-17 09:48:46 +010079 gpio_set_value(GPIO_NAND_CS, 1);
Mike Rapoport54d33c42007-04-22 08:53:21 +030080}
81
82/*
83 * hardware specific access to control-lines
84 */
Boris Brezillon0f808c12018-09-06 14:05:26 +020085static void cmx270_hwcontrol(struct nand_chip *this, int dat,
Mike Rapoport54d33c42007-04-22 08:53:21 +030086 unsigned int ctrl)
87{
Boris Brezillon82fc5092018-09-07 00:38:34 +020088 unsigned int nandaddr = (unsigned int)this->legacy.IO_ADDR_W;
Mike Rapoport54d33c42007-04-22 08:53:21 +030089
Mike Rapoport70eb33d2008-06-17 09:48:46 +010090 dsb();
Mike Rapoport54d33c42007-04-22 08:53:21 +030091
92 if (ctrl & NAND_CTRL_CHANGE) {
93 if ( ctrl & NAND_ALE )
94 nandaddr |= (1 << 3);
95 else
96 nandaddr &= ~(1 << 3);
97 if ( ctrl & NAND_CLE )
98 nandaddr |= (1 << 2);
99 else
100 nandaddr &= ~(1 << 2);
101 if ( ctrl & NAND_NCE )
102 nand_cs_on();
103 else
104 nand_cs_off();
105 }
106
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100107 dsb();
Boris Brezillon82fc5092018-09-07 00:38:34 +0200108 this->legacy.IO_ADDR_W = (void __iomem*)nandaddr;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300109 if (dat != NAND_CMD_NONE)
Boris Brezillon82fc5092018-09-07 00:38:34 +0200110 writel((dat << 16), this->legacy.IO_ADDR_W);
Mike Rapoport54d33c42007-04-22 08:53:21 +0300111
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100112 dsb();
Mike Rapoport54d33c42007-04-22 08:53:21 +0300113}
114
115/*
116 * read device ready pin
117 */
Boris Brezillon50a487e2018-09-06 14:05:27 +0200118static int cmx270_device_ready(struct nand_chip *this)
Mike Rapoport54d33c42007-04-22 08:53:21 +0300119{
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100120 dsb();
Mike Rapoport54d33c42007-04-22 08:53:21 +0300121
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100122 return (gpio_get_value(GPIO_NAND_RB));
Mike Rapoport54d33c42007-04-22 08:53:21 +0300123}
124
125/*
126 * Main initialization routine
127 */
Peter Huewe627df232009-06-11 02:23:33 +0200128static int __init cmx270_init(void)
Mike Rapoport54d33c42007-04-22 08:53:21 +0300129{
130 struct nand_chip *this;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300131 int ret;
132
Mike Rapoporta7f3f032008-10-05 10:26:55 +0100133 if (!(machine_is_armcore() && cpu_is_pxa27x()))
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100134 return -ENODEV;
135
136 ret = gpio_request(GPIO_NAND_CS, "NAND CS");
137 if (ret) {
Joe Perchese8348dc2017-02-16 23:11:37 -0800138 pr_warn("CM-X270: failed to request NAND CS gpio\n");
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100139 return ret;
140 }
141
142 gpio_direction_output(GPIO_NAND_CS, 1);
143
144 ret = gpio_request(GPIO_NAND_RB, "NAND R/B");
145 if (ret) {
Joe Perchese8348dc2017-02-16 23:11:37 -0800146 pr_warn("CM-X270: failed to request NAND R/B gpio\n");
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100147 goto err_gpio_request;
148 }
149
150 gpio_direction_input(GPIO_NAND_RB);
151
Mike Rapoport54d33c42007-04-22 08:53:21 +0300152 /* Allocate memory for MTD device structure and private data */
Boris BREZILLON2afd14f2015-12-10 08:59:56 +0100153 this = kzalloc(sizeof(struct nand_chip), GFP_KERNEL);
154 if (!this) {
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100155 ret = -ENOMEM;
156 goto err_kzalloc;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300157 }
158
159 cmx270_nand_io = ioremap(PXA_CS1_PHYS, 12);
160 if (!cmx270_nand_io) {
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100161 pr_debug("Unable to ioremap NAND device\n");
Mike Rapoport54d33c42007-04-22 08:53:21 +0300162 ret = -EINVAL;
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100163 goto err_ioremap;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300164 }
165
Boris BREZILLON2afd14f2015-12-10 08:59:56 +0100166 cmx270_nand_mtd = nand_to_mtd(this);
Mike Rapoport54d33c42007-04-22 08:53:21 +0300167
168 /* Link the private data with the MTD structure */
169 cmx270_nand_mtd->owner = THIS_MODULE;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300170
171 /* insert callbacks */
Boris Brezillon82fc5092018-09-07 00:38:34 +0200172 this->legacy.IO_ADDR_R = cmx270_nand_io;
173 this->legacy.IO_ADDR_W = cmx270_nand_io;
Boris Brezillonbf6065c2018-09-07 00:38:36 +0200174 this->legacy.cmd_ctrl = cmx270_hwcontrol;
Boris Brezillon8395b752018-09-07 00:38:37 +0200175 this->legacy.dev_ready = cmx270_device_ready;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300176
177 /* 15 us command delay time */
Boris Brezillon3cece3a2018-09-07 00:38:41 +0200178 this->legacy.chip_delay = 20;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300179 this->ecc.mode = NAND_ECC_SOFT;
Rafał Miłeckid9944e12016-04-13 14:06:59 +0200180 this->ecc.algo = NAND_ECC_HAMMING;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300181
182 /* read/write functions */
Boris Brezillon716bbba2018-09-07 00:38:35 +0200183 this->legacy.read_byte = cmx270_read_byte;
184 this->legacy.read_buf = cmx270_read_buf;
185 this->legacy.write_buf = cmx270_write_buf;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300186
187 /* Scan to find existence of the device */
Boris Brezillon00ad3782018-09-06 14:05:14 +0200188 ret = nand_scan(this, 1);
Masahiro Yamada546fe032016-11-04 19:42:50 +0900189 if (ret) {
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100190 pr_notice("No NAND device\n");
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100191 goto err_scan;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300192 }
193
Mike Rapoport54d33c42007-04-22 08:53:21 +0300194 /* Register the partitions */
Rafał Miłecki29597ca2018-07-13 11:27:31 +0200195 ret = mtd_device_register(cmx270_nand_mtd, partition_info,
196 NUM_PARTITIONS);
Mike Rapoport54d33c42007-04-22 08:53:21 +0300197 if (ret)
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100198 goto err_scan;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300199
200 /* Return happy */
201 return 0;
202
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100203err_scan:
Mike Rapoport54d33c42007-04-22 08:53:21 +0300204 iounmap(cmx270_nand_io);
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100205err_ioremap:
Boris BREZILLON2afd14f2015-12-10 08:59:56 +0100206 kfree(this);
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100207err_kzalloc:
208 gpio_free(GPIO_NAND_RB);
209err_gpio_request:
210 gpio_free(GPIO_NAND_CS);
Mike Rapoport54d33c42007-04-22 08:53:21 +0300211
212 return ret;
213
214}
215module_init(cmx270_init);
216
217/*
218 * Clean up routine
219 */
Peter Huewe627df232009-06-11 02:23:33 +0200220static void __exit cmx270_cleanup(void)
Mike Rapoport54d33c42007-04-22 08:53:21 +0300221{
222 /* Release resources, unregister device */
Boris Brezillon59ac2762018-09-06 14:05:15 +0200223 nand_release(mtd_to_nand(cmx270_nand_mtd));
Mike Rapoport54d33c42007-04-22 08:53:21 +0300224
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100225 gpio_free(GPIO_NAND_RB);
226 gpio_free(GPIO_NAND_CS);
227
Mike Rapoport54d33c42007-04-22 08:53:21 +0300228 iounmap(cmx270_nand_io);
229
Boris BREZILLON2afd14f2015-12-10 08:59:56 +0100230 kfree(mtd_to_nand(cmx270_nand_mtd));
Mike Rapoport54d33c42007-04-22 08:53:21 +0300231}
232module_exit(cmx270_cleanup);
233
234MODULE_LICENSE("GPL");
235MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
236MODULE_DESCRIPTION("NAND flash driver for Compulab CM-X270 Module");