zs.c revision 1.10 1 /* $NetBSD: zs.c,v 1.10 1999/12/22 05:55:25 tsubai 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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Zilog Z8530 Dual UART driver (machine-dependent part)
41 *
42 * Runs two serial lines per chip using slave drivers.
43 * Plain tty/async lines use the zs_async slave.
44 * Sun keyboard/mouse uses the zs_kbd/zs_ms slaves.
45 */
46
47 #include "opt_ddb.h"
48
49 #include <sys/param.h>
50 #include <sys/device.h>
51 #include <sys/tty.h>
52 #include <sys/systm.h>
53
54 #include <machine/adrsmap.h>
55 #include <machine/cpu.h>
56 #include <machine/z8530var.h>
57
58 #include <dev/ic/z8530reg.h>
59
60 #define ZS_DELAY() (*zs_delay)()
61
62 int zs_print __P((void *, const char *name));
63 int zs_get_speed __P((struct zs_chanstate *));
64 void Debugger __P((void));
65 void (*zs_delay) __P((void));
66
67 /*
68 * Some warts needed by z8530tty.c -
69 * The default parity REALLY needs to be the same as the PROM uses,
70 * or you can not see messages done with printf during boot-up...
71 */
72 int zs_def_cflag = (CREAD | CS8 | HUPCL);
73 int zs_major = 1;
74
75 int
76 zs_print(aux, name)
77 void *aux;
78 const char *name;
79 {
80 struct zsc_attach_args *args = aux;
81
82 if (name != NULL)
83 printf("%s: ", name);
84
85 if (args->channel != -1)
86 printf(" channel %d", args->channel);
87
88 return UNCONF;
89 }
90
91 /*
92 * Compute the current baud rate given a ZS channel.
93 */
94 int
95 zs_get_speed(cs)
96 struct zs_chanstate *cs;
97 {
98 int tconst;
99
100 tconst = zs_read_reg(cs, 12);
101 tconst |= zs_read_reg(cs, 13) << 8;
102 return (TCONST_TO_BPS(cs->cs_brg_clk, tconst));
103 }
104
105 /*
106 * MD functions for setting the baud rate and control modes.
107 */
108 int
109 zs_set_speed(cs, bps)
110 struct zs_chanstate *cs;
111 int bps; /* bits per second */
112 {
113 int tconst, real_bps;
114
115 if (bps == 0)
116 return (0);
117
118 #ifdef DIAGNOSTIC
119 if (cs->cs_brg_clk == 0)
120 panic("zs_set_speed");
121 #endif
122
123 tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
124 if (tconst < 0)
125 return (EINVAL);
126
127 /* Convert back to make sure we can do it. */
128 real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
129
130 /* XXX - Allow some tolerance here? */
131 if (real_bps != bps)
132 return (EINVAL);
133
134 cs->cs_preg[12] = tconst;
135 cs->cs_preg[13] = tconst >> 8;
136
137 /* Caller will stuff the pending registers. */
138 return (0);
139 }
140
141 int
142 zs_set_modes(cs, cflag)
143 struct zs_chanstate *cs;
144 int cflag; /* bits per second */
145 {
146 int s;
147
148 /*
149 * Output hardware flow control on the chip is horrendous:
150 * if carrier detect drops, the receiver is disabled, and if
151 * CTS drops, the transmitter is stoped IN MID CHARACTER!
152 * Therefore, NEVER set the HFC bit, and instead use the
153 * status interrupt to detect CTS changes.
154 */
155 s = splzs();
156 cs->cs_rr0_pps = 0;
157 if ((cflag & (CLOCAL | MDMBUF)) != 0) {
158 cs->cs_rr0_dcd = 0;
159 if ((cflag & MDMBUF) == 0)
160 cs->cs_rr0_pps = ZSRR0_DCD;
161 } else
162 cs->cs_rr0_dcd = ZSRR0_DCD;
163 if ((cflag & CRTSCTS) != 0) {
164 cs->cs_wr5_dtr = ZSWR5_DTR;
165 cs->cs_wr5_rts = ZSWR5_RTS;
166 cs->cs_rr0_cts = ZSRR0_CTS;
167 } else if ((cflag & MDMBUF) != 0) {
168 cs->cs_wr5_dtr = 0;
169 cs->cs_wr5_rts = ZSWR5_DTR;
170 cs->cs_rr0_cts = ZSRR0_DCD;
171 } else {
172 cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
173 cs->cs_wr5_rts = 0;
174 cs->cs_rr0_cts = 0;
175 }
176 splx(s);
177
178 /* Caller will stuff the pending registers. */
179 return (0);
180 }
181
182 /*
183 * Read or write the chip with suitable delays.
184 */
185
186 u_char
187 zs_read_reg(cs, reg)
188 struct zs_chanstate *cs;
189 u_char reg;
190 {
191 u_char val;
192
193 *cs->cs_reg_csr = reg;
194 ZS_DELAY();
195 val = *cs->cs_reg_csr;
196 ZS_DELAY();
197 return val;
198 }
199
200 void
201 zs_write_reg(cs, reg, val)
202 struct zs_chanstate *cs;
203 u_char reg, val;
204 {
205 *cs->cs_reg_csr = reg;
206 ZS_DELAY();
207 *cs->cs_reg_csr = val;
208 ZS_DELAY();
209 }
210
211 u_char zs_read_csr(cs)
212 struct zs_chanstate *cs;
213 {
214 register u_char val;
215
216 val = *cs->cs_reg_csr;
217 ZS_DELAY();
218 return val;
219 }
220
221 void zs_write_csr(cs, val)
222 struct zs_chanstate *cs;
223 u_char val;
224 {
225 *cs->cs_reg_csr = val;
226 ZS_DELAY();
227 }
228
229 u_char zs_read_data(cs)
230 struct zs_chanstate *cs;
231 {
232 register u_char val;
233
234 val = *cs->cs_reg_data;
235 ZS_DELAY();
236 return val;
237 }
238
239 void zs_write_data(cs, val)
240 struct zs_chanstate *cs;
241 u_char val;
242 {
243 *cs->cs_reg_data = val;
244 ZS_DELAY();
245 }
246
247 void
248 zs_abort(cs)
249 struct zs_chanstate *cs;
250 {
251 #ifdef DDB
252 Debugger();
253 #endif
254 }
255