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