zs.c revision 1.8 1 /* $NetBSD: zs.c,v 1.8 1999/02/28 00:29:24 eeh Exp $ */
2
3 /*-
4 * Copyright (c) 1996 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Gordon W. Ross.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Zilog Z8530 Dual UART driver (machine-dependent part)
41 *
42 * Runs two serial lines per chip using slave drivers.
43 * Plain tty/async lines use the zs_async slave.
44 * Sun keyboard/mouse uses the zs_kbd/zs_ms slaves.
45 */
46
47 #include "opt_ddb.h"
48
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/conf.h>
52 #include <sys/device.h>
53 #include <sys/file.h>
54 #include <sys/ioctl.h>
55 #include <sys/kernel.h>
56 #include <sys/proc.h>
57 #include <sys/tty.h>
58 #include <sys/time.h>
59 #include <sys/syslog.h>
60
61 #include <machine/autoconf.h>
62 #include <machine/openfirm.h>
63 #include <machine/bsd_openprom.h>
64 #include <machine/conf.h>
65 #include <machine/cpu.h>
66 #include <machine/eeprom.h>
67 #include <machine/psl.h>
68 #include <machine/z8530var.h>
69
70 #include <dev/cons.h>
71 #include <dev/ic/z8530reg.h>
72
73 #include <sparc64/sparc64/vaddrs.h>
74 #include <sparc64/sparc64/auxreg.h>
75 #include <sparc64/dev/cons.h>
76
77 #include "kbd.h" /* NKBD */
78 #include "zs.h" /* NZS */
79
80 /* Make life easier for the initialized arrays here. */
81 #if NZS < 3
82 #undef NZS
83 #define NZS 3
84 #endif
85
86 /*
87 * Some warts needed by z8530tty.c -
88 * The default parity REALLY needs to be the same as the PROM uses,
89 * or you can not see messages done with printf during boot-up...
90 */
91 int zs_def_cflag = (CREAD | CS8 | HUPCL);
92 int zs_major = 12;
93
94 /*
95 * The Sun provides a 4.9152 MHz clock to the ZS chips.
96 */
97 #define PCLK (9600 * 512) /* PCLK pin input clock rate */
98
99 /*
100 * Select software interrupt bit based on TTY ipl.
101 */
102 #if PIL_TTY == 1
103 # define IE_ZSSOFT IE_L1
104 #elif PIL_TTY == 4
105 # define IE_ZSSOFT IE_L4
106 #elif PIL_TTY == 6
107 # define IE_ZSSOFT IE_L6
108 #else
109 # error "no suitable software interrupt bit"
110 #endif
111
112 #define ZS_DELAY() (0)
113
114 /* The layout of this is hardware-dependent (padding, order). */
115 struct zschan {
116 volatile u_char zc_csr; /* ctrl,status, and indirect access */
117 u_char zc_xxx0;
118 volatile u_char zc_data; /* data */
119 u_char zc_xxx1;
120 };
121 struct zsdevice {
122 /* Yes, they are backwards. */
123 struct zschan zs_chan_b;
124 struct zschan zs_chan_a;
125 };
126
127 /* Saved PROM mappings */
128 static struct zsdevice *zsaddr[NZS];
129
130 /* Flags from cninit() */
131 static int zs_hwflags[NZS][2];
132
133 /* Default speed for each channel */
134 static int zs_defspeed[NZS][2] = {
135 { 9600, /* ttya */
136 9600 }, /* ttyb */
137 { 1200, /* keyboard */
138 1200 }, /* mouse */
139 { 9600, /* ttyc */
140 9600 }, /* ttyd */
141 };
142
143 static u_char zs_init_reg[16] = {
144 0, /* 0: CMD (reset, etc.) */
145 0, /* 1: No interrupts yet. */
146 0, /* 2: IVECT */
147 ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
148 ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
149 ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
150 0, /* 6: TXSYNC/SYNCLO */
151 0, /* 7: RXSYNC/SYNCHI */
152 0, /* 8: alias for data port */
153 ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR,
154 0, /*10: Misc. TX/RX control bits */
155 ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
156 ((PCLK/32)/9600)-2, /*12: BAUDLO (default=9600) */
157 0, /*13: BAUDHI (default=9600) */
158 ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
159 ZSWR15_BREAK_IE,
160 };
161
162 struct zschan *
163 zs_get_chan_addr(zs_unit, channel)
164 int zs_unit, channel;
165 {
166 struct zsdevice *addr;
167 struct zschan *zc;
168
169 if (zs_unit >= NZS)
170 return (NULL);
171 addr = zsaddr[zs_unit];
172 if (addr == NULL)
173 addr = zsaddr[zs_unit] = findzs(zs_unit);
174 if (addr == NULL)
175 return (NULL);
176 if (channel == 0) {
177 zc = &addr->zs_chan_a;
178 } else {
179 zc = &addr->zs_chan_b;
180 }
181 return (zc);
182 }
183
184
185 /****************************************************************
186 * Autoconfig
187 ****************************************************************/
188
189 /* Definition of the driver for autoconfig. */
190 static int zs_match_sbus __P((struct device *, struct cfdata *, void *));
191 static int zs_match_mainbus __P((struct device *, struct cfdata *, void *));
192 static int zs_match_obio __P((struct device *, struct cfdata *, void *));
193 static void zs_attach_sbus __P((struct device *, struct device *, void *));
194 static void zs_attach_mainbus __P((struct device *, struct device *, void *));
195 static void zs_attach_obio __P((struct device *, struct device *, void *));
196
197 static void zs_attach __P((struct zsc_softc *, int));
198 static int zs_print __P((void *, const char *name));
199
200 struct cfattach zs_ca = {
201 sizeof(struct zsc_softc), zs_match_sbus, zs_attach_sbus
202 };
203
204 struct cfattach zs_mainbus_ca = {
205 sizeof(struct zsc_softc), zs_match_mainbus, zs_attach_mainbus
206 };
207
208 struct cfattach zs_obio_ca = {
209 sizeof(struct zsc_softc), zs_match_obio, zs_attach_obio
210 };
211
212 extern struct cfdriver zs_cd;
213
214 /* Interrupt handlers. */
215 static int zshard __P((void *));
216 static int zssoft __P((void *));
217 static struct intrhand levelsoft = { zssoft };
218
219 static int zs_get_speed __P((struct zs_chanstate *));
220
221
222 /*
223 * Is the zs chip present?
224 */
225 static int
226 zs_match_mainbus(parent, cf, aux)
227 struct device *parent;
228 struct cfdata *cf;
229 void *aux;
230 {
231 struct mainbus_attach_args *ma = aux;
232
233 if (strcmp(cf->cf_driver->cd_name, ma->ma_name) != 0)
234 return (0);
235
236 return (getpropint(ma->ma_node, "slave", -2) == cf->cf_unit);
237 }
238
239 static int
240 zs_match_sbus(parent, cf, aux)
241 struct device *parent;
242 struct cfdata *cf;
243 void *aux;
244 {
245 struct sbus_attach_args *sa = aux;
246
247 if (strcmp(cf->cf_driver->cd_name, sa->sa_name) != 0)
248 return (0);
249
250 return 1;
251 }
252
253 static int
254 zs_match_obio(parent, cf, aux)
255 struct device *parent;
256 struct cfdata *cf;
257 void *aux;
258 {
259 #ifdef SUN4U
260 return 0;
261 #else
262 union obio_attach_args *uoba = aux;
263 struct obio4_attach_args *oba;
264
265 if (uoba->uoba_isobio4 == 0) {
266 struct sbus_attach_args *sa = &uoba->uoba_sbus;
267
268 if (strcmp(cf->cf_driver->cd_name, sa->sa_name) != 0)
269 return (0);
270
271 return (getpropint(sa->sa_node, "slave", -2) == cf->cf_unit);
272 }
273
274 oba = &uoba->uoba_oba4;
275 return (bus_space_probe(oba->oba_bustag, 0, oba->oba_paddr,
276 1, 0, 0, NULL, NULL));
277 #endif
278 }
279
280 static void
281 zs_attach_mainbus(parent, self, aux)
282 struct device *parent;
283 struct device *self;
284 void *aux;
285 {
286 #ifdef SUN4U
287 return 0;
288 #else
289 struct zsc_softc *zsc = (void *) self;
290 struct mainbus_attach_args *ma = aux;
291 int zs_unit = zsc->zsc_dev.dv_unit;
292
293 zsc->zsc_bustag = ma->ma_bustag;
294 zsc->zsc_dmatag = ma->ma_dmatag;
295
296 /* Use the mapping setup by the Sun PROM. */
297 if (zsaddr[zs_unit] == NULL)
298 zsaddr[zs_unit] = findzs(zs_unit);
299 if ((void*)zsaddr[zs_unit] != (void*)(u_long)ma->ma_address[0])
300 panic("zsattach_mainbus");
301 zs_attach(zsc, ma->ma_pri);
302 #endif
303 }
304
305
306 static void
307 zs_attach_sbus(parent, self, aux)
308 struct device *parent;
309 struct device *self;
310 void *aux;
311 {
312 struct zsc_softc *zsc = (void *) self;
313 struct sbus_attach_args *sa = aux;
314 int zs_unit = zsc->zsc_dev.dv_unit;
315
316 zsc->zsc_bustag = sa->sa_bustag;
317 zsc->zsc_dmatag = sa->sa_dmatag;
318
319 /* Use the mapping setup by the Sun PROM. */
320 if (zsaddr[zs_unit] == NULL)
321 zsaddr[zs_unit] = findzs(zs_unit);
322 zs_attach(zsc, sa->sa_pri);
323 }
324
325 static void
326 zs_attach_obio(parent, self, aux)
327 struct device *parent;
328 struct device *self;
329 void *aux;
330 {
331 #ifndef SUN4U
332 struct zsc_softc *zsc = (void *) self;
333 union obio_attach_args *uoba = aux;
334 int zs_unit = zsc->zsc_dev.dv_unit;
335
336 /* Use the mapping setup by the Sun PROM. */
337 if (zsaddr[zs_unit] == NULL)
338 zsaddr[zs_unit] = findzs(zs_unit);
339
340 if (uoba->uoba_isobio4 == 0) {
341 struct sbus_attach_args *sa = &uoba->uoba_sbus;
342 zsc->zsc_bustag = sa->sa_bustag;
343 zsc->zsc_dmatag = sa->sa_dmatag;
344 zs_attach(zsc, sa->sa_pri);
345 } else {
346 struct obio4_attach_args *oba = &uoba->uoba_oba4;
347 zsc->zsc_bustag = oba->oba_bustag;
348 zsc->zsc_dmatag = oba->oba_dmatag;
349 zs_attach(zsc, oba->oba_pri);
350 }
351 #endif
352 }
353 /*
354 * Attach a found zs.
355 *
356 * USE ROM PROPERTIES port-a-ignore-cd AND port-b-ignore-cd FOR
357 * SOFT CARRIER, AND keyboard PROPERTY FOR KEYBOARD/MOUSE?
358 */
359 static void
360 zs_attach(zsc, pri)
361 struct zsc_softc *zsc;
362 int pri;
363 {
364 struct zsc_attach_args zsc_args;
365 volatile struct zschan *zc;
366 struct zs_chanstate *cs;
367 int s, zs_unit, channel;
368 static int didintr, prevpri;
369
370 printf(" softpri %d\n", PIL_TTY);
371
372 /*
373 * Initialize software state for each channel.
374 */
375 zs_unit = zsc->zsc_dev.dv_unit;
376 for (channel = 0; channel < 2; channel++) {
377 zsc_args.channel = channel;
378 zsc_args.hwflags = zs_hwflags[zs_unit][channel];
379 cs = &zsc->zsc_cs_store[channel];
380 zsc->zsc_cs[channel] = cs;
381
382 cs->cs_channel = channel;
383 cs->cs_private = NULL;
384 cs->cs_ops = &zsops_null;
385 cs->cs_brg_clk = PCLK / 16;
386
387 zc = zs_get_chan_addr(zs_unit, channel);
388 cs->cs_reg_csr = &zc->zc_csr;
389 cs->cs_reg_data = &zc->zc_data;
390
391 bcopy(zs_init_reg, cs->cs_creg, 16);
392 bcopy(zs_init_reg, cs->cs_preg, 16);
393
394 /* XXX: Get these from the PROM properties! */
395 /* XXX: See the mvme167 code. Better. */
396 if (zsc_args.hwflags & ZS_HWFLAG_CONSOLE)
397 cs->cs_defspeed = zs_get_speed(cs);
398 else
399 cs->cs_defspeed = zs_defspeed[zs_unit][channel];
400 cs->cs_defcflag = zs_def_cflag;
401
402 /* Make these correspond to cs_defcflag (-crtscts) */
403 cs->cs_rr0_dcd = ZSRR0_DCD;
404 cs->cs_rr0_cts = 0;
405 cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
406 cs->cs_wr5_rts = 0;
407
408 /*
409 * Clear the master interrupt enable.
410 * The INTENA is common to both channels,
411 * so just do it on the A channel.
412 */
413 if (channel == 0) {
414 zs_write_reg(cs, 9, 0);
415 }
416
417 /*
418 * Look for a child driver for this channel.
419 * The child attach will setup the hardware.
420 */
421 if (!config_found(&zsc->zsc_dev, (void *)&zsc_args, zs_print)) {
422 /* No sub-driver. Just reset it. */
423 u_char reset = (channel == 0) ?
424 ZSWR9_A_RESET : ZSWR9_B_RESET;
425 s = splzs();
426 zs_write_reg(cs, 9, reset);
427 splx(s);
428 }
429 }
430
431 /*
432 * Now safe to install interrupt handlers. Note the arguments
433 * to the interrupt handlers aren't used. Note, we only do this
434 * once since both SCCs interrupt at the same level and vector.
435 */
436 if (!didintr) {
437 didintr = 1;
438 prevpri = pri;
439 bus_intr_establish(zsc->zsc_bustag, pri, 0, zshard, NULL);
440 intr_establish(PIL_TTY, &levelsoft);
441 } else if (pri != prevpri)
442 panic("broken zs interrupt scheme");
443
444 evcnt_attach(&zsc->zsc_dev, "intr", &zsc->zsc_intrcnt);
445
446 /*
447 * Set the master interrupt enable and interrupt vector.
448 * (common to both channels, do it on A)
449 */
450 cs = zsc->zsc_cs[0];
451 s = splhigh();
452 /* interrupt vector */
453 zs_write_reg(cs, 2, zs_init_reg[2]);
454 /* master interrupt control (enable) */
455 zs_write_reg(cs, 9, zs_init_reg[9]);
456 splx(s);
457
458 #if 0
459 /*
460 * XXX: L1A hack - We would like to be able to break into
461 * the debugger during the rest of autoconfiguration, so
462 * lower interrupts just enough to let zs interrupts in.
463 * This is done after both zs devices are attached.
464 */
465 if (zs_unit == 1) {
466 printf("zs1: enabling zs interrupts\n");
467 (void)splfd(); /* XXX: splzs - 1 */
468 }
469 #endif
470 }
471
472 static int
473 zs_print(aux, name)
474 void *aux;
475 const char *name;
476 {
477 struct zsc_attach_args *args = aux;
478
479 if (name != NULL)
480 printf("%s: ", name);
481
482 if (args->channel != -1)
483 printf(" channel %d", args->channel);
484
485 return (UNCONF);
486 }
487
488 static volatile int zssoftpending;
489
490 /*
491 * Our ZS chips all share a common, autovectored interrupt,
492 * so we have to look at all of them on each interrupt.
493 */
494 static int
495 zshard(arg)
496 void *arg;
497 {
498 register struct zsc_softc *zsc;
499 register int unit, rr3, rval, softreq;
500
501 rval = softreq = 0;
502 for (unit = 0; unit < zs_cd.cd_ndevs; unit++) {
503 zsc = zs_cd.cd_devs[unit];
504 if (zsc == NULL)
505 continue;
506 rr3 = zsc_intr_hard(zsc);
507 /* Count up the interrupts. */
508 if (rr3) {
509 rval |= rr3;
510 zsc->zsc_intrcnt.ev_count++;
511 }
512 softreq |= zsc->zsc_cs[0]->cs_softreq;
513 softreq |= zsc->zsc_cs[1]->cs_softreq;
514 }
515
516 /* We are at splzs here, so no need to lock. */
517 if (softreq && (zssoftpending == 0)) {
518 zssoftpending = IE_ZSSOFT;
519 #if defined(SUN4M)
520 if (CPU_ISSUN4M)
521 raise(0, PIL_TTY);
522 else
523 #endif
524 ienab_bis(IE_ZSSOFT);
525 }
526 return (rval);
527 }
528
529 /*
530 * Similar scheme as for zshard (look at all of them)
531 */
532 static int
533 zssoft(arg)
534 void *arg;
535 {
536 register struct zsc_softc *zsc;
537 register int s, unit;
538
539 /* This is not the only ISR on this IPL. */
540 if (zssoftpending == 0)
541 return (0);
542
543 /*
544 * The soft intr. bit will be set by zshard only if
545 * the variable zssoftpending is zero. The order of
546 * these next two statements prevents our clearing
547 * the soft intr bit just after zshard has set it.
548 */
549 /* ienab_bic(IE_ZSSOFT); */
550 zssoftpending = 0;
551
552 /* Make sure we call the tty layer at spltty. */
553 s = spltty();
554 for (unit = 0; unit < zs_cd.cd_ndevs; unit++) {
555 zsc = zs_cd.cd_devs[unit];
556 if (zsc == NULL)
557 continue;
558 (void)zsc_intr_soft(zsc);
559 }
560 splx(s);
561 return (1);
562 }
563
564
565 /*
566 * Compute the current baud rate given a ZS channel.
567 */
568 static int
569 zs_get_speed(cs)
570 struct zs_chanstate *cs;
571 {
572 int tconst;
573
574 tconst = zs_read_reg(cs, 12);
575 tconst |= zs_read_reg(cs, 13) << 8;
576 return (TCONST_TO_BPS(cs->cs_brg_clk, tconst));
577 }
578
579 /*
580 * MD functions for setting the baud rate and control modes.
581 */
582 int
583 zs_set_speed(cs, bps)
584 struct zs_chanstate *cs;
585 int bps; /* bits per second */
586 {
587 int tconst, real_bps;
588
589 if (bps == 0)
590 return (0);
591
592 #ifdef DIAGNOSTIC
593 if (cs->cs_brg_clk == 0)
594 panic("zs_set_speed");
595 #endif
596
597 tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
598 if (tconst < 0)
599 return (EINVAL);
600
601 /* Convert back to make sure we can do it. */
602 real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
603
604 /* XXX - Allow some tolerance here? */
605 if (real_bps != bps)
606 return (EINVAL);
607
608 cs->cs_preg[12] = tconst;
609 cs->cs_preg[13] = tconst >> 8;
610
611 /* Caller will stuff the pending registers. */
612 return (0);
613 }
614
615 int
616 zs_set_modes(cs, cflag)
617 struct zs_chanstate *cs;
618 int cflag; /* bits per second */
619 {
620 int s;
621
622 /*
623 * Output hardware flow control on the chip is horrendous:
624 * if carrier detect drops, the receiver is disabled, and if
625 * CTS drops, the transmitter is stoped IN MID CHARACTER!
626 * Therefore, NEVER set the HFC bit, and instead use the
627 * status interrupt to detect CTS changes.
628 */
629 s = splzs();
630 if ((cflag & (CLOCAL | MDMBUF)) != 0)
631 cs->cs_rr0_dcd = 0;
632 else
633 cs->cs_rr0_dcd = ZSRR0_DCD;
634 if ((cflag & CRTSCTS) != 0) {
635 cs->cs_wr5_dtr = ZSWR5_DTR;
636 cs->cs_wr5_rts = ZSWR5_RTS;
637 cs->cs_rr0_cts = ZSRR0_CTS;
638 } else if ((cflag & CDTRCTS) != 0) {
639 cs->cs_wr5_dtr = 0;
640 cs->cs_wr5_rts = ZSWR5_DTR;
641 cs->cs_rr0_cts = ZSRR0_CTS;
642 } else if ((cflag & MDMBUF) != 0) {
643 cs->cs_wr5_dtr = 0;
644 cs->cs_wr5_rts = ZSWR5_DTR;
645 cs->cs_rr0_cts = ZSRR0_DCD;
646 } else {
647 cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
648 cs->cs_wr5_rts = 0;
649 cs->cs_rr0_cts = 0;
650 }
651 splx(s);
652
653 /* Caller will stuff the pending registers. */
654 return (0);
655 }
656
657
658 /*
659 * Read or write the chip with suitable delays.
660 */
661
662 u_char
663 zs_read_reg(cs, reg)
664 struct zs_chanstate *cs;
665 u_char reg;
666 {
667 u_char val;
668
669 *cs->cs_reg_csr = reg;
670 ZS_DELAY();
671 val = *cs->cs_reg_csr;
672 ZS_DELAY();
673 return (val);
674 }
675
676 void
677 zs_write_reg(cs, reg, val)
678 struct zs_chanstate *cs;
679 u_char reg, val;
680 {
681 *cs->cs_reg_csr = reg;
682 ZS_DELAY();
683 *cs->cs_reg_csr = val;
684 ZS_DELAY();
685 }
686
687 u_char
688 zs_read_csr(cs)
689 struct zs_chanstate *cs;
690 {
691 register u_char val;
692
693 val = *cs->cs_reg_csr;
694 ZS_DELAY();
695 return (val);
696 }
697
698 void zs_write_csr(cs, val)
699 struct zs_chanstate *cs;
700 u_char val;
701 {
702 *cs->cs_reg_csr = val;
703 ZS_DELAY();
704 }
705
706 u_char zs_read_data(cs)
707 struct zs_chanstate *cs;
708 {
709 register u_char val;
710
711 val = *cs->cs_reg_data;
712 ZS_DELAY();
713 return (val);
714 }
715
716 void zs_write_data(cs, val)
717 struct zs_chanstate *cs;
718 u_char val;
719 {
720 *cs->cs_reg_data = val;
721 ZS_DELAY();
722 }
723
724 /****************************************************************
725 * Console support functions (Sun specific!)
726 * Note: this code is allowed to know about the layout of
727 * the chip registers, and uses that to keep things simple.
728 * XXX - I think I like the mvme167 code better. -gwr
729 ****************************************************************/
730
731 extern void Debugger __P((void));
732 void *zs_conschan;
733
734 /*
735 * Handle user request to enter kernel debugger.
736 */
737 void
738 zs_abort(cs)
739 struct zs_chanstate *cs;
740 {
741 register volatile struct zschan *zc = zs_conschan;
742 int rr0;
743
744 /* Wait for end of break to avoid PROM abort. */
745 /* XXX - Limit the wait? */
746 do {
747 rr0 = zc->zc_csr;
748 ZS_DELAY();
749 } while (rr0 & ZSRR0_BREAK);
750
751 #if defined(KGDB)
752 zskgdb(cs);
753 #elif defined(DDB)
754 Debugger();
755 #else
756 printf("stopping on keyboard abort\n");
757 callrom();
758 #endif
759 }
760
761 /*
762 * Polled input char.
763 */
764 int
765 zs_getc(arg)
766 void *arg;
767 {
768 register volatile struct zschan *zc = arg;
769 register int s, c, rr0;
770
771 s = splhigh();
772 /* Wait for a character to arrive. */
773 do {
774 rr0 = zc->zc_csr;
775 ZS_DELAY();
776 } while ((rr0 & ZSRR0_RX_READY) == 0);
777
778 c = zc->zc_data;
779 ZS_DELAY();
780 splx(s);
781
782 /*
783 * This is used by the kd driver to read scan codes,
784 * so don't translate '\r' ==> '\n' here...
785 */
786 return (c);
787 }
788
789 /*
790 * Polled output char.
791 */
792 void
793 zs_putc(arg, c)
794 void *arg;
795 int c;
796 {
797 register volatile struct zschan *zc = arg;
798 register int s, rr0;
799
800 s = splhigh();
801
802 /* Wait for transmitter to become ready. */
803 do {
804 rr0 = zc->zc_csr;
805 ZS_DELAY();
806 } while ((rr0 & ZSRR0_TX_READY) == 0);
807
808 /*
809 * Send the next character.
810 * Now you'd think that this could be followed by a ZS_DELAY()
811 * just like all the other chip accesses, but it turns out that
812 * the `transmit-ready' interrupt isn't de-asserted until
813 * some period of time after the register write completes
814 * (more than a couple instructions). So to avoid stray
815 * interrupts we put in the 2us delay regardless of cpu model.
816 */
817 zc->zc_data = c;
818 delay(2);
819
820 splx(s);
821 }
822
823 /*****************************************************************/
824
825 static void zscninit __P((struct consdev *));
826 static int zscngetc __P((dev_t));
827 static void zscnputc __P((dev_t, int));
828 static void zscnpollc __P((dev_t, int));
829 /*
830 * Console table shared by ttya, ttyb
831 */
832 struct consdev consdev_tty = {
833 nullcnprobe,
834 zscninit,
835 zscngetc,
836 zscnputc,
837 zscnpollc,
838 };
839
840 static void
841 zscninit(cn)
842 struct consdev *cn;
843 {
844 }
845
846 /*
847 * Polled console input putchar.
848 */
849 static int
850 zscngetc(dev)
851 dev_t dev;
852 {
853 return (zs_getc(zs_conschan));
854 }
855
856 /*
857 * Polled console output putchar.
858 */
859 static void
860 zscnputc(dev, c)
861 dev_t dev;
862 int c;
863 {
864 zs_putc(zs_conschan, c);
865 }
866
867 int swallow_zsintrs;
868
869 static void
870 zscnpollc(dev, on)
871 dev_t dev;
872 int on;
873 {
874 /*
875 * Need to tell zs driver to acknowledge all interrupts or we get
876 * annoying spurious interrupt messages. This is because mucking
877 * with spl() levels during polling does not prevent interrupts from
878 * being generated.
879 */
880
881 if (on) swallow_zsintrs++;
882 else swallow_zsintrs--;
883 }
884
885 /*****************************************************************/
886
887 static void prom_cninit __P((struct consdev *));
888 static int prom_cngetc __P((dev_t));
889 static void prom_cnputc __P((dev_t, int));
890
891 int stdin = NULL, stdout = NULL;
892
893 /*
894 * The console is set to this one initially,
895 * which lets us use the PROM until consinit()
896 * is called to select a real console.
897 */
898 struct consdev consdev_prom = {
899 nullcnprobe,
900 prom_cninit,
901 prom_cngetc,
902 prom_cnputc,
903 nullcnpollc,
904 };
905
906 /*
907 * The console table pointer is statically initialized
908 * to point to the PROM (output only) table, so that
909 * early calls to printf will work.
910 */
911 struct consdev *cn_tab = &consdev_prom;
912
913 void
914 nullcnprobe(cn)
915 struct consdev *cn;
916 {
917 }
918
919 static void
920 prom_cninit(cn)
921 struct consdev *cn;
922 {
923 }
924
925 /*
926 * PROM console input putchar.
927 * (dummy - this is output only)
928 */
929 static int
930 prom_cngetc(dev)
931 dev_t dev;
932 {
933 int s;
934 char c0;
935
936 if (!stdin) {
937 int node = OF_finddevice("/chosen");
938 OF_getprop(node, "stdin", &stdin, sizeof(stdin));
939 }
940 if (OF_read(stdin, &c0, 1) == 1)
941 return (c0 & 0x7f);
942 return -1;
943 }
944
945 /*
946 * PROM console output putchar.
947 */
948 static void
949 prom_cnputc(dev, c)
950 dev_t dev;
951 int c;
952 {
953 int s;
954 char c0 = (c & 0x7f);
955
956 if (!stdout) {
957 int node = OF_finddevice("/chosen");
958 OF_getprop(node, "stdout", &stdout, sizeof(stdout));
959 }
960
961 s = splhigh();
962 OF_write(stdout, &c0, 1);
963 splx(s);
964 }
965
966 /*****************************************************************/
967
968 extern struct consdev consdev_kd;
969
970 static char *prom_inSrc_name[] = {
971 "keyboard/display",
972 "ttya", "ttyb",
973 "ttyc", "ttyd" };
974
975 #ifdef DEBUG
976 #define DBPRINT(x) printf x
977 #else
978 #define DBPRINT(x)
979 #endif
980
981 /*
982 * This function replaces sys/dev/cninit.c
983 * Determine which device is the console using
984 * the PROM "input source" and "output sink".
985 */
986 void
987 consinit()
988 {
989 struct zschan *zc;
990 struct consdev *cn;
991 int channel, zs_unit, zstty_unit;
992 int inSource, outSink;
993 register int node;
994 char buffer[128];
995 register char *cp;
996 extern int fbnode;
997
998 DBPRINT(("consinit()\r\n"));
999 if (cn_tab != &consdev_prom) return;
1000
1001 inSource = outSink = -1;
1002
1003 DBPRINT(("setting up stdin\r\n"));
1004 node = OF_finddevice("/chosen");
1005 OF_getprop(node, "stdin", &stdin, sizeof(stdin));
1006 DBPRINT(("stdin instance = %x\r\n", stdin));
1007
1008 node = OF_instance_to_package(stdin);
1009 DBPRINT(("stdin package = %x\r\n", node));
1010 if (OF_getproplen(node,"keyboard") >= 0) {
1011 inSource = PROMDEV_KBD;
1012 goto setup_output;
1013 }
1014 if (strcmp(getpropstring(node,"device_type"),"serial") != 0) {
1015 /* not a serial, not keyboard. what is it?!? */
1016 inSource = -1;
1017 goto setup_output;
1018 }
1019 /*
1020 * At this point we assume the device path is in the form
1021 * ....device@x,y:a for ttya and ...device@x,y:b for ttyb.
1022 * If it isn't, we defer to the ROM
1023 */
1024 if(OF_instance_to_path(stdin, buffer, sizeof(buffer)) <= 0) {
1025 printf("consinit: bogus stdin path.\n");
1026 goto setup_output;
1027 }
1028 cp = buffer;
1029 while (*cp)
1030 cp++;
1031 cp -= 2;
1032 #ifdef DEBUG
1033 if (cp < buffer)
1034 panic("consinit: bad stdin path %s",buffer);
1035 #endif
1036 /* XXX: only allows tty's a->z, assumes PROMDEV_TTYx contig */
1037 if (cp[0]==':' && cp[1] >= 'a' && cp[1] <= 'z')
1038 inSource = PROMDEV_TTYA + (cp[1] - 'a');
1039 /* else use rom */
1040 setup_output:
1041 DBPRINT(("setting up stdout\r\n"));
1042 node = OF_finddevice("/chosen");
1043 OF_getprop(node, "stdout", &stdout, sizeof(stdout));
1044
1045 DBPRINT(("stdout instance = %x\r\n", stdout));
1046
1047 node = OF_instance_to_package(stdout);
1048 DBPRINT(("stdout package = %x\r\n", node));
1049 if (strcmp(getpropstring(node,"device_type"),"display") == 0) {
1050 /* frame buffer output */
1051 outSink = PROMDEV_SCREEN;
1052 fbnode = node;
1053 } else if (strcmp(getpropstring(node,"device_type"), "serial")
1054 != 0) {
1055 /* not screen, not serial. Whatzit? */
1056 outSink = -1;
1057 } else { /* serial console. which? */
1058 /*
1059 * At this point we assume the device path is in the
1060 * form:
1061 * ....device@x,y:a for ttya, etc.
1062 * If it isn't, we defer to the ROM
1063 */
1064 if(OF_instance_to_path(stdout, buffer, sizeof(buffer)) <= 0) {
1065 printf("consinit: bogus stdin path.\n");
1066 goto setup_output;
1067 }
1068 cp = buffer;
1069 while (*cp)
1070 cp++;
1071 cp -= 2;
1072 #ifdef DEBUG
1073 if (cp < buffer)
1074 panic("consinit: bad stdout path %s",buffer);
1075 #endif
1076 /* XXX: only allows tty's a->z, assumes PROMDEV_TTYx contig */
1077 if (cp[0]==':' && cp[1] >= 'a' && cp[1] <= 'z')
1078 outSink = PROMDEV_TTYA + (cp[1] - 'a');
1079 else outSink = -1;
1080 }
1081 if (inSource != outSink) {
1082 printf("cninit: mismatched PROM output selector\n");
1083 }
1084
1085 switch (inSource) {
1086 default:
1087 printf("cninit: invalid inSource=%d\n", inSource);
1088 callrom();
1089 inSource = PROMDEV_KBD;
1090 /* fall through */
1091
1092 case 0: /* keyboard/display */
1093 #if NKBD > 0
1094 zs_unit = 1; /* XXX - config info! */
1095 channel = 0;
1096 cn = &consdev_kd;
1097 /* Set cn_dev, cn_pri in kd.c */
1098 break;
1099 #else /* NKBD */
1100 printf("cninit: kdb/display not configured\n");
1101 callrom();
1102 inSource = PROMDEV_TTYA;
1103 /* fall through */
1104 #endif /* NKBD */
1105
1106 case PROMDEV_TTYA:
1107 case PROMDEV_TTYB:
1108 zstty_unit = inSource - PROMDEV_TTYA;
1109 zs_unit = 0; /* XXX - config info! */
1110 channel = zstty_unit & 1;
1111 cn = &consdev_tty;
1112 cn->cn_dev = makedev(zs_major, zstty_unit);
1113 cn->cn_pri = CN_REMOTE;
1114 break;
1115
1116 }
1117 /* Now that inSource has been validated, print it. */
1118 printf("console is %s\n", prom_inSrc_name[inSource]);
1119
1120 zc = zs_get_chan_addr(zs_unit, channel);
1121 if (zc == NULL) {
1122 printf("cninit: zs not mapped.\n");
1123 return;
1124 }
1125 zs_conschan = zc;
1126 zs_hwflags[zs_unit][channel] = ZS_HWFLAG_CONSOLE;
1127 cn_tab = cn;
1128 (*cn->cn_init)(cn);
1129 #ifdef KGDB
1130 zs_kgdb_init();
1131 #endif
1132 }
1133