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