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