blob: 020d38aed4f5e0624ce804c497edaeeb8392c263 [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
Geert Uytterhoeven2ba51ea22011-06-13 20:39:21 +020068#define NDEBUG 0
69
70#define NDEBUG_ABORT 0x00100000
71#define NDEBUG_TAGS 0x00200000
72#define NDEBUG_MERGING 0x00400000
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074/* dma on! */
75#define REAL_DMA
76
77#include "scsi.h"
78#include <scsi/scsi_host.h>
79#include "sun3_scsi.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Linus Torvalds1da177e2005-04-16 15:20:36 -070081/* #define OLDDMA */
82
83#define USE_WRAPPER
84/*#define RESET_BOOT */
85#define DRIVER_SETUP
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087/*
88 * BUG can be used to trigger a strange code-size related hang on 2.1 kernels
89 */
90#ifdef BUG
91#undef RESET_BOOT
92#undef DRIVER_SETUP
93#endif
94
95/* #define SUPPORT_TAGS */
96
97#define ENABLE_IRQ() enable_irq( IRQ_SUN3_SCSI );
98
99
David Howells7d12e782006-10-05 14:55:46 +0100100static irqreturn_t scsi_sun3_intr(int irq, void *dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101static inline unsigned char sun3scsi_read(int reg);
102static inline void sun3scsi_write(int reg, int value);
103
104static int setup_can_queue = -1;
105module_param(setup_can_queue, int, 0);
106static int setup_cmd_per_lun = -1;
107module_param(setup_cmd_per_lun, int, 0);
108static int setup_sg_tablesize = -1;
109module_param(setup_sg_tablesize, int, 0);
110#ifdef SUPPORT_TAGS
111static int setup_use_tagged_queuing = -1;
112module_param(setup_use_tagged_queuing, int, 0);
113#endif
114static int setup_hostid = -1;
115module_param(setup_hostid, int, 0);
116
Henne811c9362006-10-03 19:51:59 +0200117static struct scsi_cmnd *sun3_dma_setup_done = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Michael Schmitz2b0f8342014-05-02 20:43:01 +1200119#define RESET_RUN_DONE
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#define AFTER_RESET_DELAY (HZ/2)
122
123/* ms to wait after hitting dma regs */
124#define SUN3_DMA_DELAY 10
125
126/* dvma buffer to allocate -- 32k should hopefully be more than sufficient */
127#define SUN3_DVMA_BUFSIZE 0xe000
128
129/* minimum number of bytes to do dma on */
130#define SUN3_DMA_MINSIZE 128
131
132static volatile unsigned char *sun3_scsi_regp;
133static volatile struct sun3_dma_regs *dregs;
134#ifdef OLDDMA
135static unsigned char *dmabuf = NULL; /* dma memory buffer */
136#endif
137static struct sun3_udc_regs *udc_regs = NULL;
138static unsigned char *sun3_dma_orig_addr = NULL;
139static unsigned long sun3_dma_orig_count = 0;
140static int sun3_dma_active = 0;
141static unsigned long last_residual = 0;
142
143/*
144 * NCR 5380 register access functions
145 */
146
147static inline unsigned char sun3scsi_read(int reg)
148{
149 return( sun3_scsi_regp[reg] );
150}
151
152static inline void sun3scsi_write(int reg, int value)
153{
154 sun3_scsi_regp[reg] = value;
155}
156
157/* dma controller register access functions */
158
159static inline unsigned short sun3_udc_read(unsigned char reg)
160{
161 unsigned short ret;
162
163 dregs->udc_addr = UDC_CSR;
164 udelay(SUN3_DMA_DELAY);
165 ret = dregs->udc_data;
166 udelay(SUN3_DMA_DELAY);
167
168 return ret;
169}
170
171static inline void sun3_udc_write(unsigned short val, unsigned char reg)
172{
173 dregs->udc_addr = reg;
174 udelay(SUN3_DMA_DELAY);
175 dregs->udc_data = val;
176 udelay(SUN3_DMA_DELAY);
177}
178
179/*
180 * XXX: status debug
181 */
182static struct Scsi_Host *default_instance;
183
184/*
Christoph Hellwigd0be4a7d2005-10-31 18:31:40 +0100185 * Function : int sun3scsi_detect(struct scsi_host_template * tpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 *
187 * Purpose : initializes mac NCR5380 driver based on the
188 * command line / compile time port and irq definitions.
189 *
190 * Inputs : tpnt - template for this SCSI adapter.
191 *
192 * Returns : 1 if a host adapter was found, 0 if not.
193 *
194 */
195
Geert Uytterhoevend559c492011-06-13 20:39:18 +0200196int __init sun3scsi_detect(struct scsi_host_template * tpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
198 unsigned long ioaddr;
199 static int called = 0;
200 struct Scsi_Host *instance;
201
202 /* check that this machine has an onboard 5380 */
203 switch(idprom->id_machtype) {
204 case SM_SUN3|SM_3_50:
205 case SM_SUN3|SM_3_60:
206 break;
207
208 default:
209 return 0;
210 }
211
212 if(called)
213 return 0;
214
215 tpnt->proc_name = "Sun3 5380 SCSI";
216
217 /* setup variables */
218 tpnt->can_queue =
219 (setup_can_queue > 0) ? setup_can_queue : CAN_QUEUE;
220 tpnt->cmd_per_lun =
221 (setup_cmd_per_lun > 0) ? setup_cmd_per_lun : CMD_PER_LUN;
222 tpnt->sg_tablesize =
223 (setup_sg_tablesize >= 0) ? setup_sg_tablesize : SG_TABLESIZE;
224
225 if (setup_hostid >= 0)
226 tpnt->this_id = setup_hostid;
227 else {
228 /* use 7 as default */
229 tpnt->this_id = 7;
230 }
231
232 ioaddr = (unsigned long)ioremap(IOBASE_SUN3_SCSI, PAGE_SIZE);
233 sun3_scsi_regp = (unsigned char *)ioaddr;
234
235 dregs = (struct sun3_dma_regs *)(((unsigned char *)ioaddr) + 8);
236
237 if((udc_regs = dvma_malloc(sizeof(struct sun3_udc_regs)))
238 == NULL) {
239 printk("SUN3 Scsi couldn't allocate DVMA memory!\n");
240 return 0;
241 }
242#ifdef OLDDMA
243 if((dmabuf = dvma_malloc_align(SUN3_DVMA_BUFSIZE, 0x10000)) == NULL) {
244 printk("SUN3 Scsi couldn't allocate DVMA memory!\n");
245 return 0;
246 }
247#endif
248#ifdef SUPPORT_TAGS
249 if (setup_use_tagged_queuing < 0)
250 setup_use_tagged_queuing = USE_TAGGED_QUEUING;
251#endif
252
253 instance = scsi_register (tpnt, sizeof(struct NCR5380_hostdata));
254 if(instance == NULL)
255 return 0;
256
257 default_instance = instance;
258
259 instance->io_port = (unsigned long) ioaddr;
260 instance->irq = IRQ_SUN3_SCSI;
261
262 NCR5380_init(instance, 0);
263
264 instance->n_io_port = 32;
265
266 ((struct NCR5380_hostdata *)instance->hostdata)->ctrl = 0;
267
268 if (request_irq(instance->irq, scsi_sun3_intr,
Jeff Garzik1e641662007-11-11 19:52:05 -0500269 0, "Sun3SCSI-5380", instance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270#ifndef REAL_DMA
271 printk("scsi%d: IRQ%d not free, interrupts disabled\n",
272 instance->host_no, instance->irq);
273 instance->irq = SCSI_IRQ_NONE;
274#else
275 printk("scsi%d: IRQ%d not free, bailing out\n",
276 instance->host_no, instance->irq);
277 return 0;
278#endif
279 }
280
281 printk("scsi%d: Sun3 5380 at port %lX irq", instance->host_no, instance->io_port);
282 if (instance->irq == SCSI_IRQ_NONE)
283 printk ("s disabled");
284 else
285 printk (" %d", instance->irq);
286 printk(" options CAN_QUEUE=%d CMD_PER_LUN=%d release=%d",
287 instance->can_queue, instance->cmd_per_lun,
288 SUN3SCSI_PUBLIC_RELEASE);
289 printk("\nscsi%d:", instance->host_no);
290 NCR5380_print_options(instance);
291 printk("\n");
292
293 dregs->csr = 0;
294 udelay(SUN3_DMA_DELAY);
295 dregs->csr = CSR_SCSI | CSR_FIFO | CSR_INTR;
296 udelay(SUN3_DMA_DELAY);
297 dregs->fifo_count = 0;
298
299 called = 1;
300
301#ifdef RESET_BOOT
302 sun3_scsi_reset_boot(instance);
303#endif
304
305 return 1;
306}
307
308int sun3scsi_release (struct Scsi_Host *shpnt)
309{
310 if (shpnt->irq != SCSI_IRQ_NONE)
Jeff Garzik1e641662007-11-11 19:52:05 -0500311 free_irq(shpnt->irq, shpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 iounmap((void *)sun3_scsi_regp);
314
Geert Uytterhoeven19b6c512011-06-13 20:39:19 +0200315 NCR5380_exit(shpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return 0;
317}
318
319#ifdef RESET_BOOT
320/*
321 * Our 'bus reset on boot' function
322 */
323
324static void sun3_scsi_reset_boot(struct Scsi_Host *instance)
325{
326 unsigned long end;
327
328 NCR5380_local_declare();
329 NCR5380_setup(instance);
330
331 /*
332 * Do a SCSI reset to clean up the bus during initialization. No
333 * messing with the queues, interrupts, or locks necessary here.
334 */
335
336 printk( "Sun3 SCSI: resetting the SCSI bus..." );
337
338 /* switch off SCSI IRQ - catch an interrupt without IRQ bit set else */
339// sun3_disable_irq( IRQ_SUN3_SCSI );
340
341 /* get in phase */
342 NCR5380_write( TARGET_COMMAND_REG,
343 PHASE_SR_TO_TCR( NCR5380_read(STATUS_REG) ));
344
345 /* assert RST */
346 NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST );
347
348 /* The min. reset hold time is 25us, so 40us should be enough */
349 udelay( 50 );
350
351 /* reset RST and interrupt */
352 NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE );
353 NCR5380_read( RESET_PARITY_INTERRUPT_REG );
354
355 for( end = jiffies + AFTER_RESET_DELAY; time_before(jiffies, end); )
356 barrier();
357
358 /* switch on SCSI IRQ again */
359// sun3_enable_irq( IRQ_SUN3_SCSI );
360
361 printk( " done\n" );
362}
363#endif
364
365const char * sun3scsi_info (struct Scsi_Host *spnt) {
366 return "";
367}
368
369// safe bits for the CSR
370#define CSR_GOOD 0x060f
371
David Howells7d12e782006-10-05 14:55:46 +0100372static irqreturn_t scsi_sun3_intr(int irq, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
374 unsigned short csr = dregs->csr;
375 int handled = 0;
376
377 if(csr & ~CSR_GOOD) {
378 if(csr & CSR_DMA_BUSERR) {
379 printk("scsi%d: bus error in dma\n", default_instance->host_no);
380 }
381
382 if(csr & CSR_DMA_CONFLICT) {
383 printk("scsi%d: dma conflict\n", default_instance->host_no);
384 }
385 handled = 1;
386 }
387
388 if(csr & (CSR_SDB_INT | CSR_DMA_INT)) {
David Howells7d12e782006-10-05 14:55:46 +0100389 NCR5380_intr(irq, dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 handled = 1;
391 }
392
393 return IRQ_RETVAL(handled);
394}
395
396/*
397 * Debug stuff - to be called on NMI, or sysrq key. Use at your own risk;
398 * reentering NCR5380_print_status seems to have ugly side effects
399 */
400
401/* this doesn't seem to get used at all -- sam */
402#if 0
403void sun3_sun3_debug (void)
404{
405 unsigned long flags;
406 NCR5380_local_declare();
407
408 if (default_instance) {
409 local_irq_save(flags);
410 NCR5380_print_status(default_instance);
411 local_irq_restore(flags);
412 }
413}
414#endif
415
416
417/* sun3scsi_dma_setup() -- initialize the dma controller for a read/write */
418static unsigned long sun3scsi_dma_setup(void *data, unsigned long count, int write_flag)
419{
420#ifdef OLDDMA
421 if(write_flag)
422 memcpy(dmabuf, data, count);
423 else {
424 sun3_dma_orig_addr = data;
425 sun3_dma_orig_count = count;
426 }
427#else
428 void *addr;
429
430 if(sun3_dma_orig_addr != NULL)
431 dvma_unmap(sun3_dma_orig_addr);
432
433// addr = sun3_dvma_page((unsigned long)data, (unsigned long)dmabuf);
434 addr = (void *)dvma_map((unsigned long) data, count);
435
436 sun3_dma_orig_addr = addr;
437 sun3_dma_orig_count = count;
438#endif
439 dregs->fifo_count = 0;
440 sun3_udc_write(UDC_RESET, UDC_CSR);
441
442 /* reset fifo */
443 dregs->csr &= ~CSR_FIFO;
444 dregs->csr |= CSR_FIFO;
445
446 /* set direction */
447 if(write_flag)
448 dregs->csr |= CSR_SEND;
449 else
450 dregs->csr &= ~CSR_SEND;
451
452 /* byte count for fifo */
453 dregs->fifo_count = count;
454
455 sun3_udc_write(UDC_RESET, UDC_CSR);
456
457 /* reset fifo */
458 dregs->csr &= ~CSR_FIFO;
459 dregs->csr |= CSR_FIFO;
460
461 if(dregs->fifo_count != count) {
462 printk("scsi%d: fifo_mismatch %04x not %04x\n",
463 default_instance->host_no, dregs->fifo_count,
464 (unsigned int) count);
Finn Thaind614f062014-03-18 11:42:16 +1100465 NCR5380_dprint(NDEBUG_DMA, default_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
467
468 /* setup udc */
469#ifdef OLDDMA
470 udc_regs->addr_hi = ((dvma_vtob(dmabuf) & 0xff0000) >> 8);
471 udc_regs->addr_lo = (dvma_vtob(dmabuf) & 0xffff);
472#else
473 udc_regs->addr_hi = (((unsigned long)(addr) & 0xff0000) >> 8);
474 udc_regs->addr_lo = ((unsigned long)(addr) & 0xffff);
475#endif
476 udc_regs->count = count/2; /* count in words */
477 udc_regs->mode_hi = UDC_MODE_HIWORD;
478 if(write_flag) {
479 if(count & 1)
480 udc_regs->count++;
481 udc_regs->mode_lo = UDC_MODE_LSEND;
482 udc_regs->rsel = UDC_RSEL_SEND;
483 } else {
484 udc_regs->mode_lo = UDC_MODE_LRECV;
485 udc_regs->rsel = UDC_RSEL_RECV;
486 }
487
488 /* announce location of regs block */
489 sun3_udc_write(((dvma_vtob(udc_regs) & 0xff0000) >> 8),
490 UDC_CHN_HI);
491
492 sun3_udc_write((dvma_vtob(udc_regs) & 0xffff), UDC_CHN_LO);
493
494 /* set dma master on */
495 sun3_udc_write(0xd, UDC_MODE);
496
497 /* interrupt enable */
498 sun3_udc_write(UDC_INT_ENABLE, UDC_CSR);
499
500 return count;
501
502}
503
504static inline unsigned long sun3scsi_dma_count(struct Scsi_Host *instance)
505{
506 unsigned short resid;
507
508 dregs->udc_addr = 0x32;
509 udelay(SUN3_DMA_DELAY);
510 resid = dregs->udc_data;
511 udelay(SUN3_DMA_DELAY);
512 resid *= 2;
513
514 return (unsigned long) resid;
515}
516
517static inline unsigned long sun3scsi_dma_residual(struct Scsi_Host *instance)
518{
519 return last_residual;
520}
521
Henne811c9362006-10-03 19:51:59 +0200522static inline unsigned long sun3scsi_dma_xfer_len(unsigned long wanted,
523 struct scsi_cmnd *cmd,
524 int write_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200526 if (cmd->request->cmd_type == REQ_TYPE_FS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 return wanted;
528 else
529 return 0;
530}
531
532static inline int sun3scsi_dma_start(unsigned long count, unsigned char *data)
533{
534
535 sun3_udc_write(UDC_CHN_START, UDC_CSR);
536
537 return 0;
538}
539
540/* clean up after our dma is done */
541static int sun3scsi_dma_finish(int write_flag)
542{
543 unsigned short count;
544 unsigned short fifo;
545 int ret = 0;
546
547 sun3_dma_active = 0;
548#if 1
549 // check to empty the fifo on a read
550 if(!write_flag) {
551 int tmo = 20000; /* .2 sec */
552
553 while(1) {
554 if(dregs->csr & CSR_FIFO_EMPTY)
555 break;
556
557 if(--tmo <= 0) {
558 printk("sun3scsi: fifo failed to empty!\n");
559 return 1;
560 }
561 udelay(10);
562 }
563 }
564
565#endif
566
567 count = sun3scsi_dma_count(default_instance);
568#ifdef OLDDMA
569
570 /* if we've finished a read, copy out the data we read */
571 if(sun3_dma_orig_addr) {
572 /* check for residual bytes after dma end */
573 if(count && (NCR5380_read(BUS_AND_STATUS_REG) &
574 (BASR_PHASE_MATCH | BASR_ACK))) {
575 printk("scsi%d: sun3_scsi_finish: read overrun baby... ", default_instance->host_no);
576 printk("basr now %02x\n", NCR5380_read(BUS_AND_STATUS_REG));
577 ret = count;
578 }
579
580 /* copy in what we dma'd no matter what */
581 memcpy(sun3_dma_orig_addr, dmabuf, sun3_dma_orig_count);
582 sun3_dma_orig_addr = NULL;
583
584 }
585#else
586
587 fifo = dregs->fifo_count;
588 last_residual = fifo;
589
590 /* empty bytes from the fifo which didn't make it */
591 if((!write_flag) && (count - fifo) == 2) {
592 unsigned short data;
593 unsigned char *vaddr;
594
595 data = dregs->fifo_data;
596 vaddr = (unsigned char *)dvma_btov(sun3_dma_orig_addr);
597
598 vaddr += (sun3_dma_orig_count - fifo);
599
600 vaddr[-2] = (data & 0xff00) >> 8;
601 vaddr[-1] = (data & 0xff);
602 }
603
604 dvma_unmap(sun3_dma_orig_addr);
605 sun3_dma_orig_addr = NULL;
606#endif
607 sun3_udc_write(UDC_RESET, UDC_CSR);
608 dregs->fifo_count = 0;
609 dregs->csr &= ~CSR_SEND;
610
611 /* reset fifo */
612 dregs->csr &= ~CSR_FIFO;
613 dregs->csr |= CSR_FIFO;
614
615 sun3_dma_setup_done = NULL;
616
617 return ret;
618
619}
620
621#include "sun3_NCR5380.c"
622
Christoph Hellwigd0be4a7d2005-10-31 18:31:40 +0100623static struct scsi_host_template driver_template = {
Geert Uytterhoeven9dcc26c2013-04-10 13:52:09 +0200624 .show_info = sun3scsi_show_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 .name = SUN3_SCSI_NAME,
626 .detect = sun3scsi_detect,
627 .release = sun3scsi_release,
628 .info = sun3scsi_info,
629 .queuecommand = sun3scsi_queue_command,
630 .eh_abort_handler = sun3scsi_abort,
631 .eh_bus_reset_handler = sun3scsi_bus_reset,
632 .can_queue = CAN_QUEUE,
633 .this_id = 7,
634 .sg_tablesize = SG_TABLESIZE,
635 .cmd_per_lun = CMD_PER_LUN,
636 .use_clustering = DISABLE_CLUSTERING
637};
638
639
640#include "scsi_module.c"
641
642MODULE_LICENSE("GPL");