blob: 0ddfd6de75c52e75864c6cb6ab7d7550e45089dd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* linux/drivers/mtd/nand/s3c2410.c
2 *
Ben Dooksa4f957f2005-06-20 12:48:25 +01003 * Copyright (c) 2004,2005 Simtec Electronics
Ben Dooksfdf2fd52005-02-18 14:46:15 +00004 * http://www.simtec.co.uk/products/SWLINUX/
5 * Ben Dooks <ben@simtec.co.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
Ben Dooksa4f957f2005-06-20 12:48:25 +01007 * Samsung S3C2410/S3C240 NAND driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * Changelog:
10 * 21-Sep-2004 BJD Initial version
11 * 23-Sep-2004 BJD Mulitple device support
12 * 28-Sep-2004 BJD Fixed ECC placement for Hardware mode
13 * 12-Oct-2004 BJD Fixed errors in use of platform data
Ben Dooks3e4ef3b2005-03-17 11:31:30 +000014 * 18-Feb-2005 BJD Fix sparse errors
15 * 14-Mar-2005 BJD Applied tglx's code reduction patch
Ben Dooksa4f957f2005-06-20 12:48:25 +010016 * 02-May-2005 BJD Fixed s3c2440 support
17 * 02-May-2005 BJD Reduced hwcontrol decode
18 * 20-Jun-2005 BJD Updated s3c2440 support, fixed timing bug
Ben Dooksfb8d82a2005-07-06 21:05:10 +010019 * 08-Jul-2005 BJD Fix OOPS when no platform data supplied
Ben Dookscfd320f2005-10-20 22:22:58 +010020 * 20-Oct-2005 BJD Fix timing calculation bug
Ben Dooksd1fef3c2006-06-19 09:29:38 +010021 * 14-Jan-2006 BJD Allow clock to be stopped when idle
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 *
Ben Dooksd1fef3c2006-06-19 09:29:38 +010023 * $Id: s3c2410.c,v 1.23 2006/04/01 18:06:29 bjd Exp $
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 *
25 * This program is free software; you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation; either version 2 of the License, or
28 * (at your option) any later version.
29 *
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
34 *
35 * You should have received a copy of the GNU General Public License
36 * along with this program; if not, write to the Free Software
37 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
38*/
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#ifdef CONFIG_MTD_NAND_S3C2410_DEBUG
41#define DEBUG
42#endif
43
44#include <linux/module.h>
45#include <linux/types.h>
46#include <linux/init.h>
47#include <linux/kernel.h>
48#include <linux/string.h>
49#include <linux/ioport.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010050#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/delay.h>
52#include <linux/err.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080053#include <linux/slab.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000054#include <linux/clk.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56#include <linux/mtd/mtd.h>
57#include <linux/mtd/nand.h>
58#include <linux/mtd/nand_ecc.h>
59#include <linux/mtd/partitions.h>
60
61#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63#include <asm/arch/regs-nand.h>
64#include <asm/arch/nand.h>
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#ifdef CONFIG_MTD_NAND_S3C2410_HWECC
67static int hardware_ecc = 1;
68#else
69static int hardware_ecc = 0;
70#endif
71
Ben Dooksd1fef3c2006-06-19 09:29:38 +010072#ifdef CONFIG_MTD_NAND_S3C2410_CLKSTOP
73static int clock_stop = 1;
74#else
75static const int clock_stop = 0;
76#endif
77
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/* new oob placement block for use with hardware ecc generation
80 */
81
Thomas Gleixner5bd34c02006-05-27 22:16:10 +020082static struct nand_ecclayout nand_hw_eccoob = {
David Woodhousee0c7d762006-05-13 18:07:53 +010083 .eccbytes = 3,
84 .eccpos = {0, 1, 2},
85 .oobfree = {{8, 8}}
Linus Torvalds1da177e2005-04-16 15:20:36 -070086};
87
88/* controller and mtd information */
89
90struct s3c2410_nand_info;
91
92struct s3c2410_nand_mtd {
93 struct mtd_info mtd;
94 struct nand_chip chip;
95 struct s3c2410_nand_set *set;
96 struct s3c2410_nand_info *info;
97 int scan_res;
98};
99
Ben Dooks2c06a082006-06-27 14:35:46 +0100100enum s3c_cpu_type {
101 TYPE_S3C2410,
102 TYPE_S3C2412,
103 TYPE_S3C2440,
104};
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106/* overview of the s3c2410 nand state */
107
108struct s3c2410_nand_info {
109 /* mtd info */
110 struct nand_hw_control controller;
111 struct s3c2410_nand_mtd *mtds;
112 struct s3c2410_platform_nand *platform;
113
114 /* device info */
115 struct device *device;
116 struct resource *area;
117 struct clk *clk;
Ben Dooksfdf2fd52005-02-18 14:46:15 +0000118 void __iomem *regs;
Ben Dooks2c06a082006-06-27 14:35:46 +0100119 void __iomem *sel_reg;
120 int sel_bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 int mtd_count;
Ben Dooksa4f957f2005-06-20 12:48:25 +0100122
Ben Dooks2c06a082006-06-27 14:35:46 +0100123 enum s3c_cpu_type cpu_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124};
125
126/* conversion functions */
127
128static struct s3c2410_nand_mtd *s3c2410_nand_mtd_toours(struct mtd_info *mtd)
129{
130 return container_of(mtd, struct s3c2410_nand_mtd, mtd);
131}
132
133static struct s3c2410_nand_info *s3c2410_nand_mtd_toinfo(struct mtd_info *mtd)
134{
135 return s3c2410_nand_mtd_toours(mtd)->info;
136}
137
Russell King3ae5eae2005-11-09 22:32:44 +0000138static struct s3c2410_nand_info *to_nand_info(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Russell King3ae5eae2005-11-09 22:32:44 +0000140 return platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
Russell King3ae5eae2005-11-09 22:32:44 +0000143static struct s3c2410_platform_nand *to_nand_plat(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Russell King3ae5eae2005-11-09 22:32:44 +0000145 return dev->dev.platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100148static inline int allow_clk_stop(struct s3c2410_nand_info *info)
149{
150 return clock_stop;
151}
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153/* timing calculations */
154
Ben Dookscfd320f2005-10-20 22:22:58 +0100155#define NS_IN_KHZ 1000000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Ben Dooks2c06a082006-06-27 14:35:46 +0100157static int s3c_nand_calc_rate(int wanted, unsigned long clk, int max)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 int result;
160
Ben Dookscfd320f2005-10-20 22:22:58 +0100161 result = (wanted * clk) / NS_IN_KHZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 result++;
163
164 pr_debug("result %d from %ld, %d\n", result, clk, wanted);
165
166 if (result > max) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100167 printk("%d ns is too big for current clock rate %ld\n", wanted, clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return -1;
169 }
170
171 if (result < 1)
172 result = 1;
173
174 return result;
175}
176
Ben Dookscfd320f2005-10-20 22:22:58 +0100177#define to_ns(ticks,clk) (((ticks) * NS_IN_KHZ) / (unsigned int)(clk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179/* controller setup */
180
Ben Dooks2c06a082006-06-27 14:35:46 +0100181static int s3c2410_nand_inithw(struct s3c2410_nand_info *info,
182 struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Russell King3ae5eae2005-11-09 22:32:44 +0000184 struct s3c2410_platform_nand *plat = to_nand_plat(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 unsigned long clkrate = clk_get_rate(info->clk);
Ben Dooks2c06a082006-06-27 14:35:46 +0100186 int tacls_max = (info->cpu_type == TYPE_S3C2412) ? 8 : 4;
Ben Dookscfd320f2005-10-20 22:22:58 +0100187 int tacls, twrph0, twrph1;
Ben Dooks2c06a082006-06-27 14:35:46 +0100188 unsigned long cfg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 /* calculate the timing information for the controller */
191
Ben Dookscfd320f2005-10-20 22:22:58 +0100192 clkrate /= 1000; /* turn clock into kHz for ease of use */
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 if (plat != NULL) {
Ben Dooks2c06a082006-06-27 14:35:46 +0100195 tacls = s3c_nand_calc_rate(plat->tacls, clkrate, tacls_max);
196 twrph0 = s3c_nand_calc_rate(plat->twrph0, clkrate, 8);
197 twrph1 = s3c_nand_calc_rate(plat->twrph1, clkrate, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 } else {
199 /* default timings */
Ben Dooks2c06a082006-06-27 14:35:46 +0100200 tacls = tacls_max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 twrph0 = 8;
202 twrph1 = 8;
203 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (tacls < 0 || twrph0 < 0 || twrph1 < 0) {
Ben Dooks99974c62006-06-21 15:43:05 +0100206 dev_err(info->device, "cannot get suitable timings\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return -EINVAL;
208 }
209
Ben Dooks99974c62006-06-21 15:43:05 +0100210 dev_info(info->device, "Tacls=%d, %dns Twrph0=%d %dns, Twrph1=%d %dns\n",
David Woodhousee0c7d762006-05-13 18:07:53 +0100211 tacls, to_ns(tacls, clkrate), twrph0, to_ns(twrph0, clkrate), twrph1, to_ns(twrph1, clkrate));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Ben Dooks2c06a082006-06-27 14:35:46 +0100213 switch (info->cpu_type) {
214 case TYPE_S3C2410:
David Woodhousee0c7d762006-05-13 18:07:53 +0100215 cfg = S3C2410_NFCONF_EN;
216 cfg |= S3C2410_NFCONF_TACLS(tacls - 1);
217 cfg |= S3C2410_NFCONF_TWRPH0(twrph0 - 1);
218 cfg |= S3C2410_NFCONF_TWRPH1(twrph1 - 1);
Ben Dooks2c06a082006-06-27 14:35:46 +0100219 break;
220
221 case TYPE_S3C2440:
222 case TYPE_S3C2412:
David Woodhousee0c7d762006-05-13 18:07:53 +0100223 cfg = S3C2440_NFCONF_TACLS(tacls - 1);
224 cfg |= S3C2440_NFCONF_TWRPH0(twrph0 - 1);
225 cfg |= S3C2440_NFCONF_TWRPH1(twrph1 - 1);
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100226
227 /* enable the controller and de-assert nFCE */
228
Ben Dooks2c06a082006-06-27 14:35:46 +0100229 writel(S3C2440_NFCONT_ENABLE, info->regs + S3C2440_NFCONT);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Ben Dooks99974c62006-06-21 15:43:05 +0100232 dev_dbg(info->device, "NF_CONF is 0x%lx\n", cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 writel(cfg, info->regs + S3C2410_NFCONF);
235 return 0;
236}
237
238/* select chip */
239
240static void s3c2410_nand_select_chip(struct mtd_info *mtd, int chip)
241{
242 struct s3c2410_nand_info *info;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000243 struct s3c2410_nand_mtd *nmtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 struct nand_chip *this = mtd->priv;
245 unsigned long cur;
246
247 nmtd = this->priv;
248 info = nmtd->info;
249
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100250 if (chip != -1 && allow_clk_stop(info))
251 clk_enable(info->clk);
252
Ben Dooks2c06a082006-06-27 14:35:46 +0100253 cur = readl(info->sel_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 if (chip == -1) {
Ben Dooks2c06a082006-06-27 14:35:46 +0100256 cur |= info->sel_bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 } else {
Ben Dooksfb8d82a2005-07-06 21:05:10 +0100258 if (nmtd->set != NULL && chip > nmtd->set->nr_chips) {
Ben Dooks99974c62006-06-21 15:43:05 +0100259 dev_err(info->device, "invalid chip %d\n", chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return;
261 }
262
263 if (info->platform != NULL) {
264 if (info->platform->select_chip != NULL)
David Woodhousee0c7d762006-05-13 18:07:53 +0100265 (info->platform->select_chip) (nmtd->set, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
267
Ben Dooks2c06a082006-06-27 14:35:46 +0100268 cur &= ~info->sel_bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
270
Ben Dooks2c06a082006-06-27 14:35:46 +0100271 writel(cur, info->sel_reg);
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100272
273 if (chip == -1 && allow_clk_stop(info))
274 clk_disable(info->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
Ben Dooksad3b5fb2006-06-19 09:43:23 +0100277/* s3c2410_nand_hwcontrol
Ben Dooksa4f957f2005-06-20 12:48:25 +0100278 *
Ben Dooksad3b5fb2006-06-19 09:43:23 +0100279 * Issue command and address cycles to the chip
Ben Dooksa4f957f2005-06-20 12:48:25 +0100280*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200282static void s3c2410_nand_hwcontrol(struct mtd_info *mtd, int cmd,
David Woodhousef9068872006-06-10 00:53:16 +0100283 unsigned int ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
285 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
David Woodhousec9ac5972006-11-30 08:17:38 +0000286
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200287 if (cmd == NAND_CMD_NONE)
288 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
David Woodhousef9068872006-06-10 00:53:16 +0100290 if (ctrl & NAND_CLE)
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200291 writeb(cmd, info->regs + S3C2410_NFCMD);
292 else
293 writeb(cmd, info->regs + S3C2410_NFADDR);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100294}
295
296/* command and control functions */
297
David Woodhousef9068872006-06-10 00:53:16 +0100298static void s3c2440_nand_hwcontrol(struct mtd_info *mtd, int cmd,
299 unsigned int ctrl)
Ben Dooksa4f957f2005-06-20 12:48:25 +0100300{
301 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100302
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200303 if (cmd == NAND_CMD_NONE)
304 return;
Ben Dooksa4f957f2005-06-20 12:48:25 +0100305
David Woodhousef9068872006-06-10 00:53:16 +0100306 if (ctrl & NAND_CLE)
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200307 writeb(cmd, info->regs + S3C2440_NFCMD);
308 else
309 writeb(cmd, info->regs + S3C2440_NFADDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312/* s3c2410_nand_devready()
313 *
314 * returns 0 if the nand is busy, 1 if it is ready
315*/
316
317static int s3c2410_nand_devready(struct mtd_info *mtd)
318{
319 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return readb(info->regs + S3C2410_NFSTAT) & S3C2410_NFSTAT_BUSY;
321}
322
Ben Dooks2c06a082006-06-27 14:35:46 +0100323static int s3c2440_nand_devready(struct mtd_info *mtd)
324{
325 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
326 return readb(info->regs + S3C2440_NFSTAT) & S3C2440_NFSTAT_READY;
327}
328
329static int s3c2412_nand_devready(struct mtd_info *mtd)
330{
331 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
332 return readb(info->regs + S3C2412_NFSTAT) & S3C2412_NFSTAT_READY;
333}
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335/* ECC handling functions */
336
Ben Dooks2c06a082006-06-27 14:35:46 +0100337static int s3c2410_nand_correct_data(struct mtd_info *mtd, u_char *dat,
338 u_char *read_ecc, u_char *calc_ecc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Ben Dooksa2593242007-02-02 16:59:33 +0000340 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
341 unsigned int diff0, diff1, diff2;
342 unsigned int bit, byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Ben Dooksa2593242007-02-02 16:59:33 +0000344 pr_debug("%s(%p,%p,%p,%p)\n", __func__, mtd, dat, read_ecc, calc_ecc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Ben Dooksa2593242007-02-02 16:59:33 +0000346 diff0 = read_ecc[0] ^ calc_ecc[0];
347 diff1 = read_ecc[1] ^ calc_ecc[1];
348 diff2 = read_ecc[2] ^ calc_ecc[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Ben Dooksa2593242007-02-02 16:59:33 +0000350 pr_debug("%s: rd %02x%02x%02x calc %02x%02x%02x diff %02x%02x%02x\n",
351 __func__,
352 read_ecc[0], read_ecc[1], read_ecc[2],
353 calc_ecc[0], calc_ecc[1], calc_ecc[2],
354 diff0, diff1, diff2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Ben Dooksa2593242007-02-02 16:59:33 +0000356 if (diff0 == 0 && diff1 == 0 && diff2 == 0)
357 return 0; /* ECC is ok */
358
359 /* Can we correct this ECC (ie, one row and column change).
360 * Note, this is similar to the 256 error code on smartmedia */
361
362 if (((diff0 ^ (diff0 >> 1)) & 0x55) == 0x55 &&
363 ((diff1 ^ (diff1 >> 1)) & 0x55) == 0x55 &&
364 ((diff2 ^ (diff2 >> 1)) & 0x55) == 0x55) {
365 /* calculate the bit position of the error */
366
367 bit = (diff2 >> 2) & 1;
368 bit |= (diff2 >> 3) & 2;
369 bit |= (diff2 >> 4) & 4;
370
371 /* calculate the byte position of the error */
372
373 byte = (diff1 << 1) & 0x80;
374 byte |= (diff1 << 2) & 0x40;
375 byte |= (diff1 << 3) & 0x20;
376 byte |= (diff1 << 4) & 0x10;
377
378 byte |= (diff0 >> 3) & 0x08;
379 byte |= (diff0 >> 2) & 0x04;
380 byte |= (diff0 >> 1) & 0x02;
381 byte |= (diff0 >> 0) & 0x01;
382
383 byte |= (diff2 << 8) & 0x100;
384
385 dev_dbg(info->device, "correcting error bit %d, byte %d\n",
386 bit, byte);
387
388 dat[byte] ^= (1 << bit);
389 return 1;
390 }
391
392 /* if there is only one bit difference in the ECC, then
393 * one of only a row or column parity has changed, which
394 * means the error is most probably in the ECC itself */
395
396 diff0 |= (diff1 << 8);
397 diff0 |= (diff2 << 16);
398
399 if ((diff0 & ~(1<<fls(diff0))) == 0)
400 return 1;
401
402 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
Ben Dooksa4f957f2005-06-20 12:48:25 +0100405/* ECC functions
406 *
407 * These allow the s3c2410 and s3c2440 to use the controller's ECC
408 * generator block to ECC the data as it passes through]
409*/
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411static void s3c2410_nand_enable_hwecc(struct mtd_info *mtd, int mode)
412{
413 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
414 unsigned long ctrl;
415
416 ctrl = readl(info->regs + S3C2410_NFCONF);
417 ctrl |= S3C2410_NFCONF_INITECC;
418 writel(ctrl, info->regs + S3C2410_NFCONF);
419}
420
Matthieu CASTET4f659922007-02-13 12:30:38 +0100421static void s3c2412_nand_enable_hwecc(struct mtd_info *mtd, int mode)
422{
423 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
424 unsigned long ctrl;
425
426 ctrl = readl(info->regs + S3C2440_NFCONT);
427 writel(ctrl | S3C2412_NFCONT_INIT_MAIN_ECC, info->regs + S3C2440_NFCONT);
428}
429
Ben Dooksa4f957f2005-06-20 12:48:25 +0100430static void s3c2440_nand_enable_hwecc(struct mtd_info *mtd, int mode)
431{
432 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
433 unsigned long ctrl;
434
435 ctrl = readl(info->regs + S3C2440_NFCONT);
436 writel(ctrl | S3C2440_NFCONT_INITECC, info->regs + S3C2440_NFCONT);
437}
438
David Woodhousee0c7d762006-05-13 18:07:53 +0100439static int s3c2410_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
441 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
442
443 ecc_code[0] = readb(info->regs + S3C2410_NFECC + 0);
444 ecc_code[1] = readb(info->regs + S3C2410_NFECC + 1);
445 ecc_code[2] = readb(info->regs + S3C2410_NFECC + 2);
446
Ben Dooksa2593242007-02-02 16:59:33 +0000447 pr_debug("%s: returning ecc %02x%02x%02x\n", __func__,
448 ecc_code[0], ecc_code[1], ecc_code[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 return 0;
451}
452
Matthieu CASTET4f659922007-02-13 12:30:38 +0100453static int s3c2412_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
454{
455 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
456 unsigned long ecc = readl(info->regs + S3C2412_NFMECC0);
457
458 ecc_code[0] = ecc;
459 ecc_code[1] = ecc >> 8;
460 ecc_code[2] = ecc >> 16;
461
462 pr_debug("calculate_ecc: returning ecc %02x,%02x,%02x\n", ecc_code[0], ecc_code[1], ecc_code[2]);
463
464 return 0;
465}
466
David Woodhousee0c7d762006-05-13 18:07:53 +0100467static int s3c2440_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
Ben Dooksa4f957f2005-06-20 12:48:25 +0100468{
469 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
470 unsigned long ecc = readl(info->regs + S3C2440_NFMECC0);
471
472 ecc_code[0] = ecc;
473 ecc_code[1] = ecc >> 8;
474 ecc_code[2] = ecc >> 16;
475
Ben Dooksa2593242007-02-02 16:59:33 +0000476 pr_debug("%s: returning ecc %06x\n", __func__, ecc);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100477
478 return 0;
479}
480
Ben Dooksa4f957f2005-06-20 12:48:25 +0100481/* over-ride the standard functions for a little more speed. We can
482 * use read/write block to move the data buffers to/from the controller
483*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485static void s3c2410_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
486{
487 struct nand_chip *this = mtd->priv;
488 readsb(this->IO_ADDR_R, buf, len);
489}
490
David Woodhousee0c7d762006-05-13 18:07:53 +0100491static void s3c2410_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
493 struct nand_chip *this = mtd->priv;
494 writesb(this->IO_ADDR_W, buf, len);
495}
496
497/* device management functions */
498
Russell King3ae5eae2005-11-09 22:32:44 +0000499static int s3c2410_nand_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Russell King3ae5eae2005-11-09 22:32:44 +0000501 struct s3c2410_nand_info *info = to_nand_info(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Russell King3ae5eae2005-11-09 22:32:44 +0000503 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000505 if (info == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 return 0;
507
508 /* first thing we need to do is release all our mtds
509 * and their partitions, then go through freeing the
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000510 * resources used
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (info->mtds != NULL) {
514 struct s3c2410_nand_mtd *ptr = info->mtds;
515 int mtdno;
516
517 for (mtdno = 0; mtdno < info->mtd_count; mtdno++, ptr++) {
518 pr_debug("releasing mtd %d (%p)\n", mtdno, ptr);
519 nand_release(&ptr->mtd);
520 }
521
522 kfree(info->mtds);
523 }
524
525 /* free the common resources */
526
527 if (info->clk != NULL && !IS_ERR(info->clk)) {
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100528 if (!allow_clk_stop(info))
529 clk_disable(info->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 clk_put(info->clk);
531 }
532
533 if (info->regs != NULL) {
534 iounmap(info->regs);
535 info->regs = NULL;
536 }
537
538 if (info->area != NULL) {
539 release_resource(info->area);
540 kfree(info->area);
541 info->area = NULL;
542 }
543
544 kfree(info);
545
546 return 0;
547}
548
549#ifdef CONFIG_MTD_PARTITIONS
550static int s3c2410_nand_add_partition(struct s3c2410_nand_info *info,
551 struct s3c2410_nand_mtd *mtd,
552 struct s3c2410_nand_set *set)
553{
554 if (set == NULL)
555 return add_mtd_device(&mtd->mtd);
556
557 if (set->nr_partitions > 0 && set->partitions != NULL) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100558 return add_mtd_partitions(&mtd->mtd, set->partitions, set->nr_partitions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
560
561 return add_mtd_device(&mtd->mtd);
562}
563#else
564static int s3c2410_nand_add_partition(struct s3c2410_nand_info *info,
565 struct s3c2410_nand_mtd *mtd,
566 struct s3c2410_nand_set *set)
567{
568 return add_mtd_device(&mtd->mtd);
569}
570#endif
571
572/* s3c2410_nand_init_chip
573 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000574 * init a single instance of an chip
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575*/
576
577static void s3c2410_nand_init_chip(struct s3c2410_nand_info *info,
578 struct s3c2410_nand_mtd *nmtd,
579 struct s3c2410_nand_set *set)
580{
581 struct nand_chip *chip = &nmtd->chip;
Ben Dooks2c06a082006-06-27 14:35:46 +0100582 void __iomem *regs = info->regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 chip->write_buf = s3c2410_nand_write_buf;
585 chip->read_buf = s3c2410_nand_read_buf;
586 chip->select_chip = s3c2410_nand_select_chip;
587 chip->chip_delay = 50;
588 chip->priv = nmtd;
589 chip->options = 0;
590 chip->controller = &info->controller;
591
Ben Dooks2c06a082006-06-27 14:35:46 +0100592 switch (info->cpu_type) {
593 case TYPE_S3C2410:
594 chip->IO_ADDR_W = regs + S3C2410_NFDATA;
595 info->sel_reg = regs + S3C2410_NFCONF;
596 info->sel_bit = S3C2410_NFCONF_nFCE;
597 chip->cmd_ctrl = s3c2410_nand_hwcontrol;
598 chip->dev_ready = s3c2410_nand_devready;
599 break;
600
601 case TYPE_S3C2440:
602 chip->IO_ADDR_W = regs + S3C2440_NFDATA;
603 info->sel_reg = regs + S3C2440_NFCONT;
604 info->sel_bit = S3C2440_NFCONT_nFCE;
605 chip->cmd_ctrl = s3c2440_nand_hwcontrol;
606 chip->dev_ready = s3c2440_nand_devready;
607 break;
608
609 case TYPE_S3C2412:
610 chip->IO_ADDR_W = regs + S3C2440_NFDATA;
611 info->sel_reg = regs + S3C2440_NFCONT;
612 info->sel_bit = S3C2412_NFCONT_nFCE0;
613 chip->cmd_ctrl = s3c2440_nand_hwcontrol;
614 chip->dev_ready = s3c2412_nand_devready;
615
616 if (readl(regs + S3C2410_NFCONF) & S3C2412_NFCONF_NANDBOOT)
617 dev_info(info->device, "System booted from NAND\n");
618
619 break;
620 }
621
622 chip->IO_ADDR_R = chip->IO_ADDR_W;
Ben Dooksa4f957f2005-06-20 12:48:25 +0100623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 nmtd->info = info;
625 nmtd->mtd.priv = chip;
David Woodhouse552d9202006-05-14 01:20:46 +0100626 nmtd->mtd.owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 nmtd->set = set;
628
629 if (hardware_ecc) {
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +0200630 chip->ecc.calculate = s3c2410_nand_calculate_ecc;
Ben Dooks2c06a082006-06-27 14:35:46 +0100631 chip->ecc.correct = s3c2410_nand_correct_data;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +0200632 chip->ecc.mode = NAND_ECC_HW;
633 chip->ecc.size = 512;
634 chip->ecc.bytes = 3;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200635 chip->ecc.layout = &nand_hw_eccoob;
Ben Dooksa4f957f2005-06-20 12:48:25 +0100636
Ben Dooks2c06a082006-06-27 14:35:46 +0100637 switch (info->cpu_type) {
638 case TYPE_S3C2410:
639 chip->ecc.hwctl = s3c2410_nand_enable_hwecc;
640 chip->ecc.calculate = s3c2410_nand_calculate_ecc;
641 break;
642
643 case TYPE_S3C2412:
Matthieu CASTET4f659922007-02-13 12:30:38 +0100644 chip->ecc.hwctl = s3c2412_nand_enable_hwecc;
645 chip->ecc.calculate = s3c2412_nand_calculate_ecc;
646 break;
647
Ben Dooks2c06a082006-06-27 14:35:46 +0100648 case TYPE_S3C2440:
649 chip->ecc.hwctl = s3c2440_nand_enable_hwecc;
650 chip->ecc.calculate = s3c2440_nand_calculate_ecc;
651 break;
652
Ben Dooksa4f957f2005-06-20 12:48:25 +0100653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 } else {
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +0200655 chip->ecc.mode = NAND_ECC_SOFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
657}
658
659/* s3c2410_nand_probe
660 *
661 * called by device layer when it finds a device matching
662 * one our driver can handled. This code checks to see if
663 * it can allocate all necessary resources then calls the
664 * nand layer to look for devices
665*/
666
Ben Dooks2c06a082006-06-27 14:35:46 +0100667static int s3c24xx_nand_probe(struct platform_device *pdev,
668 enum s3c_cpu_type cpu_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
Russell King3ae5eae2005-11-09 22:32:44 +0000670 struct s3c2410_platform_nand *plat = to_nand_plat(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 struct s3c2410_nand_info *info;
672 struct s3c2410_nand_mtd *nmtd;
673 struct s3c2410_nand_set *sets;
674 struct resource *res;
675 int err = 0;
676 int size;
677 int nr_sets;
678 int setno;
679
Russell King3ae5eae2005-11-09 22:32:44 +0000680 pr_debug("s3c2410_nand_probe(%p)\n", pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 info = kmalloc(sizeof(*info), GFP_KERNEL);
683 if (info == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000684 dev_err(&pdev->dev, "no memory for flash info\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 err = -ENOMEM;
686 goto exit_error;
687 }
688
689 memzero(info, sizeof(*info));
Russell King3ae5eae2005-11-09 22:32:44 +0000690 platform_set_drvdata(pdev, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 spin_lock_init(&info->controller.lock);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100693 init_waitqueue_head(&info->controller.wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 /* get the clock source and enable it */
696
Russell King3ae5eae2005-11-09 22:32:44 +0000697 info->clk = clk_get(&pdev->dev, "nand");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (IS_ERR(info->clk)) {
Russell King3ae5eae2005-11-09 22:32:44 +0000699 dev_err(&pdev->dev, "failed to get clock");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 err = -ENOENT;
701 goto exit_error;
702 }
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 clk_enable(info->clk);
705
706 /* allocate and map the resource */
707
Ben Dooksa4f957f2005-06-20 12:48:25 +0100708 /* currently we assume we have the one resource */
709 res = pdev->resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 size = res->end - res->start + 1;
711
712 info->area = request_mem_region(res->start, size, pdev->name);
713
714 if (info->area == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000715 dev_err(&pdev->dev, "cannot reserve register region\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 err = -ENOENT;
717 goto exit_error;
718 }
719
Russell King3ae5eae2005-11-09 22:32:44 +0000720 info->device = &pdev->dev;
Ben Dooksa4f957f2005-06-20 12:48:25 +0100721 info->platform = plat;
722 info->regs = ioremap(res->start, size);
Ben Dooks2c06a082006-06-27 14:35:46 +0100723 info->cpu_type = cpu_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 if (info->regs == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000726 dev_err(&pdev->dev, "cannot reserve register region\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 err = -EIO;
728 goto exit_error;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Russell King3ae5eae2005-11-09 22:32:44 +0000731 dev_dbg(&pdev->dev, "mapped registers at %p\n", info->regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733 /* initialise the hardware */
734
Russell King3ae5eae2005-11-09 22:32:44 +0000735 err = s3c2410_nand_inithw(info, pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 if (err != 0)
737 goto exit_error;
738
739 sets = (plat != NULL) ? plat->sets : NULL;
740 nr_sets = (plat != NULL) ? plat->nr_sets : 1;
741
742 info->mtd_count = nr_sets;
743
744 /* allocate our information */
745
746 size = nr_sets * sizeof(*info->mtds);
747 info->mtds = kmalloc(size, GFP_KERNEL);
748 if (info->mtds == NULL) {
Russell King3ae5eae2005-11-09 22:32:44 +0000749 dev_err(&pdev->dev, "failed to allocate mtd storage\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 err = -ENOMEM;
751 goto exit_error;
752 }
753
754 memzero(info->mtds, size);
755
756 /* initialise all possible chips */
757
758 nmtd = info->mtds;
759
760 for (setno = 0; setno < nr_sets; setno++, nmtd++) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100761 pr_debug("initialising set %d (%p, info %p)\n", setno, nmtd, info);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 s3c2410_nand_init_chip(info, nmtd, sets);
764
David Woodhousee0c7d762006-05-13 18:07:53 +0100765 nmtd->scan_res = nand_scan(&nmtd->mtd, (sets) ? sets->nr_chips : 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767 if (nmtd->scan_res == 0) {
768 s3c2410_nand_add_partition(info, nmtd, sets);
769 }
770
771 if (sets != NULL)
772 sets++;
773 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000774
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100775 if (allow_clk_stop(info)) {
776 dev_info(&pdev->dev, "clock idle support enabled\n");
777 clk_disable(info->clk);
778 }
779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 pr_debug("initialised ok\n");
781 return 0;
782
783 exit_error:
Russell King3ae5eae2005-11-09 22:32:44 +0000784 s3c2410_nand_remove(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 if (err == 0)
787 err = -EINVAL;
788 return err;
789}
790
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100791/* PM Support */
792#ifdef CONFIG_PM
793
794static int s3c24xx_nand_suspend(struct platform_device *dev, pm_message_t pm)
795{
796 struct s3c2410_nand_info *info = platform_get_drvdata(dev);
797
798 if (info) {
799 if (!allow_clk_stop(info))
800 clk_disable(info->clk);
801 }
802
803 return 0;
804}
805
806static int s3c24xx_nand_resume(struct platform_device *dev)
807{
808 struct s3c2410_nand_info *info = platform_get_drvdata(dev);
809
810 if (info) {
811 clk_enable(info->clk);
812 s3c2410_nand_inithw(info, dev);
813
814 if (allow_clk_stop(info))
815 clk_disable(info->clk);
816 }
817
818 return 0;
819}
820
821#else
822#define s3c24xx_nand_suspend NULL
823#define s3c24xx_nand_resume NULL
824#endif
825
Ben Dooksa4f957f2005-06-20 12:48:25 +0100826/* driver device registration */
827
Russell King3ae5eae2005-11-09 22:32:44 +0000828static int s3c2410_nand_probe(struct platform_device *dev)
Ben Dooksa4f957f2005-06-20 12:48:25 +0100829{
Ben Dooks2c06a082006-06-27 14:35:46 +0100830 return s3c24xx_nand_probe(dev, TYPE_S3C2410);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100831}
832
Russell King3ae5eae2005-11-09 22:32:44 +0000833static int s3c2440_nand_probe(struct platform_device *dev)
Ben Dooksa4f957f2005-06-20 12:48:25 +0100834{
Ben Dooks2c06a082006-06-27 14:35:46 +0100835 return s3c24xx_nand_probe(dev, TYPE_S3C2440);
836}
837
838static int s3c2412_nand_probe(struct platform_device *dev)
839{
840 return s3c24xx_nand_probe(dev, TYPE_S3C2412);
Ben Dooksa4f957f2005-06-20 12:48:25 +0100841}
842
Russell King3ae5eae2005-11-09 22:32:44 +0000843static struct platform_driver s3c2410_nand_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 .probe = s3c2410_nand_probe,
845 .remove = s3c2410_nand_remove,
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100846 .suspend = s3c24xx_nand_suspend,
847 .resume = s3c24xx_nand_resume,
Russell King3ae5eae2005-11-09 22:32:44 +0000848 .driver = {
849 .name = "s3c2410-nand",
850 .owner = THIS_MODULE,
851 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852};
853
Russell King3ae5eae2005-11-09 22:32:44 +0000854static struct platform_driver s3c2440_nand_driver = {
Ben Dooksa4f957f2005-06-20 12:48:25 +0100855 .probe = s3c2440_nand_probe,
856 .remove = s3c2410_nand_remove,
Ben Dooksd1fef3c2006-06-19 09:29:38 +0100857 .suspend = s3c24xx_nand_suspend,
858 .resume = s3c24xx_nand_resume,
Russell King3ae5eae2005-11-09 22:32:44 +0000859 .driver = {
860 .name = "s3c2440-nand",
861 .owner = THIS_MODULE,
862 },
Ben Dooksa4f957f2005-06-20 12:48:25 +0100863};
864
Ben Dooks2c06a082006-06-27 14:35:46 +0100865static struct platform_driver s3c2412_nand_driver = {
866 .probe = s3c2412_nand_probe,
867 .remove = s3c2410_nand_remove,
868 .suspend = s3c24xx_nand_suspend,
869 .resume = s3c24xx_nand_resume,
870 .driver = {
871 .name = "s3c2412-nand",
872 .owner = THIS_MODULE,
873 },
874};
875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876static int __init s3c2410_nand_init(void)
877{
Ben Dooksa4f957f2005-06-20 12:48:25 +0100878 printk("S3C24XX NAND Driver, (c) 2004 Simtec Electronics\n");
879
Ben Dooks2c06a082006-06-27 14:35:46 +0100880 platform_driver_register(&s3c2412_nand_driver);
Russell King3ae5eae2005-11-09 22:32:44 +0000881 platform_driver_register(&s3c2440_nand_driver);
882 return platform_driver_register(&s3c2410_nand_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
884
885static void __exit s3c2410_nand_exit(void)
886{
Ben Dooks2c06a082006-06-27 14:35:46 +0100887 platform_driver_unregister(&s3c2412_nand_driver);
Russell King3ae5eae2005-11-09 22:32:44 +0000888 platform_driver_unregister(&s3c2440_nand_driver);
889 platform_driver_unregister(&s3c2410_nand_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
891
892module_init(s3c2410_nand_init);
893module_exit(s3c2410_nand_exit);
894
895MODULE_LICENSE("GPL");
896MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
Ben Dooksa4f957f2005-06-20 12:48:25 +0100897MODULE_DESCRIPTION("S3C24XX MTD NAND driver");