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   (12u)
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
23#define TIMESTAMP_INTERRUPT GPT1_INTERRUPT
24#define TIMEOUT_INTERRUPT GPT2_INTERRUPT
25
26#define TIMESTAMP_DEVICE_PADDR     GPT1_DEVICE_PADDR
27#define TIMEOUT_DEVICE_PADDR     GPT2_DEVICE_PADDR
28
29typedef struct {
30    gpt_t timestamp;
31    gpt_t timeout;
32} imx_timers_t;
33
34static inline uint64_t imx_get_time(imx_timers_t *timers)
35{
36    return gpt_get_time(&timers->timestamp);
37}
38
39static inline int imx_set_timeout(imx_timers_t *timers, uint64_t ns, bool periodic)
40{
41    return gpt_set_timeout(&timers->timeout, ns, periodic);
42}
43
44static inline void imx_start_timestamp(imx_timers_t *timers)
45{
46    gpt_start(&timers->timestamp);
47}
48
49static inline void imx_stop_timestamp(imx_timers_t *timers)
50{
51    gpt_stop(&timers->timestamp);
52}
53
54static inline void imx_stop_timeout(imx_timers_t *timers)
55{
56    gpt_stop(&timers->timeout);
57}
58
59static inline int imx_init_timer(gpt_t *gpt, ps_io_ops_t *io_ops, ltimer_callback_fn_t user_callback,
60                                 void *user_callback_token, char *device_path)
61{
62    gpt_config_t config = {
63        .io_ops = io_ops,
64        .user_callback = user_callback,
65        .user_callback_token = user_callback_token,
66        .device_path = device_path,
67        .prescaler = GPT_PRESCALER
68    };
69    return gpt_init(gpt, config);
70}
71
72static inline int imx_init_timestamp(imx_timers_t *timers, ps_io_ops_t *io_ops, ltimer_callback_fn_t user_callback,
73                                     void *user_callback_token)
74{
75    return imx_init_timer(&timers->timestamp, io_ops, user_callback, user_callback_token, GPT1_PATH);
76}
77
78static inline int imx_destroy_timestamp(imx_timers_t *timers)
79{
80    return gpt_destroy(&timers->timestamp);
81}
82
83static inline int imx_init_timeout(imx_timers_t *timers, ps_io_ops_t *io_ops, ltimer_callback_fn_t user_callback,
84                                   void *user_callback_token)
85{
86    return imx_init_timer(&timers->timeout, io_ops, user_callback, user_callback_token, GPT2_PATH);
87}
88
89static inline int imx_destroy_timeout(imx_timers_t *timers)
90{
91    return gpt_destroy(&timers->timestamp);
92}
93