zs.c revision 1.47 1 1.47 gwr /* $NetBSD: zs.c,v 1.47 1997/02/10 23:29:52 gwr Exp $ */
2 1.10 cgd
3 1.42 gwr /*-
4 1.42 gwr * Copyright (c) 1996 The NetBSD Foundation, Inc.
5 1.31 gwr * All rights reserved.
6 1.1 glass *
7 1.42 gwr * This code is derived from software contributed to The NetBSD Foundation
8 1.42 gwr * by Gordon W. Ross.
9 1.42 gwr *
10 1.1 glass * Redistribution and use in source and binary forms, with or without
11 1.1 glass * modification, are permitted provided that the following conditions
12 1.1 glass * are met:
13 1.1 glass * 1. Redistributions of source code must retain the above copyright
14 1.1 glass * notice, this list of conditions and the following disclaimer.
15 1.1 glass * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 glass * notice, this list of conditions and the following disclaimer in the
17 1.1 glass * documentation and/or other materials provided with the distribution.
18 1.42 gwr * 3. All advertising materials mentioning features or use of this software
19 1.1 glass * must display the following acknowledgement:
20 1.42 gwr * This product includes software developed by the NetBSD
21 1.42 gwr * Foundation, Inc. and its contributors.
22 1.42 gwr * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.42 gwr * contributors may be used to endorse or promote products derived
24 1.42 gwr * from this software without specific prior written permission.
25 1.1 glass *
26 1.42 gwr * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.42 gwr * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.42 gwr * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.44 gwr * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.44 gwr * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.42 gwr * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.42 gwr * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.42 gwr * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.42 gwr * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.42 gwr * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.42 gwr * POSSIBILITY OF SUCH DAMAGE.
37 1.1 glass */
38 1.1 glass
39 1.1 glass /*
40 1.31 gwr * Zilog Z8530 Dual UART driver (machine-dependent part)
41 1.1 glass *
42 1.31 gwr * Runs two serial lines per chip using slave drivers.
43 1.31 gwr * Plain tty/async lines use the zs_async slave.
44 1.31 gwr * Sun keyboard/mouse uses the zs_kbd/zs_ms slaves.
45 1.1 glass */
46 1.1 glass
47 1.5 gwr #include <sys/param.h>
48 1.1 glass #include <sys/systm.h>
49 1.43 gwr #include <sys/conf.h>
50 1.1 glass #include <sys/device.h>
51 1.1 glass #include <sys/file.h>
52 1.1 glass #include <sys/ioctl.h>
53 1.43 gwr #include <sys/kernel.h>
54 1.43 gwr #include <sys/proc.h>
55 1.1 glass #include <sys/tty.h>
56 1.1 glass #include <sys/time.h>
57 1.1 glass #include <sys/syslog.h>
58 1.1 glass
59 1.31 gwr #include <dev/cons.h>
60 1.31 gwr #include <dev/ic/z8530reg.h>
61 1.31 gwr #include <machine/z8530var.h>
62 1.31 gwr
63 1.1 glass #include <machine/autoconf.h>
64 1.1 glass #include <machine/cpu.h>
65 1.1 glass #include <machine/obio.h>
66 1.47 gwr #include <machine/machdep.h>
67 1.3 gwr #include <machine/mon.h>
68 1.1 glass
69 1.44 gwr #include <sun3/dev/zs_cons.h>
70 1.47 gwr #include "kbd.h"
71 1.47 gwr
72 1.47 gwr extern void Debugger __P((void));
73 1.43 gwr
74 1.16 gwr /*
75 1.31 gwr * XXX: Hard code this to make console init easier...
76 1.16 gwr */
77 1.43 gwr #define NZSC 2 /* XXX */
78 1.1 glass
79 1.43 gwr /*
80 1.43 gwr * Some warts needed by z8530tty.c -
81 1.43 gwr * The default parity REALLY needs to be the same as the PROM uses,
82 1.43 gwr * or you can not see messages done with printf during boot-up...
83 1.43 gwr */
84 1.43 gwr int zs_def_cflag = (CREAD | CS8 | HUPCL);
85 1.43 gwr int zs_major = 12;
86 1.1 glass
87 1.43 gwr /*
88 1.43 gwr * The Sun3 provides a 4.9152 MHz clock to the ZS chips.
89 1.43 gwr */
90 1.2 glass #define PCLK (9600 * 512) /* PCLK pin input clock rate */
91 1.2 glass
92 1.2 glass /*
93 1.22 gwr * Define interrupt levels.
94 1.2 glass */
95 1.2 glass #define ZSHARD_PRI 6 /* Wired on the CPU board... */
96 1.22 gwr #define ZSSOFT_PRI 3 /* Want tty pri (4) but this is OK. */
97 1.1 glass
98 1.33 gwr #define ZS_DELAY() delay(2)
99 1.31 gwr
100 1.31 gwr /* The layout of this is hardware-dependent (padding, order). */
101 1.31 gwr struct zschan {
102 1.31 gwr volatile u_char zc_csr; /* ctrl,status, and indirect access */
103 1.31 gwr u_char zc_xxx0;
104 1.31 gwr volatile u_char zc_data; /* data */
105 1.31 gwr u_char zc_xxx1;
106 1.31 gwr };
107 1.31 gwr struct zsdevice {
108 1.31 gwr /* Yes, they are backwards. */
109 1.31 gwr struct zschan zs_chan_b;
110 1.31 gwr struct zschan zs_chan_a;
111 1.1 glass };
112 1.1 glass
113 1.1 glass
114 1.31 gwr /* Default OBIO addresses. */
115 1.44 gwr static int zs_physaddr[NZSC] = {
116 1.44 gwr OBIO_ZS_KBD_MS,
117 1.44 gwr OBIO_ZS_TTY_AB };
118 1.43 gwr
119 1.31 gwr /* Saved PROM mappings */
120 1.43 gwr static struct zsdevice *zsaddr[NZSC]; /* See zs_init() */
121 1.43 gwr
122 1.31 gwr /* Flags from cninit() */
123 1.43 gwr static int zs_hwflags[NZSC][2];
124 1.43 gwr
125 1.31 gwr /* Default speed for each channel */
126 1.43 gwr static int zs_defspeed[NZSC][2] = {
127 1.31 gwr { 1200, /* keyboard */
128 1.31 gwr 1200 }, /* mouse */
129 1.31 gwr { 9600, /* ttya */
130 1.31 gwr 9600 }, /* ttyb */
131 1.31 gwr };
132 1.13 gwr
133 1.43 gwr static u_char zs_init_reg[16] = {
134 1.43 gwr 0, /* 0: CMD (reset, etc.) */
135 1.43 gwr 0, /* 1: No interrupts yet. */
136 1.43 gwr 0x18 + ZSHARD_PRI, /* IVECT */
137 1.43 gwr ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
138 1.43 gwr ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
139 1.43 gwr ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
140 1.43 gwr 0, /* 6: TXSYNC/SYNCLO */
141 1.43 gwr 0, /* 7: RXSYNC/SYNCHI */
142 1.43 gwr 0, /* 8: alias for data port */
143 1.43 gwr ZSWR9_MASTER_IE,
144 1.43 gwr 0, /*10: Misc. TX/RX control bits */
145 1.43 gwr ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
146 1.43 gwr 14, /*12: BAUDLO (default=9600) */
147 1.43 gwr 0, /*13: BAUDHI (default=9600) */
148 1.43 gwr ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
149 1.43 gwr ZSWR15_BREAK_IE | ZSWR15_DCD_IE,
150 1.43 gwr };
151 1.43 gwr
152 1.1 glass
153 1.31 gwr /* Find PROM mappings (for console support). */
154 1.43 gwr void
155 1.43 gwr zs_init()
156 1.31 gwr {
157 1.31 gwr int i;
158 1.1 glass
159 1.43 gwr for (i = 0; i < NZSC; i++) {
160 1.31 gwr zsaddr[i] = (struct zsdevice *)
161 1.44 gwr obio_find_mapping(zs_physaddr[i], sizeof(struct zschan));
162 1.31 gwr }
163 1.43 gwr }
164 1.13 gwr
165 1.47 gwr struct zschan *
166 1.31 gwr zs_get_chan_addr(zsc_unit, channel)
167 1.31 gwr int zsc_unit, channel;
168 1.31 gwr {
169 1.31 gwr struct zsdevice *addr;
170 1.31 gwr struct zschan *zc;
171 1.31 gwr
172 1.43 gwr if (zsc_unit >= NZSC)
173 1.31 gwr return NULL;
174 1.31 gwr addr = zsaddr[zsc_unit];
175 1.31 gwr if (addr == NULL)
176 1.31 gwr return NULL;
177 1.31 gwr if (channel == 0) {
178 1.31 gwr zc = &addr->zs_chan_a;
179 1.31 gwr } else {
180 1.31 gwr zc = &addr->zs_chan_b;
181 1.31 gwr }
182 1.31 gwr return (zc);
183 1.31 gwr }
184 1.13 gwr
185 1.18 gwr
186 1.31 gwr /****************************************************************
187 1.31 gwr * Autoconfig
188 1.31 gwr ****************************************************************/
189 1.31 gwr
190 1.31 gwr /* Definition of the driver for autoconfig. */
191 1.43 gwr static int zsc_match __P((struct device *, struct cfdata *, void *));
192 1.35 gwr static void zsc_attach __P((struct device *, struct device *, void *));
193 1.39 cgd static int zsc_print __P((void *, const char *name));
194 1.31 gwr
195 1.34 thorpej struct cfattach zsc_ca = {
196 1.34 thorpej sizeof(struct zsc_softc), zsc_match, zsc_attach
197 1.34 thorpej };
198 1.34 thorpej
199 1.34 thorpej struct cfdriver zsc_cd = {
200 1.34 thorpej NULL, "zsc", DV_DULL
201 1.31 gwr };
202 1.31 gwr
203 1.43 gwr static int zshard __P((void *));
204 1.43 gwr static int zssoft __P((void *));
205 1.43 gwr static int zs_get_speed __P((struct zs_chanstate *));
206 1.31 gwr
207 1.9 gwr
208 1.1 glass /*
209 1.31 gwr * Is the zs chip present?
210 1.1 glass */
211 1.1 glass static int
212 1.43 gwr zsc_match(parent, cf, aux)
213 1.31 gwr struct device *parent;
214 1.43 gwr struct cfdata *cf;
215 1.43 gwr void *aux;
216 1.1 glass {
217 1.31 gwr struct confargs *ca = aux;
218 1.43 gwr int unit;
219 1.35 gwr void *va;
220 1.13 gwr
221 1.43 gwr /* We have arrays sized with NZSC so validate. */
222 1.13 gwr unit = cf->cf_unit;
223 1.43 gwr if (unit < 0 || unit >= NZSC)
224 1.13 gwr return (0);
225 1.1 glass
226 1.35 gwr /*
227 1.43 gwr * This driver only supports its wired-in mappings,
228 1.43 gwr * because the console support depends on those.
229 1.35 gwr */
230 1.43 gwr if (ca->ca_paddr != zs_physaddr[unit])
231 1.35 gwr return (0);
232 1.35 gwr
233 1.31 gwr /* Make sure zs_init() found mappings. */
234 1.35 gwr va = zsaddr[unit];
235 1.35 gwr if (va == NULL)
236 1.21 gwr return (0);
237 1.21 gwr
238 1.21 gwr /* This returns -1 on a fault (bus error). */
239 1.43 gwr if (peek_byte(va) == -1)
240 1.43 gwr return (0);
241 1.43 gwr
242 1.43 gwr /* Default interrupt priority (always splbio==2) */
243 1.43 gwr if (ca->ca_intpri == -1)
244 1.43 gwr ca->ca_intpri = ZSHARD_PRI;
245 1.43 gwr
246 1.43 gwr return (1);
247 1.1 glass }
248 1.1 glass
249 1.1 glass /*
250 1.1 glass * Attach a found zs.
251 1.1 glass *
252 1.31 gwr * Match slave number to zs unit number, so that misconfiguration will
253 1.31 gwr * not set up the keyboard as ttya, etc.
254 1.1 glass */
255 1.1 glass static void
256 1.31 gwr zsc_attach(parent, self, aux)
257 1.31 gwr struct device *parent;
258 1.31 gwr struct device *self;
259 1.31 gwr void *aux;
260 1.31 gwr {
261 1.31 gwr struct zsc_softc *zsc = (void *) self;
262 1.31 gwr struct confargs *ca = aux;
263 1.31 gwr struct zsc_attach_args zsc_args;
264 1.31 gwr volatile struct zschan *zc;
265 1.31 gwr struct zs_chanstate *cs;
266 1.43 gwr int s, zsc_unit, channel;
267 1.2 glass
268 1.31 gwr zsc_unit = zsc->zsc_dev.dv_unit;
269 1.13 gwr
270 1.43 gwr printf(": (softpri %d)\n", ZSSOFT_PRI);
271 1.1 glass
272 1.31 gwr /* Use the mapping setup by the Sun PROM. */
273 1.31 gwr if (zsaddr[zsc_unit] == NULL)
274 1.31 gwr panic("zs_attach: zs%d not mapped\n", zsc_unit);
275 1.31 gwr
276 1.31 gwr /*
277 1.31 gwr * Initialize software state for each channel.
278 1.31 gwr */
279 1.31 gwr for (channel = 0; channel < 2; channel++) {
280 1.43 gwr zsc_args.channel = channel;
281 1.43 gwr zsc_args.hwflags = zs_hwflags[zsc_unit][channel];
282 1.43 gwr cs = &zsc->zsc_cs_store[channel];
283 1.43 gwr zsc->zsc_cs[channel] = cs;
284 1.1 glass
285 1.31 gwr cs->cs_channel = channel;
286 1.31 gwr cs->cs_private = NULL;
287 1.31 gwr cs->cs_ops = &zsops_null;
288 1.37 gwr cs->cs_brg_clk = PCLK / 16;
289 1.31 gwr
290 1.43 gwr zc = zs_get_chan_addr(zsc_unit, channel);
291 1.43 gwr cs->cs_reg_csr = &zc->zc_csr;
292 1.43 gwr cs->cs_reg_data = &zc->zc_data;
293 1.2 glass
294 1.31 gwr bcopy(zs_init_reg, cs->cs_creg, 16);
295 1.31 gwr bcopy(zs_init_reg, cs->cs_preg, 16);
296 1.15 gwr
297 1.43 gwr /* XXX: Get these from the EEPROM instead? */
298 1.43 gwr /* XXX: See the mvme167 code. Better. */
299 1.43 gwr if (zsc_args.hwflags & ZS_HWFLAG_CONSOLE)
300 1.43 gwr cs->cs_defspeed = zs_get_speed(cs);
301 1.43 gwr else
302 1.43 gwr cs->cs_defspeed = zs_defspeed[zsc_unit][channel];
303 1.43 gwr cs->cs_defcflag = zs_def_cflag;
304 1.43 gwr
305 1.47 gwr /* Make these correspond to cs_defcflag (-crtscts) */
306 1.46 gwr cs->cs_rr0_dcd = ZSRR0_DCD;
307 1.47 gwr cs->cs_rr0_cts = 0;
308 1.47 gwr cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
309 1.47 gwr cs->cs_wr5_rts = 0;
310 1.46 gwr
311 1.1 glass /*
312 1.31 gwr * Clear the master interrupt enable.
313 1.31 gwr * The INTENA is common to both channels,
314 1.31 gwr * so just do it on the A channel.
315 1.1 glass */
316 1.31 gwr if (channel == 0) {
317 1.32 gwr zs_write_reg(cs, 9, 0);
318 1.31 gwr }
319 1.15 gwr
320 1.1 glass /*
321 1.31 gwr * Look for a child driver for this channel.
322 1.31 gwr * The child attach will setup the hardware.
323 1.1 glass */
324 1.43 gwr if (!config_found(self, (void *)&zsc_args, zsc_print)) {
325 1.31 gwr /* No sub-driver. Just reset it. */
326 1.43 gwr u_char reset = (channel == 0) ?
327 1.31 gwr ZSWR9_A_RESET : ZSWR9_B_RESET;
328 1.45 gwr s = splhigh();
329 1.32 gwr zs_write_reg(cs, 9, reset);
330 1.31 gwr splx(s);
331 1.31 gwr }
332 1.1 glass }
333 1.1 glass
334 1.43 gwr /*
335 1.43 gwr * Now safe to install interrupt handlers. Note the arguments
336 1.43 gwr * to the interrupt handlers aren't used. Note, we only do this
337 1.43 gwr * once since both SCCs interrupt at the same level and vector.
338 1.43 gwr */
339 1.45 gwr if (zsc_unit == 0) {
340 1.31 gwr isr_add_autovect(zssoft, NULL, ZSSOFT_PRI);
341 1.43 gwr isr_add_autovect(zshard, NULL, ca->ca_intpri);
342 1.31 gwr }
343 1.24 gwr
344 1.31 gwr /*
345 1.31 gwr * Set the master interrupt enable and interrupt vector.
346 1.31 gwr * (common to both channels, do it on A)
347 1.31 gwr */
348 1.43 gwr cs = zsc->zsc_cs[0];
349 1.45 gwr s = splhigh();
350 1.31 gwr /* interrupt vector */
351 1.32 gwr zs_write_reg(cs, 2, zs_init_reg[2]);
352 1.31 gwr /* master interrupt control (enable) */
353 1.32 gwr zs_write_reg(cs, 9, zs_init_reg[9]);
354 1.31 gwr splx(s);
355 1.45 gwr
356 1.45 gwr /*
357 1.45 gwr * XXX: L1A hack - We would like to be able to break into
358 1.45 gwr * the debugger during the rest of autoconfiguration, so
359 1.45 gwr * lower interrupts just enough to let zs interrupts in.
360 1.45 gwr * This is done after both zsc devices are attached.
361 1.45 gwr */
362 1.45 gwr if (zsc_unit == 1) {
363 1.45 gwr printf("zsc1: enabling zs interrupts\n");
364 1.45 gwr (void)spl5(); /* splzs - 1 */
365 1.45 gwr }
366 1.35 gwr }
367 1.35 gwr
368 1.35 gwr static int
369 1.35 gwr zsc_print(aux, name)
370 1.35 gwr void *aux;
371 1.39 cgd const char *name;
372 1.35 gwr {
373 1.35 gwr struct zsc_attach_args *args = aux;
374 1.35 gwr
375 1.35 gwr if (name != NULL)
376 1.41 christos printf("%s: ", name);
377 1.35 gwr
378 1.35 gwr if (args->channel != -1)
379 1.41 christos printf(" channel %d", args->channel);
380 1.35 gwr
381 1.35 gwr return UNCONF;
382 1.24 gwr }
383 1.24 gwr
384 1.43 gwr static int zssoftpending;
385 1.43 gwr
386 1.43 gwr /*
387 1.43 gwr * Our ZS chips all share a common, autovectored interrupt,
388 1.43 gwr * so we have to look at all of them on each interrupt.
389 1.43 gwr */
390 1.31 gwr static int
391 1.31 gwr zshard(arg)
392 1.31 gwr void *arg;
393 1.1 glass {
394 1.43 gwr register struct zsc_softc *zsc;
395 1.43 gwr register int unit, rval;
396 1.43 gwr
397 1.31 gwr /* Do ttya/ttyb first, because they go faster. */
398 1.31 gwr rval = 0;
399 1.34 thorpej unit = zsc_cd.cd_ndevs;
400 1.31 gwr while (--unit >= 0) {
401 1.34 thorpej zsc = zsc_cd.cd_devs[unit];
402 1.43 gwr if (zsc == NULL)
403 1.43 gwr continue;
404 1.43 gwr rval |= zsc_intr_hard(zsc);
405 1.43 gwr if ((zsc->zsc_cs[0]->cs_softreq) ||
406 1.43 gwr (zsc->zsc_cs[1]->cs_softreq))
407 1.43 gwr {
408 1.43 gwr /* zsc_req_softint(zsc); */
409 1.43 gwr /* We are at splzs here, so no need to lock. */
410 1.43 gwr if (zssoftpending == 0) {
411 1.43 gwr zssoftpending = ZSSOFT_PRI;
412 1.43 gwr isr_soft_request(ZSSOFT_PRI);
413 1.43 gwr }
414 1.31 gwr }
415 1.31 gwr }
416 1.31 gwr return (rval);
417 1.1 glass }
418 1.1 glass
419 1.43 gwr /*
420 1.43 gwr * Similar scheme as for zshard (look at all of them)
421 1.43 gwr */
422 1.3 gwr static int
423 1.31 gwr zssoft(arg)
424 1.31 gwr void *arg;
425 1.3 gwr {
426 1.43 gwr register struct zsc_softc *zsc;
427 1.43 gwr register int unit;
428 1.31 gwr
429 1.31 gwr /* This is not the only ISR on this IPL. */
430 1.31 gwr if (zssoftpending == 0)
431 1.31 gwr return (0);
432 1.3 gwr
433 1.31 gwr /*
434 1.31 gwr * The soft intr. bit will be set by zshard only if
435 1.31 gwr * the variable zssoftpending is zero. The order of
436 1.31 gwr * these next two statements prevents our clearing
437 1.31 gwr * the soft intr bit just after zshard has set it.
438 1.31 gwr */
439 1.31 gwr isr_soft_clear(ZSSOFT_PRI);
440 1.31 gwr zssoftpending = 0;
441 1.2 glass
442 1.31 gwr /* Do ttya/ttyb first, because they go faster. */
443 1.34 thorpej unit = zsc_cd.cd_ndevs;
444 1.31 gwr while (--unit >= 0) {
445 1.34 thorpej zsc = zsc_cd.cd_devs[unit];
446 1.43 gwr if (zsc == NULL)
447 1.43 gwr continue;
448 1.43 gwr (void) zsc_intr_soft(zsc);
449 1.3 gwr }
450 1.31 gwr return (1);
451 1.2 glass }
452 1.2 glass
453 1.2 glass
454 1.31 gwr /*
455 1.43 gwr * Compute the current baud rate given a ZSCC channel.
456 1.43 gwr */
457 1.43 gwr static int
458 1.43 gwr zs_get_speed(cs)
459 1.43 gwr struct zs_chanstate *cs;
460 1.43 gwr {
461 1.43 gwr int tconst;
462 1.43 gwr
463 1.43 gwr tconst = zs_read_reg(cs, 12);
464 1.43 gwr tconst |= zs_read_reg(cs, 13) << 8;
465 1.43 gwr return (TCONST_TO_BPS(cs->cs_brg_clk, tconst));
466 1.43 gwr }
467 1.43 gwr
468 1.43 gwr /*
469 1.43 gwr * MD functions for setting the baud rate and control modes.
470 1.43 gwr */
471 1.43 gwr int
472 1.43 gwr zs_set_speed(cs, bps)
473 1.43 gwr struct zs_chanstate *cs;
474 1.43 gwr int bps; /* bits per second */
475 1.43 gwr {
476 1.43 gwr int tconst, real_bps;
477 1.43 gwr
478 1.43 gwr if (bps == 0)
479 1.43 gwr return (0);
480 1.43 gwr
481 1.43 gwr #ifdef DIAGNOSTIC
482 1.43 gwr if (cs->cs_brg_clk == 0)
483 1.43 gwr panic("zs_set_speed");
484 1.43 gwr #endif
485 1.43 gwr
486 1.43 gwr tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
487 1.43 gwr if (tconst < 0)
488 1.43 gwr return (EINVAL);
489 1.43 gwr
490 1.43 gwr /* Convert back to make sure we can do it. */
491 1.43 gwr real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
492 1.43 gwr
493 1.43 gwr /* XXX - Allow some tolerance here? */
494 1.43 gwr if (real_bps != bps)
495 1.43 gwr return (EINVAL);
496 1.43 gwr
497 1.43 gwr cs->cs_preg[12] = tconst;
498 1.43 gwr cs->cs_preg[13] = tconst >> 8;
499 1.43 gwr
500 1.43 gwr /* Caller will stuff the pending registers. */
501 1.43 gwr return (0);
502 1.43 gwr }
503 1.43 gwr
504 1.43 gwr int
505 1.43 gwr zs_set_modes(cs, cflag)
506 1.43 gwr struct zs_chanstate *cs;
507 1.43 gwr int cflag; /* bits per second */
508 1.43 gwr {
509 1.43 gwr int s;
510 1.43 gwr
511 1.43 gwr /*
512 1.43 gwr * Output hardware flow control on the chip is horrendous:
513 1.43 gwr * if carrier detect drops, the receiver is disabled, and if
514 1.43 gwr * CTS drops, the transmitter is stoped IN MID CHARACTER!
515 1.43 gwr * Therefore, NEVER set the HFC bit, and instead use the
516 1.43 gwr * status interrupt to detect CTS changes.
517 1.43 gwr */
518 1.43 gwr s = splzs();
519 1.46 gwr #if 0 /* XXX - See below. */
520 1.43 gwr if (cflag & CLOCAL) {
521 1.43 gwr cs->cs_rr0_dcd = 0;
522 1.43 gwr cs->cs_preg[15] &= ~ZSWR15_DCD_IE;
523 1.43 gwr } else {
524 1.46 gwr /* XXX - Need to notice DCD change here... */
525 1.43 gwr cs->cs_rr0_dcd = ZSRR0_DCD;
526 1.43 gwr cs->cs_preg[15] |= ZSWR15_DCD_IE;
527 1.43 gwr }
528 1.46 gwr #endif /* XXX */
529 1.43 gwr if (cflag & CRTSCTS) {
530 1.43 gwr cs->cs_wr5_dtr = ZSWR5_DTR;
531 1.43 gwr cs->cs_wr5_rts = ZSWR5_RTS;
532 1.43 gwr cs->cs_rr0_cts = ZSRR0_CTS;
533 1.43 gwr cs->cs_preg[15] |= ZSWR15_CTS_IE;
534 1.43 gwr } else {
535 1.43 gwr cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
536 1.43 gwr cs->cs_wr5_rts = 0;
537 1.43 gwr cs->cs_rr0_cts = 0;
538 1.43 gwr cs->cs_preg[15] &= ~ZSWR15_CTS_IE;
539 1.43 gwr }
540 1.43 gwr splx(s);
541 1.43 gwr
542 1.43 gwr /* Caller will stuff the pending registers. */
543 1.43 gwr return (0);
544 1.43 gwr }
545 1.43 gwr
546 1.43 gwr
547 1.43 gwr /*
548 1.31 gwr * Read or write the chip with suitable delays.
549 1.31 gwr */
550 1.2 glass
551 1.31 gwr u_char
552 1.31 gwr zs_read_reg(cs, reg)
553 1.31 gwr struct zs_chanstate *cs;
554 1.31 gwr u_char reg;
555 1.1 glass {
556 1.31 gwr u_char val;
557 1.1 glass
558 1.31 gwr *cs->cs_reg_csr = reg;
559 1.31 gwr ZS_DELAY();
560 1.31 gwr val = *cs->cs_reg_csr;
561 1.31 gwr ZS_DELAY();
562 1.31 gwr return val;
563 1.17 gwr }
564 1.3 gwr
565 1.31 gwr void
566 1.31 gwr zs_write_reg(cs, reg, val)
567 1.31 gwr struct zs_chanstate *cs;
568 1.31 gwr u_char reg, val;
569 1.17 gwr {
570 1.31 gwr *cs->cs_reg_csr = reg;
571 1.31 gwr ZS_DELAY();
572 1.31 gwr *cs->cs_reg_csr = val;
573 1.32 gwr ZS_DELAY();
574 1.32 gwr }
575 1.32 gwr
576 1.32 gwr u_char zs_read_csr(cs)
577 1.32 gwr struct zs_chanstate *cs;
578 1.32 gwr {
579 1.43 gwr register u_char val;
580 1.32 gwr
581 1.43 gwr val = *cs->cs_reg_csr;
582 1.32 gwr ZS_DELAY();
583 1.43 gwr return val;
584 1.32 gwr }
585 1.32 gwr
586 1.43 gwr void zs_write_csr(cs, val)
587 1.32 gwr struct zs_chanstate *cs;
588 1.43 gwr u_char val;
589 1.32 gwr {
590 1.43 gwr *cs->cs_reg_csr = val;
591 1.32 gwr ZS_DELAY();
592 1.32 gwr }
593 1.32 gwr
594 1.43 gwr u_char zs_read_data(cs)
595 1.32 gwr struct zs_chanstate *cs;
596 1.32 gwr {
597 1.43 gwr register u_char val;
598 1.43 gwr
599 1.43 gwr val = *cs->cs_reg_data;
600 1.32 gwr ZS_DELAY();
601 1.43 gwr return val;
602 1.32 gwr }
603 1.32 gwr
604 1.32 gwr void zs_write_data(cs, val)
605 1.32 gwr struct zs_chanstate *cs;
606 1.32 gwr u_char val;
607 1.32 gwr {
608 1.32 gwr *cs->cs_reg_data = val;
609 1.31 gwr ZS_DELAY();
610 1.1 glass }
611 1.3 gwr
612 1.31 gwr /****************************************************************
613 1.31 gwr * Console support functions (Sun3 specific!)
614 1.43 gwr * Note: this code is allowed to know about the layout of
615 1.43 gwr * the chip registers, and uses that to keep things simple.
616 1.43 gwr * XXX - I think I like the mvme167 code better. -gwr
617 1.31 gwr ****************************************************************/
618 1.1 glass
619 1.43 gwr void *zs_conschan;
620 1.43 gwr
621 1.2 glass /*
622 1.47 gwr * Handle user request to enter kernel debugger.
623 1.47 gwr */
624 1.47 gwr void
625 1.47 gwr zs_abort(cs)
626 1.47 gwr struct zs_chanstate *cs;
627 1.47 gwr {
628 1.47 gwr register volatile struct zschan *zc = zs_conschan;
629 1.47 gwr int rr0;
630 1.47 gwr
631 1.47 gwr /* Wait for end of break to avoid PROM abort. */
632 1.47 gwr /* XXX - Limit the wait? */
633 1.47 gwr do {
634 1.47 gwr rr0 = zc->zc_csr;
635 1.47 gwr ZS_DELAY();
636 1.47 gwr } while (rr0 & ZSRR0_BREAK);
637 1.47 gwr
638 1.47 gwr /* XXX - Always available, but may be the PROM monitor. */
639 1.47 gwr Debugger();
640 1.47 gwr }
641 1.47 gwr
642 1.47 gwr /*
643 1.31 gwr * Polled input char.
644 1.2 glass */
645 1.2 glass int
646 1.31 gwr zs_getc(arg)
647 1.31 gwr void *arg;
648 1.2 glass {
649 1.31 gwr register volatile struct zschan *zc = arg;
650 1.25 gwr register int s, c, rr0;
651 1.2 glass
652 1.2 glass s = splhigh();
653 1.9 gwr /* Wait for a character to arrive. */
654 1.25 gwr do {
655 1.25 gwr rr0 = zc->zc_csr;
656 1.3 gwr ZS_DELAY();
657 1.25 gwr } while ((rr0 & ZSRR0_RX_READY) == 0);
658 1.9 gwr
659 1.2 glass c = zc->zc_data;
660 1.9 gwr ZS_DELAY();
661 1.2 glass splx(s);
662 1.17 gwr
663 1.17 gwr /*
664 1.17 gwr * This is used by the kd driver to read scan codes,
665 1.17 gwr * so don't translate '\r' ==> '\n' here...
666 1.17 gwr */
667 1.2 glass return (c);
668 1.2 glass }
669 1.1 glass
670 1.1 glass /*
671 1.31 gwr * Polled output char.
672 1.1 glass */
673 1.31 gwr void
674 1.31 gwr zs_putc(arg, c)
675 1.31 gwr void *arg;
676 1.1 glass int c;
677 1.1 glass {
678 1.31 gwr register volatile struct zschan *zc = arg;
679 1.25 gwr register int s, rr0;
680 1.1 glass
681 1.9 gwr s = splhigh();
682 1.9 gwr /* Wait for transmitter to become ready. */
683 1.25 gwr do {
684 1.25 gwr rr0 = zc->zc_csr;
685 1.3 gwr ZS_DELAY();
686 1.25 gwr } while ((rr0 & ZSRR0_TX_READY) == 0);
687 1.9 gwr
688 1.1 glass zc->zc_data = c;
689 1.3 gwr ZS_DELAY();
690 1.1 glass splx(s);
691 1.1 glass }
692 1.2 glass
693 1.31 gwr extern struct consdev consdev_kd; /* keyboard/display */
694 1.31 gwr extern struct consdev consdev_tty;
695 1.31 gwr extern struct consdev *cn_tab; /* physical console device info */
696 1.31 gwr
697 1.43 gwr static struct {
698 1.43 gwr int zsc_unit, channel;
699 1.43 gwr } zstty_conf[NZSC*2] = {
700 1.43 gwr /* XXX: knowledge from the config file here... */
701 1.43 gwr { 1, 0 }, /* ttya */
702 1.43 gwr { 1, 1 }, /* ttyb */
703 1.43 gwr { 0, 0 }, /* ttyc */
704 1.43 gwr { 0, 1 }, /* ttyd */
705 1.43 gwr };
706 1.31 gwr
707 1.47 gwr static char *prom_inSrc_name[] = {
708 1.47 gwr "keyboard/display",
709 1.47 gwr "ttya", "ttyb",
710 1.47 gwr "ttyc", "ttyd" };
711 1.47 gwr
712 1.1 glass /*
713 1.31 gwr * This function replaces sys/dev/cninit.c
714 1.31 gwr * Determine which device is the console using
715 1.38 gwr * the PROM "input source" and "output sink".
716 1.1 glass */
717 1.31 gwr void
718 1.31 gwr cninit()
719 1.1 glass {
720 1.38 gwr MachMonRomVector *v;
721 1.31 gwr struct zschan *zc;
722 1.31 gwr struct consdev *cn;
723 1.43 gwr int channel, zsc_unit, zstty_unit;
724 1.43 gwr u_char inSource;
725 1.31 gwr
726 1.38 gwr v = romVectorPtr;
727 1.38 gwr inSource = *(v->inSource);
728 1.31 gwr
729 1.38 gwr if (inSource != *(v->outSink)) {
730 1.38 gwr mon_printf("cninit: mismatched PROM output selector\n");
731 1.38 gwr }
732 1.38 gwr
733 1.38 gwr switch (inSource) {
734 1.38 gwr
735 1.47 gwr default:
736 1.47 gwr mon_printf("cninit: invalid inSource=%d\n", inSource);
737 1.47 gwr sunmon_abort();
738 1.47 gwr inSource = 0;
739 1.47 gwr /* fall through */
740 1.47 gwr
741 1.47 gwr case 0: /* keyboard/display */
742 1.47 gwr #if NKBD > 0
743 1.47 gwr zsc_unit = 0;
744 1.47 gwr channel = 0;
745 1.47 gwr cn = &consdev_kd;
746 1.47 gwr /* Set cn_dev, cn_pri in kd.c */
747 1.47 gwr break;
748 1.47 gwr #else /* NKBD */
749 1.47 gwr mon_printf("cninit: kdb/display not configured\n");
750 1.47 gwr sunmon_abort();
751 1.47 gwr inSource = 1;
752 1.47 gwr /* fall through */
753 1.47 gwr #endif /* NKBD */
754 1.47 gwr
755 1.38 gwr case 1: /* ttya */
756 1.38 gwr case 2: /* ttyb */
757 1.38 gwr case 3: /* ttyc (rewired keyboard connector) */
758 1.38 gwr case 4: /* ttyd (rewired mouse connector) */
759 1.43 gwr zstty_unit = inSource - 1;
760 1.43 gwr zsc_unit = zstty_conf[zstty_unit].zsc_unit;
761 1.43 gwr channel = zstty_conf[zstty_unit].channel;
762 1.38 gwr cn = &consdev_tty;
763 1.43 gwr cn->cn_dev = makedev(zs_major, zstty_unit);
764 1.38 gwr cn->cn_pri = CN_REMOTE;
765 1.38 gwr break;
766 1.38 gwr
767 1.1 glass }
768 1.47 gwr /* Now that inSource has been validated, print it. */
769 1.47 gwr mon_printf("console is %s\n", prom_inSrc_name[inSource]);
770 1.1 glass
771 1.31 gwr zc = zs_get_chan_addr(zsc_unit, channel);
772 1.31 gwr if (zc == NULL) {
773 1.31 gwr mon_printf("cninit: zs not mapped.\n");
774 1.31 gwr return;
775 1.31 gwr }
776 1.31 gwr zs_conschan = zc;
777 1.31 gwr zs_hwflags[zsc_unit][channel] = ZS_HWFLAG_CONSOLE;
778 1.31 gwr cn_tab = cn;
779 1.31 gwr (*cn->cn_init)(cn);
780 1.47 gwr #ifdef KGDB
781 1.47 gwr zs_kgdb_init();
782 1.47 gwr #endif
783 1.1 glass }
784 1.1 glass
785 1.1 glass
786 1.47 gwr static void zscn_nop __P((struct consdev *));
787 1.47 gwr static int zscngetc __P((dev_t));
788 1.47 gwr static void zscnputc __P((dev_t, int));
789 1.47 gwr
790 1.47 gwr struct consdev consdev_tty = {
791 1.47 gwr zscn_nop,
792 1.47 gwr zscn_nop,
793 1.47 gwr zscngetc,
794 1.47 gwr zscnputc,
795 1.47 gwr nullcnpollc,
796 1.47 gwr };
797 1.47 gwr
798 1.47 gwr static void
799 1.47 gwr zscn_nop(cn)
800 1.31 gwr struct consdev *cn;
801 1.1 glass {
802 1.1 glass }
803 1.1 glass
804 1.1 glass /*
805 1.31 gwr * Polled console input putchar.
806 1.1 glass */
807 1.43 gwr static int
808 1.31 gwr zscngetc(dev)
809 1.29 gwr dev_t dev;
810 1.1 glass {
811 1.31 gwr register int c;
812 1.1 glass
813 1.43 gwr c = zs_getc(zs_conschan);
814 1.31 gwr return (c);
815 1.1 glass }
816 1.1 glass
817 1.1 glass /*
818 1.31 gwr * Polled console output putchar.
819 1.1 glass */
820 1.43 gwr static void
821 1.31 gwr zscnputc(dev, c)
822 1.29 gwr dev_t dev;
823 1.31 gwr int c;
824 1.1 glass {
825 1.1 glass
826 1.43 gwr zs_putc(zs_conschan, c);
827 1.1 glass }
828 1.1 glass
829