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 #define pr_fmt(fmt) "nand-s3c2410: " fmt
26 #ifdef CONFIG_MTD_NAND_S3C2410_DEBUG
30 #include <linux/module.h>
31 #include <linux/types.h>
32 #include <linux/kernel.h>
33 #include <linux/string.h>
35 #include <linux/ioport.h>
36 #include <linux/platform_device.h>
37 #include <linux/delay.h>
38 #include <linux/err.h>
39 #include <linux/slab.h>
40 #include <linux/clk.h>
41 #include <linux/cpufreq.h>
43 #include <linux/of_device.h>
45 #include <linux/mtd/mtd.h>
46 #include <linux/mtd/nand.h>
47 #include <linux/mtd/nand_ecc.h>
48 #include <linux/mtd/partitions.h>
50 #include <linux/platform_data/mtd-nand-s3c2410.h>
52 #define S3C2410_NFREG(x) (x)
54 #define S3C2410_NFCONF S3C2410_NFREG(0x00)
55 #define S3C2410_NFCMD S3C2410_NFREG(0x04)
56 #define S3C2410_NFADDR S3C2410_NFREG(0x08)
57 #define S3C2410_NFDATA S3C2410_NFREG(0x0C)
58 #define S3C2410_NFSTAT S3C2410_NFREG(0x10)
59 #define S3C2410_NFECC S3C2410_NFREG(0x14)
60 #define S3C2440_NFCONT S3C2410_NFREG(0x04)
61 #define S3C2440_NFCMD S3C2410_NFREG(0x08)
62 #define S3C2440_NFADDR S3C2410_NFREG(0x0C)
63 #define S3C2440_NFDATA S3C2410_NFREG(0x10)
64 #define S3C2440_NFSTAT S3C2410_NFREG(0x20)
65 #define S3C2440_NFMECC0 S3C2410_NFREG(0x2C)
66 #define S3C2412_NFSTAT S3C2410_NFREG(0x28)
67 #define S3C2412_NFMECC0 S3C2410_NFREG(0x34)
68 #define S3C2410_NFCONF_EN (1<<15)
69 #define S3C2410_NFCONF_INITECC (1<<12)
70 #define S3C2410_NFCONF_nFCE (1<<11)
71 #define S3C2410_NFCONF_TACLS(x) ((x)<<8)
72 #define S3C2410_NFCONF_TWRPH0(x) ((x)<<4)
73 #define S3C2410_NFCONF_TWRPH1(x) ((x)<<0)
74 #define S3C2410_NFSTAT_BUSY (1<<0)
75 #define S3C2440_NFCONF_TACLS(x) ((x)<<12)
76 #define S3C2440_NFCONF_TWRPH0(x) ((x)<<8)
77 #define S3C2440_NFCONF_TWRPH1(x) ((x)<<4)
78 #define S3C2440_NFCONT_INITECC (1<<4)
79 #define S3C2440_NFCONT_nFCE (1<<1)
80 #define S3C2440_NFCONT_ENABLE (1<<0)
81 #define S3C2440_NFSTAT_READY (1<<0)
82 #define S3C2412_NFCONF_NANDBOOT (1<<31)
83 #define S3C2412_NFCONT_INIT_MAIN_ECC (1<<5)
84 #define S3C2412_NFCONT_nFCE0 (1<<1)
85 #define S3C2412_NFSTAT_READY (1<<0)
87 /* new oob placement block for use with hardware ecc generation
89 static int s3c2410_ooblayout_ecc(struct mtd_info
*mtd
, int section
,
90 struct mtd_oob_region
*oobregion
)
95 oobregion
->offset
= 0;
96 oobregion
->length
= 3;
101 static int s3c2410_ooblayout_free(struct mtd_info
*mtd
, int section
,
102 struct mtd_oob_region
*oobregion
)
107 oobregion
->offset
= 8;
108 oobregion
->length
= 8;
113 static const struct mtd_ooblayout_ops s3c2410_ooblayout_ops
= {
114 .ecc
= s3c2410_ooblayout_ecc
,
115 .free
= s3c2410_ooblayout_free
,
118 /* controller and mtd information */
120 struct s3c2410_nand_info
;
123 * struct s3c2410_nand_mtd - driver MTD structure
124 * @mtd: The MTD instance to pass to the MTD layer.
125 * @chip: The NAND chip information.
126 * @set: The platform information supplied for this set of NAND chips.
127 * @info: Link back to the hardware information.
128 * @scan_res: The result from calling nand_scan_ident().
130 struct s3c2410_nand_mtd
{
131 struct nand_chip chip
;
132 struct s3c2410_nand_set
*set
;
133 struct s3c2410_nand_info
*info
;
143 enum s3c_nand_clk_state
{
149 /* overview of the s3c2410 nand state */
152 * struct s3c2410_nand_info - NAND controller state.
153 * @mtds: An array of MTD instances on this controoler.
154 * @platform: The platform data for this board.
155 * @device: The platform device we bound to.
156 * @clk: The clock resource for this controller.
157 * @regs: The area mapped for the hardware registers.
158 * @sel_reg: Pointer to the register controlling the NAND selection.
159 * @sel_bit: The bit in @sel_reg to select the NAND chip.
160 * @mtd_count: The number of MTDs created from this controller.
161 * @save_sel: The contents of @sel_reg to be saved over suspend.
162 * @clk_rate: The clock rate from @clk.
163 * @clk_state: The current clock state.
164 * @cpu_type: The exact type of this controller.
166 struct s3c2410_nand_info
{
168 struct nand_hw_control controller
;
169 struct s3c2410_nand_mtd
*mtds
;
170 struct s3c2410_platform_nand
*platform
;
173 struct device
*device
;
176 void __iomem
*sel_reg
;
179 unsigned long save_sel
;
180 unsigned long clk_rate
;
181 enum s3c_nand_clk_state clk_state
;
183 enum s3c_cpu_type cpu_type
;
185 #ifdef CONFIG_ARM_S3C24XX_CPUFREQ
186 struct notifier_block freq_transition
;
190 struct s3c24XX_nand_devtype_data
{
191 enum s3c_cpu_type type
;
194 static const struct s3c24XX_nand_devtype_data s3c2410_nand_devtype_data
= {
195 .type
= TYPE_S3C2410
,
198 static const struct s3c24XX_nand_devtype_data s3c2412_nand_devtype_data
= {
199 .type
= TYPE_S3C2412
,
202 static const struct s3c24XX_nand_devtype_data s3c2440_nand_devtype_data
= {
203 .type
= TYPE_S3C2440
,
206 /* conversion functions */
208 static struct s3c2410_nand_mtd
*s3c2410_nand_mtd_toours(struct mtd_info
*mtd
)
210 return container_of(mtd_to_nand(mtd
), struct s3c2410_nand_mtd
,
214 static struct s3c2410_nand_info
*s3c2410_nand_mtd_toinfo(struct mtd_info
*mtd
)
216 return s3c2410_nand_mtd_toours(mtd
)->info
;
219 static struct s3c2410_nand_info
*to_nand_info(struct platform_device
*dev
)
221 return platform_get_drvdata(dev
);
224 static struct s3c2410_platform_nand
*to_nand_plat(struct platform_device
*dev
)
226 return dev_get_platdata(&dev
->dev
);
229 static inline int allow_clk_suspend(struct s3c2410_nand_info
*info
)
231 #ifdef CONFIG_MTD_NAND_S3C2410_CLKSTOP
239 * s3c2410_nand_clk_set_state - Enable, disable or suspend NAND clock.
240 * @info: The controller instance.
241 * @new_state: State to which clock should be set.
243 static void s3c2410_nand_clk_set_state(struct s3c2410_nand_info
*info
,
244 enum s3c_nand_clk_state new_state
)
246 if (!allow_clk_suspend(info
) && new_state
== CLOCK_SUSPEND
)
249 if (info
->clk_state
== CLOCK_ENABLE
) {
250 if (new_state
!= CLOCK_ENABLE
)
251 clk_disable_unprepare(info
->clk
);
253 if (new_state
== CLOCK_ENABLE
)
254 clk_prepare_enable(info
->clk
);
257 info
->clk_state
= new_state
;
260 /* timing calculations */
262 #define NS_IN_KHZ 1000000
265 * s3c_nand_calc_rate - calculate timing data.
266 * @wanted: The cycle time in nanoseconds.
267 * @clk: The clock rate in kHz.
268 * @max: The maximum divider value.
270 * Calculate the timing value from the given parameters.
272 static int s3c_nand_calc_rate(int wanted
, unsigned long clk
, int max
)
276 result
= DIV_ROUND_UP((wanted
* clk
), NS_IN_KHZ
);
278 pr_debug("result %d from %ld, %d\n", result
, clk
, wanted
);
281 pr_err("%d ns is too big for current clock rate %ld\n",
292 #define to_ns(ticks, clk) (((ticks) * NS_IN_KHZ) / (unsigned int)(clk))
294 /* controller setup */
297 * s3c2410_nand_setrate - setup controller timing information.
298 * @info: The controller instance.
300 * Given the information supplied by the platform, calculate and set
301 * the necessary timing registers in the hardware to generate the
302 * necessary timing cycles to the hardware.
304 static int s3c2410_nand_setrate(struct s3c2410_nand_info
*info
)
306 struct s3c2410_platform_nand
*plat
= info
->platform
;
307 int tacls_max
= (info
->cpu_type
== TYPE_S3C2412
) ? 8 : 4;
308 int tacls
, twrph0
, twrph1
;
309 unsigned long clkrate
= clk_get_rate(info
->clk
);
310 unsigned long uninitialized_var(set
), cfg
, uninitialized_var(mask
);
313 /* calculate the timing information for the controller */
315 info
->clk_rate
= clkrate
;
316 clkrate
/= 1000; /* turn clock into kHz for ease of use */
319 tacls
= s3c_nand_calc_rate(plat
->tacls
, clkrate
, tacls_max
);
320 twrph0
= s3c_nand_calc_rate(plat
->twrph0
, clkrate
, 8);
321 twrph1
= s3c_nand_calc_rate(plat
->twrph1
, clkrate
, 8);
323 /* default timings */
329 if (tacls
< 0 || twrph0
< 0 || twrph1
< 0) {
330 dev_err(info
->device
, "cannot get suitable timings\n");
334 dev_info(info
->device
, "Tacls=%d, %dns Twrph0=%d %dns, Twrph1=%d %dns\n",
335 tacls
, to_ns(tacls
, clkrate
), twrph0
, to_ns(twrph0
, clkrate
),
336 twrph1
, to_ns(twrph1
, clkrate
));
338 switch (info
->cpu_type
) {
340 mask
= (S3C2410_NFCONF_TACLS(3) |
341 S3C2410_NFCONF_TWRPH0(7) |
342 S3C2410_NFCONF_TWRPH1(7));
343 set
= S3C2410_NFCONF_EN
;
344 set
|= S3C2410_NFCONF_TACLS(tacls
- 1);
345 set
|= S3C2410_NFCONF_TWRPH0(twrph0
- 1);
346 set
|= S3C2410_NFCONF_TWRPH1(twrph1
- 1);
351 mask
= (S3C2440_NFCONF_TACLS(tacls_max
- 1) |
352 S3C2440_NFCONF_TWRPH0(7) |
353 S3C2440_NFCONF_TWRPH1(7));
355 set
= S3C2440_NFCONF_TACLS(tacls
- 1);
356 set
|= S3C2440_NFCONF_TWRPH0(twrph0
- 1);
357 set
|= S3C2440_NFCONF_TWRPH1(twrph1
- 1);
364 local_irq_save(flags
);
366 cfg
= readl(info
->regs
+ S3C2410_NFCONF
);
369 writel(cfg
, info
->regs
+ S3C2410_NFCONF
);
371 local_irq_restore(flags
);
373 dev_dbg(info
->device
, "NF_CONF is 0x%lx\n", cfg
);
379 * s3c2410_nand_inithw - basic hardware initialisation
380 * @info: The hardware state.
382 * Do the basic initialisation of the hardware, using s3c2410_nand_setrate()
383 * to setup the hardware access speeds and set the controller to be enabled.
385 static int s3c2410_nand_inithw(struct s3c2410_nand_info
*info
)
389 ret
= s3c2410_nand_setrate(info
);
393 switch (info
->cpu_type
) {
400 /* enable the controller and de-assert nFCE */
402 writel(S3C2440_NFCONT_ENABLE
, info
->regs
+ S3C2440_NFCONT
);
409 * s3c2410_nand_select_chip - select the given nand chip
410 * @mtd: The MTD instance for this chip.
411 * @chip: The chip number.
413 * This is called by the MTD layer to either select a given chip for the
414 * @mtd instance, or to indicate that the access has finished and the
415 * chip can be de-selected.
417 * The routine ensures that the nFCE line is correctly setup, and any
418 * platform specific selection code is called to route nFCE to the specific
421 static void s3c2410_nand_select_chip(struct mtd_info
*mtd
, int chip
)
423 struct s3c2410_nand_info
*info
;
424 struct s3c2410_nand_mtd
*nmtd
;
425 struct nand_chip
*this = mtd_to_nand(mtd
);
428 nmtd
= nand_get_controller_data(this);
432 s3c2410_nand_clk_set_state(info
, CLOCK_ENABLE
);
434 cur
= readl(info
->sel_reg
);
437 cur
|= info
->sel_bit
;
439 if (nmtd
->set
!= NULL
&& chip
> nmtd
->set
->nr_chips
) {
440 dev_err(info
->device
, "invalid chip %d\n", chip
);
444 if (info
->platform
!= NULL
) {
445 if (info
->platform
->select_chip
!= NULL
)
446 (info
->platform
->select_chip
) (nmtd
->set
, chip
);
449 cur
&= ~info
->sel_bit
;
452 writel(cur
, info
->sel_reg
);
455 s3c2410_nand_clk_set_state(info
, CLOCK_SUSPEND
);
458 /* s3c2410_nand_hwcontrol
460 * Issue command and address cycles to the chip
463 static void s3c2410_nand_hwcontrol(struct mtd_info
*mtd
, int cmd
,
466 struct s3c2410_nand_info
*info
= s3c2410_nand_mtd_toinfo(mtd
);
468 if (cmd
== NAND_CMD_NONE
)
472 writeb(cmd
, info
->regs
+ S3C2410_NFCMD
);
474 writeb(cmd
, info
->regs
+ S3C2410_NFADDR
);
477 /* command and control functions */
479 static void s3c2440_nand_hwcontrol(struct mtd_info
*mtd
, int cmd
,
482 struct s3c2410_nand_info
*info
= s3c2410_nand_mtd_toinfo(mtd
);
484 if (cmd
== NAND_CMD_NONE
)
488 writeb(cmd
, info
->regs
+ S3C2440_NFCMD
);
490 writeb(cmd
, info
->regs
+ S3C2440_NFADDR
);
493 /* s3c2410_nand_devready()
495 * returns 0 if the nand is busy, 1 if it is ready
498 static int s3c2410_nand_devready(struct mtd_info
*mtd
)
500 struct s3c2410_nand_info
*info
= s3c2410_nand_mtd_toinfo(mtd
);
501 return readb(info
->regs
+ S3C2410_NFSTAT
) & S3C2410_NFSTAT_BUSY
;
504 static int s3c2440_nand_devready(struct mtd_info
*mtd
)
506 struct s3c2410_nand_info
*info
= s3c2410_nand_mtd_toinfo(mtd
);
507 return readb(info
->regs
+ S3C2440_NFSTAT
) & S3C2440_NFSTAT_READY
;
510 static int s3c2412_nand_devready(struct mtd_info
*mtd
)
512 struct s3c2410_nand_info
*info
= s3c2410_nand_mtd_toinfo(mtd
);
513 return readb(info
->regs
+ S3C2412_NFSTAT
) & S3C2412_NFSTAT_READY
;
516 /* ECC handling functions */
518 static int s3c2410_nand_correct_data(struct mtd_info
*mtd
, u_char
*dat
,
519 u_char
*read_ecc
, u_char
*calc_ecc
)
521 struct s3c2410_nand_info
*info
= s3c2410_nand_mtd_toinfo(mtd
);
522 unsigned int diff0
, diff1
, diff2
;
523 unsigned int bit
, byte
;
525 pr_debug("%s(%p,%p,%p,%p)\n", __func__
, mtd
, dat
, read_ecc
, calc_ecc
);
527 diff0
= read_ecc
[0] ^ calc_ecc
[0];
528 diff1
= read_ecc
[1] ^ calc_ecc
[1];
529 diff2
= read_ecc
[2] ^ calc_ecc
[2];
531 pr_debug("%s: rd %*phN calc %*phN diff %02x%02x%02x\n",
532 __func__
, 3, read_ecc
, 3, calc_ecc
,
533 diff0
, diff1
, diff2
);
535 if (diff0
== 0 && diff1
== 0 && diff2
== 0)
536 return 0; /* ECC is ok */
538 /* sometimes people do not think about using the ECC, so check
539 * to see if we have an 0xff,0xff,0xff read ECC and then ignore
540 * the error, on the assumption that this is an un-eccd page.
542 if (read_ecc
[0] == 0xff && read_ecc
[1] == 0xff && read_ecc
[2] == 0xff
543 && info
->platform
->ignore_unset_ecc
)
546 /* Can we correct this ECC (ie, one row and column change).
547 * Note, this is similar to the 256 error code on smartmedia */
549 if (((diff0
^ (diff0
>> 1)) & 0x55) == 0x55 &&
550 ((diff1
^ (diff1
>> 1)) & 0x55) == 0x55 &&
551 ((diff2
^ (diff2
>> 1)) & 0x55) == 0x55) {
552 /* calculate the bit position of the error */
554 bit
= ((diff2
>> 3) & 1) |
558 /* calculate the byte position of the error */
560 byte
= ((diff2
<< 7) & 0x100) |
561 ((diff1
<< 0) & 0x80) |
562 ((diff1
<< 1) & 0x40) |
563 ((diff1
<< 2) & 0x20) |
564 ((diff1
<< 3) & 0x10) |
565 ((diff0
>> 4) & 0x08) |
566 ((diff0
>> 3) & 0x04) |
567 ((diff0
>> 2) & 0x02) |
568 ((diff0
>> 1) & 0x01);
570 dev_dbg(info
->device
, "correcting error bit %d, byte %d\n",
573 dat
[byte
] ^= (1 << bit
);
577 /* if there is only one bit difference in the ECC, then
578 * one of only a row or column parity has changed, which
579 * means the error is most probably in the ECC itself */
581 diff0
|= (diff1
<< 8);
582 diff0
|= (diff2
<< 16);
584 /* equal to "(diff0 & ~(1 << __ffs(diff0)))" */
585 if ((diff0
& (diff0
- 1)) == 0)
593 * These allow the s3c2410 and s3c2440 to use the controller's ECC
594 * generator block to ECC the data as it passes through]
597 static void s3c2410_nand_enable_hwecc(struct mtd_info
*mtd
, int mode
)
599 struct s3c2410_nand_info
*info
= s3c2410_nand_mtd_toinfo(mtd
);
602 ctrl
= readl(info
->regs
+ S3C2410_NFCONF
);
603 ctrl
|= S3C2410_NFCONF_INITECC
;
604 writel(ctrl
, info
->regs
+ S3C2410_NFCONF
);
607 static void s3c2412_nand_enable_hwecc(struct mtd_info
*mtd
, int mode
)
609 struct s3c2410_nand_info
*info
= s3c2410_nand_mtd_toinfo(mtd
);
612 ctrl
= readl(info
->regs
+ S3C2440_NFCONT
);
613 writel(ctrl
| S3C2412_NFCONT_INIT_MAIN_ECC
,
614 info
->regs
+ S3C2440_NFCONT
);
617 static void s3c2440_nand_enable_hwecc(struct mtd_info
*mtd
, int mode
)
619 struct s3c2410_nand_info
*info
= s3c2410_nand_mtd_toinfo(mtd
);
622 ctrl
= readl(info
->regs
+ S3C2440_NFCONT
);
623 writel(ctrl
| S3C2440_NFCONT_INITECC
, info
->regs
+ S3C2440_NFCONT
);
626 static int s3c2410_nand_calculate_ecc(struct mtd_info
*mtd
, const u_char
*dat
,
629 struct s3c2410_nand_info
*info
= s3c2410_nand_mtd_toinfo(mtd
);
631 ecc_code
[0] = readb(info
->regs
+ S3C2410_NFECC
+ 0);
632 ecc_code
[1] = readb(info
->regs
+ S3C2410_NFECC
+ 1);
633 ecc_code
[2] = readb(info
->regs
+ S3C2410_NFECC
+ 2);
635 pr_debug("%s: returning ecc %*phN\n", __func__
, 3, ecc_code
);
640 static int s3c2412_nand_calculate_ecc(struct mtd_info
*mtd
, const u_char
*dat
,
643 struct s3c2410_nand_info
*info
= s3c2410_nand_mtd_toinfo(mtd
);
644 unsigned long ecc
= readl(info
->regs
+ S3C2412_NFMECC0
);
647 ecc_code
[1] = ecc
>> 8;
648 ecc_code
[2] = ecc
>> 16;
650 pr_debug("%s: returning ecc %*phN\n", __func__
, 3, ecc_code
);
655 static int s3c2440_nand_calculate_ecc(struct mtd_info
*mtd
, const u_char
*dat
,
658 struct s3c2410_nand_info
*info
= s3c2410_nand_mtd_toinfo(mtd
);
659 unsigned long ecc
= readl(info
->regs
+ S3C2440_NFMECC0
);
662 ecc_code
[1] = ecc
>> 8;
663 ecc_code
[2] = ecc
>> 16;
665 pr_debug("%s: returning ecc %06lx\n", __func__
, ecc
& 0xffffff);
670 /* over-ride the standard functions for a little more speed. We can
671 * use read/write block to move the data buffers to/from the controller
674 static void s3c2410_nand_read_buf(struct mtd_info
*mtd
, u_char
*buf
, int len
)
676 struct nand_chip
*this = mtd_to_nand(mtd
);
677 readsb(this->IO_ADDR_R
, buf
, len
);
680 static void s3c2440_nand_read_buf(struct mtd_info
*mtd
, u_char
*buf
, int len
)
682 struct s3c2410_nand_info
*info
= s3c2410_nand_mtd_toinfo(mtd
);
684 readsl(info
->regs
+ S3C2440_NFDATA
, buf
, len
>> 2);
686 /* cleanup if we've got less than a word to do */
690 for (; len
& 3; len
--)
691 *buf
++ = readb(info
->regs
+ S3C2440_NFDATA
);
695 static void s3c2410_nand_write_buf(struct mtd_info
*mtd
, const u_char
*buf
,
698 struct nand_chip
*this = mtd_to_nand(mtd
);
699 writesb(this->IO_ADDR_W
, buf
, len
);
702 static void s3c2440_nand_write_buf(struct mtd_info
*mtd
, const u_char
*buf
,
705 struct s3c2410_nand_info
*info
= s3c2410_nand_mtd_toinfo(mtd
);
707 writesl(info
->regs
+ S3C2440_NFDATA
, buf
, len
>> 2);
709 /* cleanup any fractional write */
713 for (; len
& 3; len
--, buf
++)
714 writeb(*buf
, info
->regs
+ S3C2440_NFDATA
);
718 /* cpufreq driver support */
720 #ifdef CONFIG_ARM_S3C24XX_CPUFREQ
722 static int s3c2410_nand_cpufreq_transition(struct notifier_block
*nb
,
723 unsigned long val
, void *data
)
725 struct s3c2410_nand_info
*info
;
726 unsigned long newclk
;
728 info
= container_of(nb
, struct s3c2410_nand_info
, freq_transition
);
729 newclk
= clk_get_rate(info
->clk
);
731 if ((val
== CPUFREQ_POSTCHANGE
&& newclk
< info
->clk_rate
) ||
732 (val
== CPUFREQ_PRECHANGE
&& newclk
> info
->clk_rate
)) {
733 s3c2410_nand_setrate(info
);
739 static inline int s3c2410_nand_cpufreq_register(struct s3c2410_nand_info
*info
)
741 info
->freq_transition
.notifier_call
= s3c2410_nand_cpufreq_transition
;
743 return cpufreq_register_notifier(&info
->freq_transition
,
744 CPUFREQ_TRANSITION_NOTIFIER
);
748 s3c2410_nand_cpufreq_deregister(struct s3c2410_nand_info
*info
)
750 cpufreq_unregister_notifier(&info
->freq_transition
,
751 CPUFREQ_TRANSITION_NOTIFIER
);
755 static inline int s3c2410_nand_cpufreq_register(struct s3c2410_nand_info
*info
)
761 s3c2410_nand_cpufreq_deregister(struct s3c2410_nand_info
*info
)
766 /* device management functions */
768 static int s3c24xx_nand_remove(struct platform_device
*pdev
)
770 struct s3c2410_nand_info
*info
= to_nand_info(pdev
);
775 s3c2410_nand_cpufreq_deregister(info
);
777 /* Release all our mtds and their partitions, then go through
778 * freeing the resources used
781 if (info
->mtds
!= NULL
) {
782 struct s3c2410_nand_mtd
*ptr
= info
->mtds
;
785 for (mtdno
= 0; mtdno
< info
->mtd_count
; mtdno
++, ptr
++) {
786 pr_debug("releasing mtd %d (%p)\n", mtdno
, ptr
);
787 nand_release(nand_to_mtd(&ptr
->chip
));
791 /* free the common resources */
793 if (!IS_ERR(info
->clk
))
794 s3c2410_nand_clk_set_state(info
, CLOCK_DISABLE
);
799 static int s3c2410_nand_add_partition(struct s3c2410_nand_info
*info
,
800 struct s3c2410_nand_mtd
*mtd
,
801 struct s3c2410_nand_set
*set
)
804 struct mtd_info
*mtdinfo
= nand_to_mtd(&mtd
->chip
);
806 mtdinfo
->name
= set
->name
;
808 return mtd_device_parse_register(mtdinfo
, NULL
, NULL
,
809 set
->partitions
, set
->nr_partitions
);
815 static int s3c2410_nand_setup_data_interface(struct mtd_info
*mtd
,
816 const struct nand_data_interface
*conf
,
819 struct s3c2410_nand_info
*info
= s3c2410_nand_mtd_toinfo(mtd
);
820 struct s3c2410_platform_nand
*pdata
= info
->platform
;
821 const struct nand_sdr_timings
*timings
;
824 timings
= nand_get_sdr_timings(conf
);
828 tacls
= timings
->tCLS_min
- timings
->tWP_min
;
832 pdata
->tacls
= DIV_ROUND_UP(tacls
, 1000);
833 pdata
->twrph0
= DIV_ROUND_UP(timings
->tWP_min
, 1000);
834 pdata
->twrph1
= DIV_ROUND_UP(timings
->tCLH_min
, 1000);
836 return s3c2410_nand_setrate(info
);
840 * s3c2410_nand_init_chip - initialise a single instance of an chip
841 * @info: The base NAND controller the chip is on.
842 * @nmtd: The new controller MTD instance to fill in.
843 * @set: The information passed from the board specific platform data.
845 * Initialise the given @nmtd from the information in @info and @set. This
846 * readies the structure for use with the MTD layer functions by ensuring
847 * all pointers are setup and the necessary control routines selected.
849 static void s3c2410_nand_init_chip(struct s3c2410_nand_info
*info
,
850 struct s3c2410_nand_mtd
*nmtd
,
851 struct s3c2410_nand_set
*set
)
853 struct device_node
*np
= info
->device
->of_node
;
854 struct nand_chip
*chip
= &nmtd
->chip
;
855 void __iomem
*regs
= info
->regs
;
857 nand_set_flash_node(chip
, set
->of_node
);
859 chip
->write_buf
= s3c2410_nand_write_buf
;
860 chip
->read_buf
= s3c2410_nand_read_buf
;
861 chip
->select_chip
= s3c2410_nand_select_chip
;
862 chip
->chip_delay
= 50;
863 nand_set_controller_data(chip
, nmtd
);
864 chip
->options
= set
->options
;
865 chip
->controller
= &info
->controller
;
868 * let's keep behavior unchanged for legacy boards booting via pdata and
869 * auto-detect timings only when booting with a device tree.
872 chip
->setup_data_interface
= s3c2410_nand_setup_data_interface
;
874 switch (info
->cpu_type
) {
876 chip
->IO_ADDR_W
= regs
+ S3C2410_NFDATA
;
877 info
->sel_reg
= regs
+ S3C2410_NFCONF
;
878 info
->sel_bit
= S3C2410_NFCONF_nFCE
;
879 chip
->cmd_ctrl
= s3c2410_nand_hwcontrol
;
880 chip
->dev_ready
= s3c2410_nand_devready
;
884 chip
->IO_ADDR_W
= regs
+ S3C2440_NFDATA
;
885 info
->sel_reg
= regs
+ S3C2440_NFCONT
;
886 info
->sel_bit
= S3C2440_NFCONT_nFCE
;
887 chip
->cmd_ctrl
= s3c2440_nand_hwcontrol
;
888 chip
->dev_ready
= s3c2440_nand_devready
;
889 chip
->read_buf
= s3c2440_nand_read_buf
;
890 chip
->write_buf
= s3c2440_nand_write_buf
;
894 chip
->IO_ADDR_W
= regs
+ S3C2440_NFDATA
;
895 info
->sel_reg
= regs
+ S3C2440_NFCONT
;
896 info
->sel_bit
= S3C2412_NFCONT_nFCE0
;
897 chip
->cmd_ctrl
= s3c2440_nand_hwcontrol
;
898 chip
->dev_ready
= s3c2412_nand_devready
;
900 if (readl(regs
+ S3C2410_NFCONF
) & S3C2412_NFCONF_NANDBOOT
)
901 dev_info(info
->device
, "System booted from NAND\n");
906 chip
->IO_ADDR_R
= chip
->IO_ADDR_W
;
911 chip
->ecc
.mode
= info
->platform
->ecc_mode
;
914 * If you use u-boot BBT creation code, specifying this flag will
915 * let the kernel fish out the BBT from the NAND.
918 chip
->bbt_options
|= NAND_BBT_USE_FLASH
;
922 * s3c2410_nand_update_chip - post probe update
923 * @info: The controller instance.
924 * @nmtd: The driver version of the MTD instance.
926 * This routine is called after the chip probe has successfully completed
927 * and the relevant per-chip information updated. This call ensure that
928 * we update the internal state accordingly.
930 * The internal state is currently limited to the ECC state information.
932 static int s3c2410_nand_update_chip(struct s3c2410_nand_info
*info
,
933 struct s3c2410_nand_mtd
*nmtd
)
935 struct nand_chip
*chip
= &nmtd
->chip
;
937 switch (chip
->ecc
.mode
) {
940 dev_info(info
->device
, "ECC disabled\n");
945 * This driver expects Hamming based ECC when ecc_mode is set
946 * to NAND_ECC_SOFT. Force ecc.algo to NAND_ECC_HAMMING to
947 * avoid adding an extra ecc_algo field to
948 * s3c2410_platform_nand.
950 chip
->ecc
.algo
= NAND_ECC_HAMMING
;
951 dev_info(info
->device
, "soft ECC\n");
955 chip
->ecc
.calculate
= s3c2410_nand_calculate_ecc
;
956 chip
->ecc
.correct
= s3c2410_nand_correct_data
;
957 chip
->ecc
.strength
= 1;
959 switch (info
->cpu_type
) {
961 chip
->ecc
.hwctl
= s3c2410_nand_enable_hwecc
;
962 chip
->ecc
.calculate
= s3c2410_nand_calculate_ecc
;
966 chip
->ecc
.hwctl
= s3c2412_nand_enable_hwecc
;
967 chip
->ecc
.calculate
= s3c2412_nand_calculate_ecc
;
971 chip
->ecc
.hwctl
= s3c2440_nand_enable_hwecc
;
972 chip
->ecc
.calculate
= s3c2440_nand_calculate_ecc
;
976 dev_dbg(info
->device
, "chip %p => page shift %d\n",
977 chip
, chip
->page_shift
);
979 /* change the behaviour depending on whether we are using
980 * the large or small page nand device */
981 if (chip
->page_shift
> 10) {
982 chip
->ecc
.size
= 256;
985 chip
->ecc
.size
= 512;
987 mtd_set_ooblayout(nand_to_mtd(chip
),
988 &s3c2410_ooblayout_ops
);
991 dev_info(info
->device
, "hardware ECC\n");
995 dev_err(info
->device
, "invalid ECC mode!\n");
999 if (chip
->bbt_options
& NAND_BBT_USE_FLASH
)
1000 chip
->options
|= NAND_SKIP_BBTSCAN
;
1005 static const struct of_device_id s3c24xx_nand_dt_ids
[] = {
1007 .compatible
= "samsung,s3c2410-nand",
1008 .data
= &s3c2410_nand_devtype_data
,
1010 /* also compatible with s3c6400 */
1011 .compatible
= "samsung,s3c2412-nand",
1012 .data
= &s3c2412_nand_devtype_data
,
1014 .compatible
= "samsung,s3c2440-nand",
1015 .data
= &s3c2440_nand_devtype_data
,
1019 MODULE_DEVICE_TABLE(of
, s3c24xx_nand_dt_ids
);
1021 static int s3c24xx_nand_probe_dt(struct platform_device
*pdev
)
1023 const struct s3c24XX_nand_devtype_data
*devtype_data
;
1024 struct s3c2410_platform_nand
*pdata
;
1025 struct s3c2410_nand_info
*info
= platform_get_drvdata(pdev
);
1026 struct device_node
*np
= pdev
->dev
.of_node
, *child
;
1027 struct s3c2410_nand_set
*sets
;
1029 devtype_data
= of_device_get_match_data(&pdev
->dev
);
1033 info
->cpu_type
= devtype_data
->type
;
1035 pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*pdata
), GFP_KERNEL
);
1039 pdev
->dev
.platform_data
= pdata
;
1041 pdata
->nr_sets
= of_get_child_count(np
);
1042 if (!pdata
->nr_sets
)
1045 sets
= devm_kzalloc(&pdev
->dev
, sizeof(*sets
) * pdata
->nr_sets
,
1052 for_each_available_child_of_node(np
, child
) {
1053 sets
->name
= (char *)child
->name
;
1054 sets
->of_node
= child
;
1065 static int s3c24xx_nand_probe_pdata(struct platform_device
*pdev
)
1067 struct s3c2410_nand_info
*info
= platform_get_drvdata(pdev
);
1069 info
->cpu_type
= platform_get_device_id(pdev
)->driver_data
;
1074 /* s3c24xx_nand_probe
1076 * called by device layer when it finds a device matching
1077 * one our driver can handled. This code checks to see if
1078 * it can allocate all necessary resources then calls the
1079 * nand layer to look for devices
1081 static int s3c24xx_nand_probe(struct platform_device
*pdev
)
1083 struct s3c2410_platform_nand
*plat
;
1084 struct s3c2410_nand_info
*info
;
1085 struct s3c2410_nand_mtd
*nmtd
;
1086 struct s3c2410_nand_set
*sets
;
1087 struct resource
*res
;
1093 info
= devm_kzalloc(&pdev
->dev
, sizeof(*info
), GFP_KERNEL
);
1099 platform_set_drvdata(pdev
, info
);
1101 nand_hw_control_init(&info
->controller
);
1103 /* get the clock source and enable it */
1105 info
->clk
= devm_clk_get(&pdev
->dev
, "nand");
1106 if (IS_ERR(info
->clk
)) {
1107 dev_err(&pdev
->dev
, "failed to get clock\n");
1112 s3c2410_nand_clk_set_state(info
, CLOCK_ENABLE
);
1114 if (pdev
->dev
.of_node
)
1115 err
= s3c24xx_nand_probe_dt(pdev
);
1117 err
= s3c24xx_nand_probe_pdata(pdev
);
1122 plat
= to_nand_plat(pdev
);
1124 /* allocate and map the resource */
1126 /* currently we assume we have the one resource */
1127 res
= pdev
->resource
;
1128 size
= resource_size(res
);
1130 info
->device
= &pdev
->dev
;
1131 info
->platform
= plat
;
1133 info
->regs
= devm_ioremap_resource(&pdev
->dev
, res
);
1134 if (IS_ERR(info
->regs
)) {
1135 err
= PTR_ERR(info
->regs
);
1139 dev_dbg(&pdev
->dev
, "mapped registers at %p\n", info
->regs
);
1141 sets
= (plat
!= NULL
) ? plat
->sets
: NULL
;
1142 nr_sets
= (plat
!= NULL
) ? plat
->nr_sets
: 1;
1144 info
->mtd_count
= nr_sets
;
1146 /* allocate our information */
1148 size
= nr_sets
* sizeof(*info
->mtds
);
1149 info
->mtds
= devm_kzalloc(&pdev
->dev
, size
, GFP_KERNEL
);
1150 if (info
->mtds
== NULL
) {
1155 /* initialise all possible chips */
1159 for (setno
= 0; setno
< nr_sets
; setno
++, nmtd
++) {
1160 struct mtd_info
*mtd
= nand_to_mtd(&nmtd
->chip
);
1162 pr_debug("initialising set %d (%p, info %p)\n",
1165 mtd
->dev
.parent
= &pdev
->dev
;
1166 s3c2410_nand_init_chip(info
, nmtd
, sets
);
1168 nmtd
->scan_res
= nand_scan_ident(mtd
,
1169 (sets
) ? sets
->nr_chips
: 1,
1172 if (nmtd
->scan_res
== 0) {
1173 err
= s3c2410_nand_update_chip(info
, nmtd
);
1176 nand_scan_tail(mtd
);
1177 s3c2410_nand_add_partition(info
, nmtd
, sets
);
1184 /* initialise the hardware */
1185 err
= s3c2410_nand_inithw(info
);
1189 err
= s3c2410_nand_cpufreq_register(info
);
1191 dev_err(&pdev
->dev
, "failed to init cpufreq support\n");
1195 if (allow_clk_suspend(info
)) {
1196 dev_info(&pdev
->dev
, "clock idle support enabled\n");
1197 s3c2410_nand_clk_set_state(info
, CLOCK_SUSPEND
);
1203 s3c24xx_nand_remove(pdev
);
1213 static int s3c24xx_nand_suspend(struct platform_device
*dev
, pm_message_t pm
)
1215 struct s3c2410_nand_info
*info
= platform_get_drvdata(dev
);
1218 info
->save_sel
= readl(info
->sel_reg
);
1220 /* For the moment, we must ensure nFCE is high during
1221 * the time we are suspended. This really should be
1222 * handled by suspending the MTDs we are using, but
1223 * that is currently not the case. */
1225 writel(info
->save_sel
| info
->sel_bit
, info
->sel_reg
);
1227 s3c2410_nand_clk_set_state(info
, CLOCK_DISABLE
);
1233 static int s3c24xx_nand_resume(struct platform_device
*dev
)
1235 struct s3c2410_nand_info
*info
= platform_get_drvdata(dev
);
1239 s3c2410_nand_clk_set_state(info
, CLOCK_ENABLE
);
1240 s3c2410_nand_inithw(info
);
1242 /* Restore the state of the nFCE line. */
1244 sel
= readl(info
->sel_reg
);
1245 sel
&= ~info
->sel_bit
;
1246 sel
|= info
->save_sel
& info
->sel_bit
;
1247 writel(sel
, info
->sel_reg
);
1249 s3c2410_nand_clk_set_state(info
, CLOCK_SUSPEND
);
1256 #define s3c24xx_nand_suspend NULL
1257 #define s3c24xx_nand_resume NULL
1260 /* driver device registration */
1262 static const struct platform_device_id s3c24xx_driver_ids
[] = {
1264 .name
= "s3c2410-nand",
1265 .driver_data
= TYPE_S3C2410
,
1267 .name
= "s3c2440-nand",
1268 .driver_data
= TYPE_S3C2440
,
1270 .name
= "s3c2412-nand",
1271 .driver_data
= TYPE_S3C2412
,
1273 .name
= "s3c6400-nand",
1274 .driver_data
= TYPE_S3C2412
, /* compatible with 2412 */
1279 MODULE_DEVICE_TABLE(platform
, s3c24xx_driver_ids
);
1281 static struct platform_driver s3c24xx_nand_driver
= {
1282 .probe
= s3c24xx_nand_probe
,
1283 .remove
= s3c24xx_nand_remove
,
1284 .suspend
= s3c24xx_nand_suspend
,
1285 .resume
= s3c24xx_nand_resume
,
1286 .id_table
= s3c24xx_driver_ids
,
1288 .name
= "s3c24xx-nand",
1289 .of_match_table
= s3c24xx_nand_dt_ids
,
1293 module_platform_driver(s3c24xx_nand_driver
);
1295 MODULE_LICENSE("GPL");
1296 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1297 MODULE_DESCRIPTION("S3C24XX MTD NAND driver");