• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * osal_time.c
3  *
4  * osal driver
5  *
6  * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
7  *
8  * This software is licensed under the terms of the GNU General Public
9  * License version 2, as published by the Free Software Foundation, and
10  * may be copied, distributed, and modified under those terms.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  */
18 
19 #include "osal_time.h"
20 #include <linux/delay.h>
21 #include <linux/export.h>
22 #include <linux/rtc.h>
23 #include <linux/string.h>
24 #include <linux/timekeeping.h>
25 #include "hdf_log.h"
26 #include "osal_math.h"
27 #include "securec.h"
28 
29 #define HDF_LOG_TAG osal_time
30 
31 #define TM_SINCE_YEAR 1900
32 #define TM_MINUTE_UNIT 60
33 
OsalGetTime(OsalTimespec * time)34 int32_t OsalGetTime(OsalTimespec *time)
35 {
36 	struct timespec64 ts;
37 
38 	if (time == NULL) {
39 		HDF_LOGE("%s invalid para", __func__);
40 		return HDF_ERR_INVALID_PARAM;
41 	}
42 
43 	(void)memset_s(&ts, sizeof(ts), 0, sizeof(ts));
44 	ktime_get_ts64(&ts);
45 	time->sec = ts.tv_sec;
46 	time->usec = ts.tv_nsec / HDF_KILO_UNIT;
47 
48 	return HDF_SUCCESS;
49 }
50 EXPORT_SYMBOL(OsalGetTime);
51 
OsalDiffTime(const OsalTimespec * start,const OsalTimespec * end,OsalTimespec * diff)52 int32_t OsalDiffTime(const OsalTimespec *start, const OsalTimespec *end, OsalTimespec *diff)
53 {
54 	uint32_t usec = 0;
55 	uint32_t sec = 0;
56 	if (start == NULL || end == NULL || diff == NULL) {
57 		HDF_LOGE("%s invalid para", __func__);
58 		return HDF_ERR_INVALID_PARAM;
59 	}
60 
61 	if (start->sec > end->sec) {
62 		HDF_LOGE("%s start time later then end time", __func__);
63 		return HDF_ERR_INVALID_PARAM;
64 	}
65 
66 	if (end->usec < start->usec) {
67 		usec = (HDF_KILO_UNIT * HDF_KILO_UNIT);
68 		sec = 1;
69 	}
70 	diff->usec = usec + end->usec - start->usec;
71 	diff->sec = end->sec - start->sec - sec;
72 
73 	return HDF_SUCCESS;
74 }
75 EXPORT_SYMBOL(OsalDiffTime);
76 
OsalSleep(uint32_t sec)77 void OsalSleep(uint32_t sec)
78 {
79 	msleep(sec * HDF_KILO_UNIT);
80 }
81 EXPORT_SYMBOL(OsalSleep);
82 
OsalMSleep(uint32_t mSec)83 void OsalMSleep(uint32_t mSec)
84 {
85 	msleep(mSec);
86 }
87 EXPORT_SYMBOL(OsalMSleep);
88 
OsalUDelay(uint32_t us)89 void OsalUDelay(uint32_t us)
90 {
91 	udelay(us);
92 }
93 EXPORT_SYMBOL(OsalUDelay);
94 
OsalMDelay(uint32_t ms)95 void OsalMDelay(uint32_t ms)
96 {
97 	mdelay(ms);
98 }
99 EXPORT_SYMBOL(OsalMDelay);
100 
OsalGetSysTimeMs()101 uint64_t OsalGetSysTimeMs()
102 {
103 	OsalTimespec time;
104 
105 	(void)memset_s(&time, sizeof(time), 0, sizeof(time));
106 	(void)OsalGetTime(&time);
107 
108     return (time.sec * HDF_KILO_UNIT + OsalDivS64(time.usec, HDF_KILO_UNIT));
109 }
110 EXPORT_SYMBOL(OsalGetSysTimeMs);
111