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