Home | History | Annotate | Line # | Download | only in sbd
kbms_sbdio.c revision 1.8
      1 /*	$NetBSD: kbms_sbdio.c,v 1.8 2008/03/29 08:14:41 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.8 2008/03/29 08:14:41 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 	device_t sc_dev;
     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(device_t, cfdata_t, void *);
     82 void kbms_sbdio_attach(device_t, device_t, void *);
     83 int kbms_sbdio_intr(void *);
     84 
     85 CFATTACH_DECL_NEW(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, void *, int, struct lwp *);
     91 
     92 int mouse_enable(void *);
     93 void mouse_disable(void *);
     94 int mouse_ioctl(void *, u_long, void *, int, struct lwp *);
     95 
     96 bool kbd_init(struct kbms_softc *);
     97 bool 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(device_t parent, cfdata_t cf, 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(device_t parent, device_t self, void *aux)
    145 {
    146 	struct kbms_softc *sc = device_private(self);
    147 	struct sbdio_attach_args *sa = aux;
    148 	struct wsmousedev_attach_args ma;
    149 	struct wskbddev_attach_args ka;
    150 	struct kbms_reg *reg = &sc->sc_reg;
    151 	uint8_t *base;
    152 
    153 	sc->sc_dev = self;
    154 	aprint_normal("\n");
    155 
    156 	base = (uint8_t *)MIPS_PHYS_TO_KSEG1(sa->sa_addr1);
    157 	reg->kbd_csr    = base + 0x00;	/* port B */
    158 	reg->kbd_data   = base + 0x04;
    159 	reg->mouse_csr  = base + 0x08;	/* port A */
    160 	reg->mouse_data = base + 0x0c;
    161 
    162 	if (reg->kbd_csr  == kbms_consreg.kbd_csr &&
    163 	    reg->kbd_data == kbms_consreg.kbd_data)
    164 		ka.console = true;
    165 	else
    166 		ka.console = false;
    167 
    168 	ka.keymap = &kbd_keymapdata;
    169 	ka.accessops = &kbd_accessops;
    170 	ka.accesscookie = self;
    171 
    172 	if (kbd_init(sc) == false) {
    173 		printf("keyboard not connected\n");
    174 		return;
    175 	}
    176 
    177 	sc->sc_wskbd = config_found(self, &ka, wskbddevprint);
    178 
    179 	ma.accessops = &mouse_accessops;
    180 	ma.accesscookie = self;
    181 
    182 	if (sa->sa_flags == 0x0001)
    183 		sc->sc_flags = 1;
    184 	sc->sc_mouse_sig = 0x80;
    185 	if (sc->sc_flags == 1)	/* ER/TR?? */
    186 		sc->sc_mouse_sig = 0x88;
    187 
    188 	mouse_init(sc);
    189 	sc->sc_wsmouse = config_found(self, &ma, wsmousedevprint);
    190 
    191 	intr_establish(sa->sa_irq, kbms_sbdio_intr, self);
    192 }
    193 
    194 int
    195 kbms_sbdio_intr(void *arg)
    196 {
    197 	struct kbms_softc *sc = (void *)arg;
    198 	struct kbms_reg *reg = &sc->sc_reg;
    199 	int v;
    200 
    201 	if (*reg->kbd_csr & ZSRR0_RX_READY) {
    202 		v = *reg->kbd_data;
    203 		wskbd_input(sc->sc_wskbd,
    204 		    v & 0x80 ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN,
    205 		    v & 0x7f);
    206 	}
    207 
    208 	while (*reg->mouse_csr & ZSRR0_RX_READY) {
    209 		int8_t *buf = sc->sc_mouse_buf;
    210 		*reg->mouse_csr = 1;
    211 		if (((v = *reg->mouse_csr) &
    212 		    (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) != 0) {
    213 			/* Error occured. re-initialize */
    214 			printf("initialize mouse. error=%02x\n", v);
    215 			mouse_init(sc);
    216 		} else {
    217 			v = *reg->mouse_data;
    218 			if ((sc->sc_mouse_cnt == 0) &&
    219 			    (v & 0xf8) != sc->sc_mouse_sig) {
    220 				printf("missing first packet. reset. %x\n", v);
    221 				mouse_init(sc);
    222 				continue;
    223 			}
    224 			buf[sc->sc_mouse_cnt++] = v;
    225 
    226 			if (sc->sc_mouse_cnt == MOUSE_PACKET_LEN) {
    227 				int x, y;
    228 				u_int buttons;
    229 				buttons = ~buf[0] & 0x7;
    230 				if (sc->sc_flags == 0) {
    231 					u_int b1 = (buttons & 0x1) << 2;
    232 					u_int b3 = (buttons & 0x4) >> 2;
    233 					buttons = (buttons & 0x2) | b1 | b3;
    234 				}
    235 				x = buf[1] + buf[3];
    236 				y = buf[2] + buf[4];
    237 #ifdef MOUSE_DEBUG
    238 				mouse_debug_print(buttons, x, y);
    239 #endif
    240 				wsmouse_input(sc->sc_wsmouse,
    241 						buttons,
    242 						x, y, 0, 0,
    243 						WSMOUSE_INPUT_DELTA);
    244 				sc->sc_mouse_cnt = 0;
    245 			}
    246 
    247 		}
    248 		*reg->mouse_csr = ZSWR1_REQ_RX | ZSWR1_RIE | ZSWR1_RIE_FIRST;
    249 		(void)*reg->mouse_csr;
    250 	}
    251 
    252 	return 0;
    253 }
    254 
    255 #define	__REG_WR(r, v)							\
    256 do {									\
    257 	*csr = (r);							\
    258 	delay(1);							\
    259 	*csr = (v);							\
    260 	delay(1);							\
    261 } while (/*CONSTCOND*/ 0)
    262 
    263 bool
    264 kbd_init(struct kbms_softc *sc)
    265 {
    266 	struct kbms_reg *reg = &sc->sc_reg;
    267 	volatile uint8_t *csr = reg->kbd_csr;
    268 	int retry = 2;
    269 	int reset_retry = 100;
    270 
    271 	do {
    272 		__REG_WR(9, ZSWR9_B_RESET);
    273 		delay(100);
    274 		__REG_WR(9, ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR);
    275 		__REG_WR(1, 0);
    276 		__REG_WR(4, ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_PARENB);
    277 		__REG_WR(12, BPS_TO_TCONST(KBMS_PCLK / 16, 4800));
    278 		__REG_WR(13, 0);
    279 		__REG_WR(5, ZSWR5_TX_8 | ZSWR5_RTS);
    280 		__REG_WR(3, ZSWR3_RX_8);
    281 		__REG_WR(10, 0);
    282 		__REG_WR(11, ZSWR11_RXCLK_BAUD | ZSWR11_TXCLK_BAUD |
    283 		    ZSWR11_TRXC_OUT_ENA | ZSWR11_TRXC_BAUD);
    284 		__REG_WR(14, ZSWR14_BAUD_FROM_PCLK | ZSWR14_BAUD_ENA);
    285 		__REG_WR(15, 0);
    286 		__REG_WR(5, ZSWR5_TX_8 | ZSWR5_TX_ENABLE);
    287 		__REG_WR(3, ZSWR3_RX_8 | ZSWR3_RX_ENABLE);
    288 		reset_retry *= 2;
    289 	} while (!kbd_reset(sc, reset_retry) && --retry > 0);
    290 
    291 	if (retry == 0) {
    292 		printf("keyboard initialize failed.\n");
    293 		return false;
    294 	}
    295 
    296 	return true;
    297 }
    298 
    299 bool
    300 kbd_reset(struct kbms_softc *sc, int retry)
    301 {
    302 #define	__RETRY_LOOP(x, y)						\
    303 do {									\
    304 	for (i = 0; (x) && (i < retry); i++) {				\
    305 		(void)(y);						\
    306 		delay(10);						\
    307 	}								\
    308 	if (i == retry)							\
    309 		goto error;						\
    310 } while (/*CONSTCOND*/ 0)
    311 	int i;
    312 	struct kbms_reg *reg = &sc->sc_reg;
    313 	volatile uint8_t *csr = reg->kbd_csr;
    314 	volatile uint8_t *data = reg->kbd_data;
    315 	volatile uint8_t dummy;
    316 
    317 	__REG_WR(5, ZSWR5_DTR | ZSWR5_TX_8 | ZSWR5_TX_ENABLE);
    318 	delay(100);
    319 	__RETRY_LOOP(*csr & ZSRR0_RX_READY, dummy = *data);
    320 	*csr = 48;
    321 	__REG_WR(5, ZSWR5_TX_8 | ZSWR5_TX_ENABLE | ZSWR5_RTS);
    322 	__RETRY_LOOP((*csr & ZSRR0_RX_READY) == 0, 0);
    323 	*csr = 1;
    324 	__RETRY_LOOP((*csr & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) != 0, 0);
    325 	__RETRY_LOOP(*data != 0xa0, 0);
    326 	__RETRY_LOOP((*csr & ZSRR0_RX_READY) == 0, 0);
    327 	*csr = 1;
    328 	__RETRY_LOOP((*csr & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) != 0, 0);
    329 	__REG_WR(1, ZSWR1_RIE);
    330 
    331 	/* drain buffer */
    332 	(void)*reg->kbd_data;
    333 #undef __RETRY_LOOP
    334 	return true;
    335  error:
    336 	printf("retry failed.\n");
    337 	return false;
    338 }
    339 
    340 void
    341 mouse_init(struct kbms_softc *sc)
    342 {
    343 	struct kbms_reg *reg = &sc->sc_reg;
    344 	volatile uint8_t *csr = reg->mouse_csr;
    345 	volatile uint8_t *data = reg->mouse_data;
    346 	uint8_t d[] = { 0x02, 0x52, 0x53, 0x3b, 0x4d, 0x54, 0x2c, 0x36, 0x0d };
    347 	int i;
    348 
    349 	__REG_WR(9, ZSWR9_A_RESET);
    350 	delay(100);
    351 	__REG_WR(9, ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR);
    352 	__REG_WR(1, 0);
    353 	__REG_WR(4, ZSWR4_CLK_X16 | ZSWR4_ONESB);
    354 	if (sc->sc_flags & 0x1) {
    355 		__REG_WR(12, BPS_TO_TCONST(KBMS_PCLK / 16, 4800));
    356 		__REG_WR(13, 0);
    357 	} else {
    358 		__REG_WR(12, BPS_TO_TCONST(KBMS_PCLK / 16, 1200));
    359 		__REG_WR(13, 0);
    360 	}
    361 	__REG_WR(5, ZSWR5_DTR | ZSWR5_TX_8 | ZSWR5_RTS);
    362 	__REG_WR(3, ZSWR3_RX_8);
    363 	__REG_WR(10, 0);
    364 	__REG_WR(11, ZSWR11_RXCLK_BAUD | ZSWR11_TXCLK_BAUD |
    365 	    ZSWR11_TRXC_OUT_ENA | ZSWR11_TRXC_BAUD);
    366 	__REG_WR(14, ZSWR14_BAUD_FROM_PCLK);
    367 	__REG_WR(15, 0);
    368 	__REG_WR(5, ZSWR5_DTR | ZSWR5_TX_8 | ZSWR5_TX_ENABLE);
    369 	__REG_WR(3, ZSWR3_RX_8 | ZSWR3_RX_ENABLE);
    370 
    371 	if (sc->sc_flags & 0x1) {
    372 		for (i = 0; i < sizeof d; i++) {
    373 			while ((*csr & ZSRR0_TX_READY) == 0)
    374 				;
    375 			*data = d[i];
    376 		}
    377 	}
    378 
    379 	__REG_WR(1, ZSWR1_RIE);
    380 }
    381 
    382 int
    383 kbd_enable(void *arg, int on)
    384 {
    385 
    386 	/* always active */
    387 	return 0;
    388 }
    389 
    390 void
    391 kbd_set_leds(void *arg, int leds)
    392 {
    393 	struct kbms_softc *sc = arg;
    394 	struct kbms_reg *reg = &sc->sc_reg;
    395 
    396 	sc->sc_leds = leds;
    397 	if (leds & WSKBD_LED_CAPS)
    398 		*reg->kbd_data = 0x92;
    399 	else
    400 		*reg->kbd_data = 0x90;
    401 }
    402 
    403 int
    404 kbd_ioctl(void *arg, u_long cmd, void *data, int flag, struct lwp *l)
    405 {
    406 	struct kbms_softc *sc = arg;
    407 
    408 	switch (cmd) {
    409 	case WSKBDIO_GTYPE:
    410 		*(int *)data = WSKBD_TYPE_EWS4800;
    411 		return 0;
    412 	case WSKBDIO_SETLEDS:
    413 		kbd_set_leds(arg, *(int *)data);
    414 		return 0;
    415 	case WSKBDIO_GETLEDS:
    416 		*(int *)data = sc->sc_leds;
    417 		return 0;
    418 	case WSKBDIO_BELL:
    419 	case WSKBDIO_COMPLEXBELL:
    420 		return 0;
    421 	}
    422 
    423 	return EPASSTHROUGH;
    424 }
    425 
    426 int
    427 kbd_sbdio_cnattach(uint32_t csr, uint32_t data)
    428 {
    429 	struct kbms_softc __softc, *sc;
    430 	struct kbms_reg *reg;
    431 
    432 	kbms_consreg.kbd_csr  = (void *)csr;
    433 	kbms_consreg.kbd_data = (void *)data;
    434 
    435 	/* setup dummy softc for kbd_init() */
    436 	sc = &__softc;
    437 	memset(sc, 0, sizeof(struct kbms_softc));
    438 	reg = &sc->sc_reg;
    439 	reg->kbd_csr  = (void *)csr;
    440 	reg->kbd_data = (void *)data;
    441 
    442 	if (kbd_init(sc) == false)
    443 		return false;
    444 
    445 	wskbd_cnattach(&kbd_consops, &kbms_consreg, &kbd_keymapdata);
    446 	return true;
    447 }
    448 
    449 void
    450 kbd_cngetc(void *arg, u_int *type, int *data)
    451 {
    452 	struct kbms_reg *reg = (void *)arg;
    453 	int v;
    454 
    455 	while ((*reg->kbd_csr & ZSRR0_RX_READY) == 0)
    456 		;
    457 	v = *reg->kbd_data;
    458 	*type = v & 0x80 ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
    459 	*data = v & 0x7f;
    460 }
    461 
    462 void
    463 kbd_cnpollc(void *arg, int on)
    464 {
    465 	static bool __polling = false;
    466 	static int s;
    467 
    468 	if (on && !__polling) {
    469 		s = splhigh();  /* Disable interrupt driven I/O */
    470 		__polling = true;
    471 	} else if (!on && __polling) {
    472 		__polling = false;
    473 		splx(s);        /* Enable interrupt driven I/O */
    474 	}
    475 }
    476 
    477 int
    478 mouse_enable(void *arg)
    479 {
    480 
    481 	/* always active */
    482 	return 0;
    483 }
    484 
    485 void
    486 mouse_disable(void *arg)
    487 {
    488 
    489 	/* always active */
    490 }
    491 
    492 int
    493 mouse_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
    494 {
    495 
    496 	return EPASSTHROUGH;
    497 }
    498 
    499 #ifdef MOUSE_DEBUG
    500 void
    501 mouse_debug_print(u_int buttons, int x, int y)
    502 {
    503 #define	MINMAX(x, min, max)						\
    504 	((x) < (min) ? (min) : ((x) > (max) ? (max) : (x)))
    505 	static int k, __x, __y;
    506 	int i, j;
    507 	char buf[64];
    508 
    509 	__x = MINMAX(__x + x, 0, FB_WIDTH);
    510 	__y = MINMAX(__y + y, 0, FB_HEIGHT);
    511 	*(uint8_t *)(fb.fb_addr + __x + __y * FB_LINEBYTES) = 0xff;
    512 
    513 	sprintf(buf, "%8d %8d", x, y);
    514 	for (i = 0; i < 64 && buf[i]; i++)
    515 		fb_drawchar(480 + i * 12, k, buf[i]);
    516 
    517 	i += 12;
    518 	for (j = 0x80; j > 0; j >>= 1, i++) {
    519 		fb_drawchar(480 + i * 12, k, buttons & j ? '|' : '.');
    520 	}
    521 
    522 	k += 24;
    523 	if (k > 1000)
    524 		k = 0;
    525 
    526 }
    527 #endif
    528