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