2 * ARM MPS2 SCC emulation
4 * Copyright (c) 2017 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.
12 /* This is a model of the SCC (Serial Communication Controller)
13 * found in the FPGA images of MPS2 development boards.
15 * Documentation of it can be found in the MPS2 TRM:
16 * https://developer.arm.com/documentation/100112/latest/
17 * and also in the Application Notes documenting individual FPGA images.
20 #include "qemu/osdep.h"
22 #include "qemu/module.h"
23 #include "qemu/bitops.h"
25 #include "hw/sysbus.h"
27 #include "migration/vmstate.h"
28 #include "hw/registerfields.h"
29 #include "hw/misc/mps2-scc.h"
30 #include "hw/misc/led.h"
31 #include "hw/qdev-properties.h"
41 REG32(CFGDATA_RTN
, 0xa0)
42 REG32(CFGDATA_OUT
, 0xa4)
44 FIELD(CFGCTRL
, DEVICE
, 0, 12)
45 FIELD(CFGCTRL
, RES1
, 12, 8)
46 FIELD(CFGCTRL
, FUNCTION
, 20, 6)
47 FIELD(CFGCTRL
, RES2
, 26, 4)
48 FIELD(CFGCTRL
, WRITE
, 30, 1)
49 FIELD(CFGCTRL
, START
, 31, 1)
51 FIELD(CFGSTAT
, DONE
, 0, 1)
52 FIELD(CFGSTAT
, ERROR
, 1, 1)
57 static int scc_partno(MPS2SCC
*s
)
59 /* Return the partno field of the SCC_ID (0x524, 0x511, etc) */
60 return extract32(s
->id
, 4, 8);
63 /* Is CFG_REG2 present? */
64 static bool have_cfg2(MPS2SCC
*s
)
66 return scc_partno(s
) == 0x524 || scc_partno(s
) == 0x547 ||
67 scc_partno(s
) == 0x536;
70 /* Is CFG_REG3 present? */
71 static bool have_cfg3(MPS2SCC
*s
)
73 return scc_partno(s
) != 0x524 && scc_partno(s
) != 0x547 &&
74 scc_partno(s
) != 0x536;
77 /* Is CFG_REG5 present? */
78 static bool have_cfg5(MPS2SCC
*s
)
80 return scc_partno(s
) == 0x524 || scc_partno(s
) == 0x547 ||
81 scc_partno(s
) == 0x536;
84 /* Is CFG_REG6 present? */
85 static bool have_cfg6(MPS2SCC
*s
)
87 return scc_partno(s
) == 0x524 || scc_partno(s
) == 0x536;
90 /* Is CFG_REG7 present? */
91 static bool have_cfg7(MPS2SCC
*s
)
93 return scc_partno(s
) == 0x536;
96 /* Does CFG_REG0 drive the 'remap' GPIO output? */
97 static bool cfg0_is_remap(MPS2SCC
*s
)
99 return scc_partno(s
) != 0x536;
102 /* Is CFG_REG1 driving a set of LEDs? */
103 static bool cfg1_is_leds(MPS2SCC
*s
)
105 return scc_partno(s
) != 0x536;
108 /* Handle a write via the SYS_CFG channel to the specified function/device.
109 * Return false on error (reported to guest via SYS_CFGCTRL ERROR bit).
111 static bool scc_cfg_write(MPS2SCC
*s
, unsigned function
,
112 unsigned device
, uint32_t value
)
114 trace_mps2_scc_cfg_write(function
, device
, value
);
116 if (function
!= 1 || device
>= s
->num_oscclk
) {
117 qemu_log_mask(LOG_GUEST_ERROR
,
118 "MPS2 SCC config write: bad function %d device %d\n",
123 s
->oscclk
[device
] = value
;
127 /* Handle a read via the SYS_CFG channel to the specified function/device.
128 * Return false on error (reported to guest via SYS_CFGCTRL ERROR bit),
129 * or set *value on success.
131 static bool scc_cfg_read(MPS2SCC
*s
, unsigned function
,
132 unsigned device
, uint32_t *value
)
134 if (function
!= 1 || device
>= s
->num_oscclk
) {
135 qemu_log_mask(LOG_GUEST_ERROR
,
136 "MPS2 SCC config read: bad function %d device %d\n",
141 *value
= s
->oscclk
[device
];
143 trace_mps2_scc_cfg_read(function
, device
, *value
);
147 static uint64_t mps2_scc_read(void *opaque
, hwaddr offset
, unsigned size
)
149 MPS2SCC
*s
= MPS2_SCC(opaque
);
170 * These are user-settable DIP switches on the board. We don't
171 * model that, so just return zeroes.
173 * TODO: for AN536 this is MCC_MSB_ADDR "additional MCC addressing
174 * bits". These change which part of the DDR4 the motherboard
175 * configuration controller can see in its memory map (see the
176 * appnote section 2.4). QEMU doesn't model the MCC at all, so these
177 * bits are not interesting to us; read-as-zero is as good as anything
226 qemu_log_mask(LOG_GUEST_ERROR
,
227 "MPS2 SCC read: bad offset %x\n", (int) offset
);
232 trace_mps2_scc_read(offset
, r
, size
);
236 static void mps2_scc_write(void *opaque
, hwaddr offset
, uint64_t value
,
239 MPS2SCC
*s
= MPS2_SCC(opaque
);
241 trace_mps2_scc_write(offset
, value
, size
);
246 * On some boards bit 0 controls board-specific remapping;
247 * we always reflect bit 0 in the 'remap' GPIO output line,
248 * and let the board wire it up or not as it chooses.
249 * TODO on some boards bit 1 is CPU_WAIT.
251 * TODO: on the AN536 this register controls reset and halt
252 * for both CPUs. For the moment we don't implement this, so the
253 * register just reads as written.
256 if (cfg0_is_remap(s
)) {
257 qemu_set_irq(s
->remap
, s
->cfg0
& 1);
263 * On most boards this register drives LEDs.
265 * TODO: for AN536 this controls whether flash and ATCM are
266 * enabled or disabled on reset. QEMU doesn't model this, and
267 * always wires up RAM in the ATCM area and ROM in the flash area.
269 if (cfg1_is_leds(s
)) {
270 for (size_t i
= 0; i
< ARRAY_SIZE(s
->led
); i
++) {
271 led_set_state(s
->led
[i
], extract32(value
, i
, 1));
279 /* AN524, AN536: QSPI Select signal */
286 /* AN524, AN536: ACLK frequency in Hz */
293 /* AN524: Clock divider for BRAM */
294 /* AN536: Core 0 vector table base address */
301 /* AN536: Core 1 vector table base address */
305 s
->cfgdata_out
= value
;
308 /* Writing to CFGCTRL clears SYS_CFGSTAT */
310 s
->cfgctrl
= value
& ~(R_CFGCTRL_RES1_MASK
|
311 R_CFGCTRL_RES2_MASK
|
312 R_CFGCTRL_START_MASK
);
314 if (value
& R_CFGCTRL_START_MASK
) {
315 /* Start bit set -- do a read or write (instantaneously) */
316 int device
= extract32(s
->cfgctrl
, R_CFGCTRL_DEVICE_SHIFT
,
317 R_CFGCTRL_DEVICE_LENGTH
);
318 int function
= extract32(s
->cfgctrl
, R_CFGCTRL_FUNCTION_SHIFT
,
319 R_CFGCTRL_FUNCTION_LENGTH
);
321 s
->cfgstat
= R_CFGSTAT_DONE_MASK
;
322 if (s
->cfgctrl
& R_CFGCTRL_WRITE_MASK
) {
323 if (!scc_cfg_write(s
, function
, device
, s
->cfgdata_out
)) {
324 s
->cfgstat
|= R_CFGSTAT_ERROR_MASK
;
328 if (!scc_cfg_read(s
, function
, device
, &result
)) {
329 s
->cfgstat
|= R_CFGSTAT_ERROR_MASK
;
331 s
->cfgdata_rtn
= result
;
337 /* DLL stands for Digital Locked Loop.
338 * Bits [31:24] (DLL_LOCK_MASK) are writable, and indicate a
339 * mask of which of the DLL_LOCKED bits [16:23] should be ORed
340 * together to determine the ALL_UNMASKED_DLLS_LOCKED bit [0].
341 * For QEMU, our DLLs are always locked, so we can leave bit 0
342 * as 1 always and don't need to recalculate it.
344 s
->dll
= deposit32(s
->dll
, 24, 8, extract32(value
, 24, 8));
348 qemu_log_mask(LOG_GUEST_ERROR
,
349 "MPS2 SCC write: bad offset 0x%x\n", (int) offset
);
354 static const MemoryRegionOps mps2_scc_ops
= {
355 .read
= mps2_scc_read
,
356 .write
= mps2_scc_write
,
357 .endianness
= DEVICE_LITTLE_ENDIAN
,
360 static void mps2_scc_reset(DeviceState
*dev
)
362 MPS2SCC
*s
= MPS2_SCC(dev
);
365 trace_mps2_scc_reset();
366 s
->cfg0
= s
->cfg0_reset
;
373 s
->cfgctrl
= 0x100000;
376 for (i
= 0; i
< s
->num_oscclk
; i
++) {
377 s
->oscclk
[i
] = s
->oscclk_reset
[i
];
379 for (i
= 0; i
< ARRAY_SIZE(s
->led
); i
++) {
380 device_cold_reset(DEVICE(s
->led
[i
]));
384 static void mps2_scc_init(Object
*obj
)
386 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
387 MPS2SCC
*s
= MPS2_SCC(obj
);
389 memory_region_init_io(&s
->iomem
, obj
, &mps2_scc_ops
, s
, "mps2-scc", 0x1000);
390 sysbus_init_mmio(sbd
, &s
->iomem
);
391 qdev_init_gpio_out_named(DEVICE(obj
), &s
->remap
, "remap", 1);
394 static void mps2_scc_realize(DeviceState
*dev
, Error
**errp
)
396 MPS2SCC
*s
= MPS2_SCC(dev
);
398 for (size_t i
= 0; i
< ARRAY_SIZE(s
->led
); i
++) {
399 char *name
= g_strdup_printf("SCC LED%zu", i
);
400 s
->led
[i
] = led_create_simple(OBJECT(dev
), GPIO_POLARITY_ACTIVE_HIGH
,
401 LED_COLOR_GREEN
, name
);
405 s
->oscclk
= g_new0(uint32_t, s
->num_oscclk
);
408 static void mps2_scc_finalize(Object
*obj
)
410 MPS2SCC
*s
= MPS2_SCC(obj
);
412 g_free(s
->oscclk_reset
);
415 static bool cfg7_needed(void *opaque
)
422 static const VMStateDescription vmstate_cfg7
= {
423 .name
= "mps2-scc/cfg7",
425 .minimum_version_id
= 1,
426 .needed
= cfg7_needed
,
427 .fields
= (const VMStateField
[]) {
428 VMSTATE_UINT32(cfg7
, MPS2SCC
),
429 VMSTATE_END_OF_LIST()
433 static const VMStateDescription mps2_scc_vmstate
= {
436 .minimum_version_id
= 3,
437 .fields
= (const VMStateField
[]) {
438 VMSTATE_UINT32(cfg0
, MPS2SCC
),
439 VMSTATE_UINT32(cfg1
, MPS2SCC
),
440 VMSTATE_UINT32(cfg2
, MPS2SCC
),
441 /* cfg3, cfg4 are read-only so need not be migrated */
442 VMSTATE_UINT32(cfg5
, MPS2SCC
),
443 VMSTATE_UINT32(cfg6
, MPS2SCC
),
444 VMSTATE_UINT32(cfgdata_rtn
, MPS2SCC
),
445 VMSTATE_UINT32(cfgdata_out
, MPS2SCC
),
446 VMSTATE_UINT32(cfgctrl
, MPS2SCC
),
447 VMSTATE_UINT32(cfgstat
, MPS2SCC
),
448 VMSTATE_UINT32(dll
, MPS2SCC
),
449 VMSTATE_VARRAY_UINT32(oscclk
, MPS2SCC
, num_oscclk
,
450 0, vmstate_info_uint32
, uint32_t),
451 VMSTATE_END_OF_LIST()
453 .subsections
= (const VMStateDescription
* const []) {
459 static Property mps2_scc_properties
[] = {
460 /* Values for various read-only ID registers (which are specific
461 * to the board model or FPGA image)
463 DEFINE_PROP_UINT32("scc-cfg4", MPS2SCC
, cfg4
, 0),
464 DEFINE_PROP_UINT32("scc-aid", MPS2SCC
, aid
, 0),
465 DEFINE_PROP_UINT32("scc-id", MPS2SCC
, id
, 0),
466 /* Reset value for CFG0 register */
467 DEFINE_PROP_UINT32("scc-cfg0", MPS2SCC
, cfg0_reset
, 0),
469 * These are the initial settings for the source clocks on the board.
470 * In hardware they can be configured via a config file read by the
471 * motherboard configuration controller to suit the FPGA image.
473 DEFINE_PROP_ARRAY("oscclk", MPS2SCC
, num_oscclk
, oscclk_reset
,
474 qdev_prop_uint32
, uint32_t),
475 DEFINE_PROP_END_OF_LIST(),
478 static void mps2_scc_class_init(ObjectClass
*klass
, void *data
)
480 DeviceClass
*dc
= DEVICE_CLASS(klass
);
482 dc
->realize
= mps2_scc_realize
;
483 dc
->vmsd
= &mps2_scc_vmstate
;
484 dc
->reset
= mps2_scc_reset
;
485 device_class_set_props(dc
, mps2_scc_properties
);
488 static const TypeInfo mps2_scc_info
= {
489 .name
= TYPE_MPS2_SCC
,
490 .parent
= TYPE_SYS_BUS_DEVICE
,
491 .instance_size
= sizeof(MPS2SCC
),
492 .instance_init
= mps2_scc_init
,
493 .instance_finalize
= mps2_scc_finalize
,
494 .class_init
= mps2_scc_class_init
,
497 static void mps2_scc_register_types(void)
499 type_register_static(&mps2_scc_info
);
502 type_init(mps2_scc_register_types
);