xref: /netbsd/sys/arch/hpcmips/dev/m38813c.c (revision c4a72b64)
1 /*	$NetBSD: m38813c.c,v 1.8 2002/10/02 05:26:45 thorpej 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_use_poll.h"
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/device.h>
48 
49 #include <machine/bus.h>
50 #include <machine/intr.h>
51 
52 #include <dev/hpc/hpckbdvar.h>
53 
54 #include <hpcmips/tx/tx39var.h>
55 #include <hpcmips/tx/txcsbusvar.h>
56 #include <hpcmips/dev/m38813cvar.h>
57 
58 struct m38813c_chip {
59 	bus_space_tag_t		scc_cst;
60 	bus_space_handle_t	scc_csh;
61 	int			scc_enabled;
62 	int t_lastchar;
63 	int t_extended;
64 	int t_extended1;
65 
66 	struct hpckbd_ic_if	scc_if;
67 	struct hpckbd_if	*scc_hpckbd;
68 };
69 
70 struct m38813c_softc {
71 	struct	device		sc_dev;
72 	struct m38813c_chip	*sc_chip;
73 	tx_chipset_tag_t	sc_tc;
74 	void			*sc_ih;
75 };
76 
77 int	m38813c_match(struct device *, struct cfdata *, void *);
78 void	m38813c_attach(struct device *, struct device *, void *);
79 int	m38813c_intr(void *);
80 int	m38813c_poll(void *);
81 void	m38813c_ifsetup(struct m38813c_chip *);
82 int	m38813c_input_establish(void *, struct hpckbd_if *);
83 
84 struct m38813c_chip m38813c_chip;
85 
86 CFATTACH_DECL(m38813c, sizeof(struct m38813c_softc),
87     m38813c_match, m38813c_attach, NULL, NULL);
88 
89 int
90 m38813c_match(struct device *parent, struct cfdata *cf, void *aux)
91 {
92 
93 	return (1);
94 }
95 
96 void
97 m38813c_attach(struct device *parent, struct device *self, void *aux)
98 {
99 	struct cs_attach_args *ca = aux;
100 	struct m38813c_softc *sc = (void*)self;
101 	struct hpckbd_attach_args haa;
102 
103 	sc->sc_tc = ca->ca_tc;
104 	sc->sc_chip = &m38813c_chip;
105 	sc->sc_chip->scc_cst = ca->ca_csio.cstag;
106 
107 	if (bus_space_map(sc->sc_chip->scc_cst, ca->ca_csio.csbase,
108 	    ca->ca_csio.cssize, 0, &sc->sc_chip->scc_csh)) {
109 		printf(": can't map i/o space\n");
110 	}
111 
112 #ifndef USE_POLL
113 #error options USE_POLL requied.
114 #endif
115 	if (!(sc->sc_ih = tx39_poll_establish(sc->sc_tc, 1,
116 	    IPL_TTY, m38813c_intr,
117 	    sc))) {
118 		printf(": can't establish interrupt\n");
119 	}
120 
121 	printf("\n");
122 
123 	/* setup upper interface */
124 	m38813c_ifsetup(sc->sc_chip);
125 
126 	haa.haa_ic = &sc->sc_chip->scc_if;
127 
128 	config_found(self, &haa, hpckbd_print);
129 }
130 
131 void
132 m38813c_ifsetup(struct m38813c_chip *scc)
133 {
134 
135 	scc->scc_if.hii_ctx		= scc;
136 
137 	scc->scc_if.hii_establish	= m38813c_input_establish;
138 	scc->scc_if.hii_poll		= m38813c_intr;
139 }
140 
141 int
142 m38813c_cnattach(paddr_t addr)
143 {
144 	struct m38813c_chip *scc = &m38813c_chip;
145 
146 	scc->scc_csh = MIPS_PHYS_TO_KSEG1(addr);
147 
148 	m38813c_ifsetup(scc);
149 
150 	hpckbd_cnattach(&scc->scc_if);
151 
152 	return (0);
153 }
154 
155 int
156 m38813c_input_establish(void *ic, struct hpckbd_if *kbdif)
157 {
158 	struct m38813c_chip *scc = ic;
159 
160 	/* save lower interface */
161 	scc->scc_hpckbd = kbdif;
162 
163 	scc->scc_enabled = 1;
164 
165 	return (0);
166 }
167 
168 #define	KBR_EXTENDED0	0xE0	/* extended key sequence */
169 #define	KBR_EXTENDED1	0xE1	/* extended key sequence */
170 
171 int
172 m38813c_intr(void *arg)
173 {
174 	struct m38813c_softc *sc = arg;
175 
176 	return (m38813c_poll(sc->sc_chip));
177 }
178 
179 int
180 m38813c_poll(void *arg)
181 {
182 	struct m38813c_chip *scc = arg;
183 	bus_space_tag_t t = scc->scc_cst;
184 	bus_space_handle_t h = scc->scc_csh;
185 	int datain, type, key;
186 
187 	if (!scc->scc_enabled) {
188 		return (0);
189 	}
190 
191 	datain= bus_space_read_1(t, h, 0);
192 
193 	hpckbd_input_hook(scc->scc_hpckbd);
194 
195 	if (datain == KBR_EXTENDED0) {
196 		scc->t_extended = 1;
197 		return (0);
198 	} else if (datain == KBR_EXTENDED1) {
199 		scc->t_extended1 = 2;
200 		return (0);
201 	}
202 
203  	/* map extended keys to (unused) codes 128-254 */
204 	key = (datain & 0x7f) | (scc->t_extended ? 0x80 : 0);
205 	scc->t_extended = 0;
206 
207 	/*
208 	 * process BREAK key (EXT1 1D 45  EXT1 9D C5):
209 	 * map to (unused) code 7F
210 	 */
211 	if (scc->t_extended1 == 2 && (datain == 0x1d || datain == 0x9d)) {
212 		scc->t_extended1 = 1;
213 		return (0);
214 	} else if (scc->t_extended1 == 1 &&
215 	    (datain == 0x45 || datain == 0xc5)) {
216 		scc->t_extended1 = 0;
217 		key = 0x7f;
218 	} else if (scc->t_extended1 > 0) {
219 		scc->t_extended1 = 0;
220 	}
221 
222 	if (datain & 0x80) {
223 		scc->t_lastchar = 0;
224 		type = 0;
225 	} else {
226 		/* Always ignore typematic keys */
227 		if (key == scc->t_lastchar)
228 			return 0;
229 		scc->t_lastchar = key;
230 		type = 1;
231 	}
232 
233 	hpckbd_input(scc->scc_hpckbd, type, key);
234 
235 	return (0);
236 }
237