1 /* $FreeBSD: src/sys/dev/nsp/nsp.c,v 1.1.2.6 2001/12/17 13:30:18 non Exp $ */
2 /* $DragonFly: src/sys/dev/disk/nsp/nsp.c,v 1.13 2008/06/05 18:06:31 swildner Exp $ */
3 /* $NecBSD: nsp.c,v 1.21.12.6 2001/06/29 06:27:52 honda Exp $ */
8 #define NSP_IO_CONTROL_FLAGS \
9 (NSP_READ_SUSPEND_IO | NSP_WRITE_SUSPEND_IO | \
10 NSP_READ_FIFO_INTERRUPTS | NSP_WRITE_FIFO_INTERRUPTS | \
11 NSP_USE_MEMIO | NSP_WAIT_FOR_SELECT)
14 * Copyright (c) 1998, 1999, 2000, 2001
15 * NetBSD/pc98 porting staff. All rights reserved.
17 * Copyright (c) 1998, 1999, 2000, 2001
18 * Naofumi HONDA. All rights reserved.
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
23 * 1. Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright
26 * notice, this list of conditions and the following disclaimer in the
27 * documentation and/or other materials provided with the distribution.
28 * 3. The name of the author may not be used to endorse or promote products
29 * derived from this software without specific prior written permission.
31 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
32 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
33 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
34 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
35 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
37 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
40 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGE.
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
49 #include <sys/queue.h>
50 #include <sys/malloc.h>
51 #include <sys/errno.h>
53 #include <sys/thread2.h>
55 #include <machine/clock.h>
56 #include <machine/cpu.h>
58 #include <bus/cam/scsi/scsi_low.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 u_int8_t nsp_cr_read_1 (bus_space_tag_t bst, bus_space_handle_t bsh, bus_addr_t ofs);
183 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);
185 static __inline u_int8_t
186 nsp_cr_read_1(bus_space_tag_t bst, bus_space_handle_t bsh, bus_addr_t ofs)
189 bus_space_write_1(bst, bsh, nsp_idxr, ofs);
190 return bus_space_read_1(bst, bsh, nsp_datar);
194 nsp_cr_write_1(bus_space_tag_t bst, bus_space_handle_t bsh, bus_addr_t ofs,
198 bus_space_write_1(bst, bsh, nsp_idxr, ofs);
199 bus_space_write_1(bst, bsh, nsp_datar, va);
203 nsp_expect_signal(struct nsp_softc *sc, u_int8_t curphase, u_int8_t mask)
205 struct scsi_low_softc *slp = &sc->sc_sclow;
206 bus_space_tag_t bst = sc->sc_iot;
207 bus_space_handle_t bsh = sc->sc_ioh;
211 for (wc = 0; wc < NSP_DELAY_MAX / NSP_DELAY_INTERVAL; wc ++)
213 ph = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
214 if (ph == (u_int8_t) -1)
217 isrc = bus_space_read_1(bst, bsh, nsp_irqsr);
218 if (isrc & IRQSR_SCSI)
221 if ((ph & mask) != 0 && (ph & SCBUSMON_PHMASK) == curphase)
224 SCSI_LOW_DELAY(NSP_DELAY_INTERVAL);
227 kprintf("%s: nsp_expect_signal timeout\n", slp->sl_xname);
232 nsphw_init(struct nsp_softc *sc)
234 bus_space_tag_t bst = sc->sc_iot;
235 bus_space_handle_t bsh = sc->sc_ioh;
237 /* block all interrupts */
238 bus_space_write_1(bst, bsh, nsp_irqcr, IRQCR_ALLMASK);
240 /* setup SCSI interface */
241 bus_space_write_1(bst, bsh, nsp_ifselr, IFSELR_IFSEL);
243 nsp_cr_write_1(bst, bsh, NSPR_SCIENR, 0);
245 nsp_cr_write_1(bst, bsh, NSPR_XFERMR, XFERMR_IO8);
246 nsp_cr_write_1(bst, bsh, NSPR_CLKDIVR, sc->sc_iclkdiv);
248 nsp_cr_write_1(bst, bsh, NSPR_SCIENR, sc->sc_icr);
249 nsp_cr_write_1(bst, bsh, NSPR_PARITYR, sc->sc_parr);
250 nsp_cr_write_1(bst, bsh, NSPR_PTCLRR,
251 PTCLRR_ACK | PTCLRR_REQ | PTCLRR_HOST | PTCLRR_RSS);
253 /* setup fifo asic */
254 bus_space_write_1(bst, bsh, nsp_ifselr, IFSELR_REGSEL);
255 nsp_cr_write_1(bst, bsh, NSPR_TERMPWRC, 0);
256 if ((nsp_cr_read_1(bst, bsh, NSPR_OCR) & OCR_TERMPWRS) == 0)
257 nsp_cr_write_1(bst, bsh, NSPR_TERMPWRC, TERMPWRC_POWON);
259 nsp_cr_write_1(bst, bsh, NSPR_XFERMR, XFERMR_IO8);
260 nsp_cr_write_1(bst, bsh, NSPR_CLKDIVR, sc->sc_clkdiv);
261 nsp_cr_write_1(bst, bsh, NSPR_TIMERCNT, 0);
262 nsp_cr_write_1(bst, bsh, NSPR_TIMERCNT, 0);
264 nsp_cr_write_1(bst, bsh, NSPR_SYNCR, 0);
265 nsp_cr_write_1(bst, bsh, NSPR_ACKWIDTH, 0);
267 /* enable interrupts and ack them */
268 nsp_cr_write_1(bst, bsh, NSPR_SCIENR, sc->sc_icr);
269 bus_space_write_1(bst, bsh, nsp_irqcr, IRQSR_MASK);
271 nsp_setup_fifo(sc, NSP_FIFO_OFF, SCSI_LOW_READ, 0);
274 /****************************************************
276 ****************************************************/
278 nsphw_attention(struct nsp_softc *sc)
280 bus_space_tag_t bst = sc->sc_iot;
281 bus_space_handle_t bsh = sc->sc_ioh;
284 cr = nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR)/* & ~SCBUSCR_ACK */;
285 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr | SCBUSCR_ATN);
290 nsphw_bus_reset(struct nsp_softc *sc)
292 bus_space_tag_t bst = sc->sc_iot;
293 bus_space_handle_t bsh = sc->sc_ioh;
296 bus_space_write_1(bst, bsh, nsp_irqcr, IRQCR_ALLMASK);
298 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, SCBUSCR_RST);
299 SCSI_LOW_DELAY(100 * 1000); /* 100ms */
300 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, 0);
301 for (i = 0; i < 5; i ++)
302 (void) nsp_cr_read_1(bst, bsh, NSPR_IRQPHS);
304 bus_space_write_1(bst, bsh, nsp_irqcr, IRQSR_MASK);
308 nsphw_selection_done_and_expect_msgout(struct nsp_softc *sc)
310 struct scsi_low_softc *slp = &sc->sc_sclow;
311 bus_space_tag_t bst = sc->sc_iot;
312 bus_space_handle_t bsh = sc->sc_ioh;
314 /* clear ack counter */
316 nsp_cr_write_1(bst, bsh, NSPR_PTCLRR, PTCLRR_PT | PTCLRR_ACK |
317 PTCLRR_REQ | PTCLRR_HOST);
319 /* deassert sel and assert atten */
321 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, sc->sc_busc);
323 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR,
324 sc->sc_busc | SCBUSCR_ADIR | SCBUSCR_ACKEN);
325 SCSI_LOW_ASSERT_ATN(slp);
329 nsphw_start_selection(struct nsp_softc *sc, struct slccb *cb)
331 struct scsi_low_softc *slp = &sc->sc_sclow;
332 bus_space_tag_t bst = sc->sc_iot;
333 bus_space_handle_t bsh = sc->sc_ioh;
334 struct targ_info *ti = cb->ti;
338 wc = sc->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
339 sc->sc_dataout_timeout = 0;
343 ph = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
344 if (ph != SCBUSMON_FREE)
348 nsp_statics.arbit_conflict_1 ++;
349 #endif /* NSP_STATICS */
350 return SCSI_LOW_START_FAIL;
353 /* start arbitration */
354 nsp_cr_write_1(bst, bsh, NSPR_ARBITS, ARBITS_EXEC);
357 SCSI_LOW_SETUP_PHASE(ti, PH_ARBSTART);
360 /* XXX: what a stupid chip! */
361 arbs = nsp_cr_read_1(bst, bsh, NSPR_ARBITS);
364 while ((arbs & (ARBITS_WIN | ARBITS_FAIL)) == 0 && wc -- > 0);
366 if ((arbs & ARBITS_WIN) == 0)
368 nsp_cr_write_1(bst, bsh, NSPR_ARBITS, ARBITS_CLR);
370 nsp_statics.arbit_conflict_2 ++;
371 #endif /* NSP_STATICS */
372 return SCSI_LOW_START_FAIL;
375 /* assert select line */
376 SCSI_LOW_SETUP_PHASE(ti, PH_SELSTART);
377 scsi_low_arbit_win(slp);
381 nsp_cr_write_1(bst, bsh, NSPR_DATA,
382 sc->sc_idbit | (1 << ti->ti_id));
383 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR,
384 SCBUSCR_SEL | SCBUSCR_BSY | sc->sc_busc);
386 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, SCBUSCR_SEL |
387 SCBUSCR_BSY | SCBUSCR_DOUT | sc->sc_busc);
388 nsp_cr_write_1(bst, bsh, NSPR_ARBITS, ARBITS_CLR);
390 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR,
391 SCBUSCR_SEL | SCBUSCR_DOUT | sc->sc_busc);
394 if ((nsp_io_control & NSP_WAIT_FOR_SELECT) != 0)
396 #define NSP_FIRST_SEL_WAIT 300
397 #define NSP_SEL_CHECK_INTERVAL 10
399 /* wait for a selection response */
400 for (wc = 0; wc < NSP_FIRST_SEL_WAIT / NSP_SEL_CHECK_INTERVAL;
403 ph = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
404 if ((ph & SCBUSMON_BSY) == 0)
406 SCSI_LOW_DELAY(NSP_SEL_CHECK_INTERVAL);
411 ph = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
412 if ((ph & SCBUSMON_BSY) != 0)
414 nsphw_selection_done_and_expect_msgout(sc);
417 SCSI_LOW_SETUP_PHASE(ti, PH_SELECTED);
418 return SCSI_LOW_START_OK;
424 /* check a selection timeout */
425 nsp_start_timer(sc, NSP_TIMER_1MS);
427 return SCSI_LOW_START_OK;
431 nsp_world_start(struct nsp_softc *sc, int fdone)
433 struct scsi_low_softc *slp = &sc->sc_sclow;
438 if ((slp->sl_cfgflags & CFG_NOATTEN) == 0)
439 sc->sc_busc = SCBUSCR_ATN;
443 if ((slp->sl_cfgflags & CFG_NOPARITY) == 0)
444 sc->sc_parr = PARITYR_ENABLE | PARITYR_CLEAR;
448 sc->sc_icr = (SCIENR_SCCHG | SCIENR_RESEL | SCIENR_RST);
451 scsi_low_bus_reset(slp);
453 SOFT_INTR_REQUIRED(slp);
457 struct ncp_synch_data {
464 static struct ncp_synch_data ncp_sync_data_40M[] = {
465 {0x0c,0x0c,0x1,0}, /* 20MB 50ns*/
466 {0x19,0x19,0x3,1}, /* 10MB 100ns*/
467 {0x1a,0x25,0x5,2}, /* 7.5MB 150ns*/
468 {0x26,0x32,0x7,3}, /* 5MB 200ns*/
472 static struct ncp_synch_data ncp_sync_data_20M[] = {
473 {0x19,0x19,0x1,0}, /* 10MB 100ns*/
474 {0x1a,0x25,0x2,0}, /* 7.5MB 150ns*/
475 {0x26,0x32,0x3,1}, /* 5MB 200ns*/
480 nsp_msg(struct nsp_softc *sc, struct targ_info *ti, u_int msg)
482 bus_space_tag_t bst = sc->sc_iot;
483 bus_space_handle_t bsh = sc->sc_ioh;
484 struct ncp_synch_data *sdp;
485 struct nsp_targ_info *nti = (void *) ti;
486 u_int period, offset;
489 if ((msg & SCSI_LOW_MSG_WIDE) != 0)
491 if (ti->ti_width != SCSI_LOW_BUS_WIDTH_8)
493 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
499 if ((msg & SCSI_LOW_MSG_SYNCH) == 0)
502 period = ti->ti_maxsynch.period;
503 offset = ti->ti_maxsynch.offset;
504 if (sc->sc_iclkdiv == CLKDIVR_20M)
505 sdp = &ncp_sync_data_20M[0];
507 sdp = &ncp_sync_data_40M[0];
509 for (i = 0; sdp->max_period != 0; i ++, sdp ++)
511 if (period >= sdp->min_period && period <= sdp->max_period)
515 if (period != 0 && sdp->max_period == 0)
518 * NO proper period/offset found,
519 * Retry neg with the target.
521 ti->ti_maxsynch.period = 0;
522 ti->ti_maxsynch.offset = 0;
523 nti->nti_reg_syncr = 0;
524 nti->nti_reg_ackwidth = 0;
529 nti->nti_reg_syncr = (sdp->chip_period << SYNCR_PERS) |
530 (offset & SYNCR_OFFM);
531 nti->nti_reg_ackwidth = sdp->ack_width;
535 nsp_cr_write_1(bst, bsh, NSPR_SYNCR, nti->nti_reg_syncr);
536 nsp_cr_write_1(bst, bsh, NSPR_ACKWIDTH, nti->nti_reg_ackwidth);
541 nsp_targ_init(struct nsp_softc *sc, struct targ_info *ti, int action)
543 struct nsp_targ_info *nti = (void *) ti;
545 if (action == SCSI_LOW_INFO_ALLOC || action == SCSI_LOW_INFO_REVOKE)
547 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
548 ti->ti_maxsynch.period = 100 / 4;
549 ti->ti_maxsynch.offset = 15;
550 nti->nti_reg_syncr = 0;
551 nti->nti_reg_ackwidth = 0;
557 nsp_start_timer(struct nsp_softc *sc, int time)
559 bus_space_tag_t bst = sc->sc_iot;
560 bus_space_handle_t bsh = sc->sc_ioh;
563 nsp_cr_write_1(bst, bsh, NSPR_TIMERCNT, time);
566 /**************************************************************
567 * General probe attach
568 **************************************************************/
570 nspprobesubr(bus_space_tag_t iot, bus_space_handle_t ioh, u_int dvcfg)
574 regv = bus_space_read_1(iot, ioh, nsp_fifosr);
575 if (regv < 0x11 || regv >= 0x20)
581 nspprint(void *aux, const char *name)
585 kprintf("%s: scsibus ", name);
590 nspattachsubr(struct nsp_softc *sc)
592 struct scsi_low_softc *slp = &sc->sc_sclow;
596 sc->sc_idbit = (1 << slp->sl_hostid);
597 slp->sl_flags |= HW_READ_PADDING;
598 slp->sl_funcs = &nspfuncs;
599 sc->sc_tmaxcnt = SCSI_LOW_MIN_TOUT * 1000 * 1000; /* default */
601 (void) scsi_low_attach(slp, 0, NSP_NTARGETS, NSP_NLUNS,
602 sizeof(struct nsp_targ_info), 0);
605 /**************************************************************
607 **************************************************************/
609 nsp_fifo_count(struct nsp_softc *sc)
611 bus_space_tag_t bst = sc->sc_iot;
612 bus_space_handle_t bsh = sc->sc_ioh;
615 nsp_cr_write_1(bst, bsh, NSPR_PTCLRR, PTCLRR_RSS_ACK | PTCLRR_PT);
616 count = bus_space_read_1(bst, bsh, nsp_datar);
617 count += (((u_int) bus_space_read_1(bst, bsh, nsp_datar)) << 8);
618 count += (((u_int) bus_space_read_1(bst, bsh, nsp_datar)) << 16);
623 nsp_request_count(struct nsp_softc *sc)
625 bus_space_tag_t bst = sc->sc_iot;
626 bus_space_handle_t bsh = sc->sc_ioh;
629 nsp_cr_write_1(bst, bsh, NSPR_PTCLRR, PTCLRR_RSS_REQ | PTCLRR_PT);
630 count = bus_space_read_1(bst, bsh, nsp_datar);
631 count += (((u_int) bus_space_read_1(bst, bsh, nsp_datar)) << 8);
632 count += (((u_int) bus_space_read_1(bst, bsh, nsp_datar)) << 16);
637 nsp_setup_fifo(struct nsp_softc *sc, int on, int direction, int datalen)
641 sc->sc_suspendio = 0;
642 if (on == NSP_FIFO_OFF)
644 xfermode = XFERMR_IO8;
648 /* check if suspend io OK ? */
651 if (direction == SCSI_LOW_READ)
653 if ((nsp_io_control & NSP_READ_SUSPEND_IO) != 0 &&
654 (datalen % nsp_read_suspend_bytes) == 0)
655 sc->sc_suspendio = nsp_read_suspend_bytes;
659 if ((nsp_io_control & NSP_WRITE_SUSPEND_IO) != 0 &&
660 (datalen % nsp_write_suspend_bytes) == 0)
661 sc->sc_suspendio = nsp_write_suspend_bytes;
665 /* determine a transfer type */
666 if (datalen < DEV_BSIZE || (datalen & 3) != 0)
668 if (sc->sc_memh != 0 &&
669 (nsp_io_control & NSP_USE_MEMIO) != 0)
670 xfermode = XFERMR_XEN | XFERMR_MEM8;
672 xfermode = XFERMR_XEN | XFERMR_IO8;
676 if (sc->sc_memh != 0 &&
677 (nsp_io_control & NSP_USE_MEMIO) != 0)
678 xfermode = XFERMR_XEN | XFERMR_MEM32;
680 xfermode = XFERMR_XEN | XFERMR_IO32;
682 if (sc->sc_suspendio > 0)
683 xfermode |= XFERMR_FIFOEN;
687 sc->sc_xfermr = xfermode;
688 nsp_cr_write_1(sc->sc_iot, sc->sc_ioh, NSPR_XFERMR, sc->sc_xfermr);
692 nsp_pdma_end(struct nsp_softc *sc, struct targ_info *ti)
694 struct scsi_low_softc *slp = &sc->sc_sclow;
695 struct slccb *cb = slp->sl_Qnexus;
698 sc->sc_dataout_timeout = 0;
699 slp->sl_flags &= ~HW_PDMASTART;
700 nsp_setup_fifo(sc, NSP_FIFO_OFF, SCSI_LOW_READ, 0);
701 if ((sc->sc_icr & SCIENR_FIFO) != 0)
703 sc->sc_icr &= ~SCIENR_FIFO;
704 nsp_cr_write_1(sc->sc_iot, sc->sc_ioh, NSPR_SCIENR, sc->sc_icr);
709 slp->sl_error |= PDMAERR;
713 if (ti->ti_phase == PH_DATA)
715 cnt = nsp_fifo_count(sc);
716 if (slp->sl_scp.scp_direction == SCSI_LOW_WRITE)
718 len = sc->sc_cnt - cnt;
719 if (sc->sc_cnt >= cnt &&
720 slp->sl_scp.scp_datalen + len <=
721 cb->ccb_scp.scp_datalen)
723 slp->sl_scp.scp_data -= len;
724 slp->sl_scp.scp_datalen += len;
728 slp->sl_error |= PDMAERR;
729 kprintf("%s len %x >= datalen %x\n",
731 len, slp->sl_scp.scp_datalen);
734 else if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
736 if (sc->sc_cnt != cnt ||
737 sc->sc_cnt > cb->ccb_scp.scp_datalen)
739 slp->sl_error |= PDMAERR;
740 kprintf("%s: data read count error %x != %x (%x)\n",
741 slp->sl_xname, sc->sc_cnt, cnt,
742 cb->ccb_scp.scp_datalen);
746 scsi_low_data_finish(slp);
751 kprintf("%s data phase miss\n", slp->sl_xname);
752 slp->sl_error |= PDMAERR;
756 #define RFIFO_CRIT 64
757 #define WFIFO_CRIT 32
760 nsp_data_padding(struct nsp_softc *sc, int direction, u_int count)
762 bus_space_tag_t bst = sc->sc_iot;
763 bus_space_handle_t bsh = sc->sc_ioh;
765 if (count > NSP_MAX_DATA_SIZE)
766 count = NSP_MAX_DATA_SIZE;
768 nsp_cr_write_1(bst, bsh, NSPR_XFERMR, XFERMR_XEN | XFERMR_IO8);
769 if (direction == SCSI_LOW_READ)
772 (void) bus_space_read_1(bst, bsh, nsp_fifodr);
777 (void) bus_space_write_1(bst, bsh, nsp_fifodr, 0);
779 nsp_cr_write_1(bst, bsh, NSPR_XFERMR, sc->sc_xfermr);
783 nsp_read_fifo(struct nsp_softc *sc, int suspendio)
785 struct scsi_low_softc *slp = &sc->sc_sclow;
786 bus_space_tag_t bst = sc->sc_iot;
787 bus_space_handle_t bsh = sc->sc_ioh;
790 res = nsp_fifo_count(sc);
791 if (res == sc->sc_cnt)
795 if (res < sc->sc_cnt || res == (u_int) -1)
797 kprintf("%s: strange fifo ack count 0x%x < 0x%x\n",
798 slp->sl_xname, res, sc->sc_cnt);
801 #endif /* NSP_DEBUG */
803 res = res - sc->sc_cnt;
804 if (res > slp->sl_scp.scp_datalen)
806 if ((slp->sl_error & PDMAERR) == 0)
808 kprintf("%s: data overrun 0x%x > 0x%x\n",
809 slp->sl_xname, res, slp->sl_scp.scp_datalen);
812 slp->sl_error |= PDMAERR;
813 slp->sl_scp.scp_datalen = 0;
815 if ((slp->sl_flags & HW_READ_PADDING) == 0)
817 kprintf("%s: read padding required\n", slp->sl_xname);
821 nsp_data_padding(sc, SCSI_LOW_READ, res);
823 return 1; /* padding start */
826 if (suspendio > 0 && slp->sl_scp.scp_datalen >= suspendio)
829 if ((sc->sc_xfermr & (XFERMR_MEM32 | XFERMR_MEM8)) != 0)
831 if ((sc->sc_xfermr & XFERMR_MEM32) != 0)
834 bus_space_read_region_4(sc->sc_memt, sc->sc_memh, 0,
835 (u_int32_t *) slp->sl_scp.scp_data, res >> 2);
839 bus_space_read_region_1(sc->sc_memt, sc->sc_memh, 0,
840 (u_int8_t *) slp->sl_scp.scp_data, res);
845 if ((sc->sc_xfermr & XFERMR_IO32) != 0)
848 bus_space_read_multi_4(bst, bsh, nsp_fifodr,
849 (u_int32_t *) slp->sl_scp.scp_data, res >> 2);
853 bus_space_read_multi_1(bst, bsh, nsp_fifodr,
854 (u_int8_t *) slp->sl_scp.scp_data, res);
858 if (nsp_cr_read_1(bst, bsh, NSPR_PARITYR) & PARITYR_PE)
860 nsp_cr_write_1(bst, bsh, NSPR_PARITYR,
861 PARITYR_ENABLE | PARITYR_CLEAR);
862 scsi_low_assert_msg(slp, slp->sl_Tnexus, SCSI_LOW_MSG_ERROR, 1);
865 slp->sl_scp.scp_data += res;
866 slp->sl_scp.scp_datalen -= res;
872 nsp_write_fifo(struct nsp_softc *sc, int suspendio)
874 struct scsi_low_softc *slp = &sc->sc_sclow;
875 bus_space_tag_t bst = sc->sc_iot;
876 bus_space_handle_t bsh = sc->sc_ioh;
883 if ((slp->sl_scp.scp_datalen % WFIFO_CRIT) != 0)
885 kprintf("%s: strange write length 0x%x\n",
886 slp->sl_xname, slp->sl_scp.scp_datalen);
888 #endif /* NSP_DEBUG */
889 res = slp->sl_scp.scp_datalen % suspendio;
900 if (res > slp->sl_scp.scp_datalen)
901 res = slp->sl_scp.scp_datalen;
903 /* XXX: reconfirm! */
904 stat = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON) & SCBUSMON_PHMASK;
905 if (stat != PHASE_DATAOUT)
908 if ((sc->sc_xfermr & (XFERMR_MEM32 | XFERMR_MEM8)) != 0)
910 if ((sc->sc_xfermr & XFERMR_MEM32) != 0)
912 bus_space_write_region_4(sc->sc_memt, sc->sc_memh, 0,
913 (u_int32_t *) slp->sl_scp.scp_data, res >> 2);
917 bus_space_write_region_1(sc->sc_memt, sc->sc_memh, 0,
918 (u_int8_t *) slp->sl_scp.scp_data, res);
923 if ((sc->sc_xfermr & XFERMR_IO32) != 0)
925 bus_space_write_multi_4(bst, bsh, nsp_fifodr,
926 (u_int32_t *) slp->sl_scp.scp_data, res >> 2);
930 bus_space_write_multi_1(bst, bsh, nsp_fifodr,
931 (u_int8_t *) slp->sl_scp.scp_data, res);
935 slp->sl_scp.scp_datalen -= res;
936 slp->sl_scp.scp_data += res;
942 nsp_wait_interrupt(struct nsp_softc *sc)
944 bus_space_tag_t bst = sc->sc_iot;
945 bus_space_handle_t bsh = sc->sc_ioh;
949 for (tout = 0; tout < DEV_BSIZE / 10; tout ++)
951 isrc = bus_space_read_1(bst, bsh, nsp_irqsr);
952 if ((isrc & (IRQSR_SCSI | IRQSR_FIFO)) != 0)
954 if ((isrc & IRQSR_FIFO) != 0)
956 bus_space_write_1(bst, bsh,
957 nsp_irqcr, IRQCR_FIFOCL);
967 nsp_pio_read(struct nsp_softc *sc, int suspendio)
969 struct scsi_low_softc *slp = &sc->sc_sclow;
970 bus_space_tag_t bst = sc->sc_iot;
971 bus_space_handle_t bsh = sc->sc_ioh;
972 int tout, padding, datalen;
973 u_int8_t stat, fstat;
976 tout = sc->sc_tmaxcnt;
977 slp->sl_flags |= HW_PDMASTART;
978 datalen = slp->sl_scp.scp_datalen;
983 stat = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
984 if (stat == (u_int8_t) -1)
987 /* out of data phase */
988 if ((stat & SCBUSMON_PHMASK) != PHASE_DATAIN)
990 nsp_read_fifo(sc, 0);
995 fstat = bus_space_read_1(bst, bsh, nsp_fifosr);
996 if ((fstat & FIFOSR_FULLEMP) != 0)
998 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1000 bus_space_write_1(bst, bsh, nsp_irqcr,
1006 padding |= nsp_read_fifo(sc, suspendio);
1010 padding |= nsp_read_fifo(sc, 0);
1013 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1018 if (padding == 0 && slp->sl_scp.scp_datalen <= 0)
1021 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1029 kprintf("%s: nsp_pio_read: timeout\n", slp->sl_xname);
1035 if (slp->sl_scp.scp_datalen > 0 &&
1036 slp->sl_scp.scp_datalen > datalen - nsp_read_interrupt_bytes)
1038 if (nsp_wait_interrupt(sc) != 0)
1044 nsp_pio_write(struct nsp_softc *sc, int suspendio)
1046 struct scsi_low_softc *slp = &sc->sc_sclow;
1047 bus_space_tag_t bst = sc->sc_iot;
1048 bus_space_handle_t bsh = sc->sc_ioh;
1049 u_int rcount, acount;
1051 u_int8_t stat, fstat;
1053 tout = sc->sc_tmaxcnt;
1054 slp->sl_flags |= HW_PDMASTART;
1055 datalen = slp->sl_scp.scp_datalen;
1060 stat = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON) & SCBUSMON_PHMASK;
1061 if (stat != PHASE_DATAOUT)
1064 if (slp->sl_scp.scp_datalen <= 0)
1066 if (sc->sc_dataout_timeout == 0)
1067 sc->sc_dataout_timeout = SCSI_LOW_TIMEOUT_HZ;
1071 fstat = bus_space_read_1(bst, bsh, nsp_fifosr);
1072 if ((fstat & FIFOSR_FULLEMP) != 0)
1074 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1076 bus_space_write_1(bst, bsh, nsp_irqcr,
1083 * To avoid timeout of pcmcia bus
1084 * (not scsi bus!), we should check
1085 * the scsi device sends us request
1086 * signals, which means the scsi device
1087 * is ready to recieve data without
1090 if ((slp->sl_scp.scp_datalen % suspendio) == 0)
1093 * fill the nsp fifo, and waiting for
1096 nsp_write_fifo(sc, 0);
1101 * check the request singals.
1103 acount = nsp_fifo_count(sc);
1104 rcount = nsp_request_count(sc);
1105 if (rcount <= acount)
1107 nsp_write_fifo(sc, 0);
1109 nsp_statics.device_busy ++;
1110 #endif /* NSP_STATICS */
1114 nsp_write_fifo(sc, suspendio);
1116 nsp_statics.device_data_write ++;
1117 #endif /* NSP_STATICS */
1123 nsp_write_fifo(sc, 0);
1126 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1131 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1139 kprintf("%s: nsp_pio_write: timeout\n", slp->sl_xname);
1144 if (slp->sl_scp.scp_datalen > 0 &&
1145 slp->sl_scp.scp_datalen > datalen - nsp_write_interrupt_bytes)
1147 if (nsp_wait_interrupt(sc) != 0)
1153 nsp_negate_signal(struct nsp_softc *sc, u_int8_t mask, u_char *s)
1155 struct scsi_low_softc *slp = &sc->sc_sclow;
1156 bus_space_tag_t bst = sc->sc_iot;
1157 bus_space_handle_t bsh = sc->sc_ioh;
1161 for (wc = 0; wc < NSP_DELAY_MAX / NSP_DELAY_INTERVAL; wc ++)
1163 regv = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
1164 if (regv == (u_int8_t) -1)
1166 if ((regv & mask) == 0)
1168 SCSI_LOW_DELAY(NSP_DELAY_INTERVAL);
1171 kprintf("%s: %s nsp_negate_signal timeout\n", slp->sl_xname, s);
1176 nsp_xfer(struct nsp_softc *sc, u_int8_t *buf, int len, int phase,
1179 bus_space_tag_t bst = sc->sc_iot;
1180 bus_space_handle_t bsh = sc->sc_ioh;
1183 for (ptr = 0; len > 0; len --, ptr ++)
1185 rv = nsp_expect_signal(sc, phase, SCBUSMON_REQ);
1189 if (len == 1 && clear_atn != 0)
1191 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR,
1192 SCBUSCR_ADIR | SCBUSCR_ACKEN);
1193 SCSI_LOW_DEASSERT_ATN(&sc->sc_sclow);
1196 if (phase & SCBUSMON_IO)
1198 buf[ptr] = nsp_cr_read_1(bst, bsh, NSPR_DATAACK);
1202 nsp_cr_write_1(bst, bsh, NSPR_DATAACK, buf[ptr]);
1204 nsp_negate_signal(sc, SCBUSMON_ACK, "xfer<ACK>");
1211 /**************************************************************
1212 * disconnect & reselect (HW low)
1213 **************************************************************/
1215 nsp_reselected(struct nsp_softc *sc)
1217 struct scsi_low_softc *slp = &sc->sc_sclow;
1218 bus_space_tag_t bst = sc->sc_iot;
1219 bus_space_handle_t bsh = sc->sc_ioh;
1220 struct targ_info *ti;
1224 sid = (u_int) nsp_cr_read_1(bst, bsh, NSPR_RESELR);
1225 sid &= ~sc->sc_idbit;
1227 if ((ti = scsi_low_reselected(slp, sid)) == NULL)
1230 nsp_negate_signal(sc, SCBUSMON_SEL, "reselect<SEL>");
1232 cr = nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR);
1233 cr &= ~(SCBUSCR_BSY | SCBUSCR_ATN);
1234 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1235 cr |= SCBUSCR_ADIR | SCBUSCR_ACKEN;
1236 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1239 nsp_statics.reselect ++;
1240 #endif /* NSP_STATCIS */
1245 nsp_disconnected(struct nsp_softc *sc, struct targ_info *ti)
1247 struct scsi_low_softc *slp = &sc->sc_sclow;
1248 bus_space_tag_t bst = sc->sc_iot;
1249 bus_space_handle_t bsh = sc->sc_ioh;
1251 nsp_cr_write_1(bst, bsh, NSPR_PTCLRR, PTCLRR_PT | PTCLRR_ACK |
1252 PTCLRR_REQ | PTCLRR_HOST);
1253 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1255 sc->sc_icr &= ~SCIENR_FIFO;
1256 nsp_cr_write_1(bst, bsh, NSPR_SCIENR, sc->sc_icr);
1259 sc->sc_dataout_timeout = 0;
1261 nsp_statics.disconnect ++;
1262 #endif /* NSP_STATICS */
1263 scsi_low_disconnected(slp, ti);
1267 /**************************************************************
1269 **************************************************************/
1270 static void nsp_error (struct nsp_softc *, u_char *, u_int8_t, u_int8_t, u_int8_t);
1273 nsp_error(struct nsp_softc *sc, u_char *s, u_int8_t isrc, u_int8_t ph,
1276 struct scsi_low_softc *slp = &sc->sc_sclow;
1278 kprintf("%s: %s\n", slp->sl_xname, s);
1279 kprintf("%s: isrc 0x%x scmon 0x%x irqphs 0x%x\n",
1280 slp->sl_xname, (u_int) isrc, (u_int) ph, (u_int) irqphs);
1284 nsp_target_nexus_establish(struct nsp_softc *sc)
1286 struct scsi_low_softc *slp = &sc->sc_sclow;
1287 bus_space_tag_t bst = sc->sc_iot;
1288 bus_space_handle_t bsh = sc->sc_ioh;
1289 struct targ_info *ti = slp->sl_Tnexus;
1290 struct nsp_targ_info *nti = (void *) ti;
1292 /* setup synch transfer registers */
1293 nsp_cr_write_1(bst, bsh, NSPR_SYNCR, nti->nti_reg_syncr);
1294 nsp_cr_write_1(bst, bsh, NSPR_ACKWIDTH, nti->nti_reg_ackwidth);
1296 /* setup pdma fifo (minimum) */
1297 nsp_setup_fifo(sc, NSP_FIFO_ON, SCSI_LOW_READ, 0);
1302 nsp_lun_nexus_establish(struct nsp_softc *sc)
1309 nsp_ccb_nexus_establish(struct nsp_softc *sc)
1311 struct scsi_low_softc *slp = &sc->sc_sclow;
1312 struct slccb *cb = slp->sl_Qnexus;
1314 sc->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
1316 /* setup pdma fifo */
1317 nsp_setup_fifo(sc, NSP_FIFO_ON,
1318 slp->sl_scp.scp_direction, slp->sl_scp.scp_datalen);
1320 if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
1322 if (sc->sc_suspendio > 0 &&
1323 (nsp_io_control & NSP_READ_FIFO_INTERRUPTS) != 0)
1325 sc->sc_icr |= SCIENR_FIFO;
1326 nsp_cr_write_1(sc->sc_iot, sc->sc_ioh,
1327 NSPR_SCIENR, sc->sc_icr);
1332 if (sc->sc_suspendio > 0 &&
1333 (nsp_io_control & NSP_WRITE_FIFO_INTERRUPTS) != 0)
1335 sc->sc_icr |= SCIENR_FIFO;
1336 nsp_cr_write_1(sc->sc_iot, sc->sc_ioh,
1337 NSPR_SCIENR, sc->sc_icr);
1344 nsp_phase_match(struct nsp_softc *sc, u_int8_t phase, u_int8_t stat)
1346 struct scsi_low_softc *slp = &sc->sc_sclow;
1348 if ((stat & SCBUSMON_PHMASK) != phase)
1350 kprintf("%s: phase mismatch 0x%x != 0x%x\n",
1351 slp->sl_xname, (u_int) phase, (u_int) stat);
1355 if ((stat & SCBUSMON_REQ) == 0)
1364 struct nsp_softc *sc = arg;
1365 struct scsi_low_softc *slp = &sc->sc_sclow;
1366 bus_space_tag_t bst = sc->sc_iot;
1367 bus_space_handle_t bsh = sc->sc_ioh;
1368 struct targ_info *ti;
1370 u_int derror, flags;
1372 u_int8_t isrc, ph, irqphs, cr, regv;
1374 /*******************************************
1376 *******************************************/
1377 if (slp->sl_flags & HW_INACTIVE)
1380 bus_space_write_1(bst, bsh, nsp_irqcr, IRQCR_IRQDIS);
1381 isrc = bus_space_read_1(bst, bsh, nsp_irqsr);
1382 if (isrc == (u_int8_t) -1 || (isrc & IRQSR_MASK) == 0)
1384 bus_space_write_1(bst, bsh, nsp_irqcr, 0);
1389 * Do not read an irqphs register if no scsi phase interrupt.
1390 * Unless, you should lose a scsi phase interrupt.
1392 ph = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
1393 if ((isrc & IRQSR_SCSI) != 0)
1395 irqphs = nsp_cr_read_1(bst, bsh, NSPR_IRQPHS);
1401 * timer interrupt handler (scsi vs timer interrupts)
1403 if (sc->sc_timer != 0)
1405 nsp_cr_write_1(bst, bsh, NSPR_TIMERCNT, 0);
1406 nsp_cr_write_1(bst, bsh, NSPR_TIMERCNT, 0);
1410 /* check a timer interrupt */
1412 if ((isrc & IRQSR_TIMER) != 0)
1414 if ((isrc & IRQSR_MASK) == IRQSR_TIMER && sc->sc_seltout == 0)
1416 bus_space_write_1(bst, bsh, nsp_irqcr, IRQCR_TIMERCL);
1419 regv |= IRQCR_TIMERCL;
1422 /* check a fifo interrupt */
1423 if ((isrc & IRQSR_FIFO) != 0)
1425 regv |= IRQCR_FIFOCL;
1428 /* OK. enable all interrupts */
1429 bus_space_write_1(bst, bsh, nsp_irqcr, regv);
1431 /*******************************************
1433 *******************************************/
1437 nsp_error(sc, "current status", isrc, ph, irqphs);
1438 scsi_low_print(slp, NULL);
1441 SCSI_LOW_DEBUGGER("nsp");
1444 #endif /* NSP_DEBUG */
1446 /*******************************************
1447 * Parse hardware SCSI irq reasons register
1448 *******************************************/
1449 if ((isrc & IRQSR_SCSI) != 0)
1451 if ((irqphs & IRQPHS_RST) != 0)
1453 scsi_low_restart(slp, SCSI_LOW_RESTART_SOFT,
1454 "bus reset (power off?)");
1458 if ((irqphs & IRQPHS_RSEL) != 0)
1460 bus_space_write_1(bst, bsh, nsp_irqcr, IRQCR_RESCL);
1461 if (nsp_reselected(sc) == EJUSTRETURN)
1465 if ((irqphs & (IRQPHS_PCHG | IRQPHS_LBF)) == 0)
1469 /*******************************************
1471 *******************************************/
1472 if ((ti = slp->sl_Tnexus) == NULL)
1474 /* unknown scsi phase changes */
1475 nsp_error(sc, "unknown scsi phase changes", isrc, ph, irqphs);
1479 /*******************************************
1480 * aribitration & selection
1481 *******************************************/
1482 switch (ti->ti_phase)
1485 if ((ph & SCBUSMON_BSY) == 0)
1487 if (sc->sc_seltout >= NSP_SELTIMEOUT)
1490 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, 0);
1491 return nsp_disconnected(sc, ti);
1494 nsp_start_timer(sc, NSP_TIMER_1MS);
1498 SCSI_LOW_SETUP_PHASE(ti, PH_SELECTED);
1499 nsphw_selection_done_and_expect_msgout(sc);
1503 if ((isrc & IRQSR_SCSI) == 0)
1506 nsp_target_nexus_establish(sc);
1510 if ((isrc & IRQSR_SCSI) == 0)
1513 nsp_target_nexus_establish(sc);
1514 if ((ph & SCBUSMON_PHMASK) != PHASE_MSGIN)
1516 kprintf("%s: unexpected phase after reselect\n",
1518 slp->sl_error |= FATALIO;
1519 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 1);
1525 if ((isrc & IRQSR_SCSI) != 0)
1527 if ((isrc & IRQSR_FIFO) != 0)
1529 if (NSP_IS_PHASE_DATA(ph) == 0)
1531 irqphs = (ph & IRQPHS_PHMASK);
1537 if ((isrc & IRQSR_SCSI) == 0)
1542 /*******************************************
1543 * data phase control
1544 *******************************************/
1545 if (slp->sl_flags & HW_PDMASTART)
1547 if ((isrc & IRQSR_SCSI) != 0 &&
1548 NSP_IS_IRQPHS_DATA(irqphs) == 0)
1550 if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
1551 nsp_pio_read(sc, 0);
1552 nsp_pdma_end(sc, ti);
1556 /*******************************************
1558 *******************************************/
1559 if (slp->sl_msgphase != 0 && (irqphs & IRQPHS_LBF) != 0)
1560 return nsp_disconnected(sc, ti);
1562 /* check unexpected bus free state */
1565 nsp_error(sc, "unexpected bus free", isrc, ph, irqphs);
1566 return nsp_disconnected(sc, ti);
1569 /* check normal scsi phase */
1570 switch (irqphs & IRQPHS_PHMASK)
1573 if (nsp_phase_match(sc, PHASE_CMD, ph) != 0)
1576 SCSI_LOW_SETUP_PHASE(ti, PH_CMD);
1577 if (scsi_low_cmd(slp, ti) != 0)
1579 scsi_low_attention(slp);
1582 nsp_cr_write_1(bst, bsh, NSPR_CMDCR, CMDCR_PTCLR);
1583 for (len = 0; len < slp->sl_scp.scp_cmdlen; len ++)
1584 nsp_cr_write_1(bst, bsh, NSPR_CMDDR,
1585 slp->sl_scp.scp_cmd[len]);
1587 nsp_cr_write_1(bst, bsh, NSPR_CMDCR, CMDCR_PTCLR | CMDCR_EXEC);
1590 case IRQPHS_DATAOUT:
1591 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1592 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_WRITE) != 0)
1594 scsi_low_attention(slp);
1597 nsp_pio_write(sc, sc->sc_suspendio);
1601 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1602 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_READ) != 0)
1604 scsi_low_attention(slp);
1607 nsp_pio_read(sc, sc->sc_suspendio);
1611 if (nsp_phase_match(sc, PHASE_STATUS, ph) != 0)
1614 SCSI_LOW_SETUP_PHASE(ti, PH_STAT);
1615 regv = nsp_cr_read_1(bst, bsh, NSPR_DATA);
1616 if (nsp_cr_read_1(bst, bsh, NSPR_PARITYR) & PARITYR_PE)
1618 nsp_cr_write_1(bst, bsh, NSPR_PARITYR,
1619 PARITYR_ENABLE | PARITYR_CLEAR);
1620 derror = SCSI_LOW_DATA_PE;
1626 cr = SCBUSCR_ACK | nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR);
1627 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1629 if (scsi_low_statusin(slp, ti, derror | regv) != 0)
1631 scsi_low_attention(slp);
1634 /* check REQ nagated */
1635 nsp_negate_signal(sc, SCBUSMON_REQ, "statin<REQ>");
1638 cr = nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR) & (~SCBUSCR_ACK);
1639 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1643 if (nsp_phase_match(sc, PHASE_MSGOUT, ph) != 0)
1646 #ifdef NSP_MSGOUT_SERIALIZE
1649 * NSP invoke interrupts only in the case of scsi phase changes,
1650 * therefore we should poll the scsi phase here to catch
1651 * the next "msg out" if exists (no scsi phase changes).
1655 SCSI_LOW_SETUP_PHASE(ti, PH_MSGOUT);
1656 flags = (ti->ti_ophase != ti->ti_phase) ?
1657 SCSI_LOW_MSGOUT_INIT : 0;
1658 len = scsi_low_msgout(slp, ti, flags);
1660 if (len > 1 && slp->sl_atten == 0)
1662 scsi_low_attention(slp);
1665 if (nsp_xfer(sc, ti->ti_msgoutstr, len, PHASE_MSGOUT,
1666 slp->sl_clear_atten) != 0)
1668 slp->sl_error |= FATALIO;
1669 nsp_error(sc, "MSGOUT: xfer short",
1673 /* catch a next signal */
1674 rv = nsp_expect_signal(sc, PHASE_MSGOUT, SCBUSMON_REQ);
1676 while (rv > 0 && len -- > 0);
1678 #else /* !NSP_MSGOUT_SERIALIZE */
1679 SCSI_LOW_SETUP_PHASE(ti, PH_MSGOUT);
1680 flags = SCSI_LOW_MSGOUT_UNIFY;
1681 if (ti->ti_ophase != ti->ti_phase)
1682 flags |= SCSI_LOW_MSGOUT_INIT;
1683 len = scsi_low_msgout(slp, ti, flags);
1685 if (len > 1 && slp->sl_atten == 0)
1687 scsi_low_attention(slp);
1690 if (nsp_xfer(sc, ti->ti_msgoutstr, len, PHASE_MSGOUT,
1691 slp->sl_clear_atten) != 0)
1693 nsp_error(sc, "MSGOUT: xfer short", isrc, ph, irqphs);
1696 #endif /* !NSP_MSGOUT_SERIALIZE */
1700 if (nsp_phase_match(sc, PHASE_MSGIN, ph) != 0)
1705 * NSP invoke interrupts only in the case of scsi phase changes,
1706 * therefore we should poll the scsi phase here to catch
1707 * the next "msg in" if exists (no scsi phase changes).
1711 SCSI_LOW_SETUP_PHASE(ti, PH_MSGIN);
1714 regv = nsp_cr_read_1(bst, bsh, NSPR_DATA);
1715 if (nsp_cr_read_1(bst, bsh, NSPR_PARITYR) & PARITYR_PE)
1717 nsp_cr_write_1(bst, bsh,
1719 PARITYR_ENABLE | PARITYR_CLEAR);
1720 derror = SCSI_LOW_DATA_PE;
1728 cr = nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR) | SCBUSCR_ACK;
1729 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1731 if (scsi_low_msgin(slp, ti, regv | derror) == 0)
1733 if (scsi_low_is_msgout_continue(ti, 0) != 0)
1735 scsi_low_attention(slp);
1739 /* check REQ nagated */
1740 nsp_negate_signal(sc, SCBUSMON_REQ, "msgin<REQ>");
1743 cr = nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR) & (~SCBUSCR_ACK);
1744 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1746 /* catch a next signal */
1747 rv = nsp_expect_signal(sc, PHASE_MSGIN, SCBUSMON_REQ);
1749 while (rv > 0 && len -- > 0);
1753 slp->sl_error |= FATALIO;
1754 nsp_error(sc, "unknown scsi phase", isrc, ph, irqphs);
1762 nsp_start_timer(sc, NSP_TIMER_1MS);
1768 nsp_timeout(struct nsp_softc *sc)
1770 struct scsi_low_softc *slp = &sc->sc_sclow;
1771 bus_space_tag_t iot = sc->sc_iot;
1772 bus_space_handle_t ioh = sc->sc_ioh;
1776 if (slp->sl_Tnexus == NULL)
1779 ph = nsp_cr_read_1(iot, ioh, NSPR_SCBUSMON);
1780 switch (ph & SCBUSMON_PHMASK)
1783 if (sc->sc_dataout_timeout == 0)
1786 /* check a fifo empty */
1787 regv = bus_space_read_1(iot, ioh, nsp_fifosr);
1788 if ((regv & FIFOSR_FULLEMP) == 0)
1790 bus_space_write_1(iot, ioh, nsp_irqcr, IRQCR_FIFOCL);
1792 /* check still requested */
1793 ph = nsp_cr_read_1(iot, ioh, NSPR_SCBUSMON);
1794 if ((ph & SCBUSMON_REQ) == 0)
1797 if ((-- sc->sc_dataout_timeout) > 0)
1800 slp->sl_error |= PDMAERR;
1801 if ((slp->sl_flags & HW_WRITE_PADDING) == 0)
1803 kprintf("%s: write padding required\n", slp->sl_xname);
1807 tout = NSP_DELAY_MAX;
1810 ph = nsp_cr_read_1(iot, ioh, NSPR_SCBUSMON);
1811 if ((ph & SCBUSMON_PHMASK) != PHASE_DATAOUT)
1813 regv = bus_space_read_1(iot, ioh, nsp_fifosr);
1814 if ((regv & FIFOSR_FULLEMP) == 0)
1820 bus_space_write_1(iot, ioh, nsp_irqcr, IRQCR_FIFOCL);
1821 nsp_data_padding(sc, SCSI_LOW_WRITE, 32);
1823 ph = nsp_cr_read_1(iot, ioh, NSPR_SCBUSMON);
1824 if ((ph & SCBUSMON_PHMASK) == PHASE_DATAOUT)
1825 sc->sc_dataout_timeout = SCSI_LOW_TIMEOUT_HZ;