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/clock.h>
76 #include <machine/cpu.h>
77 #include <machine/bus.h>
79 #include <compat/netbsd/dvcfg.h>
80 #include <compat/netbsd/physio_proc.h>
82 #include <cam/scsi/scsi_low.h>
83 #include <dev/nsp/nspreg.h>
84 #include <dev/nsp/nspvar.h>
85 #endif /* __FreeBSD__ */
87 /***************************************************
89 ***************************************************/
90 /* DEVICE CONFIGURATION FLAGS (MINOR)
93 * 0x02 PARITY LINE OFF
94 * 0x04 IDENTIFY MSG OFF ( = single lun)
95 * 0x08 SYNC TRANSFER OFF
98 /***************************************************
100 ***************************************************/
101 #define NSP_NTARGETS 8
104 #define NSP_MAX_DATA_SIZE (64 * 1024)
105 #define NSP_SELTIMEOUT (200)
106 #define NSP_DELAY_MAX (2 * 1000 * 1000)
107 #define NSP_DELAY_INTERVAL (1)
108 #define NSP_TIMER_1MS (1000 / 51)
110 /***************************************************
112 ***************************************************/
115 #endif /* NSP_DEBUG */
119 int arbit_conflict_1;
120 int arbit_conflict_2;
121 int device_data_write;
125 int data_phase_bypass;
127 #endif /* NSP_STATICS */
129 /***************************************************
131 ***************************************************/
132 #define NSP_READ_SUSPEND_IO 0x0001
133 #define NSP_WRITE_SUSPEND_IO 0x0002
134 #define NSP_USE_MEMIO 0x0004
135 #define NSP_READ_FIFO_INTERRUPTS 0x0010
136 #define NSP_WRITE_FIFO_INTERRUPTS 0x0020
137 #define NSP_WAIT_FOR_SELECT 0x0100
139 u_int nsp_io_control = NSP_IO_CONTROL_FLAGS;
140 int nsp_read_suspend_bytes = DEV_BSIZE;
141 int nsp_write_suspend_bytes = DEV_BSIZE;
142 int nsp_read_interrupt_bytes = 4096;
143 int nsp_write_interrupt_bytes = 4096;
145 /***************************************************
147 ***************************************************/
148 extern struct cfdriver nsp_cd;
150 /**************************************************************
152 **************************************************************/
153 #define NSP_FIFO_ON 1
154 #define NSP_FIFO_OFF 0
155 static void nsp_pio_read(struct nsp_softc *, int);
156 static void nsp_pio_write(struct nsp_softc *, int);
157 static int nsp_xfer(struct nsp_softc *, u_int8_t *, int, int, int);
158 static int nsp_msg(struct nsp_softc *, struct targ_info *, u_int);
159 static int nsp_reselected(struct nsp_softc *);
160 static int nsp_disconnected(struct nsp_softc *, struct targ_info *);
161 static void nsp_pdma_end(struct nsp_softc *, struct targ_info *);
162 static void nsphw_init(struct nsp_softc *);
163 static int nsp_target_nexus_establish(struct nsp_softc *);
164 static int nsp_lun_nexus_establish(struct nsp_softc *);
165 static int nsp_ccb_nexus_establish(struct nsp_softc *);
166 static int nsp_world_start(struct nsp_softc *, int);
167 static int nsphw_start_selection(struct nsp_softc *sc, struct slccb *);
168 static void nsphw_bus_reset(struct nsp_softc *);
169 static void nsphw_attention(struct nsp_softc *);
170 static u_int nsp_fifo_count(struct nsp_softc *);
171 static u_int nsp_request_count(struct nsp_softc *);
172 static int nsp_negate_signal(struct nsp_softc *, u_int8_t, u_char *);
173 static int nsp_expect_signal(struct nsp_softc *, u_int8_t, u_int8_t);
174 static void nsp_start_timer(struct nsp_softc *, int);
175 static void nsp_setup_fifo(struct nsp_softc *, int, int, int);
176 static int nsp_targ_init(struct nsp_softc *, struct targ_info *, int);
177 static void nsphw_selection_done_and_expect_msgout(struct nsp_softc *);
178 static void nsp_data_padding(struct nsp_softc *, int, u_int);
179 static int nsp_timeout(struct nsp_softc *);
180 static int nsp_read_fifo(struct nsp_softc *, int);
181 static int nsp_write_fifo(struct nsp_softc *, int);
182 static int nsp_phase_match(struct nsp_softc *, u_int8_t, u_int8_t);
183 static int nsp_wait_interrupt(struct nsp_softc *);
185 struct scsi_low_funcs nspfuncs = {
186 SC_LOW_INIT_T nsp_world_start,
187 SC_LOW_BUSRST_T nsphw_bus_reset,
188 SC_LOW_TARG_INIT_T nsp_targ_init,
189 SC_LOW_LUN_INIT_T NULL,
191 SC_LOW_SELECT_T nsphw_start_selection,
192 SC_LOW_NEXUS_T nsp_lun_nexus_establish,
193 SC_LOW_NEXUS_T nsp_ccb_nexus_establish,
195 SC_LOW_ATTEN_T nsphw_attention,
196 SC_LOW_MSG_T nsp_msg,
198 SC_LOW_TIMEOUT_T nsp_timeout,
199 SC_LOW_POLL_T nspintr,
204 /****************************************************
206 ****************************************************/
207 static __inline u_int8_t nsp_cr_read_1(bus_space_tag_t bst, bus_space_handle_t bsh, bus_addr_t ofs);
208 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);
210 static __inline u_int8_t
211 nsp_cr_read_1(bst, bsh, ofs)
213 bus_space_handle_t bsh;
217 bus_space_write_1(bst, bsh, nsp_idxr, ofs);
218 return bus_space_read_1(bst, bsh, nsp_datar);
222 nsp_cr_write_1(bst, bsh, ofs, va)
224 bus_space_handle_t bsh;
229 bus_space_write_1(bst, bsh, nsp_idxr, ofs);
230 bus_space_write_1(bst, bsh, nsp_datar, va);
234 nsp_expect_signal(sc, curphase, mask)
235 struct nsp_softc *sc;
236 u_int8_t curphase, mask;
238 struct scsi_low_softc *slp = &sc->sc_sclow;
239 bus_space_tag_t bst = sc->sc_iot;
240 bus_space_handle_t bsh = sc->sc_ioh;
244 for (wc = 0; wc < NSP_DELAY_MAX / NSP_DELAY_INTERVAL; wc ++)
246 ph = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
247 if (ph == (u_int8_t) -1)
250 isrc = bus_space_read_1(bst, bsh, nsp_irqsr);
251 if (isrc & IRQSR_SCSI)
254 if ((ph & mask) != 0 && (ph & SCBUSMON_PHMASK) == curphase)
257 SCSI_LOW_DELAY(NSP_DELAY_INTERVAL);
260 printf("%s: nsp_expect_signal timeout\n", slp->sl_xname);
266 struct nsp_softc *sc;
268 bus_space_tag_t bst = sc->sc_iot;
269 bus_space_handle_t bsh = sc->sc_ioh;
271 /* block all interrupts */
272 bus_space_write_1(bst, bsh, nsp_irqcr, IRQCR_ALLMASK);
274 /* setup SCSI interface */
275 bus_space_write_1(bst, bsh, nsp_ifselr, IFSELR_IFSEL);
277 nsp_cr_write_1(bst, bsh, NSPR_SCIENR, 0);
279 nsp_cr_write_1(bst, bsh, NSPR_XFERMR, XFERMR_IO8);
280 nsp_cr_write_1(bst, bsh, NSPR_CLKDIVR, sc->sc_iclkdiv);
282 nsp_cr_write_1(bst, bsh, NSPR_SCIENR, sc->sc_icr);
283 nsp_cr_write_1(bst, bsh, NSPR_PARITYR, sc->sc_parr);
284 nsp_cr_write_1(bst, bsh, NSPR_PTCLRR,
285 PTCLRR_ACK | PTCLRR_REQ | PTCLRR_HOST | PTCLRR_RSS);
287 /* setup fifo asic */
288 bus_space_write_1(bst, bsh, nsp_ifselr, IFSELR_REGSEL);
289 nsp_cr_write_1(bst, bsh, NSPR_TERMPWRC, 0);
290 if ((nsp_cr_read_1(bst, bsh, NSPR_OCR) & OCR_TERMPWRS) == 0)
291 nsp_cr_write_1(bst, bsh, NSPR_TERMPWRC, TERMPWRC_POWON);
293 nsp_cr_write_1(bst, bsh, NSPR_XFERMR, XFERMR_IO8);
294 nsp_cr_write_1(bst, bsh, NSPR_CLKDIVR, sc->sc_clkdiv);
295 nsp_cr_write_1(bst, bsh, NSPR_TIMERCNT, 0);
296 nsp_cr_write_1(bst, bsh, NSPR_TIMERCNT, 0);
298 nsp_cr_write_1(bst, bsh, NSPR_SYNCR, 0);
299 nsp_cr_write_1(bst, bsh, NSPR_ACKWIDTH, 0);
301 /* enable interrupts and ack them */
302 nsp_cr_write_1(bst, bsh, NSPR_SCIENR, sc->sc_icr);
303 bus_space_write_1(bst, bsh, nsp_irqcr, IRQSR_MASK);
305 nsp_setup_fifo(sc, NSP_FIFO_OFF, SCSI_LOW_READ, 0);
308 /****************************************************
310 ****************************************************/
313 struct nsp_softc *sc;
315 bus_space_tag_t bst = sc->sc_iot;
316 bus_space_handle_t bsh = sc->sc_ioh;
319 cr = nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR)/* & ~SCBUSCR_ACK */;
320 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr | SCBUSCR_ATN);
326 struct nsp_softc *sc;
328 bus_space_tag_t bst = sc->sc_iot;
329 bus_space_handle_t bsh = sc->sc_ioh;
332 bus_space_write_1(bst, bsh, nsp_irqcr, IRQCR_ALLMASK);
334 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, SCBUSCR_RST);
335 SCSI_LOW_DELAY(100 * 1000); /* 100ms */
336 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, 0);
337 for (i = 0; i < 5; i ++)
338 (void) nsp_cr_read_1(bst, bsh, NSPR_IRQPHS);
340 bus_space_write_1(bst, bsh, nsp_irqcr, IRQSR_MASK);
344 nsphw_selection_done_and_expect_msgout(sc)
345 struct nsp_softc *sc;
347 struct scsi_low_softc *slp = &sc->sc_sclow;
348 bus_space_tag_t bst = sc->sc_iot;
349 bus_space_handle_t bsh = sc->sc_ioh;
351 /* clear ack counter */
353 nsp_cr_write_1(bst, bsh, NSPR_PTCLRR, PTCLRR_PT | PTCLRR_ACK |
354 PTCLRR_REQ | PTCLRR_HOST);
356 /* deassert sel and assert atten */
358 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, sc->sc_busc);
360 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR,
361 sc->sc_busc | SCBUSCR_ADIR | SCBUSCR_ACKEN);
362 SCSI_LOW_ASSERT_ATN(slp);
366 nsphw_start_selection(sc, cb)
367 struct nsp_softc *sc;
370 struct scsi_low_softc *slp = &sc->sc_sclow;
371 bus_space_tag_t bst = sc->sc_iot;
372 bus_space_handle_t bsh = sc->sc_ioh;
373 struct targ_info *ti = cb->ti;
374 register u_int8_t arbs, ph;
377 wc = sc->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
378 sc->sc_dataout_timeout = 0;
382 ph = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
383 if (ph != SCBUSMON_FREE)
387 nsp_statics.arbit_conflict_1 ++;
388 #endif /* NSP_STATICS */
389 return SCSI_LOW_START_FAIL;
392 /* start arbitration */
393 nsp_cr_write_1(bst, bsh, NSPR_ARBITS, ARBITS_EXEC);
396 SCSI_LOW_SETUP_PHASE(ti, PH_ARBSTART);
399 /* XXX: what a stupid chip! */
400 arbs = nsp_cr_read_1(bst, bsh, NSPR_ARBITS);
403 while ((arbs & (ARBITS_WIN | ARBITS_FAIL)) == 0 && wc -- > 0);
405 if ((arbs & ARBITS_WIN) == 0)
407 nsp_cr_write_1(bst, bsh, NSPR_ARBITS, ARBITS_CLR);
409 nsp_statics.arbit_conflict_2 ++;
410 #endif /* NSP_STATICS */
411 return SCSI_LOW_START_FAIL;
414 /* assert select line */
415 SCSI_LOW_SETUP_PHASE(ti, PH_SELSTART);
416 scsi_low_arbit_win(slp);
420 nsp_cr_write_1(bst, bsh, NSPR_DATA,
421 sc->sc_idbit | (1 << ti->ti_id));
422 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR,
423 SCBUSCR_SEL | SCBUSCR_BSY | sc->sc_busc);
425 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, SCBUSCR_SEL |
426 SCBUSCR_BSY | SCBUSCR_DOUT | sc->sc_busc);
427 nsp_cr_write_1(bst, bsh, NSPR_ARBITS, ARBITS_CLR);
429 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR,
430 SCBUSCR_SEL | SCBUSCR_DOUT | sc->sc_busc);
433 if ((nsp_io_control & NSP_WAIT_FOR_SELECT) != 0)
435 #define NSP_FIRST_SEL_WAIT 300
436 #define NSP_SEL_CHECK_INTERVAL 10
438 /* wait for a selection response */
439 for (wc = 0; wc < NSP_FIRST_SEL_WAIT / NSP_SEL_CHECK_INTERVAL;
442 ph = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
443 if ((ph & SCBUSMON_BSY) == 0)
445 SCSI_LOW_DELAY(NSP_SEL_CHECK_INTERVAL);
450 ph = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
451 if ((ph & SCBUSMON_BSY) != 0)
453 nsphw_selection_done_and_expect_msgout(sc);
456 SCSI_LOW_SETUP_PHASE(ti, PH_SELECTED);
457 return SCSI_LOW_START_OK;
463 /* check a selection timeout */
464 nsp_start_timer(sc, NSP_TIMER_1MS);
466 return SCSI_LOW_START_OK;
470 nsp_world_start(sc, fdone)
471 struct nsp_softc *sc;
474 struct scsi_low_softc *slp = &sc->sc_sclow;
479 if ((slp->sl_cfgflags & CFG_NOATTEN) == 0)
480 sc->sc_busc = SCBUSCR_ATN;
484 if ((slp->sl_cfgflags & CFG_NOPARITY) == 0)
485 sc->sc_parr = PARITYR_ENABLE | PARITYR_CLEAR;
489 sc->sc_icr = (SCIENR_SCCHG | SCIENR_RESEL | SCIENR_RST);
492 scsi_low_bus_reset(slp);
494 SOFT_INTR_REQUIRED(slp);
498 struct ncp_synch_data {
505 static struct ncp_synch_data ncp_sync_data_40M[] = {
506 {0x0c,0x0c,0x1,0}, /* 20MB 50ns*/
507 {0x19,0x19,0x3,1}, /* 10MB 100ns*/
508 {0x1a,0x25,0x5,2}, /* 7.5MB 150ns*/
509 {0x26,0x32,0x7,3}, /* 5MB 200ns*/
513 static struct ncp_synch_data ncp_sync_data_20M[] = {
514 {0x19,0x19,0x1,0}, /* 10MB 100ns*/
515 {0x1a,0x25,0x2,0}, /* 7.5MB 150ns*/
516 {0x26,0x32,0x3,1}, /* 5MB 200ns*/
522 struct nsp_softc *sc;
523 struct targ_info *ti;
526 bus_space_tag_t bst = sc->sc_iot;
527 bus_space_handle_t bsh = sc->sc_ioh;
528 struct ncp_synch_data *sdp;
529 struct nsp_targ_info *nti = (void *) ti;
530 u_int period, offset;
533 if ((msg & SCSI_LOW_MSG_WIDE) != 0)
535 if (ti->ti_width != SCSI_LOW_BUS_WIDTH_8)
537 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
543 if ((msg & SCSI_LOW_MSG_SYNCH) == 0)
546 period = ti->ti_maxsynch.period;
547 offset = ti->ti_maxsynch.offset;
548 if (sc->sc_iclkdiv == CLKDIVR_20M)
549 sdp = &ncp_sync_data_20M[0];
551 sdp = &ncp_sync_data_40M[0];
553 for (i = 0; sdp->max_period != 0; i ++, sdp ++)
555 if (period >= sdp->min_period && period <= sdp->max_period)
559 if (period != 0 && sdp->max_period == 0)
562 * NO proper period/offset found,
563 * Retry neg with the target.
565 ti->ti_maxsynch.period = 0;
566 ti->ti_maxsynch.offset = 0;
567 nti->nti_reg_syncr = 0;
568 nti->nti_reg_ackwidth = 0;
573 nti->nti_reg_syncr = (sdp->chip_period << SYNCR_PERS) |
574 (offset & SYNCR_OFFM);
575 nti->nti_reg_ackwidth = sdp->ack_width;
579 nsp_cr_write_1(bst, bsh, NSPR_SYNCR, nti->nti_reg_syncr);
580 nsp_cr_write_1(bst, bsh, NSPR_ACKWIDTH, nti->nti_reg_ackwidth);
585 nsp_targ_init(sc, ti, action)
586 struct nsp_softc *sc;
587 struct targ_info *ti;
590 struct nsp_targ_info *nti = (void *) ti;
592 if (action == SCSI_LOW_INFO_ALLOC || action == SCSI_LOW_INFO_REVOKE)
594 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
595 ti->ti_maxsynch.period = 100 / 4;
596 ti->ti_maxsynch.offset = 15;
597 nti->nti_reg_syncr = 0;
598 nti->nti_reg_ackwidth = 0;
604 nsp_start_timer(sc, time)
605 struct nsp_softc *sc;
608 bus_space_tag_t bst = sc->sc_iot;
609 bus_space_handle_t bsh = sc->sc_ioh;
612 nsp_cr_write_1(bst, bsh, NSPR_TIMERCNT, time);
615 /**************************************************************
616 * General probe attach
617 **************************************************************/
619 nspprobesubr(iot, ioh, dvcfg)
621 bus_space_handle_t ioh;
626 regv = bus_space_read_1(iot, ioh, nsp_fifosr);
627 if (regv < 0x11 || regv >= 0x20)
639 printf("%s: scsibus ", name);
645 struct nsp_softc *sc;
647 struct scsi_low_softc *slp = &sc->sc_sclow;
651 sc->sc_idbit = (1 << slp->sl_hostid);
652 slp->sl_flags |= HW_READ_PADDING;
653 slp->sl_funcs = &nspfuncs;
654 sc->sc_tmaxcnt = SCSI_LOW_MIN_TOUT * 1000 * 1000; /* default */
656 (void) scsi_low_attach(slp, 0, NSP_NTARGETS, NSP_NLUNS,
657 sizeof(struct nsp_targ_info), 0);
660 /**************************************************************
662 **************************************************************/
665 struct nsp_softc *sc;
667 bus_space_tag_t bst = sc->sc_iot;
668 bus_space_handle_t bsh = sc->sc_ioh;
671 nsp_cr_write_1(bst, bsh, NSPR_PTCLRR, PTCLRR_RSS_ACK | PTCLRR_PT);
672 count = bus_space_read_1(bst, bsh, nsp_datar);
673 count += (((u_int) bus_space_read_1(bst, bsh, nsp_datar)) << 8);
674 count += (((u_int) bus_space_read_1(bst, bsh, nsp_datar)) << 16);
679 nsp_request_count(sc)
680 struct nsp_softc *sc;
682 bus_space_tag_t bst = sc->sc_iot;
683 bus_space_handle_t bsh = sc->sc_ioh;
686 nsp_cr_write_1(bst, bsh, NSPR_PTCLRR, PTCLRR_RSS_REQ | PTCLRR_PT);
687 count = bus_space_read_1(bst, bsh, nsp_datar);
688 count += (((u_int) bus_space_read_1(bst, bsh, nsp_datar)) << 8);
689 count += (((u_int) bus_space_read_1(bst, bsh, nsp_datar)) << 16);
694 nsp_setup_fifo(sc, on, direction, datalen)
695 struct nsp_softc *sc;
702 sc->sc_suspendio = 0;
703 if (on == NSP_FIFO_OFF)
705 xfermode = XFERMR_IO8;
709 /* check if suspend io OK ? */
712 if (direction == SCSI_LOW_READ)
714 if ((nsp_io_control & NSP_READ_SUSPEND_IO) != 0 &&
715 (datalen % nsp_read_suspend_bytes) == 0)
716 sc->sc_suspendio = nsp_read_suspend_bytes;
720 if ((nsp_io_control & NSP_WRITE_SUSPEND_IO) != 0 &&
721 (datalen % nsp_write_suspend_bytes) == 0)
722 sc->sc_suspendio = nsp_write_suspend_bytes;
726 /* determine a transfer type */
727 if (datalen < DEV_BSIZE || (datalen & 3) != 0)
729 if (sc->sc_memh != 0 &&
730 (nsp_io_control & NSP_USE_MEMIO) != 0)
731 xfermode = XFERMR_XEN | XFERMR_MEM8;
733 xfermode = XFERMR_XEN | XFERMR_IO8;
737 if (sc->sc_memh != 0 &&
738 (nsp_io_control & NSP_USE_MEMIO) != 0)
739 xfermode = XFERMR_XEN | XFERMR_MEM32;
741 xfermode = XFERMR_XEN | XFERMR_IO32;
743 if (sc->sc_suspendio > 0)
744 xfermode |= XFERMR_FIFOEN;
748 sc->sc_xfermr = xfermode;
749 nsp_cr_write_1(sc->sc_iot, sc->sc_ioh, NSPR_XFERMR, sc->sc_xfermr);
754 struct nsp_softc *sc;
755 struct targ_info *ti;
757 struct scsi_low_softc *slp = &sc->sc_sclow;
758 struct slccb *cb = slp->sl_Qnexus;
761 sc->sc_dataout_timeout = 0;
762 slp->sl_flags &= ~HW_PDMASTART;
763 nsp_setup_fifo(sc, NSP_FIFO_OFF, SCSI_LOW_READ, 0);
764 if ((sc->sc_icr & SCIENR_FIFO) != 0)
766 sc->sc_icr &= ~SCIENR_FIFO;
767 nsp_cr_write_1(sc->sc_iot, sc->sc_ioh, NSPR_SCIENR, sc->sc_icr);
772 slp->sl_error |= PDMAERR;
776 if (ti->ti_phase == PH_DATA)
778 cnt = nsp_fifo_count(sc);
779 if (slp->sl_scp.scp_direction == SCSI_LOW_WRITE)
781 len = sc->sc_cnt - cnt;
782 if (sc->sc_cnt >= cnt &&
783 slp->sl_scp.scp_datalen + len <=
784 cb->ccb_scp.scp_datalen)
786 slp->sl_scp.scp_data -= len;
787 slp->sl_scp.scp_datalen += len;
791 slp->sl_error |= PDMAERR;
792 printf("%s len %x >= datalen %x\n",
794 len, slp->sl_scp.scp_datalen);
797 else if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
799 if (sc->sc_cnt != cnt ||
800 sc->sc_cnt > cb->ccb_scp.scp_datalen)
802 slp->sl_error |= PDMAERR;
803 printf("%s: data read count error %x != %x (%x)\n",
804 slp->sl_xname, sc->sc_cnt, cnt,
805 cb->ccb_scp.scp_datalen);
809 scsi_low_data_finish(slp);
814 printf("%s data phase miss\n", slp->sl_xname);
815 slp->sl_error |= PDMAERR;
819 #define RFIFO_CRIT 64
820 #define WFIFO_CRIT 32
823 nsp_data_padding(sc, direction, count)
824 struct nsp_softc *sc;
828 bus_space_tag_t bst = sc->sc_iot;
829 bus_space_handle_t bsh = sc->sc_ioh;
831 if (count > NSP_MAX_DATA_SIZE)
832 count = NSP_MAX_DATA_SIZE;
834 nsp_cr_write_1(bst, bsh, NSPR_XFERMR, XFERMR_XEN | XFERMR_IO8);
835 if (direction == SCSI_LOW_READ)
838 (void) bus_space_read_1(bst, bsh, nsp_fifodr);
843 (void) bus_space_write_1(bst, bsh, nsp_fifodr, 0);
845 nsp_cr_write_1(bst, bsh, NSPR_XFERMR, sc->sc_xfermr);
849 nsp_read_fifo(sc, suspendio)
850 struct nsp_softc *sc;
853 struct scsi_low_softc *slp = &sc->sc_sclow;
854 bus_space_tag_t bst = sc->sc_iot;
855 bus_space_handle_t bsh = sc->sc_ioh;
858 res = nsp_fifo_count(sc);
859 if (res == sc->sc_cnt)
863 if (res < sc->sc_cnt || res == (u_int) -1)
865 printf("%s: strange fifo ack count 0x%x < 0x%x\n",
866 slp->sl_xname, res, sc->sc_cnt);
869 #endif /* NSP_DEBUG */
871 res = res - sc->sc_cnt;
872 if (res > slp->sl_scp.scp_datalen)
874 if ((slp->sl_error & PDMAERR) == 0)
876 printf("%s: data overrun 0x%x > 0x%x\n",
877 slp->sl_xname, res, slp->sl_scp.scp_datalen);
880 slp->sl_error |= PDMAERR;
881 slp->sl_scp.scp_datalen = 0;
883 if ((slp->sl_flags & HW_READ_PADDING) == 0)
885 printf("%s: read padding required\n", slp->sl_xname);
889 nsp_data_padding(sc, SCSI_LOW_READ, res);
891 return 1; /* padding start */
894 if (suspendio > 0 && slp->sl_scp.scp_datalen >= suspendio)
897 if ((sc->sc_xfermr & (XFERMR_MEM32 | XFERMR_MEM8)) != 0)
899 if ((sc->sc_xfermr & XFERMR_MEM32) != 0)
902 bus_space_read_region_4(sc->sc_memt, sc->sc_memh, 0,
903 (u_int32_t *) slp->sl_scp.scp_data, res >> 2);
907 bus_space_read_region_1(sc->sc_memt, sc->sc_memh, 0,
908 (u_int8_t *) slp->sl_scp.scp_data, res);
913 if ((sc->sc_xfermr & XFERMR_IO32) != 0)
916 bus_space_read_multi_4(bst, bsh, nsp_fifodr,
917 (u_int32_t *) slp->sl_scp.scp_data, res >> 2);
921 bus_space_read_multi_1(bst, bsh, nsp_fifodr,
922 (u_int8_t *) slp->sl_scp.scp_data, res);
926 if (nsp_cr_read_1(bst, bsh, NSPR_PARITYR) & PARITYR_PE)
928 nsp_cr_write_1(bst, bsh, NSPR_PARITYR,
929 PARITYR_ENABLE | PARITYR_CLEAR);
930 scsi_low_assert_msg(slp, slp->sl_Tnexus, SCSI_LOW_MSG_ERROR, 1);
933 slp->sl_scp.scp_data += res;
934 slp->sl_scp.scp_datalen -= res;
940 nsp_write_fifo(sc, suspendio)
941 struct nsp_softc *sc;
944 struct scsi_low_softc *slp = &sc->sc_sclow;
945 bus_space_tag_t bst = sc->sc_iot;
946 bus_space_handle_t bsh = sc->sc_ioh;
948 register u_int8_t stat;
953 if ((slp->sl_scp.scp_datalen % WFIFO_CRIT) != 0)
955 printf("%s: strange write length 0x%x\n",
956 slp->sl_xname, slp->sl_scp.scp_datalen);
958 #endif /* NSP_DEBUG */
959 res = slp->sl_scp.scp_datalen % suspendio;
970 if (res > slp->sl_scp.scp_datalen)
971 res = slp->sl_scp.scp_datalen;
973 /* XXX: reconfirm! */
974 stat = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON) & SCBUSMON_PHMASK;
975 if (stat != PHASE_DATAOUT)
978 if ((sc->sc_xfermr & (XFERMR_MEM32 | XFERMR_MEM8)) != 0)
980 if ((sc->sc_xfermr & XFERMR_MEM32) != 0)
982 bus_space_write_region_4(sc->sc_memt, sc->sc_memh, 0,
983 (u_int32_t *) slp->sl_scp.scp_data, res >> 2);
987 bus_space_write_region_1(sc->sc_memt, sc->sc_memh, 0,
988 (u_int8_t *) slp->sl_scp.scp_data, res);
993 if ((sc->sc_xfermr & XFERMR_IO32) != 0)
995 bus_space_write_multi_4(bst, bsh, nsp_fifodr,
996 (u_int32_t *) slp->sl_scp.scp_data, res >> 2);
1000 bus_space_write_multi_1(bst, bsh, nsp_fifodr,
1001 (u_int8_t *) slp->sl_scp.scp_data, res);
1005 slp->sl_scp.scp_datalen -= res;
1006 slp->sl_scp.scp_data += res;
1012 nsp_wait_interrupt(sc)
1013 struct nsp_softc *sc;
1015 bus_space_tag_t bst = sc->sc_iot;
1016 bus_space_handle_t bsh = sc->sc_ioh;
1018 register u_int8_t isrc;
1020 for (tout = 0; tout < DEV_BSIZE / 10; tout ++)
1022 isrc = bus_space_read_1(bst, bsh, nsp_irqsr);
1023 if ((isrc & (IRQSR_SCSI | IRQSR_FIFO)) != 0)
1025 if ((isrc & IRQSR_FIFO) != 0)
1027 bus_space_write_1(bst, bsh,
1028 nsp_irqcr, IRQCR_FIFOCL);
1038 nsp_pio_read(sc, suspendio)
1039 struct nsp_softc *sc;
1042 struct scsi_low_softc *slp = &sc->sc_sclow;
1043 bus_space_tag_t bst = sc->sc_iot;
1044 bus_space_handle_t bsh = sc->sc_ioh;
1045 int tout, padding, datalen;
1046 register u_int8_t stat, fstat;
1049 tout = sc->sc_tmaxcnt;
1050 slp->sl_flags |= HW_PDMASTART;
1051 datalen = slp->sl_scp.scp_datalen;
1056 stat = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
1057 if (stat == (u_int8_t) -1)
1060 /* out of data phase */
1061 if ((stat & SCBUSMON_PHMASK) != PHASE_DATAIN)
1063 nsp_read_fifo(sc, 0);
1068 fstat = bus_space_read_1(bst, bsh, nsp_fifosr);
1069 if ((fstat & FIFOSR_FULLEMP) != 0)
1071 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1073 bus_space_write_1(bst, bsh, nsp_irqcr,
1079 padding |= nsp_read_fifo(sc, suspendio);
1083 padding |= nsp_read_fifo(sc, 0);
1086 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1091 if (padding == 0 && slp->sl_scp.scp_datalen <= 0)
1094 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1102 printf("%s: nsp_pio_read: timeout\n", slp->sl_xname);
1108 if (slp->sl_scp.scp_datalen > 0 &&
1109 slp->sl_scp.scp_datalen > datalen - nsp_read_interrupt_bytes)
1111 if (nsp_wait_interrupt(sc) != 0)
1117 nsp_pio_write(sc, suspendio)
1118 struct nsp_softc *sc;
1121 struct scsi_low_softc *slp = &sc->sc_sclow;
1122 bus_space_tag_t bst = sc->sc_iot;
1123 bus_space_handle_t bsh = sc->sc_ioh;
1124 u_int rcount, acount;
1126 register u_int8_t stat, fstat;
1128 tout = sc->sc_tmaxcnt;
1129 slp->sl_flags |= HW_PDMASTART;
1130 datalen = slp->sl_scp.scp_datalen;
1135 stat = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON) & SCBUSMON_PHMASK;
1136 if (stat != PHASE_DATAOUT)
1139 if (slp->sl_scp.scp_datalen <= 0)
1141 if (sc->sc_dataout_timeout == 0)
1142 sc->sc_dataout_timeout = SCSI_LOW_TIMEOUT_HZ;
1146 fstat = bus_space_read_1(bst, bsh, nsp_fifosr);
1147 if ((fstat & FIFOSR_FULLEMP) != 0)
1149 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1151 bus_space_write_1(bst, bsh, nsp_irqcr,
1158 * To avoid timeout of pcmcia bus
1159 * (not scsi bus!), we should check
1160 * the scsi device sends us request
1161 * signals, which means the scsi device
1162 * is ready to recieve data without
1165 if ((slp->sl_scp.scp_datalen % suspendio) == 0)
1168 * fill the nsp fifo, and waiting for
1171 nsp_write_fifo(sc, 0);
1176 * check the request singals.
1178 acount = nsp_fifo_count(sc);
1179 rcount = nsp_request_count(sc);
1180 if (rcount <= acount)
1182 nsp_write_fifo(sc, 0);
1184 nsp_statics.device_busy ++;
1185 #endif /* NSP_STATICS */
1189 nsp_write_fifo(sc, suspendio);
1191 nsp_statics.device_data_write ++;
1192 #endif /* NSP_STATICS */
1198 nsp_write_fifo(sc, 0);
1201 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1206 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1214 printf("%s: nsp_pio_write: timeout\n", slp->sl_xname);
1219 if (slp->sl_scp.scp_datalen > 0 &&
1220 slp->sl_scp.scp_datalen > datalen - nsp_write_interrupt_bytes)
1222 if (nsp_wait_interrupt(sc) != 0)
1228 nsp_negate_signal(sc, mask, s)
1229 struct nsp_softc *sc;
1233 struct scsi_low_softc *slp = &sc->sc_sclow;
1234 bus_space_tag_t bst = sc->sc_iot;
1235 bus_space_handle_t bsh = sc->sc_ioh;
1239 for (wc = 0; wc < NSP_DELAY_MAX / NSP_DELAY_INTERVAL; wc ++)
1241 regv = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
1242 if (regv == (u_int8_t) -1)
1244 if ((regv & mask) == 0)
1246 SCSI_LOW_DELAY(NSP_DELAY_INTERVAL);
1249 printf("%s: %s nsp_negate_signal timeout\n", slp->sl_xname, s);
1254 nsp_xfer(sc, buf, len, phase, clear_atn)
1255 struct nsp_softc *sc;
1261 bus_space_tag_t bst = sc->sc_iot;
1262 bus_space_handle_t bsh = sc->sc_ioh;
1265 for (ptr = 0; len > 0; len --, ptr ++)
1267 rv = nsp_expect_signal(sc, phase, SCBUSMON_REQ);
1271 if (len == 1 && clear_atn != 0)
1273 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR,
1274 SCBUSCR_ADIR | SCBUSCR_ACKEN);
1275 SCSI_LOW_DEASSERT_ATN(&sc->sc_sclow);
1278 if (phase & SCBUSMON_IO)
1280 buf[ptr] = nsp_cr_read_1(bst, bsh, NSPR_DATAACK);
1284 nsp_cr_write_1(bst, bsh, NSPR_DATAACK, buf[ptr]);
1286 nsp_negate_signal(sc, SCBUSMON_ACK, "xfer<ACK>");
1293 /**************************************************************
1294 * disconnect & reselect (HW low)
1295 **************************************************************/
1298 struct nsp_softc *sc;
1300 struct scsi_low_softc *slp = &sc->sc_sclow;
1301 bus_space_tag_t bst = sc->sc_iot;
1302 bus_space_handle_t bsh = sc->sc_ioh;
1303 struct targ_info *ti;
1307 sid = (u_int) nsp_cr_read_1(bst, bsh, NSPR_RESELR);
1308 sid &= ~sc->sc_idbit;
1310 if ((ti = scsi_low_reselected(slp, sid)) == NULL)
1313 nsp_negate_signal(sc, SCBUSMON_SEL, "reselect<SEL>");
1315 cr = nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR);
1316 cr &= ~(SCBUSCR_BSY | SCBUSCR_ATN);
1317 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1318 cr |= SCBUSCR_ADIR | SCBUSCR_ACKEN;
1319 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1322 nsp_statics.reselect ++;
1323 #endif /* NSP_STATCIS */
1328 nsp_disconnected(sc, ti)
1329 struct nsp_softc *sc;
1330 struct targ_info *ti;
1332 struct scsi_low_softc *slp = &sc->sc_sclow;
1333 bus_space_tag_t bst = sc->sc_iot;
1334 bus_space_handle_t bsh = sc->sc_ioh;
1336 nsp_cr_write_1(bst, bsh, NSPR_PTCLRR, PTCLRR_PT | PTCLRR_ACK |
1337 PTCLRR_REQ | PTCLRR_HOST);
1338 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1340 sc->sc_icr &= ~SCIENR_FIFO;
1341 nsp_cr_write_1(bst, bsh, NSPR_SCIENR, sc->sc_icr);
1344 sc->sc_dataout_timeout = 0;
1346 nsp_statics.disconnect ++;
1347 #endif /* NSP_STATICS */
1348 scsi_low_disconnected(slp, ti);
1352 /**************************************************************
1354 **************************************************************/
1355 static void nsp_error(struct nsp_softc *, u_char *, u_int8_t, u_int8_t, u_int8_t);
1358 nsp_error(sc, s, isrc, ph, irqphs)
1359 struct nsp_softc *sc;
1361 u_int8_t isrc, ph, irqphs;
1363 struct scsi_low_softc *slp = &sc->sc_sclow;
1365 printf("%s: %s\n", slp->sl_xname, s);
1366 printf("%s: isrc 0x%x scmon 0x%x irqphs 0x%x\n",
1367 slp->sl_xname, (u_int) isrc, (u_int) ph, (u_int) irqphs);
1371 nsp_target_nexus_establish(sc)
1372 struct nsp_softc *sc;
1374 struct scsi_low_softc *slp = &sc->sc_sclow;
1375 bus_space_tag_t bst = sc->sc_iot;
1376 bus_space_handle_t bsh = sc->sc_ioh;
1377 struct targ_info *ti = slp->sl_Tnexus;
1378 struct nsp_targ_info *nti = (void *) ti;
1380 /* setup synch transfer registers */
1381 nsp_cr_write_1(bst, bsh, NSPR_SYNCR, nti->nti_reg_syncr);
1382 nsp_cr_write_1(bst, bsh, NSPR_ACKWIDTH, nti->nti_reg_ackwidth);
1384 /* setup pdma fifo (minimum) */
1385 nsp_setup_fifo(sc, NSP_FIFO_ON, SCSI_LOW_READ, 0);
1390 nsp_lun_nexus_establish(sc)
1391 struct nsp_softc *sc;
1398 nsp_ccb_nexus_establish(sc)
1399 struct nsp_softc *sc;
1401 struct scsi_low_softc *slp = &sc->sc_sclow;
1402 struct slccb *cb = slp->sl_Qnexus;
1404 sc->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
1406 /* setup pdma fifo */
1407 nsp_setup_fifo(sc, NSP_FIFO_ON,
1408 slp->sl_scp.scp_direction, slp->sl_scp.scp_datalen);
1410 if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
1412 if (sc->sc_suspendio > 0 &&
1413 (nsp_io_control & NSP_READ_FIFO_INTERRUPTS) != 0)
1415 sc->sc_icr |= SCIENR_FIFO;
1416 nsp_cr_write_1(sc->sc_iot, sc->sc_ioh,
1417 NSPR_SCIENR, sc->sc_icr);
1422 if (sc->sc_suspendio > 0 &&
1423 (nsp_io_control & NSP_WRITE_FIFO_INTERRUPTS) != 0)
1425 sc->sc_icr |= SCIENR_FIFO;
1426 nsp_cr_write_1(sc->sc_iot, sc->sc_ioh,
1427 NSPR_SCIENR, sc->sc_icr);
1434 nsp_phase_match(sc, phase, stat)
1435 struct nsp_softc *sc;
1439 struct scsi_low_softc *slp = &sc->sc_sclow;
1441 if ((stat & SCBUSMON_PHMASK) != phase)
1443 printf("%s: phase mismatch 0x%x != 0x%x\n",
1444 slp->sl_xname, (u_int) phase, (u_int) stat);
1448 if ((stat & SCBUSMON_REQ) == 0)
1458 struct nsp_softc *sc = arg;
1459 struct scsi_low_softc *slp = &sc->sc_sclow;
1460 bus_space_tag_t bst = sc->sc_iot;
1461 bus_space_handle_t bsh = sc->sc_ioh;
1462 struct targ_info *ti;
1463 struct physio_proc *pp;
1465 u_int derror, flags;
1467 u_int8_t isrc, ph, irqphs, cr, regv;
1469 /*******************************************
1471 *******************************************/
1472 if (slp->sl_flags & HW_INACTIVE)
1475 bus_space_write_1(bst, bsh, nsp_irqcr, IRQCR_IRQDIS);
1476 isrc = bus_space_read_1(bst, bsh, nsp_irqsr);
1477 if (isrc == (u_int8_t) -1 || (isrc & IRQSR_MASK) == 0)
1479 bus_space_write_1(bst, bsh, nsp_irqcr, 0);
1484 * Do not read an irqphs register if no scsi phase interrupt.
1485 * Unless, you should lose a scsi phase interrupt.
1487 ph = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
1488 if ((isrc & IRQSR_SCSI) != 0)
1490 irqphs = nsp_cr_read_1(bst, bsh, NSPR_IRQPHS);
1496 * timer interrupt handler (scsi vs timer interrupts)
1498 if (sc->sc_timer != 0)
1500 nsp_cr_write_1(bst, bsh, NSPR_TIMERCNT, 0);
1501 nsp_cr_write_1(bst, bsh, NSPR_TIMERCNT, 0);
1505 /* check a timer interrupt */
1507 if ((isrc & IRQSR_TIMER) != 0)
1509 if ((isrc & IRQSR_MASK) == IRQSR_TIMER && sc->sc_seltout == 0)
1511 bus_space_write_1(bst, bsh, nsp_irqcr, IRQCR_TIMERCL);
1514 regv |= IRQCR_TIMERCL;
1517 /* check a fifo interrupt */
1518 if ((isrc & IRQSR_FIFO) != 0)
1520 regv |= IRQCR_FIFOCL;
1523 /* OK. enable all interrupts */
1524 bus_space_write_1(bst, bsh, nsp_irqcr, regv);
1526 /*******************************************
1528 *******************************************/
1532 nsp_error(sc, "current status", isrc, ph, irqphs);
1533 scsi_low_print(slp, NULL);
1536 SCSI_LOW_DEBUGGER("nsp");
1539 #endif /* NSP_DEBUG */
1541 /*******************************************
1542 * Parse hardware SCSI irq reasons register
1543 *******************************************/
1544 if ((isrc & IRQSR_SCSI) != 0)
1546 if ((irqphs & IRQPHS_RST) != 0)
1548 scsi_low_restart(slp, SCSI_LOW_RESTART_SOFT,
1549 "bus reset (power off?)");
1553 if ((irqphs & IRQPHS_RSEL) != 0)
1555 bus_space_write_1(bst, bsh, nsp_irqcr, IRQCR_RESCL);
1556 if (nsp_reselected(sc) == EJUSTRETURN)
1560 if ((irqphs & (IRQPHS_PCHG | IRQPHS_LBF)) == 0)
1564 /*******************************************
1566 *******************************************/
1567 if ((ti = slp->sl_Tnexus) == NULL)
1569 /* unknown scsi phase changes */
1570 nsp_error(sc, "unknown scsi phase changes", isrc, ph, irqphs);
1574 /*******************************************
1575 * aribitration & selection
1576 *******************************************/
1577 switch (ti->ti_phase)
1580 if ((ph & SCBUSMON_BSY) == 0)
1582 if (sc->sc_seltout >= NSP_SELTIMEOUT)
1585 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, 0);
1586 return nsp_disconnected(sc, ti);
1589 nsp_start_timer(sc, NSP_TIMER_1MS);
1593 SCSI_LOW_SETUP_PHASE(ti, PH_SELECTED);
1594 nsphw_selection_done_and_expect_msgout(sc);
1598 if ((isrc & IRQSR_SCSI) == 0)
1601 nsp_target_nexus_establish(sc);
1605 if ((isrc & IRQSR_SCSI) == 0)
1608 nsp_target_nexus_establish(sc);
1609 if ((ph & SCBUSMON_PHMASK) != PHASE_MSGIN)
1611 printf("%s: unexpected phase after reselect\n",
1613 slp->sl_error |= FATALIO;
1614 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 1);
1620 if ((isrc & IRQSR_SCSI) != 0)
1622 if ((isrc & IRQSR_FIFO) != 0)
1624 if (NSP_IS_PHASE_DATA(ph) == 0)
1626 irqphs = (ph & IRQPHS_PHMASK);
1632 if ((isrc & IRQSR_SCSI) == 0)
1637 /*******************************************
1638 * data phase control
1639 *******************************************/
1640 if (slp->sl_flags & HW_PDMASTART)
1642 if ((isrc & IRQSR_SCSI) != 0 &&
1643 NSP_IS_IRQPHS_DATA(irqphs) == 0)
1645 if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
1646 nsp_pio_read(sc, 0);
1647 nsp_pdma_end(sc, ti);
1651 /*******************************************
1653 *******************************************/
1654 if (slp->sl_msgphase != 0 && (irqphs & IRQPHS_LBF) != 0)
1655 return nsp_disconnected(sc, ti);
1657 /* check unexpected bus free state */
1660 nsp_error(sc, "unexpected bus free", isrc, ph, irqphs);
1661 return nsp_disconnected(sc, ti);
1664 /* check normal scsi phase */
1665 switch (irqphs & IRQPHS_PHMASK)
1668 if (nsp_phase_match(sc, PHASE_CMD, ph) != 0)
1671 SCSI_LOW_SETUP_PHASE(ti, PH_CMD);
1672 if (scsi_low_cmd(slp, ti) != 0)
1674 scsi_low_attention(slp);
1677 nsp_cr_write_1(bst, bsh, NSPR_CMDCR, CMDCR_PTCLR);
1678 for (len = 0; len < slp->sl_scp.scp_cmdlen; len ++)
1679 nsp_cr_write_1(bst, bsh, NSPR_CMDDR,
1680 slp->sl_scp.scp_cmd[len]);
1682 nsp_cr_write_1(bst, bsh, NSPR_CMDCR, CMDCR_PTCLR | CMDCR_EXEC);
1685 case IRQPHS_DATAOUT:
1686 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1687 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_WRITE) != 0)
1689 scsi_low_attention(slp);
1692 pp = physio_proc_enter(bp);
1693 nsp_pio_write(sc, sc->sc_suspendio);
1694 physio_proc_leave(pp);
1698 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1699 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_READ) != 0)
1701 scsi_low_attention(slp);
1704 pp = physio_proc_enter(bp);
1705 nsp_pio_read(sc, sc->sc_suspendio);
1706 physio_proc_leave(pp);
1710 if (nsp_phase_match(sc, PHASE_STATUS, ph) != 0)
1713 SCSI_LOW_SETUP_PHASE(ti, PH_STAT);
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, NSPR_PARITYR,
1718 PARITYR_ENABLE | PARITYR_CLEAR);
1719 derror = SCSI_LOW_DATA_PE;
1725 cr = SCBUSCR_ACK | nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR);
1726 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1728 if (scsi_low_statusin(slp, ti, derror | regv) != 0)
1730 scsi_low_attention(slp);
1733 /* check REQ nagated */
1734 nsp_negate_signal(sc, SCBUSMON_REQ, "statin<REQ>");
1737 cr = nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR) & (~SCBUSCR_ACK);
1738 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1742 if (nsp_phase_match(sc, PHASE_MSGOUT, ph) != 0)
1745 #ifdef NSP_MSGOUT_SERIALIZE
1748 * NSP invoke interrupts only in the case of scsi phase changes,
1749 * therefore we should poll the scsi phase here to catch
1750 * the next "msg out" if exists (no scsi phase changes).
1754 SCSI_LOW_SETUP_PHASE(ti, PH_MSGOUT);
1755 flags = (ti->ti_ophase != ti->ti_phase) ?
1756 SCSI_LOW_MSGOUT_INIT : 0;
1757 len = scsi_low_msgout(slp, ti, flags);
1759 if (len > 1 && slp->sl_atten == 0)
1761 scsi_low_attention(slp);
1764 if (nsp_xfer(sc, ti->ti_msgoutstr, len, PHASE_MSGOUT,
1765 slp->sl_clear_atten) != 0)
1767 slp->sl_error |= FATALIO;
1768 nsp_error(sc, "MSGOUT: xfer short",
1772 /* catch a next signal */
1773 rv = nsp_expect_signal(sc, PHASE_MSGOUT, SCBUSMON_REQ);
1775 while (rv > 0 && len -- > 0);
1777 #else /* !NSP_MSGOUT_SERIALIZE */
1778 SCSI_LOW_SETUP_PHASE(ti, PH_MSGOUT);
1779 flags = SCSI_LOW_MSGOUT_UNIFY;
1780 if (ti->ti_ophase != ti->ti_phase)
1781 flags |= SCSI_LOW_MSGOUT_INIT;
1782 len = scsi_low_msgout(slp, ti, flags);
1784 if (len > 1 && slp->sl_atten == 0)
1786 scsi_low_attention(slp);
1789 if (nsp_xfer(sc, ti->ti_msgoutstr, len, PHASE_MSGOUT,
1790 slp->sl_clear_atten) != 0)
1792 nsp_error(sc, "MSGOUT: xfer short", isrc, ph, irqphs);
1795 #endif /* !NSP_MSGOUT_SERIALIZE */
1799 if (nsp_phase_match(sc, PHASE_MSGIN, ph) != 0)
1804 * NSP invoke interrupts only in the case of scsi phase changes,
1805 * therefore we should poll the scsi phase here to catch
1806 * the next "msg in" if exists (no scsi phase changes).
1810 SCSI_LOW_SETUP_PHASE(ti, PH_MSGIN);
1813 regv = nsp_cr_read_1(bst, bsh, NSPR_DATA);
1814 if (nsp_cr_read_1(bst, bsh, NSPR_PARITYR) & PARITYR_PE)
1816 nsp_cr_write_1(bst, bsh,
1818 PARITYR_ENABLE | PARITYR_CLEAR);
1819 derror = SCSI_LOW_DATA_PE;
1827 cr = nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR) | SCBUSCR_ACK;
1828 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1830 if (scsi_low_msgin(slp, ti, regv | derror) == 0)
1832 if (scsi_low_is_msgout_continue(ti, 0) != 0)
1834 scsi_low_attention(slp);
1838 /* check REQ nagated */
1839 nsp_negate_signal(sc, SCBUSMON_REQ, "msgin<REQ>");
1842 cr = nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR) & (~SCBUSCR_ACK);
1843 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1845 /* catch a next signal */
1846 rv = nsp_expect_signal(sc, PHASE_MSGIN, SCBUSMON_REQ);
1848 while (rv > 0 && len -- > 0);
1852 slp->sl_error |= FATALIO;
1853 nsp_error(sc, "unknown scsi phase", isrc, ph, irqphs);
1861 nsp_start_timer(sc, NSP_TIMER_1MS);
1868 struct nsp_softc *sc;
1870 struct scsi_low_softc *slp = &sc->sc_sclow;
1871 bus_space_tag_t iot = sc->sc_iot;
1872 bus_space_handle_t ioh = sc->sc_ioh;
1876 if (slp->sl_Tnexus == NULL)
1879 ph = nsp_cr_read_1(iot, ioh, NSPR_SCBUSMON);
1880 switch (ph & SCBUSMON_PHMASK)
1883 if (sc->sc_dataout_timeout == 0)
1886 /* check a fifo empty */
1887 regv = bus_space_read_1(iot, ioh, nsp_fifosr);
1888 if ((regv & FIFOSR_FULLEMP) == 0)
1890 bus_space_write_1(iot, ioh, nsp_irqcr, IRQCR_FIFOCL);
1892 /* check still requested */
1893 ph = nsp_cr_read_1(iot, ioh, NSPR_SCBUSMON);
1894 if ((ph & SCBUSMON_REQ) == 0)
1897 if ((-- sc->sc_dataout_timeout) > 0)
1900 slp->sl_error |= PDMAERR;
1901 if ((slp->sl_flags & HW_WRITE_PADDING) == 0)
1903 printf("%s: write padding required\n", slp->sl_xname);
1907 tout = NSP_DELAY_MAX;
1910 ph = nsp_cr_read_1(iot, ioh, NSPR_SCBUSMON);
1911 if ((ph & SCBUSMON_PHMASK) != PHASE_DATAOUT)
1913 regv = bus_space_read_1(iot, ioh, nsp_fifosr);
1914 if ((regv & FIFOSR_FULLEMP) == 0)
1920 bus_space_write_1(iot, ioh, nsp_irqcr, IRQCR_FIFOCL);
1921 nsp_data_padding(sc, SCSI_LOW_WRITE, 32);
1923 ph = nsp_cr_read_1(iot, ioh, NSPR_SCBUSMON);
1924 if ((ph & SCBUSMON_PHMASK) == PHASE_DATAOUT)
1925 sc->sc_dataout_timeout = SCSI_LOW_TIMEOUT_HZ;