blob: 99466e64e507cd5883fe603838416a642a1ccdd9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Sun3 SCSI stuff by Erik Verbruggen (erik@bigmama.xtdnet.nl)
3 *
4 * Sun3 DMA routines added by Sam Creasey (sammy@sammy.net)
5 *
6 * Adapted from mac_scsinew.c:
7 */
8/*
9 * Generic Macintosh NCR5380 driver
10 *
11 * Copyright 1998, Michael Schmitz <mschmitz@lbl.gov>
12 *
13 * derived in part from:
14 */
15/*
16 * Generic Generic NCR5380 driver
17 *
18 * Copyright 1995, Russell King
19 *
20 * ALPHA RELEASE 1.
21 *
22 * For more information, please consult
23 *
24 * NCR 5380 Family
25 * SCSI Protocol Controller
26 * Databook
27 *
28 * NCR Microelectronics
29 * 1635 Aeroplaza Drive
30 * Colorado Springs, CO 80916
31 * 1+ (719) 578-3400
32 * 1+ (800) 334-5454
33 */
34
35
36/*
37 * This is from mac_scsi.h, but hey, maybe this is useful for Sun3 too! :)
38 *
39 * Options :
40 *
41 * PARITY - enable parity checking. Not supported.
42 *
43 * SCSI2 - enable support for SCSI-II tagged queueing. Untested.
44 *
45 * USLEEP - enable support for devices that don't disconnect. Untested.
46 */
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#define AUTOSENSE
49
50#include <linux/types.h>
51#include <linux/stddef.h>
52#include <linux/ctype.h>
53#include <linux/delay.h>
54
55#include <linux/module.h>
56#include <linux/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <linux/ioport.h>
58#include <linux/init.h>
59#include <linux/blkdev.h>
60
61#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63#include <asm/sun3ints.h>
64#include <asm/dvma.h>
65#include <asm/idprom.h>
66#include <asm/machines.h>
67
68/* dma on! */
69#define REAL_DMA
70
71#include "scsi.h"
72#include <scsi/scsi_host.h>
73#include "sun3_scsi.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075/* #define OLDDMA */
76
77#define USE_WRAPPER
78/*#define RESET_BOOT */
79#define DRIVER_SETUP
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081/*
82 * BUG can be used to trigger a strange code-size related hang on 2.1 kernels
83 */
84#ifdef BUG
85#undef RESET_BOOT
86#undef DRIVER_SETUP
87#endif
88
89/* #define SUPPORT_TAGS */
90
91#define ENABLE_IRQ() enable_irq( IRQ_SUN3_SCSI );
92
93
David Howells7d12e782006-10-05 14:55:46 +010094static irqreturn_t scsi_sun3_intr(int irq, void *dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095static inline unsigned char sun3scsi_read(int reg);
96static inline void sun3scsi_write(int reg, int value);
97
98static int setup_can_queue = -1;
99module_param(setup_can_queue, int, 0);
100static int setup_cmd_per_lun = -1;
101module_param(setup_cmd_per_lun, int, 0);
102static int setup_sg_tablesize = -1;
103module_param(setup_sg_tablesize, int, 0);
104#ifdef SUPPORT_TAGS
105static int setup_use_tagged_queuing = -1;
106module_param(setup_use_tagged_queuing, int, 0);
107#endif
108static int setup_hostid = -1;
109module_param(setup_hostid, int, 0);
110
Henne811c9362006-10-03 19:51:59 +0200111static struct scsi_cmnd *sun3_dma_setup_done = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Michael Schmitz2b0f8342014-05-02 20:43:01 +1200113#define RESET_RUN_DONE
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115#define AFTER_RESET_DELAY (HZ/2)
116
117/* ms to wait after hitting dma regs */
118#define SUN3_DMA_DELAY 10
119
120/* dvma buffer to allocate -- 32k should hopefully be more than sufficient */
121#define SUN3_DVMA_BUFSIZE 0xe000
122
123/* minimum number of bytes to do dma on */
124#define SUN3_DMA_MINSIZE 128
125
126static volatile unsigned char *sun3_scsi_regp;
127static volatile struct sun3_dma_regs *dregs;
128#ifdef OLDDMA
129static unsigned char *dmabuf = NULL; /* dma memory buffer */
130#endif
131static struct sun3_udc_regs *udc_regs = NULL;
132static unsigned char *sun3_dma_orig_addr = NULL;
133static unsigned long sun3_dma_orig_count = 0;
134static int sun3_dma_active = 0;
135static unsigned long last_residual = 0;
136
137/*
138 * NCR 5380 register access functions
139 */
140
141static inline unsigned char sun3scsi_read(int reg)
142{
143 return( sun3_scsi_regp[reg] );
144}
145
146static inline void sun3scsi_write(int reg, int value)
147{
148 sun3_scsi_regp[reg] = value;
149}
150
151/* dma controller register access functions */
152
153static inline unsigned short sun3_udc_read(unsigned char reg)
154{
155 unsigned short ret;
156
157 dregs->udc_addr = UDC_CSR;
158 udelay(SUN3_DMA_DELAY);
159 ret = dregs->udc_data;
160 udelay(SUN3_DMA_DELAY);
161
162 return ret;
163}
164
165static inline void sun3_udc_write(unsigned short val, unsigned char reg)
166{
167 dregs->udc_addr = reg;
168 udelay(SUN3_DMA_DELAY);
169 dregs->udc_data = val;
170 udelay(SUN3_DMA_DELAY);
171}
172
173/*
174 * XXX: status debug
175 */
176static struct Scsi_Host *default_instance;
177
178/*
Christoph Hellwigd0be4a7d2005-10-31 18:31:40 +0100179 * Function : int sun3scsi_detect(struct scsi_host_template * tpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 *
181 * Purpose : initializes mac NCR5380 driver based on the
182 * command line / compile time port and irq definitions.
183 *
184 * Inputs : tpnt - template for this SCSI adapter.
185 *
186 * Returns : 1 if a host adapter was found, 0 if not.
187 *
188 */
189
Geert Uytterhoevend559c492011-06-13 20:39:18 +0200190int __init sun3scsi_detect(struct scsi_host_template * tpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
192 unsigned long ioaddr;
193 static int called = 0;
194 struct Scsi_Host *instance;
195
196 /* check that this machine has an onboard 5380 */
197 switch(idprom->id_machtype) {
198 case SM_SUN3|SM_3_50:
199 case SM_SUN3|SM_3_60:
200 break;
201
202 default:
203 return 0;
204 }
205
206 if(called)
207 return 0;
208
209 tpnt->proc_name = "Sun3 5380 SCSI";
210
211 /* setup variables */
212 tpnt->can_queue =
213 (setup_can_queue > 0) ? setup_can_queue : CAN_QUEUE;
214 tpnt->cmd_per_lun =
215 (setup_cmd_per_lun > 0) ? setup_cmd_per_lun : CMD_PER_LUN;
216 tpnt->sg_tablesize =
217 (setup_sg_tablesize >= 0) ? setup_sg_tablesize : SG_TABLESIZE;
218
219 if (setup_hostid >= 0)
220 tpnt->this_id = setup_hostid;
221 else {
222 /* use 7 as default */
223 tpnt->this_id = 7;
224 }
225
226 ioaddr = (unsigned long)ioremap(IOBASE_SUN3_SCSI, PAGE_SIZE);
227 sun3_scsi_regp = (unsigned char *)ioaddr;
228
229 dregs = (struct sun3_dma_regs *)(((unsigned char *)ioaddr) + 8);
230
231 if((udc_regs = dvma_malloc(sizeof(struct sun3_udc_regs)))
232 == NULL) {
233 printk("SUN3 Scsi couldn't allocate DVMA memory!\n");
234 return 0;
235 }
236#ifdef OLDDMA
237 if((dmabuf = dvma_malloc_align(SUN3_DVMA_BUFSIZE, 0x10000)) == NULL) {
238 printk("SUN3 Scsi couldn't allocate DVMA memory!\n");
239 return 0;
240 }
241#endif
242#ifdef SUPPORT_TAGS
243 if (setup_use_tagged_queuing < 0)
244 setup_use_tagged_queuing = USE_TAGGED_QUEUING;
245#endif
246
247 instance = scsi_register (tpnt, sizeof(struct NCR5380_hostdata));
248 if(instance == NULL)
249 return 0;
250
251 default_instance = instance;
252
253 instance->io_port = (unsigned long) ioaddr;
254 instance->irq = IRQ_SUN3_SCSI;
255
256 NCR5380_init(instance, 0);
257
258 instance->n_io_port = 32;
259
260 ((struct NCR5380_hostdata *)instance->hostdata)->ctrl = 0;
261
262 if (request_irq(instance->irq, scsi_sun3_intr,
Jeff Garzik1e641662007-11-11 19:52:05 -0500263 0, "Sun3SCSI-5380", instance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264#ifndef REAL_DMA
265 printk("scsi%d: IRQ%d not free, interrupts disabled\n",
266 instance->host_no, instance->irq);
267 instance->irq = SCSI_IRQ_NONE;
268#else
269 printk("scsi%d: IRQ%d not free, bailing out\n",
270 instance->host_no, instance->irq);
271 return 0;
272#endif
273 }
274
275 printk("scsi%d: Sun3 5380 at port %lX irq", instance->host_no, instance->io_port);
276 if (instance->irq == SCSI_IRQ_NONE)
277 printk ("s disabled");
278 else
279 printk (" %d", instance->irq);
280 printk(" options CAN_QUEUE=%d CMD_PER_LUN=%d release=%d",
281 instance->can_queue, instance->cmd_per_lun,
282 SUN3SCSI_PUBLIC_RELEASE);
283 printk("\nscsi%d:", instance->host_no);
284 NCR5380_print_options(instance);
285 printk("\n");
286
287 dregs->csr = 0;
288 udelay(SUN3_DMA_DELAY);
289 dregs->csr = CSR_SCSI | CSR_FIFO | CSR_INTR;
290 udelay(SUN3_DMA_DELAY);
291 dregs->fifo_count = 0;
292
293 called = 1;
294
295#ifdef RESET_BOOT
296 sun3_scsi_reset_boot(instance);
297#endif
298
299 return 1;
300}
301
302int sun3scsi_release (struct Scsi_Host *shpnt)
303{
304 if (shpnt->irq != SCSI_IRQ_NONE)
Jeff Garzik1e641662007-11-11 19:52:05 -0500305 free_irq(shpnt->irq, shpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 iounmap((void *)sun3_scsi_regp);
308
Geert Uytterhoeven19b6c512011-06-13 20:39:19 +0200309 NCR5380_exit(shpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return 0;
311}
312
313#ifdef RESET_BOOT
314/*
315 * Our 'bus reset on boot' function
316 */
317
318static void sun3_scsi_reset_boot(struct Scsi_Host *instance)
319{
320 unsigned long end;
321
322 NCR5380_local_declare();
323 NCR5380_setup(instance);
324
325 /*
326 * Do a SCSI reset to clean up the bus during initialization. No
327 * messing with the queues, interrupts, or locks necessary here.
328 */
329
330 printk( "Sun3 SCSI: resetting the SCSI bus..." );
331
332 /* switch off SCSI IRQ - catch an interrupt without IRQ bit set else */
333// sun3_disable_irq( IRQ_SUN3_SCSI );
334
335 /* get in phase */
336 NCR5380_write( TARGET_COMMAND_REG,
337 PHASE_SR_TO_TCR( NCR5380_read(STATUS_REG) ));
338
339 /* assert RST */
340 NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST );
341
342 /* The min. reset hold time is 25us, so 40us should be enough */
343 udelay( 50 );
344
345 /* reset RST and interrupt */
346 NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE );
347 NCR5380_read( RESET_PARITY_INTERRUPT_REG );
348
349 for( end = jiffies + AFTER_RESET_DELAY; time_before(jiffies, end); )
350 barrier();
351
352 /* switch on SCSI IRQ again */
353// sun3_enable_irq( IRQ_SUN3_SCSI );
354
355 printk( " done\n" );
356}
357#endif
358
359const char * sun3scsi_info (struct Scsi_Host *spnt) {
360 return "";
361}
362
363// safe bits for the CSR
364#define CSR_GOOD 0x060f
365
David Howells7d12e782006-10-05 14:55:46 +0100366static irqreturn_t scsi_sun3_intr(int irq, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
368 unsigned short csr = dregs->csr;
369 int handled = 0;
370
371 if(csr & ~CSR_GOOD) {
372 if(csr & CSR_DMA_BUSERR) {
373 printk("scsi%d: bus error in dma\n", default_instance->host_no);
374 }
375
376 if(csr & CSR_DMA_CONFLICT) {
377 printk("scsi%d: dma conflict\n", default_instance->host_no);
378 }
379 handled = 1;
380 }
381
382 if(csr & (CSR_SDB_INT | CSR_DMA_INT)) {
David Howells7d12e782006-10-05 14:55:46 +0100383 NCR5380_intr(irq, dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 handled = 1;
385 }
386
387 return IRQ_RETVAL(handled);
388}
389
390/*
391 * Debug stuff - to be called on NMI, or sysrq key. Use at your own risk;
392 * reentering NCR5380_print_status seems to have ugly side effects
393 */
394
395/* this doesn't seem to get used at all -- sam */
396#if 0
397void sun3_sun3_debug (void)
398{
399 unsigned long flags;
400 NCR5380_local_declare();
401
402 if (default_instance) {
403 local_irq_save(flags);
404 NCR5380_print_status(default_instance);
405 local_irq_restore(flags);
406 }
407}
408#endif
409
410
411/* sun3scsi_dma_setup() -- initialize the dma controller for a read/write */
412static unsigned long sun3scsi_dma_setup(void *data, unsigned long count, int write_flag)
413{
414#ifdef OLDDMA
415 if(write_flag)
416 memcpy(dmabuf, data, count);
417 else {
418 sun3_dma_orig_addr = data;
419 sun3_dma_orig_count = count;
420 }
421#else
422 void *addr;
423
424 if(sun3_dma_orig_addr != NULL)
425 dvma_unmap(sun3_dma_orig_addr);
426
427// addr = sun3_dvma_page((unsigned long)data, (unsigned long)dmabuf);
428 addr = (void *)dvma_map((unsigned long) data, count);
429
430 sun3_dma_orig_addr = addr;
431 sun3_dma_orig_count = count;
432#endif
433 dregs->fifo_count = 0;
434 sun3_udc_write(UDC_RESET, UDC_CSR);
435
436 /* reset fifo */
437 dregs->csr &= ~CSR_FIFO;
438 dregs->csr |= CSR_FIFO;
439
440 /* set direction */
441 if(write_flag)
442 dregs->csr |= CSR_SEND;
443 else
444 dregs->csr &= ~CSR_SEND;
445
446 /* byte count for fifo */
447 dregs->fifo_count = count;
448
449 sun3_udc_write(UDC_RESET, UDC_CSR);
450
451 /* reset fifo */
452 dregs->csr &= ~CSR_FIFO;
453 dregs->csr |= CSR_FIFO;
454
455 if(dregs->fifo_count != count) {
456 printk("scsi%d: fifo_mismatch %04x not %04x\n",
457 default_instance->host_no, dregs->fifo_count,
458 (unsigned int) count);
Finn Thaind614f062014-03-18 11:42:16 +1100459 NCR5380_dprint(NDEBUG_DMA, default_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
461
462 /* setup udc */
463#ifdef OLDDMA
464 udc_regs->addr_hi = ((dvma_vtob(dmabuf) & 0xff0000) >> 8);
465 udc_regs->addr_lo = (dvma_vtob(dmabuf) & 0xffff);
466#else
467 udc_regs->addr_hi = (((unsigned long)(addr) & 0xff0000) >> 8);
468 udc_regs->addr_lo = ((unsigned long)(addr) & 0xffff);
469#endif
470 udc_regs->count = count/2; /* count in words */
471 udc_regs->mode_hi = UDC_MODE_HIWORD;
472 if(write_flag) {
473 if(count & 1)
474 udc_regs->count++;
475 udc_regs->mode_lo = UDC_MODE_LSEND;
476 udc_regs->rsel = UDC_RSEL_SEND;
477 } else {
478 udc_regs->mode_lo = UDC_MODE_LRECV;
479 udc_regs->rsel = UDC_RSEL_RECV;
480 }
481
482 /* announce location of regs block */
483 sun3_udc_write(((dvma_vtob(udc_regs) & 0xff0000) >> 8),
484 UDC_CHN_HI);
485
486 sun3_udc_write((dvma_vtob(udc_regs) & 0xffff), UDC_CHN_LO);
487
488 /* set dma master on */
489 sun3_udc_write(0xd, UDC_MODE);
490
491 /* interrupt enable */
492 sun3_udc_write(UDC_INT_ENABLE, UDC_CSR);
493
494 return count;
495
496}
497
498static inline unsigned long sun3scsi_dma_count(struct Scsi_Host *instance)
499{
500 unsigned short resid;
501
502 dregs->udc_addr = 0x32;
503 udelay(SUN3_DMA_DELAY);
504 resid = dregs->udc_data;
505 udelay(SUN3_DMA_DELAY);
506 resid *= 2;
507
508 return (unsigned long) resid;
509}
510
511static inline unsigned long sun3scsi_dma_residual(struct Scsi_Host *instance)
512{
513 return last_residual;
514}
515
Henne811c9362006-10-03 19:51:59 +0200516static inline unsigned long sun3scsi_dma_xfer_len(unsigned long wanted,
517 struct scsi_cmnd *cmd,
518 int write_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200520 if (cmd->request->cmd_type == REQ_TYPE_FS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return wanted;
522 else
523 return 0;
524}
525
526static inline int sun3scsi_dma_start(unsigned long count, unsigned char *data)
527{
528
529 sun3_udc_write(UDC_CHN_START, UDC_CSR);
530
531 return 0;
532}
533
534/* clean up after our dma is done */
535static int sun3scsi_dma_finish(int write_flag)
536{
537 unsigned short count;
538 unsigned short fifo;
539 int ret = 0;
540
541 sun3_dma_active = 0;
542#if 1
543 // check to empty the fifo on a read
544 if(!write_flag) {
545 int tmo = 20000; /* .2 sec */
546
547 while(1) {
548 if(dregs->csr & CSR_FIFO_EMPTY)
549 break;
550
551 if(--tmo <= 0) {
552 printk("sun3scsi: fifo failed to empty!\n");
553 return 1;
554 }
555 udelay(10);
556 }
557 }
558
559#endif
560
561 count = sun3scsi_dma_count(default_instance);
562#ifdef OLDDMA
563
564 /* if we've finished a read, copy out the data we read */
565 if(sun3_dma_orig_addr) {
566 /* check for residual bytes after dma end */
567 if(count && (NCR5380_read(BUS_AND_STATUS_REG) &
568 (BASR_PHASE_MATCH | BASR_ACK))) {
569 printk("scsi%d: sun3_scsi_finish: read overrun baby... ", default_instance->host_no);
570 printk("basr now %02x\n", NCR5380_read(BUS_AND_STATUS_REG));
571 ret = count;
572 }
573
574 /* copy in what we dma'd no matter what */
575 memcpy(sun3_dma_orig_addr, dmabuf, sun3_dma_orig_count);
576 sun3_dma_orig_addr = NULL;
577
578 }
579#else
580
581 fifo = dregs->fifo_count;
582 last_residual = fifo;
583
584 /* empty bytes from the fifo which didn't make it */
585 if((!write_flag) && (count - fifo) == 2) {
586 unsigned short data;
587 unsigned char *vaddr;
588
589 data = dregs->fifo_data;
590 vaddr = (unsigned char *)dvma_btov(sun3_dma_orig_addr);
591
592 vaddr += (sun3_dma_orig_count - fifo);
593
594 vaddr[-2] = (data & 0xff00) >> 8;
595 vaddr[-1] = (data & 0xff);
596 }
597
598 dvma_unmap(sun3_dma_orig_addr);
599 sun3_dma_orig_addr = NULL;
600#endif
601 sun3_udc_write(UDC_RESET, UDC_CSR);
602 dregs->fifo_count = 0;
603 dregs->csr &= ~CSR_SEND;
604
605 /* reset fifo */
606 dregs->csr &= ~CSR_FIFO;
607 dregs->csr |= CSR_FIFO;
608
609 sun3_dma_setup_done = NULL;
610
611 return ret;
612
613}
614
615#include "sun3_NCR5380.c"
616
Christoph Hellwigd0be4a7d2005-10-31 18:31:40 +0100617static struct scsi_host_template driver_template = {
Geert Uytterhoeven9dcc26c2013-04-10 13:52:09 +0200618 .show_info = sun3scsi_show_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 .name = SUN3_SCSI_NAME,
620 .detect = sun3scsi_detect,
621 .release = sun3scsi_release,
622 .info = sun3scsi_info,
623 .queuecommand = sun3scsi_queue_command,
624 .eh_abort_handler = sun3scsi_abort,
625 .eh_bus_reset_handler = sun3scsi_bus_reset,
626 .can_queue = CAN_QUEUE,
627 .this_id = 7,
628 .sg_tablesize = SG_TABLESIZE,
629 .cmd_per_lun = CMD_PER_LUN,
630 .use_clustering = DISABLE_CLUSTERING
631};
632
633
634#include "scsi_module.c"
635
636MODULE_LICENSE("GPL");