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