sandbox.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. if (priv->start_of_line && plat->colour != -1) {
  78. priv->start_of_line = false;
  79. output_ansi_colour(plat->colour);
  80. }
  81. os_write(1, &ch, 1);
  82. if (ch == '\n')
  83. priv->start_of_line = true;
  84. return 0;
  85. }
  86. static unsigned int increment_buffer_index(unsigned int index)
  87. {
  88. return (index + 1) % ARRAY_SIZE(serial_buf);
  89. }
  90. static int sandbox_serial_pending(struct udevice *dev, bool input)
  91. {
  92. const unsigned int next_index =
  93. increment_buffer_index(serial_buf_write);
  94. ssize_t count;
  95. if (!input)
  96. return 0;
  97. os_usleep(100);
  98. #ifndef CONFIG_SPL_BUILD
  99. video_sync_all();
  100. #endif
  101. if (next_index == serial_buf_read)
  102. return 1; /* buffer full */
  103. count = os_read(0, &serial_buf[serial_buf_write], 1);
  104. if (count == 1)
  105. serial_buf_write = next_index;
  106. return serial_buf_write != serial_buf_read;
  107. }
  108. static int sandbox_serial_getc(struct udevice *dev)
  109. {
  110. int result;
  111. if (!sandbox_serial_pending(dev, true))
  112. return -EAGAIN; /* buffer empty */
  113. result = serial_buf[serial_buf_read];
  114. serial_buf_read = increment_buffer_index(serial_buf_read);
  115. return result;
  116. }
  117. #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
  118. #ifdef CONFIG_DEBUG_UART_SANDBOX
  119. #include <debug_uart.h>
  120. static inline void _debug_uart_init(void)
  121. {
  122. }
  123. static inline void _debug_uart_putc(int ch)
  124. {
  125. os_putc(ch);
  126. }
  127. DEBUG_UART_FUNCS
  128. #endif /* CONFIG_DEBUG_UART_SANDBOX */
  129. static int sandbox_serial_getconfig(struct udevice *dev, uint *serial_config)
  130. {
  131. uint config = SERIAL_DEFAULT_CONFIG;
  132. if (!serial_config)
  133. return -EINVAL;
  134. *serial_config = config;
  135. return 0;
  136. }
  137. static int sandbox_serial_setconfig(struct udevice *dev, uint serial_config)
  138. {
  139. u8 parity = SERIAL_GET_PARITY(serial_config);
  140. u8 bits = SERIAL_GET_BITS(serial_config);
  141. u8 stop = SERIAL_GET_STOP(serial_config);
  142. if (bits != SERIAL_8_BITS || stop != SERIAL_ONE_STOP ||
  143. parity != SERIAL_PAR_NONE)
  144. return -ENOTSUPP; /* not supported in driver*/
  145. return 0;
  146. }
  147. static int sandbox_serial_getinfo(struct udevice *dev,
  148. struct serial_device_info *serial_info)
  149. {
  150. struct serial_device_info info = {
  151. .type = SERIAL_CHIP_UNKNOWN,
  152. .addr_space = SERIAL_ADDRESS_SPACE_IO,
  153. .addr = SERIAL_DEFAULT_ADDRESS,
  154. .reg_width = 1,
  155. .reg_offset = 0,
  156. .reg_shift = 0,
  157. .clock = SERIAL_DEFAULT_CLOCK,
  158. };
  159. if (!serial_info)
  160. return -EINVAL;
  161. *serial_info = info;
  162. return 0;
  163. }
  164. #if CONFIG_IS_ENABLED(OF_CONTROL)
  165. static const char * const ansi_colour[] = {
  166. "black", "red", "green", "yellow", "blue", "megenta", "cyan",
  167. "white",
  168. };
  169. static int sandbox_serial_ofdata_to_platdata(struct udevice *dev)
  170. {
  171. struct sandbox_serial_platdata *plat = dev->platdata;
  172. const char *colour;
  173. int i;
  174. if (CONFIG_IS_ENABLED(OF_PLATDATA))
  175. return 0;
  176. plat->colour = -1;
  177. colour = fdt_getprop(gd->fdt_blob, dev_of_offset(dev),
  178. "sandbox,text-colour", NULL);
  179. if (colour) {
  180. for (i = 0; i < ARRAY_SIZE(ansi_colour); i++) {
  181. if (!strcmp(colour, ansi_colour[i])) {
  182. plat->colour = i;
  183. break;
  184. }
  185. }
  186. }
  187. return 0;
  188. }
  189. static const struct dm_serial_ops sandbox_serial_ops = {
  190. .putc = sandbox_serial_putc,
  191. .pending = sandbox_serial_pending,
  192. .getc = sandbox_serial_getc,
  193. .getconfig = sandbox_serial_getconfig,
  194. .setconfig = sandbox_serial_setconfig,
  195. .getinfo = sandbox_serial_getinfo,
  196. };
  197. static const struct udevice_id sandbox_serial_ids[] = {
  198. { .compatible = "sandbox,serial" },
  199. { }
  200. };
  201. U_BOOT_DRIVER(serial_sandbox) = {
  202. .name = "serial_sandbox",
  203. .id = UCLASS_SERIAL,
  204. .of_match = sandbox_serial_ids,
  205. .ofdata_to_platdata = sandbox_serial_ofdata_to_platdata,
  206. .platdata_auto_alloc_size = sizeof(struct sandbox_serial_platdata),
  207. .priv_auto_alloc_size = sizeof(struct sandbox_serial_priv),
  208. .probe = sandbox_serial_probe,
  209. .remove = sandbox_serial_remove,
  210. .ops = &sandbox_serial_ops,
  211. .flags = DM_FLAG_PRE_RELOC,
  212. };
  213. static const struct sandbox_serial_platdata platdata_non_fdt = {
  214. .colour = -1,
  215. };
  216. U_BOOT_DEVICE(serial_sandbox_non_fdt) = {
  217. .name = "serial_sandbox",
  218. .platdata = &platdata_non_fdt,
  219. };
  220. #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */