xref: /openbsd/sys/dev/isa/tcic2_isa.c (revision 09467b48)
1 /*	$OpenBSD: tcic2_isa.c,v 1.8 2017/09/08 05:36:52 deraadt Exp $	*/
2 /*	$NetBSD: tcic2_isa.c,v 1.2 1999/04/08 16:14:29 bad Exp $	*/
3 
4 #undef	TCICISADEBUG
5 
6 /*
7  *
8  * Copyright (c) 1998, 1999 Christoph Badura. All rights reserved.
9  * Copyright (c) 1997 Marc Horowitz.  All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by Marc Horowitz.
22  * 4. The name of the author may not be used to endorse or promote products
23  *    derived from this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  */
36 
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/device.h>
41 #include <sys/extent.h>
42 #include <sys/malloc.h>
43 
44 #include <machine/bus.h>
45 #include <machine/intr.h>
46 
47 #include <dev/isa/isareg.h>
48 #include <dev/isa/isavar.h>
49 
50 #include <dev/pcmcia/pcmciareg.h>
51 #include <dev/pcmcia/pcmciavar.h>
52 #include <dev/pcmcia/pcmciachip.h>
53 
54 #include <dev/ic/tcic2reg.h>
55 #include <dev/ic/tcic2var.h>
56 
57 /*****************************************************************************
58  * Configurable parameters.
59  *****************************************************************************/
60 
61 /*
62  * Default I/O allocation range.  If both are set to non-zero, these
63  * values will be used instead.  Otherwise, the code attempts to probe
64  * the bus width.  Systems with 10 address bits should use 0x300 and 0xff.
65  * Systems with 12 address bits (most) should use 0x400 and 0xbff.
66  */
67 
68 #ifndef TCIC_ISA_ALLOC_IOBASE
69 #define	TCIC_ISA_ALLOC_IOBASE		0
70 #endif
71 
72 #ifndef TCIC_ISA_ALLOC_IOSIZE
73 #define	TCIC_ISA_ALLOC_IOSIZE		0
74 #endif
75 
76 int	tcic_isa_alloc_iobase = TCIC_ISA_ALLOC_IOBASE;
77 int	tcic_isa_alloc_iosize = TCIC_ISA_ALLOC_IOSIZE;
78 
79 /*
80  * Default IRQ allocation bitmask.  This defines the range of allowable
81  * IRQs for PCMCIA slots.  Useful if order of probing would screw up other
82  * devices, or if TCIC hardware/cards have trouble with certain interrupt
83  * lines.
84  *
85  * We disable IRQ 10 by default, since some common laptops (namely, the
86  * NEC Versa series) reserve IRQ 10 for the docking station SCSI interface.
87  *
88  * XXX Do we care about this?  the Versa doesn't use a tcic. -chb
89  */
90 
91 #ifndef TCIC_ISA_INTR_ALLOC_MASK
92 #define	TCIC_ISA_INTR_ALLOC_MASK	0xffff
93 #endif
94 
95 int	tcic_isa_intr_alloc_mask = TCIC_ISA_INTR_ALLOC_MASK;
96 
97 /*****************************************************************************
98  * End of configurable parameters.
99  *****************************************************************************/
100 
101 #ifdef TCICISADEBUG
102 int	tcic_isa_debug = 1;
103 #define	DPRINTF(arg) if (tcic_isa_debug) printf arg;
104 #else
105 #define	DPRINTF(arg)
106 #endif
107 
108 int	tcic_isa_probe(struct device *, void *, void *);
109 void	tcic_isa_attach(struct device *, struct device *, void *);
110 
111 void	*tcic_isa_chip_intr_establish(pcmcia_chipset_handle_t,
112 	    struct pcmcia_function *, int, int (*) (void *), void *, char *);
113 void	tcic_isa_chip_intr_disestablish(pcmcia_chipset_handle_t, void *);
114 const char *tcic_isa_chip_intr_string(pcmcia_chipset_handle_t, void *);
115 
116 struct cfattach tcic_isa_ca = {
117 	sizeof(struct tcic_softc), tcic_isa_probe, tcic_isa_attach
118 };
119 
120 static struct pcmcia_chip_functions tcic_isa_functions = {
121 	tcic_chip_mem_alloc,
122 	tcic_chip_mem_free,
123 	tcic_chip_mem_map,
124 	tcic_chip_mem_unmap,
125 
126 	tcic_chip_io_alloc,
127 	tcic_chip_io_free,
128 	tcic_chip_io_map,
129 	tcic_chip_io_unmap,
130 
131 	tcic_isa_chip_intr_establish,
132 	tcic_isa_chip_intr_disestablish,
133 	tcic_isa_chip_intr_string,
134 
135 	tcic_chip_socket_enable,
136 	tcic_chip_socket_disable,
137 };
138 
139 int
140 tcic_isa_probe(parent, match, aux)
141 	struct device *parent;
142 	void *match;
143 	void *aux;
144 {
145 	struct isa_attach_args *ia = aux;
146 	bus_space_tag_t iot = ia->ia_iot;
147 	bus_space_handle_t ioh, memh;
148 	int val, found;
149 
150 	/* Disallow wildcarded i/o address. */
151 	if (ia->ia_iobase == -1 /* ISACF_PORT_DEFAULT */)
152 		return (0);
153 
154 	if (bus_space_map(iot, ia->ia_iobase, TCIC_IOSIZE, 0, &ioh))
155 		return (0);
156 
157 	if (ia->ia_msize == 0)
158 		ia->ia_msize = TCIC_MEMSIZE;
159 
160 	if (bus_space_map(ia->ia_memt, ia->ia_maddr, ia->ia_msize, 0, &memh))
161 		return (0);
162 
163 	DPRINTF(("tcic probing 0x%03x\n", ia->ia_iobase));
164 	found = 0;
165 
166 	/*
167 	 * First, check for the reserved bits to be zero.
168 	 */
169 	if (tcic_check_reserved_bits(iot, ioh)) {
170 		DPRINTF(("tcic: reserved bits checked OK\n"));
171 		/* Second, check whether the we know how to handle the chip. */
172 		if ((val = tcic_chipid(iot, ioh))) {
173 			DPRINTF(("tcic id: 0x%02x\n", val));
174 			if (tcic_chipid_known(val))
175 				found++;
176 		}
177 	}
178 	else
179 		DPRINTF(("tcic: reserved bits didn't check OK\n"));
180 
181 	bus_space_unmap(iot, ioh, TCIC_IOSIZE);
182 	bus_space_unmap(ia->ia_memt, memh, ia->ia_msize);
183 
184 	if (!found)
185 		return (0);
186 
187 	ia->ia_iosize = TCIC_IOSIZE;
188 
189 	return (1);
190 }
191 
192 void
193 tcic_isa_attach(parent, self, aux)
194 	struct device *parent, *self;
195 	void *aux;
196 {
197 	struct tcic_softc *sc = (void *) self;
198 	struct isa_attach_args *ia = aux;
199 	isa_chipset_tag_t ic = ia->ia_ic;
200 	bus_space_tag_t iot = ia->ia_iot;
201 	bus_space_tag_t memt = ia->ia_memt;
202 	bus_space_handle_t ioh;
203 	bus_space_handle_t memh;
204 
205 	/* Map i/o space. */
206 	if (bus_space_map(iot, ia->ia_iobase, ia->ia_iosize, 0, &ioh)) {
207 		printf(": can't map i/o space\n");
208 		return;
209 	}
210 
211 	/* Map mem space. */
212 	if (bus_space_map(memt, ia->ia_maddr, ia->ia_msize, 0, &memh)) {
213 		printf(": can't map mem space\n");
214 		return;
215 	}
216 
217 	sc->membase = ia->ia_maddr;
218 	sc->subregionmask = (1 << (ia->ia_msize / TCIC_MEM_PAGESIZE)) - 1;
219 	sc->memsize2 = tcic_log2((u_int)ia->ia_msize);
220 
221 	sc->intr_est = ic;
222 	sc->pct = (pcmcia_chipset_tag_t) &tcic_isa_functions;
223 
224 	sc->iot = iot;
225 	sc->ioh = ioh;
226 	sc->memt = memt;
227 	sc->memh = memh;
228 
229 	/*
230 	 * determine chip type and initialise some chip type dependend
231 	 * parameters in softc.
232 	 */
233 	sc->chipid = tcic_chipid(iot, ioh);
234 	sc->validirqs = tcic_validirqs(sc->chipid);
235 
236 	/*
237 	 * allocate an irq.  interrupts are relatively
238 	 * scarce but for TCIC controllers very infrequent.
239 	 */
240 
241 	if ((sc->irq = ia->ia_irq) == IRQUNK) {
242 		if (isa_intr_alloc(ic,
243 		    sc->validirqs & (tcic_isa_intr_alloc_mask & 0xff00),
244 		    IST_EDGE, &sc->irq)) {
245 			printf("\n%s: can't allocate interrupt\n",
246 			    sc->dev.dv_xname);
247 			return;
248 		}
249 		printf(": using irq %d", sc->irq);
250 	}
251 	printf("\n");
252 
253 	tcic_attach(sc);
254 
255 
256 	/*
257 	 * XXX mycroft recommends I/O space range 0x400-0xfff.
258 	 */
259 
260 	/*
261 	 * XXX some hardware doesn't seem to grok addresses in 0x400 range--
262 	 * apparently missing a bit or more of address lines. (e.g.
263 	 * CIRRUS_PD672X with Linksys EthernetCard ne2000 clone in TI
264 	 * TravelMate 5000--not clear which is at fault)
265 	 *
266 	 * Add a kludge to detect 10 bit wide buses and deal with them,
267 	 * and also a config file option to override the probe.
268 	 */
269 
270 #if 0
271 	/*
272 	 * This is what we'd like to use, but...
273 	 */
274 	sc->iobase = 0x400;
275 	sc->iosize = 0xbff;
276 #else
277 	/*
278 	 * ...the above bus width probe doesn't always work.
279 	 * So, experimentation has shown the following range
280 	 * to not lose on systems that 0x300-0x3ff loses on
281 	 * (e.g. the NEC Versa 6030X).
282 	 */
283 	sc->iobase = 0x330;
284 	sc->iosize = 0x0cf;
285 #endif
286 
287 	DPRINTF(("%s: bus_space_alloc range 0x%04lx-0x%04lx)\n",
288 	    sc->dev.dv_xname, (long) sc->iobase,
289 	    (long) sc->iobase + sc->iosize));
290 
291 	if (tcic_isa_alloc_iobase && tcic_isa_alloc_iosize) {
292 		sc->iobase = tcic_isa_alloc_iobase;
293 		sc->iosize = tcic_isa_alloc_iosize;
294 
295 		DPRINTF(("%s: bus_space_alloc range 0x%04lx-0x%04lx "
296 		    "(config override)\n", sc->dev.dv_xname, (long) sc->iobase,
297 		    (long) sc->iobase + sc->iosize));
298 	}
299 	sc->ih = isa_intr_establish(ic, sc->irq, IST_EDGE, IPL_TTY,
300 	    tcic_intr, sc, sc->dev.dv_xname);
301 	if (sc->ih == NULL) {
302 		printf("%s: can't establish interrupt\n", sc->dev.dv_xname);
303 		return;
304 	}
305 
306 	tcic_attach_sockets(sc);
307 }
308 
309 void *
310 tcic_isa_chip_intr_establish(pch, pf, ipl, fct, arg, xname)
311 	pcmcia_chipset_handle_t pch;
312 	struct pcmcia_function *pf;
313 	int ipl;
314 	int (*fct)(void *);
315 	void *arg;
316 	char *xname;
317 {
318 	struct tcic_handle *h = (struct tcic_handle *) pch;
319 	int irq, ist, val, reg;
320 	void *ih;
321 	int irqmap[] = {
322 	    0, 0, 0, TCIC_SCF1_IRQ3, TCIC_SCF1_IRQ4, TCIC_SCF1_IRQ5,
323 	    TCIC_SCF1_IRQ6, TCIC_SCF1_IRQ7, 0, TCIC_SCF1_IRQ9,
324 	    TCIC_SCF1_IRQ10, TCIC_SCF1_IRQ11, TCIC_SCF1_IRQ12, 0,
325 	    TCIC_SCF1_IRQ14, TCIC_SCF1_IRQ15
326 	};
327 
328 	DPRINTF(("%s: tcic_isa_chip_intr_establish\n", h->sc->dev.dv_xname));
329 
330 	/* XXX should we convert level to pulse? -chb  */
331 	if (pf->cfe->flags & PCMCIA_CFE_IRQLEVEL)
332 		ist = IST_LEVEL;
333 	else if (pf->cfe->flags & PCMCIA_CFE_IRQPULSE)
334 		ist = IST_PULSE;
335 	else
336 		ist = IST_LEVEL;
337 
338 	if (isa_intr_alloc(h->sc->intr_est,
339 	    h->sc->validirqs & tcic_isa_intr_alloc_mask, ist, &irq))
340 		return (NULL);
341 	if ((ih = isa_intr_establish(h->sc->intr_est, irq, ist, ipl,
342 	    fct, arg, h->pcmcia->dv_xname)) == NULL)
343 		return (NULL);
344 
345 	DPRINTF(("%s: intr established\n", h->sc->dev.dv_xname));
346 
347 	h->ih_irq = irq;
348 
349 	reg = TCIC_IR_SCF1_N(h->sock);
350 	val = (tcic_read_ind_2(h, reg) & (~TCIC_SCF1_IRQ_MASK)) | irqmap[irq];
351 	tcic_write_ind_2(h, reg, val);
352 
353 	printf(" irq %d", irq);
354 	return (ih);
355 }
356 
357 void
358 tcic_isa_chip_intr_disestablish(pch, ih)
359 	pcmcia_chipset_handle_t pch;
360 	void *ih;
361 {
362 	struct tcic_handle *h = (struct tcic_handle *) pch;
363 	int val, reg;
364 
365 	DPRINTF(("%s: tcic_isa_chip_intr_disestablish\n", h->sc->dev.dv_xname));
366 
367 	h->ih_irq = 0;
368 
369 	reg = TCIC_IR_SCF1_N(h->sock);
370 	val = tcic_read_ind_2(h, reg);
371 	val &= ~TCIC_SCF1_IRQ_MASK;
372 	tcic_write_ind_2(h, reg, val);
373 
374 	isa_intr_disestablish(h->sc->intr_est, ih);
375 }
376 
377 const char *
378 tcic_isa_chip_intr_string(pch, ih)
379 	pcmcia_chipset_handle_t pch;
380 	void *ih;
381 {
382 	struct tcic_handle *h = (struct tcic_handle *) pch;
383 	static char irqstr[64];
384 
385 	if (ih == NULL)
386 		snprintf(irqstr, sizeof(irqstr), "couldn't establish interrupt");
387 	else
388 		snprintf(irqstr, sizeof(irqstr), "irq %d", h->ih_irq);
389 	return (irqstr);
390 }
391