Statistics
| Branch: | Revision:

root / hw / pl031.c @ 5ea3c2b4

History | View | Annotate | Download (5.9 kB)

1 7e1543c2 pbrook
/*
2 7e1543c2 pbrook
 * ARM AMBA PrimeCell PL031 RTC
3 7e1543c2 pbrook
 *
4 7e1543c2 pbrook
 * Copyright (c) 2007 CodeSourcery
5 7e1543c2 pbrook
 *
6 7e1543c2 pbrook
 * This file is free software; you can redistribute it and/or modify
7 7e1543c2 pbrook
 * it under the terms of the GNU General Public License version 2 as
8 7e1543c2 pbrook
 * published by the Free Software Foundation.
9 7e1543c2 pbrook
 *
10 7e1543c2 pbrook
 */
11 7e1543c2 pbrook
12 a63bdb31 Paul Brook
#include "sysbus.h"
13 87ecb68b pbrook
#include "qemu-timer.h"
14 7e1543c2 pbrook
15 7e1543c2 pbrook
//#define DEBUG_PL031
16 7e1543c2 pbrook
17 7e1543c2 pbrook
#ifdef DEBUG_PL031
18 001faf32 Blue Swirl
#define DPRINTF(fmt, ...) \
19 001faf32 Blue Swirl
do { printf("pl031: " fmt , ## __VA_ARGS__); } while (0)
20 7e1543c2 pbrook
#else
21 001faf32 Blue Swirl
#define DPRINTF(fmt, ...) do {} while(0)
22 7e1543c2 pbrook
#endif
23 7e1543c2 pbrook
24 7e1543c2 pbrook
#define RTC_DR      0x00    /* Data read register */
25 7e1543c2 pbrook
#define RTC_MR      0x04    /* Match register */
26 7e1543c2 pbrook
#define RTC_LR      0x08    /* Data load register */
27 7e1543c2 pbrook
#define RTC_CR      0x0c    /* Control register */
28 7e1543c2 pbrook
#define RTC_IMSC    0x10    /* Interrupt mask and set register */
29 7e1543c2 pbrook
#define RTC_RIS     0x14    /* Raw interrupt status register */
30 7e1543c2 pbrook
#define RTC_MIS     0x18    /* Masked interrupt status register */
31 7e1543c2 pbrook
#define RTC_ICR     0x1c    /* Interrupt clear register */
32 7e1543c2 pbrook
33 7e1543c2 pbrook
typedef struct {
34 a63bdb31 Paul Brook
    SysBusDevice busdev;
35 7e1543c2 pbrook
    QEMUTimer *timer;
36 7e1543c2 pbrook
    qemu_irq irq;
37 7e1543c2 pbrook
38 7e1543c2 pbrook
    uint32_t tick_offset;
39 7e1543c2 pbrook
40 7e1543c2 pbrook
    uint32_t mr;
41 7e1543c2 pbrook
    uint32_t lr;
42 7e1543c2 pbrook
    uint32_t cr;
43 7e1543c2 pbrook
    uint32_t im;
44 7e1543c2 pbrook
    uint32_t is;
45 7e1543c2 pbrook
} pl031_state;
46 7e1543c2 pbrook
47 0dc5595c Peter Maydell
static const VMStateDescription vmstate_pl031 = {
48 0dc5595c Peter Maydell
    .name = "pl031",
49 0dc5595c Peter Maydell
    .version_id = 1,
50 0dc5595c Peter Maydell
    .minimum_version_id = 1,
51 0dc5595c Peter Maydell
    .fields = (VMStateField[]) {
52 0dc5595c Peter Maydell
        VMSTATE_UINT32(tick_offset, pl031_state),
53 0dc5595c Peter Maydell
        VMSTATE_UINT32(mr, pl031_state),
54 0dc5595c Peter Maydell
        VMSTATE_UINT32(lr, pl031_state),
55 0dc5595c Peter Maydell
        VMSTATE_UINT32(cr, pl031_state),
56 0dc5595c Peter Maydell
        VMSTATE_UINT32(im, pl031_state),
57 0dc5595c Peter Maydell
        VMSTATE_UINT32(is, pl031_state),
58 0dc5595c Peter Maydell
        VMSTATE_END_OF_LIST()
59 0dc5595c Peter Maydell
    }
60 0dc5595c Peter Maydell
};
61 0dc5595c Peter Maydell
62 7e1543c2 pbrook
static const unsigned char pl031_id[] = {
63 7e1543c2 pbrook
    0x31, 0x10, 0x14, 0x00,         /* Device ID        */
64 7e1543c2 pbrook
    0x0d, 0xf0, 0x05, 0xb1          /* Cell ID      */
65 7e1543c2 pbrook
};
66 7e1543c2 pbrook
67 7e1543c2 pbrook
static void pl031_update(pl031_state *s)
68 7e1543c2 pbrook
{
69 7e1543c2 pbrook
    qemu_set_irq(s->irq, s->is & s->im);
70 7e1543c2 pbrook
}
71 7e1543c2 pbrook
72 7e1543c2 pbrook
static void pl031_interrupt(void * opaque)
73 7e1543c2 pbrook
{
74 7e1543c2 pbrook
    pl031_state *s = (pl031_state *)opaque;
75 7e1543c2 pbrook
76 7e1543c2 pbrook
    s->im = 1;
77 7e1543c2 pbrook
    DPRINTF("Alarm raised\n");
78 7e1543c2 pbrook
    pl031_update(s);
79 7e1543c2 pbrook
}
80 7e1543c2 pbrook
81 7e1543c2 pbrook
static uint32_t pl031_get_count(pl031_state *s)
82 7e1543c2 pbrook
{
83 74475455 Paolo Bonzini
    /* This assumes qemu_get_clock_ns returns the time since the machine was
84 7e1543c2 pbrook
       created.  */
85 74475455 Paolo Bonzini
    return s->tick_offset + qemu_get_clock_ns(vm_clock) / get_ticks_per_sec();
86 7e1543c2 pbrook
}
87 7e1543c2 pbrook
88 7e1543c2 pbrook
static void pl031_set_alarm(pl031_state *s)
89 7e1543c2 pbrook
{
90 7e1543c2 pbrook
    int64_t now;
91 7e1543c2 pbrook
    uint32_t ticks;
92 7e1543c2 pbrook
93 74475455 Paolo Bonzini
    now = qemu_get_clock_ns(vm_clock);
94 6ee093c9 Juan Quintela
    ticks = s->tick_offset + now / get_ticks_per_sec();
95 7e1543c2 pbrook
96 7e1543c2 pbrook
    /* The timer wraps around.  This subtraction also wraps in the same way,
97 7e1543c2 pbrook
       and gives correct results when alarm < now_ticks.  */
98 7e1543c2 pbrook
    ticks = s->mr - ticks;
99 7e1543c2 pbrook
    DPRINTF("Alarm set in %ud ticks\n", ticks);
100 7e1543c2 pbrook
    if (ticks == 0) {
101 7e1543c2 pbrook
        qemu_del_timer(s->timer);
102 7e1543c2 pbrook
        pl031_interrupt(s);
103 7e1543c2 pbrook
    } else {
104 6ee093c9 Juan Quintela
        qemu_mod_timer(s->timer, now + (int64_t)ticks * get_ticks_per_sec());
105 7e1543c2 pbrook
    }
106 7e1543c2 pbrook
}
107 7e1543c2 pbrook
108 c227f099 Anthony Liguori
static uint32_t pl031_read(void *opaque, target_phys_addr_t offset)
109 7e1543c2 pbrook
{
110 7e1543c2 pbrook
    pl031_state *s = (pl031_state *)opaque;
111 7e1543c2 pbrook
112 7e1543c2 pbrook
    if (offset >= 0xfe0  &&  offset < 0x1000)
113 7e1543c2 pbrook
        return pl031_id[(offset - 0xfe0) >> 2];
114 7e1543c2 pbrook
115 7e1543c2 pbrook
    switch (offset) {
116 7e1543c2 pbrook
    case RTC_DR:
117 7e1543c2 pbrook
        return pl031_get_count(s);
118 7e1543c2 pbrook
    case RTC_MR:
119 7e1543c2 pbrook
        return s->mr;
120 7e1543c2 pbrook
    case RTC_IMSC:
121 7e1543c2 pbrook
        return s->im;
122 7e1543c2 pbrook
    case RTC_RIS:
123 7e1543c2 pbrook
        return s->is;
124 7e1543c2 pbrook
    case RTC_LR:
125 7e1543c2 pbrook
        return s->lr;
126 7e1543c2 pbrook
    case RTC_CR:
127 7e1543c2 pbrook
        /* RTC is permanently enabled.  */
128 7e1543c2 pbrook
        return 1;
129 7e1543c2 pbrook
    case RTC_MIS:
130 7e1543c2 pbrook
        return s->is & s->im;
131 7e1543c2 pbrook
    case RTC_ICR:
132 7e1543c2 pbrook
        fprintf(stderr, "qemu: pl031_read: Unexpected offset 0x%x\n",
133 7e1543c2 pbrook
                (int)offset);
134 7e1543c2 pbrook
        break;
135 7e1543c2 pbrook
    default:
136 2ac71179 Paul Brook
        hw_error("pl031_read: Bad offset 0x%x\n", (int)offset);
137 7e1543c2 pbrook
        break;
138 7e1543c2 pbrook
    }
139 7e1543c2 pbrook
140 7e1543c2 pbrook
    return 0;
141 7e1543c2 pbrook
}
142 7e1543c2 pbrook
143 c227f099 Anthony Liguori
static void pl031_write(void * opaque, target_phys_addr_t offset,
144 7e1543c2 pbrook
                        uint32_t value)
145 7e1543c2 pbrook
{
146 7e1543c2 pbrook
    pl031_state *s = (pl031_state *)opaque;
147 7e1543c2 pbrook
148 7e1543c2 pbrook
149 7e1543c2 pbrook
    switch (offset) {
150 7e1543c2 pbrook
    case RTC_LR:
151 7e1543c2 pbrook
        s->tick_offset += value - pl031_get_count(s);
152 7e1543c2 pbrook
        pl031_set_alarm(s);
153 7e1543c2 pbrook
        break;
154 7e1543c2 pbrook
    case RTC_MR:
155 7e1543c2 pbrook
        s->mr = value;
156 7e1543c2 pbrook
        pl031_set_alarm(s);
157 7e1543c2 pbrook
        break;
158 7e1543c2 pbrook
    case RTC_IMSC:
159 7e1543c2 pbrook
        s->im = value & 1;
160 7e1543c2 pbrook
        DPRINTF("Interrupt mask %d\n", s->im);
161 7e1543c2 pbrook
        pl031_update(s);
162 7e1543c2 pbrook
        break;
163 7e1543c2 pbrook
    case RTC_ICR:
164 ff2712ba Stefan Weil
        /* The PL031 documentation (DDI0224B) states that the interrupt is
165 7e1543c2 pbrook
           cleared when bit 0 of the written value is set.  However the
166 7e1543c2 pbrook
           arm926e documentation (DDI0287B) states that the interrupt is
167 7e1543c2 pbrook
           cleared when any value is written.  */
168 7e1543c2 pbrook
        DPRINTF("Interrupt cleared");
169 7e1543c2 pbrook
        s->is = 0;
170 7e1543c2 pbrook
        pl031_update(s);
171 7e1543c2 pbrook
        break;
172 7e1543c2 pbrook
    case RTC_CR:
173 7e1543c2 pbrook
        /* Written value is ignored.  */
174 7e1543c2 pbrook
        break;
175 7e1543c2 pbrook
176 7e1543c2 pbrook
    case RTC_DR:
177 7e1543c2 pbrook
    case RTC_MIS:
178 7e1543c2 pbrook
    case RTC_RIS:
179 7e1543c2 pbrook
        fprintf(stderr, "qemu: pl031_write: Unexpected offset 0x%x\n",
180 7e1543c2 pbrook
                (int)offset);
181 7e1543c2 pbrook
        break;
182 7e1543c2 pbrook
183 7e1543c2 pbrook
    default:
184 2ac71179 Paul Brook
        hw_error("pl031_write: Bad offset 0x%x\n", (int)offset);
185 7e1543c2 pbrook
        break;
186 7e1543c2 pbrook
    }
187 7e1543c2 pbrook
}
188 7e1543c2 pbrook
189 d60efc6b Blue Swirl
static CPUWriteMemoryFunc * const  pl031_writefn[] = {
190 7e1543c2 pbrook
    pl031_write,
191 7e1543c2 pbrook
    pl031_write,
192 7e1543c2 pbrook
    pl031_write
193 7e1543c2 pbrook
};
194 7e1543c2 pbrook
195 d60efc6b Blue Swirl
static CPUReadMemoryFunc * const  pl031_readfn[] = {
196 7e1543c2 pbrook
    pl031_read,
197 7e1543c2 pbrook
    pl031_read,
198 7e1543c2 pbrook
    pl031_read
199 7e1543c2 pbrook
};
200 7e1543c2 pbrook
201 81a322d4 Gerd Hoffmann
static int pl031_init(SysBusDevice *dev)
202 7e1543c2 pbrook
{
203 7e1543c2 pbrook
    int iomemtype;
204 a63bdb31 Paul Brook
    pl031_state *s = FROM_SYSBUS(pl031_state, dev);
205 f6503059 balrog
    struct tm tm;
206 7e1543c2 pbrook
207 2507c12a Alexander Graf
    iomemtype = cpu_register_io_memory(pl031_readfn, pl031_writefn, s,
208 2507c12a Alexander Graf
                                       DEVICE_NATIVE_ENDIAN);
209 2ac71179 Paul Brook
    if (iomemtype == -1) {
210 2ac71179 Paul Brook
        hw_error("pl031_init: Can't register I/O memory\n");
211 2ac71179 Paul Brook
    }
212 7e1543c2 pbrook
213 a63bdb31 Paul Brook
    sysbus_init_mmio(dev, 0x1000, iomemtype);
214 7e1543c2 pbrook
215 a63bdb31 Paul Brook
    sysbus_init_irq(dev, &s->irq);
216 7e1543c2 pbrook
    /* ??? We assume vm_clock is zero at this point.  */
217 f6503059 balrog
    qemu_get_timedate(&tm, 0);
218 0cd2df75 aurel32
    s->tick_offset = mktimegm(&tm);
219 7e1543c2 pbrook
220 74475455 Paolo Bonzini
    s->timer = qemu_new_timer_ns(vm_clock, pl031_interrupt, s);
221 81a322d4 Gerd Hoffmann
    return 0;
222 7e1543c2 pbrook
}
223 a63bdb31 Paul Brook
224 0dc5595c Peter Maydell
static SysBusDeviceInfo pl031_info = {
225 0dc5595c Peter Maydell
    .init = pl031_init,
226 0dc5595c Peter Maydell
    .qdev.name = "pl031",
227 0dc5595c Peter Maydell
    .qdev.size = sizeof(pl031_state),
228 0dc5595c Peter Maydell
    .qdev.vmsd = &vmstate_pl031,
229 0dc5595c Peter Maydell
    .qdev.no_user = 1,
230 0dc5595c Peter Maydell
};
231 0dc5595c Peter Maydell
232 a63bdb31 Paul Brook
static void pl031_register_devices(void)
233 a63bdb31 Paul Brook
{
234 0dc5595c Peter Maydell
    sysbus_register_withprop(&pl031_info);
235 a63bdb31 Paul Brook
}
236 a63bdb31 Paul Brook
237 a63bdb31 Paul Brook
device_init(pl031_register_devices)