1*c7fb772bSthorpej /* $NetBSD: j720kbd.c,v 1.8 2021/08/07 16:18:53 thorpej Exp $ */
2547e1f66Speter
3547e1f66Speter /*-
4547e1f66Speter * Copyright (c) 2006 The NetBSD Foundation, Inc.
5547e1f66Speter * All rights reserved.
6547e1f66Speter *
7547e1f66Speter * This code is derived from software contributed to The NetBSD Foundation
8547e1f66Speter * by IWAMOTO Toshihiro and Peter Postma.
9547e1f66Speter *
10547e1f66Speter * Redistribution and use in source and binary forms, with or without
11547e1f66Speter * modification, are permitted provided that the following conditions
12547e1f66Speter * are met:
13547e1f66Speter * 1. Redistributions of source code must retain the above copyright
14547e1f66Speter * notice, this list of conditions and the following disclaimer.
15547e1f66Speter * 2. Redistributions in binary form must reproduce the above copyright
16547e1f66Speter * notice, this list of conditions and the following disclaimer in the
17547e1f66Speter * documentation and/or other materials provided with the distribution.
18547e1f66Speter *
19547e1f66Speter * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20547e1f66Speter * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21547e1f66Speter * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22547e1f66Speter * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23547e1f66Speter * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24547e1f66Speter * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25547e1f66Speter * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26547e1f66Speter * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27547e1f66Speter * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28547e1f66Speter * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29547e1f66Speter * POSSIBILITY OF SUCH DAMAGE.
30547e1f66Speter */
31547e1f66Speter
32547e1f66Speter /* Jornada 720 keyboard driver. */
33547e1f66Speter
34547e1f66Speter #include <sys/cdefs.h>
35*c7fb772bSthorpej __KERNEL_RCSID(0, "$NetBSD: j720kbd.c,v 1.8 2021/08/07 16:18:53 thorpej Exp $");
36547e1f66Speter
37547e1f66Speter #include <sys/param.h>
38547e1f66Speter #include <sys/systm.h>
39547e1f66Speter #include <sys/device.h>
40547e1f66Speter #include <sys/kernel.h>
41547e1f66Speter
42547e1f66Speter #include <machine/bootinfo.h>
43547e1f66Speter #include <machine/config_hook.h>
44547e1f66Speter #include <machine/platid.h>
45547e1f66Speter #include <machine/platid_mask.h>
46547e1f66Speter
47547e1f66Speter #include <arm/sa11x0/sa11x0_var.h>
48547e1f66Speter #include <arm/sa11x0/sa11x0_gpioreg.h>
49547e1f66Speter #include <arm/sa11x0/sa11x0_ppcreg.h>
50547e1f66Speter #include <arm/sa11x0/sa11x0_sspreg.h>
51547e1f66Speter
52547e1f66Speter #include <dev/hpc/hpckbdvar.h>
53547e1f66Speter
54547e1f66Speter #include <hpcarm/dev/j720sspvar.h>
55547e1f66Speter
56547e1f66Speter #ifdef DEBUG
574c494f76Srjs #define DPRINTF(arg) aprint_normal arg
58547e1f66Speter #else
59547e1f66Speter #define DPRINTF(arg) /* nothing */
60547e1f66Speter #endif
61547e1f66Speter
62547e1f66Speter struct j720kbd_chip {
63547e1f66Speter int scc_enabled;
64547e1f66Speter
65547e1f66Speter struct j720ssp_softc *scc_ssp;
66547e1f66Speter
67547e1f66Speter struct hpckbd_ic_if scc_if;
68547e1f66Speter struct hpckbd_if *scc_hpckbd;
69547e1f66Speter };
70547e1f66Speter
71547e1f66Speter struct j720kbd_softc {
724c494f76Srjs device_t sc_dev;
73547e1f66Speter
74547e1f66Speter struct j720kbd_chip *sc_chip;
75547e1f66Speter };
76547e1f66Speter
77547e1f66Speter static struct j720kbd_chip j720kbd_chip;
78547e1f66Speter
794c494f76Srjs static int j720kbd_match(device_t, cfdata_t, void *);
804c494f76Srjs static void j720kbd_attach(device_t, device_t, void *);
81547e1f66Speter
82547e1f66Speter static void j720kbd_cnattach(void);
83547e1f66Speter static void j720kbd_ifsetup(struct j720kbd_chip *);
84547e1f66Speter static int j720kbd_input_establish(void *, struct hpckbd_if *);
85547e1f66Speter static int j720kbd_intr(void *);
86547e1f66Speter static int j720kbd_poll(void *);
87547e1f66Speter static void j720kbd_read(struct j720kbd_chip *, char *);
88547e1f66Speter
894c494f76Srjs CFATTACH_DECL_NEW(j720kbd, sizeof(struct j720kbd_softc),
90547e1f66Speter j720kbd_match, j720kbd_attach, NULL, NULL);
91547e1f66Speter
92547e1f66Speter
93547e1f66Speter static int
j720kbd_match(device_t parent,cfdata_t cf,void * aux)944c494f76Srjs j720kbd_match(device_t parent, cfdata_t cf, void *aux)
95547e1f66Speter {
96547e1f66Speter
97547e1f66Speter if (!platid_match(&platid, &platid_mask_MACH_HP_JORNADA_7XX))
98547e1f66Speter return 0;
99547e1f66Speter if (strcmp(cf->cf_name, "j720kbd") != 0)
100547e1f66Speter return 0;
101547e1f66Speter
102547e1f66Speter return 1;
103547e1f66Speter }
104547e1f66Speter
105547e1f66Speter static void
j720kbd_attach(device_t parent,device_t self,void * aux)1064c494f76Srjs j720kbd_attach(device_t parent, device_t self, void *aux)
107547e1f66Speter {
1084c494f76Srjs struct j720kbd_softc *sc = device_private(self);
109547e1f66Speter struct hpckbd_attach_args haa;
110547e1f66Speter
1114c494f76Srjs aprint_normal("\n");
1124c6681faSpeter
1134c494f76Srjs sc->sc_dev = self;
114547e1f66Speter sc->sc_chip = &j720kbd_chip;
1154c494f76Srjs sc->sc_chip->scc_ssp = device_private(parent);
116547e1f66Speter sc->sc_chip->scc_enabled = 0;
117547e1f66Speter
118547e1f66Speter /* Attach console if not using serial. */
119547e1f66Speter if (!(bootinfo->bi_cnuse & BI_CNUSE_SERIAL))
120547e1f66Speter j720kbd_cnattach();
121547e1f66Speter
122547e1f66Speter j720kbd_ifsetup(sc->sc_chip);
123547e1f66Speter
124547e1f66Speter /* Install interrupt handler. */
1254c6681faSpeter sa11x0_intr_establish(0, 0, 1, IPL_TTY, j720kbd_intr, sc);
126547e1f66Speter
127547e1f66Speter /* Attach hpckbd. */
128547e1f66Speter haa.haa_ic = &sc->sc_chip->scc_if;
129*c7fb772bSthorpej config_found(self, &haa, hpckbd_print, CFARGS_NONE);
130547e1f66Speter }
131547e1f66Speter
132547e1f66Speter static void
j720kbd_cnattach(void)133547e1f66Speter j720kbd_cnattach(void)
134547e1f66Speter {
135547e1f66Speter struct j720kbd_chip *scc = &j720kbd_chip;
136547e1f66Speter
137547e1f66Speter /* Initialize interface. */
138547e1f66Speter j720kbd_ifsetup(scc);
139547e1f66Speter
140547e1f66Speter /* Attach console. */
141547e1f66Speter hpckbd_cnattach(&scc->scc_if);
142547e1f66Speter }
143547e1f66Speter
144547e1f66Speter static void
j720kbd_ifsetup(struct j720kbd_chip * scc)145547e1f66Speter j720kbd_ifsetup(struct j720kbd_chip *scc)
146547e1f66Speter {
147547e1f66Speter
148547e1f66Speter scc->scc_if.hii_ctx = scc;
149547e1f66Speter scc->scc_if.hii_establish = j720kbd_input_establish;
150547e1f66Speter scc->scc_if.hii_poll = j720kbd_poll;
151547e1f66Speter }
152547e1f66Speter
153547e1f66Speter static int
j720kbd_input_establish(void * ic,struct hpckbd_if * kbdif)154547e1f66Speter j720kbd_input_establish(void *ic, struct hpckbd_if *kbdif)
155547e1f66Speter {
156547e1f66Speter struct j720kbd_chip *scc = ic;
157547e1f66Speter
158547e1f66Speter /* Save hpckbd interface. */
159547e1f66Speter scc->scc_hpckbd = kbdif;
160547e1f66Speter
161547e1f66Speter scc->scc_enabled = 1;
162547e1f66Speter
163547e1f66Speter return 0;
164547e1f66Speter }
165547e1f66Speter
166547e1f66Speter static int
j720kbd_intr(void * arg)167547e1f66Speter j720kbd_intr(void *arg)
168547e1f66Speter {
169547e1f66Speter struct j720kbd_softc *sc = arg;
170547e1f66Speter struct j720ssp_softc *ssp = sc->sc_chip->scc_ssp;
171547e1f66Speter
172547e1f66Speter bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_EDR, 1);
173547e1f66Speter
174547e1f66Speter return j720kbd_poll(sc->sc_chip);
175547e1f66Speter }
176547e1f66Speter
177547e1f66Speter static int
j720kbd_poll(void * arg)178547e1f66Speter j720kbd_poll(void *arg)
179547e1f66Speter {
180547e1f66Speter struct j720kbd_chip *scc = arg;
181547e1f66Speter int type, key;
182547e1f66Speter char buf[9], *p;
183547e1f66Speter
184547e1f66Speter if (!scc->scc_enabled) {
185547e1f66Speter DPRINTF(("j720kbd_poll: !scc_enabled\n"));
186547e1f66Speter return 0;
187547e1f66Speter }
188547e1f66Speter
189547e1f66Speter j720kbd_read(scc, buf);
190547e1f66Speter
191547e1f66Speter for (p = buf; *p; p++) {
192547e1f66Speter type = *p & 0x80 ? 0 : 1;
193547e1f66Speter key = *p & 0x7f;
194547e1f66Speter
195547e1f66Speter hpckbd_input(scc->scc_hpckbd, type, key);
196547e1f66Speter }
197547e1f66Speter
198547e1f66Speter return 1;
199547e1f66Speter }
200547e1f66Speter
201547e1f66Speter static void
j720kbd_read(struct j720kbd_chip * scc,char * buf)202547e1f66Speter j720kbd_read(struct j720kbd_chip *scc, char *buf)
203547e1f66Speter {
204547e1f66Speter struct j720ssp_softc *ssp = scc->scc_ssp;
205547e1f66Speter int data, count;
206547e1f66Speter
207547e1f66Speter bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PCR, 0x2000000);
208547e1f66Speter
209547e1f66Speter /* Send scan keycode command. */
2100c9a60ffSpeter if (j720ssp_readwrite(ssp, 1, 0x90, &data, 500) < 0 || data != 0x11) {
211547e1f66Speter DPRINTF(("j720kbd_read: no dummy received\n"));
212547e1f66Speter goto out;
213547e1f66Speter }
214547e1f66Speter
215547e1f66Speter /* Read number of scancodes available. */
2160c9a60ffSpeter if (j720ssp_readwrite(ssp, 0, 0x11, &data, 500) < 0) {
217547e1f66Speter DPRINTF(("j720kbd_read: unable to read number of scancodes\n"));
218547e1f66Speter goto out;
219547e1f66Speter }
220547e1f66Speter count = data;
221547e1f66Speter
222547e1f66Speter for (; count; count--) {
2230c9a60ffSpeter if (j720ssp_readwrite(ssp, 0, 0x11, &data, 100) < 0)
224547e1f66Speter goto out;
225547e1f66Speter *buf++ = data;
226547e1f66Speter }
227547e1f66Speter *buf = 0;
228547e1f66Speter bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PSR, 0x2000000);
229547e1f66Speter
230547e1f66Speter return;
231547e1f66Speter
232547e1f66Speter out:
233547e1f66Speter *buf = 0;
234547e1f66Speter bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PSR, 0x2000000);
235547e1f66Speter
236547e1f66Speter /* reset SSP */
237547e1f66Speter bus_space_write_4(ssp->sc_iot, ssp->sc_ssph, SASSP_CR0, 0x307);
238547e1f66Speter delay(100);
239547e1f66Speter bus_space_write_4(ssp->sc_iot, ssp->sc_ssph, SASSP_CR0, 0x387);
240547e1f66Speter
241547e1f66Speter DPRINTF(("j720kbd_read: error %x\n", data));
242547e1f66Speter }
243