4 * Copyright (c) 2008 CodeSourcery
5 * Copyright (c) 2010, 2013 Stefan Weil
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #include "qemu/osdep.h"
27 #include "hw/sysbus.h"
28 #include "qemu/timer.h"
46 static uint64_t syborg_rtc_read(void *opaque
, hwaddr offset
,
49 SyborgRTCState
*s
= (SyborgRTCState
*)opaque
;
51 switch (offset
>> 2) {
55 return (uint32_t)s
->data
;
57 return (uint32_t)(s
->data
>> 32);
59 cpu_abort(cpu_single_env
, "syborg_rtc_read: Bad offset %x\n",
65 static void syborg_rtc_write(void *opaque
, hwaddr offset
,
66 uint64_t value
, unsigned size
)
68 SyborgRTCState
*s
= (SyborgRTCState
*)opaque
;
72 switch (offset
>> 2) {
74 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
76 s
->offset
= s
->data
- now
;
78 s
->data
= now
+ s
->offset
;
86 s
->data
= (s
->data
& ~(uint64_t)0xffffffffu
) | value
;
89 s
->data
= (s
->data
& 0xffffffffu
) | ((uint64_t)value
<< 32);
92 cpu_abort(cpu_single_env
, "syborg_rtc_write: Bad offset %x\n",
98 static const MemoryRegionOps syborg_rtc_ops
= {
99 .read
= syborg_rtc_read
,
100 .write
= syborg_rtc_write
,
101 .endianness
= DEVICE_NATIVE_ENDIAN
,
104 static const VMStateDescription vmstate_syborg_rtc
= {
105 .name
= "syborg_keyboard",
107 .minimum_version_id
= 1,
108 .minimum_version_id_old
= 1,
109 .fields
= (VMStateField
[]) {
110 VMSTATE_INT64(offset
, SyborgRTCState
),
111 VMSTATE_INT64(data
, SyborgRTCState
),
112 VMSTATE_END_OF_LIST()
116 static int syborg_rtc_init(SysBusDevice
*sbd
)
118 DeviceState
*dev
= DEVICE(sbd
);
119 SyborgRTCState
*s
= SYBORG_RTC(dev
);
122 memory_region_init_io(&s
->iomem
, &syborg_rtc_ops
, s
, "rtc", 0x1000);
123 sysbus_init_mmio(sbd
, &s
->iomem
);
125 qemu_get_timedate(&tm
, 0);
126 s
->offset
= (uint64_t)mktime(&tm
) * 1000000000;
128 vmstate_register(&dev
->qdev
, -1, &vmstate_syborg_rtc
, s
);
132 static void syborg_rtc_class_init(ObjectClass
*klass
, void *data
)
134 DeviceClass
*dc
= DEVICE_CLASS(klass
);
135 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
136 dc
->desc
= "syborg rtc";
137 //~ dc->reset = syborg_rtc_reset;
138 //~ dc->vmsd = &syborg_rtc_vmsd;
139 k
->init
= syborg_rtc_init
;
142 static const TypeInfo syborg_rtc_info
= {
143 .name
= "syborg,rtc",
144 .parent
= TYPE_SYS_BUS_DEVICE
,
145 .instance_size
= sizeof(SyborgRTCState
),
146 .class_init
= syborg_rtc_class_init
149 static void syborg_rtc_register_types(void)
151 type_register_static(&syborg_rtc_info
);
154 type_init(syborg_rtc_register_types
)