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
22 * $Id: s3c2410.c,v 1.20 2005/11/07 11:14:31 gleixner Exp $
24 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation; either version 2 of the License, or
27 * (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program; if not, write to the Free Software
36 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
39 #include <config/mtd/nand/s3c2410/hwecc.h>
40 #include <config/mtd/nand/s3c2410/debug.h>
42 #ifdef CONFIG_MTD_NAND_S3C2410_DEBUG
46 #include <linux/module.h>
47 #include <linux/types.h>
48 #include <linux/init.h>
49 #include <linux/kernel.h>
50 #include <linux/string.h>
51 #include <linux/ioport.h>
52 #include <linux/platform_device.h>
53 #include <linux/delay.h>
54 #include <linux/err.h>
55 #include <linux/slab.h>
56 #include <linux/clk.h>
58 #include <linux/mtd/mtd.h>
59 #include <linux/mtd/nand.h>
60 #include <linux/mtd/nand_ecc.h>
61 #include <linux/mtd/partitions.h>
65 #include <asm/arch/regs-nand.h>
66 #include <asm/arch/nand.h>
68 #define PFX "s3c2410-nand: "
70 #ifdef CONFIG_MTD_NAND_S3C2410_HWECC
71 static int hardware_ecc = 1;
73 static int hardware_ecc = 0;
76 /* new oob placement block for use with hardware ecc generation
79 static struct nand_oobinfo nand_hw_eccoob = {
80 .useecc = MTD_NANDECC_AUTOPLACE,
86 /* controller and mtd information */
88 struct s3c2410_nand_info;
90 struct s3c2410_nand_mtd {
92 struct nand_chip chip;
93 struct s3c2410_nand_set *set;
94 struct s3c2410_nand_info *info;
98 /* overview of the s3c2410 nand state */
100 struct s3c2410_nand_info {
102 struct nand_hw_control controller;
103 struct s3c2410_nand_mtd *mtds;
104 struct s3c2410_platform_nand *platform;
107 struct device *device;
108 struct resource *area;
113 unsigned char is_s3c2440;
116 /* conversion functions */
118 static struct s3c2410_nand_mtd *s3c2410_nand_mtd_toours(struct mtd_info *mtd)
120 return container_of(mtd, struct s3c2410_nand_mtd, mtd);
123 static struct s3c2410_nand_info *s3c2410_nand_mtd_toinfo(struct mtd_info *mtd)
125 return s3c2410_nand_mtd_toours(mtd)->info;
128 static struct s3c2410_nand_info *to_nand_info(struct platform_device *dev)
130 return platform_get_drvdata(dev);
133 static struct s3c2410_platform_nand *to_nand_plat(struct platform_device *dev)
135 return dev->dev.platform_data;
138 /* timing calculations */
140 #define NS_IN_KHZ 1000000
142 static int s3c2410_nand_calc_rate(int wanted, unsigned long clk, int max)
146 result = (wanted * clk) / NS_IN_KHZ;
149 pr_debug("result %d from %ld, %d\n", result, clk, wanted);
152 printk("%d ns is too big for current clock rate %ld\n", wanted, clk);
162 #define to_ns(ticks,clk) (((ticks) * NS_IN_KHZ) / (unsigned int)(clk))
164 /* controller setup */
166 static int s3c2410_nand_inithw(struct s3c2410_nand_info *info, struct platform_device *pdev)
168 struct s3c2410_platform_nand *plat = to_nand_plat(pdev);
169 unsigned long clkrate = clk_get_rate(info->clk);
170 int tacls, twrph0, twrph1;
173 /* calculate the timing information for the controller */
175 clkrate /= 1000; /* turn clock into kHz for ease of use */
178 tacls = s3c2410_nand_calc_rate(plat->tacls, clkrate, 4);
179 twrph0 = s3c2410_nand_calc_rate(plat->twrph0, clkrate, 8);
180 twrph1 = s3c2410_nand_calc_rate(plat->twrph1, clkrate, 8);
182 /* default timings */
188 if (tacls < 0 || twrph0 < 0 || twrph1 < 0) {
189 printk(KERN_ERR PFX "cannot get timings suitable for board\n");
193 printk(KERN_INFO PFX "Tacls=%d, %dns Twrph0=%d %dns, Twrph1=%d %dns\n",
194 tacls, to_ns(tacls, clkrate), twrph0, to_ns(twrph0, clkrate), twrph1, to_ns(twrph1, clkrate));
196 if (!info->is_s3c2440) {
197 cfg = S3C2410_NFCONF_EN;
198 cfg |= S3C2410_NFCONF_TACLS(tacls - 1);
199 cfg |= S3C2410_NFCONF_TWRPH0(twrph0 - 1);
200 cfg |= S3C2410_NFCONF_TWRPH1(twrph1 - 1);
202 cfg = S3C2440_NFCONF_TACLS(tacls - 1);
203 cfg |= S3C2440_NFCONF_TWRPH0(twrph0 - 1);
204 cfg |= S3C2440_NFCONF_TWRPH1(twrph1 - 1);
207 pr_debug(PFX "NF_CONF is 0x%lx\n", cfg);
209 writel(cfg, info->regs + S3C2410_NFCONF);
215 static void s3c2410_nand_select_chip(struct mtd_info *mtd, int chip)
217 struct s3c2410_nand_info *info;
218 struct s3c2410_nand_mtd *nmtd;
219 struct nand_chip *this = mtd->priv;
227 bit = (info->is_s3c2440) ? S3C2440_NFCONT_nFCE : S3C2410_NFCONF_nFCE;
228 reg = info->regs + ((info->is_s3c2440) ? S3C2440_NFCONT : S3C2410_NFCONF);
235 if (nmtd->set != NULL && chip > nmtd->set->nr_chips) {
236 printk(KERN_ERR PFX "chip %d out of range\n", chip);
240 if (info->platform != NULL) {
241 if (info->platform->select_chip != NULL)
242 (info->platform->select_chip) (nmtd->set, chip);
251 /* command and control functions
253 * Note, these all use tglx's method of changing the IO_ADDR_W field
254 * to make the code simpler, and use the nand layer's code to issue the
255 * command and address sequences via the proper IO ports.
259 static void s3c2410_nand_hwcontrol(struct mtd_info *mtd, int cmd)
261 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
262 struct nand_chip *chip = mtd->priv;
265 case NAND_CTL_SETNCE:
266 case NAND_CTL_CLRNCE:
267 printk(KERN_ERR "%s: called for NCE\n", __FUNCTION__);
270 case NAND_CTL_SETCLE:
271 chip->IO_ADDR_W = info->regs + S3C2410_NFCMD;
274 case NAND_CTL_SETALE:
275 chip->IO_ADDR_W = info->regs + S3C2410_NFADDR;
278 /* NAND_CTL_CLRCLE: */
279 /* NAND_CTL_CLRALE: */
281 chip->IO_ADDR_W = info->regs + S3C2410_NFDATA;
286 /* command and control functions */
288 static void s3c2440_nand_hwcontrol(struct mtd_info *mtd, int cmd)
290 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
291 struct nand_chip *chip = mtd->priv;
294 case NAND_CTL_SETNCE:
295 case NAND_CTL_CLRNCE:
296 printk(KERN_ERR "%s: called for NCE\n", __FUNCTION__);
299 case NAND_CTL_SETCLE:
300 chip->IO_ADDR_W = info->regs + S3C2440_NFCMD;
303 case NAND_CTL_SETALE:
304 chip->IO_ADDR_W = info->regs + S3C2440_NFADDR;
307 /* NAND_CTL_CLRCLE: */
308 /* NAND_CTL_CLRALE: */
310 chip->IO_ADDR_W = info->regs + S3C2440_NFDATA;
315 /* s3c2410_nand_devready()
317 * returns 0 if the nand is busy, 1 if it is ready
320 static int s3c2410_nand_devready(struct mtd_info *mtd)
322 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
324 if (info->is_s3c2440)
325 return readb(info->regs + S3C2440_NFSTAT) & S3C2440_NFSTAT_READY;
326 return readb(info->regs + S3C2410_NFSTAT) & S3C2410_NFSTAT_BUSY;
329 /* ECC handling functions */
331 static int s3c2410_nand_correct_data(struct mtd_info *mtd, u_char *dat, u_char *read_ecc, u_char *calc_ecc)
333 pr_debug("s3c2410_nand_correct_data(%p,%p,%p,%p)\n", mtd, dat, read_ecc, calc_ecc);
335 pr_debug("eccs: read %02x,%02x,%02x vs calc %02x,%02x,%02x\n",
336 read_ecc[0], read_ecc[1], read_ecc[2], calc_ecc[0], calc_ecc[1], calc_ecc[2]);
338 if (read_ecc[0] == calc_ecc[0] && read_ecc[1] == calc_ecc[1] && read_ecc[2] == calc_ecc[2])
341 /* we curently have no method for correcting the error */
348 * These allow the s3c2410 and s3c2440 to use the controller's ECC
349 * generator block to ECC the data as it passes through]
352 static void s3c2410_nand_enable_hwecc(struct mtd_info *mtd, int mode)
354 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
357 ctrl = readl(info->regs + S3C2410_NFCONF);
358 ctrl |= S3C2410_NFCONF_INITECC;
359 writel(ctrl, info->regs + S3C2410_NFCONF);
362 static void s3c2440_nand_enable_hwecc(struct mtd_info *mtd, int mode)
364 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
367 ctrl = readl(info->regs + S3C2440_NFCONT);
368 writel(ctrl | S3C2440_NFCONT_INITECC, info->regs + S3C2440_NFCONT);
371 static int s3c2410_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
373 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
375 ecc_code[0] = readb(info->regs + S3C2410_NFECC + 0);
376 ecc_code[1] = readb(info->regs + S3C2410_NFECC + 1);
377 ecc_code[2] = readb(info->regs + S3C2410_NFECC + 2);
379 pr_debug("calculate_ecc: returning ecc %02x,%02x,%02x\n", ecc_code[0], ecc_code[1], ecc_code[2]);
384 static int s3c2440_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
386 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
387 unsigned long ecc = readl(info->regs + S3C2440_NFMECC0);
390 ecc_code[1] = ecc >> 8;
391 ecc_code[2] = ecc >> 16;
393 pr_debug("calculate_ecc: returning ecc %02x,%02x,%02x\n", ecc_code[0], ecc_code[1], ecc_code[2]);
398 /* over-ride the standard functions for a little more speed. We can
399 * use read/write block to move the data buffers to/from the controller
402 static void s3c2410_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
404 struct nand_chip *this = mtd->priv;
405 readsb(this->IO_ADDR_R, buf, len);
408 static void s3c2410_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
410 struct nand_chip *this = mtd->priv;
411 writesb(this->IO_ADDR_W, buf, len);
414 /* device management functions */
416 static int s3c2410_nand_remove(struct platform_device *pdev)
418 struct s3c2410_nand_info *info = to_nand_info(pdev);
420 platform_set_drvdata(pdev, NULL);
425 /* first thing we need to do is release all our mtds
426 * and their partitions, then go through freeing the
430 if (info->mtds != NULL) {
431 struct s3c2410_nand_mtd *ptr = info->mtds;
434 for (mtdno = 0; mtdno < info->mtd_count; mtdno++, ptr++) {
435 pr_debug("releasing mtd %d (%p)\n", mtdno, ptr);
436 nand_release(&ptr->mtd);
442 /* free the common resources */
444 if (info->clk != NULL && !IS_ERR(info->clk)) {
445 clk_disable(info->clk);
449 if (info->regs != NULL) {
454 if (info->area != NULL) {
455 release_resource(info->area);
465 #ifdef CONFIG_MTD_PARTITIONS
466 static int s3c2410_nand_add_partition(struct s3c2410_nand_info *info,
467 struct s3c2410_nand_mtd *mtd,
468 struct s3c2410_nand_set *set)
471 return add_mtd_device(&mtd->mtd);
473 if (set->nr_partitions > 0 && set->partitions != NULL) {
474 return add_mtd_partitions(&mtd->mtd, set->partitions, set->nr_partitions);
477 return add_mtd_device(&mtd->mtd);
480 static int s3c2410_nand_add_partition(struct s3c2410_nand_info *info,
481 struct s3c2410_nand_mtd *mtd,
482 struct s3c2410_nand_set *set)
484 return add_mtd_device(&mtd->mtd);
488 /* s3c2410_nand_init_chip
490 * init a single instance of an chip
493 static void s3c2410_nand_init_chip(struct s3c2410_nand_info *info,
494 struct s3c2410_nand_mtd *nmtd,
495 struct s3c2410_nand_set *set)
497 struct nand_chip *chip = &nmtd->chip;
499 chip->IO_ADDR_R = info->regs + S3C2410_NFDATA;
500 chip->IO_ADDR_W = info->regs + S3C2410_NFDATA;
501 chip->hwcontrol = s3c2410_nand_hwcontrol;
502 chip->dev_ready = s3c2410_nand_devready;
503 chip->write_buf = s3c2410_nand_write_buf;
504 chip->read_buf = s3c2410_nand_read_buf;
505 chip->select_chip = s3c2410_nand_select_chip;
506 chip->chip_delay = 50;
509 chip->controller = &info->controller;
511 if (info->is_s3c2440) {
512 chip->IO_ADDR_R = info->regs + S3C2440_NFDATA;
513 chip->IO_ADDR_W = info->regs + S3C2440_NFDATA;
514 chip->hwcontrol = s3c2440_nand_hwcontrol;
518 nmtd->mtd.priv = chip;
519 nmtd->mtd.owner = THIS_MODULE;
523 chip->ecc.correct = s3c2410_nand_correct_data;
524 chip->ecc.hwctl = s3c2410_nand_enable_hwecc;
525 chip->ecc.calculate = s3c2410_nand_calculate_ecc;
526 chip->ecc.mode = NAND_ECC_HW;
527 chip->ecc.size = 512;
529 chip->autooob = &nand_hw_eccoob;
531 if (info->is_s3c2440) {
532 chip->ecc.hwctl = s3c2440_nand_enable_hwecc;
533 chip->ecc.calculate = s3c2440_nand_calculate_ecc;
536 chip->ecc.mode = NAND_ECC_SOFT;
540 /* s3c2410_nand_probe
542 * called by device layer when it finds a device matching
543 * one our driver can handled. This code checks to see if
544 * it can allocate all necessary resources then calls the
545 * nand layer to look for devices
548 static int s3c24xx_nand_probe(struct platform_device *pdev, int is_s3c2440)
550 struct s3c2410_platform_nand *plat = to_nand_plat(pdev);
551 struct s3c2410_nand_info *info;
552 struct s3c2410_nand_mtd *nmtd;
553 struct s3c2410_nand_set *sets;
554 struct resource *res;
560 pr_debug("s3c2410_nand_probe(%p)\n", pdev);
562 info = kmalloc(sizeof(*info), GFP_KERNEL);
564 dev_err(&pdev->dev, "no memory for flash info\n");
569 memzero(info, sizeof(*info));
570 platform_set_drvdata(pdev, info);
572 spin_lock_init(&info->controller.lock);
573 init_waitqueue_head(&info->controller.wq);
575 /* get the clock source and enable it */
577 info->clk = clk_get(&pdev->dev, "nand");
578 if (IS_ERR(info->clk)) {
579 dev_err(&pdev->dev, "failed to get clock");
584 clk_enable(info->clk);
586 /* allocate and map the resource */
588 /* currently we assume we have the one resource */
589 res = pdev->resource;
590 size = res->end - res->start + 1;
592 info->area = request_mem_region(res->start, size, pdev->name);
594 if (info->area == NULL) {
595 dev_err(&pdev->dev, "cannot reserve register region\n");
600 info->device = &pdev->dev;
601 info->platform = plat;
602 info->regs = ioremap(res->start, size);
603 info->is_s3c2440 = is_s3c2440;
605 if (info->regs == NULL) {
606 dev_err(&pdev->dev, "cannot reserve register region\n");
611 dev_dbg(&pdev->dev, "mapped registers at %p\n", info->regs);
613 /* initialise the hardware */
615 err = s3c2410_nand_inithw(info, pdev);
619 sets = (plat != NULL) ? plat->sets : NULL;
620 nr_sets = (plat != NULL) ? plat->nr_sets : 1;
622 info->mtd_count = nr_sets;
624 /* allocate our information */
626 size = nr_sets * sizeof(*info->mtds);
627 info->mtds = kmalloc(size, GFP_KERNEL);
628 if (info->mtds == NULL) {
629 dev_err(&pdev->dev, "failed to allocate mtd storage\n");
634 memzero(info->mtds, size);
636 /* initialise all possible chips */
640 for (setno = 0; setno < nr_sets; setno++, nmtd++) {
641 pr_debug("initialising set %d (%p, info %p)\n", setno, nmtd, info);
643 s3c2410_nand_init_chip(info, nmtd, sets);
645 nmtd->scan_res = nand_scan(&nmtd->mtd, (sets) ? sets->nr_chips : 1);
647 if (nmtd->scan_res == 0) {
648 s3c2410_nand_add_partition(info, nmtd, sets);
655 pr_debug("initialised ok\n");
659 s3c2410_nand_remove(pdev);
666 /* driver device registration */
668 static int s3c2410_nand_probe(struct platform_device *dev)
670 return s3c24xx_nand_probe(dev, 0);
673 static int s3c2440_nand_probe(struct platform_device *dev)
675 return s3c24xx_nand_probe(dev, 1);
678 static struct platform_driver s3c2410_nand_driver = {
679 .probe = s3c2410_nand_probe,
680 .remove = s3c2410_nand_remove,
682 .name = "s3c2410-nand",
683 .owner = THIS_MODULE,
687 static struct platform_driver s3c2440_nand_driver = {
688 .probe = s3c2440_nand_probe,
689 .remove = s3c2410_nand_remove,
691 .name = "s3c2440-nand",
692 .owner = THIS_MODULE,
696 static int __init s3c2410_nand_init(void)
698 printk("S3C24XX NAND Driver, (c) 2004 Simtec Electronics\n");
700 platform_driver_register(&s3c2440_nand_driver);
701 return platform_driver_register(&s3c2410_nand_driver);
704 static void __exit s3c2410_nand_exit(void)
706 platform_driver_unregister(&s3c2440_nand_driver);
707 platform_driver_unregister(&s3c2410_nand_driver);
710 module_init(s3c2410_nand_init);
711 module_exit(s3c2410_nand_exit);
713 MODULE_LICENSE("GPL");
714 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
715 MODULE_DESCRIPTION("S3C24XX MTD NAND driver");