blob: 8bbb803a6893c319cce8a7706affb1ee5dd66b8a [file] [log] [blame]
wdenkef3386f2004-10-10 21:27:30 +00001/*
2 * (C) Copyright 2004, Psyent Corporation <www.psyent.com>
3 * Scott McNutt <smcnutt@psyent.com>
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24
25#include <common.h>
26#include <watchdog.h>
Scott McNutta5721a32006-06-08 11:59:57 -040027#include <asm/io.h>
wdenkef3386f2004-10-10 21:27:30 +000028#include <nios2-io.h>
29
Wolfgang Denk6405a152006-03-31 18:32:53 +020030DECLARE_GLOBAL_DATA_PTR;
31
wdenkef3386f2004-10-10 21:27:30 +000032/*------------------------------------------------------------------
33 * JTAG acts as the serial port
34 *-----------------------------------------------------------------*/
35#if defined(CONFIG_CONSOLE_JTAG)
36
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020037static nios_jtag_t *jtag = (nios_jtag_t *)CONFIG_SYS_NIOS_CONSOLE;
wdenkef3386f2004-10-10 21:27:30 +000038
39void serial_setbrg( void ){ return; }
40int serial_init( void ) { return(0);}
41
42void serial_putc (char c)
43{
44 unsigned val;
45
Scott McNutta5721a32006-06-08 11:59:57 -040046 while (NIOS_JTAG_WSPACE ( readl (&jtag->control)) == 0)
wdenkef3386f2004-10-10 21:27:30 +000047 WATCHDOG_RESET ();
Scott McNutta5721a32006-06-08 11:59:57 -040048 writel (&jtag->data, (unsigned char)c);
wdenkef3386f2004-10-10 21:27:30 +000049}
50
51void serial_puts (const char *s)
52{
53 while (*s != 0)
54 serial_putc (*s++);
55}
56
57int serial_tstc (void)
58{
Scott McNutta5721a32006-06-08 11:59:57 -040059 return ( readl (&jtag->control) & NIOS_JTAG_RRDY);
wdenkef3386f2004-10-10 21:27:30 +000060}
61
62int serial_getc (void)
63{
64 int c;
65 unsigned val;
66
67 while (1) {
68 WATCHDOG_RESET ();
Scott McNutta5721a32006-06-08 11:59:57 -040069 val = readl (&jtag->data);
wdenkef3386f2004-10-10 21:27:30 +000070 if (val & NIOS_JTAG_RVALID)
71 break;
72 }
73 c = val & 0x0ff;
74 return (c);
75}
76
77/*------------------------------------------------------------------
78 * UART the serial port
79 *-----------------------------------------------------------------*/
80#else
81
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020082static nios_uart_t *uart = (nios_uart_t *) CONFIG_SYS_NIOS_CONSOLE;
wdenkef3386f2004-10-10 21:27:30 +000083
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020084#if defined(CONFIG_SYS_NIOS_FIXEDBAUD)
wdenkef3386f2004-10-10 21:27:30 +000085
86/* Everything's already setup for fixed-baud PTF
87 * assignment
88 */
89void serial_setbrg (void){ return; }
90int serial_init (void) { return (0);}
91
92#else
93
94void serial_setbrg (void)
95{
wdenkef3386f2004-10-10 21:27:30 +000096 unsigned div;
97
98 div = (CONFIG_SYS_CLK_FREQ/gd->baudrate)-1;
Scott McNutta5721a32006-06-08 11:59:57 -040099 writel (&uart->divisor,div);
wdenkef3386f2004-10-10 21:27:30 +0000100 return;
101}
102
103int serial_init (void)
104{
105 serial_setbrg ();
106 return (0);
107}
108
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200109#endif /* CONFIG_SYS_NIOS_FIXEDBAUD */
wdenkef3386f2004-10-10 21:27:30 +0000110
111
112/*-----------------------------------------------------------------------
113 * UART CONSOLE
114 *---------------------------------------------------------------------*/
115void serial_putc (char c)
116{
117 if (c == '\n')
118 serial_putc ('\r');
Scott McNutta5721a32006-06-08 11:59:57 -0400119 while ((readl (&uart->status) & NIOS_UART_TRDY) == 0)
wdenkef3386f2004-10-10 21:27:30 +0000120 WATCHDOG_RESET ();
Scott McNutta5721a32006-06-08 11:59:57 -0400121 writel (&uart->txdata,(unsigned char)c);
wdenkef3386f2004-10-10 21:27:30 +0000122}
123
124void serial_puts (const char *s)
125{
126 while (*s != 0) {
127 serial_putc (*s++);
128 }
129}
130
131int serial_tstc (void)
132{
Scott McNutta5721a32006-06-08 11:59:57 -0400133 return (readl (&uart->status) & NIOS_UART_RRDY);
wdenkef3386f2004-10-10 21:27:30 +0000134}
135
136int serial_getc (void)
137{
138 while (serial_tstc () == 0)
139 WATCHDOG_RESET ();
Scott McNutta5721a32006-06-08 11:59:57 -0400140 return (readl (&uart->rxdata) & 0x00ff );
wdenkef3386f2004-10-10 21:27:30 +0000141}
142
143#endif /* CONFIG_JTAG_CONSOLE */