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://www.milkymist.org/socdoc/sysctl.pdf
25 #include "hw/sysbus.h"
26 #include "sysemu/sysemu.h"
28 #include "qemu/timer.h"
29 #include "hw/ptimer.h"
30 #include "qemu/error-report.h"
34 CTRL_AUTORESTART
= (1<<1),
52 R_DBG_SCRATCHPAD
= 20,
60 struct MilkymistSysctlState
{
62 MemoryRegion regs_region
;
66 ptimer_state
*ptimer0
;
67 ptimer_state
*ptimer1
;
70 uint32_t capabilities
;
80 typedef struct MilkymistSysctlState MilkymistSysctlState
;
82 static void sysctl_icap_write(MilkymistSysctlState
*s
, uint32_t value
)
84 trace_milkymist_sysctl_icap_write(value
);
85 switch (value
& 0xffff) {
87 qemu_system_shutdown_request();
92 static uint64_t sysctl_read(void *opaque
, hwaddr addr
,
95 MilkymistSysctlState
*s
= opaque
;
100 case R_TIMER0_COUNTER
:
101 r
= (uint32_t)ptimer_get_count(s
->ptimer0
);
102 /* milkymist timer counts up */
103 r
= s
->regs
[R_TIMER0_COMPARE
] - r
;
105 case R_TIMER1_COUNTER
:
106 r
= (uint32_t)ptimer_get_count(s
->ptimer1
);
107 /* milkymist timer counts up */
108 r
= s
->regs
[R_TIMER1_COMPARE
] - r
;
113 case R_TIMER0_CONTROL
:
114 case R_TIMER0_COMPARE
:
115 case R_TIMER1_CONTROL
:
116 case R_TIMER1_COMPARE
:
118 case R_DBG_SCRATCHPAD
:
119 case R_DBG_WRITE_LOCK
:
120 case R_CLK_FREQUENCY
:
127 error_report("milkymist_sysctl: read access to unknown register 0x"
128 TARGET_FMT_plx
, addr
<< 2);
132 trace_milkymist_sysctl_memory_read(addr
<< 2, r
);
137 static void sysctl_write(void *opaque
, hwaddr addr
, uint64_t value
,
140 MilkymistSysctlState
*s
= opaque
;
142 trace_milkymist_sysctl_memory_write(addr
, value
);
148 case R_TIMER0_COUNTER
:
149 case R_TIMER1_COUNTER
:
150 case R_DBG_SCRATCHPAD
:
151 s
->regs
[addr
] = value
;
153 case R_TIMER0_COMPARE
:
154 ptimer_set_limit(s
->ptimer0
, value
, 0);
155 s
->regs
[addr
] = value
;
157 case R_TIMER1_COMPARE
:
158 ptimer_set_limit(s
->ptimer1
, value
, 0);
159 s
->regs
[addr
] = value
;
161 case R_TIMER0_CONTROL
:
162 s
->regs
[addr
] = value
;
163 if (s
->regs
[R_TIMER0_CONTROL
] & CTRL_ENABLE
) {
164 trace_milkymist_sysctl_start_timer0();
165 ptimer_set_count(s
->ptimer0
,
166 s
->regs
[R_TIMER0_COMPARE
] - s
->regs
[R_TIMER0_COUNTER
]);
167 ptimer_run(s
->ptimer0
, 0);
169 trace_milkymist_sysctl_stop_timer0();
170 ptimer_stop(s
->ptimer0
);
173 case R_TIMER1_CONTROL
:
174 s
->regs
[addr
] = value
;
175 if (s
->regs
[R_TIMER1_CONTROL
] & CTRL_ENABLE
) {
176 trace_milkymist_sysctl_start_timer1();
177 ptimer_set_count(s
->ptimer1
,
178 s
->regs
[R_TIMER1_COMPARE
] - s
->regs
[R_TIMER1_COUNTER
]);
179 ptimer_run(s
->ptimer1
, 0);
181 trace_milkymist_sysctl_stop_timer1();
182 ptimer_stop(s
->ptimer1
);
186 sysctl_icap_write(s
, value
);
188 case R_DBG_WRITE_LOCK
:
192 qemu_system_reset_request();
196 case R_CLK_FREQUENCY
:
198 error_report("milkymist_sysctl: write to read-only register 0x"
199 TARGET_FMT_plx
, addr
<< 2);
203 error_report("milkymist_sysctl: write access to unknown register 0x"
204 TARGET_FMT_plx
, addr
<< 2);
209 static const MemoryRegionOps sysctl_mmio_ops
= {
211 .write
= sysctl_write
,
213 .min_access_size
= 4,
214 .max_access_size
= 4,
216 .endianness
= DEVICE_NATIVE_ENDIAN
,
219 static void timer0_hit(void *opaque
)
221 MilkymistSysctlState
*s
= opaque
;
223 if (!(s
->regs
[R_TIMER0_CONTROL
] & CTRL_AUTORESTART
)) {
224 s
->regs
[R_TIMER0_CONTROL
] &= ~CTRL_ENABLE
;
225 trace_milkymist_sysctl_stop_timer0();
226 ptimer_stop(s
->ptimer0
);
229 trace_milkymist_sysctl_pulse_irq_timer0();
230 qemu_irq_pulse(s
->timer0_irq
);
233 static void timer1_hit(void *opaque
)
235 MilkymistSysctlState
*s
= opaque
;
237 if (!(s
->regs
[R_TIMER1_CONTROL
] & CTRL_AUTORESTART
)) {
238 s
->regs
[R_TIMER1_CONTROL
] &= ~CTRL_ENABLE
;
239 trace_milkymist_sysctl_stop_timer1();
240 ptimer_stop(s
->ptimer1
);
243 trace_milkymist_sysctl_pulse_irq_timer1();
244 qemu_irq_pulse(s
->timer1_irq
);
247 static void milkymist_sysctl_reset(DeviceState
*d
)
249 MilkymistSysctlState
*s
=
250 container_of(d
, MilkymistSysctlState
, busdev
.qdev
);
253 for (i
= 0; i
< R_MAX
; i
++) {
257 ptimer_stop(s
->ptimer0
);
258 ptimer_stop(s
->ptimer1
);
261 s
->regs
[R_ICAP
] = ICAP_READY
;
262 s
->regs
[R_SYSTEM_ID
] = s
->systemid
;
263 s
->regs
[R_CLK_FREQUENCY
] = s
->freq_hz
;
264 s
->regs
[R_CAPABILITIES
] = s
->capabilities
;
265 s
->regs
[R_GPIO_IN
] = s
->strappings
;
268 static int milkymist_sysctl_init(SysBusDevice
*dev
)
270 MilkymistSysctlState
*s
= FROM_SYSBUS(typeof(*s
), dev
);
272 sysbus_init_irq(dev
, &s
->gpio_irq
);
273 sysbus_init_irq(dev
, &s
->timer0_irq
);
274 sysbus_init_irq(dev
, &s
->timer1_irq
);
276 s
->bh0
= qemu_bh_new(timer0_hit
, s
);
277 s
->bh1
= qemu_bh_new(timer1_hit
, s
);
278 s
->ptimer0
= ptimer_init(s
->bh0
);
279 s
->ptimer1
= ptimer_init(s
->bh1
);
280 ptimer_set_freq(s
->ptimer0
, s
->freq_hz
);
281 ptimer_set_freq(s
->ptimer1
, s
->freq_hz
);
283 memory_region_init_io(&s
->regs_region
, &sysctl_mmio_ops
, s
,
284 "milkymist-sysctl", R_MAX
* 4);
285 sysbus_init_mmio(dev
, &s
->regs_region
);
290 static const VMStateDescription vmstate_milkymist_sysctl
= {
291 .name
= "milkymist-sysctl",
293 .minimum_version_id
= 1,
294 .minimum_version_id_old
= 1,
295 .fields
= (VMStateField
[]) {
296 VMSTATE_UINT32_ARRAY(regs
, MilkymistSysctlState
, R_MAX
),
297 VMSTATE_PTIMER(ptimer0
, MilkymistSysctlState
),
298 VMSTATE_PTIMER(ptimer1
, MilkymistSysctlState
),
299 VMSTATE_END_OF_LIST()
303 static Property milkymist_sysctl_properties
[] = {
304 DEFINE_PROP_UINT32("frequency", MilkymistSysctlState
,
306 DEFINE_PROP_UINT32("capabilities", MilkymistSysctlState
,
307 capabilities
, 0x00000000),
308 DEFINE_PROP_UINT32("systemid", MilkymistSysctlState
,
309 systemid
, 0x10014d31),
310 DEFINE_PROP_UINT32("gpio_strappings", MilkymistSysctlState
,
311 strappings
, 0x00000001),
312 DEFINE_PROP_END_OF_LIST(),
315 static void milkymist_sysctl_class_init(ObjectClass
*klass
, void *data
)
317 DeviceClass
*dc
= DEVICE_CLASS(klass
);
318 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
320 k
->init
= milkymist_sysctl_init
;
321 dc
->reset
= milkymist_sysctl_reset
;
322 dc
->vmsd
= &vmstate_milkymist_sysctl
;
323 dc
->props
= milkymist_sysctl_properties
;
326 static const TypeInfo milkymist_sysctl_info
= {
327 .name
= "milkymist-sysctl",
328 .parent
= TYPE_SYS_BUS_DEVICE
,
329 .instance_size
= sizeof(MilkymistSysctlState
),
330 .class_init
= milkymist_sysctl_class_init
,
333 static void milkymist_sysctl_register_types(void)
335 type_register_static(&milkymist_sysctl_info
);
338 type_init(milkymist_sysctl_register_types
)