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