1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (c) 2011 The Chromium OS Authors.
4 */
5
6 /*
7 * This provide a test serial port. It provides an emulated serial port where
8 * a test program and read out the serial output and inject serial input for
9 * U-Boot.
10 */
11
12 #include <console.h>
13 #include <dm.h>
14 #include <os.h>
15 #include <serial.h>
16 #include <video.h>
17 #include <asm/global_data.h>
18 #include <linux/compiler.h>
19 #include <asm/serial.h>
20 #include <asm/state.h>
21
22 DECLARE_GLOBAL_DATA_PTR;
23
24 static size_t _sandbox_serial_written = 1;
25 static bool sandbox_serial_enabled = true;
26
sandbox_serial_written(void)27 size_t sandbox_serial_written(void)
28 {
29 return _sandbox_serial_written;
30 }
31
sandbox_serial_endisable(bool enabled)32 void sandbox_serial_endisable(bool enabled)
33 {
34 sandbox_serial_enabled = enabled;
35 }
36
37 /**
38 * output_ansi_colour() - Output an ANSI colour code
39 *
40 * @colour: Colour to output (0-7)
41 */
output_ansi_colour(int colour)42 static void output_ansi_colour(int colour)
43 {
44 char ansi_code[] = "\x1b[1;3Xm";
45
46 ansi_code[5] = '0' + colour;
47 os_write(1, ansi_code, sizeof(ansi_code) - 1);
48 }
49
output_ansi_reset(void)50 static void output_ansi_reset(void)
51 {
52 os_write(1, "\x1b[0m", 4);
53 }
54
sandbox_serial_probe(struct udevice * dev)55 static int sandbox_serial_probe(struct udevice *dev)
56 {
57 struct sandbox_state *state = state_get_current();
58 struct sandbox_serial_priv *priv = dev_get_priv(dev);
59
60 if (state->term_raw != STATE_TERM_COOKED)
61 os_tty_raw(0, state->term_raw == STATE_TERM_RAW_WITH_SIGS);
62 priv->start_of_line = 0;
63
64 if (state->term_raw != STATE_TERM_RAW)
65 disable_ctrlc(1);
66 membuf_init(&priv->buf, priv->serial_buf, sizeof(priv->serial_buf));
67
68 return 0;
69 }
70
sandbox_serial_remove(struct udevice * dev)71 static int sandbox_serial_remove(struct udevice *dev)
72 {
73 struct sandbox_serial_plat *plat = dev_get_plat(dev);
74
75 if (plat->colour != -1)
76 output_ansi_reset();
77
78 return 0;
79 }
80
sandbox_print_color(struct udevice * dev)81 static void sandbox_print_color(struct udevice *dev)
82 {
83 struct sandbox_serial_priv *priv = dev_get_priv(dev);
84 struct sandbox_serial_plat *plat = dev_get_plat(dev);
85
86 /* With of-platdata we don't real the colour correctly, so disable it */
87 if (!CONFIG_IS_ENABLED(OF_PLATDATA) && priv->start_of_line &&
88 plat->colour != -1) {
89 priv->start_of_line = false;
90 output_ansi_colour(plat->colour);
91 }
92 }
93
sandbox_serial_putc(struct udevice * dev,const char ch)94 static int sandbox_serial_putc(struct udevice *dev, const char ch)
95 {
96 struct sandbox_serial_priv *priv = dev_get_priv(dev);
97
98 if (ch == '\n')
99 priv->start_of_line = true;
100
101 if (sandbox_serial_enabled) {
102 sandbox_print_color(dev);
103 os_write(1, &ch, 1);
104 }
105 _sandbox_serial_written += 1;
106 return 0;
107 }
108
sandbox_serial_puts(struct udevice * dev,const char * s,size_t len)109 static ssize_t sandbox_serial_puts(struct udevice *dev, const char *s,
110 size_t len)
111 {
112 struct sandbox_serial_priv *priv = dev_get_priv(dev);
113 ssize_t ret;
114
115 if (len && s[len - 1] == '\n')
116 priv->start_of_line = true;
117
118 if (sandbox_serial_enabled) {
119 sandbox_print_color(dev);
120 ret = os_write(1, s, len);
121 if (ret < 0)
122 return ret;
123 } else {
124 ret = len;
125 }
126 _sandbox_serial_written += ret;
127 return ret;
128 }
129
sandbox_serial_pending(struct udevice * dev,bool input)130 static int sandbox_serial_pending(struct udevice *dev, bool input)
131 {
132 struct sandbox_serial_priv *priv = dev_get_priv(dev);
133 ssize_t count;
134 char *data;
135 int avail;
136
137 if (!input)
138 return 0;
139
140 os_usleep(100);
141 avail = membuf_putraw(&priv->buf, 100, false, &data);
142 if (!avail)
143 return 1; /* buffer full */
144
145 count = os_read(0, data, avail);
146 if (count > 0)
147 membuf_putraw(&priv->buf, count, true, &data);
148
149 return membuf_avail(&priv->buf);
150 }
151
sandbox_serial_getc(struct udevice * dev)152 static int sandbox_serial_getc(struct udevice *dev)
153 {
154 struct sandbox_serial_priv *priv = dev_get_priv(dev);
155
156 if (!sandbox_serial_pending(dev, true))
157 return -EAGAIN; /* buffer empty */
158
159 return membuf_getbyte(&priv->buf);
160 }
161
162 #ifdef CONFIG_DEBUG_UART_SANDBOX
163
164 #include <debug_uart.h>
165
_debug_uart_init(void)166 static inline void _debug_uart_init(void)
167 {
168 }
169
_debug_uart_putc(int ch)170 static inline void _debug_uart_putc(int ch)
171 {
172 os_putc(ch);
173 }
174
175 DEBUG_UART_FUNCS
176
177 #endif /* CONFIG_DEBUG_UART_SANDBOX */
178
sandbox_serial_getconfig(struct udevice * dev,uint * serial_config)179 static int sandbox_serial_getconfig(struct udevice *dev, uint *serial_config)
180 {
181 uint config = SERIAL_DEFAULT_CONFIG;
182
183 if (!serial_config)
184 return -EINVAL;
185
186 *serial_config = config;
187
188 return 0;
189 }
190
sandbox_serial_setconfig(struct udevice * dev,uint serial_config)191 static int sandbox_serial_setconfig(struct udevice *dev, uint serial_config)
192 {
193 u8 parity = SERIAL_GET_PARITY(serial_config);
194 u8 bits = SERIAL_GET_BITS(serial_config);
195 u8 stop = SERIAL_GET_STOP(serial_config);
196
197 if (bits != SERIAL_8_BITS || stop != SERIAL_ONE_STOP ||
198 parity != SERIAL_PAR_NONE)
199 return -ENOTSUPP; /* not supported in driver*/
200
201 return 0;
202 }
203
sandbox_serial_getinfo(struct udevice * dev,struct serial_device_info * serial_info)204 static int sandbox_serial_getinfo(struct udevice *dev,
205 struct serial_device_info *serial_info)
206 {
207 struct serial_device_info info = {
208 .type = SERIAL_CHIP_UNKNOWN,
209 .addr_space = SERIAL_ADDRESS_SPACE_IO,
210 .addr = SERIAL_DEFAULT_ADDRESS,
211 .reg_width = 1,
212 .reg_offset = 0,
213 .reg_shift = 0,
214 .clock = SERIAL_DEFAULT_CLOCK,
215 };
216
217 if (!serial_info)
218 return -EINVAL;
219
220 *serial_info = info;
221
222 return 0;
223 }
224
225 static const char * const ansi_colour[] = {
226 "black", "red", "green", "yellow", "blue", "megenta", "cyan",
227 "white",
228 };
229
sandbox_serial_of_to_plat(struct udevice * dev)230 static int sandbox_serial_of_to_plat(struct udevice *dev)
231 {
232 struct sandbox_serial_plat *plat = dev_get_plat(dev);
233 const char *colour;
234 int i;
235
236 if (CONFIG_IS_ENABLED(OF_PLATDATA))
237 return 0;
238 plat->colour = -1;
239 colour = dev_read_string(dev, "sandbox,text-colour");
240 if (colour) {
241 for (i = 0; i < ARRAY_SIZE(ansi_colour); i++) {
242 if (!strcmp(colour, ansi_colour[i])) {
243 plat->colour = i;
244 break;
245 }
246 }
247 }
248
249 return 0;
250 }
251
252 static const struct dm_serial_ops sandbox_serial_ops = {
253 .putc = sandbox_serial_putc,
254 .puts = sandbox_serial_puts,
255 .pending = sandbox_serial_pending,
256 .getc = sandbox_serial_getc,
257 .getconfig = sandbox_serial_getconfig,
258 .setconfig = sandbox_serial_setconfig,
259 .getinfo = sandbox_serial_getinfo,
260 };
261
262 static const struct udevice_id sandbox_serial_ids[] = {
263 { .compatible = "sandbox,serial" },
264 { }
265 };
266
267 U_BOOT_DRIVER(sandbox_serial) = {
268 .name = "sandbox_serial",
269 .id = UCLASS_SERIAL,
270 .of_match = sandbox_serial_ids,
271 .of_to_plat = sandbox_serial_of_to_plat,
272 .plat_auto = sizeof(struct sandbox_serial_plat),
273 .priv_auto = sizeof(struct sandbox_serial_priv),
274 .probe = sandbox_serial_probe,
275 .remove = sandbox_serial_remove,
276 .ops = &sandbox_serial_ops,
277 .flags = DM_FLAG_PRE_RELOC,
278 };
279
280 #if CONFIG_IS_ENABLED(OF_REAL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
281 static const struct sandbox_serial_plat platdata_non_fdt = {
282 .colour = -1,
283 };
284
285 U_BOOT_DRVINFO(serial_sandbox_non_fdt) = {
286 .name = "sandbox_serial",
287 .plat = &platdata_non_fdt,
288 };
289 #endif
290