xref: /netbsd-src/sys/dev/sun/ms_zs.c (revision 1ca5c1b28139779176bd5c13ad7c5f25c0bcd5f8)
1 /*	$NetBSD: ms_zs.c,v 1.6 2001/12/09 12:02:06 pk Exp $	*/
2 
3 /*
4  * Copyright (c) 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This software was developed by the Computer Systems Engineering group
8  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9  * contributed to Berkeley.
10  *
11  * All advertising materials mentioning features or use of this software
12  * must display the following acknowledgement:
13  *	This product includes software developed by the University of
14  *	California, Lawrence Berkeley Laboratory.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  * 1. Redistributions of source code must retain the above copyright
20  *    notice, this list of conditions and the following disclaimer.
21  * 2. Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the
23  *    documentation and/or other materials provided with the distribution.
24  * 3. All advertising materials mentioning features or use of this software
25  *    must display the following acknowledgement:
26  *	This product includes software developed by the University of
27  *	California, Berkeley and its contributors.
28  * 4. Neither the name of the University nor the names of its contributors
29  *    may be used to endorse or promote products derived from this software
30  *    without specific prior written permission.
31  *
32  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42  * SUCH DAMAGE.
43  *
44  *	@(#)ms.c	8.1 (Berkeley) 6/11/93
45  */
46 
47 /*
48  * Mouse driver (/dev/mouse)
49  */
50 
51 /*
52  * Zilog Z8530 Dual UART driver (mouse interface)
53  *
54  * This is the "slave" driver that will be attached to
55  * the "zsc" driver for a Sun mouse.
56  */
57 
58 #include <sys/cdefs.h>
59 __KERNEL_RCSID(0, "$NetBSD: ms_zs.c,v 1.6 2001/12/09 12:02:06 pk Exp $");
60 
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/conf.h>
64 #include <sys/device.h>
65 #include <sys/ioctl.h>
66 #include <sys/kernel.h>
67 #include <sys/proc.h>
68 #include <sys/signal.h>
69 #include <sys/signalvar.h>
70 #include <sys/time.h>
71 #include <sys/select.h>
72 #include <sys/syslog.h>
73 
74 #include <machine/vuid_event.h>
75 
76 #include <dev/ic/z8530reg.h>
77 #include <machine/z8530var.h>
78 #include <dev/sun/event_var.h>
79 #include <dev/sun/msvar.h>
80 
81 static void ms_zs_rxint __P((struct zs_chanstate *));
82 static void ms_zs_stint __P((struct zs_chanstate *, int));
83 static void ms_zs_txint __P((struct zs_chanstate *));
84 static void ms_zs_softint __P((struct zs_chanstate *));
85 
86 struct zsops zsops_ms = {
87 	ms_zs_rxint,	/* receive char available */
88 	ms_zs_stint,	/* external/status */
89 	ms_zs_txint,	/* xmit buffer empty */
90 	ms_zs_softint,	/* process software interrupt */
91 };
92 
93 /* Fall-back baud rate */
94 #ifdef SUN_MS_BPS
95 int	ms_zs_bps = SUN_MS_BPS;
96 #else
97 int	ms_zs_bps = MS_DEFAULT_BPS;
98 #endif
99 
100 static int	ms_zs_match(struct device *, struct cfdata *, void *);
101 static void	ms_zs_attach(struct device *, struct device *, void *);
102 
103 struct cfattach ms_zs_ca = {
104 	sizeof(struct ms_softc), ms_zs_match, ms_zs_attach
105 };
106 
107 /*
108  * ms_match: how is this zs channel configured?
109  */
110 int
111 ms_zs_match(parent, cf, aux)
112 	struct device *parent;
113 	struct cfdata *cf;
114 	void   *aux;
115 {
116 	struct zsc_attach_args *args = aux;
117 
118 	if (ms_zs_bps == 0)
119 		return 0;
120 
121 	/* Exact match required for keyboard. */
122 	if (cf->cf_loc[ZSCCF_CHANNEL] == args->channel)
123 		return 2;
124 
125 	return 0;
126 }
127 
128 void
129 ms_zs_attach(parent, self, aux)
130 	struct device *parent, *self;
131 	void   *aux;
132 
133 {
134 	struct zsc_softc *zsc = (void *) parent;
135 	struct ms_softc *ms = (void *) self;
136 	struct zsc_attach_args *args = aux;
137 	struct zs_chanstate *cs;
138 	struct cfdata *cf;
139 	int channel, ms_unit;
140 	int reset, s;
141 	int bps;
142 
143 	cf = ms->ms_dev.dv_cfdata;
144 	ms_unit = ms->ms_dev.dv_unit;
145 	channel = args->channel;
146 	cs = zsc->zsc_cs[channel];
147 	cs->cs_private = ms;
148 	cs->cs_ops = &zsops_ms;
149 	ms->ms_cs = cs;
150 	/* Allow kernel option SUN_MS_BPS to hard-code baud rate */
151 #ifndef SUN_MS_BPS
152 	if ((bps = cs->cs_defspeed) == 0)
153 #endif
154 		bps = ms_zs_bps;
155 
156 	printf(": baud rate %d\n", bps);
157 
158 	/* Initialize the speed, etc. */
159 	s = splzs();
160 	/* May need reset... */
161 	reset = (channel == 0) ?
162 		ZSWR9_A_RESET : ZSWR9_B_RESET;
163 	zs_write_reg(cs, 9, reset);
164 	/* These are OK as set by zscc: WR3, WR4, WR5 */
165 	/* We don't care about status or tx interrupts. */
166 	cs->cs_preg[1] = ZSWR1_RIE;
167 	(void) zs_set_speed(cs, bps);
168 	zs_loadchannelregs(cs);
169 	splx(s);
170 
171 	/* Initialize translator. */
172 	ms->ms_byteno = -1;
173 }
174 
175 /****************************************************************
176  * Interface to the lower layer (zscc)
177  ****************************************************************/
178 
179 static void
180 ms_zs_rxint(cs)
181 	struct zs_chanstate *cs;
182 {
183 	struct ms_softc *ms;
184 	int put, put_next;
185 	u_char c, rr1;
186 
187 	ms = cs->cs_private;
188 	put = ms->ms_rbput;
189 
190 	/*
191 	 * First read the status, because reading the received char
192 	 * destroys the status of this char.
193 	 */
194 	rr1 = zs_read_reg(cs, 1);
195 	c = zs_read_data(cs);
196 
197 	if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
198 		/* Clear the receive error. */
199 		zs_write_csr(cs, ZSWR0_RESET_ERRORS);
200 	}
201 
202 	ms->ms_rbuf[put] = (c << 8) | rr1;
203 	put_next = (put + 1) & MS_RX_RING_MASK;
204 
205 	/* Would overrun if increment makes (put==get). */
206 	if (put_next == ms->ms_rbget) {
207 		ms->ms_intr_flags |= INTR_RX_OVERRUN;
208 	} else {
209 		/* OK, really increment. */
210 		put = put_next;
211 	}
212 
213 	/* Done reading. */
214 	ms->ms_rbput = put;
215 
216 	/* Ask for softint() call. */
217 	cs->cs_softreq = 1;
218 }
219 
220 static void
221 ms_zs_txint(cs)
222 	struct zs_chanstate *cs;
223 {
224 	struct ms_softc *ms;
225 
226 	ms = cs->cs_private;
227 	zs_write_csr(cs, ZSWR0_RESET_TXINT);
228 	ms->ms_intr_flags |= INTR_TX_EMPTY;
229 	/* Ask for softint() call. */
230 	cs->cs_softreq = 1;
231 }
232 
233 static void
234 ms_zs_stint(cs, force)
235 	struct zs_chanstate *cs;
236 	int force;
237 {
238 	struct ms_softc *ms;
239 	int rr0;
240 
241 	ms = cs->cs_private;
242 
243 	rr0 = zs_read_csr(cs);
244 	zs_write_csr(cs, ZSWR0_RESET_STATUS);
245 
246 	/*
247 	 * We have to accumulate status line changes here.
248 	 * Otherwise, if we get multiple status interrupts
249 	 * before the softint runs, we could fail to notice
250 	 * some status line changes in the softint routine.
251 	 * Fix from Bill Studenmund, October 1996.
252 	 */
253 	cs->cs_rr0_delta |= (cs->cs_rr0 ^ rr0);
254 	cs->cs_rr0 = rr0;
255 	ms->ms_intr_flags |= INTR_ST_CHECK;
256 
257 	/* Ask for softint() call. */
258 	cs->cs_softreq = 1;
259 }
260 
261 static void
262 ms_zs_softint(cs)
263 	struct zs_chanstate *cs;
264 {
265 	struct ms_softc *ms;
266 	int get, c, s;
267 	int intr_flags;
268 	u_short ring_data;
269 
270 	ms = cs->cs_private;
271 
272 	/* Atomically get and clear flags. */
273 	s = splzs();
274 	intr_flags = ms->ms_intr_flags;
275 	ms->ms_intr_flags = 0;
276 
277 	/* Now lower to spltty for the rest. */
278 	(void) spltty();
279 
280 	/*
281 	 * Copy data from the receive ring to the event layer.
282 	 */
283 	get = ms->ms_rbget;
284 	while (get != ms->ms_rbput) {
285 		ring_data = ms->ms_rbuf[get];
286 		get = (get + 1) & MS_RX_RING_MASK;
287 
288 		/* low byte of ring_data is rr1 */
289 		c = (ring_data >> 8) & 0xff;
290 
291 		if (ring_data & ZSRR1_DO)
292 			intr_flags |= INTR_RX_OVERRUN;
293 		if (ring_data & (ZSRR1_FE | ZSRR1_PE)) {
294 			log(LOG_ERR, "%s: input error (0x%x)\n",
295 				ms->ms_dev.dv_xname, ring_data);
296 			c = -1;	/* signal input error */
297 		}
298 
299 		/* Pass this up to the "middle" layer. */
300 		ms_input(ms, c);
301 	}
302 	if (intr_flags & INTR_RX_OVERRUN) {
303 		log(LOG_ERR, "%s: input overrun\n",
304 		    ms->ms_dev.dv_xname);
305 	}
306 	ms->ms_rbget = get;
307 
308 	if (intr_flags & INTR_TX_EMPTY) {
309 		/*
310 		 * Transmit done.  (Not expected.)
311 		 */
312 		log(LOG_ERR, "%s: transmit interrupt?\n",
313 		    ms->ms_dev.dv_xname);
314 	}
315 
316 	if (intr_flags & INTR_ST_CHECK) {
317 		/*
318 		 * Status line change.  (Not expected.)
319 		 */
320 		log(LOG_ERR, "%s: status interrupt?\n",
321 		    ms->ms_dev.dv_xname);
322 		cs->cs_rr0_delta = 0;
323 	}
324 
325 	splx(s);
326 }
327