zs.c revision 1.1.18.3 1 1.1.18.3 yamt /* $NetBSD: zs.c,v 1.1.18.3 2006/12/30 20:45:55 yamt Exp $ */
2 1.1.18.2 yamt
3 1.1.18.2 yamt /*-
4 1.1.18.2 yamt * Copyright (c) 1996, 2005 The NetBSD Foundation, Inc.
5 1.1.18.2 yamt * All rights reserved.
6 1.1.18.2 yamt *
7 1.1.18.2 yamt * This code is derived from software contributed to The NetBSD Foundation
8 1.1.18.2 yamt * by Gordon W. Ross.
9 1.1.18.2 yamt *
10 1.1.18.2 yamt * Redistribution and use in source and binary forms, with or without
11 1.1.18.2 yamt * modification, are permitted provided that the following conditions
12 1.1.18.2 yamt * are met:
13 1.1.18.2 yamt * 1. Redistributions of source code must retain the above copyright
14 1.1.18.2 yamt * notice, this list of conditions and the following disclaimer.
15 1.1.18.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.18.2 yamt * notice, this list of conditions and the following disclaimer in the
17 1.1.18.2 yamt * documentation and/or other materials provided with the distribution.
18 1.1.18.2 yamt * 3. All advertising materials mentioning features or use of this software
19 1.1.18.2 yamt * must display the following acknowledgement:
20 1.1.18.2 yamt * This product includes software developed by the NetBSD
21 1.1.18.2 yamt * Foundation, Inc. and its contributors.
22 1.1.18.2 yamt * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1.18.2 yamt * contributors may be used to endorse or promote products derived
24 1.1.18.2 yamt * from this software without specific prior written permission.
25 1.1.18.2 yamt *
26 1.1.18.2 yamt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1.18.2 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1.18.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1.18.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1.18.2 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1.18.2 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1.18.2 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1.18.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1.18.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1.18.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1.18.2 yamt * POSSIBILITY OF SUCH DAMAGE.
37 1.1.18.2 yamt */
38 1.1.18.2 yamt
39 1.1.18.2 yamt /*
40 1.1.18.2 yamt * Zilog Z8530 Dual UART driver (machine-dependent part)
41 1.1.18.2 yamt *
42 1.1.18.2 yamt * Runs two serial lines per chip using slave drivers.
43 1.1.18.2 yamt * Plain tty/async lines use the zs_async slave.
44 1.1.18.2 yamt */
45 1.1.18.2 yamt
46 1.1.18.2 yamt #include <sys/cdefs.h>
47 1.1.18.3 yamt __KERNEL_RCSID(0, "$NetBSD: zs.c,v 1.1.18.3 2006/12/30 20:45:55 yamt Exp $");
48 1.1.18.2 yamt
49 1.1.18.2 yamt #include "opt_ddb.h"
50 1.1.18.2 yamt
51 1.1.18.2 yamt #include <sys/param.h>
52 1.1.18.2 yamt #include <sys/device.h>
53 1.1.18.2 yamt #include <sys/tty.h>
54 1.1.18.2 yamt #include <sys/systm.h>
55 1.1.18.2 yamt
56 1.1.18.2 yamt #include <machine/z8530var.h>
57 1.1.18.2 yamt #include <dev/ic/z8530reg.h>
58 1.1.18.2 yamt
59 1.1.18.2 yamt #include "ioconf.h"
60 1.1.18.2 yamt
61 1.1.18.2 yamt /* console status for consinit() */
62 1.1.18.2 yamt static struct zs_chanstate zs_conscs_store;
63 1.1.18.2 yamt struct zs_chanstate *zs_conscs = &zs_conscs_store;
64 1.1.18.2 yamt void *zs_consaddr;
65 1.1.18.2 yamt
66 1.1.18.2 yamt /*
67 1.1.18.2 yamt * Some warts needed by z8530tty.c -
68 1.1.18.2 yamt * The default parity REALLY needs to be the same as the PROM uses,
69 1.1.18.2 yamt * or you can not see messages done with printf during boot-up...
70 1.1.18.2 yamt */
71 1.1.18.2 yamt int zs_def_cflag = (CREAD | CS8 | HUPCL);
72 1.1.18.2 yamt
73 1.1.18.2 yamt int
74 1.1.18.2 yamt zs_print(void *aux, const char *name)
75 1.1.18.2 yamt {
76 1.1.18.2 yamt struct zsc_attach_args *args = aux;
77 1.1.18.2 yamt
78 1.1.18.2 yamt if (name != NULL)
79 1.1.18.2 yamt aprint_normal("%s: ", name);
80 1.1.18.2 yamt
81 1.1.18.2 yamt if (args->channel != -1)
82 1.1.18.2 yamt aprint_normal(" channel %d", args->channel);
83 1.1.18.2 yamt
84 1.1.18.2 yamt return UNCONF;
85 1.1.18.2 yamt }
86 1.1.18.2 yamt
87 1.1.18.2 yamt int
88 1.1.18.2 yamt zshard(void *arg)
89 1.1.18.2 yamt {
90 1.1.18.2 yamt struct zsc_softc *zsc;
91 1.1.18.3 yamt int rval;
92 1.1.18.2 yamt
93 1.1.18.3 yamt zsc = arg;
94 1.1.18.3 yamt rval = zsc_intr_hard(zsc);
95 1.1.18.3 yamt if (zsc->zsc_cs[0]->cs_softreq || zsc->zsc_cs[1]->cs_softreq)
96 1.1.18.3 yamt softintr_schedule(zsc->zsc_si);
97 1.1.18.2 yamt
98 1.1.18.2 yamt return rval;
99 1.1.18.2 yamt }
100 1.1.18.2 yamt
101 1.1.18.2 yamt /*
102 1.1.18.2 yamt * Compute the current baud rate given a ZS channel.
103 1.1.18.2 yamt */
104 1.1.18.2 yamt int
105 1.1.18.2 yamt zs_get_speed(struct zs_chanstate *cs)
106 1.1.18.2 yamt {
107 1.1.18.2 yamt int tconst;
108 1.1.18.2 yamt
109 1.1.18.2 yamt tconst = zs_read_reg(cs, 12);
110 1.1.18.2 yamt tconst |= zs_read_reg(cs, 13) << 8;
111 1.1.18.2 yamt return TCONST_TO_BPS(cs->cs_brg_clk, tconst);
112 1.1.18.2 yamt }
113 1.1.18.2 yamt
114 1.1.18.2 yamt /*
115 1.1.18.2 yamt * MD functions for setting the baud rate and control modes.
116 1.1.18.2 yamt */
117 1.1.18.2 yamt int
118 1.1.18.2 yamt zs_set_speed(struct zs_chanstate *cs, int bps)
119 1.1.18.2 yamt {
120 1.1.18.2 yamt int tconst, real_bps;
121 1.1.18.2 yamt
122 1.1.18.2 yamt if (bps == 0)
123 1.1.18.2 yamt return 0;
124 1.1.18.2 yamt
125 1.1.18.2 yamt #ifdef DIAGNOSTIC
126 1.1.18.2 yamt if (cs->cs_brg_clk == 0)
127 1.1.18.2 yamt panic("zs_set_speed");
128 1.1.18.2 yamt #endif
129 1.1.18.2 yamt
130 1.1.18.2 yamt tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
131 1.1.18.2 yamt if (tconst < 0)
132 1.1.18.2 yamt return EINVAL;
133 1.1.18.2 yamt
134 1.1.18.2 yamt /* Convert back to make sure we can do it. */
135 1.1.18.2 yamt real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
136 1.1.18.2 yamt
137 1.1.18.2 yamt /* XXX - Allow some tolerance here? */
138 1.1.18.2 yamt if (real_bps != bps)
139 1.1.18.2 yamt return EINVAL;
140 1.1.18.2 yamt
141 1.1.18.2 yamt cs->cs_preg[12] = tconst;
142 1.1.18.2 yamt cs->cs_preg[13] = tconst >> 8;
143 1.1.18.2 yamt
144 1.1.18.2 yamt /* Caller will stuff the pending registers. */
145 1.1.18.2 yamt return 0;
146 1.1.18.2 yamt }
147 1.1.18.2 yamt
148 1.1.18.2 yamt int
149 1.1.18.2 yamt zs_set_modes(struct zs_chanstate *cs, int cflag)
150 1.1.18.2 yamt {
151 1.1.18.2 yamt int s;
152 1.1.18.2 yamt
153 1.1.18.2 yamt /*
154 1.1.18.2 yamt * Output hardware flow control on the chip is horrendous:
155 1.1.18.2 yamt * if carrier detect drops, the receiver is disabled, and if
156 1.1.18.2 yamt * CTS drops, the transmitter is stoped IN MID CHARACTER!
157 1.1.18.2 yamt * Therefore, NEVER set the HFC bit, and instead use the
158 1.1.18.2 yamt * status interrupt to detect CTS changes.
159 1.1.18.2 yamt */
160 1.1.18.2 yamt s = splserial();
161 1.1.18.2 yamt cs->cs_rr0_pps = 0;
162 1.1.18.2 yamt if ((cflag & (CLOCAL | MDMBUF)) != 0) {
163 1.1.18.2 yamt cs->cs_rr0_dcd = 0;
164 1.1.18.2 yamt if ((cflag & MDMBUF) == 0)
165 1.1.18.2 yamt cs->cs_rr0_pps = ZSRR0_DCD;
166 1.1.18.2 yamt } else
167 1.1.18.2 yamt cs->cs_rr0_dcd = ZSRR0_DCD;
168 1.1.18.2 yamt if ((cflag & CRTSCTS) != 0) {
169 1.1.18.2 yamt cs->cs_wr5_dtr = ZSWR5_DTR;
170 1.1.18.2 yamt cs->cs_wr5_rts = ZSWR5_RTS;
171 1.1.18.2 yamt cs->cs_rr0_cts = ZSRR0_CTS;
172 1.1.18.2 yamt } else if ((cflag & MDMBUF) != 0) {
173 1.1.18.2 yamt cs->cs_wr5_dtr = 0;
174 1.1.18.2 yamt cs->cs_wr5_rts = ZSWR5_DTR;
175 1.1.18.2 yamt cs->cs_rr0_cts = ZSRR0_DCD;
176 1.1.18.2 yamt } else {
177 1.1.18.2 yamt cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
178 1.1.18.2 yamt cs->cs_wr5_rts = 0;
179 1.1.18.2 yamt cs->cs_rr0_cts = 0;
180 1.1.18.2 yamt }
181 1.1.18.2 yamt splx(s);
182 1.1.18.2 yamt
183 1.1.18.2 yamt /* Caller will stuff the pending registers. */
184 1.1.18.2 yamt return 0;
185 1.1.18.2 yamt }
186 1.1.18.2 yamt
187 1.1.18.2 yamt /*
188 1.1.18.2 yamt * Read or write the chip with suitable delays.
189 1.1.18.2 yamt */
190 1.1.18.2 yamt uint8_t
191 1.1.18.2 yamt zs_read_reg(struct zs_chanstate *cs, uint8_t reg)
192 1.1.18.2 yamt {
193 1.1.18.2 yamt uint8_t val;
194 1.1.18.2 yamt
195 1.1.18.2 yamt *cs->cs_reg_csr = reg;
196 1.1.18.2 yamt
197 1.1.18.2 yamt val = *cs->cs_reg_csr;
198 1.1.18.2 yamt
199 1.1.18.2 yamt return val;
200 1.1.18.2 yamt }
201 1.1.18.2 yamt
202 1.1.18.2 yamt void
203 1.1.18.2 yamt zs_write_reg(struct zs_chanstate *cs, uint8_t reg, uint8_t val)
204 1.1.18.2 yamt {
205 1.1.18.2 yamt
206 1.1.18.2 yamt *cs->cs_reg_csr = reg;
207 1.1.18.2 yamt
208 1.1.18.2 yamt *cs->cs_reg_csr = val;
209 1.1.18.2 yamt
210 1.1.18.2 yamt }
211 1.1.18.2 yamt
212 1.1.18.2 yamt uint8_t
213 1.1.18.2 yamt zs_read_csr(struct zs_chanstate *cs)
214 1.1.18.2 yamt {
215 1.1.18.2 yamt uint8_t val;
216 1.1.18.2 yamt
217 1.1.18.2 yamt val = *cs->cs_reg_csr;
218 1.1.18.2 yamt
219 1.1.18.2 yamt return val;
220 1.1.18.2 yamt }
221 1.1.18.2 yamt
222 1.1.18.2 yamt void
223 1.1.18.2 yamt zs_write_csr(struct zs_chanstate *cs, uint8_t val)
224 1.1.18.2 yamt {
225 1.1.18.2 yamt
226 1.1.18.2 yamt *cs->cs_reg_csr = val;
227 1.1.18.2 yamt
228 1.1.18.2 yamt }
229 1.1.18.2 yamt
230 1.1.18.2 yamt uint8_t
231 1.1.18.2 yamt zs_read_data(struct zs_chanstate *cs)
232 1.1.18.2 yamt {
233 1.1.18.2 yamt uint8_t val;
234 1.1.18.2 yamt
235 1.1.18.2 yamt val = *cs->cs_reg_data;
236 1.1.18.2 yamt
237 1.1.18.2 yamt return val;
238 1.1.18.2 yamt }
239 1.1.18.2 yamt
240 1.1.18.2 yamt void
241 1.1.18.2 yamt zs_write_data(struct zs_chanstate *cs, uint8_t val)
242 1.1.18.2 yamt {
243 1.1.18.2 yamt
244 1.1.18.2 yamt *cs->cs_reg_data = val;
245 1.1.18.2 yamt }
246 1.1.18.2 yamt
247 1.1.18.2 yamt void
248 1.1.18.2 yamt zs_abort(struct zs_chanstate *cs)
249 1.1.18.2 yamt {
250 1.1.18.2 yamt
251 1.1.18.2 yamt #ifdef DDB
252 1.1.18.2 yamt Debugger();
253 1.1.18.2 yamt #endif
254 1.1.18.2 yamt }
255 1.1.18.2 yamt
256 1.1.18.2 yamt /*
257 1.1.18.2 yamt * Polled input char.
258 1.1.18.2 yamt */
259 1.1.18.2 yamt int
260 1.1.18.2 yamt zs_getc(void *arg)
261 1.1.18.2 yamt {
262 1.1.18.2 yamt struct zs_chanstate *cs = arg;
263 1.1.18.2 yamt int s, c, rr0;
264 1.1.18.2 yamt
265 1.1.18.2 yamt s = splhigh();
266 1.1.18.2 yamt /* Wait for a character to arrive. */
267 1.1.18.2 yamt do {
268 1.1.18.2 yamt rr0 = *cs->cs_reg_csr;
269 1.1.18.2 yamt ZS_DELAY();
270 1.1.18.2 yamt } while ((rr0 & ZSRR0_RX_READY) == 0);
271 1.1.18.2 yamt
272 1.1.18.2 yamt c = *cs->cs_reg_data;
273 1.1.18.2 yamt ZS_DELAY();
274 1.1.18.2 yamt splx(s);
275 1.1.18.2 yamt
276 1.1.18.2 yamt /*
277 1.1.18.2 yamt * This could be used by the kd driver to read scan codes,
278 1.1.18.2 yamt * so don't translate '\r' ==> '\n' here...
279 1.1.18.2 yamt */
280 1.1.18.2 yamt return c;
281 1.1.18.2 yamt }
282 1.1.18.2 yamt
283 1.1.18.2 yamt /*
284 1.1.18.2 yamt * Polled output char.
285 1.1.18.2 yamt */
286 1.1.18.2 yamt void
287 1.1.18.2 yamt zs_putc(void *arg, int c)
288 1.1.18.2 yamt {
289 1.1.18.2 yamt struct zs_chanstate *cs = arg;
290 1.1.18.2 yamt int s, rr0;
291 1.1.18.2 yamt
292 1.1.18.2 yamt s = splhigh();
293 1.1.18.2 yamt /* Wait for transmitter to become ready. */
294 1.1.18.2 yamt do {
295 1.1.18.2 yamt rr0 = *cs->cs_reg_csr;
296 1.1.18.2 yamt ZS_DELAY();
297 1.1.18.2 yamt } while ((rr0 & ZSRR0_TX_READY) == 0);
298 1.1.18.2 yamt
299 1.1.18.2 yamt *cs->cs_reg_data = c;
300 1.1.18.2 yamt ZS_DELAY();
301 1.1.18.2 yamt splx(s);
302 1.1.18.2 yamt }
303 1.1.18.2 yamt
304 1.1.18.2 yamt int
305 1.1.18.2 yamt zscngetc(dev_t dev)
306 1.1.18.2 yamt {
307 1.1.18.2 yamt
308 1.1.18.2 yamt return zs_getc((void *)zs_conscs);
309 1.1.18.2 yamt }
310 1.1.18.2 yamt
311 1.1.18.2 yamt void
312 1.1.18.2 yamt zscnputc(dev_t dev, int c)
313 1.1.18.2 yamt {
314 1.1.18.2 yamt
315 1.1.18.2 yamt zs_putc((void *)zs_conscs, c);
316 1.1.18.2 yamt }
317