2 * arc-timer.c -- provide API for ARC timers.
4 * Copyright (c) 2024 Synopsys Inc.
6 * The authors hereby grant permission to use, copy, modify, distribute,
7 * and license this software and its documentation for any purpose, provided
8 * that existing copyright notices are retained in all copies and that this
9 * notice is included verbatim in any distributions. No written agreement,
10 * license, or royalty fee is required for any of the authorized uses.
11 * Modifications to this software may be copyrighted by their authors
12 * and need not follow the licensing terms described here, provided that
13 * the new terms are clearly indicated on the first page of each file where
18 #include "arc-specific.h"
20 #define ARC_TIM_BUILD 0x75
21 #define ARC_TIM_BUILD_VER_MASK 0x00FF
22 #define ARC_TIM_BUILD_TIM0_FL 0x0100
23 #define ARC_TIM_BUILD_TIM1_FL 0x0200
25 #define ARC_TIM_COUNT0 0x21
26 #define ARC_TIM_CONTROL0 0x22
27 #define ARC_TIM_LIMIT0 0x23
29 #define ARC_TIM_COUNT1 0x100
30 #define ARC_TIM_CONTROL1 0x101
31 #define ARC_TIM_LIMIT1 0x102
33 #define ARC_TIM_CONTROL_NH_FL 0x0002
35 /* Timer used by '_default' functions. */
36 const unsigned int arc_timer_default
= 0;
38 /* Check if given timer exists. */
40 _arc_timer_present (unsigned int tim
)
42 unsigned int bcr
= read_aux_reg (ARC_TIM_BUILD
);
43 unsigned int ver
= bcr
& ARC_TIM_BUILD_VER_MASK
;
50 return ((bcr
& ARC_TIM_BUILD_TIM0_FL
) != 0);
52 return ((bcr
& ARC_TIM_BUILD_TIM1_FL
) != 0);
57 /* Get raw value of a given timer. */
59 _arc_timer_read (unsigned int tim
)
61 if (_arc_timer_present (tim
))
64 return read_aux_reg (ARC_TIM_COUNT0
);
66 return read_aux_reg (ARC_TIM_COUNT1
);
73 * Set default values to a given timer.
74 * Defaults: Not Halted bit is set, limit is 0xFFFFFFFF, count set to 0.
77 _arc_timer_reset (unsigned int tim
)
79 unsigned int ctrl
, tim_control
, tim_count
, tim_limit
;
81 if (_arc_timer_present (tim
))
85 tim_control
= ARC_TIM_CONTROL0
;
86 tim_count
= ARC_TIM_COUNT0
;
87 tim_limit
= ARC_TIM_LIMIT0
;
91 tim_control
= ARC_TIM_CONTROL1
;
92 tim_count
= ARC_TIM_COUNT1
;
93 tim_limit
= ARC_TIM_LIMIT1
;
100 ctrl
= read_aux_reg (tim_control
);
101 /* Disable timer interrupt when programming. */
102 write_aux_reg (0, tim_control
);
103 /* Default limit is 24-bit, increase it to 32-bit. */
104 write_aux_reg (0xFFFFFFFF, tim_limit
);
105 /* Set NH bit to count only when processor is running. */
106 write_aux_reg (ctrl
| ARC_TIM_CONTROL_NH_FL
, tim_control
);
107 write_aux_reg (0, tim_count
);
112 /* Check if arc_timer_default exists. */
114 _arc_timer_default_present (void)
116 return _arc_timer_present (arc_timer_default
);
119 /* Read arc_timer_default value. */
121 _arc_timer_default_read (void)
123 return _arc_timer_read (arc_timer_default
);
126 /* Reset arc_timer_default. */
128 _arc_timer_default_reset (void)
130 _arc_timer_reset (arc_timer_default
);