sandbox.c 6.2 KB

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