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>
54 #include <machine/cpu.h>
55 #include <machine/bus.h>
57 #include <cam/scsi/scsi_low.h>
58 #include <dev/nsp/nspreg.h>
59 #include <dev/nsp/nspvar.h>
61 /***************************************************
63 ***************************************************/
64 /* DEVICE CONFIGURATION FLAGS (MINOR)
67 * 0x02 PARITY LINE OFF
68 * 0x04 IDENTIFY MSG OFF ( = single lun)
69 * 0x08 SYNC TRANSFER OFF
72 /***************************************************
74 ***************************************************/
75 #define NSP_NTARGETS 8
78 #define NSP_MAX_DATA_SIZE (64 * 1024)
79 #define NSP_SELTIMEOUT (200)
80 #define NSP_DELAY_MAX (2 * 1000 * 1000)
81 #define NSP_DELAY_INTERVAL (1)
82 #define NSP_TIMER_1MS (1000 / 51)
84 /***************************************************
86 ***************************************************/
89 #endif /* NSP_DEBUG */
95 int device_data_write;
99 int data_phase_bypass;
101 #endif /* NSP_STATICS */
103 /***************************************************
105 ***************************************************/
106 #define NSP_READ_SUSPEND_IO 0x0001
107 #define NSP_WRITE_SUSPEND_IO 0x0002
108 #define NSP_USE_MEMIO 0x0004
109 #define NSP_READ_FIFO_INTERRUPTS 0x0010
110 #define NSP_WRITE_FIFO_INTERRUPTS 0x0020
111 #define NSP_WAIT_FOR_SELECT 0x0100
113 u_int nsp_io_control = NSP_IO_CONTROL_FLAGS;
114 int nsp_read_suspend_bytes = DEV_BSIZE;
115 int nsp_write_suspend_bytes = DEV_BSIZE;
116 int nsp_read_interrupt_bytes = 4096;
117 int nsp_write_interrupt_bytes = 4096;
119 /***************************************************
121 ***************************************************/
122 extern struct cfdriver nsp_cd;
124 /**************************************************************
126 **************************************************************/
127 #define NSP_FIFO_ON 1
128 #define NSP_FIFO_OFF 0
129 static void nsp_pio_read(struct nsp_softc *, int);
130 static void nsp_pio_write(struct nsp_softc *, int);
131 static int nsp_xfer(struct nsp_softc *, u_int8_t *, int, int, int);
132 static int nsp_msg(struct nsp_softc *, struct targ_info *, u_int);
133 static int nsp_reselected(struct nsp_softc *);
134 static int nsp_disconnected(struct nsp_softc *, struct targ_info *);
135 static void nsp_pdma_end(struct nsp_softc *, struct targ_info *);
136 static void nsphw_init(struct nsp_softc *);
137 static int nsp_target_nexus_establish(struct nsp_softc *);
138 static int nsp_lun_nexus_establish(struct nsp_softc *);
139 static int nsp_ccb_nexus_establish(struct nsp_softc *);
140 static int nsp_world_start(struct nsp_softc *, int);
141 static int nsphw_start_selection(struct nsp_softc *sc, struct slccb *);
142 static void nsphw_bus_reset(struct nsp_softc *);
143 static void nsphw_attention(struct nsp_softc *);
144 static u_int nsp_fifo_count(struct nsp_softc *);
145 static u_int nsp_request_count(struct nsp_softc *);
146 static int nsp_negate_signal(struct nsp_softc *, u_int8_t, u_char *);
147 static int nsp_expect_signal(struct nsp_softc *, u_int8_t, u_int8_t);
148 static void nsp_start_timer(struct nsp_softc *, int);
149 static void nsp_setup_fifo(struct nsp_softc *, int, int, int);
150 static int nsp_targ_init(struct nsp_softc *, struct targ_info *, int);
151 static void nsphw_selection_done_and_expect_msgout(struct nsp_softc *);
152 static void nsp_data_padding(struct nsp_softc *, int, u_int);
153 static int nsp_timeout(struct nsp_softc *);
154 static int nsp_read_fifo(struct nsp_softc *, int);
155 static int nsp_write_fifo(struct nsp_softc *, int);
156 static int nsp_phase_match(struct nsp_softc *, u_int8_t, u_int8_t);
157 static int nsp_wait_interrupt(struct nsp_softc *);
159 struct scsi_low_funcs nspfuncs = {
160 SC_LOW_INIT_T nsp_world_start,
161 SC_LOW_BUSRST_T nsphw_bus_reset,
162 SC_LOW_TARG_INIT_T nsp_targ_init,
163 SC_LOW_LUN_INIT_T NULL,
165 SC_LOW_SELECT_T nsphw_start_selection,
166 SC_LOW_NEXUS_T nsp_lun_nexus_establish,
167 SC_LOW_NEXUS_T nsp_ccb_nexus_establish,
169 SC_LOW_ATTEN_T nsphw_attention,
170 SC_LOW_MSG_T nsp_msg,
172 SC_LOW_TIMEOUT_T nsp_timeout,
173 SC_LOW_POLL_T nspintr,
178 /****************************************************
180 ****************************************************/
181 static __inline u_int8_t nsp_cr_read_1(bus_space_tag_t bst, bus_space_handle_t bsh, bus_addr_t ofs);
182 static __inline void nsp_cr_write_1(bus_space_tag_t bst, bus_space_handle_t bsh, bus_addr_t ofs, u_int8_t va);
184 static __inline u_int8_t
185 nsp_cr_read_1(bst, bsh, ofs)
187 bus_space_handle_t bsh;
191 bus_space_write_1(bst, bsh, nsp_idxr, ofs);
192 return bus_space_read_1(bst, bsh, nsp_datar);
196 nsp_cr_write_1(bus_space_tag_t bst, bus_space_handle_t bsh, bus_addr_t ofs,
200 bus_space_write_1(bst, bsh, nsp_idxr, ofs);
201 bus_space_write_1(bst, bsh, nsp_datar, va);
205 nsp_expect_signal(struct nsp_softc *sc, u_int8_t curphase, u_int8_t mask)
207 struct scsi_low_softc *slp = &sc->sc_sclow;
208 bus_space_tag_t bst = sc->sc_iot;
209 bus_space_handle_t bsh = sc->sc_ioh;
213 for (wc = 0; wc < NSP_DELAY_MAX / NSP_DELAY_INTERVAL; wc ++)
215 ph = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
216 if (ph == (u_int8_t) -1)
219 isrc = bus_space_read_1(bst, bsh, nsp_irqsr);
220 if (isrc & IRQSR_SCSI)
223 if ((ph & mask) != 0 && (ph & SCBUSMON_PHMASK) == curphase)
226 DELAY(NSP_DELAY_INTERVAL);
229 device_printf(slp->sl_dev, "nsp_expect_signal timeout\n");
235 struct nsp_softc *sc;
237 bus_space_tag_t bst = sc->sc_iot;
238 bus_space_handle_t bsh = sc->sc_ioh;
240 /* block all interrupts */
241 bus_space_write_1(bst, bsh, nsp_irqcr, IRQCR_ALLMASK);
243 /* setup SCSI interface */
244 bus_space_write_1(bst, bsh, nsp_ifselr, IFSELR_IFSEL);
246 nsp_cr_write_1(bst, bsh, NSPR_SCIENR, 0);
248 nsp_cr_write_1(bst, bsh, NSPR_XFERMR, XFERMR_IO8);
249 nsp_cr_write_1(bst, bsh, NSPR_CLKDIVR, sc->sc_iclkdiv);
251 nsp_cr_write_1(bst, bsh, NSPR_SCIENR, sc->sc_icr);
252 nsp_cr_write_1(bst, bsh, NSPR_PARITYR, sc->sc_parr);
253 nsp_cr_write_1(bst, bsh, NSPR_PTCLRR,
254 PTCLRR_ACK | PTCLRR_REQ | PTCLRR_HOST | PTCLRR_RSS);
256 /* setup fifo asic */
257 bus_space_write_1(bst, bsh, nsp_ifselr, IFSELR_REGSEL);
258 nsp_cr_write_1(bst, bsh, NSPR_TERMPWRC, 0);
259 if ((nsp_cr_read_1(bst, bsh, NSPR_OCR) & OCR_TERMPWRS) == 0)
260 nsp_cr_write_1(bst, bsh, NSPR_TERMPWRC, TERMPWRC_POWON);
262 nsp_cr_write_1(bst, bsh, NSPR_XFERMR, XFERMR_IO8);
263 nsp_cr_write_1(bst, bsh, NSPR_CLKDIVR, sc->sc_clkdiv);
264 nsp_cr_write_1(bst, bsh, NSPR_TIMERCNT, 0);
265 nsp_cr_write_1(bst, bsh, NSPR_TIMERCNT, 0);
267 nsp_cr_write_1(bst, bsh, NSPR_SYNCR, 0);
268 nsp_cr_write_1(bst, bsh, NSPR_ACKWIDTH, 0);
270 /* enable interrupts and ack them */
271 nsp_cr_write_1(bst, bsh, NSPR_SCIENR, sc->sc_icr);
272 bus_space_write_1(bst, bsh, nsp_irqcr, IRQSR_MASK);
274 nsp_setup_fifo(sc, NSP_FIFO_OFF, SCSI_LOW_READ, 0);
277 /****************************************************
279 ****************************************************/
282 struct nsp_softc *sc;
284 bus_space_tag_t bst = sc->sc_iot;
285 bus_space_handle_t bsh = sc->sc_ioh;
288 cr = nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR)/* & ~SCBUSCR_ACK */;
289 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr | SCBUSCR_ATN);
295 struct nsp_softc *sc;
297 bus_space_tag_t bst = sc->sc_iot;
298 bus_space_handle_t bsh = sc->sc_ioh;
301 bus_space_write_1(bst, bsh, nsp_irqcr, IRQCR_ALLMASK);
303 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, SCBUSCR_RST);
304 DELAY(100 * 1000); /* 100ms */
305 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, 0);
306 for (i = 0; i < 5; i ++)
307 (void) nsp_cr_read_1(bst, bsh, NSPR_IRQPHS);
309 bus_space_write_1(bst, bsh, nsp_irqcr, IRQSR_MASK);
313 nsphw_selection_done_and_expect_msgout(sc)
314 struct nsp_softc *sc;
316 struct scsi_low_softc *slp = &sc->sc_sclow;
317 bus_space_tag_t bst = sc->sc_iot;
318 bus_space_handle_t bsh = sc->sc_ioh;
320 /* clear ack counter */
322 nsp_cr_write_1(bst, bsh, NSPR_PTCLRR, PTCLRR_PT | PTCLRR_ACK |
323 PTCLRR_REQ | PTCLRR_HOST);
325 /* deassert sel and assert atten */
327 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, sc->sc_busc);
329 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR,
330 sc->sc_busc | SCBUSCR_ADIR | SCBUSCR_ACKEN);
331 SCSI_LOW_ASSERT_ATN(slp);
335 nsphw_start_selection(sc, cb)
336 struct nsp_softc *sc;
339 struct scsi_low_softc *slp = &sc->sc_sclow;
340 bus_space_tag_t bst = sc->sc_iot;
341 bus_space_handle_t bsh = sc->sc_ioh;
342 struct targ_info *ti = cb->ti;
343 register u_int8_t arbs, ph;
346 wc = sc->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
347 sc->sc_dataout_timeout = 0;
351 ph = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
352 if (ph != SCBUSMON_FREE)
356 nsp_statics.arbit_conflict_1 ++;
357 #endif /* NSP_STATICS */
358 return SCSI_LOW_START_FAIL;
361 /* start arbitration */
362 nsp_cr_write_1(bst, bsh, NSPR_ARBITS, ARBITS_EXEC);
365 SCSI_LOW_SETUP_PHASE(ti, PH_ARBSTART);
368 /* XXX: what a stupid chip! */
369 arbs = nsp_cr_read_1(bst, bsh, NSPR_ARBITS);
372 while ((arbs & (ARBITS_WIN | ARBITS_FAIL)) == 0 && wc -- > 0);
374 if ((arbs & ARBITS_WIN) == 0)
376 nsp_cr_write_1(bst, bsh, NSPR_ARBITS, ARBITS_CLR);
378 nsp_statics.arbit_conflict_2 ++;
379 #endif /* NSP_STATICS */
380 return SCSI_LOW_START_FAIL;
383 /* assert select line */
384 SCSI_LOW_SETUP_PHASE(ti, PH_SELSTART);
385 scsi_low_arbit_win(slp);
389 nsp_cr_write_1(bst, bsh, NSPR_DATA,
390 sc->sc_idbit | (1 << ti->ti_id));
391 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR,
392 SCBUSCR_SEL | SCBUSCR_BSY | sc->sc_busc);
394 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, SCBUSCR_SEL |
395 SCBUSCR_BSY | SCBUSCR_DOUT | sc->sc_busc);
396 nsp_cr_write_1(bst, bsh, NSPR_ARBITS, ARBITS_CLR);
398 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR,
399 SCBUSCR_SEL | SCBUSCR_DOUT | sc->sc_busc);
402 if ((nsp_io_control & NSP_WAIT_FOR_SELECT) != 0)
404 #define NSP_FIRST_SEL_WAIT 300
405 #define NSP_SEL_CHECK_INTERVAL 10
407 /* wait for a selection response */
408 for (wc = 0; wc < NSP_FIRST_SEL_WAIT / NSP_SEL_CHECK_INTERVAL;
411 ph = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
412 if ((ph & SCBUSMON_BSY) == 0)
414 DELAY(NSP_SEL_CHECK_INTERVAL);
419 ph = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
420 if ((ph & SCBUSMON_BSY) != 0)
422 nsphw_selection_done_and_expect_msgout(sc);
425 SCSI_LOW_SETUP_PHASE(ti, PH_SELECTED);
426 return SCSI_LOW_START_OK;
432 /* check a selection timeout */
433 nsp_start_timer(sc, NSP_TIMER_1MS);
435 return SCSI_LOW_START_OK;
439 nsp_world_start(sc, fdone)
440 struct nsp_softc *sc;
443 struct scsi_low_softc *slp = &sc->sc_sclow;
448 if ((slp->sl_cfgflags & CFG_NOATTEN) == 0)
449 sc->sc_busc = SCBUSCR_ATN;
453 if ((slp->sl_cfgflags & CFG_NOPARITY) == 0)
454 sc->sc_parr = PARITYR_ENABLE | PARITYR_CLEAR;
458 sc->sc_icr = (SCIENR_SCCHG | SCIENR_RESEL | SCIENR_RST);
461 scsi_low_bus_reset(slp);
466 struct ncp_synch_data {
473 static struct ncp_synch_data ncp_sync_data_40M[] = {
474 {0x0c,0x0c,0x1,0}, /* 20MB 50ns*/
475 {0x19,0x19,0x3,1}, /* 10MB 100ns*/
476 {0x1a,0x25,0x5,2}, /* 7.5MB 150ns*/
477 {0x26,0x32,0x7,3}, /* 5MB 200ns*/
481 static struct ncp_synch_data ncp_sync_data_20M[] = {
482 {0x19,0x19,0x1,0}, /* 10MB 100ns*/
483 {0x1a,0x25,0x2,0}, /* 7.5MB 150ns*/
484 {0x26,0x32,0x3,1}, /* 5MB 200ns*/
490 struct nsp_softc *sc;
491 struct targ_info *ti;
494 bus_space_tag_t bst = sc->sc_iot;
495 bus_space_handle_t bsh = sc->sc_ioh;
496 struct ncp_synch_data *sdp;
497 struct nsp_targ_info *nti = (void *) ti;
498 u_int period, offset;
501 if ((msg & SCSI_LOW_MSG_WIDE) != 0)
503 if (ti->ti_width != SCSI_LOW_BUS_WIDTH_8)
505 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
511 if ((msg & SCSI_LOW_MSG_SYNCH) == 0)
514 period = ti->ti_maxsynch.period;
515 offset = ti->ti_maxsynch.offset;
516 if (sc->sc_iclkdiv == CLKDIVR_20M)
517 sdp = &ncp_sync_data_20M[0];
519 sdp = &ncp_sync_data_40M[0];
521 for (i = 0; sdp->max_period != 0; i ++, sdp ++)
523 if (period >= sdp->min_period && period <= sdp->max_period)
527 if (period != 0 && sdp->max_period == 0)
530 * NO proper period/offset found,
531 * Retry neg with the target.
533 ti->ti_maxsynch.period = 0;
534 ti->ti_maxsynch.offset = 0;
535 nti->nti_reg_syncr = 0;
536 nti->nti_reg_ackwidth = 0;
541 nti->nti_reg_syncr = (sdp->chip_period << SYNCR_PERS) |
542 (offset & SYNCR_OFFM);
543 nti->nti_reg_ackwidth = sdp->ack_width;
547 nsp_cr_write_1(bst, bsh, NSPR_SYNCR, nti->nti_reg_syncr);
548 nsp_cr_write_1(bst, bsh, NSPR_ACKWIDTH, nti->nti_reg_ackwidth);
553 nsp_targ_init(sc, ti, action)
554 struct nsp_softc *sc;
555 struct targ_info *ti;
558 struct nsp_targ_info *nti = (void *) ti;
560 if (action == SCSI_LOW_INFO_ALLOC || action == SCSI_LOW_INFO_REVOKE)
562 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
563 ti->ti_maxsynch.period = 100 / 4;
564 ti->ti_maxsynch.offset = 15;
565 nti->nti_reg_syncr = 0;
566 nti->nti_reg_ackwidth = 0;
572 nsp_start_timer(sc, time)
573 struct nsp_softc *sc;
576 bus_space_tag_t bst = sc->sc_iot;
577 bus_space_handle_t bsh = sc->sc_ioh;
580 nsp_cr_write_1(bst, bsh, NSPR_TIMERCNT, time);
583 /**************************************************************
584 * General probe attach
585 **************************************************************/
587 nspprobesubr(iot, ioh, dvcfg)
589 bus_space_handle_t ioh;
594 regv = bus_space_read_1(iot, ioh, nsp_fifosr);
595 if (regv < 0x11 || regv >= 0x20)
602 struct nsp_softc *sc;
604 struct scsi_low_softc *slp = &sc->sc_sclow;
608 sc->sc_idbit = (1 << slp->sl_hostid);
609 slp->sl_flags |= HW_READ_PADDING;
610 slp->sl_funcs = &nspfuncs;
611 sc->sc_tmaxcnt = SCSI_LOW_MIN_TOUT * 1000 * 1000; /* default */
613 (void) scsi_low_attach(slp, 0, NSP_NTARGETS, NSP_NLUNS,
614 sizeof(struct nsp_targ_info), 0);
617 /**************************************************************
619 **************************************************************/
622 struct nsp_softc *sc;
624 bus_space_tag_t bst = sc->sc_iot;
625 bus_space_handle_t bsh = sc->sc_ioh;
628 nsp_cr_write_1(bst, bsh, NSPR_PTCLRR, PTCLRR_RSS_ACK | PTCLRR_PT);
629 count = bus_space_read_1(bst, bsh, nsp_datar);
630 count += (((u_int) bus_space_read_1(bst, bsh, nsp_datar)) << 8);
631 count += (((u_int) bus_space_read_1(bst, bsh, nsp_datar)) << 16);
636 nsp_request_count(sc)
637 struct nsp_softc *sc;
639 bus_space_tag_t bst = sc->sc_iot;
640 bus_space_handle_t bsh = sc->sc_ioh;
643 nsp_cr_write_1(bst, bsh, NSPR_PTCLRR, PTCLRR_RSS_REQ | PTCLRR_PT);
644 count = bus_space_read_1(bst, bsh, nsp_datar);
645 count += (((u_int) bus_space_read_1(bst, bsh, nsp_datar)) << 8);
646 count += (((u_int) bus_space_read_1(bst, bsh, nsp_datar)) << 16);
651 nsp_setup_fifo(sc, on, direction, datalen)
652 struct nsp_softc *sc;
659 sc->sc_suspendio = 0;
660 if (on == NSP_FIFO_OFF)
662 xfermode = XFERMR_IO8;
666 /* check if suspend io OK ? */
669 if (direction == SCSI_LOW_READ)
671 if ((nsp_io_control & NSP_READ_SUSPEND_IO) != 0 &&
672 (datalen % nsp_read_suspend_bytes) == 0)
673 sc->sc_suspendio = nsp_read_suspend_bytes;
677 if ((nsp_io_control & NSP_WRITE_SUSPEND_IO) != 0 &&
678 (datalen % nsp_write_suspend_bytes) == 0)
679 sc->sc_suspendio = nsp_write_suspend_bytes;
683 /* determine a transfer type */
684 if (datalen < DEV_BSIZE || (datalen & 3) != 0)
686 if (sc->sc_memh != 0 &&
687 (nsp_io_control & NSP_USE_MEMIO) != 0)
688 xfermode = XFERMR_XEN | XFERMR_MEM8;
690 xfermode = XFERMR_XEN | XFERMR_IO8;
694 if (sc->sc_memh != 0 &&
695 (nsp_io_control & NSP_USE_MEMIO) != 0)
696 xfermode = XFERMR_XEN | XFERMR_MEM32;
698 xfermode = XFERMR_XEN | XFERMR_IO32;
700 if (sc->sc_suspendio > 0)
701 xfermode |= XFERMR_FIFOEN;
705 sc->sc_xfermr = xfermode;
706 nsp_cr_write_1(sc->sc_iot, sc->sc_ioh, NSPR_XFERMR, sc->sc_xfermr);
711 struct nsp_softc *sc;
712 struct targ_info *ti;
714 struct scsi_low_softc *slp = &sc->sc_sclow;
715 struct slccb *cb = slp->sl_Qnexus;
718 sc->sc_dataout_timeout = 0;
719 slp->sl_flags &= ~HW_PDMASTART;
720 nsp_setup_fifo(sc, NSP_FIFO_OFF, SCSI_LOW_READ, 0);
721 if ((sc->sc_icr & SCIENR_FIFO) != 0)
723 sc->sc_icr &= ~SCIENR_FIFO;
724 nsp_cr_write_1(sc->sc_iot, sc->sc_ioh, NSPR_SCIENR, sc->sc_icr);
729 slp->sl_error |= PDMAERR;
733 if (ti->ti_phase == PH_DATA)
735 cnt = nsp_fifo_count(sc);
736 if (slp->sl_scp.scp_direction == SCSI_LOW_WRITE)
738 len = sc->sc_cnt - cnt;
739 if (sc->sc_cnt >= cnt &&
740 slp->sl_scp.scp_datalen + len <=
741 cb->ccb_scp.scp_datalen)
743 slp->sl_scp.scp_data -= len;
744 slp->sl_scp.scp_datalen += len;
748 slp->sl_error |= PDMAERR;
749 device_printf(slp->sl_dev,
750 "len %x >= datalen %x\n",
751 len, slp->sl_scp.scp_datalen);
754 else if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
756 if (sc->sc_cnt != cnt ||
757 sc->sc_cnt > cb->ccb_scp.scp_datalen)
759 slp->sl_error |= PDMAERR;
760 device_printf(slp->sl_dev,
761 "data read count error %x != %x (%x)\n",
763 cb->ccb_scp.scp_datalen);
767 scsi_low_data_finish(slp);
772 device_printf(slp->sl_dev, "data phase miss\n");
773 slp->sl_error |= PDMAERR;
777 #define RFIFO_CRIT 64
778 #define WFIFO_CRIT 32
781 nsp_data_padding(sc, direction, count)
782 struct nsp_softc *sc;
786 bus_space_tag_t bst = sc->sc_iot;
787 bus_space_handle_t bsh = sc->sc_ioh;
789 if (count > NSP_MAX_DATA_SIZE)
790 count = NSP_MAX_DATA_SIZE;
792 nsp_cr_write_1(bst, bsh, NSPR_XFERMR, XFERMR_XEN | XFERMR_IO8);
793 if (direction == SCSI_LOW_READ)
796 (void) bus_space_read_1(bst, bsh, nsp_fifodr);
801 (void) bus_space_write_1(bst, bsh, nsp_fifodr, 0);
803 nsp_cr_write_1(bst, bsh, NSPR_XFERMR, sc->sc_xfermr);
807 nsp_read_fifo(sc, suspendio)
808 struct nsp_softc *sc;
811 struct scsi_low_softc *slp = &sc->sc_sclow;
812 bus_space_tag_t bst = sc->sc_iot;
813 bus_space_handle_t bsh = sc->sc_ioh;
816 res = nsp_fifo_count(sc);
817 if (res == sc->sc_cnt)
821 if (res < sc->sc_cnt || res == (u_int) -1)
823 device_printf(slp->sl_dev,
824 "strange fifo ack count 0x%x < 0x%x\n", res, sc->sc_cnt);
827 #endif /* NSP_DEBUG */
829 res = res - sc->sc_cnt;
830 if (res > slp->sl_scp.scp_datalen)
832 if ((slp->sl_error & PDMAERR) == 0)
834 device_printf(slp->sl_dev, "data overrun 0x%x > 0x%x\n",
835 res, slp->sl_scp.scp_datalen);
838 slp->sl_error |= PDMAERR;
839 slp->sl_scp.scp_datalen = 0;
841 if ((slp->sl_flags & HW_READ_PADDING) == 0)
843 device_printf(slp->sl_dev, "read padding required\n");
847 nsp_data_padding(sc, SCSI_LOW_READ, res);
849 return 1; /* padding start */
852 if (suspendio > 0 && slp->sl_scp.scp_datalen >= suspendio)
855 if ((sc->sc_xfermr & (XFERMR_MEM32 | XFERMR_MEM8)) != 0)
857 if ((sc->sc_xfermr & XFERMR_MEM32) != 0)
860 bus_space_read_region_4(sc->sc_memt, sc->sc_memh, 0,
861 (u_int32_t *) slp->sl_scp.scp_data, res >> 2);
865 bus_space_read_region_1(sc->sc_memt, sc->sc_memh, 0,
866 (u_int8_t *) slp->sl_scp.scp_data, res);
871 if ((sc->sc_xfermr & XFERMR_IO32) != 0)
874 bus_space_read_multi_4(bst, bsh, nsp_fifodr,
875 (u_int32_t *) slp->sl_scp.scp_data, res >> 2);
879 bus_space_read_multi_1(bst, bsh, nsp_fifodr,
880 (u_int8_t *) slp->sl_scp.scp_data, res);
884 if (nsp_cr_read_1(bst, bsh, NSPR_PARITYR) & PARITYR_PE)
886 nsp_cr_write_1(bst, bsh, NSPR_PARITYR,
887 PARITYR_ENABLE | PARITYR_CLEAR);
888 scsi_low_assert_msg(slp, slp->sl_Tnexus, SCSI_LOW_MSG_ERROR, 1);
891 slp->sl_scp.scp_data += res;
892 slp->sl_scp.scp_datalen -= res;
898 nsp_write_fifo(sc, suspendio)
899 struct nsp_softc *sc;
902 struct scsi_low_softc *slp = &sc->sc_sclow;
903 bus_space_tag_t bst = sc->sc_iot;
904 bus_space_handle_t bsh = sc->sc_ioh;
906 register u_int8_t stat;
911 if ((slp->sl_scp.scp_datalen % WFIFO_CRIT) != 0)
913 device_printf(slp->sl_dev,
914 "strange write length 0x%x\n",
915 slp->sl_scp.scp_datalen);
917 #endif /* NSP_DEBUG */
918 res = slp->sl_scp.scp_datalen % suspendio;
929 if (res > slp->sl_scp.scp_datalen)
930 res = slp->sl_scp.scp_datalen;
932 /* XXX: reconfirm! */
933 stat = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON) & SCBUSMON_PHMASK;
934 if (stat != PHASE_DATAOUT)
937 if ((sc->sc_xfermr & (XFERMR_MEM32 | XFERMR_MEM8)) != 0)
939 if ((sc->sc_xfermr & XFERMR_MEM32) != 0)
941 bus_space_write_region_4(sc->sc_memt, sc->sc_memh, 0,
942 (u_int32_t *) slp->sl_scp.scp_data, res >> 2);
946 bus_space_write_region_1(sc->sc_memt, sc->sc_memh, 0,
947 (u_int8_t *) slp->sl_scp.scp_data, res);
952 if ((sc->sc_xfermr & XFERMR_IO32) != 0)
954 bus_space_write_multi_4(bst, bsh, nsp_fifodr,
955 (u_int32_t *) slp->sl_scp.scp_data, res >> 2);
959 bus_space_write_multi_1(bst, bsh, nsp_fifodr,
960 (u_int8_t *) slp->sl_scp.scp_data, res);
964 slp->sl_scp.scp_datalen -= res;
965 slp->sl_scp.scp_data += res;
971 nsp_wait_interrupt(sc)
972 struct nsp_softc *sc;
974 bus_space_tag_t bst = sc->sc_iot;
975 bus_space_handle_t bsh = sc->sc_ioh;
977 register u_int8_t isrc;
979 for (tout = 0; tout < DEV_BSIZE / 10; tout ++)
981 isrc = bus_space_read_1(bst, bsh, nsp_irqsr);
982 if ((isrc & (IRQSR_SCSI | IRQSR_FIFO)) != 0)
984 if ((isrc & IRQSR_FIFO) != 0)
986 bus_space_write_1(bst, bsh,
987 nsp_irqcr, IRQCR_FIFOCL);
997 nsp_pio_read(sc, suspendio)
998 struct nsp_softc *sc;
1001 struct scsi_low_softc *slp = &sc->sc_sclow;
1002 bus_space_tag_t bst = sc->sc_iot;
1003 bus_space_handle_t bsh = sc->sc_ioh;
1004 int tout, padding, datalen;
1005 register u_int8_t stat, fstat;
1008 tout = sc->sc_tmaxcnt;
1009 slp->sl_flags |= HW_PDMASTART;
1010 datalen = slp->sl_scp.scp_datalen;
1015 stat = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
1016 if (stat == (u_int8_t) -1)
1019 /* out of data phase */
1020 if ((stat & SCBUSMON_PHMASK) != PHASE_DATAIN)
1022 nsp_read_fifo(sc, 0);
1027 fstat = bus_space_read_1(bst, bsh, nsp_fifosr);
1028 if ((fstat & FIFOSR_FULLEMP) != 0)
1030 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1032 bus_space_write_1(bst, bsh, nsp_irqcr,
1038 padding |= nsp_read_fifo(sc, suspendio);
1042 padding |= nsp_read_fifo(sc, 0);
1045 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1050 if (padding == 0 && slp->sl_scp.scp_datalen <= 0)
1053 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1061 device_printf(slp->sl_dev, "nsp_pio_read: timeout\n");
1067 if (slp->sl_scp.scp_datalen > 0 &&
1068 slp->sl_scp.scp_datalen > datalen - nsp_read_interrupt_bytes)
1070 if (nsp_wait_interrupt(sc) != 0)
1076 nsp_pio_write(sc, suspendio)
1077 struct nsp_softc *sc;
1080 struct scsi_low_softc *slp = &sc->sc_sclow;
1081 bus_space_tag_t bst = sc->sc_iot;
1082 bus_space_handle_t bsh = sc->sc_ioh;
1083 u_int rcount, acount;
1085 register u_int8_t stat, fstat;
1087 tout = sc->sc_tmaxcnt;
1088 slp->sl_flags |= HW_PDMASTART;
1089 datalen = slp->sl_scp.scp_datalen;
1094 stat = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON) & SCBUSMON_PHMASK;
1095 if (stat != PHASE_DATAOUT)
1098 if (slp->sl_scp.scp_datalen <= 0)
1100 if (sc->sc_dataout_timeout == 0)
1101 sc->sc_dataout_timeout = SCSI_LOW_TIMEOUT_HZ;
1105 fstat = bus_space_read_1(bst, bsh, nsp_fifosr);
1106 if ((fstat & FIFOSR_FULLEMP) != 0)
1108 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1110 bus_space_write_1(bst, bsh, nsp_irqcr,
1117 * To avoid timeout of pcmcia bus
1118 * (not scsi bus!), we should check
1119 * the scsi device sends us request
1120 * signals, which means the scsi device
1121 * is ready to recieve data without
1124 if ((slp->sl_scp.scp_datalen % suspendio) == 0)
1127 * fill the nsp fifo, and waiting for
1130 nsp_write_fifo(sc, 0);
1135 * check the request singals.
1137 acount = nsp_fifo_count(sc);
1138 rcount = nsp_request_count(sc);
1139 if (rcount <= acount)
1141 nsp_write_fifo(sc, 0);
1143 nsp_statics.device_busy ++;
1144 #endif /* NSP_STATICS */
1148 nsp_write_fifo(sc, suspendio);
1150 nsp_statics.device_data_write ++;
1151 #endif /* NSP_STATICS */
1157 nsp_write_fifo(sc, 0);
1160 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1165 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1173 device_printf(slp->sl_dev, "nsp_pio_write: timeout\n");
1178 if (slp->sl_scp.scp_datalen > 0 &&
1179 slp->sl_scp.scp_datalen > datalen - nsp_write_interrupt_bytes)
1181 if (nsp_wait_interrupt(sc) != 0)
1187 nsp_negate_signal(struct nsp_softc *sc, u_int8_t mask, u_char *s)
1189 struct scsi_low_softc *slp = &sc->sc_sclow;
1190 bus_space_tag_t bst = sc->sc_iot;
1191 bus_space_handle_t bsh = sc->sc_ioh;
1195 for (wc = 0; wc < NSP_DELAY_MAX / NSP_DELAY_INTERVAL; wc ++)
1197 regv = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
1198 if (regv == (u_int8_t) -1)
1200 if ((regv & mask) == 0)
1202 DELAY(NSP_DELAY_INTERVAL);
1205 device_printf(slp->sl_dev, "%s nsp_negate_signal timeout\n", s);
1210 nsp_xfer(sc, buf, len, phase, clear_atn)
1211 struct nsp_softc *sc;
1217 bus_space_tag_t bst = sc->sc_iot;
1218 bus_space_handle_t bsh = sc->sc_ioh;
1221 for (ptr = 0; len > 0; len --, ptr ++)
1223 rv = nsp_expect_signal(sc, phase, SCBUSMON_REQ);
1227 if (len == 1 && clear_atn != 0)
1229 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR,
1230 SCBUSCR_ADIR | SCBUSCR_ACKEN);
1231 SCSI_LOW_DEASSERT_ATN(&sc->sc_sclow);
1234 if (phase & SCBUSMON_IO)
1236 buf[ptr] = nsp_cr_read_1(bst, bsh, NSPR_DATAACK);
1240 nsp_cr_write_1(bst, bsh, NSPR_DATAACK, buf[ptr]);
1242 nsp_negate_signal(sc, SCBUSMON_ACK, "xfer<ACK>");
1249 /**************************************************************
1250 * disconnect & reselect (HW low)
1251 **************************************************************/
1254 struct nsp_softc *sc;
1256 struct scsi_low_softc *slp = &sc->sc_sclow;
1257 bus_space_tag_t bst = sc->sc_iot;
1258 bus_space_handle_t bsh = sc->sc_ioh;
1259 struct targ_info *ti;
1263 sid = (u_int) nsp_cr_read_1(bst, bsh, NSPR_RESELR);
1264 sid &= ~sc->sc_idbit;
1266 if ((ti = scsi_low_reselected(slp, sid)) == NULL)
1269 nsp_negate_signal(sc, SCBUSMON_SEL, "reselect<SEL>");
1271 cr = nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR);
1272 cr &= ~(SCBUSCR_BSY | SCBUSCR_ATN);
1273 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1274 cr |= SCBUSCR_ADIR | SCBUSCR_ACKEN;
1275 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1278 nsp_statics.reselect ++;
1279 #endif /* NSP_STATCIS */
1284 nsp_disconnected(sc, ti)
1285 struct nsp_softc *sc;
1286 struct targ_info *ti;
1288 struct scsi_low_softc *slp = &sc->sc_sclow;
1289 bus_space_tag_t bst = sc->sc_iot;
1290 bus_space_handle_t bsh = sc->sc_ioh;
1292 nsp_cr_write_1(bst, bsh, NSPR_PTCLRR, PTCLRR_PT | PTCLRR_ACK |
1293 PTCLRR_REQ | PTCLRR_HOST);
1294 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1296 sc->sc_icr &= ~SCIENR_FIFO;
1297 nsp_cr_write_1(bst, bsh, NSPR_SCIENR, sc->sc_icr);
1300 sc->sc_dataout_timeout = 0;
1302 nsp_statics.disconnect ++;
1303 #endif /* NSP_STATICS */
1304 scsi_low_disconnected(slp, ti);
1308 /**************************************************************
1310 **************************************************************/
1311 static void nsp_error(struct nsp_softc *, u_char *, u_int8_t, u_int8_t, u_int8_t);
1314 nsp_error(struct nsp_softc * sc, u_char *s, u_int8_t isrc, u_int8_t ph,
1317 struct scsi_low_softc *slp = &sc->sc_sclow;
1319 device_printf(slp->sl_dev, "%s\n", s);
1320 device_printf(slp->sl_dev, "isrc 0x%x scmon 0x%x irqphs 0x%x\n",
1321 (u_int) isrc, (u_int) ph, (u_int) irqphs);
1325 nsp_target_nexus_establish(sc)
1326 struct nsp_softc *sc;
1328 struct scsi_low_softc *slp = &sc->sc_sclow;
1329 bus_space_tag_t bst = sc->sc_iot;
1330 bus_space_handle_t bsh = sc->sc_ioh;
1331 struct targ_info *ti = slp->sl_Tnexus;
1332 struct nsp_targ_info *nti = (void *) ti;
1334 /* setup synch transfer registers */
1335 nsp_cr_write_1(bst, bsh, NSPR_SYNCR, nti->nti_reg_syncr);
1336 nsp_cr_write_1(bst, bsh, NSPR_ACKWIDTH, nti->nti_reg_ackwidth);
1338 /* setup pdma fifo (minimum) */
1339 nsp_setup_fifo(sc, NSP_FIFO_ON, SCSI_LOW_READ, 0);
1344 nsp_lun_nexus_establish(sc)
1345 struct nsp_softc *sc;
1352 nsp_ccb_nexus_establish(sc)
1353 struct nsp_softc *sc;
1355 struct scsi_low_softc *slp = &sc->sc_sclow;
1356 struct slccb *cb = slp->sl_Qnexus;
1358 sc->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
1360 /* setup pdma fifo */
1361 nsp_setup_fifo(sc, NSP_FIFO_ON,
1362 slp->sl_scp.scp_direction, slp->sl_scp.scp_datalen);
1364 if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
1366 if (sc->sc_suspendio > 0 &&
1367 (nsp_io_control & NSP_READ_FIFO_INTERRUPTS) != 0)
1369 sc->sc_icr |= SCIENR_FIFO;
1370 nsp_cr_write_1(sc->sc_iot, sc->sc_ioh,
1371 NSPR_SCIENR, sc->sc_icr);
1376 if (sc->sc_suspendio > 0 &&
1377 (nsp_io_control & NSP_WRITE_FIFO_INTERRUPTS) != 0)
1379 sc->sc_icr |= SCIENR_FIFO;
1380 nsp_cr_write_1(sc->sc_iot, sc->sc_ioh,
1381 NSPR_SCIENR, sc->sc_icr);
1388 nsp_phase_match(struct nsp_softc *sc, u_int8_t phase, u_int8_t stat)
1390 struct scsi_low_softc *slp = &sc->sc_sclow;
1392 if ((stat & SCBUSMON_PHMASK) != phase)
1394 device_printf(slp->sl_dev, "phase mismatch 0x%x != 0x%x\n",
1395 (u_int) phase, (u_int) stat);
1399 if ((stat & SCBUSMON_REQ) == 0)
1409 struct nsp_softc *sc = arg;
1410 struct scsi_low_softc *slp = &sc->sc_sclow;
1411 bus_space_tag_t bst = sc->sc_iot;
1412 bus_space_handle_t bsh = sc->sc_ioh;
1413 struct targ_info *ti;
1415 u_int derror, flags;
1417 u_int8_t isrc, ph, irqphs, cr, regv;
1419 /*******************************************
1421 *******************************************/
1422 if (slp->sl_flags & HW_INACTIVE)
1425 bus_space_write_1(bst, bsh, nsp_irqcr, IRQCR_IRQDIS);
1426 isrc = bus_space_read_1(bst, bsh, nsp_irqsr);
1427 if (isrc == (u_int8_t) -1 || (isrc & IRQSR_MASK) == 0)
1429 bus_space_write_1(bst, bsh, nsp_irqcr, 0);
1434 * Do not read an irqphs register if no scsi phase interrupt.
1435 * Unless, you should lose a scsi phase interrupt.
1437 ph = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
1438 if ((isrc & IRQSR_SCSI) != 0)
1440 irqphs = nsp_cr_read_1(bst, bsh, NSPR_IRQPHS);
1446 * timer interrupt handler (scsi vs timer interrupts)
1448 if (sc->sc_timer != 0)
1450 nsp_cr_write_1(bst, bsh, NSPR_TIMERCNT, 0);
1451 nsp_cr_write_1(bst, bsh, NSPR_TIMERCNT, 0);
1455 /* check a timer interrupt */
1457 if ((isrc & IRQSR_TIMER) != 0)
1459 if ((isrc & IRQSR_MASK) == IRQSR_TIMER && sc->sc_seltout == 0)
1461 bus_space_write_1(bst, bsh, nsp_irqcr, IRQCR_TIMERCL);
1464 regv |= IRQCR_TIMERCL;
1467 /* check a fifo interrupt */
1468 if ((isrc & IRQSR_FIFO) != 0)
1470 regv |= IRQCR_FIFOCL;
1473 /* OK. enable all interrupts */
1474 bus_space_write_1(bst, bsh, nsp_irqcr, regv);
1476 /*******************************************
1478 *******************************************/
1482 nsp_error(sc, "current status", isrc, ph, irqphs);
1483 scsi_low_print(slp, NULL);
1486 kdb_enter(KDB_WHY_CAM, "nsp");
1489 #endif /* NSP_DEBUG */
1491 /*******************************************
1492 * Parse hardware SCSI irq reasons register
1493 *******************************************/
1494 if ((isrc & IRQSR_SCSI) != 0)
1496 if ((irqphs & IRQPHS_RST) != 0)
1498 scsi_low_restart(slp, SCSI_LOW_RESTART_SOFT,
1499 "bus reset (power off?)");
1503 if ((irqphs & IRQPHS_RSEL) != 0)
1505 bus_space_write_1(bst, bsh, nsp_irqcr, IRQCR_RESCL);
1506 if (nsp_reselected(sc) == EJUSTRETURN)
1510 if ((irqphs & (IRQPHS_PCHG | IRQPHS_LBF)) == 0)
1514 /*******************************************
1516 *******************************************/
1517 if ((ti = slp->sl_Tnexus) == NULL)
1519 /* unknown scsi phase changes */
1520 nsp_error(sc, "unknown scsi phase changes", isrc, ph, irqphs);
1524 /*******************************************
1525 * aribitration & selection
1526 *******************************************/
1527 switch (ti->ti_phase)
1530 if ((ph & SCBUSMON_BSY) == 0)
1532 if (sc->sc_seltout >= NSP_SELTIMEOUT)
1535 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, 0);
1536 return nsp_disconnected(sc, ti);
1539 nsp_start_timer(sc, NSP_TIMER_1MS);
1543 SCSI_LOW_SETUP_PHASE(ti, PH_SELECTED);
1544 nsphw_selection_done_and_expect_msgout(sc);
1548 if ((isrc & IRQSR_SCSI) == 0)
1551 nsp_target_nexus_establish(sc);
1555 if ((isrc & IRQSR_SCSI) == 0)
1558 nsp_target_nexus_establish(sc);
1559 if ((ph & SCBUSMON_PHMASK) != PHASE_MSGIN)
1561 device_printf(slp->sl_dev,
1562 "unexpected phase after reselect\n");
1563 slp->sl_error |= FATALIO;
1564 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 1);
1570 if ((isrc & IRQSR_SCSI) != 0)
1572 if ((isrc & IRQSR_FIFO) != 0)
1574 if (NSP_IS_PHASE_DATA(ph) == 0)
1576 irqphs = (ph & IRQPHS_PHMASK);
1582 if ((isrc & IRQSR_SCSI) == 0)
1587 /*******************************************
1588 * data phase control
1589 *******************************************/
1590 if (slp->sl_flags & HW_PDMASTART)
1592 if ((isrc & IRQSR_SCSI) != 0 &&
1593 NSP_IS_IRQPHS_DATA(irqphs) == 0)
1595 if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
1596 nsp_pio_read(sc, 0);
1597 nsp_pdma_end(sc, ti);
1601 /*******************************************
1603 *******************************************/
1604 if (slp->sl_msgphase != 0 && (irqphs & IRQPHS_LBF) != 0)
1605 return nsp_disconnected(sc, ti);
1607 /* check unexpected bus free state */
1610 nsp_error(sc, "unexpected bus free", isrc, ph, irqphs);
1611 return nsp_disconnected(sc, ti);
1614 /* check normal scsi phase */
1615 switch (irqphs & IRQPHS_PHMASK)
1618 if (nsp_phase_match(sc, PHASE_CMD, ph) != 0)
1621 SCSI_LOW_SETUP_PHASE(ti, PH_CMD);
1622 if (scsi_low_cmd(slp, ti) != 0)
1624 scsi_low_attention(slp);
1627 nsp_cr_write_1(bst, bsh, NSPR_CMDCR, CMDCR_PTCLR);
1628 for (len = 0; len < slp->sl_scp.scp_cmdlen; len ++)
1629 nsp_cr_write_1(bst, bsh, NSPR_CMDDR,
1630 slp->sl_scp.scp_cmd[len]);
1632 nsp_cr_write_1(bst, bsh, NSPR_CMDCR, CMDCR_PTCLR | CMDCR_EXEC);
1635 case IRQPHS_DATAOUT:
1636 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1637 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_WRITE) != 0)
1639 scsi_low_attention(slp);
1642 nsp_pio_write(sc, sc->sc_suspendio);
1646 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1647 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_READ) != 0)
1649 scsi_low_attention(slp);
1652 nsp_pio_read(sc, sc->sc_suspendio);
1656 if (nsp_phase_match(sc, PHASE_STATUS, ph) != 0)
1659 SCSI_LOW_SETUP_PHASE(ti, PH_STAT);
1660 regv = nsp_cr_read_1(bst, bsh, NSPR_DATA);
1661 if (nsp_cr_read_1(bst, bsh, NSPR_PARITYR) & PARITYR_PE)
1663 nsp_cr_write_1(bst, bsh, NSPR_PARITYR,
1664 PARITYR_ENABLE | PARITYR_CLEAR);
1665 derror = SCSI_LOW_DATA_PE;
1671 cr = SCBUSCR_ACK | nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR);
1672 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1674 if (scsi_low_statusin(slp, ti, derror | regv) != 0)
1676 scsi_low_attention(slp);
1679 /* check REQ nagated */
1680 nsp_negate_signal(sc, SCBUSMON_REQ, "statin<REQ>");
1683 cr = nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR) & (~SCBUSCR_ACK);
1684 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1688 if (nsp_phase_match(sc, PHASE_MSGOUT, ph) != 0)
1691 #ifdef NSP_MSGOUT_SERIALIZE
1694 * NSP invoke interrupts only in the case of scsi phase changes,
1695 * therefore we should poll the scsi phase here to catch
1696 * the next "msg out" if exists (no scsi phase changes).
1700 SCSI_LOW_SETUP_PHASE(ti, PH_MSGOUT);
1701 flags = (ti->ti_ophase != ti->ti_phase) ?
1702 SCSI_LOW_MSGOUT_INIT : 0;
1703 len = scsi_low_msgout(slp, ti, flags);
1705 if (len > 1 && slp->sl_atten == 0)
1707 scsi_low_attention(slp);
1710 if (nsp_xfer(sc, ti->ti_msgoutstr, len, PHASE_MSGOUT,
1711 slp->sl_clear_atten) != 0)
1713 slp->sl_error |= FATALIO;
1714 nsp_error(sc, "MSGOUT: xfer short",
1718 /* catch a next signal */
1719 rv = nsp_expect_signal(sc, PHASE_MSGOUT, SCBUSMON_REQ);
1721 while (rv > 0 && len -- > 0);
1723 #else /* !NSP_MSGOUT_SERIALIZE */
1724 SCSI_LOW_SETUP_PHASE(ti, PH_MSGOUT);
1725 flags = SCSI_LOW_MSGOUT_UNIFY;
1726 if (ti->ti_ophase != ti->ti_phase)
1727 flags |= SCSI_LOW_MSGOUT_INIT;
1728 len = scsi_low_msgout(slp, ti, flags);
1730 if (len > 1 && slp->sl_atten == 0)
1732 scsi_low_attention(slp);
1735 if (nsp_xfer(sc, ti->ti_msgoutstr, len, PHASE_MSGOUT,
1736 slp->sl_clear_atten) != 0)
1738 nsp_error(sc, "MSGOUT: xfer short", isrc, ph, irqphs);
1741 #endif /* !NSP_MSGOUT_SERIALIZE */
1745 if (nsp_phase_match(sc, PHASE_MSGIN, ph) != 0)
1750 * NSP invoke interrupts only in the case of scsi phase changes,
1751 * therefore we should poll the scsi phase here to catch
1752 * the next "msg in" if exists (no scsi phase changes).
1756 SCSI_LOW_SETUP_PHASE(ti, PH_MSGIN);
1759 regv = nsp_cr_read_1(bst, bsh, NSPR_DATA);
1760 if (nsp_cr_read_1(bst, bsh, NSPR_PARITYR) & PARITYR_PE)
1762 nsp_cr_write_1(bst, bsh,
1764 PARITYR_ENABLE | PARITYR_CLEAR);
1765 derror = SCSI_LOW_DATA_PE;
1773 cr = nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR) | SCBUSCR_ACK;
1774 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1776 if (scsi_low_msgin(slp, ti, regv | derror) == 0)
1778 if (scsi_low_is_msgout_continue(ti, 0) != 0)
1780 scsi_low_attention(slp);
1784 /* check REQ nagated */
1785 nsp_negate_signal(sc, SCBUSMON_REQ, "msgin<REQ>");
1788 cr = nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR) & (~SCBUSCR_ACK);
1789 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1791 /* catch a next signal */
1792 rv = nsp_expect_signal(sc, PHASE_MSGIN, SCBUSMON_REQ);
1794 while (rv > 0 && len -- > 0);
1798 slp->sl_error |= FATALIO;
1799 nsp_error(sc, "unknown scsi phase", isrc, ph, irqphs);
1807 nsp_start_timer(sc, NSP_TIMER_1MS);
1814 struct nsp_softc *sc;
1816 struct scsi_low_softc *slp = &sc->sc_sclow;
1817 bus_space_tag_t iot = sc->sc_iot;
1818 bus_space_handle_t ioh = sc->sc_ioh;
1822 if (slp->sl_Tnexus == NULL)
1825 ph = nsp_cr_read_1(iot, ioh, NSPR_SCBUSMON);
1826 switch (ph & SCBUSMON_PHMASK)
1829 if (sc->sc_dataout_timeout == 0)
1832 /* check a fifo empty */
1833 regv = bus_space_read_1(iot, ioh, nsp_fifosr);
1834 if ((regv & FIFOSR_FULLEMP) == 0)
1836 bus_space_write_1(iot, ioh, nsp_irqcr, IRQCR_FIFOCL);
1838 /* check still requested */
1839 ph = nsp_cr_read_1(iot, ioh, NSPR_SCBUSMON);
1840 if ((ph & SCBUSMON_REQ) == 0)
1843 if ((-- sc->sc_dataout_timeout) > 0)
1846 slp->sl_error |= PDMAERR;
1847 if ((slp->sl_flags & HW_WRITE_PADDING) == 0)
1849 device_printf(slp->sl_dev, "write padding required\n");
1853 tout = NSP_DELAY_MAX;
1856 ph = nsp_cr_read_1(iot, ioh, NSPR_SCBUSMON);
1857 if ((ph & SCBUSMON_PHMASK) != PHASE_DATAOUT)
1859 regv = bus_space_read_1(iot, ioh, nsp_fifosr);
1860 if ((regv & FIFOSR_FULLEMP) == 0)
1866 bus_space_write_1(iot, ioh, nsp_irqcr, IRQCR_FIFOCL);
1867 nsp_data_padding(sc, SCSI_LOW_WRITE, 32);
1869 ph = nsp_cr_read_1(iot, ioh, NSPR_SCBUSMON);
1870 if ((ph & SCBUSMON_PHMASK) == PHASE_DATAOUT)
1871 sc->sc_dataout_timeout = SCSI_LOW_TIMEOUT_HZ;