Qemu-NUC980(七):Timer定时器
概述
本文描述了添加980 Timer定时器功能代码的步骤,在描述过程中,为了清晰的描述添加框架,部分代码的细节被删除,详细的代码,请参考文末的工程链接。
添加步骤
1、修改hw/arm/Kconfig文件,如下所示:
+号部分为新增加内容
--- a/hw/arm/Kconfig
+++ b/hw/arm/Kconfig
@@ -120,6 +120,7 @@ config NUC980select NUC980_SDICselect NUC980_UARTselect NUC980_AIC
+ select NUC980_TIMER
2、修改hw/arm/nuc980_soc.c,如下所示:
+号部分为新增加内容
--- a/hw/arm/nuc980_soc.c
+++ b/hw/arm/nuc980_soc.c
@@ -30,6 +30,15 @@ static struct devinfo serial_table[UART_COUNT] = {{ UART2_BASE, 38}};+static struct devinfo timer_table[TIMER_COUNT] = {
+ { TIMER0_BASE, 16},
+ { TIMER1_BASE, 17},
+ { TIMER2_BASE, 30},
+ { TIMER3_BASE, 31},
+ { TIMER4_BASE, 32},
+ { TIMER5_BASE, 34},
+};
+static void nuc980_init(Object *obj){int i;
@@ -54,6 +63,12 @@ static void nuc980_init(Object *obj)}/* aic */sysbus_init_child_obj(obj, "aic", &s->aic, sizeof(s->aic), TYPE_NUC980_AIC);
+ /* timer */
+ for (i = 0; i < TIMER_COUNT; i++) {
+ snprintf(name, NAME_SIZE, "timer%d", i + 1);
+ sysbus_init_child_obj(obj, name, &s->timer[i], sizeof(s->timer[i]),
+ TYPE_NUC980_TIMER);
+ }}static void nuc980_realize(DeviceState *dev, Error **errp)
@@ -107,6 +122,19 @@ static void nuc980_realize(DeviceState *dev, Error **errp)qdev_get_gpio_in(DEVICE(&s->aic),serial_table[i].irq));}
+ /* timer */
+ for (i = 0; i < TIMER_COUNT; i++) {
+ qdev_prop_set_uint32(DEVICE(&s->timer[i]), "timer_id", i);
+ object_property_set_bool(OBJECT(&s->timer[i]), true, "realized", &err);
+ if (err) {
+ error_propagate(errp, err);
+ return;
+ }
+ sysbus_mmio_map(SYS_BUS_DEVICE(&s->timer[i]), 0, timer_table[i].addr);
+ sysbus_connect_irq(SYS_BUS_DEVICE(&s->timer[i]), 0,
+ qdev_get_gpio_in(DEVICE(&s->aic),
+ timer_table[i].irq));
+ }}static void nuc980_class_init(ObjectClass *oc, void *data)
3、修改hw/timer/Kconfig,如下所示:
+号部分为新增加内容
--- a/hw/timer/Kconfig
+++ b/hw/timer/Kconfig
@@ -34,3 +34,7 @@ config CMSDK_APB_TIMERconfig CMSDK_APB_DUALTIMERboolselect PTIMER
+
+config NUC980_TIMER
+ bool
+ select PTIMER
4、修改hw/timer/Makefile.objs,如下所示:
+号部分为新增加内容
--- a/hw/timer/Makefile.objs
+++ b/hw/timer/Makefile.objs
@@ -35,3 +35,4 @@ common-obj-$(CONFIG_CMSDK_APB_TIMER) += cmsdk-apb-timer.ocommon-obj-$(CONFIG_CMSDK_APB_DUALTIMER) += cmsdk-apb-dualtimer.ocommon-obj-$(CONFIG_MSF2) += mss-timer.ocommon-obj-$(CONFIG_RASPI) += bcm2835_systmr.o
+obj-$(CONFIG_NUC980_TIMER) += nuc980_timer.o
5、添加hw/timer/nuc980_timer.c,如下所示:
+号部分为新增加内容
diff --git a/hw/timer/nuc980_timer.c b/hw/timer/nuc980_timer.c
new file mode 100644
index 00000000..9d54b29a
--- /dev/null
+++ b/hw/timer/nuc980_timer.c
@@ -0,0 +1,221 @@
+/*
+ * NUC980 SOC System emulation.
+ *
+ * Copyright (c) 2023- yanl1229@163.com.
+ * Written by yanl1229
+ *
+ * This code is licensed under the GPL.
+ */
+#include "qemu/osdep.h"
+#include "hw/timer/nuc980_timer.h"
+#include "hw/misc/nuc980_clk.h"
+#include "qemu/log.h"
+#include "hw/irq.h"
+#include "hw/qdev-properties.h"
+#include "qemu/module.h"
+#include "migration/vmstate.h"
+
+
+#ifndef TIMER_ERR_DEBUG
+#define TIMER_ERR_DEBUG 0
+#endif
+
+#define DB_PRINT_L(lvl, fmt, args...) do { \
+ if (TIMER_ERR_DEBUG >= lvl) { \
+ qemu_log("%s: " fmt, __func__, ## args); \
+ } \
+} while (0);
+
+#define DB_PRINT(fmt, args...) DB_PRINT_L(1, fmt, ## args)
+
+static void nuc980_timer_reset(DeviceState *dev)
+{
+ NUC980TimerState *s = NUC980TIMER(dev);
+
+ s->timer_ctl = 0x00000000;
+ s->timer_precnt = 0x00000000;
+ s->timer_cmp = 0x00000000;
+ s->timer_inten = 0x00000000;
+ s->timer_intsts = 0x00000000;
+ s->timer_cnt = 0x00000000;
+ s->timer_cap = 0x00000000;
+ s->timer_ectl = 0x00000000;
+}+static uint64_t nuc980_timer_read(void *opaque, hwaddr offset,
+ unsigned size)
+{
+ ......
+ return 0;
+}
+
+static void nuc980_timer_write(void *opaque, hwaddr offset,
+ uint64_t val64, unsigned size)
+{......
+}
+
+static const MemoryRegionOps nuc980_timer_ops = {
+ .read = nuc980_timer_read,
+ .write = nuc980_timer_write,
+ .endianness = DEVICE_NATIVE_ENDIAN,
+};
+
+static const VMStateDescription vmstate_nuc980_timer = {
+ .name = TYPE_NUC980_TIMER,
+ .version_id = 1,
+ .minimum_version_id = 1,
+ .fields = (VMStateField[]) {
+ VMSTATE_UINT32(timer_ctl, NUC980TimerState),
+ VMSTATE_UINT32(timer_precnt, NUC980TimerState),
+ VMSTATE_UINT32(timer_cmp, NUC980TimerState),
+ VMSTATE_UINT32(timer_inten, NUC980TimerState),
+ VMSTATE_UINT32(timer_intsts, NUC980TimerState),
+ VMSTATE_UINT32(timer_cnt, NUC980TimerState),
+ VMSTATE_UINT32(timer_cap, NUC980TimerState),
+ VMSTATE_UINT32(timer_ectl, NUC980TimerState),
+ VMSTATE_END_OF_LIST()
+ }
+};
+
+static void nuc980_timer_tick(void *opaque)
+{
+ NUC980TimerState *s = (NUC980TimerState *)opaque;
+
+ DB_PRINT("%s update", __func__);
+ s->timer_intsts = 1;
+ nuc980_timer_update(s);
+}
+
+static void nuc980_timer_realize(DeviceState *dev, Error **errp)
+{
+ NUC980TimerState *s = NUC980TIMER(dev);
+ SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
+
+ sysbus_init_irq(sbd, &s->irq);
+ s->ptimer = ptimer_init(nuc980_timer_tick, s, PTIMER_POLICY_DEFAULT);
+ memory_region_init_io(&s->iomem, OBJECT(s), &nuc980_timer_ops, s,
+ "nuc980_timer", 0x100);
+ sysbus_init_mmio(sbd, &s->iomem);
+}
+
+static Property nuc980_properties[] = {
+ DEFINE_PROP_UINT32("timer_id", NUC980TimerState, timer_id, 0),
+ DEFINE_PROP_END_OF_LIST(),
+};
+
+static void nuc980_timer_class_init(ObjectClass *klass, void *data)
+{
+ DeviceClass *dc = DEVICE_CLASS(klass);
+ dc->realize = nuc980_timer_realize;
+ dc->reset = nuc980_timer_reset;
+ dc->props = nuc980_properties;
+ dc->vmsd = &vmstate_nuc980_timer;
+}
+
+static const TypeInfo nuc980_timer_info = {
+ .name = TYPE_NUC980_TIMER,
+ .parent = TYPE_SYS_BUS_DEVICE,
+ .instance_size = sizeof(NUC980TimerState),
+ .class_init = nuc980_timer_class_init,
+};
+
+static void nuc980_timer_register_types(void)
+{
+ type_register_static(&nuc980_timer_info);
+}
+
+type_init(nuc980_timer_register_types)
6、修改include/hw/arm/nuc980.h,如下所示:
+号部分为新增加内容
--- a/include/hw/arm/nuc980.h
+++ b/include/hw/arm/nuc980.h
@@ -18,6 +18,7 @@#include "hw/misc/nuc980_sdic.h"#include "hw/intc/nuc980_aic.h"#include "hw/char/nuc980_uart.h"
+#include "hw/timer/nuc980_timer.h"#define SDRAM_BASE 0x0000000#define SDRAM_SIZE (64 *1024 * 1024)
@@ -35,6 +36,14 @@#define UART2_BASE 0xb0072000#define UART_COUNT 3+#define TIMER0_BASE 0xb0050000
+#define TIMER1_BASE 0xb0050100
+#define TIMER2_BASE 0xb0051000
+#define TIMER3_BASE 0xb0051100
+#define TIMER4_BASE 0xb0052000
+#define TIMER5_BASE 0xb0052100
+#define TIMER_COUNT 6
+#define AIC_BASE 0xb0042000#define TYPE_NUC980 "nuc980"
@@ -54,6 +63,7 @@ typedef struct NUC980State {NUC980SDICState sdic;NUC980UartState uart[UART_COUNT];NUC980AICState aic;
+ NUC980TimerState timer[TIMER_COUNT];} NUC980State;
7、添加include/hw/timer/nuc980_timer.h,如下所示:
+号部分为新增加内容
new file mode 100644
index 00000000..ff90e96e
--- /dev/null
+++ b/include/hw/timer/nuc980_timer.h
@@ -0,0 +1,59 @@
+/*
+ * NUC980 SOC System emulation.
+ *
+ * Copyright (c) 2023- yanl1229@163.com.
+ * Written by yanl1229
+ *
+ * This code is licensed under the GPL.
+ */
+#ifndef NUC980_TIMER_H
+#define NUC980_TIMER_H
+
+#include "hw/sysbus.h"
+#include "qemu/timer.h"
+#include "sysemu/sysemu.h"
+#include "hw/ptimer.h"
+
+#define TIMER_CTL 0x00
+#define TIMER_PRECNT 0x04
+#define TIMER_CMP 0x08
+#define TIMER_INTEN 0x0c
+#define TIMER_INTSTS 0x10
+#define TIMER_CNT 0x14
+#define TIMER_CAP 0x18
+#define TIMER_ECTL 0x20
+
+#define TIMER_CTL_CNTEN (0x1 << 0)
+
+#define TIMER_INTEN_CNTIEN (0x1 << 0)
+
+#define TIMER_CNT_RSTACT (0x1 << 31)
+
+#define TYPE_NUC980_TIMER "nuc980-timer"
+#define NUC980TIMER(obj) OBJECT_CHECK(NUC980TimerState, \
+ (obj), TYPE_NUC980_TIMER)
+
+typedef struct NUC980TimerState {
+ /* <private> */
+ SysBusDevice parent_obj;
+
+ /* <public> */
+ MemoryRegion iomem;
+
+ uint32_t timer_ctl;
+ uint32_t timer_precnt;
+ uint32_t timer_cmp;
+ uint32_t timer_inten;
+ uint32_t timer_intsts;
+ uint32_t timer_cnt;
+ uint32_t timer_cap;
+ uint32_t timer_ectl;
+
+ uint32_t timer_rate;
+ qemu_irq irq;
+ ptimer_state *ptimer;
+ uint32_t timer_id;
+
+} NUC980TimerState;
+
+#endif
总结
工程链接
https://gitee.com/yanl1229/qemu.git