Statistics
| Branch: | Revision:

root / hw / syborg_rtc.c @ 6df658f5

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 74475455 Paolo Bonzini
        now = qemu_get_clock_ns(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 4ba673ce Juan Quintela
static const VMStateDescription vmstate_syborg_rtc = {
106 4ba673ce Juan Quintela
    .name = "syborg_keyboard",
107 4ba673ce Juan Quintela
    .version_id = 1,
108 4ba673ce Juan Quintela
    .minimum_version_id = 1,
109 4ba673ce Juan Quintela
    .minimum_version_id_old = 1,
110 4ba673ce Juan Quintela
    .fields      = (VMStateField[]) {
111 4ba673ce Juan Quintela
        VMSTATE_INT64(offset, SyborgRTCState),
112 4ba673ce Juan Quintela
        VMSTATE_INT64(data, SyborgRTCState),
113 4ba673ce Juan Quintela
        VMSTATE_END_OF_LIST()
114 4ba673ce Juan Quintela
    }
115 4ba673ce Juan Quintela
};
116 4af39611 Paul Brook
117 81a322d4 Gerd Hoffmann
static int syborg_rtc_init(SysBusDevice *dev)
118 4af39611 Paul Brook
{
119 4af39611 Paul Brook
    SyborgRTCState *s = FROM_SYSBUS(SyborgRTCState, dev);
120 4af39611 Paul Brook
    struct tm tm;
121 4af39611 Paul Brook
    int iomemtype;
122 4af39611 Paul Brook
123 1eed09cb Avi Kivity
    iomemtype = cpu_register_io_memory(syborg_rtc_readfn,
124 2507c12a Alexander Graf
                                       syborg_rtc_writefn, s,
125 2507c12a Alexander Graf
                                       DEVICE_NATIVE_ENDIAN);
126 4af39611 Paul Brook
    sysbus_init_mmio(dev, 0x1000, iomemtype);
127 4af39611 Paul Brook
128 4af39611 Paul Brook
    qemu_get_timedate(&tm, 0);
129 4af39611 Paul Brook
    s->offset = (uint64_t)mktime(&tm) * 1000000000;
130 4af39611 Paul Brook
131 4ba673ce Juan Quintela
    vmstate_register(&dev->qdev, -1, &vmstate_syborg_rtc, s);
132 81a322d4 Gerd Hoffmann
    return 0;
133 4af39611 Paul Brook
}
134 4af39611 Paul Brook
135 4af39611 Paul Brook
static void syborg_rtc_register_devices(void)
136 4af39611 Paul Brook
{
137 4af39611 Paul Brook
    sysbus_register_dev("syborg,rtc", sizeof(SyborgRTCState), syborg_rtc_init);
138 4af39611 Paul Brook
}
139 4af39611 Paul Brook
140 4af39611 Paul Brook
device_init(syborg_rtc_register_devices)