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