blob: 18f10ae92dfc05b09552f9ca1d6a076d6888b164 [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{
Boris Brezillon82fc5092018-09-07 00:38:34 +020054 return (readl(this->legacy.IO_ADDR_R) >> 16);
Mike Rapoport54d33c42007-04-22 08:53:21 +030055}
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++)
Boris Brezillon82fc5092018-09-07 00:38:34 +020063 writel((*buf++ << 16), this->legacy.IO_ADDR_W);
Mike Rapoport54d33c42007-04-22 08:53:21 +030064}
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++)
Boris Brezillon82fc5092018-09-07 00:38:34 +020071 *buf++ = readl(this->legacy.IO_ADDR_R) >> 16;
Mike Rapoport54d33c42007-04-22 08:53:21 +030072}
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 */
Boris Brezillon0f808c12018-09-06 14:05:26 +020089static void cmx270_hwcontrol(struct nand_chip *this, int dat,
Mike Rapoport54d33c42007-04-22 08:53:21 +030090 unsigned int ctrl)
91{
Boris Brezillon82fc5092018-09-07 00:38:34 +020092 unsigned int nandaddr = (unsigned int)this->legacy.IO_ADDR_W;
Mike Rapoport54d33c42007-04-22 08:53:21 +030093
Mike Rapoport70eb33d2008-06-17 09:48:46 +010094 dsb();
Mike Rapoport54d33c42007-04-22 08:53:21 +030095
96 if (ctrl & NAND_CTRL_CHANGE) {
97 if ( ctrl & NAND_ALE )
98 nandaddr |= (1 << 3);
99 else
100 nandaddr &= ~(1 << 3);
101 if ( ctrl & NAND_CLE )
102 nandaddr |= (1 << 2);
103 else
104 nandaddr &= ~(1 << 2);
105 if ( ctrl & NAND_NCE )
106 nand_cs_on();
107 else
108 nand_cs_off();
109 }
110
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100111 dsb();
Boris Brezillon82fc5092018-09-07 00:38:34 +0200112 this->legacy.IO_ADDR_W = (void __iomem*)nandaddr;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300113 if (dat != NAND_CMD_NONE)
Boris Brezillon82fc5092018-09-07 00:38:34 +0200114 writel((dat << 16), this->legacy.IO_ADDR_W);
Mike Rapoport54d33c42007-04-22 08:53:21 +0300115
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100116 dsb();
Mike Rapoport54d33c42007-04-22 08:53:21 +0300117}
118
119/*
120 * read device ready pin
121 */
Boris Brezillon50a487e2018-09-06 14:05:27 +0200122static int cmx270_device_ready(struct nand_chip *this)
Mike Rapoport54d33c42007-04-22 08:53:21 +0300123{
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100124 dsb();
Mike Rapoport54d33c42007-04-22 08:53:21 +0300125
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100126 return (gpio_get_value(GPIO_NAND_RB));
Mike Rapoport54d33c42007-04-22 08:53:21 +0300127}
128
129/*
130 * Main initialization routine
131 */
Peter Huewe627df232009-06-11 02:23:33 +0200132static int __init cmx270_init(void)
Mike Rapoport54d33c42007-04-22 08:53:21 +0300133{
134 struct nand_chip *this;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300135 int ret;
136
Mike Rapoporta7f3f032008-10-05 10:26:55 +0100137 if (!(machine_is_armcore() && cpu_is_pxa27x()))
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100138 return -ENODEV;
139
140 ret = gpio_request(GPIO_NAND_CS, "NAND CS");
141 if (ret) {
Joe Perchese8348dc2017-02-16 23:11:37 -0800142 pr_warn("CM-X270: failed to request NAND CS gpio\n");
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100143 return ret;
144 }
145
146 gpio_direction_output(GPIO_NAND_CS, 1);
147
148 ret = gpio_request(GPIO_NAND_RB, "NAND R/B");
149 if (ret) {
Joe Perchese8348dc2017-02-16 23:11:37 -0800150 pr_warn("CM-X270: failed to request NAND R/B gpio\n");
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100151 goto err_gpio_request;
152 }
153
154 gpio_direction_input(GPIO_NAND_RB);
155
Mike Rapoport54d33c42007-04-22 08:53:21 +0300156 /* Allocate memory for MTD device structure and private data */
Boris BREZILLON2afd14f2015-12-10 08:59:56 +0100157 this = kzalloc(sizeof(struct nand_chip), GFP_KERNEL);
158 if (!this) {
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100159 ret = -ENOMEM;
160 goto err_kzalloc;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300161 }
162
163 cmx270_nand_io = ioremap(PXA_CS1_PHYS, 12);
164 if (!cmx270_nand_io) {
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100165 pr_debug("Unable to ioremap NAND device\n");
Mike Rapoport54d33c42007-04-22 08:53:21 +0300166 ret = -EINVAL;
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100167 goto err_ioremap;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300168 }
169
Boris BREZILLON2afd14f2015-12-10 08:59:56 +0100170 cmx270_nand_mtd = nand_to_mtd(this);
Mike Rapoport54d33c42007-04-22 08:53:21 +0300171
172 /* Link the private data with the MTD structure */
173 cmx270_nand_mtd->owner = THIS_MODULE;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300174
175 /* insert callbacks */
Boris Brezillon82fc5092018-09-07 00:38:34 +0200176 this->legacy.IO_ADDR_R = cmx270_nand_io;
177 this->legacy.IO_ADDR_W = cmx270_nand_io;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300178 this->cmd_ctrl = cmx270_hwcontrol;
179 this->dev_ready = cmx270_device_ready;
180
181 /* 15 us command delay time */
182 this->chip_delay = 20;
183 this->ecc.mode = NAND_ECC_SOFT;
Rafał Miłeckid9944e12016-04-13 14:06:59 +0200184 this->ecc.algo = NAND_ECC_HAMMING;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300185
186 /* read/write functions */
Boris Brezillon716bbba2018-09-07 00:38:35 +0200187 this->legacy.read_byte = cmx270_read_byte;
188 this->legacy.read_buf = cmx270_read_buf;
189 this->legacy.write_buf = cmx270_write_buf;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300190
191 /* Scan to find existence of the device */
Boris Brezillon00ad3782018-09-06 14:05:14 +0200192 ret = nand_scan(this, 1);
Masahiro Yamada546fe032016-11-04 19:42:50 +0900193 if (ret) {
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100194 pr_notice("No NAND device\n");
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100195 goto err_scan;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300196 }
197
Mike Rapoport54d33c42007-04-22 08:53:21 +0300198 /* Register the partitions */
Rafał Miłecki29597ca2018-07-13 11:27:31 +0200199 ret = mtd_device_register(cmx270_nand_mtd, partition_info,
200 NUM_PARTITIONS);
Mike Rapoport54d33c42007-04-22 08:53:21 +0300201 if (ret)
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100202 goto err_scan;
Mike Rapoport54d33c42007-04-22 08:53:21 +0300203
204 /* Return happy */
205 return 0;
206
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100207err_scan:
Mike Rapoport54d33c42007-04-22 08:53:21 +0300208 iounmap(cmx270_nand_io);
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100209err_ioremap:
Boris BREZILLON2afd14f2015-12-10 08:59:56 +0100210 kfree(this);
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100211err_kzalloc:
212 gpio_free(GPIO_NAND_RB);
213err_gpio_request:
214 gpio_free(GPIO_NAND_CS);
Mike Rapoport54d33c42007-04-22 08:53:21 +0300215
216 return ret;
217
218}
219module_init(cmx270_init);
220
221/*
222 * Clean up routine
223 */
Peter Huewe627df232009-06-11 02:23:33 +0200224static void __exit cmx270_cleanup(void)
Mike Rapoport54d33c42007-04-22 08:53:21 +0300225{
226 /* Release resources, unregister device */
Boris Brezillon59ac2762018-09-06 14:05:15 +0200227 nand_release(mtd_to_nand(cmx270_nand_mtd));
Mike Rapoport54d33c42007-04-22 08:53:21 +0300228
Mike Rapoport70eb33d2008-06-17 09:48:46 +0100229 gpio_free(GPIO_NAND_RB);
230 gpio_free(GPIO_NAND_CS);
231
Mike Rapoport54d33c42007-04-22 08:53:21 +0300232 iounmap(cmx270_nand_io);
233
Boris BREZILLON2afd14f2015-12-10 08:59:56 +0100234 kfree(mtd_to_nand(cmx270_nand_mtd));
Mike Rapoport54d33c42007-04-22 08:53:21 +0300235}
236module_exit(cmx270_cleanup);
237
238MODULE_LICENSE("GPL");
239MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
240MODULE_DESCRIPTION("NAND flash driver for Compulab CM-X270 Module");