]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/serial.c
serial: Implement serial_initfunc() macro
[people/ms/u-boot.git] / common / serial.c
CommitLineData
281e00a3
WD
1/*
2 * (C) Copyright 2004
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
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#include <common.h>
25#include <serial.h>
52cb4d4f 26#include <stdio_dev.h>
7b826c2f
MF
27#include <post.h>
28#include <linux/compiler.h>
281e00a3 29
d87080b7
WD
30DECLARE_GLOBAL_DATA_PTR;
31
a6e6f7f4
GF
32static struct serial_device *serial_devices;
33static struct serial_device *serial_current;
281e00a3 34
2a333aeb
MV
35static void serial_null(void)
36{
37}
38
39#define serial_initfunc(name) \
40 void name(void) \
41 __attribute__((weak, alias("serial_null")));
42
c52b4f79 43void serial_register(struct serial_device *dev)
281e00a3 44{
2e5167cc 45#ifdef CONFIG_NEEDS_MANUAL_RELOC
89143fb3 46 dev->start += gd->reloc_off;
281e00a3
WD
47 dev->setbrg += gd->reloc_off;
48 dev->getc += gd->reloc_off;
49 dev->tstc += gd->reloc_off;
50 dev->putc += gd->reloc_off;
51 dev->puts += gd->reloc_off;
521af04d 52#endif
281e00a3
WD
53
54 dev->next = serial_devices;
55 serial_devices = dev;
281e00a3
WD
56}
57
a6e6f7f4 58void serial_initialize(void)
281e00a3
WD
59{
60#if defined(CONFIG_8xx_CONS_SMC1) || defined(CONFIG_8xx_CONS_SMC2)
a6e6f7f4 61 serial_register(&serial_smc_device);
281e00a3 62#endif
a6e6f7f4
GF
63#if defined(CONFIG_8xx_CONS_SCC1) || defined(CONFIG_8xx_CONS_SCC2) || \
64 defined(CONFIG_8xx_CONS_SCC3) || defined(CONFIG_8xx_CONS_SCC4)
65 serial_register(&serial_scc_device);
281e00a3
WD
66#endif
67
6d0f6bcf
JCPV
68#if defined(CONFIG_SYS_NS16550_SERIAL)
69#if defined(CONFIG_SYS_NS16550_COM1)
0fd30252
WD
70 serial_register(&eserial1_device);
71#endif
6d0f6bcf 72#if defined(CONFIG_SYS_NS16550_COM2)
0fd30252
WD
73 serial_register(&eserial2_device);
74#endif
6d0f6bcf 75#if defined(CONFIG_SYS_NS16550_COM3)
0fd30252
WD
76 serial_register(&eserial3_device);
77#endif
6d0f6bcf 78#if defined(CONFIG_SYS_NS16550_COM4)
0fd30252
WD
79 serial_register(&eserial4_device);
80#endif
6d0f6bcf 81#endif /* CONFIG_SYS_NS16550_SERIAL */
a6e6f7f4 82#if defined(CONFIG_FFUART)
80172c61
SB
83 serial_register(&serial_ffuart_device);
84#endif
a6e6f7f4 85#if defined(CONFIG_BTUART)
80172c61
SB
86 serial_register(&serial_btuart_device);
87#endif
a6e6f7f4 88#if defined(CONFIG_STUART)
80172c61 89 serial_register(&serial_stuart_device);
a7c185ed
HW
90#endif
91#if defined(CONFIG_S3C2410)
92 serial_register(&s3c24xx_serial0_device);
93 serial_register(&s3c24xx_serial1_device);
94 serial_register(&s3c24xx_serial2_device);
dd2c9e6a 95#endif
889a275d 96#if defined(CONFIG_S5P)
46a3b5c8
MK
97 serial_register(&s5p_serial0_device);
98 serial_register(&s5p_serial1_device);
99 serial_register(&s5p_serial2_device);
100 serial_register(&s5p_serial3_device);
e3b28e67
AG
101#endif
102#if defined(CONFIG_MPC512X)
103#if defined(CONFIG_SYS_PSC1)
104 serial_register(&serial1_device);
105#endif
106#if defined(CONFIG_SYS_PSC3)
107 serial_register(&serial3_device);
108#endif
109#if defined(CONFIG_SYS_PSC4)
110 serial_register(&serial4_device);
111#endif
112#if defined(CONFIG_SYS_PSC6)
113 serial_register(&serial6_device);
114#endif
635f330f
MF
115#endif
116#if defined(CONFIG_SYS_BFIN_UART)
117 serial_register_bfin_uart();
80172c61 118#endif
49a23e4a
MS
119#if defined(CONFIG_XILINX_UARTLITE)
120# ifdef XILINX_UARTLITE_BASEADDR
121 serial_register(&uartlite_serial0_device);
122# endif /* XILINX_UARTLITE_BASEADDR */
123# ifdef XILINX_UARTLITE_BASEADDR1
124 serial_register(&uartlite_serial1_device);
125# endif /* XILINX_UARTLITE_BASEADDR1 */
126# ifdef XILINX_UARTLITE_BASEADDR2
127 serial_register(&uartlite_serial2_device);
128# endif /* XILINX_UARTLITE_BASEADDR2 */
129# ifdef XILINX_UARTLITE_BASEADDR3
130 serial_register(&uartlite_serial3_device);
131# endif /* XILINX_UARTLITE_BASEADDR3 */
132#endif /* CONFIG_XILINX_UARTLITE */
194846f3
MS
133#if defined(CONFIG_ZYNQ_SERIAL)
134# ifdef CONFIG_ZYNQ_SERIAL_BASEADDR0
135 serial_register(&uart_zynq_serial0_device);
136# endif
137# ifdef CONFIG_ZYNQ_SERIAL_BASEADDR1
138 serial_register(&uart_zynq_serial1_device);
139# endif
140#endif
a6e6f7f4 141 serial_assign(default_serial_console()->name);
281e00a3
WD
142}
143
a6e6f7f4 144void serial_stdio_init(void)
281e00a3 145{
52cb4d4f 146 struct stdio_dev dev;
281e00a3
WD
147 struct serial_device *s = serial_devices;
148
2ee66533 149 while (s) {
a6e6f7f4 150 memset(&dev, 0, sizeof(dev));
281e00a3 151
a6e6f7f4 152 strcpy(dev.name, s->name);
281e00a3
WD
153 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
154
89143fb3
MV
155 dev.start = s->start;
156 dev.stop = s->stop;
281e00a3
WD
157 dev.putc = s->putc;
158 dev.puts = s->puts;
159 dev.getc = s->getc;
160 dev.tstc = s->tstc;
161
a6e6f7f4 162 stdio_register(&dev);
281e00a3
WD
163
164 s = s->next;
165 }
166}
167
7813ca9b 168int serial_assign(const char *name)
281e00a3
WD
169{
170 struct serial_device *s;
171
2ee66533 172 for (s = serial_devices; s; s = s->next) {
a6e6f7f4 173 if (strcmp(s->name, name) == 0) {
281e00a3
WD
174 serial_current = s;
175 return 0;
176 }
177 }
178
179 return 1;
180}
181
a6e6f7f4 182void serial_reinit_all(void)
281e00a3
WD
183{
184 struct serial_device *s;
185
a6e6f7f4 186 for (s = serial_devices; s; s = s->next)
89143fb3 187 s->start();
281e00a3
WD
188}
189
857c283e 190static struct serial_device *get_current(void)
281e00a3 191{
857c283e 192 struct serial_device *dev;
2ee66533 193
857c283e
SG
194 if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
195 dev = default_serial_console();
196
197 /* We must have a console device */
198 if (!dev)
199 panic("Cannot find console");
200 } else
201 dev = serial_current;
202 return dev;
203}
281e00a3 204
857c283e
SG
205int serial_init(void)
206{
89143fb3 207 return get_current()->start();
281e00a3
WD
208}
209
a6e6f7f4 210void serial_setbrg(void)
281e00a3 211{
857c283e 212 get_current()->setbrg();
281e00a3
WD
213}
214
a6e6f7f4 215int serial_getc(void)
281e00a3 216{
857c283e 217 return get_current()->getc();
281e00a3
WD
218}
219
a6e6f7f4 220int serial_tstc(void)
281e00a3 221{
857c283e 222 return get_current()->tstc();
281e00a3
WD
223}
224
a6e6f7f4 225void serial_putc(const char c)
281e00a3 226{
857c283e 227 get_current()->putc(c);
281e00a3
WD
228}
229
a6e6f7f4 230void serial_puts(const char *s)
281e00a3 231{
857c283e 232 get_current()->puts(s);
281e00a3 233}
7b826c2f
MF
234
235#if CONFIG_POST & CONFIG_SYS_POST_UART
236static const int bauds[] = CONFIG_SYS_BAUDRATE_TABLE;
237
238/* Mark weak until post/cpu/.../uart.c migrate over */
239__weak
240int uart_post_test(int flags)
241{
242 unsigned char c;
243 int ret, saved_baud, b;
244 struct serial_device *saved_dev, *s;
245 bd_t *bd = gd->bd;
246
247 /* Save current serial state */
248 ret = 0;
249 saved_dev = serial_current;
250 saved_baud = bd->bi_baudrate;
251
252 for (s = serial_devices; s; s = s->next) {
253 /* If this driver doesn't support loop back, skip it */
254 if (!s->loop)
255 continue;
256
257 /* Test the next device */
258 serial_current = s;
259
260 ret = serial_init();
261 if (ret)
262 goto done;
263
264 /* Consume anything that happens to be queued */
265 while (serial_tstc())
266 serial_getc();
267
268 /* Enable loop back */
269 s->loop(1);
270
271 /* Test every available baud rate */
272 for (b = 0; b < ARRAY_SIZE(bauds); ++b) {
273 bd->bi_baudrate = bauds[b];
274 serial_setbrg();
275
276 /*
277 * Stick to printable chars to avoid issues:
278 * - terminal corruption
279 * - serial program reacting to sequences and sending
280 * back random extra data
281 * - most serial drivers add in extra chars (like \r\n)
282 */
283 for (c = 0x20; c < 0x7f; ++c) {
284 /* Send it out */
285 serial_putc(c);
286
287 /* Make sure it's the same one */
288 ret = (c != serial_getc());
289 if (ret) {
290 s->loop(0);
291 goto done;
292 }
293
294 /* Clean up the output in case it was sent */
295 serial_putc('\b');
296 ret = ('\b' != serial_getc());
297 if (ret) {
298 s->loop(0);
299 goto done;
300 }
301 }
302 }
303
304 /* Disable loop back */
305 s->loop(0);
306
89143fb3
MV
307 /* XXX: There is no serial_stop() !? */
308 if (s->stop)
309 s->stop();
7b826c2f
MF
310 }
311
312 done:
313 /* Restore previous serial state */
314 serial_current = saved_dev;
315 bd->bi_baudrate = saved_baud;
316 serial_reinit_all();
317 serial_setbrg();
318
319 return ret;
320}
321#endif