ide: add ide_device_add()
[cascardo/linux.git] / drivers / ide / legacy / ide_platform.c
1 /*
2  * Platform IDE driver
3  *
4  * Copyright (C) 2007 MontaVista Software
5  *
6  * Maintainer: Kumar Gala <galak@kernel.crashing.org>
7  *
8  * This program is free software; you can redistribute  it and/or modify it
9  * under  the terms of  the GNU General  Public License as published by the
10  * Free Software Foundation;  either version 2 of the  License, or (at your
11  * option) any later version.
12  */
13
14 #include <linux/types.h>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/ide.h>
18 #include <linux/ioport.h>
19 #include <linux/module.h>
20 #include <linux/pata_platform.h>
21 #include <linux/platform_device.h>
22 #include <linux/io.h>
23
24 static struct {
25         void __iomem *plat_ide_mapbase;
26         void __iomem *plat_ide_alt_mapbase;
27         ide_hwif_t *hwif;
28         int index;
29 } hwif_prop;
30
31 static ide_hwif_t *__devinit plat_ide_locate_hwif(void __iomem *base,
32             void __iomem *ctrl, struct pata_platform_info *pdata, int irq,
33             int mmio)
34 {
35         unsigned long port = (unsigned long)base;
36         ide_hwif_t *hwif;
37         int index, i;
38
39         for (index = 0; index < MAX_HWIFS; ++index) {
40                 hwif = ide_hwifs + index;
41                 if (hwif->io_ports[IDE_DATA_OFFSET] == port)
42                         goto found;
43         }
44
45         for (index = 0; index < MAX_HWIFS; ++index) {
46                 hwif = ide_hwifs + index;
47                 if (hwif->io_ports[IDE_DATA_OFFSET] == 0)
48                         goto found;
49         }
50
51         return NULL;
52
53 found:
54
55         hwif->hw.io_ports[IDE_DATA_OFFSET] = port;
56
57         port += (1 << pdata->ioport_shift);
58         for (i = IDE_ERROR_OFFSET; i <= IDE_STATUS_OFFSET;
59              i++, port += (1 << pdata->ioport_shift))
60                 hwif->hw.io_ports[i] = port;
61
62         hwif->hw.io_ports[IDE_CONTROL_OFFSET] = (unsigned long)ctrl;
63
64         memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->hw.io_ports));
65         hwif->hw.irq = hwif->irq = irq;
66
67         hwif->hw.dma = NO_DMA;
68         hwif->chipset = hwif->hw.chipset = ide_generic;
69
70         if (mmio) {
71                 hwif->mmio = 1;
72                 default_hwif_mmiops(hwif);
73         }
74
75         hwif_prop.hwif = hwif;
76         hwif_prop.index = index;
77
78         return hwif;
79 }
80
81 static int __devinit plat_ide_probe(struct platform_device *pdev)
82 {
83         struct resource *res_base, *res_alt, *res_irq;
84         ide_hwif_t *hwif;
85         struct pata_platform_info *pdata;
86         u8 idx[4] = { 0xff, 0xff, 0xff, 0xff };
87         int ret = 0;
88         int mmio = 0;
89
90         pdata = pdev->dev.platform_data;
91
92         /* get a pointer to the register memory */
93         res_base = platform_get_resource(pdev, IORESOURCE_IO, 0);
94         res_alt = platform_get_resource(pdev, IORESOURCE_IO, 1);
95
96         if (!res_base || !res_alt) {
97                 res_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
98                 res_alt = platform_get_resource(pdev, IORESOURCE_MEM, 1);
99                 if (!res_base || !res_alt) {
100                         ret = -ENOMEM;
101                         goto out;
102                 }
103                 mmio = 1;
104         }
105
106         res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
107         if (!res_irq) {
108                 ret = -EINVAL;
109                 goto out;
110         }
111
112         if (mmio) {
113                 hwif_prop.plat_ide_mapbase = devm_ioremap(&pdev->dev,
114                         res_base->start, res_base->end - res_base->start + 1);
115                 hwif_prop.plat_ide_alt_mapbase = devm_ioremap(&pdev->dev,
116                         res_alt->start, res_alt->end - res_alt->start + 1);
117         } else {
118                 hwif_prop.plat_ide_mapbase = devm_ioport_map(&pdev->dev,
119                         res_base->start, res_base->end - res_base->start + 1);
120                 hwif_prop.plat_ide_alt_mapbase = devm_ioport_map(&pdev->dev,
121                         res_alt->start, res_alt->end - res_alt->start + 1);
122         }
123
124         hwif = plat_ide_locate_hwif(hwif_prop.plat_ide_mapbase,
125                  hwif_prop.plat_ide_alt_mapbase, pdata, res_irq->start, mmio);
126
127         if (!hwif) {
128                 ret = -ENODEV;
129                 goto out;
130         }
131         hwif->gendev.parent = &pdev->dev;
132         hwif->noprobe = 0;
133
134         idx[0] = hwif->index;
135
136         ide_device_add(idx);
137
138         platform_set_drvdata(pdev, hwif);
139
140         return 0;
141
142 out:
143         return ret;
144 }
145
146 static int __devexit plat_ide_remove(struct platform_device *pdev)
147 {
148         ide_hwif_t *hwif = pdev->dev.driver_data;
149
150         if (hwif != hwif_prop.hwif) {
151                 dev_printk(KERN_DEBUG, &pdev->dev, "%s: hwif value error",
152                            pdev->name);
153         } else {
154                 ide_unregister(hwif_prop.index);
155                 hwif_prop.index = 0;
156                 hwif_prop.hwif = NULL;
157         }
158
159         return 0;
160 }
161
162 static struct platform_driver platform_ide_driver = {
163         .driver = {
164                 .name = "pata_platform",
165         },
166         .probe = plat_ide_probe,
167         .remove = __devexit_p(plat_ide_remove),
168 };
169
170 static int __init platform_ide_init(void)
171 {
172         return platform_driver_register(&platform_ide_driver);
173 }
174
175 static void __exit platform_ide_exit(void)
176 {
177         platform_driver_unregister(&platform_ide_driver);
178 }
179
180 MODULE_DESCRIPTION("Platform IDE driver");
181 MODULE_LICENSE("GPL");
182
183 module_init(platform_ide_init);
184 module_exit(platform_ide_exit);