SYS/BIOS  7.00
SecureTS.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2022 Texas Instruments Incorporated - https://www.ti.com
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * ======== SecureTS.h ========
35  */
36 
37 #ifndef ti_spe_SecureTS__include
38 #define ti_spe_SecureTS__include
39 
40 #include <stddef.h>
41 #include <stdint.h>
42 
43 #if defined(__GNUC__) || defined(__IAR_SYSTEMS_ICC__)
44 #ifndef __STATIC_INLINE
45 #define __STATIC_INLINE static inline
46 #endif
47 #elif defined(__clang__) && defined(__ti__)
48 #define __STATIC_INLINE static __inline
49 #endif
50 
51 #if defined (__cplusplus)
52 extern "C" {
53 #endif
54 
55 #define HWREG(x) (*((volatile unsigned long *)(x)))
56 
57 #define AON_RTC_BASE 0x40092000 // AON_RTC
58 
59 // Second Counter Value, Integer Part
60 #define AON_RTC_O_SEC 0x00000008
61 
62 // Second Counter Value, Fractional Part
63 #define AON_RTC_O_SUBSEC 0x0000000C
64 
65 
66 /*
67  * ======== AONRTCCurrent64BitValueGet ========
68  * Implementation copied from driverlib to avoid secure image
69  * dependency on driverlib.
70  */
71 
72 __STATIC_INLINE uint64_t
74 {
75  union {
76  uint64_t returnValue ;
77  uint32_t secAndSubSec[ 2 ] ;
78  } currentRtc ;
79  uint32_t ui32SecondSecRead ;
80 
81  // Reading SEC both before and after SUBSEC in order to detect if SEC incremented while reading SUBSEC
82  // If SEC incremented, we can't be sure which SEC the SUBSEC belongs to, so repeating the sequence then.
83  do {
84  currentRtc.secAndSubSec[ 1 ] = HWREG( AON_RTC_BASE + AON_RTC_O_SEC );
85  currentRtc.secAndSubSec[ 0 ] = HWREG( AON_RTC_BASE + AON_RTC_O_SUBSEC );
86  ui32SecondSecRead = HWREG( AON_RTC_BASE + AON_RTC_O_SEC );
87  } while ( currentRtc.secAndSubSec[ 1 ] != ui32SecondSecRead );
88 
89  return ( currentRtc.returnValue );
90 }
91 
92 /*
93  * ======== SecureTS_get32 ========
94  * returns the LS 32 bits of the 48 bit SEC + SUBSEC registers
95  *
96  * This API is available to both non-secure and secure code
97  */
98 __attribute__ ((always_inline)) __STATIC_INLINE
99 uint32_t SecureTS_get32()
100 {
101  return (AONRTCCurrent64BitValueGet() >> 16);
102 }
103 
104 /*
105  * ======== SecureTS_get ========
106  * returns a 48 bit coherent concatenation of the current
107  * SEC and SUBSEC register values from the SYSTEM RTC
108  *
109  * This API is available to both non-secure and secure code
110  */
111 __attribute__ ((always_inline)) __STATIC_INLINE
112 uint64_t SecureTS_get()
113 {
114  return (AONRTCCurrent64BitValueGet() >> 16);
115 }
116 
117 #if defined (__cplusplus)
118 }
119 #endif
120 
121 #endif /* ti_spe_SecureTS__include */
122 
#define AON_RTC_O_SEC
Definition: SecureTS.h:60
__attribute__((always_inline)) __STATIC_INLINE uint32_t SecureTS_get32()
Definition: SecureTS.h:98
#define AON_RTC_BASE
Definition: SecureTS.h:57
__STATIC_INLINE uint64_t AONRTCCurrent64BitValueGet(void)
Definition: SecureTS.h:73
#define HWREG(x)
Definition: SecureTS.h:55
#define AON_RTC_O_SUBSEC
Definition: SecureTS.h:63
© Copyright 1995-2022, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale