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