xref: /csrg-svn/sys/tahoe/vba/dr.c (revision 43384)
134406Skarels /*
235514Sbostic  * Copyright (c) 1988 The Regents of the University of California.
335514Sbostic  * All rights reserved.
435514Sbostic  *
535514Sbostic  * This code is derived from software contributed to Berkeley by
635514Sbostic  * Computer Consoles Inc.
735514Sbostic  *
835514Sbostic  * Redistribution and use in source and binary forms are permitted
935514Sbostic  * provided that the above copyright notice and this paragraph are
1035514Sbostic  * duplicated in all such forms and that any documentation,
1135514Sbostic  * advertising materials, and other materials related to such
1235514Sbostic  * distribution and use acknowledge that the software was developed
1335514Sbostic  * by the University of California, Berkeley.  The name of the
1435514Sbostic  * University may not be used to endorse or promote products derived
1535514Sbostic  * from this software without specific prior written permission.
1635514Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1735514Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1835514Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1935514Sbostic  *
20*43384Smckusick  *	@(#)dr.c	7.7 (Berkeley) 06/21/90
2134406Skarels  */
2229651Ssam 
2329651Ssam #include "dr.h"
2429651Ssam #if NDR > 0
2530294Ssam /*
2630294Ssam  * DRV11-W DMA interface driver.
2730294Ssam  *
2830227Ssam  * UNTESTED WITH 4.3
2929651Ssam  */
3037507Smckusick #include "machine/mtpr.h"
3137507Smckusick #include "machine/pte.h"
3229651Ssam 
3329651Ssam #include "param.h"
3429651Ssam #include "conf.h"
3529651Ssam #include "user.h"
3629651Ssam #include "proc.h"
3729651Ssam #include "map.h"
3829651Ssam #include "ioctl.h"
3929651Ssam #include "buf.h"
4029651Ssam #include "vm.h"
4130294Ssam #include "kernel.h"
4229651Ssam 
4329651Ssam #include "../tahoevba/vbavar.h"
4429651Ssam #include "../tahoevba/drreg.h"
4529651Ssam 
4629651Ssam #define YES 1
4729651Ssam #define NO  0
4829651Ssam 
4929651Ssam struct  vba_device  *drinfo[NDR];
5029651Ssam struct  dr_aux dr_aux[NDR];
5129651Ssam 
5229651Ssam unsigned drminphys();
5330294Ssam int	 drprobe(), drintr(), drattach(), drtimo(), drrwtimo();
5430294Ssam int	 drstrategy();
5530294Ssam extern	struct  vba_device  *drinfo[];
5630294Ssam static	long drstd[] = { 0 };
5729651Ssam struct  vba_driver drdriver =
5830294Ssam     { drprobe, 0, drattach, 0, drstd, "rs", drinfo };
5929651Ssam 
6029651Ssam #define RSUNIT(dev) (minor(dev) & 7)
6129651Ssam #define SPL_UP spl5
6229651Ssam 
6329651Ssam /* -------- Per-unit data -------- */
6429651Ssam 
6529651Ssam extern struct dr_aux dr_aux[];
6629651Ssam 
6729651Ssam #ifdef DR_DEBUG
6830294Ssam long	DR11 = 0;
6929651Ssam #endif
7029651Ssam 
7129651Ssam drprobe(reg, vi)
7230294Ssam 	caddr_t reg;
7330294Ssam 	struct vba_device *vi;
7429651Ssam {
7530294Ssam 	register int br, cvec;		/* must be r12, r11 */
7630294Ssam 	struct rsdevice *dr;
7729651Ssam 
7830294Ssam #ifdef lint
7930294Ssam 	br = 0; cvec = br; br = cvec;
8030294Ssam 	drintr(0);
8129651Ssam #endif
8230294Ssam 	if (badaddr(reg, 2))
8330294Ssam 		return (0);
8430294Ssam 	dr = (struct rsdevice *)reg;
8530294Ssam 	dr->dr_intvect = --vi->ui_hd->vh_lastiv;
8629651Ssam #ifdef DR_DEBUG
8730294Ssam 	printf("dprobe: Set interrupt vector %lx and init\n",dr->dr_intvec);
8829651Ssam #endif
8930294Ssam 	/* generate interrupt here for autoconfig */
9030294Ssam 	dr->dr_cstat = MCLR;		/* init board and device */
9129651Ssam #ifdef DR_DEBUG
9230294Ssam 	printf("drprobe: Initial status %lx\n", dr->dr_cstat);
9329651Ssam #endif
9430294Ssam 	br = 0x18, cvec = dr->dr_intvect;	/* XXX */
9530294Ssam 	return (sizeof (struct rsdevice));		/* DR11 exist */
9629651Ssam }
9729651Ssam 
9829651Ssam /* ARGSUSED */
9929651Ssam drattach(ui)
10030294Ssam 	struct vba_device *ui;
10129651Ssam {
10230294Ssam 	register struct dr_aux *rsd;
10329651Ssam 
10430294Ssam 	rsd = &dr_aux[ui->ui_unit];
10530294Ssam 	rsd->dr_flags = DR_PRES;		/* This dr11 is present */
10630294Ssam 	rsd->dr_addr = (struct rsdevice *)ui->ui_addr; /* Save addr of this dr11 */
10730294Ssam 	rsd->dr_istat = 0;
10830294Ssam 	rsd->dr_bycnt = 0;
10930294Ssam 	rsd->dr_cmd = 0;
11030294Ssam 	rsd->currenttimo = 0;
11129651Ssam }
11229651Ssam 
11330294Ssam /*ARGSUSED*/
11430294Ssam dropen(dev, flag)
11530294Ssam 	dev_t dev;
11630294Ssam 	int flag;
11729651Ssam {
11830294Ssam 	register int unit = RSUNIT(dev);
11930294Ssam 	register struct rsdevice *dr;
12030294Ssam 	register struct dr_aux *rsd;
12129651Ssam 
12230294Ssam 	if (drinfo[unit] == 0 || !drinfo[unit]->ui_alive)
12330294Ssam 		return (ENXIO);
12430294Ssam 	dr = RSADDR(unit);
12530294Ssam 	rsd = &dr_aux[unit];
12630294Ssam 	if (rsd->dr_flags & DR_OPEN) {
12729651Ssam #ifdef DR_DEBUG
12830294Ssam 		printf("\ndropen: dr11 unit %ld already open",unit);
12929651Ssam #endif
13030294Ssam 		return (ENXIO);	  		/* DR11 already open */
13130294Ssam 	}
13230294Ssam 	rsd->dr_flags |= DR_OPEN;	/* Mark it OPEN */
13330294Ssam 	rsd->dr_istat = 0;		/* Clear status of previous interrupt */
13430294Ssam 	rsd->rtimoticks = hz;		/* Set read no stall timout to 1 sec */
13530294Ssam 	rsd->wtimoticks = hz*60;	/* Set write no stall timout to 1 min */
13630294Ssam 	dr->dr_cstat = DR_ZERO;		/* Clear function & latches */
13730294Ssam 	dr->dr_pulse = (RDMA | RATN);	/* clear leftover attn & e-o-r flags */
13830294Ssam 	drtimo(dev);			/* start the self kicker */
13930294Ssam 	return (0);
14029651Ssam }
14129651Ssam 
14229651Ssam drclose (dev)
14330294Ssam 	dev_t dev;
14429651Ssam {
14530294Ssam 	register int unit = RSUNIT(dev);
14630294Ssam 	register struct dr_aux *dra;
14730294Ssam 	register struct rsdevice *rs;
14830294Ssam 	register short s;
14929651Ssam 
15030294Ssam 	dra = &dr_aux[unit];
15130294Ssam 	if ((dra->dr_flags & DR_OPEN) == 0) {
15229651Ssam #ifdef DR_DEBUG
15330294Ssam 		printf("\ndrclose: DR11 device %ld not open",unit);
15429651Ssam #endif
15530294Ssam 		return;
15630294Ssam 	}
15730294Ssam 	dra->dr_flags &= ~(DR_OPEN|DR_ACTV);
15830294Ssam 	rs = dra->dr_addr;
15930294Ssam 	s = SPL_UP();
16030294Ssam 	rs->dr_cstat = DR_ZERO;
16130294Ssam 	if (dra->dr_buf.b_flags & B_BUSY) {
16230294Ssam 		dra->dr_buf.b_flags &= ~B_BUSY;
16330294Ssam 		wakeup((caddr_t)&dra->dr_buf.b_flags);
16430294Ssam 	}
16530294Ssam 	splx(s);
16640735Skarels 	return (0);
16729651Ssam }
16829651Ssam 
16929651Ssam 
17029651Ssam /*	drread() works exactly like drwrite() except that the
17129651Ssam 	B_READ flag is used when physio() is called
17229651Ssam */
17329651Ssam drread (dev, uio)
17430294Ssam 	dev_t dev;
17530294Ssam 	struct uio *uio;
17629651Ssam {	register struct dr_aux *dra;
17729651Ssam 	register struct buf *bp;
17830294Ssam 	register int spl, err;
17930294Ssam 	register int unit = RSUNIT(dev);
18029651Ssam 
18130294Ssam 	if (uio->uio_iov->iov_len <= 0 ||	/* Negative count */
18230294Ssam 	    uio->uio_iov->iov_len & 1 ||	/* odd count */
18330294Ssam 	    (int)uio->uio_iov->iov_base & 1)	/* odd destination address */
18430294Ssam 		return (EINVAL);
18529651Ssam #ifdef DR_DEBUG
18630294Ssam 	if (DR11 & 8)
18730294Ssam 		printf("\ndrread: (len:%ld)(base:%lx)",
18830294Ssam 		    uio->uio_iov->iov_len,(int)uio->uio_iov->iov_base);
18929651Ssam #endif
19030294Ssam 	dra = &dr_aux[RSUNIT(dev)];
19130294Ssam 	dra->dr_op = DR_READ;
19230294Ssam 	bp =  &dra->dr_buf;
19330294Ssam 	bp->b_resid = 0;
19430294Ssam 	if (dra->dr_flags & DR_NORSTALL) {
19530294Ssam 		/*
19630294Ssam 		 * We are in no stall mode, start the timer,
19730294Ssam 		 * raise IPL so nothing can stop us once the
19830294Ssam 		 * timer's running
19930294Ssam 		 */
20030294Ssam 		spl = SPL_UP();
20130294Ssam 		timeout(drrwtimo, (caddr_t)((dra->currenttimo<<8) | unit),
20230294Ssam 		    (int)dra->rtimoticks);
20330294Ssam 		err = physio(drstrategy, bp, dev,B_READ, drminphys, uio);
20430294Ssam 		splx(spl);
20530294Ssam 		if (err)
20630294Ssam 			return (err);
20730294Ssam 		dra->currenttimo++;	/* Update current timeout number */
20830294Ssam 		/* Did we timeout */
209*43384Smckusick 		if (dra->dr_flags & DR_TMDM)
21030294Ssam 			dra->dr_flags &= ~DR_TMDM; /* Clear timeout flag */
21130294Ssam 		return (err);
21229651Ssam 	}
21330294Ssam 	return (physio(drstrategy, bp, dev,B_READ, drminphys, uio));
21429651Ssam }
21529651Ssam 
21630294Ssam drwrite(dev, uio)
21730294Ssam 	dev_t dev;
21830294Ssam 	struct uio *uio;
21929651Ssam {	register struct dr_aux *dra;
22029651Ssam 	register struct buf *bp;
22130294Ssam 	register int unit = RSUNIT(dev);
22230294Ssam 	int spl, err;
22329651Ssam 
22430294Ssam 	if (uio->uio_iov->iov_len <= 0 || uio->uio_iov->iov_len & 1 ||
22530294Ssam 	    (int)uio->uio_iov->iov_base & 1)
22630294Ssam 		return (EINVAL);
22729651Ssam #ifdef DR_DEBUG
22830294Ssam 	if (DR11 & 4)
22930294Ssam 		printf("\ndrwrite: (len:%ld)(base:%lx)",
23030294Ssam 		    uio->uio_iov->iov_len,(int)uio->uio_iov->iov_base);
23129651Ssam #endif
23230294Ssam 	dra = &dr_aux[RSUNIT(dev)];
23330294Ssam 	dra->dr_op = DR_WRITE;
23430294Ssam 	bp =  &dra->dr_buf;
23530294Ssam 	bp->b_resid = 0;
23630294Ssam 	if (dra->dr_flags & DR_NOWSTALL) {
23730294Ssam 		/*
23830294Ssam 		 * We are in no stall mode, start the timer,
23930294Ssam 		 * raise IPL so nothing can stop us once the
24030294Ssam 		 * timer's running
24130294Ssam 		 */
24230294Ssam 		spl = SPL_UP();
24330294Ssam 		timeout(drrwtimo,(caddr_t)((dra->currenttimo<<8) | unit),
24430294Ssam 		    (int)dra->wtimoticks);
24530294Ssam 		err = physio (drstrategy, bp, dev,B_WRITE, drminphys, uio);
24630294Ssam 		splx(spl);
24730294Ssam 		if (err)
24830294Ssam 			return (err);
24930294Ssam 		dra->currenttimo++;	/* Update current timeout number */
25030294Ssam 		/* Did we timeout */
251*43384Smckusick 		if (dra->dr_flags & DR_TMDM)
25230294Ssam 			dra->dr_flags &= ~DR_TMDM;	/* Clear timeout flag */
25330294Ssam 		return (err);
25429651Ssam 	}
25530294Ssam 	return (physio(drstrategy, bp, dev,B_WRITE, drminphys, uio));
25629651Ssam }
25729651Ssam 
25830294Ssam /*
25930294Ssam  * Routine used by calling program to issue commands to dr11 driver and
26030294Ssam  * through it to the device.
26130294Ssam  * It is also used to read status from the device and driver and to wait
26230294Ssam  * for attention interrupts.
26330294Ssam  * Status is returned in an 8 elements unsigned short integer array, the
26430294Ssam  * first two elements of the array are also used to pass arguments to
26530294Ssam  * drioctl() if required.
26630294Ssam  * The function bits to be written to the dr11 are included in the cmd
26730294Ssam  * argument. Even if they are not being written to the dr11 in a particular
26830294Ssam  * drioctl() call, they will update the copy of cmd that is stored in the
26930294Ssam  * driver. When drstrategy() is called, this updated copy is used if a
27030294Ssam  * deferred function bit write has been specified. The "side effect" of
27130294Ssam  * calls to the drioctl() requires that the last call prior to a read or
27230294Ssam  * write has an appropriate copy of the function bits in cmd if they are
27330294Ssam  * to be used in drstrategy().
27430294Ssam  * When used as command value, the contents of data[0] is the command
27530294Ssam  * parameter.
27630294Ssam  */
27730294Ssam drioctl(dev, cmd, data)
27830294Ssam 	dev_t dev;
27930294Ssam 	int cmd;
28030294Ssam 	long *data;
28129651Ssam {
28230294Ssam 	register int unit = RSUNIT(dev);
28330294Ssam 	register struct dr_aux *dra;
28430294Ssam 	register struct rsdevice *rsaddr = RSADDR(unit);
28537545Smckusick 	int s, error = 0;
28630294Ssam 	u_short status;
28730294Ssam 	long temp;
28829651Ssam 
28929651Ssam #ifdef DR_DEBUG
29030294Ssam 	if (DR11 & 0x10)
29130294Ssam 		printf("\ndrioctl: (dev:%lx)(cmd:%lx)(data:%lx)(data[0]:%lx)",
29230294Ssam 		    dev,cmd,data,data[0]);
29329651Ssam #endif
29430294Ssam 	dra = &dr_aux[unit];
29530294Ssam 	dra->dr_cmd = 0;	/* Fresh copy; clear all previous flags */
29630294Ssam 	switch (cmd) {
29729651Ssam 
29830294Ssam 	case DRWAIT:		/* Wait for attention interrupt */
29929651Ssam #ifdef DR_DEBUG
30030294Ssam 		printf("\ndrioctl: wait for attention interrupt");
30129651Ssam #endif
30230294Ssam 		s = SPL_UP();
30330294Ssam 		/*
30430294Ssam 		 * If the attention flag in dr_flags is set, it probably
30530294Ssam 		 * means that an attention has arrived by the time a
30630294Ssam 		 * previous DMA end-of-range interrupt was serviced. If
30730294Ssam 		 * ATRX is set, we will return with out sleeping, since
30830294Ssam 		 * we have received an attention since the last call to
30930294Ssam 		 * wait on attention.  This may not be appropriate for
31030294Ssam 		 * some applications.
31130294Ssam 		 */
31230294Ssam 		if ((dra->dr_flags & DR_ATRX) == 0) {
31330294Ssam 			dra->dr_flags |= DR_ATWT;	/* Set waiting flag */
31430294Ssam 			/*
31530294Ssam 			 * Enable interrupt; use pulse reg.
31630294Ssam 			 * so function bits are not changed
31730294Ssam 			 */
31830294Ssam 			rsaddr->dr_pulse = IENB;
31940735Skarels 			error = tsleep((caddr_t)&dra->dr_cmd, DRPRI | PCATCH,
32040735Skarels 			    devio, 0);
32130294Ssam 		}
32230294Ssam 		splx(s);
32330294Ssam 		break;
32429651Ssam 
32530294Ssam 	case DRPIOW:			/* Write to p-i/o register */
32630294Ssam 		rsaddr->dr_data = data[0];
32730294Ssam 		break;
32829651Ssam 
32930294Ssam 	case DRPACL:			/* Send pulse to device */
33030294Ssam 		rsaddr->dr_pulse = FCN2;
33130294Ssam 		break;
33229651Ssam 
33330294Ssam 	case DRDACL:			/* Defer alco pulse until go */
33430294Ssam 		dra->dr_cmd |= DR_DACL;
33530294Ssam 		break;
33629651Ssam 
33730294Ssam 	case DRPCYL:			/* Set cycle with next go */
33830294Ssam 		dra->dr_cmd |= DR_PCYL;
33930294Ssam 		break;
34029651Ssam 
34130294Ssam 	case DRDFCN:			/* Update function with next go */
34230294Ssam 		dra->dr_cmd |= DR_DFCN;
34330294Ssam 		break;
34429651Ssam 
34530294Ssam 	case DRRATN:			/* Reset attention flag */
34630294Ssam 		rsaddr->dr_pulse = RATN;
34730294Ssam 		break;
34829651Ssam 
34930294Ssam 	case DRRDMA:			/* Reset DMA e-o-r flag */
35030294Ssam 		rsaddr->dr_pulse = RDMA;
35130294Ssam 		break;
35229651Ssam 
35330294Ssam 	case DRSFCN:			/* Set function bits */
35430294Ssam 		temp = data[0] & DR_FMSK;
35530294Ssam 		/*
35630294Ssam 		 * This has a very important side effect -- It clears
35730294Ssam 		 * the interrupt enable flag. That is fine for this driver,
35830294Ssam 		 * but if it is desired to leave interrupt enable at all
35930294Ssam 		 * times, it will be necessary to read the status register
36030294Ssam 		 * first to get IENB, or carry a software flag that indicates
36130294Ssam 		 * whether interrupts are set, and or this into the control
36230294Ssam 		 * register value being written.
36330294Ssam 		 */
36430294Ssam 		rsaddr->dr_cstat = temp;
36530294Ssam 		break;
36629651Ssam 
36730294Ssam 	case DRRPER:			/* Clear parity flag */
36830294Ssam 		rsaddr->dr_pulse = RPER;
36930294Ssam 		break;
37029651Ssam 
37130294Ssam 	case DRSETRSTALL:		/* Set read stall mode. */
37230294Ssam 		dra->dr_flags &= (~DR_NORSTALL);
37330294Ssam 		break;
37429651Ssam 
37530294Ssam 	case DRSETNORSTALL:		/* Set no stall read  mode. */
37630294Ssam 		dra->dr_flags |= DR_NORSTALL;
37730294Ssam 		break;
37829651Ssam 
37930294Ssam 	case DRGETRSTALL:		/* Returns true if in read stall mode */
38030294Ssam 		data[0]  = (dra->dr_flags & DR_NORSTALL)? 0 : 1;
38130294Ssam 		break;
38229651Ssam 
38330294Ssam 	case DRSETRTIMEOUT:		/* Set read stall timeout (1/10 secs) */
384*43384Smckusick 		if (data[0] < 1)
385*43384Smckusick 			error = EINVAL;
38630294Ssam 		dra->rtimoticks = (data[0] * hz )/10;
38730294Ssam 		break;
38829651Ssam 
38930294Ssam 	case DRGETRTIMEOUT:		/* Return read stall timeout */
39030294Ssam 		data[0] = ((dra->rtimoticks)*10)/hz;
39130294Ssam 		break;
39229651Ssam 
39330294Ssam 	case DRSETWSTALL:		/* Set write stall mode. */
39430294Ssam 		dra->dr_flags &= (~DR_NOWSTALL);
39530294Ssam 		break;
39629651Ssam 
39730294Ssam 	case DRSETNOWSTALL:		/* Set write stall mode. */
39830294Ssam 		dra->dr_flags |= DR_NOWSTALL;
39930294Ssam 		break;
40029651Ssam 
40130294Ssam 	case DRGETWSTALL:		/* Return true if in write stall mode */
40230294Ssam 		data[0] = (dra->dr_flags & DR_NOWSTALL)? 0 : 1;
40330294Ssam 		break;
40429651Ssam 
40530294Ssam 	case DRSETWTIMEOUT:		/* Set write stall timeout (1/10's) */
406*43384Smckusick 		if (data[0] < 1)
407*43384Smckusick 			error = EINVAL;
40830294Ssam 		dra->wtimoticks = (data[0] * hz )/10;
40930294Ssam 		break;
41029651Ssam 
41130294Ssam 	case DRGETWTIMEOUT:		/* Return write stall timeout */
41230294Ssam 		data[0] = ((dra->wtimoticks)*10)/hz;
41330294Ssam 		break;
41429651Ssam 
41530294Ssam 	case DRWRITEREADY:		/* Return true if can write data */
41630294Ssam 		data[0] = (rsaddr->dr_cstat & STTA)? 1 : 0;
41730294Ssam 		break;
41829651Ssam 
41930294Ssam 	case DRREADREADY:		/* Return true if data to be read */
42030294Ssam 		data[0] = (rsaddr->dr_cstat & STTB)? 1 : 0;
42130294Ssam 		break;
42229651Ssam 
42330294Ssam 	case DRBUSY:			/* Return true if device busy */
42430294Ssam 		/*
42530294Ssam 		 * Internally this is the DR11-W
42630294Ssam 		 * STAT C bit, but there is a bug in the Omega 500/FIFO
42730294Ssam 		 * interface board that it cannot drive this signal low
42830294Ssam 		 * for certain DR11-W ctlr such as the Ikon. We use the
42930294Ssam 		 * REDY signal of the CSR on the Ikon DR11-W instead.
43030294Ssam 		 */
43130294Ssam #ifdef notdef
43230294Ssam 		data[0] = (rsaddr->dr_cstat & STTC)? 1 : 0;
43330294Ssam #else
43430294Ssam 		data[0] = ((rsaddr->dr_cstat & REDY)? 0 : 1);
43530294Ssam #endif
43630294Ssam 		break;
43729651Ssam 
43830294Ssam 	case DRRESET:			/* Reset device */
43930294Ssam 		/* Reset DMA ATN RPER flag */
44030294Ssam 		rsaddr->dr_pulse = (MCLR|RDMA|RATN|RPER);
44130294Ssam 		DELAY(0x1f000);
44240735Skarels 		while ((rsaddr->dr_cstat & REDY) == 0 && error == 0)
44340735Skarels 			/* Wakeup by drtimo() */
44440735Skarels 			error = tsleep((caddr_t)dra, DRPRI | PCATCH, devio, 0);
44530294Ssam 		dra->dr_istat = 0;
44630294Ssam 		dra->dr_cmd = 0;
44730294Ssam 		dra->currenttimo = 0;
44830294Ssam 		break;
44929651Ssam 
45030294Ssam 	case DR11STAT: {		/* Copy back dr11 status to user */
45130294Ssam 		register struct dr11io *dr = (struct dr11io *)data;
45230294Ssam 		dr->arg[0] = dra->dr_flags;
45330294Ssam 		dr->arg[1] = rsaddr->dr_cstat;
45430294Ssam 		dr->arg[2] = dra->dr_istat;	/* Status at last interrupt */
45530294Ssam 		dr->arg[3] = rsaddr->dr_data;	/* P-i/o input data */
45630294Ssam 		status = (u_short)((rsaddr->dr_addmod << 8) & 0xff00);
45730294Ssam 		dr->arg[4] = status | (u_short)(rsaddr->dr_intvect & 0xff);
45830294Ssam 		dr->arg[5] = rsaddr->dr_range;
45930294Ssam 		dr->arg[6] = rsaddr->dr_rahi;
46030294Ssam 		dr->arg[7] = rsaddr->dr_ralo;
46130294Ssam 		break;
46230294Ssam 	}
46330294Ssam 	case DR11LOOP:			/* Perform loopback test */
46430294Ssam 		/*
46530294Ssam 		 * NB: MUST HAVE LOOPBACK CABLE ATTACHED --
46630294Ssam 		 * Test results are printed on system console
46730294Ssam 		 */
46837545Smckusick 		if (error = suser(u.u_cred, &u.u_acflag))
46937545Smckusick 			break;
47037545Smckusick 		dr11loop(rsaddr, dra, unit);
47130294Ssam 		break;
47229651Ssam 
47330294Ssam 	default:
47430294Ssam 		return (EINVAL);
47529651Ssam 	}
47629651Ssam #ifdef DR_DEBUG
47730294Ssam 	if (DR11 & 0x10)
47830294Ssam 		printf("**** (data[0]:%lx)",data[0]);
47929651Ssam #endif
48037545Smckusick 	return (error);
48129651Ssam }
48229651Ssam 
48330294Ssam #define NPAT	2
48430294Ssam #define DMATBL	20
48530294Ssam u_short	tstpat[DMATBL] = { 0xAAAA, 0x5555};
48630294Ssam long	DMAin = 0;
48730138Ssam 
48830294Ssam /*
48930294Ssam  * Perform loopback test -- MUST HAVE LOOPBACK CABLE ATTACHED
49030294Ssam  * Test results are printed on system console
49130294Ssam  */
49230294Ssam dr11loop(dr, dra, unit)
49330294Ssam 	struct rsdevice *dr;
49430294Ssam 	struct dr_aux *dra;
49530294Ssam 	int unit;
49630294Ssam {
49730294Ssam 	register long result, ix;
49830294Ssam 	long addr, wait;
49930138Ssam 
50030138Ssam 	dr->dr_cstat = MCLR;		/* Clear board & device, disable intr */
50130294Ssam 	printf("\n\t ----- DR11 unit %ld loopback test -----", unit);
50230138Ssam 	printf("\n\t Program I/O ...");
50330138Ssam 	for (ix=0;ix<NPAT;ix++) {
50430138Ssam 		dr->dr_data = tstpat[ix];	/* Write to Data out register */
50530294Ssam 		result = dr->dr_data & 0xFFFF;	/* Read it back */
50630138Ssam 		if (result != tstpat[ix]) {
50730138Ssam 			printf("Failed, expected : %lx --- actual : %lx",
50830294Ssam 				tstpat[ix], result);
50930138Ssam 			return;
51030138Ssam 		}
51130138Ssam 	}
51230138Ssam 	printf("OK\n\t Functions & Status Bits ...");
51330138Ssam 	dr->dr_cstat = (FCN1 | FCN3);
51430138Ssam 	result = dr->dr_cstat & 0xffff;		/* Read them back */
51530138Ssam 	if ((result & (STTC | STTA)) != (STTC |STTA)) {
51630138Ssam 		printf("Failed, expected : %lx --- actual : %lx, ISR:%lx",
51730294Ssam 			(STTA|STTC), (result & (STTA|STTC)), result);
51830138Ssam 		return;
51930138Ssam 	}
52030138Ssam 	dr->dr_cstat = FCN2;
52130138Ssam 	result = dr->dr_cstat & 0xffff;		/* Read them back */
52230138Ssam 	if ((result & STTB) != STTB) {
52330138Ssam 		printf("Failed, expected : %lx --- actual : %lx, ISR:%lx",
52430294Ssam 			STTB, (result & STTB), result);
52530138Ssam 		return;
52630138Ssam 	}
52730138Ssam 	printf("OK\n\t DMA output ...");
52830294Ssam 	if (DMAin)
52930294Ssam 		goto dmain;
53030138Ssam 	/* Initialize DMA data buffer */
53130294Ssam 	for (ix=0; ix<DMATBL; ix++)
53230294Ssam 		tstpat[ix] = 0xCCCC + ix;
53330138Ssam 	tstpat[DMATBL-1] = 0xCCCC;	/* Last word output */
53430138Ssam 	/* Setup normal DMA */
53530294Ssam 	addr = (long)vtoph((struct proc *)0, (unsigned)tstpat);
53630294Ssam 	dr->dr_walo = (addr >> 1) & 0xffff;
53730294Ssam 	dr->dr_wahi = (addr >> 17) & 0x7fff;
53830294Ssam 	/* Set DMA range count: (number of words - 1) */
53930294Ssam 	dr->dr_range = DMATBL - 1;
54030294Ssam 	/* Set address modifier code to be used for DMA access to memory */
54130294Ssam 	dr->dr_addmod = DRADDMOD;
54230138Ssam 
54330294Ssam 	/*
54430294Ssam 	 * Clear dmaf and attf to assure a clean dma start, also disable
54530294Ssam 	 * attention interrupt
54630294Ssam 	 */
54730294Ssam 	dr->dr_pulse = RDMA|RATN|RMSK;  /* Use pulse register */
54830294Ssam 	dr->dr_cstat = GO|CYCL;		  /* GO...... */
54930138Ssam 
55030138Ssam 	/* Wait for DMA complete; REDY and DMAF are true in ISR */
55130138Ssam 	wait = 0;
55230294Ssam 	while ((result=(dr->dr_cstat & (REDY|DMAF))) != (REDY|DMAF)) {
55330294Ssam 		printf("\n\tWait for DMA complete...ISR : %lx", result);
55430138Ssam 		if (++wait > 5) {
55530138Ssam 			printf("\n\t DMA output fails...timeout!!, ISR:%lx",
55630138Ssam 				result);
55730138Ssam 			return;
55830138Ssam 		}
55930138Ssam 	}
56030138Ssam 	result = dr->dr_data & 0xffff;		/* Read last word output */
56130138Ssam 	if (result != 0xCCCC) {
56230138Ssam 		printf("\n\t Fails, expected : %lx --- actual : %lx",
56330294Ssam 			0xCCCC, result);
56430138Ssam 		return;
56530138Ssam 	}
56630138Ssam 	printf("OK\n\t DMA input ...");
56730138Ssam dmain:
56830138Ssam 	dr->dr_data = 0x1111;		/* DMA input data */
56930138Ssam 	/* Setup normal DMA */
57030294Ssam 	addr = (long)vtoph((struct proc *)0, (unsigned)tstpat);
57130294Ssam 	dr->dr_walo = (addr >> 1) & 0xffff;
57230294Ssam 	dr->dr_wahi = (addr >> 17) & 0x7fff;
57330294Ssam 	dr->dr_range = DMATBL - 1;
57430294Ssam 	dr->dr_addmod = (char)DRADDMOD;
57530294Ssam 	dr->dr_cstat = FCN1;		/* Set FCN1 in ICR to DMA in*/
57630294Ssam 	if ((dra->dr_flags & DR_LOOPTST) == 0) {
57730138Ssam 		/* Use pulse reg */
57830294Ssam 		dr->dr_pulse = RDMA|RATN|RMSK|CYCL|GO;
57930138Ssam 		/* Wait for DMA complete; REDY and DMAF are true in ISR */
58030138Ssam 		wait = 0;
58130294Ssam 		while ((result=(dr->dr_cstat & (REDY|DMAF))) != (REDY|DMAF)) {
58230138Ssam 			printf("\n\tWait for DMA to complete...ISR:%lx",result);
58330138Ssam 			if (++wait > 5) {
58430138Ssam 				printf("\n\t DMA input timeout!!, ISR:%lx",
58530138Ssam 					result);
58630138Ssam 				return;
58730138Ssam 			}
58830138Ssam 		}
58930294Ssam 	} else  {
59030138Ssam 		/* Enable DMA e-o-r interrupt */
59130294Ssam 		dr->dr_pulse = IENB|RDMA|RATN|CYCL|GO;
59230138Ssam 		/* Wait for DMA complete; DR_LOOPTST is false in dra->dr_flags*/
59330138Ssam 		wait = 0;
59430138Ssam 		while (dra->dr_flags & DR_LOOPTST) {
59530138Ssam 			result = dr->dr_cstat & 0xffff;
59630294Ssam 			printf("\n\tWait for DMA e-o-r intr...ISR:%lx", result);
59730138Ssam 			if (++wait > 7) {
59830138Ssam 				printf("\n\t DMA e-o-r timeout!!, ISR:%lx",
59930138Ssam 					result);
60030138Ssam 				dra->dr_flags &= ~DR_LOOPTST;
60130138Ssam 				return;
60230138Ssam 			}
60330138Ssam 		}
60430138Ssam 		dra->dr_flags |= DR_LOOPTST;
60530138Ssam 	}
60630294Ssam 	mtpr(P1DC, tstpat);			/* Purge cache */
60730294Ssam 	mtpr(P1DC, 0x3ff+tstpat);
60830294Ssam 	for (ix=0; ix<DMATBL; ix++) {
60930138Ssam 		if (tstpat[ix] != 0x1111) {
61030294Ssam 			printf("\n\t Fails, ix:%d, expected:%x --- actual:%x",
61130294Ssam 				ix, 0x1111, tstpat[ix]);
61230138Ssam 			return;
61330138Ssam 		}
61430138Ssam 	}
61530294Ssam 	if ((dra->dr_flags & DR_LOOPTST) == 0) {
61630138Ssam 		dra->dr_flags |= DR_LOOPTST;
61730138Ssam 		printf(" OK..\n\tDMA end of range interrupt...");
61830138Ssam 		goto dmain;
61930138Ssam 	}
62030138Ssam 	printf(" OK..\n\tAttention interrupt....");
62130294Ssam 	dr->dr_pulse = IENB|RDMA;
62230294Ssam 	dr->dr_pulse = FCN2;
62330138Ssam 	/* Wait for ATTN interrupt; DR_LOOPTST is false in dra->dr_flags*/
62430138Ssam 	wait = 0;
62530138Ssam 	while (dra->dr_flags & DR_LOOPTST) {
62630138Ssam 		result = dr->dr_cstat & 0xffff;
62730138Ssam 		printf("\n\tWait for Attention intr...ISR:%lx",result);
62830138Ssam 		if (++wait > 7) {
62930138Ssam 			printf("\n\t Attention interrupt timeout!!, ISR:%lx",
63030138Ssam 				result);
63130138Ssam 			dra->dr_flags &= ~DR_LOOPTST;
63230138Ssam 			return;
63330138Ssam 		}
63430138Ssam 	}
63530138Ssam 	dra->dr_flags &= ~DR_LOOPTST;
63630138Ssam 	printf(" OK..\n\tDone...");
63730138Ssam }
63830138Ssam 
63929651Ssam /* Reset state on Unibus reset */
64030294Ssam /*ARGSUSED*/
64129651Ssam drreset(uban)
64230294Ssam 	int uban;
64329651Ssam {
64429651Ssam 
64529651Ssam }
64629651Ssam 
64729651Ssam /*
64829651Ssam  * An interrupt is caused either by an error,
64929651Ssam  * base address overflow, or transfer complete
65029651Ssam  */
65130294Ssam drintr(dr11)
65230294Ssam 	int dr11;
65329651Ssam {
65430294Ssam 	register struct dr_aux *dra = &dr_aux[dr11];
65530294Ssam 	register struct rsdevice *rsaddr = RSADDR(dr11);
65630294Ssam 	register struct buf *bp;
65730294Ssam 	register short status;
65829651Ssam 
65930294Ssam 	status = rsaddr->dr_cstat & 0xffff;	/* get board status register */
66030294Ssam 	dra->dr_istat = status;
66129651Ssam #ifdef DR_DEBUG
66230294Ssam 	if (DR11 & 2)
66330294Ssam 		printf("\ndrintr: dr11 status : %lx",status & 0xffff);
66429651Ssam #endif
66530294Ssam 	if (dra->dr_flags & DR_LOOPTST) {	/* doing loopback test */
66630294Ssam 		dra->dr_flags &= ~DR_LOOPTST;
66730294Ssam 		return;
66830294Ssam 	}
66930294Ssam 	/*
67030294Ssam 	 * Make sure this is not a stray interrupt; at least one of dmaf or attf
67130294Ssam 	 * must be set. Note that if the dr11 interrupt enable latch is reset
67230294Ssam 	 * during a hardware interrupt ack sequence, and by the we get to this
67330294Ssam 	 * point in the interrupt code it will be 0. This is done to give the
67430294Ssam 	 * programmer some control over how the two more-or-less independent
67530294Ssam 	 * interrupt sources on the board are handled.
67630294Ssam 	 * If the attention flag is set when drstrategy() is called to start a
67730294Ssam 	 * dma read or write an interrupt will be generated as soon as the
67830294Ssam 	 * strategy routine enables interrupts for dma end-of-range. This will
67930294Ssam 	 * cause execution of the interrupt routine (not necessarily bad) and
68030294Ssam 	 * will cause the interrupt enable mask to be reset (very bad since the
68130294Ssam 	 * dma end-of-range condition will not be able to generate an interrupt
68230294Ssam 	 * when it occurs) causing the dma operation to time-out (even though
68330294Ssam 	 * the dma transfer will be done successfully) or hang the process if a
68430294Ssam 	 * software time-out capability is not implemented. One way to avoid
68530294Ssam 	 * this situation is to check for a pending attention interrupt (attf
68630294Ssam 	 * set) by calling drioctl() before doing a read or a write. For the
68730294Ssam 	 * time being this driver will solve the problem by clearing the attf
68830294Ssam 	 * flag in the status register before enabling interrupts in
68930294Ssam 	 * drstrategy().
69030294Ssam 	 *
69130294Ssam 	 * **** The IKON 10084 for which this driver is written will set both
69230294Ssam 	 * attf and dmaf if dma is terminated by an attention pulse. This will
69330294Ssam 	 * cause a wakeup(&dr_aux), which will be ignored since it is not being
69430294Ssam 	 * waited on, and an iodone(bp) which is the desired action. Some other
69530294Ssam 	 * dr11 emulators, in particular the IKON 10077 for the Multibus, donot
69630294Ssam 	 * dmaf in this case. This may require some addtional code in the inter-
69730294Ssam 	 * rupt routine to ensure that en iodone(bp) is issued when dma is term-
69830294Ssam 	 * inated by attention.
69930294Ssam 	 */
70030294Ssam 	bp = dra->dr_actf;
70130294Ssam 	if ((status & (ATTF | DMAF)) == 0) {
70230294Ssam 		printf("dr%d: stray interrupt, status=%x", dr11, status);
70330294Ssam 		return;
70430294Ssam 	}
70530294Ssam 	if (status & DMAF) {		/* End-of-range interrupt */
70630294Ssam 		dra->dr_flags |= DR_DMAX;
70729651Ssam 
70829651Ssam #ifdef DR_DEBUG
70930294Ssam 		if (DR11 & 2)
71030294Ssam 		printf("\ndrintr: e-o-r interrupt,cstat:%lx,dr_flags:%lx",
71130294Ssam 			status&0xffff, dra->dr_flags & DR_ACTV);
71229651Ssam #endif
71330294Ssam 		if ((dra->dr_flags & DR_ACTV) == 0) {
71430294Ssam 			/* We are not doing DMA !! */
71530294Ssam 			bp->b_flags |= B_ERROR;
71630294Ssam 		} else {
71730294Ssam 			if (dra->dr_op == DR_READ)
71830294Ssam 				mtpr(P1DC, bp->b_un.b_addr);
71930294Ssam 			dra->dr_bycnt -= bp->b_bcount;
72030294Ssam 			if (dra->dr_bycnt >0) {
72130294Ssam 				bp->b_un.b_addr += bp->b_bcount;
72230294Ssam 				bp->b_bcount = (dra->dr_bycnt > NBPG) ? NBPG:
72329651Ssam 					dra->dr_bycnt;
72430294Ssam 				drstart(rsaddr, dra, bp);
72530294Ssam 				return;
72630294Ssam 			}
72729651Ssam 		}
72830294Ssam 		dra->dr_flags &= ~DR_ACTV;
72930294Ssam 		wakeup((caddr_t)dra);		/* Wakeup waiting in drwait() */
73030294Ssam 		rsaddr->dr_pulse = (RPER|RDMA|RATN); /* reset dma e-o-r flag */
73129651Ssam 	}
73230294Ssam 	/*
73330294Ssam 	 * Now test for attention interrupt -- It may be set in addition to
73430294Ssam 	 * the dma e-o-r interrupt. If we get one we will issue a wakeup to
73530294Ssam 	 * the drioctl() routine which is presumable waiting for one.
73630294Ssam 	 * The program may have to monitor the attention interrupt received
73730294Ssam 	 * flag in addition to doing waits for the interrupt. Futhermore,
73830294Ssam 	 * interrupts are not enabled unless dma is in progress or drioctl()
73930294Ssam 	 * has been called to wait for attention -- this may produce some
74030294Ssam 	 * strange results if attf is set on the dr11 when a read or a write
74130294Ssam 	 * is initiated, since that will enables interrupts.
74230294Ssam 	 * **** The appropriate code for this interrupt routine will probably
74330294Ssam 	 * be rather application dependent.
74430294Ssam 	 */
74530294Ssam 	if (status & ATTF) {
74630294Ssam 		dra->dr_flags |= DR_ATRX;
74730294Ssam 		dra->dr_flags &= ~DR_ATWT;
74830294Ssam 		rsaddr->dr_cstat = RATN;	/* reset attention flag */
74930294Ssam 		/*
75030294Ssam 		 * Some applications which use attention to terminate
75130294Ssam 		 * dma may also want to issue an iodone() here to
75230294Ssam 		 * wakeup physio().
75330294Ssam 		 */
75430294Ssam 		wakeup((caddr_t)&dra->dr_cmd);
75530294Ssam 	}
75629651Ssam }
75729651Ssam 
75829651Ssam unsigned
75929651Ssam drminphys(bp)
76030294Ssam 	struct buf *bp;
76129651Ssam {
76230294Ssam 
76330294Ssam 	if (bp->b_bcount > 65536)
76430294Ssam 		bp->b_bcount = 65536;
76529651Ssam }
76629651Ssam 
76729651Ssam /*
76830294Ssam  * This routine performs the device unique operations on the DR11W
76930294Ssam  * it is passed as an argument to and invoked by physio
77029651Ssam  */
77129651Ssam drstrategy (bp)
77230294Ssam 	register struct buf *bp;
77329651Ssam {
77430294Ssam 	register int s;
77530294Ssam 	int unit = RSUNIT(bp->b_dev);
77630294Ssam 	register struct rsdevice *rsaddr = RSADDR(unit);
77730294Ssam 	register struct dr_aux *dra = &dr_aux[unit];
77830294Ssam 	register int ok;
77929651Ssam #ifdef DR_DEBUG
78030294Ssam 	register char *caddr;
78130294Ssam 	long drva();
78229651Ssam #endif
78329651Ssam 
78430294Ssam 	if ((dra->dr_flags & DR_OPEN) == 0) {	/* Device not open */
78530294Ssam 		bp->b_error = ENXIO;
78630294Ssam 		bp->b_flags |= B_ERROR;
78730294Ssam 		iodone (bp);
78830294Ssam 		return;
78930294Ssam 	}
79030294Ssam 	while (dra->dr_flags & DR_ACTV)
79130294Ssam 		/* Device is active; should never be in here... */
79240735Skarels 		(void) tsleep((caddr_t)&dra->dr_flags, DRPRI, devio, 0);
79330294Ssam 	dra->dr_actf = bp;
79429651Ssam #ifdef DR_DEBUG
79530294Ssam 	drva(dra, bp->b_proc, bp->b_un.b_addr, bp->b_bcount);
79629651Ssam #endif
79730294Ssam 	dra->dr_oba = bp->b_un.b_addr;	/* Save original addr, count */
79830294Ssam 	dra->dr_obc = bp->b_bcount;
79930294Ssam 	dra->dr_bycnt = bp->b_bcount;	/* Save xfer count used by drintr() */
80030294Ssam 	if ((((long)bp->b_un.b_addr & 0x3fffffff) >> PGSHIFT) !=
80130294Ssam 	    ((((long)bp->b_un.b_addr & 0x3fffffff) + bp->b_bcount) >> PGSHIFT))
80230294Ssam 		bp->b_bcount = NBPG - (((long)bp->b_un.b_addr) & PGOFSET);
80330294Ssam 	dra->dr_flags |= DR_ACTV;	/* Mark active (use in intr handler) */
80430294Ssam 	s = SPL_UP();
80530294Ssam 	drstart(rsaddr,dra,bp);
80630294Ssam 	splx(s);
80730294Ssam 	ok = drwait(rsaddr,dra);
80829651Ssam #ifdef DR_DEBUG
80930294Ssam 	if (DR11 & 0x40) {
81030294Ssam 		caddr = (char *)dra->dr_oba;
81130294Ssam 		if (dra->dr_op == DR_READ)
81230294Ssam 			printf("\nAfter read: (%lx)(%lx)",
81330294Ssam 			    caddr[0]&0xff, caddr[1]&0xff);
81430294Ssam 	}
81529651Ssam #endif
81630294Ssam 	dra->dr_flags &= ~DR_ACTV;		/* Clear active flag */
81730294Ssam 	bp->b_un.b_addr = dra->dr_oba;	/* Restore original addr, count */
81830294Ssam 	bp->b_bcount = dra->dr_obc;
81930294Ssam 	if (!ok)
82030294Ssam 		bp->b_flags |= B_ERROR;
82130294Ssam 	/* Mark buffer B_DONE,so physstrat() in ml/machdep.c won't sleep */
82230294Ssam 	iodone(bp);
82330294Ssam 	wakeup((caddr_t)&dra->dr_flags);
82430294Ssam 	/*
82530294Ssam 	 * Return to the calling program (physio()). Physio() will sleep
82630294Ssam 	 * until awaken by a call to iodone() in the interupt handler --
82730294Ssam 	 * which will be called by the dispatcher when it receives dma
82830294Ssam 	 * end-of-range interrupt.
82930294Ssam 	 */
83029651Ssam }
83129651Ssam 
83230294Ssam drwait(rs, dr)
83330294Ssam 	register struct rsdevice *rs;
83430294Ssam 	register struct dr_aux *dr;
83529651Ssam {
83630294Ssam 	int s;
83729651Ssam 
83829651Ssam 	s = SPL_UP();
83930294Ssam 	while (dr->dr_flags & DR_ACTV)
84040735Skarels 		(void) tsleep((caddr_t)dr, DRPRI, devio, 0);
84129651Ssam 	splx(s);
84230294Ssam 	if (dr->dr_flags & DR_TMDM) {		/* DMA timed out */
84329651Ssam 		dr->dr_flags &= ~DR_TMDM;
84430294Ssam 		return (0);
84529651Ssam 	}
84630294Ssam 	if (rs->dr_cstat & (PERR|BERR|TERR)) {
84730294Ssam 		dr->dr_actf->b_flags |= B_ERROR;
84830294Ssam 		return (0);
84929651Ssam 	}
85029651Ssam 	dr->dr_flags &= ~DR_DMAX;
85130294Ssam 	return (1);
85229651Ssam }
85329651Ssam 
85430294Ssam /*
85530294Ssam  *
85630294Ssam  * The lower 8-bit of tinfo is the minor device number, the
85730294Ssam  * remaining higher 8-bit is the current timout number
85830294Ssam  */
85929651Ssam drrwtimo(tinfo)
86030294Ssam 	register u_long tinfo;
86130294Ssam {
86230294Ssam 	register long unit = tinfo & 0xff;
86329651Ssam 	register struct dr_aux *dr = &dr_aux[unit];
86429651Ssam 	register struct rsdevice *rs = dr->dr_addr;
86529651Ssam 
86630294Ssam 	/*
86730294Ssam 	 * If this is not the timeout that drwrite/drread is waiting
86830294Ssam 	 * for then we should just go away
86930294Ssam 	 */
87030294Ssam 	if ((tinfo &~ 0xff) != (dr->currenttimo << 8))
87130294Ssam 		return;
87229651Ssam 	/* Mark the device timed out */
87329651Ssam 	dr->dr_flags |= DR_TMDM;
87429651Ssam 	dr->dr_flags &= ~DR_ACTV;
87529651Ssam 	rs->dr_pulse = RMSK;			/* Inihibit interrupt */
87629651Ssam 	rs->dr_pulse = (RPER|RDMA|RATN|IENB);	/* Clear DMA logic */
87730294Ssam 	/*
87830294Ssam 	 * Some applications will not issue a master after dma timeout,
87930294Ssam 	 * since doing so sends an INIT H pulse to the external device,
88030294Ssam 	 * which may produce undesirable side-effects.
88130294Ssam 	 */
88229651Ssam 	/* Wake up process waiting in drwait() and flag the error */
88330294Ssam 	dr->dr_actf->b_flags |= B_ERROR;
88429651Ssam 	wakeup((caddr_t)dr->dr_cmd);
88529651Ssam }
88629651Ssam 
88729651Ssam /*
88830294Ssam  * Kick the driver every second
88930294Ssam  */
89029651Ssam drtimo(dev)
89130294Ssam 	dev_t dev;
89229651Ssam {
89330294Ssam 	register int unit = RSUNIT(dev);
89429651Ssam 	register struct dr_aux *dr;
89529651Ssam 
89630294Ssam 	dr = &dr_aux[unit];
89729651Ssam 	if (dr->dr_flags & DR_OPEN)
89830294Ssam 		timeout(drtimo, (caddr_t)dev, hz);
89929651Ssam 	wakeup((caddr_t)dr);	/* Wakeup any process waiting for interrupt */
90029651Ssam }
90129651Ssam 
90229651Ssam #ifdef DR_DEBUG
90330294Ssam drva(dra, p, va, bcnt)
90430294Ssam 	struct dr_aux *dra;
90530294Ssam 	struct proc *p;
90630294Ssam 	char *va;
90730294Ssam 	long bcnt;
90830294Ssam {
90930294Ssam 	register long first, last , np;
91029651Ssam 
91129651Ssam 	if (DR11 & 0x20)  {
91230294Ssam 		first = ((long)(vtoph(p, (unsigned)va))) >> 10;
91330294Ssam 		last = ((long)(vtoph(p, (unsigned)va+bcnt))) >> 10;
91429651Ssam 		np = bcnt / 0x3ff;
91529651Ssam 		printf("\ndrva: (op:%ld)(first:%ld)(last:%ld)(np:%ld)(cnt:%ld)",
91629651Ssam 			dra->dr_op,first,last,np,bcnt);
91729651Ssam 	}
91829651Ssam }
91929651Ssam #endif
92029651Ssam 
92130294Ssam drstart(rsaddr, dra, bp)
92230294Ssam 	register struct rsdevice *rsaddr;
92330294Ssam 	register struct dr_aux *dra;
92430294Ssam 	register struct buf *bp;
92530294Ssam {
92630294Ssam 	register long addr;
92730294Ssam 	u_short go;
92829651Ssam 
92929651Ssam #ifdef DR_DEBUG
93030294Ssam 	if (dra->dr_op == DR_READ && (DR11 & 8)) {
93130294Ssam 		char *caddr = (char *)bp->b_un.b_addr;
93229651Ssam 		printf("\ndrstart: READ, bcnt:%ld",bp->b_bcount);
93329651Ssam 		printf(",(%lx)(%lx)",caddr[0]&0xff,caddr[1]&0xff);
93429651Ssam 	}
93529651Ssam #endif
93630294Ssam 	/* we are doing raw IO, bp->b_un.b_addr is user's address */
93730294Ssam 	addr = (long)vtoph(bp->b_proc, (unsigned)bp->b_un.b_addr);
93830294Ssam 	/*
93930294Ssam 	 * Set DMA address into DR11 interace registers: DR11 requires that
94030294Ssam 	 * the address be right shifted 1 bit position before it is written
94130294Ssam 	 * to the board (The board will left shift it one bit position before
94230294Ssam 	 * it places the address on the bus
94330294Ssam 	 */
94430294Ssam 	rsaddr->dr_walo = (addr >> 1) & 0xffff;
94530294Ssam 	rsaddr->dr_wahi = (addr >> 17) & 0x7fff;
94630294Ssam 	/* Set DMA range count: (number of words - 1) */
94730294Ssam 	rsaddr->dr_range = (bp->b_bcount >> 1) - 1;
94830294Ssam 	/* Set address modifier code to be used for DMA access to memory */
94930294Ssam 	rsaddr->dr_addmod = DRADDMOD;
95030294Ssam 	/*
95130294Ssam 	 * Now determine whether this is a read or a write. ***** This is
95230294Ssam 	 * probably only usefull for link mode operation, since dr11 doesnot
95330294Ssam 	 * controll the direction of data transfer. The C1 control input
95430294Ssam 	 * controls whether the hardware is doing a read or a write. In link
95530294Ssam 	 * mode this is controlled by function 1 latch (looped back by the
95630294Ssam 	 * cable) and could be set the program. In the general case, the dr11
95730294Ssam 	 * doesnot know in advance what the direction of transfer is - although
95830294Ssam 	 * the program and protocol logic probably is
95930294Ssam 	 */
96029651Ssam #ifdef DR_DEBUG
96130294Ssam 	if (DR11 & 1)
96230294Ssam 		printf(
96330294Ssam "\ndrstrat: about to GO..,dr_cmd:%lx,drstat:%lx,drcnt:%ld,cdata:%lx,OP:%ld",
96430294Ssam 		    dra->dr_cmd, rsaddr->dr_cstat, rsaddr->dr_range,
96530294Ssam 		    rsaddr->dr_data, dra->dr_op);
96629651Ssam #endif
96730294Ssam 	/*
96830294Ssam 	 * Update function latches may have been done already by drioctl() if
96930294Ssam 	 * request from drioctl()
97030294Ssam 	 */
97130294Ssam 	if (dra->dr_cmd & DR_DFCN) {		/* deferred function write */
97230294Ssam 		dra->dr_cmd &= ~DR_DFCN;	/* Clear request */
97330294Ssam 		go = dra->dr_cmd & DR_FMSK;	/* mask out fcn bits */
97430294Ssam 		rsaddr->dr_cstat = go;		/* Write it to the board */
97530294Ssam 	}
97630294Ssam 	/* Clear dmaf and attf to assure a clean dma start */
97730294Ssam 	rsaddr->dr_pulse = RATN|RDMA|RPER;
97830294Ssam 	rsaddr->dr_cstat = IENB|GO|CYCL|dra->dr_op; /* GO...... */
97930294Ssam 	/*
98030294Ssam 	 * Now check for software cycle request -- usually
98130294Ssam 	 * by transmitter in link mode.
98230294Ssam 	 */
98330294Ssam 	if (dra->dr_cmd & DR_PCYL) {
98430294Ssam 		dra->dr_cmd &= ~DR_PCYL;	/* Clear request */
98530294Ssam 		rsaddr->dr_pulse = CYCL;	/* Use pulse register again */
98630294Ssam 	}
98730294Ssam 	/*
98830294Ssam 	 * Now check for deferred ACLO FCNT2 pulse request -- usually to tell
98930294Ssam 	 * the transmitter (via its attention) that we have enabled dma.
99030294Ssam 	 */
99130294Ssam 	if (dra->dr_cmd & DR_DACL) {
99230294Ssam 		dra->dr_cmd &= ~DR_DACL;	/* Clear request */
99330294Ssam 		rsaddr->dr_pulse = FCN2;	/* Use pulse register again */
99430294Ssam 	}
99529651Ssam }
99629651Ssam #endif  NDR
997