1 /* $NetBSD: si_vme.c,v 1.29 2008/04/28 20:23:38 martin Exp $ */ 2 3 /*- 4 * Copyright (c) 1996 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Adam Glass, David Jones, and Gordon W. Ross. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * This file contains only the machine-dependent parts of the 34 * Sun3 SCSI driver. (Autoconfig stuff and DMA functions.) 35 * The machine-independent parts are in ncr5380sbc.c 36 * 37 * Supported hardware includes: 38 * Sun SCSI-3 on OBIO (Sun3/50,Sun3/60) 39 * Sun SCSI-3 on VME (Sun3/160,Sun3/260) 40 * 41 * Could be made to support the Sun3/E if someone wanted to. 42 * 43 * Note: Both supported variants of the Sun SCSI-3 adapter have 44 * some really unusual "features" for this driver to deal with, 45 * generally related to the DMA engine. The OBIO variant will 46 * ignore any attempt to write the FIFO count register while the 47 * SCSI bus is in DATA_IN or DATA_OUT phase. This is dealt with 48 * by setting the FIFO count early in COMMAND or MSG_IN phase. 49 * 50 * The VME variant has a bit to enable or disable the DMA engine, 51 * but that bit also gates the interrupt line from the NCR5380! 52 * Therefore, in order to get any interrupt from the 5380, (i.e. 53 * for reselect) one must clear the DMA engine transfer count and 54 * then enable DMA. This has the further complication that you 55 * CAN NOT touch the NCR5380 while the DMA enable bit is set, so 56 * we have to turn DMA back off before we even look at the 5380. 57 * 58 * What wonderfully whacky hardware this is! 59 * 60 * Credits, history: 61 * 62 * David Jones wrote the initial version of this module, which 63 * included support for the VME adapter only. (no reselection). 64 * 65 * Gordon Ross added support for the OBIO adapter, and re-worked 66 * both the VME and OBIO code to support disconnect/reselect. 67 * (Required figuring out the hardware "features" noted above.) 68 * 69 * The autoconfiguration boilerplate came from Adam Glass. 70 */ 71 72 /***************************************************************** 73 * VME functions for DMA 74 ****************************************************************/ 75 76 #include <sys/cdefs.h> 77 __KERNEL_RCSID(0, "$NetBSD: si_vme.c,v 1.29 2008/04/28 20:23:38 martin Exp $"); 78 79 #include <sys/param.h> 80 #include <sys/systm.h> 81 #include <sys/errno.h> 82 #include <sys/kernel.h> 83 #include <sys/malloc.h> 84 #include <sys/device.h> 85 #include <sys/buf.h> 86 #include <sys/proc.h> 87 #include <sys/user.h> 88 89 #include <dev/scsipi/scsi_all.h> 90 #include <dev/scsipi/scsipi_all.h> 91 #include <dev/scsipi/scsipi_debug.h> 92 #include <dev/scsipi/scsiconf.h> 93 94 #include <machine/autoconf.h> 95 #include <machine/dvma.h> 96 97 /* #define DEBUG XXX */ 98 99 #include <dev/ic/ncr5380reg.h> 100 #include <dev/ic/ncr5380var.h> 101 102 #include "sireg.h" 103 #include "sivar.h" 104 105 void si_vme_dma_setup(struct ncr5380_softc *); 106 void si_vme_dma_start(struct ncr5380_softc *); 107 void si_vme_dma_eop(struct ncr5380_softc *); 108 void si_vme_dma_stop(struct ncr5380_softc *); 109 110 void si_vme_intr_on (struct ncr5380_softc *); 111 void si_vme_intr_off(struct ncr5380_softc *); 112 113 static void si_vme_reset(struct ncr5380_softc *); 114 115 /* 116 * New-style autoconfig attachment 117 */ 118 119 static int si_vme_match(device_t, cfdata_t, void *); 120 static void si_vme_attach(device_t, device_t, void *); 121 122 CFATTACH_DECL_NEW(si_vme, sizeof(struct si_softc), 123 si_vme_match, si_vme_attach, NULL, NULL); 124 125 /* 126 * Options for disconnect/reselect, DMA, and interrupts. 127 * By default, allow disconnect/reselect on targets 4-6. 128 * Those are normally tapes that really need it enabled. 129 */ 130 int si_vme_options = 0x0f; 131 132 133 static int 134 si_vme_match(device_t parent, cfdata_t cf, void *aux) 135 { 136 struct confargs *ca = aux; 137 int probe_addr; 138 139 /* No default VME address. */ 140 if (ca->ca_paddr == -1) 141 return 0; 142 143 /* Make sure something is there... */ 144 probe_addr = ca->ca_paddr + 1; 145 if (bus_peek(ca->ca_bustype, probe_addr, 1) == -1) 146 return 0; 147 148 /* 149 * If this is a VME SCSI board, we have to determine whether 150 * it is an "sc" (Sun2) or "si" (Sun3) SCSI board. This can 151 * be determined using the fact that the "sc" board occupies 152 * 4K bytes in VME space but the "si" board occupies 2K bytes. 153 */ 154 /* Note: the "si" board should NOT respond here. */ 155 probe_addr = ca->ca_paddr + 0x801; 156 if (bus_peek(ca->ca_bustype, probe_addr, 1) != -1) { 157 /* Something responded at 2K+1. Maybe an "sc" board? */ 158 #ifdef DEBUG 159 printf("%s: May be an `sc' board at pa=0x%lx\n", 160 __func__, ca->ca_paddr); 161 #endif 162 return 0; 163 } 164 165 /* Default interrupt priority. */ 166 if (ca->ca_intpri == -1) 167 ca->ca_intpri = 2; 168 169 return 1; 170 } 171 172 static void 173 si_vme_attach(device_t parent, device_t self, void *args) 174 { 175 struct si_softc *sc = device_private(self); 176 struct ncr5380_softc *ncr_sc = &sc->ncr_sc; 177 struct cfdata *cf = device_cfdata(self); 178 struct confargs *ca = args; 179 180 ncr_sc->sc_dev = self; 181 sc->sc_bst = ca->ca_bustag; 182 sc->sc_dmat = ca->ca_dmatag; 183 184 if (bus_space_map(sc->sc_bst, ca->ca_paddr, sizeof(struct si_regs), 0, 185 &sc->sc_bsh) != 0) { 186 aprint_error(": can't map register\n"); 187 return; 188 } 189 sc->sc_regs = bus_space_vaddr(sc->sc_bst, sc->sc_bsh); 190 191 if (bus_dmamap_create(sc->sc_dmat, MAXPHYS, 1, MAXPHYS, 0, 192 BUS_DMA_NOWAIT, &sc->sc_dmap) != 0) { 193 aprint_error(": can't create DMA map\n"); 194 return; 195 } 196 197 /* Get options from config flags if specified. */ 198 if (cf->cf_flags) 199 sc->sc_options = cf->cf_flags; 200 else 201 sc->sc_options = si_vme_options; 202 203 aprint_normal(": options=0x%x\n", sc->sc_options); 204 205 sc->sc_adapter_type = ca->ca_bustype; 206 sc->sc_adapter_iv_am = VME_SUPV_DATA_24 | (ca->ca_intvec & 0xFF); 207 208 /* 209 * MD function pointers used by the MI code. 210 */ 211 ncr_sc->sc_pio_out = ncr5380_pio_out; 212 ncr_sc->sc_pio_in = ncr5380_pio_in; 213 ncr_sc->sc_dma_alloc = si_dma_alloc; 214 ncr_sc->sc_dma_free = si_dma_free; 215 ncr_sc->sc_dma_setup = si_vme_dma_setup; 216 ncr_sc->sc_dma_start = si_vme_dma_start; 217 ncr_sc->sc_dma_poll = si_dma_poll; 218 ncr_sc->sc_dma_eop = si_vme_dma_eop; 219 ncr_sc->sc_dma_stop = si_vme_dma_stop; 220 ncr_sc->sc_intr_on = si_vme_intr_on; 221 ncr_sc->sc_intr_off = si_vme_intr_off; 222 223 /* Attach interrupt handler. */ 224 isr_add_vectored(si_intr, (void *)sc, ca->ca_intpri, ca->ca_intvec); 225 226 /* Reset the hardware. */ 227 si_vme_reset(ncr_sc); 228 229 /* Do the common attach stuff. */ 230 si_attach(sc); 231 } 232 233 static void 234 si_vme_reset(struct ncr5380_softc *ncr_sc) 235 { 236 struct si_softc *sc = (struct si_softc *)ncr_sc; 237 volatile struct si_regs *si = sc->sc_regs; 238 239 #ifdef DEBUG 240 if (si_debug) { 241 printf("%s\n", __func__); 242 } 243 #endif 244 245 /* 246 * The SCSI3 controller has an 8K FIFO to buffer data between the 247 * 5380 and the DMA. Make sure it starts out empty. 248 * 249 * The reset bits in the CSR are active low. 250 */ 251 si->si_csr = 0; 252 delay(10); 253 si->si_csr = SI_CSR_FIFO_RES | SI_CSR_SCSI_RES | SI_CSR_INTR_EN; 254 delay(10); 255 si->fifo_count = 0; 256 257 /* Make sure the DMA engine is stopped. */ 258 si->dma_addrh = 0; 259 si->dma_addrl = 0; 260 si->dma_counth = 0; 261 si->dma_countl = 0; 262 si->si_iv_am = sc->sc_adapter_iv_am; 263 si->fifo_cnt_hi = 0; 264 } 265 266 /* 267 * This is called when the bus is going idle, 268 * so we want to enable the SBC interrupts. 269 * That is controlled by the DMA enable! 270 * Who would have guessed! 271 * What a NASTY trick! 272 */ 273 void 274 si_vme_intr_on(struct ncr5380_softc *ncr_sc) 275 { 276 struct si_softc *sc = (struct si_softc *)ncr_sc; 277 volatile struct si_regs *si = sc->sc_regs; 278 279 /* receive mode should be safer */ 280 si->si_csr &= ~SI_CSR_SEND; 281 282 /* Clear the count so nothing happens. */ 283 si->dma_counth = 0; 284 si->dma_countl = 0; 285 286 /* Clear the start address too. (paranoid?) */ 287 si->dma_addrh = 0; 288 si->dma_addrl = 0; 289 290 /* Finally, enable the DMA engine. */ 291 si->si_csr |= SI_CSR_DMA_EN; 292 } 293 294 /* 295 * This is called when the bus is idle and we are 296 * about to start playing with the SBC chip. 297 */ 298 void 299 si_vme_intr_off(struct ncr5380_softc *ncr_sc) 300 { 301 struct si_softc *sc = (struct si_softc *)ncr_sc; 302 volatile struct si_regs *si = sc->sc_regs; 303 304 si->si_csr &= ~SI_CSR_DMA_EN; 305 } 306 307 /* 308 * This function is called during the COMMAND or MSG_IN phase 309 * that precedes a DATA_IN or DATA_OUT phase, in case we need 310 * to setup the DMA engine before the bus enters a DATA phase. 311 * 312 * XXX: The VME adapter appears to suppress SBC interrupts 313 * when the FIFO is not empty or the FIFO count is non-zero! 314 * 315 * On the VME version, setup the start addres, but clear the 316 * count (to make sure it stays idle) and set that later. 317 */ 318 void 319 si_vme_dma_setup(struct ncr5380_softc *ncr_sc) 320 { 321 struct si_softc *sc = (struct si_softc *)ncr_sc; 322 struct sci_req *sr = ncr_sc->sc_current; 323 struct si_dma_handle *dh = sr->sr_dma_hand; 324 volatile struct si_regs *si = sc->sc_regs; 325 long data_pa; 326 int xlen; 327 328 /* 329 * Get the DVMA mapping for this segment. 330 * XXX - Should separate allocation and mapin. 331 */ 332 data_pa = dh->dh_dmaaddr; 333 if (data_pa & 1) 334 panic("%s: bad pa=0x%lx", __func__, data_pa); 335 xlen = dh->dh_dmalen; 336 xlen &= ~1; /* XXX: necessary? */ 337 sc->sc_reqlen = xlen; /* XXX: or less? */ 338 339 #ifdef DEBUG 340 if (si_debug & 2) { 341 printf("%s: dh=%p, pa=0x%lx, xlen=0x%x\n", 342 __func__, dh, data_pa, xlen); 343 } 344 #endif 345 346 /* Set direction (send/recv) */ 347 if (dh->dh_flags & SIDH_OUT) { 348 si->si_csr |= SI_CSR_SEND; 349 } else { 350 si->si_csr &= ~SI_CSR_SEND; 351 } 352 353 /* Reset the FIFO. */ 354 si->si_csr &= ~SI_CSR_FIFO_RES; /* active low */ 355 si->si_csr |= SI_CSR_FIFO_RES; 356 357 if (data_pa & 2) { 358 si->si_csr |= SI_CSR_BPCON; 359 } else { 360 si->si_csr &= ~SI_CSR_BPCON; 361 } 362 363 /* Load the start address. */ 364 si->dma_addrh = (uint16_t)(data_pa >> 16); 365 si->dma_addrl = (uint16_t)(data_pa & 0xFFFF); 366 367 /* 368 * Keep the count zero or it may start early! 369 */ 370 si->dma_counth = 0; 371 si->dma_countl = 0; 372 373 #if 0 374 /* Clear FIFO counter. (also hits dma_count) */ 375 si->fifo_cnt_hi = 0; 376 si->fifo_count = 0; 377 #endif 378 } 379 380 381 void 382 si_vme_dma_start(struct ncr5380_softc *ncr_sc) 383 { 384 struct si_softc *sc = (struct si_softc *)ncr_sc; 385 struct sci_req *sr = ncr_sc->sc_current; 386 struct si_dma_handle *dh = sr->sr_dma_hand; 387 volatile struct si_regs *si = sc->sc_regs; 388 int s, xlen; 389 390 xlen = sc->sc_reqlen; 391 392 /* This MAY be time critical (not sure). */ 393 s = splhigh(); 394 395 si->dma_counth = (uint16_t)(xlen >> 16); 396 si->dma_countl = (uint16_t)(xlen & 0xFFFF); 397 398 /* Set it anyway, even though dma_count hits it. */ 399 si->fifo_cnt_hi = (uint16_t)(xlen >> 16); 400 si->fifo_count = (uint16_t)(xlen & 0xFFFF); 401 402 /* 403 * Acknowledge the phase change. (After DMA setup!) 404 * Put the SBIC into DMA mode, and start the transfer. 405 */ 406 if (dh->dh_flags & SIDH_OUT) { 407 *ncr_sc->sci_tcmd = PHASE_DATA_OUT; 408 SCI_CLR_INTR(ncr_sc); 409 *ncr_sc->sci_icmd = SCI_ICMD_DATA; 410 *ncr_sc->sci_mode |= (SCI_MODE_DMA | SCI_MODE_DMA_IE); 411 *ncr_sc->sci_dma_send = 0; /* start it */ 412 } else { 413 *ncr_sc->sci_tcmd = PHASE_DATA_IN; 414 SCI_CLR_INTR(ncr_sc); 415 *ncr_sc->sci_icmd = 0; 416 *ncr_sc->sci_mode |= (SCI_MODE_DMA | SCI_MODE_DMA_IE); 417 *ncr_sc->sci_irecv = 0; /* start it */ 418 } 419 420 /* Let'er rip! */ 421 si->si_csr |= SI_CSR_DMA_EN; 422 423 splx(s); 424 ncr_sc->sc_state |= NCR_DOINGDMA; 425 426 #ifdef DEBUG 427 if (si_debug & 2) { 428 printf("%s: started, flags=0x%x\n", 429 __func__, ncr_sc->sc_state); 430 } 431 #endif 432 } 433 434 435 void 436 si_vme_dma_eop(struct ncr5380_softc *ncr_sc) 437 { 438 439 /* Not needed - DMA was stopped prior to examining sci_csr */ 440 } 441 442 443 void 444 si_vme_dma_stop(struct ncr5380_softc *ncr_sc) 445 { 446 struct si_softc *sc = (struct si_softc *)ncr_sc; 447 struct sci_req *sr = ncr_sc->sc_current; 448 struct si_dma_handle *dh = sr->sr_dma_hand; 449 volatile struct si_regs *si = sc->sc_regs; 450 int resid, ntrans; 451 452 if ((ncr_sc->sc_state & NCR_DOINGDMA) == 0) { 453 #ifdef DEBUG 454 printf("%s: DMA not running\n", __func__); 455 #endif 456 return; 457 } 458 ncr_sc->sc_state &= ~NCR_DOINGDMA; 459 460 /* First, halt the DMA engine. */ 461 si->si_csr &= ~SI_CSR_DMA_EN; /* VME only */ 462 463 /* Set an impossible phase to prevent data movement? */ 464 *ncr_sc->sci_tcmd = PHASE_INVALID; 465 466 if (si->si_csr & (SI_CSR_DMA_CONFLICT | SI_CSR_DMA_BUS_ERR)) { 467 printf("si: DMA error, csr=0x%x, reset\n", si->si_csr); 468 sr->sr_xs->error = XS_DRIVER_STUFFUP; 469 ncr_sc->sc_state |= NCR_ABORTING; 470 si_vme_reset(ncr_sc); 471 goto out; 472 } 473 474 /* Note that timeout may have set the error flag. */ 475 if (ncr_sc->sc_state & NCR_ABORTING) 476 goto out; 477 478 /* XXX: Wait for DMA to actually finish? */ 479 480 /* 481 * Now try to figure out how much actually transferred 482 * 483 * The fifo_count does not reflect how many bytes were 484 * actually transferred for VME. 485 * 486 * SCSI-3 VME interface is a little funny on writes: 487 * if we have a disconnect, the DMA has overshot by 488 * one byte and the resid needs to be incremented. 489 * Only happens for partial transfers. 490 * (Thanks to Matt Jacob) 491 */ 492 493 resid = si->fifo_count & 0xFFFF; 494 if (dh->dh_flags & SIDH_OUT) 495 if ((resid > 0) && (resid < sc->sc_reqlen)) 496 resid++; 497 ntrans = sc->sc_reqlen - resid; 498 499 #ifdef DEBUG 500 if (si_debug & 2) { 501 printf("%s: resid=0x%x ntrans=0x%x\n", 502 __func__, resid, ntrans); 503 } 504 #endif 505 506 if (ntrans < MIN_DMA_LEN) { 507 printf("si: fifo count: 0x%x\n", resid); 508 ncr_sc->sc_state |= NCR_ABORTING; 509 goto out; 510 } 511 if (ntrans > ncr_sc->sc_datalen) 512 panic("%s: excess transfer", __func__); 513 514 /* Adjust data pointer */ 515 ncr_sc->sc_dataptr += ntrans; 516 ncr_sc->sc_datalen -= ntrans; 517 518 /* 519 * After a read, we may need to clean-up 520 * "Left-over bytes" (yuck!) 521 */ 522 if (((dh->dh_flags & SIDH_OUT) == 0) && 523 ((si->si_csr & SI_CSR_LOB) != 0)) { 524 uint8_t *cp = ncr_sc->sc_dataptr; 525 #ifdef DEBUG 526 printf("si: Got Left-over bytes!\n"); 527 #endif 528 if (si->si_csr & SI_CSR_BPCON) { 529 /* have SI_CSR_BPCON */ 530 cp[-1] = (si->si_bprl & 0xff00) >> 8; 531 } else { 532 switch (si->si_csr & SI_CSR_LOB) { 533 case SI_CSR_LOB_THREE: 534 cp[-3] = (si->si_bprh & 0xff00) >> 8; 535 cp[-2] = (si->si_bprh & 0x00ff); 536 cp[-1] = (si->si_bprl & 0xff00) >> 8; 537 break; 538 case SI_CSR_LOB_TWO: 539 cp[-2] = (si->si_bprh & 0xff00) >> 8; 540 cp[-1] = (si->si_bprh & 0x00ff); 541 break; 542 case SI_CSR_LOB_ONE: 543 cp[-1] = (si->si_bprh & 0xff00) >> 8; 544 break; 545 } 546 } 547 } 548 549 out: 550 si->dma_addrh = 0; 551 si->dma_addrl = 0; 552 553 si->dma_counth = 0; 554 si->dma_countl = 0; 555 556 si->fifo_cnt_hi = 0; 557 si->fifo_count = 0; 558 559 /* Put SBIC back in PIO mode. */ 560 *ncr_sc->sci_mode &= ~(SCI_MODE_DMA | SCI_MODE_DMA_IE); 561 *ncr_sc->sci_icmd = 0; 562 } 563