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