xref: /netbsd-src/sys/arch/sgimips/dev/zs.c (revision 8ac07aec990b9d2e483062509d0a9fa5b4f57cf2)
1 /*	$NetBSD: zs.c,v 1.33 2008/03/29 19:15:35 tsutsui Exp $	*/
2 
3 /*-
4  * Copyright (c) 1996, 2000 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 and Wayne Knowles
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  */
45 
46 #include <sys/cdefs.h>
47 __KERNEL_RCSID(0, "$NetBSD: zs.c,v 1.33 2008/03/29 19:15:35 tsutsui Exp $");
48 
49 #include "opt_ddb.h"
50 #include "opt_kgdb.h"
51 
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/conf.h>
55 #include <sys/device.h>
56 #include <sys/file.h>
57 #include <sys/ioctl.h>
58 #include <sys/kernel.h>
59 #include <sys/proc.h>
60 #include <sys/tty.h>
61 #include <sys/time.h>
62 #include <sys/syslog.h>
63 #include <sys/cpu.h>
64 #include <sys/intr.h>
65 
66 #include <machine/machtype.h>
67 #include <machine/autoconf.h>
68 #include <machine/z8530var.h>
69 
70 #include <dev/cons.h>
71 #include <dev/ic/z8530reg.h>
72 
73 #include <sgimips/hpc/hpcvar.h>
74 #include <sgimips/hpc/hpcreg.h>
75 
76 #include <dev/arcbios/arcbios.h>
77 #include <dev/arcbios/arcbiosvar.h>
78 
79 #include "ioconf.h"
80 
81 /*
82  * Some warts needed by z8530tty.c -
83  * The default parity REALLY needs to be the same as the PROM uses,
84  * or you can not see messages done with printf during boot-up...
85  */
86 int zs_def_cflag = (CREAD | CS8 | HUPCL);
87 
88 #define PCLK		3672000	 /* PCLK pin input clock rate */
89 
90 #ifndef ZS_DEFSPEED
91 #define ZS_DEFSPEED	9600
92 #endif
93 
94 /*
95  * Define interrupt levels.
96  */
97 #define ZSHARD_PRI 64
98 
99 /* SGI shouldn't need ZS_DELAY() as recovery time is done in hardware? */
100 #define ZS_DELAY()	delay(3)
101 
102 /* The layout of this is hardware-dependent (padding, order). */
103 struct zschan {
104 	uint8_t pad1[3];
105 	volatile uint8_t zc_csr;	/* ctrl,status, and indirect access */
106 	uint8_t pad2[3];
107 	volatile uint8_t zc_data;	/* data */
108 };
109 
110 struct zsdevice {
111 	struct	zschan zs_chan_b;
112 	struct	zschan zs_chan_a;
113 };
114 
115 /* Return the byte offset of element within a structure */
116 #define OFFSET(struct_def, el)		((size_t)&((struct_def *)0)->el)
117 
118 #define ZS_CHAN_A	OFFSET(struct zsdevice, zs_chan_a)
119 #define ZS_CHAN_B	OFFSET(struct zsdevice, zs_chan_b)
120 #define ZS_REG_CSR	0
121 #define ZS_REG_DATA	1
122 static int zs_chan_offset[] = {ZS_CHAN_A, ZS_CHAN_B};
123 
124 static void zscnprobe (struct consdev *);
125 static void zscninit (struct consdev *);
126 static int  zscngetc (dev_t);
127 static void zscnputc (dev_t, int);
128 static void zscnpollc (dev_t, int);
129 
130 static int  cons_port;
131 
132 struct consdev zs_cn = {
133 	zscnprobe,
134 	zscninit,
135 	zscngetc,
136 	zscnputc,
137 	zscnpollc
138 };
139 
140 /* Flags from cninit() */
141 static int zs_consunit = -1;
142 static int zs_conschan = -1;
143 
144 /* Default speed for all channels */
145 static int zs_defspeed = ZS_DEFSPEED;
146 static volatile int zssoftpending;
147 
148 static uint8_t zs_init_reg[16] = {
149 	0,				/* 0: CMD (reset, etc.) */
150 	0,				/* 1: No interrupts yet. */
151 	ZSHARD_PRI,			/* 2: IVECT */
152 	ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
153 	ZSWR4_CLK_X16 | ZSWR4_ONESB,
154 	ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
155 	0,				/* 6: TXSYNC/SYNCLO */
156 	0,				/* 7: RXSYNC/SYNCHI */
157 	0,				/* 8: alias for data port */
158 	ZSWR9_MASTER_IE,
159 	0,				/*10: Misc. TX/RX control bits */
160 	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD | ZSWR11_TRXC_OUT_ENA,
161 	BPS_TO_TCONST(PCLK/16, ZS_DEFSPEED), /*12: BAUDLO (default=9600) */
162 	0,				/*13: BAUDHI (default=9600) */
163 	ZSWR14_BAUD_ENA,
164 	ZSWR15_BREAK_IE,
165 };
166 
167 
168 /****************************************************************
169  * Autoconfig
170  ****************************************************************/
171 
172 /* Definition of the driver for autoconfig. */
173 static int	zs_hpc_match(device_t, cfdata_t, void *);
174 static void	zs_hpc_attach(device_t, device_t, void *);
175 static int	zs_print(void *, const char *name);
176 
177 CFATTACH_DECL_NEW(zsc_hpc, sizeof(struct zsc_softc),
178     zs_hpc_match, zs_hpc_attach, NULL, NULL);
179 
180 static int	zshard (void *);
181 void		zssoft (void *);
182 static int	zs_get_speed (struct zs_chanstate *);
183 struct		zschan *zs_get_chan_addr (int zs_unit, int channel);
184 int		zs_getc (void *);
185 void		zs_putc (void *, int);
186 
187 /*
188  * Is the zs chip present?
189  */
190 static int
191 zs_hpc_match(device_t parent, cfdata_t cf, void *aux)
192 {
193 	struct hpc_attach_args *ha = aux;
194 
195 	if (strcmp(ha->ha_name, cf->cf_name) == 0)
196 		return (1);
197 
198 	return (0);
199 }
200 
201 /*
202  * Attach a found zs.
203  *
204  * Match slave number to zs unit number, so that misconfiguration will
205  * not set up the keyboard as ttya, etc.
206  */
207 static void
208 zs_hpc_attach(device_t parent, device_t self, void *aux)
209 {
210 	struct zsc_softc *zsc = device_private(self);
211 	struct hpc_attach_args *haa = aux;
212 	struct zsc_attach_args zsc_args;
213 	struct zs_chanstate *cs;
214 	struct zs_channel *ch;
215 	int    zs_unit, channel, err, s;
216 	const char  *promconsdev;
217 
218 	promconsdev = ARCBIOS->GetEnvironmentVariable("ConsoleOut");
219 
220 	zsc->zsc_dev = self;
221 	zsc->zsc_bustag = haa->ha_st;
222 	if ((err = bus_space_subregion(haa->ha_st, haa->ha_sh,
223 				       haa->ha_devoff, 0x10,
224 				       &zsc->zsc_base)) != 0) {
225 		aprint_error(": unable to map 85c30 registers, error = %d\n",
226 		    err);
227 		return;
228 	}
229 
230 	zs_unit = device_unit(self);
231 	aprint_normal("\n");
232 
233 	/*
234 	 * Initialize software state for each channel.
235 	 *
236 	 * Done in reverse order of channels since the first serial port
237 	 * is actually attached to the *second* channel, and vice versa.
238 	 * Doing it this way should force a 'zstty*' to attach zstty0 to
239 	 * channel 1 and zstty1 to channel 0.  They couldn't have wired
240 	 * it up in a more sensible fashion, could they?
241 	 */
242 	for (channel = 1; channel >= 0; channel--) {
243 		zsc_args.channel = channel;
244 		ch = &zsc->zsc_cs_store[channel];
245 		cs = zsc->zsc_cs[channel] = (struct zs_chanstate *)ch;
246 
247 		zs_lock_init(cs);
248 		cs->cs_reg_csr = NULL;
249 		cs->cs_reg_data = NULL;
250 		cs->cs_channel = channel;
251 		cs->cs_private = NULL;
252 		cs->cs_ops = &zsops_null;
253 		cs->cs_brg_clk = PCLK / 16;
254 
255 		if (bus_space_subregion(zsc->zsc_bustag, zsc->zsc_base,
256 					zs_chan_offset[channel],
257 					sizeof(struct zschan),
258 					&ch->cs_regs) != 0) {
259 			aprint_error_dev(self, "cannot map regs\n");
260 			return;
261 		}
262 		ch->cs_bustag = zsc->zsc_bustag;
263 
264 		memcpy(cs->cs_creg, zs_init_reg, 16);
265 		memcpy(cs->cs_preg, zs_init_reg, 16);
266 
267 		zsc_args.hwflags = 0;
268 		zsc_args.consdev = NULL;
269 
270 		if (zs_consunit == -1 && zs_conschan == -1) {
271 		    /*
272 		     * If this channel is being used by the PROM console,
273 		     * pass the generic zs driver a 'no reset' flag so the
274 		     * channel gets left in the appropriate state after
275 		     * attach.
276 		     *
277 		     * Note: the channel mappings are swapped.
278 		     */
279 		    if (promconsdev != NULL &&
280 			strlen(promconsdev) == 9 &&
281 			strncmp(promconsdev, "serial", 6) == 0 &&
282 			(promconsdev[7] == '0' || promconsdev[7] == '1')) {
283 			if (promconsdev[7] == '1' && channel == 0)
284 			    zsc_args.hwflags |= ZS_HWFLAG_NORESET;
285 			else if (promconsdev[7] == '0' && channel == 1)
286 			    zsc_args.hwflags |= ZS_HWFLAG_NORESET;
287 		    }
288 		}
289 
290 		/* If console, don't stomp speed, let zstty know */
291 		if (zs_unit == zs_consunit && channel == zs_conschan) {
292 			zsc_args.consdev = &zs_cn;
293 			zsc_args.hwflags = ZS_HWFLAG_CONSOLE;
294 
295 			cs->cs_defspeed = zs_get_speed(cs);
296 		} else
297 			cs->cs_defspeed = zs_defspeed;
298 
299 		cs->cs_defcflag = zs_def_cflag;
300 
301 		/* Make these correspond to cs_defcflag (-crtscts) */
302 		cs->cs_rr0_dcd = ZSRR0_DCD;
303 		cs->cs_rr0_cts = 0;
304 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
305 		cs->cs_wr5_rts = 0;
306 
307 		/*
308 		 * Clear the master interrupt enable.
309 		 * The INTENA is common to both channels,
310 		 * so just do it on the A channel.
311 		 */
312 		if (channel == 0) {
313 			zs_write_reg(cs, 9, 0);
314 		}
315 		/*
316 		 * Look for a child driver for this channel.
317 		 * The child attach will setup the hardware.
318 		 */
319 		if (!config_found(self, (void *)&zsc_args, zs_print)) {
320 			/* No sub-driver.  Just reset it. */
321 			uint8_t reset = (channel == 0) ?
322 				ZSWR9_A_RESET : ZSWR9_B_RESET;
323 
324 			s = splhigh();
325 			zs_write_reg(cs, 9, reset);
326 			splx(s);
327 		}
328 	}
329 
330 
331 	zsc->sc_si = softint_establish(SOFTINT_SERIAL, zssoft, zsc);
332 	cpu_intr_establish(haa->ha_irq, IPL_TTY, zshard, NULL);
333 
334 	evcnt_attach_dynamic(&zsc->zsc_intrcnt, EVCNT_TYPE_INTR, NULL,
335 			     device_xname(self), "intr");
336 
337 	/*
338 	 * Set the master interrupt enable and interrupt vector.
339 	 * (common to both channels, do it on A)
340 	 */
341 	cs = zsc->zsc_cs[0];
342 	s = splhigh();
343 	/* interrupt vector */
344 	zs_write_reg(cs, 2, zs_init_reg[2]);
345 	/* master interrupt control (enable) */
346 	zs_write_reg(cs, 9, zs_init_reg[9]);
347 	splx(s);
348 }
349 
350 static int
351 zs_print(void *aux, const char *name)
352 {
353 	struct zsc_attach_args *args = aux;
354 
355 	if (name != NULL)
356 		aprint_normal("%s: ", name);
357 
358 	if (args->channel != -1)
359 		aprint_normal(" channel %d", args->channel);
360 
361 	return UNCONF;
362 }
363 
364 /*
365  * Our ZS chips all share a common, autovectored interrupt,
366  * so we have to look at all of them on each interrupt.
367  */
368 static int
369 zshard(void *arg)
370 {
371 	register struct zsc_softc *zsc;
372 	register int rr3, unit, rval, softreq;
373 
374 	rval = 0;
375 	for (unit = 0; unit < zsc_cd.cd_ndevs; unit++) {
376 		zsc = device_private(zsc_cd.cd_devs[unit]);
377 		if (zsc == NULL)
378 			continue;
379 
380 		zsc->zsc_intrcnt.ev_count++;
381 		while ((rr3 = zsc_intr_hard(zsc))) {
382 			rval |= rr3;
383 		}
384 
385 		softreq = zsc->zsc_cs[0]->cs_softreq;
386 		softreq |= zsc->zsc_cs[1]->cs_softreq;
387 		if (softreq && (zssoftpending == 0)) {
388 			zssoftpending = 1;
389 			softint_schedule(zsc->sc_si);
390 		}
391 	}
392 	return rval;
393 }
394 
395 /*
396  * Similar scheme as for zshard (look at all of them)
397  */
398 void
399 zssoft(void *arg)
400 {
401 	register struct zsc_softc *zsc;
402 	register int s, unit;
403 
404 	/* This is not the only ISR on this IPL. */
405 	if (zssoftpending == 0)
406 		return;
407 
408 	/*
409 	 * The soft intr. bit will be set by zshard only if
410 	 * the variable zssoftpending is zero.  The order of
411 	 * these next two statements prevents our clearing
412 	 * the soft intr bit just after zshard has set it.
413 	 */
414 	/*isr_soft_clear(ZSSOFT_PRI);*/
415 	zssoftpending = 0;
416 
417 	/* Make sure we call the tty layer at spltty. */
418 	s = spltty();
419 	for (unit = 0; unit < zsc_cd.cd_ndevs; unit++) {
420 		zsc = device_private(zsc_cd.cd_devs[unit]);
421 		if (zsc == NULL)
422 			continue;
423 		(void) zsc_intr_soft(zsc);
424 	}
425 	splx(s);
426 	return;
427 }
428 
429 
430 /*
431  * Compute the current baud rate given a ZS channel.
432  */
433 static int
434 zs_get_speed(struct zs_chanstate *cs)
435 {
436 	int tconst;
437 
438 	tconst = zs_read_reg(cs, 12);
439 	tconst |= zs_read_reg(cs, 13) << 8;
440 	return (TCONST_TO_BPS(cs->cs_brg_clk, tconst));
441 }
442 
443 /*
444  * MD functions for setting the baud rate and control modes.
445  */
446 int
447 zs_set_speed(struct zs_chanstate *cs, int bps)
448 {
449 	int tconst, real_bps;
450 
451 #if 0
452 	while (!(zs_read_csr(cs) & ZSRR0_TX_READY))
453 		{/*nop*/}
454 #endif
455 	/* Wait for transmit buffer to empty */
456 	if (bps == 0) {
457 		return (0);
458 	}
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 0
474 	if (real_bps != bps)
475 		return (EINVAL);
476 #endif
477 
478 	cs->cs_preg[12] = tconst;
479 	cs->cs_preg[13] = tconst >> 8;
480 
481 	/* Caller will stuff the pending registers. */
482 	return (0);
483 }
484 
485 int
486 zs_set_modes(struct zs_chanstate *cs, int cflag)
487 {
488 	int s;
489 
490 	/*
491 	 * Output hardware flow control on the chip is horrendous:
492 	 * if carrier detect drops, the receiver is disabled, and if
493 	 * CTS drops, the transmitter is stoped IN MID CHARACTER!
494 	 * Therefore, NEVER set the HFC bit, and instead use the
495 	 * status interrupt to detect CTS changes.
496 	 */
497 	s = splzs();
498 	cs->cs_rr0_pps = 0;
499 	if ((cflag & (CLOCAL | MDMBUF)) != 0) {
500 		cs->cs_rr0_dcd = 0;
501 		if ((cflag & MDMBUF) == 0)
502 			cs->cs_rr0_pps = ZSRR0_DCD;
503 	} else
504 		cs->cs_rr0_dcd = ZSRR0_DCD;
505 	if ((cflag & CRTSCTS) != 0) {
506 		cs->cs_wr5_dtr = ZSWR5_DTR;
507 		cs->cs_wr5_rts = ZSWR5_RTS;
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 	splx(s);
519 
520 	/* Caller will stuff the pending registers. */
521 	return (0);
522 }
523 
524 
525 /*
526  * Read or write the chip with suitable delays.
527  */
528 
529 uint8_t
530 zs_read_reg(struct zs_chanstate *cs, uint8_t reg)
531 {
532 	uint8_t val;
533 	struct zs_channel *zsc = (struct zs_channel *)cs;
534 
535 	bus_space_write_1(zsc->cs_bustag, zsc->cs_regs, ZS_REG_CSR, reg);
536 	ZS_DELAY();
537 	val = bus_space_read_1(zsc->cs_bustag, zsc->cs_regs, ZS_REG_CSR);
538 	ZS_DELAY();
539 	return val;
540 }
541 
542 void
543 zs_write_reg(struct zs_chanstate *cs, uint8_t reg, uint8_t val)
544 {
545 	struct zs_channel *zsc = (struct zs_channel *)cs;
546 
547 	bus_space_write_1(zsc->cs_bustag, zsc->cs_regs, ZS_REG_CSR, reg);
548 	ZS_DELAY();
549 	bus_space_write_1(zsc->cs_bustag, zsc->cs_regs, ZS_REG_CSR, val);
550 	ZS_DELAY();
551 }
552 
553 uint8_t
554 zs_read_csr(struct zs_chanstate *cs)
555 {
556 	struct zs_channel *zsc = (struct zs_channel *)cs;
557 	uint8_t val;
558 
559 	val = bus_space_read_1(zsc->cs_bustag, zsc->cs_regs, ZS_REG_CSR);
560 	ZS_DELAY();
561 	return val;
562 }
563 
564 void
565 zs_write_csr(struct zs_chanstate *cs, uint8_t val)
566 {
567 	struct zs_channel *zsc = (struct zs_channel *)cs;
568 
569 	bus_space_write_1(zsc->cs_bustag, zsc->cs_regs, ZS_REG_CSR, val);
570 	ZS_DELAY();
571 }
572 
573 uint8_t
574 zs_read_data(struct zs_chanstate *cs)
575 {
576 	struct zs_channel *zsc = (struct zs_channel *)cs;
577 	uint8_t val;
578 
579 	val = bus_space_read_1(zsc->cs_bustag, zsc->cs_regs, ZS_REG_DATA);
580 	ZS_DELAY();
581 	return val;
582 }
583 
584 void
585 zs_write_data(struct zs_chanstate *cs, uint8_t val)
586 {
587 	struct zs_channel *zsc = (struct zs_channel *)cs;
588 
589 	bus_space_write_1(zsc->cs_bustag, zsc->cs_regs, ZS_REG_DATA, val);
590 	ZS_DELAY();
591 }
592 
593 void
594 zs_abort(struct zs_chanstate *cs)
595 {
596 #if defined(KGDB)
597 	zskgdb(cs);
598 #elif defined(DDB)
599 	Debugger();
600 #endif
601 }
602 
603 
604 /*********************************************************/
605 /*  Polled character I/O functions for console and KGDB  */
606 /*********************************************************/
607 
608 struct zschan *
609 zs_get_chan_addr(int zs_unit, int channel)
610 {
611 	static int dumped_addr = 0;
612 	struct zsdevice *addr;
613 	struct zschan *zc;
614 
615 	switch (mach_type) {
616 	case MACH_SGI_IP12:
617 		if (zs_unit == 2 && (mach_subtype == MACH_SGI_IP12_4D_3X ||
618 		    		     mach_subtype == MACH_SGI_IP12_VIP12)) {
619 			addr = (struct zsdevice *)
620 						MIPS_PHYS_TO_KSEG1(0x1fb80d20);
621 			break;
622 		}
623 
624 		/* FALLTHROUGH */
625 	case MACH_SGI_IP20:
626 		if (zs_unit == 0) {
627 			addr = (struct zsdevice *)
628 						MIPS_PHYS_TO_KSEG1(0x1fb80d00);
629 		} else if (zs_unit == 1) {
630 			addr = (struct zsdevice *)
631 						MIPS_PHYS_TO_KSEG1(0x1fb80d10);
632 		} else {
633 			panic("zs_get_chan_addr: bad zs_unit %d\n", zs_unit);
634 		}
635 		break;
636 
637 	case MACH_SGI_IP22:
638 		if (zs_unit != 0)
639 			panic("zs_get_chan_addr zs_unit != 0 on IP%d",
640 								mach_type);
641 
642 		addr = (struct zsdevice *) MIPS_PHYS_TO_KSEG1(0x1fbd9830);
643 		break;
644 
645 	default:
646 		panic("zs_get_chan_addr: unsupported IP%d", mach_type);
647 	}
648 
649 	/*
650 	 * We need to swap serial ports to match reality on
651 	 * non-keyboard channels.
652 	 */
653 	if (mach_type == MACH_SGI_IP22) {
654 		if (channel == 0)
655 			zc = &addr->zs_chan_b;
656 		else
657 			zc = &addr->zs_chan_a;
658 	} else {
659 		if (zs_unit == 0) {
660 			if (channel == 0)
661 				zc = &addr->zs_chan_a;
662 			else
663 				zc = &addr->zs_chan_b;
664 		} else {
665 			if (channel == 0)
666 				zc = &addr->zs_chan_b;
667 			else
668 				zc = &addr->zs_chan_a;
669 		}
670 	}
671 
672 	if (dumped_addr == 0) {
673 		dumped_addr++;
674 		aprint_debug("zs unit %d, channel %d had address %p\n",
675 						zs_unit, channel, zc);
676 	}
677 
678 	return (zc);
679 }
680 
681 int
682 zs_getc(void *arg)
683 {
684 	register volatile struct zschan *zc = arg;
685 	register int s, c, rr0;
686 
687 	s = splzs();
688 	/* Wait for a character to arrive. */
689 	do {
690 		rr0 = zc->zc_csr;
691 		ZS_DELAY();
692 	} while ((rr0 & ZSRR0_RX_READY) == 0);
693 
694 	c = zc->zc_data;
695 	ZS_DELAY();
696 	splx(s);
697 
698 	return (c);
699 }
700 
701 /*
702  * Polled output char.
703  */
704 void
705 zs_putc(void *arg, int c)
706 {
707 	register volatile struct zschan *zc = arg;
708 	register int s, rr0;
709 
710 	s = splzs();
711 	/* Wait for transmitter to become ready. */
712 	do {
713 		rr0 = zc->zc_csr;
714 		ZS_DELAY();
715 	} while ((rr0 & ZSRR0_TX_READY) == 0);
716 
717 	zc->zc_data = c;
718 	wbflush();
719 	ZS_DELAY();
720 	splx(s);
721 }
722 
723 /***************************************************************/
724 void
725 zscnprobe(struct consdev *cn)
726 {
727 }
728 
729 void
730 zscninit(struct consdev *cn)
731 {
732 	extern const struct cdevsw zstty_cdevsw;
733 	const char* consdev;
734 
735 	if ((consdev = ARCBIOS->GetEnvironmentVariable("ConsoleOut")) == NULL)
736 		panic("zscninit without valid ARCS ConsoleOut setting!");
737 
738 	if (strlen(consdev) != 9 ||
739 	    strncmp(consdev, "serial", 6) != 0)
740 		panic("zscninit with ARCS console not set to serial!");
741 
742 	cons_port = consdev[7] - '0';
743 
744 #if 0
745 	/*
746 	 * If your IP12 serial console goes missing after consinit(),
747 	 * try flipping this the other way 'round.  If there are some
748 	 * IP12 machines that actually require this, we'll be in for
749 	 * a lot of funnies once again...
750 	 */
751 	if (mach_type == MACH_SGI_IP12)
752 		cons_port = 1 - cons_port;
753 #endif
754 
755 	cn->cn_dev = makedev(cdevsw_lookup_major(&zstty_cdevsw), cons_port);
756 	cn->cn_pri = CN_REMOTE;
757 
758 	/* Mark this unit as the console */
759 	zs_consunit = 0;
760 
761 	/* SGI hardware wires serial port 1 to channel B, port 2 to A */
762 	if (cons_port == 0)
763 		zs_conschan = 1;
764 	else
765 		zs_conschan = 0;
766 }
767 
768 int
769 zscngetc(dev_t dev)
770 {
771 	struct zschan *zs;
772 
773 	switch (mach_type) {
774 	case MACH_SGI_IP12:
775 	case MACH_SGI_IP20:
776 		zs = zs_get_chan_addr(1, cons_port);
777 		break;
778 
779 	case MACH_SGI_IP22:
780 	default:
781 		zs = zs_get_chan_addr(0, cons_port);
782 		break;
783 	}
784 
785 	return zs_getc(zs);
786 }
787 
788 void
789 zscnputc(dev_t dev, int c)
790 {
791 	struct zschan *zs;
792 
793 	switch (mach_type) {
794 	case MACH_SGI_IP12:
795 	case MACH_SGI_IP20:
796 		zs = zs_get_chan_addr(1, cons_port);
797 		break;
798 
799 	case MACH_SGI_IP22:
800 	default:
801 		zs = zs_get_chan_addr(0, cons_port);
802 		break;
803 	}
804 
805 	zs_putc(zs, c);
806 }
807 
808 void
809 zscnpollc(dev_t dev, int on)
810 {
811 }
812