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