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