Merge branch 'master' of git://git.qemu.org/qemu
[qemu.git] / hw / syborg_rtc.c
blobb5f34798b6b85d5d992e372c58639ac569737ef0
1 /*
2 * Syborg RTC
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
22 * THE SOFTWARE.
25 #include "sysbus.h"
26 #include "qemu-timer.h"
27 #include "syborg.h"
29 enum {
30 RTC_ID = 0,
31 RTC_LATCH = 1,
32 RTC_DATA_LOW = 2,
33 RTC_DATA_HIGH = 3
36 typedef struct {
37 SysBusDevice busdev;
38 MemoryRegion iomem;
39 int64_t offset;
40 int64_t data;
41 qemu_irq irq;
42 } SyborgRTCState;
44 static uint64_t syborg_rtc_read(void *opaque, target_phys_addr_t offset,
45 unsigned size)
47 SyborgRTCState *s = (SyborgRTCState *)opaque;
48 offset &= 0xfff;
49 switch (offset >> 2) {
50 case RTC_ID:
51 return SYBORG_ID_RTC;
52 case RTC_DATA_LOW:
53 return (uint32_t)s->data;
54 case RTC_DATA_HIGH:
55 return (uint32_t)(s->data >> 32);
56 default:
57 cpu_abort(cpu_single_env, "syborg_rtc_read: Bad offset %x\n",
58 (int)offset);
59 return 0;
63 static void syborg_rtc_write(void *opaque, target_phys_addr_t offset,
64 uint64_t value, unsigned size)
66 SyborgRTCState *s = (SyborgRTCState *)opaque;
67 uint64_t now;
69 offset &= 0xfff;
70 switch (offset >> 2) {
71 case RTC_LATCH:
72 now = qemu_get_clock_ns(vm_clock);
73 if (value >= 4) {
74 s->offset = s->data - now;
75 } else {
76 s->data = now + s->offset;
77 while (value) {
78 s->data /= 1000;
79 value--;
82 break;
83 case RTC_DATA_LOW:
84 s->data = (s->data & ~(uint64_t)0xffffffffu) | value;
85 break;
86 case RTC_DATA_HIGH:
87 s->data = (s->data & 0xffffffffu) | ((uint64_t)value << 32);
88 break;
89 default:
90 cpu_abort(cpu_single_env, "syborg_rtc_write: Bad offset %x\n",
91 (int)offset);
92 break;
96 static const MemoryRegionOps syborg_rtc_ops = {
97 .read = syborg_rtc_read,
98 .write = syborg_rtc_write,
99 .endianness = DEVICE_NATIVE_ENDIAN,
102 static const VMStateDescription vmstate_syborg_rtc = {
103 .name = "syborg_keyboard",
104 .version_id = 1,
105 .minimum_version_id = 1,
106 .minimum_version_id_old = 1,
107 .fields = (VMStateField[]) {
108 VMSTATE_INT64(offset, SyborgRTCState),
109 VMSTATE_INT64(data, SyborgRTCState),
110 VMSTATE_END_OF_LIST()
114 static int syborg_rtc_init(SysBusDevice *dev)
116 SyborgRTCState *s = FROM_SYSBUS(SyborgRTCState, dev);
117 struct tm tm;
119 memory_region_init_io(&s->iomem, &syborg_rtc_ops, s, "rtc", 0x1000);
120 sysbus_init_mmio(dev, &s->iomem);
122 qemu_get_timedate(&tm, 0);
123 s->offset = (uint64_t)mktime(&tm) * 1000000000;
125 vmstate_register(&dev->qdev, -1, &vmstate_syborg_rtc, s);
126 return 0;
129 static void syborg_rtc_register_devices(void)
131 sysbus_register_dev("syborg,rtc", sizeof(SyborgRTCState), syborg_rtc_init);
134 device_init(syborg_rtc_register_devices)