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