1/*
2 * Copyright 2017, DornerWorks
3 * Copyright 2020, Data61
4 * Commonwealth Scientific and Industrial Research Organisation (CSIRO)
5 * ABN 41 687 119 230.
6 *
7 * This software may be distributed and modified according to the terms of
8 * the BSD 2-Clause license. Note that NO WARRANTY is provided.
9 * See "LICENSE_BSD2.txt" for details.
10 *
11 * @TAG(DATA61_DORNERWORKS_BSD)
12 */
13/*
14 * This data was produced by DornerWorks, Ltd. of Grand Rapids, MI, USA under
15 * a DARPA SBIR, Contract Number D16PC00107.
16 *
17 * Approved for Public Release, Distribution Unlimited.
18 */
19
20#pragma once
21
22#include <platsupport/io.h>
23#include <platsupport/ltimer.h>
24#include <platsupport/fdt.h>
25#include <platsupport/timer.h>
26#include <platsupport/clock.h>
27
28#define IRQS_PER_TTC 3
29
30#ifdef CONFIG_PLAT_ZYNQMP
31#define TTC0_PATH "/amba/timer@ff110000"
32#define TTC1_PATH "/amba/timer@ff120000"
33#define TTC2_PATH "/amba/timer@ff130000"
34#define TTC3_PATH "/amba/timer@ff140000"
35#else
36/* zynq7000 */
37#define TTC0_PATH "/amba/timer@f8001000"
38#define TTC1_PATH "/amba/timer@f8002000"
39#endif /* CONFIG_PLAT_ZYNQMP */
40
41/* Timers */
42typedef enum {
43    TTC0_TIMER1,
44    TTC0_TIMER2,
45    TTC0_TIMER3,
46    TTC1_TIMER1,
47    TTC1_TIMER2,
48    TTC1_TIMER3,
49#ifdef CONFIG_PLAT_ZYNQMP
50    TTC2_TIMER1,
51    TTC2_TIMER2,
52    TTC2_TIMER3,
53    TTC3_TIMER1,
54    TTC3_TIMER2,
55    TTC3_TIMER3,
56#endif
57    NTIMERS
58} ttc_id_t;
59#define TMR_DEFAULT TTC0_TIMER1
60
61typedef struct {
62    bool is_timestamp;
63    ps_io_ops_t io_ops;
64    ltimer_callback_fn_t user_callback;
65    void *user_callback_token;
66    char *device_path;
67    clk_t *clk_src;
68    ttc_id_t id;
69} ttc_config_t;
70
71typedef struct {
72    void *regs;
73    ps_io_ops_t io_ops;
74    irq_id_t irq_id;
75    pmem_region_t timer_pmem;
76    ltimer_callback_fn_t user_callback;
77    void *user_callback_token;
78    bool is_timestamp;
79    uint64_t hi_time;
80    clk_t clk;
81    freq_t freq;
82    ttc_id_t id;
83} ttc_t;
84
85static UNUSED timer_properties_t ttc_properties = {
86    .upcounter = true,
87    .timeouts = true,
88    .bit_width = 16,
89    .irqs = 1,
90    .relative_timeouts = true,
91    .absolute_timeouts = true
92};
93
94int ttc_init(ttc_t *ttc, ttc_config_t config);
95int ttc_destroy(ttc_t *ttc);
96int ttc_start(ttc_t *ttc);
97int ttc_stop(ttc_t *ttc);
98int ttc_set_timeout(ttc_t *ttc, uint64_t ns, bool periodic);
99/* set the ttc to 0 and start free running, where the timer will
100 * continually increment and trigger irqs on each overflow and reload to 0 */
101void ttc_freerun(ttc_t *tcc);
102uint64_t ttc_get_time(ttc_t *ttc);
103/* convert from a ticks value to ns for a configured ttc */
104uint64_t ttc_ticks_to_ns(ttc_t *ttc, uint32_t ticks);
105