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 "sysemu/sysemu.h"
29 #include "qemu/timer.h"
30 #include "hw/ptimer.h"
31 #include "qemu/error-report.h"
35 CTRL_AUTORESTART
= (1<<1),
53 R_DBG_SCRATCHPAD
= 20,
61 #define TYPE_MILKYMIST_SYSCTL "milkymist-sysctl"
62 #define MILKYMIST_SYSCTL(obj) \
63 OBJECT_CHECK(MilkymistSysctlState, (obj), TYPE_MILKYMIST_SYSCTL)
65 struct MilkymistSysctlState
{
66 SysBusDevice parent_obj
;
68 MemoryRegion regs_region
;
72 ptimer_state
*ptimer0
;
73 ptimer_state
*ptimer1
;
76 uint32_t capabilities
;
86 typedef struct MilkymistSysctlState MilkymistSysctlState
;
88 static void sysctl_icap_write(MilkymistSysctlState
*s
, uint32_t value
)
90 trace_milkymist_sysctl_icap_write(value
);
91 switch (value
& 0xffff) {
93 qemu_system_shutdown_request();
98 static uint64_t sysctl_read(void *opaque
, hwaddr addr
,
101 MilkymistSysctlState
*s
= opaque
;
106 case R_TIMER0_COUNTER
:
107 r
= (uint32_t)ptimer_get_count(s
->ptimer0
);
108 /* milkymist timer counts up */
109 r
= s
->regs
[R_TIMER0_COMPARE
] - r
;
111 case R_TIMER1_COUNTER
:
112 r
= (uint32_t)ptimer_get_count(s
->ptimer1
);
113 /* milkymist timer counts up */
114 r
= s
->regs
[R_TIMER1_COMPARE
] - r
;
119 case R_TIMER0_CONTROL
:
120 case R_TIMER0_COMPARE
:
121 case R_TIMER1_CONTROL
:
122 case R_TIMER1_COMPARE
:
124 case R_DBG_SCRATCHPAD
:
125 case R_DBG_WRITE_LOCK
:
126 case R_CLK_FREQUENCY
:
133 error_report("milkymist_sysctl: read access to unknown register 0x"
134 TARGET_FMT_plx
, addr
<< 2);
138 trace_milkymist_sysctl_memory_read(addr
<< 2, r
);
143 static void sysctl_write(void *opaque
, hwaddr addr
, uint64_t value
,
146 MilkymistSysctlState
*s
= opaque
;
148 trace_milkymist_sysctl_memory_write(addr
, value
);
154 case R_TIMER0_COUNTER
:
155 case R_TIMER1_COUNTER
:
156 case R_DBG_SCRATCHPAD
:
157 s
->regs
[addr
] = value
;
159 case R_TIMER0_COMPARE
:
160 ptimer_set_limit(s
->ptimer0
, value
, 0);
161 s
->regs
[addr
] = value
;
163 case R_TIMER1_COMPARE
:
164 ptimer_set_limit(s
->ptimer1
, value
, 0);
165 s
->regs
[addr
] = value
;
167 case R_TIMER0_CONTROL
:
168 s
->regs
[addr
] = value
;
169 if (s
->regs
[R_TIMER0_CONTROL
] & CTRL_ENABLE
) {
170 trace_milkymist_sysctl_start_timer0();
171 ptimer_set_count(s
->ptimer0
,
172 s
->regs
[R_TIMER0_COMPARE
] - s
->regs
[R_TIMER0_COUNTER
]);
173 ptimer_run(s
->ptimer0
, 0);
175 trace_milkymist_sysctl_stop_timer0();
176 ptimer_stop(s
->ptimer0
);
179 case R_TIMER1_CONTROL
:
180 s
->regs
[addr
] = value
;
181 if (s
->regs
[R_TIMER1_CONTROL
] & CTRL_ENABLE
) {
182 trace_milkymist_sysctl_start_timer1();
183 ptimer_set_count(s
->ptimer1
,
184 s
->regs
[R_TIMER1_COMPARE
] - s
->regs
[R_TIMER1_COUNTER
]);
185 ptimer_run(s
->ptimer1
, 0);
187 trace_milkymist_sysctl_stop_timer1();
188 ptimer_stop(s
->ptimer1
);
192 sysctl_icap_write(s
, value
);
194 case R_DBG_WRITE_LOCK
:
198 qemu_system_reset_request();
202 case R_CLK_FREQUENCY
:
204 error_report("milkymist_sysctl: write to read-only register 0x"
205 TARGET_FMT_plx
, addr
<< 2);
209 error_report("milkymist_sysctl: write access to unknown register 0x"
210 TARGET_FMT_plx
, addr
<< 2);
215 static const MemoryRegionOps sysctl_mmio_ops
= {
217 .write
= sysctl_write
,
219 .min_access_size
= 4,
220 .max_access_size
= 4,
222 .endianness
= DEVICE_NATIVE_ENDIAN
,
225 static void timer0_hit(void *opaque
)
227 MilkymistSysctlState
*s
= opaque
;
229 if (!(s
->regs
[R_TIMER0_CONTROL
] & CTRL_AUTORESTART
)) {
230 s
->regs
[R_TIMER0_CONTROL
] &= ~CTRL_ENABLE
;
231 trace_milkymist_sysctl_stop_timer0();
232 ptimer_stop(s
->ptimer0
);
235 trace_milkymist_sysctl_pulse_irq_timer0();
236 qemu_irq_pulse(s
->timer0_irq
);
239 static void timer1_hit(void *opaque
)
241 MilkymistSysctlState
*s
= opaque
;
243 if (!(s
->regs
[R_TIMER1_CONTROL
] & CTRL_AUTORESTART
)) {
244 s
->regs
[R_TIMER1_CONTROL
] &= ~CTRL_ENABLE
;
245 trace_milkymist_sysctl_stop_timer1();
246 ptimer_stop(s
->ptimer1
);
249 trace_milkymist_sysctl_pulse_irq_timer1();
250 qemu_irq_pulse(s
->timer1_irq
);
253 static void milkymist_sysctl_reset(DeviceState
*d
)
255 MilkymistSysctlState
*s
= MILKYMIST_SYSCTL(d
);
258 for (i
= 0; i
< R_MAX
; i
++) {
262 ptimer_stop(s
->ptimer0
);
263 ptimer_stop(s
->ptimer1
);
266 s
->regs
[R_ICAP
] = ICAP_READY
;
267 s
->regs
[R_SYSTEM_ID
] = s
->systemid
;
268 s
->regs
[R_CLK_FREQUENCY
] = s
->freq_hz
;
269 s
->regs
[R_CAPABILITIES
] = s
->capabilities
;
270 s
->regs
[R_GPIO_IN
] = s
->strappings
;
273 static void milkymist_sysctl_init(Object
*obj
)
275 MilkymistSysctlState
*s
= MILKYMIST_SYSCTL(obj
);
276 SysBusDevice
*dev
= SYS_BUS_DEVICE(obj
);
278 sysbus_init_irq(dev
, &s
->gpio_irq
);
279 sysbus_init_irq(dev
, &s
->timer0_irq
);
280 sysbus_init_irq(dev
, &s
->timer1_irq
);
282 s
->bh0
= qemu_bh_new(timer0_hit
, s
);
283 s
->bh1
= qemu_bh_new(timer1_hit
, s
);
284 s
->ptimer0
= ptimer_init(s
->bh0
, PTIMER_POLICY_DEFAULT
);
285 s
->ptimer1
= ptimer_init(s
->bh1
, PTIMER_POLICY_DEFAULT
);
287 memory_region_init_io(&s
->regs_region
, obj
, &sysctl_mmio_ops
, s
,
288 "milkymist-sysctl", R_MAX
* 4);
289 sysbus_init_mmio(dev
, &s
->regs_region
);
292 static void milkymist_sysctl_realize(DeviceState
*dev
, Error
**errp
)
294 MilkymistSysctlState
*s
= MILKYMIST_SYSCTL(dev
);
296 ptimer_set_freq(s
->ptimer0
, s
->freq_hz
);
297 ptimer_set_freq(s
->ptimer1
, s
->freq_hz
);
300 static const VMStateDescription vmstate_milkymist_sysctl
= {
301 .name
= "milkymist-sysctl",
303 .minimum_version_id
= 1,
304 .fields
= (VMStateField
[]) {
305 VMSTATE_UINT32_ARRAY(regs
, MilkymistSysctlState
, R_MAX
),
306 VMSTATE_PTIMER(ptimer0
, MilkymistSysctlState
),
307 VMSTATE_PTIMER(ptimer1
, MilkymistSysctlState
),
308 VMSTATE_END_OF_LIST()
312 static Property milkymist_sysctl_properties
[] = {
313 DEFINE_PROP_UINT32("frequency", MilkymistSysctlState
,
315 DEFINE_PROP_UINT32("capabilities", MilkymistSysctlState
,
316 capabilities
, 0x00000000),
317 DEFINE_PROP_UINT32("systemid", MilkymistSysctlState
,
318 systemid
, 0x10014d31),
319 DEFINE_PROP_UINT32("gpio_strappings", MilkymistSysctlState
,
320 strappings
, 0x00000001),
321 DEFINE_PROP_END_OF_LIST(),
324 static void milkymist_sysctl_class_init(ObjectClass
*klass
, void *data
)
326 DeviceClass
*dc
= DEVICE_CLASS(klass
);
328 dc
->realize
= milkymist_sysctl_realize
;
329 dc
->reset
= milkymist_sysctl_reset
;
330 dc
->vmsd
= &vmstate_milkymist_sysctl
;
331 dc
->props
= milkymist_sysctl_properties
;
334 static const TypeInfo milkymist_sysctl_info
= {
335 .name
= TYPE_MILKYMIST_SYSCTL
,
336 .parent
= TYPE_SYS_BUS_DEVICE
,
337 .instance_size
= sizeof(MilkymistSysctlState
),
338 .instance_init
= milkymist_sysctl_init
,
339 .class_init
= milkymist_sysctl_class_init
,
342 static void milkymist_sysctl_register_types(void)
344 type_register_static(&milkymist_sysctl_info
);
347 type_init(milkymist_sysctl_register_types
)