root / hw / syborg_rtc.c @ dd4239d6
History | View | Annotate | Download (4 kB)
1 | 4af39611 | Paul Brook | /*
|
---|---|---|---|
2 | 4af39611 | Paul Brook | * Syborg RTC
|
3 | 4af39611 | Paul Brook | *
|
4 | 4af39611 | Paul Brook | * Copyright (c) 2008 CodeSourcery
|
5 | 4af39611 | Paul Brook | *
|
6 | 4af39611 | Paul Brook | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 4af39611 | Paul Brook | * of this software and associated documentation files (the "Software"), to deal
|
8 | 4af39611 | Paul Brook | * in the Software without restriction, including without limitation the rights
|
9 | 4af39611 | Paul Brook | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 4af39611 | Paul Brook | * copies of the Software, and to permit persons to whom the Software is
|
11 | 4af39611 | Paul Brook | * furnished to do so, subject to the following conditions:
|
12 | 4af39611 | Paul Brook | *
|
13 | 4af39611 | Paul Brook | * The above copyright notice and this permission notice shall be included in
|
14 | 4af39611 | Paul Brook | * all copies or substantial portions of the Software.
|
15 | 4af39611 | Paul Brook | *
|
16 | 4af39611 | Paul Brook | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 4af39611 | Paul Brook | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 4af39611 | Paul Brook | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 4af39611 | Paul Brook | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 4af39611 | Paul Brook | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 4af39611 | Paul Brook | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 4af39611 | Paul Brook | * THE SOFTWARE.
|
23 | 4af39611 | Paul Brook | */
|
24 | 4af39611 | Paul Brook | |
25 | 4af39611 | Paul Brook | #include "sysbus.h" |
26 | 4af39611 | Paul Brook | #include "qemu-timer.h" |
27 | 4af39611 | Paul Brook | #include "syborg.h" |
28 | 4af39611 | Paul Brook | |
29 | 4af39611 | Paul Brook | enum {
|
30 | 4af39611 | Paul Brook | RTC_ID = 0,
|
31 | 4af39611 | Paul Brook | RTC_LATCH = 1,
|
32 | 4af39611 | Paul Brook | RTC_DATA_LOW = 2,
|
33 | 4af39611 | Paul Brook | RTC_DATA_HIGH = 3
|
34 | 4af39611 | Paul Brook | }; |
35 | 4af39611 | Paul Brook | |
36 | 4af39611 | Paul Brook | typedef struct { |
37 | 4af39611 | Paul Brook | SysBusDevice busdev; |
38 | 4af39611 | Paul Brook | int64_t offset; |
39 | 4af39611 | Paul Brook | int64_t data; |
40 | 4af39611 | Paul Brook | qemu_irq irq; |
41 | 4af39611 | Paul Brook | } SyborgRTCState; |
42 | 4af39611 | Paul Brook | |
43 | c227f099 | Anthony Liguori | static uint32_t syborg_rtc_read(void *opaque, target_phys_addr_t offset) |
44 | 4af39611 | Paul Brook | { |
45 | 4af39611 | Paul Brook | SyborgRTCState *s = (SyborgRTCState *)opaque; |
46 | 4af39611 | Paul Brook | offset &= 0xfff;
|
47 | 4af39611 | Paul Brook | switch (offset >> 2) { |
48 | 4af39611 | Paul Brook | case RTC_ID:
|
49 | 4af39611 | Paul Brook | return SYBORG_ID_RTC;
|
50 | 4af39611 | Paul Brook | case RTC_DATA_LOW:
|
51 | 4af39611 | Paul Brook | return (uint32_t)s->data;
|
52 | 4af39611 | Paul Brook | case RTC_DATA_HIGH:
|
53 | 4af39611 | Paul Brook | return (uint32_t)(s->data >> 32); |
54 | 4af39611 | Paul Brook | default:
|
55 | 4af39611 | Paul Brook | cpu_abort(cpu_single_env, "syborg_rtc_read: Bad offset %x\n",
|
56 | 4af39611 | Paul Brook | (int)offset);
|
57 | 4af39611 | Paul Brook | return 0; |
58 | 4af39611 | Paul Brook | } |
59 | 4af39611 | Paul Brook | } |
60 | 4af39611 | Paul Brook | |
61 | c227f099 | Anthony Liguori | static void syborg_rtc_write(void *opaque, target_phys_addr_t offset, uint32_t value) |
62 | 4af39611 | Paul Brook | { |
63 | 4af39611 | Paul Brook | SyborgRTCState *s = (SyborgRTCState *)opaque; |
64 | 4af39611 | Paul Brook | uint64_t now; |
65 | 4af39611 | Paul Brook | |
66 | 4af39611 | Paul Brook | offset &= 0xfff;
|
67 | 4af39611 | Paul Brook | switch (offset >> 2) { |
68 | 4af39611 | Paul Brook | case RTC_LATCH:
|
69 | 4af39611 | Paul Brook | now = qemu_get_clock(vm_clock); |
70 | 4af39611 | Paul Brook | if (value >= 4) { |
71 | 4af39611 | Paul Brook | s->offset = s->data - now; |
72 | 4af39611 | Paul Brook | } else {
|
73 | 4af39611 | Paul Brook | s->data = now + s->offset; |
74 | 4af39611 | Paul Brook | while (value) {
|
75 | 4af39611 | Paul Brook | s->data /= 1000;
|
76 | 4af39611 | Paul Brook | value--; |
77 | 4af39611 | Paul Brook | } |
78 | 4af39611 | Paul Brook | } |
79 | 4af39611 | Paul Brook | break;
|
80 | 4af39611 | Paul Brook | case RTC_DATA_LOW:
|
81 | 4af39611 | Paul Brook | s->data = (s->data & ~(uint64_t)0xffffffffu) | value;
|
82 | 4af39611 | Paul Brook | break;
|
83 | 4af39611 | Paul Brook | case RTC_DATA_HIGH:
|
84 | 4af39611 | Paul Brook | s->data = (s->data & 0xffffffffu) | ((uint64_t)value << 32); |
85 | 4af39611 | Paul Brook | break;
|
86 | 4af39611 | Paul Brook | default:
|
87 | 4af39611 | Paul Brook | cpu_abort(cpu_single_env, "syborg_rtc_write: Bad offset %x\n",
|
88 | 4af39611 | Paul Brook | (int)offset);
|
89 | 4af39611 | Paul Brook | break;
|
90 | 4af39611 | Paul Brook | } |
91 | 4af39611 | Paul Brook | } |
92 | 4af39611 | Paul Brook | |
93 | d60efc6b | Blue Swirl | static CPUReadMemoryFunc * const syborg_rtc_readfn[] = { |
94 | 4af39611 | Paul Brook | syborg_rtc_read, |
95 | 4af39611 | Paul Brook | syborg_rtc_read, |
96 | 4af39611 | Paul Brook | syborg_rtc_read |
97 | 4af39611 | Paul Brook | }; |
98 | 4af39611 | Paul Brook | |
99 | d60efc6b | Blue Swirl | static CPUWriteMemoryFunc * const syborg_rtc_writefn[] = { |
100 | 4af39611 | Paul Brook | syborg_rtc_write, |
101 | 4af39611 | Paul Brook | syborg_rtc_write, |
102 | 4af39611 | Paul Brook | syborg_rtc_write |
103 | 4af39611 | Paul Brook | }; |
104 | 4af39611 | Paul Brook | |
105 | 4af39611 | Paul Brook | static void syborg_rtc_save(QEMUFile *f, void *opaque) |
106 | 4af39611 | Paul Brook | { |
107 | 4af39611 | Paul Brook | SyborgRTCState *s = opaque; |
108 | 4af39611 | Paul Brook | |
109 | 4af39611 | Paul Brook | qemu_put_be64(f, s->offset); |
110 | 4af39611 | Paul Brook | qemu_put_be64(f, s->data); |
111 | 4af39611 | Paul Brook | } |
112 | 4af39611 | Paul Brook | |
113 | 4af39611 | Paul Brook | static int syborg_rtc_load(QEMUFile *f, void *opaque, int version_id) |
114 | 4af39611 | Paul Brook | { |
115 | 4af39611 | Paul Brook | SyborgRTCState *s = opaque; |
116 | 4af39611 | Paul Brook | |
117 | 4af39611 | Paul Brook | if (version_id != 1) |
118 | 4af39611 | Paul Brook | return -EINVAL;
|
119 | 4af39611 | Paul Brook | |
120 | 4af39611 | Paul Brook | s->offset = qemu_get_be64(f); |
121 | 4af39611 | Paul Brook | s->data = qemu_get_be64(f); |
122 | 4af39611 | Paul Brook | |
123 | 4af39611 | Paul Brook | return 0; |
124 | 4af39611 | Paul Brook | } |
125 | 4af39611 | Paul Brook | |
126 | 81a322d4 | Gerd Hoffmann | static int syborg_rtc_init(SysBusDevice *dev) |
127 | 4af39611 | Paul Brook | { |
128 | 4af39611 | Paul Brook | SyborgRTCState *s = FROM_SYSBUS(SyborgRTCState, dev); |
129 | 4af39611 | Paul Brook | struct tm tm;
|
130 | 4af39611 | Paul Brook | int iomemtype;
|
131 | 4af39611 | Paul Brook | |
132 | 1eed09cb | Avi Kivity | iomemtype = cpu_register_io_memory(syborg_rtc_readfn, |
133 | 4af39611 | Paul Brook | syborg_rtc_writefn, s); |
134 | 4af39611 | Paul Brook | sysbus_init_mmio(dev, 0x1000, iomemtype);
|
135 | 4af39611 | Paul Brook | |
136 | 4af39611 | Paul Brook | qemu_get_timedate(&tm, 0);
|
137 | 4af39611 | Paul Brook | s->offset = (uint64_t)mktime(&tm) * 1000000000;
|
138 | 4af39611 | Paul Brook | |
139 | 4af39611 | Paul Brook | register_savevm("syborg_rtc", -1, 1, syborg_rtc_save, syborg_rtc_load, s); |
140 | 81a322d4 | Gerd Hoffmann | return 0; |
141 | 4af39611 | Paul Brook | } |
142 | 4af39611 | Paul Brook | |
143 | 4af39611 | Paul Brook | static void syborg_rtc_register_devices(void) |
144 | 4af39611 | Paul Brook | { |
145 | 4af39611 | Paul Brook | sysbus_register_dev("syborg,rtc", sizeof(SyborgRTCState), syborg_rtc_init); |
146 | 4af39611 | Paul Brook | } |
147 | 4af39611 | Paul Brook | |
148 | 4af39611 | Paul Brook | device_init(syborg_rtc_register_devices) |