ide_platform.c 3.4 KB
Newer Older
A
Anton Vorontsov 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Platform IDE driver
 *
 * Copyright (C) 2007 MontaVista Software
 *
 * Maintainer: Kumar Gala <galak@kernel.crashing.org>
 *
 * 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.
 */

#include <linux/types.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/ide.h>
#include <linux/ioport.h>
#include <linux/module.h>
#include <linux/pata_platform.h>
#include <linux/platform_device.h>
#include <linux/io.h>

24 25 26 27 28
static void __devinit plat_ide_setup_ports(hw_regs_t *hw,
					   void __iomem *base,
					   void __iomem *ctrl,
					   struct pata_platform_info *pdata,
					   int irq)
A
Anton Vorontsov 已提交
29 30
{
	unsigned long port = (unsigned long)base;
31
	int i;
A
Anton Vorontsov 已提交
32

33
	hw->io_ports[IDE_DATA_OFFSET] = port;
A
Anton Vorontsov 已提交
34 35 36 37

	port += (1 << pdata->ioport_shift);
	for (i = IDE_ERROR_OFFSET; i <= IDE_STATUS_OFFSET;
	     i++, port += (1 << pdata->ioport_shift))
38
		hw->io_ports[i] = port;
A
Anton Vorontsov 已提交
39

40
	hw->io_ports[IDE_CONTROL_OFFSET] = (unsigned long)ctrl;
A
Anton Vorontsov 已提交
41

42
	hw->irq = irq;
A
Anton Vorontsov 已提交
43

44
	hw->chipset = ide_generic;
A
Anton Vorontsov 已提交
45 46 47 48 49
}

static int __devinit plat_ide_probe(struct platform_device *pdev)
{
	struct resource *res_base, *res_alt, *res_irq;
50
	void __iomem *base, *alt_base;
A
Anton Vorontsov 已提交
51 52
	ide_hwif_t *hwif;
	struct pata_platform_info *pdata;
53
	u8 idx[4] = { 0xff, 0xff, 0xff, 0xff };
A
Anton Vorontsov 已提交
54 55
	int ret = 0;
	int mmio = 0;
56
	hw_regs_t hw;
A
Anton Vorontsov 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

	pdata = pdev->dev.platform_data;

	/* get a pointer to the register memory */
	res_base = platform_get_resource(pdev, IORESOURCE_IO, 0);
	res_alt = platform_get_resource(pdev, IORESOURCE_IO, 1);

	if (!res_base || !res_alt) {
		res_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
		res_alt = platform_get_resource(pdev, IORESOURCE_MEM, 1);
		if (!res_base || !res_alt) {
			ret = -ENOMEM;
			goto out;
		}
		mmio = 1;
	}

	res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
	if (!res_irq) {
		ret = -EINVAL;
		goto out;
	}

	if (mmio) {
81
		base = devm_ioremap(&pdev->dev,
A
Anton Vorontsov 已提交
82
			res_base->start, res_base->end - res_base->start + 1);
83
		alt_base = devm_ioremap(&pdev->dev,
A
Anton Vorontsov 已提交
84 85
			res_alt->start, res_alt->end - res_alt->start + 1);
	} else {
86
		base = devm_ioport_map(&pdev->dev,
A
Anton Vorontsov 已提交
87
			res_base->start, res_base->end - res_base->start + 1);
88
		alt_base = devm_ioport_map(&pdev->dev,
A
Anton Vorontsov 已提交
89 90 91
			res_alt->start, res_alt->end - res_alt->start + 1);
	}

92
	hwif = ide_find_port((unsigned long)base);
A
Anton Vorontsov 已提交
93 94 95 96
	if (!hwif) {
		ret = -ENODEV;
		goto out;
	}
97 98

	memset(&hw, 0, sizeof(hw));
99
	plat_ide_setup_ports(&hw, base, alt_base, pdata, res_irq->start);
100 101 102 103 104 105 106 107 108
	hw.dev = &pdev->dev;

	ide_init_port_hw(hwif, &hw);

	if (mmio) {
		hwif->mmio = 1;
		default_hwif_mmiops(hwif);
	}

109 110 111
	idx[0] = hwif->index;

	ide_device_add(idx);
A
Anton Vorontsov 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124

	platform_set_drvdata(pdev, hwif);

	return 0;

out:
	return ret;
}

static int __devexit plat_ide_remove(struct platform_device *pdev)
{
	ide_hwif_t *hwif = pdev->dev.driver_data;

125
	ide_unregister(hwif->index);
A
Anton Vorontsov 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152

	return 0;
}

static struct platform_driver platform_ide_driver = {
	.driver = {
		.name = "pata_platform",
	},
	.probe = plat_ide_probe,
	.remove = __devexit_p(plat_ide_remove),
};

static int __init platform_ide_init(void)
{
	return platform_driver_register(&platform_ide_driver);
}

static void __exit platform_ide_exit(void)
{
	platform_driver_unregister(&platform_ide_driver);
}

MODULE_DESCRIPTION("Platform IDE driver");
MODULE_LICENSE("GPL");

module_init(platform_ide_init);
module_exit(platform_ide_exit);