Home | History | Annotate | Line # | Download | only in dev
m38813c.c revision 1.11.72.1
      1  1.11.72.1      yamt /*	$NetBSD: m38813c.c,v 1.11.72.1 2008/05/16 02:22:27 yamt Exp $ */
      2        1.1       uch 
      3        1.4       uch /*-
      4        1.4       uch  * Copyright (c) 1999, 2001 The NetBSD Foundation, Inc.
      5        1.1       uch  * All rights reserved.
      6        1.1       uch  *
      7        1.4       uch  * This code is derived from software contributed to The NetBSD Foundation
      8        1.4       uch  * by UCHIYAMA Yasushi.
      9        1.4       uch  *
     10        1.1       uch  * Redistribution and use in source and binary forms, with or without
     11        1.1       uch  * modification, are permitted provided that the following conditions
     12        1.1       uch  * are met:
     13        1.1       uch  * 1. Redistributions of source code must retain the above copyright
     14        1.1       uch  *    notice, this list of conditions and the following disclaimer.
     15        1.4       uch  * 2. Redistributions in binary form must reproduce the above copyright
     16        1.4       uch  *    notice, this list of conditions and the following disclaimer in the
     17        1.4       uch  *    documentation and/or other materials provided with the distribution.
     18        1.1       uch  *
     19        1.4       uch  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20        1.4       uch  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21        1.4       uch  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22        1.4       uch  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23        1.4       uch  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24        1.4       uch  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25        1.4       uch  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26        1.4       uch  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27        1.4       uch  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28        1.4       uch  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29        1.4       uch  * POSSIBILITY OF SUCH DAMAGE.
     30        1.1       uch  */
     31        1.1       uch 
     32        1.1       uch /*
     33        1.1       uch  * Device driver for MITUBISHI M38813 controller
     34        1.1       uch  */
     35        1.9     lukem 
     36        1.9     lukem #include <sys/cdefs.h>
     37  1.11.72.1      yamt __KERNEL_RCSID(0, "$NetBSD: m38813c.c,v 1.11.72.1 2008/05/16 02:22:27 yamt Exp $");
     38        1.1       uch 
     39        1.1       uch #include "opt_use_poll.h"
     40        1.1       uch 
     41        1.1       uch #include <sys/param.h>
     42        1.1       uch #include <sys/systm.h>
     43        1.1       uch #include <sys/device.h>
     44        1.1       uch 
     45        1.1       uch #include <machine/bus.h>
     46        1.1       uch #include <machine/intr.h>
     47        1.1       uch 
     48        1.4       uch #include <dev/hpc/hpckbdvar.h>
     49        1.4       uch 
     50        1.1       uch #include <hpcmips/tx/tx39var.h>
     51        1.1       uch #include <hpcmips/tx/txcsbusvar.h>
     52        1.1       uch #include <hpcmips/dev/m38813cvar.h>
     53        1.1       uch 
     54        1.1       uch struct m38813c_chip {
     55        1.1       uch 	bus_space_tag_t		scc_cst;
     56        1.1       uch 	bus_space_handle_t	scc_csh;
     57        1.1       uch 	int			scc_enabled;
     58        1.1       uch 	int t_lastchar;
     59        1.1       uch 	int t_extended;
     60        1.1       uch 	int t_extended1;
     61        1.1       uch 
     62        1.3  takemura 	struct hpckbd_ic_if	scc_if;
     63        1.3  takemura 	struct hpckbd_if	*scc_hpckbd;
     64        1.1       uch };
     65        1.1       uch 
     66        1.1       uch struct m38813c_softc {
     67        1.1       uch 	struct	device		sc_dev;
     68        1.1       uch 	struct m38813c_chip	*sc_chip;
     69        1.1       uch 	tx_chipset_tag_t	sc_tc;
     70        1.1       uch 	void			*sc_ih;
     71        1.1       uch };
     72        1.1       uch 
     73        1.5       uch int	m38813c_match(struct device *, struct cfdata *, void *);
     74        1.5       uch void	m38813c_attach(struct device *, struct device *, void *);
     75        1.5       uch int	m38813c_intr(void *);
     76        1.5       uch int	m38813c_poll(void *);
     77        1.5       uch void	m38813c_ifsetup(struct m38813c_chip *);
     78        1.5       uch int	m38813c_input_establish(void *, struct hpckbd_if *);
     79        1.1       uch 
     80        1.1       uch struct m38813c_chip m38813c_chip;
     81        1.1       uch 
     82        1.8   thorpej CFATTACH_DECL(m38813c, sizeof(struct m38813c_softc),
     83        1.8   thorpej     m38813c_match, m38813c_attach, NULL, NULL);
     84        1.1       uch 
     85        1.1       uch int
     86        1.5       uch m38813c_match(struct device *parent, struct cfdata *cf, void *aux)
     87        1.1       uch {
     88        1.5       uch 
     89        1.5       uch 	return (1);
     90        1.1       uch }
     91        1.1       uch 
     92        1.1       uch void
     93        1.5       uch m38813c_attach(struct device *parent, struct device *self, void *aux)
     94        1.1       uch {
     95        1.1       uch 	struct cs_attach_args *ca = aux;
     96        1.1       uch 	struct m38813c_softc *sc = (void*)self;
     97        1.3  takemura 	struct hpckbd_attach_args haa;
     98        1.1       uch 
     99        1.1       uch 	sc->sc_tc = ca->ca_tc;
    100        1.1       uch 	sc->sc_chip = &m38813c_chip;
    101        1.1       uch 	sc->sc_chip->scc_cst = ca->ca_csio.cstag;
    102        1.1       uch 
    103        1.1       uch 	if (bus_space_map(sc->sc_chip->scc_cst, ca->ca_csio.csbase,
    104        1.5       uch 	    ca->ca_csio.cssize, 0, &sc->sc_chip->scc_csh)) {
    105        1.1       uch 		printf(": can't map i/o space\n");
    106        1.1       uch 	}
    107        1.1       uch 
    108        1.1       uch #ifndef USE_POLL
    109       1.11     peter #error options USE_POLL required.
    110        1.1       uch #endif
    111        1.2       uch 	if (!(sc->sc_ih = tx39_poll_establish(sc->sc_tc, 1,
    112        1.5       uch 	    IPL_TTY, m38813c_intr,
    113        1.5       uch 	    sc))) {
    114        1.1       uch 		printf(": can't establish interrupt\n");
    115        1.1       uch 	}
    116        1.1       uch 
    117        1.1       uch 	printf("\n");
    118        1.1       uch 
    119        1.1       uch 	/* setup upper interface */
    120        1.1       uch 	m38813c_ifsetup(sc->sc_chip);
    121        1.1       uch 
    122        1.3  takemura 	haa.haa_ic = &sc->sc_chip->scc_if;
    123        1.1       uch 
    124        1.3  takemura 	config_found(self, &haa, hpckbd_print);
    125        1.1       uch }
    126        1.1       uch 
    127        1.1       uch void
    128        1.5       uch m38813c_ifsetup(struct m38813c_chip *scc)
    129        1.1       uch {
    130        1.5       uch 
    131        1.3  takemura 	scc->scc_if.hii_ctx		= scc;
    132        1.1       uch 
    133        1.3  takemura 	scc->scc_if.hii_establish	= m38813c_input_establish;
    134        1.3  takemura 	scc->scc_if.hii_poll		= m38813c_intr;
    135        1.1       uch }
    136        1.1       uch 
    137        1.1       uch int
    138        1.5       uch m38813c_cnattach(paddr_t addr)
    139        1.1       uch {
    140        1.1       uch 	struct m38813c_chip *scc = &m38813c_chip;
    141        1.1       uch 
    142        1.1       uch 	scc->scc_csh = MIPS_PHYS_TO_KSEG1(addr);
    143        1.1       uch 
    144        1.1       uch 	m38813c_ifsetup(scc);
    145        1.1       uch 
    146        1.3  takemura 	hpckbd_cnattach(&scc->scc_if);
    147        1.1       uch 
    148        1.5       uch 	return (0);
    149        1.1       uch }
    150        1.1       uch 
    151        1.1       uch int
    152        1.5       uch m38813c_input_establish(void *ic, struct hpckbd_if *kbdif)
    153        1.1       uch {
    154        1.1       uch 	struct m38813c_chip *scc = ic;
    155        1.1       uch 
    156        1.3  takemura 	/* save lower interface */
    157        1.3  takemura 	scc->scc_hpckbd = kbdif;
    158        1.1       uch 
    159        1.1       uch 	scc->scc_enabled = 1;
    160        1.1       uch 
    161        1.5       uch 	return (0);
    162        1.1       uch }
    163        1.1       uch 
    164        1.1       uch #define	KBR_EXTENDED0	0xE0	/* extended key sequence */
    165        1.1       uch #define	KBR_EXTENDED1	0xE1	/* extended key sequence */
    166        1.1       uch 
    167        1.1       uch int
    168        1.5       uch m38813c_intr(void *arg)
    169        1.1       uch {
    170        1.1       uch 	struct m38813c_softc *sc = arg;
    171        1.1       uch 
    172        1.5       uch 	return (m38813c_poll(sc->sc_chip));
    173        1.1       uch }
    174        1.1       uch 
    175        1.1       uch int
    176        1.5       uch m38813c_poll(void *arg)
    177        1.1       uch {
    178        1.1       uch 	struct m38813c_chip *scc = arg;
    179        1.1       uch 	bus_space_tag_t t = scc->scc_cst;
    180        1.1       uch 	bus_space_handle_t h = scc->scc_csh;
    181        1.1       uch 	int datain, type, key;
    182        1.1       uch 
    183        1.1       uch 	if (!scc->scc_enabled) {
    184        1.5       uch 		return (0);
    185        1.1       uch 	}
    186        1.1       uch 
    187        1.1       uch 	datain= bus_space_read_1(t, h, 0);
    188        1.1       uch 
    189        1.3  takemura 	hpckbd_input_hook(scc->scc_hpckbd);
    190        1.1       uch 
    191        1.1       uch 	if (datain == KBR_EXTENDED0) {
    192        1.1       uch 		scc->t_extended = 1;
    193        1.5       uch 		return (0);
    194        1.1       uch 	} else if (datain == KBR_EXTENDED1) {
    195        1.1       uch 		scc->t_extended1 = 2;
    196        1.5       uch 		return (0);
    197        1.1       uch 	}
    198        1.1       uch 
    199        1.1       uch  	/* map extended keys to (unused) codes 128-254 */
    200        1.1       uch 	key = (datain & 0x7f) | (scc->t_extended ? 0x80 : 0);
    201        1.1       uch 	scc->t_extended = 0;
    202        1.1       uch 
    203        1.1       uch 	/*
    204        1.1       uch 	 * process BREAK key (EXT1 1D 45  EXT1 9D C5):
    205        1.1       uch 	 * map to (unused) code 7F
    206        1.1       uch 	 */
    207        1.1       uch 	if (scc->t_extended1 == 2 && (datain == 0x1d || datain == 0x9d)) {
    208        1.1       uch 		scc->t_extended1 = 1;
    209        1.5       uch 		return (0);
    210        1.1       uch 	} else if (scc->t_extended1 == 1 &&
    211        1.5       uch 	    (datain == 0x45 || datain == 0xc5)) {
    212        1.1       uch 		scc->t_extended1 = 0;
    213        1.1       uch 		key = 0x7f;
    214        1.1       uch 	} else if (scc->t_extended1 > 0) {
    215        1.1       uch 		scc->t_extended1 = 0;
    216        1.1       uch 	}
    217        1.1       uch 
    218        1.1       uch 	if (datain & 0x80) {
    219        1.1       uch 		scc->t_lastchar = 0;
    220        1.1       uch 		type = 0;
    221        1.1       uch 	} else {
    222        1.1       uch 		/* Always ignore typematic keys */
    223        1.1       uch 		if (key == scc->t_lastchar)
    224        1.1       uch 			return 0;
    225        1.1       uch 		scc->t_lastchar = key;
    226        1.1       uch 		type = 1;
    227        1.1       uch 	}
    228        1.1       uch 
    229        1.3  takemura 	hpckbd_input(scc->scc_hpckbd, type, key);
    230        1.1       uch 
    231        1.5       uch 	return (0);
    232        1.1       uch }
    233