1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2011 The Chromium OS Authors.
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
18 #include <asm/global_data.h>
19 #include <linux/compiler.h>
20 #include <asm/serial.h>
21 #include <asm/state.h>
23 DECLARE_GLOBAL_DATA_PTR;
25 static size_t _sandbox_serial_written = 1;
26 static bool sandbox_serial_enabled = true;
28 size_t sandbox_serial_written(void)
30 return _sandbox_serial_written;
33 void sandbox_serial_endisable(bool enabled)
35 sandbox_serial_enabled = enabled;
39 * output_ansi_colour() - Output an ANSI colour code
41 * @colour: Colour to output (0-7)
43 static void output_ansi_colour(int colour)
45 char ansi_code[] = "\x1b[1;3Xm";
47 ansi_code[5] = '0' + colour;
48 os_write(1, ansi_code, sizeof(ansi_code) - 1);
51 static void output_ansi_reset(void)
53 os_write(1, "\x1b[0m", 4);
56 static int sandbox_serial_probe(struct udevice *dev)
58 struct sandbox_state *state = state_get_current();
59 struct sandbox_serial_priv *priv = dev_get_priv(dev);
61 if (state->term_raw != STATE_TERM_COOKED)
62 os_tty_raw(0, state->term_raw == STATE_TERM_RAW_WITH_SIGS);
63 priv->start_of_line = 0;
65 if (state->term_raw != STATE_TERM_RAW)
67 membuff_init(&priv->buf, priv->serial_buf, sizeof(priv->serial_buf));
72 static int sandbox_serial_remove(struct udevice *dev)
74 struct sandbox_serial_plat *plat = dev_get_plat(dev);
76 if (plat->colour != -1)
82 static void sandbox_print_color(struct udevice *dev)
84 struct sandbox_serial_priv *priv = dev_get_priv(dev);
85 struct sandbox_serial_plat *plat = dev_get_plat(dev);
87 /* With of-platdata we don't real the colour correctly, so disable it */
88 if (!CONFIG_IS_ENABLED(OF_PLATDATA) && priv->start_of_line &&
90 priv->start_of_line = false;
91 output_ansi_colour(plat->colour);
95 static int sandbox_serial_putc(struct udevice *dev, const char ch)
97 struct sandbox_serial_priv *priv = dev_get_priv(dev);
100 priv->start_of_line = true;
102 if (sandbox_serial_enabled) {
103 sandbox_print_color(dev);
106 _sandbox_serial_written += 1;
110 static ssize_t sandbox_serial_puts(struct udevice *dev, const char *s,
113 struct sandbox_serial_priv *priv = dev_get_priv(dev);
116 if (len && s[len - 1] == '\n')
117 priv->start_of_line = true;
119 if (sandbox_serial_enabled) {
120 sandbox_print_color(dev);
121 ret = os_write(1, s, len);
127 _sandbox_serial_written += ret;
131 static int sandbox_serial_pending(struct udevice *dev, bool input)
133 struct sandbox_serial_priv *priv = dev_get_priv(dev);
142 if (IS_ENABLED(CONFIG_VIDEO) && !IS_ENABLED(CONFIG_SPL_BUILD))
144 avail = membuff_putraw(&priv->buf, 100, false, &data);
146 return 1; /* buffer full */
148 count = os_read(0, data, avail);
150 membuff_putraw(&priv->buf, count, true, &data);
152 return membuff_avail(&priv->buf);
155 static int sandbox_serial_getc(struct udevice *dev)
157 struct sandbox_serial_priv *priv = dev_get_priv(dev);
159 if (!sandbox_serial_pending(dev, true))
160 return -EAGAIN; /* buffer empty */
162 return membuff_getbyte(&priv->buf);
165 #ifdef CONFIG_DEBUG_UART_SANDBOX
167 #include <debug_uart.h>
169 static inline void _debug_uart_init(void)
173 static inline void _debug_uart_putc(int ch)
180 #endif /* CONFIG_DEBUG_UART_SANDBOX */
182 static int sandbox_serial_getconfig(struct udevice *dev, uint *serial_config)
184 uint config = SERIAL_DEFAULT_CONFIG;
189 *serial_config = config;
194 static int sandbox_serial_setconfig(struct udevice *dev, uint serial_config)
196 u8 parity = SERIAL_GET_PARITY(serial_config);
197 u8 bits = SERIAL_GET_BITS(serial_config);
198 u8 stop = SERIAL_GET_STOP(serial_config);
200 if (bits != SERIAL_8_BITS || stop != SERIAL_ONE_STOP ||
201 parity != SERIAL_PAR_NONE)
202 return -ENOTSUPP; /* not supported in driver*/
207 static int sandbox_serial_getinfo(struct udevice *dev,
208 struct serial_device_info *serial_info)
210 struct serial_device_info info = {
211 .type = SERIAL_CHIP_UNKNOWN,
212 .addr_space = SERIAL_ADDRESS_SPACE_IO,
213 .addr = SERIAL_DEFAULT_ADDRESS,
217 .clock = SERIAL_DEFAULT_CLOCK,
228 static const char * const ansi_colour[] = {
229 "black", "red", "green", "yellow", "blue", "megenta", "cyan",
233 static int sandbox_serial_of_to_plat(struct udevice *dev)
235 struct sandbox_serial_plat *plat = dev_get_plat(dev);
239 if (CONFIG_IS_ENABLED(OF_PLATDATA))
242 colour = dev_read_string(dev, "sandbox,text-colour");
244 for (i = 0; i < ARRAY_SIZE(ansi_colour); i++) {
245 if (!strcmp(colour, ansi_colour[i])) {
255 static const struct dm_serial_ops sandbox_serial_ops = {
256 .putc = sandbox_serial_putc,
257 .puts = sandbox_serial_puts,
258 .pending = sandbox_serial_pending,
259 .getc = sandbox_serial_getc,
260 .getconfig = sandbox_serial_getconfig,
261 .setconfig = sandbox_serial_setconfig,
262 .getinfo = sandbox_serial_getinfo,
265 static const struct udevice_id sandbox_serial_ids[] = {
266 { .compatible = "sandbox,serial" },
270 U_BOOT_DRIVER(sandbox_serial) = {
271 .name = "sandbox_serial",
273 .of_match = sandbox_serial_ids,
274 .of_to_plat = sandbox_serial_of_to_plat,
275 .plat_auto = sizeof(struct sandbox_serial_plat),
276 .priv_auto = sizeof(struct sandbox_serial_priv),
277 .probe = sandbox_serial_probe,
278 .remove = sandbox_serial_remove,
279 .ops = &sandbox_serial_ops,
280 .flags = DM_FLAG_PRE_RELOC,
283 #if CONFIG_IS_ENABLED(OF_REAL)
284 static const struct sandbox_serial_plat platdata_non_fdt = {
288 U_BOOT_DRVINFO(serial_sandbox_non_fdt) = {
289 .name = "sandbox_serial",
290 .plat = &platdata_non_fdt,