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 serial_initfunc(mpc8xx_serial_initialize);
44 serial_initfunc(ns16550_serial_initialize);
45 serial_initfunc(pxa_serial_initialize);
46 serial_initfunc(s3c24xx_serial_initialize);
47 serial_initfunc(s5p_serial_initialize);
48 serial_initfunc(zynq_serial_initalize);
49 serial_initfunc(bfin_serial_initialize);
50 serial_initfunc(bfin_jtag_initialize);
51 serial_initfunc(mpc512x_serial_initialize);
52 serial_initfunc(uartlite_serial_initialize);
53 serial_initfunc(au1x00_serial_initialize);
54 serial_initfunc(asc_serial_initialize);
55 serial_initfunc(jz_serial_initialize);
56 serial_initfunc(mpc5xx_serial_initialize);
57 serial_initfunc(mpc8220_serial_initialize);
58 serial_initfunc(mpc8260_scc_serial_initialize);
59 serial_initfunc(mpc8260_smc_serial_initialize);
60 serial_initfunc(mpc85xx_serial_initialize);
61 serial_initfunc(iop480_serial_initialize);
62 serial_initfunc(leon2_serial_initialize);
63 serial_initfunc(leon3_serial_initialize);
64 serial_initfunc(marvell_serial_initialize);
65 serial_initfunc(amirix_serial_initialize);
66 serial_initfunc(bmw_serial_initialize);
67 serial_initfunc(cogent_serial_initialize);
68 serial_initfunc(cpci750_serial_initialize);
69 serial_initfunc(evb64260_serial_initialize);
70 serial_initfunc(ml2_serial_initialize);
71 serial_initfunc(sconsole_serial_initialize);
72 serial_initfunc(p3mx_serial_initialize);
73 serial_initfunc(altera_jtag_serial_initialize);
74 serial_initfunc(altera_serial_initialize);
75 serial_initfunc(atmel_serial_initialize);
76 serial_initfunc(lpc32xx_serial_initialize);
77 serial_initfunc(mcf_serial_initialize);
78 serial_initfunc(ns9750_serial_initialize);
79 serial_initfunc(oc_serial_initialize);
80 serial_initfunc(s3c4510b_serial_initialize);
81 serial_initfunc(s3c64xx_serial_initialize);
82 serial_initfunc(sandbox_serial_initialize);
83 serial_initfunc(clps7111_serial_initialize);
84 serial_initfunc(imx_serial_initialize);
86 void serial_register(struct serial_device *dev)
88 #ifdef CONFIG_NEEDS_MANUAL_RELOC
89 dev->start += gd->reloc_off;
90 dev->setbrg += gd->reloc_off;
91 dev->getc += gd->reloc_off;
92 dev->tstc += gd->reloc_off;
93 dev->putc += gd->reloc_off;
94 dev->puts += gd->reloc_off;
97 dev->next = serial_devices;
101 void serial_initialize(void)
103 mpc8xx_serial_initialize();
104 ns16550_serial_initialize();
105 pxa_serial_initialize();
106 s3c24xx_serial_initialize();
107 s5p_serial_initialize();
108 mpc512x_serial_initialize();
109 bfin_serial_initialize();
110 bfin_jtag_initialize();
111 uartlite_serial_initialize();
112 zynq_serial_initalize();
113 au1x00_serial_initialize();
114 asc_serial_initialize();
115 jz_serial_initialize();
116 mpc5xx_serial_initialize();
117 mpc8220_serial_initialize();
118 mpc8260_scc_serial_initialize();
119 mpc8260_smc_serial_initialize();
120 mpc85xx_serial_initialize();
121 iop480_serial_initialize();
122 leon2_serial_initialize();
123 leon3_serial_initialize();
124 marvell_serial_initialize();
125 amirix_serial_initialize();
126 bmw_serial_initialize();
127 cogent_serial_initialize();
128 cpci750_serial_initialize();
129 evb64260_serial_initialize();
130 ml2_serial_initialize();
131 sconsole_serial_initialize();
132 p3mx_serial_initialize();
133 altera_jtag_serial_initialize();
134 altera_serial_initialize();
135 atmel_serial_initialize();
136 lpc32xx_serial_initialize();
137 mcf_serial_initialize();
138 ns9750_serial_initialize();
139 oc_serial_initialize();
140 s3c4510b_serial_initialize();
141 s3c64xx_serial_initialize();
142 sandbox_serial_initialize();
143 clps7111_serial_initialize();
144 imx_serial_initialize();
146 serial_assign(default_serial_console()->name);
149 void serial_stdio_init(void)
151 struct stdio_dev dev;
152 struct serial_device *s = serial_devices;
155 memset(&dev, 0, sizeof(dev));
157 strcpy(dev.name, s->name);
158 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
160 dev.start = s->start;
167 stdio_register(&dev);
173 int serial_assign(const char *name)
175 struct serial_device *s;
177 for (s = serial_devices; s; s = s->next) {
178 if (strcmp(s->name, name) == 0) {
187 void serial_reinit_all(void)
189 struct serial_device *s;
191 for (s = serial_devices; s; s = s->next)
195 static struct serial_device *get_current(void)
197 struct serial_device *dev;
199 if (!(gd->flags & GD_FLG_RELOC) || !serial_current) {
200 dev = default_serial_console();
202 /* We must have a console device */
204 panic("Cannot find console");
206 dev = serial_current;
210 int serial_init(void)
212 return get_current()->start();
215 void serial_setbrg(void)
217 get_current()->setbrg();
220 int serial_getc(void)
222 return get_current()->getc();
225 int serial_tstc(void)
227 return get_current()->tstc();
230 void serial_putc(const char c)
232 get_current()->putc(c);
235 void serial_puts(const char *s)
237 get_current()->puts(s);
240 #if CONFIG_POST & CONFIG_SYS_POST_UART
241 static const int bauds[] = CONFIG_SYS_BAUDRATE_TABLE;
243 /* Mark weak until post/cpu/.../uart.c migrate over */
245 int uart_post_test(int flags)
248 int ret, saved_baud, b;
249 struct serial_device *saved_dev, *s;
252 /* Save current serial state */
254 saved_dev = serial_current;
255 saved_baud = bd->bi_baudrate;
257 for (s = serial_devices; s; s = s->next) {
258 /* If this driver doesn't support loop back, skip it */
262 /* Test the next device */
269 /* Consume anything that happens to be queued */
270 while (serial_tstc())
273 /* Enable loop back */
276 /* Test every available baud rate */
277 for (b = 0; b < ARRAY_SIZE(bauds); ++b) {
278 bd->bi_baudrate = bauds[b];
282 * Stick to printable chars to avoid issues:
283 * - terminal corruption
284 * - serial program reacting to sequences and sending
285 * back random extra data
286 * - most serial drivers add in extra chars (like \r\n)
288 for (c = 0x20; c < 0x7f; ++c) {
292 /* Make sure it's the same one */
293 ret = (c != serial_getc());
299 /* Clean up the output in case it was sent */
301 ret = ('\b' != serial_getc());
309 /* Disable loop back */
312 /* XXX: There is no serial_stop() !? */
318 /* Restore previous serial state */
319 serial_current = saved_dev;
320 bd->bi_baudrate = saved_baud;