From 1156977bfd01ef7f54a0acbd921cb7064b79366f Mon Sep 17 00:00:00 2001 From: "Wesley W. Terpstra" Date: Mon, 27 Mar 2017 14:16:05 -0700 Subject: uart: find it using fdt --- machine/uart.c | 44 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) (limited to 'machine/uart.c') diff --git a/machine/uart.c b/machine/uart.c index 28cc21c..7391063 100644 --- a/machine/uart.c +++ b/machine/uart.c @@ -1,3 +1,4 @@ +#include #include "uart.h" #include "fdt.h" @@ -27,12 +28,49 @@ int uart_getchar() return ch; } -void query_uart(uintptr_t dtb) +struct uart_scan { - uart = 0; // (void*)fdt_get_reg(dtb, "sifive,uart0"); - if (!uart) return; + int compat; + uintptr_t reg; +}; + +static void uart_open(const struct fdt_scan_node *node, void *extra) +{ + struct uart_scan *scan = (struct uart_scan *)extra; + memset(scan, 0, sizeof(*scan)); +} + +static void uart_prop(const struct fdt_scan_prop *prop, void *extra) +{ + struct uart_scan *scan = (struct uart_scan *)extra; + if (!strcmp(prop->name, "compatible") && !strcmp((const char*)prop->value, "riscv,uart0")) { + scan->compat = 1; + } else if (!strcmp(prop->name, "reg")) { + fdt_get_address(prop->node->parent, prop->value, &scan->reg); + } +} + +static void uart_done(const struct fdt_scan_node *node, void *extra) +{ + struct uart_scan *scan = (struct uart_scan *)extra; + if (!scan->compat || !scan->reg || uart) return; // Enable Rx/Tx channels + uart = (void*)scan->reg; uart[UART_REG_TXCTRL] = UART_TXEN; uart[UART_REG_RXCTRL] = UART_RXEN; } + +void query_uart(uintptr_t fdt) +{ + struct fdt_cb cb; + struct uart_scan scan; + + memset(&cb, 0, sizeof(cb)); + cb.open = uart_open; + cb.prop = uart_prop; + cb.done = uart_done; + cb.extra = &scan; + + fdt_scan(fdt, &cb); +} -- cgit v1.1