]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/mtd/nand/s3c2410.c
[MTD] [NAND] S3C2410 Fix previous nFCE suspend save patch
[karo-tx-linux.git] / drivers / mtd / nand / s3c2410.c
1 /* linux/drivers/mtd/nand/s3c2410.c
2  *
3  * Copyright (c) 2004,2005 Simtec Electronics
4  *      http://www.simtec.co.uk/products/SWLINUX/
5  *      Ben Dooks <ben@simtec.co.uk>
6  *
7  * Samsung S3C2410/S3C240 NAND driver
8  *
9  * Changelog:
10  *      21-Sep-2004  BJD  Initial version
11  *      23-Sep-2004  BJD  Multiple 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
14  *      18-Feb-2005  BJD  Fix sparse errors
15  *      14-Mar-2005  BJD  Applied tglx's code reduction patch
16  *      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
19  *      08-Jul-2005  BJD  Fix OOPS when no platform data supplied
20  *      20-Oct-2005  BJD  Fix timing calculation bug
21  *      14-Jan-2006  BJD  Allow clock to be stopped when idle
22  *
23  * $Id: s3c2410.c,v 1.23 2006/04/01 18:06:29 bjd Exp $
24  *
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
40 #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>
50 #include <linux/platform_device.h>
51 #include <linux/delay.h>
52 #include <linux/err.h>
53 #include <linux/slab.h>
54 #include <linux/clk.h>
55
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>
62
63 #include <asm/plat-s3c/regs-nand.h>
64 #include <asm/plat-s3c/nand.h>
65
66 #ifdef CONFIG_MTD_NAND_S3C2410_HWECC
67 static int hardware_ecc = 1;
68 #else
69 static int hardware_ecc = 0;
70 #endif
71
72 #ifdef CONFIG_MTD_NAND_S3C2410_CLKSTOP
73 static int clock_stop = 1;
74 #else
75 static const int clock_stop = 0;
76 #endif
77
78
79 /* new oob placement block for use with hardware ecc generation
80  */
81
82 static struct nand_ecclayout nand_hw_eccoob = {
83         .eccbytes = 3,
84         .eccpos = {0, 1, 2},
85         .oobfree = {{8, 8}}
86 };
87
88 /* controller and mtd information */
89
90 struct s3c2410_nand_info;
91
92 struct 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
100 enum s3c_cpu_type {
101         TYPE_S3C2410,
102         TYPE_S3C2412,
103         TYPE_S3C2440,
104 };
105
106 /* overview of the s3c2410 nand state */
107
108 struct 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;
118         void __iomem                    *regs;
119         void __iomem                    *sel_reg;
120         int                             sel_bit;
121         int                             mtd_count;
122         unsigned long                   save_sel;
123
124         enum s3c_cpu_type               cpu_type;
125 };
126
127 /* conversion functions */
128
129 static struct s3c2410_nand_mtd *s3c2410_nand_mtd_toours(struct mtd_info *mtd)
130 {
131         return container_of(mtd, struct s3c2410_nand_mtd, mtd);
132 }
133
134 static struct s3c2410_nand_info *s3c2410_nand_mtd_toinfo(struct mtd_info *mtd)
135 {
136         return s3c2410_nand_mtd_toours(mtd)->info;
137 }
138
139 static struct s3c2410_nand_info *to_nand_info(struct platform_device *dev)
140 {
141         return platform_get_drvdata(dev);
142 }
143
144 static struct s3c2410_platform_nand *to_nand_plat(struct platform_device *dev)
145 {
146         return dev->dev.platform_data;
147 }
148
149 static inline int allow_clk_stop(struct s3c2410_nand_info *info)
150 {
151         return clock_stop;
152 }
153
154 /* timing calculations */
155
156 #define NS_IN_KHZ 1000000
157
158 static int s3c_nand_calc_rate(int wanted, unsigned long clk, int max)
159 {
160         int result;
161
162         result = (wanted * clk) / NS_IN_KHZ;
163         result++;
164
165         pr_debug("result %d from %ld, %d\n", result, clk, wanted);
166
167         if (result > max) {
168                 printk("%d ns is too big for current clock rate %ld\n", wanted, clk);
169                 return -1;
170         }
171
172         if (result < 1)
173                 result = 1;
174
175         return result;
176 }
177
178 #define to_ns(ticks,clk) (((ticks) * NS_IN_KHZ) / (unsigned int)(clk))
179
180 /* controller setup */
181
182 static int s3c2410_nand_inithw(struct s3c2410_nand_info *info,
183                                struct platform_device *pdev)
184 {
185         struct s3c2410_platform_nand *plat = to_nand_plat(pdev);
186         unsigned long clkrate = clk_get_rate(info->clk);
187         int tacls_max = (info->cpu_type == TYPE_S3C2412) ? 8 : 4;
188         int tacls, twrph0, twrph1;
189         unsigned long cfg = 0;
190
191         /* calculate the timing information for the controller */
192
193         clkrate /= 1000;        /* turn clock into kHz for ease of use */
194
195         if (plat != NULL) {
196                 tacls = s3c_nand_calc_rate(plat->tacls, clkrate, tacls_max);
197                 twrph0 = s3c_nand_calc_rate(plat->twrph0, clkrate, 8);
198                 twrph1 = s3c_nand_calc_rate(plat->twrph1, clkrate, 8);
199         } else {
200                 /* default timings */
201                 tacls = tacls_max;
202                 twrph0 = 8;
203                 twrph1 = 8;
204         }
205
206         if (tacls < 0 || twrph0 < 0 || twrph1 < 0) {
207                 dev_err(info->device, "cannot get suitable timings\n");
208                 return -EINVAL;
209         }
210
211         dev_info(info->device, "Tacls=%d, %dns Twrph0=%d %dns, Twrph1=%d %dns\n",
212                tacls, to_ns(tacls, clkrate), twrph0, to_ns(twrph0, clkrate), twrph1, to_ns(twrph1, clkrate));
213
214         switch (info->cpu_type) {
215         case TYPE_S3C2410:
216                 cfg = S3C2410_NFCONF_EN;
217                 cfg |= S3C2410_NFCONF_TACLS(tacls - 1);
218                 cfg |= S3C2410_NFCONF_TWRPH0(twrph0 - 1);
219                 cfg |= S3C2410_NFCONF_TWRPH1(twrph1 - 1);
220                 break;
221
222         case TYPE_S3C2440:
223         case TYPE_S3C2412:
224                 cfg = S3C2440_NFCONF_TACLS(tacls - 1);
225                 cfg |= S3C2440_NFCONF_TWRPH0(twrph0 - 1);
226                 cfg |= S3C2440_NFCONF_TWRPH1(twrph1 - 1);
227
228                 /* enable the controller and de-assert nFCE */
229
230                 writel(S3C2440_NFCONT_ENABLE, info->regs + S3C2440_NFCONT);
231         }
232
233         dev_dbg(info->device, "NF_CONF is 0x%lx\n", cfg);
234
235         writel(cfg, info->regs + S3C2410_NFCONF);
236         return 0;
237 }
238
239 /* select chip */
240
241 static void s3c2410_nand_select_chip(struct mtd_info *mtd, int chip)
242 {
243         struct s3c2410_nand_info *info;
244         struct s3c2410_nand_mtd *nmtd;
245         struct nand_chip *this = mtd->priv;
246         unsigned long cur;
247
248         nmtd = this->priv;
249         info = nmtd->info;
250
251         if (chip != -1 && allow_clk_stop(info))
252                 clk_enable(info->clk);
253
254         cur = readl(info->sel_reg);
255
256         if (chip == -1) {
257                 cur |= info->sel_bit;
258         } else {
259                 if (nmtd->set != NULL && chip > nmtd->set->nr_chips) {
260                         dev_err(info->device, "invalid chip %d\n", chip);
261                         return;
262                 }
263
264                 if (info->platform != NULL) {
265                         if (info->platform->select_chip != NULL)
266                                 (info->platform->select_chip) (nmtd->set, chip);
267                 }
268
269                 cur &= ~info->sel_bit;
270         }
271
272         writel(cur, info->sel_reg);
273
274         if (chip == -1 && allow_clk_stop(info))
275                 clk_disable(info->clk);
276 }
277
278 /* s3c2410_nand_hwcontrol
279  *
280  * Issue command and address cycles to the chip
281 */
282
283 static void s3c2410_nand_hwcontrol(struct mtd_info *mtd, int cmd,
284                                    unsigned int ctrl)
285 {
286         struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
287
288         if (cmd == NAND_CMD_NONE)
289                 return;
290
291         if (ctrl & NAND_CLE)
292                 writeb(cmd, info->regs + S3C2410_NFCMD);
293         else
294                 writeb(cmd, info->regs + S3C2410_NFADDR);
295 }
296
297 /* command and control functions */
298
299 static void s3c2440_nand_hwcontrol(struct mtd_info *mtd, int cmd,
300                                    unsigned int ctrl)
301 {
302         struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
303
304         if (cmd == NAND_CMD_NONE)
305                 return;
306
307         if (ctrl & NAND_CLE)
308                 writeb(cmd, info->regs + S3C2440_NFCMD);
309         else
310                 writeb(cmd, info->regs + S3C2440_NFADDR);
311 }
312
313 /* s3c2410_nand_devready()
314  *
315  * returns 0 if the nand is busy, 1 if it is ready
316 */
317
318 static int s3c2410_nand_devready(struct mtd_info *mtd)
319 {
320         struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
321         return readb(info->regs + S3C2410_NFSTAT) & S3C2410_NFSTAT_BUSY;
322 }
323
324 static int s3c2440_nand_devready(struct mtd_info *mtd)
325 {
326         struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
327         return readb(info->regs + S3C2440_NFSTAT) & S3C2440_NFSTAT_READY;
328 }
329
330 static int s3c2412_nand_devready(struct mtd_info *mtd)
331 {
332         struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
333         return readb(info->regs + S3C2412_NFSTAT) & S3C2412_NFSTAT_READY;
334 }
335
336 /* ECC handling functions */
337
338 static int s3c2410_nand_correct_data(struct mtd_info *mtd, u_char *dat,
339                                      u_char *read_ecc, u_char *calc_ecc)
340 {
341         struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
342         unsigned int diff0, diff1, diff2;
343         unsigned int bit, byte;
344
345         pr_debug("%s(%p,%p,%p,%p)\n", __func__, mtd, dat, read_ecc, calc_ecc);
346
347         diff0 = read_ecc[0] ^ calc_ecc[0];
348         diff1 = read_ecc[1] ^ calc_ecc[1];
349         diff2 = read_ecc[2] ^ calc_ecc[2];
350
351         pr_debug("%s: rd %02x%02x%02x calc %02x%02x%02x diff %02x%02x%02x\n",
352                  __func__,
353                  read_ecc[0], read_ecc[1], read_ecc[2],
354                  calc_ecc[0], calc_ecc[1], calc_ecc[2],
355                  diff0, diff1, diff2);
356
357         if (diff0 == 0 && diff1 == 0 && diff2 == 0)
358                 return 0;               /* ECC is ok */
359
360         /* Can we correct this ECC (ie, one row and column change).
361          * Note, this is similar to the 256 error code on smartmedia */
362
363         if (((diff0 ^ (diff0 >> 1)) & 0x55) == 0x55 &&
364             ((diff1 ^ (diff1 >> 1)) & 0x55) == 0x55 &&
365             ((diff2 ^ (diff2 >> 1)) & 0x55) == 0x55) {
366                 /* calculate the bit position of the error */
367
368                 bit  = ((diff2 >> 3) & 1) |
369                        ((diff2 >> 4) & 2) |
370                        ((diff2 >> 5) & 4);
371
372                 /* calculate the byte position of the error */
373
374                 byte = ((diff2 << 7) & 0x100) |
375                        ((diff1 << 0) & 0x80)  |
376                        ((diff1 << 1) & 0x40)  |
377                        ((diff1 << 2) & 0x20)  |
378                        ((diff1 << 3) & 0x10)  |
379                        ((diff0 >> 4) & 0x08)  |
380                        ((diff0 >> 3) & 0x04)  |
381                        ((diff0 >> 2) & 0x02)  |
382                        ((diff0 >> 1) & 0x01);
383
384                 dev_dbg(info->device, "correcting error bit %d, byte %d\n",
385                         bit, byte);
386
387                 dat[byte] ^= (1 << bit);
388                 return 1;
389         }
390
391         /* if there is only one bit difference in the ECC, then
392          * one of only a row or column parity has changed, which
393          * means the error is most probably in the ECC itself */
394
395         diff0 |= (diff1 << 8);
396         diff0 |= (diff2 << 16);
397
398         if ((diff0 & ~(1<<fls(diff0))) == 0)
399                 return 1;
400
401         return -1;
402 }
403
404 /* ECC functions
405  *
406  * These allow the s3c2410 and s3c2440 to use the controller's ECC
407  * generator block to ECC the data as it passes through]
408 */
409
410 static void s3c2410_nand_enable_hwecc(struct mtd_info *mtd, int mode)
411 {
412         struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
413         unsigned long ctrl;
414
415         ctrl = readl(info->regs + S3C2410_NFCONF);
416         ctrl |= S3C2410_NFCONF_INITECC;
417         writel(ctrl, info->regs + S3C2410_NFCONF);
418 }
419
420 static void s3c2412_nand_enable_hwecc(struct mtd_info *mtd, int mode)
421 {
422         struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
423         unsigned long ctrl;
424
425         ctrl = readl(info->regs + S3C2440_NFCONT);
426         writel(ctrl | S3C2412_NFCONT_INIT_MAIN_ECC, info->regs + S3C2440_NFCONT);
427 }
428
429 static void s3c2440_nand_enable_hwecc(struct mtd_info *mtd, int mode)
430 {
431         struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
432         unsigned long ctrl;
433
434         ctrl = readl(info->regs + S3C2440_NFCONT);
435         writel(ctrl | S3C2440_NFCONT_INITECC, info->regs + S3C2440_NFCONT);
436 }
437
438 static int s3c2410_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
439 {
440         struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
441
442         ecc_code[0] = readb(info->regs + S3C2410_NFECC + 0);
443         ecc_code[1] = readb(info->regs + S3C2410_NFECC + 1);
444         ecc_code[2] = readb(info->regs + S3C2410_NFECC + 2);
445
446         pr_debug("%s: returning ecc %02x%02x%02x\n", __func__,
447                  ecc_code[0], ecc_code[1], ecc_code[2]);
448
449         return 0;
450 }
451
452 static int s3c2412_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
453 {
454         struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
455         unsigned long ecc = readl(info->regs + S3C2412_NFMECC0);
456
457         ecc_code[0] = ecc;
458         ecc_code[1] = ecc >> 8;
459         ecc_code[2] = ecc >> 16;
460
461         pr_debug("calculate_ecc: returning ecc %02x,%02x,%02x\n", ecc_code[0], ecc_code[1], ecc_code[2]);
462
463         return 0;
464 }
465
466 static int s3c2440_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
467 {
468         struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
469         unsigned long ecc = readl(info->regs + S3C2440_NFMECC0);
470
471         ecc_code[0] = ecc;
472         ecc_code[1] = ecc >> 8;
473         ecc_code[2] = ecc >> 16;
474
475         pr_debug("%s: returning ecc %06lx\n", __func__, ecc);
476
477         return 0;
478 }
479
480 /* over-ride the standard functions for a little more speed. We can
481  * use read/write block to move the data buffers to/from the controller
482 */
483
484 static void s3c2410_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
485 {
486         struct nand_chip *this = mtd->priv;
487         readsb(this->IO_ADDR_R, buf, len);
488 }
489
490 static void s3c2440_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
491 {
492         struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
493         readsl(info->regs + S3C2440_NFDATA, buf, len / 4);
494 }
495
496 static void s3c2410_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
497 {
498         struct nand_chip *this = mtd->priv;
499         writesb(this->IO_ADDR_W, buf, len);
500 }
501
502 static void s3c2440_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
503 {
504         struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
505         writesl(info->regs + S3C2440_NFDATA, buf, len / 4);
506 }
507
508 /* device management functions */
509
510 static int s3c2410_nand_remove(struct platform_device *pdev)
511 {
512         struct s3c2410_nand_info *info = to_nand_info(pdev);
513
514         platform_set_drvdata(pdev, NULL);
515
516         if (info == NULL)
517                 return 0;
518
519         /* first thing we need to do is release all our mtds
520          * and their partitions, then go through freeing the
521          * resources used
522          */
523
524         if (info->mtds != NULL) {
525                 struct s3c2410_nand_mtd *ptr = info->mtds;
526                 int mtdno;
527
528                 for (mtdno = 0; mtdno < info->mtd_count; mtdno++, ptr++) {
529                         pr_debug("releasing mtd %d (%p)\n", mtdno, ptr);
530                         nand_release(&ptr->mtd);
531                 }
532
533                 kfree(info->mtds);
534         }
535
536         /* free the common resources */
537
538         if (info->clk != NULL && !IS_ERR(info->clk)) {
539                 if (!allow_clk_stop(info))
540                         clk_disable(info->clk);
541                 clk_put(info->clk);
542         }
543
544         if (info->regs != NULL) {
545                 iounmap(info->regs);
546                 info->regs = NULL;
547         }
548
549         if (info->area != NULL) {
550                 release_resource(info->area);
551                 kfree(info->area);
552                 info->area = NULL;
553         }
554
555         kfree(info);
556
557         return 0;
558 }
559
560 #ifdef CONFIG_MTD_PARTITIONS
561 static int s3c2410_nand_add_partition(struct s3c2410_nand_info *info,
562                                       struct s3c2410_nand_mtd *mtd,
563                                       struct s3c2410_nand_set *set)
564 {
565         if (set == NULL)
566                 return add_mtd_device(&mtd->mtd);
567
568         if (set->nr_partitions > 0 && set->partitions != NULL) {
569                 return add_mtd_partitions(&mtd->mtd, set->partitions, set->nr_partitions);
570         }
571
572         return add_mtd_device(&mtd->mtd);
573 }
574 #else
575 static int s3c2410_nand_add_partition(struct s3c2410_nand_info *info,
576                                       struct s3c2410_nand_mtd *mtd,
577                                       struct s3c2410_nand_set *set)
578 {
579         return add_mtd_device(&mtd->mtd);
580 }
581 #endif
582
583 /* s3c2410_nand_init_chip
584  *
585  * init a single instance of an chip
586 */
587
588 static void s3c2410_nand_init_chip(struct s3c2410_nand_info *info,
589                                    struct s3c2410_nand_mtd *nmtd,
590                                    struct s3c2410_nand_set *set)
591 {
592         struct nand_chip *chip = &nmtd->chip;
593         void __iomem *regs = info->regs;
594
595         chip->write_buf    = s3c2410_nand_write_buf;
596         chip->read_buf     = s3c2410_nand_read_buf;
597         chip->select_chip  = s3c2410_nand_select_chip;
598         chip->chip_delay   = 50;
599         chip->priv         = nmtd;
600         chip->options      = 0;
601         chip->controller   = &info->controller;
602
603         switch (info->cpu_type) {
604         case TYPE_S3C2410:
605                 chip->IO_ADDR_W = regs + S3C2410_NFDATA;
606                 info->sel_reg   = regs + S3C2410_NFCONF;
607                 info->sel_bit   = S3C2410_NFCONF_nFCE;
608                 chip->cmd_ctrl  = s3c2410_nand_hwcontrol;
609                 chip->dev_ready = s3c2410_nand_devready;
610                 break;
611
612         case TYPE_S3C2440:
613                 chip->IO_ADDR_W = regs + S3C2440_NFDATA;
614                 info->sel_reg   = regs + S3C2440_NFCONT;
615                 info->sel_bit   = S3C2440_NFCONT_nFCE;
616                 chip->cmd_ctrl  = s3c2440_nand_hwcontrol;
617                 chip->dev_ready = s3c2440_nand_devready;
618                 chip->read_buf  = s3c2440_nand_read_buf;
619                 chip->write_buf = s3c2440_nand_write_buf;
620                 break;
621
622         case TYPE_S3C2412:
623                 chip->IO_ADDR_W = regs + S3C2440_NFDATA;
624                 info->sel_reg   = regs + S3C2440_NFCONT;
625                 info->sel_bit   = S3C2412_NFCONT_nFCE0;
626                 chip->cmd_ctrl  = s3c2440_nand_hwcontrol;
627                 chip->dev_ready = s3c2412_nand_devready;
628
629                 if (readl(regs + S3C2410_NFCONF) & S3C2412_NFCONF_NANDBOOT)
630                         dev_info(info->device, "System booted from NAND\n");
631
632                 break;
633         }
634
635         chip->IO_ADDR_R = chip->IO_ADDR_W;
636
637         nmtd->info         = info;
638         nmtd->mtd.priv     = chip;
639         nmtd->mtd.owner    = THIS_MODULE;
640         nmtd->set          = set;
641
642         if (hardware_ecc) {
643                 chip->ecc.calculate = s3c2410_nand_calculate_ecc;
644                 chip->ecc.correct   = s3c2410_nand_correct_data;
645                 chip->ecc.mode      = NAND_ECC_HW;
646                 chip->ecc.size      = 512;
647                 chip->ecc.bytes     = 3;
648                 chip->ecc.layout    = &nand_hw_eccoob;
649
650                 switch (info->cpu_type) {
651                 case TYPE_S3C2410:
652                         chip->ecc.hwctl     = s3c2410_nand_enable_hwecc;
653                         chip->ecc.calculate = s3c2410_nand_calculate_ecc;
654                         break;
655
656                 case TYPE_S3C2412:
657                         chip->ecc.hwctl     = s3c2412_nand_enable_hwecc;
658                         chip->ecc.calculate = s3c2412_nand_calculate_ecc;
659                         break;
660
661                 case TYPE_S3C2440:
662                         chip->ecc.hwctl     = s3c2440_nand_enable_hwecc;
663                         chip->ecc.calculate = s3c2440_nand_calculate_ecc;
664                         break;
665
666                 }
667         } else {
668                 chip->ecc.mode      = NAND_ECC_SOFT;
669         }
670 }
671
672 /* s3c2410_nand_probe
673  *
674  * called by device layer when it finds a device matching
675  * one our driver can handled. This code checks to see if
676  * it can allocate all necessary resources then calls the
677  * nand layer to look for devices
678 */
679
680 static int s3c24xx_nand_probe(struct platform_device *pdev,
681                               enum s3c_cpu_type cpu_type)
682 {
683         struct s3c2410_platform_nand *plat = to_nand_plat(pdev);
684         struct s3c2410_nand_info *info;
685         struct s3c2410_nand_mtd *nmtd;
686         struct s3c2410_nand_set *sets;
687         struct resource *res;
688         int err = 0;
689         int size;
690         int nr_sets;
691         int setno;
692
693         pr_debug("s3c2410_nand_probe(%p)\n", pdev);
694
695         info = kmalloc(sizeof(*info), GFP_KERNEL);
696         if (info == NULL) {
697                 dev_err(&pdev->dev, "no memory for flash info\n");
698                 err = -ENOMEM;
699                 goto exit_error;
700         }
701
702         memzero(info, sizeof(*info));
703         platform_set_drvdata(pdev, info);
704
705         spin_lock_init(&info->controller.lock);
706         init_waitqueue_head(&info->controller.wq);
707
708         /* get the clock source and enable it */
709
710         info->clk = clk_get(&pdev->dev, "nand");
711         if (IS_ERR(info->clk)) {
712                 dev_err(&pdev->dev, "failed to get clock\n");
713                 err = -ENOENT;
714                 goto exit_error;
715         }
716
717         clk_enable(info->clk);
718
719         /* allocate and map the resource */
720
721         /* currently we assume we have the one resource */
722         res  = pdev->resource;
723         size = res->end - res->start + 1;
724
725         info->area = request_mem_region(res->start, size, pdev->name);
726
727         if (info->area == NULL) {
728                 dev_err(&pdev->dev, "cannot reserve register region\n");
729                 err = -ENOENT;
730                 goto exit_error;
731         }
732
733         info->device     = &pdev->dev;
734         info->platform   = plat;
735         info->regs       = ioremap(res->start, size);
736         info->cpu_type   = cpu_type;
737
738         if (info->regs == NULL) {
739                 dev_err(&pdev->dev, "cannot reserve register region\n");
740                 err = -EIO;
741                 goto exit_error;
742         }
743
744         dev_dbg(&pdev->dev, "mapped registers at %p\n", info->regs);
745
746         /* initialise the hardware */
747
748         err = s3c2410_nand_inithw(info, pdev);
749         if (err != 0)
750                 goto exit_error;
751
752         sets = (plat != NULL) ? plat->sets : NULL;
753         nr_sets = (plat != NULL) ? plat->nr_sets : 1;
754
755         info->mtd_count = nr_sets;
756
757         /* allocate our information */
758
759         size = nr_sets * sizeof(*info->mtds);
760         info->mtds = kmalloc(size, GFP_KERNEL);
761         if (info->mtds == NULL) {
762                 dev_err(&pdev->dev, "failed to allocate mtd storage\n");
763                 err = -ENOMEM;
764                 goto exit_error;
765         }
766
767         memzero(info->mtds, size);
768
769         /* initialise all possible chips */
770
771         nmtd = info->mtds;
772
773         for (setno = 0; setno < nr_sets; setno++, nmtd++) {
774                 pr_debug("initialising set %d (%p, info %p)\n", setno, nmtd, info);
775
776                 s3c2410_nand_init_chip(info, nmtd, sets);
777
778                 nmtd->scan_res = nand_scan(&nmtd->mtd, (sets) ? sets->nr_chips : 1);
779
780                 if (nmtd->scan_res == 0) {
781                         s3c2410_nand_add_partition(info, nmtd, sets);
782                 }
783
784                 if (sets != NULL)
785                         sets++;
786         }
787
788         if (allow_clk_stop(info)) {
789                 dev_info(&pdev->dev, "clock idle support enabled\n");
790                 clk_disable(info->clk);
791         }
792
793         pr_debug("initialised ok\n");
794         return 0;
795
796  exit_error:
797         s3c2410_nand_remove(pdev);
798
799         if (err == 0)
800                 err = -EINVAL;
801         return err;
802 }
803
804 /* PM Support */
805 #ifdef CONFIG_PM
806
807 static int s3c24xx_nand_suspend(struct platform_device *dev, pm_message_t pm)
808 {
809         struct s3c2410_nand_info *info = platform_get_drvdata(dev);
810
811         if (info) {
812                 info->save_sel = readl(info->sel_reg);
813
814                 /* For the moment, we must ensure nFCE is high during
815                  * the time we are suspended. This really should be
816                  * handled by suspending the MTDs we are using, but
817                  * that is currently not the case. */
818
819                 writel(info->save_sel | info->sel_bit, info->sel_reg);
820
821                 if (!allow_clk_stop(info))
822                         clk_disable(info->clk);
823         }
824
825         return 0;
826 }
827
828 static int s3c24xx_nand_resume(struct platform_device *dev)
829 {
830         struct s3c2410_nand_info *info = platform_get_drvdata(dev);
831         unsigned long sel;
832
833         if (info) {
834                 clk_enable(info->clk);
835                 s3c2410_nand_inithw(info, dev);
836
837                 /* Restore the state of the nFCE line. */
838
839                 sel = readl(info->sel_reg);
840                 sel &= ~info->sel_bit;
841                 sel |= info->save_sel & info->sel_bit;
842                 writel(sel, info->sel_reg);
843
844                 if (allow_clk_stop(info))
845                         clk_disable(info->clk);
846         }
847
848         return 0;
849 }
850
851 #else
852 #define s3c24xx_nand_suspend NULL
853 #define s3c24xx_nand_resume NULL
854 #endif
855
856 /* driver device registration */
857
858 static int s3c2410_nand_probe(struct platform_device *dev)
859 {
860         return s3c24xx_nand_probe(dev, TYPE_S3C2410);
861 }
862
863 static int s3c2440_nand_probe(struct platform_device *dev)
864 {
865         return s3c24xx_nand_probe(dev, TYPE_S3C2440);
866 }
867
868 static int s3c2412_nand_probe(struct platform_device *dev)
869 {
870         return s3c24xx_nand_probe(dev, TYPE_S3C2412);
871 }
872
873 static struct platform_driver s3c2410_nand_driver = {
874         .probe          = s3c2410_nand_probe,
875         .remove         = s3c2410_nand_remove,
876         .suspend        = s3c24xx_nand_suspend,
877         .resume         = s3c24xx_nand_resume,
878         .driver         = {
879                 .name   = "s3c2410-nand",
880                 .owner  = THIS_MODULE,
881         },
882 };
883
884 static struct platform_driver s3c2440_nand_driver = {
885         .probe          = s3c2440_nand_probe,
886         .remove         = s3c2410_nand_remove,
887         .suspend        = s3c24xx_nand_suspend,
888         .resume         = s3c24xx_nand_resume,
889         .driver         = {
890                 .name   = "s3c2440-nand",
891                 .owner  = THIS_MODULE,
892         },
893 };
894
895 static struct platform_driver s3c2412_nand_driver = {
896         .probe          = s3c2412_nand_probe,
897         .remove         = s3c2410_nand_remove,
898         .suspend        = s3c24xx_nand_suspend,
899         .resume         = s3c24xx_nand_resume,
900         .driver         = {
901                 .name   = "s3c2412-nand",
902                 .owner  = THIS_MODULE,
903         },
904 };
905
906 static int __init s3c2410_nand_init(void)
907 {
908         printk("S3C24XX NAND Driver, (c) 2004 Simtec Electronics\n");
909
910         platform_driver_register(&s3c2412_nand_driver);
911         platform_driver_register(&s3c2440_nand_driver);
912         return platform_driver_register(&s3c2410_nand_driver);
913 }
914
915 static void __exit s3c2410_nand_exit(void)
916 {
917         platform_driver_unregister(&s3c2412_nand_driver);
918         platform_driver_unregister(&s3c2440_nand_driver);
919         platform_driver_unregister(&s3c2410_nand_driver);
920 }
921
922 module_init(s3c2410_nand_init);
923 module_exit(s3c2410_nand_exit);
924
925 MODULE_LICENSE("GPL");
926 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
927 MODULE_DESCRIPTION("S3C24XX MTD NAND driver");
928 MODULE_ALIAS("platform:s3c2410-nand");
929 MODULE_ALIAS("platform:s3c2412-nand");
930 MODULE_ALIAS("platform:s3c2440-nand");