1/*
2 * Copyright 2019, 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#pragma once
13
14/* Default ipg_clk configuration:
15 *
16 * FIN (24 MHZ)
17 *  |_ *22 PLL2 (528)
18 *           |_ /4 AHB_CLK (132 MHZ)
19 *                     |_ /2 IPG_CLK (66 MHZ)
20 *                              |_ EPIT
21 */
22#define PLL2_FREQ  (528u)
23#define AHB_FREQ   (PLL2_FREQ / 4u)
24#define IPG_FREQ   (AHB_FREQ  / 2u)
25#define GPT_FREQ   IPG_FREQ
26
27#include <platsupport/mach/gpt.h>
28#include <platsupport/mach/epit.h>
29
30typedef struct {
31    gpt_t timestamp;
32    epit_t timeout;
33} imx_timers_t;
34
35static inline uint64_t imx_get_time(imx_timers_t *timers)
36{
37    return gpt_get_time(&timers->timestamp);
38}
39
40static inline void imx_start_timestamp(imx_timers_t *timers)
41{
42    gpt_start(&timers->timestamp);
43}
44
45static inline void imx_stop_timestamp(imx_timers_t *timers)
46{
47    gpt_stop(&timers->timestamp);
48}
49
50static inline int imx_init_timestamp(imx_timers_t *timers, ps_io_ops_t io_ops, ltimer_callback_fn_t user_callback,
51                                     void *user_callback_token)
52{
53    gpt_config_t config = {
54        .io_ops = io_ops,
55        .user_callback = user_callback,
56        .user_callback_token = user_callback_token,
57        .device_path = GPT_PATH,
58        .prescaler = GPT_PRESCALER
59    };
60    return gpt_init(&timers->timestamp, config);
61}
62
63static inline int imx_destroy_timestamp(imx_timers_t *timers)
64{
65    return gpt_destroy(&timers->timestamp);
66}
67
68static inline int imx_set_timeout(imx_timers_t *timers, uint64_t ns, bool periodic)
69{
70    return epit_set_timeout(&timers->timeout, ns, periodic);
71}
72
73static inline void imx_stop_timeout(imx_timers_t *timers)
74{
75    epit_stop(&timers->timeout);
76}
77
78static inline int imx_init_timeout(imx_timers_t *timers, ps_io_ops_t io_ops, ltimer_callback_fn_t user_callback,
79                                   void *user_callback_token)
80{
81    epit_config_t config = {
82        .io_ops = io_ops,
83        .user_callback = user_callback,
84        .user_callback_token = user_callback_token,
85        .device_path = EPIT_PATH,
86        .prescaler = 0
87    };
88    return epit_init(&timers->timeout, config);
89}
90
91static inline int imx_destroy_timeout(imx_timers_t *timers)
92{
93    return epit_destroy(&timers->timeout);
94}
95