diff --git a/soc/runtime/Makefile b/soc/runtime/Makefile index 241448f37..673235928 100644 --- a/soc/runtime/Makefile +++ b/soc/runtime/Makefile @@ -1,6 +1,6 @@ include $(MSCDIR)/software/common.mak -OBJECTS := isr.o flash_storage.o clock.o elf_loader.o services.o session.o log.o test_mode.o kloader.o bridge_ctl.o mailbox.o ksupport_data.o kserver.o moninj.o main.o +OBJECTS := isr.o flash_storage.o clock.o rtiocrg.o elf_loader.o services.o session.o log.o test_mode.o kloader.o bridge_ctl.o mailbox.o ksupport_data.o kserver.o moninj.o main.o OBJECTS_KSUPPORT := ksupport.o exception_jmp.o exceptions.o mailbox.o bridge.o rtio.o ttl.o dds.o CFLAGS += -Ilwip/src/include -Iliblwip diff --git a/soc/runtime/main.c b/soc/runtime/main.c index 3a639b145..74df38aea 100644 --- a/soc/runtime/main.c +++ b/soc/runtime/main.c @@ -24,6 +24,7 @@ #include "kloader.h" #include "flash_storage.h" #include "clock.h" +#include "rtiocrg.h" #include "test_mode.h" #include "kserver.h" #include "session.h" @@ -250,13 +251,10 @@ int main(void) puts("ARTIQ runtime built "__DATE__" "__TIME__"\n"); clock_init(); + rtiocrg_init(); puts("Press 't' to enter test mode..."); blink_led(); -#ifdef CSR_RTIO_CRG_PLL_RESET_ADDR - rtio_crg_pll_reset_write(0); -#endif - if(check_test_mode()) { puts("Entering test mode."); test_main(); diff --git a/soc/runtime/rtiocrg.c b/soc/runtime/rtiocrg.c new file mode 100644 index 000000000..08cabd5b2 --- /dev/null +++ b/soc/runtime/rtiocrg.c @@ -0,0 +1,64 @@ +#include +#include + +#include "clock.h" +#include "flash_storage.h" +#include "rtiocrg.h" + +void rtiocrg_init(void) +{ + char b; + int clk; + +#ifdef CSR_RTIO_CRG_PLL_RESET_ADDR + rtio_crg_pll_reset_write(0); +#endif + b = 'i'; + clk = 0; + fs_read("startup_clock", &b, 1, NULL); + if(b == 'i') + printf("Startup RTIO clock: internal\n"); + else if(b == 'e') { + printf("Startup RTIO clock: external\n"); + clk = 1; + } else + printf("WARNING: unknown startup_clock entry in flash storage\n"); + + if(!rtiocrg_switch_clock(clk)) + printf("WARNING: startup RTIO clock failed\n"); +} + +int rtiocrg_check(void) +{ +#ifdef CSR_RTIO_CRG_PLL_RESET_ADDR + return rtio_crg_pll_locked_read(); +#else + return 1; +#endif +} + +int rtiocrg_switch_clock(int clk) +{ + int current_clk; + + current_clk = rtio_crg_clock_sel_read(); + if(clk == current_clk) { +#ifdef CSR_RTIO_CRG_PLL_RESET_ADDR + busywait_us(150); + if(!rtio_crg_pll_locked_read()) + return 0; +#endif + return 1; + } +#ifdef CSR_RTIO_CRG_PLL_RESET_ADDR + rtio_crg_pll_reset_write(1); +#endif + rtio_crg_clock_sel_write(clk); +#ifdef CSR_RTIO_CRG_PLL_RESET_ADDR + rtio_crg_pll_reset_write(0); + busywait_us(150); + if(!rtio_crg_pll_locked_read()) + return 0; +#endif + return 1; +} diff --git a/soc/runtime/rtiocrg.h b/soc/runtime/rtiocrg.h new file mode 100644 index 000000000..df498f617 --- /dev/null +++ b/soc/runtime/rtiocrg.h @@ -0,0 +1,8 @@ +#ifndef __RTIOCRG_H +#define __RTIOCRG_H + +void rtiocrg_init(void); +int rtiocrg_check(void); +int rtiocrg_switch_clock(int clk); + +#endif /* __RTIOCRG_H */ diff --git a/soc/runtime/session.c b/soc/runtime/session.c index d3227a4d6..c0908639c 100644 --- a/soc/runtime/session.c +++ b/soc/runtime/session.c @@ -12,6 +12,7 @@ #include "kloader.h" #include "exceptions.h" #include "flash_storage.h" +#include "rtiocrg.h" #include "session.h" #define BUFFER_IN_SIZE (1024*1024) @@ -128,32 +129,6 @@ static int check_flash_storage_key_len(char *key, unsigned int key_len) return 1; } -static int switch_clock(int clk) -{ - int current_clk; - - current_clk = rtio_crg_clock_sel_read(); - if(clk == current_clk) { -#ifdef CSR_RTIO_CRG_PLL_RESET_ADDR - busywait_us(150); - if(!rtio_crg_pll_locked_read()) - return 0; -#endif - return 1; - } -#ifdef CSR_RTIO_CRG_PLL_RESET_ADDR - rtio_crg_pll_reset_write(1); -#endif - rtio_crg_clock_sel_write(clk); -#ifdef CSR_RTIO_CRG_PLL_RESET_ADDR - rtio_crg_pll_reset_write(0); - busywait_us(150); - if(!rtio_crg_pll_locked_read()) - return 0; -#endif - return 1; -} - static int process_input(void) { switch(buffer_in[8]) { @@ -180,7 +155,7 @@ static int process_input(void) submit_output(9); break; } - if(switch_clock(buffer_in[9])) + if(rtiocrg_switch_clock(buffer_in[9])) buffer_out[8] = REMOTEMSG_TYPE_CLOCK_SWITCH_COMPLETED; else buffer_out[8] = REMOTEMSG_TYPE_CLOCK_SWITCH_FAILED; @@ -561,13 +536,11 @@ void session_poll(void **data, int *len) *len = -1; return; } -#ifdef CSR_RTIO_CRG_PLL_RESET_ADDR - if(!rtio_crg_pll_locked_read()) { + if(!rtiocrg_check()) { log("RTIO clock failure"); *len = -1; return; } -#endif } l = get_out_packet_len();