Statistics
| Branch: | Revision:

root / hw / milkymist-sysctl.c @ 0dad6c35

History | View | Annotate | Download (8.6 kB)

1 96832424 Michael Walle
/*
2 96832424 Michael Walle
 *  QEMU model of the Milkymist System Controller.
3 96832424 Michael Walle
 *
4 96832424 Michael Walle
 *  Copyright (c) 2010 Michael Walle <michael@walle.cc>
5 96832424 Michael Walle
 *
6 96832424 Michael Walle
 * This library is free software; you can redistribute it and/or
7 96832424 Michael Walle
 * modify it under the terms of the GNU Lesser General Public
8 96832424 Michael Walle
 * License as published by the Free Software Foundation; either
9 96832424 Michael Walle
 * version 2 of the License, or (at your option) any later version.
10 96832424 Michael Walle
 *
11 96832424 Michael Walle
 * This library is distributed in the hope that it will be useful,
12 96832424 Michael Walle
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 96832424 Michael Walle
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 96832424 Michael Walle
 * Lesser General Public License for more details.
15 96832424 Michael Walle
 *
16 96832424 Michael Walle
 * You should have received a copy of the GNU Lesser General Public
17 96832424 Michael Walle
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 96832424 Michael Walle
 *
19 96832424 Michael Walle
 *
20 96832424 Michael Walle
 * Specification available at:
21 96832424 Michael Walle
 *   http://www.milkymist.org/socdoc/sysctl.pdf
22 96832424 Michael Walle
 */
23 96832424 Michael Walle
24 96832424 Michael Walle
#include "hw.h"
25 96832424 Michael Walle
#include "sysbus.h"
26 96832424 Michael Walle
#include "sysemu.h"
27 96832424 Michael Walle
#include "trace.h"
28 96832424 Michael Walle
#include "qemu-timer.h"
29 49d4d9b6 Paolo Bonzini
#include "ptimer.h"
30 96832424 Michael Walle
#include "qemu-error.h"
31 96832424 Michael Walle
32 96832424 Michael Walle
enum {
33 96832424 Michael Walle
    CTRL_ENABLE      = (1<<0),
34 96832424 Michael Walle
    CTRL_AUTORESTART = (1<<1),
35 96832424 Michael Walle
};
36 96832424 Michael Walle
37 96832424 Michael Walle
enum {
38 96832424 Michael Walle
    ICAP_READY       = (1<<0),
39 96832424 Michael Walle
};
40 96832424 Michael Walle
41 96832424 Michael Walle
enum {
42 96832424 Michael Walle
    R_GPIO_IN = 0,
43 96832424 Michael Walle
    R_GPIO_OUT,
44 96832424 Michael Walle
    R_GPIO_INTEN,
45 96832424 Michael Walle
    R_RESERVED0,
46 96832424 Michael Walle
    R_TIMER0_CONTROL,
47 96832424 Michael Walle
    R_TIMER0_COMPARE,
48 96832424 Michael Walle
    R_TIMER0_COUNTER,
49 96832424 Michael Walle
    R_RESERVED1,
50 96832424 Michael Walle
    R_TIMER1_CONTROL,
51 96832424 Michael Walle
    R_TIMER1_COMPARE,
52 96832424 Michael Walle
    R_TIMER1_COUNTER,
53 96832424 Michael Walle
    R_RESERVED2,
54 96832424 Michael Walle
    R_RESERVED3,
55 96832424 Michael Walle
    R_ICAP,
56 96832424 Michael Walle
    R_CAPABILITIES,
57 96832424 Michael Walle
    R_SYSTEM_ID,
58 96832424 Michael Walle
    R_MAX
59 96832424 Michael Walle
};
60 96832424 Michael Walle
61 96832424 Michael Walle
struct MilkymistSysctlState {
62 96832424 Michael Walle
    SysBusDevice busdev;
63 dfa87ccf Michael Walle
    MemoryRegion regs_region;
64 96832424 Michael Walle
65 96832424 Michael Walle
    QEMUBH *bh0;
66 96832424 Michael Walle
    QEMUBH *bh1;
67 96832424 Michael Walle
    ptimer_state *ptimer0;
68 96832424 Michael Walle
    ptimer_state *ptimer1;
69 96832424 Michael Walle
70 96832424 Michael Walle
    uint32_t freq_hz;
71 96832424 Michael Walle
    uint32_t capabilities;
72 96832424 Michael Walle
    uint32_t systemid;
73 96832424 Michael Walle
    uint32_t strappings;
74 96832424 Michael Walle
75 96832424 Michael Walle
    uint32_t regs[R_MAX];
76 96832424 Michael Walle
77 96832424 Michael Walle
    qemu_irq gpio_irq;
78 96832424 Michael Walle
    qemu_irq timer0_irq;
79 96832424 Michael Walle
    qemu_irq timer1_irq;
80 96832424 Michael Walle
};
81 96832424 Michael Walle
typedef struct MilkymistSysctlState MilkymistSysctlState;
82 96832424 Michael Walle
83 96832424 Michael Walle
static void sysctl_icap_write(MilkymistSysctlState *s, uint32_t value)
84 96832424 Michael Walle
{
85 96832424 Michael Walle
    trace_milkymist_sysctl_icap_write(value);
86 96832424 Michael Walle
    switch (value & 0xffff) {
87 96832424 Michael Walle
    case 0x000e:
88 96832424 Michael Walle
        qemu_system_shutdown_request();
89 96832424 Michael Walle
        break;
90 96832424 Michael Walle
    }
91 96832424 Michael Walle
}
92 96832424 Michael Walle
93 dfa87ccf Michael Walle
static uint64_t sysctl_read(void *opaque, target_phys_addr_t addr,
94 dfa87ccf Michael Walle
                            unsigned size)
95 96832424 Michael Walle
{
96 96832424 Michael Walle
    MilkymistSysctlState *s = opaque;
97 96832424 Michael Walle
    uint32_t r = 0;
98 96832424 Michael Walle
99 96832424 Michael Walle
    addr >>= 2;
100 96832424 Michael Walle
    switch (addr) {
101 96832424 Michael Walle
    case R_TIMER0_COUNTER:
102 96832424 Michael Walle
        r = (uint32_t)ptimer_get_count(s->ptimer0);
103 96832424 Michael Walle
        /* milkymist timer counts up */
104 96832424 Michael Walle
        r = s->regs[R_TIMER0_COMPARE] - r;
105 96832424 Michael Walle
        break;
106 96832424 Michael Walle
    case R_TIMER1_COUNTER:
107 96832424 Michael Walle
        r = (uint32_t)ptimer_get_count(s->ptimer1);
108 96832424 Michael Walle
        /* milkymist timer counts up */
109 96832424 Michael Walle
        r = s->regs[R_TIMER1_COMPARE] - r;
110 96832424 Michael Walle
        break;
111 96832424 Michael Walle
    case R_GPIO_IN:
112 96832424 Michael Walle
    case R_GPIO_OUT:
113 96832424 Michael Walle
    case R_GPIO_INTEN:
114 96832424 Michael Walle
    case R_TIMER0_CONTROL:
115 96832424 Michael Walle
    case R_TIMER0_COMPARE:
116 96832424 Michael Walle
    case R_TIMER1_CONTROL:
117 96832424 Michael Walle
    case R_TIMER1_COMPARE:
118 96832424 Michael Walle
    case R_ICAP:
119 96832424 Michael Walle
    case R_CAPABILITIES:
120 96832424 Michael Walle
    case R_SYSTEM_ID:
121 96832424 Michael Walle
        r = s->regs[addr];
122 96832424 Michael Walle
        break;
123 96832424 Michael Walle
124 96832424 Michael Walle
    default:
125 dd3d6775 Markus Armbruster
        error_report("milkymist_sysctl: read access to unknown register 0x"
126 96832424 Michael Walle
                TARGET_FMT_plx, addr << 2);
127 96832424 Michael Walle
        break;
128 96832424 Michael Walle
    }
129 96832424 Michael Walle
130 96832424 Michael Walle
    trace_milkymist_sysctl_memory_read(addr << 2, r);
131 96832424 Michael Walle
132 96832424 Michael Walle
    return r;
133 96832424 Michael Walle
}
134 96832424 Michael Walle
135 dfa87ccf Michael Walle
static void sysctl_write(void *opaque, target_phys_addr_t addr, uint64_t value,
136 dfa87ccf Michael Walle
                         unsigned size)
137 96832424 Michael Walle
{
138 96832424 Michael Walle
    MilkymistSysctlState *s = opaque;
139 96832424 Michael Walle
140 96832424 Michael Walle
    trace_milkymist_sysctl_memory_write(addr, value);
141 96832424 Michael Walle
142 96832424 Michael Walle
    addr >>= 2;
143 96832424 Michael Walle
    switch (addr) {
144 96832424 Michael Walle
    case R_GPIO_OUT:
145 96832424 Michael Walle
    case R_GPIO_INTEN:
146 96832424 Michael Walle
    case R_TIMER0_COUNTER:
147 96832424 Michael Walle
    case R_TIMER1_COUNTER:
148 f3172a0e Michael Walle
        s->regs[addr] = value;
149 96832424 Michael Walle
        break;
150 96832424 Michael Walle
    case R_TIMER0_COMPARE:
151 96832424 Michael Walle
        ptimer_set_limit(s->ptimer0, value, 0);
152 96832424 Michael Walle
        s->regs[addr] = value;
153 96832424 Michael Walle
        break;
154 96832424 Michael Walle
    case R_TIMER1_COMPARE:
155 96832424 Michael Walle
        ptimer_set_limit(s->ptimer1, value, 0);
156 96832424 Michael Walle
        s->regs[addr] = value;
157 96832424 Michael Walle
        break;
158 96832424 Michael Walle
    case R_TIMER0_CONTROL:
159 96832424 Michael Walle
        s->regs[addr] = value;
160 96832424 Michael Walle
        if (s->regs[R_TIMER0_CONTROL] & CTRL_ENABLE) {
161 f3172a0e Michael Walle
            trace_milkymist_sysctl_start_timer0();
162 f3172a0e Michael Walle
            ptimer_set_count(s->ptimer0,
163 f3172a0e Michael Walle
                    s->regs[R_TIMER0_COMPARE] - s->regs[R_TIMER0_COUNTER]);
164 96832424 Michael Walle
            ptimer_run(s->ptimer0, 0);
165 96832424 Michael Walle
        } else {
166 f3172a0e Michael Walle
            trace_milkymist_sysctl_stop_timer0();
167 96832424 Michael Walle
            ptimer_stop(s->ptimer0);
168 96832424 Michael Walle
        }
169 96832424 Michael Walle
        break;
170 96832424 Michael Walle
    case R_TIMER1_CONTROL:
171 96832424 Michael Walle
        s->regs[addr] = value;
172 96832424 Michael Walle
        if (s->regs[R_TIMER1_CONTROL] & CTRL_ENABLE) {
173 96832424 Michael Walle
            trace_milkymist_sysctl_start_timer1();
174 f3172a0e Michael Walle
            ptimer_set_count(s->ptimer1,
175 f3172a0e Michael Walle
                    s->regs[R_TIMER1_COMPARE] - s->regs[R_TIMER1_COUNTER]);
176 96832424 Michael Walle
            ptimer_run(s->ptimer1, 0);
177 96832424 Michael Walle
        } else {
178 96832424 Michael Walle
            trace_milkymist_sysctl_stop_timer1();
179 96832424 Michael Walle
            ptimer_stop(s->ptimer1);
180 96832424 Michael Walle
        }
181 96832424 Michael Walle
        break;
182 96832424 Michael Walle
    case R_ICAP:
183 96832424 Michael Walle
        sysctl_icap_write(s, value);
184 96832424 Michael Walle
        break;
185 96832424 Michael Walle
    case R_SYSTEM_ID:
186 96832424 Michael Walle
        qemu_system_reset_request();
187 96832424 Michael Walle
        break;
188 96832424 Michael Walle
189 96832424 Michael Walle
    case R_GPIO_IN:
190 96832424 Michael Walle
    case R_CAPABILITIES:
191 96832424 Michael Walle
        error_report("milkymist_sysctl: write to read-only register 0x"
192 96832424 Michael Walle
                TARGET_FMT_plx, addr << 2);
193 96832424 Michael Walle
        break;
194 96832424 Michael Walle
195 96832424 Michael Walle
    default:
196 dd3d6775 Markus Armbruster
        error_report("milkymist_sysctl: write access to unknown register 0x"
197 96832424 Michael Walle
                TARGET_FMT_plx, addr << 2);
198 96832424 Michael Walle
        break;
199 96832424 Michael Walle
    }
200 96832424 Michael Walle
}
201 96832424 Michael Walle
202 dfa87ccf Michael Walle
static const MemoryRegionOps sysctl_mmio_ops = {
203 dfa87ccf Michael Walle
    .read = sysctl_read,
204 dfa87ccf Michael Walle
    .write = sysctl_write,
205 dfa87ccf Michael Walle
    .valid = {
206 dfa87ccf Michael Walle
        .min_access_size = 4,
207 dfa87ccf Michael Walle
        .max_access_size = 4,
208 dfa87ccf Michael Walle
    },
209 dfa87ccf Michael Walle
    .endianness = DEVICE_NATIVE_ENDIAN,
210 96832424 Michael Walle
};
211 96832424 Michael Walle
212 96832424 Michael Walle
static void timer0_hit(void *opaque)
213 96832424 Michael Walle
{
214 96832424 Michael Walle
    MilkymistSysctlState *s = opaque;
215 96832424 Michael Walle
216 96832424 Michael Walle
    if (!(s->regs[R_TIMER0_CONTROL] & CTRL_AUTORESTART)) {
217 96832424 Michael Walle
        s->regs[R_TIMER0_CONTROL] &= ~CTRL_ENABLE;
218 96832424 Michael Walle
        trace_milkymist_sysctl_stop_timer0();
219 96832424 Michael Walle
        ptimer_stop(s->ptimer0);
220 96832424 Michael Walle
    }
221 96832424 Michael Walle
222 96832424 Michael Walle
    trace_milkymist_sysctl_pulse_irq_timer0();
223 96832424 Michael Walle
    qemu_irq_pulse(s->timer0_irq);
224 96832424 Michael Walle
}
225 96832424 Michael Walle
226 96832424 Michael Walle
static void timer1_hit(void *opaque)
227 96832424 Michael Walle
{
228 96832424 Michael Walle
    MilkymistSysctlState *s = opaque;
229 96832424 Michael Walle
230 96832424 Michael Walle
    if (!(s->regs[R_TIMER1_CONTROL] & CTRL_AUTORESTART)) {
231 96832424 Michael Walle
        s->regs[R_TIMER1_CONTROL] &= ~CTRL_ENABLE;
232 96832424 Michael Walle
        trace_milkymist_sysctl_stop_timer1();
233 96832424 Michael Walle
        ptimer_stop(s->ptimer1);
234 96832424 Michael Walle
    }
235 96832424 Michael Walle
236 96832424 Michael Walle
    trace_milkymist_sysctl_pulse_irq_timer1();
237 96832424 Michael Walle
    qemu_irq_pulse(s->timer1_irq);
238 96832424 Michael Walle
}
239 96832424 Michael Walle
240 96832424 Michael Walle
static void milkymist_sysctl_reset(DeviceState *d)
241 96832424 Michael Walle
{
242 96832424 Michael Walle
    MilkymistSysctlState *s =
243 96832424 Michael Walle
            container_of(d, MilkymistSysctlState, busdev.qdev);
244 96832424 Michael Walle
    int i;
245 96832424 Michael Walle
246 96832424 Michael Walle
    for (i = 0; i < R_MAX; i++) {
247 96832424 Michael Walle
        s->regs[i] = 0;
248 96832424 Michael Walle
    }
249 96832424 Michael Walle
250 96832424 Michael Walle
    ptimer_stop(s->ptimer0);
251 96832424 Michael Walle
    ptimer_stop(s->ptimer1);
252 96832424 Michael Walle
253 96832424 Michael Walle
    /* defaults */
254 96832424 Michael Walle
    s->regs[R_ICAP] = ICAP_READY;
255 96832424 Michael Walle
    s->regs[R_SYSTEM_ID] = s->systemid;
256 96832424 Michael Walle
    s->regs[R_CAPABILITIES] = s->capabilities;
257 96832424 Michael Walle
    s->regs[R_GPIO_IN] = s->strappings;
258 96832424 Michael Walle
}
259 96832424 Michael Walle
260 96832424 Michael Walle
static int milkymist_sysctl_init(SysBusDevice *dev)
261 96832424 Michael Walle
{
262 96832424 Michael Walle
    MilkymistSysctlState *s = FROM_SYSBUS(typeof(*s), dev);
263 96832424 Michael Walle
264 96832424 Michael Walle
    sysbus_init_irq(dev, &s->gpio_irq);
265 96832424 Michael Walle
    sysbus_init_irq(dev, &s->timer0_irq);
266 96832424 Michael Walle
    sysbus_init_irq(dev, &s->timer1_irq);
267 96832424 Michael Walle
268 96832424 Michael Walle
    s->bh0 = qemu_bh_new(timer0_hit, s);
269 96832424 Michael Walle
    s->bh1 = qemu_bh_new(timer1_hit, s);
270 96832424 Michael Walle
    s->ptimer0 = ptimer_init(s->bh0);
271 96832424 Michael Walle
    s->ptimer1 = ptimer_init(s->bh1);
272 96832424 Michael Walle
    ptimer_set_freq(s->ptimer0, s->freq_hz);
273 96832424 Michael Walle
    ptimer_set_freq(s->ptimer1, s->freq_hz);
274 96832424 Michael Walle
275 dfa87ccf Michael Walle
    memory_region_init_io(&s->regs_region, &sysctl_mmio_ops, s,
276 dfa87ccf Michael Walle
            "milkymist-sysctl", R_MAX * 4);
277 750ecd44 Avi Kivity
    sysbus_init_mmio(dev, &s->regs_region);
278 96832424 Michael Walle
279 96832424 Michael Walle
    return 0;
280 96832424 Michael Walle
}
281 96832424 Michael Walle
282 96832424 Michael Walle
static const VMStateDescription vmstate_milkymist_sysctl = {
283 96832424 Michael Walle
    .name = "milkymist-sysctl",
284 96832424 Michael Walle
    .version_id = 1,
285 96832424 Michael Walle
    .minimum_version_id = 1,
286 96832424 Michael Walle
    .minimum_version_id_old = 1,
287 96832424 Michael Walle
    .fields      = (VMStateField[]) {
288 96832424 Michael Walle
        VMSTATE_UINT32_ARRAY(regs, MilkymistSysctlState, R_MAX),
289 96832424 Michael Walle
        VMSTATE_PTIMER(ptimer0, MilkymistSysctlState),
290 96832424 Michael Walle
        VMSTATE_PTIMER(ptimer1, MilkymistSysctlState),
291 96832424 Michael Walle
        VMSTATE_END_OF_LIST()
292 96832424 Michael Walle
    }
293 96832424 Michael Walle
};
294 96832424 Michael Walle
295 999e12bb Anthony Liguori
static Property milkymist_sysctl_properties[] = {
296 999e12bb Anthony Liguori
    DEFINE_PROP_UINT32("frequency", MilkymistSysctlState,
297 999e12bb Anthony Liguori
    freq_hz, 80000000),
298 999e12bb Anthony Liguori
    DEFINE_PROP_UINT32("capabilities", MilkymistSysctlState,
299 999e12bb Anthony Liguori
    capabilities, 0x00000000),
300 999e12bb Anthony Liguori
    DEFINE_PROP_UINT32("systemid", MilkymistSysctlState,
301 999e12bb Anthony Liguori
    systemid, 0x10014d31),
302 999e12bb Anthony Liguori
    DEFINE_PROP_UINT32("gpio_strappings", MilkymistSysctlState,
303 999e12bb Anthony Liguori
    strappings, 0x00000001),
304 999e12bb Anthony Liguori
    DEFINE_PROP_END_OF_LIST(),
305 999e12bb Anthony Liguori
};
306 999e12bb Anthony Liguori
307 999e12bb Anthony Liguori
static void milkymist_sysctl_class_init(ObjectClass *klass, void *data)
308 999e12bb Anthony Liguori
{
309 39bffca2 Anthony Liguori
    DeviceClass *dc = DEVICE_CLASS(klass);
310 999e12bb Anthony Liguori
    SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
311 999e12bb Anthony Liguori
312 999e12bb Anthony Liguori
    k->init = milkymist_sysctl_init;
313 39bffca2 Anthony Liguori
    dc->reset = milkymist_sysctl_reset;
314 39bffca2 Anthony Liguori
    dc->vmsd = &vmstate_milkymist_sysctl;
315 39bffca2 Anthony Liguori
    dc->props = milkymist_sysctl_properties;
316 999e12bb Anthony Liguori
}
317 999e12bb Anthony Liguori
318 39bffca2 Anthony Liguori
static TypeInfo milkymist_sysctl_info = {
319 39bffca2 Anthony Liguori
    .name          = "milkymist-sysctl",
320 39bffca2 Anthony Liguori
    .parent        = TYPE_SYS_BUS_DEVICE,
321 39bffca2 Anthony Liguori
    .instance_size = sizeof(MilkymistSysctlState),
322 39bffca2 Anthony Liguori
    .class_init    = milkymist_sysctl_class_init,
323 96832424 Michael Walle
};
324 96832424 Michael Walle
325 96832424 Michael Walle
static void milkymist_sysctl_register(void)
326 96832424 Michael Walle
{
327 39bffca2 Anthony Liguori
    type_register_static(&milkymist_sysctl_info);
328 96832424 Michael Walle
}
329 96832424 Michael Walle
330 96832424 Michael Walle
device_init(milkymist_sysctl_register)