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