zs_ioasic.c revision 1.41.10.1 1 /* $NetBSD: zs_ioasic.c,v 1.41.10.1 2021/03/22 02:01:02 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1996, 1998 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, Ken Hornstein, and by Jason R. Thorpe of the
9 * Numerical Aerospace Simulation Facility, NASA Ames Research Center.
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 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Zilog Z8530 Dual UART driver (machine-dependent part). This driver
35 * handles Z8530 chips attached to the DECstation/Alpha IOASIC. Modified
36 * for NetBSD/alpha by Ken Hornstein and Jason R. Thorpe. NetBSD/pmax
37 * adaption by Mattias Drochner. Merge work by Tohru Nishimura.
38 *
39 * Runs two serial lines per chip using slave drivers.
40 * Plain tty/async lines use the zstty slave.
41 */
42
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: zs_ioasic.c,v 1.41.10.1 2021/03/22 02:01:02 thorpej Exp $");
45
46 #include "opt_ddb.h"
47 #include "opt_kgdb.h"
48 #include "zskbd.h"
49
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/conf.h>
53 #include <sys/device.h>
54 #include <sys/malloc.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 #include <sys/intr.h>
63
64 #include <machine/autoconf.h>
65 #include <machine/z8530var.h>
66
67 #include <dev/cons.h>
68 #include <dev/ic/z8530reg.h>
69
70 #include <dev/tc/tcvar.h>
71 #include <dev/tc/ioasicreg.h>
72 #include <dev/tc/ioasicvar.h>
73
74 #include <dev/tc/zs_ioasicvar.h>
75
76 #if defined(__alpha__) || defined(alpha)
77 #include <machine/rpb.h>
78 #endif
79 #if defined(pmax)
80 #include <pmax/pmax/pmaxtype.h>
81 #endif
82
83 /*
84 * Helpers for console support.
85 */
86 static void zs_ioasic_cninit(tc_addr_t, tc_offset_t, int);
87 static int zs_ioasic_cngetc(dev_t);
88 static void zs_ioasic_cnputc(dev_t, int);
89 static void zs_ioasic_cnpollc(dev_t, int);
90
91 struct consdev zs_ioasic_cons = {
92 NULL, NULL, zs_ioasic_cngetc, zs_ioasic_cnputc,
93 zs_ioasic_cnpollc, NULL, NULL, NULL, NODEV, CN_NORMAL,
94 };
95
96 static tc_offset_t zs_ioasic_console_offset;
97 static int zs_ioasic_console_channel;
98 static int zs_ioasic_console;
99 static struct zs_chanstate zs_ioasic_conschanstate_store;
100
101 static int zs_ioasic_isconsole(tc_offset_t, int);
102 static void zs_putc(struct zs_chanstate *, int);
103
104 /*
105 * Some warts needed by z8530tty.c
106 */
107 int zs_def_cflag = (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8;
108
109 /*
110 * ZS chips are feeded a 7.372 MHz clock.
111 */
112 #define PCLK (9600 * 768) /* PCLK pin input clock rate */
113
114 /* The layout of this is hardware-dependent (padding, order). */
115 struct zshan {
116 #if defined(__alpha__) || defined(alpha)
117 volatile u_int zc_csr; /* ctrl,status, and indirect access */
118 u_int zc_pad0;
119 volatile u_int zc_data; /* data */
120 u_int sc_pad1;
121 #endif
122 #if defined(pmax)
123 volatile uint16_t zc_csr; /* ctrl,status, and indirect access */
124 unsigned : 16;
125 volatile uint16_t zc_data; /* data */
126 unsigned : 16;
127 #endif
128 };
129
130 struct zsdevice {
131 /* Yes, they are backwards. */
132 struct zshan zs_chan_b;
133 struct zshan zs_chan_a;
134 };
135
136 static const u_char zs_ioasic_init_reg[16] = {
137 0, /* 0: CMD (reset, etc.) */
138 0, /* 1: No interrupts yet. */
139 0xf0, /* 2: IVECT */
140 ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
141 ZSWR4_CLK_X16 | ZSWR4_ONESB,
142 ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
143 0, /* 6: TXSYNC/SYNCLO */
144 0, /* 7: RXSYNC/SYNCHI */
145 0, /* 8: alias for data port */
146 ZSWR9_MASTER_IE | ZSWR9_VECTOR_INCL_STAT,
147 0, /*10: Misc. TX/RX control bits */
148 ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
149 22, /*12: BAUDLO (default=9600) */
150 0, /*13: BAUDHI (default=9600) */
151 ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
152 ZSWR15_BREAK_IE,
153 };
154
155 static struct zshan *
156 zs_ioasic_get_chan_addr(tc_addr_t zsaddr, int channel)
157 {
158 struct zsdevice *addr;
159 struct zshan *zc;
160
161 #if defined(__alpha__) || defined(alpha)
162 addr = (struct zsdevice *)TC_DENSE_TO_SPARSE(zsaddr);
163 #endif
164 #if defined(pmax)
165 addr = (struct zsdevice *)MIPS_PHYS_TO_KSEG1(zsaddr);
166 #endif
167
168 if (channel == 0)
169 zc = &addr->zs_chan_a;
170 else
171 zc = &addr->zs_chan_b;
172
173 return (zc);
174 }
175
176
177 /****************************************************************
178 * Autoconfig
179 ****************************************************************/
180
181 /* Definition of the driver for autoconfig. */
182 static int zs_ioasic_match(device_t, cfdata_t, void *);
183 static void zs_ioasic_attach(device_t, device_t, void *);
184 static int zs_ioasic_print(void *, const char *name);
185 static int zs_ioasic_submatch(device_t, cfdata_t,
186 const int *, void *);
187
188 CFATTACH_DECL_NEW(zsc_ioasic, sizeof(struct zsc_softc),
189 zs_ioasic_match, zs_ioasic_attach, NULL, NULL);
190
191 /* Interrupt handlers. */
192 static int zs_ioasic_hardintr(void *);
193 static void zs_ioasic_softintr(void *);
194
195 /*
196 * Is the zs chip present?
197 */
198 static int
199 zs_ioasic_match(device_t parent, cfdata_t cf, void *aux)
200 {
201 struct ioasicdev_attach_args *d = aux;
202 tc_addr_t zs_addr;
203
204 /*
205 * Make sure that we're looking for the right kind of device.
206 */
207 if (strncmp(d->iada_modname, "z8530 ", TC_ROM_LLEN) != 0 &&
208 strncmp(d->iada_modname, "scc", TC_ROM_LLEN) != 0)
209 return (0);
210
211 /*
212 * Find out the device address, and check it for validity.
213 */
214 zs_addr = TC_DENSE_TO_SPARSE((tc_addr_t)d->iada_addr);
215 if (tc_badaddr(zs_addr))
216 return (0);
217
218 return (1);
219 }
220
221 /*
222 * Attach a found zs.
223 */
224 static void
225 zs_ioasic_attach(device_t parent, device_t self, void *aux)
226 {
227 struct zsc_softc *zs = device_private(self);
228 struct zsc_attach_args zs_args;
229 struct zs_chanstate *cs;
230 struct ioasicdev_attach_args *d = aux;
231 struct zshan *zc;
232 int s, channel;
233 u_long zflg;
234 int locs[ZSCCF_NLOCS];
235
236 zs->zsc_dev = self;
237 aprint_normal("\n");
238
239 /*
240 * Initialize software state for each channel.
241 */
242 for (channel = 0; channel < 2; channel++) {
243 zs_args.channel = channel;
244 zs_args.hwflags = 0;
245
246 if (zs_ioasic_isconsole(d->iada_offset, channel)) {
247 cs = &zs_ioasic_conschanstate_store;
248 zs_args.hwflags |= ZS_HWFLAG_CONSOLE;
249 } else {
250 cs = malloc(sizeof(struct zs_chanstate),
251 M_DEVBUF, M_WAITOK | M_ZERO);
252 zs_lock_init(cs);
253 zc = zs_ioasic_get_chan_addr(d->iada_addr, channel);
254 cs->cs_reg_csr = (volatile void *)&zc->zc_csr;
255
256 memcpy(cs->cs_creg, zs_ioasic_init_reg, 16);
257 memcpy(cs->cs_preg, zs_ioasic_init_reg, 16);
258
259 cs->cs_defcflag = zs_def_cflag;
260 cs->cs_defspeed = 9600; /* XXX */
261 (void)zs_set_modes(cs, cs->cs_defcflag);
262 }
263
264 zs->zsc_cs[channel] = cs;
265 zs->zsc_addroffset = d->iada_offset; /* cookie only */
266 cs->cs_channel = channel;
267 cs->cs_ops = &zsops_null;
268 cs->cs_brg_clk = PCLK / 16;
269
270 /*
271 * DCD and CTS interrupts are only meaningful on
272 * SCC 0/B, and RTS and DTR only on B of SCC 0 & 1.
273 *
274 * XXX This is sorta gross.
275 */
276 if (d->iada_offset == 0x00100000 && channel == 1) {
277 cs->cs_creg[15] |= ZSWR15_DCD_IE;
278 cs->cs_preg[15] |= ZSWR15_DCD_IE;
279 zflg = ZIP_FLAGS_DCDCTS;
280 } else
281 zflg = 0;
282 if (channel == 1)
283 zflg |= ZIP_FLAGS_DTRRTS;
284 cs->cs_private = (void *)zflg;
285
286 /*
287 * Clear the master interrupt enable.
288 * The INTENA is common to both channels,
289 * so just do it on the A channel.
290 */
291 if (channel == 0) {
292 zs_write_reg(cs, 9, 0);
293 }
294
295 /*
296 * Set up the flow/modem control channel pointer to
297 * deal with the weird wiring on the TC Alpha and
298 * DECstation.
299 */
300 if (channel == 1)
301 cs->cs_ctl_chan = zs->zsc_cs[0];
302 else
303 cs->cs_ctl_chan = NULL;
304
305 locs[ZSCCF_CHANNEL] = channel;
306
307 /*
308 * Look for a child driver for this channel.
309 * The child attach will setup the hardware.
310 */
311 if (config_found(self, (void *)&zs_args, zs_ioasic_print,
312 CFARG_SUBMATCH, zs_ioasic_submatch,
313 CFARG_IATTR, "zsc",
314 CFARG_LOCATORS, locs,
315 CFARG_EOL) == NULL) {
316 /* No sub-driver. Just reset it. */
317 uint8_t reset = (channel == 0) ?
318 ZSWR9_A_RESET : ZSWR9_B_RESET;
319 s = splhigh();
320 zs_write_reg(cs, 9, reset);
321 splx(s);
322 }
323 }
324
325 /*
326 * Set up the ioasic interrupt handler.
327 */
328 ioasic_intr_establish(parent, d->iada_cookie, TC_IPL_TTY,
329 zs_ioasic_hardintr, zs);
330 zs->zsc_sih = softint_establish(SOFTINT_SERIAL,
331 zs_ioasic_softintr, zs);
332 if (zs->zsc_sih == NULL)
333 panic("%s: unable to register softintr", __func__);
334
335 /*
336 * Set the master interrupt enable and interrupt vector. The
337 * Sun does this only on one channel. The old Alpha SCC driver
338 * did it on both. We'll do it on both.
339 */
340 s = splhigh();
341 /* interrupt vector */
342 zs_write_reg(zs->zsc_cs[0], 2, zs_ioasic_init_reg[2]);
343 zs_write_reg(zs->zsc_cs[1], 2, zs_ioasic_init_reg[2]);
344
345 /* master interrupt control (enable) */
346 zs_write_reg(zs->zsc_cs[0], 9, zs_ioasic_init_reg[9]);
347 zs_write_reg(zs->zsc_cs[1], 9, zs_ioasic_init_reg[9]);
348 #if defined(__alpha__) || defined(alpha)
349 /* ioasic interrupt enable */
350 *(volatile u_int *)(ioasic_base + IOASIC_IMSK) |=
351 IOASIC_INTR_SCC_1 | IOASIC_INTR_SCC_0;
352 tc_mb();
353 #endif
354 splx(s);
355 }
356
357 static int
358 zs_ioasic_print(void *aux, const char *name)
359 {
360 struct zsc_attach_args *args = aux;
361
362 if (name != NULL)
363 aprint_normal("%s:", name);
364
365 if (args->channel != -1)
366 aprint_normal(" channel %d", args->channel);
367
368 return (UNCONF);
369 }
370
371 static int
372 zs_ioasic_submatch(device_t parent, cfdata_t cf, const int *locs, void *aux)
373 {
374 struct zsc_softc *zs = device_private(parent);
375 struct zsc_attach_args *pa = aux;
376 const char *defname = "";
377
378 if (cf->cf_loc[ZSCCF_CHANNEL] != ZSCCF_CHANNEL_DEFAULT &&
379 cf->cf_loc[ZSCCF_CHANNEL] != locs[ZSCCF_CHANNEL])
380 return (0);
381
382 if (cf->cf_loc[ZSCCF_CHANNEL] == ZSCCF_CHANNEL_DEFAULT) {
383 if (pa->channel == 0) {
384 #if defined(pmax)
385 if (systype == DS_MAXINE)
386 return (0);
387 #endif
388 if (zs->zsc_addroffset == 0x100000)
389 defname = "vsms";
390 else
391 defname = "lkkbd";
392 }
393 else if (zs->zsc_addroffset == 0x100000)
394 defname = "zstty";
395 #if defined(pmax)
396 else if (systype == DS_MAXINE)
397 return (0);
398 #endif
399 #if defined(__alpha__) || defined(alpha)
400 else if (cputype == ST_DEC_3000_300)
401 return (0);
402 #endif
403 else
404 defname = "zstty"; /* 3min/3max+, DEC3000/500 */
405
406 if (strcmp(cf->cf_name, defname))
407 return (0);
408 }
409 return (config_match(parent, cf, aux));
410 }
411
412 /*
413 * Hardware interrupt handler.
414 */
415 static int
416 zs_ioasic_hardintr(void *arg)
417 {
418 struct zsc_softc *zsc = arg;
419
420 /*
421 * Call the upper-level MI hardware interrupt handler.
422 */
423 zsc_intr_hard(zsc);
424
425 /*
426 * Check to see if we need to schedule any software-level
427 * processing interrupts.
428 */
429 if (zsc->zsc_cs[0]->cs_softreq | zsc->zsc_cs[1]->cs_softreq)
430 softint_schedule(zsc->zsc_sih);
431
432 return (1);
433 }
434
435 /*
436 * Software-level interrupt (character processing, lower priority).
437 */
438 static void
439 zs_ioasic_softintr(void *arg)
440 {
441 struct zsc_softc *zsc = arg;
442 int s;
443
444 s = spltty();
445 (void)zsc_intr_soft(zsc);
446 splx(s);
447 }
448
449 /*
450 * MD functions for setting the baud rate and control modes.
451 */
452 int
453 zs_set_speed(struct zs_chanstate *cs, int bps /*bits per second*/)
454 {
455 int tconst, real_bps;
456
457 if (bps == 0)
458 return (0);
459
460 #ifdef DIAGNOSTIC
461 if (cs->cs_brg_clk == 0)
462 panic("zs_set_speed");
463 #endif
464
465 tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
466 if (tconst < 0)
467 return (EINVAL);
468
469 /* Convert back to make sure we can do it. */
470 real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
471
472 /* XXX - Allow some tolerance here? */
473 if (real_bps != bps)
474 return (EINVAL);
475
476 cs->cs_preg[12] = tconst;
477 cs->cs_preg[13] = tconst >> 8;
478
479 /* Caller will stuff the pending registers. */
480 return (0);
481 }
482
483 int
484 zs_set_modes(struct zs_chanstate *cs, int cflag)
485 {
486 u_long privflags = (u_long)cs->cs_private;
487 int s;
488
489 /*
490 * Output hardware flow control on the chip is horrendous:
491 * if carrier detect drops, the receiver is disabled, and if
492 * CTS drops, the transmitter is stoped IN MID CHARACTER!
493 * Therefore, NEVER set the HFC bit, and instead use the
494 * status interrupt to detect CTS changes.
495 */
496 s = splzs();
497 if ((cflag & (CLOCAL | MDMBUF)) != 0)
498 cs->cs_rr0_dcd = 0;
499 else
500 cs->cs_rr0_dcd = ZSRR0_DCD;
501 if ((cflag & CRTSCTS) != 0) {
502 cs->cs_wr5_dtr = ZSWR5_DTR;
503 cs->cs_wr5_rts = ZSWR5_RTS;
504 cs->cs_rr0_cts = ZSRR0_CTS;
505 } else if ((cflag & CDTRCTS) != 0) {
506 cs->cs_wr5_dtr = 0;
507 cs->cs_wr5_rts = ZSWR5_DTR;
508 cs->cs_rr0_cts = ZSRR0_CTS;
509 } else if ((cflag & MDMBUF) != 0) {
510 cs->cs_wr5_dtr = 0;
511 cs->cs_wr5_rts = ZSWR5_DTR;
512 cs->cs_rr0_cts = ZSRR0_DCD;
513 } else {
514 cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
515 cs->cs_wr5_rts = 0;
516 cs->cs_rr0_cts = 0;
517 }
518
519 if ((privflags & ZIP_FLAGS_DCDCTS) == 0) {
520 cs->cs_rr0_dcd &= ~(ZSRR0_CTS|ZSRR0_DCD);
521 cs->cs_rr0_cts &= ~(ZSRR0_CTS|ZSRR0_DCD);
522 }
523 if ((privflags & ZIP_FLAGS_DTRRTS) == 0) {
524 cs->cs_wr5_dtr &= ~(ZSWR5_RTS|ZSWR5_DTR);
525 cs->cs_wr5_rts &= ~(ZSWR5_RTS|ZSWR5_DTR);
526 }
527 splx(s);
528
529 /* Caller will stuff the pending registers. */
530 return (0);
531 }
532
533 /*
534 * Functions to read and write individual registers in a channel.
535 * The ZS chip requires a 1.6 uSec. recovery time between accesses,
536 * and the Alpha TC hardware does NOT take care of this for you.
537 * The delay is now handled inside the chip access functions.
538 * These could be inlines, but with the delay, speed is moot.
539 */
540 #if defined(pmax)
541 #undef DELAY
542 #define DELAY(x)
543 #endif
544
545 u_int
546 zs_read_reg(struct zs_chanstate *cs, u_int reg)
547 {
548 volatile struct zshan *zc = (volatile void *)cs->cs_reg_csr;
549 unsigned val;
550
551 zc->zc_csr = reg << 8;
552 tc_wmb();
553 DELAY(5);
554 val = (zc->zc_csr >> 8) & 0xff;
555 /* tc_mb(); */
556 DELAY(5);
557 return (val);
558 }
559
560 void
561 zs_write_reg(struct zs_chanstate *cs, u_int reg, u_int val)
562 {
563 volatile struct zshan *zc = (volatile void *)cs->cs_reg_csr;
564
565 zc->zc_csr = reg << 8;
566 tc_wmb();
567 DELAY(5);
568 zc->zc_csr = val << 8;
569 tc_wmb();
570 DELAY(5);
571 }
572
573 u_int
574 zs_read_csr(struct zs_chanstate *cs)
575 {
576 volatile struct zshan *zc = (volatile void *)cs->cs_reg_csr;
577 unsigned val;
578
579 val = (zc->zc_csr >> 8) & 0xff;
580 /* tc_mb(); */
581 DELAY(5);
582 return (val);
583 }
584
585 void
586 zs_write_csr(struct zs_chanstate *cs, u_int val)
587 {
588 volatile struct zshan *zc = (volatile void *)cs->cs_reg_csr;
589
590 zc->zc_csr = val << 8;
591 tc_wmb();
592 DELAY(5);
593 }
594
595 u_int
596 zs_read_data(struct zs_chanstate *cs)
597 {
598 volatile struct zshan *zc = (volatile void *)cs->cs_reg_csr;
599 unsigned val;
600
601 val = (zc->zc_data) >> 8 & 0xff;
602 /* tc_mb(); */
603 DELAY(5);
604 return (val);
605 }
606
607 void
608 zs_write_data(struct zs_chanstate *cs, u_int val)
609 {
610 volatile struct zshan *zc = (volatile void *)cs->cs_reg_csr;
611
612 zc->zc_data = val << 8;
613 tc_wmb();
614 DELAY(5);
615 }
616
617 /****************************************************************
618 * Console support functions
619 ****************************************************************/
620
621 /*
622 * Handle user request to enter kernel debugger.
623 */
624 void
625 zs_abort(struct zs_chanstate *cs)
626 {
627 u_int rr0;
628
629 /* Wait for end of break. */
630 /* XXX - Limit the wait? */
631 do {
632 rr0 = zs_read_csr(cs);
633 } while (rr0 & ZSRR0_BREAK);
634
635 #if defined(KGDB)
636 zskgdb(cs);
637 #elif defined(DDB)
638 Debugger();
639 #else
640 printf("zs_abort: ignoring break on console\n");
641 #endif
642 }
643
644 /*
645 * Polled input char.
646 */
647 int
648 zs_getc(struct zs_chanstate *cs)
649 {
650 int s, c;
651 u_int rr0;
652
653 s = splhigh();
654 /* Wait for a character to arrive. */
655 do {
656 rr0 = zs_read_csr(cs);
657 } while ((rr0 & ZSRR0_RX_READY) == 0);
658
659 c = zs_read_data(cs);
660 splx(s);
661
662 /*
663 * This is used by the kd driver to read scan codes,
664 * so don't translate '\r' ==> '\n' here...
665 */
666 return (c);
667 }
668
669 /*
670 * Polled output char.
671 */
672 static void
673 zs_putc(struct zs_chanstate *cs, int c)
674 {
675 int s;
676 u_int rr0;
677
678 s = splhigh();
679 /* Wait for transmitter to become ready. */
680 do {
681 rr0 = zs_read_csr(cs);
682 } while ((rr0 & ZSRR0_TX_READY) == 0);
683
684 zs_write_data(cs, c);
685
686 /* Wait for the character to be transmitted. */
687 do {
688 rr0 = zs_read_csr(cs);
689 } while ((rr0 & ZSRR0_TX_READY) == 0);
690 splx(s);
691 }
692
693 /*****************************************************************/
694
695 /*
696 * zs_ioasic_cninit --
697 * Initialize the serial channel for either a keyboard or
698 * a serial console.
699 */
700 static void
701 zs_ioasic_cninit(tc_addr_t ioasic_addr, tc_offset_t zs_offset, int channel)
702 {
703 struct zs_chanstate *cs;
704 tc_addr_t zs_addr;
705 struct zshan *zc;
706 u_long zflg;
707
708 /*
709 * Initialize the console finder helpers.
710 */
711 zs_ioasic_console_offset = zs_offset;
712 zs_ioasic_console_channel = channel;
713 zs_ioasic_console = 1;
714
715 /*
716 * Pointer to channel state.
717 */
718 cs = &zs_ioasic_conschanstate_store;
719
720 /*
721 * Compute the physical address of the chip, "map" it via
722 * K0SEG, and then get the address of the actual channel.
723 */
724 #if defined(__alpha__) || defined(alpha)
725 zs_addr = ALPHA_PHYS_TO_K0SEG(ioasic_addr + zs_offset);
726 #endif
727 #if defined(pmax)
728 zs_addr = MIPS_PHYS_TO_KSEG1(ioasic_addr + zs_offset);
729 #endif
730 zc = zs_ioasic_get_chan_addr(zs_addr, channel);
731
732 /* Setup temporary chanstate. */
733 cs->cs_reg_csr = (volatile void *)&zc->zc_csr;
734
735 cs->cs_channel = channel;
736 cs->cs_ops = &zsops_null;
737 cs->cs_brg_clk = PCLK / 16;
738
739 /* Initialize the pending registers. */
740 memcpy(cs->cs_preg, zs_ioasic_init_reg, 16);
741 /* cs->cs_preg[5] |= (ZSWR5_DTR | ZSWR5_RTS); */
742
743 /*
744 * DCD and CTS interrupts are only meaningful on
745 * SCC 0/B, and RTS and DTR only on B of SCC 0 & 1.
746 *
747 * XXX This is sorta gross.
748 */
749 if (zs_offset == 0x00100000 && channel == 1)
750 zflg = ZIP_FLAGS_DCDCTS;
751 else
752 zflg = 0;
753 if (channel == 1)
754 zflg |= ZIP_FLAGS_DTRRTS;
755 cs->cs_private = (void *)zflg;
756
757 /* Clear the master interrupt enable. */
758 zs_write_reg(cs, 9, 0);
759
760 /* Reset the whole SCC chip. */
761 zs_write_reg(cs, 9, ZSWR9_HARD_RESET);
762
763 /* Copy "pending" to "current" and H/W. */
764 zs_loadchannelregs(cs);
765 }
766
767 /*
768 * zs_ioasic_cnattach --
769 * Initialize and attach a serial console.
770 */
771 void
772 zs_ioasic_cnattach(tc_addr_t ioasic_addr, tc_offset_t zs_offset, int channel)
773 {
774 struct zs_chanstate *cs = &zs_ioasic_conschanstate_store;
775 extern const struct cdevsw zstty_cdevsw;
776
777 zs_ioasic_cninit(ioasic_addr, zs_offset, channel);
778 zs_lock_init(cs);
779 cs->cs_defspeed = 9600;
780 cs->cs_defcflag = (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8;
781
782 /* Point the console at the SCC. */
783 cn_tab = &zs_ioasic_cons;
784 cn_tab->cn_pri = CN_REMOTE;
785 cn_tab->cn_dev = makedev(cdevsw_lookup_major(&zstty_cdevsw),
786 (zs_offset == 0x100000) ? 0 : 1);
787 }
788
789 /*
790 * zs_ioasic_lk201_cnattach --
791 * Initialize and attach a keyboard.
792 */
793 int
794 zs_ioasic_lk201_cnattach(tc_addr_t ioasic_addr, tc_offset_t zs_offset,
795 int channel)
796 {
797 #if (NZSKBD > 0)
798 struct zs_chanstate *cs = &zs_ioasic_conschanstate_store;
799
800 zs_ioasic_cninit(ioasic_addr, zs_offset, channel);
801 zs_lock_init(cs);
802 cs->cs_defspeed = 4800;
803 cs->cs_defcflag = (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8;
804 return (zskbd_cnattach(cs));
805 #else
806 return (ENXIO);
807 #endif
808 }
809
810 static int
811 zs_ioasic_isconsole(tc_offset_t offset, int channel)
812 {
813
814 if (zs_ioasic_console &&
815 offset == zs_ioasic_console_offset &&
816 channel == zs_ioasic_console_channel)
817 return (1);
818
819 return (0);
820 }
821
822 /*
823 * Polled console input putchar.
824 */
825 static int
826 zs_ioasic_cngetc(dev_t dev)
827 {
828
829 return (zs_getc(&zs_ioasic_conschanstate_store));
830 }
831
832 /*
833 * Polled console output putchar.
834 */
835 static void
836 zs_ioasic_cnputc(dev_t dev, int c)
837 {
838
839 zs_putc(&zs_ioasic_conschanstate_store, c);
840 }
841
842 /*
843 * Set polling/no polling on console.
844 */
845 static void
846 zs_ioasic_cnpollc(dev_t dev, int onoff)
847 {
848
849 /* XXX ??? */
850 }
851