blob: 68b58c85789c354f16886e2c8d8045dec137bd43 [file] [log] [blame]
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +01001/*
2 * drivers/mtd/nand/ams-delta.c
3 *
4 * Copyright (C) 2006 Jonathan McDowell <noodles@earth.li>
5 *
6 * Derived from drivers/mtd/toto.c
Janusz Krzysztofik7e95d1f2010-12-14 21:09:40 +01007 * Converted to platform driver by Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +01008 * Partially stolen from drivers/mtd/nand/plat_nand.c
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +01009 *
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 * Amstrad E3 (Delta).
17 */
18
19#include <linux/slab.h>
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010020#include <linux/module.h>
21#include <linux/delay.h>
22#include <linux/mtd/mtd.h>
23#include <linux/mtd/nand.h>
24#include <linux/mtd/partitions.h>
Janusz Krzysztofik68f06762011-12-20 00:08:55 +010025#include <linux/gpio.h>
Tony Lindgren4b254082012-08-30 15:37:24 -070026#include <linux/platform_data/gpio-omap.h>
27
28#include <asm/io.h>
29#include <asm/sizes.h>
30
Tony Lindgrene27e35e2012-09-19 10:33:43 -070031#include <mach/board-ams-delta.h>
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010032
Tony Lindgren4b254082012-08-30 15:37:24 -070033#include <mach/hardware.h>
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010034
35/*
36 * MTD structure for E3 (Delta)
37 */
38static struct mtd_info *ams_delta_mtd = NULL;
39
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010040/*
41 * Define partitions for flash devices
42 */
43
44static struct mtd_partition partition_info[] = {
45 { .name = "Kernel",
46 .offset = 0,
47 .size = 3 * SZ_1M + SZ_512K },
48 { .name = "u-boot",
49 .offset = 3 * SZ_1M + SZ_512K,
50 .size = SZ_256K },
51 { .name = "u-boot params",
52 .offset = 3 * SZ_1M + SZ_512K + SZ_256K,
53 .size = SZ_256K },
54 { .name = "Amstrad LDR",
55 .offset = 4 * SZ_1M,
56 .size = SZ_256K },
57 { .name = "File system",
58 .offset = 4 * SZ_1M + 1 * SZ_256K,
59 .size = 27 * SZ_1M },
60 { .name = "PBL reserved",
61 .offset = 32 * SZ_1M - 3 * SZ_256K,
62 .size = 3 * SZ_256K },
63};
64
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010065static void ams_delta_write_byte(struct mtd_info *mtd, u_char byte)
66{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +010067 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +010068 void __iomem *io_base = (void __iomem *)nand_get_controller_data(this);
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010069
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +010070 writew(0, io_base + OMAP_MPUIO_IO_CNTL);
71 writew(byte, this->IO_ADDR_W);
Janusz Krzysztofik68f06762011-12-20 00:08:55 +010072 gpio_set_value(AMS_DELTA_GPIO_PIN_NAND_NWE, 0);
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010073 ndelay(40);
Janusz Krzysztofik68f06762011-12-20 00:08:55 +010074 gpio_set_value(AMS_DELTA_GPIO_PIN_NAND_NWE, 1);
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010075}
76
77static u_char ams_delta_read_byte(struct mtd_info *mtd)
78{
79 u_char res;
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +010080 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +010081 void __iomem *io_base = (void __iomem *)nand_get_controller_data(this);
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010082
Janusz Krzysztofik68f06762011-12-20 00:08:55 +010083 gpio_set_value(AMS_DELTA_GPIO_PIN_NAND_NRE, 0);
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010084 ndelay(40);
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +010085 writew(~0, io_base + OMAP_MPUIO_IO_CNTL);
86 res = readw(this->IO_ADDR_R);
Janusz Krzysztofik68f06762011-12-20 00:08:55 +010087 gpio_set_value(AMS_DELTA_GPIO_PIN_NAND_NRE, 1);
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010088
89 return res;
90}
91
92static void ams_delta_write_buf(struct mtd_info *mtd, const u_char *buf,
93 int len)
94{
95 int i;
96
97 for (i=0; i<len; i++)
98 ams_delta_write_byte(mtd, buf[i]);
99}
100
101static void ams_delta_read_buf(struct mtd_info *mtd, u_char *buf, int len)
102{
103 int i;
104
105 for (i=0; i<len; i++)
106 buf[i] = ams_delta_read_byte(mtd);
107}
108
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200109/*
110 * Command control function
111 *
112 * ctrl:
113 * NAND_NCE: bit 0 -> bit 2
114 * NAND_CLE: bit 1 -> bit 7
115 * NAND_ALE: bit 2 -> bit 6
116 */
117static void ams_delta_hwcontrol(struct mtd_info *mtd, int cmd,
118 unsigned int ctrl)
119{
120
121 if (ctrl & NAND_CTRL_CHANGE) {
Janusz Krzysztofik68f06762011-12-20 00:08:55 +0100122 gpio_set_value(AMS_DELTA_GPIO_PIN_NAND_NCE,
123 (ctrl & NAND_NCE) == 0);
124 gpio_set_value(AMS_DELTA_GPIO_PIN_NAND_CLE,
125 (ctrl & NAND_CLE) != 0);
126 gpio_set_value(AMS_DELTA_GPIO_PIN_NAND_ALE,
127 (ctrl & NAND_ALE) != 0);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200128 }
129
130 if (cmd != NAND_CMD_NONE)
131 ams_delta_write_byte(mtd, cmd);
132}
133
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100134static int ams_delta_nand_ready(struct mtd_info *mtd)
135{
David Brownell93a22f82008-10-15 22:03:15 -0700136 return gpio_get_value(AMS_DELTA_GPIO_PIN_NAND_RB);
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100137}
138
Janusz Krzysztofikda564a02012-02-10 17:48:43 +0100139static const struct gpio _mandatory_gpio[] = {
Janusz Krzysztofik68f06762011-12-20 00:08:55 +0100140 {
141 .gpio = AMS_DELTA_GPIO_PIN_NAND_NCE,
142 .flags = GPIOF_OUT_INIT_HIGH,
143 .label = "nand_nce",
144 },
145 {
146 .gpio = AMS_DELTA_GPIO_PIN_NAND_NRE,
147 .flags = GPIOF_OUT_INIT_HIGH,
148 .label = "nand_nre",
149 },
150 {
151 .gpio = AMS_DELTA_GPIO_PIN_NAND_NWP,
152 .flags = GPIOF_OUT_INIT_HIGH,
153 .label = "nand_nwp",
154 },
155 {
156 .gpio = AMS_DELTA_GPIO_PIN_NAND_NWE,
157 .flags = GPIOF_OUT_INIT_HIGH,
158 .label = "nand_nwe",
159 },
160 {
161 .gpio = AMS_DELTA_GPIO_PIN_NAND_ALE,
162 .flags = GPIOF_OUT_INIT_LOW,
163 .label = "nand_ale",
164 },
165 {
166 .gpio = AMS_DELTA_GPIO_PIN_NAND_CLE,
167 .flags = GPIOF_OUT_INIT_LOW,
168 .label = "nand_cle",
169 },
170};
171
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100172/*
173 * Main initialization routine
174 */
Bill Pemberton06f25512012-11-19 13:23:07 -0500175static int ams_delta_init(struct platform_device *pdev)
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100176{
177 struct nand_chip *this;
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +0100178 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
179 void __iomem *io_base;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100180 int err = 0;
181
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +0100182 if (!res)
183 return -ENXIO;
184
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100185 /* Allocate memory for MTD device structure and private data */
Boris BREZILLON187d6ad2015-12-10 08:59:49 +0100186 this = kzalloc(sizeof(struct nand_chip), GFP_KERNEL);
187 if (!this) {
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100188 printk (KERN_WARNING "Unable to allocate E3 NAND MTD device structure.\n");
189 err = -ENOMEM;
190 goto out;
191 }
192
Boris BREZILLON187d6ad2015-12-10 08:59:49 +0100193 ams_delta_mtd = nand_to_mtd(this);
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100194 ams_delta_mtd->owner = THIS_MODULE;
195
Janusz Krzysztofikb0272742012-05-07 22:51:37 +0200196 /*
197 * Don't try to request the memory region from here,
198 * it should have been already requested from the
199 * gpio-omap driver and requesting it again would fail.
200 */
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +0100201
202 io_base = ioremap(res->start, resource_size(res));
203 if (io_base == NULL) {
204 dev_err(&pdev->dev, "ioremap failed\n");
205 err = -EIO;
Janusz Krzysztofikb0272742012-05-07 22:51:37 +0200206 goto out_free;
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +0100207 }
208
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100209 nand_set_controller_data(this, (void *)io_base);
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +0100210
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100211 /* Set address of NAND IO lines */
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +0100212 this->IO_ADDR_R = io_base + OMAP_MPUIO_INPUT_LATCH;
213 this->IO_ADDR_W = io_base + OMAP_MPUIO_OUTPUT;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100214 this->read_byte = ams_delta_read_byte;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100215 this->write_buf = ams_delta_write_buf;
216 this->read_buf = ams_delta_read_buf;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200217 this->cmd_ctrl = ams_delta_hwcontrol;
David Brownell93a22f82008-10-15 22:03:15 -0700218 if (gpio_request(AMS_DELTA_GPIO_PIN_NAND_RB, "nand_rdy") == 0) {
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100219 this->dev_ready = ams_delta_nand_ready;
220 } else {
221 this->dev_ready = NULL;
222 printk(KERN_NOTICE "Couldn't request gpio for Delta NAND ready.\n");
223 }
224 /* 25 us command delay time */
225 this->chip_delay = 30;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +0200226 this->ecc.mode = NAND_ECC_SOFT;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100227
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +0100228 platform_set_drvdata(pdev, io_base);
229
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100230 /* Set chip enabled, but */
Janusz Krzysztofik68f06762011-12-20 00:08:55 +0100231 err = gpio_request_array(_mandatory_gpio, ARRAY_SIZE(_mandatory_gpio));
232 if (err)
233 goto out_gpio;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100234
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300235 /* Scan to find existence of the device */
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100236 if (nand_scan(ams_delta_mtd, 1)) {
237 err = -ENXIO;
238 goto out_mtd;
239 }
240
241 /* Register the partitions */
Jamie Ilesee0e87b2011-05-23 10:23:40 +0100242 mtd_device_register(ams_delta_mtd, partition_info,
243 ARRAY_SIZE(partition_info));
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100244
245 goto out;
246
247 out_mtd:
Janusz Krzysztofik68f06762011-12-20 00:08:55 +0100248 gpio_free_array(_mandatory_gpio, ARRAY_SIZE(_mandatory_gpio));
249out_gpio:
Janusz Krzysztofik68f06762011-12-20 00:08:55 +0100250 gpio_free(AMS_DELTA_GPIO_PIN_NAND_RB);
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +0100251 iounmap(io_base);
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +0100252out_free:
Boris BREZILLON187d6ad2015-12-10 08:59:49 +0100253 kfree(this);
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100254 out:
255 return err;
256}
257
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100258/*
259 * Clean up routine
260 */
Bill Pemberton810b7e02012-11-19 13:26:04 -0500261static int ams_delta_cleanup(struct platform_device *pdev)
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100262{
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +0100263 void __iomem *io_base = platform_get_drvdata(pdev);
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +0100264
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100265 /* Release resources, unregister device */
266 nand_release(ams_delta_mtd);
267
Janusz Krzysztofik68f06762011-12-20 00:08:55 +0100268 gpio_free_array(_mandatory_gpio, ARRAY_SIZE(_mandatory_gpio));
269 gpio_free(AMS_DELTA_GPIO_PIN_NAND_RB);
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +0100270 iounmap(io_base);
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +0100271
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100272 /* Free the MTD device structure */
Boris BREZILLON187d6ad2015-12-10 08:59:49 +0100273 kfree(mtd_to_nand(ams_delta_mtd));
Janusz Krzysztofik7e95d1f2010-12-14 21:09:40 +0100274
275 return 0;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100276}
Janusz Krzysztofik7e95d1f2010-12-14 21:09:40 +0100277
278static struct platform_driver ams_delta_nand_driver = {
279 .probe = ams_delta_init,
Bill Pemberton5153b882012-11-19 13:21:24 -0500280 .remove = ams_delta_cleanup,
Janusz Krzysztofik7e95d1f2010-12-14 21:09:40 +0100281 .driver = {
282 .name = "ams-delta-nand",
Janusz Krzysztofik7e95d1f2010-12-14 21:09:40 +0100283 },
284};
285
Axel Linf99640d2011-11-27 20:45:03 +0800286module_platform_driver(ams_delta_nand_driver);
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100287
288MODULE_LICENSE("GPL");
289MODULE_AUTHOR("Jonathan McDowell <noodles@earth.li>");
290MODULE_DESCRIPTION("Glue layer for NAND flash on Amstrad E3 (Delta)");