]> git.karo-electronics.de Git - karo-tx-uboot.git/blob - common/serial.c
7423920b9316ca43b693a493361edef186ba9783
[karo-tx-uboot.git] / common / serial.c
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>
26 #include <stdio_dev.h>
27 #include <post.h>
28 #include <linux/compiler.h>
29
30 DECLARE_GLOBAL_DATA_PTR;
31
32 static struct serial_device *serial_devices;
33 static struct serial_device *serial_current;
34
35 static void serial_null(void)
36 {
37 }
38
39 #define serial_initfunc(name)                                   \
40         void name(void)                                         \
41                 __attribute__((weak, alias("serial_null")));
42
43 void serial_register(struct serial_device *dev)
44 {
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;
52 #endif
53
54         dev->next = serial_devices;
55         serial_devices = dev;
56 }
57
58 void serial_initialize(void)
59 {
60 #if defined(CONFIG_8xx_CONS_SMC1) || defined(CONFIG_8xx_CONS_SMC2)
61         serial_register(&serial_smc_device);
62 #endif
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);
66 #endif
67
68 #if defined(CONFIG_SYS_NS16550_SERIAL)
69 #if defined(CONFIG_SYS_NS16550_COM1)
70         serial_register(&eserial1_device);
71 #endif
72 #if defined(CONFIG_SYS_NS16550_COM2)
73         serial_register(&eserial2_device);
74 #endif
75 #if defined(CONFIG_SYS_NS16550_COM3)
76         serial_register(&eserial3_device);
77 #endif
78 #if defined(CONFIG_SYS_NS16550_COM4)
79         serial_register(&eserial4_device);
80 #endif
81 #endif /* CONFIG_SYS_NS16550_SERIAL */
82 #if defined(CONFIG_FFUART)
83         serial_register(&serial_ffuart_device);
84 #endif
85 #if defined(CONFIG_BTUART)
86         serial_register(&serial_btuart_device);
87 #endif
88 #if defined(CONFIG_STUART)
89         serial_register(&serial_stuart_device);
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);
95 #endif
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);
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
115 #endif
116 #if defined(CONFIG_SYS_BFIN_UART)
117         serial_register_bfin_uart();
118 #endif
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);
136 # endif
137 # ifdef CONFIG_ZYNQ_SERIAL_BASEADDR1
138         serial_register(&uart_zynq_serial1_device);
139 # endif
140 #endif
141         serial_assign(default_serial_console()->name);
142 }
143
144 void serial_stdio_init(void)
145 {
146         struct stdio_dev dev;
147         struct serial_device *s = serial_devices;
148
149         while (s) {
150                 memset(&dev, 0, sizeof(dev));
151
152                 strcpy(dev.name, s->name);
153                 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
154
155                 dev.start = s->start;
156                 dev.stop = s->stop;
157                 dev.putc = s->putc;
158                 dev.puts = s->puts;
159                 dev.getc = s->getc;
160                 dev.tstc = s->tstc;
161
162                 stdio_register(&dev);
163
164                 s = s->next;
165         }
166 }
167
168 int serial_assign(const char *name)
169 {
170         struct serial_device *s;
171
172         for (s = serial_devices; s; s = s->next) {
173                 if (strcmp(s->name, name) == 0) {
174                         serial_current = s;
175                         return 0;
176                 }
177         }
178
179         return 1;
180 }
181
182 void serial_reinit_all(void)
183 {
184         struct serial_device *s;
185
186         for (s = serial_devices; s; s = s->next)
187                 s->start();
188 }
189
190 static struct serial_device *get_current(void)
191 {
192         struct serial_device *dev;
193
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 }
204
205 int serial_init(void)
206 {
207         return get_current()->start();
208 }
209
210 void serial_setbrg(void)
211 {
212         get_current()->setbrg();
213 }
214
215 int serial_getc(void)
216 {
217         return get_current()->getc();
218 }
219
220 int serial_tstc(void)
221 {
222         return get_current()->tstc();
223 }
224
225 void serial_putc(const char c)
226 {
227         get_current()->putc(c);
228 }
229
230 void serial_puts(const char *s)
231 {
232         get_current()->puts(s);
233 }
234
235 #if CONFIG_POST & CONFIG_SYS_POST_UART
236 static const int bauds[] = CONFIG_SYS_BAUDRATE_TABLE;
237
238 /* Mark weak until post/cpu/.../uart.c migrate over */
239 __weak
240 int 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
307                 /* XXX: There is no serial_stop() !? */
308                 if (s->stop)
309                         s->stop();
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