blob: 24ba7296ec08c7abb48a871bef47984415cf9e59 [file] [log] [blame]
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +01001/*
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +01002 * Copyright (C) 2006 Jonathan McDowell <noodles@earth.li>
3 *
Boris Brezillon187c54482018-02-05 23:02:02 +01004 * Derived from drivers/mtd/nand/toto.c (removed in v2.6.28)
Boris Brezillon7b6afee2018-02-05 23:02:03 +01005 * Copyright (c) 2003 Texas Instruments
6 * Copyright (c) 2002 Thomas Gleixner <tgxl@linutronix.de>
7 *
Janusz Krzysztofik7e95d1f12010-12-14 21:09:40 +01008 * Converted to platform driver by Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>
Boris Brezillon93cbd6f32018-02-05 23:02:00 +01009 * Partially stolen from plat_nand.c
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 *
15 * Overview:
16 * This is a device driver for the NAND flash device found on the
17 * Amstrad E3 (Delta).
18 */
19
20#include <linux/slab.h>
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010021#include <linux/module.h>
22#include <linux/delay.h>
23#include <linux/mtd/mtd.h>
Boris Brezillond4092d72017-08-04 17:29:10 +020024#include <linux/mtd/rawnand.h>
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010025#include <linux/mtd/partitions.h>
Janusz Krzysztofik68f06762011-12-20 00:08:55 +010026#include <linux/gpio.h>
Tony Lindgren4b254082012-08-30 15:37:24 -070027#include <linux/platform_data/gpio-omap.h>
28
29#include <asm/io.h>
30#include <asm/sizes.h>
31
Tony Lindgrene27e35e2012-09-19 10:33:43 -070032#include <mach/board-ams-delta.h>
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010033
Tony Lindgren4b254082012-08-30 15:37:24 -070034#include <mach/hardware.h>
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010035
36/*
37 * MTD structure for E3 (Delta)
38 */
39static struct mtd_info *ams_delta_mtd = NULL;
40
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010041/*
42 * Define partitions for flash devices
43 */
44
Arvind Yadavd4906682017-08-28 13:54:57 +053045static const struct mtd_partition partition_info[] = {
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010046 { .name = "Kernel",
47 .offset = 0,
48 .size = 3 * SZ_1M + SZ_512K },
49 { .name = "u-boot",
50 .offset = 3 * SZ_1M + SZ_512K,
51 .size = SZ_256K },
52 { .name = "u-boot params",
53 .offset = 3 * SZ_1M + SZ_512K + SZ_256K,
54 .size = SZ_256K },
55 { .name = "Amstrad LDR",
56 .offset = 4 * SZ_1M,
57 .size = SZ_256K },
58 { .name = "File system",
59 .offset = 4 * SZ_1M + 1 * SZ_256K,
60 .size = 27 * SZ_1M },
61 { .name = "PBL reserved",
62 .offset = 32 * SZ_1M - 3 * SZ_256K,
63 .size = 3 * SZ_256K },
64};
65
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010066static void ams_delta_write_byte(struct mtd_info *mtd, u_char byte)
67{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +010068 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +010069 void __iomem *io_base = (void __iomem *)nand_get_controller_data(this);
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010070
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +010071 writew(0, io_base + OMAP_MPUIO_IO_CNTL);
72 writew(byte, this->IO_ADDR_W);
Janusz Krzysztofik68f06762011-12-20 00:08:55 +010073 gpio_set_value(AMS_DELTA_GPIO_PIN_NAND_NWE, 0);
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010074 ndelay(40);
Janusz Krzysztofik68f06762011-12-20 00:08:55 +010075 gpio_set_value(AMS_DELTA_GPIO_PIN_NAND_NWE, 1);
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010076}
77
78static u_char ams_delta_read_byte(struct mtd_info *mtd)
79{
80 u_char res;
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +010081 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +010082 void __iomem *io_base = (void __iomem *)nand_get_controller_data(this);
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010083
Janusz Krzysztofik68f06762011-12-20 00:08:55 +010084 gpio_set_value(AMS_DELTA_GPIO_PIN_NAND_NRE, 0);
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010085 ndelay(40);
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +010086 writew(~0, io_base + OMAP_MPUIO_IO_CNTL);
87 res = readw(this->IO_ADDR_R);
Janusz Krzysztofik68f06762011-12-20 00:08:55 +010088 gpio_set_value(AMS_DELTA_GPIO_PIN_NAND_NRE, 1);
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010089
90 return res;
91}
92
93static void ams_delta_write_buf(struct mtd_info *mtd, const u_char *buf,
94 int len)
95{
96 int i;
97
98 for (i=0; i<len; i++)
99 ams_delta_write_byte(mtd, buf[i]);
100}
101
102static void ams_delta_read_buf(struct mtd_info *mtd, u_char *buf, int len)
103{
104 int i;
105
106 for (i=0; i<len; i++)
107 buf[i] = ams_delta_read_byte(mtd);
108}
109
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200110/*
111 * Command control function
112 *
113 * ctrl:
114 * NAND_NCE: bit 0 -> bit 2
115 * NAND_CLE: bit 1 -> bit 7
116 * NAND_ALE: bit 2 -> bit 6
117 */
118static void ams_delta_hwcontrol(struct mtd_info *mtd, int cmd,
119 unsigned int ctrl)
120{
121
122 if (ctrl & NAND_CTRL_CHANGE) {
Janusz Krzysztofik68f06762011-12-20 00:08:55 +0100123 gpio_set_value(AMS_DELTA_GPIO_PIN_NAND_NCE,
124 (ctrl & NAND_NCE) == 0);
125 gpio_set_value(AMS_DELTA_GPIO_PIN_NAND_CLE,
126 (ctrl & NAND_CLE) != 0);
127 gpio_set_value(AMS_DELTA_GPIO_PIN_NAND_ALE,
128 (ctrl & NAND_ALE) != 0);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200129 }
130
131 if (cmd != NAND_CMD_NONE)
132 ams_delta_write_byte(mtd, cmd);
133}
134
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100135static int ams_delta_nand_ready(struct mtd_info *mtd)
136{
David Brownell93a22f82008-10-15 22:03:15 -0700137 return gpio_get_value(AMS_DELTA_GPIO_PIN_NAND_RB);
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100138}
139
Janusz Krzysztofikda564a02012-02-10 17:48:43 +0100140static const struct gpio _mandatory_gpio[] = {
Janusz Krzysztofik68f06762011-12-20 00:08:55 +0100141 {
142 .gpio = AMS_DELTA_GPIO_PIN_NAND_NCE,
143 .flags = GPIOF_OUT_INIT_HIGH,
144 .label = "nand_nce",
145 },
146 {
147 .gpio = AMS_DELTA_GPIO_PIN_NAND_NRE,
148 .flags = GPIOF_OUT_INIT_HIGH,
149 .label = "nand_nre",
150 },
151 {
152 .gpio = AMS_DELTA_GPIO_PIN_NAND_NWP,
153 .flags = GPIOF_OUT_INIT_HIGH,
154 .label = "nand_nwp",
155 },
156 {
157 .gpio = AMS_DELTA_GPIO_PIN_NAND_NWE,
158 .flags = GPIOF_OUT_INIT_HIGH,
159 .label = "nand_nwe",
160 },
161 {
162 .gpio = AMS_DELTA_GPIO_PIN_NAND_ALE,
163 .flags = GPIOF_OUT_INIT_LOW,
164 .label = "nand_ale",
165 },
166 {
167 .gpio = AMS_DELTA_GPIO_PIN_NAND_CLE,
168 .flags = GPIOF_OUT_INIT_LOW,
169 .label = "nand_cle",
170 },
171};
172
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100173/*
174 * Main initialization routine
175 */
Bill Pemberton06f25512012-11-19 13:23:07 -0500176static int ams_delta_init(struct platform_device *pdev)
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100177{
178 struct nand_chip *this;
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +0100179 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
180 void __iomem *io_base;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100181 int err = 0;
182
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +0100183 if (!res)
184 return -ENXIO;
185
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100186 /* Allocate memory for MTD device structure and private data */
Boris BREZILLON187d6ad2015-12-10 08:59:49 +0100187 this = kzalloc(sizeof(struct nand_chip), GFP_KERNEL);
188 if (!this) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +0530189 pr_warn("Unable to allocate E3 NAND MTD device structure.\n");
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100190 err = -ENOMEM;
191 goto out;
192 }
193
Boris BREZILLON187d6ad2015-12-10 08:59:49 +0100194 ams_delta_mtd = nand_to_mtd(this);
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100195 ams_delta_mtd->owner = THIS_MODULE;
196
Janusz Krzysztofikb0272742012-05-07 22:51:37 +0200197 /*
198 * Don't try to request the memory region from here,
199 * it should have been already requested from the
200 * gpio-omap driver and requesting it again would fail.
201 */
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +0100202
203 io_base = ioremap(res->start, resource_size(res));
204 if (io_base == NULL) {
205 dev_err(&pdev->dev, "ioremap failed\n");
206 err = -EIO;
Janusz Krzysztofikb0272742012-05-07 22:51:37 +0200207 goto out_free;
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +0100208 }
209
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100210 nand_set_controller_data(this, (void *)io_base);
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +0100211
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100212 /* Set address of NAND IO lines */
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +0100213 this->IO_ADDR_R = io_base + OMAP_MPUIO_INPUT_LATCH;
214 this->IO_ADDR_W = io_base + OMAP_MPUIO_OUTPUT;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100215 this->read_byte = ams_delta_read_byte;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100216 this->write_buf = ams_delta_write_buf;
217 this->read_buf = ams_delta_read_buf;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200218 this->cmd_ctrl = ams_delta_hwcontrol;
David Brownell93a22f82008-10-15 22:03:15 -0700219 if (gpio_request(AMS_DELTA_GPIO_PIN_NAND_RB, "nand_rdy") == 0) {
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100220 this->dev_ready = ams_delta_nand_ready;
221 } else {
222 this->dev_ready = NULL;
Shreeya Patel63fa37f2018-02-22 22:01:22 +0530223 pr_notice("Couldn't request gpio for Delta NAND ready.\n");
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100224 }
225 /* 25 us command delay time */
226 this->chip_delay = 30;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +0200227 this->ecc.mode = NAND_ECC_SOFT;
Rafał Miłeckie58dd3c2016-04-08 12:23:44 +0200228 this->ecc.algo = NAND_ECC_HAMMING;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100229
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +0100230 platform_set_drvdata(pdev, io_base);
231
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100232 /* Set chip enabled, but */
Janusz Krzysztofik68f06762011-12-20 00:08:55 +0100233 err = gpio_request_array(_mandatory_gpio, ARRAY_SIZE(_mandatory_gpio));
234 if (err)
235 goto out_gpio;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100236
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300237 /* Scan to find existence of the device */
Boris Brezillon00ad3782018-09-06 14:05:14 +0200238 err = nand_scan(this, 1);
Masahiro Yamada0d0aa862016-11-04 19:42:49 +0900239 if (err)
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100240 goto out_mtd;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100241
242 /* Register the partitions */
Jamie Ilesee0e87b2011-05-23 10:23:40 +0100243 mtd_device_register(ams_delta_mtd, partition_info,
244 ARRAY_SIZE(partition_info));
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100245
246 goto out;
247
248 out_mtd:
Janusz Krzysztofik68f06762011-12-20 00:08:55 +0100249 gpio_free_array(_mandatory_gpio, ARRAY_SIZE(_mandatory_gpio));
250out_gpio:
Janusz Krzysztofik68f06762011-12-20 00:08:55 +0100251 gpio_free(AMS_DELTA_GPIO_PIN_NAND_RB);
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +0100252 iounmap(io_base);
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +0100253out_free:
Boris BREZILLON187d6ad2015-12-10 08:59:49 +0100254 kfree(this);
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100255 out:
256 return err;
257}
258
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100259/*
260 * Clean up routine
261 */
Bill Pemberton810b7e02012-11-19 13:26:04 -0500262static int ams_delta_cleanup(struct platform_device *pdev)
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100263{
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +0100264 void __iomem *io_base = platform_get_drvdata(pdev);
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +0100265
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100266 /* Release resources, unregister device */
267 nand_release(ams_delta_mtd);
268
Janusz Krzysztofik68f06762011-12-20 00:08:55 +0100269 gpio_free_array(_mandatory_gpio, ARRAY_SIZE(_mandatory_gpio));
270 gpio_free(AMS_DELTA_GPIO_PIN_NAND_RB);
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +0100271 iounmap(io_base);
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +0100272
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100273 /* Free the MTD device structure */
Boris BREZILLON187d6ad2015-12-10 08:59:49 +0100274 kfree(mtd_to_nand(ams_delta_mtd));
Janusz Krzysztofik7e95d1f12010-12-14 21:09:40 +0100275
276 return 0;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100277}
Janusz Krzysztofik7e95d1f12010-12-14 21:09:40 +0100278
279static struct platform_driver ams_delta_nand_driver = {
280 .probe = ams_delta_init,
Bill Pemberton5153b882012-11-19 13:21:24 -0500281 .remove = ams_delta_cleanup,
Janusz Krzysztofik7e95d1f12010-12-14 21:09:40 +0100282 .driver = {
283 .name = "ams-delta-nand",
Janusz Krzysztofik7e95d1f12010-12-14 21:09:40 +0100284 },
285};
286
Axel Linf99640d2011-11-27 20:45:03 +0800287module_platform_driver(ams_delta_nand_driver);
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100288
289MODULE_LICENSE("GPL");
290MODULE_AUTHOR("Jonathan McDowell <noodles@earth.li>");
291MODULE_DESCRIPTION("Glue layer for NAND flash on Amstrad E3 (Delta)");