sandbox.c 5.3 KB

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