1 /* $NecBSD: ncr53c500.c,v 1.30.12.3 2001/06/26 07:31:41 honda Exp $ */
6 #define NCV_IO_CONTROL_FLAGS (0)
9 * [NetBSD for NEC PC-98 series]
10 * Copyright (c) 1995, 1996, 1997, 1998, 1999, 2000, 2001
11 * NetBSD/pc98 porting staff. All rights reserved.
12 * Copyright (c) 1995, 1996, 1997, 1998, 1999, 2000, 2001
13 * Naofumi HONDA. All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. The name of the author may not be used to endorse or promote products
24 * derived from this software without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
30 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
47 #include <sys/queue.h>
48 #include <sys/malloc.h>
49 #include <sys/errno.h>
52 #include <machine/cpu.h>
53 #include <machine/bus.h>
55 #include <compat/netbsd/dvcfg.h>
57 #include <cam/scsi/scsi_low.h>
59 #include <dev/ncv/ncr53c500reg.h>
60 #include <dev/ncv/ncr53c500hw.h>
61 #include <dev/ncv/ncr53c500var.h>
63 #include <dev/ncv/ncr53c500hwtab.h>
65 #define NCV_MAX_DATA_SIZE (64 * 1024)
66 #define NCV_DELAY_MAX (2 * 1000 * 1000)
67 #define NCV_DELAY_INTERVAL (1)
68 #define NCV_PADDING_SIZE (32)
70 /***************************************************
72 ***************************************************/
73 #define NCV_READ_INTERRUPTS_DRIVEN 0x0001
74 #define NCV_WRITE_INTERRUPTS_DRIVEN 0x0002
75 #define NCV_ENABLE_FAST_SCSI 0x0010
76 #define NCV_FAST_INTERRUPTS 0x0100
78 u_int ncv_io_control = NCV_IO_CONTROL_FLAGS;
79 int ncv_data_read_bytes = 4096;
80 int ncv_data_write_bytes = 4096;
82 /***************************************************
84 ***************************************************/
87 #endif /* NCV_DEBUG */
90 static struct ncv_statics {
94 #endif /* NCV_STATICS */
96 /***************************************************
98 ***************************************************/
99 extern struct cfdriver ncv_cd;
101 /**************************************************************
103 **************************************************************/
105 static void ncv_pio_read(struct ncv_softc *, u_int8_t *, u_int);
106 static void ncv_pio_write(struct ncv_softc *, u_int8_t *, u_int);
107 static int ncv_msg(struct ncv_softc *, struct targ_info *, u_int);
108 static int ncv_reselected(struct ncv_softc *);
109 static int ncv_disconnected(struct ncv_softc *, struct targ_info *);
111 static __inline void ncvhw_set_count(struct resource *, int);
112 static __inline u_int ncvhw_get_count(struct resource *);
113 static __inline void ncvhw_select_register_0(struct resource *, struct ncv_hw *);
114 static __inline void ncvhw_select_register_1(struct resource *, struct ncv_hw *);
115 static __inline void ncvhw_fpush(struct resource *, u_int8_t *, int);
117 static void ncv_pdma_end(struct ncv_softc *sc, struct targ_info *);
118 static int ncv_world_start(struct ncv_softc *, int);
119 static void ncvhw_bus_reset(struct ncv_softc *);
120 static void ncvhw_reset(struct resource *, struct ncv_hw *);
121 static int ncvhw_check(struct resource *, struct ncv_hw *);
122 static void ncvhw_init(struct resource *, struct ncv_hw *);
123 static int ncvhw_start_selection(struct ncv_softc *sc, struct slccb *);
124 static void ncvhw_attention(struct ncv_softc *);
125 static int ncv_ccb_nexus_establish(struct ncv_softc *);
126 static int ncv_lun_nexus_establish(struct ncv_softc *);
127 static int ncv_target_nexus_establish(struct ncv_softc *);
128 static int ncv_targ_init(struct ncv_softc *, struct targ_info *, int);
129 static int ncv_catch_intr(struct ncv_softc *);
130 #ifdef NCV_POWER_CONTROL
131 static int ncvhw_power(struct ncv_softc *, u_int);
132 #endif /* NCV_POWER_CONTROL */
133 static __inline void ncv_setup_and_start_pio(struct ncv_softc *, u_int);
135 struct scsi_low_funcs ncv_funcs = {
136 SC_LOW_INIT_T ncv_world_start,
137 SC_LOW_BUSRST_T ncvhw_bus_reset,
138 SC_LOW_TARG_INIT_T ncv_targ_init,
139 SC_LOW_LUN_INIT_T NULL,
141 SC_LOW_SELECT_T ncvhw_start_selection,
142 SC_LOW_NEXUS_T ncv_lun_nexus_establish,
143 SC_LOW_NEXUS_T ncv_ccb_nexus_establish,
145 SC_LOW_ATTEN_T ncvhw_attention,
146 SC_LOW_MSG_T ncv_msg,
148 SC_LOW_TIMEOUT_T NULL,
149 SC_LOW_POLL_T ncvintr,
151 NULL, /* SC_LOW_POWER_T ncvhw_power, */
154 /**************************************************************
156 **************************************************************/
158 ncvhw_select_register_0(struct resource *res, struct ncv_hw *hw)
161 bus_write_1(res, cr0_cfg4, hw->hw_cfg4);
165 ncvhw_select_register_1(struct resource *res, struct ncv_hw *hw)
168 bus_write_1(res, cr1_cfg5, hw->hw_cfg5);
172 ncvhw_fpush(struct resource *res, u_int8_t *buf, int len)
176 for (ptr = 0; ptr < len; ptr ++)
177 bus_write_1(res, cr0_sfifo, buf[ptr]);
181 ncvhw_set_count(struct resource *res, int count)
184 bus_write_1(res, cr0_tclsb, (u_int8_t) count);
185 bus_write_1(res, cr0_tcmsb, (u_int8_t) (count >> NBBY));
186 bus_write_1(res, cr0_tchsb, (u_int8_t) (count >> (NBBY * 2)));
189 static __inline u_int
190 ncvhw_get_count(struct resource *res)
194 count = (u_int) bus_read_1(res, cr0_tclsb);
195 count |= ((u_int) bus_read_1(res, cr0_tcmsb)) << NBBY;
196 count |= ((u_int) bus_read_1(res, cr0_tchsb)) << (NBBY * 2);
201 ncvhw_check(struct resource *res, struct ncv_hw *hw)
205 ncvhw_select_register_0(res, hw);
206 bus_write_1(res, cr0_cmd, CMD_NOP | CMD_DMA);
207 if (bus_read_1(res, cr0_cmd) != (CMD_NOP | CMD_DMA))
210 printf("ncv: cr0_cmd CMD_NOP|CMD_DMA failed\n");
211 #endif /* NCV_DEBUG */
215 bus_write_1(res, cr0_cmd, CMD_NOP);
216 if (bus_read_1(res, cr0_cmd) != CMD_NOP)
219 printf("ncv: cr0_cmd CMD_NOP failed\n");
220 #endif /* NCV_DEBUG */
225 ncvhw_reset(res, hw);
229 ncvhw_select_register_0(res, hw);
230 bus_write_1(res, cr0_cmd, CMD_FLUSH);
231 bus_write_1(res, cr0_cmd, CMD_RSTSCSI);
232 bus_write_1(res, cr0_cmd, CMD_NOP | CMD_DMA);
236 bus_read_1(res, cr0_stat);
237 stat = bus_read_1(res, cr0_istat);
240 if (((stat & INTR_SBR) == 0) ||
241 (bus_read_1(res, cr0_istat) & INTR_SBR))
244 printf("ncv: cr0_istat SCSI BUS RESET failed\n");
245 #endif /* NCV_DEBUG */
253 ncvhw_reset(struct resource *res, struct ncv_hw *hw)
256 ncvhw_select_register_0(res, hw);
258 /* dummy cmd twice */
259 bus_write_1(res, cr0_cmd, CMD_NOP);
260 bus_write_1(res, cr0_cmd, CMD_NOP);
263 bus_write_1(res, cr0_cmd, CMD_RSTCHIP);
265 /* again dummy cmd twice */
266 bus_write_1(res, cr0_cmd, CMD_NOP);
267 bus_write_1(res, cr0_cmd, CMD_NOP);
271 ncvhw_init(struct resource *res, struct ncv_hw *hw)
274 ncvhw_select_register_0(res, hw);
275 bus_write_1(res, cr0_clk, hw->hw_clk);
276 bus_write_1(res, cr0_srtout, SEL_TOUT);
277 bus_write_1(res, cr0_period, 0);
278 bus_write_1(res, cr0_offs, 0);
280 bus_write_1(res, cr0_cfg1, hw->hw_cfg1);
281 bus_write_1(res, cr0_cfg2, hw->hw_cfg2);
282 bus_write_1(res, cr0_cfg3, hw->hw_cfg3);
283 bus_write_1(res, cr0_tchsb, 0);
285 ncvhw_select_register_1(res, hw);
286 bus_write_1(res, cr1_fstat, 0x0);
287 bus_write_1(res, cr1_pflag, 0x0);
288 bus_write_1(res, cr1_atacmd, ATACMD_ENGAGE);
290 ncvhw_select_register_0(res, hw);
293 #ifdef NCV_POWER_CONTROL
295 ncvhw_power(sc, flags)
296 struct ncv_softc *sc;
299 struct scsi_low_softc *slp = &sc->sc_sclow;
300 struct resource *res = sc->port_res;
302 if (flags == SCSI_LOW_POWDOWN)
304 device_printf(slp->sl_dev, "power down\n");
305 ncvhw_select_register_1(res, &sc->sc_hw);
306 bus_write_1(res, cr1_atacmd, ATACMD_POWDOWN);
310 switch (sc->sc_rstep)
313 device_printf(slp->sl_dev, "resume step O\n");
314 ncvhw_select_register_1(res, &sc->sc_hw);
315 bus_write_1(res, cr1_atacmd, ATACMD_ENGAGE);
319 device_printf(slp->sl_dev, "resume step I\n");
320 ncvhw_reset(res, &sc->sc_hw);
321 ncvhw_init(res, &sc->sc_hw);
328 #endif /* NCV_POWER_CONTROL */
330 /**************************************************************
332 **************************************************************/
335 struct ncv_softc *sc;
338 bus_write_1(sc->port_res, cr0_cmd, CMD_SETATN);
344 struct ncv_softc *sc;
347 ncvhw_select_register_0(sc->port_res, &sc->sc_hw);
348 bus_write_1(sc->port_res, cr0_cmd, CMD_FLUSH);
349 bus_write_1(sc->port_res, cr0_cmd, CMD_RSTSCSI);
350 bus_write_1(sc->port_res, cr0_cmd, CMD_NOP | CMD_DMA);
354 ncvhw_start_selection(sc, cb)
355 struct ncv_softc *sc;
358 struct scsi_low_softc *slp = &sc->sc_sclow;
359 struct resource *res = sc->port_res;
360 struct targ_info *ti = cb->ti;
365 sc->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
367 if (scsi_low_is_msgout_continue(ti, SCSI_LOW_MSG_IDENTIFY) == 0)
371 flags = SCSI_LOW_MSGOUT_UNIFY | SCSI_LOW_MSGOUT_INIT;
373 else if (scsi_low_is_msgout_continue(ti,
374 SCSI_LOW_MSG_IDENTIFY | SCSI_LOW_MSG_SIMPLE_QTAG) == 0)
378 flags = SCSI_LOW_MSGOUT_UNIFY | SCSI_LOW_MSGOUT_INIT;
384 flags = SCSI_LOW_MSGOUT_INIT;
387 ncvhw_select_register_0(res, &sc->sc_hw);
388 if ((bus_read_1(res, cr0_stat) & STAT_INT) != 0)
389 return SCSI_LOW_START_FAIL;
391 ncv_target_nexus_establish(sc);
393 len = scsi_low_msgout(slp, ti, flags);
394 if (sc->sc_selstop == 0)
395 scsi_low_cmd(slp, ti);
397 if ((bus_read_1(res, cr0_stat) & STAT_INT) != 0)
398 return SCSI_LOW_START_FAIL;
400 bus_write_1(res, cr0_dstid, ti->ti_id);
401 bus_write_1(res, cr0_cmd, CMD_FLUSH);
402 ncvhw_fpush(res, ti->ti_msgoutstr, len);
403 if (sc->sc_selstop == 0)
406 slp->sl_scp.scp_cmd, slp->sl_scp.scp_cmdlen);
408 bus_write_1(res, cr0_cmd, cmd);
410 SCSI_LOW_SETUP_PHASE(ti, PH_SELSTART);
411 return SCSI_LOW_START_OK;
415 ncv_world_start(sc, fdone)
416 struct ncv_softc *sc;
419 struct scsi_low_softc *slp = &sc->sc_sclow;
420 struct resource *res = sc->port_res;
423 if ((slp->sl_cfgflags & CFG_NOPARITY) == 0)
424 sc->sc_hw.hw_cfg1 |= C1_PARENB;
426 sc->sc_hw.hw_cfg1 &= ~C1_PARENB;
428 ncvhw_reset(res, &sc->sc_hw);
429 ncvhw_init(res, &sc->sc_hw);
431 scsi_low_bus_reset(slp);
433 ncvhw_select_register_0(res, &sc->sc_hw);
434 bus_read_1(res, cr0_stat);
435 stat = bus_read_1(res, cr0_istat);
438 if (((stat & INTR_SBR) == 0) ||
439 (bus_read_1(res, cr0_istat) & INTR_SBR))
447 struct ncv_softc *sc;
448 struct targ_info *ti;
451 struct resource *res = sc->port_res;
452 struct ncv_targ_info *nti = (void *) ti;
453 u_int hwcycle, period;
455 if ((msg & SCSI_LOW_MSG_WIDE) != 0)
457 if (ti->ti_width != SCSI_LOW_BUS_WIDTH_8)
459 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
465 if ((msg & SCSI_LOW_MSG_SYNCH) == 0)
468 period = ti->ti_maxsynch.period;
469 hwcycle = (sc->sc_hw.hw_clk == 0) ? 40 : (5 * sc->sc_hw.hw_clk);
470 hwcycle = 1000 / hwcycle;
472 if (period < 200 / 4 && period >= 100 / 4)
473 nti->nti_reg_cfg3 |= sc->sc_hw.hw_cfg3_fscsi;
475 nti->nti_reg_cfg3 &= ~sc->sc_hw.hw_cfg3_fscsi;
477 period = ((period * 40 / hwcycle) + 5) / 10;
478 nti->nti_reg_period = period & 0x1f;
479 nti->nti_reg_offset = ti->ti_maxsynch.offset;
481 bus_write_1(res, cr0_period, nti->nti_reg_period);
482 bus_write_1(res, cr0_offs, nti->nti_reg_offset);
483 bus_write_1(res, cr0_cfg3, nti->nti_reg_cfg3);
488 ncv_targ_init(sc, ti, action)
489 struct ncv_softc *sc;
490 struct targ_info *ti;
493 struct ncv_targ_info *nti = (void *) ti;
495 if (action == SCSI_LOW_INFO_ALLOC || action == SCSI_LOW_INFO_REVOKE)
497 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
498 ti->ti_maxsynch.period = sc->sc_hw.hw_mperiod;
499 ti->ti_maxsynch.offset = sc->sc_hw.hw_moffset;
501 nti->nti_reg_cfg3 = sc->sc_hw.hw_cfg3;
502 nti->nti_reg_period = 0;
503 nti->nti_reg_offset = 0;
508 /**************************************************************
509 * General probe attach
510 **************************************************************/
511 static int ncv_setup_img(struct ncv_hw *, u_int, int);
514 ncv_setup_img(hw, dvcfg, hostid)
520 if (NCV_CLKFACTOR(dvcfg) > CLK_35M_F)
522 printf("ncv: invalid dvcfg flags\n");
526 if (NCV_C5IMG(dvcfg) != 0)
528 hw->hw_cfg5 = NCV_C5IMG(dvcfg);
529 hw->hw_clk = NCV_CLKFACTOR(dvcfg);
531 if ((ncv_io_control & NCV_ENABLE_FAST_SCSI) != 0 &&
532 (NCV_SPECIAL(dvcfg) & NCVHWCFG_MAX10M) != 0)
533 hw->hw_mperiod = 100 / 4;
535 if (NCV_SPECIAL(dvcfg) & NCVHWCFG_FIFOBUG)
536 hw->hw_cfg3_fclk = 0x04;
538 if (NCV_SPECIAL(dvcfg) & NCVHWCFG_SCSI1)
539 hw->hw_cfg2 &= ~C2_SCSI2;
541 if (NCV_SPECIAL(dvcfg) & NCVHWCFG_SLOW)
542 hw->hw_cfg1 |= C1_SLOW;
545 /* setup configuration image 3 */
546 if (hw->hw_clk != CLK_40M_F && hw->hw_clk <= CLK_25M_F)
547 hw->hw_cfg3 &= ~hw->hw_cfg3_fclk;
549 hw->hw_cfg3 |= hw->hw_cfg3_fclk;
551 /* setup configuration image 1 */
552 hw->hw_cfg1 = (hw->hw_cfg1 & 0xf0) | hostid;
557 ncvprobesubr(struct resource *res, u_int dvcfg, int hsid)
561 hwtab = ncv_template;
562 if (ncv_setup_img(&hwtab, dvcfg, hsid))
564 if (ncvhw_check(res, &hwtab) != 0)
572 struct ncv_softc *sc;
574 struct scsi_low_softc *slp = &sc->sc_sclow;
577 sc->sc_hw = ncv_template;
578 ncv_setup_img(&sc->sc_hw, slp->sl_cfgflags, slp->sl_hostid);
579 slp->sl_funcs = &ncv_funcs;
580 slp->sl_flags |= HW_READ_PADDING;
581 sc->sc_tmaxcnt = SCSI_LOW_MIN_TOUT * 1000 * 1000; /* default */
583 (void) scsi_low_attach(slp, 0, NCV_NTARGETS, NCV_NLUNS,
584 sizeof(struct ncv_targ_info), 0);
587 /**************************************************************
589 **************************************************************/
591 ncv_setup_and_start_pio(sc, reqlen)
592 struct ncv_softc *sc;
595 struct resource *res = sc->port_res;
597 ncvhw_select_register_0(res, &sc->sc_hw);
598 ncvhw_set_count(res, reqlen);
599 bus_write_1(res, cr0_cmd, CMD_TRANS | CMD_DMA);
601 ncvhw_select_register_1(res, &sc->sc_hw);
602 bus_write_1(res, cr1_fstat, FIFO_EN);
607 struct ncv_softc *sc;
608 struct targ_info *ti;
610 struct scsi_low_softc *slp = &sc->sc_sclow;
611 struct resource *res = sc->port_res;
614 slp->sl_flags &= ~HW_PDMASTART;
615 if (slp->sl_Qnexus == NULL)
617 slp->sl_error |= PDMAERR;
621 if (ti->ti_phase == PH_DATA)
623 len = ncvhw_get_count(res);
624 if (slp->sl_scp.scp_direction == SCSI_LOW_WRITE)
625 len += (bus_read_1(res,
626 cr0_sffl) & CR0_SFFLR_BMASK);
628 if ((u_int) len <= (u_int) sc->sc_sdatalen)
630 if ((slp->sl_scp.scp_direction == SCSI_LOW_READ) &&
631 sc->sc_tdatalen != len)
634 len = sc->sc_sdatalen - len;
635 if ((u_int) len > (u_int) slp->sl_scp.scp_datalen)
638 slp->sl_scp.scp_data += len;
639 slp->sl_scp.scp_datalen -= len;
644 if ((slp->sl_error & PDMAERR) == 0)
646 device_printf(slp->sl_dev,
647 "strange cnt hw 0x%x soft 0x%x\n", len,
648 slp->sl_scp.scp_datalen);
650 slp->sl_error |= PDMAERR;
652 scsi_low_data_finish(slp);
656 device_printf(slp->sl_dev, "data phase miss\n");
657 slp->sl_error |= PDMAERR;
661 ncvhw_select_register_1(res, &sc->sc_hw);
662 bus_write_1(res, cr1_fstat, 0);
663 ncvhw_select_register_0(res, &sc->sc_hw);
667 ncv_pio_read(sc, buf, reqlen)
668 struct ncv_softc *sc;
672 struct scsi_low_softc *slp = &sc->sc_sclow;
673 struct resource *res = sc->port_res;
675 register u_int8_t fstat;
677 ncv_setup_and_start_pio(sc, reqlen);
678 slp->sl_flags |= HW_PDMASTART;
679 sc->sc_sdatalen = reqlen;
680 tout = sc->sc_tmaxcnt;
682 while (reqlen >= FIFO_F_SZ && tout -- > 0)
684 fstat = bus_read_1(res, cr1_fstat);
685 if (fstat == (u_int8_t) -1)
689 #define NCV_FAST32_ACCESS
690 #ifdef NCV_FAST32_ACCESS
691 bus_read_multi_4(res, cr1_fdata,
692 (u_int32_t *) buf, FIFO_F_SZ / 4);
693 #else /* !NCV_FAST32_ACCESS */
694 bus_read_multi_2(res, cr1_fdata,
695 (u_int16_t *) buf, FIFO_F_SZ / 2);
696 #endif /* !NCV_FAST32_ACCESS */
702 if (fstat & FIFO_BRK)
709 while (reqlen > 0 && tout -- > 0)
711 fstat = bus_read_1(res, cr1_fstat);
712 if ((fstat & FIFO_E) == 0)
714 *buf++ = bus_read_1(res, cr1_fdata);
719 if (fstat & FIFO_BRK)
727 ncvhw_select_register_0(res, &sc->sc_hw);
728 sc->sc_tdatalen = reqlen;
732 ncv_pio_write(sc, buf, reqlen)
733 struct ncv_softc *sc;
737 struct scsi_low_softc *slp = &sc->sc_sclow;
738 struct resource *res = sc->port_res;
740 register u_int8_t fstat;
742 ncv_setup_and_start_pio(sc, reqlen);
743 sc->sc_sdatalen = reqlen;
744 tout = sc->sc_tmaxcnt;
745 slp->sl_flags |= HW_PDMASTART;
747 while (reqlen >= FIFO_F_SZ && tout -- > 0)
749 fstat = bus_read_1(res, cr1_fstat);
750 if (fstat & FIFO_BRK)
753 if ((fstat & FIFO_E) != 0)
755 #ifdef NCV_FAST32_ACCESS
756 bus_write_multi_4(res, cr1_fdata,
757 (u_int32_t *) buf, FIFO_F_SZ / 4);
758 #else /* !NCV_FAST32_ACCESS */
759 bus_write_multi_2(res, cr1_fdata,
760 (u_int16_t *) buf, FIFO_F_SZ / 2);
761 #endif /* !NCV_FAST32_ACCESS */
771 while (reqlen > 0 && tout -- > 0)
773 fstat = bus_read_1(res, cr1_fstat);
774 if (fstat & FIFO_BRK)
777 if ((fstat & FIFO_F) == 0) /* fifo not full */
779 bus_write_1(res, cr1_fdata, *buf++);
789 ncvhw_select_register_0(res, &sc->sc_hw);
792 /**************************************************************
793 * disconnect & reselect (HW low)
794 **************************************************************/
797 struct ncv_softc *sc;
799 struct scsi_low_softc *slp = &sc->sc_sclow;
800 struct resource *res = sc->port_res;
801 struct targ_info *ti;
804 if ((bus_read_1(res, cr0_sffl) & CR0_SFFLR_BMASK) != 2)
806 device_printf(slp->sl_dev, "illegal fifo bytes\n");
807 scsi_low_restart(slp, SCSI_LOW_RESTART_HARD, "chip confused");
811 sid = (u_int) bus_read_1(res, cr0_sfifo);
812 sid &= ~(1 << slp->sl_hostid);
814 ti = scsi_low_reselected((struct scsi_low_softc *) sc, sid);
819 ncv_statics.reselect ++;
820 #endif /* NCV_STATICS */
821 bus_write_1(res, cr0_dstid, sid);
826 ncv_disconnected(sc, ti)
827 struct ncv_softc *sc;
828 struct targ_info *ti;
830 struct scsi_low_softc *slp = &sc->sc_sclow;
831 struct resource *res = sc->port_res;
833 bus_write_1(res, cr0_cmd, CMD_FLUSH);
834 bus_write_1(res, cr0_cmd, CMD_ENSEL);
837 ncv_statics.disconnect ++;
838 #endif /* NCV_STATICS */
840 scsi_low_disconnected(slp, ti);
844 /**************************************************************
846 **************************************************************/
848 ncv_target_nexus_establish(sc)
849 struct ncv_softc *sc;
851 struct scsi_low_softc *slp = &sc->sc_sclow;
852 struct targ_info *ti = slp->sl_Tnexus;
853 struct ncv_targ_info *nti = (void *) ti;
854 struct resource *res = sc->port_res;
856 bus_write_1(res, cr0_period, nti->nti_reg_period);
857 bus_write_1(res, cr0_offs, nti->nti_reg_offset);
858 bus_write_1(res, cr0_cfg3, nti->nti_reg_cfg3);
863 ncv_lun_nexus_establish(sc)
864 struct ncv_softc *sc;
871 ncv_ccb_nexus_establish(sc)
872 struct ncv_softc *sc;
874 struct scsi_low_softc *slp = &sc->sc_sclow;
875 struct slccb *cb = slp->sl_Qnexus;
877 sc->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
883 struct ncv_softc *sc;
885 struct resource *res = sc->port_res;
887 register u_int8_t status;
889 for (wc = 0; wc < NCV_DELAY_MAX / NCV_DELAY_INTERVAL; wc ++)
891 status = bus_read_1(res, cr0_stat);
892 if ((status & STAT_INT) != 0)
895 DELAY(NCV_DELAY_INTERVAL);
904 struct ncv_softc *sc = arg;
905 struct scsi_low_softc *slp = &sc->sc_sclow;
906 struct resource *res = sc->port_res;
907 struct targ_info *ti;
911 u_int8_t regv, status, ireason;
914 if (slp->sl_flags & HW_INACTIVE)
917 /********************************************
919 ********************************************/
920 ncvhw_select_register_0(res, &sc->sc_hw);
921 status = bus_read_1(res, cr0_stat);
922 if ((status & STAT_INT) == 0 || status == (u_int8_t) -1)
925 ireason = bus_read_1(res, cr0_istat);
926 if ((ireason & INTR_SBR) != 0)
930 /* avoid power off hangup */
931 val = bus_read_1(res, cr0_cfg1);
932 bus_write_1(res, cr0_cfg1, val | C1_SRR);
935 scsi_low_restart(slp, SCSI_LOW_RESTART_SOFT,
936 "bus reset (power off?)");
940 /********************************************
942 ********************************************/
946 scsi_low_print(slp, NULL);
947 device_printf(slp->sl_dev, "st %x ist %x\n\n",
951 kdb_enter(KDB_WHY_CAM, "ncv");
954 #endif /* NCV_DEBUG */
956 /********************************************
957 * Reselect or Disconnect or Nexus check
958 ********************************************/
960 if (ireason == INTR_RESELECT)
962 if (ncv_reselected(sc) == EJUSTRETURN)
967 if ((ti = slp->sl_Tnexus) == NULL)
971 if ((status & (STAT_PE | STAT_GE)) != 0)
973 slp->sl_error |= PARITYERR;
974 if ((status & PHASE_MASK) == MESSAGE_IN_PHASE)
975 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_PARITY, 0);
977 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ERROR, 1);
978 derror = SCSI_LOW_DATA_PE;
981 if ((ireason & (INTR_DIS | INTR_ILL)) != 0)
983 if ((ireason & INTR_ILL) == 0)
984 return ncv_disconnected(sc, ti);
986 slp->sl_error |= FATALIO;
987 scsi_low_restart(slp, SCSI_LOW_RESTART_HARD, "illegal cmd");
991 /********************************************
992 * Internal scsi phase
993 ********************************************/
994 switch (ti->ti_phase)
997 scsi_low_arbit_win(slp);
998 SCSI_LOW_SETUP_PHASE(ti, PH_SELECTED);
1000 if (sc->sc_selstop == 0)
1003 * Here scsi phases expected are
1005 * MSGIN : target wants to disconnect the host.
1006 * STATUSIN : immediate command completed.
1007 * CMD PHASE : command out failed
1008 * MSGOUT : identify command failed.
1010 if ((status & PHASE_MASK) != MESSAGE_OUT_PHASE)
1015 if ((status & PHASE_MASK) != MESSAGE_OUT_PHASE)
1017 if ((ireason & INTR_FC) != 0)
1019 SCSI_LOW_ASSERT_ATN(slp);
1022 SCSI_LOW_SETUP_PHASE(ti, PH_MSGOUT);
1026 ncv_target_nexus_establish(sc);
1027 if ((status & PHASE_MASK) != MESSAGE_IN_PHASE)
1029 device_printf(slp->sl_dev,
1030 "unexpected phase after reselect\n");
1031 slp->sl_error |= FATALIO;
1032 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 1);
1038 if ((slp->sl_flags & HW_PDMASTART) != 0)
1040 ncv_pdma_end(sc, ti);
1045 /********************************************
1046 * Scsi phase sequencer
1047 ********************************************/
1048 switch (status & PHASE_MASK)
1050 case DATA_OUT_PHASE: /* data out */
1051 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1052 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_WRITE) != 0)
1054 scsi_low_attention(slp);
1057 if (slp->sl_scp.scp_datalen <= 0)
1059 if ((ireason & INTR_BS) == 0)
1062 if ((slp->sl_error & PDMAERR) == 0)
1063 device_printf(slp->sl_dev, "data underrun\n");
1064 slp->sl_error |= PDMAERR;
1066 if ((slp->sl_flags & HW_WRITE_PADDING) != 0)
1068 u_int8_t padding[NCV_PADDING_SIZE];
1070 bzero(padding, sizeof(padding));
1071 ncv_pio_write(sc, padding, sizeof(padding));
1075 device_printf(slp->sl_dev,
1076 "write padding required\n");
1081 len = slp->sl_scp.scp_datalen;
1082 if ((ncv_io_control & NCV_WRITE_INTERRUPTS_DRIVEN) != 0)
1084 if (len > ncv_data_write_bytes)
1085 len = ncv_data_write_bytes;
1087 ncv_pio_write(sc, slp->sl_scp.scp_data, len);
1091 case DATA_IN_PHASE: /* data in */
1092 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1093 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_READ) != 0)
1095 scsi_low_attention(slp);
1098 if (slp->sl_scp.scp_datalen <= 0)
1100 if ((ireason & INTR_BS) == 0)
1103 if ((slp->sl_error & PDMAERR) == 0)
1104 device_printf(slp->sl_dev, "data overrun\n");
1105 slp->sl_error |= PDMAERR;
1107 if ((slp->sl_flags & HW_READ_PADDING) != 0)
1109 u_int8_t padding[NCV_PADDING_SIZE];
1111 ncv_pio_read(sc, padding, sizeof(padding));
1115 device_printf(slp->sl_dev,
1116 "read padding required\n");
1122 len = slp->sl_scp.scp_datalen;
1123 if ((ncv_io_control & NCV_READ_INTERRUPTS_DRIVEN) != 0)
1125 if (len > ncv_data_read_bytes)
1126 len = ncv_data_read_bytes;
1128 ncv_pio_read(sc, slp->sl_scp.scp_data, len);
1132 case COMMAND_PHASE: /* cmd out */
1133 SCSI_LOW_SETUP_PHASE(ti, PH_CMD);
1134 if (scsi_low_cmd(slp, ti) != 0)
1136 scsi_low_attention(slp);
1139 bus_write_1(res, cr0_cmd, CMD_FLUSH);
1141 slp->sl_scp.scp_cmd, slp->sl_scp.scp_cmdlen);
1142 bus_write_1(res, cr0_cmd, CMD_TRANS);
1145 case STATUS_PHASE: /* status in */
1146 SCSI_LOW_SETUP_PHASE(ti, PH_STAT);
1147 bus_write_1(res, cr0_cmd, CMD_FLUSH);
1148 bus_write_1(res, cr0_cmd, CMD_ICCS);
1155 case MESSAGE_OUT_PHASE: /* msg out */
1156 SCSI_LOW_SETUP_PHASE(ti, PH_MSGOUT);
1157 bus_write_1(res, cr0_cmd, CMD_FLUSH);
1159 flags = SCSI_LOW_MSGOUT_UNIFY;
1160 if (ti->ti_ophase != ti->ti_phase)
1161 flags |= SCSI_LOW_MSGOUT_INIT;
1162 len = scsi_low_msgout(slp, ti, flags);
1164 if (len > 1 && slp->sl_atten == 0)
1166 scsi_low_attention(slp);
1169 ncvhw_fpush(res, ti->ti_msgoutstr, len);
1170 bus_write_1(res, cr0_cmd, CMD_TRANS);
1171 SCSI_LOW_DEASSERT_ATN(slp);
1174 case MESSAGE_IN_PHASE: /* msg in */
1175 SCSI_LOW_SETUP_PHASE(ti, PH_MSGIN);
1177 len = bus_read_1(res, cr0_sffl) & CR0_SFFLR_BMASK;
1178 if (sc->sc_compseq != 0)
1181 if ((ireason & INTR_FC) && len == 2)
1183 regv = bus_read_1(res, cr0_sfifo);
1184 scsi_low_statusin(slp, ti, regv | derror);
1189 slp->sl_error |= FATALIO;
1190 scsi_low_assert_msg(slp, ti,
1191 SCSI_LOW_MSG_ABORT, 1);
1192 bus_write_1(res, cr0_cmd, CMD_MSGOK);
1196 else if (ireason & INTR_BS)
1198 bus_write_1(res, cr0_cmd, CMD_FLUSH);
1199 bus_write_1(res, cr0_cmd, CMD_TRANS);
1200 if ((ncv_io_control & NCV_FAST_INTERRUPTS) != 0)
1202 if (ncv_catch_intr(sc) == 0)
1208 if ((ireason & INTR_FC) && len == 1)
1210 regv = bus_read_1(res, cr0_sfifo);
1211 if (scsi_low_msgin(slp, ti, regv | derror) == 0)
1213 if (scsi_low_is_msgout_continue(ti, 0) != 0)
1215 scsi_low_attention(slp);
1218 bus_write_1(res, cr0_cmd, CMD_MSGOK);
1219 if ((ncv_io_control & NCV_FAST_INTERRUPTS) != 0)
1222 * clear a pending interrupt and sync with
1230 slp->sl_error |= FATALIO;
1231 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 1);
1232 bus_write_1(res, cr0_cmd, CMD_MSGOK);