66#include "fe300prci/fe300prci_driver.h"
77#include <unistd.h>
88
9- #define rdmcycle (x ) { \
10- uint32_t lo, hi, hi2; \
11- __asm__ __volatile__ ("1:\n\t" \
12- "csrr %0, mcycleh\n\t" \
13- "csrr %1, mcycle\n\t" \
14- "csrr %2, mcycleh\n\t" \
15- "bne %0, %2, 1b\n\t" \
16- : "=r" (hi), "=r" (lo), "=r" (hi2)) ; \
17- *(x) = lo | ((uint64_t) hi << 32); \
9+ #define rdmcycle (x ) { \
10+ uint32_t lo, hi, hi2; \
11+ __asm__ __volatile__ ("1:\n\t" \
12+ "csrr %0, mcycleh\n\t" \
13+ "csrr %1, mcycle\n\t" \
14+ "csrr %2, mcycleh\n\t" \
15+ "bne %0, %2, 1b\n\t" \
16+ : "=r" (hi), "=r" (lo), "=r" (hi2)) ; \
17+ *(x) = lo | ((uint64_t) hi << 32); \
1818 }
1919
2020uint32_t PRCI_measure_mcycle_freq (uint32_t mtime_ticks , uint32_t mtime_freq )
@@ -34,49 +34,49 @@ uint32_t PRCI_measure_mcycle_freq(uint32_t mtime_ticks, uint32_t mtime_freq)
3434 do {
3535 start_mtime = CLINT_REG (CLINT_MTIME );
3636 } while (start_mtime == tmp );
37-
37+
3838 uint64_t start_mcycle ;
3939 rdmcycle (& start_mcycle );
40-
40+
4141 while (CLINT_REG (CLINT_MTIME ) < end_mtime ) ;
42-
42+
4343 uint64_t end_mcycle ;
4444 rdmcycle (& end_mcycle );
4545 uint32_t difference = (uint32_t ) (end_mcycle - start_mcycle );
4646
4747 uint64_t freq = ((uint64_t ) difference * mtime_freq ) / mtime_ticks ;
4848 return (uint32_t ) freq & 0xFFFFFFFF ;
49-
49+
5050}
51-
51+
5252
5353void PRCI_use_hfrosc (int div , int trim )
5454{
5555 // Make sure the HFROSC is running at its default setting
5656 // It is OK to change this even if we are running off of it.
57-
57+
5858 PRCI_REG (PRCI_HFROSCCFG ) = (ROSC_DIV (div ) | ROSC_TRIM (trim ) | ROSC_EN (1 ));
5959
6060 while ((PRCI_REG (PRCI_HFROSCCFG ) & ROSC_RDY (1 )) == 0 );
61-
61+
6262 PRCI_REG (PRCI_PLLCFG ) &= ~PLL_SEL (1 );
6363}
6464
6565void PRCI_use_pll (int refsel , int bypass ,
66- int r , int f , int q , int finaldiv ,
67- int hfroscdiv , int hfrosctrim )
66+ int r , int f , int q , int finaldiv ,
67+ int hfroscdiv , int hfrosctrim )
6868{
6969 // Ensure that we aren't running off the PLL before we mess with it.
7070 if (PRCI_REG (PRCI_PLLCFG ) & PLL_SEL (1 )) {
7171 // Make sure the HFROSC is running at its default setting
7272 PRCI_use_hfrosc (4 , 16 );
7373 }
74-
74+
7575 // Set PLL Source to be HFXOSC if desired.
7676 uint32_t config_value = 0 ;
7777
7878 config_value |= PLL_REFSEL (refsel );
79-
79+
8080 if (bypass ) {
8181 // Bypass
8282 config_value |= PLL_BYPASS (1 );
@@ -87,14 +87,14 @@ void PRCI_use_pll(int refsel, int bypass,
8787 // Set our Final output divide to divide-by-1:
8888 PRCI_REG (PRCI_PLLDIV ) = (PLL_FINAL_DIV_BY_1 (1 ) | PLL_FINAL_DIV (0 ));
8989 } else {
90-
90+
9191 // To overclock, use the hfrosc
9292 if (hfrosctrim >= 0 && hfroscdiv >= 0 ) {
9393 PRCI_use_hfrosc (hfroscdiv , hfrosctrim );
9494 }
95-
95+
9696 // Set DIV Settings for PLL
97-
97+
9898 // (Legal values of f_REF are 6-48MHz)
9999
100100 // Set DIVR to divide-by-2 to get 8MHz frequency
@@ -132,7 +132,7 @@ void PRCI_use_pll(int refsel, int bypass,
132132 // So wait 4 ticks of RTC.
133133 uint32_t now = CLINT_REG (CLINT_MTIME );
134134 while (CLINT_REG (CLINT_MTIME ) - now < 4 ) ;
135-
135+
136136 // Now it is safe to check for PLL Lock
137137 while ((PRCI_REG (PRCI_PLLCFG ) & PLL_LOCK (1 )) == 0 );
138138
@@ -146,7 +146,7 @@ void PRCI_use_pll(int refsel, int bypass,
146146 if (refsel ) {
147147 PRCI_REG (PRCI_HFROSCCFG ) &= ~ROSC_EN (1 );
148148 }
149-
149+
150150}
151151
152152void PRCI_use_default_clocks ()
@@ -160,15 +160,15 @@ void PRCI_use_default_clocks()
160160
161161void PRCI_use_hfxosc (uint32_t finaldiv )
162162{
163-
163+
164164 PRCI_use_pll (1 , // Use HFXTAL
165- 1 , // Bypass = 1
166- 0 , // PLL settings don't matter
167- 0 , // PLL settings don't matter
168- 0 , // PLL settings don't matter
169- finaldiv ,
170- -1 ,
171- -1 );
165+ 1 , // Bypass = 1
166+ 0 , // PLL settings don't matter
167+ 0 , // PLL settings don't matter
168+ 0 , // PLL settings don't matter
169+ finaldiv ,
170+ -1 ,
171+ -1 );
172172}
173173
174174// This is a generic function, which
@@ -199,28 +199,28 @@ uint32_t PRCI_set_hfrosctrim_for_f_cpu(uint32_t f_cpu, PRCI_freq_target target )
199199 uint32_t desired_hfrosc_freq = (f_cpu / 16 );
200200
201201 PRCI_use_hfrosc (hfroscdiv , hfrosctrim );
202-
202+
203203 // Ignore the first run (for icache reasons)
204204 uint32_t cpu_freq = PRCI_measure_mcycle_freq (3000 , RTC_FREQ );
205205
206206 cpu_freq = PRCI_measure_mcycle_freq (3000 , RTC_FREQ );
207207 uint32_t prev_freq = cpu_freq ;
208-
208+
209209 while ((cpu_freq < desired_hfrosc_freq ) && (hfrosctrim < 0x1F )){
210210 prev_trim = hfrosctrim ;
211211 prev_freq = cpu_freq ;
212212 hfrosctrim ++ ;
213213 PRCI_use_hfrosc (hfroscdiv , hfrosctrim );
214214 cpu_freq = PRCI_measure_mcycle_freq (3000 , RTC_FREQ );
215- }
215+ }
216216
217217 // We couldn't go low enough
218218 if (prev_freq > desired_hfrosc_freq ){
219219 PRCI_use_pll (0 , 0 , 1 , 31 , 1 , 1 , hfroscdiv , prev_trim );
220220 cpu_freq = PRCI_measure_mcycle_freq (1000 , RTC_FREQ );
221221 return cpu_freq ;
222222 }
223-
223+
224224 // We couldn't go high enough
225225 if (cpu_freq < desired_hfrosc_freq ){
226226 PRCI_use_pll (0 , 0 , 1 , 31 , 1 , 1 , hfroscdiv , prev_trim );
0 commit comments