2013-12-17 20:42:36 +01:00
|
|
|
/*
|
|
|
|
* QEMU model of the Canon DIGIC timer block.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 Antony Pavlov <antonynpavlov@gmail.com>
|
|
|
|
*
|
|
|
|
* This model is based on reverse engineering efforts
|
|
|
|
* made by CHDK (http://chdk.wikia.com) and
|
|
|
|
* Magic Lantern (http://www.magiclantern.fm) projects
|
|
|
|
* contributors.
|
|
|
|
*
|
|
|
|
* See "Timer/Clock Module" docs here:
|
|
|
|
* http://magiclantern.wikia.com/wiki/Register_Map
|
|
|
|
*
|
|
|
|
* The QEMU model of the OSTimer in PKUnity SoC by Guan Xuetao
|
|
|
|
* is used as a template.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-01-26 19:17:05 +01:00
|
|
|
#include "qemu/osdep.h"
|
2013-12-17 20:42:36 +01:00
|
|
|
#include "hw/sysbus.h"
|
|
|
|
#include "hw/ptimer.h"
|
2019-05-23 16:35:07 +02:00
|
|
|
#include "qemu/module.h"
|
2015-12-15 13:16:16 +01:00
|
|
|
#include "qemu/log.h"
|
2013-12-17 20:42:36 +01:00
|
|
|
|
|
|
|
#include "hw/timer/digic-timer.h"
|
2019-08-12 07:23:45 +02:00
|
|
|
#include "migration/vmstate.h"
|
2013-12-17 20:42:36 +01:00
|
|
|
|
|
|
|
static const VMStateDescription vmstate_digic_timer = {
|
|
|
|
.name = "digic.timer",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
2023-12-21 04:16:37 +01:00
|
|
|
.fields = (const VMStateField[]) {
|
2013-12-17 20:42:36 +01:00
|
|
|
VMSTATE_PTIMER(ptimer, DigicTimerState),
|
|
|
|
VMSTATE_UINT32(control, DigicTimerState),
|
|
|
|
VMSTATE_UINT32(relvalue, DigicTimerState),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static void digic_timer_reset(DeviceState *dev)
|
|
|
|
{
|
|
|
|
DigicTimerState *s = DIGIC_TIMER(dev);
|
|
|
|
|
2019-10-08 19:17:29 +02:00
|
|
|
ptimer_transaction_begin(s->ptimer);
|
2013-12-17 20:42:36 +01:00
|
|
|
ptimer_stop(s->ptimer);
|
2019-10-08 19:17:29 +02:00
|
|
|
ptimer_transaction_commit(s->ptimer);
|
2013-12-17 20:42:36 +01:00
|
|
|
s->control = 0;
|
|
|
|
s->relvalue = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t digic_timer_read(void *opaque, hwaddr offset, unsigned size)
|
|
|
|
{
|
|
|
|
DigicTimerState *s = opaque;
|
|
|
|
uint64_t ret = 0;
|
|
|
|
|
|
|
|
switch (offset) {
|
|
|
|
case DIGIC_TIMER_CONTROL:
|
|
|
|
ret = s->control;
|
|
|
|
break;
|
|
|
|
case DIGIC_TIMER_RELVALUE:
|
|
|
|
ret = s->relvalue;
|
|
|
|
break;
|
|
|
|
case DIGIC_TIMER_VALUE:
|
|
|
|
ret = ptimer_get_count(s->ptimer) & 0xffff;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
qemu_log_mask(LOG_UNIMP,
|
|
|
|
"digic-timer: read access to unknown register 0x"
|
2023-01-10 22:29:47 +01:00
|
|
|
HWADDR_FMT_plx "\n", offset);
|
2013-12-17 20:42:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void digic_timer_write(void *opaque, hwaddr offset,
|
|
|
|
uint64_t value, unsigned size)
|
|
|
|
{
|
|
|
|
DigicTimerState *s = opaque;
|
|
|
|
|
|
|
|
switch (offset) {
|
|
|
|
case DIGIC_TIMER_CONTROL:
|
|
|
|
if (value & DIGIC_TIMER_CONTROL_RST) {
|
|
|
|
digic_timer_reset((DeviceState *)s);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-10-08 19:17:29 +02:00
|
|
|
ptimer_transaction_begin(s->ptimer);
|
2013-12-17 20:42:36 +01:00
|
|
|
if (value & DIGIC_TIMER_CONTROL_EN) {
|
|
|
|
ptimer_run(s->ptimer, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
s->control = (uint32_t)value;
|
2019-10-08 19:17:29 +02:00
|
|
|
ptimer_transaction_commit(s->ptimer);
|
2013-12-17 20:42:36 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DIGIC_TIMER_RELVALUE:
|
|
|
|
s->relvalue = extract32(value, 0, 16);
|
2019-10-08 19:17:29 +02:00
|
|
|
ptimer_transaction_begin(s->ptimer);
|
2013-12-17 20:42:36 +01:00
|
|
|
ptimer_set_limit(s->ptimer, s->relvalue, 1);
|
2019-10-08 19:17:29 +02:00
|
|
|
ptimer_transaction_commit(s->ptimer);
|
2013-12-17 20:42:36 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DIGIC_TIMER_VALUE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
qemu_log_mask(LOG_UNIMP,
|
|
|
|
"digic-timer: read access to unknown register 0x"
|
2023-01-10 22:29:47 +01:00
|
|
|
HWADDR_FMT_plx "\n", offset);
|
2013-12-17 20:42:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const MemoryRegionOps digic_timer_ops = {
|
|
|
|
.read = digic_timer_read,
|
|
|
|
.write = digic_timer_write,
|
|
|
|
.impl = {
|
|
|
|
.min_access_size = 4,
|
|
|
|
.max_access_size = 4,
|
|
|
|
},
|
|
|
|
.endianness = DEVICE_NATIVE_ENDIAN,
|
|
|
|
};
|
|
|
|
|
2019-10-08 19:17:29 +02:00
|
|
|
static void digic_timer_tick(void *opaque)
|
|
|
|
{
|
|
|
|
/* Nothing to do on timer rollover */
|
|
|
|
}
|
|
|
|
|
2013-12-17 20:42:36 +01:00
|
|
|
static void digic_timer_init(Object *obj)
|
|
|
|
{
|
|
|
|
DigicTimerState *s = DIGIC_TIMER(obj);
|
|
|
|
|
2022-05-16 12:30:58 +02:00
|
|
|
s->ptimer = ptimer_init(digic_timer_tick, NULL, PTIMER_POLICY_LEGACY);
|
2013-12-17 20:42:36 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* FIXME: there is no documentation on Digic timer
|
|
|
|
* frequency setup so let it always run at 1 MHz
|
|
|
|
*/
|
2019-10-08 19:17:29 +02:00
|
|
|
ptimer_transaction_begin(s->ptimer);
|
2013-12-17 20:42:36 +01:00
|
|
|
ptimer_set_freq(s->ptimer, 1 * 1000 * 1000);
|
2019-10-08 19:17:29 +02:00
|
|
|
ptimer_transaction_commit(s->ptimer);
|
2013-12-17 20:42:36 +01:00
|
|
|
|
|
|
|
memory_region_init_io(&s->iomem, OBJECT(s), &digic_timer_ops, s,
|
|
|
|
TYPE_DIGIC_TIMER, 0x100);
|
|
|
|
sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->iomem);
|
|
|
|
}
|
|
|
|
|
2020-12-17 12:31:49 +01:00
|
|
|
static void digic_timer_finalize(Object *obj)
|
|
|
|
{
|
|
|
|
DigicTimerState *s = DIGIC_TIMER(obj);
|
|
|
|
|
|
|
|
ptimer_free(s->ptimer);
|
|
|
|
}
|
|
|
|
|
2013-12-17 20:42:36 +01:00
|
|
|
static void digic_timer_class_init(ObjectClass *klass, void *class_data)
|
|
|
|
{
|
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
|
|
|
|
|
|
|
dc->reset = digic_timer_reset;
|
|
|
|
dc->vmsd = &vmstate_digic_timer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const TypeInfo digic_timer_info = {
|
|
|
|
.name = TYPE_DIGIC_TIMER,
|
|
|
|
.parent = TYPE_SYS_BUS_DEVICE,
|
|
|
|
.instance_size = sizeof(DigicTimerState),
|
|
|
|
.instance_init = digic_timer_init,
|
2020-12-17 12:31:49 +01:00
|
|
|
.instance_finalize = digic_timer_finalize,
|
2013-12-17 20:42:36 +01:00
|
|
|
.class_init = digic_timer_class_init,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void digic_timer_register_type(void)
|
|
|
|
{
|
|
|
|
type_register_static(&digic_timer_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
type_init(digic_timer_register_type)
|