1 1.4 thorpej /* $NetBSD: pckbc_ebus.c,v 1.4 2021/01/04 14:48:51 thorpej 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.4 thorpej __KERNEL_RCSID(0, "$NetBSD: pckbc_ebus.c,v 1.4 2021/01/04 14:48:51 thorpej 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.4 thorpej #include <sys/kmem.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.3 macallan #include "opt_tadpmu.h" 51 1.3 macallan #include <sparc64/dev/tadpmureg.h> 52 1.3 macallan #include <sparc64/dev/tadpmuvar.h> 53 1.3 macallan 54 1.1 jdc struct pckbc_ebus_softc { 55 1.1 jdc struct pckbc_softc psc_pckbc; /* real "pckbc" softc */ 56 1.3 macallan uint32_t psc_intr[5]; /* Tadpole Viper's pckbc has 3 slots */ 57 1.1 jdc }; 58 1.1 jdc 59 1.1 jdc static int pckbc_ebus_match(device_t, cfdata_t, void *); 60 1.1 jdc static void pckbc_ebus_attach(device_t, device_t, void *); 61 1.1 jdc 62 1.1 jdc static void pckbc_ebus_intr_establish(struct pckbc_softc *, pckbport_slot_t); 63 1.1 jdc 64 1.1 jdc #define PCKBC_PROM_DEVICE_NAME "8042" 65 1.2 nakayama #define PCKBC_PROM_DEVICE_NAME2 "kb_ps2" 66 1.1 jdc 67 1.1 jdc CFATTACH_DECL_NEW(pckbc_ebus, sizeof(struct pckbc_ebus_softc), 68 1.1 jdc pckbc_ebus_match, pckbc_ebus_attach, NULL, NULL); 69 1.1 jdc 70 1.1 jdc 71 1.1 jdc static int 72 1.1 jdc pckbc_ebus_match(device_t parent, cfdata_t cf, void *aux) 73 1.1 jdc { 74 1.1 jdc struct ebus_attach_args *ea = aux; 75 1.1 jdc 76 1.2 nakayama if (strcmp(ea->ea_name, PCKBC_PROM_DEVICE_NAME) == 0 || 77 1.2 nakayama strcmp(ea->ea_name, PCKBC_PROM_DEVICE_NAME2) == 0) 78 1.2 nakayama return 1; 79 1.2 nakayama return 0; 80 1.1 jdc } 81 1.1 jdc 82 1.1 jdc static void 83 1.1 jdc pckbc_ebus_attach(device_t parent, device_t self, void *aux) 84 1.1 jdc { 85 1.1 jdc struct pckbc_ebus_softc *sc = device_private(self); 86 1.1 jdc struct pckbc_softc *psc = (struct pckbc_softc *) sc; 87 1.1 jdc struct ebus_attach_args *ea = aux; 88 1.1 jdc struct pckbc_internal *t; 89 1.1 jdc bus_space_tag_t iot; 90 1.1 jdc bus_addr_t ioaddr; 91 1.1 jdc int stdin_node, node; 92 1.1 jdc int isconsole, i; 93 1.1 jdc 94 1.1 jdc psc->sc_dv = self; 95 1.1 jdc iot = ea->ea_bustag; 96 1.1 jdc ioaddr = EBUS_ADDR_FROM_REG(&ea->ea_reg[0]); 97 1.1 jdc 98 1.1 jdc stdin_node = prom_instance_to_package(prom_stdin()); 99 1.1 jdc isconsole = 0; 100 1.1 jdc for (node = prom_firstchild(ea->ea_node); 101 1.1 jdc node != 0; node = prom_nextsibling(node)) 102 1.1 jdc if (node == stdin_node) { 103 1.1 jdc isconsole = 1; 104 1.1 jdc break; 105 1.1 jdc } 106 1.1 jdc 107 1.1 jdc psc->intr_establish = pckbc_ebus_intr_establish; 108 1.1 jdc 109 1.2 nakayama if (ea->ea_nintr < PCKBC_NSLOTS) { 110 1.2 nakayama aprint_error(": no intr %d", ea->ea_nintr); 111 1.2 nakayama 112 1.2 nakayama /* 113 1.2 nakayama * XXX OpenBIOS doesn't provide interrupts for pckbc 114 1.2 nakayama * currently, so use the interrupt numbers described in 115 1.2 nakayama * QEMU's hw/sparc64/sun4u.c::isa_irq_handler. 116 1.2 nakayama */ 117 1.2 nakayama if (strcmp(machine_model, "OpenBiosTeam,OpenBIOS") == 0) { 118 1.2 nakayama sc->psc_intr[PCKBC_KBD_SLOT] = 0x29; 119 1.2 nakayama sc->psc_intr[PCKBC_AUX_SLOT] = 0x2a; 120 1.2 nakayama } else { 121 1.2 nakayama aprint_error("\n"); 122 1.2 nakayama return; 123 1.2 nakayama } 124 1.2 nakayama } else { 125 1.3 macallan for (i = 0; i < ea->ea_nintr; i++) 126 1.2 nakayama sc->psc_intr[i] = ea->ea_intr[i]; 127 1.2 nakayama } 128 1.1 jdc 129 1.1 jdc if (isconsole) { 130 1.1 jdc int status; 131 1.1 jdc 132 1.1 jdc status = pckbc_cnattach(iot, ioaddr, KBCMDP, 0, 133 1.1 jdc PCKBC_NEED_AUXWRITE | PCKBC_CANT_TRANSLATE); 134 1.1 jdc if (status == 0) 135 1.1 jdc aprint_normal(": cnattach ok"); 136 1.1 jdc else 137 1.1 jdc aprint_error(": cnattach %d", status); 138 1.1 jdc } 139 1.1 jdc 140 1.1 jdc if (pckbc_is_console(iot, ioaddr)) { 141 1.1 jdc t = &pckbc_consdata; 142 1.1 jdc pckbc_console_attached = 1; 143 1.1 jdc } else { 144 1.1 jdc bus_space_handle_t ioh_d, ioh_c; 145 1.1 jdc 146 1.1 jdc if (bus_space_map(iot, ioaddr + KBDATAP, 1, 0, &ioh_d) != 0) { 147 1.1 jdc aprint_error(": unable to map data register\n"); 148 1.1 jdc return; 149 1.1 jdc } 150 1.1 jdc 151 1.1 jdc if (bus_space_map(iot, ioaddr + KBCMDP, 1, 0, &ioh_c) != 0) { 152 1.1 jdc bus_space_unmap(iot, ioh_d, 1); 153 1.1 jdc aprint_error(": unable to map cmd register\n"); 154 1.1 jdc return; 155 1.1 jdc } 156 1.1 jdc 157 1.4 thorpej t = kmem_zalloc(sizeof(struct pckbc_internal), KM_SLEEP); 158 1.1 jdc t->t_iot = iot; 159 1.1 jdc t->t_ioh_d = ioh_d; 160 1.1 jdc t->t_ioh_c = ioh_c; 161 1.1 jdc t->t_addr = ioaddr; 162 1.1 jdc t->t_cmdbyte = KC8_CPU; /* initial command: enable ports */ 163 1.1 jdc callout_init(&t->t_cleanup, 0); 164 1.1 jdc 165 1.1 jdc (void) pckbc_poll_data1(t, PCKBC_KBD_SLOT); /* flush */ 166 1.1 jdc 167 1.1 jdc if (pckbc_send_cmd(iot, ioh_c, KBC_SELFTEST) == 0) 168 1.1 jdc aprint_error(": unable to request self test"); 169 1.1 jdc else { 170 1.1 jdc int response; 171 1.1 jdc 172 1.1 jdc response = pckbc_poll_data1(t, PCKBC_KBD_SLOT); 173 1.1 jdc if (response == 0x55) 174 1.1 jdc aprint_normal(": selftest ok"); 175 1.1 jdc else 176 1.1 jdc aprint_error(": selftest failed (0x%02x)", 177 1.1 jdc response); 178 1.1 jdc } 179 1.1 jdc } 180 1.1 jdc 181 1.1 jdc /* crosslink */ 182 1.1 jdc t->t_sc = psc; 183 1.1 jdc psc->id = t; 184 1.1 jdc 185 1.1 jdc /* finish off the attach */ 186 1.1 jdc aprint_normal("\n"); 187 1.1 jdc pckbc_attach(psc); 188 1.3 macallan #ifdef HAVE_TADPMU 189 1.3 macallan /* now look for a tadpmu child device */ 190 1.3 macallan char name[64], *p; 191 1.3 macallan int pmu = 0; 192 1.3 macallan for (node = prom_firstchild(ea->ea_node); 193 1.3 macallan node != 0; node = prom_nextsibling(node)) { 194 1.3 macallan if((p = prom_getpropstringA(node, "name", name, 64)) != NULL) { 195 1.3 macallan if (strcmp(name, "tadpmu") == 0) { 196 1.3 macallan pmu = node; 197 1.3 macallan break; 198 1.3 macallan } 199 1.3 macallan } 200 1.3 macallan } 201 1.3 macallan if (pmu != 0) { 202 1.3 macallan void *irq; 203 1.3 macallan 204 1.3 macallan bus_space_handle_t hcmd, hdata; 205 1.3 macallan if (bus_space_map(iot, ioaddr + TADPMU_CMD, 1, 0, &hcmd) != 0) { 206 1.3 macallan bus_space_unmap(iot, hcmd, 1); 207 1.3 macallan aprint_error(": unable to map PMU cmd register\n"); 208 1.3 macallan return; 209 1.3 macallan } 210 1.3 macallan if (bus_space_map(iot, ioaddr + TADPMU_DATA, 1, 0, &hdata) != 0) { 211 1.3 macallan bus_space_unmap(iot, hdata, 1); 212 1.3 macallan aprint_error(": unable to map PMU data register\n"); 213 1.3 macallan return; 214 1.3 macallan } 215 1.3 macallan tadpmu_init(iot, hcmd, hdata); 216 1.3 macallan irq = bus_intr_establish(iot, sc->psc_intr[2], IPL_TTY, 217 1.3 macallan tadpmu_intr, sc); 218 1.3 macallan if (irq == NULL) { 219 1.3 macallan aprint_error("failed to establish tadpmu interrupt\n"); 220 1.3 macallan } 221 1.3 macallan } 222 1.3 macallan #endif 223 1.1 jdc } 224 1.1 jdc 225 1.1 jdc static void 226 1.1 jdc pckbc_ebus_intr_establish(struct pckbc_softc *sc, pckbport_slot_t slot) 227 1.1 jdc { 228 1.1 jdc struct pckbc_ebus_softc *psc = (struct pckbc_ebus_softc *)sc; 229 1.1 jdc void *res; 230 1.1 jdc 231 1.1 jdc /* We assume that interrupt order is the same as slot order. */ 232 1.1 jdc res = bus_intr_establish(sc->id->t_iot, psc->psc_intr[slot], 233 1.1 jdc IPL_TTY, pckbcintr, sc); 234 1.1 jdc if (res == NULL) 235 1.1 jdc aprint_error_dev(sc->sc_dv, 236 1.1 jdc "unable to establish %s slot interrupt\n", 237 1.1 jdc pckbc_slot_names[slot]); 238 1.1 jdc 239 1.1 jdc return; 240 1.1 jdc } 241