zs.c revision 1.9 1 /* $NetBSD: zs.c,v 1.9 1999/03/27 01:21:36 wrstuden 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 cs->cs_rr0_pps = 0;
631 if ((cflag & (CLOCAL | MDMBUF)) != 0) {
632 cs->cs_rr0_dcd = 0;
633 if ((cflag & MDMBUF) == 0)
634 cs->cs_rr0_pps = ZSRR0_DCD;
635 } else
636 cs->cs_rr0_dcd = ZSRR0_DCD;
637 if ((cflag & CRTSCTS) != 0) {
638 cs->cs_wr5_dtr = ZSWR5_DTR;
639 cs->cs_wr5_rts = ZSWR5_RTS;
640 cs->cs_rr0_cts = ZSRR0_CTS;
641 } else if ((cflag & CDTRCTS) != 0) {
642 cs->cs_wr5_dtr = 0;
643 cs->cs_wr5_rts = ZSWR5_DTR;
644 cs->cs_rr0_cts = ZSRR0_CTS;
645 } else if ((cflag & MDMBUF) != 0) {
646 cs->cs_wr5_dtr = 0;
647 cs->cs_wr5_rts = ZSWR5_DTR;
648 cs->cs_rr0_cts = ZSRR0_DCD;
649 } else {
650 cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
651 cs->cs_wr5_rts = 0;
652 cs->cs_rr0_cts = 0;
653 }
654 splx(s);
655
656 /* Caller will stuff the pending registers. */
657 return (0);
658 }
659
660
661 /*
662 * Read or write the chip with suitable delays.
663 */
664
665 u_char
666 zs_read_reg(cs, reg)
667 struct zs_chanstate *cs;
668 u_char reg;
669 {
670 u_char val;
671
672 *cs->cs_reg_csr = reg;
673 ZS_DELAY();
674 val = *cs->cs_reg_csr;
675 ZS_DELAY();
676 return (val);
677 }
678
679 void
680 zs_write_reg(cs, reg, val)
681 struct zs_chanstate *cs;
682 u_char reg, val;
683 {
684 *cs->cs_reg_csr = reg;
685 ZS_DELAY();
686 *cs->cs_reg_csr = val;
687 ZS_DELAY();
688 }
689
690 u_char
691 zs_read_csr(cs)
692 struct zs_chanstate *cs;
693 {
694 register u_char val;
695
696 val = *cs->cs_reg_csr;
697 ZS_DELAY();
698 return (val);
699 }
700
701 void zs_write_csr(cs, val)
702 struct zs_chanstate *cs;
703 u_char val;
704 {
705 *cs->cs_reg_csr = val;
706 ZS_DELAY();
707 }
708
709 u_char zs_read_data(cs)
710 struct zs_chanstate *cs;
711 {
712 register u_char val;
713
714 val = *cs->cs_reg_data;
715 ZS_DELAY();
716 return (val);
717 }
718
719 void zs_write_data(cs, val)
720 struct zs_chanstate *cs;
721 u_char val;
722 {
723 *cs->cs_reg_data = val;
724 ZS_DELAY();
725 }
726
727 /****************************************************************
728 * Console support functions (Sun specific!)
729 * Note: this code is allowed to know about the layout of
730 * the chip registers, and uses that to keep things simple.
731 * XXX - I think I like the mvme167 code better. -gwr
732 ****************************************************************/
733
734 extern void Debugger __P((void));
735 void *zs_conschan;
736
737 /*
738 * Handle user request to enter kernel debugger.
739 */
740 void
741 zs_abort(cs)
742 struct zs_chanstate *cs;
743 {
744 register volatile struct zschan *zc = zs_conschan;
745 int rr0;
746
747 /* Wait for end of break to avoid PROM abort. */
748 /* XXX - Limit the wait? */
749 do {
750 rr0 = zc->zc_csr;
751 ZS_DELAY();
752 } while (rr0 & ZSRR0_BREAK);
753
754 #if defined(KGDB)
755 zskgdb(cs);
756 #elif defined(DDB)
757 Debugger();
758 #else
759 printf("stopping on keyboard abort\n");
760 callrom();
761 #endif
762 }
763
764 /*
765 * Polled input char.
766 */
767 int
768 zs_getc(arg)
769 void *arg;
770 {
771 register volatile struct zschan *zc = arg;
772 register int s, c, rr0;
773
774 s = splhigh();
775 /* Wait for a character to arrive. */
776 do {
777 rr0 = zc->zc_csr;
778 ZS_DELAY();
779 } while ((rr0 & ZSRR0_RX_READY) == 0);
780
781 c = zc->zc_data;
782 ZS_DELAY();
783 splx(s);
784
785 /*
786 * This is used by the kd driver to read scan codes,
787 * so don't translate '\r' ==> '\n' here...
788 */
789 return (c);
790 }
791
792 /*
793 * Polled output char.
794 */
795 void
796 zs_putc(arg, c)
797 void *arg;
798 int c;
799 {
800 register volatile struct zschan *zc = arg;
801 register int s, rr0;
802
803 s = splhigh();
804
805 /* Wait for transmitter to become ready. */
806 do {
807 rr0 = zc->zc_csr;
808 ZS_DELAY();
809 } while ((rr0 & ZSRR0_TX_READY) == 0);
810
811 /*
812 * Send the next character.
813 * Now you'd think that this could be followed by a ZS_DELAY()
814 * just like all the other chip accesses, but it turns out that
815 * the `transmit-ready' interrupt isn't de-asserted until
816 * some period of time after the register write completes
817 * (more than a couple instructions). So to avoid stray
818 * interrupts we put in the 2us delay regardless of cpu model.
819 */
820 zc->zc_data = c;
821 delay(2);
822
823 splx(s);
824 }
825
826 /*****************************************************************/
827
828 static void zscninit __P((struct consdev *));
829 static int zscngetc __P((dev_t));
830 static void zscnputc __P((dev_t, int));
831 static void zscnpollc __P((dev_t, int));
832 /*
833 * Console table shared by ttya, ttyb
834 */
835 struct consdev consdev_tty = {
836 nullcnprobe,
837 zscninit,
838 zscngetc,
839 zscnputc,
840 zscnpollc,
841 };
842
843 static void
844 zscninit(cn)
845 struct consdev *cn;
846 {
847 }
848
849 /*
850 * Polled console input putchar.
851 */
852 static int
853 zscngetc(dev)
854 dev_t dev;
855 {
856 return (zs_getc(zs_conschan));
857 }
858
859 /*
860 * Polled console output putchar.
861 */
862 static void
863 zscnputc(dev, c)
864 dev_t dev;
865 int c;
866 {
867 zs_putc(zs_conschan, c);
868 }
869
870 int swallow_zsintrs;
871
872 static void
873 zscnpollc(dev, on)
874 dev_t dev;
875 int on;
876 {
877 /*
878 * Need to tell zs driver to acknowledge all interrupts or we get
879 * annoying spurious interrupt messages. This is because mucking
880 * with spl() levels during polling does not prevent interrupts from
881 * being generated.
882 */
883
884 if (on) swallow_zsintrs++;
885 else swallow_zsintrs--;
886 }
887
888 /*****************************************************************/
889
890 static void prom_cninit __P((struct consdev *));
891 static int prom_cngetc __P((dev_t));
892 static void prom_cnputc __P((dev_t, int));
893
894 int stdin = NULL, stdout = NULL;
895
896 /*
897 * The console is set to this one initially,
898 * which lets us use the PROM until consinit()
899 * is called to select a real console.
900 */
901 struct consdev consdev_prom = {
902 nullcnprobe,
903 prom_cninit,
904 prom_cngetc,
905 prom_cnputc,
906 nullcnpollc,
907 };
908
909 /*
910 * The console table pointer is statically initialized
911 * to point to the PROM (output only) table, so that
912 * early calls to printf will work.
913 */
914 struct consdev *cn_tab = &consdev_prom;
915
916 void
917 nullcnprobe(cn)
918 struct consdev *cn;
919 {
920 }
921
922 static void
923 prom_cninit(cn)
924 struct consdev *cn;
925 {
926 }
927
928 /*
929 * PROM console input putchar.
930 * (dummy - this is output only)
931 */
932 static int
933 prom_cngetc(dev)
934 dev_t dev;
935 {
936 int s;
937 char c0;
938
939 if (!stdin) {
940 int node = OF_finddevice("/chosen");
941 OF_getprop(node, "stdin", &stdin, sizeof(stdin));
942 }
943 if (OF_read(stdin, &c0, 1) == 1)
944 return (c0 & 0x7f);
945 return -1;
946 }
947
948 /*
949 * PROM console output putchar.
950 */
951 static void
952 prom_cnputc(dev, c)
953 dev_t dev;
954 int c;
955 {
956 int s;
957 char c0 = (c & 0x7f);
958
959 if (!stdout) {
960 int node = OF_finddevice("/chosen");
961 OF_getprop(node, "stdout", &stdout, sizeof(stdout));
962 }
963
964 s = splhigh();
965 OF_write(stdout, &c0, 1);
966 splx(s);
967 }
968
969 /*****************************************************************/
970
971 extern struct consdev consdev_kd;
972
973 static char *prom_inSrc_name[] = {
974 "keyboard/display",
975 "ttya", "ttyb",
976 "ttyc", "ttyd" };
977
978 #ifdef DEBUG
979 #define DBPRINT(x) printf x
980 #else
981 #define DBPRINT(x)
982 #endif
983
984 /*
985 * This function replaces sys/dev/cninit.c
986 * Determine which device is the console using
987 * the PROM "input source" and "output sink".
988 */
989 void
990 consinit()
991 {
992 struct zschan *zc;
993 struct consdev *cn;
994 int channel, zs_unit, zstty_unit;
995 int inSource, outSink;
996 register int node;
997 char buffer[128];
998 register char *cp;
999 extern int fbnode;
1000
1001 DBPRINT(("consinit()\r\n"));
1002 if (cn_tab != &consdev_prom) return;
1003
1004 inSource = outSink = -1;
1005
1006 DBPRINT(("setting up stdin\r\n"));
1007 node = OF_finddevice("/chosen");
1008 OF_getprop(node, "stdin", &stdin, sizeof(stdin));
1009 DBPRINT(("stdin instance = %x\r\n", stdin));
1010
1011 node = OF_instance_to_package(stdin);
1012 DBPRINT(("stdin package = %x\r\n", node));
1013 if (OF_getproplen(node,"keyboard") >= 0) {
1014 inSource = PROMDEV_KBD;
1015 goto setup_output;
1016 }
1017 if (strcmp(getpropstring(node,"device_type"),"serial") != 0) {
1018 /* not a serial, not keyboard. what is it?!? */
1019 inSource = -1;
1020 goto setup_output;
1021 }
1022 /*
1023 * At this point we assume the device path is in the form
1024 * ....device@x,y:a for ttya and ...device@x,y:b for ttyb.
1025 * If it isn't, we defer to the ROM
1026 */
1027 if(OF_instance_to_path(stdin, buffer, sizeof(buffer)) <= 0) {
1028 printf("consinit: bogus stdin path.\n");
1029 goto setup_output;
1030 }
1031 cp = buffer;
1032 while (*cp)
1033 cp++;
1034 cp -= 2;
1035 #ifdef DEBUG
1036 if (cp < buffer)
1037 panic("consinit: bad stdin path %s",buffer);
1038 #endif
1039 /* XXX: only allows tty's a->z, assumes PROMDEV_TTYx contig */
1040 if (cp[0]==':' && cp[1] >= 'a' && cp[1] <= 'z')
1041 inSource = PROMDEV_TTYA + (cp[1] - 'a');
1042 /* else use rom */
1043 setup_output:
1044 DBPRINT(("setting up stdout\r\n"));
1045 node = OF_finddevice("/chosen");
1046 OF_getprop(node, "stdout", &stdout, sizeof(stdout));
1047
1048 DBPRINT(("stdout instance = %x\r\n", stdout));
1049
1050 node = OF_instance_to_package(stdout);
1051 DBPRINT(("stdout package = %x\r\n", node));
1052 if (strcmp(getpropstring(node,"device_type"),"display") == 0) {
1053 /* frame buffer output */
1054 outSink = PROMDEV_SCREEN;
1055 fbnode = node;
1056 } else if (strcmp(getpropstring(node,"device_type"), "serial")
1057 != 0) {
1058 /* not screen, not serial. Whatzit? */
1059 outSink = -1;
1060 } else { /* serial console. which? */
1061 /*
1062 * At this point we assume the device path is in the
1063 * form:
1064 * ....device@x,y:a for ttya, etc.
1065 * If it isn't, we defer to the ROM
1066 */
1067 if(OF_instance_to_path(stdout, buffer, sizeof(buffer)) <= 0) {
1068 printf("consinit: bogus stdin path.\n");
1069 goto setup_output;
1070 }
1071 cp = buffer;
1072 while (*cp)
1073 cp++;
1074 cp -= 2;
1075 #ifdef DEBUG
1076 if (cp < buffer)
1077 panic("consinit: bad stdout path %s",buffer);
1078 #endif
1079 /* XXX: only allows tty's a->z, assumes PROMDEV_TTYx contig */
1080 if (cp[0]==':' && cp[1] >= 'a' && cp[1] <= 'z')
1081 outSink = PROMDEV_TTYA + (cp[1] - 'a');
1082 else outSink = -1;
1083 }
1084 if (inSource != outSink) {
1085 printf("cninit: mismatched PROM output selector\n");
1086 }
1087
1088 switch (inSource) {
1089 default:
1090 printf("cninit: invalid inSource=%d\n", inSource);
1091 callrom();
1092 inSource = PROMDEV_KBD;
1093 /* fall through */
1094
1095 case 0: /* keyboard/display */
1096 #if NKBD > 0
1097 zs_unit = 1; /* XXX - config info! */
1098 channel = 0;
1099 cn = &consdev_kd;
1100 /* Set cn_dev, cn_pri in kd.c */
1101 break;
1102 #else /* NKBD */
1103 printf("cninit: kdb/display not configured\n");
1104 callrom();
1105 inSource = PROMDEV_TTYA;
1106 /* fall through */
1107 #endif /* NKBD */
1108
1109 case PROMDEV_TTYA:
1110 case PROMDEV_TTYB:
1111 zstty_unit = inSource - PROMDEV_TTYA;
1112 zs_unit = 0; /* XXX - config info! */
1113 channel = zstty_unit & 1;
1114 cn = &consdev_tty;
1115 cn->cn_dev = makedev(zs_major, zstty_unit);
1116 cn->cn_pri = CN_REMOTE;
1117 break;
1118
1119 }
1120 /* Now that inSource has been validated, print it. */
1121 printf("console is %s\n", prom_inSrc_name[inSource]);
1122
1123 zc = zs_get_chan_addr(zs_unit, channel);
1124 if (zc == NULL) {
1125 printf("cninit: zs not mapped.\n");
1126 return;
1127 }
1128 zs_conschan = zc;
1129 zs_hwflags[zs_unit][channel] = ZS_HWFLAG_CONSOLE;
1130 cn_tab = cn;
1131 (*cn->cn_init)(cn);
1132 #ifdef KGDB
1133 zs_kgdb_init();
1134 #endif
1135 }
1136