From: Michael Walle <michael@walle.cc>
To: qemu-devel@nongnu.org
Cc: "Edgar E. Iglesias" <edgar.iglesias@gmail.com>,
Michael Walle <michael@walle.cc>, Alexander Graf <agraf@suse.de>,
Richard Henderson <rth@twiddle.net>
Subject: [Qemu-devel] [PATCH 09/17] lm32: timer model
Date: Fri, 11 Feb 2011 00:12:02 +0100 [thread overview]
Message-ID: <1297379530-23487-10-git-send-email-michael@walle.cc> (raw)
In-Reply-To: <1297379530-23487-1-git-send-email-michael@walle.cc>
This patch adds support for the LatticeMico32 system timer.
Signed-off-by: Michael Walle <michael@walle.cc>
---
Makefile.target | 1 +
hw/lm32_timer.c | 227 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
trace-events | 6 ++
3 files changed, 234 insertions(+), 0 deletions(-)
create mode 100644 hw/lm32_timer.c
diff --git a/Makefile.target b/Makefile.target
index 7e8c5e9..e0f02cf 100644
--- a/Makefile.target
+++ b/Makefile.target
@@ -251,6 +251,7 @@ obj-ppc-y += xilinx_ethlite.o
obj-lm32-y += lm32_pic.o
obj-lm32-y += lm32_pic_cpu.o
obj-lm32-y += lm32_juart.o
+obj-lm32-y += lm32_timer.o
obj-mips-y = mips_r4k.o mips_jazz.o mips_malta.o mips_mipssim.o
obj-mips-y += mips_addr.o mips_timer.o mips_int.o
diff --git a/hw/lm32_timer.c b/hw/lm32_timer.c
new file mode 100644
index 0000000..3bfc29d
--- /dev/null
+++ b/hw/lm32_timer.c
@@ -0,0 +1,227 @@
+/*
+ * QEMU model of the LatticeMico32 timer block.
+ *
+ * Copyright (c) 2010 Michael Walle <michael@walle.cc>
+ *
+ * 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 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 <http://www.gnu.org/licenses/>.
+ *
+ *
+ * Specification available at:
+ * http://www.latticesemi.com/documents/mico32timer.pdf
+ */
+
+#include "hw.h"
+#include "sysbus.h"
+#include "trace.h"
+#include "qemu-timer.h"
+
+#define DEFAULT_FREQUENCY (50*1000000)
+
+enum {
+ R_SR = 0,
+ R_CR,
+ R_PERIOD,
+ R_SNAPSHOT,
+ R_MAX
+};
+
+enum {
+ SR_TO = (1 << 0),
+ SR_RUN = (1 << 1),
+};
+
+enum {
+ CR_ITO = (1 << 0),
+ CR_CONT = (1 << 1),
+ CR_START = (1 << 2),
+ CR_STOP = (1 << 3),
+};
+
+struct LM32TimerState {
+ SysBusDevice busdev;
+
+ QEMUBH *bh;
+ ptimer_state *ptimer;
+
+ qemu_irq irq;
+ uint32_t freq_hz;
+
+ uint32_t regs[R_MAX];
+};
+typedef struct LM32TimerState LM32TimerState;
+
+static void timer_update_irq(LM32TimerState *s)
+{
+ int state = s->regs[R_SR] & SR_TO && s->regs[R_CR] & CR_ITO;
+
+ trace_lm32_timer_irq_state(state);
+ qemu_set_irq(s->irq, state);
+}
+
+static uint32_t timer_read(void *opaque, target_phys_addr_t addr)
+{
+ LM32TimerState *s = opaque;
+ uint32_t r = 0;
+
+ addr >>= 2;
+ switch (addr) {
+ case R_SR:
+ case R_CR:
+ case R_PERIOD:
+ r = s->regs[addr];
+ break;
+ case R_SNAPSHOT:
+ r = (uint32_t)ptimer_get_count(s->ptimer);
+ break;
+
+ default:
+ hw_error("lm32_timer: read access to unkown register 0x"
+ TARGET_FMT_plx, addr << 2);
+ break;
+
+ }
+
+ trace_lm32_timer_memory_read(addr << 2, r);
+ return r;
+}
+
+static void timer_write(void *opaque, target_phys_addr_t addr, uint32_t value)
+{
+ LM32TimerState *s = opaque;
+
+ trace_lm32_timer_memory_write(addr, value);
+
+ addr >>= 2;
+ switch (addr) {
+ case R_SR:
+ s->regs[R_SR] &= ~SR_TO;
+ break;
+ case R_CR:
+ s->regs[R_CR] = value;
+ if (s->regs[R_CR] & CR_START) {
+ ptimer_run(s->ptimer, 1);
+ }
+ if (s->regs[R_CR] & CR_STOP) {
+ ptimer_stop(s->ptimer);
+ }
+ break;
+ case R_PERIOD:
+ s->regs[R_PERIOD] = value;
+ ptimer_set_count(s->ptimer, value);
+ break;
+ case R_SNAPSHOT:
+ hw_error("lm32_timer: write access to read only register 0x"
+ TARGET_FMT_plx, addr << 2);
+ break;
+
+ default:
+ hw_error("lm32_timer: write access to unkown register 0x"
+ TARGET_FMT_plx, addr << 2);
+ break;
+ }
+ timer_update_irq(s);
+}
+
+static CPUReadMemoryFunc * const timer_read_fn[] = {
+ NULL,
+ NULL,
+ &timer_read,
+};
+
+static CPUWriteMemoryFunc * const timer_write_fn[] = {
+ NULL,
+ NULL,
+ &timer_write,
+};
+
+static void timer_hit(void *opaque)
+{
+ LM32TimerState *s = opaque;
+
+ trace_lm32_timer_hit();
+
+ s->regs[R_SR] |= SR_TO;
+
+ if (s->regs[R_CR] & CR_CONT) {
+ ptimer_set_count(s->ptimer, s->regs[R_PERIOD]);
+ ptimer_run(s->ptimer, 1);
+ }
+
+ timer_update_irq(s);
+}
+
+static void timer_reset(void *opaque)
+{
+ LM32TimerState *s = opaque;
+ int i;
+
+ for (i = 0; i < R_MAX; i++) {
+ s->regs[i] = 0;
+ }
+ ptimer_stop(s->ptimer);
+ qemu_irq_lower(s->irq);
+}
+
+static int lm32_timer_init(SysBusDevice *dev)
+{
+ LM32TimerState *s = FROM_SYSBUS(typeof(*s), dev);
+ int timer_regs;
+
+ sysbus_init_irq(dev, &s->irq);
+
+ s->bh = qemu_bh_new(timer_hit, s);
+ s->ptimer = ptimer_init(s->bh);
+ ptimer_set_freq(s->ptimer, s->freq_hz);
+
+ timer_regs = cpu_register_io_memory(timer_read_fn, timer_write_fn, s,
+ DEVICE_NATIVE_ENDIAN);
+ sysbus_init_mmio(dev, R_MAX * 4, timer_regs);
+
+ qemu_register_reset(timer_reset, s);
+
+ return 0;
+}
+
+static const VMStateDescription vmstate_lm32_timer = {
+ .name = "lm32-timer",
+ .version_id = 1,
+ .minimum_version_id = 1,
+ .minimum_version_id_old = 1,
+ .fields = (VMStateField[]) {
+ VMSTATE_PTIMER(ptimer, LM32TimerState),
+ VMSTATE_UINT32(freq_hz, LM32TimerState),
+ VMSTATE_UINT32_ARRAY(regs, LM32TimerState, R_MAX),
+ VMSTATE_END_OF_LIST()
+ }
+};
+
+static SysBusDeviceInfo lm32_timer_info = {
+ .init = lm32_timer_init,
+ .qdev.name = "lm32-timer",
+ .qdev.size = sizeof(LM32TimerState),
+ .qdev.vmsd = &vmstate_lm32_timer,
+ .qdev.props = (Property[]) {
+ DEFINE_PROP_UINT32(
+ "frequency", LM32TimerState, freq_hz, DEFAULT_FREQUENCY
+ ),
+ DEFINE_PROP_END_OF_LIST(),
+ }
+};
+
+static void lm32_timer_register(void)
+{
+ sysbus_register_withprop(&lm32_timer_info);
+}
+
+device_init(lm32_timer_register)
diff --git a/trace-events b/trace-events
index 966099f..af7b27f 100644
--- a/trace-events
+++ b/trace-events
@@ -269,3 +269,9 @@ disable lm32_juart_get_jtx(uint32_t value) "jtx 0x%08x"
disable lm32_juart_set_jtx(uint32_t value) "jtx 0x%08x"
disable lm32_juart_get_jrx(uint32_t value) "jrx 0x%08x"
disable lm32_juart_set_jrx(uint32_t value) "jrx 0x%08x"
+
+# hw/lm32_timer.c
+disable lm32_timer_memory_write(uint32_t addr, uint32_t value) "addr 0x%08x value 0x%08x"
+disable lm32_timer_memory_read(uint32_t addr, uint32_t value) "addr 0x%08x value 0x%08x"
+disable lm32_timer_hit(void) "timer hit"
+disable lm32_timer_irq_state(int level) "irq state %d"
--
1.7.2.3
next prev parent reply other threads:[~2011-02-10 23:12 UTC|newest]
Thread overview: 46+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-02-10 23:11 [Qemu-devel] [PATCH 00/17 v2] LatticeMico32 target Michael Walle
2011-02-10 23:11 ` [Qemu-devel] [PATCH 01/17] LatticeMico32 target support Michael Walle
2011-02-10 23:11 ` [Qemu-devel] [PATCH 02/17] lm32: translation routines Michael Walle
2011-02-11 21:42 ` Blue Swirl
2011-02-11 22:23 ` Michael Walle
2011-02-12 6:49 ` Blue Swirl
2011-02-17 22:51 ` Michael Walle
2011-03-11 5:57 ` Alexander Graf
2011-03-16 23:08 ` Michael Walle
2011-03-16 23:48 ` Alexander Graf
2011-02-12 13:18 ` Michael Walle
2011-02-10 23:11 ` [Qemu-devel] [PATCH 03/17] lm32: translation code helper Michael Walle
2011-02-10 23:11 ` [Qemu-devel] [PATCH 04/17] lm32: machine state loading/saving Michael Walle
2011-02-10 23:11 ` [Qemu-devel] [PATCH 05/17] lm32: gdbstub support Michael Walle
2011-02-10 23:11 ` [Qemu-devel] [PATCH 06/17] lm32: interrupt controller model Michael Walle
2011-02-11 21:49 ` Blue Swirl
2011-02-10 23:12 ` [Qemu-devel] [PATCH 07/17] lm32: juart model Michael Walle
2011-02-11 21:09 ` Blue Swirl
2011-02-10 23:12 ` [Qemu-devel] [PATCH 08/17] lm32: pic and juart helper functions Michael Walle
2011-02-11 20:57 ` Blue Swirl
2011-02-10 23:12 ` Michael Walle [this message]
2011-02-11 21:22 ` [Qemu-devel] [PATCH 09/17] lm32: timer model Blue Swirl
2011-02-11 22:29 ` Michael Walle
2011-02-12 6:56 ` Blue Swirl
2011-02-10 23:12 ` [Qemu-devel] [PATCH 10/17] lm32: uart model Michael Walle
2011-02-11 21:16 ` Blue Swirl
2011-02-10 23:12 ` [Qemu-devel] [PATCH 11/17] lm32: system control model Michael Walle
2011-02-11 21:03 ` Blue Swirl
2011-02-11 22:35 ` Michael Walle
2011-02-12 7:54 ` Blue Swirl
2011-02-10 23:12 ` [Qemu-devel] [PATCH 12/17] lm32: support for creating device tree Michael Walle
2011-02-11 20:52 ` Blue Swirl
2011-02-11 22:40 ` Michael Walle
2011-02-12 7:56 ` Blue Swirl
2011-02-10 23:12 ` [Qemu-devel] [PATCH 13/17] lm32: EVR32 and uclinux BSP Michael Walle
2011-02-11 20:47 ` Blue Swirl
2011-02-10 23:12 ` [Qemu-devel] [PATCH 14/17] lm32: todo and documentation Michael Walle
2011-02-11 20:41 ` Blue Swirl
2011-02-11 22:45 ` Michael Walle
2011-02-12 8:00 ` Blue Swirl
2011-02-10 23:12 ` [Qemu-devel] [PATCH 15/17] lm32: opcode testsuite Michael Walle
2011-02-10 23:12 ` [Qemu-devel] [PATCH 16/17] Add lm32 target to configure Michael Walle
2011-02-10 23:12 ` [Qemu-devel] [PATCH 17/17] MAINTAINERS: add LatticeMico32 maintainer Michael Walle
2011-02-11 9:48 ` [Qemu-devel] Re: [PATCH 00/17 v2] LatticeMico32 target Edgar E. Iglesias
-- strict thread matches above, loose matches on Subject: below --
2011-02-17 22:45 [Qemu-devel] [PATCH 00/17 v3] " Michael Walle
2011-02-17 22:45 ` [Qemu-devel] [PATCH 09/17] lm32: timer model Michael Walle
2011-01-31 0:30 [Qemu-devel] [PATCH 00/17] LatticeMico32 target Michael Walle
2011-01-31 0:30 ` [Qemu-devel] [PATCH 09/17] lm32: timer model Michael Walle
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1297379530-23487-10-git-send-email-michael@walle.cc \
--to=michael@walle.cc \
--cc=agraf@suse.de \
--cc=edgar.iglesias@gmail.com \
--cc=qemu-devel@nongnu.org \
--cc=rth@twiddle.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).