blob: f9bbf55f4b0fa07c22477799583fc1d7bc5668dd [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#ifdef CONFIG_MTD_NAND_S3C2410_HWECC
53static int hardware_ecc = 1;
54#else
55static int hardware_ecc = 0;
56#endif
57
Ben Dooksd1fef3c2006-06-19 09:29:38 +010058#ifdef CONFIG_MTD_NAND_S3C2410_CLKSTOP
Jiri Pinkavaac497c12011-04-13 11:59:30 +020059static const int clock_stop = 1;
Ben Dooksd1fef3c2006-06-19 09:29:38 +010060#else
61static const int clock_stop = 0;
62#endif
63
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065/* new oob placement block for use with hardware ecc generation
66 */
67
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020068static struct nand_ecclayout nand_hw_eccoob = {
David Woodhousee0c7d762006-05-13 18:07:53 +010069 .eccbytes = 3,
70 .eccpos = {0, 1, 2},
71 .oobfree = {{8, 8}}
Linus Torvalds1da177e2005-04-16 15:20:36 -070072};
73
74/* controller and mtd information */
75
76struct s3c2410_nand_info;
77
Ben Dooks3db72152009-05-30 17:18:15 +010078/**
79 * struct s3c2410_nand_mtd - driver MTD structure
80 * @mtd: The MTD instance to pass to the MTD layer.
81 * @chip: The NAND chip information.
82 * @set: The platform information supplied for this set of NAND chips.
83 * @info: Link back to the hardware information.
84 * @scan_res: The result from calling nand_scan_ident().
85*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070086struct s3c2410_nand_mtd {
87 struct mtd_info mtd;
88 struct nand_chip chip;
89 struct s3c2410_nand_set *set;
90 struct s3c2410_nand_info *info;
91 int scan_res;
92};
93
Ben Dooks2c06a082006-06-27 14:35:46 +010094enum s3c_cpu_type {
95 TYPE_S3C2410,
96 TYPE_S3C2412,
97 TYPE_S3C2440,
98};
99
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200100enum s3c_nand_clk_state {
101 CLOCK_DISABLE = 0,
102 CLOCK_ENABLE,
103 CLOCK_SUSPEND,
104};
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106/* overview of the s3c2410 nand state */
107
Ben Dooks3db72152009-05-30 17:18:15 +0100108/**
109 * struct s3c2410_nand_info - NAND controller state.
110 * @mtds: An array of MTD instances on this controoler.
111 * @platform: The platform data for this board.
112 * @device: The platform device we bound to.
113 * @area: The IO area resource that came from request_mem_region().
114 * @clk: The clock resource for this controller.
115 * @regs: The area mapped for the hardware registers described by @area.
116 * @sel_reg: Pointer to the register controlling the NAND selection.
117 * @sel_bit: The bit in @sel_reg to select the NAND chip.
118 * @mtd_count: The number of MTDs created from this controller.
119 * @save_sel: The contents of @sel_reg to be saved over suspend.
120 * @clk_rate: The clock rate from @clk.
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200121 * @clk_state: The current clock state.
Ben Dooks3db72152009-05-30 17:18:15 +0100122 * @cpu_type: The exact type of this controller.
123 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124struct s3c2410_nand_info {
125 /* mtd info */
126 struct nand_hw_control controller;
127 struct s3c2410_nand_mtd *mtds;
128 struct s3c2410_platform_nand *platform;
129
130 /* device info */
131 struct device *device;
132 struct resource *area;
133 struct clk *clk;
Ben Dooksfdf2fd52005-02-18 14:46:15 +0000134 void __iomem *regs;
Ben Dooks2c06a082006-06-27 14:35:46 +0100135 void __iomem *sel_reg;
136 int sel_bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 int mtd_count;
Ben Dooks09160832008-04-15 11:36:18 +0100138 unsigned long save_sel;
Ben Dooks30821fe2008-07-15 11:58:31 +0100139 unsigned long clk_rate;
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200140 enum s3c_nand_clk_state clk_state;
Ben Dooks03680b12007-11-19 23:28:07 +0000141
Ben Dooks2c06a082006-06-27 14:35:46 +0100142 enum s3c_cpu_type cpu_type;
Ben Dooks30821fe2008-07-15 11:58:31 +0100143
144#ifdef CONFIG_CPU_FREQ
145 struct notifier_block freq_transition;
146#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147};
148
149/* conversion functions */
150
151static struct s3c2410_nand_mtd *s3c2410_nand_mtd_toours(struct mtd_info *mtd)
152{
153 return container_of(mtd, struct s3c2410_nand_mtd, mtd);
154}
155
156static struct s3c2410_nand_info *s3c2410_nand_mtd_toinfo(struct mtd_info *mtd)
157{
158 return s3c2410_nand_mtd_toours(mtd)->info;
159}
160
Russell King3ae5eae2005-11-09 22:32:44 +0000161static struct s3c2410_nand_info *to_nand_info(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Russell King3ae5eae2005-11-09 22:32:44 +0000163 return platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
Russell King3ae5eae2005-11-09 22:32:44 +0000166static struct s3c2410_platform_nand *to_nand_plat(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Russell King3ae5eae2005-11-09 22:32:44 +0000168 return dev->dev.platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200171static inline int allow_clk_suspend(struct s3c2410_nand_info *info)
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100172{
173 return clock_stop;
174}
175
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200176/**
177 * s3c2410_nand_clk_set_state - Enable, disable or suspend NAND clock.
178 * @info: The controller instance.
179 * @new_state: State to which clock should be set.
180 */
181static void s3c2410_nand_clk_set_state(struct s3c2410_nand_info *info,
182 enum s3c_nand_clk_state new_state)
183{
184 if (!allow_clk_suspend(info) && new_state == CLOCK_SUSPEND)
185 return;
186
187 if (info->clk_state == CLOCK_ENABLE) {
188 if (new_state != CLOCK_ENABLE)
189 clk_disable(info->clk);
190 } else {
191 if (new_state == CLOCK_ENABLE)
192 clk_enable(info->clk);
193 }
194
195 info->clk_state = new_state;
196}
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198/* timing calculations */
199
Ben Dookscfd320f2005-10-20 22:22:58 +0100200#define NS_IN_KHZ 1000000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Ben Dooks3db72152009-05-30 17:18:15 +0100202/**
203 * s3c_nand_calc_rate - calculate timing data.
204 * @wanted: The cycle time in nanoseconds.
205 * @clk: The clock rate in kHz.
206 * @max: The maximum divider value.
207 *
208 * Calculate the timing value from the given parameters.
209 */
Ben Dooks2c06a082006-06-27 14:35:46 +0100210static int s3c_nand_calc_rate(int wanted, unsigned long clk, int max)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
212 int result;
213
Ben Dooks947391c2009-05-30 18:34:16 +0100214 result = DIV_ROUND_UP((wanted * clk), NS_IN_KHZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 pr_debug("result %d from %ld, %d\n", result, clk, wanted);
217
218 if (result > max) {
Sachin Kamat92aeb5d2012-07-16 16:02:23 +0530219 pr_err("%d ns is too big for current clock rate %ld\n",
220 wanted, clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return -1;
222 }
223
224 if (result < 1)
225 result = 1;
226
227 return result;
228}
229
Ben Dookscfd320f2005-10-20 22:22:58 +0100230#define to_ns(ticks,clk) (((ticks) * NS_IN_KHZ) / (unsigned int)(clk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232/* controller setup */
233
Ben Dooks3db72152009-05-30 17:18:15 +0100234/**
235 * s3c2410_nand_setrate - setup controller timing information.
236 * @info: The controller instance.
237 *
238 * Given the information supplied by the platform, calculate and set
239 * the necessary timing registers in the hardware to generate the
240 * necessary timing cycles to the hardware.
241 */
Ben Dooks30821fe2008-07-15 11:58:31 +0100242static int s3c2410_nand_setrate(struct s3c2410_nand_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
Ben Dooks30821fe2008-07-15 11:58:31 +0100244 struct s3c2410_platform_nand *plat = info->platform;
Ben Dooks2c06a082006-06-27 14:35:46 +0100245 int tacls_max = (info->cpu_type == TYPE_S3C2412) ? 8 : 4;
Ben Dookscfd320f2005-10-20 22:22:58 +0100246 int tacls, twrph0, twrph1;
Ben Dooks30821fe2008-07-15 11:58:31 +0100247 unsigned long clkrate = clk_get_rate(info->clk);
Nelson Castillo2612e522009-05-10 15:41:54 -0500248 unsigned long uninitialized_var(set), cfg, uninitialized_var(mask);
Ben Dooks30821fe2008-07-15 11:58:31 +0100249 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 /* calculate the timing information for the controller */
252
Ben Dooks30821fe2008-07-15 11:58:31 +0100253 info->clk_rate = clkrate;
Ben Dookscfd320f2005-10-20 22:22:58 +0100254 clkrate /= 1000; /* turn clock into kHz for ease of use */
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if (plat != NULL) {
Ben Dooks2c06a082006-06-27 14:35:46 +0100257 tacls = s3c_nand_calc_rate(plat->tacls, clkrate, tacls_max);
258 twrph0 = s3c_nand_calc_rate(plat->twrph0, clkrate, 8);
259 twrph1 = s3c_nand_calc_rate(plat->twrph1, clkrate, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 } else {
261 /* default timings */
Ben Dooks2c06a082006-06-27 14:35:46 +0100262 tacls = tacls_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 twrph0 = 8;
264 twrph1 = 8;
265 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 if (tacls < 0 || twrph0 < 0 || twrph1 < 0) {
Ben Dooks99974c62006-06-21 15:43:05 +0100268 dev_err(info->device, "cannot get suitable timings\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 return -EINVAL;
270 }
271
Ben Dooks99974c62006-06-21 15:43:05 +0100272 dev_info(info->device, "Tacls=%d, %dns Twrph0=%d %dns, Twrph1=%d %dns\n",
David Woodhousee0c7d762006-05-13 18:07:53 +0100273 tacls, to_ns(tacls, clkrate), twrph0, to_ns(twrph0, clkrate), twrph1, to_ns(twrph1, clkrate));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Ben Dooks30821fe2008-07-15 11:58:31 +0100275 switch (info->cpu_type) {
276 case TYPE_S3C2410:
277 mask = (S3C2410_NFCONF_TACLS(3) |
278 S3C2410_NFCONF_TWRPH0(7) |
279 S3C2410_NFCONF_TWRPH1(7));
280 set = S3C2410_NFCONF_EN;
281 set |= S3C2410_NFCONF_TACLS(tacls - 1);
282 set |= S3C2410_NFCONF_TWRPH0(twrph0 - 1);
283 set |= S3C2410_NFCONF_TWRPH1(twrph1 - 1);
284 break;
285
286 case TYPE_S3C2440:
287 case TYPE_S3C2412:
Peter Korsgaarda755a382009-06-03 13:46:54 +0200288 mask = (S3C2440_NFCONF_TACLS(tacls_max - 1) |
289 S3C2440_NFCONF_TWRPH0(7) |
290 S3C2440_NFCONF_TWRPH1(7));
Ben Dooks30821fe2008-07-15 11:58:31 +0100291
292 set = S3C2440_NFCONF_TACLS(tacls - 1);
293 set |= S3C2440_NFCONF_TWRPH0(twrph0 - 1);
294 set |= S3C2440_NFCONF_TWRPH1(twrph1 - 1);
295 break;
296
297 default:
Ben Dooks30821fe2008-07-15 11:58:31 +0100298 BUG();
299 }
300
Ben Dooks30821fe2008-07-15 11:58:31 +0100301 local_irq_save(flags);
302
303 cfg = readl(info->regs + S3C2410_NFCONF);
304 cfg &= ~mask;
305 cfg |= set;
306 writel(cfg, info->regs + S3C2410_NFCONF);
307
308 local_irq_restore(flags);
309
Andy Greenae7304e2009-05-10 15:42:02 -0500310 dev_dbg(info->device, "NF_CONF is 0x%lx\n", cfg);
311
Ben Dooks30821fe2008-07-15 11:58:31 +0100312 return 0;
313}
314
Ben Dooks3db72152009-05-30 17:18:15 +0100315/**
316 * s3c2410_nand_inithw - basic hardware initialisation
317 * @info: The hardware state.
318 *
319 * Do the basic initialisation of the hardware, using s3c2410_nand_setrate()
320 * to setup the hardware access speeds and set the controller to be enabled.
321*/
Ben Dooks30821fe2008-07-15 11:58:31 +0100322static int s3c2410_nand_inithw(struct s3c2410_nand_info *info)
323{
324 int ret;
325
326 ret = s3c2410_nand_setrate(info);
327 if (ret < 0)
328 return ret;
329
Ben Dooks2c06a082006-06-27 14:35:46 +0100330 switch (info->cpu_type) {
331 case TYPE_S3C2410:
Ben Dooks30821fe2008-07-15 11:58:31 +0100332 default:
Ben Dooks2c06a082006-06-27 14:35:46 +0100333 break;
334
335 case TYPE_S3C2440:
336 case TYPE_S3C2412:
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100337 /* enable the controller and de-assert nFCE */
338
Ben Dooks2c06a082006-06-27 14:35:46 +0100339 writel(S3C2440_NFCONT_ENABLE, info->regs + S3C2440_NFCONT);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 return 0;
343}
344
Ben Dooks3db72152009-05-30 17:18:15 +0100345/**
346 * s3c2410_nand_select_chip - select the given nand chip
347 * @mtd: The MTD instance for this chip.
348 * @chip: The chip number.
349 *
350 * This is called by the MTD layer to either select a given chip for the
351 * @mtd instance, or to indicate that the access has finished and the
352 * chip can be de-selected.
353 *
354 * The routine ensures that the nFCE line is correctly setup, and any
355 * platform specific selection code is called to route nFCE to the specific
356 * chip.
357 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358static void s3c2410_nand_select_chip(struct mtd_info *mtd, int chip)
359{
360 struct s3c2410_nand_info *info;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000361 struct s3c2410_nand_mtd *nmtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 struct nand_chip *this = mtd->priv;
363 unsigned long cur;
364
365 nmtd = this->priv;
366 info = nmtd->info;
367
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200368 if (chip != -1)
369 s3c2410_nand_clk_set_state(info, CLOCK_ENABLE);
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100370
Ben Dooks2c06a082006-06-27 14:35:46 +0100371 cur = readl(info->sel_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 if (chip == -1) {
Ben Dooks2c06a082006-06-27 14:35:46 +0100374 cur |= info->sel_bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 } else {
Ben Dooksfb8d82a2005-07-06 21:05:10 +0100376 if (nmtd->set != NULL && chip > nmtd->set->nr_chips) {
Ben Dooks99974c62006-06-21 15:43:05 +0100377 dev_err(info->device, "invalid chip %d\n", chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return;
379 }
380
381 if (info->platform != NULL) {
382 if (info->platform->select_chip != NULL)
David Woodhousee0c7d762006-05-13 18:07:53 +0100383 (info->platform->select_chip) (nmtd->set, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
385
Ben Dooks2c06a082006-06-27 14:35:46 +0100386 cur &= ~info->sel_bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388
Ben Dooks2c06a082006-06-27 14:35:46 +0100389 writel(cur, info->sel_reg);
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100390
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200391 if (chip == -1)
392 s3c2410_nand_clk_set_state(info, CLOCK_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
Ben Dooksad3b5fb2006-06-19 09:43:23 +0100395/* s3c2410_nand_hwcontrol
Ben Dooksa4f957f2005-06-20 12:48:25 +0100396 *
Ben Dooksad3b5fb2006-06-19 09:43:23 +0100397 * Issue command and address cycles to the chip
Ben Dooksa4f957f2005-06-20 12:48:25 +0100398*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200400static void s3c2410_nand_hwcontrol(struct mtd_info *mtd, int cmd,
David Woodhousef9068872006-06-10 00:53:16 +0100401 unsigned int ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
403 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
David Woodhousec9ac5972006-11-30 08:17:38 +0000404
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200405 if (cmd == NAND_CMD_NONE)
406 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
David Woodhousef9068872006-06-10 00:53:16 +0100408 if (ctrl & NAND_CLE)
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200409 writeb(cmd, info->regs + S3C2410_NFCMD);
410 else
411 writeb(cmd, info->regs + S3C2410_NFADDR);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100412}
413
414/* command and control functions */
415
David Woodhousef9068872006-06-10 00:53:16 +0100416static void s3c2440_nand_hwcontrol(struct mtd_info *mtd, int cmd,
417 unsigned int ctrl)
Ben Dooksa4f957f2005-06-20 12:48:25 +0100418{
419 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100420
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200421 if (cmd == NAND_CMD_NONE)
422 return;
Ben Dooksa4f957f2005-06-20 12:48:25 +0100423
David Woodhousef9068872006-06-10 00:53:16 +0100424 if (ctrl & NAND_CLE)
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200425 writeb(cmd, info->regs + S3C2440_NFCMD);
426 else
427 writeb(cmd, info->regs + S3C2440_NFADDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430/* s3c2410_nand_devready()
431 *
432 * returns 0 if the nand is busy, 1 if it is ready
433*/
434
435static int s3c2410_nand_devready(struct mtd_info *mtd)
436{
437 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return readb(info->regs + S3C2410_NFSTAT) & S3C2410_NFSTAT_BUSY;
439}
440
Ben Dooks2c06a082006-06-27 14:35:46 +0100441static int s3c2440_nand_devready(struct mtd_info *mtd)
442{
443 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
444 return readb(info->regs + S3C2440_NFSTAT) & S3C2440_NFSTAT_READY;
445}
446
447static int s3c2412_nand_devready(struct mtd_info *mtd)
448{
449 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
450 return readb(info->regs + S3C2412_NFSTAT) & S3C2412_NFSTAT_READY;
451}
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453/* ECC handling functions */
454
Ben Dooks2c06a082006-06-27 14:35:46 +0100455static int s3c2410_nand_correct_data(struct mtd_info *mtd, u_char *dat,
456 u_char *read_ecc, u_char *calc_ecc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Ben Dooksa2593242007-02-02 16:59:33 +0000458 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
459 unsigned int diff0, diff1, diff2;
460 unsigned int bit, byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Ben Dooksa2593242007-02-02 16:59:33 +0000462 pr_debug("%s(%p,%p,%p,%p)\n", __func__, mtd, dat, read_ecc, calc_ecc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Ben Dooksa2593242007-02-02 16:59:33 +0000464 diff0 = read_ecc[0] ^ calc_ecc[0];
465 diff1 = read_ecc[1] ^ calc_ecc[1];
466 diff2 = read_ecc[2] ^ calc_ecc[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Ben Dooksa2593242007-02-02 16:59:33 +0000468 pr_debug("%s: rd %02x%02x%02x calc %02x%02x%02x diff %02x%02x%02x\n",
469 __func__,
470 read_ecc[0], read_ecc[1], read_ecc[2],
471 calc_ecc[0], calc_ecc[1], calc_ecc[2],
472 diff0, diff1, diff2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Ben Dooksa2593242007-02-02 16:59:33 +0000474 if (diff0 == 0 && diff1 == 0 && diff2 == 0)
475 return 0; /* ECC is ok */
476
Ben Dooksc45c6c62008-04-15 11:36:20 +0100477 /* sometimes people do not think about using the ECC, so check
478 * to see if we have an 0xff,0xff,0xff read ECC and then ignore
479 * the error, on the assumption that this is an un-eccd page.
480 */
481 if (read_ecc[0] == 0xff && read_ecc[1] == 0xff && read_ecc[2] == 0xff
482 && info->platform->ignore_unset_ecc)
483 return 0;
484
Ben Dooksa2593242007-02-02 16:59:33 +0000485 /* Can we correct this ECC (ie, one row and column change).
486 * Note, this is similar to the 256 error code on smartmedia */
487
488 if (((diff0 ^ (diff0 >> 1)) & 0x55) == 0x55 &&
489 ((diff1 ^ (diff1 >> 1)) & 0x55) == 0x55 &&
490 ((diff2 ^ (diff2 >> 1)) & 0x55) == 0x55) {
491 /* calculate the bit position of the error */
492
Matt Reimerd0bf3792007-10-18 18:02:43 -0700493 bit = ((diff2 >> 3) & 1) |
494 ((diff2 >> 4) & 2) |
495 ((diff2 >> 5) & 4);
Ben Dooksa2593242007-02-02 16:59:33 +0000496
497 /* calculate the byte position of the error */
498
Matt Reimerd0bf3792007-10-18 18:02:43 -0700499 byte = ((diff2 << 7) & 0x100) |
500 ((diff1 << 0) & 0x80) |
501 ((diff1 << 1) & 0x40) |
502 ((diff1 << 2) & 0x20) |
503 ((diff1 << 3) & 0x10) |
504 ((diff0 >> 4) & 0x08) |
505 ((diff0 >> 3) & 0x04) |
506 ((diff0 >> 2) & 0x02) |
507 ((diff0 >> 1) & 0x01);
Ben Dooksa2593242007-02-02 16:59:33 +0000508
509 dev_dbg(info->device, "correcting error bit %d, byte %d\n",
510 bit, byte);
511
512 dat[byte] ^= (1 << bit);
513 return 1;
514 }
515
516 /* if there is only one bit difference in the ECC, then
517 * one of only a row or column parity has changed, which
518 * means the error is most probably in the ECC itself */
519
520 diff0 |= (diff1 << 8);
521 diff0 |= (diff2 << 16);
522
523 if ((diff0 & ~(1<<fls(diff0))) == 0)
524 return 1;
525
Matt Reimer4fac9f62007-10-18 18:02:44 -0700526 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
Ben Dooksa4f957f2005-06-20 12:48:25 +0100529/* ECC functions
530 *
531 * These allow the s3c2410 and s3c2440 to use the controller's ECC
532 * generator block to ECC the data as it passes through]
533*/
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535static void s3c2410_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 + S3C2410_NFCONF);
541 ctrl |= S3C2410_NFCONF_INITECC;
542 writel(ctrl, info->regs + S3C2410_NFCONF);
543}
544
Matthieu CASTET4f659922007-02-13 12:30:38 +0100545static void s3c2412_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 | S3C2412_NFCONT_INIT_MAIN_ECC, info->regs + S3C2440_NFCONT);
552}
553
Ben Dooksa4f957f2005-06-20 12:48:25 +0100554static void s3c2440_nand_enable_hwecc(struct mtd_info *mtd, int mode)
555{
556 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
557 unsigned long ctrl;
558
559 ctrl = readl(info->regs + S3C2440_NFCONT);
560 writel(ctrl | S3C2440_NFCONT_INITECC, info->regs + S3C2440_NFCONT);
561}
562
David Woodhousee0c7d762006-05-13 18:07:53 +0100563static int s3c2410_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
565 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
566
567 ecc_code[0] = readb(info->regs + S3C2410_NFECC + 0);
568 ecc_code[1] = readb(info->regs + S3C2410_NFECC + 1);
569 ecc_code[2] = readb(info->regs + S3C2410_NFECC + 2);
570
Ben Dooksa2593242007-02-02 16:59:33 +0000571 pr_debug("%s: returning ecc %02x%02x%02x\n", __func__,
572 ecc_code[0], ecc_code[1], ecc_code[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 return 0;
575}
576
Matthieu CASTET4f659922007-02-13 12:30:38 +0100577static int s3c2412_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
578{
579 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
580 unsigned long ecc = readl(info->regs + S3C2412_NFMECC0);
581
582 ecc_code[0] = ecc;
583 ecc_code[1] = ecc >> 8;
584 ecc_code[2] = ecc >> 16;
585
586 pr_debug("calculate_ecc: returning ecc %02x,%02x,%02x\n", ecc_code[0], ecc_code[1], ecc_code[2]);
587
588 return 0;
589}
590
David Woodhousee0c7d762006-05-13 18:07:53 +0100591static int s3c2440_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
Ben Dooksa4f957f2005-06-20 12:48:25 +0100592{
593 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
594 unsigned long ecc = readl(info->regs + S3C2440_NFMECC0);
595
596 ecc_code[0] = ecc;
597 ecc_code[1] = ecc >> 8;
598 ecc_code[2] = ecc >> 16;
599
Ben Dooks71d54f32008-04-15 11:36:19 +0100600 pr_debug("%s: returning ecc %06lx\n", __func__, ecc & 0xffffff);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100601
602 return 0;
603}
604
Ben Dooksa4f957f2005-06-20 12:48:25 +0100605/* over-ride the standard functions for a little more speed. We can
606 * use read/write block to move the data buffers to/from the controller
607*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609static void s3c2410_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
610{
611 struct nand_chip *this = mtd->priv;
612 readsb(this->IO_ADDR_R, buf, len);
613}
614
Matt Reimerb773bb22007-10-18 17:43:07 -0700615static void s3c2440_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
616{
617 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
Ben Dooksdea2aa62009-05-30 18:30:18 +0100618
619 readsl(info->regs + S3C2440_NFDATA, buf, len >> 2);
620
621 /* cleanup if we've got less than a word to do */
622 if (len & 3) {
623 buf += len & ~3;
624
625 for (; len & 3; len--)
626 *buf++ = readb(info->regs + S3C2440_NFDATA);
627 }
Matt Reimerb773bb22007-10-18 17:43:07 -0700628}
629
David Woodhousee0c7d762006-05-13 18:07:53 +0100630static void s3c2410_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
632 struct nand_chip *this = mtd->priv;
633 writesb(this->IO_ADDR_W, buf, len);
634}
635
Matt Reimerb773bb22007-10-18 17:43:07 -0700636static void s3c2440_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
637{
638 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
Ben Dooksdea2aa62009-05-30 18:30:18 +0100639
640 writesl(info->regs + S3C2440_NFDATA, buf, len >> 2);
641
642 /* cleanup any fractional write */
643 if (len & 3) {
644 buf += len & ~3;
645
646 for (; len & 3; len--, buf++)
647 writeb(*buf, info->regs + S3C2440_NFDATA);
648 }
Matt Reimerb773bb22007-10-18 17:43:07 -0700649}
650
Ben Dooks30821fe2008-07-15 11:58:31 +0100651/* cpufreq driver support */
652
653#ifdef CONFIG_CPU_FREQ
654
655static int s3c2410_nand_cpufreq_transition(struct notifier_block *nb,
656 unsigned long val, void *data)
657{
658 struct s3c2410_nand_info *info;
659 unsigned long newclk;
660
661 info = container_of(nb, struct s3c2410_nand_info, freq_transition);
662 newclk = clk_get_rate(info->clk);
663
664 if ((val == CPUFREQ_POSTCHANGE && newclk < info->clk_rate) ||
665 (val == CPUFREQ_PRECHANGE && newclk > info->clk_rate)) {
666 s3c2410_nand_setrate(info);
667 }
668
669 return 0;
670}
671
672static inline int s3c2410_nand_cpufreq_register(struct s3c2410_nand_info *info)
673{
674 info->freq_transition.notifier_call = s3c2410_nand_cpufreq_transition;
675
676 return cpufreq_register_notifier(&info->freq_transition,
677 CPUFREQ_TRANSITION_NOTIFIER);
678}
679
680static inline void s3c2410_nand_cpufreq_deregister(struct s3c2410_nand_info *info)
681{
682 cpufreq_unregister_notifier(&info->freq_transition,
683 CPUFREQ_TRANSITION_NOTIFIER);
684}
685
686#else
687static inline int s3c2410_nand_cpufreq_register(struct s3c2410_nand_info *info)
688{
689 return 0;
690}
691
692static inline void s3c2410_nand_cpufreq_deregister(struct s3c2410_nand_info *info)
693{
694}
695#endif
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697/* device management functions */
698
Ben Dooksec0482e2009-05-30 16:55:29 +0100699static int s3c24xx_nand_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Russell King3ae5eae2005-11-09 22:32:44 +0000701 struct s3c2410_nand_info *info = to_nand_info(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Russell King3ae5eae2005-11-09 22:32:44 +0000703 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000705 if (info == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return 0;
707
Ben Dooks30821fe2008-07-15 11:58:31 +0100708 s3c2410_nand_cpufreq_deregister(info);
709
710 /* Release all our mtds and their partitions, then go through
711 * freeing the resources used
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 if (info->mtds != NULL) {
715 struct s3c2410_nand_mtd *ptr = info->mtds;
716 int mtdno;
717
718 for (mtdno = 0; mtdno < info->mtd_count; mtdno++, ptr++) {
719 pr_debug("releasing mtd %d (%p)\n", mtdno, ptr);
720 nand_release(&ptr->mtd);
721 }
722
723 kfree(info->mtds);
724 }
725
726 /* free the common resources */
727
Jonghwan Choi4aa10622011-07-21 15:33:58 +0900728 if (!IS_ERR(info->clk)) {
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200729 s3c2410_nand_clk_set_state(info, CLOCK_DISABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 clk_put(info->clk);
731 }
732
733 if (info->regs != NULL) {
734 iounmap(info->regs);
735 info->regs = NULL;
736 }
737
738 if (info->area != NULL) {
739 release_resource(info->area);
740 kfree(info->area);
741 info->area = NULL;
742 }
743
744 kfree(info);
745
746 return 0;
747}
748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749static int s3c2410_nand_add_partition(struct s3c2410_nand_info *info,
750 struct s3c2410_nand_mtd *mtd,
751 struct s3c2410_nand_set *set)
752{
Dmitry Eremin-Solenikov599501a2011-06-02 18:01:02 +0400753 if (set)
754 mtd->mtd.name = set->name;
Andy Greened27f022009-05-10 15:42:09 -0500755
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +0200756 return mtd_device_parse_register(&mtd->mtd, NULL, NULL,
757 set->partitions, set->nr_partitions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Ben Dooks3db72152009-05-30 17:18:15 +0100760/**
761 * s3c2410_nand_init_chip - initialise a single instance of an chip
762 * @info: The base NAND controller the chip is on.
763 * @nmtd: The new controller MTD instance to fill in.
764 * @set: The information passed from the board specific platform data.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 *
Ben Dooks3db72152009-05-30 17:18:15 +0100766 * Initialise the given @nmtd from the information in @info and @set. This
767 * readies the structure for use with the MTD layer functions by ensuring
768 * all pointers are setup and the necessary control routines selected.
769 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770static void s3c2410_nand_init_chip(struct s3c2410_nand_info *info,
771 struct s3c2410_nand_mtd *nmtd,
772 struct s3c2410_nand_set *set)
773{
774 struct nand_chip *chip = &nmtd->chip;
Ben Dooks2c06a082006-06-27 14:35:46 +0100775 void __iomem *regs = info->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 chip->write_buf = s3c2410_nand_write_buf;
778 chip->read_buf = s3c2410_nand_read_buf;
779 chip->select_chip = s3c2410_nand_select_chip;
780 chip->chip_delay = 50;
781 chip->priv = nmtd;
Ben Dooks74218fe2009-11-02 18:12:51 +0000782 chip->options = set->options;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 chip->controller = &info->controller;
784
Ben Dooks2c06a082006-06-27 14:35:46 +0100785 switch (info->cpu_type) {
786 case TYPE_S3C2410:
787 chip->IO_ADDR_W = regs + S3C2410_NFDATA;
788 info->sel_reg = regs + S3C2410_NFCONF;
789 info->sel_bit = S3C2410_NFCONF_nFCE;
790 chip->cmd_ctrl = s3c2410_nand_hwcontrol;
791 chip->dev_ready = s3c2410_nand_devready;
792 break;
793
794 case TYPE_S3C2440:
795 chip->IO_ADDR_W = regs + S3C2440_NFDATA;
796 info->sel_reg = regs + S3C2440_NFCONT;
797 info->sel_bit = S3C2440_NFCONT_nFCE;
798 chip->cmd_ctrl = s3c2440_nand_hwcontrol;
799 chip->dev_ready = s3c2440_nand_devready;
Matt Reimerb773bb22007-10-18 17:43:07 -0700800 chip->read_buf = s3c2440_nand_read_buf;
801 chip->write_buf = s3c2440_nand_write_buf;
Ben Dooks2c06a082006-06-27 14:35:46 +0100802 break;
803
804 case TYPE_S3C2412:
805 chip->IO_ADDR_W = regs + S3C2440_NFDATA;
806 info->sel_reg = regs + S3C2440_NFCONT;
807 info->sel_bit = S3C2412_NFCONT_nFCE0;
808 chip->cmd_ctrl = s3c2440_nand_hwcontrol;
809 chip->dev_ready = s3c2412_nand_devready;
810
811 if (readl(regs + S3C2410_NFCONF) & S3C2412_NFCONF_NANDBOOT)
812 dev_info(info->device, "System booted from NAND\n");
813
814 break;
815 }
816
817 chip->IO_ADDR_R = chip->IO_ADDR_W;
Ben Dooksa4f957f2005-06-20 12:48:25 +0100818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 nmtd->info = info;
820 nmtd->mtd.priv = chip;
David Woodhouse552d9202006-05-14 01:20:46 +0100821 nmtd->mtd.owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 nmtd->set = set;
823
824 if (hardware_ecc) {
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +0200825 chip->ecc.calculate = s3c2410_nand_calculate_ecc;
Ben Dooks2c06a082006-06-27 14:35:46 +0100826 chip->ecc.correct = s3c2410_nand_correct_data;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +0200827 chip->ecc.mode = NAND_ECC_HW;
Mike Dunn6a918ba2012-03-11 14:21:11 -0700828 chip->ecc.strength = 1;
Ben Dooksa4f957f2005-06-20 12:48:25 +0100829
Ben Dooks2c06a082006-06-27 14:35:46 +0100830 switch (info->cpu_type) {
831 case TYPE_S3C2410:
832 chip->ecc.hwctl = s3c2410_nand_enable_hwecc;
833 chip->ecc.calculate = s3c2410_nand_calculate_ecc;
834 break;
835
836 case TYPE_S3C2412:
Matthieu CASTET4f659922007-02-13 12:30:38 +0100837 chip->ecc.hwctl = s3c2412_nand_enable_hwecc;
838 chip->ecc.calculate = s3c2412_nand_calculate_ecc;
839 break;
840
Ben Dooks2c06a082006-06-27 14:35:46 +0100841 case TYPE_S3C2440:
842 chip->ecc.hwctl = s3c2440_nand_enable_hwecc;
843 chip->ecc.calculate = s3c2440_nand_calculate_ecc;
844 break;
845
Ben Dooksa4f957f2005-06-20 12:48:25 +0100846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 } else {
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +0200848 chip->ecc.mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
Ben Dooks1c21ab62008-04-15 11:36:21 +0100850
851 if (set->ecc_layout != NULL)
852 chip->ecc.layout = set->ecc_layout;
Ben Dooks37e5ffa2008-04-15 11:36:22 +0100853
854 if (set->disable_ecc)
855 chip->ecc.mode = NAND_ECC_NONE;
Andy Green8c3e8432009-05-10 15:41:25 -0500856
857 switch (chip->ecc.mode) {
858 case NAND_ECC_NONE:
859 dev_info(info->device, "NAND ECC disabled\n");
860 break;
861 case NAND_ECC_SOFT:
862 dev_info(info->device, "NAND soft ECC\n");
863 break;
864 case NAND_ECC_HW:
865 dev_info(info->device, "NAND hardware ECC\n");
866 break;
867 default:
868 dev_info(info->device, "NAND ECC UNKNOWN\n");
869 break;
870 }
Michel Pollet9db41f92009-05-13 16:54:14 +0100871
872 /* If you use u-boot BBT creation code, specifying this flag will
873 * let the kernel fish out the BBT from the NAND, and also skip the
874 * full NAND scan that can take 1/2s or so. Little things... */
Brian Norrisa40f7342011-05-31 16:31:22 -0700875 if (set->flash_bbt) {
Brian Norrisbb9ebd42011-05-31 16:31:23 -0700876 chip->bbt_options |= NAND_BBT_USE_FLASH;
Brian Norrisa40f7342011-05-31 16:31:22 -0700877 chip->options |= NAND_SKIP_BBTSCAN;
878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
880
Ben Dooks3db72152009-05-30 17:18:15 +0100881/**
882 * s3c2410_nand_update_chip - post probe update
883 * @info: The controller instance.
884 * @nmtd: The driver version of the MTD instance.
Ben Dooks71d54f32008-04-15 11:36:19 +0100885 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200886 * This routine is called after the chip probe has successfully completed
Ben Dooks3db72152009-05-30 17:18:15 +0100887 * and the relevant per-chip information updated. This call ensure that
888 * we update the internal state accordingly.
889 *
890 * The internal state is currently limited to the ECC state information.
891*/
Ben Dooks71d54f32008-04-15 11:36:19 +0100892static void s3c2410_nand_update_chip(struct s3c2410_nand_info *info,
893 struct s3c2410_nand_mtd *nmtd)
894{
895 struct nand_chip *chip = &nmtd->chip;
896
Ben Dooks451d3392008-05-20 17:32:14 +0100897 dev_dbg(info->device, "chip %p => page shift %d\n",
898 chip, chip->page_shift);
Ben Dooks71d54f32008-04-15 11:36:19 +0100899
Andy Green8c3e8432009-05-10 15:41:25 -0500900 if (chip->ecc.mode != NAND_ECC_HW)
901 return;
902
Ben Dooks71d54f32008-04-15 11:36:19 +0100903 /* change the behaviour depending on wether we are using
904 * the large or small page nand device */
905
Andy Green8c3e8432009-05-10 15:41:25 -0500906 if (chip->page_shift > 10) {
907 chip->ecc.size = 256;
908 chip->ecc.bytes = 3;
909 } else {
910 chip->ecc.size = 512;
911 chip->ecc.bytes = 3;
912 chip->ecc.layout = &nand_hw_eccoob;
Ben Dooks71d54f32008-04-15 11:36:19 +0100913 }
914}
915
Ben Dooksec0482e2009-05-30 16:55:29 +0100916/* s3c24xx_nand_probe
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 *
918 * called by device layer when it finds a device matching
919 * one our driver can handled. This code checks to see if
920 * it can allocate all necessary resources then calls the
921 * nand layer to look for devices
922*/
Ben Dooksec0482e2009-05-30 16:55:29 +0100923static int s3c24xx_nand_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
Russell King3ae5eae2005-11-09 22:32:44 +0000925 struct s3c2410_platform_nand *plat = to_nand_plat(pdev);
Ben Dooksec0482e2009-05-30 16:55:29 +0100926 enum s3c_cpu_type cpu_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 struct s3c2410_nand_info *info;
928 struct s3c2410_nand_mtd *nmtd;
929 struct s3c2410_nand_set *sets;
930 struct resource *res;
931 int err = 0;
932 int size;
933 int nr_sets;
934 int setno;
935
Ben Dooksec0482e2009-05-30 16:55:29 +0100936 cpu_type = platform_get_device_id(pdev)->driver_data;
937
Russell King3ae5eae2005-11-09 22:32:44 +0000938 pr_debug("s3c2410_nand_probe(%p)\n", pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Julia Lawallecce2a62010-05-13 22:07:46 +0200940 info = kzalloc(sizeof(*info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 if (info == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000942 dev_err(&pdev->dev, "no memory for flash info\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 err = -ENOMEM;
944 goto exit_error;
945 }
946
Russell King3ae5eae2005-11-09 22:32:44 +0000947 platform_set_drvdata(pdev, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 spin_lock_init(&info->controller.lock);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100950 init_waitqueue_head(&info->controller.wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 /* get the clock source and enable it */
953
Russell King3ae5eae2005-11-09 22:32:44 +0000954 info->clk = clk_get(&pdev->dev, "nand");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 if (IS_ERR(info->clk)) {
Joe Perches898eb712007-10-18 03:06:30 -0700956 dev_err(&pdev->dev, "failed to get clock\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 err = -ENOENT;
958 goto exit_error;
959 }
960
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200961 s3c2410_nand_clk_set_state(info, CLOCK_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
963 /* allocate and map the resource */
964
Ben Dooksa4f957f2005-06-20 12:48:25 +0100965 /* currently we assume we have the one resource */
966 res = pdev->resource;
H Hartley Sweetenfc161c42009-12-14 16:56:22 -0500967 size = resource_size(res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 info->area = request_mem_region(res->start, size, pdev->name);
970
971 if (info->area == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000972 dev_err(&pdev->dev, "cannot reserve register region\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 err = -ENOENT;
974 goto exit_error;
975 }
976
Russell King3ae5eae2005-11-09 22:32:44 +0000977 info->device = &pdev->dev;
Ben Dooksa4f957f2005-06-20 12:48:25 +0100978 info->platform = plat;
979 info->regs = ioremap(res->start, size);
Ben Dooks2c06a082006-06-27 14:35:46 +0100980 info->cpu_type = cpu_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 if (info->regs == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000983 dev_err(&pdev->dev, "cannot reserve register region\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 err = -EIO;
985 goto exit_error;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Russell King3ae5eae2005-11-09 22:32:44 +0000988 dev_dbg(&pdev->dev, "mapped registers at %p\n", info->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 /* initialise the hardware */
991
Ben Dooks30821fe2008-07-15 11:58:31 +0100992 err = s3c2410_nand_inithw(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 if (err != 0)
994 goto exit_error;
995
996 sets = (plat != NULL) ? plat->sets : NULL;
997 nr_sets = (plat != NULL) ? plat->nr_sets : 1;
998
999 info->mtd_count = nr_sets;
1000
1001 /* allocate our information */
1002
1003 size = nr_sets * sizeof(*info->mtds);
Julia Lawallecce2a62010-05-13 22:07:46 +02001004 info->mtds = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 if (info->mtds == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +00001006 dev_err(&pdev->dev, "failed to allocate mtd storage\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 err = -ENOMEM;
1008 goto exit_error;
1009 }
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 /* initialise all possible chips */
1012
1013 nmtd = info->mtds;
1014
1015 for (setno = 0; setno < nr_sets; setno++, nmtd++) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001016 pr_debug("initialising set %d (%p, info %p)\n", setno, nmtd, info);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 s3c2410_nand_init_chip(info, nmtd, sets);
1019
Ben Dooks71d54f32008-04-15 11:36:19 +01001020 nmtd->scan_res = nand_scan_ident(&nmtd->mtd,
David Woodhouse5e81e882010-02-26 18:32:56 +00001021 (sets) ? sets->nr_chips : 1,
1022 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 if (nmtd->scan_res == 0) {
Ben Dooks71d54f32008-04-15 11:36:19 +01001025 s3c2410_nand_update_chip(info, nmtd);
1026 nand_scan_tail(&nmtd->mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 s3c2410_nand_add_partition(info, nmtd, sets);
1028 }
1029
1030 if (sets != NULL)
1031 sets++;
1032 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001033
Ben Dooks30821fe2008-07-15 11:58:31 +01001034 err = s3c2410_nand_cpufreq_register(info);
1035 if (err < 0) {
1036 dev_err(&pdev->dev, "failed to init cpufreq support\n");
1037 goto exit_error;
1038 }
1039
Jiri Pinkavaac497c12011-04-13 11:59:30 +02001040 if (allow_clk_suspend(info)) {
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001041 dev_info(&pdev->dev, "clock idle support enabled\n");
Jiri Pinkavaac497c12011-04-13 11:59:30 +02001042 s3c2410_nand_clk_set_state(info, CLOCK_SUSPEND);
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001043 }
1044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 pr_debug("initialised ok\n");
1046 return 0;
1047
1048 exit_error:
Ben Dooksec0482e2009-05-30 16:55:29 +01001049 s3c24xx_nand_remove(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 if (err == 0)
1052 err = -EINVAL;
1053 return err;
1054}
1055
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001056/* PM Support */
1057#ifdef CONFIG_PM
1058
1059static int s3c24xx_nand_suspend(struct platform_device *dev, pm_message_t pm)
1060{
1061 struct s3c2410_nand_info *info = platform_get_drvdata(dev);
1062
1063 if (info) {
Ben Dooks09160832008-04-15 11:36:18 +01001064 info->save_sel = readl(info->sel_reg);
Ben Dooks03680b12007-11-19 23:28:07 +00001065
1066 /* For the moment, we must ensure nFCE is high during
1067 * the time we are suspended. This really should be
1068 * handled by suspending the MTDs we are using, but
1069 * that is currently not the case. */
1070
Ben Dooks09160832008-04-15 11:36:18 +01001071 writel(info->save_sel | info->sel_bit, info->sel_reg);
Ben Dooks03680b12007-11-19 23:28:07 +00001072
Jiri Pinkavaac497c12011-04-13 11:59:30 +02001073 s3c2410_nand_clk_set_state(info, CLOCK_DISABLE);
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001074 }
1075
1076 return 0;
1077}
1078
1079static int s3c24xx_nand_resume(struct platform_device *dev)
1080{
1081 struct s3c2410_nand_info *info = platform_get_drvdata(dev);
Ben Dooks09160832008-04-15 11:36:18 +01001082 unsigned long sel;
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001083
1084 if (info) {
Jiri Pinkavaac497c12011-04-13 11:59:30 +02001085 s3c2410_nand_clk_set_state(info, CLOCK_ENABLE);
Ben Dooks30821fe2008-07-15 11:58:31 +01001086 s3c2410_nand_inithw(info);
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001087
Ben Dooks03680b12007-11-19 23:28:07 +00001088 /* Restore the state of the nFCE line. */
1089
Ben Dooks09160832008-04-15 11:36:18 +01001090 sel = readl(info->sel_reg);
1091 sel &= ~info->sel_bit;
1092 sel |= info->save_sel & info->sel_bit;
1093 writel(sel, info->sel_reg);
Ben Dooks03680b12007-11-19 23:28:07 +00001094
Jiri Pinkavaac497c12011-04-13 11:59:30 +02001095 s3c2410_nand_clk_set_state(info, CLOCK_SUSPEND);
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001096 }
1097
1098 return 0;
1099}
1100
1101#else
1102#define s3c24xx_nand_suspend NULL
1103#define s3c24xx_nand_resume NULL
1104#endif
1105
Ben Dooksa4f957f2005-06-20 12:48:25 +01001106/* driver device registration */
1107
Ben Dooksec0482e2009-05-30 16:55:29 +01001108static struct platform_device_id s3c24xx_driver_ids[] = {
1109 {
1110 .name = "s3c2410-nand",
1111 .driver_data = TYPE_S3C2410,
1112 }, {
1113 .name = "s3c2440-nand",
1114 .driver_data = TYPE_S3C2440,
1115 }, {
1116 .name = "s3c2412-nand",
1117 .driver_data = TYPE_S3C2412,
Peter Korsgaard9dbc0902009-06-07 06:04:23 -07001118 }, {
1119 .name = "s3c6400-nand",
1120 .driver_data = TYPE_S3C2412, /* compatible with 2412 */
Russell King3ae5eae2005-11-09 22:32:44 +00001121 },
Ben Dooksec0482e2009-05-30 16:55:29 +01001122 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123};
1124
Ben Dooksec0482e2009-05-30 16:55:29 +01001125MODULE_DEVICE_TABLE(platform, s3c24xx_driver_ids);
Ben Dooksa4f957f2005-06-20 12:48:25 +01001126
Ben Dooksec0482e2009-05-30 16:55:29 +01001127static struct platform_driver s3c24xx_nand_driver = {
1128 .probe = s3c24xx_nand_probe,
1129 .remove = s3c24xx_nand_remove,
Ben Dooks2c06a082006-06-27 14:35:46 +01001130 .suspend = s3c24xx_nand_suspend,
1131 .resume = s3c24xx_nand_resume,
Ben Dooksec0482e2009-05-30 16:55:29 +01001132 .id_table = s3c24xx_driver_ids,
Ben Dooks2c06a082006-06-27 14:35:46 +01001133 .driver = {
Ben Dooksec0482e2009-05-30 16:55:29 +01001134 .name = "s3c24xx-nand",
Ben Dooks2c06a082006-06-27 14:35:46 +01001135 .owner = THIS_MODULE,
1136 },
1137};
1138
Sachin Kamat056fcab2012-07-16 16:02:22 +05301139module_platform_driver(s3c24xx_nand_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141MODULE_LICENSE("GPL");
1142MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
Ben Dooksa4f957f2005-06-20 12:48:25 +01001143MODULE_DESCRIPTION("S3C24XX MTD NAND driver");