xref: /netbsd-src/sys/dev/eisa/uha_eisa.c (revision 69b6d498973bb4d7230c2d3c12bd9a032738ec8e)
1 /*	$NetBSD: uha_eisa.c,v 1.22 2004/08/23 06:03:19 thorpej 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_eisa.c,v 1.22 2004/08/23 06:03:19 thorpej 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/eisa/eisavar.h>
59 #include <dev/eisa/eisadevs.h>
60 
61 #include <dev/ic/uhareg.h>
62 #include <dev/ic/uhavar.h>
63 
64 #define	UHA_EISA_SLOT_OFFSET	0xc80
65 #define	UHA_EISA_IOSIZE		0x020
66 
67 static int	uha_eisa_match(struct device *, struct cfdata *, void *);
68 static void	uha_eisa_attach(struct device *, struct device *, void *);
69 
70 CFATTACH_DECL(uha_eisa, sizeof(struct uha_softc),
71     uha_eisa_match, uha_eisa_attach, NULL, NULL);
72 
73 #ifndef	DDB
74 #define Debugger() panic("should call debugger here (uha_eisa.c)")
75 #endif /* ! DDB */
76 
77 static int	u24_find(bus_space_tag_t, bus_space_handle_t,
78 		    struct uha_probe_data *);
79 static void	u24_start_mbox(struct uha_softc *, struct uha_mscp *);
80 static int	u24_poll(struct uha_softc *, struct scsipi_xfer *, int);
81 static int	u24_intr(void *);
82 static void	u24_init(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 static int
90 uha_eisa_match(struct device *parent, struct cfdata *match, void *aux)
91 {
92 	struct eisa_attach_args *ea = aux;
93 	bus_space_tag_t iot = ea->ea_iot;
94 	bus_space_handle_t ioh;
95 	int rv;
96 
97 	/* must match one of our known ID strings */
98 	if (strncmp(ea->ea_idstring, "USC024", 6))
99 		return (0);
100 
101 	if (bus_space_map(iot, EISA_SLOT_ADDR(ea->ea_slot) +
102 	    UHA_EISA_SLOT_OFFSET, UHA_EISA_IOSIZE, 0, &ioh))
103 		return (0);
104 
105 	rv = u24_find(iot, ioh, NULL);
106 
107 	bus_space_unmap(iot, ioh, UHA_EISA_IOSIZE);
108 
109 	return (rv);
110 }
111 
112 /*
113  * Attach all the sub-devices we can find
114  */
115 static void
116 uha_eisa_attach(struct device *parent, struct device *self, void *aux)
117 {
118 	struct eisa_attach_args *ea = aux;
119 	struct uha_softc *sc = (void *)self;
120 	bus_space_tag_t iot = ea->ea_iot;
121 	bus_dma_tag_t dmat = ea->ea_dmat;
122 	bus_space_handle_t ioh;
123 	struct uha_probe_data upd;
124 	eisa_chipset_tag_t ec = ea->ea_ec;
125 	eisa_intr_handle_t ih;
126 	const char *model, *intrstr;
127 
128 	if (!strncmp(ea->ea_idstring, "USC024", 6))
129 		model = EISA_PRODUCT_USC0240;
130 	else
131 		model = "unknown model!";
132 	printf(": %s\n", model);
133 
134 	if (bus_space_map(iot, EISA_SLOT_ADDR(ea->ea_slot) +
135 	    UHA_EISA_SLOT_OFFSET, UHA_EISA_IOSIZE, 0, &ioh))
136 		panic("uha_eisa_attach: could not map I/O addresses");
137 
138 	sc->sc_iot = iot;
139 	sc->sc_ioh = ioh;
140 	sc->sc_dmat = dmat;
141 	if (!u24_find(iot, ioh, &upd))
142 		panic("uha_eisa_attach: u24_find failed!");
143 
144 	sc->sc_dmaflags = 0;
145 
146 	if (eisa_intr_map(ec, upd.sc_irq, &ih)) {
147 		printf("%s: couldn't map interrupt (%d)\n",
148 		    sc->sc_dev.dv_xname, upd.sc_irq);
149 		return;
150 	}
151 	intrstr = eisa_intr_string(ec, ih);
152 	sc->sc_ih = eisa_intr_establish(ec, ih, IST_LEVEL, IPL_BIO,
153 	    u24_intr, sc);
154 	if (sc->sc_ih == NULL) {
155 		printf("%s: couldn't establish interrupt",
156 		    sc->sc_dev.dv_xname);
157 		if (intrstr != NULL)
158 			printf(" at %s", intrstr);
159 		printf("\n");
160 		return;
161 	}
162 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
163 
164 	/* Save function pointers for later use. */
165 	sc->start_mbox = u24_start_mbox;
166 	sc->poll = u24_poll;
167 	sc->init = u24_init;
168 
169 	uha_attach(sc, &upd);
170 }
171 
172 static int
173 u24_find(bus_space_tag_t iot, bus_space_handle_t ioh, struct uha_probe_data *sc)
174 {
175 	u_int8_t config0, config1, config2;
176 	int irq, drq;
177 	int resetcount = 4000;	/* 4 secs? */
178 
179 	config0 = bus_space_read_1(iot, ioh, U24_CONFIG + 0);
180 	config1 = bus_space_read_1(iot, ioh, U24_CONFIG + 1);
181 	config2 = bus_space_read_1(iot, ioh, U24_CONFIG + 2);
182 	if ((config0 & U24_MAGIC1) == 0 ||
183 	    (config1 & U24_MAGIC2) == 0)
184 		return (0);
185 
186 	drq = -1;
187 
188 	switch (config0 & U24_IRQ_MASK) {
189 	case U24_IRQ10:
190 		irq = 10;
191 		break;
192 	case U24_IRQ11:
193 		irq = 11;
194 		break;
195 	case U24_IRQ14:
196 		irq = 14;
197 		break;
198 	case U24_IRQ15:
199 		irq = 15;
200 		break;
201 	default:
202 		printf("u24_find: illegal irq setting %x\n",
203 		    config0 & U24_IRQ_MASK);
204 		return (0);
205 	}
206 
207 	bus_space_write_1(iot, ioh, U24_LINT, UHA_ASRST);
208 
209 	while (--resetcount) {
210 		if (bus_space_read_1(iot, ioh, U24_LINT))
211 			break;
212 		delay(1000);	/* 1 mSec per loop */
213 	}
214 	if (!resetcount) {
215 		printf("u24_find: board timed out during reset\n");
216 		return (0);
217 	}
218 
219 	/* if we want to fill in softc, do so now */
220 	if (sc) {
221 		sc->sc_irq = irq;
222 		sc->sc_drq = drq;
223 		sc->sc_scsi_dev = config2 & U24_HOSTID_MASK;
224 	}
225 
226 	return (1);
227 }
228 
229 static void
230 u24_start_mbox(struct uha_softc *sc, struct uha_mscp *mscp)
231 {
232 	bus_space_tag_t iot = sc->sc_iot;
233 	bus_space_handle_t ioh = sc->sc_ioh;
234 	int spincount = 100000;	/* 1s should be enough */
235 
236 	while (--spincount) {
237 		if ((bus_space_read_1(iot, ioh, U24_LINT) & U24_LDIP) == 0)
238 			break;
239 		delay(100);
240 	}
241 	if (!spincount) {
242 		printf("%s: uha_start_mbox, board not responding\n",
243 		    sc->sc_dev.dv_xname);
244 		Debugger();
245 	}
246 
247 	bus_space_write_4(iot, ioh, U24_OGMPTR,
248 	    sc->sc_dmamap_mscp->dm_segs[0].ds_addr + UHA_MSCP_OFF(mscp));
249 	if (mscp->flags & MSCP_ABORT)
250 		bus_space_write_1(iot, ioh, U24_OGMCMD, 0x80);
251 	else
252 		bus_space_write_1(iot, ioh, U24_OGMCMD, 0x01);
253 	bus_space_write_1(iot, ioh, U24_LINT, U24_OGMFULL);
254 
255 	if ((mscp->xs->xs_control & XS_CTL_POLL) == 0)
256 		callout_reset(&mscp->xs->xs_callout,
257 		    mstohz(mscp->timeout), uha_timeout, mscp);
258 }
259 
260 static int
261 u24_poll(struct uha_softc *sc, struct scsipi_xfer *xs, int count)
262 {
263 	bus_space_tag_t iot = sc->sc_iot;
264 	bus_space_handle_t ioh = sc->sc_ioh;
265 
266 	while (count) {
267 		/*
268 		 * If we had interrupts enabled, would we
269 		 * have got an interrupt?
270 		 */
271 		if (bus_space_read_1(iot, ioh, U24_SINT) & U24_SDIP)
272 			u24_intr(sc);
273 		if (xs->xs_status & XS_STS_DONE)
274 			return (0);
275 		delay(1000);
276 		count--;
277 	}
278 	return (1);
279 }
280 
281 static int
282 u24_intr(void *arg)
283 {
284 	struct uha_softc *sc = arg;
285 	bus_space_tag_t iot = sc->sc_iot;
286 	bus_space_handle_t ioh = sc->sc_ioh;
287 	struct uha_mscp *mscp;
288 	u_char uhastat;
289 	u_long mboxval;
290 
291 #ifdef	UHADEBUG
292 	printf("%s: uhaintr ", sc->sc_dev.dv_xname);
293 #endif /*UHADEBUG */
294 
295 	if ((bus_space_read_1(iot, ioh, U24_SINT) & U24_SDIP) == 0)
296 		return (0);
297 
298 	for (;;) {
299 		/*
300 		 * First get all the information and then
301 		 * acknowledge the interrupt
302 		 */
303 		uhastat = bus_space_read_1(iot, ioh, U24_SINT);
304 		mboxval = bus_space_read_4(iot, ioh, U24_ICMPTR);
305 		bus_space_write_1(iot, ioh, U24_SINT, U24_ICM_ACK);
306 		bus_space_write_1(iot, ioh, U24_ICMCMD, 0);
307 
308 #ifdef	UHADEBUG
309 		printf("status = 0x%x ", uhastat);
310 #endif /*UHADEBUG*/
311 
312 		/*
313 		 * Process the completed operation
314 		 */
315 		mscp = uha_mscp_phys_kv(sc, mboxval);
316 		if (!mscp) {
317 			printf("%s: BAD MSCP RETURNED!\n",
318 			    sc->sc_dev.dv_xname);
319 			continue;	/* whatever it was, it'll timeout */
320 		}
321 		callout_stop(&mscp->xs->xs_callout);
322 		uha_done(sc, mscp);
323 
324 		if ((bus_space_read_1(iot, ioh, U24_SINT) & U24_SDIP) == 0)
325 			return (1);
326 	}
327 }
328 
329 static void
330 u24_init(struct uha_softc *sc)
331 {
332 	bus_space_tag_t iot = sc->sc_iot;
333 	bus_space_handle_t ioh = sc->sc_ioh;
334 
335 	/* free OGM and ICM */
336 	bus_space_write_1(iot, ioh, U24_OGMCMD, 0);
337 	bus_space_write_1(iot, ioh, U24_ICMCMD, 0);
338 	/* make sure interrupts are enabled */
339 #ifdef UHADEBUG
340 	printf("u24_init: lmask=%02x, smask=%02x\n",
341 	    bus_space_read_1(iot, ioh, U24_LMASK),
342 	    bus_space_read_1(iot, ioh, U24_SMASK));
343 #endif
344 	bus_space_write_1(iot, ioh, U24_LMASK, 0xd2);	/* XXX */
345 	bus_space_write_1(iot, ioh, U24_SMASK, 0x92);	/* XXX */
346 }
347