1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <common.h>
25#include <environment.h>
26#include <serial.h>
27#include <stdio_dev.h>
28#include <post.h>
29#include <linux/compiler.h>
30#include <errno.h>
31
32DECLARE_GLOBAL_DATA_PTR;
33
34static struct serial_device *serial_devices;
35static struct serial_device *serial_current;
36
37
38
39static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
40#define N_BAUDRATES (sizeof(baudrate_table) / sizeof(baudrate_table[0]))
41
42
43
44
45
46
47
48
49
50static void serial_null(void)
51{
52}
53
54
55
56
57
58
59static int on_baudrate(const char *name, const char *value, enum env_op op,
60 int flags)
61{
62 int i;
63 int baudrate;
64
65 switch (op) {
66 case env_op_create:
67 case env_op_overwrite:
68
69
70
71 baudrate = simple_strtoul(value, NULL, 10);
72
73
74 if (gd->baudrate == baudrate)
75 return 0;
76
77 for (i = 0; i < N_BAUDRATES; ++i) {
78 if (baudrate == baudrate_table[i])
79 break;
80 }
81 if (i == N_BAUDRATES) {
82 if ((flags & H_FORCE) == 0)
83 printf("## Baudrate %d bps not supported\n",
84 baudrate);
85 return 1;
86 }
87 if ((flags & H_INTERACTIVE) != 0) {
88 printf("## Switch baudrate to %d"
89 " bps and press ENTER ...\n", baudrate);
90 udelay(50000);
91 }
92
93 gd->baudrate = baudrate;
94#if defined(CONFIG_PPC) || defined(CONFIG_MCF52x2)
95 gd->bd->bi_baudrate = baudrate;
96#endif
97
98 serial_setbrg();
99
100 udelay(50000);
101
102 if ((flags & H_INTERACTIVE) != 0)
103 while (1) {
104 if (getc() == '\r')
105 break;
106 }
107
108 return 0;
109 case env_op_delete:
110 printf("## Baudrate may not be deleted\n");
111 return 1;
112 default:
113 return 0;
114 }
115}
116U_BOOT_ENV_CALLBACK(baudrate, on_baudrate);
117
118
119
120
121
122
123
124
125
126
127
128#define serial_initfunc(name) \
129 void name(void) \
130 __attribute__((weak, alias("serial_null")));
131
132serial_initfunc(mpc8xx_serial_initialize);
133serial_initfunc(ns16550_serial_initialize);
134serial_initfunc(pxa_serial_initialize);
135serial_initfunc(s3c24xx_serial_initialize);
136serial_initfunc(s5p_serial_initialize);
137serial_initfunc(zynq_serial_initalize);
138serial_initfunc(bfin_serial_initialize);
139serial_initfunc(bfin_jtag_initialize);
140serial_initfunc(mpc512x_serial_initialize);
141serial_initfunc(uartlite_serial_initialize);
142serial_initfunc(au1x00_serial_initialize);
143serial_initfunc(asc_serial_initialize);
144serial_initfunc(jz_serial_initialize);
145serial_initfunc(mpc5xx_serial_initialize);
146serial_initfunc(mpc8220_serial_initialize);
147serial_initfunc(mpc8260_scc_serial_initialize);
148serial_initfunc(mpc8260_smc_serial_initialize);
149serial_initfunc(mpc85xx_serial_initialize);
150serial_initfunc(iop480_serial_initialize);
151serial_initfunc(leon2_serial_initialize);
152serial_initfunc(leon3_serial_initialize);
153serial_initfunc(marvell_serial_initialize);
154serial_initfunc(amirix_serial_initialize);
155serial_initfunc(bmw_serial_initialize);
156serial_initfunc(cogent_serial_initialize);
157serial_initfunc(cpci750_serial_initialize);
158serial_initfunc(evb64260_serial_initialize);
159serial_initfunc(ml2_serial_initialize);
160serial_initfunc(sconsole_serial_initialize);
161serial_initfunc(p3mx_serial_initialize);
162serial_initfunc(altera_jtag_serial_initialize);
163serial_initfunc(altera_serial_initialize);
164serial_initfunc(atmel_serial_initialize);
165serial_initfunc(lpc32xx_serial_initialize);
166serial_initfunc(mcf_serial_initialize);
167serial_initfunc(ns9750_serial_initialize);
168serial_initfunc(oc_serial_initialize);
169serial_initfunc(s3c64xx_serial_initialize);
170serial_initfunc(sandbox_serial_initialize);
171serial_initfunc(clps7111_serial_initialize);
172serial_initfunc(imx_serial_initialize);
173serial_initfunc(ixp_serial_initialize);
174serial_initfunc(ks8695_serial_initialize);
175serial_initfunc(lh7a40x_serial_initialize);
176serial_initfunc(max3100_serial_initialize);
177serial_initfunc(mxc_serial_initialize);
178serial_initfunc(pl01x_serial_initialize);
179serial_initfunc(s3c44b0_serial_initialize);
180serial_initfunc(sa1100_serial_initialize);
181serial_initfunc(sh_serial_initialize);
182
183
184
185
186
187
188
189
190
191
192
193void serial_register(struct serial_device *dev)
194{
195#ifdef CONFIG_NEEDS_MANUAL_RELOC
196 if (dev->start)
197 dev->start += gd->reloc_off;
198 if (dev->stop)
199 dev->stop += gd->reloc_off;
200 if (dev->setbrg)
201 dev->setbrg += gd->reloc_off;
202 if (dev->getc)
203 dev->getc += gd->reloc_off;
204 if (dev->tstc)
205 dev->tstc += gd->reloc_off;
206 if (dev->putc)
207 dev->putc += gd->reloc_off;
208 if (dev->puts)
209 dev->puts += gd->reloc_off;
210#endif
211
212 dev->next = serial_devices;
213 serial_devices = dev;
214}
215
216
217
218
219
220
221
222
223
224
225void serial_initialize(void)
226{
227 mpc8xx_serial_initialize();
228 ns16550_serial_initialize();
229 pxa_serial_initialize();
230 s3c24xx_serial_initialize();
231 s5p_serial_initialize();
232 mpc512x_serial_initialize();
233 bfin_serial_initialize();
234 bfin_jtag_initialize();
235 uartlite_serial_initialize();
236 zynq_serial_initalize();
237 au1x00_serial_initialize();
238 asc_serial_initialize();
239 jz_serial_initialize();
240 mpc5xx_serial_initialize();
241 mpc8220_serial_initialize();
242 mpc8260_scc_serial_initialize();
243 mpc8260_smc_serial_initialize();
244 mpc85xx_serial_initialize();
245 iop480_serial_initialize();
246 leon2_serial_initialize();
247 leon3_serial_initialize();
248 marvell_serial_initialize();
249 amirix_serial_initialize();
250 bmw_serial_initialize();
251 cogent_serial_initialize();
252 cpci750_serial_initialize();
253 evb64260_serial_initialize();
254 ml2_serial_initialize();
255 sconsole_serial_initialize();
256 p3mx_serial_initialize();
257 altera_jtag_serial_initialize();
258 altera_serial_initialize();
259 atmel_serial_initialize();
260 lpc32xx_serial_initialize();
261 mcf_serial_initialize();
262 ns9750_serial_initialize();
263 oc_serial_initialize();
264 s3c64xx_serial_initialize();
265 sandbox_serial_initialize();
266 clps7111_serial_initialize();
267 imx_serial_initialize();
268 ixp_serial_initialize();
269 ks8695_serial_initialize();
270 lh7a40x_serial_initialize();
271 max3100_serial_initialize();
272 mxc_serial_initialize();
273 pl01x_serial_initialize();
274 s3c44b0_serial_initialize();
275 sa1100_serial_initialize();
276 sh_serial_initialize();
277
278 serial_assign(default_serial_console()->name);
279}
280
281
282
283
284
285
286
287
288void serial_stdio_init(void)
289{
290 struct stdio_dev dev;
291 struct serial_device *s = serial_devices;
292
293 while (s) {
294 memset(&dev, 0, sizeof(dev));
295
296 strcpy(dev.name, s->name);
297 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
298
299 dev.start = s->start;
300 dev.stop = s->stop;
301 dev.putc = s->putc;
302 dev.puts = s->puts;
303 dev.getc = s->getc;
304 dev.tstc = s->tstc;
305
306 stdio_register(&dev);
307
308 s = s->next;
309 }
310}
311
312
313
314
315
316
317
318
319
320
321
322
323
324int serial_assign(const char *name)
325{
326 struct serial_device *s;
327
328 for (s = serial_devices; s; s = s->next) {
329 if (strcmp(s->name, name))
330 continue;
331 serial_current = s;
332 return 0;
333 }
334
335 return -EINVAL;
336}
337
338
339
340
341
342
343
344void serial_reinit_all(void)
345{
346 struct serial_device *s;
347
348 for (s = serial_devices; s; s = s->next)
349 s->start();
350}
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367static struct serial_device *get_current(void)
368{
369 struct serial_device *dev;
370
371 if (!(gd->flags & GD_FLG_RELOC))
372 dev = default_serial_console();
373 else if (!serial_current)
374 dev = default_serial_console();
375 else
376 dev = serial_current;
377
378
379 if (!dev) {
380#ifdef CONFIG_SPL_BUILD
381 puts("Cannot find console\n");
382 hang();
383#else
384 panic("Cannot find console\n");
385#endif
386 }
387
388 return dev;
389}
390
391
392
393
394
395
396
397
398
399
400
401int serial_init(void)
402{
403 return get_current()->start();
404}
405
406
407
408
409
410
411
412
413
414
415
416void serial_setbrg(void)
417{
418 get_current()->setbrg();
419}
420
421
422
423
424
425
426
427
428
429
430
431
432int serial_getc(void)
433{
434 return get_current()->getc();
435}
436
437
438
439
440
441
442
443
444
445
446
447int serial_tstc(void)
448{
449 return get_current()->tstc();
450}
451
452
453
454
455
456
457
458
459
460
461
462
463void serial_putc(const char c)
464{
465 get_current()->putc(c);
466}
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481void serial_puts(const char *s)
482{
483 get_current()->puts(s);
484}
485
486
487
488
489
490
491
492
493
494
495
496
497
498void default_serial_puts(const char *s)
499{
500 struct serial_device *dev = get_current();
501 while (*s)
502 dev->putc(*s++);
503}
504
505#if CONFIG_POST & CONFIG_SYS_POST_UART
506static const int bauds[] = CONFIG_SYS_BAUDRATE_TABLE;
507
508
509
510
511
512
513
514
515
516
517
518
519
520__weak
521int uart_post_test(int flags)
522{
523 unsigned char c;
524 int ret, saved_baud, b;
525 struct serial_device *saved_dev, *s;
526 bd_t *bd = gd->bd;
527
528
529 ret = 0;
530 saved_dev = serial_current;
531 saved_baud = bd->bi_baudrate;
532
533 for (s = serial_devices; s; s = s->next) {
534
535 if (!s->loop)
536 continue;
537
538
539 serial_current = s;
540
541 ret = serial_init();
542 if (ret)
543 goto done;
544
545
546 while (serial_tstc())
547 serial_getc();
548
549
550 s->loop(1);
551
552
553 for (b = 0; b < ARRAY_SIZE(bauds); ++b) {
554 bd->bi_baudrate = bauds[b];
555 serial_setbrg();
556
557
558
559
560
561
562
563
564 for (c = 0x20; c < 0x7f; ++c) {
565
566 serial_putc(c);
567
568
569 ret = (c != serial_getc());
570 if (ret) {
571 s->loop(0);
572 goto done;
573 }
574
575
576 serial_putc('\b');
577 ret = ('\b' != serial_getc());
578 if (ret) {
579 s->loop(0);
580 goto done;
581 }
582 }
583 }
584
585
586 s->loop(0);
587
588
589 if (s->stop)
590 s->stop();
591 }
592
593 done:
594
595 serial_current = saved_dev;
596 bd->bi_baudrate = saved_baud;
597 serial_reinit_all();
598 serial_setbrg();
599
600 return ret;
601}
602#endif
603