Home | History | Annotate | Line # | Download | only in sa11x0
sa1111_kbc.c revision 1.10
      1 /*      $NetBSD: sa1111_kbc.c,v 1.10 2008/01/05 00:31:55 ad Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2004  Ben Harris.
      5  * Copyright (c) 2002, 2004  Genetec Corporation.  All rights reserved.
      6  * Written by Hiroyuki Bessho for Genetec Corporation.
      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. The name of Genetec Corporation may not be used to endorse or
     17  *    promote products derived from this software without specific prior
     18  *    written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
     21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GENETEC CORPORATION
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  *
     32  * Driver for keyboard controller in SA-1111 companion chip.
     33  */
     34 /*
     35  * Copyright (c) 1998
     36  *	Matthias Drochner.  All rights reserved.
     37  *
     38  * Redistribution and use in source and binary forms, with or without
     39  * modification, are permitted provided that the following conditions
     40  * are met:
     41  * 1. Redistributions of source code must retain the above copyright
     42  *    notice, this list of conditions and the following disclaimer.
     43  * 2. Redistributions in binary form must reproduce the above copyright
     44  *    notice, this list of conditions and the following disclaimer in the
     45  *    documentation and/or other materials provided with the distribution.
     46  *
     47  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     48  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     49  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     50  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     51  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     52  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     53  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     54  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     55  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     56  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     57  */
     58 
     59 #include <sys/cdefs.h>
     60 __KERNEL_RCSID(0, "$NetBSD: sa1111_kbc.c,v 1.10 2008/01/05 00:31:55 ad Exp $");
     61 
     62 #include <sys/param.h>
     63 #include <sys/systm.h>
     64 #include <sys/types.h>
     65 #include <sys/callout.h>
     66 #include <sys/kernel.h>
     67 #include <sys/proc.h>
     68 #include <sys/conf.h>
     69 #include <sys/device.h>
     70 #include <sys/malloc.h>
     71 #include <sys/errno.h>
     72 #include <sys/queue.h>
     73 #include <sys/bus.h>
     74 
     75 #include <arm/sa11x0/sa1111_reg.h>
     76 #include <arm/sa11x0/sa1111_var.h>
     77 
     78 #include <dev/pckbport/pckbportvar.h>		/* for prototypes */
     79 
     80 #include "pckbd.h"
     81 #include "rnd.h"
     82 #include "locators.h"
     83 
     84 struct sackbc_softc {
     85 	struct device dev;
     86 
     87 	bus_space_tag_t    iot;
     88 	bus_space_handle_t ioh;
     89 
     90 	void	*ih_rx;			/* receive interrupt */
     91 	int	intr;			/* interrupt number */
     92 	int	slot;		/* KBD_SLOT or AUX_SLOT */
     93 
     94 	int	polling;	/* don't process data in interrupt handler */
     95 	int	poll_stat;	/* data read from inr handler if polling */
     96 	int	poll_data;	/* status read from intr handler if polling */
     97 
     98 #if NRND > 0
     99 	rndsource_element_t	rnd_source;
    100 #endif
    101 	pckbport_tag_t pt;
    102 };
    103 
    104 static int	sackbc_match(struct device *, struct cfdata *, void *);
    105 static void	sackbc_attach(struct device *, struct device *, void *);
    106 
    107 static int	sackbc_xt_translation(void *, pckbport_slot_t, int);
    108 #define sackbc_send_devcmd	sackbc_send_cmd
    109 static int	sackbc_send_devcmd(void *, pckbport_slot_t, u_char);
    110 static int	sackbc_poll_data1(void *, pckbport_slot_t);
    111 static void	sackbc_slot_enable(void *, pckbport_slot_t, int);
    112 static void	sackbc_intr_establish(void *, pckbport_slot_t);
    113 static void	sackbc_set_poll(void *, pckbport_slot_t, int);
    114 
    115 CFATTACH_DECL(sackbc, sizeof(struct sackbc_softc), sackbc_match,
    116     sackbc_attach, NULL, NULL);
    117 
    118 static struct pckbport_accessops const sackbc_ops = {
    119 	sackbc_xt_translation,
    120 	sackbc_send_devcmd,
    121 	sackbc_poll_data1,
    122 	sackbc_slot_enable,
    123 	sackbc_intr_establish,
    124 	sackbc_set_poll
    125 };
    126 
    127 #define	KBD_DELAY	DELAY(8)
    128 
    129 /*#define SACKBCDEBUG*/
    130 
    131 #ifdef SACKBCDEBUG
    132 #define DPRINTF(arg)  printf arg
    133 #else
    134 #define DPRINTF(arg)
    135 #endif
    136 
    137 
    138 static int
    139 sackbc_match(struct device *parent, struct cfdata *cf, void *aux)
    140 {
    141 	struct sa1111_attach_args *aa = (struct sa1111_attach_args *)aux;
    142 
    143 	switch (aa->sa_addr) {
    144 	case SACC_KBD0:
    145 	case SACC_KBD1:
    146 		return 1;
    147 	}
    148 	return 0;
    149 }
    150 
    151 #if 0
    152 static int
    153 sackbc_txint(void *cookie)
    154 {
    155 	struct sackbc_softc *sc = cookie;
    156 
    157 	bus_space_read_4(sc->iot, sc->ioh, SACCKBD_STAT);
    158 
    159 	return 0;
    160 }
    161 #endif
    162 
    163 static int
    164 sackbc_rxint(void *cookie)
    165 {
    166 	struct sackbc_softc *sc = cookie;
    167 	int stat, code = -1;
    168 
    169 	stat = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_STAT);
    170 	DPRINTF(("sackbc_rxint stat=%x\n", stat));
    171 	if (stat & KBDSTAT_RXF) {
    172 		code = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_DATA);
    173 
    174 #if NRND > 0
    175 		rnd_add_uint32(&sc->rnd_source, (stat<<8)|data);
    176 #endif
    177 
    178 		if (sc->polling) {
    179 			sc->poll_data = code;
    180 			sc->poll_stat = stat;
    181 		} else
    182 			pckbportintr(sc->pt, sc->slot, code);
    183 		return 1;
    184 	}
    185 
    186 	return 0;
    187 }
    188 
    189 static void
    190 sackbc_intr_establish(void *cookie, pckbport_slot_t slot)
    191 {
    192 	struct sackbc_softc *sc = cookie;
    193 
    194 	if (!(sc->polling) && sc->ih_rx == NULL) {
    195 		sc->ih_rx = sacc_intr_establish(
    196 			(sacc_chipset_tag_t *) device_parent(&sc->dev),
    197 			sc->intr+1, IST_EDGE_RAISE, IPL_TTY, sackbc_rxint, sc);
    198 		if (sc->ih_rx == NULL) {
    199 			printf("%s: can't establish interrupt\n",
    200 			    sc->dev.dv_xname);
    201 		}
    202 	}
    203 }
    204 
    205 static void
    206 sackbc_disable_intrhandler(struct sackbc_softc *sc)
    207 {
    208 	if (sc->polling && sc->ih_rx) {
    209 		sacc_intr_disestablish(
    210 			(sacc_chipset_tag_t *) device_parent(&sc->dev),
    211 			sc->ih_rx);
    212 		sc->ih_rx = NULL;
    213 	}
    214 }
    215 
    216 static	void
    217 sackbc_attach(struct device *parent, struct device *self, void *aux)
    218 {
    219 	struct sackbc_softc *sc = (struct sackbc_softc *)self;
    220 	struct sacc_softc *psc = (struct sacc_softc *)parent;
    221 	struct sa1111_attach_args *aa = (struct sa1111_attach_args *)aux;
    222 	struct device *child;
    223 	uint32_t tmp, clock_bit;
    224 	int intr, slot;
    225 
    226 	switch (aa->sa_addr) {
    227 	case SACC_KBD0: clock_bit = (1<<6); intr = 21; break;
    228 	case SACC_KBD1: clock_bit = (1<<5); intr = 18; break;
    229 	default:
    230 		return;
    231 	}
    232 
    233 	if (aa->sa_size <= 0)
    234 		aa->sa_size = SACCKBD_SIZE;
    235 	if (aa->sa_intr == SACCCF_INTR_DEFAULT)
    236 		aa->sa_intr = intr;
    237 
    238 	sc->iot = psc->sc_iot;
    239 	if (bus_space_subregion(psc->sc_iot, psc->sc_ioh,
    240 	    aa->sa_addr, aa->sa_size, &sc->ioh)) {
    241 		printf(": can't map subregion\n");
    242 		return;
    243 	}
    244 
    245 	/* enable clock for PS/2 kbd or mouse */
    246 	tmp = bus_space_read_4(psc->sc_iot, psc->sc_ioh, SACCSC_SKPCR);
    247 	bus_space_write_4(psc->sc_iot, psc->sc_ioh, SACCSC_SKPCR,
    248 	    tmp | clock_bit);
    249 
    250 	sc->ih_rx = NULL;
    251 	sc->intr = aa->sa_intr;
    252 	sc->polling = 0;
    253 
    254 	tmp = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_CR);
    255 	bus_space_write_4(sc->iot, sc->ioh, SACCKBD_CR, tmp | KBDCR_ENA);
    256 
    257 	/* XXX: this is necessary to get keyboard working. but I don't know why */
    258 	bus_space_write_4(sc->iot, sc->ioh, SACCKBD_CLKDIV, 2);
    259 
    260 	tmp = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_STAT);
    261 	if ((tmp & KBDSTAT_ENA) == 0) {
    262 		printf("??? can't enable KBD controller\n");
    263 		return;
    264 	}
    265 
    266 	printf("\n");
    267 
    268 	sc->pt = pckbport_attach(sc, &sackbc_ops);
    269 
    270 	/*
    271 	 * Although there is no such thing as SLOT for SA-1111 kbd
    272 	 * controller, pckbd and pms drivers require it.
    273 	 */
    274 	for (slot = PCKBPORT_KBD_SLOT; slot <= PCKBPORT_AUX_SLOT; ++slot) {
    275 		child = pckbport_attach_slot(self, sc->pt, slot);
    276 
    277 		if (child == NULL)
    278 			continue;
    279 		sc->slot = slot;
    280 #if NRND > 0
    281 		rnd_attach_source(&sc->rnd_source, child->dv_xname,
    282 		    RND_TYPE_TTY, 0);
    283 #endif
    284 		/* only one of KBD_SLOT or AUX_SLOT is used. */
    285 		break;
    286 	}
    287 }
    288 
    289 
    290 static inline int
    291 sackbc_wait_output(struct sackbc_softc *sc)
    292 {
    293 	u_int i, stat;
    294 
    295 	for (i = 100000; i; i--){
    296 		stat = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_STAT);
    297 		delay(100);
    298 		if (stat & KBDSTAT_TXE)
    299 			return 1;
    300 	}
    301 	return 0;
    302 }
    303 
    304 static int
    305 sackbc_poll_data1(void *cookie, pckbport_slot_t slot)
    306 {
    307 	struct sackbc_softc *sc = cookie;
    308 	int i, s, stat, c = -1;
    309 
    310 	s = spltty();
    311 
    312 	if (sc->polling){
    313 		stat	= sc->poll_stat;
    314 		c	= sc->poll_data;
    315 		sc->poll_data = -1;
    316 		sc->poll_stat = -1;
    317 		if (stat >= 0 &&
    318 		    (stat & (KBDSTAT_RXF|KBDSTAT_STP)) == KBDSTAT_RXF) {
    319 			splx(s);
    320 			return c;
    321 		}
    322 	}
    323 
    324 	/* if 1 port read takes 1us (?), this polls for 100ms */
    325 	for (i = 100000; i; i--) {
    326 		stat = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_STAT);
    327 		if ((stat & (KBDSTAT_RXF|KBDSTAT_STP)) == KBDSTAT_RXF) {
    328 			KBD_DELAY;
    329 			c = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_DATA);
    330 			break;
    331 		}
    332 	}
    333 
    334 	splx(s);
    335 	return c;
    336 }
    337 
    338 static int
    339 sackbc_send_cmd(void *cookie, pckbport_slot_t slot, u_char val)
    340 {
    341 	struct sackbc_softc *sc = cookie;
    342 
    343 	if (!sackbc_wait_output(sc))
    344 		return 0;
    345 	bus_space_write_1(sc->iot, sc->ioh, SACCKBD_DATA, val);
    346 	return 1;
    347 }
    348 
    349 
    350 /*
    351  * Glue functions for pckbd on sackbc.
    352  * These functions emulate those in dev/ic/pckbc.c.
    353  *
    354  */
    355 
    356 /*
    357  * switch scancode translation on / off
    358  * return nonzero on success
    359  */
    360 static int
    361 sackbc_xt_translation(void *self, pckbport_slot_t slot, int on)
    362 {
    363 	/* KBD/Mouse controller doesn't have scancode translation */
    364 	return !on;
    365 }
    366 
    367 static void
    368 sackbc_slot_enable(void *self, pckbport_slot_t slot, int on)
    369 {
    370 #if 0
    371 	struct sackbc_softc *sc = (struct sackbc_softc *) self;
    372 	int cmd;
    373 
    374 	cmd = on ? KBC_KBDENABLE : KBC_KBDDISABLE;
    375 	if (!sackbc_send_cmd(sc, cmd))
    376 		printf("sackbc_slot_enable(%d) failed\n", on);
    377 #endif
    378 }
    379 
    380 
    381 static void
    382 sackbc_set_poll(void *self, pckbport_slot_t slot, int on)
    383 {
    384 	struct sackbc_softc *sc = (struct sackbc_softc *)self;
    385 	int s;
    386 
    387 	s = spltty();
    388 
    389 	if (sc->polling != on) {
    390 
    391 		sc->polling = on;
    392 
    393 		if (on) {
    394 			sc->poll_data = sc->poll_stat = -1;
    395 			sackbc_disable_intrhandler(sc);
    396 		} else {
    397 			/*
    398 			 * If disabling polling on a device that's
    399 			 * been configured, make sure there are no
    400 			 * bytes left in the FIFO, holding up the
    401 			 * interrupt line.  Otherwise we won't get any
    402 			 * further interrupts.
    403 			 */
    404 			sackbc_rxint(sc);
    405 			sackbc_intr_establish(sc, sc->slot);
    406 		}
    407 	}
    408 	splx(s);
    409 }
    410