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 const 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 enum s3c_nand_clk_state
{
105 /* overview of the s3c2410 nand state */
108 * struct s3c2410_nand_info - NAND controller state.
109 * @mtds: An array of MTD instances on this controoler.
110 * @platform: The platform data for this board.
111 * @device: The platform device we bound to.
112 * @area: The IO area resource that came from request_mem_region().
113 * @clk: The clock resource for this controller.
114 * @regs: The area mapped for the hardware registers described by @area.
115 * @sel_reg: Pointer to the register controlling the NAND selection.
116 * @sel_bit: The bit in @sel_reg to select the NAND chip.
117 * @mtd_count: The number of MTDs created from this controller.
118 * @save_sel: The contents of @sel_reg to be saved over suspend.
119 * @clk_rate: The clock rate from @clk.
120 * @clk_state: The current clock state.
121 * @cpu_type: The exact type of this controller.
123 struct s3c2410_nand_info
{
125 struct nand_hw_control controller
;
126 struct s3c2410_nand_mtd
*mtds
;
127 struct s3c2410_platform_nand
*platform
;
130 struct device
*device
;
131 struct resource
*area
;
134 void __iomem
*sel_reg
;
137 unsigned long save_sel
;
138 unsigned long clk_rate
;
139 enum s3c_nand_clk_state clk_state
;
141 enum s3c_cpu_type cpu_type
;
143 #ifdef CONFIG_CPU_FREQ
144 struct notifier_block freq_transition
;
148 /* conversion functions */
150 static struct s3c2410_nand_mtd
*s3c2410_nand_mtd_toours(struct mtd_info
*mtd
)
152 return container_of(mtd
, struct s3c2410_nand_mtd
, mtd
);
155 static struct s3c2410_nand_info
*s3c2410_nand_mtd_toinfo(struct mtd_info
*mtd
)
157 return s3c2410_nand_mtd_toours(mtd
)->info
;
160 static struct s3c2410_nand_info
*to_nand_info(struct platform_device
*dev
)
162 return platform_get_drvdata(dev
);
165 static struct s3c2410_platform_nand
*to_nand_plat(struct platform_device
*dev
)
167 return dev
->dev
.platform_data
;
170 static inline int allow_clk_suspend(struct s3c2410_nand_info
*info
)
176 * s3c2410_nand_clk_set_state - Enable, disable or suspend NAND clock.
177 * @info: The controller instance.
178 * @new_state: State to which clock should be set.
180 static void s3c2410_nand_clk_set_state(struct s3c2410_nand_info
*info
,
181 enum s3c_nand_clk_state new_state
)
183 if (!allow_clk_suspend(info
) && new_state
== CLOCK_SUSPEND
)
186 if (info
->clk_state
== CLOCK_ENABLE
) {
187 if (new_state
!= CLOCK_ENABLE
)
188 clk_disable(info
->clk
);
190 if (new_state
== CLOCK_ENABLE
)
191 clk_enable(info
->clk
);
194 info
->clk_state
= new_state
;
197 /* timing calculations */
199 #define NS_IN_KHZ 1000000
202 * s3c_nand_calc_rate - calculate timing data.
203 * @wanted: The cycle time in nanoseconds.
204 * @clk: The clock rate in kHz.
205 * @max: The maximum divider value.
207 * Calculate the timing value from the given parameters.
209 static int s3c_nand_calc_rate(int wanted
, unsigned long clk
, int max
)
213 result
= DIV_ROUND_UP((wanted
* clk
), NS_IN_KHZ
);
215 pr_debug("result %d from %ld, %d\n", result
, clk
, wanted
);
218 printk("%d ns is too big for current clock rate %ld\n", wanted
, clk
);
228 #define to_ns(ticks,clk) (((ticks) * NS_IN_KHZ) / (unsigned int)(clk))
230 /* controller setup */
233 * s3c2410_nand_setrate - setup controller timing information.
234 * @info: The controller instance.
236 * Given the information supplied by the platform, calculate and set
237 * the necessary timing registers in the hardware to generate the
238 * necessary timing cycles to the hardware.
240 static int s3c2410_nand_setrate(struct s3c2410_nand_info
*info
)
242 struct s3c2410_platform_nand
*plat
= info
->platform
;
243 int tacls_max
= (info
->cpu_type
== TYPE_S3C2412
) ? 8 : 4;
244 int tacls
, twrph0
, twrph1
;
245 unsigned long clkrate
= clk_get_rate(info
->clk
);
246 unsigned long uninitialized_var(set
), cfg
, uninitialized_var(mask
);
249 /* calculate the timing information for the controller */
251 info
->clk_rate
= clkrate
;
252 clkrate
/= 1000; /* turn clock into kHz for ease of use */
255 tacls
= s3c_nand_calc_rate(plat
->tacls
, clkrate
, tacls_max
);
256 twrph0
= s3c_nand_calc_rate(plat
->twrph0
, clkrate
, 8);
257 twrph1
= s3c_nand_calc_rate(plat
->twrph1
, clkrate
, 8);
259 /* default timings */
265 if (tacls
< 0 || twrph0
< 0 || twrph1
< 0) {
266 dev_err(info
->device
, "cannot get suitable timings\n");
270 dev_info(info
->device
, "Tacls=%d, %dns Twrph0=%d %dns, Twrph1=%d %dns\n",
271 tacls
, to_ns(tacls
, clkrate
), twrph0
, to_ns(twrph0
, clkrate
), twrph1
, to_ns(twrph1
, clkrate
));
273 switch (info
->cpu_type
) {
275 mask
= (S3C2410_NFCONF_TACLS(3) |
276 S3C2410_NFCONF_TWRPH0(7) |
277 S3C2410_NFCONF_TWRPH1(7));
278 set
= S3C2410_NFCONF_EN
;
279 set
|= S3C2410_NFCONF_TACLS(tacls
- 1);
280 set
|= S3C2410_NFCONF_TWRPH0(twrph0
- 1);
281 set
|= S3C2410_NFCONF_TWRPH1(twrph1
- 1);
286 mask
= (S3C2440_NFCONF_TACLS(tacls_max
- 1) |
287 S3C2440_NFCONF_TWRPH0(7) |
288 S3C2440_NFCONF_TWRPH1(7));
290 set
= S3C2440_NFCONF_TACLS(tacls
- 1);
291 set
|= S3C2440_NFCONF_TWRPH0(twrph0
- 1);
292 set
|= S3C2440_NFCONF_TWRPH1(twrph1
- 1);
299 local_irq_save(flags
);
301 cfg
= readl(info
->regs
+ S3C2410_NFCONF
);
304 writel(cfg
, info
->regs
+ S3C2410_NFCONF
);
306 local_irq_restore(flags
);
308 dev_dbg(info
->device
, "NF_CONF is 0x%lx\n", cfg
);
314 * s3c2410_nand_inithw - basic hardware initialisation
315 * @info: The hardware state.
317 * Do the basic initialisation of the hardware, using s3c2410_nand_setrate()
318 * to setup the hardware access speeds and set the controller to be enabled.
320 static int s3c2410_nand_inithw(struct s3c2410_nand_info
*info
)
324 ret
= s3c2410_nand_setrate(info
);
328 switch (info
->cpu_type
) {
335 /* enable the controller and de-assert nFCE */
337 writel(S3C2440_NFCONT_ENABLE
, info
->regs
+ S3C2440_NFCONT
);
344 * s3c2410_nand_select_chip - select the given nand chip
345 * @mtd: The MTD instance for this chip.
346 * @chip: The chip number.
348 * This is called by the MTD layer to either select a given chip for the
349 * @mtd instance, or to indicate that the access has finished and the
350 * chip can be de-selected.
352 * The routine ensures that the nFCE line is correctly setup, and any
353 * platform specific selection code is called to route nFCE to the specific
356 static void s3c2410_nand_select_chip(struct mtd_info
*mtd
, int chip
)
358 struct s3c2410_nand_info
*info
;
359 struct s3c2410_nand_mtd
*nmtd
;
360 struct nand_chip
*this = mtd
->priv
;
367 s3c2410_nand_clk_set_state(info
, CLOCK_ENABLE
);
369 cur
= readl(info
->sel_reg
);
372 cur
|= info
->sel_bit
;
374 if (nmtd
->set
!= NULL
&& chip
> nmtd
->set
->nr_chips
) {
375 dev_err(info
->device
, "invalid chip %d\n", chip
);
379 if (info
->platform
!= NULL
) {
380 if (info
->platform
->select_chip
!= NULL
)
381 (info
->platform
->select_chip
) (nmtd
->set
, chip
);
384 cur
&= ~info
->sel_bit
;
387 writel(cur
, info
->sel_reg
);
390 s3c2410_nand_clk_set_state(info
, CLOCK_SUSPEND
);
393 /* s3c2410_nand_hwcontrol
395 * Issue command and address cycles to the chip
398 static void s3c2410_nand_hwcontrol(struct mtd_info
*mtd
, int cmd
,
401 struct s3c2410_nand_info
*info
= s3c2410_nand_mtd_toinfo(mtd
);
403 if (cmd
== NAND_CMD_NONE
)
407 writeb(cmd
, info
->regs
+ S3C2410_NFCMD
);
409 writeb(cmd
, info
->regs
+ S3C2410_NFADDR
);
412 /* command and control functions */
414 static void s3c2440_nand_hwcontrol(struct mtd_info
*mtd
, int cmd
,
417 struct s3c2410_nand_info
*info
= s3c2410_nand_mtd_toinfo(mtd
);
419 if (cmd
== NAND_CMD_NONE
)
423 writeb(cmd
, info
->regs
+ S3C2440_NFCMD
);
425 writeb(cmd
, info
->regs
+ S3C2440_NFADDR
);
428 /* s3c2410_nand_devready()
430 * returns 0 if the nand is busy, 1 if it is ready
433 static int s3c2410_nand_devready(struct mtd_info
*mtd
)
435 struct s3c2410_nand_info
*info
= s3c2410_nand_mtd_toinfo(mtd
);
436 return readb(info
->regs
+ S3C2410_NFSTAT
) & S3C2410_NFSTAT_BUSY
;
439 static int s3c2440_nand_devready(struct mtd_info
*mtd
)
441 struct s3c2410_nand_info
*info
= s3c2410_nand_mtd_toinfo(mtd
);
442 return readb(info
->regs
+ S3C2440_NFSTAT
) & S3C2440_NFSTAT_READY
;
445 static int s3c2412_nand_devready(struct mtd_info
*mtd
)
447 struct s3c2410_nand_info
*info
= s3c2410_nand_mtd_toinfo(mtd
);
448 return readb(info
->regs
+ S3C2412_NFSTAT
) & S3C2412_NFSTAT_READY
;
451 /* ECC handling functions */
453 static int s3c2410_nand_correct_data(struct mtd_info
*mtd
, u_char
*dat
,
454 u_char
*read_ecc
, u_char
*calc_ecc
)
456 struct s3c2410_nand_info
*info
= s3c2410_nand_mtd_toinfo(mtd
);
457 unsigned int diff0
, diff1
, diff2
;
458 unsigned int bit
, byte
;
460 pr_debug("%s(%p,%p,%p,%p)\n", __func__
, mtd
, dat
, read_ecc
, calc_ecc
);
462 diff0
= read_ecc
[0] ^ calc_ecc
[0];
463 diff1
= read_ecc
[1] ^ calc_ecc
[1];
464 diff2
= read_ecc
[2] ^ calc_ecc
[2];
466 pr_debug("%s: rd %02x%02x%02x calc %02x%02x%02x diff %02x%02x%02x\n",
468 read_ecc
[0], read_ecc
[1], read_ecc
[2],
469 calc_ecc
[0], calc_ecc
[1], calc_ecc
[2],
470 diff0
, diff1
, diff2
);
472 if (diff0
== 0 && diff1
== 0 && diff2
== 0)
473 return 0; /* ECC is ok */
475 /* sometimes people do not think about using the ECC, so check
476 * to see if we have an 0xff,0xff,0xff read ECC and then ignore
477 * the error, on the assumption that this is an un-eccd page.
479 if (read_ecc
[0] == 0xff && read_ecc
[1] == 0xff && read_ecc
[2] == 0xff
480 && info
->platform
->ignore_unset_ecc
)
483 /* Can we correct this ECC (ie, one row and column change).
484 * Note, this is similar to the 256 error code on smartmedia */
486 if (((diff0
^ (diff0
>> 1)) & 0x55) == 0x55 &&
487 ((diff1
^ (diff1
>> 1)) & 0x55) == 0x55 &&
488 ((diff2
^ (diff2
>> 1)) & 0x55) == 0x55) {
489 /* calculate the bit position of the error */
491 bit
= ((diff2
>> 3) & 1) |
495 /* calculate the byte position of the error */
497 byte
= ((diff2
<< 7) & 0x100) |
498 ((diff1
<< 0) & 0x80) |
499 ((diff1
<< 1) & 0x40) |
500 ((diff1
<< 2) & 0x20) |
501 ((diff1
<< 3) & 0x10) |
502 ((diff0
>> 4) & 0x08) |
503 ((diff0
>> 3) & 0x04) |
504 ((diff0
>> 2) & 0x02) |
505 ((diff0
>> 1) & 0x01);
507 dev_dbg(info
->device
, "correcting error bit %d, byte %d\n",
510 dat
[byte
] ^= (1 << bit
);
514 /* if there is only one bit difference in the ECC, then
515 * one of only a row or column parity has changed, which
516 * means the error is most probably in the ECC itself */
518 diff0
|= (diff1
<< 8);
519 diff0
|= (diff2
<< 16);
521 if ((diff0
& ~(1<<fls(diff0
))) == 0)
529 * These allow the s3c2410 and s3c2440 to use the controller's ECC
530 * generator block to ECC the data as it passes through]
533 static void s3c2410_nand_enable_hwecc(struct mtd_info
*mtd
, int mode
)
535 struct s3c2410_nand_info
*info
= s3c2410_nand_mtd_toinfo(mtd
);
538 ctrl
= readl(info
->regs
+ S3C2410_NFCONF
);
539 ctrl
|= S3C2410_NFCONF_INITECC
;
540 writel(ctrl
, info
->regs
+ S3C2410_NFCONF
);
543 static void s3c2412_nand_enable_hwecc(struct mtd_info
*mtd
, int mode
)
545 struct s3c2410_nand_info
*info
= s3c2410_nand_mtd_toinfo(mtd
);
548 ctrl
= readl(info
->regs
+ S3C2440_NFCONT
);
549 writel(ctrl
| S3C2412_NFCONT_INIT_MAIN_ECC
, info
->regs
+ S3C2440_NFCONT
);
552 static void s3c2440_nand_enable_hwecc(struct mtd_info
*mtd
, int mode
)
554 struct s3c2410_nand_info
*info
= s3c2410_nand_mtd_toinfo(mtd
);
557 ctrl
= readl(info
->regs
+ S3C2440_NFCONT
);
558 writel(ctrl
| S3C2440_NFCONT_INITECC
, info
->regs
+ S3C2440_NFCONT
);
561 static int s3c2410_nand_calculate_ecc(struct mtd_info
*mtd
, const u_char
*dat
, u_char
*ecc_code
)
563 struct s3c2410_nand_info
*info
= s3c2410_nand_mtd_toinfo(mtd
);
565 ecc_code
[0] = readb(info
->regs
+ S3C2410_NFECC
+ 0);
566 ecc_code
[1] = readb(info
->regs
+ S3C2410_NFECC
+ 1);
567 ecc_code
[2] = readb(info
->regs
+ S3C2410_NFECC
+ 2);
569 pr_debug("%s: returning ecc %02x%02x%02x\n", __func__
,
570 ecc_code
[0], ecc_code
[1], ecc_code
[2]);
575 static int s3c2412_nand_calculate_ecc(struct mtd_info
*mtd
, const u_char
*dat
, u_char
*ecc_code
)
577 struct s3c2410_nand_info
*info
= s3c2410_nand_mtd_toinfo(mtd
);
578 unsigned long ecc
= readl(info
->regs
+ S3C2412_NFMECC0
);
581 ecc_code
[1] = ecc
>> 8;
582 ecc_code
[2] = ecc
>> 16;
584 pr_debug("calculate_ecc: returning ecc %02x,%02x,%02x\n", ecc_code
[0], ecc_code
[1], ecc_code
[2]);
589 static int s3c2440_nand_calculate_ecc(struct mtd_info
*mtd
, const u_char
*dat
, u_char
*ecc_code
)
591 struct s3c2410_nand_info
*info
= s3c2410_nand_mtd_toinfo(mtd
);
592 unsigned long ecc
= readl(info
->regs
+ S3C2440_NFMECC0
);
595 ecc_code
[1] = ecc
>> 8;
596 ecc_code
[2] = ecc
>> 16;
598 pr_debug("%s: returning ecc %06lx\n", __func__
, ecc
& 0xffffff);
603 /* over-ride the standard functions for a little more speed. We can
604 * use read/write block to move the data buffers to/from the controller
607 static void s3c2410_nand_read_buf(struct mtd_info
*mtd
, u_char
*buf
, int len
)
609 struct nand_chip
*this = mtd
->priv
;
610 readsb(this->IO_ADDR_R
, buf
, len
);
613 static void s3c2440_nand_read_buf(struct mtd_info
*mtd
, u_char
*buf
, int len
)
615 struct s3c2410_nand_info
*info
= s3c2410_nand_mtd_toinfo(mtd
);
617 readsl(info
->regs
+ S3C2440_NFDATA
, buf
, len
>> 2);
619 /* cleanup if we've got less than a word to do */
623 for (; len
& 3; len
--)
624 *buf
++ = readb(info
->regs
+ S3C2440_NFDATA
);
628 static void s3c2410_nand_write_buf(struct mtd_info
*mtd
, const u_char
*buf
, int len
)
630 struct nand_chip
*this = mtd
->priv
;
631 writesb(this->IO_ADDR_W
, buf
, len
);
634 static void s3c2440_nand_write_buf(struct mtd_info
*mtd
, const u_char
*buf
, int len
)
636 struct s3c2410_nand_info
*info
= s3c2410_nand_mtd_toinfo(mtd
);
638 writesl(info
->regs
+ S3C2440_NFDATA
, buf
, len
>> 2);
640 /* cleanup any fractional write */
644 for (; len
& 3; len
--, buf
++)
645 writeb(*buf
, info
->regs
+ S3C2440_NFDATA
);
649 /* cpufreq driver support */
651 #ifdef CONFIG_CPU_FREQ
653 static int s3c2410_nand_cpufreq_transition(struct notifier_block
*nb
,
654 unsigned long val
, void *data
)
656 struct s3c2410_nand_info
*info
;
657 unsigned long newclk
;
659 info
= container_of(nb
, struct s3c2410_nand_info
, freq_transition
);
660 newclk
= clk_get_rate(info
->clk
);
662 if ((val
== CPUFREQ_POSTCHANGE
&& newclk
< info
->clk_rate
) ||
663 (val
== CPUFREQ_PRECHANGE
&& newclk
> info
->clk_rate
)) {
664 s3c2410_nand_setrate(info
);
670 static inline int s3c2410_nand_cpufreq_register(struct s3c2410_nand_info
*info
)
672 info
->freq_transition
.notifier_call
= s3c2410_nand_cpufreq_transition
;
674 return cpufreq_register_notifier(&info
->freq_transition
,
675 CPUFREQ_TRANSITION_NOTIFIER
);
678 static inline void s3c2410_nand_cpufreq_deregister(struct s3c2410_nand_info
*info
)
680 cpufreq_unregister_notifier(&info
->freq_transition
,
681 CPUFREQ_TRANSITION_NOTIFIER
);
685 static inline int s3c2410_nand_cpufreq_register(struct s3c2410_nand_info
*info
)
690 static inline void s3c2410_nand_cpufreq_deregister(struct s3c2410_nand_info
*info
)
695 /* device management functions */
697 static int s3c24xx_nand_remove(struct platform_device
*pdev
)
699 struct s3c2410_nand_info
*info
= to_nand_info(pdev
);
701 platform_set_drvdata(pdev
, NULL
);
706 s3c2410_nand_cpufreq_deregister(info
);
708 /* Release all our mtds and their partitions, then go through
709 * freeing the resources used
712 if (info
->mtds
!= NULL
) {
713 struct s3c2410_nand_mtd
*ptr
= info
->mtds
;
716 for (mtdno
= 0; mtdno
< info
->mtd_count
; mtdno
++, ptr
++) {
717 pr_debug("releasing mtd %d (%p)\n", mtdno
, ptr
);
718 nand_release(&ptr
->mtd
);
724 /* free the common resources */
726 if (info
->clk
!= NULL
&& !IS_ERR(info
->clk
)) {
727 s3c2410_nand_clk_set_state(info
, CLOCK_DISABLE
);
731 if (info
->regs
!= NULL
) {
736 if (info
->area
!= NULL
) {
737 release_resource(info
->area
);
747 const char *part_probes
[] = { "cmdlinepart", NULL
};
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 struct mtd_partition
*part_info
;
756 return mtd_device_register(&mtd
->mtd
, NULL
, 0);
758 mtd
->mtd
.name
= set
->name
;
759 nr_part
= parse_mtd_partitions(&mtd
->mtd
, part_probes
, &part_info
, 0);
761 if (nr_part
<= 0 && set
->nr_partitions
> 0) {
762 nr_part
= set
->nr_partitions
;
763 part_info
= set
->partitions
;
766 return mtd_device_register(&mtd
->mtd
, part_info
, nr_part
);
770 * s3c2410_nand_init_chip - initialise a single instance of an chip
771 * @info: The base NAND controller the chip is on.
772 * @nmtd: The new controller MTD instance to fill in.
773 * @set: The information passed from the board specific platform data.
775 * Initialise the given @nmtd from the information in @info and @set. This
776 * readies the structure for use with the MTD layer functions by ensuring
777 * all pointers are setup and the necessary control routines selected.
779 static void s3c2410_nand_init_chip(struct s3c2410_nand_info
*info
,
780 struct s3c2410_nand_mtd
*nmtd
,
781 struct s3c2410_nand_set
*set
)
783 struct nand_chip
*chip
= &nmtd
->chip
;
784 void __iomem
*regs
= info
->regs
;
786 chip
->write_buf
= s3c2410_nand_write_buf
;
787 chip
->read_buf
= s3c2410_nand_read_buf
;
788 chip
->select_chip
= s3c2410_nand_select_chip
;
789 chip
->chip_delay
= 50;
791 chip
->options
= set
->options
;
792 chip
->controller
= &info
->controller
;
794 switch (info
->cpu_type
) {
796 chip
->IO_ADDR_W
= regs
+ S3C2410_NFDATA
;
797 info
->sel_reg
= regs
+ S3C2410_NFCONF
;
798 info
->sel_bit
= S3C2410_NFCONF_nFCE
;
799 chip
->cmd_ctrl
= s3c2410_nand_hwcontrol
;
800 chip
->dev_ready
= s3c2410_nand_devready
;
804 chip
->IO_ADDR_W
= regs
+ S3C2440_NFDATA
;
805 info
->sel_reg
= regs
+ S3C2440_NFCONT
;
806 info
->sel_bit
= S3C2440_NFCONT_nFCE
;
807 chip
->cmd_ctrl
= s3c2440_nand_hwcontrol
;
808 chip
->dev_ready
= s3c2440_nand_devready
;
809 chip
->read_buf
= s3c2440_nand_read_buf
;
810 chip
->write_buf
= s3c2440_nand_write_buf
;
814 chip
->IO_ADDR_W
= regs
+ S3C2440_NFDATA
;
815 info
->sel_reg
= regs
+ S3C2440_NFCONT
;
816 info
->sel_bit
= S3C2412_NFCONT_nFCE0
;
817 chip
->cmd_ctrl
= s3c2440_nand_hwcontrol
;
818 chip
->dev_ready
= s3c2412_nand_devready
;
820 if (readl(regs
+ S3C2410_NFCONF
) & S3C2412_NFCONF_NANDBOOT
)
821 dev_info(info
->device
, "System booted from NAND\n");
826 chip
->IO_ADDR_R
= chip
->IO_ADDR_W
;
829 nmtd
->mtd
.priv
= chip
;
830 nmtd
->mtd
.owner
= THIS_MODULE
;
834 chip
->ecc
.calculate
= s3c2410_nand_calculate_ecc
;
835 chip
->ecc
.correct
= s3c2410_nand_correct_data
;
836 chip
->ecc
.mode
= NAND_ECC_HW
;
838 switch (info
->cpu_type
) {
840 chip
->ecc
.hwctl
= s3c2410_nand_enable_hwecc
;
841 chip
->ecc
.calculate
= s3c2410_nand_calculate_ecc
;
845 chip
->ecc
.hwctl
= s3c2412_nand_enable_hwecc
;
846 chip
->ecc
.calculate
= s3c2412_nand_calculate_ecc
;
850 chip
->ecc
.hwctl
= s3c2440_nand_enable_hwecc
;
851 chip
->ecc
.calculate
= s3c2440_nand_calculate_ecc
;
856 chip
->ecc
.mode
= NAND_ECC_SOFT
;
859 if (set
->ecc_layout
!= NULL
)
860 chip
->ecc
.layout
= set
->ecc_layout
;
862 if (set
->disable_ecc
)
863 chip
->ecc
.mode
= NAND_ECC_NONE
;
865 switch (chip
->ecc
.mode
) {
867 dev_info(info
->device
, "NAND ECC disabled\n");
870 dev_info(info
->device
, "NAND soft ECC\n");
873 dev_info(info
->device
, "NAND hardware ECC\n");
876 dev_info(info
->device
, "NAND ECC UNKNOWN\n");
880 /* If you use u-boot BBT creation code, specifying this flag will
881 * let the kernel fish out the BBT from the NAND, and also skip the
882 * full NAND scan that can take 1/2s or so. Little things... */
884 chip
->options
|= NAND_USE_FLASH_BBT
| NAND_SKIP_BBTSCAN
;
888 * s3c2410_nand_update_chip - post probe update
889 * @info: The controller instance.
890 * @nmtd: The driver version of the MTD instance.
892 * This routine is called after the chip probe has successfully completed
893 * and the relevant per-chip information updated. This call ensure that
894 * we update the internal state accordingly.
896 * The internal state is currently limited to the ECC state information.
898 static void s3c2410_nand_update_chip(struct s3c2410_nand_info
*info
,
899 struct s3c2410_nand_mtd
*nmtd
)
901 struct nand_chip
*chip
= &nmtd
->chip
;
903 dev_dbg(info
->device
, "chip %p => page shift %d\n",
904 chip
, chip
->page_shift
);
906 if (chip
->ecc
.mode
!= NAND_ECC_HW
)
909 /* change the behaviour depending on wether we are using
910 * the large or small page nand device */
912 if (chip
->page_shift
> 10) {
913 chip
->ecc
.size
= 256;
916 chip
->ecc
.size
= 512;
918 chip
->ecc
.layout
= &nand_hw_eccoob
;
922 /* s3c24xx_nand_probe
924 * called by device layer when it finds a device matching
925 * one our driver can handled. This code checks to see if
926 * it can allocate all necessary resources then calls the
927 * nand layer to look for devices
929 static int s3c24xx_nand_probe(struct platform_device
*pdev
)
931 struct s3c2410_platform_nand
*plat
= to_nand_plat(pdev
);
932 enum s3c_cpu_type cpu_type
;
933 struct s3c2410_nand_info
*info
;
934 struct s3c2410_nand_mtd
*nmtd
;
935 struct s3c2410_nand_set
*sets
;
936 struct resource
*res
;
942 cpu_type
= platform_get_device_id(pdev
)->driver_data
;
944 pr_debug("s3c2410_nand_probe(%p)\n", pdev
);
946 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
948 dev_err(&pdev
->dev
, "no memory for flash info\n");
953 platform_set_drvdata(pdev
, info
);
955 spin_lock_init(&info
->controller
.lock
);
956 init_waitqueue_head(&info
->controller
.wq
);
958 /* get the clock source and enable it */
960 info
->clk
= clk_get(&pdev
->dev
, "nand");
961 if (IS_ERR(info
->clk
)) {
962 dev_err(&pdev
->dev
, "failed to get clock\n");
967 s3c2410_nand_clk_set_state(info
, CLOCK_ENABLE
);
969 /* allocate and map the resource */
971 /* currently we assume we have the one resource */
972 res
= pdev
->resource
;
973 size
= resource_size(res
);
975 info
->area
= request_mem_region(res
->start
, size
, pdev
->name
);
977 if (info
->area
== NULL
) {
978 dev_err(&pdev
->dev
, "cannot reserve register region\n");
983 info
->device
= &pdev
->dev
;
984 info
->platform
= plat
;
985 info
->regs
= ioremap(res
->start
, size
);
986 info
->cpu_type
= cpu_type
;
988 if (info
->regs
== NULL
) {
989 dev_err(&pdev
->dev
, "cannot reserve register region\n");
994 dev_dbg(&pdev
->dev
, "mapped registers at %p\n", info
->regs
);
996 /* initialise the hardware */
998 err
= s3c2410_nand_inithw(info
);
1002 sets
= (plat
!= NULL
) ? plat
->sets
: NULL
;
1003 nr_sets
= (plat
!= NULL
) ? plat
->nr_sets
: 1;
1005 info
->mtd_count
= nr_sets
;
1007 /* allocate our information */
1009 size
= nr_sets
* sizeof(*info
->mtds
);
1010 info
->mtds
= kzalloc(size
, GFP_KERNEL
);
1011 if (info
->mtds
== NULL
) {
1012 dev_err(&pdev
->dev
, "failed to allocate mtd storage\n");
1017 /* initialise all possible chips */
1021 for (setno
= 0; setno
< nr_sets
; setno
++, nmtd
++) {
1022 pr_debug("initialising set %d (%p, info %p)\n", setno
, nmtd
, info
);
1024 s3c2410_nand_init_chip(info
, nmtd
, sets
);
1026 nmtd
->scan_res
= nand_scan_ident(&nmtd
->mtd
,
1027 (sets
) ? sets
->nr_chips
: 1,
1030 if (nmtd
->scan_res
== 0) {
1031 s3c2410_nand_update_chip(info
, nmtd
);
1032 nand_scan_tail(&nmtd
->mtd
);
1033 s3c2410_nand_add_partition(info
, nmtd
, sets
);
1040 err
= s3c2410_nand_cpufreq_register(info
);
1042 dev_err(&pdev
->dev
, "failed to init cpufreq support\n");
1046 if (allow_clk_suspend(info
)) {
1047 dev_info(&pdev
->dev
, "clock idle support enabled\n");
1048 s3c2410_nand_clk_set_state(info
, CLOCK_SUSPEND
);
1051 pr_debug("initialised ok\n");
1055 s3c24xx_nand_remove(pdev
);
1065 static int s3c24xx_nand_suspend(struct platform_device
*dev
, pm_message_t pm
)
1067 struct s3c2410_nand_info
*info
= platform_get_drvdata(dev
);
1070 info
->save_sel
= readl(info
->sel_reg
);
1072 /* For the moment, we must ensure nFCE is high during
1073 * the time we are suspended. This really should be
1074 * handled by suspending the MTDs we are using, but
1075 * that is currently not the case. */
1077 writel(info
->save_sel
| info
->sel_bit
, info
->sel_reg
);
1079 s3c2410_nand_clk_set_state(info
, CLOCK_DISABLE
);
1085 static int s3c24xx_nand_resume(struct platform_device
*dev
)
1087 struct s3c2410_nand_info
*info
= platform_get_drvdata(dev
);
1091 s3c2410_nand_clk_set_state(info
, CLOCK_ENABLE
);
1092 s3c2410_nand_inithw(info
);
1094 /* Restore the state of the nFCE line. */
1096 sel
= readl(info
->sel_reg
);
1097 sel
&= ~info
->sel_bit
;
1098 sel
|= info
->save_sel
& info
->sel_bit
;
1099 writel(sel
, info
->sel_reg
);
1101 s3c2410_nand_clk_set_state(info
, CLOCK_SUSPEND
);
1108 #define s3c24xx_nand_suspend NULL
1109 #define s3c24xx_nand_resume NULL
1112 /* driver device registration */
1114 static struct platform_device_id s3c24xx_driver_ids
[] = {
1116 .name
= "s3c2410-nand",
1117 .driver_data
= TYPE_S3C2410
,
1119 .name
= "s3c2440-nand",
1120 .driver_data
= TYPE_S3C2440
,
1122 .name
= "s3c2412-nand",
1123 .driver_data
= TYPE_S3C2412
,
1125 .name
= "s3c6400-nand",
1126 .driver_data
= TYPE_S3C2412
, /* compatible with 2412 */
1131 MODULE_DEVICE_TABLE(platform
, s3c24xx_driver_ids
);
1133 static struct platform_driver s3c24xx_nand_driver
= {
1134 .probe
= s3c24xx_nand_probe
,
1135 .remove
= s3c24xx_nand_remove
,
1136 .suspend
= s3c24xx_nand_suspend
,
1137 .resume
= s3c24xx_nand_resume
,
1138 .id_table
= s3c24xx_driver_ids
,
1140 .name
= "s3c24xx-nand",
1141 .owner
= THIS_MODULE
,
1145 static int __init
s3c2410_nand_init(void)
1147 printk("S3C24XX NAND Driver, (c) 2004 Simtec Electronics\n");
1149 return platform_driver_register(&s3c24xx_nand_driver
);
1152 static void __exit
s3c2410_nand_exit(void)
1154 platform_driver_unregister(&s3c24xx_nand_driver
);
1157 module_init(s3c2410_nand_init
);
1158 module_exit(s3c2410_nand_exit
);
1160 MODULE_LICENSE("GPL");
1161 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
1162 MODULE_DESCRIPTION("S3C24XX MTD NAND driver");