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
13#pragma once
14
15#define GPT_FREQ   (24u)
16/* need to double check the value when the
17 * clock control module is implemented.
18 */
19#define IPG_FREQ   (24u)
20
21#include <platsupport/mach/gpt.h>
22
23typedef struct {
24    gpt_t timestamp;
25    gpt_t timeout;
26} imx_timers_t;
27
28static inline uint64_t imx_get_time(imx_timers_t *timers)
29{
30    return gpt_get_time(&timers->timestamp);
31}
32
33static inline int imx_set_timeout(imx_timers_t *timers, uint64_t ns, bool periodic)
34{
35    return gpt_set_timeout(&timers->timeout, ns, periodic);
36}
37
38static inline void imx_start_timestamp(imx_timers_t *timers)
39{
40    gpt_start(&timers->timestamp);
41}
42
43static inline void imx_stop_timestamp(imx_timers_t *timers)
44{
45    gpt_stop(&timers->timestamp);
46}
47
48static inline void imx_stop_timeout(imx_timers_t *timers)
49{
50    gpt_stop(&timers->timeout);
51}
52
53static inline int imx_init_timer(gpt_t *gpt, ps_io_ops_t io_ops, ltimer_callback_fn_t user_callback,
54                                 void *user_callback_token, char *device_path)
55{
56    gpt_config_t config = {
57        .io_ops = io_ops,
58        .user_callback = user_callback,
59        .user_callback_token = user_callback_token,
60        .device_path = device_path,
61        .prescaler = GPT_PRESCALER
62    };
63    return gpt_init(gpt, config);
64}
65
66static inline int imx_init_timestamp(imx_timers_t *timers, ps_io_ops_t io_ops, ltimer_callback_fn_t user_callback,
67                                     void *user_callback_token)
68{
69    return imx_init_timer(&timers->timestamp, io_ops, user_callback, user_callback_token, GPT1_PATH);
70}
71
72static inline int imx_destroy_timestamp(imx_timers_t *timers)
73{
74    return gpt_destroy(&timers->timestamp);
75}
76
77static inline int imx_init_timeout(imx_timers_t *timers, ps_io_ops_t io_ops, ltimer_callback_fn_t user_callback,
78                                   void *user_callback_token)
79{
80    return imx_init_timer(&timers->timeout, io_ops, user_callback, user_callback_token, GPT2_PATH);
81}
82
83static inline int imx_destroy_timeout(imx_timers_t *timers)
84{
85    return gpt_destroy(&timers->timeout);
86}
87