2021-01-12 18:22:11 +00:00
|
|
|
/* SPDX-License-Identifier: MIT */
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
#include "types.h"
|
|
|
|
#include "uart.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "vsprintf.h"
|
|
|
|
|
2021-01-14 18:55:20 +00:00
|
|
|
#define UART_CLOCK 24000000
|
|
|
|
|
2021-01-12 18:22:11 +00:00
|
|
|
#define UART_BASE 0x235200000L
|
|
|
|
|
2021-01-28 06:27:35 +00:00
|
|
|
#define ULCON 0x000
|
|
|
|
#define UCON 0x004
|
|
|
|
#define UFCON 0x008
|
|
|
|
#define UTRSTAT 0x010
|
|
|
|
#define UTXH 0x020
|
|
|
|
#define URXH 0x024
|
|
|
|
#define UBRDIV 0x028
|
2021-01-12 18:22:11 +00:00
|
|
|
#define UFRACVAL 0x02c
|
|
|
|
|
|
|
|
void *pxx = uart_init;
|
|
|
|
|
|
|
|
void uart_init(void)
|
|
|
|
{
|
|
|
|
/* keep UART config from iBoot */
|
|
|
|
}
|
|
|
|
|
|
|
|
void uart_putbyte(u8 c)
|
|
|
|
{
|
|
|
|
while (!(read32(UART_BASE + UTRSTAT) & 0x02))
|
|
|
|
;
|
|
|
|
|
|
|
|
write32(UART_BASE + UTXH, c);
|
|
|
|
}
|
|
|
|
|
2021-01-14 09:18:07 +00:00
|
|
|
u8 uart_getbyte(void)
|
|
|
|
{
|
|
|
|
while (!(read32(UART_BASE + UTRSTAT) & 0x01))
|
|
|
|
;
|
|
|
|
|
|
|
|
return read32(UART_BASE + URXH);
|
|
|
|
}
|
|
|
|
|
|
|
|
void uart_putchar(u8 c)
|
2021-01-12 18:22:11 +00:00
|
|
|
{
|
|
|
|
if (c == '\n')
|
|
|
|
uart_putbyte('\r');
|
|
|
|
|
|
|
|
uart_putbyte(c);
|
|
|
|
}
|
|
|
|
|
2021-01-14 09:18:07 +00:00
|
|
|
u8 uart_getchar(void)
|
|
|
|
{
|
2021-01-14 10:22:38 +00:00
|
|
|
return uart_getbyte();
|
2021-01-14 09:18:07 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 18:22:11 +00:00
|
|
|
void uart_puts(const char *s)
|
|
|
|
{
|
|
|
|
while (*s)
|
2021-01-14 09:18:07 +00:00
|
|
|
uart_putchar(*(s++));
|
2021-01-12 18:22:11 +00:00
|
|
|
|
2021-01-14 09:18:07 +00:00
|
|
|
uart_putchar('\n');
|
2021-01-12 18:22:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void uart_write(const void *buf, size_t count)
|
|
|
|
{
|
|
|
|
const u8 *p = buf;
|
|
|
|
|
|
|
|
while (count--)
|
|
|
|
uart_putbyte(*p++);
|
|
|
|
}
|
|
|
|
|
2021-01-14 10:22:38 +00:00
|
|
|
size_t uart_read(void *buf, size_t count)
|
2021-01-12 18:22:11 +00:00
|
|
|
{
|
2021-01-14 10:22:38 +00:00
|
|
|
u8 *p = buf;
|
|
|
|
size_t recvd = 0;
|
|
|
|
|
|
|
|
while (count--) {
|
|
|
|
*p++ = uart_getbyte();
|
|
|
|
recvd++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return recvd;
|
2021-01-12 18:22:11 +00:00
|
|
|
}
|
2021-01-14 18:55:20 +00:00
|
|
|
|
|
|
|
void uart_setbaud(int baudrate)
|
|
|
|
{
|
|
|
|
uart_flush();
|
|
|
|
write32(UART_BASE + UBRDIV, ((UART_CLOCK / baudrate + 7) / 16) - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void uart_flush(void)
|
|
|
|
{
|
|
|
|
while (!(read32(UART_BASE + UTRSTAT) & 0x04))
|
|
|
|
;
|
|
|
|
}
|