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