diff --git a/Makefile b/Makefile index f4c4cef21..7e4f34738 100644 --- a/Makefile +++ b/Makefile @@ -134,6 +134,7 @@ OBJECTS := \ dcp_iboot.o \ devicetree.o \ display.o \ + dockchannel_uart.o \ exception.o exception_asm.o \ fb.o font.o font_retina.o \ firmware.o \ diff --git a/src/dockchannel_uart.c b/src/dockchannel_uart.c new file mode 100644 index 000000000..8de89efc3 --- /dev/null +++ b/src/dockchannel_uart.c @@ -0,0 +1,158 @@ +/* SPDX-License-Identifier: MIT */ + +#include + +#include "adt.h" +#include "iodev.h" +#include "types.h" +#include "utils.h" +#include "vsprintf.h" + +#define DATA_TX8 0x4004 +#define DATA_TX_FREE 0x4014 +#define DATA_RX8 0x401c +#define DATA_RX_COUNT 0x402c + +static u64 uart_base = 0; + +int dockchannel_uart_init(void) +{ + int path[8]; + + if (adt_path_offset_trace(adt, "/arm-io/dockchannel-uart", path) < 0) + return -1; + + if (adt_get_reg(adt, path, "reg", 0, &uart_base, NULL)) { + printf("!!! Failed to get dockchannel UART reg property!\n"); + return -1; + } + + printf("Initialized dockchannel UART at 0x%lx\n", uart_base); + + return 0; +} + +void dockchannel_uart_putbyte(u8 c) +{ + if (!uart_base) + return; + + while (read32(uart_base + DATA_TX_FREE) == 0) + ; + + write32(uart_base + DATA_TX8, c); +} + +u8 dockchannel_uart_getbyte(void) +{ + if (!uart_base) + return 0; + + while (read32(uart_base + DATA_RX_COUNT) == 0) + ; + + return read32(uart_base + DATA_RX8) >> 8; +} + +void dockchannel_uart_putchar(u8 c) +{ + if (c == '\n') + dockchannel_uart_putbyte('\r'); + + dockchannel_uart_putbyte(c); +} + +u8 dockchannel_uart_getchar(void) +{ + return dockchannel_uart_getbyte(); +} + +void dockchannel_uart_puts(const char *s) +{ + while (*s) + dockchannel_uart_putchar(*(s++)); + + dockchannel_uart_putchar('\n'); +} + +void dockchannel_uart_write(const void *buf, size_t count) +{ + const u8 *p = buf; + + while (count--) + dockchannel_uart_putbyte(*p++); +} + +size_t dockchannel_uart_read(void *buf, size_t count) +{ + u8 *p = buf; + size_t recvd = 0; + + while (count--) { + *p++ = dockchannel_uart_getbyte(); + recvd++; + } + + return recvd; +} + +int dockchannel_uart_printf(const char *fmt, ...) +{ + va_list args; + char buffer[512]; + int i; + + va_start(args, fmt); + i = vsnprintf(buffer, sizeof(buffer), fmt, args); + va_end(args); + + dockchannel_uart_write(buffer, min(i, (int)(sizeof(buffer) - 1))); + + return i; +} + +static bool dockchannel_uart_iodev_can_write(void *opaque) +{ + UNUSED(opaque); + + if (!uart_base) + return false; + + return read32(uart_base + DATA_TX_FREE) > 0; +} + +static ssize_t dockchannel_uart_iodev_can_read(void *opaque) +{ + UNUSED(opaque); + + if (!uart_base) + return 0; + + return read32(uart_base + DATA_RX_COUNT); +} + +static ssize_t dockchannel_uart_iodev_read(void *opaque, void *buf, size_t len) +{ + UNUSED(opaque); + return dockchannel_uart_read(buf, len); +} + +static ssize_t dockchannel_uart_iodev_write(void *opaque, const void *buf, size_t len) +{ + UNUSED(opaque); + dockchannel_uart_write(buf, len); + return len; +} + +static struct iodev_ops iodev_dockchannel_uart_ops = { + .can_read = dockchannel_uart_iodev_can_read, + .can_write = dockchannel_uart_iodev_can_write, + .read = dockchannel_uart_iodev_read, + .write = dockchannel_uart_iodev_write, +}; + +struct iodev iodev_dockchannel_uart = { + .ops = &iodev_dockchannel_uart_ops, + .usage = USAGE_CONSOLE | USAGE_UARTPROXY, + .lock = SPINLOCK_INIT, +}; diff --git a/src/dockchannel_uart.h b/src/dockchannel_uart.h new file mode 100644 index 000000000..bcf444366 --- /dev/null +++ b/src/dockchannel_uart.h @@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: MIT */ + +#ifndef DOCKCHANNEL_UART_H +#define DOCKCHANNEL_UART_H + +#include "types.h" + +int dockchannel_uart_init(void); + +void dockchannel_uart_putbyte(u8 c); +u8 dockchannel_uart_getbyte(void); + +void dockchannel_uart_putchar(u8 c); +u8 dockchannel_uart_getchar(void); + +void dockchannel_uart_write(const void *buf, size_t count); +size_t dockchannel_uart_read(void *buf, size_t count); + +void dockchannel_uart_puts(const char *s); +int dockchannel_uart_printf(const char *fmt, ...) __attribute__((format(printf, 1, 2))); + +#endif diff --git a/src/iodev.c b/src/iodev.c index cdf7d5533..c0cc3bdd4 100644 --- a/src/iodev.c +++ b/src/iodev.c @@ -17,16 +17,20 @@ #define CONSOLE_BUFFER_SIZE 8192 extern struct iodev iodev_uart; +extern struct iodev iodev_dockchannel_uart; extern struct iodev iodev_fb; extern struct iodev iodev_log; extern struct iodev iodev_usb_vuart; +/* clang-format off */ struct iodev *iodevs[IODEV_NUM] = { [IODEV_UART] = &iodev_uart, + [IODEV_DOCKCHANNEL_UART] = &iodev_dockchannel_uart, [IODEV_FB] = &iodev_fb, [IODEV_USB_VUART] = &iodev_usb_vuart, [IODEV_LOG] = &iodev_log, }; +/* clang-format on */ char con_buf[CONSOLE_BUFFER_SIZE]; size_t con_wp; @@ -148,15 +152,24 @@ int in_iodev = 0; static DECLARE_SPINLOCK(console_lock); +static void _iodev_console_write_helper(iodev_id_t iod, char prefix, const void *buf, size_t length) +{ + if (!length) + return; + if (!(iodevs[iod]->usage & USAGE_CONSOLE)) + return; + + iodevs[iod]->ops->write(iodevs[iod]->opaque, &prefix, 1); + iodevs[iod]->ops->write(iodevs[iod]->opaque, buf, length); +} + void iodev_console_write(const void *buf, size_t length) { bool do_lock = mmu_active(); if (!do_lock && !is_boot_cpu()) { - if (length && iodevs[IODEV_UART]->usage & USAGE_CONSOLE) { - iodevs[IODEV_UART]->ops->write(iodevs[IODEV_UART]->opaque, "*", 1); - iodevs[IODEV_UART]->ops->write(iodevs[IODEV_UART]->opaque, buf, length); - } + _iodev_console_write_helper(IODEV_UART, '*', buf, length); + _iodev_console_write_helper(IODEV_DOCKCHANNEL_UART, '*', buf, length); return; } @@ -164,10 +177,9 @@ void iodev_console_write(const void *buf, size_t length) spin_lock(&console_lock); if (in_iodev) { - if (length && iodevs[IODEV_UART]->usage & USAGE_CONSOLE) { - iodevs[IODEV_UART]->ops->write(iodevs[IODEV_UART]->opaque, "+", 1); - iodevs[IODEV_UART]->ops->write(iodevs[IODEV_UART]->opaque, buf, length); - } + _iodev_console_write_helper(IODEV_UART, '+', buf, length); + _iodev_console_write_helper(IODEV_DOCKCHANNEL_UART, '+', buf, length); + if (do_lock) spin_unlock(&console_lock); return; diff --git a/src/iodev.h b/src/iodev.h index 4c8b06c05..88c5bc620 100644 --- a/src/iodev.h +++ b/src/iodev.h @@ -10,6 +10,7 @@ typedef enum _iodev_id_t { IODEV_UART, + IODEV_DOCKCHANNEL_UART, IODEV_FB, IODEV_USB_VUART, IODEV_USB0, diff --git a/src/startup.c b/src/startup.c index 2b4d48e07..61afe169c 100644 --- a/src/startup.c +++ b/src/startup.c @@ -2,6 +2,7 @@ #include "adt.h" #include "chickens.h" +#include "dockchannel_uart.h" #include "exception.h" #include "firmware.h" #include "smp.h" @@ -195,6 +196,9 @@ void _start_c(void *boot_args, void *base) debug_putc('!'); } + /* Use Dockchannel as a secondary UART for now and ignore failure */ + dockchannel_uart_init(); + uart_puts("Initializing"); get_device_info(); diff --git a/src/uartproxy.c b/src/uartproxy.c index fed9cc546..fd6773aaf 100644 --- a/src/uartproxy.c +++ b/src/uartproxy.c @@ -135,9 +135,10 @@ int uartproxy_run(struct uartproxy_msg_start *start) UartRequest request; UartReply reply = {REQ_BOOT}; if (!start) { - // Startup notification only goes out via UART + // Startup notification only goes out via UART and Dockchannel UART reply.checksum = checksum(&reply, REPLY_SIZE - 4); iodev_write(IODEV_UART, &reply, REPLY_SIZE); + iodev_write(IODEV_DOCKCHANNEL_UART, &reply, REPLY_SIZE); } else { // Exceptions / hooks keep the current iodev iodev = uartproxy_iodev; @@ -202,8 +203,8 @@ int uartproxy_run(struct uartproxy_msg_start *start) switch (request.type) { case REQ_NOP: enabled_features = request.features & PROXY_FEAT_ALL; - if (iodev == IODEV_UART) { - // Don't allow disabling checksums on UART + if (iodev == IODEV_UART || iodev == IODEV_DOCKCHANNEL_UART) { + // Don't allow disabling checksums on UART or Dockchannel UART enabled_features &= ~PROXY_FEAT_DISABLE_DATA_CSUMS; }