blob: d459c19d78de383c1c2a88a1bcda0d1301b3c003 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* linux/drivers/mtd/nand/s3c2410.c
2 *
Ben Dooks7e74a502008-05-20 17:32:27 +01003 * Copyright © 2004-2008 Simtec Electronics
4 * http://armlinux.simtec.co.uk/
Ben Dooksfdf2fd52005-02-18 14:46:15 +00005 * Ben Dooks <ben@simtec.co.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
Ben Dooks7e74a502008-05-20 17:32:27 +01007 * Samsung S3C2410/S3C2440/S3C2412 NAND driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22*/
23
Sachin Kamat92aeb5d2012-07-16 16:02:23 +053024#define pr_fmt(fmt) "nand-s3c2410: " fmt
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#ifdef CONFIG_MTD_NAND_S3C2410_DEBUG
27#define DEBUG
28#endif
29
30#include <linux/module.h>
31#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/kernel.h>
33#include <linux/string.h>
Sachin Kamatd2a89be2012-07-16 16:02:24 +053034#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/ioport.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010036#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/delay.h>
38#include <linux/err.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080039#include <linux/slab.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000040#include <linux/clk.h>
Ben Dooks30821fe2008-07-15 11:58:31 +010041#include <linux/cpufreq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include <linux/mtd/mtd.h>
44#include <linux/mtd/nand.h>
45#include <linux/mtd/nand_ecc.h>
46#include <linux/mtd/partitions.h>
47
Arnd Bergmann436d42c2012-08-24 15:22:12 +020048#include <linux/platform_data/mtd-nand-s3c2410.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Sachin Kamat02d01862014-01-10 11:24:13 +053050#define S3C2410_NFREG(x) (x)
51
52#define S3C2410_NFCONF S3C2410_NFREG(0x00)
53#define S3C2410_NFCMD S3C2410_NFREG(0x04)
54#define S3C2410_NFADDR S3C2410_NFREG(0x08)
55#define S3C2410_NFDATA S3C2410_NFREG(0x0C)
56#define S3C2410_NFSTAT S3C2410_NFREG(0x10)
57#define S3C2410_NFECC S3C2410_NFREG(0x14)
58#define S3C2440_NFCONT S3C2410_NFREG(0x04)
59#define S3C2440_NFCMD S3C2410_NFREG(0x08)
60#define S3C2440_NFADDR S3C2410_NFREG(0x0C)
61#define S3C2440_NFDATA S3C2410_NFREG(0x10)
62#define S3C2440_NFSTAT S3C2410_NFREG(0x20)
63#define S3C2440_NFMECC0 S3C2410_NFREG(0x2C)
64#define S3C2412_NFSTAT S3C2410_NFREG(0x28)
65#define S3C2412_NFMECC0 S3C2410_NFREG(0x34)
66#define S3C2410_NFCONF_EN (1<<15)
67#define S3C2410_NFCONF_INITECC (1<<12)
68#define S3C2410_NFCONF_nFCE (1<<11)
69#define S3C2410_NFCONF_TACLS(x) ((x)<<8)
70#define S3C2410_NFCONF_TWRPH0(x) ((x)<<4)
71#define S3C2410_NFCONF_TWRPH1(x) ((x)<<0)
72#define S3C2410_NFSTAT_BUSY (1<<0)
73#define S3C2440_NFCONF_TACLS(x) ((x)<<12)
74#define S3C2440_NFCONF_TWRPH0(x) ((x)<<8)
75#define S3C2440_NFCONF_TWRPH1(x) ((x)<<4)
76#define S3C2440_NFCONT_INITECC (1<<4)
77#define S3C2440_NFCONT_nFCE (1<<1)
78#define S3C2440_NFCONT_ENABLE (1<<0)
79#define S3C2440_NFSTAT_READY (1<<0)
80#define S3C2412_NFCONF_NANDBOOT (1<<31)
81#define S3C2412_NFCONT_INIT_MAIN_ECC (1<<5)
82#define S3C2412_NFCONT_nFCE0 (1<<1)
83#define S3C2412_NFSTAT_READY (1<<0)
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085/* new oob placement block for use with hardware ecc generation
86 */
Boris Brezillonbf01e06b2016-02-03 20:03:30 +010087static int s3c2410_ooblayout_ecc(struct mtd_info *mtd, int section,
88 struct mtd_oob_region *oobregion)
89{
90 if (section)
91 return -ERANGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Boris Brezillonbf01e06b2016-02-03 20:03:30 +010093 oobregion->offset = 0;
94 oobregion->length = 3;
95
96 return 0;
97}
98
99static int s3c2410_ooblayout_free(struct mtd_info *mtd, int section,
100 struct mtd_oob_region *oobregion)
101{
102 if (section)
103 return -ERANGE;
104
105 oobregion->offset = 8;
106 oobregion->length = 8;
107
108 return 0;
109}
110
111static const struct mtd_ooblayout_ops s3c2410_ooblayout_ops = {
112 .ecc = s3c2410_ooblayout_ecc,
113 .free = s3c2410_ooblayout_free,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114};
115
116/* controller and mtd information */
117
118struct s3c2410_nand_info;
119
Ben Dooks3db72152009-05-30 17:18:15 +0100120/**
121 * struct s3c2410_nand_mtd - driver MTD structure
122 * @mtd: The MTD instance to pass to the MTD layer.
123 * @chip: The NAND chip information.
124 * @set: The platform information supplied for this set of NAND chips.
125 * @info: Link back to the hardware information.
126 * @scan_res: The result from calling nand_scan_ident().
127*/
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;
132 int scan_res;
133};
134
Ben Dooks2c06a082006-06-27 14:35:46 +0100135enum s3c_cpu_type {
136 TYPE_S3C2410,
137 TYPE_S3C2412,
138 TYPE_S3C2440,
139};
140
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200141enum s3c_nand_clk_state {
142 CLOCK_DISABLE = 0,
143 CLOCK_ENABLE,
144 CLOCK_SUSPEND,
145};
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147/* overview of the s3c2410 nand state */
148
Ben Dooks3db72152009-05-30 17:18:15 +0100149/**
150 * struct s3c2410_nand_info - NAND controller state.
151 * @mtds: An array of MTD instances on this controoler.
152 * @platform: The platform data for this board.
153 * @device: The platform device we bound to.
Ben Dooks3db72152009-05-30 17:18:15 +0100154 * @clk: The clock resource for this controller.
Sachin Kamat6f32a3e2012-08-21 14:24:09 +0530155 * @regs: The area mapped for the hardware registers.
Ben Dooks3db72152009-05-30 17:18:15 +0100156 * @sel_reg: Pointer to the register controlling the NAND selection.
157 * @sel_bit: The bit in @sel_reg to select the NAND chip.
158 * @mtd_count: The number of MTDs created from this controller.
159 * @save_sel: The contents of @sel_reg to be saved over suspend.
160 * @clk_rate: The clock rate from @clk.
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200161 * @clk_state: The current clock state.
Ben Dooks3db72152009-05-30 17:18:15 +0100162 * @cpu_type: The exact type of this controller.
163 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164struct s3c2410_nand_info {
165 /* mtd info */
166 struct nand_hw_control controller;
167 struct s3c2410_nand_mtd *mtds;
168 struct s3c2410_platform_nand *platform;
169
170 /* device info */
171 struct device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 struct clk *clk;
Ben Dooksfdf2fd52005-02-18 14:46:15 +0000173 void __iomem *regs;
Ben Dooks2c06a082006-06-27 14:35:46 +0100174 void __iomem *sel_reg;
175 int sel_bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 int mtd_count;
Ben Dooks09160832008-04-15 11:36:18 +0100177 unsigned long save_sel;
Ben Dooks30821fe2008-07-15 11:58:31 +0100178 unsigned long clk_rate;
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200179 enum s3c_nand_clk_state clk_state;
Ben Dooks03680b12007-11-19 23:28:07 +0000180
Ben Dooks2c06a082006-06-27 14:35:46 +0100181 enum s3c_cpu_type cpu_type;
Ben Dooks30821fe2008-07-15 11:58:31 +0100182
Krzysztof Kozlowskid9ca77f2016-06-27 14:51:38 +0200183#ifdef CONFIG_ARM_S3C24XX_CPUFREQ
Ben Dooks30821fe2008-07-15 11:58:31 +0100184 struct notifier_block freq_transition;
185#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186};
187
188/* conversion functions */
189
190static struct s3c2410_nand_mtd *s3c2410_nand_mtd_toours(struct mtd_info *mtd)
191{
Boris BREZILLON7208b992015-12-10 09:00:22 +0100192 return container_of(mtd_to_nand(mtd), struct s3c2410_nand_mtd,
193 chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
196static struct s3c2410_nand_info *s3c2410_nand_mtd_toinfo(struct mtd_info *mtd)
197{
198 return s3c2410_nand_mtd_toours(mtd)->info;
199}
200
Russell King3ae5eae2005-11-09 22:32:44 +0000201static struct s3c2410_nand_info *to_nand_info(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Russell King3ae5eae2005-11-09 22:32:44 +0000203 return platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
Russell King3ae5eae2005-11-09 22:32:44 +0000206static struct s3c2410_platform_nand *to_nand_plat(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Jingoo Han453810b2013-07-30 17:18:33 +0900208 return dev_get_platdata(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200211static inline int allow_clk_suspend(struct s3c2410_nand_info *info)
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100212{
Sachin Kamata68c5ec2012-07-16 16:02:25 +0530213#ifdef CONFIG_MTD_NAND_S3C2410_CLKSTOP
214 return 1;
215#else
216 return 0;
217#endif
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100218}
219
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200220/**
221 * s3c2410_nand_clk_set_state - Enable, disable or suspend NAND clock.
222 * @info: The controller instance.
223 * @new_state: State to which clock should be set.
224 */
225static void s3c2410_nand_clk_set_state(struct s3c2410_nand_info *info,
226 enum s3c_nand_clk_state new_state)
227{
228 if (!allow_clk_suspend(info) && new_state == CLOCK_SUSPEND)
229 return;
230
231 if (info->clk_state == CLOCK_ENABLE) {
232 if (new_state != CLOCK_ENABLE)
Vasily Khoruzhick887957b2014-06-30 22:12:16 +0300233 clk_disable_unprepare(info->clk);
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200234 } else {
235 if (new_state == CLOCK_ENABLE)
Vasily Khoruzhick887957b2014-06-30 22:12:16 +0300236 clk_prepare_enable(info->clk);
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200237 }
238
239 info->clk_state = new_state;
240}
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242/* timing calculations */
243
Ben Dookscfd320f2005-10-20 22:22:58 +0100244#define NS_IN_KHZ 1000000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Ben Dooks3db72152009-05-30 17:18:15 +0100246/**
247 * s3c_nand_calc_rate - calculate timing data.
248 * @wanted: The cycle time in nanoseconds.
249 * @clk: The clock rate in kHz.
250 * @max: The maximum divider value.
251 *
252 * Calculate the timing value from the given parameters.
253 */
Ben Dooks2c06a082006-06-27 14:35:46 +0100254static int s3c_nand_calc_rate(int wanted, unsigned long clk, int max)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
256 int result;
257
Ben Dooks947391c2009-05-30 18:34:16 +0100258 result = DIV_ROUND_UP((wanted * clk), NS_IN_KHZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 pr_debug("result %d from %ld, %d\n", result, clk, wanted);
261
262 if (result > max) {
Sachin Kamat92aeb5d2012-07-16 16:02:23 +0530263 pr_err("%d ns is too big for current clock rate %ld\n",
264 wanted, clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return -1;
266 }
267
268 if (result < 1)
269 result = 1;
270
271 return result;
272}
273
Sachin Kamat54cd0202012-07-16 16:02:26 +0530274#define to_ns(ticks, clk) (((ticks) * NS_IN_KHZ) / (unsigned int)(clk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276/* controller setup */
277
Ben Dooks3db72152009-05-30 17:18:15 +0100278/**
279 * s3c2410_nand_setrate - setup controller timing information.
280 * @info: The controller instance.
281 *
282 * Given the information supplied by the platform, calculate and set
283 * the necessary timing registers in the hardware to generate the
284 * necessary timing cycles to the hardware.
285 */
Ben Dooks30821fe2008-07-15 11:58:31 +0100286static int s3c2410_nand_setrate(struct s3c2410_nand_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
Ben Dooks30821fe2008-07-15 11:58:31 +0100288 struct s3c2410_platform_nand *plat = info->platform;
Ben Dooks2c06a082006-06-27 14:35:46 +0100289 int tacls_max = (info->cpu_type == TYPE_S3C2412) ? 8 : 4;
Ben Dookscfd320f2005-10-20 22:22:58 +0100290 int tacls, twrph0, twrph1;
Ben Dooks30821fe2008-07-15 11:58:31 +0100291 unsigned long clkrate = clk_get_rate(info->clk);
Nelson Castillo2612e522009-05-10 15:41:54 -0500292 unsigned long uninitialized_var(set), cfg, uninitialized_var(mask);
Ben Dooks30821fe2008-07-15 11:58:31 +0100293 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 /* calculate the timing information for the controller */
296
Ben Dooks30821fe2008-07-15 11:58:31 +0100297 info->clk_rate = clkrate;
Ben Dookscfd320f2005-10-20 22:22:58 +0100298 clkrate /= 1000; /* turn clock into kHz for ease of use */
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 if (plat != NULL) {
Ben Dooks2c06a082006-06-27 14:35:46 +0100301 tacls = s3c_nand_calc_rate(plat->tacls, clkrate, tacls_max);
302 twrph0 = s3c_nand_calc_rate(plat->twrph0, clkrate, 8);
303 twrph1 = s3c_nand_calc_rate(plat->twrph1, clkrate, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 } else {
305 /* default timings */
Ben Dooks2c06a082006-06-27 14:35:46 +0100306 tacls = tacls_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 twrph0 = 8;
308 twrph1 = 8;
309 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (tacls < 0 || twrph0 < 0 || twrph1 < 0) {
Ben Dooks99974c62006-06-21 15:43:05 +0100312 dev_err(info->device, "cannot get suitable timings\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 return -EINVAL;
314 }
315
Ben Dooks99974c62006-06-21 15:43:05 +0100316 dev_info(info->device, "Tacls=%d, %dns Twrph0=%d %dns, Twrph1=%d %dns\n",
Sachin Kamat54cd0202012-07-16 16:02:26 +0530317 tacls, to_ns(tacls, clkrate), twrph0, to_ns(twrph0, clkrate),
318 twrph1, to_ns(twrph1, clkrate));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Ben Dooks30821fe2008-07-15 11:58:31 +0100320 switch (info->cpu_type) {
321 case TYPE_S3C2410:
322 mask = (S3C2410_NFCONF_TACLS(3) |
323 S3C2410_NFCONF_TWRPH0(7) |
324 S3C2410_NFCONF_TWRPH1(7));
325 set = S3C2410_NFCONF_EN;
326 set |= S3C2410_NFCONF_TACLS(tacls - 1);
327 set |= S3C2410_NFCONF_TWRPH0(twrph0 - 1);
328 set |= S3C2410_NFCONF_TWRPH1(twrph1 - 1);
329 break;
330
331 case TYPE_S3C2440:
332 case TYPE_S3C2412:
Peter Korsgaarda755a382009-06-03 13:46:54 +0200333 mask = (S3C2440_NFCONF_TACLS(tacls_max - 1) |
334 S3C2440_NFCONF_TWRPH0(7) |
335 S3C2440_NFCONF_TWRPH1(7));
Ben Dooks30821fe2008-07-15 11:58:31 +0100336
337 set = S3C2440_NFCONF_TACLS(tacls - 1);
338 set |= S3C2440_NFCONF_TWRPH0(twrph0 - 1);
339 set |= S3C2440_NFCONF_TWRPH1(twrph1 - 1);
340 break;
341
342 default:
Ben Dooks30821fe2008-07-15 11:58:31 +0100343 BUG();
344 }
345
Ben Dooks30821fe2008-07-15 11:58:31 +0100346 local_irq_save(flags);
347
348 cfg = readl(info->regs + S3C2410_NFCONF);
349 cfg &= ~mask;
350 cfg |= set;
351 writel(cfg, info->regs + S3C2410_NFCONF);
352
353 local_irq_restore(flags);
354
Andy Greenae7304e2009-05-10 15:42:02 -0500355 dev_dbg(info->device, "NF_CONF is 0x%lx\n", cfg);
356
Ben Dooks30821fe2008-07-15 11:58:31 +0100357 return 0;
358}
359
Ben Dooks3db72152009-05-30 17:18:15 +0100360/**
361 * s3c2410_nand_inithw - basic hardware initialisation
362 * @info: The hardware state.
363 *
364 * Do the basic initialisation of the hardware, using s3c2410_nand_setrate()
365 * to setup the hardware access speeds and set the controller to be enabled.
366*/
Ben Dooks30821fe2008-07-15 11:58:31 +0100367static int s3c2410_nand_inithw(struct s3c2410_nand_info *info)
368{
369 int ret;
370
371 ret = s3c2410_nand_setrate(info);
372 if (ret < 0)
373 return ret;
374
Sachin Kamat54cd0202012-07-16 16:02:26 +0530375 switch (info->cpu_type) {
376 case TYPE_S3C2410:
Ben Dooks30821fe2008-07-15 11:58:31 +0100377 default:
Ben Dooks2c06a082006-06-27 14:35:46 +0100378 break;
379
Sachin Kamat54cd0202012-07-16 16:02:26 +0530380 case TYPE_S3C2440:
381 case TYPE_S3C2412:
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100382 /* enable the controller and de-assert nFCE */
383
Ben Dooks2c06a082006-06-27 14:35:46 +0100384 writel(S3C2440_NFCONT_ENABLE, info->regs + S3C2440_NFCONT);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 return 0;
388}
389
Ben Dooks3db72152009-05-30 17:18:15 +0100390/**
391 * s3c2410_nand_select_chip - select the given nand chip
392 * @mtd: The MTD instance for this chip.
393 * @chip: The chip number.
394 *
395 * This is called by the MTD layer to either select a given chip for the
396 * @mtd instance, or to indicate that the access has finished and the
397 * chip can be de-selected.
398 *
399 * The routine ensures that the nFCE line is correctly setup, and any
400 * platform specific selection code is called to route nFCE to the specific
401 * chip.
402 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403static void s3c2410_nand_select_chip(struct mtd_info *mtd, int chip)
404{
405 struct s3c2410_nand_info *info;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000406 struct s3c2410_nand_mtd *nmtd;
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100407 struct nand_chip *this = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 unsigned long cur;
409
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100410 nmtd = nand_get_controller_data(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 info = nmtd->info;
412
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200413 if (chip != -1)
414 s3c2410_nand_clk_set_state(info, CLOCK_ENABLE);
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100415
Ben Dooks2c06a082006-06-27 14:35:46 +0100416 cur = readl(info->sel_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 if (chip == -1) {
Ben Dooks2c06a082006-06-27 14:35:46 +0100419 cur |= info->sel_bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 } else {
Ben Dooksfb8d82a2005-07-06 21:05:10 +0100421 if (nmtd->set != NULL && chip > nmtd->set->nr_chips) {
Ben Dooks99974c62006-06-21 15:43:05 +0100422 dev_err(info->device, "invalid chip %d\n", chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return;
424 }
425
426 if (info->platform != NULL) {
427 if (info->platform->select_chip != NULL)
David Woodhousee0c7d762006-05-13 18:07:53 +0100428 (info->platform->select_chip) (nmtd->set, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
430
Ben Dooks2c06a082006-06-27 14:35:46 +0100431 cur &= ~info->sel_bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
433
Ben Dooks2c06a082006-06-27 14:35:46 +0100434 writel(cur, info->sel_reg);
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100435
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200436 if (chip == -1)
437 s3c2410_nand_clk_set_state(info, CLOCK_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
Ben Dooksad3b5fb2006-06-19 09:43:23 +0100440/* s3c2410_nand_hwcontrol
Ben Dooksa4f957f2005-06-20 12:48:25 +0100441 *
Ben Dooksad3b5fb2006-06-19 09:43:23 +0100442 * Issue command and address cycles to the chip
Ben Dooksa4f957f2005-06-20 12:48:25 +0100443*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200445static void s3c2410_nand_hwcontrol(struct mtd_info *mtd, int cmd,
David Woodhousef9068872006-06-10 00:53:16 +0100446 unsigned int ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
448 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
David Woodhousec9ac5972006-11-30 08:17:38 +0000449
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200450 if (cmd == NAND_CMD_NONE)
451 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
David Woodhousef9068872006-06-10 00:53:16 +0100453 if (ctrl & NAND_CLE)
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200454 writeb(cmd, info->regs + S3C2410_NFCMD);
455 else
456 writeb(cmd, info->regs + S3C2410_NFADDR);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100457}
458
459/* command and control functions */
460
David Woodhousef9068872006-06-10 00:53:16 +0100461static void s3c2440_nand_hwcontrol(struct mtd_info *mtd, int cmd,
462 unsigned int ctrl)
Ben Dooksa4f957f2005-06-20 12:48:25 +0100463{
464 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100465
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200466 if (cmd == NAND_CMD_NONE)
467 return;
Ben Dooksa4f957f2005-06-20 12:48:25 +0100468
David Woodhousef9068872006-06-10 00:53:16 +0100469 if (ctrl & NAND_CLE)
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200470 writeb(cmd, info->regs + S3C2440_NFCMD);
471 else
472 writeb(cmd, info->regs + S3C2440_NFADDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475/* s3c2410_nand_devready()
476 *
477 * returns 0 if the nand is busy, 1 if it is ready
478*/
479
480static int s3c2410_nand_devready(struct mtd_info *mtd)
481{
482 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 return readb(info->regs + S3C2410_NFSTAT) & S3C2410_NFSTAT_BUSY;
484}
485
Ben Dooks2c06a082006-06-27 14:35:46 +0100486static int s3c2440_nand_devready(struct mtd_info *mtd)
487{
488 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
489 return readb(info->regs + S3C2440_NFSTAT) & S3C2440_NFSTAT_READY;
490}
491
492static int s3c2412_nand_devready(struct mtd_info *mtd)
493{
494 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
495 return readb(info->regs + S3C2412_NFSTAT) & S3C2412_NFSTAT_READY;
496}
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498/* ECC handling functions */
499
Sachin Kamat19da4152012-08-21 14:24:10 +0530500#ifdef CONFIG_MTD_NAND_S3C2410_HWECC
Ben Dooks2c06a082006-06-27 14:35:46 +0100501static int s3c2410_nand_correct_data(struct mtd_info *mtd, u_char *dat,
502 u_char *read_ecc, u_char *calc_ecc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Ben Dooksa2593242007-02-02 16:59:33 +0000504 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
505 unsigned int diff0, diff1, diff2;
506 unsigned int bit, byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Ben Dooksa2593242007-02-02 16:59:33 +0000508 pr_debug("%s(%p,%p,%p,%p)\n", __func__, mtd, dat, read_ecc, calc_ecc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Ben Dooksa2593242007-02-02 16:59:33 +0000510 diff0 = read_ecc[0] ^ calc_ecc[0];
511 diff1 = read_ecc[1] ^ calc_ecc[1];
512 diff2 = read_ecc[2] ^ calc_ecc[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Andy Shevchenko13e85972012-08-02 16:06:47 +0300514 pr_debug("%s: rd %*phN calc %*phN diff %02x%02x%02x\n",
515 __func__, 3, read_ecc, 3, calc_ecc,
Ben Dooksa2593242007-02-02 16:59:33 +0000516 diff0, diff1, diff2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Ben Dooksa2593242007-02-02 16:59:33 +0000518 if (diff0 == 0 && diff1 == 0 && diff2 == 0)
519 return 0; /* ECC is ok */
520
Ben Dooksc45c6c62008-04-15 11:36:20 +0100521 /* sometimes people do not think about using the ECC, so check
522 * to see if we have an 0xff,0xff,0xff read ECC and then ignore
523 * the error, on the assumption that this is an un-eccd page.
524 */
525 if (read_ecc[0] == 0xff && read_ecc[1] == 0xff && read_ecc[2] == 0xff
526 && info->platform->ignore_unset_ecc)
527 return 0;
528
Ben Dooksa2593242007-02-02 16:59:33 +0000529 /* Can we correct this ECC (ie, one row and column change).
530 * Note, this is similar to the 256 error code on smartmedia */
531
532 if (((diff0 ^ (diff0 >> 1)) & 0x55) == 0x55 &&
533 ((diff1 ^ (diff1 >> 1)) & 0x55) == 0x55 &&
534 ((diff2 ^ (diff2 >> 1)) & 0x55) == 0x55) {
535 /* calculate the bit position of the error */
536
Matt Reimerd0bf3792007-10-18 18:02:43 -0700537 bit = ((diff2 >> 3) & 1) |
538 ((diff2 >> 4) & 2) |
539 ((diff2 >> 5) & 4);
Ben Dooksa2593242007-02-02 16:59:33 +0000540
541 /* calculate the byte position of the error */
542
Matt Reimerd0bf3792007-10-18 18:02:43 -0700543 byte = ((diff2 << 7) & 0x100) |
544 ((diff1 << 0) & 0x80) |
545 ((diff1 << 1) & 0x40) |
546 ((diff1 << 2) & 0x20) |
547 ((diff1 << 3) & 0x10) |
548 ((diff0 >> 4) & 0x08) |
549 ((diff0 >> 3) & 0x04) |
550 ((diff0 >> 2) & 0x02) |
551 ((diff0 >> 1) & 0x01);
Ben Dooksa2593242007-02-02 16:59:33 +0000552
553 dev_dbg(info->device, "correcting error bit %d, byte %d\n",
554 bit, byte);
555
556 dat[byte] ^= (1 << bit);
557 return 1;
558 }
559
560 /* if there is only one bit difference in the ECC, then
561 * one of only a row or column parity has changed, which
562 * means the error is most probably in the ECC itself */
563
564 diff0 |= (diff1 << 8);
565 diff0 |= (diff2 << 16);
566
Zhaoxiu Zeng03a97552016-04-12 15:30:35 +0800567 /* equal to "(diff0 & ~(1 << __ffs(diff0)))" */
568 if ((diff0 & (diff0 - 1)) == 0)
Ben Dooksa2593242007-02-02 16:59:33 +0000569 return 1;
570
Matt Reimer4fac9f62007-10-18 18:02:44 -0700571 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
Ben Dooksa4f957f2005-06-20 12:48:25 +0100574/* ECC functions
575 *
576 * These allow the s3c2410 and s3c2440 to use the controller's ECC
577 * generator block to ECC the data as it passes through]
578*/
579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580static void s3c2410_nand_enable_hwecc(struct mtd_info *mtd, int mode)
581{
582 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
583 unsigned long ctrl;
584
585 ctrl = readl(info->regs + S3C2410_NFCONF);
586 ctrl |= S3C2410_NFCONF_INITECC;
587 writel(ctrl, info->regs + S3C2410_NFCONF);
588}
589
Matthieu CASTET4f659922007-02-13 12:30:38 +0100590static void s3c2412_nand_enable_hwecc(struct mtd_info *mtd, int mode)
591{
592 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
593 unsigned long ctrl;
594
595 ctrl = readl(info->regs + S3C2440_NFCONT);
Sachin Kamatf938bc52012-08-21 10:21:15 +0530596 writel(ctrl | S3C2412_NFCONT_INIT_MAIN_ECC,
597 info->regs + S3C2440_NFCONT);
Matthieu CASTET4f659922007-02-13 12:30:38 +0100598}
599
Ben Dooksa4f957f2005-06-20 12:48:25 +0100600static void s3c2440_nand_enable_hwecc(struct mtd_info *mtd, int mode)
601{
602 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
603 unsigned long ctrl;
604
605 ctrl = readl(info->regs + S3C2440_NFCONT);
606 writel(ctrl | S3C2440_NFCONT_INITECC, info->regs + S3C2440_NFCONT);
607}
608
Sachin Kamatf938bc52012-08-21 10:21:15 +0530609static int s3c2410_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
610 u_char *ecc_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
612 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
613
614 ecc_code[0] = readb(info->regs + S3C2410_NFECC + 0);
615 ecc_code[1] = readb(info->regs + S3C2410_NFECC + 1);
616 ecc_code[2] = readb(info->regs + S3C2410_NFECC + 2);
617
Andy Shevchenko13e85972012-08-02 16:06:47 +0300618 pr_debug("%s: returning ecc %*phN\n", __func__, 3, ecc_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 return 0;
621}
622
Sachin Kamatf938bc52012-08-21 10:21:15 +0530623static int s3c2412_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
624 u_char *ecc_code)
Matthieu CASTET4f659922007-02-13 12:30:38 +0100625{
626 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
627 unsigned long ecc = readl(info->regs + S3C2412_NFMECC0);
628
629 ecc_code[0] = ecc;
630 ecc_code[1] = ecc >> 8;
631 ecc_code[2] = ecc >> 16;
632
Andy Shevchenko13e85972012-08-02 16:06:47 +0300633 pr_debug("%s: returning ecc %*phN\n", __func__, 3, ecc_code);
Matthieu CASTET4f659922007-02-13 12:30:38 +0100634
635 return 0;
636}
637
Sachin Kamatf938bc52012-08-21 10:21:15 +0530638static int s3c2440_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
639 u_char *ecc_code)
Ben Dooksa4f957f2005-06-20 12:48:25 +0100640{
641 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
642 unsigned long ecc = readl(info->regs + S3C2440_NFMECC0);
643
644 ecc_code[0] = ecc;
645 ecc_code[1] = ecc >> 8;
646 ecc_code[2] = ecc >> 16;
647
Ben Dooks71d54f32008-04-15 11:36:19 +0100648 pr_debug("%s: returning ecc %06lx\n", __func__, ecc & 0xffffff);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100649
650 return 0;
651}
Sachin Kamat19da4152012-08-21 14:24:10 +0530652#endif
Ben Dooksa4f957f2005-06-20 12:48:25 +0100653
Ben Dooksa4f957f2005-06-20 12:48:25 +0100654/* over-ride the standard functions for a little more speed. We can
655 * use read/write block to move the data buffers to/from the controller
656*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658static void s3c2410_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
659{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100660 struct nand_chip *this = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 readsb(this->IO_ADDR_R, buf, len);
662}
663
Matt Reimerb773bb22007-10-18 17:43:07 -0700664static void s3c2440_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
665{
666 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
Ben Dooksdea2aa62009-05-30 18:30:18 +0100667
668 readsl(info->regs + S3C2440_NFDATA, buf, len >> 2);
669
670 /* cleanup if we've got less than a word to do */
671 if (len & 3) {
672 buf += len & ~3;
673
674 for (; len & 3; len--)
675 *buf++ = readb(info->regs + S3C2440_NFDATA);
676 }
Matt Reimerb773bb22007-10-18 17:43:07 -0700677}
678
Sachin Kamatf938bc52012-08-21 10:21:15 +0530679static void s3c2410_nand_write_buf(struct mtd_info *mtd, const u_char *buf,
680 int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100682 struct nand_chip *this = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 writesb(this->IO_ADDR_W, buf, len);
684}
685
Sachin Kamatf938bc52012-08-21 10:21:15 +0530686static void s3c2440_nand_write_buf(struct mtd_info *mtd, const u_char *buf,
687 int len)
Matt Reimerb773bb22007-10-18 17:43:07 -0700688{
689 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
Ben Dooksdea2aa62009-05-30 18:30:18 +0100690
691 writesl(info->regs + S3C2440_NFDATA, buf, len >> 2);
692
693 /* cleanup any fractional write */
694 if (len & 3) {
695 buf += len & ~3;
696
697 for (; len & 3; len--, buf++)
698 writeb(*buf, info->regs + S3C2440_NFDATA);
699 }
Matt Reimerb773bb22007-10-18 17:43:07 -0700700}
701
Ben Dooks30821fe2008-07-15 11:58:31 +0100702/* cpufreq driver support */
703
Krzysztof Kozlowskid9ca77f2016-06-27 14:51:38 +0200704#ifdef CONFIG_ARM_S3C24XX_CPUFREQ
Ben Dooks30821fe2008-07-15 11:58:31 +0100705
706static int s3c2410_nand_cpufreq_transition(struct notifier_block *nb,
707 unsigned long val, void *data)
708{
709 struct s3c2410_nand_info *info;
710 unsigned long newclk;
711
712 info = container_of(nb, struct s3c2410_nand_info, freq_transition);
713 newclk = clk_get_rate(info->clk);
714
715 if ((val == CPUFREQ_POSTCHANGE && newclk < info->clk_rate) ||
716 (val == CPUFREQ_PRECHANGE && newclk > info->clk_rate)) {
717 s3c2410_nand_setrate(info);
718 }
719
720 return 0;
721}
722
723static inline int s3c2410_nand_cpufreq_register(struct s3c2410_nand_info *info)
724{
725 info->freq_transition.notifier_call = s3c2410_nand_cpufreq_transition;
726
727 return cpufreq_register_notifier(&info->freq_transition,
728 CPUFREQ_TRANSITION_NOTIFIER);
729}
730
Sachin Kamatf938bc52012-08-21 10:21:15 +0530731static inline void
732s3c2410_nand_cpufreq_deregister(struct s3c2410_nand_info *info)
Ben Dooks30821fe2008-07-15 11:58:31 +0100733{
734 cpufreq_unregister_notifier(&info->freq_transition,
735 CPUFREQ_TRANSITION_NOTIFIER);
736}
737
738#else
739static inline int s3c2410_nand_cpufreq_register(struct s3c2410_nand_info *info)
740{
741 return 0;
742}
743
Sachin Kamatf938bc52012-08-21 10:21:15 +0530744static inline void
745s3c2410_nand_cpufreq_deregister(struct s3c2410_nand_info *info)
Ben Dooks30821fe2008-07-15 11:58:31 +0100746{
747}
748#endif
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750/* device management functions */
751
Ben Dooksec0482e2009-05-30 16:55:29 +0100752static int s3c24xx_nand_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
Russell King3ae5eae2005-11-09 22:32:44 +0000754 struct s3c2410_nand_info *info = to_nand_info(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000756 if (info == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 return 0;
758
Ben Dooks30821fe2008-07-15 11:58:31 +0100759 s3c2410_nand_cpufreq_deregister(info);
760
761 /* Release all our mtds and their partitions, then go through
762 * freeing the resources used
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 if (info->mtds != NULL) {
766 struct s3c2410_nand_mtd *ptr = info->mtds;
767 int mtdno;
768
769 for (mtdno = 0; mtdno < info->mtd_count; mtdno++, ptr++) {
770 pr_debug("releasing mtd %d (%p)\n", mtdno, ptr);
Boris BREZILLON7208b992015-12-10 09:00:22 +0100771 nand_release(nand_to_mtd(&ptr->chip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
774
775 /* free the common resources */
776
Sachin Kamat6f32a3e2012-08-21 14:24:09 +0530777 if (!IS_ERR(info->clk))
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200778 s3c2410_nand_clk_set_state(info, CLOCK_DISABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 return 0;
781}
782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783static int s3c2410_nand_add_partition(struct s3c2410_nand_info *info,
784 struct s3c2410_nand_mtd *mtd,
785 struct s3c2410_nand_set *set)
786{
Sachin Kamatded4c552012-11-16 16:08:22 +0530787 if (set) {
Boris BREZILLON7208b992015-12-10 09:00:22 +0100788 struct mtd_info *mtdinfo = nand_to_mtd(&mtd->chip);
Andy Greened27f022009-05-10 15:42:09 -0500789
Boris BREZILLON7208b992015-12-10 09:00:22 +0100790 mtdinfo->name = set->name;
791
792 return mtd_device_parse_register(mtdinfo, NULL, NULL,
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +0200793 set->partitions, set->nr_partitions);
Sachin Kamatded4c552012-11-16 16:08:22 +0530794 }
795
796 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Ben Dooks3db72152009-05-30 17:18:15 +0100799/**
800 * s3c2410_nand_init_chip - initialise a single instance of an chip
801 * @info: The base NAND controller the chip is on.
802 * @nmtd: The new controller MTD instance to fill in.
803 * @set: The information passed from the board specific platform data.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 *
Ben Dooks3db72152009-05-30 17:18:15 +0100805 * Initialise the given @nmtd from the information in @info and @set. This
806 * readies the structure for use with the MTD layer functions by ensuring
807 * all pointers are setup and the necessary control routines selected.
808 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809static void s3c2410_nand_init_chip(struct s3c2410_nand_info *info,
810 struct s3c2410_nand_mtd *nmtd,
811 struct s3c2410_nand_set *set)
812{
813 struct nand_chip *chip = &nmtd->chip;
Ben Dooks2c06a082006-06-27 14:35:46 +0100814 void __iomem *regs = info->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 chip->write_buf = s3c2410_nand_write_buf;
817 chip->read_buf = s3c2410_nand_read_buf;
818 chip->select_chip = s3c2410_nand_select_chip;
819 chip->chip_delay = 50;
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100820 nand_set_controller_data(chip, nmtd);
Ben Dooks74218fe2009-11-02 18:12:51 +0000821 chip->options = set->options;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 chip->controller = &info->controller;
823
Ben Dooks2c06a082006-06-27 14:35:46 +0100824 switch (info->cpu_type) {
825 case TYPE_S3C2410:
826 chip->IO_ADDR_W = regs + S3C2410_NFDATA;
827 info->sel_reg = regs + S3C2410_NFCONF;
828 info->sel_bit = S3C2410_NFCONF_nFCE;
829 chip->cmd_ctrl = s3c2410_nand_hwcontrol;
830 chip->dev_ready = s3c2410_nand_devready;
831 break;
832
833 case TYPE_S3C2440:
834 chip->IO_ADDR_W = regs + S3C2440_NFDATA;
835 info->sel_reg = regs + S3C2440_NFCONT;
836 info->sel_bit = S3C2440_NFCONT_nFCE;
837 chip->cmd_ctrl = s3c2440_nand_hwcontrol;
838 chip->dev_ready = s3c2440_nand_devready;
Matt Reimerb773bb22007-10-18 17:43:07 -0700839 chip->read_buf = s3c2440_nand_read_buf;
840 chip->write_buf = s3c2440_nand_write_buf;
Ben Dooks2c06a082006-06-27 14:35:46 +0100841 break;
842
843 case TYPE_S3C2412:
844 chip->IO_ADDR_W = regs + S3C2440_NFDATA;
845 info->sel_reg = regs + S3C2440_NFCONT;
846 info->sel_bit = S3C2412_NFCONT_nFCE0;
847 chip->cmd_ctrl = s3c2440_nand_hwcontrol;
848 chip->dev_ready = s3c2412_nand_devready;
849
850 if (readl(regs + S3C2410_NFCONF) & S3C2412_NFCONF_NANDBOOT)
851 dev_info(info->device, "System booted from NAND\n");
852
853 break;
Sachin Kamat54cd0202012-07-16 16:02:26 +0530854 }
Ben Dooks2c06a082006-06-27 14:35:46 +0100855
856 chip->IO_ADDR_R = chip->IO_ADDR_W;
Ben Dooksa4f957f2005-06-20 12:48:25 +0100857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 nmtd->info = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 nmtd->set = set;
860
Sachin Kamata68c5ec2012-07-16 16:02:25 +0530861#ifdef CONFIG_MTD_NAND_S3C2410_HWECC
862 chip->ecc.calculate = s3c2410_nand_calculate_ecc;
863 chip->ecc.correct = s3c2410_nand_correct_data;
864 chip->ecc.mode = NAND_ECC_HW;
865 chip->ecc.strength = 1;
866
867 switch (info->cpu_type) {
868 case TYPE_S3C2410:
869 chip->ecc.hwctl = s3c2410_nand_enable_hwecc;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +0200870 chip->ecc.calculate = s3c2410_nand_calculate_ecc;
Sachin Kamata68c5ec2012-07-16 16:02:25 +0530871 break;
Ben Dooksa4f957f2005-06-20 12:48:25 +0100872
Sachin Kamata68c5ec2012-07-16 16:02:25 +0530873 case TYPE_S3C2412:
874 chip->ecc.hwctl = s3c2412_nand_enable_hwecc;
875 chip->ecc.calculate = s3c2412_nand_calculate_ecc;
876 break;
Ben Dooks2c06a082006-06-27 14:35:46 +0100877
Sachin Kamata68c5ec2012-07-16 16:02:25 +0530878 case TYPE_S3C2440:
879 chip->ecc.hwctl = s3c2440_nand_enable_hwecc;
880 chip->ecc.calculate = s3c2440_nand_calculate_ecc;
881 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 }
Sachin Kamata68c5ec2012-07-16 16:02:25 +0530883#else
884 chip->ecc.mode = NAND_ECC_SOFT;
Rafał Miłeckic4fe72a2016-04-13 14:06:55 +0200885 chip->ecc.algo = NAND_ECC_HAMMING;
Sachin Kamata68c5ec2012-07-16 16:02:25 +0530886#endif
Ben Dooks1c21ab62008-04-15 11:36:21 +0100887
Ben Dooks37e5ffa2008-04-15 11:36:22 +0100888 if (set->disable_ecc)
889 chip->ecc.mode = NAND_ECC_NONE;
Andy Green8c3e8432009-05-10 15:41:25 -0500890
891 switch (chip->ecc.mode) {
892 case NAND_ECC_NONE:
893 dev_info(info->device, "NAND ECC disabled\n");
894 break;
895 case NAND_ECC_SOFT:
896 dev_info(info->device, "NAND soft ECC\n");
897 break;
898 case NAND_ECC_HW:
899 dev_info(info->device, "NAND hardware ECC\n");
900 break;
901 default:
902 dev_info(info->device, "NAND ECC UNKNOWN\n");
903 break;
904 }
Michel Pollet9db41f92009-05-13 16:54:14 +0100905
906 /* If you use u-boot BBT creation code, specifying this flag will
907 * let the kernel fish out the BBT from the NAND, and also skip the
908 * full NAND scan that can take 1/2s or so. Little things... */
Brian Norrisa40f7342011-05-31 16:31:22 -0700909 if (set->flash_bbt) {
Brian Norrisbb9ebd42011-05-31 16:31:23 -0700910 chip->bbt_options |= NAND_BBT_USE_FLASH;
Brian Norrisa40f7342011-05-31 16:31:22 -0700911 chip->options |= NAND_SKIP_BBTSCAN;
912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914
Ben Dooks3db72152009-05-30 17:18:15 +0100915/**
916 * s3c2410_nand_update_chip - post probe update
917 * @info: The controller instance.
918 * @nmtd: The driver version of the MTD instance.
Ben Dooks71d54f32008-04-15 11:36:19 +0100919 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200920 * This routine is called after the chip probe has successfully completed
Ben Dooks3db72152009-05-30 17:18:15 +0100921 * and the relevant per-chip information updated. This call ensure that
922 * we update the internal state accordingly.
923 *
924 * The internal state is currently limited to the ECC state information.
925*/
Ben Dooks71d54f32008-04-15 11:36:19 +0100926static void s3c2410_nand_update_chip(struct s3c2410_nand_info *info,
927 struct s3c2410_nand_mtd *nmtd)
928{
929 struct nand_chip *chip = &nmtd->chip;
930
Ben Dooks451d3392008-05-20 17:32:14 +0100931 dev_dbg(info->device, "chip %p => page shift %d\n",
932 chip, chip->page_shift);
Ben Dooks71d54f32008-04-15 11:36:19 +0100933
Andy Green8c3e8432009-05-10 15:41:25 -0500934 if (chip->ecc.mode != NAND_ECC_HW)
935 return;
936
Adam Buchbinder48fc7f72012-09-19 21:48:00 -0400937 /* change the behaviour depending on whether we are using
Ben Dooks71d54f32008-04-15 11:36:19 +0100938 * the large or small page nand device */
939
Andy Green8c3e8432009-05-10 15:41:25 -0500940 if (chip->page_shift > 10) {
941 chip->ecc.size = 256;
942 chip->ecc.bytes = 3;
943 } else {
944 chip->ecc.size = 512;
945 chip->ecc.bytes = 3;
Boris Brezillonbf01e06b2016-02-03 20:03:30 +0100946 mtd_set_ooblayout(nand_to_mtd(chip), &s3c2410_ooblayout_ops);
Ben Dooks71d54f32008-04-15 11:36:19 +0100947 }
948}
949
Ben Dooksec0482e2009-05-30 16:55:29 +0100950/* s3c24xx_nand_probe
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 *
952 * called by device layer when it finds a device matching
953 * one our driver can handled. This code checks to see if
954 * it can allocate all necessary resources then calls the
955 * nand layer to look for devices
956*/
Ben Dooksec0482e2009-05-30 16:55:29 +0100957static int s3c24xx_nand_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
Russell King3ae5eae2005-11-09 22:32:44 +0000959 struct s3c2410_platform_nand *plat = to_nand_plat(pdev);
Sachin Kamat54cd0202012-07-16 16:02:26 +0530960 enum s3c_cpu_type cpu_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 struct s3c2410_nand_info *info;
962 struct s3c2410_nand_mtd *nmtd;
963 struct s3c2410_nand_set *sets;
964 struct resource *res;
965 int err = 0;
966 int size;
967 int nr_sets;
968 int setno;
969
Ben Dooksec0482e2009-05-30 16:55:29 +0100970 cpu_type = platform_get_device_id(pdev)->driver_data;
971
Sachin Kamat6f32a3e2012-08-21 14:24:09 +0530972 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (info == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 err = -ENOMEM;
975 goto exit_error;
976 }
977
Russell King3ae5eae2005-11-09 22:32:44 +0000978 platform_set_drvdata(pdev, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Marc Gonzalezd45bc582016-07-27 11:23:52 +0200980 nand_hw_control_init(&info->controller);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 /* get the clock source and enable it */
983
Sachin Kamat6f32a3e2012-08-21 14:24:09 +0530984 info->clk = devm_clk_get(&pdev->dev, "nand");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 if (IS_ERR(info->clk)) {
Joe Perches898eb712007-10-18 03:06:30 -0700986 dev_err(&pdev->dev, "failed to get clock\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 err = -ENOENT;
988 goto exit_error;
989 }
990
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200991 s3c2410_nand_clk_set_state(info, CLOCK_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993 /* allocate and map the resource */
994
Ben Dooksa4f957f2005-06-20 12:48:25 +0100995 /* currently we assume we have the one resource */
Sachin Kamat6f32a3e2012-08-21 14:24:09 +0530996 res = pdev->resource;
H Hartley Sweetenfc161c42009-12-14 16:56:22 -0500997 size = resource_size(res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Sachin Kamat6f32a3e2012-08-21 14:24:09 +0530999 info->device = &pdev->dev;
1000 info->platform = plat;
1001 info->cpu_type = cpu_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Thierry Redingb0de7742013-01-21 11:09:12 +01001003 info->regs = devm_ioremap_resource(&pdev->dev, res);
1004 if (IS_ERR(info->regs)) {
1005 err = PTR_ERR(info->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 goto exit_error;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Russell King3ae5eae2005-11-09 22:32:44 +00001009 dev_dbg(&pdev->dev, "mapped registers at %p\n", info->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 /* initialise the hardware */
1012
Ben Dooks30821fe2008-07-15 11:58:31 +01001013 err = s3c2410_nand_inithw(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 if (err != 0)
1015 goto exit_error;
1016
1017 sets = (plat != NULL) ? plat->sets : NULL;
1018 nr_sets = (plat != NULL) ? plat->nr_sets : 1;
1019
1020 info->mtd_count = nr_sets;
1021
1022 /* allocate our information */
1023
1024 size = nr_sets * sizeof(*info->mtds);
Sachin Kamat6f32a3e2012-08-21 14:24:09 +05301025 info->mtds = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 if (info->mtds == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 err = -ENOMEM;
1028 goto exit_error;
1029 }
1030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 /* initialise all possible chips */
1032
1033 nmtd = info->mtds;
1034
1035 for (setno = 0; setno < nr_sets; setno++, nmtd++) {
Boris BREZILLON7208b992015-12-10 09:00:22 +01001036 struct mtd_info *mtd = nand_to_mtd(&nmtd->chip);
1037
Sachin Kamatf938bc52012-08-21 10:21:15 +05301038 pr_debug("initialising set %d (%p, info %p)\n",
1039 setno, nmtd, info);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001040
Boris BREZILLON7208b992015-12-10 09:00:22 +01001041 mtd->dev.parent = &pdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 s3c2410_nand_init_chip(info, nmtd, sets);
1043
Boris BREZILLON7208b992015-12-10 09:00:22 +01001044 nmtd->scan_res = nand_scan_ident(mtd,
David Woodhouse5e81e882010-02-26 18:32:56 +00001045 (sets) ? sets->nr_chips : 1,
1046 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
1048 if (nmtd->scan_res == 0) {
Ben Dooks71d54f32008-04-15 11:36:19 +01001049 s3c2410_nand_update_chip(info, nmtd);
Boris BREZILLON7208b992015-12-10 09:00:22 +01001050 nand_scan_tail(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 s3c2410_nand_add_partition(info, nmtd, sets);
1052 }
1053
1054 if (sets != NULL)
1055 sets++;
1056 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001057
Ben Dooks30821fe2008-07-15 11:58:31 +01001058 err = s3c2410_nand_cpufreq_register(info);
1059 if (err < 0) {
1060 dev_err(&pdev->dev, "failed to init cpufreq support\n");
1061 goto exit_error;
1062 }
1063
Jiri Pinkavaac497c12011-04-13 11:59:30 +02001064 if (allow_clk_suspend(info)) {
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001065 dev_info(&pdev->dev, "clock idle support enabled\n");
Jiri Pinkavaac497c12011-04-13 11:59:30 +02001066 s3c2410_nand_clk_set_state(info, CLOCK_SUSPEND);
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001067 }
1068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 return 0;
1070
1071 exit_error:
Ben Dooksec0482e2009-05-30 16:55:29 +01001072 s3c24xx_nand_remove(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074 if (err == 0)
1075 err = -EINVAL;
1076 return err;
1077}
1078
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001079/* PM Support */
1080#ifdef CONFIG_PM
1081
1082static int s3c24xx_nand_suspend(struct platform_device *dev, pm_message_t pm)
1083{
1084 struct s3c2410_nand_info *info = platform_get_drvdata(dev);
1085
1086 if (info) {
Ben Dooks09160832008-04-15 11:36:18 +01001087 info->save_sel = readl(info->sel_reg);
Ben Dooks03680b12007-11-19 23:28:07 +00001088
1089 /* For the moment, we must ensure nFCE is high during
1090 * the time we are suspended. This really should be
1091 * handled by suspending the MTDs we are using, but
1092 * that is currently not the case. */
1093
Ben Dooks09160832008-04-15 11:36:18 +01001094 writel(info->save_sel | info->sel_bit, info->sel_reg);
Ben Dooks03680b12007-11-19 23:28:07 +00001095
Jiri Pinkavaac497c12011-04-13 11:59:30 +02001096 s3c2410_nand_clk_set_state(info, CLOCK_DISABLE);
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001097 }
1098
1099 return 0;
1100}
1101
1102static int s3c24xx_nand_resume(struct platform_device *dev)
1103{
1104 struct s3c2410_nand_info *info = platform_get_drvdata(dev);
Ben Dooks09160832008-04-15 11:36:18 +01001105 unsigned long sel;
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001106
1107 if (info) {
Jiri Pinkavaac497c12011-04-13 11:59:30 +02001108 s3c2410_nand_clk_set_state(info, CLOCK_ENABLE);
Ben Dooks30821fe2008-07-15 11:58:31 +01001109 s3c2410_nand_inithw(info);
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001110
Ben Dooks03680b12007-11-19 23:28:07 +00001111 /* Restore the state of the nFCE line. */
1112
Ben Dooks09160832008-04-15 11:36:18 +01001113 sel = readl(info->sel_reg);
1114 sel &= ~info->sel_bit;
1115 sel |= info->save_sel & info->sel_bit;
1116 writel(sel, info->sel_reg);
Ben Dooks03680b12007-11-19 23:28:07 +00001117
Jiri Pinkavaac497c12011-04-13 11:59:30 +02001118 s3c2410_nand_clk_set_state(info, CLOCK_SUSPEND);
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001119 }
1120
1121 return 0;
1122}
1123
1124#else
1125#define s3c24xx_nand_suspend NULL
1126#define s3c24xx_nand_resume NULL
1127#endif
1128
Ben Dooksa4f957f2005-06-20 12:48:25 +01001129/* driver device registration */
1130
Krzysztof Kozlowski0abe75d2015-05-02 00:50:02 +09001131static const struct platform_device_id s3c24xx_driver_ids[] = {
Ben Dooksec0482e2009-05-30 16:55:29 +01001132 {
1133 .name = "s3c2410-nand",
1134 .driver_data = TYPE_S3C2410,
1135 }, {
1136 .name = "s3c2440-nand",
1137 .driver_data = TYPE_S3C2440,
1138 }, {
1139 .name = "s3c2412-nand",
1140 .driver_data = TYPE_S3C2412,
Peter Korsgaard9dbc0902009-06-07 06:04:23 -07001141 }, {
1142 .name = "s3c6400-nand",
1143 .driver_data = TYPE_S3C2412, /* compatible with 2412 */
Russell King3ae5eae2005-11-09 22:32:44 +00001144 },
Ben Dooksec0482e2009-05-30 16:55:29 +01001145 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146};
1147
Ben Dooksec0482e2009-05-30 16:55:29 +01001148MODULE_DEVICE_TABLE(platform, s3c24xx_driver_ids);
Ben Dooksa4f957f2005-06-20 12:48:25 +01001149
Ben Dooksec0482e2009-05-30 16:55:29 +01001150static struct platform_driver s3c24xx_nand_driver = {
1151 .probe = s3c24xx_nand_probe,
1152 .remove = s3c24xx_nand_remove,
Ben Dooks2c06a082006-06-27 14:35:46 +01001153 .suspend = s3c24xx_nand_suspend,
1154 .resume = s3c24xx_nand_resume,
Ben Dooksec0482e2009-05-30 16:55:29 +01001155 .id_table = s3c24xx_driver_ids,
Ben Dooks2c06a082006-06-27 14:35:46 +01001156 .driver = {
Ben Dooksec0482e2009-05-30 16:55:29 +01001157 .name = "s3c24xx-nand",
Ben Dooks2c06a082006-06-27 14:35:46 +01001158 },
1159};
1160
Sachin Kamat056fcab2012-07-16 16:02:22 +05301161module_platform_driver(s3c24xx_nand_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
1163MODULE_LICENSE("GPL");
1164MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
Ben Dooksa4f957f2005-06-20 12:48:25 +01001165MODULE_DESCRIPTION("S3C24XX MTD NAND driver");