No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / newsmips / dev / zs.c
blob9bbd7a19c2f68e4eb3c51b7e2abd7c6467cdf4bf
1 /* $NetBSD: zs.c,v 1.24 2008/04/28 20:23:30 martin Exp $ */
3 /*-
4 * Copyright (c) 1996 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.
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.
37 * Sun keyboard/mouse uses the zs_kbd/zs_ms slaves.
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: zs.c,v 1.24 2008/04/28 20:23:30 martin Exp $");
43 #include "opt_ddb.h"
45 #include <sys/param.h>
46 #include <sys/device.h>
47 #include <sys/tty.h>
48 #include <sys/systm.h>
49 #include <sys/cpu.h>
50 #include <sys/intr.h>
52 #include <machine/adrsmap.h>
53 #include <machine/z8530var.h>
55 #include <dev/ic/z8530reg.h>
57 #include "ioconf.h"
59 #define ZS_DELAY() (*zs_delay)()
62 * Some warts needed by z8530tty.c -
63 * The default parity REALLY needs to be the same as the PROM uses,
64 * or you can not see messages done with printf during boot-up...
66 int zs_def_cflag = (CREAD | CS8 | HUPCL);
68 int
69 zs_print(void *aux, const char *name)
71 struct zsc_attach_args *args = aux;
73 if (name != NULL)
74 aprint_normal("%s: ", name);
76 if (args->channel != -1)
77 aprint_normal(" channel %d", args->channel);
79 return UNCONF;
83 * Our ZS chips all share a common, autovectored interrupt,
84 * so we have to look at all of them on each interrupt.
86 int
87 zshard(void *arg)
89 struct zsc_softc *zsc;
90 int unit, rval, softreq;
92 rval = 0;
93 for (unit = 0; unit < zsc_cd.cd_ndevs; unit++) {
94 zsc = device_lookup_private(&zsc_cd, unit);
95 if (zsc == NULL)
96 continue;
97 rval |= zsc_intr_hard(zsc);
98 softreq = zsc->zsc_cs[0]->cs_softreq;
99 softreq |= zsc->zsc_cs[1]->cs_softreq;
100 if (softreq)
101 softint_schedule(zsc->zsc_si);
104 return rval;
108 * Similar scheme as for zshard (look at all of them)
110 void
111 zssoft(void *arg)
113 struct zsc_softc *zsc;
114 int s, unit;
116 /* Make sure we call the tty layer at spltty. */
117 s = spltty();
118 for (unit = 0; unit < zsc_cd.cd_ndevs; unit++) {
119 zsc = device_lookup_private(&zsc_cd, unit);
120 if (zsc == NULL)
121 continue;
122 (void)zsc_intr_soft(zsc);
124 splx(s);
128 * Compute the current baud rate given a ZS channel.
131 zs_get_speed(struct zs_chanstate *cs)
133 int tconst;
135 tconst = zs_read_reg(cs, 12);
136 tconst |= zs_read_reg(cs, 13) << 8;
137 return TCONST_TO_BPS(cs->cs_brg_clk, tconst);
141 * MD functions for setting the baud rate and control modes.
144 zs_set_speed(struct zs_chanstate *cs, int bps)
146 int tconst, real_bps;
148 if (bps == 0)
149 return 0;
151 #ifdef DIAGNOSTIC
152 if (cs->cs_brg_clk == 0)
153 panic("zs_set_speed");
154 #endif
156 tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
157 if (tconst < 0)
158 return EINVAL;
160 /* Convert back to make sure we can do it. */
161 real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
163 /* XXX - Allow some tolerance here? */
164 if (real_bps != bps)
165 return EINVAL;
167 cs->cs_preg[12] = tconst;
168 cs->cs_preg[13] = tconst >> 8;
170 /* Caller will stuff the pending registers. */
171 return 0;
175 zs_set_modes(struct zs_chanstate *cs, int cflag)
177 int s;
180 * Output hardware flow control on the chip is horrendous:
181 * if carrier detect drops, the receiver is disabled, and if
182 * CTS drops, the transmitter is stoped IN MID CHARACTER!
183 * Therefore, NEVER set the HFC bit, and instead use the
184 * status interrupt to detect CTS changes.
186 s = splserial();
187 cs->cs_rr0_pps = 0;
188 if ((cflag & (CLOCAL | MDMBUF)) != 0) {
189 cs->cs_rr0_dcd = 0;
190 if ((cflag & MDMBUF) == 0)
191 cs->cs_rr0_pps = ZSRR0_DCD;
192 } else
193 cs->cs_rr0_dcd = ZSRR0_DCD;
194 if ((cflag & CRTSCTS) != 0) {
195 cs->cs_wr5_dtr = ZSWR5_DTR;
196 cs->cs_wr5_rts = ZSWR5_RTS;
197 cs->cs_rr0_cts = ZSRR0_CTS;
198 } else if ((cflag & MDMBUF) != 0) {
199 cs->cs_wr5_dtr = 0;
200 cs->cs_wr5_rts = ZSWR5_DTR;
201 cs->cs_rr0_cts = ZSRR0_DCD;
202 } else {
203 cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
204 cs->cs_wr5_rts = 0;
205 cs->cs_rr0_cts = 0;
207 splx(s);
209 /* Caller will stuff the pending registers. */
210 return 0;
214 * Read or write the chip with suitable delays.
217 uint8_t
218 zs_read_reg(struct zs_chanstate *cs, uint8_t reg)
220 uint8_t val;
222 *cs->cs_reg_csr = reg;
223 ZS_DELAY();
224 val = *cs->cs_reg_csr;
225 ZS_DELAY();
226 return val;
229 void
230 zs_write_reg(struct zs_chanstate *cs, uint8_t reg, uint8_t val)
233 *cs->cs_reg_csr = reg;
234 ZS_DELAY();
235 *cs->cs_reg_csr = val;
236 ZS_DELAY();
239 uint8_t
240 zs_read_csr(struct zs_chanstate *cs)
242 uint8_t val;
244 val = *cs->cs_reg_csr;
245 ZS_DELAY();
246 return val;
249 void
250 zs_write_csr(struct zs_chanstate *cs, uint8_t val)
253 *cs->cs_reg_csr = val;
254 ZS_DELAY();
257 uint8_t
258 zs_read_data(struct zs_chanstate *cs)
260 uint8_t val;
262 val = *cs->cs_reg_data;
263 ZS_DELAY();
264 return val;
267 void
268 zs_write_data(struct zs_chanstate *cs, uint8_t val)
271 *cs->cs_reg_data = val;
272 ZS_DELAY();
275 void
276 zs_abort(struct zs_chanstate *cs)
279 #ifdef DDB
280 Debugger();
281 #endif