No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / mipsco / obio / zs.c
blob292511d72275205f64b52d9a654fb05f27db96f4
1 /* $NetBSD: zs.c,v 1.23 2008/04/28 20:23:28 martin Exp $ */
3 /*-
4 * Copyright (c) 1996, 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Gordon W. Ross and Wayne Knowles
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.
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.
33 * Zilog Z8530 Dual UART driver (machine-dependent part)
35 * Runs two serial lines per chip using slave drivers.
36 * Plain tty/async lines use the zs_async slave.
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: zs.c,v 1.23 2008/04/28 20:23:28 martin Exp $");
42 #include "opt_ddb.h"
43 #include "opt_kgdb.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/conf.h>
48 #include <sys/device.h>
49 #include <sys/file.h>
50 #include <sys/ioctl.h>
51 #include <sys/kernel.h>
52 #include <sys/proc.h>
53 #include <sys/tty.h>
54 #include <sys/time.h>
55 #include <sys/syslog.h>
56 #include <sys/cpu.h>
57 #include <sys/intr.h>
59 #include <machine/mainboard.h>
60 #include <machine/autoconf.h>
61 #include <machine/prom.h>
62 #include <machine/z8530var.h>
64 #include <dev/cons.h>
65 #include <dev/ic/z8530reg.h>
67 #include "ioconf.h"
68 #include "zsc.h" /* NZSC */
69 #define NZS NZSC
71 /* Make life easier for the initialized arrays here. */
72 #if NZS < 2
73 #undef NZS
74 #define NZS 2
75 #endif
78 * Some warts needed by z8530tty.c -
79 * The default parity REALLY needs to be the same as the PROM uses,
80 * or you can not see messages done with printf during boot-up...
82 int zs_def_cflag = (CREAD | CS8 | HUPCL);
85 #define PCLK 10000000 /* PCLK pin input clock rate */
87 #ifndef ZS_DEFSPEED
88 #define ZS_DEFSPEED 9600
89 #endif
92 * Define interrupt levels.
94 #define ZSHARD_PRI 64
96 /* Register recovery time is 3.5 to 4 PCLK Cycles */
97 #define ZS_RECOVERY 1 /* 1us = 10 PCLK Cycles */
98 #define ZS_DELAY() delay(ZS_RECOVERY)
100 /* The layout of this is hardware-dependent (padding, order). */
101 struct zschan {
102 uint8_t pad1[3];
103 volatile uint8_t zc_csr; /* ctrl,status, and indirect access */
104 uint8_t pad2[3];
105 volatile uint8_t zc_data; /* data */
107 struct zsdevice {
108 /* Yes, they are backwards. */
109 struct zschan zs_chan_b;
110 struct zschan zs_chan_a;
113 /* Return the byte offset of element within a structure */
114 #define OFFSET(struct_def, el) ((size_t)&((struct_def *)0)->el)
116 #define ZS_CHAN_A OFFSET(struct zsdevice, zs_chan_a)
117 #define ZS_CHAN_B OFFSET(struct zsdevice, zs_chan_b)
118 #define ZS_REG_CSR OFFSET(struct zschan, zc_csr)
119 #define ZS_REG_DATA OFFSET(struct zschan, zc_data)
120 static int zs_chan_offset[] = {ZS_CHAN_A, ZS_CHAN_B};
122 /* Flags from cninit() */
123 static int zs_hwflags[NZS][2];
125 /* Default speed for all channels */
126 static int zs_defspeed = ZS_DEFSPEED;
127 static volatile int zssoftpending;
129 static uint8_t zs_init_reg[16] = {
130 0, /* 0: CMD (reset, etc.) */
131 0, /* 1: No interrupts yet. */
132 ZSHARD_PRI, /* 2: IVECT */
133 ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
134 ZSWR4_CLK_X16 | ZSWR4_ONESB,
135 ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
136 0, /* 6: TXSYNC/SYNCLO */
137 0, /* 7: RXSYNC/SYNCHI */
138 0, /* 8: alias for data port */
139 ZSWR9_MASTER_IE,
140 0, /*10: Misc. TX/RX control bits */
141 ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD | ZSWR11_TRXC_OUT_ENA,
142 BPS_TO_TCONST(PCLK/16, ZS_DEFSPEED), /*12: BAUDLO (default=9600) */
143 0, /*13: BAUDHI (default=9600) */
144 ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
145 ZSWR15_BREAK_IE,
149 /****************************************************************
150 * Autoconfig
151 ****************************************************************/
153 /* Definition of the driver for autoconfig. */
154 static int zs_match(device_t, cfdata_t, void *);
155 static void zs_attach(device_t, device_t, void *);
156 static int zs_print(void *, const char *name);
158 CFATTACH_DECL_NEW(zsc, sizeof(struct zsc_softc),
159 zs_match, zs_attach, NULL, NULL);
161 static int zshard(void *);
162 void zssoft(void *);
163 static int zs_get_speed(struct zs_chanstate *);
164 struct zschan *zs_get_chan_addr(int zs_unit, int channel);
165 int zs_getc(void *);
166 void zs_putc(void *, int);
169 * Is the zs chip present?
171 static int
172 zs_match(device_t parent, cfdata_t cf, void *aux)
174 struct confargs *ca = aux;
175 void *va;
177 if (strcmp(ca->ca_name, "zsc"))
178 return 0;
180 va = (void *)cf->cf_addr;
182 /* This returns -1 on a fault (bus error). */
183 if (badaddr(va, 1))
184 return 0;
185 return 1;
189 * Attach a found zs.
191 * Match slave number to zs unit number, so that misconfiguration will
192 * not set up the keyboard as ttya, etc.
194 static void
195 zs_attach(device_t parent, device_t self, void *aux)
197 struct zsc_softc *zsc = device_private(self);
198 struct confargs *ca = aux;
199 struct zsc_attach_args zsc_args;
200 struct zs_chanstate *cs;
201 struct zs_channel *ch;
202 int zs_unit, channel, s;
204 zsc->zsc_dev = self;
205 zsc->zsc_bustag = ca->ca_bustag;
206 if (bus_space_map(ca->ca_bustag, ca->ca_addr,
207 sizeof(struct zsdevice),
208 BUS_SPACE_MAP_LINEAR,
209 &zsc->zsc_base) != 0) {
210 aprint_error(": cannot map registers\n");
211 return;
214 zs_unit = device_unit(self);
215 aprint_normal("\n");
218 * Initialize software state for each channel.
220 for (channel = 0; channel < 2; channel++) {
221 zsc_args.channel = channel;
222 zsc_args.hwflags = zs_hwflags[zs_unit][channel];
223 ch = &zsc->zsc_cs_store[channel];
224 cs = zsc->zsc_cs[channel] = (struct zs_chanstate *)ch;
226 zs_lock_init(cs);
227 cs->cs_reg_csr = NULL;
228 cs->cs_reg_data = NULL;
229 cs->cs_channel = channel;
230 cs->cs_private = NULL;
231 cs->cs_ops = &zsops_null;
232 cs->cs_brg_clk = PCLK / 16;
234 if (bus_space_subregion(ca->ca_bustag, zsc->zsc_base,
235 zs_chan_offset[channel],
236 sizeof(struct zschan),
237 &ch->cs_regs) != 0) {
238 aprint_error_dev(self, ": cannot map regs\n");
239 return;
241 ch->cs_bustag = ca->ca_bustag;
243 memcpy(cs->cs_creg, zs_init_reg, 16);
244 memcpy(cs->cs_preg, zs_init_reg, 16);
246 if (zsc_args.hwflags & ZS_HWFLAG_CONSOLE)
247 cs->cs_defspeed = zs_get_speed(cs);
248 else
249 cs->cs_defspeed = zs_defspeed;
250 cs->cs_defcflag = zs_def_cflag;
252 /* Make these correspond to cs_defcflag (-crtscts) */
253 cs->cs_rr0_dcd = ZSRR0_DCD;
254 cs->cs_rr0_cts = 0;
255 cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
256 cs->cs_wr5_rts = 0;
259 * Clear the master interrupt enable.
260 * The INTENA is common to both channels,
261 * so just do it on the A channel.
263 if (channel == 0) {
264 zs_write_reg(cs, 9, 0);
267 * Look for a child driver for this channel.
268 * The child attach will setup the hardware.
270 if (!config_found(self, (void *)&zsc_args, zs_print)) {
271 /* No sub-driver. Just reset it. */
272 uint8_t reset = (channel == 0) ?
273 ZSWR9_A_RESET : ZSWR9_B_RESET;
275 s = splhigh();
276 zs_write_reg(cs, 9, reset);
277 splx(s);
282 zsc->sc_si = softint_establish(SOFTINT_SERIAL, zssoft, zsc);
283 bus_intr_establish(zsc->zsc_bustag, SYS_INTR_SCC0, 0, 0, zshard, NULL);
285 evcnt_attach_dynamic(&zsc->zs_intrcnt, EVCNT_TYPE_INTR, NULL,
286 device_xname(self), "intr");
289 * Set the master interrupt enable and interrupt vector.
290 * (common to both channels, do it on A)
292 cs = zsc->zsc_cs[0];
293 s = splhigh();
294 /* interrupt vector */
295 zs_write_reg(cs, 2, zs_init_reg[2]);
296 /* master interrupt control (enable) */
297 zs_write_reg(cs, 9, zs_init_reg[9]);
298 splx(s);
301 static int
302 zs_print(void *aux, const char *name)
304 struct zsc_attach_args *args = aux;
306 if (name != NULL)
307 aprint_normal("%s: ", name);
309 if (args->channel != -1)
310 aprint_normal(" channel %d", args->channel);
312 return UNCONF;
316 * Our ZS chips all share a common, autovectored interrupt,
317 * so we have to look at all of them on each interrupt.
319 static int
320 zshard(void *arg)
322 struct zsc_softc *zsc;
323 int unit, rval, softreq;
325 rval = 0;
326 for (unit = 0; unit < zsc_cd.cd_ndevs; unit++) {
327 zsc = device_lookup_private(&zsc_cd, unit);
328 if (zsc == NULL)
329 continue;
330 rval |= zsc_intr_hard(zsc);
331 softreq = zsc->zsc_cs[0]->cs_softreq;
332 softreq |= zsc->zsc_cs[1]->cs_softreq;
333 if (softreq && (zssoftpending == 0)) {
334 zssoftpending = 1;
335 softint_schedule(zsc->sc_si);
337 zsc->zs_intrcnt.ev_count++;
339 return rval;
343 * Similar scheme as for zshard (look at all of them)
345 void
346 zssoft(void *arg)
348 struct zsc_softc *zsc;
349 int s, unit;
351 /* This is not the only ISR on this IPL. */
352 if (zssoftpending == 0)
353 return;
356 * The soft intr. bit will be set by zshard only if
357 * the variable zssoftpending is zero. The order of
358 * these next two statements prevents our clearing
359 * the soft intr bit just after zshard has set it.
361 /*isr_soft_clear(ZSSOFT_PRI);*/
362 zssoftpending = 0;
364 /* Make sure we call the tty layer at spltty. */
365 s = spltty();
366 for (unit = 0; unit < zsc_cd.cd_ndevs; unit++) {
367 zsc = device_lookup_private(&zsc_cd, unit);
368 if (zsc == NULL)
369 continue;
370 (void)zsc_intr_soft(zsc);
372 splx(s);
373 return;
378 * Compute the current baud rate given a ZS channel.
380 static int
381 zs_get_speed(struct zs_chanstate *cs)
383 int tconst;
385 tconst = zs_read_reg(cs, 12);
386 tconst |= zs_read_reg(cs, 13) << 8;
387 return (TCONST_TO_BPS(cs->cs_brg_clk, tconst));
391 * MD functions for setting the baud rate and control modes.
394 zs_set_speed(struct zs_chanstate *cs, int bps)
396 int tconst, real_bps;
398 #if 0
399 while (!(zs_read_csr(cs) & ZSRR0_TX_READY))
400 {/*nop*/}
401 #endif
402 /* Wait for transmit buffer to empty */
403 if (bps == 0) {
404 return (0);
407 #ifdef DIAGNOSTIC
408 if (cs->cs_brg_clk == 0)
409 panic("zs_set_speed");
410 #endif
412 tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
413 if (tconst < 0)
414 return (EINVAL);
416 /* Convert back to make sure we can do it. */
417 real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
419 /* XXX - Allow some tolerance here? */
420 #if 0
421 if (real_bps != bps)
422 return (EINVAL);
423 #endif
425 cs->cs_preg[12] = tconst;
426 cs->cs_preg[13] = tconst >> 8;
428 /* Caller will stuff the pending registers. */
429 return (0);
433 zs_set_modes(struct zs_chanstate *cs, int cflag)
435 int s;
438 * Output hardware flow control on the chip is horrendous:
439 * if carrier detect drops, the receiver is disabled, and if
440 * CTS drops, the transmitter is stoped IN MID CHARACTER!
441 * Therefore, NEVER set the HFC bit, and instead use the
442 * status interrupt to detect CTS changes.
444 s = splzs();
445 cs->cs_rr0_pps = 0;
446 if ((cflag & (CLOCAL | MDMBUF)) != 0) {
447 cs->cs_rr0_dcd = 0;
448 if ((cflag & MDMBUF) == 0)
449 cs->cs_rr0_pps = ZSRR0_DCD;
450 } else
451 cs->cs_rr0_dcd = ZSRR0_DCD;
452 if ((cflag & CRTSCTS) != 0) {
453 cs->cs_wr5_dtr = ZSWR5_DTR;
454 cs->cs_wr5_rts = ZSWR5_RTS;
455 cs->cs_rr0_cts = ZSRR0_CTS;
456 } else if ((cflag & MDMBUF) != 0) {
457 cs->cs_wr5_dtr = 0;
458 cs->cs_wr5_rts = ZSWR5_DTR;
459 cs->cs_rr0_cts = ZSRR0_DCD;
460 } else {
461 cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
462 cs->cs_wr5_rts = 0;
463 cs->cs_rr0_cts = 0;
465 splx(s);
467 /* Caller will stuff the pending registers. */
468 return (0);
473 * Read or write the chip with suitable delays.
476 uint8_t
477 zs_read_reg(struct zs_chanstate *cs, uint8_t reg)
479 uint8_t val;
480 struct zs_channel *zsc = (struct zs_channel *)cs;
482 bus_space_write_1(zsc->cs_bustag, zsc->cs_regs, ZS_REG_CSR, reg);
483 ZS_DELAY();
484 val = bus_space_read_1(zsc->cs_bustag, zsc->cs_regs, ZS_REG_CSR);
485 ZS_DELAY();
486 return val;
489 void
490 zs_write_reg(struct zs_chanstate *cs, uint8_t reg, uint8_t val)
492 struct zs_channel *zsc = (struct zs_channel *)cs;
494 bus_space_write_1(zsc->cs_bustag, zsc->cs_regs, ZS_REG_CSR, reg);
495 ZS_DELAY();
496 bus_space_write_1(zsc->cs_bustag, zsc->cs_regs, ZS_REG_CSR, val);
497 ZS_DELAY();
500 uint8_t
501 zs_read_csr(struct zs_chanstate *cs)
503 struct zs_channel *zsc = (struct zs_channel *)cs;
504 uint8_t val;
506 val = bus_space_read_1(zsc->cs_bustag, zsc->cs_regs, ZS_REG_CSR);
507 ZS_DELAY();
508 return val;
511 void
512 zs_write_csr(struct zs_chanstate *cs, uint8_t val)
514 struct zs_channel *zsc = (struct zs_channel *)cs;
516 bus_space_write_1(zsc->cs_bustag, zsc->cs_regs, ZS_REG_CSR, val);
517 ZS_DELAY();
520 uint8_t
521 zs_read_data(struct zs_chanstate *cs)
523 struct zs_channel *zsc = (struct zs_channel *)cs;
524 uint8_t val;
526 val = bus_space_read_1(zsc->cs_bustag, zsc->cs_regs, ZS_REG_DATA);
527 ZS_DELAY();
528 return val;
531 void
532 zs_write_data(struct zs_chanstate *cs, uint8_t val)
534 struct zs_channel *zsc = (struct zs_channel *)cs;
536 bus_space_write_1(zsc->cs_bustag, zsc->cs_regs, ZS_REG_DATA, val);
537 ZS_DELAY();
540 void
541 zs_abort(struct zs_chanstate *cs)
544 #if defined(KGDB)
545 zskgdb(cs);
546 #elif defined(DDB)
547 Debugger();
548 #endif
552 /*********************************************************/
553 /* Polled character I/O functions for console and KGDB */
554 /*********************************************************/
556 struct zschan *
557 zs_get_chan_addr(int zs_unit, int channel)
559 struct zsdevice *addr;
560 struct zschan *zc;
562 if (zs_unit >= NZS)
563 return NULL;
565 addr = (struct zsdevice *) ZS0_ADDR;
567 if (channel == 0) {
568 zc = &addr->zs_chan_a;
569 } else {
570 zc = &addr->zs_chan_b;
572 return (zc);
576 zs_getc(void *arg)
578 volatile struct zschan *zc = arg;
579 int s, c;
580 uint8_t rr0;
582 s = splhigh();
583 /* Wait for a character to arrive. */
584 do {
585 rr0 = zc->zc_csr;
586 ZS_DELAY();
587 } while ((rr0 & ZSRR0_RX_READY) == 0);
589 c = zc->zc_data;
590 ZS_DELAY();
591 splx(s);
593 return (c);
597 * Polled output char.
599 void
600 zs_putc(void *arg, int c)
602 volatile struct zschan *zc = arg;
603 int s;
604 uint8_t rr0;
606 s = splhigh();
607 /* Wait for transmitter to become ready. */
608 do {
609 rr0 = zc->zc_csr;
610 ZS_DELAY();
611 } while ((rr0 & ZSRR0_TX_READY) == 0);
613 zc->zc_data = c;
614 wbflush();
615 ZS_DELAY();
616 splx(s);
619 /***************************************************************/
621 static void zscnprobe(struct consdev *);
622 static void zscninit(struct consdev *);
623 static int zscngetc(dev_t);
624 static void zscnputc(dev_t, int);
625 static void zscnpollc(dev_t, int);
627 static int cons_port;
629 struct consdev consdev_zs = {
630 zscnprobe,
631 zscninit,
632 zscngetc,
633 zscnputc,
634 zscnpollc
637 void
638 zscnprobe(struct consdev *cn)
642 void
643 zscninit(struct consdev *cn)
645 extern const struct cdevsw zstty_cdevsw;
647 cons_port = prom_getconsole();
648 cn->cn_dev = makedev(cdevsw_lookup_major(&zstty_cdevsw), cons_port);
649 cn->cn_pri = CN_REMOTE;
650 zs_hwflags[0][cons_port] = ZS_HWFLAG_CONSOLE;
654 zscngetc(dev_t dev)
656 struct zschan *zs;
658 zs = zs_get_chan_addr(0, cons_port);
659 return zs_getc(zs);
662 void
663 zscnputc(dev_t dev, int c)
665 struct zschan *zs;
667 zs = zs_get_chan_addr(0, cons_port);
668 zs_putc(zs, c);
671 void
672 zscnpollc(dev_t dev, int on)