-
Notifications
You must be signed in to change notification settings - Fork 304
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
77 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,38 @@ | ||
#include "uart.h" | ||
#include "fdt.h" | ||
|
||
volatile uint32_t* uart; | ||
|
||
void uart_putchar(uint8_t ch) | ||
{ | ||
#ifdef __riscv_atomic | ||
int32_t r; | ||
do { | ||
__asm__ __volatile__ ( | ||
"amoor.w %0, %2, %1\n" | ||
: "=r" (r), "+A" (uart[UART_REG_TXFIFO]) | ||
: "r" (ch)); | ||
} while (r < 0); | ||
#else | ||
volatile uint32_t *tx = uart + UART_REG_TXFIFO; | ||
while ((int32_t)(*tx) < 0); | ||
*tx = ch; | ||
#endif | ||
} | ||
|
||
int uart_getchar() | ||
{ | ||
int32_t ch = uart[UART_REG_RXFIFO]; | ||
if (ch < 0) return -1; | ||
return ch; | ||
} | ||
|
||
void query_uart(uintptr_t dtb) | ||
{ | ||
uart = 0; // (void*)fdt_get_reg(dtb, "sifive,uart0"); | ||
if (!uart) return; | ||
|
||
// Enable Rx/Tx channels | ||
uart[UART_REG_TXCTRL] = UART_TXEN; | ||
uart[UART_REG_RXCTRL] = UART_RXEN; | ||
} |
This file contains 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,21 @@ | ||
#ifndef _RISCV_UART_H | ||
#define _RISCV_UART_H | ||
|
||
#include <stdint.h> | ||
|
||
extern volatile uint32_t* uart; | ||
|
||
#define UART_REG_TXFIFO 0 | ||
#define UART_REG_RXFIFO 1 | ||
#define UART_REG_TXCTRL 2 | ||
#define UART_REG_RXCTRL 3 | ||
#define UART_REG_DIV 4 | ||
|
||
#define UART_TXEN 0x1 | ||
#define UART_RXEN 0x1 | ||
|
||
void uart_putchar(uint8_t ch); | ||
int uart_getchar(); | ||
void query_uart(uintptr_t dtb); | ||
|
||
#endif |