blob: 556139ed1fdf8b50919dfa078fe74a98626008d6 [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 Dooksb7a70182007-07-24 13:37:27 +010048#include <asm/plat-s3c/regs-nand.h>
49#include <asm/plat-s3c/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
58static int clock_stop = 1;
59#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
77struct s3c2410_nand_mtd {
78 struct mtd_info mtd;
79 struct nand_chip chip;
80 struct s3c2410_nand_set *set;
81 struct s3c2410_nand_info *info;
82 int scan_res;
83};
84
Ben Dooks2c06a082006-06-27 14:35:46 +010085enum s3c_cpu_type {
86 TYPE_S3C2410,
87 TYPE_S3C2412,
88 TYPE_S3C2440,
89};
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091/* overview of the s3c2410 nand state */
92
93struct s3c2410_nand_info {
94 /* mtd info */
95 struct nand_hw_control controller;
96 struct s3c2410_nand_mtd *mtds;
97 struct s3c2410_platform_nand *platform;
98
99 /* device info */
100 struct device *device;
101 struct resource *area;
102 struct clk *clk;
Ben Dooksfdf2fd52005-02-18 14:46:15 +0000103 void __iomem *regs;
Ben Dooks2c06a082006-06-27 14:35:46 +0100104 void __iomem *sel_reg;
105 int sel_bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 int mtd_count;
Ben Dooks09160832008-04-15 11:36:18 +0100107 unsigned long save_sel;
Ben Dooks30821fe2008-07-15 11:58:31 +0100108 unsigned long clk_rate;
Ben Dooks03680b12007-11-19 23:28:07 +0000109
Ben Dooks2c06a082006-06-27 14:35:46 +0100110 enum s3c_cpu_type cpu_type;
Ben Dooks30821fe2008-07-15 11:58:31 +0100111
112#ifdef CONFIG_CPU_FREQ
113 struct notifier_block freq_transition;
114#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115};
116
117/* conversion functions */
118
119static struct s3c2410_nand_mtd *s3c2410_nand_mtd_toours(struct mtd_info *mtd)
120{
121 return container_of(mtd, struct s3c2410_nand_mtd, mtd);
122}
123
124static struct s3c2410_nand_info *s3c2410_nand_mtd_toinfo(struct mtd_info *mtd)
125{
126 return s3c2410_nand_mtd_toours(mtd)->info;
127}
128
Russell King3ae5eae2005-11-09 22:32:44 +0000129static struct s3c2410_nand_info *to_nand_info(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Russell King3ae5eae2005-11-09 22:32:44 +0000131 return platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
Russell King3ae5eae2005-11-09 22:32:44 +0000134static struct s3c2410_platform_nand *to_nand_plat(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Russell King3ae5eae2005-11-09 22:32:44 +0000136 return dev->dev.platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100139static inline int allow_clk_stop(struct s3c2410_nand_info *info)
140{
141 return clock_stop;
142}
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/* timing calculations */
145
Ben Dookscfd320f2005-10-20 22:22:58 +0100146#define NS_IN_KHZ 1000000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Ben Dooks2c06a082006-06-27 14:35:46 +0100148static int s3c_nand_calc_rate(int wanted, unsigned long clk, int max)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
150 int result;
151
Ben Dookscfd320f2005-10-20 22:22:58 +0100152 result = (wanted * clk) / NS_IN_KHZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 result++;
154
155 pr_debug("result %d from %ld, %d\n", result, clk, wanted);
156
157 if (result > max) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100158 printk("%d ns is too big for current clock rate %ld\n", wanted, clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 return -1;
160 }
161
162 if (result < 1)
163 result = 1;
164
165 return result;
166}
167
Ben Dookscfd320f2005-10-20 22:22:58 +0100168#define to_ns(ticks,clk) (((ticks) * NS_IN_KHZ) / (unsigned int)(clk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170/* controller setup */
171
Ben Dooks30821fe2008-07-15 11:58:31 +0100172static int s3c2410_nand_setrate(struct s3c2410_nand_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Ben Dooks30821fe2008-07-15 11:58:31 +0100174 struct s3c2410_platform_nand *plat = info->platform;
Ben Dooks2c06a082006-06-27 14:35:46 +0100175 int tacls_max = (info->cpu_type == TYPE_S3C2412) ? 8 : 4;
Ben Dookscfd320f2005-10-20 22:22:58 +0100176 int tacls, twrph0, twrph1;
Ben Dooks30821fe2008-07-15 11:58:31 +0100177 unsigned long clkrate = clk_get_rate(info->clk);
178 unsigned long set, cfg, mask;
179 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 /* calculate the timing information for the controller */
182
Ben Dooks30821fe2008-07-15 11:58:31 +0100183 info->clk_rate = clkrate;
Ben Dookscfd320f2005-10-20 22:22:58 +0100184 clkrate /= 1000; /* turn clock into kHz for ease of use */
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 if (plat != NULL) {
Ben Dooks2c06a082006-06-27 14:35:46 +0100187 tacls = s3c_nand_calc_rate(plat->tacls, clkrate, tacls_max);
188 twrph0 = s3c_nand_calc_rate(plat->twrph0, clkrate, 8);
189 twrph1 = s3c_nand_calc_rate(plat->twrph1, clkrate, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 } else {
191 /* default timings */
Ben Dooks2c06a082006-06-27 14:35:46 +0100192 tacls = tacls_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 twrph0 = 8;
194 twrph1 = 8;
195 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 if (tacls < 0 || twrph0 < 0 || twrph1 < 0) {
Ben Dooks99974c62006-06-21 15:43:05 +0100198 dev_err(info->device, "cannot get suitable timings\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return -EINVAL;
200 }
201
Ben Dooks99974c62006-06-21 15:43:05 +0100202 dev_info(info->device, "Tacls=%d, %dns Twrph0=%d %dns, Twrph1=%d %dns\n",
David Woodhousee0c7d762006-05-13 18:07:53 +0100203 tacls, to_ns(tacls, clkrate), twrph0, to_ns(twrph0, clkrate), twrph1, to_ns(twrph1, clkrate));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Ben Dooks30821fe2008-07-15 11:58:31 +0100205 switch (info->cpu_type) {
206 case TYPE_S3C2410:
207 mask = (S3C2410_NFCONF_TACLS(3) |
208 S3C2410_NFCONF_TWRPH0(7) |
209 S3C2410_NFCONF_TWRPH1(7));
210 set = S3C2410_NFCONF_EN;
211 set |= S3C2410_NFCONF_TACLS(tacls - 1);
212 set |= S3C2410_NFCONF_TWRPH0(twrph0 - 1);
213 set |= S3C2410_NFCONF_TWRPH1(twrph1 - 1);
214 break;
215
216 case TYPE_S3C2440:
217 case TYPE_S3C2412:
218 mask = (S3C2410_NFCONF_TACLS(tacls_max - 1) |
219 S3C2410_NFCONF_TWRPH0(7) |
220 S3C2410_NFCONF_TWRPH1(7));
221
222 set = S3C2440_NFCONF_TACLS(tacls - 1);
223 set |= S3C2440_NFCONF_TWRPH0(twrph0 - 1);
224 set |= S3C2440_NFCONF_TWRPH1(twrph1 - 1);
225 break;
226
227 default:
228 /* keep compiler happy */
229 mask = 0;
230 set = 0;
231 BUG();
232 }
233
234 dev_dbg(info->device, "NF_CONF is 0x%lx\n", cfg);
235
236 local_irq_save(flags);
237
238 cfg = readl(info->regs + S3C2410_NFCONF);
239 cfg &= ~mask;
240 cfg |= set;
241 writel(cfg, info->regs + S3C2410_NFCONF);
242
243 local_irq_restore(flags);
244
245 return 0;
246}
247
248static int s3c2410_nand_inithw(struct s3c2410_nand_info *info)
249{
250 int ret;
251
252 ret = s3c2410_nand_setrate(info);
253 if (ret < 0)
254 return ret;
255
Ben Dooks2c06a082006-06-27 14:35:46 +0100256 switch (info->cpu_type) {
257 case TYPE_S3C2410:
Ben Dooks30821fe2008-07-15 11:58:31 +0100258 default:
Ben Dooks2c06a082006-06-27 14:35:46 +0100259 break;
260
261 case TYPE_S3C2440:
262 case TYPE_S3C2412:
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100263 /* enable the controller and de-assert nFCE */
264
Ben Dooks2c06a082006-06-27 14:35:46 +0100265 writel(S3C2440_NFCONT_ENABLE, info->regs + S3C2440_NFCONT);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return 0;
269}
270
271/* select chip */
272
273static void s3c2410_nand_select_chip(struct mtd_info *mtd, int chip)
274{
275 struct s3c2410_nand_info *info;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000276 struct s3c2410_nand_mtd *nmtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 struct nand_chip *this = mtd->priv;
278 unsigned long cur;
279
280 nmtd = this->priv;
281 info = nmtd->info;
282
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100283 if (chip != -1 && allow_clk_stop(info))
284 clk_enable(info->clk);
285
Ben Dooks2c06a082006-06-27 14:35:46 +0100286 cur = readl(info->sel_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 if (chip == -1) {
Ben Dooks2c06a082006-06-27 14:35:46 +0100289 cur |= info->sel_bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 } else {
Ben Dooksfb8d82a2005-07-06 21:05:10 +0100291 if (nmtd->set != NULL && chip > nmtd->set->nr_chips) {
Ben Dooks99974c62006-06-21 15:43:05 +0100292 dev_err(info->device, "invalid chip %d\n", chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return;
294 }
295
296 if (info->platform != NULL) {
297 if (info->platform->select_chip != NULL)
David Woodhousee0c7d762006-05-13 18:07:53 +0100298 (info->platform->select_chip) (nmtd->set, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300
Ben Dooks2c06a082006-06-27 14:35:46 +0100301 cur &= ~info->sel_bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
303
Ben Dooks2c06a082006-06-27 14:35:46 +0100304 writel(cur, info->sel_reg);
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100305
306 if (chip == -1 && allow_clk_stop(info))
307 clk_disable(info->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
309
Ben Dooksad3b5fb2006-06-19 09:43:23 +0100310/* s3c2410_nand_hwcontrol
Ben Dooksa4f957f2005-06-20 12:48:25 +0100311 *
Ben Dooksad3b5fb2006-06-19 09:43:23 +0100312 * Issue command and address cycles to the chip
Ben Dooksa4f957f2005-06-20 12:48:25 +0100313*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200315static void s3c2410_nand_hwcontrol(struct mtd_info *mtd, int cmd,
David Woodhousef9068872006-06-10 00:53:16 +0100316 unsigned int ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
318 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
David Woodhousec9ac5972006-11-30 08:17:38 +0000319
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200320 if (cmd == NAND_CMD_NONE)
321 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
David Woodhousef9068872006-06-10 00:53:16 +0100323 if (ctrl & NAND_CLE)
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200324 writeb(cmd, info->regs + S3C2410_NFCMD);
325 else
326 writeb(cmd, info->regs + S3C2410_NFADDR);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100327}
328
329/* command and control functions */
330
David Woodhousef9068872006-06-10 00:53:16 +0100331static void s3c2440_nand_hwcontrol(struct mtd_info *mtd, int cmd,
332 unsigned int ctrl)
Ben Dooksa4f957f2005-06-20 12:48:25 +0100333{
334 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100335
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200336 if (cmd == NAND_CMD_NONE)
337 return;
Ben Dooksa4f957f2005-06-20 12:48:25 +0100338
David Woodhousef9068872006-06-10 00:53:16 +0100339 if (ctrl & NAND_CLE)
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200340 writeb(cmd, info->regs + S3C2440_NFCMD);
341 else
342 writeb(cmd, info->regs + S3C2440_NFADDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345/* s3c2410_nand_devready()
346 *
347 * returns 0 if the nand is busy, 1 if it is ready
348*/
349
350static int s3c2410_nand_devready(struct mtd_info *mtd)
351{
352 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return readb(info->regs + S3C2410_NFSTAT) & S3C2410_NFSTAT_BUSY;
354}
355
Ben Dooks2c06a082006-06-27 14:35:46 +0100356static int s3c2440_nand_devready(struct mtd_info *mtd)
357{
358 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
359 return readb(info->regs + S3C2440_NFSTAT) & S3C2440_NFSTAT_READY;
360}
361
362static int s3c2412_nand_devready(struct mtd_info *mtd)
363{
364 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
365 return readb(info->regs + S3C2412_NFSTAT) & S3C2412_NFSTAT_READY;
366}
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368/* ECC handling functions */
369
Ben Dooks2c06a082006-06-27 14:35:46 +0100370static int s3c2410_nand_correct_data(struct mtd_info *mtd, u_char *dat,
371 u_char *read_ecc, u_char *calc_ecc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
Ben Dooksa2593242007-02-02 16:59:33 +0000373 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
374 unsigned int diff0, diff1, diff2;
375 unsigned int bit, byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Ben Dooksa2593242007-02-02 16:59:33 +0000377 pr_debug("%s(%p,%p,%p,%p)\n", __func__, mtd, dat, read_ecc, calc_ecc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Ben Dooksa2593242007-02-02 16:59:33 +0000379 diff0 = read_ecc[0] ^ calc_ecc[0];
380 diff1 = read_ecc[1] ^ calc_ecc[1];
381 diff2 = read_ecc[2] ^ calc_ecc[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Ben Dooksa2593242007-02-02 16:59:33 +0000383 pr_debug("%s: rd %02x%02x%02x calc %02x%02x%02x diff %02x%02x%02x\n",
384 __func__,
385 read_ecc[0], read_ecc[1], read_ecc[2],
386 calc_ecc[0], calc_ecc[1], calc_ecc[2],
387 diff0, diff1, diff2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Ben Dooksa2593242007-02-02 16:59:33 +0000389 if (diff0 == 0 && diff1 == 0 && diff2 == 0)
390 return 0; /* ECC is ok */
391
Ben Dooksc45c6c62008-04-15 11:36:20 +0100392 /* sometimes people do not think about using the ECC, so check
393 * to see if we have an 0xff,0xff,0xff read ECC and then ignore
394 * the error, on the assumption that this is an un-eccd page.
395 */
396 if (read_ecc[0] == 0xff && read_ecc[1] == 0xff && read_ecc[2] == 0xff
397 && info->platform->ignore_unset_ecc)
398 return 0;
399
Ben Dooksa2593242007-02-02 16:59:33 +0000400 /* Can we correct this ECC (ie, one row and column change).
401 * Note, this is similar to the 256 error code on smartmedia */
402
403 if (((diff0 ^ (diff0 >> 1)) & 0x55) == 0x55 &&
404 ((diff1 ^ (diff1 >> 1)) & 0x55) == 0x55 &&
405 ((diff2 ^ (diff2 >> 1)) & 0x55) == 0x55) {
406 /* calculate the bit position of the error */
407
Matt Reimerd0bf3792007-10-18 18:02:43 -0700408 bit = ((diff2 >> 3) & 1) |
409 ((diff2 >> 4) & 2) |
410 ((diff2 >> 5) & 4);
Ben Dooksa2593242007-02-02 16:59:33 +0000411
412 /* calculate the byte position of the error */
413
Matt Reimerd0bf3792007-10-18 18:02:43 -0700414 byte = ((diff2 << 7) & 0x100) |
415 ((diff1 << 0) & 0x80) |
416 ((diff1 << 1) & 0x40) |
417 ((diff1 << 2) & 0x20) |
418 ((diff1 << 3) & 0x10) |
419 ((diff0 >> 4) & 0x08) |
420 ((diff0 >> 3) & 0x04) |
421 ((diff0 >> 2) & 0x02) |
422 ((diff0 >> 1) & 0x01);
Ben Dooksa2593242007-02-02 16:59:33 +0000423
424 dev_dbg(info->device, "correcting error bit %d, byte %d\n",
425 bit, byte);
426
427 dat[byte] ^= (1 << bit);
428 return 1;
429 }
430
431 /* if there is only one bit difference in the ECC, then
432 * one of only a row or column parity has changed, which
433 * means the error is most probably in the ECC itself */
434
435 diff0 |= (diff1 << 8);
436 diff0 |= (diff2 << 16);
437
438 if ((diff0 & ~(1<<fls(diff0))) == 0)
439 return 1;
440
Matt Reimer4fac9f62007-10-18 18:02:44 -0700441 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
Ben Dooksa4f957f2005-06-20 12:48:25 +0100444/* ECC functions
445 *
446 * These allow the s3c2410 and s3c2440 to use the controller's ECC
447 * generator block to ECC the data as it passes through]
448*/
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450static void s3c2410_nand_enable_hwecc(struct mtd_info *mtd, int mode)
451{
452 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
453 unsigned long ctrl;
454
455 ctrl = readl(info->regs + S3C2410_NFCONF);
456 ctrl |= S3C2410_NFCONF_INITECC;
457 writel(ctrl, info->regs + S3C2410_NFCONF);
458}
459
Matthieu CASTET4f659922007-02-13 12:30:38 +0100460static void s3c2412_nand_enable_hwecc(struct mtd_info *mtd, int mode)
461{
462 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
463 unsigned long ctrl;
464
465 ctrl = readl(info->regs + S3C2440_NFCONT);
466 writel(ctrl | S3C2412_NFCONT_INIT_MAIN_ECC, info->regs + S3C2440_NFCONT);
467}
468
Ben Dooksa4f957f2005-06-20 12:48:25 +0100469static void s3c2440_nand_enable_hwecc(struct mtd_info *mtd, int mode)
470{
471 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
472 unsigned long ctrl;
473
474 ctrl = readl(info->regs + S3C2440_NFCONT);
475 writel(ctrl | S3C2440_NFCONT_INITECC, info->regs + S3C2440_NFCONT);
476}
477
David Woodhousee0c7d762006-05-13 18:07:53 +0100478static int s3c2410_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
480 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
481
482 ecc_code[0] = readb(info->regs + S3C2410_NFECC + 0);
483 ecc_code[1] = readb(info->regs + S3C2410_NFECC + 1);
484 ecc_code[2] = readb(info->regs + S3C2410_NFECC + 2);
485
Ben Dooksa2593242007-02-02 16:59:33 +0000486 pr_debug("%s: returning ecc %02x%02x%02x\n", __func__,
487 ecc_code[0], ecc_code[1], ecc_code[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 return 0;
490}
491
Matthieu CASTET4f659922007-02-13 12:30:38 +0100492static int s3c2412_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
493{
494 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
495 unsigned long ecc = readl(info->regs + S3C2412_NFMECC0);
496
497 ecc_code[0] = ecc;
498 ecc_code[1] = ecc >> 8;
499 ecc_code[2] = ecc >> 16;
500
501 pr_debug("calculate_ecc: returning ecc %02x,%02x,%02x\n", ecc_code[0], ecc_code[1], ecc_code[2]);
502
503 return 0;
504}
505
David Woodhousee0c7d762006-05-13 18:07:53 +0100506static int s3c2440_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
Ben Dooksa4f957f2005-06-20 12:48:25 +0100507{
508 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
509 unsigned long ecc = readl(info->regs + S3C2440_NFMECC0);
510
511 ecc_code[0] = ecc;
512 ecc_code[1] = ecc >> 8;
513 ecc_code[2] = ecc >> 16;
514
Ben Dooks71d54f32008-04-15 11:36:19 +0100515 pr_debug("%s: returning ecc %06lx\n", __func__, ecc & 0xffffff);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100516
517 return 0;
518}
519
Ben Dooksa4f957f2005-06-20 12:48:25 +0100520/* over-ride the standard functions for a little more speed. We can
521 * use read/write block to move the data buffers to/from the controller
522*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524static void s3c2410_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
525{
526 struct nand_chip *this = mtd->priv;
527 readsb(this->IO_ADDR_R, buf, len);
528}
529
Matt Reimerb773bb22007-10-18 17:43:07 -0700530static void s3c2440_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
531{
532 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
533 readsl(info->regs + S3C2440_NFDATA, buf, len / 4);
534}
535
David Woodhousee0c7d762006-05-13 18:07:53 +0100536static void s3c2410_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
538 struct nand_chip *this = mtd->priv;
539 writesb(this->IO_ADDR_W, buf, len);
540}
541
Matt Reimerb773bb22007-10-18 17:43:07 -0700542static void s3c2440_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
543{
544 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
545 writesl(info->regs + S3C2440_NFDATA, buf, len / 4);
546}
547
Ben Dooks30821fe2008-07-15 11:58:31 +0100548/* cpufreq driver support */
549
550#ifdef CONFIG_CPU_FREQ
551
552static int s3c2410_nand_cpufreq_transition(struct notifier_block *nb,
553 unsigned long val, void *data)
554{
555 struct s3c2410_nand_info *info;
556 unsigned long newclk;
557
558 info = container_of(nb, struct s3c2410_nand_info, freq_transition);
559 newclk = clk_get_rate(info->clk);
560
561 if ((val == CPUFREQ_POSTCHANGE && newclk < info->clk_rate) ||
562 (val == CPUFREQ_PRECHANGE && newclk > info->clk_rate)) {
563 s3c2410_nand_setrate(info);
564 }
565
566 return 0;
567}
568
569static inline int s3c2410_nand_cpufreq_register(struct s3c2410_nand_info *info)
570{
571 info->freq_transition.notifier_call = s3c2410_nand_cpufreq_transition;
572
573 return cpufreq_register_notifier(&info->freq_transition,
574 CPUFREQ_TRANSITION_NOTIFIER);
575}
576
577static inline void s3c2410_nand_cpufreq_deregister(struct s3c2410_nand_info *info)
578{
579 cpufreq_unregister_notifier(&info->freq_transition,
580 CPUFREQ_TRANSITION_NOTIFIER);
581}
582
583#else
584static inline int s3c2410_nand_cpufreq_register(struct s3c2410_nand_info *info)
585{
586 return 0;
587}
588
589static inline void s3c2410_nand_cpufreq_deregister(struct s3c2410_nand_info *info)
590{
591}
592#endif
593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594/* device management functions */
595
Russell King3ae5eae2005-11-09 22:32:44 +0000596static int s3c2410_nand_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Russell King3ae5eae2005-11-09 22:32:44 +0000598 struct s3c2410_nand_info *info = to_nand_info(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Russell King3ae5eae2005-11-09 22:32:44 +0000600 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000602 if (info == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 return 0;
604
Ben Dooks30821fe2008-07-15 11:58:31 +0100605 s3c2410_nand_cpufreq_deregister(info);
606
607 /* Release all our mtds and their partitions, then go through
608 * freeing the resources used
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 if (info->mtds != NULL) {
612 struct s3c2410_nand_mtd *ptr = info->mtds;
613 int mtdno;
614
615 for (mtdno = 0; mtdno < info->mtd_count; mtdno++, ptr++) {
616 pr_debug("releasing mtd %d (%p)\n", mtdno, ptr);
617 nand_release(&ptr->mtd);
618 }
619
620 kfree(info->mtds);
621 }
622
623 /* free the common resources */
624
625 if (info->clk != NULL && !IS_ERR(info->clk)) {
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100626 if (!allow_clk_stop(info))
627 clk_disable(info->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 clk_put(info->clk);
629 }
630
631 if (info->regs != NULL) {
632 iounmap(info->regs);
633 info->regs = NULL;
634 }
635
636 if (info->area != NULL) {
637 release_resource(info->area);
638 kfree(info->area);
639 info->area = NULL;
640 }
641
642 kfree(info);
643
644 return 0;
645}
646
647#ifdef CONFIG_MTD_PARTITIONS
648static int s3c2410_nand_add_partition(struct s3c2410_nand_info *info,
649 struct s3c2410_nand_mtd *mtd,
650 struct s3c2410_nand_set *set)
651{
652 if (set == NULL)
653 return add_mtd_device(&mtd->mtd);
654
655 if (set->nr_partitions > 0 && set->partitions != NULL) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100656 return add_mtd_partitions(&mtd->mtd, set->partitions, set->nr_partitions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 }
658
659 return add_mtd_device(&mtd->mtd);
660}
661#else
662static int s3c2410_nand_add_partition(struct s3c2410_nand_info *info,
663 struct s3c2410_nand_mtd *mtd,
664 struct s3c2410_nand_set *set)
665{
666 return add_mtd_device(&mtd->mtd);
667}
668#endif
669
670/* s3c2410_nand_init_chip
671 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000672 * init a single instance of an chip
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673*/
674
675static void s3c2410_nand_init_chip(struct s3c2410_nand_info *info,
676 struct s3c2410_nand_mtd *nmtd,
677 struct s3c2410_nand_set *set)
678{
679 struct nand_chip *chip = &nmtd->chip;
Ben Dooks2c06a082006-06-27 14:35:46 +0100680 void __iomem *regs = info->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 chip->write_buf = s3c2410_nand_write_buf;
683 chip->read_buf = s3c2410_nand_read_buf;
684 chip->select_chip = s3c2410_nand_select_chip;
685 chip->chip_delay = 50;
686 chip->priv = nmtd;
687 chip->options = 0;
688 chip->controller = &info->controller;
689
Ben Dooks2c06a082006-06-27 14:35:46 +0100690 switch (info->cpu_type) {
691 case TYPE_S3C2410:
692 chip->IO_ADDR_W = regs + S3C2410_NFDATA;
693 info->sel_reg = regs + S3C2410_NFCONF;
694 info->sel_bit = S3C2410_NFCONF_nFCE;
695 chip->cmd_ctrl = s3c2410_nand_hwcontrol;
696 chip->dev_ready = s3c2410_nand_devready;
697 break;
698
699 case TYPE_S3C2440:
700 chip->IO_ADDR_W = regs + S3C2440_NFDATA;
701 info->sel_reg = regs + S3C2440_NFCONT;
702 info->sel_bit = S3C2440_NFCONT_nFCE;
703 chip->cmd_ctrl = s3c2440_nand_hwcontrol;
704 chip->dev_ready = s3c2440_nand_devready;
Matt Reimerb773bb22007-10-18 17:43:07 -0700705 chip->read_buf = s3c2440_nand_read_buf;
706 chip->write_buf = s3c2440_nand_write_buf;
Ben Dooks2c06a082006-06-27 14:35:46 +0100707 break;
708
709 case TYPE_S3C2412:
710 chip->IO_ADDR_W = regs + S3C2440_NFDATA;
711 info->sel_reg = regs + S3C2440_NFCONT;
712 info->sel_bit = S3C2412_NFCONT_nFCE0;
713 chip->cmd_ctrl = s3c2440_nand_hwcontrol;
714 chip->dev_ready = s3c2412_nand_devready;
715
716 if (readl(regs + S3C2410_NFCONF) & S3C2412_NFCONF_NANDBOOT)
717 dev_info(info->device, "System booted from NAND\n");
718
719 break;
720 }
721
722 chip->IO_ADDR_R = chip->IO_ADDR_W;
Ben Dooksa4f957f2005-06-20 12:48:25 +0100723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 nmtd->info = info;
725 nmtd->mtd.priv = chip;
David Woodhouse552d9202006-05-14 01:20:46 +0100726 nmtd->mtd.owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 nmtd->set = set;
728
729 if (hardware_ecc) {
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +0200730 chip->ecc.calculate = s3c2410_nand_calculate_ecc;
Ben Dooks2c06a082006-06-27 14:35:46 +0100731 chip->ecc.correct = s3c2410_nand_correct_data;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +0200732 chip->ecc.mode = NAND_ECC_HW;
Ben Dooksa4f957f2005-06-20 12:48:25 +0100733
Ben Dooks2c06a082006-06-27 14:35:46 +0100734 switch (info->cpu_type) {
735 case TYPE_S3C2410:
736 chip->ecc.hwctl = s3c2410_nand_enable_hwecc;
737 chip->ecc.calculate = s3c2410_nand_calculate_ecc;
738 break;
739
740 case TYPE_S3C2412:
Matthieu CASTET4f659922007-02-13 12:30:38 +0100741 chip->ecc.hwctl = s3c2412_nand_enable_hwecc;
742 chip->ecc.calculate = s3c2412_nand_calculate_ecc;
743 break;
744
Ben Dooks2c06a082006-06-27 14:35:46 +0100745 case TYPE_S3C2440:
746 chip->ecc.hwctl = s3c2440_nand_enable_hwecc;
747 chip->ecc.calculate = s3c2440_nand_calculate_ecc;
748 break;
749
Ben Dooksa4f957f2005-06-20 12:48:25 +0100750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 } else {
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +0200752 chip->ecc.mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
Ben Dooks1c21ab62008-04-15 11:36:21 +0100754
755 if (set->ecc_layout != NULL)
756 chip->ecc.layout = set->ecc_layout;
Ben Dooks37e5ffa2008-04-15 11:36:22 +0100757
758 if (set->disable_ecc)
759 chip->ecc.mode = NAND_ECC_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
Ben Dooks71d54f32008-04-15 11:36:19 +0100762/* s3c2410_nand_update_chip
763 *
764 * post-probe chip update, to change any items, such as the
765 * layout for large page nand
766 */
767
768static void s3c2410_nand_update_chip(struct s3c2410_nand_info *info,
769 struct s3c2410_nand_mtd *nmtd)
770{
771 struct nand_chip *chip = &nmtd->chip;
772
Ben Dooks451d3392008-05-20 17:32:14 +0100773 dev_dbg(info->device, "chip %p => page shift %d\n",
774 chip, chip->page_shift);
Ben Dooks71d54f32008-04-15 11:36:19 +0100775
776 if (hardware_ecc) {
777 /* change the behaviour depending on wether we are using
778 * the large or small page nand device */
779
780 if (chip->page_shift > 10) {
781 chip->ecc.size = 256;
782 chip->ecc.bytes = 3;
783 } else {
784 chip->ecc.size = 512;
785 chip->ecc.bytes = 3;
786 chip->ecc.layout = &nand_hw_eccoob;
787 }
788 }
789}
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791/* s3c2410_nand_probe
792 *
793 * called by device layer when it finds a device matching
794 * one our driver can handled. This code checks to see if
795 * it can allocate all necessary resources then calls the
796 * nand layer to look for devices
797*/
798
Ben Dooks2c06a082006-06-27 14:35:46 +0100799static int s3c24xx_nand_probe(struct platform_device *pdev,
800 enum s3c_cpu_type cpu_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Russell King3ae5eae2005-11-09 22:32:44 +0000802 struct s3c2410_platform_nand *plat = to_nand_plat(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 struct s3c2410_nand_info *info;
804 struct s3c2410_nand_mtd *nmtd;
805 struct s3c2410_nand_set *sets;
806 struct resource *res;
807 int err = 0;
808 int size;
809 int nr_sets;
810 int setno;
811
Russell King3ae5eae2005-11-09 22:32:44 +0000812 pr_debug("s3c2410_nand_probe(%p)\n", pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 info = kmalloc(sizeof(*info), GFP_KERNEL);
815 if (info == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000816 dev_err(&pdev->dev, "no memory for flash info\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 err = -ENOMEM;
818 goto exit_error;
819 }
820
821 memzero(info, sizeof(*info));
Russell King3ae5eae2005-11-09 22:32:44 +0000822 platform_set_drvdata(pdev, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
824 spin_lock_init(&info->controller.lock);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100825 init_waitqueue_head(&info->controller.wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 /* get the clock source and enable it */
828
Russell King3ae5eae2005-11-09 22:32:44 +0000829 info->clk = clk_get(&pdev->dev, "nand");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (IS_ERR(info->clk)) {
Joe Perches898eb712007-10-18 03:06:30 -0700831 dev_err(&pdev->dev, "failed to get clock\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 err = -ENOENT;
833 goto exit_error;
834 }
835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 clk_enable(info->clk);
837
838 /* allocate and map the resource */
839
Ben Dooksa4f957f2005-06-20 12:48:25 +0100840 /* currently we assume we have the one resource */
841 res = pdev->resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 size = res->end - res->start + 1;
843
844 info->area = request_mem_region(res->start, size, pdev->name);
845
846 if (info->area == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000847 dev_err(&pdev->dev, "cannot reserve register region\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 err = -ENOENT;
849 goto exit_error;
850 }
851
Russell King3ae5eae2005-11-09 22:32:44 +0000852 info->device = &pdev->dev;
Ben Dooksa4f957f2005-06-20 12:48:25 +0100853 info->platform = plat;
854 info->regs = ioremap(res->start, size);
Ben Dooks2c06a082006-06-27 14:35:46 +0100855 info->cpu_type = cpu_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857 if (info->regs == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000858 dev_err(&pdev->dev, "cannot reserve register region\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 err = -EIO;
860 goto exit_error;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Russell King3ae5eae2005-11-09 22:32:44 +0000863 dev_dbg(&pdev->dev, "mapped registers at %p\n", info->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865 /* initialise the hardware */
866
Ben Dooks30821fe2008-07-15 11:58:31 +0100867 err = s3c2410_nand_inithw(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 if (err != 0)
869 goto exit_error;
870
871 sets = (plat != NULL) ? plat->sets : NULL;
872 nr_sets = (plat != NULL) ? plat->nr_sets : 1;
873
874 info->mtd_count = nr_sets;
875
876 /* allocate our information */
877
878 size = nr_sets * sizeof(*info->mtds);
879 info->mtds = kmalloc(size, GFP_KERNEL);
880 if (info->mtds == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000881 dev_err(&pdev->dev, "failed to allocate mtd storage\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 err = -ENOMEM;
883 goto exit_error;
884 }
885
886 memzero(info->mtds, size);
887
888 /* initialise all possible chips */
889
890 nmtd = info->mtds;
891
892 for (setno = 0; setno < nr_sets; setno++, nmtd++) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100893 pr_debug("initialising set %d (%p, info %p)\n", setno, nmtd, info);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 s3c2410_nand_init_chip(info, nmtd, sets);
896
Ben Dooks71d54f32008-04-15 11:36:19 +0100897 nmtd->scan_res = nand_scan_ident(&nmtd->mtd,
898 (sets) ? sets->nr_chips : 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 if (nmtd->scan_res == 0) {
Ben Dooks71d54f32008-04-15 11:36:19 +0100901 s3c2410_nand_update_chip(info, nmtd);
902 nand_scan_tail(&nmtd->mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 s3c2410_nand_add_partition(info, nmtd, sets);
904 }
905
906 if (sets != NULL)
907 sets++;
908 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000909
Ben Dooks30821fe2008-07-15 11:58:31 +0100910 err = s3c2410_nand_cpufreq_register(info);
911 if (err < 0) {
912 dev_err(&pdev->dev, "failed to init cpufreq support\n");
913 goto exit_error;
914 }
915
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100916 if (allow_clk_stop(info)) {
917 dev_info(&pdev->dev, "clock idle support enabled\n");
918 clk_disable(info->clk);
919 }
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 pr_debug("initialised ok\n");
922 return 0;
923
924 exit_error:
Russell King3ae5eae2005-11-09 22:32:44 +0000925 s3c2410_nand_remove(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927 if (err == 0)
928 err = -EINVAL;
929 return err;
930}
931
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100932/* PM Support */
933#ifdef CONFIG_PM
934
935static int s3c24xx_nand_suspend(struct platform_device *dev, pm_message_t pm)
936{
937 struct s3c2410_nand_info *info = platform_get_drvdata(dev);
938
939 if (info) {
Ben Dooks09160832008-04-15 11:36:18 +0100940 info->save_sel = readl(info->sel_reg);
Ben Dooks03680b12007-11-19 23:28:07 +0000941
942 /* For the moment, we must ensure nFCE is high during
943 * the time we are suspended. This really should be
944 * handled by suspending the MTDs we are using, but
945 * that is currently not the case. */
946
Ben Dooks09160832008-04-15 11:36:18 +0100947 writel(info->save_sel | info->sel_bit, info->sel_reg);
Ben Dooks03680b12007-11-19 23:28:07 +0000948
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100949 if (!allow_clk_stop(info))
950 clk_disable(info->clk);
951 }
952
953 return 0;
954}
955
956static int s3c24xx_nand_resume(struct platform_device *dev)
957{
958 struct s3c2410_nand_info *info = platform_get_drvdata(dev);
Ben Dooks09160832008-04-15 11:36:18 +0100959 unsigned long sel;
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100960
961 if (info) {
962 clk_enable(info->clk);
Ben Dooks30821fe2008-07-15 11:58:31 +0100963 s3c2410_nand_inithw(info);
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100964
Ben Dooks03680b12007-11-19 23:28:07 +0000965 /* Restore the state of the nFCE line. */
966
Ben Dooks09160832008-04-15 11:36:18 +0100967 sel = readl(info->sel_reg);
968 sel &= ~info->sel_bit;
969 sel |= info->save_sel & info->sel_bit;
970 writel(sel, info->sel_reg);
Ben Dooks03680b12007-11-19 23:28:07 +0000971
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100972 if (allow_clk_stop(info))
973 clk_disable(info->clk);
974 }
975
976 return 0;
977}
978
979#else
980#define s3c24xx_nand_suspend NULL
981#define s3c24xx_nand_resume NULL
982#endif
983
Ben Dooksa4f957f2005-06-20 12:48:25 +0100984/* driver device registration */
985
Russell King3ae5eae2005-11-09 22:32:44 +0000986static int s3c2410_nand_probe(struct platform_device *dev)
Ben Dooksa4f957f2005-06-20 12:48:25 +0100987{
Ben Dooks2c06a082006-06-27 14:35:46 +0100988 return s3c24xx_nand_probe(dev, TYPE_S3C2410);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100989}
990
Russell King3ae5eae2005-11-09 22:32:44 +0000991static int s3c2440_nand_probe(struct platform_device *dev)
Ben Dooksa4f957f2005-06-20 12:48:25 +0100992{
Ben Dooks2c06a082006-06-27 14:35:46 +0100993 return s3c24xx_nand_probe(dev, TYPE_S3C2440);
994}
995
996static int s3c2412_nand_probe(struct platform_device *dev)
997{
998 return s3c24xx_nand_probe(dev, TYPE_S3C2412);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100999}
1000
Russell King3ae5eae2005-11-09 22:32:44 +00001001static struct platform_driver s3c2410_nand_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 .probe = s3c2410_nand_probe,
1003 .remove = s3c2410_nand_remove,
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001004 .suspend = s3c24xx_nand_suspend,
1005 .resume = s3c24xx_nand_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00001006 .driver = {
1007 .name = "s3c2410-nand",
1008 .owner = THIS_MODULE,
1009 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010};
1011
Russell King3ae5eae2005-11-09 22:32:44 +00001012static struct platform_driver s3c2440_nand_driver = {
Ben Dooksa4f957f2005-06-20 12:48:25 +01001013 .probe = s3c2440_nand_probe,
1014 .remove = s3c2410_nand_remove,
Ben Dooksd1fef3c2006-06-19 09:29:38 +01001015 .suspend = s3c24xx_nand_suspend,
1016 .resume = s3c24xx_nand_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00001017 .driver = {
1018 .name = "s3c2440-nand",
1019 .owner = THIS_MODULE,
1020 },
Ben Dooksa4f957f2005-06-20 12:48:25 +01001021};
1022
Ben Dooks2c06a082006-06-27 14:35:46 +01001023static struct platform_driver s3c2412_nand_driver = {
1024 .probe = s3c2412_nand_probe,
1025 .remove = s3c2410_nand_remove,
1026 .suspend = s3c24xx_nand_suspend,
1027 .resume = s3c24xx_nand_resume,
1028 .driver = {
1029 .name = "s3c2412-nand",
1030 .owner = THIS_MODULE,
1031 },
1032};
1033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034static int __init s3c2410_nand_init(void)
1035{
Ben Dooksa4f957f2005-06-20 12:48:25 +01001036 printk("S3C24XX NAND Driver, (c) 2004 Simtec Electronics\n");
1037
Ben Dooks2c06a082006-06-27 14:35:46 +01001038 platform_driver_register(&s3c2412_nand_driver);
Russell King3ae5eae2005-11-09 22:32:44 +00001039 platform_driver_register(&s3c2440_nand_driver);
1040 return platform_driver_register(&s3c2410_nand_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041}
1042
1043static void __exit s3c2410_nand_exit(void)
1044{
Ben Dooks2c06a082006-06-27 14:35:46 +01001045 platform_driver_unregister(&s3c2412_nand_driver);
Russell King3ae5eae2005-11-09 22:32:44 +00001046 platform_driver_unregister(&s3c2440_nand_driver);
1047 platform_driver_unregister(&s3c2410_nand_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048}
1049
1050module_init(s3c2410_nand_init);
1051module_exit(s3c2410_nand_exit);
1052
1053MODULE_LICENSE("GPL");
1054MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
Ben Dooksa4f957f2005-06-20 12:48:25 +01001055MODULE_DESCRIPTION("S3C24XX MTD NAND driver");
Kay Sievers1ff18422008-04-18 13:44:27 -07001056MODULE_ALIAS("platform:s3c2410-nand");
1057MODULE_ALIAS("platform:s3c2412-nand");
1058MODULE_ALIAS("platform:s3c2440-nand");