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