SYS/BIOS  7.00
SecureTS.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2020 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__)
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) \
56  (*((volatile unsigned long *)(x)))
57 
58 #define AON_RTC_BASE 0x40092000 // AON_RTC
59 
60 // Second Counter Value, Integer Part
61 #define AON_RTC_O_SEC 0x00000008
62 
63 // Second Counter Value, Fractional Part
64 #define AON_RTC_O_SUBSEC 0x0000000C
65 
66 
67 /*
68  * ======== AONRTCCurrent64BitValueGet ========
69  * Implementation copied from driverlib to avoid secure image
70  * dependency on driverlib.
71  */
72 
73 __STATIC_INLINE uint64_t
75 {
76  union {
77  uint64_t returnValue ;
78  uint32_t secAndSubSec[ 2 ] ;
79  } currentRtc ;
80  uint32_t ui32SecondSecRead ;
81 
82  // Reading SEC both before and after SUBSEC in order to detect if SEC incremented while reading SUBSEC
83  // If SEC incremented, we can't be sure which SEC the SUBSEC belongs to, so repeating the sequence then.
84  do {
85  currentRtc.secAndSubSec[ 1 ] = HWREG( AON_RTC_BASE + AON_RTC_O_SEC );
86  currentRtc.secAndSubSec[ 0 ] = HWREG( AON_RTC_BASE + AON_RTC_O_SUBSEC );
87  ui32SecondSecRead = HWREG( AON_RTC_BASE + AON_RTC_O_SEC );
88  } while ( currentRtc.secAndSubSec[ 1 ] != ui32SecondSecRead );
89 
90  return ( currentRtc.returnValue );
91 }
92 
93 /*
94  * ======== SecureTS_get32 ========
95  * returns the LS 32 bits of the 48 bit SEC + SUBSEC registers
96  *
97  * This API is available to both non-secure and secure code
98  */
99 __attribute__ ((always_inline)) __STATIC_INLINE
100 uint32_t SecureTS_get32()
101 {
102  return (AONRTCCurrent64BitValueGet() >> 16);
103 }
104 
105 /*
106  * ======== SecureTS_get ========
107  * returns a 48 bit coherent concatenation of the current
108  * SEC and SUBSEC register values from the SYSTEM RTC
109  *
110  * This API is available to both non-secure and secure code
111  */
112 __attribute__ ((always_inline)) __STATIC_INLINE
113 uint64_t SecureTS_get()
114 {
115  return (AONRTCCurrent64BitValueGet() >> 16);
116 }
117 
118 #if defined (__cplusplus)
119 }
120 #endif
121 
122 #endif /* ti_spe_SecureTS__include */
123 
#define AON_RTC_O_SEC
Definition: SecureTS.h:61
__attribute__((always_inline)) __STATIC_INLINE uint32_t SecureTS_get32()
Definition: SecureTS.h:99
#define AON_RTC_BASE
Definition: SecureTS.h:58
__STATIC_INLINE uint64_t AONRTCCurrent64BitValueGet(void)
Definition: SecureTS.h:74
#define HWREG(x)
Definition: SecureTS.h:55
#define AON_RTC_O_SUBSEC
Definition: SecureTS.h:64
© Copyright 1995-2021, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale