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