]>
Commit | Line | Data |
---|---|---|
b9dc07d4 PM |
1 | /* |
2 | * Private peripheral timer/watchdog blocks for ARM 11MPCore and A9MP | |
3 | * | |
4 | * Copyright (c) 2006-2007 CodeSourcery. | |
5 | * Copyright (c) 2011 Linaro Limited | |
6 | * Written by Paul Brook, Peter Maydell | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU General Public License | |
10 | * as published by the Free Software Foundation; either version | |
11 | * 2 of the License, or (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License along | |
19 | * with this program; if not, see <http://www.gnu.org/licenses/>. | |
20 | */ | |
21 | ||
8ef94f0b | 22 | #include "qemu/osdep.h" |
650d103d | 23 | #include "hw/hw.h" |
64552b6b | 24 | #include "hw/irq.h" |
226fb5aa | 25 | #include "hw/ptimer.h" |
a27bd6c7 | 26 | #include "hw/qdev-properties.h" |
eb110bd8 | 27 | #include "hw/timer/arm_mptimer.h" |
d6454270 | 28 | #include "migration/vmstate.h" |
da34e65c | 29 | #include "qapi/error.h" |
0b8fa32f | 30 | #include "qemu/module.h" |
2e5b09fd | 31 | #include "hw/core/cpu.h" |
b9dc07d4 | 32 | |
226fb5aa DO |
33 | #define PTIMER_POLICY \ |
34 | (PTIMER_POLICY_WRAP_AFTER_ONE_PERIOD | \ | |
35 | PTIMER_POLICY_CONTINUOUS_TRIGGER | \ | |
36 | PTIMER_POLICY_NO_IMMEDIATE_TRIGGER | \ | |
37 | PTIMER_POLICY_NO_IMMEDIATE_RELOAD | \ | |
38 | PTIMER_POLICY_NO_COUNTER_ROUND_DOWN) | |
39 | ||
b9dc07d4 PM |
40 | /* This device implements the per-cpu private timer and watchdog block |
41 | * which is used in both the ARM11MPCore and Cortex-A9MP. | |
42 | */ | |
43 | ||
c6205ddf | 44 | static inline int get_current_cpu(ARMMPTimerState *s) |
b9dc07d4 | 45 | { |
226fb5aa DO |
46 | int cpu_id = current_cpu ? current_cpu->cpu_index : 0; |
47 | ||
48 | if (cpu_id >= s->num_cpu) { | |
b9dc07d4 | 49 | hw_error("arm_mptimer: num-cpu %d but this cpu is %d!\n", |
226fb5aa | 50 | s->num_cpu, cpu_id); |
b9dc07d4 | 51 | } |
226fb5aa DO |
52 | |
53 | return cpu_id; | |
b9dc07d4 PM |
54 | } |
55 | ||
c6205ddf | 56 | static inline void timerblock_update_irq(TimerBlock *tb) |
b9dc07d4 | 57 | { |
257621a9 | 58 | qemu_set_irq(tb->irq, tb->status && (tb->control & 4)); |
b9dc07d4 PM |
59 | } |
60 | ||
61 | /* Return conversion factor from mpcore timer ticks to qemu timer ticks. */ | |
226fb5aa | 62 | static inline uint32_t timerblock_scale(uint32_t control) |
b9dc07d4 | 63 | { |
226fb5aa | 64 | return (((control >> 8) & 0xff) + 1) * 10; |
b9dc07d4 PM |
65 | } |
66 | ||
581b0880 | 67 | /* Must be called within a ptimer transaction block */ |
226fb5aa DO |
68 | static inline void timerblock_set_count(struct ptimer_state *timer, |
69 | uint32_t control, uint64_t *count) | |
b9dc07d4 | 70 | { |
226fb5aa DO |
71 | /* PTimer would trigger interrupt for periodic timer when counter set |
72 | * to 0, MPtimer under certain condition only. | |
73 | */ | |
74 | if ((control & 3) == 3 && (control & 0xff00) == 0 && *count == 0) { | |
75 | *count = ptimer_get_limit(timer); | |
b9dc07d4 | 76 | } |
226fb5aa DO |
77 | ptimer_set_count(timer, *count); |
78 | } | |
79 | ||
581b0880 | 80 | /* Must be called within a ptimer transaction block */ |
226fb5aa DO |
81 | static inline void timerblock_run(struct ptimer_state *timer, |
82 | uint32_t control, uint32_t load) | |
83 | { | |
84 | if ((control & 1) && ((control & 0xff00) || load != 0)) { | |
85 | ptimer_run(timer, !(control & 2)); | |
b9dc07d4 | 86 | } |
b9dc07d4 PM |
87 | } |
88 | ||
89 | static void timerblock_tick(void *opaque) | |
90 | { | |
c6205ddf | 91 | TimerBlock *tb = (TimerBlock *)opaque; |
226fb5aa DO |
92 | /* Periodic timer with load = 0 and prescaler != 0 would re-trigger |
93 | * IRQ after one period, otherwise it either stops or wraps around. | |
94 | */ | |
95 | if ((tb->control & 2) && (tb->control & 0xff00) == 0 && | |
96 | ptimer_get_limit(tb->timer) == 0) { | |
97 | ptimer_stop(tb->timer); | |
b9dc07d4 | 98 | } |
226fb5aa | 99 | tb->status = 1; |
b9dc07d4 PM |
100 | timerblock_update_irq(tb); |
101 | } | |
102 | ||
a8170e5e | 103 | static uint64_t timerblock_read(void *opaque, hwaddr addr, |
b9dc07d4 PM |
104 | unsigned size) |
105 | { | |
c6205ddf | 106 | TimerBlock *tb = (TimerBlock *)opaque; |
b9dc07d4 PM |
107 | switch (addr) { |
108 | case 0: /* Load */ | |
226fb5aa | 109 | return ptimer_get_limit(tb->timer); |
b9dc07d4 | 110 | case 4: /* Counter. */ |
226fb5aa | 111 | return ptimer_get_count(tb->timer); |
b9dc07d4 PM |
112 | case 8: /* Control. */ |
113 | return tb->control; | |
114 | case 12: /* Interrupt status. */ | |
115 | return tb->status; | |
116 | default: | |
117 | return 0; | |
118 | } | |
119 | } | |
120 | ||
a8170e5e | 121 | static void timerblock_write(void *opaque, hwaddr addr, |
b9dc07d4 PM |
122 | uint64_t value, unsigned size) |
123 | { | |
c6205ddf | 124 | TimerBlock *tb = (TimerBlock *)opaque; |
226fb5aa | 125 | uint32_t control = tb->control; |
b9dc07d4 PM |
126 | switch (addr) { |
127 | case 0: /* Load */ | |
581b0880 | 128 | ptimer_transaction_begin(tb->timer); |
226fb5aa DO |
129 | /* Setting load to 0 stops the timer without doing the tick if |
130 | * prescaler = 0. | |
131 | */ | |
132 | if ((control & 1) && (control & 0xff00) == 0 && value == 0) { | |
133 | ptimer_stop(tb->timer); | |
b9dc07d4 | 134 | } |
226fb5aa DO |
135 | ptimer_set_limit(tb->timer, value, 1); |
136 | timerblock_run(tb->timer, control, value); | |
581b0880 | 137 | ptimer_transaction_commit(tb->timer); |
226fb5aa DO |
138 | break; |
139 | case 4: /* Counter. */ | |
581b0880 | 140 | ptimer_transaction_begin(tb->timer); |
226fb5aa DO |
141 | /* Setting counter to 0 stops the one-shot timer, or periodic with |
142 | * load = 0, without doing the tick if prescaler = 0. | |
143 | */ | |
144 | if ((control & 1) && (control & 0xff00) == 0 && value == 0 && | |
145 | (!(control & 2) || ptimer_get_limit(tb->timer) == 0)) { | |
146 | ptimer_stop(tb->timer); | |
b9dc07d4 | 147 | } |
226fb5aa DO |
148 | timerblock_set_count(tb->timer, control, &value); |
149 | timerblock_run(tb->timer, control, value); | |
581b0880 | 150 | ptimer_transaction_commit(tb->timer); |
b9dc07d4 PM |
151 | break; |
152 | case 8: /* Control. */ | |
581b0880 | 153 | ptimer_transaction_begin(tb->timer); |
226fb5aa DO |
154 | if ((control & 3) != (value & 3)) { |
155 | ptimer_stop(tb->timer); | |
156 | } | |
157 | if ((control & 0xff00) != (value & 0xff00)) { | |
158 | ptimer_set_period(tb->timer, timerblock_scale(value)); | |
159 | } | |
8a52340c | 160 | if (value & 1) { |
226fb5aa DO |
161 | uint64_t count = ptimer_get_count(tb->timer); |
162 | /* Re-load periodic timer counter if needed. */ | |
163 | if ((value & 2) && count == 0) { | |
164 | timerblock_set_count(tb->timer, value, &count); | |
8a52340c | 165 | } |
226fb5aa | 166 | timerblock_run(tb->timer, value, count); |
b9dc07d4 | 167 | } |
226fb5aa | 168 | tb->control = value; |
581b0880 | 169 | ptimer_transaction_commit(tb->timer); |
b9dc07d4 PM |
170 | break; |
171 | case 12: /* Interrupt status. */ | |
172 | tb->status &= ~value; | |
173 | timerblock_update_irq(tb); | |
174 | break; | |
175 | } | |
176 | } | |
177 | ||
178 | /* Wrapper functions to implement the "read timer/watchdog for | |
179 | * the current CPU" memory regions. | |
180 | */ | |
a8170e5e | 181 | static uint64_t arm_thistimer_read(void *opaque, hwaddr addr, |
b9dc07d4 PM |
182 | unsigned size) |
183 | { | |
c6205ddf | 184 | ARMMPTimerState *s = (ARMMPTimerState *)opaque; |
b9dc07d4 | 185 | int id = get_current_cpu(s); |
cde4577f | 186 | return timerblock_read(&s->timerblock[id], addr, size); |
b9dc07d4 PM |
187 | } |
188 | ||
a8170e5e | 189 | static void arm_thistimer_write(void *opaque, hwaddr addr, |
b9dc07d4 PM |
190 | uint64_t value, unsigned size) |
191 | { | |
c6205ddf | 192 | ARMMPTimerState *s = (ARMMPTimerState *)opaque; |
b9dc07d4 | 193 | int id = get_current_cpu(s); |
cde4577f | 194 | timerblock_write(&s->timerblock[id], addr, value, size); |
b9dc07d4 PM |
195 | } |
196 | ||
197 | static const MemoryRegionOps arm_thistimer_ops = { | |
198 | .read = arm_thistimer_read, | |
199 | .write = arm_thistimer_write, | |
200 | .valid = { | |
201 | .min_access_size = 4, | |
202 | .max_access_size = 4, | |
203 | }, | |
204 | .endianness = DEVICE_NATIVE_ENDIAN, | |
205 | }; | |
206 | ||
b9dc07d4 PM |
207 | static const MemoryRegionOps timerblock_ops = { |
208 | .read = timerblock_read, | |
209 | .write = timerblock_write, | |
210 | .valid = { | |
211 | .min_access_size = 4, | |
212 | .max_access_size = 4, | |
213 | }, | |
214 | .endianness = DEVICE_NATIVE_ENDIAN, | |
215 | }; | |
216 | ||
c6205ddf | 217 | static void timerblock_reset(TimerBlock *tb) |
b9dc07d4 | 218 | { |
b9dc07d4 PM |
219 | tb->control = 0; |
220 | tb->status = 0; | |
bdac1c1e | 221 | if (tb->timer) { |
581b0880 | 222 | ptimer_transaction_begin(tb->timer); |
226fb5aa DO |
223 | ptimer_stop(tb->timer); |
224 | ptimer_set_limit(tb->timer, 0, 1); | |
225 | ptimer_set_period(tb->timer, timerblock_scale(0)); | |
581b0880 | 226 | ptimer_transaction_commit(tb->timer); |
bdac1c1e | 227 | } |
b9dc07d4 PM |
228 | } |
229 | ||
230 | static void arm_mptimer_reset(DeviceState *dev) | |
231 | { | |
68653fd6 | 232 | ARMMPTimerState *s = ARM_MPTIMER(dev); |
b9dc07d4 | 233 | int i; |
68653fd6 | 234 | |
b9dc07d4 PM |
235 | for (i = 0; i < ARRAY_SIZE(s->timerblock); i++) { |
236 | timerblock_reset(&s->timerblock[i]); | |
237 | } | |
238 | } | |
239 | ||
a1f9a907 | 240 | static void arm_mptimer_init(Object *obj) |
b9dc07d4 | 241 | { |
0aadb490 AF |
242 | ARMMPTimerState *s = ARM_MPTIMER(obj); |
243 | ||
244 | memory_region_init_io(&s->iomem, obj, &arm_thistimer_ops, s, | |
245 | "arm_mptimer_timer", 0x20); | |
246 | sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->iomem); | |
247 | } | |
248 | ||
249 | static void arm_mptimer_realize(DeviceState *dev, Error **errp) | |
250 | { | |
251 | SysBusDevice *sbd = SYS_BUS_DEVICE(dev); | |
68653fd6 | 252 | ARMMPTimerState *s = ARM_MPTIMER(dev); |
b9dc07d4 | 253 | int i; |
68653fd6 | 254 | |
eb110bd8 | 255 | if (s->num_cpu < 1 || s->num_cpu > ARM_MPTIMER_MAX_CPUS) { |
b097e481 MA |
256 | error_setg(errp, "num-cpu must be between 1 and %d", |
257 | ARM_MPTIMER_MAX_CPUS); | |
258 | return; | |
b9dc07d4 | 259 | } |
cde4577f | 260 | /* We implement one timer block per CPU, and expose multiple MMIO regions: |
b9dc07d4 | 261 | * * region 0 is "timer for this core" |
cde4577f PC |
262 | * * region 1 is "timer for core 0" |
263 | * * region 2 is "timer for core 1" | |
b9dc07d4 PM |
264 | * and so on. |
265 | * The outgoing interrupt lines are | |
266 | * * timer for core 0 | |
b9dc07d4 | 267 | * * timer for core 1 |
b9dc07d4 PM |
268 | * and so on. |
269 | */ | |
cde4577f | 270 | for (i = 0; i < s->num_cpu; i++) { |
c6205ddf | 271 | TimerBlock *tb = &s->timerblock[i]; |
581b0880 | 272 | tb->timer = ptimer_init(timerblock_tick, tb, PTIMER_POLICY); |
0aadb490 | 273 | sysbus_init_irq(sbd, &tb->irq); |
853dca12 | 274 | memory_region_init_io(&tb->iomem, OBJECT(s), &timerblock_ops, tb, |
b9dc07d4 | 275 | "arm_mptimer_timerblock", 0x20); |
0aadb490 | 276 | sysbus_init_mmio(sbd, &tb->iomem); |
b9dc07d4 | 277 | } |
b9dc07d4 PM |
278 | } |
279 | ||
280 | static const VMStateDescription vmstate_timerblock = { | |
281 | .name = "arm_mptimer_timerblock", | |
226fb5aa DO |
282 | .version_id = 3, |
283 | .minimum_version_id = 3, | |
b9dc07d4 | 284 | .fields = (VMStateField[]) { |
c6205ddf PC |
285 | VMSTATE_UINT32(control, TimerBlock), |
286 | VMSTATE_UINT32(status, TimerBlock), | |
226fb5aa | 287 | VMSTATE_PTIMER(timer, TimerBlock), |
b9dc07d4 PM |
288 | VMSTATE_END_OF_LIST() |
289 | } | |
290 | }; | |
291 | ||
292 | static const VMStateDescription vmstate_arm_mptimer = { | |
293 | .name = "arm_mptimer", | |
226fb5aa DO |
294 | .version_id = 3, |
295 | .minimum_version_id = 3, | |
b9dc07d4 | 296 | .fields = (VMStateField[]) { |
cde4577f | 297 | VMSTATE_STRUCT_VARRAY_UINT32(timerblock, ARMMPTimerState, num_cpu, |
226fb5aa | 298 | 3, vmstate_timerblock, TimerBlock), |
b9dc07d4 PM |
299 | VMSTATE_END_OF_LIST() |
300 | } | |
301 | }; | |
302 | ||
39bffca2 | 303 | static Property arm_mptimer_properties[] = { |
c6205ddf | 304 | DEFINE_PROP_UINT32("num-cpu", ARMMPTimerState, num_cpu, 0), |
39bffca2 AL |
305 | DEFINE_PROP_END_OF_LIST() |
306 | }; | |
307 | ||
999e12bb AL |
308 | static void arm_mptimer_class_init(ObjectClass *klass, void *data) |
309 | { | |
39bffca2 | 310 | DeviceClass *dc = DEVICE_CLASS(klass); |
999e12bb | 311 | |
0aadb490 | 312 | dc->realize = arm_mptimer_realize; |
39bffca2 AL |
313 | dc->vmsd = &vmstate_arm_mptimer; |
314 | dc->reset = arm_mptimer_reset; | |
4f67d30b | 315 | device_class_set_props(dc, arm_mptimer_properties); |
999e12bb AL |
316 | } |
317 | ||
8c43a6f0 | 318 | static const TypeInfo arm_mptimer_info = { |
68653fd6 | 319 | .name = TYPE_ARM_MPTIMER, |
39bffca2 | 320 | .parent = TYPE_SYS_BUS_DEVICE, |
c6205ddf | 321 | .instance_size = sizeof(ARMMPTimerState), |
a1f9a907 | 322 | .instance_init = arm_mptimer_init, |
39bffca2 | 323 | .class_init = arm_mptimer_class_init, |
b9dc07d4 PM |
324 | }; |
325 | ||
83f7d43a | 326 | static void arm_mptimer_register_types(void) |
b9dc07d4 | 327 | { |
39bffca2 | 328 | type_register_static(&arm_mptimer_info); |
b9dc07d4 PM |
329 | } |
330 | ||
83f7d43a | 331 | type_init(arm_mptimer_register_types) |