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