1 /* $NecBSD: nsp.c,v 1.21.12.6 2001/06/29 06:27:52 honda Exp $ */
6 #define NSP_IO_CONTROL_FLAGS \
7 (NSP_READ_SUSPEND_IO | NSP_WRITE_SUSPEND_IO | \
8 NSP_READ_FIFO_INTERRUPTS | NSP_WRITE_FIFO_INTERRUPTS | \
9 NSP_USE_MEMIO | NSP_WAIT_FOR_SELECT)
12 * Copyright (c) 1998, 1999, 2000, 2001
13 * NetBSD/pc98 porting staff. All rights reserved.
15 * Copyright (c) 1998, 1999, 2000, 2001
16 * Naofumi HONDA. All rights reserved.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. The name of the author may not be used to endorse or promote products
27 * derived from this software without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
30 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
31 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
32 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
38 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGE.
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
50 #include <sys/queue.h>
51 #include <sys/malloc.h>
52 #include <sys/errno.h>
55 #include <machine/cpu.h>
56 #include <machine/bus.h>
58 #include <cam/scsi/scsi_low.h>
59 #include <dev/nsp/nspreg.h>
60 #include <dev/nsp/nspvar.h>
62 /***************************************************
64 ***************************************************/
65 /* DEVICE CONFIGURATION FLAGS (MINOR)
68 * 0x02 PARITY LINE OFF
69 * 0x04 IDENTIFY MSG OFF ( = single lun)
70 * 0x08 SYNC TRANSFER OFF
73 /***************************************************
75 ***************************************************/
76 #define NSP_NTARGETS 8
79 #define NSP_MAX_DATA_SIZE (64 * 1024)
80 #define NSP_SELTIMEOUT (200)
81 #define NSP_DELAY_MAX (2 * 1000 * 1000)
82 #define NSP_DELAY_INTERVAL (1)
83 #define NSP_TIMER_1MS (1000 / 51)
85 /***************************************************
87 ***************************************************/
90 #endif /* NSP_DEBUG */
96 int device_data_write;
100 int data_phase_bypass;
102 #endif /* NSP_STATICS */
104 /***************************************************
106 ***************************************************/
107 #define NSP_READ_SUSPEND_IO 0x0001
108 #define NSP_WRITE_SUSPEND_IO 0x0002
109 #define NSP_USE_MEMIO 0x0004
110 #define NSP_READ_FIFO_INTERRUPTS 0x0010
111 #define NSP_WRITE_FIFO_INTERRUPTS 0x0020
112 #define NSP_WAIT_FOR_SELECT 0x0100
114 u_int nsp_io_control = NSP_IO_CONTROL_FLAGS;
115 int nsp_read_suspend_bytes = DEV_BSIZE;
116 int nsp_write_suspend_bytes = DEV_BSIZE;
117 int nsp_read_interrupt_bytes = 4096;
118 int nsp_write_interrupt_bytes = 4096;
120 /***************************************************
122 ***************************************************/
123 extern struct cfdriver nsp_cd;
125 /**************************************************************
127 **************************************************************/
128 #define NSP_FIFO_ON 1
129 #define NSP_FIFO_OFF 0
130 static void nsp_pio_read(struct nsp_softc *, int);
131 static void nsp_pio_write(struct nsp_softc *, int);
132 static int nsp_xfer(struct nsp_softc *, u_int8_t *, int, int, int);
133 static int nsp_msg(struct nsp_softc *, struct targ_info *, u_int);
134 static int nsp_reselected(struct nsp_softc *);
135 static int nsp_disconnected(struct nsp_softc *, struct targ_info *);
136 static void nsp_pdma_end(struct nsp_softc *, struct targ_info *);
137 static void nsphw_init(struct nsp_softc *);
138 static int nsp_target_nexus_establish(struct nsp_softc *);
139 static int nsp_lun_nexus_establish(struct nsp_softc *);
140 static int nsp_ccb_nexus_establish(struct nsp_softc *);
141 static int nsp_world_start(struct nsp_softc *, int);
142 static int nsphw_start_selection(struct nsp_softc *sc, struct slccb *);
143 static void nsphw_bus_reset(struct nsp_softc *);
144 static void nsphw_attention(struct nsp_softc *);
145 static u_int nsp_fifo_count(struct nsp_softc *);
146 static u_int nsp_request_count(struct nsp_softc *);
147 static int nsp_negate_signal(struct nsp_softc *, u_int8_t, u_char *);
148 static int nsp_expect_signal(struct nsp_softc *, u_int8_t, u_int8_t);
149 static void nsp_start_timer(struct nsp_softc *, int);
150 static void nsp_setup_fifo(struct nsp_softc *, int, int, int);
151 static int nsp_targ_init(struct nsp_softc *, struct targ_info *, int);
152 static void nsphw_selection_done_and_expect_msgout(struct nsp_softc *);
153 static void nsp_data_padding(struct nsp_softc *, int, u_int);
154 static int nsp_timeout(struct nsp_softc *);
155 static int nsp_read_fifo(struct nsp_softc *, int);
156 static int nsp_write_fifo(struct nsp_softc *, int);
157 static int nsp_phase_match(struct nsp_softc *, u_int8_t, u_int8_t);
158 static int nsp_wait_interrupt(struct nsp_softc *);
160 struct scsi_low_funcs nspfuncs = {
161 SC_LOW_INIT_T nsp_world_start,
162 SC_LOW_BUSRST_T nsphw_bus_reset,
163 SC_LOW_TARG_INIT_T nsp_targ_init,
164 SC_LOW_LUN_INIT_T NULL,
166 SC_LOW_SELECT_T nsphw_start_selection,
167 SC_LOW_NEXUS_T nsp_lun_nexus_establish,
168 SC_LOW_NEXUS_T nsp_ccb_nexus_establish,
170 SC_LOW_ATTEN_T nsphw_attention,
171 SC_LOW_MSG_T nsp_msg,
173 SC_LOW_TIMEOUT_T nsp_timeout,
174 SC_LOW_POLL_T nspintr,
179 /****************************************************
181 ****************************************************/
182 static __inline uint8_t
183 nsp_cr_read_1(struct resource *res, bus_addr_t ofs)
186 bus_write_1(res, nsp_idxr, ofs);
187 return bus_read_1(res, nsp_datar);
191 nsp_cr_write_1(struct resource *res, bus_addr_t ofs, uint8_t va)
194 bus_write_1(res, nsp_idxr, ofs);
195 bus_write_1(res, nsp_datar, va);
199 nsp_expect_signal(struct nsp_softc *sc, u_int8_t curphase, u_int8_t mask)
201 struct scsi_low_softc *slp = &sc->sc_sclow;
205 for (wc = 0; wc < NSP_DELAY_MAX / NSP_DELAY_INTERVAL; wc ++)
207 ph = nsp_cr_read_1(sc->port_res, NSPR_SCBUSMON);
208 if (ph == (u_int8_t) -1)
211 isrc = bus_read_1(sc->port_res, nsp_irqsr);
212 if (isrc & IRQSR_SCSI)
215 if ((ph & mask) != 0 && (ph & SCBUSMON_PHMASK) == curphase)
218 DELAY(NSP_DELAY_INTERVAL);
221 device_printf(slp->sl_dev, "nsp_expect_signal timeout\n");
227 struct nsp_softc *sc;
230 /* block all interrupts */
231 bus_write_1(sc->port_res, nsp_irqcr, IRQCR_ALLMASK);
233 /* setup SCSI interface */
234 bus_write_1(sc->port_res, nsp_ifselr, IFSELR_IFSEL);
236 nsp_cr_write_1(sc->port_res, NSPR_SCIENR, 0);
238 nsp_cr_write_1(sc->port_res, NSPR_XFERMR, XFERMR_IO8);
239 nsp_cr_write_1(sc->port_res, NSPR_CLKDIVR, sc->sc_iclkdiv);
241 nsp_cr_write_1(sc->port_res, NSPR_SCIENR, sc->sc_icr);
242 nsp_cr_write_1(sc->port_res, NSPR_PARITYR, sc->sc_parr);
243 nsp_cr_write_1(sc->port_res, NSPR_PTCLRR,
244 PTCLRR_ACK | PTCLRR_REQ | PTCLRR_HOST | PTCLRR_RSS);
246 /* setup fifo asic */
247 bus_write_1(sc->port_res, nsp_ifselr, IFSELR_REGSEL);
248 nsp_cr_write_1(sc->port_res, NSPR_TERMPWRC, 0);
249 if ((nsp_cr_read_1(sc->port_res, NSPR_OCR) & OCR_TERMPWRS) == 0)
250 nsp_cr_write_1(sc->port_res, NSPR_TERMPWRC, TERMPWRC_POWON);
252 nsp_cr_write_1(sc->port_res, NSPR_XFERMR, XFERMR_IO8);
253 nsp_cr_write_1(sc->port_res, NSPR_CLKDIVR, sc->sc_clkdiv);
254 nsp_cr_write_1(sc->port_res, NSPR_TIMERCNT, 0);
255 nsp_cr_write_1(sc->port_res, NSPR_TIMERCNT, 0);
257 nsp_cr_write_1(sc->port_res, NSPR_SYNCR, 0);
258 nsp_cr_write_1(sc->port_res, NSPR_ACKWIDTH, 0);
260 /* enable interrupts and ack them */
261 nsp_cr_write_1(sc->port_res, NSPR_SCIENR, sc->sc_icr);
262 bus_write_1(sc->port_res, nsp_irqcr, IRQSR_MASK);
264 nsp_setup_fifo(sc, NSP_FIFO_OFF, SCSI_LOW_READ, 0);
267 /****************************************************
269 ****************************************************/
272 struct nsp_softc *sc;
276 cr = nsp_cr_read_1(sc->port_res, NSPR_SCBUSCR)/* & ~SCBUSCR_ACK */;
277 nsp_cr_write_1(sc->port_res, NSPR_SCBUSCR, cr | SCBUSCR_ATN);
283 struct nsp_softc *sc;
287 bus_write_1(sc->port_res, nsp_irqcr, IRQCR_ALLMASK);
289 nsp_cr_write_1(sc->port_res, NSPR_SCBUSCR, SCBUSCR_RST);
290 DELAY(100 * 1000); /* 100ms */
291 nsp_cr_write_1(sc->port_res, NSPR_SCBUSCR, 0);
292 for (i = 0; i < 5; i ++)
293 (void) nsp_cr_read_1(sc->port_res, NSPR_IRQPHS);
295 bus_write_1(sc->port_res, nsp_irqcr, IRQSR_MASK);
299 nsphw_selection_done_and_expect_msgout(sc)
300 struct nsp_softc *sc;
302 struct scsi_low_softc *slp = &sc->sc_sclow;
304 /* clear ack counter */
306 nsp_cr_write_1(sc->port_res, NSPR_PTCLRR, PTCLRR_PT | PTCLRR_ACK |
307 PTCLRR_REQ | PTCLRR_HOST);
309 /* deassert sel and assert atten */
311 nsp_cr_write_1(sc->port_res, NSPR_SCBUSCR, sc->sc_busc);
313 nsp_cr_write_1(sc->port_res, NSPR_SCBUSCR,
314 sc->sc_busc | SCBUSCR_ADIR | SCBUSCR_ACKEN);
315 SCSI_LOW_ASSERT_ATN(slp);
319 nsphw_start_selection(sc, cb)
320 struct nsp_softc *sc;
323 struct scsi_low_softc *slp = &sc->sc_sclow;
324 struct targ_info *ti = cb->ti;
325 register u_int8_t arbs, ph;
328 wc = sc->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
329 sc->sc_dataout_timeout = 0;
332 ph = nsp_cr_read_1(sc->port_res, NSPR_SCBUSMON);
333 if (ph != SCBUSMON_FREE)
336 nsp_statics.arbit_conflict_1 ++;
337 #endif /* NSP_STATICS */
338 return SCSI_LOW_START_FAIL;
341 /* start arbitration */
342 nsp_cr_write_1(sc->port_res, NSPR_ARBITS, ARBITS_EXEC);
344 SCSI_LOW_SETUP_PHASE(ti, PH_ARBSTART);
347 /* XXX: what a stupid chip! */
348 arbs = nsp_cr_read_1(sc->port_res, NSPR_ARBITS);
351 while ((arbs & (ARBITS_WIN | ARBITS_FAIL)) == 0 && wc -- > 0);
353 if ((arbs & ARBITS_WIN) == 0)
355 nsp_cr_write_1(sc->port_res, NSPR_ARBITS, ARBITS_CLR);
357 nsp_statics.arbit_conflict_2 ++;
358 #endif /* NSP_STATICS */
359 return SCSI_LOW_START_FAIL;
362 /* assert select line */
363 SCSI_LOW_SETUP_PHASE(ti, PH_SELSTART);
364 scsi_low_arbit_win(slp);
367 nsp_cr_write_1(sc->port_res, NSPR_DATA,
368 sc->sc_idbit | (1 << ti->ti_id));
369 nsp_cr_write_1(sc->port_res, NSPR_SCBUSCR,
370 SCBUSCR_SEL | SCBUSCR_BSY | sc->sc_busc);
372 nsp_cr_write_1(sc->port_res, NSPR_SCBUSCR, SCBUSCR_SEL |
373 SCBUSCR_BSY | SCBUSCR_DOUT | sc->sc_busc);
374 nsp_cr_write_1(sc->port_res, NSPR_ARBITS, ARBITS_CLR);
376 nsp_cr_write_1(sc->port_res, NSPR_SCBUSCR,
377 SCBUSCR_SEL | SCBUSCR_DOUT | sc->sc_busc);
380 if ((nsp_io_control & NSP_WAIT_FOR_SELECT) != 0)
382 #define NSP_FIRST_SEL_WAIT 300
383 #define NSP_SEL_CHECK_INTERVAL 10
385 /* wait for a selection response */
386 for (wc = 0; wc < NSP_FIRST_SEL_WAIT / NSP_SEL_CHECK_INTERVAL;
389 ph = nsp_cr_read_1(sc->port_res, NSPR_SCBUSMON);
390 if ((ph & SCBUSMON_BSY) == 0)
392 DELAY(NSP_SEL_CHECK_INTERVAL);
397 ph = nsp_cr_read_1(sc->port_res, NSPR_SCBUSMON);
398 if ((ph & SCBUSMON_BSY) != 0)
400 nsphw_selection_done_and_expect_msgout(sc);
402 SCSI_LOW_SETUP_PHASE(ti, PH_SELECTED);
403 return SCSI_LOW_START_OK;
408 /* check a selection timeout */
409 nsp_start_timer(sc, NSP_TIMER_1MS);
411 return SCSI_LOW_START_OK;
415 nsp_world_start(sc, fdone)
416 struct nsp_softc *sc;
419 struct scsi_low_softc *slp = &sc->sc_sclow;
424 if ((slp->sl_cfgflags & CFG_NOATTEN) == 0)
425 sc->sc_busc = SCBUSCR_ATN;
429 if ((slp->sl_cfgflags & CFG_NOPARITY) == 0)
430 sc->sc_parr = PARITYR_ENABLE | PARITYR_CLEAR;
434 sc->sc_icr = (SCIENR_SCCHG | SCIENR_RESEL | SCIENR_RST);
437 scsi_low_bus_reset(slp);
442 struct ncp_synch_data {
449 static struct ncp_synch_data ncp_sync_data_40M[] = {
450 {0x0c,0x0c,0x1,0}, /* 20MB 50ns*/
451 {0x19,0x19,0x3,1}, /* 10MB 100ns*/
452 {0x1a,0x25,0x5,2}, /* 7.5MB 150ns*/
453 {0x26,0x32,0x7,3}, /* 5MB 200ns*/
457 static struct ncp_synch_data ncp_sync_data_20M[] = {
458 {0x19,0x19,0x1,0}, /* 10MB 100ns*/
459 {0x1a,0x25,0x2,0}, /* 7.5MB 150ns*/
460 {0x26,0x32,0x3,1}, /* 5MB 200ns*/
466 struct nsp_softc *sc;
467 struct targ_info *ti;
470 struct ncp_synch_data *sdp;
471 struct nsp_targ_info *nti = (void *) ti;
472 u_int period, offset;
475 if ((msg & SCSI_LOW_MSG_WIDE) != 0)
477 if (ti->ti_width != SCSI_LOW_BUS_WIDTH_8)
479 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
485 if ((msg & SCSI_LOW_MSG_SYNCH) == 0)
488 period = ti->ti_maxsynch.period;
489 offset = ti->ti_maxsynch.offset;
490 if (sc->sc_iclkdiv == CLKDIVR_20M)
491 sdp = &ncp_sync_data_20M[0];
493 sdp = &ncp_sync_data_40M[0];
495 for (i = 0; sdp->max_period != 0; i ++, sdp ++)
497 if (period >= sdp->min_period && period <= sdp->max_period)
501 if (period != 0 && sdp->max_period == 0)
504 * NO proper period/offset found,
505 * Retry neg with the target.
507 ti->ti_maxsynch.period = 0;
508 ti->ti_maxsynch.offset = 0;
509 nti->nti_reg_syncr = 0;
510 nti->nti_reg_ackwidth = 0;
515 nti->nti_reg_syncr = (sdp->chip_period << SYNCR_PERS) |
516 (offset & SYNCR_OFFM);
517 nti->nti_reg_ackwidth = sdp->ack_width;
521 nsp_cr_write_1(sc->port_res, NSPR_SYNCR, nti->nti_reg_syncr);
522 nsp_cr_write_1(sc->port_res, NSPR_ACKWIDTH, nti->nti_reg_ackwidth);
527 nsp_targ_init(sc, ti, action)
528 struct nsp_softc *sc;
529 struct targ_info *ti;
532 struct nsp_targ_info *nti = (void *) ti;
534 if (action == SCSI_LOW_INFO_ALLOC || action == SCSI_LOW_INFO_REVOKE)
536 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
537 ti->ti_maxsynch.period = 100 / 4;
538 ti->ti_maxsynch.offset = 15;
539 nti->nti_reg_syncr = 0;
540 nti->nti_reg_ackwidth = 0;
546 nsp_start_timer(sc, time)
547 struct nsp_softc *sc;
552 nsp_cr_write_1(sc->port_res, NSPR_TIMERCNT, time);
555 /**************************************************************
556 * General probe attach
557 **************************************************************/
559 nspprobesubr(struct resource *res, u_int dvcfg)
563 regv = bus_read_1(res, nsp_fifosr);
564 if (regv < 0x11 || regv >= 0x20)
571 struct nsp_softc *sc;
573 struct scsi_low_softc *slp = &sc->sc_sclow;
575 sc->sc_idbit = (1 << slp->sl_hostid);
576 slp->sl_flags |= HW_READ_PADDING;
577 slp->sl_funcs = &nspfuncs;
578 sc->sc_tmaxcnt = SCSI_LOW_MIN_TOUT * 1000 * 1000; /* default */
580 (void) scsi_low_attach(slp, 0, NSP_NTARGETS, NSP_NLUNS,
581 sizeof(struct nsp_targ_info), 0);
584 /**************************************************************
586 **************************************************************/
589 struct nsp_softc *sc;
593 nsp_cr_write_1(sc->port_res, NSPR_PTCLRR, PTCLRR_RSS_ACK | PTCLRR_PT);
594 count = bus_read_1(sc->port_res, nsp_datar);
595 count += (((u_int) bus_read_1(sc->port_res, nsp_datar)) << 8);
596 count += (((u_int) bus_read_1(sc->port_res, nsp_datar)) << 16);
601 nsp_request_count(sc)
602 struct nsp_softc *sc;
606 nsp_cr_write_1(sc->port_res, NSPR_PTCLRR, PTCLRR_RSS_REQ | PTCLRR_PT);
607 count = bus_read_1(sc->port_res, nsp_datar);
608 count += (((u_int) bus_read_1(sc->port_res, nsp_datar)) << 8);
609 count += (((u_int) bus_read_1(sc->port_res, nsp_datar)) << 16);
614 nsp_setup_fifo(sc, on, direction, datalen)
615 struct nsp_softc *sc;
622 sc->sc_suspendio = 0;
623 if (on == NSP_FIFO_OFF)
625 xfermode = XFERMR_IO8;
629 /* check if suspend io OK ? */
632 if (direction == SCSI_LOW_READ)
634 if ((nsp_io_control & NSP_READ_SUSPEND_IO) != 0 &&
635 (datalen % nsp_read_suspend_bytes) == 0)
636 sc->sc_suspendio = nsp_read_suspend_bytes;
640 if ((nsp_io_control & NSP_WRITE_SUSPEND_IO) != 0 &&
641 (datalen % nsp_write_suspend_bytes) == 0)
642 sc->sc_suspendio = nsp_write_suspend_bytes;
646 /* determine a transfer type */
647 if (datalen < DEV_BSIZE || (datalen & 3) != 0)
649 if (sc->mem_res != NULL &&
650 (nsp_io_control & NSP_USE_MEMIO) != 0)
651 xfermode = XFERMR_XEN | XFERMR_MEM8;
653 xfermode = XFERMR_XEN | XFERMR_IO8;
657 if (sc->mem_res != NULL &&
658 (nsp_io_control & NSP_USE_MEMIO) != 0)
659 xfermode = XFERMR_XEN | XFERMR_MEM32;
661 xfermode = XFERMR_XEN | XFERMR_IO32;
663 if (sc->sc_suspendio > 0)
664 xfermode |= XFERMR_FIFOEN;
668 sc->sc_xfermr = xfermode;
669 nsp_cr_write_1(sc->port_res, NSPR_XFERMR, sc->sc_xfermr);
674 struct nsp_softc *sc;
675 struct targ_info *ti;
677 struct scsi_low_softc *slp = &sc->sc_sclow;
678 struct slccb *cb = slp->sl_Qnexus;
681 sc->sc_dataout_timeout = 0;
682 slp->sl_flags &= ~HW_PDMASTART;
683 nsp_setup_fifo(sc, NSP_FIFO_OFF, SCSI_LOW_READ, 0);
684 if ((sc->sc_icr & SCIENR_FIFO) != 0)
686 sc->sc_icr &= ~SCIENR_FIFO;
687 nsp_cr_write_1(sc->port_res, NSPR_SCIENR, sc->sc_icr);
692 slp->sl_error |= PDMAERR;
696 if (ti->ti_phase == PH_DATA)
698 cnt = nsp_fifo_count(sc);
699 if (slp->sl_scp.scp_direction == SCSI_LOW_WRITE)
701 len = sc->sc_cnt - cnt;
702 if (sc->sc_cnt >= cnt &&
703 slp->sl_scp.scp_datalen + len <=
704 cb->ccb_scp.scp_datalen)
706 slp->sl_scp.scp_data -= len;
707 slp->sl_scp.scp_datalen += len;
711 slp->sl_error |= PDMAERR;
712 device_printf(slp->sl_dev,
713 "len %x >= datalen %x\n",
714 len, slp->sl_scp.scp_datalen);
717 else if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
719 if (sc->sc_cnt != cnt ||
720 sc->sc_cnt > cb->ccb_scp.scp_datalen)
722 slp->sl_error |= PDMAERR;
723 device_printf(slp->sl_dev,
724 "data read count error %x != %x (%x)\n",
726 cb->ccb_scp.scp_datalen);
730 scsi_low_data_finish(slp);
735 device_printf(slp->sl_dev, "data phase miss\n");
736 slp->sl_error |= PDMAERR;
740 #define RFIFO_CRIT 64
741 #define WFIFO_CRIT 32
744 nsp_data_padding(sc, direction, count)
745 struct nsp_softc *sc;
750 if (count > NSP_MAX_DATA_SIZE)
751 count = NSP_MAX_DATA_SIZE;
753 nsp_cr_write_1(sc->port_res, NSPR_XFERMR, XFERMR_XEN | XFERMR_IO8);
754 if (direction == SCSI_LOW_READ)
757 (void) bus_read_1(sc->port_res, nsp_fifodr);
762 (void) bus_write_1(sc->port_res, nsp_fifodr, 0);
764 nsp_cr_write_1(sc->port_res, NSPR_XFERMR, sc->sc_xfermr);
768 nsp_read_fifo(sc, suspendio)
769 struct nsp_softc *sc;
772 struct scsi_low_softc *slp = &sc->sc_sclow;
775 res = nsp_fifo_count(sc);
776 if (res == sc->sc_cnt)
780 if (res < sc->sc_cnt || res == (u_int) -1)
782 device_printf(slp->sl_dev,
783 "strange fifo ack count 0x%x < 0x%x\n", res, sc->sc_cnt);
786 #endif /* NSP_DEBUG */
788 res = res - sc->sc_cnt;
789 if (res > slp->sl_scp.scp_datalen)
791 if ((slp->sl_error & PDMAERR) == 0)
793 device_printf(slp->sl_dev, "data overrun 0x%x > 0x%x\n",
794 res, slp->sl_scp.scp_datalen);
797 slp->sl_error |= PDMAERR;
798 slp->sl_scp.scp_datalen = 0;
800 if ((slp->sl_flags & HW_READ_PADDING) == 0)
802 device_printf(slp->sl_dev, "read padding required\n");
806 nsp_data_padding(sc, SCSI_LOW_READ, res);
808 return 1; /* padding start */
811 if (suspendio > 0 && slp->sl_scp.scp_datalen >= suspendio)
814 if ((sc->sc_xfermr & (XFERMR_MEM32 | XFERMR_MEM8)) != 0)
816 if ((sc->sc_xfermr & XFERMR_MEM32) != 0)
819 bus_read_region_4(sc->mem_res, 0,
820 (u_int32_t *) slp->sl_scp.scp_data, res >> 2);
824 bus_read_region_1(sc->mem_res, 0,
825 (u_int8_t *) slp->sl_scp.scp_data, res);
830 if ((sc->sc_xfermr & XFERMR_IO32) != 0)
833 bus_read_multi_4(sc->port_res, nsp_fifodr,
834 (u_int32_t *) slp->sl_scp.scp_data, res >> 2);
838 bus_read_multi_1(sc->port_res, nsp_fifodr,
839 (u_int8_t *) slp->sl_scp.scp_data, res);
843 if (nsp_cr_read_1(sc->port_res, NSPR_PARITYR) & PARITYR_PE)
845 nsp_cr_write_1(sc->port_res, NSPR_PARITYR,
846 PARITYR_ENABLE | PARITYR_CLEAR);
847 scsi_low_assert_msg(slp, slp->sl_Tnexus, SCSI_LOW_MSG_ERROR, 1);
850 slp->sl_scp.scp_data += res;
851 slp->sl_scp.scp_datalen -= res;
857 nsp_write_fifo(sc, suspendio)
858 struct nsp_softc *sc;
861 struct scsi_low_softc *slp = &sc->sc_sclow;
863 register u_int8_t stat;
868 if ((slp->sl_scp.scp_datalen % WFIFO_CRIT) != 0)
870 device_printf(slp->sl_dev,
871 "strange write length 0x%x\n",
872 slp->sl_scp.scp_datalen);
874 #endif /* NSP_DEBUG */
875 res = slp->sl_scp.scp_datalen % suspendio;
886 if (res > slp->sl_scp.scp_datalen)
887 res = slp->sl_scp.scp_datalen;
889 /* XXX: reconfirm! */
890 stat = nsp_cr_read_1(sc->port_res, NSPR_SCBUSMON) & SCBUSMON_PHMASK;
891 if (stat != PHASE_DATAOUT)
894 if ((sc->sc_xfermr & (XFERMR_MEM32 | XFERMR_MEM8)) != 0)
896 if ((sc->sc_xfermr & XFERMR_MEM32) != 0)
898 bus_write_region_4(sc->mem_res, 0,
899 (u_int32_t *) slp->sl_scp.scp_data, res >> 2);
903 bus_write_region_1(sc->mem_res, 0,
904 (u_int8_t *) slp->sl_scp.scp_data, res);
909 if ((sc->sc_xfermr & XFERMR_IO32) != 0)
911 bus_write_multi_4(sc->port_res, nsp_fifodr,
912 (u_int32_t *) slp->sl_scp.scp_data, res >> 2);
916 bus_write_multi_1(sc->port_res, nsp_fifodr,
917 (u_int8_t *) slp->sl_scp.scp_data, res);
921 slp->sl_scp.scp_datalen -= res;
922 slp->sl_scp.scp_data += res;
928 nsp_wait_interrupt(sc)
929 struct nsp_softc *sc;
932 register u_int8_t isrc;
934 for (tout = 0; tout < DEV_BSIZE / 10; tout ++)
936 isrc = bus_read_1(sc->port_res, nsp_irqsr);
937 if ((isrc & (IRQSR_SCSI | IRQSR_FIFO)) != 0)
939 if ((isrc & IRQSR_FIFO) != 0)
941 bus_write_1(sc->port_res,
942 nsp_irqcr, IRQCR_FIFOCL);
952 nsp_pio_read(sc, suspendio)
953 struct nsp_softc *sc;
956 struct scsi_low_softc *slp = &sc->sc_sclow;
957 int tout, padding, datalen;
958 register u_int8_t stat, fstat;
961 tout = sc->sc_tmaxcnt;
962 slp->sl_flags |= HW_PDMASTART;
963 datalen = slp->sl_scp.scp_datalen;
968 stat = nsp_cr_read_1(sc->port_res, NSPR_SCBUSMON);
969 if (stat == (u_int8_t) -1)
972 /* out of data phase */
973 if ((stat & SCBUSMON_PHMASK) != PHASE_DATAIN)
975 nsp_read_fifo(sc, 0);
980 fstat = bus_read_1(sc->port_res, nsp_fifosr);
981 if ((fstat & FIFOSR_FULLEMP) != 0)
983 if ((sc->sc_icr & SCIENR_FIFO) != 0)
985 bus_write_1(sc->port_res, nsp_irqcr,
991 padding |= nsp_read_fifo(sc, suspendio);
995 padding |= nsp_read_fifo(sc, 0);
998 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1003 if (padding == 0 && slp->sl_scp.scp_datalen <= 0)
1006 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1014 device_printf(slp->sl_dev, "nsp_pio_read: timeout\n");
1020 if (slp->sl_scp.scp_datalen > 0 &&
1021 slp->sl_scp.scp_datalen > datalen - nsp_read_interrupt_bytes)
1023 if (nsp_wait_interrupt(sc) != 0)
1029 nsp_pio_write(sc, suspendio)
1030 struct nsp_softc *sc;
1033 struct scsi_low_softc *slp = &sc->sc_sclow;
1034 u_int rcount, acount;
1036 register u_int8_t stat, fstat;
1038 tout = sc->sc_tmaxcnt;
1039 slp->sl_flags |= HW_PDMASTART;
1040 datalen = slp->sl_scp.scp_datalen;
1045 stat = nsp_cr_read_1(sc->port_res, NSPR_SCBUSMON) & SCBUSMON_PHMASK;
1046 if (stat != PHASE_DATAOUT)
1049 if (slp->sl_scp.scp_datalen <= 0)
1051 if (sc->sc_dataout_timeout == 0)
1052 sc->sc_dataout_timeout = SCSI_LOW_TIMEOUT_HZ;
1056 fstat = bus_read_1(sc->port_res, nsp_fifosr);
1057 if ((fstat & FIFOSR_FULLEMP) != 0)
1059 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1061 bus_write_1(sc->port_res, nsp_irqcr,
1068 * To avoid timeout of pcmcia bus
1069 * (not scsi bus!), we should check
1070 * the scsi device sends us request
1071 * signals, which means the scsi device
1072 * is ready to recieve data without
1075 if ((slp->sl_scp.scp_datalen % suspendio) == 0)
1078 * fill the nsp fifo, and waiting for
1081 nsp_write_fifo(sc, 0);
1086 * check the request singals.
1088 acount = nsp_fifo_count(sc);
1089 rcount = nsp_request_count(sc);
1090 if (rcount <= acount)
1092 nsp_write_fifo(sc, 0);
1094 nsp_statics.device_busy ++;
1095 #endif /* NSP_STATICS */
1099 nsp_write_fifo(sc, suspendio);
1101 nsp_statics.device_data_write ++;
1102 #endif /* NSP_STATICS */
1108 nsp_write_fifo(sc, 0);
1111 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1116 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1124 device_printf(slp->sl_dev, "nsp_pio_write: timeout\n");
1129 if (slp->sl_scp.scp_datalen > 0 &&
1130 slp->sl_scp.scp_datalen > datalen - nsp_write_interrupt_bytes)
1132 if (nsp_wait_interrupt(sc) != 0)
1138 nsp_negate_signal(struct nsp_softc *sc, u_int8_t mask, u_char *s)
1140 struct scsi_low_softc *slp = &sc->sc_sclow;
1144 for (wc = 0; wc < NSP_DELAY_MAX / NSP_DELAY_INTERVAL; wc ++)
1146 regv = nsp_cr_read_1(sc->port_res, NSPR_SCBUSMON);
1147 if (regv == (u_int8_t) -1)
1149 if ((regv & mask) == 0)
1151 DELAY(NSP_DELAY_INTERVAL);
1154 device_printf(slp->sl_dev, "%s nsp_negate_signal timeout\n", s);
1159 nsp_xfer(sc, buf, len, phase, clear_atn)
1160 struct nsp_softc *sc;
1168 for (ptr = 0; len > 0; len --, ptr ++)
1170 rv = nsp_expect_signal(sc, phase, SCBUSMON_REQ);
1174 if (len == 1 && clear_atn != 0)
1176 nsp_cr_write_1(sc->port_res, NSPR_SCBUSCR,
1177 SCBUSCR_ADIR | SCBUSCR_ACKEN);
1178 SCSI_LOW_DEASSERT_ATN(&sc->sc_sclow);
1181 if (phase & SCBUSMON_IO)
1183 buf[ptr] = nsp_cr_read_1(sc->port_res, NSPR_DATAACK);
1187 nsp_cr_write_1(sc->port_res, NSPR_DATAACK, buf[ptr]);
1189 nsp_negate_signal(sc, SCBUSMON_ACK, "xfer<ACK>");
1196 /**************************************************************
1197 * disconnect & reselect (HW low)
1198 **************************************************************/
1201 struct nsp_softc *sc;
1203 struct scsi_low_softc *slp = &sc->sc_sclow;
1204 struct targ_info *ti;
1208 sid = (u_int) nsp_cr_read_1(sc->port_res, NSPR_RESELR);
1209 sid &= ~sc->sc_idbit;
1211 if ((ti = scsi_low_reselected(slp, sid)) == NULL)
1214 nsp_negate_signal(sc, SCBUSMON_SEL, "reselect<SEL>");
1216 cr = nsp_cr_read_1(sc->port_res, NSPR_SCBUSCR);
1217 cr &= ~(SCBUSCR_BSY | SCBUSCR_ATN);
1218 nsp_cr_write_1(sc->port_res, NSPR_SCBUSCR, cr);
1219 cr |= SCBUSCR_ADIR | SCBUSCR_ACKEN;
1220 nsp_cr_write_1(sc->port_res, NSPR_SCBUSCR, cr);
1223 nsp_statics.reselect ++;
1224 #endif /* NSP_STATCIS */
1229 nsp_disconnected(sc, ti)
1230 struct nsp_softc *sc;
1231 struct targ_info *ti;
1233 struct scsi_low_softc *slp = &sc->sc_sclow;
1235 nsp_cr_write_1(sc->port_res, NSPR_PTCLRR, PTCLRR_PT | PTCLRR_ACK |
1236 PTCLRR_REQ | PTCLRR_HOST);
1237 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1239 sc->sc_icr &= ~SCIENR_FIFO;
1240 nsp_cr_write_1(sc->port_res, NSPR_SCIENR, sc->sc_icr);
1243 sc->sc_dataout_timeout = 0;
1245 nsp_statics.disconnect ++;
1246 #endif /* NSP_STATICS */
1247 scsi_low_disconnected(slp, ti);
1251 /**************************************************************
1253 **************************************************************/
1254 static void nsp_error(struct nsp_softc *, u_char *, u_int8_t, u_int8_t, u_int8_t);
1257 nsp_error(struct nsp_softc * sc, u_char *s, u_int8_t isrc, u_int8_t ph,
1260 struct scsi_low_softc *slp = &sc->sc_sclow;
1262 device_printf(slp->sl_dev, "%s\n", s);
1263 device_printf(slp->sl_dev, "isrc 0x%x scmon 0x%x irqphs 0x%x\n",
1264 (u_int) isrc, (u_int) ph, (u_int) irqphs);
1268 nsp_target_nexus_establish(sc)
1269 struct nsp_softc *sc;
1271 struct scsi_low_softc *slp = &sc->sc_sclow;
1272 struct targ_info *ti = slp->sl_Tnexus;
1273 struct nsp_targ_info *nti = (void *) ti;
1275 /* setup synch transfer registers */
1276 nsp_cr_write_1(sc->port_res, NSPR_SYNCR, nti->nti_reg_syncr);
1277 nsp_cr_write_1(sc->port_res, NSPR_ACKWIDTH, nti->nti_reg_ackwidth);
1279 /* setup pdma fifo (minimum) */
1280 nsp_setup_fifo(sc, NSP_FIFO_ON, SCSI_LOW_READ, 0);
1285 nsp_lun_nexus_establish(sc)
1286 struct nsp_softc *sc;
1293 nsp_ccb_nexus_establish(sc)
1294 struct nsp_softc *sc;
1296 struct scsi_low_softc *slp = &sc->sc_sclow;
1297 struct slccb *cb = slp->sl_Qnexus;
1299 sc->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
1301 /* setup pdma fifo */
1302 nsp_setup_fifo(sc, NSP_FIFO_ON,
1303 slp->sl_scp.scp_direction, slp->sl_scp.scp_datalen);
1305 if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
1307 if (sc->sc_suspendio > 0 &&
1308 (nsp_io_control & NSP_READ_FIFO_INTERRUPTS) != 0)
1310 sc->sc_icr |= SCIENR_FIFO;
1311 nsp_cr_write_1(sc->port_res,
1312 NSPR_SCIENR, sc->sc_icr);
1317 if (sc->sc_suspendio > 0 &&
1318 (nsp_io_control & NSP_WRITE_FIFO_INTERRUPTS) != 0)
1320 sc->sc_icr |= SCIENR_FIFO;
1321 nsp_cr_write_1(sc->port_res,
1322 NSPR_SCIENR, sc->sc_icr);
1329 nsp_phase_match(struct nsp_softc *sc, u_int8_t phase, u_int8_t stat)
1331 struct scsi_low_softc *slp = &sc->sc_sclow;
1333 if ((stat & SCBUSMON_PHMASK) != phase)
1335 device_printf(slp->sl_dev, "phase mismatch 0x%x != 0x%x\n",
1336 (u_int) phase, (u_int) stat);
1340 if ((stat & SCBUSMON_REQ) == 0)
1350 struct nsp_softc *sc = arg;
1351 struct scsi_low_softc *slp = &sc->sc_sclow;
1352 struct targ_info *ti;
1354 u_int derror, flags;
1356 u_int8_t isrc, ph, irqphs, cr, regv;
1358 /*******************************************
1360 *******************************************/
1361 if (slp->sl_flags & HW_INACTIVE)
1364 bus_write_1(sc->port_res, nsp_irqcr, IRQCR_IRQDIS);
1365 isrc = bus_read_1(sc->port_res, nsp_irqsr);
1366 if (isrc == (u_int8_t) -1 || (isrc & IRQSR_MASK) == 0)
1368 bus_write_1(sc->port_res, nsp_irqcr, 0);
1373 * Do not read an irqphs register if no scsi phase interrupt.
1374 * Unless, you should lose a scsi phase interrupt.
1376 ph = nsp_cr_read_1(sc->port_res, NSPR_SCBUSMON);
1377 if ((isrc & IRQSR_SCSI) != 0)
1379 irqphs = nsp_cr_read_1(sc->port_res, NSPR_IRQPHS);
1385 * timer interrupt handler (scsi vs timer interrupts)
1387 if (sc->sc_timer != 0)
1389 nsp_cr_write_1(sc->port_res, NSPR_TIMERCNT, 0);
1390 nsp_cr_write_1(sc->port_res, NSPR_TIMERCNT, 0);
1394 /* check a timer interrupt */
1396 if ((isrc & IRQSR_TIMER) != 0)
1398 if ((isrc & IRQSR_MASK) == IRQSR_TIMER && sc->sc_seltout == 0)
1400 bus_write_1(sc->port_res, nsp_irqcr, IRQCR_TIMERCL);
1403 regv |= IRQCR_TIMERCL;
1406 /* check a fifo interrupt */
1407 if ((isrc & IRQSR_FIFO) != 0)
1409 regv |= IRQCR_FIFOCL;
1412 /* OK. enable all interrupts */
1413 bus_write_1(sc->port_res, nsp_irqcr, regv);
1415 /*******************************************
1417 *******************************************/
1421 nsp_error(sc, "current status", isrc, ph, irqphs);
1422 scsi_low_print(slp, NULL);
1425 kdb_enter(KDB_WHY_CAM, "nsp");
1428 #endif /* NSP_DEBUG */
1430 /*******************************************
1431 * Parse hardware SCSI irq reasons register
1432 *******************************************/
1433 if ((isrc & IRQSR_SCSI) != 0)
1435 if ((irqphs & IRQPHS_RST) != 0)
1437 scsi_low_restart(slp, SCSI_LOW_RESTART_SOFT,
1438 "bus reset (power off?)");
1442 if ((irqphs & IRQPHS_RSEL) != 0)
1444 bus_write_1(sc->port_res, nsp_irqcr, IRQCR_RESCL);
1445 if (nsp_reselected(sc) == EJUSTRETURN)
1449 if ((irqphs & (IRQPHS_PCHG | IRQPHS_LBF)) == 0)
1453 /*******************************************
1455 *******************************************/
1456 if ((ti = slp->sl_Tnexus) == NULL)
1458 /* unknown scsi phase changes */
1459 nsp_error(sc, "unknown scsi phase changes", isrc, ph, irqphs);
1463 /*******************************************
1464 * aribitration & selection
1465 *******************************************/
1466 switch (ti->ti_phase)
1469 if ((ph & SCBUSMON_BSY) == 0)
1471 if (sc->sc_seltout >= NSP_SELTIMEOUT)
1474 nsp_cr_write_1(sc->port_res, NSPR_SCBUSCR, 0);
1475 return nsp_disconnected(sc, ti);
1478 nsp_start_timer(sc, NSP_TIMER_1MS);
1482 SCSI_LOW_SETUP_PHASE(ti, PH_SELECTED);
1483 nsphw_selection_done_and_expect_msgout(sc);
1487 if ((isrc & IRQSR_SCSI) == 0)
1490 nsp_target_nexus_establish(sc);
1494 if ((isrc & IRQSR_SCSI) == 0)
1497 nsp_target_nexus_establish(sc);
1498 if ((ph & SCBUSMON_PHMASK) != PHASE_MSGIN)
1500 device_printf(slp->sl_dev,
1501 "unexpected phase after reselect\n");
1502 slp->sl_error |= FATALIO;
1503 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 1);
1509 if ((isrc & IRQSR_SCSI) != 0)
1511 if ((isrc & IRQSR_FIFO) != 0)
1513 if (NSP_IS_PHASE_DATA(ph) == 0)
1515 irqphs = (ph & IRQPHS_PHMASK);
1521 if ((isrc & IRQSR_SCSI) == 0)
1526 /*******************************************
1527 * data phase control
1528 *******************************************/
1529 if (slp->sl_flags & HW_PDMASTART)
1531 if ((isrc & IRQSR_SCSI) != 0 &&
1532 NSP_IS_IRQPHS_DATA(irqphs) == 0)
1534 if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
1535 nsp_pio_read(sc, 0);
1536 nsp_pdma_end(sc, ti);
1540 /*******************************************
1542 *******************************************/
1543 if (slp->sl_msgphase != 0 && (irqphs & IRQPHS_LBF) != 0)
1544 return nsp_disconnected(sc, ti);
1546 /* check unexpected bus free state */
1549 nsp_error(sc, "unexpected bus free", isrc, ph, irqphs);
1550 return nsp_disconnected(sc, ti);
1553 /* check normal scsi phase */
1554 switch (irqphs & IRQPHS_PHMASK)
1557 if (nsp_phase_match(sc, PHASE_CMD, ph) != 0)
1560 SCSI_LOW_SETUP_PHASE(ti, PH_CMD);
1561 if (scsi_low_cmd(slp, ti) != 0)
1563 scsi_low_attention(slp);
1566 nsp_cr_write_1(sc->port_res, NSPR_CMDCR, CMDCR_PTCLR);
1567 for (len = 0; len < slp->sl_scp.scp_cmdlen; len ++)
1568 nsp_cr_write_1(sc->port_res, NSPR_CMDDR,
1569 slp->sl_scp.scp_cmd[len]);
1571 nsp_cr_write_1(sc->port_res, NSPR_CMDCR, CMDCR_PTCLR | CMDCR_EXEC);
1574 case IRQPHS_DATAOUT:
1575 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1576 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_WRITE) != 0)
1578 scsi_low_attention(slp);
1581 nsp_pio_write(sc, sc->sc_suspendio);
1585 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1586 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_READ) != 0)
1588 scsi_low_attention(slp);
1591 nsp_pio_read(sc, sc->sc_suspendio);
1595 if (nsp_phase_match(sc, PHASE_STATUS, ph) != 0)
1598 SCSI_LOW_SETUP_PHASE(ti, PH_STAT);
1599 regv = nsp_cr_read_1(sc->port_res, NSPR_DATA);
1600 if (nsp_cr_read_1(sc->port_res, NSPR_PARITYR) & PARITYR_PE)
1602 nsp_cr_write_1(sc->port_res, NSPR_PARITYR,
1603 PARITYR_ENABLE | PARITYR_CLEAR);
1604 derror = SCSI_LOW_DATA_PE;
1610 cr = SCBUSCR_ACK | nsp_cr_read_1(sc->port_res, NSPR_SCBUSCR);
1611 nsp_cr_write_1(sc->port_res, NSPR_SCBUSCR, cr);
1613 if (scsi_low_statusin(slp, ti, derror | regv) != 0)
1615 scsi_low_attention(slp);
1618 /* check REQ nagated */
1619 nsp_negate_signal(sc, SCBUSMON_REQ, "statin<REQ>");
1622 cr = nsp_cr_read_1(sc->port_res, NSPR_SCBUSCR) & (~SCBUSCR_ACK);
1623 nsp_cr_write_1(sc->port_res, NSPR_SCBUSCR, cr);
1627 if (nsp_phase_match(sc, PHASE_MSGOUT, ph) != 0)
1630 #ifdef NSP_MSGOUT_SERIALIZE
1633 * NSP invoke interrupts only in the case of scsi phase changes,
1634 * therefore we should poll the scsi phase here to catch
1635 * the next "msg out" if exists (no scsi phase changes).
1639 SCSI_LOW_SETUP_PHASE(ti, PH_MSGOUT);
1640 flags = (ti->ti_ophase != ti->ti_phase) ?
1641 SCSI_LOW_MSGOUT_INIT : 0;
1642 len = scsi_low_msgout(slp, ti, flags);
1644 if (len > 1 && slp->sl_atten == 0)
1646 scsi_low_attention(slp);
1649 if (nsp_xfer(sc, ti->ti_msgoutstr, len, PHASE_MSGOUT,
1650 slp->sl_clear_atten) != 0)
1652 slp->sl_error |= FATALIO;
1653 nsp_error(sc, "MSGOUT: xfer short",
1657 /* catch a next signal */
1658 rv = nsp_expect_signal(sc, PHASE_MSGOUT, SCBUSMON_REQ);
1660 while (rv > 0 && len -- > 0);
1662 #else /* !NSP_MSGOUT_SERIALIZE */
1663 SCSI_LOW_SETUP_PHASE(ti, PH_MSGOUT);
1664 flags = SCSI_LOW_MSGOUT_UNIFY;
1665 if (ti->ti_ophase != ti->ti_phase)
1666 flags |= SCSI_LOW_MSGOUT_INIT;
1667 len = scsi_low_msgout(slp, ti, flags);
1669 if (len > 1 && slp->sl_atten == 0)
1671 scsi_low_attention(slp);
1674 if (nsp_xfer(sc, ti->ti_msgoutstr, len, PHASE_MSGOUT,
1675 slp->sl_clear_atten) != 0)
1677 nsp_error(sc, "MSGOUT: xfer short", isrc, ph, irqphs);
1680 #endif /* !NSP_MSGOUT_SERIALIZE */
1684 if (nsp_phase_match(sc, PHASE_MSGIN, ph) != 0)
1689 * NSP invoke interrupts only in the case of scsi phase changes,
1690 * therefore we should poll the scsi phase here to catch
1691 * the next "msg in" if exists (no scsi phase changes).
1695 SCSI_LOW_SETUP_PHASE(ti, PH_MSGIN);
1698 regv = nsp_cr_read_1(sc->port_res, NSPR_DATA);
1699 if (nsp_cr_read_1(sc->port_res, NSPR_PARITYR) & PARITYR_PE)
1701 nsp_cr_write_1(sc->port_res,
1703 PARITYR_ENABLE | PARITYR_CLEAR);
1704 derror = SCSI_LOW_DATA_PE;
1712 cr = nsp_cr_read_1(sc->port_res, NSPR_SCBUSCR) | SCBUSCR_ACK;
1713 nsp_cr_write_1(sc->port_res, NSPR_SCBUSCR, cr);
1715 if (scsi_low_msgin(slp, ti, regv | derror) == 0)
1717 if (scsi_low_is_msgout_continue(ti, 0) != 0)
1719 scsi_low_attention(slp);
1723 /* check REQ nagated */
1724 nsp_negate_signal(sc, SCBUSMON_REQ, "msgin<REQ>");
1727 cr = nsp_cr_read_1(sc->port_res, NSPR_SCBUSCR) & (~SCBUSCR_ACK);
1728 nsp_cr_write_1(sc->port_res, NSPR_SCBUSCR, cr);
1730 /* catch a next signal */
1731 rv = nsp_expect_signal(sc, PHASE_MSGIN, SCBUSMON_REQ);
1733 while (rv > 0 && len -- > 0);
1737 slp->sl_error |= FATALIO;
1738 nsp_error(sc, "unknown scsi phase", isrc, ph, irqphs);
1746 nsp_start_timer(sc, NSP_TIMER_1MS);
1753 struct nsp_softc *sc;
1755 struct scsi_low_softc *slp = &sc->sc_sclow;
1759 if (slp->sl_Tnexus == NULL)
1762 ph = nsp_cr_read_1(sc->port_res, NSPR_SCBUSMON);
1763 switch (ph & SCBUSMON_PHMASK)
1766 if (sc->sc_dataout_timeout == 0)
1769 /* check a fifo empty */
1770 regv = bus_read_1(sc->port_res, nsp_fifosr);
1771 if ((regv & FIFOSR_FULLEMP) == 0)
1773 bus_write_1(sc->port_res, nsp_irqcr, IRQCR_FIFOCL);
1775 /* check still requested */
1776 ph = nsp_cr_read_1(sc->port_res, NSPR_SCBUSMON);
1777 if ((ph & SCBUSMON_REQ) == 0)
1780 if ((-- sc->sc_dataout_timeout) > 0)
1783 slp->sl_error |= PDMAERR;
1784 if ((slp->sl_flags & HW_WRITE_PADDING) == 0)
1786 device_printf(slp->sl_dev, "write padding required\n");
1790 tout = NSP_DELAY_MAX;
1793 ph = nsp_cr_read_1(sc->port_res, NSPR_SCBUSMON);
1794 if ((ph & SCBUSMON_PHMASK) != PHASE_DATAOUT)
1796 regv = bus_read_1(sc->port_res, nsp_fifosr);
1797 if ((regv & FIFOSR_FULLEMP) == 0)
1803 bus_write_1(sc->port_res, nsp_irqcr, IRQCR_FIFOCL);
1804 nsp_data_padding(sc, SCSI_LOW_WRITE, 32);
1806 ph = nsp_cr_read_1(sc->port_res, NSPR_SCBUSMON);
1807 if ((ph & SCBUSMON_PHMASK) == PHASE_DATAOUT)
1808 sc->sc_dataout_timeout = SCSI_LOW_TIMEOUT_HZ;