xref: /netbsd-src/sys/arch/hpcarm/dev/j720kbd.c (revision fad4c9f71477ae11cea2ee75ec82151ac770a534)
1 /*	$NetBSD: j720kbd.c,v 1.3 2006/06/27 14:36:50 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.3 2006/06/27 14:36:50 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 };
84 
85 static struct j720kbd_chip j720kbd_chip;
86 
87 static int	j720kbd_match(struct device *, struct cfdata *, void *);
88 static void	j720kbd_attach(struct device *, struct device *, void *);
89 
90 static void	j720kbd_cnattach(void);
91 static void	j720kbd_ifsetup(struct j720kbd_chip *);
92 static int	j720kbd_input_establish(void *, struct hpckbd_if *);
93 static int	j720kbd_intr(void *);
94 static int	j720kbd_poll(void *);
95 static void	j720kbd_read(struct j720kbd_chip *, char *);
96 static int	j720kbd_button_event(void *, int, long, void *);
97 
98 CFATTACH_DECL(j720kbd, sizeof(struct j720kbd_softc),
99     j720kbd_match, j720kbd_attach, NULL, NULL);
100 
101 
102 static int
103 j720kbd_match(struct device *parent, struct cfdata *cf, void *aux)
104 {
105 
106 	if (!platid_match(&platid, &platid_mask_MACH_HP_JORNADA_7XX))
107 		return 0;
108 	if (strcmp(cf->cf_name, "j720kbd") != 0)
109 		return 0;
110 
111 	return 1;
112 }
113 
114 static void
115 j720kbd_attach(struct device *parent, struct device *self, void *aux)
116 {
117 	struct j720kbd_softc *sc = (void *)self;
118 	struct hpckbd_attach_args haa;
119 
120 	printf("\n");
121 
122 	sc->sc_chip = &j720kbd_chip;
123 	sc->sc_chip->scc_ssp = (struct j720ssp_softc *)parent;
124 	sc->sc_chip->scc_enabled = 0;
125 
126 	/* Attach console if not using serial. */
127 	if (!(bootinfo->bi_cnuse & BI_CNUSE_SERIAL))
128 		j720kbd_cnattach();
129 
130 	j720kbd_ifsetup(sc->sc_chip);
131 
132 	/* Install interrupt handler. */
133 	sa11x0_intr_establish(0, 0, 1, IPL_TTY, j720kbd_intr, sc);
134 
135 	/* Add config hook for light button event. */
136 	config_hook(CONFIG_HOOK_BUTTONEVENT,
137 		    CONFIG_HOOK_BUTTONEVENT_LIGHT,
138 		    CONFIG_HOOK_SHARE, j720kbd_button_event, sc);
139 
140 	/* Attach hpckbd. */
141 	haa.haa_ic = &sc->sc_chip->scc_if;
142 	config_found(self, &haa, hpckbd_print);
143 }
144 
145 static void
146 j720kbd_cnattach(void)
147 {
148 	struct j720kbd_chip *scc = &j720kbd_chip;
149 
150 	/* Initialize interface. */
151 	j720kbd_ifsetup(scc);
152 
153 	/* Attach console. */
154 	hpckbd_cnattach(&scc->scc_if);
155 }
156 
157 static void
158 j720kbd_ifsetup(struct j720kbd_chip *scc)
159 {
160 
161 	scc->scc_if.hii_ctx = scc;
162 	scc->scc_if.hii_establish = j720kbd_input_establish;
163 	scc->scc_if.hii_poll = j720kbd_poll;
164 }
165 
166 static int
167 j720kbd_input_establish(void *ic, struct hpckbd_if *kbdif)
168 {
169 	struct j720kbd_chip *scc = ic;
170 
171 	/* Save hpckbd interface. */
172 	scc->scc_hpckbd = kbdif;
173 
174 	scc->scc_enabled = 1;
175 
176 	return 0;
177 }
178 
179 static int
180 j720kbd_intr(void *arg)
181 {
182 	struct j720kbd_softc *sc = arg;
183 	struct j720ssp_softc *ssp = sc->sc_chip->scc_ssp;
184 
185 	bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_EDR, 1);
186 
187 	return j720kbd_poll(sc->sc_chip);
188 }
189 
190 static int
191 j720kbd_poll(void *arg)
192 {
193 	struct j720kbd_chip *scc = arg;
194 	int type, key;
195 	char buf[9], *p;
196 
197 	if (!scc->scc_enabled) {
198 		DPRINTF(("j720kbd_poll: !scc_enabled\n"));
199 		return 0;
200 	}
201 
202 	j720kbd_read(scc, buf);
203 
204 	for (p = buf; *p; p++) {
205 		type = *p & 0x80 ? 0 : 1;
206 		key = *p & 0x7f;
207 
208 		hpckbd_input(scc->scc_hpckbd, type, key);
209 	}
210 
211 	return 1;
212 }
213 
214 static void
215 j720kbd_read(struct j720kbd_chip *scc, char *buf)
216 {
217 	struct j720ssp_softc *ssp = scc->scc_ssp;
218 	int data, count;
219 
220 	bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PCR, 0x2000000);
221 
222 	/* Send scan keycode command. */
223 	if (j720ssp_readwrite(ssp, 1, 0x90, &data, 500) < 0 || data != 0x11) {
224 		DPRINTF(("j720kbd_read: no dummy received\n"));
225 		goto out;
226 	}
227 
228 	/* Read number of scancodes available. */
229 	if (j720ssp_readwrite(ssp, 0, 0x11, &data, 500) < 0) {
230 		DPRINTF(("j720kbd_read: unable to read number of scancodes\n"));
231 		goto out;
232 	}
233 	count = data;
234 
235 	for (; count; count--) {
236 		if (j720ssp_readwrite(ssp, 0, 0x11, &data, 100) < 0)
237 			goto out;
238 		*buf++ = data;
239 	}
240 	*buf = 0;
241 	bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PSR, 0x2000000);
242 
243 	return;
244 
245 out:
246 	*buf = 0;
247 	bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PSR, 0x2000000);
248 
249 	/* reset SSP */
250 	bus_space_write_4(ssp->sc_iot, ssp->sc_ssph, SASSP_CR0, 0x307);
251 	delay(100);
252 	bus_space_write_4(ssp->sc_iot, ssp->sc_ssph, SASSP_CR0, 0x387);
253 
254 	DPRINTF(("j720kbd_read: error %x\n", data));
255 }
256 
257 static int
258 j720kbd_button_event(void *ctx, int type, long id, void *msg)
259 {
260 
261 	if (type != CONFIG_HOOK_BUTTONEVENT ||
262 	    id != CONFIG_HOOK_BUTTONEVENT_LIGHT)
263 		return 0;
264 
265 	sed1356_toggle_lcdlight();
266 
267 	return 1;
268 }
269