imx_gpt.c 13.3 KB
Newer Older
P
Peter Chubb 已提交
1
/*
2
 * IMX GPT Timer
P
Peter Chubb 已提交
3 4 5
 *
 * Copyright (c) 2008 OK Labs
 * Copyright (c) 2011 NICTA Pty Ltd
6
 * Originally written by Hans Jiang
P
Peter Chubb 已提交
7
 * Updated by Peter Chubb
8
 * Updated by Jean-Christophe Dubois <jcd@tribudubois.net>
P
Peter Chubb 已提交
9
 *
10
 * This code is licensed under GPL version 2 or later.  See
P
Peter Chubb 已提交
11 12 13 14
 * the COPYING file in the top-level directory.
 *
 */

P
Peter Maydell 已提交
15
#include "qemu/osdep.h"
16
#include "hw/timer/imx_gpt.h"
17
#include "qemu/main-loop.h"
18
#include "qemu/log.h"
P
Peter Chubb 已提交
19

20 21 22 23 24 25 26 27 28 29 30
#ifndef DEBUG_IMX_GPT
#define DEBUG_IMX_GPT 0
#endif

#define DPRINTF(fmt, args...) \
    do { \
        if (DEBUG_IMX_GPT) { \
            fprintf(stderr, "[%s]%s: " fmt , TYPE_IMX_GPT, \
                                             __func__, ##args); \
        } \
    } while (0)
31

32
static const char *imx_gpt_reg_name(uint32_t reg)
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
{
    switch (reg) {
    case 0:
        return "CR";
    case 1:
        return "PR";
    case 2:
        return "SR";
    case 3:
        return "IR";
    case 4:
        return "OCR1";
    case 5:
        return "OCR2";
    case 6:
        return "OCR3";
    case 7:
        return "ICR1";
    case 8:
        return "ICR2";
    case 9:
        return "CNT";
    default:
        return "[?]";
    }
}

60
static const VMStateDescription vmstate_imx_timer_gpt = {
61
    .name = TYPE_IMX_GPT,
62 63
    .version_id = 3,
    .minimum_version_id = 3,
64
    .fields = (VMStateField[]) {
65 66 67 68 69 70 71 72 73 74 75 76 77 78
        VMSTATE_UINT32(cr, IMXGPTState),
        VMSTATE_UINT32(pr, IMXGPTState),
        VMSTATE_UINT32(sr, IMXGPTState),
        VMSTATE_UINT32(ir, IMXGPTState),
        VMSTATE_UINT32(ocr1, IMXGPTState),
        VMSTATE_UINT32(ocr2, IMXGPTState),
        VMSTATE_UINT32(ocr3, IMXGPTState),
        VMSTATE_UINT32(icr1, IMXGPTState),
        VMSTATE_UINT32(icr2, IMXGPTState),
        VMSTATE_UINT32(cnt, IMXGPTState),
        VMSTATE_UINT32(next_timeout, IMXGPTState),
        VMSTATE_UINT32(next_int, IMXGPTState),
        VMSTATE_UINT32(freq, IMXGPTState),
        VMSTATE_PTIMER(timer, IMXGPTState),
P
Peter Chubb 已提交
79 80 81 82
        VMSTATE_END_OF_LIST()
    }
};

83 84 85 86 87 88 89 90 91 92 93 94
static const IMXClk imx25_gpt_clocks[] = {
    CLK_NONE,      /* 000 No clock source */
    CLK_IPG,       /* 001 ipg_clk, 532MHz*/
    CLK_IPG_HIGH,  /* 010 ipg_clk_highfreq */
    CLK_NONE,      /* 011 not defined */
    CLK_32k,       /* 100 ipg_clk_32k */
    CLK_32k,       /* 101 ipg_clk_32k */
    CLK_32k,       /* 110 ipg_clk_32k */
    CLK_32k,       /* 111 ipg_clk_32k */
};

static const IMXClk imx31_gpt_clocks[] = {
95 96 97 98 99 100 101 102
    CLK_NONE,      /* 000 No clock source */
    CLK_IPG,       /* 001 ipg_clk, 532MHz*/
    CLK_IPG_HIGH,  /* 010 ipg_clk_highfreq */
    CLK_NONE,      /* 011 not defined */
    CLK_32k,       /* 100 ipg_clk_32k */
    CLK_NONE,      /* 101 not defined */
    CLK_NONE,      /* 110 not defined */
    CLK_NONE,      /* 111 not defined */
P
Peter Chubb 已提交
103 104
};

105 106 107 108 109 110 111 112 113 114 115
static const IMXClk imx6_gpt_clocks[] = {
    CLK_NONE,      /* 000 No clock source */
    CLK_IPG,       /* 001 ipg_clk, 532MHz*/
    CLK_IPG_HIGH,  /* 010 ipg_clk_highfreq */
    CLK_EXT,       /* 011 External clock */
    CLK_32k,       /* 100 ipg_clk_32k */
    CLK_HIGH_DIV,  /* 101 reference clock / 8 */
    CLK_NONE,      /* 110 not defined */
    CLK_HIGH,      /* 111 reference clock */
};

116
static void imx_gpt_set_freq(IMXGPTState *s)
P
Peter Chubb 已提交
117
{
118
    uint32_t clksrc = extract32(s->cr, GPT_CR_CLKSRC_SHIFT, 3);
P
Peter Chubb 已提交
119

120
    s->freq = imx_ccm_get_clock_frequency(s->ccm,
121
                                          s->clocks[clksrc]) / (1 + s->pr);
122

123 124 125 126
    DPRINTF("Setting clksrc %d to frequency %d\n", clksrc, s->freq);

    if (s->freq) {
        ptimer_set_freq(s->timer, s->freq);
P
Peter Chubb 已提交
127 128 129
    }
}

130
static void imx_gpt_update_int(IMXGPTState *s)
P
Peter Chubb 已提交
131
{
132 133 134 135 136
    if ((s->sr & s->ir) && (s->cr & GPT_CR_EN)) {
        qemu_irq_raise(s->irq);
    } else {
        qemu_irq_lower(s->irq);
    }
P
Peter Chubb 已提交
137 138
}

139
static uint32_t imx_gpt_update_count(IMXGPTState *s)
P
Peter Chubb 已提交
140
{
141 142
    s->cnt = s->next_timeout - (uint32_t)ptimer_get_count(s->timer);

P
Peter Chubb 已提交
143 144 145
    return s->cnt;
}

146
static inline uint32_t imx_gpt_find_limit(uint32_t count, uint32_t reg,
147
                                          uint32_t timeout)
P
Peter Chubb 已提交
148
{
149 150 151 152 153 154
    if ((count < reg) && (timeout > reg)) {
        timeout = reg;
    }

    return timeout;
}
P
Peter Chubb 已提交
155

156
static void imx_gpt_compute_next_timeout(IMXGPTState *s, bool event)
157
{
158
    uint32_t timeout = GPT_TIMER_MAX;
159
    uint32_t count;
160 161 162 163
    long long limit;

    if (!(s->cr & GPT_CR_EN)) {
        /* if not enabled just return */
P
Peter Chubb 已提交
164 165 166
        return;
    }

167 168
    /* update the count */
    count = imx_gpt_update_count(s);
169

170 171 172 173 174 175 176 177
    if (event) {
        /*
         * This is an event (the ptimer reached 0 and stopped), and the
         * timer counter is now equal to s->next_timeout.
         */
        if (!(s->cr & GPT_CR_FRR) && (count == s->ocr1)) {
            /* We are in restart mode and we crossed the compare channel 1
             * value. We need to reset the counter to 0.
178
             */
179 180 181 182
            count = s->cnt = s->next_timeout = 0;
        } else if (count == GPT_TIMER_MAX) {
            /* We reached GPT_TIMER_MAX so we need to rollover */
            count = s->cnt = s->next_timeout = 0;
183 184 185 186 187 188
        }
    }

    /* now, find the next timeout related to count */

    if (s->ir & GPT_IR_OF1IE) {
189
        timeout = imx_gpt_find_limit(count, s->ocr1, timeout);
190 191
    }
    if (s->ir & GPT_IR_OF2IE) {
192
        timeout = imx_gpt_find_limit(count, s->ocr2, timeout);
193 194
    }
    if (s->ir & GPT_IR_OF3IE) {
195
        timeout = imx_gpt_find_limit(count, s->ocr3, timeout);
196 197 198 199 200 201 202 203 204 205 206 207 208 209
    }

    /* find the next set of interrupts to raise for next timer event */

    s->next_int = 0;
    if ((s->ir & GPT_IR_OF1IE) && (timeout == s->ocr1)) {
        s->next_int |= GPT_SR_OF1;
    }
    if ((s->ir & GPT_IR_OF2IE) && (timeout == s->ocr2)) {
        s->next_int |= GPT_SR_OF2;
    }
    if ((s->ir & GPT_IR_OF3IE) && (timeout == s->ocr3)) {
        s->next_int |= GPT_SR_OF3;
    }
210
    if ((s->ir & GPT_IR_ROVIE) && (timeout == GPT_TIMER_MAX)) {
211 212 213 214
        s->next_int |= GPT_SR_ROV;
    }

    /* the new range to count down from */
215
    limit = timeout - imx_gpt_update_count(s);
216 217 218 219 220 221 222 223 224

    if (limit < 0) {
        /*
         * if we reach here, then QEMU is running too slow and we pass the
         * timeout limit while computing it. Let's deliver the interrupt
         * and compute a new limit.
         */
        s->sr |= s->next_int;

225
        imx_gpt_compute_next_timeout(s, event);
226

227
        imx_gpt_update_int(s);
228 229 230 231 232 233
    } else {
        /* New timeout value */
        s->next_timeout = timeout;

        /* reset the limit to the computed range */
        ptimer_set_limit(s->timer, limit, 1);
P
Peter Chubb 已提交
234 235 236
    }
}

237
static uint64_t imx_gpt_read(void *opaque, hwaddr offset, unsigned size)
P
Peter Chubb 已提交
238
{
239
    IMXGPTState *s = IMX_GPT(opaque);
240
    uint32_t reg_value = 0;
P
Peter Chubb 已提交
241

242
    switch (offset >> 2) {
P
Peter Chubb 已提交
243
    case 0: /* Control Register */
244 245
        reg_value = s->cr;
        break;
P
Peter Chubb 已提交
246 247

    case 1: /* prescaler */
248 249
        reg_value = s->pr;
        break;
P
Peter Chubb 已提交
250 251

    case 2: /* Status Register */
252 253
        reg_value = s->sr;
        break;
P
Peter Chubb 已提交
254 255

    case 3: /* Interrupt Register */
256 257
        reg_value = s->ir;
        break;
P
Peter Chubb 已提交
258 259

    case 4: /* Output Compare Register 1 */
260 261
        reg_value = s->ocr1;
        break;
P
Peter Chubb 已提交
262

263
    case 5: /* Output Compare Register 2 */
264 265
        reg_value = s->ocr2;
        break;
266 267

    case 6: /* Output Compare Register 3 */
268 269
        reg_value = s->ocr3;
        break;
270 271

    case 7: /* input Capture Register 1 */
272 273
        qemu_log_mask(LOG_UNIMP, "[%s]%s: icr1 feature is not implemented\n",
                      TYPE_IMX_GPT, __func__);
274 275
        reg_value = s->icr1;
        break;
276 277

    case 8: /* input Capture Register 2 */
278 279
        qemu_log_mask(LOG_UNIMP, "[%s]%s: icr2 feature is not implemented\n",
                      TYPE_IMX_GPT, __func__);
280 281
        reg_value = s->icr2;
        break;
P
Peter Chubb 已提交
282 283

    case 9: /* cnt */
284
        imx_gpt_update_count(s);
285 286 287 288
        reg_value = s->cnt;
        break;

    default:
289 290
        qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%"
                      HWADDR_PRIx "\n", TYPE_IMX_GPT, __func__, offset);
291
        break;
P
Peter Chubb 已提交
292 293
    }

294
    DPRINTF("(%s) = 0x%08x\n", imx_gpt_reg_name(offset >> 2), reg_value);
295

296
    return reg_value;
P
Peter Chubb 已提交
297 298
}

299
static void imx_gpt_reset(DeviceState *dev)
P
Peter Chubb 已提交
300
{
301
    IMXGPTState *s = IMX_GPT(dev);
P
Peter Chubb 已提交
302

303 304 305
    /* stop timer */
    ptimer_stop(s->timer);

P
Peter Chubb 已提交
306 307 308
    /*
     * Soft reset doesn't touch some bits; hard reset clears them
     */
309 310
    s->cr &= ~(GPT_CR_EN|GPT_CR_ENMOD|GPT_CR_STOPEN|GPT_CR_DOZEN|
               GPT_CR_WAITEN|GPT_CR_DBGEN);
P
Peter Chubb 已提交
311 312 313 314
    s->sr = 0;
    s->pr = 0;
    s->ir = 0;
    s->cnt = 0;
315 316 317
    s->ocr1 = GPT_TIMER_MAX;
    s->ocr2 = GPT_TIMER_MAX;
    s->ocr3 = GPT_TIMER_MAX;
318 319
    s->icr1 = 0;
    s->icr2 = 0;
320

321
    s->next_timeout = GPT_TIMER_MAX;
322 323 324
    s->next_int = 0;

    /* compute new freq */
325
    imx_gpt_set_freq(s);
326

327 328
    /* reset the limit to GPT_TIMER_MAX */
    ptimer_set_limit(s->timer, GPT_TIMER_MAX, 1);
329 330 331 332 333

    /* if the timer is still enabled, restart it */
    if (s->freq && (s->cr & GPT_CR_EN)) {
        ptimer_run(s->timer, 1);
    }
P
Peter Chubb 已提交
334 335
}

336 337
static void imx_gpt_write(void *opaque, hwaddr offset, uint64_t value,
                          unsigned size)
P
Peter Chubb 已提交
338
{
339
    IMXGPTState *s = IMX_GPT(opaque);
340 341
    uint32_t oldreg;

342
    DPRINTF("(%s, value = 0x%08x)\n", imx_gpt_reg_name(offset >> 2),
343 344
            (uint32_t)value);

345
    switch (offset >> 2) {
346 347 348 349 350
    case 0:
        oldreg = s->cr;
        s->cr = value & ~0x7c14;
        if (s->cr & GPT_CR_SWR) { /* force reset */
            /* handle the reset */
351
            imx_gpt_reset(DEVICE(s));
352 353
        } else {
            /* set our freq, as the source might have changed */
354
            imx_gpt_set_freq(s);
355 356 357 358

            if ((oldreg ^ s->cr) & GPT_CR_EN) {
                if (s->cr & GPT_CR_EN) {
                    if (s->cr & GPT_CR_ENMOD) {
359 360
                        s->next_timeout = GPT_TIMER_MAX;
                        ptimer_set_count(s->timer, GPT_TIMER_MAX);
361
                        imx_gpt_compute_next_timeout(s, false);
362 363 364 365 366
                    }
                    ptimer_run(s->timer, 1);
                } else {
                    /* stop timer */
                    ptimer_stop(s->timer);
P
Peter Chubb 已提交
367
                }
368
            }
P
Peter Chubb 已提交
369
        }
370
        break;
P
Peter Chubb 已提交
371 372 373

    case 1: /* Prescaler */
        s->pr = value & 0xfff;
374
        imx_gpt_set_freq(s);
375
        break;
P
Peter Chubb 已提交
376 377

    case 2: /* SR */
378
        s->sr &= ~(value & 0x3f);
379
        imx_gpt_update_int(s);
380
        break;
P
Peter Chubb 已提交
381 382 383

    case 3: /* IR -- interrupt register */
        s->ir = value & 0x3f;
384
        imx_gpt_update_int(s);
385

386
        imx_gpt_compute_next_timeout(s, false);
387 388

        break;
P
Peter Chubb 已提交
389 390

    case 4: /* OCR1 -- output compare register */
391 392
        s->ocr1 = value;

P
Peter Chubb 已提交
393 394
        /* In non-freerun mode, reset count when this register is written */
        if (!(s->cr & GPT_CR_FRR)) {
395 396
            s->next_timeout = GPT_TIMER_MAX;
            ptimer_set_limit(s->timer, GPT_TIMER_MAX, 1);
P
Peter Chubb 已提交
397
        }
398 399

        /* compute the new timeout */
400
        imx_gpt_compute_next_timeout(s, false);
401 402

        break;
P
Peter Chubb 已提交
403

404
    case 5: /* OCR2 -- output compare register */
405 406 407
        s->ocr2 = value;

        /* compute the new timeout */
408
        imx_gpt_compute_next_timeout(s, false);
409 410 411

        break;

412
    case 6: /* OCR3 -- output compare register */
413 414 415
        s->ocr3 = value;

        /* compute the new timeout */
416
        imx_gpt_compute_next_timeout(s, false);
417 418 419

        break;

P
Peter Chubb 已提交
420
    default:
421 422
        qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%"
                      HWADDR_PRIx "\n", TYPE_IMX_GPT, __func__, offset);
423
        break;
P
Peter Chubb 已提交
424 425 426
    }
}

427
static void imx_gpt_timeout(void *opaque)
P
Peter Chubb 已提交
428
{
429
    IMXGPTState *s = IMX_GPT(opaque);
P
Peter Chubb 已提交
430

431
    DPRINTF("\n");
P
Peter Chubb 已提交
432

433 434 435
    s->sr |= s->next_int;
    s->next_int = 0;

436
    imx_gpt_compute_next_timeout(s, true);
P
Peter Chubb 已提交
437

438
    imx_gpt_update_int(s);
439 440 441 442

    if (s->freq && (s->cr & GPT_CR_EN)) {
        ptimer_run(s->timer, 1);
    }
P
Peter Chubb 已提交
443 444
}

445 446 447
static const MemoryRegionOps imx_gpt_ops = {
    .read = imx_gpt_read,
    .write = imx_gpt_write,
P
Peter Chubb 已提交
448 449 450 451
    .endianness = DEVICE_NATIVE_ENDIAN,
};


452
static void imx_gpt_realize(DeviceState *dev, Error **errp)
P
Peter Chubb 已提交
453
{
454 455
    IMXGPTState *s = IMX_GPT(dev);
    SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
P
Peter Chubb 已提交
456 457
    QEMUBH *bh;

458
    sysbus_init_irq(sbd, &s->irq);
459
    memory_region_init_io(&s->iomem, OBJECT(s), &imx_gpt_ops, s, TYPE_IMX_GPT,
P
Peter Chubb 已提交
460
                          0x00001000);
461
    sysbus_init_mmio(sbd, &s->iomem);
P
Peter Chubb 已提交
462

463
    bh = qemu_bh_new(imx_gpt_timeout, s);
464
    s->timer = ptimer_init(bh, PTIMER_POLICY_DEFAULT);
P
Peter Chubb 已提交
465 466
}

467
static void imx_gpt_class_init(ObjectClass *klass, void *data)
P
Peter Chubb 已提交
468
{
469 470 471 472 473
    DeviceClass *dc = DEVICE_CLASS(klass);

    dc->realize = imx_gpt_realize;
    dc->reset = imx_gpt_reset;
    dc->vmsd = &vmstate_imx_timer_gpt;
P
Peter Chubb 已提交
474 475 476
    dc->desc = "i.MX general timer";
}

477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
static void imx25_gpt_init(Object *obj)
{
    IMXGPTState *s = IMX_GPT(obj);

    s->clocks = imx25_gpt_clocks;
}

static void imx31_gpt_init(Object *obj)
{
    IMXGPTState *s = IMX_GPT(obj);

    s->clocks = imx31_gpt_clocks;
}

static void imx6_gpt_init(Object *obj)
{
    IMXGPTState *s = IMX_GPT(obj);

    s->clocks = imx6_gpt_clocks;
}

static const TypeInfo imx25_gpt_info = {
    .name = TYPE_IMX25_GPT,
P
Peter Chubb 已提交
500
    .parent = TYPE_SYS_BUS_DEVICE,
501
    .instance_size = sizeof(IMXGPTState),
502
    .instance_init = imx25_gpt_init,
503
    .class_init = imx_gpt_class_init,
P
Peter Chubb 已提交
504 505
};

506 507 508 509 510 511 512 513 514 515 516 517
static const TypeInfo imx31_gpt_info = {
    .name = TYPE_IMX31_GPT,
    .parent = TYPE_IMX25_GPT,
    .instance_init = imx31_gpt_init,
};

static const TypeInfo imx6_gpt_info = {
    .name = TYPE_IMX6_GPT,
    .parent = TYPE_IMX25_GPT,
    .instance_init = imx6_gpt_init,
};

518
static void imx_gpt_register_types(void)
P
Peter Chubb 已提交
519
{
520 521 522
    type_register_static(&imx25_gpt_info);
    type_register_static(&imx31_gpt_info);
    type_register_static(&imx6_gpt_info);
P
Peter Chubb 已提交
523 524
}

525
type_init(imx_gpt_register_types)