/*
 *  drivers/mtd/nand/nc650.c
 *
 *  Copyright (C) 2004 DENX Software Engineering, Wolfgang Denk, wd@denx.de
 *
 *  Derived from drivers/mtd/nand/edb7312.c
 *       Copyright (C) 2002 Marius Gröger (mag@sysgo.de)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 *  Overview:
 *   This is a device driver for the NAND flash device found on the
 *   NC650 board which utilizes the Samsung KM29U256T part. This is
 *   a 256Mibit (32MiB x 8 bits) NAND flash device.
 */

#include <linux/slab.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/mtd/mtd.h>
#include <linux/mtd/nand.h>
#include <linux/mtd/partitions.h>
#include <asm/io.h>
#include <asm/delay.h>
#include <platforms/nc650.h>

/* handy sizes */
#define SZ_1K                           0x00000400



/*
 * MTD structure for NC650 board
 */
static struct mtd_info *nc650_mtd = NULL;

/* Internal buffers. Page buffer and oob buffer for one block*/
static u_char data_buf[512 + 16];
static u_char oob_buf[16 * 32];

/*
 * Module stuff
 */

static int nc650_fio_pbase = CFG_NAND_PADDR;

#ifdef MODULE
MODULE_PARM(nc650_fio_pbase, "i");

__setup("nc650_fio_pbase=",nc650_fio_pbase);
#endif

#if ((defined CONFIG_MTD_PARTITIONS) || (defined CONFIG_MTD_PARTITIONS_MODULE))
/*
 * Define static partitions for flash device
 */
static struct mtd_partition partition_info[] = {
	{ name: "NC650 Nand partition 1",
		  offset: 0,
   	          size: 4*1024*1024 },
	{ name: "NC650 Nand partition 2",
		  offset: 4*1024*1024,
   	          size: 28*1024*1024 },
};
#define NUM_PARTITIONS 2
#endif


/*
 *	hardware specific access to control-lines
 */
static void nc650_hwcontrol(struct mtd_info *mtd, int cmd)
{
	struct nand_chip *this = mtd->priv;

	switch(cmd) {
		case NAND_CTL_SETCLE:
			this->IO_ADDR_W += 2;
			break;
		case NAND_CTL_CLRCLE:
			this->IO_ADDR_W -= 2;
			break;
		case NAND_CTL_SETALE:
			this->IO_ADDR_W += 1;
			break;
		case NAND_CTL_CLRALE:
			this->IO_ADDR_W -= 1;
			break;
		case NAND_CTL_SETNCE:
		case NAND_CTL_CLRNCE:
			/* nop */
			break;
	}
}

#ifdef CONFIG_MTD_PARTITIONS
static const char *part_probes[] = { "cmdlinepart", NULL };
#endif

/*
 * Main initialization routine
 */
static int __init nc650_init (void)
{
	struct nand_chip *this;
	const char *part_type = 0;
	int mtd_parts_nb = 0;
	struct mtd_partition *mtd_parts = 0;
	int nc650_fio_base;

#ifdef CONFIG_MTD_DEBUG
	printk(  "[nc650_init]\n");
#endif

	/* Allocate memory for MTD device structure and private data */
	nc650_mtd = kmalloc(sizeof(struct mtd_info) + sizeof(struct nand_chip),
			    GFP_KERNEL);
	if (!nc650_mtd) {
		printk("Unable to allocate NC650 NAND MTD device structure.\n");
		return -ENOMEM;
	}

	/* map physical address */
	nc650_fio_base = (unsigned long)ioremap(nc650_fio_pbase, SZ_1K);
	if(!nc650_fio_base) {
		printk("ioremap NC650 NAND flash failed\n");
		kfree(nc650_mtd);
		return -EIO;
	}

	/* Get pointer to private data */
	this = (struct nand_chip *) (&nc650_mtd[1]);

	/* Initialize structures */
	memset((char *) nc650_mtd, 0, sizeof(struct mtd_info));
	memset((char *) this, 0, sizeof(struct nand_chip));

	/* Link the private data with the MTD structure */
	nc650_mtd->priv = this;

	/* insert callbacks */
	this->IO_ADDR_R = nc650_fio_base;
	this->IO_ADDR_W = nc650_fio_base;
	this->hwcontrol = nc650_hwcontrol;
        this->dev_ready = NULL;
	/* 12 us command delay time */
	this->chip_delay = 12;
	/* ECC mode */
	this->eccmode = NAND_ECC_SOFT;

	/* Set internal data buffer */
	this->data_buf = data_buf;
	this->oob_buf = oob_buf;

	/* Scan to find existence of the device */
	if (nand_scan (nc650_mtd, 1)) {
		iounmap((void *)nc650_fio_base);
		kfree (nc650_mtd);
		return -ENXIO;
	}

#ifdef CONFIG_MTD_PARTITIONS
	nc650_mtd->name = "nc650-nand";
	mtd_parts_nb = parse_mtd_partitions(nc650_mtd, part_probes, &mtd_parts, 0);
	if (mtd_parts_nb > 0)
	  part_type = "command line";
	else
	  mtd_parts_nb = 0;
#endif
	if (mtd_parts_nb == 0)
	{
#if ((defined CONFIG_MTD_PARTITIONS) || (defined CONFIG_MTD_PARTITIONS_MODULE))
		mtd_parts = partition_info;
		mtd_parts_nb = NUM_PARTITIONS;
#endif
		part_type = "static";
	}

	/* Register the partitions */
	printk(KERN_NOTICE "Using %s partition definition\n", part_type);
	add_mtd_partitions(nc650_mtd, mtd_parts, mtd_parts_nb);

	/* Return happy */
	return 0;
}
module_init(nc650_init);

/*
 * Clean up routine
 */
static void __exit nc650_cleanup (void)
{
#ifdef CONFIG_MTD_DEBUG
	printk(  "[nc650_cleanup]\n");
#endif

	/* Unregister the device */
	del_mtd_device (nc650_mtd);

	/* Free the MTD device structure */
	kfree (nc650_mtd);
}
module_exit(nc650_cleanup);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Wolfgang Denk <wd@denx.de>");
MODULE_DESCRIPTION("MTD map driver for NC650 board");
