blob: 98ba9493663186460edf729d7d9c0b19d0a6243b [file] [log] [blame]
Boris Brezillon43a0a452018-02-05 23:01:59 +01001/*
Ben Dooks7e74a502008-05-20 17:32:27 +01002 * Copyright © 2004-2008 Simtec Electronics
3 * http://armlinux.simtec.co.uk/
Ben Dooksfdf2fd52005-02-18 14:46:15 +00004 * Ben Dooks <ben@simtec.co.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Ben Dooks7e74a502008-05-20 17:32:27 +01006 * Samsung S3C2410/S3C2440/S3C2412 NAND driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21*/
22
Sachin Kamat92aeb5d2012-07-16 16:02:23 +053023#define pr_fmt(fmt) "nand-s3c2410: " fmt
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#ifdef CONFIG_MTD_NAND_S3C2410_DEBUG
26#define DEBUG
27#endif
28
29#include <linux/module.h>
30#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/kernel.h>
32#include <linux/string.h>
Sachin Kamatd2a89be2012-07-16 16:02:24 +053033#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/ioport.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010035#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/delay.h>
37#include <linux/err.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080038#include <linux/slab.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000039#include <linux/clk.h>
Ben Dooks30821fe2008-07-15 11:58:31 +010040#include <linux/cpufreq.h>
Sergio Prado1c825ad2016-10-26 21:59:55 -020041#include <linux/of.h>
42#include <linux/of_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#include <linux/mtd/mtd.h>
Boris Brezillond4092d72017-08-04 17:29:10 +020045#include <linux/mtd/rawnand.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/mtd/nand_ecc.h>
47#include <linux/mtd/partitions.h>
48
Arnd Bergmann436d42c2012-08-24 15:22:12 +020049#include <linux/platform_data/mtd-nand-s3c2410.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Sachin Kamat02d01862014-01-10 11:24:13 +053051#define S3C2410_NFREG(x) (x)
52
53#define S3C2410_NFCONF S3C2410_NFREG(0x00)
54#define S3C2410_NFCMD S3C2410_NFREG(0x04)
55#define S3C2410_NFADDR S3C2410_NFREG(0x08)
56#define S3C2410_NFDATA S3C2410_NFREG(0x0C)
57#define S3C2410_NFSTAT S3C2410_NFREG(0x10)
58#define S3C2410_NFECC S3C2410_NFREG(0x14)
59#define S3C2440_NFCONT S3C2410_NFREG(0x04)
60#define S3C2440_NFCMD S3C2410_NFREG(0x08)
61#define S3C2440_NFADDR S3C2410_NFREG(0x0C)
62#define S3C2440_NFDATA S3C2410_NFREG(0x10)
63#define S3C2440_NFSTAT S3C2410_NFREG(0x20)
64#define S3C2440_NFMECC0 S3C2410_NFREG(0x2C)
65#define S3C2412_NFSTAT S3C2410_NFREG(0x28)
66#define S3C2412_NFMECC0 S3C2410_NFREG(0x34)
67#define S3C2410_NFCONF_EN (1<<15)
68#define S3C2410_NFCONF_INITECC (1<<12)
69#define S3C2410_NFCONF_nFCE (1<<11)
70#define S3C2410_NFCONF_TACLS(x) ((x)<<8)
71#define S3C2410_NFCONF_TWRPH0(x) ((x)<<4)
72#define S3C2410_NFCONF_TWRPH1(x) ((x)<<0)
73#define S3C2410_NFSTAT_BUSY (1<<0)
74#define S3C2440_NFCONF_TACLS(x) ((x)<<12)
75#define S3C2440_NFCONF_TWRPH0(x) ((x)<<8)
76#define S3C2440_NFCONF_TWRPH1(x) ((x)<<4)
77#define S3C2440_NFCONT_INITECC (1<<4)
78#define S3C2440_NFCONT_nFCE (1<<1)
79#define S3C2440_NFCONT_ENABLE (1<<0)
80#define S3C2440_NFSTAT_READY (1<<0)
81#define S3C2412_NFCONF_NANDBOOT (1<<31)
82#define S3C2412_NFCONT_INIT_MAIN_ECC (1<<5)
83#define S3C2412_NFCONT_nFCE0 (1<<1)
84#define S3C2412_NFSTAT_READY (1<<0)
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086/* new oob placement block for use with hardware ecc generation
87 */
Boris Brezillonbf01e06b2016-02-03 20:03:30 +010088static int s3c2410_ooblayout_ecc(struct mtd_info *mtd, int section,
89 struct mtd_oob_region *oobregion)
90{
91 if (section)
92 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Boris Brezillonbf01e06b2016-02-03 20:03:30 +010094 oobregion->offset = 0;
95 oobregion->length = 3;
96
97 return 0;
98}
99
100static int s3c2410_ooblayout_free(struct mtd_info *mtd, int section,
101 struct mtd_oob_region *oobregion)
102{
103 if (section)
104 return -ERANGE;
105
106 oobregion->offset = 8;
107 oobregion->length = 8;
108
109 return 0;
110}
111
112static const struct mtd_ooblayout_ops s3c2410_ooblayout_ops = {
113 .ecc = s3c2410_ooblayout_ecc,
114 .free = s3c2410_ooblayout_free,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115};
116
117/* controller and mtd information */
118
119struct s3c2410_nand_info;
120
Ben Dooks3db72152009-05-30 17:18:15 +0100121/**
122 * struct s3c2410_nand_mtd - driver MTD structure
123 * @mtd: The MTD instance to pass to the MTD layer.
124 * @chip: The NAND chip information.
125 * @set: The platform information supplied for this set of NAND chips.
126 * @info: Link back to the hardware information.
Ben Dooks3db72152009-05-30 17:18:15 +0100127*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128struct s3c2410_nand_mtd {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 struct nand_chip chip;
130 struct s3c2410_nand_set *set;
131 struct s3c2410_nand_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132};
133
Ben Dooks2c06a082006-06-27 14:35:46 +0100134enum s3c_cpu_type {
135 TYPE_S3C2410,
136 TYPE_S3C2412,
137 TYPE_S3C2440,
138};
139
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200140enum s3c_nand_clk_state {
141 CLOCK_DISABLE = 0,
142 CLOCK_ENABLE,
143 CLOCK_SUSPEND,
144};
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146/* overview of the s3c2410 nand state */
147
Ben Dooks3db72152009-05-30 17:18:15 +0100148/**
149 * struct s3c2410_nand_info - NAND controller state.
150 * @mtds: An array of MTD instances on this controoler.
151 * @platform: The platform data for this board.
152 * @device: The platform device we bound to.
Ben Dooks3db72152009-05-30 17:18:15 +0100153 * @clk: The clock resource for this controller.
Sachin Kamat6f32a3e2012-08-21 14:24:09 +0530154 * @regs: The area mapped for the hardware registers.
Ben Dooks3db72152009-05-30 17:18:15 +0100155 * @sel_reg: Pointer to the register controlling the NAND selection.
156 * @sel_bit: The bit in @sel_reg to select the NAND chip.
157 * @mtd_count: The number of MTDs created from this controller.
158 * @save_sel: The contents of @sel_reg to be saved over suspend.
159 * @clk_rate: The clock rate from @clk.
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200160 * @clk_state: The current clock state.
Ben Dooks3db72152009-05-30 17:18:15 +0100161 * @cpu_type: The exact type of this controller.
162 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163struct s3c2410_nand_info {
164 /* mtd info */
Miquel Raynal7da45132018-07-17 09:08:02 +0200165 struct nand_controller controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 struct s3c2410_nand_mtd *mtds;
167 struct s3c2410_platform_nand *platform;
168
169 /* device info */
170 struct device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 struct clk *clk;
Ben Dooksfdf2fd52005-02-18 14:46:15 +0000172 void __iomem *regs;
Ben Dooks2c06a082006-06-27 14:35:46 +0100173 void __iomem *sel_reg;
174 int sel_bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 int mtd_count;
Ben Dooks09160832008-04-15 11:36:18 +0100176 unsigned long save_sel;
Ben Dooks30821fe2008-07-15 11:58:31 +0100177 unsigned long clk_rate;
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200178 enum s3c_nand_clk_state clk_state;
Ben Dooks03680b12007-11-19 23:28:07 +0000179
Ben Dooks2c06a082006-06-27 14:35:46 +0100180 enum s3c_cpu_type cpu_type;
Ben Dooks30821fe2008-07-15 11:58:31 +0100181
Krzysztof Kozlowskid9ca77f2016-06-27 14:51:38 +0200182#ifdef CONFIG_ARM_S3C24XX_CPUFREQ
Ben Dooks30821fe2008-07-15 11:58:31 +0100183 struct notifier_block freq_transition;
184#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185};
186
Sergio Prado1c825ad2016-10-26 21:59:55 -0200187struct s3c24XX_nand_devtype_data {
188 enum s3c_cpu_type type;
189};
190
191static const struct s3c24XX_nand_devtype_data s3c2410_nand_devtype_data = {
192 .type = TYPE_S3C2410,
193};
194
195static const struct s3c24XX_nand_devtype_data s3c2412_nand_devtype_data = {
196 .type = TYPE_S3C2412,
197};
198
199static const struct s3c24XX_nand_devtype_data s3c2440_nand_devtype_data = {
200 .type = TYPE_S3C2440,
201};
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203/* conversion functions */
204
205static struct s3c2410_nand_mtd *s3c2410_nand_mtd_toours(struct mtd_info *mtd)
206{
Boris BREZILLON7208b992015-12-10 09:00:22 +0100207 return container_of(mtd_to_nand(mtd), struct s3c2410_nand_mtd,
208 chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
211static struct s3c2410_nand_info *s3c2410_nand_mtd_toinfo(struct mtd_info *mtd)
212{
213 return s3c2410_nand_mtd_toours(mtd)->info;
214}
215
Russell King3ae5eae2005-11-09 22:32:44 +0000216static struct s3c2410_nand_info *to_nand_info(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
Russell King3ae5eae2005-11-09 22:32:44 +0000218 return platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
Russell King3ae5eae2005-11-09 22:32:44 +0000221static struct s3c2410_platform_nand *to_nand_plat(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
Jingoo Han453810b2013-07-30 17:18:33 +0900223 return dev_get_platdata(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200226static inline int allow_clk_suspend(struct s3c2410_nand_info *info)
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100227{
Sachin Kamata68c5ec2012-07-16 16:02:25 +0530228#ifdef CONFIG_MTD_NAND_S3C2410_CLKSTOP
229 return 1;
230#else
231 return 0;
232#endif
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100233}
234
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200235/**
236 * s3c2410_nand_clk_set_state - Enable, disable or suspend NAND clock.
237 * @info: The controller instance.
238 * @new_state: State to which clock should be set.
239 */
240static void s3c2410_nand_clk_set_state(struct s3c2410_nand_info *info,
241 enum s3c_nand_clk_state new_state)
242{
243 if (!allow_clk_suspend(info) && new_state == CLOCK_SUSPEND)
244 return;
245
246 if (info->clk_state == CLOCK_ENABLE) {
247 if (new_state != CLOCK_ENABLE)
Vasily Khoruzhick887957b2014-06-30 22:12:16 +0300248 clk_disable_unprepare(info->clk);
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200249 } else {
250 if (new_state == CLOCK_ENABLE)
Vasily Khoruzhick887957b2014-06-30 22:12:16 +0300251 clk_prepare_enable(info->clk);
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200252 }
253
254 info->clk_state = new_state;
255}
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257/* timing calculations */
258
Ben Dookscfd320f2005-10-20 22:22:58 +0100259#define NS_IN_KHZ 1000000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Ben Dooks3db72152009-05-30 17:18:15 +0100261/**
262 * s3c_nand_calc_rate - calculate timing data.
263 * @wanted: The cycle time in nanoseconds.
264 * @clk: The clock rate in kHz.
265 * @max: The maximum divider value.
266 *
267 * Calculate the timing value from the given parameters.
268 */
Ben Dooks2c06a082006-06-27 14:35:46 +0100269static int s3c_nand_calc_rate(int wanted, unsigned long clk, int max)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271 int result;
272
Ben Dooks947391c2009-05-30 18:34:16 +0100273 result = DIV_ROUND_UP((wanted * clk), NS_IN_KHZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 pr_debug("result %d from %ld, %d\n", result, clk, wanted);
276
277 if (result > max) {
Sachin Kamat92aeb5d2012-07-16 16:02:23 +0530278 pr_err("%d ns is too big for current clock rate %ld\n",
279 wanted, clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 return -1;
281 }
282
283 if (result < 1)
284 result = 1;
285
286 return result;
287}
288
Sachin Kamat54cd0202012-07-16 16:02:26 +0530289#define to_ns(ticks, clk) (((ticks) * NS_IN_KHZ) / (unsigned int)(clk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291/* controller setup */
292
Ben Dooks3db72152009-05-30 17:18:15 +0100293/**
294 * s3c2410_nand_setrate - setup controller timing information.
295 * @info: The controller instance.
296 *
297 * Given the information supplied by the platform, calculate and set
298 * the necessary timing registers in the hardware to generate the
299 * necessary timing cycles to the hardware.
300 */
Ben Dooks30821fe2008-07-15 11:58:31 +0100301static int s3c2410_nand_setrate(struct s3c2410_nand_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
Ben Dooks30821fe2008-07-15 11:58:31 +0100303 struct s3c2410_platform_nand *plat = info->platform;
Ben Dooks2c06a082006-06-27 14:35:46 +0100304 int tacls_max = (info->cpu_type == TYPE_S3C2412) ? 8 : 4;
Ben Dookscfd320f2005-10-20 22:22:58 +0100305 int tacls, twrph0, twrph1;
Ben Dooks30821fe2008-07-15 11:58:31 +0100306 unsigned long clkrate = clk_get_rate(info->clk);
Nelson Castillo2612e522009-05-10 15:41:54 -0500307 unsigned long uninitialized_var(set), cfg, uninitialized_var(mask);
Ben Dooks30821fe2008-07-15 11:58:31 +0100308 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 /* calculate the timing information for the controller */
311
Ben Dooks30821fe2008-07-15 11:58:31 +0100312 info->clk_rate = clkrate;
Ben Dookscfd320f2005-10-20 22:22:58 +0100313 clkrate /= 1000; /* turn clock into kHz for ease of use */
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 if (plat != NULL) {
Ben Dooks2c06a082006-06-27 14:35:46 +0100316 tacls = s3c_nand_calc_rate(plat->tacls, clkrate, tacls_max);
317 twrph0 = s3c_nand_calc_rate(plat->twrph0, clkrate, 8);
318 twrph1 = s3c_nand_calc_rate(plat->twrph1, clkrate, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 } else {
320 /* default timings */
Ben Dooks2c06a082006-06-27 14:35:46 +0100321 tacls = tacls_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 twrph0 = 8;
323 twrph1 = 8;
324 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (tacls < 0 || twrph0 < 0 || twrph1 < 0) {
Ben Dooks99974c62006-06-21 15:43:05 +0100327 dev_err(info->device, "cannot get suitable timings\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 return -EINVAL;
329 }
330
Ben Dooks99974c62006-06-21 15:43:05 +0100331 dev_info(info->device, "Tacls=%d, %dns Twrph0=%d %dns, Twrph1=%d %dns\n",
Sachin Kamat54cd0202012-07-16 16:02:26 +0530332 tacls, to_ns(tacls, clkrate), twrph0, to_ns(twrph0, clkrate),
333 twrph1, to_ns(twrph1, clkrate));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Ben Dooks30821fe2008-07-15 11:58:31 +0100335 switch (info->cpu_type) {
336 case TYPE_S3C2410:
337 mask = (S3C2410_NFCONF_TACLS(3) |
338 S3C2410_NFCONF_TWRPH0(7) |
339 S3C2410_NFCONF_TWRPH1(7));
340 set = S3C2410_NFCONF_EN;
341 set |= S3C2410_NFCONF_TACLS(tacls - 1);
342 set |= S3C2410_NFCONF_TWRPH0(twrph0 - 1);
343 set |= S3C2410_NFCONF_TWRPH1(twrph1 - 1);
344 break;
345
346 case TYPE_S3C2440:
347 case TYPE_S3C2412:
Peter Korsgaarda755a382009-06-03 13:46:54 +0200348 mask = (S3C2440_NFCONF_TACLS(tacls_max - 1) |
349 S3C2440_NFCONF_TWRPH0(7) |
350 S3C2440_NFCONF_TWRPH1(7));
Ben Dooks30821fe2008-07-15 11:58:31 +0100351
352 set = S3C2440_NFCONF_TACLS(tacls - 1);
353 set |= S3C2440_NFCONF_TWRPH0(twrph0 - 1);
354 set |= S3C2440_NFCONF_TWRPH1(twrph1 - 1);
355 break;
356
357 default:
Ben Dooks30821fe2008-07-15 11:58:31 +0100358 BUG();
359 }
360
Ben Dooks30821fe2008-07-15 11:58:31 +0100361 local_irq_save(flags);
362
363 cfg = readl(info->regs + S3C2410_NFCONF);
364 cfg &= ~mask;
365 cfg |= set;
366 writel(cfg, info->regs + S3C2410_NFCONF);
367
368 local_irq_restore(flags);
369
Andy Greenae7304e2009-05-10 15:42:02 -0500370 dev_dbg(info->device, "NF_CONF is 0x%lx\n", cfg);
371
Ben Dooks30821fe2008-07-15 11:58:31 +0100372 return 0;
373}
374
Ben Dooks3db72152009-05-30 17:18:15 +0100375/**
376 * s3c2410_nand_inithw - basic hardware initialisation
377 * @info: The hardware state.
378 *
379 * Do the basic initialisation of the hardware, using s3c2410_nand_setrate()
380 * to setup the hardware access speeds and set the controller to be enabled.
381*/
Ben Dooks30821fe2008-07-15 11:58:31 +0100382static int s3c2410_nand_inithw(struct s3c2410_nand_info *info)
383{
384 int ret;
385
386 ret = s3c2410_nand_setrate(info);
387 if (ret < 0)
388 return ret;
389
Sachin Kamat54cd0202012-07-16 16:02:26 +0530390 switch (info->cpu_type) {
391 case TYPE_S3C2410:
Ben Dooks30821fe2008-07-15 11:58:31 +0100392 default:
Ben Dooks2c06a082006-06-27 14:35:46 +0100393 break;
394
Sachin Kamat54cd0202012-07-16 16:02:26 +0530395 case TYPE_S3C2440:
396 case TYPE_S3C2412:
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100397 /* enable the controller and de-assert nFCE */
398
Ben Dooks2c06a082006-06-27 14:35:46 +0100399 writel(S3C2440_NFCONT_ENABLE, info->regs + S3C2440_NFCONT);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return 0;
403}
404
Ben Dooks3db72152009-05-30 17:18:15 +0100405/**
406 * s3c2410_nand_select_chip - select the given nand chip
Boris Brezillon758b56f2018-09-06 14:05:24 +0200407 * @this: NAND chip object.
Ben Dooks3db72152009-05-30 17:18:15 +0100408 * @chip: The chip number.
409 *
410 * This is called by the MTD layer to either select a given chip for the
411 * @mtd instance, or to indicate that the access has finished and the
412 * chip can be de-selected.
413 *
414 * The routine ensures that the nFCE line is correctly setup, and any
415 * platform specific selection code is called to route nFCE to the specific
416 * chip.
417 */
Boris Brezillon758b56f2018-09-06 14:05:24 +0200418static void s3c2410_nand_select_chip(struct nand_chip *this, int chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
420 struct s3c2410_nand_info *info;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000421 struct s3c2410_nand_mtd *nmtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 unsigned long cur;
423
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100424 nmtd = nand_get_controller_data(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 info = nmtd->info;
426
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200427 if (chip != -1)
428 s3c2410_nand_clk_set_state(info, CLOCK_ENABLE);
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100429
Ben Dooks2c06a082006-06-27 14:35:46 +0100430 cur = readl(info->sel_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 if (chip == -1) {
Ben Dooks2c06a082006-06-27 14:35:46 +0100433 cur |= info->sel_bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 } else {
Ben Dooksfb8d82a2005-07-06 21:05:10 +0100435 if (nmtd->set != NULL && chip > nmtd->set->nr_chips) {
Ben Dooks99974c62006-06-21 15:43:05 +0100436 dev_err(info->device, "invalid chip %d\n", chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return;
438 }
439
440 if (info->platform != NULL) {
441 if (info->platform->select_chip != NULL)
David Woodhousee0c7d762006-05-13 18:07:53 +0100442 (info->platform->select_chip) (nmtd->set, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
444
Ben Dooks2c06a082006-06-27 14:35:46 +0100445 cur &= ~info->sel_bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 }
447
Ben Dooks2c06a082006-06-27 14:35:46 +0100448 writel(cur, info->sel_reg);
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100449
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200450 if (chip == -1)
451 s3c2410_nand_clk_set_state(info, CLOCK_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
Ben Dooksad3b5fb2006-06-19 09:43:23 +0100454/* s3c2410_nand_hwcontrol
Ben Dooksa4f957f2005-06-20 12:48:25 +0100455 *
Ben Dooksad3b5fb2006-06-19 09:43:23 +0100456 * Issue command and address cycles to the chip
Ben Dooksa4f957f2005-06-20 12:48:25 +0100457*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Boris Brezillon0f808c12018-09-06 14:05:26 +0200459static void s3c2410_nand_hwcontrol(struct nand_chip *chip, int cmd,
David Woodhousef9068872006-06-10 00:53:16 +0100460 unsigned int ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Boris Brezillon0f808c12018-09-06 14:05:26 +0200462 struct mtd_info *mtd = nand_to_mtd(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
David Woodhousec9ac5972006-11-30 08:17:38 +0000464
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200465 if (cmd == NAND_CMD_NONE)
466 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
David Woodhousef9068872006-06-10 00:53:16 +0100468 if (ctrl & NAND_CLE)
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200469 writeb(cmd, info->regs + S3C2410_NFCMD);
470 else
471 writeb(cmd, info->regs + S3C2410_NFADDR);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100472}
473
474/* command and control functions */
475
Boris Brezillon0f808c12018-09-06 14:05:26 +0200476static void s3c2440_nand_hwcontrol(struct nand_chip *chip, int cmd,
David Woodhousef9068872006-06-10 00:53:16 +0100477 unsigned int ctrl)
Ben Dooksa4f957f2005-06-20 12:48:25 +0100478{
Boris Brezillon0f808c12018-09-06 14:05:26 +0200479 struct mtd_info *mtd = nand_to_mtd(chip);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100480 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100481
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200482 if (cmd == NAND_CMD_NONE)
483 return;
Ben Dooksa4f957f2005-06-20 12:48:25 +0100484
David Woodhousef9068872006-06-10 00:53:16 +0100485 if (ctrl & NAND_CLE)
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200486 writeb(cmd, info->regs + S3C2440_NFCMD);
487 else
488 writeb(cmd, info->regs + S3C2440_NFADDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491/* s3c2410_nand_devready()
492 *
493 * returns 0 if the nand is busy, 1 if it is ready
494*/
495
496static int s3c2410_nand_devready(struct mtd_info *mtd)
497{
498 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return readb(info->regs + S3C2410_NFSTAT) & S3C2410_NFSTAT_BUSY;
500}
501
Ben Dooks2c06a082006-06-27 14:35:46 +0100502static int s3c2440_nand_devready(struct mtd_info *mtd)
503{
504 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
505 return readb(info->regs + S3C2440_NFSTAT) & S3C2440_NFSTAT_READY;
506}
507
508static int s3c2412_nand_devready(struct mtd_info *mtd)
509{
510 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
511 return readb(info->regs + S3C2412_NFSTAT) & S3C2412_NFSTAT_READY;
512}
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514/* ECC handling functions */
515
Boris Brezillon00da2ea2018-09-06 14:05:19 +0200516static int s3c2410_nand_correct_data(struct nand_chip *chip, u_char *dat,
Ben Dooks2c06a082006-06-27 14:35:46 +0100517 u_char *read_ecc, u_char *calc_ecc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Boris Brezillon00da2ea2018-09-06 14:05:19 +0200519 struct mtd_info *mtd = nand_to_mtd(chip);
Ben Dooksa2593242007-02-02 16:59:33 +0000520 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
521 unsigned int diff0, diff1, diff2;
522 unsigned int bit, byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Ben Dooksa2593242007-02-02 16:59:33 +0000524 pr_debug("%s(%p,%p,%p,%p)\n", __func__, mtd, dat, read_ecc, calc_ecc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Ben Dooksa2593242007-02-02 16:59:33 +0000526 diff0 = read_ecc[0] ^ calc_ecc[0];
527 diff1 = read_ecc[1] ^ calc_ecc[1];
528 diff2 = read_ecc[2] ^ calc_ecc[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Andy Shevchenko13e85972012-08-02 16:06:47 +0300530 pr_debug("%s: rd %*phN calc %*phN diff %02x%02x%02x\n",
531 __func__, 3, read_ecc, 3, calc_ecc,
Ben Dooksa2593242007-02-02 16:59:33 +0000532 diff0, diff1, diff2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Ben Dooksa2593242007-02-02 16:59:33 +0000534 if (diff0 == 0 && diff1 == 0 && diff2 == 0)
535 return 0; /* ECC is ok */
536
Ben Dooksc45c6c62008-04-15 11:36:20 +0100537 /* sometimes people do not think about using the ECC, so check
538 * to see if we have an 0xff,0xff,0xff read ECC and then ignore
539 * the error, on the assumption that this is an un-eccd page.
540 */
541 if (read_ecc[0] == 0xff && read_ecc[1] == 0xff && read_ecc[2] == 0xff
542 && info->platform->ignore_unset_ecc)
543 return 0;
544
Ben Dooksa2593242007-02-02 16:59:33 +0000545 /* Can we correct this ECC (ie, one row and column change).
546 * Note, this is similar to the 256 error code on smartmedia */
547
548 if (((diff0 ^ (diff0 >> 1)) & 0x55) == 0x55 &&
549 ((diff1 ^ (diff1 >> 1)) & 0x55) == 0x55 &&
550 ((diff2 ^ (diff2 >> 1)) & 0x55) == 0x55) {
551 /* calculate the bit position of the error */
552
Matt Reimerd0bf3792007-10-18 18:02:43 -0700553 bit = ((diff2 >> 3) & 1) |
554 ((diff2 >> 4) & 2) |
555 ((diff2 >> 5) & 4);
Ben Dooksa2593242007-02-02 16:59:33 +0000556
557 /* calculate the byte position of the error */
558
Matt Reimerd0bf3792007-10-18 18:02:43 -0700559 byte = ((diff2 << 7) & 0x100) |
560 ((diff1 << 0) & 0x80) |
561 ((diff1 << 1) & 0x40) |
562 ((diff1 << 2) & 0x20) |
563 ((diff1 << 3) & 0x10) |
564 ((diff0 >> 4) & 0x08) |
565 ((diff0 >> 3) & 0x04) |
566 ((diff0 >> 2) & 0x02) |
567 ((diff0 >> 1) & 0x01);
Ben Dooksa2593242007-02-02 16:59:33 +0000568
569 dev_dbg(info->device, "correcting error bit %d, byte %d\n",
570 bit, byte);
571
572 dat[byte] ^= (1 << bit);
573 return 1;
574 }
575
576 /* if there is only one bit difference in the ECC, then
577 * one of only a row or column parity has changed, which
578 * means the error is most probably in the ECC itself */
579
580 diff0 |= (diff1 << 8);
581 diff0 |= (diff2 << 16);
582
Zhaoxiu Zeng03a97552016-04-12 15:30:35 +0800583 /* equal to "(diff0 & ~(1 << __ffs(diff0)))" */
584 if ((diff0 & (diff0 - 1)) == 0)
Ben Dooksa2593242007-02-02 16:59:33 +0000585 return 1;
586
Matt Reimer4fac9f62007-10-18 18:02:44 -0700587 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589
Ben Dooksa4f957f2005-06-20 12:48:25 +0100590/* ECC functions
591 *
592 * These allow the s3c2410 and s3c2440 to use the controller's ECC
593 * generator block to ECC the data as it passes through]
594*/
595
Boris Brezillonec476362018-09-06 14:05:17 +0200596static void s3c2410_nand_enable_hwecc(struct nand_chip *chip, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Boris Brezillonec476362018-09-06 14:05:17 +0200598 struct s3c2410_nand_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 unsigned long ctrl;
600
Boris Brezillonec476362018-09-06 14:05:17 +0200601 info = s3c2410_nand_mtd_toinfo(nand_to_mtd(chip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 ctrl = readl(info->regs + S3C2410_NFCONF);
603 ctrl |= S3C2410_NFCONF_INITECC;
604 writel(ctrl, info->regs + S3C2410_NFCONF);
605}
606
Boris Brezillonec476362018-09-06 14:05:17 +0200607static void s3c2412_nand_enable_hwecc(struct nand_chip *chip, int mode)
Matthieu CASTET4f659922007-02-13 12:30:38 +0100608{
Boris Brezillonec476362018-09-06 14:05:17 +0200609 struct s3c2410_nand_info *info;
Matthieu CASTET4f659922007-02-13 12:30:38 +0100610 unsigned long ctrl;
611
Boris Brezillonec476362018-09-06 14:05:17 +0200612 info = s3c2410_nand_mtd_toinfo(nand_to_mtd(chip));
Matthieu CASTET4f659922007-02-13 12:30:38 +0100613 ctrl = readl(info->regs + S3C2440_NFCONT);
Sachin Kamatf938bc52012-08-21 10:21:15 +0530614 writel(ctrl | S3C2412_NFCONT_INIT_MAIN_ECC,
615 info->regs + S3C2440_NFCONT);
Matthieu CASTET4f659922007-02-13 12:30:38 +0100616}
617
Boris Brezillonec476362018-09-06 14:05:17 +0200618static void s3c2440_nand_enable_hwecc(struct nand_chip *chip, int mode)
Ben Dooksa4f957f2005-06-20 12:48:25 +0100619{
Boris Brezillonec476362018-09-06 14:05:17 +0200620 struct s3c2410_nand_info *info;
Ben Dooksa4f957f2005-06-20 12:48:25 +0100621 unsigned long ctrl;
622
Boris Brezillonec476362018-09-06 14:05:17 +0200623 info = s3c2410_nand_mtd_toinfo(nand_to_mtd(chip));
Ben Dooksa4f957f2005-06-20 12:48:25 +0100624 ctrl = readl(info->regs + S3C2440_NFCONT);
625 writel(ctrl | S3C2440_NFCONT_INITECC, info->regs + S3C2440_NFCONT);
626}
627
Boris Brezillonaf37d2c2018-09-06 14:05:18 +0200628static int s3c2410_nand_calculate_ecc(struct nand_chip *chip,
629 const u_char *dat, u_char *ecc_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Boris Brezillonaf37d2c2018-09-06 14:05:18 +0200631 struct mtd_info *mtd = nand_to_mtd(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
633
634 ecc_code[0] = readb(info->regs + S3C2410_NFECC + 0);
635 ecc_code[1] = readb(info->regs + S3C2410_NFECC + 1);
636 ecc_code[2] = readb(info->regs + S3C2410_NFECC + 2);
637
Andy Shevchenko13e85972012-08-02 16:06:47 +0300638 pr_debug("%s: returning ecc %*phN\n", __func__, 3, ecc_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 return 0;
641}
642
Boris Brezillonaf37d2c2018-09-06 14:05:18 +0200643static int s3c2412_nand_calculate_ecc(struct nand_chip *chip,
644 const u_char *dat, u_char *ecc_code)
Matthieu CASTET4f659922007-02-13 12:30:38 +0100645{
Boris Brezillonaf37d2c2018-09-06 14:05:18 +0200646 struct mtd_info *mtd = nand_to_mtd(chip);
Matthieu CASTET4f659922007-02-13 12:30:38 +0100647 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
648 unsigned long ecc = readl(info->regs + S3C2412_NFMECC0);
649
650 ecc_code[0] = ecc;
651 ecc_code[1] = ecc >> 8;
652 ecc_code[2] = ecc >> 16;
653
Andy Shevchenko13e85972012-08-02 16:06:47 +0300654 pr_debug("%s: returning ecc %*phN\n", __func__, 3, ecc_code);
Matthieu CASTET4f659922007-02-13 12:30:38 +0100655
656 return 0;
657}
658
Boris Brezillonaf37d2c2018-09-06 14:05:18 +0200659static int s3c2440_nand_calculate_ecc(struct nand_chip *chip,
660 const u_char *dat, u_char *ecc_code)
Ben Dooksa4f957f2005-06-20 12:48:25 +0100661{
Boris Brezillonaf37d2c2018-09-06 14:05:18 +0200662 struct mtd_info *mtd = nand_to_mtd(chip);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100663 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
664 unsigned long ecc = readl(info->regs + S3C2440_NFMECC0);
665
666 ecc_code[0] = ecc;
667 ecc_code[1] = ecc >> 8;
668 ecc_code[2] = ecc >> 16;
669
Ben Dooks71d54f32008-04-15 11:36:19 +0100670 pr_debug("%s: returning ecc %06lx\n", __func__, ecc & 0xffffff);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100671
672 return 0;
673}
674
Ben Dooksa4f957f2005-06-20 12:48:25 +0100675/* over-ride the standard functions for a little more speed. We can
676 * use read/write block to move the data buffers to/from the controller
677*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Boris Brezillon7e534322018-09-06 14:05:22 +0200679static void s3c2410_nand_read_buf(struct nand_chip *this, u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 readsb(this->IO_ADDR_R, buf, len);
682}
683
Boris Brezillon7e534322018-09-06 14:05:22 +0200684static void s3c2440_nand_read_buf(struct nand_chip *this, u_char *buf, int len)
Matt Reimerb773bb22007-10-18 17:43:07 -0700685{
Boris Brezillon7e534322018-09-06 14:05:22 +0200686 struct mtd_info *mtd = nand_to_mtd(this);
Matt Reimerb773bb22007-10-18 17:43:07 -0700687 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
Ben Dooksdea2aa62009-05-30 18:30:18 +0100688
689 readsl(info->regs + S3C2440_NFDATA, buf, len >> 2);
690
691 /* cleanup if we've got less than a word to do */
692 if (len & 3) {
693 buf += len & ~3;
694
695 for (; len & 3; len--)
696 *buf++ = readb(info->regs + S3C2440_NFDATA);
697 }
Matt Reimerb773bb22007-10-18 17:43:07 -0700698}
699
Boris Brezillonc0739d82018-09-06 14:05:23 +0200700static void s3c2410_nand_write_buf(struct nand_chip *this, const u_char *buf,
Sachin Kamatf938bc52012-08-21 10:21:15 +0530701 int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 writesb(this->IO_ADDR_W, buf, len);
704}
705
Boris Brezillonc0739d82018-09-06 14:05:23 +0200706static void s3c2440_nand_write_buf(struct nand_chip *this, const u_char *buf,
Sachin Kamatf938bc52012-08-21 10:21:15 +0530707 int len)
Matt Reimerb773bb22007-10-18 17:43:07 -0700708{
Boris Brezillonc0739d82018-09-06 14:05:23 +0200709 struct mtd_info *mtd = nand_to_mtd(this);
Matt Reimerb773bb22007-10-18 17:43:07 -0700710 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
Ben Dooksdea2aa62009-05-30 18:30:18 +0100711
712 writesl(info->regs + S3C2440_NFDATA, buf, len >> 2);
713
714 /* cleanup any fractional write */
715 if (len & 3) {
716 buf += len & ~3;
717
718 for (; len & 3; len--, buf++)
719 writeb(*buf, info->regs + S3C2440_NFDATA);
720 }
Matt Reimerb773bb22007-10-18 17:43:07 -0700721}
722
Ben Dooks30821fe2008-07-15 11:58:31 +0100723/* cpufreq driver support */
724
Krzysztof Kozlowskid9ca77f2016-06-27 14:51:38 +0200725#ifdef CONFIG_ARM_S3C24XX_CPUFREQ
Ben Dooks30821fe2008-07-15 11:58:31 +0100726
727static int s3c2410_nand_cpufreq_transition(struct notifier_block *nb,
728 unsigned long val, void *data)
729{
730 struct s3c2410_nand_info *info;
731 unsigned long newclk;
732
733 info = container_of(nb, struct s3c2410_nand_info, freq_transition);
734 newclk = clk_get_rate(info->clk);
735
736 if ((val == CPUFREQ_POSTCHANGE && newclk < info->clk_rate) ||
737 (val == CPUFREQ_PRECHANGE && newclk > info->clk_rate)) {
738 s3c2410_nand_setrate(info);
739 }
740
741 return 0;
742}
743
744static inline int s3c2410_nand_cpufreq_register(struct s3c2410_nand_info *info)
745{
746 info->freq_transition.notifier_call = s3c2410_nand_cpufreq_transition;
747
748 return cpufreq_register_notifier(&info->freq_transition,
749 CPUFREQ_TRANSITION_NOTIFIER);
750}
751
Sachin Kamatf938bc52012-08-21 10:21:15 +0530752static inline void
753s3c2410_nand_cpufreq_deregister(struct s3c2410_nand_info *info)
Ben Dooks30821fe2008-07-15 11:58:31 +0100754{
755 cpufreq_unregister_notifier(&info->freq_transition,
756 CPUFREQ_TRANSITION_NOTIFIER);
757}
758
759#else
760static inline int s3c2410_nand_cpufreq_register(struct s3c2410_nand_info *info)
761{
762 return 0;
763}
764
Sachin Kamatf938bc52012-08-21 10:21:15 +0530765static inline void
766s3c2410_nand_cpufreq_deregister(struct s3c2410_nand_info *info)
Ben Dooks30821fe2008-07-15 11:58:31 +0100767{
768}
769#endif
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771/* device management functions */
772
Ben Dooksec0482e2009-05-30 16:55:29 +0100773static int s3c24xx_nand_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
Russell King3ae5eae2005-11-09 22:32:44 +0000775 struct s3c2410_nand_info *info = to_nand_info(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000777 if (info == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 return 0;
779
Ben Dooks30821fe2008-07-15 11:58:31 +0100780 s3c2410_nand_cpufreq_deregister(info);
781
782 /* Release all our mtds and their partitions, then go through
783 * freeing the resources used
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 if (info->mtds != NULL) {
787 struct s3c2410_nand_mtd *ptr = info->mtds;
788 int mtdno;
789
790 for (mtdno = 0; mtdno < info->mtd_count; mtdno++, ptr++) {
791 pr_debug("releasing mtd %d (%p)\n", mtdno, ptr);
Boris Brezillon59ac2762018-09-06 14:05:15 +0200792 nand_release(&ptr->chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 }
795
796 /* free the common resources */
797
Sachin Kamat6f32a3e2012-08-21 14:24:09 +0530798 if (!IS_ERR(info->clk))
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200799 s3c2410_nand_clk_set_state(info, CLOCK_DISABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801 return 0;
802}
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804static int s3c2410_nand_add_partition(struct s3c2410_nand_info *info,
805 struct s3c2410_nand_mtd *mtd,
806 struct s3c2410_nand_set *set)
807{
Sachin Kamatded4c552012-11-16 16:08:22 +0530808 if (set) {
Boris BREZILLON7208b992015-12-10 09:00:22 +0100809 struct mtd_info *mtdinfo = nand_to_mtd(&mtd->chip);
Andy Greened27f022009-05-10 15:42:09 -0500810
Boris BREZILLON7208b992015-12-10 09:00:22 +0100811 mtdinfo->name = set->name;
812
Rafał Miłecki29597ca2018-07-13 11:27:31 +0200813 return mtd_device_register(mtdinfo, set->partitions,
814 set->nr_partitions);
Sachin Kamatded4c552012-11-16 16:08:22 +0530815 }
816
817 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Boris Brezillon104e4422017-03-16 09:35:58 +0100820static int s3c2410_nand_setup_data_interface(struct mtd_info *mtd, int csline,
821 const struct nand_data_interface *conf)
Sergio Prado1c825ad2016-10-26 21:59:55 -0200822{
823 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
824 struct s3c2410_platform_nand *pdata = info->platform;
825 const struct nand_sdr_timings *timings;
826 int tacls;
827
828 timings = nand_get_sdr_timings(conf);
829 if (IS_ERR(timings))
830 return -ENOTSUPP;
831
832 tacls = timings->tCLS_min - timings->tWP_min;
833 if (tacls < 0)
834 tacls = 0;
835
836 pdata->tacls = DIV_ROUND_UP(tacls, 1000);
837 pdata->twrph0 = DIV_ROUND_UP(timings->tWP_min, 1000);
838 pdata->twrph1 = DIV_ROUND_UP(timings->tCLH_min, 1000);
839
840 return s3c2410_nand_setrate(info);
841}
842
Ben Dooks3db72152009-05-30 17:18:15 +0100843/**
844 * s3c2410_nand_init_chip - initialise a single instance of an chip
845 * @info: The base NAND controller the chip is on.
846 * @nmtd: The new controller MTD instance to fill in.
847 * @set: The information passed from the board specific platform data.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 *
Ben Dooks3db72152009-05-30 17:18:15 +0100849 * Initialise the given @nmtd from the information in @info and @set. This
850 * readies the structure for use with the MTD layer functions by ensuring
851 * all pointers are setup and the necessary control routines selected.
852 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853static void s3c2410_nand_init_chip(struct s3c2410_nand_info *info,
854 struct s3c2410_nand_mtd *nmtd,
855 struct s3c2410_nand_set *set)
856{
Sergio Prado1c825ad2016-10-26 21:59:55 -0200857 struct device_node *np = info->device->of_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 struct nand_chip *chip = &nmtd->chip;
Ben Dooks2c06a082006-06-27 14:35:46 +0100859 void __iomem *regs = info->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Sergio Prado1c825ad2016-10-26 21:59:55 -0200861 nand_set_flash_node(chip, set->of_node);
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 chip->write_buf = s3c2410_nand_write_buf;
864 chip->read_buf = s3c2410_nand_read_buf;
865 chip->select_chip = s3c2410_nand_select_chip;
866 chip->chip_delay = 50;
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100867 nand_set_controller_data(chip, nmtd);
Ben Dooks74218fe2009-11-02 18:12:51 +0000868 chip->options = set->options;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 chip->controller = &info->controller;
870
Sergio Prado1c825ad2016-10-26 21:59:55 -0200871 /*
872 * let's keep behavior unchanged for legacy boards booting via pdata and
873 * auto-detect timings only when booting with a device tree.
874 */
875 if (np)
876 chip->setup_data_interface = s3c2410_nand_setup_data_interface;
877
Ben Dooks2c06a082006-06-27 14:35:46 +0100878 switch (info->cpu_type) {
879 case TYPE_S3C2410:
880 chip->IO_ADDR_W = regs + S3C2410_NFDATA;
881 info->sel_reg = regs + S3C2410_NFCONF;
882 info->sel_bit = S3C2410_NFCONF_nFCE;
883 chip->cmd_ctrl = s3c2410_nand_hwcontrol;
884 chip->dev_ready = s3c2410_nand_devready;
885 break;
886
887 case TYPE_S3C2440:
888 chip->IO_ADDR_W = regs + S3C2440_NFDATA;
889 info->sel_reg = regs + S3C2440_NFCONT;
890 info->sel_bit = S3C2440_NFCONT_nFCE;
891 chip->cmd_ctrl = s3c2440_nand_hwcontrol;
892 chip->dev_ready = s3c2440_nand_devready;
Matt Reimerb773bb22007-10-18 17:43:07 -0700893 chip->read_buf = s3c2440_nand_read_buf;
894 chip->write_buf = s3c2440_nand_write_buf;
Ben Dooks2c06a082006-06-27 14:35:46 +0100895 break;
896
897 case TYPE_S3C2412:
898 chip->IO_ADDR_W = regs + S3C2440_NFDATA;
899 info->sel_reg = regs + S3C2440_NFCONT;
900 info->sel_bit = S3C2412_NFCONT_nFCE0;
901 chip->cmd_ctrl = s3c2440_nand_hwcontrol;
902 chip->dev_ready = s3c2412_nand_devready;
903
904 if (readl(regs + S3C2410_NFCONF) & S3C2412_NFCONF_NANDBOOT)
905 dev_info(info->device, "System booted from NAND\n");
906
907 break;
Sachin Kamat54cd0202012-07-16 16:02:26 +0530908 }
Ben Dooks2c06a082006-06-27 14:35:46 +0100909
910 chip->IO_ADDR_R = chip->IO_ADDR_W;
Ben Dooksa4f957f2005-06-20 12:48:25 +0100911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 nmtd->info = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 nmtd->set = set;
914
Sergio Pradoe9f66ae2016-10-20 19:42:44 -0200915 chip->ecc.mode = info->platform->ecc_mode;
Michel Pollet9db41f92009-05-13 16:54:14 +0100916
Sergio Prado1c825ad2016-10-26 21:59:55 -0200917 /*
918 * If you use u-boot BBT creation code, specifying this flag will
919 * let the kernel fish out the BBT from the NAND.
920 */
921 if (set->flash_bbt)
Brian Norrisbb9ebd4e2011-05-31 16:31:23 -0700922 chip->bbt_options |= NAND_BBT_USE_FLASH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923}
924
Ben Dooks3db72152009-05-30 17:18:15 +0100925/**
Miquel Raynal12748312018-07-20 17:15:10 +0200926 * s3c2410_nand_attach_chip - Init the ECC engine after NAND scan
927 * @chip: The NAND chip
Ben Dooks71d54f32008-04-15 11:36:19 +0100928 *
Miquel Raynal12748312018-07-20 17:15:10 +0200929 * This hook is called by the core after the identification of the NAND chip,
930 * once the relevant per-chip information is up to date.. This call ensure that
Ben Dooks3db72152009-05-30 17:18:15 +0100931 * we update the internal state accordingly.
932 *
933 * The internal state is currently limited to the ECC state information.
934*/
Miquel Raynal12748312018-07-20 17:15:10 +0200935static int s3c2410_nand_attach_chip(struct nand_chip *chip)
Ben Dooks71d54f32008-04-15 11:36:19 +0100936{
Miquel Raynal12748312018-07-20 17:15:10 +0200937 struct mtd_info *mtd = nand_to_mtd(chip);
938 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
Ben Dooks71d54f32008-04-15 11:36:19 +0100939
Sergio Pradoe9f66ae2016-10-20 19:42:44 -0200940 switch (chip->ecc.mode) {
Ben Dooks71d54f32008-04-15 11:36:19 +0100941
Sergio Pradoe9f66ae2016-10-20 19:42:44 -0200942 case NAND_ECC_NONE:
943 dev_info(info->device, "ECC disabled\n");
944 break;
945
946 case NAND_ECC_SOFT:
947 /*
948 * This driver expects Hamming based ECC when ecc_mode is set
949 * to NAND_ECC_SOFT. Force ecc.algo to NAND_ECC_HAMMING to
950 * avoid adding an extra ecc_algo field to
951 * s3c2410_platform_nand.
952 */
953 chip->ecc.algo = NAND_ECC_HAMMING;
954 dev_info(info->device, "soft ECC\n");
955 break;
956
957 case NAND_ECC_HW:
958 chip->ecc.calculate = s3c2410_nand_calculate_ecc;
959 chip->ecc.correct = s3c2410_nand_correct_data;
960 chip->ecc.strength = 1;
961
962 switch (info->cpu_type) {
963 case TYPE_S3C2410:
964 chip->ecc.hwctl = s3c2410_nand_enable_hwecc;
965 chip->ecc.calculate = s3c2410_nand_calculate_ecc;
966 break;
967
968 case TYPE_S3C2412:
969 chip->ecc.hwctl = s3c2412_nand_enable_hwecc;
970 chip->ecc.calculate = s3c2412_nand_calculate_ecc;
971 break;
972
973 case TYPE_S3C2440:
974 chip->ecc.hwctl = s3c2440_nand_enable_hwecc;
975 chip->ecc.calculate = s3c2440_nand_calculate_ecc;
976 break;
977 }
978
979 dev_dbg(info->device, "chip %p => page shift %d\n",
980 chip, chip->page_shift);
Andy Green8c3e8432009-05-10 15:41:25 -0500981
Adam Buchbinder48fc7f72012-09-19 21:48:00 -0400982 /* change the behaviour depending on whether we are using
Ben Dooks71d54f32008-04-15 11:36:19 +0100983 * the large or small page nand device */
Sergio Pradoe9f66ae2016-10-20 19:42:44 -0200984 if (chip->page_shift > 10) {
985 chip->ecc.size = 256;
986 chip->ecc.bytes = 3;
987 } else {
988 chip->ecc.size = 512;
989 chip->ecc.bytes = 3;
990 mtd_set_ooblayout(nand_to_mtd(chip),
991 &s3c2410_ooblayout_ops);
992 }
Ben Dooks71d54f32008-04-15 11:36:19 +0100993
Sergio Pradoe9f66ae2016-10-20 19:42:44 -0200994 dev_info(info->device, "hardware ECC\n");
995 break;
996
997 default:
998 dev_err(info->device, "invalid ECC mode!\n");
999 return -EINVAL;
Ben Dooks71d54f32008-04-15 11:36:19 +01001000 }
Sergio Pradoe9f66ae2016-10-20 19:42:44 -02001001
Sergio Prado1c825ad2016-10-26 21:59:55 -02001002 if (chip->bbt_options & NAND_BBT_USE_FLASH)
1003 chip->options |= NAND_SKIP_BBTSCAN;
1004
1005 return 0;
1006}
1007
Miquel Raynal12748312018-07-20 17:15:10 +02001008static const struct nand_controller_ops s3c24xx_nand_controller_ops = {
1009 .attach_chip = s3c2410_nand_attach_chip,
1010};
1011
Sergio Prado1c825ad2016-10-26 21:59:55 -02001012static const struct of_device_id s3c24xx_nand_dt_ids[] = {
1013 {
1014 .compatible = "samsung,s3c2410-nand",
1015 .data = &s3c2410_nand_devtype_data,
1016 }, {
1017 /* also compatible with s3c6400 */
1018 .compatible = "samsung,s3c2412-nand",
1019 .data = &s3c2412_nand_devtype_data,
1020 }, {
1021 .compatible = "samsung,s3c2440-nand",
1022 .data = &s3c2440_nand_devtype_data,
1023 },
1024 { /* sentinel */ }
1025};
1026MODULE_DEVICE_TABLE(of, s3c24xx_nand_dt_ids);
1027
1028static int s3c24xx_nand_probe_dt(struct platform_device *pdev)
1029{
1030 const struct s3c24XX_nand_devtype_data *devtype_data;
1031 struct s3c2410_platform_nand *pdata;
1032 struct s3c2410_nand_info *info = platform_get_drvdata(pdev);
1033 struct device_node *np = pdev->dev.of_node, *child;
1034 struct s3c2410_nand_set *sets;
1035
1036 devtype_data = of_device_get_match_data(&pdev->dev);
1037 if (!devtype_data)
1038 return -ENODEV;
1039
1040 info->cpu_type = devtype_data->type;
1041
1042 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1043 if (!pdata)
1044 return -ENOMEM;
1045
1046 pdev->dev.platform_data = pdata;
1047
1048 pdata->nr_sets = of_get_child_count(np);
1049 if (!pdata->nr_sets)
1050 return 0;
1051
Kees Cooka86854d2018-06-12 14:07:58 -07001052 sets = devm_kcalloc(&pdev->dev, pdata->nr_sets, sizeof(*sets),
Sergio Prado1c825ad2016-10-26 21:59:55 -02001053 GFP_KERNEL);
1054 if (!sets)
1055 return -ENOMEM;
1056
1057 pdata->sets = sets;
1058
1059 for_each_available_child_of_node(np, child) {
1060 sets->name = (char *)child->name;
1061 sets->of_node = child;
1062 sets->nr_chips = 1;
1063
1064 of_node_get(child);
1065
1066 sets++;
1067 }
1068
1069 return 0;
1070}
1071
1072static int s3c24xx_nand_probe_pdata(struct platform_device *pdev)
1073{
1074 struct s3c2410_nand_info *info = platform_get_drvdata(pdev);
1075
1076 info->cpu_type = platform_get_device_id(pdev)->driver_data;
1077
Sergio Pradoe9f66ae2016-10-20 19:42:44 -02001078 return 0;
Ben Dooks71d54f32008-04-15 11:36:19 +01001079}
1080
Ben Dooksec0482e2009-05-30 16:55:29 +01001081/* s3c24xx_nand_probe
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 *
1083 * called by device layer when it finds a device matching
1084 * one our driver can handled. This code checks to see if
1085 * it can allocate all necessary resources then calls the
1086 * nand layer to look for devices
1087*/
Ben Dooksec0482e2009-05-30 16:55:29 +01001088static int s3c24xx_nand_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
Sergio Prado1c825ad2016-10-26 21:59:55 -02001090 struct s3c2410_platform_nand *plat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 struct s3c2410_nand_info *info;
1092 struct s3c2410_nand_mtd *nmtd;
1093 struct s3c2410_nand_set *sets;
1094 struct resource *res;
1095 int err = 0;
1096 int size;
1097 int nr_sets;
1098 int setno;
1099
Sachin Kamat6f32a3e2012-08-21 14:24:09 +05301100 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 if (info == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 err = -ENOMEM;
1103 goto exit_error;
1104 }
1105
Russell King3ae5eae2005-11-09 22:32:44 +00001106 platform_set_drvdata(pdev, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Miquel Raynal7da45132018-07-17 09:08:02 +02001108 nand_controller_init(&info->controller);
Miquel Raynal12748312018-07-20 17:15:10 +02001109 info->controller.ops = &s3c24xx_nand_controller_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
1111 /* get the clock source and enable it */
1112
Sachin Kamat6f32a3e2012-08-21 14:24:09 +05301113 info->clk = devm_clk_get(&pdev->dev, "nand");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 if (IS_ERR(info->clk)) {
Joe Perches898eb712007-10-18 03:06:30 -07001115 dev_err(&pdev->dev, "failed to get clock\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 err = -ENOENT;
1117 goto exit_error;
1118 }
1119
Jiri Pinkavaac497c12011-04-13 11:59:30 +02001120 s3c2410_nand_clk_set_state(info, CLOCK_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Sergio Prado1c825ad2016-10-26 21:59:55 -02001122 if (pdev->dev.of_node)
1123 err = s3c24xx_nand_probe_dt(pdev);
1124 else
1125 err = s3c24xx_nand_probe_pdata(pdev);
1126
1127 if (err)
1128 goto exit_error;
1129
1130 plat = to_nand_plat(pdev);
1131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 /* allocate and map the resource */
1133
Ben Dooksa4f957f2005-06-20 12:48:25 +01001134 /* currently we assume we have the one resource */
Sachin Kamat6f32a3e2012-08-21 14:24:09 +05301135 res = pdev->resource;
H Hartley Sweetenfc161c42009-12-14 16:56:22 -05001136 size = resource_size(res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Sachin Kamat6f32a3e2012-08-21 14:24:09 +05301138 info->device = &pdev->dev;
1139 info->platform = plat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Thierry Redingb0de7742013-01-21 11:09:12 +01001141 info->regs = devm_ioremap_resource(&pdev->dev, res);
1142 if (IS_ERR(info->regs)) {
1143 err = PTR_ERR(info->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 goto exit_error;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Russell King3ae5eae2005-11-09 22:32:44 +00001147 dev_dbg(&pdev->dev, "mapped registers at %p\n", info->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Boris Brezillonbdc4e582018-07-19 22:53:50 +02001149 if (!plat->sets || plat->nr_sets < 1) {
1150 err = -EINVAL;
1151 goto exit_error;
1152 }
1153
1154 sets = plat->sets;
1155 nr_sets = plat->nr_sets;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
1157 info->mtd_count = nr_sets;
1158
1159 /* allocate our information */
1160
1161 size = nr_sets * sizeof(*info->mtds);
Sachin Kamat6f32a3e2012-08-21 14:24:09 +05301162 info->mtds = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 if (info->mtds == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 err = -ENOMEM;
1165 goto exit_error;
1166 }
1167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 /* initialise all possible chips */
1169
1170 nmtd = info->mtds;
1171
Boris Brezillonbdc4e582018-07-19 22:53:50 +02001172 for (setno = 0; setno < nr_sets; setno++, nmtd++, sets++) {
Boris BREZILLON7208b992015-12-10 09:00:22 +01001173 struct mtd_info *mtd = nand_to_mtd(&nmtd->chip);
1174
Sachin Kamatf938bc52012-08-21 10:21:15 +05301175 pr_debug("initialising set %d (%p, info %p)\n",
1176 setno, nmtd, info);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001177
Boris BREZILLON7208b992015-12-10 09:00:22 +01001178 mtd->dev.parent = &pdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 s3c2410_nand_init_chip(info, nmtd, sets);
1180
Boris Brezillon00ad3782018-09-06 14:05:14 +02001181 err = nand_scan(&nmtd->chip, sets ? sets->nr_chips : 1);
Miquel Raynalbb00ff22018-03-21 14:01:57 +01001182 if (err)
1183 goto exit_error;
1184
1185 s3c2410_nand_add_partition(info, nmtd, sets);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001187
Sergio Prado1c825ad2016-10-26 21:59:55 -02001188 /* initialise the hardware */
1189 err = s3c2410_nand_inithw(info);
1190 if (err != 0)
1191 goto exit_error;
1192
Ben Dooks30821fe2008-07-15 11:58:31 +01001193 err = s3c2410_nand_cpufreq_register(info);
1194 if (err < 0) {
1195 dev_err(&pdev->dev, "failed to init cpufreq support\n");
1196 goto exit_error;
1197 }
1198
Jiri Pinkavaac497c12011-04-13 11:59:30 +02001199 if (allow_clk_suspend(info)) {
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001200 dev_info(&pdev->dev, "clock idle support enabled\n");
Jiri Pinkavaac497c12011-04-13 11:59:30 +02001201 s3c2410_nand_clk_set_state(info, CLOCK_SUSPEND);
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001202 }
1203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 return 0;
1205
1206 exit_error:
Ben Dooksec0482e2009-05-30 16:55:29 +01001207 s3c24xx_nand_remove(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209 if (err == 0)
1210 err = -EINVAL;
1211 return err;
1212}
1213
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001214/* PM Support */
1215#ifdef CONFIG_PM
1216
1217static int s3c24xx_nand_suspend(struct platform_device *dev, pm_message_t pm)
1218{
1219 struct s3c2410_nand_info *info = platform_get_drvdata(dev);
1220
1221 if (info) {
Ben Dooks09160832008-04-15 11:36:18 +01001222 info->save_sel = readl(info->sel_reg);
Ben Dooks03680b12007-11-19 23:28:07 +00001223
1224 /* For the moment, we must ensure nFCE is high during
1225 * the time we are suspended. This really should be
1226 * handled by suspending the MTDs we are using, but
1227 * that is currently not the case. */
1228
Ben Dooks09160832008-04-15 11:36:18 +01001229 writel(info->save_sel | info->sel_bit, info->sel_reg);
Ben Dooks03680b12007-11-19 23:28:07 +00001230
Jiri Pinkavaac497c12011-04-13 11:59:30 +02001231 s3c2410_nand_clk_set_state(info, CLOCK_DISABLE);
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001232 }
1233
1234 return 0;
1235}
1236
1237static int s3c24xx_nand_resume(struct platform_device *dev)
1238{
1239 struct s3c2410_nand_info *info = platform_get_drvdata(dev);
Ben Dooks09160832008-04-15 11:36:18 +01001240 unsigned long sel;
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001241
1242 if (info) {
Jiri Pinkavaac497c12011-04-13 11:59:30 +02001243 s3c2410_nand_clk_set_state(info, CLOCK_ENABLE);
Ben Dooks30821fe2008-07-15 11:58:31 +01001244 s3c2410_nand_inithw(info);
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001245
Ben Dooks03680b12007-11-19 23:28:07 +00001246 /* Restore the state of the nFCE line. */
1247
Ben Dooks09160832008-04-15 11:36:18 +01001248 sel = readl(info->sel_reg);
1249 sel &= ~info->sel_bit;
1250 sel |= info->save_sel & info->sel_bit;
1251 writel(sel, info->sel_reg);
Ben Dooks03680b12007-11-19 23:28:07 +00001252
Jiri Pinkavaac497c12011-04-13 11:59:30 +02001253 s3c2410_nand_clk_set_state(info, CLOCK_SUSPEND);
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001254 }
1255
1256 return 0;
1257}
1258
1259#else
1260#define s3c24xx_nand_suspend NULL
1261#define s3c24xx_nand_resume NULL
1262#endif
1263
Ben Dooksa4f957f2005-06-20 12:48:25 +01001264/* driver device registration */
1265
Krzysztof Kozlowski0abe75d2015-05-02 00:50:02 +09001266static const struct platform_device_id s3c24xx_driver_ids[] = {
Ben Dooksec0482e2009-05-30 16:55:29 +01001267 {
1268 .name = "s3c2410-nand",
1269 .driver_data = TYPE_S3C2410,
1270 }, {
1271 .name = "s3c2440-nand",
1272 .driver_data = TYPE_S3C2440,
1273 }, {
1274 .name = "s3c2412-nand",
1275 .driver_data = TYPE_S3C2412,
Peter Korsgaard9dbc0902009-06-07 06:04:23 -07001276 }, {
1277 .name = "s3c6400-nand",
1278 .driver_data = TYPE_S3C2412, /* compatible with 2412 */
Russell King3ae5eae2005-11-09 22:32:44 +00001279 },
Ben Dooksec0482e2009-05-30 16:55:29 +01001280 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281};
1282
Ben Dooksec0482e2009-05-30 16:55:29 +01001283MODULE_DEVICE_TABLE(platform, s3c24xx_driver_ids);
Ben Dooksa4f957f2005-06-20 12:48:25 +01001284
Ben Dooksec0482e2009-05-30 16:55:29 +01001285static struct platform_driver s3c24xx_nand_driver = {
1286 .probe = s3c24xx_nand_probe,
1287 .remove = s3c24xx_nand_remove,
Ben Dooks2c06a082006-06-27 14:35:46 +01001288 .suspend = s3c24xx_nand_suspend,
1289 .resume = s3c24xx_nand_resume,
Ben Dooksec0482e2009-05-30 16:55:29 +01001290 .id_table = s3c24xx_driver_ids,
Ben Dooks2c06a082006-06-27 14:35:46 +01001291 .driver = {
Ben Dooksec0482e2009-05-30 16:55:29 +01001292 .name = "s3c24xx-nand",
Sergio Prado1c825ad2016-10-26 21:59:55 -02001293 .of_match_table = s3c24xx_nand_dt_ids,
Ben Dooks2c06a082006-06-27 14:35:46 +01001294 },
1295};
1296
Sachin Kamat056fcab2012-07-16 16:02:22 +05301297module_platform_driver(s3c24xx_nand_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299MODULE_LICENSE("GPL");
1300MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
Ben Dooksa4f957f2005-06-20 12:48:25 +01001301MODULE_DESCRIPTION("S3C24XX MTD NAND driver");