xref: /csrg-svn/sys/vax/uba/dh.c (revision 717)
1*717Sbill /*	dh.c	3.14	08/24/80	*/
213Sbill 
313Sbill /*
413Sbill  *	DH-11 driver
513Sbill  *	This driver calls on the DHDM driver.
613Sbill  *	If the DH has no DM11-BB, then the latter will
713Sbill  *	be fake. To insure loading of the correct DM code,
813Sbill  *	lib2 should have dhdm.o, dh.o and dhfdm.o in that order.
913Sbill  */
1013Sbill 
1113Sbill #include "../h/param.h"
1213Sbill #include "../h/conf.h"
1313Sbill #include "../h/dir.h"
1413Sbill #include "../h/user.h"
1513Sbill #include "../h/tty.h"
1613Sbill #include "../h/map.h"
1713Sbill #include "../h/pte.h"
1813Sbill #include "../h/uba.h"
19113Sbill #include "../h/bk.h"
2013Sbill 
21144Sbill /*
22144Sbill  * When running dz's using only SAE (silo alarm) on input
23144Sbill  * it is necessary to call dzrint() at clock interrupt time.
24144Sbill  * This is unsafe unless spl5()s in tty code are changed to
25144Sbill  * spl6()s to block clock interrupts.  Note that the dh driver
26144Sbill  * currently in use works the same way as the dz, even though
27144Sbill  * we could try to more intelligently manage its silo.
28144Sbill  * Thus don't take this out if you have no dz's unless you
29144Sbill  * change clock.c and dhtimer().
30144Sbill  */
31144Sbill #define	spl5	spl6
32144Sbill 
3313Sbill #define	DHADDR	((struct device *)(UBA0_DEV + 0160020))
34280Sbill #define	NDH11	32	/* number of lines */
3513Sbill #define UBACVT(x) (cbase + (short)((x)-(char *)cfree))
3613Sbill 
3713Sbill struct cblock {
3813Sbill 	struct cblock *c_next;
3913Sbill 	char	c_info[CBSIZE];
4013Sbill };
4113Sbill 
4213Sbill struct	tty dh11[NDH11];
43117Sbill int	dhact;
44280Sbill int	dhisilo;
4513Sbill int	ndh11	= NDH11;
4613Sbill int	dhstart();
4713Sbill int	ttrstrt();
48280Sbill int	dh_ubinfo;
49280Sbill int	cbase;
50280Sbill int	getcbase;
5113Sbill extern struct cblock cfree[];
5213Sbill 
5313Sbill /*
5413Sbill  * Hardware control bits
5513Sbill  */
5613Sbill #define	BITS6	01
5713Sbill #define	BITS7	02
5813Sbill #define	BITS8	03
5913Sbill #define	TWOSB	04
6013Sbill #define	PENABLE	020
6113Sbill /* DEC manuals incorrectly say this bit causes generation of even parity. */
6213Sbill #define	OPAR	040
6313Sbill #define	HDUPLX	040000
6413Sbill 
6513Sbill #define	IENAB	030100
66105Sbill #define	NXM	02000
67105Sbill #define	CLRNXM	0400
6813Sbill #define	PERROR	010000
6913Sbill #define	FRERROR	020000
7013Sbill #define	OVERRUN	040000
7113Sbill #define	XINT	0100000
7213Sbill #define	SSPEED	7	/* standard speed: 300 baud */
7313Sbill 
7413Sbill /*
7513Sbill  * DM control bits
7613Sbill  */
7713Sbill #define	TURNON	03	/* CD lead + line enable */
7813Sbill #define	TURNOFF	01	/* line enable */
79168Sbill #define	DTR	02	/* data terminal ready */
8013Sbill #define	RQS	04	/* request to send */
8113Sbill 
8213Sbill /*
8313Sbill  * Software copy of last dhbar
8413Sbill  */
8513Sbill short	dhsar[(NDH11+15)/16];
8613Sbill 
8713Sbill struct device
8813Sbill {
8913Sbill 	union {
9013Sbill 		short	dhcsr;
9113Sbill 		char	dhcsrl;
9213Sbill 	} un;
9313Sbill 	short	dhnxch;
9413Sbill 	short	dhlpr;
9513Sbill 	unsigned short	dhcar;
9613Sbill 	short	dhbcr;
9713Sbill 	unsigned short	dhbar;
9813Sbill 	short	dhbreak;
9913Sbill 	short	dhsilo;
10013Sbill };
10113Sbill 
10213Sbill /*
10313Sbill  * Open a DH11 line.
10413Sbill  */
10513Sbill /*ARGSUSED*/
10613Sbill dhopen(dev, flag)
10713Sbill {
10813Sbill 	register struct tty *tp;
10913Sbill 	register d;
11013Sbill 	register struct device *addr;
11113Sbill 	int s;
11213Sbill 
11313Sbill 	d = minor(dev) & 0177;
11413Sbill 	if (d >= NDH11) {
11513Sbill 		u.u_error = ENXIO;
11613Sbill 		return;
11713Sbill 	}
11813Sbill 	tp = &dh11[d];
11913Sbill 	addr = DHADDR;
12013Sbill 	addr += d>>4;
12113Sbill 	tp->t_addr = (caddr_t)addr;
12213Sbill 	tp->t_oproc = dhstart;
12313Sbill 	tp->t_iproc = NULL;
12413Sbill 	tp->t_state |= WOPEN;
12513Sbill 	s = spl6();
126117Sbill 	if (!getcbase) {
127117Sbill 		getcbase++;
128*717Sbill 		/* 512+ is a kludge to try to get around a hardware problem */
129*717Sbill 		dh_ubinfo = uballoc((caddr_t)cfree, 512+NCLIST*sizeof(struct cblock), 0);
130280Sbill 		cbase = (short)dh_ubinfo;
13113Sbill 	}
13213Sbill 	splx(s);
13313Sbill 	addr->un.dhcsr |= IENAB;
134117Sbill 	dhact |= (1<<(d>>4));
13513Sbill 	if ((tp->t_state&ISOPEN) == 0) {
13613Sbill 		ttychars(tp);
137168Sbill 		if (tp->t_ispeed == 0) {
138168Sbill 			tp->t_ispeed = SSPEED;
139168Sbill 			tp->t_ospeed = SSPEED;
140168Sbill 			tp->t_flags = ODDP|EVENP|ECHO;
141168Sbill 		}
14213Sbill 		dhparam(d);
14313Sbill 	}
14413Sbill 	if (tp->t_state&XCLUDE && u.u_uid!=0) {
14513Sbill 		u.u_error = EBUSY;
14613Sbill 		return;
14713Sbill 	}
14813Sbill 	dmopen(dev);
14913Sbill 	(*linesw[tp->t_line].l_open)(dev,tp);
15013Sbill }
15113Sbill 
15213Sbill /*
15313Sbill  * Close a DH11 line.
15413Sbill  */
15513Sbill /*ARGSUSED*/
15613Sbill dhclose(dev, flag)
15713Sbill dev_t dev;
15813Sbill int  flag;
15913Sbill {
16013Sbill 	register struct tty *tp;
16113Sbill 	register d;
16213Sbill 
16313Sbill 	d = minor(dev) & 0177;
16413Sbill 	tp = &dh11[d];
16513Sbill 	(*linesw[tp->t_line].l_close)(tp);
16613Sbill 	if (tp->t_state&HUPCLS || (tp->t_state&ISOPEN)==0)
167168Sbill 		dmctl(d, TURNOFF, DMSET);
16813Sbill 	ttyclose(tp);
16913Sbill }
17013Sbill 
17113Sbill /*
17213Sbill  * Read from a DH11 line.
17313Sbill  */
17413Sbill dhread(dev)
17513Sbill {
17613Sbill register struct tty *tp;
17713Sbill 
17813Sbill 	tp = &dh11[minor(dev) & 0177];
17913Sbill 	(*linesw[tp->t_line].l_read)(tp);
18013Sbill }
18113Sbill 
18213Sbill /*
18313Sbill  * write on a DH11 line
18413Sbill  */
18513Sbill dhwrite(dev)
18613Sbill {
18713Sbill register struct tty *tp;
18813Sbill 
18913Sbill 	tp = &dh11[minor(dev) & 0177];
19013Sbill 	(*linesw[tp->t_line].l_write)(tp);
19113Sbill }
19213Sbill 
19313Sbill /*
19413Sbill  * DH11 receiver interrupt.
19513Sbill  */
19613Sbill dhrint(dev)
19713Sbill {
19813Sbill 	register struct tty *tp;
19913Sbill 	register short c;
20013Sbill 	register struct device *addr;
201117Sbill 	register struct tty *tp0;
202139Sbill 	int s;
20313Sbill 
204139Sbill 	s = spl6();	/* see comment in clock.c */
20513Sbill 	addr = DHADDR;
20613Sbill 	addr += minor(dev) & 0177;
207117Sbill 	tp0 = &dh11[((minor(dev)&0177)<<4)];
20813Sbill 	while ((c = addr->dhnxch) < 0) {	/* char. present */
209117Sbill 		tp = tp0 + ((c>>8)&017);
21013Sbill 		if (tp >= &dh11[NDH11])
21113Sbill 			continue;
21213Sbill 		if((tp->t_state&ISOPEN)==0) {
21313Sbill 			wakeup((caddr_t)tp);
21413Sbill 			continue;
21513Sbill 		}
21613Sbill 		if (c&PERROR)
21713Sbill 			if ((tp->t_flags&(EVENP|ODDP))==EVENP
21813Sbill 			 || (tp->t_flags&(EVENP|ODDP))==ODDP )
21913Sbill 				continue;
22013Sbill 		if (c&OVERRUN)
22113Sbill 			printf("O");
22213Sbill 		if (c&FRERROR)		/* break */
22313Sbill 			if (tp->t_flags&RAW)
22413Sbill 				c = 0;	/* null (for getty) */
22513Sbill 			else
226168Sbill #ifdef IIASA
227168Sbill 				continue;
228168Sbill #else
229184Sbill 				c = tun.t_intrc;
230168Sbill #endif
231139Sbill 		if (tp->t_line == NETLDISC) {
232117Sbill 			c &= 0177;
233168Sbill 			BKINPUT(c, tp);
234117Sbill 		} else
235117Sbill 			(*linesw[tp->t_line].l_rint)(c,tp);
23613Sbill 	}
237139Sbill 	splx(s);
23813Sbill }
23913Sbill 
24013Sbill /*
24113Sbill  * stty/gtty for DH11
24213Sbill  */
24313Sbill /*ARGSUSED*/
24413Sbill dhioctl(dev, cmd, addr, flag)
24513Sbill caddr_t addr;
24613Sbill {
24713Sbill 	register struct tty *tp;
24813Sbill 
24913Sbill 	tp = &dh11[minor(dev) & 0177];
250113Sbill 	cmd = (*linesw[tp->t_line].l_ioctl)(tp, cmd, addr);
251113Sbill 	if (cmd==0)
252113Sbill 		return;
25313Sbill 	if (ttioccomm(cmd, tp, addr, dev)) {
25413Sbill 		if (cmd==TIOCSETP||cmd==TIOCSETN)
25513Sbill 			dhparam(dev);
256168Sbill 	} else switch(cmd) {
257168Sbill 	case TIOCSBRK:
258168Sbill 		((struct device *)(tp->t_addr))->dhbreak |= 1<<(minor(dev)&017);
259168Sbill 		break;
260168Sbill 	case TIOCCBRK:
261168Sbill 		((struct device *)(tp->t_addr))->dhbreak &= ~(1<<(minor(dev)&017));
262168Sbill 		break;
263168Sbill 	case TIOCSDTR:
264168Sbill 		dmctl(minor(dev), DTR|RQS, DMBIS);
265168Sbill 		break;
266168Sbill 	case TIOCCDTR:
267168Sbill 		dmctl(minor(dev), DTR|RQS, DMBIC);
268168Sbill 		break;
269168Sbill 	default:
27013Sbill 		u.u_error = ENOTTY;
271168Sbill 	}
27213Sbill }
27313Sbill 
27413Sbill /*
27513Sbill  * Set parameters from open or stty into the DH hardware
27613Sbill  * registers.
27713Sbill  */
27813Sbill dhparam(dev)
27913Sbill {
28013Sbill 	register struct tty *tp;
28113Sbill 	register struct device *addr;
28213Sbill 	register d;
283300Sbill 	int s;
28413Sbill 
28513Sbill 	d = minor(dev) & 0177;
28613Sbill 	tp = &dh11[d];
28713Sbill 	addr = (struct device *)tp->t_addr;
288300Sbill 	s = spl5();
28913Sbill 	addr->un.dhcsrl = (d&017) | IENAB;
29013Sbill 	/*
29113Sbill 	 * Hang up line?
29213Sbill 	 */
29313Sbill 	if ((tp->t_ispeed)==0) {
29413Sbill 		tp->t_state |= HUPCLS;
295168Sbill 		dmctl(d, TURNOFF, DMSET);
29613Sbill 		return;
29713Sbill 	}
29813Sbill 	d = ((tp->t_ospeed)<<10) | ((tp->t_ispeed)<<6);
29913Sbill 	if ((tp->t_ispeed) == 4)		/* 134.5 baud */
30013Sbill 		d |= BITS6|PENABLE|HDUPLX;
30113Sbill 	else if (tp->t_flags&RAW)
30213Sbill 		d |= BITS8;
30313Sbill 	else
30413Sbill 		d |= BITS7|PENABLE;
30513Sbill 	if ((tp->t_flags&EVENP) == 0)
30613Sbill 		d |= OPAR;
30713Sbill 	if ((tp->t_ospeed) == 3)	/* 110 baud */
30813Sbill 		d |= TWOSB;
30913Sbill 	addr->dhlpr = d;
310300Sbill 	splx(s);
31113Sbill }
31213Sbill 
31313Sbill /*
31413Sbill  * DH11 transmitter interrupt.
31513Sbill  * Restart each line which used to be active but has
31613Sbill  * terminated transmission since the last interrupt.
31713Sbill  */
31813Sbill dhxint(dev)
31913Sbill {
32013Sbill 	register struct tty *tp;
32113Sbill 	register struct device *addr;
32213Sbill 	register d;
32313Sbill 	short ttybit, bar, *sbar;
324144Sbill 	int s;
32513Sbill 
326144Sbill 	s = spl6();	/* block the clock */
32713Sbill 	d = minor(dev) & 0177;
32813Sbill 	addr = DHADDR + d;
32913Sbill 	addr->un.dhcsr &= (short)~XINT;
330105Sbill 	if (addr->un.dhcsr & NXM) {
331105Sbill 		addr->un.dhcsr |= CLRNXM;
332105Sbill 		printf("dh clr NXM\n");
333105Sbill 	}
33413Sbill 	sbar = &dhsar[d];
33513Sbill 	bar = *sbar & ~addr->dhbar;
33613Sbill 	d <<= 4; ttybit = 1;
33713Sbill 
33813Sbill 	for(; bar; d++, ttybit <<= 1) {
33913Sbill 		if(bar&ttybit) {
34013Sbill 			*sbar &= ~ttybit;
34113Sbill 			bar &= ~ttybit;
34213Sbill 			tp = &dh11[d];
343113Sbill 			tp->t_state &= ~BUSY;
344113Sbill 			if (tp->t_state&FLUSH)
345113Sbill 				tp->t_state &= ~FLUSH;
346113Sbill 			else {
347113Sbill 				addr->un.dhcsrl = (d&017)|IENAB;
348219Sbill 				ndflush(&tp->t_outq,
349219Sbill 				    (int)addr->dhcar-UBACVT(tp->t_outq.c_cf));
350113Sbill 			}
351113Sbill 			if (tp->t_line)
35213Sbill 				(*linesw[tp->t_line].l_start)(tp);
353113Sbill 			else
35413Sbill 				dhstart(tp);
35513Sbill 		}
35613Sbill 	}
357144Sbill 	splx(s);
35813Sbill }
35913Sbill 
36013Sbill /*
36113Sbill  * Start (restart) transmission on the given DH11 line.
36213Sbill  */
36313Sbill dhstart(tp)
36413Sbill register struct tty *tp;
36513Sbill {
36613Sbill 	register struct device *addr;
36713Sbill 	register short nch;
36813Sbill 	int s, d;
36913Sbill 
37013Sbill 	/*
37113Sbill 	 * If it's currently active, or delaying,
37213Sbill 	 * no need to do anything.
37313Sbill 	 */
37413Sbill 	s = spl5();
37513Sbill 	d = tp-dh11;
37613Sbill 	addr = (struct device *)tp->t_addr;
37713Sbill 	if (tp->t_state&(TIMEOUT|BUSY|TTSTOP))
37813Sbill 		goto out;
37913Sbill 
38013Sbill 	/*
38113Sbill 	 * If the writer was sleeping on output overflow,
38213Sbill 	 * wake him when low tide is reached.
38313Sbill 	 */
38413Sbill 	if (tp->t_state&ASLEEP && tp->t_outq.c_cc<=TTLOWAT) {
38513Sbill 		tp->t_state &= ~ASLEEP;
38613Sbill 		if (tp->t_chan)
387168Sbill 			mcstart(tp->t_chan, (caddr_t)&tp->t_outq);
388168Sbill 		else
38913Sbill 			wakeup((caddr_t)&tp->t_outq);
39013Sbill 	}
39113Sbill 
39213Sbill 	if (tp->t_outq.c_cc == 0)
39313Sbill 		goto out;
39413Sbill 
39513Sbill 	/*
39613Sbill 	 * Find number of characters to transfer.
39713Sbill 	 */
39813Sbill 	if (tp->t_flags & RAW) {
39913Sbill 		nch = ndqb(&tp->t_outq, 0);
40013Sbill 	} else {
40113Sbill 		nch = ndqb(&tp->t_outq, 0200);
40213Sbill 		if (nch == 0) {
40313Sbill 			nch = getc(&tp->t_outq);
40413Sbill 			timeout(ttrstrt, (caddr_t)tp, (nch&0177)+6);
40513Sbill 			tp->t_state |= TIMEOUT;
40613Sbill 			goto out;
40713Sbill 		}
40813Sbill 	}
40913Sbill 	/*
41013Sbill 	 * If any characters were set up, start transmission;
41113Sbill 	 */
41213Sbill 	if (nch) {
41313Sbill 		addr->un.dhcsrl = (d&017)|IENAB;
41413Sbill 		addr->dhcar = UBACVT(tp->t_outq.c_cf);
41513Sbill 		addr->dhbcr = -nch;
41613Sbill 		nch = 1<<(d&017);
41713Sbill 		addr->dhbar |= nch;
41813Sbill 		dhsar[d>>4] |= nch;
41913Sbill 		tp->t_state |= BUSY;
42013Sbill 	}
42113Sbill     out:
42213Sbill 	splx(s);
42313Sbill }
42413Sbill 
42513Sbill /*
42613Sbill  * Stop output on a line.
42713Sbill  * Assume call is made at spl6.
42813Sbill  */
42913Sbill /*ARGSUSED*/
43013Sbill dhstop(tp, flag)
43113Sbill register struct tty *tp;
43213Sbill {
433113Sbill 	register struct device *addr;
434113Sbill 	register d, s;
43513Sbill 
436113Sbill 	addr = (struct device *)tp->t_addr;
43713Sbill 	s = spl6();
438113Sbill 	if (tp->t_state & BUSY) {
439113Sbill 		d = minor(tp->t_dev);
440113Sbill 		addr->un.dhcsrl = (d&017) | IENAB;
44113Sbill 		if ((tp->t_state&TTSTOP)==0)
44213Sbill 			tp->t_state |= FLUSH;
443113Sbill 		addr->dhbcr = -1;
444113Sbill 	}
44513Sbill 	splx(s);
44613Sbill }
44713Sbill 
448117Sbill int	dhsilo = 16;
449168Sbill /*
450168Sbill  * Silo control is fixed strategy
451168Sbill  * here, paralleling only option available
452168Sbill  * on DZ-11.
453168Sbill  */
45413Sbill /*ARGSUSED*/
455168Sbill dhtimer()
45613Sbill {
457168Sbill 	register d;
458117Sbill 	register struct device *addr;
459117Sbill 
46013Sbill 	addr = DHADDR; d = 0;
46113Sbill 	do {
462117Sbill 		if (dhact & (1<<d)) {
463280Sbill 			if ((dhisilo & (1<<d)) == 0) {
464280Sbill 				addr->dhsilo = dhsilo;
465280Sbill 				dhisilo |= 1<<d;
466280Sbill 			}
467117Sbill 			dhrint(d);
468117Sbill 		}
469117Sbill 		d++;
47013Sbill 		addr++;
47113Sbill 	} while (d < (NDH11+15)/16);
47213Sbill }
473280Sbill 
474280Sbill /*
475280Sbill  * Reset state of driver if UBA reset was necessary.
476280Sbill  * Reset the csrl and lpr registers on open lines, and
477280Sbill  * restart transmitters.
478280Sbill  */
479280Sbill dhreset()
480280Sbill {
481280Sbill 	int d;
482280Sbill 	register struct tty *tp;
483280Sbill 	register struct device *addr;
484280Sbill 
485280Sbill 	if (getcbase == 0)
486280Sbill 		return;
487280Sbill 	printf(" dh");
488280Sbill 	dhisilo = 0;
489280Sbill 	ubafree(dh_ubinfo);
490280Sbill 	dh_ubinfo = uballoc((caddr_t)cfree, NCLIST*sizeof (struct cblock), 0);
491280Sbill 	cbase = (short)dh_ubinfo;
492280Sbill 	d = 0;
493280Sbill 	do {
494280Sbill 		addr = DHADDR + d;
495280Sbill 		if (dhact & (1<<d))
496300Sbill 			addr->un.dhcsr |= IENAB;
497280Sbill 		d++;
498280Sbill 	} while (d < (NDH11+15)/16);
499300Sbill 	for (d = 0; d < NDH11; d++) {
500300Sbill 		tp = &dh11[d];
501300Sbill 		if (tp->t_state & (ISOPEN|WOPEN)) {
502300Sbill 			dhparam(d);
503300Sbill 			dmctl(d, TURNON, DMSET);
504300Sbill 			tp->t_state &= ~BUSY;
505300Sbill 			dhstart(tp);
506300Sbill 		}
507300Sbill 	}
508300Sbill 	dhtimer();
509280Sbill }
510