xref: /netbsd-src/sys/arch/hpcmips/dev/optpoint.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: optpoint.c,v 1.3 2005/12/11 12:17:33 christos Exp $ */
2 
3 /*-
4  * Copyright (c) 2005 HAMAJIMA Katsuomi. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 /*
29  * OptOpint on Telios HC-AJ2
30  */
31 
32 #include <sys/cdefs.h>
33 
34 __KERNEL_RCSID(0, "$NetBSD: optpoint.c,v 1.3 2005/12/11 12:17:33 christos Exp $");
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/device.h>
39 #include <machine/bus.h>
40 #include <machine/config_hook.h>
41 #include <dev/hpc/hpciovar.h>
42 #include <dev/wscons/wsconsio.h>
43 #include <dev/wscons/wsmousevar.h>
44 #include <hpcmips/tx/tx39var.h>
45 #include <hpcmips/tx/tx39spivar.h>
46 #include <hpcmips/tx/tx39icureg.h>
47 
48 #undef	OPTPOINTDEBUG
49 
50 #ifdef OPTPOINTDEBUG
51 #define	DPRINTF(arg) printf arg
52 #else
53 #define	DPRINTF(arg)
54 #endif
55 
56 struct optpoint_softc {
57 	struct device sc_dev;
58 	tx_chipset_tag_t sc_tc;
59 	struct tx39spi_softc *sc_spi;
60 	struct hpcio_chip *sc_hc;
61 	struct device *sc_wsmousedev;
62 	void *sc_powerhook;	/* power management hook */
63 	char packet[4];
64 	int index;	/* number of bytes received for this packet */
65 	u_int buttons;	/* mouse button status */
66 	int enabled;
67 };
68 
69 static int optpoint_match(struct device *, struct cfdata *, void *);
70 static void optpoint_attach(struct device *, struct device *, void *);
71 static int optpoint_intr(void *);
72 static int optpoint_enable(void *);
73 static void optpoint_disable(void *);
74 static int optpoint_ioctl(void *, u_long, caddr_t, int, struct proc *);
75 static int optpoint_initialize(void *);
76 static void optpoint_send(struct optpoint_softc *, int);
77 static int optpoint_recv(struct optpoint_softc *);
78 static int optpoint_power(void *, int, long, void *);
79 
80 #define LBUTMASK 0x01
81 #define RBUTMASK 0x02
82 
83 #define TX39_INTRSTATUS4_OPTPOINTINT	TX39_INTRSTATUS4_CARDIORDNEGINT
84 #define TX39_IO_MFIO_CARDREG	11
85 #define TX39_IO_MFIO_CARDIOWR	10
86 #define TX39_IO_MFIO_CARDIORD	9
87 #define TELIOS_MFIO_OPTP_T_RDY		TX39_IO_MFIO_CARDREG
88 #define TELIOS_MFIO_OPTP_C_REQ		TX39_IO_MFIO_CARDIOWR
89 #define TELIOS_MFIO_OPTP_S_ENB_N	TX39_IO_MFIO_CARDIORD
90 
91 CFATTACH_DECL(optpoint, sizeof(struct optpoint_softc),
92     optpoint_match, optpoint_attach, NULL, NULL);
93 
94 const struct wsmouse_accessops optpoint_accessops = {
95 	optpoint_enable,
96 	optpoint_ioctl,
97 	optpoint_disable,
98 };
99 
100 int
101 optpoint_match(struct device *parent, struct cfdata *cf, void *aux)
102 {
103 	return (ATTACH_NORMAL);
104 }
105 
106 void
107 optpoint_attach(struct device *parent, struct device *self, void *aux)
108 {
109 	struct txspi_attach_args *ta = aux;
110 	struct optpoint_softc *sc = (void*)self;
111 	struct tx39spi_softc *spi = sc->sc_spi = (void*)parent;
112 	tx_chipset_tag_t tc = sc->sc_tc = ta->sa_tc;
113 	struct wsmousedev_attach_args wsmaa;
114 
115 	sc->sc_hc = tc->tc_iochip[MFIO];
116 	sc->enabled = 0;
117 
118 	/* Specific SPI settings for OptPoint of HC-AJ2 */
119 	tx39spi_delayval(spi, 0);
120 	tx39spi_baudrate(spi, 4);	/* SPICLK Rate = 737.3 kHz */
121 	tx39spi_word(spi, 0);		/* Use 8bits of data */
122 	tx39spi_phapol(spi, 0);
123 	tx39spi_clkpol(spi, 1);
124 	tx39spi_lsb(spi, 0);		/* MSB first */
125 
126 	optpoint_enable(sc);
127 	printf("\n");
128 
129 	wsmaa.accessops = &optpoint_accessops;
130 	wsmaa.accesscookie = sc;
131 	/* attach the wsmouse */
132 	sc->sc_wsmousedev = config_found(self, &wsmaa, wsmousedevprint);
133 
134 	/* Add a hard power hook to power saving */
135 	sc->sc_powerhook = config_hook(CONFIG_HOOK_PMEVENT,
136 				       CONFIG_HOOK_PMEVENT_HARDPOWER,
137 				       CONFIG_HOOK_SHARE,
138 				       optpoint_power, sc);
139 #ifdef DIAGNOSTIC
140 	if (sc->sc_powerhook == 0)
141 		printf("%s: unable to establish hard power hook",
142 		       sc->sc_dev.dv_xname);
143 #endif
144 }
145 
146 int
147 optpoint_intr(void *self)
148 {
149 	struct optpoint_softc *sc = (void*)self;
150 	tx_chipset_tag_t tc = sc->sc_tc;
151 	char data = optpoint_recv(sc) & 0xff;
152 
153 #ifdef DIAGNOSTIC
154 	if (sc->index >= 3){
155 		printf("%s: Receive buffer overflow\n", sc->sc_dev.dv_xname);
156 		sc->index = 0;
157 		memset(sc->packet, 0, 3);
158 	}
159 #endif
160 	if ((sc->index == 1) && (data & 0xcc) != 0x08){
161 		DPRINTF(("%s: Bad second byte (0x%02x)\n",
162 			 sc->sc_dev.dv_xname, data));
163 		tx_conf_write(tc, TX39_INTRCLEAR4_REG,
164 				  TX39_INTRSTATUS4_OPTPOINTINT);
165 		return 0;
166 	}
167 
168 	sc->packet[sc->index++] = data;
169 	if (sc->index >= 3){
170 		u_int newbuttons = ((sc->packet[1] & LBUTMASK) ? 0x1 : 0)
171 				 | ((sc->packet[1] & RBUTMASK) ? 0x2 : 0);
172 		int dx = sc->packet[2];
173 		int dy = sc->packet[0];
174 		u_int changed = (sc->buttons ^ newbuttons);
175 
176 		if (dx || dy || changed){
177 			DPRINTF(("%s: buttons=0x%x, dx=%d, dy=%d\n",
178 				 sc->sc_dev.dv_xname, newbuttons, dx, dy));
179 			wsmouse_input(sc->sc_wsmousedev, newbuttons, dx, dy, 0,
180 				      WSMOUSE_INPUT_DELTA);
181 		}
182 		sc->buttons = newbuttons;
183 		sc->index = 0;
184 		memset(sc->packet, 0, 3);
185 	}
186 	tx_conf_write(tc, TX39_INTRCLEAR4_REG, TX39_INTRSTATUS4_OPTPOINTINT);
187 
188 	return 0;
189 }
190 
191 int
192 optpoint_enable(void *self)
193 {
194 	struct optpoint_softc *sc = (void*)self;
195 
196 	if (!sc->enabled){
197 		tx_chipset_tag_t tc = sc->sc_tc;
198 		struct hpcio_chip *hc = sc->sc_hc;
199 		int s = spltty();
200 
201 		DPRINTF(("%s: enable\n", sc->sc_dev.dv_xname));
202 
203 		sc->enabled = 1;
204 		sc->index = 0;
205 		sc->buttons = 0;
206 		sc->packet[0] = 0xf5;	/* Disable */
207 		sc->packet[1] = 0xf2;	/* Set Stream Mode */
208 		sc->packet[2] = 0xf8;	/* Stanby */
209 		sc->packet[3] = 0xf4;	/* Enable */
210 
211 		tx39spi_enable(sc->sc_spi, 1);
212 		tx_intr_establish(tc, MAKEINTR(4, TX39_INTRSTATUS4_OPTPOINTINT),
213 				  IST_EDGE, IPL_TTY, optpoint_initialize, sc);
214 		(*hc->hc_portwrite)(hc, TELIOS_MFIO_OPTP_C_REQ, 1);
215 		(*hc->hc_portwrite)(hc, TELIOS_MFIO_OPTP_T_RDY, 1);
216 		splx(s);
217 	}
218 
219         return 0;
220 }
221 
222 void
223 optpoint_disable(void *self)
224 {
225 	struct optpoint_softc *sc = (void*)self;
226 
227 	if (sc->enabled){
228 		tx_chipset_tag_t tc = sc->sc_tc;
229 		struct hpcio_chip *hc = sc->sc_hc;
230 		int s = spltty();
231 
232 		DPRINTF(("%s: disable\n", sc->sc_dev.dv_xname));
233 
234 		sc->enabled = 0;
235 		(*hc->hc_portwrite)(hc, TELIOS_MFIO_OPTP_C_REQ, 0);
236 		(*hc->hc_portwrite)(hc, TELIOS_MFIO_OPTP_T_RDY, 0);
237 		tx_intr_disestablish(tc,
238 			    (void *)MAKEINTR(4, TX39_INTRSTATUS4_OPTPOINTINT));
239 		tx39spi_enable(sc->sc_spi, 0);
240 		splx(s);
241 	}
242 }
243 
244 int
245 optpoint_ioctl(void *self, u_long cmd, caddr_t data, int flag, struct proc *p)
246 {
247 	switch (cmd) {
248 	case WSMOUSEIO_GTYPE:
249 		*(u_int *)data = WSMOUSE_TYPE_PS2;
250 		break;
251 
252 	default:
253 		return (EPASSTHROUGH);
254 	}
255         return 0;
256 }
257 
258 int
259 optpoint_initialize(void *self)
260 {
261 	struct optpoint_softc *sc = (void*)self;
262 	struct hpcio_chip *hc = sc->sc_hc;
263 	tx_chipset_tag_t tc = sc->sc_tc;
264 
265 	if (sc->index < 4){
266 		optpoint_send(sc, sc->packet[sc->index++]);
267 		(*hc->hc_portwrite)(hc, TELIOS_MFIO_OPTP_C_REQ, 1);
268 		(*hc->hc_portwrite)(hc, TELIOS_MFIO_OPTP_T_RDY, 1);
269 	} else {
270 		tx_intr_disestablish(tc,
271 			    (void *)MAKEINTR(4, TX39_INTRSTATUS4_OPTPOINTINT));
272 		sc->index = 0;
273 		memset(sc->packet, 0, 3);
274 		tx_intr_establish(tc,
275 				  MAKEINTR(4, TX39_INTRSTATUS4_OPTPOINTINT),
276 				  IST_EDGE, IPL_TTY, optpoint_intr, sc);
277 		(*hc->hc_portwrite)(hc, TELIOS_MFIO_OPTP_C_REQ, 0);
278 		(*hc->hc_portwrite)(hc, TELIOS_MFIO_OPTP_T_RDY, 1);
279 	}
280 	tx_conf_write(tc, TX39_INTRCLEAR4_REG, TX39_INTRSTATUS4_OPTPOINTINT);
281 
282 	return 0;
283 }
284 
285 void
286 optpoint_send(struct optpoint_softc *sc, int data)
287 {
288 	struct hpcio_chip *hc = sc->sc_hc;
289 
290 	while ((*hc->hc_portread)(hc, TELIOS_MFIO_OPTP_S_ENB_N))
291 		;
292 	tx39spi_put_word(sc->sc_spi, data & 0xff);
293 }
294 
295 int
296 optpoint_recv(struct optpoint_softc *sc)
297 {
298 	struct hpcio_chip *hc = sc->sc_hc;
299 
300 	optpoint_send(sc, 0x00);
301 	while ((*hc->hc_portread)(hc, TELIOS_MFIO_OPTP_S_ENB_N))
302 		;
303 	return tx39spi_get_word(sc->sc_spi) & 0xff;
304 }
305 
306 int
307 optpoint_power(void *self, int type, long id, void *msg)
308 {
309 	struct optpoint_softc *sc = (void *)self;
310 	int why = (int)msg;
311 
312 	switch (why) {
313 	case PWR_RESUME:
314 		/* power on */
315 		optpoint_enable(sc);
316 		break;
317 	case PWR_SUSPEND:
318 		/* FALLTHROUGH */
319 	case PWR_STANDBY:
320 		/* power off */
321 		optpoint_disable(sc);
322 		break;
323 	}
324 
325 	return 0;
326 }
327