kbms_sbdio.c revision 1.1 1 /* $NetBSD: kbms_sbdio.c,v 1.1 2005/12/29 15:20:09 tsutsui Exp $ */
2
3 /*-
4 * Copyright (c) 2004, 2005 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by UCHIYAMA Yasushi.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: kbms_sbdio.c,v 1.1 2005/12/29 15:20:09 tsutsui Exp $");
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/device.h>
45
46 #include <dev/wscons/wsconsio.h>
47 #include <dev/wscons/wskbdvar.h>
48 #include <dev/wscons/wsmousevar.h>
49
50 #include <dev/wscons/wsksymdef.h>
51 #include <dev/wscons/wsksymvar.h>
52
53 #include <dev/ic/z8530reg.h>
54
55 #include <machine/sbdiovar.h>
56
57 #include <ews4800mips/dev/ews4800keymap.h>
58
59 /* 85C30 keyboard, mouse driver */
60
61 struct kbms_reg {
62 volatile uint8_t *kbd_csr;
63 volatile uint8_t *kbd_data;
64 volatile uint8_t *mouse_csr;
65 volatile uint8_t *mouse_data;
66 };
67
68 enum { MOUSE_PACKET_LEN = 5 };
69 struct kbms_softc {
70 struct device sc_dv;
71 struct device *sc_wskbd;
72 struct device *sc_wsmouse;
73 struct kbms_reg sc_reg;
74 int sc_leds;
75 int sc_flags;
76 int sc_mouse_sig;
77 int sc_mouse_cnt;
78 int8_t sc_mouse_buf[MOUSE_PACKET_LEN];
79 };
80
81 int kbms_sbdio_match(struct device *, struct cfdata *, void *);
82 void kbms_sbdio_attach(struct device *, struct device *, void *);
83 int kbms_sbdio_intr(void *);
84
85 CFATTACH_DECL(kbms_sbdio, sizeof(struct kbms_softc),
86 kbms_sbdio_match, kbms_sbdio_attach, NULL, NULL);
87
88 int kbd_enable(void *, int);
89 void kbd_set_leds(void *, int);
90 int kbd_ioctl(void *, u_long, caddr_t, int, struct lwp *);
91
92 int mouse_enable(void *);
93 void mouse_disable(void *);
94 int mouse_ioctl(void *, u_long, caddr_t, int, struct lwp *);
95
96 boolean_t kbd_init(struct kbms_softc *);
97 boolean_t kbd_reset(struct kbms_softc *, int);
98
99 void mouse_init(struct kbms_softc *);
100 #ifdef MOUSE_DEBUG
101 void mouse_debug_print(u_int, int, int);
102 #endif
103
104 int kbd_sbdio_cnattach(uint32_t, uint32_t);
105 void kbd_cngetc(void *, u_int *, int *);
106 void kbd_cnpollc(void *, int);
107
108 static struct kbms_reg kbms_consreg;
109
110 const struct wskbd_consops kbd_consops = {
111 kbd_cngetc,
112 kbd_cnpollc,
113 };
114
115 const struct wskbd_accessops kbd_accessops = {
116 kbd_enable,
117 kbd_set_leds,
118 kbd_ioctl,
119 };
120
121 struct wskbd_mapdata kbd_keymapdata = {
122 ews4800kbd_keydesctab,
123 KB_JP,
124 };
125
126 const struct wsmouse_accessops mouse_accessops = {
127 mouse_enable,
128 mouse_ioctl,
129 mouse_disable,
130 };
131
132 #define KBMS_PCLK (9600 * 512)
133
134
135 int
136 kbms_sbdio_match(struct device *parent, struct cfdata *match, void *aux)
137 {
138 struct sbdio_attach_args *sa = aux;
139
140 return strcmp(sa->sa_name, "zkbms") ? 0 : 1;
141 }
142
143 void
144 kbms_sbdio_attach(struct device *parent, struct device *self, void *aux)
145 {
146 struct sbdio_attach_args *sa = aux;
147 struct wsmousedev_attach_args ma;
148 struct wskbddev_attach_args ka;
149 struct kbms_softc *sc = (void *)self;
150 struct kbms_reg *reg = &sc->sc_reg;
151 uint8_t *base;
152
153 printf(" at %p irq %d\n", (void *)sa->sa_addr1, sa->sa_irq);
154
155 base = (uint8_t *)MIPS_PHYS_TO_KSEG1(sa->sa_addr1);
156 reg->kbd_csr = base + 0x00; /* port B */
157 reg->kbd_data = base + 0x04;
158 reg->mouse_csr = base + 0x08; /* port A */
159 reg->mouse_data = base + 0x0c;
160
161 if (reg->kbd_csr == kbms_consreg.kbd_csr &&
162 reg->kbd_data == kbms_consreg.kbd_data)
163 ka.console = TRUE;
164 else
165 ka.console = FALSE;
166
167 ka.keymap = &kbd_keymapdata;
168 ka.accessops = &kbd_accessops;
169 ka.accesscookie = self;
170
171 if (kbd_init(sc) == FALSE) {
172 printf("keyboard not connected\n");
173 return;
174 }
175
176 sc->sc_wskbd = config_found(self, &ka, wskbddevprint);
177
178 ma.accessops = &mouse_accessops;
179 ma.accesscookie = self;
180
181 if (sa->sa_flags == 0x0001)
182 sc->sc_flags = 1;
183 sc->sc_mouse_sig = 0x80;
184 if (sc->sc_flags == 1) /* ER/TR?? */
185 sc->sc_mouse_sig = 0x88;
186
187 mouse_init(sc);
188 sc->sc_wsmouse = config_found(self, &ma, wsmousedevprint);
189
190 intr_establish(sa->sa_irq, kbms_sbdio_intr, self);
191 }
192
193 int
194 kbms_sbdio_intr(void *arg)
195 {
196 struct kbms_softc *sc = (void *)arg;
197 struct kbms_reg *reg = &sc->sc_reg;
198 int v;
199
200 if (*reg->kbd_csr & ZSRR0_RX_READY) {
201 v = *reg->kbd_data;
202 wskbd_input(sc->sc_wskbd,
203 v & 0x80 ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN,
204 v & 0x7f);
205 }
206
207 while (*reg->mouse_csr & ZSRR0_RX_READY) {
208 int8_t *buf = sc->sc_mouse_buf;
209 *reg->mouse_csr = 1;
210 if (((v = *reg->mouse_csr) &
211 (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) != 0) {
212 /* Error occured. re-initialize */
213 printf("initialize mouse. error=%02x\n", v);
214 mouse_init(sc);
215 } else {
216 v = *reg->mouse_data;
217 if ((sc->sc_mouse_cnt == 0) &&
218 (v & 0xf8) != sc->sc_mouse_sig) {
219 printf("missing first packet. reset. %x\n", v);
220 mouse_init(sc);
221 continue;
222 }
223 buf[sc->sc_mouse_cnt++] = v;
224
225 if (sc->sc_mouse_cnt == MOUSE_PACKET_LEN) {
226 int x, y;
227 u_int buttons;
228 buttons = ~buf[0] & 0x7;
229 if (sc->sc_flags == 0) {
230 u_int b1 = (buttons & 0x1) << 2;
231 u_int b3 = (buttons & 0x4) >> 2;
232 buttons = (buttons & 0x2) | b1 | b3;
233 }
234 x = buf[1] + buf[3];
235 y = buf[2] + buf[4];
236 #ifdef MOUSE_DEBUG
237 mouse_debug_print(buttons, x, y);
238 #endif
239 wsmouse_input(sc->sc_wsmouse, buttons, x, y,
240 0, 0);
241 sc->sc_mouse_cnt = 0;
242 }
243
244 }
245 *reg->mouse_csr = ZSWR1_REQ_RX | ZSWR1_RIE | ZSWR1_RIE_FIRST;
246 (void)*reg->mouse_csr;
247 }
248
249 return 0;
250 }
251
252 #define __REG_WR(r, v) \
253 do { \
254 *csr = (r); \
255 delay(1); \
256 *csr = (v); \
257 delay(1); \
258 } while (/*CONSTCOND*/ 0)
259
260 boolean_t
261 kbd_init(struct kbms_softc *sc)
262 {
263 struct kbms_reg *reg = &sc->sc_reg;
264 volatile uint8_t *csr = reg->kbd_csr;
265 int retry = 2;
266 int reset_retry = 100;
267
268 do {
269 __REG_WR(9, ZSWR9_B_RESET);
270 delay(100);
271 __REG_WR(9, ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR);
272 __REG_WR(1, 0);
273 __REG_WR(4, ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_PARENB);
274 __REG_WR(12, BPS_TO_TCONST(KBMS_PCLK / 16, 4800));
275 __REG_WR(13, 0);
276 __REG_WR(5, ZSWR5_TX_8 | ZSWR5_RTS);
277 __REG_WR(3, ZSWR3_RX_8);
278 __REG_WR(10, 0);
279 __REG_WR(11, ZSWR11_RXCLK_BAUD | ZSWR11_TXCLK_BAUD |
280 ZSWR11_TRXC_OUT_ENA | ZSWR11_TRXC_BAUD);
281 __REG_WR(14, ZSWR14_BAUD_FROM_PCLK | ZSWR14_BAUD_ENA);
282 __REG_WR(15, 0);
283 __REG_WR(5, ZSWR5_TX_8 | ZSWR5_TX_ENABLE);
284 __REG_WR(3, ZSWR3_RX_8 | ZSWR3_RX_ENABLE);
285 reset_retry *= 2;
286 } while (!kbd_reset(sc, reset_retry) && --retry > 0);
287
288 if (retry == 0) {
289 printf("keyboard initialize failed.\n");
290 return FALSE;
291 }
292
293 return TRUE;
294 }
295
296 boolean_t
297 kbd_reset(struct kbms_softc *sc, int retry)
298 {
299 #define __RETRY_LOOP(x, y) \
300 do { \
301 for (i = 0; (x) && (i < retry); i++) { \
302 (void)(y); \
303 delay(10); \
304 } \
305 if (i == retry) \
306 goto error; \
307 } while (/*CONSTCOND*/ 0)
308 int i;
309 struct kbms_reg *reg = &sc->sc_reg;
310 volatile uint8_t *csr = reg->kbd_csr;
311 volatile uint8_t *data = reg->kbd_data;
312 volatile uint8_t dummy;
313
314 __REG_WR(5, ZSWR5_DTR | ZSWR5_TX_8 | ZSWR5_TX_ENABLE);
315 delay(100);
316 __RETRY_LOOP(*csr & ZSRR0_RX_READY, dummy = *data);
317 *csr = 48;
318 __REG_WR(5, ZSWR5_TX_8 | ZSWR5_TX_ENABLE | ZSWR5_RTS);
319 __RETRY_LOOP((*csr & ZSRR0_RX_READY) == 0, 0);
320 *csr = 1;
321 __RETRY_LOOP((*csr & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) != 0, 0);
322 __RETRY_LOOP(*data != 0xa0, 0);
323 __RETRY_LOOP((*csr & ZSRR0_RX_READY) == 0, 0);
324 *csr = 1;
325 __RETRY_LOOP((*csr & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) != 0, 0);
326 __REG_WR(1, ZSWR1_RIE);
327
328 /* drain buffer */
329 (void)*reg->kbd_data;
330 #undef __RETRY_LOOP
331 return TRUE;
332 error:
333 printf("retry failed.\n");
334 return FALSE;
335 }
336
337 void
338 mouse_init(struct kbms_softc *sc)
339 {
340 struct kbms_reg *reg = &sc->sc_reg;
341 volatile uint8_t *csr = reg->mouse_csr;
342 volatile uint8_t *data = reg->mouse_data;
343 uint8_t d[] = { 0x02, 0x52, 0x53, 0x3b, 0x4d, 0x54, 0x2c, 0x36, 0x0d };
344 int i;
345
346 __REG_WR(9, ZSWR9_A_RESET);
347 delay(100);
348 __REG_WR(9, ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR);
349 __REG_WR(1, 0);
350 __REG_WR(4, ZSWR4_CLK_X16 | ZSWR4_ONESB);
351 if (sc->sc_flags & 0x1) {
352 __REG_WR(12, BPS_TO_TCONST(KBMS_PCLK / 16, 4800));
353 __REG_WR(13, 0);
354 } else {
355 __REG_WR(12, BPS_TO_TCONST(KBMS_PCLK / 16, 1200));
356 __REG_WR(13, 0);
357 }
358 __REG_WR(5, ZSWR5_DTR | ZSWR5_TX_8 | ZSWR5_RTS);
359 __REG_WR(3, ZSWR3_RX_8);
360 __REG_WR(10, 0);
361 __REG_WR(11, ZSWR11_RXCLK_BAUD | ZSWR11_TXCLK_BAUD |
362 ZSWR11_TRXC_OUT_ENA | ZSWR11_TRXC_BAUD);
363 __REG_WR(14, ZSWR14_BAUD_FROM_PCLK);
364 __REG_WR(15, 0);
365 __REG_WR(5, ZSWR5_DTR | ZSWR5_TX_8 | ZSWR5_TX_ENABLE);
366 __REG_WR(3, ZSWR3_RX_8 | ZSWR3_RX_ENABLE);
367
368 if (sc->sc_flags & 0x1) {
369 for (i = 0; i < sizeof d; i++) {
370 while ((*csr & ZSRR0_TX_READY) == 0)
371 ;
372 *data = d[i];
373 }
374 }
375
376 __REG_WR(1, ZSWR1_RIE);
377 }
378
379 int
380 kbd_enable(void *arg, int on)
381 {
382
383 /* always active */
384 return 0;
385 }
386
387 void
388 kbd_set_leds(void *arg, int leds)
389 {
390 struct kbms_softc *sc = arg;
391 struct kbms_reg *reg = &sc->sc_reg;
392
393 sc->sc_leds = leds;
394 if (leds & WSKBD_LED_CAPS)
395 *reg->kbd_data = 0x92;
396 else
397 *reg->kbd_data = 0x90;
398 }
399
400 int
401 kbd_ioctl(void *arg, u_long cmd, caddr_t data, int flag, struct lwp *l)
402 {
403 struct kbms_softc *sc = arg;
404
405 switch (cmd) {
406 case WSKBDIO_GTYPE:
407 *(int *)data = WSKBD_TYPE_EWS4800;
408 return 0;
409 case WSKBDIO_SETLEDS:
410 kbd_set_leds(arg, *(int *)data);
411 return 0;
412 case WSKBDIO_GETLEDS:
413 *(int *)data = sc->sc_leds;
414 return 0;
415 case WSKBDIO_BELL:
416 case WSKBDIO_COMPLEXBELL:
417 return 0;
418 }
419
420 return EPASSTHROUGH;
421 }
422
423 int
424 kbd_sbdio_cnattach(uint32_t csr, uint32_t data)
425 {
426 struct kbms_softc __softc, *sc;
427 struct kbms_reg *reg;
428
429 kbms_consreg.kbd_csr = (void *)csr;
430 kbms_consreg.kbd_data = (void *)data;
431
432 /* setup dummy softc for kbd_init() */
433 sc = &__softc;
434 memset(sc, 0, sizeof(struct kbms_softc));
435 reg = &sc->sc_reg;
436 reg->kbd_csr = (void *)csr;
437 reg->kbd_data = (void *)data;
438
439 if (kbd_init(sc) == FALSE)
440 return FALSE;
441
442 wskbd_cnattach(&kbd_consops, &kbms_consreg, &kbd_keymapdata);
443 return TRUE;
444 }
445
446 void
447 kbd_cngetc(void *arg, u_int *type, int *data)
448 {
449 struct kbms_reg *reg = (void *)arg;
450 int v;
451
452 while ((*reg->kbd_csr & ZSRR0_RX_READY) == 0)
453 ;
454 v = *reg->kbd_data;
455 *type = v & 0x80 ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
456 *data = v & 0x7f;
457 }
458
459 void
460 kbd_cnpollc(void *arg, int on)
461 {
462 static boolean_t __polling = FALSE;
463 static int s;
464
465 if (on && !__polling) {
466 s = splhigh(); /* Disable interrupt driven I/O */
467 } else if (!on && __polling) {
468 __polling = FALSE;
469 splx(s); /* Enable interrupt driven I/O */
470 }
471 }
472
473 int
474 mouse_enable(void *arg)
475 {
476
477 /* always active */
478 return 0;
479 }
480
481 void
482 mouse_disable(void *arg)
483 {
484
485 /* always active */
486 }
487
488 int
489 mouse_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct lwp *l)
490 {
491
492 return EPASSTHROUGH;
493 }
494
495 #ifdef MOUSE_DEBUG
496 void
497 mouse_debug_print(u_int buttons, int x, int y)
498 {
499 #define MINMAX(x, min, max) \
500 ((x) < (min) ? (min) : ((x) > (max) ? (max) : (x)))
501 static int k, __x, __y;
502 int i, j;
503 char buf[64];
504
505 __x = MINMAX(__x + x, 0, FB_WIDTH);
506 __y = MINMAX(__y + y, 0, FB_HEIGHT);
507 *(uint8_t *)(fb.fb_addr + __x + __y * FB_LINEBYTES) = 0xff;
508
509 sprintf(buf, "%8d %8d", x, y);
510 for (i = 0; i < 64 && buf[i]; i++)
511 fb_drawchar(480 + i * 12, k, buf[i]);
512
513 i += 12;
514 for (j = 0x80; j > 0; j >>= 1, i++) {
515 fb_drawchar(480 + i * 12, k, buttons & j ? '|' : '.');
516 }
517
518 k += 24;
519 if (k > 1000)
520 k = 0;
521
522 }
523 #endif
524