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