2015-07-28 00:19:07 +08:00
|
|
|
#include <generated/csr.h>
|
|
|
|
|
2015-10-31 23:26:09 +08:00
|
|
|
#include "log.h"
|
2015-07-28 00:19:07 +08:00
|
|
|
#include "clock.h"
|
|
|
|
#include "flash_storage.h"
|
|
|
|
#include "rtiocrg.h"
|
|
|
|
|
|
|
|
void rtiocrg_init(void)
|
|
|
|
{
|
|
|
|
char b;
|
|
|
|
int clk;
|
|
|
|
|
2015-07-28 12:18:45 +08:00
|
|
|
#ifdef CSR_RTIO_CRG_PLL_RESET_ADDR
|
2015-07-28 00:19:07 +08:00
|
|
|
rtio_crg_pll_reset_write(0);
|
2015-07-28 12:18:45 +08:00
|
|
|
#endif
|
2015-07-28 00:19:07 +08:00
|
|
|
b = 'i';
|
|
|
|
clk = 0;
|
|
|
|
fs_read("startup_clock", &b, 1, NULL);
|
|
|
|
if(b == 'i')
|
2016-02-15 06:54:54 +08:00
|
|
|
core_log("Startup RTIO clock: internal\n");
|
2015-07-28 00:19:07 +08:00
|
|
|
else if(b == 'e') {
|
2016-02-15 06:54:54 +08:00
|
|
|
core_log("Startup RTIO clock: external\n");
|
2015-07-28 00:19:07 +08:00
|
|
|
clk = 1;
|
|
|
|
} else
|
2016-02-15 06:54:54 +08:00
|
|
|
core_log("ERROR: unrecognized startup_clock entry in flash storage\n");
|
2015-07-28 00:19:07 +08:00
|
|
|
|
2015-07-28 00:38:38 +08:00
|
|
|
if(!rtiocrg_switch_clock(clk)) {
|
2016-02-15 06:54:54 +08:00
|
|
|
core_log("ERROR: startup RTIO clock failed\n");
|
|
|
|
core_log("WARNING: this may cause the system initialization to fail\n");
|
|
|
|
core_log("WARNING: fix clocking and reset the device\n");
|
2015-07-28 00:38:38 +08:00
|
|
|
}
|
2015-07-28 00:19:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int rtiocrg_check(void)
|
|
|
|
{
|
2015-07-28 12:18:45 +08:00
|
|
|
#ifdef CSR_RTIO_CRG_PLL_RESET_ADDR
|
2015-07-28 00:19:07 +08:00
|
|
|
return rtio_crg_pll_locked_read();
|
2015-07-28 12:18:45 +08:00
|
|
|
#else
|
|
|
|
return 1;
|
|
|
|
#endif
|
2015-07-28 00:19:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int rtiocrg_switch_clock(int clk)
|
|
|
|
{
|
|
|
|
int current_clk;
|
|
|
|
|
|
|
|
current_clk = rtio_crg_clock_sel_read();
|
|
|
|
if(clk == current_clk) {
|
2015-07-28 12:18:45 +08:00
|
|
|
#ifdef CSR_RTIO_CRG_PLL_RESET_ADDR
|
2015-07-28 00:19:07 +08:00
|
|
|
busywait_us(150);
|
|
|
|
if(!rtio_crg_pll_locked_read())
|
|
|
|
return 0;
|
2015-07-28 12:18:45 +08:00
|
|
|
#endif
|
2015-07-28 00:19:07 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2015-07-28 12:18:45 +08:00
|
|
|
#ifdef CSR_RTIO_CRG_PLL_RESET_ADDR
|
2015-07-28 00:19:07 +08:00
|
|
|
rtio_crg_pll_reset_write(1);
|
2015-07-28 12:18:45 +08:00
|
|
|
#endif
|
2015-07-28 00:19:07 +08:00
|
|
|
rtio_crg_clock_sel_write(clk);
|
2015-07-28 12:18:45 +08:00
|
|
|
#ifdef CSR_RTIO_CRG_PLL_RESET_ADDR
|
2015-07-28 00:19:07 +08:00
|
|
|
rtio_crg_pll_reset_write(0);
|
|
|
|
busywait_us(150);
|
|
|
|
if(!rtio_crg_pll_locked_read())
|
|
|
|
return 0;
|
2015-07-28 12:18:45 +08:00
|
|
|
#endif
|
2015-07-28 00:19:07 +08:00
|
|
|
return 1;
|
|
|
|
}
|