tcds.c revision 1.1.4.1 1 /* $NetBSD: tcds.c,v 1.1.4.1 2001/09/21 22:36:17 nathanw Exp $ */
2
3 /*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
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 /*
41 * Copyright (c) 1994, 1995, 1996 Carnegie-Mellon University.
42 * All rights reserved.
43 *
44 * Author: Keith Bostic, Chris G. Demetriou
45 *
46 * Permission to use, copy, modify and distribute this software and
47 * its documentation is hereby granted, provided that both the copyright
48 * notice and this permission notice appear in all copies of the
49 * software, derivative works or modified versions, and any portions
50 * thereof, and that both notices appear in supporting documentation.
51 *
52 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
53 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
54 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
55 *
56 * Carnegie Mellon requests users of this software to return to
57 *
58 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
59 * School of Computer Science
60 * Carnegie Mellon University
61 * Pittsburgh PA 15213-3890
62 *
63 * any improvements or extensions that they make and grant Carnegie the
64 * rights to redistribute these changes.
65 */
66
67 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
68
69 __KERNEL_RCSID(0, "$NetBSD: tcds.c,v 1.1.4.1 2001/09/21 22:36:17 nathanw Exp $");
70
71 #include <sys/param.h>
72 #include <sys/kernel.h>
73 #include <sys/systm.h>
74 #include <sys/device.h>
75 #include <sys/malloc.h>
76
77 #ifdef __alpha__
78 #include <machine/rpb.h>
79 #endif /* __alpha__ */
80
81 #include <dev/scsipi/scsi_all.h>
82 #include <dev/scsipi/scsipi_all.h>
83 #include <dev/scsipi/scsiconf.h>
84
85 #include <dev/ic/ncr53c9xvar.h>
86
87 #include <machine/bus.h>
88
89 #include <dev/tc/tcvar.h>
90 #include <dev/tc/tcdsreg.h>
91 #include <dev/tc/tcdsvar.h>
92
93 #include "locators.h"
94
95 struct tcds_softc {
96 struct device sc_dv;
97 bus_space_tag_t sc_bst;
98 bus_space_handle_t sc_bsh;
99 bus_dma_tag_t sc_dmat;
100 void *sc_cookie;
101 int sc_flags;
102 struct tcds_slotconfig sc_slots[2];
103 };
104
105 /* sc_flags */
106 #define TCDSF_BASEBOARD 0x01 /* baseboard on DEC 3000 */
107 #define TCDSF_FASTSCSI 0x02 /* supports Fast SCSI */
108
109 /* Definition of the driver for autoconfig. */
110 int tcdsmatch __P((struct device *, struct cfdata *, void *));
111 void tcdsattach __P((struct device *, struct device *, void *));
112 int tcdsprint __P((void *, const char *));
113 int tcdssubmatch __P((struct device *, struct cfdata *, void *));
114
115 struct cfattach tcds_ca = {
116 sizeof(struct tcds_softc), tcdsmatch, tcdsattach,
117 };
118
119 /*static*/ int tcds_intr __P((void *));
120 /*static*/ int tcds_intrnull __P((void *));
121
122 struct tcds_device {
123 const char *td_name;
124 int td_flags;
125 } tcds_devices[] = {
126 #ifdef __alpha__
127 { "PMAZ-DS ", TCDSF_BASEBOARD },
128 { "PMAZ-FS ", TCDSF_BASEBOARD|TCDSF_FASTSCSI },
129 #endif /* __alpha__ */
130 { "PMAZB-AA", 0 },
131 { "PMAZC-AA", TCDSF_FASTSCSI },
132 { NULL, 0 },
133 };
134
135 struct tcds_device *tcds_lookup __P((const char *));
136 void tcds_params __P((struct tcds_softc *, int, int *, int *));
137
138 struct tcds_device *
139 tcds_lookup(modname)
140 const char *modname;
141 {
142 struct tcds_device *td;
143
144 for (td = tcds_devices; td->td_name != NULL; td++)
145 if (strncmp(td->td_name, modname, TC_ROM_LLEN) == 0)
146 return (td);
147
148 return (NULL);
149 }
150
151 int
152 tcdsmatch(parent, cfdata, aux)
153 struct device *parent;
154 struct cfdata *cfdata;
155 void *aux;
156 {
157 struct tc_attach_args *ta = aux;
158
159 return (tcds_lookup(ta->ta_modname) != NULL);
160 }
161
162 void
163 tcdsattach(parent, self, aux)
164 struct device *parent, *self;
165 void *aux;
166 {
167 struct tcds_softc *sc = (struct tcds_softc *)self;
168 struct tc_attach_args *ta = aux;
169 struct tcdsdev_attach_args tcdsdev;
170 struct tcds_slotconfig *slotc;
171 struct tcds_device *td;
172 bus_space_handle_t sbsh[2];
173 int i, gpi2;
174 const struct evcnt *pevcnt;
175
176 td = tcds_lookup(ta->ta_modname);
177 if (td == NULL)
178 panic("\ntcdsattach: impossible");
179
180 printf(": TurboChannel Dual SCSI");
181 if (td->td_flags & TCDSF_BASEBOARD)
182 printf(" (baseboard)");
183 printf("\n");
184
185 sc->sc_flags = td->td_flags;
186
187 sc->sc_bst = ta->ta_memt;
188 sc->sc_dmat = ta->ta_dmat;
189
190 /*
191 * Map the device.
192 */
193 if (bus_space_map(sc->sc_bst, ta->ta_addr,
194 (TCDS_SCSI1_OFFSET + 0x100), 0, &sc->sc_bsh)) {
195 printf("%s: unable to map device\n", sc->sc_dv.dv_xname);
196 return;
197 }
198
199 /*
200 * Now, slice off two subregions for the individual NCR SCSI chips.
201 */
202 if (bus_space_subregion(sc->sc_bst, sc->sc_bsh, TCDS_SCSI0_OFFSET,
203 0x100, &sbsh[0]) ||
204 bus_space_subregion(sc->sc_bst, sc->sc_bsh, TCDS_SCSI1_OFFSET,
205 0x100, &sbsh[1])) {
206 printf("%s: unable to subregion SCSI chip space\n",
207 sc->sc_dv.dv_xname);
208 return;
209 }
210
211 sc->sc_cookie = ta->ta_cookie;
212
213 pevcnt = tc_intr_evcnt(parent, sc->sc_cookie);
214 tc_intr_establish(parent, sc->sc_cookie, TC_IPL_BIO, tcds_intr, sc);
215
216 /*
217 * XXX
218 * IMER apparently has some random (or, not so random, but still
219 * not useful) bits set in it when the system boots. Clear it.
220 */
221 bus_space_write_4(sc->sc_bst, sc->sc_bsh, TCDS_IMER, 0);
222
223 /* XXX Initial contents of CIR? */
224
225 /*
226 * Remember if GPI2 is set in the CIR; we'll need it later.
227 */
228 gpi2 = (bus_space_read_4(sc->sc_bst, sc->sc_bsh, TCDS_CIR) &
229 TCDS_CIR_GPI_2) != 0;
230
231 /*
232 * Set up the per-slot defintions for later use.
233 */
234
235 /* fill in common information first */
236 for (i = 0; i < 2; i++) {
237 char *cp;
238
239 slotc = &sc->sc_slots[i];
240 bzero(slotc, sizeof *slotc); /* clear everything */
241
242 cp = slotc->sc_name;
243 snprintf(cp, sizeof(slotc->sc_name), "chip %d", i);
244 evcnt_attach_dynamic(&slotc->sc_evcnt, EVCNT_TYPE_INTR,
245 pevcnt, sc->sc_dv.dv_xname, cp);
246
247 slotc->sc_slot = i;
248 slotc->sc_bst = sc->sc_bst;
249 slotc->sc_bsh = sc->sc_bsh;
250 slotc->sc_intrhand = tcds_intrnull;
251 slotc->sc_intrarg = (void *)(long)i;
252 }
253
254 /* information for slot 0 */
255 slotc = &sc->sc_slots[0];
256 slotc->sc_resetbits = TCDS_CIR_SCSI0_RESET;
257 slotc->sc_intrmaskbits =
258 TCDS_IMER_SCSI0_MASK | TCDS_IMER_SCSI0_ENB;
259 slotc->sc_intrbits = TCDS_CIR_SCSI0_INT;
260 slotc->sc_dmabits = TCDS_CIR_SCSI0_DMAENA;
261 slotc->sc_errorbits = 0; /* XXX */
262 slotc->sc_sda = TCDS_SCSI0_DMA_ADDR;
263 slotc->sc_dic = TCDS_SCSI0_DMA_INTR;
264 slotc->sc_dud0 = TCDS_SCSI0_DMA_DUD0;
265 slotc->sc_dud1 = TCDS_SCSI0_DMA_DUD1;
266
267 /* information for slot 1 */
268 slotc = &sc->sc_slots[1];
269 slotc->sc_resetbits = TCDS_CIR_SCSI1_RESET;
270 slotc->sc_intrmaskbits =
271 TCDS_IMER_SCSI1_MASK | TCDS_IMER_SCSI1_ENB;
272 slotc->sc_intrbits = TCDS_CIR_SCSI1_INT;
273 slotc->sc_dmabits = TCDS_CIR_SCSI1_DMAENA;
274 slotc->sc_errorbits = 0; /* XXX */
275 slotc->sc_sda = TCDS_SCSI1_DMA_ADDR;
276 slotc->sc_dic = TCDS_SCSI1_DMA_INTR;
277 slotc->sc_dud0 = TCDS_SCSI1_DMA_DUD0;
278 slotc->sc_dud1 = TCDS_SCSI1_DMA_DUD1;
279
280 /* find the hardware attached to the TCDS ASIC */
281 for (i = 0; i < 2; i++) {
282 tcds_params(sc, i, &tcdsdev.tcdsda_id,
283 &tcdsdev.tcdsda_fast);
284
285 tcdsdev.tcdsda_bst = sc->sc_bst;
286 tcdsdev.tcdsda_bsh = sbsh[i];
287 tcdsdev.tcdsda_dmat = sc->sc_dmat;
288 tcdsdev.tcdsda_chip = i;
289 tcdsdev.tcdsda_sc = &sc->sc_slots[i];
290 /*
291 * Determine the chip frequency. TCDSF_FASTSCSI will be set
292 * for TC option cards. For baseboard chips, GPI2 is set, for a
293 * 25MHz clock, else a 40MHz clock.
294 */
295 if ((sc->sc_flags & TCDSF_BASEBOARD && gpi2 == 0) ||
296 sc->sc_flags & TCDSF_FASTSCSI) {
297 tcdsdev.tcdsda_freq = 40000000;
298 tcdsdev.tcdsda_period = tcdsdev.tcdsda_fast ? 4 : 8;
299 } else {
300 tcdsdev.tcdsda_freq = 25000000;
301 tcdsdev.tcdsda_period = 5;
302 }
303 if (sc->sc_flags & TCDSF_BASEBOARD)
304 tcdsdev.tcdsda_variant = NCR_VARIANT_NCR53C94;
305 else
306 tcdsdev.tcdsda_variant = NCR_VARIANT_NCR53C96;
307
308 tcds_scsi_reset(tcdsdev.tcdsda_sc);
309
310 config_found_sm(self, &tcdsdev, tcdsprint, tcdssubmatch);
311 #ifdef __alpha__
312 /*
313 * The second SCSI chip isn't present on the baseboard TCDS
314 * on the DEC Alpha 3000/300 series.
315 */
316 if (sc->sc_flags & TCDSF_BASEBOARD &&
317 cputype == ST_DEC_3000_300)
318 break;
319 #endif /* __alpha__ */
320 }
321 }
322
323 int
324 tcdssubmatch(parent, cf, aux)
325 struct device *parent;
326 struct cfdata *cf;
327 void *aux;
328 {
329 struct tcdsdev_attach_args *tcdsdev = aux;
330
331 if (cf->cf_loc[TCDSCF_CHIP] != TCDSCF_CHIP_DEFAULT &&
332 cf->cf_loc[TCDSCF_CHIP] != tcdsdev->tcdsda_chip)
333 return (0);
334
335 return ((*cf->cf_attach->ca_match)(parent, cf, aux));
336 }
337
338 int
339 tcdsprint(aux, pnp)
340 void *aux;
341 const char *pnp;
342 {
343 struct tcdsdev_attach_args *tcdsdev = aux;
344
345 /* Only ASCs can attach to TCDSs; easy. */
346 if (pnp)
347 printf("asc at %s", pnp);
348
349 printf(" chip %d", tcdsdev->tcdsda_chip);
350
351 return (UNCONF);
352 }
353
354 void
355 tcds_intr_establish(tcds, slot, func, arg)
356 struct device *tcds;
357 int slot;
358 int (*func) __P((void *));
359 void *arg;
360 {
361 struct tcds_softc *sc = (struct tcds_softc *)tcds;
362
363 if (sc->sc_slots[slot].sc_intrhand != tcds_intrnull)
364 panic("tcds_intr_establish: chip %d twice", slot);
365
366 sc->sc_slots[slot].sc_intrhand = func;
367 sc->sc_slots[slot].sc_intrarg = arg;
368 tcds_scsi_reset(&sc->sc_slots[slot]);
369 }
370
371 void
372 tcds_intr_disestablish(tcds, slot)
373 struct device *tcds;
374 int slot;
375 {
376 struct tcds_softc *sc = (struct tcds_softc *)tcds;
377
378 if (sc->sc_slots[slot].sc_intrhand == tcds_intrnull)
379 panic("tcds_intr_disestablish: chip %d missing intr",
380 slot);
381
382 sc->sc_slots[slot].sc_intrhand = tcds_intrnull;
383 sc->sc_slots[slot].sc_intrarg = (void *)(u_long)slot;
384
385 tcds_dma_enable(&sc->sc_slots[slot], 0);
386 tcds_scsi_enable(&sc->sc_slots[slot], 0);
387 }
388
389 int
390 tcds_intrnull(val)
391 void *val;
392 {
393
394 panic("tcds_intrnull: uncaught TCDS intr for chip %lu\n",
395 (u_long)val);
396 }
397
398 void
399 tcds_scsi_reset(sc)
400 struct tcds_slotconfig *sc;
401 {
402 u_int32_t cir;
403
404 tcds_dma_enable(sc, 0);
405 tcds_scsi_enable(sc, 0);
406
407 cir = bus_space_read_4(sc->sc_bst, sc->sc_bsh, TCDS_CIR);
408 TCDS_CIR_CLR(cir, sc->sc_resetbits);
409 bus_space_write_4(sc->sc_bst, sc->sc_bsh, TCDS_CIR, cir);
410
411 DELAY(1);
412
413 cir = bus_space_read_4(sc->sc_bst, sc->sc_bsh, TCDS_CIR);
414 TCDS_CIR_SET(cir, sc->sc_resetbits);
415 bus_space_write_4(sc->sc_bst, sc->sc_bsh, TCDS_CIR, cir);
416
417 tcds_scsi_enable(sc, 1);
418 tcds_dma_enable(sc, 1);
419 }
420
421 void
422 tcds_scsi_enable(sc, on)
423 struct tcds_slotconfig *sc;
424 int on;
425 {
426 u_int32_t imer;
427
428 imer = bus_space_read_4(sc->sc_bst, sc->sc_bsh, TCDS_IMER);
429
430 if (on)
431 imer |= sc->sc_intrmaskbits;
432 else
433 imer &= ~sc->sc_intrmaskbits;
434
435 bus_space_write_4(sc->sc_bst, sc->sc_bsh, TCDS_IMER, imer);
436 }
437
438 void
439 tcds_dma_enable(sc, on)
440 struct tcds_slotconfig *sc;
441 int on;
442 {
443 u_int32_t cir;
444
445 cir = bus_space_read_4(sc->sc_bst, sc->sc_bsh, TCDS_CIR);
446
447 /* XXX Clear/set IOSLOT/PBS bits. */
448 if (on)
449 TCDS_CIR_SET(cir, sc->sc_dmabits);
450 else
451 TCDS_CIR_CLR(cir, sc->sc_dmabits);
452
453 bus_space_write_4(sc->sc_bst, sc->sc_bsh, TCDS_CIR, cir);
454 }
455
456 int
457 tcds_scsi_isintr(sc, clear)
458 struct tcds_slotconfig *sc;
459 int clear;
460 {
461 u_int32_t cir;
462
463 cir = bus_space_read_4(sc->sc_bst, sc->sc_bsh, TCDS_CIR);
464
465 if ((cir & sc->sc_intrbits) != 0) {
466 if (clear) {
467 TCDS_CIR_CLR(cir, sc->sc_intrbits);
468 bus_space_write_4(sc->sc_bst, sc->sc_bsh, TCDS_CIR,
469 cir);
470 }
471 return (1);
472 } else
473 return (0);
474 }
475
476 int
477 tcds_scsi_iserr(sc)
478 struct tcds_slotconfig *sc;
479 {
480 u_int32_t cir;
481
482 cir = bus_space_read_4(sc->sc_bst, sc->sc_bsh, TCDS_CIR);
483 return ((cir & sc->sc_errorbits) != 0);
484 }
485
486 int
487 tcds_intr(arg)
488 void *arg;
489 {
490 struct tcds_softc *sc = arg;
491 u_int32_t ir, ir0;
492
493 /*
494 * XXX
495 * Copy and clear (gag!) the interrupts.
496 */
497 ir = ir0 = bus_space_read_4(sc->sc_bst, sc->sc_bsh, TCDS_CIR);
498 TCDS_CIR_CLR(ir0, TCDS_CIR_ALLINTR);
499 bus_space_write_4(sc->sc_bst, sc->sc_bsh, TCDS_CIR, ir0);
500 tc_syncbus();
501
502 #define INCRINTRCNT(slot) sc->sc_slots[slot].sc_evcnt.ev_count++
503
504 #define CHECKINTR(slot) \
505 if (ir & sc->sc_slots[slot].sc_intrbits) { \
506 INCRINTRCNT(slot); \
507 (void)(*sc->sc_slots[slot].sc_intrhand) \
508 (sc->sc_slots[slot].sc_intrarg); \
509 }
510 CHECKINTR(0);
511 CHECKINTR(1);
512 #undef CHECKINTR
513
514 #ifdef DIAGNOSTIC
515 /*
516 * Interrupts not currently handled, but would like to know if they
517 * occur.
518 *
519 * XXX
520 * Don't know if we have to set the interrupt mask and enable bits
521 * in the IMER to allow some of them to happen?
522 */
523 #define PRINTINTR(msg, bits) \
524 if (ir & bits) \
525 printf("%s: %s", sc->sc_dv.dv_xname, msg);
526 PRINTINTR("SCSI0 DREQ interrupt.\n", TCDS_CIR_SCSI0_DREQ);
527 PRINTINTR("SCSI1 DREQ interrupt.\n", TCDS_CIR_SCSI1_DREQ);
528 PRINTINTR("SCSI0 prefetch interrupt.\n", TCDS_CIR_SCSI0_PREFETCH);
529 PRINTINTR("SCSI1 prefetch interrupt.\n", TCDS_CIR_SCSI1_PREFETCH);
530 PRINTINTR("SCSI0 DMA error.\n", TCDS_CIR_SCSI0_DMA);
531 PRINTINTR("SCSI1 DMA error.\n", TCDS_CIR_SCSI1_DMA);
532 PRINTINTR("SCSI0 DB parity error.\n", TCDS_CIR_SCSI0_DB);
533 PRINTINTR("SCSI1 DB parity error.\n", TCDS_CIR_SCSI1_DB);
534 PRINTINTR("SCSI0 DMA buffer parity error.\n", TCDS_CIR_SCSI0_DMAB_PAR);
535 PRINTINTR("SCSI1 DMA buffer parity error.\n", TCDS_CIR_SCSI1_DMAB_PAR);
536 PRINTINTR("SCSI0 DMA read parity error.\n", TCDS_CIR_SCSI0_DMAR_PAR);
537 PRINTINTR("SCSI1 DMA read parity error.\n", TCDS_CIR_SCSI1_DMAR_PAR);
538 PRINTINTR("TC write parity error.\n", TCDS_CIR_TCIOW_PAR);
539 PRINTINTR("TC I/O address parity error.\n", TCDS_CIR_TCIOA_PAR);
540 #undef PRINTINTR
541 #endif
542
543 /*
544 * XXX
545 * The MACH source had this, with the comment:
546 * This is wrong, but machine keeps dying.
547 */
548 DELAY(1);
549
550 return (1);
551 }
552
553 void
554 tcds_params(sc, chip, idp, fastp)
555 struct tcds_softc *sc;
556 int chip, *idp, *fastp;
557 {
558 int id, fast;
559 u_int32_t ids;
560
561 #ifdef __alpha__
562 if (sc->sc_flags & TCDSF_BASEBOARD) {
563 extern u_int8_t dec_3000_scsiid[], dec_3000_scsifast[];
564
565 id = dec_3000_scsiid[chip];
566 fast = dec_3000_scsifast[chip];
567 } else
568 #endif /* __alpha__ */
569 {
570 /*
571 * SCSI IDs are stored in the EEPROM, along with whether or
572 * not the device is "fast". Chip 0 is the high nibble,
573 * chip 1 the low nibble.
574 */
575 ids = bus_space_read_4(sc->sc_bst, sc->sc_bsh, TCDS_EEPROM_IDS);
576 if (chip == 0)
577 ids >>= 4;
578
579 id = ids & 0x7;
580 fast = ids & 0x8;
581 }
582
583 if (id < 0 || id > 7) {
584 printf("%s: WARNING: bad SCSI ID %d for chip %d, using 7\n",
585 sc->sc_dv.dv_xname, id, chip);
586 id = 7;
587 }
588
589 if (fast)
590 printf("%s: fast mode set for chip %d\n",
591 sc->sc_dv.dv_xname, chip);
592
593 *idp = id;
594 *fastp = fast;
595 }
596