Home | History | Annotate | Line # | Download | only in sa11x0
sa1111_kbc.c revision 1.5
      1 /*      $NetBSD: sa1111_kbc.c,v 1.5 2004/04/06 01:16:34 bsh Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2004  Ben Harris.
      5  * Copyright (c) 2002  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.5 2004/04/06 01:16:34 bsh 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/lock.h>
     74 
     75 #include <machine/bus.h>
     76 #include <arm/sa11x0/sa1111_reg.h>
     77 #include <arm/sa11x0/sa1111_var.h>
     78 
     79 #include <dev/pckbport/pckbportvar.h>		/* for prototypes */
     80 
     81 #include "pckbd.h"
     82 #include "rnd.h"
     83 #include "locators.h"
     84 
     85 struct sackbc_softc {
     86 	struct device dev;
     87 
     88 	bus_space_tag_t    iot;
     89 	bus_space_handle_t ioh;
     90 
     91 	void	*ih_rx;			/* receive interrupt */
     92 	int	intr;			/* interrupt number */
     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 	pckbport_tag_t pt;
     99 };
    100 
    101 static	int	sackbc_match(struct device *, struct cfdata *, void *);
    102 static	void	sackbc_attach(struct device *, struct device *, void *);
    103 
    104 static int	sackbc_xt_translation(void *, pckbport_slot_t, int);
    105 #define sackbc_send_devcmd	sackbc_send_cmd
    106 static int	sackbc_send_devcmd(void *, pckbport_slot_t, u_char);
    107 static int	sackbc_poll_data1(void *, pckbport_slot_t);
    108 static void	sackbc_slot_enable(void *, pckbport_slot_t, int);
    109 static void	sackbc_intr_establish(void *, pckbport_slot_t);
    110 static void	sackbc_set_poll(void *, pckbport_slot_t, int);
    111 
    112 CFATTACH_DECL(sackbc, sizeof(struct sackbc_softc), sackbc_match,
    113     sackbc_attach, NULL, NULL);
    114 
    115 static struct pckbport_accessops const sackbc_ops = {
    116 	sackbc_xt_translation,
    117 	sackbc_send_devcmd,
    118 	sackbc_poll_data1,
    119 	sackbc_slot_enable,
    120 	sackbc_intr_establish,
    121 	sackbc_set_poll
    122 };
    123 
    124 #define	KBD_DELAY	DELAY(8)
    125 
    126 /*#define SACKBCDEBUG*/
    127 
    128 #ifdef SACKBCDEBUG
    129 #define DPRINTF(arg)  printf arg
    130 #else
    131 #define DPRINTF(arg)
    132 #endif
    133 
    134 
    135 static int
    136 sackbc_match(struct device *parent, struct cfdata *cf, void *aux)
    137 {
    138 	struct sa1111_attach_args *aa = (struct sa1111_attach_args *)aux;
    139 
    140 	switch (aa->sa_addr) {
    141 	case SACC_KBD0: case SACC_KBD1:
    142 		return 1;
    143 	}
    144 	return 0;
    145 }
    146 
    147 #if 0
    148 static int
    149 sackbc_txint(void *cookie)
    150 {
    151 	struct sackbc_softc *sc = cookie;
    152 
    153 	bus_space_read_4(sc->iot, sc->ioh, SACCKBD_STAT);
    154 
    155 	return 0;
    156 }
    157 #endif
    158 
    159 static int
    160 sackbc_rxint(void *cookie)
    161 {
    162 	struct sackbc_softc *sc = cookie;
    163 	int stat, code=-1;
    164 
    165 	stat = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_STAT);
    166 	DPRINTF(("sackbc_rxint stat=%x\n", stat));
    167 	if (stat & KBDSTAT_RXF) {
    168 		code = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_DATA);
    169 
    170 		if (sc->polling) {
    171 			sc->poll_data = code;
    172 			sc->poll_stat = stat;
    173 		}
    174 		else
    175 			pckbportintr(sc->pt, PCKBPORT_KBD_SLOT, code);
    176 		return 1;
    177 	}
    178 
    179 	return 0;
    180 }
    181 
    182 static void
    183 sackbc_intr_establish(void *cookie, pckbport_slot_t slot)
    184 {
    185 	struct sackbc_softc *sc = cookie;
    186 
    187 	if (!(sc->polling) && sc->ih_rx==NULL) {
    188 		sc->ih_rx = sacc_intr_establish(
    189 			(sacc_chipset_tag_t *)(sc->dev.dv_parent),
    190 			sc->intr+1, IST_EDGE_RAISE, IPL_TTY, sackbc_rxint, sc);
    191 		if (sc->ih_rx == NULL) {
    192 			printf("%s: can't establish interrupt\n",
    193 			    sc->dev.dv_xname);
    194 		}
    195 	}
    196 }
    197 
    198 static void
    199 sackbc_disable_intrhandler(struct sackbc_softc *sc)
    200 {
    201 	if (sc->polling && sc->ih_rx) {
    202 		sacc_intr_disestablish(
    203 			(sacc_chipset_tag_t *)(sc->dev.dv_parent),
    204 			sc->ih_rx);
    205 		sc->ih_rx = NULL;
    206 	}
    207 }
    208 
    209 static	void
    210 sackbc_attach(struct device *parent, struct device *self, void *aux)
    211 {
    212 	struct sackbc_softc *sc = (struct sackbc_softc *)self;
    213 	struct sacc_softc *psc = (struct sacc_softc *)parent;
    214 	struct sa1111_attach_args *aa = (struct sa1111_attach_args *)aux;
    215 	struct device *child;
    216 	uint32_t tmp, clock_bit;
    217 	int intr;
    218 
    219 	switch (aa->sa_addr) {
    220 	case SACC_KBD0: clock_bit = (1<<6); intr = 21; break;
    221 	case SACC_KBD1: clock_bit = (1<<5); intr = 18; break;
    222 	default:
    223 		return;
    224 	}
    225 
    226 	if (aa->sa_size <= 0)
    227 		aa->sa_size = SACCKBD_SIZE;
    228 	if (aa->sa_intr == SACCCF_INTR_DEFAULT)
    229 		aa->sa_intr = intr;
    230 
    231 	sc->iot = psc->sc_iot;
    232 	if (bus_space_subregion(psc->sc_iot, psc->sc_ioh,
    233 	    aa->sa_addr, aa->sa_size, &sc->ioh)) {
    234 		printf(": can't map subregion\n");
    235 		return;
    236 	}
    237 
    238 	/* enable clock for PS/2 kbd or mouse */
    239 	tmp = bus_space_read_4(psc->sc_iot, psc->sc_ioh, SACCSC_SKPCR);
    240 	bus_space_write_4(psc->sc_iot, psc->sc_ioh, SACCSC_SKPCR,
    241 	    tmp | clock_bit);
    242 
    243 	sc->ih_rx = NULL;
    244 	sc->intr = aa->sa_intr;
    245 	sc->polling = 0;
    246 
    247 	tmp = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_CR);
    248 	bus_space_write_4(sc->iot, sc->ioh, SACCKBD_CR, tmp | KBDCR_ENA);
    249 
    250 	/* XXX: this is necessary to get keyboard working. but I don't know why */
    251 	bus_space_write_4(sc->iot, sc->ioh, SACCKBD_CLKDIV, 2);
    252 
    253 	tmp = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_STAT);
    254 	if ((tmp & KBDSTAT_ENA) == 0) {
    255 		printf("??? can't enable KBD controller\n");
    256 		return;
    257 	}
    258 
    259 	printf("\n");
    260 
    261 	sc->pt = pckbport_attach(sc, &sackbc_ops);
    262 
    263 	child = pckbport_attach_slot(self, sc->pt, PCKBPORT_KBD_SLOT);
    264 
    265 #if 0 && NRND > 0			/* XXX: not yet */
    266 	    if (child != NULL && (t->t_slotdata[slot] != NULL))
    267 		    rnd_attach_source(&t->t_slotdata[slot]->rnd_source,
    268 			child->dv_xname, RND_TYPE_TTY, 0);
    269 #endif
    270 }
    271 
    272 
    273 static inline int
    274 sackbc_wait_output(struct sackbc_softc *sc)
    275 {
    276 	u_int i, stat;
    277 
    278 	for (i = 100000; i; i--){
    279 		stat = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_STAT);
    280 		delay(100);
    281 		if (stat & KBDSTAT_TXE)
    282 			return 1;
    283 	}
    284 	return 0;
    285 }
    286 
    287 static int
    288 sackbc_poll_data1(void *cookie, pckbport_slot_t slot)
    289 {
    290 	struct sackbc_softc *sc = cookie;
    291 	int i, s, stat, c = -1;
    292 
    293 	s = spltty();
    294 
    295 	if (sc->polling){
    296 		stat	= sc->poll_stat;
    297 		c	= sc->poll_data;
    298 		sc->poll_data = -1;
    299 		sc->poll_stat = -1;
    300 		if (stat >= 0 &&
    301 		    (stat & (KBDSTAT_RXF|KBDSTAT_STP)) == KBDSTAT_RXF) {
    302 			splx(s);
    303 			return c;
    304 		}
    305 	}
    306 
    307 	/* if 1 port read takes 1us (?), this polls for 100ms */
    308 	for (i = 100000; i; i--) {
    309 		stat = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_STAT);
    310 		if ((stat & (KBDSTAT_RXF|KBDSTAT_STP)) == KBDSTAT_RXF) {
    311 			KBD_DELAY;
    312 			c = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_DATA);
    313 			break;
    314 		}
    315 	}
    316 
    317 	splx(s);
    318 	return (c);
    319 }
    320 
    321 static int
    322 sackbc_send_cmd(void *cookie, pckbport_slot_t slot, u_char val)
    323 {
    324 	struct sackbc_softc *sc = cookie;
    325 
    326 	if (!sackbc_wait_output(sc))
    327 		return (0);
    328 	bus_space_write_1(sc->iot, sc->ioh, SACCKBD_DATA, val);
    329 	return (1);
    330 }
    331 
    332 
    333 /*
    334  * Glue functions for pckbd on sackbc.
    335  * These functions emulate those in dev/ic/pckbc.c.
    336  *
    337  */
    338 
    339 /*
    340  * switch scancode translation on / off
    341  * return nonzero on success
    342  */
    343 static int
    344 sackbc_xt_translation(void *self, pckbport_slot_t slot, int on)
    345 {
    346 	/* KBD/Mouse controller doesn't have scancode translation */
    347 	return !on;
    348 }
    349 
    350 static void
    351 sackbc_slot_enable(void *self, pckbport_slot_t slot, int on)
    352 {
    353 #if 0
    354 	struct sackbc_softc *sc = (struct sackbc_softc *) self;
    355 	int cmd;
    356 
    357 	cmd = on ? KBC_KBDENABLE : KBC_KBDDISABLE;
    358 	if (!sackbc_send_cmd(sc, cmd))
    359 		printf("sackbc_slot_enable(%d) failed\n", on);
    360 #endif
    361 }
    362 
    363 
    364 static void
    365 sackbc_set_poll(void *self, pckbport_slot_t slot, int on)
    366 {
    367 	struct sackbc_softc *sc = (struct sackbc_softc *)self;
    368 	int s;
    369 
    370 	s = spltty();
    371 
    372 	if (sc->polling != on) {
    373 
    374 		sc->polling = on;
    375 
    376 		if (on) {
    377 			sc->poll_data = sc->poll_stat = -1;
    378 			sackbc_disable_intrhandler(sc);
    379 		}
    380 		else {
    381 			/*
    382 			 * If disabling polling on a device that's
    383 			 * been configured, make sure there are no
    384 			 * bytes left in the FIFO, holding up the
    385 			 * interrupt line.  Otherwise we won't get any
    386 			 * further interrupts.
    387 			 */
    388 			sackbc_rxint(sc);
    389 			sackbc_intr_establish(sc, PCKBPORT_KBD_SLOT);
    390 		}
    391 	}
    392 	splx(s);
    393 }
    394