summaryrefslogtreecommitdiffstats
path: root/firmware/target/arm/imx31/gigabeat-s/kernel-imx31.c
blob: c2fddc40a71ab3e980c44cc3ef1d7167d89e4704 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "config.h"
#include "system.h"
#include "avic-imx31.h"
#include "kernel.h"
#include "thread.h"
#include <stdio.h>

extern void (*tick_funcs[MAX_NUM_TICK_TASKS])(void);

static __attribute__((interrupt("IRQ"))) void EPIT1_HANDLER(void)
{
    int i;

    EPITSR1 = 1; /* Clear the pending status */

    /* Run through the list of tick tasks */
    for(i = 0;i < MAX_NUM_TICK_TASKS;i++)
    {
        if(tick_funcs[i])
            tick_funcs[i]();
    }

    current_tick++;
}

void tick_start(unsigned int interval_in_ms)
{
    CLKCTL_CGR0 |= (3 << 6);   /* EPIT1 module clock ON - before writing regs! */
    EPITCR1 &= ~((1 << 2) | (1 << 0)); /* Disable the counter */
    CLKCTL_WIMR0 &= ~(1 << 23);  /* Clear wakeup mask */

    /* NOTE: This isn't really accurate yet but it's close enough to work
     * with for the moment */

    /* CLKSRC=32KHz, EPIT Output Disconnected, Enabled
     * prescale 1/32, Reload from modulus register, Compare interrupt enabled,
     * Count from load value */
    EPITCR1 = (3 << 24) | (1 << 19) | (32 << 4) |
              (1 << 3) | (1 << 2) | (1 << 1);

    EPITLR1 = interval_in_ms;   /* Count down from interval */
    EPITCMPR1 = 0;              /* Event when counter reaches 0 */
    EPITSR1 = 1;                /* Clear any pending interrupt */
    avic_enable_int(EPIT1, IRQ, 7, EPIT1_HANDLER);
    EPITCR1 |= (1 << 0);        /* Enable the counter */
}

#ifdef BOOTLOADER
void tick_stop(void)
{
    avic_disable_int(EPIT1);    /* Disable insterrupt */
    EPITCR1 &= ~((1 << 2) | (1 << 0)); /* Disable counter */
    CLKCTL_CGR0 &= ~(3 << 6);   /* EPIT1 module clock OFF */
}
#endif