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