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