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