2 * (C) Copyright 2004, Psyent Corporation <www.psyent.com>
3 * Scott McNutt <smcnutt@psyent.com>
6 * Copyright 2010, Renato Andreola <renato.andreola@imagos.it>
8 * See file CREDITS for list of people who contributed to this
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
32 #include <nios2-yanu.h>
34 DECLARE_GLOBAL_DATA_PTR;
36 /*------------------------------------------------------------------
37 * JTAG acts as the serial port
38 *-----------------------------------------------------------------*/
39 #if defined(CONFIG_CONSOLE_JTAG)
41 static nios_jtag_t *jtag = (nios_jtag_t *)CONFIG_SYS_NIOS_CONSOLE;
43 void serial_setbrg( void ){ return; }
44 int serial_init( void ) { return(0);}
46 void serial_putc (char c)
50 while (NIOS_JTAG_WSPACE ( readl (&jtag->control)) == 0)
52 writel (&jtag->data, (unsigned char)c);
55 void serial_puts (const char *s)
61 int serial_tstc (void)
63 return ( readl (&jtag->control) & NIOS_JTAG_RRDY);
66 int serial_getc (void)
73 val = readl (&jtag->data);
74 if (val & NIOS_JTAG_RVALID)
81 #elif defined(CONFIG_CONSOLE_YANU)
82 /*-----------------------------------------------------------------*/
83 /* YANU Imagos serial port */
84 /*-----------------------------------------------------------------*/
86 static yanu_uart_t *uart = (yanu_uart_t *)CONFIG_SYS_NIOS_CONSOLE;
88 #if defined(CONFIG_SYS_NIOS_FIXEDBAUD)
90 /* Everything's already setup for fixed-baud PTF assignment*/
92 void serial_setbrg (void)
95 const unsigned max_uns = 0xFFFFFFFF;
96 unsigned best_n, best_m, baud;
98 /* compute best N and M couple */
99 best_n = YANU_MAX_PRESCALER_N;
100 for (n = YANU_MAX_PRESCALER_N; n >= 0; n--) {
101 if ((unsigned)CONFIG_SYS_CLK_FREQ / (1 << (n + 4)) >=
102 (unsigned)CONFIG_BAUDRATE) {
108 if ((unsigned)CONFIG_BAUDRATE <= (max_uns >> (15+n-k)))
112 ((unsigned)CONFIG_BAUDRATE * (1 << (15 + n - k))) /
113 ((unsigned)CONFIG_SYS_CLK_FREQ >> k);
115 baud = best_m + best_n * YANU_BAUDE;
116 writel(&uart->baud, baud);
123 void serial_setbrg (void)
126 const unsigned max_uns = 0xFFFFFFFF;
127 unsigned best_n, best_m, baud;
129 /* compute best N and M couple */
130 best_n = YANU_MAX_PRESCALER_N;
131 for (n = YANU_MAX_PRESCALER_N; n >= 0; n--) {
132 if ((unsigned)CONFIG_SYS_CLK_FREQ / (1 << (n + 4)) >=
139 if (gd->baudrate <= (max_uns >> (15+n-k)))
143 (gd->baudrate * (1 << (15 + n - k))) /
144 ((unsigned)CONFIG_SYS_CLK_FREQ >> k);
146 baud = best_m + best_n * YANU_BAUDE;
147 writel(&uart->baud, baud);
153 #endif /* CONFIG_SYS_NIOS_FIXEDBAUD */
155 int serial_init (void)
157 unsigned action,control;
159 /* status register cleanup */
160 action = YANU_ACTION_RRRDY |
166 YANU_ACTION_RFE | YANU_ACTION_RFIFO_CLEAR | YANU_ACTION_TFIFO_CLEAR;
168 writel(&uart->action, action);
170 /* control register cleanup */
171 /* no interrupts enabled */
173 /* hardware flow control disabled */
175 control = (0x7 << YANU_CONTROL_BITS_POS);
176 /* enven parity just to be clean */
177 control |= YANU_CONTROL_PAREVEN;
178 /* we set threshold for fifo */
179 control |= YANU_CONTROL_RDYDLY * YANU_RXFIFO_DLY;
180 control |= YANU_CONTROL_TXTHR * YANU_TXFIFO_THR;
182 writel(&uart->control, control);
184 /* to set baud rate */
191 /*-----------------------------------------------------------------------
193 *---------------------------------------------------------------------*/
194 void serial_putc (char c)
203 status = readl(&uart->status);
204 tx_chars = (status>>YANU_TFIFO_CHARS_POS)
205 & ((1<<YANU_TFIFO_CHARS_N)-1);
206 if (tx_chars < YANU_TXFIFO_SIZE-1)
211 writel(&uart->data, (unsigned char)c);
214 void serial_puts (const char *s)
222 int serial_tstc(void)
226 status = readl(&uart->status);
227 return (((status >> YANU_RFIFO_CHARS_POS) &
228 ((1 << YANU_RFIFO_CHARS_N) - 1)) > 0);
231 int serial_getc (void)
233 while (serial_tstc() == 0)
236 /* first we pull the char */
237 writel(&uart->action, YANU_ACTION_RFIFO_PULL);
239 return(readl(&uart->data) & YANU_DATA_CHAR_MASK);
242 #else /*CONFIG_CONSOLE_YANU*/
244 /*------------------------------------------------------------------
245 * UART the serial port
246 *-----------------------------------------------------------------*/
248 static nios_uart_t *uart = (nios_uart_t *) CONFIG_SYS_NIOS_CONSOLE;
250 #if defined(CONFIG_SYS_NIOS_FIXEDBAUD)
252 /* Everything's already setup for fixed-baud PTF
255 void serial_setbrg (void){ return; }
256 int serial_init (void) { return (0);}
260 void serial_setbrg (void)
264 div = (CONFIG_SYS_CLK_FREQ/gd->baudrate)-1;
265 writel (&uart->divisor,div);
269 int serial_init (void)
275 #endif /* CONFIG_SYS_NIOS_FIXEDBAUD */
278 /*-----------------------------------------------------------------------
280 *---------------------------------------------------------------------*/
281 void serial_putc (char c)
285 while ((readl (&uart->status) & NIOS_UART_TRDY) == 0)
287 writel (&uart->txdata,(unsigned char)c);
290 void serial_puts (const char *s)
297 int serial_tstc (void)
299 return (readl (&uart->status) & NIOS_UART_RRDY);
302 int serial_getc (void)
304 while (serial_tstc () == 0)
306 return (readl (&uart->rxdata) & 0x00ff );
309 #endif /* CONFIG_JTAG_CONSOLE */