xref: /netbsd/sys/arch/hpcmips/dev/m38813c.c (revision 6550d01e)
1 /*	$NetBSD: m38813c.c,v 1.12 2008/04/28 20:23:21 martin 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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Device driver for MITUBISHI M38813 controller
34  */
35 
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: m38813c.c,v 1.12 2008/04/28 20:23:21 martin Exp $");
38 
39 #include "opt_use_poll.h"
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/device.h>
44 
45 #include <machine/bus.h>
46 #include <machine/intr.h>
47 
48 #include <dev/hpc/hpckbdvar.h>
49 
50 #include <hpcmips/tx/tx39var.h>
51 #include <hpcmips/tx/txcsbusvar.h>
52 #include <hpcmips/dev/m38813cvar.h>
53 
54 struct m38813c_chip {
55 	bus_space_tag_t		scc_cst;
56 	bus_space_handle_t	scc_csh;
57 	int			scc_enabled;
58 	int t_lastchar;
59 	int t_extended;
60 	int t_extended1;
61 
62 	struct hpckbd_ic_if	scc_if;
63 	struct hpckbd_if	*scc_hpckbd;
64 };
65 
66 struct m38813c_softc {
67 	struct	device		sc_dev;
68 	struct m38813c_chip	*sc_chip;
69 	tx_chipset_tag_t	sc_tc;
70 	void			*sc_ih;
71 };
72 
73 int	m38813c_match(struct device *, struct cfdata *, void *);
74 void	m38813c_attach(struct device *, struct device *, void *);
75 int	m38813c_intr(void *);
76 int	m38813c_poll(void *);
77 void	m38813c_ifsetup(struct m38813c_chip *);
78 int	m38813c_input_establish(void *, struct hpckbd_if *);
79 
80 struct m38813c_chip m38813c_chip;
81 
82 CFATTACH_DECL(m38813c, sizeof(struct m38813c_softc),
83     m38813c_match, m38813c_attach, NULL, NULL);
84 
85 int
86 m38813c_match(struct device *parent, struct cfdata *cf, void *aux)
87 {
88 
89 	return (1);
90 }
91 
92 void
93 m38813c_attach(struct device *parent, struct device *self, void *aux)
94 {
95 	struct cs_attach_args *ca = aux;
96 	struct m38813c_softc *sc = (void*)self;
97 	struct hpckbd_attach_args haa;
98 
99 	sc->sc_tc = ca->ca_tc;
100 	sc->sc_chip = &m38813c_chip;
101 	sc->sc_chip->scc_cst = ca->ca_csio.cstag;
102 
103 	if (bus_space_map(sc->sc_chip->scc_cst, ca->ca_csio.csbase,
104 	    ca->ca_csio.cssize, 0, &sc->sc_chip->scc_csh)) {
105 		printf(": can't map i/o space\n");
106 	}
107 
108 #ifndef USE_POLL
109 #error options USE_POLL required.
110 #endif
111 	if (!(sc->sc_ih = tx39_poll_establish(sc->sc_tc, 1,
112 	    IPL_TTY, m38813c_intr,
113 	    sc))) {
114 		printf(": can't establish interrupt\n");
115 	}
116 
117 	printf("\n");
118 
119 	/* setup upper interface */
120 	m38813c_ifsetup(sc->sc_chip);
121 
122 	haa.haa_ic = &sc->sc_chip->scc_if;
123 
124 	config_found(self, &haa, hpckbd_print);
125 }
126 
127 void
128 m38813c_ifsetup(struct m38813c_chip *scc)
129 {
130 
131 	scc->scc_if.hii_ctx		= scc;
132 
133 	scc->scc_if.hii_establish	= m38813c_input_establish;
134 	scc->scc_if.hii_poll		= m38813c_intr;
135 }
136 
137 int
138 m38813c_cnattach(paddr_t addr)
139 {
140 	struct m38813c_chip *scc = &m38813c_chip;
141 
142 	scc->scc_csh = MIPS_PHYS_TO_KSEG1(addr);
143 
144 	m38813c_ifsetup(scc);
145 
146 	hpckbd_cnattach(&scc->scc_if);
147 
148 	return (0);
149 }
150 
151 int
152 m38813c_input_establish(void *ic, struct hpckbd_if *kbdif)
153 {
154 	struct m38813c_chip *scc = ic;
155 
156 	/* save lower interface */
157 	scc->scc_hpckbd = kbdif;
158 
159 	scc->scc_enabled = 1;
160 
161 	return (0);
162 }
163 
164 #define	KBR_EXTENDED0	0xE0	/* extended key sequence */
165 #define	KBR_EXTENDED1	0xE1	/* extended key sequence */
166 
167 int
168 m38813c_intr(void *arg)
169 {
170 	struct m38813c_softc *sc = arg;
171 
172 	return (m38813c_poll(sc->sc_chip));
173 }
174 
175 int
176 m38813c_poll(void *arg)
177 {
178 	struct m38813c_chip *scc = arg;
179 	bus_space_tag_t t = scc->scc_cst;
180 	bus_space_handle_t h = scc->scc_csh;
181 	int datain, type, key;
182 
183 	if (!scc->scc_enabled) {
184 		return (0);
185 	}
186 
187 	datain= bus_space_read_1(t, h, 0);
188 
189 	hpckbd_input_hook(scc->scc_hpckbd);
190 
191 	if (datain == KBR_EXTENDED0) {
192 		scc->t_extended = 1;
193 		return (0);
194 	} else if (datain == KBR_EXTENDED1) {
195 		scc->t_extended1 = 2;
196 		return (0);
197 	}
198 
199  	/* map extended keys to (unused) codes 128-254 */
200 	key = (datain & 0x7f) | (scc->t_extended ? 0x80 : 0);
201 	scc->t_extended = 0;
202 
203 	/*
204 	 * process BREAK key (EXT1 1D 45  EXT1 9D C5):
205 	 * map to (unused) code 7F
206 	 */
207 	if (scc->t_extended1 == 2 && (datain == 0x1d || datain == 0x9d)) {
208 		scc->t_extended1 = 1;
209 		return (0);
210 	} else if (scc->t_extended1 == 1 &&
211 	    (datain == 0x45 || datain == 0xc5)) {
212 		scc->t_extended1 = 0;
213 		key = 0x7f;
214 	} else if (scc->t_extended1 > 0) {
215 		scc->t_extended1 = 0;
216 	}
217 
218 	if (datain & 0x80) {
219 		scc->t_lastchar = 0;
220 		type = 0;
221 	} else {
222 		/* Always ignore typematic keys */
223 		if (key == scc->t_lastchar)
224 			return 0;
225 		scc->t_lastchar = key;
226 		type = 1;
227 	}
228 
229 	hpckbd_input(scc->scc_hpckbd, type, key);
230 
231 	return (0);
232 }
233