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