summaryrefslogtreecommitdiff
path: root/drivers/serial/serial_xtensa_semihosting.c
blob: 0e59a9bfdc752aa2ab3870c6dcf9cef229c59558 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (C) 2024 Jiaxun Yang <jiaxun.yang@flygoat.com>
 */

#include <dm.h>
#include <malloc.h>
#include <serial.h>

#include <asm/platform/simcall.h>

/**
 * struct simc_serial_priv - Semihosting serial private data
 * @counter: Counter used to fake pending every other call
 */
struct simc_serial_priv {
	unsigned int counter;
};

static int simc_serial_getc(struct udevice *dev)
{
	char ch = 0;

	simc_read(0, &ch, sizeof(ch));

	return ch;
}

static int simc_serial_putc(struct udevice *dev, const char ch)
{
	char str[2] = {0};

	str[0] = ch;
	simc_write(1, str, 1);

	return 0;
}

static int simc_serial_pending(struct udevice *dev, bool input)
{
	struct simc_serial_priv *priv = dev_get_priv(dev);

	if (input) {
		int res = simc_poll(0);
		return res < 0 ? priv->counter++ & 1 : res;
	}

	return false;
}

static ssize_t smh_serial_puts(struct udevice *dev, const char *s, size_t len)
{
	int ret;

	ret = simc_write(1, s, len);

	return ret;
}

static const struct dm_serial_ops simc_serial_ops = {
	.putc = simc_serial_putc,
	.puts = smh_serial_puts,
	.getc = simc_serial_getc,
	.pending = simc_serial_pending,
};

U_BOOT_DRIVER(simc_serial) = {
	.name	= "serial_xtensa_semihosting",
	.id	= UCLASS_SERIAL,
	.priv_auto = sizeof(struct simc_serial_priv),
	.ops	= &simc_serial_ops,
	.flags	= DM_FLAG_PRE_RELOC,
};

U_BOOT_DRVINFO(simc_serial) = {
	.name = "serial_xtensa_semihosting",
};

#if CONFIG_IS_ENABLED(DEBUG_UART_XTENSA_SEMIHOSTING)
#include <debug_uart.h>

static inline void _debug_uart_init(void)
{
}

static inline void _debug_uart_putc(int c)
{
	simc_serial_putc(NULL, c);
}

DEBUG_UART_FUNCS
#endif