1 /* $NetBSD: atari5380.c,v 1.31 2000/03/29 14:19:22 leo Exp $ */ 2 3 /* 4 * Copyright (c) 1995 Leo Weppelman. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Leo Weppelman. 18 * 4. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/kernel.h> 36 #include <sys/device.h> 37 #include <sys/buf.h> 38 #include <dev/scsipi/scsi_all.h> 39 #include <dev/scsipi/scsipi_all.h> 40 #include <dev/scsipi/scsi_message.h> 41 #include <dev/scsipi/scsiconf.h> 42 43 #include <m68k/asm_single.h> 44 #include <m68k/cpu.h> 45 #include <m68k/cacheops.h> 46 47 #include <atari/atari/stalloc.h> 48 49 /* 50 * Include the driver definitions 51 */ 52 #include <atari/dev/ncr5380reg.h> 53 54 #include <machine/stdarg.h> 55 #include <machine/iomap.h> 56 #include <machine/mfp.h> 57 58 #include <atari/atari/intr.h> 59 60 #if defined(FALCON_SCSI) 61 #include <machine/dma.h> 62 #endif 63 64 /* 65 * Set the various driver options 66 */ 67 #define NREQ 18 /* Size of issue queue */ 68 #define AUTO_SENSE 1 /* Automatically issue a request-sense */ 69 70 #define DRNAME ncrscsi /* used in various prints */ 71 #undef DBG_SEL /* Show the selection process */ 72 #undef DBG_REQ /* Show enqueued/ready requests */ 73 #undef DBG_ERR_RET /* Show requests with != 0 return code */ 74 #undef DBG_NOWRITE /* Do not allow writes to the targets */ 75 #undef DBG_PIO /* Show the polled-I/O process */ 76 #undef DBG_INF /* Show information transfer process */ 77 #define DBG_NOSTATIC /* No static functions, all in DDB trace*/ 78 #define DBG_PID 15 /* Keep track of driver */ 79 #define REAL_DMA /* Use DMA if sensible */ 80 #if defined(FALCON_SCSI) 81 #define REAL_DMA_POLL 1 /* 1: Poll for end of DMA-transfer */ 82 #else 83 #define REAL_DMA_POLL 0 /* 1: Poll for end of DMA-transfer */ 84 #endif 85 #undef USE_PDMA /* Use special pdma-transfer function */ 86 #define MIN_PHYS 65536 /*BARF!!!!*/ 87 88 /* 89 * Include more driver definitions 90 */ 91 #include <atari/dev/ncr5380var.h> 92 93 /* 94 * The atari specific driver options 95 */ 96 #undef NO_TTRAM_DMA /* Do not use DMA to TT-ram. This */ 97 /* fails on older atari's */ 98 #define ENABLE_NCR5380(sc) cur_softc = sc; 99 100 static u_char *alloc_bounceb __P((u_long)); 101 static void free_bounceb __P((u_char *)); 102 static int machine_match __P((struct device *, void *, void *, 103 struct cfdriver *)); 104 void scsi_ctrl __P((int)); 105 void scsi_dma __P((int)); 106 107 /* 108 * Functions that do nothing on the atari 109 */ 110 #define pdma_ready() 0 111 112 #if defined(TT_SCSI) 113 114 void ncr5380_drq_intr __P((int)); 115 116 /* 117 * Define all the things we need of the DMA-controller 118 */ 119 #define SCSI_DMA ((struct scsi_dma *)AD_SCSI_DMA) 120 #define SCSI_5380 ((struct scsi_5380 *)AD_NCR5380) 121 122 struct scsi_dma { 123 volatile u_char s_dma_ptr[8]; /* use only the odd bytes */ 124 volatile u_char s_dma_cnt[8]; /* use only the odd bytes */ 125 volatile u_char s_dma_res[4]; /* data residue register */ 126 volatile u_char s_dma_gap; /* not used */ 127 volatile u_char s_dma_ctrl; /* control register */ 128 volatile u_char s_dma_gap2; /* not used */ 129 volatile u_char s_hdma_ctrl; /* Hades control register */ 130 }; 131 132 #define set_scsi_dma(addr, val) (void)( \ 133 { \ 134 u_char *address = (u_char*)addr+1; \ 135 u_long nval = (u_long)val; \ 136 __asm("movepl %0, %1@(0)": :"d" (nval), "a" (address)); \ 137 }) 138 139 #define get_scsi_dma(addr, res) ( \ 140 { \ 141 u_char *address = (u_char*)addr+1; \ 142 u_long nval; \ 143 __asm("movepl %1@(0), %0": "=d" (nval) : "a" (address)); \ 144 res = (u_long)nval; \ 145 }) 146 147 /* 148 * Defines for TT-DMA control register 149 */ 150 #define SD_BUSERR 0x80 /* 1 = transfer caused bus error*/ 151 #define SD_ZERO 0x40 /* 1 = byte counter is zero */ 152 #define SD_ENABLE 0x02 /* 1 = Enable DMA */ 153 #define SD_OUT 0x01 /* Direction: memory to SCSI */ 154 #define SD_IN 0x00 /* Direction: SCSI to memory */ 155 156 /* 157 * Defines for Hades-DMA control register 158 */ 159 #define SDH_BUSERR 0x02 /* 1 = Bus error */ 160 #define SDH_EOP 0x01 /* 1 = Signal EOP on 5380 */ 161 #define SDH_ZERO 0x40 /* 1 = Byte counter is zero */ 162 163 /* 164 * Define the 5380 register set 165 */ 166 struct scsi_5380 { 167 volatile u_char scsi_5380[16]; /* use only the odd bytes */ 168 }; 169 #endif /* TT_SCSI */ 170 171 /********************************************** 172 * Variables present for both TT and Falcon. * 173 **********************************************/ 174 175 /* 176 * Softc of currently active controller (a bit of fake; we only have one) 177 */ 178 static struct ncr_softc *cur_softc; 179 180 #if defined(TT_SCSI) && !defined(FALCON_SCSI) 181 /* 182 * We can be more efficient for some functions when only TT_SCSI is selected 183 */ 184 #define GET_5380_REG(rnum) SCSI_5380->scsi_5380[(rnum << 1) | 1] 185 #define SET_5380_REG(rnum,val) (SCSI_5380->scsi_5380[(rnum << 1) | 1] = val) 186 187 #define scsi_mach_init(sc) scsi_tt_init(sc) 188 #define scsi_ienable() scsi_tt_ienable() 189 #define scsi_idisable() scsi_tt_idisable() 190 #define scsi_clr_ipend() scsi_tt_clr_ipend() 191 #define scsi_ipending() (GET_5380_REG(NCR5380_DMSTAT) & SC_IRQ_SET) 192 #define scsi_dma_setup(r,p,m) scsi_tt_dmasetup(r, p, m) 193 #define wrong_dma_range(r,d) tt_wrong_dma_range(r, d) 194 #define poll_edma(reqp) tt_poll_edma(reqp) 195 #define get_dma_result(r, b) tt_get_dma_result(r, b) 196 #define can_access_5380() 1 197 #define emulated_dma() ((machineid & ATARI_HADES) ? 1 : 0) 198 199 #define fair_to_keep_dma() 1 200 #define claimed_dma() 1 201 #define reconsider_dma() 202 203 #endif /* defined(TT_SCSI) && !defined(FALCON_SCSI) */ 204 205 #if defined(TT_SCSI) 206 207 /* 208 * Prototype functions defined below 209 */ 210 #ifdef NO_TTRAM_DMA 211 static int tt_wrong_dma_range __P((SC_REQ *, struct dma_chain *)); 212 #endif 213 static void scsi_tt_init __P((struct ncr_softc *)); 214 static u_char get_tt_5380_reg __P((u_short)); 215 static void set_tt_5380_reg __P((u_short, u_short)); 216 static void scsi_tt_dmasetup __P((SC_REQ *, u_int, u_char)); 217 static int tt_poll_edma __P((SC_REQ *)); 218 static u_char *ptov __P((SC_REQ *, u_long*)); 219 static int tt_get_dma_result __P((SC_REQ *, u_long *)); 220 void scsi_tt_ienable __P((void)); 221 void scsi_tt_idisable __P((void)); 222 void scsi_tt_clr_ipend __P((void)); 223 224 /* 225 * Define these too, so we can use them locally... 226 */ 227 #define GET_TT_REG(rnum) SCSI_5380->scsi_5380[(rnum << 1) | 1] 228 #define SET_TT_REG(rnum,val) (SCSI_5380->scsi_5380[(rnum << 1) | 1] = val) 229 230 #ifdef NO_TTRAM_DMA 231 static int 232 tt_wrong_dma_range(reqp, dm) 233 SC_REQ *reqp; 234 struct dma_chain *dm; 235 { 236 if (dm->dm_addr & 0xff000000) { 237 reqp->dr_flag |= DRIVER_BOUNCING; 238 return(1); 239 } 240 return(0); 241 } 242 #else 243 #define tt_wrong_dma_range(reqp, dm) 0 244 #endif 245 246 static void 247 scsi_tt_init(struct ncr_softc *sc) 248 { 249 /* 250 * Enable SCSI-related interrupts 251 */ 252 MFP2->mf_aer |= 0x80; /* SCSI IRQ goes HIGH!!!!! */ 253 254 if (machineid & ATARI_TT) { 255 /* SCSI-dma interrupts */ 256 MFP2->mf_ierb |= IB_SCDM; 257 MFP2->mf_iprb = (u_int8_t)~IB_SCDM; 258 MFP2->mf_imrb |= IB_SCDM; 259 } 260 else if (machineid & ATARI_HADES) { 261 SCSI_DMA->s_hdma_ctrl = 0; 262 263 if (intr_establish(2, AUTO_VEC, 0, 264 (hw_ifun_t)ncr5380_drq_intr, 265 NULL) == NULL) 266 panic("scsi_tt_init: Can't establish drq-interrupt"); 267 } 268 else panic("scsi_tt_init: should not come here"); 269 270 MFP2->mf_iera |= IA_SCSI; /* SCSI-5380 interrupts */ 271 MFP2->mf_ipra = (u_int8_t)~IA_SCSI; 272 MFP2->mf_imra |= IA_SCSI; 273 274 /* 275 * LWP: DMA transfers to TT-ram causes data to be garbeled 276 * without notice on some revisons of the TT-mainboard. 277 * When program's generate misterious Segmentations faults, 278 * try turning on NO_TTRAM_DMA. 279 */ 280 #ifdef NO_TTRAM_DMA 281 printf(": DMA to TT-RAM is disabled!"); 282 #endif 283 } 284 285 static u_char 286 get_tt_5380_reg(u_short rnum) 287 { 288 return(SCSI_5380->scsi_5380[(rnum << 1) | 1]); 289 } 290 291 static void 292 set_tt_5380_reg(u_short rnum, u_short val) 293 { 294 SCSI_5380->scsi_5380[(rnum << 1) | 1] = val; 295 } 296 297 extern __inline__ void 298 scsi_tt_ienable(void) 299 { 300 if (machineid & ATARI_TT) 301 single_inst_bset_b(MFP2->mf_imrb, IB_SCDM); 302 single_inst_bset_b(MFP2->mf_imra, IA_SCSI); 303 } 304 305 extern __inline__ void 306 scsi_tt_idisable(void) 307 { 308 if (machineid & ATARI_TT) 309 single_inst_bclr_b(MFP2->mf_imrb, IB_SCDM); 310 single_inst_bclr_b(MFP2->mf_imra, IA_SCSI); 311 } 312 313 extern __inline__ void 314 scsi_tt_clr_ipend(void) 315 { 316 int tmp; 317 318 SCSI_DMA->s_dma_ctrl = 0; 319 tmp = GET_TT_REG(NCR5380_IRCV); 320 if (machineid & ATARI_TT) 321 MFP2->mf_iprb = (u_int8_t)~IB_SCDM; 322 MFP2->mf_ipra = (u_int8_t)~IA_SCSI; 323 324 /* 325 * Remove interrupts already scheduled. 326 */ 327 rem_sicallback((si_farg)ncr_ctrl_intr); 328 rem_sicallback((si_farg)ncr_dma_intr); 329 } 330 331 static void 332 scsi_tt_dmasetup(SC_REQ *reqp, u_int phase, u_char mode) 333 { 334 if (PH_IN(phase)) { 335 SCSI_DMA->s_dma_ctrl = SD_IN; 336 if (machineid & ATARI_HADES) 337 SCSI_DMA->s_hdma_ctrl &= ~(SDH_BUSERR|SDH_EOP); 338 set_scsi_dma(&(SCSI_DMA->s_dma_ptr), reqp->dm_cur->dm_addr); 339 set_scsi_dma(&(SCSI_DMA->s_dma_cnt), reqp->dm_cur->dm_count); 340 SET_TT_REG(NCR5380_ICOM, 0); 341 SET_TT_REG(NCR5380_MODE, mode); 342 SCSI_DMA->s_dma_ctrl = SD_ENABLE; 343 SET_TT_REG(NCR5380_IRCV, 0); 344 } 345 else { 346 SCSI_DMA->s_dma_ctrl = SD_OUT; 347 if (machineid & ATARI_HADES) 348 SCSI_DMA->s_hdma_ctrl &= ~(SDH_BUSERR|SDH_EOP); 349 set_scsi_dma(&(SCSI_DMA->s_dma_ptr), reqp->dm_cur->dm_addr); 350 set_scsi_dma(&(SCSI_DMA->s_dma_cnt), reqp->dm_cur->dm_count); 351 SET_TT_REG(NCR5380_MODE, mode); 352 SET_TT_REG(NCR5380_ICOM, SC_ADTB); 353 SET_TT_REG(NCR5380_DMSTAT, 0); 354 SCSI_DMA->s_dma_ctrl = SD_ENABLE|SD_OUT; 355 } 356 } 357 358 static int 359 tt_poll_edma(SC_REQ *reqp) 360 { 361 u_char dmstat, dmastat; 362 int timeout = 9000; /* XXX */ 363 364 /* 365 * We wait here until the DMA has finished. This can be 366 * achieved by checking the following conditions: 367 * - 5380: 368 * - End of DMA flag is set 369 * - We lost BSY (error!!) 370 * - A phase mismatch has occured (partial transfer) 371 * - DMA-controller: 372 * - A bus error occurred (Kernel error!!) 373 * - All bytes are transferred 374 * If one of the terminating conditions was met, we call 375 * 'dma_ready' to check errors and perform the bookkeeping. 376 */ 377 378 scsi_tt_idisable(); 379 for (;;) { 380 delay(20); 381 if (--timeout <= 0) { 382 ncr_tprint(reqp, "timeout on polled transfer\n"); 383 reqp->xs->error = XS_TIMEOUT; 384 scsi_tt_ienable(); 385 return(0); 386 } 387 dmstat = GET_TT_REG(NCR5380_DMSTAT); 388 389 if ((machineid & ATARI_HADES) && (dmstat & SC_DMA_REQ)) { 390 ncr5380_drq_intr(1); 391 dmstat = GET_TT_REG(NCR5380_DMSTAT); 392 } 393 394 dmastat = SCSI_DMA->s_dma_ctrl; 395 if (dmstat & (SC_END_DMA|SC_BSY_ERR|SC_IRQ_SET)) 396 break; 397 if (!(dmstat & SC_PHS_MTCH)) 398 break; 399 if (dmastat & (SD_BUSERR|SD_ZERO)) 400 break; 401 } 402 scsi_tt_ienable(); 403 return(1); 404 } 405 406 /* 407 * Convert physical DMA address to a virtual address. 408 */ 409 static u_char * 410 ptov(SC_REQ *reqp, u_long *phaddr) 411 { 412 struct dma_chain *dm; 413 u_char *vaddr; 414 415 dm = reqp->dm_chain; 416 vaddr = reqp->xdata_ptr; 417 for(; dm < reqp->dm_cur; dm++) 418 vaddr += dm->dm_count; 419 vaddr += (u_long)phaddr - dm->dm_addr; 420 return(vaddr); 421 } 422 423 static int 424 tt_get_dma_result(SC_REQ *reqp, u_long *bytes_left) 425 { 426 int dmastat, dmstat; 427 u_char *byte_p; 428 u_long leftover; 429 430 dmastat = SCSI_DMA->s_dma_ctrl; 431 dmstat = GET_TT_REG(NCR5380_DMSTAT); 432 get_scsi_dma(SCSI_DMA->s_dma_cnt, leftover); 433 get_scsi_dma(SCSI_DMA->s_dma_ptr, (u_long)byte_p); 434 435 if (dmastat & SD_BUSERR) { 436 /* 437 * The DMA-controller seems to access 8 bytes beyond 438 * it's limits on output. Therefore check also the byte 439 * count. If it's zero, ignore the bus error. 440 */ 441 if (leftover != 0) { 442 ncr_tprint(reqp, 443 "SCSI-DMA buserror - accessing 0x%x\n", byte_p); 444 reqp->xs->error = XS_DRIVER_STUFFUP; 445 } 446 } 447 448 /* 449 * We handle the following special condition below: 450 * -- The device disconnects in the middle of a write operation -- 451 * In this case, the 5380 has already pre-fetched the next byte from 452 * the DMA-controller before the phase mismatch occurs. Therefore, 453 * leftover is 1 too low. 454 * This does not always happen! Therefore, we only do this when 455 * leftover is odd. This assumes that DMA transfers are _even_! This 456 * is normally the case on disks and types but might not always be. 457 * XXX: Check if ACK is consistently high on these occasions LWP 458 */ 459 if ((leftover & 1) && !(dmstat & SC_PHS_MTCH) && PH_OUT(reqp->phase)) 460 leftover++; 461 462 /* 463 * Check if there are some 'restbytes' left in the DMA-controller. 464 */ 465 if ((machineid & ATARI_TT) && ((u_long)byte_p & 3) 466 && PH_IN(reqp->phase)) { 467 u_char *p, *q; 468 469 p = ptov(reqp, (u_long *)((u_long)byte_p & ~3)); 470 q = (u_char*)&(SCSI_DMA->s_dma_res); 471 switch ((u_long)byte_p & 3) { 472 case 3: *p++ = *q++; 473 case 2: *p++ = *q++; 474 case 1: *p++ = *q++; 475 } 476 } 477 *bytes_left = leftover; 478 return ((dmastat & (SD_BUSERR|SD_ZERO)) ? 1 : 0); 479 } 480 481 static u_char *dma_ptr; 482 void 483 ncr5380_drq_intr(poll) 484 int poll; 485 { 486 extern int *nofault; 487 label_t faultbuf; 488 int write; 489 u_long count; 490 u_char *data_p = (u_char*)(stio_addr+0x741); 491 492 /* 493 * Block SCSI interrupts while emulating DMA. They come 494 * at a higher priority. 495 */ 496 single_inst_bclr_b(MFP2->mf_imra, IA_SCSI); 497 498 /* 499 * Setup for a possible bus error caused by SCSI controller 500 * switching out of DATA-IN/OUT before we're done with the 501 * current transfer. 502 */ 503 nofault = (int *) &faultbuf; 504 505 if (setjmp((label_t *) nofault)) { 506 u_char *ptr; 507 u_long cnt, tmp; 508 509 PID("drq berr"); 510 nofault = (int *) 0; 511 512 /* 513 * Determine number of bytes transferred 514 */ 515 get_scsi_dma(SCSI_DMA->s_dma_ptr, (u_long)ptr); 516 cnt = dma_ptr - ptr; 517 518 if (cnt != 0) { 519 /* 520 * Update the dma pointer/count fields 521 */ 522 set_scsi_dma(SCSI_DMA->s_dma_ptr, dma_ptr); 523 get_scsi_dma(SCSI_DMA->s_dma_cnt, tmp); 524 set_scsi_dma(SCSI_DMA->s_dma_cnt, tmp - cnt); 525 526 if (tmp > cnt) { 527 /* 528 * Still more to transfer 529 */ 530 if (!poll) 531 single_inst_bset_b(MFP2->mf_imra, IA_SCSI); 532 return; 533 } 534 535 /* 536 * Signal EOP to 5380 537 */ 538 SCSI_DMA->s_hdma_ctrl |= SDH_EOP; 539 } 540 else { 541 nofault = (int *) &faultbuf; 542 543 /* 544 * Try to figure out if the byte-count was 545 * zero because there was no (more) data or 546 * because the dma_ptr is bogus. 547 */ 548 if (setjmp((label_t *) nofault)) { 549 /* 550 * Set the bus-error bit 551 */ 552 SCSI_DMA->s_hdma_ctrl |= SDH_BUSERR; 553 } 554 __asm __volatile ("tstb %0@(0)": : "a" (dma_ptr)); 555 nofault = (int *)0; 556 } 557 558 /* 559 * Schedule an interrupt 560 */ 561 if (!poll && (SCSI_DMA->s_dma_ctrl & SD_ENABLE)) 562 add_sicallback((si_farg)ncr_dma_intr, (void *)cur_softc, 0); 563 564 /* 565 * Clear DMA-mode 566 */ 567 SCSI_DMA->s_dma_ctrl &= ~SD_ENABLE; 568 if (!poll) 569 single_inst_bset_b(MFP2->mf_imra, IA_SCSI); 570 571 return; 572 } 573 574 write = (SCSI_DMA->s_dma_ctrl & SD_OUT) ? 1 : 0; 575 #if DBG_PID 576 if (write) { 577 PID("drq (in)"); 578 } else { 579 PID("drq (out)"); 580 } 581 #endif 582 583 get_scsi_dma(SCSI_DMA->s_dma_cnt, count); 584 get_scsi_dma(SCSI_DMA->s_dma_ptr, (u_long)dma_ptr); 585 586 /* 587 * Keep pushing bytes until we're done or a bus-error 588 * signals that the SCSI controller is not ready. 589 * NOTE: I tried some optimalizations in these loops, 590 * but they had no effect on transfer speed. 591 */ 592 if (write) { 593 while(count--) { 594 *data_p = *dma_ptr++; 595 } 596 } 597 else { 598 while(count--) { 599 *dma_ptr++ = *data_p; 600 } 601 } 602 603 /* 604 * OK. No bus error occurred above. Clear the nofault flag 605 * so we no longer short-circuit bus errors. 606 */ 607 nofault = (int *) 0; 608 609 /* 610 * Schedule an interrupt 611 */ 612 if (!poll && (SCSI_DMA->s_dma_ctrl & SD_ENABLE)) 613 add_sicallback((si_farg)ncr_dma_intr, (void *)cur_softc, 0); 614 615 /* 616 * Clear DMA-mode 617 */ 618 SCSI_DMA->s_dma_ctrl &= ~SD_ENABLE; 619 620 /* 621 * Update the DMA 'registers' to reflect that all bytes 622 * have been transfered and tell this to the 5380 too. 623 */ 624 set_scsi_dma(SCSI_DMA->s_dma_ptr, (u_long)dma_ptr); 625 set_scsi_dma(SCSI_DMA->s_dma_cnt, 0); 626 SCSI_DMA->s_hdma_ctrl |= SDH_EOP; 627 628 PID("end drq"); 629 if (!poll) 630 single_inst_bset_b(MFP2->mf_imra, IA_SCSI); 631 632 return; 633 } 634 635 #endif /* defined(TT_SCSI) */ 636 637 #if defined(FALCON_SCSI) && !defined(TT_SCSI) 638 639 #define GET_5380_REG(rnum) get_falcon_5380_reg(rnum) 640 #define SET_5380_REG(rnum,val) set_falcon_5380_reg(rnum, val) 641 #define scsi_mach_init(sc) scsi_falcon_init(sc) 642 #define scsi_ienable() scsi_falcon_ienable() 643 #define scsi_idisable() scsi_falcon_idisable() 644 #define scsi_clr_ipend() scsi_falcon_clr_ipend() 645 #define scsi_ipending() scsi_falcon_ipending() 646 #define scsi_dma_setup(r,p,m) scsi_falcon_dmasetup(r, p, m) 647 #define wrong_dma_range(r,d) falcon_wrong_dma_range(r, d) 648 #define poll_edma(reqp) falcon_poll_edma(reqp) 649 #define get_dma_result(r, b) falcon_get_dma_result(r, b) 650 #define can_access_5380() falcon_can_access_5380() 651 #define emulated_dma() 0 652 653 #define fair_to_keep_dma() (!st_dmawanted()) 654 #define claimed_dma() falcon_claimed_dma() 655 #define reconsider_dma() falcon_reconsider_dma() 656 657 #endif /* defined(FALCON_SCSI) && !defined(TT_SCSI) */ 658 659 #if defined(FALCON_SCSI) 660 661 /* 662 * Prototype functions defined below 663 */ 664 static void scsi_falcon_init __P((struct ncr_softc *)); 665 static u_char get_falcon_5380_reg __P((u_short)); 666 static void set_falcon_5380_reg __P((u_short, u_short)); 667 static int falcon_wrong_dma_range __P((SC_REQ *, struct dma_chain *)); 668 static void fal1_dma __P((u_int, u_int, SC_REQ *)); 669 static void scsi_falcon_dmasetup __P((SC_REQ *, u_int, u_char)); 670 static int falcon_poll_edma __P((SC_REQ *)); 671 static int falcon_get_dma_result __P((SC_REQ *, u_long *)); 672 int falcon_can_access_5380 __P((void)); 673 void scsi_falcon_clr_ipend __P((void)); 674 void scsi_falcon_idisable __P((void)); 675 void scsi_falcon_ienable __P((void)); 676 int scsi_falcon_ipending __P((void)); 677 int falcon_claimed_dma __P((void)); 678 void falcon_reconsider_dma __P((void)); 679 680 static void 681 scsi_falcon_init(sc) 682 struct ncr_softc *sc; 683 { 684 /* 685 * Enable disk related interrupts 686 */ 687 MFP->mf_ierb |= IB_DINT; 688 MFP->mf_iprb = (u_int8_t)~IB_DINT; 689 MFP->mf_imrb |= IB_DINT; 690 } 691 692 static u_char 693 get_falcon_5380_reg(rnum) 694 u_short rnum; 695 { 696 DMA->dma_mode = DMA_SCSI + rnum; 697 return(DMA->dma_data); 698 } 699 700 static void 701 set_falcon_5380_reg(rnum, val) 702 u_short rnum, val; 703 { 704 DMA->dma_mode = DMA_SCSI + rnum; 705 DMA->dma_data = val; 706 } 707 708 extern __inline__ void 709 scsi_falcon_ienable() 710 { 711 single_inst_bset_b(MFP->mf_imrb, IB_DINT); 712 } 713 714 extern __inline__ void 715 scsi_falcon_idisable() 716 { 717 single_inst_bclr_b(MFP->mf_imrb, IB_DINT); 718 } 719 720 extern __inline__ void 721 scsi_falcon_clr_ipend() 722 { 723 int tmp; 724 725 tmp = get_falcon_5380_reg(NCR5380_IRCV); 726 rem_sicallback((si_farg)ncr_ctrl_intr); 727 } 728 729 extern __inline__ int 730 scsi_falcon_ipending() 731 { 732 if (connected && (connected->dr_flag & DRIVER_IN_DMA)) { 733 /* 734 * XXX: When DMA is running, we are only allowed to 735 * check the 5380 when DMA _might_ be finished. 736 */ 737 if (MFP->mf_gpip & IO_DINT) 738 return (0); /* XXX: Actually: we're not allowed to check */ 739 740 /* LWP: 28-06, must be a dma interrupt! should the 741 * ST-DMA unit be taken out of dma mode????? 742 */ 743 DMA->dma_mode = 0x90; 744 745 } 746 return(get_falcon_5380_reg(NCR5380_DMSTAT) & SC_IRQ_SET); 747 } 748 749 static int 750 falcon_wrong_dma_range(reqp, dm) 751 SC_REQ *reqp; 752 struct dma_chain *dm; 753 { 754 /* 755 * Do not allow chains yet! See also comment with 756 * falcon_poll_edma() !!! 757 */ 758 if (((dm - reqp->dm_chain) > 0) || (dm->dm_addr & 0xff000000)) { 759 reqp->dr_flag |= DRIVER_BOUNCING; 760 return(1); 761 } 762 /* 763 * Never allow DMA to happen on a Falcon when the transfer 764 * size is no multiple of 512. This is the transfer unit of the 765 * ST DMA-controller. 766 */ 767 if(dm->dm_count & 511) 768 return(1); 769 return(0); 770 } 771 772 static int falcon_lock = 0; 773 774 extern __inline__ int 775 falcon_claimed_dma() 776 { 777 if (falcon_lock != DMA_LOCK_GRANT) { 778 if (falcon_lock == DMA_LOCK_REQ) { 779 /* 780 * DMA access is being claimed. 781 */ 782 return(0); 783 } 784 if (!st_dmagrab((dma_farg)ncr_ctrl_intr, (dma_farg)run_main, 785 cur_softc, &falcon_lock, 1)) 786 return(0); 787 } 788 return(1); 789 } 790 791 extern __inline__ void 792 falcon_reconsider_dma() 793 { 794 if (falcon_lock && (connected == NULL) && (discon_q == NULL)) { 795 /* 796 * No need to keep DMA locked by us as we are not currently 797 * connected and no disconnected jobs are pending. 798 */ 799 rem_sicallback((si_farg)ncr_ctrl_intr); 800 st_dmafree(cur_softc, &falcon_lock); 801 } 802 803 if (!falcon_lock && (issue_q != NULL)) { 804 /* 805 * We must (re)claim DMA access as there are jobs 806 * waiting in the issue queue. 807 */ 808 st_dmagrab((dma_farg)ncr_ctrl_intr, (dma_farg)run_main, 809 cur_softc, &falcon_lock, 0); 810 } 811 } 812 813 static void 814 fal1_dma(dir, nsects, reqp) 815 u_int dir, nsects; 816 SC_REQ *reqp; 817 { 818 dir <<= 8; 819 st_dmaaddr_set((caddr_t)reqp->dm_cur->dm_addr); 820 DMA->dma_mode = 0x90 | dir; 821 DMA->dma_mode = 0x90 | (dir ^ DMA_WRBIT); 822 DMA->dma_mode = 0x90 | dir; 823 DMA->dma_data = nsects; 824 delay(2); /* _really_ needed (Thomas Gerner) */ 825 DMA->dma_mode = 0x10 | dir; 826 } 827 828 static void 829 scsi_falcon_dmasetup(reqp, phase, mode) 830 SC_REQ *reqp; 831 u_int phase; 832 u_char mode; 833 { 834 int nsects = reqp->dm_cur->dm_count / 512; /* XXX */ 835 836 /* 837 * XXX: We should probably clear the fifo before putting the 838 * 5380 into DMA-mode. 839 */ 840 if (PH_IN(phase)) { 841 set_falcon_5380_reg(NCR5380_ICOM, 0); 842 set_falcon_5380_reg(NCR5380_MODE, mode); 843 set_falcon_5380_reg(NCR5380_IRCV, 0); 844 fal1_dma(0, nsects, reqp); 845 } 846 else { 847 set_falcon_5380_reg(NCR5380_MODE, mode); 848 set_falcon_5380_reg(NCR5380_ICOM, SC_ADTB); 849 set_falcon_5380_reg(NCR5380_DMSTAT, 0); 850 fal1_dma(1, nsects, reqp); 851 } 852 } 853 854 static int 855 falcon_poll_edma(reqp) 856 SC_REQ *reqp; 857 { 858 int timeout = 9000; /* XXX */ 859 860 /* 861 * Because of the Falcon hardware, it is impossible to reach 862 * the 5380 while doing DMA-transfers. So we have to rely on 863 * the interrupt line to determine if DMA-has finished. the 864 * DMA-controller itself will never fire an interrupt. This means 865 * that 'broken-up' DMA transfers are not (yet) possible on the 866 * Falcon. 867 */ 868 for (;;) { 869 delay(20); 870 if (--timeout <= 0) { 871 ncr_tprint(reqp, "Timeout on polled transfer\n"); 872 reqp->xs->error = XS_TIMEOUT; 873 return(0); 874 } 875 if (!(MFP->mf_gpip & IO_DINT)) 876 break; 877 } 878 return(1); 879 } 880 881 static int 882 falcon_get_dma_result(reqp, bytes_left) 883 SC_REQ *reqp; 884 u_long *bytes_left; 885 { 886 int rv = 0; 887 int st_dmastat; 888 u_long bytes_done; 889 890 /* 891 * Select sector counter register first (See Atari docu.) 892 */ 893 DMA->dma_mode = 0x90; 894 if (!(st_dmastat = DMA->dma_stat) & 0x01) { 895 /* 896 * Misc. DMA-error according to Atari... 897 */ 898 ncr_tprint(reqp, "Unknow ST-SCSI error near 0x%x\n", 899 st_dmaaddr_get()); 900 reqp->xs->error = XS_DRIVER_STUFFUP; 901 rv = 1; 902 } 903 /* 904 * Because we NEVER start DMA on the Falcon when the data size 905 * is not a multiple of 512 bytes, we can safely round down the 906 * byte count on writes. We need to because in case of a disconnect, 907 * the DMA has already prefetched the next couple of bytes. 908 * On read, these byte counts are an error. They are logged and 909 * should be handled by the mi-part of the driver. 910 * NOTE: We formerly did this by using the 'byte-count-zero' bit 911 * of the DMA controller, but this didn't seem to work??? 912 * [lwp 29/06/96] 913 */ 914 bytes_done = st_dmaaddr_get() - reqp->dm_cur->dm_addr; 915 if (bytes_done & 511) { 916 if (PH_IN(reqp->phase)) { 917 ncr_tprint(reqp, "Byte count on read not a multiple " 918 "of 512 (%ld)\n", bytes_done); 919 } 920 bytes_done &= ~511; 921 } 922 if ((*bytes_left = reqp->dm_cur->dm_count - bytes_done) == 0) 923 rv = 1; 924 return(rv); 925 } 926 927 static int 928 falcon_can_access_5380() 929 { 930 if (connected && (connected->dr_flag & DRIVER_IN_DMA) 931 && (MFP->mf_gpip & IO_DINT)) 932 return(0); 933 return(1); 934 } 935 936 #endif /* defined(FALCON_SCSI) */ 937 938 #if defined(TT_SCSI) && defined(FALCON_SCSI) 939 /* 940 * Define some functions to support _both_ TT and Falcon SCSI 941 */ 942 943 /* 944 * The prototypes first... 945 */ 946 static void scsi_mach_init __P((struct ncr_softc *)); 947 void scsi_ienable __P((void)); 948 void scsi_idisable __P((void)); 949 void scsi_clr_ipend __P((void)); 950 int scsi_ipending __P((void)); 951 void scsi_dma_setup __P((SC_REQ *, u_int, u_char)); 952 int wrong_dma_range __P((SC_REQ *, struct dma_chain *)); 953 int poll_edma __P((SC_REQ *)); 954 int get_dma_result __P((SC_REQ *, u_long *)); 955 int can_access_5380 __P((void)); 956 957 /* 958 * Register access will be done through the following 2 function pointers. 959 */ 960 static u_char (*get_5380_reg) __P((u_short)); 961 static void (*set_5380_reg) __P((u_short, u_short)); 962 963 #define GET_5380_REG (*get_5380_reg) 964 #define SET_5380_REG (*set_5380_reg) 965 966 static void 967 scsi_mach_init(sc) 968 struct ncr_softc *sc; 969 { 970 if (machineid & ATARI_FALCON) { 971 get_5380_reg = get_falcon_5380_reg; 972 set_5380_reg = set_falcon_5380_reg; 973 scsi_falcon_init(sc); 974 } 975 else { 976 get_5380_reg = get_tt_5380_reg; 977 set_5380_reg = set_tt_5380_reg; 978 scsi_tt_init(sc); 979 } 980 } 981 982 extern __inline__ void 983 scsi_ienable() 984 { 985 if (machineid & ATARI_FALCON) 986 scsi_falcon_ienable(); 987 else scsi_tt_ienable(); 988 } 989 990 extern __inline__ void 991 scsi_idisable() 992 { 993 if (machineid & ATARI_FALCON) 994 scsi_falcon_idisable(); 995 else scsi_tt_idisable(); 996 } 997 998 extern __inline__ void 999 scsi_clr_ipend() 1000 { 1001 if (machineid & ATARI_FALCON) 1002 scsi_falcon_clr_ipend(); 1003 else scsi_tt_clr_ipend(); 1004 } 1005 1006 extern __inline__ int 1007 scsi_ipending() 1008 { 1009 if (machineid & ATARI_FALCON) 1010 return(scsi_falcon_ipending()); 1011 else return (GET_TT_REG(NCR5380_DMSTAT) & SC_IRQ_SET); 1012 } 1013 1014 extern __inline__ void 1015 scsi_dma_setup(reqp, phase, mbase) 1016 SC_REQ *reqp; 1017 u_int phase; 1018 u_char mbase; 1019 { 1020 if (machineid & ATARI_FALCON) 1021 scsi_falcon_dmasetup(reqp, phase, mbase); 1022 else scsi_tt_dmasetup(reqp, phase, mbase); 1023 } 1024 1025 extern __inline__ int 1026 wrong_dma_range(reqp, dm) 1027 SC_REQ *reqp; 1028 struct dma_chain *dm; 1029 { 1030 if (machineid & ATARI_FALCON) 1031 return(falcon_wrong_dma_range(reqp, dm)); 1032 else return(tt_wrong_dma_range(reqp, dm)); 1033 } 1034 1035 extern __inline__ int 1036 poll_edma(reqp) 1037 SC_REQ *reqp; 1038 { 1039 if (machineid & ATARI_FALCON) 1040 return(falcon_poll_edma(reqp)); 1041 else return(tt_poll_edma(reqp)); 1042 } 1043 1044 extern __inline__ int 1045 get_dma_result(reqp, bytes_left) 1046 SC_REQ *reqp; 1047 u_long *bytes_left; 1048 { 1049 if (machineid & ATARI_FALCON) 1050 return(falcon_get_dma_result(reqp, bytes_left)); 1051 else return(tt_get_dma_result(reqp, bytes_left)); 1052 } 1053 1054 extern __inline__ int 1055 can_access_5380() 1056 { 1057 if (machineid & ATARI_FALCON) 1058 return(falcon_can_access_5380()); 1059 return(1); 1060 } 1061 1062 #define emulated_dma() ((machineid & ATARI_HADES) ? 1 : 0) 1063 1064 /* 1065 * Locking stuff. All turns into NOP's on the TT. 1066 */ 1067 #define fair_to_keep_dma() ((machineid & ATARI_FALCON) ? \ 1068 !st_dmawanted() : 1) 1069 #define claimed_dma() ((machineid & ATARI_FALCON) ? \ 1070 falcon_claimed_dma() : 1) 1071 #define reconsider_dma() { \ 1072 if(machineid & ATARI_FALCON) \ 1073 falcon_reconsider_dma();\ 1074 } 1075 #endif /* defined(TT_SCSI) && defined(FALCON_SCSI) */ 1076 1077 /********************************************** 1078 * Functions present for both TT and Falcon. * 1079 **********************************************/ 1080 /* 1081 * Our autoconfig matching function 1082 */ 1083 static int 1084 machine_match(struct device *pdp, void *match, void *auxp, 1085 struct cfdriver *cd) 1086 { 1087 static int we_matched = 0; /* Only one unit */ 1088 1089 if (strcmp(auxp, cd->cd_name) || we_matched) 1090 return(0); 1091 1092 we_matched = 1; 1093 return(1); 1094 } 1095 1096 /* 1097 * Bounce buffer (de)allocation. Those buffers are gotten from the ST-mem 1098 * pool. Allocation here is both contiguous and in the lower 16Mb of 1099 * the address space. Thus being DMA-able for all controllers. 1100 */ 1101 static u_char * 1102 alloc_bounceb(u_long len) 1103 { 1104 void *tmp; 1105 1106 return((u_char *)alloc_stmem(len, &tmp)); 1107 } 1108 1109 static void 1110 free_bounceb(u_char *bounceb) 1111 { 1112 free_stmem(bounceb); 1113 } 1114 1115 /* 1116 * 5380 interrupt. 1117 */ 1118 void 1119 scsi_ctrl(int sr) 1120 { 1121 if (GET_5380_REG(NCR5380_DMSTAT) & SC_IRQ_SET) { 1122 scsi_idisable(); 1123 if (!BASEPRI(sr)) 1124 add_sicallback((si_farg)ncr_ctrl_intr, 1125 (void *)cur_softc, 0); 1126 else { 1127 spl1(); 1128 ncr_ctrl_intr(cur_softc); 1129 spl0(); 1130 } 1131 } 1132 } 1133 1134 /* 1135 * DMA controller interrupt 1136 */ 1137 void 1138 scsi_dma(int sr) 1139 { 1140 SC_REQ *reqp; 1141 1142 if ((reqp = connected) && (reqp->dr_flag & DRIVER_IN_DMA)) { 1143 scsi_idisable(); 1144 if (!BASEPRI(sr)) 1145 add_sicallback((si_farg)ncr_dma_intr, 1146 (void *)cur_softc, 0); 1147 else { 1148 spl1(); 1149 ncr_dma_intr(cur_softc); 1150 spl0(); 1151 } 1152 } 1153 } 1154 1155 /* 1156 * Last but not least... Include the general driver code 1157 */ 1158 #include <atari/dev/ncr5380.c> 1159