esp_sbus.c revision 1.20 1 1.20 eeh /* $NetBSD: esp_sbus.c,v 1.20 2002/03/21 00:16:15 eeh Exp $ */
2 1.1 pk
3 1.1 pk /*-
4 1.1 pk * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
5 1.1 pk * All rights reserved.
6 1.1 pk *
7 1.1 pk * This code is derived from software contributed to The NetBSD Foundation
8 1.1 pk * by Charles M. Hannum; Jason R. Thorpe of the Numerical Aerospace
9 1.1 pk * Simulation Facility, NASA Ames Research Center; Paul Kranenburg.
10 1.1 pk *
11 1.1 pk * Redistribution and use in source and binary forms, with or without
12 1.1 pk * modification, are permitted provided that the following conditions
13 1.1 pk * are met:
14 1.1 pk * 1. Redistributions of source code must retain the above copyright
15 1.1 pk * notice, this list of conditions and the following disclaimer.
16 1.1 pk * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 pk * notice, this list of conditions and the following disclaimer in the
18 1.1 pk * documentation and/or other materials provided with the distribution.
19 1.1 pk * 3. All advertising materials mentioning features or use of this software
20 1.1 pk * must display the following acknowledgement:
21 1.1 pk * This product includes software developed by the NetBSD
22 1.1 pk * Foundation, Inc. and its contributors.
23 1.1 pk * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.1 pk * contributors may be used to endorse or promote products derived
25 1.1 pk * from this software without specific prior written permission.
26 1.1 pk *
27 1.1 pk * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.1 pk * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.1 pk * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.1 pk * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.1 pk * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.1 pk * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.1 pk * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.1 pk * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.1 pk * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.1 pk * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.1 pk * POSSIBILITY OF SUCH DAMAGE.
38 1.1 pk */
39 1.17 lukem
40 1.17 lukem #include <sys/cdefs.h>
41 1.20 eeh __KERNEL_RCSID(0, "$NetBSD: esp_sbus.c,v 1.20 2002/03/21 00:16:15 eeh Exp $");
42 1.1 pk
43 1.1 pk #include <sys/param.h>
44 1.1 pk #include <sys/systm.h>
45 1.1 pk #include <sys/device.h>
46 1.1 pk #include <sys/buf.h>
47 1.13 petrov #include <sys/malloc.h>
48 1.1 pk
49 1.1 pk #include <dev/scsipi/scsi_all.h>
50 1.1 pk #include <dev/scsipi/scsipi_all.h>
51 1.1 pk #include <dev/scsipi/scsiconf.h>
52 1.1 pk #include <dev/scsipi/scsi_message.h>
53 1.1 pk
54 1.1 pk #include <machine/bus.h>
55 1.11 pk #include <machine/intr.h>
56 1.1 pk #include <machine/autoconf.h>
57 1.1 pk
58 1.1 pk #include <dev/ic/lsi64854reg.h>
59 1.1 pk #include <dev/ic/lsi64854var.h>
60 1.1 pk
61 1.1 pk #include <dev/ic/ncr53c9xreg.h>
62 1.1 pk #include <dev/ic/ncr53c9xvar.h>
63 1.1 pk
64 1.1 pk #include <dev/sbus/sbusvar.h>
65 1.1 pk
66 1.13 petrov /* #define ESP_SBUS_DEBUG */
67 1.13 petrov
68 1.1 pk struct esp_softc {
69 1.1 pk struct ncr53c9x_softc sc_ncr53c9x; /* glue to MI code */
70 1.1 pk struct sbusdev sc_sd; /* sbus device */
71 1.1 pk
72 1.1 pk bus_space_tag_t sc_bustag;
73 1.1 pk bus_dma_tag_t sc_dmatag;
74 1.1 pk
75 1.1 pk bus_space_handle_t sc_reg; /* the registers */
76 1.1 pk struct lsi64854_softc *sc_dma; /* pointer to my dma */
77 1.1 pk
78 1.1 pk int sc_pri; /* SBUS priority */
79 1.1 pk };
80 1.1 pk
81 1.1 pk void espattach_sbus __P((struct device *, struct device *, void *));
82 1.1 pk void espattach_dma __P((struct device *, struct device *, void *));
83 1.1 pk int espmatch_sbus __P((struct device *, struct cfdata *, void *));
84 1.1 pk
85 1.1 pk
86 1.1 pk /* Linkup to the rest of the kernel */
87 1.1 pk struct cfattach esp_sbus_ca = {
88 1.1 pk sizeof(struct esp_softc), espmatch_sbus, espattach_sbus
89 1.1 pk };
90 1.1 pk struct cfattach esp_dma_ca = {
91 1.1 pk sizeof(struct esp_softc), espmatch_sbus, espattach_dma
92 1.1 pk };
93 1.1 pk
94 1.1 pk /*
95 1.1 pk * Functions and the switch for the MI code.
96 1.1 pk */
97 1.1 pk static u_char esp_read_reg __P((struct ncr53c9x_softc *, int));
98 1.1 pk static void esp_write_reg __P((struct ncr53c9x_softc *, int, u_char));
99 1.6 mjacob static u_char esp_rdreg1 __P((struct ncr53c9x_softc *, int));
100 1.6 mjacob static void esp_wrreg1 __P((struct ncr53c9x_softc *, int, u_char));
101 1.1 pk static int esp_dma_isintr __P((struct ncr53c9x_softc *));
102 1.1 pk static void esp_dma_reset __P((struct ncr53c9x_softc *));
103 1.1 pk static int esp_dma_intr __P((struct ncr53c9x_softc *));
104 1.1 pk static int esp_dma_setup __P((struct ncr53c9x_softc *, caddr_t *,
105 1.1 pk size_t *, int, size_t *));
106 1.1 pk static void esp_dma_go __P((struct ncr53c9x_softc *));
107 1.1 pk static void esp_dma_stop __P((struct ncr53c9x_softc *));
108 1.1 pk static int esp_dma_isactive __P((struct ncr53c9x_softc *));
109 1.1 pk
110 1.1 pk static struct ncr53c9x_glue esp_sbus_glue = {
111 1.1 pk esp_read_reg,
112 1.1 pk esp_write_reg,
113 1.1 pk esp_dma_isintr,
114 1.1 pk esp_dma_reset,
115 1.1 pk esp_dma_intr,
116 1.1 pk esp_dma_setup,
117 1.1 pk esp_dma_go,
118 1.1 pk esp_dma_stop,
119 1.1 pk esp_dma_isactive,
120 1.1 pk NULL, /* gl_clear_latched_intr */
121 1.1 pk };
122 1.1 pk
123 1.6 mjacob static struct ncr53c9x_glue esp_sbus_glue1 = {
124 1.6 mjacob esp_rdreg1,
125 1.6 mjacob esp_wrreg1,
126 1.6 mjacob esp_dma_isintr,
127 1.6 mjacob esp_dma_reset,
128 1.6 mjacob esp_dma_intr,
129 1.6 mjacob esp_dma_setup,
130 1.6 mjacob esp_dma_go,
131 1.6 mjacob esp_dma_stop,
132 1.6 mjacob esp_dma_isactive,
133 1.6 mjacob NULL, /* gl_clear_latched_intr */
134 1.6 mjacob };
135 1.6 mjacob
136 1.6 mjacob static void espattach __P((struct esp_softc *, struct ncr53c9x_glue *));
137 1.6 mjacob
138 1.1 pk int
139 1.1 pk espmatch_sbus(parent, cf, aux)
140 1.1 pk struct device *parent;
141 1.1 pk struct cfdata *cf;
142 1.1 pk void *aux;
143 1.1 pk {
144 1.6 mjacob int rv;
145 1.1 pk struct sbus_attach_args *sa = aux;
146 1.1 pk
147 1.13 petrov if (strcmp("SUNW,fas", sa->sa_name) == 0)
148 1.13 petrov return 1;
149 1.13 petrov
150 1.6 mjacob rv = (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0 ||
151 1.6 mjacob strcmp("ptscII", sa->sa_name) == 0);
152 1.6 mjacob return (rv);
153 1.1 pk }
154 1.1 pk
155 1.1 pk void
156 1.1 pk espattach_sbus(parent, self, aux)
157 1.1 pk struct device *parent, *self;
158 1.1 pk void *aux;
159 1.1 pk {
160 1.1 pk struct esp_softc *esc = (void *)self;
161 1.1 pk struct ncr53c9x_softc *sc = &esc->sc_ncr53c9x;
162 1.1 pk struct sbus_attach_args *sa = aux;
163 1.13 petrov struct lsi64854_softc *lsc;
164 1.13 petrov int burst, sbusburst;
165 1.1 pk
166 1.1 pk esc->sc_bustag = sa->sa_bustag;
167 1.1 pk esc->sc_dmatag = sa->sa_dmatag;
168 1.1 pk
169 1.16 eeh sc->sc_id = PROM_getpropint(sa->sa_node, "initiator-id", 7);
170 1.16 eeh sc->sc_freq = PROM_getpropint(sa->sa_node, "clock-frequency", -1);
171 1.1 pk if (sc->sc_freq < 0)
172 1.1 pk sc->sc_freq = ((struct sbus_softc *)
173 1.1 pk sc->sc_dev.dv_parent)->sc_clockfreq;
174 1.1 pk
175 1.13 petrov #ifdef ESP_SBUS_DEBUG
176 1.13 petrov printf("%s: espattach_sbus: sc_id %d, freq %d\n",
177 1.13 petrov self->dv_xname, sc->sc_id, sc->sc_freq);
178 1.13 petrov #endif
179 1.13 petrov
180 1.13 petrov if (strcmp("SUNW,fas", sa->sa_name) == 0) {
181 1.13 petrov
182 1.13 petrov /*
183 1.13 petrov * fas has 2 register spaces: dma(lsi64854) and SCSI core (ncr53c9x)
184 1.13 petrov */
185 1.13 petrov if (sa->sa_nreg != 2) {
186 1.13 petrov printf("%s: %d register spaces\n", self->dv_xname, sa->sa_nreg);
187 1.13 petrov return;
188 1.13 petrov }
189 1.13 petrov
190 1.13 petrov /*
191 1.13 petrov * allocate space for dma, in SUNW,fas there are no separate
192 1.13 petrov * dma device
193 1.13 petrov */
194 1.13 petrov lsc = malloc(sizeof (struct lsi64854_softc), M_DEVBUF, M_NOWAIT);
195 1.13 petrov
196 1.13 petrov if (lsc == NULL) {
197 1.13 petrov printf("%s: out of memory (lsi64854_softc)\n",
198 1.13 petrov self->dv_xname);
199 1.13 petrov return;
200 1.13 petrov }
201 1.13 petrov esc->sc_dma = lsc;
202 1.13 petrov
203 1.13 petrov lsc->sc_bustag = sa->sa_bustag;
204 1.13 petrov lsc->sc_dmatag = sa->sa_dmatag;
205 1.13 petrov
206 1.13 petrov bcopy(sc->sc_dev.dv_xname, lsc->sc_dev.dv_xname,
207 1.13 petrov sizeof (lsc->sc_dev.dv_xname));
208 1.13 petrov
209 1.13 petrov /* Map dma registers */
210 1.20 eeh if (sa->sa_npromvaddrs) {
211 1.20 eeh sbus_promaddr_to_handle(sa->sa_bustag,
212 1.20 eeh sa->sa_promvaddrs[0], &lsc->sc_regs);
213 1.20 eeh } else {
214 1.20 eeh if (sbus_bus_map(sa->sa_bustag,
215 1.20 eeh sa->sa_reg[0].sbr_slot,
216 1.20 eeh sa->sa_reg[0].sbr_offset,
217 1.20 eeh sa->sa_reg[0].sbr_size,
218 1.20 eeh 0, &lsc->sc_regs) != 0) {
219 1.20 eeh printf("%s: cannot map dma registers\n",
220 1.20 eeh self->dv_xname);
221 1.20 eeh return;
222 1.20 eeh }
223 1.13 petrov }
224 1.13 petrov
225 1.13 petrov /*
226 1.13 petrov * XXX is this common(from bpp.c), the same in dma_sbus...etc.
227 1.13 petrov *
228 1.13 petrov * Get transfer burst size from PROM and plug it into the
229 1.13 petrov * controller registers. This is needed on the Sun4m; do
230 1.13 petrov * others need it too?
231 1.13 petrov */
232 1.13 petrov sbusburst = ((struct sbus_softc *)parent)->sc_burst;
233 1.13 petrov if (sbusburst == 0)
234 1.13 petrov sbusburst = SBUS_BURST_32 - 1; /* 1->16 */
235 1.13 petrov
236 1.16 eeh burst = PROM_getpropint(sa->sa_node, "burst-sizes", -1);
237 1.13 petrov
238 1.13 petrov #if ESP_SBUS_DEBUG
239 1.13 petrov printf("espattach_sbus: burst 0x%x, sbus 0x%x\n",
240 1.13 petrov burst, sbusburst);
241 1.13 petrov #endif
242 1.13 petrov
243 1.13 petrov if (burst == -1)
244 1.13 petrov /* take SBus burst sizes */
245 1.13 petrov burst = sbusburst;
246 1.13 petrov
247 1.13 petrov /* Clamp at parent's burst sizes */
248 1.13 petrov burst &= sbusburst;
249 1.13 petrov lsc->sc_burst = (burst & SBUS_BURST_32) ? 32 :
250 1.13 petrov (burst & SBUS_BURST_16) ? 16 : 0;
251 1.13 petrov
252 1.13 petrov lsc->sc_channel = L64854_CHANNEL_SCSI;
253 1.13 petrov lsc->sc_client = sc;
254 1.13 petrov
255 1.13 petrov lsi64854_attach(lsc);
256 1.13 petrov
257 1.13 petrov /*
258 1.13 petrov * map SCSI core registers
259 1.13 petrov */
260 1.20 eeh if (sa->sa_npromvaddrs > 1) {
261 1.20 eeh sbus_promaddr_to_handle(sa->sa_bustag,
262 1.20 eeh sa->sa_promvaddrs[1], &esc->sc_reg);
263 1.20 eeh } else {
264 1.20 eeh if (sbus_bus_map(sa->sa_bustag,
265 1.20 eeh sa->sa_reg[1].sbr_slot,
266 1.20 eeh sa->sa_reg[1].sbr_offset,
267 1.20 eeh sa->sa_reg[1].sbr_size,
268 1.20 eeh 0, &esc->sc_reg) != 0) {
269 1.20 eeh printf("%s @ sbus: "
270 1.20 eeh "cannot map scsi core registers\n",
271 1.20 eeh self->dv_xname);
272 1.20 eeh return;
273 1.20 eeh }
274 1.13 petrov }
275 1.13 petrov
276 1.13 petrov if (sa->sa_nintr == 0) {
277 1.13 petrov printf("\n%s: no interrupt property\n", self->dv_xname);
278 1.13 petrov return;
279 1.13 petrov }
280 1.13 petrov
281 1.13 petrov esc->sc_pri = sa->sa_pri;
282 1.13 petrov
283 1.13 petrov /* add me to the sbus structures */
284 1.13 petrov esc->sc_sd.sd_reset = (void *) ncr53c9x_reset;
285 1.13 petrov sbus_establish(&esc->sc_sd, &sc->sc_dev);
286 1.13 petrov
287 1.13 petrov espattach(esc, &esp_sbus_glue);
288 1.13 petrov
289 1.13 petrov return;
290 1.13 petrov }
291 1.13 petrov
292 1.1 pk /*
293 1.1 pk * Find the DMA by poking around the dma device structures
294 1.1 pk *
295 1.1 pk * What happens here is that if the dma driver has not been
296 1.1 pk * configured, then this returns a NULL pointer. Then when the
297 1.1 pk * dma actually gets configured, it does the opposing test, and
298 1.1 pk * if the sc->sc_esp field in it's softc is NULL, then tries to
299 1.1 pk * find the matching esp driver.
300 1.1 pk */
301 1.1 pk esc->sc_dma = (struct lsi64854_softc *)
302 1.1 pk getdevunit("dma", sc->sc_dev.dv_unit);
303 1.1 pk
304 1.1 pk /*
305 1.1 pk * and a back pointer to us, for DMA
306 1.1 pk */
307 1.1 pk if (esc->sc_dma)
308 1.2 pk esc->sc_dma->sc_client = sc;
309 1.1 pk else {
310 1.1 pk printf("\n");
311 1.1 pk panic("espattach: no dma found");
312 1.1 pk }
313 1.15 pk
314 1.15 pk /*
315 1.15 pk * The `ESC' DMA chip must be reset before we can access
316 1.15 pk * the esp registers.
317 1.15 pk */
318 1.15 pk if (esc->sc_dma->sc_rev == DMAREV_ESC)
319 1.15 pk DMA_RESET(esc->sc_dma);
320 1.1 pk
321 1.1 pk /*
322 1.1 pk * Map my registers in, if they aren't already in virtual
323 1.1 pk * address space.
324 1.1 pk */
325 1.20 eeh if (sa->sa_npromvaddrs) {
326 1.20 eeh sbus_promaddr_to_handle(sa->sa_bustag,
327 1.20 eeh sa->sa_promvaddrs[0], &esc->sc_reg);
328 1.20 eeh } else {
329 1.19 pk if (sbus_bus_map(sa->sa_bustag,
330 1.20 eeh sa->sa_slot, sa->sa_offset, sa->sa_size,
331 1.20 eeh 0, &esc->sc_reg) != 0) {
332 1.1 pk printf("%s @ sbus: cannot map registers\n",
333 1.1 pk self->dv_xname);
334 1.1 pk return;
335 1.1 pk }
336 1.1 pk }
337 1.1 pk
338 1.7 pk if (sa->sa_nintr == 0) {
339 1.7 pk /*
340 1.7 pk * No interrupt properties: we quit; this might
341 1.7 pk * happen on e.g. a Sparc X terminal.
342 1.7 pk */
343 1.7 pk printf("\n%s: no interrupt property\n", self->dv_xname);
344 1.7 pk return;
345 1.7 pk }
346 1.7 pk
347 1.1 pk esc->sc_pri = sa->sa_pri;
348 1.1 pk
349 1.1 pk /* add me to the sbus structures */
350 1.1 pk esc->sc_sd.sd_reset = (void *) ncr53c9x_reset;
351 1.1 pk sbus_establish(&esc->sc_sd, &sc->sc_dev);
352 1.1 pk
353 1.6 mjacob if (strcmp("ptscII", sa->sa_name) == 0) {
354 1.6 mjacob espattach(esc, &esp_sbus_glue1);
355 1.6 mjacob } else {
356 1.6 mjacob espattach(esc, &esp_sbus_glue);
357 1.6 mjacob }
358 1.1 pk }
359 1.1 pk
360 1.1 pk void
361 1.1 pk espattach_dma(parent, self, aux)
362 1.1 pk struct device *parent, *self;
363 1.1 pk void *aux;
364 1.1 pk {
365 1.1 pk struct esp_softc *esc = (void *)self;
366 1.1 pk struct ncr53c9x_softc *sc = &esc->sc_ncr53c9x;
367 1.1 pk struct sbus_attach_args *sa = aux;
368 1.1 pk
369 1.6 mjacob if (strcmp("ptscII", sa->sa_name) == 0) {
370 1.6 mjacob return;
371 1.6 mjacob }
372 1.6 mjacob
373 1.1 pk esc->sc_bustag = sa->sa_bustag;
374 1.1 pk esc->sc_dmatag = sa->sa_dmatag;
375 1.1 pk
376 1.16 eeh sc->sc_id = PROM_getpropint(sa->sa_node, "initiator-id", 7);
377 1.16 eeh sc->sc_freq = PROM_getpropint(sa->sa_node, "clock-frequency", -1);
378 1.1 pk
379 1.1 pk esc->sc_dma = (struct lsi64854_softc *)parent;
380 1.2 pk esc->sc_dma->sc_client = sc;
381 1.1 pk
382 1.1 pk /*
383 1.1 pk * Map my registers in, if they aren't already in virtual
384 1.1 pk * address space.
385 1.1 pk */
386 1.20 eeh if (sa->sa_npromvaddrs) {
387 1.20 eeh sbus_promaddr_to_handle(sa->sa_bustag,
388 1.20 eeh sa->sa_promvaddrs[0], &esc->sc_reg);
389 1.20 eeh } else {
390 1.19 pk if (sbus_bus_map(sa->sa_bustag,
391 1.20 eeh sa->sa_slot, sa->sa_offset, sa->sa_size,
392 1.20 eeh 0, &esc->sc_reg) != 0) {
393 1.1 pk printf("%s @ dma: cannot map registers\n",
394 1.1 pk self->dv_xname);
395 1.1 pk return;
396 1.1 pk }
397 1.1 pk }
398 1.1 pk
399 1.7 pk if (sa->sa_nintr == 0) {
400 1.7 pk /*
401 1.7 pk * No interrupt properties: we quit; this might
402 1.7 pk * happen on e.g. a Sparc X terminal.
403 1.7 pk */
404 1.7 pk printf("\n%s: no interrupt property\n", self->dv_xname);
405 1.7 pk return;
406 1.7 pk }
407 1.7 pk
408 1.1 pk esc->sc_pri = sa->sa_pri;
409 1.1 pk
410 1.1 pk /* Assume SBus is grandparent */
411 1.1 pk esc->sc_sd.sd_reset = (void *) ncr53c9x_reset;
412 1.1 pk sbus_establish(&esc->sc_sd, parent);
413 1.1 pk
414 1.6 mjacob espattach(esc, &esp_sbus_glue);
415 1.1 pk }
416 1.1 pk
417 1.1 pk
418 1.1 pk /*
419 1.1 pk * Attach this instance, and then all the sub-devices
420 1.1 pk */
421 1.1 pk void
422 1.6 mjacob espattach(esc, gluep)
423 1.1 pk struct esp_softc *esc;
424 1.6 mjacob struct ncr53c9x_glue *gluep;
425 1.1 pk {
426 1.1 pk struct ncr53c9x_softc *sc = &esc->sc_ncr53c9x;
427 1.1 pk void *icookie;
428 1.13 petrov unsigned int uid = 0;
429 1.1 pk
430 1.1 pk /*
431 1.1 pk * Set up glue for MI code early; we use some of it here.
432 1.1 pk */
433 1.6 mjacob sc->sc_glue = gluep;
434 1.1 pk
435 1.1 pk /* gimme Mhz */
436 1.1 pk sc->sc_freq /= 1000000;
437 1.1 pk
438 1.1 pk /*
439 1.1 pk * XXX More of this should be in ncr53c9x_attach(), but
440 1.1 pk * XXX should we really poke around the chip that much in
441 1.1 pk * XXX the MI code? Think about this more...
442 1.1 pk */
443 1.1 pk
444 1.1 pk /*
445 1.1 pk * It is necessary to try to load the 2nd config register here,
446 1.1 pk * to find out what rev the esp chip is, else the ncr53c9x_reset
447 1.1 pk * will not set up the defaults correctly.
448 1.1 pk */
449 1.1 pk sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB;
450 1.1 pk sc->sc_cfg2 = NCRCFG2_SCSI2 | NCRCFG2_RPE;
451 1.13 petrov sc->sc_cfg3 = NCRCFG3_CDB;
452 1.1 pk NCR_WRITE_REG(sc, NCR_CFG2, sc->sc_cfg2);
453 1.1 pk
454 1.1 pk if ((NCR_READ_REG(sc, NCR_CFG2) & ~NCRCFG2_RSVD) !=
455 1.1 pk (NCRCFG2_SCSI2 | NCRCFG2_RPE)) {
456 1.1 pk sc->sc_rev = NCR_VARIANT_ESP100;
457 1.1 pk } else {
458 1.1 pk sc->sc_cfg2 = NCRCFG2_SCSI2;
459 1.1 pk NCR_WRITE_REG(sc, NCR_CFG2, sc->sc_cfg2);
460 1.1 pk sc->sc_cfg3 = 0;
461 1.1 pk NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
462 1.13 petrov sc->sc_cfg3 = (NCRCFG3_CDB | NCRCFG3_FCLK);
463 1.1 pk NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
464 1.1 pk if (NCR_READ_REG(sc, NCR_CFG3) !=
465 1.1 pk (NCRCFG3_CDB | NCRCFG3_FCLK)) {
466 1.1 pk sc->sc_rev = NCR_VARIANT_ESP100A;
467 1.1 pk } else {
468 1.1 pk /* NCRCFG2_FE enables > 64K transfers */
469 1.1 pk sc->sc_cfg2 |= NCRCFG2_FE;
470 1.13 petrov sc->sc_cfg3 = 0;
471 1.1 pk NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
472 1.1 pk sc->sc_rev = NCR_VARIANT_ESP200;
473 1.13 petrov
474 1.13 petrov /* XXX spec says it's valid after power up or chip reset */
475 1.13 petrov uid = NCR_READ_REG(sc, NCR_UID);
476 1.13 petrov if (((uid & 0xf8) >> 3) == 0x0a) /* XXX */
477 1.13 petrov sc->sc_rev = NCR_VARIANT_FAS366;
478 1.1 pk }
479 1.1 pk }
480 1.1 pk
481 1.13 petrov #ifdef ESP_SBUS_DEBUG
482 1.13 petrov printf("espattach: revision %d, uid 0x%x\n", sc->sc_rev, uid);
483 1.13 petrov #endif
484 1.13 petrov
485 1.1 pk /*
486 1.1 pk * XXX minsync and maxxfer _should_ be set up in MI code,
487 1.1 pk * XXX but it appears to have some dependency on what sort
488 1.1 pk * XXX of DMA we're hooked up to, etc.
489 1.1 pk */
490 1.1 pk
491 1.1 pk /*
492 1.1 pk * This is the value used to start sync negotiations
493 1.1 pk * Note that the NCR register "SYNCTP" is programmed
494 1.1 pk * in "clocks per byte", and has a minimum value of 4.
495 1.1 pk * The SCSI period used in negotiation is one-fourth
496 1.1 pk * of the time (in nanoseconds) needed to transfer one byte.
497 1.1 pk * Since the chip's clock is given in MHz, we have the following
498 1.1 pk * formula: 4 * period = (1000 / freq) * 4
499 1.1 pk */
500 1.1 pk sc->sc_minsync = 1000 / sc->sc_freq;
501 1.1 pk
502 1.1 pk /*
503 1.1 pk * Alas, we must now modify the value a bit, because it's
504 1.1 pk * only valid when can switch on FASTCLK and FASTSCSI bits
505 1.1 pk * in config register 3...
506 1.1 pk */
507 1.1 pk switch (sc->sc_rev) {
508 1.1 pk case NCR_VARIANT_ESP100:
509 1.1 pk sc->sc_maxxfer = 64 * 1024;
510 1.1 pk sc->sc_minsync = 0; /* No synch on old chip? */
511 1.1 pk break;
512 1.1 pk
513 1.1 pk case NCR_VARIANT_ESP100A:
514 1.1 pk sc->sc_maxxfer = 64 * 1024;
515 1.1 pk /* Min clocks/byte is 5 */
516 1.1 pk sc->sc_minsync = ncr53c9x_cpb2stp(sc, 5);
517 1.1 pk break;
518 1.1 pk
519 1.1 pk case NCR_VARIANT_ESP200:
520 1.13 petrov case NCR_VARIANT_FAS366:
521 1.1 pk sc->sc_maxxfer = 16 * 1024 * 1024;
522 1.1 pk /* XXX - do actually set FAST* bits */
523 1.1 pk break;
524 1.1 pk }
525 1.1 pk
526 1.1 pk /* Establish interrupt channel */
527 1.11 pk icookie = bus_intr_establish(esc->sc_bustag, esc->sc_pri, IPL_BIO, 0,
528 1.10 nisimura ncr53c9x_intr, sc);
529 1.1 pk
530 1.1 pk /* register interrupt stats */
531 1.9 cgd evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
532 1.9 cgd sc->sc_dev.dv_xname, "intr");
533 1.1 pk
534 1.13 petrov /* Turn on target selection using the `dma' method */
535 1.13 petrov if (sc->sc_rev != NCR_VARIANT_FAS366)
536 1.13 petrov sc->sc_features |= NCR_F_DMASELECT;
537 1.13 petrov
538 1.1 pk /* Do the common parts of attachment. */
539 1.14 bouyer sc->sc_adapter.adapt_minphys = minphys;
540 1.14 bouyer sc->sc_adapter.adapt_request = ncr53c9x_scsipi_request;
541 1.14 bouyer ncr53c9x_attach(sc);
542 1.14 bouyer
543 1.1 pk }
544 1.1 pk
545 1.1 pk /*
546 1.1 pk * Glue functions.
547 1.1 pk */
548 1.1 pk
549 1.13 petrov #ifdef ESP_SBUS_DEBUG
550 1.13 petrov int esp_sbus_debug = 0;
551 1.13 petrov
552 1.13 petrov static struct {
553 1.13 petrov char *r_name;
554 1.13 petrov int r_flag;
555 1.13 petrov } esp__read_regnames [] = {
556 1.13 petrov { "TCL", 0}, /* 0/00 */
557 1.13 petrov { "TCM", 0}, /* 1/04 */
558 1.13 petrov { "FIFO", 0}, /* 2/08 */
559 1.13 petrov { "CMD", 0}, /* 3/0c */
560 1.13 petrov { "STAT", 0}, /* 4/10 */
561 1.13 petrov { "INTR", 0}, /* 5/14 */
562 1.13 petrov { "STEP", 0}, /* 6/18 */
563 1.13 petrov { "FFLAGS", 1}, /* 7/1c */
564 1.13 petrov { "CFG1", 1}, /* 8/20 */
565 1.13 petrov { "STAT2", 0}, /* 9/24 */
566 1.13 petrov { "CFG4", 1}, /* a/28 */
567 1.13 petrov { "CFG2", 1}, /* b/2c */
568 1.13 petrov { "CFG3", 1}, /* c/30 */
569 1.13 petrov { "-none", 1}, /* d/34 */
570 1.13 petrov { "TCH", 1}, /* e/38 */
571 1.13 petrov { "TCX", 1}, /* f/3c */
572 1.13 petrov };
573 1.13 petrov
574 1.13 petrov static struct {
575 1.13 petrov char *r_name;
576 1.13 petrov int r_flag;
577 1.13 petrov } esp__write_regnames[] = {
578 1.13 petrov { "TCL", 1}, /* 0/00 */
579 1.13 petrov { "TCM", 1}, /* 1/04 */
580 1.13 petrov { "FIFO", 0}, /* 2/08 */
581 1.13 petrov { "CMD", 0}, /* 3/0c */
582 1.13 petrov { "SELID", 1}, /* 4/10 */
583 1.13 petrov { "TIMEOUT", 1}, /* 5/14 */
584 1.13 petrov { "SYNCTP", 1}, /* 6/18 */
585 1.13 petrov { "SYNCOFF", 1}, /* 7/1c */
586 1.13 petrov { "CFG1", 1}, /* 8/20 */
587 1.13 petrov { "CCF", 1}, /* 9/24 */
588 1.13 petrov { "TEST", 1}, /* a/28 */
589 1.13 petrov { "CFG2", 1}, /* b/2c */
590 1.13 petrov { "CFG3", 1}, /* c/30 */
591 1.13 petrov { "-none", 1}, /* d/34 */
592 1.13 petrov { "TCH", 1}, /* e/38 */
593 1.13 petrov { "TCX", 1}, /* f/3c */
594 1.13 petrov };
595 1.13 petrov #endif
596 1.13 petrov
597 1.1 pk u_char
598 1.1 pk esp_read_reg(sc, reg)
599 1.1 pk struct ncr53c9x_softc *sc;
600 1.1 pk int reg;
601 1.1 pk {
602 1.1 pk struct esp_softc *esc = (struct esp_softc *)sc;
603 1.13 petrov u_char v;
604 1.1 pk
605 1.13 petrov v = bus_space_read_1(esc->sc_bustag, esc->sc_reg, reg * 4);
606 1.13 petrov #ifdef ESP_SBUS_DEBUG
607 1.13 petrov if (esp_sbus_debug && (reg < 0x10) && esp__read_regnames[reg].r_flag)
608 1.13 petrov printf("RD:%x <%s> %x\n", reg * 4,
609 1.13 petrov ((unsigned)reg < 0x10) ? esp__read_regnames[reg].r_name : "<***>", v);
610 1.13 petrov #endif
611 1.13 petrov return v;
612 1.1 pk }
613 1.1 pk
614 1.1 pk void
615 1.1 pk esp_write_reg(sc, reg, v)
616 1.1 pk struct ncr53c9x_softc *sc;
617 1.1 pk int reg;
618 1.1 pk u_char v;
619 1.1 pk {
620 1.1 pk struct esp_softc *esc = (struct esp_softc *)sc;
621 1.1 pk
622 1.13 petrov #ifdef ESP_SBUS_DEBUG
623 1.13 petrov if (esp_sbus_debug && (reg < 0x10) && esp__write_regnames[reg].r_flag)
624 1.13 petrov printf("WR:%x <%s> %x\n", reg * 4,
625 1.13 petrov ((unsigned)reg < 0x10) ? esp__write_regnames[reg].r_name : "<***>", v);
626 1.13 petrov #endif
627 1.1 pk bus_space_write_1(esc->sc_bustag, esc->sc_reg, reg * 4, v);
628 1.6 mjacob }
629 1.6 mjacob
630 1.6 mjacob u_char
631 1.6 mjacob esp_rdreg1(sc, reg)
632 1.6 mjacob struct ncr53c9x_softc *sc;
633 1.6 mjacob int reg;
634 1.6 mjacob {
635 1.6 mjacob struct esp_softc *esc = (struct esp_softc *)sc;
636 1.6 mjacob
637 1.6 mjacob return (bus_space_read_1(esc->sc_bustag, esc->sc_reg, reg));
638 1.6 mjacob }
639 1.6 mjacob
640 1.6 mjacob void
641 1.6 mjacob esp_wrreg1(sc, reg, v)
642 1.6 mjacob struct ncr53c9x_softc *sc;
643 1.6 mjacob int reg;
644 1.6 mjacob u_char v;
645 1.6 mjacob {
646 1.6 mjacob struct esp_softc *esc = (struct esp_softc *)sc;
647 1.6 mjacob
648 1.6 mjacob bus_space_write_1(esc->sc_bustag, esc->sc_reg, reg, v);
649 1.1 pk }
650 1.1 pk
651 1.1 pk int
652 1.1 pk esp_dma_isintr(sc)
653 1.1 pk struct ncr53c9x_softc *sc;
654 1.1 pk {
655 1.1 pk struct esp_softc *esc = (struct esp_softc *)sc;
656 1.1 pk
657 1.1 pk return (DMA_ISINTR(esc->sc_dma));
658 1.1 pk }
659 1.1 pk
660 1.1 pk void
661 1.1 pk esp_dma_reset(sc)
662 1.1 pk struct ncr53c9x_softc *sc;
663 1.1 pk {
664 1.1 pk struct esp_softc *esc = (struct esp_softc *)sc;
665 1.1 pk
666 1.1 pk DMA_RESET(esc->sc_dma);
667 1.1 pk }
668 1.1 pk
669 1.1 pk int
670 1.1 pk esp_dma_intr(sc)
671 1.1 pk struct ncr53c9x_softc *sc;
672 1.1 pk {
673 1.1 pk struct esp_softc *esc = (struct esp_softc *)sc;
674 1.1 pk
675 1.1 pk return (DMA_INTR(esc->sc_dma));
676 1.1 pk }
677 1.1 pk
678 1.1 pk int
679 1.1 pk esp_dma_setup(sc, addr, len, datain, dmasize)
680 1.1 pk struct ncr53c9x_softc *sc;
681 1.1 pk caddr_t *addr;
682 1.1 pk size_t *len;
683 1.1 pk int datain;
684 1.1 pk size_t *dmasize;
685 1.1 pk {
686 1.1 pk struct esp_softc *esc = (struct esp_softc *)sc;
687 1.1 pk
688 1.1 pk return (DMA_SETUP(esc->sc_dma, addr, len, datain, dmasize));
689 1.1 pk }
690 1.1 pk
691 1.1 pk void
692 1.1 pk esp_dma_go(sc)
693 1.1 pk struct ncr53c9x_softc *sc;
694 1.1 pk {
695 1.1 pk struct esp_softc *esc = (struct esp_softc *)sc;
696 1.1 pk
697 1.1 pk DMA_GO(esc->sc_dma);
698 1.1 pk }
699 1.1 pk
700 1.1 pk void
701 1.1 pk esp_dma_stop(sc)
702 1.1 pk struct ncr53c9x_softc *sc;
703 1.1 pk {
704 1.1 pk struct esp_softc *esc = (struct esp_softc *)sc;
705 1.1 pk u_int32_t csr;
706 1.1 pk
707 1.1 pk csr = L64854_GCSR(esc->sc_dma);
708 1.1 pk csr &= ~D_EN_DMA;
709 1.1 pk L64854_SCSR(esc->sc_dma, csr);
710 1.1 pk }
711 1.1 pk
712 1.1 pk int
713 1.1 pk esp_dma_isactive(sc)
714 1.1 pk struct ncr53c9x_softc *sc;
715 1.1 pk {
716 1.1 pk struct esp_softc *esc = (struct esp_softc *)sc;
717 1.1 pk
718 1.1 pk return (DMA_ISACTIVE(esc->sc_dma));
719 1.1 pk }
720 1.12 eeh
721 1.12 eeh #include "opt_ddb.h"
722 1.12 eeh #ifdef DDB
723 1.12 eeh #include <machine/db_machdep.h>
724 1.12 eeh #include <ddb/db_output.h>
725 1.12 eeh
726 1.12 eeh void db_esp __P((db_expr_t, int, db_expr_t, char*));
727 1.12 eeh
728 1.12 eeh void
729 1.12 eeh db_esp(addr, have_addr, count, modif)
730 1.12 eeh db_expr_t addr;
731 1.12 eeh int have_addr;
732 1.12 eeh db_expr_t count;
733 1.12 eeh char *modif;
734 1.12 eeh {
735 1.12 eeh struct ncr53c9x_softc *sc;
736 1.12 eeh struct ncr53c9x_ecb *ecb;
737 1.12 eeh struct ncr53c9x_linfo *li;
738 1.12 eeh int u, t, i;
739 1.12 eeh
740 1.12 eeh for (u=0; u<10; u++) {
741 1.12 eeh sc = (struct ncr53c9x_softc *)
742 1.12 eeh getdevunit("esp", u);
743 1.12 eeh if (!sc) continue;
744 1.12 eeh
745 1.12 eeh db_printf("esp%d: nexus %p phase %x prev %x dp %p dleft %lx ify %x\n",
746 1.12 eeh u, sc->sc_nexus, sc->sc_phase, sc->sc_prevphase,
747 1.12 eeh sc->sc_dp, sc->sc_dleft, sc->sc_msgify);
748 1.12 eeh db_printf("\tmsgout %x msgpriq %x msgin %x:%x:%x:%x:%x\n",
749 1.12 eeh sc->sc_msgout, sc->sc_msgpriq, sc->sc_imess[0],
750 1.12 eeh sc->sc_imess[1], sc->sc_imess[2], sc->sc_imess[3],
751 1.12 eeh sc->sc_imess[0]);
752 1.12 eeh db_printf("ready: ");
753 1.12 eeh for (ecb = sc->ready_list.tqh_first; ecb; ecb = ecb->chain.tqe_next) {
754 1.12 eeh db_printf("ecb %p ", ecb);
755 1.12 eeh if (ecb == ecb->chain.tqe_next) {
756 1.12 eeh db_printf("\nWARNING: tailq loop on ecb %p", ecb);
757 1.12 eeh break;
758 1.12 eeh }
759 1.12 eeh }
760 1.12 eeh db_printf("\n");
761 1.12 eeh
762 1.12 eeh for (t=0; t<NCR_NTARG; t++) {
763 1.12 eeh LIST_FOREACH(li, &sc->sc_tinfo[t].luns, link) {
764 1.12 eeh db_printf("t%d lun %d untagged %p busy %d used %x\n",
765 1.12 eeh t, (int)li->lun, li->untagged, li->busy,
766 1.12 eeh li->used);
767 1.12 eeh for (i=0; i<256; i++)
768 1.12 eeh if ((ecb = li->queued[i])) {
769 1.12 eeh db_printf("ecb %p tag %x\n", ecb, i);
770 1.12 eeh }
771 1.12 eeh }
772 1.12 eeh }
773 1.12 eeh }
774 1.12 eeh }
775 1.12 eeh #endif
776 1.13 petrov
777