1
0
mirror of https://github.com/CTCaer/hekate.git synced 2024-12-27 03:15:21 +00:00
hekate/bootloader/soc/uart.c

84 lines
2.3 KiB
C
Raw Normal View History

2018-03-26 23:04:16 +00:00
/*
* Copyright (c) 2018 naehrwert
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2018-08-13 08:58:24 +00:00
#include "../soc/uart.h"
#include "../soc/t210.h"
#include "../utils/util.h"
2018-03-07 01:11:46 +00:00
/* UART A, B, C, D and E. */
static const u32 uart_baseoff[5] = { 0, 0x40, 0x200, 0x300, 0x400 };
void uart_init(u32 idx, u32 baud)
{
uart_t *uart = (uart_t *)(UART_BASE + uart_baseoff[idx]);
2018-12-07 20:00:19 +00:00
// Make sure no data is being sent.
uart_wait_idle(idx, UART_TX_IDLE);
// Misc settings.
2018-03-07 01:11:46 +00:00
u32 rate = (8 * baud + 408000000) / (16 * baud);
2018-12-07 20:00:19 +00:00
uart->UART_IER_DLAB = 0; // Disable interrupts.
uart->UART_MCR = 0; // Disable hardware flow control.
uart->UART_LCR = UART_LCR_DLAB | UART_LCR_WORD_LENGTH_8; // Enable DLAB & set 8n1 mode.
uart->UART_THR_DLAB = (u8)rate; // Divisor latch LSB.
uart->UART_IER_DLAB = (u8)(rate >> 8); // Divisor latch MSB.
uart->UART_LCR = UART_LCR_WORD_LENGTH_8; // Diable DLAB.
2018-03-07 01:11:46 +00:00
2018-12-07 20:00:19 +00:00
// Setup and flush fifo.
uart->UART_IIR_FCR = UART_IIR_FCR_EN_FIFO | UART_IIR_FCR_RX_CLR | UART_IIR_FCR_TX_CLR;
usleep(3 * ((baud + 999999) / baud));
2018-12-07 20:00:19 +00:00
uart_wait_idle(idx, UART_TX_IDLE | UART_RX_IDLE);
2018-03-07 01:11:46 +00:00
}
void uart_wait_idle(u32 idx, u32 which)
{
uart_t *uart = (uart_t *)(UART_BASE + uart_baseoff[idx]);
2018-12-07 20:00:19 +00:00
if (UART_TX_IDLE & which)
{
while (!(uart->UART_LSR & UART_LSR_TMTY))
;
}
if (UART_RX_IDLE & which)
{
while (uart->UART_LSR & UART_LSR_RDR)
;
}
2018-03-07 01:11:46 +00:00
}
void uart_send(u32 idx, u8 *buf, u32 len)
{
uart_t *uart = (uart_t *)(UART_BASE + uart_baseoff[idx]);
for (u32 i = 0; i != len; i++)
{
2018-12-07 20:00:19 +00:00
while (!(uart->UART_LSR & UART_LSR_THRE))
2018-03-07 01:11:46 +00:00
;
uart->UART_THR_DLAB = buf[i];
};
}
void uart_recv(u32 idx, u8 *buf, u32 len)
{
uart_t *uart = (uart_t *)(UART_BASE + uart_baseoff[idx]);
for (u32 i = 0; i != len; i++)
{
2018-12-07 20:00:19 +00:00
while (!(uart->UART_LSR & UART_LSR_RDR))
2018-03-07 01:11:46 +00:00
;
buf[i] = uart->UART_THR_DLAB;
};
}