1 /* $NetBSD: m38813c.c,v 1.16 2021/09/07 06:36:06 rin Exp $ */ 2 3 /*- 4 * Copyright (c) 1999, 2001 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by UCHIYAMA Yasushi. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Device driver for MITUBISHI M38813 controller 34 */ 35 36 #include <sys/cdefs.h> 37 __KERNEL_RCSID(0, "$NetBSD: m38813c.c,v 1.16 2021/09/07 06:36:06 rin Exp $"); 38 39 #include "opt_use_poll.h" 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/device.h> 44 45 #include <mips/cpuregs.h> 46 47 #include <machine/bus.h> 48 #include <machine/intr.h> 49 50 #include <dev/hpc/hpckbdvar.h> 51 52 #include <hpcmips/tx/tx39var.h> 53 #include <hpcmips/tx/txcsbusvar.h> 54 #include <hpcmips/dev/m38813cvar.h> 55 56 struct m38813c_chip { 57 bus_space_tag_t scc_cst; 58 bus_space_handle_t scc_csh; 59 int scc_enabled; 60 int t_lastchar; 61 int t_extended; 62 int t_extended1; 63 64 struct hpckbd_ic_if scc_if; 65 struct hpckbd_if *scc_hpckbd; 66 }; 67 68 struct m38813c_softc { 69 struct m38813c_chip *sc_chip; 70 tx_chipset_tag_t sc_tc; 71 void *sc_ih; 72 }; 73 74 int m38813c_match(device_t, cfdata_t, void *); 75 void m38813c_attach(device_t, device_t, void *); 76 int m38813c_intr(void *); 77 int m38813c_poll(void *); 78 void m38813c_ifsetup(struct m38813c_chip *); 79 int m38813c_input_establish(void *, struct hpckbd_if *); 80 81 struct m38813c_chip m38813c_chip; 82 83 CFATTACH_DECL_NEW(m38813c, sizeof(struct m38813c_softc), 84 m38813c_match, m38813c_attach, NULL, NULL); 85 86 int 87 m38813c_match(device_t parent, cfdata_t cf, void *aux) 88 { 89 90 return (1); 91 } 92 93 void 94 m38813c_attach(device_t parent, device_t self, void *aux) 95 { 96 struct cs_attach_args *ca = aux; 97 struct m38813c_softc *sc = device_private(self); 98 struct hpckbd_attach_args haa; 99 100 sc->sc_tc = ca->ca_tc; 101 sc->sc_chip = &m38813c_chip; 102 sc->sc_chip->scc_cst = ca->ca_csio.cstag; 103 104 if (bus_space_map(sc->sc_chip->scc_cst, ca->ca_csio.csbase, 105 ca->ca_csio.cssize, 0, &sc->sc_chip->scc_csh)) { 106 printf(": can't map i/o space\n"); 107 } 108 109 #ifndef USE_POLL 110 #error options USE_POLL required. 111 #endif 112 if (!(sc->sc_ih = tx39_poll_establish(sc->sc_tc, 1, 113 IPL_TTY, m38813c_intr, 114 sc))) { 115 printf(": can't establish interrupt\n"); 116 } 117 118 printf("\n"); 119 120 /* setup upper interface */ 121 m38813c_ifsetup(sc->sc_chip); 122 123 haa.haa_ic = &sc->sc_chip->scc_if; 124 125 config_found(self, &haa, hpckbd_print, CFARGS_NONE); 126 } 127 128 void 129 m38813c_ifsetup(struct m38813c_chip *scc) 130 { 131 132 scc->scc_if.hii_ctx = scc; 133 134 scc->scc_if.hii_establish = m38813c_input_establish; 135 scc->scc_if.hii_poll = m38813c_intr; 136 } 137 138 int 139 m38813c_cnattach(paddr_t addr) 140 { 141 struct m38813c_chip *scc = &m38813c_chip; 142 143 scc->scc_csh = MIPS_PHYS_TO_KSEG1(addr); 144 145 m38813c_ifsetup(scc); 146 147 hpckbd_cnattach(&scc->scc_if); 148 149 return (0); 150 } 151 152 int 153 m38813c_input_establish(void *ic, struct hpckbd_if *kbdif) 154 { 155 struct m38813c_chip *scc = ic; 156 157 /* save lower interface */ 158 scc->scc_hpckbd = kbdif; 159 160 scc->scc_enabled = 1; 161 162 return (0); 163 } 164 165 #define KBR_EXTENDED0 0xE0 /* extended key sequence */ 166 #define KBR_EXTENDED1 0xE1 /* extended key sequence */ 167 168 int 169 m38813c_intr(void *arg) 170 { 171 struct m38813c_softc *sc = arg; 172 173 return (m38813c_poll(sc->sc_chip)); 174 } 175 176 int 177 m38813c_poll(void *arg) 178 { 179 struct m38813c_chip *scc = arg; 180 bus_space_tag_t t = scc->scc_cst; 181 bus_space_handle_t h = scc->scc_csh; 182 int datain, type, key; 183 184 if (!scc->scc_enabled) { 185 return (0); 186 } 187 188 datain= bus_space_read_1(t, h, 0); 189 190 hpckbd_input_hook(scc->scc_hpckbd); 191 192 if (datain == KBR_EXTENDED0) { 193 scc->t_extended = 1; 194 return (0); 195 } else if (datain == KBR_EXTENDED1) { 196 scc->t_extended1 = 2; 197 return (0); 198 } 199 200 /* map extended keys to (unused) codes 128-254 */ 201 key = (datain & 0x7f) | (scc->t_extended ? 0x80 : 0); 202 scc->t_extended = 0; 203 204 /* 205 * process BREAK key (EXT1 1D 45 EXT1 9D C5): 206 * map to (unused) code 7F 207 */ 208 if (scc->t_extended1 == 2 && (datain == 0x1d || datain == 0x9d)) { 209 scc->t_extended1 = 1; 210 return (0); 211 } else if (scc->t_extended1 == 1 && 212 (datain == 0x45 || datain == 0xc5)) { 213 scc->t_extended1 = 0; 214 key = 0x7f; 215 } else if (scc->t_extended1 > 0) { 216 scc->t_extended1 = 0; 217 } 218 219 if (datain & 0x80) { 220 scc->t_lastchar = 0; 221 type = 0; 222 } else { 223 /* Always ignore typematic keys */ 224 if (key == scc->t_lastchar) 225 return 0; 226 scc->t_lastchar = key; 227 type = 1; 228 } 229 230 hpckbd_input(scc->scc_hpckbd, type, key); 231 232 return (0); 233 } 234