]> git.proxmox.com Git - qemu.git/blame - hw/misc/lm32_sys.c
Merge git://github.com/hw-claudio/qemu-aarch64-queue into tcg-next
[qemu.git] / hw / misc / lm32_sys.c
CommitLineData
f19410ca
MW
1/*
2 * QEMU model of the LatticeMico32 system control block.
3 *
4 * Copyright (c) 2010 Michael Walle <michael@walle.cc>
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20/*
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.
24 *
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.
29 */
30
83c9f4ca
PB
31#include "hw/hw.h"
32#include "hw/sysbus.h"
f19410ca 33#include "trace.h"
1de7afc9
PB
34#include "qemu/log.h"
35#include "qemu/error-report.h"
9c17d615 36#include "sysemu/sysemu.h"
f19410ca
MW
37
38enum {
39 R_CTRL = 0,
40 R_PASSFAIL,
41 R_TESTNAME,
42 R_MAX
43};
44
45#define MAX_TESTNAME_LEN 16
46
47struct LM32SysState {
48 SysBusDevice busdev;
0aa27efa 49 MemoryRegion iomem;
f19410ca
MW
50 uint32_t base;
51 uint32_t regs[R_MAX];
52 uint8_t testname[MAX_TESTNAME_LEN];
53};
54typedef struct LM32SysState LM32SysState;
55
56static void copy_testname(LM32SysState *s)
57{
58 cpu_physical_memory_read(s->regs[R_TESTNAME], s->testname,
59 MAX_TESTNAME_LEN);
60 s->testname[MAX_TESTNAME_LEN - 1] = '\0';
61}
62
a8170e5e 63static void sys_write(void *opaque, hwaddr addr,
0aa27efa 64 uint64_t value, unsigned size)
f19410ca
MW
65{
66 LM32SysState *s = opaque;
67 char *testname;
68
69 trace_lm32_sys_memory_write(addr, value);
70
71 addr >>= 2;
72 switch (addr) {
73 case R_CTRL:
74 qemu_system_shutdown_request();
75 break;
76 case R_PASSFAIL:
77 s->regs[addr] = value;
78 testname = (char *)s->testname;
79 qemu_log("TC %-16s %s\n", testname, (value) ? "FAILED" : "OK");
80 break;
81 case R_TESTNAME:
82 s->regs[addr] = value;
83 copy_testname(s);
84 break;
85
86 default:
dd3d6775 87 error_report("lm32_sys: write access to unknown register 0x"
f19410ca
MW
88 TARGET_FMT_plx, addr << 2);
89 break;
90 }
91}
92
a8170e5e 93static bool sys_ops_accepts(void *opaque, hwaddr addr,
0aa27efa
BC
94 unsigned size, bool is_write)
95{
96 return is_write && size == 4;
97}
f19410ca 98
0aa27efa
BC
99static const MemoryRegionOps sys_ops = {
100 .write = sys_write,
101 .valid.accepts = sys_ops_accepts,
102 .endianness = DEVICE_NATIVE_ENDIAN,
f19410ca
MW
103};
104
105static void sys_reset(DeviceState *d)
106{
107 LM32SysState *s = container_of(d, LM32SysState, busdev.qdev);
108 int i;
109
110 for (i = 0; i < R_MAX; i++) {
111 s->regs[i] = 0;
112 }
113 memset(s->testname, 0, MAX_TESTNAME_LEN);
114}
115
116static int lm32_sys_init(SysBusDevice *dev)
117{
118 LM32SysState *s = FROM_SYSBUS(typeof(*s), dev);
f19410ca 119
3c161542
PB
120 memory_region_init_io(&s->iomem, OBJECT(dev), &sys_ops , s,
121 "sys", R_MAX * 4);
750ecd44 122 sysbus_init_mmio(dev, &s->iomem);
f19410ca
MW
123
124 /* Note: This device is not created in the board initialization,
125 * instead it has to be added with the -device parameter. Therefore,
126 * the device maps itself. */
127 sysbus_mmio_map(dev, 0, s->base);
128
129 return 0;
130}
131
132static const VMStateDescription vmstate_lm32_sys = {
133 .name = "lm32-sys",
134 .version_id = 1,
135 .minimum_version_id = 1,
136 .minimum_version_id_old = 1,
137 .fields = (VMStateField[]) {
138 VMSTATE_UINT32_ARRAY(regs, LM32SysState, R_MAX),
139 VMSTATE_BUFFER(testname, LM32SysState),
140 VMSTATE_END_OF_LIST()
141 }
142};
143
999e12bb
AL
144static Property lm32_sys_properties[] = {
145 DEFINE_PROP_UINT32("base", LM32SysState, base, 0xffff0000),
146 DEFINE_PROP_END_OF_LIST(),
147};
148
149static void lm32_sys_class_init(ObjectClass *klass, void *data)
150{
39bffca2 151 DeviceClass *dc = DEVICE_CLASS(klass);
999e12bb
AL
152 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
153
154 k->init = lm32_sys_init;
39bffca2
AL
155 dc->reset = sys_reset;
156 dc->vmsd = &vmstate_lm32_sys;
157 dc->props = lm32_sys_properties;
999e12bb
AL
158}
159
8c43a6f0 160static const TypeInfo lm32_sys_info = {
39bffca2
AL
161 .name = "lm32-sys",
162 .parent = TYPE_SYS_BUS_DEVICE,
163 .instance_size = sizeof(LM32SysState),
164 .class_init = lm32_sys_class_init,
f19410ca
MW
165};
166
83f7d43a 167static void lm32_sys_register_types(void)
f19410ca 168{
39bffca2 169 type_register_static(&lm32_sys_info);
f19410ca
MW
170}
171
83f7d43a 172type_init(lm32_sys_register_types)