Statistics
| Branch: | Revision:

root / hw / sh_timer.c @ 6059631c

History | View | Annotate | Download (8.6 kB)

1 cd1a3f68 ths
/*
2 cd1a3f68 ths
 * SuperH Timer modules.
3 cd1a3f68 ths
 *
4 cd1a3f68 ths
 * Copyright (c) 2007 Magnus Damm
5 cd1a3f68 ths
 * Based on arm_timer.c by Paul Brook
6 cd1a3f68 ths
 * Copyright (c) 2005-2006 CodeSourcery.
7 cd1a3f68 ths
 *
8 cd1a3f68 ths
 * This code is licenced under the GPL.
9 cd1a3f68 ths
 */
10 cd1a3f68 ths
11 87ecb68b pbrook
#include "hw.h"
12 87ecb68b pbrook
#include "sh.h"
13 87ecb68b pbrook
#include "qemu-timer.h"
14 cd1a3f68 ths
15 cd1a3f68 ths
//#define DEBUG_TIMER
16 cd1a3f68 ths
17 cd1a3f68 ths
#define TIMER_TCR_TPSC          (7 << 0)
18 cd1a3f68 ths
#define TIMER_TCR_CKEG          (3 << 3)
19 cd1a3f68 ths
#define TIMER_TCR_UNIE          (1 << 5)
20 cd1a3f68 ths
#define TIMER_TCR_ICPE          (3 << 6)
21 cd1a3f68 ths
#define TIMER_TCR_UNF           (1 << 8)
22 cd1a3f68 ths
#define TIMER_TCR_ICPF          (1 << 9)
23 cd1a3f68 ths
#define TIMER_TCR_RESERVED      (0x3f << 10)
24 cd1a3f68 ths
25 cd1a3f68 ths
#define TIMER_FEAT_CAPT   (1 << 0)
26 cd1a3f68 ths
#define TIMER_FEAT_EXTCLK (1 << 1)
27 cd1a3f68 ths
28 e7786f27 aurel32
#define OFFSET_TCOR   0
29 e7786f27 aurel32
#define OFFSET_TCNT   1
30 e7786f27 aurel32
#define OFFSET_TCR    2
31 e7786f27 aurel32
#define OFFSET_TCPR   3
32 e7786f27 aurel32
33 cd1a3f68 ths
typedef struct {
34 cd1a3f68 ths
    ptimer_state *timer;
35 cd1a3f68 ths
    uint32_t tcnt;
36 cd1a3f68 ths
    uint32_t tcor;
37 cd1a3f68 ths
    uint32_t tcr;
38 cd1a3f68 ths
    uint32_t tcpr;
39 cd1a3f68 ths
    int freq;
40 cd1a3f68 ths
    int int_level;
41 703243a0 balrog
    int old_level;
42 cd1a3f68 ths
    int feat;
43 cd1a3f68 ths
    int enabled;
44 96e2fc41 aurel32
    qemu_irq irq;
45 cd1a3f68 ths
} sh_timer_state;
46 cd1a3f68 ths
47 cd1a3f68 ths
/* Check all active timers, and schedule the next timer interrupt. */
48 cd1a3f68 ths
49 cd1a3f68 ths
static void sh_timer_update(sh_timer_state *s)
50 cd1a3f68 ths
{
51 703243a0 balrog
    int new_level = s->int_level && (s->tcr & TIMER_TCR_UNIE);
52 703243a0 balrog
53 703243a0 balrog
    if (new_level != s->old_level)
54 96e2fc41 aurel32
      qemu_set_irq (s->irq, new_level);
55 703243a0 balrog
56 703243a0 balrog
    s->old_level = s->int_level;
57 703243a0 balrog
    s->int_level = new_level;
58 cd1a3f68 ths
}
59 cd1a3f68 ths
60 c227f099 Anthony Liguori
static uint32_t sh_timer_read(void *opaque, target_phys_addr_t offset)
61 cd1a3f68 ths
{
62 cd1a3f68 ths
    sh_timer_state *s = (sh_timer_state *)opaque;
63 cd1a3f68 ths
64 cd1a3f68 ths
    switch (offset >> 2) {
65 e7786f27 aurel32
    case OFFSET_TCOR:
66 cd1a3f68 ths
        return s->tcor;
67 e7786f27 aurel32
    case OFFSET_TCNT:
68 cd1a3f68 ths
        return ptimer_get_count(s->timer);
69 e7786f27 aurel32
    case OFFSET_TCR:
70 cd1a3f68 ths
        return s->tcr | (s->int_level ? TIMER_TCR_UNF : 0);
71 e7786f27 aurel32
    case OFFSET_TCPR:
72 cd1a3f68 ths
        if (s->feat & TIMER_FEAT_CAPT)
73 cd1a3f68 ths
            return s->tcpr;
74 cd1a3f68 ths
    default:
75 2ac71179 Paul Brook
        hw_error("sh_timer_read: Bad offset %x\n", (int)offset);
76 cd1a3f68 ths
        return 0;
77 cd1a3f68 ths
    }
78 cd1a3f68 ths
}
79 cd1a3f68 ths
80 c227f099 Anthony Liguori
static void sh_timer_write(void *opaque, target_phys_addr_t offset,
81 cd1a3f68 ths
                            uint32_t value)
82 cd1a3f68 ths
{
83 cd1a3f68 ths
    sh_timer_state *s = (sh_timer_state *)opaque;
84 cd1a3f68 ths
    int freq;
85 cd1a3f68 ths
86 cd1a3f68 ths
    switch (offset >> 2) {
87 e7786f27 aurel32
    case OFFSET_TCOR:
88 cd1a3f68 ths
        s->tcor = value;
89 cd1a3f68 ths
        ptimer_set_limit(s->timer, s->tcor, 0);
90 cd1a3f68 ths
        break;
91 e7786f27 aurel32
    case OFFSET_TCNT:
92 cd1a3f68 ths
        s->tcnt = value;
93 cd1a3f68 ths
        ptimer_set_count(s->timer, s->tcnt);
94 cd1a3f68 ths
        break;
95 e7786f27 aurel32
    case OFFSET_TCR:
96 cd1a3f68 ths
        if (s->enabled) {
97 cd1a3f68 ths
            /* Pause the timer if it is running.  This may cause some
98 cd1a3f68 ths
               inaccuracy dure to rounding, but avoids a whole lot of other
99 cd1a3f68 ths
               messyness.  */
100 cd1a3f68 ths
            ptimer_stop(s->timer);
101 cd1a3f68 ths
        }
102 cd1a3f68 ths
        freq = s->freq;
103 cd1a3f68 ths
        /* ??? Need to recalculate expiry time after changing divisor.  */
104 cd1a3f68 ths
        switch (value & TIMER_TCR_TPSC) {
105 cd1a3f68 ths
        case 0: freq >>= 2; break;
106 cd1a3f68 ths
        case 1: freq >>= 4; break;
107 cd1a3f68 ths
        case 2: freq >>= 6; break;
108 cd1a3f68 ths
        case 3: freq >>= 8; break;
109 cd1a3f68 ths
        case 4: freq >>= 10; break;
110 cd1a3f68 ths
        case 6:
111 cd1a3f68 ths
        case 7: if (s->feat & TIMER_FEAT_EXTCLK) break;
112 2ac71179 Paul Brook
        default: hw_error("sh_timer_write: Reserved TPSC value\n"); break;
113 cd1a3f68 ths
        }
114 cd1a3f68 ths
        switch ((value & TIMER_TCR_CKEG) >> 3) {
115 cd1a3f68 ths
        case 0: break;
116 cd1a3f68 ths
        case 1:
117 cd1a3f68 ths
        case 2:
118 cd1a3f68 ths
        case 3: if (s->feat & TIMER_FEAT_EXTCLK) break;
119 2ac71179 Paul Brook
        default: hw_error("sh_timer_write: Reserved CKEG value\n"); break;
120 cd1a3f68 ths
        }
121 cd1a3f68 ths
        switch ((value & TIMER_TCR_ICPE) >> 6) {
122 cd1a3f68 ths
        case 0: break;
123 cd1a3f68 ths
        case 2:
124 cd1a3f68 ths
        case 3: if (s->feat & TIMER_FEAT_CAPT) break;
125 2ac71179 Paul Brook
        default: hw_error("sh_timer_write: Reserved ICPE value\n"); break;
126 cd1a3f68 ths
        }
127 cd1a3f68 ths
        if ((value & TIMER_TCR_UNF) == 0)
128 cd1a3f68 ths
            s->int_level = 0;
129 cd1a3f68 ths
130 cd1a3f68 ths
        value &= ~TIMER_TCR_UNF;
131 cd1a3f68 ths
132 cd1a3f68 ths
        if ((value & TIMER_TCR_ICPF) && (!(s->feat & TIMER_FEAT_CAPT)))
133 2ac71179 Paul Brook
            hw_error("sh_timer_write: Reserved ICPF value\n");
134 cd1a3f68 ths
135 cd1a3f68 ths
        value &= ~TIMER_TCR_ICPF; /* capture not supported */
136 cd1a3f68 ths
137 cd1a3f68 ths
        if (value & TIMER_TCR_RESERVED)
138 2ac71179 Paul Brook
            hw_error("sh_timer_write: Reserved TCR bits set\n");
139 cd1a3f68 ths
        s->tcr = value;
140 cd1a3f68 ths
        ptimer_set_limit(s->timer, s->tcor, 0);
141 cd1a3f68 ths
        ptimer_set_freq(s->timer, freq);
142 cd1a3f68 ths
        if (s->enabled) {
143 cd1a3f68 ths
            /* Restart the timer if still enabled.  */
144 cd1a3f68 ths
            ptimer_run(s->timer, 0);
145 cd1a3f68 ths
        }
146 cd1a3f68 ths
        break;
147 e7786f27 aurel32
    case OFFSET_TCPR:
148 cd1a3f68 ths
        if (s->feat & TIMER_FEAT_CAPT) {
149 cd1a3f68 ths
            s->tcpr = value;
150 cd1a3f68 ths
            break;
151 cd1a3f68 ths
        }
152 cd1a3f68 ths
    default:
153 2ac71179 Paul Brook
        hw_error("sh_timer_write: Bad offset %x\n", (int)offset);
154 cd1a3f68 ths
    }
155 cd1a3f68 ths
    sh_timer_update(s);
156 cd1a3f68 ths
}
157 cd1a3f68 ths
158 cd1a3f68 ths
static void sh_timer_start_stop(void *opaque, int enable)
159 cd1a3f68 ths
{
160 cd1a3f68 ths
    sh_timer_state *s = (sh_timer_state *)opaque;
161 cd1a3f68 ths
162 cd1a3f68 ths
#ifdef DEBUG_TIMER
163 cd1a3f68 ths
    printf("sh_timer_start_stop %d (%d)\n", enable, s->enabled);
164 cd1a3f68 ths
#endif
165 cd1a3f68 ths
166 cd1a3f68 ths
    if (s->enabled && !enable) {
167 cd1a3f68 ths
        ptimer_stop(s->timer);
168 cd1a3f68 ths
    }
169 cd1a3f68 ths
    if (!s->enabled && enable) {
170 cd1a3f68 ths
        ptimer_run(s->timer, 0);
171 cd1a3f68 ths
    }
172 cd1a3f68 ths
    s->enabled = !!enable;
173 cd1a3f68 ths
174 cd1a3f68 ths
#ifdef DEBUG_TIMER
175 cd1a3f68 ths
    printf("sh_timer_start_stop done %d\n", s->enabled);
176 cd1a3f68 ths
#endif
177 cd1a3f68 ths
}
178 cd1a3f68 ths
179 cd1a3f68 ths
static void sh_timer_tick(void *opaque)
180 cd1a3f68 ths
{
181 cd1a3f68 ths
    sh_timer_state *s = (sh_timer_state *)opaque;
182 cd1a3f68 ths
    s->int_level = s->enabled;
183 cd1a3f68 ths
    sh_timer_update(s);
184 cd1a3f68 ths
}
185 cd1a3f68 ths
186 96e2fc41 aurel32
static void *sh_timer_init(uint32_t freq, int feat, qemu_irq irq)
187 cd1a3f68 ths
{
188 cd1a3f68 ths
    sh_timer_state *s;
189 cd1a3f68 ths
    QEMUBH *bh;
190 cd1a3f68 ths
191 cd1a3f68 ths
    s = (sh_timer_state *)qemu_mallocz(sizeof(sh_timer_state));
192 cd1a3f68 ths
    s->freq = freq;
193 cd1a3f68 ths
    s->feat = feat;
194 cd1a3f68 ths
    s->tcor = 0xffffffff;
195 cd1a3f68 ths
    s->tcnt = 0xffffffff;
196 cd1a3f68 ths
    s->tcpr = 0xdeadbeef;
197 e7786f27 aurel32
    s->tcr = 0;
198 cd1a3f68 ths
    s->enabled = 0;
199 703243a0 balrog
    s->irq = irq;
200 cd1a3f68 ths
201 cd1a3f68 ths
    bh = qemu_bh_new(sh_timer_tick, s);
202 cd1a3f68 ths
    s->timer = ptimer_init(bh);
203 e7786f27 aurel32
204 e7786f27 aurel32
    sh_timer_write(s, OFFSET_TCOR >> 2, s->tcor);
205 e7786f27 aurel32
    sh_timer_write(s, OFFSET_TCNT >> 2, s->tcnt);
206 e7786f27 aurel32
    sh_timer_write(s, OFFSET_TCPR >> 2, s->tcpr);
207 e7786f27 aurel32
    sh_timer_write(s, OFFSET_TCR  >> 2, s->tcpr);
208 cd1a3f68 ths
    /* ??? Save/restore.  */
209 cd1a3f68 ths
    return s;
210 cd1a3f68 ths
}
211 cd1a3f68 ths
212 cd1a3f68 ths
typedef struct {
213 cd1a3f68 ths
    void *timer[3];
214 cd1a3f68 ths
    int level[3];
215 cd1a3f68 ths
    uint32_t tocr;
216 cd1a3f68 ths
    uint32_t tstr;
217 cd1a3f68 ths
    int feat;
218 cd1a3f68 ths
} tmu012_state;
219 cd1a3f68 ths
220 c227f099 Anthony Liguori
static uint32_t tmu012_read(void *opaque, target_phys_addr_t offset)
221 cd1a3f68 ths
{
222 cd1a3f68 ths
    tmu012_state *s = (tmu012_state *)opaque;
223 cd1a3f68 ths
224 cd1a3f68 ths
#ifdef DEBUG_TIMER
225 cd1a3f68 ths
    printf("tmu012_read 0x%lx\n", (unsigned long) offset);
226 cd1a3f68 ths
#endif
227 cd1a3f68 ths
228 cd1a3f68 ths
    if (offset >= 0x20) {
229 cd1a3f68 ths
        if (!(s->feat & TMU012_FEAT_3CHAN))
230 2ac71179 Paul Brook
            hw_error("tmu012_write: Bad channel offset %x\n", (int)offset);
231 cd1a3f68 ths
        return sh_timer_read(s->timer[2], offset - 0x20);
232 cd1a3f68 ths
    }
233 cd1a3f68 ths
234 cd1a3f68 ths
    if (offset >= 0x14)
235 cd1a3f68 ths
        return sh_timer_read(s->timer[1], offset - 0x14);
236 cd1a3f68 ths
237 cd1a3f68 ths
    if (offset >= 0x08)
238 cd1a3f68 ths
        return sh_timer_read(s->timer[0], offset - 0x08);
239 cd1a3f68 ths
240 cd1a3f68 ths
    if (offset == 4)
241 cd1a3f68 ths
        return s->tstr;
242 cd1a3f68 ths
243 cd1a3f68 ths
    if ((s->feat & TMU012_FEAT_TOCR) && offset == 0)
244 cd1a3f68 ths
        return s->tocr;
245 cd1a3f68 ths
246 2ac71179 Paul Brook
    hw_error("tmu012_write: Bad offset %x\n", (int)offset);
247 cd1a3f68 ths
    return 0;
248 cd1a3f68 ths
}
249 cd1a3f68 ths
250 c227f099 Anthony Liguori
static void tmu012_write(void *opaque, target_phys_addr_t offset,
251 cd1a3f68 ths
                        uint32_t value)
252 cd1a3f68 ths
{
253 cd1a3f68 ths
    tmu012_state *s = (tmu012_state *)opaque;
254 cd1a3f68 ths
255 cd1a3f68 ths
#ifdef DEBUG_TIMER
256 cd1a3f68 ths
    printf("tmu012_write 0x%lx 0x%08x\n", (unsigned long) offset, value);
257 cd1a3f68 ths
#endif
258 cd1a3f68 ths
259 cd1a3f68 ths
    if (offset >= 0x20) {
260 cd1a3f68 ths
        if (!(s->feat & TMU012_FEAT_3CHAN))
261 2ac71179 Paul Brook
            hw_error("tmu012_write: Bad channel offset %x\n", (int)offset);
262 cd1a3f68 ths
        sh_timer_write(s->timer[2], offset - 0x20, value);
263 cd1a3f68 ths
        return;
264 cd1a3f68 ths
    }
265 cd1a3f68 ths
266 cd1a3f68 ths
    if (offset >= 0x14) {
267 cd1a3f68 ths
        sh_timer_write(s->timer[1], offset - 0x14, value);
268 cd1a3f68 ths
        return;
269 cd1a3f68 ths
    }
270 cd1a3f68 ths
271 cd1a3f68 ths
    if (offset >= 0x08) {
272 cd1a3f68 ths
        sh_timer_write(s->timer[0], offset - 0x08, value);
273 cd1a3f68 ths
        return;
274 cd1a3f68 ths
    }
275 cd1a3f68 ths
276 cd1a3f68 ths
    if (offset == 4) {
277 cd1a3f68 ths
        sh_timer_start_stop(s->timer[0], value & (1 << 0));
278 cd1a3f68 ths
        sh_timer_start_stop(s->timer[1], value & (1 << 1));
279 cd1a3f68 ths
        if (s->feat & TMU012_FEAT_3CHAN)
280 cd1a3f68 ths
            sh_timer_start_stop(s->timer[2], value & (1 << 2));
281 cd1a3f68 ths
        else
282 cd1a3f68 ths
            if (value & (1 << 2))
283 2ac71179 Paul Brook
                hw_error("tmu012_write: Bad channel\n");
284 cd1a3f68 ths
285 cd1a3f68 ths
        s->tstr = value;
286 cd1a3f68 ths
        return;
287 cd1a3f68 ths
    }
288 cd1a3f68 ths
289 cd1a3f68 ths
    if ((s->feat & TMU012_FEAT_TOCR) && offset == 0) {
290 cd1a3f68 ths
        s->tocr = value & (1 << 0);
291 cd1a3f68 ths
    }
292 cd1a3f68 ths
}
293 cd1a3f68 ths
294 d60efc6b Blue Swirl
static CPUReadMemoryFunc * const tmu012_readfn[] = {
295 cd1a3f68 ths
    tmu012_read,
296 cd1a3f68 ths
    tmu012_read,
297 cd1a3f68 ths
    tmu012_read
298 cd1a3f68 ths
};
299 cd1a3f68 ths
300 d60efc6b Blue Swirl
static CPUWriteMemoryFunc * const tmu012_writefn[] = {
301 cd1a3f68 ths
    tmu012_write,
302 cd1a3f68 ths
    tmu012_write,
303 cd1a3f68 ths
    tmu012_write
304 cd1a3f68 ths
};
305 cd1a3f68 ths
306 c227f099 Anthony Liguori
void tmu012_init(target_phys_addr_t base, int feat, uint32_t freq,
307 96e2fc41 aurel32
                 qemu_irq ch0_irq, qemu_irq ch1_irq,
308 96e2fc41 aurel32
                 qemu_irq ch2_irq0, qemu_irq ch2_irq1)
309 cd1a3f68 ths
{
310 cd1a3f68 ths
    int iomemtype;
311 cd1a3f68 ths
    tmu012_state *s;
312 cd1a3f68 ths
    int timer_feat = (feat & TMU012_FEAT_EXTCLK) ? TIMER_FEAT_EXTCLK : 0;
313 cd1a3f68 ths
314 cd1a3f68 ths
    s = (tmu012_state *)qemu_mallocz(sizeof(tmu012_state));
315 cd1a3f68 ths
    s->feat = feat;
316 703243a0 balrog
    s->timer[0] = sh_timer_init(freq, timer_feat, ch0_irq);
317 703243a0 balrog
    s->timer[1] = sh_timer_init(freq, timer_feat, ch1_irq);
318 cd1a3f68 ths
    if (feat & TMU012_FEAT_3CHAN)
319 703243a0 balrog
        s->timer[2] = sh_timer_init(freq, timer_feat | TIMER_FEAT_CAPT,
320 703243a0 balrog
                                    ch2_irq0); /* ch2_irq1 not supported */
321 1eed09cb Avi Kivity
    iomemtype = cpu_register_io_memory(tmu012_readfn,
322 2507c12a Alexander Graf
                                       tmu012_writefn, s,
323 2507c12a Alexander Graf
                                       DEVICE_NATIVE_ENDIAN);
324 5c16736a balrog
    cpu_register_physical_memory(P4ADDR(base), 0x00001000, iomemtype);
325 5c16736a balrog
    cpu_register_physical_memory(A7ADDR(base), 0x00001000, iomemtype);
326 cd1a3f68 ths
    /* ??? Save/restore.  */
327 cd1a3f68 ths
}