blob: 3021b174761fdd7e5516f26bfc01b9663eed8450 [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>
32#include <linux/init.h>
33#include <linux/kernel.h>
34#include <linux/string.h>
35#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
48#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Ben Dooks7926b5a2008-10-30 10:14:35 +000050#include <plat/regs-nand.h>
51#include <plat/nand.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#ifdef CONFIG_MTD_NAND_S3C2410_HWECC
54static int hardware_ecc = 1;
55#else
56static int hardware_ecc = 0;
57#endif
58
Ben Dooksd1fef3c2006-06-19 09:29:38 +010059#ifdef CONFIG_MTD_NAND_S3C2410_CLKSTOP
Jiri Pinkavaac497c12011-04-13 11:59:30 +020060static const int clock_stop = 1;
Ben Dooksd1fef3c2006-06-19 09:29:38 +010061#else
62static const int clock_stop = 0;
63#endif
64
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066/* new oob placement block for use with hardware ecc generation
67 */
68
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020069static struct nand_ecclayout nand_hw_eccoob = {
David Woodhousee0c7d762006-05-13 18:07:53 +010070 .eccbytes = 3,
71 .eccpos = {0, 1, 2},
72 .oobfree = {{8, 8}}
Linus Torvalds1da177e2005-04-16 15:20:36 -070073};
74
75/* controller and mtd information */
76
77struct s3c2410_nand_info;
78
Ben Dooks3db72152009-05-30 17:18:15 +010079/**
80 * struct s3c2410_nand_mtd - driver MTD structure
81 * @mtd: The MTD instance to pass to the MTD layer.
82 * @chip: The NAND chip information.
83 * @set: The platform information supplied for this set of NAND chips.
84 * @info: Link back to the hardware information.
85 * @scan_res: The result from calling nand_scan_ident().
86*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070087struct s3c2410_nand_mtd {
88 struct mtd_info mtd;
89 struct nand_chip chip;
90 struct s3c2410_nand_set *set;
91 struct s3c2410_nand_info *info;
92 int scan_res;
93};
94
Ben Dooks2c06a082006-06-27 14:35:46 +010095enum s3c_cpu_type {
96 TYPE_S3C2410,
97 TYPE_S3C2412,
98 TYPE_S3C2440,
99};
100
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200101enum s3c_nand_clk_state {
102 CLOCK_DISABLE = 0,
103 CLOCK_ENABLE,
104 CLOCK_SUSPEND,
105};
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107/* overview of the s3c2410 nand state */
108
Ben Dooks3db72152009-05-30 17:18:15 +0100109/**
110 * struct s3c2410_nand_info - NAND controller state.
111 * @mtds: An array of MTD instances on this controoler.
112 * @platform: The platform data for this board.
113 * @device: The platform device we bound to.
114 * @area: The IO area resource that came from request_mem_region().
115 * @clk: The clock resource for this controller.
116 * @regs: The area mapped for the hardware registers described by @area.
117 * @sel_reg: Pointer to the register controlling the NAND selection.
118 * @sel_bit: The bit in @sel_reg to select the NAND chip.
119 * @mtd_count: The number of MTDs created from this controller.
120 * @save_sel: The contents of @sel_reg to be saved over suspend.
121 * @clk_rate: The clock rate from @clk.
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200122 * @clk_state: The current clock state.
Ben Dooks3db72152009-05-30 17:18:15 +0100123 * @cpu_type: The exact type of this controller.
124 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125struct s3c2410_nand_info {
126 /* mtd info */
127 struct nand_hw_control controller;
128 struct s3c2410_nand_mtd *mtds;
129 struct s3c2410_platform_nand *platform;
130
131 /* device info */
132 struct device *device;
133 struct resource *area;
134 struct clk *clk;
Ben Dooksfdf2fd52005-02-18 14:46:15 +0000135 void __iomem *regs;
Ben Dooks2c06a082006-06-27 14:35:46 +0100136 void __iomem *sel_reg;
137 int sel_bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 int mtd_count;
Ben Dooks09160832008-04-15 11:36:18 +0100139 unsigned long save_sel;
Ben Dooks30821fe2008-07-15 11:58:31 +0100140 unsigned long clk_rate;
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200141 enum s3c_nand_clk_state clk_state;
Ben Dooks03680b12007-11-19 23:28:07 +0000142
Ben Dooks2c06a082006-06-27 14:35:46 +0100143 enum s3c_cpu_type cpu_type;
Ben Dooks30821fe2008-07-15 11:58:31 +0100144
145#ifdef CONFIG_CPU_FREQ
146 struct notifier_block freq_transition;
147#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148};
149
150/* conversion functions */
151
152static struct s3c2410_nand_mtd *s3c2410_nand_mtd_toours(struct mtd_info *mtd)
153{
154 return container_of(mtd, struct s3c2410_nand_mtd, mtd);
155}
156
157static struct s3c2410_nand_info *s3c2410_nand_mtd_toinfo(struct mtd_info *mtd)
158{
159 return s3c2410_nand_mtd_toours(mtd)->info;
160}
161
Russell King3ae5eae2005-11-09 22:32:44 +0000162static struct s3c2410_nand_info *to_nand_info(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Russell King3ae5eae2005-11-09 22:32:44 +0000164 return platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
Russell King3ae5eae2005-11-09 22:32:44 +0000167static struct s3c2410_platform_nand *to_nand_plat(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Russell King3ae5eae2005-11-09 22:32:44 +0000169 return dev->dev.platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200172static inline int allow_clk_suspend(struct s3c2410_nand_info *info)
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100173{
174 return clock_stop;
175}
176
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200177/**
178 * s3c2410_nand_clk_set_state - Enable, disable or suspend NAND clock.
179 * @info: The controller instance.
180 * @new_state: State to which clock should be set.
181 */
182static void s3c2410_nand_clk_set_state(struct s3c2410_nand_info *info,
183 enum s3c_nand_clk_state new_state)
184{
185 if (!allow_clk_suspend(info) && new_state == CLOCK_SUSPEND)
186 return;
187
188 if (info->clk_state == CLOCK_ENABLE) {
189 if (new_state != CLOCK_ENABLE)
190 clk_disable(info->clk);
191 } else {
192 if (new_state == CLOCK_ENABLE)
193 clk_enable(info->clk);
194 }
195
196 info->clk_state = new_state;
197}
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199/* timing calculations */
200
Ben Dookscfd320f2005-10-20 22:22:58 +0100201#define NS_IN_KHZ 1000000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Ben Dooks3db72152009-05-30 17:18:15 +0100203/**
204 * s3c_nand_calc_rate - calculate timing data.
205 * @wanted: The cycle time in nanoseconds.
206 * @clk: The clock rate in kHz.
207 * @max: The maximum divider value.
208 *
209 * Calculate the timing value from the given parameters.
210 */
Ben Dooks2c06a082006-06-27 14:35:46 +0100211static int s3c_nand_calc_rate(int wanted, unsigned long clk, int max)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 int result;
214
Ben Dooks947391c2009-05-30 18:34:16 +0100215 result = DIV_ROUND_UP((wanted * clk), NS_IN_KHZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 pr_debug("result %d from %ld, %d\n", result, clk, wanted);
218
219 if (result > max) {
Sachin Kamat92aeb5d2012-07-16 16:02:23 +0530220 pr_err("%d ns is too big for current clock rate %ld\n",
221 wanted, clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return -1;
223 }
224
225 if (result < 1)
226 result = 1;
227
228 return result;
229}
230
Ben Dookscfd320f2005-10-20 22:22:58 +0100231#define to_ns(ticks,clk) (((ticks) * NS_IN_KHZ) / (unsigned int)(clk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233/* controller setup */
234
Ben Dooks3db72152009-05-30 17:18:15 +0100235/**
236 * s3c2410_nand_setrate - setup controller timing information.
237 * @info: The controller instance.
238 *
239 * Given the information supplied by the platform, calculate and set
240 * the necessary timing registers in the hardware to generate the
241 * necessary timing cycles to the hardware.
242 */
Ben Dooks30821fe2008-07-15 11:58:31 +0100243static int s3c2410_nand_setrate(struct s3c2410_nand_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Ben Dooks30821fe2008-07-15 11:58:31 +0100245 struct s3c2410_platform_nand *plat = info->platform;
Ben Dooks2c06a082006-06-27 14:35:46 +0100246 int tacls_max = (info->cpu_type == TYPE_S3C2412) ? 8 : 4;
Ben Dookscfd320f2005-10-20 22:22:58 +0100247 int tacls, twrph0, twrph1;
Ben Dooks30821fe2008-07-15 11:58:31 +0100248 unsigned long clkrate = clk_get_rate(info->clk);
Nelson Castillo2612e522009-05-10 15:41:54 -0500249 unsigned long uninitialized_var(set), cfg, uninitialized_var(mask);
Ben Dooks30821fe2008-07-15 11:58:31 +0100250 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 /* calculate the timing information for the controller */
253
Ben Dooks30821fe2008-07-15 11:58:31 +0100254 info->clk_rate = clkrate;
Ben Dookscfd320f2005-10-20 22:22:58 +0100255 clkrate /= 1000; /* turn clock into kHz for ease of use */
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if (plat != NULL) {
Ben Dooks2c06a082006-06-27 14:35:46 +0100258 tacls = s3c_nand_calc_rate(plat->tacls, clkrate, tacls_max);
259 twrph0 = s3c_nand_calc_rate(plat->twrph0, clkrate, 8);
260 twrph1 = s3c_nand_calc_rate(plat->twrph1, clkrate, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 } else {
262 /* default timings */
Ben Dooks2c06a082006-06-27 14:35:46 +0100263 tacls = tacls_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 twrph0 = 8;
265 twrph1 = 8;
266 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (tacls < 0 || twrph0 < 0 || twrph1 < 0) {
Ben Dooks99974c62006-06-21 15:43:05 +0100269 dev_err(info->device, "cannot get suitable timings\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 return -EINVAL;
271 }
272
Ben Dooks99974c62006-06-21 15:43:05 +0100273 dev_info(info->device, "Tacls=%d, %dns Twrph0=%d %dns, Twrph1=%d %dns\n",
David Woodhousee0c7d762006-05-13 18:07:53 +0100274 tacls, to_ns(tacls, clkrate), twrph0, to_ns(twrph0, clkrate), twrph1, to_ns(twrph1, clkrate));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Ben Dooks30821fe2008-07-15 11:58:31 +0100276 switch (info->cpu_type) {
277 case TYPE_S3C2410:
278 mask = (S3C2410_NFCONF_TACLS(3) |
279 S3C2410_NFCONF_TWRPH0(7) |
280 S3C2410_NFCONF_TWRPH1(7));
281 set = S3C2410_NFCONF_EN;
282 set |= S3C2410_NFCONF_TACLS(tacls - 1);
283 set |= S3C2410_NFCONF_TWRPH0(twrph0 - 1);
284 set |= S3C2410_NFCONF_TWRPH1(twrph1 - 1);
285 break;
286
287 case TYPE_S3C2440:
288 case TYPE_S3C2412:
Peter Korsgaarda755a382009-06-03 13:46:54 +0200289 mask = (S3C2440_NFCONF_TACLS(tacls_max - 1) |
290 S3C2440_NFCONF_TWRPH0(7) |
291 S3C2440_NFCONF_TWRPH1(7));
Ben Dooks30821fe2008-07-15 11:58:31 +0100292
293 set = S3C2440_NFCONF_TACLS(tacls - 1);
294 set |= S3C2440_NFCONF_TWRPH0(twrph0 - 1);
295 set |= S3C2440_NFCONF_TWRPH1(twrph1 - 1);
296 break;
297
298 default:
Ben Dooks30821fe2008-07-15 11:58:31 +0100299 BUG();
300 }
301
Ben Dooks30821fe2008-07-15 11:58:31 +0100302 local_irq_save(flags);
303
304 cfg = readl(info->regs + S3C2410_NFCONF);
305 cfg &= ~mask;
306 cfg |= set;
307 writel(cfg, info->regs + S3C2410_NFCONF);
308
309 local_irq_restore(flags);
310
Andy Greenae7304e2009-05-10 15:42:02 -0500311 dev_dbg(info->device, "NF_CONF is 0x%lx\n", cfg);
312
Ben Dooks30821fe2008-07-15 11:58:31 +0100313 return 0;
314}
315
Ben Dooks3db72152009-05-30 17:18:15 +0100316/**
317 * s3c2410_nand_inithw - basic hardware initialisation
318 * @info: The hardware state.
319 *
320 * Do the basic initialisation of the hardware, using s3c2410_nand_setrate()
321 * to setup the hardware access speeds and set the controller to be enabled.
322*/
Ben Dooks30821fe2008-07-15 11:58:31 +0100323static int s3c2410_nand_inithw(struct s3c2410_nand_info *info)
324{
325 int ret;
326
327 ret = s3c2410_nand_setrate(info);
328 if (ret < 0)
329 return ret;
330
Ben Dooks2c06a082006-06-27 14:35:46 +0100331 switch (info->cpu_type) {
332 case TYPE_S3C2410:
Ben Dooks30821fe2008-07-15 11:58:31 +0100333 default:
Ben Dooks2c06a082006-06-27 14:35:46 +0100334 break;
335
336 case TYPE_S3C2440:
337 case TYPE_S3C2412:
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100338 /* enable the controller and de-assert nFCE */
339
Ben Dooks2c06a082006-06-27 14:35:46 +0100340 writel(S3C2440_NFCONT_ENABLE, info->regs + S3C2440_NFCONT);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return 0;
344}
345
Ben Dooks3db72152009-05-30 17:18:15 +0100346/**
347 * s3c2410_nand_select_chip - select the given nand chip
348 * @mtd: The MTD instance for this chip.
349 * @chip: The chip number.
350 *
351 * This is called by the MTD layer to either select a given chip for the
352 * @mtd instance, or to indicate that the access has finished and the
353 * chip can be de-selected.
354 *
355 * The routine ensures that the nFCE line is correctly setup, and any
356 * platform specific selection code is called to route nFCE to the specific
357 * chip.
358 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359static void s3c2410_nand_select_chip(struct mtd_info *mtd, int chip)
360{
361 struct s3c2410_nand_info *info;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000362 struct s3c2410_nand_mtd *nmtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 struct nand_chip *this = mtd->priv;
364 unsigned long cur;
365
366 nmtd = this->priv;
367 info = nmtd->info;
368
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200369 if (chip != -1)
370 s3c2410_nand_clk_set_state(info, CLOCK_ENABLE);
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100371
Ben Dooks2c06a082006-06-27 14:35:46 +0100372 cur = readl(info->sel_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 if (chip == -1) {
Ben Dooks2c06a082006-06-27 14:35:46 +0100375 cur |= info->sel_bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 } else {
Ben Dooksfb8d82a2005-07-06 21:05:10 +0100377 if (nmtd->set != NULL && chip > nmtd->set->nr_chips) {
Ben Dooks99974c62006-06-21 15:43:05 +0100378 dev_err(info->device, "invalid chip %d\n", chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return;
380 }
381
382 if (info->platform != NULL) {
383 if (info->platform->select_chip != NULL)
David Woodhousee0c7d762006-05-13 18:07:53 +0100384 (info->platform->select_chip) (nmtd->set, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
386
Ben Dooks2c06a082006-06-27 14:35:46 +0100387 cur &= ~info->sel_bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
389
Ben Dooks2c06a082006-06-27 14:35:46 +0100390 writel(cur, info->sel_reg);
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100391
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200392 if (chip == -1)
393 s3c2410_nand_clk_set_state(info, CLOCK_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
395
Ben Dooksad3b5fb2006-06-19 09:43:23 +0100396/* s3c2410_nand_hwcontrol
Ben Dooksa4f957f2005-06-20 12:48:25 +0100397 *
Ben Dooksad3b5fb2006-06-19 09:43:23 +0100398 * Issue command and address cycles to the chip
Ben Dooksa4f957f2005-06-20 12:48:25 +0100399*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200401static void s3c2410_nand_hwcontrol(struct mtd_info *mtd, int cmd,
David Woodhousef9068872006-06-10 00:53:16 +0100402 unsigned int ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
404 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
David Woodhousec9ac5972006-11-30 08:17:38 +0000405
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200406 if (cmd == NAND_CMD_NONE)
407 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
David Woodhousef9068872006-06-10 00:53:16 +0100409 if (ctrl & NAND_CLE)
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200410 writeb(cmd, info->regs + S3C2410_NFCMD);
411 else
412 writeb(cmd, info->regs + S3C2410_NFADDR);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100413}
414
415/* command and control functions */
416
David Woodhousef9068872006-06-10 00:53:16 +0100417static void s3c2440_nand_hwcontrol(struct mtd_info *mtd, int cmd,
418 unsigned int ctrl)
Ben Dooksa4f957f2005-06-20 12:48:25 +0100419{
420 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100421
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200422 if (cmd == NAND_CMD_NONE)
423 return;
Ben Dooksa4f957f2005-06-20 12:48:25 +0100424
David Woodhousef9068872006-06-10 00:53:16 +0100425 if (ctrl & NAND_CLE)
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200426 writeb(cmd, info->regs + S3C2440_NFCMD);
427 else
428 writeb(cmd, info->regs + S3C2440_NFADDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431/* s3c2410_nand_devready()
432 *
433 * returns 0 if the nand is busy, 1 if it is ready
434*/
435
436static int s3c2410_nand_devready(struct mtd_info *mtd)
437{
438 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return readb(info->regs + S3C2410_NFSTAT) & S3C2410_NFSTAT_BUSY;
440}
441
Ben Dooks2c06a082006-06-27 14:35:46 +0100442static int s3c2440_nand_devready(struct mtd_info *mtd)
443{
444 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
445 return readb(info->regs + S3C2440_NFSTAT) & S3C2440_NFSTAT_READY;
446}
447
448static int s3c2412_nand_devready(struct mtd_info *mtd)
449{
450 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
451 return readb(info->regs + S3C2412_NFSTAT) & S3C2412_NFSTAT_READY;
452}
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454/* ECC handling functions */
455
Ben Dooks2c06a082006-06-27 14:35:46 +0100456static int s3c2410_nand_correct_data(struct mtd_info *mtd, u_char *dat,
457 u_char *read_ecc, u_char *calc_ecc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Ben Dooksa2593242007-02-02 16:59:33 +0000459 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
460 unsigned int diff0, diff1, diff2;
461 unsigned int bit, byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Ben Dooksa2593242007-02-02 16:59:33 +0000463 pr_debug("%s(%p,%p,%p,%p)\n", __func__, mtd, dat, read_ecc, calc_ecc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Ben Dooksa2593242007-02-02 16:59:33 +0000465 diff0 = read_ecc[0] ^ calc_ecc[0];
466 diff1 = read_ecc[1] ^ calc_ecc[1];
467 diff2 = read_ecc[2] ^ calc_ecc[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Ben Dooksa2593242007-02-02 16:59:33 +0000469 pr_debug("%s: rd %02x%02x%02x calc %02x%02x%02x diff %02x%02x%02x\n",
470 __func__,
471 read_ecc[0], read_ecc[1], read_ecc[2],
472 calc_ecc[0], calc_ecc[1], calc_ecc[2],
473 diff0, diff1, diff2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Ben Dooksa2593242007-02-02 16:59:33 +0000475 if (diff0 == 0 && diff1 == 0 && diff2 == 0)
476 return 0; /* ECC is ok */
477
Ben Dooksc45c6c62008-04-15 11:36:20 +0100478 /* sometimes people do not think about using the ECC, so check
479 * to see if we have an 0xff,0xff,0xff read ECC and then ignore
480 * the error, on the assumption that this is an un-eccd page.
481 */
482 if (read_ecc[0] == 0xff && read_ecc[1] == 0xff && read_ecc[2] == 0xff
483 && info->platform->ignore_unset_ecc)
484 return 0;
485
Ben Dooksa2593242007-02-02 16:59:33 +0000486 /* Can we correct this ECC (ie, one row and column change).
487 * Note, this is similar to the 256 error code on smartmedia */
488
489 if (((diff0 ^ (diff0 >> 1)) & 0x55) == 0x55 &&
490 ((diff1 ^ (diff1 >> 1)) & 0x55) == 0x55 &&
491 ((diff2 ^ (diff2 >> 1)) & 0x55) == 0x55) {
492 /* calculate the bit position of the error */
493
Matt Reimerd0bf3792007-10-18 18:02:43 -0700494 bit = ((diff2 >> 3) & 1) |
495 ((diff2 >> 4) & 2) |
496 ((diff2 >> 5) & 4);
Ben Dooksa2593242007-02-02 16:59:33 +0000497
498 /* calculate the byte position of the error */
499
Matt Reimerd0bf3792007-10-18 18:02:43 -0700500 byte = ((diff2 << 7) & 0x100) |
501 ((diff1 << 0) & 0x80) |
502 ((diff1 << 1) & 0x40) |
503 ((diff1 << 2) & 0x20) |
504 ((diff1 << 3) & 0x10) |
505 ((diff0 >> 4) & 0x08) |
506 ((diff0 >> 3) & 0x04) |
507 ((diff0 >> 2) & 0x02) |
508 ((diff0 >> 1) & 0x01);
Ben Dooksa2593242007-02-02 16:59:33 +0000509
510 dev_dbg(info->device, "correcting error bit %d, byte %d\n",
511 bit, byte);
512
513 dat[byte] ^= (1 << bit);
514 return 1;
515 }
516
517 /* if there is only one bit difference in the ECC, then
518 * one of only a row or column parity has changed, which
519 * means the error is most probably in the ECC itself */
520
521 diff0 |= (diff1 << 8);
522 diff0 |= (diff2 << 16);
523
524 if ((diff0 & ~(1<<fls(diff0))) == 0)
525 return 1;
526
Matt Reimer4fac9f62007-10-18 18:02:44 -0700527 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
Ben Dooksa4f957f2005-06-20 12:48:25 +0100530/* ECC functions
531 *
532 * These allow the s3c2410 and s3c2440 to use the controller's ECC
533 * generator block to ECC the data as it passes through]
534*/
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536static void s3c2410_nand_enable_hwecc(struct mtd_info *mtd, int mode)
537{
538 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
539 unsigned long ctrl;
540
541 ctrl = readl(info->regs + S3C2410_NFCONF);
542 ctrl |= S3C2410_NFCONF_INITECC;
543 writel(ctrl, info->regs + S3C2410_NFCONF);
544}
545
Matthieu CASTET4f659922007-02-13 12:30:38 +0100546static void s3c2412_nand_enable_hwecc(struct mtd_info *mtd, int mode)
547{
548 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
549 unsigned long ctrl;
550
551 ctrl = readl(info->regs + S3C2440_NFCONT);
552 writel(ctrl | S3C2412_NFCONT_INIT_MAIN_ECC, info->regs + S3C2440_NFCONT);
553}
554
Ben Dooksa4f957f2005-06-20 12:48:25 +0100555static void s3c2440_nand_enable_hwecc(struct mtd_info *mtd, int mode)
556{
557 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
558 unsigned long ctrl;
559
560 ctrl = readl(info->regs + S3C2440_NFCONT);
561 writel(ctrl | S3C2440_NFCONT_INITECC, info->regs + S3C2440_NFCONT);
562}
563
David Woodhousee0c7d762006-05-13 18:07:53 +0100564static int s3c2410_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
566 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
567
568 ecc_code[0] = readb(info->regs + S3C2410_NFECC + 0);
569 ecc_code[1] = readb(info->regs + S3C2410_NFECC + 1);
570 ecc_code[2] = readb(info->regs + S3C2410_NFECC + 2);
571
Ben Dooksa2593242007-02-02 16:59:33 +0000572 pr_debug("%s: returning ecc %02x%02x%02x\n", __func__,
573 ecc_code[0], ecc_code[1], ecc_code[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 return 0;
576}
577
Matthieu CASTET4f659922007-02-13 12:30:38 +0100578static int s3c2412_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
579{
580 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
581 unsigned long ecc = readl(info->regs + S3C2412_NFMECC0);
582
583 ecc_code[0] = ecc;
584 ecc_code[1] = ecc >> 8;
585 ecc_code[2] = ecc >> 16;
586
587 pr_debug("calculate_ecc: returning ecc %02x,%02x,%02x\n", ecc_code[0], ecc_code[1], ecc_code[2]);
588
589 return 0;
590}
591
David Woodhousee0c7d762006-05-13 18:07:53 +0100592static int s3c2440_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
Ben Dooksa4f957f2005-06-20 12:48:25 +0100593{
594 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
595 unsigned long ecc = readl(info->regs + S3C2440_NFMECC0);
596
597 ecc_code[0] = ecc;
598 ecc_code[1] = ecc >> 8;
599 ecc_code[2] = ecc >> 16;
600
Ben Dooks71d54f32008-04-15 11:36:19 +0100601 pr_debug("%s: returning ecc %06lx\n", __func__, ecc & 0xffffff);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100602
603 return 0;
604}
605
Ben Dooksa4f957f2005-06-20 12:48:25 +0100606/* over-ride the standard functions for a little more speed. We can
607 * use read/write block to move the data buffers to/from the controller
608*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610static void s3c2410_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
611{
612 struct nand_chip *this = mtd->priv;
613 readsb(this->IO_ADDR_R, buf, len);
614}
615
Matt Reimerb773bb22007-10-18 17:43:07 -0700616static void s3c2440_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
617{
618 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
Ben Dooksdea2aa62009-05-30 18:30:18 +0100619
620 readsl(info->regs + S3C2440_NFDATA, buf, len >> 2);
621
622 /* cleanup if we've got less than a word to do */
623 if (len & 3) {
624 buf += len & ~3;
625
626 for (; len & 3; len--)
627 *buf++ = readb(info->regs + S3C2440_NFDATA);
628 }
Matt Reimerb773bb22007-10-18 17:43:07 -0700629}
630
David Woodhousee0c7d762006-05-13 18:07:53 +0100631static void s3c2410_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
633 struct nand_chip *this = mtd->priv;
634 writesb(this->IO_ADDR_W, buf, len);
635}
636
Matt Reimerb773bb22007-10-18 17:43:07 -0700637static void s3c2440_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
638{
639 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
Ben Dooksdea2aa62009-05-30 18:30:18 +0100640
641 writesl(info->regs + S3C2440_NFDATA, buf, len >> 2);
642
643 /* cleanup any fractional write */
644 if (len & 3) {
645 buf += len & ~3;
646
647 for (; len & 3; len--, buf++)
648 writeb(*buf, info->regs + S3C2440_NFDATA);
649 }
Matt Reimerb773bb22007-10-18 17:43:07 -0700650}
651
Ben Dooks30821fe2008-07-15 11:58:31 +0100652/* cpufreq driver support */
653
654#ifdef CONFIG_CPU_FREQ
655
656static int s3c2410_nand_cpufreq_transition(struct notifier_block *nb,
657 unsigned long val, void *data)
658{
659 struct s3c2410_nand_info *info;
660 unsigned long newclk;
661
662 info = container_of(nb, struct s3c2410_nand_info, freq_transition);
663 newclk = clk_get_rate(info->clk);
664
665 if ((val == CPUFREQ_POSTCHANGE && newclk < info->clk_rate) ||
666 (val == CPUFREQ_PRECHANGE && newclk > info->clk_rate)) {
667 s3c2410_nand_setrate(info);
668 }
669
670 return 0;
671}
672
673static inline int s3c2410_nand_cpufreq_register(struct s3c2410_nand_info *info)
674{
675 info->freq_transition.notifier_call = s3c2410_nand_cpufreq_transition;
676
677 return cpufreq_register_notifier(&info->freq_transition,
678 CPUFREQ_TRANSITION_NOTIFIER);
679}
680
681static inline void s3c2410_nand_cpufreq_deregister(struct s3c2410_nand_info *info)
682{
683 cpufreq_unregister_notifier(&info->freq_transition,
684 CPUFREQ_TRANSITION_NOTIFIER);
685}
686
687#else
688static inline int s3c2410_nand_cpufreq_register(struct s3c2410_nand_info *info)
689{
690 return 0;
691}
692
693static inline void s3c2410_nand_cpufreq_deregister(struct s3c2410_nand_info *info)
694{
695}
696#endif
697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698/* device management functions */
699
Ben Dooksec0482e2009-05-30 16:55:29 +0100700static int s3c24xx_nand_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
Russell King3ae5eae2005-11-09 22:32:44 +0000702 struct s3c2410_nand_info *info = to_nand_info(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Russell King3ae5eae2005-11-09 22:32:44 +0000704 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000706 if (info == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return 0;
708
Ben Dooks30821fe2008-07-15 11:58:31 +0100709 s3c2410_nand_cpufreq_deregister(info);
710
711 /* Release all our mtds and their partitions, then go through
712 * freeing the resources used
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (info->mtds != NULL) {
716 struct s3c2410_nand_mtd *ptr = info->mtds;
717 int mtdno;
718
719 for (mtdno = 0; mtdno < info->mtd_count; mtdno++, ptr++) {
720 pr_debug("releasing mtd %d (%p)\n", mtdno, ptr);
721 nand_release(&ptr->mtd);
722 }
723
724 kfree(info->mtds);
725 }
726
727 /* free the common resources */
728
Jonghwan Choi4aa10622011-07-21 15:33:58 +0900729 if (!IS_ERR(info->clk)) {
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200730 s3c2410_nand_clk_set_state(info, CLOCK_DISABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 clk_put(info->clk);
732 }
733
734 if (info->regs != NULL) {
735 iounmap(info->regs);
736 info->regs = NULL;
737 }
738
739 if (info->area != NULL) {
740 release_resource(info->area);
741 kfree(info->area);
742 info->area = NULL;
743 }
744
745 kfree(info);
746
747 return 0;
748}
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750static int s3c2410_nand_add_partition(struct s3c2410_nand_info *info,
751 struct s3c2410_nand_mtd *mtd,
752 struct s3c2410_nand_set *set)
753{
Dmitry Eremin-Solenikov599501a2011-06-02 18:01:02 +0400754 if (set)
755 mtd->mtd.name = set->name;
Andy Greened27f022009-05-10 15:42:09 -0500756
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +0200757 return mtd_device_parse_register(&mtd->mtd, NULL, NULL,
758 set->partitions, set->nr_partitions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Ben Dooks3db72152009-05-30 17:18:15 +0100761/**
762 * s3c2410_nand_init_chip - initialise a single instance of an chip
763 * @info: The base NAND controller the chip is on.
764 * @nmtd: The new controller MTD instance to fill in.
765 * @set: The information passed from the board specific platform data.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 *
Ben Dooks3db72152009-05-30 17:18:15 +0100767 * Initialise the given @nmtd from the information in @info and @set. This
768 * readies the structure for use with the MTD layer functions by ensuring
769 * all pointers are setup and the necessary control routines selected.
770 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771static void s3c2410_nand_init_chip(struct s3c2410_nand_info *info,
772 struct s3c2410_nand_mtd *nmtd,
773 struct s3c2410_nand_set *set)
774{
775 struct nand_chip *chip = &nmtd->chip;
Ben Dooks2c06a082006-06-27 14:35:46 +0100776 void __iomem *regs = info->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 chip->write_buf = s3c2410_nand_write_buf;
779 chip->read_buf = s3c2410_nand_read_buf;
780 chip->select_chip = s3c2410_nand_select_chip;
781 chip->chip_delay = 50;
782 chip->priv = nmtd;
Ben Dooks74218fe2009-11-02 18:12:51 +0000783 chip->options = set->options;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 chip->controller = &info->controller;
785
Ben Dooks2c06a082006-06-27 14:35:46 +0100786 switch (info->cpu_type) {
787 case TYPE_S3C2410:
788 chip->IO_ADDR_W = regs + S3C2410_NFDATA;
789 info->sel_reg = regs + S3C2410_NFCONF;
790 info->sel_bit = S3C2410_NFCONF_nFCE;
791 chip->cmd_ctrl = s3c2410_nand_hwcontrol;
792 chip->dev_ready = s3c2410_nand_devready;
793 break;
794
795 case TYPE_S3C2440:
796 chip->IO_ADDR_W = regs + S3C2440_NFDATA;
797 info->sel_reg = regs + S3C2440_NFCONT;
798 info->sel_bit = S3C2440_NFCONT_nFCE;
799 chip->cmd_ctrl = s3c2440_nand_hwcontrol;
800 chip->dev_ready = s3c2440_nand_devready;
Matt Reimerb773bb22007-10-18 17:43:07 -0700801 chip->read_buf = s3c2440_nand_read_buf;
802 chip->write_buf = s3c2440_nand_write_buf;
Ben Dooks2c06a082006-06-27 14:35:46 +0100803 break;
804
805 case TYPE_S3C2412:
806 chip->IO_ADDR_W = regs + S3C2440_NFDATA;
807 info->sel_reg = regs + S3C2440_NFCONT;
808 info->sel_bit = S3C2412_NFCONT_nFCE0;
809 chip->cmd_ctrl = s3c2440_nand_hwcontrol;
810 chip->dev_ready = s3c2412_nand_devready;
811
812 if (readl(regs + S3C2410_NFCONF) & S3C2412_NFCONF_NANDBOOT)
813 dev_info(info->device, "System booted from NAND\n");
814
815 break;
816 }
817
818 chip->IO_ADDR_R = chip->IO_ADDR_W;
Ben Dooksa4f957f2005-06-20 12:48:25 +0100819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 nmtd->info = info;
821 nmtd->mtd.priv = chip;
David Woodhouse552d9202006-05-14 01:20:46 +0100822 nmtd->mtd.owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 nmtd->set = set;
824
825 if (hardware_ecc) {
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +0200826 chip->ecc.calculate = s3c2410_nand_calculate_ecc;
Ben Dooks2c06a082006-06-27 14:35:46 +0100827 chip->ecc.correct = s3c2410_nand_correct_data;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +0200828 chip->ecc.mode = NAND_ECC_HW;
Mike Dunn6a918ba2012-03-11 14:21:11 -0700829 chip->ecc.strength = 1;
Ben Dooksa4f957f2005-06-20 12:48:25 +0100830
Ben Dooks2c06a082006-06-27 14:35:46 +0100831 switch (info->cpu_type) {
832 case TYPE_S3C2410:
833 chip->ecc.hwctl = s3c2410_nand_enable_hwecc;
834 chip->ecc.calculate = s3c2410_nand_calculate_ecc;
835 break;
836
837 case TYPE_S3C2412:
Matthieu CASTET4f659922007-02-13 12:30:38 +0100838 chip->ecc.hwctl = s3c2412_nand_enable_hwecc;
839 chip->ecc.calculate = s3c2412_nand_calculate_ecc;
840 break;
841
Ben Dooks2c06a082006-06-27 14:35:46 +0100842 case TYPE_S3C2440:
843 chip->ecc.hwctl = s3c2440_nand_enable_hwecc;
844 chip->ecc.calculate = s3c2440_nand_calculate_ecc;
845 break;
846
Ben Dooksa4f957f2005-06-20 12:48:25 +0100847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 } else {
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +0200849 chip->ecc.mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 }
Ben Dooks1c21ab62008-04-15 11:36:21 +0100851
852 if (set->ecc_layout != NULL)
853 chip->ecc.layout = set->ecc_layout;
Ben Dooks37e5ffa2008-04-15 11:36:22 +0100854
855 if (set->disable_ecc)
856 chip->ecc.mode = NAND_ECC_NONE;
Andy Green8c3e8432009-05-10 15:41:25 -0500857
858 switch (chip->ecc.mode) {
859 case NAND_ECC_NONE:
860 dev_info(info->device, "NAND ECC disabled\n");
861 break;
862 case NAND_ECC_SOFT:
863 dev_info(info->device, "NAND soft ECC\n");
864 break;
865 case NAND_ECC_HW:
866 dev_info(info->device, "NAND hardware ECC\n");
867 break;
868 default:
869 dev_info(info->device, "NAND ECC UNKNOWN\n");
870 break;
871 }
Michel Pollet9db41f92009-05-13 16:54:14 +0100872
873 /* If you use u-boot BBT creation code, specifying this flag will
874 * let the kernel fish out the BBT from the NAND, and also skip the
875 * full NAND scan that can take 1/2s or so. Little things... */
Brian Norrisa40f7342011-05-31 16:31:22 -0700876 if (set->flash_bbt) {
Brian Norrisbb9ebd42011-05-31 16:31:23 -0700877 chip->bbt_options |= NAND_BBT_USE_FLASH;
Brian Norrisa40f7342011-05-31 16:31:22 -0700878 chip->options |= NAND_SKIP_BBTSCAN;
879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880}
881
Ben Dooks3db72152009-05-30 17:18:15 +0100882/**
883 * s3c2410_nand_update_chip - post probe update
884 * @info: The controller instance.
885 * @nmtd: The driver version of the MTD instance.
Ben Dooks71d54f32008-04-15 11:36:19 +0100886 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200887 * This routine is called after the chip probe has successfully completed
Ben Dooks3db72152009-05-30 17:18:15 +0100888 * and the relevant per-chip information updated. This call ensure that
889 * we update the internal state accordingly.
890 *
891 * The internal state is currently limited to the ECC state information.
892*/
Ben Dooks71d54f32008-04-15 11:36:19 +0100893static void s3c2410_nand_update_chip(struct s3c2410_nand_info *info,
894 struct s3c2410_nand_mtd *nmtd)
895{
896 struct nand_chip *chip = &nmtd->chip;
897
Ben Dooks451d3392008-05-20 17:32:14 +0100898 dev_dbg(info->device, "chip %p => page shift %d\n",
899 chip, chip->page_shift);
Ben Dooks71d54f32008-04-15 11:36:19 +0100900
Andy Green8c3e8432009-05-10 15:41:25 -0500901 if (chip->ecc.mode != NAND_ECC_HW)
902 return;
903
Ben Dooks71d54f32008-04-15 11:36:19 +0100904 /* change the behaviour depending on wether we are using
905 * the large or small page nand device */
906
Andy Green8c3e8432009-05-10 15:41:25 -0500907 if (chip->page_shift > 10) {
908 chip->ecc.size = 256;
909 chip->ecc.bytes = 3;
910 } else {
911 chip->ecc.size = 512;
912 chip->ecc.bytes = 3;
913 chip->ecc.layout = &nand_hw_eccoob;
Ben Dooks71d54f32008-04-15 11:36:19 +0100914 }
915}
916
Ben Dooksec0482e2009-05-30 16:55:29 +0100917/* s3c24xx_nand_probe
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 *
919 * called by device layer when it finds a device matching
920 * one our driver can handled. This code checks to see if
921 * it can allocate all necessary resources then calls the
922 * nand layer to look for devices
923*/
Ben Dooksec0482e2009-05-30 16:55:29 +0100924static int s3c24xx_nand_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
Russell King3ae5eae2005-11-09 22:32:44 +0000926 struct s3c2410_platform_nand *plat = to_nand_plat(pdev);
Ben Dooksec0482e2009-05-30 16:55:29 +0100927 enum s3c_cpu_type cpu_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 struct s3c2410_nand_info *info;
929 struct s3c2410_nand_mtd *nmtd;
930 struct s3c2410_nand_set *sets;
931 struct resource *res;
932 int err = 0;
933 int size;
934 int nr_sets;
935 int setno;
936
Ben Dooksec0482e2009-05-30 16:55:29 +0100937 cpu_type = platform_get_device_id(pdev)->driver_data;
938
Russell King3ae5eae2005-11-09 22:32:44 +0000939 pr_debug("s3c2410_nand_probe(%p)\n", pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Julia Lawallecce2a62010-05-13 22:07:46 +0200941 info = kzalloc(sizeof(*info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 if (info == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000943 dev_err(&pdev->dev, "no memory for flash info\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 err = -ENOMEM;
945 goto exit_error;
946 }
947
Russell King3ae5eae2005-11-09 22:32:44 +0000948 platform_set_drvdata(pdev, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950 spin_lock_init(&info->controller.lock);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100951 init_waitqueue_head(&info->controller.wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 /* get the clock source and enable it */
954
Russell King3ae5eae2005-11-09 22:32:44 +0000955 info->clk = clk_get(&pdev->dev, "nand");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 if (IS_ERR(info->clk)) {
Joe Perches898eb712007-10-18 03:06:30 -0700957 dev_err(&pdev->dev, "failed to get clock\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 err = -ENOENT;
959 goto exit_error;
960 }
961
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200962 s3c2410_nand_clk_set_state(info, CLOCK_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 /* allocate and map the resource */
965
Ben Dooksa4f957f2005-06-20 12:48:25 +0100966 /* currently we assume we have the one resource */
967 res = pdev->resource;
H Hartley Sweetenfc161c42009-12-14 16:56:22 -0500968 size = resource_size(res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970 info->area = request_mem_region(res->start, size, pdev->name);
971
972 if (info->area == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000973 dev_err(&pdev->dev, "cannot reserve register region\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 err = -ENOENT;
975 goto exit_error;
976 }
977
Russell King3ae5eae2005-11-09 22:32:44 +0000978 info->device = &pdev->dev;
Ben Dooksa4f957f2005-06-20 12:48:25 +0100979 info->platform = plat;
980 info->regs = ioremap(res->start, size);
Ben Dooks2c06a082006-06-27 14:35:46 +0100981 info->cpu_type = cpu_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983 if (info->regs == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000984 dev_err(&pdev->dev, "cannot reserve register region\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 err = -EIO;
986 goto exit_error;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Russell King3ae5eae2005-11-09 22:32:44 +0000989 dev_dbg(&pdev->dev, "mapped registers at %p\n", info->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 /* initialise the hardware */
992
Ben Dooks30821fe2008-07-15 11:58:31 +0100993 err = s3c2410_nand_inithw(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 if (err != 0)
995 goto exit_error;
996
997 sets = (plat != NULL) ? plat->sets : NULL;
998 nr_sets = (plat != NULL) ? plat->nr_sets : 1;
999
1000 info->mtd_count = nr_sets;
1001
1002 /* allocate our information */
1003
1004 size = nr_sets * sizeof(*info->mtds);
Julia Lawallecce2a62010-05-13 22:07:46 +02001005 info->mtds = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 if (info->mtds == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +00001007 dev_err(&pdev->dev, "failed to allocate mtd storage\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 err = -ENOMEM;
1009 goto exit_error;
1010 }
1011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 /* initialise all possible chips */
1013
1014 nmtd = info->mtds;
1015
1016 for (setno = 0; setno < nr_sets; setno++, nmtd++) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001017 pr_debug("initialising set %d (%p, info %p)\n", setno, nmtd, info);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 s3c2410_nand_init_chip(info, nmtd, sets);
1020
Ben Dooks71d54f32008-04-15 11:36:19 +01001021 nmtd->scan_res = nand_scan_ident(&nmtd->mtd,
David Woodhouse5e81e882010-02-26 18:32:56 +00001022 (sets) ? sets->nr_chips : 1,
1023 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025 if (nmtd->scan_res == 0) {
Ben Dooks71d54f32008-04-15 11:36:19 +01001026 s3c2410_nand_update_chip(info, nmtd);
1027 nand_scan_tail(&nmtd->mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 s3c2410_nand_add_partition(info, nmtd, sets);
1029 }
1030
1031 if (sets != NULL)
1032 sets++;
1033 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001034
Ben Dooks30821fe2008-07-15 11:58:31 +01001035 err = s3c2410_nand_cpufreq_register(info);
1036 if (err < 0) {
1037 dev_err(&pdev->dev, "failed to init cpufreq support\n");
1038 goto exit_error;
1039 }
1040
Jiri Pinkavaac497c12011-04-13 11:59:30 +02001041 if (allow_clk_suspend(info)) {
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001042 dev_info(&pdev->dev, "clock idle support enabled\n");
Jiri Pinkavaac497c12011-04-13 11:59:30 +02001043 s3c2410_nand_clk_set_state(info, CLOCK_SUSPEND);
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001044 }
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 pr_debug("initialised ok\n");
1047 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
Ben Dooksec0482e2009-05-30 16:55:29 +01001109static struct platform_device_id s3c24xx_driver_ids[] = {
1110 {
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 .owner = THIS_MODULE,
1137 },
1138};
1139
Sachin Kamat056fcab2012-07-16 16:02:22 +05301140module_platform_driver(s3c24xx_nand_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
1142MODULE_LICENSE("GPL");
1143MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
Ben Dooksa4f957f2005-06-20 12:48:25 +01001144MODULE_DESCRIPTION("S3C24XX MTD NAND driver");