xref: /rk3399_rockchip-uboot/drivers/serial/serial-uclass.c (revision 87e4c6020eff05133e40ab8b7b0e37e6a2be37e4)
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 		} else {
112 			if (!serial_check_stdout(blob, &dev)) {
113 				gd->cur_serial_dev = dev;
114 				return;
115 			}
116 		}
117 	}
118 	if (!SPL_BUILD || !CONFIG_IS_ENABLED(OF_CONTROL) || !blob) {
119 		/*
120 		 * Try to use CONFIG_CONS_INDEX if available (it is numbered
121 		 * from 1!).
122 		 *
123 		 * Failing that, get the device with sequence number 0, or in
124 		 * extremis just the first serial device we can find. But we
125 		 * insist on having a console (even if it is silent).
126 		 */
127 #ifdef CONFIG_CONS_INDEX
128 #define INDEX (CONFIG_CONS_INDEX - 1)
129 #else
130 #define INDEX 0
131 #endif
132 		if (!uclass_get_device_by_seq(UCLASS_SERIAL, INDEX, &dev) ||
133 		    !uclass_get_device(UCLASS_SERIAL, INDEX, &dev) ||
134 		    (!uclass_first_device(UCLASS_SERIAL, &dev) && dev)) {
135 			gd->cur_serial_dev = dev;
136 			return;
137 		}
138 #undef INDEX
139 	}
140 
141 #ifdef CONFIG_REQUIRE_SERIAL_CONSOLE
142 	panic_str("No serial driver found");
143 #endif
144 }
145 
146 /* Called prior to relocation */
147 int serial_init(void)
148 {
149 	serial_find_console_or_panic();
150 	gd->flags |= GD_FLG_SERIAL_READY;
151 
152 	return 0;
153 }
154 
155 /* Called after relocation */
156 void serial_initialize(void)
157 {
158 	serial_init();
159 }
160 
161 static void _serial_putc(struct udevice *dev, char ch)
162 {
163 	struct dm_serial_ops *ops = serial_get_ops(dev);
164 	int err;
165 
166 	if (ch == '\n')
167 		_serial_putc(dev, '\r');
168 
169 	do {
170 		err = ops->putc(dev, ch);
171 	} while (err == -EAGAIN);
172 }
173 
174 static void _serial_puts(struct udevice *dev, const char *str)
175 {
176 	while (*str)
177 		_serial_putc(dev, *str++);
178 }
179 
180 static int __serial_getc(struct udevice *dev)
181 {
182 	struct dm_serial_ops *ops = serial_get_ops(dev);
183 	int err;
184 
185 	do {
186 		err = ops->getc(dev);
187 		if (err == -EAGAIN)
188 			WATCHDOG_RESET();
189 	} while (err == -EAGAIN);
190 
191 	return err >= 0 ? err : 0;
192 }
193 
194 static int __serial_tstc(struct udevice *dev)
195 {
196 	struct dm_serial_ops *ops = serial_get_ops(dev);
197 
198 	if (ops->pending)
199 		return ops->pending(dev, true);
200 
201 	return 1;
202 }
203 
204 #if CONFIG_IS_ENABLED(SERIAL_RX_BUFFER)
205 static int _serial_tstc(struct udevice *dev)
206 {
207 	struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
208 
209 	/* Read all available chars into the RX buffer */
210 	while (__serial_tstc(dev)) {
211 		upriv->buf[upriv->wr_ptr++] = __serial_getc(dev);
212 		upriv->wr_ptr %= CONFIG_SERIAL_RX_BUFFER_SIZE;
213 	}
214 
215 	return upriv->rd_ptr != upriv->wr_ptr ? 1 : 0;
216 }
217 
218 static int _serial_getc(struct udevice *dev)
219 {
220 	struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
221 	char val;
222 
223 	val = upriv->buf[upriv->rd_ptr++];
224 	upriv->rd_ptr %= CONFIG_SERIAL_RX_BUFFER_SIZE;
225 
226 	return val;
227 }
228 
229 #else /* CONFIG_IS_ENABLED(SERIAL_RX_BUFFER) */
230 
231 static int _serial_getc(struct udevice *dev)
232 {
233 	return __serial_getc(dev);
234 }
235 
236 static int _serial_tstc(struct udevice *dev)
237 {
238 	return __serial_tstc(dev);
239 }
240 #endif /* CONFIG_IS_ENABLED(SERIAL_RX_BUFFER) */
241 
242 void serial_putc(char ch)
243 {
244 	if (gd->cur_serial_dev)
245 		_serial_putc(gd->cur_serial_dev, ch);
246 }
247 
248 void serial_puts(const char *str)
249 {
250 	if (gd->cur_serial_dev)
251 		_serial_puts(gd->cur_serial_dev, str);
252 }
253 
254 int serial_getc(void)
255 {
256 	if (!gd->cur_serial_dev)
257 		return 0;
258 
259 	return _serial_getc(gd->cur_serial_dev);
260 }
261 
262 int serial_tstc(void)
263 {
264 	if (!gd->cur_serial_dev)
265 		return 0;
266 
267 	return _serial_tstc(gd->cur_serial_dev);
268 }
269 
270 void serial_setbrg(void)
271 {
272 	struct dm_serial_ops *ops;
273 
274 	if (!gd->cur_serial_dev)
275 		return;
276 
277 	ops = serial_get_ops(gd->cur_serial_dev);
278 	if (ops->setbrg)
279 		ops->setbrg(gd->cur_serial_dev, gd->baudrate);
280 }
281 
282 void serial_clear(void)
283 {
284 	struct dm_serial_ops *ops;
285 
286 	if (!gd->cur_serial_dev)
287 		return;
288 
289 	ops = serial_get_ops(gd->cur_serial_dev);
290 	if (ops->setbrg)
291 		ops->clear(gd->cur_serial_dev);
292 }
293 
294 void serial_stdio_init(void)
295 {
296 }
297 
298 #if defined(CONFIG_DM_STDIO)
299 
300 #if CONFIG_IS_ENABLED(SERIAL_PRESENT)
301 static void serial_stub_putc(struct stdio_dev *sdev, const char ch)
302 {
303 	_serial_putc(sdev->priv, ch);
304 }
305 #endif
306 
307 static void serial_stub_puts(struct stdio_dev *sdev, const char *str)
308 {
309 	_serial_puts(sdev->priv, str);
310 }
311 
312 static int serial_stub_getc(struct stdio_dev *sdev)
313 {
314 	return _serial_getc(sdev->priv);
315 }
316 
317 static int serial_stub_tstc(struct stdio_dev *sdev)
318 {
319 	return _serial_tstc(sdev->priv);
320 }
321 #endif
322 
323 /**
324  * on_baudrate() - Update the actual baudrate when the env var changes
325  *
326  * This will check for a valid baudrate and only apply it if valid.
327  */
328 static int on_baudrate(const char *name, const char *value, enum env_op op,
329 	int flags)
330 {
331 	int i;
332 	int baudrate;
333 
334 	switch (op) {
335 	case env_op_create:
336 	case env_op_overwrite:
337 		/*
338 		 * Switch to new baudrate if new baudrate is supported
339 		 */
340 		baudrate = simple_strtoul(value, NULL, 10);
341 
342 		/* Not actually changing */
343 		if (gd->baudrate == baudrate)
344 			return 0;
345 
346 		for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) {
347 			if (baudrate == baudrate_table[i])
348 				break;
349 		}
350 		if (i == ARRAY_SIZE(baudrate_table)) {
351 			if ((flags & H_FORCE) == 0)
352 				printf("## Baudrate %d bps not supported\n",
353 				       baudrate);
354 			return 1;
355 		}
356 		if ((flags & H_INTERACTIVE) != 0) {
357 			printf("## Switch baudrate to %d bps and press ENTER ...\n",
358 			       baudrate);
359 			udelay(50000);
360 		}
361 
362 		gd->baudrate = baudrate;
363 
364 		serial_setbrg();
365 
366 		udelay(50000);
367 
368 		if ((flags & H_INTERACTIVE) != 0)
369 			while (1) {
370 				if (getc() == '\r')
371 					break;
372 			}
373 
374 		return 0;
375 	case env_op_delete:
376 		printf("## Baudrate may not be deleted\n");
377 		return 1;
378 	default:
379 		return 0;
380 	}
381 }
382 U_BOOT_ENV_CALLBACK(baudrate, on_baudrate);
383 
384 #if CONFIG_IS_ENABLED(SERIAL_PRESENT)
385 static int serial_post_probe(struct udevice *dev)
386 {
387 	struct dm_serial_ops *ops = serial_get_ops(dev);
388 #ifdef CONFIG_DM_STDIO
389 	struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
390 	struct stdio_dev sdev;
391 #endif
392 	int ret;
393 
394 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
395 	if (ops->setbrg)
396 		ops->setbrg += gd->reloc_off;
397 	if (ops->getc)
398 		ops->getc += gd->reloc_off;
399 	if (ops->putc)
400 		ops->putc += gd->reloc_off;
401 	if (ops->pending)
402 		ops->pending += gd->reloc_off;
403 	if (ops->clear)
404 		ops->clear += gd->reloc_off;
405 #if CONFIG_POST & CONFIG_SYS_POST_UART
406 	if (ops->loop)
407 		ops->loop += gd->reloc_off
408 #endif
409 #endif
410 	/* Set the baud rate */
411 	if (ops->setbrg) {
412 		ret = ops->setbrg(dev, gd->baudrate);
413 		if (ret)
414 			return ret;
415 	}
416 
417 #ifdef CONFIG_DM_STDIO
418 	if (!(gd->flags & GD_FLG_RELOC))
419 		return 0;
420 	memset(&sdev, '\0', sizeof(sdev));
421 
422 	strncpy(sdev.name, dev->name, sizeof(sdev.name));
423 	sdev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_DM;
424 	sdev.priv = dev;
425 	sdev.putc = serial_stub_putc;
426 	sdev.puts = serial_stub_puts;
427 	sdev.getc = serial_stub_getc;
428 	sdev.tstc = serial_stub_tstc;
429 
430 #if CONFIG_IS_ENABLED(SERIAL_RX_BUFFER)
431 	/* Allocate the RX buffer */
432 	upriv->buf = malloc(CONFIG_SERIAL_RX_BUFFER_SIZE);
433 #endif
434 
435 	stdio_register_dev(&sdev, &upriv->sdev);
436 #endif
437 	return 0;
438 }
439 
440 static int serial_pre_remove(struct udevice *dev)
441 {
442 #if CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER)
443 	struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
444 
445 	if (stdio_deregister_dev(upriv->sdev, true))
446 		return -EPERM;
447 #endif
448 
449 	return 0;
450 }
451 
452 UCLASS_DRIVER(serial) = {
453 	.id		= UCLASS_SERIAL,
454 	.name		= "serial",
455 	.flags		= DM_UC_FLAG_SEQ_ALIAS,
456 	.post_probe	= serial_post_probe,
457 	.pre_remove	= serial_pre_remove,
458 	.per_device_auto_alloc_size = sizeof(struct serial_dev_priv),
459 };
460 #endif
461