1 /* $NecBSD: bshw_machdep.c,v 1.8.12.6 2001/06/29 06:28:05 honda Exp $ */
8 * [NetBSD for NEC PC-98 series]
9 * Copyright (c) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
10 * NetBSD/pc98 porting staff. All rights reserved.
12 * Copyright (c) 1994, 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.
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #if defined(__FreeBSD__) && __FreeBSD_version > 500001
46 #endif /* __ FreeBSD__ */
48 #include <sys/queue.h>
49 #include <sys/malloc.h>
50 #include <sys/errno.h>
55 #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>
70 #include <dev/ic/wd33c93reg.h>
71 #include <i386/Cbus/dev/ct/ctvar.h>
72 #include <i386/Cbus/dev/ct/ct_machdep.h>
73 #include <i386/Cbus/dev/ct/bshwvar.h>
74 #endif /* __NetBSD__ */
77 #include <machine/bus.h>
78 #include <machine/md_var.h>
80 #include <compat/netbsd/dvcfg.h>
81 #include <compat/netbsd/physio_proc.h>
83 #include <cam/scsi/scsi_low.h>
85 #include <dev/ic/wd33c93reg.h>
86 #include <dev/ct/ctvar.h>
87 #include <dev/ct/ct_machdep.h>
88 #include <dev/ct/bshwvar.h>
91 #endif /* __FreeBSD__ */
93 #define BSHW_IO_CONTROL_FLAGS 0
95 u_int bshw_io_control = BSHW_IO_CONTROL_FLAGS;
96 int bshw_data_read_bytes = 4096;
97 int bshw_data_write_bytes = 4096;
99 /*********************************************************
101 *********************************************************/
103 #define BSHW_PAGE_SIZE NBPG
104 #endif /* __NetBSD__ */
107 #define BSHW_PAGE_SIZE PAGE_SIZE
108 typedef unsigned long vaddr_t;
109 #endif /* __FreeBSD__ */
111 /*********************************************************
112 * GENERIC MACHDEP FUNCTIONS
113 *********************************************************/
115 bshw_synch_setup(ct, ti)
117 struct targ_info *ti;
119 struct ct_bus_access_handle *chp = &ct->sc_ch;
120 struct ct_targ_info *cti = (void *) ti;
121 struct bshw_softc *bs = ct->ct_hw;
122 struct bshw *hw = bs->sc_hw;
124 if (hw->hw_sregaddr == 0)
127 ct_cr_write_1(chp, hw->hw_sregaddr + ti->ti_id, cti->cti_syncreg);
128 if (hw->hw_flags & BSHW_DOUBLE_DMACHAN)
130 ct_cr_write_1(chp, hw->hw_sregaddr + ti->ti_id + 8,
139 struct scsi_low_softc *slp = &ct->sc_sclow;
140 struct ct_bus_access_handle *chp = &ct->sc_ch;
141 struct bshw_softc *bs = ct->ct_hw;
142 struct bshw *hw = bs->sc_hw;
147 /* open hardware busmaster mode */
148 if (hw->hw_dma_init != NULL && ((*hw->hw_dma_init)(ct)) != 0)
150 printf("%s: change mode using external DMA (%x)\n",
151 slp->sl_xname, (u_int)ct_cr_read_1(chp, 0x37));
154 /* clear hardware synch registers */
155 offs = hw->hw_sregaddr;
158 for (i = 0; i < 8; i ++, offs ++)
160 ct_cr_write_1(chp, offs, 0);
161 if ((hw->hw_flags & BSHW_DOUBLE_DMACHAN) != 0)
162 ct_cr_write_1(chp, offs + 8, 0);
166 /* disable interrupt & assert reset */
167 regv = ct_cr_read_1(chp, wd3s_mbank);
170 ct_cr_write_1(chp, wd3s_mbank, regv);
172 SCSI_LOW_DELAY(500000);
174 /* reset signal off */
176 ct_cr_write_1(chp, wd3s_mbank, regv);
178 /* interrupt enable */
180 ct_cr_write_1(chp, wd3s_mbank, regv);
185 bshw_read_settings(chp, bs)
186 struct ct_bus_access_handle *chp;
187 struct bshw_softc *bs;
189 static int irq_tbl[] = { 3, 5, 6, 9, 12, 13 };
191 bs->sc_hostid = (ct_cr_read_1(chp, wd3s_auxc) & AUXCR_HIDM);
192 bs->sc_irq = irq_tbl[(ct_cr_read_1(chp, wd3s_auxc) >> 3) & 7];
193 bs->sc_drq = ct_cmdp_read_1(chp) & 3;
197 /*********************************************************
198 * DMA PIO TRANSFER (SMIT)
199 *********************************************************/
200 #define LC_SMIT_TIMEOUT 2 /* 2 sec: timeout for a fifo status ready */
201 #define LC_SMIT_OFFSET 0x1000
202 #define LC_FSZ DEV_BSIZE
204 #define LC_REST (LC_FSZ - LC_SFSZ)
206 #define BSHW_LC_FSET 0x36
207 #define BSHW_LC_FCTRL 0x44
208 #define FCTRL_EN 0x01
209 #define FCTRL_WRITE 0x02
211 #define SF_ABORT 0x08
214 static __inline void bshw_lc_smit_start(struct ct_softc *, int, u_int);
215 static __inline void bshw_lc_smit_stop(struct ct_softc *);
216 static int bshw_lc_smit_fstat(struct ct_softc *, int, int);
219 bshw_lc_smit_stop(ct)
222 struct ct_bus_access_handle *chp = &ct->sc_ch;
224 ct_cr_write_1(chp, BSHW_LC_FCTRL, 0);
225 ct_cmdp_write_1(chp, CMDP_DMER);
229 bshw_lc_smit_start(ct, count, direction)
234 struct ct_bus_access_handle *chp = &ct->sc_ch;
237 val = ct_cr_read_1(chp, BSHW_LC_FSET);
238 cthw_set_count(chp, count);
241 if (direction == SCSI_LOW_WRITE)
242 pval |= (val & 0xe0) | FCTRL_WRITE;
243 ct_cr_write_1(chp, BSHW_LC_FCTRL, pval);
244 ct_cr_write_1(chp, wd3s_cmd, WD3S_TFR_INFO);
248 bshw_lc_smit_fstat(ct, wc, read)
252 struct ct_bus_access_handle *chp = &ct->sc_ch;
257 chp->ch_bus_weight(chp);
258 stat = ct_cmdp_read_1(chp);
259 if (read == SCSI_LOW_READ)
261 if ((stat & SF_RDY) != 0)
263 if ((stat & SF_ABORT) != 0)
268 if ((stat & SF_ABORT) != 0)
270 if ((stat & SF_RDY) != 0)
275 printf("%s: SMIT fifo status timeout\n", ct->sc_sclow.sl_xname);
280 bshw_smit_xfer_stop(ct)
283 struct scsi_low_softc *slp = &ct->sc_sclow;
284 struct bshw_softc *bs = ct->ct_hw;
285 struct targ_info *ti;
286 struct sc_p *sp = &slp->sl_scp;
289 bshw_lc_smit_stop(ct);
295 if (ti->ti_phase == PH_DATA)
297 count = cthw_get_count(&ct->sc_ch);
298 if (count < bs->sc_sdatalen)
300 if (sp->scp_direction == SCSI_LOW_READ &&
301 count != bs->sc_edatalen)
304 count = bs->sc_sdatalen - count;
305 if (count > (u_int) sp->scp_datalen)
308 sp->scp_data += count;
309 sp->scp_datalen -= count;
311 else if (count > bs->sc_sdatalen)
314 printf("%s: smit_xfer_end: cnt error\n", slp->sl_xname);
315 slp->sl_error |= PDMAERR;
317 scsi_low_data_finish(slp);
321 printf("%s: smit_xfer_end: phase miss\n", slp->sl_xname);
322 slp->sl_error |= PDMAERR;
327 bshw_smit_xfer_start(ct)
330 struct scsi_low_softc *slp = &ct->sc_sclow;
331 struct ct_bus_access_handle *chp = &ct->sc_ch;
332 struct bshw_softc *bs = ct->ct_hw;
333 struct sc_p *sp = &slp->sl_scp;
334 struct targ_info *ti = slp->sl_Tnexus;
335 struct ct_targ_info *cti = (void *) ti;
336 u_int datalen, count, io_control;
340 io_control = bs->sc_io_control | bshw_io_control;
341 if ((io_control & BSHW_SMIT_BLOCK) != 0)
344 if ((slp->sl_scp.scp_datalen % DEV_BSIZE) != 0)
347 datalen = sp->scp_datalen;
348 if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
350 if ((io_control & BSHW_READ_INTERRUPT_DRIVEN) != 0 &&
351 datalen > bshw_data_read_bytes)
352 datalen = bshw_data_read_bytes;
356 if ((io_control & BSHW_WRITE_INTERRUPT_DRIVEN) != 0 &&
357 datalen > bshw_data_write_bytes)
358 datalen = bshw_data_write_bytes;
361 bs->sc_sdatalen = datalen;
363 wc = LC_SMIT_TIMEOUT * 1024 * 1024;
365 ct_cr_write_1(chp, wd3s_ctrl, ct->sc_creg | CR_DMA);
366 bshw_lc_smit_start(ct, datalen, sp->scp_direction);
368 if (sp->scp_direction == SCSI_LOW_READ)
372 if (bshw_lc_smit_fstat(ct, wc, SCSI_LOW_READ))
375 count = (datalen > LC_FSZ ? LC_FSZ : datalen);
376 bus_space_read_region_4(chp->ch_memt, chp->ch_memh,
377 LC_SMIT_OFFSET, (u_int32_t *) data, count >> 2);
383 bs->sc_edatalen = datalen;
389 if (bshw_lc_smit_fstat(ct, wc, SCSI_LOW_WRITE))
391 if (cti->cti_syncreg == 0)
394 * If async transfer, reconfirm a scsi phase
395 * again. Unless C bus might hang up.
397 if (bshw_lc_smit_fstat(ct, wc, SCSI_LOW_WRITE))
401 count = (datalen > LC_SFSZ ? LC_SFSZ : datalen);
402 bus_space_write_region_4(chp->ch_memt, chp->ch_memh,
403 LC_SMIT_OFFSET, (u_int32_t *) data, count >> 2);
407 if (bshw_lc_smit_fstat(ct, wc, SCSI_LOW_WRITE))
410 count = (datalen > LC_REST ? LC_REST : datalen);
411 bus_space_write_region_4(chp->ch_memt, chp->ch_memh,
412 LC_SMIT_OFFSET + LC_SFSZ,
413 (u_int32_t *) data, count >> 2);
422 /*********************************************************
424 *********************************************************/
425 static __inline void bshw_dma_write_1 \
426 (struct ct_bus_access_handle *, bus_addr_t, u_int8_t);
427 static void bshw_dmastart(struct ct_softc *);
428 static void bshw_dmadone(struct ct_softc *);
431 bshw_dma_xfer_start(ct)
434 struct scsi_low_softc *slp = &ct->sc_sclow;
435 struct sc_p *sp = &slp->sl_scp;
436 struct ct_bus_access_handle *chp = &ct->sc_ch;
437 struct bshw_softc *bs = ct->ct_hw;
438 vaddr_t va, endva, phys, nphys;
441 io_control = bs->sc_io_control | bshw_io_control;
442 if ((io_control & BSHW_DMA_BLOCK) != 0 && sp->scp_datalen < 256)
445 ct_cr_write_1(chp, wd3s_ctrl, ct->sc_creg | CR_DMA);
446 phys = vtophys((vaddr_t) sp->scp_data);
447 if (phys >= bs->sc_minphys)
450 bs->sc_segaddr = bs->sc_bounce_phys;
452 bs->sc_seglen = sp->scp_datalen;
453 if (bs->sc_seglen > bs->sc_bounce_size)
454 bs->sc_seglen = bs->sc_bounce_size;
456 bs->sc_bufp = bs->sc_bounce_addr;
457 if (sp->scp_direction == SCSI_LOW_WRITE)
458 bcopy(sp->scp_data, bs->sc_bufp, bs->sc_seglen);
463 bs->sc_segaddr = (u_int8_t *) phys;
465 endva = (vaddr_t) round_page((vaddr_t) sp->scp_data + sp->scp_datalen);
466 for (va = (vaddr_t) sp->scp_data; ; phys = nphys)
468 if ((va += BSHW_PAGE_SIZE) >= endva)
470 bs->sc_seglen = sp->scp_datalen;
475 if (phys + BSHW_PAGE_SIZE != nphys || nphys >= bs->sc_minphys)
478 (u_int8_t *) trunc_page(va) - sp->scp_data;
487 cthw_set_count(chp, bs->sc_seglen);
488 ct_cr_write_1(chp, wd3s_cmd, WD3S_TFR_INFO);
493 bshw_dma_xfer_stop(ct)
496 struct scsi_low_softc *slp = &ct->sc_sclow;
497 struct sc_p *sp = &slp->sl_scp;
498 struct bshw_softc *bs = ct->ct_hw;
499 struct targ_info *ti;
500 u_int count, transbytes;
508 if (ti->ti_phase == PH_DATA)
510 count = cthw_get_count(&ct->sc_ch);
511 if (count < (u_int) bs->sc_seglen)
513 transbytes = bs->sc_seglen - count;
514 if (bs->sc_bufp != NULL &&
515 sp->scp_direction == SCSI_LOW_READ)
516 bcopy(bs->sc_bufp, sp->scp_data, transbytes);
518 sp->scp_data += transbytes;
519 sp->scp_datalen -= transbytes;
521 else if (count > (u_int) bs->sc_seglen)
523 printf("%s: port data %x != seglen %x\n",
524 slp->sl_xname, count, bs->sc_seglen);
525 slp->sl_error |= PDMAERR;
528 scsi_low_data_finish(slp);
532 printf("%s: extra DMA interrupt\n", slp->sl_xname);
533 slp->sl_error |= PDMAERR;
539 /* common dma settings */
541 #define DMA1_SMSK (0x15)
543 #define DMA1_MODE (0x17)
545 #define DMA1_FFC (0x19)
547 #define DMA1_CHN(c) (0x01 + ((c) << 2))
549 #define DMA37SM_SET 0x04
550 #define DMA37MD_WRITE 0x04
551 #define DMA37MD_READ 0x08
552 #define DMA37MD_SINGLE 0x40
554 static bus_addr_t dmapageport[4] = { 0x27, 0x21, 0x23, 0x25 };
557 bshw_dma_write_1(chp, port, val)
558 struct ct_bus_access_handle *chp;
571 struct scsi_low_softc *slp = &ct->sc_sclow;
572 struct bshw_softc *bs = ct->ct_hw;
573 struct ct_bus_access_handle *chp = &ct->sc_ch;
574 int chan = bs->sc_drq;
576 u_int8_t regv, *phys = bs->sc_segaddr;
577 u_int nbytes = bs->sc_seglen;
579 /* flush cpu cache */
580 (*bs->sc_dmasync_before) (ct);
583 * Program one of DMA channels 0..3. These are
584 * byte mode channels.
586 /* set dma channel mode, and reset address ff */
588 if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
589 regv = DMA37MD_WRITE | DMA37MD_SINGLE | chan;
591 regv = DMA37MD_READ | DMA37MD_SINGLE | chan;
593 bshw_dma_write_1(chp, DMA1_MODE, regv);
594 bshw_dma_write_1(chp, DMA1_FFC, 0);
596 /* send start address */
597 waport = DMA1_CHN(chan);
598 bshw_dma_write_1(chp, waport, (u_int) phys);
599 bshw_dma_write_1(chp, waport, ((u_int) phys) >> 8);
600 bshw_dma_write_1(chp, dmapageport[chan], ((u_int) phys) >> 16);
603 bshw_dma_write_1(chp, waport + 2, --nbytes);
604 bshw_dma_write_1(chp, waport + 2, nbytes >> 8);
606 /* vendor unique hook */
607 if (bs->sc_hw->hw_dma_start)
608 (*bs->sc_hw->hw_dma_start)(ct);
610 bshw_dma_write_1(chp, DMA1_SMSK, chan);
611 ct_cmdp_write_1(chp, CMDP_DMES);
618 struct bshw_softc *bs = ct->ct_hw;
619 struct ct_bus_access_handle *chp = &ct->sc_ch;
621 bshw_dma_write_1(chp, DMA1_SMSK, (bs->sc_drq | DMA37SM_SET));
622 ct_cmdp_write_1(chp, CMDP_DMER);
624 /* vendor unique hook */
625 if (bs->sc_hw->hw_dma_stop)
626 (*bs->sc_hw->hw_dma_stop) (ct);
628 /* flush cpu cache */
629 (*bs->sc_dmasync_after) (ct);
632 /**********************************************
633 * VENDOR UNIQUE DMA FUNCS
634 **********************************************/
635 static int bshw_dma_init_sc98(struct ct_softc *);
636 static void bshw_dma_start_sc98(struct ct_softc *);
637 static void bshw_dma_stop_sc98(struct ct_softc *);
638 static int bshw_dma_init_texa(struct ct_softc *);
639 static void bshw_dma_start_elecom(struct ct_softc *);
640 static void bshw_dma_stop_elecom(struct ct_softc *);
643 bshw_dma_init_texa(ct)
646 struct ct_bus_access_handle *chp = &ct->sc_ch;
649 if ((regval = ct_cr_read_1(chp, 0x37)) & 0x08)
652 ct_cr_write_1(chp, 0x37, regval | 0x08);
653 regval = ct_cr_read_1(chp, 0x3f);
654 ct_cr_write_1(chp, 0x3f, regval | 0x08);
659 bshw_dma_init_sc98(ct)
662 struct ct_bus_access_handle *chp = &ct->sc_ch;
664 if (ct_cr_read_1(chp, 0x37) & 0x08)
667 /* If your card is SC98 with bios ver 1.01 or 1.02 under no PCI */
668 ct_cr_write_1(chp, 0x37, 0x1a);
669 ct_cr_write_1(chp, 0x3f, 0x1a);
671 /* only valid for IO */
672 ct_cr_write_1(chp, 0x40, 0xf4);
673 ct_cr_write_1(chp, 0x41, 0x9);
674 ct_cr_write_1(chp, 0x43, 0xff);
675 ct_cr_write_1(chp, 0x46, 0x4e);
677 ct_cr_write_1(chp, 0x48, 0xf4);
678 ct_cr_write_1(chp, 0x49, 0x9);
679 ct_cr_write_1(chp, 0x4b, 0xff);
680 ct_cr_write_1(chp, 0x4e, 0x4e);
686 bshw_dma_start_sc98(ct)
689 struct ct_bus_access_handle *chp = &ct->sc_ch;
691 ct_cr_write_1(chp, 0x73, 0x32);
692 ct_cr_write_1(chp, 0x74, 0x23);
696 bshw_dma_stop_sc98(ct)
699 struct ct_bus_access_handle *chp = &ct->sc_ch;
701 ct_cr_write_1(chp, 0x73, 0x43);
702 ct_cr_write_1(chp, 0x74, 0x34);
706 bshw_dma_start_elecom(ct)
709 struct ct_bus_access_handle *chp = &ct->sc_ch;
710 u_int8_t tmp = ct_cr_read_1(chp, 0x4c);
712 ct_cr_write_1(chp, 0x32, tmp & 0xdf);
716 bshw_dma_stop_elecom(ct)
719 struct ct_bus_access_handle *chp = &ct->sc_ch;
720 u_int8_t tmp = ct_cr_read_1(chp, 0x4c);
722 ct_cr_write_1(chp, 0x32, tmp | 0x20);
725 static struct bshw bshw_generic = {
735 static struct bshw bshw_sc98 = {
745 static struct bshw bshw_texa = {
755 static struct bshw bshw_elecom = {
761 bshw_dma_start_elecom,
762 bshw_dma_stop_elecom,
765 static struct bshw bshw_lc_smit = {
766 BSHW_SMFIFO | BSHW_DOUBLE_DMACHAN,
775 static struct bshw bshw_lha20X = {
786 static dvcfg_hw_t bshw_hwsel_array[] = {
787 /* 0x00 */ &bshw_generic,
788 /* 0x01 */ &bshw_sc98,
789 /* 0x02 */ &bshw_texa,
790 /* 0x03 */ &bshw_elecom,
791 /* 0x04 */ &bshw_lc_smit,
792 /* 0x05 */ &bshw_lha20X,
795 struct dvcfg_hwsel bshw_hwsel = {
796 DVCFG_HWSEL_SZ(bshw_hwsel_array),