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