xref: /f-stack/freebsd/arm/versatile/pl050.c (revision 22ce4aff)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2012 Oleksandr Tymoshenko <[email protected]>
5  * All rights reserved.
6  *
7  * Based on dev/usb/input/ukbd.c
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/bus.h>
37 #include <sys/kernel.h>
38 #include <sys/lock.h>
39 #include <sys/module.h>
40 #include <sys/malloc.h>
41 #include <sys/mutex.h>
42 #include <sys/rman.h>
43 #include <sys/proc.h>
44 #include <sys/sched.h>
45 #include <sys/kdb.h>
46 
47 #include <machine/bus.h>
48 #include <machine/cpu.h>
49 #include <machine/intr.h>
50 
51 #include <dev/ofw/openfirm.h>
52 #include <dev/ofw/ofw_bus.h>
53 #include <dev/ofw/ofw_bus_subr.h>
54 
55 #include <sys/ioccom.h>
56 #include <sys/filio.h>
57 #include <sys/kbio.h>
58 
59 #include <dev/kbd/kbdreg.h>
60 
61 #include <machine/bus.h>
62 
63 #include <dev/kbd/kbdtables.h>
64 
65 #define	KMI_LOCK()	mtx_lock(&Giant)
66 #define	KMI_UNLOCK()	mtx_unlock(&Giant)
67 
68 #ifdef	INVARIANTS
69 /*
70  * Assert that the lock is held in all contexts
71  * where the code can be executed.
72  */
73 #define	KMI_LOCK_ASSERT()	mtx_assert(&Giant, MA_OWNED)
74 /*
75  * Assert that the lock is held in the contexts
76  * where it really has to be so.
77  */
78 #define	KMI_CTX_LOCK_ASSERT()			 	\
79 	do {						\
80 		if (!kdb_active && !KERNEL_PANICKED())	\
81 			mtx_assert(&Giant, MA_OWNED);	\
82 	} while (0)
83 #else
84 #define KMI_LOCK_ASSERT()	(void)0
85 #define KMI_CTX_LOCK_ASSERT()	(void)0
86 #endif
87 
88 #define	KMICR		0x00
89 #define		KMICR_TYPE_NONPS2	(1 << 5)
90 #define		KMICR_RXINTREN		(1 << 4)
91 #define		KMICR_TXINTREN		(1 << 3)
92 #define		KMICR_EN		(1 << 2)
93 #define		KMICR_FKMID		(1 << 1)
94 #define		KMICR_FKMIC		(1 << 0)
95 #define	KMISTAT		0x04
96 #define		KMISTAT_TXEMPTY		(1 << 6)
97 #define		KMISTAT_TXBUSY		(1 << 5)
98 #define		KMISTAT_RXFULL		(1 << 4)
99 #define		KMISTAT_RXBUSY		(1 << 3)
100 #define		KMISTAT_RXPARITY	(1 << 2)
101 #define		KMISTAT_KMIC		(1 << 1)
102 #define		KMISTAT_KMID		(1 << 0)
103 #define	KMIDATA		0x08
104 #define	KMICLKDIV	0x0C
105 #define	KMIIR		0x10
106 #define		KMIIR_TXINTR		(1 << 1)
107 #define		KMIIR_RXINTR		(1 << 0)
108 
109 #define	KMI_DRIVER_NAME          "kmi"
110 #define	KMI_NFKEY        (sizeof(fkey_tab)/sizeof(fkey_tab[0]))	/* units */
111 
112 #define	SET_SCANCODE_SET	0xf0
113 
114 struct kmi_softc {
115 	device_t sc_dev;
116 	keyboard_t sc_kbd;
117 	keymap_t sc_keymap;
118 	accentmap_t sc_accmap;
119 	fkeytab_t sc_fkeymap[KMI_NFKEY];
120 
121 	struct resource*	sc_mem_res;
122 	struct resource*	sc_irq_res;
123 	void*			sc_intr_hl;
124 
125 	int			sc_mode;		/* input mode (K_XLATE,K_RAW,K_CODE) */
126 	int			sc_state;		/* shift/lock key state */
127 	int			sc_accents;		/* accent key index (> 0) */
128 	uint32_t		sc_flags;		/* flags */
129 #define	KMI_FLAG_COMPOSE	0x00000001
130 #define	KMI_FLAG_POLLING	0x00000002
131 
132 	struct			thread *sc_poll_thread;
133 };
134 
135 /* Read/Write macros for Timer used as timecounter */
136 #define pl050_kmi_read_4(sc, reg)		\
137 	bus_read_4((sc)->sc_mem_res, (reg))
138 
139 #define pl050_kmi_write_4(sc, reg, val)	\
140 	bus_write_4((sc)->sc_mem_res, (reg), (val))
141 
142 /* prototypes */
143 static void	kmi_set_leds(struct kmi_softc *, uint8_t);
144 static int	kmi_set_typematic(keyboard_t *, int);
145 static uint32_t	kmi_read_char(keyboard_t *, int);
146 static void	kmi_clear_state(keyboard_t *);
147 static int	kmi_ioctl(keyboard_t *, u_long, caddr_t);
148 static int	kmi_enable(keyboard_t *);
149 static int	kmi_disable(keyboard_t *);
150 
151 static int	kmi_attached = 0;
152 
153 /* early keyboard probe, not supported */
154 static int
kmi_configure(int flags)155 kmi_configure(int flags)
156 {
157 	return (0);
158 }
159 
160 /* detect a keyboard, not used */
161 static int
kmi_probe(int unit,void * arg,int flags)162 kmi_probe(int unit, void *arg, int flags)
163 {
164 	return (ENXIO);
165 }
166 
167 /* reset and initialize the device, not used */
168 static int
kmi_init(int unit,keyboard_t ** kbdp,void * arg,int flags)169 kmi_init(int unit, keyboard_t **kbdp, void *arg, int flags)
170 {
171 	return (ENXIO);
172 }
173 
174 /* test the interface to the device, not used */
175 static int
kmi_test_if(keyboard_t * kbd)176 kmi_test_if(keyboard_t *kbd)
177 {
178 	return (0);
179 }
180 
181 /* finish using this keyboard, not used */
182 static int
kmi_term(keyboard_t * kbd)183 kmi_term(keyboard_t *kbd)
184 {
185 	return (ENXIO);
186 }
187 
188 /* keyboard interrupt routine, not used */
189 static int
kmi_intr(keyboard_t * kbd,void * arg)190 kmi_intr(keyboard_t *kbd, void *arg)
191 {
192 
193 	return (0);
194 }
195 
196 /* lock the access to the keyboard, not used */
197 static int
kmi_lock(keyboard_t * kbd,int lock)198 kmi_lock(keyboard_t *kbd, int lock)
199 {
200 	return (1);
201 }
202 
203 /*
204  * Enable the access to the device; until this function is called,
205  * the client cannot read from the keyboard.
206  */
207 static int
kmi_enable(keyboard_t * kbd)208 kmi_enable(keyboard_t *kbd)
209 {
210 
211 	KMI_LOCK();
212 	KBD_ACTIVATE(kbd);
213 	KMI_UNLOCK();
214 
215 	return (0);
216 }
217 
218 /* disallow the access to the device */
219 static int
kmi_disable(keyboard_t * kbd)220 kmi_disable(keyboard_t *kbd)
221 {
222 
223 	KMI_LOCK();
224 	KBD_DEACTIVATE(kbd);
225 	KMI_UNLOCK();
226 
227 	return (0);
228 }
229 
230 /* check if data is waiting */
231 static int
kmi_check(keyboard_t * kbd)232 kmi_check(keyboard_t *kbd)
233 {
234 	struct kmi_softc *sc = kbd->kb_data;
235 	uint32_t reg;
236 
237 	KMI_CTX_LOCK_ASSERT();
238 
239 	if (!KBD_IS_ACTIVE(kbd))
240 		return (0);
241 
242 	reg = pl050_kmi_read_4(sc, KMIIR);
243 	return (reg & KMIIR_RXINTR);
244 }
245 
246 /* check if char is waiting */
247 static int
kmi_check_char_locked(keyboard_t * kbd)248 kmi_check_char_locked(keyboard_t *kbd)
249 {
250 	KMI_CTX_LOCK_ASSERT();
251 
252 	if (!KBD_IS_ACTIVE(kbd))
253 		return (0);
254 
255 	return (kmi_check(kbd));
256 }
257 
258 static int
kmi_check_char(keyboard_t * kbd)259 kmi_check_char(keyboard_t *kbd)
260 {
261 	int result;
262 
263 	KMI_LOCK();
264 	result = kmi_check_char_locked(kbd);
265 	KMI_UNLOCK();
266 
267 	return (result);
268 }
269 
270 /* read one byte from the keyboard if it's allowed */
271 /* Currently unused. */
272 static int
kmi_read(keyboard_t * kbd,int wait)273 kmi_read(keyboard_t *kbd, int wait)
274 {
275 	KMI_CTX_LOCK_ASSERT();
276 
277 	if (!KBD_IS_ACTIVE(kbd))
278 		return (-1);
279 
280 	++(kbd->kb_count);
281 	printf("Implement ME: %s\n", __func__);
282 	return (0);
283 }
284 
285 /* read char from the keyboard */
286 static uint32_t
kmi_read_char_locked(keyboard_t * kbd,int wait)287 kmi_read_char_locked(keyboard_t *kbd, int wait)
288 {
289 	struct kmi_softc *sc = kbd->kb_data;
290 	uint32_t reg, data;
291 
292 	KMI_CTX_LOCK_ASSERT();
293 
294 	if (!KBD_IS_ACTIVE(kbd))
295 		return (NOKEY);
296 
297 	reg = pl050_kmi_read_4(sc, KMIIR);
298 	if (reg & KMIIR_RXINTR) {
299 		data = pl050_kmi_read_4(sc, KMIDATA);
300 		return (data);
301 	}
302 
303 	++kbd->kb_count;
304 	return (NOKEY);
305 }
306 
307 /* Currently wait is always false. */
308 static uint32_t
kmi_read_char(keyboard_t * kbd,int wait)309 kmi_read_char(keyboard_t *kbd, int wait)
310 {
311 	uint32_t keycode;
312 
313 	KMI_LOCK();
314 	keycode = kmi_read_char_locked(kbd, wait);
315 	KMI_UNLOCK();
316 
317 	return (keycode);
318 }
319 
320 /* some useful control functions */
321 static int
kmi_ioctl_locked(keyboard_t * kbd,u_long cmd,caddr_t arg)322 kmi_ioctl_locked(keyboard_t *kbd, u_long cmd, caddr_t arg)
323 {
324 	struct kmi_softc *sc = kbd->kb_data;
325 	int i;
326 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
327     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
328 	int ival;
329 
330 #endif
331 
332 	KMI_LOCK_ASSERT();
333 
334 	switch (cmd) {
335 	case KDGKBMODE:		/* get keyboard mode */
336 		*(int *)arg = sc->sc_mode;
337 		break;
338 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
339     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
340 	case _IO('K', 7):
341 		ival = IOCPARM_IVAL(arg);
342 		arg = (caddr_t)&ival;
343 		/* FALLTHROUGH */
344 #endif
345 	case KDSKBMODE:		/* set keyboard mode */
346 		switch (*(int *)arg) {
347 		case K_XLATE:
348 			if (sc->sc_mode != K_XLATE) {
349 				/* make lock key state and LED state match */
350 				sc->sc_state &= ~LOCK_MASK;
351 				sc->sc_state |= KBD_LED_VAL(kbd);
352 			}
353 			/* FALLTHROUGH */
354 		case K_RAW:
355 		case K_CODE:
356 			if (sc->sc_mode != *(int *)arg) {
357 				if ((sc->sc_flags & KMI_FLAG_POLLING) == 0)
358 					kmi_clear_state(kbd);
359 				sc->sc_mode = *(int *)arg;
360 			}
361 			break;
362 		default:
363 			return (EINVAL);
364 		}
365 		break;
366 
367 	case KDGETLED:			/* get keyboard LED */
368 		*(int *)arg = KBD_LED_VAL(kbd);
369 		break;
370 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
371     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
372 	case _IO('K', 66):
373 		ival = IOCPARM_IVAL(arg);
374 		arg = (caddr_t)&ival;
375 		/* FALLTHROUGH */
376 #endif
377 	case KDSETLED:			/* set keyboard LED */
378 		/* NOTE: lock key state in "sc_state" won't be changed */
379 		if (*(int *)arg & ~LOCK_MASK)
380 			return (EINVAL);
381 
382 		i = *(int *)arg;
383 
384 		/* replace CAPS LED with ALTGR LED for ALTGR keyboards */
385 		if (sc->sc_mode == K_XLATE &&
386 		    kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
387 			if (i & ALKED)
388 				i |= CLKED;
389 			else
390 				i &= ~CLKED;
391 		}
392 		if (KBD_HAS_DEVICE(kbd))
393 			kmi_set_leds(sc, i);
394 
395 		KBD_LED_VAL(kbd) = *(int *)arg;
396 		break;
397 	case KDGKBSTATE:		/* get lock key state */
398 		*(int *)arg = sc->sc_state & LOCK_MASK;
399 		break;
400 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
401     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
402 	case _IO('K', 20):
403 		ival = IOCPARM_IVAL(arg);
404 		arg = (caddr_t)&ival;
405 		/* FALLTHROUGH */
406 #endif
407 	case KDSKBSTATE:		/* set lock key state */
408 		if (*(int *)arg & ~LOCK_MASK) {
409 			return (EINVAL);
410 		}
411 		sc->sc_state &= ~LOCK_MASK;
412 		sc->sc_state |= *(int *)arg;
413 
414 		/* set LEDs and quit */
415 		return (kmi_ioctl(kbd, KDSETLED, arg));
416 
417 	case KDSETREPEAT:		/* set keyboard repeat rate (new
418 					 * interface) */
419 		if (!KBD_HAS_DEVICE(kbd)) {
420 			return (0);
421 		}
422 		if (((int *)arg)[1] < 0) {
423 			return (EINVAL);
424 		}
425 		if (((int *)arg)[0] < 0) {
426 			return (EINVAL);
427 		}
428 		if (((int *)arg)[0] < 200)	/* fastest possible value */
429 			kbd->kb_delay1 = 200;
430 		else
431 			kbd->kb_delay1 = ((int *)arg)[0];
432 		kbd->kb_delay2 = ((int *)arg)[1];
433 		return (0);
434 
435 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
436     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
437 	case _IO('K', 67):
438 		ival = IOCPARM_IVAL(arg);
439 		arg = (caddr_t)&ival;
440 		/* FALLTHROUGH */
441 #endif
442 	case KDSETRAD:			/* set keyboard repeat rate (old
443 					 * interface) */
444 		return (kmi_set_typematic(kbd, *(int *)arg));
445 
446 	case PIO_KEYMAP:		/* set keyboard translation table */
447 	case OPIO_KEYMAP:		/* set keyboard translation table
448 					 * (compat) */
449 	case PIO_KEYMAPENT:		/* set keyboard translation table
450 					 * entry */
451 	case PIO_DEADKEYMAP:		/* set accent key translation table */
452 		sc->sc_accents = 0;
453 		/* FALLTHROUGH */
454 	default:
455 		return (genkbd_commonioctl(kbd, cmd, arg));
456 	}
457 
458 	return (0);
459 }
460 
461 static int
kmi_ioctl(keyboard_t * kbd,u_long cmd,caddr_t arg)462 kmi_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
463 {
464 	int result;
465 
466 	/*
467 	 * XXX KDGKBSTATE, KDSKBSTATE and KDSETLED can be called from any
468 	 * context where printf(9) can be called, which among other things
469 	 * includes interrupt filters and threads with any kinds of locks
470 	 * already held.  For this reason it would be dangerous to acquire
471 	 * the Giant here unconditionally.  On the other hand we have to
472 	 * have it to handle the ioctl.
473 	 * So we make our best effort to auto-detect whether we can grab
474 	 * the Giant or not.  Blame syscons(4) for this.
475 	 */
476 	switch (cmd) {
477 	case KDGKBSTATE:
478 	case KDSKBSTATE:
479 	case KDSETLED:
480 		if (!mtx_owned(&Giant) && !SCHEDULER_STOPPED())
481 			return (EDEADLK);	/* best I could come up with */
482 		/* FALLTHROUGH */
483 	default:
484 		KMI_LOCK();
485 		result = kmi_ioctl_locked(kbd, cmd, arg);
486 		KMI_UNLOCK();
487 		return (result);
488 	}
489 }
490 
491 /* clear the internal state of the keyboard */
492 static void
kmi_clear_state(keyboard_t * kbd)493 kmi_clear_state(keyboard_t *kbd)
494 {
495 	struct kmi_softc *sc = kbd->kb_data;
496 
497 	KMI_CTX_LOCK_ASSERT();
498 
499 	sc->sc_flags &= ~(KMI_FLAG_COMPOSE | KMI_FLAG_POLLING);
500 	sc->sc_state &= LOCK_MASK;	/* preserve locking key state */
501 	sc->sc_accents = 0;
502 }
503 
504 /* save the internal state, not used */
505 static int
kmi_get_state(keyboard_t * kbd,void * buf,size_t len)506 kmi_get_state(keyboard_t *kbd, void *buf, size_t len)
507 {
508 	return (len == 0) ? 1 : -1;
509 }
510 
511 /* set the internal state, not used */
512 static int
kmi_set_state(keyboard_t * kbd,void * buf,size_t len)513 kmi_set_state(keyboard_t *kbd, void *buf, size_t len)
514 {
515 	return (EINVAL);
516 }
517 
518 static int
kmi_poll(keyboard_t * kbd,int on)519 kmi_poll(keyboard_t *kbd, int on)
520 {
521 	struct kmi_softc *sc = kbd->kb_data;
522 
523 	KMI_LOCK();
524 	if (on) {
525 		sc->sc_flags |= KMI_FLAG_POLLING;
526 		sc->sc_poll_thread = curthread;
527 	} else {
528 		sc->sc_flags &= ~KMI_FLAG_POLLING;
529 	}
530 	KMI_UNLOCK();
531 
532 	return (0);
533 }
534 
535 /* local functions */
536 
537 static void
kmi_set_leds(struct kmi_softc * sc,uint8_t leds)538 kmi_set_leds(struct kmi_softc *sc, uint8_t leds)
539 {
540 
541 	KMI_LOCK_ASSERT();
542 
543 	/* start transfer, if not already started */
544 	printf("Implement me: %s\n", __func__);
545 }
546 
547 static int
kmi_set_typematic(keyboard_t * kbd,int code)548 kmi_set_typematic(keyboard_t *kbd, int code)
549 {
550 	static const int delays[] = {250, 500, 750, 1000};
551 	static const int rates[] = {34, 38, 42, 46, 50, 55, 59, 63,
552 		68, 76, 84, 92, 100, 110, 118, 126,
553 		136, 152, 168, 184, 200, 220, 236, 252,
554 	272, 304, 336, 368, 400, 440, 472, 504};
555 
556 	if (code & ~0x7f) {
557 		return (EINVAL);
558 	}
559 	kbd->kb_delay1 = delays[(code >> 5) & 3];
560 	kbd->kb_delay2 = rates[code & 0x1f];
561 	return (0);
562 }
563 
564 static keyboard_switch_t kmisw = {
565 	.probe = &kmi_probe,
566 	.init = &kmi_init,
567 	.term = &kmi_term,
568 	.intr = &kmi_intr,
569 	.test_if = &kmi_test_if,
570 	.enable = &kmi_enable,
571 	.disable = &kmi_disable,
572 	.read = &kmi_read,
573 	.check = &kmi_check,
574 	.read_char = &kmi_read_char,
575 	.check_char = &kmi_check_char,
576 	.ioctl = &kmi_ioctl,
577 	.lock = &kmi_lock,
578 	.clear_state = &kmi_clear_state,
579 	.get_state = &kmi_get_state,
580 	.set_state = &kmi_set_state,
581 	.poll = &kmi_poll,
582 };
583 
584 KEYBOARD_DRIVER(kmi, kmisw, kmi_configure);
585 
586 static void
pl050_kmi_intr(void * arg)587 pl050_kmi_intr(void *arg)
588 {
589 	struct kmi_softc *sc = arg;
590 	uint32_t c;
591 
592 	KMI_CTX_LOCK_ASSERT();
593 
594 	if ((sc->sc_flags & KMI_FLAG_POLLING) != 0)
595 		return;
596 
597 	if (KBD_IS_ACTIVE(&sc->sc_kbd) &&
598 	    KBD_IS_BUSY(&sc->sc_kbd)) {
599 		/* let the callback function process the input */
600 		(sc->sc_kbd.kb_callback.kc_func) (&sc->sc_kbd, KBDIO_KEYINPUT,
601 		    sc->sc_kbd.kb_callback.kc_arg);
602 	} else {
603 		/* read and discard the input, no one is waiting for it */
604 		do {
605 			c = kmi_read_char_locked(&sc->sc_kbd, 0);
606 		} while (c != NOKEY);
607 	}
608 
609 }
610 
611 static int
pl050_kmi_probe(device_t dev)612 pl050_kmi_probe(device_t dev)
613 {
614 
615 	if (!ofw_bus_status_okay(dev))
616 		return (ENXIO);
617 
618 	/*
619 	 * PL050 is plain PS2 port that pushes bytes to/from computer
620 	 * VersatilePB has two such ports and QEMU simulates keyboard
621 	 * connected to port #0 and mouse connected to port #1. This
622 	 * information can't be obtained from device tree so we just
623 	 * hardcode this knowledge here. We attach keyboard driver to
624 	 * port #0 and ignore port #1
625 	 */
626 	if (kmi_attached)
627 		return (ENXIO);
628 
629 	if (ofw_bus_is_compatible(dev, "arm,pl050")) {
630 		device_set_desc(dev, "PL050 Keyboard/Mouse Interface");
631 		return (BUS_PROBE_DEFAULT);
632 	}
633 
634 	return (ENXIO);
635 }
636 
637 static int
pl050_kmi_attach(device_t dev)638 pl050_kmi_attach(device_t dev)
639 {
640 	struct kmi_softc *sc = device_get_softc(dev);
641 	keyboard_t *kbd;
642 	int rid;
643 	int i;
644 	uint32_t ack;
645 
646 	sc->sc_dev = dev;
647 	kbd = &sc->sc_kbd;
648 	rid = 0;
649 
650 	sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
651 	if (sc->sc_mem_res == NULL) {
652 		device_printf(dev, "could not allocate memory resource\n");
653 		return (ENXIO);
654 	}
655 
656 	/* Request the IRQ resources */
657 	sc->sc_irq_res =  bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
658 	if (sc->sc_irq_res == NULL) {
659 		device_printf(dev, "Error: could not allocate irq resources\n");
660 		return (ENXIO);
661 	}
662 
663 	/* Setup and enable the timer */
664 	if (bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_CLK,
665 			NULL, pl050_kmi_intr, sc,
666 			&sc->sc_intr_hl) != 0) {
667 		bus_release_resource(dev, SYS_RES_IRQ, rid,
668 			sc->sc_irq_res);
669 		device_printf(dev, "Unable to setup the clock irq handler.\n");
670 		return (ENXIO);
671 	}
672 
673 	/* TODO: clock & divisor */
674 
675 	pl050_kmi_write_4(sc, KMICR, KMICR_EN);
676 
677 	pl050_kmi_write_4(sc, KMIDATA, SET_SCANCODE_SET);
678 	/* read out ACK */
679 	ack = pl050_kmi_read_4(sc, KMIDATA);
680 	/* Set Scan Code set 1 (XT) */
681 	pl050_kmi_write_4(sc, KMIDATA, 1);
682 	/* read out ACK */
683 	ack = pl050_kmi_read_4(sc, KMIDATA);
684 
685 	pl050_kmi_write_4(sc, KMICR, KMICR_EN | KMICR_RXINTREN);
686 
687 	kbd_init_struct(kbd, KMI_DRIVER_NAME, KB_OTHER,
688 			device_get_unit(dev), 0, 0, 0);
689 	kbd->kb_data = (void *)sc;
690 
691 	sc->sc_keymap = key_map;
692 	sc->sc_accmap = accent_map;
693 	for (i = 0; i < KMI_NFKEY; i++) {
694 		sc->sc_fkeymap[i] = fkey_tab[i];
695 	}
696 
697 	kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap,
698 	    sc->sc_fkeymap, KMI_NFKEY);
699 
700 	KBD_FOUND_DEVICE(kbd);
701 	kmi_clear_state(kbd);
702 	KBD_PROBE_DONE(kbd);
703 
704 	KBD_INIT_DONE(kbd);
705 
706 	if (kbd_register(kbd) < 0) {
707 		goto detach;
708 	}
709 	KBD_CONFIG_DONE(kbd);
710 
711 #ifdef KBD_INSTALL_CDEV
712 	if (kbd_attach(kbd)) {
713 		goto detach;
714 	}
715 #endif
716 
717 	if (bootverbose) {
718 		kbdd_diag(kbd, bootverbose);
719 	}
720 	kmi_attached = 1;
721 	return (0);
722 
723 detach:
724 	return (ENXIO);
725 
726 }
727 
728 static device_method_t pl050_kmi_methods[] = {
729 	DEVMETHOD(device_probe,		pl050_kmi_probe),
730 	DEVMETHOD(device_attach,	pl050_kmi_attach),
731 	{ 0, 0 }
732 };
733 
734 static driver_t pl050_kmi_driver = {
735 	"kmi",
736 	pl050_kmi_methods,
737 	sizeof(struct kmi_softc),
738 };
739 
740 static devclass_t pl050_kmi_devclass;
741 
742 DRIVER_MODULE(pl050_kmi, simplebus, pl050_kmi_driver, pl050_kmi_devclass, 0, 0);
743