]>
Commit | Line | Data |
---|---|---|
281e00a3 WD |
1 | /* |
2 | * (C) Copyright 2004 | |
3 | * Wolfgang Denk, DENX Software Engineering, [email protected]. | |
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 |
30 | DECLARE_GLOBAL_DATA_PTR; |
31 | ||
a6e6f7f4 GF |
32 | static struct serial_device *serial_devices; |
33 | static struct serial_device *serial_current; | |
281e00a3 | 34 | |
2a333aeb MV |
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 | ||
f0eb1f61 | 43 | serial_initfunc(mpc8xx_serial_initialize); |
abc0ed8d | 44 | serial_initfunc(ns16550_serial_initialize); |
1fe5c110 | 45 | serial_initfunc(pxa_serial_initialize); |
28af6385 | 46 | serial_initfunc(s3c24xx_serial_initialize); |
b4980515 | 47 | serial_initfunc(s5p_serial_initialize); |
51d8102f | 48 | serial_initfunc(zynq_serial_initalize); |
5ae1de0d | 49 | serial_initfunc(bfin_serial_initialize); |
41651cab | 50 | serial_initfunc(bfin_jtag_initialize); |
918327c8 | 51 | serial_initfunc(mpc512x_serial_initialize); |
87d69229 | 52 | serial_initfunc(uartlite_serial_initialize); |
7b953c51 | 53 | serial_initfunc(au1x00_serial_initialize); |
01911422 | 54 | serial_initfunc(asc_serial_initialize); |
6cb3273d | 55 | serial_initfunc(jz_serial_initialize); |
b57c6528 | 56 | serial_initfunc(mpc5xx_serial_initialize); |
2063a54e | 57 | serial_initfunc(mpc8220_serial_initialize); |
d68f4da2 MV |
58 | serial_initfunc(mpc8260_scc_serial_initialize); |
59 | serial_initfunc(mpc8260_smc_serial_initialize); | |
7a311546 | 60 | serial_initfunc(mpc85xx_serial_initialize); |
fcc2074d | 61 | serial_initfunc(iop480_serial_initialize); |
af7be3b6 | 62 | serial_initfunc(leon2_serial_initialize); |
29b5ff7f | 63 | serial_initfunc(leon3_serial_initialize); |
afe4c389 | 64 | serial_initfunc(marvell_serial_initialize); |
5dee6f55 | 65 | serial_initfunc(amirix_serial_initialize); |
c45a14ad | 66 | serial_initfunc(bmw_serial_initialize); |
d25c39d3 | 67 | serial_initfunc(cogent_serial_initialize); |
619c90bb | 68 | serial_initfunc(cpci750_serial_initialize); |
829b3b2e | 69 | serial_initfunc(evb64260_serial_initialize); |
d126f285 | 70 | serial_initfunc(ml2_serial_initialize); |
26d8eaaa | 71 | serial_initfunc(sconsole_serial_initialize); |
088a4f3c | 72 | serial_initfunc(p3mx_serial_initialize); |
8c085757 | 73 | serial_initfunc(altera_jtag_serial_initialize); |
b207d645 | 74 | serial_initfunc(altera_serial_initialize); |
cfba4573 | 75 | serial_initfunc(atmel_serial_initialize); |
e503f90a | 76 | serial_initfunc(lpc32xx_serial_initialize); |
abaef69f | 77 | serial_initfunc(mcf_serial_initialize); |
28ea4c34 | 78 | serial_initfunc(ns9750_serial_initialize); |
69ea8070 | 79 | serial_initfunc(oc_serial_initialize); |
4bee0b36 | 80 | serial_initfunc(s3c4510b_serial_initialize); |
cc61c31d | 81 | serial_initfunc(s3c64xx_serial_initialize); |
cef46b77 | 82 | serial_initfunc(sandbox_serial_initialize); |
8b029db8 | 83 | serial_initfunc(clps7111_serial_initialize); |
7882e7c1 | 84 | serial_initfunc(imx_serial_initialize); |
b6cd0fef | 85 | serial_initfunc(ixp_serial_initialize); |
a7ffb2fc | 86 | serial_initfunc(ks8695_serial_initialize); |
1f673b4f | 87 | serial_initfunc(lh7a40x_serial_initialize); |
21310b57 | 88 | serial_initfunc(lpc2292_serial_initialize); |
7cd851a5 | 89 | serial_initfunc(max3100_serial_initialize); |
a943472c | 90 | serial_initfunc(mxc_serial_initialize); |
0dd9b849 | 91 | serial_initfunc(netarm_serial_initialize); |
f0eb1f61 | 92 | |
c52b4f79 | 93 | void serial_register(struct serial_device *dev) |
281e00a3 | 94 | { |
2e5167cc | 95 | #ifdef CONFIG_NEEDS_MANUAL_RELOC |
89143fb3 | 96 | dev->start += gd->reloc_off; |
281e00a3 WD |
97 | dev->setbrg += gd->reloc_off; |
98 | dev->getc += gd->reloc_off; | |
99 | dev->tstc += gd->reloc_off; | |
100 | dev->putc += gd->reloc_off; | |
101 | dev->puts += gd->reloc_off; | |
521af04d | 102 | #endif |
281e00a3 WD |
103 | |
104 | dev->next = serial_devices; | |
105 | serial_devices = dev; | |
281e00a3 WD |
106 | } |
107 | ||
a6e6f7f4 | 108 | void serial_initialize(void) |
281e00a3 | 109 | { |
f0eb1f61 | 110 | mpc8xx_serial_initialize(); |
abc0ed8d | 111 | ns16550_serial_initialize(); |
1fe5c110 | 112 | pxa_serial_initialize(); |
28af6385 | 113 | s3c24xx_serial_initialize(); |
b4980515 | 114 | s5p_serial_initialize(); |
918327c8 | 115 | mpc512x_serial_initialize(); |
5ae1de0d | 116 | bfin_serial_initialize(); |
41651cab | 117 | bfin_jtag_initialize(); |
87d69229 | 118 | uartlite_serial_initialize(); |
51d8102f | 119 | zynq_serial_initalize(); |
7b953c51 | 120 | au1x00_serial_initialize(); |
01911422 | 121 | asc_serial_initialize(); |
6cb3273d | 122 | jz_serial_initialize(); |
b57c6528 | 123 | mpc5xx_serial_initialize(); |
2063a54e | 124 | mpc8220_serial_initialize(); |
d68f4da2 MV |
125 | mpc8260_scc_serial_initialize(); |
126 | mpc8260_smc_serial_initialize(); | |
7a311546 | 127 | mpc85xx_serial_initialize(); |
fcc2074d | 128 | iop480_serial_initialize(); |
af7be3b6 | 129 | leon2_serial_initialize(); |
29b5ff7f | 130 | leon3_serial_initialize(); |
afe4c389 | 131 | marvell_serial_initialize(); |
5dee6f55 | 132 | amirix_serial_initialize(); |
c45a14ad | 133 | bmw_serial_initialize(); |
d25c39d3 | 134 | cogent_serial_initialize(); |
619c90bb | 135 | cpci750_serial_initialize(); |
829b3b2e | 136 | evb64260_serial_initialize(); |
d126f285 | 137 | ml2_serial_initialize(); |
26d8eaaa | 138 | sconsole_serial_initialize(); |
088a4f3c | 139 | p3mx_serial_initialize(); |
8c085757 | 140 | altera_jtag_serial_initialize(); |
b207d645 | 141 | altera_serial_initialize(); |
cfba4573 | 142 | atmel_serial_initialize(); |
e503f90a | 143 | lpc32xx_serial_initialize(); |
abaef69f | 144 | mcf_serial_initialize(); |
28ea4c34 | 145 | ns9750_serial_initialize(); |
69ea8070 | 146 | oc_serial_initialize(); |
4bee0b36 | 147 | s3c4510b_serial_initialize(); |
cc61c31d | 148 | s3c64xx_serial_initialize(); |
cef46b77 | 149 | sandbox_serial_initialize(); |
8b029db8 | 150 | clps7111_serial_initialize(); |
7882e7c1 | 151 | imx_serial_initialize(); |
b6cd0fef | 152 | ixp_serial_initialize(); |
a7ffb2fc | 153 | ks8695_serial_initialize(); |
1f673b4f | 154 | lh7a40x_serial_initialize(); |
21310b57 | 155 | lpc2292_serial_initialize(); |
7cd851a5 | 156 | max3100_serial_initialize(); |
a943472c | 157 | mxc_serial_initialize(); |
0dd9b849 | 158 | netarm_serial_initialize(); |
7b953c51 | 159 | |
a6e6f7f4 | 160 | serial_assign(default_serial_console()->name); |
281e00a3 WD |
161 | } |
162 | ||
a6e6f7f4 | 163 | void serial_stdio_init(void) |
281e00a3 | 164 | { |
52cb4d4f | 165 | struct stdio_dev dev; |
281e00a3 WD |
166 | struct serial_device *s = serial_devices; |
167 | ||
2ee66533 | 168 | while (s) { |
a6e6f7f4 | 169 | memset(&dev, 0, sizeof(dev)); |
281e00a3 | 170 | |
a6e6f7f4 | 171 | strcpy(dev.name, s->name); |
281e00a3 WD |
172 | dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT; |
173 | ||
89143fb3 MV |
174 | dev.start = s->start; |
175 | dev.stop = s->stop; | |
281e00a3 WD |
176 | dev.putc = s->putc; |
177 | dev.puts = s->puts; | |
178 | dev.getc = s->getc; | |
179 | dev.tstc = s->tstc; | |
180 | ||
a6e6f7f4 | 181 | stdio_register(&dev); |
281e00a3 WD |
182 | |
183 | s = s->next; | |
184 | } | |
185 | } | |
186 | ||
7813ca9b | 187 | int serial_assign(const char *name) |
281e00a3 WD |
188 | { |
189 | struct serial_device *s; | |
190 | ||
2ee66533 | 191 | for (s = serial_devices; s; s = s->next) { |
a6e6f7f4 | 192 | if (strcmp(s->name, name) == 0) { |
281e00a3 WD |
193 | serial_current = s; |
194 | return 0; | |
195 | } | |
196 | } | |
197 | ||
198 | return 1; | |
199 | } | |
200 | ||
a6e6f7f4 | 201 | void serial_reinit_all(void) |
281e00a3 WD |
202 | { |
203 | struct serial_device *s; | |
204 | ||
a6e6f7f4 | 205 | for (s = serial_devices; s; s = s->next) |
89143fb3 | 206 | s->start(); |
281e00a3 WD |
207 | } |
208 | ||
857c283e | 209 | static struct serial_device *get_current(void) |
281e00a3 | 210 | { |
857c283e | 211 | struct serial_device *dev; |
2ee66533 | 212 | |
857c283e SG |
213 | if (!(gd->flags & GD_FLG_RELOC) || !serial_current) { |
214 | dev = default_serial_console(); | |
215 | ||
216 | /* We must have a console device */ | |
217 | if (!dev) | |
218 | panic("Cannot find console"); | |
219 | } else | |
220 | dev = serial_current; | |
221 | return dev; | |
222 | } | |
281e00a3 | 223 | |
857c283e SG |
224 | int serial_init(void) |
225 | { | |
89143fb3 | 226 | return get_current()->start(); |
281e00a3 WD |
227 | } |
228 | ||
a6e6f7f4 | 229 | void serial_setbrg(void) |
281e00a3 | 230 | { |
857c283e | 231 | get_current()->setbrg(); |
281e00a3 WD |
232 | } |
233 | ||
a6e6f7f4 | 234 | int serial_getc(void) |
281e00a3 | 235 | { |
857c283e | 236 | return get_current()->getc(); |
281e00a3 WD |
237 | } |
238 | ||
a6e6f7f4 | 239 | int serial_tstc(void) |
281e00a3 | 240 | { |
857c283e | 241 | return get_current()->tstc(); |
281e00a3 WD |
242 | } |
243 | ||
a6e6f7f4 | 244 | void serial_putc(const char c) |
281e00a3 | 245 | { |
857c283e | 246 | get_current()->putc(c); |
281e00a3 WD |
247 | } |
248 | ||
a6e6f7f4 | 249 | void serial_puts(const char *s) |
281e00a3 | 250 | { |
857c283e | 251 | get_current()->puts(s); |
281e00a3 | 252 | } |
7b826c2f MF |
253 | |
254 | #if CONFIG_POST & CONFIG_SYS_POST_UART | |
255 | static const int bauds[] = CONFIG_SYS_BAUDRATE_TABLE; | |
256 | ||
257 | /* Mark weak until post/cpu/.../uart.c migrate over */ | |
258 | __weak | |
259 | int uart_post_test(int flags) | |
260 | { | |
261 | unsigned char c; | |
262 | int ret, saved_baud, b; | |
263 | struct serial_device *saved_dev, *s; | |
264 | bd_t *bd = gd->bd; | |
265 | ||
266 | /* Save current serial state */ | |
267 | ret = 0; | |
268 | saved_dev = serial_current; | |
269 | saved_baud = bd->bi_baudrate; | |
270 | ||
271 | for (s = serial_devices; s; s = s->next) { | |
272 | /* If this driver doesn't support loop back, skip it */ | |
273 | if (!s->loop) | |
274 | continue; | |
275 | ||
276 | /* Test the next device */ | |
277 | serial_current = s; | |
278 | ||
279 | ret = serial_init(); | |
280 | if (ret) | |
281 | goto done; | |
282 | ||
283 | /* Consume anything that happens to be queued */ | |
284 | while (serial_tstc()) | |
285 | serial_getc(); | |
286 | ||
287 | /* Enable loop back */ | |
288 | s->loop(1); | |
289 | ||
290 | /* Test every available baud rate */ | |
291 | for (b = 0; b < ARRAY_SIZE(bauds); ++b) { | |
292 | bd->bi_baudrate = bauds[b]; | |
293 | serial_setbrg(); | |
294 | ||
295 | /* | |
296 | * Stick to printable chars to avoid issues: | |
297 | * - terminal corruption | |
298 | * - serial program reacting to sequences and sending | |
299 | * back random extra data | |
300 | * - most serial drivers add in extra chars (like \r\n) | |
301 | */ | |
302 | for (c = 0x20; c < 0x7f; ++c) { | |
303 | /* Send it out */ | |
304 | serial_putc(c); | |
305 | ||
306 | /* Make sure it's the same one */ | |
307 | ret = (c != serial_getc()); | |
308 | if (ret) { | |
309 | s->loop(0); | |
310 | goto done; | |
311 | } | |
312 | ||
313 | /* Clean up the output in case it was sent */ | |
314 | serial_putc('\b'); | |
315 | ret = ('\b' != serial_getc()); | |
316 | if (ret) { | |
317 | s->loop(0); | |
318 | goto done; | |
319 | } | |
320 | } | |
321 | } | |
322 | ||
323 | /* Disable loop back */ | |
324 | s->loop(0); | |
325 | ||
89143fb3 MV |
326 | /* XXX: There is no serial_stop() !? */ |
327 | if (s->stop) | |
328 | s->stop(); | |
7b826c2f MF |
329 | } |
330 | ||
331 | done: | |
332 | /* Restore previous serial state */ | |
333 | serial_current = saved_dev; | |
334 | bd->bi_baudrate = saved_baud; | |
335 | serial_reinit_all(); | |
336 | serial_setbrg(); | |
337 | ||
338 | return ret; | |
339 | } | |
340 | #endif |