blob: 7423920b9316ca43b693a493361edef186ba9783 [file] [log] [blame]
wdenk7ac16102004-08-01 22:48:16 +00001/*
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>
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +020026#include <stdio_dev.h>
Mike Frysinger078f2f12011-05-14 06:56:15 +000027#include <post.h>
28#include <linux/compiler.h>
wdenk7ac16102004-08-01 22:48:16 +000029
Wolfgang Denk6405a152006-03-31 18:32:53 +020030DECLARE_GLOBAL_DATA_PTR;
31
Gerlando Falauto34148d62011-11-18 06:49:11 +000032static struct serial_device *serial_devices;
33static struct serial_device *serial_current;
wdenk7ac16102004-08-01 22:48:16 +000034
Marek Vasutf6af0482012-09-12 17:49:58 +020035static void serial_null(void)
36{
37}
38
39#define serial_initfunc(name) \
40 void name(void) \
41 __attribute__((weak, alias("serial_null")));
42
Mike Frysingerffadc822011-04-29 18:03:30 +000043void serial_register(struct serial_device *dev)
wdenk7ac16102004-08-01 22:48:16 +000044{
Wolfgang Denkd0813e52010-10-28 20:00:11 +020045#ifdef CONFIG_NEEDS_MANUAL_RELOC
Marek Vasutb46931d2012-09-07 14:35:31 +020046 dev->start += gd->reloc_off;
wdenk7ac16102004-08-01 22:48:16 +000047 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;
Peter Tyser9057cbf2009-09-21 11:20:36 -050052#endif
wdenk7ac16102004-08-01 22:48:16 +000053
54 dev->next = serial_devices;
55 serial_devices = dev;
wdenk7ac16102004-08-01 22:48:16 +000056}
57
Gerlando Falauto34148d62011-11-18 06:49:11 +000058void serial_initialize(void)
wdenk7ac16102004-08-01 22:48:16 +000059{
60#if defined(CONFIG_8xx_CONS_SMC1) || defined(CONFIG_8xx_CONS_SMC2)
Gerlando Falauto34148d62011-11-18 06:49:11 +000061 serial_register(&serial_smc_device);
wdenk7ac16102004-08-01 22:48:16 +000062#endif
Gerlando Falauto34148d62011-11-18 06:49:11 +000063#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);
wdenk7ac16102004-08-01 22:48:16 +000066#endif
67
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020068#if defined(CONFIG_SYS_NS16550_SERIAL)
69#if defined(CONFIG_SYS_NS16550_COM1)
Wolfgang Denk44df5612006-08-30 23:02:10 +020070 serial_register(&eserial1_device);
71#endif
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020072#if defined(CONFIG_SYS_NS16550_COM2)
Wolfgang Denk44df5612006-08-30 23:02:10 +020073 serial_register(&eserial2_device);
74#endif
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020075#if defined(CONFIG_SYS_NS16550_COM3)
Wolfgang Denk44df5612006-08-30 23:02:10 +020076 serial_register(&eserial3_device);
77#endif
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020078#if defined(CONFIG_SYS_NS16550_COM4)
Wolfgang Denk44df5612006-08-30 23:02:10 +020079 serial_register(&eserial4_device);
80#endif
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020081#endif /* CONFIG_SYS_NS16550_SERIAL */
Gerlando Falauto34148d62011-11-18 06:49:11 +000082#if defined(CONFIG_FFUART)
stefano babic5ab4f032007-08-30 22:57:04 +020083 serial_register(&serial_ffuart_device);
84#endif
Gerlando Falauto34148d62011-11-18 06:49:11 +000085#if defined(CONFIG_BTUART)
stefano babic5ab4f032007-08-30 22:57:04 +020086 serial_register(&serial_btuart_device);
87#endif
Gerlando Falauto34148d62011-11-18 06:49:11 +000088#if defined(CONFIG_STUART)
stefano babic5ab4f032007-08-30 22:57:04 +020089 serial_register(&serial_stuart_device);
90#endif
Harald Welteda412622007-12-19 14:24:40 +010091#if defined(CONFIG_S3C2410)
92 serial_register(&s3c24xx_serial0_device);
93 serial_register(&s3c24xx_serial1_device);
94 serial_register(&s3c24xx_serial2_device);
95#endif
Minkyu Kang485feb62010-08-23 19:52:03 +090096#if defined(CONFIG_S5P)
Minkyu Kangbaa36882010-03-24 16:59:30 +090097 serial_register(&s5p_serial0_device);
98 serial_register(&s5p_serial1_device);
99 serial_register(&s5p_serial2_device);
100 serial_register(&s5p_serial3_device);
Minkyu Kangfca30842009-10-01 17:20:28 +0900101#endif
Anatolij Gustschinee4aac82010-04-24 19:27:05 +0200102#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
115#endif
Mike Frysinger53ba3222011-04-29 23:23:28 -0400116#if defined(CONFIG_SYS_BFIN_UART)
117 serial_register_bfin_uart();
118#endif
Michal Simek75348da2011-09-25 21:03:08 +0000119#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 */
Michal Simek76bed832012-09-14 00:55:24 +0000133#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
Gerlando Falauto34148d62011-11-18 06:49:11 +0000141 serial_assign(default_serial_console()->name);
wdenk7ac16102004-08-01 22:48:16 +0000142}
143
Gerlando Falauto34148d62011-11-18 06:49:11 +0000144void serial_stdio_init(void)
wdenk7ac16102004-08-01 22:48:16 +0000145{
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200146 struct stdio_dev dev;
wdenk7ac16102004-08-01 22:48:16 +0000147 struct serial_device *s = serial_devices;
148
wdenk8f08c5b2004-10-11 22:43:02 +0000149 while (s) {
Gerlando Falauto34148d62011-11-18 06:49:11 +0000150 memset(&dev, 0, sizeof(dev));
wdenk7ac16102004-08-01 22:48:16 +0000151
Gerlando Falauto34148d62011-11-18 06:49:11 +0000152 strcpy(dev.name, s->name);
wdenk7ac16102004-08-01 22:48:16 +0000153 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
154
Marek Vasutb46931d2012-09-07 14:35:31 +0200155 dev.start = s->start;
156 dev.stop = s->stop;
wdenk7ac16102004-08-01 22:48:16 +0000157 dev.putc = s->putc;
158 dev.puts = s->puts;
159 dev.getc = s->getc;
160 dev.tstc = s->tstc;
161
Gerlando Falauto34148d62011-11-18 06:49:11 +0000162 stdio_register(&dev);
wdenk7ac16102004-08-01 22:48:16 +0000163
164 s = s->next;
165 }
166}
167
Gerlando Falauto92999582011-11-18 06:49:12 +0000168int serial_assign(const char *name)
wdenk7ac16102004-08-01 22:48:16 +0000169{
170 struct serial_device *s;
171
wdenk8f08c5b2004-10-11 22:43:02 +0000172 for (s = serial_devices; s; s = s->next) {
Gerlando Falauto34148d62011-11-18 06:49:11 +0000173 if (strcmp(s->name, name) == 0) {
wdenk7ac16102004-08-01 22:48:16 +0000174 serial_current = s;
175 return 0;
176 }
177 }
178
179 return 1;
180}
181
Gerlando Falauto34148d62011-11-18 06:49:11 +0000182void serial_reinit_all(void)
wdenk7ac16102004-08-01 22:48:16 +0000183{
184 struct serial_device *s;
185
Gerlando Falauto34148d62011-11-18 06:49:11 +0000186 for (s = serial_devices; s; s = s->next)
Marek Vasutb46931d2012-09-07 14:35:31 +0200187 s->start();
wdenk7ac16102004-08-01 22:48:16 +0000188}
189
Simon Glass8f4e6f12011-08-19 11:09:43 +0000190static struct serial_device *get_current(void)
wdenk7ac16102004-08-01 22:48:16 +0000191{
Simon Glass8f4e6f12011-08-19 11:09:43 +0000192 struct serial_device *dev;
193
wdenk8f08c5b2004-10-11 22:43:02 +0000194 if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
Simon Glass8f4e6f12011-08-19 11:09:43 +0000195 dev = default_serial_console();
wdenk8f08c5b2004-10-11 22:43:02 +0000196
Simon Glass8f4e6f12011-08-19 11:09:43 +0000197 /* We must have a console device */
198 if (!dev)
199 panic("Cannot find console");
200 } else
201 dev = serial_current;
202 return dev;
203}
wdenk7ac16102004-08-01 22:48:16 +0000204
Simon Glass8f4e6f12011-08-19 11:09:43 +0000205int serial_init(void)
206{
Marek Vasutb46931d2012-09-07 14:35:31 +0200207 return get_current()->start();
wdenk7ac16102004-08-01 22:48:16 +0000208}
209
Gerlando Falauto34148d62011-11-18 06:49:11 +0000210void serial_setbrg(void)
wdenk7ac16102004-08-01 22:48:16 +0000211{
Simon Glass8f4e6f12011-08-19 11:09:43 +0000212 get_current()->setbrg();
wdenk7ac16102004-08-01 22:48:16 +0000213}
214
Gerlando Falauto34148d62011-11-18 06:49:11 +0000215int serial_getc(void)
wdenk7ac16102004-08-01 22:48:16 +0000216{
Simon Glass8f4e6f12011-08-19 11:09:43 +0000217 return get_current()->getc();
wdenk7ac16102004-08-01 22:48:16 +0000218}
219
Gerlando Falauto34148d62011-11-18 06:49:11 +0000220int serial_tstc(void)
wdenk7ac16102004-08-01 22:48:16 +0000221{
Simon Glass8f4e6f12011-08-19 11:09:43 +0000222 return get_current()->tstc();
wdenk7ac16102004-08-01 22:48:16 +0000223}
224
Gerlando Falauto34148d62011-11-18 06:49:11 +0000225void serial_putc(const char c)
wdenk7ac16102004-08-01 22:48:16 +0000226{
Simon Glass8f4e6f12011-08-19 11:09:43 +0000227 get_current()->putc(c);
wdenk7ac16102004-08-01 22:48:16 +0000228}
229
Gerlando Falauto34148d62011-11-18 06:49:11 +0000230void serial_puts(const char *s)
wdenk7ac16102004-08-01 22:48:16 +0000231{
Simon Glass8f4e6f12011-08-19 11:09:43 +0000232 get_current()->puts(s);
wdenk7ac16102004-08-01 22:48:16 +0000233}
Mike Frysinger078f2f12011-05-14 06:56:15 +0000234
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
Marek Vasutb46931d2012-09-07 14:35:31 +0200307 /* XXX: There is no serial_stop() !? */
308 if (s->stop)
309 s->stop();
Mike Frysinger078f2f12011-05-14 06:56:15 +0000310 }
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