LINT build test. Aggregated source code adjustments to bring most of the
[dragonfly.git] / sys / dev / misc / pcic / i82365_isa.c
1 /*      $NetBSD: i82365_isasubr.c,v 1.3 1999/10/15 06:07:27 haya Exp $  */
2 /*      $NetBSD: i82365_isa.c,v 1.11 1998/06/09 07:25:00 thorpej Exp $  */
3 /* $FreeBSD: src/sys/dev/pcic/i82365_isa.c,v 1.13.2.1 2000/05/23 03:57:02 imp Exp $ */
4 /* $DragonFly: src/sys/dev/misc/pcic/Attic/i82365_isa.c,v 1.2 2003/06/17 04:28:29 dillon Exp $ */
5
6 /*
7  * Copyright (c) 1998 Bill Sommerfeld.  All rights reserved.
8  * Copyright (c) 1997 Marc Horowitz.  All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by Marc Horowitz.
21  * 4. The name of the author may not be used to endorse or promote products
22  *    derived from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/kernel.h>
41 #include <sys/queue.h>
42 #include <sys/types.h>
43
44 #include <sys/bus.h>
45 #include <machine/bus.h>
46 #include <sys/rman.h>
47 #include <machine/resource.h>
48
49 #include <isa/isavar.h>
50
51 #include <dev/pccard/pccardreg.h>
52 #include <dev/pccard/pccardvar.h>
53
54 #include <dev/pcic/i82365reg.h>
55 #include <dev/pcic/i82365var.h>
56
57 #include "power_if.h"
58 #include "card_if.h"
59
60 /*****************************************************************************
61  * Configurable parameters.
62  *****************************************************************************/
63
64 #if 0
65 #include "opt_pcic_isa_alloc_iobase.h"
66 #include "opt_pcic_isa_alloc_iosize.h"
67 #include "opt_pcic_isa_intr_alloc_mask.h"
68 #endif
69
70 /*
71  * Default I/O allocation range.  If both are set to non-zero, these
72  * values will be used instead.  Otherwise, the code attempts to probe
73  * the bus width.  Systems with 10 address bits should use 0x300 and 0xff.
74  * Systems with 12 address bits (most) should use 0x400 and 0xbff.
75  */
76
77 #ifndef PCIC_ISA_ALLOC_IOBASE
78 #define PCIC_ISA_ALLOC_IOBASE           0
79 #endif
80
81 #ifndef PCIC_ISA_ALLOC_IOSIZE
82 #define PCIC_ISA_ALLOC_IOSIZE           0
83 #endif
84
85 int     pcic_isa_alloc_iobase = PCIC_ISA_ALLOC_IOBASE;
86 int     pcic_isa_alloc_iosize = PCIC_ISA_ALLOC_IOSIZE;
87
88
89 /*
90  * Default IRQ allocation bitmask.  This defines the range of allowable
91  * IRQs for PCCARD slots.  Useful if order of probing would screw up other
92  * devices, or if PCIC hardware/cards have trouble with certain interrupt
93  * lines.
94  *
95  * We disable IRQ 10 by default, since some common laptops (namely, the
96  * NEC Versa series) reserve IRQ 10 for the docking station SCSI interface.
97  */
98
99 #ifndef PCIC_ISA_INTR_ALLOC_MASK
100 #define PCIC_ISA_INTR_ALLOC_MASK        0xfbff
101 #endif
102
103 int     pcic_isa_intr_alloc_mask = PCIC_ISA_INTR_ALLOC_MASK;
104
105 /*****************************************************************************
106  * End of configurable parameters.
107  *****************************************************************************/
108
109 #ifdef PCICISADEBUG
110 int     pcicisa_debug = 0 /* XXX */ ;
111 #define DPRINTF(arg) if (pcicisa_debug) printf arg;
112 #define DEVPRINTF(arg) if (pcicisa_debug) device_printf arg;
113 #else
114 #define DPRINTF(arg)
115 #define DEVPRINTF(arg)
116 #endif
117
118 static struct isa_pnp_id pcic_ids[] = {
119         {PCIC_PNP_82365,                NULL},          /* PNP0E00 */
120         {PCIC_PNP_CL_PD6720,            NULL},          /* PNP0E01 */
121         {PCIC_PNP_VLSI_82C146,          NULL},          /* PNP0E02 */
122         {PCIC_PNP_82365_CARDBUS,        NULL},          /* PNP0E03 */
123         {0}
124 };
125
126 static void
127 pcic_isa_bus_width_probe (device_t dev)
128 {
129         struct pcic_softc *sc = (struct pcic_softc *)
130             device_get_softc(dev);
131         bus_space_handle_t ioh_high;
132         int i, iobuswidth, tmp1, tmp2;
133         int rid;
134         u_long base;
135         u_int32_t length;
136         bus_space_tag_t iot;
137         bus_space_handle_t ioh;
138         struct resource *r;
139
140         base = rman_get_start(sc->port_res);
141         length = rman_get_end(sc->port_res) - rman_get_end(sc->port_res) + 1;
142         iot = sc->iot;
143         ioh = sc->ioh;
144
145         /*
146          * figure out how wide the isa bus is.  Do this by checking if the
147          * pcic controller is mirrored 0x400 above where we expect it to be.
148          */
149
150         iobuswidth = 12;
151         rid = 1;
152         r = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, base + 0x400,
153             base + 0x400 + length, length, RF_ACTIVE);
154         if (!r) {
155                 printf("Can't allocated mirror area for pcic bus width probe\n");
156                 return;
157         }
158         ioh_high = rman_get_bushandle(r);
159         for (i = 0; i < PCIC_NSLOTS; i++) {
160                 if (sc->handle[i].flags & PCIC_FLAG_SOCKETP) {
161                         /*
162                          * read the ident flags from the normal space and
163                          * from the mirror, and compare them
164                          */
165
166                         bus_space_write_1(iot, ioh, PCIC_REG_INDEX,
167                             sc->handle[i].sock + PCIC_IDENT);
168                         tmp1 = bus_space_read_1(iot, ioh, PCIC_REG_DATA);
169
170                         bus_space_write_1(iot, ioh_high, PCIC_REG_INDEX,
171                             sc->handle[i].sock + PCIC_IDENT);
172                         tmp2 = bus_space_read_1(iot, ioh_high, PCIC_REG_DATA);
173
174                         if (tmp1 == tmp2)
175                                 iobuswidth = 10;
176                 }
177         }
178         bus_release_resource(dev, SYS_RES_IOPORT, rid, r);
179
180         /*
181          * XXX mycroft recommends I/O space range 0x400-0xfff .  I should put
182          * this in a header somewhere
183          */
184
185         /*
186          * XXX some hardware doesn't seem to grok addresses in 0x400 range--
187          * apparently missing a bit or more of address lines. (e.g.
188          * CIRRUS_PD672X with Linksys EthernetCard ne2000 clone in TI
189          * TravelMate 5000--not clear which is at fault)
190          * 
191          * Add a kludge to detect 10 bit wide buses and deal with them,
192          * and also a config file option to override the probe.
193          */
194
195         if (iobuswidth == 10) {
196                 sc->iobase = 0x300;
197                 sc->iosize = 0x0ff;
198         } else {
199 #if 0
200                 /*
201                  * This is what we'd like to use, but...
202                  */
203                 sc->iobase = 0x400;
204                 sc->iosize = 0xbff;
205 #else
206                 /*
207                  * ...the above bus width probe doesn't always work.
208                  * So, experimentation has shown the following range
209                  * to not lose on systems that 0x300-0x3ff loses on
210                  * (e.g. the NEC Versa 6030X).
211                  */
212                 sc->iobase = 0x330;
213                 sc->iosize = 0x0cf;
214 #endif
215         }
216
217         DEVPRINTF((dev, "bus_space_alloc range 0x%04lx-0x%04lx (probed)\n",
218             (long) sc->iobase, (long) sc->iobase + sc->iosize));
219
220         if (pcic_isa_alloc_iobase && pcic_isa_alloc_iosize) {
221                 sc->iobase = pcic_isa_alloc_iobase;
222                 sc->iosize = pcic_isa_alloc_iosize;
223
224                 DEVPRINTF((dev, "bus_space_alloc range 0x%04lx-0x%04lx "
225                     "(config override)\n", (long) sc->iobase,
226                     (long) sc->iobase + sc->iosize));
227         }
228 }
229
230 static int
231 pcic_isa_check(device_t dev, u_int16_t addr)
232 {
233         bus_space_tag_t iot;
234         bus_space_handle_t ioh;
235         int val, found;
236         int rid;
237         struct resource *res;
238
239         rid = 0;
240         res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, addr, addr,
241             PCIC_IOSIZE, RF_ACTIVE);
242         if (!res)
243                 return(ENXIO);
244         iot = rman_get_bustag(res);
245         ioh = rman_get_bushandle(res);
246         found = 0;
247
248         /*
249          * this could be done with a loop, but it would violate the
250          * abstraction
251          */
252         bus_space_write_1(iot, ioh, PCIC_REG_INDEX, C0SA + PCIC_IDENT);
253         val = bus_space_read_1(iot, ioh, PCIC_REG_DATA);
254         if (pcic_ident_ok(val))
255                 found++;
256
257         bus_space_write_1(iot, ioh, PCIC_REG_INDEX, C0SB + PCIC_IDENT);
258         val = bus_space_read_1(iot, ioh, PCIC_REG_DATA);
259         if (pcic_ident_ok(val))
260                 found++;
261
262         bus_space_write_1(iot, ioh, PCIC_REG_INDEX, C1SA + PCIC_IDENT);
263         val = bus_space_read_1(iot, ioh, PCIC_REG_DATA);
264         if (pcic_ident_ok(val))
265                 found++;
266
267         bus_space_write_1(iot, ioh, PCIC_REG_INDEX, C1SB + PCIC_IDENT);
268         val = bus_space_read_1(iot, ioh, PCIC_REG_DATA);
269         if (pcic_ident_ok(val))
270                 found++;
271
272         bus_release_resource(dev, SYS_RES_IOPORT, rid, res);
273
274         return (found);
275 }
276
277 static void
278 pcic_isa_identify(driver_t *driver, device_t parent)
279 {
280         device_t child;
281         u_int16_t ioaddrs[] = { 0x3e0, 0x3e2, 0x3e4, 0x3e6, 0 };
282         u_int16_t ioaddr;
283         int i;
284
285         for (i = 0; ioaddrs[i]; i++) {
286                 ioaddr = ioaddrs[i];
287                 if (pcic_isa_check(parent, ioaddr)) {
288                         child = BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, 
289                             "pcic", -1);
290                         device_set_driver(child, driver);
291 /* XXX */
292                         bus_set_resource(child, SYS_RES_IRQ, 0, 10, 1);
293                         bus_set_resource(child, SYS_RES_MEMORY, 0, 0xd0000, 1 << 12);
294                         bus_set_resource(child, SYS_RES_IOPORT, 0, ioaddr,
295                             PCIC_IOSIZE);
296                 }
297         }
298 }
299
300 static int
301 pcic_isa_probe(device_t dev)
302 {
303         int error;
304
305         /* Check isapnp ids */
306         error = ISA_PNP_PROBE(device_get_parent(dev), dev, pcic_ids);
307         if (error == ENXIO)
308                 return (ENXIO);
309
310         /* If we had some other problem. */
311         if (!(error == 0 || error == ENOENT)) {
312                 return (error);
313         }
314
315         /* If we have the resources we need then we're good to go. */
316         if ((bus_get_resource_start(dev, SYS_RES_IOPORT, 0) != 0) &&
317             (bus_get_resource_start(dev, SYS_RES_IRQ, 0) != 0)) {
318                 return (0);
319         }
320
321         return (ENXIO);
322 }
323
324 static int
325 pcic_isa_attach(device_t dev)
326 {
327         int err = 0;
328
329         if ((err = pcic_attach(dev)) == 0)
330                 pcic_isa_bus_width_probe (dev);
331
332         return err;
333 }
334
335 static int
336 pcic_isa_detach(device_t dev)
337 {
338         return 0;
339 }
340
341 static device_method_t pcic_isa_methods[] = {
342         /* Device interface */
343         DEVMETHOD(device_identify,      pcic_isa_identify),
344         DEVMETHOD(device_probe,         pcic_isa_probe),
345         DEVMETHOD(device_attach,        pcic_isa_attach),
346         DEVMETHOD(device_detach,        pcic_isa_detach),
347         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
348         DEVMETHOD(device_suspend,       pcic_suspend),
349         DEVMETHOD(device_resume,        pcic_resume),
350
351         /* Bus Interface */
352         DEVMETHOD(bus_driver_added,     bus_generic_driver_added),
353         DEVMETHOD(bus_print_child,      bus_generic_print_child),
354         DEVMETHOD(bus_alloc_resource,   pcic_alloc_resource),
355         DEVMETHOD(bus_release_resource, pcic_release_resource),
356         DEVMETHOD(bus_activate_resource, pcic_activate_resource),
357         DEVMETHOD(bus_deactivate_resource, pcic_deactivate_resource),
358         DEVMETHOD(bus_setup_intr,       pcic_setup_intr),
359         DEVMETHOD(bus_teardown_intr,    pcic_teardown_intr),
360
361         /* pccard/cardbus interface */
362         DEVMETHOD(card_set_res_flags, pcic_set_res_flags),
363         DEVMETHOD(card_set_memory_offset, pcic_set_memory_offset),
364
365         /* Power Interface */
366         DEVMETHOD(power_enable_socket,  pcic_enable_socket),
367         DEVMETHOD(power_disable_socket, pcic_disable_socket),
368         { 0, 0 }
369 };
370
371 static driver_t pcic_driver = {
372         "pcic",
373         pcic_isa_methods,
374         sizeof(struct pcic_softc)
375 };
376
377 static devclass_t pcic_devclass;
378
379 DRIVER_MODULE(pcic, isa, pcic_driver, pcic_devclass, 0, 0);