zs.c revision 1.88 1 /* $NetBSD: zs.c,v 1.88 2002/09/27 02:24:24 thorpej 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 #include "opt_kgdb.h"
49 #include "opt_sparc_arch.h"
50
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/conf.h>
54 #include <sys/device.h>
55 #include <sys/file.h>
56 #include <sys/ioctl.h>
57 #include <sys/kernel.h>
58 #include <sys/proc.h>
59 #include <sys/tty.h>
60 #include <sys/time.h>
61 #include <sys/syslog.h>
62
63 #include <machine/bsd_openprom.h>
64 #include <machine/autoconf.h>
65 #include <machine/intr.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 <sparc/sparc/vaddrs.h>
74 #include <sparc/sparc/auxreg.h>
75 #include <sparc/sparc/auxiotwo.h>
76 #include <sparc/dev/cons.h>
77
78 #include "kbd.h" /* NKBD */
79 #include "zs.h" /* NZS */
80
81 /* Make life easier for the initialized arrays here. */
82 #if NZS < 3
83 #undef NZS
84 #define NZS 3
85 #endif
86
87 /*
88 * Some warts needed by z8530tty.c -
89 * The default parity REALLY needs to be the same as the PROM uses,
90 * or you can not see messages done with printf during boot-up...
91 */
92 int zs_def_cflag = (CREAD | CS8 | HUPCL);
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 /* ZS channel used as the console device (if any) */
128 void *zs_conschan_get, *zs_conschan_put;
129
130 static u_char zs_init_reg[16] = {
131 0, /* 0: CMD (reset, etc.) */
132 0, /* 1: No interrupts yet. */
133 0, /* 2: IVECT */
134 ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
135 ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
136 ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
137 0, /* 6: TXSYNC/SYNCLO */
138 0, /* 7: RXSYNC/SYNCHI */
139 0, /* 8: alias for data port */
140 ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR,
141 0, /*10: Misc. TX/RX control bits */
142 ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
143 ((PCLK/32)/9600)-2, /*12: BAUDLO (default=9600) */
144 0, /*13: BAUDHI (default=9600) */
145 ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
146 ZSWR15_BREAK_IE,
147 };
148
149 /* Console ops */
150 static int zscngetc __P((dev_t));
151 static void zscnputc __P((dev_t, int));
152 static void zscnpollc __P((dev_t, int));
153
154 struct consdev zs_consdev = {
155 NULL,
156 NULL,
157 zscngetc,
158 zscnputc,
159 zscnpollc,
160 NULL,
161 };
162
163
164 /****************************************************************
165 * Autoconfig
166 ****************************************************************/
167
168 /* Definition of the driver for autoconfig. */
169 static int zs_match_mainbus __P((struct device *, struct cfdata *, void *));
170 static int zs_match_obio __P((struct device *, struct cfdata *, void *));
171 static void zs_attach_mainbus __P((struct device *, struct device *, void *));
172 static void zs_attach_obio __P((struct device *, struct device *, void *));
173
174 #if defined(SUN4D)
175 #include <sparc/dev/bootbusvar.h>
176
177 static int zs_match_bootbus __P((struct device *, struct cfdata *, void *));
178 static void zs_attach_bootbus __P((struct device *, struct device *, void *));
179
180 struct cfattach zs_bootbus_ca = {
181 sizeof(struct zsc_softc), zs_match_bootbus, zs_attach_bootbus
182 };
183 #endif /* SUN4D */
184
185 static void zs_attach __P((struct zsc_softc *, struct zsdevice *, int));
186 static int zs_print __P((void *, const char *name));
187
188 struct cfattach zs_mainbus_ca = {
189 sizeof(struct zsc_softc), zs_match_mainbus, zs_attach_mainbus
190 };
191
192 struct cfattach zs_obio_ca = {
193 sizeof(struct zsc_softc), zs_match_obio, zs_attach_obio
194 };
195
196 extern struct cfdriver zs_cd;
197
198 /* Interrupt handlers. */
199 static int zshard __P((void *));
200 static int zssoft __P((void *));
201
202 static int zs_get_speed __P((struct zs_chanstate *));
203
204 /* Console device support */
205 static int zs_console_flags __P((int, int, int));
206
207 /* Power management hooks */
208 int zs_enable __P((struct zs_chanstate *));
209 void zs_disable __P((struct zs_chanstate *));
210
211
212 /*
213 * Is the zs chip present?
214 */
215 static int
216 zs_match_mainbus(parent, cf, aux)
217 struct device *parent;
218 struct cfdata *cf;
219 void *aux;
220 {
221 struct mainbus_attach_args *ma = aux;
222
223 if (strcmp(cf->cf_name, ma->ma_name) != 0)
224 return (0);
225
226 return (1);
227 }
228
229 static int
230 zs_match_obio(parent, cf, aux)
231 struct device *parent;
232 struct cfdata *cf;
233 void *aux;
234 {
235 union obio_attach_args *uoba = aux;
236 struct obio4_attach_args *oba;
237
238 if (uoba->uoba_isobio4 == 0) {
239 struct sbus_attach_args *sa = &uoba->uoba_sbus;
240
241 if (strcmp(cf->cf_name, sa->sa_name) != 0)
242 return (0);
243
244 return (1);
245 }
246
247 oba = &uoba->uoba_oba4;
248 return (bus_space_probe(oba->oba_bustag, oba->oba_paddr,
249 1, 0, 0, NULL, NULL));
250 }
251
252 #if defined(SUN4D)
253 static int
254 zs_match_bootbus(parent, cf, aux)
255 struct device *parent;
256 struct cfdata *cf;
257 void *aux;
258 {
259 struct bootbus_attach_args *baa = aux;
260
261 return (strcmp(cf->cf_name, baa->ba_name) == 0);
262 }
263 #endif /* SUN4D */
264
265 static void
266 zs_attach_mainbus(parent, self, aux)
267 struct device *parent;
268 struct device *self;
269 void *aux;
270 {
271 struct zsc_softc *zsc = (void *) self;
272 struct mainbus_attach_args *ma = aux;
273
274 zsc->zsc_bustag = ma->ma_bustag;
275 zsc->zsc_dmatag = ma->ma_dmatag;
276 zsc->zsc_promunit = PROM_getpropint(ma->ma_node, "slave", -2);
277 zsc->zsc_node = ma->ma_node;
278
279 /*
280 * For machines with zs on mainbus (all sun4c models), we expect
281 * the device registers to be mapped by the PROM.
282 */
283 zs_attach(zsc, ma->ma_promvaddr, ma->ma_pri);
284 }
285
286 static void
287 zs_attach_obio(parent, self, aux)
288 struct device *parent;
289 struct device *self;
290 void *aux;
291 {
292 struct zsc_softc *zsc = (void *) self;
293 union obio_attach_args *uoba = aux;
294
295 if (uoba->uoba_isobio4 == 0) {
296 struct sbus_attach_args *sa = &uoba->uoba_sbus;
297 void *va;
298 struct zs_chanstate *cs;
299 int channel;
300
301 if (sa->sa_nintr == 0) {
302 printf(" no interrupt lines\n");
303 return;
304 }
305
306 /*
307 * Some sun4m models (Javastations) may not map the zs device.
308 */
309 if (sa->sa_npromvaddrs > 0)
310 va = (void *)sa->sa_promvaddr;
311 else {
312 bus_space_handle_t bh;
313
314 if (sbus_bus_map(sa->sa_bustag,
315 sa->sa_slot,
316 sa->sa_offset,
317 sa->sa_size,
318 BUS_SPACE_MAP_LINEAR, &bh) != 0) {
319 printf(" cannot map zs registers\n");
320 return;
321 }
322 va = (void *)bh;
323 }
324
325 /*
326 * Check if power state can be set, e.g. Tadpole 3GX
327 */
328 if (PROM_getpropint(sa->sa_node, "pwr-on-auxio2", 0))
329 {
330 printf (" powered via auxio2");
331 for (channel = 0; channel < 2; channel++) {
332 cs = &zsc->zsc_cs_store[channel];
333 cs->enable = zs_enable;
334 cs->disable = zs_disable;
335 }
336 }
337
338 zsc->zsc_bustag = sa->sa_bustag;
339 zsc->zsc_dmatag = sa->sa_dmatag;
340 zsc->zsc_promunit = PROM_getpropint(sa->sa_node, "slave", -2);
341 zsc->zsc_node = sa->sa_node;
342 zs_attach(zsc, va, sa->sa_pri);
343 } else {
344 struct obio4_attach_args *oba = &uoba->uoba_oba4;
345 bus_space_handle_t bh;
346 bus_addr_t paddr = oba->oba_paddr;
347
348 /*
349 * As for zs on mainbus, we require a PROM mapping.
350 */
351 if (bus_space_map(oba->oba_bustag,
352 paddr,
353 sizeof(struct zsdevice),
354 BUS_SPACE_MAP_LINEAR | OBIO_BUS_MAP_USE_ROM,
355 &bh) != 0) {
356 printf(" cannot map zs registers\n");
357 return;
358 }
359 zsc->zsc_bustag = oba->oba_bustag;
360 zsc->zsc_dmatag = oba->oba_dmatag;
361 /* Find prom unit by physical address */
362 if (cpuinfo.cpu_type == CPUTYP_4_100)
363 /*
364 * On the sun4/100, the top-most 4 bits are zero
365 * on obio addresses; force them to 1's for the
366 * sake of the comparison here.
367 */
368 paddr |= 0xf0000000;
369 zsc->zsc_promunit =
370 (paddr == 0xf1000000) ? 0 :
371 (paddr == 0xf0000000) ? 1 :
372 (paddr == 0xe0000000) ? 2 : -2;
373
374 zs_attach(zsc, (void *)bh, oba->oba_pri);
375 }
376 }
377
378 #if defined(SUN4D)
379 static void
380 zs_attach_bootbus(parent, self, aux)
381 struct device *parent;
382 struct device *self;
383 void *aux;
384 {
385 struct zsc_softc *zsc = (void *) self;
386 struct bootbus_attach_args *baa = aux;
387 void *va;
388
389 if (baa->ba_nintr == 0) {
390 printf(": no interrupt lines\n");
391 return;
392 }
393
394 if (baa->ba_npromvaddrs > 0)
395 va = (void *) baa->ba_promvaddrs;
396 else {
397 bus_space_handle_t bh;
398
399 if (bus_space_map(baa->ba_bustag,
400 BUS_ADDR(baa->ba_slot, baa->ba_offset),
401 baa->ba_size, BUS_SPACE_MAP_LINEAR, &bh) != 0) {
402 printf(": cannot map zs registers\n");
403 return;
404 }
405 va = (void *) bh;
406 }
407
408 zsc->zsc_bustag = baa->ba_bustag;
409 zsc->zsc_promunit = PROM_getpropint(baa->ba_node, "slave", -2);
410 zsc->zsc_node = baa->ba_node;
411 zs_attach(zsc, va, baa->ba_intr[0].oi_pri);
412 }
413 #endif /* SUN4D */
414
415 /*
416 * Attach a found zs.
417 *
418 * USE ROM PROPERTIES port-a-ignore-cd AND port-b-ignore-cd FOR
419 * SOFT CARRIER, AND keyboard PROPERTY FOR KEYBOARD/MOUSE?
420 */
421 static void
422 zs_attach(zsc, zsd, pri)
423 struct zsc_softc *zsc;
424 struct zsdevice *zsd;
425 int pri;
426 {
427 struct zsc_attach_args zsc_args;
428 struct zs_chanstate *cs;
429 int s, channel;
430 static int didintr, prevpri;
431
432 if (zsd == NULL) {
433 printf("configuration incomplete\n");
434 return;
435 }
436
437 printf(" softpri %d\n", PIL_TTY);
438
439 /*
440 * Initialize software state for each channel.
441 */
442 for (channel = 0; channel < 2; channel++) {
443 struct zschan *zc;
444
445 zsc_args.channel = channel;
446 cs = &zsc->zsc_cs_store[channel];
447 zsc->zsc_cs[channel] = cs;
448
449 cs->cs_channel = channel;
450 cs->cs_private = NULL;
451 cs->cs_ops = &zsops_null;
452 cs->cs_brg_clk = PCLK / 16;
453
454 zc = (channel == 0) ? &zsd->zs_chan_a : &zsd->zs_chan_b;
455
456 zsc_args.hwflags = zs_console_flags(zsc->zsc_promunit,
457 zsc->zsc_node,
458 channel);
459
460 if (zsc_args.hwflags & ZS_HWFLAG_CONSOLE) {
461 zsc_args.hwflags |= ZS_HWFLAG_USE_CONSDEV;
462 zsc_args.consdev = &zs_consdev;
463 }
464
465 if ((zsc_args.hwflags & ZS_HWFLAG_CONSOLE_INPUT) != 0) {
466 zs_conschan_get = zc;
467 }
468 if ((zsc_args.hwflags & ZS_HWFLAG_CONSOLE_OUTPUT) != 0) {
469 zs_conschan_put = zc;
470 }
471 /* Childs need to set cn_dev, etc */
472
473 cs->cs_reg_csr = &zc->zc_csr;
474 cs->cs_reg_data = &zc->zc_data;
475
476 bcopy(zs_init_reg, cs->cs_creg, 16);
477 bcopy(zs_init_reg, cs->cs_preg, 16);
478
479 /* XXX: Consult PROM properties for this?! */
480 cs->cs_defspeed = zs_get_speed(cs);
481 cs->cs_defcflag = zs_def_cflag;
482
483 /* Make these correspond to cs_defcflag (-crtscts) */
484 cs->cs_rr0_dcd = ZSRR0_DCD;
485 cs->cs_rr0_cts = 0;
486 cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
487 cs->cs_wr5_rts = 0;
488
489 /*
490 * Clear the master interrupt enable.
491 * The INTENA is common to both channels,
492 * so just do it on the A channel.
493 */
494 if (channel == 0) {
495 zs_write_reg(cs, 9, 0);
496 }
497
498 /*
499 * Look for a child driver for this channel.
500 * The child attach will setup the hardware.
501 */
502 if (!config_found(&zsc->zsc_dev, (void *)&zsc_args, zs_print)) {
503 /* No sub-driver. Just reset it. */
504 u_char reset = (channel == 0) ?
505 ZSWR9_A_RESET : ZSWR9_B_RESET;
506 s = splzs();
507 zs_write_reg(cs, 9, reset);
508 splx(s);
509 }
510 }
511
512 /*
513 * Now safe to install interrupt handlers. Note the arguments
514 * to the interrupt handlers aren't used. Note, we only do this
515 * once since both SCCs interrupt at the same level and vector.
516 */
517 if (!didintr) {
518 didintr = 1;
519 prevpri = pri;
520 bus_intr_establish(zsc->zsc_bustag, pri, IPL_SERIAL, 0,
521 zshard, NULL);
522 bus_intr_establish(zsc->zsc_bustag, PIL_TTY,
523 IPL_SOFTSERIAL,
524 BUS_INTR_ESTABLISH_SOFTINTR,
525 zssoft, NULL);
526 } else if (pri != prevpri)
527 panic("broken zs interrupt scheme");
528
529 evcnt_attach_dynamic(&zsc->zsc_intrcnt, EVCNT_TYPE_INTR, NULL,
530 zsc->zsc_dev.dv_xname, "intr");
531
532 /*
533 * Set the master interrupt enable and interrupt vector.
534 * (common to both channels, do it on A)
535 */
536 cs = zsc->zsc_cs[0];
537 s = splhigh();
538 /* interrupt vector */
539 zs_write_reg(cs, 2, zs_init_reg[2]);
540 /* master interrupt control (enable) */
541 zs_write_reg(cs, 9, zs_init_reg[9]);
542 splx(s);
543
544 #if 0
545 /*
546 * XXX: L1A hack - We would like to be able to break into
547 * the debugger during the rest of autoconfiguration, so
548 * lower interrupts just enough to let zs interrupts in.
549 * This is done after both zs devices are attached.
550 */
551 if (zsc->zsc_promunit == 1) {
552 printf("zs1: enabling zs interrupts\n");
553 (void)splfd(); /* XXX: splzs - 1 */
554 }
555 #endif
556 }
557
558 static int
559 zs_print(aux, name)
560 void *aux;
561 const char *name;
562 {
563 struct zsc_attach_args *args = aux;
564
565 if (name != NULL)
566 printf("%s: ", name);
567
568 if (args->channel != -1)
569 printf(" channel %d", args->channel);
570
571 return (UNCONF);
572 }
573
574 static volatile int zssoftpending;
575
576 /*
577 * Our ZS chips all share a common, autovectored interrupt,
578 * so we have to look at all of them on each interrupt.
579 */
580 static int
581 zshard(arg)
582 void *arg;
583 {
584 struct zsc_softc *zsc;
585 int unit, rr3, rval, softreq;
586
587 rval = softreq = 0;
588 for (unit = 0; unit < zs_cd.cd_ndevs; unit++) {
589 struct zs_chanstate *cs;
590
591 zsc = zs_cd.cd_devs[unit];
592 if (zsc == NULL)
593 continue;
594 rr3 = zsc_intr_hard(zsc);
595 /* Count up the interrupts. */
596 if (rr3) {
597 rval |= rr3;
598 zsc->zsc_intrcnt.ev_count++;
599 }
600 if ((cs = zsc->zsc_cs[0]) != NULL)
601 softreq |= cs->cs_softreq;
602 if ((cs = zsc->zsc_cs[1]) != NULL)
603 softreq |= cs->cs_softreq;
604 }
605
606 /* We are at splzs here, so no need to lock. */
607 if (softreq && (zssoftpending == 0)) {
608 zssoftpending = IE_ZSSOFT;
609 #if defined(SUN4M)
610 if (CPU_ISSUN4M)
611 raise(0, PIL_TTY);
612 else
613 #endif
614 ienab_bis(IE_ZSSOFT);
615 }
616 return (rval);
617 }
618
619 /*
620 * Similar scheme as for zshard (look at all of them)
621 */
622 static int
623 zssoft(arg)
624 void *arg;
625 {
626 struct zsc_softc *zsc;
627 int s, unit;
628
629 /* This is not the only ISR on this IPL. */
630 if (zssoftpending == 0)
631 return (0);
632
633 /*
634 * The soft intr. bit will be set by zshard only if
635 * the variable zssoftpending is zero. The order of
636 * these next two statements prevents our clearing
637 * the soft intr bit just after zshard has set it.
638 */
639 /* ienab_bic(IE_ZSSOFT); */
640 zssoftpending = 0;
641
642 /* Make sure we call the tty layer at spltty. */
643 s = spltty();
644 for (unit = 0; unit < zs_cd.cd_ndevs; unit++) {
645 zsc = zs_cd.cd_devs[unit];
646 if (zsc == NULL)
647 continue;
648 (void)zsc_intr_soft(zsc);
649 }
650 splx(s);
651 return (1);
652 }
653
654
655 /*
656 * Compute the current baud rate given a ZS channel.
657 */
658 static int
659 zs_get_speed(cs)
660 struct zs_chanstate *cs;
661 {
662 int tconst;
663
664 tconst = zs_read_reg(cs, 12);
665 tconst |= zs_read_reg(cs, 13) << 8;
666 return (TCONST_TO_BPS(cs->cs_brg_clk, tconst));
667 }
668
669 /*
670 * MD functions for setting the baud rate and control modes.
671 */
672 int
673 zs_set_speed(cs, bps)
674 struct zs_chanstate *cs;
675 int bps; /* bits per second */
676 {
677 int tconst, real_bps;
678
679 if (bps == 0)
680 return (0);
681
682 #ifdef DIAGNOSTIC
683 if (cs->cs_brg_clk == 0)
684 panic("zs_set_speed");
685 #endif
686
687 tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
688 if (tconst < 0)
689 return (EINVAL);
690
691 /* Convert back to make sure we can do it. */
692 real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
693
694 /* XXX - Allow some tolerance here? */
695 if (real_bps != bps)
696 return (EINVAL);
697
698 cs->cs_preg[12] = tconst;
699 cs->cs_preg[13] = tconst >> 8;
700
701 /* Caller will stuff the pending registers. */
702 return (0);
703 }
704
705 int
706 zs_set_modes(cs, cflag)
707 struct zs_chanstate *cs;
708 int cflag; /* bits per second */
709 {
710 int s;
711
712 /*
713 * Output hardware flow control on the chip is horrendous:
714 * if carrier detect drops, the receiver is disabled, and if
715 * CTS drops, the transmitter is stoped IN MID CHARACTER!
716 * Therefore, NEVER set the HFC bit, and instead use the
717 * status interrupt to detect CTS changes.
718 */
719 s = splzs();
720 cs->cs_rr0_pps = 0;
721 if ((cflag & (CLOCAL | MDMBUF)) != 0) {
722 cs->cs_rr0_dcd = 0;
723 if ((cflag & MDMBUF) == 0)
724 cs->cs_rr0_pps = ZSRR0_DCD;
725 } else
726 cs->cs_rr0_dcd = ZSRR0_DCD;
727 if ((cflag & CRTSCTS) != 0) {
728 cs->cs_wr5_dtr = ZSWR5_DTR;
729 cs->cs_wr5_rts = ZSWR5_RTS;
730 cs->cs_rr0_cts = ZSRR0_CTS;
731 } else if ((cflag & CDTRCTS) != 0) {
732 cs->cs_wr5_dtr = 0;
733 cs->cs_wr5_rts = ZSWR5_DTR;
734 cs->cs_rr0_cts = ZSRR0_CTS;
735 } else if ((cflag & MDMBUF) != 0) {
736 cs->cs_wr5_dtr = 0;
737 cs->cs_wr5_rts = ZSWR5_DTR;
738 cs->cs_rr0_cts = ZSRR0_DCD;
739 } else {
740 cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
741 cs->cs_wr5_rts = 0;
742 cs->cs_rr0_cts = 0;
743 }
744 splx(s);
745
746 /* Caller will stuff the pending registers. */
747 return (0);
748 }
749
750
751 /*
752 * Read or write the chip with suitable delays.
753 */
754
755 u_char
756 zs_read_reg(cs, reg)
757 struct zs_chanstate *cs;
758 u_char reg;
759 {
760 u_char val;
761
762 *cs->cs_reg_csr = reg;
763 ZS_DELAY();
764 val = *cs->cs_reg_csr;
765 ZS_DELAY();
766 return (val);
767 }
768
769 void
770 zs_write_reg(cs, reg, val)
771 struct zs_chanstate *cs;
772 u_char reg, val;
773 {
774 *cs->cs_reg_csr = reg;
775 ZS_DELAY();
776 *cs->cs_reg_csr = val;
777 ZS_DELAY();
778 }
779
780 u_char
781 zs_read_csr(cs)
782 struct zs_chanstate *cs;
783 {
784 u_char val;
785
786 val = *cs->cs_reg_csr;
787 ZS_DELAY();
788 return (val);
789 }
790
791 void
792 zs_write_csr(cs, val)
793 struct zs_chanstate *cs;
794 u_char val;
795 {
796 *cs->cs_reg_csr = val;
797 ZS_DELAY();
798 }
799
800 u_char
801 zs_read_data(cs)
802 struct zs_chanstate *cs;
803 {
804 u_char val;
805
806 val = *cs->cs_reg_data;
807 ZS_DELAY();
808 return (val);
809 }
810
811 void zs_write_data(cs, val)
812 struct zs_chanstate *cs;
813 u_char val;
814 {
815 *cs->cs_reg_data = val;
816 ZS_DELAY();
817 }
818
819 /****************************************************************
820 * Console support functions (Sun specific!)
821 * Note: this code is allowed to know about the layout of
822 * the chip registers, and uses that to keep things simple.
823 * XXX - I think I like the mvme167 code better. -gwr
824 ****************************************************************/
825
826 /*
827 * Handle user request to enter kernel debugger.
828 */
829 void
830 zs_abort(cs)
831 struct zs_chanstate *cs;
832 {
833 struct zschan *zc = zs_conschan_get;
834 int rr0;
835
836 /* Wait for end of break to avoid PROM abort. */
837 /* XXX - Limit the wait? */
838 do {
839 rr0 = zc->zc_csr;
840 ZS_DELAY();
841 } while (rr0 & ZSRR0_BREAK);
842
843 #if defined(KGDB)
844 zskgdb(cs);
845 #elif defined(DDB)
846 Debugger();
847 #else
848 printf("stopping on keyboard abort\n");
849 callrom();
850 #endif
851 }
852
853 int zs_getc __P((void *arg));
854 void zs_putc __P((void *arg, int c));
855
856 /*
857 * Polled input char.
858 */
859 int
860 zs_getc(arg)
861 void *arg;
862 {
863 struct zschan *zc = arg;
864 int s, c, rr0;
865
866 s = splhigh();
867 /* Wait for a character to arrive. */
868 do {
869 rr0 = zc->zc_csr;
870 ZS_DELAY();
871 } while ((rr0 & ZSRR0_RX_READY) == 0);
872
873 c = zc->zc_data;
874 ZS_DELAY();
875 splx(s);
876
877 /*
878 * This is used by the kd driver to read scan codes,
879 * so don't translate '\r' ==> '\n' here...
880 */
881 return (c);
882 }
883
884 /*
885 * Polled output char.
886 */
887 void
888 zs_putc(arg, c)
889 void *arg;
890 int c;
891 {
892 struct zschan *zc = arg;
893 int s, rr0;
894
895 s = splhigh();
896
897 /* Wait for transmitter to become ready. */
898 do {
899 rr0 = zc->zc_csr;
900 ZS_DELAY();
901 } while ((rr0 & ZSRR0_TX_READY) == 0);
902
903 /*
904 * Send the next character.
905 * Now you'd think that this could be followed by a ZS_DELAY()
906 * just like all the other chip accesses, but it turns out that
907 * the `transmit-ready' interrupt isn't de-asserted until
908 * some period of time after the register write completes
909 * (more than a couple instructions). So to avoid stray
910 * interrupts we put in the 2us delay regardless of cpu model.
911 */
912 zc->zc_data = c;
913 delay(2);
914
915 splx(s);
916 }
917
918 /*****************************************************************/
919 /*
920 * Polled console input putchar.
921 */
922 int
923 zscngetc(dev)
924 dev_t dev;
925 {
926 return (zs_getc(zs_conschan_get));
927 }
928
929 /*
930 * Polled console output putchar.
931 */
932 void
933 zscnputc(dev, c)
934 dev_t dev;
935 int c;
936 {
937 zs_putc(zs_conschan_put, c);
938 }
939
940 void
941 zscnpollc(dev, on)
942 dev_t dev;
943 int on;
944 {
945 /* No action needed */
946 }
947
948 int
949 zs_console_flags(promunit, node, channel)
950 int promunit;
951 int node;
952 int channel;
953 {
954 int cookie, flags = 0;
955
956 switch (prom_version()) {
957 case PROM_OLDMON:
958 case PROM_OBP_V0:
959 /*
960 * Use `promunit' and `channel' to derive the PROM
961 * stdio handles that correspond to this device.
962 */
963 if (promunit == 0)
964 cookie = PROMDEV_TTYA + channel;
965 else if (promunit == 1 && channel == 0)
966 cookie = PROMDEV_KBD;
967 else
968 cookie = -1;
969
970 if (cookie == prom_stdin())
971 flags |= ZS_HWFLAG_CONSOLE_INPUT;
972
973 /*
974 * Prevent the keyboard from matching the output device
975 * (note that PROMDEV_KBD == PROMDEV_SCREEN == 0!).
976 */
977 if (cookie != PROMDEV_KBD && cookie == prom_stdout())
978 flags |= ZS_HWFLAG_CONSOLE_OUTPUT;
979
980 break;
981
982 case PROM_OBP_V2:
983 case PROM_OBP_V3:
984 case PROM_OPENFIRM:
985
986 /*
987 * Match the nodes and device arguments prepared by
988 * consinit() against our device node and channel.
989 * (The device argument is the part of the OBP path
990 * following the colon, as in `/obio/zs@0,100000:a')
991 */
992
993 /* Default to channel 0 if there are no explicit prom args */
994 cookie = 0;
995
996 if (node == prom_stdin_node) {
997 if (prom_stdin_args[0] != '\0')
998 /* Translate (a,b) -> (0,1) */
999 cookie = prom_stdin_args[0] - 'a';
1000
1001 if (channel == cookie)
1002 flags |= ZS_HWFLAG_CONSOLE_INPUT;
1003 }
1004
1005 if (node == prom_stdout_node) {
1006 if (prom_stdout_args[0] != '\0')
1007 /* Translate (a,b) -> (0,1) */
1008 cookie = prom_stdout_args[0] - 'a';
1009
1010 if (channel == cookie)
1011 flags |= ZS_HWFLAG_CONSOLE_OUTPUT;
1012 }
1013
1014 break;
1015
1016 default:
1017 break;
1018 }
1019
1020 return (flags);
1021 }
1022
1023 /*
1024 * Power management hooks for zsopen() and zsclose().
1025 * We use them to power on/off the ports, if necessary.
1026 */
1027 int
1028 zs_enable(cs)
1029 struct zs_chanstate *cs;
1030 {
1031 auxiotwoserialendis (ZS_ENABLE);
1032 cs->enabled = 1;
1033 return(0);
1034 }
1035
1036 void
1037 zs_disable(cs)
1038 struct zs_chanstate *cs;
1039 {
1040 auxiotwoserialendis (ZS_DISABLE);
1041 cs->enabled = 0;
1042 }
1043