]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/dev/ata/ata-cbus.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / dev / ata / ata-cbus.c
1 /*-
2  * Copyright (c) 2002 - 2007 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 "opt_ata.h"
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/ata.h>
35 #include <sys/bus.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/conf.h>
39 #include <sys/sema.h>
40 #include <sys/taskqueue.h>
41 #include <vm/uma.h>
42 #include <machine/resource.h>
43 #include <machine/bus.h>
44 #include <sys/rman.h>
45 #include <isa/isavar.h>
46 #include <dev/ata/ata-all.h>
47 #include <ata_if.h>
48
49 /* local vars */
50 struct ata_cbus_controller {
51     struct resource *io;
52     struct resource *ctlio;
53     struct resource *bankio;
54     struct resource *irq;
55     void *ih;
56     struct mtx bank_mtx;
57     int locked_bank;
58     int restart_bank;
59     int hardware_bank;
60     struct {
61         void (*function)(void *);
62         void *argument;
63     } interrupt[2];
64 };
65
66 /* local prototypes */
67 static void ata_cbus_intr(void *);
68 static int ata_cbuschannel_banking(device_t dev, int flags);
69
70 static int
71 ata_cbus_probe(device_t dev)
72 {
73     struct resource *io;
74     int rid;
75     u_long tmp;
76
77     /* dont probe PnP devices */
78     if (isa_get_vendorid(dev))
79         return (ENXIO);
80
81     /* allocate the ioport range */
82     rid = ATA_IOADDR_RID;
83     if (!(io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
84                                   ATA_PC98_IOSIZE, RF_ACTIVE)))
85         return ENOMEM;
86
87     /* calculate & set the altport range */
88     rid = ATA_PC98_CTLADDR_RID;
89     if (bus_get_resource(dev, SYS_RES_IOPORT, rid, &tmp, &tmp)) {
90         bus_set_resource(dev, SYS_RES_IOPORT, rid,
91                          rman_get_start(io)+ATA_PC98_CTLOFFSET, ATA_CTLIOSIZE);
92     }
93
94     /* calculate & set the bank range */
95     rid = ATA_PC98_BANKADDR_RID;
96     if (bus_get_resource(dev, SYS_RES_IOPORT, rid, &tmp, &tmp)) {
97         bus_set_resource(dev, SYS_RES_IOPORT, rid,
98                          ATA_PC98_BANK, ATA_PC98_BANKIOSIZE);
99     }
100
101     bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
102     return 0;
103 }
104
105 static int
106 ata_cbus_attach(device_t dev)
107 {
108     struct ata_cbus_controller *ctlr = device_get_softc(dev);
109     int rid;
110
111     /* allocate resources */
112     rid = ATA_IOADDR_RID;
113     if (!(ctlr->io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
114                                         ATA_PC98_IOSIZE, RF_ACTIVE)))
115        return ENOMEM;
116
117     rid = ATA_PC98_CTLADDR_RID;
118     if (!(ctlr->ctlio = 
119           bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
120                              rman_get_start(ctlr->io) + ATA_PC98_CTLOFFSET, ~0,
121                              ATA_CTLIOSIZE, RF_ACTIVE))) {
122         bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ctlr->io);
123         return ENOMEM;
124     }
125
126     rid = ATA_PC98_BANKADDR_RID;
127     if (!(ctlr->bankio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
128                                             ATA_PC98_BANK, ~0,
129                                             ATA_PC98_BANKIOSIZE, RF_ACTIVE))) {
130         bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ctlr->io);
131         bus_release_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID, ctlr->ctlio);
132         return ENOMEM;
133     }
134
135     rid = ATA_IRQ_RID;
136     if (!(ctlr->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
137                                              RF_ACTIVE | RF_SHAREABLE))) {
138         device_printf(dev, "unable to alloc interrupt\n");
139         bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ctlr->io);
140         bus_release_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID, ctlr->ctlio);
141         bus_release_resource(dev, SYS_RES_IOPORT, 
142                              ATA_PC98_BANKADDR_RID, ctlr->bankio);
143         return ENXIO;
144     }
145
146     if ((bus_setup_intr(dev, ctlr->irq, ATA_INTR_FLAGS,
147                         NULL, ata_cbus_intr, ctlr, &ctlr->ih))) {
148         device_printf(dev, "unable to setup interrupt\n");
149         bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, ctlr->io);
150         bus_release_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID, ctlr->ctlio);
151         bus_release_resource(dev, SYS_RES_IOPORT, 
152                              ATA_PC98_BANKADDR_RID, ctlr->bankio);
153         bus_release_resource(dev, SYS_RES_IOPORT, ATA_IRQ_RID, ctlr->irq);
154         return ENXIO;
155     }
156
157     mtx_init(&ctlr->bank_mtx, "ATA cbus bank lock", NULL, MTX_DEF);
158     ctlr->hardware_bank = -1;
159     ctlr->locked_bank = -1;
160     ctlr->restart_bank = -1;
161
162     if (!device_add_child(dev, "ata", 0))
163         return ENOMEM;
164     if (!device_add_child(dev, "ata", 1))
165         return ENOMEM;
166
167     return bus_generic_attach(dev);
168 }
169
170 static struct resource *
171 ata_cbus_alloc_resource(device_t dev, device_t child, int type, int *rid,
172                         u_long start, u_long end, u_long count, u_int flags)
173 {
174     struct ata_cbus_controller *ctlr = device_get_softc(dev);
175
176     if (type == SYS_RES_IOPORT) {
177         switch (*rid) {
178         case ATA_IOADDR_RID:
179             return ctlr->io;
180         case ATA_CTLADDR_RID:
181             return ctlr->ctlio;
182         }
183     }
184     if (type == SYS_RES_IRQ)
185         return ctlr->irq;
186     return 0;
187 }
188
189 static int
190 ata_cbus_setup_intr(device_t dev, device_t child, struct resource *irq,
191                int flags, driver_filter_t *filter, driver_intr_t *intr, 
192                void *arg, void **cookiep)
193 {
194     struct ata_cbus_controller *controller = device_get_softc(dev);
195     int unit = ((struct ata_channel *)device_get_softc(child))->unit;
196
197     if (filter != NULL) {
198             printf("ata-cbus.c: we cannot use a filter here\n");
199             return (EINVAL);
200     }
201     controller->interrupt[unit].function = intr;
202     controller->interrupt[unit].argument = arg;
203     *cookiep = controller;
204
205     return 0;
206 }
207
208 static int
209 ata_cbus_print_child(device_t dev, device_t child)
210 {
211     struct ata_channel *ch = device_get_softc(child);
212     int retval = 0;
213
214     retval += bus_print_child_header(dev, child);
215     retval += printf(" at bank %d", ch->unit);
216     retval += bus_print_child_footer(dev, child);
217     return retval;
218 }
219
220 static void
221 ata_cbus_intr(void *data)
222 {  
223     struct ata_cbus_controller *ctlr = data;
224     struct ata_channel *ch;
225     int unit;
226
227     for (unit = 0; unit < 2; unit++) {
228         if (!(ch = ctlr->interrupt[unit].argument))
229             continue;
230         if (ata_cbuschannel_banking(ch->dev, ATA_LF_WHICH) == unit)
231             ctlr->interrupt[unit].function(ch);
232     }
233 }
234
235 static device_method_t ata_cbus_methods[] = {
236     /* device interface */
237     DEVMETHOD(device_probe,             ata_cbus_probe),
238     DEVMETHOD(device_attach,            ata_cbus_attach),
239 //  DEVMETHOD(device_detach,            ata_cbus_detach),
240
241     /* bus methods */
242     DEVMETHOD(bus_alloc_resource,       ata_cbus_alloc_resource),
243     DEVMETHOD(bus_setup_intr,           ata_cbus_setup_intr),
244     DEVMETHOD(bus_print_child,          ata_cbus_print_child),
245
246     { 0, 0 }
247 };
248
249 static driver_t ata_cbus_driver = {
250     "atacbus",
251     ata_cbus_methods,
252     sizeof(struct ata_cbus_controller),
253 };
254
255 static devclass_t ata_cbus_devclass;
256
257 DRIVER_MODULE(atacbus, isa, ata_cbus_driver, ata_cbus_devclass, 0, 0);
258
259 static int
260 ata_cbuschannel_probe(device_t dev)
261 {
262     struct ata_cbus_controller *ctlr = device_get_softc(device_get_parent(dev));
263     struct ata_channel *ch = device_get_softc(dev);
264     device_t *children;
265     int count, i;
266
267     /* find channel number on this controller */
268     device_get_children(device_get_parent(dev), &children, &count);
269     for (i = 0; i < count; i++) {
270         if (children[i] == dev) 
271             ch->unit = i;
272     }
273     free(children, M_TEMP);
274
275     /* setup the resource vectors */
276     for (i = ATA_DATA; i <= ATA_COMMAND; i ++) {
277         ch->r_io[i].res = ctlr->io;
278         ch->r_io[i].offset = i << 1;
279     }
280     ch->r_io[ATA_CONTROL].res = ctlr->ctlio;
281     ch->r_io[ATA_CONTROL].offset = 0;
282     ch->r_io[ATA_IDX_ADDR].res = ctlr->io;
283     ata_default_registers(dev);
284
285     /* initialize softc for this channel */
286     ch->flags |= ATA_USE_16BIT;
287     ata_generic_hw(dev);
288     return ata_probe(dev);
289 }
290
291 static int
292 ata_cbuschannel_banking(device_t dev, int flags)
293 {
294     struct ata_cbus_controller *ctlr = device_get_softc(device_get_parent(dev));
295     struct ata_channel *ch = device_get_softc(dev);
296     int res;
297
298     mtx_lock(&ctlr->bank_mtx);
299     switch (flags) {
300     case ATA_LF_LOCK:
301         if (ctlr->locked_bank == -1)
302             ctlr->locked_bank = ch->unit;
303         if (ctlr->locked_bank == ch->unit) {
304             ctlr->hardware_bank = ch->unit;
305             ATA_OUTB(ctlr->bankio, 0, ch->unit);
306         }
307         else
308             ctlr->restart_bank = ch->unit;
309         break;
310
311     case ATA_LF_UNLOCK:
312         if (ctlr->locked_bank == ch->unit) {
313             ctlr->locked_bank = -1;
314             if (ctlr->restart_bank != -1) {
315                 if ((ch = ctlr->interrupt[ctlr->restart_bank].argument)) {
316                     ctlr->restart_bank = -1;
317                     mtx_unlock(&ctlr->bank_mtx);
318                     ata_start(ch->dev);
319                     return -1;
320                 }
321             }
322         }
323         break;
324
325     case ATA_LF_WHICH:
326         break;
327     }
328     res = ctlr->locked_bank;
329     mtx_unlock(&ctlr->bank_mtx);
330     return res;
331 }
332
333 static device_method_t ata_cbuschannel_methods[] = {
334     /* device interface */
335     DEVMETHOD(device_probe,     ata_cbuschannel_probe),
336     DEVMETHOD(device_attach,    ata_attach),
337     DEVMETHOD(device_detach,    ata_detach),
338     DEVMETHOD(device_suspend,   ata_suspend),
339     DEVMETHOD(device_resume,    ata_resume),
340
341     /* ATA methods */
342     DEVMETHOD(ata_locking,      ata_cbuschannel_banking),
343     { 0, 0 }
344 };
345
346 static driver_t ata_cbuschannel_driver = {
347     "ata",
348     ata_cbuschannel_methods,
349     sizeof(struct ata_channel),
350 };
351
352 DRIVER_MODULE(ata, atacbus, ata_cbuschannel_driver, ata_devclass, 0, 0);
353 MODULE_DEPEND(ata, ata, 1, 1, 1);