xref: /netbsd/sys/arch/vax/vax/gencons.c (revision bf9ec67e)
1 /*	$NetBSD: gencons.c,v 1.36 2002/03/17 19:40:52 atatat Exp $	*/
2 
3 /*
4  * Copyright (c) 1994 Gordon W. Ross
5  * Copyright (c) 1994 Ludd, University of Lule}, Sweden.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *     This product includes software developed at Ludd, University of Lule}.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  *	kd.c,v 1.2 1994/05/05 04:46:51 gwr Exp $
34  */
35 
36  /* All bugs are subject to removal without further notice */
37 
38 #include "opt_ddb.h"
39 #include "opt_cputype.h"
40 #include "opt_multiprocessor.h"
41 
42 #include <sys/param.h>
43 #include <sys/proc.h>
44 #include <sys/systm.h>
45 #include <sys/ioctl.h>
46 #include <sys/tty.h>
47 #include <sys/file.h>
48 #include <sys/conf.h>
49 #include <sys/device.h>
50 #include <sys/reboot.h>
51 #include <sys/kernel.h>
52 
53 #include <dev/cons.h>
54 
55 #include <machine/mtpr.h>
56 #include <machine/sid.h>
57 #include <machine/cpu.h>
58 #include <machine/scb.h>
59 #include <machine/../vax/gencons.h>
60 
61 static	struct gc_softc {
62 	short alive;
63 	short unit;
64 	struct tty *gencn_tty;
65 } gc_softc[4];
66 
67 static	int maxttys = 1;
68 
69 static	int pr_txcs[4] = {PR_TXCS, PR_TXCS1, PR_TXCS2, PR_TXCS3};
70 static	int pr_rxcs[4] = {PR_RXCS, PR_RXCS1, PR_RXCS2, PR_RXCS3};
71 static	int pr_txdb[4] = {PR_TXDB, PR_TXDB1, PR_TXDB2, PR_TXDB3};
72 static	int pr_rxdb[4] = {PR_RXDB, PR_RXDB1, PR_RXDB2, PR_RXDB3};
73 
74 cons_decl(gen);
75 cdev_decl(gencn);
76 
77 static	int gencnparam __P((struct tty *, struct termios *));
78 static	void gencnstart __P((struct tty *));
79 
80 int
81 gencnopen(dev_t dev, int flag, int mode, struct proc *p)
82 {
83 	int unit;
84 	struct tty *tp;
85 
86 	unit = minor(dev);
87 	if (unit >= maxttys)
88 		return ENXIO;
89 
90 	if (gc_softc[unit].gencn_tty == NULL)
91 		gc_softc[unit].gencn_tty = ttymalloc();
92 
93 	gc_softc[unit].alive = 1;
94 	gc_softc[unit].unit = unit;
95 	tp = gc_softc[unit].gencn_tty;
96 
97 	tp->t_oproc = gencnstart;
98 	tp->t_param = gencnparam;
99 	tp->t_dev = dev;
100 	if ((tp->t_state & TS_ISOPEN) == 0) {
101 		ttychars(tp);
102 		tp->t_iflag = TTYDEF_IFLAG;
103 		tp->t_oflag = TTYDEF_OFLAG;
104 		tp->t_cflag = TTYDEF_CFLAG;
105 		tp->t_lflag = TTYDEF_LFLAG;
106 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
107 		gencnparam(tp, &tp->t_termios);
108 		ttsetwater(tp);
109 	} else if (tp->t_state & TS_XCLUDE && p->p_ucred->cr_uid != 0)
110 		return EBUSY;
111 	tp->t_state |= TS_CARR_ON;
112 
113 	return ((*tp->t_linesw->l_open)(dev, tp));
114 }
115 
116 int
117 gencnclose(dev_t dev, int flag, int mode, struct proc *p)
118 {
119 	struct tty *tp = gc_softc[minor(dev)].gencn_tty;
120 
121 	gc_softc[minor(dev)].alive = 0;
122 	(*tp->t_linesw->l_close)(tp, flag);
123 	ttyclose(tp);
124 	return (0);
125 }
126 
127 struct tty *
128 gencntty(dev_t dev)
129 {
130 	return gc_softc[minor(dev)].gencn_tty;
131 }
132 
133 int
134 gencnread(dev_t dev, struct uio *uio, int flag)
135 {
136 	struct tty *tp = gc_softc[minor(dev)].gencn_tty;
137 
138 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
139 }
140 
141 int
142 gencnwrite(dev_t dev, struct uio *uio, int flag)
143 {
144 	struct tty *tp = gc_softc[minor(dev)].gencn_tty;
145 
146 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
147 }
148 
149 int
150 gencnpoll(dev_t dev, int events, struct proc *p)
151 {
152 	struct tty *tp = gc_softc[minor(dev)].gencn_tty;
153 
154 	return ((*tp->t_linesw->l_poll)(tp, events, p));
155 }
156 
157 int
158 gencnioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
159 {
160 	struct tty *tp = gc_softc[minor(dev)].gencn_tty;
161 	int error;
162 
163 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
164 	if (error != EPASSTHROUGH)
165 		return error;
166 	return ttioctl(tp, cmd, data, flag, p);
167 }
168 
169 void
170 gencnstart(struct tty *tp)
171 {
172 	struct clist *cl;
173 	int s, ch;
174 
175 #if defined(MULTIPROCESSOR)
176 	if ((curcpu()->ci_flags & CI_MASTERCPU) == 0)
177 		return cpu_send_ipi(IPI_DEST_MASTER, IPI_START_CNTX);
178 #endif
179 
180 	s = spltty();
181 	if (tp->t_state & (TS_BUSY|TS_TTSTOP|TS_TIMEOUT))
182 		goto out;
183 	cl = &tp->t_outq;
184 
185 	if(cl->c_cc){
186 		tp->t_state |= TS_BUSY;
187 		ch = getc(cl);
188 		mtpr(ch, pr_txdb[minor(tp->t_dev)]);
189 	} else {
190 		if (tp->t_state & TS_ASLEEP) {
191 			tp->t_state &= ~TS_ASLEEP;
192 			wakeup((caddr_t)cl);
193 		}
194 		selwakeup(&tp->t_wsel);
195 	}
196 
197 out:	splx(s);
198 }
199 
200 static void
201 gencnrint(void *arg)
202 {
203 	struct gc_softc *sc = arg;
204 	struct tty *tp = sc->gencn_tty;
205 	int i;
206 
207 	if (sc->alive == 0)
208 		return;
209 	i = mfpr(pr_rxdb[sc->unit]) & 0377; /* Mask status flags etc... */
210 	KERNEL_LOCK(LK_CANRECURSE|LK_EXCLUSIVE);
211 
212 #ifdef DDB
213 	if (tp->t_dev == cn_tab->cn_dev) {
214 		int j = kdbrint(i);
215 
216 		if (j == 1) {	/* Escape received, just return */
217 			KERNEL_UNLOCK();
218 			return;
219 		}
220 
221 		if (j == 2)	/* Second char wasn't 'D' */
222 			(*tp->t_linesw->l_rint)(27, tp);
223 	}
224 #endif
225 
226 	(*tp->t_linesw->l_rint)(i, tp);
227 	KERNEL_UNLOCK();
228 }
229 
230 void
231 gencnstop(struct tty *tp, int flag)
232 {
233 }
234 
235 static void
236 gencntint(void *arg)
237 {
238 	struct gc_softc *sc = arg;
239 	struct tty *tp = sc->gencn_tty;
240 
241 	if (sc->alive == 0)
242 		return;
243 	KERNEL_LOCK(LK_CANRECURSE|LK_EXCLUSIVE);
244 	tp->t_state &= ~TS_BUSY;
245 
246 	gencnstart(tp);
247 	KERNEL_UNLOCK();
248 }
249 
250 int
251 gencnparam(struct tty *tp, struct termios *t)
252 {
253 	/* XXX - These are ignored... */
254 	tp->t_ispeed = t->c_ispeed;
255 	tp->t_ospeed = t->c_ospeed;
256 	tp->t_cflag = t->c_cflag;
257 	return 0;
258 }
259 
260 void
261 gencnprobe(struct consdev *cndev)
262 {
263 	if ((vax_cputype < VAX_TYP_UV2) || /* All older has MTPR console */
264 	    (vax_boardtype == VAX_BTYP_9RR) ||
265 	    (vax_boardtype == VAX_BTYP_630) ||
266 	    (vax_boardtype == VAX_BTYP_660) ||
267 	    (vax_boardtype == VAX_BTYP_670) ||
268 	    (vax_boardtype == VAX_BTYP_680) ||
269 	    (vax_boardtype == VAX_BTYP_681) ||
270 	    (vax_boardtype == VAX_BTYP_650)) {
271 		cndev->cn_dev = makedev(25, 0);
272 		cndev->cn_pri = CN_NORMAL;
273 	} else
274 		cndev->cn_pri = CN_DEAD;
275 }
276 
277 void
278 gencninit(struct consdev *cndev)
279 {
280 
281 	/* Allocate interrupt vectors */
282 	scb_vecalloc(SCB_G0R, gencnrint, &gc_softc[0], SCB_ISTACK, NULL);
283 	scb_vecalloc(SCB_G0T, gencntint, &gc_softc[0], SCB_ISTACK, NULL);
284 	mtpr(GC_RIE, pr_rxcs[0]); /* Turn on interrupts */
285 	mtpr(GC_TIE, pr_txcs[0]);
286 
287 	if (vax_cputype == VAX_TYP_8SS) {
288 		maxttys = 4;
289 		scb_vecalloc(SCB_G1R, gencnrint, &gc_softc[1], SCB_ISTACK, NULL);
290 		scb_vecalloc(SCB_G1T, gencntint, &gc_softc[1], SCB_ISTACK, NULL);
291 
292 		scb_vecalloc(SCB_G2R, gencnrint, &gc_softc[2], SCB_ISTACK, NULL);
293 		scb_vecalloc(SCB_G2T, gencntint, &gc_softc[2], SCB_ISTACK, NULL);
294 
295 		scb_vecalloc(SCB_G3R, gencnrint, &gc_softc[3], SCB_ISTACK, NULL);
296 		scb_vecalloc(SCB_G3T, gencntint, &gc_softc[3], SCB_ISTACK, NULL);
297 	}
298 #if 0
299 	mtpr(0, PR_RXCS);
300 	mtpr(0, PR_TXCS);
301 	mtpr(0, PR_TBIA); /* ??? */
302 #endif
303 }
304 
305 void
306 gencnputc(dev_t dev, int ch)
307 {
308 #if VAX8800 || VAXANY
309 	/*
310 	 * On KA88 we may get C-S/C-Q from the console.
311 	 * XXX - this will cause a loop at spltty() in kernel and will
312 	 * interfere with other console communication. Fortunately
313 	 * kernel printf's are uncommon.
314 	 */
315 	if (vax_cputype == VAX_TYP_8NN) {
316 		int s = spltty();
317 
318 		while (mfpr(PR_RXCS) & GC_DON) {
319 			if ((mfpr(PR_RXDB) & 0x7f) == 19) {
320 				while (1) {
321 					while ((mfpr(PR_RXCS) & GC_DON) == 0)
322 						;
323 					if ((mfpr(PR_RXDB) & 0x7f) == 17)
324 						break;
325 				}
326 			}
327 		}
328 		splx(s);
329 	}
330 #endif
331 
332 	while ((mfpr(PR_TXCS) & GC_RDY) == 0) /* Wait until xmit ready */
333 		;
334 	mtpr(ch, PR_TXDB);	/* xmit character */
335 	if(ch == 10)
336 		gencnputc(dev, 13); /* CR/LF */
337 
338 }
339 
340 int
341 gencngetc(dev_t dev)
342 {
343 	int i;
344 
345 	while ((mfpr(PR_RXCS) & GC_DON) == 0) /* Receive chr */
346 		;
347 	i = mfpr(PR_RXDB) & 0x7f;
348 	if (i == 13)
349 		i = 10;
350 	return i;
351 }
352 
353 void
354 gencnpollc(dev_t dev, int pollflag)
355 {
356 	if (pollflag)  {
357 		mtpr(0, PR_RXCS);
358 		mtpr(0, PR_TXCS);
359 	} else {
360 		mtpr(GC_RIE, PR_RXCS);
361 		mtpr(GC_TIE, PR_TXCS);
362 	}
363 }
364 
365 #if defined(MULTIPROCESSOR)
366 void
367 gencnstarttx()
368 {
369 	gencnstart(gc_softc[0].gencn_tty);
370 }
371 #endif
372