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 * SPDX-License-Identifier: BSD-3-Clause
11 * [NetBSD for NEC PC-98 series]
12 * Copyright (c) 1995, 1996, 1997, 1998, 1999, 2000, 2001
13 * NetBSD/pc98 porting staff. All rights reserved.
14 * Copyright (c) 1995, 1996, 1997, 1998, 1999, 2000, 2001
15 * Naofumi HONDA. All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 * 3. The name of the author may not be used to endorse or promote products
26 * derived from this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
30 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
32 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
33 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
34 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
49 #include <sys/queue.h>
50 #include <sys/malloc.h>
51 #include <sys/errno.h>
54 #include <machine/cpu.h>
55 #include <machine/bus.h>
57 #include <compat/netbsd/dvcfg.h>
59 #include <cam/scsi/scsi_low.h>
61 #include <dev/ncv/ncr53c500reg.h>
62 #include <dev/ncv/ncr53c500hw.h>
63 #include <dev/ncv/ncr53c500var.h>
65 #include <dev/ncv/ncr53c500hwtab.h>
67 #define NCV_MAX_DATA_SIZE (64 * 1024)
68 #define NCV_DELAY_MAX (2 * 1000 * 1000)
69 #define NCV_DELAY_INTERVAL (1)
70 #define NCV_PADDING_SIZE (32)
72 /***************************************************
74 ***************************************************/
75 #define NCV_READ_INTERRUPTS_DRIVEN 0x0001
76 #define NCV_WRITE_INTERRUPTS_DRIVEN 0x0002
77 #define NCV_ENABLE_FAST_SCSI 0x0010
78 #define NCV_FAST_INTERRUPTS 0x0100
80 u_int ncv_io_control = NCV_IO_CONTROL_FLAGS;
81 int ncv_data_read_bytes = 4096;
82 int ncv_data_write_bytes = 4096;
84 /***************************************************
86 ***************************************************/
89 #endif /* NCV_DEBUG */
92 static struct ncv_statics {
96 #endif /* NCV_STATICS */
98 /***************************************************
100 ***************************************************/
101 extern struct cfdriver ncv_cd;
103 /**************************************************************
105 **************************************************************/
107 static void ncv_pio_read(struct ncv_softc *, u_int8_t *, u_int);
108 static void ncv_pio_write(struct ncv_softc *, u_int8_t *, u_int);
109 static int ncv_msg(struct ncv_softc *, struct targ_info *, u_int);
110 static int ncv_reselected(struct ncv_softc *);
111 static int ncv_disconnected(struct ncv_softc *, struct targ_info *);
113 static __inline void ncvhw_set_count(struct resource *, int);
114 static __inline u_int ncvhw_get_count(struct resource *);
115 static __inline void ncvhw_select_register_0(struct resource *, struct ncv_hw *);
116 static __inline void ncvhw_select_register_1(struct resource *, struct ncv_hw *);
117 static __inline void ncvhw_fpush(struct resource *, u_int8_t *, int);
119 static void ncv_pdma_end(struct ncv_softc *sc, struct targ_info *);
120 static int ncv_world_start(struct ncv_softc *, int);
121 static void ncvhw_bus_reset(struct ncv_softc *);
122 static void ncvhw_reset(struct resource *, struct ncv_hw *);
123 static int ncvhw_check(struct resource *, struct ncv_hw *);
124 static void ncvhw_init(struct resource *, struct ncv_hw *);
125 static int ncvhw_start_selection(struct ncv_softc *sc, struct slccb *);
126 static void ncvhw_attention(struct ncv_softc *);
127 static int ncv_ccb_nexus_establish(struct ncv_softc *);
128 static int ncv_lun_nexus_establish(struct ncv_softc *);
129 static int ncv_target_nexus_establish(struct ncv_softc *);
130 static int ncv_targ_init(struct ncv_softc *, struct targ_info *, int);
131 static int ncv_catch_intr(struct ncv_softc *);
132 #ifdef NCV_POWER_CONTROL
133 static int ncvhw_power(struct ncv_softc *, u_int);
134 #endif /* NCV_POWER_CONTROL */
135 static __inline void ncv_setup_and_start_pio(struct ncv_softc *, u_int);
137 struct scsi_low_funcs ncv_funcs = {
138 SC_LOW_INIT_T ncv_world_start,
139 SC_LOW_BUSRST_T ncvhw_bus_reset,
140 SC_LOW_TARG_INIT_T ncv_targ_init,
141 SC_LOW_LUN_INIT_T NULL,
143 SC_LOW_SELECT_T ncvhw_start_selection,
144 SC_LOW_NEXUS_T ncv_lun_nexus_establish,
145 SC_LOW_NEXUS_T ncv_ccb_nexus_establish,
147 SC_LOW_ATTEN_T ncvhw_attention,
148 SC_LOW_MSG_T ncv_msg,
150 SC_LOW_TIMEOUT_T NULL,
151 SC_LOW_POLL_T ncvintr,
153 NULL, /* SC_LOW_POWER_T ncvhw_power, */
156 /**************************************************************
158 **************************************************************/
160 ncvhw_select_register_0(struct resource *res, struct ncv_hw *hw)
163 bus_write_1(res, cr0_cfg4, hw->hw_cfg4);
167 ncvhw_select_register_1(struct resource *res, struct ncv_hw *hw)
170 bus_write_1(res, cr1_cfg5, hw->hw_cfg5);
174 ncvhw_fpush(struct resource *res, u_int8_t *buf, int len)
178 for (ptr = 0; ptr < len; ptr ++)
179 bus_write_1(res, cr0_sfifo, buf[ptr]);
183 ncvhw_set_count(struct resource *res, int count)
186 bus_write_1(res, cr0_tclsb, (u_int8_t) count);
187 bus_write_1(res, cr0_tcmsb, (u_int8_t) (count >> NBBY));
188 bus_write_1(res, cr0_tchsb, (u_int8_t) (count >> (NBBY * 2)));
191 static __inline u_int
192 ncvhw_get_count(struct resource *res)
196 count = (u_int) bus_read_1(res, cr0_tclsb);
197 count |= ((u_int) bus_read_1(res, cr0_tcmsb)) << NBBY;
198 count |= ((u_int) bus_read_1(res, cr0_tchsb)) << (NBBY * 2);
203 ncvhw_check(struct resource *res, struct ncv_hw *hw)
207 ncvhw_select_register_0(res, hw);
208 bus_write_1(res, cr0_cmd, CMD_NOP | CMD_DMA);
209 if (bus_read_1(res, cr0_cmd) != (CMD_NOP | CMD_DMA))
212 printf("ncv: cr0_cmd CMD_NOP|CMD_DMA failed\n");
213 #endif /* NCV_DEBUG */
217 bus_write_1(res, cr0_cmd, CMD_NOP);
218 if (bus_read_1(res, cr0_cmd) != CMD_NOP)
221 printf("ncv: cr0_cmd CMD_NOP failed\n");
222 #endif /* NCV_DEBUG */
227 ncvhw_reset(res, hw);
231 ncvhw_select_register_0(res, hw);
232 bus_write_1(res, cr0_cmd, CMD_FLUSH);
233 bus_write_1(res, cr0_cmd, CMD_RSTSCSI);
234 bus_write_1(res, cr0_cmd, CMD_NOP | CMD_DMA);
238 bus_read_1(res, cr0_stat);
239 stat = bus_read_1(res, cr0_istat);
242 if (((stat & INTR_SBR) == 0) ||
243 (bus_read_1(res, cr0_istat) & INTR_SBR))
246 printf("ncv: cr0_istat SCSI BUS RESET failed\n");
247 #endif /* NCV_DEBUG */
255 ncvhw_reset(struct resource *res, struct ncv_hw *hw)
258 ncvhw_select_register_0(res, hw);
260 /* dummy cmd twice */
261 bus_write_1(res, cr0_cmd, CMD_NOP);
262 bus_write_1(res, cr0_cmd, CMD_NOP);
265 bus_write_1(res, cr0_cmd, CMD_RSTCHIP);
267 /* again dummy cmd twice */
268 bus_write_1(res, cr0_cmd, CMD_NOP);
269 bus_write_1(res, cr0_cmd, CMD_NOP);
273 ncvhw_init(struct resource *res, struct ncv_hw *hw)
276 ncvhw_select_register_0(res, hw);
277 bus_write_1(res, cr0_clk, hw->hw_clk);
278 bus_write_1(res, cr0_srtout, SEL_TOUT);
279 bus_write_1(res, cr0_period, 0);
280 bus_write_1(res, cr0_offs, 0);
282 bus_write_1(res, cr0_cfg1, hw->hw_cfg1);
283 bus_write_1(res, cr0_cfg2, hw->hw_cfg2);
284 bus_write_1(res, cr0_cfg3, hw->hw_cfg3);
285 bus_write_1(res, cr0_tchsb, 0);
287 ncvhw_select_register_1(res, hw);
288 bus_write_1(res, cr1_fstat, 0x0);
289 bus_write_1(res, cr1_pflag, 0x0);
290 bus_write_1(res, cr1_atacmd, ATACMD_ENGAGE);
292 ncvhw_select_register_0(res, hw);
295 #ifdef NCV_POWER_CONTROL
297 ncvhw_power(sc, flags)
298 struct ncv_softc *sc;
301 struct scsi_low_softc *slp = &sc->sc_sclow;
302 struct resource *res = sc->port_res;
304 if (flags == SCSI_LOW_POWDOWN)
306 device_printf(slp->sl_dev, "power down\n");
307 ncvhw_select_register_1(res, &sc->sc_hw);
308 bus_write_1(res, cr1_atacmd, ATACMD_POWDOWN);
312 switch (sc->sc_rstep)
315 device_printf(slp->sl_dev, "resume step O\n");
316 ncvhw_select_register_1(res, &sc->sc_hw);
317 bus_write_1(res, cr1_atacmd, ATACMD_ENGAGE);
321 device_printf(slp->sl_dev, "resume step I\n");
322 ncvhw_reset(res, &sc->sc_hw);
323 ncvhw_init(res, &sc->sc_hw);
330 #endif /* NCV_POWER_CONTROL */
332 /**************************************************************
334 **************************************************************/
337 struct ncv_softc *sc;
340 bus_write_1(sc->port_res, cr0_cmd, CMD_SETATN);
346 struct ncv_softc *sc;
349 ncvhw_select_register_0(sc->port_res, &sc->sc_hw);
350 bus_write_1(sc->port_res, cr0_cmd, CMD_FLUSH);
351 bus_write_1(sc->port_res, cr0_cmd, CMD_RSTSCSI);
352 bus_write_1(sc->port_res, cr0_cmd, CMD_NOP | CMD_DMA);
356 ncvhw_start_selection(sc, cb)
357 struct ncv_softc *sc;
360 struct scsi_low_softc *slp = &sc->sc_sclow;
361 struct resource *res = sc->port_res;
362 struct targ_info *ti = cb->ti;
367 sc->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
369 if (scsi_low_is_msgout_continue(ti, SCSI_LOW_MSG_IDENTIFY) == 0)
373 flags = SCSI_LOW_MSGOUT_UNIFY | SCSI_LOW_MSGOUT_INIT;
375 else if (scsi_low_is_msgout_continue(ti,
376 SCSI_LOW_MSG_IDENTIFY | SCSI_LOW_MSG_SIMPLE_QTAG) == 0)
380 flags = SCSI_LOW_MSGOUT_UNIFY | SCSI_LOW_MSGOUT_INIT;
386 flags = SCSI_LOW_MSGOUT_INIT;
389 ncvhw_select_register_0(res, &sc->sc_hw);
390 if ((bus_read_1(res, cr0_stat) & STAT_INT) != 0)
391 return SCSI_LOW_START_FAIL;
393 ncv_target_nexus_establish(sc);
395 len = scsi_low_msgout(slp, ti, flags);
396 if (sc->sc_selstop == 0)
397 scsi_low_cmd(slp, ti);
399 if ((bus_read_1(res, cr0_stat) & STAT_INT) != 0)
400 return SCSI_LOW_START_FAIL;
402 bus_write_1(res, cr0_dstid, ti->ti_id);
403 bus_write_1(res, cr0_cmd, CMD_FLUSH);
404 ncvhw_fpush(res, ti->ti_msgoutstr, len);
405 if (sc->sc_selstop == 0)
408 slp->sl_scp.scp_cmd, slp->sl_scp.scp_cmdlen);
410 bus_write_1(res, cr0_cmd, cmd);
412 SCSI_LOW_SETUP_PHASE(ti, PH_SELSTART);
413 return SCSI_LOW_START_OK;
417 ncv_world_start(sc, fdone)
418 struct ncv_softc *sc;
421 struct scsi_low_softc *slp = &sc->sc_sclow;
422 struct resource *res = sc->port_res;
425 if ((slp->sl_cfgflags & CFG_NOPARITY) == 0)
426 sc->sc_hw.hw_cfg1 |= C1_PARENB;
428 sc->sc_hw.hw_cfg1 &= ~C1_PARENB;
430 ncvhw_reset(res, &sc->sc_hw);
431 ncvhw_init(res, &sc->sc_hw);
433 scsi_low_bus_reset(slp);
435 ncvhw_select_register_0(res, &sc->sc_hw);
436 bus_read_1(res, cr0_stat);
437 stat = bus_read_1(res, cr0_istat);
440 if (((stat & INTR_SBR) == 0) ||
441 (bus_read_1(res, cr0_istat) & INTR_SBR))
449 struct ncv_softc *sc;
450 struct targ_info *ti;
453 struct resource *res = sc->port_res;
454 struct ncv_targ_info *nti = (void *) ti;
455 u_int hwcycle, period;
457 if ((msg & SCSI_LOW_MSG_WIDE) != 0)
459 if (ti->ti_width != SCSI_LOW_BUS_WIDTH_8)
461 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
467 if ((msg & SCSI_LOW_MSG_SYNCH) == 0)
470 period = ti->ti_maxsynch.period;
471 hwcycle = (sc->sc_hw.hw_clk == 0) ? 40 : (5 * sc->sc_hw.hw_clk);
472 hwcycle = 1000 / hwcycle;
474 if (period < 200 / 4 && period >= 100 / 4)
475 nti->nti_reg_cfg3 |= sc->sc_hw.hw_cfg3_fscsi;
477 nti->nti_reg_cfg3 &= ~sc->sc_hw.hw_cfg3_fscsi;
479 period = ((period * 40 / hwcycle) + 5) / 10;
480 nti->nti_reg_period = period & 0x1f;
481 nti->nti_reg_offset = ti->ti_maxsynch.offset;
483 bus_write_1(res, cr0_period, nti->nti_reg_period);
484 bus_write_1(res, cr0_offs, nti->nti_reg_offset);
485 bus_write_1(res, cr0_cfg3, nti->nti_reg_cfg3);
490 ncv_targ_init(sc, ti, action)
491 struct ncv_softc *sc;
492 struct targ_info *ti;
495 struct ncv_targ_info *nti = (void *) ti;
497 if (action == SCSI_LOW_INFO_ALLOC || action == SCSI_LOW_INFO_REVOKE)
499 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
500 ti->ti_maxsynch.period = sc->sc_hw.hw_mperiod;
501 ti->ti_maxsynch.offset = sc->sc_hw.hw_moffset;
503 nti->nti_reg_cfg3 = sc->sc_hw.hw_cfg3;
504 nti->nti_reg_period = 0;
505 nti->nti_reg_offset = 0;
510 /**************************************************************
511 * General probe attach
512 **************************************************************/
513 static int ncv_setup_img(struct ncv_hw *, u_int, int);
516 ncv_setup_img(hw, dvcfg, hostid)
522 if (NCV_CLKFACTOR(dvcfg) > CLK_35M_F)
524 printf("ncv: invalid dvcfg flags\n");
528 if (NCV_C5IMG(dvcfg) != 0)
530 hw->hw_cfg5 = NCV_C5IMG(dvcfg);
531 hw->hw_clk = NCV_CLKFACTOR(dvcfg);
533 if ((ncv_io_control & NCV_ENABLE_FAST_SCSI) != 0 &&
534 (NCV_SPECIAL(dvcfg) & NCVHWCFG_MAX10M) != 0)
535 hw->hw_mperiod = 100 / 4;
537 if (NCV_SPECIAL(dvcfg) & NCVHWCFG_FIFOBUG)
538 hw->hw_cfg3_fclk = 0x04;
540 if (NCV_SPECIAL(dvcfg) & NCVHWCFG_SCSI1)
541 hw->hw_cfg2 &= ~C2_SCSI2;
543 if (NCV_SPECIAL(dvcfg) & NCVHWCFG_SLOW)
544 hw->hw_cfg1 |= C1_SLOW;
547 /* setup configuration image 3 */
548 if (hw->hw_clk != CLK_40M_F && hw->hw_clk <= CLK_25M_F)
549 hw->hw_cfg3 &= ~hw->hw_cfg3_fclk;
551 hw->hw_cfg3 |= hw->hw_cfg3_fclk;
553 /* setup configuration image 1 */
554 hw->hw_cfg1 = (hw->hw_cfg1 & 0xf0) | hostid;
559 ncvprobesubr(struct resource *res, u_int dvcfg, int hsid)
563 hwtab = ncv_template;
564 if (ncv_setup_img(&hwtab, dvcfg, hsid))
566 if (ncvhw_check(res, &hwtab) != 0)
574 struct ncv_softc *sc;
576 struct scsi_low_softc *slp = &sc->sc_sclow;
579 sc->sc_hw = ncv_template;
580 ncv_setup_img(&sc->sc_hw, slp->sl_cfgflags, slp->sl_hostid);
581 slp->sl_funcs = &ncv_funcs;
582 slp->sl_flags |= HW_READ_PADDING;
583 sc->sc_tmaxcnt = SCSI_LOW_MIN_TOUT * 1000 * 1000; /* default */
585 (void) scsi_low_attach(slp, 0, NCV_NTARGETS, NCV_NLUNS,
586 sizeof(struct ncv_targ_info), 0);
589 /**************************************************************
591 **************************************************************/
593 ncv_setup_and_start_pio(sc, reqlen)
594 struct ncv_softc *sc;
597 struct resource *res = sc->port_res;
599 ncvhw_select_register_0(res, &sc->sc_hw);
600 ncvhw_set_count(res, reqlen);
601 bus_write_1(res, cr0_cmd, CMD_TRANS | CMD_DMA);
603 ncvhw_select_register_1(res, &sc->sc_hw);
604 bus_write_1(res, cr1_fstat, FIFO_EN);
609 struct ncv_softc *sc;
610 struct targ_info *ti;
612 struct scsi_low_softc *slp = &sc->sc_sclow;
613 struct resource *res = sc->port_res;
616 slp->sl_flags &= ~HW_PDMASTART;
617 if (slp->sl_Qnexus == NULL)
619 slp->sl_error |= PDMAERR;
623 if (ti->ti_phase == PH_DATA)
625 len = ncvhw_get_count(res);
626 if (slp->sl_scp.scp_direction == SCSI_LOW_WRITE)
627 len += (bus_read_1(res,
628 cr0_sffl) & CR0_SFFLR_BMASK);
630 if ((u_int) len <= (u_int) sc->sc_sdatalen)
632 if ((slp->sl_scp.scp_direction == SCSI_LOW_READ) &&
633 sc->sc_tdatalen != len)
636 len = sc->sc_sdatalen - len;
637 if ((u_int) len > (u_int) slp->sl_scp.scp_datalen)
640 slp->sl_scp.scp_data += len;
641 slp->sl_scp.scp_datalen -= len;
646 if ((slp->sl_error & PDMAERR) == 0)
648 device_printf(slp->sl_dev,
649 "strange cnt hw 0x%x soft 0x%x\n", len,
650 slp->sl_scp.scp_datalen);
652 slp->sl_error |= PDMAERR;
654 scsi_low_data_finish(slp);
658 device_printf(slp->sl_dev, "data phase miss\n");
659 slp->sl_error |= PDMAERR;
663 ncvhw_select_register_1(res, &sc->sc_hw);
664 bus_write_1(res, cr1_fstat, 0);
665 ncvhw_select_register_0(res, &sc->sc_hw);
669 ncv_pio_read(sc, buf, reqlen)
670 struct ncv_softc *sc;
674 struct scsi_low_softc *slp = &sc->sc_sclow;
675 struct resource *res = sc->port_res;
677 register u_int8_t fstat;
679 ncv_setup_and_start_pio(sc, reqlen);
680 slp->sl_flags |= HW_PDMASTART;
681 sc->sc_sdatalen = reqlen;
682 tout = sc->sc_tmaxcnt;
684 while (reqlen >= FIFO_F_SZ && tout -- > 0)
686 fstat = bus_read_1(res, cr1_fstat);
687 if (fstat == (u_int8_t) -1)
691 #define NCV_FAST32_ACCESS
692 #ifdef NCV_FAST32_ACCESS
693 bus_read_multi_4(res, cr1_fdata,
694 (u_int32_t *) buf, FIFO_F_SZ / 4);
695 #else /* !NCV_FAST32_ACCESS */
696 bus_read_multi_2(res, cr1_fdata,
697 (u_int16_t *) buf, FIFO_F_SZ / 2);
698 #endif /* !NCV_FAST32_ACCESS */
704 if (fstat & FIFO_BRK)
711 while (reqlen > 0 && tout -- > 0)
713 fstat = bus_read_1(res, cr1_fstat);
714 if ((fstat & FIFO_E) == 0)
716 *buf++ = bus_read_1(res, cr1_fdata);
721 if (fstat & FIFO_BRK)
729 ncvhw_select_register_0(res, &sc->sc_hw);
730 sc->sc_tdatalen = reqlen;
734 ncv_pio_write(sc, buf, reqlen)
735 struct ncv_softc *sc;
739 struct scsi_low_softc *slp = &sc->sc_sclow;
740 struct resource *res = sc->port_res;
742 register u_int8_t fstat;
744 ncv_setup_and_start_pio(sc, reqlen);
745 sc->sc_sdatalen = reqlen;
746 tout = sc->sc_tmaxcnt;
747 slp->sl_flags |= HW_PDMASTART;
749 while (reqlen >= FIFO_F_SZ && tout -- > 0)
751 fstat = bus_read_1(res, cr1_fstat);
752 if (fstat & FIFO_BRK)
755 if ((fstat & FIFO_E) != 0)
757 #ifdef NCV_FAST32_ACCESS
758 bus_write_multi_4(res, cr1_fdata,
759 (u_int32_t *) buf, FIFO_F_SZ / 4);
760 #else /* !NCV_FAST32_ACCESS */
761 bus_write_multi_2(res, cr1_fdata,
762 (u_int16_t *) buf, FIFO_F_SZ / 2);
763 #endif /* !NCV_FAST32_ACCESS */
773 while (reqlen > 0 && tout -- > 0)
775 fstat = bus_read_1(res, cr1_fstat);
776 if (fstat & FIFO_BRK)
779 if ((fstat & FIFO_F) == 0) /* fifo not full */
781 bus_write_1(res, cr1_fdata, *buf++);
791 ncvhw_select_register_0(res, &sc->sc_hw);
794 /**************************************************************
795 * disconnect & reselect (HW low)
796 **************************************************************/
799 struct ncv_softc *sc;
801 struct scsi_low_softc *slp = &sc->sc_sclow;
802 struct resource *res = sc->port_res;
803 struct targ_info *ti;
806 if ((bus_read_1(res, cr0_sffl) & CR0_SFFLR_BMASK) != 2)
808 device_printf(slp->sl_dev, "illegal fifo bytes\n");
809 scsi_low_restart(slp, SCSI_LOW_RESTART_HARD, "chip confused");
813 sid = (u_int) bus_read_1(res, cr0_sfifo);
814 sid &= ~(1 << slp->sl_hostid);
816 ti = scsi_low_reselected((struct scsi_low_softc *) sc, sid);
821 ncv_statics.reselect ++;
822 #endif /* NCV_STATICS */
823 bus_write_1(res, cr0_dstid, sid);
828 ncv_disconnected(sc, ti)
829 struct ncv_softc *sc;
830 struct targ_info *ti;
832 struct scsi_low_softc *slp = &sc->sc_sclow;
833 struct resource *res = sc->port_res;
835 bus_write_1(res, cr0_cmd, CMD_FLUSH);
836 bus_write_1(res, cr0_cmd, CMD_ENSEL);
839 ncv_statics.disconnect ++;
840 #endif /* NCV_STATICS */
842 scsi_low_disconnected(slp, ti);
846 /**************************************************************
848 **************************************************************/
850 ncv_target_nexus_establish(sc)
851 struct ncv_softc *sc;
853 struct scsi_low_softc *slp = &sc->sc_sclow;
854 struct targ_info *ti = slp->sl_Tnexus;
855 struct ncv_targ_info *nti = (void *) ti;
856 struct resource *res = sc->port_res;
858 bus_write_1(res, cr0_period, nti->nti_reg_period);
859 bus_write_1(res, cr0_offs, nti->nti_reg_offset);
860 bus_write_1(res, cr0_cfg3, nti->nti_reg_cfg3);
865 ncv_lun_nexus_establish(sc)
866 struct ncv_softc *sc;
873 ncv_ccb_nexus_establish(sc)
874 struct ncv_softc *sc;
876 struct scsi_low_softc *slp = &sc->sc_sclow;
877 struct slccb *cb = slp->sl_Qnexus;
879 sc->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
885 struct ncv_softc *sc;
887 struct resource *res = sc->port_res;
889 register u_int8_t status;
891 for (wc = 0; wc < NCV_DELAY_MAX / NCV_DELAY_INTERVAL; wc ++)
893 status = bus_read_1(res, cr0_stat);
894 if ((status & STAT_INT) != 0)
897 DELAY(NCV_DELAY_INTERVAL);
906 struct ncv_softc *sc = arg;
907 struct scsi_low_softc *slp = &sc->sc_sclow;
908 struct resource *res = sc->port_res;
909 struct targ_info *ti;
913 u_int8_t regv, status, ireason;
916 if (slp->sl_flags & HW_INACTIVE)
919 /********************************************
921 ********************************************/
922 ncvhw_select_register_0(res, &sc->sc_hw);
923 status = bus_read_1(res, cr0_stat);
924 if ((status & STAT_INT) == 0 || status == (u_int8_t) -1)
927 ireason = bus_read_1(res, cr0_istat);
928 if ((ireason & INTR_SBR) != 0)
932 /* avoid power off hangup */
933 val = bus_read_1(res, cr0_cfg1);
934 bus_write_1(res, cr0_cfg1, val | C1_SRR);
937 scsi_low_restart(slp, SCSI_LOW_RESTART_SOFT,
938 "bus reset (power off?)");
942 /********************************************
944 ********************************************/
948 scsi_low_print(slp, NULL);
949 device_printf(slp->sl_dev, "st %x ist %x\n\n",
953 kdb_enter(KDB_WHY_CAM, "ncv");
956 #endif /* NCV_DEBUG */
958 /********************************************
959 * Reselect or Disconnect or Nexus check
960 ********************************************/
962 if (ireason == INTR_RESELECT)
964 if (ncv_reselected(sc) == EJUSTRETURN)
969 if ((ti = slp->sl_Tnexus) == NULL)
973 if ((status & (STAT_PE | STAT_GE)) != 0)
975 slp->sl_error |= PARITYERR;
976 if ((status & PHASE_MASK) == MESSAGE_IN_PHASE)
977 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_PARITY, 0);
979 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ERROR, 1);
980 derror = SCSI_LOW_DATA_PE;
983 if ((ireason & (INTR_DIS | INTR_ILL)) != 0)
985 if ((ireason & INTR_ILL) == 0)
986 return ncv_disconnected(sc, ti);
988 slp->sl_error |= FATALIO;
989 scsi_low_restart(slp, SCSI_LOW_RESTART_HARD, "illegal cmd");
993 /********************************************
994 * Internal scsi phase
995 ********************************************/
996 switch (ti->ti_phase)
999 scsi_low_arbit_win(slp);
1000 SCSI_LOW_SETUP_PHASE(ti, PH_SELECTED);
1002 if (sc->sc_selstop == 0)
1005 * Here scsi phases expected are
1007 * MSGIN : target wants to disconnect the host.
1008 * STATUSIN : immediate command completed.
1009 * CMD PHASE : command out failed
1010 * MSGOUT : identify command failed.
1012 if ((status & PHASE_MASK) != MESSAGE_OUT_PHASE)
1017 if ((status & PHASE_MASK) != MESSAGE_OUT_PHASE)
1019 if ((ireason & INTR_FC) != 0)
1021 SCSI_LOW_ASSERT_ATN(slp);
1024 SCSI_LOW_SETUP_PHASE(ti, PH_MSGOUT);
1028 ncv_target_nexus_establish(sc);
1029 if ((status & PHASE_MASK) != MESSAGE_IN_PHASE)
1031 device_printf(slp->sl_dev,
1032 "unexpected phase after reselect\n");
1033 slp->sl_error |= FATALIO;
1034 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 1);
1040 if ((slp->sl_flags & HW_PDMASTART) != 0)
1042 ncv_pdma_end(sc, ti);
1047 /********************************************
1048 * Scsi phase sequencer
1049 ********************************************/
1050 switch (status & PHASE_MASK)
1052 case DATA_OUT_PHASE: /* data out */
1053 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1054 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_WRITE) != 0)
1056 scsi_low_attention(slp);
1059 if (slp->sl_scp.scp_datalen <= 0)
1061 if ((ireason & INTR_BS) == 0)
1064 if ((slp->sl_error & PDMAERR) == 0)
1065 device_printf(slp->sl_dev, "data underrun\n");
1066 slp->sl_error |= PDMAERR;
1068 if ((slp->sl_flags & HW_WRITE_PADDING) != 0)
1070 u_int8_t padding[NCV_PADDING_SIZE];
1072 bzero(padding, sizeof(padding));
1073 ncv_pio_write(sc, padding, sizeof(padding));
1077 device_printf(slp->sl_dev,
1078 "write padding required\n");
1083 len = slp->sl_scp.scp_datalen;
1084 if ((ncv_io_control & NCV_WRITE_INTERRUPTS_DRIVEN) != 0)
1086 if (len > ncv_data_write_bytes)
1087 len = ncv_data_write_bytes;
1089 ncv_pio_write(sc, slp->sl_scp.scp_data, len);
1093 case DATA_IN_PHASE: /* data in */
1094 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1095 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_READ) != 0)
1097 scsi_low_attention(slp);
1100 if (slp->sl_scp.scp_datalen <= 0)
1102 if ((ireason & INTR_BS) == 0)
1105 if ((slp->sl_error & PDMAERR) == 0)
1106 device_printf(slp->sl_dev, "data overrun\n");
1107 slp->sl_error |= PDMAERR;
1109 if ((slp->sl_flags & HW_READ_PADDING) != 0)
1111 u_int8_t padding[NCV_PADDING_SIZE];
1113 ncv_pio_read(sc, padding, sizeof(padding));
1117 device_printf(slp->sl_dev,
1118 "read padding required\n");
1124 len = slp->sl_scp.scp_datalen;
1125 if ((ncv_io_control & NCV_READ_INTERRUPTS_DRIVEN) != 0)
1127 if (len > ncv_data_read_bytes)
1128 len = ncv_data_read_bytes;
1130 ncv_pio_read(sc, slp->sl_scp.scp_data, len);
1134 case COMMAND_PHASE: /* cmd out */
1135 SCSI_LOW_SETUP_PHASE(ti, PH_CMD);
1136 if (scsi_low_cmd(slp, ti) != 0)
1138 scsi_low_attention(slp);
1141 bus_write_1(res, cr0_cmd, CMD_FLUSH);
1143 slp->sl_scp.scp_cmd, slp->sl_scp.scp_cmdlen);
1144 bus_write_1(res, cr0_cmd, CMD_TRANS);
1147 case STATUS_PHASE: /* status in */
1148 SCSI_LOW_SETUP_PHASE(ti, PH_STAT);
1149 bus_write_1(res, cr0_cmd, CMD_FLUSH);
1150 bus_write_1(res, cr0_cmd, CMD_ICCS);
1157 case MESSAGE_OUT_PHASE: /* msg out */
1158 SCSI_LOW_SETUP_PHASE(ti, PH_MSGOUT);
1159 bus_write_1(res, cr0_cmd, CMD_FLUSH);
1161 flags = SCSI_LOW_MSGOUT_UNIFY;
1162 if (ti->ti_ophase != ti->ti_phase)
1163 flags |= SCSI_LOW_MSGOUT_INIT;
1164 len = scsi_low_msgout(slp, ti, flags);
1166 if (len > 1 && slp->sl_atten == 0)
1168 scsi_low_attention(slp);
1171 ncvhw_fpush(res, ti->ti_msgoutstr, len);
1172 bus_write_1(res, cr0_cmd, CMD_TRANS);
1173 SCSI_LOW_DEASSERT_ATN(slp);
1176 case MESSAGE_IN_PHASE: /* msg in */
1177 SCSI_LOW_SETUP_PHASE(ti, PH_MSGIN);
1179 len = bus_read_1(res, cr0_sffl) & CR0_SFFLR_BMASK;
1180 if (sc->sc_compseq != 0)
1183 if ((ireason & INTR_FC) && len == 2)
1185 regv = bus_read_1(res, cr0_sfifo);
1186 scsi_low_statusin(slp, ti, regv | derror);
1191 slp->sl_error |= FATALIO;
1192 scsi_low_assert_msg(slp, ti,
1193 SCSI_LOW_MSG_ABORT, 1);
1194 bus_write_1(res, cr0_cmd, CMD_MSGOK);
1198 else if (ireason & INTR_BS)
1200 bus_write_1(res, cr0_cmd, CMD_FLUSH);
1201 bus_write_1(res, cr0_cmd, CMD_TRANS);
1202 if ((ncv_io_control & NCV_FAST_INTERRUPTS) != 0)
1204 if (ncv_catch_intr(sc) == 0)
1210 if ((ireason & INTR_FC) && len == 1)
1212 regv = bus_read_1(res, cr0_sfifo);
1213 if (scsi_low_msgin(slp, ti, regv | derror) == 0)
1215 if (scsi_low_is_msgout_continue(ti, 0) != 0)
1217 scsi_low_attention(slp);
1220 bus_write_1(res, cr0_cmd, CMD_MSGOK);
1221 if ((ncv_io_control & NCV_FAST_INTERRUPTS) != 0)
1224 * clear a pending interrupt and sync with
1232 slp->sl_error |= FATALIO;
1233 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 1);
1234 bus_write_1(res, cr0_cmd, CMD_MSGOK);