sandbox.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 <fdtdec.h>
  14. #include <lcd.h>
  15. #include <os.h>
  16. #include <serial.h>
  17. #include <video.h>
  18. #include <linux/compiler.h>
  19. #include <asm/state.h>
  20. DECLARE_GLOBAL_DATA_PTR;
  21. #if CONFIG_IS_ENABLED(OF_CONTROL)
  22. /*
  23. *
  24. * serial_buf: A buffer that holds keyboard characters for the
  25. * Sandbox U-Boot.
  26. *
  27. * invariants:
  28. * serial_buf_write == serial_buf_read -> empty buffer
  29. * (serial_buf_write + 1) % 16 == serial_buf_read -> full buffer
  30. */
  31. static unsigned char serial_buf[16];
  32. static unsigned int serial_buf_write;
  33. static unsigned int serial_buf_read;
  34. struct sandbox_serial_platdata {
  35. int colour; /* Text colour to use for output, -1 for none */
  36. };
  37. struct sandbox_serial_priv {
  38. bool start_of_line;
  39. };
  40. /**
  41. * output_ansi_colour() - Output an ANSI colour code
  42. *
  43. * @colour: Colour to output (0-7)
  44. */
  45. static void output_ansi_colour(int colour)
  46. {
  47. char ansi_code[] = "\x1b[1;3Xm";
  48. ansi_code[5] = '0' + colour;
  49. os_write(1, ansi_code, sizeof(ansi_code) - 1);
  50. }
  51. static void output_ansi_reset(void)
  52. {
  53. os_write(1, "\x1b[0m", 4);
  54. }
  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. if (state->term_raw != STATE_TERM_COOKED)
  60. os_tty_raw(0, state->term_raw == STATE_TERM_RAW_WITH_SIGS);
  61. priv->start_of_line = 0;
  62. if (state->term_raw != STATE_TERM_RAW)
  63. disable_ctrlc(1);
  64. return 0;
  65. }
  66. static int sandbox_serial_remove(struct udevice *dev)
  67. {
  68. struct sandbox_serial_platdata *plat = dev->platdata;
  69. if (plat->colour != -1)
  70. output_ansi_reset();
  71. return 0;
  72. }
  73. static int sandbox_serial_putc(struct udevice *dev, const char ch)
  74. {
  75. struct sandbox_serial_priv *priv = dev_get_priv(dev);
  76. struct sandbox_serial_platdata *plat = dev->platdata;
  77. /* With of-platdata we don't real the colour correctly, so disable it */
  78. if (!CONFIG_IS_ENABLED(OF_PLATDATA) && priv->start_of_line &&
  79. plat->colour != -1) {
  80. priv->start_of_line = false;
  81. output_ansi_colour(plat->colour);
  82. }
  83. os_write(1, &ch, 1);
  84. if (ch == '\n')
  85. priv->start_of_line = true;
  86. return 0;
  87. }
  88. static unsigned int increment_buffer_index(unsigned int index)
  89. {
  90. return (index + 1) % ARRAY_SIZE(serial_buf);
  91. }
  92. static int sandbox_serial_pending(struct udevice *dev, bool input)
  93. {
  94. const unsigned int next_index =
  95. increment_buffer_index(serial_buf_write);
  96. ssize_t count;
  97. if (!input)
  98. return 0;
  99. os_usleep(100);
  100. #ifndef CONFIG_SPL_BUILD
  101. video_sync_all();
  102. #endif
  103. if (next_index == serial_buf_read)
  104. return 1; /* buffer full */
  105. count = os_read(0, &serial_buf[serial_buf_write], 1);
  106. if (count == 1)
  107. serial_buf_write = next_index;
  108. return serial_buf_write != serial_buf_read;
  109. }
  110. static int sandbox_serial_getc(struct udevice *dev)
  111. {
  112. int result;
  113. if (!sandbox_serial_pending(dev, true))
  114. return -EAGAIN; /* buffer empty */
  115. result = serial_buf[serial_buf_read];
  116. serial_buf_read = increment_buffer_index(serial_buf_read);
  117. return result;
  118. }
  119. #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
  120. #ifdef CONFIG_DEBUG_UART_SANDBOX
  121. #include <debug_uart.h>
  122. static inline void _debug_uart_init(void)
  123. {
  124. }
  125. static inline void _debug_uart_putc(int ch)
  126. {
  127. os_putc(ch);
  128. }
  129. DEBUG_UART_FUNCS
  130. #endif /* CONFIG_DEBUG_UART_SANDBOX */
  131. static int sandbox_serial_getconfig(struct udevice *dev, uint *serial_config)
  132. {
  133. uint config = SERIAL_DEFAULT_CONFIG;
  134. if (!serial_config)
  135. return -EINVAL;
  136. *serial_config = config;
  137. return 0;
  138. }
  139. static int sandbox_serial_setconfig(struct udevice *dev, uint serial_config)
  140. {
  141. u8 parity = SERIAL_GET_PARITY(serial_config);
  142. u8 bits = SERIAL_GET_BITS(serial_config);
  143. u8 stop = SERIAL_GET_STOP(serial_config);
  144. if (bits != SERIAL_8_BITS || stop != SERIAL_ONE_STOP ||
  145. parity != SERIAL_PAR_NONE)
  146. return -ENOTSUPP; /* not supported in driver*/
  147. return 0;
  148. }
  149. static int sandbox_serial_getinfo(struct udevice *dev,
  150. struct serial_device_info *serial_info)
  151. {
  152. struct serial_device_info info = {
  153. .type = SERIAL_CHIP_UNKNOWN,
  154. .addr_space = SERIAL_ADDRESS_SPACE_IO,
  155. .addr = SERIAL_DEFAULT_ADDRESS,
  156. .reg_width = 1,
  157. .reg_offset = 0,
  158. .reg_shift = 0,
  159. .clock = SERIAL_DEFAULT_CLOCK,
  160. };
  161. if (!serial_info)
  162. return -EINVAL;
  163. *serial_info = info;
  164. return 0;
  165. }
  166. #if CONFIG_IS_ENABLED(OF_CONTROL)
  167. static const char * const ansi_colour[] = {
  168. "black", "red", "green", "yellow", "blue", "megenta", "cyan",
  169. "white",
  170. };
  171. static int sandbox_serial_ofdata_to_platdata(struct udevice *dev)
  172. {
  173. struct sandbox_serial_platdata *plat = dev->platdata;
  174. const char *colour;
  175. int i;
  176. if (CONFIG_IS_ENABLED(OF_PLATDATA))
  177. return 0;
  178. plat->colour = -1;
  179. colour = fdt_getprop(gd->fdt_blob, dev_of_offset(dev),
  180. "sandbox,text-colour", NULL);
  181. if (colour) {
  182. for (i = 0; i < ARRAY_SIZE(ansi_colour); i++) {
  183. if (!strcmp(colour, ansi_colour[i])) {
  184. plat->colour = i;
  185. break;
  186. }
  187. }
  188. }
  189. return 0;
  190. }
  191. static const struct dm_serial_ops sandbox_serial_ops = {
  192. .putc = sandbox_serial_putc,
  193. .pending = sandbox_serial_pending,
  194. .getc = sandbox_serial_getc,
  195. .getconfig = sandbox_serial_getconfig,
  196. .setconfig = sandbox_serial_setconfig,
  197. .getinfo = sandbox_serial_getinfo,
  198. };
  199. static const struct udevice_id sandbox_serial_ids[] = {
  200. { .compatible = "sandbox,serial" },
  201. { }
  202. };
  203. U_BOOT_DRIVER(sandbox_serial) = {
  204. .name = "sandbox_serial",
  205. .id = UCLASS_SERIAL,
  206. .of_match = sandbox_serial_ids,
  207. .ofdata_to_platdata = sandbox_serial_ofdata_to_platdata,
  208. .platdata_auto_alloc_size = sizeof(struct sandbox_serial_platdata),
  209. .priv_auto_alloc_size = sizeof(struct sandbox_serial_priv),
  210. .probe = sandbox_serial_probe,
  211. .remove = sandbox_serial_remove,
  212. .ops = &sandbox_serial_ops,
  213. .flags = DM_FLAG_PRE_RELOC,
  214. };
  215. #if !CONFIG_IS_ENABLED(OF_PLATDATA)
  216. static const struct sandbox_serial_platdata platdata_non_fdt = {
  217. .colour = -1,
  218. };
  219. U_BOOT_DEVICE(serial_sandbox_non_fdt) = {
  220. .name = "sandbox_serial",
  221. .platdata = &platdata_non_fdt,
  222. };
  223. #endif
  224. #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */