Statistics
| Branch: | Revision:

root / hw / milkymist-sysctl.c @ 43997225

History | View | Annotate | Download (8.9 kB)

1 96832424 Michael Walle
/*
2 96832424 Michael Walle
 *  QEMU model of the Milkymist System Controller.
3 96832424 Michael Walle
 *
4 060544d3 Michael Walle
 *  Copyright (c) 2010-2012 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 060544d3 Michael Walle
    R_GPIO_IN         = 0,
43 96832424 Michael Walle
    R_GPIO_OUT,
44 96832424 Michael Walle
    R_GPIO_INTEN,
45 060544d3 Michael Walle
    R_TIMER0_CONTROL  = 4,
46 96832424 Michael Walle
    R_TIMER0_COMPARE,
47 96832424 Michael Walle
    R_TIMER0_COUNTER,
48 060544d3 Michael Walle
    R_TIMER1_CONTROL  = 8,
49 96832424 Michael Walle
    R_TIMER1_COMPARE,
50 96832424 Michael Walle
    R_TIMER1_COUNTER,
51 060544d3 Michael Walle
    R_ICAP = 16,
52 060544d3 Michael Walle
    R_DBG_SCRATCHPAD  = 20,
53 060544d3 Michael Walle
    R_DBG_WRITE_LOCK,
54 060544d3 Michael Walle
    R_CLK_FREQUENCY   = 29,
55 96832424 Michael Walle
    R_CAPABILITIES,
56 96832424 Michael Walle
    R_SYSTEM_ID,
57 96832424 Michael Walle
    R_MAX
58 96832424 Michael Walle
};
59 96832424 Michael Walle
60 96832424 Michael Walle
struct MilkymistSysctlState {
61 96832424 Michael Walle
    SysBusDevice busdev;
62 dfa87ccf Michael Walle
    MemoryRegion regs_region;
63 96832424 Michael Walle
64 96832424 Michael Walle
    QEMUBH *bh0;
65 96832424 Michael Walle
    QEMUBH *bh1;
66 96832424 Michael Walle
    ptimer_state *ptimer0;
67 96832424 Michael Walle
    ptimer_state *ptimer1;
68 96832424 Michael Walle
69 96832424 Michael Walle
    uint32_t freq_hz;
70 96832424 Michael Walle
    uint32_t capabilities;
71 96832424 Michael Walle
    uint32_t systemid;
72 96832424 Michael Walle
    uint32_t strappings;
73 96832424 Michael Walle
74 96832424 Michael Walle
    uint32_t regs[R_MAX];
75 96832424 Michael Walle
76 96832424 Michael Walle
    qemu_irq gpio_irq;
77 96832424 Michael Walle
    qemu_irq timer0_irq;
78 96832424 Michael Walle
    qemu_irq timer1_irq;
79 96832424 Michael Walle
};
80 96832424 Michael Walle
typedef struct MilkymistSysctlState MilkymistSysctlState;
81 96832424 Michael Walle
82 96832424 Michael Walle
static void sysctl_icap_write(MilkymistSysctlState *s, uint32_t value)
83 96832424 Michael Walle
{
84 96832424 Michael Walle
    trace_milkymist_sysctl_icap_write(value);
85 96832424 Michael Walle
    switch (value & 0xffff) {
86 96832424 Michael Walle
    case 0x000e:
87 96832424 Michael Walle
        qemu_system_shutdown_request();
88 96832424 Michael Walle
        break;
89 96832424 Michael Walle
    }
90 96832424 Michael Walle
}
91 96832424 Michael Walle
92 dfa87ccf Michael Walle
static uint64_t sysctl_read(void *opaque, target_phys_addr_t addr,
93 dfa87ccf Michael Walle
                            unsigned size)
94 96832424 Michael Walle
{
95 96832424 Michael Walle
    MilkymistSysctlState *s = opaque;
96 96832424 Michael Walle
    uint32_t r = 0;
97 96832424 Michael Walle
98 96832424 Michael Walle
    addr >>= 2;
99 96832424 Michael Walle
    switch (addr) {
100 96832424 Michael Walle
    case R_TIMER0_COUNTER:
101 96832424 Michael Walle
        r = (uint32_t)ptimer_get_count(s->ptimer0);
102 96832424 Michael Walle
        /* milkymist timer counts up */
103 96832424 Michael Walle
        r = s->regs[R_TIMER0_COMPARE] - r;
104 96832424 Michael Walle
        break;
105 96832424 Michael Walle
    case R_TIMER1_COUNTER:
106 96832424 Michael Walle
        r = (uint32_t)ptimer_get_count(s->ptimer1);
107 96832424 Michael Walle
        /* milkymist timer counts up */
108 96832424 Michael Walle
        r = s->regs[R_TIMER1_COMPARE] - r;
109 96832424 Michael Walle
        break;
110 96832424 Michael Walle
    case R_GPIO_IN:
111 96832424 Michael Walle
    case R_GPIO_OUT:
112 96832424 Michael Walle
    case R_GPIO_INTEN:
113 96832424 Michael Walle
    case R_TIMER0_CONTROL:
114 96832424 Michael Walle
    case R_TIMER0_COMPARE:
115 96832424 Michael Walle
    case R_TIMER1_CONTROL:
116 96832424 Michael Walle
    case R_TIMER1_COMPARE:
117 96832424 Michael Walle
    case R_ICAP:
118 060544d3 Michael Walle
    case R_DBG_SCRATCHPAD:
119 060544d3 Michael Walle
    case R_DBG_WRITE_LOCK:
120 060544d3 Michael Walle
    case R_CLK_FREQUENCY:
121 96832424 Michael Walle
    case R_CAPABILITIES:
122 96832424 Michael Walle
    case R_SYSTEM_ID:
123 96832424 Michael Walle
        r = s->regs[addr];
124 96832424 Michael Walle
        break;
125 96832424 Michael Walle
126 96832424 Michael Walle
    default:
127 dd3d6775 Markus Armbruster
        error_report("milkymist_sysctl: read access to unknown register 0x"
128 96832424 Michael Walle
                TARGET_FMT_plx, addr << 2);
129 96832424 Michael Walle
        break;
130 96832424 Michael Walle
    }
131 96832424 Michael Walle
132 96832424 Michael Walle
    trace_milkymist_sysctl_memory_read(addr << 2, r);
133 96832424 Michael Walle
134 96832424 Michael Walle
    return r;
135 96832424 Michael Walle
}
136 96832424 Michael Walle
137 dfa87ccf Michael Walle
static void sysctl_write(void *opaque, target_phys_addr_t addr, uint64_t value,
138 dfa87ccf Michael Walle
                         unsigned size)
139 96832424 Michael Walle
{
140 96832424 Michael Walle
    MilkymistSysctlState *s = opaque;
141 96832424 Michael Walle
142 96832424 Michael Walle
    trace_milkymist_sysctl_memory_write(addr, value);
143 96832424 Michael Walle
144 96832424 Michael Walle
    addr >>= 2;
145 96832424 Michael Walle
    switch (addr) {
146 96832424 Michael Walle
    case R_GPIO_OUT:
147 96832424 Michael Walle
    case R_GPIO_INTEN:
148 96832424 Michael Walle
    case R_TIMER0_COUNTER:
149 96832424 Michael Walle
    case R_TIMER1_COUNTER:
150 060544d3 Michael Walle
    case R_DBG_SCRATCHPAD:
151 f3172a0e Michael Walle
        s->regs[addr] = value;
152 96832424 Michael Walle
        break;
153 96832424 Michael Walle
    case R_TIMER0_COMPARE:
154 96832424 Michael Walle
        ptimer_set_limit(s->ptimer0, value, 0);
155 96832424 Michael Walle
        s->regs[addr] = value;
156 96832424 Michael Walle
        break;
157 96832424 Michael Walle
    case R_TIMER1_COMPARE:
158 96832424 Michael Walle
        ptimer_set_limit(s->ptimer1, value, 0);
159 96832424 Michael Walle
        s->regs[addr] = value;
160 96832424 Michael Walle
        break;
161 96832424 Michael Walle
    case R_TIMER0_CONTROL:
162 96832424 Michael Walle
        s->regs[addr] = value;
163 96832424 Michael Walle
        if (s->regs[R_TIMER0_CONTROL] & CTRL_ENABLE) {
164 f3172a0e Michael Walle
            trace_milkymist_sysctl_start_timer0();
165 f3172a0e Michael Walle
            ptimer_set_count(s->ptimer0,
166 f3172a0e Michael Walle
                    s->regs[R_TIMER0_COMPARE] - s->regs[R_TIMER0_COUNTER]);
167 96832424 Michael Walle
            ptimer_run(s->ptimer0, 0);
168 96832424 Michael Walle
        } else {
169 f3172a0e Michael Walle
            trace_milkymist_sysctl_stop_timer0();
170 96832424 Michael Walle
            ptimer_stop(s->ptimer0);
171 96832424 Michael Walle
        }
172 96832424 Michael Walle
        break;
173 96832424 Michael Walle
    case R_TIMER1_CONTROL:
174 96832424 Michael Walle
        s->regs[addr] = value;
175 96832424 Michael Walle
        if (s->regs[R_TIMER1_CONTROL] & CTRL_ENABLE) {
176 96832424 Michael Walle
            trace_milkymist_sysctl_start_timer1();
177 f3172a0e Michael Walle
            ptimer_set_count(s->ptimer1,
178 f3172a0e Michael Walle
                    s->regs[R_TIMER1_COMPARE] - s->regs[R_TIMER1_COUNTER]);
179 96832424 Michael Walle
            ptimer_run(s->ptimer1, 0);
180 96832424 Michael Walle
        } else {
181 96832424 Michael Walle
            trace_milkymist_sysctl_stop_timer1();
182 96832424 Michael Walle
            ptimer_stop(s->ptimer1);
183 96832424 Michael Walle
        }
184 96832424 Michael Walle
        break;
185 96832424 Michael Walle
    case R_ICAP:
186 96832424 Michael Walle
        sysctl_icap_write(s, value);
187 96832424 Michael Walle
        break;
188 060544d3 Michael Walle
    case R_DBG_WRITE_LOCK:
189 060544d3 Michael Walle
        s->regs[addr] = 1;
190 060544d3 Michael Walle
        break;
191 96832424 Michael Walle
    case R_SYSTEM_ID:
192 96832424 Michael Walle
        qemu_system_reset_request();
193 96832424 Michael Walle
        break;
194 96832424 Michael Walle
195 96832424 Michael Walle
    case R_GPIO_IN:
196 060544d3 Michael Walle
    case R_CLK_FREQUENCY:
197 96832424 Michael Walle
    case R_CAPABILITIES:
198 96832424 Michael Walle
        error_report("milkymist_sysctl: write to read-only register 0x"
199 96832424 Michael Walle
                TARGET_FMT_plx, addr << 2);
200 96832424 Michael Walle
        break;
201 96832424 Michael Walle
202 96832424 Michael Walle
    default:
203 dd3d6775 Markus Armbruster
        error_report("milkymist_sysctl: write access to unknown register 0x"
204 96832424 Michael Walle
                TARGET_FMT_plx, addr << 2);
205 96832424 Michael Walle
        break;
206 96832424 Michael Walle
    }
207 96832424 Michael Walle
}
208 96832424 Michael Walle
209 dfa87ccf Michael Walle
static const MemoryRegionOps sysctl_mmio_ops = {
210 dfa87ccf Michael Walle
    .read = sysctl_read,
211 dfa87ccf Michael Walle
    .write = sysctl_write,
212 dfa87ccf Michael Walle
    .valid = {
213 dfa87ccf Michael Walle
        .min_access_size = 4,
214 dfa87ccf Michael Walle
        .max_access_size = 4,
215 dfa87ccf Michael Walle
    },
216 dfa87ccf Michael Walle
    .endianness = DEVICE_NATIVE_ENDIAN,
217 96832424 Michael Walle
};
218 96832424 Michael Walle
219 96832424 Michael Walle
static void timer0_hit(void *opaque)
220 96832424 Michael Walle
{
221 96832424 Michael Walle
    MilkymistSysctlState *s = opaque;
222 96832424 Michael Walle
223 96832424 Michael Walle
    if (!(s->regs[R_TIMER0_CONTROL] & CTRL_AUTORESTART)) {
224 96832424 Michael Walle
        s->regs[R_TIMER0_CONTROL] &= ~CTRL_ENABLE;
225 96832424 Michael Walle
        trace_milkymist_sysctl_stop_timer0();
226 96832424 Michael Walle
        ptimer_stop(s->ptimer0);
227 96832424 Michael Walle
    }
228 96832424 Michael Walle
229 96832424 Michael Walle
    trace_milkymist_sysctl_pulse_irq_timer0();
230 96832424 Michael Walle
    qemu_irq_pulse(s->timer0_irq);
231 96832424 Michael Walle
}
232 96832424 Michael Walle
233 96832424 Michael Walle
static void timer1_hit(void *opaque)
234 96832424 Michael Walle
{
235 96832424 Michael Walle
    MilkymistSysctlState *s = opaque;
236 96832424 Michael Walle
237 96832424 Michael Walle
    if (!(s->regs[R_TIMER1_CONTROL] & CTRL_AUTORESTART)) {
238 96832424 Michael Walle
        s->regs[R_TIMER1_CONTROL] &= ~CTRL_ENABLE;
239 96832424 Michael Walle
        trace_milkymist_sysctl_stop_timer1();
240 96832424 Michael Walle
        ptimer_stop(s->ptimer1);
241 96832424 Michael Walle
    }
242 96832424 Michael Walle
243 96832424 Michael Walle
    trace_milkymist_sysctl_pulse_irq_timer1();
244 96832424 Michael Walle
    qemu_irq_pulse(s->timer1_irq);
245 96832424 Michael Walle
}
246 96832424 Michael Walle
247 96832424 Michael Walle
static void milkymist_sysctl_reset(DeviceState *d)
248 96832424 Michael Walle
{
249 96832424 Michael Walle
    MilkymistSysctlState *s =
250 96832424 Michael Walle
            container_of(d, MilkymistSysctlState, busdev.qdev);
251 96832424 Michael Walle
    int i;
252 96832424 Michael Walle
253 96832424 Michael Walle
    for (i = 0; i < R_MAX; i++) {
254 96832424 Michael Walle
        s->regs[i] = 0;
255 96832424 Michael Walle
    }
256 96832424 Michael Walle
257 96832424 Michael Walle
    ptimer_stop(s->ptimer0);
258 96832424 Michael Walle
    ptimer_stop(s->ptimer1);
259 96832424 Michael Walle
260 96832424 Michael Walle
    /* defaults */
261 96832424 Michael Walle
    s->regs[R_ICAP] = ICAP_READY;
262 96832424 Michael Walle
    s->regs[R_SYSTEM_ID] = s->systemid;
263 060544d3 Michael Walle
    s->regs[R_CLK_FREQUENCY] = s->freq_hz;
264 96832424 Michael Walle
    s->regs[R_CAPABILITIES] = s->capabilities;
265 96832424 Michael Walle
    s->regs[R_GPIO_IN] = s->strappings;
266 96832424 Michael Walle
}
267 96832424 Michael Walle
268 96832424 Michael Walle
static int milkymist_sysctl_init(SysBusDevice *dev)
269 96832424 Michael Walle
{
270 96832424 Michael Walle
    MilkymistSysctlState *s = FROM_SYSBUS(typeof(*s), dev);
271 96832424 Michael Walle
272 96832424 Michael Walle
    sysbus_init_irq(dev, &s->gpio_irq);
273 96832424 Michael Walle
    sysbus_init_irq(dev, &s->timer0_irq);
274 96832424 Michael Walle
    sysbus_init_irq(dev, &s->timer1_irq);
275 96832424 Michael Walle
276 96832424 Michael Walle
    s->bh0 = qemu_bh_new(timer0_hit, s);
277 96832424 Michael Walle
    s->bh1 = qemu_bh_new(timer1_hit, s);
278 96832424 Michael Walle
    s->ptimer0 = ptimer_init(s->bh0);
279 96832424 Michael Walle
    s->ptimer1 = ptimer_init(s->bh1);
280 96832424 Michael Walle
    ptimer_set_freq(s->ptimer0, s->freq_hz);
281 96832424 Michael Walle
    ptimer_set_freq(s->ptimer1, s->freq_hz);
282 96832424 Michael Walle
283 dfa87ccf Michael Walle
    memory_region_init_io(&s->regs_region, &sysctl_mmio_ops, s,
284 dfa87ccf Michael Walle
            "milkymist-sysctl", R_MAX * 4);
285 750ecd44 Avi Kivity
    sysbus_init_mmio(dev, &s->regs_region);
286 96832424 Michael Walle
287 96832424 Michael Walle
    return 0;
288 96832424 Michael Walle
}
289 96832424 Michael Walle
290 96832424 Michael Walle
static const VMStateDescription vmstate_milkymist_sysctl = {
291 96832424 Michael Walle
    .name = "milkymist-sysctl",
292 96832424 Michael Walle
    .version_id = 1,
293 96832424 Michael Walle
    .minimum_version_id = 1,
294 96832424 Michael Walle
    .minimum_version_id_old = 1,
295 96832424 Michael Walle
    .fields      = (VMStateField[]) {
296 96832424 Michael Walle
        VMSTATE_UINT32_ARRAY(regs, MilkymistSysctlState, R_MAX),
297 96832424 Michael Walle
        VMSTATE_PTIMER(ptimer0, MilkymistSysctlState),
298 96832424 Michael Walle
        VMSTATE_PTIMER(ptimer1, MilkymistSysctlState),
299 96832424 Michael Walle
        VMSTATE_END_OF_LIST()
300 96832424 Michael Walle
    }
301 96832424 Michael Walle
};
302 96832424 Michael Walle
303 999e12bb Anthony Liguori
static Property milkymist_sysctl_properties[] = {
304 999e12bb Anthony Liguori
    DEFINE_PROP_UINT32("frequency", MilkymistSysctlState,
305 999e12bb Anthony Liguori
    freq_hz, 80000000),
306 999e12bb Anthony Liguori
    DEFINE_PROP_UINT32("capabilities", MilkymistSysctlState,
307 999e12bb Anthony Liguori
    capabilities, 0x00000000),
308 999e12bb Anthony Liguori
    DEFINE_PROP_UINT32("systemid", MilkymistSysctlState,
309 999e12bb Anthony Liguori
    systemid, 0x10014d31),
310 999e12bb Anthony Liguori
    DEFINE_PROP_UINT32("gpio_strappings", MilkymistSysctlState,
311 999e12bb Anthony Liguori
    strappings, 0x00000001),
312 999e12bb Anthony Liguori
    DEFINE_PROP_END_OF_LIST(),
313 999e12bb Anthony Liguori
};
314 999e12bb Anthony Liguori
315 999e12bb Anthony Liguori
static void milkymist_sysctl_class_init(ObjectClass *klass, void *data)
316 999e12bb Anthony Liguori
{
317 39bffca2 Anthony Liguori
    DeviceClass *dc = DEVICE_CLASS(klass);
318 999e12bb Anthony Liguori
    SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
319 999e12bb Anthony Liguori
320 999e12bb Anthony Liguori
    k->init = milkymist_sysctl_init;
321 39bffca2 Anthony Liguori
    dc->reset = milkymist_sysctl_reset;
322 39bffca2 Anthony Liguori
    dc->vmsd = &vmstate_milkymist_sysctl;
323 39bffca2 Anthony Liguori
    dc->props = milkymist_sysctl_properties;
324 999e12bb Anthony Liguori
}
325 999e12bb Anthony Liguori
326 39bffca2 Anthony Liguori
static TypeInfo milkymist_sysctl_info = {
327 39bffca2 Anthony Liguori
    .name          = "milkymist-sysctl",
328 39bffca2 Anthony Liguori
    .parent        = TYPE_SYS_BUS_DEVICE,
329 39bffca2 Anthony Liguori
    .instance_size = sizeof(MilkymistSysctlState),
330 39bffca2 Anthony Liguori
    .class_init    = milkymist_sysctl_class_init,
331 96832424 Michael Walle
};
332 96832424 Michael Walle
333 83f7d43a Andreas Färber
static void milkymist_sysctl_register_types(void)
334 96832424 Michael Walle
{
335 39bffca2 Anthony Liguori
    type_register_static(&milkymist_sysctl_info);
336 96832424 Michael Walle
}
337 96832424 Michael Walle
338 83f7d43a Andreas Färber
type_init(milkymist_sysctl_register_types)