]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/mtd/nand/raw/cmx270_nand.c
treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 500
[mirror_ubuntu-hirsute-kernel.git] / drivers / mtd / nand / raw / cmx270_nand.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
54d33c4c 2/*
54d33c4c
MR
3 * Copyright (C) 2006 Compulab, Ltd.
4 * Mike Rapoport <mike@compulab.co.il>
5 *
187c5448 6 * Derived from drivers/mtd/nand/h1910.c (removed in v3.10)
54d33c4c
MR
7 * Copyright (C) 2002 Marius Gröger (mag@sysgo.de)
8 * Copyright (c) 2001 Thomas Gleixner (gleixner@autronix.de)
9 *
54d33c4c
MR
10 * Overview:
11 * This is a device driver for the NAND flash device found on the
12 * CM-X270 board.
13 */
14
d4092d76 15#include <linux/mtd/rawnand.h>
54d33c4c 16#include <linux/mtd/partitions.h>
5a0e3ad6 17#include <linux/slab.h>
70eb33d6 18#include <linux/gpio.h>
a0e5cc58 19#include <linux/module.h>
54d33c4c
MR
20
21#include <asm/io.h>
22#include <asm/irq.h>
70eb33d6 23#include <asm/mach-types.h>
54d33c4c 24
b74d1969 25#include <mach/pxa2xx-regs.h>
54d33c4c
MR
26
27#define GPIO_NAND_CS (11)
28#define GPIO_NAND_RB (89)
29
54d33c4c
MR
30/* 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 */
d4906688 39static const struct mtd_partition partition_info[] = {
54d33c4c
MR
40 [0] = {
41 .name = "cmx270-0",
42 .offset = 0,
43 .size = MTDPART_SIZ_FULL
44 }
45};
46#define NUM_PARTITIONS (ARRAY_SIZE(partition_info))
47
7e534323 48static u_char cmx270_read_byte(struct nand_chip *this)
54d33c4c 49{
82fc5099 50 return (readl(this->legacy.IO_ADDR_R) >> 16);
54d33c4c
MR
51}
52
c0739d85
BB
53static void cmx270_write_buf(struct nand_chip *this, const u_char *buf,
54 int len)
54d33c4c
MR
55{
56 int i;
54d33c4c
MR
57
58 for (i=0; i<len; i++)
82fc5099 59 writel((*buf++ << 16), this->legacy.IO_ADDR_W);
54d33c4c
MR
60}
61
7e534323 62static void cmx270_read_buf(struct nand_chip *this, u_char *buf, int len)
54d33c4c
MR
63{
64 int i;
54d33c4c
MR
65
66 for (i=0; i<len; i++)
82fc5099 67 *buf++ = readl(this->legacy.IO_ADDR_R) >> 16;
54d33c4c
MR
68}
69
54d33c4c
MR
70static inline void nand_cs_on(void)
71{
70eb33d6 72 gpio_set_value(GPIO_NAND_CS, 0);
54d33c4c
MR
73}
74
75static void nand_cs_off(void)
76{
70eb33d6 77 dsb();
54d33c4c 78
70eb33d6 79 gpio_set_value(GPIO_NAND_CS, 1);
54d33c4c
MR
80}
81
82/*
83 * hardware specific access to control-lines
84 */
0f808c16 85static void cmx270_hwcontrol(struct nand_chip *this, int dat,
54d33c4c
MR
86 unsigned int ctrl)
87{
82fc5099 88 unsigned int nandaddr = (unsigned int)this->legacy.IO_ADDR_W;
54d33c4c 89
70eb33d6 90 dsb();
54d33c4c
MR
91
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
70eb33d6 107 dsb();
82fc5099 108 this->legacy.IO_ADDR_W = (void __iomem*)nandaddr;
54d33c4c 109 if (dat != NAND_CMD_NONE)
82fc5099 110 writel((dat << 16), this->legacy.IO_ADDR_W);
54d33c4c 111
70eb33d6 112 dsb();
54d33c4c
MR
113}
114
115/*
116 * read device ready pin
117 */
50a487e7 118static int cmx270_device_ready(struct nand_chip *this)
54d33c4c 119{
70eb33d6 120 dsb();
54d33c4c 121
70eb33d6 122 return (gpio_get_value(GPIO_NAND_RB));
54d33c4c
MR
123}
124
125/*
126 * Main initialization routine
127 */
627df23c 128static int __init cmx270_init(void)
54d33c4c
MR
129{
130 struct nand_chip *this;
54d33c4c
MR
131 int ret;
132
a7f3f030 133 if (!(machine_is_armcore() && cpu_is_pxa27x()))
70eb33d6
MR
134 return -ENODEV;
135
136 ret = gpio_request(GPIO_NAND_CS, "NAND CS");
137 if (ret) {
e8348dc5 138 pr_warn("CM-X270: failed to request NAND CS gpio\n");
70eb33d6
MR
139 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) {
e8348dc5 146 pr_warn("CM-X270: failed to request NAND R/B gpio\n");
70eb33d6
MR
147 goto err_gpio_request;
148 }
149
150 gpio_direction_input(GPIO_NAND_RB);
151
54d33c4c 152 /* Allocate memory for MTD device structure and private data */
2afd14f9
BB
153 this = kzalloc(sizeof(struct nand_chip), GFP_KERNEL);
154 if (!this) {
70eb33d6
MR
155 ret = -ENOMEM;
156 goto err_kzalloc;
54d33c4c
MR
157 }
158
159 cmx270_nand_io = ioremap(PXA_CS1_PHYS, 12);
160 if (!cmx270_nand_io) {
70eb33d6 161 pr_debug("Unable to ioremap NAND device\n");
54d33c4c 162 ret = -EINVAL;
70eb33d6 163 goto err_ioremap;
54d33c4c
MR
164 }
165
2afd14f9 166 cmx270_nand_mtd = nand_to_mtd(this);
54d33c4c
MR
167
168 /* Link the private data with the MTD structure */
169 cmx270_nand_mtd->owner = THIS_MODULE;
54d33c4c
MR
170
171 /* insert callbacks */
82fc5099
BB
172 this->legacy.IO_ADDR_R = cmx270_nand_io;
173 this->legacy.IO_ADDR_W = cmx270_nand_io;
bf6065c6 174 this->legacy.cmd_ctrl = cmx270_hwcontrol;
8395b753 175 this->legacy.dev_ready = cmx270_device_ready;
54d33c4c
MR
176
177 /* 15 us command delay time */
3cece3ab 178 this->legacy.chip_delay = 20;
54d33c4c 179 this->ecc.mode = NAND_ECC_SOFT;
d9944e1f 180 this->ecc.algo = NAND_ECC_HAMMING;
54d33c4c
MR
181
182 /* read/write functions */
716bbbab
BB
183 this->legacy.read_byte = cmx270_read_byte;
184 this->legacy.read_buf = cmx270_read_buf;
185 this->legacy.write_buf = cmx270_write_buf;
54d33c4c
MR
186
187 /* Scan to find existence of the device */
00ad378f 188 ret = nand_scan(this, 1);
546fe03f 189 if (ret) {
70eb33d6 190 pr_notice("No NAND device\n");
70eb33d6 191 goto err_scan;
54d33c4c
MR
192 }
193
54d33c4c 194 /* Register the partitions */
29597ca1
RM
195 ret = mtd_device_register(cmx270_nand_mtd, partition_info,
196 NUM_PARTITIONS);
54d33c4c 197 if (ret)
70eb33d6 198 goto err_scan;
54d33c4c
MR
199
200 /* Return happy */
201 return 0;
202
70eb33d6 203err_scan:
54d33c4c 204 iounmap(cmx270_nand_io);
70eb33d6 205err_ioremap:
2afd14f9 206 kfree(this);
70eb33d6
MR
207err_kzalloc:
208 gpio_free(GPIO_NAND_RB);
209err_gpio_request:
210 gpio_free(GPIO_NAND_CS);
54d33c4c
MR
211
212 return ret;
213
214}
215module_init(cmx270_init);
216
217/*
218 * Clean up routine
219 */
627df23c 220static void __exit cmx270_cleanup(void)
54d33c4c
MR
221{
222 /* Release resources, unregister device */
59ac276f 223 nand_release(mtd_to_nand(cmx270_nand_mtd));
54d33c4c 224
70eb33d6
MR
225 gpio_free(GPIO_NAND_RB);
226 gpio_free(GPIO_NAND_CS);
227
54d33c4c
MR
228 iounmap(cmx270_nand_io);
229
2afd14f9 230 kfree(mtd_to_nand(cmx270_nand_mtd));
54d33c4c
MR
231}
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");