2 * Intel PXA27X Keypad Controller emulation.
4 * Copyright (c) 2007 MontaVista Software, Inc
5 * Written by Armin Kuster <akuster@kama-aina.net>
6 * or <Akuster@mvista.com>
8 * This code is licensed under the GPLv2.
18 #define KPC 0x00 /* Keypad Interface Control register */
19 #define KPDK 0x08 /* Keypad Interface Direct Key register */
20 #define KPREC 0x10 /* Keypad Interface Rotary Encoder register */
21 #define KPMK 0x18 /* Keypad Interface Matrix Key register */
22 #define KPAS 0x20 /* Keypad Interface Automatic Scan register */
23 #define KPASMKP0 0x28 /* Keypad Interface Automatic Scan Multiple
24 Key Presser register 0 */
25 #define KPASMKP1 0x30 /* Keypad Interface Automatic Scan Multiple
26 Key Presser register 1 */
27 #define KPASMKP2 0x38 /* Keypad Interface Automatic Scan Multiple
28 Key Presser register 2 */
29 #define KPASMKP3 0x40 /* Keypad Interface Automatic Scan Multiple
30 Key Presser register 3 */
31 #define KPKDI 0x48 /* Keypad Interface Key Debounce Interval
35 #define KPC_AS (0x1 << 30) /* Automatic Scan bit */
36 #define KPC_ASACT (0x1 << 29) /* Automatic Scan on Activity */
37 #define KPC_MI (0x1 << 22) /* Matrix interrupt bit */
38 #define KPC_IMKP (0x1 << 21) /* Ignore Multiple Key Press */
39 #define KPC_MS7 (0x1 << 20) /* Matrix scan line 7 */
40 #define KPC_MS6 (0x1 << 19) /* Matrix scan line 6 */
41 #define KPC_MS5 (0x1 << 18) /* Matrix scan line 5 */
42 #define KPC_MS4 (0x1 << 17) /* Matrix scan line 4 */
43 #define KPC_MS3 (0x1 << 16) /* Matrix scan line 3 */
44 #define KPC_MS2 (0x1 << 15) /* Matrix scan line 2 */
45 #define KPC_MS1 (0x1 << 14) /* Matrix scan line 1 */
46 #define KPC_MS0 (0x1 << 13) /* Matrix scan line 0 */
47 #define KPC_ME (0x1 << 12) /* Matrix Keypad Enable */
48 #define KPC_MIE (0x1 << 11) /* Matrix Interrupt Enable */
49 #define KPC_DK_DEB_SEL (0x1 << 9) /* Direct Keypad Debounce Select */
50 #define KPC_DI (0x1 << 5) /* Direct key interrupt bit */
51 #define KPC_RE_ZERO_DEB (0x1 << 4) /* Rotary Encoder Zero Debounce */
52 #define KPC_REE1 (0x1 << 3) /* Rotary Encoder1 Enable */
53 #define KPC_REE0 (0x1 << 2) /* Rotary Encoder0 Enable */
54 #define KPC_DE (0x1 << 1) /* Direct Keypad Enable */
55 #define KPC_DIE (0x1 << 0) /* Direct Keypad interrupt Enable */
57 #define KPDK_DKP (0x1 << 31)
58 #define KPDK_DK7 (0x1 << 7)
59 #define KPDK_DK6 (0x1 << 6)
60 #define KPDK_DK5 (0x1 << 5)
61 #define KPDK_DK4 (0x1 << 4)
62 #define KPDK_DK3 (0x1 << 3)
63 #define KPDK_DK2 (0x1 << 2)
64 #define KPDK_DK1 (0x1 << 1)
65 #define KPDK_DK0 (0x1 << 0)
67 #define KPREC_OF1 (0x1 << 31)
68 #define KPREC_UF1 (0x1 << 30)
69 #define KPREC_OF0 (0x1 << 15)
70 #define KPREC_UF0 (0x1 << 14)
72 #define KPMK_MKP (0x1 << 31)
73 #define KPAS_SO (0x1 << 31)
74 #define KPASMKPx_SO (0x1 << 31)
77 #define KPASMKPx_MKC(row, col) (1 << (row + 16 * (col % 2)))
79 #define PXAKBD_MAXROW 8
80 #define PXAKBD_MAXCOL 8
82 struct pxa2xx_keypad_s
{
83 target_phys_addr_t base
;
99 static void pxa27x_keyboard_event (struct pxa2xx_keypad_s
*kp
, int keycode
)
103 if(!(kp
->kpc
& KPC_ME
)) /* skip if not enabled */
106 if(kp
->kpc
& KPC_AS
|| kp
->kpc
& KPC_ASACT
) {
108 kp
->kpc
&= ~(KPC_AS
);
110 rel
= (keycode
& 0x80) ? 1 : 0; /* key release from qemu */
111 keycode
&= ~(0x80); /* strip qemu key release bit */
112 row
= kp
->map
[keycode
].row
;
113 col
= kp
->map
[keycode
].column
;
114 if(row
== -1 || col
== -1)
120 kp
->kpasmkp0
= ~(0xffffffff);
122 kp
->kpasmkp0
|= KPASMKPx_MKC(row
,col
);
127 kp
->kpasmkp1
= ~(0xffffffff);
129 kp
->kpasmkp1
|= KPASMKPx_MKC(row
,col
);
134 kp
->kpasmkp2
= ~(0xffffffff);
136 kp
->kpasmkp2
|= KPASMKPx_MKC(row
,col
);
141 kp
->kpasmkp3
= ~(0xffffffff);
143 kp
->kpasmkp3
|= KPASMKPx_MKC(row
,col
);
151 if(kp
->kpc
& KPC_MIE
) {
153 qemu_irq_raise(kp
->irq
);
158 static uint32_t pxa2xx_keypad_read(void *opaque
, target_phys_addr_t offset
)
160 struct pxa2xx_keypad_s
*s
= (struct pxa2xx_keypad_s
*) opaque
;
171 qemu_irq_lower(s
->irq
);
180 s
->kprec
&= ~(KPREC_OF1
);
182 s
->kprec
&= ~(KPREC_UF1
);
184 s
->kprec
&= ~(KPREC_OF0
);
186 s
->kprec
&= ~(KPREC_UF0
);
192 s
->kpmk
&= ~(KPMK_MKP
);
214 cpu_abort(cpu_single_env
, "%s: Bad offset " REG_FMT
"\n",
215 __FUNCTION__
, offset
);
221 static void pxa2xx_keypad_write(void *opaque
,
222 target_phys_addr_t offset
, uint32_t value
)
224 struct pxa2xx_keypad_s
*s
= (struct pxa2xx_keypad_s
*) opaque
;
260 cpu_abort(cpu_single_env
, "%s: Bad offset " REG_FMT
"\n",
261 __FUNCTION__
, offset
);
265 static CPUReadMemoryFunc
*pxa2xx_keypad_readfn
[] = {
271 static CPUWriteMemoryFunc
*pxa2xx_keypad_writefn
[] = {
277 static void pxa2xx_keypad_save(QEMUFile
*f
, void *opaque
)
279 struct pxa2xx_keypad_s
*s
= (struct pxa2xx_keypad_s
*) opaque
;
281 qemu_put_be32s(f
, &s
->kpc
);
282 qemu_put_be32s(f
, &s
->kpdk
);
283 qemu_put_be32s(f
, &s
->kprec
);
284 qemu_put_be32s(f
, &s
->kpmk
);
285 qemu_put_be32s(f
, &s
->kpas
);
286 qemu_put_be32s(f
, &s
->kpasmkp0
);
287 qemu_put_be32s(f
, &s
->kpasmkp1
);
288 qemu_put_be32s(f
, &s
->kpasmkp2
);
289 qemu_put_be32s(f
, &s
->kpasmkp3
);
290 qemu_put_be32s(f
, &s
->kpkdi
);
294 static int pxa2xx_keypad_load(QEMUFile
*f
, void *opaque
, int version_id
)
296 struct pxa2xx_keypad_s
*s
= (struct pxa2xx_keypad_s
*) opaque
;
298 qemu_get_be32s(f
, &s
->kpc
);
299 qemu_get_be32s(f
, &s
->kpdk
);
300 qemu_get_be32s(f
, &s
->kprec
);
301 qemu_get_be32s(f
, &s
->kpmk
);
302 qemu_get_be32s(f
, &s
->kpas
);
303 qemu_get_be32s(f
, &s
->kpasmkp0
);
304 qemu_get_be32s(f
, &s
->kpasmkp1
);
305 qemu_get_be32s(f
, &s
->kpasmkp2
);
306 qemu_get_be32s(f
, &s
->kpasmkp3
);
307 qemu_get_be32s(f
, &s
->kpkdi
);
312 struct pxa2xx_keypad_s
*pxa27x_keypad_init(target_phys_addr_t base
,
316 struct pxa2xx_keypad_s
*s
;
318 s
= (struct pxa2xx_keypad_s
*) qemu_mallocz(sizeof(struct pxa2xx_keypad_s
));
322 iomemtype
= cpu_register_io_memory(0, pxa2xx_keypad_readfn
,
323 pxa2xx_keypad_writefn
, s
);
324 cpu_register_physical_memory(base
, 0x00100000, iomemtype
);
326 register_savevm("pxa2xx_keypad", 0, 0,
327 pxa2xx_keypad_save
, pxa2xx_keypad_load
, s
);
332 void pxa27x_register_keypad(struct pxa2xx_keypad_s
*kp
, struct keymap
*map
,
335 if(!map
|| size
< 0x80) {
336 fprintf(stderr
, "%s - No PXA keypad map defined\n", __FUNCTION__
);
341 qemu_add_kbd_event_handler((QEMUPutKBDEvent
*) pxa27x_keyboard_event
, kp
);