1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include <common.h>
24#include <watchdog.h>
25#include <asm/io.h>
26#include <nios2-yanu.h>
27
28DECLARE_GLOBAL_DATA_PTR;
29
30
31
32
33
34static yanu_uart_t *uart = (yanu_uart_t *)CONFIG_SYS_NIOS_CONSOLE;
35
36#if defined(CONFIG_SYS_NIOS_FIXEDBAUD)
37
38
39
40void serial_setbrg (void)
41{
42 int n, k;
43 const unsigned max_uns = 0xFFFFFFFF;
44 unsigned best_n, best_m, baud;
45
46
47 best_n = YANU_MAX_PRESCALER_N;
48 for (n = YANU_MAX_PRESCALER_N; n >= 0; n--) {
49 if ((unsigned)CONFIG_SYS_CLK_FREQ / (1 << (n + 4)) >=
50 (unsigned)CONFIG_BAUDRATE) {
51 best_n = n;
52 break;
53 }
54 }
55 for (k = 0;; k++) {
56 if ((unsigned)CONFIG_BAUDRATE <= (max_uns >> (15+n-k)))
57 break;
58 }
59 best_m =
60 ((unsigned)CONFIG_BAUDRATE * (1 << (15 + n - k))) /
61 ((unsigned)CONFIG_SYS_CLK_FREQ >> k);
62
63 baud = best_m + best_n * YANU_BAUDE;
64 writel(baud, &uart->baud);
65
66 return;
67}
68
69#else
70
71void serial_setbrg (void)
72{
73 int n, k;
74 const unsigned max_uns = 0xFFFFFFFF;
75 unsigned best_n, best_m, baud;
76
77
78 best_n = YANU_MAX_PRESCALER_N;
79 for (n = YANU_MAX_PRESCALER_N; n >= 0; n--) {
80 if ((unsigned)CONFIG_SYS_CLK_FREQ / (1 << (n + 4)) >=
81 gd->baudrate) {
82 best_n = n;
83 break;
84 }
85 }
86 for (k = 0;; k++) {
87 if (gd->baudrate <= (max_uns >> (15+n-k)))
88 break;
89 }
90 best_m =
91 (gd->baudrate * (1 << (15 + n - k))) /
92 ((unsigned)CONFIG_SYS_CLK_FREQ >> k);
93
94 baud = best_m + best_n * YANU_BAUDE;
95 writel(baud, &uart->baud);
96
97 return;
98}
99
100
101#endif
102
103int serial_init (void)
104{
105 unsigned action,control;
106
107
108 action = YANU_ACTION_RRRDY |
109 YANU_ACTION_RTRDY |
110 YANU_ACTION_ROE |
111 YANU_ACTION_RBRK |
112 YANU_ACTION_RFE |
113 YANU_ACTION_RPE |
114 YANU_ACTION_RFE | YANU_ACTION_RFIFO_CLEAR | YANU_ACTION_TFIFO_CLEAR;
115
116 writel(action, &uart->action);
117
118
119
120
121
122
123
124
125 control = (0x7 << YANU_CONTROL_BITS_POS);
126
127 control |= YANU_CONTROL_PAREVEN;
128
129 control |= YANU_CONTROL_RDYDLY * YANU_RXFIFO_DLY;
130 control |= YANU_CONTROL_TXTHR * YANU_TXFIFO_THR;
131
132 writel(control, &uart->control);
133
134
135 serial_setbrg();
136
137 return (0);
138}
139
140
141
142
143
144void serial_putc (char c)
145{
146 int tx_chars;
147 unsigned status;
148
149 if (c == '\n')
150 serial_putc ('\r');
151
152 while (1) {
153 status = readl(&uart->status);
154 tx_chars = (status>>YANU_TFIFO_CHARS_POS)
155 & ((1<<YANU_TFIFO_CHARS_N)-1);
156 if (tx_chars < YANU_TXFIFO_SIZE-1)
157 break;
158 WATCHDOG_RESET ();
159 }
160
161 writel((unsigned char)c, &uart->data);
162}
163
164void serial_puts (const char *s)
165{
166 while (*s != 0) {
167 serial_putc (*s++);
168 }
169}
170
171
172int serial_tstc(void)
173{
174 unsigned status ;
175
176 status = readl(&uart->status);
177 return (((status >> YANU_RFIFO_CHARS_POS) &
178 ((1 << YANU_RFIFO_CHARS_N) - 1)) > 0);
179}
180
181int serial_getc (void)
182{
183 while (serial_tstc() == 0)
184 WATCHDOG_RESET ();
185
186
187 writel(YANU_ACTION_RFIFO_PULL, &uart->action);
188
189 return(readl(&uart->data) & YANU_DATA_CHAR_MASK);
190}
191