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