1 /* linux/drivers/mtd/nand/s3c2410.c
3 * Copyright © 2004-2008 Simtec Electronics
4 * http://armlinux.simtec.co.uk/
5 * Ben Dooks <ben@simtec.co.uk>
7 * Samsung S3C2410/S3C2440/S3C2412 NAND driver
9 * 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.
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.
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
24 #ifdef CONFIG_MTD_NAND_S3C2410_DEBUG
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>
34 #include <linux/platform_device.h>
35 #include <linux/delay.h>
36 #include <linux/err.h>
37 #include <linux/slab.h>
38 #include <linux/clk.h>
39 #include <linux/cpufreq.h>
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>
48 #include <plat/regs-nand.h>
49 #include <plat/nand.h>
51 #ifdef CONFIG_MTD_NAND_S3C2410_HWECC
52 static int hardware_ecc = 1;
54 static int hardware_ecc = 0;
57 #ifdef CONFIG_MTD_NAND_S3C2410_CLKSTOP
58 static int clock_stop = 1;
60 static const int clock_stop = 0;
64 /* new oob placement block for use with hardware ecc generation
67 static struct nand_ecclayout nand_hw_eccoob = {
73 /* controller and mtd information */
75 struct s3c2410_nand_info;
78 * struct s3c2410_nand_mtd - driver MTD structure
79 * @mtd: The MTD instance to pass to the MTD layer.
80 * @chip: The NAND chip information.
81 * @set: The platform information supplied for this set of NAND chips.
82 * @info: Link back to the hardware information.
83 * @scan_res: The result from calling nand_scan_ident().
85 struct s3c2410_nand_mtd {
87 struct nand_chip chip;
88 struct s3c2410_nand_set *set;
89 struct s3c2410_nand_info *info;
99 /* overview of the s3c2410 nand state */
102 * struct s3c2410_nand_info - NAND controller state.
103 * @mtds: An array of MTD instances on this controoler.
104 * @platform: The platform data for this board.
105 * @device: The platform device we bound to.
106 * @area: The IO area resource that came from request_mem_region().
107 * @clk: The clock resource for this controller.
108 * @regs: The area mapped for the hardware registers described by @area.
109 * @sel_reg: Pointer to the register controlling the NAND selection.
110 * @sel_bit: The bit in @sel_reg to select the NAND chip.
111 * @mtd_count: The number of MTDs created from this controller.
112 * @save_sel: The contents of @sel_reg to be saved over suspend.
113 * @clk_rate: The clock rate from @clk.
114 * @cpu_type: The exact type of this controller.
116 struct s3c2410_nand_info {
118 struct nand_hw_control controller;
119 struct s3c2410_nand_mtd *mtds;
120 struct s3c2410_platform_nand *platform;
123 struct device *device;
124 struct resource *area;
127 void __iomem *sel_reg;
130 unsigned long save_sel;
131 unsigned long clk_rate;
133 enum s3c_cpu_type cpu_type;
135 #ifdef CONFIG_CPU_FREQ
136 struct notifier_block freq_transition;
140 /* conversion functions */
142 static struct s3c2410_nand_mtd *s3c2410_nand_mtd_toours(struct mtd_info *mtd)
144 return container_of(mtd, struct s3c2410_nand_mtd, mtd);
147 static struct s3c2410_nand_info *s3c2410_nand_mtd_toinfo(struct mtd_info *mtd)
149 return s3c2410_nand_mtd_toours(mtd)->info;
152 static struct s3c2410_nand_info *to_nand_info(struct platform_device *dev)
154 return platform_get_drvdata(dev);
157 static struct s3c2410_platform_nand *to_nand_plat(struct platform_device *dev)
159 return dev->dev.platform_data;
162 static inline int allow_clk_stop(struct s3c2410_nand_info *info)
167 /* timing calculations */
169 #define NS_IN_KHZ 1000000
172 * s3c_nand_calc_rate - calculate timing data.
173 * @wanted: The cycle time in nanoseconds.
174 * @clk: The clock rate in kHz.
175 * @max: The maximum divider value.
177 * Calculate the timing value from the given parameters.
179 static int s3c_nand_calc_rate(int wanted, unsigned long clk, int max)
183 result = (wanted * clk) / NS_IN_KHZ;
186 pr_debug("result %d from %ld, %d\n", result, clk, wanted);
189 printk("%d ns is too big for current clock rate %ld\n", wanted, clk);
199 #define to_ns(ticks,clk) (((ticks) * NS_IN_KHZ) / (unsigned int)(clk))
201 /* controller setup */
204 * s3c2410_nand_setrate - setup controller timing information.
205 * @info: The controller instance.
207 * Given the information supplied by the platform, calculate and set
208 * the necessary timing registers in the hardware to generate the
209 * necessary timing cycles to the hardware.
211 static int s3c2410_nand_setrate(struct s3c2410_nand_info *info)
213 struct s3c2410_platform_nand *plat = info->platform;
214 int tacls_max = (info->cpu_type == TYPE_S3C2412) ? 8 : 4;
215 int tacls, twrph0, twrph1;
216 unsigned long clkrate = clk_get_rate(info->clk);
217 unsigned long uninitialized_var(set), cfg, uninitialized_var(mask);
220 /* calculate the timing information for the controller */
222 info->clk_rate = clkrate;
223 clkrate /= 1000; /* turn clock into kHz for ease of use */
226 tacls = s3c_nand_calc_rate(plat->tacls, clkrate, tacls_max);
227 twrph0 = s3c_nand_calc_rate(plat->twrph0, clkrate, 8);
228 twrph1 = s3c_nand_calc_rate(plat->twrph1, clkrate, 8);
230 /* default timings */
236 if (tacls < 0 || twrph0 < 0 || twrph1 < 0) {
237 dev_err(info->device, "cannot get suitable timings\n");
241 dev_info(info->device, "Tacls=%d, %dns Twrph0=%d %dns, Twrph1=%d %dns\n",
242 tacls, to_ns(tacls, clkrate), twrph0, to_ns(twrph0, clkrate), twrph1, to_ns(twrph1, clkrate));
244 switch (info->cpu_type) {
246 mask = (S3C2410_NFCONF_TACLS(3) |
247 S3C2410_NFCONF_TWRPH0(7) |
248 S3C2410_NFCONF_TWRPH1(7));
249 set = S3C2410_NFCONF_EN;
250 set |= S3C2410_NFCONF_TACLS(tacls - 1);
251 set |= S3C2410_NFCONF_TWRPH0(twrph0 - 1);
252 set |= S3C2410_NFCONF_TWRPH1(twrph1 - 1);
257 mask = (S3C2410_NFCONF_TACLS(tacls_max - 1) |
258 S3C2410_NFCONF_TWRPH0(7) |
259 S3C2410_NFCONF_TWRPH1(7));
261 set = S3C2440_NFCONF_TACLS(tacls - 1);
262 set |= S3C2440_NFCONF_TWRPH0(twrph0 - 1);
263 set |= S3C2440_NFCONF_TWRPH1(twrph1 - 1);
270 local_irq_save(flags);
272 cfg = readl(info->regs + S3C2410_NFCONF);
275 writel(cfg, info->regs + S3C2410_NFCONF);
277 local_irq_restore(flags);
279 dev_dbg(info->device, "NF_CONF is 0x%lx\n", cfg);
285 * s3c2410_nand_inithw - basic hardware initialisation
286 * @info: The hardware state.
288 * Do the basic initialisation of the hardware, using s3c2410_nand_setrate()
289 * to setup the hardware access speeds and set the controller to be enabled.
291 static int s3c2410_nand_inithw(struct s3c2410_nand_info *info)
295 ret = s3c2410_nand_setrate(info);
299 switch (info->cpu_type) {
306 /* enable the controller and de-assert nFCE */
308 writel(S3C2440_NFCONT_ENABLE, info->regs + S3C2440_NFCONT);
315 * s3c2410_nand_select_chip - select the given nand chip
316 * @mtd: The MTD instance for this chip.
317 * @chip: The chip number.
319 * This is called by the MTD layer to either select a given chip for the
320 * @mtd instance, or to indicate that the access has finished and the
321 * chip can be de-selected.
323 * The routine ensures that the nFCE line is correctly setup, and any
324 * platform specific selection code is called to route nFCE to the specific
327 static void s3c2410_nand_select_chip(struct mtd_info *mtd, int chip)
329 struct s3c2410_nand_info *info;
330 struct s3c2410_nand_mtd *nmtd;
331 struct nand_chip *this = mtd->priv;
337 if (chip != -1 && allow_clk_stop(info))
338 clk_enable(info->clk);
340 cur = readl(info->sel_reg);
343 cur |= info->sel_bit;
345 if (nmtd->set != NULL && chip > nmtd->set->nr_chips) {
346 dev_err(info->device, "invalid chip %d\n", chip);
350 if (info->platform != NULL) {
351 if (info->platform->select_chip != NULL)
352 (info->platform->select_chip) (nmtd->set, chip);
355 cur &= ~info->sel_bit;
358 writel(cur, info->sel_reg);
360 if (chip == -1 && allow_clk_stop(info))
361 clk_disable(info->clk);
364 /* s3c2410_nand_hwcontrol
366 * Issue command and address cycles to the chip
369 static void s3c2410_nand_hwcontrol(struct mtd_info *mtd, int cmd,
372 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
374 if (cmd == NAND_CMD_NONE)
378 writeb(cmd, info->regs + S3C2410_NFCMD);
380 writeb(cmd, info->regs + S3C2410_NFADDR);
383 /* command and control functions */
385 static void s3c2440_nand_hwcontrol(struct mtd_info *mtd, int cmd,
388 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
390 if (cmd == NAND_CMD_NONE)
394 writeb(cmd, info->regs + S3C2440_NFCMD);
396 writeb(cmd, info->regs + S3C2440_NFADDR);
399 /* s3c2410_nand_devready()
401 * returns 0 if the nand is busy, 1 if it is ready
404 static int s3c2410_nand_devready(struct mtd_info *mtd)
406 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
407 return readb(info->regs + S3C2410_NFSTAT) & S3C2410_NFSTAT_BUSY;
410 static int s3c2440_nand_devready(struct mtd_info *mtd)
412 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
413 return readb(info->regs + S3C2440_NFSTAT) & S3C2440_NFSTAT_READY;
416 static int s3c2412_nand_devready(struct mtd_info *mtd)
418 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
419 return readb(info->regs + S3C2412_NFSTAT) & S3C2412_NFSTAT_READY;
422 /* ECC handling functions */
424 static int s3c2410_nand_correct_data(struct mtd_info *mtd, u_char *dat,
425 u_char *read_ecc, u_char *calc_ecc)
427 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
428 unsigned int diff0, diff1, diff2;
429 unsigned int bit, byte;
431 pr_debug("%s(%p,%p,%p,%p)\n", __func__, mtd, dat, read_ecc, calc_ecc);
433 diff0 = read_ecc[0] ^ calc_ecc[0];
434 diff1 = read_ecc[1] ^ calc_ecc[1];
435 diff2 = read_ecc[2] ^ calc_ecc[2];
437 pr_debug("%s: rd %02x%02x%02x calc %02x%02x%02x diff %02x%02x%02x\n",
439 read_ecc[0], read_ecc[1], read_ecc[2],
440 calc_ecc[0], calc_ecc[1], calc_ecc[2],
441 diff0, diff1, diff2);
443 if (diff0 == 0 && diff1 == 0 && diff2 == 0)
444 return 0; /* ECC is ok */
446 /* sometimes people do not think about using the ECC, so check
447 * to see if we have an 0xff,0xff,0xff read ECC and then ignore
448 * the error, on the assumption that this is an un-eccd page.
450 if (read_ecc[0] == 0xff && read_ecc[1] == 0xff && read_ecc[2] == 0xff
451 && info->platform->ignore_unset_ecc)
454 /* Can we correct this ECC (ie, one row and column change).
455 * Note, this is similar to the 256 error code on smartmedia */
457 if (((diff0 ^ (diff0 >> 1)) & 0x55) == 0x55 &&
458 ((diff1 ^ (diff1 >> 1)) & 0x55) == 0x55 &&
459 ((diff2 ^ (diff2 >> 1)) & 0x55) == 0x55) {
460 /* calculate the bit position of the error */
462 bit = ((diff2 >> 3) & 1) |
466 /* calculate the byte position of the error */
468 byte = ((diff2 << 7) & 0x100) |
469 ((diff1 << 0) & 0x80) |
470 ((diff1 << 1) & 0x40) |
471 ((diff1 << 2) & 0x20) |
472 ((diff1 << 3) & 0x10) |
473 ((diff0 >> 4) & 0x08) |
474 ((diff0 >> 3) & 0x04) |
475 ((diff0 >> 2) & 0x02) |
476 ((diff0 >> 1) & 0x01);
478 dev_dbg(info->device, "correcting error bit %d, byte %d\n",
481 dat[byte] ^= (1 << bit);
485 /* if there is only one bit difference in the ECC, then
486 * one of only a row or column parity has changed, which
487 * means the error is most probably in the ECC itself */
489 diff0 |= (diff1 << 8);
490 diff0 |= (diff2 << 16);
492 if ((diff0 & ~(1<<fls(diff0))) == 0)
500 * These allow the s3c2410 and s3c2440 to use the controller's ECC
501 * generator block to ECC the data as it passes through]
504 static void s3c2410_nand_enable_hwecc(struct mtd_info *mtd, int mode)
506 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
509 ctrl = readl(info->regs + S3C2410_NFCONF);
510 ctrl |= S3C2410_NFCONF_INITECC;
511 writel(ctrl, info->regs + S3C2410_NFCONF);
514 static void s3c2412_nand_enable_hwecc(struct mtd_info *mtd, int mode)
516 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
519 ctrl = readl(info->regs + S3C2440_NFCONT);
520 writel(ctrl | S3C2412_NFCONT_INIT_MAIN_ECC, info->regs + S3C2440_NFCONT);
523 static void s3c2440_nand_enable_hwecc(struct mtd_info *mtd, int mode)
525 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
528 ctrl = readl(info->regs + S3C2440_NFCONT);
529 writel(ctrl | S3C2440_NFCONT_INITECC, info->regs + S3C2440_NFCONT);
532 static int s3c2410_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
534 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
536 ecc_code[0] = readb(info->regs + S3C2410_NFECC + 0);
537 ecc_code[1] = readb(info->regs + S3C2410_NFECC + 1);
538 ecc_code[2] = readb(info->regs + S3C2410_NFECC + 2);
540 pr_debug("%s: returning ecc %02x%02x%02x\n", __func__,
541 ecc_code[0], ecc_code[1], ecc_code[2]);
546 static int s3c2412_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
548 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
549 unsigned long ecc = readl(info->regs + S3C2412_NFMECC0);
552 ecc_code[1] = ecc >> 8;
553 ecc_code[2] = ecc >> 16;
555 pr_debug("calculate_ecc: returning ecc %02x,%02x,%02x\n", ecc_code[0], ecc_code[1], ecc_code[2]);
560 static int s3c2440_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
562 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
563 unsigned long ecc = readl(info->regs + S3C2440_NFMECC0);
566 ecc_code[1] = ecc >> 8;
567 ecc_code[2] = ecc >> 16;
569 pr_debug("%s: returning ecc %06lx\n", __func__, ecc & 0xffffff);
574 /* over-ride the standard functions for a little more speed. We can
575 * use read/write block to move the data buffers to/from the controller
578 static void s3c2410_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
580 struct nand_chip *this = mtd->priv;
581 readsb(this->IO_ADDR_R, buf, len);
584 static void s3c2440_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
586 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
588 readsl(info->regs + S3C2440_NFDATA, buf, len >> 2);
590 /* cleanup if we've got less than a word to do */
594 for (; len & 3; len--)
595 *buf++ = readb(info->regs + S3C2440_NFDATA);
599 static void s3c2410_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
601 struct nand_chip *this = mtd->priv;
602 writesb(this->IO_ADDR_W, buf, len);
605 static void s3c2440_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
607 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
609 writesl(info->regs + S3C2440_NFDATA, buf, len >> 2);
611 /* cleanup any fractional write */
615 for (; len & 3; len--, buf++)
616 writeb(*buf, info->regs + S3C2440_NFDATA);
620 /* cpufreq driver support */
622 #ifdef CONFIG_CPU_FREQ
624 static int s3c2410_nand_cpufreq_transition(struct notifier_block *nb,
625 unsigned long val, void *data)
627 struct s3c2410_nand_info *info;
628 unsigned long newclk;
630 info = container_of(nb, struct s3c2410_nand_info, freq_transition);
631 newclk = clk_get_rate(info->clk);
633 if ((val == CPUFREQ_POSTCHANGE && newclk < info->clk_rate) ||
634 (val == CPUFREQ_PRECHANGE && newclk > info->clk_rate)) {
635 s3c2410_nand_setrate(info);
641 static inline int s3c2410_nand_cpufreq_register(struct s3c2410_nand_info *info)
643 info->freq_transition.notifier_call = s3c2410_nand_cpufreq_transition;
645 return cpufreq_register_notifier(&info->freq_transition,
646 CPUFREQ_TRANSITION_NOTIFIER);
649 static inline void s3c2410_nand_cpufreq_deregister(struct s3c2410_nand_info *info)
651 cpufreq_unregister_notifier(&info->freq_transition,
652 CPUFREQ_TRANSITION_NOTIFIER);
656 static inline int s3c2410_nand_cpufreq_register(struct s3c2410_nand_info *info)
661 static inline void s3c2410_nand_cpufreq_deregister(struct s3c2410_nand_info *info)
666 /* device management functions */
668 static int s3c24xx_nand_remove(struct platform_device *pdev)
670 struct s3c2410_nand_info *info = to_nand_info(pdev);
672 platform_set_drvdata(pdev, NULL);
677 s3c2410_nand_cpufreq_deregister(info);
679 /* Release all our mtds and their partitions, then go through
680 * freeing the resources used
683 if (info->mtds != NULL) {
684 struct s3c2410_nand_mtd *ptr = info->mtds;
687 for (mtdno = 0; mtdno < info->mtd_count; mtdno++, ptr++) {
688 pr_debug("releasing mtd %d (%p)\n", mtdno, ptr);
689 nand_release(&ptr->mtd);
695 /* free the common resources */
697 if (info->clk != NULL && !IS_ERR(info->clk)) {
698 if (!allow_clk_stop(info))
699 clk_disable(info->clk);
703 if (info->regs != NULL) {
708 if (info->area != NULL) {
709 release_resource(info->area);
719 #ifdef CONFIG_MTD_PARTITIONS
720 const char *part_probes[] = { "cmdlinepart", NULL };
721 static int s3c2410_nand_add_partition(struct s3c2410_nand_info *info,
722 struct s3c2410_nand_mtd *mtd,
723 struct s3c2410_nand_set *set)
725 struct mtd_partition *part_info;
729 return add_mtd_device(&mtd->mtd);
731 if (set->nr_partitions == 0) {
732 mtd->mtd.name = set->name;
733 nr_part = parse_mtd_partitions(&mtd->mtd, part_probes,
736 if (set->nr_partitions > 0 && set->partitions != NULL) {
737 nr_part = set->nr_partitions;
738 part_info = set->partitions;
742 if (nr_part > 0 && part_info)
743 return add_mtd_partitions(&mtd->mtd, part_info, nr_part);
745 return add_mtd_device(&mtd->mtd);
748 static int s3c2410_nand_add_partition(struct s3c2410_nand_info *info,
749 struct s3c2410_nand_mtd *mtd,
750 struct s3c2410_nand_set *set)
752 return add_mtd_device(&mtd->mtd);
757 * s3c2410_nand_init_chip - initialise a single instance of an chip
758 * @info: The base NAND controller the chip is on.
759 * @nmtd: The new controller MTD instance to fill in.
760 * @set: The information passed from the board specific platform data.
762 * Initialise the given @nmtd from the information in @info and @set. This
763 * readies the structure for use with the MTD layer functions by ensuring
764 * all pointers are setup and the necessary control routines selected.
766 static void s3c2410_nand_init_chip(struct s3c2410_nand_info *info,
767 struct s3c2410_nand_mtd *nmtd,
768 struct s3c2410_nand_set *set)
770 struct nand_chip *chip = &nmtd->chip;
771 void __iomem *regs = info->regs;
773 chip->write_buf = s3c2410_nand_write_buf;
774 chip->read_buf = s3c2410_nand_read_buf;
775 chip->select_chip = s3c2410_nand_select_chip;
776 chip->chip_delay = 50;
779 chip->controller = &info->controller;
781 switch (info->cpu_type) {
783 chip->IO_ADDR_W = regs + S3C2410_NFDATA;
784 info->sel_reg = regs + S3C2410_NFCONF;
785 info->sel_bit = S3C2410_NFCONF_nFCE;
786 chip->cmd_ctrl = s3c2410_nand_hwcontrol;
787 chip->dev_ready = s3c2410_nand_devready;
791 chip->IO_ADDR_W = regs + S3C2440_NFDATA;
792 info->sel_reg = regs + S3C2440_NFCONT;
793 info->sel_bit = S3C2440_NFCONT_nFCE;
794 chip->cmd_ctrl = s3c2440_nand_hwcontrol;
795 chip->dev_ready = s3c2440_nand_devready;
796 chip->read_buf = s3c2440_nand_read_buf;
797 chip->write_buf = s3c2440_nand_write_buf;
801 chip->IO_ADDR_W = regs + S3C2440_NFDATA;
802 info->sel_reg = regs + S3C2440_NFCONT;
803 info->sel_bit = S3C2412_NFCONT_nFCE0;
804 chip->cmd_ctrl = s3c2440_nand_hwcontrol;
805 chip->dev_ready = s3c2412_nand_devready;
807 if (readl(regs + S3C2410_NFCONF) & S3C2412_NFCONF_NANDBOOT)
808 dev_info(info->device, "System booted from NAND\n");
813 chip->IO_ADDR_R = chip->IO_ADDR_W;
816 nmtd->mtd.priv = chip;
817 nmtd->mtd.owner = THIS_MODULE;
821 chip->ecc.calculate = s3c2410_nand_calculate_ecc;
822 chip->ecc.correct = s3c2410_nand_correct_data;
823 chip->ecc.mode = NAND_ECC_HW;
825 switch (info->cpu_type) {
827 chip->ecc.hwctl = s3c2410_nand_enable_hwecc;
828 chip->ecc.calculate = s3c2410_nand_calculate_ecc;
832 chip->ecc.hwctl = s3c2412_nand_enable_hwecc;
833 chip->ecc.calculate = s3c2412_nand_calculate_ecc;
837 chip->ecc.hwctl = s3c2440_nand_enable_hwecc;
838 chip->ecc.calculate = s3c2440_nand_calculate_ecc;
843 chip->ecc.mode = NAND_ECC_SOFT;
846 if (set->ecc_layout != NULL)
847 chip->ecc.layout = set->ecc_layout;
849 if (set->disable_ecc)
850 chip->ecc.mode = NAND_ECC_NONE;
852 switch (chip->ecc.mode) {
854 dev_info(info->device, "NAND ECC disabled\n");
857 dev_info(info->device, "NAND soft ECC\n");
860 dev_info(info->device, "NAND hardware ECC\n");
863 dev_info(info->device, "NAND ECC UNKNOWN\n");
867 /* If you use u-boot BBT creation code, specifying this flag will
868 * let the kernel fish out the BBT from the NAND, and also skip the
869 * full NAND scan that can take 1/2s or so. Little things... */
871 chip->options |= NAND_USE_FLASH_BBT | NAND_SKIP_BBTSCAN;
875 * s3c2410_nand_update_chip - post probe update
876 * @info: The controller instance.
877 * @nmtd: The driver version of the MTD instance.
879 * This routine is called after the chip probe has succesfully completed
880 * and the relevant per-chip information updated. This call ensure that
881 * we update the internal state accordingly.
883 * The internal state is currently limited to the ECC state information.
885 static void s3c2410_nand_update_chip(struct s3c2410_nand_info *info,
886 struct s3c2410_nand_mtd *nmtd)
888 struct nand_chip *chip = &nmtd->chip;
890 dev_dbg(info->device, "chip %p => page shift %d\n",
891 chip, chip->page_shift);
893 if (chip->ecc.mode != NAND_ECC_HW)
896 /* change the behaviour depending on wether we are using
897 * the large or small page nand device */
899 if (chip->page_shift > 10) {
900 chip->ecc.size = 256;
903 chip->ecc.size = 512;
905 chip->ecc.layout = &nand_hw_eccoob;
909 /* s3c24xx_nand_probe
911 * called by device layer when it finds a device matching
912 * one our driver can handled. This code checks to see if
913 * it can allocate all necessary resources then calls the
914 * nand layer to look for devices
916 static int s3c24xx_nand_probe(struct platform_device *pdev)
918 struct s3c2410_platform_nand *plat = to_nand_plat(pdev);
919 enum s3c_cpu_type cpu_type;
920 struct s3c2410_nand_info *info;
921 struct s3c2410_nand_mtd *nmtd;
922 struct s3c2410_nand_set *sets;
923 struct resource *res;
929 cpu_type = platform_get_device_id(pdev)->driver_data;
931 pr_debug("s3c2410_nand_probe(%p)\n", pdev);
933 info = kmalloc(sizeof(*info), GFP_KERNEL);
935 dev_err(&pdev->dev, "no memory for flash info\n");
940 memset(info, 0, sizeof(*info));
941 platform_set_drvdata(pdev, info);
943 spin_lock_init(&info->controller.lock);
944 init_waitqueue_head(&info->controller.wq);
946 /* get the clock source and enable it */
948 info->clk = clk_get(&pdev->dev, "nand");
949 if (IS_ERR(info->clk)) {
950 dev_err(&pdev->dev, "failed to get clock\n");
955 clk_enable(info->clk);
957 /* allocate and map the resource */
959 /* currently we assume we have the one resource */
960 res = pdev->resource;
961 size = res->end - res->start + 1;
963 info->area = request_mem_region(res->start, size, pdev->name);
965 if (info->area == NULL) {
966 dev_err(&pdev->dev, "cannot reserve register region\n");
971 info->device = &pdev->dev;
972 info->platform = plat;
973 info->regs = ioremap(res->start, size);
974 info->cpu_type = cpu_type;
976 if (info->regs == NULL) {
977 dev_err(&pdev->dev, "cannot reserve register region\n");
982 dev_dbg(&pdev->dev, "mapped registers at %p\n", info->regs);
984 /* initialise the hardware */
986 err = s3c2410_nand_inithw(info);
990 sets = (plat != NULL) ? plat->sets : NULL;
991 nr_sets = (plat != NULL) ? plat->nr_sets : 1;
993 info->mtd_count = nr_sets;
995 /* allocate our information */
997 size = nr_sets * sizeof(*info->mtds);
998 info->mtds = kmalloc(size, GFP_KERNEL);
999 if (info->mtds == NULL) {
1000 dev_err(&pdev->dev, "failed to allocate mtd storage\n");
1005 memset(info->mtds, 0, size);
1007 /* initialise all possible chips */
1011 for (setno = 0; setno < nr_sets; setno++, nmtd++) {
1012 pr_debug("initialising set %d (%p, info %p)\n", setno, nmtd, info);
1014 s3c2410_nand_init_chip(info, nmtd, sets);
1016 nmtd->scan_res = nand_scan_ident(&nmtd->mtd,
1017 (sets) ? sets->nr_chips : 1);
1019 if (nmtd->scan_res == 0) {
1020 s3c2410_nand_update_chip(info, nmtd);
1021 nand_scan_tail(&nmtd->mtd);
1022 s3c2410_nand_add_partition(info, nmtd, sets);
1029 err = s3c2410_nand_cpufreq_register(info);
1031 dev_err(&pdev->dev, "failed to init cpufreq support\n");
1035 if (allow_clk_stop(info)) {
1036 dev_info(&pdev->dev, "clock idle support enabled\n");
1037 clk_disable(info->clk);
1040 pr_debug("initialised ok\n");
1044 s3c24xx_nand_remove(pdev);
1054 static int s3c24xx_nand_suspend(struct platform_device *dev, pm_message_t pm)
1056 struct s3c2410_nand_info *info = platform_get_drvdata(dev);
1059 info->save_sel = readl(info->sel_reg);
1061 /* For the moment, we must ensure nFCE is high during
1062 * the time we are suspended. This really should be
1063 * handled by suspending the MTDs we are using, but
1064 * that is currently not the case. */
1066 writel(info->save_sel | info->sel_bit, info->sel_reg);
1068 if (!allow_clk_stop(info))
1069 clk_disable(info->clk);
1075 static int s3c24xx_nand_resume(struct platform_device *dev)
1077 struct s3c2410_nand_info *info = platform_get_drvdata(dev);
1081 clk_enable(info->clk);
1082 s3c2410_nand_inithw(info);
1084 /* Restore the state of the nFCE line. */
1086 sel = readl(info->sel_reg);
1087 sel &= ~info->sel_bit;
1088 sel |= info->save_sel & info->sel_bit;
1089 writel(sel, info->sel_reg);
1091 if (allow_clk_stop(info))
1092 clk_disable(info->clk);
1099 #define s3c24xx_nand_suspend NULL
1100 #define s3c24xx_nand_resume NULL
1103 /* driver device registration */
1105 static struct platform_device_id s3c24xx_driver_ids[] = {
1107 .name = "s3c2410-nand",
1108 .driver_data = TYPE_S3C2410,
1110 .name = "s3c2440-nand",
1111 .driver_data = TYPE_S3C2440,
1113 .name = "s3c2412-nand",
1114 .driver_data = TYPE_S3C2412,
1119 MODULE_DEVICE_TABLE(platform, s3c24xx_driver_ids);
1121 static struct platform_driver s3c24xx_nand_driver = {
1122 .probe = s3c24xx_nand_probe,
1123 .remove = s3c24xx_nand_remove,
1124 .suspend = s3c24xx_nand_suspend,
1125 .resume = s3c24xx_nand_resume,
1126 .id_table = s3c24xx_driver_ids,
1128 .name = "s3c24xx-nand",
1129 .owner = THIS_MODULE,
1133 static int __init s3c2410_nand_init(void)
1135 printk("S3C24XX NAND Driver, (c) 2004 Simtec Electronics\n");
1137 return platform_driver_register(&s3c24xx_nand_driver);
1140 static void __exit s3c2410_nand_exit(void)
1142 platform_driver_unregister(&s3c24xx_nand_driver);
1145 module_init(s3c2410_nand_init);
1146 module_exit(s3c2410_nand_exit);
1148 MODULE_LICENSE("GPL");
1149 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1150 MODULE_DESCRIPTION("S3C24XX MTD NAND driver");