1 // SPDX-License-Identifier: GPL-2.0-or-later
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
10 #define pr_fmt(fmt) "nand-s3c2410: " fmt
12 #ifdef CONFIG_MTD_NAND_S3C2410_DEBUG
16 #include <linux/module.h>
17 #include <linux/types.h>
18 #include <linux/kernel.h>
19 #include <linux/string.h>
21 #include <linux/ioport.h>
22 #include <linux/platform_device.h>
23 #include <linux/delay.h>
24 #include <linux/err.h>
25 #include <linux/slab.h>
26 #include <linux/clk.h>
27 #include <linux/cpufreq.h>
30 #include <linux/mtd/mtd.h>
31 #include <linux/mtd/rawnand.h>
32 #include <linux/mtd/partitions.h>
34 #include <linux/platform_data/mtd-nand-s3c2410.h>
36 #define S3C2410_NFREG(x) (x)
38 #define S3C2410_NFCONF S3C2410_NFREG(0x00)
39 #define S3C2410_NFCMD S3C2410_NFREG(0x04)
40 #define S3C2410_NFADDR S3C2410_NFREG(0x08)
41 #define S3C2410_NFDATA S3C2410_NFREG(0x0C)
42 #define S3C2410_NFSTAT S3C2410_NFREG(0x10)
43 #define S3C2410_NFECC S3C2410_NFREG(0x14)
44 #define S3C2440_NFCONT S3C2410_NFREG(0x04)
45 #define S3C2440_NFCMD S3C2410_NFREG(0x08)
46 #define S3C2440_NFADDR S3C2410_NFREG(0x0C)
47 #define S3C2440_NFDATA S3C2410_NFREG(0x10)
48 #define S3C2440_NFSTAT S3C2410_NFREG(0x20)
49 #define S3C2440_NFMECC0 S3C2410_NFREG(0x2C)
50 #define S3C2412_NFSTAT S3C2410_NFREG(0x28)
51 #define S3C2412_NFMECC0 S3C2410_NFREG(0x34)
52 #define S3C2410_NFCONF_EN (1<<15)
53 #define S3C2410_NFCONF_INITECC (1<<12)
54 #define S3C2410_NFCONF_nFCE (1<<11)
55 #define S3C2410_NFCONF_TACLS(x) ((x)<<8)
56 #define S3C2410_NFCONF_TWRPH0(x) ((x)<<4)
57 #define S3C2410_NFCONF_TWRPH1(x) ((x)<<0)
58 #define S3C2410_NFSTAT_BUSY (1<<0)
59 #define S3C2440_NFCONF_TACLS(x) ((x)<<12)
60 #define S3C2440_NFCONF_TWRPH0(x) ((x)<<8)
61 #define S3C2440_NFCONF_TWRPH1(x) ((x)<<4)
62 #define S3C2440_NFCONT_INITECC (1<<4)
63 #define S3C2440_NFCONT_nFCE (1<<1)
64 #define S3C2440_NFCONT_ENABLE (1<<0)
65 #define S3C2440_NFSTAT_READY (1<<0)
66 #define S3C2412_NFCONF_NANDBOOT (1<<31)
67 #define S3C2412_NFCONT_INIT_MAIN_ECC (1<<5)
68 #define S3C2412_NFCONT_nFCE0 (1<<1)
69 #define S3C2412_NFSTAT_READY (1<<0)
71 /* new oob placement block for use with hardware ecc generation
73 static int s3c2410_ooblayout_ecc(struct mtd_info *mtd, int section,
74 struct mtd_oob_region *oobregion)
79 oobregion->offset = 0;
80 oobregion->length = 3;
85 static int s3c2410_ooblayout_free(struct mtd_info *mtd, int section,
86 struct mtd_oob_region *oobregion)
91 oobregion->offset = 8;
92 oobregion->length = 8;
97 static const struct mtd_ooblayout_ops s3c2410_ooblayout_ops = {
98 .ecc = s3c2410_ooblayout_ecc,
99 .free = s3c2410_ooblayout_free,
102 /* controller and mtd information */
104 struct s3c2410_nand_info;
107 * struct s3c2410_nand_mtd - driver MTD structure
108 * @chip: The NAND chip information.
109 * @set: The platform information supplied for this set of NAND chips.
110 * @info: Link back to the hardware information.
112 struct s3c2410_nand_mtd {
113 struct nand_chip chip;
114 struct s3c2410_nand_set *set;
115 struct s3c2410_nand_info *info;
124 enum s3c_nand_clk_state {
130 /* overview of the s3c2410 nand state */
133 * struct s3c2410_nand_info - NAND controller state.
134 * @controller: Base controller structure.
135 * @mtds: An array of MTD instances on this controller.
136 * @platform: The platform data for this board.
137 * @device: The platform device we bound to.
138 * @clk: The clock resource for this controller.
139 * @regs: The area mapped for the hardware registers.
140 * @sel_reg: Pointer to the register controlling the NAND selection.
141 * @sel_bit: The bit in @sel_reg to select the NAND chip.
142 * @mtd_count: The number of MTDs created from this controller.
143 * @save_sel: The contents of @sel_reg to be saved over suspend.
144 * @clk_rate: The clock rate from @clk.
145 * @clk_state: The current clock state.
146 * @cpu_type: The exact type of this controller.
148 struct s3c2410_nand_info {
150 struct nand_controller controller;
151 struct s3c2410_nand_mtd *mtds;
152 struct s3c2410_platform_nand *platform;
155 struct device *device;
158 void __iomem *sel_reg;
161 unsigned long save_sel;
162 unsigned long clk_rate;
163 enum s3c_nand_clk_state clk_state;
165 enum s3c_cpu_type cpu_type;
168 struct s3c24XX_nand_devtype_data {
169 enum s3c_cpu_type type;
172 static const struct s3c24XX_nand_devtype_data s3c2410_nand_devtype_data = {
173 .type = TYPE_S3C2410,
176 static const struct s3c24XX_nand_devtype_data s3c2412_nand_devtype_data = {
177 .type = TYPE_S3C2412,
180 static const struct s3c24XX_nand_devtype_data s3c2440_nand_devtype_data = {
181 .type = TYPE_S3C2440,
184 /* conversion functions */
186 static struct s3c2410_nand_mtd *s3c2410_nand_mtd_toours(struct mtd_info *mtd)
188 return container_of(mtd_to_nand(mtd), struct s3c2410_nand_mtd,
192 static struct s3c2410_nand_info *s3c2410_nand_mtd_toinfo(struct mtd_info *mtd)
194 return s3c2410_nand_mtd_toours(mtd)->info;
197 static struct s3c2410_nand_info *to_nand_info(struct platform_device *dev)
199 return platform_get_drvdata(dev);
202 static struct s3c2410_platform_nand *to_nand_plat(struct platform_device *dev)
204 return dev_get_platdata(&dev->dev);
207 static inline int allow_clk_suspend(struct s3c2410_nand_info *info)
209 #ifdef CONFIG_MTD_NAND_S3C2410_CLKSTOP
217 * s3c2410_nand_clk_set_state - Enable, disable or suspend NAND clock.
218 * @info: The controller instance.
219 * @new_state: State to which clock should be set.
221 static void s3c2410_nand_clk_set_state(struct s3c2410_nand_info *info,
222 enum s3c_nand_clk_state new_state)
224 if (!allow_clk_suspend(info) && new_state == CLOCK_SUSPEND)
227 if (info->clk_state == CLOCK_ENABLE) {
228 if (new_state != CLOCK_ENABLE)
229 clk_disable_unprepare(info->clk);
231 if (new_state == CLOCK_ENABLE)
232 clk_prepare_enable(info->clk);
235 info->clk_state = new_state;
238 /* timing calculations */
240 #define NS_IN_KHZ 1000000
243 * s3c_nand_calc_rate - calculate timing data.
244 * @wanted: The cycle time in nanoseconds.
245 * @clk: The clock rate in kHz.
246 * @max: The maximum divider value.
248 * Calculate the timing value from the given parameters.
250 static int s3c_nand_calc_rate(int wanted, unsigned long clk, int max)
254 result = DIV_ROUND_UP((wanted * clk), NS_IN_KHZ);
256 pr_debug("result %d from %ld, %d\n", result, clk, wanted);
259 pr_err("%d ns is too big for current clock rate %ld\n",
270 #define to_ns(ticks, clk) (((ticks) * NS_IN_KHZ) / (unsigned int)(clk))
272 /* controller setup */
275 * s3c2410_nand_setrate - setup controller timing information.
276 * @info: The controller instance.
278 * Given the information supplied by the platform, calculate and set
279 * the necessary timing registers in the hardware to generate the
280 * necessary timing cycles to the hardware.
282 static int s3c2410_nand_setrate(struct s3c2410_nand_info *info)
284 struct s3c2410_platform_nand *plat = info->platform;
285 int tacls_max = (info->cpu_type == TYPE_S3C2412) ? 8 : 4;
286 int tacls, twrph0, twrph1;
287 unsigned long clkrate = clk_get_rate(info->clk);
288 unsigned long set, cfg, mask;
291 /* calculate the timing information for the controller */
293 info->clk_rate = clkrate;
294 clkrate /= 1000; /* turn clock into kHz for ease of use */
297 tacls = s3c_nand_calc_rate(plat->tacls, clkrate, tacls_max);
298 twrph0 = s3c_nand_calc_rate(plat->twrph0, clkrate, 8);
299 twrph1 = s3c_nand_calc_rate(plat->twrph1, clkrate, 8);
301 /* default timings */
307 if (tacls < 0 || twrph0 < 0 || twrph1 < 0) {
308 dev_err(info->device, "cannot get suitable timings\n");
312 dev_info(info->device, "Tacls=%d, %dns Twrph0=%d %dns, Twrph1=%d %dns\n",
313 tacls, to_ns(tacls, clkrate), twrph0, to_ns(twrph0, clkrate),
314 twrph1, to_ns(twrph1, clkrate));
316 switch (info->cpu_type) {
318 mask = (S3C2410_NFCONF_TACLS(3) |
319 S3C2410_NFCONF_TWRPH0(7) |
320 S3C2410_NFCONF_TWRPH1(7));
321 set = S3C2410_NFCONF_EN;
322 set |= S3C2410_NFCONF_TACLS(tacls - 1);
323 set |= S3C2410_NFCONF_TWRPH0(twrph0 - 1);
324 set |= S3C2410_NFCONF_TWRPH1(twrph1 - 1);
329 mask = (S3C2440_NFCONF_TACLS(tacls_max - 1) |
330 S3C2440_NFCONF_TWRPH0(7) |
331 S3C2440_NFCONF_TWRPH1(7));
333 set = S3C2440_NFCONF_TACLS(tacls - 1);
334 set |= S3C2440_NFCONF_TWRPH0(twrph0 - 1);
335 set |= S3C2440_NFCONF_TWRPH1(twrph1 - 1);
342 local_irq_save(flags);
344 cfg = readl(info->regs + S3C2410_NFCONF);
347 writel(cfg, info->regs + S3C2410_NFCONF);
349 local_irq_restore(flags);
351 dev_dbg(info->device, "NF_CONF is 0x%lx\n", cfg);
357 * s3c2410_nand_inithw - basic hardware initialisation
358 * @info: The hardware state.
360 * Do the basic initialisation of the hardware, using s3c2410_nand_setrate()
361 * to setup the hardware access speeds and set the controller to be enabled.
363 static int s3c2410_nand_inithw(struct s3c2410_nand_info *info)
367 ret = s3c2410_nand_setrate(info);
371 switch (info->cpu_type) {
378 /* enable the controller and de-assert nFCE */
380 writel(S3C2440_NFCONT_ENABLE, info->regs + S3C2440_NFCONT);
387 * s3c2410_nand_select_chip - select the given nand chip
388 * @this: NAND chip object.
389 * @chip: The chip number.
391 * This is called by the MTD layer to either select a given chip for the
392 * @mtd instance, or to indicate that the access has finished and the
393 * chip can be de-selected.
395 * The routine ensures that the nFCE line is correctly setup, and any
396 * platform specific selection code is called to route nFCE to the specific
399 static void s3c2410_nand_select_chip(struct nand_chip *this, int chip)
401 struct s3c2410_nand_info *info;
402 struct s3c2410_nand_mtd *nmtd;
405 nmtd = nand_get_controller_data(this);
409 s3c2410_nand_clk_set_state(info, CLOCK_ENABLE);
411 cur = readl(info->sel_reg);
414 cur |= info->sel_bit;
416 if (nmtd->set != NULL && chip > nmtd->set->nr_chips) {
417 dev_err(info->device, "invalid chip %d\n", chip);
421 if (info->platform != NULL) {
422 if (info->platform->select_chip != NULL)
423 (info->platform->select_chip) (nmtd->set, chip);
426 cur &= ~info->sel_bit;
429 writel(cur, info->sel_reg);
432 s3c2410_nand_clk_set_state(info, CLOCK_SUSPEND);
435 /* s3c2410_nand_hwcontrol
437 * Issue command and address cycles to the chip
440 static void s3c2410_nand_hwcontrol(struct nand_chip *chip, int cmd,
443 struct mtd_info *mtd = nand_to_mtd(chip);
444 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
446 if (cmd == NAND_CMD_NONE)
450 writeb(cmd, info->regs + S3C2410_NFCMD);
452 writeb(cmd, info->regs + S3C2410_NFADDR);
455 /* command and control functions */
457 static void s3c2440_nand_hwcontrol(struct nand_chip *chip, int cmd,
460 struct mtd_info *mtd = nand_to_mtd(chip);
461 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
463 if (cmd == NAND_CMD_NONE)
467 writeb(cmd, info->regs + S3C2440_NFCMD);
469 writeb(cmd, info->regs + S3C2440_NFADDR);
472 /* s3c2410_nand_devready()
474 * returns 0 if the nand is busy, 1 if it is ready
477 static int s3c2410_nand_devready(struct nand_chip *chip)
479 struct mtd_info *mtd = nand_to_mtd(chip);
480 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
481 return readb(info->regs + S3C2410_NFSTAT) & S3C2410_NFSTAT_BUSY;
484 static int s3c2440_nand_devready(struct nand_chip *chip)
486 struct mtd_info *mtd = nand_to_mtd(chip);
487 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
488 return readb(info->regs + S3C2440_NFSTAT) & S3C2440_NFSTAT_READY;
491 static int s3c2412_nand_devready(struct nand_chip *chip)
493 struct mtd_info *mtd = nand_to_mtd(chip);
494 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
495 return readb(info->regs + S3C2412_NFSTAT) & S3C2412_NFSTAT_READY;
498 /* ECC handling functions */
500 static int s3c2410_nand_correct_data(struct nand_chip *chip, u_char *dat,
501 u_char *read_ecc, u_char *calc_ecc)
503 struct mtd_info *mtd = nand_to_mtd(chip);
504 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
505 unsigned int diff0, diff1, diff2;
506 unsigned int bit, byte;
508 pr_debug("%s(%p,%p,%p,%p)\n", __func__, mtd, dat, read_ecc, calc_ecc);
510 diff0 = read_ecc[0] ^ calc_ecc[0];
511 diff1 = read_ecc[1] ^ calc_ecc[1];
512 diff2 = read_ecc[2] ^ calc_ecc[2];
514 pr_debug("%s: rd %*phN calc %*phN diff %02x%02x%02x\n",
515 __func__, 3, read_ecc, 3, calc_ecc,
516 diff0, diff1, diff2);
518 if (diff0 == 0 && diff1 == 0 && diff2 == 0)
519 return 0; /* ECC is ok */
521 /* sometimes people do not think about using the ECC, so check
522 * to see if we have an 0xff,0xff,0xff read ECC and then ignore
523 * the error, on the assumption that this is an un-eccd page.
525 if (read_ecc[0] == 0xff && read_ecc[1] == 0xff && read_ecc[2] == 0xff
526 && info->platform->ignore_unset_ecc)
529 /* Can we correct this ECC (ie, one row and column change).
530 * Note, this is similar to the 256 error code on smartmedia */
532 if (((diff0 ^ (diff0 >> 1)) & 0x55) == 0x55 &&
533 ((diff1 ^ (diff1 >> 1)) & 0x55) == 0x55 &&
534 ((diff2 ^ (diff2 >> 1)) & 0x55) == 0x55) {
535 /* calculate the bit position of the error */
537 bit = ((diff2 >> 3) & 1) |
541 /* calculate the byte position of the error */
543 byte = ((diff2 << 7) & 0x100) |
544 ((diff1 << 0) & 0x80) |
545 ((diff1 << 1) & 0x40) |
546 ((diff1 << 2) & 0x20) |
547 ((diff1 << 3) & 0x10) |
548 ((diff0 >> 4) & 0x08) |
549 ((diff0 >> 3) & 0x04) |
550 ((diff0 >> 2) & 0x02) |
551 ((diff0 >> 1) & 0x01);
553 dev_dbg(info->device, "correcting error bit %d, byte %d\n",
556 dat[byte] ^= (1 << bit);
560 /* if there is only one bit difference in the ECC, then
561 * one of only a row or column parity has changed, which
562 * means the error is most probably in the ECC itself */
564 diff0 |= (diff1 << 8);
565 diff0 |= (diff2 << 16);
567 /* equal to "(diff0 & ~(1 << __ffs(diff0)))" */
568 if ((diff0 & (diff0 - 1)) == 0)
576 * These allow the s3c2410 and s3c2440 to use the controller's ECC
577 * generator block to ECC the data as it passes through]
580 static void s3c2410_nand_enable_hwecc(struct nand_chip *chip, int mode)
582 struct s3c2410_nand_info *info;
585 info = s3c2410_nand_mtd_toinfo(nand_to_mtd(chip));
586 ctrl = readl(info->regs + S3C2410_NFCONF);
587 ctrl |= S3C2410_NFCONF_INITECC;
588 writel(ctrl, info->regs + S3C2410_NFCONF);
591 static void s3c2412_nand_enable_hwecc(struct nand_chip *chip, int mode)
593 struct s3c2410_nand_info *info;
596 info = s3c2410_nand_mtd_toinfo(nand_to_mtd(chip));
597 ctrl = readl(info->regs + S3C2440_NFCONT);
598 writel(ctrl | S3C2412_NFCONT_INIT_MAIN_ECC,
599 info->regs + S3C2440_NFCONT);
602 static void s3c2440_nand_enable_hwecc(struct nand_chip *chip, int mode)
604 struct s3c2410_nand_info *info;
607 info = s3c2410_nand_mtd_toinfo(nand_to_mtd(chip));
608 ctrl = readl(info->regs + S3C2440_NFCONT);
609 writel(ctrl | S3C2440_NFCONT_INITECC, info->regs + S3C2440_NFCONT);
612 static int s3c2410_nand_calculate_ecc(struct nand_chip *chip,
613 const u_char *dat, u_char *ecc_code)
615 struct mtd_info *mtd = nand_to_mtd(chip);
616 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
618 ecc_code[0] = readb(info->regs + S3C2410_NFECC + 0);
619 ecc_code[1] = readb(info->regs + S3C2410_NFECC + 1);
620 ecc_code[2] = readb(info->regs + S3C2410_NFECC + 2);
622 pr_debug("%s: returning ecc %*phN\n", __func__, 3, ecc_code);
627 static int s3c2412_nand_calculate_ecc(struct nand_chip *chip,
628 const u_char *dat, u_char *ecc_code)
630 struct mtd_info *mtd = nand_to_mtd(chip);
631 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
632 unsigned long ecc = readl(info->regs + S3C2412_NFMECC0);
635 ecc_code[1] = ecc >> 8;
636 ecc_code[2] = ecc >> 16;
638 pr_debug("%s: returning ecc %*phN\n", __func__, 3, ecc_code);
643 static int s3c2440_nand_calculate_ecc(struct nand_chip *chip,
644 const u_char *dat, u_char *ecc_code)
646 struct mtd_info *mtd = nand_to_mtd(chip);
647 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
648 unsigned long ecc = readl(info->regs + S3C2440_NFMECC0);
651 ecc_code[1] = ecc >> 8;
652 ecc_code[2] = ecc >> 16;
654 pr_debug("%s: returning ecc %06lx\n", __func__, ecc & 0xffffff);
659 /* over-ride the standard functions for a little more speed. We can
660 * use read/write block to move the data buffers to/from the controller
663 static void s3c2410_nand_read_buf(struct nand_chip *this, u_char *buf, int len)
665 readsb(this->legacy.IO_ADDR_R, buf, len);
668 static void s3c2440_nand_read_buf(struct nand_chip *this, u_char *buf, int len)
670 struct mtd_info *mtd = nand_to_mtd(this);
671 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
673 readsl(info->regs + S3C2440_NFDATA, buf, len >> 2);
675 /* cleanup if we've got less than a word to do */
679 for (; len & 3; len--)
680 *buf++ = readb(info->regs + S3C2440_NFDATA);
684 static void s3c2410_nand_write_buf(struct nand_chip *this, const u_char *buf,
687 writesb(this->legacy.IO_ADDR_W, buf, len);
690 static void s3c2440_nand_write_buf(struct nand_chip *this, const u_char *buf,
693 struct mtd_info *mtd = nand_to_mtd(this);
694 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
696 writesl(info->regs + S3C2440_NFDATA, buf, len >> 2);
698 /* cleanup any fractional write */
702 for (; len & 3; len--, buf++)
703 writeb(*buf, info->regs + S3C2440_NFDATA);
707 /* device management functions */
709 static void s3c24xx_nand_remove(struct platform_device *pdev)
711 struct s3c2410_nand_info *info = to_nand_info(pdev);
716 /* Release all our mtds and their partitions, then go through
717 * freeing the resources used
720 if (info->mtds != NULL) {
721 struct s3c2410_nand_mtd *ptr = info->mtds;
724 for (mtdno = 0; mtdno < info->mtd_count; mtdno++, ptr++) {
725 pr_debug("releasing mtd %d (%p)\n", mtdno, ptr);
726 WARN_ON(mtd_device_unregister(nand_to_mtd(&ptr->chip)));
727 nand_cleanup(&ptr->chip);
731 /* free the common resources */
733 if (!IS_ERR(info->clk))
734 s3c2410_nand_clk_set_state(info, CLOCK_DISABLE);
737 static int s3c2410_nand_add_partition(struct s3c2410_nand_info *info,
738 struct s3c2410_nand_mtd *mtd,
739 struct s3c2410_nand_set *set)
742 struct mtd_info *mtdinfo = nand_to_mtd(&mtd->chip);
744 mtdinfo->name = set->name;
746 return mtd_device_register(mtdinfo, set->partitions,
753 static int s3c2410_nand_setup_interface(struct nand_chip *chip, int csline,
754 const struct nand_interface_config *conf)
756 struct mtd_info *mtd = nand_to_mtd(chip);
757 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
758 struct s3c2410_platform_nand *pdata = info->platform;
759 const struct nand_sdr_timings *timings;
762 timings = nand_get_sdr_timings(conf);
766 tacls = timings->tCLS_min - timings->tWP_min;
770 pdata->tacls = DIV_ROUND_UP(tacls, 1000);
771 pdata->twrph0 = DIV_ROUND_UP(timings->tWP_min, 1000);
772 pdata->twrph1 = DIV_ROUND_UP(timings->tCLH_min, 1000);
774 return s3c2410_nand_setrate(info);
778 * s3c2410_nand_init_chip - initialise a single instance of an chip
779 * @info: The base NAND controller the chip is on.
780 * @nmtd: The new controller MTD instance to fill in.
781 * @set: The information passed from the board specific platform data.
783 * Initialise the given @nmtd from the information in @info and @set. This
784 * readies the structure for use with the MTD layer functions by ensuring
785 * all pointers are setup and the necessary control routines selected.
787 static void s3c2410_nand_init_chip(struct s3c2410_nand_info *info,
788 struct s3c2410_nand_mtd *nmtd,
789 struct s3c2410_nand_set *set)
791 struct device_node *np = info->device->of_node;
792 struct nand_chip *chip = &nmtd->chip;
793 void __iomem *regs = info->regs;
795 nand_set_flash_node(chip, set->of_node);
797 chip->legacy.write_buf = s3c2410_nand_write_buf;
798 chip->legacy.read_buf = s3c2410_nand_read_buf;
799 chip->legacy.select_chip = s3c2410_nand_select_chip;
800 chip->legacy.chip_delay = 50;
801 nand_set_controller_data(chip, nmtd);
802 chip->options = set->options;
803 chip->controller = &info->controller;
806 * let's keep behavior unchanged for legacy boards booting via pdata and
807 * auto-detect timings only when booting with a device tree.
810 chip->options |= NAND_KEEP_TIMINGS;
812 switch (info->cpu_type) {
814 chip->legacy.IO_ADDR_W = regs + S3C2410_NFDATA;
815 info->sel_reg = regs + S3C2410_NFCONF;
816 info->sel_bit = S3C2410_NFCONF_nFCE;
817 chip->legacy.cmd_ctrl = s3c2410_nand_hwcontrol;
818 chip->legacy.dev_ready = s3c2410_nand_devready;
822 chip->legacy.IO_ADDR_W = regs + S3C2440_NFDATA;
823 info->sel_reg = regs + S3C2440_NFCONT;
824 info->sel_bit = S3C2440_NFCONT_nFCE;
825 chip->legacy.cmd_ctrl = s3c2440_nand_hwcontrol;
826 chip->legacy.dev_ready = s3c2440_nand_devready;
827 chip->legacy.read_buf = s3c2440_nand_read_buf;
828 chip->legacy.write_buf = s3c2440_nand_write_buf;
832 chip->legacy.IO_ADDR_W = regs + S3C2440_NFDATA;
833 info->sel_reg = regs + S3C2440_NFCONT;
834 info->sel_bit = S3C2412_NFCONT_nFCE0;
835 chip->legacy.cmd_ctrl = s3c2440_nand_hwcontrol;
836 chip->legacy.dev_ready = s3c2412_nand_devready;
838 if (readl(regs + S3C2410_NFCONF) & S3C2412_NFCONF_NANDBOOT)
839 dev_info(info->device, "System booted from NAND\n");
844 chip->legacy.IO_ADDR_R = chip->legacy.IO_ADDR_W;
849 chip->ecc.engine_type = info->platform->engine_type;
852 * If you use u-boot BBT creation code, specifying this flag will
853 * let the kernel fish out the BBT from the NAND.
856 chip->bbt_options |= NAND_BBT_USE_FLASH;
860 * s3c2410_nand_attach_chip - Init the ECC engine after NAND scan
861 * @chip: The NAND chip
863 * This hook is called by the core after the identification of the NAND chip,
864 * once the relevant per-chip information is up to date.. This call ensure that
865 * we update the internal state accordingly.
867 * The internal state is currently limited to the ECC state information.
869 static int s3c2410_nand_attach_chip(struct nand_chip *chip)
871 struct mtd_info *mtd = nand_to_mtd(chip);
872 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
874 switch (chip->ecc.engine_type) {
876 case NAND_ECC_ENGINE_TYPE_NONE:
877 dev_info(info->device, "ECC disabled\n");
880 case NAND_ECC_ENGINE_TYPE_SOFT:
882 * This driver expects Hamming based ECC when engine_type is set
883 * to NAND_ECC_ENGINE_TYPE_SOFT. Force ecc.algo to
884 * NAND_ECC_ALGO_HAMMING to avoid adding an extra ecc_algo field
885 * to s3c2410_platform_nand.
887 chip->ecc.algo = NAND_ECC_ALGO_HAMMING;
888 dev_info(info->device, "soft ECC\n");
891 case NAND_ECC_ENGINE_TYPE_ON_HOST:
892 chip->ecc.calculate = s3c2410_nand_calculate_ecc;
893 chip->ecc.correct = s3c2410_nand_correct_data;
894 chip->ecc.strength = 1;
896 switch (info->cpu_type) {
898 chip->ecc.hwctl = s3c2410_nand_enable_hwecc;
899 chip->ecc.calculate = s3c2410_nand_calculate_ecc;
903 chip->ecc.hwctl = s3c2412_nand_enable_hwecc;
904 chip->ecc.calculate = s3c2412_nand_calculate_ecc;
908 chip->ecc.hwctl = s3c2440_nand_enable_hwecc;
909 chip->ecc.calculate = s3c2440_nand_calculate_ecc;
913 dev_dbg(info->device, "chip %p => page shift %d\n",
914 chip, chip->page_shift);
916 /* change the behaviour depending on whether we are using
917 * the large or small page nand device */
918 if (chip->page_shift > 10) {
919 chip->ecc.size = 256;
922 chip->ecc.size = 512;
924 mtd_set_ooblayout(nand_to_mtd(chip),
925 &s3c2410_ooblayout_ops);
928 dev_info(info->device, "hardware ECC\n");
932 dev_err(info->device, "invalid ECC mode!\n");
936 if (chip->bbt_options & NAND_BBT_USE_FLASH)
937 chip->options |= NAND_SKIP_BBTSCAN;
942 static const struct nand_controller_ops s3c24xx_nand_controller_ops = {
943 .attach_chip = s3c2410_nand_attach_chip,
944 .setup_interface = s3c2410_nand_setup_interface,
947 static const struct of_device_id s3c24xx_nand_dt_ids[] = {
949 .compatible = "samsung,s3c2410-nand",
950 .data = &s3c2410_nand_devtype_data,
952 /* also compatible with s3c6400 */
953 .compatible = "samsung,s3c2412-nand",
954 .data = &s3c2412_nand_devtype_data,
956 .compatible = "samsung,s3c2440-nand",
957 .data = &s3c2440_nand_devtype_data,
961 MODULE_DEVICE_TABLE(of, s3c24xx_nand_dt_ids);
963 static int s3c24xx_nand_probe_dt(struct platform_device *pdev)
965 const struct s3c24XX_nand_devtype_data *devtype_data;
966 struct s3c2410_platform_nand *pdata;
967 struct s3c2410_nand_info *info = platform_get_drvdata(pdev);
968 struct device_node *np = pdev->dev.of_node, *child;
969 struct s3c2410_nand_set *sets;
971 devtype_data = of_device_get_match_data(&pdev->dev);
975 info->cpu_type = devtype_data->type;
977 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
981 pdev->dev.platform_data = pdata;
983 pdata->nr_sets = of_get_child_count(np);
987 sets = devm_kcalloc(&pdev->dev, pdata->nr_sets, sizeof(*sets),
994 for_each_available_child_of_node(np, child) {
995 sets->name = (char *)child->name;
996 sets->of_node = child;
1007 static int s3c24xx_nand_probe_pdata(struct platform_device *pdev)
1009 struct s3c2410_nand_info *info = platform_get_drvdata(pdev);
1011 info->cpu_type = platform_get_device_id(pdev)->driver_data;
1016 /* s3c24xx_nand_probe
1018 * called by device layer when it finds a device matching
1019 * one our driver can handled. This code checks to see if
1020 * it can allocate all necessary resources then calls the
1021 * nand layer to look for devices
1023 static int s3c24xx_nand_probe(struct platform_device *pdev)
1025 struct s3c2410_platform_nand *plat;
1026 struct s3c2410_nand_info *info;
1027 struct s3c2410_nand_mtd *nmtd;
1028 struct s3c2410_nand_set *sets;
1029 struct resource *res;
1035 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
1041 platform_set_drvdata(pdev, info);
1043 nand_controller_init(&info->controller);
1044 info->controller.ops = &s3c24xx_nand_controller_ops;
1046 /* get the clock source and enable it */
1048 info->clk = devm_clk_get(&pdev->dev, "nand");
1049 if (IS_ERR(info->clk)) {
1050 dev_err(&pdev->dev, "failed to get clock\n");
1055 s3c2410_nand_clk_set_state(info, CLOCK_ENABLE);
1057 if (pdev->dev.of_node)
1058 err = s3c24xx_nand_probe_dt(pdev);
1060 err = s3c24xx_nand_probe_pdata(pdev);
1065 plat = to_nand_plat(pdev);
1067 /* allocate and map the resource */
1069 /* currently we assume we have the one resource */
1070 res = pdev->resource;
1071 size = resource_size(res);
1073 info->device = &pdev->dev;
1074 info->platform = plat;
1076 info->regs = devm_ioremap_resource(&pdev->dev, res);
1077 if (IS_ERR(info->regs)) {
1078 err = PTR_ERR(info->regs);
1082 dev_dbg(&pdev->dev, "mapped registers at %p\n", info->regs);
1084 if (!plat->sets || plat->nr_sets < 1) {
1090 nr_sets = plat->nr_sets;
1092 info->mtd_count = nr_sets;
1094 /* allocate our information */
1096 size = nr_sets * sizeof(*info->mtds);
1097 info->mtds = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
1098 if (info->mtds == NULL) {
1103 /* initialise all possible chips */
1107 for (setno = 0; setno < nr_sets; setno++, nmtd++, sets++) {
1108 struct mtd_info *mtd = nand_to_mtd(&nmtd->chip);
1110 pr_debug("initialising set %d (%p, info %p)\n",
1113 mtd->dev.parent = &pdev->dev;
1114 s3c2410_nand_init_chip(info, nmtd, sets);
1116 err = nand_scan(&nmtd->chip, sets ? sets->nr_chips : 1);
1120 s3c2410_nand_add_partition(info, nmtd, sets);
1123 /* initialise the hardware */
1124 err = s3c2410_nand_inithw(info);
1128 if (allow_clk_suspend(info)) {
1129 dev_info(&pdev->dev, "clock idle support enabled\n");
1130 s3c2410_nand_clk_set_state(info, CLOCK_SUSPEND);
1136 s3c24xx_nand_remove(pdev);
1146 static int s3c24xx_nand_suspend(struct platform_device *dev, pm_message_t pm)
1148 struct s3c2410_nand_info *info = platform_get_drvdata(dev);
1151 info->save_sel = readl(info->sel_reg);
1153 /* For the moment, we must ensure nFCE is high during
1154 * the time we are suspended. This really should be
1155 * handled by suspending the MTDs we are using, but
1156 * that is currently not the case. */
1158 writel(info->save_sel | info->sel_bit, info->sel_reg);
1160 s3c2410_nand_clk_set_state(info, CLOCK_DISABLE);
1166 static int s3c24xx_nand_resume(struct platform_device *dev)
1168 struct s3c2410_nand_info *info = platform_get_drvdata(dev);
1172 s3c2410_nand_clk_set_state(info, CLOCK_ENABLE);
1173 s3c2410_nand_inithw(info);
1175 /* Restore the state of the nFCE line. */
1177 sel = readl(info->sel_reg);
1178 sel &= ~info->sel_bit;
1179 sel |= info->save_sel & info->sel_bit;
1180 writel(sel, info->sel_reg);
1182 s3c2410_nand_clk_set_state(info, CLOCK_SUSPEND);
1189 #define s3c24xx_nand_suspend NULL
1190 #define s3c24xx_nand_resume NULL
1193 /* driver device registration */
1195 static const struct platform_device_id s3c24xx_driver_ids[] = {
1197 .name = "s3c2410-nand",
1198 .driver_data = TYPE_S3C2410,
1200 .name = "s3c2440-nand",
1201 .driver_data = TYPE_S3C2440,
1203 .name = "s3c2412-nand",
1204 .driver_data = TYPE_S3C2412,
1206 .name = "s3c6400-nand",
1207 .driver_data = TYPE_S3C2412, /* compatible with 2412 */
1212 MODULE_DEVICE_TABLE(platform, s3c24xx_driver_ids);
1214 static struct platform_driver s3c24xx_nand_driver = {
1215 .probe = s3c24xx_nand_probe,
1216 .remove_new = s3c24xx_nand_remove,
1217 .suspend = s3c24xx_nand_suspend,
1218 .resume = s3c24xx_nand_resume,
1219 .id_table = s3c24xx_driver_ids,
1221 .name = "s3c24xx-nand",
1222 .of_match_table = s3c24xx_nand_dt_ids,
1226 module_platform_driver(s3c24xx_nand_driver);
1228 MODULE_LICENSE("GPL");
1229 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1230 MODULE_DESCRIPTION("S3C24XX MTD NAND driver");