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