blob: 370516c3f7c74ebb0a9a784ebfb9de2adbccdeb2 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#ifdef CONFIG_MTD_NAND_S3C2410_DEBUG
25#define DEBUG
26#endif
27
28#include <linux/module.h>
29#include <linux/types.h>
30#include <linux/init.h>
31#include <linux/kernel.h>
32#include <linux/string.h>
33#include <linux/ioport.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010034#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/delay.h>
36#include <linux/err.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080037#include <linux/slab.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000038#include <linux/clk.h>
Ben Dooks30821fe2008-07-15 11:58:31 +010039#include <linux/cpufreq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#include <linux/mtd/mtd.h>
42#include <linux/mtd/nand.h>
43#include <linux/mtd/nand_ecc.h>
44#include <linux/mtd/partitions.h>
45
46#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Ben Dooks7926b5a2008-10-30 10:14:35 +000048#include <plat/regs-nand.h>
49#include <plat/nand.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#ifdef CONFIG_MTD_NAND_S3C2410_HWECC
52static int hardware_ecc = 1;
53#else
54static int hardware_ecc = 0;
55#endif
56
Ben Dooksd1fef3c2006-06-19 09:29:38 +010057#ifdef CONFIG_MTD_NAND_S3C2410_CLKSTOP
Jiri Pinkavaac497c12011-04-13 11:59:30 +020058static const int clock_stop = 1;
Ben Dooksd1fef3c2006-06-19 09:29:38 +010059#else
60static const int clock_stop = 0;
61#endif
62
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/* new oob placement block for use with hardware ecc generation
65 */
66
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020067static struct nand_ecclayout nand_hw_eccoob = {
David Woodhousee0c7d762006-05-13 18:07:53 +010068 .eccbytes = 3,
69 .eccpos = {0, 1, 2},
70 .oobfree = {{8, 8}}
Linus Torvalds1da177e2005-04-16 15:20:36 -070071};
72
73/* controller and mtd information */
74
75struct s3c2410_nand_info;
76
Ben Dooks3db72152009-05-30 17:18:15 +010077/**
78 * struct s3c2410_nand_mtd - driver MTD structure
79 * @mtd: The MTD instance to pass to the MTD layer.
80 * @chip: The NAND chip information.
81 * @set: The platform information supplied for this set of NAND chips.
82 * @info: Link back to the hardware information.
83 * @scan_res: The result from calling nand_scan_ident().
84*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070085struct s3c2410_nand_mtd {
86 struct mtd_info mtd;
87 struct nand_chip chip;
88 struct s3c2410_nand_set *set;
89 struct s3c2410_nand_info *info;
90 int scan_res;
91};
92
Ben Dooks2c06a082006-06-27 14:35:46 +010093enum s3c_cpu_type {
94 TYPE_S3C2410,
95 TYPE_S3C2412,
96 TYPE_S3C2440,
97};
98
Jiri Pinkavaac497c12011-04-13 11:59:30 +020099enum s3c_nand_clk_state {
100 CLOCK_DISABLE = 0,
101 CLOCK_ENABLE,
102 CLOCK_SUSPEND,
103};
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105/* overview of the s3c2410 nand state */
106
Ben Dooks3db72152009-05-30 17:18:15 +0100107/**
108 * struct s3c2410_nand_info - NAND controller state.
109 * @mtds: An array of MTD instances on this controoler.
110 * @platform: The platform data for this board.
111 * @device: The platform device we bound to.
112 * @area: The IO area resource that came from request_mem_region().
113 * @clk: The clock resource for this controller.
114 * @regs: The area mapped for the hardware registers described by @area.
115 * @sel_reg: Pointer to the register controlling the NAND selection.
116 * @sel_bit: The bit in @sel_reg to select the NAND chip.
117 * @mtd_count: The number of MTDs created from this controller.
118 * @save_sel: The contents of @sel_reg to be saved over suspend.
119 * @clk_rate: The clock rate from @clk.
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200120 * @clk_state: The current clock state.
Ben Dooks3db72152009-05-30 17:18:15 +0100121 * @cpu_type: The exact type of this controller.
122 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123struct s3c2410_nand_info {
124 /* mtd info */
125 struct nand_hw_control controller;
126 struct s3c2410_nand_mtd *mtds;
127 struct s3c2410_platform_nand *platform;
128
129 /* device info */
130 struct device *device;
131 struct resource *area;
132 struct clk *clk;
Ben Dooksfdf2fd52005-02-18 14:46:15 +0000133 void __iomem *regs;
Ben Dooks2c06a082006-06-27 14:35:46 +0100134 void __iomem *sel_reg;
135 int sel_bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 int mtd_count;
Ben Dooks09160832008-04-15 11:36:18 +0100137 unsigned long save_sel;
Ben Dooks30821fe2008-07-15 11:58:31 +0100138 unsigned long clk_rate;
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200139 enum s3c_nand_clk_state clk_state;
Ben Dooks03680b12007-11-19 23:28:07 +0000140
Ben Dooks2c06a082006-06-27 14:35:46 +0100141 enum s3c_cpu_type cpu_type;
Ben Dooks30821fe2008-07-15 11:58:31 +0100142
143#ifdef CONFIG_CPU_FREQ
144 struct notifier_block freq_transition;
145#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146};
147
148/* conversion functions */
149
150static struct s3c2410_nand_mtd *s3c2410_nand_mtd_toours(struct mtd_info *mtd)
151{
152 return container_of(mtd, struct s3c2410_nand_mtd, mtd);
153}
154
155static struct s3c2410_nand_info *s3c2410_nand_mtd_toinfo(struct mtd_info *mtd)
156{
157 return s3c2410_nand_mtd_toours(mtd)->info;
158}
159
Russell King3ae5eae2005-11-09 22:32:44 +0000160static struct s3c2410_nand_info *to_nand_info(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Russell King3ae5eae2005-11-09 22:32:44 +0000162 return platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
Russell King3ae5eae2005-11-09 22:32:44 +0000165static struct s3c2410_platform_nand *to_nand_plat(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Russell King3ae5eae2005-11-09 22:32:44 +0000167 return dev->dev.platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200170static inline int allow_clk_suspend(struct s3c2410_nand_info *info)
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100171{
172 return clock_stop;
173}
174
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200175/**
176 * s3c2410_nand_clk_set_state - Enable, disable or suspend NAND clock.
177 * @info: The controller instance.
178 * @new_state: State to which clock should be set.
179 */
180static void s3c2410_nand_clk_set_state(struct s3c2410_nand_info *info,
181 enum s3c_nand_clk_state new_state)
182{
183 if (!allow_clk_suspend(info) && new_state == CLOCK_SUSPEND)
184 return;
185
186 if (info->clk_state == CLOCK_ENABLE) {
187 if (new_state != CLOCK_ENABLE)
188 clk_disable(info->clk);
189 } else {
190 if (new_state == CLOCK_ENABLE)
191 clk_enable(info->clk);
192 }
193
194 info->clk_state = new_state;
195}
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197/* timing calculations */
198
Ben Dookscfd320f2005-10-20 22:22:58 +0100199#define NS_IN_KHZ 1000000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Ben Dooks3db72152009-05-30 17:18:15 +0100201/**
202 * s3c_nand_calc_rate - calculate timing data.
203 * @wanted: The cycle time in nanoseconds.
204 * @clk: The clock rate in kHz.
205 * @max: The maximum divider value.
206 *
207 * Calculate the timing value from the given parameters.
208 */
Ben Dooks2c06a082006-06-27 14:35:46 +0100209static int s3c_nand_calc_rate(int wanted, unsigned long clk, int max)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
211 int result;
212
Ben Dooks947391c2009-05-30 18:34:16 +0100213 result = DIV_ROUND_UP((wanted * clk), NS_IN_KHZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 pr_debug("result %d from %ld, %d\n", result, clk, wanted);
216
217 if (result > max) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100218 printk("%d ns is too big for current clock rate %ld\n", wanted, clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return -1;
220 }
221
222 if (result < 1)
223 result = 1;
224
225 return result;
226}
227
Ben Dookscfd320f2005-10-20 22:22:58 +0100228#define to_ns(ticks,clk) (((ticks) * NS_IN_KHZ) / (unsigned int)(clk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230/* controller setup */
231
Ben Dooks3db72152009-05-30 17:18:15 +0100232/**
233 * s3c2410_nand_setrate - setup controller timing information.
234 * @info: The controller instance.
235 *
236 * Given the information supplied by the platform, calculate and set
237 * the necessary timing registers in the hardware to generate the
238 * necessary timing cycles to the hardware.
239 */
Ben Dooks30821fe2008-07-15 11:58:31 +0100240static int s3c2410_nand_setrate(struct s3c2410_nand_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Ben Dooks30821fe2008-07-15 11:58:31 +0100242 struct s3c2410_platform_nand *plat = info->platform;
Ben Dooks2c06a082006-06-27 14:35:46 +0100243 int tacls_max = (info->cpu_type == TYPE_S3C2412) ? 8 : 4;
Ben Dookscfd320f2005-10-20 22:22:58 +0100244 int tacls, twrph0, twrph1;
Ben Dooks30821fe2008-07-15 11:58:31 +0100245 unsigned long clkrate = clk_get_rate(info->clk);
Nelson Castillo2612e522009-05-10 15:41:54 -0500246 unsigned long uninitialized_var(set), cfg, uninitialized_var(mask);
Ben Dooks30821fe2008-07-15 11:58:31 +0100247 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 /* calculate the timing information for the controller */
250
Ben Dooks30821fe2008-07-15 11:58:31 +0100251 info->clk_rate = clkrate;
Ben Dookscfd320f2005-10-20 22:22:58 +0100252 clkrate /= 1000; /* turn clock into kHz for ease of use */
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (plat != NULL) {
Ben Dooks2c06a082006-06-27 14:35:46 +0100255 tacls = s3c_nand_calc_rate(plat->tacls, clkrate, tacls_max);
256 twrph0 = s3c_nand_calc_rate(plat->twrph0, clkrate, 8);
257 twrph1 = s3c_nand_calc_rate(plat->twrph1, clkrate, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 } else {
259 /* default timings */
Ben Dooks2c06a082006-06-27 14:35:46 +0100260 tacls = tacls_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 twrph0 = 8;
262 twrph1 = 8;
263 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 if (tacls < 0 || twrph0 < 0 || twrph1 < 0) {
Ben Dooks99974c62006-06-21 15:43:05 +0100266 dev_err(info->device, "cannot get suitable timings\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 return -EINVAL;
268 }
269
Ben Dooks99974c62006-06-21 15:43:05 +0100270 dev_info(info->device, "Tacls=%d, %dns Twrph0=%d %dns, Twrph1=%d %dns\n",
David Woodhousee0c7d762006-05-13 18:07:53 +0100271 tacls, to_ns(tacls, clkrate), twrph0, to_ns(twrph0, clkrate), twrph1, to_ns(twrph1, clkrate));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Ben Dooks30821fe2008-07-15 11:58:31 +0100273 switch (info->cpu_type) {
274 case TYPE_S3C2410:
275 mask = (S3C2410_NFCONF_TACLS(3) |
276 S3C2410_NFCONF_TWRPH0(7) |
277 S3C2410_NFCONF_TWRPH1(7));
278 set = S3C2410_NFCONF_EN;
279 set |= S3C2410_NFCONF_TACLS(tacls - 1);
280 set |= S3C2410_NFCONF_TWRPH0(twrph0 - 1);
281 set |= S3C2410_NFCONF_TWRPH1(twrph1 - 1);
282 break;
283
284 case TYPE_S3C2440:
285 case TYPE_S3C2412:
Peter Korsgaarda755a382009-06-03 13:46:54 +0200286 mask = (S3C2440_NFCONF_TACLS(tacls_max - 1) |
287 S3C2440_NFCONF_TWRPH0(7) |
288 S3C2440_NFCONF_TWRPH1(7));
Ben Dooks30821fe2008-07-15 11:58:31 +0100289
290 set = S3C2440_NFCONF_TACLS(tacls - 1);
291 set |= S3C2440_NFCONF_TWRPH0(twrph0 - 1);
292 set |= S3C2440_NFCONF_TWRPH1(twrph1 - 1);
293 break;
294
295 default:
Ben Dooks30821fe2008-07-15 11:58:31 +0100296 BUG();
297 }
298
Ben Dooks30821fe2008-07-15 11:58:31 +0100299 local_irq_save(flags);
300
301 cfg = readl(info->regs + S3C2410_NFCONF);
302 cfg &= ~mask;
303 cfg |= set;
304 writel(cfg, info->regs + S3C2410_NFCONF);
305
306 local_irq_restore(flags);
307
Andy Greenae7304e2009-05-10 15:42:02 -0500308 dev_dbg(info->device, "NF_CONF is 0x%lx\n", cfg);
309
Ben Dooks30821fe2008-07-15 11:58:31 +0100310 return 0;
311}
312
Ben Dooks3db72152009-05-30 17:18:15 +0100313/**
314 * s3c2410_nand_inithw - basic hardware initialisation
315 * @info: The hardware state.
316 *
317 * Do the basic initialisation of the hardware, using s3c2410_nand_setrate()
318 * to setup the hardware access speeds and set the controller to be enabled.
319*/
Ben Dooks30821fe2008-07-15 11:58:31 +0100320static int s3c2410_nand_inithw(struct s3c2410_nand_info *info)
321{
322 int ret;
323
324 ret = s3c2410_nand_setrate(info);
325 if (ret < 0)
326 return ret;
327
Ben Dooks2c06a082006-06-27 14:35:46 +0100328 switch (info->cpu_type) {
329 case TYPE_S3C2410:
Ben Dooks30821fe2008-07-15 11:58:31 +0100330 default:
Ben Dooks2c06a082006-06-27 14:35:46 +0100331 break;
332
333 case TYPE_S3C2440:
334 case TYPE_S3C2412:
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100335 /* enable the controller and de-assert nFCE */
336
Ben Dooks2c06a082006-06-27 14:35:46 +0100337 writel(S3C2440_NFCONT_ENABLE, info->regs + S3C2440_NFCONT);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return 0;
341}
342
Ben Dooks3db72152009-05-30 17:18:15 +0100343/**
344 * s3c2410_nand_select_chip - select the given nand chip
345 * @mtd: The MTD instance for this chip.
346 * @chip: The chip number.
347 *
348 * This is called by the MTD layer to either select a given chip for the
349 * @mtd instance, or to indicate that the access has finished and the
350 * chip can be de-selected.
351 *
352 * The routine ensures that the nFCE line is correctly setup, and any
353 * platform specific selection code is called to route nFCE to the specific
354 * chip.
355 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356static void s3c2410_nand_select_chip(struct mtd_info *mtd, int chip)
357{
358 struct s3c2410_nand_info *info;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000359 struct s3c2410_nand_mtd *nmtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 struct nand_chip *this = mtd->priv;
361 unsigned long cur;
362
363 nmtd = this->priv;
364 info = nmtd->info;
365
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200366 if (chip != -1)
367 s3c2410_nand_clk_set_state(info, CLOCK_ENABLE);
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100368
Ben Dooks2c06a082006-06-27 14:35:46 +0100369 cur = readl(info->sel_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 if (chip == -1) {
Ben Dooks2c06a082006-06-27 14:35:46 +0100372 cur |= info->sel_bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 } else {
Ben Dooksfb8d82a2005-07-06 21:05:10 +0100374 if (nmtd->set != NULL && chip > nmtd->set->nr_chips) {
Ben Dooks99974c62006-06-21 15:43:05 +0100375 dev_err(info->device, "invalid chip %d\n", chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return;
377 }
378
379 if (info->platform != NULL) {
380 if (info->platform->select_chip != NULL)
David Woodhousee0c7d762006-05-13 18:07:53 +0100381 (info->platform->select_chip) (nmtd->set, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 }
383
Ben Dooks2c06a082006-06-27 14:35:46 +0100384 cur &= ~info->sel_bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
386
Ben Dooks2c06a082006-06-27 14:35:46 +0100387 writel(cur, info->sel_reg);
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100388
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200389 if (chip == -1)
390 s3c2410_nand_clk_set_state(info, CLOCK_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392
Ben Dooksad3b5fb2006-06-19 09:43:23 +0100393/* s3c2410_nand_hwcontrol
Ben Dooksa4f957f2005-06-20 12:48:25 +0100394 *
Ben Dooksad3b5fb2006-06-19 09:43:23 +0100395 * Issue command and address cycles to the chip
Ben Dooksa4f957f2005-06-20 12:48:25 +0100396*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200398static void s3c2410_nand_hwcontrol(struct mtd_info *mtd, int cmd,
David Woodhousef9068872006-06-10 00:53:16 +0100399 unsigned int ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
401 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
David Woodhousec9ac5972006-11-30 08:17:38 +0000402
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200403 if (cmd == NAND_CMD_NONE)
404 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
David Woodhousef9068872006-06-10 00:53:16 +0100406 if (ctrl & NAND_CLE)
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200407 writeb(cmd, info->regs + S3C2410_NFCMD);
408 else
409 writeb(cmd, info->regs + S3C2410_NFADDR);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100410}
411
412/* command and control functions */
413
David Woodhousef9068872006-06-10 00:53:16 +0100414static void s3c2440_nand_hwcontrol(struct mtd_info *mtd, int cmd,
415 unsigned int ctrl)
Ben Dooksa4f957f2005-06-20 12:48:25 +0100416{
417 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100418
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200419 if (cmd == NAND_CMD_NONE)
420 return;
Ben Dooksa4f957f2005-06-20 12:48:25 +0100421
David Woodhousef9068872006-06-10 00:53:16 +0100422 if (ctrl & NAND_CLE)
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200423 writeb(cmd, info->regs + S3C2440_NFCMD);
424 else
425 writeb(cmd, info->regs + S3C2440_NFADDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428/* s3c2410_nand_devready()
429 *
430 * returns 0 if the nand is busy, 1 if it is ready
431*/
432
433static int s3c2410_nand_devready(struct mtd_info *mtd)
434{
435 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return readb(info->regs + S3C2410_NFSTAT) & S3C2410_NFSTAT_BUSY;
437}
438
Ben Dooks2c06a082006-06-27 14:35:46 +0100439static int s3c2440_nand_devready(struct mtd_info *mtd)
440{
441 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
442 return readb(info->regs + S3C2440_NFSTAT) & S3C2440_NFSTAT_READY;
443}
444
445static int s3c2412_nand_devready(struct mtd_info *mtd)
446{
447 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
448 return readb(info->regs + S3C2412_NFSTAT) & S3C2412_NFSTAT_READY;
449}
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451/* ECC handling functions */
452
Ben Dooks2c06a082006-06-27 14:35:46 +0100453static int s3c2410_nand_correct_data(struct mtd_info *mtd, u_char *dat,
454 u_char *read_ecc, u_char *calc_ecc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Ben Dooksa2593242007-02-02 16:59:33 +0000456 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
457 unsigned int diff0, diff1, diff2;
458 unsigned int bit, byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Ben Dooksa2593242007-02-02 16:59:33 +0000460 pr_debug("%s(%p,%p,%p,%p)\n", __func__, mtd, dat, read_ecc, calc_ecc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Ben Dooksa2593242007-02-02 16:59:33 +0000462 diff0 = read_ecc[0] ^ calc_ecc[0];
463 diff1 = read_ecc[1] ^ calc_ecc[1];
464 diff2 = read_ecc[2] ^ calc_ecc[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Ben Dooksa2593242007-02-02 16:59:33 +0000466 pr_debug("%s: rd %02x%02x%02x calc %02x%02x%02x diff %02x%02x%02x\n",
467 __func__,
468 read_ecc[0], read_ecc[1], read_ecc[2],
469 calc_ecc[0], calc_ecc[1], calc_ecc[2],
470 diff0, diff1, diff2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Ben Dooksa2593242007-02-02 16:59:33 +0000472 if (diff0 == 0 && diff1 == 0 && diff2 == 0)
473 return 0; /* ECC is ok */
474
Ben Dooksc45c6c62008-04-15 11:36:20 +0100475 /* sometimes people do not think about using the ECC, so check
476 * to see if we have an 0xff,0xff,0xff read ECC and then ignore
477 * the error, on the assumption that this is an un-eccd page.
478 */
479 if (read_ecc[0] == 0xff && read_ecc[1] == 0xff && read_ecc[2] == 0xff
480 && info->platform->ignore_unset_ecc)
481 return 0;
482
Ben Dooksa2593242007-02-02 16:59:33 +0000483 /* Can we correct this ECC (ie, one row and column change).
484 * Note, this is similar to the 256 error code on smartmedia */
485
486 if (((diff0 ^ (diff0 >> 1)) & 0x55) == 0x55 &&
487 ((diff1 ^ (diff1 >> 1)) & 0x55) == 0x55 &&
488 ((diff2 ^ (diff2 >> 1)) & 0x55) == 0x55) {
489 /* calculate the bit position of the error */
490
Matt Reimerd0bf3792007-10-18 18:02:43 -0700491 bit = ((diff2 >> 3) & 1) |
492 ((diff2 >> 4) & 2) |
493 ((diff2 >> 5) & 4);
Ben Dooksa2593242007-02-02 16:59:33 +0000494
495 /* calculate the byte position of the error */
496
Matt Reimerd0bf3792007-10-18 18:02:43 -0700497 byte = ((diff2 << 7) & 0x100) |
498 ((diff1 << 0) & 0x80) |
499 ((diff1 << 1) & 0x40) |
500 ((diff1 << 2) & 0x20) |
501 ((diff1 << 3) & 0x10) |
502 ((diff0 >> 4) & 0x08) |
503 ((diff0 >> 3) & 0x04) |
504 ((diff0 >> 2) & 0x02) |
505 ((diff0 >> 1) & 0x01);
Ben Dooksa2593242007-02-02 16:59:33 +0000506
507 dev_dbg(info->device, "correcting error bit %d, byte %d\n",
508 bit, byte);
509
510 dat[byte] ^= (1 << bit);
511 return 1;
512 }
513
514 /* if there is only one bit difference in the ECC, then
515 * one of only a row or column parity has changed, which
516 * means the error is most probably in the ECC itself */
517
518 diff0 |= (diff1 << 8);
519 diff0 |= (diff2 << 16);
520
521 if ((diff0 & ~(1<<fls(diff0))) == 0)
522 return 1;
523
Matt Reimer4fac9f62007-10-18 18:02:44 -0700524 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
526
Ben Dooksa4f957f2005-06-20 12:48:25 +0100527/* ECC functions
528 *
529 * These allow the s3c2410 and s3c2440 to use the controller's ECC
530 * generator block to ECC the data as it passes through]
531*/
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533static void s3c2410_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 + S3C2410_NFCONF);
539 ctrl |= S3C2410_NFCONF_INITECC;
540 writel(ctrl, info->regs + S3C2410_NFCONF);
541}
542
Matthieu CASTET4f659922007-02-13 12:30:38 +0100543static void s3c2412_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 | S3C2412_NFCONT_INIT_MAIN_ECC, info->regs + S3C2440_NFCONT);
550}
551
Ben Dooksa4f957f2005-06-20 12:48:25 +0100552static void s3c2440_nand_enable_hwecc(struct mtd_info *mtd, int mode)
553{
554 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
555 unsigned long ctrl;
556
557 ctrl = readl(info->regs + S3C2440_NFCONT);
558 writel(ctrl | S3C2440_NFCONT_INITECC, info->regs + S3C2440_NFCONT);
559}
560
David Woodhousee0c7d762006-05-13 18:07:53 +0100561static int s3c2410_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
563 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
564
565 ecc_code[0] = readb(info->regs + S3C2410_NFECC + 0);
566 ecc_code[1] = readb(info->regs + S3C2410_NFECC + 1);
567 ecc_code[2] = readb(info->regs + S3C2410_NFECC + 2);
568
Ben Dooksa2593242007-02-02 16:59:33 +0000569 pr_debug("%s: returning ecc %02x%02x%02x\n", __func__,
570 ecc_code[0], ecc_code[1], ecc_code[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 return 0;
573}
574
Matthieu CASTET4f659922007-02-13 12:30:38 +0100575static int s3c2412_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
576{
577 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
578 unsigned long ecc = readl(info->regs + S3C2412_NFMECC0);
579
580 ecc_code[0] = ecc;
581 ecc_code[1] = ecc >> 8;
582 ecc_code[2] = ecc >> 16;
583
584 pr_debug("calculate_ecc: returning ecc %02x,%02x,%02x\n", ecc_code[0], ecc_code[1], ecc_code[2]);
585
586 return 0;
587}
588
David Woodhousee0c7d762006-05-13 18:07:53 +0100589static int s3c2440_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
Ben Dooksa4f957f2005-06-20 12:48:25 +0100590{
591 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
592 unsigned long ecc = readl(info->regs + S3C2440_NFMECC0);
593
594 ecc_code[0] = ecc;
595 ecc_code[1] = ecc >> 8;
596 ecc_code[2] = ecc >> 16;
597
Ben Dooks71d54f32008-04-15 11:36:19 +0100598 pr_debug("%s: returning ecc %06lx\n", __func__, ecc & 0xffffff);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100599
600 return 0;
601}
602
Ben Dooksa4f957f2005-06-20 12:48:25 +0100603/* over-ride the standard functions for a little more speed. We can
604 * use read/write block to move the data buffers to/from the controller
605*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607static void s3c2410_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
608{
609 struct nand_chip *this = mtd->priv;
610 readsb(this->IO_ADDR_R, buf, len);
611}
612
Matt Reimerb773bb22007-10-18 17:43:07 -0700613static void s3c2440_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
614{
615 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
Ben Dooksdea2aa62009-05-30 18:30:18 +0100616
617 readsl(info->regs + S3C2440_NFDATA, buf, len >> 2);
618
619 /* cleanup if we've got less than a word to do */
620 if (len & 3) {
621 buf += len & ~3;
622
623 for (; len & 3; len--)
624 *buf++ = readb(info->regs + S3C2440_NFDATA);
625 }
Matt Reimerb773bb22007-10-18 17:43:07 -0700626}
627
David Woodhousee0c7d762006-05-13 18:07:53 +0100628static void s3c2410_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
630 struct nand_chip *this = mtd->priv;
631 writesb(this->IO_ADDR_W, buf, len);
632}
633
Matt Reimerb773bb22007-10-18 17:43:07 -0700634static void s3c2440_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
635{
636 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
Ben Dooksdea2aa62009-05-30 18:30:18 +0100637
638 writesl(info->regs + S3C2440_NFDATA, buf, len >> 2);
639
640 /* cleanup any fractional write */
641 if (len & 3) {
642 buf += len & ~3;
643
644 for (; len & 3; len--, buf++)
645 writeb(*buf, info->regs + S3C2440_NFDATA);
646 }
Matt Reimerb773bb22007-10-18 17:43:07 -0700647}
648
Ben Dooks30821fe2008-07-15 11:58:31 +0100649/* cpufreq driver support */
650
651#ifdef CONFIG_CPU_FREQ
652
653static int s3c2410_nand_cpufreq_transition(struct notifier_block *nb,
654 unsigned long val, void *data)
655{
656 struct s3c2410_nand_info *info;
657 unsigned long newclk;
658
659 info = container_of(nb, struct s3c2410_nand_info, freq_transition);
660 newclk = clk_get_rate(info->clk);
661
662 if ((val == CPUFREQ_POSTCHANGE && newclk < info->clk_rate) ||
663 (val == CPUFREQ_PRECHANGE && newclk > info->clk_rate)) {
664 s3c2410_nand_setrate(info);
665 }
666
667 return 0;
668}
669
670static inline int s3c2410_nand_cpufreq_register(struct s3c2410_nand_info *info)
671{
672 info->freq_transition.notifier_call = s3c2410_nand_cpufreq_transition;
673
674 return cpufreq_register_notifier(&info->freq_transition,
675 CPUFREQ_TRANSITION_NOTIFIER);
676}
677
678static inline void s3c2410_nand_cpufreq_deregister(struct s3c2410_nand_info *info)
679{
680 cpufreq_unregister_notifier(&info->freq_transition,
681 CPUFREQ_TRANSITION_NOTIFIER);
682}
683
684#else
685static inline int s3c2410_nand_cpufreq_register(struct s3c2410_nand_info *info)
686{
687 return 0;
688}
689
690static inline void s3c2410_nand_cpufreq_deregister(struct s3c2410_nand_info *info)
691{
692}
693#endif
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695/* device management functions */
696
Ben Dooksec0482e2009-05-30 16:55:29 +0100697static int s3c24xx_nand_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
Russell King3ae5eae2005-11-09 22:32:44 +0000699 struct s3c2410_nand_info *info = to_nand_info(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Russell King3ae5eae2005-11-09 22:32:44 +0000701 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000703 if (info == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return 0;
705
Ben Dooks30821fe2008-07-15 11:58:31 +0100706 s3c2410_nand_cpufreq_deregister(info);
707
708 /* Release all our mtds and their partitions, then go through
709 * freeing the resources used
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if (info->mtds != NULL) {
713 struct s3c2410_nand_mtd *ptr = info->mtds;
714 int mtdno;
715
716 for (mtdno = 0; mtdno < info->mtd_count; mtdno++, ptr++) {
717 pr_debug("releasing mtd %d (%p)\n", mtdno, ptr);
718 nand_release(&ptr->mtd);
719 }
720
721 kfree(info->mtds);
722 }
723
724 /* free the common resources */
725
726 if (info->clk != NULL && !IS_ERR(info->clk)) {
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200727 s3c2410_nand_clk_set_state(info, CLOCK_DISABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 clk_put(info->clk);
729 }
730
731 if (info->regs != NULL) {
732 iounmap(info->regs);
733 info->regs = NULL;
734 }
735
736 if (info->area != NULL) {
737 release_resource(info->area);
738 kfree(info->area);
739 info->area = NULL;
740 }
741
742 kfree(info);
743
744 return 0;
745}
746
Andy Greened27f022009-05-10 15:42:09 -0500747const char *part_probes[] = { "cmdlinepart", NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748static int s3c2410_nand_add_partition(struct s3c2410_nand_info *info,
749 struct s3c2410_nand_mtd *mtd,
750 struct s3c2410_nand_set *set)
751{
Andy Greened27f022009-05-10 15:42:09 -0500752 struct mtd_partition *part_info;
753 int nr_part = 0;
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 if (set == NULL)
Jamie Ilesaf86ea82011-05-23 10:23:33 +0100756 return mtd_device_register(&mtd->mtd, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Conke Hu78d10222010-07-13 12:56:30 +0800758 mtd->mtd.name = set->name;
759 nr_part = parse_mtd_partitions(&mtd->mtd, part_probes, &part_info, 0);
760
761 if (nr_part <= 0 && set->nr_partitions > 0) {
762 nr_part = set->nr_partitions;
763 part_info = set->partitions;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 }
765
Jamie Ilesaf86ea82011-05-23 10:23:33 +0100766 return mtd_device_register(&mtd->mtd, part_info, nr_part);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Ben Dooks3db72152009-05-30 17:18:15 +0100769/**
770 * s3c2410_nand_init_chip - initialise a single instance of an chip
771 * @info: The base NAND controller the chip is on.
772 * @nmtd: The new controller MTD instance to fill in.
773 * @set: The information passed from the board specific platform data.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 *
Ben Dooks3db72152009-05-30 17:18:15 +0100775 * Initialise the given @nmtd from the information in @info and @set. This
776 * readies the structure for use with the MTD layer functions by ensuring
777 * all pointers are setup and the necessary control routines selected.
778 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779static void s3c2410_nand_init_chip(struct s3c2410_nand_info *info,
780 struct s3c2410_nand_mtd *nmtd,
781 struct s3c2410_nand_set *set)
782{
783 struct nand_chip *chip = &nmtd->chip;
Ben Dooks2c06a082006-06-27 14:35:46 +0100784 void __iomem *regs = info->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 chip->write_buf = s3c2410_nand_write_buf;
787 chip->read_buf = s3c2410_nand_read_buf;
788 chip->select_chip = s3c2410_nand_select_chip;
789 chip->chip_delay = 50;
790 chip->priv = nmtd;
Ben Dooks74218fe2009-11-02 18:12:51 +0000791 chip->options = set->options;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 chip->controller = &info->controller;
793
Ben Dooks2c06a082006-06-27 14:35:46 +0100794 switch (info->cpu_type) {
795 case TYPE_S3C2410:
796 chip->IO_ADDR_W = regs + S3C2410_NFDATA;
797 info->sel_reg = regs + S3C2410_NFCONF;
798 info->sel_bit = S3C2410_NFCONF_nFCE;
799 chip->cmd_ctrl = s3c2410_nand_hwcontrol;
800 chip->dev_ready = s3c2410_nand_devready;
801 break;
802
803 case TYPE_S3C2440:
804 chip->IO_ADDR_W = regs + S3C2440_NFDATA;
805 info->sel_reg = regs + S3C2440_NFCONT;
806 info->sel_bit = S3C2440_NFCONT_nFCE;
807 chip->cmd_ctrl = s3c2440_nand_hwcontrol;
808 chip->dev_ready = s3c2440_nand_devready;
Matt Reimerb773bb22007-10-18 17:43:07 -0700809 chip->read_buf = s3c2440_nand_read_buf;
810 chip->write_buf = s3c2440_nand_write_buf;
Ben Dooks2c06a082006-06-27 14:35:46 +0100811 break;
812
813 case TYPE_S3C2412:
814 chip->IO_ADDR_W = regs + S3C2440_NFDATA;
815 info->sel_reg = regs + S3C2440_NFCONT;
816 info->sel_bit = S3C2412_NFCONT_nFCE0;
817 chip->cmd_ctrl = s3c2440_nand_hwcontrol;
818 chip->dev_ready = s3c2412_nand_devready;
819
820 if (readl(regs + S3C2410_NFCONF) & S3C2412_NFCONF_NANDBOOT)
821 dev_info(info->device, "System booted from NAND\n");
822
823 break;
824 }
825
826 chip->IO_ADDR_R = chip->IO_ADDR_W;
Ben Dooksa4f957f2005-06-20 12:48:25 +0100827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 nmtd->info = info;
829 nmtd->mtd.priv = chip;
David Woodhouse552d9202006-05-14 01:20:46 +0100830 nmtd->mtd.owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 nmtd->set = set;
832
833 if (hardware_ecc) {
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +0200834 chip->ecc.calculate = s3c2410_nand_calculate_ecc;
Ben Dooks2c06a082006-06-27 14:35:46 +0100835 chip->ecc.correct = s3c2410_nand_correct_data;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +0200836 chip->ecc.mode = NAND_ECC_HW;
Ben Dooksa4f957f2005-06-20 12:48:25 +0100837
Ben Dooks2c06a082006-06-27 14:35:46 +0100838 switch (info->cpu_type) {
839 case TYPE_S3C2410:
840 chip->ecc.hwctl = s3c2410_nand_enable_hwecc;
841 chip->ecc.calculate = s3c2410_nand_calculate_ecc;
842 break;
843
844 case TYPE_S3C2412:
Matthieu CASTET4f659922007-02-13 12:30:38 +0100845 chip->ecc.hwctl = s3c2412_nand_enable_hwecc;
846 chip->ecc.calculate = s3c2412_nand_calculate_ecc;
847 break;
848
Ben Dooks2c06a082006-06-27 14:35:46 +0100849 case TYPE_S3C2440:
850 chip->ecc.hwctl = s3c2440_nand_enable_hwecc;
851 chip->ecc.calculate = s3c2440_nand_calculate_ecc;
852 break;
853
Ben Dooksa4f957f2005-06-20 12:48:25 +0100854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 } else {
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +0200856 chip->ecc.mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 }
Ben Dooks1c21ab62008-04-15 11:36:21 +0100858
859 if (set->ecc_layout != NULL)
860 chip->ecc.layout = set->ecc_layout;
Ben Dooks37e5ffa2008-04-15 11:36:22 +0100861
862 if (set->disable_ecc)
863 chip->ecc.mode = NAND_ECC_NONE;
Andy Green8c3e8432009-05-10 15:41:25 -0500864
865 switch (chip->ecc.mode) {
866 case NAND_ECC_NONE:
867 dev_info(info->device, "NAND ECC disabled\n");
868 break;
869 case NAND_ECC_SOFT:
870 dev_info(info->device, "NAND soft ECC\n");
871 break;
872 case NAND_ECC_HW:
873 dev_info(info->device, "NAND hardware ECC\n");
874 break;
875 default:
876 dev_info(info->device, "NAND ECC UNKNOWN\n");
877 break;
878 }
Michel Pollet9db41f92009-05-13 16:54:14 +0100879
880 /* If you use u-boot BBT creation code, specifying this flag will
881 * let the kernel fish out the BBT from the NAND, and also skip the
882 * full NAND scan that can take 1/2s or so. Little things... */
Brian Norrisa40f7342011-05-31 16:31:22 -0700883 if (set->flash_bbt) {
884 chip->bbt_options |= NAND_USE_FLASH_BBT;
885 chip->options |= NAND_SKIP_BBTSCAN;
886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
888
Ben Dooks3db72152009-05-30 17:18:15 +0100889/**
890 * s3c2410_nand_update_chip - post probe update
891 * @info: The controller instance.
892 * @nmtd: The driver version of the MTD instance.
Ben Dooks71d54f32008-04-15 11:36:19 +0100893 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200894 * This routine is called after the chip probe has successfully completed
Ben Dooks3db72152009-05-30 17:18:15 +0100895 * and the relevant per-chip information updated. This call ensure that
896 * we update the internal state accordingly.
897 *
898 * The internal state is currently limited to the ECC state information.
899*/
Ben Dooks71d54f32008-04-15 11:36:19 +0100900static void s3c2410_nand_update_chip(struct s3c2410_nand_info *info,
901 struct s3c2410_nand_mtd *nmtd)
902{
903 struct nand_chip *chip = &nmtd->chip;
904
Ben Dooks451d3392008-05-20 17:32:14 +0100905 dev_dbg(info->device, "chip %p => page shift %d\n",
906 chip, chip->page_shift);
Ben Dooks71d54f32008-04-15 11:36:19 +0100907
Andy Green8c3e8432009-05-10 15:41:25 -0500908 if (chip->ecc.mode != NAND_ECC_HW)
909 return;
910
Ben Dooks71d54f32008-04-15 11:36:19 +0100911 /* change the behaviour depending on wether we are using
912 * the large or small page nand device */
913
Andy Green8c3e8432009-05-10 15:41:25 -0500914 if (chip->page_shift > 10) {
915 chip->ecc.size = 256;
916 chip->ecc.bytes = 3;
917 } else {
918 chip->ecc.size = 512;
919 chip->ecc.bytes = 3;
920 chip->ecc.layout = &nand_hw_eccoob;
Ben Dooks71d54f32008-04-15 11:36:19 +0100921 }
922}
923
Ben Dooksec0482e2009-05-30 16:55:29 +0100924/* s3c24xx_nand_probe
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 *
926 * called by device layer when it finds a device matching
927 * one our driver can handled. This code checks to see if
928 * it can allocate all necessary resources then calls the
929 * nand layer to look for devices
930*/
Ben Dooksec0482e2009-05-30 16:55:29 +0100931static int s3c24xx_nand_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
Russell King3ae5eae2005-11-09 22:32:44 +0000933 struct s3c2410_platform_nand *plat = to_nand_plat(pdev);
Ben Dooksec0482e2009-05-30 16:55:29 +0100934 enum s3c_cpu_type cpu_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 struct s3c2410_nand_info *info;
936 struct s3c2410_nand_mtd *nmtd;
937 struct s3c2410_nand_set *sets;
938 struct resource *res;
939 int err = 0;
940 int size;
941 int nr_sets;
942 int setno;
943
Ben Dooksec0482e2009-05-30 16:55:29 +0100944 cpu_type = platform_get_device_id(pdev)->driver_data;
945
Russell King3ae5eae2005-11-09 22:32:44 +0000946 pr_debug("s3c2410_nand_probe(%p)\n", pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Julia Lawallecce2a62010-05-13 22:07:46 +0200948 info = kzalloc(sizeof(*info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (info == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000950 dev_err(&pdev->dev, "no memory for flash info\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 err = -ENOMEM;
952 goto exit_error;
953 }
954
Russell King3ae5eae2005-11-09 22:32:44 +0000955 platform_set_drvdata(pdev, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957 spin_lock_init(&info->controller.lock);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100958 init_waitqueue_head(&info->controller.wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 /* get the clock source and enable it */
961
Russell King3ae5eae2005-11-09 22:32:44 +0000962 info->clk = clk_get(&pdev->dev, "nand");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 if (IS_ERR(info->clk)) {
Joe Perches898eb712007-10-18 03:06:30 -0700964 dev_err(&pdev->dev, "failed to get clock\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 err = -ENOENT;
966 goto exit_error;
967 }
968
Jiri Pinkavaac497c12011-04-13 11:59:30 +0200969 s3c2410_nand_clk_set_state(info, CLOCK_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971 /* allocate and map the resource */
972
Ben Dooksa4f957f2005-06-20 12:48:25 +0100973 /* currently we assume we have the one resource */
974 res = pdev->resource;
H Hartley Sweetenfc161c42009-12-14 16:56:22 -0500975 size = resource_size(res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 info->area = request_mem_region(res->start, size, pdev->name);
978
979 if (info->area == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000980 dev_err(&pdev->dev, "cannot reserve register region\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 err = -ENOENT;
982 goto exit_error;
983 }
984
Russell King3ae5eae2005-11-09 22:32:44 +0000985 info->device = &pdev->dev;
Ben Dooksa4f957f2005-06-20 12:48:25 +0100986 info->platform = plat;
987 info->regs = ioremap(res->start, size);
Ben Dooks2c06a082006-06-27 14:35:46 +0100988 info->cpu_type = cpu_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 if (info->regs == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000991 dev_err(&pdev->dev, "cannot reserve register region\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 err = -EIO;
993 goto exit_error;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Russell King3ae5eae2005-11-09 22:32:44 +0000996 dev_dbg(&pdev->dev, "mapped registers at %p\n", info->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998 /* initialise the hardware */
999
Ben Dooks30821fe2008-07-15 11:58:31 +01001000 err = s3c2410_nand_inithw(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 if (err != 0)
1002 goto exit_error;
1003
1004 sets = (plat != NULL) ? plat->sets : NULL;
1005 nr_sets = (plat != NULL) ? plat->nr_sets : 1;
1006
1007 info->mtd_count = nr_sets;
1008
1009 /* allocate our information */
1010
1011 size = nr_sets * sizeof(*info->mtds);
Julia Lawallecce2a62010-05-13 22:07:46 +02001012 info->mtds = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 if (info->mtds == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +00001014 dev_err(&pdev->dev, "failed to allocate mtd storage\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 err = -ENOMEM;
1016 goto exit_error;
1017 }
1018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 /* initialise all possible chips */
1020
1021 nmtd = info->mtds;
1022
1023 for (setno = 0; setno < nr_sets; setno++, nmtd++) {
David Woodhousee0c7d762006-05-13 18:07:53 +01001024 pr_debug("initialising set %d (%p, info %p)\n", setno, nmtd, info);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 s3c2410_nand_init_chip(info, nmtd, sets);
1027
Ben Dooks71d54f32008-04-15 11:36:19 +01001028 nmtd->scan_res = nand_scan_ident(&nmtd->mtd,
David Woodhouse5e81e882010-02-26 18:32:56 +00001029 (sets) ? sets->nr_chips : 1,
1030 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032 if (nmtd->scan_res == 0) {
Ben Dooks71d54f32008-04-15 11:36:19 +01001033 s3c2410_nand_update_chip(info, nmtd);
1034 nand_scan_tail(&nmtd->mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 s3c2410_nand_add_partition(info, nmtd, sets);
1036 }
1037
1038 if (sets != NULL)
1039 sets++;
1040 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001041
Ben Dooks30821fe2008-07-15 11:58:31 +01001042 err = s3c2410_nand_cpufreq_register(info);
1043 if (err < 0) {
1044 dev_err(&pdev->dev, "failed to init cpufreq support\n");
1045 goto exit_error;
1046 }
1047
Jiri Pinkavaac497c12011-04-13 11:59:30 +02001048 if (allow_clk_suspend(info)) {
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001049 dev_info(&pdev->dev, "clock idle support enabled\n");
Jiri Pinkavaac497c12011-04-13 11:59:30 +02001050 s3c2410_nand_clk_set_state(info, CLOCK_SUSPEND);
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001051 }
1052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 pr_debug("initialised ok\n");
1054 return 0;
1055
1056 exit_error:
Ben Dooksec0482e2009-05-30 16:55:29 +01001057 s3c24xx_nand_remove(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
1059 if (err == 0)
1060 err = -EINVAL;
1061 return err;
1062}
1063
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001064/* PM Support */
1065#ifdef CONFIG_PM
1066
1067static int s3c24xx_nand_suspend(struct platform_device *dev, pm_message_t pm)
1068{
1069 struct s3c2410_nand_info *info = platform_get_drvdata(dev);
1070
1071 if (info) {
Ben Dooks09160832008-04-15 11:36:18 +01001072 info->save_sel = readl(info->sel_reg);
Ben Dooks03680b12007-11-19 23:28:07 +00001073
1074 /* For the moment, we must ensure nFCE is high during
1075 * the time we are suspended. This really should be
1076 * handled by suspending the MTDs we are using, but
1077 * that is currently not the case. */
1078
Ben Dooks09160832008-04-15 11:36:18 +01001079 writel(info->save_sel | info->sel_bit, info->sel_reg);
Ben Dooks03680b12007-11-19 23:28:07 +00001080
Jiri Pinkavaac497c12011-04-13 11:59:30 +02001081 s3c2410_nand_clk_set_state(info, CLOCK_DISABLE);
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001082 }
1083
1084 return 0;
1085}
1086
1087static int s3c24xx_nand_resume(struct platform_device *dev)
1088{
1089 struct s3c2410_nand_info *info = platform_get_drvdata(dev);
Ben Dooks09160832008-04-15 11:36:18 +01001090 unsigned long sel;
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001091
1092 if (info) {
Jiri Pinkavaac497c12011-04-13 11:59:30 +02001093 s3c2410_nand_clk_set_state(info, CLOCK_ENABLE);
Ben Dooks30821fe2008-07-15 11:58:31 +01001094 s3c2410_nand_inithw(info);
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001095
Ben Dooks03680b12007-11-19 23:28:07 +00001096 /* Restore the state of the nFCE line. */
1097
Ben Dooks09160832008-04-15 11:36:18 +01001098 sel = readl(info->sel_reg);
1099 sel &= ~info->sel_bit;
1100 sel |= info->save_sel & info->sel_bit;
1101 writel(sel, info->sel_reg);
Ben Dooks03680b12007-11-19 23:28:07 +00001102
Jiri Pinkavaac497c12011-04-13 11:59:30 +02001103 s3c2410_nand_clk_set_state(info, CLOCK_SUSPEND);
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001104 }
1105
1106 return 0;
1107}
1108
1109#else
1110#define s3c24xx_nand_suspend NULL
1111#define s3c24xx_nand_resume NULL
1112#endif
1113
Ben Dooksa4f957f2005-06-20 12:48:25 +01001114/* driver device registration */
1115
Ben Dooksec0482e2009-05-30 16:55:29 +01001116static struct platform_device_id s3c24xx_driver_ids[] = {
1117 {
1118 .name = "s3c2410-nand",
1119 .driver_data = TYPE_S3C2410,
1120 }, {
1121 .name = "s3c2440-nand",
1122 .driver_data = TYPE_S3C2440,
1123 }, {
1124 .name = "s3c2412-nand",
1125 .driver_data = TYPE_S3C2412,
Peter Korsgaard9dbc0902009-06-07 06:04:23 -07001126 }, {
1127 .name = "s3c6400-nand",
1128 .driver_data = TYPE_S3C2412, /* compatible with 2412 */
Russell King3ae5eae2005-11-09 22:32:44 +00001129 },
Ben Dooksec0482e2009-05-30 16:55:29 +01001130 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131};
1132
Ben Dooksec0482e2009-05-30 16:55:29 +01001133MODULE_DEVICE_TABLE(platform, s3c24xx_driver_ids);
Ben Dooksa4f957f2005-06-20 12:48:25 +01001134
Ben Dooksec0482e2009-05-30 16:55:29 +01001135static struct platform_driver s3c24xx_nand_driver = {
1136 .probe = s3c24xx_nand_probe,
1137 .remove = s3c24xx_nand_remove,
Ben Dooks2c06a082006-06-27 14:35:46 +01001138 .suspend = s3c24xx_nand_suspend,
1139 .resume = s3c24xx_nand_resume,
Ben Dooksec0482e2009-05-30 16:55:29 +01001140 .id_table = s3c24xx_driver_ids,
Ben Dooks2c06a082006-06-27 14:35:46 +01001141 .driver = {
Ben Dooksec0482e2009-05-30 16:55:29 +01001142 .name = "s3c24xx-nand",
Ben Dooks2c06a082006-06-27 14:35:46 +01001143 .owner = THIS_MODULE,
1144 },
1145};
1146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147static int __init s3c2410_nand_init(void)
1148{
Ben Dooksa4f957f2005-06-20 12:48:25 +01001149 printk("S3C24XX NAND Driver, (c) 2004 Simtec Electronics\n");
1150
Ben Dooksec0482e2009-05-30 16:55:29 +01001151 return platform_driver_register(&s3c24xx_nand_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152}
1153
1154static void __exit s3c2410_nand_exit(void)
1155{
Ben Dooksec0482e2009-05-30 16:55:29 +01001156 platform_driver_unregister(&s3c24xx_nand_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157}
1158
1159module_init(s3c2410_nand_init);
1160module_exit(s3c2410_nand_exit);
1161
1162MODULE_LICENSE("GPL");
1163MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
Ben Dooksa4f957f2005-06-20 12:48:25 +01001164MODULE_DESCRIPTION("S3C24XX MTD NAND driver");