1 /* GRLIB APBUART Serial controller driver
4 * Daniel Hellstrom, Gaisler Research, daniel@gaisler.com.
6 * SPDX-License-Identifier: GPL-2.0+
10 #include <asm/processor.h>
13 #include <linux/compiler.h>
15 DECLARE_GLOBAL_DATA_PTR;
17 static int leon2_serial_init(void)
19 LEON2_regs *leon2 = (LEON2_regs *) LEON2_PREGS;
20 LEON2_Uart_regs *regs;
25 * Set scaler / baud rate
27 * Receiver & transmitter enable
29 #if LEON2_CONSOLE_SELECT == LEON_CONSOLE_UART1
30 regs = (LEON2_Uart_regs *) & leon2->UART_Channel_1;
32 regs = (LEON2_Uart_regs *) & leon2->UART_Channel_2;
35 regs->UART_Scaler = CONFIG_SYS_LEON2_UART1_SCALER;
37 /* Let bit 11 be unchanged (debug bit for GRMON) */
38 tmp = READ_WORD(regs->UART_Control);
40 regs->UART_Control = ((tmp & LEON2_UART_CTRL_DBG) |
41 (LEON2_UART1_LOOPBACK_ENABLE << 7) |
42 (LEON2_UART1_FLOWCTRL_ENABLE << 6) |
43 (LEON2_UART1_PARITY_ENABLE << 5) |
44 (LEON2_UART1_ODDPAR_ENABLE << 4) |
45 LEON2_UART_CTRL_RE | LEON2_UART_CTRL_TE);
50 static void leon2_serial_putc_raw(const char c)
52 LEON2_regs *leon2 = (LEON2_regs *) LEON2_PREGS;
53 LEON2_Uart_regs *regs;
55 #if LEON2_CONSOLE_SELECT == LEON_CONSOLE_UART1
56 regs = (LEON2_Uart_regs *) & leon2->UART_Channel_1;
58 regs = (LEON2_Uart_regs *) & leon2->UART_Channel_2;
61 /* Wait for last character to go. */
62 while (!(READ_WORD(regs->UART_Status) & LEON2_UART_STAT_THE)) ;
65 regs->UART_Channel = c;
68 /* Wait for data to be sent */
69 while (!(READ_WORD(regs->UART_Status) & LEON2_UART_STAT_TSE)) ;
73 static void leon2_serial_putc(const char c)
76 leon2_serial_putc_raw('\r');
78 leon2_serial_putc_raw(c);
81 static int leon2_serial_getc(void)
83 LEON2_regs *leon2 = (LEON2_regs *) LEON2_PREGS;
84 LEON2_Uart_regs *regs;
86 #if LEON2_CONSOLE_SELECT == LEON_CONSOLE_UART1
87 regs = (LEON2_Uart_regs *) & leon2->UART_Channel_1;
89 regs = (LEON2_Uart_regs *) & leon2->UART_Channel_2;
92 /* Wait for a character to arrive. */
93 while (!(READ_WORD(regs->UART_Status) & LEON2_UART_STAT_DR)) ;
96 return READ_WORD(regs->UART_Channel);
99 static int leon2_serial_tstc(void)
101 LEON2_regs *leon2 = (LEON2_regs *) LEON2_PREGS;
102 LEON2_Uart_regs *regs;
104 #if LEON2_CONSOLE_SELECT == LEON_CONSOLE_UART1
105 regs = (LEON2_Uart_regs *) & leon2->UART_Channel_1;
107 regs = (LEON2_Uart_regs *) & leon2->UART_Channel_2;
110 return (READ_WORD(regs->UART_Status) & LEON2_UART_STAT_DR);
113 /* set baud rate for uart */
114 static void leon2_serial_setbrg(void)
116 /* update baud rate settings, read it from gd->baudrate */
118 LEON2_regs *leon2 = (LEON2_regs *) LEON2_PREGS;
119 LEON2_Uart_regs *regs;
121 #if LEON2_CONSOLE_SELECT == LEON_CONSOLE_UART1
122 regs = (LEON2_Uart_regs *) & leon2->UART_Channel_1;
124 regs = (LEON2_Uart_regs *) & leon2->UART_Channel_2;
127 if (gd->baudrate > 0) {
129 (((CONFIG_SYS_CLK_FREQ * 10) / (gd->baudrate * 8)) -
131 regs->UART_Scaler = scaler;
135 static struct serial_device leon2_serial_drv = {
136 .name = "leon2_serial",
137 .start = leon2_serial_init,
139 .setbrg = leon2_serial_setbrg,
140 .putc = leon2_serial_putc,
141 .puts = default_serial_puts,
142 .getc = leon2_serial_getc,
143 .tstc = leon2_serial_tstc,
146 void leon2_serial_initialize(void)
148 serial_register(&leon2_serial_drv);
151 __weak struct serial_device *default_serial_console(void)
153 return &leon2_serial_drv;