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