Statistics
| Branch: | Revision:

root / hw / lm32_sys.c @ ad3376cc

History | View | Annotate | Download (4.2 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 f19410ca Michael Walle
#include "qemu-log.h"
35 f19410ca Michael Walle
#include "qemu-error.h"
36 f19410ca Michael Walle
#include "sysemu.h"
37 f19410ca Michael Walle
#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 f19410ca Michael Walle
    uint32_t base;
51 f19410ca Michael Walle
    uint32_t regs[R_MAX];
52 f19410ca Michael Walle
    uint8_t testname[MAX_TESTNAME_LEN];
53 f19410ca Michael Walle
};
54 f19410ca Michael Walle
typedef struct LM32SysState LM32SysState;
55 f19410ca Michael Walle
56 f19410ca Michael Walle
static void copy_testname(LM32SysState *s)
57 f19410ca Michael Walle
{
58 f19410ca Michael Walle
    cpu_physical_memory_read(s->regs[R_TESTNAME], s->testname,
59 f19410ca Michael Walle
            MAX_TESTNAME_LEN);
60 f19410ca Michael Walle
    s->testname[MAX_TESTNAME_LEN - 1] = '\0';
61 f19410ca Michael Walle
}
62 f19410ca Michael Walle
63 f19410ca Michael Walle
static void sys_write(void *opaque, target_phys_addr_t addr, uint32_t value)
64 f19410ca Michael Walle
{
65 f19410ca Michael Walle
    LM32SysState *s = opaque;
66 f19410ca Michael Walle
    char *testname;
67 f19410ca Michael Walle
68 f19410ca Michael Walle
    trace_lm32_sys_memory_write(addr, value);
69 f19410ca Michael Walle
70 f19410ca Michael Walle
    addr >>= 2;
71 f19410ca Michael Walle
    switch (addr) {
72 f19410ca Michael Walle
    case R_CTRL:
73 f19410ca Michael Walle
        qemu_system_shutdown_request();
74 f19410ca Michael Walle
        break;
75 f19410ca Michael Walle
    case R_PASSFAIL:
76 f19410ca Michael Walle
        s->regs[addr] = value;
77 f19410ca Michael Walle
        testname = (char *)s->testname;
78 f19410ca Michael Walle
        qemu_log("TC  %-16s %s\n", testname, (value) ? "FAILED" : "OK");
79 f19410ca Michael Walle
        break;
80 f19410ca Michael Walle
    case R_TESTNAME:
81 f19410ca Michael Walle
        s->regs[addr] = value;
82 f19410ca Michael Walle
        copy_testname(s);
83 f19410ca Michael Walle
        break;
84 f19410ca Michael Walle
85 f19410ca Michael Walle
    default:
86 f19410ca Michael Walle
        error_report("lm32_sys: write access to unkown register 0x"
87 f19410ca Michael Walle
                TARGET_FMT_plx, addr << 2);
88 f19410ca Michael Walle
        break;
89 f19410ca Michael Walle
    }
90 f19410ca Michael Walle
}
91 f19410ca Michael Walle
92 f19410ca Michael Walle
static CPUReadMemoryFunc * const sys_read_fn[] = {
93 f19410ca Michael Walle
    NULL,
94 f19410ca Michael Walle
    NULL,
95 f19410ca Michael Walle
    NULL,
96 f19410ca Michael Walle
};
97 f19410ca Michael Walle
98 f19410ca Michael Walle
static CPUWriteMemoryFunc * const sys_write_fn[] = {
99 f19410ca Michael Walle
    NULL,
100 f19410ca Michael Walle
    NULL,
101 f19410ca Michael Walle
    &sys_write,
102 f19410ca Michael Walle
};
103 f19410ca Michael Walle
104 f19410ca Michael Walle
static void sys_reset(DeviceState *d)
105 f19410ca Michael Walle
{
106 f19410ca Michael Walle
    LM32SysState *s = container_of(d, LM32SysState, busdev.qdev);
107 f19410ca Michael Walle
    int i;
108 f19410ca Michael Walle
109 f19410ca Michael Walle
    for (i = 0; i < R_MAX; i++) {
110 f19410ca Michael Walle
        s->regs[i] = 0;
111 f19410ca Michael Walle
    }
112 f19410ca Michael Walle
    memset(s->testname, 0, MAX_TESTNAME_LEN);
113 f19410ca Michael Walle
}
114 f19410ca Michael Walle
115 f19410ca Michael Walle
static int lm32_sys_init(SysBusDevice *dev)
116 f19410ca Michael Walle
{
117 f19410ca Michael Walle
    LM32SysState *s = FROM_SYSBUS(typeof(*s), dev);
118 f19410ca Michael Walle
    int sys_regs;
119 f19410ca Michael Walle
120 f19410ca Michael Walle
    sys_regs = cpu_register_io_memory(sys_read_fn, sys_write_fn, s,
121 f19410ca Michael Walle
            DEVICE_NATIVE_ENDIAN);
122 f19410ca Michael Walle
    sysbus_init_mmio(dev, R_MAX * 4, sys_regs);
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 f19410ca Michael Walle
static SysBusDeviceInfo lm32_sys_info = {
145 f19410ca Michael Walle
    .init = lm32_sys_init,
146 f19410ca Michael Walle
    .qdev.name  = "lm32-sys",
147 f19410ca Michael Walle
    .qdev.size  = sizeof(LM32SysState),
148 f19410ca Michael Walle
    .qdev.vmsd  = &vmstate_lm32_sys,
149 f19410ca Michael Walle
    .qdev.reset = sys_reset,
150 f19410ca Michael Walle
    .qdev.props = (Property[]) {
151 f19410ca Michael Walle
        DEFINE_PROP_UINT32("base", LM32SysState, base, 0xffff0000),
152 f19410ca Michael Walle
        DEFINE_PROP_END_OF_LIST(),
153 f19410ca Michael Walle
    }
154 f19410ca Michael Walle
};
155 f19410ca Michael Walle
156 f19410ca Michael Walle
static void lm32_sys_register(void)
157 f19410ca Michael Walle
{
158 f19410ca Michael Walle
    sysbus_register_withprop(&lm32_sys_info);
159 f19410ca Michael Walle
}
160 f19410ca Michael Walle
161 f19410ca Michael Walle
device_init(lm32_sys_register)