4 * Copyright (c) 2008 CodeSourcery
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #include "qemu-timer.h"
43 static uint32_t syborg_rtc_read(void *opaque
, target_phys_addr_t offset
)
45 SyborgRTCState
*s
= (SyborgRTCState
*)opaque
;
47 switch (offset
>> 2) {
51 return (uint32_t)s
->data
;
53 return (uint32_t)(s
->data
>> 32);
55 cpu_abort(cpu_single_env
, "syborg_rtc_read: Bad offset %x\n",
61 static void syborg_rtc_write(void *opaque
, target_phys_addr_t offset
, uint32_t value
)
63 SyborgRTCState
*s
= (SyborgRTCState
*)opaque
;
67 switch (offset
>> 2) {
69 now
= qemu_get_clock(vm_clock
);
71 s
->offset
= s
->data
- now
;
73 s
->data
= now
+ s
->offset
;
81 s
->data
= (s
->data
& ~(uint64_t)0xffffffffu
) | value
;
84 s
->data
= (s
->data
& 0xffffffffu
) | ((uint64_t)value
<< 32);
87 cpu_abort(cpu_single_env
, "syborg_rtc_write: Bad offset %x\n",
93 static CPUReadMemoryFunc
*syborg_rtc_readfn
[] = {
99 static CPUWriteMemoryFunc
*syborg_rtc_writefn
[] = {
105 static void syborg_rtc_save(QEMUFile
*f
, void *opaque
)
107 SyborgRTCState
*s
= opaque
;
109 qemu_put_be64(f
, s
->offset
);
110 qemu_put_be64(f
, s
->data
);
113 static int syborg_rtc_load(QEMUFile
*f
, void *opaque
, int version_id
)
115 SyborgRTCState
*s
= opaque
;
120 s
->offset
= qemu_get_be64(f
);
121 s
->data
= qemu_get_be64(f
);
126 static void syborg_rtc_init(SysBusDevice
*dev
)
128 SyborgRTCState
*s
= FROM_SYSBUS(SyborgRTCState
, dev
);
132 iomemtype
= cpu_register_io_memory(syborg_rtc_readfn
,
133 syborg_rtc_writefn
, s
);
134 sysbus_init_mmio(dev
, 0x1000, iomemtype
);
136 qemu_get_timedate(&tm
, 0);
137 s
->offset
= (uint64_t)mktime(&tm
) * 1000000000;
139 register_savevm("syborg_rtc", -1, 1, syborg_rtc_save
, syborg_rtc_load
, s
);
142 static void syborg_rtc_register_devices(void)
144 sysbus_register_dev("syborg,rtc", sizeof(SyborgRTCState
), syborg_rtc_init
);
147 device_init(syborg_rtc_register_devices
)