GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / scsi / pcmcia / nsp_cs.c
blobd81a7e00f9082cb1e7424cf469c843035a36edc3
1 /*======================================================================
3 NinjaSCSI-3 / NinjaSCSI-32Bi PCMCIA SCSI host adapter card driver
4 By: YOKOTA Hiroshi <yokota@netlab.is.tsukuba.ac.jp>
6 Ver.2.8 Support 32bit MMIO mode
7 Support Synchronous Data Transfer Request (SDTR) mode
8 Ver.2.0 Support 32bit PIO mode
9 Ver.1.1.2 Fix for scatter list buffer exceeds
10 Ver.1.1 Support scatter list
11 Ver.0.1 Initial version
13 This software may be used and distributed according to the terms of
14 the GNU General Public License.
16 ======================================================================*/
18 /***********************************************************************
19 This driver is for these PCcards.
21 I-O DATA PCSC-F (Workbit NinjaSCSI-3)
22 "WBT", "NinjaSCSI-3", "R1.0"
23 I-O DATA CBSC-II (Workbit NinjaSCSI-32Bi in 16bit mode)
24 "IO DATA", "CBSC16 ", "1"
26 ***********************************************************************/
28 #include <linux/module.h>
29 #include <linux/kernel.h>
30 #include <linux/init.h>
31 #include <linux/slab.h>
32 #include <linux/string.h>
33 #include <linux/timer.h>
34 #include <linux/ioport.h>
35 #include <linux/delay.h>
36 #include <linux/interrupt.h>
37 #include <linux/major.h>
38 #include <linux/blkdev.h>
39 #include <linux/stat.h>
41 #include <asm/io.h>
42 #include <asm/irq.h>
44 #include <../drivers/scsi/scsi.h>
45 #include <scsi/scsi_host.h>
47 #include <scsi/scsi.h>
48 #include <scsi/scsi_ioctl.h>
50 #include <pcmcia/cs.h>
51 #include <pcmcia/cistpl.h>
52 #include <pcmcia/cisreg.h>
53 #include <pcmcia/ds.h>
55 #include "nsp_cs.h"
57 MODULE_AUTHOR("YOKOTA Hiroshi <yokota@netlab.is.tsukuba.ac.jp>");
58 MODULE_DESCRIPTION("WorkBit NinjaSCSI-3 / NinjaSCSI-32Bi(16bit) PCMCIA SCSI host adapter module");
59 MODULE_SUPPORTED_DEVICE("sd,sr,sg,st");
60 #ifdef MODULE_LICENSE
61 MODULE_LICENSE("GPL");
62 #endif
64 #include "nsp_io.h"
66 /*====================================================================*/
67 /* Parameters that can be set with 'insmod' */
69 static int nsp_burst_mode = BURST_MEM32;
70 module_param(nsp_burst_mode, int, 0);
71 MODULE_PARM_DESC(nsp_burst_mode, "Burst transfer mode (0=io8, 1=io32, 2=mem32(default))");
73 /* Release IO ports after configuration? */
74 static int free_ports = 0;
75 module_param(free_ports, bool, 0);
76 MODULE_PARM_DESC(free_ports, "Release IO ports after configuration? (default: 0 (=no))");
78 static struct scsi_host_template nsp_driver_template = {
79 .proc_name = "nsp_cs",
80 .proc_info = nsp_proc_info,
81 .name = "WorkBit NinjaSCSI-3/32Bi(16bit)",
82 .info = nsp_info,
83 .queuecommand = nsp_queuecommand,
84 /* .eh_abort_handler = nsp_eh_abort,*/
85 .eh_bus_reset_handler = nsp_eh_bus_reset,
86 .eh_host_reset_handler = nsp_eh_host_reset,
87 .can_queue = 1,
88 .this_id = NSP_INITIATOR_ID,
89 .sg_tablesize = SG_ALL,
90 .cmd_per_lun = 1,
91 .use_clustering = DISABLE_CLUSTERING,
94 static nsp_hw_data nsp_data_base; /* attach <-> detect glue */
99 * debug, error print
101 #ifndef NSP_DEBUG
102 # define NSP_DEBUG_MASK 0x000000
103 # define nsp_msg(type, args...) nsp_cs_message("", 0, (type), args)
104 # define nsp_dbg(mask, args...) /* */
105 #else
106 # define NSP_DEBUG_MASK 0xffffff
107 # define nsp_msg(type, args...) \
108 nsp_cs_message (__func__, __LINE__, (type), args)
109 # define nsp_dbg(mask, args...) \
110 nsp_cs_dmessage(__func__, __LINE__, (mask), args)
111 #endif
113 #define NSP_DEBUG_QUEUECOMMAND BIT(0)
114 #define NSP_DEBUG_REGISTER BIT(1)
115 #define NSP_DEBUG_AUTOSCSI BIT(2)
116 #define NSP_DEBUG_INTR BIT(3)
117 #define NSP_DEBUG_SGLIST BIT(4)
118 #define NSP_DEBUG_BUSFREE BIT(5)
119 #define NSP_DEBUG_CDB_CONTENTS BIT(6)
120 #define NSP_DEBUG_RESELECTION BIT(7)
121 #define NSP_DEBUG_MSGINOCCUR BIT(8)
122 #define NSP_DEBUG_EEPROM BIT(9)
123 #define NSP_DEBUG_MSGOUTOCCUR BIT(10)
124 #define NSP_DEBUG_BUSRESET BIT(11)
125 #define NSP_DEBUG_RESTART BIT(12)
126 #define NSP_DEBUG_SYNC BIT(13)
127 #define NSP_DEBUG_WAIT BIT(14)
128 #define NSP_DEBUG_TARGETFLAG BIT(15)
129 #define NSP_DEBUG_PROC BIT(16)
130 #define NSP_DEBUG_INIT BIT(17)
131 #define NSP_DEBUG_DATA_IO BIT(18)
132 #define NSP_SPECIAL_PRINT_REGISTER BIT(20)
134 #define NSP_DEBUG_BUF_LEN 150
136 static inline void nsp_inc_resid(struct scsi_cmnd *SCpnt, int residInc)
138 scsi_set_resid(SCpnt, scsi_get_resid(SCpnt) + residInc);
141 static void nsp_cs_message(const char *func, int line, char *type, char *fmt, ...)
143 va_list args;
144 char buf[NSP_DEBUG_BUF_LEN];
146 va_start(args, fmt);
147 vsnprintf(buf, sizeof(buf), fmt, args);
148 va_end(args);
150 #ifndef NSP_DEBUG
151 printk("%snsp_cs: %s\n", type, buf);
152 #else
153 printk("%snsp_cs: %s (%d): %s\n", type, func, line, buf);
154 #endif
157 #ifdef NSP_DEBUG
158 static void nsp_cs_dmessage(const char *func, int line, int mask, char *fmt, ...)
160 va_list args;
161 char buf[NSP_DEBUG_BUF_LEN];
163 va_start(args, fmt);
164 vsnprintf(buf, sizeof(buf), fmt, args);
165 va_end(args);
167 if (mask & NSP_DEBUG_MASK) {
168 printk("nsp_cs-debug: 0x%x %s (%d): %s\n", mask, func, line, buf);
171 #endif
173 /***********************************************************/
175 /*====================================================
176 * Clenaup parameters and call done() functions.
177 * You must be set SCpnt->result before call this function.
179 static void nsp_scsi_done(struct scsi_cmnd *SCpnt)
181 nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
183 data->CurrentSC = NULL;
185 SCpnt->scsi_done(SCpnt);
188 static int nsp_queuecommand(struct scsi_cmnd *SCpnt,
189 void (*done)(struct scsi_cmnd *))
191 #ifdef NSP_DEBUG
192 /*unsigned int host_id = SCpnt->device->host->this_id;*/
193 /*unsigned int base = SCpnt->device->host->io_port;*/
194 unsigned char target = scmd_id(SCpnt);
195 #endif
196 nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
198 nsp_dbg(NSP_DEBUG_QUEUECOMMAND,
199 "SCpnt=0x%p target=%d lun=%d sglist=0x%p bufflen=%d sg_count=%d",
200 SCpnt, target, SCpnt->device->lun, scsi_sglist(SCpnt),
201 scsi_bufflen(SCpnt), scsi_sg_count(SCpnt));
202 //nsp_dbg(NSP_DEBUG_QUEUECOMMAND, "before CurrentSC=0x%p", data->CurrentSC);
204 SCpnt->scsi_done = done;
206 if (data->CurrentSC != NULL) {
207 nsp_msg(KERN_DEBUG, "CurrentSC!=NULL this can't be happen");
208 SCpnt->result = DID_BAD_TARGET << 16;
209 nsp_scsi_done(SCpnt);
210 return 0;
214 show_command(SCpnt);
216 data->CurrentSC = SCpnt;
218 SCpnt->SCp.Status = CHECK_CONDITION;
219 SCpnt->SCp.Message = 0;
220 SCpnt->SCp.have_data_in = IO_UNKNOWN;
221 SCpnt->SCp.sent_command = 0;
222 SCpnt->SCp.phase = PH_UNDETERMINED;
223 scsi_set_resid(SCpnt, scsi_bufflen(SCpnt));
225 /* setup scratch area
226 SCp.ptr : buffer pointer
227 SCp.this_residual : buffer length
228 SCp.buffer : next buffer
229 SCp.buffers_residual : left buffers in list
230 SCp.phase : current state of the command */
231 if (scsi_bufflen(SCpnt)) {
232 SCpnt->SCp.buffer = scsi_sglist(SCpnt);
233 SCpnt->SCp.ptr = BUFFER_ADDR;
234 SCpnt->SCp.this_residual = SCpnt->SCp.buffer->length;
235 SCpnt->SCp.buffers_residual = scsi_sg_count(SCpnt) - 1;
236 } else {
237 SCpnt->SCp.ptr = NULL;
238 SCpnt->SCp.this_residual = 0;
239 SCpnt->SCp.buffer = NULL;
240 SCpnt->SCp.buffers_residual = 0;
243 if (nsphw_start_selection(SCpnt) == FALSE) {
244 nsp_dbg(NSP_DEBUG_QUEUECOMMAND, "selection fail");
245 SCpnt->result = DID_BUS_BUSY << 16;
246 nsp_scsi_done(SCpnt);
247 return 0;
251 //nsp_dbg(NSP_DEBUG_QUEUECOMMAND, "out");
252 #ifdef NSP_DEBUG
253 data->CmdId++;
254 #endif
255 return 0;
259 * setup PIO FIFO transfer mode and enable/disable to data out
261 static void nsp_setup_fifo(nsp_hw_data *data, int enabled)
263 unsigned int base = data->BaseAddress;
264 unsigned char transfer_mode_reg;
266 //nsp_dbg(NSP_DEBUG_DATA_IO, "enabled=%d", enabled);
268 if (enabled != FALSE) {
269 transfer_mode_reg = TRANSFER_GO | BRAIND;
270 } else {
271 transfer_mode_reg = 0;
274 transfer_mode_reg |= data->TransferMode;
276 nsp_index_write(base, TRANSFERMODE, transfer_mode_reg);
279 static void nsphw_init_sync(nsp_hw_data *data)
281 sync_data tmp_sync = { .SyncNegotiation = SYNC_NOT_YET,
282 .SyncPeriod = 0,
283 .SyncOffset = 0
285 int i;
287 /* setup sync data */
288 for ( i = 0; i < ARRAY_SIZE(data->Sync); i++ ) {
289 data->Sync[i] = tmp_sync;
294 * Initialize Ninja hardware
296 static int nsphw_init(nsp_hw_data *data)
298 unsigned int base = data->BaseAddress;
300 nsp_dbg(NSP_DEBUG_INIT, "in base=0x%x", base);
302 data->ScsiClockDiv = CLOCK_40M | FAST_20;
303 data->CurrentSC = NULL;
304 data->FifoCount = 0;
305 data->TransferMode = MODE_IO8;
307 nsphw_init_sync(data);
309 /* block all interrupts */
310 nsp_write(base, IRQCONTROL, IRQCONTROL_ALLMASK);
312 /* setup SCSI interface */
313 nsp_write(base, IFSELECT, IF_IFSEL);
315 nsp_index_write(base, SCSIIRQMODE, 0);
317 nsp_index_write(base, TRANSFERMODE, MODE_IO8);
318 nsp_index_write(base, CLOCKDIV, data->ScsiClockDiv);
320 nsp_index_write(base, PARITYCTRL, 0);
321 nsp_index_write(base, POINTERCLR, POINTER_CLEAR |
322 ACK_COUNTER_CLEAR |
323 REQ_COUNTER_CLEAR |
324 HOST_COUNTER_CLEAR);
326 /* setup fifo asic */
327 nsp_write(base, IFSELECT, IF_REGSEL);
328 nsp_index_write(base, TERMPWRCTRL, 0);
329 if ((nsp_index_read(base, OTHERCONTROL) & TPWR_SENSE) == 0) {
330 nsp_msg(KERN_INFO, "terminator power on");
331 nsp_index_write(base, TERMPWRCTRL, POWER_ON);
334 nsp_index_write(base, TIMERCOUNT, 0);
335 nsp_index_write(base, TIMERCOUNT, 0); /* requires 2 times!! */
337 nsp_index_write(base, SYNCREG, 0);
338 nsp_index_write(base, ACKWIDTH, 0);
340 /* enable interrupts and ack them */
341 nsp_index_write(base, SCSIIRQMODE, SCSI_PHASE_CHANGE_EI |
342 RESELECT_EI |
343 SCSI_RESET_IRQ_EI );
344 nsp_write(base, IRQCONTROL, IRQCONTROL_ALLCLEAR);
346 nsp_setup_fifo(data, FALSE);
348 return TRUE;
352 * Start selection phase
354 static int nsphw_start_selection(struct scsi_cmnd *SCpnt)
356 unsigned int host_id = SCpnt->device->host->this_id;
357 unsigned int base = SCpnt->device->host->io_port;
358 unsigned char target = scmd_id(SCpnt);
359 nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
360 int time_out;
361 unsigned char phase, arbit;
363 //nsp_dbg(NSP_DEBUG_RESELECTION, "in");
365 phase = nsp_index_read(base, SCSIBUSMON);
366 if(phase != BUSMON_BUS_FREE) {
367 //nsp_dbg(NSP_DEBUG_RESELECTION, "bus busy");
368 return FALSE;
371 /* start arbitration */
372 //nsp_dbg(NSP_DEBUG_RESELECTION, "start arbit");
373 SCpnt->SCp.phase = PH_ARBSTART;
374 nsp_index_write(base, SETARBIT, ARBIT_GO);
376 time_out = 1000;
377 do {
378 arbit = nsp_index_read(base, ARBITSTATUS);
379 //nsp_dbg(NSP_DEBUG_RESELECTION, "arbit=%d, wait_count=%d", arbit, wait_count);
380 udelay(1); /* hold 1.2us */
381 } while((arbit & (ARBIT_WIN | ARBIT_FAIL)) == 0 &&
382 (time_out-- != 0));
384 if (!(arbit & ARBIT_WIN)) {
385 //nsp_dbg(NSP_DEBUG_RESELECTION, "arbit fail");
386 nsp_index_write(base, SETARBIT, ARBIT_FLAG_CLEAR);
387 return FALSE;
390 /* assert select line */
391 //nsp_dbg(NSP_DEBUG_RESELECTION, "assert SEL line");
392 SCpnt->SCp.phase = PH_SELSTART;
393 udelay(3); /* wait 2.4us */
394 nsp_index_write(base, SCSIDATALATCH, BIT(host_id) | BIT(target));
395 nsp_index_write(base, SCSIBUSCTRL, SCSI_SEL | SCSI_BSY | SCSI_ATN);
396 udelay(2); /* wait >1.2us */
397 nsp_index_write(base, SCSIBUSCTRL, SCSI_SEL | SCSI_BSY | SCSI_DATAOUT_ENB | SCSI_ATN);
398 nsp_index_write(base, SETARBIT, ARBIT_FLAG_CLEAR);
399 /*udelay(1);*/ /* wait >90ns */
400 nsp_index_write(base, SCSIBUSCTRL, SCSI_SEL | SCSI_DATAOUT_ENB | SCSI_ATN);
402 /* check selection timeout */
403 nsp_start_timer(SCpnt, 1000/51);
404 data->SelectionTimeOut = 1;
406 return TRUE;
409 struct nsp_sync_table {
410 unsigned int min_period;
411 unsigned int max_period;
412 unsigned int chip_period;
413 unsigned int ack_width;
416 static struct nsp_sync_table nsp_sync_table_40M[] = {
417 {0x0c, 0x0c, 0x1, 0}, /* 20MB 50ns*/
418 {0x19, 0x19, 0x3, 1}, /* 10MB 100ns*/
419 {0x1a, 0x25, 0x5, 2}, /* 7.5MB 150ns*/
420 {0x26, 0x32, 0x7, 3}, /* 5MB 200ns*/
421 { 0, 0, 0, 0},
424 static struct nsp_sync_table nsp_sync_table_20M[] = {
425 {0x19, 0x19, 0x1, 0}, /* 10MB 100ns*/
426 {0x1a, 0x25, 0x2, 0}, /* 7.5MB 150ns*/
427 {0x26, 0x32, 0x3, 1}, /* 5MB 200ns*/
428 { 0, 0, 0, 0},
432 * setup synchronous data transfer mode
434 static int nsp_analyze_sdtr(struct scsi_cmnd *SCpnt)
436 unsigned char target = scmd_id(SCpnt);
437 // unsigned char lun = SCpnt->device->lun;
438 nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
439 sync_data *sync = &(data->Sync[target]);
440 struct nsp_sync_table *sync_table;
441 unsigned int period, offset;
442 int i;
445 nsp_dbg(NSP_DEBUG_SYNC, "in");
447 period = sync->SyncPeriod;
448 offset = sync->SyncOffset;
450 nsp_dbg(NSP_DEBUG_SYNC, "period=0x%x, offset=0x%x", period, offset);
452 if ((data->ScsiClockDiv & (BIT(0)|BIT(1))) == CLOCK_20M) {
453 sync_table = nsp_sync_table_20M;
454 } else {
455 sync_table = nsp_sync_table_40M;
458 for ( i = 0; sync_table->max_period != 0; i++, sync_table++) {
459 if ( period >= sync_table->min_period &&
460 period <= sync_table->max_period ) {
461 break;
465 if (period != 0 && sync_table->max_period == 0) {
467 * No proper period/offset found
469 nsp_dbg(NSP_DEBUG_SYNC, "no proper period/offset");
471 sync->SyncPeriod = 0;
472 sync->SyncOffset = 0;
473 sync->SyncRegister = 0;
474 sync->AckWidth = 0;
476 return FALSE;
479 sync->SyncRegister = (sync_table->chip_period << SYNCREG_PERIOD_SHIFT) |
480 (offset & SYNCREG_OFFSET_MASK);
481 sync->AckWidth = sync_table->ack_width;
483 nsp_dbg(NSP_DEBUG_SYNC, "sync_reg=0x%x, ack_width=0x%x", sync->SyncRegister, sync->AckWidth);
485 return TRUE;
490 * start ninja hardware timer
492 static void nsp_start_timer(struct scsi_cmnd *SCpnt, int time)
494 unsigned int base = SCpnt->device->host->io_port;
495 nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
497 //nsp_dbg(NSP_DEBUG_INTR, "in SCpnt=0x%p, time=%d", SCpnt, time);
498 data->TimerCount = time;
499 nsp_index_write(base, TIMERCOUNT, time);
503 * wait for bus phase change
505 static int nsp_negate_signal(struct scsi_cmnd *SCpnt, unsigned char mask,
506 char *str)
508 unsigned int base = SCpnt->device->host->io_port;
509 unsigned char reg;
510 int time_out;
512 //nsp_dbg(NSP_DEBUG_INTR, "in");
514 time_out = 100;
516 do {
517 reg = nsp_index_read(base, SCSIBUSMON);
518 if (reg == 0xff) {
519 break;
521 } while ((--time_out != 0) && (reg & mask) != 0);
523 if (time_out == 0) {
524 nsp_msg(KERN_DEBUG, " %s signal off timeut", str);
527 return 0;
531 * expect Ninja Irq
533 static int nsp_expect_signal(struct scsi_cmnd *SCpnt,
534 unsigned char current_phase,
535 unsigned char mask)
537 unsigned int base = SCpnt->device->host->io_port;
538 int time_out;
539 unsigned char phase, i_src;
541 //nsp_dbg(NSP_DEBUG_INTR, "current_phase=0x%x, mask=0x%x", current_phase, mask);
543 time_out = 100;
544 do {
545 phase = nsp_index_read(base, SCSIBUSMON);
546 if (phase == 0xff) {
547 //nsp_dbg(NSP_DEBUG_INTR, "ret -1");
548 return -1;
550 i_src = nsp_read(base, IRQSTATUS);
551 if (i_src & IRQSTATUS_SCSI) {
552 //nsp_dbg(NSP_DEBUG_INTR, "ret 0 found scsi signal");
553 return 0;
555 if ((phase & mask) != 0 && (phase & BUSMON_PHASE_MASK) == current_phase) {
556 //nsp_dbg(NSP_DEBUG_INTR, "ret 1 phase=0x%x", phase);
557 return 1;
559 } while(time_out-- != 0);
561 //nsp_dbg(NSP_DEBUG_INTR, "timeout");
562 return -1;
566 * transfer SCSI message
568 static int nsp_xfer(struct scsi_cmnd *SCpnt, int phase)
570 unsigned int base = SCpnt->device->host->io_port;
571 nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
572 char *buf = data->MsgBuffer;
573 int len = min(MSGBUF_SIZE, data->MsgLen);
574 int ptr;
575 int ret;
577 //nsp_dbg(NSP_DEBUG_DATA_IO, "in");
578 for (ptr = 0; len > 0; len--, ptr++) {
580 ret = nsp_expect_signal(SCpnt, phase, BUSMON_REQ);
581 if (ret <= 0) {
582 nsp_dbg(NSP_DEBUG_DATA_IO, "xfer quit");
583 return 0;
586 /* if last byte, negate ATN */
587 if (len == 1 && SCpnt->SCp.phase == PH_MSG_OUT) {
588 nsp_index_write(base, SCSIBUSCTRL, AUTODIRECTION | ACKENB);
591 /* read & write message */
592 if (phase & BUSMON_IO) {
593 nsp_dbg(NSP_DEBUG_DATA_IO, "read msg");
594 buf[ptr] = nsp_index_read(base, SCSIDATAWITHACK);
595 } else {
596 nsp_dbg(NSP_DEBUG_DATA_IO, "write msg");
597 nsp_index_write(base, SCSIDATAWITHACK, buf[ptr]);
599 nsp_negate_signal(SCpnt, BUSMON_ACK, "xfer<ack>");
602 return len;
606 * get extra SCSI data from fifo
608 static int nsp_dataphase_bypass(struct scsi_cmnd *SCpnt)
610 nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
611 unsigned int count;
613 //nsp_dbg(NSP_DEBUG_DATA_IO, "in");
615 if (SCpnt->SCp.have_data_in != IO_IN) {
616 return 0;
619 count = nsp_fifo_count(SCpnt);
620 if (data->FifoCount == count) {
621 //nsp_dbg(NSP_DEBUG_DATA_IO, "not use bypass quirk");
622 return 0;
625 nsp_dbg(NSP_DEBUG_DATA_IO, "use bypass quirk");
626 SCpnt->SCp.phase = PH_DATA;
627 nsp_pio_read(SCpnt);
628 nsp_setup_fifo(data, FALSE);
630 return 0;
634 * accept reselection
636 static int nsp_reselected(struct scsi_cmnd *SCpnt)
638 unsigned int base = SCpnt->device->host->io_port;
639 unsigned int host_id = SCpnt->device->host->this_id;
640 //nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
641 unsigned char bus_reg;
642 unsigned char id_reg, tmp;
643 int target;
645 nsp_dbg(NSP_DEBUG_RESELECTION, "in");
647 id_reg = nsp_index_read(base, RESELECTID);
648 tmp = id_reg & (~BIT(host_id));
649 target = 0;
650 while(tmp != 0) {
651 if (tmp & BIT(0)) {
652 break;
654 tmp >>= 1;
655 target++;
658 if (scmd_id(SCpnt) != target) {
659 nsp_msg(KERN_ERR, "XXX: reselect ID must be %d in this implementation.", target);
662 nsp_negate_signal(SCpnt, BUSMON_SEL, "reselect<SEL>");
664 nsp_nexus(SCpnt);
665 bus_reg = nsp_index_read(base, SCSIBUSCTRL) & ~(SCSI_BSY | SCSI_ATN);
666 nsp_index_write(base, SCSIBUSCTRL, bus_reg);
667 nsp_index_write(base, SCSIBUSCTRL, bus_reg | AUTODIRECTION | ACKENB);
669 return TRUE;
673 * count how many data transferd
675 static int nsp_fifo_count(struct scsi_cmnd *SCpnt)
677 unsigned int base = SCpnt->device->host->io_port;
678 unsigned int count;
679 unsigned int l, m, h, dummy;
681 nsp_index_write(base, POINTERCLR, POINTER_CLEAR | ACK_COUNTER);
683 l = nsp_index_read(base, TRANSFERCOUNT);
684 m = nsp_index_read(base, TRANSFERCOUNT);
685 h = nsp_index_read(base, TRANSFERCOUNT);
686 dummy = nsp_index_read(base, TRANSFERCOUNT); /* required this! */
688 count = (h << 16) | (m << 8) | (l << 0);
690 //nsp_dbg(NSP_DEBUG_DATA_IO, "count=0x%x", count);
692 return count;
695 /* fifo size */
696 #define RFIFO_CRIT 64
697 #define WFIFO_CRIT 64
700 * read data in DATA IN phase
702 static void nsp_pio_read(struct scsi_cmnd *SCpnt)
704 unsigned int base = SCpnt->device->host->io_port;
705 unsigned long mmio_base = SCpnt->device->host->base;
706 nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
707 long time_out;
708 int ocount, res;
709 unsigned char stat, fifo_stat;
711 ocount = data->FifoCount;
713 nsp_dbg(NSP_DEBUG_DATA_IO, "in SCpnt=0x%p resid=%d ocount=%d ptr=0x%p this_residual=%d buffers=0x%p nbuf=%d",
714 SCpnt, scsi_get_resid(SCpnt), ocount, SCpnt->SCp.ptr,
715 SCpnt->SCp.this_residual, SCpnt->SCp.buffer,
716 SCpnt->SCp.buffers_residual);
718 time_out = 1000;
720 while ((time_out-- != 0) &&
721 (SCpnt->SCp.this_residual > 0 || SCpnt->SCp.buffers_residual > 0 ) ) {
723 stat = nsp_index_read(base, SCSIBUSMON);
724 stat &= BUSMON_PHASE_MASK;
727 res = nsp_fifo_count(SCpnt) - ocount;
728 //nsp_dbg(NSP_DEBUG_DATA_IO, "ptr=0x%p this=0x%x ocount=0x%x res=0x%x", SCpnt->SCp.ptr, SCpnt->SCp.this_residual, ocount, res);
729 if (res == 0) { /* if some data avilable ? */
730 if (stat == BUSPHASE_DATA_IN) { /* phase changed? */
731 //nsp_dbg(NSP_DEBUG_DATA_IO, " wait for data this=%d", SCpnt->SCp.this_residual);
732 continue;
733 } else {
734 nsp_dbg(NSP_DEBUG_DATA_IO, "phase changed stat=0x%x", stat);
735 break;
739 fifo_stat = nsp_read(base, FIFOSTATUS);
740 if ((fifo_stat & FIFOSTATUS_FULL_EMPTY) == 0 &&
741 stat == BUSPHASE_DATA_IN) {
742 continue;
745 res = min(res, SCpnt->SCp.this_residual);
747 switch (data->TransferMode) {
748 case MODE_IO32:
749 res &= ~(BIT(1)|BIT(0)); /* align 4 */
750 nsp_fifo32_read(base, SCpnt->SCp.ptr, res >> 2);
751 break;
752 case MODE_IO8:
753 nsp_fifo8_read (base, SCpnt->SCp.ptr, res );
754 break;
756 case MODE_MEM32:
757 res &= ~(BIT(1)|BIT(0)); /* align 4 */
758 nsp_mmio_fifo32_read(mmio_base, SCpnt->SCp.ptr, res >> 2);
759 break;
761 default:
762 nsp_dbg(NSP_DEBUG_DATA_IO, "unknown read mode");
763 return;
766 nsp_inc_resid(SCpnt, -res);
767 SCpnt->SCp.ptr += res;
768 SCpnt->SCp.this_residual -= res;
769 ocount += res;
770 //nsp_dbg(NSP_DEBUG_DATA_IO, "ptr=0x%p this_residual=0x%x ocount=0x%x", SCpnt->SCp.ptr, SCpnt->SCp.this_residual, ocount);
772 /* go to next scatter list if available */
773 if (SCpnt->SCp.this_residual == 0 &&
774 SCpnt->SCp.buffers_residual != 0 ) {
775 //nsp_dbg(NSP_DEBUG_DATA_IO, "scatterlist next timeout=%d", time_out);
776 SCpnt->SCp.buffers_residual--;
777 SCpnt->SCp.buffer++;
778 SCpnt->SCp.ptr = BUFFER_ADDR;
779 SCpnt->SCp.this_residual = SCpnt->SCp.buffer->length;
780 time_out = 1000;
782 //nsp_dbg(NSP_DEBUG_DATA_IO, "page: 0x%p, off: 0x%x", SCpnt->SCp.buffer->page, SCpnt->SCp.buffer->offset);
786 data->FifoCount = ocount;
788 if (time_out < 0) {
789 nsp_msg(KERN_DEBUG, "pio read timeout resid=%d this_residual=%d buffers_residual=%d",
790 scsi_get_resid(SCpnt), SCpnt->SCp.this_residual,
791 SCpnt->SCp.buffers_residual);
793 nsp_dbg(NSP_DEBUG_DATA_IO, "read ocount=0x%x", ocount);
794 nsp_dbg(NSP_DEBUG_DATA_IO, "r cmd=%d resid=0x%x\n", data->CmdId,
795 scsi_get_resid(SCpnt));
799 * write data in DATA OUT phase
801 static void nsp_pio_write(struct scsi_cmnd *SCpnt)
803 unsigned int base = SCpnt->device->host->io_port;
804 unsigned long mmio_base = SCpnt->device->host->base;
805 nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
806 int time_out;
807 int ocount, res;
808 unsigned char stat;
810 ocount = data->FifoCount;
812 nsp_dbg(NSP_DEBUG_DATA_IO, "in fifocount=%d ptr=0x%p this_residual=%d buffers=0x%p nbuf=%d resid=0x%x",
813 data->FifoCount, SCpnt->SCp.ptr, SCpnt->SCp.this_residual,
814 SCpnt->SCp.buffer, SCpnt->SCp.buffers_residual,
815 scsi_get_resid(SCpnt));
817 time_out = 1000;
819 while ((time_out-- != 0) &&
820 (SCpnt->SCp.this_residual > 0 || SCpnt->SCp.buffers_residual > 0)) {
821 stat = nsp_index_read(base, SCSIBUSMON);
822 stat &= BUSMON_PHASE_MASK;
824 if (stat != BUSPHASE_DATA_OUT) {
825 res = ocount - nsp_fifo_count(SCpnt);
827 nsp_dbg(NSP_DEBUG_DATA_IO, "phase changed stat=0x%x, res=%d\n", stat, res);
828 /* Put back pointer */
829 nsp_inc_resid(SCpnt, res);
830 SCpnt->SCp.ptr -= res;
831 SCpnt->SCp.this_residual += res;
832 ocount -= res;
834 break;
837 res = ocount - nsp_fifo_count(SCpnt);
838 if (res > 0) { /* write all data? */
839 nsp_dbg(NSP_DEBUG_DATA_IO, "wait for all data out. ocount=0x%x res=%d", ocount, res);
840 continue;
843 res = min(SCpnt->SCp.this_residual, WFIFO_CRIT);
845 //nsp_dbg(NSP_DEBUG_DATA_IO, "ptr=0x%p this=0x%x res=0x%x", SCpnt->SCp.ptr, SCpnt->SCp.this_residual, res);
846 switch (data->TransferMode) {
847 case MODE_IO32:
848 res &= ~(BIT(1)|BIT(0)); /* align 4 */
849 nsp_fifo32_write(base, SCpnt->SCp.ptr, res >> 2);
850 break;
851 case MODE_IO8:
852 nsp_fifo8_write (base, SCpnt->SCp.ptr, res );
853 break;
855 case MODE_MEM32:
856 res &= ~(BIT(1)|BIT(0)); /* align 4 */
857 nsp_mmio_fifo32_write(mmio_base, SCpnt->SCp.ptr, res >> 2);
858 break;
860 default:
861 nsp_dbg(NSP_DEBUG_DATA_IO, "unknown write mode");
862 break;
865 nsp_inc_resid(SCpnt, -res);
866 SCpnt->SCp.ptr += res;
867 SCpnt->SCp.this_residual -= res;
868 ocount += res;
870 /* go to next scatter list if available */
871 if (SCpnt->SCp.this_residual == 0 &&
872 SCpnt->SCp.buffers_residual != 0 ) {
873 //nsp_dbg(NSP_DEBUG_DATA_IO, "scatterlist next");
874 SCpnt->SCp.buffers_residual--;
875 SCpnt->SCp.buffer++;
876 SCpnt->SCp.ptr = BUFFER_ADDR;
877 SCpnt->SCp.this_residual = SCpnt->SCp.buffer->length;
878 time_out = 1000;
882 data->FifoCount = ocount;
884 if (time_out < 0) {
885 nsp_msg(KERN_DEBUG, "pio write timeout resid=0x%x",
886 scsi_get_resid(SCpnt));
888 nsp_dbg(NSP_DEBUG_DATA_IO, "write ocount=0x%x", ocount);
889 nsp_dbg(NSP_DEBUG_DATA_IO, "w cmd=%d resid=0x%x\n", data->CmdId,
890 scsi_get_resid(SCpnt));
892 #undef RFIFO_CRIT
893 #undef WFIFO_CRIT
896 * setup synchronous/asynchronous data transfer mode
898 static int nsp_nexus(struct scsi_cmnd *SCpnt)
900 unsigned int base = SCpnt->device->host->io_port;
901 unsigned char target = scmd_id(SCpnt);
902 // unsigned char lun = SCpnt->device->lun;
903 nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
904 sync_data *sync = &(data->Sync[target]);
906 //nsp_dbg(NSP_DEBUG_DATA_IO, "in SCpnt=0x%p", SCpnt);
908 /* setup synch transfer registers */
909 nsp_index_write(base, SYNCREG, sync->SyncRegister);
910 nsp_index_write(base, ACKWIDTH, sync->AckWidth);
912 if (scsi_get_resid(SCpnt) % 4 != 0 ||
913 scsi_get_resid(SCpnt) <= PAGE_SIZE ) {
914 data->TransferMode = MODE_IO8;
915 } else if (nsp_burst_mode == BURST_MEM32) {
916 data->TransferMode = MODE_MEM32;
917 } else if (nsp_burst_mode == BURST_IO32) {
918 data->TransferMode = MODE_IO32;
919 } else {
920 data->TransferMode = MODE_IO8;
923 /* setup pdma fifo */
924 nsp_setup_fifo(data, TRUE);
926 /* clear ack counter */
927 data->FifoCount = 0;
928 nsp_index_write(base, POINTERCLR, POINTER_CLEAR |
929 ACK_COUNTER_CLEAR |
930 REQ_COUNTER_CLEAR |
931 HOST_COUNTER_CLEAR);
933 return 0;
936 #include "nsp_message.c"
938 * interrupt handler
940 static irqreturn_t nspintr(int irq, void *dev_id)
942 unsigned int base;
943 unsigned char irq_status, irq_phase, phase;
944 struct scsi_cmnd *tmpSC;
945 unsigned char target, lun;
946 unsigned int *sync_neg;
947 int i, tmp;
948 nsp_hw_data *data;
951 //nsp_dbg(NSP_DEBUG_INTR, "dev_id=0x%p", dev_id);
952 //nsp_dbg(NSP_DEBUG_INTR, "host=0x%p", ((scsi_info_t *)dev_id)->host);
954 if ( dev_id != NULL &&
955 ((scsi_info_t *)dev_id)->host != NULL ) {
956 scsi_info_t *info = (scsi_info_t *)dev_id;
958 data = (nsp_hw_data *)info->host->hostdata;
959 } else {
960 nsp_dbg(NSP_DEBUG_INTR, "host data wrong");
961 return IRQ_NONE;
964 //nsp_dbg(NSP_DEBUG_INTR, "&nsp_data_base=0x%p, dev_id=0x%p", &nsp_data_base, dev_id);
966 base = data->BaseAddress;
967 //nsp_dbg(NSP_DEBUG_INTR, "base=0x%x", base);
970 * interrupt check
972 nsp_write(base, IRQCONTROL, IRQCONTROL_IRQDISABLE);
973 irq_status = nsp_read(base, IRQSTATUS);
974 //nsp_dbg(NSP_DEBUG_INTR, "irq_status=0x%x", irq_status);
975 if ((irq_status == 0xff) || ((irq_status & IRQSTATUS_MASK) == 0)) {
976 nsp_write(base, IRQCONTROL, 0);
977 //nsp_dbg(NSP_DEBUG_INTR, "no irq/shared irq");
978 return IRQ_NONE;
981 phase = nsp_index_read(base, SCSIBUSMON);
982 if((irq_status & IRQSTATUS_SCSI) != 0) {
983 irq_phase = nsp_index_read(base, IRQPHASESENCE);
984 } else {
985 irq_phase = 0;
988 //nsp_dbg(NSP_DEBUG_INTR, "irq_phase=0x%x", irq_phase);
991 * timer interrupt handler (scsi vs timer interrupts)
993 //nsp_dbg(NSP_DEBUG_INTR, "timercount=%d", data->TimerCount);
994 if (data->TimerCount != 0) {
995 //nsp_dbg(NSP_DEBUG_INTR, "stop timer");
996 nsp_index_write(base, TIMERCOUNT, 0);
997 nsp_index_write(base, TIMERCOUNT, 0);
998 data->TimerCount = 0;
1001 if ((irq_status & IRQSTATUS_MASK) == IRQSTATUS_TIMER &&
1002 data->SelectionTimeOut == 0) {
1003 //nsp_dbg(NSP_DEBUG_INTR, "timer start");
1004 nsp_write(base, IRQCONTROL, IRQCONTROL_TIMER_CLEAR);
1005 return IRQ_HANDLED;
1008 nsp_write(base, IRQCONTROL, IRQCONTROL_TIMER_CLEAR | IRQCONTROL_FIFO_CLEAR);
1010 if ((irq_status & IRQSTATUS_SCSI) &&
1011 (irq_phase & SCSI_RESET_IRQ)) {
1012 nsp_msg(KERN_ERR, "bus reset (power off?)");
1014 nsphw_init(data);
1015 nsp_bus_reset(data);
1017 if(data->CurrentSC != NULL) {
1018 tmpSC = data->CurrentSC;
1019 tmpSC->result = (DID_RESET << 16) |
1020 ((tmpSC->SCp.Message & 0xff) << 8) |
1021 ((tmpSC->SCp.Status & 0xff) << 0);
1022 nsp_scsi_done(tmpSC);
1024 return IRQ_HANDLED;
1027 if (data->CurrentSC == NULL) {
1028 nsp_msg(KERN_ERR, "CurrentSC==NULL irq_status=0x%x phase=0x%x irq_phase=0x%x this can't be happen. reset everything", irq_status, phase, irq_phase);
1029 nsphw_init(data);
1030 nsp_bus_reset(data);
1031 return IRQ_HANDLED;
1034 tmpSC = data->CurrentSC;
1035 target = tmpSC->device->id;
1036 lun = tmpSC->device->lun;
1037 sync_neg = &(data->Sync[target].SyncNegotiation);
1040 * parse hardware SCSI irq reasons register
1042 if (irq_status & IRQSTATUS_SCSI) {
1043 if (irq_phase & RESELECT_IRQ) {
1044 nsp_dbg(NSP_DEBUG_INTR, "reselect");
1045 nsp_write(base, IRQCONTROL, IRQCONTROL_RESELECT_CLEAR);
1046 if (nsp_reselected(tmpSC) != FALSE) {
1047 return IRQ_HANDLED;
1051 if ((irq_phase & (PHASE_CHANGE_IRQ | LATCHED_BUS_FREE)) == 0) {
1052 return IRQ_HANDLED;
1056 //show_phase(tmpSC);
1058 switch(tmpSC->SCp.phase) {
1059 case PH_SELSTART:
1060 // *sync_neg = SYNC_NOT_YET;
1061 if ((phase & BUSMON_BSY) == 0) {
1062 //nsp_dbg(NSP_DEBUG_INTR, "selection count=%d", data->SelectionTimeOut);
1063 if (data->SelectionTimeOut >= NSP_SELTIMEOUT) {
1064 nsp_dbg(NSP_DEBUG_INTR, "selection time out");
1065 data->SelectionTimeOut = 0;
1066 nsp_index_write(base, SCSIBUSCTRL, 0);
1068 tmpSC->result = DID_TIME_OUT << 16;
1069 nsp_scsi_done(tmpSC);
1071 return IRQ_HANDLED;
1073 data->SelectionTimeOut += 1;
1074 nsp_start_timer(tmpSC, 1000/51);
1075 return IRQ_HANDLED;
1078 /* attention assert */
1079 //nsp_dbg(NSP_DEBUG_INTR, "attention assert");
1080 data->SelectionTimeOut = 0;
1081 tmpSC->SCp.phase = PH_SELECTED;
1082 nsp_index_write(base, SCSIBUSCTRL, SCSI_ATN);
1083 udelay(1);
1084 nsp_index_write(base, SCSIBUSCTRL, SCSI_ATN | AUTODIRECTION | ACKENB);
1085 return IRQ_HANDLED;
1087 break;
1089 case PH_RESELECT:
1090 //nsp_dbg(NSP_DEBUG_INTR, "phase reselect");
1091 // *sync_neg = SYNC_NOT_YET;
1092 if ((phase & BUSMON_PHASE_MASK) != BUSPHASE_MESSAGE_IN) {
1094 tmpSC->result = DID_ABORT << 16;
1095 nsp_scsi_done(tmpSC);
1096 return IRQ_HANDLED;
1098 /* fall thru */
1099 default:
1100 if ((irq_status & (IRQSTATUS_SCSI | IRQSTATUS_FIFO)) == 0) {
1101 return IRQ_HANDLED;
1103 break;
1107 * SCSI sequencer
1109 //nsp_dbg(NSP_DEBUG_INTR, "start scsi seq");
1111 /* normal disconnect */
1112 if (((tmpSC->SCp.phase == PH_MSG_IN) || (tmpSC->SCp.phase == PH_MSG_OUT)) &&
1113 (irq_phase & LATCHED_BUS_FREE) != 0 ) {
1114 nsp_dbg(NSP_DEBUG_INTR, "normal disconnect irq_status=0x%x, phase=0x%x, irq_phase=0x%x", irq_status, phase, irq_phase);
1116 //*sync_neg = SYNC_NOT_YET;
1118 if ((tmpSC->SCp.Message == MSG_COMMAND_COMPLETE)) { /* all command complete and return status */
1119 tmpSC->result = (DID_OK << 16) |
1120 ((tmpSC->SCp.Message & 0xff) << 8) |
1121 ((tmpSC->SCp.Status & 0xff) << 0);
1122 nsp_dbg(NSP_DEBUG_INTR, "command complete result=0x%x", tmpSC->result);
1123 nsp_scsi_done(tmpSC);
1125 return IRQ_HANDLED;
1128 return IRQ_HANDLED;
1132 /* check unexpected bus free state */
1133 if (phase == 0) {
1134 nsp_msg(KERN_DEBUG, "unexpected bus free. irq_status=0x%x, phase=0x%x, irq_phase=0x%x", irq_status, phase, irq_phase);
1136 *sync_neg = SYNC_NG;
1137 tmpSC->result = DID_ERROR << 16;
1138 nsp_scsi_done(tmpSC);
1139 return IRQ_HANDLED;
1142 switch (phase & BUSMON_PHASE_MASK) {
1143 case BUSPHASE_COMMAND:
1144 nsp_dbg(NSP_DEBUG_INTR, "BUSPHASE_COMMAND");
1145 if ((phase & BUSMON_REQ) == 0) {
1146 nsp_dbg(NSP_DEBUG_INTR, "REQ == 0");
1147 return IRQ_HANDLED;
1150 tmpSC->SCp.phase = PH_COMMAND;
1152 nsp_nexus(tmpSC);
1154 /* write scsi command */
1155 nsp_dbg(NSP_DEBUG_INTR, "cmd_len=%d", tmpSC->cmd_len);
1156 nsp_index_write(base, COMMANDCTRL, CLEAR_COMMAND_POINTER);
1157 for (i = 0; i < tmpSC->cmd_len; i++) {
1158 nsp_index_write(base, COMMANDDATA, tmpSC->cmnd[i]);
1160 nsp_index_write(base, COMMANDCTRL, CLEAR_COMMAND_POINTER | AUTO_COMMAND_GO);
1161 break;
1163 case BUSPHASE_DATA_OUT:
1164 nsp_dbg(NSP_DEBUG_INTR, "BUSPHASE_DATA_OUT");
1166 tmpSC->SCp.phase = PH_DATA;
1167 tmpSC->SCp.have_data_in = IO_OUT;
1169 nsp_pio_write(tmpSC);
1171 break;
1173 case BUSPHASE_DATA_IN:
1174 nsp_dbg(NSP_DEBUG_INTR, "BUSPHASE_DATA_IN");
1176 tmpSC->SCp.phase = PH_DATA;
1177 tmpSC->SCp.have_data_in = IO_IN;
1179 nsp_pio_read(tmpSC);
1181 break;
1183 case BUSPHASE_STATUS:
1184 nsp_dataphase_bypass(tmpSC);
1185 nsp_dbg(NSP_DEBUG_INTR, "BUSPHASE_STATUS");
1187 tmpSC->SCp.phase = PH_STATUS;
1189 tmpSC->SCp.Status = nsp_index_read(base, SCSIDATAWITHACK);
1190 nsp_dbg(NSP_DEBUG_INTR, "message=0x%x status=0x%x", tmpSC->SCp.Message, tmpSC->SCp.Status);
1192 break;
1194 case BUSPHASE_MESSAGE_OUT:
1195 nsp_dbg(NSP_DEBUG_INTR, "BUSPHASE_MESSAGE_OUT");
1196 if ((phase & BUSMON_REQ) == 0) {
1197 goto timer_out;
1200 tmpSC->SCp.phase = PH_MSG_OUT;
1202 //*sync_neg = SYNC_NOT_YET;
1204 data->MsgLen = i = 0;
1205 data->MsgBuffer[i] = IDENTIFY(TRUE, lun); i++;
1207 if (*sync_neg == SYNC_NOT_YET) {
1208 data->Sync[target].SyncPeriod = 0;
1209 data->Sync[target].SyncOffset = 0;
1211 /**/
1212 data->MsgBuffer[i] = MSG_EXTENDED; i++;
1213 data->MsgBuffer[i] = 3; i++;
1214 data->MsgBuffer[i] = MSG_EXT_SDTR; i++;
1215 data->MsgBuffer[i] = 0x0c; i++;
1216 data->MsgBuffer[i] = 15; i++;
1217 /**/
1219 data->MsgLen = i;
1221 nsp_analyze_sdtr(tmpSC);
1222 show_message(data);
1223 nsp_message_out(tmpSC);
1224 break;
1226 case BUSPHASE_MESSAGE_IN:
1227 nsp_dataphase_bypass(tmpSC);
1228 nsp_dbg(NSP_DEBUG_INTR, "BUSPHASE_MESSAGE_IN");
1229 if ((phase & BUSMON_REQ) == 0) {
1230 goto timer_out;
1233 tmpSC->SCp.phase = PH_MSG_IN;
1234 nsp_message_in(tmpSC);
1236 /**/
1237 if (*sync_neg == SYNC_NOT_YET) {
1238 //nsp_dbg(NSP_DEBUG_INTR, "sync target=%d,lun=%d",target,lun);
1240 if (data->MsgLen >= 5 &&
1241 data->MsgBuffer[0] == MSG_EXTENDED &&
1242 data->MsgBuffer[1] == 3 &&
1243 data->MsgBuffer[2] == MSG_EXT_SDTR ) {
1244 data->Sync[target].SyncPeriod = data->MsgBuffer[3];
1245 data->Sync[target].SyncOffset = data->MsgBuffer[4];
1246 //nsp_dbg(NSP_DEBUG_INTR, "sync ok, %d %d", data->MsgBuffer[3], data->MsgBuffer[4]);
1247 *sync_neg = SYNC_OK;
1248 } else {
1249 data->Sync[target].SyncPeriod = 0;
1250 data->Sync[target].SyncOffset = 0;
1251 *sync_neg = SYNC_NG;
1253 nsp_analyze_sdtr(tmpSC);
1255 /**/
1257 /* search last messeage byte */
1258 tmp = -1;
1259 for (i = 0; i < data->MsgLen; i++) {
1260 tmp = data->MsgBuffer[i];
1261 if (data->MsgBuffer[i] == MSG_EXTENDED) {
1262 i += (1 + data->MsgBuffer[i+1]);
1265 tmpSC->SCp.Message = tmp;
1267 nsp_dbg(NSP_DEBUG_INTR, "message=0x%x len=%d", tmpSC->SCp.Message, data->MsgLen);
1268 show_message(data);
1270 break;
1272 case BUSPHASE_SELECT:
1273 default:
1274 nsp_dbg(NSP_DEBUG_INTR, "BUSPHASE other");
1276 break;
1279 //nsp_dbg(NSP_DEBUG_INTR, "out");
1280 return IRQ_HANDLED;
1282 timer_out:
1283 nsp_start_timer(tmpSC, 1000/102);
1284 return IRQ_HANDLED;
1287 #ifdef NSP_DEBUG
1288 #include "nsp_debug.c"
1289 #endif /* NSP_DEBUG */
1291 /*----------------------------------------------------------------*/
1292 /* look for ninja3 card and init if found */
1293 /*----------------------------------------------------------------*/
1294 static struct Scsi_Host *nsp_detect(struct scsi_host_template *sht)
1296 struct Scsi_Host *host; /* registered host structure */
1297 nsp_hw_data *data_b = &nsp_data_base, *data;
1299 nsp_dbg(NSP_DEBUG_INIT, "this_id=%d", sht->this_id);
1300 host = scsi_host_alloc(&nsp_driver_template, sizeof(nsp_hw_data));
1301 if (host == NULL) {
1302 nsp_dbg(NSP_DEBUG_INIT, "host failed");
1303 return NULL;
1306 memcpy(host->hostdata, data_b, sizeof(nsp_hw_data));
1307 data = (nsp_hw_data *)host->hostdata;
1308 data->ScsiInfo->host = host;
1309 #ifdef NSP_DEBUG
1310 data->CmdId = 0;
1311 #endif
1313 nsp_dbg(NSP_DEBUG_INIT, "irq=%d,%d", data_b->IrqNumber, ((nsp_hw_data *)host->hostdata)->IrqNumber);
1315 host->unique_id = data->BaseAddress;
1316 host->io_port = data->BaseAddress;
1317 host->n_io_port = data->NumAddress;
1318 host->irq = data->IrqNumber;
1319 host->base = data->MmioAddress;
1321 spin_lock_init(&(data->Lock));
1323 snprintf(data->nspinfo,
1324 sizeof(data->nspinfo),
1325 "NinjaSCSI-3/32Bi Driver $Revision: 1.23 $ IO:0x%04lx-0x%04lx MMIO(virt addr):0x%04lx IRQ:%02d",
1326 host->io_port, host->io_port + host->n_io_port - 1,
1327 host->base,
1328 host->irq);
1329 sht->name = data->nspinfo;
1331 nsp_dbg(NSP_DEBUG_INIT, "end");
1334 return host; /* detect done. */
1337 /*----------------------------------------------------------------*/
1338 /* return info string */
1339 /*----------------------------------------------------------------*/
1340 static const char *nsp_info(struct Scsi_Host *shpnt)
1342 nsp_hw_data *data = (nsp_hw_data *)shpnt->hostdata;
1344 return data->nspinfo;
1347 #undef SPRINTF
1348 #define SPRINTF(args...) \
1349 do { \
1350 if(length > (pos - buffer)) { \
1351 pos += snprintf(pos, length - (pos - buffer) + 1, ## args); \
1352 nsp_dbg(NSP_DEBUG_PROC, "buffer=0x%p pos=0x%p length=%d %d\n", buffer, pos, length, length - (pos - buffer));\
1354 } while(0)
1356 static int nsp_proc_info(struct Scsi_Host *host, char *buffer, char **start,
1357 off_t offset, int length, int inout)
1359 int id;
1360 char *pos = buffer;
1361 int thislength;
1362 int speed;
1363 unsigned long flags;
1364 nsp_hw_data *data;
1365 int hostno;
1367 if (inout) {
1368 return -EINVAL;
1371 hostno = host->host_no;
1372 data = (nsp_hw_data *)host->hostdata;
1375 SPRINTF("NinjaSCSI status\n\n");
1376 SPRINTF("Driver version: $Revision: 1.23 $\n");
1377 SPRINTF("SCSI host No.: %d\n", hostno);
1378 SPRINTF("IRQ: %d\n", host->irq);
1379 SPRINTF("IO: 0x%lx-0x%lx\n", host->io_port, host->io_port + host->n_io_port - 1);
1380 SPRINTF("MMIO(virtual address): 0x%lx-0x%lx\n", host->base, host->base + data->MmioLength - 1);
1381 SPRINTF("sg_tablesize: %d\n", host->sg_tablesize);
1383 SPRINTF("burst transfer mode: ");
1384 switch (nsp_burst_mode) {
1385 case BURST_IO8:
1386 SPRINTF("io8");
1387 break;
1388 case BURST_IO32:
1389 SPRINTF("io32");
1390 break;
1391 case BURST_MEM32:
1392 SPRINTF("mem32");
1393 break;
1394 default:
1395 SPRINTF("???");
1396 break;
1398 SPRINTF("\n");
1401 spin_lock_irqsave(&(data->Lock), flags);
1402 SPRINTF("CurrentSC: 0x%p\n\n", data->CurrentSC);
1403 spin_unlock_irqrestore(&(data->Lock), flags);
1405 SPRINTF("SDTR status\n");
1406 for(id = 0; id < ARRAY_SIZE(data->Sync); id++) {
1408 SPRINTF("id %d: ", id);
1410 if (id == host->this_id) {
1411 SPRINTF("----- NinjaSCSI-3 host adapter\n");
1412 continue;
1415 switch(data->Sync[id].SyncNegotiation) {
1416 case SYNC_OK:
1417 SPRINTF(" sync");
1418 break;
1419 case SYNC_NG:
1420 SPRINTF("async");
1421 break;
1422 case SYNC_NOT_YET:
1423 SPRINTF(" none");
1424 break;
1425 default:
1426 SPRINTF("?????");
1427 break;
1430 if (data->Sync[id].SyncPeriod != 0) {
1431 speed = 1000000 / (data->Sync[id].SyncPeriod * 4);
1433 SPRINTF(" transfer %d.%dMB/s, offset %d",
1434 speed / 1000,
1435 speed % 1000,
1436 data->Sync[id].SyncOffset
1439 SPRINTF("\n");
1442 thislength = pos - (buffer + offset);
1444 if(thislength < 0) {
1445 *start = NULL;
1446 return 0;
1450 thislength = min(thislength, length);
1451 *start = buffer + offset;
1453 return thislength;
1455 #undef SPRINTF
1457 /*---------------------------------------------------------------*/
1458 /* error handler */
1459 /*---------------------------------------------------------------*/
1462 static int nsp_eh_abort(struct scsi_cmnd *SCpnt)
1464 nsp_dbg(NSP_DEBUG_BUSRESET, "SCpnt=0x%p", SCpnt);
1466 return nsp_eh_bus_reset(SCpnt);
1469 static int nsp_bus_reset(nsp_hw_data *data)
1471 unsigned int base = data->BaseAddress;
1472 int i;
1474 nsp_write(base, IRQCONTROL, IRQCONTROL_ALLMASK);
1476 nsp_index_write(base, SCSIBUSCTRL, SCSI_RST);
1477 mdelay(100); /* 100ms */
1478 nsp_index_write(base, SCSIBUSCTRL, 0);
1479 for(i = 0; i < 5; i++) {
1480 nsp_index_read(base, IRQPHASESENCE); /* dummy read */
1483 nsphw_init_sync(data);
1485 nsp_write(base, IRQCONTROL, IRQCONTROL_ALLCLEAR);
1487 return SUCCESS;
1490 static int nsp_eh_bus_reset(struct scsi_cmnd *SCpnt)
1492 nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
1494 nsp_dbg(NSP_DEBUG_BUSRESET, "SCpnt=0x%p", SCpnt);
1496 return nsp_bus_reset(data);
1499 static int nsp_eh_host_reset(struct scsi_cmnd *SCpnt)
1501 nsp_hw_data *data = (nsp_hw_data *)SCpnt->device->host->hostdata;
1503 nsp_dbg(NSP_DEBUG_BUSRESET, "in");
1505 nsphw_init(data);
1507 return SUCCESS;
1511 /**********************************************************************
1512 PCMCIA functions
1513 **********************************************************************/
1515 /*======================================================================
1516 nsp_cs_attach() creates an "instance" of the driver, allocating
1517 local data structures for one device. The device is registered
1518 with Card Services.
1520 The dev_link structure is initialized, but we don't actually
1521 configure the card at this point -- we wait until we receive a
1522 card insertion event.
1523 ======================================================================*/
1524 static int nsp_cs_probe(struct pcmcia_device *link)
1526 scsi_info_t *info;
1527 nsp_hw_data *data = &nsp_data_base;
1528 int ret;
1530 nsp_dbg(NSP_DEBUG_INIT, "in");
1532 /* Create new SCSI device */
1533 info = kzalloc(sizeof(*info), GFP_KERNEL);
1534 if (info == NULL) { return -ENOMEM; }
1535 info->p_dev = link;
1536 link->priv = info;
1537 data->ScsiInfo = info;
1539 nsp_dbg(NSP_DEBUG_INIT, "info=0x%p", info);
1541 /* The io structure describes IO port mapping */
1542 link->resource[0]->end = 0x10;
1543 link->resource[0]->flags = IO_DATA_PATH_WIDTH_AUTO;
1545 /* General socket configuration */
1546 link->conf.Attributes = CONF_ENABLE_IRQ;
1547 link->conf.IntType = INT_MEMORY_AND_IO;
1549 ret = nsp_cs_config(link);
1551 nsp_dbg(NSP_DEBUG_INIT, "link=0x%p", link);
1552 return ret;
1553 } /* nsp_cs_attach */
1556 /*======================================================================
1557 This deletes a driver "instance". The device is de-registered
1558 with Card Services. If it has been released, all local data
1559 structures are freed. Otherwise, the structures will be freed
1560 when the device is released.
1561 ======================================================================*/
1562 static void nsp_cs_detach(struct pcmcia_device *link)
1564 nsp_dbg(NSP_DEBUG_INIT, "in, link=0x%p", link);
1566 ((scsi_info_t *)link->priv)->stop = 1;
1567 nsp_cs_release(link);
1569 kfree(link->priv);
1570 link->priv = NULL;
1571 } /* nsp_cs_detach */
1574 /*======================================================================
1575 nsp_cs_config() is scheduled to run after a CARD_INSERTION event
1576 is received, to configure the PCMCIA socket, and to make the
1577 ethernet device available to the system.
1578 ======================================================================*/
1580 struct nsp_cs_configdata {
1581 nsp_hw_data *data;
1582 win_req_t req;
1585 static int nsp_cs_config_check(struct pcmcia_device *p_dev,
1586 cistpl_cftable_entry_t *cfg,
1587 cistpl_cftable_entry_t *dflt,
1588 unsigned int vcc,
1589 void *priv_data)
1591 struct nsp_cs_configdata *cfg_mem = priv_data;
1593 if (cfg->index == 0)
1594 return -ENODEV;
1596 /* Does this card need audio output? */
1597 if (cfg->flags & CISTPL_CFTABLE_AUDIO) {
1598 p_dev->conf.Attributes |= CONF_ENABLE_SPKR;
1599 p_dev->conf.Status = CCSR_AUDIO_ENA;
1602 /* Use power settings for Vcc and Vpp if present */
1603 /* Note that the CIS values need to be rescaled */
1604 if (cfg->vcc.present & (1<<CISTPL_POWER_VNOM)) {
1605 if (vcc != cfg->vcc.param[CISTPL_POWER_VNOM]/10000)
1606 return -ENODEV;
1607 else if (dflt->vcc.present & (1<<CISTPL_POWER_VNOM)) {
1608 if (vcc != dflt->vcc.param[CISTPL_POWER_VNOM]/10000)
1609 return -ENODEV;
1612 if (cfg->vpp1.present & (1 << CISTPL_POWER_VNOM)) {
1613 p_dev->conf.Vpp =
1614 cfg->vpp1.param[CISTPL_POWER_VNOM] / 10000;
1615 } else if (dflt->vpp1.present & (1 << CISTPL_POWER_VNOM)) {
1616 p_dev->conf.Vpp =
1617 dflt->vpp1.param[CISTPL_POWER_VNOM] / 10000;
1620 /* Do we need to allocate an interrupt? */
1621 p_dev->conf.Attributes |= CONF_ENABLE_IRQ;
1623 /* IO window settings */
1624 p_dev->resource[0]->end = p_dev->resource[1]->end = 0;
1625 if ((cfg->io.nwin > 0) || (dflt->io.nwin > 0)) {
1626 cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt->io;
1627 p_dev->io_lines = io->flags & CISTPL_IO_LINES_MASK;
1628 p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
1629 p_dev->resource[0]->flags |=
1630 pcmcia_io_cfg_data_width(io->flags);
1631 p_dev->resource[0]->start = io->win[0].base;
1632 p_dev->resource[0]->end = io->win[0].len;
1633 if (io->nwin > 1) {
1634 p_dev->resource[1]->flags =
1635 p_dev->resource[0]->flags;
1636 p_dev->resource[1]->start = io->win[1].base;
1637 p_dev->resource[1]->end = io->win[1].len;
1639 /* This reserves IO space but doesn't actually enable it */
1640 if (pcmcia_request_io(p_dev) != 0)
1641 goto next_entry;
1644 if ((cfg->mem.nwin > 0) || (dflt->mem.nwin > 0)) {
1645 cistpl_mem_t *mem =
1646 (cfg->mem.nwin) ? &cfg->mem : &dflt->mem;
1647 cfg_mem->req.Attributes = WIN_DATA_WIDTH_16|WIN_MEMORY_TYPE_CM;
1648 cfg_mem->req.Attributes |= WIN_ENABLE;
1649 cfg_mem->req.Base = mem->win[0].host_addr;
1650 cfg_mem->req.Size = mem->win[0].len;
1651 if (cfg_mem->req.Size < 0x1000)
1652 cfg_mem->req.Size = 0x1000;
1653 cfg_mem->req.AccessSpeed = 0;
1654 if (pcmcia_request_window(p_dev, &cfg_mem->req, &p_dev->win) != 0)
1655 goto next_entry;
1656 if (pcmcia_map_mem_page(p_dev, p_dev->win,
1657 mem->win[0].card_addr) != 0)
1658 goto next_entry;
1660 cfg_mem->data->MmioAddress = (unsigned long) ioremap_nocache(cfg_mem->req.Base, cfg_mem->req.Size);
1661 cfg_mem->data->MmioLength = cfg_mem->req.Size;
1663 /* If we got this far, we're cool! */
1664 return 0;
1667 next_entry:
1668 nsp_dbg(NSP_DEBUG_INIT, "next");
1669 pcmcia_disable_device(p_dev);
1670 return -ENODEV;
1673 static int nsp_cs_config(struct pcmcia_device *link)
1675 int ret;
1676 scsi_info_t *info = link->priv;
1677 struct nsp_cs_configdata *cfg_mem;
1678 struct Scsi_Host *host;
1679 nsp_hw_data *data = &nsp_data_base;
1681 nsp_dbg(NSP_DEBUG_INIT, "in");
1683 cfg_mem = kzalloc(sizeof(*cfg_mem), GFP_KERNEL);
1684 if (!cfg_mem)
1685 return -ENOMEM;
1686 cfg_mem->data = data;
1688 ret = pcmcia_loop_config(link, nsp_cs_config_check, cfg_mem);
1689 if (ret)
1690 goto cs_failed;
1692 if (pcmcia_request_irq(link, nspintr))
1693 goto cs_failed;
1695 ret = pcmcia_request_configuration(link, &link->conf);
1696 if (ret)
1697 goto cs_failed;
1699 if (free_ports) {
1700 if (link->resource[0]) {
1701 release_region(link->resource[0]->start,
1702 resource_size(link->resource[0]));
1704 if (link->resource[1]) {
1705 release_region(link->resource[1]->start,
1706 resource_size(link->resource[1]));
1710 /* Set port and IRQ */
1711 data->BaseAddress = link->resource[0]->start;
1712 data->NumAddress = resource_size(link->resource[0]);
1713 data->IrqNumber = link->irq;
1715 nsp_dbg(NSP_DEBUG_INIT, "I/O[0x%x+0x%x] IRQ %d",
1716 data->BaseAddress, data->NumAddress, data->IrqNumber);
1718 if(nsphw_init(data) == FALSE) {
1719 goto cs_failed;
1722 host = nsp_detect(&nsp_driver_template);
1724 if (host == NULL) {
1725 nsp_dbg(NSP_DEBUG_INIT, "detect failed");
1726 goto cs_failed;
1730 ret = scsi_add_host (host, NULL);
1731 if (ret)
1732 goto cs_failed;
1734 scsi_scan_host(host);
1736 info->host = host;
1738 /* Finally, report what we've done */
1739 printk(KERN_INFO "nsp_cs: index 0x%02x: ",
1740 link->conf.ConfigIndex);
1741 if (link->conf.Vpp) {
1742 printk(", Vpp %d.%d", link->conf.Vpp/10, link->conf.Vpp%10);
1744 if (link->conf.Attributes & CONF_ENABLE_IRQ) {
1745 printk(", irq %d", link->irq);
1747 if (link->resource[0])
1748 printk(", io %pR", link->resource[0]);
1749 if (link->resource[1])
1750 printk(" & %pR", link->resource[1]);
1751 if (link->win)
1752 printk(", mem 0x%06lx-0x%06lx", cfg_mem->req.Base,
1753 cfg_mem->req.Base+cfg_mem->req.Size-1);
1754 printk("\n");
1756 kfree(cfg_mem);
1757 return 0;
1759 cs_failed:
1760 nsp_dbg(NSP_DEBUG_INIT, "config fail");
1761 nsp_cs_release(link);
1762 kfree(cfg_mem);
1764 return -ENODEV;
1765 } /* nsp_cs_config */
1768 /*======================================================================
1769 After a card is removed, nsp_cs_release() will unregister the net
1770 device, and release the PCMCIA configuration. If the device is
1771 still open, this will be postponed until it is closed.
1772 ======================================================================*/
1773 static void nsp_cs_release(struct pcmcia_device *link)
1775 scsi_info_t *info = link->priv;
1776 nsp_hw_data *data = NULL;
1778 if (info->host == NULL) {
1779 nsp_msg(KERN_DEBUG, "unexpected card release call.");
1780 } else {
1781 data = (nsp_hw_data *)info->host->hostdata;
1784 nsp_dbg(NSP_DEBUG_INIT, "link=0x%p", link);
1786 /* Unlink the device chain */
1787 if (info->host != NULL) {
1788 scsi_remove_host(info->host);
1791 if (link->win) {
1792 if (data != NULL) {
1793 iounmap((void *)(data->MmioAddress));
1796 pcmcia_disable_device(link);
1798 if (info->host != NULL) {
1799 scsi_host_put(info->host);
1801 } /* nsp_cs_release */
1803 static int nsp_cs_suspend(struct pcmcia_device *link)
1805 scsi_info_t *info = link->priv;
1806 nsp_hw_data *data;
1808 nsp_dbg(NSP_DEBUG_INIT, "event: suspend");
1810 if (info->host != NULL) {
1811 nsp_msg(KERN_INFO, "clear SDTR status");
1813 data = (nsp_hw_data *)info->host->hostdata;
1815 nsphw_init_sync(data);
1818 info->stop = 1;
1820 return 0;
1823 static int nsp_cs_resume(struct pcmcia_device *link)
1825 scsi_info_t *info = link->priv;
1826 nsp_hw_data *data;
1828 nsp_dbg(NSP_DEBUG_INIT, "event: resume");
1830 info->stop = 0;
1832 if (info->host != NULL) {
1833 nsp_msg(KERN_INFO, "reset host and bus");
1835 data = (nsp_hw_data *)info->host->hostdata;
1837 nsphw_init (data);
1838 nsp_bus_reset(data);
1841 return 0;
1844 /*======================================================================*
1845 * module entry point
1846 *====================================================================*/
1847 static struct pcmcia_device_id nsp_cs_ids[] = {
1848 PCMCIA_DEVICE_PROD_ID123("IO DATA", "CBSC16 ", "1", 0x547e66dc, 0x0d63a3fd, 0x51de003a),
1849 PCMCIA_DEVICE_PROD_ID123("KME ", "SCSI-CARD-001", "1", 0x534c02bc, 0x52008408, 0x51de003a),
1850 PCMCIA_DEVICE_PROD_ID123("KME ", "SCSI-CARD-002", "1", 0x534c02bc, 0xcb09d5b2, 0x51de003a),
1851 PCMCIA_DEVICE_PROD_ID123("KME ", "SCSI-CARD-003", "1", 0x534c02bc, 0xbc0ee524, 0x51de003a),
1852 PCMCIA_DEVICE_PROD_ID123("KME ", "SCSI-CARD-004", "1", 0x534c02bc, 0x226a7087, 0x51de003a),
1853 PCMCIA_DEVICE_PROD_ID123("WBT", "NinjaSCSI-3", "R1.0", 0xc7ba805f, 0xfdc7c97d, 0x6973710e),
1854 PCMCIA_DEVICE_PROD_ID123("WORKBIT", "UltraNinja-16", "1", 0x28191418, 0xb70f4b09, 0x51de003a),
1855 PCMCIA_DEVICE_NULL
1857 MODULE_DEVICE_TABLE(pcmcia, nsp_cs_ids);
1859 static struct pcmcia_driver nsp_driver = {
1860 .owner = THIS_MODULE,
1861 .drv = {
1862 .name = "nsp_cs",
1864 .probe = nsp_cs_probe,
1865 .remove = nsp_cs_detach,
1866 .id_table = nsp_cs_ids,
1867 .suspend = nsp_cs_suspend,
1868 .resume = nsp_cs_resume,
1871 static int __init nsp_cs_init(void)
1873 nsp_msg(KERN_INFO, "loading...");
1875 return pcmcia_register_driver(&nsp_driver);
1878 static void __exit nsp_cs_exit(void)
1880 nsp_msg(KERN_INFO, "unloading...");
1881 pcmcia_unregister_driver(&nsp_driver);
1885 module_init(nsp_cs_init)
1886 module_exit(nsp_cs_exit)
1888 /* end */