zs.c revision 1.12 1 1.1 deraadt /*
2 1.1 deraadt * Copyright (c) 1992, 1993
3 1.1 deraadt * The Regents of the University of California. All rights reserved.
4 1.1 deraadt *
5 1.1 deraadt * This software was developed by the Computer Systems Engineering group
6 1.1 deraadt * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
7 1.1 deraadt * contributed to Berkeley.
8 1.1 deraadt *
9 1.1 deraadt * All advertising materials mentioning features or use of this software
10 1.1 deraadt * must display the following acknowledgement:
11 1.1 deraadt * This product includes software developed by the University of
12 1.1 deraadt * California, Lawrence Berkeley Laboratory.
13 1.1 deraadt *
14 1.1 deraadt * Redistribution and use in source and binary forms, with or without
15 1.1 deraadt * modification, are permitted provided that the following conditions
16 1.1 deraadt * are met:
17 1.1 deraadt * 1. Redistributions of source code must retain the above copyright
18 1.1 deraadt * notice, this list of conditions and the following disclaimer.
19 1.1 deraadt * 2. Redistributions in binary form must reproduce the above copyright
20 1.1 deraadt * notice, this list of conditions and the following disclaimer in the
21 1.1 deraadt * documentation and/or other materials provided with the distribution.
22 1.1 deraadt * 3. All advertising materials mentioning features or use of this software
23 1.1 deraadt * must display the following acknowledgement:
24 1.1 deraadt * This product includes software developed by the University of
25 1.1 deraadt * California, Berkeley and its contributors.
26 1.1 deraadt * 4. Neither the name of the University nor the names of its contributors
27 1.1 deraadt * may be used to endorse or promote products derived from this software
28 1.1 deraadt * without specific prior written permission.
29 1.1 deraadt *
30 1.1 deraadt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 1.1 deraadt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 1.1 deraadt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 1.1 deraadt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 1.1 deraadt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 1.1 deraadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 1.1 deraadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 1.1 deraadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 1.1 deraadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 1.1 deraadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 1.1 deraadt * SUCH DAMAGE.
41 1.1 deraadt *
42 1.1 deraadt * @(#)zs.c 8.1 (Berkeley) 7/19/93
43 1.1 deraadt *
44 1.1 deraadt * from: Header: zs.c,v 1.30 93/07/19 23:44:42 torek Exp
45 1.12 deraadt * $Id: zs.c,v 1.12 1994/08/20 09:11:02 deraadt Exp $
46 1.1 deraadt */
47 1.1 deraadt
48 1.1 deraadt /*
49 1.1 deraadt * Zilog Z8530 (ZSCC) driver.
50 1.1 deraadt *
51 1.1 deraadt * Runs two tty ports (ttya and ttyb) on zs0,
52 1.1 deraadt * and runs a keyboard and mouse on zs1.
53 1.1 deraadt *
54 1.1 deraadt * This driver knows far too much about chip to usage mappings.
55 1.1 deraadt */
56 1.1 deraadt #define NZS 2 /* XXX */
57 1.1 deraadt
58 1.1 deraadt #include <sys/param.h>
59 1.1 deraadt #include <sys/proc.h>
60 1.1 deraadt #include <sys/device.h>
61 1.1 deraadt #include <sys/conf.h>
62 1.1 deraadt #include <sys/file.h>
63 1.1 deraadt #include <sys/ioctl.h>
64 1.1 deraadt #include <sys/tty.h>
65 1.1 deraadt #include <sys/time.h>
66 1.1 deraadt #include <sys/kernel.h>
67 1.1 deraadt #include <sys/syslog.h>
68 1.1 deraadt
69 1.1 deraadt #include <machine/autoconf.h>
70 1.1 deraadt #include <machine/cpu.h>
71 1.1 deraadt
72 1.1 deraadt #include <sparc/sparc/vaddrs.h>
73 1.1 deraadt #include <sparc/sparc/auxreg.h>
74 1.1 deraadt
75 1.11 deraadt #include <machine/kbd.h>
76 1.1 deraadt #include <sparc/dev/zsreg.h>
77 1.1 deraadt #include <sparc/dev/zsvar.h>
78 1.1 deraadt
79 1.1 deraadt #ifdef KGDB
80 1.1 deraadt #include <machine/remote-sl.h>
81 1.1 deraadt #endif
82 1.1 deraadt
83 1.1 deraadt #define ZSMAJOR 12 /* XXX */
84 1.1 deraadt
85 1.1 deraadt #define ZS_KBD 2 /* XXX */
86 1.1 deraadt #define ZS_MOUSE 3 /* XXX */
87 1.1 deraadt
88 1.1 deraadt /* the magic number below was stolen from the Sprite source. */
89 1.1 deraadt #define PCLK (19660800/4) /* PCLK pin input clock rate */
90 1.1 deraadt
91 1.1 deraadt /*
92 1.1 deraadt * Select software interrupt bit based on TTY ipl.
93 1.1 deraadt */
94 1.1 deraadt #if PIL_TTY == 1
95 1.1 deraadt # define IE_ZSSOFT IE_L1
96 1.1 deraadt #elif PIL_TTY == 4
97 1.1 deraadt # define IE_ZSSOFT IE_L4
98 1.1 deraadt #elif PIL_TTY == 6
99 1.1 deraadt # define IE_ZSSOFT IE_L6
100 1.1 deraadt #else
101 1.1 deraadt # error "no suitable software interrupt bit"
102 1.1 deraadt #endif
103 1.1 deraadt
104 1.1 deraadt /*
105 1.1 deraadt * Software state per found chip. This would be called `zs_softc',
106 1.1 deraadt * but the previous driver had a rather different zs_softc....
107 1.1 deraadt */
108 1.1 deraadt struct zsinfo {
109 1.1 deraadt struct device zi_dev; /* base device */
110 1.1 deraadt volatile struct zsdevice *zi_zs;/* chip registers */
111 1.1 deraadt struct zs_chanstate zi_cs[2]; /* channel A and B software state */
112 1.1 deraadt };
113 1.1 deraadt
114 1.2 deraadt struct tty *zs_tty[NZS * 2]; /* XXX should be dynamic */
115 1.1 deraadt
116 1.1 deraadt /* Definition of the driver for autoconfig. */
117 1.1 deraadt static int zsmatch(struct device *, struct cfdata *, void *);
118 1.1 deraadt static void zsattach(struct device *, struct device *, void *);
119 1.1 deraadt struct cfdriver zscd =
120 1.1 deraadt { NULL, "zs", zsmatch, zsattach, DV_TTY, sizeof(struct zsinfo) };
121 1.1 deraadt
122 1.1 deraadt /* Interrupt handlers. */
123 1.1 deraadt static int zshard(void *);
124 1.1 deraadt static struct intrhand levelhard = { zshard };
125 1.1 deraadt static int zssoft(void *);
126 1.1 deraadt static struct intrhand levelsoft = { zssoft };
127 1.1 deraadt
128 1.1 deraadt struct zs_chanstate *zslist;
129 1.1 deraadt
130 1.1 deraadt /* Routines called from other code. */
131 1.1 deraadt static void zsiopen(struct tty *);
132 1.1 deraadt static void zsiclose(struct tty *);
133 1.1 deraadt static void zsstart(struct tty *);
134 1.3 deraadt void zsstop(struct tty *, int);
135 1.1 deraadt static int zsparam(struct tty *, struct termios *);
136 1.1 deraadt
137 1.1 deraadt /* Routines purely local to this driver. */
138 1.1 deraadt static int zs_getspeed(volatile struct zschan *);
139 1.1 deraadt static void zs_reset(volatile struct zschan *, int, int);
140 1.1 deraadt static void zs_modem(struct zs_chanstate *, int);
141 1.1 deraadt static void zs_loadchannelregs(volatile struct zschan *, u_char *);
142 1.1 deraadt
143 1.1 deraadt /* Console stuff. */
144 1.1 deraadt static struct tty *zs_ctty; /* console `struct tty *' */
145 1.1 deraadt static int zs_consin = -1, zs_consout = -1;
146 1.1 deraadt static int zscnputc(int); /* console putc function */
147 1.1 deraadt static volatile struct zschan *zs_conschan;
148 1.1 deraadt static struct tty *zs_checkcons(struct zsinfo *, int, struct zs_chanstate *);
149 1.1 deraadt
150 1.1 deraadt #ifdef KGDB
151 1.1 deraadt /* KGDB stuff. Must reboot to change zs_kgdbunit. */
152 1.1 deraadt extern int kgdb_dev, kgdb_rate;
153 1.1 deraadt static int zs_kgdb_savedspeed;
154 1.1 deraadt static void zs_checkkgdb(int, struct zs_chanstate *, struct tty *);
155 1.1 deraadt #endif
156 1.1 deraadt
157 1.1 deraadt extern volatile struct zsdevice *findzs(int);
158 1.1 deraadt static volatile struct zsdevice *zsaddr[NZS]; /* XXX, but saves work */
159 1.1 deraadt
160 1.1 deraadt /*
161 1.1 deraadt * Console keyboard L1-A processing is done in the hardware interrupt code,
162 1.1 deraadt * so we need to duplicate some of the console keyboard decode state. (We
163 1.1 deraadt * must not use the regular state as the hardware code keeps ahead of the
164 1.1 deraadt * software state: the software state tracks the most recent ring input but
165 1.1 deraadt * the hardware state tracks the most recent ZSCC input.) See also kbd.h.
166 1.1 deraadt */
167 1.1 deraadt static struct conk_state { /* console keyboard state */
168 1.1 deraadt char conk_id; /* true => ID coming up (console only) */
169 1.1 deraadt char conk_l1; /* true => L1 pressed (console only) */
170 1.1 deraadt } zsconk_state;
171 1.1 deraadt
172 1.1 deraadt int zshardscope;
173 1.1 deraadt int zsshortcuts; /* number of "shortcut" software interrupts */
174 1.1 deraadt
175 1.12 deraadt #ifdef SUN4
176 1.12 deraadt static u_char
177 1.12 deraadt zs_read(zc, reg)
178 1.12 deraadt volatile struct zschan *zc;
179 1.12 deraadt u_char reg;
180 1.12 deraadt {
181 1.12 deraadt u_char val;
182 1.12 deraadt
183 1.12 deraadt zc->zc_csr = reg;
184 1.12 deraadt ZS_DELAY();
185 1.12 deraadt val = zc->zc_csr;
186 1.12 deraadt ZS_DELAY();
187 1.12 deraadt return val;
188 1.12 deraadt }
189 1.12 deraadt
190 1.12 deraadt static u_char
191 1.12 deraadt zs_write(zc, reg, val)
192 1.12 deraadt volatile struct zschan *zc;
193 1.12 deraadt u_char reg, val;
194 1.12 deraadt {
195 1.12 deraadt zc->zc_csr = reg;
196 1.12 deraadt ZS_DELAY();
197 1.12 deraadt zc->zc_csr = val;
198 1.12 deraadt ZS_DELAY();
199 1.12 deraadt return val;
200 1.12 deraadt }
201 1.12 deraadt #endif /* SUN4 */
202 1.12 deraadt
203 1.1 deraadt /*
204 1.1 deraadt * Match slave number to zs unit number, so that misconfiguration will
205 1.1 deraadt * not set up the keyboard as ttya, etc.
206 1.1 deraadt */
207 1.1 deraadt static int
208 1.1 deraadt zsmatch(struct device *parent, struct cfdata *cf, void *aux)
209 1.1 deraadt {
210 1.1 deraadt struct romaux *ra = aux;
211 1.1 deraadt
212 1.1 deraadt return (getpropint(ra->ra_node, "slave", -2) == cf->cf_unit);
213 1.1 deraadt }
214 1.1 deraadt
215 1.1 deraadt /*
216 1.1 deraadt * Attach a found zs.
217 1.1 deraadt *
218 1.1 deraadt * USE ROM PROPERTIES port-a-ignore-cd AND port-b-ignore-cd FOR
219 1.1 deraadt * SOFT CARRIER, AND keyboard PROPERTY FOR KEYBOARD/MOUSE?
220 1.1 deraadt */
221 1.1 deraadt static void
222 1.1 deraadt zsattach(struct device *parent, struct device *dev, void *aux)
223 1.1 deraadt {
224 1.1 deraadt register int zs = dev->dv_unit, unit;
225 1.1 deraadt register struct zsinfo *zi;
226 1.1 deraadt register struct zs_chanstate *cs;
227 1.1 deraadt register volatile struct zsdevice *addr;
228 1.1 deraadt register struct tty *tp, *ctp;
229 1.1 deraadt register struct romaux *ra = aux;
230 1.1 deraadt int pri, softcar;
231 1.1 deraadt static int didintr, prevpri;
232 1.1 deraadt
233 1.1 deraadt if ((addr = zsaddr[zs]) == NULL)
234 1.1 deraadt addr = zsaddr[zs] = findzs(zs);
235 1.1 deraadt if ((void *)addr != ra->ra_vaddr)
236 1.1 deraadt panic("zsattach");
237 1.1 deraadt if (ra->ra_nintr != 1) {
238 1.1 deraadt printf(": expected 1 interrupt, got %d\n", ra->ra_nintr);
239 1.1 deraadt return;
240 1.1 deraadt }
241 1.1 deraadt pri = ra->ra_intr[0].int_pri;
242 1.1 deraadt printf(" pri %d, softpri %d\n", pri, PIL_TTY);
243 1.1 deraadt if (!didintr) {
244 1.1 deraadt didintr = 1;
245 1.1 deraadt prevpri = pri;
246 1.1 deraadt intr_establish(pri, &levelhard);
247 1.1 deraadt intr_establish(PIL_TTY, &levelsoft);
248 1.1 deraadt } else if (pri != prevpri)
249 1.1 deraadt panic("broken zs interrupt scheme");
250 1.1 deraadt zi = (struct zsinfo *)dev;
251 1.1 deraadt zi->zi_zs = addr;
252 1.1 deraadt unit = zs * 2;
253 1.1 deraadt cs = zi->zi_cs;
254 1.1 deraadt
255 1.2 deraadt if(!zs_tty[unit])
256 1.2 deraadt zs_tty[unit] = ttymalloc();
257 1.2 deraadt tp = zs_tty[unit];
258 1.2 deraadt if(!zs_tty[unit+1])
259 1.2 deraadt zs_tty[unit+1] = ttymalloc();
260 1.2 deraadt
261 1.1 deraadt if (unit == 0) {
262 1.1 deraadt /* Get software carrier flags from options node in OPENPROM. */
263 1.1 deraadt extern int optionsnode;
264 1.1 deraadt
265 1.1 deraadt softcar = 0;
266 1.1 deraadt if (*getpropstring(optionsnode, "ttya-ignore-cd") == 't')
267 1.1 deraadt softcar |= 1;
268 1.1 deraadt if (*getpropstring(optionsnode, "ttyb-ignore-cd") == 't')
269 1.1 deraadt softcar |= 2;
270 1.1 deraadt } else
271 1.1 deraadt softcar = dev->dv_cfdata->cf_flags;
272 1.1 deraadt
273 1.1 deraadt /* link into interrupt list with order (A,B) (B=A+1) */
274 1.1 deraadt cs[0].cs_next = &cs[1];
275 1.1 deraadt cs[1].cs_next = zslist;
276 1.1 deraadt zslist = cs;
277 1.1 deraadt
278 1.1 deraadt cs->cs_unit = unit;
279 1.1 deraadt cs->cs_speed = zs_getspeed(&addr->zs_chan[CHAN_A]);
280 1.1 deraadt cs->cs_softcar = softcar & 1;
281 1.1 deraadt cs->cs_zc = &addr->zs_chan[CHAN_A];
282 1.1 deraadt tp->t_dev = makedev(ZSMAJOR, unit);
283 1.1 deraadt tp->t_oproc = zsstart;
284 1.1 deraadt tp->t_param = zsparam;
285 1.1 deraadt if ((ctp = zs_checkcons(zi, unit, cs)) != NULL)
286 1.1 deraadt tp = ctp;
287 1.1 deraadt cs->cs_ttyp = tp;
288 1.1 deraadt #ifdef KGDB
289 1.1 deraadt if (ctp == NULL)
290 1.1 deraadt zs_checkkgdb(unit, cs, tp);
291 1.1 deraadt #endif
292 1.1 deraadt if (unit == ZS_KBD) {
293 1.1 deraadt /*
294 1.1 deraadt * Keyboard: tell /dev/kbd driver how to talk to us.
295 1.1 deraadt */
296 1.1 deraadt tp->t_ispeed = tp->t_ospeed = cs->cs_speed;
297 1.1 deraadt tp->t_cflag = CS8;
298 1.1 deraadt kbd_serial(tp, zsiopen, zsiclose);
299 1.1 deraadt cs->cs_conk = 1; /* do L1-A processing */
300 1.1 deraadt }
301 1.1 deraadt unit++;
302 1.1 deraadt cs++;
303 1.2 deraadt tp = zs_tty[unit];
304 1.1 deraadt cs->cs_unit = unit;
305 1.1 deraadt cs->cs_speed = zs_getspeed(&addr->zs_chan[CHAN_B]);
306 1.1 deraadt cs->cs_softcar = softcar & 2;
307 1.1 deraadt cs->cs_zc = &addr->zs_chan[CHAN_B];
308 1.1 deraadt tp->t_dev = makedev(ZSMAJOR, unit);
309 1.1 deraadt tp->t_oproc = zsstart;
310 1.1 deraadt tp->t_param = zsparam;
311 1.1 deraadt if ((ctp = zs_checkcons(zi, unit, cs)) != NULL)
312 1.1 deraadt tp = ctp;
313 1.1 deraadt cs->cs_ttyp = tp;
314 1.1 deraadt #ifdef KGDB
315 1.1 deraadt if (ctp == NULL)
316 1.1 deraadt zs_checkkgdb(unit, cs, tp);
317 1.1 deraadt #endif
318 1.1 deraadt if (unit == ZS_MOUSE) {
319 1.1 deraadt /*
320 1.1 deraadt * Mouse: tell /dev/mouse driver how to talk to us.
321 1.1 deraadt */
322 1.1 deraadt tp->t_ispeed = tp->t_ospeed = cs->cs_speed;
323 1.1 deraadt tp->t_cflag = CS8;
324 1.1 deraadt ms_serial(tp, zsiopen, zsiclose);
325 1.1 deraadt }
326 1.1 deraadt }
327 1.1 deraadt
328 1.1 deraadt /*
329 1.1 deraadt * Put a channel in a known state. Interrupts may be left disabled
330 1.1 deraadt * or enabled, as desired.
331 1.1 deraadt */
332 1.1 deraadt static void
333 1.1 deraadt zs_reset(zc, inten, speed)
334 1.1 deraadt volatile struct zschan *zc;
335 1.1 deraadt int inten, speed;
336 1.1 deraadt {
337 1.1 deraadt int tconst;
338 1.1 deraadt static u_char reg[16] = {
339 1.1 deraadt 0,
340 1.1 deraadt 0,
341 1.1 deraadt 0,
342 1.1 deraadt ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
343 1.1 deraadt ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
344 1.1 deraadt ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
345 1.1 deraadt 0,
346 1.1 deraadt 0,
347 1.1 deraadt 0,
348 1.1 deraadt 0,
349 1.1 deraadt ZSWR10_NRZ,
350 1.1 deraadt ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
351 1.1 deraadt 0,
352 1.1 deraadt 0,
353 1.1 deraadt ZSWR14_BAUD_FROM_PCLK | ZSWR14_BAUD_ENA,
354 1.1 deraadt ZSWR15_BREAK_IE | ZSWR15_DCD_IE,
355 1.1 deraadt };
356 1.1 deraadt
357 1.1 deraadt reg[9] = inten ? ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR : ZSWR9_NO_VECTOR;
358 1.1 deraadt tconst = BPS_TO_TCONST(PCLK / 16, speed);
359 1.1 deraadt reg[12] = tconst;
360 1.1 deraadt reg[13] = tconst >> 8;
361 1.1 deraadt zs_loadchannelregs(zc, reg);
362 1.1 deraadt }
363 1.1 deraadt
364 1.1 deraadt /*
365 1.1 deraadt * Declare the given tty (which is in fact &cons) as a console input
366 1.1 deraadt * or output. This happens before the zs chip is attached; the hookup
367 1.1 deraadt * is finished later, in zs_setcons() below.
368 1.1 deraadt *
369 1.1 deraadt * This is used only for ports a and b. The console keyboard is decoded
370 1.1 deraadt * independently (we always send unit-2 input to /dev/kbd, which will
371 1.1 deraadt * direct it to /dev/console if appropriate).
372 1.1 deraadt */
373 1.1 deraadt void
374 1.3 deraadt zsconsole(tp, unit, out, fnstop)
375 1.1 deraadt register struct tty *tp;
376 1.1 deraadt register int unit;
377 1.1 deraadt int out;
378 1.3 deraadt void (**fnstop) __P((struct tty *, int));
379 1.1 deraadt {
380 1.1 deraadt extern int (*v_putc)();
381 1.1 deraadt int zs;
382 1.1 deraadt volatile struct zsdevice *addr;
383 1.1 deraadt
384 1.1 deraadt if (unit >= ZS_KBD)
385 1.1 deraadt panic("zsconsole");
386 1.1 deraadt if (out) {
387 1.1 deraadt zs_consout = unit;
388 1.1 deraadt zs = unit >> 1;
389 1.1 deraadt if ((addr = zsaddr[zs]) == NULL)
390 1.1 deraadt addr = zsaddr[zs] = findzs(zs);
391 1.1 deraadt zs_conschan = (unit & 1) == 0 ? &addr->zs_chan[CHAN_A] :
392 1.1 deraadt &addr->zs_chan[CHAN_B];
393 1.1 deraadt v_putc = zscnputc;
394 1.1 deraadt } else
395 1.1 deraadt zs_consin = unit;
396 1.3 deraadt if(fnstop)
397 1.3 deraadt *fnstop = &zsstop;
398 1.1 deraadt zs_ctty = tp;
399 1.1 deraadt }
400 1.1 deraadt
401 1.1 deraadt /*
402 1.1 deraadt * Polled console output putchar.
403 1.1 deraadt */
404 1.1 deraadt static int
405 1.1 deraadt zscnputc(c)
406 1.1 deraadt int c;
407 1.1 deraadt {
408 1.1 deraadt register volatile struct zschan *zc = zs_conschan;
409 1.1 deraadt register int s;
410 1.1 deraadt
411 1.1 deraadt if (c == '\n')
412 1.1 deraadt zscnputc('\r');
413 1.1 deraadt /*
414 1.1 deraadt * Must block output interrupts (i.e., raise to >= splzs) without
415 1.1 deraadt * lowering current ipl. Need a better way.
416 1.1 deraadt */
417 1.1 deraadt s = splhigh();
418 1.10 deraadt #ifdef SUN4C /* XXX */
419 1.1 deraadt if (s <= (12 << 8))
420 1.1 deraadt (void) splzs();
421 1.1 deraadt #endif
422 1.1 deraadt while ((zc->zc_csr & ZSRR0_TX_READY) == 0)
423 1.12 deraadt ZS_DELAY();
424 1.1 deraadt zc->zc_data = c;
425 1.12 deraadt ZS_DELAY();
426 1.1 deraadt splx(s);
427 1.1 deraadt }
428 1.1 deraadt
429 1.1 deraadt /*
430 1.1 deraadt * Set up the given unit as console input, output, both, or neither, as
431 1.1 deraadt * needed. Return console tty if it is to receive console input.
432 1.1 deraadt */
433 1.1 deraadt static struct tty *
434 1.1 deraadt zs_checkcons(struct zsinfo *zi, int unit, struct zs_chanstate *cs)
435 1.1 deraadt {
436 1.1 deraadt register struct tty *tp;
437 1.1 deraadt char *i, *o;
438 1.1 deraadt
439 1.1 deraadt if ((tp = zs_ctty) == NULL)
440 1.1 deraadt return (0);
441 1.1 deraadt i = zs_consin == unit ? "input" : NULL;
442 1.1 deraadt o = zs_consout == unit ? "output" : NULL;
443 1.1 deraadt if (i == NULL && o == NULL)
444 1.1 deraadt return (0);
445 1.1 deraadt
446 1.1 deraadt /* rewire the minor device (gack) */
447 1.1 deraadt tp->t_dev = makedev(major(tp->t_dev), unit);
448 1.1 deraadt
449 1.1 deraadt /*
450 1.1 deraadt * Rewire input and/or output. Note that baud rate reflects
451 1.1 deraadt * input settings, not output settings, but we can do no better
452 1.1 deraadt * if the console is split across two ports.
453 1.1 deraadt *
454 1.1 deraadt * XXX split consoles don't work anyway -- this needs to be
455 1.1 deraadt * thrown away and redone
456 1.1 deraadt */
457 1.1 deraadt if (i) {
458 1.1 deraadt tp->t_param = zsparam;
459 1.1 deraadt tp->t_ispeed = tp->t_ospeed = cs->cs_speed;
460 1.1 deraadt tp->t_cflag = CS8;
461 1.1 deraadt ttsetwater(tp);
462 1.1 deraadt }
463 1.1 deraadt if (o) {
464 1.1 deraadt tp->t_oproc = zsstart;
465 1.1 deraadt }
466 1.1 deraadt printf("%s%c: console %s\n",
467 1.1 deraadt zi->zi_dev.dv_xname, (unit & 1) + 'a', i ? (o ? "i/o" : i) : o);
468 1.1 deraadt cs->cs_consio = 1;
469 1.1 deraadt cs->cs_brkabort = 1;
470 1.1 deraadt return (tp);
471 1.1 deraadt }
472 1.1 deraadt
473 1.1 deraadt #ifdef KGDB
474 1.1 deraadt /*
475 1.1 deraadt * The kgdb zs port, if any, was altered at boot time (see zs_kgdb_init).
476 1.1 deraadt * Pick up the current speed and character size and restore the original
477 1.1 deraadt * speed.
478 1.1 deraadt */
479 1.1 deraadt static void
480 1.1 deraadt zs_checkkgdb(int unit, struct zs_chanstate *cs, struct tty *tp)
481 1.1 deraadt {
482 1.1 deraadt
483 1.1 deraadt if (kgdb_dev == makedev(ZSMAJOR, unit)) {
484 1.1 deraadt tp->t_ispeed = tp->t_ospeed = kgdb_rate;
485 1.1 deraadt tp->t_cflag = CS8;
486 1.1 deraadt cs->cs_kgdb = 1;
487 1.1 deraadt cs->cs_speed = zs_kgdb_savedspeed;
488 1.1 deraadt (void) zsparam(tp, &tp->t_termios);
489 1.1 deraadt }
490 1.1 deraadt }
491 1.1 deraadt #endif
492 1.1 deraadt
493 1.1 deraadt /*
494 1.1 deraadt * Compute the current baud rate given a ZSCC channel.
495 1.1 deraadt */
496 1.1 deraadt static int
497 1.1 deraadt zs_getspeed(zc)
498 1.1 deraadt register volatile struct zschan *zc;
499 1.1 deraadt {
500 1.1 deraadt register int tconst;
501 1.1 deraadt
502 1.1 deraadt tconst = ZS_READ(zc, 12);
503 1.1 deraadt tconst |= ZS_READ(zc, 13) << 8;
504 1.1 deraadt return (TCONST_TO_BPS(PCLK / 16, tconst));
505 1.1 deraadt }
506 1.1 deraadt
507 1.1 deraadt
508 1.1 deraadt /*
509 1.1 deraadt * Do an internal open.
510 1.1 deraadt */
511 1.1 deraadt static void
512 1.1 deraadt zsiopen(struct tty *tp)
513 1.1 deraadt {
514 1.1 deraadt
515 1.1 deraadt (void) zsparam(tp, &tp->t_termios);
516 1.1 deraadt ttsetwater(tp);
517 1.1 deraadt tp->t_state = TS_ISOPEN | TS_CARR_ON;
518 1.1 deraadt }
519 1.1 deraadt
520 1.1 deraadt /*
521 1.1 deraadt * Do an internal close. Eventually we should shut off the chip when both
522 1.1 deraadt * ports on it are closed.
523 1.1 deraadt */
524 1.1 deraadt static void
525 1.1 deraadt zsiclose(struct tty *tp)
526 1.1 deraadt {
527 1.1 deraadt
528 1.1 deraadt ttylclose(tp, 0); /* ??? */
529 1.1 deraadt ttyclose(tp); /* ??? */
530 1.1 deraadt tp->t_state = 0;
531 1.1 deraadt }
532 1.1 deraadt
533 1.1 deraadt
534 1.1 deraadt /*
535 1.1 deraadt * Open a zs serial port. This interface may not be used to open
536 1.1 deraadt * the keyboard and mouse ports. (XXX)
537 1.1 deraadt */
538 1.1 deraadt int
539 1.1 deraadt zsopen(dev_t dev, int flags, int mode, struct proc *p)
540 1.1 deraadt {
541 1.1 deraadt register struct tty *tp;
542 1.1 deraadt register struct zs_chanstate *cs;
543 1.1 deraadt struct zsinfo *zi;
544 1.1 deraadt int unit = minor(dev), zs = unit >> 1, error, s;
545 1.1 deraadt
546 1.1 deraadt if (zs >= zscd.cd_ndevs || (zi = zscd.cd_devs[zs]) == NULL ||
547 1.1 deraadt unit == ZS_KBD || unit == ZS_MOUSE)
548 1.1 deraadt return (ENXIO);
549 1.1 deraadt cs = &zi->zi_cs[unit & 1];
550 1.1 deraadt if (cs->cs_consio)
551 1.1 deraadt return (ENXIO); /* ??? */
552 1.1 deraadt tp = cs->cs_ttyp;
553 1.1 deraadt s = spltty();
554 1.1 deraadt if ((tp->t_state & TS_ISOPEN) == 0) {
555 1.1 deraadt ttychars(tp);
556 1.1 deraadt if (tp->t_ispeed == 0) {
557 1.1 deraadt tp->t_iflag = TTYDEF_IFLAG;
558 1.1 deraadt tp->t_oflag = TTYDEF_OFLAG;
559 1.1 deraadt tp->t_cflag = TTYDEF_CFLAG;
560 1.1 deraadt tp->t_lflag = TTYDEF_LFLAG;
561 1.1 deraadt tp->t_ispeed = tp->t_ospeed = cs->cs_speed;
562 1.1 deraadt }
563 1.1 deraadt (void) zsparam(tp, &tp->t_termios);
564 1.1 deraadt ttsetwater(tp);
565 1.1 deraadt } else if (tp->t_state & TS_XCLUDE && p->p_ucred->cr_uid != 0) {
566 1.1 deraadt splx(s);
567 1.1 deraadt return (EBUSY);
568 1.1 deraadt }
569 1.1 deraadt error = 0;
570 1.1 deraadt for (;;) {
571 1.1 deraadt /* loop, turning on the device, until carrier present */
572 1.1 deraadt zs_modem(cs, 1);
573 1.1 deraadt if (cs->cs_softcar)
574 1.1 deraadt tp->t_state |= TS_CARR_ON;
575 1.1 deraadt if (flags & O_NONBLOCK || tp->t_cflag & CLOCAL ||
576 1.1 deraadt tp->t_state & TS_CARR_ON)
577 1.1 deraadt break;
578 1.1 deraadt tp->t_state |= TS_WOPEN;
579 1.1 deraadt if (error = ttysleep(tp, (caddr_t)&tp->t_rawq, TTIPRI | PCATCH,
580 1.1 deraadt ttopen, 0))
581 1.1 deraadt break;
582 1.1 deraadt }
583 1.1 deraadt splx(s);
584 1.1 deraadt if (error == 0)
585 1.1 deraadt error = linesw[tp->t_line].l_open(dev, tp);
586 1.1 deraadt if (error)
587 1.1 deraadt zs_modem(cs, 0);
588 1.1 deraadt return (error);
589 1.1 deraadt }
590 1.1 deraadt
591 1.1 deraadt /*
592 1.1 deraadt * Close a zs serial port.
593 1.1 deraadt */
594 1.1 deraadt int
595 1.1 deraadt zsclose(dev_t dev, int flags, int mode, struct proc *p)
596 1.1 deraadt {
597 1.1 deraadt register struct zs_chanstate *cs;
598 1.1 deraadt register struct tty *tp;
599 1.1 deraadt struct zsinfo *zi;
600 1.1 deraadt int unit = minor(dev), s;
601 1.1 deraadt
602 1.1 deraadt zi = zscd.cd_devs[unit >> 1];
603 1.1 deraadt cs = &zi->zi_cs[unit & 1];
604 1.1 deraadt tp = cs->cs_ttyp;
605 1.1 deraadt linesw[tp->t_line].l_close(tp, flags);
606 1.1 deraadt if (tp->t_cflag & HUPCL || tp->t_state & TS_WOPEN ||
607 1.1 deraadt (tp->t_state & TS_ISOPEN) == 0) {
608 1.1 deraadt zs_modem(cs, 0);
609 1.1 deraadt /* hold low for 1 second */
610 1.1 deraadt (void) tsleep((caddr_t)cs, TTIPRI, ttclos, hz);
611 1.1 deraadt }
612 1.7 deraadt if (cs->cs_creg[5] & ZSWR5_BREAK)
613 1.7 deraadt {
614 1.7 deraadt s = splzs();
615 1.7 deraadt cs->cs_preg[5] &= ~ZSWR5_BREAK;
616 1.7 deraadt cs->cs_creg[5] &= ~ZSWR5_BREAK;
617 1.7 deraadt ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
618 1.7 deraadt splx(s);
619 1.7 deraadt }
620 1.1 deraadt ttyclose(tp);
621 1.1 deraadt #ifdef KGDB
622 1.1 deraadt /* Reset the speed if we're doing kgdb on this port */
623 1.1 deraadt if (cs->cs_kgdb) {
624 1.1 deraadt tp->t_ispeed = tp->t_ospeed = kgdb_rate;
625 1.1 deraadt (void) zsparam(tp, &tp->t_termios);
626 1.1 deraadt }
627 1.1 deraadt #endif
628 1.1 deraadt return (0);
629 1.1 deraadt }
630 1.1 deraadt
631 1.1 deraadt /*
632 1.1 deraadt * Read/write zs serial port.
633 1.1 deraadt */
634 1.1 deraadt int
635 1.1 deraadt zsread(dev_t dev, struct uio *uio, int flags)
636 1.1 deraadt {
637 1.2 deraadt register struct tty *tp = zs_tty[minor(dev)];
638 1.1 deraadt
639 1.1 deraadt return (linesw[tp->t_line].l_read(tp, uio, flags));
640 1.1 deraadt }
641 1.1 deraadt
642 1.1 deraadt int
643 1.1 deraadt zswrite(dev_t dev, struct uio *uio, int flags)
644 1.1 deraadt {
645 1.2 deraadt register struct tty *tp = zs_tty[minor(dev)];
646 1.1 deraadt
647 1.1 deraadt return (linesw[tp->t_line].l_write(tp, uio, flags));
648 1.1 deraadt }
649 1.1 deraadt
650 1.1 deraadt /*
651 1.1 deraadt * ZS hardware interrupt. Scan all ZS channels. NB: we know here that
652 1.1 deraadt * channels are kept in (A,B) pairs.
653 1.1 deraadt *
654 1.1 deraadt * Do just a little, then get out; set a software interrupt if more
655 1.1 deraadt * work is needed.
656 1.1 deraadt *
657 1.1 deraadt * We deliberately ignore the vectoring Zilog gives us, and match up
658 1.1 deraadt * only the number of `reset interrupt under service' operations, not
659 1.1 deraadt * the order.
660 1.1 deraadt */
661 1.1 deraadt /* ARGSUSED */
662 1.1 deraadt int
663 1.1 deraadt zshard(void *intrarg)
664 1.1 deraadt {
665 1.1 deraadt register struct zs_chanstate *a;
666 1.1 deraadt #define b (a + 1)
667 1.1 deraadt register volatile struct zschan *zc;
668 1.1 deraadt register int rr3, intflags = 0, v, i;
669 1.1 deraadt static int zsrint(struct zs_chanstate *, volatile struct zschan *);
670 1.1 deraadt static int zsxint(struct zs_chanstate *, volatile struct zschan *);
671 1.1 deraadt static int zssint(struct zs_chanstate *, volatile struct zschan *);
672 1.1 deraadt
673 1.1 deraadt for (a = zslist; a != NULL; a = b->cs_next) {
674 1.1 deraadt rr3 = ZS_READ(a->cs_zc, 3);
675 1.1 deraadt if (rr3 & (ZSRR3_IP_A_RX|ZSRR3_IP_A_TX|ZSRR3_IP_A_STAT)) {
676 1.1 deraadt intflags |= 2;
677 1.1 deraadt zc = a->cs_zc;
678 1.1 deraadt i = a->cs_rbput;
679 1.1 deraadt if (rr3 & ZSRR3_IP_A_RX && (v = zsrint(a, zc)) != 0) {
680 1.1 deraadt a->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
681 1.1 deraadt intflags |= 1;
682 1.1 deraadt }
683 1.1 deraadt if (rr3 & ZSRR3_IP_A_TX && (v = zsxint(a, zc)) != 0) {
684 1.1 deraadt a->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
685 1.1 deraadt intflags |= 1;
686 1.1 deraadt }
687 1.1 deraadt if (rr3 & ZSRR3_IP_A_STAT && (v = zssint(a, zc)) != 0) {
688 1.1 deraadt a->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
689 1.1 deraadt intflags |= 1;
690 1.1 deraadt }
691 1.1 deraadt a->cs_rbput = i;
692 1.1 deraadt }
693 1.1 deraadt if (rr3 & (ZSRR3_IP_B_RX|ZSRR3_IP_B_TX|ZSRR3_IP_B_STAT)) {
694 1.1 deraadt intflags |= 2;
695 1.1 deraadt zc = b->cs_zc;
696 1.1 deraadt i = b->cs_rbput;
697 1.1 deraadt if (rr3 & ZSRR3_IP_B_RX && (v = zsrint(b, zc)) != 0) {
698 1.1 deraadt b->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
699 1.1 deraadt intflags |= 1;
700 1.1 deraadt }
701 1.1 deraadt if (rr3 & ZSRR3_IP_B_TX && (v = zsxint(b, zc)) != 0) {
702 1.1 deraadt b->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
703 1.1 deraadt intflags |= 1;
704 1.1 deraadt }
705 1.1 deraadt if (rr3 & ZSRR3_IP_B_STAT && (v = zssint(b, zc)) != 0) {
706 1.1 deraadt b->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
707 1.1 deraadt intflags |= 1;
708 1.1 deraadt }
709 1.1 deraadt b->cs_rbput = i;
710 1.1 deraadt }
711 1.1 deraadt }
712 1.1 deraadt #undef b
713 1.1 deraadt if (intflags & 1) {
714 1.10 deraadt #ifdef SUN4C /* XXX -- but this will go away when zshard moves to locore.s */
715 1.1 deraadt struct clockframe *p = intrarg;
716 1.1 deraadt
717 1.1 deraadt if ((p->psr & PSR_PIL) < (PIL_TTY << 8)) {
718 1.1 deraadt zsshortcuts++;
719 1.1 deraadt (void) spltty();
720 1.1 deraadt if (zshardscope) {
721 1.1 deraadt LED_ON;
722 1.1 deraadt LED_OFF;
723 1.1 deraadt }
724 1.1 deraadt return (zssoft(intrarg));
725 1.1 deraadt }
726 1.1 deraadt #endif
727 1.1 deraadt ienab_bis(IE_ZSSOFT);
728 1.1 deraadt }
729 1.1 deraadt return (intflags & 2);
730 1.1 deraadt }
731 1.1 deraadt
732 1.1 deraadt static int
733 1.1 deraadt zsrint(register struct zs_chanstate *cs, register volatile struct zschan *zc)
734 1.1 deraadt {
735 1.1 deraadt register int c = zc->zc_data;
736 1.1 deraadt
737 1.1 deraadt if (cs->cs_conk) {
738 1.1 deraadt register struct conk_state *conk = &zsconk_state;
739 1.1 deraadt
740 1.1 deraadt /*
741 1.1 deraadt * Check here for console abort function, so that we
742 1.1 deraadt * can abort even when interrupts are locking up the
743 1.1 deraadt * machine.
744 1.1 deraadt */
745 1.1 deraadt if (c == KBD_RESET) {
746 1.1 deraadt conk->conk_id = 1; /* ignore next byte */
747 1.1 deraadt conk->conk_l1 = 0;
748 1.1 deraadt } else if (conk->conk_id)
749 1.1 deraadt conk->conk_id = 0; /* stop ignoring bytes */
750 1.1 deraadt else if (c == KBD_L1)
751 1.1 deraadt conk->conk_l1 = 1; /* L1 went down */
752 1.1 deraadt else if (c == (KBD_L1|KBD_UP))
753 1.1 deraadt conk->conk_l1 = 0; /* L1 went up */
754 1.1 deraadt else if (c == KBD_A && conk->conk_l1) {
755 1.1 deraadt zsabort();
756 1.1 deraadt conk->conk_l1 = 0; /* we never see the up */
757 1.1 deraadt goto clearit; /* eat the A after L1-A */
758 1.1 deraadt }
759 1.1 deraadt }
760 1.1 deraadt #ifdef KGDB
761 1.1 deraadt if (c == FRAME_START && cs->cs_kgdb &&
762 1.1 deraadt (cs->cs_ttyp->t_state & TS_ISOPEN) == 0) {
763 1.1 deraadt zskgdb(cs->cs_unit);
764 1.1 deraadt goto clearit;
765 1.1 deraadt }
766 1.1 deraadt #endif
767 1.1 deraadt /* compose receive character and status */
768 1.1 deraadt c <<= 8;
769 1.1 deraadt c |= ZS_READ(zc, 1);
770 1.1 deraadt
771 1.1 deraadt /* clear receive error & interrupt condition */
772 1.1 deraadt zc->zc_csr = ZSWR0_RESET_ERRORS;
773 1.1 deraadt zc->zc_csr = ZSWR0_CLR_INTR;
774 1.1 deraadt
775 1.1 deraadt return (ZRING_MAKE(ZRING_RINT, c));
776 1.1 deraadt
777 1.1 deraadt clearit:
778 1.1 deraadt zc->zc_csr = ZSWR0_RESET_ERRORS;
779 1.1 deraadt zc->zc_csr = ZSWR0_CLR_INTR;
780 1.1 deraadt return (0);
781 1.1 deraadt }
782 1.1 deraadt
783 1.1 deraadt static int
784 1.1 deraadt zsxint(register struct zs_chanstate *cs, register volatile struct zschan *zc)
785 1.1 deraadt {
786 1.1 deraadt register int i = cs->cs_tbc;
787 1.1 deraadt
788 1.1 deraadt if (i == 0) {
789 1.1 deraadt zc->zc_csr = ZSWR0_RESET_TXINT;
790 1.1 deraadt zc->zc_csr = ZSWR0_CLR_INTR;
791 1.1 deraadt return (ZRING_MAKE(ZRING_XINT, 0));
792 1.1 deraadt }
793 1.1 deraadt cs->cs_tbc = i - 1;
794 1.1 deraadt zc->zc_data = *cs->cs_tba++;
795 1.1 deraadt zc->zc_csr = ZSWR0_CLR_INTR;
796 1.1 deraadt return (0);
797 1.1 deraadt }
798 1.1 deraadt
799 1.1 deraadt static int
800 1.1 deraadt zssint(register struct zs_chanstate *cs, register volatile struct zschan *zc)
801 1.1 deraadt {
802 1.1 deraadt register int rr0;
803 1.1 deraadt
804 1.1 deraadt rr0 = zc->zc_csr;
805 1.1 deraadt zc->zc_csr = ZSWR0_RESET_STATUS;
806 1.1 deraadt zc->zc_csr = ZSWR0_CLR_INTR;
807 1.1 deraadt /*
808 1.1 deraadt * The chip's hardware flow control is, as noted in zsreg.h,
809 1.1 deraadt * busted---if the DCD line goes low the chip shuts off the
810 1.1 deraadt * receiver (!). If we want hardware CTS flow control but do
811 1.1 deraadt * not have it, and carrier is now on, turn HFC on; if we have
812 1.1 deraadt * HFC now but carrier has gone low, turn it off.
813 1.1 deraadt */
814 1.1 deraadt if (rr0 & ZSRR0_DCD) {
815 1.1 deraadt if (cs->cs_ttyp->t_cflag & CCTS_OFLOW &&
816 1.1 deraadt (cs->cs_creg[3] & ZSWR3_HFC) == 0) {
817 1.1 deraadt cs->cs_creg[3] |= ZSWR3_HFC;
818 1.1 deraadt ZS_WRITE(zc, 3, cs->cs_creg[3]);
819 1.1 deraadt }
820 1.1 deraadt } else {
821 1.1 deraadt if (cs->cs_creg[3] & ZSWR3_HFC) {
822 1.1 deraadt cs->cs_creg[3] &= ~ZSWR3_HFC;
823 1.1 deraadt ZS_WRITE(zc, 3, cs->cs_creg[3]);
824 1.1 deraadt }
825 1.1 deraadt }
826 1.1 deraadt if ((rr0 & ZSRR0_BREAK) && cs->cs_brkabort) {
827 1.12 deraadt #ifdef SUN4
828 1.12 deraadt /*
829 1.12 deraadt * XXX This might not be necessary. Test and
830 1.12 deraadt * delete if it isn't.
831 1.12 deraadt */
832 1.12 deraadt while (zc->zc_csr & ZSRR0_BREAK)
833 1.12 deraadt ZS_DELAY();
834 1.12 deraadt #endif
835 1.1 deraadt zsabort();
836 1.1 deraadt return (0);
837 1.1 deraadt }
838 1.1 deraadt return (ZRING_MAKE(ZRING_SINT, rr0));
839 1.1 deraadt }
840 1.1 deraadt
841 1.1 deraadt zsabort()
842 1.1 deraadt {
843 1.1 deraadt
844 1.5 pk #ifdef DDB
845 1.5 pk Debugger();
846 1.5 pk #else
847 1.1 deraadt printf("stopping on keyboard abort\n");
848 1.1 deraadt callrom();
849 1.5 pk #endif
850 1.1 deraadt }
851 1.1 deraadt
852 1.1 deraadt #ifdef KGDB
853 1.1 deraadt /*
854 1.1 deraadt * KGDB framing character received: enter kernel debugger. This probably
855 1.1 deraadt * should time out after a few seconds to avoid hanging on spurious input.
856 1.1 deraadt */
857 1.1 deraadt zskgdb(int unit)
858 1.1 deraadt {
859 1.1 deraadt
860 1.1 deraadt printf("zs%d%c: kgdb interrupt\n", unit >> 1, (unit & 1) + 'a');
861 1.1 deraadt kgdb_connect(1);
862 1.1 deraadt }
863 1.1 deraadt #endif
864 1.1 deraadt
865 1.1 deraadt /*
866 1.1 deraadt * Print out a ring or fifo overrun error message.
867 1.1 deraadt */
868 1.1 deraadt static void
869 1.1 deraadt zsoverrun(int unit, long *ptime, char *what)
870 1.1 deraadt {
871 1.1 deraadt
872 1.1 deraadt if (*ptime != time.tv_sec) {
873 1.1 deraadt *ptime = time.tv_sec;
874 1.1 deraadt log(LOG_WARNING, "zs%d%c: %s overrun\n", unit >> 1,
875 1.1 deraadt (unit & 1) + 'a', what);
876 1.1 deraadt }
877 1.1 deraadt }
878 1.1 deraadt
879 1.1 deraadt /*
880 1.1 deraadt * ZS software interrupt. Scan all channels for deferred interrupts.
881 1.1 deraadt */
882 1.1 deraadt int
883 1.1 deraadt zssoft(void *arg)
884 1.1 deraadt {
885 1.1 deraadt register struct zs_chanstate *cs;
886 1.1 deraadt register volatile struct zschan *zc;
887 1.1 deraadt register struct linesw *line;
888 1.1 deraadt register struct tty *tp;
889 1.1 deraadt register int get, n, c, cc, unit, s;
890 1.1 deraadt
891 1.1 deraadt for (cs = zslist; cs != NULL; cs = cs->cs_next) {
892 1.1 deraadt get = cs->cs_rbget;
893 1.1 deraadt again:
894 1.1 deraadt n = cs->cs_rbput; /* atomic */
895 1.1 deraadt if (get == n) /* nothing more on this line */
896 1.1 deraadt continue;
897 1.1 deraadt unit = cs->cs_unit; /* set up to handle interrupts */
898 1.1 deraadt zc = cs->cs_zc;
899 1.1 deraadt tp = cs->cs_ttyp;
900 1.1 deraadt line = &linesw[tp->t_line];
901 1.1 deraadt /*
902 1.1 deraadt * Compute the number of interrupts in the receive ring.
903 1.1 deraadt * If the count is overlarge, we lost some events, and
904 1.1 deraadt * must advance to the first valid one. It may get
905 1.1 deraadt * overwritten if more data are arriving, but this is
906 1.1 deraadt * too expensive to check and gains nothing (we already
907 1.1 deraadt * lost out; all we can do at this point is trade one
908 1.1 deraadt * kind of loss for another).
909 1.1 deraadt */
910 1.1 deraadt n -= get;
911 1.1 deraadt if (n > ZLRB_RING_SIZE) {
912 1.1 deraadt zsoverrun(unit, &cs->cs_rotime, "ring");
913 1.1 deraadt get += n - ZLRB_RING_SIZE;
914 1.1 deraadt n = ZLRB_RING_SIZE;
915 1.1 deraadt }
916 1.1 deraadt while (--n >= 0) {
917 1.1 deraadt /* race to keep ahead of incoming interrupts */
918 1.1 deraadt c = cs->cs_rbuf[get++ & ZLRB_RING_MASK];
919 1.1 deraadt switch (ZRING_TYPE(c)) {
920 1.1 deraadt
921 1.1 deraadt case ZRING_RINT:
922 1.1 deraadt c = ZRING_VALUE(c);
923 1.1 deraadt if (c & ZSRR1_DO)
924 1.1 deraadt zsoverrun(unit, &cs->cs_fotime, "fifo");
925 1.1 deraadt cc = c >> 8;
926 1.1 deraadt if (c & ZSRR1_FE)
927 1.1 deraadt cc |= TTY_FE;
928 1.1 deraadt if (c & ZSRR1_PE)
929 1.1 deraadt cc |= TTY_PE;
930 1.1 deraadt /*
931 1.1 deraadt * this should be done through
932 1.1 deraadt * bstreams XXX gag choke
933 1.1 deraadt */
934 1.1 deraadt if (unit == ZS_KBD)
935 1.1 deraadt kbd_rint(cc);
936 1.1 deraadt else if (unit == ZS_MOUSE)
937 1.1 deraadt ms_rint(cc);
938 1.1 deraadt else
939 1.1 deraadt line->l_rint(cc, tp);
940 1.1 deraadt break;
941 1.1 deraadt
942 1.1 deraadt case ZRING_XINT:
943 1.1 deraadt /*
944 1.1 deraadt * Transmit done: change registers and resume,
945 1.1 deraadt * or clear BUSY.
946 1.1 deraadt */
947 1.1 deraadt if (cs->cs_heldchange) {
948 1.1 deraadt s = splzs();
949 1.1 deraadt c = zc->zc_csr;
950 1.1 deraadt if ((c & ZSRR0_DCD) == 0)
951 1.1 deraadt cs->cs_preg[3] &= ~ZSWR3_HFC;
952 1.1 deraadt bcopy((caddr_t)cs->cs_preg,
953 1.1 deraadt (caddr_t)cs->cs_creg, 16);
954 1.1 deraadt zs_loadchannelregs(zc, cs->cs_creg);
955 1.1 deraadt splx(s);
956 1.1 deraadt cs->cs_heldchange = 0;
957 1.1 deraadt if (cs->cs_heldtbc &&
958 1.1 deraadt (tp->t_state & TS_TTSTOP) == 0) {
959 1.1 deraadt cs->cs_tbc = cs->cs_heldtbc - 1;
960 1.1 deraadt zc->zc_data = *cs->cs_tba++;
961 1.1 deraadt goto again;
962 1.1 deraadt }
963 1.1 deraadt }
964 1.1 deraadt tp->t_state &= ~TS_BUSY;
965 1.1 deraadt if (tp->t_state & TS_FLUSH)
966 1.1 deraadt tp->t_state &= ~TS_FLUSH;
967 1.1 deraadt else
968 1.1 deraadt ndflush(&tp->t_outq,
969 1.9 pk cs->cs_tba - (caddr_t)tp->t_outq.c_cf);
970 1.1 deraadt line->l_start(tp);
971 1.1 deraadt break;
972 1.1 deraadt
973 1.1 deraadt case ZRING_SINT:
974 1.1 deraadt /*
975 1.1 deraadt * Status line change. HFC bit is run in
976 1.1 deraadt * hardware interrupt, to avoid locking
977 1.1 deraadt * at splzs here.
978 1.1 deraadt */
979 1.1 deraadt c = ZRING_VALUE(c);
980 1.1 deraadt if ((c ^ cs->cs_rr0) & ZSRR0_DCD) {
981 1.1 deraadt cc = (c & ZSRR0_DCD) != 0;
982 1.1 deraadt if (line->l_modem(tp, cc) == 0)
983 1.1 deraadt zs_modem(cs, cc);
984 1.1 deraadt }
985 1.1 deraadt cs->cs_rr0 = c;
986 1.1 deraadt break;
987 1.1 deraadt
988 1.1 deraadt default:
989 1.1 deraadt log(LOG_ERR, "zs%d%c: bad ZRING_TYPE (%x)\n",
990 1.1 deraadt unit >> 1, (unit & 1) + 'a', c);
991 1.1 deraadt break;
992 1.1 deraadt }
993 1.1 deraadt }
994 1.1 deraadt cs->cs_rbget = get;
995 1.1 deraadt goto again;
996 1.1 deraadt }
997 1.1 deraadt return (1);
998 1.1 deraadt }
999 1.1 deraadt
1000 1.1 deraadt int
1001 1.1 deraadt zsioctl(dev_t dev, int cmd, caddr_t data, int flag, struct proc *p)
1002 1.1 deraadt {
1003 1.1 deraadt int unit = minor(dev);
1004 1.1 deraadt struct zsinfo *zi = zscd.cd_devs[unit >> 1];
1005 1.1 deraadt register struct tty *tp = zi->zi_cs[unit & 1].cs_ttyp;
1006 1.7 deraadt register int error, s;
1007 1.7 deraadt register struct zs_chanstate *cs = &zi->zi_cs[unit & 1];
1008 1.1 deraadt
1009 1.4 deraadt error = linesw[tp->t_line].l_ioctl(tp, cmd, data, flag, p);
1010 1.1 deraadt if (error >= 0)
1011 1.1 deraadt return (error);
1012 1.4 deraadt error = ttioctl(tp, cmd, data, flag, p);
1013 1.1 deraadt if (error >= 0)
1014 1.1 deraadt return (error);
1015 1.1 deraadt
1016 1.1 deraadt switch (cmd) {
1017 1.1 deraadt
1018 1.1 deraadt case TIOCSBRK:
1019 1.7 deraadt {
1020 1.7 deraadt s = splzs();
1021 1.7 deraadt cs->cs_preg[5] |= ZSWR5_BREAK;
1022 1.7 deraadt cs->cs_creg[5] |= ZSWR5_BREAK;
1023 1.7 deraadt ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
1024 1.7 deraadt splx(s);
1025 1.7 deraadt break;
1026 1.7 deraadt }
1027 1.1 deraadt
1028 1.1 deraadt case TIOCCBRK:
1029 1.7 deraadt {
1030 1.7 deraadt s = splzs();
1031 1.7 deraadt cs->cs_preg[5] &= ~ZSWR5_BREAK;
1032 1.7 deraadt cs->cs_creg[5] &= ~ZSWR5_BREAK;
1033 1.7 deraadt ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
1034 1.7 deraadt splx(s);
1035 1.7 deraadt break;
1036 1.7 deraadt }
1037 1.1 deraadt
1038 1.1 deraadt case TIOCSDTR:
1039 1.1 deraadt
1040 1.1 deraadt case TIOCCDTR:
1041 1.1 deraadt
1042 1.1 deraadt case TIOCMSET:
1043 1.1 deraadt
1044 1.1 deraadt case TIOCMBIS:
1045 1.1 deraadt
1046 1.1 deraadt case TIOCMBIC:
1047 1.1 deraadt
1048 1.1 deraadt case TIOCMGET:
1049 1.1 deraadt
1050 1.1 deraadt default:
1051 1.1 deraadt return (ENOTTY);
1052 1.1 deraadt }
1053 1.1 deraadt return (0);
1054 1.1 deraadt }
1055 1.1 deraadt
1056 1.1 deraadt /*
1057 1.1 deraadt * Start or restart transmission.
1058 1.1 deraadt */
1059 1.1 deraadt static void
1060 1.1 deraadt zsstart(register struct tty *tp)
1061 1.1 deraadt {
1062 1.1 deraadt register struct zs_chanstate *cs;
1063 1.1 deraadt register int s, nch;
1064 1.1 deraadt int unit = minor(tp->t_dev);
1065 1.1 deraadt struct zsinfo *zi = zscd.cd_devs[unit >> 1];
1066 1.1 deraadt
1067 1.1 deraadt cs = &zi->zi_cs[unit & 1];
1068 1.1 deraadt s = spltty();
1069 1.1 deraadt
1070 1.1 deraadt /*
1071 1.1 deraadt * If currently active or delaying, no need to do anything.
1072 1.1 deraadt */
1073 1.1 deraadt if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))
1074 1.1 deraadt goto out;
1075 1.1 deraadt
1076 1.1 deraadt /*
1077 1.1 deraadt * If there are sleepers, and output has drained below low
1078 1.1 deraadt * water mark, awaken.
1079 1.1 deraadt */
1080 1.1 deraadt if (tp->t_outq.c_cc <= tp->t_lowat) {
1081 1.1 deraadt if (tp->t_state & TS_ASLEEP) {
1082 1.1 deraadt tp->t_state &= ~TS_ASLEEP;
1083 1.1 deraadt wakeup((caddr_t)&tp->t_outq);
1084 1.1 deraadt }
1085 1.1 deraadt selwakeup(&tp->t_wsel);
1086 1.1 deraadt }
1087 1.1 deraadt
1088 1.1 deraadt nch = ndqb(&tp->t_outq, 0); /* XXX */
1089 1.1 deraadt if (nch) {
1090 1.1 deraadt register char *p = tp->t_outq.c_cf;
1091 1.1 deraadt
1092 1.1 deraadt /* mark busy, enable tx done interrupts, & send first byte */
1093 1.1 deraadt tp->t_state |= TS_BUSY;
1094 1.1 deraadt (void) splzs();
1095 1.1 deraadt cs->cs_preg[1] |= ZSWR1_TIE;
1096 1.1 deraadt cs->cs_creg[1] |= ZSWR1_TIE;
1097 1.1 deraadt ZS_WRITE(cs->cs_zc, 1, cs->cs_creg[1]);
1098 1.1 deraadt cs->cs_zc->zc_data = *p;
1099 1.1 deraadt cs->cs_tba = p + 1;
1100 1.1 deraadt cs->cs_tbc = nch - 1;
1101 1.1 deraadt } else {
1102 1.1 deraadt /*
1103 1.1 deraadt * Nothing to send, turn off transmit done interrupts.
1104 1.1 deraadt * This is useful if something is doing polled output.
1105 1.1 deraadt */
1106 1.1 deraadt (void) splzs();
1107 1.1 deraadt cs->cs_preg[1] &= ~ZSWR1_TIE;
1108 1.1 deraadt cs->cs_creg[1] &= ~ZSWR1_TIE;
1109 1.1 deraadt ZS_WRITE(cs->cs_zc, 1, cs->cs_creg[1]);
1110 1.1 deraadt }
1111 1.1 deraadt out:
1112 1.1 deraadt splx(s);
1113 1.1 deraadt }
1114 1.1 deraadt
1115 1.1 deraadt /*
1116 1.1 deraadt * Stop output, e.g., for ^S or output flush.
1117 1.1 deraadt */
1118 1.3 deraadt void
1119 1.1 deraadt zsstop(register struct tty *tp, int flag)
1120 1.1 deraadt {
1121 1.1 deraadt register struct zs_chanstate *cs;
1122 1.1 deraadt register int s, unit = minor(tp->t_dev);
1123 1.1 deraadt struct zsinfo *zi = zscd.cd_devs[unit >> 1];
1124 1.1 deraadt
1125 1.1 deraadt cs = &zi->zi_cs[unit & 1];
1126 1.1 deraadt s = splzs();
1127 1.1 deraadt if (tp->t_state & TS_BUSY) {
1128 1.1 deraadt /*
1129 1.1 deraadt * Device is transmitting; must stop it.
1130 1.1 deraadt */
1131 1.1 deraadt cs->cs_tbc = 0;
1132 1.1 deraadt if ((tp->t_state & TS_TTSTOP) == 0)
1133 1.1 deraadt tp->t_state |= TS_FLUSH;
1134 1.1 deraadt }
1135 1.1 deraadt splx(s);
1136 1.1 deraadt }
1137 1.1 deraadt
1138 1.1 deraadt /*
1139 1.1 deraadt * Set ZS tty parameters from termios.
1140 1.1 deraadt *
1141 1.1 deraadt * This routine makes use of the fact that only registers
1142 1.1 deraadt * 1, 3, 4, 5, 9, 10, 11, 12, 13, 14, and 15 are written.
1143 1.1 deraadt */
1144 1.1 deraadt static int
1145 1.1 deraadt zsparam(register struct tty *tp, register struct termios *t)
1146 1.1 deraadt {
1147 1.1 deraadt int unit = minor(tp->t_dev);
1148 1.1 deraadt struct zsinfo *zi = zscd.cd_devs[unit >> 1];
1149 1.1 deraadt register struct zs_chanstate *cs = &zi->zi_cs[unit & 1];
1150 1.1 deraadt register int tmp, tmp5, cflag, s;
1151 1.1 deraadt
1152 1.1 deraadt /*
1153 1.1 deraadt * Because PCLK is only run at 4.9 MHz, the fastest we
1154 1.1 deraadt * can go is 51200 baud (this corresponds to TC=1).
1155 1.1 deraadt * This is somewhat unfortunate as there is no real
1156 1.1 deraadt * reason we should not be able to handle higher rates.
1157 1.1 deraadt */
1158 1.1 deraadt tmp = t->c_ospeed;
1159 1.1 deraadt if (tmp < 0 || (t->c_ispeed && t->c_ispeed != tmp))
1160 1.1 deraadt return (EINVAL);
1161 1.1 deraadt if (tmp == 0) {
1162 1.1 deraadt /* stty 0 => drop DTR and RTS */
1163 1.1 deraadt zs_modem(cs, 0);
1164 1.1 deraadt return (0);
1165 1.1 deraadt }
1166 1.1 deraadt tmp = BPS_TO_TCONST(PCLK / 16, tmp);
1167 1.1 deraadt if (tmp < 2)
1168 1.1 deraadt return (EINVAL);
1169 1.1 deraadt
1170 1.1 deraadt cflag = t->c_cflag;
1171 1.1 deraadt tp->t_ispeed = tp->t_ospeed = TCONST_TO_BPS(PCLK / 16, tmp);
1172 1.1 deraadt tp->t_cflag = cflag;
1173 1.1 deraadt
1174 1.1 deraadt /*
1175 1.1 deraadt * Block interrupts so that state will not
1176 1.1 deraadt * be altered until we are done setting it up.
1177 1.1 deraadt */
1178 1.1 deraadt s = splzs();
1179 1.1 deraadt cs->cs_preg[12] = tmp;
1180 1.1 deraadt cs->cs_preg[13] = tmp >> 8;
1181 1.1 deraadt cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_TIE | ZSWR1_SIE;
1182 1.1 deraadt switch (cflag & CSIZE) {
1183 1.1 deraadt case CS5:
1184 1.1 deraadt tmp = ZSWR3_RX_5;
1185 1.1 deraadt tmp5 = ZSWR5_TX_5;
1186 1.1 deraadt break;
1187 1.1 deraadt case CS6:
1188 1.1 deraadt tmp = ZSWR3_RX_6;
1189 1.1 deraadt tmp5 = ZSWR5_TX_6;
1190 1.1 deraadt break;
1191 1.1 deraadt case CS7:
1192 1.1 deraadt tmp = ZSWR3_RX_7;
1193 1.1 deraadt tmp5 = ZSWR5_TX_7;
1194 1.1 deraadt break;
1195 1.1 deraadt case CS8:
1196 1.1 deraadt default:
1197 1.1 deraadt tmp = ZSWR3_RX_8;
1198 1.1 deraadt tmp5 = ZSWR5_TX_8;
1199 1.1 deraadt break;
1200 1.1 deraadt }
1201 1.1 deraadt
1202 1.1 deraadt /*
1203 1.1 deraadt * Output hardware flow control on the chip is horrendous: if
1204 1.1 deraadt * carrier detect drops, the receiver is disabled. Hence we
1205 1.1 deraadt * can only do this when the carrier is on.
1206 1.1 deraadt */
1207 1.1 deraadt if (cflag & CCTS_OFLOW && cs->cs_zc->zc_csr & ZSRR0_DCD)
1208 1.1 deraadt tmp |= ZSWR3_HFC | ZSWR3_RX_ENABLE;
1209 1.1 deraadt else
1210 1.1 deraadt tmp |= ZSWR3_RX_ENABLE;
1211 1.1 deraadt cs->cs_preg[3] = tmp;
1212 1.1 deraadt cs->cs_preg[5] = tmp5 | ZSWR5_TX_ENABLE | ZSWR5_DTR | ZSWR5_RTS;
1213 1.1 deraadt
1214 1.1 deraadt tmp = ZSWR4_CLK_X16 | (cflag & CSTOPB ? ZSWR4_TWOSB : ZSWR4_ONESB);
1215 1.1 deraadt if ((cflag & PARODD) == 0)
1216 1.1 deraadt tmp |= ZSWR4_EVENP;
1217 1.1 deraadt if (cflag & PARENB)
1218 1.1 deraadt tmp |= ZSWR4_PARENB;
1219 1.1 deraadt cs->cs_preg[4] = tmp;
1220 1.1 deraadt cs->cs_preg[9] = ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR;
1221 1.1 deraadt cs->cs_preg[10] = ZSWR10_NRZ;
1222 1.1 deraadt cs->cs_preg[11] = ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD;
1223 1.1 deraadt cs->cs_preg[14] = ZSWR14_BAUD_FROM_PCLK | ZSWR14_BAUD_ENA;
1224 1.1 deraadt cs->cs_preg[15] = ZSWR15_BREAK_IE | ZSWR15_DCD_IE;
1225 1.1 deraadt
1226 1.1 deraadt /*
1227 1.1 deraadt * If nothing is being transmitted, set up new current values,
1228 1.1 deraadt * else mark them as pending.
1229 1.1 deraadt */
1230 1.1 deraadt if (cs->cs_heldchange == 0) {
1231 1.1 deraadt if (cs->cs_ttyp->t_state & TS_BUSY) {
1232 1.1 deraadt cs->cs_heldtbc = cs->cs_tbc;
1233 1.1 deraadt cs->cs_tbc = 0;
1234 1.1 deraadt cs->cs_heldchange = 1;
1235 1.1 deraadt } else {
1236 1.1 deraadt bcopy((caddr_t)cs->cs_preg, (caddr_t)cs->cs_creg, 16);
1237 1.1 deraadt zs_loadchannelregs(cs->cs_zc, cs->cs_creg);
1238 1.1 deraadt }
1239 1.1 deraadt }
1240 1.1 deraadt splx(s);
1241 1.1 deraadt return (0);
1242 1.1 deraadt }
1243 1.1 deraadt
1244 1.1 deraadt /*
1245 1.1 deraadt * Raise or lower modem control (DTR/RTS) signals. If a character is
1246 1.1 deraadt * in transmission, the change is deferred.
1247 1.1 deraadt */
1248 1.1 deraadt static void
1249 1.1 deraadt zs_modem(struct zs_chanstate *cs, int onoff)
1250 1.1 deraadt {
1251 1.1 deraadt int s, bis, and;
1252 1.1 deraadt
1253 1.1 deraadt if (onoff) {
1254 1.1 deraadt bis = ZSWR5_DTR | ZSWR5_RTS;
1255 1.1 deraadt and = ~0;
1256 1.1 deraadt } else {
1257 1.1 deraadt bis = 0;
1258 1.1 deraadt and = ~(ZSWR5_DTR | ZSWR5_RTS);
1259 1.1 deraadt }
1260 1.1 deraadt s = splzs();
1261 1.1 deraadt cs->cs_preg[5] = (cs->cs_preg[5] | bis) & and;
1262 1.1 deraadt if (cs->cs_heldchange == 0) {
1263 1.1 deraadt if (cs->cs_ttyp->t_state & TS_BUSY) {
1264 1.1 deraadt cs->cs_heldtbc = cs->cs_tbc;
1265 1.1 deraadt cs->cs_tbc = 0;
1266 1.1 deraadt cs->cs_heldchange = 1;
1267 1.1 deraadt } else {
1268 1.1 deraadt cs->cs_creg[5] = (cs->cs_creg[5] | bis) & and;
1269 1.1 deraadt ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
1270 1.1 deraadt }
1271 1.1 deraadt }
1272 1.1 deraadt splx(s);
1273 1.1 deraadt }
1274 1.1 deraadt
1275 1.1 deraadt /*
1276 1.1 deraadt * Write the given register set to the given zs channel in the proper order.
1277 1.1 deraadt * The channel must not be transmitting at the time. The receiver will
1278 1.1 deraadt * be disabled for the time it takes to write all the registers.
1279 1.1 deraadt */
1280 1.1 deraadt static void
1281 1.1 deraadt zs_loadchannelregs(volatile struct zschan *zc, u_char *reg)
1282 1.1 deraadt {
1283 1.1 deraadt int i;
1284 1.1 deraadt
1285 1.1 deraadt zc->zc_csr = ZSM_RESET_ERR; /* reset error condition */
1286 1.1 deraadt i = zc->zc_data; /* drain fifo */
1287 1.1 deraadt i = zc->zc_data;
1288 1.1 deraadt i = zc->zc_data;
1289 1.1 deraadt ZS_WRITE(zc, 4, reg[4]);
1290 1.1 deraadt ZS_WRITE(zc, 10, reg[10]);
1291 1.1 deraadt ZS_WRITE(zc, 3, reg[3] & ~ZSWR3_RX_ENABLE);
1292 1.1 deraadt ZS_WRITE(zc, 5, reg[5] & ~ZSWR5_TX_ENABLE);
1293 1.1 deraadt ZS_WRITE(zc, 1, reg[1]);
1294 1.1 deraadt ZS_WRITE(zc, 9, reg[9]);
1295 1.1 deraadt ZS_WRITE(zc, 11, reg[11]);
1296 1.1 deraadt ZS_WRITE(zc, 12, reg[12]);
1297 1.1 deraadt ZS_WRITE(zc, 13, reg[13]);
1298 1.1 deraadt ZS_WRITE(zc, 14, reg[14]);
1299 1.1 deraadt ZS_WRITE(zc, 15, reg[15]);
1300 1.1 deraadt ZS_WRITE(zc, 3, reg[3]);
1301 1.1 deraadt ZS_WRITE(zc, 5, reg[5]);
1302 1.1 deraadt }
1303 1.1 deraadt
1304 1.1 deraadt #ifdef KGDB
1305 1.1 deraadt /*
1306 1.1 deraadt * Get a character from the given kgdb channel. Called at splhigh().
1307 1.1 deraadt */
1308 1.1 deraadt static int
1309 1.1 deraadt zs_kgdb_getc(void *arg)
1310 1.1 deraadt {
1311 1.1 deraadt register volatile struct zschan *zc = (volatile struct zschan *)arg;
1312 1.1 deraadt
1313 1.1 deraadt while ((zc->zc_csr & ZSRR0_RX_READY) == 0)
1314 1.1 deraadt continue;
1315 1.1 deraadt return (zc->zc_data);
1316 1.1 deraadt }
1317 1.1 deraadt
1318 1.1 deraadt /*
1319 1.1 deraadt * Put a character to the given kgdb channel. Called at splhigh().
1320 1.1 deraadt */
1321 1.1 deraadt static void
1322 1.1 deraadt zs_kgdb_putc(void *arg, int c)
1323 1.1 deraadt {
1324 1.1 deraadt register volatile struct zschan *zc = (volatile struct zschan *)arg;
1325 1.1 deraadt
1326 1.1 deraadt while ((zc->zc_csr & ZSRR0_TX_READY) == 0)
1327 1.1 deraadt continue;
1328 1.1 deraadt zc->zc_data = c;
1329 1.1 deraadt }
1330 1.1 deraadt
1331 1.1 deraadt /*
1332 1.1 deraadt * Set up for kgdb; called at boot time before configuration.
1333 1.1 deraadt * KGDB interrupts will be enabled later when zs0 is configured.
1334 1.1 deraadt */
1335 1.1 deraadt void
1336 1.1 deraadt zs_kgdb_init()
1337 1.1 deraadt {
1338 1.1 deraadt volatile struct zsdevice *addr;
1339 1.1 deraadt volatile struct zschan *zc;
1340 1.1 deraadt int unit, zs;
1341 1.1 deraadt
1342 1.1 deraadt if (major(kgdb_dev) != ZSMAJOR)
1343 1.1 deraadt return;
1344 1.1 deraadt unit = minor(kgdb_dev);
1345 1.1 deraadt /*
1346 1.1 deraadt * Unit must be 0 or 1 (zs0).
1347 1.1 deraadt */
1348 1.1 deraadt if ((unsigned)unit >= ZS_KBD) {
1349 1.1 deraadt printf("zs_kgdb_init: bad minor dev %d\n", unit);
1350 1.1 deraadt return;
1351 1.1 deraadt }
1352 1.1 deraadt zs = unit >> 1;
1353 1.1 deraadt if ((addr = zsaddr[zs]) == NULL)
1354 1.1 deraadt addr = zsaddr[zs] = findzs(zs);
1355 1.1 deraadt unit &= 1;
1356 1.1 deraadt zc = unit == 0 ? &addr->zs_chan[CHAN_A] : &addr->zs_chan[CHAN_B];
1357 1.1 deraadt zs_kgdb_savedspeed = zs_getspeed(zc);
1358 1.1 deraadt printf("zs_kgdb_init: attaching zs%d%c at %d baud\n",
1359 1.1 deraadt zs, unit + 'a', kgdb_rate);
1360 1.1 deraadt zs_reset(zc, 1, kgdb_rate);
1361 1.1 deraadt kgdb_attach(zs_kgdb_getc, zs_kgdb_putc, (void *)zc);
1362 1.1 deraadt }
1363 1.1 deraadt #endif /* KGDB */
1364