Merge from vendor branch AWK:
[dragonfly.git] / sys / dev / pccard / 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.24 2002/05/30 17:37:46 takawata Exp $ */
4 /* $DragonFly: src/sys/dev/pccard/pcic/Attic/i82365_isa.c,v 1.1 2004/02/10 07:55:47 joerg 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 <bus/isa/isavar.h>
50
51 #include <bus/pccard/pccardreg.h>
52 #include <bus/pccard/pccardvar.h>
53
54 #include <dev/pccard/pcic/i82365reg.h>
55 #include <dev/pccard/pcic/i82365var.h>
56
57 #include "power_if.h"
58 #include "card_if.h"
59
60 /*****************************************************************************
61  * Configurable parameters.
62  *****************************************************************************/
63
64 /*
65  * Default I/O allocation range.  If both are set to non-zero, these
66  * values will be used instead.  Otherwise, the code attempts to probe
67  * the bus width.  Systems with 10 address bits should use 0x300 and 0xff.
68  * Systems with 12 address bits (most) should use 0x400 and 0xbff.
69  */
70
71 #ifndef PCIC_ISA_ALLOC_IOBASE
72 #define PCIC_ISA_ALLOC_IOBASE           0
73 #endif
74
75 #ifndef PCIC_ISA_ALLOC_IOSIZE
76 #define PCIC_ISA_ALLOC_IOSIZE           0
77 #endif
78
79 int     pcic_isa_alloc_iobase = PCIC_ISA_ALLOC_IOBASE;
80 int     pcic_isa_alloc_iosize = PCIC_ISA_ALLOC_IOSIZE;
81
82
83 /*
84  * Default IRQ allocation bitmask.  This defines the range of allowable
85  * IRQs for PCCARD slots.  Useful if order of probing would screw up other
86  * devices, or if PCIC hardware/cards have trouble with certain interrupt
87  * lines.
88  *
89  * We disable IRQ 10 by default, since some common laptops (namely, the
90  * NEC Versa series) reserve IRQ 10 for the docking station SCSI interface.
91  */
92
93 #ifndef PCIC_ISA_INTR_ALLOC_MASK
94 #define PCIC_ISA_INTR_ALLOC_MASK        0xfbff
95 #endif
96
97 int     pcic_isa_intr_alloc_mask = PCIC_ISA_INTR_ALLOC_MASK;
98
99 /*****************************************************************************
100  * End of configurable parameters.
101  *****************************************************************************/
102
103 #define PCICISADEBUG 1
104
105 #ifdef PCICISADEBUG
106 int     pcicisa_debug = PCICISADEBUG;
107 #define DPRINTF(arg) if (pcicisa_debug) printf arg;
108 #define DEVPRINTF(arg) if (pcicisa_debug) device_printf arg;
109 #else
110 #define DPRINTF(arg)
111 #define DEVPRINTF(arg)
112 #endif
113
114 static struct isa_pnp_id pcic_ids[] = {
115         {PCIC_PNP_ACTIONTEC,            NULL},          /* AEI0218 */
116         {PCIC_PNP_IBM3765,              NULL},          /* IBM3765 */
117         {PCIC_PNP_82365,                NULL},          /* PNP0E00 */
118         {PCIC_PNP_CL_PD6720,            NULL},          /* PNP0E01 */
119         {PCIC_PNP_VLSI_82C146,          NULL},          /* PNP0E02 */
120         {PCIC_PNP_82365_CARDBUS,        NULL},          /* PNP0E03 */
121         {PCIC_PNP_SCM_SWAPBOX,          NULL},          /* SCM0469 */
122         {0}
123 };
124
125 static void
126 pcic_isa_bus_width_probe (device_t dev)
127 {
128         struct pcic_softc *sc = PCIC_SOFTC(dev);
129         bus_space_handle_t ioh_high;
130         int i, iobuswidth, tmp1, tmp2;
131         int rid;
132         u_long base;
133         u_int32_t length;
134         bus_space_tag_t iot;
135         bus_space_handle_t ioh;
136         struct resource *r;
137
138         base = rman_get_start(sc->port_res);
139         length = rman_get_size(sc->port_res);
140         iot = sc->iot;
141         ioh = sc->ioh;
142
143         /*
144          * figure out how wide the isa bus is.  Do this by checking if the
145          * pcic controller is mirrored 0x400 above where we expect it to be.
146          */
147
148         iobuswidth = 12;
149         rid = 1;
150         r = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, base + 0x400,
151             base + 0x400 + length, length, RF_ACTIVE);
152         if (!r) {
153                 printf("Can't allocated mirror area for pcic bus width probe\n");
154                 return;
155         }
156         ioh_high = rman_get_bushandle(r);
157         for (i = 0; i < PCIC_NSLOTS; i++) {
158                 if (sc->handle[i].flags & PCIC_FLAG_SOCKETP) {
159                         /*
160                          * read the ident flags from the normal space and
161                          * from the mirror, and compare them
162                          */
163
164                         bus_space_write_1(iot, ioh, PCIC_REG_INDEX,
165                             sc->handle[i].sock + PCIC_IDENT);
166                         tmp1 = bus_space_read_1(iot, ioh, PCIC_REG_DATA);
167
168                         bus_space_write_1(iot, ioh_high, PCIC_REG_INDEX,
169                             sc->handle[i].sock + PCIC_IDENT);
170                         tmp2 = bus_space_read_1(iot, ioh_high, PCIC_REG_DATA);
171
172                         if (tmp1 == tmp2)
173                                 iobuswidth = 10;
174                 }
175         }
176         bus_release_resource(dev, SYS_RES_IOPORT, rid, r);
177
178         /*
179          * XXX mycroft recommends I/O space range 0x400-0xfff .  I should put
180          * this in a header somewhere
181          */
182
183         /*
184          * XXX some hardware doesn't seem to grok addresses in 0x400 range--
185          * apparently missing a bit or more of address lines. (e.g.
186          * CIRRUS_PD672X with Linksys EthernetCard ne2000 clone in TI
187          * TravelMate 5000--not clear which is at fault)
188          * 
189          * Add a kludge to detect 10 bit wide buses and deal with them,
190          * and also a config file option to override the probe.
191          */
192
193         if (iobuswidth == 10) {
194                 sc->iobase = 0x300;
195                 sc->iosize = 0x0ff;
196         } else {
197 #if 0
198                 /*
199                  * This is what we'd like to use, but...
200                  */
201                 sc->iobase = 0x400;
202                 sc->iosize = 0xbff;
203 #else
204                 /*
205                  * ...the above bus width probe doesn't always work.
206                  * So, experimentation has shown the following range
207                  * to not lose on systems that 0x300-0x3ff loses on
208                  * (e.g. the NEC Versa 6030X).
209                  */
210                 sc->iobase = 0x330;
211                 sc->iosize = 0x0cf;
212 #endif
213         }
214
215         DEVPRINTF((dev, "bus_space_alloc range 0x%04lx-0x%04lx (probed)\n",
216             (long) sc->iobase, (long) sc->iobase + sc->iosize));
217
218         if (pcic_isa_alloc_iobase && pcic_isa_alloc_iosize) {
219                 sc->iobase = pcic_isa_alloc_iobase;
220                 sc->iosize = pcic_isa_alloc_iosize;
221
222                 DEVPRINTF((dev, "bus_space_alloc range 0x%04lx-0x%04lx "
223                     "(config override)\n", (long) sc->iobase,
224                     (long) sc->iobase + sc->iosize));
225         }
226 }
227
228 #if 0
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 #endif
276
277 static int
278 pcic_isa_probe(device_t dev)
279 {
280         int error;
281         struct resource *res;
282         int rid;
283         int i;
284         u_long mem;
285
286         /* Check isapnp ids */
287         error = ISA_PNP_PROBE(device_get_parent(dev), dev, pcic_ids);
288         if (error == ENXIO)
289                 return (ENXIO);
290
291         /* If we had some other problem. */
292         if (!(error == 0 || error == ENOENT))
293                 return (error);
294
295         /* If we have the resources we need then we're good to go. */
296         if (bus_get_resource_start(dev, SYS_RES_IOPORT, 0) == 0)
297                 return (ENXIO);
298         rid = 0;
299         res = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, RF_ACTIVE);
300         if (res == NULL) {
301                 /*
302                  * No IRQ specified, find one.  This can be due to the PnP
303                  * data not specifying any IRQ
304                  */
305                 for (i = 0; i < 16; i++) {
306                         if (((1 << i) & PCIC_INTR_IRQ_VALIDMASK) == 0)
307                                 continue;
308                         res = bus_alloc_resource(dev, SYS_RES_IRQ,
309                             &rid, i, i, 1, RF_ACTIVE);
310                         if (res != NULL)
311                                 break;
312                 }
313                 if (res == NULL)
314                         return (ENXIO);
315                 mem = rman_get_start(res);
316                 bus_release_resource(dev, SYS_RES_IRQ, rid, res);       
317                 bus_set_resource(dev, SYS_RES_IRQ, 0, i, 1);
318         } else {
319                 bus_release_resource(dev, SYS_RES_IRQ, rid, res);
320         }
321         /* XXX This might not be needed in future, get it directly from
322          * XXX parent */
323         rid = 0;
324         res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, 0, ~0, 
325             1 << 13, RF_ACTIVE);
326         if (res == NULL) {
327                 /*
328                  * We failed to get memory.  Since this XXX comment above
329                  * indicates that this is transient, we try to get a hunk
330                  * of memory in the isa hole.  Sure would be nice if there
331                  * were some MI constants for this.
332                  */
333                 res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, 
334                     0xa0000, 0xdffff, 1 << 13, RF_ACTIVE);
335                 if (res != NULL) {
336                         mem = rman_get_start(res);
337                         bus_release_resource(dev, SYS_RES_MEMORY, rid, res);
338                         bus_set_resource(dev, SYS_RES_MEMORY, 0, mem, 1 << 13);
339                 }
340         } else {
341                 bus_release_resource(dev, SYS_RES_MEMORY, rid, res);
342         }
343         if (res == NULL) {
344                 device_printf(dev, "Cannot allocate mem\n");
345                 return ENOMEM;
346         }
347         return (0);
348 }
349
350 static int
351 pcic_isa_attach(device_t dev)
352 {
353         int err = 0;
354
355         if ((err = pcic_attach(dev)) == 0)
356                 pcic_isa_bus_width_probe (dev);
357         return err;
358 }
359
360 static int
361 pcic_isa_detach(device_t dev)
362 {
363         pcic_detach(dev);
364         return 0;
365 }
366
367 static device_method_t pcic_isa_methods[] = {
368         /* Device interface */
369         DEVMETHOD(device_probe,         pcic_isa_probe),
370         DEVMETHOD(device_attach,        pcic_isa_attach),
371         DEVMETHOD(device_detach,        pcic_isa_detach),
372         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
373         DEVMETHOD(device_suspend,       pcic_suspend),
374         DEVMETHOD(device_resume,        pcic_resume),
375
376         /* Bus Interface */
377         DEVMETHOD(bus_driver_added,     bus_generic_driver_added),
378         DEVMETHOD(bus_print_child,      bus_generic_print_child),
379         DEVMETHOD(bus_alloc_resource,   pcic_alloc_resource),
380         DEVMETHOD(bus_release_resource, pcic_release_resource),
381         DEVMETHOD(bus_activate_resource, pcic_activate_resource),
382         DEVMETHOD(bus_deactivate_resource, pcic_deactivate_resource),
383         DEVMETHOD(bus_setup_intr,       pcic_setup_intr),
384         DEVMETHOD(bus_teardown_intr,    pcic_teardown_intr),
385
386         /* pccard/cardbus interface */
387         DEVMETHOD(card_set_res_flags, pcic_set_res_flags),
388         DEVMETHOD(card_set_memory_offset, pcic_set_memory_offset),
389
390         /* Power Interface */
391         DEVMETHOD(power_enable_socket,  pcic_enable_socket),
392         DEVMETHOD(power_disable_socket, pcic_disable_socket),
393         { 0, 0 }
394 };
395
396 static driver_t pcic_driver = {
397         "pcic",
398         pcic_isa_methods,
399         sizeof(struct pcic_softc)
400 };
401
402 static devclass_t pcic_devclass;
403
404 DRIVER_MODULE(pcic, isa, pcic_driver, pcic_devclass, 0, 0);
405 MODULE_DEPEND(pcic, pccard, 1, 1, 1);