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