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