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>
57 #include <linux/mtd/mtd.h>
58 #include <linux/mtd/nand.h>
59 #include <linux/mtd/nand_ecc.h>
60 #include <linux/mtd/partitions.h>
63 #include <asm/hardware/clock.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 device *dev)
130 return dev_get_drvdata(dev);
133 static struct s3c2410_platform_nand *to_nand_plat(struct device *dev)
135 return 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",
163 #define to_ns(ticks,clk) (((ticks) * NS_IN_KHZ) / (unsigned int)(clk))
165 /* controller setup */
167 static int s3c2410_nand_inithw(struct s3c2410_nand_info *info,
170 struct s3c2410_platform_nand *plat = to_nand_plat(dev);
171 unsigned long clkrate = clk_get_rate(info->clk);
172 int tacls, twrph0, twrph1;
175 /* calculate the timing information for the controller */
177 clkrate /= 1000; /* turn clock into kHz for ease of use */
180 tacls = s3c2410_nand_calc_rate(plat->tacls, clkrate, 4);
181 twrph0 = s3c2410_nand_calc_rate(plat->twrph0, clkrate, 8);
182 twrph1 = s3c2410_nand_calc_rate(plat->twrph1, clkrate, 8);
184 /* default timings */
190 if (tacls < 0 || twrph0 < 0 || twrph1 < 0) {
191 printk(KERN_ERR PFX "cannot get timings suitable for board\n");
195 printk(KERN_INFO PFX "Tacls=%d, %dns Twrph0=%d %dns, Twrph1=%d %dns\n",
196 tacls, to_ns(tacls, clkrate),
197 twrph0, to_ns(twrph0, clkrate),
198 twrph1, to_ns(twrph1, clkrate));
200 if (!info->is_s3c2440) {
201 cfg = S3C2410_NFCONF_EN;
202 cfg |= S3C2410_NFCONF_TACLS(tacls-1);
203 cfg |= S3C2410_NFCONF_TWRPH0(twrph0-1);
204 cfg |= S3C2410_NFCONF_TWRPH1(twrph1-1);
206 cfg = S3C2440_NFCONF_TACLS(tacls-1);
207 cfg |= S3C2440_NFCONF_TWRPH0(twrph0-1);
208 cfg |= S3C2440_NFCONF_TWRPH1(twrph1-1);
211 pr_debug(PFX "NF_CONF is 0x%lx\n", cfg);
213 writel(cfg, info->regs + S3C2410_NFCONF);
219 static void s3c2410_nand_select_chip(struct mtd_info *mtd, int chip)
221 struct s3c2410_nand_info *info;
222 struct s3c2410_nand_mtd *nmtd;
223 struct nand_chip *this = mtd->priv;
231 bit = (info->is_s3c2440) ? S3C2440_NFCONT_nFCE : S3C2410_NFCONF_nFCE;
232 reg = info->regs+((info->is_s3c2440) ? S3C2440_NFCONT:S3C2410_NFCONF);
239 if (nmtd->set != NULL && chip > nmtd->set->nr_chips) {
240 printk(KERN_ERR PFX "chip %d out of range\n", chip);
244 if (info->platform != NULL) {
245 if (info->platform->select_chip != NULL)
246 (info->platform->select_chip)(nmtd->set, chip);
255 /* command and control functions
257 * Note, these all use tglx's method of changing the IO_ADDR_W field
258 * to make the code simpler, and use the nand layer's code to issue the
259 * command and address sequences via the proper IO ports.
263 static void s3c2410_nand_hwcontrol(struct mtd_info *mtd, int cmd)
265 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
266 struct nand_chip *chip = mtd->priv;
269 case NAND_CTL_SETNCE:
270 case NAND_CTL_CLRNCE:
271 printk(KERN_ERR "%s: called for NCE\n", __FUNCTION__);
274 case NAND_CTL_SETCLE:
275 chip->IO_ADDR_W = info->regs + S3C2410_NFCMD;
278 case NAND_CTL_SETALE:
279 chip->IO_ADDR_W = info->regs + S3C2410_NFADDR;
282 /* NAND_CTL_CLRCLE: */
283 /* NAND_CTL_CLRALE: */
285 chip->IO_ADDR_W = info->regs + S3C2410_NFDATA;
290 /* command and control functions */
292 static void s3c2440_nand_hwcontrol(struct mtd_info *mtd, int cmd)
294 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
295 struct nand_chip *chip = mtd->priv;
298 case NAND_CTL_SETNCE:
299 case NAND_CTL_CLRNCE:
300 printk(KERN_ERR "%s: called for NCE\n", __FUNCTION__);
303 case NAND_CTL_SETCLE:
304 chip->IO_ADDR_W = info->regs + S3C2440_NFCMD;
307 case NAND_CTL_SETALE:
308 chip->IO_ADDR_W = info->regs + S3C2440_NFADDR;
311 /* NAND_CTL_CLRCLE: */
312 /* NAND_CTL_CLRALE: */
314 chip->IO_ADDR_W = info->regs + S3C2440_NFDATA;
319 /* s3c2410_nand_devready()
321 * returns 0 if the nand is busy, 1 if it is ready
324 static int s3c2410_nand_devready(struct mtd_info *mtd)
326 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
328 if (info->is_s3c2440)
329 return readb(info->regs + S3C2440_NFSTAT) & S3C2440_NFSTAT_READY;
330 return readb(info->regs + S3C2410_NFSTAT) & S3C2410_NFSTAT_BUSY;
334 /* ECC handling functions */
336 static int s3c2410_nand_correct_data(struct mtd_info *mtd, u_char *dat,
337 u_char *read_ecc, u_char *calc_ecc)
339 pr_debug("s3c2410_nand_correct_data(%p,%p,%p,%p)\n",
340 mtd, dat, read_ecc, calc_ecc);
342 pr_debug("eccs: read %02x,%02x,%02x vs calc %02x,%02x,%02x\n",
343 read_ecc[0], read_ecc[1], read_ecc[2],
344 calc_ecc[0], calc_ecc[1], calc_ecc[2]);
346 if (read_ecc[0] == calc_ecc[0] &&
347 read_ecc[1] == calc_ecc[1] &&
348 read_ecc[2] == calc_ecc[2])
351 /* we curently have no method for correcting the error */
358 * These allow the s3c2410 and s3c2440 to use the controller's ECC
359 * generator block to ECC the data as it passes through]
362 static void s3c2410_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 + S3C2410_NFCONF);
368 ctrl |= S3C2410_NFCONF_INITECC;
369 writel(ctrl, info->regs + S3C2410_NFCONF);
372 static void s3c2440_nand_enable_hwecc(struct mtd_info *mtd, int mode)
374 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
377 ctrl = readl(info->regs + S3C2440_NFCONT);
378 writel(ctrl | S3C2440_NFCONT_INITECC, info->regs + S3C2440_NFCONT);
381 static int s3c2410_nand_calculate_ecc(struct mtd_info *mtd,
382 const u_char *dat, u_char *ecc_code)
384 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
386 ecc_code[0] = readb(info->regs + S3C2410_NFECC + 0);
387 ecc_code[1] = readb(info->regs + S3C2410_NFECC + 1);
388 ecc_code[2] = readb(info->regs + S3C2410_NFECC + 2);
390 pr_debug("calculate_ecc: returning ecc %02x,%02x,%02x\n",
391 ecc_code[0], ecc_code[1], ecc_code[2]);
397 static int s3c2440_nand_calculate_ecc(struct mtd_info *mtd,
398 const u_char *dat, u_char *ecc_code)
400 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
401 unsigned long ecc = readl(info->regs + S3C2440_NFMECC0);
404 ecc_code[1] = ecc >> 8;
405 ecc_code[2] = ecc >> 16;
407 pr_debug("calculate_ecc: returning ecc %02x,%02x,%02x\n",
408 ecc_code[0], ecc_code[1], ecc_code[2]);
414 /* over-ride the standard functions for a little more speed. We can
415 * use read/write block to move the data buffers to/from the controller
418 static void s3c2410_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
420 struct nand_chip *this = mtd->priv;
421 readsb(this->IO_ADDR_R, buf, len);
424 static void s3c2410_nand_write_buf(struct mtd_info *mtd,
425 const u_char *buf, int len)
427 struct nand_chip *this = mtd->priv;
428 writesb(this->IO_ADDR_W, buf, len);
431 /* device management functions */
433 static int s3c2410_nand_remove(struct device *dev)
435 struct s3c2410_nand_info *info = to_nand_info(dev);
437 dev_set_drvdata(dev, NULL);
442 /* first thing we need to do is release all our mtds
443 * and their partitions, then go through freeing the
447 if (info->mtds != NULL) {
448 struct s3c2410_nand_mtd *ptr = info->mtds;
451 for (mtdno = 0; mtdno < info->mtd_count; mtdno++, ptr++) {
452 pr_debug("releasing mtd %d (%p)\n", mtdno, ptr);
453 nand_release(&ptr->mtd);
459 /* free the common resources */
461 if (info->clk != NULL && !IS_ERR(info->clk)) {
462 clk_disable(info->clk);
463 clk_unuse(info->clk);
467 if (info->regs != NULL) {
472 if (info->area != NULL) {
473 release_resource(info->area);
483 #ifdef CONFIG_MTD_PARTITIONS
484 static int s3c2410_nand_add_partition(struct s3c2410_nand_info *info,
485 struct s3c2410_nand_mtd *mtd,
486 struct s3c2410_nand_set *set)
489 return add_mtd_device(&mtd->mtd);
491 if (set->nr_partitions > 0 && set->partitions != NULL) {
492 return add_mtd_partitions(&mtd->mtd,
497 return add_mtd_device(&mtd->mtd);
500 static int s3c2410_nand_add_partition(struct s3c2410_nand_info *info,
501 struct s3c2410_nand_mtd *mtd,
502 struct s3c2410_nand_set *set)
504 return add_mtd_device(&mtd->mtd);
508 /* s3c2410_nand_init_chip
510 * init a single instance of an chip
513 static void s3c2410_nand_init_chip(struct s3c2410_nand_info *info,
514 struct s3c2410_nand_mtd *nmtd,
515 struct s3c2410_nand_set *set)
517 struct nand_chip *chip = &nmtd->chip;
519 chip->IO_ADDR_R = info->regs + S3C2410_NFDATA;
520 chip->IO_ADDR_W = info->regs + S3C2410_NFDATA;
521 chip->hwcontrol = s3c2410_nand_hwcontrol;
522 chip->dev_ready = s3c2410_nand_devready;
523 chip->write_buf = s3c2410_nand_write_buf;
524 chip->read_buf = s3c2410_nand_read_buf;
525 chip->select_chip = s3c2410_nand_select_chip;
526 chip->chip_delay = 50;
529 chip->controller = &info->controller;
531 if (info->is_s3c2440) {
532 chip->IO_ADDR_R = info->regs + S3C2440_NFDATA;
533 chip->IO_ADDR_W = info->regs + S3C2440_NFDATA;
534 chip->hwcontrol = s3c2440_nand_hwcontrol;
538 nmtd->mtd.priv = chip;
542 chip->correct_data = s3c2410_nand_correct_data;
543 chip->enable_hwecc = s3c2410_nand_enable_hwecc;
544 chip->calculate_ecc = s3c2410_nand_calculate_ecc;
545 chip->eccmode = NAND_ECC_HW3_512;
546 chip->autooob = &nand_hw_eccoob;
548 if (info->is_s3c2440) {
549 chip->enable_hwecc = s3c2440_nand_enable_hwecc;
550 chip->calculate_ecc = s3c2440_nand_calculate_ecc;
553 chip->eccmode = NAND_ECC_SOFT;
557 /* s3c2410_nand_probe
559 * called by device layer when it finds a device matching
560 * one our driver can handled. This code checks to see if
561 * it can allocate all necessary resources then calls the
562 * nand layer to look for devices
565 static int s3c24xx_nand_probe(struct device *dev, int is_s3c2440)
567 struct platform_device *pdev = to_platform_device(dev);
568 struct s3c2410_platform_nand *plat = to_nand_plat(dev);
569 struct s3c2410_nand_info *info;
570 struct s3c2410_nand_mtd *nmtd;
571 struct s3c2410_nand_set *sets;
572 struct resource *res;
578 pr_debug("s3c2410_nand_probe(%p)\n", dev);
580 info = kmalloc(sizeof(*info), GFP_KERNEL);
582 dev_err(dev, "no memory for flash info\n");
587 memzero(info, sizeof(*info));
588 dev_set_drvdata(dev, info);
590 spin_lock_init(&info->controller.lock);
591 init_waitqueue_head(&info->controller.wq);
593 /* get the clock source and enable it */
595 info->clk = clk_get(dev, "nand");
596 if (IS_ERR(info->clk)) {
597 dev_err(dev, "failed to get clock");
603 clk_enable(info->clk);
605 /* allocate and map the resource */
607 /* currently we assume we have the one resource */
608 res = pdev->resource;
609 size = res->end - res->start + 1;
611 info->area = request_mem_region(res->start, size, pdev->name);
613 if (info->area == NULL) {
614 dev_err(dev, "cannot reserve register region\n");
620 info->platform = plat;
621 info->regs = ioremap(res->start, size);
622 info->is_s3c2440 = is_s3c2440;
624 if (info->regs == NULL) {
625 dev_err(dev, "cannot reserve register region\n");
630 dev_dbg(dev, "mapped registers at %p\n", info->regs);
632 /* initialise the hardware */
634 err = s3c2410_nand_inithw(info, dev);
638 sets = (plat != NULL) ? plat->sets : NULL;
639 nr_sets = (plat != NULL) ? plat->nr_sets : 1;
641 info->mtd_count = nr_sets;
643 /* allocate our information */
645 size = nr_sets * sizeof(*info->mtds);
646 info->mtds = kmalloc(size, GFP_KERNEL);
647 if (info->mtds == NULL) {
648 dev_err(dev, "failed to allocate mtd storage\n");
653 memzero(info->mtds, size);
655 /* initialise all possible chips */
659 for (setno = 0; setno < nr_sets; setno++, nmtd++) {
660 pr_debug("initialising set %d (%p, info %p)\n",
663 s3c2410_nand_init_chip(info, nmtd, sets);
665 nmtd->scan_res = nand_scan(&nmtd->mtd,
666 (sets) ? sets->nr_chips : 1);
668 if (nmtd->scan_res == 0) {
669 s3c2410_nand_add_partition(info, nmtd, sets);
676 pr_debug("initialised ok\n");
680 s3c2410_nand_remove(dev);
687 /* driver device registration */
689 static int s3c2410_nand_probe(struct device *dev)
691 return s3c24xx_nand_probe(dev, 0);
694 static int s3c2440_nand_probe(struct device *dev)
696 return s3c24xx_nand_probe(dev, 1);
699 static struct device_driver s3c2410_nand_driver = {
700 .name = "s3c2410-nand",
701 .owner = THIS_MODULE,
702 .bus = &platform_bus_type,
703 .probe = s3c2410_nand_probe,
704 .remove = s3c2410_nand_remove,
707 static struct device_driver s3c2440_nand_driver = {
708 .name = "s3c2440-nand",
709 .owner = THIS_MODULE,
710 .bus = &platform_bus_type,
711 .probe = s3c2440_nand_probe,
712 .remove = s3c2410_nand_remove,
715 static int __init s3c2410_nand_init(void)
717 printk("S3C24XX NAND Driver, (c) 2004 Simtec Electronics\n");
719 driver_register(&s3c2440_nand_driver);
720 return driver_register(&s3c2410_nand_driver);
723 static void __exit s3c2410_nand_exit(void)
725 driver_unregister(&s3c2440_nand_driver);
726 driver_unregister(&s3c2410_nand_driver);
729 module_init(s3c2410_nand_init);
730 module_exit(s3c2410_nand_exit);
732 MODULE_LICENSE("GPL");
733 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
734 MODULE_DESCRIPTION("S3C24XX MTD NAND driver");