xref: /openbsd-src/sys/dev/eisa/uha_eisa.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: uha_eisa.c,v 1.13 2014/09/14 14:17:24 jsg Exp $	*/
2 /*	$NetBSD: uha_eisa.c,v 1.5 1996/10/21 22:31:07 thorpej Exp $	*/
3 
4 /*
5  * Copyright (c) 1994, 1996 Charles M. Hannum.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Charles M. Hannum.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/device.h>
37 #include <sys/kernel.h>
38 #include <uvm/uvm_extern.h>
39 
40 #include <machine/bus.h>
41 #include <machine/intr.h>
42 
43 #include <scsi/scsi_all.h>
44 #include <scsi/scsiconf.h>
45 
46 #include <dev/eisa/eisavar.h>
47 #include <dev/eisa/eisadevs.h>
48 
49 #include <dev/ic/uhareg.h>
50 #include <dev/ic/uhavar.h>
51 
52 #define	UHA_EISA_SLOT_OFFSET	0xc80
53 #define	UHA_EISA_IOSIZE		0x020
54 
55 int	uha_eisa_match(struct device *, void *, void *);
56 void	uha_eisa_attach(struct device *, struct device *, void *);
57 
58 struct cfattach uha_eisa_ca = {
59 	sizeof(struct uha_softc), uha_eisa_match, uha_eisa_attach
60 };
61 
62 #define KVTOPHYS(x)	vtophys((vaddr_t)(x))
63 
64 int u24_find(bus_space_tag_t, bus_space_handle_t, struct uha_softc *);
65 void u24_start_mbox(struct uha_softc *, struct uha_mscp *);
66 int u24_poll(struct uha_softc *, struct scsi_xfer *, int);
67 int u24_intr(void *);
68 void u24_init(struct uha_softc *);
69 
70 /*
71  * Check the slots looking for a board we recognise
72  * If we find one, note its address (slot) and call
73  * the actual probe routine to check it out.
74  */
75 int
76 uha_eisa_match(parent, match, aux)
77 	struct device *parent;
78 	void *match, *aux;
79 {
80 	struct eisa_attach_args *ea = aux;
81 	bus_space_tag_t iot = ea->ea_iot;
82 	bus_space_handle_t ioh;
83 	int rv;
84 
85 	/* must match one of our known ID strings */
86 	if (strncmp(ea->ea_idstring, "USC024", 6))
87 		return (0);
88 
89 	if (bus_space_map(iot, EISA_SLOT_ADDR(ea->ea_slot) +
90 	    UHA_EISA_SLOT_OFFSET, UHA_EISA_IOSIZE, 0, &ioh))
91 		return (0);
92 
93 	rv = u24_find(iot, ioh, NULL);
94 
95 	bus_space_unmap(iot, ioh, UHA_EISA_IOSIZE);
96 
97 	return (rv);
98 }
99 
100 /*
101  * Attach all the sub-devices we can find
102  */
103 void
104 uha_eisa_attach(parent, self, aux)
105 	struct device *parent, *self;
106 	void *aux;
107 {
108 	struct eisa_attach_args *ea = aux;
109 	struct uha_softc *sc = (void *)self;
110 	bus_space_tag_t iot = ea->ea_iot;
111 	bus_space_handle_t ioh;
112 	eisa_chipset_tag_t ec = ea->ea_ec;
113 	eisa_intr_handle_t ih;
114 	const char *model, *intrstr;
115 
116 	if (!strncmp(ea->ea_idstring, "USC024", 6))
117 		model = EISA_PRODUCT_USC0240;
118 	else
119 		model = "unknown model!";
120 	printf(": %s\n", model);
121 
122 	if (bus_space_map(iot, EISA_SLOT_ADDR(ea->ea_slot) +
123 	    UHA_EISA_SLOT_OFFSET, UHA_EISA_IOSIZE, 0, &ioh))
124 		panic("uha_attach: can't map I/O addresses");
125 
126 	sc->sc_iot = iot;
127 	sc->sc_ioh = ioh;
128 	if (!u24_find(iot, ioh, sc))
129 		panic("uha_attach: u24_find failed!");
130 
131 	if (eisa_intr_map(ec, sc->sc_irq, &ih)) {
132 		printf("%s: couldn't map interrupt (%d)\n",
133 		    sc->sc_dev.dv_xname, sc->sc_irq);
134 		return;
135 	}
136 	intrstr = eisa_intr_string(ec, ih);
137 	sc->sc_ih = eisa_intr_establish(ec, ih, IST_LEVEL, IPL_BIO,
138 	    u24_intr, sc, sc->sc_dev.dv_xname);
139 	if (sc->sc_ih == NULL) {
140 		printf("%s: couldn't establish interrupt",
141 		    sc->sc_dev.dv_xname);
142 		if (intrstr != NULL)
143 			printf(" at %s", intrstr);
144 		printf("\n");
145 		return;
146 	}
147 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
148 
149 	/* Save function pointers for later use. */
150 	sc->start_mbox = u24_start_mbox;
151 	sc->poll = u24_poll;
152 	sc->init = u24_init;
153 
154 	uha_attach(sc);
155 }
156 
157 int
158 u24_find(iot, ioh, sc)
159 	bus_space_tag_t iot;
160 	bus_space_handle_t ioh;
161 	struct uha_softc *sc;
162 {
163 	u_int8_t config0, config1, config2;
164 	int irq, drq;
165 	int resetcount = 4000;	/* 4 secs? */
166 
167 	config0 = bus_space_read_1(iot, ioh, U24_CONFIG + 0);
168 	config1 = bus_space_read_1(iot, ioh, U24_CONFIG + 1);
169 	config2 = bus_space_read_1(iot, ioh, U24_CONFIG + 2);
170 	if ((config0 & U24_MAGIC1) == 0 ||
171 	    (config1 & U24_MAGIC2) == 0)
172 		return (0);
173 
174 	drq = -1;
175 
176 	switch (config0 & U24_IRQ_MASK) {
177 	case U24_IRQ10:
178 		irq = 10;
179 		break;
180 	case U24_IRQ11:
181 		irq = 11;
182 		break;
183 	case U24_IRQ14:
184 		irq = 14;
185 		break;
186 	case U24_IRQ15:
187 		irq = 15;
188 		break;
189 	default:
190 		printf("u24_find: illegal irq setting %x\n",
191 		    config0 & U24_IRQ_MASK);
192 		return (0);
193 	}
194 
195 	bus_space_write_1(iot, ioh, U24_LINT, UHA_ASRST);
196 
197 	while (--resetcount) {
198 		if (bus_space_read_1(iot, ioh, U24_LINT))
199 			break;
200 		delay(1000);	/* 1 mSec per loop */
201 	}
202 	if (!resetcount) {
203 		printf("u24_find: board timed out during reset\n");
204 		return (0);
205 	}
206 
207 	/* if we want to fill in softc, do so now */
208 	if (sc != NULL) {
209 		sc->sc_irq = irq;
210 		sc->sc_drq = drq;
211 		sc->sc_scsi_dev = config2 & U24_HOSTID_MASK;
212 	}
213 
214 	return (1);
215 }
216 
217 void
218 u24_start_mbox(sc, mscp)
219 	struct uha_softc *sc;
220 	struct uha_mscp *mscp;
221 {
222 	bus_space_tag_t iot = sc->sc_iot;
223 	bus_space_handle_t ioh = sc->sc_ioh;
224 	int spincount = 100000;	/* 1s should be enough */
225 
226 	while (--spincount) {
227 		if ((bus_space_read_1(iot, ioh, U24_LINT) & U24_LDIP) == 0)
228 			break;
229 		delay(100);
230 	}
231 	if (!spincount)
232 		panic("%s: uha_start_mbox, board not responding",
233 		    sc->sc_dev.dv_xname);
234 
235 	bus_space_write_4(iot, ioh, U24_OGMPTR, KVTOPHYS(mscp));
236 	if (mscp->flags & MSCP_ABORT)
237 		bus_space_write_1(iot, ioh, U24_OGMCMD, 0x80);
238 	else
239 		bus_space_write_1(iot, ioh, U24_OGMCMD, 0x01);
240 	bus_space_write_1(iot, ioh, U24_LINT, U24_OGMFULL);
241 
242 	if ((mscp->xs->flags & SCSI_POLL) == 0)
243 		timeout_add_msec(&mscp->xs->stimeout, mscp->timeout);
244 }
245 
246 int
247 u24_poll(sc, xs, count)
248 	struct uha_softc *sc;
249 	struct scsi_xfer *xs;
250 	int count;
251 {
252 	bus_space_tag_t iot = sc->sc_iot;
253 	bus_space_handle_t ioh = sc->sc_ioh;
254 	int s;
255 
256 	while (count) {
257 		/*
258 		 * If we had interrupts enabled, would we
259 		 * have got an interrupt?
260 		 */
261 		if (bus_space_read_1(iot, ioh, U24_SINT) & U24_SDIP) {
262 			s = splbio();
263 			u24_intr(sc);
264 			splx(s);
265 		}
266 		if (xs->flags & ITSDONE)
267 			return (0);
268 		delay(1000);
269 		count--;
270 	}
271 	return (1);
272 }
273 
274 int
275 u24_intr(arg)
276 	void *arg;
277 {
278 	struct uha_softc *sc = arg;
279 	bus_space_tag_t iot = sc->sc_iot;
280 	bus_space_handle_t ioh = sc->sc_ioh;
281 	struct uha_mscp *mscp;
282 	u_char uhastat;
283 	u_long mboxval;
284 
285 #ifdef	UHADEBUG
286 	printf("%s: uhaintr ", sc->sc_dev.dv_xname);
287 #endif /*UHADEBUG */
288 
289 	if ((bus_space_read_1(iot, ioh, U24_SINT) & U24_SDIP) == 0)
290 		return (0);
291 
292 	for (;;) {
293 		/*
294 		 * First get all the information and then
295 		 * acknowledge the interrupt
296 		 */
297 		uhastat = bus_space_read_1(iot, ioh, U24_SINT);
298 		mboxval = bus_space_read_4(iot, ioh, U24_ICMPTR);
299 		bus_space_write_1(iot, ioh, U24_SINT, U24_ICM_ACK);
300 		bus_space_write_1(iot, ioh, U24_ICMCMD, 0);
301 
302 #ifdef	UHADEBUG
303 		printf("status = 0x%x ", uhastat);
304 #endif /*UHADEBUG*/
305 
306 		/*
307 		 * Process the completed operation
308 		 */
309 		mscp = uha_mscp_phys_kv(sc, mboxval);
310 		if (!mscp) {
311 			printf("%s: BAD MSCP RETURNED!\n",
312 			    sc->sc_dev.dv_xname);
313 			continue;	/* whatever it was, it'll timeout */
314 		}
315 		timeout_del(&mscp->xs->stimeout);
316 		uha_done(sc, mscp);
317 
318 		if ((bus_space_read_1(iot, ioh, U24_SINT) & U24_SDIP) == 0)
319 			return (1);
320 	}
321 }
322 
323 void
324 u24_init(sc)
325 	struct uha_softc *sc;
326 {
327 	bus_space_tag_t iot = sc->sc_iot;
328 	bus_space_handle_t ioh = sc->sc_ioh;
329 
330 	/* free OGM and ICM */
331 	bus_space_write_1(iot, ioh, U24_OGMCMD, 0);
332 	bus_space_write_1(iot, ioh, U24_ICMCMD, 0);
333 	/* make sure interrupts are enabled */
334 #ifdef UHADEBUG
335 	printf("u24_init: lmask=%02x, smask=%02x\n",
336 	    bus_space_read_1(iot, ioh, U24_LMASK),
337 	    bus_space_read_1(iot, ioh, U24_SMASK));
338 #endif
339 	bus_space_write_1(iot, ioh, U24_LMASK, 0xd2);	/* XXX */
340 	bus_space_write_1(iot, ioh, U24_SMASK, 0x92);	/* XXX */
341 }
342