litedram: Update to new LiteX/LiteDRAM version
[microwatt.git] / lib / console.c
1 #include <stdint.h>
2 #include <stdbool.h>
3
4 #include "console.h"
5 #include "microwatt_soc.h"
6 #include "io.h"
7
8 #define UART_FREQ 115200
9
10 /*
11 * Core UART functions to implement for a port
12 */
13
14 static uint64_t potato_uart_base;
15
16 static uint64_t potato_uart_reg_read(int offset)
17 {
18 return readq(potato_uart_base + offset);
19 }
20
21 static void potato_uart_reg_write(int offset, uint64_t val)
22 {
23 writeq(val, potato_uart_base + offset);
24 }
25
26 static int potato_uart_rx_empty(void)
27 {
28 uint64_t val;
29
30 val = potato_uart_reg_read(POTATO_CONSOLE_STATUS);
31
32 if (val & POTATO_CONSOLE_STATUS_RX_EMPTY)
33 return 1;
34
35 return 0;
36 }
37
38 static int potato_uart_tx_full(void)
39 {
40 uint64_t val;
41
42 val = potato_uart_reg_read(POTATO_CONSOLE_STATUS);
43
44 if (val & POTATO_CONSOLE_STATUS_TX_FULL)
45 return 1;
46
47 return 0;
48 }
49
50 static char potato_uart_read(void)
51 {
52 uint64_t val;
53
54 val = potato_uart_reg_read(POTATO_CONSOLE_RX);
55
56 return (char)(val & 0x000000ff);
57 }
58
59 static void potato_uart_write(char c)
60 {
61 uint64_t val;
62
63 val = c;
64
65 potato_uart_reg_write(POTATO_CONSOLE_TX, val);
66 }
67
68 static unsigned long potato_uart_divisor(unsigned long proc_freq, unsigned long uart_freq)
69 {
70 return proc_freq / (uart_freq * 16) - 1;
71 }
72
73 void potato_uart_init(void)
74 {
75 uint64_t proc_freq;
76
77 potato_uart_base = UART_BASE;
78 proc_freq = readq(SYSCON_BASE + SYS_REG_CLKINFO);
79
80 potato_uart_reg_write(POTATO_CONSOLE_CLOCK_DIV, potato_uart_divisor(proc_freq, UART_FREQ));
81 }
82
83 void potato_uart_irq_en(void)
84 {
85 potato_uart_reg_write(POTATO_CONSOLE_IRQ_EN, 0xff);
86 }
87
88 void potato_uart_irq_dis(void)
89 {
90 potato_uart_reg_write(POTATO_CONSOLE_IRQ_EN, 0x00);
91 }
92
93 int getchar(void)
94 {
95 while (potato_uart_rx_empty())
96 /* Do nothing */ ;
97
98 return potato_uart_read();
99 }
100
101 int putchar(int c)
102 {
103 while (potato_uart_tx_full())
104 /* Do Nothing */;
105
106 potato_uart_write(c);
107 return c;
108 }
109
110 int puts(const char *str)
111 {
112 unsigned int i;
113
114 for (i = 0; *str; i++) {
115 char c = *(str++);
116 if (c == 10)
117 putchar(13);
118 putchar(c);
119 }
120 return 0;
121 }
122
123 #ifndef __USE_LIBC
124 size_t strlen(const char *s)
125 {
126 size_t len = 0;
127
128 while (*s++)
129 len++;
130
131 return len;
132 }
133 #endif