/*
 * Handle mapping of the flash memory access routines 
 * on MATRIX VISION mvBLUE devices.
 *
 * $Id: mvblue_mtd.c 1972 2005-07-04 14:36:40Z hg $
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * based on rpxlite.c:
 * Copyright(C) 2001 Kirk Lee <kirk@hpc.ee.ntu.edu.tw>
 * this version:
 * Copyright(C) 2002-2005 MATRIX VISION GmbH <info@matrix-vision.de>
 *
 * 
 */


#include <linux/config.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <asm/io.h>

#include <linux/mtd/mtd.h>
#include <linux/mtd/map.h>
#include <linux/mtd/partitions.h>

#define FLASH_ADDR 0xffc00000
#define FLASH_SIZE 0x00400000
#define FLASH_BANK_MAX 1


// trivial struct to describe partition information
struct mtd_part_def
{
	int nums;
	unsigned char *type;
	struct mtd_partition* mtd_part;
};

//static struct mtd_info *mymtd;
static struct mtd_info* mtd_banks[FLASH_BANK_MAX];
static struct map_info* map_banks[FLASH_BANK_MAX];
static struct mtd_part_def part_banks[FLASH_BANK_MAX];
static unsigned long num_banks;
static unsigned long start_scan_addr;


#ifdef CONFIG_MTD_COMPLEX_MAPPINGS
__u8 mvBLUE_read8(struct map_info *map, unsigned long ofs)
{
	return *((__u8 *)(map->map_priv_1 + ofs));
}

__u16 mvBLUE_read16(struct map_info *map, unsigned long ofs)
{
	return *((__u16 *)(map->map_priv_1 + ofs));
}

__u32 mvBLUE_read32(struct map_info *map, unsigned long ofs)
{
	return *((__u32 *)(map->map_priv_1 + ofs));
}

void mvBLUE_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
{
	memcpy_fromio(to, (void *)(map->map_priv_1 + from), len);
}

void mvBLUE_write8(struct map_info *map, __u8 d, unsigned long adr)
{
	*((__u8 *)(map->map_priv_1 + adr)) = d;
}

void mvBLUE_write16(struct map_info *map, __u16 d, unsigned long adr)
{
	*((__u16 *)( map->map_priv_1 + adr)) = d;
}

void mvBLUE_write32(struct map_info *map, __u32 d, unsigned long adr)
{
	*((__u32 *)(map->map_priv_1 + adr)) = d;
}

void mvBLUE_copy_to(struct map_info *map, unsigned long to, const void *from, ssize_t len)
{
	memcpy_toio((void *)(map->map_priv_1 + to), from, len);
}

#endif

/*
 * Here are partition information for all known mvBLUE series devices.
 * See include/linux/mtd/partitions.h for definition of the mtd_partition
 * structure.
 * 
 * The *_max_flash_size is the maximum possible mapped flash size which
 * is not necessarily the actual flash size.  It must correspond to the 
 * value specified in the mapping definition defined by the
 * "struct map_desc *_io_desc" for the corresponding machine.
 */

#ifdef CONFIG_MTD_PARTITIONS
/* partition definition for first flash bank
 * also ref. to "drivers\char\flash_config.c" 
 */
static struct mtd_partition mvBLUE_partitions[] = {
	{
	  .name = 	"u-boot",
	  .offset =	0x00300000,
	  .size = 	0x00040000,			/* 256 kB */   
	},
	{
	  .name = 	"kernel",            
#ifdef CONFIG_MVBLUE_M
	  .offset =	0x00002000,
	  .size = 	0x000fe000,			/* nearly 1 MB */
#else
	  .offset =	0x00000000,
	  .size = 	0x00100000,			/* exactly 1 MB */
#endif
	},
	{
	  .name = 	"safe_root",           
	  .offset =	0x00100000,
	  .size = 	0x00200000,			/* 2 MB */
//	  mask_flags: MTD_WRITEABLE,    /* force read-only */
	},
	{
	  .name = 	"var",			
	  .offset =	0x00380000,
	  .size = 	0x00080000,			/* 512 kB */
	},
    {
      .name =   "ppcboot_env",
#ifdef CONFIG_MVBLUE_M
      .offset = 0x00000000,
      .size =   0x00002000,         /* 8 kB */
#else
      .offset = 0x00320000,
      .size =   0x00010000,         /* 64 kB */
#endif
    },
};
#endif

#define NB_OF(x)  (sizeof(x)/sizeof(x[0]))


int __init init_mvBLUE_mtd(void)
{
	int idx = 0, ret = 0;
	unsigned long flash_addr, flash_size, mtd_size = 0;


	flash_addr = FLASH_ADDR; 
	flash_size = FLASH_SIZE;
				
	//request maximum flash size address space
	start_scan_addr = (unsigned long)ioremap (flash_addr, flash_size);
	if (!start_scan_addr) {
		printk("%s:Failed to ioremap address:0x%x\n", __FUNCTION__, flash_addr);
		return -EIO;
	}

	for (idx = 0 ; idx < FLASH_BANK_MAX ; idx++) {
		if(mtd_size >= flash_size)
			break;
		
		printk(KERN_INFO "%s: chip probing count %d\n", __FUNCTION__, idx);
		
		map_banks[idx] = (struct map_info *)kmalloc(sizeof(struct map_info), GFP_KERNEL);
		if(map_banks[idx] == NULL) {
			ret = -ENOMEM;
			/* FIXME: What if some MTD devices were probed already? */
			goto error_mem;
		}

		memset((void *)map_banks[idx], 0, sizeof(struct map_info));
		map_banks[idx]->name = (char *)kmalloc(16, GFP_KERNEL);

		if (!map_banks[idx]->name) {
			ret = -ENOMEM;
			/* FIXME: What if some MTD devices were probed already? */
			goto error_mem;
		}
		sprintf(map_banks[idx]->name, "mvBLUE%d", idx);

		map_banks[idx]->size = flash_size;
		map_banks[idx]->bankwidth = 1;
// ** HG		map_banks[idx]->buswidth = 1;
#ifdef CONFIG_MTD_COMPLEX_MAPPINGS
		map_banks[idx]->read8 = mvBLUE_read8;
		map_banks[idx]->read16 = mvBLUE_read16;
		map_banks[idx]->read32 = mvBLUE_read32;
		map_banks[idx]->copy_from = mvBLUE_copy_from;
		map_banks[idx]->write8 = mvBLUE_write8;
		map_banks[idx]->write16 = mvBLUE_write16;
		map_banks[idx]->write32 = mvBLUE_write32;
		map_banks[idx]->copy_to = mvBLUE_copy_to;

		map_banks[idx]->map_priv_1 =
			start_scan_addr + ((idx > 0) ?
			(mtd_banks[idx-1] ? mtd_banks[idx-1]->size : 0) : 0);
#else
		simple_map_init(map_banks[idx]);
		map_banks[idx]->virt = start_scan_addr;
		map_banks[idx]->phys = flash_addr;

		if (idx && mtd_banks[idx-1]) {
			map_banks[idx]->virt += mtd_banks[idx-1]->size;
			map_banks[idx]->phys += mtd_banks[idx-1]->size;
		}
#endif
		mtd_banks[idx] = do_map_probe("cfi_probe", map_banks[idx]);

		if (mtd_banks[idx]) {
			mtd_banks[idx]->owner = THIS_MODULE;
			mtd_size += mtd_banks[idx]->size;
			num_banks++;

			printk(KERN_INFO "%s: bank%ld, name:%s, size:%dbytes \n", __FUNCTION__, num_banks, 
				mtd_banks[idx]->name, mtd_banks[idx]->size);
		}
	}

	if (!num_banks) {
		printk(KERN_NOTICE "mvBLUE: No support flash chips found!\n");
		ret = -ENXIO;
		goto error_mem;
	}

#ifdef CONFIG_MTD_PARTITIONS
	/*
	 * Select Static partition definitions
	 */
	part_banks[0].mtd_part = mvBLUE_partitions;
	part_banks[0].type = "Static image";
	part_banks[0].nums = NB_OF(mvBLUE_partitions);

	for(idx = 0; idx < num_banks ; idx++) {
		if (part_banks[idx].nums == 0) {
			printk(KERN_NOTICE "mvBLUE flash%d: no partition info available, registering whole flash at once\n", idx);
			add_mtd_device(mtd_banks[idx]);
		} else {
			printk(KERN_NOTICE "mvBLUE flash%d: Using %s partition definition\n",
					idx, part_banks[idx].type);
			add_mtd_partitions(mtd_banks[idx], part_banks[idx].mtd_part, 
								part_banks[idx].nums);
		}
	}
#else
	printk(KERN_NOTICE "mvBLUE flash: registering %d whole flash banks at once\n", num_banks);
	for(idx = 0 ; idx < num_banks ; idx++)
		add_mtd_device(mtd_banks[idx]);
#endif
	return 0;
error_mem:
	for(idx = 0 ; idx < FLASH_BANK_MAX ; idx++) {
		if(map_banks[idx] != NULL) {
			if(map_banks[idx]->name != NULL) {
				kfree(map_banks[idx]->name);
				map_banks[idx]->name = NULL;
			}
			kfree(map_banks[idx]);
			map_banks[idx] = NULL;
		}
	}
	iounmap((void *)start_scan_addr);
	return ret;
}

static void __exit cleanup_mvBLUE_mtd(void)
{
	unsigned int idx = 0;
	for(idx = 0 ; idx < num_banks ; idx++) {
		/* destroy mtd_info previously allocated */
		if (mtd_banks[idx]) {
			del_mtd_partitions(mtd_banks[idx]);
			map_destroy(mtd_banks[idx]);
		}
		/* release map_info not used anymore */
		kfree(map_banks[idx]->name);
		kfree(map_banks[idx]);
	}

	if (start_scan_addr) {
		iounmap((void *)start_scan_addr);
		start_scan_addr = 0;
	}
}

module_init(init_mvBLUE_mtd);
module_exit(cleanup_mvBLUE_mtd);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("MATRIX VISION GmbH <info@matrix-vision.de>");
MODULE_DESCRIPTION("MTD map driver for mvBLUE devices");
