esp_sbus.c revision 1.6 1 /* $NetBSD: esp_sbus.c,v 1.6 1999/03/26 06:48:40 mjacob 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 /*
79 * Is this esp on the bootpath?
80 * We may get two forms of the bootpath:
81 * (1) ../sbus (at) .../esp@<offset>,<slot>/sd@.. (PROM v3 style)
82 * (2) /sbus0/esp0/sd@.. (PROM v2 style)
83 */
84 #define SAME_ESP(sc, bp, sa) \
85 ((bp->val[0] == sa->sa_slot && bp->val[1] == sa->sa_offset) || \
86 (bp->val[0] == -1 && bp->val[1] == sc->sc_dev.dv_unit))
87
88 void espattach_sbus __P((struct device *, struct device *, void *));
89 void espattach_dma __P((struct device *, struct device *, void *));
90 int espmatch_sbus __P((struct device *, struct cfdata *, void *));
91
92
93 /* Linkup to the rest of the kernel */
94 struct cfattach esp_sbus_ca = {
95 sizeof(struct esp_softc), espmatch_sbus, espattach_sbus
96 };
97 struct cfattach esp_dma_ca = {
98 sizeof(struct esp_softc), espmatch_sbus, espattach_dma
99 };
100
101 static struct scsipi_device esp_sbus_dev = {
102 NULL, /* Use default error handler */
103 NULL, /* have a queue, served by this */
104 NULL, /* have no async handler */
105 NULL, /* Use default 'done' routine */
106 };
107
108 /*
109 * Functions and the switch for the MI code.
110 */
111 static u_char esp_read_reg __P((struct ncr53c9x_softc *, int));
112 static void esp_write_reg __P((struct ncr53c9x_softc *, int, u_char));
113 static u_char esp_rdreg1 __P((struct ncr53c9x_softc *, int));
114 static void esp_wrreg1 __P((struct ncr53c9x_softc *, int, u_char));
115 static int esp_dma_isintr __P((struct ncr53c9x_softc *));
116 static void esp_dma_reset __P((struct ncr53c9x_softc *));
117 static int esp_dma_intr __P((struct ncr53c9x_softc *));
118 static int esp_dma_setup __P((struct ncr53c9x_softc *, caddr_t *,
119 size_t *, int, size_t *));
120 static void esp_dma_go __P((struct ncr53c9x_softc *));
121 static void esp_dma_stop __P((struct ncr53c9x_softc *));
122 static int esp_dma_isactive __P((struct ncr53c9x_softc *));
123
124 static struct ncr53c9x_glue esp_sbus_glue = {
125 esp_read_reg,
126 esp_write_reg,
127 esp_dma_isintr,
128 esp_dma_reset,
129 esp_dma_intr,
130 esp_dma_setup,
131 esp_dma_go,
132 esp_dma_stop,
133 esp_dma_isactive,
134 NULL, /* gl_clear_latched_intr */
135 };
136
137 static struct ncr53c9x_glue esp_sbus_glue1 = {
138 esp_rdreg1,
139 esp_wrreg1,
140 esp_dma_isintr,
141 esp_dma_reset,
142 esp_dma_intr,
143 esp_dma_setup,
144 esp_dma_go,
145 esp_dma_stop,
146 esp_dma_isactive,
147 NULL, /* gl_clear_latched_intr */
148 };
149
150 static void espattach __P((struct esp_softc *, struct ncr53c9x_glue *));
151
152 int
153 espmatch_sbus(parent, cf, aux)
154 struct device *parent;
155 struct cfdata *cf;
156 void *aux;
157 {
158 int rv;
159 struct sbus_attach_args *sa = aux;
160
161 rv = (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0 ||
162 strcmp("ptscII", sa->sa_name) == 0);
163 return (rv);
164 }
165
166 void
167 espattach_sbus(parent, self, aux)
168 struct device *parent, *self;
169 void *aux;
170 {
171 struct esp_softc *esc = (void *)self;
172 struct ncr53c9x_softc *sc = &esc->sc_ncr53c9x;
173 struct sbus_attach_args *sa = aux;
174
175 esc->sc_bustag = sa->sa_bustag;
176 esc->sc_dmatag = sa->sa_dmatag;
177
178 sc->sc_id = getpropint(sa->sa_node, "initiator-id", 7);
179 sc->sc_freq = getpropint(sa->sa_node, "clock-frequency", -1);
180 if (sc->sc_freq < 0)
181 sc->sc_freq = ((struct sbus_softc *)
182 sc->sc_dev.dv_parent)->sc_clockfreq;
183
184 /*
185 * Find the DMA by poking around the dma device structures
186 *
187 * What happens here is that if the dma driver has not been
188 * configured, then this returns a NULL pointer. Then when the
189 * dma actually gets configured, it does the opposing test, and
190 * if the sc->sc_esp field in it's softc is NULL, then tries to
191 * find the matching esp driver.
192 */
193 esc->sc_dma = (struct lsi64854_softc *)
194 getdevunit("dma", sc->sc_dev.dv_unit);
195
196 /*
197 * and a back pointer to us, for DMA
198 */
199 if (esc->sc_dma)
200 esc->sc_dma->sc_client = sc;
201 else {
202 printf("\n");
203 panic("espattach: no dma found");
204 }
205
206 /*
207 * Map my registers in, if they aren't already in virtual
208 * address space.
209 */
210 if (sa->sa_npromvaddrs)
211 esc->sc_reg = (bus_space_handle_t)sa->sa_promvaddrs[0];
212 else {
213 if (sbus_bus_map(sa->sa_bustag, sa->sa_slot,
214 sa->sa_offset,
215 sa->sa_size,
216 BUS_SPACE_MAP_LINEAR,
217 0, &esc->sc_reg) != 0) {
218 printf("%s @ sbus: cannot map registers\n",
219 self->dv_xname);
220 return;
221 }
222 }
223
224 esc->sc_pri = sa->sa_pri;
225
226 /* add me to the sbus structures */
227 esc->sc_sd.sd_reset = (void *) ncr53c9x_reset;
228 sbus_establish(&esc->sc_sd, &sc->sc_dev);
229
230 if (sa->sa_bp != NULL && strcmp("esp", sa->sa_bp->name) == 0 &&
231 SAME_ESP(sc, sa->sa_bp, sa))
232 bootpath_store(1, sa->sa_bp + 1);
233
234 if (strcmp("ptscII", sa->sa_name) == 0) {
235 espattach(esc, &esp_sbus_glue1);
236 } else {
237 espattach(esc, &esp_sbus_glue);
238 }
239 }
240
241 void
242 espattach_dma(parent, self, aux)
243 struct device *parent, *self;
244 void *aux;
245 {
246 struct esp_softc *esc = (void *)self;
247 struct ncr53c9x_softc *sc = &esc->sc_ncr53c9x;
248 struct sbus_attach_args *sa = aux;
249
250 if (strcmp("ptscII", sa->sa_name) == 0) {
251 return;
252 }
253
254 esc->sc_bustag = sa->sa_bustag;
255 esc->sc_dmatag = sa->sa_dmatag;
256
257 sc->sc_id = getpropint(sa->sa_node, "initiator-id", 7);
258 sc->sc_freq = getpropint(sa->sa_node, "clock-frequency", -1);
259
260 esc->sc_dma = (struct lsi64854_softc *)parent;
261 esc->sc_dma->sc_client = sc;
262
263 /*
264 * Map my registers in, if they aren't already in virtual
265 * address space.
266 */
267 if (sa->sa_npromvaddrs)
268 esc->sc_reg = (bus_space_handle_t)sa->sa_promvaddrs[0];
269 else {
270 if (bus_space_map2(sa->sa_bustag,
271 sa->sa_slot,
272 sa->sa_offset,
273 sa->sa_size,
274 BUS_SPACE_MAP_LINEAR,
275 0, &esc->sc_reg) != 0) {
276 printf("%s @ dma: cannot map registers\n",
277 self->dv_xname);
278 return;
279 }
280 }
281
282 /* Establish interrupt handler */
283 esc->sc_pri = sa->sa_pri;
284
285 /* Assume SBus is grandparent */
286 esc->sc_sd.sd_reset = (void *) ncr53c9x_reset;
287 sbus_establish(&esc->sc_sd, parent);
288
289 if (sa->sa_bp != NULL && strcmp("esp", sa->sa_bp->name) == 0 &&
290 SAME_ESP(sc, sa->sa_bp, sa))
291 bootpath_store(1, sa->sa_bp + 1);
292
293 espattach(esc, &esp_sbus_glue);
294 }
295
296
297 /*
298 * Attach this instance, and then all the sub-devices
299 */
300 void
301 espattach(esc, gluep)
302 struct esp_softc *esc;
303 struct ncr53c9x_glue *gluep;
304 {
305 struct ncr53c9x_softc *sc = &esc->sc_ncr53c9x;
306 void *icookie;
307
308 /*
309 * Set up glue for MI code early; we use some of it here.
310 */
311 sc->sc_glue = gluep;
312
313 /* gimme Mhz */
314 sc->sc_freq /= 1000000;
315
316 /*
317 * XXX More of this should be in ncr53c9x_attach(), but
318 * XXX should we really poke around the chip that much in
319 * XXX the MI code? Think about this more...
320 */
321
322 /*
323 * It is necessary to try to load the 2nd config register here,
324 * to find out what rev the esp chip is, else the ncr53c9x_reset
325 * will not set up the defaults correctly.
326 */
327 sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB;
328 sc->sc_cfg2 = NCRCFG2_SCSI2 | NCRCFG2_RPE;
329 sc->sc_cfg3 = NCRCFG3_CDB;
330 NCR_WRITE_REG(sc, NCR_CFG2, sc->sc_cfg2);
331
332 if ((NCR_READ_REG(sc, NCR_CFG2) & ~NCRCFG2_RSVD) !=
333 (NCRCFG2_SCSI2 | NCRCFG2_RPE)) {
334 sc->sc_rev = NCR_VARIANT_ESP100;
335 } else {
336 sc->sc_cfg2 = NCRCFG2_SCSI2;
337 NCR_WRITE_REG(sc, NCR_CFG2, sc->sc_cfg2);
338 sc->sc_cfg3 = 0;
339 NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
340 sc->sc_cfg3 = (NCRCFG3_CDB | NCRCFG3_FCLK);
341 NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
342 if (NCR_READ_REG(sc, NCR_CFG3) !=
343 (NCRCFG3_CDB | NCRCFG3_FCLK)) {
344 sc->sc_rev = NCR_VARIANT_ESP100A;
345 } else {
346 /* NCRCFG2_FE enables > 64K transfers */
347 sc->sc_cfg2 |= NCRCFG2_FE;
348 sc->sc_cfg3 = 0;
349 NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
350 sc->sc_rev = NCR_VARIANT_ESP200;
351 }
352 }
353
354 /*
355 * XXX minsync and maxxfer _should_ be set up in MI code,
356 * XXX but it appears to have some dependency on what sort
357 * XXX of DMA we're hooked up to, etc.
358 */
359
360 /*
361 * This is the value used to start sync negotiations
362 * Note that the NCR register "SYNCTP" is programmed
363 * in "clocks per byte", and has a minimum value of 4.
364 * The SCSI period used in negotiation is one-fourth
365 * of the time (in nanoseconds) needed to transfer one byte.
366 * Since the chip's clock is given in MHz, we have the following
367 * formula: 4 * period = (1000 / freq) * 4
368 */
369 sc->sc_minsync = 1000 / sc->sc_freq;
370
371 /*
372 * Alas, we must now modify the value a bit, because it's
373 * only valid when can switch on FASTCLK and FASTSCSI bits
374 * in config register 3...
375 */
376 switch (sc->sc_rev) {
377 case NCR_VARIANT_ESP100:
378 sc->sc_maxxfer = 64 * 1024;
379 sc->sc_minsync = 0; /* No synch on old chip? */
380 break;
381
382 case NCR_VARIANT_ESP100A:
383 sc->sc_maxxfer = 64 * 1024;
384 /* Min clocks/byte is 5 */
385 sc->sc_minsync = ncr53c9x_cpb2stp(sc, 5);
386 break;
387
388 case NCR_VARIANT_ESP200:
389 sc->sc_maxxfer = 16 * 1024 * 1024;
390 /* XXX - do actually set FAST* bits */
391 break;
392 }
393
394 /* Establish interrupt channel */
395 icookie = bus_intr_establish(esc->sc_bustag,
396 esc->sc_pri, 0,
397 (int(*)__P((void*)))ncr53c9x_intr, sc);
398
399 /* register interrupt stats */
400 evcnt_attach(&sc->sc_dev, "intr", &sc->sc_intrcnt);
401
402 /* Do the common parts of attachment. */
403 sc->sc_adapter.scsipi_cmd = ncr53c9x_scsi_cmd;
404 sc->sc_adapter.scsipi_minphys = minphys;
405 ncr53c9x_attach(sc, &esp_sbus_dev);
406
407 /* Turn on target selection using the `dma' method */
408 ncr53c9x_dmaselect = 1;
409
410 bootpath_store(1, NULL);
411 }
412
413 /*
414 * Glue functions.
415 */
416
417 u_char
418 esp_read_reg(sc, reg)
419 struct ncr53c9x_softc *sc;
420 int reg;
421 {
422 struct esp_softc *esc = (struct esp_softc *)sc;
423
424 return (bus_space_read_1(esc->sc_bustag, esc->sc_reg, reg * 4));
425 }
426
427 void
428 esp_write_reg(sc, reg, v)
429 struct ncr53c9x_softc *sc;
430 int reg;
431 u_char v;
432 {
433 struct esp_softc *esc = (struct esp_softc *)sc;
434
435 bus_space_write_1(esc->sc_bustag, esc->sc_reg, reg * 4, v);
436 }
437
438 u_char
439 esp_rdreg1(sc, reg)
440 struct ncr53c9x_softc *sc;
441 int reg;
442 {
443 struct esp_softc *esc = (struct esp_softc *)sc;
444
445 return (bus_space_read_1(esc->sc_bustag, esc->sc_reg, reg));
446 }
447
448 void
449 esp_wrreg1(sc, reg, v)
450 struct ncr53c9x_softc *sc;
451 int reg;
452 u_char v;
453 {
454 struct esp_softc *esc = (struct esp_softc *)sc;
455
456 bus_space_write_1(esc->sc_bustag, esc->sc_reg, reg, v);
457 }
458
459 int
460 esp_dma_isintr(sc)
461 struct ncr53c9x_softc *sc;
462 {
463 struct esp_softc *esc = (struct esp_softc *)sc;
464
465 return (DMA_ISINTR(esc->sc_dma));
466 }
467
468 void
469 esp_dma_reset(sc)
470 struct ncr53c9x_softc *sc;
471 {
472 struct esp_softc *esc = (struct esp_softc *)sc;
473
474 DMA_RESET(esc->sc_dma);
475 }
476
477 int
478 esp_dma_intr(sc)
479 struct ncr53c9x_softc *sc;
480 {
481 struct esp_softc *esc = (struct esp_softc *)sc;
482
483 return (DMA_INTR(esc->sc_dma));
484 }
485
486 int
487 esp_dma_setup(sc, addr, len, datain, dmasize)
488 struct ncr53c9x_softc *sc;
489 caddr_t *addr;
490 size_t *len;
491 int datain;
492 size_t *dmasize;
493 {
494 struct esp_softc *esc = (struct esp_softc *)sc;
495
496 return (DMA_SETUP(esc->sc_dma, addr, len, datain, dmasize));
497 }
498
499 void
500 esp_dma_go(sc)
501 struct ncr53c9x_softc *sc;
502 {
503 struct esp_softc *esc = (struct esp_softc *)sc;
504
505 DMA_GO(esc->sc_dma);
506 }
507
508 void
509 esp_dma_stop(sc)
510 struct ncr53c9x_softc *sc;
511 {
512 struct esp_softc *esc = (struct esp_softc *)sc;
513 u_int32_t csr;
514
515 csr = L64854_GCSR(esc->sc_dma);
516 csr &= ~D_EN_DMA;
517 L64854_SCSR(esc->sc_dma, csr);
518 }
519
520 int
521 esp_dma_isactive(sc)
522 struct ncr53c9x_softc *sc;
523 {
524 struct esp_softc *esc = (struct esp_softc *)sc;
525
526 return (DMA_ISACTIVE(esc->sc_dma));
527 }
528