From patchwork Sat Jun 30 08:33:55 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Huth X-Patchwork-Id: 10498059 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id A045860230 for ; Sat, 30 Jun 2018 08:36:01 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8A1CC290E1 for ; Sat, 30 Jun 2018 08:36:01 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7E852290E7; Sat, 30 Jun 2018 08:36:01 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id B7089290E6 for ; Sat, 30 Jun 2018 08:36:00 +0000 (UTC) Received: from localhost ([::1]:46099 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fZBMO-0005Gm-0q for patchwork-qemu-devel@patchwork.kernel.org; Sat, 30 Jun 2018 04:36:00 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55596) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fZBKe-0003YQ-U3 for qemu-devel@nongnu.org; Sat, 30 Jun 2018 04:34:16 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fZBKb-0001N0-KT for qemu-devel@nongnu.org; Sat, 30 Jun 2018 04:34:12 -0400 Received: from mail-wr0-f195.google.com ([209.85.128.195]:47093) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fZBKb-0001LI-Au for qemu-devel@nongnu.org; Sat, 30 Jun 2018 04:34:09 -0400 Received: by mail-wr0-f195.google.com with SMTP id s11-v6so1614655wra.13 for ; Sat, 30 Jun 2018 01:34:09 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=+fWZNDdsK1ktEPtsnZ8f80E3G9YTuy/ZaHQFkpOe9vE=; b=L6Obwrlif4xSqE3iaDCli3e5E1z5VtCz45VCRPLbRxvB9CWXG9sclX42erG1/mapqu ovtmdJFNcWO+h93qw6IR8GgtzGh+LviZcdA1/hZnkouXPtB5z7RSBqU2F3qGyKL6R3yt U1rnJo9ZIp9OHlJD6QPkMi8RaXVp1MINdqWJ7YPi6U8FyUENgKg4TtzLyUckDVk+nZWJ ZfuiYBjk7iMq4sVTwB0KBjYjWbxx420FIslLdDh71FRAZf7eFgmqwsrgBIsH6UgnGVsf 2p/hrVSpa5WJJqVU+/DPkU8FCkTTkBYMoUxApBRZM1YS431Ca7Q9MQrkzZNS1c2mPtv+ aerQ== X-Gm-Message-State: APt69E1+DnsHxktPH3JbcejwB8G2161G1NXwouXBqhlfBbaoxpaZK1ef 2QqkeCkmy5gCt2EUMI2+3kqfxNqR X-Google-Smtp-Source: AAOMgpft3yeqZaFZ2CNYEZwuAjd+uUmvwYIovUz3AI6C+iLnalUgCjeNR5efgEdb/kiyNPQjs4EK5A== X-Received: by 2002:adf:e6d0:: with SMTP id y16-v6mr15185640wrm.35.1530347647818; Sat, 30 Jun 2018 01:34:07 -0700 (PDT) Received: from thl530.multi.box (pD9E83B8A.dip0.t-ipconnect.de. [217.232.59.138]) by smtp.gmail.com with ESMTPSA id y8-v6sm10968831wrs.0.2018.06.30.01.34.06 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sat, 30 Jun 2018 01:34:07 -0700 (PDT) From: Thomas Huth To: qemu-devel@nongnu.org Date: Sat, 30 Jun 2018 10:33:55 +0200 Message-Id: <20180630083357.23489-3-huth@tuxfamily.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180630083357.23489-1-huth@tuxfamily.org> References: <20180630083357.23489-1-huth@tuxfamily.org> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.85.128.195 Subject: [Qemu-devel] [PATCH v1 2/4] m68k: Add NeXTcube keyboard device X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Natalia Portillo , Laurent Vivier , Bryce Lanham Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP It is likely still quite incomplete (e.g. mouse and interrupts are not implemented yet), but it is good enough for keyboard input at the firmware monitor. This code has been taken from Bryce Lanham's GSoC 2011 NeXT branch at https://github.com/blanham/qemu-NeXT/blob/next-cube/hw/next-kbd.c and altered to fit the latest interface of the current QEMU (e.g. to use memory_region_init_io() instead of cpu_register_physical_memory()). Signed-off-by: Thomas Huth --- hw/m68k/Makefile.objs | 5 +- hw/m68k/next-kbd.c | 289 ++++++++++++++++++++++++++++++++++++ include/hw/m68k/next-cube.h | 3 + 3 files changed, 295 insertions(+), 2 deletions(-) create mode 100644 hw/m68k/next-kbd.c diff --git a/hw/m68k/Makefile.objs b/hw/m68k/Makefile.objs index d1f089c08a..5852fd2500 100644 --- a/hw/m68k/Makefile.objs +++ b/hw/m68k/Makefile.objs @@ -1,2 +1,3 @@ -obj-y += an5206.o mcf5208.o -obj-y += mcf5206.o mcf_intc.o +obj-$(CONFIG_COLDFIRE) += an5206.o mcf5208.o +obj-$(CONFIG_COLDFIRE) += mcf5206.o mcf_intc.o +obj-$(CONFIG_NEXTCUBE) += next-kbd.o diff --git a/hw/m68k/next-kbd.c b/hw/m68k/next-kbd.c new file mode 100644 index 0000000000..0c4655f586 --- /dev/null +++ b/hw/m68k/next-kbd.c @@ -0,0 +1,289 @@ +/* + * QEMU NeXT Keyboard/Mouse emulation + * + * Copyright (c) 2011 Bryce Lanham + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +/* + * This is admittedly hackish, but works well enough for basic input. Mouse + * support will be added once we can boot something that needs the mouse. + */ + +#include "qemu/osdep.h" +#include "exec/address-spaces.h" +#include "hw/hw.h" +#include "hw/m68k/next-cube.h" +#include "ui/console.h" +#include "sysemu/sysemu.h" + +/* debug NeXT keyboard */ +//#define DEBUG_KBD + +#ifdef DEBUG_KBD +#define DPRINTF(fmt, ...) do { printf("KBD: " fmt , ## __VA_ARGS__); } while (0) +#else +#define DPRINTF(fmt, ...) do { } while (0) +#endif + +/* following defintions from next68k netbsd */ +#define CSR_INT 0x00800000 +#define CSR_DATA 0x00400000 + +#define KD_KEYMASK 0x007f +#define KD_DIRECTION 0x0080 /* pressed or released */ +#define KD_CNTL 0x0100 +#define KD_LSHIFT 0x0200 +#define KD_RSHIFT 0x0400 +#define KD_LCOMM 0x0800 +#define KD_RCOMM 0x1000 +#define KD_LALT 0x2000 +#define KD_RALT 0x4000 +#define KD_VALID 0x8000 /* only set for scancode keys ? */ +#define KD_MODS 0x4f00 + +#define KBD_QUEUE_SIZE 256 + +typedef struct { + uint8_t data[KBD_QUEUE_SIZE]; + int rptr, wptr, count; +} KBDQueue; + + +typedef struct KBDState { + KBDQueue queue; + uint8_t blank; + uint16_t shift; +} KBDState; +KBDState *kbd_env; + +static void queue_code(void *opaque, int code); + +/* lots of magic numbers here */ +static uint32_t kbd_read_byte(void *opaque, hwaddr addr) +{ + switch (addr & 0x3) { + case 0x0: /* 0xe000 */ + return 0x80 | 0x20; + + case 0x1: /* 0xe001 */ + return 0x80 | 0x40 | 0x20 | 0x10; + + case 0x2: /* 0xe002 */ + // return 0x40|0x10|0x2|0x1; + /* returning 0x40 caused mach to hang */ + return 0x10 | 0x2 | 0x1; + + default: + DPRINTF("RB ADDR %x\n", addr); + } + + return 0; +} + +static uint32_t kbd_read_word(void *opaque, hwaddr addr) +{ + DPRINTF("RW ADDR %x\n", addr); + return 0; +} + +/* even more magic numbers */ +static uint32_t kbd_read_long(void *opaque, hwaddr addr) +{ + int key = 0; + KBDState *s = (KBDState *)opaque; + KBDQueue *q = &s->queue; + + switch (addr & 0xf) { + case 0x0: /* 0xe000 */ + // DPRINTF("KB L Read: @ %X\n", pc); + return 0xA0F09300; + + case 0x8: /* 0xe008 */ + /* get keycode from buffer */ + if (q->count > 0) { + key = q->data[q->rptr]; + if (++q->rptr == KBD_QUEUE_SIZE) { + q->rptr = 0; + } + + q->count--; + + if (s->shift) { + key |= s->shift; + } + + if (key & 0x80) { + return 0; + } else { + return 0x10000000 | KD_VALID | key; + } + } else { + return 0; + } + + default: + DPRINTF("RL ADDR %x\n", addr); + return 0; + } +} + +static void kbd_write_byte(void *opaque, hwaddr addr, uint32_t val) +{ + DPRINTF("WB ADDR %x\n", addr); +} +static void kbd_write_word(void *opaque, hwaddr addr, uint32_t val) +{ + DPRINTF("WW ADDR %x\n", addr); +} +static void kbd_write_long(void *opaque, hwaddr addr, uint32_t val) +{ + DPRINTF("WL ADDR %x\n", addr); +} + +static uint64_t kbd_readfn(void *opaque, hwaddr addr, unsigned size) +{ + switch (size) { + case 1: + return kbd_read_byte(opaque, addr); + case 2: + return kbd_read_word(opaque, addr); + case 4: + return kbd_read_long(opaque, addr); + default: + g_assert_not_reached(); + } +} + +static void kbd_writefn(void *opaque, hwaddr addr, uint64_t value, + unsigned size) +{ + switch (size) { + case 1: + kbd_write_byte(opaque, addr, value); + break; + case 2: + kbd_write_word(opaque, addr, value); + break; + case 4: + kbd_write_long(opaque, addr, value); + break; + default: + g_assert_not_reached(); + } +} + +static const MemoryRegionOps kbd_ops = { + .read = kbd_readfn, + .write = kbd_writefn, + .valid.min_access_size = 1, + .valid.max_access_size = 4, + .endianness = DEVICE_NATIVE_ENDIAN, +}; + +static void nextkbd_event(void *opaque, int ch) +{ + /* Will want to set vars for caps/num lock */ + /* if (ch & 0x80) -> key release */ + /* there's also e0 escaped scancodes that might need to be handled */ + DPRINTF("EVENT %X\n", ch); + + queue_code(opaque, ch); +} + +static const unsigned char next_keycodes[128] = { + 0x00, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x50, 0x4F, + 0x4E, 0x1E, 0x1F, 0x20, 0x1D, 0x1C, 0x1B, 0x00, + 0x42, 0x43, 0x44, 0x45, 0x48, 0x47, 0x46, 0x06, + 0x07, 0x08, 0x00, 0x00, 0x2A, 0x00, 0x39, 0x3A, + 0x3B, 0x3C, 0x3D, 0x40, 0x3F, 0x3E, 0x2D, 0x2C, + 0x2B, 0x26, 0x00, 0x00, 0x31, 0x32, 0x33, 0x34, + 0x35, 0x37, 0x36, 0x2e, 0x2f, 0x30, 0x00, 0x00, + 0x00, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 +}; + +static void queue_code(void *opaque, int code) +{ + KBDState *s = (KBDState *)opaque; + KBDQueue *q = &s->queue; + int key = code & 0x7F; + int release = code & 0x80; + static int ext; + + if (code == 0xE0) { + ext = 1; + } + + if (code == 0x2A || code == 0x1D || code == 0x36) { + if (code == 0x2A) { + s->shift = KD_LSHIFT; + } else if (code == 0x36) { + s->shift = KD_RSHIFT; + ext = 0; + } else if (code == 0x1D && !ext) { + s->shift = KD_LCOMM; + } else if (code == 0x1D && ext) { + ext = 0; + s->shift = KD_RCOMM; + } + return; + } else if (code == (0x2A | 0x80) || code == (0x1D | 0x80) || + code == (0x36 | 0x80)) { + s->shift = 0; + return; + } + + if (q->count >= KBD_QUEUE_SIZE) { + return; + } + + q->data[q->wptr] = next_keycodes[key] | release; + + if (++q->wptr == KBD_QUEUE_SIZE) { + q->wptr = 0; + } + + q->count++; + + /* + * might need to actually trigger the NeXT irq, but as the keyboard works + * at the moment, I'll worry about it later + */ + // s->update_irq(s->update_arg, 1); + + s->blank += 1; +} + +void nextkbd_init(void) +{ + KBDState *s = g_new0(KBDState, 1); + MemoryRegion *kbdmem = g_new(MemoryRegion, 1); + + s->shift = 0; + + memory_region_init_io(kbdmem, NULL, &kbd_ops, s, "next.kbd", 0x1000); + memory_region_add_subregion(get_system_memory(), 0x200e000, kbdmem); + + qemu_add_kbd_event_handler(nextkbd_event, s); +} diff --git a/include/hw/m68k/next-cube.h b/include/hw/m68k/next-cube.h index cf07243bda..88e94f6595 100644 --- a/include/hw/m68k/next-cube.h +++ b/include/hw/m68k/next-cube.h @@ -5,4 +5,7 @@ /* next-fb.c */ void nextfb_init(void); +/* next-kbd.c */ +void nextkbd_init(void); + #endif /* NEXT_CUBE_H */