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