1 /*
2 * Copyright (c) 2019-2025 Allwinner Technology Co., Ltd. ALL rights reserved.
3 *
4 * Allwinner is a trademark of Allwinner Technology Co.,Ltd., registered in
5 * the the people's Republic of China and other countries.
6 * All Allwinner Technology Co.,Ltd. trademarks are used with permission.
7 *
8 * DISCLAIMER
9 * THIRD PARTY LICENCES MAY BE REQUIRED TO IMPLEMENT THE SOLUTION/PRODUCT.
10 * IF YOU NEED TO INTEGRATE THIRD PARTY’S TECHNOLOGY (SONY, DTS, DOLBY, AVS OR MPEGLA, ETC.)
11 * IN ALLWINNERS’SDK OR PRODUCTS, YOU SHALL BE SOLELY RESPONSIBLE TO OBTAIN
12 * ALL APPROPRIATELY REQUIRED THIRD PARTY LICENCES.
13 * ALLWINNER SHALL HAVE NO WARRANTY, INDEMNITY OR OTHER OBLIGATIONS WITH RESPECT TO MATTERS
14 * COVERED UNDER ANY REQUIRED THIRD PARTY LICENSE.
15 * YOU ARE SOLELY RESPONSIBLE FOR YOUR USAGE OF THIRD PARTY’S TECHNOLOGY.
16 *
17 *
18 * THIS SOFTWARE IS PROVIDED BY ALLWINNER"AS IS" AND TO THE MAXIMUM EXTENT
19 * PERMITTED BY LAW, ALLWINNER EXPRESSLY DISCLAIMS ALL WARRANTIES OF ANY KIND,
20 * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING WITHOUT LIMITATION REGARDING
21 * THE TITLE, NON-INFRINGEMENT, ACCURACY, CONDITION, COMPLETENESS, PERFORMANCE
22 * OR MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23 * IN NO EVENT SHALL ALLWINNER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS, OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30 * OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include "sunxi_htimer.h"
34 #include "aw_common.h"
35 #include <stdlib.h>
36 #include <io.h>
37 #include <interrupt.h>
38
39 static struct sunxi_htimer *g_htimer;
40
41 static const int sunxi_htimer_irq_num[] =
42 {
43 SUNXI_IRQ_HSTIMER0,
44 SUNXI_IRQ_HSTIMER1,
45 };
46
47
sunxi_htimer_irq_handle(int dummy,void * data)48 static void sunxi_htimer_irq_handle(int dummy, void *data)
49 {
50 struct sunxi_htimer *timer = (struct sunxi_htimer *)data;
51
52 /* clear pending */
53 writel((0x1 << timer->timer_id), HTIMER_IRQ_ST_REG);
54
55 /*callback*/
56 if (timer->callback != NULL)
57 {
58 timer->callback(timer->param);
59 }
60 }
61
sunxi_htimer_sync(uint32_t timer)62 static void sunxi_htimer_sync(uint32_t timer)
63 {
64 uint32_t old = readl(HTIMER_CNTVAL_LO_REG(timer));
65
66 while ((old - readl(HTIMER_CNTVAL_LO_REG(timer))) < HTIMER_SYNC_TICKS)
67 {
68 int i = 10;
69 while (i--);
70 break;
71 }
72 }
73
sunxi_htimer_stop(uint32_t timer)74 void sunxi_htimer_stop(uint32_t timer)
75 {
76 uint32_t val = readl(HTIMER_CTL_REG(timer));
77
78 writel(val & ~HTIMER_CTL_ENABLE, HTIMER_CTL_REG(timer));
79
80 sunxi_htimer_sync(timer);
81 }
82
sunxi_htimer_start(uint32_t timer,bool periodic)83 void sunxi_htimer_start(uint32_t timer, bool periodic)
84 {
85 uint32_t val = readl(HTIMER_CTL_REG(timer));
86
87 if (periodic)
88 {
89 val &= ~HTIMER_CTL_ONESHOT;
90 }
91 else
92 {
93 val |= HTIMER_CTL_ONESHOT;
94 }
95
96 val |= HTIMER_CTL_CLK_PRES(1); //100M
97
98 writel(val | HTIMER_CTL_ENABLE | HTIMER_CTL_RELOAD, HTIMER_CTL_REG(timer));
99 }
100
sunxi_htimer_setup(uint32_t tick,uint32_t timer)101 static void sunxi_htimer_setup(uint32_t tick, uint32_t timer)
102 {
103 writel(tick, HTIMER_INTVAL_LO_REG(timer));
104 }
105
sunxi_htimer_set_oneshot(uint32_t delay_us,uint32_t timer,timer_callback callback,void * callback_param)106 int sunxi_htimer_set_oneshot(uint32_t delay_us, uint32_t timer, timer_callback callback, void *callback_param)
107 {
108 uint32_t tick = delay_us * 100;
109
110 if (tick < g_htimer[timer].min_delta_ticks || tick > g_htimer[timer].max_delta_ticks)
111 {
112 HTIMER_INFO("not support!\n");
113 return -1;
114 }
115
116 if (callback != NULL)
117 {
118 g_htimer[timer].callback = callback;
119 g_htimer[timer].param = callback_param;
120 }
121
122 sunxi_htimer_stop(timer);
123
124 sunxi_htimer_setup(tick, timer);
125
126 sunxi_htimer_start(timer, false);
127 }
128
sunxi_htimer_set_periodic(uint32_t delay_us,uint32_t timer,timer_callback callback,void * callback_param)129 int sunxi_htimer_set_periodic(uint32_t delay_us, uint32_t timer, timer_callback callback, void *callback_param)
130 {
131 uint32_t tick = delay_us * 100;
132
133 if (tick < g_htimer[timer].min_delta_ticks || tick > g_htimer[timer].max_delta_ticks)
134 {
135 HTIMER_INFO("not support!\n");
136 return -1;
137 }
138
139 if (callback != NULL)
140 {
141 g_htimer[timer].callback = callback;
142 g_htimer[timer].param = callback_param;
143 }
144
145 sunxi_htimer_stop(timer);
146
147 sunxi_htimer_setup(tick, timer);
148
149 sunxi_htimer_start(timer, true);
150
151 }
152
sunxi_htimer_init(void)153 void sunxi_htimer_init(void)
154 {
155 int i;
156 struct sunxi_htimer *timer = NULL;
157 uint32_t val;
158 uint32_t size = ARRAY_SIZE(sunxi_htimer_irq_num);
159
160 for (i = 0; i < size; i++)
161 {
162 /* disable all hrtimer */
163 val = readl(HTIMER_CTL_REG(i));
164 writel(val & ~HTIMER_CTL_ENABLE, HTIMER_CTL_REG(i));
165 }
166
167 /* clear pending */
168 writel(0x3, HTIMER_IRQ_ST_REG);
169
170 timer = (struct sunxi_htimer *)malloc(size * sizeof(struct sunxi_htimer));
171 if (timer == NULL)
172 {
173 HTIMER_INFO("alloc memory error!\n");
174 return;
175 }
176
177 for (i = 0; i < size; i++)
178 {
179 timer[i].timer_id = i;
180 timer[i].clk_rate = 200000000; //ahb1,should get form clk driver
181 timer[i].irq = sunxi_htimer_irq_num[i];
182 timer[i].min_delta_ticks = HTIMER_SYNC_TICKS;
183 timer[i].max_delta_ticks = 0xffffffff;
184 timer[i].callback = NULL;
185 timer[i].param = NULL;
186 irq_request(timer[i].irq, sunxi_htimer_irq_handle, (void *)&timer[i]);
187 }
188
189 /*enable timer irq*/
190 for (i = 0; i < size; i++)
191 {
192 val = readl(HTIMER_IRQ_EN_REG);
193 val |= HTIMER_IRQ_EN(i);
194 writel(val, HTIMER_IRQ_EN_REG);
195 }
196
197 /* enable irq */
198 for (i = 0; i < size; i++)
199 {
200 irq_enable(timer[i].irq);
201 }
202
203 g_htimer = timer;
204
205 }
206
207