xref: /rk3399_rockchip-uboot/drivers/serial/altera_uart.c (revision 3ea0037f2337de692b5fd2b6a4449db1de3067a2)
1c9d4f46bSScott McNutt /*
2c9d4f46bSScott McNutt  * (C) Copyright 2004, Psyent Corporation <www.psyent.com>
3c9d4f46bSScott McNutt  * Scott McNutt <smcnutt@psyent.com>
4c9d4f46bSScott McNutt  *
5c9d4f46bSScott McNutt  * See file CREDITS for list of people who contributed to this
6c9d4f46bSScott McNutt  * project.
7c9d4f46bSScott McNutt  *
8c9d4f46bSScott McNutt  * This program is free software; you can redistribute it and/or
9c9d4f46bSScott McNutt  * modify it under the terms of the GNU General Public License as
10c9d4f46bSScott McNutt  * published by the Free Software Foundation; either version 2 of
11c9d4f46bSScott McNutt  * the License, or (at your option) any later version.
12c9d4f46bSScott McNutt  *
13c9d4f46bSScott McNutt  * This program is distributed in the hope that it will be useful,
14c9d4f46bSScott McNutt  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15c9d4f46bSScott McNutt  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16c9d4f46bSScott McNutt  * GNU General Public License for more details.
17c9d4f46bSScott McNutt  *
18c9d4f46bSScott McNutt  * You should have received a copy of the GNU General Public License
19c9d4f46bSScott McNutt  * along with this program; if not, write to the Free Software
20c9d4f46bSScott McNutt  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21c9d4f46bSScott McNutt  * MA 02111-1307 USA
22c9d4f46bSScott McNutt  */
23c9d4f46bSScott McNutt 
24c9d4f46bSScott McNutt 
25c9d4f46bSScott McNutt #include <common.h>
26c9d4f46bSScott McNutt #include <watchdog.h>
27c9d4f46bSScott McNutt #include <asm/io.h>
28c9d4f46bSScott McNutt #include <nios2-io.h>
29c9d4f46bSScott McNutt 
30c9d4f46bSScott McNutt DECLARE_GLOBAL_DATA_PTR;
31c9d4f46bSScott McNutt 
32c9d4f46bSScott McNutt /*------------------------------------------------------------------
33c9d4f46bSScott McNutt  * UART the serial port
34c9d4f46bSScott McNutt  *-----------------------------------------------------------------*/
35c9d4f46bSScott McNutt 
36c9d4f46bSScott McNutt static nios_uart_t *uart = (nios_uart_t *) CONFIG_SYS_NIOS_CONSOLE;
37c9d4f46bSScott McNutt 
38c9d4f46bSScott McNutt #if defined(CONFIG_SYS_NIOS_FIXEDBAUD)
39c9d4f46bSScott McNutt 
40c9d4f46bSScott McNutt /* Everything's already setup for fixed-baud PTF
41c9d4f46bSScott McNutt  * assignment
42c9d4f46bSScott McNutt  */
43c9d4f46bSScott McNutt void serial_setbrg (void){ return; }
44c9d4f46bSScott McNutt int serial_init (void) { return (0);}
45c9d4f46bSScott McNutt 
46c9d4f46bSScott McNutt #else
47c9d4f46bSScott McNutt 
48c9d4f46bSScott McNutt void serial_setbrg (void)
49c9d4f46bSScott McNutt {
50c9d4f46bSScott McNutt 	unsigned div;
51c9d4f46bSScott McNutt 
52c9d4f46bSScott McNutt 	div = (CONFIG_SYS_CLK_FREQ/gd->baudrate)-1;
53*3ea0037fSScott McNutt 	writel (div, &uart->divisor);
54c9d4f46bSScott McNutt 	return;
55c9d4f46bSScott McNutt }
56c9d4f46bSScott McNutt 
57c9d4f46bSScott McNutt int serial_init (void)
58c9d4f46bSScott McNutt {
59c9d4f46bSScott McNutt 	serial_setbrg ();
60c9d4f46bSScott McNutt 	return (0);
61c9d4f46bSScott McNutt }
62c9d4f46bSScott McNutt 
63c9d4f46bSScott McNutt #endif /* CONFIG_SYS_NIOS_FIXEDBAUD */
64c9d4f46bSScott McNutt 
65c9d4f46bSScott McNutt /*-----------------------------------------------------------------------
66c9d4f46bSScott McNutt  * UART CONSOLE
67c9d4f46bSScott McNutt  *---------------------------------------------------------------------*/
68c9d4f46bSScott McNutt void serial_putc (char c)
69c9d4f46bSScott McNutt {
70c9d4f46bSScott McNutt 	if (c == '\n')
71c9d4f46bSScott McNutt 		serial_putc ('\r');
72c9d4f46bSScott McNutt 	while ((readl (&uart->status) & NIOS_UART_TRDY) == 0)
73c9d4f46bSScott McNutt 		WATCHDOG_RESET ();
74*3ea0037fSScott McNutt 	writel ((unsigned char)c, &uart->txdata);
75c9d4f46bSScott McNutt }
76c9d4f46bSScott McNutt 
77c9d4f46bSScott McNutt void serial_puts (const char *s)
78c9d4f46bSScott McNutt {
79c9d4f46bSScott McNutt 	while (*s != 0) {
80c9d4f46bSScott McNutt 		serial_putc (*s++);
81c9d4f46bSScott McNutt 	}
82c9d4f46bSScott McNutt }
83c9d4f46bSScott McNutt 
84c9d4f46bSScott McNutt int serial_tstc (void)
85c9d4f46bSScott McNutt {
86c9d4f46bSScott McNutt 	return (readl (&uart->status) & NIOS_UART_RRDY);
87c9d4f46bSScott McNutt }
88c9d4f46bSScott McNutt 
89c9d4f46bSScott McNutt int serial_getc (void)
90c9d4f46bSScott McNutt {
91c9d4f46bSScott McNutt 	while (serial_tstc () == 0)
92c9d4f46bSScott McNutt 		WATCHDOG_RESET ();
93c9d4f46bSScott McNutt 	return (readl (&uart->rxdata) & 0x00ff );
94c9d4f46bSScott McNutt }
95