From patchwork Thu Jun 2 20:06:55 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Rolnik X-Patchwork-Id: 9151159 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 BFA9B60221 for ; Thu, 2 Jun 2016 20:23:05 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B0F622656B for ; Thu, 2 Jun 2016 20:23:05 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A5D5528328; Thu, 2 Jun 2016 20:23:05 +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=-6.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID 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 9C7272656B for ; Thu, 2 Jun 2016 20:23:04 +0000 (UTC) Received: from localhost ([::1]:50180 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b8Z8x-0003jM-C0 for patchwork-qemu-devel@patchwork.kernel.org; Thu, 02 Jun 2016 16:23:03 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55263) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b8Ytq-0006Hl-LS for qemu-devel@nongnu.org; Thu, 02 Jun 2016 16:07:28 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b8Yth-0003ls-Vg for qemu-devel@nongnu.org; Thu, 02 Jun 2016 16:07:25 -0400 Received: from mail-wm0-x241.google.com ([2a00:1450:400c:c09::241]:35762) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b8Yth-0003li-IP for qemu-devel@nongnu.org; Thu, 02 Jun 2016 16:07:17 -0400 Received: by mail-wm0-x241.google.com with SMTP id e3so18540007wme.2 for ; Thu, 02 Jun 2016 13:07:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=hj8aEbsAhGVPGxcrbD7irFcgaDdz9RHDZHyGMABdy8w=; b=DhJ6QAVcBXx/g6eKjqnG7OoVAMPEVWy16dJtmmePZk2G23cBCU4r6MrPJXuDmHcHaX PDwy8tuRZikmFxeqks/OdJSvCRe3G7J42j+1mplRF7L9F9Zg/77X2MvB0puwfE0kniFQ zAeTyE6fCB2G3brChgh0Fy+Xx/QcepBF3uVFeRFBfzrrDc0FVBMkY3OozQYxcuJVFjyn T6O7Jg+FrlAyDoEyh3FBoqE0uyrgMF0/nQfmh7YXxA54gI98rBpI5quSDbdvscj3IYfA shQCMDrrgwMhWTIp0tKhTBKPOR352SbCpMUaaD8JvAotn2x091ZeMwbgii0eKVa6F9oM R7xg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=hj8aEbsAhGVPGxcrbD7irFcgaDdz9RHDZHyGMABdy8w=; b=KV/lzz+YfRsVxA5Q30dsyUJDlysZBiM/wprcwtncbv2TDA5naJYCBzwZIlrfTjU7Fo iTE7Q9HN9REZNHp1ri/9+TyO2q7weUx9kFulEBR0YqvaWLL6Ol3fpsgnBgGNK70LxwRx 1XVG/gcrxffKDLA2Fa0SFxvoeBieBHcxTR+Wldaa1+Gpe/RkuT/I92leoi/k4/nzyMb/ o1LZXPLzfR/WMQ6YFrYiOFlKOZ5ff/665cz3NMENgTI6xVURbsqYccfRvREhQun222xU BQFSCQnN9mUdDM94TQSutt7HPnDgi+RndUnN+Qqh8+ikwc+hIwji3usO06OKxDQnxaH7 1jSA== X-Gm-Message-State: ALyK8tLna+JZhgSXBm0IQ4qaF8PQzuNNcDqqH6bnlTX8KXwZDkEXpIVEtJzqES2VDOaeBg== X-Received: by 10.194.191.133 with SMTP id gy5mr10954178wjc.136.1464898036821; Thu, 02 Jun 2016 13:07:16 -0700 (PDT) Received: from a0999b0126e1.ant.amazon.com ([94.230.86.227]) by smtp.gmail.com with ESMTPSA id i4sm2127090wjj.49.2016.06.02.13.07.15 (version=TLS1 cipher=AES128-SHA bits=128/128); Thu, 02 Jun 2016 13:07:16 -0700 (PDT) From: Michael Rolnik X-Google-Original-From: Michael Rolnik To: qemu-devel@nongnu.org Date: Thu, 2 Jun 2016 23:06:55 +0300 Message-Id: <1464898022-97990-3-git-send-email-rolnik@amazon.com> X-Mailer: git-send-email 2.4.9 (Apple Git-60) In-Reply-To: <1464898022-97990-1-git-send-email-rolnik@amazon.com> References: <1464898022-97990-1-git-send-email-rolnik@amazon.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::241 Subject: [Qemu-devel] [PATCH 03/10] target-avr: adding a sample AVR board 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: Michael Rolnik , Michael Rolnik Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Signed-off-by: Michael Rolnik --- hw/Makefile.objs | 1 + hw/avr/Makefile.objs | 1 + hw/avr/sample-io.c | 246 +++++++++++++++++++++++++++++++++++++++++++++++++++ hw/avr/sample.c | 120 +++++++++++++++++++++++++ 4 files changed, 368 insertions(+) create mode 100644 hw/avr/Makefile.objs create mode 100644 hw/avr/sample-io.c create mode 100644 hw/avr/sample.c diff --git a/hw/Makefile.objs b/hw/Makefile.objs index 4a07ed4..262ca15 100644 --- a/hw/Makefile.objs +++ b/hw/Makefile.objs @@ -33,6 +33,7 @@ devices-dirs-$(CONFIG_SOFTMMU) += watchdog/ devices-dirs-$(CONFIG_SOFTMMU) += xen/ devices-dirs-$(CONFIG_MEM_HOTPLUG) += mem/ devices-dirs-$(CONFIG_SMBIOS) += smbios/ +devices-dirs-$(CONFIG_SOFTMMU) += avr/ devices-dirs-y += core/ common-obj-y += $(devices-dirs-y) obj-y += $(devices-dirs-y) diff --git a/hw/avr/Makefile.objs b/hw/avr/Makefile.objs new file mode 100644 index 0000000..9f6be2f --- /dev/null +++ b/hw/avr/Makefile.objs @@ -0,0 +1 @@ +obj-y += sample.o sample-io.o diff --git a/hw/avr/sample-io.c b/hw/avr/sample-io.c new file mode 100644 index 0000000..133c72f --- /dev/null +++ b/hw/avr/sample-io.c @@ -0,0 +1,246 @@ +/* + * QEMU AVR CPU + * + * Copyright (c) 2016 Michael Rolnik + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, see + * + */ + +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "qemu-common.h" +#include "cpu.h" +#include "include/hw/sysbus.h" + +#define TYPE_SAMPLEIO "SampleIO" +#define SAMPLEIO(obj) OBJECT_CHECK(SAMPLEIOState, (obj), TYPE_SAMPLEIO) + +#ifndef DEBUG_SAMPLEIO +#define DEBUG_SAMPLEIO 1 +#endif + +#define DPRINTF(fmt, args...) \ + do { \ + if (DEBUG_SAMPLEIO) { \ + fprintf(stderr, "[%s]%s: " fmt , TYPE_SAMPLEIO, __func__, ##args);\ + } \ + } \ + while (0) + +#define AVR_IO_CPU_REGS_SIZE 0x0020 +#define AVR_IO_CPU_IO_SIZE 0x0040 +#define AVR_IO_EXTERN_IO_SIZE 0x00a0 +#define AVR_IO_SIZE (AVR_IO_CPU_REGS_SIZE \ + + AVR_IO_CPU_IO_SIZE \ + + AVR_IO_EXTERN_IO_SIZE) + +#define AVR_IO_CPU_REGS_BASE 0x0000 +#define AVR_IO_CPU_IO_BASE (AVR_IO_CPU_REGS_BASE \ + + AVR_IO_CPU_REGS_SIZE) +#define AVR_IO_EXTERN_IO_BASE (AVR_IO_CPU_IO_BASE \ + + AVR_IO_CPU_IO_SIZE) + + +typedef struct SAMPLEIOState { + SysBusDevice parent; + + MemoryRegion iomem; + + AVRCPU *cpu; + + uint8_t io[0x40]; + uint8_t exio[0xa0]; +} SAMPLEIOState; + +static uint64_t sample_io_read( + void *opaque, + hwaddr offset, + unsigned size); +static void sample_io_write(void *opaque, + hwaddr offset, + uint64_t value, + unsigned size); +static int sample_io_init( + DeviceState *sbd); +static void sample_io_class_init( + ObjectClass *klass, + void *data); +static void sample_io_register_types(void); + +static void write_Rx( + CPUAVRState *env, + int inst, + uint8_t data); +static uint8_t read_Rx( + CPUAVRState *env, + int inst); + +static const +MemoryRegionOps sample_io_ops = { + .read = sample_io_read, + .write = sample_io_write, + .endianness = DEVICE_NATIVE_ENDIAN, +}; + +static +Property sample_io_properties[] = { + DEFINE_PROP_END_OF_LIST(), +}; +static const +VMStateDescription sample_io_vmstate = { + .name = TYPE_SAMPLEIO, + .version_id = 1, + .minimum_version_id = 1, + .fields = (VMStateField[]) + { + VMSTATE_END_OF_LIST() + } +}; + +void write_Rx( + CPUAVRState *env, + int inst, + uint8_t data) +{ + env->r[inst] = data; +} +uint8_t read_Rx( + CPUAVRState *env, + int inst) +{ + return env->r[inst]; +} + +static +void sample_io_reset(DeviceState *dev) +{ + DPRINTF("\n"); +} + +static +uint64_t sample_io_read( + void *opaque, + hwaddr offset, + unsigned size) +{ + SAMPLEIOState *s = SAMPLEIO(opaque); + AVRCPU *cpu = s->cpu; + CPUAVRState *env = &cpu->env; + uint64_t res = 0; + + assert(size == 1); + + if (AVR_IO_CPU_REGS_BASE <= offset + && offset < (AVR_IO_CPU_REGS_BASE + AVR_IO_CPU_REGS_SIZE)) { + res = read_Rx(env, offset - AVR_IO_CPU_REGS_BASE); + } else if (AVR_IO_CPU_IO_BASE <= offset + && offset < (AVR_IO_CPU_IO_BASE + AVR_IO_CPU_IO_SIZE)) { + /* TODO: do IO related stuff here */ + res = s->io[offset - AVR_IO_CPU_IO_BASE]; + } else if (AVR_IO_EXTERN_IO_BASE <= offset + && offset < (AVR_IO_EXTERN_IO_BASE + AVR_IO_EXTERN_IO_SIZE)) { + /* TODO: do IO related stuff here */ + res = s->io[offset - AVR_IO_EXTERN_IO_BASE]; + } else { + g_assert_not_reached(); + } + + return res; +} + +static +void sample_io_write( + void *opaque, + hwaddr offset, + uint64_t value, + unsigned size) +{ + SAMPLEIOState *s = SAMPLEIO(opaque); + AVRCPU *cpu = s->cpu; + CPUAVRState *env = &cpu->env; + + assert(size == 1); + + if (AVR_IO_CPU_REGS_BASE <= offset + && offset < (AVR_IO_CPU_REGS_BASE + AVR_IO_CPU_REGS_SIZE)) { + return write_Rx(env, offset - AVR_IO_CPU_REGS_BASE, value); + } else if (AVR_IO_CPU_IO_BASE <= offset + && offset < (AVR_IO_CPU_IO_BASE + AVR_IO_CPU_IO_SIZE)) { + /* TODO: do IO related stuff here */ + s->io[offset - AVR_IO_CPU_IO_BASE] = value; + } else if (AVR_IO_EXTERN_IO_BASE <= offset + && offset < (AVR_IO_EXTERN_IO_BASE + AVR_IO_EXTERN_IO_SIZE)) { + /* TODO: do IO related stuff here */ + s->io[offset - AVR_IO_EXTERN_IO_BASE] = value; + } else { + g_assert_not_reached(); + } +} + +static +int sample_io_init( + DeviceState *dev) +{ + SysBusDevice *sbd = SYS_BUS_DEVICE(dev); + SAMPLEIOState *s = SAMPLEIO(dev); + + assert(AVR_IO_SIZE <= TARGET_PAGE_SIZE); + + s->cpu = AVR_CPU(qemu_get_cpu(0)); + + memory_region_init_io( + &s->iomem, + OBJECT(s), + &sample_io_ops, + s, + TYPE_SAMPLEIO, + AVR_IO_SIZE); + sysbus_init_mmio(sbd, &s->iomem); + + return 0; +} + +static +void sample_io_class_init( + ObjectClass *klass, + void *data) +{ + DeviceClass *dc = DEVICE_CLASS(klass); + + DPRINTF("\n"); + + dc->init = sample_io_init; + dc->reset = sample_io_reset; + dc->desc = "at90 io regs"; + dc->vmsd = &sample_io_vmstate; + dc->props = sample_io_properties; +} + +static const +TypeInfo sample_io_info = { + .name = TYPE_SAMPLEIO, + .parent = TYPE_SYS_BUS_DEVICE, + .instance_size = sizeof(SAMPLEIOState), + .class_init = sample_io_class_init, +}; + +static +void sample_io_register_types(void) +{ + DPRINTF("\n"); + type_register_static(&sample_io_info); +} + +type_init(sample_io_register_types) diff --git a/hw/avr/sample.c b/hw/avr/sample.c new file mode 100644 index 0000000..d8b9d5c --- /dev/null +++ b/hw/avr/sample.c @@ -0,0 +1,120 @@ +/* + * QEMU AVR CPU + * + * Copyright (c) 2016 Michael Rolnik + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, see + * + */ + +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "qemu-common.h" +#include "cpu.h" +#include "hw/hw.h" +#include "sysemu/sysemu.h" +#include "sysemu/qtest.h" +#include "ui/console.h" +#include "hw/boards.h" +#include "hw/devices.h" +#include "hw/loader.h" +#include "qemu/error-report.h" +#include "exec/address-spaces.h" +#include "include/hw/sysbus.h" + +#define VIRT_BASE_FLASH 0x00000000 +#define VIRT_BASE_IOREG 0x00000000 +#define VIRT_BASE_ISRAM 0x00000100 +#define VIRT_BASE_EXMEM 0x00001100 +#define VIRT_BASE_EEPROM 0x00000000 + +#define VIRT_BASE_BOOT 0x0001e000 +#define PHYS_BASE_BOOT (PHYS_BASE_FLASH + VIRT_BASE_BOOT) + +#define SIZE_FLASH 0x00020000 +#define SIZE_IOREG 0x00000100 +#define SIZE_ISRAM 0x00001000 +#define SIZE_EXMEM 0x00010000 +#define SIZE_EEPROM 0x00001000 + +#define PHYS_BASE_FLASH (PHYS_CODE_BASE) +#define PHYS_BASE_IOREG (PHYS_DATA_BASE) +#define PHYS_BASE_ISRAM (PHYS_BASE_IOREG + SIZE_IOREG) +#define PHYS_BASE_EXMEM (PHYS_BASE_ISRAM + SIZE_ISRAM) +#define PHYS_BASE_EEPROM (PHYS_BASE_EXMEM + SIZE_EXMEM) + + +static +void sample_init(MachineState *machine) +{ + MemoryRegion *address_space_mem = get_system_memory(); + + MemoryRegion *flash; + MemoryRegion *isram; + MemoryRegion *exmem; + + AVRCPU *cpu_avr; + DeviceState *io; + SysBusDevice *bus; + + flash = g_new(MemoryRegion, 1); + isram = g_new(MemoryRegion, 1); + exmem = g_new(MemoryRegion, 1); + + cpu_avr = cpu_avr_init("avr5"); + io = qdev_create(NULL, "SampleIO"); + bus = SYS_BUS_DEVICE(io); + qdev_init_nofail(io); + + memory_region_init_ram(flash, NULL, "flash", SIZE_FLASH, &error_fatal); + memory_region_init_ram(isram, NULL, "isram", SIZE_ISRAM, &error_fatal); + memory_region_init_ram(exmem, NULL, "exmem", SIZE_EXMEM, &error_fatal); + + memory_region_add_subregion(address_space_mem, PHYS_BASE_FLASH, flash); + memory_region_add_subregion(address_space_mem, PHYS_BASE_ISRAM, isram); + memory_region_add_subregion(address_space_mem, PHYS_BASE_EXMEM, exmem); + + vmstate_register_ram_global(flash); + vmstate_register_ram_global(isram); + vmstate_register_ram_global(exmem); + + memory_region_set_readonly(flash, true); + + char const *firmware = NULL; + char const *filename; + + if (machine->firmware) { + firmware = machine->firmware; + } + + filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, firmware); + if (!filename) { + error_report("Could not find flash image file '%s'", firmware); + exit(1); + } + + load_image_targphys(filename, PHYS_BASE_FLASH, SIZE_FLASH); + + sysbus_mmio_map(bus, 0, PHYS_BASE_IOREG); +} + +static +void sample_machine_init(MachineClass *mc) +{ + mc->desc = "sample"; + mc->init = sample_init; + mc->is_default = 1; +} + +DEFINE_MACHINE("sample", sample_machine_init)