xref: /netbsd-src/sys/arch/cobalt/dev/zs.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*	$NetBSD: zs.c,v 1.4 2015/06/09 22:47:59 matt 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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Zilog Z8530 Dual UART driver (machine-dependent part)
34  *
35  * Runs two serial lines per chip using slave drivers.
36  * Plain tty/async lines use the zs_async slave.
37  */
38 
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: zs.c,v 1.4 2015/06/09 22:47:59 matt Exp $");
41 
42 #include "opt_ddb.h"
43 
44 #include <sys/param.h>
45 #include <sys/conf.h>
46 #include <sys/cpu.h>
47 #include <sys/device.h>
48 #include <sys/intr.h>
49 #include <sys/tty.h>
50 #include <sys/systm.h>
51 
52 #include <dev/cons.h>
53 #include <dev/ic/z8530reg.h>
54 
55 #include <mips/cpuregs.h>
56 
57 #include <machine/autoconf.h>
58 #include <machine/z8530var.h>
59 
60 #include <cobalt/cobalt/console.h>
61 
62 #include "ioconf.h"
63 
64 /*
65  * Some warts needed by z8530tty.c -
66  * The default parity REALLY needs to be the same as the PROM uses,
67  * or you can not see messages done with printf during boot-up...
68  */
69 int zs_def_cflag = (CREAD | CS8 | HUPCL);
70 
71 #define ZS_DEFSPEED	115200
72 #define PCLK		(115200 * 96)	/*  11.0592MHz */
73 
74 #define ZS_DELAY()	delay(2)
75 
76 /* The layout of this is hardware-dependent (padding, order). */
77 /* A/~B (Channel A/Channel B) pin is connected to DAdr0 */
78 #define ZS_CHAN_A	0x01
79 #define ZS_CHAN_B	0x00
80 
81 /* D/~C (Data/Control) pin is connected to DAdr1 */
82 #define ZS_CSR		0x00		/* ctrl, status, and indirect access */
83 #define ZS_DATA		0x02		/* data */
84 
85 
86 /* Definition of the driver for autoconfig. */
87 static int  zs_match(device_t, cfdata_t, void *);
88 static void zs_attach(device_t, device_t, void *);
89 static int  zs_print(void *, const char *name);
90 
91 CFATTACH_DECL_NEW(zsc, sizeof(struct zsc_softc),
92     zs_match, zs_attach, NULL, NULL);
93 
94 static int zshard(void *);
95 #if 0
96 static int zs_get_speed(struct zs_chanstate *);
97 #endif
98 static int  zs_getc(void *);
99 static void zs_putc(void *, int);
100 
101 /* console status from cninit */
102 static struct zs_chanstate zs_conschan_store;
103 static struct zs_chanstate *zs_conschan;
104 static uint8_t *zs_cons;
105 
106 /* default speed for all channels */
107 static int zs_defspeed = ZS_DEFSPEED;
108 
109 static uint8_t zs_init_reg[16] = {
110 	0,					/* 0: CMD (reset, etc.) */
111 	0,					/* 1: No interrupts yet. */
112 	0,					/* 2: no IVECT */
113 	ZSWR3_RX_8 | ZSWR3_RX_ENABLE,		/* 3: RX params and ctrl */
114 	ZSWR4_CLK_X16 | ZSWR4_ONESB,		/* 4: TX/RX misc params */
115 	ZSWR5_TX_8 | ZSWR5_TX_ENABLE,		/* 5: TX params and ctrl */
116 	0,					/* 6: TXSYNC/SYNCLO */
117 	0,					/* 7: RXSYNC/SYNCHI */
118 	0,					/* 8: alias for data port */
119 	ZSWR9_MASTER_IE,			/* 9: Master interrupt ctrl */
120 	0,					/*10: Misc TX/RX ctrl */
121 	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,	/*11: Clock Mode ctrl */
122 	BPS_TO_TCONST((PCLK/16), ZS_DEFSPEED),	/*12: BAUDLO */
123 	0,					/*13: BAUDHI */
124 	ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK, /*14: Misc ctrl */
125 	ZSWR15_BREAK_IE,			/*15: Ext/Status intr ctrl */
126 };
127 
128 /* register address offset for each channel */
129 static const int chanoff[] = { ZS_CHAN_A, ZS_CHAN_B };
130 
131 
132 static int
133 zs_match(device_t parent, cfdata_t cf, void *aux)
134 {
135 	static int matched;
136 
137 	/* only one zs */
138 	if (matched)
139 		return 0;
140 
141 	/* only Qube 2700 could have Z85C30 serial */
142 	if (cobalt_id != COBALT_ID_QUBE2700)
143 		return 0;
144 
145 	if (!console_present)
146 		return 0;
147 
148 	matched = 1;
149 	return 1;
150 }
151 
152 /*
153  * Attach a found zs.
154  */
155 static void
156 zs_attach(device_t parent, device_t self, void *aux)
157 {
158 	struct zsc_softc *zsc = device_private(self);
159 	struct mainbus_attach_args *maa = aux;
160 	struct zsc_attach_args zsc_args;
161 	uint8_t *zs_base;
162 	struct zs_chanstate *cs;
163 	int s, channel;
164 
165 	zsc->zsc_dev = self;
166 
167 	/* XXX: MI z8530 doesn't use bus_space(9) yet */
168 	zs_base = (void *)MIPS_PHYS_TO_KSEG1(maa->ma_addr);
169 
170 	aprint_normal(": optional Z85C30 serial port\n");
171 
172 	/*
173 	 * Initialize software state for each channel.
174 	 */
175 	for (channel = 0; channel < 2; channel++) {
176 		zsc_args.channel = channel;
177 		cs = &zsc->zsc_cs_store[channel];
178 
179 		zsc->zsc_cs[channel] = cs;
180 
181 		zs_init_reg[2] = 0;
182 
183 		if ((zs_base + chanoff[channel]) == zs_cons) {
184 			memcpy(cs, zs_conschan, sizeof(struct zs_chanstate));
185 			zs_conschan = cs;
186 			zsc_args.hwflags = ZS_HWFLAG_CONSOLE;
187 		} else {
188 			cs->cs_reg_csr  = zs_base + chanoff[channel] + ZS_CSR;
189 			cs->cs_reg_data = zs_base + chanoff[channel] + ZS_DATA;
190 			memcpy(cs->cs_creg, zs_init_reg, 16);
191 			memcpy(cs->cs_preg, zs_init_reg, 16);
192 			cs->cs_defspeed = zs_defspeed;
193 			zsc_args.hwflags = 0;
194 		}
195 
196 		zs_lock_init(cs);
197 		cs->cs_defcflag = zs_def_cflag;
198 
199 		cs->cs_channel = channel;
200 		cs->cs_private = NULL;
201 		cs->cs_ops = &zsops_null;
202 		cs->cs_brg_clk = PCLK / 16;
203 
204 		/* Make these correspond to cs_defcflag (-crtscts) */
205 		cs->cs_rr0_dcd = ZSRR0_DCD;
206 		cs->cs_rr0_cts = 0;
207 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
208 		cs->cs_wr5_rts = 0;
209 
210 		/*
211 		 * Clear the master interrupt enable.
212 		 * The INTENA is common to both channels,
213 		 * so just do it on the A channel.
214 		 */
215 		if (channel == 0) {
216 			s = splhigh();
217 			zs_write_reg(cs, 9, 0);
218 			splx(s);
219 		}
220 
221 		/*
222 		 * Look for a child driver for this channel.
223 		 * The child attach will setup the hardware.
224 		 */
225 		if (!config_found(self, (void *)&zsc_args, zs_print)) {
226 			/* No sub-driver.  Just reset it. */
227 			uint8_t reset = (channel == 0) ?
228 			    ZSWR9_A_RESET : ZSWR9_B_RESET;
229 			s = splhigh();
230 			zs_write_reg(cs,  9, reset);
231 			splx(s);
232 		}
233 	}
234 
235 	/*
236 	 * Now safe to install interrupt handlers.
237 	 */
238 	icu_intr_establish(maa->ma_irq, IST_EDGE, IPL_SERIAL, zshard, zsc);
239 	zsc->zsc_softintr_cookie = softint_establish(SOFTINT_SERIAL,
240 	    (void (*)(void *))zsc_intr_soft, zsc);
241 
242 	/*
243 	 * Set the master interrupt enable and interrupt vector.
244 	 * (common to both channels, do it on A)
245 	 */
246 	cs = zsc->zsc_cs[0];
247 	s = splhigh();
248 	/* interrupt vector */
249 	zs_write_reg(cs, 2, 0);
250 	/* master interrupt control (enable) */
251 	zs_write_reg(cs, 9, zs_init_reg[9]);
252 	splx(s);
253 }
254 
255 static int
256 zs_print(void *aux, const char *name)
257 {
258 	struct zsc_attach_args *args = aux;
259 
260 	if (name != NULL)
261 		aprint_normal("%s: ", name);
262 
263 	if (args->channel != -1)
264 		aprint_normal(" channel %d", args->channel);
265 
266 	return UNCONF;
267 }
268 
269 static int
270 zshard(void *arg)
271 {
272 	struct zsc_softc *zsc = arg;
273 	int rval;
274 
275 	rval = zsc_intr_hard(zsc);
276 
277 #if 1
278 	/* XXX: there is some race condition? */
279 	if (rval)
280 		while (zsc_intr_hard(zsc))
281 			;
282 #endif
283 
284 	/* We are at splzs here, so no need to lock. */
285 	if (zsc->zsc_cs[0]->cs_softreq || zsc->zsc_cs[1]->cs_softreq)
286 		softint_schedule(zsc->zsc_softintr_cookie);
287 
288 	return rval;
289 }
290 
291 /*
292  * Compute the current baud rate given a ZS channel.
293  */
294 #if 0
295 static int
296 zs_get_speed(struct zs_chanstate *cs)
297 {
298 	int tconst;
299 
300 	tconst =  zs_read_reg(cs, 12);
301 	tconst |= zs_read_reg(cs, 13) << 8;
302 	return TCONST_TO_BPS(cs->cs_brg_clk, tconst);
303 }
304 #endif
305 
306 /*
307  * MD functions for setting the baud rate and control modes.
308  */
309 int
310 zs_set_speed(struct zs_chanstate *cs, int bps)
311 {
312 	int tconst, real_bps;
313 
314 	if (bps == 0)
315 		return 0;
316 
317 #ifdef	DIAGNOSTIC
318 	if (cs->cs_brg_clk == 0)
319 		panic("zs_set_speed");
320 #endif
321 
322 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
323 	if (tconst < 0)
324 		return EINVAL;
325 
326 	/* Convert back to make sure we can do it. */
327 	real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
328 
329 	/* Allow ~4% tolerance here */
330 	if (abs(real_bps - bps) >= bps * 4 / 100)
331 		return EINVAL;
332 
333 	cs->cs_preg[12] = tconst;
334 	cs->cs_preg[13] = tconst >> 8;
335 
336 	/* Caller will stuff the pending registers. */
337 	return 0;
338 }
339 
340 int
341 zs_set_modes(struct zs_chanstate *cs, int cflag)
342 {
343 	int s;
344 
345 	/*
346 	 * Output hardware flow control on the chip is horrendous:
347 	 * if carrier detect drops, the receiver is disabled, and if
348 	 * CTS drops, the transmitter is stoped IN MID CHARACTER!
349 	 * Therefore, NEVER set the HFC bit, and instead use the
350 	 * status interrupt to detect CTS changes.
351 	 */
352 	s = splzs();
353 	cs->cs_rr0_pps = 0;
354 	if ((cflag & (CLOCAL | MDMBUF)) != 0) {
355 		cs->cs_rr0_dcd = 0;
356 		if ((cflag & MDMBUF) == 0)
357 			cs->cs_rr0_pps = ZSRR0_DCD;
358 	} else
359 		cs->cs_rr0_dcd = ZSRR0_DCD;
360 	if ((cflag & CRTSCTS) != 0) {
361 		cs->cs_wr5_dtr = ZSWR5_DTR;
362 		cs->cs_wr5_rts = ZSWR5_RTS;
363 		cs->cs_rr0_cts = ZSRR0_CTS;
364 	} else if ((cflag & MDMBUF) != 0) {
365 		cs->cs_wr5_dtr = 0;
366 		cs->cs_wr5_rts = ZSWR5_DTR;
367 		cs->cs_rr0_cts = ZSRR0_DCD;
368 	} else {
369 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
370 		cs->cs_wr5_rts = 0;
371 		cs->cs_rr0_cts = 0;
372 	}
373 	splx(s);
374 
375 	/* Caller will stuff the pending registers. */
376 	return 0;
377 }
378 
379 
380 /*
381  * Read or write the chip with suitable delays.
382  */
383 
384 uint8_t
385 zs_read_reg(struct zs_chanstate *cs, uint8_t reg)
386 {
387 	uint8_t val;
388 
389 	*cs->cs_reg_csr = reg;
390 	ZS_DELAY();
391 	val = *cs->cs_reg_csr;
392 	ZS_DELAY();
393 	return val;
394 }
395 
396 void
397 zs_write_reg(struct zs_chanstate *cs, uint8_t reg, uint8_t val)
398 {
399 
400 	*cs->cs_reg_csr = reg;
401 	ZS_DELAY();
402 	*cs->cs_reg_csr = val;
403 	ZS_DELAY();
404 }
405 
406 uint8_t
407 zs_read_csr(struct zs_chanstate *cs)
408 {
409 	uint8_t val;
410 
411 	val = *cs->cs_reg_csr;
412 	ZS_DELAY();
413 	return val;
414 }
415 
416 void
417 zs_write_csr(struct zs_chanstate *cs, uint8_t val)
418 {
419 
420 	*cs->cs_reg_csr = val;
421 	ZS_DELAY();
422 }
423 
424 uint8_t
425 zs_read_data(struct zs_chanstate *cs)
426 {
427 	uint8_t val;
428 
429 	val = *cs->cs_reg_data;
430 	ZS_DELAY();
431 	return val;
432 }
433 
434 void
435 zs_write_data(struct zs_chanstate *cs, uint8_t val)
436 {
437 
438 	*cs->cs_reg_data = val;
439 	ZS_DELAY();
440 }
441 
442 void
443 zs_abort(struct zs_chanstate *cs)
444 {
445 
446 #ifdef DDB
447 	Debugger();
448 #endif
449 }
450 
451 /*
452  * Polled input char.
453  */
454 int
455 zs_getc(void *arg)
456 {
457 	struct zs_chanstate *cs = arg;
458 	int s, c;
459 	uint8_t rr0;
460 
461 	s = splhigh();
462 	/* Wait for a character to arrive. */
463 	do {
464 		rr0 = *cs->cs_reg_csr;
465 		ZS_DELAY();
466 	} while ((rr0 & ZSRR0_RX_READY) == 0);
467 
468 	c = *cs->cs_reg_data;
469 	ZS_DELAY();
470 	splx(s);
471 
472 	return c;
473 }
474 
475 /*
476  * Polled output char.
477  */
478 void
479 zs_putc(void *arg, int c)
480 {
481 	struct zs_chanstate *cs = arg;
482 	int s;
483 	uint8_t rr0;
484 
485 	s = splhigh();
486 	/* Wait for transmitter to become ready. */
487 	do {
488 		rr0 = *cs->cs_reg_csr;
489 		ZS_DELAY();
490 	} while ((rr0 & ZSRR0_TX_READY) == 0);
491 
492 	*cs->cs_reg_data = c;
493 	ZS_DELAY();
494 	splx(s);
495 }
496 
497 void
498 zscnprobe(struct consdev *cn)
499 {
500 
501 	cn->cn_pri = (console_present != 0 && cobalt_id == COBALT_ID_QUBE2700)
502 	    ? CN_NORMAL : CN_DEAD;
503 }
504 
505 void
506 zscninit(struct consdev *cn)
507 {
508 	struct zs_chanstate *cs;
509 
510 	extern const struct cdevsw zstty_cdevsw;
511 
512 	cn->cn_dev = makedev(cdevsw_lookup_major(&zstty_cdevsw), 0);
513 
514 	zs_cons = (uint8_t *)MIPS_PHYS_TO_KSEG1(ZS_BASE) + ZS_CHAN_A; /* XXX */
515 
516 	zs_conschan = cs = &zs_conschan_store;
517 
518 	/* Setup temporary chanstate. */
519 	cs->cs_reg_csr  = zs_cons + ZS_CSR;
520 	cs->cs_reg_data = zs_cons + ZS_DATA;
521 
522 	/* Initialize the pending registers. */
523 	memcpy(cs->cs_preg, zs_init_reg, 16);
524 	cs->cs_preg[5] |= ZSWR5_DTR | ZSWR5_RTS;
525 
526 	cs->cs_preg[12] = BPS_TO_TCONST(PCLK / 16, ZS_DEFSPEED);
527 	cs->cs_preg[13] = 0;
528 	cs->cs_defspeed = ZS_DEFSPEED;
529 
530 	/* Clear the master interrupt enable. */
531 	zs_write_reg(cs, 9, 0);
532 
533 	/* Reset the whole SCC chip. */
534 	zs_write_reg(cs, 9, ZSWR9_HARD_RESET);
535 
536 	/* Copy "pending" to "current" and H/W */
537 	zs_loadchannelregs(cs);
538 }
539 
540 int
541 zscngetc(dev_t dev)
542 {
543 
544 	return zs_getc((void *)zs_conschan);
545 }
546 
547 void
548 zscnputc(dev_t dev, int c)
549 {
550 
551 	zs_putc((void *)zs_conschan, c);
552 }
553