/* 
 * drivers/ide/el392_cf_disk.c
 *
 * Based upon code written by Benedikt Spranger, (2005)Linutronix
 *
 * Maintainer : Hans Lehmann <hans.lehmann@ritter-elektronik.de>
 *
 * Copyright 2007-2008 RITTER-Elektronik <www.ritter-elektronik.de>
 *
 * This file is licensed under the terms of the GNU General Public License
 * version 2. This program is licensed "as is" without any warranty of any
 * kind, whether express or implied.
*/

#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/types.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/module.h>
#include <asm/io.h>
#include <asm/prom.h>
#include <linux/ide.h>
#include <asm/of_device.h>
#include <asm/of_platform.h>
#include <asm/mpc52xx.h>

#include "el392_cf_disk_drv.h"
#include "ritter_el392a.h"


void el392_ide_setup_ports(ide_hwif_t *hwif, void *ioaddr)
{
	hwif->hw.io_ports[IDE_DATA_OFFSET] = (unsigned long) ioaddr + DATA;
	hwif->hw.io_ports[IDE_ERROR_OFFSET] = (unsigned long) ioaddr + ERROR;
	hwif->hw.io_ports[IDE_NSECTOR_OFFSET] = (unsigned long) ioaddr + SECTOR_CNT;
	hwif->hw.io_ports[IDE_SECTOR_OFFSET] = (unsigned long) ioaddr + SECTOR_NO;
	hwif->hw.io_ports[IDE_LCYL_OFFSET] = (unsigned long) ioaddr + CYL_LOW;
	hwif->hw.io_ports[IDE_HCYL_OFFSET] = (unsigned long) ioaddr + CYL_HIGH;
	hwif->hw.io_ports[IDE_SELECT_OFFSET] = (unsigned long) ioaddr + SELECT;
	hwif->hw.io_ports[IDE_STATUS_OFFSET] = (unsigned long) ioaddr + STATUS;
	hwif->hw.io_ports[IDE_CONTROL_OFFSET] = (unsigned long) ioaddr + ALTSTAT_CTL;
}

static int __devinit el392_ata_probe(struct of_device *op, const struct of_device_id *match)
{
	int ret = -ENODEV;
	int i = 0;
	unsigned long res_start, res_length;
	void *ioaddr;
	ide_hwif_t *hwif;
	int ata_irq = NO_IRQ;

	struct resource res_mem;

	if ((ret = of_address_to_resource(op->node, 0, &res_mem)) != 0)
	{
		printk(KERN_ERR "EL392_ATA: Can not find IO mem!\n");
		goto out;
	}

	res_start = res_mem.start;
	res_length  = res_mem.end - res_mem.start + 1;
	
	if (!request_mem_region(res_start, res_length, DRIVER_NAME)) {
                printk(KERN_ERR"device busy.\n");
                ret = -EBUSY;
                goto out;
        }

	printk("Base address: %08lx, IO Size: %08lx\n",res_start, res_length); 	
	
	ioaddr = ioremap(res_start, res_length);
	if (!ioaddr) {
                printk(KERN_ERR "EL392_ATA: ioremap failed.\n");
		ret = -ENOMEM;
		goto no_mem;
        }
	printk ("Memory remapped: 0x%08lx -> 0x%08x\n", res_start, (unsigned int)ioaddr);

	while (i < MAX_HWIFS && (ide_hwifs[i].present))
		++i;
	if (i >= MAX_HWIFS) {
		printk(KERN_ERR "EL392_ATA: No IDE-slot free\n");
		return -ENODEV;
		goto no_ide;
	}
	
	hwif = &ide_hwifs[i];

	el392_ide_setup_ports(hwif, ioaddr);

	memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->hw.io_ports));

	ata_irq = irq_of_parse_and_map(op->node, 0);
	if (ata_irq == NO_IRQ) {
		printk(KERN_ERR "mpc52xx-ide: Invalid IRQ!\n");
		return -EINVAL;
	}

	hwif->drives[0].autotune = IDE_TUNE_NOAUTO;
	hwif->drives[1].autotune = IDE_TUNE_NOAUTO;
	hwif->drives[0].unmask = 1;
	hwif->drives[0].autodma = hwif->autodma;
	hwif->drives[0].no_io_32bit = 1;
	
	hwif->irq = ata_irq;
	hwif->mmio = 0;
	hwif->no_io_32bit = 1;
	hwif->atapi_dma = 0;
	hwif->ultra_mask = 0x00;
	hwif->mwdma_mask = 0x00;
	hwif->swdma_mask = 0x00;
	hwif->present = 1;
	hwif->noprobe = 0;
	hwif->sharing_irq = 0;
	hwif->no_lba48 = 1;
	hwif->no_lba48_dma = 1;
	hwif->chipset = ide_forced;	
		
	default_hwif_mmiops(hwif);
	
	dev_set_drvdata(&op->dev, hwif);	
		
	probe_hwif_init(hwif);
	printk("Wat denn hier lose\n");
	
/*	if (i == -1) {
		printk(KERN_INFO "EL392_ATA: Registering IDE failed\n");
		ret = -EIO;
		goto no_ide;
	}*/
	
//	ide_proc_register_port(hwif);
	
	printk(KERN_INFO
	  "EL392_IDE: Setup successful for %s (mem=%08lx-%08lx irq=%d), i = %d\n",
	  hwif->name, (unsigned long) res_mem.start, (unsigned long) res_mem.end, hwif->irq, i);

	ret = 0;
	goto out;

no_ide:
	iounmap(ioaddr);
no_mem:
	release_mem_region(res_start, res_length);
out:
	return 0;
}

static int __devexit el392_ata_remove(struct of_device *op)
{
	unsigned long res_start;
	unsigned long res_length;

	ide_hwif_t *hwif = dev_get_drvdata(&op->dev);

	struct resource res_mem;

	if (of_address_to_resource(op->node, 0, &res_mem) != 0)
	{
		printk(KERN_ERR "EL392_ATA: Can not find IO mem!\n");
		return -EIO;
	}

	if (!hwif) {
		printk(KERN_ERR "%s: Unable to remove device, please report.\n", DRIVER_NAME);
		return -EIO;
	}
	
	ide_unregister(hwif->index);

	iounmap((void*)hwif->hw.io_ports[0]);

        res_start = res_mem.start;
        res_length   = res_mem.end - res_mem.start;
	
	release_mem_region(res_start, res_length);
	
	return 0;
}


static int __init el392_ata_init(void)
{
	printk(KERN_INFO "IDE: EL392 IDE/ATA driver\n");
	return of_register_platform_driver(&el392_ide_of_platform_driver);
}



static void __exit el392_ata_exit(void)
{
	of_unregister_platform_driver(&el392_ide_of_platform_driver);
	printk( KERN_DEBUG "Module EL392_CF_DISK exit\n" );
}

module_init(el392_ata_init);
module_exit(el392_ata_exit);

MODULE_DESCRIPTION("EL392 CF_Card Kernel driver");
MODULE_AUTHOR("Hans Lehmann (hans.lehmann@ritter-elektronik.de)");
MODULE_LICENSE("GPL");

