1 /* $FreeBSD: src/sys/dev/nsp/nsp.c,v 1.1.2.6 2001/12/17 13:30:18 non Exp $ */
2 /* $NecBSD: nsp.c,v 1.21.12.6 2001/06/29 06:27:52 honda Exp $ */
7 #define NSP_IO_CONTROL_FLAGS \
8 (NSP_READ_SUSPEND_IO | NSP_WRITE_SUSPEND_IO | \
9 NSP_READ_FIFO_INTERRUPTS | NSP_WRITE_FIFO_INTERRUPTS | \
10 NSP_USE_MEMIO | NSP_WAIT_FOR_SELECT)
13 * Copyright (c) 1998, 1999, 2000, 2001
14 * NetBSD/pc98 porting staff. All rights reserved.
16 * Copyright (c) 1998, 1999, 2000, 2001
17 * Naofumi HONDA. All rights reserved.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
27 * 3. The name of the author may not be used to endorse or promote products
28 * derived from this software without specific prior written permission.
30 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
33 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
34 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
36 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
38 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
39 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 * POSSIBILITY OF SUCH DAMAGE.
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #if defined(__FreeBSD__) && __FreeBSD_version > 500001
49 #endif /* __ FreeBSD__ */
51 #include <sys/queue.h>
52 #include <sys/malloc.h>
53 #include <sys/errno.h>
56 #include <sys/device.h>
57 #include <machine/bus.h>
58 #include <machine/intr.h>
60 #include <dev/scsipi/scsi_all.h>
61 #include <dev/scsipi/scsipi_all.h>
62 #include <dev/scsipi/scsiconf.h>
63 #include <dev/scsipi/scsi_disk.h>
65 #include <machine/dvcfg.h>
66 #include <machine/physio_proc.h>
68 #include <i386/Cbus/dev/scsi_low.h>
69 #include <i386/Cbus/dev/nspreg.h>
70 #include <i386/Cbus/dev/nspvar.h>
71 #endif /* __NetBSD__ */
74 #include <machine/clock.h>
75 #include <machine/cpu.h>
76 #include <machine/bus_pio.h>
77 #include <machine/bus_memio.h>
78 #include <machine/bus.h>
80 #include <machine/dvcfg.h>
81 #include <machine/physio_proc.h>
83 #include <cam/scsi/scsi_low.h>
84 #include <dev/nsp/nspreg.h>
85 #include <dev/nsp/nspvar.h>
86 #endif /* __FreeBSD__ */
88 /***************************************************
90 ***************************************************/
91 /* DEVICE CONFIGURATION FLAGS (MINOR)
94 * 0x02 PARITY LINE OFF
95 * 0x04 IDENTIFY MSG OFF ( = single lun)
96 * 0x08 SYNC TRANSFER OFF
99 /***************************************************
101 ***************************************************/
102 #define NSP_NTARGETS 8
105 #define NSP_MAX_DATA_SIZE (64 * 1024)
106 #define NSP_SELTIMEOUT (200)
107 #define NSP_DELAY_MAX (2 * 1000 * 1000)
108 #define NSP_DELAY_INTERVAL (1)
109 #define NSP_TIMER_1MS (1000 / 51)
111 /***************************************************
113 ***************************************************/
116 #endif /* NSP_DEBUG */
120 int arbit_conflict_1;
121 int arbit_conflict_2;
122 int device_data_write;
126 int data_phase_bypass;
128 #endif /* NSP_STATICS */
130 /***************************************************
132 ***************************************************/
133 #define NSP_READ_SUSPEND_IO 0x0001
134 #define NSP_WRITE_SUSPEND_IO 0x0002
135 #define NSP_USE_MEMIO 0x0004
136 #define NSP_READ_FIFO_INTERRUPTS 0x0010
137 #define NSP_WRITE_FIFO_INTERRUPTS 0x0020
138 #define NSP_WAIT_FOR_SELECT 0x0100
140 u_int nsp_io_control = NSP_IO_CONTROL_FLAGS;
141 int nsp_read_suspend_bytes = DEV_BSIZE;
142 int nsp_write_suspend_bytes = DEV_BSIZE;
143 int nsp_read_interrupt_bytes = 4096;
144 int nsp_write_interrupt_bytes = 4096;
146 /***************************************************
148 ***************************************************/
149 extern struct cfdriver nsp_cd;
151 /**************************************************************
153 **************************************************************/
154 #define NSP_FIFO_ON 1
155 #define NSP_FIFO_OFF 0
156 static void nsp_pio_read __P((struct nsp_softc *, int));
157 static void nsp_pio_write __P((struct nsp_softc *, int));
158 static int nsp_xfer __P((struct nsp_softc *, u_int8_t *, int, int, int));
159 static int nsp_msg __P((struct nsp_softc *, struct targ_info *, u_int));
160 static int nsp_reselected __P((struct nsp_softc *));
161 static int nsp_disconnected __P((struct nsp_softc *, struct targ_info *));
162 static void nsp_pdma_end __P((struct nsp_softc *, struct targ_info *));
163 static void nsphw_init __P((struct nsp_softc *));
164 static int nsp_target_nexus_establish __P((struct nsp_softc *));
165 static int nsp_lun_nexus_establish __P((struct nsp_softc *));
166 static int nsp_ccb_nexus_establish __P((struct nsp_softc *));
167 static int nsp_world_start __P((struct nsp_softc *, int));
168 static int nsphw_start_selection __P((struct nsp_softc *sc, struct slccb *));
169 static void nsphw_bus_reset __P((struct nsp_softc *));
170 static void nsphw_attention __P((struct nsp_softc *));
171 static u_int nsp_fifo_count __P((struct nsp_softc *));
172 static u_int nsp_request_count __P((struct nsp_softc *));
173 static int nsp_negate_signal __P((struct nsp_softc *, u_int8_t, u_char *));
174 static int nsp_expect_signal __P((struct nsp_softc *, u_int8_t, u_int8_t));
175 static void nsp_start_timer __P((struct nsp_softc *, int));
176 static void nsp_setup_fifo __P((struct nsp_softc *, int, int, int));
177 static int nsp_targ_init __P((struct nsp_softc *, struct targ_info *, int));
178 static void nsphw_selection_done_and_expect_msgout __P((struct nsp_softc *));
179 static void nsp_data_padding __P((struct nsp_softc *, int, u_int));
180 static int nsp_timeout __P((struct nsp_softc *));
181 static int nsp_read_fifo __P((struct nsp_softc *, int));
182 static int nsp_write_fifo __P((struct nsp_softc *, int));
183 static int nsp_phase_match __P((struct nsp_softc *, u_int8_t, u_int8_t));
184 static int nsp_wait_interrupt __P((struct nsp_softc *));
186 struct scsi_low_funcs nspfuncs = {
187 SC_LOW_INIT_T nsp_world_start,
188 SC_LOW_BUSRST_T nsphw_bus_reset,
189 SC_LOW_TARG_INIT_T nsp_targ_init,
190 SC_LOW_LUN_INIT_T NULL,
192 SC_LOW_SELECT_T nsphw_start_selection,
193 SC_LOW_NEXUS_T nsp_lun_nexus_establish,
194 SC_LOW_NEXUS_T nsp_ccb_nexus_establish,
196 SC_LOW_ATTEN_T nsphw_attention,
197 SC_LOW_MSG_T nsp_msg,
199 SC_LOW_TIMEOUT_T nsp_timeout,
200 SC_LOW_POLL_T nspintr,
205 /****************************************************
207 ****************************************************/
208 static __inline u_int8_t nsp_cr_read_1 __P((bus_space_tag_t bst, bus_space_handle_t bsh, bus_addr_t ofs));
209 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));
211 static __inline u_int8_t
212 nsp_cr_read_1(bst, bsh, ofs)
214 bus_space_handle_t bsh;
218 bus_space_write_1(bst, bsh, nsp_idxr, ofs);
219 return bus_space_read_1(bst, bsh, nsp_datar);
223 nsp_cr_write_1(bst, bsh, ofs, va)
225 bus_space_handle_t bsh;
230 bus_space_write_1(bst, bsh, nsp_idxr, ofs);
231 bus_space_write_1(bst, bsh, nsp_datar, va);
235 nsp_expect_signal(sc, curphase, mask)
236 struct nsp_softc *sc;
237 u_int8_t curphase, mask;
239 struct scsi_low_softc *slp = &sc->sc_sclow;
240 bus_space_tag_t bst = sc->sc_iot;
241 bus_space_handle_t bsh = sc->sc_ioh;
245 for (wc = 0; wc < NSP_DELAY_MAX / NSP_DELAY_INTERVAL; wc ++)
247 ph = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
248 if (ph == (u_int8_t) -1)
251 isrc = bus_space_read_1(bst, bsh, nsp_irqsr);
252 if (isrc & IRQSR_SCSI)
255 if ((ph & mask) != 0 && (ph & SCBUSMON_PHMASK) == curphase)
258 SCSI_LOW_DELAY(NSP_DELAY_INTERVAL);
261 printf("%s: nsp_expect_signal timeout\n", slp->sl_xname);
267 struct nsp_softc *sc;
269 bus_space_tag_t bst = sc->sc_iot;
270 bus_space_handle_t bsh = sc->sc_ioh;
272 /* block all interrupts */
273 bus_space_write_1(bst, bsh, nsp_irqcr, IRQCR_ALLMASK);
275 /* setup SCSI interface */
276 bus_space_write_1(bst, bsh, nsp_ifselr, IFSELR_IFSEL);
278 nsp_cr_write_1(bst, bsh, NSPR_SCIENR, 0);
280 nsp_cr_write_1(bst, bsh, NSPR_XFERMR, XFERMR_IO8);
281 nsp_cr_write_1(bst, bsh, NSPR_CLKDIVR, sc->sc_iclkdiv);
283 nsp_cr_write_1(bst, bsh, NSPR_SCIENR, sc->sc_icr);
284 nsp_cr_write_1(bst, bsh, NSPR_PARITYR, sc->sc_parr);
285 nsp_cr_write_1(bst, bsh, NSPR_PTCLRR,
286 PTCLRR_ACK | PTCLRR_REQ | PTCLRR_HOST | PTCLRR_RSS);
288 /* setup fifo asic */
289 bus_space_write_1(bst, bsh, nsp_ifselr, IFSELR_REGSEL);
290 nsp_cr_write_1(bst, bsh, NSPR_TERMPWRC, 0);
291 if ((nsp_cr_read_1(bst, bsh, NSPR_OCR) & OCR_TERMPWRS) == 0)
292 nsp_cr_write_1(bst, bsh, NSPR_TERMPWRC, TERMPWRC_POWON);
294 nsp_cr_write_1(bst, bsh, NSPR_XFERMR, XFERMR_IO8);
295 nsp_cr_write_1(bst, bsh, NSPR_CLKDIVR, sc->sc_clkdiv);
296 nsp_cr_write_1(bst, bsh, NSPR_TIMERCNT, 0);
297 nsp_cr_write_1(bst, bsh, NSPR_TIMERCNT, 0);
299 nsp_cr_write_1(bst, bsh, NSPR_SYNCR, 0);
300 nsp_cr_write_1(bst, bsh, NSPR_ACKWIDTH, 0);
302 /* enable interrupts and ack them */
303 nsp_cr_write_1(bst, bsh, NSPR_SCIENR, sc->sc_icr);
304 bus_space_write_1(bst, bsh, nsp_irqcr, IRQSR_MASK);
306 nsp_setup_fifo(sc, NSP_FIFO_OFF, SCSI_LOW_READ, 0);
309 /****************************************************
311 ****************************************************/
314 struct nsp_softc *sc;
316 bus_space_tag_t bst = sc->sc_iot;
317 bus_space_handle_t bsh = sc->sc_ioh;
320 cr = nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR)/* & ~SCBUSCR_ACK */;
321 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr | SCBUSCR_ATN);
327 struct nsp_softc *sc;
329 bus_space_tag_t bst = sc->sc_iot;
330 bus_space_handle_t bsh = sc->sc_ioh;
333 bus_space_write_1(bst, bsh, nsp_irqcr, IRQCR_ALLMASK);
335 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, SCBUSCR_RST);
336 SCSI_LOW_DELAY(100 * 1000); /* 100ms */
337 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, 0);
338 for (i = 0; i < 5; i ++)
339 (void) nsp_cr_read_1(bst, bsh, NSPR_IRQPHS);
341 bus_space_write_1(bst, bsh, nsp_irqcr, IRQSR_MASK);
345 nsphw_selection_done_and_expect_msgout(sc)
346 struct nsp_softc *sc;
348 struct scsi_low_softc *slp = &sc->sc_sclow;
349 bus_space_tag_t bst = sc->sc_iot;
350 bus_space_handle_t bsh = sc->sc_ioh;
352 /* clear ack counter */
354 nsp_cr_write_1(bst, bsh, NSPR_PTCLRR, PTCLRR_PT | PTCLRR_ACK |
355 PTCLRR_REQ | PTCLRR_HOST);
357 /* deassert sel and assert atten */
359 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, sc->sc_busc);
361 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR,
362 sc->sc_busc | SCBUSCR_ADIR | SCBUSCR_ACKEN);
363 SCSI_LOW_ASSERT_ATN(slp);
367 nsphw_start_selection(sc, cb)
368 struct nsp_softc *sc;
371 struct scsi_low_softc *slp = &sc->sc_sclow;
372 bus_space_tag_t bst = sc->sc_iot;
373 bus_space_handle_t bsh = sc->sc_ioh;
374 struct targ_info *ti = cb->ti;
375 register u_int8_t arbs, ph;
378 wc = sc->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
379 sc->sc_dataout_timeout = 0;
383 ph = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
384 if (ph != SCBUSMON_FREE)
388 nsp_statics.arbit_conflict_1 ++;
389 #endif /* NSP_STATICS */
390 return SCSI_LOW_START_FAIL;
393 /* start arbitration */
394 nsp_cr_write_1(bst, bsh, NSPR_ARBITS, ARBITS_EXEC);
397 SCSI_LOW_SETUP_PHASE(ti, PH_ARBSTART);
400 /* XXX: what a stupid chip! */
401 arbs = nsp_cr_read_1(bst, bsh, NSPR_ARBITS);
404 while ((arbs & (ARBITS_WIN | ARBITS_FAIL)) == 0 && wc -- > 0);
406 if ((arbs & ARBITS_WIN) == 0)
408 nsp_cr_write_1(bst, bsh, NSPR_ARBITS, ARBITS_CLR);
410 nsp_statics.arbit_conflict_2 ++;
411 #endif /* NSP_STATICS */
412 return SCSI_LOW_START_FAIL;
415 /* assert select line */
416 SCSI_LOW_SETUP_PHASE(ti, PH_SELSTART);
417 scsi_low_arbit_win(slp);
421 nsp_cr_write_1(bst, bsh, NSPR_DATA,
422 sc->sc_idbit | (1 << ti->ti_id));
423 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR,
424 SCBUSCR_SEL | SCBUSCR_BSY | sc->sc_busc);
426 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, SCBUSCR_SEL |
427 SCBUSCR_BSY | SCBUSCR_DOUT | sc->sc_busc);
428 nsp_cr_write_1(bst, bsh, NSPR_ARBITS, ARBITS_CLR);
430 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR,
431 SCBUSCR_SEL | SCBUSCR_DOUT | sc->sc_busc);
434 if ((nsp_io_control & NSP_WAIT_FOR_SELECT) != 0)
436 #define NSP_FIRST_SEL_WAIT 300
437 #define NSP_SEL_CHECK_INTERVAL 10
439 /* wait for a selection response */
440 for (wc = 0; wc < NSP_FIRST_SEL_WAIT / NSP_SEL_CHECK_INTERVAL;
443 ph = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
444 if ((ph & SCBUSMON_BSY) == 0)
446 SCSI_LOW_DELAY(NSP_SEL_CHECK_INTERVAL);
451 ph = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
452 if ((ph & SCBUSMON_BSY) != 0)
454 nsphw_selection_done_and_expect_msgout(sc);
457 SCSI_LOW_SETUP_PHASE(ti, PH_SELECTED);
458 return SCSI_LOW_START_OK;
464 /* check a selection timeout */
465 nsp_start_timer(sc, NSP_TIMER_1MS);
467 return SCSI_LOW_START_OK;
471 nsp_world_start(sc, fdone)
472 struct nsp_softc *sc;
475 struct scsi_low_softc *slp = &sc->sc_sclow;
480 if ((slp->sl_cfgflags & CFG_NOATTEN) == 0)
481 sc->sc_busc = SCBUSCR_ATN;
485 if ((slp->sl_cfgflags & CFG_NOPARITY) == 0)
486 sc->sc_parr = PARITYR_ENABLE | PARITYR_CLEAR;
490 sc->sc_icr = (SCIENR_SCCHG | SCIENR_RESEL | SCIENR_RST);
493 scsi_low_bus_reset(slp);
495 SOFT_INTR_REQUIRED(slp);
499 struct ncp_synch_data {
506 static struct ncp_synch_data ncp_sync_data_40M[] = {
507 {0x0c,0x0c,0x1,0}, /* 20MB 50ns*/
508 {0x19,0x19,0x3,1}, /* 10MB 100ns*/
509 {0x1a,0x25,0x5,2}, /* 7.5MB 150ns*/
510 {0x26,0x32,0x7,3}, /* 5MB 200ns*/
514 static struct ncp_synch_data ncp_sync_data_20M[] = {
515 {0x19,0x19,0x1,0}, /* 10MB 100ns*/
516 {0x1a,0x25,0x2,0}, /* 7.5MB 150ns*/
517 {0x26,0x32,0x3,1}, /* 5MB 200ns*/
523 struct nsp_softc *sc;
524 struct targ_info *ti;
527 bus_space_tag_t bst = sc->sc_iot;
528 bus_space_handle_t bsh = sc->sc_ioh;
529 struct ncp_synch_data *sdp;
530 struct nsp_targ_info *nti = (void *) ti;
531 u_int period, offset;
534 if ((msg & SCSI_LOW_MSG_WIDE) != 0)
536 if (ti->ti_width != SCSI_LOW_BUS_WIDTH_8)
538 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
544 if ((msg & SCSI_LOW_MSG_SYNCH) == 0)
547 period = ti->ti_maxsynch.period;
548 offset = ti->ti_maxsynch.offset;
549 if (sc->sc_iclkdiv == CLKDIVR_20M)
550 sdp = &ncp_sync_data_20M[0];
552 sdp = &ncp_sync_data_40M[0];
554 for (i = 0; sdp->max_period != 0; i ++, sdp ++)
556 if (period >= sdp->min_period && period <= sdp->max_period)
560 if (period != 0 && sdp->max_period == 0)
563 * NO proper period/offset found,
564 * Retry neg with the target.
566 ti->ti_maxsynch.period = 0;
567 ti->ti_maxsynch.offset = 0;
568 nti->nti_reg_syncr = 0;
569 nti->nti_reg_ackwidth = 0;
574 nti->nti_reg_syncr = (sdp->chip_period << SYNCR_PERS) |
575 (offset & SYNCR_OFFM);
576 nti->nti_reg_ackwidth = sdp->ack_width;
580 nsp_cr_write_1(bst, bsh, NSPR_SYNCR, nti->nti_reg_syncr);
581 nsp_cr_write_1(bst, bsh, NSPR_ACKWIDTH, nti->nti_reg_ackwidth);
586 nsp_targ_init(sc, ti, action)
587 struct nsp_softc *sc;
588 struct targ_info *ti;
591 struct nsp_targ_info *nti = (void *) ti;
593 if (action == SCSI_LOW_INFO_ALLOC || action == SCSI_LOW_INFO_REVOKE)
595 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
596 ti->ti_maxsynch.period = 100 / 4;
597 ti->ti_maxsynch.offset = 15;
598 nti->nti_reg_syncr = 0;
599 nti->nti_reg_ackwidth = 0;
605 nsp_start_timer(sc, time)
606 struct nsp_softc *sc;
609 bus_space_tag_t bst = sc->sc_iot;
610 bus_space_handle_t bsh = sc->sc_ioh;
613 nsp_cr_write_1(bst, bsh, NSPR_TIMERCNT, time);
616 /**************************************************************
617 * General probe attach
618 **************************************************************/
620 nspprobesubr(iot, ioh, dvcfg)
622 bus_space_handle_t ioh;
627 regv = bus_space_read_1(iot, ioh, nsp_fifosr);
628 if (regv < 0x11 || regv >= 0x20)
640 printf("%s: scsibus ", name);
646 struct nsp_softc *sc;
648 struct scsi_low_softc *slp = &sc->sc_sclow;
652 sc->sc_idbit = (1 << slp->sl_hostid);
653 slp->sl_flags |= HW_READ_PADDING;
654 slp->sl_funcs = &nspfuncs;
655 sc->sc_tmaxcnt = SCSI_LOW_MIN_TOUT * 1000 * 1000; /* default */
657 (void) scsi_low_attach(slp, 0, NSP_NTARGETS, NSP_NLUNS,
658 sizeof(struct nsp_targ_info), 0);
661 /**************************************************************
663 **************************************************************/
666 struct nsp_softc *sc;
668 bus_space_tag_t bst = sc->sc_iot;
669 bus_space_handle_t bsh = sc->sc_ioh;
672 nsp_cr_write_1(bst, bsh, NSPR_PTCLRR, PTCLRR_RSS_ACK | PTCLRR_PT);
673 count = bus_space_read_1(bst, bsh, nsp_datar);
674 count += (((u_int) bus_space_read_1(bst, bsh, nsp_datar)) << 8);
675 count += (((u_int) bus_space_read_1(bst, bsh, nsp_datar)) << 16);
680 nsp_request_count(sc)
681 struct nsp_softc *sc;
683 bus_space_tag_t bst = sc->sc_iot;
684 bus_space_handle_t bsh = sc->sc_ioh;
687 nsp_cr_write_1(bst, bsh, NSPR_PTCLRR, PTCLRR_RSS_REQ | PTCLRR_PT);
688 count = bus_space_read_1(bst, bsh, nsp_datar);
689 count += (((u_int) bus_space_read_1(bst, bsh, nsp_datar)) << 8);
690 count += (((u_int) bus_space_read_1(bst, bsh, nsp_datar)) << 16);
695 nsp_setup_fifo(sc, on, direction, datalen)
696 struct nsp_softc *sc;
703 sc->sc_suspendio = 0;
704 if (on == NSP_FIFO_OFF)
706 xfermode = XFERMR_IO8;
710 /* check if suspend io OK ? */
713 if (direction == SCSI_LOW_READ)
715 if ((nsp_io_control & NSP_READ_SUSPEND_IO) != 0 &&
716 (datalen % nsp_read_suspend_bytes) == 0)
717 sc->sc_suspendio = nsp_read_suspend_bytes;
721 if ((nsp_io_control & NSP_WRITE_SUSPEND_IO) != 0 &&
722 (datalen % nsp_write_suspend_bytes) == 0)
723 sc->sc_suspendio = nsp_write_suspend_bytes;
727 /* determine a transfer type */
728 if (datalen < DEV_BSIZE || (datalen & 3) != 0)
730 if (sc->sc_memh != NULL &&
731 (nsp_io_control & NSP_USE_MEMIO) != 0)
732 xfermode = XFERMR_XEN | XFERMR_MEM8;
734 xfermode = XFERMR_XEN | XFERMR_IO8;
738 if (sc->sc_memh != NULL &&
739 (nsp_io_control & NSP_USE_MEMIO) != 0)
740 xfermode = XFERMR_XEN | XFERMR_MEM32;
742 xfermode = XFERMR_XEN | XFERMR_IO32;
744 if (sc->sc_suspendio > 0)
745 xfermode |= XFERMR_FIFOEN;
749 sc->sc_xfermr = xfermode;
750 nsp_cr_write_1(sc->sc_iot, sc->sc_ioh, NSPR_XFERMR, sc->sc_xfermr);
755 struct nsp_softc *sc;
756 struct targ_info *ti;
758 struct scsi_low_softc *slp = &sc->sc_sclow;
759 struct slccb *cb = slp->sl_Qnexus;
762 sc->sc_dataout_timeout = 0;
763 slp->sl_flags &= ~HW_PDMASTART;
764 nsp_setup_fifo(sc, NSP_FIFO_OFF, SCSI_LOW_READ, 0);
765 if ((sc->sc_icr & SCIENR_FIFO) != 0)
767 sc->sc_icr &= ~SCIENR_FIFO;
768 nsp_cr_write_1(sc->sc_iot, sc->sc_ioh, NSPR_SCIENR, sc->sc_icr);
773 slp->sl_error |= PDMAERR;
777 if (ti->ti_phase == PH_DATA)
779 cnt = nsp_fifo_count(sc);
780 if (slp->sl_scp.scp_direction == SCSI_LOW_WRITE)
782 len = sc->sc_cnt - cnt;
783 if (sc->sc_cnt >= cnt &&
784 slp->sl_scp.scp_datalen + len <=
785 cb->ccb_scp.scp_datalen)
787 slp->sl_scp.scp_data -= len;
788 slp->sl_scp.scp_datalen += len;
792 slp->sl_error |= PDMAERR;
793 printf("%s len %x >= datalen %x\n",
795 len, slp->sl_scp.scp_datalen);
798 else if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
800 if (sc->sc_cnt != cnt ||
801 sc->sc_cnt > cb->ccb_scp.scp_datalen)
803 slp->sl_error |= PDMAERR;
804 printf("%s: data read count error %x != %x (%x)\n",
805 slp->sl_xname, sc->sc_cnt, cnt,
806 cb->ccb_scp.scp_datalen);
810 scsi_low_data_finish(slp);
815 printf("%s data phase miss\n", slp->sl_xname);
816 slp->sl_error |= PDMAERR;
820 #define RFIFO_CRIT 64
821 #define WFIFO_CRIT 32
824 nsp_data_padding(sc, direction, count)
825 struct nsp_softc *sc;
829 bus_space_tag_t bst = sc->sc_iot;
830 bus_space_handle_t bsh = sc->sc_ioh;
832 if (count > NSP_MAX_DATA_SIZE)
833 count = NSP_MAX_DATA_SIZE;
835 nsp_cr_write_1(bst, bsh, NSPR_XFERMR, XFERMR_XEN | XFERMR_IO8);
836 if (direction == SCSI_LOW_READ)
839 (void) bus_space_read_1(bst, bsh, nsp_fifodr);
844 (void) bus_space_write_1(bst, bsh, nsp_fifodr, 0);
846 nsp_cr_write_1(bst, bsh, NSPR_XFERMR, sc->sc_xfermr);
850 nsp_read_fifo(sc, suspendio)
851 struct nsp_softc *sc;
854 struct scsi_low_softc *slp = &sc->sc_sclow;
855 bus_space_tag_t bst = sc->sc_iot;
856 bus_space_handle_t bsh = sc->sc_ioh;
859 res = nsp_fifo_count(sc);
860 if (res == sc->sc_cnt)
864 if (res < sc->sc_cnt || res == (u_int) -1)
866 printf("%s: strange fifo ack count 0x%x < 0x%x\n",
867 slp->sl_xname, res, sc->sc_cnt);
870 #endif /* NSP_DEBUG */
872 res = res - sc->sc_cnt;
873 if (res > slp->sl_scp.scp_datalen)
875 if ((slp->sl_error & PDMAERR) == 0)
877 printf("%s: data overrun 0x%x > 0x%x\n",
878 slp->sl_xname, res, slp->sl_scp.scp_datalen);
881 slp->sl_error |= PDMAERR;
882 slp->sl_scp.scp_datalen = 0;
884 if ((slp->sl_flags & HW_READ_PADDING) == 0)
886 printf("%s: read padding required\n", slp->sl_xname);
890 nsp_data_padding(sc, SCSI_LOW_READ, res);
892 return 1; /* padding start */
895 if (suspendio > 0 && slp->sl_scp.scp_datalen >= suspendio)
898 if ((sc->sc_xfermr & (XFERMR_MEM32 | XFERMR_MEM8)) != 0)
900 if ((sc->sc_xfermr & XFERMR_MEM32) != 0)
903 bus_space_read_region_4(sc->sc_memt, sc->sc_memh, 0,
904 (u_int32_t *) slp->sl_scp.scp_data, res >> 2);
908 bus_space_read_region_1(sc->sc_memt, sc->sc_memh, 0,
909 (u_int8_t *) slp->sl_scp.scp_data, res);
914 if ((sc->sc_xfermr & XFERMR_IO32) != 0)
917 bus_space_read_multi_4(bst, bsh, nsp_fifodr,
918 (u_int32_t *) slp->sl_scp.scp_data, res >> 2);
922 bus_space_read_multi_1(bst, bsh, nsp_fifodr,
923 (u_int8_t *) slp->sl_scp.scp_data, res);
927 if (nsp_cr_read_1(bst, bsh, NSPR_PARITYR) & PARITYR_PE)
929 nsp_cr_write_1(bst, bsh, NSPR_PARITYR,
930 PARITYR_ENABLE | PARITYR_CLEAR);
931 scsi_low_assert_msg(slp, slp->sl_Tnexus, SCSI_LOW_MSG_ERROR, 1);
934 slp->sl_scp.scp_data += res;
935 slp->sl_scp.scp_datalen -= res;
941 nsp_write_fifo(sc, suspendio)
942 struct nsp_softc *sc;
945 struct scsi_low_softc *slp = &sc->sc_sclow;
946 bus_space_tag_t bst = sc->sc_iot;
947 bus_space_handle_t bsh = sc->sc_ioh;
949 register u_int8_t stat;
954 if ((slp->sl_scp.scp_datalen % WFIFO_CRIT) != 0)
956 printf("%s: strange write length 0x%x\n",
957 slp->sl_xname, slp->sl_scp.scp_datalen);
959 #endif /* NSP_DEBUG */
960 res = slp->sl_scp.scp_datalen % suspendio;
971 if (res > slp->sl_scp.scp_datalen)
972 res = slp->sl_scp.scp_datalen;
974 /* XXX: reconfirm! */
975 stat = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON) & SCBUSMON_PHMASK;
976 if (stat != PHASE_DATAOUT)
979 if ((sc->sc_xfermr & (XFERMR_MEM32 | XFERMR_MEM8)) != 0)
981 if ((sc->sc_xfermr & XFERMR_MEM32) != 0)
983 bus_space_write_region_4(sc->sc_memt, sc->sc_memh, 0,
984 (u_int32_t *) slp->sl_scp.scp_data, res >> 2);
988 bus_space_write_region_1(sc->sc_memt, sc->sc_memh, 0,
989 (u_int8_t *) slp->sl_scp.scp_data, res);
994 if ((sc->sc_xfermr & XFERMR_IO32) != 0)
996 bus_space_write_multi_4(bst, bsh, nsp_fifodr,
997 (u_int32_t *) slp->sl_scp.scp_data, res >> 2);
1001 bus_space_write_multi_1(bst, bsh, nsp_fifodr,
1002 (u_int8_t *) slp->sl_scp.scp_data, res);
1006 slp->sl_scp.scp_datalen -= res;
1007 slp->sl_scp.scp_data += res;
1013 nsp_wait_interrupt(sc)
1014 struct nsp_softc *sc;
1016 bus_space_tag_t bst = sc->sc_iot;
1017 bus_space_handle_t bsh = sc->sc_ioh;
1019 register u_int8_t isrc;
1021 for (tout = 0; tout < DEV_BSIZE / 10; tout ++)
1023 isrc = bus_space_read_1(bst, bsh, nsp_irqsr);
1024 if ((isrc & (IRQSR_SCSI | IRQSR_FIFO)) != 0)
1026 if ((isrc & IRQSR_FIFO) != 0)
1028 bus_space_write_1(bst, bsh,
1029 nsp_irqcr, IRQCR_FIFOCL);
1039 nsp_pio_read(sc, suspendio)
1040 struct nsp_softc *sc;
1043 struct scsi_low_softc *slp = &sc->sc_sclow;
1044 bus_space_tag_t bst = sc->sc_iot;
1045 bus_space_handle_t bsh = sc->sc_ioh;
1046 int tout, padding, datalen;
1047 register u_int8_t stat, fstat;
1050 tout = sc->sc_tmaxcnt;
1051 slp->sl_flags |= HW_PDMASTART;
1052 datalen = slp->sl_scp.scp_datalen;
1057 stat = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
1058 if (stat == (u_int8_t) -1)
1061 /* out of data phase */
1062 if ((stat & SCBUSMON_PHMASK) != PHASE_DATAIN)
1064 nsp_read_fifo(sc, 0);
1069 fstat = bus_space_read_1(bst, bsh, nsp_fifosr);
1070 if ((fstat & FIFOSR_FULLEMP) != 0)
1072 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1074 bus_space_write_1(bst, bsh, nsp_irqcr,
1080 padding |= nsp_read_fifo(sc, suspendio);
1084 padding |= nsp_read_fifo(sc, 0);
1087 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1092 if (padding == 0 && slp->sl_scp.scp_datalen <= 0)
1095 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1103 printf("%s: nsp_pio_read: timeout\n", slp->sl_xname);
1109 if (slp->sl_scp.scp_datalen > 0 &&
1110 slp->sl_scp.scp_datalen > datalen - nsp_read_interrupt_bytes)
1112 if (nsp_wait_interrupt(sc) != 0)
1118 nsp_pio_write(sc, suspendio)
1119 struct nsp_softc *sc;
1122 struct scsi_low_softc *slp = &sc->sc_sclow;
1123 bus_space_tag_t bst = sc->sc_iot;
1124 bus_space_handle_t bsh = sc->sc_ioh;
1125 u_int rcount, acount;
1127 register u_int8_t stat, fstat;
1129 tout = sc->sc_tmaxcnt;
1130 slp->sl_flags |= HW_PDMASTART;
1131 datalen = slp->sl_scp.scp_datalen;
1136 stat = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON) & SCBUSMON_PHMASK;
1137 if (stat != PHASE_DATAOUT)
1140 if (slp->sl_scp.scp_datalen <= 0)
1142 if (sc->sc_dataout_timeout == 0)
1143 sc->sc_dataout_timeout = SCSI_LOW_TIMEOUT_HZ;
1147 fstat = bus_space_read_1(bst, bsh, nsp_fifosr);
1148 if ((fstat & FIFOSR_FULLEMP) != 0)
1150 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1152 bus_space_write_1(bst, bsh, nsp_irqcr,
1159 * To avoid timeout of pcmcia bus
1160 * (not scsi bus!), we should check
1161 * the scsi device sends us request
1162 * signals, which means the scsi device
1163 * is ready to recieve data without
1166 if ((slp->sl_scp.scp_datalen % suspendio) == 0)
1169 * fill the nsp fifo, and waiting for
1172 nsp_write_fifo(sc, 0);
1177 * check the request singals.
1179 acount = nsp_fifo_count(sc);
1180 rcount = nsp_request_count(sc);
1181 if (rcount <= acount)
1183 nsp_write_fifo(sc, 0);
1185 nsp_statics.device_busy ++;
1186 #endif /* NSP_STATICS */
1190 nsp_write_fifo(sc, suspendio);
1192 nsp_statics.device_data_write ++;
1193 #endif /* NSP_STATICS */
1199 nsp_write_fifo(sc, 0);
1202 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1207 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1215 printf("%s: nsp_pio_write: timeout\n", slp->sl_xname);
1220 if (slp->sl_scp.scp_datalen > 0 &&
1221 slp->sl_scp.scp_datalen > datalen - nsp_write_interrupt_bytes)
1223 if (nsp_wait_interrupt(sc) != 0)
1229 nsp_negate_signal(sc, mask, s)
1230 struct nsp_softc *sc;
1234 struct scsi_low_softc *slp = &sc->sc_sclow;
1235 bus_space_tag_t bst = sc->sc_iot;
1236 bus_space_handle_t bsh = sc->sc_ioh;
1240 for (wc = 0; wc < NSP_DELAY_MAX / NSP_DELAY_INTERVAL; wc ++)
1242 regv = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
1243 if (regv == (u_int8_t) -1)
1245 if ((regv & mask) == 0)
1247 SCSI_LOW_DELAY(NSP_DELAY_INTERVAL);
1250 printf("%s: %s nsp_negate_signal timeout\n", slp->sl_xname, s);
1255 nsp_xfer(sc, buf, len, phase, clear_atn)
1256 struct nsp_softc *sc;
1262 bus_space_tag_t bst = sc->sc_iot;
1263 bus_space_handle_t bsh = sc->sc_ioh;
1266 for (ptr = 0; len > 0; len --, ptr ++)
1268 rv = nsp_expect_signal(sc, phase, SCBUSMON_REQ);
1272 if (len == 1 && clear_atn != 0)
1274 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR,
1275 SCBUSCR_ADIR | SCBUSCR_ACKEN);
1276 SCSI_LOW_DEASSERT_ATN(&sc->sc_sclow);
1279 if (phase & SCBUSMON_IO)
1281 buf[ptr] = nsp_cr_read_1(bst, bsh, NSPR_DATAACK);
1285 nsp_cr_write_1(bst, bsh, NSPR_DATAACK, buf[ptr]);
1287 nsp_negate_signal(sc, SCBUSMON_ACK, "xfer<ACK>");
1294 /**************************************************************
1295 * disconnect & reselect (HW low)
1296 **************************************************************/
1299 struct nsp_softc *sc;
1301 struct scsi_low_softc *slp = &sc->sc_sclow;
1302 bus_space_tag_t bst = sc->sc_iot;
1303 bus_space_handle_t bsh = sc->sc_ioh;
1304 struct targ_info *ti;
1308 sid = (u_int) nsp_cr_read_1(bst, bsh, NSPR_RESELR);
1309 sid &= ~sc->sc_idbit;
1311 if ((ti = scsi_low_reselected(slp, sid)) == NULL)
1314 nsp_negate_signal(sc, SCBUSMON_SEL, "reselect<SEL>");
1316 cr = nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR);
1317 cr &= ~(SCBUSCR_BSY | SCBUSCR_ATN);
1318 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1319 cr |= SCBUSCR_ADIR | SCBUSCR_ACKEN;
1320 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1323 nsp_statics.reselect ++;
1324 #endif /* NSP_STATCIS */
1329 nsp_disconnected(sc, ti)
1330 struct nsp_softc *sc;
1331 struct targ_info *ti;
1333 struct scsi_low_softc *slp = &sc->sc_sclow;
1334 bus_space_tag_t bst = sc->sc_iot;
1335 bus_space_handle_t bsh = sc->sc_ioh;
1337 nsp_cr_write_1(bst, bsh, NSPR_PTCLRR, PTCLRR_PT | PTCLRR_ACK |
1338 PTCLRR_REQ | PTCLRR_HOST);
1339 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1341 sc->sc_icr &= ~SCIENR_FIFO;
1342 nsp_cr_write_1(bst, bsh, NSPR_SCIENR, sc->sc_icr);
1345 sc->sc_dataout_timeout = 0;
1347 nsp_statics.disconnect ++;
1348 #endif /* NSP_STATICS */
1349 scsi_low_disconnected(slp, ti);
1353 /**************************************************************
1355 **************************************************************/
1356 static void nsp_error __P((struct nsp_softc *, u_char *, u_int8_t, u_int8_t, u_int8_t));
1359 nsp_error(sc, s, isrc, ph, irqphs)
1360 struct nsp_softc *sc;
1362 u_int8_t isrc, ph, irqphs;
1364 struct scsi_low_softc *slp = &sc->sc_sclow;
1366 printf("%s: %s\n", slp->sl_xname, s);
1367 printf("%s: isrc 0x%x scmon 0x%x irqphs 0x%x\n",
1368 slp->sl_xname, (u_int) isrc, (u_int) ph, (u_int) irqphs);
1372 nsp_target_nexus_establish(sc)
1373 struct nsp_softc *sc;
1375 struct scsi_low_softc *slp = &sc->sc_sclow;
1376 bus_space_tag_t bst = sc->sc_iot;
1377 bus_space_handle_t bsh = sc->sc_ioh;
1378 struct targ_info *ti = slp->sl_Tnexus;
1379 struct nsp_targ_info *nti = (void *) ti;
1381 /* setup synch transfer registers */
1382 nsp_cr_write_1(bst, bsh, NSPR_SYNCR, nti->nti_reg_syncr);
1383 nsp_cr_write_1(bst, bsh, NSPR_ACKWIDTH, nti->nti_reg_ackwidth);
1385 /* setup pdma fifo (minimum) */
1386 nsp_setup_fifo(sc, NSP_FIFO_ON, SCSI_LOW_READ, 0);
1391 nsp_lun_nexus_establish(sc)
1392 struct nsp_softc *sc;
1399 nsp_ccb_nexus_establish(sc)
1400 struct nsp_softc *sc;
1402 struct scsi_low_softc *slp = &sc->sc_sclow;
1403 struct slccb *cb = slp->sl_Qnexus;
1405 sc->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
1407 /* setup pdma fifo */
1408 nsp_setup_fifo(sc, NSP_FIFO_ON,
1409 slp->sl_scp.scp_direction, slp->sl_scp.scp_datalen);
1411 if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
1413 if (sc->sc_suspendio > 0 &&
1414 (nsp_io_control & NSP_READ_FIFO_INTERRUPTS) != 0)
1416 sc->sc_icr |= SCIENR_FIFO;
1417 nsp_cr_write_1(sc->sc_iot, sc->sc_ioh,
1418 NSPR_SCIENR, sc->sc_icr);
1423 if (sc->sc_suspendio > 0 &&
1424 (nsp_io_control & NSP_WRITE_FIFO_INTERRUPTS) != 0)
1426 sc->sc_icr |= SCIENR_FIFO;
1427 nsp_cr_write_1(sc->sc_iot, sc->sc_ioh,
1428 NSPR_SCIENR, sc->sc_icr);
1435 nsp_phase_match(sc, phase, stat)
1436 struct nsp_softc *sc;
1440 struct scsi_low_softc *slp = &sc->sc_sclow;
1442 if ((stat & SCBUSMON_PHMASK) != phase)
1444 printf("%s: phase mismatch 0x%x != 0x%x\n",
1445 slp->sl_xname, (u_int) phase, (u_int) stat);
1449 if ((stat & SCBUSMON_REQ) == 0)
1459 struct nsp_softc *sc = arg;
1460 struct scsi_low_softc *slp = &sc->sc_sclow;
1461 bus_space_tag_t bst = sc->sc_iot;
1462 bus_space_handle_t bsh = sc->sc_ioh;
1463 struct targ_info *ti;
1464 struct physio_proc *pp;
1466 u_int derror, flags;
1468 u_int8_t isrc, ph, irqphs, cr, regv;
1470 /*******************************************
1472 *******************************************/
1473 if (slp->sl_flags & HW_INACTIVE)
1476 bus_space_write_1(bst, bsh, nsp_irqcr, IRQCR_IRQDIS);
1477 isrc = bus_space_read_1(bst, bsh, nsp_irqsr);
1478 if (isrc == (u_int8_t) -1 || (isrc & IRQSR_MASK) == 0)
1480 bus_space_write_1(bst, bsh, nsp_irqcr, 0);
1485 * Do not read an irqphs register if no scsi phase interrupt.
1486 * Unless, you should lose a scsi phase interrupt.
1488 ph = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
1489 if ((isrc & IRQSR_SCSI) != 0)
1491 irqphs = nsp_cr_read_1(bst, bsh, NSPR_IRQPHS);
1497 * timer interrupt handler (scsi vs timer interrupts)
1499 if (sc->sc_timer != 0)
1501 nsp_cr_write_1(bst, bsh, NSPR_TIMERCNT, 0);
1502 nsp_cr_write_1(bst, bsh, NSPR_TIMERCNT, 0);
1506 /* check a timer interrupt */
1508 if ((isrc & IRQSR_TIMER) != 0)
1510 if ((isrc & IRQSR_MASK) == IRQSR_TIMER && sc->sc_seltout == 0)
1512 bus_space_write_1(bst, bsh, nsp_irqcr, IRQCR_TIMERCL);
1515 regv |= IRQCR_TIMERCL;
1518 /* check a fifo interrupt */
1519 if ((isrc & IRQSR_FIFO) != 0)
1521 regv |= IRQCR_FIFOCL;
1524 /* OK. enable all interrupts */
1525 bus_space_write_1(bst, bsh, nsp_irqcr, regv);
1527 /*******************************************
1529 *******************************************/
1533 nsp_error(sc, "current status", isrc, ph, irqphs);
1534 scsi_low_print(slp, NULL);
1537 SCSI_LOW_DEBUGGER("nsp");
1540 #endif /* NSP_DEBUG */
1542 /*******************************************
1543 * Parse hardware SCSI irq reasons register
1544 *******************************************/
1545 if ((isrc & IRQSR_SCSI) != 0)
1547 if ((irqphs & IRQPHS_RST) != 0)
1549 scsi_low_restart(slp, SCSI_LOW_RESTART_SOFT,
1550 "bus reset (power off?)");
1554 if ((irqphs & IRQPHS_RSEL) != 0)
1556 bus_space_write_1(bst, bsh, nsp_irqcr, IRQCR_RESCL);
1557 if (nsp_reselected(sc) == EJUSTRETURN)
1561 if ((irqphs & (IRQPHS_PCHG | IRQPHS_LBF)) == 0)
1565 /*******************************************
1567 *******************************************/
1568 if ((ti = slp->sl_Tnexus) == NULL)
1570 /* unknown scsi phase changes */
1571 nsp_error(sc, "unknown scsi phase changes", isrc, ph, irqphs);
1575 /*******************************************
1576 * aribitration & selection
1577 *******************************************/
1578 switch (ti->ti_phase)
1581 if ((ph & SCBUSMON_BSY) == 0)
1583 if (sc->sc_seltout >= NSP_SELTIMEOUT)
1586 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, 0);
1587 return nsp_disconnected(sc, ti);
1590 nsp_start_timer(sc, NSP_TIMER_1MS);
1594 SCSI_LOW_SETUP_PHASE(ti, PH_SELECTED);
1595 nsphw_selection_done_and_expect_msgout(sc);
1599 if ((isrc & IRQSR_SCSI) == 0)
1602 nsp_target_nexus_establish(sc);
1606 if ((isrc & IRQSR_SCSI) == 0)
1609 nsp_target_nexus_establish(sc);
1610 if ((ph & SCBUSMON_PHMASK) != PHASE_MSGIN)
1612 printf("%s: unexpected phase after reselect\n",
1614 slp->sl_error |= FATALIO;
1615 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 1);
1621 if ((isrc & IRQSR_SCSI) != 0)
1623 if ((isrc & IRQSR_FIFO) != 0)
1625 if (NSP_IS_PHASE_DATA(ph) == 0)
1627 irqphs = (ph & IRQPHS_PHMASK);
1633 if ((isrc & IRQSR_SCSI) == 0)
1638 /*******************************************
1639 * data phase control
1640 *******************************************/
1641 if (slp->sl_flags & HW_PDMASTART)
1643 if ((isrc & IRQSR_SCSI) != 0 &&
1644 NSP_IS_IRQPHS_DATA(irqphs) == 0)
1646 if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
1647 nsp_pio_read(sc, 0);
1648 nsp_pdma_end(sc, ti);
1652 /*******************************************
1654 *******************************************/
1655 if (slp->sl_msgphase != 0 && (irqphs & IRQPHS_LBF) != 0)
1656 return nsp_disconnected(sc, ti);
1658 /* check unexpected bus free state */
1661 nsp_error(sc, "unexpected bus free", isrc, ph, irqphs);
1662 return nsp_disconnected(sc, ti);
1665 /* check normal scsi phase */
1666 switch (irqphs & IRQPHS_PHMASK)
1669 if (nsp_phase_match(sc, PHASE_CMD, ph) != 0)
1672 SCSI_LOW_SETUP_PHASE(ti, PH_CMD);
1673 if (scsi_low_cmd(slp, ti) != 0)
1675 scsi_low_attention(slp);
1678 nsp_cr_write_1(bst, bsh, NSPR_CMDCR, CMDCR_PTCLR);
1679 for (len = 0; len < slp->sl_scp.scp_cmdlen; len ++)
1680 nsp_cr_write_1(bst, bsh, NSPR_CMDDR,
1681 slp->sl_scp.scp_cmd[len]);
1683 nsp_cr_write_1(bst, bsh, NSPR_CMDCR, CMDCR_PTCLR | CMDCR_EXEC);
1686 case IRQPHS_DATAOUT:
1687 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1688 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_WRITE) != 0)
1690 scsi_low_attention(slp);
1693 pp = physio_proc_enter(bp);
1694 nsp_pio_write(sc, sc->sc_suspendio);
1695 physio_proc_leave(pp);
1699 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1700 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_READ) != 0)
1702 scsi_low_attention(slp);
1705 pp = physio_proc_enter(bp);
1706 nsp_pio_read(sc, sc->sc_suspendio);
1707 physio_proc_leave(pp);
1711 if (nsp_phase_match(sc, PHASE_STATUS, ph) != 0)
1714 SCSI_LOW_SETUP_PHASE(ti, PH_STAT);
1715 regv = nsp_cr_read_1(bst, bsh, NSPR_DATA);
1716 if (nsp_cr_read_1(bst, bsh, NSPR_PARITYR) & PARITYR_PE)
1718 nsp_cr_write_1(bst, bsh, NSPR_PARITYR,
1719 PARITYR_ENABLE | PARITYR_CLEAR);
1720 derror = SCSI_LOW_DATA_PE;
1726 cr = SCBUSCR_ACK | nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR);
1727 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1729 if (scsi_low_statusin(slp, ti, derror | regv) != 0)
1731 scsi_low_attention(slp);
1734 /* check REQ nagated */
1735 nsp_negate_signal(sc, SCBUSMON_REQ, "statin<REQ>");
1738 cr = nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR) & (~SCBUSCR_ACK);
1739 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1743 if (nsp_phase_match(sc, PHASE_MSGOUT, ph) != 0)
1746 #ifdef NSP_MSGOUT_SERIALIZE
1749 * NSP invoke interrupts only in the case of scsi phase changes,
1750 * therefore we should poll the scsi phase here to catch
1751 * the next "msg out" if exists (no scsi phase changes).
1755 SCSI_LOW_SETUP_PHASE(ti, PH_MSGOUT);
1756 flags = (ti->ti_ophase != ti->ti_phase) ?
1757 SCSI_LOW_MSGOUT_INIT : 0;
1758 len = scsi_low_msgout(slp, ti, flags);
1760 if (len > 1 && slp->sl_atten == 0)
1762 scsi_low_attention(slp);
1765 if (nsp_xfer(sc, ti->ti_msgoutstr, len, PHASE_MSGOUT,
1766 slp->sl_clear_atten) != 0)
1768 slp->sl_error |= FATALIO;
1769 nsp_error(sc, "MSGOUT: xfer short",
1773 /* catch a next signal */
1774 rv = nsp_expect_signal(sc, PHASE_MSGOUT, SCBUSMON_REQ);
1776 while (rv > 0 && len -- > 0);
1778 #else /* !NSP_MSGOUT_SERIALIZE */
1779 SCSI_LOW_SETUP_PHASE(ti, PH_MSGOUT);
1780 flags = SCSI_LOW_MSGOUT_UNIFY;
1781 if (ti->ti_ophase != ti->ti_phase)
1782 flags |= SCSI_LOW_MSGOUT_INIT;
1783 len = scsi_low_msgout(slp, ti, flags);
1785 if (len > 1 && slp->sl_atten == 0)
1787 scsi_low_attention(slp);
1790 if (nsp_xfer(sc, ti->ti_msgoutstr, len, PHASE_MSGOUT,
1791 slp->sl_clear_atten) != 0)
1793 nsp_error(sc, "MSGOUT: xfer short", isrc, ph, irqphs);
1796 #endif /* !NSP_MSGOUT_SERIALIZE */
1800 if (nsp_phase_match(sc, PHASE_MSGIN, ph) != 0)
1805 * NSP invoke interrupts only in the case of scsi phase changes,
1806 * therefore we should poll the scsi phase here to catch
1807 * the next "msg in" if exists (no scsi phase changes).
1811 SCSI_LOW_SETUP_PHASE(ti, PH_MSGIN);
1814 regv = nsp_cr_read_1(bst, bsh, NSPR_DATA);
1815 if (nsp_cr_read_1(bst, bsh, NSPR_PARITYR) & PARITYR_PE)
1817 nsp_cr_write_1(bst, bsh,
1819 PARITYR_ENABLE | PARITYR_CLEAR);
1820 derror = SCSI_LOW_DATA_PE;
1828 cr = nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR) | SCBUSCR_ACK;
1829 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1831 if (scsi_low_msgin(slp, ti, regv | derror) == 0)
1833 if (scsi_low_is_msgout_continue(ti, 0) != 0)
1835 scsi_low_attention(slp);
1839 /* check REQ nagated */
1840 nsp_negate_signal(sc, SCBUSMON_REQ, "msgin<REQ>");
1843 cr = nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR) & (~SCBUSCR_ACK);
1844 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1846 /* catch a next signal */
1847 rv = nsp_expect_signal(sc, PHASE_MSGIN, SCBUSMON_REQ);
1849 while (rv > 0 && len -- > 0);
1853 slp->sl_error |= FATALIO;
1854 nsp_error(sc, "unknown scsi phase", isrc, ph, irqphs);
1862 nsp_start_timer(sc, NSP_TIMER_1MS);
1869 struct nsp_softc *sc;
1871 struct scsi_low_softc *slp = &sc->sc_sclow;
1872 bus_space_tag_t iot = sc->sc_iot;
1873 bus_space_handle_t ioh = sc->sc_ioh;
1877 if (slp->sl_Tnexus == NULL)
1880 ph = nsp_cr_read_1(iot, ioh, NSPR_SCBUSMON);
1881 switch (ph & SCBUSMON_PHMASK)
1884 if (sc->sc_dataout_timeout == 0)
1887 /* check a fifo empty */
1888 regv = bus_space_read_1(iot, ioh, nsp_fifosr);
1889 if ((regv & FIFOSR_FULLEMP) == 0)
1891 bus_space_write_1(iot, ioh, nsp_irqcr, IRQCR_FIFOCL);
1893 /* check still requested */
1894 ph = nsp_cr_read_1(iot, ioh, NSPR_SCBUSMON);
1895 if ((ph & SCBUSMON_REQ) == 0)
1898 if ((-- sc->sc_dataout_timeout) > 0)
1901 slp->sl_error |= PDMAERR;
1902 if ((slp->sl_flags & HW_WRITE_PADDING) == 0)
1904 printf("%s: write padding required\n", slp->sl_xname);
1908 tout = NSP_DELAY_MAX;
1911 ph = nsp_cr_read_1(iot, ioh, NSPR_SCBUSMON);
1912 if ((ph & SCBUSMON_PHMASK) != PHASE_DATAOUT)
1914 regv = bus_space_read_1(iot, ioh, nsp_fifosr);
1915 if ((regv & FIFOSR_FULLEMP) == 0)
1921 bus_space_write_1(iot, ioh, nsp_irqcr, IRQCR_FIFOCL);
1922 nsp_data_padding(sc, SCSI_LOW_WRITE, 32);
1924 ph = nsp_cr_read_1(iot, ioh, NSPR_SCBUSMON);
1925 if ((ph & SCBUSMON_PHMASK) == PHASE_DATAOUT)
1926 sc->sc_dataout_timeout = SCSI_LOW_TIMEOUT_HZ;