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
21 * $Id: s3c2410.c,v 1.14 2005/07/06 20:05:06 bjd Exp $
23 * This program is free software; you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation; either version 2 of the License, or
26 * (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
38 #include <config/mtd/nand/s3c2410/hwecc.h>
39 #include <config/mtd/nand/s3c2410/debug.h>
41 #ifdef CONFIG_MTD_NAND_S3C2410_DEBUG
45 #include <linux/module.h>
46 #include <linux/types.h>
47 #include <linux/init.h>
48 #include <linux/kernel.h>
49 #include <linux/string.h>
50 #include <linux/ioport.h>
51 #include <linux/device.h>
52 #include <linux/delay.h>
53 #include <linux/err.h>
55 #include <linux/mtd/mtd.h>
56 #include <linux/mtd/nand.h>
57 #include <linux/mtd/nand_ecc.h>
58 #include <linux/mtd/partitions.h>
61 #include <asm/hardware/clock.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 /* new oob placement block for use with hardware ecc generation
77 static struct nand_oobinfo nand_hw_eccoob = {
78 .useecc = MTD_NANDECC_AUTOPLACE,
84 /* controller and mtd information */
86 struct s3c2410_nand_info;
88 struct s3c2410_nand_mtd {
90 struct nand_chip chip;
91 struct s3c2410_nand_set *set;
92 struct s3c2410_nand_info *info;
96 /* overview of the s3c2410 nand state */
98 struct s3c2410_nand_info {
100 struct nand_hw_control controller;
101 struct s3c2410_nand_mtd *mtds;
102 struct s3c2410_platform_nand *platform;
105 struct device *device;
106 struct resource *area;
111 unsigned char is_s3c2440;
114 /* conversion functions */
116 static struct s3c2410_nand_mtd *s3c2410_nand_mtd_toours(struct mtd_info *mtd)
118 return container_of(mtd, struct s3c2410_nand_mtd, mtd);
121 static struct s3c2410_nand_info *s3c2410_nand_mtd_toinfo(struct mtd_info *mtd)
123 return s3c2410_nand_mtd_toours(mtd)->info;
126 static struct s3c2410_nand_info *to_nand_info(struct device *dev)
128 return dev_get_drvdata(dev);
131 static struct s3c2410_platform_nand *to_nand_plat(struct device *dev)
133 return dev->platform_data;
136 /* timing calculations */
138 #define NS_IN_KHZ 10000000
140 static int s3c2410_nand_calc_rate(int wanted, unsigned long clk, int max)
144 result = (wanted * NS_IN_KHZ) / clk;
147 pr_debug("result %d from %ld, %d\n", result, clk, wanted);
150 printk("%d ns is too big for current clock rate %ld\n",
161 #define to_ns(ticks,clk) (((clk) * (ticks)) / NS_IN_KHZ)
163 /* controller setup */
165 static int s3c2410_nand_inithw(struct s3c2410_nand_info *info,
168 struct s3c2410_platform_nand *plat = to_nand_plat(dev);
169 unsigned int tacls, twrph0, twrph1;
170 unsigned long clkrate = clk_get_rate(info->clk);
173 /* calculate the timing information for the controller */
176 tacls = s3c2410_nand_calc_rate(plat->tacls, clkrate, 4);
177 twrph0 = s3c2410_nand_calc_rate(plat->twrph0, clkrate, 8);
178 twrph1 = s3c2410_nand_calc_rate(plat->twrph1, clkrate, 8);
180 /* default timings */
186 if (tacls < 0 || twrph0 < 0 || twrph1 < 0) {
187 printk(KERN_ERR PFX "cannot get timings suitable for board\n");
191 printk(KERN_INFO PFX "timing: Tacls %ldns, Twrph0 %ldns, Twrph1 %ldns\n",
192 to_ns(tacls, clkrate),
193 to_ns(twrph0, clkrate),
194 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;
330 /* ECC handling functions */
332 static int s3c2410_nand_correct_data(struct mtd_info *mtd, u_char *dat,
333 u_char *read_ecc, u_char *calc_ecc)
335 pr_debug("s3c2410_nand_correct_data(%p,%p,%p,%p)\n",
336 mtd, dat, read_ecc, calc_ecc);
338 pr_debug("eccs: read %02x,%02x,%02x vs calc %02x,%02x,%02x\n",
339 read_ecc[0], read_ecc[1], read_ecc[2],
340 calc_ecc[0], calc_ecc[1], calc_ecc[2]);
342 if (read_ecc[0] == calc_ecc[0] &&
343 read_ecc[1] == calc_ecc[1] &&
344 read_ecc[2] == calc_ecc[2])
347 /* we curently have no method for correcting the error */
354 * These allow the s3c2410 and s3c2440 to use the controller's ECC
355 * generator block to ECC the data as it passes through]
358 static void s3c2410_nand_enable_hwecc(struct mtd_info *mtd, int mode)
360 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
363 ctrl = readl(info->regs + S3C2410_NFCONF);
364 ctrl |= S3C2410_NFCONF_INITECC;
365 writel(ctrl, info->regs + S3C2410_NFCONF);
368 static void s3c2440_nand_enable_hwecc(struct mtd_info *mtd, int mode)
370 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
373 ctrl = readl(info->regs + S3C2440_NFCONT);
374 writel(ctrl | S3C2440_NFCONT_INITECC, info->regs + S3C2440_NFCONT);
377 static int s3c2410_nand_calculate_ecc(struct mtd_info *mtd,
378 const u_char *dat, u_char *ecc_code)
380 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
382 ecc_code[0] = readb(info->regs + S3C2410_NFECC + 0);
383 ecc_code[1] = readb(info->regs + S3C2410_NFECC + 1);
384 ecc_code[2] = readb(info->regs + S3C2410_NFECC + 2);
386 pr_debug("calculate_ecc: returning ecc %02x,%02x,%02x\n",
387 ecc_code[0], ecc_code[1], ecc_code[2]);
393 static int s3c2440_nand_calculate_ecc(struct mtd_info *mtd,
394 const u_char *dat, u_char *ecc_code)
396 struct s3c2410_nand_info *info = s3c2410_nand_mtd_toinfo(mtd);
397 unsigned long ecc = readl(info->regs + S3C2440_NFMECC0);
400 ecc_code[1] = ecc >> 8;
401 ecc_code[2] = ecc >> 16;
403 pr_debug("calculate_ecc: returning ecc %02x,%02x,%02x\n",
404 ecc_code[0], ecc_code[1], ecc_code[2]);
410 /* over-ride the standard functions for a little more speed. We can
411 * use read/write block to move the data buffers to/from the controller
414 static void s3c2410_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
416 struct nand_chip *this = mtd->priv;
417 readsb(this->IO_ADDR_R, buf, len);
420 static void s3c2410_nand_write_buf(struct mtd_info *mtd,
421 const u_char *buf, int len)
423 struct nand_chip *this = mtd->priv;
424 writesb(this->IO_ADDR_W, buf, len);
427 /* device management functions */
429 static int s3c2410_nand_remove(struct device *dev)
431 struct s3c2410_nand_info *info = to_nand_info(dev);
433 dev_set_drvdata(dev, NULL);
438 /* first thing we need to do is release all our mtds
439 * and their partitions, then go through freeing the
443 if (info->mtds != NULL) {
444 struct s3c2410_nand_mtd *ptr = info->mtds;
447 for (mtdno = 0; mtdno < info->mtd_count; mtdno++, ptr++) {
448 pr_debug("releasing mtd %d (%p)\n", mtdno, ptr);
449 nand_release(&ptr->mtd);
455 /* free the common resources */
457 if (info->clk != NULL && !IS_ERR(info->clk)) {
458 clk_disable(info->clk);
459 clk_unuse(info->clk);
463 if (info->regs != NULL) {
468 if (info->area != NULL) {
469 release_resource(info->area);
479 #ifdef CONFIG_MTD_PARTITIONS
480 static int s3c2410_nand_add_partition(struct s3c2410_nand_info *info,
481 struct s3c2410_nand_mtd *mtd,
482 struct s3c2410_nand_set *set)
485 return add_mtd_device(&mtd->mtd);
487 if (set->nr_partitions > 0 && set->partitions != NULL) {
488 return add_mtd_partitions(&mtd->mtd,
493 return add_mtd_device(&mtd->mtd);
496 static int s3c2410_nand_add_partition(struct s3c2410_nand_info *info,
497 struct s3c2410_nand_mtd *mtd,
498 struct s3c2410_nand_set *set)
500 return add_mtd_device(&mtd->mtd);
504 /* s3c2410_nand_init_chip
506 * init a single instance of an chip
509 static void s3c2410_nand_init_chip(struct s3c2410_nand_info *info,
510 struct s3c2410_nand_mtd *nmtd,
511 struct s3c2410_nand_set *set)
513 struct nand_chip *chip = &nmtd->chip;
515 chip->IO_ADDR_R = info->regs + S3C2410_NFDATA;
516 chip->IO_ADDR_W = info->regs + S3C2410_NFDATA;
517 chip->hwcontrol = s3c2410_nand_hwcontrol;
518 chip->dev_ready = s3c2410_nand_devready;
519 chip->write_buf = s3c2410_nand_write_buf;
520 chip->read_buf = s3c2410_nand_read_buf;
521 chip->select_chip = s3c2410_nand_select_chip;
522 chip->chip_delay = 50;
525 chip->controller = &info->controller;
527 if (info->is_s3c2440) {
528 chip->IO_ADDR_R = info->regs + S3C2440_NFDATA;
529 chip->IO_ADDR_W = info->regs + S3C2440_NFDATA;
530 chip->hwcontrol = s3c2440_nand_hwcontrol;
534 nmtd->mtd.priv = chip;
538 chip->correct_data = s3c2410_nand_correct_data;
539 chip->enable_hwecc = s3c2410_nand_enable_hwecc;
540 chip->calculate_ecc = s3c2410_nand_calculate_ecc;
541 chip->eccmode = NAND_ECC_HW3_512;
542 chip->autooob = &nand_hw_eccoob;
544 if (info->is_s3c2440) {
545 chip->enable_hwecc = s3c2440_nand_enable_hwecc;
546 chip->calculate_ecc = s3c2440_nand_calculate_ecc;
549 chip->eccmode = NAND_ECC_SOFT;
553 /* s3c2410_nand_probe
555 * called by device layer when it finds a device matching
556 * one our driver can handled. This code checks to see if
557 * it can allocate all necessary resources then calls the
558 * nand layer to look for devices
561 static int s3c24xx_nand_probe(struct device *dev, int is_s3c2440)
563 struct platform_device *pdev = to_platform_device(dev);
564 struct s3c2410_platform_nand *plat = to_nand_plat(dev);
565 struct s3c2410_nand_info *info;
566 struct s3c2410_nand_mtd *nmtd;
567 struct s3c2410_nand_set *sets;
568 struct resource *res;
574 pr_debug("s3c2410_nand_probe(%p)\n", dev);
576 info = kmalloc(sizeof(*info), GFP_KERNEL);
578 printk(KERN_ERR PFX "no memory for flash info\n");
583 memzero(info, sizeof(*info));
584 dev_set_drvdata(dev, info);
586 spin_lock_init(&info->controller.lock);
587 init_waitqueue_head(&info->controller.wq);
589 /* get the clock source and enable it */
591 info->clk = clk_get(dev, "nand");
592 if (IS_ERR(info->clk)) {
593 printk(KERN_ERR PFX "failed to get clock");
599 clk_enable(info->clk);
601 /* allocate and map the resource */
603 /* currently we assume we have the one resource */
604 res = pdev->resource;
605 size = res->end - res->start + 1;
607 info->area = request_mem_region(res->start, size, pdev->name);
609 if (info->area == NULL) {
610 printk(KERN_ERR PFX "cannot reserve register region\n");
616 info->platform = plat;
617 info->regs = ioremap(res->start, size);
618 info->is_s3c2440 = is_s3c2440;
620 if (info->regs == NULL) {
621 printk(KERN_ERR PFX "cannot reserve register region\n");
626 printk(KERN_INFO PFX "mapped registers at %p\n", info->regs);
628 /* initialise the hardware */
630 err = s3c2410_nand_inithw(info, dev);
634 sets = (plat != NULL) ? plat->sets : NULL;
635 nr_sets = (plat != NULL) ? plat->nr_sets : 1;
637 info->mtd_count = nr_sets;
639 /* allocate our information */
641 size = nr_sets * sizeof(*info->mtds);
642 info->mtds = kmalloc(size, GFP_KERNEL);
643 if (info->mtds == NULL) {
644 printk(KERN_ERR PFX "failed to allocate mtd storage\n");
649 memzero(info->mtds, size);
651 /* initialise all possible chips */
655 for (setno = 0; setno < nr_sets; setno++, nmtd++) {
656 pr_debug("initialising set %d (%p, info %p)\n",
659 s3c2410_nand_init_chip(info, nmtd, sets);
661 nmtd->scan_res = nand_scan(&nmtd->mtd,
662 (sets) ? sets->nr_chips : 1);
664 if (nmtd->scan_res == 0) {
665 s3c2410_nand_add_partition(info, nmtd, sets);
672 pr_debug("initialised ok\n");
676 s3c2410_nand_remove(dev);
683 /* driver device registration */
685 static int s3c2410_nand_probe(struct device *dev)
687 return s3c24xx_nand_probe(dev, 0);
690 static int s3c2440_nand_probe(struct device *dev)
692 return s3c24xx_nand_probe(dev, 1);
695 static struct device_driver s3c2410_nand_driver = {
696 .name = "s3c2410-nand",
697 .bus = &platform_bus_type,
698 .probe = s3c2410_nand_probe,
699 .remove = s3c2410_nand_remove,
702 static struct device_driver s3c2440_nand_driver = {
703 .name = "s3c2440-nand",
704 .bus = &platform_bus_type,
705 .probe = s3c2440_nand_probe,
706 .remove = s3c2410_nand_remove,
709 static int __init s3c2410_nand_init(void)
711 printk("S3C24XX NAND Driver, (c) 2004 Simtec Electronics\n");
713 driver_register(&s3c2440_nand_driver);
714 return driver_register(&s3c2410_nand_driver);
717 static void __exit s3c2410_nand_exit(void)
719 driver_unregister(&s3c2440_nand_driver);
720 driver_unregister(&s3c2410_nand_driver);
723 module_init(s3c2410_nand_init);
724 module_exit(s3c2410_nand_exit);
726 MODULE_LICENSE("GPL");
727 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
728 MODULE_DESCRIPTION("S3C24XX MTD NAND driver");