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 #define TYPE_MILKYMIST_SYSCTL "milkymist-sysctl"
61 #define MILKYMIST_SYSCTL(obj) \
62 OBJECT_CHECK(MilkymistSysctlState, (obj), TYPE_MILKYMIST_SYSCTL)
64 struct MilkymistSysctlState
{
65 SysBusDevice parent_obj
;
67 MemoryRegion regs_region
;
71 ptimer_state
*ptimer0
;
72 ptimer_state
*ptimer1
;
75 uint32_t capabilities
;
85 typedef struct MilkymistSysctlState MilkymistSysctlState
;
87 static void sysctl_icap_write(MilkymistSysctlState
*s
, uint32_t value
)
89 trace_milkymist_sysctl_icap_write(value
);
90 switch (value
& 0xffff) {
92 qemu_system_shutdown_request();
97 static uint64_t sysctl_read(void *opaque
, hwaddr addr
,
100 MilkymistSysctlState
*s
= opaque
;
105 case R_TIMER0_COUNTER
:
106 r
= (uint32_t)ptimer_get_count(s
->ptimer0
);
107 /* milkymist timer counts up */
108 r
= s
->regs
[R_TIMER0_COMPARE
] - r
;
110 case R_TIMER1_COUNTER
:
111 r
= (uint32_t)ptimer_get_count(s
->ptimer1
);
112 /* milkymist timer counts up */
113 r
= s
->regs
[R_TIMER1_COMPARE
] - r
;
118 case R_TIMER0_CONTROL
:
119 case R_TIMER0_COMPARE
:
120 case R_TIMER1_CONTROL
:
121 case R_TIMER1_COMPARE
:
123 case R_DBG_SCRATCHPAD
:
124 case R_DBG_WRITE_LOCK
:
125 case R_CLK_FREQUENCY
:
132 error_report("milkymist_sysctl: read access to unknown register 0x"
133 TARGET_FMT_plx
, addr
<< 2);
137 trace_milkymist_sysctl_memory_read(addr
<< 2, r
);
142 static void sysctl_write(void *opaque
, hwaddr addr
, uint64_t value
,
145 MilkymistSysctlState
*s
= opaque
;
147 trace_milkymist_sysctl_memory_write(addr
, value
);
153 case R_TIMER0_COUNTER
:
154 case R_TIMER1_COUNTER
:
155 case R_DBG_SCRATCHPAD
:
156 s
->regs
[addr
] = value
;
158 case R_TIMER0_COMPARE
:
159 ptimer_set_limit(s
->ptimer0
, value
, 0);
160 s
->regs
[addr
] = value
;
162 case R_TIMER1_COMPARE
:
163 ptimer_set_limit(s
->ptimer1
, value
, 0);
164 s
->regs
[addr
] = value
;
166 case R_TIMER0_CONTROL
:
167 s
->regs
[addr
] = value
;
168 if (s
->regs
[R_TIMER0_CONTROL
] & CTRL_ENABLE
) {
169 trace_milkymist_sysctl_start_timer0();
170 ptimer_set_count(s
->ptimer0
,
171 s
->regs
[R_TIMER0_COMPARE
] - s
->regs
[R_TIMER0_COUNTER
]);
172 ptimer_run(s
->ptimer0
, 0);
174 trace_milkymist_sysctl_stop_timer0();
175 ptimer_stop(s
->ptimer0
);
178 case R_TIMER1_CONTROL
:
179 s
->regs
[addr
] = value
;
180 if (s
->regs
[R_TIMER1_CONTROL
] & CTRL_ENABLE
) {
181 trace_milkymist_sysctl_start_timer1();
182 ptimer_set_count(s
->ptimer1
,
183 s
->regs
[R_TIMER1_COMPARE
] - s
->regs
[R_TIMER1_COUNTER
]);
184 ptimer_run(s
->ptimer1
, 0);
186 trace_milkymist_sysctl_stop_timer1();
187 ptimer_stop(s
->ptimer1
);
191 sysctl_icap_write(s
, value
);
193 case R_DBG_WRITE_LOCK
:
197 qemu_system_reset_request();
201 case R_CLK_FREQUENCY
:
203 error_report("milkymist_sysctl: write to read-only register 0x"
204 TARGET_FMT_plx
, addr
<< 2);
208 error_report("milkymist_sysctl: write access to unknown register 0x"
209 TARGET_FMT_plx
, addr
<< 2);
214 static const MemoryRegionOps sysctl_mmio_ops
= {
216 .write
= sysctl_write
,
218 .min_access_size
= 4,
219 .max_access_size
= 4,
221 .endianness
= DEVICE_NATIVE_ENDIAN
,
224 static void timer0_hit(void *opaque
)
226 MilkymistSysctlState
*s
= opaque
;
228 if (!(s
->regs
[R_TIMER0_CONTROL
] & CTRL_AUTORESTART
)) {
229 s
->regs
[R_TIMER0_CONTROL
] &= ~CTRL_ENABLE
;
230 trace_milkymist_sysctl_stop_timer0();
231 ptimer_stop(s
->ptimer0
);
234 trace_milkymist_sysctl_pulse_irq_timer0();
235 qemu_irq_pulse(s
->timer0_irq
);
238 static void timer1_hit(void *opaque
)
240 MilkymistSysctlState
*s
= opaque
;
242 if (!(s
->regs
[R_TIMER1_CONTROL
] & CTRL_AUTORESTART
)) {
243 s
->regs
[R_TIMER1_CONTROL
] &= ~CTRL_ENABLE
;
244 trace_milkymist_sysctl_stop_timer1();
245 ptimer_stop(s
->ptimer1
);
248 trace_milkymist_sysctl_pulse_irq_timer1();
249 qemu_irq_pulse(s
->timer1_irq
);
252 static void milkymist_sysctl_reset(DeviceState
*d
)
254 MilkymistSysctlState
*s
= MILKYMIST_SYSCTL(d
);
257 for (i
= 0; i
< R_MAX
; i
++) {
261 ptimer_stop(s
->ptimer0
);
262 ptimer_stop(s
->ptimer1
);
265 s
->regs
[R_ICAP
] = ICAP_READY
;
266 s
->regs
[R_SYSTEM_ID
] = s
->systemid
;
267 s
->regs
[R_CLK_FREQUENCY
] = s
->freq_hz
;
268 s
->regs
[R_CAPABILITIES
] = s
->capabilities
;
269 s
->regs
[R_GPIO_IN
] = s
->strappings
;
272 static int milkymist_sysctl_init(SysBusDevice
*dev
)
274 MilkymistSysctlState
*s
= MILKYMIST_SYSCTL(dev
);
276 sysbus_init_irq(dev
, &s
->gpio_irq
);
277 sysbus_init_irq(dev
, &s
->timer0_irq
);
278 sysbus_init_irq(dev
, &s
->timer1_irq
);
280 s
->bh0
= qemu_bh_new(timer0_hit
, s
);
281 s
->bh1
= qemu_bh_new(timer1_hit
, s
);
282 s
->ptimer0
= ptimer_init(s
->bh0
);
283 s
->ptimer1
= ptimer_init(s
->bh1
);
284 ptimer_set_freq(s
->ptimer0
, s
->freq_hz
);
285 ptimer_set_freq(s
->ptimer1
, s
->freq_hz
);
287 memory_region_init_io(&s
->regs_region
, OBJECT(s
), &sysctl_mmio_ops
, s
,
288 "milkymist-sysctl", R_MAX
* 4);
289 sysbus_init_mmio(dev
, &s
->regs_region
);
294 static const VMStateDescription vmstate_milkymist_sysctl
= {
295 .name
= "milkymist-sysctl",
297 .minimum_version_id
= 1,
298 .fields
= (VMStateField
[]) {
299 VMSTATE_UINT32_ARRAY(regs
, MilkymistSysctlState
, R_MAX
),
300 VMSTATE_PTIMER(ptimer0
, MilkymistSysctlState
),
301 VMSTATE_PTIMER(ptimer1
, MilkymistSysctlState
),
302 VMSTATE_END_OF_LIST()
306 static Property milkymist_sysctl_properties
[] = {
307 DEFINE_PROP_UINT32("frequency", MilkymistSysctlState
,
309 DEFINE_PROP_UINT32("capabilities", MilkymistSysctlState
,
310 capabilities
, 0x00000000),
311 DEFINE_PROP_UINT32("systemid", MilkymistSysctlState
,
312 systemid
, 0x10014d31),
313 DEFINE_PROP_UINT32("gpio_strappings", MilkymistSysctlState
,
314 strappings
, 0x00000001),
315 DEFINE_PROP_END_OF_LIST(),
318 static void milkymist_sysctl_class_init(ObjectClass
*klass
, void *data
)
320 DeviceClass
*dc
= DEVICE_CLASS(klass
);
321 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
323 k
->init
= milkymist_sysctl_init
;
324 dc
->reset
= milkymist_sysctl_reset
;
325 dc
->vmsd
= &vmstate_milkymist_sysctl
;
326 dc
->props
= milkymist_sysctl_properties
;
329 static const TypeInfo milkymist_sysctl_info
= {
330 .name
= TYPE_MILKYMIST_SYSCTL
,
331 .parent
= TYPE_SYS_BUS_DEVICE
,
332 .instance_size
= sizeof(MilkymistSysctlState
),
333 .class_init
= milkymist_sysctl_class_init
,
336 static void milkymist_sysctl_register_types(void)
338 type_register_static(&milkymist_sysctl_info
);
341 type_init(milkymist_sysctl_register_types
)