Home | History | Annotate | Line # | Download | only in isa
pckbc_isa.c revision 1.18
      1 /* $NetBSD: pckbc_isa.c,v 1.18 2006/04/11 17:14:44 garbled 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  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 #include <sys/cdefs.h>
     29 __KERNEL_RCSID(0, "$NetBSD: pckbc_isa.c,v 1.18 2006/04/11 17:14:44 garbled Exp $");
     30 
     31 #include "opt_pckbc.h"
     32 
     33 #include <sys/param.h>
     34 #include <sys/systm.h>
     35 #include <sys/kernel.h>
     36 #include <sys/proc.h>
     37 #include <sys/device.h>
     38 #include <sys/malloc.h>
     39 #include <sys/errno.h>
     40 #include <sys/queue.h>
     41 #include <sys/lock.h>
     42 
     43 #include <machine/bus.h>
     44 
     45 #include <dev/isa/isareg.h>
     46 #include <dev/isa/isavar.h>
     47 
     48 #include <dev/ic/i8042reg.h>
     49 #include <dev/ic/pckbcvar.h>
     50 
     51 int	pckbc_isa_match(struct device *, struct cfdata *, void *);
     52 void	pckbc_isa_attach(struct device *, struct device *, void *);
     53 
     54 struct pckbc_isa_softc {
     55 	struct pckbc_softc sc_pckbc;
     56 
     57 	isa_chipset_tag_t sc_ic;
     58 	int sc_irq[PCKBC_NSLOTS];
     59 };
     60 
     61 CFATTACH_DECL(pckbc_isa, sizeof(struct pckbc_isa_softc),
     62     pckbc_isa_match, pckbc_isa_attach, NULL, NULL);
     63 
     64 void	pckbc_isa_intr_establish(struct pckbc_softc *, pckbc_slot_t);
     65 
     66 int
     67 pckbc_isa_match(parent, match, aux)
     68 	struct device *parent;
     69 	struct cfdata *match;
     70 	void *aux;
     71 {
     72 	struct isa_attach_args *ia = aux;
     73 	bus_space_tag_t iot = ia->ia_iot;
     74 	bus_space_handle_t ioh_d, ioh_c;
     75 	int res, ok = 1;
     76 
     77 	if (ISA_DIRECT_CONFIG(ia))
     78 		return (0);
     79 
     80 	/* If values are hardwired to something that they can't be, punt. */
     81 	if (ia->ia_nio < 1 ||
     82 	    (ia->ia_io[0].ir_addr != ISA_UNKNOWN_PORT &&
     83 	     ia->ia_io[0].ir_addr != IO_KBD))
     84 		return (0);
     85 
     86 	if (ia->ia_niomem > 0 &&
     87 	    (ia->ia_iomem[0].ir_addr != ISA_UNKNOWN_IOMEM))
     88 		return (0);
     89 
     90 	if (ia->ia_nirq < 1 ||
     91 	    (ia->ia_irq[0].ir_irq != ISA_UNKNOWN_IRQ &&
     92 	     ia->ia_irq[0].ir_irq != 1 /*XXX*/))
     93 		return (0);
     94 
     95 	if (ia->ia_ndrq > 0 &&
     96 	    (ia->ia_drq[0].ir_drq != ISA_UNKNOWN_DRQ))
     97 		return (0);
     98 
     99 	if (pckbc_is_console(iot, IO_KBD) == 0) {
    100 		struct pckbc_internal t;
    101 
    102 		if (bus_space_map(iot, IO_KBD + KBDATAP, 1, 0, &ioh_d))
    103 			return (0);
    104 		if (bus_space_map(iot, IO_KBD + KBCMDP, 1, 0, &ioh_c)) {
    105 			bus_space_unmap(iot, ioh_d, 1);
    106 			return (0);
    107 		}
    108 
    109 		memset(&t, 0, sizeof(t));
    110 		t.t_iot = iot;
    111 		t.t_ioh_d = ioh_d;
    112 		t.t_ioh_c = ioh_c;
    113 
    114 		/* flush KBC */
    115 		(void) pckbc_poll_data1(&t, PCKBC_KBD_SLOT);
    116 
    117 		/* KBC selftest */
    118 		if (pckbc_send_cmd(iot, ioh_c, KBC_SELFTEST) == 0) {
    119 			ok = 0;
    120 			goto out;
    121 		}
    122 		res = pckbc_poll_data1(&t, PCKBC_KBD_SLOT);
    123 #ifndef PCKBCNOTEST
    124 		if (res != 0x55) {
    125 #ifdef PCKBCDEBUG
    126 			printf("kbc selftest: %x\n", res);
    127 #endif
    128 			ok = 0;
    129 		}
    130 #endif /* PCKBCNOTEST */
    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