2 * ARM IoTKit system control element
4 * Copyright (c) 2018 Linaro Limited
5 * Written by Peter Maydell
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 or
9 * (at your option) any later version.
13 * This is a model of the "system control element" which is part of the
14 * Arm IoTKit and documented in
15 * http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ecm0601256/index.html
16 * Specifically, it implements the "system control register" blocks.
19 #include "qemu/osdep.h"
20 #include "qemu/bitops.h"
22 #include "qemu/module.h"
23 #include "sysemu/runstate.h"
25 #include "qapi/error.h"
26 #include "hw/sysbus.h"
27 #include "migration/vmstate.h"
28 #include "hw/registerfields.h"
29 #include "hw/misc/iotkit-sysctl.h"
30 #include "hw/qdev-properties.h"
31 #include "target/arm/arm-powerctl.h"
32 #include "target/arm/cpu.h"
34 REG32(SECDBGSTAT
, 0x0)
39 REG32(SYSCLK_DIV
, 0x14)
40 REG32(CLOCK_FORCE
, 0x18)
41 REG32(RESET_SYNDROME
, 0x100)
42 REG32(RESET_MASK
, 0x104)
44 FIELD(SWRESET
, SWRESETREQ
, 9, 1)
46 REG32(INITSVTOR0
, 0x110)
47 REG32(INITSVTOR1
, 0x114)
49 REG32(NMI_ENABLE
, 0x11c) /* BUSWAIT in IoTKit */
52 REG32(PDCM_PD_SYS_SENSE
, 0x200)
53 REG32(PDCM_PD_SRAM0_SENSE
, 0x20c)
54 REG32(PDCM_PD_SRAM1_SENSE
, 0x210)
55 REG32(PDCM_PD_SRAM2_SENSE
, 0x214)
56 REG32(PDCM_PD_SRAM3_SENSE
, 0x218)
71 static const int sysctl_id
[] = {
72 0x04, 0x00, 0x00, 0x00, /* PID4..PID7 */
73 0x54, 0xb8, 0x0b, 0x00, /* PID0..PID3 */
74 0x0d, 0xf0, 0x05, 0xb1, /* CID0..CID3 */
78 * Set the initial secure vector table offset address for the core.
79 * This will take effect when the CPU next resets.
81 static void set_init_vtor(uint64_t cpuid
, uint32_t vtor
)
83 Object
*cpuobj
= OBJECT(arm_get_cpu_by_id(cpuid
));
86 if (object_property_find(cpuobj
, "init-svtor", NULL
)) {
87 object_property_set_uint(cpuobj
, vtor
, "init-svtor", &error_abort
);
92 static uint64_t iotkit_sysctl_read(void *opaque
, hwaddr offset
,
95 IoTKitSysCtl
*s
= IOTKIT_SYSCTL(opaque
);
126 case A_RESET_SYNDROME
:
127 r
= s
->reset_syndrome
;
148 /* In IoTKit this is named BUSWAIT but is marked reserved, R/O, zero */
164 case A_PDCM_PD_SYS_SENSE
:
168 r
= s
->pdcm_pd_sys_sense
;
170 case A_PDCM_PD_SRAM0_SENSE
:
174 r
= s
->pdcm_pd_sram0_sense
;
176 case A_PDCM_PD_SRAM1_SENSE
:
180 r
= s
->pdcm_pd_sram1_sense
;
182 case A_PDCM_PD_SRAM2_SENSE
:
186 r
= s
->pdcm_pd_sram2_sense
;
188 case A_PDCM_PD_SRAM3_SENSE
:
192 r
= s
->pdcm_pd_sram3_sense
;
194 case A_PID4
... A_CID3
:
195 r
= sysctl_id
[(offset
- A_PID4
) / 4];
200 qemu_log_mask(LOG_GUEST_ERROR
,
201 "IoTKit SysCtl read: read of WO offset %x\n",
207 qemu_log_mask(LOG_GUEST_ERROR
,
208 "IoTKit SysCtl read: bad offset %x\n", (int)offset
);
212 trace_iotkit_sysctl_read(offset
, r
, size
);
216 static void iotkit_sysctl_write(void *opaque
, hwaddr offset
,
217 uint64_t value
, unsigned size
)
219 IoTKitSysCtl
*s
= IOTKIT_SYSCTL(opaque
);
221 trace_iotkit_sysctl_write(offset
, value
, size
);
224 * Most of the state here has to do with control of reset and
225 * similar kinds of power up -- for instance the guest can ask
226 * what the reason for the last reset was, or forbid reset for
227 * some causes (like the non-secure watchdog). Most of this is
228 * not relevant to QEMU, which doesn't really model anything other
229 * than a full power-on reset.
230 * We just model the registers as reads-as-written.
234 case A_RESET_SYNDROME
:
235 qemu_log_mask(LOG_UNIMP
,
236 "IoTKit SysCtl RESET_SYNDROME unimplemented\n");
237 s
->reset_syndrome
= value
;
240 qemu_log_mask(LOG_UNIMP
, "IoTKit SysCtl RESET_MASK unimplemented\n");
241 s
->reset_mask
= value
;
245 * General retention register, which is only reset by a power-on
246 * reset. Technically this implementation is complete, since
247 * QEMU only supports power-on resets...
252 s
->initsvtor0
= value
;
253 set_init_vtor(0, s
->initsvtor0
);
256 if ((s
->cpuwait
& 1) && !(value
& 1)) {
257 /* Powering up CPU 0 */
258 arm_set_cpu_on_and_reset(0);
260 if ((s
->cpuwait
& 2) && !(value
& 2)) {
261 /* Powering up CPU 1 */
262 arm_set_cpu_on_and_reset(1);
267 qemu_log_mask(LOG_UNIMP
, "IoTKit SysCtl WICCTRL unimplemented\n");
272 qemu_log_mask(LOG_UNIMP
, "IoTKit SysCtl SECDBGSET unimplemented\n");
273 s
->secure_debug
|= value
;
276 /* write-1-to-clear */
277 s
->secure_debug
&= ~value
;
280 /* One w/o bit to request a reset; all other bits reserved */
281 if (value
& R_SWRESET_SWRESETREQ_MASK
) {
282 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET
);
289 qemu_log_mask(LOG_UNIMP
, "IoTKit SysCtl SCSECCTRL unimplemented\n");
290 s
->scsecctrl
= value
;
296 qemu_log_mask(LOG_UNIMP
, "IoTKit SysCtl FCLK_DIV unimplemented\n");
303 qemu_log_mask(LOG_UNIMP
, "IoTKit SysCtl SYSCLK_DIV unimplemented\n");
304 s
->sysclk_div
= value
;
310 qemu_log_mask(LOG_UNIMP
, "IoTKit SysCtl CLOCK_FORCE unimplemented\n");
311 s
->clock_force
= value
;
317 s
->initsvtor1
= value
;
318 set_init_vtor(1, s
->initsvtor1
);
324 qemu_log_mask(LOG_UNIMP
, "IoTKit SysCtl EWCTRL unimplemented\n");
327 case A_PDCM_PD_SYS_SENSE
:
331 qemu_log_mask(LOG_UNIMP
,
332 "IoTKit SysCtl PDCM_PD_SYS_SENSE unimplemented\n");
333 s
->pdcm_pd_sys_sense
= value
;
335 case A_PDCM_PD_SRAM0_SENSE
:
339 qemu_log_mask(LOG_UNIMP
,
340 "IoTKit SysCtl PDCM_PD_SRAM0_SENSE unimplemented\n");
341 s
->pdcm_pd_sram0_sense
= value
;
343 case A_PDCM_PD_SRAM1_SENSE
:
347 qemu_log_mask(LOG_UNIMP
,
348 "IoTKit SysCtl PDCM_PD_SRAM1_SENSE unimplemented\n");
349 s
->pdcm_pd_sram1_sense
= value
;
351 case A_PDCM_PD_SRAM2_SENSE
:
355 qemu_log_mask(LOG_UNIMP
,
356 "IoTKit SysCtl PDCM_PD_SRAM2_SENSE unimplemented\n");
357 s
->pdcm_pd_sram2_sense
= value
;
359 case A_PDCM_PD_SRAM3_SENSE
:
363 qemu_log_mask(LOG_UNIMP
,
364 "IoTKit SysCtl PDCM_PD_SRAM3_SENSE unimplemented\n");
365 s
->pdcm_pd_sram3_sense
= value
;
368 /* In IoTKit this is BUSWAIT: reserved, R/O, zero */
372 qemu_log_mask(LOG_UNIMP
, "IoTKit SysCtl NMI_ENABLE unimplemented\n");
373 s
->nmi_enable
= value
;
376 case A_PID4
... A_CID3
:
378 qemu_log_mask(LOG_GUEST_ERROR
,
379 "IoTKit SysCtl write: write of RO offset %x\n",
384 qemu_log_mask(LOG_GUEST_ERROR
,
385 "IoTKit SysCtl write: bad offset %x\n", (int)offset
);
390 static const MemoryRegionOps iotkit_sysctl_ops
= {
391 .read
= iotkit_sysctl_read
,
392 .write
= iotkit_sysctl_write
,
393 .endianness
= DEVICE_LITTLE_ENDIAN
,
394 /* byte/halfword accesses are just zero-padded on reads and writes */
395 .impl
.min_access_size
= 4,
396 .impl
.max_access_size
= 4,
397 .valid
.min_access_size
= 1,
398 .valid
.max_access_size
= 4,
401 static void iotkit_sysctl_reset(DeviceState
*dev
)
403 IoTKitSysCtl
*s
= IOTKIT_SYSCTL(dev
);
405 trace_iotkit_sysctl_reset();
407 s
->reset_syndrome
= 1;
410 s
->initsvtor0
= s
->initsvtor0_rst
;
411 s
->initsvtor1
= s
->initsvtor1_rst
;
412 s
->cpuwait
= s
->cpuwait_rst
;
420 s
->pdcm_pd_sys_sense
= 0x7f;
421 s
->pdcm_pd_sram0_sense
= 0;
422 s
->pdcm_pd_sram1_sense
= 0;
423 s
->pdcm_pd_sram2_sense
= 0;
424 s
->pdcm_pd_sram3_sense
= 0;
427 static void iotkit_sysctl_init(Object
*obj
)
429 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
430 IoTKitSysCtl
*s
= IOTKIT_SYSCTL(obj
);
432 memory_region_init_io(&s
->iomem
, obj
, &iotkit_sysctl_ops
,
433 s
, "iotkit-sysctl", 0x1000);
434 sysbus_init_mmio(sbd
, &s
->iomem
);
437 static void iotkit_sysctl_realize(DeviceState
*dev
, Error
**errp
)
439 IoTKitSysCtl
*s
= IOTKIT_SYSCTL(dev
);
441 /* The top 4 bits of the SYS_VERSION register tell us if we're an SSE-200 */
442 if (extract32(s
->sys_version
, 28, 4) == 2) {
447 static bool sse200_needed(void *opaque
)
449 IoTKitSysCtl
*s
= IOTKIT_SYSCTL(opaque
);
454 static const VMStateDescription iotkit_sysctl_sse200_vmstate
= {
455 .name
= "iotkit-sysctl/sse-200",
457 .minimum_version_id
= 1,
458 .needed
= sse200_needed
,
459 .fields
= (VMStateField
[]) {
460 VMSTATE_UINT32(scsecctrl
, IoTKitSysCtl
),
461 VMSTATE_UINT32(fclk_div
, IoTKitSysCtl
),
462 VMSTATE_UINT32(sysclk_div
, IoTKitSysCtl
),
463 VMSTATE_UINT32(clock_force
, IoTKitSysCtl
),
464 VMSTATE_UINT32(initsvtor1
, IoTKitSysCtl
),
465 VMSTATE_UINT32(nmi_enable
, IoTKitSysCtl
),
466 VMSTATE_UINT32(pdcm_pd_sys_sense
, IoTKitSysCtl
),
467 VMSTATE_UINT32(pdcm_pd_sram0_sense
, IoTKitSysCtl
),
468 VMSTATE_UINT32(pdcm_pd_sram1_sense
, IoTKitSysCtl
),
469 VMSTATE_UINT32(pdcm_pd_sram2_sense
, IoTKitSysCtl
),
470 VMSTATE_UINT32(pdcm_pd_sram3_sense
, IoTKitSysCtl
),
471 VMSTATE_END_OF_LIST()
475 static const VMStateDescription iotkit_sysctl_vmstate
= {
476 .name
= "iotkit-sysctl",
478 .minimum_version_id
= 1,
479 .fields
= (VMStateField
[]) {
480 VMSTATE_UINT32(secure_debug
, IoTKitSysCtl
),
481 VMSTATE_UINT32(reset_syndrome
, IoTKitSysCtl
),
482 VMSTATE_UINT32(reset_mask
, IoTKitSysCtl
),
483 VMSTATE_UINT32(gretreg
, IoTKitSysCtl
),
484 VMSTATE_UINT32(initsvtor0
, IoTKitSysCtl
),
485 VMSTATE_UINT32(cpuwait
, IoTKitSysCtl
),
486 VMSTATE_UINT32(wicctrl
, IoTKitSysCtl
),
487 VMSTATE_END_OF_LIST()
489 .subsections
= (const VMStateDescription
*[]) {
490 &iotkit_sysctl_sse200_vmstate
,
495 static Property iotkit_sysctl_props
[] = {
496 DEFINE_PROP_UINT32("SYS_VERSION", IoTKitSysCtl
, sys_version
, 0),
497 DEFINE_PROP_UINT32("CPUWAIT_RST", IoTKitSysCtl
, cpuwait_rst
, 0),
498 DEFINE_PROP_UINT32("INITSVTOR0_RST", IoTKitSysCtl
, initsvtor0_rst
,
500 DEFINE_PROP_UINT32("INITSVTOR1_RST", IoTKitSysCtl
, initsvtor1_rst
,
502 DEFINE_PROP_END_OF_LIST()
505 static void iotkit_sysctl_class_init(ObjectClass
*klass
, void *data
)
507 DeviceClass
*dc
= DEVICE_CLASS(klass
);
509 dc
->vmsd
= &iotkit_sysctl_vmstate
;
510 dc
->reset
= iotkit_sysctl_reset
;
511 device_class_set_props(dc
, iotkit_sysctl_props
);
512 dc
->realize
= iotkit_sysctl_realize
;
515 static const TypeInfo iotkit_sysctl_info
= {
516 .name
= TYPE_IOTKIT_SYSCTL
,
517 .parent
= TYPE_SYS_BUS_DEVICE
,
518 .instance_size
= sizeof(IoTKitSysCtl
),
519 .instance_init
= iotkit_sysctl_init
,
520 .class_init
= iotkit_sysctl_class_init
,
523 static void iotkit_sysctl_register_types(void)
525 type_register_static(&iotkit_sysctl_info
);
528 type_init(iotkit_sysctl_register_types
);