Home | History | Annotate | Line # | Download | only in dev
pckbc_ebus.c revision 1.1
      1  1.1  jdc /*	$NetBSD: pckbc_ebus.c,v 1.1 2012/10/09 20:55:04 jdc 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  jdc __KERNEL_RCSID(0, "$NetBSD: pckbc_ebus.c,v 1.1 2012/10/09 20:55:04 jdc 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  jdc 
     62  1.1  jdc CFATTACH_DECL_NEW(pckbc_ebus, sizeof(struct pckbc_ebus_softc),
     63  1.1  jdc     pckbc_ebus_match, pckbc_ebus_attach, NULL, NULL);
     64  1.1  jdc 
     65  1.1  jdc 
     66  1.1  jdc static int
     67  1.1  jdc pckbc_ebus_match(device_t parent, cfdata_t cf, void *aux)
     68  1.1  jdc {
     69  1.1  jdc 	struct ebus_attach_args *ea = aux;
     70  1.1  jdc 
     71  1.1  jdc 	return (strcmp(ea->ea_name, PCKBC_PROM_DEVICE_NAME) == 0);
     72  1.1  jdc }
     73  1.1  jdc 
     74  1.1  jdc static void
     75  1.1  jdc pckbc_ebus_attach(device_t parent, device_t self, void *aux)
     76  1.1  jdc {
     77  1.1  jdc 	struct pckbc_ebus_softc *sc = device_private(self);
     78  1.1  jdc 	struct pckbc_softc *psc = (struct pckbc_softc *) sc;
     79  1.1  jdc 	struct ebus_attach_args *ea = aux;
     80  1.1  jdc 	struct pckbc_internal *t;
     81  1.1  jdc 	bus_space_tag_t iot;
     82  1.1  jdc 	bus_addr_t ioaddr;
     83  1.1  jdc 	int stdin_node, node;
     84  1.1  jdc 	int isconsole, i;
     85  1.1  jdc 
     86  1.1  jdc 	psc->sc_dv = self;
     87  1.1  jdc 	iot = ea->ea_bustag;
     88  1.1  jdc 	ioaddr = EBUS_ADDR_FROM_REG(&ea->ea_reg[0]);
     89  1.1  jdc 
     90  1.1  jdc 	stdin_node = prom_instance_to_package(prom_stdin());
     91  1.1  jdc 	isconsole = 0;
     92  1.1  jdc 	for (node = prom_firstchild(ea->ea_node);
     93  1.1  jdc 	     node != 0; node = prom_nextsibling(node))
     94  1.1  jdc 		if (node == stdin_node) {
     95  1.1  jdc 			isconsole = 1;
     96  1.1  jdc 			break;
     97  1.1  jdc 		}
     98  1.1  jdc 
     99  1.1  jdc 	psc->intr_establish = pckbc_ebus_intr_establish;
    100  1.1  jdc 
    101  1.1  jdc 	for (i = 0; i < PCKBC_NSLOTS; i++)
    102  1.1  jdc 		sc->psc_intr[i] = ea->ea_intr[i];
    103  1.1  jdc 
    104  1.1  jdc 	if (isconsole) {
    105  1.1  jdc 		int status;
    106  1.1  jdc 
    107  1.1  jdc 		status = pckbc_cnattach(iot, ioaddr, KBCMDP, 0,
    108  1.1  jdc 		    PCKBC_NEED_AUXWRITE | PCKBC_CANT_TRANSLATE);
    109  1.1  jdc 		if (status == 0)
    110  1.1  jdc 			aprint_normal(": cnattach ok");
    111  1.1  jdc 		else
    112  1.1  jdc 			aprint_error(": cnattach %d", status);
    113  1.1  jdc 	}
    114  1.1  jdc 
    115  1.1  jdc 	if (pckbc_is_console(iot, ioaddr)) {
    116  1.1  jdc 		t = &pckbc_consdata;
    117  1.1  jdc 		pckbc_console_attached = 1;
    118  1.1  jdc 	} else {
    119  1.1  jdc 		bus_space_handle_t ioh_d, ioh_c;
    120  1.1  jdc 
    121  1.1  jdc 		if (bus_space_map(iot, ioaddr + KBDATAP, 1, 0, &ioh_d) != 0) {
    122  1.1  jdc 			aprint_error(": unable to map data register\n");
    123  1.1  jdc 			return;
    124  1.1  jdc 		}
    125  1.1  jdc 
    126  1.1  jdc 		if (bus_space_map(iot, ioaddr + KBCMDP,  1, 0, &ioh_c) != 0) {
    127  1.1  jdc 			bus_space_unmap(iot, ioh_d, 1);
    128  1.1  jdc 			aprint_error(": unable to map cmd register\n");
    129  1.1  jdc 			return;
    130  1.1  jdc 		}
    131  1.1  jdc 
    132  1.1  jdc 		t = malloc(sizeof(struct pckbc_internal), M_DEVBUF, M_WAITOK);
    133  1.1  jdc 		memset(t, 0, sizeof(struct pckbc_internal));
    134  1.1  jdc 		t->t_iot = iot;
    135  1.1  jdc 		t->t_ioh_d = ioh_d;
    136  1.1  jdc 		t->t_ioh_c = ioh_c;
    137  1.1  jdc 		t->t_addr = ioaddr;
    138  1.1  jdc 		t->t_cmdbyte = KC8_CPU; /* initial command: enable ports */
    139  1.1  jdc 		callout_init(&t->t_cleanup, 0);
    140  1.1  jdc 
    141  1.1  jdc 		(void) pckbc_poll_data1(t, PCKBC_KBD_SLOT); /* flush */
    142  1.1  jdc 
    143  1.1  jdc 		if (pckbc_send_cmd(iot, ioh_c, KBC_SELFTEST) == 0)
    144  1.1  jdc 			aprint_error(": unable to request self test");
    145  1.1  jdc 		else {
    146  1.1  jdc 			int response;
    147  1.1  jdc 
    148  1.1  jdc 			response = pckbc_poll_data1(t, PCKBC_KBD_SLOT);
    149  1.1  jdc 			if (response == 0x55)
    150  1.1  jdc 				aprint_normal(": selftest ok");
    151  1.1  jdc 			else
    152  1.1  jdc 				aprint_error(": selftest failed (0x%02x)",
    153  1.1  jdc 				    response);
    154  1.1  jdc 		}
    155  1.1  jdc 	}
    156  1.1  jdc 
    157  1.1  jdc 	/* crosslink */
    158  1.1  jdc 	t->t_sc = psc;
    159  1.1  jdc 	psc->id = t;
    160  1.1  jdc 
    161  1.1  jdc 	/* finish off the attach */
    162  1.1  jdc 	aprint_normal("\n");
    163  1.1  jdc 	pckbc_attach(psc);
    164  1.1  jdc }
    165  1.1  jdc 
    166  1.1  jdc 
    167  1.1  jdc static void
    168  1.1  jdc pckbc_ebus_intr_establish(struct pckbc_softc *sc, pckbport_slot_t slot)
    169  1.1  jdc {
    170  1.1  jdc 	struct pckbc_ebus_softc *psc = (struct pckbc_ebus_softc *)sc;
    171  1.1  jdc 	void *res;
    172  1.1  jdc 
    173  1.1  jdc 	/* We assume that interrupt order is the same as slot order. */
    174  1.1  jdc 	res = bus_intr_establish(sc->id->t_iot, psc->psc_intr[slot],
    175  1.1  jdc 				 IPL_TTY, pckbcintr, sc);
    176  1.1  jdc 	if (res == NULL)
    177  1.1  jdc 		aprint_error_dev(sc->sc_dv,
    178  1.1  jdc 		    "unable to establish %s slot interrupt\n",
    179  1.1  jdc 		    pckbc_slot_names[slot]);
    180  1.1  jdc 
    181  1.1  jdc 	return;
    182  1.1  jdc }
    183