1 /* $FreeBSD: src/sys/dev/nsp/nsp.c,v 1.1.2.6 2001/12/17 13:30:18 non Exp $ */
2 /* $NecBSD: nsp.c,v 1.21.12.6 2001/06/29 06:27:52 honda Exp $ */
7 #define NSP_IO_CONTROL_FLAGS \
8 (NSP_READ_SUSPEND_IO | NSP_WRITE_SUSPEND_IO | \
9 NSP_READ_FIFO_INTERRUPTS | NSP_WRITE_FIFO_INTERRUPTS | \
10 NSP_USE_MEMIO | NSP_WAIT_FOR_SELECT)
13 * Copyright (c) 1998, 1999, 2000, 2001
14 * NetBSD/pc98 porting staff. All rights reserved.
16 * Copyright (c) 1998, 1999, 2000, 2001
17 * Naofumi HONDA. All rights reserved.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
27 * 3. The name of the author may not be used to endorse or promote products
28 * derived from this software without specific prior written permission.
30 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
33 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
34 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
36 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
38 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
39 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 * POSSIBILITY OF SUCH DAMAGE.
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
48 #include <sys/queue.h>
49 #include <sys/malloc.h>
50 #include <sys/errno.h>
52 #include <sys/thread2.h>
54 #include <machine/clock.h>
55 #include <machine/cpu.h>
57 #include <bus/cam/scsi/scsi_low.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 #define NSP_FIFO_ON 1
123 #define NSP_FIFO_OFF 0
124 static void nsp_pio_read (struct nsp_softc *, int);
125 static void nsp_pio_write (struct nsp_softc *, int);
126 static int nsp_xfer (struct nsp_softc *, u_int8_t *, int, int, int);
127 static int nsp_msg (struct nsp_softc *, struct targ_info *, u_int);
128 static int nsp_reselected (struct nsp_softc *);
129 static int nsp_disconnected (struct nsp_softc *, struct targ_info *);
130 static void nsp_pdma_end (struct nsp_softc *, struct targ_info *);
131 static void nsphw_init (struct nsp_softc *);
132 static int nsp_target_nexus_establish (struct nsp_softc *);
133 static int nsp_lun_nexus_establish (struct nsp_softc *);
134 static int nsp_ccb_nexus_establish (struct nsp_softc *);
135 static int nsp_world_start (struct nsp_softc *, int);
136 static int nsphw_start_selection (struct nsp_softc *sc, struct slccb *);
137 static void nsphw_bus_reset (struct nsp_softc *);
138 static void nsphw_attention (struct nsp_softc *);
139 static u_int nsp_fifo_count (struct nsp_softc *);
140 static u_int nsp_request_count (struct nsp_softc *);
141 static int nsp_negate_signal (struct nsp_softc *, u_int8_t, u_char *);
142 static int nsp_expect_signal (struct nsp_softc *, u_int8_t, u_int8_t);
143 static void nsp_start_timer (struct nsp_softc *, int);
144 static void nsp_setup_fifo (struct nsp_softc *, int, int, int);
145 static int nsp_targ_init (struct nsp_softc *, struct targ_info *, int);
146 static void nsphw_selection_done_and_expect_msgout (struct nsp_softc *);
147 static void nsp_data_padding (struct nsp_softc *, int, u_int);
148 static int nsp_timeout (struct nsp_softc *);
149 static int nsp_read_fifo (struct nsp_softc *, int);
150 static int nsp_write_fifo (struct nsp_softc *, int);
151 static int nsp_phase_match (struct nsp_softc *, u_int8_t, u_int8_t);
152 static int nsp_wait_interrupt (struct nsp_softc *);
154 struct scsi_low_funcs nspfuncs = {
155 SC_LOW_INIT_T nsp_world_start,
156 SC_LOW_BUSRST_T nsphw_bus_reset,
157 SC_LOW_TARG_INIT_T nsp_targ_init,
158 SC_LOW_LUN_INIT_T NULL,
160 SC_LOW_SELECT_T nsphw_start_selection,
161 SC_LOW_NEXUS_T nsp_lun_nexus_establish,
162 SC_LOW_NEXUS_T nsp_ccb_nexus_establish,
164 SC_LOW_ATTEN_T nsphw_attention,
165 SC_LOW_MSG_T nsp_msg,
167 SC_LOW_TIMEOUT_T nsp_timeout,
168 SC_LOW_POLL_T nspintr,
173 /****************************************************
175 ****************************************************/
176 static __inline u_int8_t nsp_cr_read_1 (bus_space_tag_t bst, bus_space_handle_t bsh, bus_addr_t ofs);
177 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);
179 static __inline u_int8_t
180 nsp_cr_read_1(bus_space_tag_t bst, bus_space_handle_t bsh, bus_addr_t ofs)
183 bus_space_write_1(bst, bsh, nsp_idxr, ofs);
184 return bus_space_read_1(bst, bsh, nsp_datar);
188 nsp_cr_write_1(bus_space_tag_t bst, bus_space_handle_t bsh, bus_addr_t ofs,
192 bus_space_write_1(bst, bsh, nsp_idxr, ofs);
193 bus_space_write_1(bst, bsh, nsp_datar, va);
197 nsp_expect_signal(struct nsp_softc *sc, u_int8_t curphase, u_int8_t mask)
199 struct scsi_low_softc *slp = &sc->sc_sclow;
200 bus_space_tag_t bst = sc->sc_iot;
201 bus_space_handle_t bsh = sc->sc_ioh;
205 for (wc = 0; wc < NSP_DELAY_MAX / NSP_DELAY_INTERVAL; wc ++)
207 ph = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
208 if (ph == (u_int8_t) -1)
211 isrc = bus_space_read_1(bst, bsh, nsp_irqsr);
212 if (isrc & IRQSR_SCSI)
215 if ((ph & mask) != 0 && (ph & SCBUSMON_PHMASK) == curphase)
218 SCSI_LOW_DELAY(NSP_DELAY_INTERVAL);
221 kprintf("%s: nsp_expect_signal timeout\n", slp->sl_xname);
226 nsphw_init(struct nsp_softc *sc)
228 bus_space_tag_t bst = sc->sc_iot;
229 bus_space_handle_t bsh = sc->sc_ioh;
231 /* block all interrupts */
232 bus_space_write_1(bst, bsh, nsp_irqcr, IRQCR_ALLMASK);
234 /* setup SCSI interface */
235 bus_space_write_1(bst, bsh, nsp_ifselr, IFSELR_IFSEL);
237 nsp_cr_write_1(bst, bsh, NSPR_SCIENR, 0);
239 nsp_cr_write_1(bst, bsh, NSPR_XFERMR, XFERMR_IO8);
240 nsp_cr_write_1(bst, bsh, NSPR_CLKDIVR, sc->sc_iclkdiv);
242 nsp_cr_write_1(bst, bsh, NSPR_SCIENR, sc->sc_icr);
243 nsp_cr_write_1(bst, bsh, NSPR_PARITYR, sc->sc_parr);
244 nsp_cr_write_1(bst, bsh, NSPR_PTCLRR,
245 PTCLRR_ACK | PTCLRR_REQ | PTCLRR_HOST | PTCLRR_RSS);
247 /* setup fifo asic */
248 bus_space_write_1(bst, bsh, nsp_ifselr, IFSELR_REGSEL);
249 nsp_cr_write_1(bst, bsh, NSPR_TERMPWRC, 0);
250 if ((nsp_cr_read_1(bst, bsh, NSPR_OCR) & OCR_TERMPWRS) == 0)
251 nsp_cr_write_1(bst, bsh, NSPR_TERMPWRC, TERMPWRC_POWON);
253 nsp_cr_write_1(bst, bsh, NSPR_XFERMR, XFERMR_IO8);
254 nsp_cr_write_1(bst, bsh, NSPR_CLKDIVR, sc->sc_clkdiv);
255 nsp_cr_write_1(bst, bsh, NSPR_TIMERCNT, 0);
256 nsp_cr_write_1(bst, bsh, NSPR_TIMERCNT, 0);
258 nsp_cr_write_1(bst, bsh, NSPR_SYNCR, 0);
259 nsp_cr_write_1(bst, bsh, NSPR_ACKWIDTH, 0);
261 /* enable interrupts and ack them */
262 nsp_cr_write_1(bst, bsh, NSPR_SCIENR, sc->sc_icr);
263 bus_space_write_1(bst, bsh, nsp_irqcr, IRQSR_MASK);
265 nsp_setup_fifo(sc, NSP_FIFO_OFF, SCSI_LOW_READ, 0);
268 /****************************************************
270 ****************************************************/
272 nsphw_attention(struct nsp_softc *sc)
274 bus_space_tag_t bst = sc->sc_iot;
275 bus_space_handle_t bsh = sc->sc_ioh;
278 cr = nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR)/* & ~SCBUSCR_ACK */;
279 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr | SCBUSCR_ATN);
284 nsphw_bus_reset(struct nsp_softc *sc)
286 bus_space_tag_t bst = sc->sc_iot;
287 bus_space_handle_t bsh = sc->sc_ioh;
290 bus_space_write_1(bst, bsh, nsp_irqcr, IRQCR_ALLMASK);
292 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, SCBUSCR_RST);
293 SCSI_LOW_DELAY(100 * 1000); /* 100ms */
294 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, 0);
295 for (i = 0; i < 5; i ++)
296 (void) nsp_cr_read_1(bst, bsh, NSPR_IRQPHS);
298 bus_space_write_1(bst, bsh, nsp_irqcr, IRQSR_MASK);
302 nsphw_selection_done_and_expect_msgout(struct nsp_softc *sc)
304 struct scsi_low_softc *slp = &sc->sc_sclow;
305 bus_space_tag_t bst = sc->sc_iot;
306 bus_space_handle_t bsh = sc->sc_ioh;
308 /* clear ack counter */
310 nsp_cr_write_1(bst, bsh, NSPR_PTCLRR, PTCLRR_PT | PTCLRR_ACK |
311 PTCLRR_REQ | PTCLRR_HOST);
313 /* deassert sel and assert atten */
315 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, sc->sc_busc);
317 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR,
318 sc->sc_busc | SCBUSCR_ADIR | SCBUSCR_ACKEN);
319 SCSI_LOW_ASSERT_ATN(slp);
323 nsphw_start_selection(struct nsp_softc *sc, struct slccb *cb)
325 struct scsi_low_softc *slp = &sc->sc_sclow;
326 bus_space_tag_t bst = sc->sc_iot;
327 bus_space_handle_t bsh = sc->sc_ioh;
328 struct targ_info *ti = cb->ti;
332 wc = sc->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
333 sc->sc_dataout_timeout = 0;
337 ph = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
338 if (ph != SCBUSMON_FREE)
342 nsp_statics.arbit_conflict_1 ++;
343 #endif /* NSP_STATICS */
344 return SCSI_LOW_START_FAIL;
347 /* start arbitration */
348 nsp_cr_write_1(bst, bsh, NSPR_ARBITS, ARBITS_EXEC);
351 SCSI_LOW_SETUP_PHASE(ti, PH_ARBSTART);
354 /* XXX: what a stupid chip! */
355 arbs = nsp_cr_read_1(bst, bsh, NSPR_ARBITS);
358 while ((arbs & (ARBITS_WIN | ARBITS_FAIL)) == 0 && wc -- > 0);
360 if ((arbs & ARBITS_WIN) == 0)
362 nsp_cr_write_1(bst, bsh, NSPR_ARBITS, ARBITS_CLR);
364 nsp_statics.arbit_conflict_2 ++;
365 #endif /* NSP_STATICS */
366 return SCSI_LOW_START_FAIL;
369 /* assert select line */
370 SCSI_LOW_SETUP_PHASE(ti, PH_SELSTART);
371 scsi_low_arbit_win(slp);
375 nsp_cr_write_1(bst, bsh, NSPR_DATA,
376 sc->sc_idbit | (1 << ti->ti_id));
377 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR,
378 SCBUSCR_SEL | SCBUSCR_BSY | sc->sc_busc);
380 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, SCBUSCR_SEL |
381 SCBUSCR_BSY | SCBUSCR_DOUT | sc->sc_busc);
382 nsp_cr_write_1(bst, bsh, NSPR_ARBITS, ARBITS_CLR);
384 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR,
385 SCBUSCR_SEL | SCBUSCR_DOUT | sc->sc_busc);
388 if ((nsp_io_control & NSP_WAIT_FOR_SELECT) != 0)
390 #define NSP_FIRST_SEL_WAIT 300
391 #define NSP_SEL_CHECK_INTERVAL 10
393 /* wait for a selection response */
394 for (wc = 0; wc < NSP_FIRST_SEL_WAIT / NSP_SEL_CHECK_INTERVAL;
397 ph = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
398 if ((ph & SCBUSMON_BSY) == 0)
400 SCSI_LOW_DELAY(NSP_SEL_CHECK_INTERVAL);
405 ph = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
406 if ((ph & SCBUSMON_BSY) != 0)
408 nsphw_selection_done_and_expect_msgout(sc);
411 SCSI_LOW_SETUP_PHASE(ti, PH_SELECTED);
412 return SCSI_LOW_START_OK;
418 /* check a selection timeout */
419 nsp_start_timer(sc, NSP_TIMER_1MS);
421 return SCSI_LOW_START_OK;
425 nsp_world_start(struct nsp_softc *sc, int fdone)
427 struct scsi_low_softc *slp = &sc->sc_sclow;
432 if ((slp->sl_cfgflags & CFG_NOATTEN) == 0)
433 sc->sc_busc = SCBUSCR_ATN;
437 if ((slp->sl_cfgflags & CFG_NOPARITY) == 0)
438 sc->sc_parr = PARITYR_ENABLE | PARITYR_CLEAR;
442 sc->sc_icr = (SCIENR_SCCHG | SCIENR_RESEL | SCIENR_RST);
445 scsi_low_bus_reset(slp);
447 SOFT_INTR_REQUIRED(slp);
451 struct ncp_synch_data {
458 static struct ncp_synch_data ncp_sync_data_40M[] = {
459 {0x0c,0x0c,0x1,0}, /* 20MB 50ns*/
460 {0x19,0x19,0x3,1}, /* 10MB 100ns*/
461 {0x1a,0x25,0x5,2}, /* 7.5MB 150ns*/
462 {0x26,0x32,0x7,3}, /* 5MB 200ns*/
466 static struct ncp_synch_data ncp_sync_data_20M[] = {
467 {0x19,0x19,0x1,0}, /* 10MB 100ns*/
468 {0x1a,0x25,0x2,0}, /* 7.5MB 150ns*/
469 {0x26,0x32,0x3,1}, /* 5MB 200ns*/
474 nsp_msg(struct nsp_softc *sc, struct targ_info *ti, u_int msg)
476 bus_space_tag_t bst = sc->sc_iot;
477 bus_space_handle_t bsh = sc->sc_ioh;
478 struct ncp_synch_data *sdp;
479 struct nsp_targ_info *nti = (void *) ti;
480 u_int period, offset;
483 if ((msg & SCSI_LOW_MSG_WIDE) != 0)
485 if (ti->ti_width != SCSI_LOW_BUS_WIDTH_8)
487 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
493 if ((msg & SCSI_LOW_MSG_SYNCH) == 0)
496 period = ti->ti_maxsynch.period;
497 offset = ti->ti_maxsynch.offset;
498 if (sc->sc_iclkdiv == CLKDIVR_20M)
499 sdp = &ncp_sync_data_20M[0];
501 sdp = &ncp_sync_data_40M[0];
503 for (i = 0; sdp->max_period != 0; i ++, sdp ++)
505 if (period >= sdp->min_period && period <= sdp->max_period)
509 if (period != 0 && sdp->max_period == 0)
512 * NO proper period/offset found,
513 * Retry neg with the target.
515 ti->ti_maxsynch.period = 0;
516 ti->ti_maxsynch.offset = 0;
517 nti->nti_reg_syncr = 0;
518 nti->nti_reg_ackwidth = 0;
523 nti->nti_reg_syncr = (sdp->chip_period << SYNCR_PERS) |
524 (offset & SYNCR_OFFM);
525 nti->nti_reg_ackwidth = sdp->ack_width;
529 nsp_cr_write_1(bst, bsh, NSPR_SYNCR, nti->nti_reg_syncr);
530 nsp_cr_write_1(bst, bsh, NSPR_ACKWIDTH, nti->nti_reg_ackwidth);
535 nsp_targ_init(struct nsp_softc *sc, struct targ_info *ti, int action)
537 struct nsp_targ_info *nti = (void *) ti;
539 if (action == SCSI_LOW_INFO_ALLOC || action == SCSI_LOW_INFO_REVOKE)
541 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
542 ti->ti_maxsynch.period = 100 / 4;
543 ti->ti_maxsynch.offset = 15;
544 nti->nti_reg_syncr = 0;
545 nti->nti_reg_ackwidth = 0;
551 nsp_start_timer(struct nsp_softc *sc, int time)
553 bus_space_tag_t bst = sc->sc_iot;
554 bus_space_handle_t bsh = sc->sc_ioh;
557 nsp_cr_write_1(bst, bsh, NSPR_TIMERCNT, time);
560 /**************************************************************
561 * General probe attach
562 **************************************************************/
564 nspprobesubr(bus_space_tag_t iot, bus_space_handle_t ioh, u_int dvcfg)
568 regv = bus_space_read_1(iot, ioh, nsp_fifosr);
569 if (regv < 0x11 || regv >= 0x20)
575 nspprint(void *aux, const char *name)
579 kprintf("%s: scsibus ", name);
584 nspattachsubr(struct nsp_softc *sc)
586 struct scsi_low_softc *slp = &sc->sc_sclow;
590 sc->sc_idbit = (1 << slp->sl_hostid);
591 slp->sl_flags |= HW_READ_PADDING;
592 slp->sl_funcs = &nspfuncs;
593 sc->sc_tmaxcnt = SCSI_LOW_MIN_TOUT * 1000 * 1000; /* default */
595 (void) scsi_low_attach(slp, 0, NSP_NTARGETS, NSP_NLUNS,
596 sizeof(struct nsp_targ_info), 0);
599 /**************************************************************
601 **************************************************************/
603 nsp_fifo_count(struct nsp_softc *sc)
605 bus_space_tag_t bst = sc->sc_iot;
606 bus_space_handle_t bsh = sc->sc_ioh;
609 nsp_cr_write_1(bst, bsh, NSPR_PTCLRR, PTCLRR_RSS_ACK | PTCLRR_PT);
610 count = bus_space_read_1(bst, bsh, nsp_datar);
611 count += (((u_int) bus_space_read_1(bst, bsh, nsp_datar)) << 8);
612 count += (((u_int) bus_space_read_1(bst, bsh, nsp_datar)) << 16);
617 nsp_request_count(struct nsp_softc *sc)
619 bus_space_tag_t bst = sc->sc_iot;
620 bus_space_handle_t bsh = sc->sc_ioh;
623 nsp_cr_write_1(bst, bsh, NSPR_PTCLRR, PTCLRR_RSS_REQ | PTCLRR_PT);
624 count = bus_space_read_1(bst, bsh, nsp_datar);
625 count += (((u_int) bus_space_read_1(bst, bsh, nsp_datar)) << 8);
626 count += (((u_int) bus_space_read_1(bst, bsh, nsp_datar)) << 16);
631 nsp_setup_fifo(struct nsp_softc *sc, int on, int direction, int datalen)
635 sc->sc_suspendio = 0;
636 if (on == NSP_FIFO_OFF)
638 xfermode = XFERMR_IO8;
642 /* check if suspend io OK ? */
645 if (direction == SCSI_LOW_READ)
647 if ((nsp_io_control & NSP_READ_SUSPEND_IO) != 0 &&
648 (datalen % nsp_read_suspend_bytes) == 0)
649 sc->sc_suspendio = nsp_read_suspend_bytes;
653 if ((nsp_io_control & NSP_WRITE_SUSPEND_IO) != 0 &&
654 (datalen % nsp_write_suspend_bytes) == 0)
655 sc->sc_suspendio = nsp_write_suspend_bytes;
659 /* determine a transfer type */
660 if (datalen < DEV_BSIZE || (datalen & 3) != 0)
662 if (sc->sc_memh != 0 &&
663 (nsp_io_control & NSP_USE_MEMIO) != 0)
664 xfermode = XFERMR_XEN | XFERMR_MEM8;
666 xfermode = XFERMR_XEN | XFERMR_IO8;
670 if (sc->sc_memh != 0 &&
671 (nsp_io_control & NSP_USE_MEMIO) != 0)
672 xfermode = XFERMR_XEN | XFERMR_MEM32;
674 xfermode = XFERMR_XEN | XFERMR_IO32;
676 if (sc->sc_suspendio > 0)
677 xfermode |= XFERMR_FIFOEN;
681 sc->sc_xfermr = xfermode;
682 nsp_cr_write_1(sc->sc_iot, sc->sc_ioh, NSPR_XFERMR, sc->sc_xfermr);
686 nsp_pdma_end(struct nsp_softc *sc, struct targ_info *ti)
688 struct scsi_low_softc *slp = &sc->sc_sclow;
689 struct slccb *cb = slp->sl_Qnexus;
692 sc->sc_dataout_timeout = 0;
693 slp->sl_flags &= ~HW_PDMASTART;
694 nsp_setup_fifo(sc, NSP_FIFO_OFF, SCSI_LOW_READ, 0);
695 if ((sc->sc_icr & SCIENR_FIFO) != 0)
697 sc->sc_icr &= ~SCIENR_FIFO;
698 nsp_cr_write_1(sc->sc_iot, sc->sc_ioh, NSPR_SCIENR, sc->sc_icr);
703 slp->sl_error |= PDMAERR;
707 if (ti->ti_phase == PH_DATA)
709 cnt = nsp_fifo_count(sc);
710 if (slp->sl_scp.scp_direction == SCSI_LOW_WRITE)
712 len = sc->sc_cnt - cnt;
713 if (sc->sc_cnt >= cnt &&
714 slp->sl_scp.scp_datalen + len <=
715 cb->ccb_scp.scp_datalen)
717 slp->sl_scp.scp_data -= len;
718 slp->sl_scp.scp_datalen += len;
722 slp->sl_error |= PDMAERR;
723 kprintf("%s len %x >= datalen %x\n",
725 len, slp->sl_scp.scp_datalen);
728 else if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
730 if (sc->sc_cnt != cnt ||
731 sc->sc_cnt > cb->ccb_scp.scp_datalen)
733 slp->sl_error |= PDMAERR;
734 kprintf("%s: data read count error %x != %x (%x)\n",
735 slp->sl_xname, sc->sc_cnt, cnt,
736 cb->ccb_scp.scp_datalen);
740 scsi_low_data_finish(slp);
745 kprintf("%s data phase miss\n", slp->sl_xname);
746 slp->sl_error |= PDMAERR;
750 #define RFIFO_CRIT 64
751 #define WFIFO_CRIT 32
754 nsp_data_padding(struct nsp_softc *sc, int direction, u_int count)
756 bus_space_tag_t bst = sc->sc_iot;
757 bus_space_handle_t bsh = sc->sc_ioh;
759 if (count > NSP_MAX_DATA_SIZE)
760 count = NSP_MAX_DATA_SIZE;
762 nsp_cr_write_1(bst, bsh, NSPR_XFERMR, XFERMR_XEN | XFERMR_IO8);
763 if (direction == SCSI_LOW_READ)
766 (void) bus_space_read_1(bst, bsh, nsp_fifodr);
771 (void) bus_space_write_1(bst, bsh, nsp_fifodr, 0);
773 nsp_cr_write_1(bst, bsh, NSPR_XFERMR, sc->sc_xfermr);
777 nsp_read_fifo(struct nsp_softc *sc, int suspendio)
779 struct scsi_low_softc *slp = &sc->sc_sclow;
780 bus_space_tag_t bst = sc->sc_iot;
781 bus_space_handle_t bsh = sc->sc_ioh;
784 res = nsp_fifo_count(sc);
785 if (res == sc->sc_cnt)
789 if (res < sc->sc_cnt || res == (u_int) -1)
791 kprintf("%s: strange fifo ack count 0x%x < 0x%x\n",
792 slp->sl_xname, res, sc->sc_cnt);
795 #endif /* NSP_DEBUG */
797 res = res - sc->sc_cnt;
798 if (res > slp->sl_scp.scp_datalen)
800 if ((slp->sl_error & PDMAERR) == 0)
802 kprintf("%s: data overrun 0x%x > 0x%x\n",
803 slp->sl_xname, res, slp->sl_scp.scp_datalen);
806 slp->sl_error |= PDMAERR;
807 slp->sl_scp.scp_datalen = 0;
809 if ((slp->sl_flags & HW_READ_PADDING) == 0)
811 kprintf("%s: read padding required\n", slp->sl_xname);
815 nsp_data_padding(sc, SCSI_LOW_READ, res);
817 return 1; /* padding start */
820 if (suspendio > 0 && slp->sl_scp.scp_datalen >= suspendio)
823 if ((sc->sc_xfermr & (XFERMR_MEM32 | XFERMR_MEM8)) != 0)
825 if ((sc->sc_xfermr & XFERMR_MEM32) != 0)
828 bus_space_read_region_4(sc->sc_memt, sc->sc_memh, 0,
829 (u_int32_t *) slp->sl_scp.scp_data, res >> 2);
833 bus_space_read_region_1(sc->sc_memt, sc->sc_memh, 0,
834 (u_int8_t *) slp->sl_scp.scp_data, res);
839 if ((sc->sc_xfermr & XFERMR_IO32) != 0)
842 bus_space_read_multi_4(bst, bsh, nsp_fifodr,
843 (u_int32_t *) slp->sl_scp.scp_data, res >> 2);
847 bus_space_read_multi_1(bst, bsh, nsp_fifodr,
848 (u_int8_t *) slp->sl_scp.scp_data, res);
852 if (nsp_cr_read_1(bst, bsh, NSPR_PARITYR) & PARITYR_PE)
854 nsp_cr_write_1(bst, bsh, NSPR_PARITYR,
855 PARITYR_ENABLE | PARITYR_CLEAR);
856 scsi_low_assert_msg(slp, slp->sl_Tnexus, SCSI_LOW_MSG_ERROR, 1);
859 slp->sl_scp.scp_data += res;
860 slp->sl_scp.scp_datalen -= res;
866 nsp_write_fifo(struct nsp_softc *sc, int suspendio)
868 struct scsi_low_softc *slp = &sc->sc_sclow;
869 bus_space_tag_t bst = sc->sc_iot;
870 bus_space_handle_t bsh = sc->sc_ioh;
877 if ((slp->sl_scp.scp_datalen % WFIFO_CRIT) != 0)
879 kprintf("%s: strange write length 0x%x\n",
880 slp->sl_xname, slp->sl_scp.scp_datalen);
882 #endif /* NSP_DEBUG */
883 res = slp->sl_scp.scp_datalen % suspendio;
894 if (res > slp->sl_scp.scp_datalen)
895 res = slp->sl_scp.scp_datalen;
897 /* XXX: reconfirm! */
898 stat = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON) & SCBUSMON_PHMASK;
899 if (stat != PHASE_DATAOUT)
902 if ((sc->sc_xfermr & (XFERMR_MEM32 | XFERMR_MEM8)) != 0)
904 if ((sc->sc_xfermr & XFERMR_MEM32) != 0)
906 bus_space_write_region_4(sc->sc_memt, sc->sc_memh, 0,
907 (u_int32_t *) slp->sl_scp.scp_data, res >> 2);
911 bus_space_write_region_1(sc->sc_memt, sc->sc_memh, 0,
912 (u_int8_t *) slp->sl_scp.scp_data, res);
917 if ((sc->sc_xfermr & XFERMR_IO32) != 0)
919 bus_space_write_multi_4(bst, bsh, nsp_fifodr,
920 (u_int32_t *) slp->sl_scp.scp_data, res >> 2);
924 bus_space_write_multi_1(bst, bsh, nsp_fifodr,
925 (u_int8_t *) slp->sl_scp.scp_data, res);
929 slp->sl_scp.scp_datalen -= res;
930 slp->sl_scp.scp_data += res;
936 nsp_wait_interrupt(struct nsp_softc *sc)
938 bus_space_tag_t bst = sc->sc_iot;
939 bus_space_handle_t bsh = sc->sc_ioh;
943 for (tout = 0; tout < DEV_BSIZE / 10; tout ++)
945 isrc = bus_space_read_1(bst, bsh, nsp_irqsr);
946 if ((isrc & (IRQSR_SCSI | IRQSR_FIFO)) != 0)
948 if ((isrc & IRQSR_FIFO) != 0)
950 bus_space_write_1(bst, bsh,
951 nsp_irqcr, IRQCR_FIFOCL);
961 nsp_pio_read(struct nsp_softc *sc, int suspendio)
963 struct scsi_low_softc *slp = &sc->sc_sclow;
964 bus_space_tag_t bst = sc->sc_iot;
965 bus_space_handle_t bsh = sc->sc_ioh;
966 int tout, padding, datalen;
967 u_int8_t stat, fstat;
970 tout = sc->sc_tmaxcnt;
971 slp->sl_flags |= HW_PDMASTART;
972 datalen = slp->sl_scp.scp_datalen;
977 stat = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
978 if (stat == (u_int8_t) -1)
981 /* out of data phase */
982 if ((stat & SCBUSMON_PHMASK) != PHASE_DATAIN)
984 nsp_read_fifo(sc, 0);
989 fstat = bus_space_read_1(bst, bsh, nsp_fifosr);
990 if ((fstat & FIFOSR_FULLEMP) != 0)
992 if ((sc->sc_icr & SCIENR_FIFO) != 0)
994 bus_space_write_1(bst, bsh, nsp_irqcr,
1000 padding |= nsp_read_fifo(sc, suspendio);
1004 padding |= nsp_read_fifo(sc, 0);
1007 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1012 if (padding == 0 && slp->sl_scp.scp_datalen <= 0)
1015 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1023 kprintf("%s: nsp_pio_read: timeout\n", slp->sl_xname);
1029 if (slp->sl_scp.scp_datalen > 0 &&
1030 slp->sl_scp.scp_datalen > datalen - nsp_read_interrupt_bytes)
1032 if (nsp_wait_interrupt(sc) != 0)
1038 nsp_pio_write(struct nsp_softc *sc, int suspendio)
1040 struct scsi_low_softc *slp = &sc->sc_sclow;
1041 bus_space_tag_t bst = sc->sc_iot;
1042 bus_space_handle_t bsh = sc->sc_ioh;
1043 u_int rcount, acount;
1045 u_int8_t stat, fstat;
1047 tout = sc->sc_tmaxcnt;
1048 slp->sl_flags |= HW_PDMASTART;
1049 datalen = slp->sl_scp.scp_datalen;
1054 stat = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON) & SCBUSMON_PHMASK;
1055 if (stat != PHASE_DATAOUT)
1058 if (slp->sl_scp.scp_datalen <= 0)
1060 if (sc->sc_dataout_timeout == 0)
1061 sc->sc_dataout_timeout = SCSI_LOW_TIMEOUT_HZ;
1065 fstat = bus_space_read_1(bst, bsh, nsp_fifosr);
1066 if ((fstat & FIFOSR_FULLEMP) != 0)
1068 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1070 bus_space_write_1(bst, bsh, nsp_irqcr,
1077 * To avoid timeout of pcmcia bus
1078 * (not scsi bus!), we should check
1079 * the scsi device sends us request
1080 * signals, which means the scsi device
1081 * is ready to recieve data without
1084 if ((slp->sl_scp.scp_datalen % suspendio) == 0)
1087 * fill the nsp fifo, and waiting for
1090 nsp_write_fifo(sc, 0);
1095 * check the request singals.
1097 acount = nsp_fifo_count(sc);
1098 rcount = nsp_request_count(sc);
1099 if (rcount <= acount)
1101 nsp_write_fifo(sc, 0);
1103 nsp_statics.device_busy ++;
1104 #endif /* NSP_STATICS */
1108 nsp_write_fifo(sc, suspendio);
1110 nsp_statics.device_data_write ++;
1111 #endif /* NSP_STATICS */
1117 nsp_write_fifo(sc, 0);
1120 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1125 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1133 kprintf("%s: nsp_pio_write: timeout\n", slp->sl_xname);
1138 if (slp->sl_scp.scp_datalen > 0 &&
1139 slp->sl_scp.scp_datalen > datalen - nsp_write_interrupt_bytes)
1141 if (nsp_wait_interrupt(sc) != 0)
1147 nsp_negate_signal(struct nsp_softc *sc, u_int8_t mask, u_char *s)
1149 struct scsi_low_softc *slp = &sc->sc_sclow;
1150 bus_space_tag_t bst = sc->sc_iot;
1151 bus_space_handle_t bsh = sc->sc_ioh;
1155 for (wc = 0; wc < NSP_DELAY_MAX / NSP_DELAY_INTERVAL; wc ++)
1157 regv = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
1158 if (regv == (u_int8_t) -1)
1160 if ((regv & mask) == 0)
1162 SCSI_LOW_DELAY(NSP_DELAY_INTERVAL);
1165 kprintf("%s: %s nsp_negate_signal timeout\n", slp->sl_xname, s);
1170 nsp_xfer(struct nsp_softc *sc, u_int8_t *buf, int len, int phase,
1173 bus_space_tag_t bst = sc->sc_iot;
1174 bus_space_handle_t bsh = sc->sc_ioh;
1177 for (ptr = 0; len > 0; len --, ptr ++)
1179 rv = nsp_expect_signal(sc, phase, SCBUSMON_REQ);
1183 if (len == 1 && clear_atn != 0)
1185 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR,
1186 SCBUSCR_ADIR | SCBUSCR_ACKEN);
1187 SCSI_LOW_DEASSERT_ATN(&sc->sc_sclow);
1190 if (phase & SCBUSMON_IO)
1192 buf[ptr] = nsp_cr_read_1(bst, bsh, NSPR_DATAACK);
1196 nsp_cr_write_1(bst, bsh, NSPR_DATAACK, buf[ptr]);
1198 nsp_negate_signal(sc, SCBUSMON_ACK, "xfer<ACK>");
1205 /**************************************************************
1206 * disconnect & reselect (HW low)
1207 **************************************************************/
1209 nsp_reselected(struct nsp_softc *sc)
1211 struct scsi_low_softc *slp = &sc->sc_sclow;
1212 bus_space_tag_t bst = sc->sc_iot;
1213 bus_space_handle_t bsh = sc->sc_ioh;
1214 struct targ_info *ti;
1218 sid = (u_int) nsp_cr_read_1(bst, bsh, NSPR_RESELR);
1219 sid &= ~sc->sc_idbit;
1221 if ((ti = scsi_low_reselected(slp, sid)) == NULL)
1224 nsp_negate_signal(sc, SCBUSMON_SEL, "reselect<SEL>");
1226 cr = nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR);
1227 cr &= ~(SCBUSCR_BSY | SCBUSCR_ATN);
1228 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1229 cr |= SCBUSCR_ADIR | SCBUSCR_ACKEN;
1230 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1233 nsp_statics.reselect ++;
1234 #endif /* NSP_STATCIS */
1239 nsp_disconnected(struct nsp_softc *sc, struct targ_info *ti)
1241 struct scsi_low_softc *slp = &sc->sc_sclow;
1242 bus_space_tag_t bst = sc->sc_iot;
1243 bus_space_handle_t bsh = sc->sc_ioh;
1245 nsp_cr_write_1(bst, bsh, NSPR_PTCLRR, PTCLRR_PT | PTCLRR_ACK |
1246 PTCLRR_REQ | PTCLRR_HOST);
1247 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1249 sc->sc_icr &= ~SCIENR_FIFO;
1250 nsp_cr_write_1(bst, bsh, NSPR_SCIENR, sc->sc_icr);
1253 sc->sc_dataout_timeout = 0;
1255 nsp_statics.disconnect ++;
1256 #endif /* NSP_STATICS */
1257 scsi_low_disconnected(slp, ti);
1261 /**************************************************************
1263 **************************************************************/
1264 static void nsp_error (struct nsp_softc *, u_char *, u_int8_t, u_int8_t, u_int8_t);
1267 nsp_error(struct nsp_softc *sc, u_char *s, u_int8_t isrc, u_int8_t ph,
1270 struct scsi_low_softc *slp = &sc->sc_sclow;
1272 kprintf("%s: %s\n", slp->sl_xname, s);
1273 kprintf("%s: isrc 0x%x scmon 0x%x irqphs 0x%x\n",
1274 slp->sl_xname, (u_int) isrc, (u_int) ph, (u_int) irqphs);
1278 nsp_target_nexus_establish(struct nsp_softc *sc)
1280 struct scsi_low_softc *slp = &sc->sc_sclow;
1281 bus_space_tag_t bst = sc->sc_iot;
1282 bus_space_handle_t bsh = sc->sc_ioh;
1283 struct targ_info *ti = slp->sl_Tnexus;
1284 struct nsp_targ_info *nti = (void *) ti;
1286 /* setup synch transfer registers */
1287 nsp_cr_write_1(bst, bsh, NSPR_SYNCR, nti->nti_reg_syncr);
1288 nsp_cr_write_1(bst, bsh, NSPR_ACKWIDTH, nti->nti_reg_ackwidth);
1290 /* setup pdma fifo (minimum) */
1291 nsp_setup_fifo(sc, NSP_FIFO_ON, SCSI_LOW_READ, 0);
1296 nsp_lun_nexus_establish(struct nsp_softc *sc)
1303 nsp_ccb_nexus_establish(struct nsp_softc *sc)
1305 struct scsi_low_softc *slp = &sc->sc_sclow;
1306 struct slccb *cb = slp->sl_Qnexus;
1308 sc->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
1310 /* setup pdma fifo */
1311 nsp_setup_fifo(sc, NSP_FIFO_ON,
1312 slp->sl_scp.scp_direction, slp->sl_scp.scp_datalen);
1314 if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
1316 if (sc->sc_suspendio > 0 &&
1317 (nsp_io_control & NSP_READ_FIFO_INTERRUPTS) != 0)
1319 sc->sc_icr |= SCIENR_FIFO;
1320 nsp_cr_write_1(sc->sc_iot, sc->sc_ioh,
1321 NSPR_SCIENR, sc->sc_icr);
1326 if (sc->sc_suspendio > 0 &&
1327 (nsp_io_control & NSP_WRITE_FIFO_INTERRUPTS) != 0)
1329 sc->sc_icr |= SCIENR_FIFO;
1330 nsp_cr_write_1(sc->sc_iot, sc->sc_ioh,
1331 NSPR_SCIENR, sc->sc_icr);
1338 nsp_phase_match(struct nsp_softc *sc, u_int8_t phase, u_int8_t stat)
1340 struct scsi_low_softc *slp = &sc->sc_sclow;
1342 if ((stat & SCBUSMON_PHMASK) != phase)
1344 kprintf("%s: phase mismatch 0x%x != 0x%x\n",
1345 slp->sl_xname, (u_int) phase, (u_int) stat);
1349 if ((stat & SCBUSMON_REQ) == 0)
1358 struct nsp_softc *sc = arg;
1359 struct scsi_low_softc *slp = &sc->sc_sclow;
1360 bus_space_tag_t bst = sc->sc_iot;
1361 bus_space_handle_t bsh = sc->sc_ioh;
1362 struct targ_info *ti;
1364 u_int derror, flags;
1366 u_int8_t isrc, ph, irqphs, cr, regv;
1368 /*******************************************
1370 *******************************************/
1371 if (slp->sl_flags & HW_INACTIVE)
1374 bus_space_write_1(bst, bsh, nsp_irqcr, IRQCR_IRQDIS);
1375 isrc = bus_space_read_1(bst, bsh, nsp_irqsr);
1376 if (isrc == (u_int8_t) -1 || (isrc & IRQSR_MASK) == 0)
1378 bus_space_write_1(bst, bsh, nsp_irqcr, 0);
1383 * Do not read an irqphs register if no scsi phase interrupt.
1384 * Unless, you should lose a scsi phase interrupt.
1386 ph = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
1387 if ((isrc & IRQSR_SCSI) != 0)
1389 irqphs = nsp_cr_read_1(bst, bsh, NSPR_IRQPHS);
1395 * timer interrupt handler (scsi vs timer interrupts)
1397 if (sc->sc_timer != 0)
1399 nsp_cr_write_1(bst, bsh, NSPR_TIMERCNT, 0);
1400 nsp_cr_write_1(bst, bsh, NSPR_TIMERCNT, 0);
1404 /* check a timer interrupt */
1406 if ((isrc & IRQSR_TIMER) != 0)
1408 if ((isrc & IRQSR_MASK) == IRQSR_TIMER && sc->sc_seltout == 0)
1410 bus_space_write_1(bst, bsh, nsp_irqcr, IRQCR_TIMERCL);
1413 regv |= IRQCR_TIMERCL;
1416 /* check a fifo interrupt */
1417 if ((isrc & IRQSR_FIFO) != 0)
1419 regv |= IRQCR_FIFOCL;
1422 /* OK. enable all interrupts */
1423 bus_space_write_1(bst, bsh, nsp_irqcr, regv);
1425 /*******************************************
1427 *******************************************/
1431 nsp_error(sc, "current status", isrc, ph, irqphs);
1432 scsi_low_print(slp, NULL);
1435 SCSI_LOW_DEBUGGER("nsp");
1438 #endif /* NSP_DEBUG */
1440 /*******************************************
1441 * Parse hardware SCSI irq reasons register
1442 *******************************************/
1443 if ((isrc & IRQSR_SCSI) != 0)
1445 if ((irqphs & IRQPHS_RST) != 0)
1447 scsi_low_restart(slp, SCSI_LOW_RESTART_SOFT,
1448 "bus reset (power off?)");
1452 if ((irqphs & IRQPHS_RSEL) != 0)
1454 bus_space_write_1(bst, bsh, nsp_irqcr, IRQCR_RESCL);
1455 if (nsp_reselected(sc) == EJUSTRETURN)
1459 if ((irqphs & (IRQPHS_PCHG | IRQPHS_LBF)) == 0)
1463 /*******************************************
1465 *******************************************/
1466 if ((ti = slp->sl_Tnexus) == NULL)
1468 /* unknown scsi phase changes */
1469 nsp_error(sc, "unknown scsi phase changes", isrc, ph, irqphs);
1473 /*******************************************
1474 * aribitration & selection
1475 *******************************************/
1476 switch (ti->ti_phase)
1479 if ((ph & SCBUSMON_BSY) == 0)
1481 if (sc->sc_seltout >= NSP_SELTIMEOUT)
1484 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, 0);
1485 return nsp_disconnected(sc, ti);
1488 nsp_start_timer(sc, NSP_TIMER_1MS);
1492 SCSI_LOW_SETUP_PHASE(ti, PH_SELECTED);
1493 nsphw_selection_done_and_expect_msgout(sc);
1497 if ((isrc & IRQSR_SCSI) == 0)
1500 nsp_target_nexus_establish(sc);
1504 if ((isrc & IRQSR_SCSI) == 0)
1507 nsp_target_nexus_establish(sc);
1508 if ((ph & SCBUSMON_PHMASK) != PHASE_MSGIN)
1510 kprintf("%s: unexpected phase after reselect\n",
1512 slp->sl_error |= FATALIO;
1513 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 1);
1519 if ((isrc & IRQSR_SCSI) != 0)
1521 if ((isrc & IRQSR_FIFO) != 0)
1523 if (NSP_IS_PHASE_DATA(ph) == 0)
1525 irqphs = (ph & IRQPHS_PHMASK);
1531 if ((isrc & IRQSR_SCSI) == 0)
1536 /*******************************************
1537 * data phase control
1538 *******************************************/
1539 if (slp->sl_flags & HW_PDMASTART)
1541 if ((isrc & IRQSR_SCSI) != 0 &&
1542 NSP_IS_IRQPHS_DATA(irqphs) == 0)
1544 if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
1545 nsp_pio_read(sc, 0);
1546 nsp_pdma_end(sc, ti);
1550 /*******************************************
1552 *******************************************/
1553 if (slp->sl_msgphase != 0 && (irqphs & IRQPHS_LBF) != 0)
1554 return nsp_disconnected(sc, ti);
1556 /* check unexpected bus free state */
1559 nsp_error(sc, "unexpected bus free", isrc, ph, irqphs);
1560 return nsp_disconnected(sc, ti);
1563 /* check normal scsi phase */
1564 switch (irqphs & IRQPHS_PHMASK)
1567 if (nsp_phase_match(sc, PHASE_CMD, ph) != 0)
1570 SCSI_LOW_SETUP_PHASE(ti, PH_CMD);
1571 if (scsi_low_cmd(slp, ti) != 0)
1573 scsi_low_attention(slp);
1576 nsp_cr_write_1(bst, bsh, NSPR_CMDCR, CMDCR_PTCLR);
1577 for (len = 0; len < slp->sl_scp.scp_cmdlen; len ++)
1578 nsp_cr_write_1(bst, bsh, NSPR_CMDDR,
1579 slp->sl_scp.scp_cmd[len]);
1581 nsp_cr_write_1(bst, bsh, NSPR_CMDCR, CMDCR_PTCLR | CMDCR_EXEC);
1584 case IRQPHS_DATAOUT:
1585 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1586 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_WRITE) != 0)
1588 scsi_low_attention(slp);
1591 nsp_pio_write(sc, sc->sc_suspendio);
1595 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1596 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_READ) != 0)
1598 scsi_low_attention(slp);
1601 nsp_pio_read(sc, sc->sc_suspendio);
1605 if (nsp_phase_match(sc, PHASE_STATUS, ph) != 0)
1608 SCSI_LOW_SETUP_PHASE(ti, PH_STAT);
1609 regv = nsp_cr_read_1(bst, bsh, NSPR_DATA);
1610 if (nsp_cr_read_1(bst, bsh, NSPR_PARITYR) & PARITYR_PE)
1612 nsp_cr_write_1(bst, bsh, NSPR_PARITYR,
1613 PARITYR_ENABLE | PARITYR_CLEAR);
1614 derror = SCSI_LOW_DATA_PE;
1620 cr = SCBUSCR_ACK | nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR);
1621 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1623 if (scsi_low_statusin(slp, ti, derror | regv) != 0)
1625 scsi_low_attention(slp);
1628 /* check REQ nagated */
1629 nsp_negate_signal(sc, SCBUSMON_REQ, "statin<REQ>");
1632 cr = nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR) & (~SCBUSCR_ACK);
1633 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1637 if (nsp_phase_match(sc, PHASE_MSGOUT, ph) != 0)
1640 #ifdef NSP_MSGOUT_SERIALIZE
1643 * NSP invoke interrupts only in the case of scsi phase changes,
1644 * therefore we should poll the scsi phase here to catch
1645 * the next "msg out" if exists (no scsi phase changes).
1649 SCSI_LOW_SETUP_PHASE(ti, PH_MSGOUT);
1650 flags = (ti->ti_ophase != ti->ti_phase) ?
1651 SCSI_LOW_MSGOUT_INIT : 0;
1652 len = scsi_low_msgout(slp, ti, flags);
1654 if (len > 1 && slp->sl_atten == 0)
1656 scsi_low_attention(slp);
1659 if (nsp_xfer(sc, ti->ti_msgoutstr, len, PHASE_MSGOUT,
1660 slp->sl_clear_atten) != 0)
1662 slp->sl_error |= FATALIO;
1663 nsp_error(sc, "MSGOUT: xfer short",
1667 /* catch a next signal */
1668 rv = nsp_expect_signal(sc, PHASE_MSGOUT, SCBUSMON_REQ);
1670 while (rv > 0 && len -- > 0);
1672 #else /* !NSP_MSGOUT_SERIALIZE */
1673 SCSI_LOW_SETUP_PHASE(ti, PH_MSGOUT);
1674 flags = SCSI_LOW_MSGOUT_UNIFY;
1675 if (ti->ti_ophase != ti->ti_phase)
1676 flags |= SCSI_LOW_MSGOUT_INIT;
1677 len = scsi_low_msgout(slp, ti, flags);
1679 if (len > 1 && slp->sl_atten == 0)
1681 scsi_low_attention(slp);
1684 if (nsp_xfer(sc, ti->ti_msgoutstr, len, PHASE_MSGOUT,
1685 slp->sl_clear_atten) != 0)
1687 nsp_error(sc, "MSGOUT: xfer short", isrc, ph, irqphs);
1690 #endif /* !NSP_MSGOUT_SERIALIZE */
1694 if (nsp_phase_match(sc, PHASE_MSGIN, ph) != 0)
1699 * NSP invoke interrupts only in the case of scsi phase changes,
1700 * therefore we should poll the scsi phase here to catch
1701 * the next "msg in" if exists (no scsi phase changes).
1705 SCSI_LOW_SETUP_PHASE(ti, PH_MSGIN);
1708 regv = nsp_cr_read_1(bst, bsh, NSPR_DATA);
1709 if (nsp_cr_read_1(bst, bsh, NSPR_PARITYR) & PARITYR_PE)
1711 nsp_cr_write_1(bst, bsh,
1713 PARITYR_ENABLE | PARITYR_CLEAR);
1714 derror = SCSI_LOW_DATA_PE;
1722 cr = nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR) | SCBUSCR_ACK;
1723 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1725 if (scsi_low_msgin(slp, ti, regv | derror) == 0)
1727 if (scsi_low_is_msgout_continue(ti, 0) != 0)
1729 scsi_low_attention(slp);
1733 /* check REQ nagated */
1734 nsp_negate_signal(sc, SCBUSMON_REQ, "msgin<REQ>");
1737 cr = nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR) & (~SCBUSCR_ACK);
1738 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1740 /* catch a next signal */
1741 rv = nsp_expect_signal(sc, PHASE_MSGIN, SCBUSMON_REQ);
1743 while (rv > 0 && len -- > 0);
1747 slp->sl_error |= FATALIO;
1748 nsp_error(sc, "unknown scsi phase", isrc, ph, irqphs);
1756 nsp_start_timer(sc, NSP_TIMER_1MS);
1762 nsp_timeout(struct nsp_softc *sc)
1764 struct scsi_low_softc *slp = &sc->sc_sclow;
1765 bus_space_tag_t iot = sc->sc_iot;
1766 bus_space_handle_t ioh = sc->sc_ioh;
1770 if (slp->sl_Tnexus == NULL)
1773 ph = nsp_cr_read_1(iot, ioh, NSPR_SCBUSMON);
1774 switch (ph & SCBUSMON_PHMASK)
1777 if (sc->sc_dataout_timeout == 0)
1780 /* check a fifo empty */
1781 regv = bus_space_read_1(iot, ioh, nsp_fifosr);
1782 if ((regv & FIFOSR_FULLEMP) == 0)
1784 bus_space_write_1(iot, ioh, nsp_irqcr, IRQCR_FIFOCL);
1786 /* check still requested */
1787 ph = nsp_cr_read_1(iot, ioh, NSPR_SCBUSMON);
1788 if ((ph & SCBUSMON_REQ) == 0)
1791 if ((-- sc->sc_dataout_timeout) > 0)
1794 slp->sl_error |= PDMAERR;
1795 if ((slp->sl_flags & HW_WRITE_PADDING) == 0)
1797 kprintf("%s: write padding required\n", slp->sl_xname);
1801 tout = NSP_DELAY_MAX;
1804 ph = nsp_cr_read_1(iot, ioh, NSPR_SCBUSMON);
1805 if ((ph & SCBUSMON_PHMASK) != PHASE_DATAOUT)
1807 regv = bus_space_read_1(iot, ioh, nsp_fifosr);
1808 if ((regv & FIFOSR_FULLEMP) == 0)
1814 bus_space_write_1(iot, ioh, nsp_irqcr, IRQCR_FIFOCL);
1815 nsp_data_padding(sc, SCSI_LOW_WRITE, 32);
1817 ph = nsp_cr_read_1(iot, ioh, NSPR_SCBUSMON);
1818 if ((ph & SCBUSMON_PHMASK) == PHASE_DATAOUT)
1819 sc->sc_dataout_timeout = SCSI_LOW_TIMEOUT_HZ;