blob: 8ae9399fb4c8251b9352591cd120b4e92361dba9 [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>
Sachin Kamatd2a89be2012-07-16 16:02:24 +053035#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/ioport.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010037#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/delay.h>
39#include <linux/err.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080040#include <linux/slab.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000041#include <linux/clk.h>
Ben Dooks30821fe2008-07-15 11:58:31 +010042#include <linux/cpufreq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#include <linux/mtd/mtd.h>
45#include <linux/mtd/nand.h>
46#include <linux/mtd/nand_ecc.h>
47#include <linux/mtd/partitions.h>
48
Ben Dooks7926b5a2008-10-30 10:14:35 +000049#include <plat/regs-nand.h>
50#include <plat/nand.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/* new oob placement block for use with hardware ecc generation
53 */
54
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020055static struct nand_ecclayout nand_hw_eccoob = {
David Woodhousee0c7d762006-05-13 18:07:53 +010056 .eccbytes = 3,
57 .eccpos = {0, 1, 2},
58 .oobfree = {{8, 8}}
Linus Torvalds1da177e2005-04-16 15:20:36 -070059};
60
61/* controller and mtd information */
62
63struct s3c2410_nand_info;
64
Ben Dooks3db72152009-05-30 17:18:15 +010065/**
66 * struct s3c2410_nand_mtd - driver MTD structure
67 * @mtd: The MTD instance to pass to the MTD layer.
68 * @chip: The NAND chip information.
69 * @set: The platform information supplied for this set of NAND chips.
70 * @info: Link back to the hardware information.
71 * @scan_res: The result from calling nand_scan_ident().
72*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070073struct s3c2410_nand_mtd {
74 struct mtd_info mtd;
75 struct nand_chip chip;
76 struct s3c2410_nand_set *set;
77 struct s3c2410_nand_info *info;
78 int scan_res;
79};
80
Ben Dooks2c06a082006-06-27 14:35:46 +010081enum s3c_cpu_type {
82 TYPE_S3C2410,
83 TYPE_S3C2412,
84 TYPE_S3C2440,
85};
86
Jiri Pinkavaac497c12011-04-13 11:59:30 +020087enum s3c_nand_clk_state {
88 CLOCK_DISABLE = 0,
89 CLOCK_ENABLE,
90 CLOCK_SUSPEND,
91};
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093/* overview of the s3c2410 nand state */
94
Ben Dooks3db72152009-05-30 17:18:15 +010095/**
96 * struct s3c2410_nand_info - NAND controller state.
97 * @mtds: An array of MTD instances on this controoler.
98 * @platform: The platform data for this board.
99 * @device: The platform device we bound to.
100 * @area: The IO area resource that came from request_mem_region().
101 * @clk: The clock resource for this controller.
102 * @regs: The area mapped for the hardware registers described by @area.
103 * @sel_reg: Pointer to the register controlling the NAND selection.
104 * @sel_bit: The bit in @sel_reg to select the NAND chip.
105 * @mtd_count: The number of MTDs created from this controller.
106 * @save_sel: The contents of @sel_reg to be saved over suspend.
107 * @clk_rate: The clock rate from @clk.
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200108 * @clk_state: The current clock state.
Ben Dooks3db72152009-05-30 17:18:15 +0100109 * @cpu_type: The exact type of this controller.
110 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111struct s3c2410_nand_info {
112 /* mtd info */
113 struct nand_hw_control controller;
114 struct s3c2410_nand_mtd *mtds;
115 struct s3c2410_platform_nand *platform;
116
117 /* device info */
118 struct device *device;
119 struct resource *area;
120 struct clk *clk;
Ben Dooksfdf2fd52005-02-18 14:46:15 +0000121 void __iomem *regs;
Ben Dooks2c06a082006-06-27 14:35:46 +0100122 void __iomem *sel_reg;
123 int sel_bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 int mtd_count;
Ben Dooks09160832008-04-15 11:36:18 +0100125 unsigned long save_sel;
Ben Dooks30821fe2008-07-15 11:58:31 +0100126 unsigned long clk_rate;
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200127 enum s3c_nand_clk_state clk_state;
Ben Dooks03680b12007-11-19 23:28:07 +0000128
Ben Dooks2c06a082006-06-27 14:35:46 +0100129 enum s3c_cpu_type cpu_type;
Ben Dooks30821fe2008-07-15 11:58:31 +0100130
131#ifdef CONFIG_CPU_FREQ
132 struct notifier_block freq_transition;
133#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134};
135
136/* conversion functions */
137
138static struct s3c2410_nand_mtd *s3c2410_nand_mtd_toours(struct mtd_info *mtd)
139{
140 return container_of(mtd, struct s3c2410_nand_mtd, mtd);
141}
142
143static struct s3c2410_nand_info *s3c2410_nand_mtd_toinfo(struct mtd_info *mtd)
144{
145 return s3c2410_nand_mtd_toours(mtd)->info;
146}
147
Russell King3ae5eae2005-11-09 22:32:44 +0000148static struct s3c2410_nand_info *to_nand_info(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Russell King3ae5eae2005-11-09 22:32:44 +0000150 return platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
Russell King3ae5eae2005-11-09 22:32:44 +0000153static struct s3c2410_platform_nand *to_nand_plat(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Russell King3ae5eae2005-11-09 22:32:44 +0000155 return dev->dev.platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200158static inline int allow_clk_suspend(struct s3c2410_nand_info *info)
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100159{
Sachin Kamata68c5ec2012-07-16 16:02:25 +0530160#ifdef CONFIG_MTD_NAND_S3C2410_CLKSTOP
161 return 1;
162#else
163 return 0;
164#endif
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100165}
166
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200167/**
168 * s3c2410_nand_clk_set_state - Enable, disable or suspend NAND clock.
169 * @info: The controller instance.
170 * @new_state: State to which clock should be set.
171 */
172static void s3c2410_nand_clk_set_state(struct s3c2410_nand_info *info,
173 enum s3c_nand_clk_state new_state)
174{
175 if (!allow_clk_suspend(info) && new_state == CLOCK_SUSPEND)
176 return;
177
178 if (info->clk_state == CLOCK_ENABLE) {
179 if (new_state != CLOCK_ENABLE)
180 clk_disable(info->clk);
181 } else {
182 if (new_state == CLOCK_ENABLE)
183 clk_enable(info->clk);
184 }
185
186 info->clk_state = new_state;
187}
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189/* timing calculations */
190
Ben Dookscfd320f2005-10-20 22:22:58 +0100191#define NS_IN_KHZ 1000000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Ben Dooks3db72152009-05-30 17:18:15 +0100193/**
194 * s3c_nand_calc_rate - calculate timing data.
195 * @wanted: The cycle time in nanoseconds.
196 * @clk: The clock rate in kHz.
197 * @max: The maximum divider value.
198 *
199 * Calculate the timing value from the given parameters.
200 */
Ben Dooks2c06a082006-06-27 14:35:46 +0100201static int s3c_nand_calc_rate(int wanted, unsigned long clk, int max)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 int result;
204
Ben Dooks947391c2009-05-30 18:34:16 +0100205 result = DIV_ROUND_UP((wanted * clk), NS_IN_KHZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 pr_debug("result %d from %ld, %d\n", result, clk, wanted);
208
209 if (result > max) {
Sachin Kamat92aeb5d2012-07-16 16:02:23 +0530210 pr_err("%d ns is too big for current clock rate %ld\n",
211 wanted, clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 return -1;
213 }
214
215 if (result < 1)
216 result = 1;
217
218 return result;
219}
220
Sachin Kamat54cd0202012-07-16 16:02:26 +0530221#define to_ns(ticks, clk) (((ticks) * NS_IN_KHZ) / (unsigned int)(clk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223/* controller setup */
224
Ben Dooks3db72152009-05-30 17:18:15 +0100225/**
226 * s3c2410_nand_setrate - setup controller timing information.
227 * @info: The controller instance.
228 *
229 * Given the information supplied by the platform, calculate and set
230 * the necessary timing registers in the hardware to generate the
231 * necessary timing cycles to the hardware.
232 */
Ben Dooks30821fe2008-07-15 11:58:31 +0100233static int s3c2410_nand_setrate(struct s3c2410_nand_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
Ben Dooks30821fe2008-07-15 11:58:31 +0100235 struct s3c2410_platform_nand *plat = info->platform;
Ben Dooks2c06a082006-06-27 14:35:46 +0100236 int tacls_max = (info->cpu_type == TYPE_S3C2412) ? 8 : 4;
Ben Dookscfd320f2005-10-20 22:22:58 +0100237 int tacls, twrph0, twrph1;
Ben Dooks30821fe2008-07-15 11:58:31 +0100238 unsigned long clkrate = clk_get_rate(info->clk);
Nelson Castillo2612e522009-05-10 15:41:54 -0500239 unsigned long uninitialized_var(set), cfg, uninitialized_var(mask);
Ben Dooks30821fe2008-07-15 11:58:31 +0100240 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 /* calculate the timing information for the controller */
243
Ben Dooks30821fe2008-07-15 11:58:31 +0100244 info->clk_rate = clkrate;
Ben Dookscfd320f2005-10-20 22:22:58 +0100245 clkrate /= 1000; /* turn clock into kHz for ease of use */
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (plat != NULL) {
Ben Dooks2c06a082006-06-27 14:35:46 +0100248 tacls = s3c_nand_calc_rate(plat->tacls, clkrate, tacls_max);
249 twrph0 = s3c_nand_calc_rate(plat->twrph0, clkrate, 8);
250 twrph1 = s3c_nand_calc_rate(plat->twrph1, clkrate, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 } else {
252 /* default timings */
Ben Dooks2c06a082006-06-27 14:35:46 +0100253 tacls = tacls_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 twrph0 = 8;
255 twrph1 = 8;
256 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (tacls < 0 || twrph0 < 0 || twrph1 < 0) {
Ben Dooks99974c62006-06-21 15:43:05 +0100259 dev_err(info->device, "cannot get suitable timings\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return -EINVAL;
261 }
262
Ben Dooks99974c62006-06-21 15:43:05 +0100263 dev_info(info->device, "Tacls=%d, %dns Twrph0=%d %dns, Twrph1=%d %dns\n",
Sachin Kamat54cd0202012-07-16 16:02:26 +0530264 tacls, to_ns(tacls, clkrate), twrph0, to_ns(twrph0, clkrate),
265 twrph1, to_ns(twrph1, clkrate));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Ben Dooks30821fe2008-07-15 11:58:31 +0100267 switch (info->cpu_type) {
268 case TYPE_S3C2410:
269 mask = (S3C2410_NFCONF_TACLS(3) |
270 S3C2410_NFCONF_TWRPH0(7) |
271 S3C2410_NFCONF_TWRPH1(7));
272 set = S3C2410_NFCONF_EN;
273 set |= S3C2410_NFCONF_TACLS(tacls - 1);
274 set |= S3C2410_NFCONF_TWRPH0(twrph0 - 1);
275 set |= S3C2410_NFCONF_TWRPH1(twrph1 - 1);
276 break;
277
278 case TYPE_S3C2440:
279 case TYPE_S3C2412:
Peter Korsgaarda755a382009-06-03 13:46:54 +0200280 mask = (S3C2440_NFCONF_TACLS(tacls_max - 1) |
281 S3C2440_NFCONF_TWRPH0(7) |
282 S3C2440_NFCONF_TWRPH1(7));
Ben Dooks30821fe2008-07-15 11:58:31 +0100283
284 set = S3C2440_NFCONF_TACLS(tacls - 1);
285 set |= S3C2440_NFCONF_TWRPH0(twrph0 - 1);
286 set |= S3C2440_NFCONF_TWRPH1(twrph1 - 1);
287 break;
288
289 default:
Ben Dooks30821fe2008-07-15 11:58:31 +0100290 BUG();
291 }
292
Ben Dooks30821fe2008-07-15 11:58:31 +0100293 local_irq_save(flags);
294
295 cfg = readl(info->regs + S3C2410_NFCONF);
296 cfg &= ~mask;
297 cfg |= set;
298 writel(cfg, info->regs + S3C2410_NFCONF);
299
300 local_irq_restore(flags);
301
Andy Greenae7304e2009-05-10 15:42:02 -0500302 dev_dbg(info->device, "NF_CONF is 0x%lx\n", cfg);
303
Ben Dooks30821fe2008-07-15 11:58:31 +0100304 return 0;
305}
306
Ben Dooks3db72152009-05-30 17:18:15 +0100307/**
308 * s3c2410_nand_inithw - basic hardware initialisation
309 * @info: The hardware state.
310 *
311 * Do the basic initialisation of the hardware, using s3c2410_nand_setrate()
312 * to setup the hardware access speeds and set the controller to be enabled.
313*/
Ben Dooks30821fe2008-07-15 11:58:31 +0100314static int s3c2410_nand_inithw(struct s3c2410_nand_info *info)
315{
316 int ret;
317
318 ret = s3c2410_nand_setrate(info);
319 if (ret < 0)
320 return ret;
321
Sachin Kamat54cd0202012-07-16 16:02:26 +0530322 switch (info->cpu_type) {
323 case TYPE_S3C2410:
Ben Dooks30821fe2008-07-15 11:58:31 +0100324 default:
Ben Dooks2c06a082006-06-27 14:35:46 +0100325 break;
326
Sachin Kamat54cd0202012-07-16 16:02:26 +0530327 case TYPE_S3C2440:
328 case TYPE_S3C2412:
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100329 /* enable the controller and de-assert nFCE */
330
Ben Dooks2c06a082006-06-27 14:35:46 +0100331 writel(S3C2440_NFCONT_ENABLE, info->regs + S3C2440_NFCONT);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return 0;
335}
336
Ben Dooks3db72152009-05-30 17:18:15 +0100337/**
338 * s3c2410_nand_select_chip - select the given nand chip
339 * @mtd: The MTD instance for this chip.
340 * @chip: The chip number.
341 *
342 * This is called by the MTD layer to either select a given chip for the
343 * @mtd instance, or to indicate that the access has finished and the
344 * chip can be de-selected.
345 *
346 * The routine ensures that the nFCE line is correctly setup, and any
347 * platform specific selection code is called to route nFCE to the specific
348 * chip.
349 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350static void s3c2410_nand_select_chip(struct mtd_info *mtd, int chip)
351{
352 struct s3c2410_nand_info *info;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000353 struct s3c2410_nand_mtd *nmtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 struct nand_chip *this = mtd->priv;
355 unsigned long cur;
356
357 nmtd = this->priv;
358 info = nmtd->info;
359
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200360 if (chip != -1)
361 s3c2410_nand_clk_set_state(info, CLOCK_ENABLE);
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100362
Ben Dooks2c06a082006-06-27 14:35:46 +0100363 cur = readl(info->sel_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 if (chip == -1) {
Ben Dooks2c06a082006-06-27 14:35:46 +0100366 cur |= info->sel_bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 } else {
Ben Dooksfb8d82a2005-07-06 21:05:10 +0100368 if (nmtd->set != NULL && chip > nmtd->set->nr_chips) {
Ben Dooks99974c62006-06-21 15:43:05 +0100369 dev_err(info->device, "invalid chip %d\n", chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return;
371 }
372
373 if (info->platform != NULL) {
374 if (info->platform->select_chip != NULL)
David Woodhousee0c7d762006-05-13 18:07:53 +0100375 (info->platform->select_chip) (nmtd->set, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
377
Ben Dooks2c06a082006-06-27 14:35:46 +0100378 cur &= ~info->sel_bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
380
Ben Dooks2c06a082006-06-27 14:35:46 +0100381 writel(cur, info->sel_reg);
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100382
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200383 if (chip == -1)
384 s3c2410_nand_clk_set_state(info, CLOCK_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
386
Ben Dooksad3b5fb2006-06-19 09:43:23 +0100387/* s3c2410_nand_hwcontrol
Ben Dooksa4f957f2005-06-20 12:48:25 +0100388 *
Ben Dooksad3b5fb2006-06-19 09:43:23 +0100389 * Issue command and address cycles to the chip
Ben Dooksa4f957f2005-06-20 12:48:25 +0100390*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200392static void s3c2410_nand_hwcontrol(struct mtd_info *mtd, int cmd,
David Woodhousef9068872006-06-10 00:53:16 +0100393 unsigned int ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
395 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
David Woodhousec9ac5972006-11-30 08:17:38 +0000396
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200397 if (cmd == NAND_CMD_NONE)
398 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
David Woodhousef9068872006-06-10 00:53:16 +0100400 if (ctrl & NAND_CLE)
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200401 writeb(cmd, info->regs + S3C2410_NFCMD);
402 else
403 writeb(cmd, info->regs + S3C2410_NFADDR);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100404}
405
406/* command and control functions */
407
David Woodhousef9068872006-06-10 00:53:16 +0100408static void s3c2440_nand_hwcontrol(struct mtd_info *mtd, int cmd,
409 unsigned int ctrl)
Ben Dooksa4f957f2005-06-20 12:48:25 +0100410{
411 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100412
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200413 if (cmd == NAND_CMD_NONE)
414 return;
Ben Dooksa4f957f2005-06-20 12:48:25 +0100415
David Woodhousef9068872006-06-10 00:53:16 +0100416 if (ctrl & NAND_CLE)
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200417 writeb(cmd, info->regs + S3C2440_NFCMD);
418 else
419 writeb(cmd, info->regs + S3C2440_NFADDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422/* s3c2410_nand_devready()
423 *
424 * returns 0 if the nand is busy, 1 if it is ready
425*/
426
427static int s3c2410_nand_devready(struct mtd_info *mtd)
428{
429 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return readb(info->regs + S3C2410_NFSTAT) & S3C2410_NFSTAT_BUSY;
431}
432
Ben Dooks2c06a082006-06-27 14:35:46 +0100433static int s3c2440_nand_devready(struct mtd_info *mtd)
434{
435 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
436 return readb(info->regs + S3C2440_NFSTAT) & S3C2440_NFSTAT_READY;
437}
438
439static int s3c2412_nand_devready(struct mtd_info *mtd)
440{
441 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
442 return readb(info->regs + S3C2412_NFSTAT) & S3C2412_NFSTAT_READY;
443}
444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445/* ECC handling functions */
446
Ben Dooks2c06a082006-06-27 14:35:46 +0100447static int s3c2410_nand_correct_data(struct mtd_info *mtd, u_char *dat,
448 u_char *read_ecc, u_char *calc_ecc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Ben Dooksa2593242007-02-02 16:59:33 +0000450 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
451 unsigned int diff0, diff1, diff2;
452 unsigned int bit, byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Ben Dooksa2593242007-02-02 16:59:33 +0000454 pr_debug("%s(%p,%p,%p,%p)\n", __func__, mtd, dat, read_ecc, calc_ecc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Ben Dooksa2593242007-02-02 16:59:33 +0000456 diff0 = read_ecc[0] ^ calc_ecc[0];
457 diff1 = read_ecc[1] ^ calc_ecc[1];
458 diff2 = read_ecc[2] ^ calc_ecc[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Andy Shevchenko13e85972012-08-02 16:06:47 +0300460 pr_debug("%s: rd %*phN calc %*phN diff %02x%02x%02x\n",
461 __func__, 3, read_ecc, 3, calc_ecc,
Ben Dooksa2593242007-02-02 16:59:33 +0000462 diff0, diff1, diff2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Ben Dooksa2593242007-02-02 16:59:33 +0000464 if (diff0 == 0 && diff1 == 0 && diff2 == 0)
465 return 0; /* ECC is ok */
466
Ben Dooksc45c6c62008-04-15 11:36:20 +0100467 /* sometimes people do not think about using the ECC, so check
468 * to see if we have an 0xff,0xff,0xff read ECC and then ignore
469 * the error, on the assumption that this is an un-eccd page.
470 */
471 if (read_ecc[0] == 0xff && read_ecc[1] == 0xff && read_ecc[2] == 0xff
472 && info->platform->ignore_unset_ecc)
473 return 0;
474
Ben Dooksa2593242007-02-02 16:59:33 +0000475 /* Can we correct this ECC (ie, one row and column change).
476 * Note, this is similar to the 256 error code on smartmedia */
477
478 if (((diff0 ^ (diff0 >> 1)) & 0x55) == 0x55 &&
479 ((diff1 ^ (diff1 >> 1)) & 0x55) == 0x55 &&
480 ((diff2 ^ (diff2 >> 1)) & 0x55) == 0x55) {
481 /* calculate the bit position of the error */
482
Matt Reimerd0bf3792007-10-18 18:02:43 -0700483 bit = ((diff2 >> 3) & 1) |
484 ((diff2 >> 4) & 2) |
485 ((diff2 >> 5) & 4);
Ben Dooksa2593242007-02-02 16:59:33 +0000486
487 /* calculate the byte position of the error */
488
Matt Reimerd0bf3792007-10-18 18:02:43 -0700489 byte = ((diff2 << 7) & 0x100) |
490 ((diff1 << 0) & 0x80) |
491 ((diff1 << 1) & 0x40) |
492 ((diff1 << 2) & 0x20) |
493 ((diff1 << 3) & 0x10) |
494 ((diff0 >> 4) & 0x08) |
495 ((diff0 >> 3) & 0x04) |
496 ((diff0 >> 2) & 0x02) |
497 ((diff0 >> 1) & 0x01);
Ben Dooksa2593242007-02-02 16:59:33 +0000498
499 dev_dbg(info->device, "correcting error bit %d, byte %d\n",
500 bit, byte);
501
502 dat[byte] ^= (1 << bit);
503 return 1;
504 }
505
506 /* if there is only one bit difference in the ECC, then
507 * one of only a row or column parity has changed, which
508 * means the error is most probably in the ECC itself */
509
510 diff0 |= (diff1 << 8);
511 diff0 |= (diff2 << 16);
512
513 if ((diff0 & ~(1<<fls(diff0))) == 0)
514 return 1;
515
Matt Reimer4fac9f62007-10-18 18:02:44 -0700516 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517}
518
Ben Dooksa4f957f2005-06-20 12:48:25 +0100519/* ECC functions
520 *
521 * These allow the s3c2410 and s3c2440 to use the controller's ECC
522 * generator block to ECC the data as it passes through]
523*/
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525static void s3c2410_nand_enable_hwecc(struct mtd_info *mtd, int mode)
526{
527 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
528 unsigned long ctrl;
529
530 ctrl = readl(info->regs + S3C2410_NFCONF);
531 ctrl |= S3C2410_NFCONF_INITECC;
532 writel(ctrl, info->regs + S3C2410_NFCONF);
533}
534
Matthieu CASTET4f659922007-02-13 12:30:38 +0100535static void s3c2412_nand_enable_hwecc(struct mtd_info *mtd, int mode)
536{
537 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
538 unsigned long ctrl;
539
540 ctrl = readl(info->regs + S3C2440_NFCONT);
Sachin Kamatf938bc52012-08-21 10:21:15 +0530541 writel(ctrl | S3C2412_NFCONT_INIT_MAIN_ECC,
542 info->regs + S3C2440_NFCONT);
Matthieu CASTET4f659922007-02-13 12:30:38 +0100543}
544
Ben Dooksa4f957f2005-06-20 12:48:25 +0100545static void s3c2440_nand_enable_hwecc(struct mtd_info *mtd, int mode)
546{
547 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
548 unsigned long ctrl;
549
550 ctrl = readl(info->regs + S3C2440_NFCONT);
551 writel(ctrl | S3C2440_NFCONT_INITECC, info->regs + S3C2440_NFCONT);
552}
553
Sachin Kamatf938bc52012-08-21 10:21:15 +0530554static int s3c2410_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
555 u_char *ecc_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
557 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
558
559 ecc_code[0] = readb(info->regs + S3C2410_NFECC + 0);
560 ecc_code[1] = readb(info->regs + S3C2410_NFECC + 1);
561 ecc_code[2] = readb(info->regs + S3C2410_NFECC + 2);
562
Andy Shevchenko13e85972012-08-02 16:06:47 +0300563 pr_debug("%s: returning ecc %*phN\n", __func__, 3, ecc_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 return 0;
566}
567
Sachin Kamatf938bc52012-08-21 10:21:15 +0530568static int s3c2412_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
569 u_char *ecc_code)
Matthieu CASTET4f659922007-02-13 12:30:38 +0100570{
571 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
572 unsigned long ecc = readl(info->regs + S3C2412_NFMECC0);
573
574 ecc_code[0] = ecc;
575 ecc_code[1] = ecc >> 8;
576 ecc_code[2] = ecc >> 16;
577
Andy Shevchenko13e85972012-08-02 16:06:47 +0300578 pr_debug("%s: returning ecc %*phN\n", __func__, 3, ecc_code);
Matthieu CASTET4f659922007-02-13 12:30:38 +0100579
580 return 0;
581}
582
Sachin Kamatf938bc52012-08-21 10:21:15 +0530583static int s3c2440_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
584 u_char *ecc_code)
Ben Dooksa4f957f2005-06-20 12:48:25 +0100585{
586 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
587 unsigned long ecc = readl(info->regs + S3C2440_NFMECC0);
588
589 ecc_code[0] = ecc;
590 ecc_code[1] = ecc >> 8;
591 ecc_code[2] = ecc >> 16;
592
Ben Dooks71d54f32008-04-15 11:36:19 +0100593 pr_debug("%s: returning ecc %06lx\n", __func__, ecc & 0xffffff);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100594
595 return 0;
596}
597
Ben Dooksa4f957f2005-06-20 12:48:25 +0100598/* over-ride the standard functions for a little more speed. We can
599 * use read/write block to move the data buffers to/from the controller
600*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602static void s3c2410_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
603{
604 struct nand_chip *this = mtd->priv;
605 readsb(this->IO_ADDR_R, buf, len);
606}
607
Matt Reimerb773bb22007-10-18 17:43:07 -0700608static void s3c2440_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
609{
610 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
Ben Dooksdea2aa62009-05-30 18:30:18 +0100611
612 readsl(info->regs + S3C2440_NFDATA, buf, len >> 2);
613
614 /* cleanup if we've got less than a word to do */
615 if (len & 3) {
616 buf += len & ~3;
617
618 for (; len & 3; len--)
619 *buf++ = readb(info->regs + S3C2440_NFDATA);
620 }
Matt Reimerb773bb22007-10-18 17:43:07 -0700621}
622
Sachin Kamatf938bc52012-08-21 10:21:15 +0530623static void s3c2410_nand_write_buf(struct mtd_info *mtd, const u_char *buf,
624 int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
626 struct nand_chip *this = mtd->priv;
627 writesb(this->IO_ADDR_W, buf, len);
628}
629
Sachin Kamatf938bc52012-08-21 10:21:15 +0530630static void s3c2440_nand_write_buf(struct mtd_info *mtd, const u_char *buf,
631 int len)
Matt Reimerb773bb22007-10-18 17:43:07 -0700632{
633 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
Ben Dooksdea2aa62009-05-30 18:30:18 +0100634
635 writesl(info->regs + S3C2440_NFDATA, buf, len >> 2);
636
637 /* cleanup any fractional write */
638 if (len & 3) {
639 buf += len & ~3;
640
641 for (; len & 3; len--, buf++)
642 writeb(*buf, info->regs + S3C2440_NFDATA);
643 }
Matt Reimerb773bb22007-10-18 17:43:07 -0700644}
645
Ben Dooks30821fe2008-07-15 11:58:31 +0100646/* cpufreq driver support */
647
648#ifdef CONFIG_CPU_FREQ
649
650static int s3c2410_nand_cpufreq_transition(struct notifier_block *nb,
651 unsigned long val, void *data)
652{
653 struct s3c2410_nand_info *info;
654 unsigned long newclk;
655
656 info = container_of(nb, struct s3c2410_nand_info, freq_transition);
657 newclk = clk_get_rate(info->clk);
658
659 if ((val == CPUFREQ_POSTCHANGE && newclk < info->clk_rate) ||
660 (val == CPUFREQ_PRECHANGE && newclk > info->clk_rate)) {
661 s3c2410_nand_setrate(info);
662 }
663
664 return 0;
665}
666
667static inline int s3c2410_nand_cpufreq_register(struct s3c2410_nand_info *info)
668{
669 info->freq_transition.notifier_call = s3c2410_nand_cpufreq_transition;
670
671 return cpufreq_register_notifier(&info->freq_transition,
672 CPUFREQ_TRANSITION_NOTIFIER);
673}
674
Sachin Kamatf938bc52012-08-21 10:21:15 +0530675static inline void
676s3c2410_nand_cpufreq_deregister(struct s3c2410_nand_info *info)
Ben Dooks30821fe2008-07-15 11:58:31 +0100677{
678 cpufreq_unregister_notifier(&info->freq_transition,
679 CPUFREQ_TRANSITION_NOTIFIER);
680}
681
682#else
683static inline int s3c2410_nand_cpufreq_register(struct s3c2410_nand_info *info)
684{
685 return 0;
686}
687
Sachin Kamatf938bc52012-08-21 10:21:15 +0530688static inline void
689s3c2410_nand_cpufreq_deregister(struct s3c2410_nand_info *info)
Ben Dooks30821fe2008-07-15 11:58:31 +0100690{
691}
692#endif
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694/* device management functions */
695
Ben Dooksec0482e2009-05-30 16:55:29 +0100696static int s3c24xx_nand_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
Russell King3ae5eae2005-11-09 22:32:44 +0000698 struct s3c2410_nand_info *info = to_nand_info(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Russell King3ae5eae2005-11-09 22:32:44 +0000700 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000702 if (info == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 return 0;
704
Ben Dooks30821fe2008-07-15 11:58:31 +0100705 s3c2410_nand_cpufreq_deregister(info);
706
707 /* Release all our mtds and their partitions, then go through
708 * freeing the resources used
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if (info->mtds != NULL) {
712 struct s3c2410_nand_mtd *ptr = info->mtds;
713 int mtdno;
714
715 for (mtdno = 0; mtdno < info->mtd_count; mtdno++, ptr++) {
716 pr_debug("releasing mtd %d (%p)\n", mtdno, ptr);
717 nand_release(&ptr->mtd);
718 }
719
720 kfree(info->mtds);
721 }
722
723 /* free the common resources */
724
Jonghwan Choi4aa10622011-07-21 15:33:58 +0900725 if (!IS_ERR(info->clk)) {
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200726 s3c2410_nand_clk_set_state(info, CLOCK_DISABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 clk_put(info->clk);
728 }
729
730 if (info->regs != NULL) {
731 iounmap(info->regs);
732 info->regs = NULL;
733 }
734
735 if (info->area != NULL) {
736 release_resource(info->area);
737 kfree(info->area);
738 info->area = NULL;
739 }
740
741 kfree(info);
742
743 return 0;
744}
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746static int s3c2410_nand_add_partition(struct s3c2410_nand_info *info,
747 struct s3c2410_nand_mtd *mtd,
748 struct s3c2410_nand_set *set)
749{
Dmitry Eremin-Solenikov599501a2011-06-02 18:01:02 +0400750 if (set)
751 mtd->mtd.name = set->name;
Andy Greened27f022009-05-10 15:42:09 -0500752
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +0200753 return mtd_device_parse_register(&mtd->mtd, NULL, NULL,
754 set->partitions, set->nr_partitions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Ben Dooks3db72152009-05-30 17:18:15 +0100757/**
758 * s3c2410_nand_init_chip - initialise a single instance of an chip
759 * @info: The base NAND controller the chip is on.
760 * @nmtd: The new controller MTD instance to fill in.
761 * @set: The information passed from the board specific platform data.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 *
Ben Dooks3db72152009-05-30 17:18:15 +0100763 * Initialise the given @nmtd from the information in @info and @set. This
764 * readies the structure for use with the MTD layer functions by ensuring
765 * all pointers are setup and the necessary control routines selected.
766 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767static void s3c2410_nand_init_chip(struct s3c2410_nand_info *info,
768 struct s3c2410_nand_mtd *nmtd,
769 struct s3c2410_nand_set *set)
770{
771 struct nand_chip *chip = &nmtd->chip;
Ben Dooks2c06a082006-06-27 14:35:46 +0100772 void __iomem *regs = info->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 chip->write_buf = s3c2410_nand_write_buf;
775 chip->read_buf = s3c2410_nand_read_buf;
776 chip->select_chip = s3c2410_nand_select_chip;
777 chip->chip_delay = 50;
778 chip->priv = nmtd;
Ben Dooks74218fe2009-11-02 18:12:51 +0000779 chip->options = set->options;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 chip->controller = &info->controller;
781
Ben Dooks2c06a082006-06-27 14:35:46 +0100782 switch (info->cpu_type) {
783 case TYPE_S3C2410:
784 chip->IO_ADDR_W = regs + S3C2410_NFDATA;
785 info->sel_reg = regs + S3C2410_NFCONF;
786 info->sel_bit = S3C2410_NFCONF_nFCE;
787 chip->cmd_ctrl = s3c2410_nand_hwcontrol;
788 chip->dev_ready = s3c2410_nand_devready;
789 break;
790
791 case TYPE_S3C2440:
792 chip->IO_ADDR_W = regs + S3C2440_NFDATA;
793 info->sel_reg = regs + S3C2440_NFCONT;
794 info->sel_bit = S3C2440_NFCONT_nFCE;
795 chip->cmd_ctrl = s3c2440_nand_hwcontrol;
796 chip->dev_ready = s3c2440_nand_devready;
Matt Reimerb773bb22007-10-18 17:43:07 -0700797 chip->read_buf = s3c2440_nand_read_buf;
798 chip->write_buf = s3c2440_nand_write_buf;
Ben Dooks2c06a082006-06-27 14:35:46 +0100799 break;
800
801 case TYPE_S3C2412:
802 chip->IO_ADDR_W = regs + S3C2440_NFDATA;
803 info->sel_reg = regs + S3C2440_NFCONT;
804 info->sel_bit = S3C2412_NFCONT_nFCE0;
805 chip->cmd_ctrl = s3c2440_nand_hwcontrol;
806 chip->dev_ready = s3c2412_nand_devready;
807
808 if (readl(regs + S3C2410_NFCONF) & S3C2412_NFCONF_NANDBOOT)
809 dev_info(info->device, "System booted from NAND\n");
810
811 break;
Sachin Kamat54cd0202012-07-16 16:02:26 +0530812 }
Ben Dooks2c06a082006-06-27 14:35:46 +0100813
814 chip->IO_ADDR_R = chip->IO_ADDR_W;
Ben Dooksa4f957f2005-06-20 12:48:25 +0100815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 nmtd->info = info;
817 nmtd->mtd.priv = chip;
David Woodhouse552d9202006-05-14 01:20:46 +0100818 nmtd->mtd.owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 nmtd->set = set;
820
Sachin Kamata68c5ec2012-07-16 16:02:25 +0530821#ifdef CONFIG_MTD_NAND_S3C2410_HWECC
822 chip->ecc.calculate = s3c2410_nand_calculate_ecc;
823 chip->ecc.correct = s3c2410_nand_correct_data;
824 chip->ecc.mode = NAND_ECC_HW;
825 chip->ecc.strength = 1;
826
827 switch (info->cpu_type) {
828 case TYPE_S3C2410:
829 chip->ecc.hwctl = s3c2410_nand_enable_hwecc;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +0200830 chip->ecc.calculate = s3c2410_nand_calculate_ecc;
Sachin Kamata68c5ec2012-07-16 16:02:25 +0530831 break;
Ben Dooksa4f957f2005-06-20 12:48:25 +0100832
Sachin Kamata68c5ec2012-07-16 16:02:25 +0530833 case TYPE_S3C2412:
834 chip->ecc.hwctl = s3c2412_nand_enable_hwecc;
835 chip->ecc.calculate = s3c2412_nand_calculate_ecc;
836 break;
Ben Dooks2c06a082006-06-27 14:35:46 +0100837
Sachin Kamata68c5ec2012-07-16 16:02:25 +0530838 case TYPE_S3C2440:
839 chip->ecc.hwctl = s3c2440_nand_enable_hwecc;
840 chip->ecc.calculate = s3c2440_nand_calculate_ecc;
841 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 }
Sachin Kamata68c5ec2012-07-16 16:02:25 +0530843#else
844 chip->ecc.mode = NAND_ECC_SOFT;
845#endif
Ben Dooks1c21ab62008-04-15 11:36:21 +0100846
847 if (set->ecc_layout != NULL)
848 chip->ecc.layout = set->ecc_layout;
Ben Dooks37e5ffa2008-04-15 11:36:22 +0100849
850 if (set->disable_ecc)
851 chip->ecc.mode = NAND_ECC_NONE;
Andy Green8c3e8432009-05-10 15:41:25 -0500852
853 switch (chip->ecc.mode) {
854 case NAND_ECC_NONE:
855 dev_info(info->device, "NAND ECC disabled\n");
856 break;
857 case NAND_ECC_SOFT:
858 dev_info(info->device, "NAND soft ECC\n");
859 break;
860 case NAND_ECC_HW:
861 dev_info(info->device, "NAND hardware ECC\n");
862 break;
863 default:
864 dev_info(info->device, "NAND ECC UNKNOWN\n");
865 break;
866 }
Michel Pollet9db41f92009-05-13 16:54:14 +0100867
868 /* If you use u-boot BBT creation code, specifying this flag will
869 * let the kernel fish out the BBT from the NAND, and also skip the
870 * full NAND scan that can take 1/2s or so. Little things... */
Brian Norrisa40f7342011-05-31 16:31:22 -0700871 if (set->flash_bbt) {
Brian Norrisbb9ebd42011-05-31 16:31:23 -0700872 chip->bbt_options |= NAND_BBT_USE_FLASH;
Brian Norrisa40f7342011-05-31 16:31:22 -0700873 chip->options |= NAND_SKIP_BBTSCAN;
874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875}
876
Ben Dooks3db72152009-05-30 17:18:15 +0100877/**
878 * s3c2410_nand_update_chip - post probe update
879 * @info: The controller instance.
880 * @nmtd: The driver version of the MTD instance.
Ben Dooks71d54f32008-04-15 11:36:19 +0100881 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200882 * This routine is called after the chip probe has successfully completed
Ben Dooks3db72152009-05-30 17:18:15 +0100883 * and the relevant per-chip information updated. This call ensure that
884 * we update the internal state accordingly.
885 *
886 * The internal state is currently limited to the ECC state information.
887*/
Ben Dooks71d54f32008-04-15 11:36:19 +0100888static void s3c2410_nand_update_chip(struct s3c2410_nand_info *info,
889 struct s3c2410_nand_mtd *nmtd)
890{
891 struct nand_chip *chip = &nmtd->chip;
892
Ben Dooks451d3392008-05-20 17:32:14 +0100893 dev_dbg(info->device, "chip %p => page shift %d\n",
894 chip, chip->page_shift);
Ben Dooks71d54f32008-04-15 11:36:19 +0100895
Andy Green8c3e8432009-05-10 15:41:25 -0500896 if (chip->ecc.mode != NAND_ECC_HW)
897 return;
898
Ben Dooks71d54f32008-04-15 11:36:19 +0100899 /* change the behaviour depending on wether we are using
900 * the large or small page nand device */
901
Andy Green8c3e8432009-05-10 15:41:25 -0500902 if (chip->page_shift > 10) {
903 chip->ecc.size = 256;
904 chip->ecc.bytes = 3;
905 } else {
906 chip->ecc.size = 512;
907 chip->ecc.bytes = 3;
908 chip->ecc.layout = &nand_hw_eccoob;
Ben Dooks71d54f32008-04-15 11:36:19 +0100909 }
910}
911
Ben Dooksec0482e2009-05-30 16:55:29 +0100912/* s3c24xx_nand_probe
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 *
914 * called by device layer when it finds a device matching
915 * one our driver can handled. This code checks to see if
916 * it can allocate all necessary resources then calls the
917 * nand layer to look for devices
918*/
Ben Dooksec0482e2009-05-30 16:55:29 +0100919static int s3c24xx_nand_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
Russell King3ae5eae2005-11-09 22:32:44 +0000921 struct s3c2410_platform_nand *plat = to_nand_plat(pdev);
Sachin Kamat54cd0202012-07-16 16:02:26 +0530922 enum s3c_cpu_type cpu_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 struct s3c2410_nand_info *info;
924 struct s3c2410_nand_mtd *nmtd;
925 struct s3c2410_nand_set *sets;
926 struct resource *res;
927 int err = 0;
928 int size;
929 int nr_sets;
930 int setno;
931
Ben Dooksec0482e2009-05-30 16:55:29 +0100932 cpu_type = platform_get_device_id(pdev)->driver_data;
933
Russell King3ae5eae2005-11-09 22:32:44 +0000934 pr_debug("s3c2410_nand_probe(%p)\n", pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Julia Lawallecce2a62010-05-13 22:07:46 +0200936 info = kzalloc(sizeof(*info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (info == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000938 dev_err(&pdev->dev, "no memory for flash info\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 err = -ENOMEM;
940 goto exit_error;
941 }
942
Russell King3ae5eae2005-11-09 22:32:44 +0000943 platform_set_drvdata(pdev, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 spin_lock_init(&info->controller.lock);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100946 init_waitqueue_head(&info->controller.wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948 /* get the clock source and enable it */
949
Russell King3ae5eae2005-11-09 22:32:44 +0000950 info->clk = clk_get(&pdev->dev, "nand");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 if (IS_ERR(info->clk)) {
Joe Perches898eb712007-10-18 03:06:30 -0700952 dev_err(&pdev->dev, "failed to get clock\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 err = -ENOENT;
954 goto exit_error;
955 }
956
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200957 s3c2410_nand_clk_set_state(info, CLOCK_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 /* allocate and map the resource */
960
Ben Dooksa4f957f2005-06-20 12:48:25 +0100961 /* currently we assume we have the one resource */
962 res = pdev->resource;
H Hartley Sweetenfc161c42009-12-14 16:56:22 -0500963 size = resource_size(res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965 info->area = request_mem_region(res->start, size, pdev->name);
966
967 if (info->area == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000968 dev_err(&pdev->dev, "cannot reserve register region\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 err = -ENOENT;
970 goto exit_error;
971 }
972
Russell King3ae5eae2005-11-09 22:32:44 +0000973 info->device = &pdev->dev;
Ben Dooksa4f957f2005-06-20 12:48:25 +0100974 info->platform = plat;
975 info->regs = ioremap(res->start, size);
Ben Dooks2c06a082006-06-27 14:35:46 +0100976 info->cpu_type = cpu_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 if (info->regs == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000979 dev_err(&pdev->dev, "cannot reserve register region\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 err = -EIO;
981 goto exit_error;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Russell King3ae5eae2005-11-09 22:32:44 +0000984 dev_dbg(&pdev->dev, "mapped registers at %p\n", info->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 /* initialise the hardware */
987
Ben Dooks30821fe2008-07-15 11:58:31 +0100988 err = s3c2410_nand_inithw(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 if (err != 0)
990 goto exit_error;
991
992 sets = (plat != NULL) ? plat->sets : NULL;
993 nr_sets = (plat != NULL) ? plat->nr_sets : 1;
994
995 info->mtd_count = nr_sets;
996
997 /* allocate our information */
998
999 size = nr_sets * sizeof(*info->mtds);
Julia Lawallecce2a62010-05-13 22:07:46 +02001000 info->mtds = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 if (info->mtds == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +00001002 dev_err(&pdev->dev, "failed to allocate mtd storage\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 err = -ENOMEM;
1004 goto exit_error;
1005 }
1006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 /* initialise all possible chips */
1008
1009 nmtd = info->mtds;
1010
1011 for (setno = 0; setno < nr_sets; setno++, nmtd++) {
Sachin Kamatf938bc52012-08-21 10:21:15 +05301012 pr_debug("initialising set %d (%p, info %p)\n",
1013 setno, nmtd, info);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 s3c2410_nand_init_chip(info, nmtd, sets);
1016
Ben Dooks71d54f32008-04-15 11:36:19 +01001017 nmtd->scan_res = nand_scan_ident(&nmtd->mtd,
David Woodhouse5e81e882010-02-26 18:32:56 +00001018 (sets) ? sets->nr_chips : 1,
1019 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 if (nmtd->scan_res == 0) {
Ben Dooks71d54f32008-04-15 11:36:19 +01001022 s3c2410_nand_update_chip(info, nmtd);
1023 nand_scan_tail(&nmtd->mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 s3c2410_nand_add_partition(info, nmtd, sets);
1025 }
1026
1027 if (sets != NULL)
1028 sets++;
1029 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001030
Ben Dooks30821fe2008-07-15 11:58:31 +01001031 err = s3c2410_nand_cpufreq_register(info);
1032 if (err < 0) {
1033 dev_err(&pdev->dev, "failed to init cpufreq support\n");
1034 goto exit_error;
1035 }
1036
Jiri Pinkavaac497c12011-04-13 11:59:30 +02001037 if (allow_clk_suspend(info)) {
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001038 dev_info(&pdev->dev, "clock idle support enabled\n");
Jiri Pinkavaac497c12011-04-13 11:59:30 +02001039 s3c2410_nand_clk_set_state(info, CLOCK_SUSPEND);
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001040 }
1041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 pr_debug("initialised ok\n");
1043 return 0;
1044
1045 exit_error:
Ben Dooksec0482e2009-05-30 16:55:29 +01001046 s3c24xx_nand_remove(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
1048 if (err == 0)
1049 err = -EINVAL;
1050 return err;
1051}
1052
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001053/* PM Support */
1054#ifdef CONFIG_PM
1055
1056static int s3c24xx_nand_suspend(struct platform_device *dev, pm_message_t pm)
1057{
1058 struct s3c2410_nand_info *info = platform_get_drvdata(dev);
1059
1060 if (info) {
Ben Dooks09160832008-04-15 11:36:18 +01001061 info->save_sel = readl(info->sel_reg);
Ben Dooks03680b12007-11-19 23:28:07 +00001062
1063 /* For the moment, we must ensure nFCE is high during
1064 * the time we are suspended. This really should be
1065 * handled by suspending the MTDs we are using, but
1066 * that is currently not the case. */
1067
Ben Dooks09160832008-04-15 11:36:18 +01001068 writel(info->save_sel | info->sel_bit, info->sel_reg);
Ben Dooks03680b12007-11-19 23:28:07 +00001069
Jiri Pinkavaac497c12011-04-13 11:59:30 +02001070 s3c2410_nand_clk_set_state(info, CLOCK_DISABLE);
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001071 }
1072
1073 return 0;
1074}
1075
1076static int s3c24xx_nand_resume(struct platform_device *dev)
1077{
1078 struct s3c2410_nand_info *info = platform_get_drvdata(dev);
Ben Dooks09160832008-04-15 11:36:18 +01001079 unsigned long sel;
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001080
1081 if (info) {
Jiri Pinkavaac497c12011-04-13 11:59:30 +02001082 s3c2410_nand_clk_set_state(info, CLOCK_ENABLE);
Ben Dooks30821fe2008-07-15 11:58:31 +01001083 s3c2410_nand_inithw(info);
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001084
Ben Dooks03680b12007-11-19 23:28:07 +00001085 /* Restore the state of the nFCE line. */
1086
Ben Dooks09160832008-04-15 11:36:18 +01001087 sel = readl(info->sel_reg);
1088 sel &= ~info->sel_bit;
1089 sel |= info->save_sel & info->sel_bit;
1090 writel(sel, info->sel_reg);
Ben Dooks03680b12007-11-19 23:28:07 +00001091
Jiri Pinkavaac497c12011-04-13 11:59:30 +02001092 s3c2410_nand_clk_set_state(info, CLOCK_SUSPEND);
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001093 }
1094
1095 return 0;
1096}
1097
1098#else
1099#define s3c24xx_nand_suspend NULL
1100#define s3c24xx_nand_resume NULL
1101#endif
1102
Ben Dooksa4f957f2005-06-20 12:48:25 +01001103/* driver device registration */
1104
Ben Dooksec0482e2009-05-30 16:55:29 +01001105static struct platform_device_id s3c24xx_driver_ids[] = {
1106 {
1107 .name = "s3c2410-nand",
1108 .driver_data = TYPE_S3C2410,
1109 }, {
1110 .name = "s3c2440-nand",
1111 .driver_data = TYPE_S3C2440,
1112 }, {
1113 .name = "s3c2412-nand",
1114 .driver_data = TYPE_S3C2412,
Peter Korsgaard9dbc0902009-06-07 06:04:23 -07001115 }, {
1116 .name = "s3c6400-nand",
1117 .driver_data = TYPE_S3C2412, /* compatible with 2412 */
Russell King3ae5eae2005-11-09 22:32:44 +00001118 },
Ben Dooksec0482e2009-05-30 16:55:29 +01001119 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120};
1121
Ben Dooksec0482e2009-05-30 16:55:29 +01001122MODULE_DEVICE_TABLE(platform, s3c24xx_driver_ids);
Ben Dooksa4f957f2005-06-20 12:48:25 +01001123
Ben Dooksec0482e2009-05-30 16:55:29 +01001124static struct platform_driver s3c24xx_nand_driver = {
1125 .probe = s3c24xx_nand_probe,
1126 .remove = s3c24xx_nand_remove,
Ben Dooks2c06a082006-06-27 14:35:46 +01001127 .suspend = s3c24xx_nand_suspend,
1128 .resume = s3c24xx_nand_resume,
Ben Dooksec0482e2009-05-30 16:55:29 +01001129 .id_table = s3c24xx_driver_ids,
Ben Dooks2c06a082006-06-27 14:35:46 +01001130 .driver = {
Ben Dooksec0482e2009-05-30 16:55:29 +01001131 .name = "s3c24xx-nand",
Ben Dooks2c06a082006-06-27 14:35:46 +01001132 .owner = THIS_MODULE,
1133 },
1134};
1135
Sachin Kamat056fcab2012-07-16 16:02:22 +05301136module_platform_driver(s3c24xx_nand_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138MODULE_LICENSE("GPL");
1139MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
Ben Dooksa4f957f2005-06-20 12:48:25 +01001140MODULE_DESCRIPTION("S3C24XX MTD NAND driver");