1 /* $FreeBSD: src/sys/dev/nsp/nsp.c,v 1.1.2.6 2001/12/17 13:30:18 non Exp $ */
2 /* $DragonFly: src/sys/dev/disk/nsp/nsp.c,v 1.3 2003/07/26 14:25:23 rob Exp $ */
3 /* $NecBSD: nsp.c,v 1.21.12.6 2001/06/29 06:27:52 honda Exp $ */
8 #define NSP_IO_CONTROL_FLAGS \
9 (NSP_READ_SUSPEND_IO | NSP_WRITE_SUSPEND_IO | \
10 NSP_READ_FIFO_INTERRUPTS | NSP_WRITE_FIFO_INTERRUPTS | \
11 NSP_USE_MEMIO | NSP_WAIT_FOR_SELECT)
14 * Copyright (c) 1998, 1999, 2000, 2001
15 * NetBSD/pc98 porting staff. All rights reserved.
17 * Copyright (c) 1998, 1999, 2000, 2001
18 * Naofumi HONDA. All rights reserved.
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
23 * 1. Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright
26 * notice, this list of conditions and the following disclaimer in the
27 * documentation and/or other materials provided with the distribution.
28 * 3. The name of the author may not be used to endorse or promote products
29 * derived from this software without specific prior written permission.
31 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
32 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
33 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
34 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
35 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
37 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
40 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGE.
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
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_pio.h>
78 #include <machine/bus_memio.h>
79 #include <machine/bus.h>
81 #include <machine/dvcfg.h>
82 #include <machine/physio_proc.h>
84 #include <cam/scsi/scsi_low.h>
85 #include <dev/nsp/nspreg.h>
86 #include <dev/nsp/nspvar.h>
87 #endif /* __FreeBSD__ */
89 /***************************************************
91 ***************************************************/
92 /* DEVICE CONFIGURATION FLAGS (MINOR)
95 * 0x02 PARITY LINE OFF
96 * 0x04 IDENTIFY MSG OFF ( = single lun)
97 * 0x08 SYNC TRANSFER OFF
100 /***************************************************
102 ***************************************************/
103 #define NSP_NTARGETS 8
106 #define NSP_MAX_DATA_SIZE (64 * 1024)
107 #define NSP_SELTIMEOUT (200)
108 #define NSP_DELAY_MAX (2 * 1000 * 1000)
109 #define NSP_DELAY_INTERVAL (1)
110 #define NSP_TIMER_1MS (1000 / 51)
112 /***************************************************
114 ***************************************************/
117 #endif /* NSP_DEBUG */
121 int arbit_conflict_1;
122 int arbit_conflict_2;
123 int device_data_write;
127 int data_phase_bypass;
129 #endif /* NSP_STATICS */
131 /***************************************************
133 ***************************************************/
134 #define NSP_READ_SUSPEND_IO 0x0001
135 #define NSP_WRITE_SUSPEND_IO 0x0002
136 #define NSP_USE_MEMIO 0x0004
137 #define NSP_READ_FIFO_INTERRUPTS 0x0010
138 #define NSP_WRITE_FIFO_INTERRUPTS 0x0020
139 #define NSP_WAIT_FOR_SELECT 0x0100
141 u_int nsp_io_control = NSP_IO_CONTROL_FLAGS;
142 int nsp_read_suspend_bytes = DEV_BSIZE;
143 int nsp_write_suspend_bytes = DEV_BSIZE;
144 int nsp_read_interrupt_bytes = 4096;
145 int nsp_write_interrupt_bytes = 4096;
147 /***************************************************
149 ***************************************************/
150 extern struct cfdriver nsp_cd;
152 /**************************************************************
154 **************************************************************/
155 #define NSP_FIFO_ON 1
156 #define NSP_FIFO_OFF 0
157 static void nsp_pio_read __P((struct nsp_softc *, int));
158 static void nsp_pio_write __P((struct nsp_softc *, int));
159 static int nsp_xfer __P((struct nsp_softc *, u_int8_t *, int, int, int));
160 static int nsp_msg __P((struct nsp_softc *, struct targ_info *, u_int));
161 static int nsp_reselected __P((struct nsp_softc *));
162 static int nsp_disconnected __P((struct nsp_softc *, struct targ_info *));
163 static void nsp_pdma_end __P((struct nsp_softc *, struct targ_info *));
164 static void nsphw_init __P((struct nsp_softc *));
165 static int nsp_target_nexus_establish __P((struct nsp_softc *));
166 static int nsp_lun_nexus_establish __P((struct nsp_softc *));
167 static int nsp_ccb_nexus_establish __P((struct nsp_softc *));
168 static int nsp_world_start __P((struct nsp_softc *, int));
169 static int nsphw_start_selection __P((struct nsp_softc *sc, struct slccb *));
170 static void nsphw_bus_reset __P((struct nsp_softc *));
171 static void nsphw_attention __P((struct nsp_softc *));
172 static u_int nsp_fifo_count __P((struct nsp_softc *));
173 static u_int nsp_request_count __P((struct nsp_softc *));
174 static int nsp_negate_signal __P((struct nsp_softc *, u_int8_t, u_char *));
175 static int nsp_expect_signal __P((struct nsp_softc *, u_int8_t, u_int8_t));
176 static void nsp_start_timer __P((struct nsp_softc *, int));
177 static void nsp_setup_fifo __P((struct nsp_softc *, int, int, int));
178 static int nsp_targ_init __P((struct nsp_softc *, struct targ_info *, int));
179 static void nsphw_selection_done_and_expect_msgout __P((struct nsp_softc *));
180 static void nsp_data_padding __P((struct nsp_softc *, int, u_int));
181 static int nsp_timeout __P((struct nsp_softc *));
182 static int nsp_read_fifo __P((struct nsp_softc *, int));
183 static int nsp_write_fifo __P((struct nsp_softc *, int));
184 static int nsp_phase_match __P((struct nsp_softc *, u_int8_t, u_int8_t));
185 static int nsp_wait_interrupt __P((struct nsp_softc *));
187 struct scsi_low_funcs nspfuncs = {
188 SC_LOW_INIT_T nsp_world_start,
189 SC_LOW_BUSRST_T nsphw_bus_reset,
190 SC_LOW_TARG_INIT_T nsp_targ_init,
191 SC_LOW_LUN_INIT_T NULL,
193 SC_LOW_SELECT_T nsphw_start_selection,
194 SC_LOW_NEXUS_T nsp_lun_nexus_establish,
195 SC_LOW_NEXUS_T nsp_ccb_nexus_establish,
197 SC_LOW_ATTEN_T nsphw_attention,
198 SC_LOW_MSG_T nsp_msg,
200 SC_LOW_TIMEOUT_T nsp_timeout,
201 SC_LOW_POLL_T nspintr,
206 /****************************************************
208 ****************************************************/
209 static __inline u_int8_t nsp_cr_read_1 __P((bus_space_tag_t bst, bus_space_handle_t bsh, bus_addr_t ofs));
210 static __inline void nsp_cr_write_1 __P((bus_space_tag_t bst, bus_space_handle_t bsh, bus_addr_t ofs, u_int8_t va));
212 static __inline u_int8_t
213 nsp_cr_read_1(bst, bsh, ofs)
215 bus_space_handle_t bsh;
219 bus_space_write_1(bst, bsh, nsp_idxr, ofs);
220 return bus_space_read_1(bst, bsh, nsp_datar);
224 nsp_cr_write_1(bst, bsh, ofs, va)
226 bus_space_handle_t bsh;
231 bus_space_write_1(bst, bsh, nsp_idxr, ofs);
232 bus_space_write_1(bst, bsh, nsp_datar, va);
236 nsp_expect_signal(sc, curphase, mask)
237 struct nsp_softc *sc;
238 u_int8_t curphase, mask;
240 struct scsi_low_softc *slp = &sc->sc_sclow;
241 bus_space_tag_t bst = sc->sc_iot;
242 bus_space_handle_t bsh = sc->sc_ioh;
246 for (wc = 0; wc < NSP_DELAY_MAX / NSP_DELAY_INTERVAL; wc ++)
248 ph = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
249 if (ph == (u_int8_t) -1)
252 isrc = bus_space_read_1(bst, bsh, nsp_irqsr);
253 if (isrc & IRQSR_SCSI)
256 if ((ph & mask) != 0 && (ph & SCBUSMON_PHMASK) == curphase)
259 SCSI_LOW_DELAY(NSP_DELAY_INTERVAL);
262 printf("%s: nsp_expect_signal timeout\n", slp->sl_xname);
268 struct nsp_softc *sc;
270 bus_space_tag_t bst = sc->sc_iot;
271 bus_space_handle_t bsh = sc->sc_ioh;
273 /* block all interrupts */
274 bus_space_write_1(bst, bsh, nsp_irqcr, IRQCR_ALLMASK);
276 /* setup SCSI interface */
277 bus_space_write_1(bst, bsh, nsp_ifselr, IFSELR_IFSEL);
279 nsp_cr_write_1(bst, bsh, NSPR_SCIENR, 0);
281 nsp_cr_write_1(bst, bsh, NSPR_XFERMR, XFERMR_IO8);
282 nsp_cr_write_1(bst, bsh, NSPR_CLKDIVR, sc->sc_iclkdiv);
284 nsp_cr_write_1(bst, bsh, NSPR_SCIENR, sc->sc_icr);
285 nsp_cr_write_1(bst, bsh, NSPR_PARITYR, sc->sc_parr);
286 nsp_cr_write_1(bst, bsh, NSPR_PTCLRR,
287 PTCLRR_ACK | PTCLRR_REQ | PTCLRR_HOST | PTCLRR_RSS);
289 /* setup fifo asic */
290 bus_space_write_1(bst, bsh, nsp_ifselr, IFSELR_REGSEL);
291 nsp_cr_write_1(bst, bsh, NSPR_TERMPWRC, 0);
292 if ((nsp_cr_read_1(bst, bsh, NSPR_OCR) & OCR_TERMPWRS) == 0)
293 nsp_cr_write_1(bst, bsh, NSPR_TERMPWRC, TERMPWRC_POWON);
295 nsp_cr_write_1(bst, bsh, NSPR_XFERMR, XFERMR_IO8);
296 nsp_cr_write_1(bst, bsh, NSPR_CLKDIVR, sc->sc_clkdiv);
297 nsp_cr_write_1(bst, bsh, NSPR_TIMERCNT, 0);
298 nsp_cr_write_1(bst, bsh, NSPR_TIMERCNT, 0);
300 nsp_cr_write_1(bst, bsh, NSPR_SYNCR, 0);
301 nsp_cr_write_1(bst, bsh, NSPR_ACKWIDTH, 0);
303 /* enable interrupts and ack them */
304 nsp_cr_write_1(bst, bsh, NSPR_SCIENR, sc->sc_icr);
305 bus_space_write_1(bst, bsh, nsp_irqcr, IRQSR_MASK);
307 nsp_setup_fifo(sc, NSP_FIFO_OFF, SCSI_LOW_READ, 0);
310 /****************************************************
312 ****************************************************/
315 struct nsp_softc *sc;
317 bus_space_tag_t bst = sc->sc_iot;
318 bus_space_handle_t bsh = sc->sc_ioh;
321 cr = nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR)/* & ~SCBUSCR_ACK */;
322 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr | SCBUSCR_ATN);
328 struct nsp_softc *sc;
330 bus_space_tag_t bst = sc->sc_iot;
331 bus_space_handle_t bsh = sc->sc_ioh;
334 bus_space_write_1(bst, bsh, nsp_irqcr, IRQCR_ALLMASK);
336 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, SCBUSCR_RST);
337 SCSI_LOW_DELAY(100 * 1000); /* 100ms */
338 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, 0);
339 for (i = 0; i < 5; i ++)
340 (void) nsp_cr_read_1(bst, bsh, NSPR_IRQPHS);
342 bus_space_write_1(bst, bsh, nsp_irqcr, IRQSR_MASK);
346 nsphw_selection_done_and_expect_msgout(sc)
347 struct nsp_softc *sc;
349 struct scsi_low_softc *slp = &sc->sc_sclow;
350 bus_space_tag_t bst = sc->sc_iot;
351 bus_space_handle_t bsh = sc->sc_ioh;
353 /* clear ack counter */
355 nsp_cr_write_1(bst, bsh, NSPR_PTCLRR, PTCLRR_PT | PTCLRR_ACK |
356 PTCLRR_REQ | PTCLRR_HOST);
358 /* deassert sel and assert atten */
360 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, sc->sc_busc);
362 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR,
363 sc->sc_busc | SCBUSCR_ADIR | SCBUSCR_ACKEN);
364 SCSI_LOW_ASSERT_ATN(slp);
368 nsphw_start_selection(sc, cb)
369 struct nsp_softc *sc;
372 struct scsi_low_softc *slp = &sc->sc_sclow;
373 bus_space_tag_t bst = sc->sc_iot;
374 bus_space_handle_t bsh = sc->sc_ioh;
375 struct targ_info *ti = cb->ti;
379 wc = sc->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
380 sc->sc_dataout_timeout = 0;
384 ph = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
385 if (ph != SCBUSMON_FREE)
389 nsp_statics.arbit_conflict_1 ++;
390 #endif /* NSP_STATICS */
391 return SCSI_LOW_START_FAIL;
394 /* start arbitration */
395 nsp_cr_write_1(bst, bsh, NSPR_ARBITS, ARBITS_EXEC);
398 SCSI_LOW_SETUP_PHASE(ti, PH_ARBSTART);
401 /* XXX: what a stupid chip! */
402 arbs = nsp_cr_read_1(bst, bsh, NSPR_ARBITS);
405 while ((arbs & (ARBITS_WIN | ARBITS_FAIL)) == 0 && wc -- > 0);
407 if ((arbs & ARBITS_WIN) == 0)
409 nsp_cr_write_1(bst, bsh, NSPR_ARBITS, ARBITS_CLR);
411 nsp_statics.arbit_conflict_2 ++;
412 #endif /* NSP_STATICS */
413 return SCSI_LOW_START_FAIL;
416 /* assert select line */
417 SCSI_LOW_SETUP_PHASE(ti, PH_SELSTART);
418 scsi_low_arbit_win(slp);
422 nsp_cr_write_1(bst, bsh, NSPR_DATA,
423 sc->sc_idbit | (1 << ti->ti_id));
424 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR,
425 SCBUSCR_SEL | SCBUSCR_BSY | sc->sc_busc);
427 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, SCBUSCR_SEL |
428 SCBUSCR_BSY | SCBUSCR_DOUT | sc->sc_busc);
429 nsp_cr_write_1(bst, bsh, NSPR_ARBITS, ARBITS_CLR);
431 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR,
432 SCBUSCR_SEL | SCBUSCR_DOUT | sc->sc_busc);
435 if ((nsp_io_control & NSP_WAIT_FOR_SELECT) != 0)
437 #define NSP_FIRST_SEL_WAIT 300
438 #define NSP_SEL_CHECK_INTERVAL 10
440 /* wait for a selection response */
441 for (wc = 0; wc < NSP_FIRST_SEL_WAIT / NSP_SEL_CHECK_INTERVAL;
444 ph = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
445 if ((ph & SCBUSMON_BSY) == 0)
447 SCSI_LOW_DELAY(NSP_SEL_CHECK_INTERVAL);
452 ph = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
453 if ((ph & SCBUSMON_BSY) != 0)
455 nsphw_selection_done_and_expect_msgout(sc);
458 SCSI_LOW_SETUP_PHASE(ti, PH_SELECTED);
459 return SCSI_LOW_START_OK;
465 /* check a selection timeout */
466 nsp_start_timer(sc, NSP_TIMER_1MS);
468 return SCSI_LOW_START_OK;
472 nsp_world_start(sc, fdone)
473 struct nsp_softc *sc;
476 struct scsi_low_softc *slp = &sc->sc_sclow;
481 if ((slp->sl_cfgflags & CFG_NOATTEN) == 0)
482 sc->sc_busc = SCBUSCR_ATN;
486 if ((slp->sl_cfgflags & CFG_NOPARITY) == 0)
487 sc->sc_parr = PARITYR_ENABLE | PARITYR_CLEAR;
491 sc->sc_icr = (SCIENR_SCCHG | SCIENR_RESEL | SCIENR_RST);
494 scsi_low_bus_reset(slp);
496 SOFT_INTR_REQUIRED(slp);
500 struct ncp_synch_data {
507 static struct ncp_synch_data ncp_sync_data_40M[] = {
508 {0x0c,0x0c,0x1,0}, /* 20MB 50ns*/
509 {0x19,0x19,0x3,1}, /* 10MB 100ns*/
510 {0x1a,0x25,0x5,2}, /* 7.5MB 150ns*/
511 {0x26,0x32,0x7,3}, /* 5MB 200ns*/
515 static struct ncp_synch_data ncp_sync_data_20M[] = {
516 {0x19,0x19,0x1,0}, /* 10MB 100ns*/
517 {0x1a,0x25,0x2,0}, /* 7.5MB 150ns*/
518 {0x26,0x32,0x3,1}, /* 5MB 200ns*/
524 struct nsp_softc *sc;
525 struct targ_info *ti;
528 bus_space_tag_t bst = sc->sc_iot;
529 bus_space_handle_t bsh = sc->sc_ioh;
530 struct ncp_synch_data *sdp;
531 struct nsp_targ_info *nti = (void *) ti;
532 u_int period, offset;
535 if ((msg & SCSI_LOW_MSG_WIDE) != 0)
537 if (ti->ti_width != SCSI_LOW_BUS_WIDTH_8)
539 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
545 if ((msg & SCSI_LOW_MSG_SYNCH) == 0)
548 period = ti->ti_maxsynch.period;
549 offset = ti->ti_maxsynch.offset;
550 if (sc->sc_iclkdiv == CLKDIVR_20M)
551 sdp = &ncp_sync_data_20M[0];
553 sdp = &ncp_sync_data_40M[0];
555 for (i = 0; sdp->max_period != 0; i ++, sdp ++)
557 if (period >= sdp->min_period && period <= sdp->max_period)
561 if (period != 0 && sdp->max_period == 0)
564 * NO proper period/offset found,
565 * Retry neg with the target.
567 ti->ti_maxsynch.period = 0;
568 ti->ti_maxsynch.offset = 0;
569 nti->nti_reg_syncr = 0;
570 nti->nti_reg_ackwidth = 0;
575 nti->nti_reg_syncr = (sdp->chip_period << SYNCR_PERS) |
576 (offset & SYNCR_OFFM);
577 nti->nti_reg_ackwidth = sdp->ack_width;
581 nsp_cr_write_1(bst, bsh, NSPR_SYNCR, nti->nti_reg_syncr);
582 nsp_cr_write_1(bst, bsh, NSPR_ACKWIDTH, nti->nti_reg_ackwidth);
587 nsp_targ_init(sc, ti, action)
588 struct nsp_softc *sc;
589 struct targ_info *ti;
592 struct nsp_targ_info *nti = (void *) ti;
594 if (action == SCSI_LOW_INFO_ALLOC || action == SCSI_LOW_INFO_REVOKE)
596 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
597 ti->ti_maxsynch.period = 100 / 4;
598 ti->ti_maxsynch.offset = 15;
599 nti->nti_reg_syncr = 0;
600 nti->nti_reg_ackwidth = 0;
606 nsp_start_timer(sc, time)
607 struct nsp_softc *sc;
610 bus_space_tag_t bst = sc->sc_iot;
611 bus_space_handle_t bsh = sc->sc_ioh;
614 nsp_cr_write_1(bst, bsh, NSPR_TIMERCNT, time);
617 /**************************************************************
618 * General probe attach
619 **************************************************************/
621 nspprobesubr(iot, ioh, dvcfg)
623 bus_space_handle_t ioh;
628 regv = bus_space_read_1(iot, ioh, nsp_fifosr);
629 if (regv < 0x11 || regv >= 0x20)
641 printf("%s: scsibus ", name);
647 struct nsp_softc *sc;
649 struct scsi_low_softc *slp = &sc->sc_sclow;
653 sc->sc_idbit = (1 << slp->sl_hostid);
654 slp->sl_flags |= HW_READ_PADDING;
655 slp->sl_funcs = &nspfuncs;
656 sc->sc_tmaxcnt = SCSI_LOW_MIN_TOUT * 1000 * 1000; /* default */
658 (void) scsi_low_attach(slp, 0, NSP_NTARGETS, NSP_NLUNS,
659 sizeof(struct nsp_targ_info), 0);
662 /**************************************************************
664 **************************************************************/
667 struct nsp_softc *sc;
669 bus_space_tag_t bst = sc->sc_iot;
670 bus_space_handle_t bsh = sc->sc_ioh;
673 nsp_cr_write_1(bst, bsh, NSPR_PTCLRR, PTCLRR_RSS_ACK | PTCLRR_PT);
674 count = bus_space_read_1(bst, bsh, nsp_datar);
675 count += (((u_int) bus_space_read_1(bst, bsh, nsp_datar)) << 8);
676 count += (((u_int) bus_space_read_1(bst, bsh, nsp_datar)) << 16);
681 nsp_request_count(sc)
682 struct nsp_softc *sc;
684 bus_space_tag_t bst = sc->sc_iot;
685 bus_space_handle_t bsh = sc->sc_ioh;
688 nsp_cr_write_1(bst, bsh, NSPR_PTCLRR, PTCLRR_RSS_REQ | PTCLRR_PT);
689 count = bus_space_read_1(bst, bsh, nsp_datar);
690 count += (((u_int) bus_space_read_1(bst, bsh, nsp_datar)) << 8);
691 count += (((u_int) bus_space_read_1(bst, bsh, nsp_datar)) << 16);
696 nsp_setup_fifo(sc, on, direction, datalen)
697 struct nsp_softc *sc;
704 sc->sc_suspendio = 0;
705 if (on == NSP_FIFO_OFF)
707 xfermode = XFERMR_IO8;
711 /* check if suspend io OK ? */
714 if (direction == SCSI_LOW_READ)
716 if ((nsp_io_control & NSP_READ_SUSPEND_IO) != 0 &&
717 (datalen % nsp_read_suspend_bytes) == 0)
718 sc->sc_suspendio = nsp_read_suspend_bytes;
722 if ((nsp_io_control & NSP_WRITE_SUSPEND_IO) != 0 &&
723 (datalen % nsp_write_suspend_bytes) == 0)
724 sc->sc_suspendio = nsp_write_suspend_bytes;
728 /* determine a transfer type */
729 if (datalen < DEV_BSIZE || (datalen & 3) != 0)
731 if (sc->sc_memh != NULL &&
732 (nsp_io_control & NSP_USE_MEMIO) != 0)
733 xfermode = XFERMR_XEN | XFERMR_MEM8;
735 xfermode = XFERMR_XEN | XFERMR_IO8;
739 if (sc->sc_memh != NULL &&
740 (nsp_io_control & NSP_USE_MEMIO) != 0)
741 xfermode = XFERMR_XEN | XFERMR_MEM32;
743 xfermode = XFERMR_XEN | XFERMR_IO32;
745 if (sc->sc_suspendio > 0)
746 xfermode |= XFERMR_FIFOEN;
750 sc->sc_xfermr = xfermode;
751 nsp_cr_write_1(sc->sc_iot, sc->sc_ioh, NSPR_XFERMR, sc->sc_xfermr);
756 struct nsp_softc *sc;
757 struct targ_info *ti;
759 struct scsi_low_softc *slp = &sc->sc_sclow;
760 struct slccb *cb = slp->sl_Qnexus;
763 sc->sc_dataout_timeout = 0;
764 slp->sl_flags &= ~HW_PDMASTART;
765 nsp_setup_fifo(sc, NSP_FIFO_OFF, SCSI_LOW_READ, 0);
766 if ((sc->sc_icr & SCIENR_FIFO) != 0)
768 sc->sc_icr &= ~SCIENR_FIFO;
769 nsp_cr_write_1(sc->sc_iot, sc->sc_ioh, NSPR_SCIENR, sc->sc_icr);
774 slp->sl_error |= PDMAERR;
778 if (ti->ti_phase == PH_DATA)
780 cnt = nsp_fifo_count(sc);
781 if (slp->sl_scp.scp_direction == SCSI_LOW_WRITE)
783 len = sc->sc_cnt - cnt;
784 if (sc->sc_cnt >= cnt &&
785 slp->sl_scp.scp_datalen + len <=
786 cb->ccb_scp.scp_datalen)
788 slp->sl_scp.scp_data -= len;
789 slp->sl_scp.scp_datalen += len;
793 slp->sl_error |= PDMAERR;
794 printf("%s len %x >= datalen %x\n",
796 len, slp->sl_scp.scp_datalen);
799 else if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
801 if (sc->sc_cnt != cnt ||
802 sc->sc_cnt > cb->ccb_scp.scp_datalen)
804 slp->sl_error |= PDMAERR;
805 printf("%s: data read count error %x != %x (%x)\n",
806 slp->sl_xname, sc->sc_cnt, cnt,
807 cb->ccb_scp.scp_datalen);
811 scsi_low_data_finish(slp);
816 printf("%s data phase miss\n", slp->sl_xname);
817 slp->sl_error |= PDMAERR;
821 #define RFIFO_CRIT 64
822 #define WFIFO_CRIT 32
825 nsp_data_padding(sc, direction, count)
826 struct nsp_softc *sc;
830 bus_space_tag_t bst = sc->sc_iot;
831 bus_space_handle_t bsh = sc->sc_ioh;
833 if (count > NSP_MAX_DATA_SIZE)
834 count = NSP_MAX_DATA_SIZE;
836 nsp_cr_write_1(bst, bsh, NSPR_XFERMR, XFERMR_XEN | XFERMR_IO8);
837 if (direction == SCSI_LOW_READ)
840 (void) bus_space_read_1(bst, bsh, nsp_fifodr);
845 (void) bus_space_write_1(bst, bsh, nsp_fifodr, 0);
847 nsp_cr_write_1(bst, bsh, NSPR_XFERMR, sc->sc_xfermr);
851 nsp_read_fifo(sc, suspendio)
852 struct nsp_softc *sc;
855 struct scsi_low_softc *slp = &sc->sc_sclow;
856 bus_space_tag_t bst = sc->sc_iot;
857 bus_space_handle_t bsh = sc->sc_ioh;
860 res = nsp_fifo_count(sc);
861 if (res == sc->sc_cnt)
865 if (res < sc->sc_cnt || res == (u_int) -1)
867 printf("%s: strange fifo ack count 0x%x < 0x%x\n",
868 slp->sl_xname, res, sc->sc_cnt);
871 #endif /* NSP_DEBUG */
873 res = res - sc->sc_cnt;
874 if (res > slp->sl_scp.scp_datalen)
876 if ((slp->sl_error & PDMAERR) == 0)
878 printf("%s: data overrun 0x%x > 0x%x\n",
879 slp->sl_xname, res, slp->sl_scp.scp_datalen);
882 slp->sl_error |= PDMAERR;
883 slp->sl_scp.scp_datalen = 0;
885 if ((slp->sl_flags & HW_READ_PADDING) == 0)
887 printf("%s: read padding required\n", slp->sl_xname);
891 nsp_data_padding(sc, SCSI_LOW_READ, res);
893 return 1; /* padding start */
896 if (suspendio > 0 && slp->sl_scp.scp_datalen >= suspendio)
899 if ((sc->sc_xfermr & (XFERMR_MEM32 | XFERMR_MEM8)) != 0)
901 if ((sc->sc_xfermr & XFERMR_MEM32) != 0)
904 bus_space_read_region_4(sc->sc_memt, sc->sc_memh, 0,
905 (u_int32_t *) slp->sl_scp.scp_data, res >> 2);
909 bus_space_read_region_1(sc->sc_memt, sc->sc_memh, 0,
910 (u_int8_t *) slp->sl_scp.scp_data, res);
915 if ((sc->sc_xfermr & XFERMR_IO32) != 0)
918 bus_space_read_multi_4(bst, bsh, nsp_fifodr,
919 (u_int32_t *) slp->sl_scp.scp_data, res >> 2);
923 bus_space_read_multi_1(bst, bsh, nsp_fifodr,
924 (u_int8_t *) slp->sl_scp.scp_data, res);
928 if (nsp_cr_read_1(bst, bsh, NSPR_PARITYR) & PARITYR_PE)
930 nsp_cr_write_1(bst, bsh, NSPR_PARITYR,
931 PARITYR_ENABLE | PARITYR_CLEAR);
932 scsi_low_assert_msg(slp, slp->sl_Tnexus, SCSI_LOW_MSG_ERROR, 1);
935 slp->sl_scp.scp_data += res;
936 slp->sl_scp.scp_datalen -= res;
942 nsp_write_fifo(sc, suspendio)
943 struct nsp_softc *sc;
946 struct scsi_low_softc *slp = &sc->sc_sclow;
947 bus_space_tag_t bst = sc->sc_iot;
948 bus_space_handle_t bsh = sc->sc_ioh;
955 if ((slp->sl_scp.scp_datalen % WFIFO_CRIT) != 0)
957 printf("%s: strange write length 0x%x\n",
958 slp->sl_xname, slp->sl_scp.scp_datalen);
960 #endif /* NSP_DEBUG */
961 res = slp->sl_scp.scp_datalen % suspendio;
972 if (res > slp->sl_scp.scp_datalen)
973 res = slp->sl_scp.scp_datalen;
975 /* XXX: reconfirm! */
976 stat = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON) & SCBUSMON_PHMASK;
977 if (stat != PHASE_DATAOUT)
980 if ((sc->sc_xfermr & (XFERMR_MEM32 | XFERMR_MEM8)) != 0)
982 if ((sc->sc_xfermr & XFERMR_MEM32) != 0)
984 bus_space_write_region_4(sc->sc_memt, sc->sc_memh, 0,
985 (u_int32_t *) slp->sl_scp.scp_data, res >> 2);
989 bus_space_write_region_1(sc->sc_memt, sc->sc_memh, 0,
990 (u_int8_t *) slp->sl_scp.scp_data, res);
995 if ((sc->sc_xfermr & XFERMR_IO32) != 0)
997 bus_space_write_multi_4(bst, bsh, nsp_fifodr,
998 (u_int32_t *) slp->sl_scp.scp_data, res >> 2);
1002 bus_space_write_multi_1(bst, bsh, nsp_fifodr,
1003 (u_int8_t *) slp->sl_scp.scp_data, res);
1007 slp->sl_scp.scp_datalen -= res;
1008 slp->sl_scp.scp_data += res;
1014 nsp_wait_interrupt(sc)
1015 struct nsp_softc *sc;
1017 bus_space_tag_t bst = sc->sc_iot;
1018 bus_space_handle_t bsh = sc->sc_ioh;
1022 for (tout = 0; tout < DEV_BSIZE / 10; tout ++)
1024 isrc = bus_space_read_1(bst, bsh, nsp_irqsr);
1025 if ((isrc & (IRQSR_SCSI | IRQSR_FIFO)) != 0)
1027 if ((isrc & IRQSR_FIFO) != 0)
1029 bus_space_write_1(bst, bsh,
1030 nsp_irqcr, IRQCR_FIFOCL);
1040 nsp_pio_read(sc, suspendio)
1041 struct nsp_softc *sc;
1044 struct scsi_low_softc *slp = &sc->sc_sclow;
1045 bus_space_tag_t bst = sc->sc_iot;
1046 bus_space_handle_t bsh = sc->sc_ioh;
1047 int tout, padding, datalen;
1048 u_int8_t stat, fstat;
1051 tout = sc->sc_tmaxcnt;
1052 slp->sl_flags |= HW_PDMASTART;
1053 datalen = slp->sl_scp.scp_datalen;
1058 stat = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
1059 if (stat == (u_int8_t) -1)
1062 /* out of data phase */
1063 if ((stat & SCBUSMON_PHMASK) != PHASE_DATAIN)
1065 nsp_read_fifo(sc, 0);
1070 fstat = bus_space_read_1(bst, bsh, nsp_fifosr);
1071 if ((fstat & FIFOSR_FULLEMP) != 0)
1073 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1075 bus_space_write_1(bst, bsh, nsp_irqcr,
1081 padding |= nsp_read_fifo(sc, suspendio);
1085 padding |= nsp_read_fifo(sc, 0);
1088 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1093 if (padding == 0 && slp->sl_scp.scp_datalen <= 0)
1096 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1104 printf("%s: nsp_pio_read: timeout\n", slp->sl_xname);
1110 if (slp->sl_scp.scp_datalen > 0 &&
1111 slp->sl_scp.scp_datalen > datalen - nsp_read_interrupt_bytes)
1113 if (nsp_wait_interrupt(sc) != 0)
1119 nsp_pio_write(sc, suspendio)
1120 struct nsp_softc *sc;
1123 struct scsi_low_softc *slp = &sc->sc_sclow;
1124 bus_space_tag_t bst = sc->sc_iot;
1125 bus_space_handle_t bsh = sc->sc_ioh;
1126 u_int rcount, acount;
1128 u_int8_t stat, fstat;
1130 tout = sc->sc_tmaxcnt;
1131 slp->sl_flags |= HW_PDMASTART;
1132 datalen = slp->sl_scp.scp_datalen;
1137 stat = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON) & SCBUSMON_PHMASK;
1138 if (stat != PHASE_DATAOUT)
1141 if (slp->sl_scp.scp_datalen <= 0)
1143 if (sc->sc_dataout_timeout == 0)
1144 sc->sc_dataout_timeout = SCSI_LOW_TIMEOUT_HZ;
1148 fstat = bus_space_read_1(bst, bsh, nsp_fifosr);
1149 if ((fstat & FIFOSR_FULLEMP) != 0)
1151 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1153 bus_space_write_1(bst, bsh, nsp_irqcr,
1160 * To avoid timeout of pcmcia bus
1161 * (not scsi bus!), we should check
1162 * the scsi device sends us request
1163 * signals, which means the scsi device
1164 * is ready to recieve data without
1167 if ((slp->sl_scp.scp_datalen % suspendio) == 0)
1170 * fill the nsp fifo, and waiting for
1173 nsp_write_fifo(sc, 0);
1178 * check the request singals.
1180 acount = nsp_fifo_count(sc);
1181 rcount = nsp_request_count(sc);
1182 if (rcount <= acount)
1184 nsp_write_fifo(sc, 0);
1186 nsp_statics.device_busy ++;
1187 #endif /* NSP_STATICS */
1191 nsp_write_fifo(sc, suspendio);
1193 nsp_statics.device_data_write ++;
1194 #endif /* NSP_STATICS */
1200 nsp_write_fifo(sc, 0);
1203 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1208 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1216 printf("%s: nsp_pio_write: timeout\n", slp->sl_xname);
1221 if (slp->sl_scp.scp_datalen > 0 &&
1222 slp->sl_scp.scp_datalen > datalen - nsp_write_interrupt_bytes)
1224 if (nsp_wait_interrupt(sc) != 0)
1230 nsp_negate_signal(sc, mask, s)
1231 struct nsp_softc *sc;
1235 struct scsi_low_softc *slp = &sc->sc_sclow;
1236 bus_space_tag_t bst = sc->sc_iot;
1237 bus_space_handle_t bsh = sc->sc_ioh;
1241 for (wc = 0; wc < NSP_DELAY_MAX / NSP_DELAY_INTERVAL; wc ++)
1243 regv = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
1244 if (regv == (u_int8_t) -1)
1246 if ((regv & mask) == 0)
1248 SCSI_LOW_DELAY(NSP_DELAY_INTERVAL);
1251 printf("%s: %s nsp_negate_signal timeout\n", slp->sl_xname, s);
1256 nsp_xfer(sc, buf, len, phase, clear_atn)
1257 struct nsp_softc *sc;
1263 bus_space_tag_t bst = sc->sc_iot;
1264 bus_space_handle_t bsh = sc->sc_ioh;
1267 for (ptr = 0; len > 0; len --, ptr ++)
1269 rv = nsp_expect_signal(sc, phase, SCBUSMON_REQ);
1273 if (len == 1 && clear_atn != 0)
1275 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR,
1276 SCBUSCR_ADIR | SCBUSCR_ACKEN);
1277 SCSI_LOW_DEASSERT_ATN(&sc->sc_sclow);
1280 if (phase & SCBUSMON_IO)
1282 buf[ptr] = nsp_cr_read_1(bst, bsh, NSPR_DATAACK);
1286 nsp_cr_write_1(bst, bsh, NSPR_DATAACK, buf[ptr]);
1288 nsp_negate_signal(sc, SCBUSMON_ACK, "xfer<ACK>");
1295 /**************************************************************
1296 * disconnect & reselect (HW low)
1297 **************************************************************/
1300 struct nsp_softc *sc;
1302 struct scsi_low_softc *slp = &sc->sc_sclow;
1303 bus_space_tag_t bst = sc->sc_iot;
1304 bus_space_handle_t bsh = sc->sc_ioh;
1305 struct targ_info *ti;
1309 sid = (u_int) nsp_cr_read_1(bst, bsh, NSPR_RESELR);
1310 sid &= ~sc->sc_idbit;
1312 if ((ti = scsi_low_reselected(slp, sid)) == NULL)
1315 nsp_negate_signal(sc, SCBUSMON_SEL, "reselect<SEL>");
1317 cr = nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR);
1318 cr &= ~(SCBUSCR_BSY | SCBUSCR_ATN);
1319 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1320 cr |= SCBUSCR_ADIR | SCBUSCR_ACKEN;
1321 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1324 nsp_statics.reselect ++;
1325 #endif /* NSP_STATCIS */
1330 nsp_disconnected(sc, ti)
1331 struct nsp_softc *sc;
1332 struct targ_info *ti;
1334 struct scsi_low_softc *slp = &sc->sc_sclow;
1335 bus_space_tag_t bst = sc->sc_iot;
1336 bus_space_handle_t bsh = sc->sc_ioh;
1338 nsp_cr_write_1(bst, bsh, NSPR_PTCLRR, PTCLRR_PT | PTCLRR_ACK |
1339 PTCLRR_REQ | PTCLRR_HOST);
1340 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1342 sc->sc_icr &= ~SCIENR_FIFO;
1343 nsp_cr_write_1(bst, bsh, NSPR_SCIENR, sc->sc_icr);
1346 sc->sc_dataout_timeout = 0;
1348 nsp_statics.disconnect ++;
1349 #endif /* NSP_STATICS */
1350 scsi_low_disconnected(slp, ti);
1354 /**************************************************************
1356 **************************************************************/
1357 static void nsp_error __P((struct nsp_softc *, u_char *, u_int8_t, u_int8_t, u_int8_t));
1360 nsp_error(sc, s, isrc, ph, irqphs)
1361 struct nsp_softc *sc;
1363 u_int8_t isrc, ph, irqphs;
1365 struct scsi_low_softc *slp = &sc->sc_sclow;
1367 printf("%s: %s\n", slp->sl_xname, s);
1368 printf("%s: isrc 0x%x scmon 0x%x irqphs 0x%x\n",
1369 slp->sl_xname, (u_int) isrc, (u_int) ph, (u_int) irqphs);
1373 nsp_target_nexus_establish(sc)
1374 struct nsp_softc *sc;
1376 struct scsi_low_softc *slp = &sc->sc_sclow;
1377 bus_space_tag_t bst = sc->sc_iot;
1378 bus_space_handle_t bsh = sc->sc_ioh;
1379 struct targ_info *ti = slp->sl_Tnexus;
1380 struct nsp_targ_info *nti = (void *) ti;
1382 /* setup synch transfer registers */
1383 nsp_cr_write_1(bst, bsh, NSPR_SYNCR, nti->nti_reg_syncr);
1384 nsp_cr_write_1(bst, bsh, NSPR_ACKWIDTH, nti->nti_reg_ackwidth);
1386 /* setup pdma fifo (minimum) */
1387 nsp_setup_fifo(sc, NSP_FIFO_ON, SCSI_LOW_READ, 0);
1392 nsp_lun_nexus_establish(sc)
1393 struct nsp_softc *sc;
1400 nsp_ccb_nexus_establish(sc)
1401 struct nsp_softc *sc;
1403 struct scsi_low_softc *slp = &sc->sc_sclow;
1404 struct slccb *cb = slp->sl_Qnexus;
1406 sc->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
1408 /* setup pdma fifo */
1409 nsp_setup_fifo(sc, NSP_FIFO_ON,
1410 slp->sl_scp.scp_direction, slp->sl_scp.scp_datalen);
1412 if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
1414 if (sc->sc_suspendio > 0 &&
1415 (nsp_io_control & NSP_READ_FIFO_INTERRUPTS) != 0)
1417 sc->sc_icr |= SCIENR_FIFO;
1418 nsp_cr_write_1(sc->sc_iot, sc->sc_ioh,
1419 NSPR_SCIENR, sc->sc_icr);
1424 if (sc->sc_suspendio > 0 &&
1425 (nsp_io_control & NSP_WRITE_FIFO_INTERRUPTS) != 0)
1427 sc->sc_icr |= SCIENR_FIFO;
1428 nsp_cr_write_1(sc->sc_iot, sc->sc_ioh,
1429 NSPR_SCIENR, sc->sc_icr);
1436 nsp_phase_match(sc, phase, stat)
1437 struct nsp_softc *sc;
1441 struct scsi_low_softc *slp = &sc->sc_sclow;
1443 if ((stat & SCBUSMON_PHMASK) != phase)
1445 printf("%s: phase mismatch 0x%x != 0x%x\n",
1446 slp->sl_xname, (u_int) phase, (u_int) stat);
1450 if ((stat & SCBUSMON_REQ) == 0)
1460 struct nsp_softc *sc = arg;
1461 struct scsi_low_softc *slp = &sc->sc_sclow;
1462 bus_space_tag_t bst = sc->sc_iot;
1463 bus_space_handle_t bsh = sc->sc_ioh;
1464 struct targ_info *ti;
1465 struct physio_proc *pp;
1467 u_int derror, flags;
1469 u_int8_t isrc, ph, irqphs, cr, regv;
1471 /*******************************************
1473 *******************************************/
1474 if (slp->sl_flags & HW_INACTIVE)
1477 bus_space_write_1(bst, bsh, nsp_irqcr, IRQCR_IRQDIS);
1478 isrc = bus_space_read_1(bst, bsh, nsp_irqsr);
1479 if (isrc == (u_int8_t) -1 || (isrc & IRQSR_MASK) == 0)
1481 bus_space_write_1(bst, bsh, nsp_irqcr, 0);
1486 * Do not read an irqphs register if no scsi phase interrupt.
1487 * Unless, you should lose a scsi phase interrupt.
1489 ph = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
1490 if ((isrc & IRQSR_SCSI) != 0)
1492 irqphs = nsp_cr_read_1(bst, bsh, NSPR_IRQPHS);
1498 * timer interrupt handler (scsi vs timer interrupts)
1500 if (sc->sc_timer != 0)
1502 nsp_cr_write_1(bst, bsh, NSPR_TIMERCNT, 0);
1503 nsp_cr_write_1(bst, bsh, NSPR_TIMERCNT, 0);
1507 /* check a timer interrupt */
1509 if ((isrc & IRQSR_TIMER) != 0)
1511 if ((isrc & IRQSR_MASK) == IRQSR_TIMER && sc->sc_seltout == 0)
1513 bus_space_write_1(bst, bsh, nsp_irqcr, IRQCR_TIMERCL);
1516 regv |= IRQCR_TIMERCL;
1519 /* check a fifo interrupt */
1520 if ((isrc & IRQSR_FIFO) != 0)
1522 regv |= IRQCR_FIFOCL;
1525 /* OK. enable all interrupts */
1526 bus_space_write_1(bst, bsh, nsp_irqcr, regv);
1528 /*******************************************
1530 *******************************************/
1534 nsp_error(sc, "current status", isrc, ph, irqphs);
1535 scsi_low_print(slp, NULL);
1538 SCSI_LOW_DEBUGGER("nsp");
1541 #endif /* NSP_DEBUG */
1543 /*******************************************
1544 * Parse hardware SCSI irq reasons register
1545 *******************************************/
1546 if ((isrc & IRQSR_SCSI) != 0)
1548 if ((irqphs & IRQPHS_RST) != 0)
1550 scsi_low_restart(slp, SCSI_LOW_RESTART_SOFT,
1551 "bus reset (power off?)");
1555 if ((irqphs & IRQPHS_RSEL) != 0)
1557 bus_space_write_1(bst, bsh, nsp_irqcr, IRQCR_RESCL);
1558 if (nsp_reselected(sc) == EJUSTRETURN)
1562 if ((irqphs & (IRQPHS_PCHG | IRQPHS_LBF)) == 0)
1566 /*******************************************
1568 *******************************************/
1569 if ((ti = slp->sl_Tnexus) == NULL)
1571 /* unknown scsi phase changes */
1572 nsp_error(sc, "unknown scsi phase changes", isrc, ph, irqphs);
1576 /*******************************************
1577 * aribitration & selection
1578 *******************************************/
1579 switch (ti->ti_phase)
1582 if ((ph & SCBUSMON_BSY) == 0)
1584 if (sc->sc_seltout >= NSP_SELTIMEOUT)
1587 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, 0);
1588 return nsp_disconnected(sc, ti);
1591 nsp_start_timer(sc, NSP_TIMER_1MS);
1595 SCSI_LOW_SETUP_PHASE(ti, PH_SELECTED);
1596 nsphw_selection_done_and_expect_msgout(sc);
1600 if ((isrc & IRQSR_SCSI) == 0)
1603 nsp_target_nexus_establish(sc);
1607 if ((isrc & IRQSR_SCSI) == 0)
1610 nsp_target_nexus_establish(sc);
1611 if ((ph & SCBUSMON_PHMASK) != PHASE_MSGIN)
1613 printf("%s: unexpected phase after reselect\n",
1615 slp->sl_error |= FATALIO;
1616 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 1);
1622 if ((isrc & IRQSR_SCSI) != 0)
1624 if ((isrc & IRQSR_FIFO) != 0)
1626 if (NSP_IS_PHASE_DATA(ph) == 0)
1628 irqphs = (ph & IRQPHS_PHMASK);
1634 if ((isrc & IRQSR_SCSI) == 0)
1639 /*******************************************
1640 * data phase control
1641 *******************************************/
1642 if (slp->sl_flags & HW_PDMASTART)
1644 if ((isrc & IRQSR_SCSI) != 0 &&
1645 NSP_IS_IRQPHS_DATA(irqphs) == 0)
1647 if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
1648 nsp_pio_read(sc, 0);
1649 nsp_pdma_end(sc, ti);
1653 /*******************************************
1655 *******************************************/
1656 if (slp->sl_msgphase != 0 && (irqphs & IRQPHS_LBF) != 0)
1657 return nsp_disconnected(sc, ti);
1659 /* check unexpected bus free state */
1662 nsp_error(sc, "unexpected bus free", isrc, ph, irqphs);
1663 return nsp_disconnected(sc, ti);
1666 /* check normal scsi phase */
1667 switch (irqphs & IRQPHS_PHMASK)
1670 if (nsp_phase_match(sc, PHASE_CMD, ph) != 0)
1673 SCSI_LOW_SETUP_PHASE(ti, PH_CMD);
1674 if (scsi_low_cmd(slp, ti) != 0)
1676 scsi_low_attention(slp);
1679 nsp_cr_write_1(bst, bsh, NSPR_CMDCR, CMDCR_PTCLR);
1680 for (len = 0; len < slp->sl_scp.scp_cmdlen; len ++)
1681 nsp_cr_write_1(bst, bsh, NSPR_CMDDR,
1682 slp->sl_scp.scp_cmd[len]);
1684 nsp_cr_write_1(bst, bsh, NSPR_CMDCR, CMDCR_PTCLR | CMDCR_EXEC);
1687 case IRQPHS_DATAOUT:
1688 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1689 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_WRITE) != 0)
1691 scsi_low_attention(slp);
1694 pp = physio_proc_enter(bp);
1695 nsp_pio_write(sc, sc->sc_suspendio);
1696 physio_proc_leave(pp);
1700 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1701 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_READ) != 0)
1703 scsi_low_attention(slp);
1706 pp = physio_proc_enter(bp);
1707 nsp_pio_read(sc, sc->sc_suspendio);
1708 physio_proc_leave(pp);
1712 if (nsp_phase_match(sc, PHASE_STATUS, ph) != 0)
1715 SCSI_LOW_SETUP_PHASE(ti, PH_STAT);
1716 regv = nsp_cr_read_1(bst, bsh, NSPR_DATA);
1717 if (nsp_cr_read_1(bst, bsh, NSPR_PARITYR) & PARITYR_PE)
1719 nsp_cr_write_1(bst, bsh, NSPR_PARITYR,
1720 PARITYR_ENABLE | PARITYR_CLEAR);
1721 derror = SCSI_LOW_DATA_PE;
1727 cr = SCBUSCR_ACK | nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR);
1728 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1730 if (scsi_low_statusin(slp, ti, derror | regv) != 0)
1732 scsi_low_attention(slp);
1735 /* check REQ nagated */
1736 nsp_negate_signal(sc, SCBUSMON_REQ, "statin<REQ>");
1739 cr = nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR) & (~SCBUSCR_ACK);
1740 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1744 if (nsp_phase_match(sc, PHASE_MSGOUT, ph) != 0)
1747 #ifdef NSP_MSGOUT_SERIALIZE
1750 * NSP invoke interrupts only in the case of scsi phase changes,
1751 * therefore we should poll the scsi phase here to catch
1752 * the next "msg out" if exists (no scsi phase changes).
1756 SCSI_LOW_SETUP_PHASE(ti, PH_MSGOUT);
1757 flags = (ti->ti_ophase != ti->ti_phase) ?
1758 SCSI_LOW_MSGOUT_INIT : 0;
1759 len = scsi_low_msgout(slp, ti, flags);
1761 if (len > 1 && slp->sl_atten == 0)
1763 scsi_low_attention(slp);
1766 if (nsp_xfer(sc, ti->ti_msgoutstr, len, PHASE_MSGOUT,
1767 slp->sl_clear_atten) != 0)
1769 slp->sl_error |= FATALIO;
1770 nsp_error(sc, "MSGOUT: xfer short",
1774 /* catch a next signal */
1775 rv = nsp_expect_signal(sc, PHASE_MSGOUT, SCBUSMON_REQ);
1777 while (rv > 0 && len -- > 0);
1779 #else /* !NSP_MSGOUT_SERIALIZE */
1780 SCSI_LOW_SETUP_PHASE(ti, PH_MSGOUT);
1781 flags = SCSI_LOW_MSGOUT_UNIFY;
1782 if (ti->ti_ophase != ti->ti_phase)
1783 flags |= SCSI_LOW_MSGOUT_INIT;
1784 len = scsi_low_msgout(slp, ti, flags);
1786 if (len > 1 && slp->sl_atten == 0)
1788 scsi_low_attention(slp);
1791 if (nsp_xfer(sc, ti->ti_msgoutstr, len, PHASE_MSGOUT,
1792 slp->sl_clear_atten) != 0)
1794 nsp_error(sc, "MSGOUT: xfer short", isrc, ph, irqphs);
1797 #endif /* !NSP_MSGOUT_SERIALIZE */
1801 if (nsp_phase_match(sc, PHASE_MSGIN, ph) != 0)
1806 * NSP invoke interrupts only in the case of scsi phase changes,
1807 * therefore we should poll the scsi phase here to catch
1808 * the next "msg in" if exists (no scsi phase changes).
1812 SCSI_LOW_SETUP_PHASE(ti, PH_MSGIN);
1815 regv = nsp_cr_read_1(bst, bsh, NSPR_DATA);
1816 if (nsp_cr_read_1(bst, bsh, NSPR_PARITYR) & PARITYR_PE)
1818 nsp_cr_write_1(bst, bsh,
1820 PARITYR_ENABLE | PARITYR_CLEAR);
1821 derror = SCSI_LOW_DATA_PE;
1829 cr = nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR) | SCBUSCR_ACK;
1830 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1832 if (scsi_low_msgin(slp, ti, regv | derror) == 0)
1834 if (scsi_low_is_msgout_continue(ti, 0) != 0)
1836 scsi_low_attention(slp);
1840 /* check REQ nagated */
1841 nsp_negate_signal(sc, SCBUSMON_REQ, "msgin<REQ>");
1844 cr = nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR) & (~SCBUSCR_ACK);
1845 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1847 /* catch a next signal */
1848 rv = nsp_expect_signal(sc, PHASE_MSGIN, SCBUSMON_REQ);
1850 while (rv > 0 && len -- > 0);
1854 slp->sl_error |= FATALIO;
1855 nsp_error(sc, "unknown scsi phase", isrc, ph, irqphs);
1863 nsp_start_timer(sc, NSP_TIMER_1MS);
1870 struct nsp_softc *sc;
1872 struct scsi_low_softc *slp = &sc->sc_sclow;
1873 bus_space_tag_t iot = sc->sc_iot;
1874 bus_space_handle_t ioh = sc->sc_ioh;
1878 if (slp->sl_Tnexus == NULL)
1881 ph = nsp_cr_read_1(iot, ioh, NSPR_SCBUSMON);
1882 switch (ph & SCBUSMON_PHMASK)
1885 if (sc->sc_dataout_timeout == 0)
1888 /* check a fifo empty */
1889 regv = bus_space_read_1(iot, ioh, nsp_fifosr);
1890 if ((regv & FIFOSR_FULLEMP) == 0)
1892 bus_space_write_1(iot, ioh, nsp_irqcr, IRQCR_FIFOCL);
1894 /* check still requested */
1895 ph = nsp_cr_read_1(iot, ioh, NSPR_SCBUSMON);
1896 if ((ph & SCBUSMON_REQ) == 0)
1899 if ((-- sc->sc_dataout_timeout) > 0)
1902 slp->sl_error |= PDMAERR;
1903 if ((slp->sl_flags & HW_WRITE_PADDING) == 0)
1905 printf("%s: write padding required\n", slp->sl_xname);
1909 tout = NSP_DELAY_MAX;
1912 ph = nsp_cr_read_1(iot, ioh, NSPR_SCBUSMON);
1913 if ((ph & SCBUSMON_PHMASK) != PHASE_DATAOUT)
1915 regv = bus_space_read_1(iot, ioh, nsp_fifosr);
1916 if ((regv & FIFOSR_FULLEMP) == 0)
1922 bus_space_write_1(iot, ioh, nsp_irqcr, IRQCR_FIFOCL);
1923 nsp_data_padding(sc, SCSI_LOW_WRITE, 32);
1925 ph = nsp_cr_read_1(iot, ioh, NSPR_SCBUSMON);
1926 if ((ph & SCBUSMON_PHMASK) == PHASE_DATAOUT)
1927 sc->sc_dataout_timeout = SCSI_LOW_TIMEOUT_HZ;