xref: /netbsd-src/sys/arch/hpcarm/dev/j720kbd.c (revision c71562d660be5e4ad22016bce45e96f08af190cc)
1 /*	$NetBSD: j720kbd.c,v 1.1 2006/03/04 14:09:36 peter Exp $	*/
2 
3 /*-
4  * Copyright (c) 2006 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by IWAMOTO Toshihiro and Peter Postma.
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 /* Jornada 720 keyboard driver. */
40 
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: j720kbd.c,v 1.1 2006/03/04 14:09:36 peter Exp $");
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/device.h>
47 #include <sys/kernel.h>
48 
49 #include <machine/bootinfo.h>
50 #include <machine/config_hook.h>
51 #include <machine/platid.h>
52 #include <machine/platid_mask.h>
53 
54 #include <arm/sa11x0/sa11x0_var.h>
55 #include <arm/sa11x0/sa11x0_gpioreg.h>
56 #include <arm/sa11x0/sa11x0_ppcreg.h>
57 #include <arm/sa11x0/sa11x0_sspreg.h>
58 
59 #include <dev/hpc/hpckbdvar.h>
60 
61 #include <hpcarm/dev/j720sspvar.h>
62 #include <hpcarm/dev/sed1356var.h>
63 
64 #ifdef DEBUG
65 #define DPRINTF(arg)	printf arg
66 #else
67 #define DPRINTF(arg)	/* nothing */
68 #endif
69 
70 struct j720kbd_chip {
71 	int			scc_enabled;
72 
73 	struct j720ssp_softc	*scc_ssp;
74 
75 	struct hpckbd_ic_if	scc_if;
76 	struct hpckbd_if	*scc_hpckbd;
77 };
78 
79 struct j720kbd_softc {
80 	struct device		sc_dev;
81 
82 	struct j720kbd_chip	*sc_chip;
83 	void			*sc_ih;
84 };
85 
86 static struct j720kbd_chip j720kbd_chip;
87 
88 static int	j720kbd_match(struct device *, struct cfdata *, void *);
89 static void	j720kbd_attach(struct device *, struct device *, void *);
90 
91 static void	j720kbd_cnattach(void);
92 static void	j720kbd_ifsetup(struct j720kbd_chip *);
93 static int	j720kbd_input_establish(void *, struct hpckbd_if *);
94 static int	j720kbd_intr(void *);
95 static int	j720kbd_poll(void *);
96 static void	j720kbd_read(struct j720kbd_chip *, char *);
97 static int	j720kbd_button_event(void *, int, long, void *);
98 
99 CFATTACH_DECL(j720kbd, sizeof(struct device),
100     j720kbd_match, j720kbd_attach, NULL, NULL);
101 
102 
103 static int
104 j720kbd_match(struct device *parent, struct cfdata *cf, void *aux)
105 {
106 
107 	if (!platid_match(&platid, &platid_mask_MACH_HP_JORNADA_7XX))
108 		return 0;
109 	if (strcmp(cf->cf_name, "j720kbd") != 0)
110 		return 0;
111 
112 	return 1;
113 }
114 
115 static void
116 j720kbd_attach(struct device *parent, struct device *self, void *aux)
117 {
118 	struct j720kbd_softc *sc = (void *)self;
119 	struct hpckbd_attach_args haa;
120 
121 	sc->sc_chip = &j720kbd_chip;
122 	sc->sc_chip->scc_ssp = (struct j720ssp_softc *)parent;
123 	sc->sc_chip->scc_enabled = 0;
124 
125 	/* Attach console if not using serial. */
126 	if (!(bootinfo->bi_cnuse & BI_CNUSE_SERIAL))
127 		j720kbd_cnattach();
128 
129 	j720kbd_ifsetup(sc->sc_chip);
130 
131 	/* Install interrupt handler. */
132 	if ((sc->sc_ih = sa11x0_intr_establish(0, 0, 1, IPL_TTY,
133 	    j720kbd_intr, sc)) == NULL) {
134 		printf(": can't establish interrupt\n");
135 		return;
136 	}
137 	printf("\n");
138 
139 	/* Add config hook for light button event. */
140 	config_hook(CONFIG_HOOK_BUTTONEVENT,
141 		    CONFIG_HOOK_BUTTONEVENT_LIGHT,
142 		    CONFIG_HOOK_SHARE, j720kbd_button_event, sc);
143 
144 	/* Attach hpckbd. */
145 	haa.haa_ic = &sc->sc_chip->scc_if;
146 	config_found(self, &haa, hpckbd_print);
147 }
148 
149 static void
150 j720kbd_cnattach(void)
151 {
152 	struct j720kbd_chip *scc = &j720kbd_chip;
153 
154 	/* Initialize interface. */
155 	j720kbd_ifsetup(scc);
156 
157 	/* Attach console. */
158 	hpckbd_cnattach(&scc->scc_if);
159 }
160 
161 static void
162 j720kbd_ifsetup(struct j720kbd_chip *scc)
163 {
164 
165 	scc->scc_if.hii_ctx = scc;
166 	scc->scc_if.hii_establish = j720kbd_input_establish;
167 	scc->scc_if.hii_poll = j720kbd_poll;
168 }
169 
170 static int
171 j720kbd_input_establish(void *ic, struct hpckbd_if *kbdif)
172 {
173 	struct j720kbd_chip *scc = ic;
174 
175 	/* Save hpckbd interface. */
176 	scc->scc_hpckbd = kbdif;
177 
178 	scc->scc_enabled = 1;
179 
180 	return 0;
181 }
182 
183 static int
184 j720kbd_intr(void *arg)
185 {
186 	struct j720kbd_softc *sc = arg;
187 	struct j720ssp_softc *ssp = sc->sc_chip->scc_ssp;
188 
189 	bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_EDR, 1);
190 
191 	return j720kbd_poll(sc->sc_chip);
192 }
193 
194 static int
195 j720kbd_poll(void *arg)
196 {
197 	struct j720kbd_chip *scc = arg;
198 	int type, key;
199 	char buf[9], *p;
200 
201 	if (!scc->scc_enabled) {
202 		DPRINTF(("j720kbd_poll: !scc_enabled\n"));
203 		return 0;
204 	}
205 
206 	j720kbd_read(scc, buf);
207 
208 	for (p = buf; *p; p++) {
209 		type = *p & 0x80 ? 0 : 1;
210 		key = *p & 0x7f;
211 
212 		hpckbd_input(scc->scc_hpckbd, type, key);
213 	}
214 
215 	return 1;
216 }
217 
218 static void
219 j720kbd_read(struct j720kbd_chip *scc, char *buf)
220 {
221 	struct j720ssp_softc *ssp = scc->scc_ssp;
222 	int data, count;
223 
224 	bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PCR, 0x2000000);
225 
226 	/* Send scan keycode command. */
227 	if (j720ssp_readwrite(ssp, 1, 0x900, &data, 500) < 0 || data != 0x88) {
228 		DPRINTF(("j720kbd_read: no dummy received\n"));
229 		goto out;
230 	}
231 
232 	/* Read number of scancodes available. */
233 	if (j720ssp_readwrite(ssp, 0, 0x8800, &data, 500) < 0) {
234 		DPRINTF(("j720kbd_read: unable to read number of scancodes\n"));
235 		goto out;
236 	}
237 	J720SSP_INVERT(data);
238 	count = data;
239 
240 	for (; count; count--) {
241 		if (j720ssp_readwrite(ssp, 0, 0x8800, &data, 100) < 0)
242 			goto out;
243 		J720SSP_INVERT(data);
244 		*buf++ = data;
245 	}
246 	*buf = 0;
247 	bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PSR, 0x2000000);
248 
249 	return;
250 
251 out:
252 	*buf = 0;
253 	bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PSR, 0x2000000);
254 
255 	/* reset SSP */
256 	bus_space_write_4(ssp->sc_iot, ssp->sc_ssph, SASSP_CR0, 0x307);
257 	delay(100);
258 	bus_space_write_4(ssp->sc_iot, ssp->sc_ssph, SASSP_CR0, 0x387);
259 
260 	DPRINTF(("j720kbd_read: error %x\n", data));
261 }
262 
263 static int
264 j720kbd_button_event(void *ctx, int type, long id, void *msg)
265 {
266 
267 	if (type != CONFIG_HOOK_BUTTONEVENT ||
268 	    id != CONFIG_HOOK_BUTTONEVENT_LIGHT)
269 		return 0;
270 
271 	sed1356_toggle_lcdlight();
272 
273 	return 1;
274 }
275