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 "hw/sysbus.h"
27 #include "qemu/timer.h"
45 static uint64_t syborg_rtc_read(void *opaque
, hwaddr offset
,
48 SyborgRTCState
*s
= (SyborgRTCState
*)opaque
;
50 switch (offset
>> 2) {
54 return (uint32_t)s
->data
;
56 return (uint32_t)(s
->data
>> 32);
58 cpu_abort(cpu_single_env
, "syborg_rtc_read: Bad offset %x\n",
64 static void syborg_rtc_write(void *opaque
, hwaddr offset
,
65 uint64_t value
, unsigned size
)
67 SyborgRTCState
*s
= (SyborgRTCState
*)opaque
;
71 switch (offset
>> 2) {
73 now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
75 s
->offset
= s
->data
- now
;
77 s
->data
= now
+ s
->offset
;
85 s
->data
= (s
->data
& ~(uint64_t)0xffffffffu
) | value
;
88 s
->data
= (s
->data
& 0xffffffffu
) | ((uint64_t)value
<< 32);
91 cpu_abort(cpu_single_env
, "syborg_rtc_write: Bad offset %x\n",
97 static const MemoryRegionOps syborg_rtc_ops
= {
98 .read
= syborg_rtc_read
,
99 .write
= syborg_rtc_write
,
100 .endianness
= DEVICE_NATIVE_ENDIAN
,
103 static const VMStateDescription vmstate_syborg_rtc
= {
104 .name
= "syborg_keyboard",
106 .minimum_version_id
= 1,
107 .minimum_version_id_old
= 1,
108 .fields
= (VMStateField
[]) {
109 VMSTATE_INT64(offset
, SyborgRTCState
),
110 VMSTATE_INT64(data
, SyborgRTCState
),
111 VMSTATE_END_OF_LIST()
115 static int syborg_rtc_init(SysBusDevice
*sbd
)
117 DeviceState
*dev
= DEVICE(sbd
);
118 SyborgRTCState
*s
= SYBORG_RTC(dev
);
121 memory_region_init_io(&s
->iomem
, &syborg_rtc_ops
, s
, "rtc", 0x1000);
122 sysbus_init_mmio(sbd
, &s
->iomem
);
124 qemu_get_timedate(&tm
, 0);
125 s
->offset
= (uint64_t)mktime(&tm
) * 1000000000;
127 vmstate_register(&dev
->qdev
, -1, &vmstate_syborg_rtc
, s
);
131 static void syborg_rtc_class_init(ObjectClass
*klass
, void *data
)
133 DeviceClass
*dc
= DEVICE_CLASS(klass
);
134 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
135 dc
->desc
= "syborg rtc";
136 //~ dc->reset = syborg_rtc_reset;
137 //~ dc->vmsd = &syborg_rtc_vmsd;
138 k
->init
= syborg_rtc_init
;
141 static const TypeInfo syborg_rtc_info
= {
142 .name
= "syborg,rtc",
143 .parent
= TYPE_SYS_BUS_DEVICE
,
144 .instance_size
= sizeof(SyborgRTCState
),
145 .class_init
= syborg_rtc_class_init
148 static void syborg_rtc_register_types(void)
150 type_register_static(&syborg_rtc_info
);
153 type_init(syborg_rtc_register_types
)