?? clock.c
字號:
/* $Id: //depot/software/SDK/Triscend/a7hal/src/clock.c#23 $ *//* ********************************************************** * clock.c * Copyright(C) 2001-2003 Triscend Corporation. All Rights Reserved. * This file is licensed under the terms of Triscend SDK License Agreement. ********************************************************** */#include "hal_conf.h"#if A7HAL_USE_CLOCK/** * \file * Contains functions to get/set the CPU clock speed. * The functions within this file are only accessible if A7HAL_USE_CLOCK * is #defined as YES in hal_conf.h *//** \defgroup clock Clock Driver * @{ */#include "triscend_a7.h"#include "clock.h"#ifdef __KERNEL__#include <linux/autoconf.h>#ifndef CONFIG_OSC_FREQ #define CONFIG_OSC_FREQ 25000000#endif#endifstatic unsigned long a7hal_ring_freq = A7HAL_RING_FREQ;#ifdef __KERNEL__static unsigned long a7hal_xclk_freq = CONFIG_OSC_FREQ;#elsestatic unsigned long a7hal_xclk_freq = A7HAL_XCLK_FREQ;#endifstatic unsigned long a7hal_xtal_freq = A7HAL_XTAL_FREQ;/** * \brief Initialize the clock driver. * * This function is called from the a7hal_driverInit function * to initialize the clock driver. It calls a7hal_clock_reset * to set the default clock values. * * \return N/A * * \see a7hal_clock_reset, a7hal_driverInit */void a7hal_clock_init( void ){ a7hal_clock_reset( );}/** * \brief Get the clock device base address. * * \return This function always returns 0. * * \note The clock driver accesses a number of clock registers, * there is no one base address for this device. */unsigned long a7hal_clock_getDeviceAddress( void ){ return ( 0 );}/** * \brief Reset the clock driver. * * This function sets the the PLL, external clock, * and ring oscillator to their default values. * * \return N/A * * \note This function will not change the actual * clock speed, since this is controlled by hardware, * it does not write to any clock registers. This function * is for software configuration only. */void a7hal_clock_reset( void ){ a7hal_ring_freq = A7HAL_RING_FREQ;#ifdef __KERNEL__ a7hal_xclk_freq = CONFIG_OSC_FREQ;#else a7hal_xclk_freq = A7HAL_XCLK_FREQ;#endif a7hal_xtal_freq = A7HAL_XTAL_FREQ;}/** * \brief Get the CPU clock speed. * * This function is called to return the CPU clock frequency in Hz. * It can be called before or after RAM is initialized, if called * before RAM is initialized a non zero value MUST be specified for * the 'noRAM' parameter. In this case the clock frequency returned * will be based on the values of A7HAL_RING_FREQ, A7HAL_XCLK_FREQ, * and A7HAL_XTAL_FREQ defined in clock.h * * If the 'noRAM' parameter is zero, the clock frequency returned * will be based on the values set by a7hal_clock_setPllClock, * a7hal_clock_setXClock, and a7hal_clock_setRing. * * \param noRAM Should be non zero if this function is called before RAM is initialized * * \return Clock frequency in Hz. * * \see a7hal_clock_setPllClock, a7hal_clock_setXClock, a7hal_clock_setRing, a7hal_sdram_init * * \note The a7hal_clock_get... and a7hal_clock_set... function * calls use global variables to store the clock values. These * global variables will proberly not exist untill after the * RAM system has been initialized. For this reason, the 'noRAM' * parameter is used to tell this function to use the #defined * clock values in clock.h */unsigned long a7hal_clock_getFreq( int noRAM ){ unsigned int freq; unsigned long local_a7hal_ring_freq; unsigned long local_a7hal_xclk_freq; unsigned long local_a7hal_xtal_freq;#if defined(A7VE) || defined(A7VC) || defined(A7VT) || defined(A7VL) unsigned long feedbackDivider; unsigned long outputDivider; unsigned long referenceDivider;#else unsigned int scale;#endif /* If this function is called before the SDRAM * is initialized, we can not use any global vars * so we use the hard coded values for the clocks * defined in clock.h */ if ( noRAM ) { local_a7hal_ring_freq = A7HAL_RING_FREQ;#ifdef __KERNEL__ local_a7hal_xclk_freq = CONFIG_OSC_FREQ;#else local_a7hal_xclk_freq = A7HAL_XCLK_FREQ;#endif local_a7hal_xtal_freq = A7HAL_XTAL_FREQ; } else { local_a7hal_ring_freq = a7hal_ring_freq; local_a7hal_xclk_freq = a7hal_xclk_freq; local_a7hal_xtal_freq = a7hal_xtal_freq; } freq = local_a7hal_ring_freq; /* * if (clock is not ring) * if (clock is xtal/PLL) calculate freq from PLL settings. * else clock is ext.osc. and user MUST define clock freq. * else clock is ring and approximate freq = 20,000,000. */ if ( GET_BIT( SYS_CLOCK_CONTROL_REG, CLK_SEL_BIT ) ) { if ( GET_BIT( SYS_CLOCK_CONTROL_REG, PLL_SEL_BIT ) ) {#if defined(A7VE) || defined(A7VC) || defined(A7VT) || defined(A7VL) feedbackDivider = GET_FIELD( SYS_PLL_CONTROL_REG, PLL_CLKF_FIELD, NBITS_PLL_CLKF ) + 1; outputDivider = GET_FIELD( SYS_PLL_CONTROL_REG, PLL_CLKOD_FIELD, NBITS_PLL_CLKOD ) + 1; referenceDivider = GET_FIELD( SYS_PLL_CONTROL_REG, PLL_CLKR_FIELD, NBITS_PLL_CLKR ) + 1; /* * Is the input to the PLL the 32KHz xtal or the external osc. */ if ( GET_BIT( SYS_PLL_CONTROL_REG, PLL_REFSEL_BIT ) ) { freq = ( local_a7hal_xclk_freq / ( referenceDivider * outputDivider )) * feedbackDivider; } else { freq = ( local_a7hal_xtal_freq * feedbackDivider ) / ( referenceDivider * outputDivider ); }#else freq = local_a7hal_xtal_freq * GET_FIELD( SYS_CLOCK_CONTROL_REG, PLL_DIV_FIELD, NBITS_PLL_DIV ); scale = GET_FIELD( SYS_CLOCK_CONTROL_REG, PLL_SCALE_FIELD, NBITS_PLL_SCALE ); while ( scale ) { freq >>= 1; scale >>= 1; }#endif } else { freq = local_a7hal_xclk_freq; } } return freq;}/** * \brief Set the frequency of the clock that drives the PLL. * * \param freq Frequency in Hertz of the PLL driver clock * * \return N/A * * \see a7hal_clock_getPllClock, a7hal_clock_getFreq * * \note This function will not change the actual * clock speed, since this is controlled by hardware, * it does not write to any clock registers. This function * is for software configuration only. */void a7hal_clock_setPllClock( unsigned long freq ){ a7hal_xtal_freq = freq;}/** * \brief Get the frequency of the clock that drives the PLL. * * \return The frequency in Hz of the clock that drives the PLL. * * \see a7hal_clock_getPllClock, a7hal_clock_getFreq * * \note The default value for clock that drives the PLL is A7HAL_XTAL_FREQ, * defined in clock.h */unsigned long a7hal_clock_getPllClock( void ){ return ( a7hal_xtal_freq );}/** * \brief Set the frequency of the external clock. * * \param freq Frequency in Hertz of the external clock * * \return N/A * * \see a7hal_clock_getXClock, a7hal_clock_getFreq * * \note This function will not change the actual * clock speed, since this is controlled by hardware, * it does not write to any clock registers. This function * is for software configuration only. */void a7hal_clock_setXClock( unsigned long freq ){ a7hal_xclk_freq = freq;}/** * \brief Get the frequency of the external clock. * * \return The frequency in Hz of the external clock. * * \see a7hal_clock_setXClock, a7hal_clock_getFreq * * \note The default value for the external clock is A7HAL_XCLK_FREQ, * defined in clock.h */unsigned long a7hal_clock_getXClock( void ){ return ( a7hal_xclk_freq );}/** * \brief Set the frequency of the ring oscillator. * * \param freq Frequency in Hertz ring oscillator * * \return N/A * * \see a7hal_clock_getRing, a7hal_clock_getFreq * * \note This function will not change the actual * clock speed, since this is controlled by hardware, * it does not write to any clock registers. This function * is for software configuration only. */void a7hal_clock_setRing( unsigned long freq ){ a7hal_ring_freq = freq;}/** * \brief Get the frequency of the ring oscillator. * * \return The frequency in Hz of the ring oscillator. * * \see a7hal_clock_setRing, a7hal_clock_getFreq * * \note The default value for the ring oscillator is A7HAL_RING_FREQ, * defined in clock.h */unsigned long a7hal_clock_getRing( void ){ return ( a7hal_ring_freq );}/** @} */#endif
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -