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