m38813c.c revision 1.5 1 /* $NetBSD: m38813c.c,v 1.5 2001/09/15 12:47:05 uch 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(struct device *, struct cfdata *, void *);
79 void m38813c_attach(struct device *, struct device *, void *);
80 int m38813c_intr(void *);
81 int m38813c_poll(void *);
82 void m38813c_ifsetup(struct m38813c_chip *);
83 int m38813c_input_establish(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(struct device *parent, struct cfdata *cf, void *aux)
93 {
94
95 return (1);
96 }
97
98 void
99 m38813c_attach(struct device *parent, struct device *self, void *aux)
100 {
101 struct cs_attach_args *ca = aux;
102 struct m38813c_softc *sc = (void*)self;
103 struct hpckbd_attach_args haa;
104
105 sc->sc_tc = ca->ca_tc;
106 sc->sc_chip = &m38813c_chip;
107 sc->sc_chip->scc_cst = ca->ca_csio.cstag;
108
109 if (bus_space_map(sc->sc_chip->scc_cst, ca->ca_csio.csbase,
110 ca->ca_csio.cssize, 0, &sc->sc_chip->scc_csh)) {
111 printf(": can't map i/o space\n");
112 }
113
114 #ifndef USE_POLL
115 #error options USE_POLL requied.
116 #endif
117 if (!(sc->sc_ih = tx39_poll_establish(sc->sc_tc, 1,
118 IPL_TTY, m38813c_intr,
119 sc))) {
120 printf(": can't establish interrupt\n");
121 }
122
123 printf("\n");
124
125 /* setup upper interface */
126 m38813c_ifsetup(sc->sc_chip);
127
128 haa.haa_ic = &sc->sc_chip->scc_if;
129
130 config_found(self, &haa, hpckbd_print);
131 }
132
133 void
134 m38813c_ifsetup(struct m38813c_chip *scc)
135 {
136
137 scc->scc_if.hii_ctx = scc;
138
139 scc->scc_if.hii_establish = m38813c_input_establish;
140 scc->scc_if.hii_poll = m38813c_intr;
141 }
142
143 int
144 m38813c_cnattach(paddr_t addr)
145 {
146 struct m38813c_chip *scc = &m38813c_chip;
147
148 scc->scc_csh = MIPS_PHYS_TO_KSEG1(addr);
149
150 m38813c_ifsetup(scc);
151
152 hpckbd_cnattach(&scc->scc_if);
153
154 return (0);
155 }
156
157 int
158 m38813c_input_establish(void *ic, struct hpckbd_if *kbdif)
159 {
160 struct m38813c_chip *scc = ic;
161
162 /* save lower interface */
163 scc->scc_hpckbd = kbdif;
164
165 scc->scc_enabled = 1;
166
167 return (0);
168 }
169
170 #define KBR_EXTENDED0 0xE0 /* extended key sequence */
171 #define KBR_EXTENDED1 0xE1 /* extended key sequence */
172
173 int
174 m38813c_intr(void *arg)
175 {
176 struct m38813c_softc *sc = arg;
177
178 return (m38813c_poll(sc->sc_chip));
179 }
180
181 int
182 m38813c_poll(void *arg)
183 {
184 struct m38813c_chip *scc = arg;
185 bus_space_tag_t t = scc->scc_cst;
186 bus_space_handle_t h = scc->scc_csh;
187 int datain, type, key;
188
189 if (!scc->scc_enabled) {
190 return (0);
191 }
192
193 datain= bus_space_read_1(t, h, 0);
194
195 hpckbd_input_hook(scc->scc_hpckbd);
196
197 if (datain == KBR_EXTENDED0) {
198 scc->t_extended = 1;
199 return (0);
200 } else if (datain == KBR_EXTENDED1) {
201 scc->t_extended1 = 2;
202 return (0);
203 }
204
205 /* map extended keys to (unused) codes 128-254 */
206 key = (datain & 0x7f) | (scc->t_extended ? 0x80 : 0);
207 scc->t_extended = 0;
208
209 /*
210 * process BREAK key (EXT1 1D 45 EXT1 9D C5):
211 * map to (unused) code 7F
212 */
213 if (scc->t_extended1 == 2 && (datain == 0x1d || datain == 0x9d)) {
214 scc->t_extended1 = 1;
215 return (0);
216 } else if (scc->t_extended1 == 1 &&
217 (datain == 0x45 || datain == 0xc5)) {
218 scc->t_extended1 = 0;
219 key = 0x7f;
220 } else if (scc->t_extended1 > 0) {
221 scc->t_extended1 = 0;
222 }
223
224 if (datain & 0x80) {
225 scc->t_lastchar = 0;
226 type = 0;
227 } else {
228 /* Always ignore typematic keys */
229 if (key == scc->t_lastchar)
230 return 0;
231 scc->t_lastchar = key;
232 type = 1;
233 }
234
235 hpckbd_input(scc->scc_hpckbd, type, key);
236
237 return (0);
238 }
239