1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2014 The Chromium OS Authors.
4  */
5 
6 #define LOG_CATEGORY UCLASS_SERIAL
7 
8 #include <config.h>
9 #include <dm.h>
10 #include <env_internal.h>
11 #include <errno.h>
12 #include <malloc.h>
13 #include <os.h>
14 #include <serial.h>
15 #include <stdio_dev.h>
16 #include <watchdog.h>
17 #include <asm/global_data.h>
18 #include <dm/lists.h>
19 #include <dm/device-internal.h>
20 #include <dm/of_access.h>
21 #include <linux/build_bug.h>
22 #include <linux/delay.h>
23 
24 DECLARE_GLOBAL_DATA_PTR;
25 
26 /*
27  * Table with supported baudrates (defined in config_xyz.h)
28  */
29 static const unsigned long baudrate_table[] = CFG_SYS_BAUDRATE_TABLE;
30 
31 #if CONFIG_IS_ENABLED(SERIAL_PRESENT)
serial_check_stdout(const void * blob,struct udevice ** devp)32 static int serial_check_stdout(const void *blob, struct udevice **devp)
33 {
34 	int node = -1;
35 	const char *str, *p;
36 	int namelen;
37 
38 	/* Check for a chosen console */
39 	str = fdtdec_get_chosen_prop(blob, "stdout-path");
40 	if (str) {
41 		p = strchr(str, ':');
42 		namelen = p ? p - str : strlen(str);
43 		/*
44 		 * This also deals with things like
45 		 *
46 		 *	stdout-path = "serial0:115200n8";
47 		 *
48 		 * since fdt_path_offset_namelen() treats a str not
49 		 * beginning with '/' as an alias and thus applies
50 		 * fdt_get_alias_namelen() to it.
51 		 */
52 		node = fdt_path_offset_namelen(blob, str, namelen);
53 	}
54 
55 	if (node < 0)
56 		node = fdt_path_offset(blob, "console");
57 	if (!uclass_get_device_by_of_offset(UCLASS_SERIAL, node, devp))
58 		return 0;
59 
60 	/*
61 	 * If the console is not marked to be bound before relocation, bind it
62 	 * anyway.
63 	 */
64 	if (node > 0 && !lists_bind_fdt(gd->dm_root, offset_to_ofnode(node),
65 					devp, NULL, false)) {
66 		if (device_get_uclass_id(*devp) == UCLASS_SERIAL &&
67 		    !device_probe(*devp))
68 			return 0;
69 	}
70 
71 	return -ENODEV;
72 }
73 
serial_find_console_or_panic(void)74 static void serial_find_console_or_panic(void)
75 {
76 	const void *blob = gd->fdt_blob;
77 	struct udevice *dev;
78 #ifdef CONFIG_SERIAL_SEARCH_ALL
79 	int ret;
80 #endif
81 
82 	if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
83 		uclass_first_device(UCLASS_SERIAL, &dev);
84 		if (dev) {
85 			gd->cur_serial_dev = dev;
86 			return;
87 		}
88 	} else if (CONFIG_IS_ENABLED(OF_CONTROL) && blob) {
89 		/* Live tree has support for stdout */
90 		if (of_live_active()) {
91 			struct device_node *np = of_get_stdout();
92 
93 			if (np && !uclass_get_device_by_ofnode(UCLASS_SERIAL,
94 					np_to_ofnode(np), &dev)) {
95 				gd->cur_serial_dev = dev;
96 				return;
97 			}
98 		} else {
99 			if (!serial_check_stdout(blob, &dev)) {
100 				gd->cur_serial_dev = dev;
101 				return;
102 			}
103 		}
104 	}
105 	if (!IS_ENABLED(CONFIG_XPL_BUILD) || !CONFIG_IS_ENABLED(OF_CONTROL) ||
106 	    !blob) {
107 		/*
108 		 * Try to use CONFIG_CONS_INDEX if available (it is numbered
109 		 * from 1!).
110 		 *
111 		 * Failing that, get the device with sequence number 0, or in
112 		 * extremis just the first working serial device we can find.
113 		 * But we insist on having a console (even if it is silent).
114 		 */
115 #ifdef CONFIG_CONS_INDEX
116 #define INDEX (CONFIG_CONS_INDEX - 1)
117 #else
118 #define INDEX 0
119 #endif
120 
121 #ifdef CONFIG_SERIAL_SEARCH_ALL
122 		if (!uclass_get_device_by_seq(UCLASS_SERIAL, INDEX, &dev) ||
123 		    !uclass_get_device(UCLASS_SERIAL, INDEX, &dev)) {
124 			if (dev_get_flags(dev) & DM_FLAG_ACTIVATED) {
125 				gd->cur_serial_dev = dev;
126 				return;
127 			}
128 		}
129 
130 		/* Search for any working device */
131 		for (ret = uclass_first_device_check(UCLASS_SERIAL, &dev);
132 		     dev;
133 		     ret = uclass_next_device_check(&dev)) {
134 			if (!ret) {
135 				/* Device did succeed probing */
136 				gd->cur_serial_dev = dev;
137 				return;
138 			}
139 		}
140 #else
141 		if (!uclass_get_device_by_seq(UCLASS_SERIAL, INDEX, &dev) ||
142 		    !uclass_get_device(UCLASS_SERIAL, INDEX, &dev) ||
143 		    !uclass_first_device_err(UCLASS_SERIAL, &dev)) {
144 			gd->cur_serial_dev = dev;
145 			return;
146 		}
147 #endif
148 
149 #undef INDEX
150 	}
151 
152 #ifdef CONFIG_REQUIRE_SERIAL_CONSOLE
153 	panic_str("No serial driver found");
154 #endif
155 	gd->cur_serial_dev = NULL;
156 }
157 #endif /* CONFIG_SERIAL_PRESENT */
158 
159 /**
160  * check_valid_baudrate() - Check whether baudrate is valid or not
161  *
162  * @baud: baud rate to check
163  * Return: 0 if OK, -ve on error
164  */
check_valid_baudrate(int baud)165 static int check_valid_baudrate(int baud)
166 {
167 	int i;
168 
169 	for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) {
170 		if (baud == baudrate_table[i])
171 			return 0;
172 	}
173 
174 	return -EINVAL;
175 }
176 
fetch_baud_from_dtb(void)177 int fetch_baud_from_dtb(void)
178 {
179 	int baud_value, ret;
180 
181 	baud_value = ofnode_read_baud();
182 	ret = check_valid_baudrate(baud_value);
183 	if (ret)
184 		return ret;
185 
186 	return baud_value;
187 }
188 
189 /* Called prior to relocation */
serial_init(void)190 int serial_init(void)
191 {
192 #if CONFIG_IS_ENABLED(SERIAL_PRESENT)
193 	serial_find_console_or_panic();
194 	gd->flags |= GD_FLG_SERIAL_READY;
195 
196 	if (IS_ENABLED(CONFIG_OF_SERIAL_BAUD)) {
197 		int ret = 0;
198 		char *ptr = (char*)&default_environment[0];
199 
200 		/*
201 		 * Fetch the baudrate from the dtb and update the value in the
202 		 * default environment.
203 		 */
204 		ret = fetch_baud_from_dtb();
205 		if (ret != -EINVAL && ret != -EFAULT) {
206 			gd->baudrate = ret;
207 
208 			while (*ptr != '\0' && *(ptr + 1) != '\0')
209 				ptr++;
210 			ptr += 2;
211 			sprintf(ptr, "baudrate=%d", gd->baudrate);
212 		}
213 	}
214 	serial_setbrg();
215 #endif
216 
217 	return 0;
218 }
219 
220 /* Called after relocation */
serial_initialize(void)221 int serial_initialize(void)
222 {
223 	/* Scanning uclass to probe devices */
224 	if (IS_ENABLED(CONFIG_SERIAL_PROBE_ALL)) {
225 		int ret;
226 
227 		ret  = uclass_probe_all(UCLASS_SERIAL);
228 		if (ret)
229 			return ret;
230 	}
231 
232 	return serial_init();
233 }
234 
_serial_flush(struct udevice * dev)235 static void _serial_flush(struct udevice *dev)
236 {
237 	struct dm_serial_ops *ops = serial_get_ops(dev);
238 
239 	if (!ops->pending)
240 		return;
241 	while (ops->pending(dev, false) > 0)
242 		;
243 }
244 
_serial_putc(struct udevice * dev,char ch)245 static void _serial_putc(struct udevice *dev, char ch)
246 {
247 	struct dm_serial_ops *ops = serial_get_ops(dev);
248 	int err;
249 
250 	if (ch == '\n')
251 		_serial_putc(dev, '\r');
252 
253 	do {
254 		err = ops->putc(dev, ch);
255 	} while (err == -EAGAIN);
256 
257 	if (IS_ENABLED(CONFIG_CONSOLE_FLUSH_ON_NEWLINE) && ch == '\n')
258 		_serial_flush(dev);
259 }
260 
__serial_puts(struct udevice * dev,const char * str,size_t len)261 static int __serial_puts(struct udevice *dev, const char *str, size_t len)
262 {
263 	struct dm_serial_ops *ops = serial_get_ops(dev);
264 
265 	do {
266 		ssize_t written = ops->puts(dev, str, len);
267 
268 		if (written < 0)
269 			return written;
270 		str += written;
271 		len -= written;
272 	} while (len);
273 
274 	return 0;
275 }
276 
_serial_puts(struct udevice * dev,const char * str)277 static void _serial_puts(struct udevice *dev, const char *str)
278 {
279 	struct dm_serial_ops *ops = serial_get_ops(dev);
280 
281 	if (!CONFIG_IS_ENABLED(SERIAL_PUTS) || !ops->puts) {
282 		while (*str)
283 			_serial_putc(dev, *str++);
284 		return;
285 	}
286 
287 	do {
288 		const char *newline = strchrnul(str, '\n');
289 		size_t len = newline - str;
290 
291 		if (__serial_puts(dev, str, len))
292 			return;
293 
294 		if (*newline && __serial_puts(dev, "\r\n", 2))
295 			return;
296 
297 		if (IS_ENABLED(CONFIG_CONSOLE_FLUSH_ON_NEWLINE) && *newline)
298 			_serial_flush(dev);
299 
300 		str += len + !!*newline;
301 	} while (*str);
302 }
303 
__serial_getc(struct udevice * dev)304 static int __serial_getc(struct udevice *dev)
305 {
306 	struct dm_serial_ops *ops = serial_get_ops(dev);
307 	int err;
308 
309 	do {
310 		err = ops->getc(dev);
311 		if (err == -EAGAIN)
312 			schedule();
313 	} while (err == -EAGAIN);
314 
315 	return err >= 0 ? err : 0;
316 }
317 
__serial_tstc(struct udevice * dev)318 static int __serial_tstc(struct udevice *dev)
319 {
320 	struct dm_serial_ops *ops = serial_get_ops(dev);
321 
322 	if (ops->pending)
323 		return ops->pending(dev, true);
324 
325 	return 1;
326 }
327 
328 #if CONFIG_IS_ENABLED(SERIAL_RX_BUFFER)
_serial_tstc(struct udevice * dev)329 static int _serial_tstc(struct udevice *dev)
330 {
331 	struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
332 	uint wr, avail;
333 
334 	BUILD_BUG_ON_NOT_POWER_OF_2(CONFIG_SERIAL_RX_BUFFER_SIZE);
335 
336 	/* Read all available chars into the RX buffer while there's room */
337 	avail = CONFIG_SERIAL_RX_BUFFER_SIZE - (upriv->wr_ptr - upriv->rd_ptr);
338 	while (avail-- && __serial_tstc(dev)) {
339 		wr = upriv->wr_ptr++ % CONFIG_SERIAL_RX_BUFFER_SIZE;
340 		upriv->buf[wr] = __serial_getc(dev);
341 	}
342 
343 	return upriv->rd_ptr != upriv->wr_ptr ? 1 : 0;
344 }
345 
_serial_getc(struct udevice * dev)346 static int _serial_getc(struct udevice *dev)
347 {
348 	struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
349 	char val;
350 	uint rd;
351 
352 	if (upriv->rd_ptr == upriv->wr_ptr)
353 		return __serial_getc(dev);
354 
355 	rd = upriv->rd_ptr++ % CONFIG_SERIAL_RX_BUFFER_SIZE;
356 	val = upriv->buf[rd];
357 
358 	return val;
359 }
360 
361 #else /* CONFIG_IS_ENABLED(SERIAL_RX_BUFFER) */
362 
_serial_getc(struct udevice * dev)363 static int _serial_getc(struct udevice *dev)
364 {
365 	return __serial_getc(dev);
366 }
367 
_serial_tstc(struct udevice * dev)368 static int _serial_tstc(struct udevice *dev)
369 {
370 	return __serial_tstc(dev);
371 }
372 #endif /* CONFIG_IS_ENABLED(SERIAL_RX_BUFFER) */
373 
serial_putc(char ch)374 void serial_putc(char ch)
375 {
376 	if (gd->cur_serial_dev)
377 		_serial_putc(gd->cur_serial_dev, ch);
378 }
379 
serial_puts(const char * str)380 void serial_puts(const char *str)
381 {
382 	if (gd->cur_serial_dev)
383 		_serial_puts(gd->cur_serial_dev, str);
384 }
385 
386 #ifdef CONFIG_CONSOLE_FLUSH_SUPPORT
serial_flush(void)387 void serial_flush(void)
388 {
389 	if (!gd->cur_serial_dev)
390 		return;
391 
392 	_serial_flush(gd->cur_serial_dev);
393 }
394 #endif
395 
serial_getc(void)396 int serial_getc(void)
397 {
398 	if (!gd->cur_serial_dev)
399 		return 0;
400 
401 	return _serial_getc(gd->cur_serial_dev);
402 }
403 
serial_tstc(void)404 int serial_tstc(void)
405 {
406 	if (!gd->cur_serial_dev)
407 		return 0;
408 
409 	return _serial_tstc(gd->cur_serial_dev);
410 }
411 
serial_setbrg(void)412 void serial_setbrg(void)
413 {
414 	struct dm_serial_ops *ops;
415 
416 	if (!gd->cur_serial_dev)
417 		return;
418 
419 	ops = serial_get_ops(gd->cur_serial_dev);
420 	if (ops->setbrg)
421 		ops->setbrg(gd->cur_serial_dev, gd->baudrate);
422 }
423 
serial_getconfig(struct udevice * dev,uint * config)424 int serial_getconfig(struct udevice *dev, uint *config)
425 {
426 	struct dm_serial_ops *ops;
427 
428 	ops = serial_get_ops(dev);
429 	if (ops->getconfig)
430 		return ops->getconfig(dev, config);
431 
432 	return 0;
433 }
434 
serial_setconfig(struct udevice * dev,uint config)435 int serial_setconfig(struct udevice *dev, uint config)
436 {
437 	struct dm_serial_ops *ops;
438 
439 	ops = serial_get_ops(dev);
440 	if (ops->setconfig)
441 		return ops->setconfig(dev, config);
442 
443 	return 0;
444 }
445 
serial_getinfo(struct udevice * dev,struct serial_device_info * info)446 int serial_getinfo(struct udevice *dev, struct serial_device_info *info)
447 {
448 	struct dm_serial_ops *ops;
449 
450 	if (!info)
451 		return -EINVAL;
452 
453 	info->baudrate = gd->baudrate;
454 
455 	ops = serial_get_ops(dev);
456 	if (ops->getinfo)
457 		return ops->getinfo(dev, info);
458 
459 	return -EINVAL;
460 }
461 
serial_stdio_init(void)462 void serial_stdio_init(void)
463 {
464 }
465 
466 #if CONFIG_IS_ENABLED(DM_STDIO)
467 
468 #if CONFIG_IS_ENABLED(SERIAL_PRESENT)
serial_stub_putc(struct stdio_dev * sdev,const char ch)469 static void serial_stub_putc(struct stdio_dev *sdev, const char ch)
470 {
471 	_serial_putc(sdev->priv, ch);
472 }
473 
serial_stub_puts(struct stdio_dev * sdev,const char * str)474 static void serial_stub_puts(struct stdio_dev *sdev, const char *str)
475 {
476 	_serial_puts(sdev->priv, str);
477 }
478 
479 #ifdef CONFIG_CONSOLE_FLUSH_SUPPORT
serial_stub_flush(struct stdio_dev * sdev)480 static void serial_stub_flush(struct stdio_dev *sdev)
481 {
482 	_serial_flush(sdev->priv);
483 }
484 #endif
485 
serial_stub_getc(struct stdio_dev * sdev)486 static int serial_stub_getc(struct stdio_dev *sdev)
487 {
488 	return _serial_getc(sdev->priv);
489 }
490 
serial_stub_tstc(struct stdio_dev * sdev)491 static int serial_stub_tstc(struct stdio_dev *sdev)
492 {
493 	return _serial_tstc(sdev->priv);
494 }
495 #endif
496 #endif
497 
498 /**
499  * on_baudrate() - Update the actual baudrate when the env var changes
500  *
501  * This will check for a valid baudrate and only apply it if valid.
502  */
on_baudrate(const char * name,const char * value,enum env_op op,int flags)503 static int on_baudrate(const char *name, const char *value, enum env_op op,
504 	int flags)
505 {
506 	int i;
507 	int baudrate;
508 
509 	switch (op) {
510 	case env_op_create:
511 	case env_op_overwrite:
512 		/*
513 		 * Switch to new baudrate if new baudrate is supported
514 		 */
515 		baudrate = dectoul(value, NULL);
516 
517 		/* Not actually changing */
518 		if (gd->baudrate == baudrate)
519 			return 0;
520 
521 		for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) {
522 			if (baudrate == baudrate_table[i])
523 				break;
524 		}
525 		if (i == ARRAY_SIZE(baudrate_table)) {
526 			if ((flags & H_FORCE) == 0)
527 				printf("## Baudrate %d bps not supported\n",
528 				       baudrate);
529 			return 1;
530 		}
531 		if ((flags & H_INTERACTIVE) != 0) {
532 			printf("## Switch baudrate to %d bps and press ENTER ...\n",
533 			       baudrate);
534 			udelay(50000);
535 			flush();
536 		}
537 
538 		gd->baudrate = baudrate;
539 
540 		serial_setbrg();
541 
542 		udelay(50000);
543 
544 		if ((flags & H_INTERACTIVE) != 0)
545 			while (1) {
546 				if (getchar() == '\r')
547 					break;
548 			}
549 
550 		return 0;
551 	case env_op_delete:
552 		printf("## Baudrate may not be deleted\n");
553 		return 1;
554 	default:
555 		return 0;
556 	}
557 }
558 U_BOOT_ENV_CALLBACK(baudrate, on_baudrate);
559 
560 #if CONFIG_IS_ENABLED(SERIAL_PRESENT)
serial_post_probe(struct udevice * dev)561 static int serial_post_probe(struct udevice *dev)
562 {
563 	struct dm_serial_ops *ops = serial_get_ops(dev);
564 #if CONFIG_IS_ENABLED(DM_STDIO)
565 	struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
566 	struct stdio_dev sdev;
567 #endif
568 	int ret;
569 
570 	/* Set the baud rate */
571 	if (ops->setbrg) {
572 		ret = ops->setbrg(dev, gd->baudrate);
573 		if (ret)
574 			return ret;
575 	}
576 
577 #if CONFIG_IS_ENABLED(DM_STDIO)
578 	if (!(gd->flags & GD_FLG_RELOC))
579 		return 0;
580 	memset(&sdev, '\0', sizeof(sdev));
581 
582 	strlcpy(sdev.name, dev->name, sizeof(sdev.name));
583 	sdev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_DM;
584 	sdev.priv = dev;
585 	sdev.putc = serial_stub_putc;
586 	sdev.puts = serial_stub_puts;
587 	STDIO_DEV_ASSIGN_FLUSH(&sdev, serial_stub_flush);
588 	sdev.getc = serial_stub_getc;
589 	sdev.tstc = serial_stub_tstc;
590 
591 	stdio_register_dev(&sdev, &upriv->sdev);
592 #endif
593 	return 0;
594 }
595 
serial_pre_remove(struct udevice * dev)596 static int serial_pre_remove(struct udevice *dev)
597 {
598 #if CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER)
599 	struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
600 
601 	if (stdio_deregister_dev(upriv->sdev, true))
602 		return -EPERM;
603 #endif
604 
605 	return 0;
606 }
607 
608 UCLASS_DRIVER(serial) = {
609 	.id		= UCLASS_SERIAL,
610 	.name		= "serial",
611 	.flags		= DM_UC_FLAG_SEQ_ALIAS,
612 	.post_probe	= serial_post_probe,
613 	.pre_remove	= serial_pre_remove,
614 	.per_device_auto	= sizeof(struct serial_dev_priv),
615 };
616 #endif
617