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