2 * Copyright (c) 2011 The Chromium OS Authors.
4 * SPDX-License-Identifier: GPL-2.0+
8 * This provide a test serial port. It provides an emulated serial port where
9 * a test program and read out the serial output and inject serial input for
19 #include <linux/compiler.h>
20 #include <asm/state.h>
22 DECLARE_GLOBAL_DATA_PTR;
26 * serial_buf: A buffer that holds keyboard characters for the
30 * serial_buf_write == serial_buf_read -> empty buffer
31 * (serial_buf_write + 1) % 16 == serial_buf_read -> full buffer
33 static char serial_buf[16];
34 static unsigned int serial_buf_write;
35 static unsigned int serial_buf_read;
37 static int sandbox_serial_probe(struct udevice *dev)
39 struct sandbox_state *state = state_get_current();
41 if (state->term_raw != STATE_TERM_COOKED)
42 os_tty_raw(0, state->term_raw == STATE_TERM_RAW_WITH_SIGS);
47 static int sandbox_serial_putc(struct udevice *dev, const char ch)
54 static unsigned int increment_buffer_index(unsigned int index)
56 return (index + 1) % ARRAY_SIZE(serial_buf);
59 static int sandbox_serial_pending(struct udevice *dev, bool input)
61 const unsigned int next_index =
62 increment_buffer_index(serial_buf_write);
72 if (next_index == serial_buf_read)
73 return 1; /* buffer full */
75 count = os_read_no_block(0, &serial_buf[serial_buf_write], 1);
77 serial_buf_write = next_index;
79 return serial_buf_write != serial_buf_read;
82 static int sandbox_serial_getc(struct udevice *dev)
86 if (!sandbox_serial_pending(dev, true))
87 return -EAGAIN; /* buffer empty */
89 result = serial_buf[serial_buf_read];
90 serial_buf_read = increment_buffer_index(serial_buf_read);
94 static const struct dm_serial_ops sandbox_serial_ops = {
95 .putc = sandbox_serial_putc,
96 .pending = sandbox_serial_pending,
97 .getc = sandbox_serial_getc,
100 static const struct udevice_id sandbox_serial_ids[] = {
101 { .compatible = "sandbox,serial" },
105 U_BOOT_DRIVER(serial_sandbox) = {
106 .name = "serial_sandbox",
108 .of_match = sandbox_serial_ids,
109 .probe = sandbox_serial_probe,
110 .ops = &sandbox_serial_ops,
111 .flags = DM_FLAG_PRE_RELOC,
114 U_BOOT_DEVICE(serial_sandbox_non_fdt) = {
115 .name = "serial_sandbox",