1 /*
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2020 Vijai Kumar K <vijai@behindbytes.com>
5  */
6 
7 #include <sbi/riscv_io.h>
8 #include <sbi/sbi_console.h>
9 #include <sbi_utils/serial/shakti-uart.h>
10 
11 #define REG_BAUD	0x00
12 #define REG_TX		0x04
13 #define REG_RX		0x08
14 #define REG_STATUS	0x0C
15 #define REG_DELAY	0x10
16 #define REG_CONTROL	0x14
17 #define REG_INT_EN	0x18
18 #define REG_IQ_CYCLES	0x1C
19 #define REG_RX_THRES	0x20
20 
21 #define UART_TX_FULL  0x2
22 #define UART_RX_FULL  0x8
23 
24 static volatile void *uart_base;
25 
shakti_uart_putc(char ch)26 void shakti_uart_putc(char ch)
27 {
28 	while((readw(uart_base + REG_STATUS) & UART_TX_FULL))
29 		;
30 	writeb(ch, uart_base + REG_TX);
31 }
32 
shakti_uart_getc(void)33 int shakti_uart_getc(void)
34 {
35 	u16 status = readw(uart_base + REG_STATUS);
36 	if (status & UART_RX_FULL)
37 		return readb(uart_base + REG_RX);
38 	return -1;
39 }
40 
shakti_uart_init(unsigned long base,u32 in_freq,u32 baudrate)41 int shakti_uart_init(unsigned long base, u32 in_freq, u32 baudrate)
42 {
43 	uart_base = (volatile void *)base;
44 	u16 baud = (u16)(in_freq/(16 * baudrate));
45 	writew(baud, uart_base + REG_BAUD);
46 
47 	return 0;
48 }
49