Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
158 changes: 158 additions & 0 deletions src/dockchannel_uart.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/* SPDX-License-Identifier: MIT */

#include <stdarg.h>

#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)
;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to change our .clang-format to format this on a single line? It looks a little bit ugly as is

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'll see what I can do later this week

@jannau jannau Mar 1, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

feel free to merge as. We can reformat and change clang-format later. The option should be AllowShortBlocksOnASingleLine: Empty
edit: but requires writing it as

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,
};
22 changes: 22 additions & 0 deletions src/dockchannel_uart.h
Original file line number Diff line number Diff line change
@@ -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
28 changes: 20 additions & 8 deletions src/iodev.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -148,26 +152,34 @@ 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;
}

if (do_lock)
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;
Expand Down
1 change: 1 addition & 0 deletions src/iodev.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

typedef enum _iodev_id_t {
IODEV_UART,
IODEV_DOCKCHANNEL_UART,
IODEV_FB,
IODEV_USB_VUART,
IODEV_USB0,
Expand Down
4 changes: 4 additions & 0 deletions src/startup.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "adt.h"
#include "chickens.h"
#include "dockchannel_uart.h"
#include "exception.h"
#include "firmware.h"
#include "smp.h"
Expand Down Expand Up @@ -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();

Expand Down
7 changes: 4 additions & 3 deletions src/uartproxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down