blob: 6c835af4b1b18ce95d723e23b8be41ec5a452ad6 [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 *
Renato Andreola9f64dcb2010-03-16 16:01:29 -04005 * YANU Support:
6 * Copyright 2010, Renato Andreola <renato.andreola@imagos.it>
7 *
wdenkef3386f2004-10-10 21:27:30 +00008 * See file CREDITS for list of people who contributed to this
9 * project.
10 *
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.
15 *
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.
20 *
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,
24 * MA 02111-1307 USA
25 */
26
27
28#include <common.h>
29#include <watchdog.h>
Scott McNutta5721a32006-06-08 11:59:57 -040030#include <asm/io.h>
wdenkef3386f2004-10-10 21:27:30 +000031#include <nios2-io.h>
Renato Andreola9f64dcb2010-03-16 16:01:29 -040032#include <nios2-yanu.h>
wdenkef3386f2004-10-10 21:27:30 +000033
Wolfgang Denk6405a152006-03-31 18:32:53 +020034DECLARE_GLOBAL_DATA_PTR;
35
wdenkef3386f2004-10-10 21:27:30 +000036/*------------------------------------------------------------------
37 * JTAG acts as the serial port
38 *-----------------------------------------------------------------*/
39#if defined(CONFIG_CONSOLE_JTAG)
40
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020041static nios_jtag_t *jtag = (nios_jtag_t *)CONFIG_SYS_NIOS_CONSOLE;
wdenkef3386f2004-10-10 21:27:30 +000042
43void serial_setbrg( void ){ return; }
44int serial_init( void ) { return(0);}
45
46void serial_putc (char c)
47{
48 unsigned val;
49
Scott McNutta5721a32006-06-08 11:59:57 -040050 while (NIOS_JTAG_WSPACE ( readl (&jtag->control)) == 0)
wdenkef3386f2004-10-10 21:27:30 +000051 WATCHDOG_RESET ();
Scott McNutta5721a32006-06-08 11:59:57 -040052 writel (&jtag->data, (unsigned char)c);
wdenkef3386f2004-10-10 21:27:30 +000053}
54
55void serial_puts (const char *s)
56{
57 while (*s != 0)
58 serial_putc (*s++);
59}
60
61int serial_tstc (void)
62{
Scott McNutta5721a32006-06-08 11:59:57 -040063 return ( readl (&jtag->control) & NIOS_JTAG_RRDY);
wdenkef3386f2004-10-10 21:27:30 +000064}
65
66int serial_getc (void)
67{
68 int c;
69 unsigned val;
70
71 while (1) {
72 WATCHDOG_RESET ();
Scott McNutta5721a32006-06-08 11:59:57 -040073 val = readl (&jtag->data);
wdenkef3386f2004-10-10 21:27:30 +000074 if (val & NIOS_JTAG_RVALID)
75 break;
76 }
77 c = val & 0x0ff;
78 return (c);
79}
80
Renato Andreola9f64dcb2010-03-16 16:01:29 -040081#elif defined(CONFIG_CONSOLE_YANU)
82/*-----------------------------------------------------------------*/
83/* YANU Imagos serial port */
84/*-----------------------------------------------------------------*/
85
86static yanu_uart_t *uart = (yanu_uart_t *)CONFIG_SYS_NIOS_CONSOLE;
87
88#if defined(CONFIG_SYS_NIOS_FIXEDBAUD)
89
90/* Everything's already setup for fixed-baud PTF assignment*/
91
92void serial_setbrg (void)
93{
94 int n, k;
95 const unsigned max_uns = 0xFFFFFFFF;
96 unsigned best_n, best_m, baud;
97
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) {
103 best_n = n;
104 break;
105 }
106 }
107 for (k = 0;; k++) {
108 if ((unsigned)CONFIG_BAUDRATE <= (max_uns >> (15+n-k)))
109 break;
110 }
111 best_m =
112 ((unsigned)CONFIG_BAUDRATE * (1 << (15 + n - k))) /
113 ((unsigned)CONFIG_SYS_CLK_FREQ >> k);
114
115 baud = best_m + best_n * YANU_BAUDE;
116 writel(&uart->baud, baud);
117
118 return;
119}
120
121#else
122
123void serial_setbrg (void)
124{
125 int n, k;
126 const unsigned max_uns = 0xFFFFFFFF;
127 unsigned best_n, best_m, baud;
128
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)) >=
133 gd->baudrate) {
134 best_n = n;
135 break;
136 }
137 }
138 for (k = 0;; k++) {
139 if (gd->baudrate <= (max_uns >> (15+n-k)))
140 break;
141 }
142 best_m =
143 (gd->baudrate * (1 << (15 + n - k))) /
144 ((unsigned)CONFIG_SYS_CLK_FREQ >> k);
145
146 baud = best_m + best_n * YANU_BAUDE;
147 writel(&uart->baud, baud);
148
149 return;
150}
151
152
153#endif /* CONFIG_SYS_NIOS_FIXEDBAUD */
154
155int serial_init (void)
156{
157 unsigned action,control;
158
159 /* status register cleanup */
160 action = YANU_ACTION_RRRDY |
161 YANU_ACTION_RTRDY |
162 YANU_ACTION_ROE |
163 YANU_ACTION_RBRK |
164 YANU_ACTION_RFE |
165 YANU_ACTION_RPE |
166 YANU_ACTION_RFE | YANU_ACTION_RFIFO_CLEAR | YANU_ACTION_TFIFO_CLEAR;
167
168 writel(&uart->action, action);
169
170 /* control register cleanup */
171 /* no interrupts enabled */
172 /* one stop bit */
173 /* hardware flow control disabled */
174 /* 8 bits */
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;
181
182 writel(&uart->control, control);
183
184 /* to set baud rate */
185 serial_setbrg();
186
187 return (0);
188}
189
190
191/*-----------------------------------------------------------------------
192 * YANU CONSOLE
193 *---------------------------------------------------------------------*/
194void serial_putc (char c)
195{
196 int tx_chars;
197 unsigned status;
198
199 if (c == '\n')
200 serial_putc ('\r');
201
202 while (1) {
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)
207 break;
208 WATCHDOG_RESET ();
209 }
210
211 writel(&uart->data, (unsigned char)c);
212}
213
214void serial_puts (const char *s)
215{
216 while (*s != 0) {
217 serial_putc (*s++);
218 }
219}
220
221
222int serial_tstc(void)
223{
224 unsigned status ;
225
226 status = readl(&uart->status);
227 return (((status >> YANU_RFIFO_CHARS_POS) &
228 ((1 << YANU_RFIFO_CHARS_N) - 1)) > 0);
229}
230
231int serial_getc (void)
232{
233 while (serial_tstc() == 0)
234 WATCHDOG_RESET ();
235
236 /* first we pull the char */
237 writel(&uart->action, YANU_ACTION_RFIFO_PULL);
238
239 return(readl(&uart->data) & YANU_DATA_CHAR_MASK);
240}
241
242#else /*CONFIG_CONSOLE_YANU*/
243
wdenkef3386f2004-10-10 21:27:30 +0000244/*------------------------------------------------------------------
245 * UART the serial port
246 *-----------------------------------------------------------------*/
wdenkef3386f2004-10-10 21:27:30 +0000247
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200248static nios_uart_t *uart = (nios_uart_t *) CONFIG_SYS_NIOS_CONSOLE;
wdenkef3386f2004-10-10 21:27:30 +0000249
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200250#if defined(CONFIG_SYS_NIOS_FIXEDBAUD)
wdenkef3386f2004-10-10 21:27:30 +0000251
252/* Everything's already setup for fixed-baud PTF
253 * assignment
254 */
255void serial_setbrg (void){ return; }
256int serial_init (void) { return (0);}
257
258#else
259
260void serial_setbrg (void)
261{
wdenkef3386f2004-10-10 21:27:30 +0000262 unsigned div;
263
264 div = (CONFIG_SYS_CLK_FREQ/gd->baudrate)-1;
Scott McNutta5721a32006-06-08 11:59:57 -0400265 writel (&uart->divisor,div);
wdenkef3386f2004-10-10 21:27:30 +0000266 return;
267}
268
269int serial_init (void)
270{
271 serial_setbrg ();
272 return (0);
273}
274
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200275#endif /* CONFIG_SYS_NIOS_FIXEDBAUD */
wdenkef3386f2004-10-10 21:27:30 +0000276
277
278/*-----------------------------------------------------------------------
279 * UART CONSOLE
280 *---------------------------------------------------------------------*/
281void serial_putc (char c)
282{
283 if (c == '\n')
284 serial_putc ('\r');
Scott McNutta5721a32006-06-08 11:59:57 -0400285 while ((readl (&uart->status) & NIOS_UART_TRDY) == 0)
wdenkef3386f2004-10-10 21:27:30 +0000286 WATCHDOG_RESET ();
Scott McNutta5721a32006-06-08 11:59:57 -0400287 writel (&uart->txdata,(unsigned char)c);
wdenkef3386f2004-10-10 21:27:30 +0000288}
289
290void serial_puts (const char *s)
291{
292 while (*s != 0) {
293 serial_putc (*s++);
294 }
295}
296
297int serial_tstc (void)
298{
Scott McNutta5721a32006-06-08 11:59:57 -0400299 return (readl (&uart->status) & NIOS_UART_RRDY);
wdenkef3386f2004-10-10 21:27:30 +0000300}
301
302int serial_getc (void)
303{
304 while (serial_tstc () == 0)
305 WATCHDOG_RESET ();
Scott McNutta5721a32006-06-08 11:59:57 -0400306 return (readl (&uart->rxdata) & 0x00ff );
wdenkef3386f2004-10-10 21:27:30 +0000307}
308
309#endif /* CONFIG_JTAG_CONSOLE */