1/*
2 * Copyright 2017, Data61
3 * Commonwealth Scientific and Industrial Research Organisation (CSIRO)
4 * ABN 41 687 119 230.
5 *
6 * This software may be distributed and modified according to the terms of
7 * the BSD 2-Clause license. Note that NO WARRANTY is provided.
8 * See "LICENSE_BSD2.txt" for details.
9 *
10 * @TAG(DATA61_BSD)
11 */
12#include <autoconf.h>
13#include <platsupport/gen_config.h>
14#include <stdio.h>
15#include <assert.h>
16#include <errno.h>
17#include <stdlib.h>
18
19#include <utils/util.h>
20
21#include <platsupport/arch/generic_timer.h>
22
23uint64_t generic_timer_get_time(generic_timer_t *timer)
24{
25    return freq_cycles_and_hz_to_ns(generic_timer_get_ticks(), timer->freq);
26}
27
28int generic_timer_get_init(generic_timer_t *timer)
29{
30
31    if (timer == NULL) {
32        ZF_LOGE("Must provide memory for generic timer");
33        return EINVAL;
34    }
35
36    if (!config_set(CONFIG_EXPORT_PCNT_USER)) {
37        ZF_LOGE("Generic timer not exported!");
38        return ENXIO;
39    }
40
41    /* try to read the frequency */
42    timer->freq = generic_timer_get_freq();
43
44#ifdef PCT_TICKS_PER_US
45    if (timer->freq == 0) {
46        freq = PCT_TICKS_PER_US;
47    }
48#endif
49
50    if (timer->freq == 0) {
51        /* fail init, we don't know what the frequency of the timer is */
52        ZF_LOGE("Failed to find generic timer frequency");
53        return ENXIO;
54    }
55
56    return 0;
57}
58