]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - sys/dev/ata/ata-cbus.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / sys / dev / ata / ata-cbus.c
1 /*-
2  * Copyright (c) 2002 - 2008 Søren Schmidt <sos@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification, immediately at the beginning of the file.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/ata.h>
34 #include <sys/bus.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/conf.h>
38 #include <sys/sema.h>
39 #include <sys/taskqueue.h>
40 #include <vm/uma.h>
41 #include <machine/resource.h>
42 #include <machine/bus.h>
43 #include <sys/rman.h>
44 #include <isa/isavar.h>
45 #include <dev/ata/ata-all.h>
46 #include <ata_if.h>
47
48 /* local vars */
49 struct ata_cbus_controller {
50     struct resource *io;
51     struct resource *ctlio;
52     struct resource *bankio;
53     struct resource *irq;
54     void *ih;
55     int channels;
56     struct {
57         void (*function)(void *);
58         void *argument;
59     } interrupt[2];
60 };
61
62 /* local prototypes */
63 static void ata_cbus_intr(void *);
64
65 static int
66 ata_cbus_probe(device_t dev)
67 {
68     struct resource *io;
69     int rid;
70     u_long tmp;
71
72     /* dont probe PnP devices */
73     if (isa_get_vendorid(dev))
74         return (ENXIO);
75
76     /* allocate the ioport range */
77     rid = ATA_IOADDR_RID;
78     if (!(io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
79                                   ATA_PC98_IOSIZE, RF_ACTIVE)))
80         return ENOMEM;
81
82     /* calculate & set the altport range */
83     rid = ATA_PC98_CTLADDR_RID;
84     if (bus_get_resource(dev, SYS_RES_IOPORT, rid, &tmp, &tmp)) {
85         bus_set_resource(dev, SYS_RES_IOPORT, rid,
86                          rman_get_start(io)+ATA_PC98_CTLOFFSET, ATA_CTLIOSIZE);
87     }
88
89     /* calculate & set the bank range */
90     rid = ATA_PC98_BANKADDR_RID;
91     if (bus_get_resource(dev, SYS_RES_IOPORT, rid, &tmp, &tmp)) {
92         bus_set_resource(dev, SYS_RES_IOPORT, rid,
93                          ATA_PC98_BANK, ATA_PC98_BANKIOSIZE);
94     }
95
96     bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
97     return 0;
98 }
99
100 static int
101 ata_cbus_attach(device_t dev)
102 {
103     struct ata_cbus_controller *ctlr = device_get_softc(dev);
104     device_t child;
105     int rid, unit;
106
107     /* allocate resources */
108     rid = ATA_IOADDR_RID;
109     if (!(ctlr->io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
110                                         ATA_PC98_IOSIZE, RF_ACTIVE)))
111        return ENOMEM;
112
113     rid = ATA_PC98_CTLADDR_RID;
114     if (!(ctlr->ctlio = 
115           bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
116                              rman_get_start(ctlr->io) + ATA_PC98_CTLOFFSET, ~0,
117                              ATA_CTLIOSIZE, RF_ACTIVE))) {
118         bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ctlr->io);
119         return ENOMEM;
120     }
121
122     rid = ATA_PC98_BANKADDR_RID;
123     if (!(ctlr->bankio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
124                                             ATA_PC98_BANK, ~0,
125                                             ATA_PC98_BANKIOSIZE, RF_ACTIVE))) {
126         bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ctlr->io);
127         bus_release_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID, ctlr->ctlio);
128         return ENOMEM;
129     }
130
131     rid = ATA_IRQ_RID;
132     if (!(ctlr->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
133                                              RF_ACTIVE | RF_SHAREABLE))) {
134         device_printf(dev, "unable to alloc interrupt\n");
135         bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ctlr->io);
136         bus_release_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID, ctlr->ctlio);
137         bus_release_resource(dev, SYS_RES_IOPORT, 
138                              ATA_PC98_BANKADDR_RID, ctlr->bankio);
139         return ENXIO;
140     }
141
142     if ((bus_setup_intr(dev, ctlr->irq, ATA_INTR_FLAGS,
143                         NULL, ata_cbus_intr, ctlr, &ctlr->ih))) {
144         device_printf(dev, "unable to setup interrupt\n");
145         bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ctlr->io);
146         bus_release_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID, ctlr->ctlio);
147         bus_release_resource(dev, SYS_RES_IOPORT, 
148                              ATA_PC98_BANKADDR_RID, ctlr->bankio);
149         bus_release_resource(dev, SYS_RES_IOPORT, ATA_IRQ_RID, ctlr->irq);
150         return ENXIO;
151     }
152
153         /* Work around the lack of channel serialization in ATA_CAM. */
154         ctlr->channels = 1;
155         device_printf(dev, "second channel ignored\n");
156
157     for (unit = 0; unit < ctlr->channels; unit++) {
158         child = device_add_child(dev, "ata", unit);
159         if (child == NULL)
160             device_printf(dev, "failed to add ata child device\n");
161         else
162             device_set_ivars(child, (void *)(intptr_t)unit);
163     }
164
165     bus_generic_attach(dev);
166     return (0);
167 }
168
169 static struct resource *
170 ata_cbus_alloc_resource(device_t dev, device_t child, int type, int *rid,
171                         u_long start, u_long end, u_long count, u_int flags)
172 {
173     struct ata_cbus_controller *ctlr = device_get_softc(dev);
174
175     if (type == SYS_RES_IOPORT) {
176         switch (*rid) {
177         case ATA_IOADDR_RID:
178             return ctlr->io;
179         case ATA_CTLADDR_RID:
180             return ctlr->ctlio;
181         }
182     }
183     if (type == SYS_RES_IRQ)
184         return ctlr->irq;
185     return 0;
186 }
187
188 static int
189 ata_cbus_setup_intr(device_t dev, device_t child, struct resource *irq,
190                int flags, driver_filter_t *filter, driver_intr_t *intr, 
191                void *arg, void **cookiep)
192 {
193     struct ata_cbus_controller *controller = device_get_softc(dev);
194     int unit = ((struct ata_channel *)device_get_softc(child))->unit;
195
196     if (filter != NULL) {
197             printf("ata-cbus.c: we cannot use a filter here\n");
198             return (EINVAL);
199     }
200     controller->interrupt[unit].function = intr;
201     controller->interrupt[unit].argument = arg;
202     *cookiep = controller;
203
204     return 0;
205 }
206
207 static int
208 ata_cbus_print_child(device_t dev, device_t child)
209 {
210     struct ata_channel *ch = device_get_softc(child);
211     int retval = 0;
212
213     retval += bus_print_child_header(dev, child);
214     retval += printf(" at bank %d", ch->unit);
215     retval += bus_print_child_footer(dev, child);
216     return retval;
217 }
218
219 static void
220 ata_cbus_intr(void *data)
221 {  
222     struct ata_cbus_controller *ctlr = data;
223     struct ata_channel *ch;
224     int unit;
225
226     for (unit = 0; unit < ctlr->channels; unit++) {
227         if (!(ch = ctlr->interrupt[unit].argument))
228             continue;
229         ctlr->interrupt[unit].function(ch);
230     }
231 }
232
233 static device_method_t ata_cbus_methods[] = {
234     /* device interface */
235     DEVMETHOD(device_probe,             ata_cbus_probe),
236     DEVMETHOD(device_attach,            ata_cbus_attach),
237 //  DEVMETHOD(device_detach,            ata_cbus_detach),
238
239     /* bus methods */
240     DEVMETHOD(bus_alloc_resource,       ata_cbus_alloc_resource),
241     DEVMETHOD(bus_setup_intr,           ata_cbus_setup_intr),
242     DEVMETHOD(bus_print_child,          ata_cbus_print_child),
243
244     DEVMETHOD_END
245 };
246
247 static driver_t ata_cbus_driver = {
248     "atacbus",
249     ata_cbus_methods,
250     sizeof(struct ata_cbus_controller),
251 };
252
253 static devclass_t ata_cbus_devclass;
254
255 DRIVER_MODULE(atacbus, isa, ata_cbus_driver, ata_cbus_devclass, NULL, NULL);
256
257 static int
258 ata_cbuschannel_probe(device_t dev)
259 {
260     char buffer[32];
261
262     sprintf(buffer, "ATA channel %d", (int)(intptr_t)device_get_ivars(dev));
263     device_set_desc_copy(dev, buffer);
264
265     return ata_probe(dev);
266 }
267
268 static int
269 ata_cbuschannel_attach(device_t dev)
270 {
271     struct ata_cbus_controller *ctlr = device_get_softc(device_get_parent(dev));
272     struct ata_channel *ch = device_get_softc(dev);
273     int i;
274
275     if (ch->attached)
276         return (0);
277     ch->attached = 1;
278
279     ch->unit = (intptr_t)device_get_ivars(dev);
280     /* setup the resource vectors */
281     for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
282         ch->r_io[i].res = ctlr->io;
283         ch->r_io[i].offset = i << 1;
284     }
285     ch->r_io[ATA_CONTROL].res = ctlr->ctlio;
286     ch->r_io[ATA_CONTROL].offset = 0;
287     ch->r_io[ATA_IDX_ADDR].res = ctlr->io;
288     ata_default_registers(dev);
289
290     /* initialize softc for this channel */
291     ch->flags |= ATA_USE_16BIT;
292     ata_generic_hw(dev);
293
294     return ata_attach(dev);
295 }
296
297 static int
298 ata_cbuschannel_detach(device_t dev)
299 {
300     struct ata_channel *ch = device_get_softc(dev);
301
302     if (!ch->attached)
303         return (0);
304     ch->attached = 0;
305
306     return ata_detach(dev);
307 }
308
309 static int
310 ata_cbuschannel_suspend(device_t dev)
311 {
312     struct ata_channel *ch = device_get_softc(dev);
313
314     if (!ch->attached)
315         return (0);
316
317     return ata_suspend(dev);
318 }
319
320 static int
321 ata_cbuschannel_resume(device_t dev)
322 {
323     struct ata_channel *ch = device_get_softc(dev);
324
325     if (!ch->attached)
326         return (0);
327
328     return ata_resume(dev);
329 }
330
331 static device_method_t ata_cbuschannel_methods[] = {
332     /* device interface */
333     DEVMETHOD(device_probe,     ata_cbuschannel_probe),
334     DEVMETHOD(device_attach,    ata_cbuschannel_attach),
335     DEVMETHOD(device_detach,    ata_cbuschannel_detach),
336     DEVMETHOD(device_suspend,   ata_cbuschannel_suspend),
337     DEVMETHOD(device_resume,    ata_cbuschannel_resume),
338     DEVMETHOD_END
339 };
340
341 static driver_t ata_cbuschannel_driver = {
342     "ata",
343     ata_cbuschannel_methods,
344     sizeof(struct ata_channel),
345 };
346
347 DRIVER_MODULE(ata, atacbus, ata_cbuschannel_driver, ata_devclass, NULL, NULL);
348 MODULE_DEPEND(ata, ata, 1, 1, 1);