2 * QEMU model of the Milkymist System Controller.
4 * Copyright (c) 2010-2012 Michael Walle <michael@walle.cc>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 * Specification available at:
21 * http://milkymist.walle.cc/socdoc/sysctl.pdf
24 #include "qemu/osdep.h"
26 #include "hw/sysbus.h"
27 #include "migration/vmstate.h"
29 #include "qemu/timer.h"
30 #include "sysemu/runstate.h"
31 #include "hw/ptimer.h"
32 #include "hw/qdev-properties.h"
33 #include "qemu/error-report.h"
34 #include "qemu/main-loop.h"
35 #include "qemu/module.h"
39 CTRL_AUTORESTART
= (1<<1),
57 R_DBG_SCRATCHPAD
= 20,
65 #define TYPE_MILKYMIST_SYSCTL "milkymist-sysctl"
66 #define MILKYMIST_SYSCTL(obj) \
67 OBJECT_CHECK(MilkymistSysctlState, (obj), TYPE_MILKYMIST_SYSCTL)
69 struct MilkymistSysctlState
{
70 SysBusDevice parent_obj
;
72 MemoryRegion regs_region
;
76 ptimer_state
*ptimer0
;
77 ptimer_state
*ptimer1
;
80 uint32_t capabilities
;
90 typedef struct MilkymistSysctlState MilkymistSysctlState
;
92 static void sysctl_icap_write(MilkymistSysctlState
*s
, uint32_t value
)
94 trace_milkymist_sysctl_icap_write(value
);
95 switch (value
& 0xffff) {
97 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN
);
102 static uint64_t sysctl_read(void *opaque
, hwaddr addr
,
105 MilkymistSysctlState
*s
= opaque
;
110 case R_TIMER0_COUNTER
:
111 r
= (uint32_t)ptimer_get_count(s
->ptimer0
);
112 /* milkymist timer counts up */
113 r
= s
->regs
[R_TIMER0_COMPARE
] - r
;
115 case R_TIMER1_COUNTER
:
116 r
= (uint32_t)ptimer_get_count(s
->ptimer1
);
117 /* milkymist timer counts up */
118 r
= s
->regs
[R_TIMER1_COMPARE
] - r
;
123 case R_TIMER0_CONTROL
:
124 case R_TIMER0_COMPARE
:
125 case R_TIMER1_CONTROL
:
126 case R_TIMER1_COMPARE
:
128 case R_DBG_SCRATCHPAD
:
129 case R_DBG_WRITE_LOCK
:
130 case R_CLK_FREQUENCY
:
137 error_report("milkymist_sysctl: read access to unknown register 0x"
138 TARGET_FMT_plx
, addr
<< 2);
142 trace_milkymist_sysctl_memory_read(addr
<< 2, r
);
147 static void sysctl_write(void *opaque
, hwaddr addr
, uint64_t value
,
150 MilkymistSysctlState
*s
= opaque
;
152 trace_milkymist_sysctl_memory_write(addr
, value
);
158 case R_TIMER0_COUNTER
:
159 case R_TIMER1_COUNTER
:
160 case R_DBG_SCRATCHPAD
:
161 s
->regs
[addr
] = value
;
163 case R_TIMER0_COMPARE
:
164 ptimer_set_limit(s
->ptimer0
, value
, 0);
165 s
->regs
[addr
] = value
;
167 case R_TIMER1_COMPARE
:
168 ptimer_set_limit(s
->ptimer1
, value
, 0);
169 s
->regs
[addr
] = value
;
171 case R_TIMER0_CONTROL
:
172 s
->regs
[addr
] = value
;
173 if (s
->regs
[R_TIMER0_CONTROL
] & CTRL_ENABLE
) {
174 trace_milkymist_sysctl_start_timer0();
175 ptimer_set_count(s
->ptimer0
,
176 s
->regs
[R_TIMER0_COMPARE
] - s
->regs
[R_TIMER0_COUNTER
]);
177 ptimer_run(s
->ptimer0
, 0);
179 trace_milkymist_sysctl_stop_timer0();
180 ptimer_stop(s
->ptimer0
);
183 case R_TIMER1_CONTROL
:
184 s
->regs
[addr
] = value
;
185 if (s
->regs
[R_TIMER1_CONTROL
] & CTRL_ENABLE
) {
186 trace_milkymist_sysctl_start_timer1();
187 ptimer_set_count(s
->ptimer1
,
188 s
->regs
[R_TIMER1_COMPARE
] - s
->regs
[R_TIMER1_COUNTER
]);
189 ptimer_run(s
->ptimer1
, 0);
191 trace_milkymist_sysctl_stop_timer1();
192 ptimer_stop(s
->ptimer1
);
196 sysctl_icap_write(s
, value
);
198 case R_DBG_WRITE_LOCK
:
202 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET
);
206 case R_CLK_FREQUENCY
:
208 error_report("milkymist_sysctl: write to read-only register 0x"
209 TARGET_FMT_plx
, addr
<< 2);
213 error_report("milkymist_sysctl: write access to unknown register 0x"
214 TARGET_FMT_plx
, addr
<< 2);
219 static const MemoryRegionOps sysctl_mmio_ops
= {
221 .write
= sysctl_write
,
223 .min_access_size
= 4,
224 .max_access_size
= 4,
226 .endianness
= DEVICE_NATIVE_ENDIAN
,
229 static void timer0_hit(void *opaque
)
231 MilkymistSysctlState
*s
= opaque
;
233 if (!(s
->regs
[R_TIMER0_CONTROL
] & CTRL_AUTORESTART
)) {
234 s
->regs
[R_TIMER0_CONTROL
] &= ~CTRL_ENABLE
;
235 trace_milkymist_sysctl_stop_timer0();
236 ptimer_stop(s
->ptimer0
);
239 trace_milkymist_sysctl_pulse_irq_timer0();
240 qemu_irq_pulse(s
->timer0_irq
);
243 static void timer1_hit(void *opaque
)
245 MilkymistSysctlState
*s
= opaque
;
247 if (!(s
->regs
[R_TIMER1_CONTROL
] & CTRL_AUTORESTART
)) {
248 s
->regs
[R_TIMER1_CONTROL
] &= ~CTRL_ENABLE
;
249 trace_milkymist_sysctl_stop_timer1();
250 ptimer_stop(s
->ptimer1
);
253 trace_milkymist_sysctl_pulse_irq_timer1();
254 qemu_irq_pulse(s
->timer1_irq
);
257 static void milkymist_sysctl_reset(DeviceState
*d
)
259 MilkymistSysctlState
*s
= MILKYMIST_SYSCTL(d
);
262 for (i
= 0; i
< R_MAX
; i
++) {
266 ptimer_stop(s
->ptimer0
);
267 ptimer_stop(s
->ptimer1
);
270 s
->regs
[R_ICAP
] = ICAP_READY
;
271 s
->regs
[R_SYSTEM_ID
] = s
->systemid
;
272 s
->regs
[R_CLK_FREQUENCY
] = s
->freq_hz
;
273 s
->regs
[R_CAPABILITIES
] = s
->capabilities
;
274 s
->regs
[R_GPIO_IN
] = s
->strappings
;
277 static void milkymist_sysctl_init(Object
*obj
)
279 MilkymistSysctlState
*s
= MILKYMIST_SYSCTL(obj
);
280 SysBusDevice
*dev
= SYS_BUS_DEVICE(obj
);
282 sysbus_init_irq(dev
, &s
->gpio_irq
);
283 sysbus_init_irq(dev
, &s
->timer0_irq
);
284 sysbus_init_irq(dev
, &s
->timer1_irq
);
286 s
->bh0
= qemu_bh_new(timer0_hit
, s
);
287 s
->bh1
= qemu_bh_new(timer1_hit
, s
);
288 s
->ptimer0
= ptimer_init(s
->bh0
, PTIMER_POLICY_DEFAULT
);
289 s
->ptimer1
= ptimer_init(s
->bh1
, PTIMER_POLICY_DEFAULT
);
291 memory_region_init_io(&s
->regs_region
, obj
, &sysctl_mmio_ops
, s
,
292 "milkymist-sysctl", R_MAX
* 4);
293 sysbus_init_mmio(dev
, &s
->regs_region
);
296 static void milkymist_sysctl_realize(DeviceState
*dev
, Error
**errp
)
298 MilkymistSysctlState
*s
= MILKYMIST_SYSCTL(dev
);
300 ptimer_set_freq(s
->ptimer0
, s
->freq_hz
);
301 ptimer_set_freq(s
->ptimer1
, s
->freq_hz
);
304 static const VMStateDescription vmstate_milkymist_sysctl
= {
305 .name
= "milkymist-sysctl",
307 .minimum_version_id
= 1,
308 .fields
= (VMStateField
[]) {
309 VMSTATE_UINT32_ARRAY(regs
, MilkymistSysctlState
, R_MAX
),
310 VMSTATE_PTIMER(ptimer0
, MilkymistSysctlState
),
311 VMSTATE_PTIMER(ptimer1
, MilkymistSysctlState
),
312 VMSTATE_END_OF_LIST()
316 static Property milkymist_sysctl_properties
[] = {
317 DEFINE_PROP_UINT32("frequency", MilkymistSysctlState
,
319 DEFINE_PROP_UINT32("capabilities", MilkymistSysctlState
,
320 capabilities
, 0x00000000),
321 DEFINE_PROP_UINT32("systemid", MilkymistSysctlState
,
322 systemid
, 0x10014d31),
323 DEFINE_PROP_UINT32("gpio_strappings", MilkymistSysctlState
,
324 strappings
, 0x00000001),
325 DEFINE_PROP_END_OF_LIST(),
328 static void milkymist_sysctl_class_init(ObjectClass
*klass
, void *data
)
330 DeviceClass
*dc
= DEVICE_CLASS(klass
);
332 dc
->realize
= milkymist_sysctl_realize
;
333 dc
->reset
= milkymist_sysctl_reset
;
334 dc
->vmsd
= &vmstate_milkymist_sysctl
;
335 dc
->props
= milkymist_sysctl_properties
;
338 static const TypeInfo milkymist_sysctl_info
= {
339 .name
= TYPE_MILKYMIST_SYSCTL
,
340 .parent
= TYPE_SYS_BUS_DEVICE
,
341 .instance_size
= sizeof(MilkymistSysctlState
),
342 .instance_init
= milkymist_sysctl_init
,
343 .class_init
= milkymist_sysctl_class_init
,
346 static void milkymist_sysctl_register_types(void)
348 type_register_static(&milkymist_sysctl_info
);
351 type_init(milkymist_sysctl_register_types
)