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>
14 #include <linux/compiler.h>
16 DECLARE_GLOBAL_DATA_PTR;
18 ambapp_dev_apbuart *leon3_apbuart = NULL;
20 static int leon3_serial_init(void)
26 if (ambapp_apb_first(VENDOR_GAISLER, GAISLER_APBUART, &apbdev) == 1) {
28 leon3_apbuart = (ambapp_dev_apbuart *) apbdev.address;
30 /* found apbuart, let's init...
32 * Set scaler / baud rate
34 * Receiver & transmitter enable
36 leon3_apbuart->scaler = CONFIG_SYS_GRLIB_APBUART_SCALER;
38 /* Let bit 11 be unchanged (debug bit for GRMON) */
39 tmp = READ_WORD(leon3_apbuart->ctrl);
41 leon3_apbuart->ctrl = ((tmp & LEON_REG_UART_CTRL_DBG) |
42 LEON_REG_UART_CTRL_RE |
43 LEON_REG_UART_CTRL_TE);
47 return -1; /* didn't find hardware */
50 static void leon3_serial_putc_raw(const char c)
55 /* Wait for last character to go. */
56 while (!(READ_WORD(leon3_apbuart->status) & LEON_REG_UART_STATUS_THE)) ;
59 leon3_apbuart->data = c;
62 /* Wait for data to be sent */
63 while (!(READ_WORD(leon3_apbuart->status) & LEON_REG_UART_STATUS_TSE)) ;
67 static void leon3_serial_putc(const char c)
70 leon3_serial_putc_raw('\r');
72 leon3_serial_putc_raw(c);
75 static int leon3_serial_getc(void)
80 /* Wait for a character to arrive. */
81 while (!(READ_WORD(leon3_apbuart->status) & LEON_REG_UART_STATUS_DR)) ;
84 return READ_WORD(leon3_apbuart->data);
87 static int leon3_serial_tstc(void)
90 return (READ_WORD(leon3_apbuart->status) &
91 LEON_REG_UART_STATUS_DR);
95 /* set baud rate for uart */
96 static void leon3_serial_setbrg(void)
98 /* update baud rate settings, read it from gd->baudrate */
100 if (leon3_apbuart && (gd->baudrate > 0)) {
102 (((CONFIG_SYS_CLK_FREQ * 10) / (gd->baudrate * 8)) -
104 leon3_apbuart->scaler = scaler;
109 static struct serial_device leon3_serial_drv = {
110 .name = "leon3_serial",
111 .start = leon3_serial_init,
113 .setbrg = leon3_serial_setbrg,
114 .putc = leon3_serial_putc,
115 .puts = default_serial_puts,
116 .getc = leon3_serial_getc,
117 .tstc = leon3_serial_tstc,
120 void leon3_serial_initialize(void)
122 serial_register(&leon3_serial_drv);
125 __weak struct serial_device *default_serial_console(void)
127 return &leon3_serial_drv;