- pre3:
[davej-history.git] / drivers / scsi / sun3_scsi.c
blob6769009d58e2102a55ccb80e681abfd1d31ac247
1 /*
2 * Sun3 SCSI stuff by Erik Verbruggen (erik@bigmama.xtdnet.nl)
4 * Sun3 DMA routines added by Sam Creasey (sammy@oh.verio.com)
6 * Adapted from mac_scsinew.c:
7 */
8 /*
9 * Generic Macintosh NCR5380 driver
11 * Copyright 1998, Michael Schmitz <mschmitz@lbl.gov>
13 * derived in part from:
16 * Generic Generic NCR5380 driver
18 * Copyright 1995, Russell King
20 * ALPHA RELEASE 1.
22 * For more information, please consult
24 * NCR 5380 Family
25 * SCSI Protocol Controller
26 * Databook
28 * NCR Microelectronics
29 * 1635 Aeroplaza Drive
30 * Colorado Springs, CO 80916
31 * 1+ (719) 578-3400
32 * 1+ (800) 334-5454
37 * This is from mac_scsi.h, but hey, maybe this is usefull for Sun3 too! :)
39 * Options :
41 * PARITY - enable parity checking. Not supported.
43 * SCSI2 - enable support for SCSI-II tagged queueing. Untested.
45 * USLEEP - enable support for devices that don't disconnect. Untested.
49 * $Log: sun3_NCR5380.c,v $
52 #define AUTOSENSE
54 #include <linux/types.h>
55 #include <linux/stddef.h>
56 #include <linux/ctype.h>
57 #include <linux/delay.h>
59 #include <linux/module.h>
60 #include <linux/signal.h>
61 #include <linux/sched.h>
62 #include <linux/ioport.h>
63 #include <linux/init.h>
64 #include <linux/blk.h>
66 #include <asm/io.h>
67 #include <asm/system.h>
69 #include <asm/sun3ints.h>
70 #include <asm/dvma.h>
71 /* dma on! */
72 #define REAL_DMA
74 #include "scsi.h"
75 #include "hosts.h"
76 #include "sun3_scsi.h"
77 #include "NCR5380.h"
78 #include "constants.h"
80 #define USE_WRAPPER
81 #define RESET_BOOT
82 #define DRIVER_SETUP
84 #define NDEBUG 0
87 * BUG can be used to trigger a strange code-size related hang on 2.1 kernels
89 #ifdef BUG
90 #undef RESET_BOOT
91 #undef DRIVER_SETUP
92 #endif
94 #undef SUPPORT_TAGS
96 #define ENABLE_IRQ() enable_irq( IRQ_SUN3_SCSI );
99 static void scsi_sun3_intr(int irq, void *dummy, struct pt_regs *fp);
100 static inline unsigned char sun3scsi_read(int reg);
101 static inline void sun3scsi_write(int reg, int value);
103 static int setup_can_queue = -1;
104 static int setup_cmd_per_lun = -1;
105 static int setup_sg_tablesize = -1;
106 #ifdef SUPPORT_TAGS
107 static int setup_use_tagged_queuing = -1;
108 #endif
109 static int setup_hostid = -1;
111 static Scsi_Cmnd *sun3_dma_setup_done = NULL;
113 #define AFTER_RESET_DELAY (HZ/2)
115 /* ms to wait after hitting dma regs */
116 #define SUN3_DMA_DELAY 5
118 /* dvma buffer to allocate -- 32k should hopefully be more than sufficient */
119 #define SUN3_DVMA_BUFSIZE 0xe000
121 /* minimum number of bytes to to dma on */
122 #define SUN3_DMA_MINSIZE 128
124 static volatile unsigned char *sun3_scsi_regp;
125 static volatile struct sun3_dma_regs *dregs;
126 static unsigned char *dmabuf = NULL; /* dma memory buffer */
127 static struct sun3_udc_regs *udc_regs = NULL;
128 static void *sun3_dma_orig_addr = NULL;
129 static unsigned long sun3_dma_orig_count = 0;
130 static int sun3_dma_active = 0;
133 * NCR 5380 register access functions
136 static inline unsigned char sun3scsi_read(int reg)
138 return( sun3_scsi_regp[reg] );
141 static inline void sun3scsi_write(int reg, int value)
143 sun3_scsi_regp[reg] = value;
146 /* dma controller register access functions */
148 static inline unsigned short sun3_udc_read(unsigned char reg)
150 unsigned short ret;
152 dregs->udc_addr = UDC_CSR;
153 udelay(SUN3_DMA_DELAY);
154 ret = dregs->udc_data;
155 udelay(SUN3_DMA_DELAY);
157 return ret;
160 static inline void sun3_udc_write(unsigned short val, unsigned char reg)
162 dregs->udc_addr = reg;
163 udelay(SUN3_DMA_DELAY);
164 dregs->udc_data = val;
165 udelay(SUN3_DMA_DELAY);
169 * XXX: status debug
171 static struct Scsi_Host *default_instance;
174 * Function : int sun3scsi_detect(Scsi_Host_Template * tpnt)
176 * Purpose : initializes mac NCR5380 driver based on the
177 * command line / compile time port and irq definitions.
179 * Inputs : tpnt - template for this SCSI adapter.
181 * Returns : 1 if a host adapter was found, 0 if not.
185 int sun3scsi_detect(Scsi_Host_Template * tpnt)
187 unsigned long ioaddr, iopte;
188 int count = 0;
189 static int called = 0;
190 struct Scsi_Host *instance;
192 if(called)
193 return 0;
195 tpnt->proc_name = "Sun3 5380 SCSI";
197 /* setup variables */
198 tpnt->can_queue =
199 (setup_can_queue > 0) ? setup_can_queue : CAN_QUEUE;
200 tpnt->cmd_per_lun =
201 (setup_cmd_per_lun > 0) ? setup_cmd_per_lun : CMD_PER_LUN;
202 tpnt->sg_tablesize =
203 (setup_sg_tablesize >= 0) ? setup_sg_tablesize : SG_TABLESIZE;
205 if (setup_hostid >= 0)
206 tpnt->this_id = setup_hostid;
207 else {
208 /* use 7 as default */
209 tpnt->this_id = 7;
212 /* Taken from Sammy's lance driver: */
213 /* IOBASE_SUN3_SCSI can be found within the IO pmeg with some effort */
214 for(ioaddr = 0xfe00000; ioaddr < (0xfe00000 + SUN3_PMEG_SIZE);
215 ioaddr += SUN3_PTE_SIZE) {
217 iopte = sun3_get_pte(ioaddr);
218 if(!(iopte & SUN3_PAGE_TYPE_IO)) /* this an io page? */
219 continue;
221 if(((iopte & SUN3_PAGE_PGNUM_MASK) << PAGE_SHIFT) ==
222 IOBASE_SUN3_SCSI) {
223 count = 1;
224 break;
228 if(!count) {
229 printk("No Sun3 NCR5380 found!\n");
230 return 0;
233 sun3_scsi_regp = (unsigned char *)ioaddr;
234 dregs = (struct sun3_dma_regs *)(((unsigned char *)ioaddr) + 8);
236 if((dmabuf = sun3_dvma_malloc(SUN3_DVMA_BUFSIZE)) == NULL) {
237 printk("SUN3 Scsi couldn't allocate DVMA memory!\n");
238 return 0;
241 if((udc_regs = sun3_dvma_malloc(sizeof(struct sun3_udc_regs)))
242 == NULL) {
243 printk("SUN3 Scsi couldn't allocate DVMA memory!\n");
244 return 0;
247 #ifdef SUPPORT_TAGS
248 if (setup_use_tagged_queuing < 0)
249 setup_use_tagged_queuing = DEFAULT_USE_TAGGED_QUEUING;
250 #endif
252 instance = scsi_register (tpnt, sizeof(struct NCR5380_hostdata));
253 if(instance == NULL)
254 return 0;
256 default_instance = instance;
258 instance->io_port = (unsigned long) ioaddr;
259 instance->irq = IRQ_SUN3_SCSI;
261 NCR5380_init(instance, 0);
263 instance->n_io_port = 32;
265 ((struct NCR5380_hostdata *)instance->hostdata)->ctrl = 0;
267 if (request_irq(instance->irq, scsi_sun3_intr,
268 0, "Sun3SCSI-5380", NULL)) {
269 #ifndef REAL_DMA
270 printk("scsi%d: IRQ%d not free, interrupts disabled\n",
271 instance->host_no, instance->irq);
272 instance->irq = IRQ_NONE;
273 #else
274 printk("scsi%d: IRQ%d not free, bailing out\n",
275 instance->host_no, instance->irq);
276 return 0;
277 #endif
280 printk("scsi%d: Sun3 5380 at port %lX irq", instance->host_no, instance->io_port);
281 if (instance->irq == IRQ_NONE)
282 printk ("s disabled");
283 else
284 printk (" %d", instance->irq);
285 printk(" options CAN_QUEUE=%d CMD_PER_LUN=%d release=%d",
286 instance->can_queue, instance->cmd_per_lun,
287 SUN3SCSI_PUBLIC_RELEASE);
288 printk("\nscsi%d:", instance->host_no);
289 NCR5380_print_options(instance);
290 printk("\n");
292 dregs->csr = 0;
293 udelay(SUN3_DMA_DELAY);
294 dregs->csr = CSR_SCSI | CSR_FIFO | CSR_INTR;
295 udelay(SUN3_DMA_DELAY);
296 dregs->fifo_count = 0;
298 called = 1;
299 return 1;
302 int sun3scsi_release (struct Scsi_Host *shpnt)
304 if (shpnt->irq != IRQ_NONE)
305 free_irq (shpnt->irq, NULL);
307 return 0;
310 #ifdef RESET_BOOT
312 * Our 'bus reset on boot' function
315 static void sun3_scsi_reset_boot(struct Scsi_Host *instance)
317 unsigned long end;
319 NCR5380_local_declare();
320 NCR5380_setup(instance);
323 * Do a SCSI reset to clean up the bus during initialization. No
324 * messing with the queues, interrupts, or locks necessary here.
327 printk( "Sun3 SCSI: resetting the SCSI bus..." );
329 /* switch off SCSI IRQ - catch an interrupt without IRQ bit set else */
330 sun3_disable_irq( IRQ_SUN3_SCSI );
332 /* get in phase */
333 NCR5380_write( TARGET_COMMAND_REG,
334 PHASE_SR_TO_TCR( NCR5380_read(STATUS_REG) ));
336 /* assert RST */
337 NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST );
339 /* The min. reset hold time is 25us, so 40us should be enough */
340 udelay( 50 );
342 /* reset RST and interrupt */
343 NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE );
344 NCR5380_read( RESET_PARITY_INTERRUPT_REG );
346 for( end = jiffies + AFTER_RESET_DELAY; jiffies < end; )
347 barrier();
349 /* switch on SCSI IRQ again */
350 sun3_enable_irq( IRQ_SUN3_SCSI );
352 printk( " done\n" );
354 #endif
356 const char * sun3scsi_info (struct Scsi_Host *spnt) {
357 return "";
360 // safe bits for the CSR
361 #define CSR_GOOD 0x060f
363 static void scsi_sun3_intr(int irq, void *dummy, struct pt_regs *fp)
365 unsigned short csr = dregs->csr;
367 if(csr & ~CSR_GOOD) {
368 if(csr & CSR_DMA_BUSERR) {
369 printk("scsi%d: bus error in dma\n", default_instance->host_no);
372 if(csr & CSR_DMA_CONFLICT) {
373 printk("scsi%d: dma conflict\n", default_instance->host_no);
377 if(csr & (CSR_SDB_INT | CSR_DMA_INT))
378 NCR5380_intr(irq, dummy, fp);
382 * Debug stuff - to be called on NMI, or sysrq key. Use at your own risk;
383 * reentering NCR5380_print_status seems to have ugly side effects
386 /* this doesn't seem to get used at all -- sam */
387 #if 0
388 void sun3_sun3_debug (void)
390 unsigned long flags;
391 NCR5380_local_declare();
393 if (default_instance) {
394 save_flags(flags);
395 cli();
396 NCR5380_print_status(default_instance);
397 restore_flags(flags);
400 #endif
403 /* sun3scsi_dma_setup() -- initialize the dma controller for a read/write */
404 static unsigned long sun3scsi_dma_setup(void *data, unsigned long count, int write_flag)
406 if(write_flag)
407 memcpy(dmabuf, data, count);
408 else {
409 sun3_dma_orig_addr = data;
410 sun3_dma_orig_count = count;
413 dregs->fifo_count = 0;
414 sun3_udc_write(UDC_RESET, UDC_CSR);
416 /* reset fifo */
417 dregs->csr &= ~CSR_FIFO;
418 dregs->csr |= CSR_FIFO;
420 /* set direction */
421 if(write_flag)
422 dregs->csr |= CSR_SEND;
423 else
424 dregs->csr &= ~CSR_SEND;
426 /* byte count for fifo */
427 dregs->fifo_count = count;
429 sun3_udc_write(UDC_RESET, UDC_CSR);
431 /* reset fifo */
432 dregs->csr &= ~CSR_FIFO;
433 dregs->csr |= CSR_FIFO;
436 if(dregs->fifo_count != count) {
437 printk("scsi%d: fifo_mismatch %04x not %04x\n",
438 default_instance->host_no, dregs->fifo_count,
439 (unsigned int) count);
440 NCR5380_print(default_instance);
443 /* setup udc */
444 udc_regs->addr_hi = ((sun3_dvma_vtop(dmabuf) & 0xff0000) >> 8);
445 udc_regs->addr_lo = (sun3_dvma_vtop(dmabuf) & 0xffff);
446 udc_regs->count = count/2; /* count in words */
447 udc_regs->mode_hi = UDC_MODE_HIWORD;
448 if(write_flag) {
449 if(count & 1)
450 udc_regs->count++;
451 udc_regs->mode_lo = UDC_MODE_LSEND;
452 udc_regs->rsel = UDC_RSEL_SEND;
453 } else {
454 udc_regs->mode_lo = UDC_MODE_LRECV;
455 udc_regs->rsel = UDC_RSEL_RECV;
458 /* announce location of regs block */
459 sun3_udc_write(((sun3_dvma_vtop(udc_regs) & 0xff0000) >> 8),
460 UDC_CHN_HI);
462 sun3_udc_write((sun3_dvma_vtop(udc_regs) & 0xffff), UDC_CHN_LO);
464 /* set dma master on */
465 sun3_udc_write(0xd, UDC_MODE);
467 /* interrupt enable */
468 sun3_udc_write(UDC_INT_ENABLE, UDC_CSR);
470 return count;
474 static inline unsigned long sun3scsi_dma_residual(struct Scsi_Host *instance)
476 unsigned short resid;
478 dregs->udc_addr = 0x32;
479 udelay(SUN3_DMA_DELAY);
480 resid = dregs->udc_data;
481 udelay(SUN3_DMA_DELAY);
482 resid *= 2;
484 return (unsigned long) resid;
487 static inline unsigned long sun3scsi_dma_xfer_len(unsigned long wanted, Scsi_Cmnd *cmd,
488 int write_flag)
490 return wanted;
493 /* clean up after our dma is done */
494 static int sun3scsi_dma_finish(void)
496 unsigned short count;
497 int ret = 0;
499 count = sun3scsi_dma_residual(default_instance);
501 sun3_dma_active = 0;
503 /* if we've finished a read, copy out the data we read */
504 if(sun3_dma_orig_addr) {
505 /* check for residual bytes after dma end */
506 if(count && (NCR5380_read(BUS_AND_STATUS_REG) &
507 (BASR_PHASE_MATCH | BASR_ACK))) {
508 printk("scsi%d: sun3_scsi_finish: read overrun baby... ", default_instance->host_no);
509 printk("basr now %02x\n", NCR5380_read(BUS_AND_STATUS_REG));
510 ret = count;
513 /* copy in what we dma'd no matter what */
514 memcpy(sun3_dma_orig_addr, dmabuf, sun3_dma_orig_count);
515 sun3_dma_orig_addr = NULL;
519 sun3_udc_write(UDC_RESET, UDC_CSR);
521 dregs->csr &= ~CSR_SEND;
523 /* reset fifo */
524 dregs->csr &= ~CSR_FIFO;
525 dregs->csr |= CSR_FIFO;
527 sun3_dma_setup_done = NULL;
529 return ret;
533 #include "sun3_NCR5380.c"
535 static Scsi_Host_Template driver_template = SUN3_NCR5380;
537 #include "scsi_module.c"