summaryrefslogtreecommitdiff
path: root/arch/arm/mach-tegra/timer.c
blob: 8ccec6551f537fd0bb59a618ba09d0ca28bc37fd (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
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
/*
 * arch/arm/mach-tegra/timer.c
 *
 * Timer and clock event support for NVIDIA Tegra SoCs
 *
 * Copyright (c) 2008-2009, NVIDIA Corporation.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <linux/init.h>
#include <linux/time.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/clockchips.h>
#include <asm/mach/time.h>
#include <asm/io.h>

#include "ap15/artimer.h"
#include "ap15/artimerus.h"
#include "ap15/arclk_rst.h"
#include "nvcommon.h"
#include "nvrm_drf.h"
#include "nvrm_init.h"
#include "nvrm_module.h"
#include "nvrm_interrupt.h"
#include "nvrm_hardware_access.h"
#include "mach/nvrm_linux.h"
#include "nvos.h"
#include "nvassert.h"

/* CPU complex uses timer instance 2 */
#define NV_CPU_TIMER_INSTANCE   2
#define NV_CPU_SPARE_TIMER_INSTANCE  3 

static volatile NvU8 *s_NvTimerTick;
static volatile NvU8 *s_NvTimerUsec;

static irqreturn_t NvTimerIntrHandler(
    int irq,
    void *dev_id)
{
    struct clock_event_device *dev =
        (struct clock_event_device *)dev_id;
    NV_WRITE32(s_NvTimerTick + TIMER_TMR_PCR_0,
               NV_DRF_NUM(TIMER, TMR_PCR, INTR_CLR, 1));
    BUG_ON(dev == NULL);
    dev->event_handler(dev);
    return IRQ_HANDLED;
}

static inline void NvTimerUpdateHardware(
    unsigned long cycles,
    int periodic)
{
    NvU32 v;
    v  = NV_DRF_NUM(TIMER, TMR_PTV, TMR_PTV, cycles);
    if (periodic) {
        v |= NV_DRF_DEF(TIMER, TMR_PTV, PER, ENABLE);
    }
    v |= NV_DRF_DEF(TIMER, TMR_PTV, EN, ENABLE);
    NV_WRITE32(s_NvTimerTick + TIMER_TMR_PTV_0, v);
}

static inline int NvTimerSetEvent(
    unsigned long cycles,
    struct clock_event_device *dev)
{
    NvTimerUpdateHardware(cycles, 0);
    return 0;
}

static inline void NvTimerSetMode(
    enum clock_event_mode mode,
    struct clock_event_device *dev)
{
 
    NV_WRITE32(s_NvTimerTick + TIMER_TMR_PTV_0, 0);

    switch (mode) {
        case CLOCK_EVT_MODE_PERIODIC:
            NvTimerUpdateHardware((1000000/HZ)-1, 1);
            break;
        case CLOCK_EVT_MODE_ONESHOT:
            break;
        case CLOCK_EVT_MODE_RESUME:
        case CLOCK_EVT_MODE_SHUTDOWN:
        case CLOCK_EVT_MODE_UNUSED:
            break;
    }
}

static cycle_t NvReadTimerUs(void)
{
    return (cycle_t) NV_READ32(s_NvTimerUsec + TIMERUS_CNTR_1US_0);
}

/* Timers on Tegra run at microsecond resolution, so the
 * best way to approximate a divide by 1000 (ns-to-cycle)
 * and maintain a decent amount of precision is to multiply
 * by 16777 and shift right by 24.  This causes 1000.01ns
 * in kernel time to represent 1000ns in actual time. */

static struct clock_event_device s_NvTimer = {
    .name           = "timer0",
    .rating         = 300,
    .features       = CLOCK_EVT_FEAT_ONESHOT,
    .mult           = 16777,
    .shift          = 24,
    .set_next_event = NvTimerSetEvent,
    .set_mode       = NvTimerSetMode,
};

static struct irqaction s_NvTimerIrq = {
    .name    = "timer0",
    .flags   = IRQF_DISABLED | IRQF_TIMER | IRQF_TRIGGER_HIGH,
    .handler = NvTimerIntrHandler,
    .dev_id  = &s_NvTimer,
};

/*  Converting from clock-cycles to nanoseconds is trivial -
 *  just multiply by 1000 */
static struct clocksource s_NvClockUs =
{
    .name    = "timer_us",
    .rating  = 300,
    .read   = NvReadTimerUs,
    .mask   = 0xFFFFFFFFUL,
    .mult   = 1000,
    .shift  = 0,
    .flags  = CLOCK_SOURCE_IS_CONTINUOUS,
};

static void NvSpareTimerInit(void);

static void __init tegra_timer_init(void)
{
    NvRmPhysAddr Phys;
    NvU32 Len;
    volatile NvU8 *pCar = NULL;
    NvU32 OscFreq;

    NvRmModuleGetBaseAddress(s_hRmGlobal,
        NVRM_MODULE_ID(NvRmPrivModuleID_ClockAndReset, 0), &Phys, &Len);
    if (NvRmPhysicalMemMap(Phys, Len, NVOS_MEM_READ_WRITE,
            NvOsMemAttribute_Uncached, (void **)&pCar)!=NvSuccess)
    {
        NV_ASSERT(!"Error: Unable to get clock controller base address\n");
    }

    OscFreq = NV_READ32(pCar + CLK_RST_CONTROLLER_OSC_CTRL_0);

    NvRmModuleGetBaseAddress(s_hRmGlobal, 
        NVRM_MODULE_ID(NvRmModuleID_TimerUs, 0), &Phys, &Len);

    if (NvRmPhysicalMemMap(Phys, Len, NVOS_MEM_READ_WRITE,
            NvOsMemAttribute_Uncached, (void **)&s_NvTimerUsec)!=NvSuccess)
    {
        NV_ASSERT(!"ERROR: Unable to get microsecod timer base address\n");
    }

    switch (NV_DRF_VAL(CLK_RST_CONTROLLER, OSC_CTRL, OSC_FREQ, OscFreq))
    {
    case 0:  // 13MHz
        NV_WRITE32(s_NvTimerUsec + TIMERUS_USEC_CFG_0,
                   NV_DRF_NUM(TIMERUS, USEC_CFG, USEC_DIVIDEND, 0) |
                   NV_DRF_NUM(TIMERUS, USEC_CFG, USEC_DIVISOR, 12));
        break;
    case 1:  // 19.2 MHz
        NV_WRITE32(s_NvTimerUsec + TIMERUS_USEC_CFG_0,
                   NV_DRF_NUM(TIMERUS, USEC_CFG, USEC_DIVIDEND, 4) |
                   NV_DRF_NUM(TIMERUS, USEC_CFG, USEC_DIVISOR, 95));
        break;
    case 2: // 12 MHz
        NV_WRITE32(s_NvTimerUsec + TIMERUS_USEC_CFG_0,
                   NV_DRF_NUM(TIMERUS, USEC_CFG, USEC_DIVIDEND, 0) |
                   NV_DRF_NUM(TIMERUS, USEC_CFG, USEC_DIVISOR, 11));
        break;
    case 3:  // 26 MHz
    default:
        NV_WRITE32(s_NvTimerUsec + TIMERUS_USEC_CFG_0,
                   NV_DRF_NUM(TIMERUS, USEC_CFG, USEC_DIVIDEND, 0) |
                   NV_DRF_NUM(TIMERUS, USEC_CFG, USEC_DIVISOR, 25));
        break;
    }
    if (clocksource_register(&s_NvClockUs)) {
        NV_ASSERT(!"ERROR: Could not register microsecond timer\n");
    }

    NvRmModuleGetBaseAddress(s_hRmGlobal,
        NVRM_MODULE_ID(NvRmModuleID_Timer, NV_CPU_TIMER_INSTANCE), &Phys, &Len);
    if (NvRmPhysicalMemMap(Phys, Len, NVOS_MEM_READ_WRITE,
            NvOsMemAttribute_Uncached,
            (void **)&s_NvTimerTick)!=NvSuccess) {
        NV_ASSERT(!"ERROR: Unable to get tick-timer base address\n");
    }

    s_NvTimerIrq.irq = NvRmGetIrqForLogicalInterrupt(s_hRmGlobal,
         NVRM_MODULE_ID(NvRmModuleID_Timer, NV_CPU_TIMER_INSTANCE), 0);

    if (setup_irq(s_NvTimerIrq.irq, &s_NvTimerIrq)) {
        NV_ASSERT(!"ERROR: Could not configure timer IRQ\n");
    }


    s_NvTimer.max_delta_ns =
        clockevent_delta2ns(TIMER_TMR_PTV_0_TMR_PTV_DEFAULT_MASK,
                            &s_NvTimer);
    s_NvTimer.min_delta_ns = clockevent_delta2ns(1, &s_NvTimer);
    s_NvTimer.cpumask = cpu_all_mask;
    s_NvTimer.irq = s_NvTimerIrq.irq;
    clockevents_register_device(&s_NvTimer);

    NvSpareTimerInit();
}

struct sys_timer tegra_timer = 
{
    .init = tegra_timer_init,
};

static volatile NvU8 *s_NvSpareTimerTick;

static irqreturn_t NvSpareTimerIntrHandler(
    int irq,
    void *driver_data)
{
    NV_WRITE32(s_NvSpareTimerTick + TIMER_TMR_PCR_0, 
        NV_DRF_NUM(TIMER, TMR_PCR, INTR_CLR, 1));

    return IRQ_HANDLED;
}

static struct irqaction s_SpareTimerIrq = {
    .name    = "spare_timer",
    .flags   = IRQF_DISABLED,
    .handler = NvSpareTimerIntrHandler,
    .dev_id  = &s_NvSpareTimerTick,
};

void NvSpareTimerTrigger(unsigned long cycles)
{
    NvU32 v;
    v  = NV_DRF_NUM(TIMER, TMR_PTV, TMR_PTV, cycles);
    v |= NV_DRF_DEF(TIMER, TMR_PTV, EN, ENABLE);
    NV_WRITE32(s_NvSpareTimerTick + TIMER_TMR_PTV_0, v);
}

static void NvSpareTimerInit(void)
{
    int irq;
    NvRmPhysAddr Phys;
    NvU32 Len;

    NvRmModuleGetBaseAddress(s_hRmGlobal,
        NVRM_MODULE_ID(NvRmModuleID_Timer, NV_CPU_SPARE_TIMER_INSTANCE), &Phys, &Len);

    if (NvRmPhysicalMemMap(Phys, Len, NVOS_MEM_READ_WRITE,
            NvOsMemAttribute_Uncached,
            (void **)&s_NvSpareTimerTick)!=NvSuccess) {
        NV_ASSERT(!"ERROR: Unable to get tick-timer base address\n");
    }

    irq = NvRmGetIrqForLogicalInterrupt(s_hRmGlobal,
         NVRM_MODULE_ID(NvRmModuleID_Timer, NV_CPU_SPARE_TIMER_INSTANCE), 0);

    if (setup_irq(irq, &s_SpareTimerIrq)) {
        NV_ASSERT(!"ERROR: Could not configure timer IRQ\n");
    }
}