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