• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 C-SKY Microsystems Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /******************************************************************************
18  * @file     dw_timer.c
19  * @brief    CSI Source File for timer Driver
20  * @version  V1.0
21  * @date     02. June 2017
22  ******************************************************************************/
23 
24 #include <csi_config.h>
25 #include "drv_timer.h"
26 #include "dw_timer.h"
27 #if defined CONFIG_CHIP_ERAGON3 || defined CONFIG_CHIP_SMARTH_MMU || defined CONFIG_CHIP_SMARTH_610 || defined CONFIG_CHIP_SMARTH_610M
28 #include <drv_intc.h>
29 #endif
30 #include "soc.h"
31 #include "csi_core.h"
32 
33 #define ERR_TIMER(errno) (CSI_DRV_ERRNO_TIMER_BASE | errno)
34 
35 #define TIMER_NULL_PARAM_CHK(para)  HANDLE_PARAM_CHK(para, ERR_TIMER(DRV_ERROR_PARAMETER))
36 
37 typedef struct {
38 #ifdef CONFIG_LPM
39     uint8_t timer_power_status;
40     uint32_t timer_regs_saved[2];
41 #endif
42     uint32_t base;
43     uint32_t irq;
44     timer_event_cb_t cb_event;
45     uint32_t timeout;                  ///< the set time (us)
46     uint32_t timeout_flag;
47 } dw_timer_priv_t;
48 
49 extern int32_t target_get_timer_count(void);
50 extern int32_t target_get_timer(int32_t idx, uint32_t *base, uint32_t *irq);
51 
52 static dw_timer_priv_t timer_instance[CONFIG_TIMER_NUM];
53 /**
54   \brief      Make all the timers in the idle state.
55   \param[in]  pointer to timer register base
56 */
timer_deactive_control(dw_timer_reg_t * addr)57 static void timer_deactive_control(dw_timer_reg_t *addr)
58 {
59     /* stop the corresponding timer */
60     addr->TxControl &= ~DW_TIMER_TXCONTROL_ENABLE;
61     /* Disable interrupt. */
62     addr->TxControl |= DW_TIMER_TXCONTROL_INTMASK;
63 }
64 
dw_timer_irqhandler(int idx)65 void dw_timer_irqhandler(int idx)
66 {
67     dw_timer_priv_t *timer_priv = &timer_instance[idx];
68     timer_priv->timeout_flag = 1;
69 
70     dw_timer_reg_t *addr = (dw_timer_reg_t *)(timer_priv->base);
71 
72     addr->TxEOI;
73 
74     if (timer_priv->cb_event) {
75         return timer_priv->cb_event(idx, TIMER_EVENT_TIMEOUT);
76     }
77 
78 }
79 
80 #ifdef CONFIG_LPM
manage_clock(timer_handle_t handle,uint8_t enable)81 static void manage_clock(timer_handle_t handle, uint8_t enable)
82 {
83     if (handle == &timer_instance[0] || handle == &timer_instance[1]) {
84         drv_clock_manager_config(CLOCK_MANAGER_TIM, enable);
85     } else if (handle == &timer_instance[3] || handle == &timer_instance[2]) {
86         drv_clock_manager_config(CLOCK_MANAGER_TIM1, enable);
87     }
88 }
89 
do_prepare_sleep_action(timer_handle_t handle)90 static void do_prepare_sleep_action(timer_handle_t handle)
91 {
92     dw_timer_priv_t *timer_priv = (dw_timer_priv_t *)handle;
93     uint32_t *tbase = (uint32_t *)(timer_priv->base);
94     registers_save(timer_priv->timer_regs_saved, tbase, 1);
95     registers_save(&timer_priv->timer_regs_saved[1], tbase + 2, 1);
96 }
97 
do_wakeup_sleep_action(timer_handle_t handle)98 static void do_wakeup_sleep_action(timer_handle_t handle)
99 {
100     dw_timer_priv_t *timer_priv = (dw_timer_priv_t *)handle;
101     uint32_t *tbase = (uint32_t *)(timer_priv->base);
102     registers_restore(tbase, timer_priv->timer_regs_saved, 1);
103     registers_restore(tbase + 2, &timer_priv->timer_regs_saved[1], 1);
104 }
105 #endif
106 
107 /**
108   \brief       Initialize TIMER Interface. 1. Initializes the resources needed for the TIMER interface 2.registers event callback function
109   \param[in]   idx  instance timer index
110   \param[in]   cb_event  Pointer to \ref timer_event_cb_t
111   \return      pointer to timer instance
112 */
csi_timer_initialize(int32_t idx,timer_event_cb_t cb_event)113 timer_handle_t csi_timer_initialize(int32_t idx, timer_event_cb_t cb_event)
114 {
115     if (idx < 0 || idx >= CONFIG_TIMER_NUM) {
116         return NULL;
117     }
118 
119     uint32_t base = 0u;
120     uint32_t irq = 0u;
121 
122     int32_t real_idx = target_get_timer(idx, &base, &irq);
123 
124     if (real_idx != idx) {
125         return NULL;
126     }
127 
128     dw_timer_priv_t *timer_priv = &timer_instance[idx];
129     timer_priv->base = base;
130     timer_priv->irq  = irq;
131 
132     dw_timer_reg_t *addr = (dw_timer_reg_t *)(timer_priv->base);
133     timer_priv->timeout = DW_TIMER_INIT_DEFAULT_VALUE;
134 
135 #ifdef CONFIG_LPM
136     csi_timer_power_control(timer_priv, DRV_POWER_FULL);
137 #endif
138 
139     timer_deactive_control(addr);
140     timer_priv->cb_event = cb_event;
141 
142     if (cb_event != NULL) {
143 #if defined CONFIG_CHIP_ERAGON3 || defined CONFIG_CHIP_SMARTH_MMU || defined CONFIG_CHIP_SMARTH_610 || defined CONFIG_CHIP_SMARTH_610M
144         csi_intc_enable_irq(timer_priv->irq);
145 #else
146         csi_vic_enable_irq(timer_priv->irq);
147 #endif
148     }
149 
150     return (timer_handle_t)timer_priv;
151 }
152 
153 /**
154   \brief       De-initialize TIMER Interface. stops operation and releases the software resources used by the interface
155   \param[in]   handle timer handle to operate.
156   \return      error code
157 */
csi_timer_uninitialize(timer_handle_t handle)158 int32_t csi_timer_uninitialize(timer_handle_t handle)
159 {
160     TIMER_NULL_PARAM_CHK(handle);
161 
162     dw_timer_priv_t *timer_priv = (dw_timer_priv_t *)handle;
163     dw_timer_reg_t *addr = (dw_timer_reg_t *)(timer_priv->base);
164 
165     timer_deactive_control(addr);
166     timer_priv->cb_event = NULL;
167 
168 #if defined CONFIG_CHIP_ERAGON3 || defined CONFIG_CHIP_SMARTH_MMU || defined CONFIG_CHIP_SMARTH_610 || defined CONFIG_CHIP_SMARTH_610M
169     csi_intc_disable_irq(timer_priv->irq);
170 #else
171     csi_vic_disable_irq(timer_priv->irq);
172 #endif
173 #ifdef CONFIG_LPM
174     csi_timer_power_control(timer_priv, DRV_POWER_OFF);
175 #endif
176 
177     return 0;
178 }
179 
csi_timer_power_control(timer_handle_t handle,csi_power_stat_e state)180 int32_t csi_timer_power_control(timer_handle_t handle, csi_power_stat_e state)
181 {
182     TIMER_NULL_PARAM_CHK(handle);
183 #ifdef CONFIG_LPM
184     power_cb_t callback = {
185         .wakeup = do_wakeup_sleep_action,
186         .sleep = do_prepare_sleep_action,
187         .manage_clock = manage_clock
188     };
189     return drv_soc_power_control(handle, state, &callback);
190 #else
191     return ERR_TIMER(DRV_ERROR_UNSUPPORTED);
192 #endif
193 }
194 
195 /**
196   \brief       config timer mode.
197   \param[in]   handle timer handle to operate.
198   \param[in]   mode      \ref timer_mode_e
199   \return      error code
200 */
csi_timer_config(timer_handle_t handle,timer_mode_e mode)201 int32_t csi_timer_config(timer_handle_t handle, timer_mode_e mode)
202 {
203     TIMER_NULL_PARAM_CHK(handle);
204 
205     dw_timer_priv_t *timer_priv = handle;
206     dw_timer_reg_t *addr = (dw_timer_reg_t *)(timer_priv->base);
207 
208     switch (mode) {
209         case TIMER_MODE_FREE_RUNNING:
210             addr->TxControl &= ~DW_TIMER_TXCONTROL_MODE;
211             break;
212 
213         case TIMER_MODE_RELOAD:
214             addr->TxControl |= DW_TIMER_TXCONTROL_MODE;
215             break;
216 
217         default:
218             return ERR_TIMER(DRV_ERROR_PARAMETER);
219     }
220 
221     return 0;
222 }
223 
224 /**
225   \brief       Set timer.
226   \param[in]   instance  timer instance to operate.
227   \param[in]   timeout the timeout value in microseconds(us).
228   \return      error code
229 */
csi_timer_set_timeout(timer_handle_t handle,uint32_t timeout)230 int32_t csi_timer_set_timeout(timer_handle_t handle, uint32_t timeout)
231 {
232     TIMER_NULL_PARAM_CHK(handle);
233 
234     dw_timer_priv_t *timer_priv = handle;
235     timer_priv->timeout = timeout;
236     return 0;
237 }
238 
239 /**
240   \brief       Start timer.
241   \param[in]   handle timer handle to operate.
242   \return      error code
243 */
csi_timer_start(timer_handle_t handle)244 int32_t csi_timer_start(timer_handle_t handle)
245 {
246     TIMER_NULL_PARAM_CHK(handle);
247 
248     dw_timer_priv_t *timer_priv = handle;
249 
250     timer_priv->timeout_flag = 0;
251 
252     uint32_t min_us = drv_get_sys_freq() / 1000000;
253     uint32_t load;
254 
255     if (timer_priv->timeout > 0xffffffff / min_us) {
256         return ERR_TIMER(DRV_ERROR_PARAMETER);
257     }
258 
259     if (min_us) {
260         load = (uint32_t)(timer_priv->timeout * min_us);
261     } else {
262         load = (uint32_t)(((timer_priv->timeout) * drv_get_sys_freq()) / 1000000);
263     }
264 
265     dw_timer_reg_t *addr = (dw_timer_reg_t *)(timer_priv->base);
266 
267     if (timer_priv->timeout == 0) {
268         addr->TxLoadCount = 0xffffffff;                           /* load time(us) */
269     } else {
270         if ((addr->TxControl | 0x2) == 0x2) {
271             addr->TxLoadCount = 0xffffffff;                           /* load time(us) */
272         } else {
273             addr->TxLoadCount = load;                           /* load time(us) */
274         }
275     }
276 
277     addr->TxControl &= ~DW_TIMER_TXCONTROL_ENABLE;      /* disable the timer */
278     addr->TxControl |= DW_TIMER_TXCONTROL_ENABLE;       /* enable the corresponding timer */
279     addr->TxControl &= ~DW_TIMER_TXCONTROL_INTMASK;     /* enable interrupt */
280     return 0;
281 }
282 
283 /**
284   \brief       Stop timer.
285   \param[in]   handle timer handle to operate.
286   \return      error code
287 */
csi_timer_stop(timer_handle_t handle)288 int32_t csi_timer_stop(timer_handle_t handle)
289 {
290     TIMER_NULL_PARAM_CHK(handle);
291 
292     dw_timer_priv_t *timer_priv = handle;
293     dw_timer_reg_t *addr = (dw_timer_reg_t *)(timer_priv->base);
294 
295     addr->TxControl |= DW_TIMER_TXCONTROL_INTMASK;      /* enable interrupt */
296     addr->TxControl &= ~DW_TIMER_TXCONTROL_ENABLE;      /* disable the timer */
297 
298     return 0;
299 }
300 
301 /**
302   \brief       suspend timer.
303   \param[in]   instance  timer instance to operate.
304   \return      error code
305 */
csi_timer_suspend(timer_handle_t handle)306 int32_t csi_timer_suspend(timer_handle_t handle)
307 {
308     TIMER_NULL_PARAM_CHK(handle);
309 
310     return ERR_TIMER(DRV_ERROR_UNSUPPORTED);
311 }
312 
313 /**
314   \brief       resume timer.
315   \param[in]   handle timer handle to operate.
316   \return      error code
317 */
csi_timer_resume(timer_handle_t handle)318 int32_t csi_timer_resume(timer_handle_t handle)
319 {
320     TIMER_NULL_PARAM_CHK(handle);
321 
322     dw_timer_priv_t *timer_priv = handle;
323     dw_timer_reg_t *addr = (dw_timer_reg_t *)(timer_priv->base);
324 
325     addr->TxControl &= ~DW_TIMER_TXCONTROL_ENABLE;      /* stop the corresponding timer */
326     addr->TxControl &= DW_TIMER_TXCONTROL_ENABLE;       /* restart the corresponding timer */
327 
328     return 0;
329 }
330 
331 /**
332   \brief       get timer current value
333   \param[in]   handle timer handle to operate.
334   \param[out]   value     timer current value
335   \return      error code
336 */
csi_timer_get_current_value(timer_handle_t handle,uint32_t * value)337 int32_t csi_timer_get_current_value(timer_handle_t handle, uint32_t *value)
338 {
339     TIMER_NULL_PARAM_CHK(handle);
340     TIMER_NULL_PARAM_CHK(value);
341 
342     dw_timer_priv_t *timer_priv = handle;
343     dw_timer_reg_t *addr = (dw_timer_reg_t *)(timer_priv->base);
344 
345     *value = addr->TxCurrentValue;
346     return 0;
347 }
348 
349 /**
350   \brief       Get TIMER status.
351   \param[in]   handle timer handle to operate.
352   \return      TIMER status \ref timer_status_t
353 */
csi_timer_get_status(timer_handle_t handle)354 timer_status_t csi_timer_get_status(timer_handle_t handle)
355 {
356     timer_status_t timer_status = {0};
357 
358     if (handle == NULL) {
359         return timer_status;
360     }
361 
362     dw_timer_priv_t *timer_priv = handle;
363     dw_timer_reg_t *addr = (dw_timer_reg_t *)(timer_priv->base);
364 
365     if (addr->TxControl & DW_TIMER_TXCONTROL_ENABLE) {
366         timer_status.active = 1;
367     }
368 
369     if (timer_priv->timeout_flag == 1) {
370         timer_status.timeout = 1;
371     }
372 
373     return timer_status;
374 }
375 
376 /**
377   \brief       get timer reload value
378   \param[in]   handle timer handle to operate.
379   \param[out]   value    timer reload value
380   \return      error code
381 */
csi_timer_get_load_value(timer_handle_t handle,uint32_t * value)382 int32_t csi_timer_get_load_value(timer_handle_t handle, uint32_t *value)
383 {
384     TIMER_NULL_PARAM_CHK(handle);
385     TIMER_NULL_PARAM_CHK(value);
386 
387     dw_timer_priv_t *timer_priv = handle;
388     dw_timer_reg_t *addr = (dw_timer_reg_t *)(timer_priv->base);
389 
390     *value = addr->TxLoadCount;
391     return 0;
392 }
393