tcds.c revision 1.15 1 /* $NetBSD: tcds.c,v 1.15 2005/08/25 18:35:40 drochner 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>
68 __KERNEL_RCSID(0, "$NetBSD: tcds.c,v 1.15 2005/08/25 18:35:40 drochner Exp $");
69
70 #include <sys/param.h>
71 #include <sys/kernel.h>
72 #include <sys/systm.h>
73 #include <sys/device.h>
74 #include <sys/malloc.h>
75
76 #ifdef __alpha__
77 #include <machine/rpb.h>
78 #endif /* __alpha__ */
79
80 #include <dev/scsipi/scsi_all.h>
81 #include <dev/scsipi/scsipi_all.h>
82 #include <dev/scsipi/scsiconf.h>
83
84 #include <dev/ic/ncr53c9xvar.h>
85
86 #include <machine/bus.h>
87
88 #include <dev/tc/tcvar.h>
89 #include <dev/tc/tcdsreg.h>
90 #include <dev/tc/tcdsvar.h>
91
92 #include "locators.h"
93
94 struct tcds_softc {
95 struct device sc_dv;
96 bus_space_tag_t sc_bst;
97 bus_space_handle_t sc_bsh;
98 bus_dma_tag_t sc_dmat;
99 void *sc_cookie;
100 int sc_flags;
101 struct tcds_slotconfig sc_slots[2];
102 };
103
104 /* sc_flags */
105 #define TCDSF_BASEBOARD 0x01 /* baseboard on DEC 3000 */
106 #define TCDSF_FASTSCSI 0x02 /* supports Fast SCSI */
107
108 /* Definition of the driver for autoconfig. */
109 int tcdsmatch(struct device *, struct cfdata *, void *);
110 void tcdsattach(struct device *, struct device *, void *);
111 int tcdsprint(void *, const char *);
112 int tcdssubmatch(struct device *, struct cfdata *,
113 const locdesc_t *, void *);
114
115 CFATTACH_DECL(tcds, sizeof(struct tcds_softc),
116 tcdsmatch, tcdsattach, NULL, NULL);
117
118 /*static*/ int tcds_intr(void *);
119 /*static*/ int tcds_intrnull(void *);
120
121 struct tcds_device {
122 const char *td_name;
123 int td_flags;
124 } tcds_devices[] = {
125 #ifdef __alpha__
126 { "PMAZ-DS ", TCDSF_BASEBOARD },
127 { "PMAZ-FS ", TCDSF_BASEBOARD|TCDSF_FASTSCSI },
128 #endif /* __alpha__ */
129 { "PMAZB-AA", 0 },
130 { "PMAZC-AA", TCDSF_FASTSCSI },
131 { NULL, 0 },
132 };
133
134 struct tcds_device *tcds_lookup(const char *);
135 void tcds_params(struct tcds_softc *, int, int *, int *);
136
137 struct tcds_device *
138 tcds_lookup(modname)
139 const char *modname;
140 {
141 struct tcds_device *td;
142
143 for (td = tcds_devices; td->td_name != NULL; td++)
144 if (strncmp(td->td_name, modname, TC_ROM_LLEN) == 0)
145 return (td);
146
147 return (NULL);
148 }
149
150 int
151 tcdsmatch(parent, cfdata, aux)
152 struct device *parent;
153 struct cfdata *cfdata;
154 void *aux;
155 {
156 struct tc_attach_args *ta = aux;
157
158 return (tcds_lookup(ta->ta_modname) != NULL);
159 }
160
161 void
162 tcdsattach(parent, self, aux)
163 struct device *parent, *self;
164 void *aux;
165 {
166 struct tcds_softc *sc = (struct tcds_softc *)self;
167 struct tc_attach_args *ta = aux;
168 struct tcdsdev_attach_args tcdsdev;
169 struct tcds_slotconfig *slotc;
170 struct tcds_device *td;
171 bus_space_handle_t sbsh[2];
172 int i, gpi2;
173 const struct evcnt *pevcnt;
174 int locs[TCDSCF_NLOCS];
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 definitions 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 locs[TCDSCF_CHIP] = i;
311
312 config_found_sm_loc(self, "tcds", locs, &tcdsdev,
313 tcdsprint, tcdssubmatch);
314 #ifdef __alpha__
315 /*
316 * The second SCSI chip isn't present on the baseboard TCDS
317 * on the DEC Alpha 3000/300 series.
318 */
319 if (sc->sc_flags & TCDSF_BASEBOARD &&
320 cputype == ST_DEC_3000_300)
321 break;
322 #endif /* __alpha__ */
323 }
324 }
325
326 int
327 tcdssubmatch(parent, cf, locs, aux)
328 struct device *parent;
329 struct cfdata *cf;
330 const locdesc_t *locs;
331 void *aux;
332 {
333
334 if (cf->cf_loc[TCDSCF_CHIP] != TCDSCF_CHIP_DEFAULT &&
335 cf->cf_loc[TCDSCF_CHIP] != locs[TCDSCF_CHIP])
336 return (0);
337
338 return (config_match(parent, cf, aux));
339 }
340
341 int
342 tcdsprint(aux, pnp)
343 void *aux;
344 const char *pnp;
345 {
346 struct tcdsdev_attach_args *tcdsdev = aux;
347
348 /* Only ASCs can attach to TCDSs; easy. */
349 if (pnp)
350 aprint_normal("asc at %s", pnp);
351
352 aprint_normal(" chip %d", tcdsdev->tcdsda_chip);
353
354 return (UNCONF);
355 }
356
357 void
358 tcds_intr_establish(tcds, slot, func, arg)
359 struct device *tcds;
360 int slot;
361 int (*func)(void *);
362 void *arg;
363 {
364 struct tcds_softc *sc = (struct tcds_softc *)tcds;
365
366 if (sc->sc_slots[slot].sc_intrhand != tcds_intrnull)
367 panic("tcds_intr_establish: chip %d twice", slot);
368
369 sc->sc_slots[slot].sc_intrhand = func;
370 sc->sc_slots[slot].sc_intrarg = arg;
371 tcds_scsi_reset(&sc->sc_slots[slot]);
372 }
373
374 void
375 tcds_intr_disestablish(tcds, slot)
376 struct device *tcds;
377 int slot;
378 {
379 struct tcds_softc *sc = (struct tcds_softc *)tcds;
380
381 if (sc->sc_slots[slot].sc_intrhand == tcds_intrnull)
382 panic("tcds_intr_disestablish: chip %d missing intr",
383 slot);
384
385 sc->sc_slots[slot].sc_intrhand = tcds_intrnull;
386 sc->sc_slots[slot].sc_intrarg = (void *)(u_long)slot;
387
388 tcds_dma_enable(&sc->sc_slots[slot], 0);
389 tcds_scsi_enable(&sc->sc_slots[slot], 0);
390 }
391
392 int
393 tcds_intrnull(val)
394 void *val;
395 {
396
397 panic("tcds_intrnull: uncaught TCDS intr for chip %lu",
398 (u_long)val);
399 }
400
401 void
402 tcds_scsi_reset(sc)
403 struct tcds_slotconfig *sc;
404 {
405 u_int32_t cir;
406
407 tcds_dma_enable(sc, 0);
408 tcds_scsi_enable(sc, 0);
409
410 cir = bus_space_read_4(sc->sc_bst, sc->sc_bsh, TCDS_CIR);
411 TCDS_CIR_CLR(cir, sc->sc_resetbits);
412 bus_space_write_4(sc->sc_bst, sc->sc_bsh, TCDS_CIR, cir);
413
414 DELAY(1);
415
416 cir = bus_space_read_4(sc->sc_bst, sc->sc_bsh, TCDS_CIR);
417 TCDS_CIR_SET(cir, sc->sc_resetbits);
418 bus_space_write_4(sc->sc_bst, sc->sc_bsh, TCDS_CIR, cir);
419
420 tcds_scsi_enable(sc, 1);
421 tcds_dma_enable(sc, 1);
422 }
423
424 void
425 tcds_scsi_enable(sc, on)
426 struct tcds_slotconfig *sc;
427 int on;
428 {
429 u_int32_t imer;
430
431 imer = bus_space_read_4(sc->sc_bst, sc->sc_bsh, TCDS_IMER);
432
433 if (on)
434 imer |= sc->sc_intrmaskbits;
435 else
436 imer &= ~sc->sc_intrmaskbits;
437
438 bus_space_write_4(sc->sc_bst, sc->sc_bsh, TCDS_IMER, imer);
439 }
440
441 void
442 tcds_dma_enable(sc, on)
443 struct tcds_slotconfig *sc;
444 int on;
445 {
446 u_int32_t cir;
447
448 cir = bus_space_read_4(sc->sc_bst, sc->sc_bsh, TCDS_CIR);
449
450 /* XXX Clear/set IOSLOT/PBS bits. */
451 if (on)
452 TCDS_CIR_SET(cir, sc->sc_dmabits);
453 else
454 TCDS_CIR_CLR(cir, sc->sc_dmabits);
455
456 bus_space_write_4(sc->sc_bst, sc->sc_bsh, TCDS_CIR, cir);
457 }
458
459 int
460 tcds_scsi_isintr(sc, clear)
461 struct tcds_slotconfig *sc;
462 int clear;
463 {
464 u_int32_t cir;
465
466 cir = bus_space_read_4(sc->sc_bst, sc->sc_bsh, TCDS_CIR);
467
468 if ((cir & sc->sc_intrbits) != 0) {
469 if (clear) {
470 TCDS_CIR_CLR(cir, sc->sc_intrbits);
471 bus_space_write_4(sc->sc_bst, sc->sc_bsh, TCDS_CIR,
472 cir);
473 }
474 return (1);
475 } else
476 return (0);
477 }
478
479 int
480 tcds_scsi_iserr(sc)
481 struct tcds_slotconfig *sc;
482 {
483 u_int32_t cir;
484
485 cir = bus_space_read_4(sc->sc_bst, sc->sc_bsh, TCDS_CIR);
486 return ((cir & sc->sc_errorbits) != 0);
487 }
488
489 int
490 tcds_intr(arg)
491 void *arg;
492 {
493 struct tcds_softc *sc = arg;
494 u_int32_t ir, ir0;
495
496 /*
497 * XXX
498 * Copy and clear (gag!) the interrupts.
499 */
500 ir = ir0 = bus_space_read_4(sc->sc_bst, sc->sc_bsh, TCDS_CIR);
501 TCDS_CIR_CLR(ir0, TCDS_CIR_ALLINTR);
502 bus_space_write_4(sc->sc_bst, sc->sc_bsh, TCDS_CIR, ir0);
503 tc_syncbus();
504
505 #define INCRINTRCNT(slot) sc->sc_slots[slot].sc_evcnt.ev_count++
506
507 #define CHECKINTR(slot) \
508 if (ir & sc->sc_slots[slot].sc_intrbits) { \
509 INCRINTRCNT(slot); \
510 (void)(*sc->sc_slots[slot].sc_intrhand) \
511 (sc->sc_slots[slot].sc_intrarg); \
512 }
513 CHECKINTR(0);
514 CHECKINTR(1);
515 #undef CHECKINTR
516
517 #ifdef DIAGNOSTIC
518 /*
519 * Interrupts not currently handled, but would like to know if they
520 * occur.
521 *
522 * XXX
523 * Don't know if we have to set the interrupt mask and enable bits
524 * in the IMER to allow some of them to happen?
525 */
526 #define PRINTINTR(msg, bits) \
527 if (ir & bits) \
528 printf("%s: %s", sc->sc_dv.dv_xname, msg);
529 PRINTINTR("SCSI0 DREQ interrupt.\n", TCDS_CIR_SCSI0_DREQ);
530 PRINTINTR("SCSI1 DREQ interrupt.\n", TCDS_CIR_SCSI1_DREQ);
531 PRINTINTR("SCSI0 prefetch interrupt.\n", TCDS_CIR_SCSI0_PREFETCH);
532 PRINTINTR("SCSI1 prefetch interrupt.\n", TCDS_CIR_SCSI1_PREFETCH);
533 PRINTINTR("SCSI0 DMA error.\n", TCDS_CIR_SCSI0_DMA);
534 PRINTINTR("SCSI1 DMA error.\n", TCDS_CIR_SCSI1_DMA);
535 PRINTINTR("SCSI0 DB parity error.\n", TCDS_CIR_SCSI0_DB);
536 PRINTINTR("SCSI1 DB parity error.\n", TCDS_CIR_SCSI1_DB);
537 PRINTINTR("SCSI0 DMA buffer parity error.\n", TCDS_CIR_SCSI0_DMAB_PAR);
538 PRINTINTR("SCSI1 DMA buffer parity error.\n", TCDS_CIR_SCSI1_DMAB_PAR);
539 PRINTINTR("SCSI0 DMA read parity error.\n", TCDS_CIR_SCSI0_DMAR_PAR);
540 PRINTINTR("SCSI1 DMA read parity error.\n", TCDS_CIR_SCSI1_DMAR_PAR);
541 PRINTINTR("TC write parity error.\n", TCDS_CIR_TCIOW_PAR);
542 PRINTINTR("TC I/O address parity error.\n", TCDS_CIR_TCIOA_PAR);
543 #undef PRINTINTR
544 #endif
545
546 /*
547 * XXX
548 * The MACH source had this, with the comment:
549 * This is wrong, but machine keeps dying.
550 */
551 DELAY(1);
552
553 return (1);
554 }
555
556 void
557 tcds_params(sc, chip, idp, fastp)
558 struct tcds_softc *sc;
559 int chip, *idp, *fastp;
560 {
561 int id, fast;
562 u_int32_t ids;
563
564 #ifdef __alpha__
565 if (sc->sc_flags & TCDSF_BASEBOARD) {
566 extern u_int8_t dec_3000_scsiid[], dec_3000_scsifast[];
567
568 id = dec_3000_scsiid[chip];
569 fast = dec_3000_scsifast[chip];
570 } else
571 #endif /* __alpha__ */
572 {
573 /*
574 * SCSI IDs are stored in the EEPROM, along with whether or
575 * not the device is "fast". Chip 0 is the high nibble,
576 * chip 1 the low nibble.
577 */
578 ids = bus_space_read_4(sc->sc_bst, sc->sc_bsh, TCDS_EEPROM_IDS);
579 if (chip == 0)
580 ids >>= 4;
581
582 id = ids & 0x7;
583 fast = ids & 0x8;
584 }
585
586 if (id < 0 || id > 7) {
587 printf("%s: WARNING: bad SCSI ID %d for chip %d, using 7\n",
588 sc->sc_dv.dv_xname, id, chip);
589 id = 7;
590 }
591
592 if (fast)
593 printf("%s: fast mode set for chip %d\n",
594 sc->sc_dv.dv_xname, chip);
595
596 *idp = id;
597 *fastp = fast;
598 }
599