FreeBSD/Linux Kernel Cross Reference
sys/dev/tc/tcds.c
1 /* $NetBSD: tcds.c,v 1.10 2003/01/01 00:10:25 thorpej 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@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.10 2003/01/01 00:10:25 thorpej 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 __P((struct device *, struct cfdata *, void *));
110 void tcdsattach __P((struct device *, struct device *, void *));
111 int tcdsprint __P((void *, const char *));
112 int tcdssubmatch __P((struct device *, struct cfdata *, void *));
113
114 CFATTACH_DECL(tcds, sizeof(struct tcds_softc),
115 tcdsmatch, tcdsattach, NULL, NULL);
116
117 /*static*/ int tcds_intr __P((void *));
118 /*static*/ int tcds_intrnull __P((void *));
119
120 struct tcds_device {
121 const char *td_name;
122 int td_flags;
123 } tcds_devices[] = {
124 #ifdef __alpha__
125 { "PMAZ-DS ", TCDSF_BASEBOARD },
126 { "PMAZ-FS ", TCDSF_BASEBOARD|TCDSF_FASTSCSI },
127 #endif /* __alpha__ */
128 { "PMAZB-AA", 0 },
129 { "PMAZC-AA", TCDSF_FASTSCSI },
130 { NULL, 0 },
131 };
132
133 struct tcds_device *tcds_lookup __P((const char *));
134 void tcds_params __P((struct tcds_softc *, int, int *, int *));
135
136 struct tcds_device *
137 tcds_lookup(modname)
138 const char *modname;
139 {
140 struct tcds_device *td;
141
142 for (td = tcds_devices; td->td_name != NULL; td++)
143 if (strncmp(td->td_name, modname, TC_ROM_LLEN) == 0)
144 return (td);
145
146 return (NULL);
147 }
148
149 int
150 tcdsmatch(parent, cfdata, aux)
151 struct device *parent;
152 struct cfdata *cfdata;
153 void *aux;
154 {
155 struct tc_attach_args *ta = aux;
156
157 return (tcds_lookup(ta->ta_modname) != NULL);
158 }
159
160 void
161 tcdsattach(parent, self, aux)
162 struct device *parent, *self;
163 void *aux;
164 {
165 struct tcds_softc *sc = (struct tcds_softc *)self;
166 struct tc_attach_args *ta = aux;
167 struct tcdsdev_attach_args tcdsdev;
168 struct tcds_slotconfig *slotc;
169 struct tcds_device *td;
170 bus_space_handle_t sbsh[2];
171 int i, gpi2;
172 const struct evcnt *pevcnt;
173
174 td = tcds_lookup(ta->ta_modname);
175 if (td == NULL)
176 panic("\ntcdsattach: impossible");
177
178 printf(": TurboChannel Dual SCSI");
179 if (td->td_flags & TCDSF_BASEBOARD)
180 printf(" (baseboard)");
181 printf("\n");
182
183 sc->sc_flags = td->td_flags;
184
185 sc->sc_bst = ta->ta_memt;
186 sc->sc_dmat = ta->ta_dmat;
187
188 /*
189 * Map the device.
190 */
191 if (bus_space_map(sc->sc_bst, ta->ta_addr,
192 (TCDS_SCSI1_OFFSET + 0x100), 0, &sc->sc_bsh)) {
193 printf("%s: unable to map device\n", sc->sc_dv.dv_xname);
194 return;
195 }
196
197 /*
198 * Now, slice off two subregions for the individual NCR SCSI chips.
199 */
200 if (bus_space_subregion(sc->sc_bst, sc->sc_bsh, TCDS_SCSI0_OFFSET,
201 0x100, &sbsh[0]) ||
202 bus_space_subregion(sc->sc_bst, sc->sc_bsh, TCDS_SCSI1_OFFSET,
203 0x100, &sbsh[1])) {
204 printf("%s: unable to subregion SCSI chip space\n",
205 sc->sc_dv.dv_xname);
206 return;
207 }
208
209 sc->sc_cookie = ta->ta_cookie;
210
211 pevcnt = tc_intr_evcnt(parent, sc->sc_cookie);
212 tc_intr_establish(parent, sc->sc_cookie, TC_IPL_BIO, tcds_intr, sc);
213
214 /*
215 * XXX
216 * IMER apparently has some random (or, not so random, but still
217 * not useful) bits set in it when the system boots. Clear it.
218 */
219 bus_space_write_4(sc->sc_bst, sc->sc_bsh, TCDS_IMER, 0);
220
221 /* XXX Initial contents of CIR? */
222
223 /*
224 * Remember if GPI2 is set in the CIR; we'll need it later.
225 */
226 gpi2 = (bus_space_read_4(sc->sc_bst, sc->sc_bsh, TCDS_CIR) &
227 TCDS_CIR_GPI_2) != 0;
228
229 /*
230 * Set up the per-slot definitions for later use.
231 */
232
233 /* fill in common information first */
234 for (i = 0; i < 2; i++) {
235 char *cp;
236
237 slotc = &sc->sc_slots[i];
238 bzero(slotc, sizeof *slotc); /* clear everything */
239
240 cp = slotc->sc_name;
241 snprintf(cp, sizeof(slotc->sc_name), "chip %d", i);
242 evcnt_attach_dynamic(&slotc->sc_evcnt, EVCNT_TYPE_INTR,
243 pevcnt, sc->sc_dv.dv_xname, cp);
244
245 slotc->sc_slot = i;
246 slotc->sc_bst = sc->sc_bst;
247 slotc->sc_bsh = sc->sc_bsh;
248 slotc->sc_intrhand = tcds_intrnull;
249 slotc->sc_intrarg = (void *)(long)i;
250 }
251
252 /* information for slot 0 */
253 slotc = &sc->sc_slots[0];
254 slotc->sc_resetbits = TCDS_CIR_SCSI0_RESET;
255 slotc->sc_intrmaskbits =
256 TCDS_IMER_SCSI0_MASK | TCDS_IMER_SCSI0_ENB;
257 slotc->sc_intrbits = TCDS_CIR_SCSI0_INT;
258 slotc->sc_dmabits = TCDS_CIR_SCSI0_DMAENA;
259 slotc->sc_errorbits = 0; /* XXX */
260 slotc->sc_sda = TCDS_SCSI0_DMA_ADDR;
261 slotc->sc_dic = TCDS_SCSI0_DMA_INTR;
262 slotc->sc_dud0 = TCDS_SCSI0_DMA_DUD0;
263 slotc->sc_dud1 = TCDS_SCSI0_DMA_DUD1;
264
265 /* information for slot 1 */
266 slotc = &sc->sc_slots[1];
267 slotc->sc_resetbits = TCDS_CIR_SCSI1_RESET;
268 slotc->sc_intrmaskbits =
269 TCDS_IMER_SCSI1_MASK | TCDS_IMER_SCSI1_ENB;
270 slotc->sc_intrbits = TCDS_CIR_SCSI1_INT;
271 slotc->sc_dmabits = TCDS_CIR_SCSI1_DMAENA;
272 slotc->sc_errorbits = 0; /* XXX */
273 slotc->sc_sda = TCDS_SCSI1_DMA_ADDR;
274 slotc->sc_dic = TCDS_SCSI1_DMA_INTR;
275 slotc->sc_dud0 = TCDS_SCSI1_DMA_DUD0;
276 slotc->sc_dud1 = TCDS_SCSI1_DMA_DUD1;
277
278 /* find the hardware attached to the TCDS ASIC */
279 for (i = 0; i < 2; i++) {
280 tcds_params(sc, i, &tcdsdev.tcdsda_id,
281 &tcdsdev.tcdsda_fast);
282
283 tcdsdev.tcdsda_bst = sc->sc_bst;
284 tcdsdev.tcdsda_bsh = sbsh[i];
285 tcdsdev.tcdsda_dmat = sc->sc_dmat;
286 tcdsdev.tcdsda_chip = i;
287 tcdsdev.tcdsda_sc = &sc->sc_slots[i];
288 /*
289 * Determine the chip frequency. TCDSF_FASTSCSI will be set
290 * for TC option cards. For baseboard chips, GPI2 is set, for a
291 * 25MHz clock, else a 40MHz clock.
292 */
293 if ((sc->sc_flags & TCDSF_BASEBOARD && gpi2 == 0) ||
294 sc->sc_flags & TCDSF_FASTSCSI) {
295 tcdsdev.tcdsda_freq = 40000000;
296 tcdsdev.tcdsda_period = tcdsdev.tcdsda_fast ? 4 : 8;
297 } else {
298 tcdsdev.tcdsda_freq = 25000000;
299 tcdsdev.tcdsda_period = 5;
300 }
301 if (sc->sc_flags & TCDSF_BASEBOARD)
302 tcdsdev.tcdsda_variant = NCR_VARIANT_NCR53C94;
303 else
304 tcdsdev.tcdsda_variant = NCR_VARIANT_NCR53C96;
305
306 tcds_scsi_reset(tcdsdev.tcdsda_sc);
307
308 config_found_sm(self, &tcdsdev, tcdsprint, tcdssubmatch);
309 #ifdef __alpha__
310 /*
311 * The second SCSI chip isn't present on the baseboard TCDS
312 * on the DEC Alpha 3000/300 series.
313 */
314 if (sc->sc_flags & TCDSF_BASEBOARD &&
315 cputype == ST_DEC_3000_300)
316 break;
317 #endif /* __alpha__ */
318 }
319 }
320
321 int
322 tcdssubmatch(parent, cf, aux)
323 struct device *parent;
324 struct cfdata *cf;
325 void *aux;
326 {
327 struct tcdsdev_attach_args *tcdsdev = aux;
328
329 if (cf->cf_loc[TCDSCF_CHIP] != TCDSCF_CHIP_DEFAULT &&
330 cf->cf_loc[TCDSCF_CHIP] != tcdsdev->tcdsda_chip)
331 return (0);
332
333 return (config_match(parent, cf, aux));
334 }
335
336 int
337 tcdsprint(aux, pnp)
338 void *aux;
339 const char *pnp;
340 {
341 struct tcdsdev_attach_args *tcdsdev = aux;
342
343 /* Only ASCs can attach to TCDSs; easy. */
344 if (pnp)
345 aprint_normal("asc at %s", pnp);
346
347 aprint_normal(" chip %d", tcdsdev->tcdsda_chip);
348
349 return (UNCONF);
350 }
351
352 void
353 tcds_intr_establish(tcds, slot, func, arg)
354 struct device *tcds;
355 int slot;
356 int (*func) __P((void *));
357 void *arg;
358 {
359 struct tcds_softc *sc = (struct tcds_softc *)tcds;
360
361 if (sc->sc_slots[slot].sc_intrhand != tcds_intrnull)
362 panic("tcds_intr_establish: chip %d twice", slot);
363
364 sc->sc_slots[slot].sc_intrhand = func;
365 sc->sc_slots[slot].sc_intrarg = arg;
366 tcds_scsi_reset(&sc->sc_slots[slot]);
367 }
368
369 void
370 tcds_intr_disestablish(tcds, slot)
371 struct device *tcds;
372 int slot;
373 {
374 struct tcds_softc *sc = (struct tcds_softc *)tcds;
375
376 if (sc->sc_slots[slot].sc_intrhand == tcds_intrnull)
377 panic("tcds_intr_disestablish: chip %d missing intr",
378 slot);
379
380 sc->sc_slots[slot].sc_intrhand = tcds_intrnull;
381 sc->sc_slots[slot].sc_intrarg = (void *)(u_long)slot;
382
383 tcds_dma_enable(&sc->sc_slots[slot], 0);
384 tcds_scsi_enable(&sc->sc_slots[slot], 0);
385 }
386
387 int
388 tcds_intrnull(val)
389 void *val;
390 {
391
392 panic("tcds_intrnull: uncaught TCDS intr for chip %lu",
393 (u_long)val);
394 }
395
396 void
397 tcds_scsi_reset(sc)
398 struct tcds_slotconfig *sc;
399 {
400 u_int32_t cir;
401
402 tcds_dma_enable(sc, 0);
403 tcds_scsi_enable(sc, 0);
404
405 cir = bus_space_read_4(sc->sc_bst, sc->sc_bsh, TCDS_CIR);
406 TCDS_CIR_CLR(cir, sc->sc_resetbits);
407 bus_space_write_4(sc->sc_bst, sc->sc_bsh, TCDS_CIR, cir);
408
409 DELAY(1);
410
411 cir = bus_space_read_4(sc->sc_bst, sc->sc_bsh, TCDS_CIR);
412 TCDS_CIR_SET(cir, sc->sc_resetbits);
413 bus_space_write_4(sc->sc_bst, sc->sc_bsh, TCDS_CIR, cir);
414
415 tcds_scsi_enable(sc, 1);
416 tcds_dma_enable(sc, 1);
417 }
418
419 void
420 tcds_scsi_enable(sc, on)
421 struct tcds_slotconfig *sc;
422 int on;
423 {
424 u_int32_t imer;
425
426 imer = bus_space_read_4(sc->sc_bst, sc->sc_bsh, TCDS_IMER);
427
428 if (on)
429 imer |= sc->sc_intrmaskbits;
430 else
431 imer &= ~sc->sc_intrmaskbits;
432
433 bus_space_write_4(sc->sc_bst, sc->sc_bsh, TCDS_IMER, imer);
434 }
435
436 void
437 tcds_dma_enable(sc, on)
438 struct tcds_slotconfig *sc;
439 int on;
440 {
441 u_int32_t cir;
442
443 cir = bus_space_read_4(sc->sc_bst, sc->sc_bsh, TCDS_CIR);
444
445 /* XXX Clear/set IOSLOT/PBS bits. */
446 if (on)
447 TCDS_CIR_SET(cir, sc->sc_dmabits);
448 else
449 TCDS_CIR_CLR(cir, sc->sc_dmabits);
450
451 bus_space_write_4(sc->sc_bst, sc->sc_bsh, TCDS_CIR, cir);
452 }
453
454 int
455 tcds_scsi_isintr(sc, clear)
456 struct tcds_slotconfig *sc;
457 int clear;
458 {
459 u_int32_t cir;
460
461 cir = bus_space_read_4(sc->sc_bst, sc->sc_bsh, TCDS_CIR);
462
463 if ((cir & sc->sc_intrbits) != 0) {
464 if (clear) {
465 TCDS_CIR_CLR(cir, sc->sc_intrbits);
466 bus_space_write_4(sc->sc_bst, sc->sc_bsh, TCDS_CIR,
467 cir);
468 }
469 return (1);
470 } else
471 return (0);
472 }
473
474 int
475 tcds_scsi_iserr(sc)
476 struct tcds_slotconfig *sc;
477 {
478 u_int32_t cir;
479
480 cir = bus_space_read_4(sc->sc_bst, sc->sc_bsh, TCDS_CIR);
481 return ((cir & sc->sc_errorbits) != 0);
482 }
483
484 int
485 tcds_intr(arg)
486 void *arg;
487 {
488 struct tcds_softc *sc = arg;
489 u_int32_t ir, ir0;
490
491 /*
492 * XXX
493 * Copy and clear (gag!) the interrupts.
494 */
495 ir = ir0 = bus_space_read_4(sc->sc_bst, sc->sc_bsh, TCDS_CIR);
496 TCDS_CIR_CLR(ir0, TCDS_CIR_ALLINTR);
497 bus_space_write_4(sc->sc_bst, sc->sc_bsh, TCDS_CIR, ir0);
498 tc_syncbus();
499
500 #define INCRINTRCNT(slot) sc->sc_slots[slot].sc_evcnt.ev_count++
501
502 #define CHECKINTR(slot) \
503 if (ir & sc->sc_slots[slot].sc_intrbits) { \
504 INCRINTRCNT(slot); \
505 (void)(*sc->sc_slots[slot].sc_intrhand) \
506 (sc->sc_slots[slot].sc_intrarg); \
507 }
508 CHECKINTR(0);
509 CHECKINTR(1);
510 #undef CHECKINTR
511
512 #ifdef DIAGNOSTIC
513 /*
514 * Interrupts not currently handled, but would like to know if they
515 * occur.
516 *
517 * XXX
518 * Don't know if we have to set the interrupt mask and enable bits
519 * in the IMER to allow some of them to happen?
520 */
521 #define PRINTINTR(msg, bits) \
522 if (ir & bits) \
523 printf("%s: %s", sc->sc_dv.dv_xname, msg);
524 PRINTINTR("SCSI0 DREQ interrupt.\n", TCDS_CIR_SCSI0_DREQ);
525 PRINTINTR("SCSI1 DREQ interrupt.\n", TCDS_CIR_SCSI1_DREQ);
526 PRINTINTR("SCSI0 prefetch interrupt.\n", TCDS_CIR_SCSI0_PREFETCH);
527 PRINTINTR("SCSI1 prefetch interrupt.\n", TCDS_CIR_SCSI1_PREFETCH);
528 PRINTINTR("SCSI0 DMA error.\n", TCDS_CIR_SCSI0_DMA);
529 PRINTINTR("SCSI1 DMA error.\n", TCDS_CIR_SCSI1_DMA);
530 PRINTINTR("SCSI0 DB parity error.\n", TCDS_CIR_SCSI0_DB);
531 PRINTINTR("SCSI1 DB parity error.\n", TCDS_CIR_SCSI1_DB);
532 PRINTINTR("SCSI0 DMA buffer parity error.\n", TCDS_CIR_SCSI0_DMAB_PAR);
533 PRINTINTR("SCSI1 DMA buffer parity error.\n", TCDS_CIR_SCSI1_DMAB_PAR);
534 PRINTINTR("SCSI0 DMA read parity error.\n", TCDS_CIR_SCSI0_DMAR_PAR);
535 PRINTINTR("SCSI1 DMA read parity error.\n", TCDS_CIR_SCSI1_DMAR_PAR);
536 PRINTINTR("TC write parity error.\n", TCDS_CIR_TCIOW_PAR);
537 PRINTINTR("TC I/O address parity error.\n", TCDS_CIR_TCIOA_PAR);
538 #undef PRINTINTR
539 #endif
540
541 /*
542 * XXX
543 * The MACH source had this, with the comment:
544 * This is wrong, but machine keeps dying.
545 */
546 DELAY(1);
547
548 return (1);
549 }
550
551 void
552 tcds_params(sc, chip, idp, fastp)
553 struct tcds_softc *sc;
554 int chip, *idp, *fastp;
555 {
556 int id, fast;
557 u_int32_t ids;
558
559 #ifdef __alpha__
560 if (sc->sc_flags & TCDSF_BASEBOARD) {
561 extern u_int8_t dec_3000_scsiid[], dec_3000_scsifast[];
562
563 id = dec_3000_scsiid[chip];
564 fast = dec_3000_scsifast[chip];
565 } else
566 #endif /* __alpha__ */
567 {
568 /*
569 * SCSI IDs are stored in the EEPROM, along with whether or
570 * not the device is "fast". Chip 0 is the high nibble,
571 * chip 1 the low nibble.
572 */
573 ids = bus_space_read_4(sc->sc_bst, sc->sc_bsh, TCDS_EEPROM_IDS);
574 if (chip == 0)
575 ids >>= 4;
576
577 id = ids & 0x7;
578 fast = ids & 0x8;
579 }
580
581 if (id < 0 || id > 7) {
582 printf("%s: WARNING: bad SCSI ID %d for chip %d, using 7\n",
583 sc->sc_dv.dv_xname, id, chip);
584 id = 7;
585 }
586
587 if (fast)
588 printf("%s: fast mode set for chip %d\n",
589 sc->sc_dv.dv_xname, chip);
590
591 *idp = id;
592 *fastp = fast;
593 }
Cache object: 1f6899aebfc9c98f45765782fe1a736d
|