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