Home | History | Annotate | Line # | Download | only in dev
pckbc_ebus.c revision 1.1.18.1
      1  1.1.18.1  skrll /*	$NetBSD: pckbc_ebus.c,v 1.1.18.1 2015/09/22 12:05:52 skrll Exp $ */
      2       1.1    jdc 
      3       1.1    jdc /*
      4       1.1    jdc  * Copyright (c) 2002 Valeriy E. Ushakov
      5       1.1    jdc  * All rights reserved.
      6       1.1    jdc  *
      7       1.1    jdc  * Redistribution and use in source and binary forms, with or without
      8       1.1    jdc  * modification, are permitted provided that the following conditions
      9       1.1    jdc  * are met:
     10       1.1    jdc  * 1. Redistributions of source code must retain the above copyright
     11       1.1    jdc  *    notice, this list of conditions and the following disclaimer.
     12       1.1    jdc  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1    jdc  *    notice, this list of conditions and the following disclaimer in the
     14       1.1    jdc  *    documentation and/or other materials provided with the distribution.
     15       1.1    jdc  * 3. The name of the author may not be used to endorse or promote products
     16       1.1    jdc  *    derived from this software without specific prior written permission
     17       1.1    jdc  *
     18       1.1    jdc  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19       1.1    jdc  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20       1.1    jdc  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21       1.1    jdc  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22       1.1    jdc  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23       1.1    jdc  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24       1.1    jdc  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25       1.1    jdc  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26       1.1    jdc  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27       1.1    jdc  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28       1.1    jdc  */
     29       1.1    jdc 
     30       1.1    jdc #include <sys/cdefs.h>
     31  1.1.18.1  skrll __KERNEL_RCSID(0, "$NetBSD: pckbc_ebus.c,v 1.1.18.1 2015/09/22 12:05:52 skrll Exp $");
     32       1.1    jdc 
     33       1.1    jdc #include <sys/param.h>
     34       1.1    jdc #include <sys/systm.h>
     35       1.1    jdc #include <sys/kernel.h>
     36       1.1    jdc #include <sys/device.h>
     37       1.1    jdc #include <sys/malloc.h>
     38       1.1    jdc #include <sys/bus.h>
     39       1.1    jdc #include <sys/intr.h>
     40       1.1    jdc 
     41       1.1    jdc #include <machine/autoconf.h>
     42       1.1    jdc 
     43       1.1    jdc #include <dev/ic/i8042reg.h>
     44       1.1    jdc #include <dev/ic/pckbcvar.h>
     45       1.1    jdc #include <dev/pckbport/pckbportvar.h>
     46       1.1    jdc 
     47       1.1    jdc #include <dev/ebus/ebusreg.h>
     48       1.1    jdc #include <dev/ebus/ebusvar.h>
     49       1.1    jdc 
     50       1.1    jdc struct pckbc_ebus_softc {
     51       1.1    jdc 	struct pckbc_softc psc_pckbc;	/* real "pckbc" softc */
     52       1.1    jdc 	uint32_t psc_intr[PCKBC_NSLOTS];
     53       1.1    jdc };
     54       1.1    jdc 
     55       1.1    jdc static int	pckbc_ebus_match(device_t, cfdata_t, void *);
     56       1.1    jdc static void	pckbc_ebus_attach(device_t, device_t, void *);
     57       1.1    jdc 
     58       1.1    jdc static void	pckbc_ebus_intr_establish(struct pckbc_softc *, pckbport_slot_t);
     59       1.1    jdc 
     60       1.1    jdc #define PCKBC_PROM_DEVICE_NAME "8042"
     61  1.1.18.1  skrll #define PCKBC_PROM_DEVICE_NAME2 "kb_ps2"
     62       1.1    jdc 
     63       1.1    jdc CFATTACH_DECL_NEW(pckbc_ebus, sizeof(struct pckbc_ebus_softc),
     64       1.1    jdc     pckbc_ebus_match, pckbc_ebus_attach, NULL, NULL);
     65       1.1    jdc 
     66       1.1    jdc 
     67       1.1    jdc static int
     68       1.1    jdc pckbc_ebus_match(device_t parent, cfdata_t cf, void *aux)
     69       1.1    jdc {
     70       1.1    jdc 	struct ebus_attach_args *ea = aux;
     71       1.1    jdc 
     72  1.1.18.1  skrll 	if (strcmp(ea->ea_name, PCKBC_PROM_DEVICE_NAME) == 0 ||
     73  1.1.18.1  skrll 	    strcmp(ea->ea_name, PCKBC_PROM_DEVICE_NAME2) == 0)
     74  1.1.18.1  skrll 		return 1;
     75  1.1.18.1  skrll 	return 0;
     76       1.1    jdc }
     77       1.1    jdc 
     78       1.1    jdc static void
     79       1.1    jdc pckbc_ebus_attach(device_t parent, device_t self, void *aux)
     80       1.1    jdc {
     81       1.1    jdc 	struct pckbc_ebus_softc *sc = device_private(self);
     82       1.1    jdc 	struct pckbc_softc *psc = (struct pckbc_softc *) sc;
     83       1.1    jdc 	struct ebus_attach_args *ea = aux;
     84       1.1    jdc 	struct pckbc_internal *t;
     85       1.1    jdc 	bus_space_tag_t iot;
     86       1.1    jdc 	bus_addr_t ioaddr;
     87       1.1    jdc 	int stdin_node, node;
     88       1.1    jdc 	int isconsole, i;
     89       1.1    jdc 
     90       1.1    jdc 	psc->sc_dv = self;
     91       1.1    jdc 	iot = ea->ea_bustag;
     92       1.1    jdc 	ioaddr = EBUS_ADDR_FROM_REG(&ea->ea_reg[0]);
     93       1.1    jdc 
     94       1.1    jdc 	stdin_node = prom_instance_to_package(prom_stdin());
     95       1.1    jdc 	isconsole = 0;
     96       1.1    jdc 	for (node = prom_firstchild(ea->ea_node);
     97       1.1    jdc 	     node != 0; node = prom_nextsibling(node))
     98       1.1    jdc 		if (node == stdin_node) {
     99       1.1    jdc 			isconsole = 1;
    100       1.1    jdc 			break;
    101       1.1    jdc 		}
    102       1.1    jdc 
    103       1.1    jdc 	psc->intr_establish = pckbc_ebus_intr_establish;
    104       1.1    jdc 
    105  1.1.18.1  skrll 	if (ea->ea_nintr < PCKBC_NSLOTS) {
    106  1.1.18.1  skrll 		aprint_error(": no intr %d", ea->ea_nintr);
    107  1.1.18.1  skrll 
    108  1.1.18.1  skrll 		/*
    109  1.1.18.1  skrll 		 * XXX OpenBIOS doesn't provide interrupts for pckbc
    110  1.1.18.1  skrll 		 * currently, so use the interrupt numbers described in
    111  1.1.18.1  skrll 		 * QEMU's hw/sparc64/sun4u.c::isa_irq_handler.
    112  1.1.18.1  skrll 		 */
    113  1.1.18.1  skrll 		if (strcmp(machine_model, "OpenBiosTeam,OpenBIOS") == 0) {
    114  1.1.18.1  skrll 			sc->psc_intr[PCKBC_KBD_SLOT] = 0x29;
    115  1.1.18.1  skrll 			sc->psc_intr[PCKBC_AUX_SLOT] = 0x2a;
    116  1.1.18.1  skrll 		} else {
    117  1.1.18.1  skrll 			aprint_error("\n");
    118  1.1.18.1  skrll 			return;
    119  1.1.18.1  skrll 		}
    120  1.1.18.1  skrll 	} else {
    121  1.1.18.1  skrll 		for (i = 0; i < PCKBC_NSLOTS; i++)
    122  1.1.18.1  skrll 			sc->psc_intr[i] = ea->ea_intr[i];
    123  1.1.18.1  skrll 	}
    124       1.1    jdc 
    125       1.1    jdc 	if (isconsole) {
    126       1.1    jdc 		int status;
    127       1.1    jdc 
    128       1.1    jdc 		status = pckbc_cnattach(iot, ioaddr, KBCMDP, 0,
    129       1.1    jdc 		    PCKBC_NEED_AUXWRITE | PCKBC_CANT_TRANSLATE);
    130       1.1    jdc 		if (status == 0)
    131       1.1    jdc 			aprint_normal(": cnattach ok");
    132       1.1    jdc 		else
    133       1.1    jdc 			aprint_error(": cnattach %d", status);
    134       1.1    jdc 	}
    135       1.1    jdc 
    136       1.1    jdc 	if (pckbc_is_console(iot, ioaddr)) {
    137       1.1    jdc 		t = &pckbc_consdata;
    138       1.1    jdc 		pckbc_console_attached = 1;
    139       1.1    jdc 	} else {
    140       1.1    jdc 		bus_space_handle_t ioh_d, ioh_c;
    141       1.1    jdc 
    142       1.1    jdc 		if (bus_space_map(iot, ioaddr + KBDATAP, 1, 0, &ioh_d) != 0) {
    143       1.1    jdc 			aprint_error(": unable to map data register\n");
    144       1.1    jdc 			return;
    145       1.1    jdc 		}
    146       1.1    jdc 
    147       1.1    jdc 		if (bus_space_map(iot, ioaddr + KBCMDP,  1, 0, &ioh_c) != 0) {
    148       1.1    jdc 			bus_space_unmap(iot, ioh_d, 1);
    149       1.1    jdc 			aprint_error(": unable to map cmd register\n");
    150       1.1    jdc 			return;
    151       1.1    jdc 		}
    152       1.1    jdc 
    153       1.1    jdc 		t = malloc(sizeof(struct pckbc_internal), M_DEVBUF, M_WAITOK);
    154       1.1    jdc 		memset(t, 0, sizeof(struct pckbc_internal));
    155       1.1    jdc 		t->t_iot = iot;
    156       1.1    jdc 		t->t_ioh_d = ioh_d;
    157       1.1    jdc 		t->t_ioh_c = ioh_c;
    158       1.1    jdc 		t->t_addr = ioaddr;
    159       1.1    jdc 		t->t_cmdbyte = KC8_CPU; /* initial command: enable ports */
    160       1.1    jdc 		callout_init(&t->t_cleanup, 0);
    161       1.1    jdc 
    162       1.1    jdc 		(void) pckbc_poll_data1(t, PCKBC_KBD_SLOT); /* flush */
    163       1.1    jdc 
    164       1.1    jdc 		if (pckbc_send_cmd(iot, ioh_c, KBC_SELFTEST) == 0)
    165       1.1    jdc 			aprint_error(": unable to request self test");
    166       1.1    jdc 		else {
    167       1.1    jdc 			int response;
    168       1.1    jdc 
    169       1.1    jdc 			response = pckbc_poll_data1(t, PCKBC_KBD_SLOT);
    170       1.1    jdc 			if (response == 0x55)
    171       1.1    jdc 				aprint_normal(": selftest ok");
    172       1.1    jdc 			else
    173       1.1    jdc 				aprint_error(": selftest failed (0x%02x)",
    174       1.1    jdc 				    response);
    175       1.1    jdc 		}
    176       1.1    jdc 	}
    177       1.1    jdc 
    178       1.1    jdc 	/* crosslink */
    179       1.1    jdc 	t->t_sc = psc;
    180       1.1    jdc 	psc->id = t;
    181       1.1    jdc 
    182       1.1    jdc 	/* finish off the attach */
    183       1.1    jdc 	aprint_normal("\n");
    184       1.1    jdc 	pckbc_attach(psc);
    185       1.1    jdc }
    186       1.1    jdc 
    187       1.1    jdc 
    188       1.1    jdc static void
    189       1.1    jdc pckbc_ebus_intr_establish(struct pckbc_softc *sc, pckbport_slot_t slot)
    190       1.1    jdc {
    191       1.1    jdc 	struct pckbc_ebus_softc *psc = (struct pckbc_ebus_softc *)sc;
    192       1.1    jdc 	void *res;
    193       1.1    jdc 
    194       1.1    jdc 	/* We assume that interrupt order is the same as slot order. */
    195       1.1    jdc 	res = bus_intr_establish(sc->id->t_iot, psc->psc_intr[slot],
    196       1.1    jdc 				 IPL_TTY, pckbcintr, sc);
    197       1.1    jdc 	if (res == NULL)
    198       1.1    jdc 		aprint_error_dev(sc->sc_dv,
    199       1.1    jdc 		    "unable to establish %s slot interrupt\n",
    200       1.1    jdc 		    pckbc_slot_names[slot]);
    201       1.1    jdc 
    202       1.1    jdc 	return;
    203       1.1    jdc }
    204