at91_tc.c
9.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
* AT91 Timer Counter
*
* Copyright (c) 2009 Filip Navara
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "sysbus.h"
#include "qemu-timer.h"
#include "at91.h"
#define TC_SIZE 0x4000
#define TC_CCR 0x00 /* Channel Control Register */
#define TC_CMR 0x04 /* Channel Mode Register */
#define TC_CV 0x10 /* Counter Value Register */
#define TC_RA 0x14 /* Register A */
#define TC_RB 0x18 /* Register B */
#define TC_RC 0x1c /* Register C */
#define TC_SR 0x20 /* Status Register */
#define TC_IER 0x24 /* Interrupt Enable Register */
#define TC_IDR 0x28 /* Interrupt Disable Register */
#define TC_IMR 0x2c /* Interrupt Mask Register */
#define TC_BCR 0xc0 /* Block Control Register */
#define TC_BMR 0xc4 /* Block Mode Register */
#define MR_CLKI 0x08 /* Clock Invert */
#define MR_CPCSTOP 0x40 /* Counter Clock Stopped with RC Compare */
#define MR_CPCDIS 0x80 /* Counter Clock Disable with RC Compare */
#define MR_RCTRIG 0x4000
#define MR_WAVE 0x8000
#define SR_CPAS 0x04
#define SR_CPBS 0x08
#define SR_CPCS 0x10
typedef struct TCChannelState {
qemu_irq irq;
ptimer_state *timer;
uint32_t mr;
uint16_t cv;
uint16_t ra;
uint16_t rb;
uint16_t rc;
uint32_t sr;
uint32_t imr;
} TCChannelState;
typedef struct TCState {
SysBusDevice busdev;
TCChannelState channels[3];
} TCState;
static void at91_tc_tick(void *opaque)
{
TCChannelState *s = opaque;
if (s->mr & MR_WAVE) {
s->cv++;
/* TODO: Overflow check */
if (s->cv == s->ra) {
s->sr |= SR_CPAS;
}
if (s->cv == s->rb) {
s->sr |= SR_CPBS;
}
if (s->cv == s->rc) {
s->sr |= SR_CPCS;
if (s->mr & MR_RCTRIG) {
s->cv = 0;
}
}
} else {
s->cv = s->rc;
s->sr |= SR_CPCS;
if (s->mr & MR_RCTRIG) {
s->cv = 0;
}
}
if (s->sr & s->imr) {
qemu_set_irq(s->irq, 1);
}
}
static uint32_t at91_tc_channel_read(TCChannelState *s,
target_phys_addr_t offset)
{
uint32_t sr;
switch (offset) {
case TC_CMR:
return s->mr;
case TC_CV:
return s->cv;
case TC_RA:
return s->ra;
case TC_RB:
return s->rb;
case TC_RC:
return s->rc;
case TC_SR:
sr = s->sr;
s->sr = 0;
qemu_set_irq(s->irq, 0);
return sr;
case TC_IMR:
return s->imr;
default:
return 0;
}
}
static int at91_tc_get_freq(uint32_t cmr)
{
int freq;
switch (cmr & 7) {
case 0: freq = at91_master_clock_frequency / 2; break;
case 1: freq = at91_master_clock_frequency / 8; break;
case 2: freq = at91_master_clock_frequency / 32; break;
case 3: freq = at91_master_clock_frequency / 128; break;
case 4: freq = at91_master_clock_frequency / 1024; break;
default: /* TODO: External clocks */
freq = at91_master_clock_frequency / 16;
break;
}
return freq;
}
static void at91_tc_channel_write(TCChannelState *s,
target_phys_addr_t offset, uint32_t value)
{
int freq;
switch (offset) {
case TC_CCR:
if ((value & 3) == 1) {
if (!(s->mr & MR_WAVE)) {
if (s->imr == 0 && s->rc != 0) {
freq = at91_tc_get_freq(s->mr);
freq /= s->rc;
if (freq > 1000) {
//just busy loop too wait something, with resolution more then 1ms
s->cv = s->rc;
s->sr |= SR_CPCS;
if (s->mr & MR_RCTRIG) {
s->cv = 0;
}
break;
}
}
//tick only once, this should speedup system
ptimer_set_limit(s->timer, s->rc, 1);
}
s->cv = 0;
ptimer_run(s->timer, 0);
} else if (value & 2) {
ptimer_stop(s->timer);
ptimer_set_limit(s->timer, 1, 1);
}
break;
case TC_CMR:
freq = at91_tc_get_freq(value);
ptimer_set_freq(s->timer, freq);
s->mr = value;
break;
case TC_RA:
s->ra = value;
break;
case TC_RB:
s->rb = value;
break;
case TC_RC:
s->rc = value;
break;
case TC_IER:
s->imr |= value;
break;
case TC_IDR:
s->imr &= ~value;
break;
}
}
static uint32_t at91_tc_mem_read(void *opaque, target_phys_addr_t offset)
{
TCState *s = opaque;
offset &= TC_SIZE - 1;
switch (offset) {
case TC_BMR:
return 0; /* TODO */
case 0 ... 0x3f:
return at91_tc_channel_read(&s->channels[0], offset);
case 0x40 ... 0x7f:
return at91_tc_channel_read(&s->channels[1], offset - 0x40);
case 0x80 ... 0xbf:
return at91_tc_channel_read(&s->channels[2], offset - 0x80);
default:
return 0;
}
}
static void at91_tc_mem_write(void *opaque, target_phys_addr_t offset,
uint32_t value)
{
TCState *s = opaque;
offset &= TC_SIZE - 1;
switch (offset) {
case TC_BCR:
return; /* TODO */
case TC_BMR:
return; /* TODO */
case 0 ... 0x3f:
at91_tc_channel_write(&s->channels[0], offset, value);
break;
case 0x40 ... 0x7f:
at91_tc_channel_write(&s->channels[1], offset - 0x40, value);
break;
case 0x80 ... 0xbf:
at91_tc_channel_write(&s->channels[2], offset - 0x80, value);
break;
}
}
static CPUReadMemoryFunc *at91_tc_readfn[] = {
at91_tc_mem_read,
at91_tc_mem_read,
at91_tc_mem_read,
};
static CPUWriteMemoryFunc *at91_tc_writefn[] = {
at91_tc_mem_write,
at91_tc_mem_write,
at91_tc_mem_write,
};
static void at91_tc_save(QEMUFile *f, void *opaque)
{
TCState *s = opaque;
int channel;
for (channel = 0; channel < 3; channel++) {
qemu_put_ptimer(f, s->channels[channel].timer);
qemu_put_be32(f, s->channels[channel].mr);
qemu_put_be16(f, s->channels[channel].cv);
qemu_put_be16(f, s->channels[channel].ra);
qemu_put_be16(f, s->channels[channel].rb);
qemu_put_be16(f, s->channels[channel].rc);
qemu_put_be32(f, s->channels[channel].sr);
qemu_put_be32(f, s->channels[channel].imr);
}
}
static int at91_tc_load(QEMUFile *f, void *opaque, int version_id)
{
TCState *s = opaque;
int channel;
if (version_id != 1)
return -EINVAL;
for (channel = 0; channel < 3; channel++) {
qemu_get_ptimer(f, s->channels[channel].timer);
s->channels[channel].mr = qemu_get_be32(f);
s->channels[channel].cv = qemu_get_be16(f);
s->channels[channel].ra = qemu_get_be16(f);
s->channels[channel].rb = qemu_get_be16(f);
s->channels[channel].rc = qemu_get_be16(f);
s->channels[channel].sr = qemu_get_be32(f);
s->channels[channel].imr = qemu_get_be32(f);
}
return 0;
}
static void at91_tc_reset(void *opaque)
{
TCState *s = opaque;
int channel;
for (channel = 0; channel < 3; channel++) {
ptimer_stop(s->channels[channel].timer);
s->channels[channel].mr = 0;
s->channels[channel].cv = 0;
s->channels[channel].ra = 0;
s->channels[channel].rb = 0;
s->channels[channel].rc = 0;
s->channels[channel].sr = 0;
s->channels[channel].imr = 0;
}
}
static void at91_tc_init(SysBusDevice *dev)
{
TCState *s = FROM_SYSBUS(typeof (*s), dev);
QEMUBH *bh;
int tc_regs;
int channel;
for (channel = 0; channel < 3; channel++) {
bh = qemu_bh_new(at91_tc_tick, &s->channels[channel]);
s->channels[channel].timer = ptimer_init(bh);
ptimer_set_limit(s->channels[channel].timer, 1, 1);
sysbus_init_irq(dev, &s->channels[channel].irq);
}
tc_regs = cpu_register_io_memory(at91_tc_readfn,
at91_tc_writefn, s);
sysbus_init_mmio(dev, TC_SIZE, tc_regs);
qemu_register_reset(at91_tc_reset, s);
register_savevm("at91_tc", -1, 1, at91_tc_save, at91_tc_load, s);
}
static void at91_tc_register(void)
{
sysbus_register_dev("at91,tc", sizeof(TCState), at91_tc_init);
}
device_init(at91_tc_register)