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