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