xref: /netbsd-src/sys/dev/isa/uha_isa.c (revision 481fca6e59249d8ffcf24fef7cfbe7b131bfb080)
1 /*	$NetBSD: uha_isa.c,v 1.19 2000/03/23 07:01:35 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Charles M. Hannum.
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 the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include "opt_ddb.h"
40 
41 #include <sys/types.h>
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/device.h>
45 #include <sys/kernel.h>
46 #include <sys/proc.h>
47 #include <sys/user.h>
48 
49 #include <machine/bus.h>
50 #include <machine/intr.h>
51 
52 #include <dev/scsipi/scsi_all.h>
53 #include <dev/scsipi/scsipi_all.h>
54 #include <dev/scsipi/scsiconf.h>
55 
56 #include <dev/isa/isavar.h>
57 #include <dev/isa/isadmavar.h>
58 
59 #include <dev/ic/uhareg.h>
60 #include <dev/ic/uhavar.h>
61 
62 #define	UHA_ISA_IOSIZE	16
63 
64 int	uha_isa_probe __P((struct device *, struct cfdata *, void *));
65 void	uha_isa_attach __P((struct device *, struct device *, void *));
66 
67 struct cfattach uha_isa_ca = {
68 	sizeof(struct uha_softc), uha_isa_probe, uha_isa_attach
69 };
70 
71 #ifndef	DDB
72 #define Debugger() panic("should call debugger here (uha_isa.c)")
73 #endif /* ! DDB */
74 
75 int	u14_find __P((bus_space_tag_t, bus_space_handle_t,
76 	    struct uha_probe_data *));
77 void	u14_start_mbox __P((struct uha_softc *, struct uha_mscp *));
78 int	u14_poll __P((struct uha_softc *, struct scsipi_xfer *, int));
79 int	u14_intr __P((void *));
80 void	u14_init __P((struct uha_softc *));
81 
82 /*
83  * Check the slots looking for a board we recognise
84  * If we find one, note it's address (slot) and call
85  * the actual probe routine to check it out.
86  */
87 int
88 uha_isa_probe(parent, match, aux)
89 	struct device *parent;
90 	struct cfdata *match;
91 	void *aux;
92 {
93 	struct isa_attach_args *ia = aux;
94 	bus_space_tag_t iot = ia->ia_iot;
95 	bus_space_handle_t ioh;
96 	struct uha_probe_data upd;
97 	int rv;
98 
99 	/* Disallow wildcarded i/o address. */
100 	if (ia->ia_iobase == ISACF_PORT_DEFAULT)
101 		return (0);
102 
103 	if (bus_space_map(iot, ia->ia_iobase, UHA_ISA_IOSIZE, 0, &ioh))
104 		return (0);
105 
106 	rv = u14_find(iot, ioh, &upd);
107 
108 	bus_space_unmap(iot, ioh, UHA_ISA_IOSIZE);
109 
110 	if (rv) {
111 		if (ia->ia_irq != -1 && ia->ia_irq != upd.sc_irq)
112 			return (0);
113 		if (ia->ia_drq != -1 && ia->ia_drq != upd.sc_drq)
114 			return (0);
115 		ia->ia_irq = upd.sc_irq;
116 		ia->ia_drq = upd.sc_drq;
117 		ia->ia_msize = 0;
118 		ia->ia_iosize = UHA_ISA_IOSIZE;
119 	}
120 	return (rv);
121 }
122 
123 /*
124  * Attach all the sub-devices we can find
125  */
126 void
127 uha_isa_attach(parent, self, aux)
128 	struct device *parent, *self;
129 	void *aux;
130 {
131 	struct isa_attach_args *ia = aux;
132 	struct uha_softc *sc = (void *)self;
133 	bus_space_tag_t iot = ia->ia_iot;
134 	bus_dma_tag_t dmat = ia->ia_dmat;
135 	bus_space_handle_t ioh;
136 	struct uha_probe_data upd;
137 	isa_chipset_tag_t ic = ia->ia_ic;
138 	int error;
139 
140 	printf("\n");
141 
142 	if (bus_space_map(iot, ia->ia_iobase, UHA_ISA_IOSIZE, 0, &ioh)) {
143 		printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
144 		return;
145 	}
146 
147 	sc->sc_iot = iot;
148 	sc->sc_ioh = ioh;
149 	sc->sc_dmat = dmat;
150 	if (!u14_find(iot, ioh, &upd)) {
151 		printf("%s: u14_find failed\n", sc->sc_dev.dv_xname);
152 		return;
153 	}
154 
155 	if (upd.sc_drq != -1) {
156 		sc->sc_dmaflags = 0;
157 		if ((error = isa_dmacascade(ic, upd.sc_drq)) != 0) {
158 			printf("%s: unable to cascade DRQ, error = %d\n",
159 			    sc->sc_dev.dv_xname, error);
160 			return;
161 		}
162 	} else {
163 		/*
164 		 * We have a VLB controller, and can do 32-bit DMA.
165 		 */
166 		sc->sc_dmaflags = ISABUS_DMA_32BIT;
167 	}
168 
169 	sc->sc_ih = isa_intr_establish(ic, upd.sc_irq, IST_EDGE, IPL_BIO,
170 	    u14_intr, sc);
171 	if (sc->sc_ih == NULL) {
172 		printf("%s: couldn't establish interrupt\n",
173 		    sc->sc_dev.dv_xname);
174 		return;
175 	}
176 
177 	/* Save function pointers for later use. */
178 	sc->start_mbox = u14_start_mbox;
179 	sc->poll = u14_poll;
180 	sc->init = u14_init;
181 
182 	uha_attach(sc, &upd);
183 }
184 
185 /*
186  * Start the board, ready for normal operation
187  */
188 int
189 u14_find(iot, ioh, sc)
190 	bus_space_tag_t iot;
191 	bus_space_handle_t ioh;
192 	struct uha_probe_data *sc;
193 {
194 	u_int16_t model, config;
195 	int irq, drq;
196 	int resetcount = 4000;	/* 4 secs? */
197 
198 	model = (bus_space_read_1(iot, ioh, U14_ID + 0) << 8) |
199 		(bus_space_read_1(iot, ioh, U14_ID + 1) << 0);
200 	if ((model & 0xfff0) != 0x5640)
201 		return (0);
202 
203 	config = (bus_space_read_1(iot, ioh, U14_CONFIG + 0) << 8) |
204 		 (bus_space_read_1(iot, ioh, U14_CONFIG + 1) << 0);
205 
206 	switch (model & 0x000f) {
207 	case 0x0000:
208 		switch (config & U14_DMA_MASK) {
209 		case U14_DMA_CH5:
210 			drq = 5;
211 			break;
212 		case U14_DMA_CH6:
213 			drq = 6;
214 			break;
215 		case U14_DMA_CH7:
216 			drq = 7;
217 			break;
218 		default:
219 			printf("u14_find: illegal drq setting %x\n",
220 			    config & U14_DMA_MASK);
221 			return (0);
222 		}
223 		break;
224 	case 0x0001:
225 		/* This is a 34f, and doesn't need an ISA DMA channel. */
226 		drq = -1;
227 		break;
228 	default:
229 		printf("u14_find: unknown model %x\n", model);
230 		return (0);
231 	}
232 
233 	switch (config & U14_IRQ_MASK) {
234 	case U14_IRQ10:
235 		irq = 10;
236 		break;
237 	case U14_IRQ11:
238 		irq = 11;
239 		break;
240 	case U14_IRQ14:
241 		irq = 14;
242 		break;
243 	case U14_IRQ15:
244 		irq = 15;
245 		break;
246 	default:
247 		printf("u14_find: illegal irq setting %x\n",
248 		    config & U14_IRQ_MASK);
249 		return (0);
250 	}
251 
252 	bus_space_write_1(iot, ioh, U14_LINT, UHA_ASRST);
253 
254 	while (--resetcount) {
255 		if (bus_space_read_1(iot, ioh, U14_LINT))
256 			break;
257 		delay(1000);	/* 1 mSec per loop */
258 	}
259 	if (!resetcount) {
260 		printf("u14_find: board timed out during reset\n");
261 		return (0);
262 	}
263 
264 	/* if we want to fill in softc, do so now */
265 	if (sc) {
266 		sc->sc_irq = irq;
267 		sc->sc_drq = drq;
268 		sc->sc_scsi_dev = config & U14_HOSTID_MASK;
269 	}
270 
271 	return (1);
272 }
273 
274 /*
275  * Function to send a command out through a mailbox
276  */
277 void
278 u14_start_mbox(sc, mscp)
279 	struct uha_softc *sc;
280 	struct uha_mscp *mscp;
281 {
282 	bus_space_tag_t iot = sc->sc_iot;
283 	bus_space_handle_t ioh = sc->sc_ioh;
284 	int spincount = 100000;	/* 1s should be enough */
285 
286 	while (--spincount) {
287 		if ((bus_space_read_1(iot, ioh, U14_LINT) & U14_LDIP) == 0)
288 			break;
289 		delay(100);
290 	}
291 	if (!spincount) {
292 		printf("%s: uha_start_mbox, board not responding\n",
293 		    sc->sc_dev.dv_xname);
294 		Debugger();
295 	}
296 
297 	bus_space_write_4(iot, ioh, U14_OGMPTR,
298 	    sc->sc_dmamap_mscp->dm_segs[0].ds_addr + UHA_MSCP_OFF(mscp));
299 	if (mscp->flags & MSCP_ABORT)
300 		bus_space_write_1(iot, ioh, U14_LINT, U14_ABORT);
301 	else
302 		bus_space_write_1(iot, ioh, U14_LINT, U14_OGMFULL);
303 
304 	if ((mscp->xs->xs_control & XS_CTL_POLL) == 0)
305 		callout_reset(&mscp->xs->xs_callout,
306 		    (mscp->timeout * hz) / 1000, uha_timeout, mscp);
307 }
308 
309 /*
310  * Function to poll for command completion when in poll mode.
311  *
312  *	wait = timeout in msec
313  */
314 int
315 u14_poll(sc, xs, count)
316 	struct uha_softc *sc;
317 	struct scsipi_xfer *xs;
318 	int count;
319 {
320 	bus_space_tag_t iot = sc->sc_iot;
321 	bus_space_handle_t ioh = sc->sc_ioh;
322 
323 	while (count) {
324 		/*
325 		 * If we had interrupts enabled, would we
326 		 * have got an interrupt?
327 		 */
328 		if (bus_space_read_1(iot, ioh, U14_SINT) & U14_SDIP)
329 			u14_intr(sc);
330 		if (xs->xs_status & XS_STS_DONE)
331 			return (0);
332 		delay(1000);
333 		count--;
334 	}
335 	return (1);
336 }
337 
338 /*
339  * Catch an interrupt from the adaptor
340  */
341 int
342 u14_intr(arg)
343 	void *arg;
344 {
345 	struct uha_softc *sc = arg;
346 	bus_space_tag_t iot = sc->sc_iot;
347 	bus_space_handle_t ioh = sc->sc_ioh;
348 	struct uha_mscp *mscp;
349 	u_char uhastat;
350 	u_long mboxval;
351 
352 #ifdef	UHADEBUG
353 	printf("%s: uhaintr ", sc->sc_dev.dv_xname);
354 #endif /*UHADEBUG */
355 
356 	if ((bus_space_read_1(iot, ioh, U14_SINT) & U14_SDIP) == 0)
357 		return (0);
358 
359 	for (;;) {
360 		/*
361 		 * First get all the information and then
362 		 * acknowledge the interrupt
363 		 */
364 		uhastat = bus_space_read_1(iot, ioh, U14_SINT);
365 		mboxval = bus_space_read_4(iot, ioh, U14_ICMPTR);
366 		/* XXX Send an ABORT_ACK instead? */
367 		bus_space_write_1(iot, ioh, U14_SINT, U14_ICM_ACK);
368 
369 #ifdef	UHADEBUG
370 		printf("status = 0x%x ", uhastat);
371 #endif /*UHADEBUG*/
372 
373 		/*
374 		 * Process the completed operation
375 		 */
376 		mscp = uha_mscp_phys_kv(sc, mboxval);
377 		if (!mscp) {
378 			printf("%s: BAD MSCP RETURNED!\n",
379 			    sc->sc_dev.dv_xname);
380 			continue;	/* whatever it was, it'll timeout */
381 		}
382 
383 		callout_stop(&mscp->xs->xs_callout);
384 		uha_done(sc, mscp);
385 
386 		if ((bus_space_read_1(iot, ioh, U14_SINT) & U14_SDIP) == 0)
387 			return (1);
388 	}
389 }
390 
391 void
392 u14_init(sc)
393 	struct uha_softc *sc;
394 {
395 	bus_space_tag_t iot = sc->sc_iot;
396 	bus_space_handle_t ioh = sc->sc_ioh;
397 
398 	/* make sure interrupts are enabled */
399 #ifdef UHADEBUG
400 	printf("u14_init: lmask=%02x, smask=%02x\n",
401 	    bus_space_read_1(iot, ioh, U14_LMASK),
402 	    bus_space_read_1(iot, ioh, U14_SMASK));
403 #endif
404 	bus_space_write_1(iot, ioh, U14_LMASK, 0xd1);	/* XXX */
405 	bus_space_write_1(iot, ioh, U14_SMASK, 0x91);	/* XXX */
406 }
407