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