]> git.cryptolib.org Git - arm-crypto-lib.git/blob - test_src/sysclock.c
switching to dedicated endian switching function
[arm-crypto-lib.git] / test_src / sysclock.c
1 /* sysclock.c */
2 /*
3     This file is part of the OpenARMWare.
4     Copyright (C) 2006-2010  Daniel Otte (daniel.otte@rub.de)
5
6     This program is free software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 3 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <stdint.h>
21 #include "hw_regs.h"
22 #include "sysclock.h"
23
24 #define CRYSTAL_FREQ 16000000UL
25 #define CRYSTAL_CODE 0x15 /* 16 MHz */
26
27 #define PIOSC_FREQ 16000000UL
28
29 void sysclk_set_rawclock(void){
30         volatile uint32_t tmp_rcc;
31         tmp_rcc = 0; //HW_REG(SYSCTL_BASE+RCC_OFFSET);
32         tmp_rcc &= ~(_BV(RCC_IOSCDIS) | _BV(RCC_MOSCDIS) | _BV(RCC_USESYSDIV));
33         tmp_rcc |= _BV(RCC_BYPASS) | _BV(RCC_PWRDN);
34         tmp_rcc &= ~(3<<RCC_OSCSRC);
35         tmp_rcc |=  (0<<RCC_OSCSRC);
36         HW_REG(SYSCTL_BASE+RCC_OFFSET) = tmp_rcc;
37 //      tmp_rcc |= _BV(RCC_PWRDN);
38         HW_REG(SYSCTL_BASE+RCC_OFFSET) = tmp_rcc;
39         HW_REG(SYSCTL_BASE+RCC2_OFFSET) &= ~(_BV(31));
40
41 }
42
43 void sysclk_mosc_verify_enable(void){
44         HW_REG(SYSCTL_BASE+MOSCCTL_OFFSET) |= 1; // turn on main oscillator verify circuit
45 }
46
47 void sysclk_mosc_verify_disable(void){
48         HW_REG(SYSCTL_BASE+MOSCCTL_OFFSET) &= ~1UL; // turn on main oscillator verify circuit
49 }
50
51 void sysclk_set_freq(uint8_t freq_id){
52         uint32_t rcc1, rcc2=0;
53         sysclk_set_rawclock();
54         rcc1 = HW_REG(SYSCTL_BASE+RCC_OFFSET);
55 //      rcc2 = HW_REG(SYSCTL_BASE+RCC2_OFFSET);
56         rcc1 &= ~(0x1f<<RCC_XTAL);
57         rcc1 |= CRYSTAL_CODE<<RCC_XTAL;
58         rcc2 = _BV(RCC2_USERCC2) | _BV(RCC2_PWRDN2) | _BV(RCC2_BYPASS2) | _BV(RCC2_USBPWRDN); /* OSCSRC2 is set to 0 */
59         HW_REG(SYSCTL_BASE+RCC_OFFSET)  = rcc1;
60         HW_REG(SYSCTL_BASE+RCC2_OFFSET) = rcc2;
61         rcc2 &= ~_BV(RCC2_PWRDN2);
62         HW_REG(SYSCTL_BASE+RCC2_OFFSET) = rcc2;
63         rcc2 |= _BV(RCC2_DIV400) | (freq_id<<RCC2_SYSDIV2LSB);
64         HW_REG(SYSCTL_BASE+RCC2_OFFSET) = rcc2;
65         while(!(HW_REG(SYSCTL_BASE+RIS_OFFSET)&_BV(RIS_PLLLRIS))){
66         }
67 //      return;
68         rcc2 &= ~_BV(RCC2_BYPASS2);
69         HW_REG(SYSCTL_BASE+RCC2_OFFSET) = rcc2;
70 }
71
72 void sysclk_set_80MHz(void){
73         sysclk_set_freq(SYS_FREQ_80MHZ000);
74 }
75
76 uint32_t sysclk_get_freq(void){
77         uint32_t rcc1, rcc2, basefreq=400000000UL, divider=1;
78         const uint32_t bypass_freq[] = {
79                         CRYSTAL_FREQ, PIOSC_FREQ,  PIOSC_FREQ/4, 30000,
80                         0, 0, 4194304, 32768 };
81         rcc1 = HW_REG(SYSCTL_BASE+RCC_OFFSET);
82         rcc2 = HW_REG(SYSCTL_BASE+RCC2_OFFSET);
83         if(rcc2&_BV(RCC2_USERCC2)){
84                 /* use RCC2 */
85                 if(rcc2&_BV(RCC2_BYPASS2)){
86                         basefreq = bypass_freq[(rcc2>>RCC2_OSCSR2)&0x07];
87                 }
88                 if(rcc2&_BV(RCC2_DIV400)){
89                         divider = ((rcc2>>RCC2_SYSDIV2LSB)&0x7F)+1;
90                 }else{
91                         divider = ((rcc2>>RCC2_SYSDIV2)&0x3F)+1;
92                 }
93         }else{
94                 /* use RCC */
95                 if(rcc1&_BV(RCC_BYPASS)){
96                          basefreq = bypass_freq[(rcc1>>RCC_OSCSRC)&0x03];
97                 }
98                 divider = ((rcc1>>RCC_SYSDIV)&0xf)+1;
99         }
100         return basefreq/divider;
101 }