Statistics
| Branch: | Revision:

root / hw / etraxfs_timer.c @ e62b5b13

History | View | Annotate | Download (6.6 kB)

1
/*
2
 * QEMU ETRAX Timers
3
 *
4
 * Copyright (c) 2007 Edgar E. Iglesias, Axis Communications AB.
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24
#include <stdio.h>
25
#include <sys/time.h>
26
#include "hw.h"
27
#include "qemu-timer.h"
28

    
29
#define D(x)
30

    
31
#define R_TIME 0xb001e038
32
#define RW_TMR0_DIV 0xb001e000
33
#define R_TMR0_DATA 0xb001e004
34
#define RW_TMR0_CTRL 0xb001e008
35
#define RW_TMR1_DIV 0xb001e010
36
#define R_TMR1_DATA 0xb001e014
37
#define RW_TMR1_CTRL 0xb001e018
38

    
39
#define RW_WD_CTRL 0xb001e040
40
#define RW_INTR_MASK 0xb001e048
41
#define RW_ACK_INTR 0xb001e04c
42
#define R_INTR 0xb001e050
43
#define R_MASKED_INTR 0xb001e054
44

    
45
struct fs_timer_t {
46
        QEMUBH *bh;
47
        unsigned int limit;
48
        int scale;
49
        ptimer_state *ptimer;
50
        CPUState *env;
51
        qemu_irq *irq;
52
        uint32_t mask;
53
        struct timeval last;
54

    
55
        uint32_t rw_intr_mask;
56
        uint32_t rw_ack_intr;
57
        uint32_t r_intr;
58
};
59

    
60
static struct fs_timer_t timer[2];
61

    
62
static inline int timer_index(target_phys_addr_t addr)
63
{
64
        int t = 0;
65
        if (addr >= 0xb005e000)
66
                t = 1;
67
        return t;
68
}
69

    
70
/* diff two timevals.  Return a single int in us. */
71
int diff_timeval_us(struct timeval *a, struct timeval *b)
72
{
73
        int diff;
74

    
75
        /* assume these values are signed.  */
76
        diff = (a->tv_sec - b->tv_sec) * 1000 * 1000;
77
        diff += (a->tv_usec - b->tv_usec);
78
        return diff;
79
}
80

    
81
static uint32_t timer_readb (void *opaque, target_phys_addr_t addr)
82
{
83
        CPUState *env;
84
        uint32_t r = 0;
85

    
86
        env = opaque;
87
        D(printf ("%s %x pc=%x\n", __func__, addr, env->pc));
88
        return r;
89
}
90
static uint32_t timer_readw (void *opaque, target_phys_addr_t addr)
91
{
92
        CPUState *env;
93
        uint32_t r = 0;
94

    
95
        env = opaque;
96
        D(printf ("%s %x pc=%x\n", __func__, addr, env->pc));
97
        return r;
98
}
99

    
100
static uint32_t timer_readl (void *opaque, target_phys_addr_t addr)
101
{
102
        CPUState *env = opaque;
103
        uint32_t r = 0;
104
        int t = timer_index(addr);
105

    
106
        switch (addr) {
107
        case R_TMR0_DATA:
108
                break;
109
        case R_TMR1_DATA:
110
                D(printf ("R_TMR1_DATA\n"));
111
                break;
112
        case R_TIME:
113
        {
114
                struct timeval now;
115
                gettimeofday(&now, NULL);
116
                if (!(timer[t].last.tv_sec == 0 
117
                      && timer[t].last.tv_usec == 0)) {
118
                        r = diff_timeval_us(&now, &timer[t].last);
119
                        r *= 1000; /* convert to ns.  */
120
                        r++; /* make sure we increase for each call.  */
121
                }
122
                timer[t].last = now;
123
                break;
124
        }
125

    
126
        case RW_INTR_MASK:
127
                r = timer[t].rw_intr_mask;
128
                break;
129
        case R_MASKED_INTR:
130
                r = timer[t].r_intr & timer[t].rw_intr_mask;
131
                break;
132
        default:
133
                D(printf ("%s %x p=%x\n", __func__, addr, env->pc));
134
                break;
135
        }
136
        return r;
137
}
138

    
139
static void
140
timer_writeb (void *opaque, target_phys_addr_t addr, uint32_t value)
141
{
142
        CPUState *env;
143
        env = opaque;
144
        D(printf ("%s %x %x pc=%x\n", __func__, addr, value, env->pc));
145
}
146
static void
147
timer_writew (void *opaque, target_phys_addr_t addr, uint32_t value)
148
{
149
        CPUState *env;
150
        env = opaque;
151
        D(printf ("%s %x %x pc=%x\n", __func__, addr, value, env->pc));
152
}
153

    
154
static void write_ctrl(struct fs_timer_t *t, uint32_t v)
155
{
156
        int op;
157
        int freq;
158
        int freq_hz;
159

    
160
        op = v & 3;
161
        freq = v >> 2;
162
        freq_hz = 32000000;
163

    
164
        switch (freq)
165
        {
166
        case 0:
167
        case 1:
168
                D(printf ("extern or disabled timer clock?\n"));
169
                break;
170
        case 4: freq_hz =  29493000; break;
171
        case 5: freq_hz =  32000000; break;
172
        case 6: freq_hz =  32768000; break;
173
        case 7: freq_hz = 100000000; break;
174
        default:
175
                abort();
176
                break;
177
        }
178

    
179
        D(printf ("freq_hz=%d limit=%d\n", freq_hz, t->limit));
180
        t->scale = 0;
181
        if (t->limit > 2048)
182
        {
183
                t->scale = 2048;
184
                ptimer_set_period(t->ptimer, freq_hz / t->scale);
185
        }
186

    
187
        switch (op)
188
        {
189
                case 0:
190
                        D(printf ("limit=%d %d\n", 
191
                                  t->limit, t->limit/t->scale));
192
                        ptimer_set_limit(t->ptimer, t->limit / t->scale, 1);
193
                        break;
194
                case 1:
195
                        ptimer_stop(t->ptimer);
196
                        break;
197
                case 2:
198
                        ptimer_run(t->ptimer, 0);
199
                        break;
200
                default:
201
                        abort();
202
                        break;
203
        }
204
}
205

    
206
static void timer_ack_irq(struct fs_timer_t *t)
207
{
208
        if (!(t->r_intr & t->mask & t->rw_intr_mask))
209
                qemu_irq_lower(t->irq[0]);
210
}
211

    
212
static void
213
timer_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
214
{
215
        CPUState *env = opaque;
216
        int t = timer_index(addr);
217

    
218
        D(printf ("%s %x %x pc=%x\n",
219
                __func__, addr, value, env->pc));
220
        switch (addr)
221
        {
222
                case RW_TMR0_DIV:
223
                        D(printf ("RW_TMR0_DIV=%x\n", value));
224
                        timer[t].limit = value;
225
                        break;
226
                case RW_TMR0_CTRL:
227
                        D(printf ("RW_TMR0_CTRL=%x\n", value));
228
                        write_ctrl(&timer[t], value);
229
                        break;
230
                case RW_TMR1_DIV:
231
                        D(printf ("RW_TMR1_DIV=%x\n", value));
232
                        break;
233
                case RW_TMR1_CTRL:
234
                        D(printf ("RW_TMR1_CTRL=%x\n", value));
235
                        break;
236
                case RW_INTR_MASK:
237
                        D(printf ("RW_INTR_MASK=%x\n", value));
238
                        timer[t].rw_intr_mask = value;
239
                        break;
240
                case RW_WD_CTRL:
241
                        D(printf ("RW_WD_CTRL=%x\n", value));
242
                        break;
243
                case RW_ACK_INTR:
244
                        timer[t].r_intr &= ~value;
245
                        timer_ack_irq(&timer[t]);
246
                        break;
247
                default:
248
                        printf ("%s %x %x pc=%x\n",
249
                                __func__, addr, value, env->pc);
250
                        break;
251
        }
252
}
253

    
254
static CPUReadMemoryFunc *timer_read[] = {
255
    &timer_readb,
256
    &timer_readw,
257
    &timer_readl,
258
};
259

    
260
static CPUWriteMemoryFunc *timer_write[] = {
261
    &timer_writeb,
262
    &timer_writew,
263
    &timer_writel,
264
};
265

    
266
static void timer_irq(void *opaque)
267
{
268
        struct fs_timer_t *t = opaque;
269
        t->r_intr |= t->mask;
270
        if (t->mask & t->rw_intr_mask) {
271
                D(printf("%s raise\n", __func__));
272
                qemu_irq_raise(t->irq[0]);
273
        }
274
}
275

    
276
void etraxfs_timer_init(CPUState *env, qemu_irq *irqs)
277
{
278
        int timer_regs;
279

    
280
        timer[0].bh = qemu_bh_new(timer_irq, &timer[0]);
281
        timer[0].ptimer = ptimer_init(timer[0].bh);
282
        timer[0].irq = irqs + 26;
283
        timer[0].mask = 1;
284
        timer[0].env = env;
285

    
286
        timer[1].bh = qemu_bh_new(timer_irq, &timer[1]);
287
        timer[1].ptimer = ptimer_init(timer[1].bh);
288
        timer[1].irq = irqs + 26;
289
        timer[1].mask = 1;
290
        timer[1].env = env;
291

    
292
        timer_regs = cpu_register_io_memory(0, timer_read, timer_write, env);
293
        cpu_register_physical_memory (0xb001e000, 0x5c, timer_regs);
294
        cpu_register_physical_memory (0xb005e000, 0x5c, timer_regs);
295
}