Statistics
| Branch: | Revision:

root / hw / syborg_rtc.c @ cde844fa

History | View | Annotate | Download (3.9 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 43b8c30d Benoît Canet
    MemoryRegion iomem;
39 4af39611 Paul Brook
    int64_t offset;
40 4af39611 Paul Brook
    int64_t data;
41 4af39611 Paul Brook
    qemu_irq irq;
42 4af39611 Paul Brook
} SyborgRTCState;
43 4af39611 Paul Brook
44 43b8c30d Benoît Canet
static uint64_t syborg_rtc_read(void *opaque, target_phys_addr_t offset,
45 43b8c30d Benoît Canet
                                unsigned size)
46 4af39611 Paul Brook
{
47 4af39611 Paul Brook
    SyborgRTCState *s = (SyborgRTCState *)opaque;
48 4af39611 Paul Brook
    offset &= 0xfff;
49 4af39611 Paul Brook
    switch (offset >> 2) {
50 4af39611 Paul Brook
    case RTC_ID:
51 4af39611 Paul Brook
        return SYBORG_ID_RTC;
52 4af39611 Paul Brook
    case RTC_DATA_LOW:
53 4af39611 Paul Brook
        return (uint32_t)s->data;
54 4af39611 Paul Brook
    case RTC_DATA_HIGH:
55 4af39611 Paul Brook
        return (uint32_t)(s->data >> 32);
56 4af39611 Paul Brook
    default:
57 4af39611 Paul Brook
        cpu_abort(cpu_single_env, "syborg_rtc_read: Bad offset %x\n",
58 4af39611 Paul Brook
                  (int)offset);
59 4af39611 Paul Brook
        return 0;
60 4af39611 Paul Brook
    }
61 4af39611 Paul Brook
}
62 4af39611 Paul Brook
63 43b8c30d Benoît Canet
static void syborg_rtc_write(void *opaque, target_phys_addr_t offset,
64 43b8c30d Benoît Canet
                             uint64_t value, unsigned size)
65 4af39611 Paul Brook
{
66 4af39611 Paul Brook
    SyborgRTCState *s = (SyborgRTCState *)opaque;
67 4af39611 Paul Brook
    uint64_t now;
68 4af39611 Paul Brook
69 4af39611 Paul Brook
    offset &= 0xfff;
70 4af39611 Paul Brook
    switch (offset >> 2) {
71 4af39611 Paul Brook
    case RTC_LATCH:
72 74475455 Paolo Bonzini
        now = qemu_get_clock_ns(vm_clock);
73 4af39611 Paul Brook
        if (value >= 4) {
74 4af39611 Paul Brook
            s->offset = s->data - now;
75 4af39611 Paul Brook
        } else {
76 4af39611 Paul Brook
            s->data = now + s->offset;
77 4af39611 Paul Brook
            while (value) {
78 4af39611 Paul Brook
                s->data /= 1000;
79 4af39611 Paul Brook
                value--;
80 4af39611 Paul Brook
            }
81 4af39611 Paul Brook
        }
82 4af39611 Paul Brook
        break;
83 4af39611 Paul Brook
    case RTC_DATA_LOW:
84 4af39611 Paul Brook
        s->data = (s->data & ~(uint64_t)0xffffffffu) | value;
85 4af39611 Paul Brook
        break;
86 4af39611 Paul Brook
    case RTC_DATA_HIGH:
87 4af39611 Paul Brook
        s->data = (s->data & 0xffffffffu) | ((uint64_t)value << 32);
88 4af39611 Paul Brook
        break;
89 4af39611 Paul Brook
    default:
90 4af39611 Paul Brook
        cpu_abort(cpu_single_env, "syborg_rtc_write: Bad offset %x\n",
91 4af39611 Paul Brook
                  (int)offset);
92 4af39611 Paul Brook
        break;
93 4af39611 Paul Brook
    }
94 4af39611 Paul Brook
}
95 4af39611 Paul Brook
96 43b8c30d Benoît Canet
static const MemoryRegionOps syborg_rtc_ops = {
97 43b8c30d Benoît Canet
    .read = syborg_rtc_read,
98 43b8c30d Benoît Canet
    .write = syborg_rtc_write,
99 43b8c30d Benoît Canet
    .endianness = DEVICE_NATIVE_ENDIAN,
100 4af39611 Paul Brook
};
101 4af39611 Paul Brook
102 4ba673ce Juan Quintela
static const VMStateDescription vmstate_syborg_rtc = {
103 4ba673ce Juan Quintela
    .name = "syborg_keyboard",
104 4ba673ce Juan Quintela
    .version_id = 1,
105 4ba673ce Juan Quintela
    .minimum_version_id = 1,
106 4ba673ce Juan Quintela
    .minimum_version_id_old = 1,
107 4ba673ce Juan Quintela
    .fields      = (VMStateField[]) {
108 4ba673ce Juan Quintela
        VMSTATE_INT64(offset, SyborgRTCState),
109 4ba673ce Juan Quintela
        VMSTATE_INT64(data, SyborgRTCState),
110 4ba673ce Juan Quintela
        VMSTATE_END_OF_LIST()
111 4ba673ce Juan Quintela
    }
112 4ba673ce Juan Quintela
};
113 4af39611 Paul Brook
114 81a322d4 Gerd Hoffmann
static int syborg_rtc_init(SysBusDevice *dev)
115 4af39611 Paul Brook
{
116 4af39611 Paul Brook
    SyborgRTCState *s = FROM_SYSBUS(SyborgRTCState, dev);
117 4af39611 Paul Brook
    struct tm tm;
118 4af39611 Paul Brook
119 43b8c30d Benoît Canet
    memory_region_init_io(&s->iomem, &syborg_rtc_ops, s, "rtc", 0x1000);
120 43b8c30d Benoît Canet
    sysbus_init_mmio_region(dev, &s->iomem);
121 4af39611 Paul Brook
122 4af39611 Paul Brook
    qemu_get_timedate(&tm, 0);
123 4af39611 Paul Brook
    s->offset = (uint64_t)mktime(&tm) * 1000000000;
124 4af39611 Paul Brook
125 4ba673ce Juan Quintela
    vmstate_register(&dev->qdev, -1, &vmstate_syborg_rtc, s);
126 81a322d4 Gerd Hoffmann
    return 0;
127 4af39611 Paul Brook
}
128 4af39611 Paul Brook
129 4af39611 Paul Brook
static void syborg_rtc_register_devices(void)
130 4af39611 Paul Brook
{
131 4af39611 Paul Brook
    sysbus_register_dev("syborg,rtc", sizeof(SyborgRTCState), syborg_rtc_init);
132 4af39611 Paul Brook
}
133 4af39611 Paul Brook
134 4af39611 Paul Brook
device_init(syborg_rtc_register_devices)