etraxfs_timer.c 8.8 KB
Newer Older
T
ths 已提交
1
/*
2
 * QEMU ETRAX Timers
T
ths 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * Copyright (c) 2007 Edgar E. Iglesias, Axis Communications AB.
 *
 * 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.
 */
24
#include "hw/sysbus.h"
25
#include "sysemu/sysemu.h"
26
#include "qemu/timer.h"
27
#include "hw/ptimer.h"
T
ths 已提交
28

29 30
#define D(x)

31 32 33 34 35 36 37 38
#define RW_TMR0_DIV   0x00
#define R_TMR0_DATA   0x04
#define RW_TMR0_CTRL  0x08
#define RW_TMR1_DIV   0x10
#define R_TMR1_DATA   0x14
#define RW_TMR1_CTRL  0x18
#define R_TIME        0x38
#define RW_WD_CTRL    0x40
39
#define R_WD_STAT     0x44
40 41 42 43
#define RW_INTR_MASK  0x48
#define RW_ACK_INTR   0x4c
#define R_INTR        0x50
#define R_MASKED_INTR 0x54
T
ths 已提交
44

E
Edgar E. Iglesias 已提交
45 46
struct etrax_timer {
    SysBusDevice busdev;
47
    MemoryRegion mmio;
E
Edgar E. Iglesias 已提交
48 49
    qemu_irq irq;
    qemu_irq nmi;
E
Edgar E. Iglesias 已提交
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

    QEMUBH *bh_t0;
    QEMUBH *bh_t1;
    QEMUBH *bh_wd;
    ptimer_state *ptimer_t0;
    ptimer_state *ptimer_t1;
    ptimer_state *ptimer_wd;

    int wd_hits;

    /* Control registers.  */
    uint32_t rw_tmr0_div;
    uint32_t r_tmr0_data;
    uint32_t rw_tmr0_ctrl;

    uint32_t rw_tmr1_div;
    uint32_t r_tmr1_data;
    uint32_t rw_tmr1_ctrl;

    uint32_t rw_wd_ctrl;

    uint32_t rw_intr_mask;
    uint32_t rw_ack_intr;
    uint32_t r_intr;
    uint32_t r_masked_intr;
T
ths 已提交
75 76
};

77
static uint64_t
A
Avi Kivity 已提交
78
timer_read(void *opaque, hwaddr addr, unsigned int size)
T
ths 已提交
79
{
E
Edgar E. Iglesias 已提交
80
    struct etrax_timer *t = opaque;
E
Edgar E. Iglesias 已提交
81 82 83 84 85 86 87 88 89 90
    uint32_t r = 0;

    switch (addr) {
    case R_TMR0_DATA:
        r = ptimer_get_count(t->ptimer_t0);
        break;
    case R_TMR1_DATA:
        r = ptimer_get_count(t->ptimer_t1);
        break;
    case R_TIME:
91
        r = qemu_get_clock_ns(vm_clock) / 10;
E
Edgar E. Iglesias 已提交
92 93 94 95 96 97 98 99 100 101 102 103
        break;
    case RW_INTR_MASK:
        r = t->rw_intr_mask;
        break;
    case R_MASKED_INTR:
        r = t->r_intr & t->rw_intr_mask;
        break;
    default:
        D(printf ("%s %x\n", __func__, addr));
        break;
    }
    return r;
T
ths 已提交
104 105
}

E
Edgar E. Iglesias 已提交
106
static void update_ctrl(struct etrax_timer *t, int tnum)
T
ths 已提交
107
{
E
Edgar E. Iglesias 已提交
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
    unsigned int op;
    unsigned int freq;
    unsigned int freq_hz;
    unsigned int div;
    uint32_t ctrl;

    ptimer_state *timer;

    if (tnum == 0) {
        ctrl = t->rw_tmr0_ctrl;
        div = t->rw_tmr0_div;
        timer = t->ptimer_t0;
    } else {
        ctrl = t->rw_tmr1_ctrl;
        div = t->rw_tmr1_div;
        timer = t->ptimer_t1;
    }


    op = ctrl & 3;
    freq = ctrl >> 2;
    freq_hz = 32000000;

    switch (freq)
    {
    case 0:
    case 1:
        D(printf ("extern or disabled timer clock?\n"));
        break;
    case 4: freq_hz =  29493000; break;
    case 5: freq_hz =  32000000; break;
    case 6: freq_hz =  32768000; break;
    case 7: freq_hz = 100000000; break;
    default:
        abort();
        break;
    }

    D(printf ("freq_hz=%d div=%d\n", freq_hz, div));
    ptimer_set_freq(timer, freq_hz);
    ptimer_set_limit(timer, div, 0);

    switch (op)
    {
        case 0:
            /* Load.  */
            ptimer_set_limit(timer, div, 1);
            break;
        case 1:
            /* Hold.  */
            ptimer_stop(timer);
            break;
        case 2:
            /* Run.  */
            ptimer_run(timer, 0);
            break;
        default:
            abort();
            break;
    }
T
ths 已提交
168 169
}

E
Edgar E. Iglesias 已提交
170
static void timer_update_irq(struct etrax_timer *t)
T
ths 已提交
171
{
E
Edgar E. Iglesias 已提交
172 173
    t->r_intr &= ~(t->rw_ack_intr);
    t->r_masked_intr = t->r_intr & t->rw_intr_mask;
174

E
Edgar E. Iglesias 已提交
175
    D(printf("%s: masked_intr=%x\n", __func__, t->r_masked_intr));
E
Edgar E. Iglesias 已提交
176
    qemu_set_irq(t->irq, !!t->r_masked_intr);
T
ths 已提交
177 178
}

179
static void timer0_hit(void *opaque)
180
{
E
Edgar E. Iglesias 已提交
181
    struct etrax_timer *t = opaque;
E
Edgar E. Iglesias 已提交
182 183
    t->r_intr |= 1;
    timer_update_irq(t);
184 185
}

186 187
static void timer1_hit(void *opaque)
{
E
Edgar E. Iglesias 已提交
188
    struct etrax_timer *t = opaque;
E
Edgar E. Iglesias 已提交
189 190
    t->r_intr |= 2;
    timer_update_irq(t);
191 192 193 194
}

static void watchdog_hit(void *opaque)
{
E
Edgar E. Iglesias 已提交
195
    struct etrax_timer *t = opaque;
E
Edgar E. Iglesias 已提交
196 197 198 199 200
    if (t->wd_hits == 0) {
        /* real hw gives a single tick before reseting but we are
           a bit friendlier to compensate for our slower execution.  */
        ptimer_set_count(t->ptimer_wd, 10);
        ptimer_run(t->ptimer_wd, 1);
E
Edgar E. Iglesias 已提交
201
        qemu_irq_raise(t->nmi);
E
Edgar E. Iglesias 已提交
202 203 204 205 206
    }
    else
        qemu_system_reset_request();

    t->wd_hits++;
207 208
}

E
Edgar E. Iglesias 已提交
209
static inline void timer_watchdog_update(struct etrax_timer *t, uint32_t value)
210
{
E
Edgar E. Iglesias 已提交
211 212 213 214 215
    unsigned int wd_en = t->rw_wd_ctrl & (1 << 8);
    unsigned int wd_key = t->rw_wd_ctrl >> 9;
    unsigned int wd_cnt = t->rw_wd_ctrl & 511;
    unsigned int new_key = value >> 9 & ((1 << 7) - 1);
    unsigned int new_cmd = (value >> 8) & 1;
216

E
Edgar E. Iglesias 已提交
217 218 219
    /* If the watchdog is enabled, they written key must match the
       complement of the previous.  */
    wd_key = ~wd_key & ((1 << 7) - 1);
220

E
Edgar E. Iglesias 已提交
221 222
    if (wd_en && wd_key != new_key)
        return;
223

E
Edgar E. Iglesias 已提交
224 225
    D(printf("en=%d new_key=%x oldkey=%x cmd=%d cnt=%d\n", 
         wd_en, new_key, wd_key, new_cmd, wd_cnt));
226

E
Edgar E. Iglesias 已提交
227
    if (t->wd_hits)
E
Edgar E. Iglesias 已提交
228
        qemu_irq_lower(t->nmi);
229

E
Edgar E. Iglesias 已提交
230
    t->wd_hits = 0;
231

E
Edgar E. Iglesias 已提交
232 233 234 235 236 237 238 239
    ptimer_set_freq(t->ptimer_wd, 760);
    if (wd_cnt == 0)
        wd_cnt = 256;
    ptimer_set_count(t->ptimer_wd, wd_cnt);
    if (new_cmd)
        ptimer_run(t->ptimer_wd, 1);
    else
        ptimer_stop(t->ptimer_wd);
240

E
Edgar E. Iglesias 已提交
241
    t->rw_wd_ctrl = value;
242 243
}

T
ths 已提交
244
static void
A
Avi Kivity 已提交
245
timer_write(void *opaque, hwaddr addr,
246
            uint64_t val64, unsigned int size)
T
ths 已提交
247
{
E
Edgar E. Iglesias 已提交
248
    struct etrax_timer *t = opaque;
249
    uint32_t value = val64;
E
Edgar E. Iglesias 已提交
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

    switch (addr)
    {
        case RW_TMR0_DIV:
            t->rw_tmr0_div = value;
            break;
        case RW_TMR0_CTRL:
            D(printf ("RW_TMR0_CTRL=%x\n", value));
            t->rw_tmr0_ctrl = value;
            update_ctrl(t, 0);
            break;
        case RW_TMR1_DIV:
            t->rw_tmr1_div = value;
            break;
        case RW_TMR1_CTRL:
            D(printf ("RW_TMR1_CTRL=%x\n", value));
            t->rw_tmr1_ctrl = value;
            update_ctrl(t, 1);
            break;
        case RW_INTR_MASK:
            D(printf ("RW_INTR_MASK=%x\n", value));
            t->rw_intr_mask = value;
            timer_update_irq(t);
            break;
        case RW_WD_CTRL:
            timer_watchdog_update(t, value);
            break;
        case RW_ACK_INTR:
            t->rw_ack_intr = value;
            timer_update_irq(t);
            t->rw_ack_intr = 0;
            break;
        default:
            printf ("%s " TARGET_FMT_plx " %x\n",
                __func__, addr, value);
            break;
    }
T
ths 已提交
287 288
}

289 290 291 292 293 294 295 296
static const MemoryRegionOps timer_ops = {
    .read = timer_read,
    .write = timer_write,
    .endianness = DEVICE_LITTLE_ENDIAN,
    .valid = {
        .min_access_size = 4,
        .max_access_size = 4
    }
T
ths 已提交
297 298
};

299 300
static void etraxfs_timer_reset(void *opaque)
{
E
Edgar E. Iglesias 已提交
301
    struct etrax_timer *t = opaque;
E
Edgar E. Iglesias 已提交
302 303 304 305 306 307 308

    ptimer_stop(t->ptimer_t0);
    ptimer_stop(t->ptimer_t1);
    ptimer_stop(t->ptimer_wd);
    t->rw_wd_ctrl = 0;
    t->r_intr = 0;
    t->rw_intr_mask = 0;
E
Edgar E. Iglesias 已提交
309
    qemu_irq_lower(t->irq);
310 311
}

312
static int etraxfs_timer_init(SysBusDevice *dev)
T
ths 已提交
313
{
E
Edgar E. Iglesias 已提交
314
    struct etrax_timer *t = FROM_SYSBUS(typeof (*t), dev);
T
ths 已提交
315

E
Edgar E. Iglesias 已提交
316 317 318 319 320 321
    t->bh_t0 = qemu_bh_new(timer0_hit, t);
    t->bh_t1 = qemu_bh_new(timer1_hit, t);
    t->bh_wd = qemu_bh_new(watchdog_hit, t);
    t->ptimer_t0 = ptimer_init(t->bh_t0);
    t->ptimer_t1 = ptimer_init(t->bh_t1);
    t->ptimer_wd = ptimer_init(t->bh_wd);
E
Edgar E. Iglesias 已提交
322 323 324

    sysbus_init_irq(dev, &t->irq);
    sysbus_init_irq(dev, &t->nmi);
T
ths 已提交
325

326 327
    memory_region_init_io(&t->mmio, OBJECT(t), &timer_ops, t,
                          "etraxfs-timer", 0x5c);
328
    sysbus_init_mmio(dev, &t->mmio);
329
    qemu_register_reset(etraxfs_timer_reset, t);
330
    return 0;
T
ths 已提交
331
}
E
Edgar E. Iglesias 已提交
332

333 334 335 336 337 338 339
static void etraxfs_timer_class_init(ObjectClass *klass, void *data)
{
    SysBusDeviceClass *sdc = SYS_BUS_DEVICE_CLASS(klass);

    sdc->init = etraxfs_timer_init;
}

340
static const TypeInfo etraxfs_timer_info = {
341 342 343 344
    .name          = "etraxfs,timer",
    .parent        = TYPE_SYS_BUS_DEVICE,
    .instance_size = sizeof (struct etrax_timer),
    .class_init    = etraxfs_timer_class_init,
345 346
};

A
Andreas Färber 已提交
347
static void etraxfs_timer_register_types(void)
E
Edgar E. Iglesias 已提交
348
{
349
    type_register_static(&etraxfs_timer_info);
E
Edgar E. Iglesias 已提交
350 351
}

A
Andreas Färber 已提交
352
type_init(etraxfs_timer_register_types)