1 /* linux/drivers/mtd/nand/s3c2410.c
3 * Copyright (c) 2004,2005 Simtec Electronics
4 * http://www.simtec.co.uk/products/SWLINUX/
5 * Ben Dooks <ben@simtec.co.uk>
7 * Samsung S3C2410/S3C240 NAND driver
10 * 21-Sep-2004 BJD Initial version
11 * 23-Sep-2004 BJD Mulitple device support
12 * 28-Sep-2004 BJD Fixed ECC placement for Hardware mode
13 * 12-Oct-2004 BJD Fixed errors in use of platform data
14 * 18-Feb-2005 BJD Fix sparse errors
15 * 14-Mar-2005 BJD Applied tglx's code reduction patch
16 * 02-May-2005 BJD Fixed s3c2440 support
17 * 02-May-2005 BJD Reduced hwcontrol decode
18 * 20-Jun-2005 BJD Updated s3c2440 support, fixed timing bug
19 * 08-Jul-2005 BJD Fix OOPS when no platform data supplied
20 * 20-Oct-2005 BJD Fix timing calculation bug
21 * 14-Jan-2006 BJD Allow clock to be stopped when idle
23 * $Id: s3c2410.c,v 1.23 2006/04/01 18:06:29 bjd Exp $
25 * This program is free software; you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation; either version 2 of the License, or
28 * (at your option) any later version.
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
35 * You should have received a copy of the GNU General Public License
36 * along with this program; if not, write to the Free Software
37 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
40 #ifdef CONFIG_MTD_NAND_S3C2410_DEBUG
44 #include <linux/module.h>
45 #include <linux/types.h>
46 #include <linux/init.h>
47 #include <linux/kernel.h>
48 #include <linux/string.h>
49 #include <linux/ioport.h>
50 #include <linux/platform_device.h>
51 #include <linux/delay.h>
52 #include <linux/err.h>
53 #include <linux/slab.h>
54 #include <linux/clk.h>
56 #include <linux/mtd/mtd.h>
57 #include <linux/mtd/nand.h>
58 #include <linux/mtd/nand_ecc.h>
59 #include <linux/mtd/partitions.h>
63 #include <asm/arch/regs-nand.h>
64 #include <asm/arch/nand.h>
66 #define PFX "s3c2410-nand: "
68 #ifdef CONFIG_MTD_NAND_S3C2410_HWECC
69 static int hardware_ecc = 1;
71 static int hardware_ecc = 0;
74 #ifdef CONFIG_MTD_NAND_S3C2410_CLKSTOP
75 static int clock_stop = 1;
77 static const int clock_stop = 0;
81 /* new oob placement block for use with hardware ecc generation
84 static struct nand_ecclayout nand_hw_eccoob = {
90 /* controller and mtd information */
92 struct s3c2410_nand_info;
94 struct s3c2410_nand_mtd {
96 struct nand_chip chip;
97 struct s3c2410_nand_set *set;
98 struct s3c2410_nand_info *info;
102 /* overview of the s3c2410 nand state */
104 struct s3c2410_nand_info {
106 struct nand_hw_control controller;
107 struct s3c2410_nand_mtd *mtds;
108 struct s3c2410_platform_nand *platform;
111 struct device *device;
112 struct resource *area;
117 unsigned char is_s3c2440;
120 /* conversion functions */
122 static struct s3c2410_nand_mtd *s3c2410_nand_mtd_toours(struct mtd_info *mtd)
124 return container_of(mtd, struct s3c2410_nand_mtd, mtd);
127 static struct s3c2410_nand_info *s3c2410_nand_mtd_toinfo(struct mtd_info *mtd)
129 return s3c2410_nand_mtd_toours(mtd)->info;
132 static struct s3c2410_nand_info *to_nand_info(struct platform_device *dev)
134 return platform_get_drvdata(dev);
137 static struct s3c2410_platform_nand *to_nand_plat(struct platform_device *dev)
139 return dev->dev.platform_data;
142 static inline int allow_clk_stop(struct s3c2410_nand_info *info)
147 /* timing calculations */
149 #define NS_IN_KHZ 1000000
151 static int s3c2410_nand_calc_rate(int wanted, unsigned long clk, int max)
155 result = (wanted * clk) / NS_IN_KHZ;
158 pr_debug("result %d from %ld, %d\n", result, clk, wanted);
161 printk("%d ns is too big for current clock rate %ld\n", wanted, clk);
171 #define to_ns(ticks,clk) (((ticks) * NS_IN_KHZ) / (unsigned int)(clk))
173 /* controller setup */
175 static int s3c2410_nand_inithw(struct s3c2410_nand_info *info, struct platform_device *pdev)
177 struct s3c2410_platform_nand *plat = to_nand_plat(pdev);
178 unsigned long clkrate = clk_get_rate(info->clk);
179 int tacls, twrph0, twrph1;
182 /* calculate the timing information for the controller */
184 clkrate /= 1000; /* turn clock into kHz for ease of use */
187 tacls = s3c2410_nand_calc_rate(plat->tacls, clkrate, 4);
188 twrph0 = s3c2410_nand_calc_rate(plat->twrph0, clkrate, 8);
189 twrph1 = s3c2410_nand_calc_rate(plat->twrph1, clkrate, 8);
191 /* default timings */
197 if (tacls < 0 || twrph0 < 0 || twrph1 < 0) {
198 printk(KERN_ERR PFX "cannot get timings suitable for board\n");
202 printk(KERN_INFO PFX "Tacls=%d, %dns Twrph0=%d %dns, Twrph1=%d %dns\n",
203 tacls, to_ns(tacls, clkrate), twrph0, to_ns(twrph0, clkrate), twrph1, to_ns(twrph1, clkrate));
205 if (!info->is_s3c2440) {
206 cfg = S3C2410_NFCONF_EN;
207 cfg |= S3C2410_NFCONF_TACLS(tacls - 1);
208 cfg |= S3C2410_NFCONF_TWRPH0(twrph0 - 1);
209 cfg |= S3C2410_NFCONF_TWRPH1(twrph1 - 1);
211 cfg = S3C2440_NFCONF_TACLS(tacls - 1);
212 cfg |= S3C2440_NFCONF_TWRPH0(twrph0 - 1);
213 cfg |= S3C2440_NFCONF_TWRPH1(twrph1 - 1);
215 /* enable the controller and de-assert nFCE */
217 writel(S3C2440_NFCONT_ENABLE | S3C2440_NFCONT_ENABLE,
218 info->regs + S3C2440_NFCONT);
221 pr_debug(PFX "NF_CONF is 0x%lx\n", cfg);
223 writel(cfg, info->regs + S3C2410_NFCONF);
229 static void s3c2410_nand_select_chip(struct mtd_info *mtd, int chip)
231 struct s3c2410_nand_info *info;
232 struct s3c2410_nand_mtd *nmtd;
233 struct nand_chip *this = mtd->priv;
241 bit = (info->is_s3c2440) ? S3C2440_NFCONT_nFCE : S3C2410_NFCONF_nFCE;
242 reg = info->regs + ((info->is_s3c2440) ? S3C2440_NFCONT : S3C2410_NFCONF);
244 if (chip != -1 && allow_clk_stop(info))
245 clk_enable(info->clk);
252 if (nmtd->set != NULL && chip > nmtd->set->nr_chips) {
253 printk(KERN_ERR PFX "chip %d out of range\n", chip);
257 if (info->platform != NULL) {
258 if (info->platform->select_chip != NULL)
259 (info->platform->select_chip) (nmtd->set, chip);
267 if (chip == -1 && allow_clk_stop(info))
268 clk_disable(info->clk);
271 /* s3c2410_nand_hwcontrol
273 * Issue command and address cycles to the chip
276 static void s3c2410_nand_hwcontrol(struct mtd_info *mtd, int cmd,
279 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
281 if (cmd == NAND_CMD_NONE)
285 writeb(cmd, info->regs + S3C2410_NFCMD);
287 writeb(cmd, info->regs + S3C2410_NFADDR);
290 /* command and control functions */
292 static void s3c2440_nand_hwcontrol(struct mtd_info *mtd, int cmd,
295 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
297 if (cmd == NAND_CMD_NONE)
301 writeb(cmd, info->regs + S3C2440_NFCMD);
303 writeb(cmd, info->regs + S3C2440_NFADDR);
306 /* s3c2410_nand_devready()
308 * returns 0 if the nand is busy, 1 if it is ready
311 static int s3c2410_nand_devready(struct mtd_info *mtd)
313 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
315 if (info->is_s3c2440)
316 return readb(info->regs + S3C2440_NFSTAT) & S3C2440_NFSTAT_READY;
317 return readb(info->regs + S3C2410_NFSTAT) & S3C2410_NFSTAT_BUSY;
320 /* ECC handling functions */
322 static int s3c2410_nand_correct_data(struct mtd_info *mtd, u_char *dat, u_char *read_ecc, u_char *calc_ecc)
324 pr_debug("s3c2410_nand_correct_data(%p,%p,%p,%p)\n", mtd, dat, read_ecc, calc_ecc);
326 pr_debug("eccs: read %02x,%02x,%02x vs calc %02x,%02x,%02x\n",
327 read_ecc[0], read_ecc[1], read_ecc[2], calc_ecc[0], calc_ecc[1], calc_ecc[2]);
329 if (read_ecc[0] == calc_ecc[0] && read_ecc[1] == calc_ecc[1] && read_ecc[2] == calc_ecc[2])
332 /* we curently have no method for correcting the error */
339 * These allow the s3c2410 and s3c2440 to use the controller's ECC
340 * generator block to ECC the data as it passes through]
343 static void s3c2410_nand_enable_hwecc(struct mtd_info *mtd, int mode)
345 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
348 ctrl = readl(info->regs + S3C2410_NFCONF);
349 ctrl |= S3C2410_NFCONF_INITECC;
350 writel(ctrl, info->regs + S3C2410_NFCONF);
353 static void s3c2440_nand_enable_hwecc(struct mtd_info *mtd, int mode)
355 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
358 ctrl = readl(info->regs + S3C2440_NFCONT);
359 writel(ctrl | S3C2440_NFCONT_INITECC, info->regs + S3C2440_NFCONT);
362 static int s3c2410_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
364 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
366 ecc_code[0] = readb(info->regs + S3C2410_NFECC + 0);
367 ecc_code[1] = readb(info->regs + S3C2410_NFECC + 1);
368 ecc_code[2] = readb(info->regs + S3C2410_NFECC + 2);
370 pr_debug("calculate_ecc: returning ecc %02x,%02x,%02x\n", ecc_code[0], ecc_code[1], ecc_code[2]);
375 static int s3c2440_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
377 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
378 unsigned long ecc = readl(info->regs + S3C2440_NFMECC0);
381 ecc_code[1] = ecc >> 8;
382 ecc_code[2] = ecc >> 16;
384 pr_debug("calculate_ecc: returning ecc %02x,%02x,%02x\n", ecc_code[0], ecc_code[1], ecc_code[2]);
389 /* over-ride the standard functions for a little more speed. We can
390 * use read/write block to move the data buffers to/from the controller
393 static void s3c2410_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
395 struct nand_chip *this = mtd->priv;
396 readsb(this->IO_ADDR_R, buf, len);
399 static void s3c2410_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
401 struct nand_chip *this = mtd->priv;
402 writesb(this->IO_ADDR_W, buf, len);
405 /* device management functions */
407 static int s3c2410_nand_remove(struct platform_device *pdev)
409 struct s3c2410_nand_info *info = to_nand_info(pdev);
411 platform_set_drvdata(pdev, NULL);
416 /* first thing we need to do is release all our mtds
417 * and their partitions, then go through freeing the
421 if (info->mtds != NULL) {
422 struct s3c2410_nand_mtd *ptr = info->mtds;
425 for (mtdno = 0; mtdno < info->mtd_count; mtdno++, ptr++) {
426 pr_debug("releasing mtd %d (%p)\n", mtdno, ptr);
427 nand_release(&ptr->mtd);
433 /* free the common resources */
435 if (info->clk != NULL && !IS_ERR(info->clk)) {
436 if (!allow_clk_stop(info))
437 clk_disable(info->clk);
441 if (info->regs != NULL) {
446 if (info->area != NULL) {
447 release_resource(info->area);
457 #ifdef CONFIG_MTD_PARTITIONS
458 static int s3c2410_nand_add_partition(struct s3c2410_nand_info *info,
459 struct s3c2410_nand_mtd *mtd,
460 struct s3c2410_nand_set *set)
463 return add_mtd_device(&mtd->mtd);
465 if (set->nr_partitions > 0 && set->partitions != NULL) {
466 return add_mtd_partitions(&mtd->mtd, set->partitions, set->nr_partitions);
469 return add_mtd_device(&mtd->mtd);
472 static int s3c2410_nand_add_partition(struct s3c2410_nand_info *info,
473 struct s3c2410_nand_mtd *mtd,
474 struct s3c2410_nand_set *set)
476 return add_mtd_device(&mtd->mtd);
480 /* s3c2410_nand_init_chip
482 * init a single instance of an chip
485 static void s3c2410_nand_init_chip(struct s3c2410_nand_info *info,
486 struct s3c2410_nand_mtd *nmtd,
487 struct s3c2410_nand_set *set)
489 struct nand_chip *chip = &nmtd->chip;
491 chip->IO_ADDR_R = info->regs + S3C2410_NFDATA;
492 chip->IO_ADDR_W = info->regs + S3C2410_NFDATA;
493 chip->cmd_ctrl = s3c2410_nand_hwcontrol;
494 chip->dev_ready = s3c2410_nand_devready;
495 chip->write_buf = s3c2410_nand_write_buf;
496 chip->read_buf = s3c2410_nand_read_buf;
497 chip->select_chip = s3c2410_nand_select_chip;
498 chip->chip_delay = 50;
501 chip->controller = &info->controller;
503 if (info->is_s3c2440) {
504 chip->IO_ADDR_R = info->regs + S3C2440_NFDATA;
505 chip->IO_ADDR_W = info->regs + S3C2440_NFDATA;
506 chip->cmd_ctrl = s3c2440_nand_hwcontrol;
510 nmtd->mtd.priv = chip;
511 nmtd->mtd.owner = THIS_MODULE;
515 chip->ecc.correct = s3c2410_nand_correct_data;
516 chip->ecc.hwctl = s3c2410_nand_enable_hwecc;
517 chip->ecc.calculate = s3c2410_nand_calculate_ecc;
518 chip->ecc.mode = NAND_ECC_HW;
519 chip->ecc.size = 512;
521 chip->ecc.layout = &nand_hw_eccoob;
523 if (info->is_s3c2440) {
524 chip->ecc.hwctl = s3c2440_nand_enable_hwecc;
525 chip->ecc.calculate = s3c2440_nand_calculate_ecc;
528 chip->ecc.mode = NAND_ECC_SOFT;
532 /* s3c2410_nand_probe
534 * called by device layer when it finds a device matching
535 * one our driver can handled. This code checks to see if
536 * it can allocate all necessary resources then calls the
537 * nand layer to look for devices
540 static int s3c24xx_nand_probe(struct platform_device *pdev, int is_s3c2440)
542 struct s3c2410_platform_nand *plat = to_nand_plat(pdev);
543 struct s3c2410_nand_info *info;
544 struct s3c2410_nand_mtd *nmtd;
545 struct s3c2410_nand_set *sets;
546 struct resource *res;
552 pr_debug("s3c2410_nand_probe(%p)\n", pdev);
554 info = kmalloc(sizeof(*info), GFP_KERNEL);
556 dev_err(&pdev->dev, "no memory for flash info\n");
561 memzero(info, sizeof(*info));
562 platform_set_drvdata(pdev, info);
564 spin_lock_init(&info->controller.lock);
565 init_waitqueue_head(&info->controller.wq);
567 /* get the clock source and enable it */
569 info->clk = clk_get(&pdev->dev, "nand");
570 if (IS_ERR(info->clk)) {
571 dev_err(&pdev->dev, "failed to get clock");
576 clk_enable(info->clk);
578 /* allocate and map the resource */
580 /* currently we assume we have the one resource */
581 res = pdev->resource;
582 size = res->end - res->start + 1;
584 info->area = request_mem_region(res->start, size, pdev->name);
586 if (info->area == NULL) {
587 dev_err(&pdev->dev, "cannot reserve register region\n");
592 info->device = &pdev->dev;
593 info->platform = plat;
594 info->regs = ioremap(res->start, size);
595 info->is_s3c2440 = is_s3c2440;
597 if (info->regs == NULL) {
598 dev_err(&pdev->dev, "cannot reserve register region\n");
603 dev_dbg(&pdev->dev, "mapped registers at %p\n", info->regs);
605 /* initialise the hardware */
607 err = s3c2410_nand_inithw(info, pdev);
611 sets = (plat != NULL) ? plat->sets : NULL;
612 nr_sets = (plat != NULL) ? plat->nr_sets : 1;
614 info->mtd_count = nr_sets;
616 /* allocate our information */
618 size = nr_sets * sizeof(*info->mtds);
619 info->mtds = kmalloc(size, GFP_KERNEL);
620 if (info->mtds == NULL) {
621 dev_err(&pdev->dev, "failed to allocate mtd storage\n");
626 memzero(info->mtds, size);
628 /* initialise all possible chips */
632 for (setno = 0; setno < nr_sets; setno++, nmtd++) {
633 pr_debug("initialising set %d (%p, info %p)\n", setno, nmtd, info);
635 s3c2410_nand_init_chip(info, nmtd, sets);
637 nmtd->scan_res = nand_scan(&nmtd->mtd, (sets) ? sets->nr_chips : 1);
639 if (nmtd->scan_res == 0) {
640 s3c2410_nand_add_partition(info, nmtd, sets);
647 if (allow_clk_stop(info)) {
648 dev_info(&pdev->dev, "clock idle support enabled\n");
649 clk_disable(info->clk);
652 pr_debug("initialised ok\n");
656 s3c2410_nand_remove(pdev);
666 static int s3c24xx_nand_suspend(struct platform_device *dev, pm_message_t pm)
668 struct s3c2410_nand_info *info = platform_get_drvdata(dev);
671 if (!allow_clk_stop(info))
672 clk_disable(info->clk);
678 static int s3c24xx_nand_resume(struct platform_device *dev)
680 struct s3c2410_nand_info *info = platform_get_drvdata(dev);
683 clk_enable(info->clk);
684 s3c2410_nand_inithw(info, dev);
686 if (allow_clk_stop(info))
687 clk_disable(info->clk);
694 #define s3c24xx_nand_suspend NULL
695 #define s3c24xx_nand_resume NULL
698 /* driver device registration */
700 static int s3c2410_nand_probe(struct platform_device *dev)
702 return s3c24xx_nand_probe(dev, 0);
705 static int s3c2440_nand_probe(struct platform_device *dev)
707 return s3c24xx_nand_probe(dev, 1);
710 static struct platform_driver s3c2410_nand_driver = {
711 .probe = s3c2410_nand_probe,
712 .remove = s3c2410_nand_remove,
713 .suspend = s3c24xx_nand_suspend,
714 .resume = s3c24xx_nand_resume,
716 .name = "s3c2410-nand",
717 .owner = THIS_MODULE,
721 static struct platform_driver s3c2440_nand_driver = {
722 .probe = s3c2440_nand_probe,
723 .remove = s3c2410_nand_remove,
724 .suspend = s3c24xx_nand_suspend,
725 .resume = s3c24xx_nand_resume,
727 .name = "s3c2440-nand",
728 .owner = THIS_MODULE,
732 static int __init s3c2410_nand_init(void)
734 printk("S3C24XX NAND Driver, (c) 2004 Simtec Electronics\n");
736 platform_driver_register(&s3c2440_nand_driver);
737 return platform_driver_register(&s3c2410_nand_driver);
740 static void __exit s3c2410_nand_exit(void)
742 platform_driver_unregister(&s3c2440_nand_driver);
743 platform_driver_unregister(&s3c2410_nand_driver);
746 module_init(s3c2410_nand_init);
747 module_exit(s3c2410_nand_exit);
749 MODULE_LICENSE("GPL");
750 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
751 MODULE_DESCRIPTION("S3C24XX MTD NAND driver");