]> Git Repo - u-boot.git/blame - drivers/serial/serial.c
omap3_logic: Fix CONS_INDEX
[u-boot.git] / drivers / serial / serial.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
281e00a3
WD
2/*
3 * (C) Copyright 2004
4 * Wolfgang Denk, DENX Software Engineering, [email protected].
281e00a3
WD
5 */
6
7#include <common.h>
32057717 8#include <environment.h>
281e00a3 9#include <serial.h>
52cb4d4f 10#include <stdio_dev.h>
7b826c2f
MF
11#include <post.h>
12#include <linux/compiler.h>
6d93e258 13#include <errno.h>
281e00a3 14
d87080b7
WD
15DECLARE_GLOBAL_DATA_PTR;
16
a6e6f7f4
GF
17static struct serial_device *serial_devices;
18static struct serial_device *serial_current;
32057717
JH
19/*
20 * Table with supported baudrates (defined in config_xyz.h)
21 */
22static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
281e00a3 23
9cd2b9e4
MV
24/**
25 * serial_null() - Void registration routine of a serial driver
26 *
27 * This routine implements a void registration routine of a serial
28 * driver. The registration routine of a particular driver is aliased
29 * to this empty function in case the driver is not compiled into
30 * U-Boot.
31 */
2a333aeb
MV
32static void serial_null(void)
33{
34}
35
32057717
JH
36/**
37 * on_baudrate() - Update the actual baudrate when the env var changes
38 *
39 * This will check for a valid baudrate and only apply it if valid.
40 */
41static int on_baudrate(const char *name, const char *value, enum env_op op,
42 int flags)
43{
44 int i;
45 int baudrate;
46
47 switch (op) {
48 case env_op_create:
49 case env_op_overwrite:
50 /*
51 * Switch to new baudrate if new baudrate is supported
52 */
53 baudrate = simple_strtoul(value, NULL, 10);
54
55 /* Not actually changing */
56 if (gd->baudrate == baudrate)
57 return 0;
58
9935175f 59 for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) {
32057717
JH
60 if (baudrate == baudrate_table[i])
61 break;
62 }
9935175f 63 if (i == ARRAY_SIZE(baudrate_table)) {
32057717
JH
64 if ((flags & H_FORCE) == 0)
65 printf("## Baudrate %d bps not supported\n",
66 baudrate);
67 return 1;
68 }
69 if ((flags & H_INTERACTIVE) != 0) {
70 printf("## Switch baudrate to %d"
71 " bps and press ENTER ...\n", baudrate);
72 udelay(50000);
73 }
74
75 gd->baudrate = baudrate;
32057717
JH
76
77 serial_setbrg();
78
79 udelay(50000);
80
81 if ((flags & H_INTERACTIVE) != 0)
82 while (1) {
83 if (getc() == '\r')
84 break;
85 }
86
87 return 0;
88 case env_op_delete:
89 printf("## Baudrate may not be deleted\n");
90 return 1;
91 default:
92 return 0;
93 }
94}
95U_BOOT_ENV_CALLBACK(baudrate, on_baudrate);
96
9cd2b9e4
MV
97/**
98 * serial_initfunc() - Forward declare of driver registration routine
99 * @name: Name of the real driver registration routine.
100 *
101 * This macro expands onto forward declaration of a driver registration
102 * routine, which is then used below in serial_initialize() function.
103 * The declaration is made weak and aliases to serial_null() so in case
104 * the driver is not compiled in, the function is still declared and can
105 * be used, but aliases to serial_null() and thus is optimized away.
106 */
2a333aeb
MV
107#define serial_initfunc(name) \
108 void name(void) \
109 __attribute__((weak, alias("serial_null")));
110
94a255df 111serial_initfunc(atmel_serial_initialize);
94a255df 112serial_initfunc(mcf_serial_initialize);
94a255df
JH
113serial_initfunc(mpc85xx_serial_initialize);
114serial_initfunc(mpc8xx_serial_initialize);
a943472c 115serial_initfunc(mxc_serial_initialize);
94a255df 116serial_initfunc(ns16550_serial_initialize);
39f61477 117serial_initfunc(pl01x_serial_initialize);
94a255df 118serial_initfunc(pxa_serial_initialize);
8bdd7efa 119serial_initfunc(sh_serial_initialize);
f0eb1f61 120
9cd2b9e4
MV
121/**
122 * serial_register() - Register serial driver with serial driver core
123 * @dev: Pointer to the serial driver structure
124 *
125 * This function registers the serial driver supplied via @dev with
126 * serial driver core, thus making U-Boot aware of it and making it
127 * available for U-Boot to use. On platforms that still require manual
128 * relocation of constant variables, relocation of the supplied structure
129 * is performed.
130 */
c52b4f79 131void serial_register(struct serial_device *dev)
281e00a3 132{
2e5167cc 133#ifdef CONFIG_NEEDS_MANUAL_RELOC
f2760c4a
MV
134 if (dev->start)
135 dev->start += gd->reloc_off;
136 if (dev->stop)
137 dev->stop += gd->reloc_off;
138 if (dev->setbrg)
139 dev->setbrg += gd->reloc_off;
140 if (dev->getc)
141 dev->getc += gd->reloc_off;
142 if (dev->tstc)
143 dev->tstc += gd->reloc_off;
144 if (dev->putc)
145 dev->putc += gd->reloc_off;
146 if (dev->puts)
147 dev->puts += gd->reloc_off;
521af04d 148#endif
281e00a3
WD
149
150 dev->next = serial_devices;
151 serial_devices = dev;
281e00a3
WD
152}
153
9cd2b9e4
MV
154/**
155 * serial_initialize() - Register all compiled-in serial port drivers
156 *
157 * This function registers all serial port drivers that are compiled
158 * into the U-Boot binary with the serial core, thus making them
159 * available to U-Boot to use. Lastly, this function assigns a default
160 * serial port to the serial core. That serial port is then used as a
161 * default output.
162 */
a6e6f7f4 163void serial_initialize(void)
281e00a3 164{
94a255df 165 atmel_serial_initialize();
94a255df 166 mcf_serial_initialize();
94a255df
JH
167 mpc85xx_serial_initialize();
168 mpc8xx_serial_initialize();
a943472c 169 mxc_serial_initialize();
94a255df 170 ns16550_serial_initialize();
39f61477 171 pl01x_serial_initialize();
94a255df 172 pxa_serial_initialize();
8bdd7efa 173 sh_serial_initialize();
7b953c51 174
a6e6f7f4 175 serial_assign(default_serial_console()->name);
281e00a3
WD
176}
177
654f8d0f 178static int serial_stub_start(struct stdio_dev *sdev)
709ea543
SG
179{
180 struct serial_device *dev = sdev->priv;
181
182 return dev->start();
183}
184
654f8d0f 185static int serial_stub_stop(struct stdio_dev *sdev)
709ea543
SG
186{
187 struct serial_device *dev = sdev->priv;
188
189 return dev->stop();
190}
191
654f8d0f 192static void serial_stub_putc(struct stdio_dev *sdev, const char ch)
709ea543
SG
193{
194 struct serial_device *dev = sdev->priv;
195
196 dev->putc(ch);
197}
198
654f8d0f 199static void serial_stub_puts(struct stdio_dev *sdev, const char *str)
709ea543
SG
200{
201 struct serial_device *dev = sdev->priv;
202
203 dev->puts(str);
204}
205
49ddcf3e 206static int serial_stub_getc(struct stdio_dev *sdev)
709ea543
SG
207{
208 struct serial_device *dev = sdev->priv;
209
210 return dev->getc();
211}
212
49ddcf3e 213static int serial_stub_tstc(struct stdio_dev *sdev)
709ea543
SG
214{
215 struct serial_device *dev = sdev->priv;
216
217 return dev->tstc();
218}
219
9cd2b9e4
MV
220/**
221 * serial_stdio_init() - Register serial ports with STDIO core
222 *
223 * This function generates a proxy driver for each serial port driver.
224 * These proxy drivers then register with the STDIO core, making the
225 * serial drivers available as STDIO devices.
226 */
a6e6f7f4 227void serial_stdio_init(void)
281e00a3 228{
52cb4d4f 229 struct stdio_dev dev;
281e00a3
WD
230 struct serial_device *s = serial_devices;
231
2ee66533 232 while (s) {
a6e6f7f4 233 memset(&dev, 0, sizeof(dev));
281e00a3 234
a6e6f7f4 235 strcpy(dev.name, s->name);
281e00a3
WD
236 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
237
709ea543
SG
238 dev.start = serial_stub_start;
239 dev.stop = serial_stub_stop;
240 dev.putc = serial_stub_putc;
241 dev.puts = serial_stub_puts;
242 dev.getc = serial_stub_getc;
243 dev.tstc = serial_stub_tstc;
addf9513 244 dev.priv = s;
281e00a3 245
a6e6f7f4 246 stdio_register(&dev);
281e00a3
WD
247
248 s = s->next;
249 }
250}
251
9cd2b9e4
MV
252/**
253 * serial_assign() - Select the serial output device by name
254 * @name: Name of the serial driver to be used as default output
255 *
256 * This function configures the serial output multiplexing by
257 * selecting which serial device will be used as default. In case
258 * the STDIO "serial" device is selected as stdin/stdout/stderr,
259 * the serial device previously configured by this function will be
260 * used for the particular operation.
261 *
262 * Returns 0 on success, negative on error.
263 */
7813ca9b 264int serial_assign(const char *name)
281e00a3
WD
265{
266 struct serial_device *s;
267
2ee66533 268 for (s = serial_devices; s; s = s->next) {
6d93e258
MV
269 if (strcmp(s->name, name))
270 continue;
271 serial_current = s;
272 return 0;
281e00a3
WD
273 }
274
6d93e258 275 return -EINVAL;
281e00a3
WD
276}
277
9cd2b9e4
MV
278/**
279 * serial_reinit_all() - Reinitialize all compiled-in serial ports
280 *
281 * This function reinitializes all serial ports that are compiled
282 * into U-Boot by calling their serial_start() functions.
283 */
a6e6f7f4 284void serial_reinit_all(void)
281e00a3
WD
285{
286 struct serial_device *s;
287
a6e6f7f4 288 for (s = serial_devices; s; s = s->next)
89143fb3 289 s->start();
281e00a3
WD
290}
291
9cd2b9e4
MV
292/**
293 * get_current() - Return pointer to currently selected serial port
294 *
295 * This function returns a pointer to currently selected serial port.
296 * The currently selected serial port is altered by serial_assign()
297 * function.
298 *
299 * In case this function is called before relocation or before any serial
300 * port is configured, this function calls default_serial_console() to
301 * determine the serial port. Otherwise, the configured serial port is
302 * returned.
303 *
304 * Returns pointer to the currently selected serial port on success,
305 * NULL on error.
306 */
857c283e 307static struct serial_device *get_current(void)
281e00a3 308{
857c283e 309 struct serial_device *dev;
2ee66533 310
dee19416 311 if (!(gd->flags & GD_FLG_RELOC))
857c283e 312 dev = default_serial_console();
dee19416
MV
313 else if (!serial_current)
314 dev = default_serial_console();
315 else
316 dev = serial_current;
857c283e 317
dee19416
MV
318 /* We must have a console device */
319 if (!dev) {
f1f1e9cb 320#ifdef CONFIG_SPL_BUILD
dee19416
MV
321 puts("Cannot find console\n");
322 hang();
f1f1e9cb 323#else
dee19416 324 panic("Cannot find console\n");
f1f1e9cb 325#endif
dee19416
MV
326 }
327
857c283e
SG
328 return dev;
329}
281e00a3 330
9cd2b9e4
MV
331/**
332 * serial_init() - Initialize currently selected serial port
333 *
334 * This function initializes the currently selected serial port. This
335 * usually involves setting up the registers of that particular port,
336 * enabling clock and such. This function uses the get_current() call
337 * to determine which port is selected.
338 *
339 * Returns 0 on success, negative on error.
340 */
857c283e
SG
341int serial_init(void)
342{
093f79ab 343 gd->flags |= GD_FLG_SERIAL_READY;
89143fb3 344 return get_current()->start();
281e00a3
WD
345}
346
9cd2b9e4
MV
347/**
348 * serial_setbrg() - Configure baud-rate of currently selected serial port
349 *
350 * This function configures the baud-rate of the currently selected
351 * serial port. The baud-rate is retrieved from global data within
352 * the serial port driver. This function uses the get_current() call
353 * to determine which port is selected.
354 *
355 * Returns 0 on success, negative on error.
356 */
a6e6f7f4 357void serial_setbrg(void)
281e00a3 358{
857c283e 359 get_current()->setbrg();
281e00a3
WD
360}
361
9cd2b9e4
MV
362/**
363 * serial_getc() - Read character from currently selected serial port
364 *
365 * This function retrieves a character from currently selected serial
366 * port. In case there is no character waiting on the serial port,
367 * this function will block and wait for the character to appear. This
368 * function uses the get_current() call to determine which port is
369 * selected.
370 *
371 * Returns the character on success, negative on error.
372 */
a6e6f7f4 373int serial_getc(void)
281e00a3 374{
857c283e 375 return get_current()->getc();
281e00a3
WD
376}
377
9cd2b9e4
MV
378/**
379 * serial_tstc() - Test if data is available on currently selected serial port
380 *
381 * This function tests if one or more characters are available on
382 * currently selected serial port. This function never blocks. This
383 * function uses the get_current() call to determine which port is
384 * selected.
385 *
386 * Returns positive if character is available, zero otherwise.
387 */
a6e6f7f4 388int serial_tstc(void)
281e00a3 389{
857c283e 390 return get_current()->tstc();
281e00a3
WD
391}
392
9cd2b9e4
MV
393/**
394 * serial_putc() - Output character via currently selected serial port
395 * @c: Single character to be output from the serial port.
396 *
397 * This function outputs a character via currently selected serial
398 * port. This character is passed to the serial port driver responsible
399 * for controlling the hardware. The hardware may still be in process
400 * of transmitting another character, therefore this function may block
401 * for a short amount of time. This function uses the get_current()
402 * call to determine which port is selected.
403 */
a6e6f7f4 404void serial_putc(const char c)
281e00a3 405{
857c283e 406 get_current()->putc(c);
281e00a3
WD
407}
408
9cd2b9e4
MV
409/**
410 * serial_puts() - Output string via currently selected serial port
411 * @s: Zero-terminated string to be output from the serial port.
412 *
413 * This function outputs a zero-terminated string via currently
414 * selected serial port. This function behaves as an accelerator
415 * in case the hardware can queue multiple characters for transfer.
416 * The whole string that is to be output is available to the function
417 * implementing the hardware manipulation. Transmitting the whole
418 * string may take some time, thus this function may block for some
419 * amount of time. This function uses the get_current() call to
420 * determine which port is selected.
421 */
a6e6f7f4 422void serial_puts(const char *s)
281e00a3 423{
857c283e 424 get_current()->puts(s);
281e00a3 425}
7b826c2f 426
9cd2b9e4
MV
427/**
428 * default_serial_puts() - Output string by calling serial_putc() in loop
429 * @s: Zero-terminated string to be output from the serial port.
430 *
431 * This function outputs a zero-terminated string by calling serial_putc()
432 * in a loop. Most drivers do not support queueing more than one byte for
433 * transfer, thus this function precisely implements their serial_puts().
434 *
435 * To optimize the number of get_current() calls, this function only
436 * calls get_current() once and then directly accesses the putc() call
437 * of the &struct serial_device .
438 */
bfb7d7a3
MV
439void default_serial_puts(const char *s)
440{
441 struct serial_device *dev = get_current();
442 while (*s)
443 dev->putc(*s++);
444}
445
7b826c2f
MF
446#if CONFIG_POST & CONFIG_SYS_POST_UART
447static const int bauds[] = CONFIG_SYS_BAUDRATE_TABLE;
448
9cd2b9e4
MV
449/**
450 * uart_post_test() - Test the currently selected serial port using POST
451 * @flags: POST framework flags
452 *
453 * Do a loopback test of the currently selected serial port. This
454 * function is only useful in the context of the POST testing framwork.
1b25e586 455 * The serial port is first configured into loopback mode and then
9cd2b9e4
MV
456 * characters are sent through it.
457 *
458 * Returns 0 on success, value otherwise.
459 */
7b826c2f
MF
460/* Mark weak until post/cpu/.../uart.c migrate over */
461__weak
462int uart_post_test(int flags)
463{
464 unsigned char c;
465 int ret, saved_baud, b;
466 struct serial_device *saved_dev, *s;
7b826c2f
MF
467
468 /* Save current serial state */
469 ret = 0;
470 saved_dev = serial_current;
8e261575 471 saved_baud = gd->baudrate;
7b826c2f
MF
472
473 for (s = serial_devices; s; s = s->next) {
474 /* If this driver doesn't support loop back, skip it */
475 if (!s->loop)
476 continue;
477
478 /* Test the next device */
479 serial_current = s;
480
481 ret = serial_init();
482 if (ret)
483 goto done;
484
485 /* Consume anything that happens to be queued */
486 while (serial_tstc())
487 serial_getc();
488
489 /* Enable loop back */
490 s->loop(1);
491
492 /* Test every available baud rate */
493 for (b = 0; b < ARRAY_SIZE(bauds); ++b) {
8e261575 494 gd->baudrate = bauds[b];
7b826c2f
MF
495 serial_setbrg();
496
497 /*
498 * Stick to printable chars to avoid issues:
499 * - terminal corruption
500 * - serial program reacting to sequences and sending
501 * back random extra data
502 * - most serial drivers add in extra chars (like \r\n)
503 */
504 for (c = 0x20; c < 0x7f; ++c) {
505 /* Send it out */
506 serial_putc(c);
507
508 /* Make sure it's the same one */
509 ret = (c != serial_getc());
510 if (ret) {
511 s->loop(0);
512 goto done;
513 }
514
515 /* Clean up the output in case it was sent */
516 serial_putc('\b');
517 ret = ('\b' != serial_getc());
518 if (ret) {
519 s->loop(0);
520 goto done;
521 }
522 }
523 }
524
525 /* Disable loop back */
526 s->loop(0);
527
89143fb3
MV
528 /* XXX: There is no serial_stop() !? */
529 if (s->stop)
530 s->stop();
7b826c2f
MF
531 }
532
533 done:
534 /* Restore previous serial state */
535 serial_current = saved_dev;
8e261575 536 gd->baudrate = saved_baud;
7b826c2f
MF
537 serial_reinit_all();
538 serial_setbrg();
539
540 return ret;
541}
542#endif
This page took 0.395621 seconds and 4 git commands to generate.