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