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