]>
Commit | Line | Data |
---|---|---|
a32a2253 CW |
1 | /* |
2 | * QEMU model of the Altera timer. | |
3 | * | |
4 | * Copyright (c) 2012 Chris Wulff <[email protected]> | |
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.1 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 | |
18 | * <http://www.gnu.org/licenses/lgpl-2.1.html> | |
19 | */ | |
20 | ||
21 | #include "qemu/osdep.h" | |
db725815 | 22 | #include "qemu/main-loop.h" |
0b8fa32f | 23 | #include "qemu/module.h" |
a32a2253 CW |
24 | #include "qapi/error.h" |
25 | ||
26 | #include "hw/sysbus.h" | |
27 | #include "sysemu/sysemu.h" | |
64552b6b | 28 | #include "hw/irq.h" |
a32a2253 | 29 | #include "hw/ptimer.h" |
a27bd6c7 | 30 | #include "hw/qdev-properties.h" |
a32a2253 CW |
31 | |
32 | #define R_STATUS 0 | |
33 | #define R_CONTROL 1 | |
34 | #define R_PERIODL 2 | |
35 | #define R_PERIODH 3 | |
36 | #define R_SNAPL 4 | |
37 | #define R_SNAPH 5 | |
38 | #define R_MAX 6 | |
39 | ||
40 | #define STATUS_TO 0x0001 | |
41 | #define STATUS_RUN 0x0002 | |
42 | ||
43 | #define CONTROL_ITO 0x0001 | |
44 | #define CONTROL_CONT 0x0002 | |
45 | #define CONTROL_START 0x0004 | |
46 | #define CONTROL_STOP 0x0008 | |
47 | ||
48 | #define TYPE_ALTERA_TIMER "ALTR.timer" | |
49 | #define ALTERA_TIMER(obj) \ | |
50 | OBJECT_CHECK(AlteraTimer, (obj), TYPE_ALTERA_TIMER) | |
51 | ||
52 | typedef struct AlteraTimer { | |
53 | SysBusDevice busdev; | |
54 | MemoryRegion mmio; | |
55 | qemu_irq irq; | |
56 | uint32_t freq_hz; | |
57 | QEMUBH *bh; | |
58 | ptimer_state *ptimer; | |
59 | uint32_t regs[R_MAX]; | |
60 | } AlteraTimer; | |
61 | ||
62 | static int timer_irq_state(AlteraTimer *t) | |
63 | { | |
64 | bool irq = (t->regs[R_STATUS] & STATUS_TO) && | |
65 | (t->regs[R_CONTROL] & CONTROL_ITO); | |
66 | return irq; | |
67 | } | |
68 | ||
69 | static uint64_t timer_read(void *opaque, hwaddr addr, | |
70 | unsigned int size) | |
71 | { | |
72 | AlteraTimer *t = opaque; | |
73 | uint64_t r = 0; | |
74 | ||
75 | addr >>= 2; | |
76 | ||
77 | switch (addr) { | |
78 | case R_CONTROL: | |
79 | r = t->regs[R_CONTROL] & (CONTROL_ITO | CONTROL_CONT); | |
80 | break; | |
81 | ||
82 | default: | |
83 | if (addr < ARRAY_SIZE(t->regs)) { | |
84 | r = t->regs[addr]; | |
85 | } | |
86 | break; | |
87 | } | |
88 | ||
89 | return r; | |
90 | } | |
91 | ||
92 | static void timer_write(void *opaque, hwaddr addr, | |
93 | uint64_t value, unsigned int size) | |
94 | { | |
95 | AlteraTimer *t = opaque; | |
96 | uint64_t tvalue; | |
97 | uint32_t count = 0; | |
98 | int irqState = timer_irq_state(t); | |
99 | ||
100 | addr >>= 2; | |
101 | ||
102 | switch (addr) { | |
103 | case R_STATUS: | |
104 | /* The timeout bit is cleared by writing the status register. */ | |
105 | t->regs[R_STATUS] &= ~STATUS_TO; | |
106 | break; | |
107 | ||
108 | case R_CONTROL: | |
109 | t->regs[R_CONTROL] = value & (CONTROL_ITO | CONTROL_CONT); | |
110 | if ((value & CONTROL_START) && | |
111 | !(t->regs[R_STATUS] & STATUS_RUN)) { | |
112 | ptimer_run(t->ptimer, 1); | |
113 | t->regs[R_STATUS] |= STATUS_RUN; | |
114 | } | |
115 | if ((value & CONTROL_STOP) && (t->regs[R_STATUS] & STATUS_RUN)) { | |
116 | ptimer_stop(t->ptimer); | |
117 | t->regs[R_STATUS] &= ~STATUS_RUN; | |
118 | } | |
119 | break; | |
120 | ||
121 | case R_PERIODL: | |
122 | case R_PERIODH: | |
123 | t->regs[addr] = value & 0xFFFF; | |
124 | if (t->regs[R_STATUS] & STATUS_RUN) { | |
125 | ptimer_stop(t->ptimer); | |
126 | t->regs[R_STATUS] &= ~STATUS_RUN; | |
127 | } | |
128 | tvalue = (t->regs[R_PERIODH] << 16) | t->regs[R_PERIODL]; | |
129 | ptimer_set_limit(t->ptimer, tvalue + 1, 1); | |
130 | break; | |
131 | ||
132 | case R_SNAPL: | |
133 | case R_SNAPH: | |
134 | count = ptimer_get_count(t->ptimer); | |
135 | t->regs[R_SNAPL] = count & 0xFFFF; | |
136 | t->regs[R_SNAPH] = count >> 16; | |
137 | break; | |
138 | ||
139 | default: | |
140 | break; | |
141 | } | |
142 | ||
143 | if (irqState != timer_irq_state(t)) { | |
144 | qemu_set_irq(t->irq, timer_irq_state(t)); | |
145 | } | |
146 | } | |
147 | ||
148 | static const MemoryRegionOps timer_ops = { | |
149 | .read = timer_read, | |
150 | .write = timer_write, | |
151 | .endianness = DEVICE_NATIVE_ENDIAN, | |
152 | .valid = { | |
153 | .min_access_size = 1, | |
154 | .max_access_size = 4 | |
155 | } | |
156 | }; | |
157 | ||
158 | static void timer_hit(void *opaque) | |
159 | { | |
160 | AlteraTimer *t = opaque; | |
161 | const uint64_t tvalue = (t->regs[R_PERIODH] << 16) | t->regs[R_PERIODL]; | |
162 | ||
163 | t->regs[R_STATUS] |= STATUS_TO; | |
164 | ||
165 | ptimer_set_limit(t->ptimer, tvalue + 1, 1); | |
166 | ||
167 | if (!(t->regs[R_CONTROL] & CONTROL_CONT)) { | |
168 | t->regs[R_STATUS] &= ~STATUS_RUN; | |
169 | ptimer_set_count(t->ptimer, tvalue); | |
170 | } else { | |
171 | ptimer_run(t->ptimer, 1); | |
172 | } | |
173 | ||
174 | qemu_set_irq(t->irq, timer_irq_state(t)); | |
175 | } | |
176 | ||
177 | static void altera_timer_realize(DeviceState *dev, Error **errp) | |
178 | { | |
179 | AlteraTimer *t = ALTERA_TIMER(dev); | |
180 | SysBusDevice *sbd = SYS_BUS_DEVICE(dev); | |
181 | ||
182 | if (t->freq_hz == 0) { | |
183 | error_setg(errp, "\"clock-frequency\" property must be provided."); | |
184 | return; | |
185 | } | |
186 | ||
187 | t->bh = qemu_bh_new(timer_hit, t); | |
188 | t->ptimer = ptimer_init(t->bh, PTIMER_POLICY_DEFAULT); | |
189 | ptimer_set_freq(t->ptimer, t->freq_hz); | |
190 | ||
191 | memory_region_init_io(&t->mmio, OBJECT(t), &timer_ops, t, | |
192 | TYPE_ALTERA_TIMER, R_MAX * sizeof(uint32_t)); | |
193 | sysbus_init_mmio(sbd, &t->mmio); | |
194 | } | |
195 | ||
196 | static void altera_timer_init(Object *obj) | |
197 | { | |
198 | AlteraTimer *t = ALTERA_TIMER(obj); | |
199 | SysBusDevice *sbd = SYS_BUS_DEVICE(obj); | |
200 | ||
201 | sysbus_init_irq(sbd, &t->irq); | |
202 | } | |
203 | ||
204 | static void altera_timer_reset(DeviceState *dev) | |
205 | { | |
206 | AlteraTimer *t = ALTERA_TIMER(dev); | |
207 | ||
208 | ptimer_stop(t->ptimer); | |
209 | ptimer_set_limit(t->ptimer, 0xffffffff, 1); | |
cc16ee9d | 210 | memset(t->regs, 0, sizeof(t->regs)); |
a32a2253 CW |
211 | } |
212 | ||
213 | static Property altera_timer_properties[] = { | |
214 | DEFINE_PROP_UINT32("clock-frequency", AlteraTimer, freq_hz, 0), | |
215 | DEFINE_PROP_END_OF_LIST(), | |
216 | }; | |
217 | ||
218 | static void altera_timer_class_init(ObjectClass *klass, void *data) | |
219 | { | |
220 | DeviceClass *dc = DEVICE_CLASS(klass); | |
221 | ||
222 | dc->realize = altera_timer_realize; | |
223 | dc->props = altera_timer_properties; | |
224 | dc->reset = altera_timer_reset; | |
225 | } | |
226 | ||
227 | static const TypeInfo altera_timer_info = { | |
228 | .name = TYPE_ALTERA_TIMER, | |
229 | .parent = TYPE_SYS_BUS_DEVICE, | |
230 | .instance_size = sizeof(AlteraTimer), | |
231 | .instance_init = altera_timer_init, | |
232 | .class_init = altera_timer_class_init, | |
233 | }; | |
234 | ||
235 | static void altera_timer_register(void) | |
236 | { | |
237 | type_register_static(&altera_timer_info); | |
238 | } | |
239 | ||
240 | type_init(altera_timer_register) |