z8530sc.c revision 1.1 1 1.1 gwr /* $NetBSD: z8530sc.c,v 1.1 1996/01/24 01:07:23 gwr Exp $ */
2 1.1 gwr
3 1.1 gwr /*
4 1.1 gwr * Copyright (c) 1994 Gordon W. Ross
5 1.1 gwr * Copyright (c) 1992, 1993
6 1.1 gwr * The Regents of the University of California. All rights reserved.
7 1.1 gwr *
8 1.1 gwr * This software was developed by the Computer Systems Engineering group
9 1.1 gwr * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
10 1.1 gwr * contributed to Berkeley.
11 1.1 gwr *
12 1.1 gwr * All advertising materials mentioning features or use of this software
13 1.1 gwr * must display the following acknowledgement:
14 1.1 gwr * This product includes software developed by the University of
15 1.1 gwr * California, Lawrence Berkeley Laboratory.
16 1.1 gwr *
17 1.1 gwr * Redistribution and use in source and binary forms, with or without
18 1.1 gwr * modification, are permitted provided that the following conditions
19 1.1 gwr * are met:
20 1.1 gwr * 1. Redistributions of source code must retain the above copyright
21 1.1 gwr * notice, this list of conditions and the following disclaimer.
22 1.1 gwr * 2. Redistributions in binary form must reproduce the above copyright
23 1.1 gwr * notice, this list of conditions and the following disclaimer in the
24 1.1 gwr * documentation and/or other materials provided with the distribution.
25 1.1 gwr * 3. All advertising materials mentioning features or use of this software
26 1.1 gwr * must display the following acknowledgement:
27 1.1 gwr * This product includes software developed by the University of
28 1.1 gwr * California, Berkeley and its contributors.
29 1.1 gwr * 4. Neither the name of the University nor the names of its contributors
30 1.1 gwr * may be used to endorse or promote products derived from this software
31 1.1 gwr * without specific prior written permission.
32 1.1 gwr *
33 1.1 gwr * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
34 1.1 gwr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35 1.1 gwr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 1.1 gwr * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
37 1.1 gwr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38 1.1 gwr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39 1.1 gwr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 1.1 gwr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41 1.1 gwr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42 1.1 gwr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 1.1 gwr * SUCH DAMAGE.
44 1.1 gwr *
45 1.1 gwr * @(#)zs.c 8.1 (Berkeley) 7/19/93
46 1.1 gwr */
47 1.1 gwr
48 1.1 gwr /*
49 1.1 gwr * Zilog Z8530 Dual UART driver (common part)
50 1.1 gwr *
51 1.1 gwr * This file contains the machine-independent parts of the
52 1.1 gwr * driver common to tty and keyboard/mouse sub-drivers.
53 1.1 gwr */
54 1.1 gwr
55 1.1 gwr #include <sys/param.h>
56 1.1 gwr #include <sys/systm.h>
57 1.1 gwr #include <sys/proc.h>
58 1.1 gwr #include <sys/device.h>
59 1.1 gwr #include <sys/conf.h>
60 1.1 gwr #include <sys/file.h>
61 1.1 gwr #include <sys/ioctl.h>
62 1.1 gwr #include <sys/tty.h>
63 1.1 gwr #include <sys/time.h>
64 1.1 gwr #include <sys/kernel.h>
65 1.1 gwr #include <sys/syslog.h>
66 1.1 gwr
67 1.1 gwr #include <dev/ic/z8530reg.h>
68 1.1 gwr #include <machine/z8530var.h>
69 1.1 gwr
70 1.1 gwr int
71 1.1 gwr zs_break(cs, set)
72 1.1 gwr struct zs_chanstate *cs;
73 1.1 gwr int set;
74 1.1 gwr {
75 1.1 gwr int s;
76 1.1 gwr
77 1.1 gwr s = splzs();
78 1.1 gwr if (set) {
79 1.1 gwr cs->cs_preg[5] |= ZSWR5_BREAK;
80 1.1 gwr cs->cs_creg[5] |= ZSWR5_BREAK;
81 1.1 gwr } else {
82 1.1 gwr cs->cs_preg[5] &= ~ZSWR5_BREAK;
83 1.1 gwr cs->cs_creg[5] &= ~ZSWR5_BREAK;
84 1.1 gwr }
85 1.1 gwr ZS_WRITE(cs, 5, cs->cs_creg[5]);
86 1.1 gwr splx(s);
87 1.1 gwr }
88 1.1 gwr
89 1.1 gwr
90 1.1 gwr /*
91 1.1 gwr * Compute the current baud rate given a ZSCC channel.
92 1.1 gwr */
93 1.1 gwr int
94 1.1 gwr zs_getspeed(cs)
95 1.1 gwr struct zs_chanstate *cs;
96 1.1 gwr {
97 1.1 gwr int tconst;
98 1.1 gwr
99 1.1 gwr tconst = ZS_READ(cs, 12);
100 1.1 gwr tconst |= ZS_READ(cs, 13) << 8;
101 1.1 gwr return (TCONST_TO_BPS(cs->cs_pclk_div16, tconst));
102 1.1 gwr }
103 1.1 gwr
104 1.1 gwr /*
105 1.1 gwr * drain on-chip fifo
106 1.1 gwr */
107 1.1 gwr void
108 1.1 gwr zs_iflush(cs)
109 1.1 gwr struct zs_chanstate *cs;
110 1.1 gwr {
111 1.1 gwr u_char c, rr0, rr1;
112 1.1 gwr
113 1.1 gwr for (;;) {
114 1.1 gwr /* Is there input available? */
115 1.1 gwr rr0 = *(cs->cs_reg_csr);
116 1.1 gwr ZS_DELAY();
117 1.1 gwr if ((rr0 & ZSRR0_RX_READY) == 0)
118 1.1 gwr break;
119 1.1 gwr
120 1.1 gwr /* Read the data. */
121 1.1 gwr c = *(cs->cs_reg_data);
122 1.1 gwr ZS_DELAY();
123 1.1 gwr
124 1.1 gwr /* Need to read status register too? */
125 1.1 gwr rr1 = ZS_READ(cs, 1);
126 1.1 gwr if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
127 1.1 gwr /* Clear the receive error. */
128 1.1 gwr *(cs->cs_reg_csr) = ZSWR0_RESET_ERRORS;
129 1.1 gwr ZS_DELAY();
130 1.1 gwr }
131 1.1 gwr }
132 1.1 gwr }
133 1.1 gwr
134 1.1 gwr
135 1.1 gwr /*
136 1.1 gwr * Write the given register set to the given zs channel in the proper order.
137 1.1 gwr * The channel must not be transmitting at the time. The receiver will
138 1.1 gwr * be disabled for the time it takes to write all the registers.
139 1.1 gwr * Call this with interrupts disabled.
140 1.1 gwr */
141 1.1 gwr void
142 1.1 gwr zs_loadchannelregs(cs)
143 1.1 gwr struct zs_chanstate *cs;
144 1.1 gwr {
145 1.1 gwr u_char *reg;
146 1.1 gwr int i;
147 1.1 gwr
148 1.1 gwr /* Copy "pending" regs to "current" */
149 1.1 gwr bcopy((caddr_t)cs->cs_preg, (caddr_t)cs->cs_creg, 16);
150 1.1 gwr reg = cs->cs_creg; /* current regs */
151 1.1 gwr
152 1.1 gwr *(cs->cs_reg_csr) = ZSM_RESET_ERR; /* XXX: reset error condition */
153 1.1 gwr ZS_DELAY();
154 1.1 gwr
155 1.1 gwr #if 1
156 1.1 gwr /*
157 1.1 gwr * XXX: Is this really a good idea?
158 1.1 gwr * XXX: Should go elsewhere! -gwr
159 1.1 gwr */
160 1.1 gwr zs_iflush(cs); /* XXX */
161 1.1 gwr #endif
162 1.1 gwr
163 1.1 gwr /* baud clock divisor, stop bits, parity */
164 1.1 gwr ZS_WRITE(cs, 4, reg[4]);
165 1.1 gwr
166 1.1 gwr /* misc. TX/RX control bits */
167 1.1 gwr ZS_WRITE(cs, 10, reg[10]);
168 1.1 gwr
169 1.1 gwr /* char size, enable (RX/TX) */
170 1.1 gwr ZS_WRITE(cs, 3, reg[3] & ~ZSWR3_RX_ENABLE);
171 1.1 gwr ZS_WRITE(cs, 5, reg[5] & ~ZSWR5_TX_ENABLE);
172 1.1 gwr
173 1.1 gwr /* interrupt enables: TX, TX, STATUS */
174 1.1 gwr ZS_WRITE(cs, 1, reg[1]);
175 1.1 gwr
176 1.1 gwr #if 0
177 1.1 gwr /*
178 1.1 gwr * Registers 2 and 9 are special because they are
179 1.1 gwr * actually common to both channels, but must be
180 1.1 gwr * programmed through channel A. The "zsc" attach
181 1.1 gwr * function takes care of setting these registers
182 1.1 gwr * and they should not be touched thereafter.
183 1.1 gwr */
184 1.1 gwr /* interrupt vector */
185 1.1 gwr ZS_WRITE(cs, 2, reg[2]);
186 1.1 gwr /* master interrupt control */
187 1.1 gwr ZS_WRITE(cs, 9, reg[9]);
188 1.1 gwr #endif
189 1.1 gwr
190 1.1 gwr /* clock mode control */
191 1.1 gwr ZS_WRITE(cs, 11, reg[11]);
192 1.1 gwr
193 1.1 gwr /* baud rate (lo/hi) */
194 1.1 gwr ZS_WRITE(cs, 12, reg[12]);
195 1.1 gwr ZS_WRITE(cs, 13, reg[13]);
196 1.1 gwr
197 1.1 gwr /* Misc. control bits */
198 1.1 gwr ZS_WRITE(cs, 14, reg[14]);
199 1.1 gwr
200 1.1 gwr /* which lines cause status interrupts */
201 1.1 gwr ZS_WRITE(cs, 15, reg[15]);
202 1.1 gwr
203 1.1 gwr /* char size, enable (RX/TX)*/
204 1.1 gwr ZS_WRITE(cs, 3, reg[3]);
205 1.1 gwr ZS_WRITE(cs, 5, reg[5]);
206 1.1 gwr }
207 1.1 gwr
208 1.1 gwr
209 1.1 gwr /*
210 1.1 gwr * ZS hardware interrupt. Scan all ZS channels. NB: we know here that
211 1.1 gwr * channels are kept in (A,B) pairs.
212 1.1 gwr *
213 1.1 gwr * Do just a little, then get out; set a software interrupt if more
214 1.1 gwr * work is needed.
215 1.1 gwr *
216 1.1 gwr * We deliberately ignore the vectoring Zilog gives us, and match up
217 1.1 gwr * only the number of `reset interrupt under service' operations, not
218 1.1 gwr * the order.
219 1.1 gwr */
220 1.1 gwr int
221 1.1 gwr zsc_intr_hard(arg)
222 1.1 gwr void *arg;
223 1.1 gwr {
224 1.1 gwr register struct zsc_softc *zsc = arg;
225 1.1 gwr register struct zs_chanstate *cs_a;
226 1.1 gwr register struct zs_chanstate *cs_b;
227 1.1 gwr register int rval, soft;
228 1.1 gwr register u_char rr3;
229 1.1 gwr
230 1.1 gwr cs_a = &zsc->zsc_cs[0];
231 1.1 gwr cs_b = &zsc->zsc_cs[1];
232 1.1 gwr rval = 0;
233 1.1 gwr soft = 0;
234 1.1 gwr
235 1.1 gwr /* Note: only channel A has an RR3 */
236 1.1 gwr rr3 = ZS_READ(cs_a, 3);
237 1.1 gwr
238 1.1 gwr /* Handle receive interrupts first. */
239 1.1 gwr if (rr3 & ZSRR3_IP_A_RX)
240 1.1 gwr (*cs_a->cs_ops->zsop_rxint)(cs_a);
241 1.1 gwr if (rr3 & ZSRR3_IP_B_RX)
242 1.1 gwr (*cs_b->cs_ops->zsop_rxint)(cs_b);
243 1.1 gwr
244 1.1 gwr /* Handle transmit done interrupts. */
245 1.1 gwr if (rr3 & ZSRR3_IP_A_TX)
246 1.1 gwr (*cs_a->cs_ops->zsop_txint)(cs_a);
247 1.1 gwr if (rr3 & ZSRR3_IP_B_TX)
248 1.1 gwr (*cs_b->cs_ops->zsop_txint)(cs_b);
249 1.1 gwr
250 1.1 gwr /* Handle status interrupts. */
251 1.1 gwr if (rr3 & ZSRR3_IP_A_STAT)
252 1.1 gwr (*cs_a->cs_ops->zsop_stint)(cs_a);
253 1.1 gwr if (rr3 & ZSRR3_IP_B_STAT)
254 1.1 gwr (*cs_b->cs_ops->zsop_stint)(cs_b);
255 1.1 gwr
256 1.1 gwr /* Clear interrupt. */
257 1.1 gwr if (rr3 & (ZSRR3_IP_A_RX | ZSRR3_IP_A_TX | ZSRR3_IP_A_STAT)) {
258 1.1 gwr *(cs_a->cs_reg_csr) = ZSWR0_CLR_INTR;
259 1.1 gwr ZS_DELAY();
260 1.1 gwr rval |= 1;
261 1.1 gwr }
262 1.1 gwr if (rr3 & (ZSRR3_IP_B_RX | ZSRR3_IP_B_TX | ZSRR3_IP_B_STAT)) {
263 1.1 gwr *(cs_b->cs_reg_csr) = ZSWR0_CLR_INTR;
264 1.1 gwr ZS_DELAY();
265 1.1 gwr rval |= 2;
266 1.1 gwr }
267 1.1 gwr
268 1.1 gwr if ((cs_a->cs_softreq) || (cs_b->cs_softreq))
269 1.1 gwr {
270 1.1 gwr /* This is a machine-dependent function. */
271 1.1 gwr zsc_req_softint(zsc);
272 1.1 gwr }
273 1.1 gwr
274 1.1 gwr return (rval);
275 1.1 gwr }
276 1.1 gwr
277 1.1 gwr
278 1.1 gwr /*
279 1.1 gwr * ZS software interrupt. Scan all channels for deferred interrupts.
280 1.1 gwr */
281 1.1 gwr int
282 1.1 gwr zsc_intr_soft(arg)
283 1.1 gwr void *arg;
284 1.1 gwr {
285 1.1 gwr register struct zsc_softc *zsc = arg;
286 1.1 gwr register struct zs_chanstate *cs;
287 1.1 gwr register int req, rval, s, unit;
288 1.1 gwr
289 1.1 gwr rval = 0;
290 1.1 gwr for (unit = 0; unit < 2; unit++) {
291 1.1 gwr cs = &zsc->zsc_cs[unit];
292 1.1 gwr
293 1.1 gwr s = splzs();
294 1.1 gwr req = cs->cs_softreq;
295 1.1 gwr cs->cs_softreq = 0;
296 1.1 gwr splx(s);
297 1.1 gwr
298 1.1 gwr if (req) {
299 1.1 gwr (*cs->cs_ops->zsop_softint)(cs);
300 1.1 gwr rval = 1;
301 1.1 gwr }
302 1.1 gwr }
303 1.1 gwr return (rval);
304 1.1 gwr }
305 1.1 gwr
306 1.1 gwr
307 1.1 gwr static int
308 1.1 gwr zsnull_intr(cs)
309 1.1 gwr struct zs_chanstate *cs;
310 1.1 gwr {
311 1.1 gwr ZS_WRITE(cs, 1, 0);
312 1.1 gwr ZS_WRITE(cs, 15, 0);
313 1.1 gwr }
314 1.1 gwr
315 1.1 gwr static int
316 1.1 gwr zsnull_softint(cs)
317 1.1 gwr struct zs_chanstate *cs;
318 1.1 gwr {
319 1.1 gwr }
320 1.1 gwr
321 1.1 gwr struct zsops zsops_null = {
322 1.1 gwr zsnull_intr, /* receive char available */
323 1.1 gwr zsnull_intr, /* external/status */
324 1.1 gwr zsnull_intr, /* xmit buffer empty */
325 1.1 gwr zsnull_softint, /* process software interrupt */
326 1.1 gwr };
327