sa1111_kbc.c revision 1.13 1 1.13 tls /* $NetBSD: sa1111_kbc.c,v 1.13 2011/11/19 22:51:19 tls Exp $ */
2 1.1 bsh
3 1.1 bsh /*
4 1.3 bjh21 * Copyright (c) 2004 Ben Harris.
5 1.6 bsh * Copyright (c) 2002, 2004 Genetec Corporation. All rights reserved.
6 1.1 bsh * Written by Hiroyuki Bessho for Genetec Corporation.
7 1.1 bsh *
8 1.1 bsh * Redistribution and use in source and binary forms, with or without
9 1.1 bsh * modification, are permitted provided that the following conditions
10 1.1 bsh * are met:
11 1.1 bsh * 1. Redistributions of source code must retain the above copyright
12 1.1 bsh * notice, this list of conditions and the following disclaimer.
13 1.1 bsh * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 bsh * notice, this list of conditions and the following disclaimer in the
15 1.1 bsh * documentation and/or other materials provided with the distribution.
16 1.1 bsh * 3. The name of Genetec Corporation may not be used to endorse or
17 1.1 bsh * promote products derived from this software without specific prior
18 1.1 bsh * written permission.
19 1.1 bsh *
20 1.1 bsh * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
21 1.1 bsh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.1 bsh * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.1 bsh * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GENETEC CORPORATION
24 1.1 bsh * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.1 bsh * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.1 bsh * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.1 bsh * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.1 bsh * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.1 bsh * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1 bsh * POSSIBILITY OF SUCH DAMAGE.
31 1.1 bsh *
32 1.1 bsh * Driver for keyboard controller in SA-1111 companion chip.
33 1.1 bsh */
34 1.1 bsh /*
35 1.1 bsh * Copyright (c) 1998
36 1.1 bsh * Matthias Drochner. All rights reserved.
37 1.1 bsh *
38 1.1 bsh * Redistribution and use in source and binary forms, with or without
39 1.1 bsh * modification, are permitted provided that the following conditions
40 1.1 bsh * are met:
41 1.1 bsh * 1. Redistributions of source code must retain the above copyright
42 1.1 bsh * notice, this list of conditions and the following disclaimer.
43 1.1 bsh * 2. Redistributions in binary form must reproduce the above copyright
44 1.1 bsh * notice, this list of conditions and the following disclaimer in the
45 1.1 bsh * documentation and/or other materials provided with the distribution.
46 1.1 bsh *
47 1.1 bsh * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
48 1.1 bsh * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49 1.1 bsh * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
50 1.1 bsh * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
51 1.1 bsh * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
52 1.1 bsh * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
53 1.1 bsh * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
54 1.1 bsh * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
55 1.1 bsh * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
56 1.1 bsh * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57 1.1 bsh */
58 1.2 lukem
59 1.2 lukem #include <sys/cdefs.h>
60 1.13 tls __KERNEL_RCSID(0, "$NetBSD: sa1111_kbc.c,v 1.13 2011/11/19 22:51:19 tls Exp $");
61 1.1 bsh
62 1.1 bsh #include <sys/param.h>
63 1.1 bsh #include <sys/systm.h>
64 1.1 bsh #include <sys/types.h>
65 1.1 bsh #include <sys/callout.h>
66 1.1 bsh #include <sys/kernel.h>
67 1.1 bsh #include <sys/proc.h>
68 1.1 bsh #include <sys/conf.h>
69 1.1 bsh #include <sys/device.h>
70 1.1 bsh #include <sys/malloc.h>
71 1.1 bsh #include <sys/errno.h>
72 1.1 bsh #include <sys/queue.h>
73 1.10 ad #include <sys/bus.h>
74 1.1 bsh
75 1.1 bsh #include <arm/sa11x0/sa1111_reg.h>
76 1.1 bsh #include <arm/sa11x0/sa1111_var.h>
77 1.1 bsh
78 1.3 bjh21 #include <dev/pckbport/pckbportvar.h> /* for prototypes */
79 1.1 bsh
80 1.1 bsh #include "pckbd.h"
81 1.1 bsh #include "rnd.h"
82 1.1 bsh #include "locators.h"
83 1.1 bsh
84 1.1 bsh struct sackbc_softc {
85 1.11 rjs device_t dev;
86 1.1 bsh
87 1.1 bsh bus_space_tag_t iot;
88 1.1 bsh bus_space_handle_t ioh;
89 1.1 bsh
90 1.1 bsh void *ih_rx; /* receive interrupt */
91 1.1 bsh int intr; /* interrupt number */
92 1.6 bsh int slot; /* KBD_SLOT or AUX_SLOT */
93 1.1 bsh
94 1.1 bsh int polling; /* don't process data in interrupt handler */
95 1.1 bsh int poll_stat; /* data read from inr handler if polling */
96 1.1 bsh int poll_data; /* status read from intr handler if polling */
97 1.1 bsh
98 1.6 bsh #if NRND > 0
99 1.13 tls krndsource_t rnd_source;
100 1.6 bsh #endif
101 1.3 bjh21 pckbport_tag_t pt;
102 1.1 bsh };
103 1.1 bsh
104 1.11 rjs static int sackbc_match(device_t, cfdata_t, void *);
105 1.11 rjs static void sackbc_attach(device_t, device_t, void *);
106 1.3 bjh21
107 1.3 bjh21 static int sackbc_xt_translation(void *, pckbport_slot_t, int);
108 1.3 bjh21 #define sackbc_send_devcmd sackbc_send_cmd
109 1.3 bjh21 static int sackbc_send_devcmd(void *, pckbport_slot_t, u_char);
110 1.3 bjh21 static int sackbc_poll_data1(void *, pckbport_slot_t);
111 1.3 bjh21 static void sackbc_slot_enable(void *, pckbport_slot_t, int);
112 1.3 bjh21 static void sackbc_intr_establish(void *, pckbport_slot_t);
113 1.3 bjh21 static void sackbc_set_poll(void *, pckbport_slot_t, int);
114 1.1 bsh
115 1.11 rjs CFATTACH_DECL_NEW(sackbc, sizeof(struct sackbc_softc), sackbc_match,
116 1.1 bsh sackbc_attach, NULL, NULL);
117 1.1 bsh
118 1.3 bjh21 static struct pckbport_accessops const sackbc_ops = {
119 1.3 bjh21 sackbc_xt_translation,
120 1.3 bjh21 sackbc_send_devcmd,
121 1.3 bjh21 sackbc_poll_data1,
122 1.3 bjh21 sackbc_slot_enable,
123 1.3 bjh21 sackbc_intr_establish,
124 1.3 bjh21 sackbc_set_poll
125 1.3 bjh21 };
126 1.1 bsh
127 1.1 bsh #define KBD_DELAY DELAY(8)
128 1.1 bsh
129 1.1 bsh /*#define SACKBCDEBUG*/
130 1.1 bsh
131 1.1 bsh #ifdef SACKBCDEBUG
132 1.1 bsh #define DPRINTF(arg) printf arg
133 1.1 bsh #else
134 1.1 bsh #define DPRINTF(arg)
135 1.1 bsh #endif
136 1.1 bsh
137 1.1 bsh
138 1.1 bsh static int
139 1.11 rjs sackbc_match(device_t parent, cfdata_t cf, void *aux)
140 1.1 bsh {
141 1.1 bsh struct sa1111_attach_args *aa = (struct sa1111_attach_args *)aux;
142 1.1 bsh
143 1.5 bsh switch (aa->sa_addr) {
144 1.9 peter case SACC_KBD0:
145 1.9 peter case SACC_KBD1:
146 1.1 bsh return 1;
147 1.1 bsh }
148 1.1 bsh return 0;
149 1.1 bsh }
150 1.1 bsh
151 1.1 bsh #if 0
152 1.1 bsh static int
153 1.5 bsh sackbc_txint(void *cookie)
154 1.1 bsh {
155 1.1 bsh struct sackbc_softc *sc = cookie;
156 1.1 bsh
157 1.5 bsh bus_space_read_4(sc->iot, sc->ioh, SACCKBD_STAT);
158 1.1 bsh
159 1.1 bsh return 0;
160 1.1 bsh }
161 1.1 bsh #endif
162 1.1 bsh
163 1.1 bsh static int
164 1.5 bsh sackbc_rxint(void *cookie)
165 1.1 bsh {
166 1.1 bsh struct sackbc_softc *sc = cookie;
167 1.9 peter int stat, code = -1;
168 1.1 bsh
169 1.5 bsh stat = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_STAT);
170 1.5 bsh DPRINTF(("sackbc_rxint stat=%x\n", stat));
171 1.5 bsh if (stat & KBDSTAT_RXF) {
172 1.5 bsh code = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_DATA);
173 1.1 bsh
174 1.6 bsh #if NRND > 0
175 1.6 bsh rnd_add_uint32(&sc->rnd_source, (stat<<8)|data);
176 1.6 bsh #endif
177 1.6 bsh
178 1.5 bsh if (sc->polling) {
179 1.1 bsh sc->poll_data = code;
180 1.1 bsh sc->poll_stat = stat;
181 1.9 peter } else
182 1.6 bsh pckbportintr(sc->pt, sc->slot, code);
183 1.1 bsh return 1;
184 1.1 bsh }
185 1.1 bsh
186 1.1 bsh return 0;
187 1.1 bsh }
188 1.1 bsh
189 1.3 bjh21 static void
190 1.3 bjh21 sackbc_intr_establish(void *cookie, pckbport_slot_t slot)
191 1.1 bsh {
192 1.3 bjh21 struct sackbc_softc *sc = cookie;
193 1.1 bsh
194 1.9 peter if (!(sc->polling) && sc->ih_rx == NULL) {
195 1.5 bsh sc->ih_rx = sacc_intr_establish(
196 1.12 bsh (sacc_chipset_tag_t *)
197 1.12 bsh device_private(device_parent(sc->dev)),
198 1.5 bsh sc->intr+1, IST_EDGE_RAISE, IPL_TTY, sackbc_rxint, sc);
199 1.5 bsh if (sc->ih_rx == NULL) {
200 1.11 rjs aprint_normal_dev(sc->dev, "can't establish interrupt\n");
201 1.1 bsh }
202 1.1 bsh }
203 1.1 bsh }
204 1.1 bsh
205 1.1 bsh static void
206 1.5 bsh sackbc_disable_intrhandler(struct sackbc_softc *sc)
207 1.1 bsh {
208 1.5 bsh if (sc->polling && sc->ih_rx) {
209 1.5 bsh sacc_intr_disestablish(
210 1.12 bsh (sacc_chipset_tag_t *)
211 1.12 bsh device_private(device_parent(sc->dev)),
212 1.5 bsh sc->ih_rx);
213 1.1 bsh sc->ih_rx = NULL;
214 1.1 bsh }
215 1.1 bsh }
216 1.1 bsh
217 1.1 bsh static void
218 1.11 rjs sackbc_attach(device_t parent, device_t self, void *aux)
219 1.1 bsh {
220 1.11 rjs struct sackbc_softc *sc = device_private(self);
221 1.11 rjs struct sacc_softc *psc = device_private(parent);
222 1.1 bsh struct sa1111_attach_args *aa = (struct sa1111_attach_args *)aux;
223 1.11 rjs device_t child;
224 1.1 bsh uint32_t tmp, clock_bit;
225 1.6 bsh int intr, slot;
226 1.1 bsh
227 1.5 bsh switch (aa->sa_addr) {
228 1.1 bsh case SACC_KBD0: clock_bit = (1<<6); intr = 21; break;
229 1.1 bsh case SACC_KBD1: clock_bit = (1<<5); intr = 18; break;
230 1.1 bsh default:
231 1.1 bsh return;
232 1.1 bsh }
233 1.1 bsh
234 1.5 bsh if (aa->sa_size <= 0)
235 1.1 bsh aa->sa_size = SACCKBD_SIZE;
236 1.5 bsh if (aa->sa_intr == SACCCF_INTR_DEFAULT)
237 1.1 bsh aa->sa_intr = intr;
238 1.1 bsh
239 1.11 rjs sc->dev = self;
240 1.1 bsh sc->iot = psc->sc_iot;
241 1.5 bsh if (bus_space_subregion(psc->sc_iot, psc->sc_ioh,
242 1.5 bsh aa->sa_addr, aa->sa_size, &sc->ioh)) {
243 1.11 rjs aprint_normal(": can't map subregion\n");
244 1.1 bsh return;
245 1.1 bsh }
246 1.1 bsh
247 1.1 bsh /* enable clock for PS/2 kbd or mouse */
248 1.5 bsh tmp = bus_space_read_4(psc->sc_iot, psc->sc_ioh, SACCSC_SKPCR);
249 1.5 bsh bus_space_write_4(psc->sc_iot, psc->sc_ioh, SACCSC_SKPCR,
250 1.5 bsh tmp | clock_bit);
251 1.1 bsh
252 1.1 bsh sc->ih_rx = NULL;
253 1.1 bsh sc->intr = aa->sa_intr;
254 1.1 bsh sc->polling = 0;
255 1.1 bsh
256 1.5 bsh tmp = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_CR);
257 1.5 bsh bus_space_write_4(sc->iot, sc->ioh, SACCKBD_CR, tmp | KBDCR_ENA);
258 1.1 bsh
259 1.1 bsh /* XXX: this is necessary to get keyboard working. but I don't know why */
260 1.5 bsh bus_space_write_4(sc->iot, sc->ioh, SACCKBD_CLKDIV, 2);
261 1.1 bsh
262 1.5 bsh tmp = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_STAT);
263 1.5 bsh if ((tmp & KBDSTAT_ENA) == 0) {
264 1.1 bsh printf("??? can't enable KBD controller\n");
265 1.1 bsh return;
266 1.1 bsh }
267 1.1 bsh
268 1.1 bsh printf("\n");
269 1.1 bsh
270 1.3 bjh21 sc->pt = pckbport_attach(sc, &sackbc_ops);
271 1.1 bsh
272 1.6 bsh /*
273 1.9 peter * Although there is no such thing as SLOT for SA-1111 kbd
274 1.6 bsh * controller, pckbd and pms drivers require it.
275 1.6 bsh */
276 1.9 peter for (slot = PCKBPORT_KBD_SLOT; slot <= PCKBPORT_AUX_SLOT; ++slot) {
277 1.6 bsh child = pckbport_attach_slot(self, sc->pt, slot);
278 1.6 bsh
279 1.6 bsh if (child == NULL)
280 1.6 bsh continue;
281 1.6 bsh sc->slot = slot;
282 1.6 bsh #if NRND > 0
283 1.11 rjs rnd_attach_source(&sc->rnd_source, device_xname(child),
284 1.6 bsh RND_TYPE_TTY, 0);
285 1.1 bsh #endif
286 1.6 bsh /* only one of KBD_SLOT or AUX_SLOT is used. */
287 1.6 bsh break;
288 1.6 bsh }
289 1.1 bsh }
290 1.1 bsh
291 1.1 bsh
292 1.1 bsh static inline int
293 1.5 bsh sackbc_wait_output(struct sackbc_softc *sc)
294 1.1 bsh {
295 1.1 bsh u_int i, stat;
296 1.1 bsh
297 1.1 bsh for (i = 100000; i; i--){
298 1.1 bsh stat = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_STAT);
299 1.1 bsh delay(100);
300 1.5 bsh if (stat & KBDSTAT_TXE)
301 1.1 bsh return 1;
302 1.1 bsh }
303 1.1 bsh return 0;
304 1.1 bsh }
305 1.1 bsh
306 1.1 bsh static int
307 1.5 bsh sackbc_poll_data1(void *cookie, pckbport_slot_t slot)
308 1.1 bsh {
309 1.3 bjh21 struct sackbc_softc *sc = cookie;
310 1.1 bsh int i, s, stat, c = -1;
311 1.1 bsh
312 1.1 bsh s = spltty();
313 1.1 bsh
314 1.1 bsh if (sc->polling){
315 1.1 bsh stat = sc->poll_stat;
316 1.1 bsh c = sc->poll_data;
317 1.1 bsh sc->poll_data = -1;
318 1.1 bsh sc->poll_stat = -1;
319 1.5 bsh if (stat >= 0 &&
320 1.5 bsh (stat & (KBDSTAT_RXF|KBDSTAT_STP)) == KBDSTAT_RXF) {
321 1.1 bsh splx(s);
322 1.1 bsh return c;
323 1.1 bsh }
324 1.1 bsh }
325 1.1 bsh
326 1.1 bsh /* if 1 port read takes 1us (?), this polls for 100ms */
327 1.1 bsh for (i = 100000; i; i--) {
328 1.1 bsh stat = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_STAT);
329 1.5 bsh if ((stat & (KBDSTAT_RXF|KBDSTAT_STP)) == KBDSTAT_RXF) {
330 1.1 bsh KBD_DELAY;
331 1.1 bsh c = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_DATA);
332 1.1 bsh break;
333 1.1 bsh }
334 1.1 bsh }
335 1.1 bsh
336 1.1 bsh splx(s);
337 1.9 peter return c;
338 1.1 bsh }
339 1.1 bsh
340 1.1 bsh static int
341 1.5 bsh sackbc_send_cmd(void *cookie, pckbport_slot_t slot, u_char val)
342 1.1 bsh {
343 1.3 bjh21 struct sackbc_softc *sc = cookie;
344 1.3 bjh21
345 1.5 bsh if (!sackbc_wait_output(sc))
346 1.9 peter return 0;
347 1.5 bsh bus_space_write_1(sc->iot, sc->ioh, SACCKBD_DATA, val);
348 1.9 peter return 1;
349 1.1 bsh }
350 1.1 bsh
351 1.1 bsh
352 1.1 bsh /*
353 1.1 bsh * Glue functions for pckbd on sackbc.
354 1.1 bsh * These functions emulate those in dev/ic/pckbc.c.
355 1.1 bsh *
356 1.1 bsh */
357 1.1 bsh
358 1.1 bsh /*
359 1.1 bsh * switch scancode translation on / off
360 1.1 bsh * return nonzero on success
361 1.1 bsh */
362 1.3 bjh21 static int
363 1.3 bjh21 sackbc_xt_translation(void *self, pckbport_slot_t slot, int on)
364 1.1 bsh {
365 1.1 bsh /* KBD/Mouse controller doesn't have scancode translation */
366 1.1 bsh return !on;
367 1.1 bsh }
368 1.1 bsh
369 1.3 bjh21 static void
370 1.3 bjh21 sackbc_slot_enable(void *self, pckbport_slot_t slot, int on)
371 1.1 bsh {
372 1.1 bsh #if 0
373 1.1 bsh struct sackbc_softc *sc = (struct sackbc_softc *) self;
374 1.1 bsh int cmd;
375 1.1 bsh
376 1.1 bsh cmd = on ? KBC_KBDENABLE : KBC_KBDDISABLE;
377 1.5 bsh if (!sackbc_send_cmd(sc, cmd))
378 1.1 bsh printf("sackbc_slot_enable(%d) failed\n", on);
379 1.1 bsh #endif
380 1.1 bsh }
381 1.1 bsh
382 1.1 bsh
383 1.3 bjh21 static void
384 1.3 bjh21 sackbc_set_poll(void *self, pckbport_slot_t slot, int on)
385 1.1 bsh {
386 1.1 bsh struct sackbc_softc *sc = (struct sackbc_softc *)self;
387 1.1 bsh int s;
388 1.1 bsh
389 1.1 bsh s = spltty();
390 1.1 bsh
391 1.5 bsh if (sc->polling != on) {
392 1.1 bsh
393 1.1 bsh sc->polling = on;
394 1.1 bsh
395 1.5 bsh if (on) {
396 1.1 bsh sc->poll_data = sc->poll_stat = -1;
397 1.1 bsh sackbc_disable_intrhandler(sc);
398 1.9 peter } else {
399 1.1 bsh /*
400 1.1 bsh * If disabling polling on a device that's
401 1.1 bsh * been configured, make sure there are no
402 1.1 bsh * bytes left in the FIFO, holding up the
403 1.1 bsh * interrupt line. Otherwise we won't get any
404 1.1 bsh * further interrupts.
405 1.1 bsh */
406 1.1 bsh sackbc_rxint(sc);
407 1.6 bsh sackbc_intr_establish(sc, sc->slot);
408 1.1 bsh }
409 1.1 bsh }
410 1.1 bsh splx(s);
411 1.1 bsh }
412