Home | History | Annotate | Line # | Download | only in isa
pckbc_isa.c revision 1.4
      1 /* $NetBSD: pckbc_isa.c,v 1.4 2001/07/23 21:03:22 jdolecek Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1998
      5  *	Matthias Drochner.  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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed for the NetBSD Project
     18  *	by Matthias Drochner.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/param.h>
     35 #include <sys/systm.h>
     36 #include <sys/kernel.h>
     37 #include <sys/proc.h>
     38 #include <sys/device.h>
     39 #include <sys/malloc.h>
     40 #include <sys/errno.h>
     41 #include <sys/queue.h>
     42 #include <sys/lock.h>
     43 
     44 #include <machine/bus.h>
     45 
     46 #include <dev/isa/isareg.h>
     47 #include <dev/isa/isavar.h>
     48 
     49 #include <dev/ic/i8042reg.h>
     50 #include <dev/ic/pckbcvar.h>
     51 
     52 int	pckbc_isa_match __P((struct device *, struct cfdata *, void *));
     53 void	pckbc_isa_attach __P((struct device *, struct device *, void *));
     54 
     55 struct pckbc_isa_softc {
     56 	struct pckbc_softc sc_pckbc;
     57 
     58 	isa_chipset_tag_t sc_ic;
     59 	int sc_irq[PCKBC_NSLOTS];
     60 };
     61 
     62 struct cfattach pckbc_isa_ca = {
     63 	sizeof(struct pckbc_isa_softc), pckbc_isa_match, pckbc_isa_attach,
     64 };
     65 
     66 void	pckbc_isa_intr_establish __P((struct pckbc_softc *, pckbc_slot_t));
     67 
     68 int
     69 pckbc_isa_match(parent, match, aux)
     70 	struct device *parent;
     71 	struct cfdata *match;
     72 	void *aux;
     73 {
     74 	struct isa_attach_args *ia = aux;
     75 	bus_space_tag_t iot = ia->ia_iot;
     76 	bus_space_handle_t ioh_d, ioh_c;
     77 	int res, ok = 1;
     78 
     79 	/* If values are hardwired to something that they can't be, punt. */
     80 	if ((ia->ia_iobase != IOBASEUNK && ia->ia_iobase != IO_KBD) ||
     81 	    ia->ia_maddr != MADDRUNK ||
     82 	    (ia->ia_irq != IRQUNK && ia->ia_irq != 1 /* XXX */) ||
     83 	    ia->ia_drq != DRQUNK)
     84 		return (0);
     85 
     86 	if (pckbc_is_console(iot, IO_KBD) == 0) {
     87 		struct pckbc_internal t;
     88 
     89 		if (bus_space_map(iot, IO_KBD + KBDATAP, 1, 0, &ioh_d))
     90 			return (0);
     91 		if (bus_space_map(iot, IO_KBD + KBCMDP, 1, 0, &ioh_c)) {
     92 			bus_space_unmap(iot, ioh_d, 1);
     93 			return (0);
     94 		}
     95 
     96 		memset(&t, 0, sizeof(t));
     97 		t.t_iot = iot;
     98 		t.t_ioh_d = ioh_d;
     99 		t.t_ioh_c = ioh_c;
    100 
    101 		/* flush KBC */
    102 		(void) pckbc_poll_data1(&t, PCKBC_KBD_SLOT, 0);
    103 
    104 		/* KBC selftest */
    105 		if (pckbc_send_cmd(iot, ioh_c, KBC_SELFTEST) == 0) {
    106 			ok = 0;
    107 			goto out;
    108 		}
    109 		res = pckbc_poll_data1(&t, PCKBC_KBD_SLOT, 0);
    110 		if (res != 0x55) {
    111 			printf("kbc selftest: %x\n", res);
    112 			ok = 0;
    113 		}
    114  out:
    115 		bus_space_unmap(iot, ioh_d, 1);
    116 		bus_space_unmap(iot, ioh_c, 1);
    117 	}
    118 
    119 	if (ok) {
    120 		ia->ia_iobase = IO_KBD;
    121 		ia->ia_iosize = 5;
    122 		ia->ia_msize = 0x0;
    123 	}
    124 	return (ok);
    125 }
    126 
    127 void
    128 pckbc_isa_attach(parent, self, aux)
    129 	struct device *parent, *self;
    130 	void *aux;
    131 {
    132 	struct pckbc_isa_softc *isc = (void *)self;
    133 	struct pckbc_softc *sc = &isc->sc_pckbc;
    134 	struct isa_attach_args *ia = aux;
    135 	struct pckbc_internal *t;
    136 	bus_space_tag_t iot;
    137 	bus_space_handle_t ioh_d, ioh_c;
    138 
    139 	isc->sc_ic = ia->ia_ic;
    140 	iot = ia->ia_iot;
    141 
    142 	/*
    143 	 * Set up IRQs for "normal" ISA.
    144 	 *
    145 	 * XXX The "aux" slot is different (9) on the Alpha AXP150 Jensen.
    146 	 */
    147 	isc->sc_irq[PCKBC_KBD_SLOT] = 1;
    148 	isc->sc_irq[PCKBC_AUX_SLOT] = 12;
    149 
    150 	sc->intr_establish = pckbc_isa_intr_establish;
    151 
    152 	if (pckbc_is_console(iot, IO_KBD)) {
    153 		t = &pckbc_consdata;
    154 		ioh_d = t->t_ioh_d;
    155 		ioh_c = t->t_ioh_c;
    156 		pckbc_console_attached = 1;
    157 		/* t->t_cmdbyte was initialized by cnattach */
    158 	} else {
    159 		if (bus_space_map(iot, IO_KBD + KBDATAP, 1, 0, &ioh_d) ||
    160 		    bus_space_map(iot, IO_KBD + KBCMDP, 1, 0, &ioh_c))
    161 			panic("pckbc_attach: couldn't map");
    162 
    163 		t = malloc(sizeof(struct pckbc_internal), M_DEVBUF, M_WAITOK);
    164 		memset(t, 0, sizeof(struct pckbc_internal));
    165 		t->t_iot = iot;
    166 		t->t_ioh_d = ioh_d;
    167 		t->t_ioh_c = ioh_c;
    168 		t->t_addr = IO_KBD;
    169 		t->t_cmdbyte = KC8_CPU; /* Enable ports */
    170 		callout_init(&t->t_cleanup);
    171 	}
    172 
    173 	t->t_sc = sc;
    174 	sc->id = t;
    175 
    176 	printf("\n");
    177 
    178 	/* Finish off the attach. */
    179 	pckbc_attach(sc);
    180 }
    181 
    182 void
    183 pckbc_isa_intr_establish(sc, slot)
    184 	struct pckbc_softc *sc;
    185 	pckbc_slot_t slot;
    186 {
    187 	struct pckbc_isa_softc *isc = (void *) sc;
    188 	void *rv;
    189 
    190 	rv = isa_intr_establish(isc->sc_ic, isc->sc_irq[slot], IST_EDGE,
    191 	    IPL_TTY, pckbcintr, sc);
    192 	if (rv == NULL) {
    193 		printf("%s: unable to establish interrupt for %s slot\n",
    194 		    sc->sc_dv.dv_xname, pckbc_slot_names[slot]);
    195 	} else {
    196 		printf("%s: using irq %d for %s slot\n", sc->sc_dv.dv_xname,
    197 		    isc->sc_irq[slot], pckbc_slot_names[slot]);
    198 	}
    199 }
    200