esp_sbus.c revision 1.10 1 /* $NetBSD: esp_sbus.c,v 1.10 2000/06/05 07:59:55 nisimura Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum; Jason R. Thorpe of the Numerical Aerospace
9 * Simulation Facility, NASA Ames Research Center; Paul Kranenburg.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 #include <sys/types.h>
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/errno.h>
45 #include <sys/device.h>
46 #include <sys/buf.h>
47
48 #include <dev/scsipi/scsi_all.h>
49 #include <dev/scsipi/scsipi_all.h>
50 #include <dev/scsipi/scsiconf.h>
51 #include <dev/scsipi/scsi_message.h>
52
53 #include <machine/bus.h>
54 #include <machine/autoconf.h>
55 #include <machine/cpu.h>
56
57 #include <dev/ic/lsi64854reg.h>
58 #include <dev/ic/lsi64854var.h>
59
60 #include <dev/ic/ncr53c9xreg.h>
61 #include <dev/ic/ncr53c9xvar.h>
62
63 #include <dev/sbus/sbusvar.h>
64
65 struct esp_softc {
66 struct ncr53c9x_softc sc_ncr53c9x; /* glue to MI code */
67 struct sbusdev sc_sd; /* sbus device */
68
69 bus_space_tag_t sc_bustag;
70 bus_dma_tag_t sc_dmatag;
71
72 bus_space_handle_t sc_reg; /* the registers */
73 struct lsi64854_softc *sc_dma; /* pointer to my dma */
74
75 int sc_pri; /* SBUS priority */
76 };
77
78 void espattach_sbus __P((struct device *, struct device *, void *));
79 void espattach_dma __P((struct device *, struct device *, void *));
80 int espmatch_sbus __P((struct device *, struct cfdata *, void *));
81
82
83 /* Linkup to the rest of the kernel */
84 struct cfattach esp_sbus_ca = {
85 sizeof(struct esp_softc), espmatch_sbus, espattach_sbus
86 };
87 struct cfattach esp_dma_ca = {
88 sizeof(struct esp_softc), espmatch_sbus, espattach_dma
89 };
90
91 /*
92 * Functions and the switch for the MI code.
93 */
94 static u_char esp_read_reg __P((struct ncr53c9x_softc *, int));
95 static void esp_write_reg __P((struct ncr53c9x_softc *, int, u_char));
96 static u_char esp_rdreg1 __P((struct ncr53c9x_softc *, int));
97 static void esp_wrreg1 __P((struct ncr53c9x_softc *, int, u_char));
98 static int esp_dma_isintr __P((struct ncr53c9x_softc *));
99 static void esp_dma_reset __P((struct ncr53c9x_softc *));
100 static int esp_dma_intr __P((struct ncr53c9x_softc *));
101 static int esp_dma_setup __P((struct ncr53c9x_softc *, caddr_t *,
102 size_t *, int, size_t *));
103 static void esp_dma_go __P((struct ncr53c9x_softc *));
104 static void esp_dma_stop __P((struct ncr53c9x_softc *));
105 static int esp_dma_isactive __P((struct ncr53c9x_softc *));
106
107 static struct ncr53c9x_glue esp_sbus_glue = {
108 esp_read_reg,
109 esp_write_reg,
110 esp_dma_isintr,
111 esp_dma_reset,
112 esp_dma_intr,
113 esp_dma_setup,
114 esp_dma_go,
115 esp_dma_stop,
116 esp_dma_isactive,
117 NULL, /* gl_clear_latched_intr */
118 };
119
120 static struct ncr53c9x_glue esp_sbus_glue1 = {
121 esp_rdreg1,
122 esp_wrreg1,
123 esp_dma_isintr,
124 esp_dma_reset,
125 esp_dma_intr,
126 esp_dma_setup,
127 esp_dma_go,
128 esp_dma_stop,
129 esp_dma_isactive,
130 NULL, /* gl_clear_latched_intr */
131 };
132
133 static void espattach __P((struct esp_softc *, struct ncr53c9x_glue *));
134
135 int
136 espmatch_sbus(parent, cf, aux)
137 struct device *parent;
138 struct cfdata *cf;
139 void *aux;
140 {
141 int rv;
142 struct sbus_attach_args *sa = aux;
143
144 rv = (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0 ||
145 strcmp("ptscII", sa->sa_name) == 0);
146 return (rv);
147 }
148
149 void
150 espattach_sbus(parent, self, aux)
151 struct device *parent, *self;
152 void *aux;
153 {
154 struct esp_softc *esc = (void *)self;
155 struct ncr53c9x_softc *sc = &esc->sc_ncr53c9x;
156 struct sbus_attach_args *sa = aux;
157
158 esc->sc_bustag = sa->sa_bustag;
159 esc->sc_dmatag = sa->sa_dmatag;
160
161 sc->sc_id = getpropint(sa->sa_node, "initiator-id", 7);
162 sc->sc_freq = getpropint(sa->sa_node, "clock-frequency", -1);
163 if (sc->sc_freq < 0)
164 sc->sc_freq = ((struct sbus_softc *)
165 sc->sc_dev.dv_parent)->sc_clockfreq;
166
167 /*
168 * Find the DMA by poking around the dma device structures
169 *
170 * What happens here is that if the dma driver has not been
171 * configured, then this returns a NULL pointer. Then when the
172 * dma actually gets configured, it does the opposing test, and
173 * if the sc->sc_esp field in it's softc is NULL, then tries to
174 * find the matching esp driver.
175 */
176 esc->sc_dma = (struct lsi64854_softc *)
177 getdevunit("dma", sc->sc_dev.dv_unit);
178
179 /*
180 * and a back pointer to us, for DMA
181 */
182 if (esc->sc_dma)
183 esc->sc_dma->sc_client = sc;
184 else {
185 printf("\n");
186 panic("espattach: no dma found");
187 }
188
189 /*
190 * Map my registers in, if they aren't already in virtual
191 * address space.
192 */
193 if (sa->sa_npromvaddrs)
194 esc->sc_reg = (bus_space_handle_t)sa->sa_promvaddrs[0];
195 else {
196 if (sbus_bus_map(sa->sa_bustag, sa->sa_slot,
197 sa->sa_offset,
198 sa->sa_size,
199 BUS_SPACE_MAP_LINEAR,
200 0, &esc->sc_reg) != 0) {
201 printf("%s @ sbus: cannot map registers\n",
202 self->dv_xname);
203 return;
204 }
205 }
206
207 if (sa->sa_nintr == 0) {
208 /*
209 * No interrupt properties: we quit; this might
210 * happen on e.g. a Sparc X terminal.
211 */
212 printf("\n%s: no interrupt property\n", self->dv_xname);
213 return;
214 }
215
216 esc->sc_pri = sa->sa_pri;
217
218 /* add me to the sbus structures */
219 esc->sc_sd.sd_reset = (void *) ncr53c9x_reset;
220 sbus_establish(&esc->sc_sd, &sc->sc_dev);
221
222 if (strcmp("ptscII", sa->sa_name) == 0) {
223 espattach(esc, &esp_sbus_glue1);
224 } else {
225 espattach(esc, &esp_sbus_glue);
226 }
227 }
228
229 void
230 espattach_dma(parent, self, aux)
231 struct device *parent, *self;
232 void *aux;
233 {
234 struct esp_softc *esc = (void *)self;
235 struct ncr53c9x_softc *sc = &esc->sc_ncr53c9x;
236 struct sbus_attach_args *sa = aux;
237
238 if (strcmp("ptscII", sa->sa_name) == 0) {
239 return;
240 }
241
242 esc->sc_bustag = sa->sa_bustag;
243 esc->sc_dmatag = sa->sa_dmatag;
244
245 sc->sc_id = getpropint(sa->sa_node, "initiator-id", 7);
246 sc->sc_freq = getpropint(sa->sa_node, "clock-frequency", -1);
247
248 esc->sc_dma = (struct lsi64854_softc *)parent;
249 esc->sc_dma->sc_client = sc;
250
251 /*
252 * Map my registers in, if they aren't already in virtual
253 * address space.
254 */
255 if (sa->sa_npromvaddrs)
256 esc->sc_reg = (bus_space_handle_t)sa->sa_promvaddrs[0];
257 else {
258 if (bus_space_map2(sa->sa_bustag,
259 sa->sa_slot,
260 sa->sa_offset,
261 sa->sa_size,
262 BUS_SPACE_MAP_LINEAR,
263 0, &esc->sc_reg) != 0) {
264 printf("%s @ dma: cannot map registers\n",
265 self->dv_xname);
266 return;
267 }
268 }
269
270 if (sa->sa_nintr == 0) {
271 /*
272 * No interrupt properties: we quit; this might
273 * happen on e.g. a Sparc X terminal.
274 */
275 printf("\n%s: no interrupt property\n", self->dv_xname);
276 return;
277 }
278
279 esc->sc_pri = sa->sa_pri;
280
281 /* Assume SBus is grandparent */
282 esc->sc_sd.sd_reset = (void *) ncr53c9x_reset;
283 sbus_establish(&esc->sc_sd, parent);
284
285 espattach(esc, &esp_sbus_glue);
286 }
287
288
289 /*
290 * Attach this instance, and then all the sub-devices
291 */
292 void
293 espattach(esc, gluep)
294 struct esp_softc *esc;
295 struct ncr53c9x_glue *gluep;
296 {
297 struct ncr53c9x_softc *sc = &esc->sc_ncr53c9x;
298 void *icookie;
299
300 /*
301 * Set up glue for MI code early; we use some of it here.
302 */
303 sc->sc_glue = gluep;
304
305 /* gimme Mhz */
306 sc->sc_freq /= 1000000;
307
308 /*
309 * XXX More of this should be in ncr53c9x_attach(), but
310 * XXX should we really poke around the chip that much in
311 * XXX the MI code? Think about this more...
312 */
313
314 /*
315 * It is necessary to try to load the 2nd config register here,
316 * to find out what rev the esp chip is, else the ncr53c9x_reset
317 * will not set up the defaults correctly.
318 */
319 sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB;
320 sc->sc_cfg2 = NCRCFG2_SCSI2 | NCRCFG2_RPE;
321 sc->sc_cfg3 = NCRCFG3_CDB;
322 NCR_WRITE_REG(sc, NCR_CFG2, sc->sc_cfg2);
323
324 if ((NCR_READ_REG(sc, NCR_CFG2) & ~NCRCFG2_RSVD) !=
325 (NCRCFG2_SCSI2 | NCRCFG2_RPE)) {
326 sc->sc_rev = NCR_VARIANT_ESP100;
327 } else {
328 sc->sc_cfg2 = NCRCFG2_SCSI2;
329 NCR_WRITE_REG(sc, NCR_CFG2, sc->sc_cfg2);
330 sc->sc_cfg3 = 0;
331 NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
332 sc->sc_cfg3 = (NCRCFG3_CDB | NCRCFG3_FCLK);
333 NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
334 if (NCR_READ_REG(sc, NCR_CFG3) !=
335 (NCRCFG3_CDB | NCRCFG3_FCLK)) {
336 sc->sc_rev = NCR_VARIANT_ESP100A;
337 } else {
338 /* NCRCFG2_FE enables > 64K transfers */
339 sc->sc_cfg2 |= NCRCFG2_FE;
340 sc->sc_cfg3 = 0;
341 NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
342 sc->sc_rev = NCR_VARIANT_ESP200;
343 }
344 }
345
346 /*
347 * XXX minsync and maxxfer _should_ be set up in MI code,
348 * XXX but it appears to have some dependency on what sort
349 * XXX of DMA we're hooked up to, etc.
350 */
351
352 /*
353 * This is the value used to start sync negotiations
354 * Note that the NCR register "SYNCTP" is programmed
355 * in "clocks per byte", and has a minimum value of 4.
356 * The SCSI period used in negotiation is one-fourth
357 * of the time (in nanoseconds) needed to transfer one byte.
358 * Since the chip's clock is given in MHz, we have the following
359 * formula: 4 * period = (1000 / freq) * 4
360 */
361 sc->sc_minsync = 1000 / sc->sc_freq;
362
363 /*
364 * Alas, we must now modify the value a bit, because it's
365 * only valid when can switch on FASTCLK and FASTSCSI bits
366 * in config register 3...
367 */
368 switch (sc->sc_rev) {
369 case NCR_VARIANT_ESP100:
370 sc->sc_maxxfer = 64 * 1024;
371 sc->sc_minsync = 0; /* No synch on old chip? */
372 break;
373
374 case NCR_VARIANT_ESP100A:
375 sc->sc_maxxfer = 64 * 1024;
376 /* Min clocks/byte is 5 */
377 sc->sc_minsync = ncr53c9x_cpb2stp(sc, 5);
378 break;
379
380 case NCR_VARIANT_ESP200:
381 sc->sc_maxxfer = 16 * 1024 * 1024;
382 /* XXX - do actually set FAST* bits */
383 break;
384 }
385
386 /* Establish interrupt channel */
387 icookie = bus_intr_establish(esc->sc_bustag,
388 esc->sc_pri, 0,
389 ncr53c9x_intr, sc);
390
391 /* register interrupt stats */
392 evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
393 sc->sc_dev.dv_xname, "intr");
394
395 /* Do the common parts of attachment. */
396 ncr53c9x_attach(sc, NULL, NULL);
397
398 /* Turn on target selection using the `dma' method */
399 ncr53c9x_dmaselect = 1;
400 }
401
402 /*
403 * Glue functions.
404 */
405
406 u_char
407 esp_read_reg(sc, reg)
408 struct ncr53c9x_softc *sc;
409 int reg;
410 {
411 struct esp_softc *esc = (struct esp_softc *)sc;
412
413 return (bus_space_read_1(esc->sc_bustag, esc->sc_reg, reg * 4));
414 }
415
416 void
417 esp_write_reg(sc, reg, v)
418 struct ncr53c9x_softc *sc;
419 int reg;
420 u_char v;
421 {
422 struct esp_softc *esc = (struct esp_softc *)sc;
423
424 bus_space_write_1(esc->sc_bustag, esc->sc_reg, reg * 4, v);
425 }
426
427 u_char
428 esp_rdreg1(sc, reg)
429 struct ncr53c9x_softc *sc;
430 int reg;
431 {
432 struct esp_softc *esc = (struct esp_softc *)sc;
433
434 return (bus_space_read_1(esc->sc_bustag, esc->sc_reg, reg));
435 }
436
437 void
438 esp_wrreg1(sc, reg, v)
439 struct ncr53c9x_softc *sc;
440 int reg;
441 u_char v;
442 {
443 struct esp_softc *esc = (struct esp_softc *)sc;
444
445 bus_space_write_1(esc->sc_bustag, esc->sc_reg, reg, v);
446 }
447
448 int
449 esp_dma_isintr(sc)
450 struct ncr53c9x_softc *sc;
451 {
452 struct esp_softc *esc = (struct esp_softc *)sc;
453
454 return (DMA_ISINTR(esc->sc_dma));
455 }
456
457 void
458 esp_dma_reset(sc)
459 struct ncr53c9x_softc *sc;
460 {
461 struct esp_softc *esc = (struct esp_softc *)sc;
462
463 DMA_RESET(esc->sc_dma);
464 }
465
466 int
467 esp_dma_intr(sc)
468 struct ncr53c9x_softc *sc;
469 {
470 struct esp_softc *esc = (struct esp_softc *)sc;
471
472 return (DMA_INTR(esc->sc_dma));
473 }
474
475 int
476 esp_dma_setup(sc, addr, len, datain, dmasize)
477 struct ncr53c9x_softc *sc;
478 caddr_t *addr;
479 size_t *len;
480 int datain;
481 size_t *dmasize;
482 {
483 struct esp_softc *esc = (struct esp_softc *)sc;
484
485 return (DMA_SETUP(esc->sc_dma, addr, len, datain, dmasize));
486 }
487
488 void
489 esp_dma_go(sc)
490 struct ncr53c9x_softc *sc;
491 {
492 struct esp_softc *esc = (struct esp_softc *)sc;
493
494 DMA_GO(esc->sc_dma);
495 }
496
497 void
498 esp_dma_stop(sc)
499 struct ncr53c9x_softc *sc;
500 {
501 struct esp_softc *esc = (struct esp_softc *)sc;
502 u_int32_t csr;
503
504 csr = L64854_GCSR(esc->sc_dma);
505 csr &= ~D_EN_DMA;
506 L64854_SCSR(esc->sc_dma, csr);
507 }
508
509 int
510 esp_dma_isactive(sc)
511 struct ncr53c9x_softc *sc;
512 {
513 struct esp_softc *esc = (struct esp_softc *)sc;
514
515 return (DMA_ISACTIVE(esc->sc_dma));
516 }
517