sa1111_kbc.c revision 1.6
1/* $NetBSD: sa1111_kbc.c,v 1.6 2004/04/06 01:32:17 bsh 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.6 2004/04/06 01:32:17 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 85struct 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 int slot; /* KBD_SLOT or AUX_SLOT */ 94 95 int polling; /* don't process data in interrupt handler */ 96 int poll_stat; /* data read from inr handler if polling */ 97 int poll_data; /* status read from intr handler if polling */ 98 99#if NRND > 0 100 rndsource_element_t rnd_source; 101#endif 102 pckbport_tag_t pt; 103}; 104 105static int sackbc_match(struct device *, struct cfdata *, void *); 106static void sackbc_attach(struct device *, struct device *, void *); 107 108static int sackbc_xt_translation(void *, pckbport_slot_t, int); 109#define sackbc_send_devcmd sackbc_send_cmd 110static int sackbc_send_devcmd(void *, pckbport_slot_t, u_char); 111static int sackbc_poll_data1(void *, pckbport_slot_t); 112static void sackbc_slot_enable(void *, pckbport_slot_t, int); 113static void sackbc_intr_establish(void *, pckbport_slot_t); 114static void sackbc_set_poll(void *, pckbport_slot_t, int); 115 116CFATTACH_DECL(sackbc, sizeof(struct sackbc_softc), sackbc_match, 117 sackbc_attach, NULL, NULL); 118 119static struct pckbport_accessops const sackbc_ops = { 120 sackbc_xt_translation, 121 sackbc_send_devcmd, 122 sackbc_poll_data1, 123 sackbc_slot_enable, 124 sackbc_intr_establish, 125 sackbc_set_poll 126}; 127 128#define KBD_DELAY DELAY(8) 129 130/*#define SACKBCDEBUG*/ 131 132#ifdef SACKBCDEBUG 133#define DPRINTF(arg) printf arg 134#else 135#define DPRINTF(arg) 136#endif 137 138 139static int 140sackbc_match(struct device *parent, struct cfdata *cf, void *aux) 141{ 142 struct sa1111_attach_args *aa = (struct sa1111_attach_args *)aux; 143 144 switch (aa->sa_addr) { 145 case SACC_KBD0: case SACC_KBD1: 146 return 1; 147 } 148 return 0; 149} 150 151#if 0 152static int 153sackbc_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 163static int 164sackbc_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 } 182 else 183 pckbportintr(sc->pt, sc->slot, code); 184 return 1; 185 } 186 187 return 0; 188} 189 190static void 191sackbc_intr_establish(void *cookie, pckbport_slot_t slot) 192{ 193 struct sackbc_softc *sc = cookie; 194 195 if (!(sc->polling) && sc->ih_rx==NULL) { 196 sc->ih_rx = sacc_intr_establish( 197 (sacc_chipset_tag_t *)(sc->dev.dv_parent), 198 sc->intr+1, IST_EDGE_RAISE, IPL_TTY, sackbc_rxint, sc); 199 if (sc->ih_rx == NULL) { 200 printf("%s: can't establish interrupt\n", 201 sc->dev.dv_xname); 202 } 203 } 204} 205 206static void 207sackbc_disable_intrhandler(struct sackbc_softc *sc) 208{ 209 if (sc->polling && sc->ih_rx) { 210 sacc_intr_disestablish( 211 (sacc_chipset_tag_t *)(sc->dev.dv_parent), 212 sc->ih_rx); 213 sc->ih_rx = NULL; 214 } 215} 216 217static void 218sackbc_attach(struct device *parent, struct device *self, void *aux) 219{ 220 struct sackbc_softc *sc = (struct sackbc_softc *)self; 221 struct sacc_softc *psc = (struct sacc_softc *)parent; 222 struct sa1111_attach_args *aa = (struct sa1111_attach_args *)aux; 223 struct device *child; 224 uint32_t tmp, clock_bit; 225 int intr, slot; 226 227 switch (aa->sa_addr) { 228 case SACC_KBD0: clock_bit = (1<<6); intr = 21; break; 229 case SACC_KBD1: clock_bit = (1<<5); intr = 18; break; 230 default: 231 return; 232 } 233 234 if (aa->sa_size <= 0) 235 aa->sa_size = SACCKBD_SIZE; 236 if (aa->sa_intr == SACCCF_INTR_DEFAULT) 237 aa->sa_intr = intr; 238 239 sc->iot = psc->sc_iot; 240 if (bus_space_subregion(psc->sc_iot, psc->sc_ioh, 241 aa->sa_addr, aa->sa_size, &sc->ioh)) { 242 printf(": can't map subregion\n"); 243 return; 244 } 245 246 /* enable clock for PS/2 kbd or mouse */ 247 tmp = bus_space_read_4(psc->sc_iot, psc->sc_ioh, SACCSC_SKPCR); 248 bus_space_write_4(psc->sc_iot, psc->sc_ioh, SACCSC_SKPCR, 249 tmp | clock_bit); 250 251 sc->ih_rx = NULL; 252 sc->intr = aa->sa_intr; 253 sc->polling = 0; 254 255 tmp = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_CR); 256 bus_space_write_4(sc->iot, sc->ioh, SACCKBD_CR, tmp | KBDCR_ENA); 257 258 /* XXX: this is necessary to get keyboard working. but I don't know why */ 259 bus_space_write_4(sc->iot, sc->ioh, SACCKBD_CLKDIV, 2); 260 261 tmp = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_STAT); 262 if ((tmp & KBDSTAT_ENA) == 0) { 263 printf("??? can't enable KBD controller\n"); 264 return; 265 } 266 267 printf("\n"); 268 269 sc->pt = pckbport_attach(sc, &sackbc_ops); 270 271 /* 272 * Although there is no such thing as SLOT for SA1111 kbd 273 * controller, pckbd and pms drivers require it. 274 */ 275 for (slot=PCKBPORT_KBD_SLOT; slot <= PCKBPORT_AUX_SLOT; ++slot) { 276 child = pckbport_attach_slot(self, sc->pt, slot); 277 278 if (child == NULL) 279 continue; 280 sc->slot = slot; 281#if NRND > 0 282 rnd_attach_source(&sc->rnd_source, child->dv_xname, 283 RND_TYPE_TTY, 0); 284#endif 285 /* only one of KBD_SLOT or AUX_SLOT is used. */ 286 break; 287 } 288} 289 290 291static inline int 292sackbc_wait_output(struct sackbc_softc *sc) 293{ 294 u_int i, stat; 295 296 for (i = 100000; i; i--){ 297 stat = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_STAT); 298 delay(100); 299 if (stat & KBDSTAT_TXE) 300 return 1; 301 } 302 return 0; 303} 304 305static int 306sackbc_poll_data1(void *cookie, pckbport_slot_t slot) 307{ 308 struct sackbc_softc *sc = cookie; 309 int i, s, stat, c = -1; 310 311 s = spltty(); 312 313 if (sc->polling){ 314 stat = sc->poll_stat; 315 c = sc->poll_data; 316 sc->poll_data = -1; 317 sc->poll_stat = -1; 318 if (stat >= 0 && 319 (stat & (KBDSTAT_RXF|KBDSTAT_STP)) == KBDSTAT_RXF) { 320 splx(s); 321 return c; 322 } 323 } 324 325 /* if 1 port read takes 1us (?), this polls for 100ms */ 326 for (i = 100000; i; i--) { 327 stat = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_STAT); 328 if ((stat & (KBDSTAT_RXF|KBDSTAT_STP)) == KBDSTAT_RXF) { 329 KBD_DELAY; 330 c = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_DATA); 331 break; 332 } 333 } 334 335 splx(s); 336 return (c); 337} 338 339static int 340sackbc_send_cmd(void *cookie, pckbport_slot_t slot, u_char val) 341{ 342 struct sackbc_softc *sc = cookie; 343 344 if (!sackbc_wait_output(sc)) 345 return (0); 346 bus_space_write_1(sc->iot, sc->ioh, SACCKBD_DATA, val); 347 return (1); 348} 349 350 351/* 352 * Glue functions for pckbd on sackbc. 353 * These functions emulate those in dev/ic/pckbc.c. 354 * 355 */ 356 357/* 358 * switch scancode translation on / off 359 * return nonzero on success 360 */ 361static int 362sackbc_xt_translation(void *self, pckbport_slot_t slot, int on) 363{ 364 /* KBD/Mouse controller doesn't have scancode translation */ 365 return !on; 366} 367 368static void 369sackbc_slot_enable(void *self, pckbport_slot_t slot, int on) 370{ 371#if 0 372 struct sackbc_softc *sc = (struct sackbc_softc *) self; 373 int cmd; 374 375 cmd = on ? KBC_KBDENABLE : KBC_KBDDISABLE; 376 if (!sackbc_send_cmd(sc, cmd)) 377 printf("sackbc_slot_enable(%d) failed\n", on); 378#endif 379} 380 381 382static void 383sackbc_set_poll(void *self, pckbport_slot_t slot, int on) 384{ 385 struct sackbc_softc *sc = (struct sackbc_softc *)self; 386 int s; 387 388 s = spltty(); 389 390 if (sc->polling != on) { 391 392 sc->polling = on; 393 394 if (on) { 395 sc->poll_data = sc->poll_stat = -1; 396 sackbc_disable_intrhandler(sc); 397 } 398 else { 399 /* 400 * If disabling polling on a device that's 401 * been configured, make sure there are no 402 * bytes left in the FIFO, holding up the 403 * interrupt line. Otherwise we won't get any 404 * further interrupts. 405 */ 406 sackbc_rxint(sc); 407 sackbc_intr_establish(sc, sc->slot); 408 } 409 } 410 splx(s); 411} 412