2 * OSTimer device simulation in PKUnity SoC
4 * Copyright (C) 2010-2012 Guan Xuetao
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation, or any later version.
9 * See the COPYING file in the top-level directory.
12 #include "qemu/osdep.h"
13 #include "hw/sysbus.h"
15 #include "hw/ptimer.h"
16 #include "qemu/module.h"
20 #include "hw/unicore32/puv3.h"
22 #define TYPE_PUV3_OST "puv3_ost"
23 #define PUV3_OST(obj) OBJECT_CHECK(PUV3OSTState, (obj), TYPE_PUV3_OST)
25 /* puv3 ostimer implementation. */
26 typedef struct PUV3OSTState
{
27 SysBusDevice parent_obj
;
39 static uint64_t puv3_ost_read(void *opaque
, hwaddr offset
,
42 PUV3OSTState
*s
= opaque
;
46 case 0x10: /* Counter Register */
47 ret
= s
->reg_OSMR0
- (uint32_t)ptimer_get_count(s
->ptimer
);
49 case 0x14: /* Status Register */
52 case 0x1c: /* Interrupt Enable Register */
56 qemu_log_mask(LOG_GUEST_ERROR
,
57 "%s: Bad read offset 0x%"HWADDR_PRIx
"\n",
60 DPRINTF("offset 0x%x, value 0x%x\n", offset
, ret
);
64 static void puv3_ost_write(void *opaque
, hwaddr offset
,
65 uint64_t value
, unsigned size
)
67 PUV3OSTState
*s
= opaque
;
69 DPRINTF("offset 0x%x, value 0x%x\n", offset
, value
);
71 case 0x00: /* Match Register 0 */
72 ptimer_transaction_begin(s
->ptimer
);
74 if (s
->reg_OSMR0
> s
->reg_OSCR
) {
75 ptimer_set_count(s
->ptimer
, s
->reg_OSMR0
- s
->reg_OSCR
);
77 ptimer_set_count(s
->ptimer
, s
->reg_OSMR0
+
78 (0xffffffff - s
->reg_OSCR
));
80 ptimer_run(s
->ptimer
, 2);
81 ptimer_transaction_commit(s
->ptimer
);
83 case 0x14: /* Status Register */
87 qemu_irq_lower(s
->irq
);
90 case 0x1c: /* Interrupt Enable Register */
94 qemu_log_mask(LOG_GUEST_ERROR
,
95 "%s: Bad write offset 0x%"HWADDR_PRIx
"\n",
100 static const MemoryRegionOps puv3_ost_ops
= {
101 .read
= puv3_ost_read
,
102 .write
= puv3_ost_write
,
104 .min_access_size
= 4,
105 .max_access_size
= 4,
107 .endianness
= DEVICE_NATIVE_ENDIAN
,
110 static void puv3_ost_tick(void *opaque
)
112 PUV3OSTState
*s
= opaque
;
114 DPRINTF("ost hit when ptimer counter from 0x%x to 0x%x!\n",
115 s
->reg_OSCR
, s
->reg_OSMR0
);
117 s
->reg_OSCR
= s
->reg_OSMR0
;
120 qemu_irq_raise(s
->irq
);
124 static void puv3_ost_realize(DeviceState
*dev
, Error
**errp
)
126 PUV3OSTState
*s
= PUV3_OST(dev
);
127 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
134 sysbus_init_irq(sbd
, &s
->irq
);
136 s
->ptimer
= ptimer_init(puv3_ost_tick
, s
, PTIMER_POLICY_DEFAULT
);
137 ptimer_transaction_begin(s
->ptimer
);
138 ptimer_set_freq(s
->ptimer
, 50 * 1000 * 1000);
139 ptimer_transaction_commit(s
->ptimer
);
141 memory_region_init_io(&s
->iomem
, OBJECT(s
), &puv3_ost_ops
, s
, "puv3_ost",
143 sysbus_init_mmio(sbd
, &s
->iomem
);
146 static void puv3_ost_class_init(ObjectClass
*klass
, void *data
)
148 DeviceClass
*dc
= DEVICE_CLASS(klass
);
150 dc
->realize
= puv3_ost_realize
;
153 static const TypeInfo puv3_ost_info
= {
154 .name
= TYPE_PUV3_OST
,
155 .parent
= TYPE_SYS_BUS_DEVICE
,
156 .instance_size
= sizeof(PUV3OSTState
),
157 .class_init
= puv3_ost_class_init
,
160 static void puv3_ost_register_type(void)
162 type_register_static(&puv3_ost_info
);
165 type_init(puv3_ost_register_type
)