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