2007-12-16 15:13:51 +03:00
|
|
|
/*
|
|
|
|
* Intel PXA27X Keypad Controller emulation.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2007 MontaVista Software, Inc
|
|
|
|
* Written by Armin Kuster <akuster@kama-aina.net>
|
|
|
|
* or <Akuster@mvista.com>
|
|
|
|
*
|
|
|
|
* This code is licensed under the GPLv2.
|
2012-01-13 20:44:23 +04:00
|
|
|
*
|
|
|
|
* Contributions after 2012-01-13 are licensed under the terms of the
|
|
|
|
* GNU GPL, version 2 or (at your option) any later version.
|
2007-12-16 15:13:51 +03:00
|
|
|
*/
|
|
|
|
|
2016-01-26 21:17:05 +03:00
|
|
|
#include "qemu/osdep.h"
|
2020-06-02 15:44:34 +03:00
|
|
|
#include "qemu/log.h"
|
2019-08-12 08:23:42 +03:00
|
|
|
#include "hw/irq.h"
|
2019-08-12 08:23:45 +03:00
|
|
|
#include "migration/vmstate.h"
|
2013-02-05 20:06:20 +04:00
|
|
|
#include "hw/arm/pxa.h"
|
2012-11-28 15:06:30 +04:00
|
|
|
#include "ui/console.h"
|
2007-12-16 15:13:51 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Keypad
|
|
|
|
*/
|
|
|
|
#define KPC 0x00 /* Keypad Interface Control register */
|
|
|
|
#define KPDK 0x08 /* Keypad Interface Direct Key register */
|
|
|
|
#define KPREC 0x10 /* Keypad Interface Rotary Encoder register */
|
|
|
|
#define KPMK 0x18 /* Keypad Interface Matrix Key register */
|
|
|
|
#define KPAS 0x20 /* Keypad Interface Automatic Scan register */
|
|
|
|
#define KPASMKP0 0x28 /* Keypad Interface Automatic Scan Multiple
|
|
|
|
Key Presser register 0 */
|
|
|
|
#define KPASMKP1 0x30 /* Keypad Interface Automatic Scan Multiple
|
|
|
|
Key Presser register 1 */
|
|
|
|
#define KPASMKP2 0x38 /* Keypad Interface Automatic Scan Multiple
|
|
|
|
Key Presser register 2 */
|
|
|
|
#define KPASMKP3 0x40 /* Keypad Interface Automatic Scan Multiple
|
|
|
|
Key Presser register 3 */
|
|
|
|
#define KPKDI 0x48 /* Keypad Interface Key Debounce Interval
|
|
|
|
register */
|
|
|
|
|
|
|
|
/* Keypad defines */
|
|
|
|
#define KPC_AS (0x1 << 30) /* Automatic Scan bit */
|
|
|
|
#define KPC_ASACT (0x1 << 29) /* Automatic Scan on Activity */
|
|
|
|
#define KPC_MI (0x1 << 22) /* Matrix interrupt bit */
|
|
|
|
#define KPC_IMKP (0x1 << 21) /* Ignore Multiple Key Press */
|
|
|
|
#define KPC_MS7 (0x1 << 20) /* Matrix scan line 7 */
|
|
|
|
#define KPC_MS6 (0x1 << 19) /* Matrix scan line 6 */
|
|
|
|
#define KPC_MS5 (0x1 << 18) /* Matrix scan line 5 */
|
|
|
|
#define KPC_MS4 (0x1 << 17) /* Matrix scan line 4 */
|
|
|
|
#define KPC_MS3 (0x1 << 16) /* Matrix scan line 3 */
|
|
|
|
#define KPC_MS2 (0x1 << 15) /* Matrix scan line 2 */
|
|
|
|
#define KPC_MS1 (0x1 << 14) /* Matrix scan line 1 */
|
|
|
|
#define KPC_MS0 (0x1 << 13) /* Matrix scan line 0 */
|
|
|
|
#define KPC_ME (0x1 << 12) /* Matrix Keypad Enable */
|
|
|
|
#define KPC_MIE (0x1 << 11) /* Matrix Interrupt Enable */
|
|
|
|
#define KPC_DK_DEB_SEL (0x1 << 9) /* Direct Keypad Debounce Select */
|
|
|
|
#define KPC_DI (0x1 << 5) /* Direct key interrupt bit */
|
|
|
|
#define KPC_RE_ZERO_DEB (0x1 << 4) /* Rotary Encoder Zero Debounce */
|
|
|
|
#define KPC_REE1 (0x1 << 3) /* Rotary Encoder1 Enable */
|
|
|
|
#define KPC_REE0 (0x1 << 2) /* Rotary Encoder0 Enable */
|
|
|
|
#define KPC_DE (0x1 << 1) /* Direct Keypad Enable */
|
|
|
|
#define KPC_DIE (0x1 << 0) /* Direct Keypad interrupt Enable */
|
|
|
|
|
|
|
|
#define KPDK_DKP (0x1 << 31)
|
|
|
|
#define KPDK_DK7 (0x1 << 7)
|
|
|
|
#define KPDK_DK6 (0x1 << 6)
|
|
|
|
#define KPDK_DK5 (0x1 << 5)
|
|
|
|
#define KPDK_DK4 (0x1 << 4)
|
|
|
|
#define KPDK_DK3 (0x1 << 3)
|
|
|
|
#define KPDK_DK2 (0x1 << 2)
|
|
|
|
#define KPDK_DK1 (0x1 << 1)
|
|
|
|
#define KPDK_DK0 (0x1 << 0)
|
|
|
|
|
|
|
|
#define KPREC_OF1 (0x1 << 31)
|
|
|
|
#define KPREC_UF1 (0x1 << 30)
|
|
|
|
#define KPREC_OF0 (0x1 << 15)
|
|
|
|
#define KPREC_UF0 (0x1 << 14)
|
|
|
|
|
|
|
|
#define KPMK_MKP (0x1 << 31)
|
|
|
|
#define KPAS_SO (0x1 << 31)
|
|
|
|
#define KPASMKPx_SO (0x1 << 31)
|
|
|
|
|
|
|
|
|
|
|
|
#define KPASMKPx_MKC(row, col) (1 << (row + 16 * (col % 2)))
|
|
|
|
|
|
|
|
#define PXAKBD_MAXROW 8
|
|
|
|
#define PXAKBD_MAXCOL 8
|
|
|
|
|
2009-05-10 04:44:56 +04:00
|
|
|
struct PXA2xxKeyPadState {
|
2011-10-30 17:50:15 +04:00
|
|
|
MemoryRegion iomem;
|
2007-12-16 15:13:51 +03:00
|
|
|
qemu_irq irq;
|
2013-12-22 18:22:57 +04:00
|
|
|
const struct keymap *map;
|
2011-02-15 16:27:28 +03:00
|
|
|
int pressed_cnt;
|
2011-02-15 16:27:29 +03:00
|
|
|
int alt_code;
|
2007-12-16 15:13:51 +03:00
|
|
|
|
|
|
|
uint32_t kpc;
|
|
|
|
uint32_t kpdk;
|
|
|
|
uint32_t kprec;
|
|
|
|
uint32_t kpmk;
|
|
|
|
uint32_t kpas;
|
2011-02-15 16:27:28 +03:00
|
|
|
uint32_t kpasmkp[4];
|
2007-12-16 15:13:51 +03:00
|
|
|
uint32_t kpkdi;
|
|
|
|
};
|
|
|
|
|
2011-02-15 16:27:28 +03:00
|
|
|
static void pxa27x_keypad_find_pressed_key(PXA2xxKeyPadState *kp, int *row, int *col)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
*col = i * 2;
|
|
|
|
for (*row = 0; *row < 8; (*row)++) {
|
|
|
|
if (kp->kpasmkp[i] & (1 << *row))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
*col = i * 2 + 1;
|
|
|
|
for (*row = 0; *row < 8; (*row)++) {
|
|
|
|
if (kp->kpasmkp[i] & (1 << (*row + 16)))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-10 04:44:56 +04:00
|
|
|
static void pxa27x_keyboard_event (PXA2xxKeyPadState *kp, int keycode)
|
2007-12-16 15:13:51 +03:00
|
|
|
{
|
2011-02-15 16:27:28 +03:00
|
|
|
int row, col, rel, assert_irq = 0;
|
|
|
|
uint32_t val;
|
2007-12-16 15:13:51 +03:00
|
|
|
|
2011-02-15 16:27:29 +03:00
|
|
|
if (keycode == 0xe0) {
|
|
|
|
kp->alt_code = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-12-16 15:13:51 +03:00
|
|
|
if(!(kp->kpc & KPC_ME)) /* skip if not enabled */
|
|
|
|
return;
|
|
|
|
|
2012-01-12 23:30:34 +04:00
|
|
|
rel = (keycode & 0x80) ? 1 : 0; /* key release from qemu */
|
|
|
|
keycode &= ~0x80; /* strip qemu key release bit */
|
|
|
|
if (kp->alt_code) {
|
|
|
|
keycode |= 0x80;
|
|
|
|
kp->alt_code = 0;
|
|
|
|
}
|
2011-02-15 16:27:28 +03:00
|
|
|
|
2012-01-12 23:30:34 +04:00
|
|
|
row = kp->map[keycode].row;
|
|
|
|
col = kp->map[keycode].column;
|
|
|
|
if (row == -1 || col == -1) {
|
|
|
|
return;
|
|
|
|
}
|
2011-02-15 16:27:28 +03:00
|
|
|
|
2012-01-12 23:30:34 +04:00
|
|
|
val = KPASMKPx_MKC(row, col);
|
|
|
|
if (rel) {
|
|
|
|
if (kp->kpasmkp[col / 2] & val) {
|
|
|
|
kp->kpasmkp[col / 2] &= ~val;
|
|
|
|
kp->pressed_cnt--;
|
|
|
|
assert_irq = 1;
|
2011-02-15 16:27:28 +03:00
|
|
|
}
|
2012-01-12 23:30:34 +04:00
|
|
|
} else {
|
|
|
|
if (!(kp->kpasmkp[col / 2] & val)) {
|
|
|
|
kp->kpasmkp[col / 2] |= val;
|
|
|
|
kp->pressed_cnt++;
|
|
|
|
assert_irq = 1;
|
2011-02-15 16:27:28 +03:00
|
|
|
}
|
2007-12-16 15:13:51 +03:00
|
|
|
}
|
2012-01-12 23:30:34 +04:00
|
|
|
kp->kpas = ((kp->pressed_cnt & 0x1f) << 26) | (0xf << 4) | 0xf;
|
|
|
|
if (kp->pressed_cnt == 1) {
|
|
|
|
kp->kpas &= ~((0xf << 4) | 0xf);
|
|
|
|
if (rel) {
|
|
|
|
pxa27x_keypad_find_pressed_key(kp, &row, &col);
|
|
|
|
}
|
|
|
|
kp->kpas |= ((row & 0xf) << 4) | (col & 0xf);
|
|
|
|
}
|
|
|
|
|
2012-01-17 05:14:42 +04:00
|
|
|
if (!(kp->kpc & (KPC_AS | KPC_ASACT)))
|
2012-01-12 23:30:34 +04:00
|
|
|
assert_irq = 0;
|
2007-12-16 15:13:51 +03:00
|
|
|
|
2011-02-15 16:27:28 +03:00
|
|
|
if (assert_irq && (kp->kpc & KPC_MIE)) {
|
2007-12-16 15:13:51 +03:00
|
|
|
kp->kpc |= KPC_MI;
|
|
|
|
qemu_irq_raise(kp->irq);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-23 14:30:10 +04:00
|
|
|
static uint64_t pxa2xx_keypad_read(void *opaque, hwaddr offset,
|
2011-10-30 17:50:15 +04:00
|
|
|
unsigned size)
|
2007-12-16 15:13:51 +03:00
|
|
|
{
|
2009-05-10 04:44:56 +04:00
|
|
|
PXA2xxKeyPadState *s = (PXA2xxKeyPadState *) opaque;
|
2007-12-16 15:13:51 +03:00
|
|
|
uint32_t tmp;
|
|
|
|
|
|
|
|
switch (offset) {
|
|
|
|
case KPC:
|
|
|
|
tmp = s->kpc;
|
|
|
|
if(tmp & KPC_MI)
|
|
|
|
s->kpc &= ~(KPC_MI);
|
|
|
|
if(tmp & KPC_DI)
|
|
|
|
s->kpc &= ~(KPC_DI);
|
|
|
|
qemu_irq_lower(s->irq);
|
|
|
|
return tmp;
|
|
|
|
case KPDK:
|
|
|
|
return s->kpdk;
|
|
|
|
case KPREC:
|
|
|
|
tmp = s->kprec;
|
|
|
|
if(tmp & KPREC_OF1)
|
|
|
|
s->kprec &= ~(KPREC_OF1);
|
|
|
|
if(tmp & KPREC_UF1)
|
|
|
|
s->kprec &= ~(KPREC_UF1);
|
|
|
|
if(tmp & KPREC_OF0)
|
|
|
|
s->kprec &= ~(KPREC_OF0);
|
|
|
|
if(tmp & KPREC_UF0)
|
|
|
|
s->kprec &= ~(KPREC_UF0);
|
|
|
|
return tmp;
|
|
|
|
case KPMK:
|
|
|
|
tmp = s->kpmk;
|
|
|
|
if(tmp & KPMK_MKP)
|
|
|
|
s->kpmk &= ~(KPMK_MKP);
|
|
|
|
return tmp;
|
|
|
|
case KPAS:
|
|
|
|
return s->kpas;
|
|
|
|
case KPASMKP0:
|
2011-02-15 16:27:28 +03:00
|
|
|
return s->kpasmkp[0];
|
2007-12-16 15:13:51 +03:00
|
|
|
case KPASMKP1:
|
2011-02-15 16:27:28 +03:00
|
|
|
return s->kpasmkp[1];
|
2007-12-16 15:13:51 +03:00
|
|
|
case KPASMKP2:
|
2011-02-15 16:27:28 +03:00
|
|
|
return s->kpasmkp[2];
|
2007-12-16 15:13:51 +03:00
|
|
|
case KPASMKP3:
|
2011-02-15 16:27:28 +03:00
|
|
|
return s->kpasmkp[3];
|
2007-12-16 15:13:51 +03:00
|
|
|
case KPKDI:
|
|
|
|
return s->kpkdi;
|
|
|
|
default:
|
2020-06-02 15:44:34 +03:00
|
|
|
qemu_log_mask(LOG_GUEST_ERROR,
|
|
|
|
"%s: Bad read offset 0x%"HWADDR_PRIx"\n",
|
|
|
|
__func__, offset);
|
2007-12-16 15:13:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-23 14:30:10 +04:00
|
|
|
static void pxa2xx_keypad_write(void *opaque, hwaddr offset,
|
2011-10-30 17:50:15 +04:00
|
|
|
uint64_t value, unsigned size)
|
2007-12-16 15:13:51 +03:00
|
|
|
{
|
2009-05-10 04:44:56 +04:00
|
|
|
PXA2xxKeyPadState *s = (PXA2xxKeyPadState *) opaque;
|
2007-12-16 15:13:51 +03:00
|
|
|
|
|
|
|
switch (offset) {
|
|
|
|
case KPC:
|
|
|
|
s->kpc = value;
|
2012-01-12 23:30:34 +04:00
|
|
|
if (s->kpc & KPC_AS) {
|
|
|
|
s->kpc &= ~(KPC_AS);
|
|
|
|
}
|
2007-12-16 15:13:51 +03:00
|
|
|
break;
|
|
|
|
case KPDK:
|
|
|
|
s->kpdk = value;
|
|
|
|
break;
|
|
|
|
case KPREC:
|
|
|
|
s->kprec = value;
|
|
|
|
break;
|
|
|
|
case KPMK:
|
|
|
|
s->kpmk = value;
|
|
|
|
break;
|
|
|
|
case KPAS:
|
|
|
|
s->kpas = value;
|
|
|
|
break;
|
|
|
|
case KPASMKP0:
|
2011-02-15 16:27:28 +03:00
|
|
|
s->kpasmkp[0] = value;
|
2007-12-16 15:13:51 +03:00
|
|
|
break;
|
|
|
|
case KPASMKP1:
|
2011-02-15 16:27:28 +03:00
|
|
|
s->kpasmkp[1] = value;
|
2007-12-16 15:13:51 +03:00
|
|
|
break;
|
|
|
|
case KPASMKP2:
|
2011-02-15 16:27:28 +03:00
|
|
|
s->kpasmkp[2] = value;
|
2007-12-16 15:13:51 +03:00
|
|
|
break;
|
|
|
|
case KPASMKP3:
|
2011-02-15 16:27:28 +03:00
|
|
|
s->kpasmkp[3] = value;
|
2007-12-16 15:13:51 +03:00
|
|
|
break;
|
|
|
|
case KPKDI:
|
|
|
|
s->kpkdi = value;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2020-06-02 15:44:34 +03:00
|
|
|
qemu_log_mask(LOG_GUEST_ERROR,
|
|
|
|
"%s: Bad write offset 0x%"HWADDR_PRIx"\n",
|
|
|
|
__func__, offset);
|
2007-12-16 15:13:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-30 17:50:15 +04:00
|
|
|
static const MemoryRegionOps pxa2xx_keypad_ops = {
|
|
|
|
.read = pxa2xx_keypad_read,
|
|
|
|
.write = pxa2xx_keypad_write,
|
|
|
|
.endianness = DEVICE_NATIVE_ENDIAN,
|
2007-12-16 15:13:51 +03:00
|
|
|
};
|
|
|
|
|
2010-12-02 03:06:08 +03:00
|
|
|
static const VMStateDescription vmstate_pxa2xx_keypad = {
|
|
|
|
.name = "pxa2xx_keypad",
|
|
|
|
.version_id = 0,
|
|
|
|
.minimum_version_id = 0,
|
2014-05-13 19:09:35 +04:00
|
|
|
.fields = (VMStateField[]) {
|
2010-12-02 03:06:08 +03:00
|
|
|
VMSTATE_UINT32(kpc, PXA2xxKeyPadState),
|
|
|
|
VMSTATE_UINT32(kpdk, PXA2xxKeyPadState),
|
|
|
|
VMSTATE_UINT32(kprec, PXA2xxKeyPadState),
|
|
|
|
VMSTATE_UINT32(kpmk, PXA2xxKeyPadState),
|
|
|
|
VMSTATE_UINT32(kpas, PXA2xxKeyPadState),
|
|
|
|
VMSTATE_UINT32_ARRAY(kpasmkp, PXA2xxKeyPadState, 4),
|
|
|
|
VMSTATE_UINT32(kpkdi, PXA2xxKeyPadState),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
2007-12-16 15:13:51 +03:00
|
|
|
|
2011-10-30 17:50:15 +04:00
|
|
|
PXA2xxKeyPadState *pxa27x_keypad_init(MemoryRegion *sysmem,
|
2012-10-23 14:30:10 +04:00
|
|
|
hwaddr base,
|
2011-10-30 17:50:15 +04:00
|
|
|
qemu_irq irq)
|
2007-12-16 15:13:51 +03:00
|
|
|
{
|
2009-05-10 04:44:56 +04:00
|
|
|
PXA2xxKeyPadState *s;
|
2007-12-16 15:13:51 +03:00
|
|
|
|
2022-03-15 17:41:56 +03:00
|
|
|
s = g_new0(PXA2xxKeyPadState, 1);
|
2007-12-16 15:13:51 +03:00
|
|
|
s->irq = irq;
|
|
|
|
|
2013-06-06 13:41:28 +04:00
|
|
|
memory_region_init_io(&s->iomem, NULL, &pxa2xx_keypad_ops, s,
|
2011-10-30 17:50:15 +04:00
|
|
|
"pxa2xx-keypad", 0x00100000);
|
|
|
|
memory_region_add_subregion(sysmem, base, &s->iomem);
|
2007-12-16 15:13:51 +03:00
|
|
|
|
2010-12-02 03:06:08 +03:00
|
|
|
vmstate_register(NULL, 0, &vmstate_pxa2xx_keypad, s);
|
2007-12-16 15:13:51 +03:00
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2013-12-22 18:22:57 +04:00
|
|
|
void pxa27x_register_keypad(PXA2xxKeyPadState *kp,
|
|
|
|
const struct keymap *map, int size)
|
2007-12-16 15:13:51 +03:00
|
|
|
{
|
2007-12-16 15:19:43 +03:00
|
|
|
if(!map || size < 0x80) {
|
2017-11-09 01:56:31 +03:00
|
|
|
fprintf(stderr, "%s - No PXA keypad map defined\n", __func__);
|
2007-12-16 15:13:51 +03:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
kp->map = map;
|
|
|
|
qemu_add_kbd_event_handler((QEMUPutKBDEvent *) pxa27x_keyboard_event, kp);
|
|
|
|
}
|