Statistics
| Branch: | Revision:

root / hw / lm32_timer.c @ da726e5e

History | View | Annotate | Download (5.2 kB)

1 ea7924dc Michael Walle
/*
2 ea7924dc Michael Walle
 *  QEMU model of the LatticeMico32 timer block.
3 ea7924dc Michael Walle
 *
4 ea7924dc Michael Walle
 *  Copyright (c) 2010 Michael Walle <michael@walle.cc>
5 ea7924dc Michael Walle
 *
6 ea7924dc Michael Walle
 * This library is free software; you can redistribute it and/or
7 ea7924dc Michael Walle
 * modify it under the terms of the GNU Lesser General Public
8 ea7924dc Michael Walle
 * License as published by the Free Software Foundation; either
9 ea7924dc Michael Walle
 * version 2 of the License, or (at your option) any later version.
10 ea7924dc Michael Walle
 *
11 ea7924dc Michael Walle
 * This library is distributed in the hope that it will be useful,
12 ea7924dc Michael Walle
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ea7924dc Michael Walle
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 ea7924dc Michael Walle
 * Lesser General Public License for more details.
15 ea7924dc Michael Walle
 *
16 ea7924dc Michael Walle
 * You should have received a copy of the GNU Lesser General Public
17 ea7924dc Michael Walle
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 ea7924dc Michael Walle
 *
19 ea7924dc Michael Walle
 *
20 ea7924dc Michael Walle
 * Specification available at:
21 ea7924dc Michael Walle
 *   http://www.latticesemi.com/documents/mico32timer.pdf
22 ea7924dc Michael Walle
 */
23 ea7924dc Michael Walle
24 ea7924dc Michael Walle
#include "hw.h"
25 ea7924dc Michael Walle
#include "sysbus.h"
26 ea7924dc Michael Walle
#include "trace.h"
27 ea7924dc Michael Walle
#include "qemu-timer.h"
28 ea7924dc Michael Walle
#include "qemu-error.h"
29 ea7924dc Michael Walle
30 ea7924dc Michael Walle
#define DEFAULT_FREQUENCY (50*1000000)
31 ea7924dc Michael Walle
32 ea7924dc Michael Walle
enum {
33 ea7924dc Michael Walle
    R_SR = 0,
34 ea7924dc Michael Walle
    R_CR,
35 ea7924dc Michael Walle
    R_PERIOD,
36 ea7924dc Michael Walle
    R_SNAPSHOT,
37 ea7924dc Michael Walle
    R_MAX
38 ea7924dc Michael Walle
};
39 ea7924dc Michael Walle
40 ea7924dc Michael Walle
enum {
41 ea7924dc Michael Walle
    SR_TO    = (1 << 0),
42 ea7924dc Michael Walle
    SR_RUN   = (1 << 1),
43 ea7924dc Michael Walle
};
44 ea7924dc Michael Walle
45 ea7924dc Michael Walle
enum {
46 ea7924dc Michael Walle
    CR_ITO   = (1 << 0),
47 ea7924dc Michael Walle
    CR_CONT  = (1 << 1),
48 ea7924dc Michael Walle
    CR_START = (1 << 2),
49 ea7924dc Michael Walle
    CR_STOP  = (1 << 3),
50 ea7924dc Michael Walle
};
51 ea7924dc Michael Walle
52 ea7924dc Michael Walle
struct LM32TimerState {
53 ea7924dc Michael Walle
    SysBusDevice busdev;
54 d09510b2 Avi Kivity
    MemoryRegion iomem;
55 ea7924dc Michael Walle
56 ea7924dc Michael Walle
    QEMUBH *bh;
57 ea7924dc Michael Walle
    ptimer_state *ptimer;
58 ea7924dc Michael Walle
59 ea7924dc Michael Walle
    qemu_irq irq;
60 ea7924dc Michael Walle
    uint32_t freq_hz;
61 ea7924dc Michael Walle
62 ea7924dc Michael Walle
    uint32_t regs[R_MAX];
63 ea7924dc Michael Walle
};
64 ea7924dc Michael Walle
typedef struct LM32TimerState LM32TimerState;
65 ea7924dc Michael Walle
66 ea7924dc Michael Walle
static void timer_update_irq(LM32TimerState *s)
67 ea7924dc Michael Walle
{
68 ea7924dc Michael Walle
    int state = (s->regs[R_SR] & SR_TO) && (s->regs[R_CR] & CR_ITO);
69 ea7924dc Michael Walle
70 ea7924dc Michael Walle
    trace_lm32_timer_irq_state(state);
71 ea7924dc Michael Walle
    qemu_set_irq(s->irq, state);
72 ea7924dc Michael Walle
}
73 ea7924dc Michael Walle
74 d09510b2 Avi Kivity
static uint64_t timer_read(void *opaque, target_phys_addr_t addr, unsigned size)
75 ea7924dc Michael Walle
{
76 ea7924dc Michael Walle
    LM32TimerState *s = opaque;
77 ea7924dc Michael Walle
    uint32_t r = 0;
78 ea7924dc Michael Walle
79 ea7924dc Michael Walle
    addr >>= 2;
80 ea7924dc Michael Walle
    switch (addr) {
81 ea7924dc Michael Walle
    case R_SR:
82 ea7924dc Michael Walle
    case R_CR:
83 ea7924dc Michael Walle
    case R_PERIOD:
84 ea7924dc Michael Walle
        r = s->regs[addr];
85 ea7924dc Michael Walle
        break;
86 ea7924dc Michael Walle
    case R_SNAPSHOT:
87 ea7924dc Michael Walle
        r = (uint32_t)ptimer_get_count(s->ptimer);
88 ea7924dc Michael Walle
        break;
89 ea7924dc Michael Walle
    default:
90 dd3d6775 Markus Armbruster
        error_report("lm32_timer: read access to unknown register 0x"
91 ea7924dc Michael Walle
                TARGET_FMT_plx, addr << 2);
92 ea7924dc Michael Walle
        break;
93 ea7924dc Michael Walle
    }
94 ea7924dc Michael Walle
95 ea7924dc Michael Walle
    trace_lm32_timer_memory_read(addr << 2, r);
96 ea7924dc Michael Walle
    return r;
97 ea7924dc Michael Walle
}
98 ea7924dc Michael Walle
99 d09510b2 Avi Kivity
static void timer_write(void *opaque, target_phys_addr_t addr,
100 d09510b2 Avi Kivity
                        uint64_t value, unsigned size)
101 ea7924dc Michael Walle
{
102 ea7924dc Michael Walle
    LM32TimerState *s = opaque;
103 ea7924dc Michael Walle
104 ea7924dc Michael Walle
    trace_lm32_timer_memory_write(addr, value);
105 ea7924dc Michael Walle
106 ea7924dc Michael Walle
    addr >>= 2;
107 ea7924dc Michael Walle
    switch (addr) {
108 ea7924dc Michael Walle
    case R_SR:
109 ea7924dc Michael Walle
        s->regs[R_SR] &= ~SR_TO;
110 ea7924dc Michael Walle
        break;
111 ea7924dc Michael Walle
    case R_CR:
112 ea7924dc Michael Walle
        s->regs[R_CR] = value;
113 ea7924dc Michael Walle
        if (s->regs[R_CR] & CR_START) {
114 ea7924dc Michael Walle
            ptimer_run(s->ptimer, 1);
115 ea7924dc Michael Walle
        }
116 ea7924dc Michael Walle
        if (s->regs[R_CR] & CR_STOP) {
117 ea7924dc Michael Walle
            ptimer_stop(s->ptimer);
118 ea7924dc Michael Walle
        }
119 ea7924dc Michael Walle
        break;
120 ea7924dc Michael Walle
    case R_PERIOD:
121 ea7924dc Michael Walle
        s->regs[R_PERIOD] = value;
122 ea7924dc Michael Walle
        ptimer_set_count(s->ptimer, value);
123 ea7924dc Michael Walle
        break;
124 ea7924dc Michael Walle
    case R_SNAPSHOT:
125 ea7924dc Michael Walle
        error_report("lm32_timer: write access to read only register 0x"
126 ea7924dc Michael Walle
                TARGET_FMT_plx, addr << 2);
127 ea7924dc Michael Walle
        break;
128 ea7924dc Michael Walle
    default:
129 dd3d6775 Markus Armbruster
        error_report("lm32_timer: write access to unknown register 0x"
130 ea7924dc Michael Walle
                TARGET_FMT_plx, addr << 2);
131 ea7924dc Michael Walle
        break;
132 ea7924dc Michael Walle
    }
133 ea7924dc Michael Walle
    timer_update_irq(s);
134 ea7924dc Michael Walle
}
135 ea7924dc Michael Walle
136 d09510b2 Avi Kivity
static const MemoryRegionOps timer_ops = {
137 d09510b2 Avi Kivity
    .read = timer_read,
138 d09510b2 Avi Kivity
    .write = timer_write,
139 d09510b2 Avi Kivity
    .endianness = DEVICE_NATIVE_ENDIAN,
140 d09510b2 Avi Kivity
    .valid = {
141 d09510b2 Avi Kivity
        .min_access_size = 4,
142 d09510b2 Avi Kivity
        .max_access_size = 4,
143 d09510b2 Avi Kivity
    },
144 ea7924dc Michael Walle
};
145 ea7924dc Michael Walle
146 ea7924dc Michael Walle
static void timer_hit(void *opaque)
147 ea7924dc Michael Walle
{
148 ea7924dc Michael Walle
    LM32TimerState *s = opaque;
149 ea7924dc Michael Walle
150 ea7924dc Michael Walle
    trace_lm32_timer_hit();
151 ea7924dc Michael Walle
152 ea7924dc Michael Walle
    s->regs[R_SR] |= SR_TO;
153 ea7924dc Michael Walle
154 ea7924dc Michael Walle
    if (s->regs[R_CR] & CR_CONT) {
155 ea7924dc Michael Walle
        ptimer_set_count(s->ptimer, s->regs[R_PERIOD]);
156 ea7924dc Michael Walle
        ptimer_run(s->ptimer, 1);
157 ea7924dc Michael Walle
    }
158 ea7924dc Michael Walle
    timer_update_irq(s);
159 ea7924dc Michael Walle
}
160 ea7924dc Michael Walle
161 ea7924dc Michael Walle
static void timer_reset(DeviceState *d)
162 ea7924dc Michael Walle
{
163 ea7924dc Michael Walle
    LM32TimerState *s = container_of(d, LM32TimerState, busdev.qdev);
164 ea7924dc Michael Walle
    int i;
165 ea7924dc Michael Walle
166 ea7924dc Michael Walle
    for (i = 0; i < R_MAX; i++) {
167 ea7924dc Michael Walle
        s->regs[i] = 0;
168 ea7924dc Michael Walle
    }
169 ea7924dc Michael Walle
    ptimer_stop(s->ptimer);
170 ea7924dc Michael Walle
}
171 ea7924dc Michael Walle
172 ea7924dc Michael Walle
static int lm32_timer_init(SysBusDevice *dev)
173 ea7924dc Michael Walle
{
174 ea7924dc Michael Walle
    LM32TimerState *s = FROM_SYSBUS(typeof(*s), dev);
175 ea7924dc Michael Walle
176 ea7924dc Michael Walle
    sysbus_init_irq(dev, &s->irq);
177 ea7924dc Michael Walle
178 ea7924dc Michael Walle
    s->bh = qemu_bh_new(timer_hit, s);
179 ea7924dc Michael Walle
    s->ptimer = ptimer_init(s->bh);
180 ea7924dc Michael Walle
    ptimer_set_freq(s->ptimer, s->freq_hz);
181 ea7924dc Michael Walle
182 d09510b2 Avi Kivity
    memory_region_init_io(&s->iomem, &timer_ops, s, "timer", R_MAX * 4);
183 d09510b2 Avi Kivity
    sysbus_init_mmio_region(dev, &s->iomem);
184 ea7924dc Michael Walle
185 ea7924dc Michael Walle
    return 0;
186 ea7924dc Michael Walle
}
187 ea7924dc Michael Walle
188 ea7924dc Michael Walle
static const VMStateDescription vmstate_lm32_timer = {
189 ea7924dc Michael Walle
    .name = "lm32-timer",
190 ea7924dc Michael Walle
    .version_id = 1,
191 ea7924dc Michael Walle
    .minimum_version_id = 1,
192 ea7924dc Michael Walle
    .minimum_version_id_old = 1,
193 ea7924dc Michael Walle
    .fields      = (VMStateField[]) {
194 ea7924dc Michael Walle
        VMSTATE_PTIMER(ptimer, LM32TimerState),
195 ea7924dc Michael Walle
        VMSTATE_UINT32(freq_hz, LM32TimerState),
196 ea7924dc Michael Walle
        VMSTATE_UINT32_ARRAY(regs, LM32TimerState, R_MAX),
197 ea7924dc Michael Walle
        VMSTATE_END_OF_LIST()
198 ea7924dc Michael Walle
    }
199 ea7924dc Michael Walle
};
200 ea7924dc Michael Walle
201 ea7924dc Michael Walle
static SysBusDeviceInfo lm32_timer_info = {
202 ea7924dc Michael Walle
    .init = lm32_timer_init,
203 ea7924dc Michael Walle
    .qdev.name  = "lm32-timer",
204 ea7924dc Michael Walle
    .qdev.size  = sizeof(LM32TimerState),
205 ea7924dc Michael Walle
    .qdev.vmsd  = &vmstate_lm32_timer,
206 ea7924dc Michael Walle
    .qdev.reset = timer_reset,
207 ea7924dc Michael Walle
    .qdev.props = (Property[]) {
208 ea7924dc Michael Walle
        DEFINE_PROP_UINT32(
209 ea7924dc Michael Walle
                "frequency", LM32TimerState, freq_hz, DEFAULT_FREQUENCY
210 ea7924dc Michael Walle
        ),
211 ea7924dc Michael Walle
        DEFINE_PROP_END_OF_LIST(),
212 ea7924dc Michael Walle
    }
213 ea7924dc Michael Walle
};
214 ea7924dc Michael Walle
215 ea7924dc Michael Walle
static void lm32_timer_register(void)
216 ea7924dc Michael Walle
{
217 ea7924dc Michael Walle
    sysbus_register_withprop(&lm32_timer_info);
218 ea7924dc Michael Walle
}
219 ea7924dc Michael Walle
220 ea7924dc Michael Walle
device_init(lm32_timer_register)