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>
48 #if defined(__FreeBSD__) && __FreeBSD_version > 500001
50 #endif /* __ FreeBSD__ */
52 #include <sys/queue.h>
53 #include <sys/malloc.h>
54 #include <sys/errno.h>
57 #include <sys/device.h>
58 #include <machine/bus.h>
59 #include <machine/intr.h>
61 #include <dev/scsipi/scsi_all.h>
62 #include <dev/scsipi/scsipi_all.h>
63 #include <dev/scsipi/scsiconf.h>
64 #include <dev/scsipi/scsi_disk.h>
66 #include <machine/dvcfg.h>
67 #include <machine/physio_proc.h>
69 #include <i386/Cbus/dev/scsi_low.h>
70 #include <i386/Cbus/dev/nspreg.h>
71 #include <i386/Cbus/dev/nspvar.h>
72 #endif /* __NetBSD__ */
75 #include <machine/cpu.h>
76 #include <machine/bus.h>
78 #include <compat/netbsd/dvcfg.h>
79 #include <compat/netbsd/physio_proc.h>
81 #include <cam/scsi/scsi_low.h>
82 #include <dev/nsp/nspreg.h>
83 #include <dev/nsp/nspvar.h>
84 #endif /* __FreeBSD__ */
86 /***************************************************
88 ***************************************************/
89 /* DEVICE CONFIGURATION FLAGS (MINOR)
92 * 0x02 PARITY LINE OFF
93 * 0x04 IDENTIFY MSG OFF ( = single lun)
94 * 0x08 SYNC TRANSFER OFF
97 /***************************************************
99 ***************************************************/
100 #define NSP_NTARGETS 8
103 #define NSP_MAX_DATA_SIZE (64 * 1024)
104 #define NSP_SELTIMEOUT (200)
105 #define NSP_DELAY_MAX (2 * 1000 * 1000)
106 #define NSP_DELAY_INTERVAL (1)
107 #define NSP_TIMER_1MS (1000 / 51)
109 /***************************************************
111 ***************************************************/
114 #endif /* NSP_DEBUG */
118 int arbit_conflict_1;
119 int arbit_conflict_2;
120 int device_data_write;
124 int data_phase_bypass;
126 #endif /* NSP_STATICS */
128 /***************************************************
130 ***************************************************/
131 #define NSP_READ_SUSPEND_IO 0x0001
132 #define NSP_WRITE_SUSPEND_IO 0x0002
133 #define NSP_USE_MEMIO 0x0004
134 #define NSP_READ_FIFO_INTERRUPTS 0x0010
135 #define NSP_WRITE_FIFO_INTERRUPTS 0x0020
136 #define NSP_WAIT_FOR_SELECT 0x0100
138 u_int nsp_io_control = NSP_IO_CONTROL_FLAGS;
139 int nsp_read_suspend_bytes = DEV_BSIZE;
140 int nsp_write_suspend_bytes = DEV_BSIZE;
141 int nsp_read_interrupt_bytes = 4096;
142 int nsp_write_interrupt_bytes = 4096;
144 /***************************************************
146 ***************************************************/
147 extern struct cfdriver nsp_cd;
149 /**************************************************************
151 **************************************************************/
152 #define NSP_FIFO_ON 1
153 #define NSP_FIFO_OFF 0
154 static void nsp_pio_read(struct nsp_softc *, int);
155 static void nsp_pio_write(struct nsp_softc *, int);
156 static int nsp_xfer(struct nsp_softc *, u_int8_t *, int, int, int);
157 static int nsp_msg(struct nsp_softc *, struct targ_info *, u_int);
158 static int nsp_reselected(struct nsp_softc *);
159 static int nsp_disconnected(struct nsp_softc *, struct targ_info *);
160 static void nsp_pdma_end(struct nsp_softc *, struct targ_info *);
161 static void nsphw_init(struct nsp_softc *);
162 static int nsp_target_nexus_establish(struct nsp_softc *);
163 static int nsp_lun_nexus_establish(struct nsp_softc *);
164 static int nsp_ccb_nexus_establish(struct nsp_softc *);
165 static int nsp_world_start(struct nsp_softc *, int);
166 static int nsphw_start_selection(struct nsp_softc *sc, struct slccb *);
167 static void nsphw_bus_reset(struct nsp_softc *);
168 static void nsphw_attention(struct nsp_softc *);
169 static u_int nsp_fifo_count(struct nsp_softc *);
170 static u_int nsp_request_count(struct nsp_softc *);
171 static int nsp_negate_signal(struct nsp_softc *, u_int8_t, u_char *);
172 static int nsp_expect_signal(struct nsp_softc *, u_int8_t, u_int8_t);
173 static void nsp_start_timer(struct nsp_softc *, int);
174 static void nsp_setup_fifo(struct nsp_softc *, int, int, int);
175 static int nsp_targ_init(struct nsp_softc *, struct targ_info *, int);
176 static void nsphw_selection_done_and_expect_msgout(struct nsp_softc *);
177 static void nsp_data_padding(struct nsp_softc *, int, u_int);
178 static int nsp_timeout(struct nsp_softc *);
179 static int nsp_read_fifo(struct nsp_softc *, int);
180 static int nsp_write_fifo(struct nsp_softc *, int);
181 static int nsp_phase_match(struct nsp_softc *, u_int8_t, u_int8_t);
182 static int nsp_wait_interrupt(struct nsp_softc *);
184 struct scsi_low_funcs nspfuncs = {
185 SC_LOW_INIT_T nsp_world_start,
186 SC_LOW_BUSRST_T nsphw_bus_reset,
187 SC_LOW_TARG_INIT_T nsp_targ_init,
188 SC_LOW_LUN_INIT_T NULL,
190 SC_LOW_SELECT_T nsphw_start_selection,
191 SC_LOW_NEXUS_T nsp_lun_nexus_establish,
192 SC_LOW_NEXUS_T nsp_ccb_nexus_establish,
194 SC_LOW_ATTEN_T nsphw_attention,
195 SC_LOW_MSG_T nsp_msg,
197 SC_LOW_TIMEOUT_T nsp_timeout,
198 SC_LOW_POLL_T nspintr,
203 /****************************************************
205 ****************************************************/
206 static __inline u_int8_t nsp_cr_read_1(bus_space_tag_t bst, bus_space_handle_t bsh, bus_addr_t ofs);
207 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);
209 static __inline u_int8_t
210 nsp_cr_read_1(bst, bsh, ofs)
212 bus_space_handle_t bsh;
216 bus_space_write_1(bst, bsh, nsp_idxr, ofs);
217 return bus_space_read_1(bst, bsh, nsp_datar);
221 nsp_cr_write_1(bus_space_tag_t bst, bus_space_handle_t bsh, bus_addr_t ofs,
225 bus_space_write_1(bst, bsh, nsp_idxr, ofs);
226 bus_space_write_1(bst, bsh, nsp_datar, va);
230 nsp_expect_signal(struct nsp_softc *sc, u_int8_t curphase, u_int8_t mask)
232 struct scsi_low_softc *slp = &sc->sc_sclow;
233 bus_space_tag_t bst = sc->sc_iot;
234 bus_space_handle_t bsh = sc->sc_ioh;
238 for (wc = 0; wc < NSP_DELAY_MAX / NSP_DELAY_INTERVAL; wc ++)
240 ph = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
241 if (ph == (u_int8_t) -1)
244 isrc = bus_space_read_1(bst, bsh, nsp_irqsr);
245 if (isrc & IRQSR_SCSI)
248 if ((ph & mask) != 0 && (ph & SCBUSMON_PHMASK) == curphase)
251 SCSI_LOW_DELAY(NSP_DELAY_INTERVAL);
254 printf("%s: nsp_expect_signal timeout\n", slp->sl_xname);
260 struct nsp_softc *sc;
262 bus_space_tag_t bst = sc->sc_iot;
263 bus_space_handle_t bsh = sc->sc_ioh;
265 /* block all interrupts */
266 bus_space_write_1(bst, bsh, nsp_irqcr, IRQCR_ALLMASK);
268 /* setup SCSI interface */
269 bus_space_write_1(bst, bsh, nsp_ifselr, IFSELR_IFSEL);
271 nsp_cr_write_1(bst, bsh, NSPR_SCIENR, 0);
273 nsp_cr_write_1(bst, bsh, NSPR_XFERMR, XFERMR_IO8);
274 nsp_cr_write_1(bst, bsh, NSPR_CLKDIVR, sc->sc_iclkdiv);
276 nsp_cr_write_1(bst, bsh, NSPR_SCIENR, sc->sc_icr);
277 nsp_cr_write_1(bst, bsh, NSPR_PARITYR, sc->sc_parr);
278 nsp_cr_write_1(bst, bsh, NSPR_PTCLRR,
279 PTCLRR_ACK | PTCLRR_REQ | PTCLRR_HOST | PTCLRR_RSS);
281 /* setup fifo asic */
282 bus_space_write_1(bst, bsh, nsp_ifselr, IFSELR_REGSEL);
283 nsp_cr_write_1(bst, bsh, NSPR_TERMPWRC, 0);
284 if ((nsp_cr_read_1(bst, bsh, NSPR_OCR) & OCR_TERMPWRS) == 0)
285 nsp_cr_write_1(bst, bsh, NSPR_TERMPWRC, TERMPWRC_POWON);
287 nsp_cr_write_1(bst, bsh, NSPR_XFERMR, XFERMR_IO8);
288 nsp_cr_write_1(bst, bsh, NSPR_CLKDIVR, sc->sc_clkdiv);
289 nsp_cr_write_1(bst, bsh, NSPR_TIMERCNT, 0);
290 nsp_cr_write_1(bst, bsh, NSPR_TIMERCNT, 0);
292 nsp_cr_write_1(bst, bsh, NSPR_SYNCR, 0);
293 nsp_cr_write_1(bst, bsh, NSPR_ACKWIDTH, 0);
295 /* enable interrupts and ack them */
296 nsp_cr_write_1(bst, bsh, NSPR_SCIENR, sc->sc_icr);
297 bus_space_write_1(bst, bsh, nsp_irqcr, IRQSR_MASK);
299 nsp_setup_fifo(sc, NSP_FIFO_OFF, SCSI_LOW_READ, 0);
302 /****************************************************
304 ****************************************************/
307 struct nsp_softc *sc;
309 bus_space_tag_t bst = sc->sc_iot;
310 bus_space_handle_t bsh = sc->sc_ioh;
313 cr = nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR)/* & ~SCBUSCR_ACK */;
314 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr | SCBUSCR_ATN);
320 struct nsp_softc *sc;
322 bus_space_tag_t bst = sc->sc_iot;
323 bus_space_handle_t bsh = sc->sc_ioh;
326 bus_space_write_1(bst, bsh, nsp_irqcr, IRQCR_ALLMASK);
328 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, SCBUSCR_RST);
329 SCSI_LOW_DELAY(100 * 1000); /* 100ms */
330 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, 0);
331 for (i = 0; i < 5; i ++)
332 (void) nsp_cr_read_1(bst, bsh, NSPR_IRQPHS);
334 bus_space_write_1(bst, bsh, nsp_irqcr, IRQSR_MASK);
338 nsphw_selection_done_and_expect_msgout(sc)
339 struct nsp_softc *sc;
341 struct scsi_low_softc *slp = &sc->sc_sclow;
342 bus_space_tag_t bst = sc->sc_iot;
343 bus_space_handle_t bsh = sc->sc_ioh;
345 /* clear ack counter */
347 nsp_cr_write_1(bst, bsh, NSPR_PTCLRR, PTCLRR_PT | PTCLRR_ACK |
348 PTCLRR_REQ | PTCLRR_HOST);
350 /* deassert sel and assert atten */
352 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, sc->sc_busc);
354 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR,
355 sc->sc_busc | SCBUSCR_ADIR | SCBUSCR_ACKEN);
356 SCSI_LOW_ASSERT_ATN(slp);
360 nsphw_start_selection(sc, cb)
361 struct nsp_softc *sc;
364 struct scsi_low_softc *slp = &sc->sc_sclow;
365 bus_space_tag_t bst = sc->sc_iot;
366 bus_space_handle_t bsh = sc->sc_ioh;
367 struct targ_info *ti = cb->ti;
368 register u_int8_t arbs, ph;
371 wc = sc->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
372 sc->sc_dataout_timeout = 0;
376 ph = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
377 if (ph != SCBUSMON_FREE)
381 nsp_statics.arbit_conflict_1 ++;
382 #endif /* NSP_STATICS */
383 return SCSI_LOW_START_FAIL;
386 /* start arbitration */
387 nsp_cr_write_1(bst, bsh, NSPR_ARBITS, ARBITS_EXEC);
390 SCSI_LOW_SETUP_PHASE(ti, PH_ARBSTART);
393 /* XXX: what a stupid chip! */
394 arbs = nsp_cr_read_1(bst, bsh, NSPR_ARBITS);
397 while ((arbs & (ARBITS_WIN | ARBITS_FAIL)) == 0 && wc -- > 0);
399 if ((arbs & ARBITS_WIN) == 0)
401 nsp_cr_write_1(bst, bsh, NSPR_ARBITS, ARBITS_CLR);
403 nsp_statics.arbit_conflict_2 ++;
404 #endif /* NSP_STATICS */
405 return SCSI_LOW_START_FAIL;
408 /* assert select line */
409 SCSI_LOW_SETUP_PHASE(ti, PH_SELSTART);
410 scsi_low_arbit_win(slp);
414 nsp_cr_write_1(bst, bsh, NSPR_DATA,
415 sc->sc_idbit | (1 << ti->ti_id));
416 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR,
417 SCBUSCR_SEL | SCBUSCR_BSY | sc->sc_busc);
419 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, SCBUSCR_SEL |
420 SCBUSCR_BSY | SCBUSCR_DOUT | sc->sc_busc);
421 nsp_cr_write_1(bst, bsh, NSPR_ARBITS, ARBITS_CLR);
423 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR,
424 SCBUSCR_SEL | SCBUSCR_DOUT | sc->sc_busc);
427 if ((nsp_io_control & NSP_WAIT_FOR_SELECT) != 0)
429 #define NSP_FIRST_SEL_WAIT 300
430 #define NSP_SEL_CHECK_INTERVAL 10
432 /* wait for a selection response */
433 for (wc = 0; wc < NSP_FIRST_SEL_WAIT / NSP_SEL_CHECK_INTERVAL;
436 ph = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
437 if ((ph & SCBUSMON_BSY) == 0)
439 SCSI_LOW_DELAY(NSP_SEL_CHECK_INTERVAL);
444 ph = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
445 if ((ph & SCBUSMON_BSY) != 0)
447 nsphw_selection_done_and_expect_msgout(sc);
450 SCSI_LOW_SETUP_PHASE(ti, PH_SELECTED);
451 return SCSI_LOW_START_OK;
457 /* check a selection timeout */
458 nsp_start_timer(sc, NSP_TIMER_1MS);
460 return SCSI_LOW_START_OK;
464 nsp_world_start(sc, fdone)
465 struct nsp_softc *sc;
468 struct scsi_low_softc *slp = &sc->sc_sclow;
473 if ((slp->sl_cfgflags & CFG_NOATTEN) == 0)
474 sc->sc_busc = SCBUSCR_ATN;
478 if ((slp->sl_cfgflags & CFG_NOPARITY) == 0)
479 sc->sc_parr = PARITYR_ENABLE | PARITYR_CLEAR;
483 sc->sc_icr = (SCIENR_SCCHG | SCIENR_RESEL | SCIENR_RST);
486 scsi_low_bus_reset(slp);
488 SOFT_INTR_REQUIRED(slp);
492 struct ncp_synch_data {
499 static struct ncp_synch_data ncp_sync_data_40M[] = {
500 {0x0c,0x0c,0x1,0}, /* 20MB 50ns*/
501 {0x19,0x19,0x3,1}, /* 10MB 100ns*/
502 {0x1a,0x25,0x5,2}, /* 7.5MB 150ns*/
503 {0x26,0x32,0x7,3}, /* 5MB 200ns*/
507 static struct ncp_synch_data ncp_sync_data_20M[] = {
508 {0x19,0x19,0x1,0}, /* 10MB 100ns*/
509 {0x1a,0x25,0x2,0}, /* 7.5MB 150ns*/
510 {0x26,0x32,0x3,1}, /* 5MB 200ns*/
516 struct nsp_softc *sc;
517 struct targ_info *ti;
520 bus_space_tag_t bst = sc->sc_iot;
521 bus_space_handle_t bsh = sc->sc_ioh;
522 struct ncp_synch_data *sdp;
523 struct nsp_targ_info *nti = (void *) ti;
524 u_int period, offset;
527 if ((msg & SCSI_LOW_MSG_WIDE) != 0)
529 if (ti->ti_width != SCSI_LOW_BUS_WIDTH_8)
531 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
537 if ((msg & SCSI_LOW_MSG_SYNCH) == 0)
540 period = ti->ti_maxsynch.period;
541 offset = ti->ti_maxsynch.offset;
542 if (sc->sc_iclkdiv == CLKDIVR_20M)
543 sdp = &ncp_sync_data_20M[0];
545 sdp = &ncp_sync_data_40M[0];
547 for (i = 0; sdp->max_period != 0; i ++, sdp ++)
549 if (period >= sdp->min_period && period <= sdp->max_period)
553 if (period != 0 && sdp->max_period == 0)
556 * NO proper period/offset found,
557 * Retry neg with the target.
559 ti->ti_maxsynch.period = 0;
560 ti->ti_maxsynch.offset = 0;
561 nti->nti_reg_syncr = 0;
562 nti->nti_reg_ackwidth = 0;
567 nti->nti_reg_syncr = (sdp->chip_period << SYNCR_PERS) |
568 (offset & SYNCR_OFFM);
569 nti->nti_reg_ackwidth = sdp->ack_width;
573 nsp_cr_write_1(bst, bsh, NSPR_SYNCR, nti->nti_reg_syncr);
574 nsp_cr_write_1(bst, bsh, NSPR_ACKWIDTH, nti->nti_reg_ackwidth);
579 nsp_targ_init(sc, ti, action)
580 struct nsp_softc *sc;
581 struct targ_info *ti;
584 struct nsp_targ_info *nti = (void *) ti;
586 if (action == SCSI_LOW_INFO_ALLOC || action == SCSI_LOW_INFO_REVOKE)
588 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
589 ti->ti_maxsynch.period = 100 / 4;
590 ti->ti_maxsynch.offset = 15;
591 nti->nti_reg_syncr = 0;
592 nti->nti_reg_ackwidth = 0;
598 nsp_start_timer(sc, time)
599 struct nsp_softc *sc;
602 bus_space_tag_t bst = sc->sc_iot;
603 bus_space_handle_t bsh = sc->sc_ioh;
606 nsp_cr_write_1(bst, bsh, NSPR_TIMERCNT, time);
609 /**************************************************************
610 * General probe attach
611 **************************************************************/
613 nspprobesubr(iot, ioh, dvcfg)
615 bus_space_handle_t ioh;
620 regv = bus_space_read_1(iot, ioh, nsp_fifosr);
621 if (regv < 0x11 || regv >= 0x20)
633 printf("%s: scsibus ", name);
639 struct nsp_softc *sc;
641 struct scsi_low_softc *slp = &sc->sc_sclow;
645 sc->sc_idbit = (1 << slp->sl_hostid);
646 slp->sl_flags |= HW_READ_PADDING;
647 slp->sl_funcs = &nspfuncs;
648 sc->sc_tmaxcnt = SCSI_LOW_MIN_TOUT * 1000 * 1000; /* default */
650 (void) scsi_low_attach(slp, 0, NSP_NTARGETS, NSP_NLUNS,
651 sizeof(struct nsp_targ_info), 0);
654 /**************************************************************
656 **************************************************************/
659 struct nsp_softc *sc;
661 bus_space_tag_t bst = sc->sc_iot;
662 bus_space_handle_t bsh = sc->sc_ioh;
665 nsp_cr_write_1(bst, bsh, NSPR_PTCLRR, PTCLRR_RSS_ACK | PTCLRR_PT);
666 count = bus_space_read_1(bst, bsh, nsp_datar);
667 count += (((u_int) bus_space_read_1(bst, bsh, nsp_datar)) << 8);
668 count += (((u_int) bus_space_read_1(bst, bsh, nsp_datar)) << 16);
673 nsp_request_count(sc)
674 struct nsp_softc *sc;
676 bus_space_tag_t bst = sc->sc_iot;
677 bus_space_handle_t bsh = sc->sc_ioh;
680 nsp_cr_write_1(bst, bsh, NSPR_PTCLRR, PTCLRR_RSS_REQ | PTCLRR_PT);
681 count = bus_space_read_1(bst, bsh, nsp_datar);
682 count += (((u_int) bus_space_read_1(bst, bsh, nsp_datar)) << 8);
683 count += (((u_int) bus_space_read_1(bst, bsh, nsp_datar)) << 16);
688 nsp_setup_fifo(sc, on, direction, datalen)
689 struct nsp_softc *sc;
696 sc->sc_suspendio = 0;
697 if (on == NSP_FIFO_OFF)
699 xfermode = XFERMR_IO8;
703 /* check if suspend io OK ? */
706 if (direction == SCSI_LOW_READ)
708 if ((nsp_io_control & NSP_READ_SUSPEND_IO) != 0 &&
709 (datalen % nsp_read_suspend_bytes) == 0)
710 sc->sc_suspendio = nsp_read_suspend_bytes;
714 if ((nsp_io_control & NSP_WRITE_SUSPEND_IO) != 0 &&
715 (datalen % nsp_write_suspend_bytes) == 0)
716 sc->sc_suspendio = nsp_write_suspend_bytes;
720 /* determine a transfer type */
721 if (datalen < DEV_BSIZE || (datalen & 3) != 0)
723 if (sc->sc_memh != 0 &&
724 (nsp_io_control & NSP_USE_MEMIO) != 0)
725 xfermode = XFERMR_XEN | XFERMR_MEM8;
727 xfermode = XFERMR_XEN | XFERMR_IO8;
731 if (sc->sc_memh != 0 &&
732 (nsp_io_control & NSP_USE_MEMIO) != 0)
733 xfermode = XFERMR_XEN | XFERMR_MEM32;
735 xfermode = XFERMR_XEN | XFERMR_IO32;
737 if (sc->sc_suspendio > 0)
738 xfermode |= XFERMR_FIFOEN;
742 sc->sc_xfermr = xfermode;
743 nsp_cr_write_1(sc->sc_iot, sc->sc_ioh, NSPR_XFERMR, sc->sc_xfermr);
748 struct nsp_softc *sc;
749 struct targ_info *ti;
751 struct scsi_low_softc *slp = &sc->sc_sclow;
752 struct slccb *cb = slp->sl_Qnexus;
755 sc->sc_dataout_timeout = 0;
756 slp->sl_flags &= ~HW_PDMASTART;
757 nsp_setup_fifo(sc, NSP_FIFO_OFF, SCSI_LOW_READ, 0);
758 if ((sc->sc_icr & SCIENR_FIFO) != 0)
760 sc->sc_icr &= ~SCIENR_FIFO;
761 nsp_cr_write_1(sc->sc_iot, sc->sc_ioh, NSPR_SCIENR, sc->sc_icr);
766 slp->sl_error |= PDMAERR;
770 if (ti->ti_phase == PH_DATA)
772 cnt = nsp_fifo_count(sc);
773 if (slp->sl_scp.scp_direction == SCSI_LOW_WRITE)
775 len = sc->sc_cnt - cnt;
776 if (sc->sc_cnt >= cnt &&
777 slp->sl_scp.scp_datalen + len <=
778 cb->ccb_scp.scp_datalen)
780 slp->sl_scp.scp_data -= len;
781 slp->sl_scp.scp_datalen += len;
785 slp->sl_error |= PDMAERR;
786 printf("%s len %x >= datalen %x\n",
788 len, slp->sl_scp.scp_datalen);
791 else if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
793 if (sc->sc_cnt != cnt ||
794 sc->sc_cnt > cb->ccb_scp.scp_datalen)
796 slp->sl_error |= PDMAERR;
797 printf("%s: data read count error %x != %x (%x)\n",
798 slp->sl_xname, sc->sc_cnt, cnt,
799 cb->ccb_scp.scp_datalen);
803 scsi_low_data_finish(slp);
808 printf("%s data phase miss\n", slp->sl_xname);
809 slp->sl_error |= PDMAERR;
813 #define RFIFO_CRIT 64
814 #define WFIFO_CRIT 32
817 nsp_data_padding(sc, direction, count)
818 struct nsp_softc *sc;
822 bus_space_tag_t bst = sc->sc_iot;
823 bus_space_handle_t bsh = sc->sc_ioh;
825 if (count > NSP_MAX_DATA_SIZE)
826 count = NSP_MAX_DATA_SIZE;
828 nsp_cr_write_1(bst, bsh, NSPR_XFERMR, XFERMR_XEN | XFERMR_IO8);
829 if (direction == SCSI_LOW_READ)
832 (void) bus_space_read_1(bst, bsh, nsp_fifodr);
837 (void) bus_space_write_1(bst, bsh, nsp_fifodr, 0);
839 nsp_cr_write_1(bst, bsh, NSPR_XFERMR, sc->sc_xfermr);
843 nsp_read_fifo(sc, suspendio)
844 struct nsp_softc *sc;
847 struct scsi_low_softc *slp = &sc->sc_sclow;
848 bus_space_tag_t bst = sc->sc_iot;
849 bus_space_handle_t bsh = sc->sc_ioh;
852 res = nsp_fifo_count(sc);
853 if (res == sc->sc_cnt)
857 if (res < sc->sc_cnt || res == (u_int) -1)
859 printf("%s: strange fifo ack count 0x%x < 0x%x\n",
860 slp->sl_xname, res, sc->sc_cnt);
863 #endif /* NSP_DEBUG */
865 res = res - sc->sc_cnt;
866 if (res > slp->sl_scp.scp_datalen)
868 if ((slp->sl_error & PDMAERR) == 0)
870 printf("%s: data overrun 0x%x > 0x%x\n",
871 slp->sl_xname, res, slp->sl_scp.scp_datalen);
874 slp->sl_error |= PDMAERR;
875 slp->sl_scp.scp_datalen = 0;
877 if ((slp->sl_flags & HW_READ_PADDING) == 0)
879 printf("%s: read padding required\n", slp->sl_xname);
883 nsp_data_padding(sc, SCSI_LOW_READ, res);
885 return 1; /* padding start */
888 if (suspendio > 0 && slp->sl_scp.scp_datalen >= suspendio)
891 if ((sc->sc_xfermr & (XFERMR_MEM32 | XFERMR_MEM8)) != 0)
893 if ((sc->sc_xfermr & XFERMR_MEM32) != 0)
896 bus_space_read_region_4(sc->sc_memt, sc->sc_memh, 0,
897 (u_int32_t *) slp->sl_scp.scp_data, res >> 2);
901 bus_space_read_region_1(sc->sc_memt, sc->sc_memh, 0,
902 (u_int8_t *) slp->sl_scp.scp_data, res);
907 if ((sc->sc_xfermr & XFERMR_IO32) != 0)
910 bus_space_read_multi_4(bst, bsh, nsp_fifodr,
911 (u_int32_t *) slp->sl_scp.scp_data, res >> 2);
915 bus_space_read_multi_1(bst, bsh, nsp_fifodr,
916 (u_int8_t *) slp->sl_scp.scp_data, res);
920 if (nsp_cr_read_1(bst, bsh, NSPR_PARITYR) & PARITYR_PE)
922 nsp_cr_write_1(bst, bsh, NSPR_PARITYR,
923 PARITYR_ENABLE | PARITYR_CLEAR);
924 scsi_low_assert_msg(slp, slp->sl_Tnexus, SCSI_LOW_MSG_ERROR, 1);
927 slp->sl_scp.scp_data += res;
928 slp->sl_scp.scp_datalen -= res;
934 nsp_write_fifo(sc, suspendio)
935 struct nsp_softc *sc;
938 struct scsi_low_softc *slp = &sc->sc_sclow;
939 bus_space_tag_t bst = sc->sc_iot;
940 bus_space_handle_t bsh = sc->sc_ioh;
942 register u_int8_t stat;
947 if ((slp->sl_scp.scp_datalen % WFIFO_CRIT) != 0)
949 printf("%s: strange write length 0x%x\n",
950 slp->sl_xname, slp->sl_scp.scp_datalen);
952 #endif /* NSP_DEBUG */
953 res = slp->sl_scp.scp_datalen % suspendio;
964 if (res > slp->sl_scp.scp_datalen)
965 res = slp->sl_scp.scp_datalen;
967 /* XXX: reconfirm! */
968 stat = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON) & SCBUSMON_PHMASK;
969 if (stat != PHASE_DATAOUT)
972 if ((sc->sc_xfermr & (XFERMR_MEM32 | XFERMR_MEM8)) != 0)
974 if ((sc->sc_xfermr & XFERMR_MEM32) != 0)
976 bus_space_write_region_4(sc->sc_memt, sc->sc_memh, 0,
977 (u_int32_t *) slp->sl_scp.scp_data, res >> 2);
981 bus_space_write_region_1(sc->sc_memt, sc->sc_memh, 0,
982 (u_int8_t *) slp->sl_scp.scp_data, res);
987 if ((sc->sc_xfermr & XFERMR_IO32) != 0)
989 bus_space_write_multi_4(bst, bsh, nsp_fifodr,
990 (u_int32_t *) slp->sl_scp.scp_data, res >> 2);
994 bus_space_write_multi_1(bst, bsh, nsp_fifodr,
995 (u_int8_t *) slp->sl_scp.scp_data, res);
999 slp->sl_scp.scp_datalen -= res;
1000 slp->sl_scp.scp_data += res;
1006 nsp_wait_interrupt(sc)
1007 struct nsp_softc *sc;
1009 bus_space_tag_t bst = sc->sc_iot;
1010 bus_space_handle_t bsh = sc->sc_ioh;
1012 register u_int8_t isrc;
1014 for (tout = 0; tout < DEV_BSIZE / 10; tout ++)
1016 isrc = bus_space_read_1(bst, bsh, nsp_irqsr);
1017 if ((isrc & (IRQSR_SCSI | IRQSR_FIFO)) != 0)
1019 if ((isrc & IRQSR_FIFO) != 0)
1021 bus_space_write_1(bst, bsh,
1022 nsp_irqcr, IRQCR_FIFOCL);
1032 nsp_pio_read(sc, suspendio)
1033 struct nsp_softc *sc;
1036 struct scsi_low_softc *slp = &sc->sc_sclow;
1037 bus_space_tag_t bst = sc->sc_iot;
1038 bus_space_handle_t bsh = sc->sc_ioh;
1039 int tout, padding, datalen;
1040 register u_int8_t stat, fstat;
1043 tout = sc->sc_tmaxcnt;
1044 slp->sl_flags |= HW_PDMASTART;
1045 datalen = slp->sl_scp.scp_datalen;
1050 stat = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
1051 if (stat == (u_int8_t) -1)
1054 /* out of data phase */
1055 if ((stat & SCBUSMON_PHMASK) != PHASE_DATAIN)
1057 nsp_read_fifo(sc, 0);
1062 fstat = bus_space_read_1(bst, bsh, nsp_fifosr);
1063 if ((fstat & FIFOSR_FULLEMP) != 0)
1065 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1067 bus_space_write_1(bst, bsh, nsp_irqcr,
1073 padding |= nsp_read_fifo(sc, suspendio);
1077 padding |= nsp_read_fifo(sc, 0);
1080 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1085 if (padding == 0 && slp->sl_scp.scp_datalen <= 0)
1088 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1096 printf("%s: nsp_pio_read: timeout\n", slp->sl_xname);
1102 if (slp->sl_scp.scp_datalen > 0 &&
1103 slp->sl_scp.scp_datalen > datalen - nsp_read_interrupt_bytes)
1105 if (nsp_wait_interrupt(sc) != 0)
1111 nsp_pio_write(sc, suspendio)
1112 struct nsp_softc *sc;
1115 struct scsi_low_softc *slp = &sc->sc_sclow;
1116 bus_space_tag_t bst = sc->sc_iot;
1117 bus_space_handle_t bsh = sc->sc_ioh;
1118 u_int rcount, acount;
1120 register u_int8_t stat, fstat;
1122 tout = sc->sc_tmaxcnt;
1123 slp->sl_flags |= HW_PDMASTART;
1124 datalen = slp->sl_scp.scp_datalen;
1129 stat = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON) & SCBUSMON_PHMASK;
1130 if (stat != PHASE_DATAOUT)
1133 if (slp->sl_scp.scp_datalen <= 0)
1135 if (sc->sc_dataout_timeout == 0)
1136 sc->sc_dataout_timeout = SCSI_LOW_TIMEOUT_HZ;
1140 fstat = bus_space_read_1(bst, bsh, nsp_fifosr);
1141 if ((fstat & FIFOSR_FULLEMP) != 0)
1143 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1145 bus_space_write_1(bst, bsh, nsp_irqcr,
1152 * To avoid timeout of pcmcia bus
1153 * (not scsi bus!), we should check
1154 * the scsi device sends us request
1155 * signals, which means the scsi device
1156 * is ready to recieve data without
1159 if ((slp->sl_scp.scp_datalen % suspendio) == 0)
1162 * fill the nsp fifo, and waiting for
1165 nsp_write_fifo(sc, 0);
1170 * check the request singals.
1172 acount = nsp_fifo_count(sc);
1173 rcount = nsp_request_count(sc);
1174 if (rcount <= acount)
1176 nsp_write_fifo(sc, 0);
1178 nsp_statics.device_busy ++;
1179 #endif /* NSP_STATICS */
1183 nsp_write_fifo(sc, suspendio);
1185 nsp_statics.device_data_write ++;
1186 #endif /* NSP_STATICS */
1192 nsp_write_fifo(sc, 0);
1195 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1200 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1208 printf("%s: nsp_pio_write: timeout\n", slp->sl_xname);
1213 if (slp->sl_scp.scp_datalen > 0 &&
1214 slp->sl_scp.scp_datalen > datalen - nsp_write_interrupt_bytes)
1216 if (nsp_wait_interrupt(sc) != 0)
1222 nsp_negate_signal(struct nsp_softc *sc, u_int8_t mask, u_char *s)
1224 struct scsi_low_softc *slp = &sc->sc_sclow;
1225 bus_space_tag_t bst = sc->sc_iot;
1226 bus_space_handle_t bsh = sc->sc_ioh;
1230 for (wc = 0; wc < NSP_DELAY_MAX / NSP_DELAY_INTERVAL; wc ++)
1232 regv = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
1233 if (regv == (u_int8_t) -1)
1235 if ((regv & mask) == 0)
1237 SCSI_LOW_DELAY(NSP_DELAY_INTERVAL);
1240 printf("%s: %s nsp_negate_signal timeout\n", slp->sl_xname, s);
1245 nsp_xfer(sc, buf, len, phase, clear_atn)
1246 struct nsp_softc *sc;
1252 bus_space_tag_t bst = sc->sc_iot;
1253 bus_space_handle_t bsh = sc->sc_ioh;
1256 for (ptr = 0; len > 0; len --, ptr ++)
1258 rv = nsp_expect_signal(sc, phase, SCBUSMON_REQ);
1262 if (len == 1 && clear_atn != 0)
1264 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR,
1265 SCBUSCR_ADIR | SCBUSCR_ACKEN);
1266 SCSI_LOW_DEASSERT_ATN(&sc->sc_sclow);
1269 if (phase & SCBUSMON_IO)
1271 buf[ptr] = nsp_cr_read_1(bst, bsh, NSPR_DATAACK);
1275 nsp_cr_write_1(bst, bsh, NSPR_DATAACK, buf[ptr]);
1277 nsp_negate_signal(sc, SCBUSMON_ACK, "xfer<ACK>");
1284 /**************************************************************
1285 * disconnect & reselect (HW low)
1286 **************************************************************/
1289 struct nsp_softc *sc;
1291 struct scsi_low_softc *slp = &sc->sc_sclow;
1292 bus_space_tag_t bst = sc->sc_iot;
1293 bus_space_handle_t bsh = sc->sc_ioh;
1294 struct targ_info *ti;
1298 sid = (u_int) nsp_cr_read_1(bst, bsh, NSPR_RESELR);
1299 sid &= ~sc->sc_idbit;
1301 if ((ti = scsi_low_reselected(slp, sid)) == NULL)
1304 nsp_negate_signal(sc, SCBUSMON_SEL, "reselect<SEL>");
1306 cr = nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR);
1307 cr &= ~(SCBUSCR_BSY | SCBUSCR_ATN);
1308 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1309 cr |= SCBUSCR_ADIR | SCBUSCR_ACKEN;
1310 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1313 nsp_statics.reselect ++;
1314 #endif /* NSP_STATCIS */
1319 nsp_disconnected(sc, ti)
1320 struct nsp_softc *sc;
1321 struct targ_info *ti;
1323 struct scsi_low_softc *slp = &sc->sc_sclow;
1324 bus_space_tag_t bst = sc->sc_iot;
1325 bus_space_handle_t bsh = sc->sc_ioh;
1327 nsp_cr_write_1(bst, bsh, NSPR_PTCLRR, PTCLRR_PT | PTCLRR_ACK |
1328 PTCLRR_REQ | PTCLRR_HOST);
1329 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1331 sc->sc_icr &= ~SCIENR_FIFO;
1332 nsp_cr_write_1(bst, bsh, NSPR_SCIENR, sc->sc_icr);
1335 sc->sc_dataout_timeout = 0;
1337 nsp_statics.disconnect ++;
1338 #endif /* NSP_STATICS */
1339 scsi_low_disconnected(slp, ti);
1343 /**************************************************************
1345 **************************************************************/
1346 static void nsp_error(struct nsp_softc *, u_char *, u_int8_t, u_int8_t, u_int8_t);
1349 nsp_error(struct nsp_softc * sc, u_char *s, u_int8_t isrc, u_int8_t ph,
1352 struct scsi_low_softc *slp = &sc->sc_sclow;
1354 printf("%s: %s\n", slp->sl_xname, s);
1355 printf("%s: isrc 0x%x scmon 0x%x irqphs 0x%x\n",
1356 slp->sl_xname, (u_int) isrc, (u_int) ph, (u_int) irqphs);
1360 nsp_target_nexus_establish(sc)
1361 struct nsp_softc *sc;
1363 struct scsi_low_softc *slp = &sc->sc_sclow;
1364 bus_space_tag_t bst = sc->sc_iot;
1365 bus_space_handle_t bsh = sc->sc_ioh;
1366 struct targ_info *ti = slp->sl_Tnexus;
1367 struct nsp_targ_info *nti = (void *) ti;
1369 /* setup synch transfer registers */
1370 nsp_cr_write_1(bst, bsh, NSPR_SYNCR, nti->nti_reg_syncr);
1371 nsp_cr_write_1(bst, bsh, NSPR_ACKWIDTH, nti->nti_reg_ackwidth);
1373 /* setup pdma fifo (minimum) */
1374 nsp_setup_fifo(sc, NSP_FIFO_ON, SCSI_LOW_READ, 0);
1379 nsp_lun_nexus_establish(sc)
1380 struct nsp_softc *sc;
1387 nsp_ccb_nexus_establish(sc)
1388 struct nsp_softc *sc;
1390 struct scsi_low_softc *slp = &sc->sc_sclow;
1391 struct slccb *cb = slp->sl_Qnexus;
1393 sc->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
1395 /* setup pdma fifo */
1396 nsp_setup_fifo(sc, NSP_FIFO_ON,
1397 slp->sl_scp.scp_direction, slp->sl_scp.scp_datalen);
1399 if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
1401 if (sc->sc_suspendio > 0 &&
1402 (nsp_io_control & NSP_READ_FIFO_INTERRUPTS) != 0)
1404 sc->sc_icr |= SCIENR_FIFO;
1405 nsp_cr_write_1(sc->sc_iot, sc->sc_ioh,
1406 NSPR_SCIENR, sc->sc_icr);
1411 if (sc->sc_suspendio > 0 &&
1412 (nsp_io_control & NSP_WRITE_FIFO_INTERRUPTS) != 0)
1414 sc->sc_icr |= SCIENR_FIFO;
1415 nsp_cr_write_1(sc->sc_iot, sc->sc_ioh,
1416 NSPR_SCIENR, sc->sc_icr);
1423 nsp_phase_match(struct nsp_softc *sc, u_int8_t phase, u_int8_t stat)
1425 struct scsi_low_softc *slp = &sc->sc_sclow;
1427 if ((stat & SCBUSMON_PHMASK) != phase)
1429 printf("%s: phase mismatch 0x%x != 0x%x\n",
1430 slp->sl_xname, (u_int) phase, (u_int) stat);
1434 if ((stat & SCBUSMON_REQ) == 0)
1444 struct nsp_softc *sc = arg;
1445 struct scsi_low_softc *slp = &sc->sc_sclow;
1446 bus_space_tag_t bst = sc->sc_iot;
1447 bus_space_handle_t bsh = sc->sc_ioh;
1448 struct targ_info *ti;
1449 struct physio_proc *pp;
1451 u_int derror, flags;
1453 u_int8_t isrc, ph, irqphs, cr, regv;
1455 /*******************************************
1457 *******************************************/
1458 if (slp->sl_flags & HW_INACTIVE)
1461 bus_space_write_1(bst, bsh, nsp_irqcr, IRQCR_IRQDIS);
1462 isrc = bus_space_read_1(bst, bsh, nsp_irqsr);
1463 if (isrc == (u_int8_t) -1 || (isrc & IRQSR_MASK) == 0)
1465 bus_space_write_1(bst, bsh, nsp_irqcr, 0);
1470 * Do not read an irqphs register if no scsi phase interrupt.
1471 * Unless, you should lose a scsi phase interrupt.
1473 ph = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
1474 if ((isrc & IRQSR_SCSI) != 0)
1476 irqphs = nsp_cr_read_1(bst, bsh, NSPR_IRQPHS);
1482 * timer interrupt handler (scsi vs timer interrupts)
1484 if (sc->sc_timer != 0)
1486 nsp_cr_write_1(bst, bsh, NSPR_TIMERCNT, 0);
1487 nsp_cr_write_1(bst, bsh, NSPR_TIMERCNT, 0);
1491 /* check a timer interrupt */
1493 if ((isrc & IRQSR_TIMER) != 0)
1495 if ((isrc & IRQSR_MASK) == IRQSR_TIMER && sc->sc_seltout == 0)
1497 bus_space_write_1(bst, bsh, nsp_irqcr, IRQCR_TIMERCL);
1500 regv |= IRQCR_TIMERCL;
1503 /* check a fifo interrupt */
1504 if ((isrc & IRQSR_FIFO) != 0)
1506 regv |= IRQCR_FIFOCL;
1509 /* OK. enable all interrupts */
1510 bus_space_write_1(bst, bsh, nsp_irqcr, regv);
1512 /*******************************************
1514 *******************************************/
1518 nsp_error(sc, "current status", isrc, ph, irqphs);
1519 scsi_low_print(slp, NULL);
1522 SCSI_LOW_DEBUGGER("nsp");
1525 #endif /* NSP_DEBUG */
1527 /*******************************************
1528 * Parse hardware SCSI irq reasons register
1529 *******************************************/
1530 if ((isrc & IRQSR_SCSI) != 0)
1532 if ((irqphs & IRQPHS_RST) != 0)
1534 scsi_low_restart(slp, SCSI_LOW_RESTART_SOFT,
1535 "bus reset (power off?)");
1539 if ((irqphs & IRQPHS_RSEL) != 0)
1541 bus_space_write_1(bst, bsh, nsp_irqcr, IRQCR_RESCL);
1542 if (nsp_reselected(sc) == EJUSTRETURN)
1546 if ((irqphs & (IRQPHS_PCHG | IRQPHS_LBF)) == 0)
1550 /*******************************************
1552 *******************************************/
1553 if ((ti = slp->sl_Tnexus) == NULL)
1555 /* unknown scsi phase changes */
1556 nsp_error(sc, "unknown scsi phase changes", isrc, ph, irqphs);
1560 /*******************************************
1561 * aribitration & selection
1562 *******************************************/
1563 switch (ti->ti_phase)
1566 if ((ph & SCBUSMON_BSY) == 0)
1568 if (sc->sc_seltout >= NSP_SELTIMEOUT)
1571 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, 0);
1572 return nsp_disconnected(sc, ti);
1575 nsp_start_timer(sc, NSP_TIMER_1MS);
1579 SCSI_LOW_SETUP_PHASE(ti, PH_SELECTED);
1580 nsphw_selection_done_and_expect_msgout(sc);
1584 if ((isrc & IRQSR_SCSI) == 0)
1587 nsp_target_nexus_establish(sc);
1591 if ((isrc & IRQSR_SCSI) == 0)
1594 nsp_target_nexus_establish(sc);
1595 if ((ph & SCBUSMON_PHMASK) != PHASE_MSGIN)
1597 printf("%s: unexpected phase after reselect\n",
1599 slp->sl_error |= FATALIO;
1600 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 1);
1606 if ((isrc & IRQSR_SCSI) != 0)
1608 if ((isrc & IRQSR_FIFO) != 0)
1610 if (NSP_IS_PHASE_DATA(ph) == 0)
1612 irqphs = (ph & IRQPHS_PHMASK);
1618 if ((isrc & IRQSR_SCSI) == 0)
1623 /*******************************************
1624 * data phase control
1625 *******************************************/
1626 if (slp->sl_flags & HW_PDMASTART)
1628 if ((isrc & IRQSR_SCSI) != 0 &&
1629 NSP_IS_IRQPHS_DATA(irqphs) == 0)
1631 if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
1632 nsp_pio_read(sc, 0);
1633 nsp_pdma_end(sc, ti);
1637 /*******************************************
1639 *******************************************/
1640 if (slp->sl_msgphase != 0 && (irqphs & IRQPHS_LBF) != 0)
1641 return nsp_disconnected(sc, ti);
1643 /* check unexpected bus free state */
1646 nsp_error(sc, "unexpected bus free", isrc, ph, irqphs);
1647 return nsp_disconnected(sc, ti);
1650 /* check normal scsi phase */
1651 switch (irqphs & IRQPHS_PHMASK)
1654 if (nsp_phase_match(sc, PHASE_CMD, ph) != 0)
1657 SCSI_LOW_SETUP_PHASE(ti, PH_CMD);
1658 if (scsi_low_cmd(slp, ti) != 0)
1660 scsi_low_attention(slp);
1663 nsp_cr_write_1(bst, bsh, NSPR_CMDCR, CMDCR_PTCLR);
1664 for (len = 0; len < slp->sl_scp.scp_cmdlen; len ++)
1665 nsp_cr_write_1(bst, bsh, NSPR_CMDDR,
1666 slp->sl_scp.scp_cmd[len]);
1668 nsp_cr_write_1(bst, bsh, NSPR_CMDCR, CMDCR_PTCLR | CMDCR_EXEC);
1671 case IRQPHS_DATAOUT:
1672 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1673 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_WRITE) != 0)
1675 scsi_low_attention(slp);
1678 pp = physio_proc_enter(bp);
1679 nsp_pio_write(sc, sc->sc_suspendio);
1680 physio_proc_leave(pp);
1684 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1685 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_READ) != 0)
1687 scsi_low_attention(slp);
1690 pp = physio_proc_enter(bp);
1691 nsp_pio_read(sc, sc->sc_suspendio);
1692 physio_proc_leave(pp);
1696 if (nsp_phase_match(sc, PHASE_STATUS, ph) != 0)
1699 SCSI_LOW_SETUP_PHASE(ti, PH_STAT);
1700 regv = nsp_cr_read_1(bst, bsh, NSPR_DATA);
1701 if (nsp_cr_read_1(bst, bsh, NSPR_PARITYR) & PARITYR_PE)
1703 nsp_cr_write_1(bst, bsh, NSPR_PARITYR,
1704 PARITYR_ENABLE | PARITYR_CLEAR);
1705 derror = SCSI_LOW_DATA_PE;
1711 cr = SCBUSCR_ACK | nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR);
1712 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1714 if (scsi_low_statusin(slp, ti, derror | regv) != 0)
1716 scsi_low_attention(slp);
1719 /* check REQ nagated */
1720 nsp_negate_signal(sc, SCBUSMON_REQ, "statin<REQ>");
1723 cr = nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR) & (~SCBUSCR_ACK);
1724 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1728 if (nsp_phase_match(sc, PHASE_MSGOUT, ph) != 0)
1731 #ifdef NSP_MSGOUT_SERIALIZE
1734 * NSP invoke interrupts only in the case of scsi phase changes,
1735 * therefore we should poll the scsi phase here to catch
1736 * the next "msg out" if exists (no scsi phase changes).
1740 SCSI_LOW_SETUP_PHASE(ti, PH_MSGOUT);
1741 flags = (ti->ti_ophase != ti->ti_phase) ?
1742 SCSI_LOW_MSGOUT_INIT : 0;
1743 len = scsi_low_msgout(slp, ti, flags);
1745 if (len > 1 && slp->sl_atten == 0)
1747 scsi_low_attention(slp);
1750 if (nsp_xfer(sc, ti->ti_msgoutstr, len, PHASE_MSGOUT,
1751 slp->sl_clear_atten) != 0)
1753 slp->sl_error |= FATALIO;
1754 nsp_error(sc, "MSGOUT: xfer short",
1758 /* catch a next signal */
1759 rv = nsp_expect_signal(sc, PHASE_MSGOUT, SCBUSMON_REQ);
1761 while (rv > 0 && len -- > 0);
1763 #else /* !NSP_MSGOUT_SERIALIZE */
1764 SCSI_LOW_SETUP_PHASE(ti, PH_MSGOUT);
1765 flags = SCSI_LOW_MSGOUT_UNIFY;
1766 if (ti->ti_ophase != ti->ti_phase)
1767 flags |= SCSI_LOW_MSGOUT_INIT;
1768 len = scsi_low_msgout(slp, ti, flags);
1770 if (len > 1 && slp->sl_atten == 0)
1772 scsi_low_attention(slp);
1775 if (nsp_xfer(sc, ti->ti_msgoutstr, len, PHASE_MSGOUT,
1776 slp->sl_clear_atten) != 0)
1778 nsp_error(sc, "MSGOUT: xfer short", isrc, ph, irqphs);
1781 #endif /* !NSP_MSGOUT_SERIALIZE */
1785 if (nsp_phase_match(sc, PHASE_MSGIN, ph) != 0)
1790 * NSP invoke interrupts only in the case of scsi phase changes,
1791 * therefore we should poll the scsi phase here to catch
1792 * the next "msg in" if exists (no scsi phase changes).
1796 SCSI_LOW_SETUP_PHASE(ti, PH_MSGIN);
1799 regv = nsp_cr_read_1(bst, bsh, NSPR_DATA);
1800 if (nsp_cr_read_1(bst, bsh, NSPR_PARITYR) & PARITYR_PE)
1802 nsp_cr_write_1(bst, bsh,
1804 PARITYR_ENABLE | PARITYR_CLEAR);
1805 derror = SCSI_LOW_DATA_PE;
1813 cr = nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR) | SCBUSCR_ACK;
1814 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1816 if (scsi_low_msgin(slp, ti, regv | derror) == 0)
1818 if (scsi_low_is_msgout_continue(ti, 0) != 0)
1820 scsi_low_attention(slp);
1824 /* check REQ nagated */
1825 nsp_negate_signal(sc, SCBUSMON_REQ, "msgin<REQ>");
1828 cr = nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR) & (~SCBUSCR_ACK);
1829 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1831 /* catch a next signal */
1832 rv = nsp_expect_signal(sc, PHASE_MSGIN, SCBUSMON_REQ);
1834 while (rv > 0 && len -- > 0);
1838 slp->sl_error |= FATALIO;
1839 nsp_error(sc, "unknown scsi phase", isrc, ph, irqphs);
1847 nsp_start_timer(sc, NSP_TIMER_1MS);
1854 struct nsp_softc *sc;
1856 struct scsi_low_softc *slp = &sc->sc_sclow;
1857 bus_space_tag_t iot = sc->sc_iot;
1858 bus_space_handle_t ioh = sc->sc_ioh;
1862 if (slp->sl_Tnexus == NULL)
1865 ph = nsp_cr_read_1(iot, ioh, NSPR_SCBUSMON);
1866 switch (ph & SCBUSMON_PHMASK)
1869 if (sc->sc_dataout_timeout == 0)
1872 /* check a fifo empty */
1873 regv = bus_space_read_1(iot, ioh, nsp_fifosr);
1874 if ((regv & FIFOSR_FULLEMP) == 0)
1876 bus_space_write_1(iot, ioh, nsp_irqcr, IRQCR_FIFOCL);
1878 /* check still requested */
1879 ph = nsp_cr_read_1(iot, ioh, NSPR_SCBUSMON);
1880 if ((ph & SCBUSMON_REQ) == 0)
1883 if ((-- sc->sc_dataout_timeout) > 0)
1886 slp->sl_error |= PDMAERR;
1887 if ((slp->sl_flags & HW_WRITE_PADDING) == 0)
1889 printf("%s: write padding required\n", slp->sl_xname);
1893 tout = NSP_DELAY_MAX;
1896 ph = nsp_cr_read_1(iot, ioh, NSPR_SCBUSMON);
1897 if ((ph & SCBUSMON_PHMASK) != PHASE_DATAOUT)
1899 regv = bus_space_read_1(iot, ioh, nsp_fifosr);
1900 if ((regv & FIFOSR_FULLEMP) == 0)
1906 bus_space_write_1(iot, ioh, nsp_irqcr, IRQCR_FIFOCL);
1907 nsp_data_padding(sc, SCSI_LOW_WRITE, 32);
1909 ph = nsp_cr_read_1(iot, ioh, NSPR_SCBUSMON);
1910 if ((ph & SCBUSMON_PHMASK) == PHASE_DATAOUT)
1911 sc->sc_dataout_timeout = SCSI_LOW_TIMEOUT_HZ;