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