Home | History | Annotate | Line # | Download | only in dev
j720kbd.c revision 1.2.4.2
      1  1.2.4.2  yamt /*	$NetBSD: j720kbd.c,v 1.2.4.2 2006/06/21 14:51:44 yamt Exp $	*/
      2  1.2.4.2  yamt 
      3  1.2.4.2  yamt /*-
      4  1.2.4.2  yamt  * Copyright (c) 2006 The NetBSD Foundation, Inc.
      5  1.2.4.2  yamt  * All rights reserved.
      6  1.2.4.2  yamt  *
      7  1.2.4.2  yamt  * This code is derived from software contributed to The NetBSD Foundation
      8  1.2.4.2  yamt  * by IWAMOTO Toshihiro and Peter Postma.
      9  1.2.4.2  yamt  *
     10  1.2.4.2  yamt  * Redistribution and use in source and binary forms, with or without
     11  1.2.4.2  yamt  * modification, are permitted provided that the following conditions
     12  1.2.4.2  yamt  * are met:
     13  1.2.4.2  yamt  * 1. Redistributions of source code must retain the above copyright
     14  1.2.4.2  yamt  *    notice, this list of conditions and the following disclaimer.
     15  1.2.4.2  yamt  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.2.4.2  yamt  *    notice, this list of conditions and the following disclaimer in the
     17  1.2.4.2  yamt  *    documentation and/or other materials provided with the distribution.
     18  1.2.4.2  yamt  * 3. All advertising materials mentioning features or use of this software
     19  1.2.4.2  yamt  *    must display the following acknowledgement:
     20  1.2.4.2  yamt  *        This product includes software developed by the NetBSD
     21  1.2.4.2  yamt  *        Foundation, Inc. and its contributors.
     22  1.2.4.2  yamt  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.2.4.2  yamt  *    contributors may be used to endorse or promote products derived
     24  1.2.4.2  yamt  *    from this software without specific prior written permission.
     25  1.2.4.2  yamt  *
     26  1.2.4.2  yamt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.2.4.2  yamt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.2.4.2  yamt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.2.4.2  yamt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.2.4.2  yamt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.2.4.2  yamt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.2.4.2  yamt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.2.4.2  yamt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.2.4.2  yamt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.2.4.2  yamt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.2.4.2  yamt  * POSSIBILITY OF SUCH DAMAGE.
     37  1.2.4.2  yamt  */
     38  1.2.4.2  yamt 
     39  1.2.4.2  yamt /* Jornada 720 keyboard driver. */
     40  1.2.4.2  yamt 
     41  1.2.4.2  yamt #include <sys/cdefs.h>
     42  1.2.4.2  yamt __KERNEL_RCSID(0, "$NetBSD: j720kbd.c,v 1.2.4.2 2006/06/21 14:51:44 yamt Exp $");
     43  1.2.4.2  yamt 
     44  1.2.4.2  yamt #include <sys/param.h>
     45  1.2.4.2  yamt #include <sys/systm.h>
     46  1.2.4.2  yamt #include <sys/device.h>
     47  1.2.4.2  yamt #include <sys/kernel.h>
     48  1.2.4.2  yamt 
     49  1.2.4.2  yamt #include <machine/bootinfo.h>
     50  1.2.4.2  yamt #include <machine/config_hook.h>
     51  1.2.4.2  yamt #include <machine/platid.h>
     52  1.2.4.2  yamt #include <machine/platid_mask.h>
     53  1.2.4.2  yamt 
     54  1.2.4.2  yamt #include <arm/sa11x0/sa11x0_var.h>
     55  1.2.4.2  yamt #include <arm/sa11x0/sa11x0_gpioreg.h>
     56  1.2.4.2  yamt #include <arm/sa11x0/sa11x0_ppcreg.h>
     57  1.2.4.2  yamt #include <arm/sa11x0/sa11x0_sspreg.h>
     58  1.2.4.2  yamt 
     59  1.2.4.2  yamt #include <dev/hpc/hpckbdvar.h>
     60  1.2.4.2  yamt 
     61  1.2.4.2  yamt #include <hpcarm/dev/j720sspvar.h>
     62  1.2.4.2  yamt #include <hpcarm/dev/sed1356var.h>
     63  1.2.4.2  yamt 
     64  1.2.4.2  yamt #ifdef DEBUG
     65  1.2.4.2  yamt #define DPRINTF(arg)	printf arg
     66  1.2.4.2  yamt #else
     67  1.2.4.2  yamt #define DPRINTF(arg)	/* nothing */
     68  1.2.4.2  yamt #endif
     69  1.2.4.2  yamt 
     70  1.2.4.2  yamt struct j720kbd_chip {
     71  1.2.4.2  yamt 	int			scc_enabled;
     72  1.2.4.2  yamt 
     73  1.2.4.2  yamt 	struct j720ssp_softc	*scc_ssp;
     74  1.2.4.2  yamt 
     75  1.2.4.2  yamt 	struct hpckbd_ic_if	scc_if;
     76  1.2.4.2  yamt 	struct hpckbd_if	*scc_hpckbd;
     77  1.2.4.2  yamt };
     78  1.2.4.2  yamt 
     79  1.2.4.2  yamt struct j720kbd_softc {
     80  1.2.4.2  yamt 	struct device		sc_dev;
     81  1.2.4.2  yamt 
     82  1.2.4.2  yamt 	struct j720kbd_chip	*sc_chip;
     83  1.2.4.2  yamt };
     84  1.2.4.2  yamt 
     85  1.2.4.2  yamt static struct j720kbd_chip j720kbd_chip;
     86  1.2.4.2  yamt 
     87  1.2.4.2  yamt static int	j720kbd_match(struct device *, struct cfdata *, void *);
     88  1.2.4.2  yamt static void	j720kbd_attach(struct device *, struct device *, void *);
     89  1.2.4.2  yamt 
     90  1.2.4.2  yamt static void	j720kbd_cnattach(void);
     91  1.2.4.2  yamt static void	j720kbd_ifsetup(struct j720kbd_chip *);
     92  1.2.4.2  yamt static int	j720kbd_input_establish(void *, struct hpckbd_if *);
     93  1.2.4.2  yamt static int	j720kbd_intr(void *);
     94  1.2.4.2  yamt static int	j720kbd_poll(void *);
     95  1.2.4.2  yamt static void	j720kbd_read(struct j720kbd_chip *, char *);
     96  1.2.4.2  yamt static int	j720kbd_button_event(void *, int, long, void *);
     97  1.2.4.2  yamt 
     98  1.2.4.2  yamt CFATTACH_DECL(j720kbd, sizeof(struct j720kbd_softc),
     99  1.2.4.2  yamt     j720kbd_match, j720kbd_attach, NULL, NULL);
    100  1.2.4.2  yamt 
    101  1.2.4.2  yamt 
    102  1.2.4.2  yamt static int
    103  1.2.4.2  yamt j720kbd_match(struct device *parent, struct cfdata *cf, void *aux)
    104  1.2.4.2  yamt {
    105  1.2.4.2  yamt 
    106  1.2.4.2  yamt 	if (!platid_match(&platid, &platid_mask_MACH_HP_JORNADA_7XX))
    107  1.2.4.2  yamt 		return 0;
    108  1.2.4.2  yamt 	if (strcmp(cf->cf_name, "j720kbd") != 0)
    109  1.2.4.2  yamt 		return 0;
    110  1.2.4.2  yamt 
    111  1.2.4.2  yamt 	return 1;
    112  1.2.4.2  yamt }
    113  1.2.4.2  yamt 
    114  1.2.4.2  yamt static void
    115  1.2.4.2  yamt j720kbd_attach(struct device *parent, struct device *self, void *aux)
    116  1.2.4.2  yamt {
    117  1.2.4.2  yamt 	struct j720kbd_softc *sc = (void *)self;
    118  1.2.4.2  yamt 	struct hpckbd_attach_args haa;
    119  1.2.4.2  yamt 
    120  1.2.4.2  yamt 	printf("\n");
    121  1.2.4.2  yamt 
    122  1.2.4.2  yamt 	sc->sc_chip = &j720kbd_chip;
    123  1.2.4.2  yamt 	sc->sc_chip->scc_ssp = (struct j720ssp_softc *)parent;
    124  1.2.4.2  yamt 	sc->sc_chip->scc_enabled = 0;
    125  1.2.4.2  yamt 
    126  1.2.4.2  yamt 	/* Attach console if not using serial. */
    127  1.2.4.2  yamt 	if (!(bootinfo->bi_cnuse & BI_CNUSE_SERIAL))
    128  1.2.4.2  yamt 		j720kbd_cnattach();
    129  1.2.4.2  yamt 
    130  1.2.4.2  yamt 	j720kbd_ifsetup(sc->sc_chip);
    131  1.2.4.2  yamt 
    132  1.2.4.2  yamt 	/* Install interrupt handler. */
    133  1.2.4.2  yamt 	sa11x0_intr_establish(0, 0, 1, IPL_TTY, j720kbd_intr, sc);
    134  1.2.4.2  yamt 
    135  1.2.4.2  yamt 	/* Add config hook for light button event. */
    136  1.2.4.2  yamt 	config_hook(CONFIG_HOOK_BUTTONEVENT,
    137  1.2.4.2  yamt 		    CONFIG_HOOK_BUTTONEVENT_LIGHT,
    138  1.2.4.2  yamt 		    CONFIG_HOOK_SHARE, j720kbd_button_event, sc);
    139  1.2.4.2  yamt 
    140  1.2.4.2  yamt 	/* Attach hpckbd. */
    141  1.2.4.2  yamt 	haa.haa_ic = &sc->sc_chip->scc_if;
    142  1.2.4.2  yamt 	config_found(self, &haa, hpckbd_print);
    143  1.2.4.2  yamt }
    144  1.2.4.2  yamt 
    145  1.2.4.2  yamt static void
    146  1.2.4.2  yamt j720kbd_cnattach(void)
    147  1.2.4.2  yamt {
    148  1.2.4.2  yamt 	struct j720kbd_chip *scc = &j720kbd_chip;
    149  1.2.4.2  yamt 
    150  1.2.4.2  yamt 	/* Initialize interface. */
    151  1.2.4.2  yamt 	j720kbd_ifsetup(scc);
    152  1.2.4.2  yamt 
    153  1.2.4.2  yamt 	/* Attach console. */
    154  1.2.4.2  yamt 	hpckbd_cnattach(&scc->scc_if);
    155  1.2.4.2  yamt }
    156  1.2.4.2  yamt 
    157  1.2.4.2  yamt static void
    158  1.2.4.2  yamt j720kbd_ifsetup(struct j720kbd_chip *scc)
    159  1.2.4.2  yamt {
    160  1.2.4.2  yamt 
    161  1.2.4.2  yamt 	scc->scc_if.hii_ctx = scc;
    162  1.2.4.2  yamt 	scc->scc_if.hii_establish = j720kbd_input_establish;
    163  1.2.4.2  yamt 	scc->scc_if.hii_poll = j720kbd_poll;
    164  1.2.4.2  yamt }
    165  1.2.4.2  yamt 
    166  1.2.4.2  yamt static int
    167  1.2.4.2  yamt j720kbd_input_establish(void *ic, struct hpckbd_if *kbdif)
    168  1.2.4.2  yamt {
    169  1.2.4.2  yamt 	struct j720kbd_chip *scc = ic;
    170  1.2.4.2  yamt 
    171  1.2.4.2  yamt 	/* Save hpckbd interface. */
    172  1.2.4.2  yamt 	scc->scc_hpckbd = kbdif;
    173  1.2.4.2  yamt 
    174  1.2.4.2  yamt 	scc->scc_enabled = 1;
    175  1.2.4.2  yamt 
    176  1.2.4.2  yamt 	return 0;
    177  1.2.4.2  yamt }
    178  1.2.4.2  yamt 
    179  1.2.4.2  yamt static int
    180  1.2.4.2  yamt j720kbd_intr(void *arg)
    181  1.2.4.2  yamt {
    182  1.2.4.2  yamt 	struct j720kbd_softc *sc = arg;
    183  1.2.4.2  yamt 	struct j720ssp_softc *ssp = sc->sc_chip->scc_ssp;
    184  1.2.4.2  yamt 
    185  1.2.4.2  yamt 	bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_EDR, 1);
    186  1.2.4.2  yamt 
    187  1.2.4.2  yamt 	return j720kbd_poll(sc->sc_chip);
    188  1.2.4.2  yamt }
    189  1.2.4.2  yamt 
    190  1.2.4.2  yamt static int
    191  1.2.4.2  yamt j720kbd_poll(void *arg)
    192  1.2.4.2  yamt {
    193  1.2.4.2  yamt 	struct j720kbd_chip *scc = arg;
    194  1.2.4.2  yamt 	int type, key;
    195  1.2.4.2  yamt 	char buf[9], *p;
    196  1.2.4.2  yamt 
    197  1.2.4.2  yamt 	if (!scc->scc_enabled) {
    198  1.2.4.2  yamt 		DPRINTF(("j720kbd_poll: !scc_enabled\n"));
    199  1.2.4.2  yamt 		return 0;
    200  1.2.4.2  yamt 	}
    201  1.2.4.2  yamt 
    202  1.2.4.2  yamt 	j720kbd_read(scc, buf);
    203  1.2.4.2  yamt 
    204  1.2.4.2  yamt 	for (p = buf; *p; p++) {
    205  1.2.4.2  yamt 		type = *p & 0x80 ? 0 : 1;
    206  1.2.4.2  yamt 		key = *p & 0x7f;
    207  1.2.4.2  yamt 
    208  1.2.4.2  yamt 		hpckbd_input(scc->scc_hpckbd, type, key);
    209  1.2.4.2  yamt 	}
    210  1.2.4.2  yamt 
    211  1.2.4.2  yamt 	return 1;
    212  1.2.4.2  yamt }
    213  1.2.4.2  yamt 
    214  1.2.4.2  yamt static void
    215  1.2.4.2  yamt j720kbd_read(struct j720kbd_chip *scc, char *buf)
    216  1.2.4.2  yamt {
    217  1.2.4.2  yamt 	struct j720ssp_softc *ssp = scc->scc_ssp;
    218  1.2.4.2  yamt 	int data, count;
    219  1.2.4.2  yamt 
    220  1.2.4.2  yamt 	bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PCR, 0x2000000);
    221  1.2.4.2  yamt 
    222  1.2.4.2  yamt 	/* Send scan keycode command. */
    223  1.2.4.2  yamt 	if (j720ssp_readwrite(ssp, 1, 0x900, &data, 500) < 0 || data != 0x88) {
    224  1.2.4.2  yamt 		DPRINTF(("j720kbd_read: no dummy received\n"));
    225  1.2.4.2  yamt 		goto out;
    226  1.2.4.2  yamt 	}
    227  1.2.4.2  yamt 
    228  1.2.4.2  yamt 	/* Read number of scancodes available. */
    229  1.2.4.2  yamt 	if (j720ssp_readwrite(ssp, 0, 0x8800, &data, 500) < 0) {
    230  1.2.4.2  yamt 		DPRINTF(("j720kbd_read: unable to read number of scancodes\n"));
    231  1.2.4.2  yamt 		goto out;
    232  1.2.4.2  yamt 	}
    233  1.2.4.2  yamt 	J720SSP_INVERT(data);
    234  1.2.4.2  yamt 	count = data;
    235  1.2.4.2  yamt 
    236  1.2.4.2  yamt 	for (; count; count--) {
    237  1.2.4.2  yamt 		if (j720ssp_readwrite(ssp, 0, 0x8800, &data, 100) < 0)
    238  1.2.4.2  yamt 			goto out;
    239  1.2.4.2  yamt 		J720SSP_INVERT(data);
    240  1.2.4.2  yamt 		*buf++ = data;
    241  1.2.4.2  yamt 	}
    242  1.2.4.2  yamt 	*buf = 0;
    243  1.2.4.2  yamt 	bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PSR, 0x2000000);
    244  1.2.4.2  yamt 
    245  1.2.4.2  yamt 	return;
    246  1.2.4.2  yamt 
    247  1.2.4.2  yamt out:
    248  1.2.4.2  yamt 	*buf = 0;
    249  1.2.4.2  yamt 	bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PSR, 0x2000000);
    250  1.2.4.2  yamt 
    251  1.2.4.2  yamt 	/* reset SSP */
    252  1.2.4.2  yamt 	bus_space_write_4(ssp->sc_iot, ssp->sc_ssph, SASSP_CR0, 0x307);
    253  1.2.4.2  yamt 	delay(100);
    254  1.2.4.2  yamt 	bus_space_write_4(ssp->sc_iot, ssp->sc_ssph, SASSP_CR0, 0x387);
    255  1.2.4.2  yamt 
    256  1.2.4.2  yamt 	DPRINTF(("j720kbd_read: error %x\n", data));
    257  1.2.4.2  yamt }
    258  1.2.4.2  yamt 
    259  1.2.4.2  yamt static int
    260  1.2.4.2  yamt j720kbd_button_event(void *ctx, int type, long id, void *msg)
    261  1.2.4.2  yamt {
    262  1.2.4.2  yamt 
    263  1.2.4.2  yamt 	if (type != CONFIG_HOOK_BUTTONEVENT ||
    264  1.2.4.2  yamt 	    id != CONFIG_HOOK_BUTTONEVENT_LIGHT)
    265  1.2.4.2  yamt 		return 0;
    266  1.2.4.2  yamt 
    267  1.2.4.2  yamt 	sed1356_toggle_lcdlight();
    268  1.2.4.2  yamt 
    269  1.2.4.2  yamt 	return 1;
    270  1.2.4.2  yamt }
    271