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