Statistics
| Branch: | Revision:

root / hw / etraxfs_timer.c @ 06adb549

History | View | Annotate | Download (8 kB)

1 83fa1010 ths
/*
2 e62b5b13 edgar_igl
 * QEMU ETRAX Timers
3 83fa1010 ths
 *
4 83fa1010 ths
 * Copyright (c) 2007 Edgar E. Iglesias, Axis Communications AB.
5 83fa1010 ths
 *
6 83fa1010 ths
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 83fa1010 ths
 * of this software and associated documentation files (the "Software"), to deal
8 83fa1010 ths
 * in the Software without restriction, including without limitation the rights
9 83fa1010 ths
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 83fa1010 ths
 * copies of the Software, and to permit persons to whom the Software is
11 83fa1010 ths
 * furnished to do so, subject to the following conditions:
12 83fa1010 ths
 *
13 83fa1010 ths
 * The above copyright notice and this permission notice shall be included in
14 83fa1010 ths
 * all copies or substantial portions of the Software.
15 83fa1010 ths
 *
16 83fa1010 ths
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 83fa1010 ths
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 83fa1010 ths
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 83fa1010 ths
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 83fa1010 ths
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 83fa1010 ths
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 83fa1010 ths
 * THE SOFTWARE.
23 83fa1010 ths
 */
24 83fa1010 ths
#include <stdio.h>
25 83fa1010 ths
#include <sys/time.h>
26 87ecb68b pbrook
#include "hw.h"
27 5439779e edgar_igl
#include "sysemu.h"
28 87ecb68b pbrook
#include "qemu-timer.h"
29 83fa1010 ths
30 bbaf29c7 edgar_igl
#define D(x)
31 bbaf29c7 edgar_igl
32 ca87d03b edgar_igl
#define RW_TMR0_DIV   0x00
33 ca87d03b edgar_igl
#define R_TMR0_DATA   0x04
34 ca87d03b edgar_igl
#define RW_TMR0_CTRL  0x08
35 ca87d03b edgar_igl
#define RW_TMR1_DIV   0x10
36 ca87d03b edgar_igl
#define R_TMR1_DATA   0x14
37 ca87d03b edgar_igl
#define RW_TMR1_CTRL  0x18
38 ca87d03b edgar_igl
#define R_TIME        0x38
39 ca87d03b edgar_igl
#define RW_WD_CTRL    0x40
40 5439779e edgar_igl
#define R_WD_STAT     0x44
41 ca87d03b edgar_igl
#define RW_INTR_MASK  0x48
42 ca87d03b edgar_igl
#define RW_ACK_INTR   0x4c
43 ca87d03b edgar_igl
#define R_INTR        0x50
44 ca87d03b edgar_igl
#define R_MASKED_INTR 0x54
45 83fa1010 ths
46 83fa1010 ths
struct fs_timer_t {
47 ca87d03b edgar_igl
        CPUState *env;
48 ca87d03b edgar_igl
        qemu_irq *irq;
49 5ef98b47 edgar_igl
        qemu_irq *nmi;
50 ca87d03b edgar_igl
        target_phys_addr_t base;
51 ca87d03b edgar_igl
52 5439779e edgar_igl
        QEMUBH *bh_t0;
53 5439779e edgar_igl
        QEMUBH *bh_t1;
54 5439779e edgar_igl
        QEMUBH *bh_wd;
55 5439779e edgar_igl
        ptimer_state *ptimer_t0;
56 5439779e edgar_igl
        ptimer_state *ptimer_t1;
57 5439779e edgar_igl
        ptimer_state *ptimer_wd;
58 bbaf29c7 edgar_igl
        struct timeval last;
59 e62b5b13 edgar_igl
60 5ef98b47 edgar_igl
        int wd_hits;
61 5ef98b47 edgar_igl
62 60237223 edgar_igl
        /* Control registers.  */
63 60237223 edgar_igl
        uint32_t rw_tmr0_div;
64 60237223 edgar_igl
        uint32_t r_tmr0_data;
65 60237223 edgar_igl
        uint32_t rw_tmr0_ctrl;
66 60237223 edgar_igl
67 60237223 edgar_igl
        uint32_t rw_tmr1_div;
68 60237223 edgar_igl
        uint32_t r_tmr1_data;
69 60237223 edgar_igl
        uint32_t rw_tmr1_ctrl;
70 60237223 edgar_igl
71 5439779e edgar_igl
        uint32_t rw_wd_ctrl;
72 5439779e edgar_igl
73 e62b5b13 edgar_igl
        uint32_t rw_intr_mask;
74 e62b5b13 edgar_igl
        uint32_t rw_ack_intr;
75 e62b5b13 edgar_igl
        uint32_t r_intr;
76 60237223 edgar_igl
        uint32_t r_masked_intr;
77 83fa1010 ths
};
78 83fa1010 ths
79 ca87d03b edgar_igl
static uint32_t timer_rinvalid (void *opaque, target_phys_addr_t addr)
80 83fa1010 ths
{
81 ca87d03b edgar_igl
        struct fs_timer_t *t = opaque;
82 ca87d03b edgar_igl
        CPUState *env = t->env;
83 d27b2e50 edgar_igl
        cpu_abort(env, "Unsupported short access. reg=" TARGET_FMT_plx "\n",
84 d27b2e50 edgar_igl
                  addr);
85 ca87d03b edgar_igl
        return 0;
86 83fa1010 ths
}
87 83fa1010 ths
88 83fa1010 ths
static uint32_t timer_readl (void *opaque, target_phys_addr_t addr)
89 83fa1010 ths
{
90 ca87d03b edgar_igl
        struct fs_timer_t *t = opaque;
91 83fa1010 ths
        uint32_t r = 0;
92 83fa1010 ths
93 ca87d03b edgar_igl
        /* Make addr relative to this instances base.  */
94 ca87d03b edgar_igl
        addr -= t->base;
95 83fa1010 ths
        switch (addr) {
96 83fa1010 ths
        case R_TMR0_DATA:
97 83fa1010 ths
                break;
98 83fa1010 ths
        case R_TMR1_DATA:
99 bbaf29c7 edgar_igl
                D(printf ("R_TMR1_DATA\n"));
100 83fa1010 ths
                break;
101 83fa1010 ths
        case R_TIME:
102 60237223 edgar_igl
                r = qemu_get_clock(vm_clock) * 10;
103 83fa1010 ths
                break;
104 83fa1010 ths
        case RW_INTR_MASK:
105 ca87d03b edgar_igl
                r = t->rw_intr_mask;
106 83fa1010 ths
                break;
107 83fa1010 ths
        case R_MASKED_INTR:
108 ca87d03b edgar_igl
                r = t->r_intr & t->rw_intr_mask;
109 83fa1010 ths
                break;
110 83fa1010 ths
        default:
111 d27b2e50 edgar_igl
                D(printf ("%s %x\n", __func__, addr));
112 83fa1010 ths
                break;
113 83fa1010 ths
        }
114 83fa1010 ths
        return r;
115 83fa1010 ths
}
116 83fa1010 ths
117 83fa1010 ths
static void
118 ca87d03b edgar_igl
timer_winvalid (void *opaque, target_phys_addr_t addr, uint32_t value)
119 83fa1010 ths
{
120 ca87d03b edgar_igl
        struct fs_timer_t *t = opaque;
121 ca87d03b edgar_igl
        CPUState *env = t->env;
122 d27b2e50 edgar_igl
        cpu_abort(env, "Unsupported short access. reg=" TARGET_FMT_plx "\n",
123 d27b2e50 edgar_igl
                  addr);
124 83fa1010 ths
}
125 83fa1010 ths
126 f0b86b14 edgar_igl
#define TIMER_SLOWDOWN 1
127 5439779e edgar_igl
static void update_ctrl(struct fs_timer_t *t, int tnum)
128 83fa1010 ths
{
129 60237223 edgar_igl
        unsigned int op;
130 60237223 edgar_igl
        unsigned int freq;
131 60237223 edgar_igl
        unsigned int freq_hz;
132 60237223 edgar_igl
        unsigned int div;
133 5439779e edgar_igl
        uint32_t ctrl;
134 5ef98b47 edgar_igl
135 5439779e edgar_igl
        ptimer_state *timer;
136 5439779e edgar_igl
137 5439779e edgar_igl
        if (tnum == 0) {
138 5439779e edgar_igl
                ctrl = t->rw_tmr0_ctrl;
139 5439779e edgar_igl
                div = t->rw_tmr0_div;
140 5439779e edgar_igl
                timer = t->ptimer_t0;
141 5439779e edgar_igl
        } else {
142 5439779e edgar_igl
                ctrl = t->rw_tmr1_ctrl;
143 5439779e edgar_igl
                div = t->rw_tmr1_div;
144 5439779e edgar_igl
                timer = t->ptimer_t1;
145 5439779e edgar_igl
        }
146 5439779e edgar_igl
147 83fa1010 ths
148 5439779e edgar_igl
        op = ctrl & 3;
149 5439779e edgar_igl
        freq = ctrl >> 2;
150 83fa1010 ths
        freq_hz = 32000000;
151 83fa1010 ths
152 83fa1010 ths
        switch (freq)
153 83fa1010 ths
        {
154 83fa1010 ths
        case 0:
155 83fa1010 ths
        case 1:
156 e62b5b13 edgar_igl
                D(printf ("extern or disabled timer clock?\n"));
157 83fa1010 ths
                break;
158 83fa1010 ths
        case 4: freq_hz =  29493000; break;
159 83fa1010 ths
        case 5: freq_hz =  32000000; break;
160 83fa1010 ths
        case 6: freq_hz =  32768000; break;
161 5439779e edgar_igl
        case 7: freq_hz = 100001000; break;
162 83fa1010 ths
        default:
163 83fa1010 ths
                abort();
164 83fa1010 ths
                break;
165 83fa1010 ths
        }
166 83fa1010 ths
167 5439779e edgar_igl
        D(printf ("freq_hz=%d div=%d\n", freq_hz, div));
168 5439779e edgar_igl
        div = div * TIMER_SLOWDOWN;
169 5ef98b47 edgar_igl
        div >>= 10;
170 5ef98b47 edgar_igl
        freq_hz >>= 10;
171 5439779e edgar_igl
        ptimer_set_freq(timer, freq_hz);
172 5439779e edgar_igl
        ptimer_set_limit(timer, div, 0);
173 83fa1010 ths
174 83fa1010 ths
        switch (op)
175 83fa1010 ths
        {
176 83fa1010 ths
                case 0:
177 60237223 edgar_igl
                        /* Load.  */
178 5439779e edgar_igl
                        ptimer_set_limit(timer, div, 1);
179 83fa1010 ths
                        break;
180 83fa1010 ths
                case 1:
181 60237223 edgar_igl
                        /* Hold.  */
182 5439779e edgar_igl
                        ptimer_stop(timer);
183 83fa1010 ths
                        break;
184 83fa1010 ths
                case 2:
185 60237223 edgar_igl
                        /* Run.  */
186 5439779e edgar_igl
                        ptimer_run(timer, 0);
187 83fa1010 ths
                        break;
188 83fa1010 ths
                default:
189 83fa1010 ths
                        abort();
190 83fa1010 ths
                        break;
191 83fa1010 ths
        }
192 83fa1010 ths
}
193 83fa1010 ths
194 60237223 edgar_igl
static void timer_update_irq(struct fs_timer_t *t)
195 83fa1010 ths
{
196 60237223 edgar_igl
        t->r_intr &= ~(t->rw_ack_intr);
197 60237223 edgar_igl
        t->r_masked_intr = t->r_intr & t->rw_intr_mask;
198 60237223 edgar_igl
199 60237223 edgar_igl
        D(printf("%s: masked_intr=%x\n", __func__, t->r_masked_intr));
200 eb173de6 edgar_igl
        if (t->r_masked_intr)
201 60237223 edgar_igl
                qemu_irq_raise(t->irq[0]);
202 60237223 edgar_igl
        else
203 bbaf29c7 edgar_igl
                qemu_irq_lower(t->irq[0]);
204 83fa1010 ths
}
205 83fa1010 ths
206 5439779e edgar_igl
static void timer0_hit(void *opaque)
207 60237223 edgar_igl
{
208 63c1d925 edgar_igl
        struct fs_timer_t *t = opaque;
209 60237223 edgar_igl
        t->r_intr |= 1;
210 60237223 edgar_igl
        timer_update_irq(t);
211 60237223 edgar_igl
}
212 60237223 edgar_igl
213 5439779e edgar_igl
static void timer1_hit(void *opaque)
214 5439779e edgar_igl
{
215 5439779e edgar_igl
        struct fs_timer_t *t = opaque;
216 5439779e edgar_igl
        t->r_intr |= 2;
217 5439779e edgar_igl
        timer_update_irq(t);
218 5439779e edgar_igl
}
219 5439779e edgar_igl
220 5439779e edgar_igl
static void watchdog_hit(void *opaque)
221 5439779e edgar_igl
{
222 5ef98b47 edgar_igl
        struct fs_timer_t *t = opaque;
223 5ef98b47 edgar_igl
        if (t->wd_hits == 0) {
224 5ef98b47 edgar_igl
                /* real hw gives a single tick before reseting but we are
225 5ef98b47 edgar_igl
                   a bit friendlier to compensate for our slower execution.  */
226 5ef98b47 edgar_igl
                ptimer_set_count(t->ptimer_wd, 10);
227 5ef98b47 edgar_igl
                ptimer_run(t->ptimer_wd, 1);
228 5ef98b47 edgar_igl
                qemu_irq_raise(t->nmi[0]);
229 5ef98b47 edgar_igl
        }
230 5ef98b47 edgar_igl
        else
231 5ef98b47 edgar_igl
                qemu_system_reset_request();
232 5ef98b47 edgar_igl
233 5ef98b47 edgar_igl
        t->wd_hits++;
234 5439779e edgar_igl
}
235 5439779e edgar_igl
236 5439779e edgar_igl
static inline void timer_watchdog_update(struct fs_timer_t *t, uint32_t value)
237 5439779e edgar_igl
{
238 5439779e edgar_igl
        unsigned int wd_en = t->rw_wd_ctrl & (1 << 8);
239 5439779e edgar_igl
        unsigned int wd_key = t->rw_wd_ctrl >> 9;
240 5439779e edgar_igl
        unsigned int wd_cnt = t->rw_wd_ctrl & 511;
241 5439779e edgar_igl
        unsigned int new_key = value >> 9 & ((1 << 7) - 1);
242 5439779e edgar_igl
        unsigned int new_cmd = (value >> 8) & 1;
243 5439779e edgar_igl
244 5439779e edgar_igl
        /* If the watchdog is enabled, they written key must match the
245 5439779e edgar_igl
           complement of the previous.  */
246 5439779e edgar_igl
        wd_key = ~wd_key & ((1 << 7) - 1);
247 5439779e edgar_igl
248 5439779e edgar_igl
        if (wd_en && wd_key != new_key)
249 5439779e edgar_igl
                return;
250 5439779e edgar_igl
251 5439779e edgar_igl
        D(printf("en=%d new_key=%x oldkey=%x cmd=%d cnt=%d\n", 
252 96768ff7 edgar_igl
                 wd_en, new_key, wd_key, new_cmd, wd_cnt));
253 5439779e edgar_igl
254 5ef98b47 edgar_igl
        if (t->wd_hits)
255 5ef98b47 edgar_igl
                qemu_irq_lower(t->nmi[0]);
256 5ef98b47 edgar_igl
257 5ef98b47 edgar_igl
        t->wd_hits = 0;
258 5ef98b47 edgar_igl
259 5439779e edgar_igl
        ptimer_set_freq(t->ptimer_wd, 760);
260 5439779e edgar_igl
        if (wd_cnt == 0)
261 5439779e edgar_igl
                wd_cnt = 256;
262 5439779e edgar_igl
        ptimer_set_count(t->ptimer_wd, wd_cnt);
263 5439779e edgar_igl
        if (new_cmd)
264 5439779e edgar_igl
                ptimer_run(t->ptimer_wd, 1);
265 5439779e edgar_igl
        else
266 5439779e edgar_igl
                ptimer_stop(t->ptimer_wd);
267 5439779e edgar_igl
268 5439779e edgar_igl
        t->rw_wd_ctrl = value;
269 5439779e edgar_igl
}
270 5439779e edgar_igl
271 83fa1010 ths
static void
272 83fa1010 ths
timer_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
273 83fa1010 ths
{
274 ca87d03b edgar_igl
        struct fs_timer_t *t = opaque;
275 bbaf29c7 edgar_igl
276 ca87d03b edgar_igl
        /* Make addr relative to this instances base.  */
277 ca87d03b edgar_igl
        addr -= t->base;
278 83fa1010 ths
        switch (addr)
279 83fa1010 ths
        {
280 83fa1010 ths
                case RW_TMR0_DIV:
281 60237223 edgar_igl
                        t->rw_tmr0_div = value;
282 83fa1010 ths
                        break;
283 83fa1010 ths
                case RW_TMR0_CTRL:
284 bbaf29c7 edgar_igl
                        D(printf ("RW_TMR0_CTRL=%x\n", value));
285 60237223 edgar_igl
                        t->rw_tmr0_ctrl = value;
286 5439779e edgar_igl
                        update_ctrl(t, 0);
287 83fa1010 ths
                        break;
288 83fa1010 ths
                case RW_TMR1_DIV:
289 60237223 edgar_igl
                        t->rw_tmr1_div = value;
290 83fa1010 ths
                        break;
291 83fa1010 ths
                case RW_TMR1_CTRL:
292 bbaf29c7 edgar_igl
                        D(printf ("RW_TMR1_CTRL=%x\n", value));
293 5439779e edgar_igl
                        t->rw_tmr1_ctrl = value;
294 5439779e edgar_igl
                        update_ctrl(t, 1);
295 83fa1010 ths
                        break;
296 83fa1010 ths
                case RW_INTR_MASK:
297 bbaf29c7 edgar_igl
                        D(printf ("RW_INTR_MASK=%x\n", value));
298 ca87d03b edgar_igl
                        t->rw_intr_mask = value;
299 60237223 edgar_igl
                        timer_update_irq(t);
300 e62b5b13 edgar_igl
                        break;
301 e62b5b13 edgar_igl
                case RW_WD_CTRL:
302 5439779e edgar_igl
                        timer_watchdog_update(t, value);
303 83fa1010 ths
                        break;
304 83fa1010 ths
                case RW_ACK_INTR:
305 60237223 edgar_igl
                        t->rw_ack_intr = value;
306 60237223 edgar_igl
                        timer_update_irq(t);
307 60237223 edgar_igl
                        t->rw_ack_intr = 0;
308 83fa1010 ths
                        break;
309 83fa1010 ths
                default:
310 d27b2e50 edgar_igl
                        printf ("%s " TARGET_FMT_plx " %x\n",
311 d27b2e50 edgar_igl
                                __func__, addr, value);
312 83fa1010 ths
                        break;
313 83fa1010 ths
        }
314 83fa1010 ths
}
315 83fa1010 ths
316 83fa1010 ths
static CPUReadMemoryFunc *timer_read[] = {
317 5439779e edgar_igl
        &timer_rinvalid,
318 5439779e edgar_igl
        &timer_rinvalid,
319 5439779e edgar_igl
        &timer_readl,
320 83fa1010 ths
};
321 83fa1010 ths
322 83fa1010 ths
static CPUWriteMemoryFunc *timer_write[] = {
323 5439779e edgar_igl
        &timer_winvalid,
324 5439779e edgar_igl
        &timer_winvalid,
325 5439779e edgar_igl
        &timer_writel,
326 83fa1010 ths
};
327 83fa1010 ths
328 5439779e edgar_igl
static void etraxfs_timer_reset(void *opaque)
329 5439779e edgar_igl
{
330 5439779e edgar_igl
        struct fs_timer_t *t = opaque;
331 5439779e edgar_igl
332 5439779e edgar_igl
        ptimer_stop(t->ptimer_t0);
333 5439779e edgar_igl
        ptimer_stop(t->ptimer_t1);
334 5439779e edgar_igl
        ptimer_stop(t->ptimer_wd);
335 5439779e edgar_igl
        t->rw_wd_ctrl = 0;
336 5439779e edgar_igl
        t->r_intr = 0;
337 5439779e edgar_igl
        t->rw_intr_mask = 0;
338 5439779e edgar_igl
        qemu_irq_lower(t->irq[0]);
339 5439779e edgar_igl
}
340 5439779e edgar_igl
341 5ef98b47 edgar_igl
void etraxfs_timer_init(CPUState *env, qemu_irq *irqs, qemu_irq *nmi,
342 ca87d03b edgar_igl
                        target_phys_addr_t base)
343 83fa1010 ths
{
344 ca87d03b edgar_igl
        static struct fs_timer_t *t;
345 83fa1010 ths
        int timer_regs;
346 83fa1010 ths
347 ca87d03b edgar_igl
        t = qemu_mallocz(sizeof *t);
348 ca87d03b edgar_igl
        if (!t)
349 ca87d03b edgar_igl
                return;
350 bbaf29c7 edgar_igl
351 5439779e edgar_igl
        t->bh_t0 = qemu_bh_new(timer0_hit, t);
352 5439779e edgar_igl
        t->bh_t1 = qemu_bh_new(timer1_hit, t);
353 5439779e edgar_igl
        t->bh_wd = qemu_bh_new(watchdog_hit, t);
354 5439779e edgar_igl
        t->ptimer_t0 = ptimer_init(t->bh_t0);
355 5439779e edgar_igl
        t->ptimer_t1 = ptimer_init(t->bh_t1);
356 5439779e edgar_igl
        t->ptimer_wd = ptimer_init(t->bh_wd);
357 60237223 edgar_igl
        t->irq = irqs;
358 5ef98b47 edgar_igl
        t->nmi = nmi;
359 ca87d03b edgar_igl
        t->env = env;
360 ca87d03b edgar_igl
        t->base = base;
361 83fa1010 ths
362 ca87d03b edgar_igl
        timer_regs = cpu_register_io_memory(0, timer_read, timer_write, t);
363 ca87d03b edgar_igl
        cpu_register_physical_memory (base, 0x5c, timer_regs);
364 5439779e edgar_igl
365 5439779e edgar_igl
        qemu_register_reset(etraxfs_timer_reset, t);
366 83fa1010 ths
}