sandbox.c 5.3 KB

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