1 // FL: Obtained this code from Ivan Pang,
2 // https://bitbucket.itg.ti.com/projects/PROCESSOR-SDK/repos/board/browse/src/evmKeystone/board_utils.c
3 //
5 // FL: added this locally
6 #include <xdc/std.h>
7 #define uint32_t UInt32
9 #if 0 // FL: remove everything but code for PMCR
10 /**
11 * @file board_utils.c
12 *
13 * @brief This file includes the Keystone board level functions
14 */
15 /*
16 * Copyright (c) 2017, Texas Instruments Incorporated
17 * All rights reserved.
18 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
22 *
23 * * Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 *
26 * * Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
29 *
30 * * Neither the name of Texas Instruments Incorporated nor the names of
31 * its contributors may be used to endorse or promote products derived
32 * from this software without specific prior written permission.
33 *
34 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
35 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
36 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
37 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
38 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
39 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
40 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
41 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
42 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
43 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
44 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 */
47 #include <ti/csl/csl_bootcfg.h>
48 #include <ti/csl/csl_bootcfgAux.h>
49 #if defined (_TMS320C6X)
50 #include <ti/csl/csl_chipAux.h>
51 #endif
53 #include "board_utils.h"
55 /**
56 * \brief This API gets the input clock frequency.
57 *
58 * \param pll PLL controller type.
59 *
60 * \return input clock to the PLL controller.
61 *
62 * \note This API gets the input clock frequency based on the
63 * PLL controller type.\n
64 */
65 /** ============================================================================
66 * @n@b BOARD_getExternalClk
67 *
68 * @b Description
69 * @n This function is used to get the exteranl clock frequency.
70 *
71 *
72 * @b Arguments
73 * @verbatim
74 pll Specifies the PLL controller type.
75 @endverbatim
76 *
77 * <b> Return Value </b>
78 * @n external clock frequency to the PLL controller, if 0, means failure
79 *
80 * <b> Pre Condition </b>
81 * @n None
82 *
83 * <b> Post Condition </b>
84 * @n None
85 *
86 * @b Affects
87 * @n None.
88 *
89 * @b Example
90 * @verbatim
91 uint32_t ext_clk;
93 ext_clk = BOARD_getExternalClk(CSL_PLL_SYS);
95 @endverbatim
96 * ===========================================================================
97 */
98 uint32_t BOARD_getExternalClk(CSL_PllcType pll)
99 {
100 return Board_ext_clk[pll];
101 }
102 #endif
104 /** ============================================================================
105 * @n@b BOARD_initPerfCounters
106 *
107 * @b Description
108 * @n This function enables the A15 performance counters.
109 *
110 *
111 * @b Arguments
113 * <b> Return Value </b>
114 * @n None
115 *
116 * <b> Pre Condition </b>
117 * @n None
118 *
119 * <b> Post Condition </b>
120 * @n None
121 *
122 * @b Affects
123 * @n None.
124 *
125 * @b Example
126 * @verbatim
127 BOARD_initPerfCounters();
128 @endverbatim
129 * ===========================================================================
130 */
131 void BOARD_initPerfCounters()
132 {
133 #if defined (_TMS320C6X) || defined(__TI_ARM_V7M4__)
134 // Do nothing for C6x and M4 cores
135 #else
136 /* PMCR
137 31......24 23......16 15....11 10.....6 5 4 3 2 1 0
138 IMP IDCODE N Res DP X D C P E
139 [31:24] IMP: Implementer code; read-only
140 [23:16] IDCODE: Identification code; read-only
141 [15:11] N: Number of event counters; read-only
142 [10:6] Reserved
143 [5] DP: Disable cycle counter in prohibited regions; r/w
144 [4] X: Export enable; r/w
145 [3] D: Clock divider - PMCCNTR counts every 64 clock cycles when enabled; r/w
146 [2] C: Clock counter reset; write-only
147 [1] P: Event counter reset; write-only
148 [0] E: Enable all counters; r/w
149 */
150 __asm__ __volatile__ ("MCR p15, 0, %0, c9, c12, 0\t\n" :: "r"(0x17));
152 /* PMCNTENSET - Count Enable Set Register */
153 __asm__ __volatile__ ("MCR p15, 0, %0, c9, c12, 1\t\n" :: "r"(0x8000000f));
155 /* PMOVSR - Overflow Flag Status Register */
156 __asm__ __volatile__ ("MCR p15, 0, %0, c9, c12, 3\t\n" :: "r"(0x8000000f));
157 #endif
158 }
160 //static uint32_t readTime32(void) // FL: static function??
161 uint32_t readTime32(void)
162 {
163 uint32_t timeVal;
165 #if defined (_TMS320C6X)
166 timeVal = TSCL;
167 #else
168 __asm__ __volatile__ ("MRC p15, 0, %0, c9, c13, 0\t\n": "=r"(timeVal));
169 #endif
170 return timeVal;
171 }
173 #if 0 // FL: remove everything but code for PMCR
174 /** ============================================================================
175 * @n@b BOARD_delay
176 *
177 * @b Description
178 * @n This function delays a certain period of time in micro seconds.
179 *
180 *
181 * @b Arguments
182 * @verbatim
183 usecs Specifies the time to delay in micro second.
184 @endverbatim
185 * @n To prevent 32-bit counter roll over, the delay time should be less than 2^32/freq_in_mhz (usec)
186 * e.g. for 1000Mhz PLL clock, delay time should be less than ~4.2 seconds.
187 *
188 * <b> Return Value </b>
189 * @n None
190 *
191 * <b> Pre Condition </b>
192 * @n None
193 *
194 * <b> Post Condition </b>
195 * @n None
196 *
197 * @b Affects
198 * @n None.
199 *
200 * @b Example
201 * @verbatim
202 uint32_t delay = 5;
204 BOARD_delay(delay);
206 @endverbatim
207 * ===========================================================================
208 */
209 void BOARD_delay(uint32_t usecs)
210 {
211 uint32_t ext_clk, freq, start, delayCount;
212 CSL_PllcType pllType;
214 #if defined (_TMS320C6X)
215 pllType = CSL_PLL_SYS;
216 CSL_chipWriteTSCL(0);
217 #else
218 #if defined(SOC_K2E)
219 pllType = CSL_PLL_SYS;
220 #else
221 pllType = CSL_PLL_ARM;
222 #endif
223 #endif
225 start = readTime32();
226 ext_clk = BOARD_getExternalClk(pllType);
227 freq = CSL_BootCfgGetPllFreq(pllType, ext_clk);
229 delayCount = usecs * (freq / 1000000);
230 while ((readTime32() - start) < delayCount);
231 }
232 #endif