uha_eisa.c revision 1.3 1 1.3 christos /* $NetBSD: uha_eisa.c,v 1.3 1996/10/10 19:54:14 christos Exp $ */
2 1.2 mycroft
3 1.2 mycroft /*
4 1.2 mycroft * Copyright (c) 1994, 1996 Charles M. Hannum. All rights reserved.
5 1.2 mycroft *
6 1.2 mycroft * Redistribution and use in source and binary forms, with or without
7 1.2 mycroft * modification, are permitted provided that the following conditions
8 1.2 mycroft * are met:
9 1.2 mycroft * 1. Redistributions of source code must retain the above copyright
10 1.2 mycroft * notice, this list of conditions and the following disclaimer.
11 1.2 mycroft * 2. Redistributions in binary form must reproduce the above copyright
12 1.2 mycroft * notice, this list of conditions and the following disclaimer in the
13 1.2 mycroft * documentation and/or other materials provided with the distribution.
14 1.2 mycroft * 3. All advertising materials mentioning features or use of this software
15 1.2 mycroft * must display the following acknowledgement:
16 1.2 mycroft * This product includes software developed by Charles M. Hannum.
17 1.2 mycroft * 4. The name of the author may not be used to endorse or promote products
18 1.2 mycroft * derived from this software without specific prior written permission.
19 1.2 mycroft *
20 1.2 mycroft * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 1.2 mycroft * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 1.2 mycroft * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 1.2 mycroft * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 1.2 mycroft * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 1.2 mycroft * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 1.2 mycroft * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 1.2 mycroft * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 1.2 mycroft * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 1.2 mycroft * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 1.2 mycroft */
31 1.2 mycroft
32 1.1 mycroft #include <sys/types.h>
33 1.1 mycroft #include <sys/param.h>
34 1.3 christos #include <sys/systm.h>
35 1.1 mycroft #include <sys/device.h>
36 1.1 mycroft #include <sys/kernel.h>
37 1.1 mycroft #include <sys/proc.h>
38 1.1 mycroft #include <sys/user.h>
39 1.1 mycroft
40 1.1 mycroft #include <machine/bus.h>
41 1.1 mycroft #include <machine/intr.h>
42 1.1 mycroft
43 1.1 mycroft #include <scsi/scsi_all.h>
44 1.1 mycroft #include <scsi/scsiconf.h>
45 1.1 mycroft
46 1.1 mycroft #include <dev/eisa/eisavar.h>
47 1.1 mycroft #include <dev/eisa/eisadevs.h>
48 1.1 mycroft
49 1.1 mycroft #include <dev/ic/uhareg.h>
50 1.1 mycroft #include <dev/ic/uhavar.h>
51 1.1 mycroft
52 1.1 mycroft #define UHA_EISA_SLOT_OFFSET 0xc80
53 1.1 mycroft #define UHA_EISA_IOSIZE 0x020
54 1.1 mycroft
55 1.1 mycroft int uha_eisa_match __P((struct device *, void *, void *));
56 1.1 mycroft void uha_eisa_attach __P((struct device *, struct device *, void *));
57 1.1 mycroft
58 1.1 mycroft struct cfattach uha_eisa_ca = {
59 1.1 mycroft sizeof(struct uha_softc), uha_eisa_match, uha_eisa_attach
60 1.1 mycroft };
61 1.1 mycroft
62 1.1 mycroft #define KVTOPHYS(x) vtophys(x)
63 1.1 mycroft
64 1.1 mycroft int u24_find __P((bus_chipset_tag_t, bus_io_handle_t, struct uha_softc *));
65 1.1 mycroft void u24_start_mbox __P((struct uha_softc *, struct uha_mscp *));
66 1.1 mycroft int u24_poll __P((struct uha_softc *, struct scsi_xfer *, int));
67 1.1 mycroft int u24_intr __P((void *));
68 1.1 mycroft void u24_init __P((struct uha_softc *));
69 1.1 mycroft
70 1.1 mycroft /*
71 1.1 mycroft * Check the slots looking for a board we recognise
72 1.1 mycroft * If we find one, note it's address (slot) and call
73 1.1 mycroft * the actual probe routine to check it out.
74 1.1 mycroft */
75 1.1 mycroft int
76 1.1 mycroft uha_eisa_match(parent, match, aux)
77 1.1 mycroft struct device *parent;
78 1.1 mycroft void *match, *aux;
79 1.1 mycroft {
80 1.1 mycroft struct eisa_attach_args *ea = aux;
81 1.1 mycroft bus_chipset_tag_t bc = ea->ea_bc;
82 1.1 mycroft bus_io_handle_t ioh;
83 1.1 mycroft int rv;
84 1.1 mycroft
85 1.1 mycroft /* must match one of our known ID strings */
86 1.1 mycroft if (strncmp(ea->ea_idstring, "USC024", 6))
87 1.1 mycroft return (0);
88 1.1 mycroft
89 1.1 mycroft if (bus_io_map(bc, EISA_SLOT_ADDR(ea->ea_slot) + UHA_EISA_SLOT_OFFSET,
90 1.1 mycroft UHA_EISA_IOSIZE, &ioh))
91 1.1 mycroft return (0);
92 1.1 mycroft
93 1.1 mycroft rv = u24_find(bc, ioh, NULL);
94 1.1 mycroft
95 1.1 mycroft bus_io_unmap(bc, ioh, UHA_EISA_IOSIZE);
96 1.1 mycroft
97 1.1 mycroft return (rv);
98 1.1 mycroft }
99 1.1 mycroft
100 1.1 mycroft /*
101 1.1 mycroft * Attach all the sub-devices we can find
102 1.1 mycroft */
103 1.1 mycroft void
104 1.1 mycroft uha_eisa_attach(parent, self, aux)
105 1.1 mycroft struct device *parent, *self;
106 1.1 mycroft void *aux;
107 1.1 mycroft {
108 1.1 mycroft struct eisa_attach_args *ea = aux;
109 1.1 mycroft struct uha_softc *sc = (void *)self;
110 1.1 mycroft bus_chipset_tag_t bc = ea->ea_bc;
111 1.1 mycroft bus_io_handle_t ioh;
112 1.1 mycroft eisa_chipset_tag_t ec = ea->ea_ec;
113 1.1 mycroft eisa_intr_handle_t ih;
114 1.1 mycroft const char *model, *intrstr;
115 1.1 mycroft
116 1.1 mycroft if (!strncmp(ea->ea_idstring, "USC024", 6))
117 1.1 mycroft model = EISA_PRODUCT_USC0240;
118 1.1 mycroft else
119 1.1 mycroft model = "unknown model!";
120 1.3 christos kprintf(": %s\n", model);
121 1.1 mycroft
122 1.1 mycroft if (bus_io_map(bc, EISA_SLOT_ADDR(ea->ea_slot) + UHA_EISA_SLOT_OFFSET,
123 1.1 mycroft UHA_EISA_IOSIZE, &ioh))
124 1.1 mycroft panic("uha_attach: could not map I/O addresses");
125 1.1 mycroft
126 1.1 mycroft sc->sc_bc = bc;
127 1.1 mycroft sc->sc_ioh = ioh;
128 1.1 mycroft if (!u24_find(bc, ioh, sc))
129 1.1 mycroft panic("uha_attach: u24_find failed!");
130 1.1 mycroft
131 1.1 mycroft if (eisa_intr_map(ec, sc->sc_irq, &ih)) {
132 1.3 christos kprintf("%s: couldn't map interrupt (%d)\n",
133 1.1 mycroft sc->sc_dev.dv_xname, sc->sc_irq);
134 1.1 mycroft return;
135 1.1 mycroft }
136 1.1 mycroft intrstr = eisa_intr_string(ec, ih);
137 1.1 mycroft sc->sc_ih = eisa_intr_establish(ec, ih, IST_LEVEL, IPL_BIO,
138 1.1 mycroft u24_intr, sc);
139 1.1 mycroft if (sc->sc_ih == NULL) {
140 1.3 christos kprintf("%s: couldn't establish interrupt",
141 1.1 mycroft sc->sc_dev.dv_xname);
142 1.1 mycroft if (intrstr != NULL)
143 1.3 christos kprintf(" at %s", intrstr);
144 1.3 christos kprintf("\n");
145 1.1 mycroft return;
146 1.1 mycroft }
147 1.3 christos kprintf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
148 1.1 mycroft
149 1.1 mycroft /* Save function pointers for later use. */
150 1.1 mycroft sc->start_mbox = u24_start_mbox;
151 1.1 mycroft sc->poll = u24_poll;
152 1.1 mycroft sc->init = u24_init;
153 1.1 mycroft
154 1.1 mycroft uha_attach(sc);
155 1.1 mycroft }
156 1.1 mycroft
157 1.1 mycroft int
158 1.1 mycroft u24_find(bc, ioh, sc)
159 1.1 mycroft bus_chipset_tag_t bc;
160 1.1 mycroft bus_io_handle_t ioh;
161 1.1 mycroft struct uha_softc *sc;
162 1.1 mycroft {
163 1.1 mycroft u_int8_t config0, config1, config2;
164 1.1 mycroft int irq, drq;
165 1.1 mycroft int resetcount = 4000; /* 4 secs? */
166 1.1 mycroft
167 1.1 mycroft config0 = bus_io_read_1(bc, ioh, U24_CONFIG + 0);
168 1.1 mycroft config1 = bus_io_read_1(bc, ioh, U24_CONFIG + 1);
169 1.1 mycroft config2 = bus_io_read_1(bc, ioh, U24_CONFIG + 2);
170 1.1 mycroft if ((config0 & U24_MAGIC1) == 0 ||
171 1.1 mycroft (config1 & U24_MAGIC2) == 0)
172 1.1 mycroft return (0);
173 1.1 mycroft
174 1.1 mycroft drq = -1;
175 1.1 mycroft
176 1.1 mycroft switch (config0 & U24_IRQ_MASK) {
177 1.1 mycroft case U24_IRQ10:
178 1.1 mycroft irq = 10;
179 1.1 mycroft break;
180 1.1 mycroft case U24_IRQ11:
181 1.1 mycroft irq = 11;
182 1.1 mycroft break;
183 1.1 mycroft case U24_IRQ14:
184 1.1 mycroft irq = 14;
185 1.1 mycroft break;
186 1.1 mycroft case U24_IRQ15:
187 1.1 mycroft irq = 15;
188 1.1 mycroft break;
189 1.1 mycroft default:
190 1.3 christos kprintf("u24_find: illegal irq setting %x\n",
191 1.1 mycroft config0 & U24_IRQ_MASK);
192 1.1 mycroft return (0);
193 1.1 mycroft }
194 1.1 mycroft
195 1.1 mycroft bus_io_write_1(bc, ioh, U24_LINT, UHA_ASRST);
196 1.1 mycroft
197 1.1 mycroft while (--resetcount) {
198 1.1 mycroft if (bus_io_read_1(bc, ioh, U24_LINT))
199 1.1 mycroft break;
200 1.1 mycroft delay(1000); /* 1 mSec per loop */
201 1.1 mycroft }
202 1.1 mycroft if (!resetcount) {
203 1.3 christos kprintf("u24_find: board timed out during reset\n");
204 1.1 mycroft return (0);
205 1.1 mycroft }
206 1.1 mycroft
207 1.1 mycroft /* if we want to fill in softc, do so now */
208 1.1 mycroft if (sc != NULL) {
209 1.1 mycroft sc->sc_irq = irq;
210 1.1 mycroft sc->sc_drq = drq;
211 1.1 mycroft sc->sc_scsi_dev = config2 & U24_HOSTID_MASK;
212 1.1 mycroft }
213 1.1 mycroft
214 1.1 mycroft return (1);
215 1.1 mycroft }
216 1.1 mycroft
217 1.1 mycroft void
218 1.1 mycroft u24_start_mbox(sc, mscp)
219 1.1 mycroft struct uha_softc *sc;
220 1.1 mycroft struct uha_mscp *mscp;
221 1.1 mycroft {
222 1.1 mycroft bus_chipset_tag_t bc = sc->sc_bc;
223 1.1 mycroft bus_io_handle_t ioh = sc->sc_ioh;
224 1.1 mycroft int spincount = 100000; /* 1s should be enough */
225 1.1 mycroft
226 1.1 mycroft while (--spincount) {
227 1.1 mycroft if ((bus_io_read_1(bc, ioh, U24_LINT) & U24_LDIP) == 0)
228 1.1 mycroft break;
229 1.1 mycroft delay(100);
230 1.1 mycroft }
231 1.1 mycroft if (!spincount) {
232 1.3 christos kprintf("%s: uha_start_mbox, board not responding\n",
233 1.1 mycroft sc->sc_dev.dv_xname);
234 1.1 mycroft Debugger();
235 1.1 mycroft }
236 1.1 mycroft
237 1.1 mycroft bus_io_write_4(bc, ioh, U24_OGMPTR, KVTOPHYS(mscp));
238 1.1 mycroft if (mscp->flags & MSCP_ABORT)
239 1.1 mycroft bus_io_write_1(bc, ioh, U24_OGMCMD, 0x80);
240 1.1 mycroft else
241 1.1 mycroft bus_io_write_1(bc, ioh, U24_OGMCMD, 0x01);
242 1.1 mycroft bus_io_write_1(bc, ioh, U24_LINT, U24_OGMFULL);
243 1.1 mycroft
244 1.1 mycroft if ((mscp->xs->flags & SCSI_POLL) == 0)
245 1.1 mycroft timeout(uha_timeout, mscp, (mscp->timeout * hz) / 1000);
246 1.1 mycroft }
247 1.1 mycroft
248 1.1 mycroft int
249 1.1 mycroft u24_poll(sc, xs, count)
250 1.1 mycroft struct uha_softc *sc;
251 1.1 mycroft struct scsi_xfer *xs;
252 1.1 mycroft int count;
253 1.1 mycroft {
254 1.1 mycroft bus_chipset_tag_t bc = sc->sc_bc;
255 1.1 mycroft bus_io_handle_t ioh = sc->sc_ioh;
256 1.1 mycroft
257 1.1 mycroft while (count) {
258 1.1 mycroft /*
259 1.1 mycroft * If we had interrupts enabled, would we
260 1.1 mycroft * have got an interrupt?
261 1.1 mycroft */
262 1.1 mycroft if (bus_io_read_1(bc, ioh, U24_SINT) & U24_SDIP)
263 1.1 mycroft u24_intr(sc);
264 1.1 mycroft if (xs->flags & ITSDONE)
265 1.1 mycroft return (0);
266 1.1 mycroft delay(1000);
267 1.1 mycroft count--;
268 1.1 mycroft }
269 1.1 mycroft return (1);
270 1.1 mycroft }
271 1.1 mycroft
272 1.1 mycroft int
273 1.1 mycroft u24_intr(arg)
274 1.1 mycroft void *arg;
275 1.1 mycroft {
276 1.1 mycroft struct uha_softc *sc = arg;
277 1.1 mycroft bus_chipset_tag_t bc = sc->sc_bc;
278 1.1 mycroft bus_io_handle_t ioh = sc->sc_ioh;
279 1.1 mycroft struct uha_mscp *mscp;
280 1.1 mycroft u_char uhastat;
281 1.1 mycroft u_long mboxval;
282 1.1 mycroft
283 1.1 mycroft #ifdef UHADEBUG
284 1.3 christos kprintf("%s: uhaintr ", sc->sc_dev.dv_xname);
285 1.1 mycroft #endif /*UHADEBUG */
286 1.1 mycroft
287 1.1 mycroft if ((bus_io_read_1(bc, ioh, U24_SINT) & U24_SDIP) == 0)
288 1.1 mycroft return (0);
289 1.1 mycroft
290 1.1 mycroft for (;;) {
291 1.1 mycroft /*
292 1.1 mycroft * First get all the information and then
293 1.1 mycroft * acknowledge the interrupt
294 1.1 mycroft */
295 1.1 mycroft uhastat = bus_io_read_1(bc, ioh, U24_SINT);
296 1.1 mycroft mboxval = bus_io_read_4(bc, ioh, U24_ICMPTR);
297 1.1 mycroft bus_io_write_1(bc, ioh, U24_SINT, U24_ICM_ACK);
298 1.1 mycroft bus_io_write_1(bc, ioh, U24_ICMCMD, 0);
299 1.1 mycroft
300 1.1 mycroft #ifdef UHADEBUG
301 1.3 christos kprintf("status = 0x%x ", uhastat);
302 1.1 mycroft #endif /*UHADEBUG*/
303 1.1 mycroft
304 1.1 mycroft /*
305 1.1 mycroft * Process the completed operation
306 1.1 mycroft */
307 1.1 mycroft mscp = uha_mscp_phys_kv(sc, mboxval);
308 1.1 mycroft if (!mscp) {
309 1.3 christos kprintf("%s: BAD MSCP RETURNED!\n",
310 1.1 mycroft sc->sc_dev.dv_xname);
311 1.1 mycroft continue; /* whatever it was, it'll timeout */
312 1.1 mycroft }
313 1.1 mycroft untimeout(uha_timeout, mscp);
314 1.1 mycroft uha_done(sc, mscp);
315 1.1 mycroft
316 1.1 mycroft if ((bus_io_read_1(bc, ioh, U24_SINT) & U24_SDIP) == 0)
317 1.1 mycroft return (1);
318 1.1 mycroft }
319 1.1 mycroft }
320 1.1 mycroft
321 1.1 mycroft void
322 1.1 mycroft u24_init(sc)
323 1.1 mycroft struct uha_softc *sc;
324 1.1 mycroft {
325 1.1 mycroft bus_chipset_tag_t bc = sc->sc_bc;
326 1.1 mycroft bus_io_handle_t ioh = sc->sc_ioh;
327 1.1 mycroft
328 1.1 mycroft /* free OGM and ICM */
329 1.1 mycroft bus_io_write_1(bc, ioh, U24_OGMCMD, 0);
330 1.1 mycroft bus_io_write_1(bc, ioh, U24_ICMCMD, 0);
331 1.1 mycroft /* make sure interrupts are enabled */
332 1.1 mycroft #ifdef UHADEBUG
333 1.3 christos kprintf("u24_init: lmask=%02x, smask=%02x\n",
334 1.1 mycroft bus_io_read_1(bc, ioh, U24_LMASK),
335 1.1 mycroft bus_io_read_1(bc, ioh, U24_SMASK));
336 1.1 mycroft #endif
337 1.1 mycroft bus_io_write_1(bc, ioh, U24_LMASK, 0xd2); /* XXX */
338 1.1 mycroft bus_io_write_1(bc, ioh, U24_SMASK, 0x92); /* XXX */
339 1.1 mycroft }
340