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