aboutsummaryrefslogtreecommitdiff
path: root/module/gtimer/src/mod_gtimer.c
blob: a1fd882d8cb4c7e3bdfa381b61b2c54710256858 (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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
 * Arm SCP/MCP Software
 * Copyright (c) 2017-2018, Arm Limited and Contributors. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <stdint.h>
#include <fwk_assert.h>
#include <fwk_errno.h>
#include <fwk_id.h>
#include <fwk_macros.h>
#include <fwk_mm.h>
#include <fwk_module.h>
#include <fwk_notification.h>
#include <gtimer_reg.h>
#include <mod_clock.h>
#include <mod_gtimer.h>
#include <mod_power_domain.h>
#include <mod_timer.h>

#define GTIMER_FREQUENCY_MIN_HZ  UINT32_C(1)
#define GTIMER_FREQUENCY_MAX_HZ  UINT32_C(1000000000)
#define GTIMER_MIN_TIMESTAMP 2000

/* Device content */
struct dev_ctx {
    struct cntbase_reg *hw_timer;
    struct cntctl_reg *hw_counter;
    struct cntcontrol_reg *control;
    const struct mod_gtimer_dev_config *config;
};

/* Table of device context structures */
static struct dev_ctx *ctx_table;

/*
 * Functions fulfilling the Timer module's driver interface
 */

static int enable(fwk_id_t dev_id)
{
    struct dev_ctx *ctx;
    int status;

    status = fwk_module_check_call(dev_id);
    if (status != FWK_SUCCESS)
        return status;

    ctx = ctx_table + fwk_id_get_element_idx(dev_id);

    ctx->hw_timer->P_CTL &= ~CNTBASE_P_CTL_IMASK;
    ctx->hw_timer->P_CTL |=  CNTBASE_P_CTL_ENABLE;

    return FWK_SUCCESS;
}

static int disable(fwk_id_t dev_id)
{
    struct dev_ctx *ctx;
    int status;

    status = fwk_module_check_call(dev_id);
    if (status != FWK_SUCCESS)
        return status;

    ctx = ctx_table + fwk_id_get_element_idx(dev_id);

    ctx->hw_timer->P_CTL |=  CNTBASE_P_CTL_IMASK;
    ctx->hw_timer->P_CTL &= ~CNTBASE_P_CTL_ENABLE;

    return FWK_SUCCESS;
}

static int get_counter(fwk_id_t dev_id, uint64_t *value)
{
    const struct dev_ctx *ctx;
    int status;
    uint32_t counter_low;
    uint32_t counter_high;

    status = fwk_module_check_call(dev_id);
    if (status != FWK_SUCCESS)
        return status;

    ctx = ctx_table + fwk_id_get_element_idx(dev_id);

    /*
     * To avoid race conditions where the high half of the counter increments
     * after it has been sampled but before the low half is sampled, the values
     * are resampled until the high half has stabilized. This assumes that the
     * loop is faster than the high half incrementation.
     */
    do {
        counter_high = ctx->hw_timer->PCTH;
        counter_low = ctx->hw_timer->PCTL;
    } while (counter_high != ctx->hw_timer->PCTH);

    *value = ((uint64_t)counter_high << 32) | counter_low;

    return FWK_SUCCESS;
}

static int set_timer(fwk_id_t dev_id, uint64_t timestamp)
{
    struct dev_ctx *ctx;
    uint64_t counter;
    int status;

    status = fwk_module_check_call(dev_id);
    if (status != FWK_SUCCESS)
        return status;

    status = get_counter(dev_id, &counter);
    if (status != FWK_SUCCESS)
        return status;

    /*
     * If an alarm's period is very small, the timer device could be configured
     * to interrupt on a timestamp that is "in the past". In this case, with the
     * current FVP implementation an interrupt will not be generated. To avoid
     * this issue, the minimum timestamp is GTIMER_MIN_TIMESTAMP ticks from now.
     *
     * It is assumed here that the 64-bit counter will never loop back during
     * the course of the execution (@1GHz it would loop back after ~585 years).
     */
    timestamp = FWK_MAX(counter + GTIMER_MIN_TIMESTAMP, timestamp);

    ctx = ctx_table + fwk_id_get_element_idx(dev_id);

    ctx->hw_timer->P_CVALL = timestamp & 0xFFFFFFFF;
    ctx->hw_timer->P_CVALH = timestamp >> 32;

    return FWK_SUCCESS;
}

static int get_timer(fwk_id_t dev_id, uint64_t *timestamp)
{
    struct dev_ctx *ctx;
    int status;

    status = fwk_module_check_call(dev_id);
    if (status != FWK_SUCCESS)
        return status;

    ctx = ctx_table + fwk_id_get_element_idx(dev_id);

    struct {
        uint32_t low;
        uint32_t high;
    } value;

    /* Read 64-bit timer value */
    value.low = ctx->hw_timer->P_CVALL;
    value.high = ctx->hw_timer->P_CVALH;

    *timestamp = *(uint64_t *)&value;
    return FWK_SUCCESS;
}

static int get_frequency(fwk_id_t dev_id, uint32_t *frequency)
{
    int status;
    struct dev_ctx *ctx;

    if (frequency == NULL)
        return FWK_E_PARAM;

    status = fwk_module_check_call(dev_id);
    if (status != FWK_SUCCESS)
        return status;

    ctx = ctx_table + fwk_id_get_element_idx(dev_id);

    *frequency = ctx->config->frequency;

    return FWK_SUCCESS;
}

static const struct mod_timer_driver_api module_api = {
    .name = "Generic Timer Driver",
    .enable = enable,
    .disable = disable,
    .set_timer = set_timer,
    .get_timer = get_timer,
    .get_counter = get_counter,
    .get_frequency = get_frequency,
};

/*
 * Functions fulfilling the framework's module interface
 */

static int gtimer_init(fwk_id_t module_id,
                       unsigned int element_count,
                       const void *data)
{
    ctx_table = fwk_mm_alloc(element_count, sizeof(struct dev_ctx));

    if (ctx_table == NULL)
        return FWK_E_NOMEM;

    return FWK_SUCCESS;
}

static int gtimer_device_init(fwk_id_t element_id, unsigned int unused,
                              const void *data)
{
    struct dev_ctx *ctx;

    ctx = ctx_table + fwk_id_get_element_idx(element_id);

    ctx->config = data;
    if (ctx->config->hw_timer == 0   ||
        ctx->config->hw_counter == 0 ||
        ctx->config->control == 0    ||
        ctx->config->frequency < GTIMER_FREQUENCY_MIN_HZ ||
        ctx->config->frequency > GTIMER_FREQUENCY_MAX_HZ) {

        return FWK_E_DEVICE;
    }

    ctx->hw_timer   = (struct cntbase_reg *)ctx->config->hw_timer;
    ctx->hw_counter = (struct cntctl_reg *)ctx->config->hw_counter;
    ctx->control    = (struct cntcontrol_reg *)ctx->config->control;

    disable(element_id);

    ctx->hw_counter->ACR = CNTCTL_ACR_RPCT |
                           CNTCTL_ACR_RVCT |
                           CNTCTL_ACR_RFRQ |
                           CNTCTL_ACR_RVOFF|
                           CNTCTL_ACR_RWPT;
    ctx->hw_counter->FRQ = ctx->config->frequency;

    return FWK_SUCCESS;
}

static int gtimer_process_bind_request(fwk_id_t requester_id,
                                       fwk_id_t id,
                                       fwk_id_t api_type,
                                       const void **api)
{
    /* No binding to the module */
    if (fwk_module_is_valid_module_id(id))
        return FWK_E_ACCESS;

    *api = &module_api;

    return FWK_SUCCESS;
}

static void gtimer_control_init(struct dev_ctx *ctx)
{
    /* Set primary counter update frequency and enable counter. */
    ctx->control->CR &= ~CNTCONTROL_CR_EN;
    ctx->control->FID0 = ctx->config->frequency;
    ctx->control->CR |= CNTCONTROL_CR_FCREQ | CNTCONTROL_CR_EN;
}

static int gtimer_start(fwk_id_t id)
{
    struct dev_ctx *ctx;

    if (!fwk_id_is_type(id, FWK_ID_TYPE_ELEMENT))
        return FWK_SUCCESS;

    ctx = ctx_table + fwk_id_get_element_idx(id);

    if (!fwk_id_is_type(ctx->config->clock_id, FWK_ID_TYPE_NONE)) {
        /* Register for clock state notifications */
        return fwk_notification_subscribe(
            mod_clock_notification_id_state_changed,
            ctx->config->clock_id,
            id);
    } else
        gtimer_control_init(ctx);

    return FWK_SUCCESS;
}

static int gtimer_process_notification(
    const struct fwk_event *event,
    struct fwk_event *resp_event)
{
    struct clock_notification_params *params;
    struct dev_ctx *ctx;

    assert(fwk_id_is_equal(event->id, mod_clock_notification_id_state_changed));
    assert(fwk_id_is_type(event->target_id, FWK_ID_TYPE_ELEMENT));

    params = (struct clock_notification_params *)event->params;

    if (params->new_state == MOD_CLOCK_STATE_RUNNING) {
        ctx = ctx_table + fwk_id_get_element_idx(event->target_id);
        gtimer_control_init(ctx);
    }

    return FWK_SUCCESS;
}


/*
 * Module descriptor
 */
const struct fwk_module module_gtimer = {
    .name = "Generic Timer Driver",
    .api_count = 1,
    .event_count = 0,
    .type = FWK_MODULE_TYPE_DRIVER,
    .init = gtimer_init,
    .element_init = gtimer_device_init,
    .start = gtimer_start,
    .process_bind_request = gtimer_process_bind_request,
    .process_notification = gtimer_process_notification,
};