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