vrkiu.c revision 1.33 1 /* $NetBSD: vrkiu.c,v 1.33 2002/10/02 05:26:56 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1999 SASAKI Takesi All rights reserved.
5 * Copyright (c) 1999, 2000, 2002 TAKEMRUA, Shin All rights reserved.
6 * Copyright (c) 1999 PocketBSD Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the PocketBSD project
19 * and its contributors.
20 * 4. Neither the name of the project nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 */
37
38 #include <sys/param.h>
39 #include <sys/tty.h>
40 #include <sys/systm.h>
41 #include <sys/device.h>
42 #include <sys/conf.h>
43 #include <sys/kernel.h>
44 #include <sys/proc.h>
45
46 #include <machine/intr.h>
47 #include <machine/cpu.h>
48 #include <machine/bus.h>
49 #include <machine/platid.h>
50 #include <machine/platid_mask.h>
51
52 #include <dev/hpc/hpckbdvar.h>
53
54 #include <hpcmips/vr/vr.h>
55 #include <hpcmips/vr/vripif.h>
56 #include <hpcmips/vr/vrkiureg.h>
57 #include <hpcmips/vr/vrkiuvar.h>
58 #include <hpcmips/vr/icureg.h>
59
60 #include "opt_wsdisplay_compat.h"
61 #include "opt_pckbd_layout.h"
62 #include <dev/wscons/wsconsio.h>
63 #include <dev/wscons/wskbdvar.h>
64 #include <dev/wscons/wsksymdef.h>
65 #include <dev/wscons/wsksymvar.h>
66 #include <dev/pckbc/wskbdmap_mfii.h>
67 #ifdef WSDISPLAY_COMPAT_RAWKBD
68 #include <dev/hpc/pckbd_encode.h>
69 #endif
70
71 #define VRKIUDEBUG
72 #ifdef VRKIUDEBUG
73 int vrkiu_debug = 0;
74 #define DPRINTF(arg) if (vrkiu_debug) printf arg;
75 #else
76 #define DPRINTF(arg)
77 #endif
78
79 static int vrkiumatch(struct device *, struct cfdata *, void *);
80 static void vrkiuattach(struct device *, struct device *, void *);
81
82 int vrkiu_intr(void *);
83
84 static int vrkiu_init(struct vrkiu_chip *, bus_space_tag_t, bus_space_handle_t);
85 static void vrkiu_write(struct vrkiu_chip *, int, unsigned short);
86 static unsigned short vrkiu_read(struct vrkiu_chip *, int);
87 static int vrkiu_is_console(bus_space_tag_t, bus_space_handle_t);
88 static void vrkiu_scan(struct vrkiu_chip *);
89 static int countbits(int);
90 static void eliminate_phantom_keys(struct vrkiu_chip *, unsigned short *);
91 static int vrkiu_poll(void*);
92 static int vrkiu_input_establish(void*, struct hpckbd_if*);
93
94 CFATTACH_DECL(vrkiu, sizeof(struct vrkiu_softc),
95 vrkiumatch, vrkiuattach, NULL, NULL);
96
97 struct vrkiu_chip *vrkiu_consdata = NULL;
98
99 static inline void
100 vrkiu_write(struct vrkiu_chip *chip, int port, unsigned short val)
101 {
102
103 bus_space_write_2(chip->kc_iot, chip->kc_ioh, port, val);
104 }
105
106 static inline unsigned short
107 vrkiu_read(struct vrkiu_chip *chip, int port)
108 {
109
110 return (bus_space_read_2(chip->kc_iot, chip->kc_ioh, port));
111 }
112
113 static inline int
114 vrkiu_is_console(bus_space_tag_t iot, bus_space_handle_t ioh)
115 {
116
117 if (vrkiu_consdata &&
118 vrkiu_consdata->kc_iot == iot &&
119 vrkiu_consdata->kc_ioh == ioh) {
120 return (1);
121 } else {
122 return (0);
123 }
124 }
125
126 static int
127 vrkiumatch(struct device *parent, struct cfdata *cf, void *aux)
128 {
129
130 return (1);
131 }
132
133 static void
134 vrkiuattach(struct device *parent, struct device *self, void *aux)
135 {
136 struct vrkiu_softc *sc = (struct vrkiu_softc *)self;
137 struct vrip_attach_args *va = aux;
138 struct hpckbd_attach_args haa;
139 int isconsole, res;
140
141 bus_space_tag_t iot = va->va_iot;
142 bus_space_handle_t ioh;
143
144 if (va->va_parent_ioh != NULL)
145 res = bus_space_subregion(iot, va->va_parent_ioh, va->va_addr,
146 va->va_size, &ioh);
147 else
148 res = bus_space_map(iot, va->va_addr, 1, 0, &ioh);
149 if (res != 0) {
150 printf(": can't map bus space\n");
151 return;
152 }
153
154 isconsole = vrkiu_is_console(iot, ioh);
155 if (isconsole) {
156 sc->sc_chip = vrkiu_consdata;
157 } else {
158 sc->sc_chip = &sc->sc_chip_body;
159 vrkiu_init(sc->sc_chip, iot, ioh);
160 }
161 sc->sc_chip->kc_sc = sc;
162
163 if (!(sc->sc_handler =
164 vrip_intr_establish(va->va_vc, va->va_unit, 0, IPL_TTY,
165 vrkiu_intr, sc))) {
166 printf (": can't map interrupt line.\n");
167 return;
168 }
169 /* Level2 register setting */
170 vrip_intr_setmask2(va->va_vc, sc->sc_handler, KIUINT_KDATRDY, 1);
171
172 printf("\n");
173
174 /* attach hpckbd */
175 haa.haa_ic = &sc->sc_chip->kc_if;
176 config_found(self, &haa, hpckbd_print);
177 }
178
179 /*
180 * initialize device
181 */
182 static int
183 vrkiu_init(struct vrkiu_chip *chip, bus_space_tag_t iot,
184 bus_space_handle_t ioh)
185 {
186
187 memset(chip, 0, sizeof(struct vrkiu_chip));
188 chip->kc_iot = iot;
189 chip->kc_ioh = ioh;
190 chip->kc_enabled = 0;
191
192 chip->kc_if.hii_ctx = chip;
193 chip->kc_if.hii_establish = vrkiu_input_establish;
194 chip->kc_if.hii_poll = vrkiu_poll;
195
196 /* set KIU */
197 vrkiu_write(chip, KIURST, 1); /* reset */
198 vrkiu_write(chip, KIUSCANLINE, 0); /* 96keys */
199 vrkiu_write(chip, KIUWKS, 0x18a4); /* XXX: scan timing! */
200 vrkiu_write(chip, KIUWKI, 450);
201 vrkiu_write(chip, KIUSCANREP, 0x8023);
202 /* KEYEN | STPREP = 2 | ATSTP | ATSCAN */
203
204 return (0);
205 }
206
207 int
208 vrkiu_intr(void *arg)
209 {
210 struct vrkiu_softc *sc = arg;
211
212 /* When key scan finisshed, this entry is called. */
213 #if 0
214 DPRINTF(("vrkiu_intr: intr=%x scan=%x\n",
215 vrkiu_read(sc->sc_chip, KIUINT) & 7,
216 vrkiu_read(sc->sc_chip, KIUSCANS) & KIUSCANS_SSTAT_MASK));
217 #endif
218
219 /*
220 * First, we must clear the interrupt register because
221 * vrkiu_scan() may takes long time if a bitmap screen
222 * scrolls up and it makes us to miss some key release
223 * event.
224 */
225 vrkiu_write(sc->sc_chip, KIUINT, 0x7); /* Clear all interrupt */
226
227 #if 1
228 /* just return if kiu is scanning keyboard. */
229 if ((vrkiu_read(sc->sc_chip, KIUSCANS) & KIUSCANS_SSTAT_MASK) ==
230 KIUSCANS_SSTAT_SCANNING)
231 return (0);
232 #endif
233 vrkiu_scan(sc->sc_chip);
234
235 return (0);
236 }
237
238 static int
239 countbits(int d)
240 {
241 int i, n;
242
243 for (i = 0, n = 0; i < NBBY; i++)
244 if (d & (1 << i))
245 n++;
246 return (n);
247 }
248
249 static void
250 eliminate_phantom_keys(struct vrkiu_chip *chip, unsigned short *scandata)
251 {
252 unsigned char inkey[KIU_NSCANLINE], *prevkey, *reskey;
253 int i, j;
254 #ifdef VRKIUDEBUG
255 int modified = 0;
256 static int prevmod = 0;
257 #endif
258
259 reskey = (unsigned char *)scandata;
260 for (i = 0; i < KIU_NSCANLINE; i++)
261 inkey[i] = reskey[i];
262 prevkey = (unsigned char *)chip->kc_scandata;
263
264 for (i = 0; i < KIU_NSCANLINE; i++) {
265 if (countbits(inkey[i]) > 1) {
266 for (j = 0; j < KIU_NSCANLINE; j++) {
267 if (i != j && (inkey[i] & inkey[j])) {
268 #ifdef VRKIUDEBUG
269 modified = 1;
270 if (!prevmod) {
271 DPRINTF(("vrkiu_scan: %x:%02x->%02x",
272 i, inkey[i], prevkey[i]));
273 DPRINTF((" %x:%02x->%02x\n",
274 j, inkey[j], prevkey[j]));
275 }
276 #endif
277 reskey[i] = prevkey[i];
278 reskey[j] = prevkey[j];
279 }
280 }
281 }
282 }
283 #ifdef VRKIUDEBUG
284 prevmod = modified;
285 #endif
286 }
287
288 static void
289 vrkiu_scan(struct vrkiu_chip* chip)
290 {
291 int i, j, modified, mask;
292 unsigned short scandata[KIU_NSCANLINE/2];
293
294 if (!chip->kc_enabled)
295 return;
296
297 for (i = 0; i < KIU_NSCANLINE / 2; i++) {
298 scandata[i] = vrkiu_read(chip, KIUDATP + i * 2);
299 }
300 eliminate_phantom_keys(chip, scandata);
301
302 for (i = 0; i < KIU_NSCANLINE / 2; i++) {
303 modified = scandata[i] ^ chip->kc_scandata[i];
304 chip->kc_scandata[i] = scandata[i];
305 mask = 1;
306 for (j = 0; j < 16; j++, mask <<= 1) {
307 /*
308 * Simultaneous keypresses are resolved by registering
309 * the one with the lowest bit index first.
310 */
311 if (modified & mask) {
312 int key = i * 16 + j;
313 DPRINTF(("vrkiu_scan: %s(%d,%d)\n",
314 (scandata[i] & mask) ? "down" : "up",
315 i, j));
316 hpckbd_input(chip->kc_hpckbd,
317 (scandata[i] & mask), key);
318 }
319 }
320 }
321 }
322
323 /* called from bicons.c */
324 int
325 vrkiu_getc()
326 {
327 static int flag = 1;
328
329 /*
330 * XXX, currently
331 */
332 if (flag) {
333 flag = 0;
334 printf("%s(%d): vrkiu_getc() is not implemented\n",
335 __FILE__, __LINE__);
336 }
337 return (0);
338 }
339
340 /*
341 * hpckbd interface routines
342 */
343 int
344 vrkiu_input_establish(void *ic, struct hpckbd_if *kbdif)
345 {
346 struct vrkiu_chip *kc = ic;
347
348 /* save hpckbd interface */
349 kc->kc_hpckbd = kbdif;
350
351 kc->kc_enabled = 1;
352
353 return (0);
354 }
355
356 int
357 vrkiu_poll(void *ic)
358 {
359 struct vrkiu_chip *kc = ic;
360
361 #if 1
362 /* wait until kiu completes keyboard scan. */
363 while ((vrkiu_read(kc, KIUSCANS) & KIUSCANS_SSTAT_MASK) ==
364 KIUSCANS_SSTAT_SCANNING)
365 /* wait until kiu completes keyboard scan */;
366 #endif
367
368 vrkiu_scan(kc);
369
370 return (0);
371 }
372
373 /*
374 * console support routine
375 */
376 int
377 vrkiu_cnattach(bus_space_tag_t iot, int iobase)
378 {
379 static struct vrkiu_chip vrkiu_consdata_body;
380 bus_space_handle_t ioh;
381
382 if (vrkiu_consdata) {
383 panic("vrkiu is already attached as the console");
384 }
385 if (bus_space_map(iot, iobase, 1, 0, &ioh)) {
386 printf("%s(%d): can't map bus space\n", __FILE__, __LINE__);
387 return (-1);
388 }
389
390 if (vrkiu_init(&vrkiu_consdata_body, iot, ioh) != 0) {
391 DPRINTF(("%s(%d): vrkiu_init() failed\n", __FILE__, __LINE__));
392 return (-1);
393 }
394 vrkiu_consdata = &vrkiu_consdata_body;
395
396 hpckbd_cnattach(&vrkiu_consdata_body.kc_if);
397
398 return (0);
399 }
400