3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
26 #include <stdio_dev.h>
28 #include <linux/compiler.h>
30 DECLARE_GLOBAL_DATA_PTR;
32 static struct serial_device *serial_devices;
33 static struct serial_device *serial_current;
35 static void serial_null(void)
39 #define serial_initfunc(name) \
41 __attribute__((weak, alias("serial_null")));
43 void serial_register(struct serial_device *dev)
45 #ifdef CONFIG_NEEDS_MANUAL_RELOC
46 dev->start += gd->reloc_off;
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;
54 dev->next = serial_devices;
58 void serial_initialize(void)
60 #if defined(CONFIG_8xx_CONS_SMC1) || defined(CONFIG_8xx_CONS_SMC2)
61 serial_register(&serial_smc_device);
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);
68 #if defined(CONFIG_SYS_NS16550_SERIAL)
69 #if defined(CONFIG_SYS_NS16550_COM1)
70 serial_register(&eserial1_device);
72 #if defined(CONFIG_SYS_NS16550_COM2)
73 serial_register(&eserial2_device);
75 #if defined(CONFIG_SYS_NS16550_COM3)
76 serial_register(&eserial3_device);
78 #if defined(CONFIG_SYS_NS16550_COM4)
79 serial_register(&eserial4_device);
81 #endif /* CONFIG_SYS_NS16550_SERIAL */
82 #if defined(CONFIG_FFUART)
83 serial_register(&serial_ffuart_device);
85 #if defined(CONFIG_BTUART)
86 serial_register(&serial_btuart_device);
88 #if defined(CONFIG_STUART)
89 serial_register(&serial_stuart_device);
91 #if defined(CONFIG_S3C2410)
92 serial_register(&s3c24xx_serial0_device);
93 serial_register(&s3c24xx_serial1_device);
94 serial_register(&s3c24xx_serial2_device);
96 #if defined(CONFIG_S5P)
97 serial_register(&s5p_serial0_device);
98 serial_register(&s5p_serial1_device);
99 serial_register(&s5p_serial2_device);
100 serial_register(&s5p_serial3_device);
102 #if defined(CONFIG_MPC512X)
103 #if defined(CONFIG_SYS_PSC1)
104 serial_register(&serial1_device);
106 #if defined(CONFIG_SYS_PSC3)
107 serial_register(&serial3_device);
109 #if defined(CONFIG_SYS_PSC4)
110 serial_register(&serial4_device);
112 #if defined(CONFIG_SYS_PSC6)
113 serial_register(&serial6_device);
116 #if defined(CONFIG_SYS_BFIN_UART)
117 serial_register_bfin_uart();
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 */
133 #if defined(CONFIG_ZYNQ_SERIAL)
134 # ifdef CONFIG_ZYNQ_SERIAL_BASEADDR0
135 serial_register(&uart_zynq_serial0_device);
137 # ifdef CONFIG_ZYNQ_SERIAL_BASEADDR1
138 serial_register(&uart_zynq_serial1_device);
141 serial_assign(default_serial_console()->name);
144 void serial_stdio_init(void)
146 struct stdio_dev dev;
147 struct serial_device *s = serial_devices;
150 memset(&dev, 0, sizeof(dev));
152 strcpy(dev.name, s->name);
153 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
155 dev.start = s->start;
162 stdio_register(&dev);
168 int serial_assign(const char *name)
170 struct serial_device *s;
172 for (s = serial_devices; s; s = s->next) {
173 if (strcmp(s->name, name) == 0) {
182 void serial_reinit_all(void)
184 struct serial_device *s;
186 for (s = serial_devices; s; s = s->next)
190 static struct serial_device *get_current(void)
192 struct serial_device *dev;
194 if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
195 dev = default_serial_console();
197 /* We must have a console device */
199 panic("Cannot find console");
201 dev = serial_current;
205 int serial_init(void)
207 return get_current()->start();
210 void serial_setbrg(void)
212 get_current()->setbrg();
215 int serial_getc(void)
217 return get_current()->getc();
220 int serial_tstc(void)
222 return get_current()->tstc();
225 void serial_putc(const char c)
227 get_current()->putc(c);
230 void serial_puts(const char *s)
232 get_current()->puts(s);
235 #if CONFIG_POST & CONFIG_SYS_POST_UART
236 static const int bauds[] = CONFIG_SYS_BAUDRATE_TABLE;
238 /* Mark weak until post/cpu/.../uart.c migrate over */
240 int uart_post_test(int flags)
243 int ret, saved_baud, b;
244 struct serial_device *saved_dev, *s;
247 /* Save current serial state */
249 saved_dev = serial_current;
250 saved_baud = bd->bi_baudrate;
252 for (s = serial_devices; s; s = s->next) {
253 /* If this driver doesn't support loop back, skip it */
257 /* Test the next device */
264 /* Consume anything that happens to be queued */
265 while (serial_tstc())
268 /* Enable loop back */
271 /* Test every available baud rate */
272 for (b = 0; b < ARRAY_SIZE(bauds); ++b) {
273 bd->bi_baudrate = bauds[b];
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)
283 for (c = 0x20; c < 0x7f; ++c) {
287 /* Make sure it's the same one */
288 ret = (c != serial_getc());
294 /* Clean up the output in case it was sent */
296 ret = ('\b' != serial_getc());
304 /* Disable loop back */
307 /* XXX: There is no serial_stop() !? */
313 /* Restore previous serial state */
314 serial_current = saved_dev;
315 bd->bi_baudrate = saved_baud;