Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 7accb6e | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2011 The Chromium OS Authors. |
Simon Glass | 7accb6e | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 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 <common.h> |
Joe Hershberger | 82a115f | 2018-07-02 20:06:49 -0500 | [diff] [blame] | 13 | #include <console.h> |
Simon Glass | 890fcef | 2014-09-04 16:27:27 -0600 | [diff] [blame] | 14 | #include <dm.h> |
Simon Glass | 7accb6e | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 15 | #include <os.h> |
Marek Vasut | cef46b7 | 2012-09-14 22:33:21 +0200 | [diff] [blame] | 16 | #include <serial.h> |
Simon Glass | 3ade5bc | 2016-01-18 19:52:25 -0700 | [diff] [blame] | 17 | #include <video.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 18 | #include <asm/global_data.h> |
Marek Vasut | cef46b7 | 2012-09-14 22:33:21 +0200 | [diff] [blame] | 19 | #include <linux/compiler.h> |
Simon Glass | bfae6cc | 2020-12-19 10:39:53 -0700 | [diff] [blame] | 20 | #include <asm/serial.h> |
Simon Glass | ffb8790 | 2014-02-27 13:26:22 -0700 | [diff] [blame] | 21 | #include <asm/state.h> |
Simon Glass | 7accb6e | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 22 | |
Simon Glass | 890fcef | 2014-09-04 16:27:27 -0600 | [diff] [blame] | 23 | DECLARE_GLOBAL_DATA_PTR; |
| 24 | |
Sean Anderson | 679190c | 2022-04-04 14:17:59 -0400 | [diff] [blame] | 25 | static size_t _sandbox_serial_written = 1; |
| 26 | static bool sandbox_serial_enabled = true; |
| 27 | |
| 28 | size_t sandbox_serial_written(void) |
| 29 | { |
| 30 | return _sandbox_serial_written; |
| 31 | } |
| 32 | |
| 33 | void sandbox_serial_endisable(bool enabled) |
| 34 | { |
| 35 | sandbox_serial_enabled = enabled; |
| 36 | } |
| 37 | |
Simon Glass | 72e9822 | 2014-09-04 16:27:28 -0600 | [diff] [blame] | 38 | /** |
| 39 | * output_ansi_colour() - Output an ANSI colour code |
| 40 | * |
| 41 | * @colour: Colour to output (0-7) |
| 42 | */ |
| 43 | static void output_ansi_colour(int colour) |
| 44 | { |
| 45 | char ansi_code[] = "\x1b[1;3Xm"; |
| 46 | |
| 47 | ansi_code[5] = '0' + colour; |
| 48 | os_write(1, ansi_code, sizeof(ansi_code) - 1); |
| 49 | } |
| 50 | |
| 51 | static void output_ansi_reset(void) |
| 52 | { |
| 53 | os_write(1, "\x1b[0m", 4); |
| 54 | } |
| 55 | |
Simon Glass | 890fcef | 2014-09-04 16:27:27 -0600 | [diff] [blame] | 56 | static int sandbox_serial_probe(struct udevice *dev) |
Simon Glass | 7accb6e | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 57 | { |
Simon Glass | ffb8790 | 2014-02-27 13:26:22 -0700 | [diff] [blame] | 58 | struct sandbox_state *state = state_get_current(); |
Simon Glass | 72e9822 | 2014-09-04 16:27:28 -0600 | [diff] [blame] | 59 | struct sandbox_serial_priv *priv = dev_get_priv(dev); |
Simon Glass | ffb8790 | 2014-02-27 13:26:22 -0700 | [diff] [blame] | 60 | |
| 61 | if (state->term_raw != STATE_TERM_COOKED) |
| 62 | os_tty_raw(0, state->term_raw == STATE_TERM_RAW_WITH_SIGS); |
Simon Glass | 72e9822 | 2014-09-04 16:27:28 -0600 | [diff] [blame] | 63 | priv->start_of_line = 0; |
| 64 | |
Joe Hershberger | 82a115f | 2018-07-02 20:06:49 -0500 | [diff] [blame] | 65 | if (state->term_raw != STATE_TERM_RAW) |
| 66 | disable_ctrlc(1); |
Simon Glass | 21f9075 | 2020-11-08 20:36:50 -0700 | [diff] [blame] | 67 | membuff_init(&priv->buf, priv->serial_buf, sizeof(priv->serial_buf)); |
Joe Hershberger | 82a115f | 2018-07-02 20:06:49 -0500 | [diff] [blame] | 68 | |
Simon Glass | 72e9822 | 2014-09-04 16:27:28 -0600 | [diff] [blame] | 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | static int sandbox_serial_remove(struct udevice *dev) |
| 73 | { |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 74 | struct sandbox_serial_plat *plat = dev_get_plat(dev); |
Simon Glass | 72e9822 | 2014-09-04 16:27:28 -0600 | [diff] [blame] | 75 | |
| 76 | if (plat->colour != -1) |
| 77 | output_ansi_reset(); |
Simon Glass | 890fcef | 2014-09-04 16:27:27 -0600 | [diff] [blame] | 78 | |
Simon Glass | 7accb6e | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 79 | return 0; |
| 80 | } |
| 81 | |
Sean Anderson | efa51f2 | 2022-04-04 14:17:58 -0400 | [diff] [blame] | 82 | static void sandbox_print_color(struct udevice *dev) |
Simon Glass | 7accb6e | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 83 | { |
Simon Glass | 72e9822 | 2014-09-04 16:27:28 -0600 | [diff] [blame] | 84 | struct sandbox_serial_priv *priv = dev_get_priv(dev); |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 85 | struct sandbox_serial_plat *plat = dev_get_plat(dev); |
Simon Glass | 72e9822 | 2014-09-04 16:27:28 -0600 | [diff] [blame] | 86 | |
Walter Lozano | e3e2470 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 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 && |
| 89 | plat->colour != -1) { |
Simon Glass | 72e9822 | 2014-09-04 16:27:28 -0600 | [diff] [blame] | 90 | priv->start_of_line = false; |
| 91 | output_ansi_colour(plat->colour); |
| 92 | } |
Sean Anderson | efa51f2 | 2022-04-04 14:17:58 -0400 | [diff] [blame] | 93 | } |
Simon Glass | 72e9822 | 2014-09-04 16:27:28 -0600 | [diff] [blame] | 94 | |
Sean Anderson | efa51f2 | 2022-04-04 14:17:58 -0400 | [diff] [blame] | 95 | static int sandbox_serial_putc(struct udevice *dev, const char ch) |
| 96 | { |
| 97 | struct sandbox_serial_priv *priv = dev_get_priv(dev); |
| 98 | |
Simon Glass | 72e9822 | 2014-09-04 16:27:28 -0600 | [diff] [blame] | 99 | if (ch == '\n') |
| 100 | priv->start_of_line = true; |
Simon Glass | 7accb6e | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 101 | |
Sean Anderson | 679190c | 2022-04-04 14:17:59 -0400 | [diff] [blame] | 102 | if (sandbox_serial_enabled) { |
| 103 | sandbox_print_color(dev); |
| 104 | os_write(1, &ch, 1); |
| 105 | } |
| 106 | _sandbox_serial_written += 1; |
Simon Glass | 890fcef | 2014-09-04 16:27:27 -0600 | [diff] [blame] | 107 | return 0; |
Simon Glass | 7accb6e | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Sean Anderson | efa51f2 | 2022-04-04 14:17:58 -0400 | [diff] [blame] | 110 | static ssize_t sandbox_serial_puts(struct udevice *dev, const char *s, |
| 111 | size_t len) |
| 112 | { |
| 113 | struct sandbox_serial_priv *priv = dev_get_priv(dev); |
Sean Anderson | 679190c | 2022-04-04 14:17:59 -0400 | [diff] [blame] | 114 | ssize_t ret; |
Sean Anderson | efa51f2 | 2022-04-04 14:17:58 -0400 | [diff] [blame] | 115 | |
Andrew Scull | 337b26e | 2022-05-30 10:00:01 +0000 | [diff] [blame] | 116 | if (len && s[len - 1] == '\n') |
Sean Anderson | efa51f2 | 2022-04-04 14:17:58 -0400 | [diff] [blame] | 117 | priv->start_of_line = true; |
| 118 | |
Sean Anderson | 679190c | 2022-04-04 14:17:59 -0400 | [diff] [blame] | 119 | if (sandbox_serial_enabled) { |
| 120 | sandbox_print_color(dev); |
| 121 | ret = os_write(1, s, len); |
| 122 | if (ret < 0) |
| 123 | return ret; |
| 124 | } else { |
| 125 | ret = len; |
| 126 | } |
| 127 | _sandbox_serial_written += ret; |
| 128 | return ret; |
Sean Anderson | efa51f2 | 2022-04-04 14:17:58 -0400 | [diff] [blame] | 129 | } |
| 130 | |
Simon Glass | 890fcef | 2014-09-04 16:27:27 -0600 | [diff] [blame] | 131 | static int sandbox_serial_pending(struct udevice *dev, bool input) |
Simon Glass | 7accb6e | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 132 | { |
Simon Glass | 21f9075 | 2020-11-08 20:36:50 -0700 | [diff] [blame] | 133 | struct sandbox_serial_priv *priv = dev_get_priv(dev); |
Taylor Hutt | e101550 | 2013-02-24 17:33:13 +0000 | [diff] [blame] | 134 | ssize_t count; |
Simon Glass | 21f9075 | 2020-11-08 20:36:50 -0700 | [diff] [blame] | 135 | char *data; |
| 136 | int avail; |
Taylor Hutt | e101550 | 2013-02-24 17:33:13 +0000 | [diff] [blame] | 137 | |
Simon Glass | 890fcef | 2014-09-04 16:27:27 -0600 | [diff] [blame] | 138 | if (!input) |
| 139 | return 0; |
| 140 | |
Taylor Hutt | e101550 | 2013-02-24 17:33:13 +0000 | [diff] [blame] | 141 | os_usleep(100); |
Simon Glass | b86986c | 2022-10-18 07:46:31 -0600 | [diff] [blame] | 142 | if (IS_ENABLED(CONFIG_VIDEO) && !IS_ENABLED(CONFIG_SPL_BUILD)) |
Simon Glass | c5ea016 | 2020-11-08 20:36:48 -0700 | [diff] [blame] | 143 | video_sync_all(); |
Simon Glass | 21f9075 | 2020-11-08 20:36:50 -0700 | [diff] [blame] | 144 | avail = membuff_putraw(&priv->buf, 100, false, &data); |
| 145 | if (!avail) |
Taylor Hutt | e101550 | 2013-02-24 17:33:13 +0000 | [diff] [blame] | 146 | return 1; /* buffer full */ |
| 147 | |
Simon Glass | 21f9075 | 2020-11-08 20:36:50 -0700 | [diff] [blame] | 148 | count = os_read(0, data, avail); |
| 149 | if (count > 0) |
| 150 | membuff_putraw(&priv->buf, count, true, &data); |
Simon Glass | 890fcef | 2014-09-04 16:27:27 -0600 | [diff] [blame] | 151 | |
Simon Glass | 21f9075 | 2020-11-08 20:36:50 -0700 | [diff] [blame] | 152 | return membuff_avail(&priv->buf); |
Taylor Hutt | e101550 | 2013-02-24 17:33:13 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Simon Glass | 890fcef | 2014-09-04 16:27:27 -0600 | [diff] [blame] | 155 | static int sandbox_serial_getc(struct udevice *dev) |
Taylor Hutt | e101550 | 2013-02-24 17:33:13 +0000 | [diff] [blame] | 156 | { |
Simon Glass | 21f9075 | 2020-11-08 20:36:50 -0700 | [diff] [blame] | 157 | struct sandbox_serial_priv *priv = dev_get_priv(dev); |
Taylor Hutt | e101550 | 2013-02-24 17:33:13 +0000 | [diff] [blame] | 158 | |
Simon Glass | 890fcef | 2014-09-04 16:27:27 -0600 | [diff] [blame] | 159 | if (!sandbox_serial_pending(dev, true)) |
| 160 | return -EAGAIN; /* buffer empty */ |
Taylor Hutt | e101550 | 2013-02-24 17:33:13 +0000 | [diff] [blame] | 161 | |
Simon Glass | 21f9075 | 2020-11-08 20:36:50 -0700 | [diff] [blame] | 162 | return membuff_getbyte(&priv->buf); |
Simon Glass | 7accb6e | 2011-10-03 19:26:46 +0000 | [diff] [blame] | 163 | } |
Marek Vasut | cef46b7 | 2012-09-14 22:33:21 +0200 | [diff] [blame] | 164 | |
Simon Glass | ee44176 | 2018-10-01 11:55:15 -0600 | [diff] [blame] | 165 | #ifdef CONFIG_DEBUG_UART_SANDBOX |
| 166 | |
| 167 | #include <debug_uart.h> |
| 168 | |
| 169 | static inline void _debug_uart_init(void) |
| 170 | { |
| 171 | } |
| 172 | |
| 173 | static inline void _debug_uart_putc(int ch) |
| 174 | { |
| 175 | os_putc(ch); |
| 176 | } |
| 177 | |
| 178 | DEBUG_UART_FUNCS |
| 179 | |
| 180 | #endif /* CONFIG_DEBUG_UART_SANDBOX */ |
| 181 | |
Andy Shevchenko | ac7f5db | 2018-11-20 23:52:32 +0200 | [diff] [blame] | 182 | static int sandbox_serial_getconfig(struct udevice *dev, uint *serial_config) |
| 183 | { |
| 184 | uint config = SERIAL_DEFAULT_CONFIG; |
| 185 | |
| 186 | if (!serial_config) |
| 187 | return -EINVAL; |
| 188 | |
| 189 | *serial_config = config; |
| 190 | |
| 191 | return 0; |
| 192 | } |
| 193 | |
Patrice Chotard | d7c0968 | 2018-08-03 15:07:41 +0200 | [diff] [blame] | 194 | static int sandbox_serial_setconfig(struct udevice *dev, uint serial_config) |
| 195 | { |
| 196 | u8 parity = SERIAL_GET_PARITY(serial_config); |
| 197 | u8 bits = SERIAL_GET_BITS(serial_config); |
| 198 | u8 stop = SERIAL_GET_STOP(serial_config); |
| 199 | |
| 200 | if (bits != SERIAL_8_BITS || stop != SERIAL_ONE_STOP || |
| 201 | parity != SERIAL_PAR_NONE) |
| 202 | return -ENOTSUPP; /* not supported in driver*/ |
| 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |
Andy Shevchenko | d5bb4f8 | 2018-11-20 23:52:33 +0200 | [diff] [blame] | 207 | static int sandbox_serial_getinfo(struct udevice *dev, |
| 208 | struct serial_device_info *serial_info) |
| 209 | { |
| 210 | struct serial_device_info info = { |
| 211 | .type = SERIAL_CHIP_UNKNOWN, |
| 212 | .addr_space = SERIAL_ADDRESS_SPACE_IO, |
| 213 | .addr = SERIAL_DEFAULT_ADDRESS, |
| 214 | .reg_width = 1, |
| 215 | .reg_offset = 0, |
| 216 | .reg_shift = 0, |
Andy Shevchenko | bf4661bc | 2020-02-27 17:21:54 +0200 | [diff] [blame] | 217 | .clock = SERIAL_DEFAULT_CLOCK, |
Andy Shevchenko | d5bb4f8 | 2018-11-20 23:52:33 +0200 | [diff] [blame] | 218 | }; |
| 219 | |
| 220 | if (!serial_info) |
| 221 | return -EINVAL; |
| 222 | |
| 223 | *serial_info = info; |
| 224 | |
| 225 | return 0; |
| 226 | } |
| 227 | |
Simon Glass | 72e9822 | 2014-09-04 16:27:28 -0600 | [diff] [blame] | 228 | static const char * const ansi_colour[] = { |
| 229 | "black", "red", "green", "yellow", "blue", "megenta", "cyan", |
| 230 | "white", |
| 231 | }; |
| 232 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 233 | static int sandbox_serial_of_to_plat(struct udevice *dev) |
Simon Glass | 72e9822 | 2014-09-04 16:27:28 -0600 | [diff] [blame] | 234 | { |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 235 | struct sandbox_serial_plat *plat = dev_get_plat(dev); |
Simon Glass | 72e9822 | 2014-09-04 16:27:28 -0600 | [diff] [blame] | 236 | const char *colour; |
| 237 | int i; |
| 238 | |
Simon Glass | ea14778 | 2019-09-25 08:55:53 -0600 | [diff] [blame] | 239 | if (CONFIG_IS_ENABLED(OF_PLATDATA)) |
| 240 | return 0; |
Simon Glass | 72e9822 | 2014-09-04 16:27:28 -0600 | [diff] [blame] | 241 | plat->colour = -1; |
Simon Glass | 1bf0a40 | 2020-11-08 20:36:49 -0700 | [diff] [blame] | 242 | colour = dev_read_string(dev, "sandbox,text-colour"); |
Simon Glass | 72e9822 | 2014-09-04 16:27:28 -0600 | [diff] [blame] | 243 | if (colour) { |
| 244 | for (i = 0; i < ARRAY_SIZE(ansi_colour); i++) { |
| 245 | if (!strcmp(colour, ansi_colour[i])) { |
| 246 | plat->colour = i; |
| 247 | break; |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | return 0; |
| 253 | } |
| 254 | |
Simon Glass | 890fcef | 2014-09-04 16:27:27 -0600 | [diff] [blame] | 255 | static const struct dm_serial_ops sandbox_serial_ops = { |
| 256 | .putc = sandbox_serial_putc, |
Sean Anderson | efa51f2 | 2022-04-04 14:17:58 -0400 | [diff] [blame] | 257 | .puts = sandbox_serial_puts, |
Simon Glass | 890fcef | 2014-09-04 16:27:27 -0600 | [diff] [blame] | 258 | .pending = sandbox_serial_pending, |
| 259 | .getc = sandbox_serial_getc, |
Andy Shevchenko | ac7f5db | 2018-11-20 23:52:32 +0200 | [diff] [blame] | 260 | .getconfig = sandbox_serial_getconfig, |
Patrice Chotard | d7c0968 | 2018-08-03 15:07:41 +0200 | [diff] [blame] | 261 | .setconfig = sandbox_serial_setconfig, |
Andy Shevchenko | d5bb4f8 | 2018-11-20 23:52:33 +0200 | [diff] [blame] | 262 | .getinfo = sandbox_serial_getinfo, |
Marek Vasut | cef46b7 | 2012-09-14 22:33:21 +0200 | [diff] [blame] | 263 | }; |
| 264 | |
Simon Glass | 890fcef | 2014-09-04 16:27:27 -0600 | [diff] [blame] | 265 | static const struct udevice_id sandbox_serial_ids[] = { |
| 266 | { .compatible = "sandbox,serial" }, |
| 267 | { } |
| 268 | }; |
Marek Vasut | cef46b7 | 2012-09-14 22:33:21 +0200 | [diff] [blame] | 269 | |
Walter Lozano | e3e2470 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 270 | U_BOOT_DRIVER(sandbox_serial) = { |
| 271 | .name = "sandbox_serial", |
Simon Glass | 890fcef | 2014-09-04 16:27:27 -0600 | [diff] [blame] | 272 | .id = UCLASS_SERIAL, |
| 273 | .of_match = sandbox_serial_ids, |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 274 | .of_to_plat = sandbox_serial_of_to_plat, |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 275 | .plat_auto = sizeof(struct sandbox_serial_plat), |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 276 | .priv_auto = sizeof(struct sandbox_serial_priv), |
Simon Glass | 890fcef | 2014-09-04 16:27:27 -0600 | [diff] [blame] | 277 | .probe = sandbox_serial_probe, |
Simon Glass | 72e9822 | 2014-09-04 16:27:28 -0600 | [diff] [blame] | 278 | .remove = sandbox_serial_remove, |
Simon Glass | 890fcef | 2014-09-04 16:27:27 -0600 | [diff] [blame] | 279 | .ops = &sandbox_serial_ops, |
| 280 | .flags = DM_FLAG_PRE_RELOC, |
| 281 | }; |
| 282 | |
Sean Anderson | eaf7385 | 2023-10-14 16:47:46 -0400 | [diff] [blame] | 283 | #if CONFIG_IS_ENABLED(OF_REAL) && !CONFIG_IS_ENABLED(OF_PLATDATA) |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 284 | static const struct sandbox_serial_plat platdata_non_fdt = { |
Simon Glass | 72e9822 | 2014-09-04 16:27:28 -0600 | [diff] [blame] | 285 | .colour = -1, |
| 286 | }; |
| 287 | |
Simon Glass | 20e442a | 2020-12-28 20:34:54 -0700 | [diff] [blame] | 288 | U_BOOT_DRVINFO(serial_sandbox_non_fdt) = { |
Walter Lozano | e3e2470 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 289 | .name = "sandbox_serial", |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 290 | .plat = &platdata_non_fdt, |
Simon Glass | 890fcef | 2014-09-04 16:27:27 -0600 | [diff] [blame] | 291 | }; |
Simon Glass | c147329 | 2020-10-03 11:31:23 -0600 | [diff] [blame] | 292 | #endif |