z8530sc.c revision 1.13 1 /* $NetBSD: z8530sc.c,v 1.13 2001/06/25 08:30:11 wdk 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 void
71 zs_break(cs, set)
72 struct zs_chanstate *cs;
73 int set;
74 {
75
76 if (set) {
77 cs->cs_preg[5] |= ZSWR5_BREAK;
78 cs->cs_creg[5] |= ZSWR5_BREAK;
79 } else {
80 cs->cs_preg[5] &= ~ZSWR5_BREAK;
81 cs->cs_creg[5] &= ~ZSWR5_BREAK;
82 }
83 zs_write_reg(cs, 5, cs->cs_creg[5]);
84 }
85
86
87 /*
88 * drain on-chip fifo
89 */
90 void
91 zs_iflush(cs)
92 struct zs_chanstate *cs;
93 {
94 u_char c, rr0, rr1;
95 int i;
96
97 /*
98 * Count how many times we loop. Some systems, such as some
99 * Apple PowerBooks, claim to have SCC's which they really don't.
100 */
101 for (i = 0; i < 32; i++) {
102 /* Is there input available? */
103 rr0 = zs_read_csr(cs);
104 if ((rr0 & ZSRR0_RX_READY) == 0)
105 break;
106
107 /*
108 * First read the status, because reading the data
109 * destroys the status of this char.
110 */
111 rr1 = zs_read_reg(cs, 1);
112 c = zs_read_data(cs);
113
114 if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
115 /* Clear the receive error. */
116 zs_write_csr(cs, ZSWR0_RESET_ERRORS);
117 }
118 }
119 }
120
121
122 /*
123 * Write the given register set to the given zs channel in the proper order.
124 * The channel must not be transmitting at the time. The receiver will
125 * be disabled for the time it takes to write all the registers.
126 * Call this with interrupts disabled.
127 */
128 void
129 zs_loadchannelregs(cs)
130 struct zs_chanstate *cs;
131 {
132 u_char *reg;
133
134 zs_write_csr(cs, ZSM_RESET_ERR); /* XXX: reset error condition */
135
136 #if 1
137 /*
138 * XXX: Is this really a good idea?
139 * XXX: Should go elsewhere! -gwr
140 */
141 zs_iflush(cs); /* XXX */
142 #endif
143
144 if (bcmp((caddr_t)cs->cs_preg, (caddr_t)cs->cs_creg, 16) == 0)
145 return; /* only change if values are different */
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 /* disable interrupts */
152 zs_write_reg(cs, 1, reg[1] & ~ZSWR1_IMASK);
153
154 /* baud clock divisor, stop bits, parity */
155 zs_write_reg(cs, 4, reg[4]);
156
157 /* misc. TX/RX control bits */
158 zs_write_reg(cs, 10, reg[10]);
159
160 /* char size, enable (RX/TX) */
161 zs_write_reg(cs, 3, reg[3] & ~ZSWR3_RX_ENABLE);
162 zs_write_reg(cs, 5, reg[5] & ~ZSWR5_TX_ENABLE);
163
164 /* synchronous mode stuff */
165 zs_write_reg(cs, 6, reg[6]);
166 zs_write_reg(cs, 7, reg[7]);
167
168 #if 0
169 /*
170 * Registers 2 and 9 are special because they are
171 * actually common to both channels, but must be
172 * programmed through channel A. The "zsc" attach
173 * function takes care of setting these registers
174 * and they should not be touched thereafter.
175 */
176 /* interrupt vector */
177 zs_write_reg(cs, 2, reg[2]);
178 /* master interrupt control */
179 zs_write_reg(cs, 9, reg[9]);
180 #endif
181
182 /* Shut down the BRG */
183 zs_write_reg(cs, 14, reg[14] & ~ZSWR14_BAUD_ENA);
184
185 #ifdef ZS_MD_SETCLK
186 /* Let the MD code setup any external clock. */
187 ZS_MD_SETCLK(cs);
188 #endif /* ZS_MD_SETCLK */
189
190 /* clock mode control */
191 zs_write_reg(cs, 11, reg[11]);
192
193 /* baud rate (lo/hi) */
194 zs_write_reg(cs, 12, reg[12]);
195 zs_write_reg(cs, 13, reg[13]);
196
197 /* Misc. control bits */
198 zs_write_reg(cs, 14, reg[14]);
199
200 /* which lines cause status interrupts */
201 zs_write_reg(cs, 15, reg[15]);
202
203 /*
204 * Zilog docs recommend resetting external status twice at this
205 * point. Mainly as the status bits are latched, and the first
206 * interrupt clear might unlatch them to new values, generating
207 * a second interrupt request.
208 */
209 zs_write_csr(cs, ZSM_RESET_STINT);
210 zs_write_csr(cs, ZSM_RESET_STINT);
211
212 /* char size, enable (RX/TX)*/
213 zs_write_reg(cs, 3, reg[3]);
214 zs_write_reg(cs, 5, reg[5]);
215
216 /* interrupt enables: RX, TX, STATUS */
217 zs_write_reg(cs, 1, reg[1]);
218 }
219
220
221 /*
222 * ZS hardware interrupt. Scan all ZS channels. NB: we know here that
223 * channels are kept in (A,B) pairs.
224 *
225 * Do just a little, then get out; set a software interrupt if more
226 * work is needed.
227 *
228 * We deliberately ignore the vectoring Zilog gives us, and match up
229 * only the number of `reset interrupt under service' operations, not
230 * the order.
231 */
232 int
233 zsc_intr_hard(arg)
234 void *arg;
235 {
236 struct zsc_softc *zsc = arg;
237 struct zs_chanstate *cs;
238 u_char rr3;
239
240 /* First look at channel A. */
241 cs = zsc->zsc_cs[0];
242 /* Note: only channel A has an RR3 */
243 rr3 = zs_read_reg(cs, 3);
244
245 /*
246 * Clear interrupt first to avoid a race condition.
247 * If a new interrupt condition happens while we are
248 * servicing this one, we will get another interrupt
249 * shortly. We can NOT just sit here in a loop, or
250 * we will cause horrible latency for other devices
251 * on this interrupt level (i.e. sun3x floppy disk).
252 */
253 if (rr3 & (ZSRR3_IP_A_RX | ZSRR3_IP_A_TX | ZSRR3_IP_A_STAT)) {
254 zs_write_csr(cs, ZSWR0_CLR_INTR);
255 if (rr3 & ZSRR3_IP_A_RX)
256 (*cs->cs_ops->zsop_rxint)(cs);
257 if (rr3 & ZSRR3_IP_A_STAT)
258 (*cs->cs_ops->zsop_stint)(cs, 0);
259 if (rr3 & ZSRR3_IP_A_TX)
260 (*cs->cs_ops->zsop_txint)(cs);
261 }
262
263 /* Now look at channel B. */
264 cs = zsc->zsc_cs[1];
265 if (rr3 & (ZSRR3_IP_B_RX | ZSRR3_IP_B_TX | ZSRR3_IP_B_STAT)) {
266 zs_write_csr(cs, ZSWR0_CLR_INTR);
267 if (rr3 & ZSRR3_IP_B_RX)
268 (*cs->cs_ops->zsop_rxint)(cs);
269 if (rr3 & ZSRR3_IP_B_STAT)
270 (*cs->cs_ops->zsop_stint)(cs, 0);
271 if (rr3 & ZSRR3_IP_B_TX)
272 (*cs->cs_ops->zsop_txint)(cs);
273 }
274
275 /* Note: caller will check cs_x->cs_softreq and DTRT. */
276 return (rr3);
277 }
278
279
280 /*
281 * ZS software interrupt. Scan all channels for deferred interrupts.
282 */
283 int
284 zsc_intr_soft(arg)
285 void *arg;
286 {
287 struct zsc_softc *zsc = arg;
288 struct zs_chanstate *cs;
289 int rval, chan;
290
291 rval = 0;
292 for (chan = 0; chan < 2; chan++) {
293 cs = zsc->zsc_cs[chan];
294
295 /*
296 * The softint flag can be safely cleared once
297 * we have decided to call the softint routine.
298 * (No need to do splzs() first.)
299 */
300 if (cs->cs_softreq) {
301 cs->cs_softreq = 0;
302 (*cs->cs_ops->zsop_softint)(cs);
303 rval++;
304 }
305 }
306 return (rval);
307 }
308
309 /*
310 * Provide a null zs "ops" vector.
311 */
312
313 static void zsnull_rxint __P((struct zs_chanstate *));
314 static void zsnull_stint __P((struct zs_chanstate *, int));
315 static void zsnull_txint __P((struct zs_chanstate *));
316 static void zsnull_softint __P((struct zs_chanstate *));
317
318 static void
319 zsnull_rxint(cs)
320 struct zs_chanstate *cs;
321 {
322 /* Ask for softint() call. */
323 cs->cs_softreq = 1;
324 }
325
326 static void
327 zsnull_stint(cs, force)
328 struct zs_chanstate *cs;
329 int force;
330 {
331 /* Ask for softint() call. */
332 cs->cs_softreq = 1;
333 }
334
335 static void
336 zsnull_txint(cs)
337 struct zs_chanstate *cs;
338 {
339 /* Ask for softint() call. */
340 cs->cs_softreq = 1;
341 }
342
343 static void
344 zsnull_softint(cs)
345 struct zs_chanstate *cs;
346 {
347 zs_write_reg(cs, 1, 0);
348 zs_write_reg(cs, 15, 0);
349 }
350
351 struct zsops zsops_null = {
352 zsnull_rxint, /* receive char available */
353 zsnull_stint, /* external/status */
354 zsnull_txint, /* xmit buffer empty */
355 zsnull_softint, /* process software interrupt */
356 };
357