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