Home | History | Annotate | Line # | Download | only in dev
pckbc_js.c revision 1.3
      1  1.3  thorpej /*	$NetBSD: pckbc_js.c,v 1.3 2002/09/27 20:35:51 thorpej Exp $ */
      2  1.1      uwe 
      3  1.1      uwe /*
      4  1.1      uwe  * Copyright (c) 2002 Valeriy E. Ushakov
      5  1.1      uwe  * All rights reserved.
      6  1.1      uwe  *
      7  1.1      uwe  * Redistribution and use in source and binary forms, with or without
      8  1.1      uwe  * modification, are permitted provided that the following conditions
      9  1.1      uwe  * are met:
     10  1.1      uwe  * 1. Redistributions of source code must retain the above copyright
     11  1.1      uwe  *    notice, this list of conditions and the following disclaimer.
     12  1.1      uwe  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1      uwe  *    notice, this list of conditions and the following disclaimer in the
     14  1.1      uwe  *    documentation and/or other materials provided with the distribution.
     15  1.1      uwe  * 3. The name of the author may not be used to endorse or promote products
     16  1.1      uwe  *    derived from this software without specific prior written permission
     17  1.1      uwe  *
     18  1.1      uwe  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  1.1      uwe  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  1.1      uwe  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  1.1      uwe  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  1.1      uwe  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  1.1      uwe  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  1.1      uwe  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  1.1      uwe  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  1.1      uwe  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  1.1      uwe  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  1.1      uwe  */
     29  1.1      uwe 
     30  1.1      uwe #include <sys/param.h>
     31  1.1      uwe #include <sys/systm.h>
     32  1.1      uwe #include <sys/kernel.h>
     33  1.1      uwe #include <sys/device.h>
     34  1.1      uwe #include <sys/malloc.h>
     35  1.1      uwe 
     36  1.1      uwe #include <machine/autoconf.h>
     37  1.1      uwe #include <machine/bus.h>
     38  1.1      uwe #include <machine/intr.h>
     39  1.1      uwe 
     40  1.1      uwe #include <dev/ic/i8042reg.h>
     41  1.1      uwe #include <dev/ic/pckbcvar.h>
     42  1.1      uwe #include <dev/pckbc/pckbdvar.h>
     43  1.1      uwe 
     44  1.2      uwe #include <dev/ebus/ebusreg.h>
     45  1.2      uwe #include <dev/ebus/ebusvar.h>
     46  1.1      uwe 
     47  1.1      uwe 
     48  1.1      uwe struct pckbc_js_softc {
     49  1.1      uwe 	struct pckbc_softc jsc_pckbc;	/* real "pckbc" softc */
     50  1.1      uwe 
     51  1.1      uwe 	/* kbd and mouse share interrupt in both mr.coffee and krups */
     52  1.1      uwe 	u_int32_t jsc_intr;
     53  1.1      uwe 	int jsc_establised;
     54  1.1      uwe };
     55  1.1      uwe 
     56  1.1      uwe 
     57  1.1      uwe static int	pckbc_obio_match(struct device *, struct cfdata *, void *);
     58  1.1      uwe static void	pckbc_obio_attach(struct device *, struct device *, void *);
     59  1.1      uwe 
     60  1.1      uwe static int	pckbc_ebus_match(struct device *, struct cfdata *, void *);
     61  1.1      uwe static void	pckbc_ebus_attach(struct device *, struct device *, void *);
     62  1.1      uwe 
     63  1.1      uwe static void	pckbc_js_attach_common(	struct pckbc_js_softc *,
     64  1.1      uwe 					bus_space_tag_t, bus_addr_t, int, int);
     65  1.1      uwe static void	pckbc_js_intr_establish(struct pckbc_softc *, pckbc_slot_t);
     66  1.1      uwe 
     67  1.1      uwe /* Mr.Coffee */
     68  1.3  thorpej const struct cfattach pckbc_obio_ca = {
     69  1.1      uwe 	sizeof(struct pckbc_js_softc), pckbc_obio_match, pckbc_obio_attach
     70  1.1      uwe };
     71  1.1      uwe 
     72  1.1      uwe /* ms-IIep */
     73  1.3  thorpej const struct cfattach pckbc_ebus_ca = {
     74  1.1      uwe 	sizeof(struct pckbc_js_softc), pckbc_ebus_match, pckbc_ebus_attach
     75  1.1      uwe };
     76  1.1      uwe 
     77  1.1      uwe 
     78  1.1      uwe #define PCKBC_PROM_DEVICE_NAME "8042"
     79  1.1      uwe 
     80  1.1      uwe static int
     81  1.1      uwe pckbc_obio_match(parent, cf, aux)
     82  1.1      uwe 	struct device *parent;
     83  1.1      uwe 	struct cfdata *cf;
     84  1.1      uwe 	void *aux;
     85  1.1      uwe {
     86  1.1      uwe 	union obio_attach_args *uoba = aux;
     87  1.1      uwe 	struct sbus_attach_args *sa = &uoba->uoba_sbus;
     88  1.1      uwe 
     89  1.1      uwe 	return (strcmp(sa->sa_name, PCKBC_PROM_DEVICE_NAME) == 0);
     90  1.1      uwe }
     91  1.1      uwe 
     92  1.1      uwe static int
     93  1.1      uwe pckbc_ebus_match(parent, cf, aux)
     94  1.1      uwe 	struct device *parent;
     95  1.1      uwe 	struct cfdata *cf;
     96  1.1      uwe 	void *aux;
     97  1.1      uwe {
     98  1.1      uwe 	struct ebus_attach_args *ea = aux;
     99  1.1      uwe 
    100  1.1      uwe 	return (strcmp(ea->ea_name, PCKBC_PROM_DEVICE_NAME) == 0);
    101  1.1      uwe }
    102  1.1      uwe 
    103  1.1      uwe 
    104  1.1      uwe static void
    105  1.1      uwe pckbc_obio_attach(parent, self, aux)
    106  1.1      uwe 	struct device *parent, *self;
    107  1.1      uwe 	void *aux;
    108  1.1      uwe {
    109  1.1      uwe 	struct pckbc_js_softc *jsc = (struct pckbc_js_softc *)self;
    110  1.1      uwe 	union obio_attach_args *uoba = aux;
    111  1.1      uwe 	struct sbus_attach_args *sa = &uoba->uoba_sbus;
    112  1.1      uwe 	bus_space_tag_t iot;
    113  1.1      uwe 	bus_addr_t ioaddr;
    114  1.1      uwe 	int intr, isconsole;
    115  1.1      uwe 
    116  1.1      uwe 	iot = sa->sa_bustag;
    117  1.1      uwe 	ioaddr = BUS_ADDR(sa->sa_slot, sa->sa_offset);
    118  1.1      uwe 	intr = sa->sa_nintr ? sa->sa_pri : /* level */ 13;
    119  1.1      uwe 
    120  1.1      uwe 	/*
    121  1.1      uwe 	 * TODO:
    122  1.1      uwe 	 * . on OFW machines stdin is keyboard node, not 8042 node
    123  1.1      uwe 	 * . on OBP machines 8042 node is faked by boot's prompatch
    124  1.1      uwe 	 *   and PROM's stdin points to zs keyboard (add prom patch?)
    125  1.1      uwe 	 * . we probably want the stdout node to control whether
    126  1.1      uwe 	 *   console goes to serial
    127  1.1      uwe 	 */
    128  1.1      uwe 	isconsole = 1;
    129  1.1      uwe 
    130  1.1      uwe 	pckbc_js_attach_common(jsc, iot, ioaddr, intr, isconsole);
    131  1.1      uwe }
    132  1.1      uwe 
    133  1.1      uwe static void
    134  1.1      uwe pckbc_ebus_attach(parent, self, aux)
    135  1.1      uwe 	struct device *parent, *self;
    136  1.1      uwe 	void *aux;
    137  1.1      uwe {
    138  1.1      uwe 	struct pckbc_js_softc *jsc = (struct pckbc_js_softc *)self;
    139  1.1      uwe 	struct ebus_attach_args *ea = aux;
    140  1.1      uwe 	bus_space_tag_t iot;
    141  1.1      uwe 	bus_addr_t ioaddr;
    142  1.1      uwe 	int intr, isconsole;
    143  1.1      uwe 
    144  1.1      uwe 	iot = ea->ea_bustag;
    145  1.2      uwe 	ioaddr = EBUS_ADDR_FROM_REG(&ea->ea_reg[0]);
    146  1.1      uwe 	intr = ea->ea_nintr ? ea->ea_intr[0] : /* line */ 0;
    147  1.1      uwe 
    148  1.1      uwe 	/* TODO: see comment in pckbc_obio_attach above */
    149  1.1      uwe 	isconsole = 1;
    150  1.1      uwe 
    151  1.1      uwe 	pckbc_js_attach_common(jsc, iot, ioaddr, intr, isconsole);
    152  1.1      uwe }
    153  1.1      uwe 
    154  1.1      uwe 
    155  1.1      uwe static void
    156  1.1      uwe pckbc_js_attach_common(jsc, iot, ioaddr, intr, isconsole)
    157  1.1      uwe 	struct pckbc_js_softc *jsc;
    158  1.1      uwe 	bus_space_tag_t iot;
    159  1.1      uwe 	bus_addr_t ioaddr;
    160  1.1      uwe 	int intr;
    161  1.1      uwe 	int isconsole;
    162  1.1      uwe {
    163  1.1      uwe 	struct pckbc_softc *sc = (struct pckbc_softc *)jsc;
    164  1.1      uwe 	struct pckbc_internal *t;
    165  1.1      uwe 
    166  1.1      uwe 	jsc->jsc_pckbc.intr_establish = pckbc_js_intr_establish;
    167  1.1      uwe 	jsc->jsc_intr = intr;
    168  1.1      uwe 	jsc->jsc_establised = 0;
    169  1.1      uwe 
    170  1.1      uwe 	if (isconsole) {
    171  1.1      uwe 		int status;
    172  1.1      uwe 
    173  1.1      uwe 		status = pckbc_cnattach(iot, ioaddr, KBCMDP, PCKBC_KBD_SLOT);
    174  1.1      uwe 		if (status == 0)
    175  1.1      uwe 			printf(": cnattach ok");
    176  1.1      uwe 		else
    177  1.1      uwe 			printf(": cnattach %d", status);
    178  1.1      uwe 	}
    179  1.1      uwe 
    180  1.1      uwe 	if (pckbc_is_console(iot, ioaddr)) {
    181  1.1      uwe 		t = &pckbc_consdata;
    182  1.1      uwe 		pckbc_console_attached = 1;
    183  1.1      uwe 	} else {
    184  1.1      uwe 		bus_space_handle_t ioh_d, ioh_c;
    185  1.1      uwe 
    186  1.1      uwe 		if (bus_space_map(iot, ioaddr + KBDATAP, 1, 0, &ioh_d) != 0) {
    187  1.1      uwe 			printf(": unable to map data register\n");
    188  1.1      uwe 			return;
    189  1.1      uwe 		}
    190  1.1      uwe 
    191  1.1      uwe 		if (bus_space_map(iot, ioaddr + KBCMDP,  1, 0, &ioh_c) != 0) {
    192  1.1      uwe 			bus_space_unmap(iot, ioh_d, 1);
    193  1.1      uwe 			printf(": unable to map cmd register\n");
    194  1.1      uwe 			return;
    195  1.1      uwe 		}
    196  1.1      uwe 
    197  1.1      uwe 		t = malloc(sizeof(struct pckbc_internal), M_DEVBUF, M_WAITOK);
    198  1.1      uwe 		memset(t, 0, sizeof(struct pckbc_internal));
    199  1.1      uwe 		t->t_iot = iot;
    200  1.1      uwe 		t->t_ioh_d = ioh_d;
    201  1.1      uwe 		t->t_ioh_c = ioh_c;
    202  1.1      uwe 		t->t_addr = ioaddr;
    203  1.1      uwe 		t->t_cmdbyte = KC8_CPU; /* initial command: enable ports */
    204  1.1      uwe 		callout_init(&t->t_cleanup);
    205  1.1      uwe 
    206  1.1      uwe 		(void) pckbc_poll_data1(t, PCKBC_KBD_SLOT, 0); /* flush */
    207  1.1      uwe 
    208  1.1      uwe 		if (pckbc_send_cmd(iot, ioh_c, KBC_SELFTEST) == 0)
    209  1.1      uwe 			printf(": unable to request self test");
    210  1.1      uwe 		else {
    211  1.1      uwe 			int response;
    212  1.1      uwe 
    213  1.1      uwe 			response = pckbc_poll_data1(t, PCKBC_KBD_SLOT, 0);
    214  1.1      uwe 			if (response == 0x55)
    215  1.1      uwe 				printf(": selftest ok");
    216  1.1      uwe 			else
    217  1.1      uwe 				printf(": selftest failed (0x%02x)", response);
    218  1.1      uwe 		}
    219  1.1      uwe 	}
    220  1.1      uwe 
    221  1.1      uwe 	/* crosslink */
    222  1.1      uwe 	t->t_sc = sc;
    223  1.1      uwe 	sc->id = t;
    224  1.1      uwe 
    225  1.1      uwe 	/* finish off the attach */
    226  1.1      uwe 	printf("\n");
    227  1.1      uwe 	pckbc_attach(sc);
    228  1.1      uwe }
    229  1.1      uwe 
    230  1.1      uwe 
    231  1.1      uwe /*
    232  1.1      uwe  * Keyboard and mouse share the interrupt
    233  1.1      uwe  * so don't install interrupt handler twice.
    234  1.1      uwe  */
    235  1.1      uwe static void
    236  1.1      uwe pckbc_js_intr_establish(sc, slot)
    237  1.1      uwe 	struct pckbc_softc *sc;
    238  1.1      uwe 	pckbc_slot_t slot;
    239  1.1      uwe {
    240  1.1      uwe 	struct pckbc_js_softc *jsc = (struct pckbc_js_softc *)sc;
    241  1.1      uwe 	void *res;
    242  1.1      uwe 
    243  1.1      uwe 	if (jsc->jsc_establised) {
    244  1.1      uwe #ifdef DEBUG
    245  1.1      uwe 		printf("%s: %s slot shares interrupt (already established)\n",
    246  1.1      uwe 		       sc->sc_dv.dv_xname, pckbc_slot_names[slot]);
    247  1.1      uwe #endif
    248  1.1      uwe 		return;
    249  1.1      uwe 	}
    250  1.1      uwe 
    251  1.1      uwe 	res = bus_intr_establish(sc->id->t_iot, jsc->jsc_intr,
    252  1.1      uwe 				 IPL_TTY, 0, pckbcintr, sc);
    253  1.1      uwe 	if (res == NULL)
    254  1.1      uwe 		printf("%s: unable to establish %s slot interrupt\n",
    255  1.1      uwe 		       sc->sc_dv.dv_xname, pckbc_slot_names[slot]);
    256  1.1      uwe 	else
    257  1.1      uwe 		jsc->jsc_establised = 1;
    258  1.1      uwe }
    259  1.1      uwe 
    260  1.1      uwe 
    261  1.1      uwe /*
    262  1.1      uwe  * MD hook for use without MI wscons.
    263  1.1      uwe  * Called by pckbc_cnattach().
    264  1.1      uwe  */
    265  1.1      uwe int
    266  1.1      uwe pckbc_machdep_cnattach(constag, slot)
    267  1.1      uwe     pckbc_tag_t constag;
    268  1.1      uwe     pckbc_slot_t slot;
    269  1.1      uwe {
    270  1.1      uwe 
    271  1.1      uwe #if !defined(__HAVE_NWSCONS) && defined(MSIIEP)
    272  1.1      uwe 	/* we do use wscons on krups! */
    273  1.1      uwe 	return (pckbd_cnattach(constag, slot));
    274  1.1      uwe #else
    275  1.1      uwe 	return (0);
    276  1.1      uwe #endif
    277  1.1      uwe }
    278