xref: /openbmc/u-boot/drivers/serial/altera_uart.c (revision 7ca6f363)
1 /*
2  * (C) Copyright 2004, Psyent Corporation <www.psyent.com>
3  * Scott McNutt <smcnutt@psyent.com>
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 
9 #include <common.h>
10 #include <watchdog.h>
11 #include <asm/io.h>
12 #include <nios2-io.h>
13 #include <linux/compiler.h>
14 #include <serial.h>
15 
16 DECLARE_GLOBAL_DATA_PTR;
17 
18 /*------------------------------------------------------------------
19  * UART the serial port
20  *-----------------------------------------------------------------*/
21 
22 static nios_uart_t *uart = (nios_uart_t *) CONFIG_SYS_NIOS_CONSOLE;
23 
24 #if defined(CONFIG_SYS_NIOS_FIXEDBAUD)
25 
26 /*
27  * Everything's already setup for fixed-baud PTF
28  * assignment
29  */
30 static void altera_serial_setbrg(void)
31 {
32 }
33 
34 static int altera_serial_init(void)
35 {
36 	return 0;
37 }
38 
39 #else
40 
41 static void altera_serial_setbrg(void)
42 {
43 	unsigned div;
44 
45 	div = (CONFIG_SYS_CLK_FREQ/gd->baudrate)-1;
46 	writel (div, &uart->divisor);
47 }
48 
49 static int altera_serial_init(void)
50 {
51 	serial_setbrg();
52 	return 0;
53 }
54 
55 #endif /* CONFIG_SYS_NIOS_FIXEDBAUD */
56 
57 /*-----------------------------------------------------------------------
58  * UART CONSOLE
59  *---------------------------------------------------------------------*/
60 static void altera_serial_putc(char c)
61 {
62 	if (c == '\n')
63 		serial_putc ('\r');
64 	while ((readl (&uart->status) & NIOS_UART_TRDY) == 0)
65 		WATCHDOG_RESET ();
66 	writel ((unsigned char)c, &uart->txdata);
67 }
68 
69 static int altera_serial_tstc(void)
70 {
71 	return (readl (&uart->status) & NIOS_UART_RRDY);
72 }
73 
74 static int altera_serial_getc(void)
75 {
76 	while (serial_tstc () == 0)
77 		WATCHDOG_RESET ();
78 	return (readl (&uart->rxdata) & 0x00ff );
79 }
80 
81 static struct serial_device altera_serial_drv = {
82 	.name	= "altera_serial",
83 	.start	= altera_serial_init,
84 	.stop	= NULL,
85 	.setbrg	= altera_serial_setbrg,
86 	.putc	= altera_serial_putc,
87 	.puts	= default_serial_puts,
88 	.getc	= altera_serial_getc,
89 	.tstc	= altera_serial_tstc,
90 };
91 
92 void altera_serial_initialize(void)
93 {
94 	serial_register(&altera_serial_drv);
95 }
96 
97 __weak struct serial_device *default_serial_console(void)
98 {
99 	return &altera_serial_drv;
100 }
101