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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (C) 2022 Sean Anderson <sean.anderson@seco.com>
*/
#include <dm.h>
#include <malloc.h>
#include <serial.h>
#include <semihosting.h>
/**
* struct smh_serial_priv - Semihosting serial private data
* @infd: stdin file descriptor (or error)
* @outfd: stdout file descriptor (or error)
* @counter: Counter used to fake pending every other call
*/
struct smh_serial_priv {
int infd;
int outfd;
unsigned counter;
};
#if CONFIG_IS_ENABLED(DM_SERIAL)
static int smh_serial_getc(struct udevice *dev)
{
char ch = 0;
struct smh_serial_priv *priv = dev_get_priv(dev);
if (priv->infd < 0)
return smh_getc();
smh_read(priv->infd, &ch, sizeof(ch));
return ch;
}
static int smh_serial_putc(struct udevice *dev, const char ch)
{
smh_putc(ch);
return 0;
}
static ssize_t smh_serial_puts(struct udevice *dev, const char *s, size_t len)
{
int ret;
struct smh_serial_priv *priv = dev_get_priv(dev);
unsigned long written;
if (priv->outfd < 0) {
char *buf;
/* Try and avoid a copy if we can */
if (!s[len + 1]) {
smh_puts(s);
return len;
}
buf = strndup(s, len);
if (!buf)
return -ENOMEM;
smh_puts(buf);
free(buf);
return len;
}
ret = smh_write(priv->outfd, s, len, &written);
if (written)
return written;
return ret;
}
static int smh_serial_pending(struct udevice *dev, bool input)
{
struct smh_serial_priv *priv = dev_get_priv(dev);
if (input)
return priv->counter++ & 1;
return false;
}
static const struct dm_serial_ops smh_serial_ops = {
.putc = smh_serial_putc,
.puts = smh_serial_puts,
.getc = smh_serial_getc,
.pending = smh_serial_pending,
};
static int smh_serial_bind(struct udevice *dev)
{
if (semihosting_enabled())
return 0;
return -ENOENT;
}
static int smh_serial_probe(struct udevice *dev)
{
struct smh_serial_priv *priv = dev_get_priv(dev);
priv->infd = smh_open(":tt", MODE_READ);
priv->outfd = smh_open(":tt", MODE_WRITE);
return 0;
}
U_BOOT_DRIVER(smh_serial) = {
.name = "serial_semihosting",
.id = UCLASS_SERIAL,
.bind = smh_serial_bind,
.probe = smh_serial_probe,
.priv_auto = sizeof(struct smh_serial_priv),
.ops = &smh_serial_ops,
.flags = DM_FLAG_PRE_RELOC,
};
U_BOOT_DRVINFO(smh_serial) = {
.name = "serial_semihosting",
};
#else /* DM_SERIAL */
static int infd = -ENODEV;
static int outfd = -ENODEV;
static unsigned counter = 1;
static int smh_serial_start(void)
{
infd = smh_open(":tt", MODE_READ);
outfd = smh_open(":tt", MODE_WRITE);
return 0;
}
static int smh_serial_stop(void)
{
if (outfd >= 0)
smh_close(outfd);
return 0;
}
static void smh_serial_setbrg(void)
{
}
static int smh_serial_getc(void)
{
char ch = 0;
if (infd < 0)
return smh_getc();
smh_read(infd, &ch, sizeof(ch));
return ch;
}
static int smh_serial_tstc(void)
{
return counter++ & 1;
}
static void smh_serial_puts(const char *s)
{
ulong unused;
if (outfd < 0)
smh_puts(s);
else
smh_write(outfd, s, strlen(s), &unused);
}
struct serial_device serial_smh_device = {
.name = "serial_smh",
.start = smh_serial_start,
.stop = smh_serial_stop,
.setbrg = smh_serial_setbrg,
.getc = smh_serial_getc,
.tstc = smh_serial_tstc,
.putc = smh_putc,
.puts = smh_serial_puts,
};
void smh_serial_initialize(void)
{
if (semihosting_enabled())
serial_register(&serial_smh_device);
}
__weak struct serial_device *default_serial_console(void)
{
return &serial_smh_device;
}
#endif
#ifdef CONFIG_DEBUG_UART_SEMIHOSTING
#include <debug_uart.h>
static inline void _debug_uart_init(void)
{
}
static inline void _debug_uart_putc(int c)
{
smh_putc(c);
}
DEBUG_UART_FUNCS
#endif
|