blob: 90a630a6f0b95f72e5274cac8167308ce91eac3e [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.
Ben Dooks3db72152009-05-30 17:18:15 +0100100 * @clk: The clock resource for this controller.
Sachin Kamat6f32a3e2012-08-21 14:24:09 +0530101 * @regs: The area mapped for the hardware registers.
Ben Dooks3db72152009-05-30 17:18:15 +0100102 * @sel_reg: Pointer to the register controlling the NAND selection.
103 * @sel_bit: The bit in @sel_reg to select the NAND chip.
104 * @mtd_count: The number of MTDs created from this controller.
105 * @save_sel: The contents of @sel_reg to be saved over suspend.
106 * @clk_rate: The clock rate from @clk.
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200107 * @clk_state: The current clock state.
Ben Dooks3db72152009-05-30 17:18:15 +0100108 * @cpu_type: The exact type of this controller.
109 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110struct s3c2410_nand_info {
111 /* mtd info */
112 struct nand_hw_control controller;
113 struct s3c2410_nand_mtd *mtds;
114 struct s3c2410_platform_nand *platform;
115
116 /* device info */
117 struct device *device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 struct clk *clk;
Ben Dooksfdf2fd52005-02-18 14:46:15 +0000119 void __iomem *regs;
Ben Dooks2c06a082006-06-27 14:35:46 +0100120 void __iomem *sel_reg;
121 int sel_bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 int mtd_count;
Ben Dooks09160832008-04-15 11:36:18 +0100123 unsigned long save_sel;
Ben Dooks30821fe2008-07-15 11:58:31 +0100124 unsigned long clk_rate;
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200125 enum s3c_nand_clk_state clk_state;
Ben Dooks03680b12007-11-19 23:28:07 +0000126
Ben Dooks2c06a082006-06-27 14:35:46 +0100127 enum s3c_cpu_type cpu_type;
Ben Dooks30821fe2008-07-15 11:58:31 +0100128
129#ifdef CONFIG_CPU_FREQ
130 struct notifier_block freq_transition;
131#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132};
133
134/* conversion functions */
135
136static struct s3c2410_nand_mtd *s3c2410_nand_mtd_toours(struct mtd_info *mtd)
137{
138 return container_of(mtd, struct s3c2410_nand_mtd, mtd);
139}
140
141static struct s3c2410_nand_info *s3c2410_nand_mtd_toinfo(struct mtd_info *mtd)
142{
143 return s3c2410_nand_mtd_toours(mtd)->info;
144}
145
Russell King3ae5eae2005-11-09 22:32:44 +0000146static struct s3c2410_nand_info *to_nand_info(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Russell King3ae5eae2005-11-09 22:32:44 +0000148 return platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
Russell King3ae5eae2005-11-09 22:32:44 +0000151static struct s3c2410_platform_nand *to_nand_plat(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Russell King3ae5eae2005-11-09 22:32:44 +0000153 return dev->dev.platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200156static inline int allow_clk_suspend(struct s3c2410_nand_info *info)
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100157{
Sachin Kamata68c5ec2012-07-16 16:02:25 +0530158#ifdef CONFIG_MTD_NAND_S3C2410_CLKSTOP
159 return 1;
160#else
161 return 0;
162#endif
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100163}
164
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200165/**
166 * s3c2410_nand_clk_set_state - Enable, disable or suspend NAND clock.
167 * @info: The controller instance.
168 * @new_state: State to which clock should be set.
169 */
170static void s3c2410_nand_clk_set_state(struct s3c2410_nand_info *info,
171 enum s3c_nand_clk_state new_state)
172{
173 if (!allow_clk_suspend(info) && new_state == CLOCK_SUSPEND)
174 return;
175
176 if (info->clk_state == CLOCK_ENABLE) {
177 if (new_state != CLOCK_ENABLE)
178 clk_disable(info->clk);
179 } else {
180 if (new_state == CLOCK_ENABLE)
181 clk_enable(info->clk);
182 }
183
184 info->clk_state = new_state;
185}
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187/* timing calculations */
188
Ben Dookscfd320f2005-10-20 22:22:58 +0100189#define NS_IN_KHZ 1000000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Ben Dooks3db72152009-05-30 17:18:15 +0100191/**
192 * s3c_nand_calc_rate - calculate timing data.
193 * @wanted: The cycle time in nanoseconds.
194 * @clk: The clock rate in kHz.
195 * @max: The maximum divider value.
196 *
197 * Calculate the timing value from the given parameters.
198 */
Ben Dooks2c06a082006-06-27 14:35:46 +0100199static int s3c_nand_calc_rate(int wanted, unsigned long clk, int max)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
201 int result;
202
Ben Dooks947391c2009-05-30 18:34:16 +0100203 result = DIV_ROUND_UP((wanted * clk), NS_IN_KHZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 pr_debug("result %d from %ld, %d\n", result, clk, wanted);
206
207 if (result > max) {
Sachin Kamat92aeb5d2012-07-16 16:02:23 +0530208 pr_err("%d ns is too big for current clock rate %ld\n",
209 wanted, clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 return -1;
211 }
212
213 if (result < 1)
214 result = 1;
215
216 return result;
217}
218
Sachin Kamat54cd0202012-07-16 16:02:26 +0530219#define to_ns(ticks, clk) (((ticks) * NS_IN_KHZ) / (unsigned int)(clk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221/* controller setup */
222
Ben Dooks3db72152009-05-30 17:18:15 +0100223/**
224 * s3c2410_nand_setrate - setup controller timing information.
225 * @info: The controller instance.
226 *
227 * Given the information supplied by the platform, calculate and set
228 * the necessary timing registers in the hardware to generate the
229 * necessary timing cycles to the hardware.
230 */
Ben Dooks30821fe2008-07-15 11:58:31 +0100231static int s3c2410_nand_setrate(struct s3c2410_nand_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
Ben Dooks30821fe2008-07-15 11:58:31 +0100233 struct s3c2410_platform_nand *plat = info->platform;
Ben Dooks2c06a082006-06-27 14:35:46 +0100234 int tacls_max = (info->cpu_type == TYPE_S3C2412) ? 8 : 4;
Ben Dookscfd320f2005-10-20 22:22:58 +0100235 int tacls, twrph0, twrph1;
Ben Dooks30821fe2008-07-15 11:58:31 +0100236 unsigned long clkrate = clk_get_rate(info->clk);
Nelson Castillo2612e522009-05-10 15:41:54 -0500237 unsigned long uninitialized_var(set), cfg, uninitialized_var(mask);
Ben Dooks30821fe2008-07-15 11:58:31 +0100238 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 /* calculate the timing information for the controller */
241
Ben Dooks30821fe2008-07-15 11:58:31 +0100242 info->clk_rate = clkrate;
Ben Dookscfd320f2005-10-20 22:22:58 +0100243 clkrate /= 1000; /* turn clock into kHz for ease of use */
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 if (plat != NULL) {
Ben Dooks2c06a082006-06-27 14:35:46 +0100246 tacls = s3c_nand_calc_rate(plat->tacls, clkrate, tacls_max);
247 twrph0 = s3c_nand_calc_rate(plat->twrph0, clkrate, 8);
248 twrph1 = s3c_nand_calc_rate(plat->twrph1, clkrate, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 } else {
250 /* default timings */
Ben Dooks2c06a082006-06-27 14:35:46 +0100251 tacls = tacls_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 twrph0 = 8;
253 twrph1 = 8;
254 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if (tacls < 0 || twrph0 < 0 || twrph1 < 0) {
Ben Dooks99974c62006-06-21 15:43:05 +0100257 dev_err(info->device, "cannot get suitable timings\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return -EINVAL;
259 }
260
Ben Dooks99974c62006-06-21 15:43:05 +0100261 dev_info(info->device, "Tacls=%d, %dns Twrph0=%d %dns, Twrph1=%d %dns\n",
Sachin Kamat54cd0202012-07-16 16:02:26 +0530262 tacls, to_ns(tacls, clkrate), twrph0, to_ns(twrph0, clkrate),
263 twrph1, to_ns(twrph1, clkrate));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Ben Dooks30821fe2008-07-15 11:58:31 +0100265 switch (info->cpu_type) {
266 case TYPE_S3C2410:
267 mask = (S3C2410_NFCONF_TACLS(3) |
268 S3C2410_NFCONF_TWRPH0(7) |
269 S3C2410_NFCONF_TWRPH1(7));
270 set = S3C2410_NFCONF_EN;
271 set |= S3C2410_NFCONF_TACLS(tacls - 1);
272 set |= S3C2410_NFCONF_TWRPH0(twrph0 - 1);
273 set |= S3C2410_NFCONF_TWRPH1(twrph1 - 1);
274 break;
275
276 case TYPE_S3C2440:
277 case TYPE_S3C2412:
Peter Korsgaarda755a382009-06-03 13:46:54 +0200278 mask = (S3C2440_NFCONF_TACLS(tacls_max - 1) |
279 S3C2440_NFCONF_TWRPH0(7) |
280 S3C2440_NFCONF_TWRPH1(7));
Ben Dooks30821fe2008-07-15 11:58:31 +0100281
282 set = S3C2440_NFCONF_TACLS(tacls - 1);
283 set |= S3C2440_NFCONF_TWRPH0(twrph0 - 1);
284 set |= S3C2440_NFCONF_TWRPH1(twrph1 - 1);
285 break;
286
287 default:
Ben Dooks30821fe2008-07-15 11:58:31 +0100288 BUG();
289 }
290
Ben Dooks30821fe2008-07-15 11:58:31 +0100291 local_irq_save(flags);
292
293 cfg = readl(info->regs + S3C2410_NFCONF);
294 cfg &= ~mask;
295 cfg |= set;
296 writel(cfg, info->regs + S3C2410_NFCONF);
297
298 local_irq_restore(flags);
299
Andy Greenae7304e2009-05-10 15:42:02 -0500300 dev_dbg(info->device, "NF_CONF is 0x%lx\n", cfg);
301
Ben Dooks30821fe2008-07-15 11:58:31 +0100302 return 0;
303}
304
Ben Dooks3db72152009-05-30 17:18:15 +0100305/**
306 * s3c2410_nand_inithw - basic hardware initialisation
307 * @info: The hardware state.
308 *
309 * Do the basic initialisation of the hardware, using s3c2410_nand_setrate()
310 * to setup the hardware access speeds and set the controller to be enabled.
311*/
Ben Dooks30821fe2008-07-15 11:58:31 +0100312static int s3c2410_nand_inithw(struct s3c2410_nand_info *info)
313{
314 int ret;
315
316 ret = s3c2410_nand_setrate(info);
317 if (ret < 0)
318 return ret;
319
Sachin Kamat54cd0202012-07-16 16:02:26 +0530320 switch (info->cpu_type) {
321 case TYPE_S3C2410:
Ben Dooks30821fe2008-07-15 11:58:31 +0100322 default:
Ben Dooks2c06a082006-06-27 14:35:46 +0100323 break;
324
Sachin Kamat54cd0202012-07-16 16:02:26 +0530325 case TYPE_S3C2440:
326 case TYPE_S3C2412:
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100327 /* enable the controller and de-assert nFCE */
328
Ben Dooks2c06a082006-06-27 14:35:46 +0100329 writel(S3C2440_NFCONT_ENABLE, info->regs + S3C2440_NFCONT);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 return 0;
333}
334
Ben Dooks3db72152009-05-30 17:18:15 +0100335/**
336 * s3c2410_nand_select_chip - select the given nand chip
337 * @mtd: The MTD instance for this chip.
338 * @chip: The chip number.
339 *
340 * This is called by the MTD layer to either select a given chip for the
341 * @mtd instance, or to indicate that the access has finished and the
342 * chip can be de-selected.
343 *
344 * The routine ensures that the nFCE line is correctly setup, and any
345 * platform specific selection code is called to route nFCE to the specific
346 * chip.
347 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348static void s3c2410_nand_select_chip(struct mtd_info *mtd, int chip)
349{
350 struct s3c2410_nand_info *info;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000351 struct s3c2410_nand_mtd *nmtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 struct nand_chip *this = mtd->priv;
353 unsigned long cur;
354
355 nmtd = this->priv;
356 info = nmtd->info;
357
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200358 if (chip != -1)
359 s3c2410_nand_clk_set_state(info, CLOCK_ENABLE);
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100360
Ben Dooks2c06a082006-06-27 14:35:46 +0100361 cur = readl(info->sel_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 if (chip == -1) {
Ben Dooks2c06a082006-06-27 14:35:46 +0100364 cur |= info->sel_bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 } else {
Ben Dooksfb8d82a2005-07-06 21:05:10 +0100366 if (nmtd->set != NULL && chip > nmtd->set->nr_chips) {
Ben Dooks99974c62006-06-21 15:43:05 +0100367 dev_err(info->device, "invalid chip %d\n", chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return;
369 }
370
371 if (info->platform != NULL) {
372 if (info->platform->select_chip != NULL)
David Woodhousee0c7d762006-05-13 18:07:53 +0100373 (info->platform->select_chip) (nmtd->set, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
375
Ben Dooks2c06a082006-06-27 14:35:46 +0100376 cur &= ~info->sel_bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 }
378
Ben Dooks2c06a082006-06-27 14:35:46 +0100379 writel(cur, info->sel_reg);
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100380
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200381 if (chip == -1)
382 s3c2410_nand_clk_set_state(info, CLOCK_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
Ben Dooksad3b5fb2006-06-19 09:43:23 +0100385/* s3c2410_nand_hwcontrol
Ben Dooksa4f957f2005-06-20 12:48:25 +0100386 *
Ben Dooksad3b5fb2006-06-19 09:43:23 +0100387 * Issue command and address cycles to the chip
Ben Dooksa4f957f2005-06-20 12:48:25 +0100388*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200390static void s3c2410_nand_hwcontrol(struct mtd_info *mtd, int cmd,
David Woodhousef9068872006-06-10 00:53:16 +0100391 unsigned int ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
393 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
David Woodhousec9ac5972006-11-30 08:17:38 +0000394
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200395 if (cmd == NAND_CMD_NONE)
396 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
David Woodhousef9068872006-06-10 00:53:16 +0100398 if (ctrl & NAND_CLE)
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200399 writeb(cmd, info->regs + S3C2410_NFCMD);
400 else
401 writeb(cmd, info->regs + S3C2410_NFADDR);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100402}
403
404/* command and control functions */
405
David Woodhousef9068872006-06-10 00:53:16 +0100406static void s3c2440_nand_hwcontrol(struct mtd_info *mtd, int cmd,
407 unsigned int ctrl)
Ben Dooksa4f957f2005-06-20 12:48:25 +0100408{
409 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100410
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200411 if (cmd == NAND_CMD_NONE)
412 return;
Ben Dooksa4f957f2005-06-20 12:48:25 +0100413
David Woodhousef9068872006-06-10 00:53:16 +0100414 if (ctrl & NAND_CLE)
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200415 writeb(cmd, info->regs + S3C2440_NFCMD);
416 else
417 writeb(cmd, info->regs + S3C2440_NFADDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420/* s3c2410_nand_devready()
421 *
422 * returns 0 if the nand is busy, 1 if it is ready
423*/
424
425static int s3c2410_nand_devready(struct mtd_info *mtd)
426{
427 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 return readb(info->regs + S3C2410_NFSTAT) & S3C2410_NFSTAT_BUSY;
429}
430
Ben Dooks2c06a082006-06-27 14:35:46 +0100431static int s3c2440_nand_devready(struct mtd_info *mtd)
432{
433 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
434 return readb(info->regs + S3C2440_NFSTAT) & S3C2440_NFSTAT_READY;
435}
436
437static int s3c2412_nand_devready(struct mtd_info *mtd)
438{
439 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
440 return readb(info->regs + S3C2412_NFSTAT) & S3C2412_NFSTAT_READY;
441}
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443/* ECC handling functions */
444
Ben Dooks2c06a082006-06-27 14:35:46 +0100445static int s3c2410_nand_correct_data(struct mtd_info *mtd, u_char *dat,
446 u_char *read_ecc, u_char *calc_ecc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Ben Dooksa2593242007-02-02 16:59:33 +0000448 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
449 unsigned int diff0, diff1, diff2;
450 unsigned int bit, byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Ben Dooksa2593242007-02-02 16:59:33 +0000452 pr_debug("%s(%p,%p,%p,%p)\n", __func__, mtd, dat, read_ecc, calc_ecc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Ben Dooksa2593242007-02-02 16:59:33 +0000454 diff0 = read_ecc[0] ^ calc_ecc[0];
455 diff1 = read_ecc[1] ^ calc_ecc[1];
456 diff2 = read_ecc[2] ^ calc_ecc[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Andy Shevchenko13e85972012-08-02 16:06:47 +0300458 pr_debug("%s: rd %*phN calc %*phN diff %02x%02x%02x\n",
459 __func__, 3, read_ecc, 3, calc_ecc,
Ben Dooksa2593242007-02-02 16:59:33 +0000460 diff0, diff1, diff2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Ben Dooksa2593242007-02-02 16:59:33 +0000462 if (diff0 == 0 && diff1 == 0 && diff2 == 0)
463 return 0; /* ECC is ok */
464
Ben Dooksc45c6c62008-04-15 11:36:20 +0100465 /* sometimes people do not think about using the ECC, so check
466 * to see if we have an 0xff,0xff,0xff read ECC and then ignore
467 * the error, on the assumption that this is an un-eccd page.
468 */
469 if (read_ecc[0] == 0xff && read_ecc[1] == 0xff && read_ecc[2] == 0xff
470 && info->platform->ignore_unset_ecc)
471 return 0;
472
Ben Dooksa2593242007-02-02 16:59:33 +0000473 /* Can we correct this ECC (ie, one row and column change).
474 * Note, this is similar to the 256 error code on smartmedia */
475
476 if (((diff0 ^ (diff0 >> 1)) & 0x55) == 0x55 &&
477 ((diff1 ^ (diff1 >> 1)) & 0x55) == 0x55 &&
478 ((diff2 ^ (diff2 >> 1)) & 0x55) == 0x55) {
479 /* calculate the bit position of the error */
480
Matt Reimerd0bf3792007-10-18 18:02:43 -0700481 bit = ((diff2 >> 3) & 1) |
482 ((diff2 >> 4) & 2) |
483 ((diff2 >> 5) & 4);
Ben Dooksa2593242007-02-02 16:59:33 +0000484
485 /* calculate the byte position of the error */
486
Matt Reimerd0bf3792007-10-18 18:02:43 -0700487 byte = ((diff2 << 7) & 0x100) |
488 ((diff1 << 0) & 0x80) |
489 ((diff1 << 1) & 0x40) |
490 ((diff1 << 2) & 0x20) |
491 ((diff1 << 3) & 0x10) |
492 ((diff0 >> 4) & 0x08) |
493 ((diff0 >> 3) & 0x04) |
494 ((diff0 >> 2) & 0x02) |
495 ((diff0 >> 1) & 0x01);
Ben Dooksa2593242007-02-02 16:59:33 +0000496
497 dev_dbg(info->device, "correcting error bit %d, byte %d\n",
498 bit, byte);
499
500 dat[byte] ^= (1 << bit);
501 return 1;
502 }
503
504 /* if there is only one bit difference in the ECC, then
505 * one of only a row or column parity has changed, which
506 * means the error is most probably in the ECC itself */
507
508 diff0 |= (diff1 << 8);
509 diff0 |= (diff2 << 16);
510
511 if ((diff0 & ~(1<<fls(diff0))) == 0)
512 return 1;
513
Matt Reimer4fac9f62007-10-18 18:02:44 -0700514 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516
Ben Dooksa4f957f2005-06-20 12:48:25 +0100517/* ECC functions
518 *
519 * These allow the s3c2410 and s3c2440 to use the controller's ECC
520 * generator block to ECC the data as it passes through]
521*/
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523static void s3c2410_nand_enable_hwecc(struct mtd_info *mtd, int mode)
524{
525 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
526 unsigned long ctrl;
527
528 ctrl = readl(info->regs + S3C2410_NFCONF);
529 ctrl |= S3C2410_NFCONF_INITECC;
530 writel(ctrl, info->regs + S3C2410_NFCONF);
531}
532
Matthieu CASTET4f659922007-02-13 12:30:38 +0100533static void s3c2412_nand_enable_hwecc(struct mtd_info *mtd, int mode)
534{
535 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
536 unsigned long ctrl;
537
538 ctrl = readl(info->regs + S3C2440_NFCONT);
Sachin Kamatf938bc52012-08-21 10:21:15 +0530539 writel(ctrl | S3C2412_NFCONT_INIT_MAIN_ECC,
540 info->regs + S3C2440_NFCONT);
Matthieu CASTET4f659922007-02-13 12:30:38 +0100541}
542
Ben Dooksa4f957f2005-06-20 12:48:25 +0100543static void s3c2440_nand_enable_hwecc(struct mtd_info *mtd, int mode)
544{
545 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
546 unsigned long ctrl;
547
548 ctrl = readl(info->regs + S3C2440_NFCONT);
549 writel(ctrl | S3C2440_NFCONT_INITECC, info->regs + S3C2440_NFCONT);
550}
551
Sachin Kamatf938bc52012-08-21 10:21:15 +0530552static int s3c2410_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
553 u_char *ecc_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
555 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
556
557 ecc_code[0] = readb(info->regs + S3C2410_NFECC + 0);
558 ecc_code[1] = readb(info->regs + S3C2410_NFECC + 1);
559 ecc_code[2] = readb(info->regs + S3C2410_NFECC + 2);
560
Andy Shevchenko13e85972012-08-02 16:06:47 +0300561 pr_debug("%s: returning ecc %*phN\n", __func__, 3, ecc_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 return 0;
564}
565
Sachin Kamatf938bc52012-08-21 10:21:15 +0530566static int s3c2412_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
567 u_char *ecc_code)
Matthieu CASTET4f659922007-02-13 12:30:38 +0100568{
569 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
570 unsigned long ecc = readl(info->regs + S3C2412_NFMECC0);
571
572 ecc_code[0] = ecc;
573 ecc_code[1] = ecc >> 8;
574 ecc_code[2] = ecc >> 16;
575
Andy Shevchenko13e85972012-08-02 16:06:47 +0300576 pr_debug("%s: returning ecc %*phN\n", __func__, 3, ecc_code);
Matthieu CASTET4f659922007-02-13 12:30:38 +0100577
578 return 0;
579}
580
Sachin Kamatf938bc52012-08-21 10:21:15 +0530581static int s3c2440_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
582 u_char *ecc_code)
Ben Dooksa4f957f2005-06-20 12:48:25 +0100583{
584 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
585 unsigned long ecc = readl(info->regs + S3C2440_NFMECC0);
586
587 ecc_code[0] = ecc;
588 ecc_code[1] = ecc >> 8;
589 ecc_code[2] = ecc >> 16;
590
Ben Dooks71d54f382008-04-15 11:36:19 +0100591 pr_debug("%s: returning ecc %06lx\n", __func__, ecc & 0xffffff);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100592
593 return 0;
594}
595
Ben Dooksa4f957f2005-06-20 12:48:25 +0100596/* over-ride the standard functions for a little more speed. We can
597 * use read/write block to move the data buffers to/from the controller
598*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600static void s3c2410_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
601{
602 struct nand_chip *this = mtd->priv;
603 readsb(this->IO_ADDR_R, buf, len);
604}
605
Matt Reimerb773bb22007-10-18 17:43:07 -0700606static void s3c2440_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
607{
608 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
Ben Dooksdea2aa62009-05-30 18:30:18 +0100609
610 readsl(info->regs + S3C2440_NFDATA, buf, len >> 2);
611
612 /* cleanup if we've got less than a word to do */
613 if (len & 3) {
614 buf += len & ~3;
615
616 for (; len & 3; len--)
617 *buf++ = readb(info->regs + S3C2440_NFDATA);
618 }
Matt Reimerb773bb22007-10-18 17:43:07 -0700619}
620
Sachin Kamatf938bc52012-08-21 10:21:15 +0530621static void s3c2410_nand_write_buf(struct mtd_info *mtd, const u_char *buf,
622 int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
624 struct nand_chip *this = mtd->priv;
625 writesb(this->IO_ADDR_W, buf, len);
626}
627
Sachin Kamatf938bc52012-08-21 10:21:15 +0530628static void s3c2440_nand_write_buf(struct mtd_info *mtd, const u_char *buf,
629 int len)
Matt Reimerb773bb22007-10-18 17:43:07 -0700630{
631 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
Ben Dooksdea2aa62009-05-30 18:30:18 +0100632
633 writesl(info->regs + S3C2440_NFDATA, buf, len >> 2);
634
635 /* cleanup any fractional write */
636 if (len & 3) {
637 buf += len & ~3;
638
639 for (; len & 3; len--, buf++)
640 writeb(*buf, info->regs + S3C2440_NFDATA);
641 }
Matt Reimerb773bb22007-10-18 17:43:07 -0700642}
643
Ben Dooks30821fe2008-07-15 11:58:31 +0100644/* cpufreq driver support */
645
646#ifdef CONFIG_CPU_FREQ
647
648static int s3c2410_nand_cpufreq_transition(struct notifier_block *nb,
649 unsigned long val, void *data)
650{
651 struct s3c2410_nand_info *info;
652 unsigned long newclk;
653
654 info = container_of(nb, struct s3c2410_nand_info, freq_transition);
655 newclk = clk_get_rate(info->clk);
656
657 if ((val == CPUFREQ_POSTCHANGE && newclk < info->clk_rate) ||
658 (val == CPUFREQ_PRECHANGE && newclk > info->clk_rate)) {
659 s3c2410_nand_setrate(info);
660 }
661
662 return 0;
663}
664
665static inline int s3c2410_nand_cpufreq_register(struct s3c2410_nand_info *info)
666{
667 info->freq_transition.notifier_call = s3c2410_nand_cpufreq_transition;
668
669 return cpufreq_register_notifier(&info->freq_transition,
670 CPUFREQ_TRANSITION_NOTIFIER);
671}
672
Sachin Kamatf938bc52012-08-21 10:21:15 +0530673static inline void
674s3c2410_nand_cpufreq_deregister(struct s3c2410_nand_info *info)
Ben Dooks30821fe2008-07-15 11:58:31 +0100675{
676 cpufreq_unregister_notifier(&info->freq_transition,
677 CPUFREQ_TRANSITION_NOTIFIER);
678}
679
680#else
681static inline int s3c2410_nand_cpufreq_register(struct s3c2410_nand_info *info)
682{
683 return 0;
684}
685
Sachin Kamatf938bc52012-08-21 10:21:15 +0530686static inline void
687s3c2410_nand_cpufreq_deregister(struct s3c2410_nand_info *info)
Ben Dooks30821fe2008-07-15 11:58:31 +0100688{
689}
690#endif
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692/* device management functions */
693
Ben Dooksec0482e2009-05-30 16:55:29 +0100694static int s3c24xx_nand_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Russell King3ae5eae2005-11-09 22:32:44 +0000696 struct s3c2410_nand_info *info = to_nand_info(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Russell King3ae5eae2005-11-09 22:32:44 +0000698 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000700 if (info == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return 0;
702
Ben Dooks30821fe2008-07-15 11:58:31 +0100703 s3c2410_nand_cpufreq_deregister(info);
704
705 /* Release all our mtds and their partitions, then go through
706 * freeing the resources used
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 if (info->mtds != NULL) {
710 struct s3c2410_nand_mtd *ptr = info->mtds;
711 int mtdno;
712
713 for (mtdno = 0; mtdno < info->mtd_count; mtdno++, ptr++) {
714 pr_debug("releasing mtd %d (%p)\n", mtdno, ptr);
715 nand_release(&ptr->mtd);
716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
718
719 /* free the common resources */
720
Sachin Kamat6f32a3e2012-08-21 14:24:09 +0530721 if (!IS_ERR(info->clk))
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200722 s3c2410_nand_clk_set_state(info, CLOCK_DISABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 return 0;
725}
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727static int s3c2410_nand_add_partition(struct s3c2410_nand_info *info,
728 struct s3c2410_nand_mtd *mtd,
729 struct s3c2410_nand_set *set)
730{
Dmitry Eremin-Solenikov599501a2011-06-02 18:01:02 +0400731 if (set)
732 mtd->mtd.name = set->name;
Andy Greened27f022009-05-10 15:42:09 -0500733
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +0200734 return mtd_device_parse_register(&mtd->mtd, NULL, NULL,
735 set->partitions, set->nr_partitions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Ben Dooks3db72152009-05-30 17:18:15 +0100738/**
739 * s3c2410_nand_init_chip - initialise a single instance of an chip
740 * @info: The base NAND controller the chip is on.
741 * @nmtd: The new controller MTD instance to fill in.
742 * @set: The information passed from the board specific platform data.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 *
Ben Dooks3db72152009-05-30 17:18:15 +0100744 * Initialise the given @nmtd from the information in @info and @set. This
745 * readies the structure for use with the MTD layer functions by ensuring
746 * all pointers are setup and the necessary control routines selected.
747 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748static void s3c2410_nand_init_chip(struct s3c2410_nand_info *info,
749 struct s3c2410_nand_mtd *nmtd,
750 struct s3c2410_nand_set *set)
751{
752 struct nand_chip *chip = &nmtd->chip;
Ben Dooks2c06a082006-06-27 14:35:46 +0100753 void __iomem *regs = info->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 chip->write_buf = s3c2410_nand_write_buf;
756 chip->read_buf = s3c2410_nand_read_buf;
757 chip->select_chip = s3c2410_nand_select_chip;
758 chip->chip_delay = 50;
759 chip->priv = nmtd;
Ben Dooks74218fe2009-11-02 18:12:51 +0000760 chip->options = set->options;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 chip->controller = &info->controller;
762
Ben Dooks2c06a082006-06-27 14:35:46 +0100763 switch (info->cpu_type) {
764 case TYPE_S3C2410:
765 chip->IO_ADDR_W = regs + S3C2410_NFDATA;
766 info->sel_reg = regs + S3C2410_NFCONF;
767 info->sel_bit = S3C2410_NFCONF_nFCE;
768 chip->cmd_ctrl = s3c2410_nand_hwcontrol;
769 chip->dev_ready = s3c2410_nand_devready;
770 break;
771
772 case TYPE_S3C2440:
773 chip->IO_ADDR_W = regs + S3C2440_NFDATA;
774 info->sel_reg = regs + S3C2440_NFCONT;
775 info->sel_bit = S3C2440_NFCONT_nFCE;
776 chip->cmd_ctrl = s3c2440_nand_hwcontrol;
777 chip->dev_ready = s3c2440_nand_devready;
Matt Reimerb773bb22007-10-18 17:43:07 -0700778 chip->read_buf = s3c2440_nand_read_buf;
779 chip->write_buf = s3c2440_nand_write_buf;
Ben Dooks2c06a082006-06-27 14:35:46 +0100780 break;
781
782 case TYPE_S3C2412:
783 chip->IO_ADDR_W = regs + S3C2440_NFDATA;
784 info->sel_reg = regs + S3C2440_NFCONT;
785 info->sel_bit = S3C2412_NFCONT_nFCE0;
786 chip->cmd_ctrl = s3c2440_nand_hwcontrol;
787 chip->dev_ready = s3c2412_nand_devready;
788
789 if (readl(regs + S3C2410_NFCONF) & S3C2412_NFCONF_NANDBOOT)
790 dev_info(info->device, "System booted from NAND\n");
791
792 break;
Sachin Kamat54cd0202012-07-16 16:02:26 +0530793 }
Ben Dooks2c06a082006-06-27 14:35:46 +0100794
795 chip->IO_ADDR_R = chip->IO_ADDR_W;
Ben Dooksa4f957f2005-06-20 12:48:25 +0100796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 nmtd->info = info;
798 nmtd->mtd.priv = chip;
David Woodhouse552d9202006-05-14 01:20:46 +0100799 nmtd->mtd.owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 nmtd->set = set;
801
Sachin Kamata68c5ec2012-07-16 16:02:25 +0530802#ifdef CONFIG_MTD_NAND_S3C2410_HWECC
803 chip->ecc.calculate = s3c2410_nand_calculate_ecc;
804 chip->ecc.correct = s3c2410_nand_correct_data;
805 chip->ecc.mode = NAND_ECC_HW;
806 chip->ecc.strength = 1;
807
808 switch (info->cpu_type) {
809 case TYPE_S3C2410:
810 chip->ecc.hwctl = s3c2410_nand_enable_hwecc;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +0200811 chip->ecc.calculate = s3c2410_nand_calculate_ecc;
Sachin Kamata68c5ec2012-07-16 16:02:25 +0530812 break;
Ben Dooksa4f957f2005-06-20 12:48:25 +0100813
Sachin Kamata68c5ec2012-07-16 16:02:25 +0530814 case TYPE_S3C2412:
815 chip->ecc.hwctl = s3c2412_nand_enable_hwecc;
816 chip->ecc.calculate = s3c2412_nand_calculate_ecc;
817 break;
Ben Dooks2c06a082006-06-27 14:35:46 +0100818
Sachin Kamata68c5ec2012-07-16 16:02:25 +0530819 case TYPE_S3C2440:
820 chip->ecc.hwctl = s3c2440_nand_enable_hwecc;
821 chip->ecc.calculate = s3c2440_nand_calculate_ecc;
822 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
Sachin Kamata68c5ec2012-07-16 16:02:25 +0530824#else
825 chip->ecc.mode = NAND_ECC_SOFT;
826#endif
Ben Dooks1c21ab62008-04-15 11:36:21 +0100827
828 if (set->ecc_layout != NULL)
829 chip->ecc.layout = set->ecc_layout;
Ben Dooks37e5ffa2008-04-15 11:36:22 +0100830
831 if (set->disable_ecc)
832 chip->ecc.mode = NAND_ECC_NONE;
Andy Green8c3e8432009-05-10 15:41:25 -0500833
834 switch (chip->ecc.mode) {
835 case NAND_ECC_NONE:
836 dev_info(info->device, "NAND ECC disabled\n");
837 break;
838 case NAND_ECC_SOFT:
839 dev_info(info->device, "NAND soft ECC\n");
840 break;
841 case NAND_ECC_HW:
842 dev_info(info->device, "NAND hardware ECC\n");
843 break;
844 default:
845 dev_info(info->device, "NAND ECC UNKNOWN\n");
846 break;
847 }
Michel Pollet9db41f92009-05-13 16:54:14 +0100848
849 /* If you use u-boot BBT creation code, specifying this flag will
850 * let the kernel fish out the BBT from the NAND, and also skip the
851 * full NAND scan that can take 1/2s or so. Little things... */
Brian Norrisa40f7342011-05-31 16:31:22 -0700852 if (set->flash_bbt) {
Brian Norrisbb9ebd42011-05-31 16:31:23 -0700853 chip->bbt_options |= NAND_BBT_USE_FLASH;
Brian Norrisa40f7342011-05-31 16:31:22 -0700854 chip->options |= NAND_SKIP_BBTSCAN;
855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}
857
Ben Dooks3db72152009-05-30 17:18:15 +0100858/**
859 * s3c2410_nand_update_chip - post probe update
860 * @info: The controller instance.
861 * @nmtd: The driver version of the MTD instance.
Ben Dooks71d54f382008-04-15 11:36:19 +0100862 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200863 * This routine is called after the chip probe has successfully completed
Ben Dooks3db72152009-05-30 17:18:15 +0100864 * and the relevant per-chip information updated. This call ensure that
865 * we update the internal state accordingly.
866 *
867 * The internal state is currently limited to the ECC state information.
868*/
Ben Dooks71d54f382008-04-15 11:36:19 +0100869static void s3c2410_nand_update_chip(struct s3c2410_nand_info *info,
870 struct s3c2410_nand_mtd *nmtd)
871{
872 struct nand_chip *chip = &nmtd->chip;
873
Ben Dooks451d3392008-05-20 17:32:14 +0100874 dev_dbg(info->device, "chip %p => page shift %d\n",
875 chip, chip->page_shift);
Ben Dooks71d54f382008-04-15 11:36:19 +0100876
Andy Green8c3e8432009-05-10 15:41:25 -0500877 if (chip->ecc.mode != NAND_ECC_HW)
878 return;
879
Ben Dooks71d54f382008-04-15 11:36:19 +0100880 /* change the behaviour depending on wether we are using
881 * the large or small page nand device */
882
Andy Green8c3e8432009-05-10 15:41:25 -0500883 if (chip->page_shift > 10) {
884 chip->ecc.size = 256;
885 chip->ecc.bytes = 3;
886 } else {
887 chip->ecc.size = 512;
888 chip->ecc.bytes = 3;
889 chip->ecc.layout = &nand_hw_eccoob;
Ben Dooks71d54f382008-04-15 11:36:19 +0100890 }
891}
892
Ben Dooksec0482e2009-05-30 16:55:29 +0100893/* s3c24xx_nand_probe
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 *
895 * called by device layer when it finds a device matching
896 * one our driver can handled. This code checks to see if
897 * it can allocate all necessary resources then calls the
898 * nand layer to look for devices
899*/
Ben Dooksec0482e2009-05-30 16:55:29 +0100900static int s3c24xx_nand_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
Russell King3ae5eae2005-11-09 22:32:44 +0000902 struct s3c2410_platform_nand *plat = to_nand_plat(pdev);
Sachin Kamat54cd0202012-07-16 16:02:26 +0530903 enum s3c_cpu_type cpu_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 struct s3c2410_nand_info *info;
905 struct s3c2410_nand_mtd *nmtd;
906 struct s3c2410_nand_set *sets;
907 struct resource *res;
908 int err = 0;
909 int size;
910 int nr_sets;
911 int setno;
912
Ben Dooksec0482e2009-05-30 16:55:29 +0100913 cpu_type = platform_get_device_id(pdev)->driver_data;
914
Russell King3ae5eae2005-11-09 22:32:44 +0000915 pr_debug("s3c2410_nand_probe(%p)\n", pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Sachin Kamat6f32a3e2012-08-21 14:24:09 +0530917 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 if (info == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000919 dev_err(&pdev->dev, "no memory for flash info\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 err = -ENOMEM;
921 goto exit_error;
922 }
923
Russell King3ae5eae2005-11-09 22:32:44 +0000924 platform_set_drvdata(pdev, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 spin_lock_init(&info->controller.lock);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100927 init_waitqueue_head(&info->controller.wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929 /* get the clock source and enable it */
930
Sachin Kamat6f32a3e2012-08-21 14:24:09 +0530931 info->clk = devm_clk_get(&pdev->dev, "nand");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 if (IS_ERR(info->clk)) {
Joe Perches898eb712007-10-18 03:06:30 -0700933 dev_err(&pdev->dev, "failed to get clock\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 err = -ENOENT;
935 goto exit_error;
936 }
937
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200938 s3c2410_nand_clk_set_state(info, CLOCK_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 /* allocate and map the resource */
941
Ben Dooksa4f957f2005-06-20 12:48:25 +0100942 /* currently we assume we have the one resource */
Sachin Kamat6f32a3e2012-08-21 14:24:09 +0530943 res = pdev->resource;
H Hartley Sweetenfc161c42009-12-14 16:56:22 -0500944 size = resource_size(res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Sachin Kamat6f32a3e2012-08-21 14:24:09 +0530946 info->device = &pdev->dev;
947 info->platform = plat;
948 info->cpu_type = cpu_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Sachin Kamat6f32a3e2012-08-21 14:24:09 +0530950 info->regs = devm_request_and_ioremap(&pdev->dev, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 if (info->regs == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000952 dev_err(&pdev->dev, "cannot reserve register region\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 err = -EIO;
954 goto exit_error;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Russell King3ae5eae2005-11-09 22:32:44 +0000957 dev_dbg(&pdev->dev, "mapped registers at %p\n", info->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 /* initialise the hardware */
960
Ben Dooks30821fe2008-07-15 11:58:31 +0100961 err = s3c2410_nand_inithw(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 if (err != 0)
963 goto exit_error;
964
965 sets = (plat != NULL) ? plat->sets : NULL;
966 nr_sets = (plat != NULL) ? plat->nr_sets : 1;
967
968 info->mtd_count = nr_sets;
969
970 /* allocate our information */
971
972 size = nr_sets * sizeof(*info->mtds);
Sachin Kamat6f32a3e2012-08-21 14:24:09 +0530973 info->mtds = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 if (info->mtds == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000975 dev_err(&pdev->dev, "failed to allocate mtd storage\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 err = -ENOMEM;
977 goto exit_error;
978 }
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 /* initialise all possible chips */
981
982 nmtd = info->mtds;
983
984 for (setno = 0; setno < nr_sets; setno++, nmtd++) {
Sachin Kamatf938bc52012-08-21 10:21:15 +0530985 pr_debug("initialising set %d (%p, info %p)\n",
986 setno, nmtd, info);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 s3c2410_nand_init_chip(info, nmtd, sets);
989
Ben Dooks71d54f382008-04-15 11:36:19 +0100990 nmtd->scan_res = nand_scan_ident(&nmtd->mtd,
David Woodhouse5e81e882010-02-26 18:32:56 +0000991 (sets) ? sets->nr_chips : 1,
992 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 if (nmtd->scan_res == 0) {
Ben Dooks71d54f382008-04-15 11:36:19 +0100995 s3c2410_nand_update_chip(info, nmtd);
996 nand_scan_tail(&nmtd->mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 s3c2410_nand_add_partition(info, nmtd, sets);
998 }
999
1000 if (sets != NULL)
1001 sets++;
1002 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001003
Ben Dooks30821fe2008-07-15 11:58:31 +01001004 err = s3c2410_nand_cpufreq_register(info);
1005 if (err < 0) {
1006 dev_err(&pdev->dev, "failed to init cpufreq support\n");
1007 goto exit_error;
1008 }
1009
Jiri Pinkavaac497c12011-04-13 11:59:30 +02001010 if (allow_clk_suspend(info)) {
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001011 dev_info(&pdev->dev, "clock idle support enabled\n");
Jiri Pinkavaac497c12011-04-13 11:59:30 +02001012 s3c2410_nand_clk_set_state(info, CLOCK_SUSPEND);
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001013 }
1014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 pr_debug("initialised ok\n");
1016 return 0;
1017
1018 exit_error:
Ben Dooksec0482e2009-05-30 16:55:29 +01001019 s3c24xx_nand_remove(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 if (err == 0)
1022 err = -EINVAL;
1023 return err;
1024}
1025
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001026/* PM Support */
1027#ifdef CONFIG_PM
1028
1029static int s3c24xx_nand_suspend(struct platform_device *dev, pm_message_t pm)
1030{
1031 struct s3c2410_nand_info *info = platform_get_drvdata(dev);
1032
1033 if (info) {
Ben Dooks09160832008-04-15 11:36:18 +01001034 info->save_sel = readl(info->sel_reg);
Ben Dooks03680b12007-11-19 23:28:07 +00001035
1036 /* For the moment, we must ensure nFCE is high during
1037 * the time we are suspended. This really should be
1038 * handled by suspending the MTDs we are using, but
1039 * that is currently not the case. */
1040
Ben Dooks09160832008-04-15 11:36:18 +01001041 writel(info->save_sel | info->sel_bit, info->sel_reg);
Ben Dooks03680b12007-11-19 23:28:07 +00001042
Jiri Pinkavaac497c12011-04-13 11:59:30 +02001043 s3c2410_nand_clk_set_state(info, CLOCK_DISABLE);
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001044 }
1045
1046 return 0;
1047}
1048
1049static int s3c24xx_nand_resume(struct platform_device *dev)
1050{
1051 struct s3c2410_nand_info *info = platform_get_drvdata(dev);
Ben Dooks09160832008-04-15 11:36:18 +01001052 unsigned long sel;
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001053
1054 if (info) {
Jiri Pinkavaac497c12011-04-13 11:59:30 +02001055 s3c2410_nand_clk_set_state(info, CLOCK_ENABLE);
Ben Dooks30821fe2008-07-15 11:58:31 +01001056 s3c2410_nand_inithw(info);
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001057
Ben Dooks03680b12007-11-19 23:28:07 +00001058 /* Restore the state of the nFCE line. */
1059
Ben Dooks09160832008-04-15 11:36:18 +01001060 sel = readl(info->sel_reg);
1061 sel &= ~info->sel_bit;
1062 sel |= info->save_sel & info->sel_bit;
1063 writel(sel, info->sel_reg);
Ben Dooks03680b12007-11-19 23:28:07 +00001064
Jiri Pinkavaac497c12011-04-13 11:59:30 +02001065 s3c2410_nand_clk_set_state(info, CLOCK_SUSPEND);
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001066 }
1067
1068 return 0;
1069}
1070
1071#else
1072#define s3c24xx_nand_suspend NULL
1073#define s3c24xx_nand_resume NULL
1074#endif
1075
Ben Dooksa4f957f2005-06-20 12:48:25 +01001076/* driver device registration */
1077
Ben Dooksec0482e2009-05-30 16:55:29 +01001078static struct platform_device_id s3c24xx_driver_ids[] = {
1079 {
1080 .name = "s3c2410-nand",
1081 .driver_data = TYPE_S3C2410,
1082 }, {
1083 .name = "s3c2440-nand",
1084 .driver_data = TYPE_S3C2440,
1085 }, {
1086 .name = "s3c2412-nand",
1087 .driver_data = TYPE_S3C2412,
Peter Korsgaard9dbc0902009-06-07 06:04:23 -07001088 }, {
1089 .name = "s3c6400-nand",
1090 .driver_data = TYPE_S3C2412, /* compatible with 2412 */
Russell King3ae5eae2005-11-09 22:32:44 +00001091 },
Ben Dooksec0482e2009-05-30 16:55:29 +01001092 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093};
1094
Ben Dooksec0482e2009-05-30 16:55:29 +01001095MODULE_DEVICE_TABLE(platform, s3c24xx_driver_ids);
Ben Dooksa4f957f2005-06-20 12:48:25 +01001096
Ben Dooksec0482e2009-05-30 16:55:29 +01001097static struct platform_driver s3c24xx_nand_driver = {
1098 .probe = s3c24xx_nand_probe,
1099 .remove = s3c24xx_nand_remove,
Ben Dooks2c06a082006-06-27 14:35:46 +01001100 .suspend = s3c24xx_nand_suspend,
1101 .resume = s3c24xx_nand_resume,
Ben Dooksec0482e2009-05-30 16:55:29 +01001102 .id_table = s3c24xx_driver_ids,
Ben Dooks2c06a082006-06-27 14:35:46 +01001103 .driver = {
Ben Dooksec0482e2009-05-30 16:55:29 +01001104 .name = "s3c24xx-nand",
Ben Dooks2c06a082006-06-27 14:35:46 +01001105 .owner = THIS_MODULE,
1106 },
1107};
1108
Sachin Kamat056fcab2012-07-16 16:02:22 +05301109module_platform_driver(s3c24xx_nand_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
1111MODULE_LICENSE("GPL");
1112MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
Ben Dooksa4f957f2005-06-20 12:48:25 +01001113MODULE_DESCRIPTION("S3C24XX MTD NAND driver");