pxa2xx_keypad.c 10.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
/*
 * 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.
 */

#include "hw.h"
#include "pxa.h"
#include "console.h"

/*
 * 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

P
Paul Brook 已提交
82
struct PXA2xxKeyPadState {
83 84
    qemu_irq    irq;
    struct  keymap *map;
85
    int         pressed_cnt;
86
    int         alt_code;
87 88 89 90 91 92

    uint32_t    kpc;
    uint32_t    kpdk;
    uint32_t    kprec;
    uint32_t    kpmk;
    uint32_t    kpas;
93
    uint32_t    kpasmkp[4];
94 95 96
    uint32_t    kpkdi;
};

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
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;
        }
    }
}

P
Paul Brook 已提交
115
static void pxa27x_keyboard_event (PXA2xxKeyPadState *kp, int keycode)
116
{
117 118
    int row, col, rel, assert_irq = 0;
    uint32_t val;
119

120 121 122 123 124
    if (keycode == 0xe0) {
        kp->alt_code = 1;
        return;
    }

125 126 127 128 129 130 131 132 133
    if(!(kp->kpc & KPC_ME)) /* skip if not enabled */
        return;

    if(kp->kpc & KPC_AS || kp->kpc & KPC_ASACT) {
        if(kp->kpc & KPC_AS)
            kp->kpc &= ~(KPC_AS);

        rel = (keycode & 0x80) ? 1 : 0; /* key release from qemu */
        keycode &= ~(0x80); /* strip qemu key release bit */
134 135 136 137
        if (kp->alt_code) {
            keycode |= 0x80;
            kp->alt_code = 0;
        }
138

139 140 141 142
        row = kp->map[keycode].row;
        col = kp->map[keycode].column;
        if(row == -1 || col == -1)
            return;
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164

        val = KPASMKPx_MKC(row, col);
        if (rel) {
            if (kp->kpasmkp[col / 2] & val) {
                kp->kpasmkp[col / 2] &= ~val;
                kp->pressed_cnt--;
                assert_irq = 1;
            }
        } else {
            if (!(kp->kpasmkp[col / 2] & val)) {
                kp->kpasmkp[col / 2] |= val;
                kp->pressed_cnt++;
                assert_irq = 1;
            }
        }
        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);
        }
165 166 167 168 169
        goto out;
    }
    return;

out:
170
    if (assert_irq && (kp->kpc & KPC_MIE)) {
171 172 173 174 175 176
        kp->kpc |= KPC_MI;
        qemu_irq_raise(kp->irq);
    }
    return;
}

A
Anthony Liguori 已提交
177
static uint32_t pxa2xx_keypad_read(void *opaque, target_phys_addr_t offset)
178
{
P
Paul Brook 已提交
179
    PXA2xxKeyPadState *s = (PXA2xxKeyPadState *) opaque;
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
    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;
        break;
    case KPDK:
        return s->kpdk;
        break;
    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;
        break;
    case KPMK:
        tmp = s->kpmk;
        if(tmp & KPMK_MKP)
            s->kpmk &= ~(KPMK_MKP);
        return tmp;
        break;
    case KPAS:
        return s->kpas;
        break;
    case KPASMKP0:
217
        return s->kpasmkp[0];
218 219
        break;
    case KPASMKP1:
220
        return s->kpasmkp[1];
221 222
        break;
    case KPASMKP2:
223
        return s->kpasmkp[2];
224 225
        break;
    case KPASMKP3:
226
        return s->kpasmkp[3];
227 228 229 230 231
        break;
    case KPKDI:
        return s->kpkdi;
        break;
    default:
P
Paul Brook 已提交
232
        hw_error("%s: Bad offset " REG_FMT "\n", __FUNCTION__, offset);
233 234 235 236 237 238
    }

    return 0;
}

static void pxa2xx_keypad_write(void *opaque,
A
Anthony Liguori 已提交
239
                target_phys_addr_t offset, uint32_t value)
240
{
P
Paul Brook 已提交
241
    PXA2xxKeyPadState *s = (PXA2xxKeyPadState *) opaque;
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259

    switch (offset) {
    case KPC:
        s->kpc = value;
        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:
260
        s->kpasmkp[0] = value;
261 262
        break;
    case KPASMKP1:
263
        s->kpasmkp[1] = value;
264 265
        break;
    case KPASMKP2:
266
        s->kpasmkp[2] = value;
267 268
        break;
    case KPASMKP3:
269
        s->kpasmkp[3] = value;
270 271 272 273 274 275
        break;
    case KPKDI:
        s->kpkdi = value;
        break;

    default:
P
Paul Brook 已提交
276
        hw_error("%s: Bad offset " REG_FMT "\n", __FUNCTION__, offset);
277 278 279
    }
}

280
static CPUReadMemoryFunc * const pxa2xx_keypad_readfn[] = {
281 282 283 284 285
    pxa2xx_keypad_read,
    pxa2xx_keypad_read,
    pxa2xx_keypad_read
};

286
static CPUWriteMemoryFunc * const pxa2xx_keypad_writefn[] = {
287 288 289 290 291 292 293
    pxa2xx_keypad_write,
    pxa2xx_keypad_write,
    pxa2xx_keypad_write
};

static void pxa2xx_keypad_save(QEMUFile *f, void *opaque)
{
P
Paul Brook 已提交
294
    PXA2xxKeyPadState *s = (PXA2xxKeyPadState *) opaque;
295 296 297 298 299 300

    qemu_put_be32s(f, &s->kpc);
    qemu_put_be32s(f, &s->kpdk);
    qemu_put_be32s(f, &s->kprec);
    qemu_put_be32s(f, &s->kpmk);
    qemu_put_be32s(f, &s->kpas);
301 302 303 304
    qemu_put_be32s(f, &s->kpasmkp[0]);
    qemu_put_be32s(f, &s->kpasmkp[1]);
    qemu_put_be32s(f, &s->kpasmkp[2]);
    qemu_put_be32s(f, &s->kpasmkp[3]);
305 306 307 308 309 310
    qemu_put_be32s(f, &s->kpkdi);

}

static int pxa2xx_keypad_load(QEMUFile *f, void *opaque, int version_id)
{
P
Paul Brook 已提交
311
    PXA2xxKeyPadState *s = (PXA2xxKeyPadState *) opaque;
312 313 314 315 316 317

    qemu_get_be32s(f, &s->kpc);
    qemu_get_be32s(f, &s->kpdk);
    qemu_get_be32s(f, &s->kprec);
    qemu_get_be32s(f, &s->kpmk);
    qemu_get_be32s(f, &s->kpas);
318 319 320 321
    qemu_get_be32s(f, &s->kpasmkp[0]);
    qemu_get_be32s(f, &s->kpasmkp[1]);
    qemu_get_be32s(f, &s->kpasmkp[2]);
    qemu_get_be32s(f, &s->kpasmkp[3]);
322 323 324 325 326
    qemu_get_be32s(f, &s->kpkdi);

    return 0;
}

A
Anthony Liguori 已提交
327
PXA2xxKeyPadState *pxa27x_keypad_init(target_phys_addr_t base,
328 329 330
        qemu_irq irq)
{
    int iomemtype;
P
Paul Brook 已提交
331
    PXA2xxKeyPadState *s;
332

P
Paul Brook 已提交
333
    s = (PXA2xxKeyPadState *) qemu_mallocz(sizeof(PXA2xxKeyPadState));
334 335
    s->irq = irq;

336
    iomemtype = cpu_register_io_memory(pxa2xx_keypad_readfn,
337
                    pxa2xx_keypad_writefn, s, DEVICE_NATIVE_ENDIAN);
338
    cpu_register_physical_memory(base, 0x00100000, iomemtype);
339

A
Alex Williamson 已提交
340
    register_savevm(NULL, "pxa2xx_keypad", 0, 0,
341 342 343 344 345
                    pxa2xx_keypad_save, pxa2xx_keypad_load, s);

    return s;
}

P
Paul Brook 已提交
346
void pxa27x_register_keypad(PXA2xxKeyPadState *kp, struct keymap *map,
347 348
        int size)
{
349
    if(!map || size < 0x80) {
350 351 352 353 354 355 356
        fprintf(stderr, "%s - No PXA keypad map defined\n", __FUNCTION__);
        exit(-1);
    }

    kp->map = map;
    qemu_add_kbd_event_handler((QEMUPutKBDEvent *) pxa27x_keyboard_event, kp);
}