zs.c revision 1.6 1 /* $NetBSD: zs.c,v 1.6 1999/02/03 20:25:07 mycroft 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 14, /*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 return (0);
934 }
935
936 /*
937 * PROM console output putchar.
938 */
939 static void
940 prom_cnputc(dev, c)
941 dev_t dev;
942 int c;
943 {
944 int s;
945 char c0 = (c & 0x7f);
946
947 if (!stdout) {
948 int node = OF_finddevice("/chosen");
949 OF_getprop(node, "stdout", &stdout, sizeof(stdout));
950 }
951
952 s = splhigh();
953 OF_write(stdout, &c0, 1);
954 splx(s);
955 }
956
957 /*****************************************************************/
958
959 extern struct consdev consdev_kd;
960
961 static char *prom_inSrc_name[] = {
962 "keyboard/display",
963 "ttya", "ttyb",
964 "ttyc", "ttyd" };
965
966 /*
967 * This function replaces sys/dev/cninit.c
968 * Determine which device is the console using
969 * the PROM "input source" and "output sink".
970 */
971 void
972 consinit()
973 {
974 struct zschan *zc;
975 struct consdev *cn;
976 int channel, zs_unit, zstty_unit;
977 int inSource, outSink;
978 register int node;
979 char buffer[128];
980 register char *cp;
981 extern int fbnode;
982
983 prom_printf("consinit()\r\n");
984 if (cn_tab != &consdev_prom) return;
985
986 inSource = outSink = -1;
987
988 prom_printf("setting up stdin\r\n");
989 node = OF_finddevice("/chosen");
990 OF_getprop(node, "stdin", &stdin, sizeof(stdin));
991 prom_printf("stdin instance = %x\r\n", stdin);
992
993 node = OF_instance_to_package(stdin);
994 prom_printf("stdin package = %x\r\n", node);
995 if (OF_getproplen(node,"keyboard") >= 0) {
996 inSource = PROMDEV_KBD;
997 goto setup_output;
998 }
999 if (strcmp(getpropstring(node,"device_type"),"serial") != 0) {
1000 /* not a serial, not keyboard. what is it?!? */
1001 inSource = -1;
1002 goto setup_output;
1003 }
1004 /*
1005 * At this point we assume the device path is in the form
1006 * ....device@x,y:a for ttya and ...device@x,y:b for ttyb.
1007 * If it isn't, we defer to the ROM
1008 */
1009 if(OF_instance_to_path(stdin, buffer, sizeof(buffer)) <= 0) {
1010 printf("consinit: bogus stdin path.\n");
1011 goto setup_output;
1012 }
1013 cp = buffer;
1014 while (*cp)
1015 cp++;
1016 cp -= 2;
1017 #ifdef DEBUG
1018 if (cp < buffer)
1019 panic("consinit: bad stdin path %s",buffer);
1020 #endif
1021 /* XXX: only allows tty's a->z, assumes PROMDEV_TTYx contig */
1022 if (cp[0]==':' && cp[1] >= 'a' && cp[1] <= 'z')
1023 inSource = PROMDEV_TTYA + (cp[1] - 'a');
1024 /* else use rom */
1025 setup_output:
1026 prom_printf("setting up stdout\r\n");
1027 node = OF_finddevice("/chosen");
1028 OF_getprop(node, "stdout", &stdout, sizeof(stdout));
1029
1030 prom_printf("stdout instance = %x\r\n", stdout);
1031
1032 node = OF_instance_to_package(stdout);
1033 prom_printf("stdout package = %x\r\n", node);
1034 if (strcmp(getpropstring(node,"device_type"),"display") == 0) {
1035 /* frame buffer output */
1036 outSink = PROMDEV_SCREEN;
1037 fbnode = node;
1038 } else if (strcmp(getpropstring(node,"device_type"), "serial")
1039 != 0) {
1040 /* not screen, not serial. Whatzit? */
1041 outSink = -1;
1042 } else { /* serial console. which? */
1043 /*
1044 * At this point we assume the device path is in the
1045 * form:
1046 * ....device@x,y:a for ttya, etc.
1047 * If it isn't, we defer to the ROM
1048 */
1049 if(OF_instance_to_path(stdout, buffer, sizeof(buffer)) <= 0) {
1050 printf("consinit: bogus stdin path.\n");
1051 goto setup_output;
1052 }
1053 cp = buffer;
1054 while (*cp)
1055 cp++;
1056 cp -= 2;
1057 #ifdef DEBUG
1058 if (cp < buffer)
1059 panic("consinit: bad stdout path %s",buffer);
1060 #endif
1061 /* XXX: only allows tty's a->z, assumes PROMDEV_TTYx contig */
1062 if (cp[0]==':' && cp[1] >= 'a' && cp[1] <= 'z')
1063 outSink = PROMDEV_TTYA + (cp[1] - 'a');
1064 else outSink = -1;
1065 }
1066 #if 0
1067 setup_console:
1068 #endif
1069
1070 if (inSource != outSink) {
1071 printf("cninit: mismatched PROM output selector\n");
1072 }
1073
1074 switch (inSource) {
1075 default:
1076 printf("cninit: invalid inSource=%d\n", inSource);
1077 callrom();
1078 inSource = PROMDEV_KBD;
1079 /* fall through */
1080
1081 case 0: /* keyboard/display */
1082 #if NKBD > 0
1083 zs_unit = 1; /* XXX - config info! */
1084 channel = 0;
1085 cn = &consdev_kd;
1086 /* Set cn_dev, cn_pri in kd.c */
1087 break;
1088 #else /* NKBD */
1089 printf("cninit: kdb/display not configured\n");
1090 callrom();
1091 inSource = PROMDEV_TTYA;
1092 /* fall through */
1093 #endif /* NKBD */
1094
1095 case PROMDEV_TTYA:
1096 case PROMDEV_TTYB:
1097 zstty_unit = inSource - PROMDEV_TTYA;
1098 zs_unit = 0; /* XXX - config info! */
1099 channel = zstty_unit & 1;
1100 cn = &consdev_tty;
1101 cn->cn_dev = makedev(zs_major, zstty_unit);
1102 cn->cn_pri = CN_REMOTE;
1103 break;
1104
1105 }
1106 /* Now that inSource has been validated, print it. */
1107 printf("console is %s\n", prom_inSrc_name[inSource]);
1108
1109 zc = zs_get_chan_addr(zs_unit, channel);
1110 if (zc == NULL) {
1111 printf("cninit: zs not mapped.\n");
1112 return;
1113 }
1114 zs_conschan = zc;
1115 zs_hwflags[zs_unit][channel] = ZS_HWFLAG_CONSOLE;
1116 cn_tab = cn;
1117 (*cn->cn_init)(cn);
1118 #ifdef KGDB
1119 zs_kgdb_init();
1120 #endif
1121 }
1122