xref: /netbsd/sys/arch/x68k/dev/zs.c (revision c4a72b64)
1 /*	$NetBSD: zs.c,v 1.22 2002/10/02 16:02:44 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998 Minoura Makoto
5  * Copyright (c) 1996 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Gordon W. Ross.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *        This product includes software developed by the NetBSD
22  *        Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * Zilog Z8530 Dual UART driver (machine-dependent part)
42  *
43  * X68k uses one Z8530 built-in. Channel A is for RS-232C serial port;
44  * while channel B is dedicated to the mouse.
45  * Extra Z8530's can be installed for serial ports.  This driver
46  * supports up to 5 chips including the built-in one.
47  */
48 
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/conf.h>
52 #include <sys/device.h>
53 #include <sys/file.h>
54 #include <sys/ioctl.h>
55 #include <sys/kernel.h>
56 #include <sys/proc.h>
57 #include <sys/tty.h>
58 #include <sys/time.h>
59 #include <sys/syslog.h>
60 
61 #include <machine/cpu.h>
62 #include <machine/bus.h>
63 #include <arch/x68k/dev/intiovar.h>
64 #include <machine/z8530var.h>
65 
66 #include <dev/ic/z8530reg.h>
67 
68 #include "zsc.h"	/* NZSC */
69 #include "opt_zsc.h"
70 #ifndef ZSCN_SPEED
71 #define ZSCN_SPEED 9600
72 #endif
73 #include "zstty.h"
74 
75 
76 extern void Debugger __P((void));
77 
78 /*
79  * Some warts needed by z8530tty.c -
80  * The default parity REALLY needs to be the same as the PROM uses,
81  * or you can not see messages done with printf during boot-up...
82  */
83 int zs_def_cflag = (CREAD | CS8 | HUPCL);
84 int zscn_def_cflag = (CREAD | CS8 | HUPCL);
85 
86 /*
87  * X68k provides a 5.0 MHz clock to the ZS chips.
88  */
89 #define PCLK	(5 * 1000 * 1000)	/* PCLK pin input clock rate */
90 
91 
92 /* Default physical addresses. */
93 #define ZS_MAXDEV 5
94 static bus_addr_t zs_physaddr[ZS_MAXDEV] = {
95 	0x00e98000,
96 	0x00eafc00,
97 	0x00eafc10,
98 	0x00eafc20,
99 	0x00eafc30
100 };
101 
102 static u_char zs_init_reg[16] = {
103 	0,	/* 0: CMD (reset, etc.) */
104 	0,	/* 1: No interrupts yet. */
105 	0x70,	/* 2: XXX: IVECT */
106 	ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
107 	ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
108 	ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
109 	0,	/* 6: TXSYNC/SYNCLO */
110 	0,	/* 7: RXSYNC/SYNCHI */
111 	0,	/* 8: alias for data port */
112 	ZSWR9_MASTER_IE,
113 	ZSWR10_NRZ,	/*10: Misc. TX/RX control bits */
114 	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
115 	((PCLK/32)/9600)-2,	/*12: BAUDLO (default=9600) */
116 	0,			/*13: BAUDHI (default=9600) */
117 	ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
118 	ZSWR15_BREAK_IE,
119 };
120 
121 static volatile struct zschan *conschan = 0;
122 
123 
124 /****************************************************************
125  * Autoconfig
126  ****************************************************************/
127 
128 /* Definition of the driver for autoconfig. */
129 static int	zs_match __P((struct device *, struct cfdata *, void *));
130 static void	zs_attach __P((struct device *, struct device *, void *));
131 static int  zs_print __P((void *, const char *name));
132 
133 CFATTACH_DECL(zsc, sizeof(struct zsc_softc),
134     zs_match, zs_attach, NULL, NULL);
135 
136 extern struct cfdriver zsc_cd;
137 
138 static int zshard __P((void *));
139 int zssoft __P((void *));
140 static int zs_get_speed __P((struct zs_chanstate *));
141 
142 
143 /*
144  * Is the zs chip present?
145  */
146 static int
147 zs_match(parent, cf, aux)
148 	struct device *parent;
149 	struct cfdata *cf;
150 	void *aux;
151 {
152 	struct intio_attach_args *ia = aux;
153 	struct zsdevice *zsaddr = (void*) ia->ia_addr;
154 	int i;
155 
156 	if (strcmp (ia->ia_name, "zsc") != 0)
157 		return 0;
158 
159 	for (i = 0; i < ZS_MAXDEV; i++)
160 		if (zsaddr == (void*) zs_physaddr[i]) /* XXX */
161 			break;
162 
163 	ia->ia_size = 8;
164 	if (intio_map_allocate_region (parent, ia, INTIO_MAP_TESTONLY))
165 		return 0;
166 
167 	if (zsaddr != (void*) zs_physaddr[i])
168 		return 0;
169 	if (badaddr((caddr_t)INTIO_ADDR(zsaddr)))
170 		return 0;
171 
172 	return (1);
173 }
174 
175 /*
176  * Attach a found zs.
177  */
178 static void
179 zs_attach(parent, self, aux)
180 	struct device *parent;
181 	struct device *self;
182 	void *aux;
183 {
184 	struct zsc_softc *zsc = (void *) self;
185 	struct intio_attach_args *ia = aux;
186 	struct zsc_attach_args zsc_args;
187 	volatile struct zschan *zc;
188 	struct zs_chanstate *cs;
189 	int r, s, zs_unit, channel;
190 
191 	zs_unit = zsc->zsc_dev.dv_unit;
192 	zsc->zsc_addr = (void*) ia->ia_addr;
193 
194 	ia->ia_size = 8;
195 	r = intio_map_allocate_region (parent, ia, INTIO_MAP_ALLOCATE);
196 #ifdef DIAGNOSTIC
197 	if (r)
198 		panic ("zs: intio IO map corruption");
199 #endif
200 
201 	printf("\n");
202 
203 	/*
204 	 * Initialize software state for each channel.
205 	 */
206 	for (channel = 0; channel < 2; channel++) {
207 		struct device *child;
208 
209 		zsc_args.channel = channel;
210 		zsc_args.hwflags = 0;
211 		cs = &zsc->zsc_cs_store[channel];
212 		zsc->zsc_cs[channel] = cs;
213 
214 		cs->cs_channel = channel;
215 		cs->cs_private = NULL;
216 		cs->cs_ops = &zsops_null;
217 		cs->cs_brg_clk = PCLK / 16;
218 
219 		if (channel == 0)
220 			zc = (void*) INTIO_ADDR(&zsc->zsc_addr->zs_chan_a);
221 		else
222 			zc = (void*) INTIO_ADDR(&zsc->zsc_addr->zs_chan_b);
223 		cs->cs_reg_csr  = &zc->zc_csr;
224 		cs->cs_reg_data = &zc->zc_data;
225 
226 		zs_init_reg[2] = ia->ia_intr;
227 		memcpy(cs->cs_creg, zs_init_reg, 16);
228 		memcpy(cs->cs_preg, zs_init_reg, 16);
229 
230 		if (zc == conschan) {
231 			zsc_args.hwflags |= ZS_HWFLAG_CONSOLE;
232 			cs->cs_defspeed = zs_get_speed(cs);
233 			cs->cs_defcflag = zscn_def_cflag;
234 		} else {
235 			cs->cs_defspeed = 9600;
236 			cs->cs_defcflag = zs_def_cflag;
237 		}
238 
239 		/* Make these correspond to cs_defcflag (-crtscts) */
240 		cs->cs_rr0_dcd = ZSRR0_DCD;
241 		cs->cs_rr0_cts = 0;
242 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
243 		cs->cs_wr5_rts = 0;
244 
245 		/*
246 		 * Clear the master interrupt enable.
247 		 * The INTENA is common to both channels,
248 		 * so just do it on the A channel.
249 		 */
250 		if (channel == 0) {
251 			s = splzs();
252 			zs_write_reg(cs, 9, 0);
253 			splx(s);
254 		}
255 
256 		/*
257 		 * Look for a child driver for this channel.
258 		 * The child attach will setup the hardware.
259 		 */
260 		child = config_found(self, (void *)&zsc_args, zs_print);
261 #if ZSTTY > 0
262 		if (zc == conschan &&
263 		    ((child && strcmp (child->dv_xname, "zstty0")) ||
264 		     child == NULL)) /* XXX */
265 			panic ("zs_attach: console device mismatch");
266 #endif
267 		if (child == NULL) {
268 			/* No sub-driver.  Just reset it. */
269 			u_char reset = (channel == 0) ?
270 				ZSWR9_A_RESET : ZSWR9_B_RESET;
271 			s = splzs();
272 			zs_write_reg(cs,  9, reset);
273 			splx(s);
274 		}
275 	}
276 
277 	/*
278 	 * Now safe to install interrupt handlers.
279 	 */
280 	if (intio_intr_establish(ia->ia_intr, "zs", zshard, zsc))
281 		panic("zs_attach: interrupt vector busy");
282 	/* XXX; evcnt_attach() ? */
283 
284 	/*
285 	 * Set the master interrupt enable and interrupt vector.
286 	 * (common to both channels, do it on A)
287 	 */
288 	cs = zsc->zsc_cs[0];
289 	s = splzs();
290 	/* interrupt vector */
291 	zs_write_reg(cs, 2, ia->ia_intr);
292 	/* master interrupt control (enable) */
293 	zs_write_reg(cs, 9, zs_init_reg[9]);
294 	splx(s);
295 }
296 
297 static int
298 zs_print(aux, name)
299 	void *aux;
300 	const char *name;
301 {
302 	struct zsc_attach_args *args = aux;
303 
304 	if (name != NULL)
305 		printf("%s: ", name);
306 
307 	if (args->channel != -1)
308 		printf(" channel %d", args->channel);
309 
310 	return UNCONF;
311 }
312 
313 
314 /*
315  * For x68k-port, we don't use autovectored interrupt.
316  * We do not need to look at all of the zs chips.
317  */
318 static int
319 zshard(arg)
320 	void *arg;
321 {
322 	register struct zsc_softc *zsc = arg;
323 	register int rval;
324 	int s;
325 
326 	/*
327 	 * Actually, zs hardware ipl is 5.
328 	 * Here we disable all interrupts to shorten the zshard
329 	 * handling time.  Otherwise, too many characters are
330 	 * dropped.
331 	 */
332 	s = splhigh();
333 	rval = zsc_intr_hard(zsc);
334 
335 	/* We are at splzs here, so no need to lock. */
336 	if (zsc->zsc_cs[0]->cs_softreq || zsc->zsc_cs[1]->cs_softreq)
337 		setsoftserial();
338 
339 	return (rval);
340 }
341 
342 /*
343  * Shared among the all chips. We have to look at all of them.
344  */
345 int
346 zssoft(arg)
347 	void *arg;
348 {
349 	register struct zsc_softc *zsc;
350 	register int s, unit;
351 
352 	/* Make sure we call the tty layer at spltty. */
353 	s = spltty();
354 	for (unit = 0; unit < zsc_cd.cd_ndevs; unit++) {
355 		zsc = zsc_cd.cd_devs[unit];
356 		if (zsc == NULL)
357 			continue;
358 		(void) zsc_intr_soft(zsc);
359 	}
360 	splx(s);
361 
362 	return (1);
363 }
364 
365 
366 /*
367  * Compute the current baud rate given a ZS channel.
368  */
369 static int
370 zs_get_speed(cs)
371 	struct zs_chanstate *cs;
372 {
373 	int tconst;
374 
375 	tconst = zs_read_reg(cs, 12);
376 	tconst |= zs_read_reg(cs, 13) << 8;
377 	return (TCONST_TO_BPS(cs->cs_brg_clk, tconst));
378 }
379 
380 /*
381  * MD functions for setting the baud rate and control modes.
382  */
383 int
384 zs_set_speed(cs, bps)
385 	struct zs_chanstate *cs;
386 	int bps;	/* bits per second */
387 {
388 	int tconst, real_bps;
389 
390 	if (bps == 0)
391 		return (0);
392 
393 #ifdef	DIAGNOSTIC
394 	if (cs->cs_brg_clk == 0)
395 		panic("zs_set_speed");
396 #endif
397 
398 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
399 	if (tconst < 0)
400 		return (EINVAL);
401 
402 	/* Convert back to make sure we can do it. */
403 	real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
404 
405 #if 0				/* XXX */
406 	/* XXX - Allow some tolerance here? */
407 	if (real_bps != bps)
408 		return (EINVAL);
409 #else
410 	/*
411 	 * Since our PCLK has somewhat strange value,
412 	 * we have to allow tolerance here.
413 	 */
414 	if (BPS_TO_TCONST(cs->cs_brg_clk, real_bps) != tconst)
415 		return (EINVAL);
416 #endif
417 
418 	cs->cs_preg[12] = tconst;
419 	cs->cs_preg[13] = tconst >> 8;
420 
421 	/* Caller will stuff the pending registers. */
422 	return (0);
423 }
424 
425 int
426 zs_set_modes(cs, cflag)
427 	struct zs_chanstate *cs;
428 	int cflag;	/* bits per second */
429 {
430 	int s;
431 
432 	/*
433 	 * Output hardware flow control on the chip is horrendous:
434 	 * if carrier detect drops, the receiver is disabled, and if
435 	 * CTS drops, the transmitter is stoped IN MID CHARACTER!
436 	 * Therefore, NEVER set the HFC bit, and instead use the
437 	 * status interrupt to detect CTS changes.
438 	 */
439 	s = splzs();
440 	cs->cs_rr0_pps = 0;
441 	if ((cflag & (CLOCAL | MDMBUF)) != 0) {
442 		cs->cs_rr0_dcd = 0;
443 		if ((cflag & MDMBUF) == 0)
444 			cs->cs_rr0_pps = ZSRR0_DCD;
445 	} else
446 		cs->cs_rr0_dcd = ZSRR0_DCD;
447 	if ((cflag & CRTSCTS) != 0) {
448 		cs->cs_wr5_dtr = ZSWR5_DTR;
449 		cs->cs_wr5_rts = ZSWR5_RTS;
450 		cs->cs_rr0_cts = ZSRR0_CTS;
451 	} else if ((cflag & MDMBUF) != 0) {
452 		cs->cs_wr5_dtr = 0;
453 		cs->cs_wr5_rts = ZSWR5_DTR;
454 		cs->cs_rr0_cts = ZSRR0_DCD;
455 	} else {
456 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
457 		cs->cs_wr5_rts = 0;
458 		cs->cs_rr0_cts = 0;
459 	}
460 	splx(s);
461 
462 	/* Caller will stuff the pending registers. */
463 	return (0);
464 }
465 
466 
467 /*
468  * Read or write the chip with suitable delays.
469  */
470 
471 u_char
472 zs_read_reg(cs, reg)
473 	struct zs_chanstate *cs;
474 	u_char reg;
475 {
476 	u_char val;
477 
478 	*cs->cs_reg_csr = reg;
479 	ZS_DELAY();
480 	val = *cs->cs_reg_csr;
481 	ZS_DELAY();
482 	return val;
483 }
484 
485 void
486 zs_write_reg(cs, reg, val)
487 	struct zs_chanstate *cs;
488 	u_char reg, val;
489 {
490 	*cs->cs_reg_csr = reg;
491 	ZS_DELAY();
492 	*cs->cs_reg_csr = val;
493 	ZS_DELAY();
494 }
495 
496 u_char zs_read_csr(cs)
497 	struct zs_chanstate *cs;
498 {
499 	register u_char val;
500 
501 	val = *cs->cs_reg_csr;
502 	ZS_DELAY();
503 	return val;
504 }
505 
506 void  zs_write_csr(cs, val)
507 	struct zs_chanstate *cs;
508 	u_char val;
509 {
510 	*cs->cs_reg_csr = val;
511 	ZS_DELAY();
512 }
513 
514 u_char zs_read_data(cs)
515 	struct zs_chanstate *cs;
516 {
517 	register u_char val;
518 
519 	val = *cs->cs_reg_data;
520 	ZS_DELAY();
521 	return val;
522 }
523 
524 void  zs_write_data(cs, val)
525 	struct zs_chanstate *cs;
526 	u_char val;
527 {
528 	*cs->cs_reg_data = val;
529 	ZS_DELAY();
530 }
531 
532 
533 static struct zs_chanstate zscn_cs;
534 
535 /****************************************************************
536  * Console support functions (x68k specific!)
537  * Note: this code is allowed to know about the layout of
538  * the chip registers, and uses that to keep things simple.
539  * XXX - I think I like the mvme167 code better. -gwr
540  ****************************************************************/
541 
542 /*
543  * Handle user request to enter kernel debugger.
544  */
545 void
546 zs_abort(cs)
547 	struct zs_chanstate *cs;
548 {
549 	int rr0;
550 
551 	/* Wait for end of break to avoid PROM abort. */
552 	/* XXX - Limit the wait? */
553 	do {
554 		rr0 = *cs->cs_reg_csr;
555 		ZS_DELAY();
556 	} while (rr0 & ZSRR0_BREAK);
557 
558 #ifdef DDB
559 	Debugger();
560 #else
561 	printf ("BREAK!!\n");
562 #endif
563 }
564 
565 
566 #if NZSTTY > 0
567 
568 #include <dev/cons.h>
569 cons_decl(zs);
570 
571 static int zs_getc __P((void));
572 static void zs_putc __P((int));
573 
574 /*
575  * Polled input char.
576  */
577 static int
578 zs_getc(void)
579 {
580 	register int s, c, rr0;
581 
582 	s = splzs();
583 	/* Wait for a character to arrive. */
584 	do {
585 		rr0 = zs_read_csr(&zscn_cs);
586 	} while ((rr0 & ZSRR0_RX_READY) == 0);
587 
588 	c = zs_read_data (&zscn_cs);
589 	splx(s);
590 
591 	/*
592 	 * This is used by the kd driver to read scan codes,
593 	 * so don't translate '\r' ==> '\n' here...
594 	 */
595 	return (c);
596 }
597 
598 /*
599  * Polled output char.
600  */
601 static void
602 zs_putc(c)
603 	int c;
604 {
605 	register int s, rr0;
606 
607 	s = splzs();
608 	/* Wait for transmitter to become ready. */
609 	do {
610 		rr0 = zs_read_csr (&zscn_cs);
611 	} while ((rr0 & ZSRR0_TX_READY) == 0);
612 
613 	zs_write_data(&zscn_cs, c);
614 	splx(s);
615 }
616 
617 void
618 zscninit(cn)
619 	struct consdev *cn;
620 {
621 	volatile struct zschan *cnchan = (void*) INTIO_ADDR(ZSCN_PHYSADDR);
622 	int s;
623 
624 	memset(&zscn_cs, 0, sizeof (struct zs_chanstate));
625 	zscn_cs.cs_reg_csr = &cnchan->zc_csr;
626 	zscn_cs.cs_reg_data = &cnchan->zc_data;
627 	zscn_cs.cs_channel = 0;
628 	zscn_cs.cs_brg_clk = PCLK / 16;
629 	memcpy(zscn_cs.cs_preg, zs_init_reg, 16);
630 	zscn_cs.cs_preg[4] = ZSWR4_CLK_X16 | ZSWR4_ONESB; /* XXX */
631 	zscn_cs.cs_preg[9] = 0;
632 	zs_set_speed(&zscn_cs, ZSCN_SPEED);
633 	s = splzs();
634 	zs_loadchannelregs(&zscn_cs);
635 	splx(s);
636 	conschan = cnchan;
637 }
638 
639 /*
640  * Polled console input putchar.
641  */
642 int
643 zscngetc(dev)
644 	dev_t dev;
645 {
646 	return (zs_getc());
647 }
648 
649 /*
650  * Polled console output putchar.
651  */
652 void
653 zscnputc(dev, c)
654 	dev_t dev;
655 	int c;
656 {
657 	zs_putc(c);
658 }
659 
660 void
661 zscnprobe(cd)
662 	struct consdev *cd;
663 {
664 	int maj;
665 	extern const struct cdevsw zstty_cdevsw;
666 
667 	/* locate the major number */
668 	maj = cdevsw_lookup_major(&zstty_cdevsw);
669 	/* XXX: minor number is 0 */
670 
671 	if (maj == -1)
672 		cd->cn_pri = CN_DEAD;
673 	else {
674 #ifdef ZSCONSOLE
675 		cd->cn_pri = CN_REMOTE;	/* higher than ITE (CN_INTERNAL) */
676 #else
677 		cd->cn_pri = CN_NORMAL;
678 #endif
679 		cd->cn_dev = makedev(maj, 0);
680 	}
681 }
682 
683 void
684 zscnpollc(dev, on)
685 	dev_t dev;
686 	int on;
687 {
688 }
689 
690 #endif
691