Statistics
| Branch: | Revision:

root / hw / misc / lm32_sys.c @ ddf5636d

History | View | Annotate | Download (4.9 kB)

1 f19410ca Michael Walle
/*
2 f19410ca Michael Walle
 *  QEMU model of the LatticeMico32 system control block.
3 f19410ca Michael Walle
 *
4 f19410ca Michael Walle
 *  Copyright (c) 2010 Michael Walle <michael@walle.cc>
5 f19410ca Michael Walle
 *
6 f19410ca Michael Walle
 * This library is free software; you can redistribute it and/or
7 f19410ca Michael Walle
 * modify it under the terms of the GNU Lesser General Public
8 f19410ca Michael Walle
 * License as published by the Free Software Foundation; either
9 f19410ca Michael Walle
 * version 2 of the License, or (at your option) any later version.
10 f19410ca Michael Walle
 *
11 f19410ca Michael Walle
 * This library is distributed in the hope that it will be useful,
12 f19410ca Michael Walle
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 f19410ca Michael Walle
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 f19410ca Michael Walle
 * Lesser General Public License for more details.
15 f19410ca Michael Walle
 *
16 f19410ca Michael Walle
 * You should have received a copy of the GNU Lesser General Public
17 f19410ca Michael Walle
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 f19410ca Michael Walle
 */
19 f19410ca Michael Walle
20 f19410ca Michael Walle
/*
21 f19410ca Michael Walle
 * This model is mainly intended for testing purposes and doesn't fit to any
22 f19410ca Michael Walle
 * real hardware. On the one hand it provides a control register (R_CTRL) on
23 f19410ca Michael Walle
 * the other hand it supports the lm32 tests.
24 f19410ca Michael Walle
 *
25 f19410ca Michael Walle
 * A write to the control register causes a system shutdown.
26 f19410ca Michael Walle
 * Tests first write the pointer to a test name to the test name register
27 f19410ca Michael Walle
 * (R_TESTNAME) and then write a zero to the pass/fail register (R_PASSFAIL) if
28 f19410ca Michael Walle
 * the test is passed or any non-zero value to it if the test is failed.
29 f19410ca Michael Walle
 */
30 f19410ca Michael Walle
31 83c9f4ca Paolo Bonzini
#include "hw/hw.h"
32 83c9f4ca Paolo Bonzini
#include "hw/sysbus.h"
33 f19410ca Michael Walle
#include "trace.h"
34 1de7afc9 Paolo Bonzini
#include "qemu/log.h"
35 1de7afc9 Paolo Bonzini
#include "qemu/error-report.h"
36 9c17d615 Paolo Bonzini
#include "sysemu/sysemu.h"
37 f19410ca Michael Walle
38 f19410ca Michael Walle
enum {
39 f19410ca Michael Walle
    R_CTRL = 0,
40 f19410ca Michael Walle
    R_PASSFAIL,
41 f19410ca Michael Walle
    R_TESTNAME,
42 f19410ca Michael Walle
    R_MAX
43 f19410ca Michael Walle
};
44 f19410ca Michael Walle
45 e67b3ca5 Michael Walle
#define MAX_TESTNAME_LEN 32
46 f19410ca Michael Walle
47 816d323b Andreas Färber
#define TYPE_LM32_SYS "lm32-sys"
48 816d323b Andreas Färber
#define LM32_SYS(obj) OBJECT_CHECK(LM32SysState, (obj), TYPE_LM32_SYS)
49 816d323b Andreas Färber
50 f19410ca Michael Walle
struct LM32SysState {
51 816d323b Andreas Färber
    SysBusDevice parent_obj;
52 816d323b Andreas Färber
53 0aa27efa Benoît Canet
    MemoryRegion iomem;
54 f19410ca Michael Walle
    uint32_t base;
55 f19410ca Michael Walle
    uint32_t regs[R_MAX];
56 f19410ca Michael Walle
    uint8_t testname[MAX_TESTNAME_LEN];
57 f19410ca Michael Walle
};
58 f19410ca Michael Walle
typedef struct LM32SysState LM32SysState;
59 f19410ca Michael Walle
60 f19410ca Michael Walle
static void copy_testname(LM32SysState *s)
61 f19410ca Michael Walle
{
62 f19410ca Michael Walle
    cpu_physical_memory_read(s->regs[R_TESTNAME], s->testname,
63 f19410ca Michael Walle
            MAX_TESTNAME_LEN);
64 f19410ca Michael Walle
    s->testname[MAX_TESTNAME_LEN - 1] = '\0';
65 f19410ca Michael Walle
}
66 f19410ca Michael Walle
67 a8170e5e Avi Kivity
static void sys_write(void *opaque, hwaddr addr,
68 0aa27efa Benoît Canet
                      uint64_t value, unsigned size)
69 f19410ca Michael Walle
{
70 f19410ca Michael Walle
    LM32SysState *s = opaque;
71 f19410ca Michael Walle
    char *testname;
72 f19410ca Michael Walle
73 f19410ca Michael Walle
    trace_lm32_sys_memory_write(addr, value);
74 f19410ca Michael Walle
75 f19410ca Michael Walle
    addr >>= 2;
76 f19410ca Michael Walle
    switch (addr) {
77 f19410ca Michael Walle
    case R_CTRL:
78 f19410ca Michael Walle
        qemu_system_shutdown_request();
79 f19410ca Michael Walle
        break;
80 f19410ca Michael Walle
    case R_PASSFAIL:
81 f19410ca Michael Walle
        s->regs[addr] = value;
82 f19410ca Michael Walle
        testname = (char *)s->testname;
83 9a59e6e3 Michael Walle
        fprintf(stderr, "TC  %-*s %s\n", MAX_TESTNAME_LEN,
84 e67b3ca5 Michael Walle
                testname, (value) ? "FAILED" : "OK");
85 8c5edce5 Michael Walle
        if (value) {
86 8c5edce5 Michael Walle
            cpu_dump_state(qemu_get_cpu(0), stderr, fprintf, 0);
87 8c5edce5 Michael Walle
        }
88 f19410ca Michael Walle
        break;
89 f19410ca Michael Walle
    case R_TESTNAME:
90 f19410ca Michael Walle
        s->regs[addr] = value;
91 f19410ca Michael Walle
        copy_testname(s);
92 f19410ca Michael Walle
        break;
93 f19410ca Michael Walle
94 f19410ca Michael Walle
    default:
95 dd3d6775 Markus Armbruster
        error_report("lm32_sys: write access to unknown register 0x"
96 f19410ca Michael Walle
                TARGET_FMT_plx, addr << 2);
97 f19410ca Michael Walle
        break;
98 f19410ca Michael Walle
    }
99 f19410ca Michael Walle
}
100 f19410ca Michael Walle
101 a8170e5e Avi Kivity
static bool sys_ops_accepts(void *opaque, hwaddr addr,
102 0aa27efa Benoît Canet
                            unsigned size, bool is_write)
103 0aa27efa Benoît Canet
{
104 0aa27efa Benoît Canet
    return is_write && size == 4;
105 0aa27efa Benoît Canet
}
106 f19410ca Michael Walle
107 0aa27efa Benoît Canet
static const MemoryRegionOps sys_ops = {
108 0aa27efa Benoît Canet
    .write = sys_write,
109 0aa27efa Benoît Canet
    .valid.accepts = sys_ops_accepts,
110 0aa27efa Benoît Canet
    .endianness = DEVICE_NATIVE_ENDIAN,
111 f19410ca Michael Walle
};
112 f19410ca Michael Walle
113 f19410ca Michael Walle
static void sys_reset(DeviceState *d)
114 f19410ca Michael Walle
{
115 816d323b Andreas Färber
    LM32SysState *s = LM32_SYS(d);
116 f19410ca Michael Walle
    int i;
117 f19410ca Michael Walle
118 f19410ca Michael Walle
    for (i = 0; i < R_MAX; i++) {
119 f19410ca Michael Walle
        s->regs[i] = 0;
120 f19410ca Michael Walle
    }
121 f19410ca Michael Walle
    memset(s->testname, 0, MAX_TESTNAME_LEN);
122 f19410ca Michael Walle
}
123 f19410ca Michael Walle
124 f19410ca Michael Walle
static int lm32_sys_init(SysBusDevice *dev)
125 f19410ca Michael Walle
{
126 816d323b Andreas Färber
    LM32SysState *s = LM32_SYS(dev);
127 f19410ca Michael Walle
128 3c161542 Paolo Bonzini
    memory_region_init_io(&s->iomem, OBJECT(dev), &sys_ops , s,
129 3c161542 Paolo Bonzini
                          "sys", R_MAX * 4);
130 750ecd44 Avi Kivity
    sysbus_init_mmio(dev, &s->iomem);
131 f19410ca Michael Walle
132 f19410ca Michael Walle
    /* Note: This device is not created in the board initialization,
133 f19410ca Michael Walle
     * instead it has to be added with the -device parameter. Therefore,
134 f19410ca Michael Walle
     * the device maps itself. */
135 f19410ca Michael Walle
    sysbus_mmio_map(dev, 0, s->base);
136 f19410ca Michael Walle
137 f19410ca Michael Walle
    return 0;
138 f19410ca Michael Walle
}
139 f19410ca Michael Walle
140 f19410ca Michael Walle
static const VMStateDescription vmstate_lm32_sys = {
141 f19410ca Michael Walle
    .name = "lm32-sys",
142 f19410ca Michael Walle
    .version_id = 1,
143 f19410ca Michael Walle
    .minimum_version_id = 1,
144 f19410ca Michael Walle
    .minimum_version_id_old = 1,
145 f19410ca Michael Walle
    .fields      = (VMStateField[]) {
146 f19410ca Michael Walle
        VMSTATE_UINT32_ARRAY(regs, LM32SysState, R_MAX),
147 f19410ca Michael Walle
        VMSTATE_BUFFER(testname, LM32SysState),
148 f19410ca Michael Walle
        VMSTATE_END_OF_LIST()
149 f19410ca Michael Walle
    }
150 f19410ca Michael Walle
};
151 f19410ca Michael Walle
152 999e12bb Anthony Liguori
static Property lm32_sys_properties[] = {
153 999e12bb Anthony Liguori
    DEFINE_PROP_UINT32("base", LM32SysState, base, 0xffff0000),
154 999e12bb Anthony Liguori
    DEFINE_PROP_END_OF_LIST(),
155 999e12bb Anthony Liguori
};
156 999e12bb Anthony Liguori
157 999e12bb Anthony Liguori
static void lm32_sys_class_init(ObjectClass *klass, void *data)
158 999e12bb Anthony Liguori
{
159 39bffca2 Anthony Liguori
    DeviceClass *dc = DEVICE_CLASS(klass);
160 999e12bb Anthony Liguori
    SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
161 999e12bb Anthony Liguori
162 999e12bb Anthony Liguori
    k->init = lm32_sys_init;
163 39bffca2 Anthony Liguori
    dc->reset = sys_reset;
164 39bffca2 Anthony Liguori
    dc->vmsd = &vmstate_lm32_sys;
165 39bffca2 Anthony Liguori
    dc->props = lm32_sys_properties;
166 999e12bb Anthony Liguori
}
167 999e12bb Anthony Liguori
168 8c43a6f0 Andreas Färber
static const TypeInfo lm32_sys_info = {
169 816d323b Andreas Färber
    .name          = TYPE_LM32_SYS,
170 39bffca2 Anthony Liguori
    .parent        = TYPE_SYS_BUS_DEVICE,
171 39bffca2 Anthony Liguori
    .instance_size = sizeof(LM32SysState),
172 39bffca2 Anthony Liguori
    .class_init    = lm32_sys_class_init,
173 f19410ca Michael Walle
};
174 f19410ca Michael Walle
175 83f7d43a Andreas Färber
static void lm32_sys_register_types(void)
176 f19410ca Michael Walle
{
177 39bffca2 Anthony Liguori
    type_register_static(&lm32_sys_info);
178 f19410ca Michael Walle
}
179 f19410ca Michael Walle
180 83f7d43a Andreas Färber
type_init(lm32_sys_register_types)