From: Benjamin Herrenschmidt Date: Thu, 14 May 2020 00:08:06 +0000 (+1000) Subject: console: Move console files X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=7bc118c7db9aa003301a903002acfc32cec04ed7;p=microwatt.git console: Move console files console.c goes to a new lib/ where we'll store other general utilities and console.h goes to include/ Signed-off-by: Benjamin Herrenschmidt --- diff --git a/hello_world/Makefile b/hello_world/Makefile index 9051e7d..4c7d3ac 100644 --- a/hello_world/Makefile +++ b/hello_world/Makefile @@ -15,6 +15,9 @@ LDFLAGS = -T powerpc.lds all: hello_world.hex +console.o: ../lib/console.c + $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@ + hello_world.elf: hello_world.o head.o console.o $(LD) $(LDFLAGS) -o $@ $^ diff --git a/hello_world/console.c b/hello_world/console.c deleted file mode 100644 index 66a7d3a..0000000 --- a/hello_world/console.c +++ /dev/null @@ -1,138 +0,0 @@ -#include -#include - -#include "console.h" -#include "microwatt_soc.h" -#include "io.h" - -#define UART_FREQ 115200 - -/* - * Core UART functions to implement for a port - */ - -static uint64_t potato_uart_base; - -static uint64_t potato_uart_reg_read(int offset) -{ - return readq(potato_uart_base + offset); -} - -static void potato_uart_reg_write(int offset, uint64_t val) -{ - writeq(val, potato_uart_base + offset); -} - -static int potato_uart_rx_empty(void) -{ - uint64_t val; - - val = potato_uart_reg_read(POTATO_CONSOLE_STATUS); - - if (val & POTATO_CONSOLE_STATUS_RX_EMPTY) - return 1; - - return 0; -} - -static int potato_uart_tx_full(void) -{ - uint64_t val; - - val = potato_uart_reg_read(POTATO_CONSOLE_STATUS); - - if (val & POTATO_CONSOLE_STATUS_TX_FULL) - return 1; - - return 0; -} - -static char potato_uart_read(void) -{ - uint64_t val; - - val = potato_uart_reg_read(POTATO_CONSOLE_RX); - - return (char)(val & 0x000000ff); -} - -static void potato_uart_write(char c) -{ - uint64_t val; - - val = c; - - potato_uart_reg_write(POTATO_CONSOLE_TX, val); -} - -static unsigned long potato_uart_divisor(unsigned long proc_freq, unsigned long uart_freq) -{ - return proc_freq / (uart_freq * 16) - 1; -} - -void potato_uart_init(void) -{ - uint64_t proc_freq; - - potato_uart_base = UART_BASE; - proc_freq = readq(SYSCON_BASE + SYS_REG_CLKINFO); - - potato_uart_reg_write(POTATO_CONSOLE_CLOCK_DIV, potato_uart_divisor(proc_freq, UART_FREQ)); -} - -void potato_uart_irq_en(void) -{ - potato_uart_reg_write(POTATO_CONSOLE_IRQ_EN, 0xff); -} - -void potato_uart_irq_dis(void) -{ - potato_uart_reg_write(POTATO_CONSOLE_IRQ_EN, 0x00); -} - -int getchar(void) -{ - while (potato_uart_rx_empty()) - /* Do nothing */ ; - - return potato_uart_read(); -} - -int putchar(int c) -{ - while (potato_uart_tx_full()) - /* Do Nothing */; - - potato_uart_write(c); - return c; -} - -void putstr(const char *str, unsigned long len) -{ - for (unsigned long i = 0; i < len; i++) { - putchar(str[i]); - } -} - -int puts(const char *str) -{ - unsigned int i; - - for (i = 0; *str; i++) { - char c = *(str++); - if (c == 10) - putchar(13); - putchar(c); - } - return 0; -} - -size_t strlen(const char *s) -{ - size_t len = 0; - - while (*s++) - len++; - - return len; -} diff --git a/hello_world/console.h b/hello_world/console.h deleted file mode 100644 index 5cf303a..0000000 --- a/hello_world/console.h +++ /dev/null @@ -1,9 +0,0 @@ -#include - -void potato_uart_init(void); -void potato_uart_irq_en(void); -void potato_uart_irq_dis(void); -int getchar(void); -void putchar(unsigned char c); -void putstr(const char *str, unsigned long len); -size_t strlen(const char *s); diff --git a/include/console.h b/include/console.h new file mode 100644 index 0000000..a16db9a --- /dev/null +++ b/include/console.h @@ -0,0 +1,10 @@ +#include + +void potato_uart_init(void); +void potato_uart_irq_en(void); +void potato_uart_irq_dis(void); +int getchar(void); +int putchar(int c); +void putstr(const char *str, unsigned long len); +int puts(const char *str); +size_t strlen(const char *s); diff --git a/lib/console.c b/lib/console.c new file mode 100644 index 0000000..66a7d3a --- /dev/null +++ b/lib/console.c @@ -0,0 +1,138 @@ +#include +#include + +#include "console.h" +#include "microwatt_soc.h" +#include "io.h" + +#define UART_FREQ 115200 + +/* + * Core UART functions to implement for a port + */ + +static uint64_t potato_uart_base; + +static uint64_t potato_uart_reg_read(int offset) +{ + return readq(potato_uart_base + offset); +} + +static void potato_uart_reg_write(int offset, uint64_t val) +{ + writeq(val, potato_uart_base + offset); +} + +static int potato_uart_rx_empty(void) +{ + uint64_t val; + + val = potato_uart_reg_read(POTATO_CONSOLE_STATUS); + + if (val & POTATO_CONSOLE_STATUS_RX_EMPTY) + return 1; + + return 0; +} + +static int potato_uart_tx_full(void) +{ + uint64_t val; + + val = potato_uart_reg_read(POTATO_CONSOLE_STATUS); + + if (val & POTATO_CONSOLE_STATUS_TX_FULL) + return 1; + + return 0; +} + +static char potato_uart_read(void) +{ + uint64_t val; + + val = potato_uart_reg_read(POTATO_CONSOLE_RX); + + return (char)(val & 0x000000ff); +} + +static void potato_uart_write(char c) +{ + uint64_t val; + + val = c; + + potato_uart_reg_write(POTATO_CONSOLE_TX, val); +} + +static unsigned long potato_uart_divisor(unsigned long proc_freq, unsigned long uart_freq) +{ + return proc_freq / (uart_freq * 16) - 1; +} + +void potato_uart_init(void) +{ + uint64_t proc_freq; + + potato_uart_base = UART_BASE; + proc_freq = readq(SYSCON_BASE + SYS_REG_CLKINFO); + + potato_uart_reg_write(POTATO_CONSOLE_CLOCK_DIV, potato_uart_divisor(proc_freq, UART_FREQ)); +} + +void potato_uart_irq_en(void) +{ + potato_uart_reg_write(POTATO_CONSOLE_IRQ_EN, 0xff); +} + +void potato_uart_irq_dis(void) +{ + potato_uart_reg_write(POTATO_CONSOLE_IRQ_EN, 0x00); +} + +int getchar(void) +{ + while (potato_uart_rx_empty()) + /* Do nothing */ ; + + return potato_uart_read(); +} + +int putchar(int c) +{ + while (potato_uart_tx_full()) + /* Do Nothing */; + + potato_uart_write(c); + return c; +} + +void putstr(const char *str, unsigned long len) +{ + for (unsigned long i = 0; i < len; i++) { + putchar(str[i]); + } +} + +int puts(const char *str) +{ + unsigned int i; + + for (i = 0; *str; i++) { + char c = *(str++); + if (c == 10) + putchar(13); + putchar(c); + } + return 0; +} + +size_t strlen(const char *s) +{ + size_t len = 0; + + while (*s++) + len++; + + return len; +} diff --git a/litedram/gen-src/sdram_init/Makefile b/litedram/gen-src/sdram_init/Makefile index 28395a3..367b89d 100644 --- a/litedram/gen-src/sdram_init/Makefile +++ b/litedram/gen-src/sdram_init/Makefile @@ -4,7 +4,7 @@ include variables.mak OBJ = $(BUILD_DIR)/obj PROGRAM = sdram_init -OBJECTS = $(OBJ)/head.o $(OBJ)/main.o $(OBJ)/sdram.o +OBJECTS = $(OBJ)/head.o $(OBJ)/main.o $(OBJ)/sdram.o $(OBJ)/console.o #### Compiler @@ -50,10 +50,12 @@ all: objdir $(OBJ)/$(PROGRAM).hex $(OBJ)/sdram.o: $(LXSRC_DIR)/sdram.c $(call Q,CC, $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@, $@) -$(OBJ)/%.o : $(SRC_DIR)/%.S - $(call Q,AS, $(CC) $(ASFLAGS) -c $< -o $@, $@) +$(OBJ)/console.o: $(SRC_DIR)/../../../lib/console.c + $(call Q,CC, $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@, $@) $(OBJ)/%.o : $(SRC_DIR)/%.c $(call Q,CC, $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@, $@) +$(OBJ)/%.o : $(SRC_DIR)/%.S + $(call Q,AS, $(CC) $(ASFLAGS) -c $< -o $@, $@) $(OBJ)/%.o : $(SRC_DIR)/libc/src/%.c $(call Q,CC, $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@, $@) diff --git a/rust_lib_demo/console.c b/rust_lib_demo/console.c index 112b085..1c80e34 120000 --- a/rust_lib_demo/console.c +++ b/rust_lib_demo/console.c @@ -1 +1 @@ -../hello_world/console.c \ No newline at end of file +../lib/console.c \ No newline at end of file diff --git a/rust_lib_demo/console.h b/rust_lib_demo/console.h index fbe66d5..7a6f2c3 120000 --- a/rust_lib_demo/console.h +++ b/rust_lib_demo/console.h @@ -1 +1 @@ -../hello_world/console.h \ No newline at end of file +../include/console.h \ No newline at end of file diff --git a/tests/Makefile.test b/tests/Makefile.test index 9241e3f..3b800d0 100644 --- a/tests/Makefile.test +++ b/tests/Makefile.test @@ -9,14 +9,17 @@ CC = $(CROSS_COMPILE)gcc LD = $(CROSS_COMPILE)ld OBJCOPY = $(CROSS_COMPILE)objcopy -CFLAGS = -Os -g -Wall -std=c99 -msoft-float -mno-string -mno-multiple -mno-vsx -mno-altivec -mlittle-endian -fno-stack-protector -mstrict-align -ffreestanding -fdata-sections -ffunction-sections -I ../../hello_world -I ../../include +CFLAGS = -Os -g -Wall -std=c99 -msoft-float -mno-string -mno-multiple -mno-vsx -mno-altivec -mlittle-endian -fno-stack-protector -mstrict-align -ffreestanding -fdata-sections -ffunction-sections -I ../../include ASFLAGS = $(CFLAGS) LDFLAGS = -T powerpc.lds all: $(TEST).hex -$(TEST).elf: $(TEST).o head.o ../../hello_world/console.o - $(LD) $(LDFLAGS) -o $(TEST).elf $(TEST).o head.o ../../hello_world/console.o +console.o: ../../lib/console.c + $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@ + +$(TEST).elf: $(TEST).o head.o console.o + $(LD) $(LDFLAGS) -o $(TEST).elf $(TEST).o head.o console.o $(TEST).bin: $(TEST).elf $(OBJCOPY) -O binary $(TEST).elf $(TEST).bin