Repository navigation
dockchannel_uart: Add Dockchannel UART as fallback for the hardware UART #546
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| ; | ||
|
|
||
| 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, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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: Emptyedit: but requires writing it as