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