Home | History | Annotate | Line # | Download | only in sbd
kbms_sbdio.c revision 1.5.4.1
      1 /*	$NetBSD: kbms_sbdio.c,v 1.5.4.1 2007/07/11 19:59:13 mjf 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.5.4.1 2007/07/11 19:59:13 mjf 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, 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(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,
    240 						buttons,
    241 						x, y, 0, 0,
    242 						WSMOUSE_INPUT_DELTA);
    243 				sc->sc_mouse_cnt = 0;
    244 			}
    245 
    246 		}
    247 		*reg->mouse_csr = ZSWR1_REQ_RX | ZSWR1_RIE | ZSWR1_RIE_FIRST;
    248 		(void)*reg->mouse_csr;
    249 	}
    250 
    251 	return 0;
    252 }
    253 
    254 #define	__REG_WR(r, v)							\
    255 do {									\
    256 	*csr = (r);							\
    257 	delay(1);							\
    258 	*csr = (v);							\
    259 	delay(1);							\
    260 } while (/*CONSTCOND*/ 0)
    261 
    262 bool
    263 kbd_init(struct kbms_softc *sc)
    264 {
    265 	struct kbms_reg *reg = &sc->sc_reg;
    266 	volatile uint8_t *csr = reg->kbd_csr;
    267 	int retry = 2;
    268 	int reset_retry = 100;
    269 
    270 	do {
    271 		__REG_WR(9, ZSWR9_B_RESET);
    272 		delay(100);
    273 		__REG_WR(9, ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR);
    274 		__REG_WR(1, 0);
    275 		__REG_WR(4, ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_PARENB);
    276 		__REG_WR(12, BPS_TO_TCONST(KBMS_PCLK / 16, 4800));
    277 		__REG_WR(13, 0);
    278 		__REG_WR(5, ZSWR5_TX_8 | ZSWR5_RTS);
    279 		__REG_WR(3, ZSWR3_RX_8);
    280 		__REG_WR(10, 0);
    281 		__REG_WR(11, ZSWR11_RXCLK_BAUD | ZSWR11_TXCLK_BAUD |
    282 		    ZSWR11_TRXC_OUT_ENA | ZSWR11_TRXC_BAUD);
    283 		__REG_WR(14, ZSWR14_BAUD_FROM_PCLK | ZSWR14_BAUD_ENA);
    284 		__REG_WR(15, 0);
    285 		__REG_WR(5, ZSWR5_TX_8 | ZSWR5_TX_ENABLE);
    286 		__REG_WR(3, ZSWR3_RX_8 | ZSWR3_RX_ENABLE);
    287 		reset_retry *= 2;
    288 	} while (!kbd_reset(sc, reset_retry) && --retry > 0);
    289 
    290 	if (retry == 0) {
    291 		printf("keyboard initialize failed.\n");
    292 		return false;
    293 	}
    294 
    295 	return true;
    296 }
    297 
    298 bool
    299 kbd_reset(struct kbms_softc *sc, int retry)
    300 {
    301 #define	__RETRY_LOOP(x, y)						\
    302 do {									\
    303 	for (i = 0; (x) && (i < retry); i++) {				\
    304 		(void)(y);						\
    305 		delay(10);						\
    306 	}								\
    307 	if (i == retry)							\
    308 		goto error;						\
    309 } while (/*CONSTCOND*/ 0)
    310 	int i;
    311 	struct kbms_reg *reg = &sc->sc_reg;
    312 	volatile uint8_t *csr = reg->kbd_csr;
    313 	volatile uint8_t *data = reg->kbd_data;
    314 	volatile uint8_t dummy;
    315 
    316 	__REG_WR(5, ZSWR5_DTR | ZSWR5_TX_8 | ZSWR5_TX_ENABLE);
    317 	delay(100);
    318 	__RETRY_LOOP(*csr & ZSRR0_RX_READY, dummy = *data);
    319 	*csr = 48;
    320 	__REG_WR(5, ZSWR5_TX_8 | ZSWR5_TX_ENABLE | ZSWR5_RTS);
    321 	__RETRY_LOOP((*csr & ZSRR0_RX_READY) == 0, 0);
    322 	*csr = 1;
    323 	__RETRY_LOOP((*csr & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) != 0, 0);
    324 	__RETRY_LOOP(*data != 0xa0, 0);
    325 	__RETRY_LOOP((*csr & ZSRR0_RX_READY) == 0, 0);
    326 	*csr = 1;
    327 	__RETRY_LOOP((*csr & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) != 0, 0);
    328 	__REG_WR(1, ZSWR1_RIE);
    329 
    330 	/* drain buffer */
    331 	(void)*reg->kbd_data;
    332 #undef __RETRY_LOOP
    333 	return true;
    334  error:
    335 	printf("retry failed.\n");
    336 	return false;
    337 }
    338 
    339 void
    340 mouse_init(struct kbms_softc *sc)
    341 {
    342 	struct kbms_reg *reg = &sc->sc_reg;
    343 	volatile uint8_t *csr = reg->mouse_csr;
    344 	volatile uint8_t *data = reg->mouse_data;
    345 	uint8_t d[] = { 0x02, 0x52, 0x53, 0x3b, 0x4d, 0x54, 0x2c, 0x36, 0x0d };
    346 	int i;
    347 
    348 	__REG_WR(9, ZSWR9_A_RESET);
    349 	delay(100);
    350 	__REG_WR(9, ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR);
    351 	__REG_WR(1, 0);
    352 	__REG_WR(4, ZSWR4_CLK_X16 | ZSWR4_ONESB);
    353 	if (sc->sc_flags & 0x1) {
    354 		__REG_WR(12, BPS_TO_TCONST(KBMS_PCLK / 16, 4800));
    355 		__REG_WR(13, 0);
    356 	} else {
    357 		__REG_WR(12, BPS_TO_TCONST(KBMS_PCLK / 16, 1200));
    358 		__REG_WR(13, 0);
    359 	}
    360 	__REG_WR(5, ZSWR5_DTR | ZSWR5_TX_8 | ZSWR5_RTS);
    361 	__REG_WR(3, ZSWR3_RX_8);
    362 	__REG_WR(10, 0);
    363 	__REG_WR(11, ZSWR11_RXCLK_BAUD | ZSWR11_TXCLK_BAUD |
    364 	    ZSWR11_TRXC_OUT_ENA | ZSWR11_TRXC_BAUD);
    365 	__REG_WR(14, ZSWR14_BAUD_FROM_PCLK);
    366 	__REG_WR(15, 0);
    367 	__REG_WR(5, ZSWR5_DTR | ZSWR5_TX_8 | ZSWR5_TX_ENABLE);
    368 	__REG_WR(3, ZSWR3_RX_8 | ZSWR3_RX_ENABLE);
    369 
    370 	if (sc->sc_flags & 0x1) {
    371 		for (i = 0; i < sizeof d; i++) {
    372 			while ((*csr & ZSRR0_TX_READY) == 0)
    373 				;
    374 			*data = d[i];
    375 		}
    376 	}
    377 
    378 	__REG_WR(1, ZSWR1_RIE);
    379 }
    380 
    381 int
    382 kbd_enable(void *arg, int on)
    383 {
    384 
    385 	/* always active */
    386 	return 0;
    387 }
    388 
    389 void
    390 kbd_set_leds(void *arg, int leds)
    391 {
    392 	struct kbms_softc *sc = arg;
    393 	struct kbms_reg *reg = &sc->sc_reg;
    394 
    395 	sc->sc_leds = leds;
    396 	if (leds & WSKBD_LED_CAPS)
    397 		*reg->kbd_data = 0x92;
    398 	else
    399 		*reg->kbd_data = 0x90;
    400 }
    401 
    402 int
    403 kbd_ioctl(void *arg, u_long cmd, void *data, int flag, struct lwp *l)
    404 {
    405 	struct kbms_softc *sc = arg;
    406 
    407 	switch (cmd) {
    408 	case WSKBDIO_GTYPE:
    409 		*(int *)data = WSKBD_TYPE_EWS4800;
    410 		return 0;
    411 	case WSKBDIO_SETLEDS:
    412 		kbd_set_leds(arg, *(int *)data);
    413 		return 0;
    414 	case WSKBDIO_GETLEDS:
    415 		*(int *)data = sc->sc_leds;
    416 		return 0;
    417 	case WSKBDIO_BELL:
    418 	case WSKBDIO_COMPLEXBELL:
    419 		return 0;
    420 	}
    421 
    422 	return EPASSTHROUGH;
    423 }
    424 
    425 int
    426 kbd_sbdio_cnattach(uint32_t csr, uint32_t data)
    427 {
    428 	struct kbms_softc __softc, *sc;
    429 	struct kbms_reg *reg;
    430 
    431 	kbms_consreg.kbd_csr  = (void *)csr;
    432 	kbms_consreg.kbd_data = (void *)data;
    433 
    434 	/* setup dummy softc for kbd_init() */
    435 	sc = &__softc;
    436 	memset(sc, 0, sizeof(struct kbms_softc));
    437 	reg = &sc->sc_reg;
    438 	reg->kbd_csr  = (void *)csr;
    439 	reg->kbd_data = (void *)data;
    440 
    441 	if (kbd_init(sc) == false)
    442 		return false;
    443 
    444 	wskbd_cnattach(&kbd_consops, &kbms_consreg, &kbd_keymapdata);
    445 	return true;
    446 }
    447 
    448 void
    449 kbd_cngetc(void *arg, u_int *type, int *data)
    450 {
    451 	struct kbms_reg *reg = (void *)arg;
    452 	int v;
    453 
    454 	while ((*reg->kbd_csr & ZSRR0_RX_READY) == 0)
    455 		;
    456 	v = *reg->kbd_data;
    457 	*type = v & 0x80 ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
    458 	*data = v & 0x7f;
    459 }
    460 
    461 void
    462 kbd_cnpollc(void *arg, int on)
    463 {
    464 	static bool __polling = false;
    465 	static int s;
    466 
    467 	if (on && !__polling) {
    468 		s = splhigh();  /* Disable interrupt driven I/O */
    469 		__polling = true;
    470 	} else if (!on && __polling) {
    471 		__polling = false;
    472 		splx(s);        /* Enable interrupt driven I/O */
    473 	}
    474 }
    475 
    476 int
    477 mouse_enable(void *arg)
    478 {
    479 
    480 	/* always active */
    481 	return 0;
    482 }
    483 
    484 void
    485 mouse_disable(void *arg)
    486 {
    487 
    488 	/* always active */
    489 }
    490 
    491 int
    492 mouse_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
    493 {
    494 
    495 	return EPASSTHROUGH;
    496 }
    497 
    498 #ifdef MOUSE_DEBUG
    499 void
    500 mouse_debug_print(u_int buttons, int x, int y)
    501 {
    502 #define	MINMAX(x, min, max)						\
    503 	((x) < (min) ? (min) : ((x) > (max) ? (max) : (x)))
    504 	static int k, __x, __y;
    505 	int i, j;
    506 	char buf[64];
    507 
    508 	__x = MINMAX(__x + x, 0, FB_WIDTH);
    509 	__y = MINMAX(__y + y, 0, FB_HEIGHT);
    510 	*(uint8_t *)(fb.fb_addr + __x + __y * FB_LINEBYTES) = 0xff;
    511 
    512 	sprintf(buf, "%8d %8d", x, y);
    513 	for (i = 0; i < 64 && buf[i]; i++)
    514 		fb_drawchar(480 + i * 12, k, buf[i]);
    515 
    516 	i += 12;
    517 	for (j = 0x80; j > 0; j >>= 1, i++) {
    518 		fb_drawchar(480 + i * 12, k, buttons & j ? '|' : '.');
    519 	}
    520 
    521 	k += 24;
    522 	if (k > 1000)
    523 		k = 0;
    524 
    525 }
    526 #endif
    527