2 * QEMU model of the LatticeMico32 system control block.
4 * Copyright (c) 2010 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/>.
21 * This model is mainly intended for testing purposes and doesn't fit to any
22 * real hardware. On the one hand it provides a control register (R_CTRL) on
23 * the other hand it supports the lm32 tests.
25 * A write to the control register causes a system shutdown.
26 * Tests first write the pointer to a test name to the test name register
27 * (R_TESTNAME) and then write a zero to the pass/fail register (R_PASSFAIL) if
28 * the test is passed or any non-zero value to it if the test is failed.
32 #include "hw/sysbus.h"
35 #include "qemu/error-report.h"
36 #include "sysemu/sysemu.h"
45 #define MAX_TESTNAME_LEN 16
47 #define TYPE_LM32_SYS "lm32-sys"
48 #define LM32_SYS(obj) OBJECT_CHECK(LM32SysState, (obj), TYPE_LM32_SYS)
51 SysBusDevice parent_obj
;
56 uint8_t testname
[MAX_TESTNAME_LEN
];
58 typedef struct LM32SysState LM32SysState
;
60 static void copy_testname(LM32SysState
*s
)
62 cpu_physical_memory_read(s
->regs
[R_TESTNAME
], s
->testname
,
64 s
->testname
[MAX_TESTNAME_LEN
- 1] = '\0';
67 static void sys_write(void *opaque
, hwaddr addr
,
68 uint64_t value
, unsigned size
)
70 LM32SysState
*s
= opaque
;
73 trace_lm32_sys_memory_write(addr
, value
);
78 qemu_system_shutdown_request();
81 s
->regs
[addr
] = value
;
82 testname
= (char *)s
->testname
;
83 qemu_log("TC %-16s %s\n", testname
, (value
) ? "FAILED" : "OK");
86 s
->regs
[addr
] = value
;
91 error_report("lm32_sys: write access to unknown register 0x"
92 TARGET_FMT_plx
, addr
<< 2);
97 static bool sys_ops_accepts(void *opaque
, hwaddr addr
,
98 unsigned size
, bool is_write
)
100 return is_write
&& size
== 4;
103 static const MemoryRegionOps sys_ops
= {
105 .valid
.accepts
= sys_ops_accepts
,
106 .endianness
= DEVICE_NATIVE_ENDIAN
,
109 static void sys_reset(DeviceState
*d
)
111 LM32SysState
*s
= LM32_SYS(d
);
114 for (i
= 0; i
< R_MAX
; i
++) {
117 memset(s
->testname
, 0, MAX_TESTNAME_LEN
);
120 static int lm32_sys_init(SysBusDevice
*dev
)
122 LM32SysState
*s
= LM32_SYS(dev
);
124 memory_region_init_io(&s
->iomem
, OBJECT(dev
), &sys_ops
, s
,
126 sysbus_init_mmio(dev
, &s
->iomem
);
128 /* Note: This device is not created in the board initialization,
129 * instead it has to be added with the -device parameter. Therefore,
130 * the device maps itself. */
131 sysbus_mmio_map(dev
, 0, s
->base
);
136 static const VMStateDescription vmstate_lm32_sys
= {
139 .minimum_version_id
= 1,
140 .minimum_version_id_old
= 1,
141 .fields
= (VMStateField
[]) {
142 VMSTATE_UINT32_ARRAY(regs
, LM32SysState
, R_MAX
),
143 VMSTATE_BUFFER(testname
, LM32SysState
),
144 VMSTATE_END_OF_LIST()
148 static Property lm32_sys_properties
[] = {
149 DEFINE_PROP_UINT32("base", LM32SysState
, base
, 0xffff0000),
150 DEFINE_PROP_END_OF_LIST(),
153 static void lm32_sys_class_init(ObjectClass
*klass
, void *data
)
155 DeviceClass
*dc
= DEVICE_CLASS(klass
);
156 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
158 k
->init
= lm32_sys_init
;
159 dc
->reset
= sys_reset
;
160 dc
->vmsd
= &vmstate_lm32_sys
;
161 dc
->props
= lm32_sys_properties
;
164 static const TypeInfo lm32_sys_info
= {
165 .name
= TYPE_LM32_SYS
,
166 .parent
= TYPE_SYS_BUS_DEVICE
,
167 .instance_size
= sizeof(LM32SysState
),
168 .class_init
= lm32_sys_class_init
,
171 static void lm32_sys_register_types(void)
173 type_register_static(&lm32_sys_info
);
176 type_init(lm32_sys_register_types
)