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