xref: /netbsd/sys/dev/sun/ms_zs.c (revision c4a72b64)
1 /*	$NetBSD: ms_zs.c,v 1.9 2002/10/02 16:52:59 thorpej 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.9 2002/10/02 16:52:59 thorpej 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 CFATTACH_DECL(ms_zs, sizeof(struct ms_softc),
104     ms_zs_match, ms_zs_attach, NULL, NULL);
105 
106 /*
107  * ms_match: how is this zs channel configured?
108  */
109 int
110 ms_zs_match(parent, cf, aux)
111 	struct device *parent;
112 	struct cfdata *cf;
113 	void   *aux;
114 {
115 	struct zsc_attach_args *args = aux;
116 
117 	if (ms_zs_bps == 0)
118 		return 0;
119 
120 	/* Exact match required for keyboard. */
121 	if (cf->cf_loc[ZSCCF_CHANNEL] == args->channel)
122 		return 2;
123 
124 	return 0;
125 }
126 
127 void
128 ms_zs_attach(parent, self, aux)
129 	struct device *parent, *self;
130 	void   *aux;
131 
132 {
133 	struct zsc_softc *zsc = (void *) parent;
134 	struct ms_softc *ms = (void *) self;
135 	struct zsc_attach_args *args = aux;
136 	struct zs_chanstate *cs;
137 	struct cfdata *cf;
138 	int channel, ms_unit;
139 	int reset, s;
140 	int bps;
141 
142 	cf = ms->ms_dev.dv_cfdata;
143 	ms_unit = ms->ms_dev.dv_unit;
144 	channel = args->channel;
145 	cs = zsc->zsc_cs[channel];
146 	cs->cs_private = ms;
147 	cs->cs_ops = &zsops_ms;
148 	ms->ms_cs = cs;
149 	/* Allow kernel option SUN_MS_BPS to hard-code baud rate */
150 #ifndef SUN_MS_BPS
151 	if ((bps = cs->cs_defspeed) == 0)
152 #endif
153 		bps = ms_zs_bps;
154 
155 	printf(": baud rate %d\n", bps);
156 
157 	/* Initialize the speed, etc. */
158 	s = splzs();
159 	/* May need reset... */
160 	reset = (channel == 0) ?
161 		ZSWR9_A_RESET : ZSWR9_B_RESET;
162 	zs_write_reg(cs, 9, reset);
163 	/* These are OK as set by zscc: WR3, WR4, WR5 */
164 	/* We don't care about status or tx interrupts. */
165 	cs->cs_preg[1] = ZSWR1_RIE;
166 	(void) zs_set_speed(cs, bps);
167 	zs_loadchannelregs(cs);
168 	splx(s);
169 
170 	/* Initialize translator. */
171 	ms->ms_byteno = -1;
172 }
173 
174 /****************************************************************
175  * Interface to the lower layer (zscc)
176  ****************************************************************/
177 
178 static void
179 ms_zs_rxint(cs)
180 	struct zs_chanstate *cs;
181 {
182 	struct ms_softc *ms;
183 	int put, put_next;
184 	u_char c, rr1;
185 
186 	ms = cs->cs_private;
187 	put = ms->ms_rbput;
188 
189 	/*
190 	 * First read the status, because reading the received char
191 	 * destroys the status of this char.
192 	 */
193 	rr1 = zs_read_reg(cs, 1);
194 	c = zs_read_data(cs);
195 
196 	if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
197 		/* Clear the receive error. */
198 		zs_write_csr(cs, ZSWR0_RESET_ERRORS);
199 	}
200 
201 	ms->ms_rbuf[put] = (c << 8) | rr1;
202 	put_next = (put + 1) & MS_RX_RING_MASK;
203 
204 	/* Would overrun if increment makes (put==get). */
205 	if (put_next == ms->ms_rbget) {
206 		ms->ms_intr_flags |= INTR_RX_OVERRUN;
207 	} else {
208 		/* OK, really increment. */
209 		put = put_next;
210 	}
211 
212 	/* Done reading. */
213 	ms->ms_rbput = put;
214 
215 	/* Ask for softint() call. */
216 	cs->cs_softreq = 1;
217 }
218 
219 static void
220 ms_zs_txint(cs)
221 	struct zs_chanstate *cs;
222 {
223 	struct ms_softc *ms;
224 
225 	ms = cs->cs_private;
226 	zs_write_csr(cs, ZSWR0_RESET_TXINT);
227 	ms->ms_intr_flags |= INTR_TX_EMPTY;
228 	/* Ask for softint() call. */
229 	cs->cs_softreq = 1;
230 }
231 
232 static void
233 ms_zs_stint(cs, force)
234 	struct zs_chanstate *cs;
235 	int force;
236 {
237 	struct ms_softc *ms;
238 	int rr0;
239 
240 	ms = cs->cs_private;
241 
242 	rr0 = zs_read_csr(cs);
243 	zs_write_csr(cs, ZSWR0_RESET_STATUS);
244 
245 	/*
246 	 * We have to accumulate status line changes here.
247 	 * Otherwise, if we get multiple status interrupts
248 	 * before the softint runs, we could fail to notice
249 	 * some status line changes in the softint routine.
250 	 * Fix from Bill Studenmund, October 1996.
251 	 */
252 	cs->cs_rr0_delta |= (cs->cs_rr0 ^ rr0);
253 	cs->cs_rr0 = rr0;
254 	ms->ms_intr_flags |= INTR_ST_CHECK;
255 
256 	/* Ask for softint() call. */
257 	cs->cs_softreq = 1;
258 }
259 
260 static void
261 ms_zs_softint(cs)
262 	struct zs_chanstate *cs;
263 {
264 	struct ms_softc *ms;
265 	int get, c, s;
266 	int intr_flags;
267 	u_short ring_data;
268 
269 	ms = cs->cs_private;
270 
271 	/* Atomically get and clear flags. */
272 	s = splzs();
273 	intr_flags = ms->ms_intr_flags;
274 	ms->ms_intr_flags = 0;
275 
276 	/* Now lower to spltty for the rest. */
277 	(void) spltty();
278 
279 	/*
280 	 * Copy data from the receive ring to the event layer.
281 	 */
282 	get = ms->ms_rbget;
283 	while (get != ms->ms_rbput) {
284 		ring_data = ms->ms_rbuf[get];
285 		get = (get + 1) & MS_RX_RING_MASK;
286 
287 		/* low byte of ring_data is rr1 */
288 		c = (ring_data >> 8) & 0xff;
289 
290 		if (ring_data & ZSRR1_DO)
291 			intr_flags |= INTR_RX_OVERRUN;
292 		if (ring_data & (ZSRR1_FE | ZSRR1_PE)) {
293 			log(LOG_ERR, "%s: input error (0x%x)\n",
294 				ms->ms_dev.dv_xname, ring_data);
295 			c = -1;	/* signal input error */
296 		}
297 
298 		/* Pass this up to the "middle" layer. */
299 		ms_input(ms, c);
300 	}
301 	if (intr_flags & INTR_RX_OVERRUN) {
302 		log(LOG_ERR, "%s: input overrun\n",
303 		    ms->ms_dev.dv_xname);
304 	}
305 	ms->ms_rbget = get;
306 
307 	if (intr_flags & INTR_TX_EMPTY) {
308 		/*
309 		 * Transmit done.  (Not expected.)
310 		 */
311 		log(LOG_ERR, "%s: transmit interrupt?\n",
312 		    ms->ms_dev.dv_xname);
313 	}
314 
315 	if (intr_flags & INTR_ST_CHECK) {
316 		/*
317 		 * Status line change.  (Not expected.)
318 		 */
319 		log(LOG_ERR, "%s: status interrupt?\n",
320 		    ms->ms_dev.dv_xname);
321 		cs->cs_rr0_delta = 0;
322 	}
323 
324 	splx(s);
325 }
326