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