xref: /rk3399_rockchip-uboot/drivers/serial/serial-uclass.c (revision f05ce84792cbd2e5573a414010d421eb8fbb7689)
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_stdio_init(void)
283 {
284 }
285 
286 #if defined(CONFIG_DM_STDIO)
287 
288 #if CONFIG_IS_ENABLED(SERIAL_PRESENT)
289 static void serial_stub_putc(struct stdio_dev *sdev, const char ch)
290 {
291 	_serial_putc(sdev->priv, ch);
292 }
293 #endif
294 
295 static void serial_stub_puts(struct stdio_dev *sdev, const char *str)
296 {
297 	_serial_puts(sdev->priv, str);
298 }
299 
300 static int serial_stub_getc(struct stdio_dev *sdev)
301 {
302 	return _serial_getc(sdev->priv);
303 }
304 
305 static int serial_stub_tstc(struct stdio_dev *sdev)
306 {
307 	return _serial_tstc(sdev->priv);
308 }
309 #endif
310 
311 /**
312  * on_baudrate() - Update the actual baudrate when the env var changes
313  *
314  * This will check for a valid baudrate and only apply it if valid.
315  */
316 static int on_baudrate(const char *name, const char *value, enum env_op op,
317 	int flags)
318 {
319 	int i;
320 	int baudrate;
321 
322 	switch (op) {
323 	case env_op_create:
324 	case env_op_overwrite:
325 		/*
326 		 * Switch to new baudrate if new baudrate is supported
327 		 */
328 		baudrate = simple_strtoul(value, NULL, 10);
329 
330 		/* Not actually changing */
331 		if (gd->baudrate == baudrate)
332 			return 0;
333 
334 		for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) {
335 			if (baudrate == baudrate_table[i])
336 				break;
337 		}
338 		if (i == ARRAY_SIZE(baudrate_table)) {
339 			if ((flags & H_FORCE) == 0)
340 				printf("## Baudrate %d bps not supported\n",
341 				       baudrate);
342 			return 1;
343 		}
344 		if ((flags & H_INTERACTIVE) != 0) {
345 			printf("## Switch baudrate to %d bps and press ENTER ...\n",
346 			       baudrate);
347 			udelay(50000);
348 		}
349 
350 		gd->baudrate = baudrate;
351 
352 		serial_setbrg();
353 
354 		udelay(50000);
355 
356 		if ((flags & H_INTERACTIVE) != 0)
357 			while (1) {
358 				if (getc() == '\r')
359 					break;
360 			}
361 
362 		return 0;
363 	case env_op_delete:
364 		printf("## Baudrate may not be deleted\n");
365 		return 1;
366 	default:
367 		return 0;
368 	}
369 }
370 U_BOOT_ENV_CALLBACK(baudrate, on_baudrate);
371 
372 #if CONFIG_IS_ENABLED(SERIAL_PRESENT)
373 static int serial_post_probe(struct udevice *dev)
374 {
375 	struct dm_serial_ops *ops = serial_get_ops(dev);
376 #ifdef CONFIG_DM_STDIO
377 	struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
378 	struct stdio_dev sdev;
379 #endif
380 	int ret;
381 
382 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
383 	if (ops->setbrg)
384 		ops->setbrg += gd->reloc_off;
385 	if (ops->getc)
386 		ops->getc += gd->reloc_off;
387 	if (ops->putc)
388 		ops->putc += gd->reloc_off;
389 	if (ops->pending)
390 		ops->pending += gd->reloc_off;
391 	if (ops->clear)
392 		ops->clear += gd->reloc_off;
393 #if CONFIG_POST & CONFIG_SYS_POST_UART
394 	if (ops->loop)
395 		ops->loop += gd->reloc_off
396 #endif
397 #endif
398 	/* Set the baud rate */
399 	if (ops->setbrg) {
400 		ret = ops->setbrg(dev, gd->baudrate);
401 		if (ret)
402 			return ret;
403 	}
404 
405 #ifdef CONFIG_DM_STDIO
406 	if (!(gd->flags & GD_FLG_RELOC))
407 		return 0;
408 	memset(&sdev, '\0', sizeof(sdev));
409 
410 	strncpy(sdev.name, dev->name, sizeof(sdev.name));
411 	sdev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_DM;
412 	sdev.priv = dev;
413 	sdev.putc = serial_stub_putc;
414 	sdev.puts = serial_stub_puts;
415 	sdev.getc = serial_stub_getc;
416 	sdev.tstc = serial_stub_tstc;
417 
418 #if CONFIG_IS_ENABLED(SERIAL_RX_BUFFER)
419 	/* Allocate the RX buffer */
420 	upriv->buf = malloc(CONFIG_SERIAL_RX_BUFFER_SIZE);
421 #endif
422 
423 	stdio_register_dev(&sdev, &upriv->sdev);
424 #endif
425 	return 0;
426 }
427 
428 static int serial_pre_remove(struct udevice *dev)
429 {
430 #if CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER)
431 	struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
432 
433 	if (stdio_deregister_dev(upriv->sdev, true))
434 		return -EPERM;
435 #endif
436 
437 	return 0;
438 }
439 
440 UCLASS_DRIVER(serial) = {
441 	.id		= UCLASS_SERIAL,
442 	.name		= "serial",
443 	.flags		= DM_UC_FLAG_SEQ_ALIAS,
444 	.post_probe	= serial_post_probe,
445 	.pre_remove	= serial_pre_remove,
446 	.per_device_auto_alloc_size = sizeof(struct serial_dev_priv),
447 };
448 #endif
449