Home | History | Annotate | Line # | Download | only in isa
pckbc_isa.c revision 1.9
      1 /* $NetBSD: pckbc_isa.c,v 1.9 2002/10/02 02:09:20 thorpej 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/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: pckbc_isa.c,v 1.9 2002/10/02 02:09:20 thorpej Exp $");
     36 
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/kernel.h>
     40 #include <sys/proc.h>
     41 #include <sys/device.h>
     42 #include <sys/malloc.h>
     43 #include <sys/errno.h>
     44 #include <sys/queue.h>
     45 #include <sys/lock.h>
     46 
     47 #include <machine/bus.h>
     48 
     49 #include <dev/isa/isareg.h>
     50 #include <dev/isa/isavar.h>
     51 
     52 #include <dev/ic/i8042reg.h>
     53 #include <dev/ic/pckbcvar.h>
     54 
     55 int	pckbc_isa_match __P((struct device *, struct cfdata *, void *));
     56 void	pckbc_isa_attach __P((struct device *, struct device *, void *));
     57 
     58 struct pckbc_isa_softc {
     59 	struct pckbc_softc sc_pckbc;
     60 
     61 	isa_chipset_tag_t sc_ic;
     62 	int sc_irq[PCKBC_NSLOTS];
     63 };
     64 
     65 CFATTACH_DECL(pckbc_isa, sizeof(struct pckbc_isa_softc),
     66 	pckbc_isa_match, pckbc_isa_attach, NULL, NULL);
     67 
     68 void	pckbc_isa_intr_establish __P((struct pckbc_softc *, pckbc_slot_t));
     69 
     70 int
     71 pckbc_isa_match(parent, match, aux)
     72 	struct device *parent;
     73 	struct cfdata *match;
     74 	void *aux;
     75 {
     76 	struct isa_attach_args *ia = aux;
     77 	bus_space_tag_t iot = ia->ia_iot;
     78 	bus_space_handle_t ioh_d, ioh_c;
     79 	int res, ok = 1;
     80 
     81 	if (ISA_DIRECT_CONFIG(ia))
     82 		return (0);
     83 
     84 	/* If values are hardwired to something that they can't be, punt. */
     85 	if (ia->ia_nio < 1 ||
     86 	    (ia->ia_io[0].ir_addr != ISACF_PORT_DEFAULT &&
     87 	     ia->ia_io[0].ir_addr != IO_KBD))
     88 		return (0);
     89 
     90 	if (ia->ia_niomem > 0 &&
     91 	    (ia->ia_iomem[0].ir_addr != ISACF_IOMEM_DEFAULT))
     92 		return (0);
     93 
     94 	if (ia->ia_nirq < 1 ||
     95 	    (ia->ia_irq[0].ir_irq != ISACF_IRQ_DEFAULT &&
     96 	     ia->ia_irq[0].ir_irq != 1 /*XXX*/))
     97 		return (0);
     98 
     99 	if (ia->ia_ndrq > 0 &&
    100 	    (ia->ia_drq[0].ir_drq != ISACF_DRQ_DEFAULT))
    101 		return (0);
    102 
    103 	if (pckbc_is_console(iot, IO_KBD) == 0) {
    104 		struct pckbc_internal t;
    105 
    106 		if (bus_space_map(iot, IO_KBD + KBDATAP, 1, 0, &ioh_d))
    107 			return (0);
    108 		if (bus_space_map(iot, IO_KBD + KBCMDP, 1, 0, &ioh_c)) {
    109 			bus_space_unmap(iot, ioh_d, 1);
    110 			return (0);
    111 		}
    112 
    113 		memset(&t, 0, sizeof(t));
    114 		t.t_iot = iot;
    115 		t.t_ioh_d = ioh_d;
    116 		t.t_ioh_c = ioh_c;
    117 
    118 		/* flush KBC */
    119 		(void) pckbc_poll_data1(&t, PCKBC_KBD_SLOT, 0);
    120 
    121 		/* KBC selftest */
    122 		if (pckbc_send_cmd(iot, ioh_c, KBC_SELFTEST) == 0) {
    123 			ok = 0;
    124 			goto out;
    125 		}
    126 		res = pckbc_poll_data1(&t, PCKBC_KBD_SLOT, 0);
    127 		if (res != 0x55) {
    128 			printf("kbc selftest: %x\n", res);
    129 			ok = 0;
    130 		}
    131  out:
    132 		bus_space_unmap(iot, ioh_d, 1);
    133 		bus_space_unmap(iot, ioh_c, 1);
    134 	}
    135 
    136 	if (ok) {
    137 		ia->ia_io[0].ir_addr = IO_KBD;
    138 		ia->ia_io[0].ir_size = 5;
    139 		ia->ia_nio = 1;
    140 
    141 		ia->ia_niomem = 0;
    142 		ia->ia_nirq = 0;
    143 		ia->ia_ndrq = 0;
    144 	}
    145 	return (ok);
    146 }
    147 
    148 void
    149 pckbc_isa_attach(parent, self, aux)
    150 	struct device *parent, *self;
    151 	void *aux;
    152 {
    153 	struct pckbc_isa_softc *isc = (void *)self;
    154 	struct pckbc_softc *sc = &isc->sc_pckbc;
    155 	struct isa_attach_args *ia = aux;
    156 	struct pckbc_internal *t;
    157 	bus_space_tag_t iot;
    158 	bus_space_handle_t ioh_d, ioh_c;
    159 
    160 	isc->sc_ic = ia->ia_ic;
    161 	iot = ia->ia_iot;
    162 
    163 	switch (ia->ia_nirq) {
    164 	case 1:
    165 		/* Both channels use the same IRQ. */
    166 		isc->sc_irq[PCKBC_KBD_SLOT] =
    167 		    isc->sc_irq[PCKBC_AUX_SLOT] = ia->ia_irq[0].ir_irq;
    168 		break;
    169 
    170 	case 2:
    171 		/* First IRQ is kbd, second IRQ is aux port. */
    172 		isc->sc_irq[PCKBC_KBD_SLOT] = ia->ia_irq[0].ir_irq;
    173 		isc->sc_irq[PCKBC_AUX_SLOT] = ia->ia_irq[1].ir_irq;
    174 		break;
    175 
    176 	default:
    177 		/* Set up IRQs for "normal" ISA. */
    178 		isc->sc_irq[PCKBC_KBD_SLOT] = 1;
    179 		isc->sc_irq[PCKBC_AUX_SLOT] = 12;
    180 		break;
    181 	}
    182 
    183 	sc->intr_establish = pckbc_isa_intr_establish;
    184 
    185 	if (pckbc_is_console(iot, IO_KBD)) {
    186 		t = &pckbc_consdata;
    187 		ioh_d = t->t_ioh_d;
    188 		ioh_c = t->t_ioh_c;
    189 		pckbc_console_attached = 1;
    190 		/* t->t_cmdbyte was initialized by cnattach */
    191 	} else {
    192 		if (bus_space_map(iot, IO_KBD + KBDATAP, 1, 0, &ioh_d) ||
    193 		    bus_space_map(iot, IO_KBD + KBCMDP, 1, 0, &ioh_c))
    194 			panic("pckbc_attach: couldn't map");
    195 
    196 		t = malloc(sizeof(struct pckbc_internal), M_DEVBUF,
    197 		    M_WAITOK|M_ZERO);
    198 		t->t_iot = iot;
    199 		t->t_ioh_d = ioh_d;
    200 		t->t_ioh_c = ioh_c;
    201 		t->t_addr = IO_KBD;
    202 		t->t_cmdbyte = KC8_CPU; /* Enable ports */
    203 		callout_init(&t->t_cleanup);
    204 	}
    205 
    206 	t->t_sc = sc;
    207 	sc->id = t;
    208 
    209 	printf("\n");
    210 
    211 	/* Finish off the attach. */
    212 	pckbc_attach(sc);
    213 }
    214 
    215 void
    216 pckbc_isa_intr_establish(sc, slot)
    217 	struct pckbc_softc *sc;
    218 	pckbc_slot_t slot;
    219 {
    220 	struct pckbc_isa_softc *isc = (void *) sc;
    221 	void *rv;
    222 
    223 	rv = isa_intr_establish(isc->sc_ic, isc->sc_irq[slot], IST_EDGE,
    224 	    IPL_TTY, pckbcintr, sc);
    225 	if (rv == NULL) {
    226 		printf("%s: unable to establish interrupt for %s slot\n",
    227 		    sc->sc_dv.dv_xname, pckbc_slot_names[slot]);
    228 	} else {
    229 		printf("%s: using irq %d for %s slot\n", sc->sc_dv.dv_xname,
    230 		    isc->sc_irq[slot], pckbc_slot_names[slot]);
    231 	}
    232 }
    233