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