blob: 86ffb73e10853b9ac22dfce93bb5a85984b2cfdf [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 */
87
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020088static struct nand_ecclayout nand_hw_eccoob = {
David Woodhousee0c7d762006-05-13 18:07:53 +010089 .eccbytes = 3,
90 .eccpos = {0, 1, 2},
91 .oobfree = {{8, 8}}
Linus Torvalds1da177e2005-04-16 15:20:36 -070092};
93
94/* controller and mtd information */
95
96struct s3c2410_nand_info;
97
Ben Dooks3db72152009-05-30 17:18:15 +010098/**
99 * struct s3c2410_nand_mtd - driver MTD structure
100 * @mtd: The MTD instance to pass to the MTD layer.
101 * @chip: The NAND chip information.
102 * @set: The platform information supplied for this set of NAND chips.
103 * @info: Link back to the hardware information.
104 * @scan_res: The result from calling nand_scan_ident().
105*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106struct s3c2410_nand_mtd {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 struct nand_chip chip;
108 struct s3c2410_nand_set *set;
109 struct s3c2410_nand_info *info;
110 int scan_res;
111};
112
Ben Dooks2c06a082006-06-27 14:35:46 +0100113enum s3c_cpu_type {
114 TYPE_S3C2410,
115 TYPE_S3C2412,
116 TYPE_S3C2440,
117};
118
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200119enum s3c_nand_clk_state {
120 CLOCK_DISABLE = 0,
121 CLOCK_ENABLE,
122 CLOCK_SUSPEND,
123};
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125/* overview of the s3c2410 nand state */
126
Ben Dooks3db72152009-05-30 17:18:15 +0100127/**
128 * struct s3c2410_nand_info - NAND controller state.
129 * @mtds: An array of MTD instances on this controoler.
130 * @platform: The platform data for this board.
131 * @device: The platform device we bound to.
Ben Dooks3db72152009-05-30 17:18:15 +0100132 * @clk: The clock resource for this controller.
Sachin Kamat6f32a3e2012-08-21 14:24:09 +0530133 * @regs: The area mapped for the hardware registers.
Ben Dooks3db72152009-05-30 17:18:15 +0100134 * @sel_reg: Pointer to the register controlling the NAND selection.
135 * @sel_bit: The bit in @sel_reg to select the NAND chip.
136 * @mtd_count: The number of MTDs created from this controller.
137 * @save_sel: The contents of @sel_reg to be saved over suspend.
138 * @clk_rate: The clock rate from @clk.
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200139 * @clk_state: The current clock state.
Ben Dooks3db72152009-05-30 17:18:15 +0100140 * @cpu_type: The exact type of this controller.
141 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142struct s3c2410_nand_info {
143 /* mtd info */
144 struct nand_hw_control controller;
145 struct s3c2410_nand_mtd *mtds;
146 struct s3c2410_platform_nand *platform;
147
148 /* device info */
149 struct device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 struct clk *clk;
Ben Dooksfdf2fd52005-02-18 14:46:15 +0000151 void __iomem *regs;
Ben Dooks2c06a082006-06-27 14:35:46 +0100152 void __iomem *sel_reg;
153 int sel_bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 int mtd_count;
Ben Dooks09160832008-04-15 11:36:18 +0100155 unsigned long save_sel;
Ben Dooks30821fe2008-07-15 11:58:31 +0100156 unsigned long clk_rate;
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200157 enum s3c_nand_clk_state clk_state;
Ben Dooks03680b12007-11-19 23:28:07 +0000158
Ben Dooks2c06a082006-06-27 14:35:46 +0100159 enum s3c_cpu_type cpu_type;
Ben Dooks30821fe2008-07-15 11:58:31 +0100160
161#ifdef CONFIG_CPU_FREQ
162 struct notifier_block freq_transition;
163#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164};
165
166/* conversion functions */
167
168static struct s3c2410_nand_mtd *s3c2410_nand_mtd_toours(struct mtd_info *mtd)
169{
Boris BREZILLON7208b992015-12-10 09:00:22 +0100170 return container_of(mtd_to_nand(mtd), struct s3c2410_nand_mtd,
171 chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
174static struct s3c2410_nand_info *s3c2410_nand_mtd_toinfo(struct mtd_info *mtd)
175{
176 return s3c2410_nand_mtd_toours(mtd)->info;
177}
178
Russell King3ae5eae2005-11-09 22:32:44 +0000179static struct s3c2410_nand_info *to_nand_info(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Russell King3ae5eae2005-11-09 22:32:44 +0000181 return platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
Russell King3ae5eae2005-11-09 22:32:44 +0000184static struct s3c2410_platform_nand *to_nand_plat(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Jingoo Han453810b2013-07-30 17:18:33 +0900186 return dev_get_platdata(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200189static inline int allow_clk_suspend(struct s3c2410_nand_info *info)
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100190{
Sachin Kamata68c5ec2012-07-16 16:02:25 +0530191#ifdef CONFIG_MTD_NAND_S3C2410_CLKSTOP
192 return 1;
193#else
194 return 0;
195#endif
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100196}
197
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200198/**
199 * s3c2410_nand_clk_set_state - Enable, disable or suspend NAND clock.
200 * @info: The controller instance.
201 * @new_state: State to which clock should be set.
202 */
203static void s3c2410_nand_clk_set_state(struct s3c2410_nand_info *info,
204 enum s3c_nand_clk_state new_state)
205{
206 if (!allow_clk_suspend(info) && new_state == CLOCK_SUSPEND)
207 return;
208
209 if (info->clk_state == CLOCK_ENABLE) {
210 if (new_state != CLOCK_ENABLE)
Vasily Khoruzhick887957b2014-06-30 22:12:16 +0300211 clk_disable_unprepare(info->clk);
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200212 } else {
213 if (new_state == CLOCK_ENABLE)
Vasily Khoruzhick887957b2014-06-30 22:12:16 +0300214 clk_prepare_enable(info->clk);
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200215 }
216
217 info->clk_state = new_state;
218}
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220/* timing calculations */
221
Ben Dookscfd320f2005-10-20 22:22:58 +0100222#define NS_IN_KHZ 1000000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Ben Dooks3db72152009-05-30 17:18:15 +0100224/**
225 * s3c_nand_calc_rate - calculate timing data.
226 * @wanted: The cycle time in nanoseconds.
227 * @clk: The clock rate in kHz.
228 * @max: The maximum divider value.
229 *
230 * Calculate the timing value from the given parameters.
231 */
Ben Dooks2c06a082006-06-27 14:35:46 +0100232static int s3c_nand_calc_rate(int wanted, unsigned long clk, int max)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
234 int result;
235
Ben Dooks947391c2009-05-30 18:34:16 +0100236 result = DIV_ROUND_UP((wanted * clk), NS_IN_KHZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 pr_debug("result %d from %ld, %d\n", result, clk, wanted);
239
240 if (result > max) {
Sachin Kamat92aeb5d2012-07-16 16:02:23 +0530241 pr_err("%d ns is too big for current clock rate %ld\n",
242 wanted, clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return -1;
244 }
245
246 if (result < 1)
247 result = 1;
248
249 return result;
250}
251
Sachin Kamat54cd0202012-07-16 16:02:26 +0530252#define to_ns(ticks, clk) (((ticks) * NS_IN_KHZ) / (unsigned int)(clk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254/* controller setup */
255
Ben Dooks3db72152009-05-30 17:18:15 +0100256/**
257 * s3c2410_nand_setrate - setup controller timing information.
258 * @info: The controller instance.
259 *
260 * Given the information supplied by the platform, calculate and set
261 * the necessary timing registers in the hardware to generate the
262 * necessary timing cycles to the hardware.
263 */
Ben Dooks30821fe2008-07-15 11:58:31 +0100264static int s3c2410_nand_setrate(struct s3c2410_nand_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Ben Dooks30821fe2008-07-15 11:58:31 +0100266 struct s3c2410_platform_nand *plat = info->platform;
Ben Dooks2c06a082006-06-27 14:35:46 +0100267 int tacls_max = (info->cpu_type == TYPE_S3C2412) ? 8 : 4;
Ben Dookscfd320f2005-10-20 22:22:58 +0100268 int tacls, twrph0, twrph1;
Ben Dooks30821fe2008-07-15 11:58:31 +0100269 unsigned long clkrate = clk_get_rate(info->clk);
Nelson Castillo2612e522009-05-10 15:41:54 -0500270 unsigned long uninitialized_var(set), cfg, uninitialized_var(mask);
Ben Dooks30821fe2008-07-15 11:58:31 +0100271 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 /* calculate the timing information for the controller */
274
Ben Dooks30821fe2008-07-15 11:58:31 +0100275 info->clk_rate = clkrate;
Ben Dookscfd320f2005-10-20 22:22:58 +0100276 clkrate /= 1000; /* turn clock into kHz for ease of use */
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (plat != NULL) {
Ben Dooks2c06a082006-06-27 14:35:46 +0100279 tacls = s3c_nand_calc_rate(plat->tacls, clkrate, tacls_max);
280 twrph0 = s3c_nand_calc_rate(plat->twrph0, clkrate, 8);
281 twrph1 = s3c_nand_calc_rate(plat->twrph1, clkrate, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 } else {
283 /* default timings */
Ben Dooks2c06a082006-06-27 14:35:46 +0100284 tacls = tacls_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 twrph0 = 8;
286 twrph1 = 8;
287 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 if (tacls < 0 || twrph0 < 0 || twrph1 < 0) {
Ben Dooks99974c62006-06-21 15:43:05 +0100290 dev_err(info->device, "cannot get suitable timings\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return -EINVAL;
292 }
293
Ben Dooks99974c62006-06-21 15:43:05 +0100294 dev_info(info->device, "Tacls=%d, %dns Twrph0=%d %dns, Twrph1=%d %dns\n",
Sachin Kamat54cd0202012-07-16 16:02:26 +0530295 tacls, to_ns(tacls, clkrate), twrph0, to_ns(twrph0, clkrate),
296 twrph1, to_ns(twrph1, clkrate));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Ben Dooks30821fe2008-07-15 11:58:31 +0100298 switch (info->cpu_type) {
299 case TYPE_S3C2410:
300 mask = (S3C2410_NFCONF_TACLS(3) |
301 S3C2410_NFCONF_TWRPH0(7) |
302 S3C2410_NFCONF_TWRPH1(7));
303 set = S3C2410_NFCONF_EN;
304 set |= S3C2410_NFCONF_TACLS(tacls - 1);
305 set |= S3C2410_NFCONF_TWRPH0(twrph0 - 1);
306 set |= S3C2410_NFCONF_TWRPH1(twrph1 - 1);
307 break;
308
309 case TYPE_S3C2440:
310 case TYPE_S3C2412:
Peter Korsgaarda755a382009-06-03 13:46:54 +0200311 mask = (S3C2440_NFCONF_TACLS(tacls_max - 1) |
312 S3C2440_NFCONF_TWRPH0(7) |
313 S3C2440_NFCONF_TWRPH1(7));
Ben Dooks30821fe2008-07-15 11:58:31 +0100314
315 set = S3C2440_NFCONF_TACLS(tacls - 1);
316 set |= S3C2440_NFCONF_TWRPH0(twrph0 - 1);
317 set |= S3C2440_NFCONF_TWRPH1(twrph1 - 1);
318 break;
319
320 default:
Ben Dooks30821fe2008-07-15 11:58:31 +0100321 BUG();
322 }
323
Ben Dooks30821fe2008-07-15 11:58:31 +0100324 local_irq_save(flags);
325
326 cfg = readl(info->regs + S3C2410_NFCONF);
327 cfg &= ~mask;
328 cfg |= set;
329 writel(cfg, info->regs + S3C2410_NFCONF);
330
331 local_irq_restore(flags);
332
Andy Greenae7304e2009-05-10 15:42:02 -0500333 dev_dbg(info->device, "NF_CONF is 0x%lx\n", cfg);
334
Ben Dooks30821fe2008-07-15 11:58:31 +0100335 return 0;
336}
337
Ben Dooks3db72152009-05-30 17:18:15 +0100338/**
339 * s3c2410_nand_inithw - basic hardware initialisation
340 * @info: The hardware state.
341 *
342 * Do the basic initialisation of the hardware, using s3c2410_nand_setrate()
343 * to setup the hardware access speeds and set the controller to be enabled.
344*/
Ben Dooks30821fe2008-07-15 11:58:31 +0100345static int s3c2410_nand_inithw(struct s3c2410_nand_info *info)
346{
347 int ret;
348
349 ret = s3c2410_nand_setrate(info);
350 if (ret < 0)
351 return ret;
352
Sachin Kamat54cd0202012-07-16 16:02:26 +0530353 switch (info->cpu_type) {
354 case TYPE_S3C2410:
Ben Dooks30821fe2008-07-15 11:58:31 +0100355 default:
Ben Dooks2c06a082006-06-27 14:35:46 +0100356 break;
357
Sachin Kamat54cd0202012-07-16 16:02:26 +0530358 case TYPE_S3C2440:
359 case TYPE_S3C2412:
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100360 /* enable the controller and de-assert nFCE */
361
Ben Dooks2c06a082006-06-27 14:35:46 +0100362 writel(S3C2440_NFCONT_ENABLE, info->regs + S3C2440_NFCONT);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return 0;
366}
367
Ben Dooks3db72152009-05-30 17:18:15 +0100368/**
369 * s3c2410_nand_select_chip - select the given nand chip
370 * @mtd: The MTD instance for this chip.
371 * @chip: The chip number.
372 *
373 * This is called by the MTD layer to either select a given chip for the
374 * @mtd instance, or to indicate that the access has finished and the
375 * chip can be de-selected.
376 *
377 * The routine ensures that the nFCE line is correctly setup, and any
378 * platform specific selection code is called to route nFCE to the specific
379 * chip.
380 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381static void s3c2410_nand_select_chip(struct mtd_info *mtd, int chip)
382{
383 struct s3c2410_nand_info *info;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000384 struct s3c2410_nand_mtd *nmtd;
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100385 struct nand_chip *this = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 unsigned long cur;
387
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100388 nmtd = nand_get_controller_data(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 info = nmtd->info;
390
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200391 if (chip != -1)
392 s3c2410_nand_clk_set_state(info, CLOCK_ENABLE);
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100393
Ben Dooks2c06a082006-06-27 14:35:46 +0100394 cur = readl(info->sel_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 if (chip == -1) {
Ben Dooks2c06a082006-06-27 14:35:46 +0100397 cur |= info->sel_bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 } else {
Ben Dooksfb8d82a2005-07-06 21:05:10 +0100399 if (nmtd->set != NULL && chip > nmtd->set->nr_chips) {
Ben Dooks99974c62006-06-21 15:43:05 +0100400 dev_err(info->device, "invalid chip %d\n", chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return;
402 }
403
404 if (info->platform != NULL) {
405 if (info->platform->select_chip != NULL)
David Woodhousee0c7d762006-05-13 18:07:53 +0100406 (info->platform->select_chip) (nmtd->set, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
408
Ben Dooks2c06a082006-06-27 14:35:46 +0100409 cur &= ~info->sel_bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
411
Ben Dooks2c06a082006-06-27 14:35:46 +0100412 writel(cur, info->sel_reg);
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100413
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200414 if (chip == -1)
415 s3c2410_nand_clk_set_state(info, CLOCK_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
417
Ben Dooksad3b5fb2006-06-19 09:43:23 +0100418/* s3c2410_nand_hwcontrol
Ben Dooksa4f957f2005-06-20 12:48:25 +0100419 *
Ben Dooksad3b5fb2006-06-19 09:43:23 +0100420 * Issue command and address cycles to the chip
Ben Dooksa4f957f2005-06-20 12:48:25 +0100421*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200423static void s3c2410_nand_hwcontrol(struct mtd_info *mtd, int cmd,
David Woodhousef9068872006-06-10 00:53:16 +0100424 unsigned int ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
426 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
David Woodhousec9ac59772006-11-30 08:17:38 +0000427
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200428 if (cmd == NAND_CMD_NONE)
429 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
David Woodhousef9068872006-06-10 00:53:16 +0100431 if (ctrl & NAND_CLE)
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200432 writeb(cmd, info->regs + S3C2410_NFCMD);
433 else
434 writeb(cmd, info->regs + S3C2410_NFADDR);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100435}
436
437/* command and control functions */
438
David Woodhousef9068872006-06-10 00:53:16 +0100439static void s3c2440_nand_hwcontrol(struct mtd_info *mtd, int cmd,
440 unsigned int ctrl)
Ben Dooksa4f957f2005-06-20 12:48:25 +0100441{
442 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100443
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200444 if (cmd == NAND_CMD_NONE)
445 return;
Ben Dooksa4f957f2005-06-20 12:48:25 +0100446
David Woodhousef9068872006-06-10 00:53:16 +0100447 if (ctrl & NAND_CLE)
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200448 writeb(cmd, info->regs + S3C2440_NFCMD);
449 else
450 writeb(cmd, info->regs + S3C2440_NFADDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453/* s3c2410_nand_devready()
454 *
455 * returns 0 if the nand is busy, 1 if it is ready
456*/
457
458static int s3c2410_nand_devready(struct mtd_info *mtd)
459{
460 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 return readb(info->regs + S3C2410_NFSTAT) & S3C2410_NFSTAT_BUSY;
462}
463
Ben Dooks2c06a082006-06-27 14:35:46 +0100464static int s3c2440_nand_devready(struct mtd_info *mtd)
465{
466 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
467 return readb(info->regs + S3C2440_NFSTAT) & S3C2440_NFSTAT_READY;
468}
469
470static int s3c2412_nand_devready(struct mtd_info *mtd)
471{
472 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
473 return readb(info->regs + S3C2412_NFSTAT) & S3C2412_NFSTAT_READY;
474}
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476/* ECC handling functions */
477
Sachin Kamat19da4152012-08-21 14:24:10 +0530478#ifdef CONFIG_MTD_NAND_S3C2410_HWECC
Ben Dooks2c06a082006-06-27 14:35:46 +0100479static int s3c2410_nand_correct_data(struct mtd_info *mtd, u_char *dat,
480 u_char *read_ecc, u_char *calc_ecc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Ben Dooksa2593242007-02-02 16:59:33 +0000482 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
483 unsigned int diff0, diff1, diff2;
484 unsigned int bit, byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Ben Dooksa2593242007-02-02 16:59:33 +0000486 pr_debug("%s(%p,%p,%p,%p)\n", __func__, mtd, dat, read_ecc, calc_ecc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Ben Dooksa2593242007-02-02 16:59:33 +0000488 diff0 = read_ecc[0] ^ calc_ecc[0];
489 diff1 = read_ecc[1] ^ calc_ecc[1];
490 diff2 = read_ecc[2] ^ calc_ecc[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Andy Shevchenko13e85972012-08-02 16:06:47 +0300492 pr_debug("%s: rd %*phN calc %*phN diff %02x%02x%02x\n",
493 __func__, 3, read_ecc, 3, calc_ecc,
Ben Dooksa2593242007-02-02 16:59:33 +0000494 diff0, diff1, diff2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Ben Dooksa2593242007-02-02 16:59:33 +0000496 if (diff0 == 0 && diff1 == 0 && diff2 == 0)
497 return 0; /* ECC is ok */
498
Ben Dooksc45c6c62008-04-15 11:36:20 +0100499 /* sometimes people do not think about using the ECC, so check
500 * to see if we have an 0xff,0xff,0xff read ECC and then ignore
501 * the error, on the assumption that this is an un-eccd page.
502 */
503 if (read_ecc[0] == 0xff && read_ecc[1] == 0xff && read_ecc[2] == 0xff
504 && info->platform->ignore_unset_ecc)
505 return 0;
506
Ben Dooksa2593242007-02-02 16:59:33 +0000507 /* Can we correct this ECC (ie, one row and column change).
508 * Note, this is similar to the 256 error code on smartmedia */
509
510 if (((diff0 ^ (diff0 >> 1)) & 0x55) == 0x55 &&
511 ((diff1 ^ (diff1 >> 1)) & 0x55) == 0x55 &&
512 ((diff2 ^ (diff2 >> 1)) & 0x55) == 0x55) {
513 /* calculate the bit position of the error */
514
Matt Reimerd0bf3792007-10-18 18:02:43 -0700515 bit = ((diff2 >> 3) & 1) |
516 ((diff2 >> 4) & 2) |
517 ((diff2 >> 5) & 4);
Ben Dooksa2593242007-02-02 16:59:33 +0000518
519 /* calculate the byte position of the error */
520
Matt Reimerd0bf3792007-10-18 18:02:43 -0700521 byte = ((diff2 << 7) & 0x100) |
522 ((diff1 << 0) & 0x80) |
523 ((diff1 << 1) & 0x40) |
524 ((diff1 << 2) & 0x20) |
525 ((diff1 << 3) & 0x10) |
526 ((diff0 >> 4) & 0x08) |
527 ((diff0 >> 3) & 0x04) |
528 ((diff0 >> 2) & 0x02) |
529 ((diff0 >> 1) & 0x01);
Ben Dooksa2593242007-02-02 16:59:33 +0000530
531 dev_dbg(info->device, "correcting error bit %d, byte %d\n",
532 bit, byte);
533
534 dat[byte] ^= (1 << bit);
535 return 1;
536 }
537
538 /* if there is only one bit difference in the ECC, then
539 * one of only a row or column parity has changed, which
540 * means the error is most probably in the ECC itself */
541
542 diff0 |= (diff1 << 8);
543 diff0 |= (diff2 << 16);
544
Zhaoxiu Zeng03a97552016-04-12 15:30:35 +0800545 /* equal to "(diff0 & ~(1 << __ffs(diff0)))" */
546 if ((diff0 & (diff0 - 1)) == 0)
Ben Dooksa2593242007-02-02 16:59:33 +0000547 return 1;
548
Matt Reimer4fac9f62007-10-18 18:02:44 -0700549 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
Ben Dooksa4f957f2005-06-20 12:48:25 +0100552/* ECC functions
553 *
554 * These allow the s3c2410 and s3c2440 to use the controller's ECC
555 * generator block to ECC the data as it passes through]
556*/
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558static void s3c2410_nand_enable_hwecc(struct mtd_info *mtd, int mode)
559{
560 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
561 unsigned long ctrl;
562
563 ctrl = readl(info->regs + S3C2410_NFCONF);
564 ctrl |= S3C2410_NFCONF_INITECC;
565 writel(ctrl, info->regs + S3C2410_NFCONF);
566}
567
Matthieu CASTET4f659922007-02-13 12:30:38 +0100568static void s3c2412_nand_enable_hwecc(struct mtd_info *mtd, int mode)
569{
570 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
571 unsigned long ctrl;
572
573 ctrl = readl(info->regs + S3C2440_NFCONT);
Sachin Kamatf938bc52012-08-21 10:21:15 +0530574 writel(ctrl | S3C2412_NFCONT_INIT_MAIN_ECC,
575 info->regs + S3C2440_NFCONT);
Matthieu CASTET4f659922007-02-13 12:30:38 +0100576}
577
Ben Dooksa4f957f2005-06-20 12:48:25 +0100578static void s3c2440_nand_enable_hwecc(struct mtd_info *mtd, int mode)
579{
580 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
581 unsigned long ctrl;
582
583 ctrl = readl(info->regs + S3C2440_NFCONT);
584 writel(ctrl | S3C2440_NFCONT_INITECC, info->regs + S3C2440_NFCONT);
585}
586
Sachin Kamatf938bc52012-08-21 10:21:15 +0530587static int s3c2410_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
588 u_char *ecc_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
590 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
591
592 ecc_code[0] = readb(info->regs + S3C2410_NFECC + 0);
593 ecc_code[1] = readb(info->regs + S3C2410_NFECC + 1);
594 ecc_code[2] = readb(info->regs + S3C2410_NFECC + 2);
595
Andy Shevchenko13e85972012-08-02 16:06:47 +0300596 pr_debug("%s: returning ecc %*phN\n", __func__, 3, ecc_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 return 0;
599}
600
Sachin Kamatf938bc52012-08-21 10:21:15 +0530601static int s3c2412_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
602 u_char *ecc_code)
Matthieu CASTET4f659922007-02-13 12:30:38 +0100603{
604 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
605 unsigned long ecc = readl(info->regs + S3C2412_NFMECC0);
606
607 ecc_code[0] = ecc;
608 ecc_code[1] = ecc >> 8;
609 ecc_code[2] = ecc >> 16;
610
Andy Shevchenko13e85972012-08-02 16:06:47 +0300611 pr_debug("%s: returning ecc %*phN\n", __func__, 3, ecc_code);
Matthieu CASTET4f659922007-02-13 12:30:38 +0100612
613 return 0;
614}
615
Sachin Kamatf938bc52012-08-21 10:21:15 +0530616static int s3c2440_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
617 u_char *ecc_code)
Ben Dooksa4f957f2005-06-20 12:48:25 +0100618{
619 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
620 unsigned long ecc = readl(info->regs + S3C2440_NFMECC0);
621
622 ecc_code[0] = ecc;
623 ecc_code[1] = ecc >> 8;
624 ecc_code[2] = ecc >> 16;
625
Ben Dooks71d54f32008-04-15 11:36:19 +0100626 pr_debug("%s: returning ecc %06lx\n", __func__, ecc & 0xffffff);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100627
628 return 0;
629}
Sachin Kamat19da4152012-08-21 14:24:10 +0530630#endif
Ben Dooksa4f957f2005-06-20 12:48:25 +0100631
Ben Dooksa4f957f2005-06-20 12:48:25 +0100632/* over-ride the standard functions for a little more speed. We can
633 * use read/write block to move the data buffers to/from the controller
634*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636static void s3c2410_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
637{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100638 struct nand_chip *this = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 readsb(this->IO_ADDR_R, buf, len);
640}
641
Matt Reimerb773bb22007-10-18 17:43:07 -0700642static void s3c2440_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
643{
644 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
Ben Dooksdea2aa62009-05-30 18:30:18 +0100645
646 readsl(info->regs + S3C2440_NFDATA, buf, len >> 2);
647
648 /* cleanup if we've got less than a word to do */
649 if (len & 3) {
650 buf += len & ~3;
651
652 for (; len & 3; len--)
653 *buf++ = readb(info->regs + S3C2440_NFDATA);
654 }
Matt Reimerb773bb22007-10-18 17:43:07 -0700655}
656
Sachin Kamatf938bc52012-08-21 10:21:15 +0530657static void s3c2410_nand_write_buf(struct mtd_info *mtd, const u_char *buf,
658 int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100660 struct nand_chip *this = mtd_to_nand(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 writesb(this->IO_ADDR_W, buf, len);
662}
663
Sachin Kamatf938bc52012-08-21 10:21:15 +0530664static void s3c2440_nand_write_buf(struct mtd_info *mtd, const u_char *buf,
665 int len)
Matt Reimerb773bb22007-10-18 17:43:07 -0700666{
667 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
Ben Dooksdea2aa62009-05-30 18:30:18 +0100668
669 writesl(info->regs + S3C2440_NFDATA, buf, len >> 2);
670
671 /* cleanup any fractional write */
672 if (len & 3) {
673 buf += len & ~3;
674
675 for (; len & 3; len--, buf++)
676 writeb(*buf, info->regs + S3C2440_NFDATA);
677 }
Matt Reimerb773bb22007-10-18 17:43:07 -0700678}
679
Ben Dooks30821fe2008-07-15 11:58:31 +0100680/* cpufreq driver support */
681
682#ifdef CONFIG_CPU_FREQ
683
684static int s3c2410_nand_cpufreq_transition(struct notifier_block *nb,
685 unsigned long val, void *data)
686{
687 struct s3c2410_nand_info *info;
688 unsigned long newclk;
689
690 info = container_of(nb, struct s3c2410_nand_info, freq_transition);
691 newclk = clk_get_rate(info->clk);
692
693 if ((val == CPUFREQ_POSTCHANGE && newclk < info->clk_rate) ||
694 (val == CPUFREQ_PRECHANGE && newclk > info->clk_rate)) {
695 s3c2410_nand_setrate(info);
696 }
697
698 return 0;
699}
700
701static inline int s3c2410_nand_cpufreq_register(struct s3c2410_nand_info *info)
702{
703 info->freq_transition.notifier_call = s3c2410_nand_cpufreq_transition;
704
705 return cpufreq_register_notifier(&info->freq_transition,
706 CPUFREQ_TRANSITION_NOTIFIER);
707}
708
Sachin Kamatf938bc52012-08-21 10:21:15 +0530709static inline void
710s3c2410_nand_cpufreq_deregister(struct s3c2410_nand_info *info)
Ben Dooks30821fe2008-07-15 11:58:31 +0100711{
712 cpufreq_unregister_notifier(&info->freq_transition,
713 CPUFREQ_TRANSITION_NOTIFIER);
714}
715
716#else
717static inline int s3c2410_nand_cpufreq_register(struct s3c2410_nand_info *info)
718{
719 return 0;
720}
721
Sachin Kamatf938bc52012-08-21 10:21:15 +0530722static inline void
723s3c2410_nand_cpufreq_deregister(struct s3c2410_nand_info *info)
Ben Dooks30821fe2008-07-15 11:58:31 +0100724{
725}
726#endif
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728/* device management functions */
729
Ben Dooksec0482e2009-05-30 16:55:29 +0100730static int s3c24xx_nand_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
Russell King3ae5eae2005-11-09 22:32:44 +0000732 struct s3c2410_nand_info *info = to_nand_info(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000734 if (info == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return 0;
736
Ben Dooks30821fe2008-07-15 11:58:31 +0100737 s3c2410_nand_cpufreq_deregister(info);
738
739 /* Release all our mtds and their partitions, then go through
740 * freeing the resources used
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 if (info->mtds != NULL) {
744 struct s3c2410_nand_mtd *ptr = info->mtds;
745 int mtdno;
746
747 for (mtdno = 0; mtdno < info->mtd_count; mtdno++, ptr++) {
748 pr_debug("releasing mtd %d (%p)\n", mtdno, ptr);
Boris BREZILLON7208b992015-12-10 09:00:22 +0100749 nand_release(nand_to_mtd(&ptr->chip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
752
753 /* free the common resources */
754
Sachin Kamat6f32a3e2012-08-21 14:24:09 +0530755 if (!IS_ERR(info->clk))
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200756 s3c2410_nand_clk_set_state(info, CLOCK_DISABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 return 0;
759}
760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761static int s3c2410_nand_add_partition(struct s3c2410_nand_info *info,
762 struct s3c2410_nand_mtd *mtd,
763 struct s3c2410_nand_set *set)
764{
Sachin Kamatded4c552012-11-16 16:08:22 +0530765 if (set) {
Boris BREZILLON7208b992015-12-10 09:00:22 +0100766 struct mtd_info *mtdinfo = nand_to_mtd(&mtd->chip);
Andy Greened27f022009-05-10 15:42:09 -0500767
Boris BREZILLON7208b992015-12-10 09:00:22 +0100768 mtdinfo->name = set->name;
769
770 return mtd_device_parse_register(mtdinfo, NULL, NULL,
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +0200771 set->partitions, set->nr_partitions);
Sachin Kamatded4c552012-11-16 16:08:22 +0530772 }
773
774 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Ben Dooks3db72152009-05-30 17:18:15 +0100777/**
778 * s3c2410_nand_init_chip - initialise a single instance of an chip
779 * @info: The base NAND controller the chip is on.
780 * @nmtd: The new controller MTD instance to fill in.
781 * @set: The information passed from the board specific platform data.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 *
Ben Dooks3db72152009-05-30 17:18:15 +0100783 * Initialise the given @nmtd from the information in @info and @set. This
784 * readies the structure for use with the MTD layer functions by ensuring
785 * all pointers are setup and the necessary control routines selected.
786 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787static void s3c2410_nand_init_chip(struct s3c2410_nand_info *info,
788 struct s3c2410_nand_mtd *nmtd,
789 struct s3c2410_nand_set *set)
790{
791 struct nand_chip *chip = &nmtd->chip;
Ben Dooks2c06a082006-06-27 14:35:46 +0100792 void __iomem *regs = info->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 chip->write_buf = s3c2410_nand_write_buf;
795 chip->read_buf = s3c2410_nand_read_buf;
796 chip->select_chip = s3c2410_nand_select_chip;
797 chip->chip_delay = 50;
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100798 nand_set_controller_data(chip, nmtd);
Ben Dooks74218fe2009-11-02 18:12:51 +0000799 chip->options = set->options;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 chip->controller = &info->controller;
801
Ben Dooks2c06a082006-06-27 14:35:46 +0100802 switch (info->cpu_type) {
803 case TYPE_S3C2410:
804 chip->IO_ADDR_W = regs + S3C2410_NFDATA;
805 info->sel_reg = regs + S3C2410_NFCONF;
806 info->sel_bit = S3C2410_NFCONF_nFCE;
807 chip->cmd_ctrl = s3c2410_nand_hwcontrol;
808 chip->dev_ready = s3c2410_nand_devready;
809 break;
810
811 case TYPE_S3C2440:
812 chip->IO_ADDR_W = regs + S3C2440_NFDATA;
813 info->sel_reg = regs + S3C2440_NFCONT;
814 info->sel_bit = S3C2440_NFCONT_nFCE;
815 chip->cmd_ctrl = s3c2440_nand_hwcontrol;
816 chip->dev_ready = s3c2440_nand_devready;
Matt Reimerb773bb22007-10-18 17:43:07 -0700817 chip->read_buf = s3c2440_nand_read_buf;
818 chip->write_buf = s3c2440_nand_write_buf;
Ben Dooks2c06a082006-06-27 14:35:46 +0100819 break;
820
821 case TYPE_S3C2412:
822 chip->IO_ADDR_W = regs + S3C2440_NFDATA;
823 info->sel_reg = regs + S3C2440_NFCONT;
824 info->sel_bit = S3C2412_NFCONT_nFCE0;
825 chip->cmd_ctrl = s3c2440_nand_hwcontrol;
826 chip->dev_ready = s3c2412_nand_devready;
827
828 if (readl(regs + S3C2410_NFCONF) & S3C2412_NFCONF_NANDBOOT)
829 dev_info(info->device, "System booted from NAND\n");
830
831 break;
Sachin Kamat54cd0202012-07-16 16:02:26 +0530832 }
Ben Dooks2c06a082006-06-27 14:35:46 +0100833
834 chip->IO_ADDR_R = chip->IO_ADDR_W;
Ben Dooksa4f957f2005-06-20 12:48:25 +0100835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 nmtd->info = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 nmtd->set = set;
838
Sachin Kamata68c5ec2012-07-16 16:02:25 +0530839#ifdef CONFIG_MTD_NAND_S3C2410_HWECC
840 chip->ecc.calculate = s3c2410_nand_calculate_ecc;
841 chip->ecc.correct = s3c2410_nand_correct_data;
842 chip->ecc.mode = NAND_ECC_HW;
843 chip->ecc.strength = 1;
844
845 switch (info->cpu_type) {
846 case TYPE_S3C2410:
847 chip->ecc.hwctl = s3c2410_nand_enable_hwecc;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +0200848 chip->ecc.calculate = s3c2410_nand_calculate_ecc;
Sachin Kamata68c5ec2012-07-16 16:02:25 +0530849 break;
Ben Dooksa4f957f2005-06-20 12:48:25 +0100850
Sachin Kamata68c5ec2012-07-16 16:02:25 +0530851 case TYPE_S3C2412:
852 chip->ecc.hwctl = s3c2412_nand_enable_hwecc;
853 chip->ecc.calculate = s3c2412_nand_calculate_ecc;
854 break;
Ben Dooks2c06a082006-06-27 14:35:46 +0100855
Sachin Kamata68c5ec2012-07-16 16:02:25 +0530856 case TYPE_S3C2440:
857 chip->ecc.hwctl = s3c2440_nand_enable_hwecc;
858 chip->ecc.calculate = s3c2440_nand_calculate_ecc;
859 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
Sachin Kamata68c5ec2012-07-16 16:02:25 +0530861#else
862 chip->ecc.mode = NAND_ECC_SOFT;
863#endif
Ben Dooks1c21ab62008-04-15 11:36:21 +0100864
Ben Dooks37e5ffa2008-04-15 11:36:22 +0100865 if (set->disable_ecc)
866 chip->ecc.mode = NAND_ECC_NONE;
Andy Green8c3e8432009-05-10 15:41:25 -0500867
868 switch (chip->ecc.mode) {
869 case NAND_ECC_NONE:
870 dev_info(info->device, "NAND ECC disabled\n");
871 break;
872 case NAND_ECC_SOFT:
873 dev_info(info->device, "NAND soft ECC\n");
874 break;
875 case NAND_ECC_HW:
876 dev_info(info->device, "NAND hardware ECC\n");
877 break;
878 default:
879 dev_info(info->device, "NAND ECC UNKNOWN\n");
880 break;
881 }
Michel Pollet9db41f92009-05-13 16:54:14 +0100882
883 /* If you use u-boot BBT creation code, specifying this flag will
884 * let the kernel fish out the BBT from the NAND, and also skip the
885 * full NAND scan that can take 1/2s or so. Little things... */
Brian Norrisa40f7342011-05-31 16:31:22 -0700886 if (set->flash_bbt) {
Brian Norrisbb9ebd42011-05-31 16:31:23 -0700887 chip->bbt_options |= NAND_BBT_USE_FLASH;
Brian Norrisa40f7342011-05-31 16:31:22 -0700888 chip->options |= NAND_SKIP_BBTSCAN;
889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
891
Ben Dooks3db72152009-05-30 17:18:15 +0100892/**
893 * s3c2410_nand_update_chip - post probe update
894 * @info: The controller instance.
895 * @nmtd: The driver version of the MTD instance.
Ben Dooks71d54f32008-04-15 11:36:19 +0100896 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200897 * This routine is called after the chip probe has successfully completed
Ben Dooks3db72152009-05-30 17:18:15 +0100898 * and the relevant per-chip information updated. This call ensure that
899 * we update the internal state accordingly.
900 *
901 * The internal state is currently limited to the ECC state information.
902*/
Ben Dooks71d54f32008-04-15 11:36:19 +0100903static void s3c2410_nand_update_chip(struct s3c2410_nand_info *info,
904 struct s3c2410_nand_mtd *nmtd)
905{
906 struct nand_chip *chip = &nmtd->chip;
907
Ben Dooks451d3392008-05-20 17:32:14 +0100908 dev_dbg(info->device, "chip %p => page shift %d\n",
909 chip, chip->page_shift);
Ben Dooks71d54f32008-04-15 11:36:19 +0100910
Andy Green8c3e8432009-05-10 15:41:25 -0500911 if (chip->ecc.mode != NAND_ECC_HW)
912 return;
913
Adam Buchbinder48fc7f72012-09-19 21:48:00 -0400914 /* change the behaviour depending on whether we are using
Ben Dooks71d54f32008-04-15 11:36:19 +0100915 * the large or small page nand device */
916
Andy Green8c3e8432009-05-10 15:41:25 -0500917 if (chip->page_shift > 10) {
918 chip->ecc.size = 256;
919 chip->ecc.bytes = 3;
920 } else {
921 chip->ecc.size = 512;
922 chip->ecc.bytes = 3;
923 chip->ecc.layout = &nand_hw_eccoob;
Ben Dooks71d54f32008-04-15 11:36:19 +0100924 }
925}
926
Ben Dooksec0482e2009-05-30 16:55:29 +0100927/* s3c24xx_nand_probe
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 *
929 * called by device layer when it finds a device matching
930 * one our driver can handled. This code checks to see if
931 * it can allocate all necessary resources then calls the
932 * nand layer to look for devices
933*/
Ben Dooksec0482e2009-05-30 16:55:29 +0100934static int s3c24xx_nand_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
Russell King3ae5eae2005-11-09 22:32:44 +0000936 struct s3c2410_platform_nand *plat = to_nand_plat(pdev);
Sachin Kamat54cd0202012-07-16 16:02:26 +0530937 enum s3c_cpu_type cpu_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 struct s3c2410_nand_info *info;
939 struct s3c2410_nand_mtd *nmtd;
940 struct s3c2410_nand_set *sets;
941 struct resource *res;
942 int err = 0;
943 int size;
944 int nr_sets;
945 int setno;
946
Ben Dooksec0482e2009-05-30 16:55:29 +0100947 cpu_type = platform_get_device_id(pdev)->driver_data;
948
Sachin Kamat6f32a3e2012-08-21 14:24:09 +0530949 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 if (info == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 err = -ENOMEM;
952 goto exit_error;
953 }
954
Russell King3ae5eae2005-11-09 22:32:44 +0000955 platform_set_drvdata(pdev, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957 spin_lock_init(&info->controller.lock);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100958 init_waitqueue_head(&info->controller.wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 /* get the clock source and enable it */
961
Sachin Kamat6f32a3e2012-08-21 14:24:09 +0530962 info->clk = devm_clk_get(&pdev->dev, "nand");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 if (IS_ERR(info->clk)) {
Joe Perches898eb712007-10-18 03:06:30 -0700964 dev_err(&pdev->dev, "failed to get clock\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 err = -ENOENT;
966 goto exit_error;
967 }
968
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200969 s3c2410_nand_clk_set_state(info, CLOCK_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971 /* allocate and map the resource */
972
Ben Dooksa4f957f2005-06-20 12:48:25 +0100973 /* currently we assume we have the one resource */
Sachin Kamat6f32a3e2012-08-21 14:24:09 +0530974 res = pdev->resource;
H Hartley Sweetenfc161c42009-12-14 16:56:22 -0500975 size = resource_size(res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Sachin Kamat6f32a3e2012-08-21 14:24:09 +0530977 info->device = &pdev->dev;
978 info->platform = plat;
979 info->cpu_type = cpu_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Thierry Redingb0de7742013-01-21 11:09:12 +0100981 info->regs = devm_ioremap_resource(&pdev->dev, res);
982 if (IS_ERR(info->regs)) {
983 err = PTR_ERR(info->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 goto exit_error;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Russell King3ae5eae2005-11-09 22:32:44 +0000987 dev_dbg(&pdev->dev, "mapped registers at %p\n", info->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989 /* initialise the hardware */
990
Ben Dooks30821fe2008-07-15 11:58:31 +0100991 err = s3c2410_nand_inithw(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 if (err != 0)
993 goto exit_error;
994
995 sets = (plat != NULL) ? plat->sets : NULL;
996 nr_sets = (plat != NULL) ? plat->nr_sets : 1;
997
998 info->mtd_count = nr_sets;
999
1000 /* allocate our information */
1001
1002 size = nr_sets * sizeof(*info->mtds);
Sachin Kamat6f32a3e2012-08-21 14:24:09 +05301003 info->mtds = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 if (info->mtds == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 err = -ENOMEM;
1006 goto exit_error;
1007 }
1008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 /* initialise all possible chips */
1010
1011 nmtd = info->mtds;
1012
1013 for (setno = 0; setno < nr_sets; setno++, nmtd++) {
Boris BREZILLON7208b992015-12-10 09:00:22 +01001014 struct mtd_info *mtd = nand_to_mtd(&nmtd->chip);
1015
Sachin Kamatf938bc52012-08-21 10:21:15 +05301016 pr_debug("initialising set %d (%p, info %p)\n",
1017 setno, nmtd, info);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001018
Boris BREZILLON7208b992015-12-10 09:00:22 +01001019 mtd->dev.parent = &pdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 s3c2410_nand_init_chip(info, nmtd, sets);
1021
Boris BREZILLON7208b992015-12-10 09:00:22 +01001022 nmtd->scan_res = nand_scan_ident(mtd,
David Woodhouse5e81e882010-02-26 18:32:56 +00001023 (sets) ? sets->nr_chips : 1,
1024 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026 if (nmtd->scan_res == 0) {
Ben Dooks71d54f32008-04-15 11:36:19 +01001027 s3c2410_nand_update_chip(info, nmtd);
Boris BREZILLON7208b992015-12-10 09:00:22 +01001028 nand_scan_tail(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 s3c2410_nand_add_partition(info, nmtd, sets);
1030 }
1031
1032 if (sets != NULL)
1033 sets++;
1034 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001035
Ben Dooks30821fe2008-07-15 11:58:31 +01001036 err = s3c2410_nand_cpufreq_register(info);
1037 if (err < 0) {
1038 dev_err(&pdev->dev, "failed to init cpufreq support\n");
1039 goto exit_error;
1040 }
1041
Jiri Pinkavaac497c12011-04-13 11:59:30 +02001042 if (allow_clk_suspend(info)) {
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001043 dev_info(&pdev->dev, "clock idle support enabled\n");
Jiri Pinkavaac497c12011-04-13 11:59:30 +02001044 s3c2410_nand_clk_set_state(info, CLOCK_SUSPEND);
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001045 }
1046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 return 0;
1048
1049 exit_error:
Ben Dooksec0482e2009-05-30 16:55:29 +01001050 s3c24xx_nand_remove(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 if (err == 0)
1053 err = -EINVAL;
1054 return err;
1055}
1056
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001057/* PM Support */
1058#ifdef CONFIG_PM
1059
1060static int s3c24xx_nand_suspend(struct platform_device *dev, pm_message_t pm)
1061{
1062 struct s3c2410_nand_info *info = platform_get_drvdata(dev);
1063
1064 if (info) {
Ben Dooks09160832008-04-15 11:36:18 +01001065 info->save_sel = readl(info->sel_reg);
Ben Dooks03680b12007-11-19 23:28:07 +00001066
1067 /* For the moment, we must ensure nFCE is high during
1068 * the time we are suspended. This really should be
1069 * handled by suspending the MTDs we are using, but
1070 * that is currently not the case. */
1071
Ben Dooks09160832008-04-15 11:36:18 +01001072 writel(info->save_sel | info->sel_bit, info->sel_reg);
Ben Dooks03680b12007-11-19 23:28:07 +00001073
Jiri Pinkavaac497c12011-04-13 11:59:30 +02001074 s3c2410_nand_clk_set_state(info, CLOCK_DISABLE);
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001075 }
1076
1077 return 0;
1078}
1079
1080static int s3c24xx_nand_resume(struct platform_device *dev)
1081{
1082 struct s3c2410_nand_info *info = platform_get_drvdata(dev);
Ben Dooks09160832008-04-15 11:36:18 +01001083 unsigned long sel;
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001084
1085 if (info) {
Jiri Pinkavaac497c12011-04-13 11:59:30 +02001086 s3c2410_nand_clk_set_state(info, CLOCK_ENABLE);
Ben Dooks30821fe2008-07-15 11:58:31 +01001087 s3c2410_nand_inithw(info);
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001088
Ben Dooks03680b12007-11-19 23:28:07 +00001089 /* Restore the state of the nFCE line. */
1090
Ben Dooks09160832008-04-15 11:36:18 +01001091 sel = readl(info->sel_reg);
1092 sel &= ~info->sel_bit;
1093 sel |= info->save_sel & info->sel_bit;
1094 writel(sel, 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_SUSPEND);
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001097 }
1098
1099 return 0;
1100}
1101
1102#else
1103#define s3c24xx_nand_suspend NULL
1104#define s3c24xx_nand_resume NULL
1105#endif
1106
Ben Dooksa4f957f2005-06-20 12:48:25 +01001107/* driver device registration */
1108
Krzysztof Kozlowski0abe75d2015-05-02 00:50:02 +09001109static const struct platform_device_id s3c24xx_driver_ids[] = {
Ben Dooksec0482e2009-05-30 16:55:29 +01001110 {
1111 .name = "s3c2410-nand",
1112 .driver_data = TYPE_S3C2410,
1113 }, {
1114 .name = "s3c2440-nand",
1115 .driver_data = TYPE_S3C2440,
1116 }, {
1117 .name = "s3c2412-nand",
1118 .driver_data = TYPE_S3C2412,
Peter Korsgaard9dbc0902009-06-07 06:04:23 -07001119 }, {
1120 .name = "s3c6400-nand",
1121 .driver_data = TYPE_S3C2412, /* compatible with 2412 */
Russell King3ae5eae2005-11-09 22:32:44 +00001122 },
Ben Dooksec0482e2009-05-30 16:55:29 +01001123 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124};
1125
Ben Dooksec0482e2009-05-30 16:55:29 +01001126MODULE_DEVICE_TABLE(platform, s3c24xx_driver_ids);
Ben Dooksa4f957f2005-06-20 12:48:25 +01001127
Ben Dooksec0482e2009-05-30 16:55:29 +01001128static struct platform_driver s3c24xx_nand_driver = {
1129 .probe = s3c24xx_nand_probe,
1130 .remove = s3c24xx_nand_remove,
Ben Dooks2c06a082006-06-27 14:35:46 +01001131 .suspend = s3c24xx_nand_suspend,
1132 .resume = s3c24xx_nand_resume,
Ben Dooksec0482e2009-05-30 16:55:29 +01001133 .id_table = s3c24xx_driver_ids,
Ben Dooks2c06a082006-06-27 14:35:46 +01001134 .driver = {
Ben Dooksec0482e2009-05-30 16:55:29 +01001135 .name = "s3c24xx-nand",
Ben Dooks2c06a082006-06-27 14:35:46 +01001136 },
1137};
1138
Sachin Kamat056fcab2012-07-16 16:02:22 +05301139module_platform_driver(s3c24xx_nand_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141MODULE_LICENSE("GPL");
1142MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
Ben Dooksa4f957f2005-06-20 12:48:25 +01001143MODULE_DESCRIPTION("S3C24XX MTD NAND driver");