Home | History | Annotate | Line # | Download | only in dev
pckbc_js.c revision 1.8
      1 /*	$NetBSD: pckbc_js.c,v 1.8 2003/07/15 00:04:56 lukem 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/cdefs.h>
     31 __KERNEL_RCSID(0, "$NetBSD: pckbc_js.c,v 1.8 2003/07/15 00:04:56 lukem Exp $");
     32 
     33 #include <sys/param.h>
     34 #include <sys/systm.h>
     35 #include <sys/kernel.h>
     36 #include <sys/device.h>
     37 #include <sys/malloc.h>
     38 
     39 #include <machine/autoconf.h>
     40 #include <machine/bus.h>
     41 #include <machine/intr.h>
     42 
     43 #include <dev/ic/i8042reg.h>
     44 #include <dev/ic/pckbcvar.h>
     45 #include <dev/pckbc/pckbdvar.h>
     46 
     47 #include <dev/ebus/ebusreg.h>
     48 #include <dev/ebus/ebusvar.h>
     49 
     50 
     51 struct pckbc_js_softc {
     52 	struct pckbc_softc jsc_pckbc;	/* real "pckbc" softc */
     53 
     54 	/* kbd and mouse share interrupt in both mr.coffee and krups */
     55 	u_int32_t jsc_intr;
     56 	int jsc_establised;
     57 };
     58 
     59 
     60 static int	pckbc_obio_match(struct device *, struct cfdata *, void *);
     61 static void	pckbc_obio_attach(struct device *, struct device *, void *);
     62 
     63 static int	pckbc_ebus_match(struct device *, struct cfdata *, void *);
     64 static void	pckbc_ebus_attach(struct device *, struct device *, void *);
     65 
     66 static void	pckbc_js_attach_common(	struct pckbc_js_softc *,
     67 					bus_space_tag_t, bus_addr_t, int, int);
     68 static void	pckbc_js_intr_establish(struct pckbc_softc *, pckbc_slot_t);
     69 
     70 /* Mr.Coffee */
     71 CFATTACH_DECL(pckbc_obio, sizeof(struct pckbc_js_softc),
     72     pckbc_obio_match, pckbc_obio_attach, NULL, NULL);
     73 
     74 /* ms-IIep */
     75 CFATTACH_DECL(pckbc_ebus, sizeof(struct pckbc_js_softc),
     76     pckbc_ebus_match, pckbc_ebus_attach, NULL, NULL);
     77 
     78 #define PCKBC_PROM_DEVICE_NAME "8042"
     79 
     80 static int
     81 pckbc_obio_match(parent, cf, aux)
     82 	struct device *parent;
     83 	struct cfdata *cf;
     84 	void *aux;
     85 {
     86 	union obio_attach_args *uoba = aux;
     87 	struct sbus_attach_args *sa = &uoba->uoba_sbus;
     88 
     89 	return (strcmp(sa->sa_name, PCKBC_PROM_DEVICE_NAME) == 0);
     90 }
     91 
     92 static int
     93 pckbc_ebus_match(parent, cf, aux)
     94 	struct device *parent;
     95 	struct cfdata *cf;
     96 	void *aux;
     97 {
     98 	struct ebus_attach_args *ea = aux;
     99 
    100 	return (strcmp(ea->ea_name, PCKBC_PROM_DEVICE_NAME) == 0);
    101 }
    102 
    103 
    104 static void
    105 pckbc_obio_attach(parent, self, aux)
    106 	struct device *parent, *self;
    107 	void *aux;
    108 {
    109 	struct pckbc_js_softc *jsc = (struct pckbc_js_softc *)self;
    110 	union obio_attach_args *uoba = aux;
    111 	struct sbus_attach_args *sa = &uoba->uoba_sbus;
    112 	bus_space_tag_t iot;
    113 	bus_addr_t ioaddr;
    114 	int intr, isconsole;
    115 
    116 	iot = sa->sa_bustag;
    117 	ioaddr = BUS_ADDR(sa->sa_slot, sa->sa_offset);
    118 	intr = sa->sa_nintr ? sa->sa_pri : /* level */ 13;
    119 
    120 	/*
    121 	 * TODO:
    122 	 * . on OFW machines stdin is keyboard node, not 8042 node
    123 	 * . on OBP machines 8042 node is faked by boot's prompatch
    124 	 *   and PROM's stdin points to zs keyboard (add prom patch?)
    125 	 * . we probably want the stdout node to control whether
    126 	 *   console goes to serial
    127 	 */
    128 	isconsole = 1;
    129 
    130 	pckbc_js_attach_common(jsc, iot, ioaddr, intr, isconsole);
    131 }
    132 
    133 static void
    134 pckbc_ebus_attach(parent, self, aux)
    135 	struct device *parent, *self;
    136 	void *aux;
    137 {
    138 	struct pckbc_js_softc *jsc = (struct pckbc_js_softc *)self;
    139 	struct ebus_attach_args *ea = aux;
    140 	bus_space_tag_t iot;
    141 	bus_addr_t ioaddr;
    142 	int intr;
    143 	int stdin_node,	node;
    144 	int isconsole;
    145 
    146 	iot = ea->ea_bustag;
    147 	ioaddr = EBUS_ADDR_FROM_REG(&ea->ea_reg[0]);
    148 	intr = ea->ea_nintr ? ea->ea_intr[0] : /* line */ 0;
    149 
    150 	/* search children of "8042" node for stdin (keyboard) */
    151 	stdin_node = prom_instance_to_package(prom_stdin());
    152 	isconsole = 0;
    153 
    154 	for (node = prom_firstchild(ea->ea_node);
    155 	     node != 0; node = prom_nextsibling(node))
    156 		if (node == stdin_node) {
    157 			isconsole = 1;
    158 			break;
    159 		}
    160 
    161 	pckbc_js_attach_common(jsc, iot, ioaddr, intr, isconsole);
    162 }
    163 
    164 
    165 static void
    166 pckbc_js_attach_common(jsc, iot, ioaddr, intr, isconsole)
    167 	struct pckbc_js_softc *jsc;
    168 	bus_space_tag_t iot;
    169 	bus_addr_t ioaddr;
    170 	int intr;
    171 	int isconsole;
    172 {
    173 	struct pckbc_softc *sc = (struct pckbc_softc *)jsc;
    174 	struct pckbc_internal *t;
    175 
    176 	jsc->jsc_pckbc.intr_establish = pckbc_js_intr_establish;
    177 	jsc->jsc_intr = intr;
    178 	jsc->jsc_establised = 0;
    179 
    180 	if (isconsole) {
    181 		int status;
    182 
    183 		status = pckbc_cnattach(iot, ioaddr, KBCMDP, PCKBC_KBD_SLOT);
    184 		if (status == 0)
    185 			printf(": cnattach ok");
    186 		else
    187 			printf(": cnattach %d", status);
    188 	}
    189 
    190 	if (pckbc_is_console(iot, ioaddr)) {
    191 		t = &pckbc_consdata;
    192 		pckbc_console_attached = 1;
    193 	} else {
    194 		bus_space_handle_t ioh_d, ioh_c;
    195 
    196 		if (bus_space_map(iot, ioaddr + KBDATAP, 1, 0, &ioh_d) != 0) {
    197 			printf(": unable to map data register\n");
    198 			return;
    199 		}
    200 
    201 		if (bus_space_map(iot, ioaddr + KBCMDP,  1, 0, &ioh_c) != 0) {
    202 			bus_space_unmap(iot, ioh_d, 1);
    203 			printf(": unable to map cmd register\n");
    204 			return;
    205 		}
    206 
    207 		t = malloc(sizeof(struct pckbc_internal), M_DEVBUF, M_WAITOK);
    208 		memset(t, 0, sizeof(struct pckbc_internal));
    209 		t->t_iot = iot;
    210 		t->t_ioh_d = ioh_d;
    211 		t->t_ioh_c = ioh_c;
    212 		t->t_addr = ioaddr;
    213 		t->t_cmdbyte = KC8_CPU; /* initial command: enable ports */
    214 		callout_init(&t->t_cleanup);
    215 
    216 		(void) pckbc_poll_data1(t, PCKBC_KBD_SLOT, 0); /* flush */
    217 
    218 		if (pckbc_send_cmd(iot, ioh_c, KBC_SELFTEST) == 0)
    219 			printf(": unable to request self test");
    220 		else {
    221 			int response;
    222 
    223 			response = pckbc_poll_data1(t, PCKBC_KBD_SLOT, 0);
    224 			if (response == 0x55)
    225 				printf(": selftest ok");
    226 			else
    227 				printf(": selftest failed (0x%02x)", response);
    228 		}
    229 	}
    230 
    231 	/* crosslink */
    232 	t->t_sc = sc;
    233 	sc->id = t;
    234 
    235 	/* finish off the attach */
    236 	printf("\n");
    237 	pckbc_attach(sc);
    238 }
    239 
    240 
    241 /*
    242  * Keyboard and mouse share the interrupt
    243  * so don't install interrupt handler twice.
    244  */
    245 static void
    246 pckbc_js_intr_establish(sc, slot)
    247 	struct pckbc_softc *sc;
    248 	pckbc_slot_t slot;
    249 {
    250 	struct pckbc_js_softc *jsc = (struct pckbc_js_softc *)sc;
    251 	void *res;
    252 
    253 	if (jsc->jsc_establised) {
    254 #ifdef DEBUG
    255 		printf("%s: %s slot shares interrupt (already established)\n",
    256 		       sc->sc_dv.dv_xname, pckbc_slot_names[slot]);
    257 #endif
    258 		return;
    259 	}
    260 
    261 	res = bus_intr_establish(sc->id->t_iot, jsc->jsc_intr,
    262 				 IPL_TTY, pckbcintr, sc);
    263 	if (res == NULL)
    264 		printf("%s: unable to establish %s slot interrupt\n",
    265 		       sc->sc_dv.dv_xname, pckbc_slot_names[slot]);
    266 	else
    267 		jsc->jsc_establised = 1;
    268 }
    269 
    270 
    271 /*
    272  * MD hook for use without MI wscons.
    273  * Called by pckbc_cnattach().
    274  */
    275 int
    276 pckbc_machdep_cnattach(constag, slot)
    277     pckbc_tag_t constag;
    278     pckbc_slot_t slot;
    279 {
    280 
    281 #if !defined(__HAVE_NWSCONS) && defined(MSIIEP)
    282 	/* we do use wscons on krups! */
    283 	return (pckbd_cnattach(constag, slot));
    284 #else
    285 	return (0);
    286 #endif
    287 }
    288