1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28#include <common.h>
29#include <watchdog.h>
30#include <asm/io.h>
31#include <nios2-io.h>
32#include <nios2-yanu.h>
33
34DECLARE_GLOBAL_DATA_PTR;
35
36
37
38
39#if defined(CONFIG_CONSOLE_JTAG)
40
41static nios_jtag_t *jtag = (nios_jtag_t *)CONFIG_SYS_NIOS_CONSOLE;
42
43void serial_setbrg( void ){ return; }
44int serial_init( void ) { return(0);}
45
46void serial_putc (char c)
47{
48 unsigned val;
49
50 while (NIOS_JTAG_WSPACE ( readl (&jtag->control)) == 0)
51 WATCHDOG_RESET ();
52 writel (&jtag->data, (unsigned char)c);
53}
54
55void serial_puts (const char *s)
56{
57 while (*s != 0)
58 serial_putc (*s++);
59}
60
61int serial_tstc (void)
62{
63 return ( readl (&jtag->control) & NIOS_JTAG_RRDY);
64}
65
66int serial_getc (void)
67{
68 int c;
69 unsigned val;
70
71 while (1) {
72 WATCHDOG_RESET ();
73 val = readl (&jtag->data);
74 if (val & NIOS_JTAG_RVALID)
75 break;
76 }
77 c = val & 0x0ff;
78 return (c);
79}
80
81#elif defined(CONFIG_CONSOLE_YANU)
82
83
84
85
86static yanu_uart_t *uart = (yanu_uart_t *)CONFIG_SYS_NIOS_CONSOLE;
87
88#if defined(CONFIG_SYS_NIOS_FIXEDBAUD)
89
90
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
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
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
154
155int serial_init (void)
156{
157 unsigned action,control;
158
159
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
171
172
173
174
175 control = (0x7 << YANU_CONTROL_BITS_POS);
176
177 control |= YANU_CONTROL_PAREVEN;
178
179 control |= YANU_CONTROL_RDYDLY * YANU_RXFIFO_DLY;
180 control |= YANU_CONTROL_TXTHR * YANU_TXFIFO_THR;
181
182 writel(&uart->control, control);
183
184
185 serial_setbrg();
186
187 return (0);
188}
189
190
191
192
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
237 writel(&uart->action, YANU_ACTION_RFIFO_PULL);
238
239 return(readl(&uart->data) & YANU_DATA_CHAR_MASK);
240}
241
242#else
243
244
245
246
247
248static nios_uart_t *uart = (nios_uart_t *) CONFIG_SYS_NIOS_CONSOLE;
249
250#if defined(CONFIG_SYS_NIOS_FIXEDBAUD)
251
252
253
254
255void serial_setbrg (void){ return; }
256int serial_init (void) { return (0);}
257
258#else
259
260void serial_setbrg (void)
261{
262 unsigned div;
263
264 div = (CONFIG_SYS_CLK_FREQ/gd->baudrate)-1;
265 writel (&uart->divisor,div);
266 return;
267}
268
269int serial_init (void)
270{
271 serial_setbrg ();
272 return (0);
273}
274
275#endif
276
277
278
279
280
281void serial_putc (char c)
282{
283 if (c == '\n')
284 serial_putc ('\r');
285 while ((readl (&uart->status) & NIOS_UART_TRDY) == 0)
286 WATCHDOG_RESET ();
287 writel (&uart->txdata,(unsigned char)c);
288}
289
290void serial_puts (const char *s)
291{
292 while (*s != 0) {
293 serial_putc (*s++);
294 }
295}
296
297int serial_tstc (void)
298{
299 return (readl (&uart->status) & NIOS_UART_RRDY);
300}
301
302int serial_getc (void)
303{
304 while (serial_tstc () == 0)
305 WATCHDOG_RESET ();
306 return (readl (&uart->rxdata) & 0x00ff );
307}
308
309#endif
310