j720kbd.c revision 1.1 1 1.1 peter /* $NetBSD: j720kbd.c,v 1.1 2006/03/04 14:09:36 peter Exp $ */
2 1.1 peter
3 1.1 peter /*-
4 1.1 peter * Copyright (c) 2006 The NetBSD Foundation, Inc.
5 1.1 peter * All rights reserved.
6 1.1 peter *
7 1.1 peter * This code is derived from software contributed to The NetBSD Foundation
8 1.1 peter * by IWAMOTO Toshihiro and Peter Postma.
9 1.1 peter *
10 1.1 peter * Redistribution and use in source and binary forms, with or without
11 1.1 peter * modification, are permitted provided that the following conditions
12 1.1 peter * are met:
13 1.1 peter * 1. Redistributions of source code must retain the above copyright
14 1.1 peter * notice, this list of conditions and the following disclaimer.
15 1.1 peter * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 peter * notice, this list of conditions and the following disclaimer in the
17 1.1 peter * documentation and/or other materials provided with the distribution.
18 1.1 peter * 3. All advertising materials mentioning features or use of this software
19 1.1 peter * must display the following acknowledgement:
20 1.1 peter * This product includes software developed by the NetBSD
21 1.1 peter * Foundation, Inc. and its contributors.
22 1.1 peter * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 peter * contributors may be used to endorse or promote products derived
24 1.1 peter * from this software without specific prior written permission.
25 1.1 peter *
26 1.1 peter * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 peter * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 peter * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 peter * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 peter * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 peter * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 peter * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 peter * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 peter * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 peter * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 peter * POSSIBILITY OF SUCH DAMAGE.
37 1.1 peter */
38 1.1 peter
39 1.1 peter /* Jornada 720 keyboard driver. */
40 1.1 peter
41 1.1 peter #include <sys/cdefs.h>
42 1.1 peter __KERNEL_RCSID(0, "$NetBSD: j720kbd.c,v 1.1 2006/03/04 14:09:36 peter Exp $");
43 1.1 peter
44 1.1 peter #include <sys/param.h>
45 1.1 peter #include <sys/systm.h>
46 1.1 peter #include <sys/device.h>
47 1.1 peter #include <sys/kernel.h>
48 1.1 peter
49 1.1 peter #include <machine/bootinfo.h>
50 1.1 peter #include <machine/config_hook.h>
51 1.1 peter #include <machine/platid.h>
52 1.1 peter #include <machine/platid_mask.h>
53 1.1 peter
54 1.1 peter #include <arm/sa11x0/sa11x0_var.h>
55 1.1 peter #include <arm/sa11x0/sa11x0_gpioreg.h>
56 1.1 peter #include <arm/sa11x0/sa11x0_ppcreg.h>
57 1.1 peter #include <arm/sa11x0/sa11x0_sspreg.h>
58 1.1 peter
59 1.1 peter #include <dev/hpc/hpckbdvar.h>
60 1.1 peter
61 1.1 peter #include <hpcarm/dev/j720sspvar.h>
62 1.1 peter #include <hpcarm/dev/sed1356var.h>
63 1.1 peter
64 1.1 peter #ifdef DEBUG
65 1.1 peter #define DPRINTF(arg) printf arg
66 1.1 peter #else
67 1.1 peter #define DPRINTF(arg) /* nothing */
68 1.1 peter #endif
69 1.1 peter
70 1.1 peter struct j720kbd_chip {
71 1.1 peter int scc_enabled;
72 1.1 peter
73 1.1 peter struct j720ssp_softc *scc_ssp;
74 1.1 peter
75 1.1 peter struct hpckbd_ic_if scc_if;
76 1.1 peter struct hpckbd_if *scc_hpckbd;
77 1.1 peter };
78 1.1 peter
79 1.1 peter struct j720kbd_softc {
80 1.1 peter struct device sc_dev;
81 1.1 peter
82 1.1 peter struct j720kbd_chip *sc_chip;
83 1.1 peter void *sc_ih;
84 1.1 peter };
85 1.1 peter
86 1.1 peter static struct j720kbd_chip j720kbd_chip;
87 1.1 peter
88 1.1 peter static int j720kbd_match(struct device *, struct cfdata *, void *);
89 1.1 peter static void j720kbd_attach(struct device *, struct device *, void *);
90 1.1 peter
91 1.1 peter static void j720kbd_cnattach(void);
92 1.1 peter static void j720kbd_ifsetup(struct j720kbd_chip *);
93 1.1 peter static int j720kbd_input_establish(void *, struct hpckbd_if *);
94 1.1 peter static int j720kbd_intr(void *);
95 1.1 peter static int j720kbd_poll(void *);
96 1.1 peter static void j720kbd_read(struct j720kbd_chip *, char *);
97 1.1 peter static int j720kbd_button_event(void *, int, long, void *);
98 1.1 peter
99 1.1 peter CFATTACH_DECL(j720kbd, sizeof(struct device),
100 1.1 peter j720kbd_match, j720kbd_attach, NULL, NULL);
101 1.1 peter
102 1.1 peter
103 1.1 peter static int
104 1.1 peter j720kbd_match(struct device *parent, struct cfdata *cf, void *aux)
105 1.1 peter {
106 1.1 peter
107 1.1 peter if (!platid_match(&platid, &platid_mask_MACH_HP_JORNADA_7XX))
108 1.1 peter return 0;
109 1.1 peter if (strcmp(cf->cf_name, "j720kbd") != 0)
110 1.1 peter return 0;
111 1.1 peter
112 1.1 peter return 1;
113 1.1 peter }
114 1.1 peter
115 1.1 peter static void
116 1.1 peter j720kbd_attach(struct device *parent, struct device *self, void *aux)
117 1.1 peter {
118 1.1 peter struct j720kbd_softc *sc = (void *)self;
119 1.1 peter struct hpckbd_attach_args haa;
120 1.1 peter
121 1.1 peter sc->sc_chip = &j720kbd_chip;
122 1.1 peter sc->sc_chip->scc_ssp = (struct j720ssp_softc *)parent;
123 1.1 peter sc->sc_chip->scc_enabled = 0;
124 1.1 peter
125 1.1 peter /* Attach console if not using serial. */
126 1.1 peter if (!(bootinfo->bi_cnuse & BI_CNUSE_SERIAL))
127 1.1 peter j720kbd_cnattach();
128 1.1 peter
129 1.1 peter j720kbd_ifsetup(sc->sc_chip);
130 1.1 peter
131 1.1 peter /* Install interrupt handler. */
132 1.1 peter if ((sc->sc_ih = sa11x0_intr_establish(0, 0, 1, IPL_TTY,
133 1.1 peter j720kbd_intr, sc)) == NULL) {
134 1.1 peter printf(": can't establish interrupt\n");
135 1.1 peter return;
136 1.1 peter }
137 1.1 peter printf("\n");
138 1.1 peter
139 1.1 peter /* Add config hook for light button event. */
140 1.1 peter config_hook(CONFIG_HOOK_BUTTONEVENT,
141 1.1 peter CONFIG_HOOK_BUTTONEVENT_LIGHT,
142 1.1 peter CONFIG_HOOK_SHARE, j720kbd_button_event, sc);
143 1.1 peter
144 1.1 peter /* Attach hpckbd. */
145 1.1 peter haa.haa_ic = &sc->sc_chip->scc_if;
146 1.1 peter config_found(self, &haa, hpckbd_print);
147 1.1 peter }
148 1.1 peter
149 1.1 peter static void
150 1.1 peter j720kbd_cnattach(void)
151 1.1 peter {
152 1.1 peter struct j720kbd_chip *scc = &j720kbd_chip;
153 1.1 peter
154 1.1 peter /* Initialize interface. */
155 1.1 peter j720kbd_ifsetup(scc);
156 1.1 peter
157 1.1 peter /* Attach console. */
158 1.1 peter hpckbd_cnattach(&scc->scc_if);
159 1.1 peter }
160 1.1 peter
161 1.1 peter static void
162 1.1 peter j720kbd_ifsetup(struct j720kbd_chip *scc)
163 1.1 peter {
164 1.1 peter
165 1.1 peter scc->scc_if.hii_ctx = scc;
166 1.1 peter scc->scc_if.hii_establish = j720kbd_input_establish;
167 1.1 peter scc->scc_if.hii_poll = j720kbd_poll;
168 1.1 peter }
169 1.1 peter
170 1.1 peter static int
171 1.1 peter j720kbd_input_establish(void *ic, struct hpckbd_if *kbdif)
172 1.1 peter {
173 1.1 peter struct j720kbd_chip *scc = ic;
174 1.1 peter
175 1.1 peter /* Save hpckbd interface. */
176 1.1 peter scc->scc_hpckbd = kbdif;
177 1.1 peter
178 1.1 peter scc->scc_enabled = 1;
179 1.1 peter
180 1.1 peter return 0;
181 1.1 peter }
182 1.1 peter
183 1.1 peter static int
184 1.1 peter j720kbd_intr(void *arg)
185 1.1 peter {
186 1.1 peter struct j720kbd_softc *sc = arg;
187 1.1 peter struct j720ssp_softc *ssp = sc->sc_chip->scc_ssp;
188 1.1 peter
189 1.1 peter bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_EDR, 1);
190 1.1 peter
191 1.1 peter return j720kbd_poll(sc->sc_chip);
192 1.1 peter }
193 1.1 peter
194 1.1 peter static int
195 1.1 peter j720kbd_poll(void *arg)
196 1.1 peter {
197 1.1 peter struct j720kbd_chip *scc = arg;
198 1.1 peter int type, key;
199 1.1 peter char buf[9], *p;
200 1.1 peter
201 1.1 peter if (!scc->scc_enabled) {
202 1.1 peter DPRINTF(("j720kbd_poll: !scc_enabled\n"));
203 1.1 peter return 0;
204 1.1 peter }
205 1.1 peter
206 1.1 peter j720kbd_read(scc, buf);
207 1.1 peter
208 1.1 peter for (p = buf; *p; p++) {
209 1.1 peter type = *p & 0x80 ? 0 : 1;
210 1.1 peter key = *p & 0x7f;
211 1.1 peter
212 1.1 peter hpckbd_input(scc->scc_hpckbd, type, key);
213 1.1 peter }
214 1.1 peter
215 1.1 peter return 1;
216 1.1 peter }
217 1.1 peter
218 1.1 peter static void
219 1.1 peter j720kbd_read(struct j720kbd_chip *scc, char *buf)
220 1.1 peter {
221 1.1 peter struct j720ssp_softc *ssp = scc->scc_ssp;
222 1.1 peter int data, count;
223 1.1 peter
224 1.1 peter bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PCR, 0x2000000);
225 1.1 peter
226 1.1 peter /* Send scan keycode command. */
227 1.1 peter if (j720ssp_readwrite(ssp, 1, 0x900, &data, 500) < 0 || data != 0x88) {
228 1.1 peter DPRINTF(("j720kbd_read: no dummy received\n"));
229 1.1 peter goto out;
230 1.1 peter }
231 1.1 peter
232 1.1 peter /* Read number of scancodes available. */
233 1.1 peter if (j720ssp_readwrite(ssp, 0, 0x8800, &data, 500) < 0) {
234 1.1 peter DPRINTF(("j720kbd_read: unable to read number of scancodes\n"));
235 1.1 peter goto out;
236 1.1 peter }
237 1.1 peter J720SSP_INVERT(data);
238 1.1 peter count = data;
239 1.1 peter
240 1.1 peter for (; count; count--) {
241 1.1 peter if (j720ssp_readwrite(ssp, 0, 0x8800, &data, 100) < 0)
242 1.1 peter goto out;
243 1.1 peter J720SSP_INVERT(data);
244 1.1 peter *buf++ = data;
245 1.1 peter }
246 1.1 peter *buf = 0;
247 1.1 peter bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PSR, 0x2000000);
248 1.1 peter
249 1.1 peter return;
250 1.1 peter
251 1.1 peter out:
252 1.1 peter *buf = 0;
253 1.1 peter bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PSR, 0x2000000);
254 1.1 peter
255 1.1 peter /* reset SSP */
256 1.1 peter bus_space_write_4(ssp->sc_iot, ssp->sc_ssph, SASSP_CR0, 0x307);
257 1.1 peter delay(100);
258 1.1 peter bus_space_write_4(ssp->sc_iot, ssp->sc_ssph, SASSP_CR0, 0x387);
259 1.1 peter
260 1.1 peter DPRINTF(("j720kbd_read: error %x\n", data));
261 1.1 peter }
262 1.1 peter
263 1.1 peter static int
264 1.1 peter j720kbd_button_event(void *ctx, int type, long id, void *msg)
265 1.1 peter {
266 1.1 peter
267 1.1 peter if (type != CONFIG_HOOK_BUTTONEVENT ||
268 1.1 peter id != CONFIG_HOOK_BUTTONEVENT_LIGHT)
269 1.1 peter return 0;
270 1.1 peter
271 1.1 peter sed1356_toggle_lcdlight();
272 1.1 peter
273 1.1 peter return 1;
274 1.1 peter }
275