Statistics
| Branch: | Revision:

root / hw / lm32_sys.c @ 9c17d615

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