uha_isa.c revision 1.10 1 /* $NetBSD: uha_isa.c,v 1.10 1997/10/19 18:57:17 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1994, 1996, 1997 Charles M. Hannum. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Charles M. Hannum.
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/device.h>
36 #include <sys/kernel.h>
37 #include <sys/proc.h>
38 #include <sys/user.h>
39
40 #include <machine/bus.h>
41 #include <machine/intr.h>
42
43 #include <dev/scsipi/scsi_all.h>
44 #include <dev/scsipi/scsipi_all.h>
45 #include <dev/scsipi/scsiconf.h>
46
47 #include <dev/isa/isavar.h>
48 #include <dev/isa/isadmavar.h>
49
50 #include <dev/ic/uhareg.h>
51 #include <dev/ic/uhavar.h>
52
53 #define UHA_ISA_IOSIZE 16
54
55 #ifdef __BROKEN_INDIRECT_CONFIG
56 int uha_isa_probe __P((struct device *, void *, void *));
57 #else
58 int uha_isa_probe __P((struct device *, struct cfdata *, void *));
59 #endif
60 void uha_isa_attach __P((struct device *, struct device *, void *));
61
62 struct cfattach uha_isa_ca = {
63 sizeof(struct uha_softc), uha_isa_probe, uha_isa_attach
64 };
65
66 #ifndef DDB
67 #define Debugger() panic("should call debugger here (uha_isa.c)")
68 #endif /* ! DDB */
69
70 int u14_find __P((bus_space_tag_t, bus_space_handle_t,
71 struct uha_probe_data *));
72 void u14_start_mbox __P((struct uha_softc *, struct uha_mscp *));
73 int u14_poll __P((struct uha_softc *, struct scsipi_xfer *, int));
74 int u14_intr __P((void *));
75 void u14_init __P((struct uha_softc *));
76
77 /*
78 * Check the slots looking for a board we recognise
79 * If we find one, note it's address (slot) and call
80 * the actual probe routine to check it out.
81 */
82 int
83 uha_isa_probe(parent, match, aux)
84 struct device *parent;
85 #ifdef __BROKEN_INDIRECT_CONFIG
86 void *match;
87 #else
88 struct cfdata *match;
89 #endif
90 void *aux;
91 {
92 struct isa_attach_args *ia = aux;
93 bus_space_tag_t iot = ia->ia_iot;
94 bus_space_handle_t ioh;
95 struct uha_probe_data upd;
96 int rv;
97
98 /* Disallow wildcarded i/o address. */
99 if (ia->ia_iobase == ISACF_PORT_DEFAULT)
100 return (0);
101
102 if (bus_space_map(iot, ia->ia_iobase, UHA_ISA_IOSIZE, 0, &ioh))
103 return (0);
104
105 rv = u14_find(iot, ioh, &upd);
106
107 bus_space_unmap(iot, ioh, UHA_ISA_IOSIZE);
108
109 if (rv) {
110 if (ia->ia_irq != -1 && ia->ia_irq != upd.sc_irq)
111 return (0);
112 if (ia->ia_drq != -1 && ia->ia_drq != upd.sc_drq)
113 return (0);
114 ia->ia_irq = upd.sc_irq;
115 ia->ia_drq = upd.sc_drq;
116 ia->ia_msize = 0;
117 ia->ia_iosize = UHA_ISA_IOSIZE;
118 }
119 return (rv);
120 }
121
122 /*
123 * Attach all the sub-devices we can find
124 */
125 void
126 uha_isa_attach(parent, self, aux)
127 struct device *parent, *self;
128 void *aux;
129 {
130 struct isa_attach_args *ia = aux;
131 struct uha_softc *sc = (void *)self;
132 bus_space_tag_t iot = ia->ia_iot;
133 bus_dma_tag_t dmat = ia->ia_dmat;
134 bus_space_handle_t ioh;
135 struct uha_probe_data upd;
136 isa_chipset_tag_t ic = ia->ia_ic;
137
138 printf("\n");
139
140 if (bus_space_map(iot, ia->ia_iobase, UHA_ISA_IOSIZE, 0, &ioh))
141 panic("uha_isa_attach: bus_space_map failed!");
142
143 sc->sc_iot = iot;
144 sc->sc_ioh = ioh;
145 sc->sc_dmat = dmat;
146 if (!u14_find(iot, ioh, &upd))
147 panic("uha_isa_attach: u14_find failed!");
148
149 if (upd.sc_drq != -1) {
150 sc->sc_dmaflags = 0;
151 isa_dmacascade(parent, upd.sc_drq);
152 } else {
153 /*
154 * We have a VLB controller, and can do 32-bit DMA.
155 */
156 sc->sc_dmaflags = ISABUS_DMA_32BIT;
157 }
158
159 sc->sc_ih = isa_intr_establish(ic, upd.sc_irq, IST_EDGE, IPL_BIO,
160 u14_intr, sc);
161 if (sc->sc_ih == NULL) {
162 printf("%s: couldn't establish interrupt\n",
163 sc->sc_dev.dv_xname);
164 return;
165 }
166
167 /* Save function pointers for later use. */
168 sc->start_mbox = u14_start_mbox;
169 sc->poll = u14_poll;
170 sc->init = u14_init;
171
172 uha_attach(sc, &upd);
173 }
174
175 /*
176 * Start the board, ready for normal operation
177 */
178 int
179 u14_find(iot, ioh, sc)
180 bus_space_tag_t iot;
181 bus_space_handle_t ioh;
182 struct uha_probe_data *sc;
183 {
184 u_int16_t model, config;
185 int irq, drq;
186 int resetcount = 4000; /* 4 secs? */
187
188 model = (bus_space_read_1(iot, ioh, U14_ID + 0) << 8) |
189 (bus_space_read_1(iot, ioh, U14_ID + 1) << 0);
190 if ((model & 0xfff0) != 0x5640)
191 return (0);
192
193 config = (bus_space_read_1(iot, ioh, U14_CONFIG + 0) << 8) |
194 (bus_space_read_1(iot, ioh, U14_CONFIG + 1) << 0);
195
196 switch (model & 0x000f) {
197 case 0x0000:
198 switch (config & U14_DMA_MASK) {
199 case U14_DMA_CH5:
200 drq = 5;
201 break;
202 case U14_DMA_CH6:
203 drq = 6;
204 break;
205 case U14_DMA_CH7:
206 drq = 7;
207 break;
208 default:
209 printf("u14_find: illegal drq setting %x\n",
210 config & U14_DMA_MASK);
211 return (0);
212 }
213 break;
214 case 0x0001:
215 /* This is a 34f, and doesn't need an ISA DMA channel. */
216 drq = -1;
217 break;
218 default:
219 printf("u14_find: unknown model %x\n", model);
220 return (0);
221 }
222
223 switch (config & U14_IRQ_MASK) {
224 case U14_IRQ10:
225 irq = 10;
226 break;
227 case U14_IRQ11:
228 irq = 11;
229 break;
230 case U14_IRQ14:
231 irq = 14;
232 break;
233 case U14_IRQ15:
234 irq = 15;
235 break;
236 default:
237 printf("u14_find: illegal irq setting %x\n",
238 config & U14_IRQ_MASK);
239 return (0);
240 }
241
242 bus_space_write_1(iot, ioh, U14_LINT, UHA_ASRST);
243
244 while (--resetcount) {
245 if (bus_space_read_1(iot, ioh, U14_LINT))
246 break;
247 delay(1000); /* 1 mSec per loop */
248 }
249 if (!resetcount) {
250 printf("u14_find: board timed out during reset\n");
251 return (0);
252 }
253
254 /* if we want to fill in softc, do so now */
255 if (sc) {
256 sc->sc_irq = irq;
257 sc->sc_drq = drq;
258 sc->sc_scsi_dev = config & U14_HOSTID_MASK;
259 }
260
261 return (1);
262 }
263
264 /*
265 * Function to send a command out through a mailbox
266 */
267 void
268 u14_start_mbox(sc, mscp)
269 struct uha_softc *sc;
270 struct uha_mscp *mscp;
271 {
272 bus_space_tag_t iot = sc->sc_iot;
273 bus_space_handle_t ioh = sc->sc_ioh;
274 int spincount = 100000; /* 1s should be enough */
275
276 while (--spincount) {
277 if ((bus_space_read_1(iot, ioh, U14_LINT) & U14_LDIP) == 0)
278 break;
279 delay(100);
280 }
281 if (!spincount) {
282 printf("%s: uha_start_mbox, board not responding\n",
283 sc->sc_dev.dv_xname);
284 Debugger();
285 }
286
287 bus_space_write_4(iot, ioh, U14_OGMPTR,
288 mscp->dmamap_self->dm_segs[0].ds_addr);
289 if (mscp->flags & MSCP_ABORT)
290 bus_space_write_1(iot, ioh, U14_LINT, U14_ABORT);
291 else
292 bus_space_write_1(iot, ioh, U14_LINT, U14_OGMFULL);
293
294 if ((mscp->xs->flags & SCSI_POLL) == 0)
295 timeout(uha_timeout, mscp, (mscp->timeout * hz) / 1000);
296 }
297
298 /*
299 * Function to poll for command completion when in poll mode.
300 *
301 * wait = timeout in msec
302 */
303 int
304 u14_poll(sc, xs, count)
305 struct uha_softc *sc;
306 struct scsipi_xfer *xs;
307 int count;
308 {
309 bus_space_tag_t iot = sc->sc_iot;
310 bus_space_handle_t ioh = sc->sc_ioh;
311
312 while (count) {
313 /*
314 * If we had interrupts enabled, would we
315 * have got an interrupt?
316 */
317 if (bus_space_read_1(iot, ioh, U14_SINT) & U14_SDIP)
318 u14_intr(sc);
319 if (xs->flags & ITSDONE)
320 return (0);
321 delay(1000);
322 count--;
323 }
324 return (1);
325 }
326
327 /*
328 * Catch an interrupt from the adaptor
329 */
330 int
331 u14_intr(arg)
332 void *arg;
333 {
334 struct uha_softc *sc = arg;
335 bus_space_tag_t iot = sc->sc_iot;
336 bus_space_handle_t ioh = sc->sc_ioh;
337 struct uha_mscp *mscp;
338 u_char uhastat;
339 u_long mboxval;
340
341 #ifdef UHADEBUG
342 printf("%s: uhaintr ", sc->sc_dev.dv_xname);
343 #endif /*UHADEBUG */
344
345 if ((bus_space_read_1(iot, ioh, U14_SINT) & U14_SDIP) == 0)
346 return (0);
347
348 for (;;) {
349 /*
350 * First get all the information and then
351 * acknowledge the interrupt
352 */
353 uhastat = bus_space_read_1(iot, ioh, U14_SINT);
354 mboxval = bus_space_read_4(iot, ioh, U14_ICMPTR);
355 /* XXX Send an ABORT_ACK instead? */
356 bus_space_write_1(iot, ioh, U14_SINT, U14_ICM_ACK);
357
358 #ifdef UHADEBUG
359 printf("status = 0x%x ", uhastat);
360 #endif /*UHADEBUG*/
361
362 /*
363 * Process the completed operation
364 */
365 mscp = uha_mscp_phys_kv(sc, mboxval);
366 if (!mscp) {
367 printf("%s: BAD MSCP RETURNED!\n",
368 sc->sc_dev.dv_xname);
369 continue; /* whatever it was, it'll timeout */
370 }
371
372 untimeout(uha_timeout, mscp);
373 uha_done(sc, mscp);
374
375 if ((bus_space_read_1(iot, ioh, U14_SINT) & U14_SDIP) == 0)
376 return (1);
377 }
378 }
379
380 void
381 u14_init(sc)
382 struct uha_softc *sc;
383 {
384 bus_space_tag_t iot = sc->sc_iot;
385 bus_space_handle_t ioh = sc->sc_ioh;
386
387 /* make sure interrupts are enabled */
388 #ifdef UHADEBUG
389 printf("u14_init: lmask=%02x, smask=%02x\n",
390 bus_space_read_1(iot, ioh, U14_LMASK),
391 bus_space_read_1(iot, ioh, U14_SMASK));
392 #endif
393 bus_space_write_1(iot, ioh, U14_LMASK, 0xd1); /* XXX */
394 bus_space_write_1(iot, ioh, U14_SMASK, 0x91); /* XXX */
395 }
396