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