xref: /netbsd-src/sys/arch/amiga/dev/cbsc.c (revision 40f7eaaf2596ce16adf343e6680d62b6ed6f12dd)
1*40f7eaafSjdolecek /*	$NetBSD: cbsc.c,v 1.35 2019/01/08 19:41:09 jdolecek Exp $ */
266e9e901Smhitch 
366e9e901Smhitch /*
466e9e901Smhitch  * Copyright (c) 1997 Michael L. Hitch
566e9e901Smhitch  * Copyright (c) 1982, 1990 The Regents of the University of California.
666e9e901Smhitch  * All rights reserved.
766e9e901Smhitch  *
866e9e901Smhitch  * Redistribution and use in source and binary forms, with or without
966e9e901Smhitch  * modification, are permitted provided that the following conditions
1066e9e901Smhitch  * are met:
1166e9e901Smhitch  * 1. Redistributions of source code must retain the above copyright
1266e9e901Smhitch  *    notice, this list of conditions and the following disclaimer.
1366e9e901Smhitch  * 2. Redistributions in binary form must reproduce the above copyright
1466e9e901Smhitch  *    notice, this list of conditions and the following disclaimer in the
1566e9e901Smhitch  *    documentation and/or other materials provided with the distribution.
1678f74058Ssnj  * 3. Neither the name of the University nor the names of its contributors
1766e9e901Smhitch  *    may be used to endorse or promote products derived from this software
1866e9e901Smhitch  *    without specific prior written permission.
1966e9e901Smhitch  *
2066e9e901Smhitch  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2166e9e901Smhitch  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2266e9e901Smhitch  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2366e9e901Smhitch  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2466e9e901Smhitch  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2566e9e901Smhitch  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2666e9e901Smhitch  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2766e9e901Smhitch  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2866e9e901Smhitch  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2966e9e901Smhitch  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3066e9e901Smhitch  * SUCH DAMAGE.
3166e9e901Smhitch  *
3266e9e901Smhitch  */
3366e9e901Smhitch 
3423b820a8Sphx #ifdef __m68k__
35bd01b4a3Smrg #include "opt_m68k_arch.h"
3623b820a8Sphx #endif
37bd01b4a3Smrg 
381ea4df81Saymeric #include <sys/cdefs.h>
39*40f7eaafSjdolecek __KERNEL_RCSID(0, "$NetBSD: cbsc.c,v 1.35 2019/01/08 19:41:09 jdolecek Exp $");
401ea4df81Saymeric 
4166e9e901Smhitch #include <sys/types.h>
4266e9e901Smhitch #include <sys/param.h>
4366e9e901Smhitch #include <sys/systm.h>
4466e9e901Smhitch #include <sys/kernel.h>
4566e9e901Smhitch #include <sys/errno.h>
4666e9e901Smhitch #include <sys/ioctl.h>
4766e9e901Smhitch #include <sys/device.h>
4866e9e901Smhitch #include <sys/buf.h>
4966e9e901Smhitch #include <sys/proc.h>
5066e9e901Smhitch #include <sys/queue.h>
5166e9e901Smhitch 
5266e9e901Smhitch #include <dev/scsipi/scsi_all.h>
5366e9e901Smhitch #include <dev/scsipi/scsipi_all.h>
5466e9e901Smhitch #include <dev/scsipi/scsiconf.h>
5566e9e901Smhitch #include <dev/scsipi/scsi_message.h>
5666e9e901Smhitch 
5766e9e901Smhitch #include <machine/cpu.h>
5866e9e901Smhitch 
5966e9e901Smhitch #include <dev/ic/ncr53c9xreg.h>
6066e9e901Smhitch #include <dev/ic/ncr53c9xvar.h>
6166e9e901Smhitch 
6266e9e901Smhitch #include <amiga/amiga/isr.h>
6366e9e901Smhitch #include <amiga/dev/cbscvar.h>
6466e9e901Smhitch #include <amiga/dev/zbusvar.h>
6566e9e901Smhitch 
661ee8b50fSis #ifdef __powerpc__
671ee8b50fSis #define badaddr(a)      badaddr_read(a, 2, NULL)
681ee8b50fSis #endif
691ee8b50fSis 
7078a1d236Stsutsui int	cbscmatch(device_t, cfdata_t, void *);
7178a1d236Stsutsui void	cbscattach(device_t, device_t, void *);
7266e9e901Smhitch 
7366e9e901Smhitch /* Linkup to the rest of the kernel */
7478a1d236Stsutsui CFATTACH_DECL_NEW(cbsc, sizeof(struct cbsc_softc),
75c5e91d44Sthorpej     cbscmatch, cbscattach, NULL, NULL);
7666e9e901Smhitch 
7766e9e901Smhitch /*
7866e9e901Smhitch  * Functions and the switch for the MI code.
7966e9e901Smhitch  */
8078a1d236Stsutsui uint8_t	cbsc_read_reg(struct ncr53c9x_softc *, int);
8178a1d236Stsutsui void	cbsc_write_reg(struct ncr53c9x_softc *, int, uint8_t);
829382c873Saymeric int	cbsc_dma_isintr(struct ncr53c9x_softc *);
839382c873Saymeric void	cbsc_dma_reset(struct ncr53c9x_softc *);
849382c873Saymeric int	cbsc_dma_intr(struct ncr53c9x_softc *);
8578a1d236Stsutsui int	cbsc_dma_setup(struct ncr53c9x_softc *, uint8_t **,
869382c873Saymeric 	    size_t *, int, size_t *);
879382c873Saymeric void	cbsc_dma_go(struct ncr53c9x_softc *);
889382c873Saymeric void	cbsc_dma_stop(struct ncr53c9x_softc *);
899382c873Saymeric int	cbsc_dma_isactive(struct ncr53c9x_softc *);
9066e9e901Smhitch 
9166e9e901Smhitch struct ncr53c9x_glue cbsc_glue = {
9266e9e901Smhitch 	cbsc_read_reg,
9366e9e901Smhitch 	cbsc_write_reg,
9466e9e901Smhitch 	cbsc_dma_isintr,
9566e9e901Smhitch 	cbsc_dma_reset,
9666e9e901Smhitch 	cbsc_dma_intr,
9766e9e901Smhitch 	cbsc_dma_setup,
9866e9e901Smhitch 	cbsc_dma_go,
9966e9e901Smhitch 	cbsc_dma_stop,
10066e9e901Smhitch 	cbsc_dma_isactive,
10178a1d236Stsutsui 	NULL,
10266e9e901Smhitch };
10366e9e901Smhitch 
10466e9e901Smhitch /* Maximum DMA transfer length to reduce impact on high-speed serial input */
10566e9e901Smhitch u_long cbsc_max_dma = 1024;
10666e9e901Smhitch extern int ser_open_speed;
10766e9e901Smhitch 
10866e9e901Smhitch u_long cbsc_cnt_pio = 0;	/* number of PIO transfers */
10966e9e901Smhitch u_long cbsc_cnt_dma = 0;	/* number of DMA transfers */
11066e9e901Smhitch u_long cbsc_cnt_dma2 = 0;	/* number of DMA transfers broken up */
11166e9e901Smhitch u_long cbsc_cnt_dma3 = 0;	/* number of pages combined */
11266e9e901Smhitch 
11366e9e901Smhitch #ifdef DEBUG
11466e9e901Smhitch struct {
11578a1d236Stsutsui 	uint8_t hardbits;
11678a1d236Stsutsui 	uint8_t status;
11778a1d236Stsutsui 	uint8_t xx;
11878a1d236Stsutsui 	uint8_t yy;
11966e9e901Smhitch } cbsc_trace[128];
12066e9e901Smhitch int cbsc_trace_ptr = 0;
12166e9e901Smhitch int cbsc_trace_enable = 1;
1229382c873Saymeric void cbsc_dump(void);
12366e9e901Smhitch #endif
12466e9e901Smhitch 
12566e9e901Smhitch /*
12666e9e901Smhitch  * if we are a Phase5 CyberSCSI [mark I?]
12766e9e901Smhitch  */
12866e9e901Smhitch int
cbscmatch(device_t parent,cfdata_t cf,void * aux)12978a1d236Stsutsui cbscmatch(device_t parent, cfdata_t cf, void *aux)
13066e9e901Smhitch {
13166e9e901Smhitch 	struct zbus_args *zap;
13278a1d236Stsutsui 	volatile uint8_t *regs;
13366e9e901Smhitch 
13466e9e901Smhitch 	zap = aux;
135f6adcd46Smhitch 	if (zap->manid != 0x2140)
13678a1d236Stsutsui 		return 0;		/* It's not Phase5 */
137f6adcd46Smhitch 	if (zap->prodid != 12 && zap->prodid != 11)
13878a1d236Stsutsui 		return 0;		/* Not CyberStorm MKI SCSI */
139f6adcd46Smhitch 	if (zap->prodid == 11 && iszthreepa(zap->pa))
14078a1d236Stsutsui 		return 0;		/* Fastlane Z3! */
14178a1d236Stsutsui 	regs = &((volatile uint8_t *)zap->va)[0xf400];
14253524e44Schristos 	if (badaddr((void *)__UNVOLATILE(regs)))
14378a1d236Stsutsui 		return 0;
14466e9e901Smhitch 	regs[NCR_CFG1 * 4] = 0;
14566e9e901Smhitch 	regs[NCR_CFG1 * 4] = NCRCFG1_PARENB | 7;
14666e9e901Smhitch 	delay(5);
14766e9e901Smhitch 	if (regs[NCR_CFG1 * 4] != (NCRCFG1_PARENB | 7))
14878a1d236Stsutsui 		return 0;
14978a1d236Stsutsui 	return 1;
15066e9e901Smhitch }
15166e9e901Smhitch 
15266e9e901Smhitch /*
15366e9e901Smhitch  * Attach this instance, and then all the sub-devices
15466e9e901Smhitch  */
15566e9e901Smhitch void
cbscattach(device_t parent,device_t self,void * aux)15678a1d236Stsutsui cbscattach(device_t parent, device_t self, void *aux)
15766e9e901Smhitch {
15878a1d236Stsutsui 	struct cbsc_softc *csc = device_private(self);
15966e9e901Smhitch 	struct ncr53c9x_softc *sc = &csc->sc_ncr53c9x;
16066e9e901Smhitch 	struct zbus_args  *zap;
16166e9e901Smhitch 	extern u_long scsi_nosync;
16266e9e901Smhitch 	extern int shift_nosync;
16366e9e901Smhitch 	extern int ncr53c9x_debug;
16466e9e901Smhitch 
16566e9e901Smhitch 	/*
16666e9e901Smhitch 	 * Set up the glue for MI code early; we use some of it here.
16766e9e901Smhitch 	 */
16878a1d236Stsutsui 	sc->sc_dev = self;
16966e9e901Smhitch 	sc->sc_glue = &cbsc_glue;
17066e9e901Smhitch 
17166e9e901Smhitch 	/*
17266e9e901Smhitch 	 * Save the regs
17366e9e901Smhitch 	 */
17466e9e901Smhitch 	zap = aux;
17578a1d236Stsutsui 	csc->sc_reg = &((volatile uint8_t *)zap->va)[0xf400];
17666e9e901Smhitch 	csc->sc_dmabase = &csc->sc_reg[0x400];
17766e9e901Smhitch 
178a1f606d3Slukem 	sc->sc_freq = 40;		/* Clocked at 40 MHz */
17966e9e901Smhitch 
18078a1d236Stsutsui 	aprint_normal(": address %p", csc->sc_reg);
18166e9e901Smhitch 
18266e9e901Smhitch 	sc->sc_id = 7;
18366e9e901Smhitch 
18466e9e901Smhitch 	/*
18566e9e901Smhitch 	 * It is necessary to try to load the 2nd config register here,
18666e9e901Smhitch 	 * to find out what rev the FAS chip is, else the ncr53c9x_reset
18766e9e901Smhitch 	 * will not set up the defaults correctly.
18866e9e901Smhitch 	 */
18966e9e901Smhitch 	sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB;
19066e9e901Smhitch 	sc->sc_cfg2 = NCRCFG2_SCSI2 | NCRCFG2_FE;
19166e9e901Smhitch 	sc->sc_cfg3 = 0x08 /*FCLK*/ | NCRESPCFG3_FSCSI | NCRESPCFG3_CDB;
19266e9e901Smhitch 	sc->sc_rev = NCR_VARIANT_FAS216;
19366e9e901Smhitch 
19466e9e901Smhitch 	/*
19566e9e901Smhitch 	 * This is the value used to start sync negotiations
19666e9e901Smhitch 	 * Note that the NCR register "SYNCTP" is programmed
19766e9e901Smhitch 	 * in "clocks per byte", and has a minimum value of 4.
19866e9e901Smhitch 	 * The SCSI period used in negotiation is one-fourth
19966e9e901Smhitch 	 * of the time (in nanoseconds) needed to transfer one byte.
20066e9e901Smhitch 	 * Since the chip's clock is given in MHz, we have the following
20166e9e901Smhitch 	 * formula: 4 * period = (1000 / freq) * 4
20266e9e901Smhitch 	 */
20366e9e901Smhitch 	sc->sc_minsync = 1000 / sc->sc_freq;
20466e9e901Smhitch 
20566e9e901Smhitch 	/*
20666e9e901Smhitch 	 * get flags from -I argument and set cf_flags.
20766e9e901Smhitch 	 * NOTE: low 8 bits are to disable disconnect, and the next
20866e9e901Smhitch 	 *       8 bits are to disable sync.
20966e9e901Smhitch 	 */
21078a1d236Stsutsui 	device_cfdata(self)->cf_flags |= (scsi_nosync >> shift_nosync)
21166e9e901Smhitch 	    & 0xffff;
21266e9e901Smhitch 	shift_nosync += 16;
21366e9e901Smhitch 
21466e9e901Smhitch 	/* Use next 16 bits of -I argument to set ncr53c9x_debug flags */
21566e9e901Smhitch 	ncr53c9x_debug |= (scsi_nosync >> shift_nosync) & 0xffff;
21666e9e901Smhitch 	shift_nosync += 16;
21766e9e901Smhitch 
21866e9e901Smhitch #if 1
21966e9e901Smhitch 	if (((scsi_nosync >> shift_nosync) & 0xff00) == 0xff00)
22066e9e901Smhitch 		sc->sc_minsync = 0;
22166e9e901Smhitch #endif
22266e9e901Smhitch 
22366e9e901Smhitch 	/* Really no limit, but since we want to fit into the TCR... */
22466e9e901Smhitch 	sc->sc_maxxfer = 64 * 1024;
22566e9e901Smhitch 
22666e9e901Smhitch 	/*
22766e9e901Smhitch 	 * Configure interrupts.
22866e9e901Smhitch 	 */
2298c4d1bf1Stsutsui 	csc->sc_isr.isr_intr = ncr53c9x_intr;
23066e9e901Smhitch 	csc->sc_isr.isr_arg  = sc;
23166e9e901Smhitch 	csc->sc_isr.isr_ipl  = 2;
23266e9e901Smhitch 	add_isr(&csc->sc_isr);
23366e9e901Smhitch 
23466e9e901Smhitch 	/*
23566e9e901Smhitch 	 * Now try to attach all the sub-devices
23666e9e901Smhitch 	 */
237937a7a3eSbouyer 	sc->sc_adapter.adapt_request = ncr53c9x_scsipi_request;
238937a7a3eSbouyer 	sc->sc_adapter.adapt_minphys = minphys;
239937a7a3eSbouyer 	ncr53c9x_attach(sc);
24066e9e901Smhitch }
24166e9e901Smhitch 
24266e9e901Smhitch /*
24366e9e901Smhitch  * Glue functions.
24466e9e901Smhitch  */
24566e9e901Smhitch 
24678a1d236Stsutsui uint8_t
cbsc_read_reg(struct ncr53c9x_softc * sc,int reg)2479382c873Saymeric cbsc_read_reg(struct ncr53c9x_softc *sc, int reg)
24866e9e901Smhitch {
24966e9e901Smhitch 	struct cbsc_softc *csc = (struct cbsc_softc *)sc;
25066e9e901Smhitch 
25166e9e901Smhitch 	return csc->sc_reg[reg * 4];
25266e9e901Smhitch }
25366e9e901Smhitch 
25466e9e901Smhitch void
cbsc_write_reg(struct ncr53c9x_softc * sc,int reg,uint8_t val)25578a1d236Stsutsui cbsc_write_reg(struct ncr53c9x_softc *sc, int reg, uint8_t val)
25666e9e901Smhitch {
25766e9e901Smhitch 	struct cbsc_softc *csc = (struct cbsc_softc *)sc;
25878a1d236Stsutsui 	uint8_t v = val;
25966e9e901Smhitch 
26066e9e901Smhitch 	csc->sc_reg[reg * 4] = v;
26166e9e901Smhitch #ifdef DEBUG
262e6c88a76Sthorpej if (cbsc_trace_enable/* && sc->sc_nexus && sc->sc_nexus->xs->xs_control & XS_CTL_POLL*/ &&
26366e9e901Smhitch   reg == NCR_CMD/* && csc->sc_active*/) {
26466e9e901Smhitch   cbsc_trace[(cbsc_trace_ptr - 1) & 127].yy = v;
26566e9e901Smhitch /*  printf(" cmd %x", v);*/
26666e9e901Smhitch }
26766e9e901Smhitch #endif
26866e9e901Smhitch }
26966e9e901Smhitch 
27066e9e901Smhitch int
cbsc_dma_isintr(struct ncr53c9x_softc * sc)2719382c873Saymeric cbsc_dma_isintr(struct ncr53c9x_softc *sc)
27266e9e901Smhitch {
27366e9e901Smhitch 	struct cbsc_softc *csc = (struct cbsc_softc *)sc;
27466e9e901Smhitch 
27566e9e901Smhitch 	if ((csc->sc_reg[NCR_STAT * 4] & NCRSTAT_INT) == 0)
27666e9e901Smhitch 		return 0;
27766e9e901Smhitch 
27866e9e901Smhitch 	if (sc->sc_state == NCR_CONNECTED)
27966e9e901Smhitch 		csc->sc_portbits |= CBSC_PB_LED;
28066e9e901Smhitch 	else
28166e9e901Smhitch 		csc->sc_portbits &= ~CBSC_PB_LED;
28266e9e901Smhitch 	csc->sc_reg[0x802] = csc->sc_portbits;
28366e9e901Smhitch 
28466e9e901Smhitch 	if ((csc->sc_reg[0x802] & CBSC_HB_CREQ) == 0)
28566e9e901Smhitch 		return 0;
28666e9e901Smhitch #ifdef DEBUG
287e6c88a76Sthorpej if (/*sc->sc_nexus && sc->sc_nexus->xs->xs_control & XS_CTL_POLL &&*/ cbsc_trace_enable) {
28866e9e901Smhitch   cbsc_trace[cbsc_trace_ptr].status = csc->sc_reg[NCR_STAT * 4];
28966e9e901Smhitch   cbsc_trace[cbsc_trace_ptr].xx = csc->sc_reg[NCR_CMD * 4];
29066e9e901Smhitch   cbsc_trace[cbsc_trace_ptr].yy = csc->sc_active;
29166e9e901Smhitch   cbsc_trace_ptr = (cbsc_trace_ptr + 1) & 127;
29266e9e901Smhitch }
29366e9e901Smhitch #endif
29466e9e901Smhitch 	return 1;
29566e9e901Smhitch }
29666e9e901Smhitch 
29766e9e901Smhitch void
cbsc_dma_reset(struct ncr53c9x_softc * sc)2989382c873Saymeric cbsc_dma_reset(struct ncr53c9x_softc *sc)
29966e9e901Smhitch {
30066e9e901Smhitch 	struct cbsc_softc *csc = (struct cbsc_softc *)sc;
30166e9e901Smhitch 
30266e9e901Smhitch 	csc->sc_active = 0;
30366e9e901Smhitch }
30466e9e901Smhitch 
30566e9e901Smhitch int
cbsc_dma_intr(struct ncr53c9x_softc * sc)3069382c873Saymeric cbsc_dma_intr(struct ncr53c9x_softc *sc)
30766e9e901Smhitch {
30866e9e901Smhitch 	register struct cbsc_softc *csc = (struct cbsc_softc *)sc;
30966e9e901Smhitch 	register int	cnt;
31066e9e901Smhitch 
31166e9e901Smhitch 	NCR_DMA(("cbsc_dma_intr: cnt %d int %x stat %x fifo %d ",
31266e9e901Smhitch 	    csc->sc_dmasize, sc->sc_espintr, sc->sc_espstat,
31366e9e901Smhitch 	    csc->sc_reg[NCR_FFLAG * 4] & NCRFIFO_FF));
31466e9e901Smhitch 	if (csc->sc_active == 0) {
31566e9e901Smhitch 		printf("cbsc_intr--inactive DMA\n");
31666e9e901Smhitch 		return -1;
31766e9e901Smhitch 	}
31866e9e901Smhitch 
31966e9e901Smhitch 	/* update sc_dmaaddr and sc_pdmalen */
32066e9e901Smhitch 	cnt = csc->sc_reg[NCR_TCL * 4];
32166e9e901Smhitch 	cnt += csc->sc_reg[NCR_TCM * 4] << 8;
32266e9e901Smhitch 	cnt += csc->sc_reg[NCR_TCH * 4] << 16;
32366e9e901Smhitch 	if (!csc->sc_datain) {
32466e9e901Smhitch 		cnt += csc->sc_reg[NCR_FFLAG * 4] & NCRFIFO_FF;
32566e9e901Smhitch 		csc->sc_reg[NCR_CMD * 4] = NCRCMD_FLUSH;
32666e9e901Smhitch 	}
32766e9e901Smhitch 	cnt = csc->sc_dmasize - cnt;	/* number of bytes transferred */
32866e9e901Smhitch 	NCR_DMA(("DMA xferred %d\n", cnt));
32966e9e901Smhitch 	if (csc->sc_xfr_align) {
33078a1d236Stsutsui 		memcpy(*csc->sc_dmaaddr, csc->sc_alignbuf, cnt);
33166e9e901Smhitch 		csc->sc_xfr_align = 0;
33266e9e901Smhitch 	}
33366e9e901Smhitch 	*csc->sc_dmaaddr += cnt;
33466e9e901Smhitch 	*csc->sc_pdmalen -= cnt;
33566e9e901Smhitch 	csc->sc_active = 0;
33666e9e901Smhitch 	return 0;
33766e9e901Smhitch }
33866e9e901Smhitch 
33966e9e901Smhitch int
cbsc_dma_setup(struct ncr53c9x_softc * sc,uint8_t ** addr,size_t * len,int datain,size_t * dmasize)34078a1d236Stsutsui cbsc_dma_setup(struct ncr53c9x_softc *sc, uint8_t **addr, size_t *len,
3419382c873Saymeric                int datain, size_t *dmasize)
34266e9e901Smhitch {
34366e9e901Smhitch 	struct cbsc_softc *csc = (struct cbsc_softc *)sc;
344744246faSis 	paddr_t pa;
34578a1d236Stsutsui 	uint8_t *ptr;
34666e9e901Smhitch 	size_t xfer;
34766e9e901Smhitch 
34878a1d236Stsutsui 	csc->sc_dmaaddr = addr;
34966e9e901Smhitch 	csc->sc_pdmalen = len;
35066e9e901Smhitch 	csc->sc_datain = datain;
35166e9e901Smhitch 	csc->sc_dmasize = *dmasize;
35266e9e901Smhitch 	/*
35366e9e901Smhitch 	 * DMA can be nasty for high-speed serial input, so limit the
35466e9e901Smhitch 	 * size of this DMA operation if the serial port is running at
35566e9e901Smhitch 	 * a high speed (higher than 19200 for now - should be adjusted
356d20841bbSwiz 	 * based on CPU type and speed?).
35766e9e901Smhitch 	 * XXX - add serial speed check XXX
35866e9e901Smhitch 	 */
35966e9e901Smhitch 	if (ser_open_speed > 19200 && cbsc_max_dma != 0 &&
36066e9e901Smhitch 	    csc->sc_dmasize > cbsc_max_dma)
36166e9e901Smhitch 		csc->sc_dmasize = cbsc_max_dma;
36266e9e901Smhitch 	ptr = *addr;			/* Kernel virtual address */
36366e9e901Smhitch 	pa = kvtop(ptr);		/* Physical address of DMA */
364d1579b2dSriastradh 	xfer = uimin(csc->sc_dmasize, PAGE_SIZE - (pa & (PAGE_SIZE - 1)));
36566e9e901Smhitch 	csc->sc_xfr_align = 0;
36666e9e901Smhitch 	/*
36766e9e901Smhitch 	 * If output and unaligned, stuff odd byte into FIFO
36866e9e901Smhitch 	 */
36966e9e901Smhitch 	if (datain == 0 && (int)ptr & 1) {
37066e9e901Smhitch 		NCR_DMA(("cbsc_dma_setup: align byte written to fifo\n"));
37166e9e901Smhitch 		pa++;
37266e9e901Smhitch 		xfer--;			/* XXXX CHECK THIS !!!! XXXX */
37366e9e901Smhitch 		csc->sc_reg[NCR_FIFO * 4] = *ptr++;
37466e9e901Smhitch 	}
37566e9e901Smhitch 	/*
37666e9e901Smhitch 	 * If unaligned address, read unaligned bytes into alignment buffer
37766e9e901Smhitch 	 */
37866e9e901Smhitch 	else if ((int)ptr & 1) {
37953524e44Schristos 		pa = kvtop((void *)&csc->sc_alignbuf);
380d1579b2dSriastradh 		xfer = csc->sc_dmasize = uimin(xfer, sizeof(csc->sc_alignbuf));
38166e9e901Smhitch 		NCR_DMA(("cbsc_dma_setup: align read by %d bytes\n", xfer));
38266e9e901Smhitch 		csc->sc_xfr_align = 1;
38366e9e901Smhitch 	}
38466e9e901Smhitch ++cbsc_cnt_dma;		/* number of DMA operations */
38566e9e901Smhitch 
38666e9e901Smhitch 	while (xfer < csc->sc_dmasize) {
38778a1d236Stsutsui 		if ((pa + xfer) != kvtop(*addr + xfer))
38866e9e901Smhitch 			break;
3898818afa4Sthorpej 		if ((csc->sc_dmasize - xfer) < PAGE_SIZE)
39066e9e901Smhitch 			xfer = csc->sc_dmasize;
39166e9e901Smhitch 		else
3928818afa4Sthorpej 			xfer += PAGE_SIZE;
39366e9e901Smhitch ++cbsc_cnt_dma3;
39466e9e901Smhitch 	}
39566e9e901Smhitch if (xfer != *len)
39666e9e901Smhitch   ++cbsc_cnt_dma2;
39766e9e901Smhitch 
39866e9e901Smhitch 	csc->sc_dmasize = xfer;
39966e9e901Smhitch 	*dmasize = csc->sc_dmasize;
40066e9e901Smhitch 	csc->sc_pa = pa;
40166e9e901Smhitch #if defined(M68040) || defined(M68060)
40266e9e901Smhitch 	if (mmutype == MMU_68040) {
40366e9e901Smhitch 		if (csc->sc_xfr_align) {
40466e9e901Smhitch 			dma_cachectl(csc->sc_alignbuf,
40566e9e901Smhitch 			    sizeof(csc->sc_alignbuf));
40666e9e901Smhitch 		}
40766e9e901Smhitch 		else
40866e9e901Smhitch 			dma_cachectl(*csc->sc_dmaaddr, csc->sc_dmasize);
40966e9e901Smhitch 	}
41066e9e901Smhitch #endif
41166e9e901Smhitch 
41266e9e901Smhitch 	if (csc->sc_datain)
41366e9e901Smhitch 		pa &= ~1;
41466e9e901Smhitch 	else
41566e9e901Smhitch 		pa |= 1;
41678a1d236Stsutsui 	csc->sc_dmabase[0] = (uint8_t)(pa >> 24);
41778a1d236Stsutsui 	csc->sc_dmabase[2] = (uint8_t)(pa >> 16);
41878a1d236Stsutsui 	csc->sc_dmabase[4] = (uint8_t)(pa >> 8);
41978a1d236Stsutsui 	csc->sc_dmabase[6] = (uint8_t)(pa);
42066e9e901Smhitch 	if (csc->sc_datain)
42166e9e901Smhitch 		csc->sc_portbits &= ~CBSC_PB_WRITE;
42266e9e901Smhitch 	else
42366e9e901Smhitch 		csc->sc_portbits |= CBSC_PB_WRITE;
42466e9e901Smhitch 	csc->sc_reg[0x802] = csc->sc_portbits;
42566e9e901Smhitch 	csc->sc_active = 1;
42666e9e901Smhitch 	return 0;
42766e9e901Smhitch }
42866e9e901Smhitch 
42966e9e901Smhitch void
cbsc_dma_go(struct ncr53c9x_softc * sc)4309382c873Saymeric cbsc_dma_go(struct ncr53c9x_softc *sc)
43166e9e901Smhitch {
43266e9e901Smhitch }
43366e9e901Smhitch 
43466e9e901Smhitch void
cbsc_dma_stop(struct ncr53c9x_softc * sc)4359382c873Saymeric cbsc_dma_stop(struct ncr53c9x_softc *sc)
43666e9e901Smhitch {
43766e9e901Smhitch }
43866e9e901Smhitch 
43966e9e901Smhitch int
cbsc_dma_isactive(struct ncr53c9x_softc * sc)4409382c873Saymeric cbsc_dma_isactive(struct ncr53c9x_softc *sc)
44166e9e901Smhitch {
44266e9e901Smhitch 	struct cbsc_softc *csc = (struct cbsc_softc *)sc;
44366e9e901Smhitch 
44466e9e901Smhitch 	return csc->sc_active;
44566e9e901Smhitch }
44666e9e901Smhitch 
44766e9e901Smhitch #ifdef DEBUG
44866e9e901Smhitch void
cbsc_dump(void)4499382c873Saymeric cbsc_dump(void)
45066e9e901Smhitch {
45166e9e901Smhitch 	int i;
45266e9e901Smhitch 
45366e9e901Smhitch 	i = cbsc_trace_ptr;
45466e9e901Smhitch 	printf("cbsc_trace dump: ptr %x\n", cbsc_trace_ptr);
45566e9e901Smhitch 	do {
45666e9e901Smhitch 		if (cbsc_trace[i].hardbits == 0) {
45766e9e901Smhitch 			i = (i + 1) & 127;
45866e9e901Smhitch 			continue;
45966e9e901Smhitch 		}
46066e9e901Smhitch 		printf("%02x%02x%02x%02x(", cbsc_trace[i].hardbits,
46166e9e901Smhitch 		    cbsc_trace[i].status, cbsc_trace[i].xx, cbsc_trace[i].yy);
46266e9e901Smhitch 		if (cbsc_trace[i].status & NCRSTAT_INT)
46366e9e901Smhitch 			printf("NCRINT/");
46466e9e901Smhitch 		if (cbsc_trace[i].status & NCRSTAT_TC)
46566e9e901Smhitch 			printf("NCRTC/");
46666e9e901Smhitch 		switch(cbsc_trace[i].status & NCRSTAT_PHASE) {
46766e9e901Smhitch 		case 0:
46866e9e901Smhitch 			printf("dataout"); break;
46966e9e901Smhitch 		case 1:
47066e9e901Smhitch 			printf("datain"); break;
47166e9e901Smhitch 		case 2:
47266e9e901Smhitch 			printf("cmdout"); break;
47366e9e901Smhitch 		case 3:
47466e9e901Smhitch 			printf("status"); break;
47566e9e901Smhitch 		case 6:
47666e9e901Smhitch 			printf("msgout"); break;
47766e9e901Smhitch 		case 7:
47866e9e901Smhitch 			printf("msgin"); break;
47966e9e901Smhitch 		default:
48066e9e901Smhitch 			printf("phase%d?", cbsc_trace[i].status & NCRSTAT_PHASE);
48166e9e901Smhitch 		}
48266e9e901Smhitch 		printf(") ");
48366e9e901Smhitch 		i = (i + 1) & 127;
48466e9e901Smhitch 	} while (i != cbsc_trace_ptr);
48566e9e901Smhitch 	printf("\n");
48666e9e901Smhitch }
48766e9e901Smhitch #endif
488