2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2011-10-03 19:26:46 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2011 The Chromium OS Authors.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This provide a test serial port. It provides an emulated serial port where
|
|
|
|
* a test program and read out the serial output and inject serial input for
|
|
|
|
* U-Boot.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
2018-07-03 01:06:49 +00:00
|
|
|
#include <console.h>
|
2014-09-04 22:27:27 +00:00
|
|
|
#include <dm.h>
|
|
|
|
#include <fdtdec.h>
|
2014-02-27 20:26:19 +00:00
|
|
|
#include <lcd.h>
|
2011-10-03 19:26:46 +00:00
|
|
|
#include <os.h>
|
2012-09-14 20:33:21 +00:00
|
|
|
#include <serial.h>
|
2016-01-19 02:52:25 +00:00
|
|
|
#include <video.h>
|
2012-09-14 20:33:21 +00:00
|
|
|
#include <linux/compiler.h>
|
2014-02-27 20:26:22 +00:00
|
|
|
#include <asm/state.h>
|
2011-10-03 19:26:46 +00:00
|
|
|
|
2014-09-04 22:27:27 +00:00
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
2018-10-01 17:55:16 +00:00
|
|
|
#if CONFIG_IS_ENABLED(OF_CONTROL)
|
|
|
|
|
2013-02-24 17:33:13 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* serial_buf: A buffer that holds keyboard characters for the
|
2016-02-06 03:30:11 +00:00
|
|
|
* Sandbox U-Boot.
|
2013-02-24 17:33:13 +00:00
|
|
|
*
|
|
|
|
* invariants:
|
|
|
|
* serial_buf_write == serial_buf_read -> empty buffer
|
|
|
|
* (serial_buf_write + 1) % 16 == serial_buf_read -> full buffer
|
|
|
|
*/
|
2019-11-09 09:59:02 +00:00
|
|
|
static unsigned char serial_buf[16];
|
2013-02-24 17:33:13 +00:00
|
|
|
static unsigned int serial_buf_write;
|
|
|
|
static unsigned int serial_buf_read;
|
|
|
|
|
2014-09-04 22:27:28 +00:00
|
|
|
struct sandbox_serial_platdata {
|
|
|
|
int colour; /* Text colour to use for output, -1 for none */
|
|
|
|
};
|
|
|
|
|
|
|
|
struct sandbox_serial_priv {
|
|
|
|
bool start_of_line;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* output_ansi_colour() - Output an ANSI colour code
|
|
|
|
*
|
|
|
|
* @colour: Colour to output (0-7)
|
|
|
|
*/
|
|
|
|
static void output_ansi_colour(int colour)
|
|
|
|
{
|
|
|
|
char ansi_code[] = "\x1b[1;3Xm";
|
|
|
|
|
|
|
|
ansi_code[5] = '0' + colour;
|
|
|
|
os_write(1, ansi_code, sizeof(ansi_code) - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void output_ansi_reset(void)
|
|
|
|
{
|
|
|
|
os_write(1, "\x1b[0m", 4);
|
|
|
|
}
|
|
|
|
|
2014-09-04 22:27:27 +00:00
|
|
|
static int sandbox_serial_probe(struct udevice *dev)
|
2011-10-03 19:26:46 +00:00
|
|
|
{
|
2014-02-27 20:26:22 +00:00
|
|
|
struct sandbox_state *state = state_get_current();
|
2014-09-04 22:27:28 +00:00
|
|
|
struct sandbox_serial_priv *priv = dev_get_priv(dev);
|
2014-02-27 20:26:22 +00:00
|
|
|
|
|
|
|
if (state->term_raw != STATE_TERM_COOKED)
|
|
|
|
os_tty_raw(0, state->term_raw == STATE_TERM_RAW_WITH_SIGS);
|
2014-09-04 22:27:28 +00:00
|
|
|
priv->start_of_line = 0;
|
|
|
|
|
2018-07-03 01:06:49 +00:00
|
|
|
if (state->term_raw != STATE_TERM_RAW)
|
|
|
|
disable_ctrlc(1);
|
|
|
|
|
2014-09-04 22:27:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sandbox_serial_remove(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct sandbox_serial_platdata *plat = dev->platdata;
|
|
|
|
|
|
|
|
if (plat->colour != -1)
|
|
|
|
output_ansi_reset();
|
2011-10-03 19:26:46 +00:00
|
|
|
|
2014-09-04 22:27:27 +00:00
|
|
|
return 0;
|
2011-10-03 19:26:46 +00:00
|
|
|
}
|
|
|
|
|
2014-09-04 22:27:27 +00:00
|
|
|
static int sandbox_serial_putc(struct udevice *dev, const char ch)
|
2011-10-03 19:26:46 +00:00
|
|
|
{
|
2014-09-04 22:27:28 +00:00
|
|
|
struct sandbox_serial_priv *priv = dev_get_priv(dev);
|
|
|
|
struct sandbox_serial_platdata *plat = dev->platdata;
|
|
|
|
|
2020-06-25 04:10:04 +00:00
|
|
|
/* With of-platdata we don't real the colour correctly, so disable it */
|
|
|
|
if (!CONFIG_IS_ENABLED(OF_PLATDATA) && priv->start_of_line &&
|
|
|
|
plat->colour != -1) {
|
2014-09-04 22:27:28 +00:00
|
|
|
priv->start_of_line = false;
|
|
|
|
output_ansi_colour(plat->colour);
|
|
|
|
}
|
|
|
|
|
2011-10-03 19:26:46 +00:00
|
|
|
os_write(1, &ch, 1);
|
2014-09-04 22:27:28 +00:00
|
|
|
if (ch == '\n')
|
|
|
|
priv->start_of_line = true;
|
2011-10-03 19:26:46 +00:00
|
|
|
|
2014-09-04 22:27:27 +00:00
|
|
|
return 0;
|
2011-10-03 19:26:46 +00:00
|
|
|
}
|
|
|
|
|
2013-02-24 17:33:13 +00:00
|
|
|
static unsigned int increment_buffer_index(unsigned int index)
|
|
|
|
{
|
|
|
|
return (index + 1) % ARRAY_SIZE(serial_buf);
|
|
|
|
}
|
|
|
|
|
2014-09-04 22:27:27 +00:00
|
|
|
static int sandbox_serial_pending(struct udevice *dev, bool input)
|
2011-10-03 19:26:46 +00:00
|
|
|
{
|
2013-02-24 17:33:13 +00:00
|
|
|
const unsigned int next_index =
|
|
|
|
increment_buffer_index(serial_buf_write);
|
2011-10-26 00:21:01 +00:00
|
|
|
ssize_t count;
|
2011-10-03 19:26:46 +00:00
|
|
|
|
2014-09-04 22:27:27 +00:00
|
|
|
if (!input)
|
|
|
|
return 0;
|
|
|
|
|
2013-02-24 17:33:13 +00:00
|
|
|
os_usleep(100);
|
2016-07-04 17:57:53 +00:00
|
|
|
#ifndef CONFIG_SPL_BUILD
|
2016-01-19 02:52:25 +00:00
|
|
|
video_sync_all();
|
2016-07-04 17:57:53 +00:00
|
|
|
#endif
|
2013-02-24 17:33:13 +00:00
|
|
|
if (next_index == serial_buf_read)
|
|
|
|
return 1; /* buffer full */
|
|
|
|
|
2018-10-01 17:55:20 +00:00
|
|
|
count = os_read(0, &serial_buf[serial_buf_write], 1);
|
2013-02-24 17:33:13 +00:00
|
|
|
if (count == 1)
|
|
|
|
serial_buf_write = next_index;
|
2014-09-04 22:27:27 +00:00
|
|
|
|
2013-02-24 17:33:13 +00:00
|
|
|
return serial_buf_write != serial_buf_read;
|
2011-10-03 19:26:46 +00:00
|
|
|
}
|
|
|
|
|
2014-09-04 22:27:27 +00:00
|
|
|
static int sandbox_serial_getc(struct udevice *dev)
|
2011-10-03 19:26:46 +00:00
|
|
|
{
|
2013-02-24 17:33:13 +00:00
|
|
|
int result;
|
|
|
|
|
2014-09-04 22:27:27 +00:00
|
|
|
if (!sandbox_serial_pending(dev, true))
|
|
|
|
return -EAGAIN; /* buffer empty */
|
2013-02-24 17:33:13 +00:00
|
|
|
|
|
|
|
result = serial_buf[serial_buf_read];
|
|
|
|
serial_buf_read = increment_buffer_index(serial_buf_read);
|
|
|
|
return result;
|
2011-10-03 19:26:46 +00:00
|
|
|
}
|
2018-10-01 17:55:16 +00:00
|
|
|
#endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
|
2012-09-14 20:33:21 +00:00
|
|
|
|
2018-10-01 17:55:15 +00:00
|
|
|
#ifdef CONFIG_DEBUG_UART_SANDBOX
|
|
|
|
|
|
|
|
#include <debug_uart.h>
|
|
|
|
|
|
|
|
static inline void _debug_uart_init(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void _debug_uart_putc(int ch)
|
|
|
|
{
|
|
|
|
os_putc(ch);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG_UART_FUNCS
|
|
|
|
|
|
|
|
#endif /* CONFIG_DEBUG_UART_SANDBOX */
|
|
|
|
|
2018-11-20 21:52:32 +00:00
|
|
|
static int sandbox_serial_getconfig(struct udevice *dev, uint *serial_config)
|
|
|
|
{
|
|
|
|
uint config = SERIAL_DEFAULT_CONFIG;
|
|
|
|
|
|
|
|
if (!serial_config)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
*serial_config = config;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-08-03 13:07:41 +00:00
|
|
|
static int sandbox_serial_setconfig(struct udevice *dev, uint serial_config)
|
|
|
|
{
|
|
|
|
u8 parity = SERIAL_GET_PARITY(serial_config);
|
|
|
|
u8 bits = SERIAL_GET_BITS(serial_config);
|
|
|
|
u8 stop = SERIAL_GET_STOP(serial_config);
|
|
|
|
|
|
|
|
if (bits != SERIAL_8_BITS || stop != SERIAL_ONE_STOP ||
|
|
|
|
parity != SERIAL_PAR_NONE)
|
|
|
|
return -ENOTSUPP; /* not supported in driver*/
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-20 21:52:33 +00:00
|
|
|
static int sandbox_serial_getinfo(struct udevice *dev,
|
|
|
|
struct serial_device_info *serial_info)
|
|
|
|
{
|
|
|
|
struct serial_device_info info = {
|
|
|
|
.type = SERIAL_CHIP_UNKNOWN,
|
|
|
|
.addr_space = SERIAL_ADDRESS_SPACE_IO,
|
|
|
|
.addr = SERIAL_DEFAULT_ADDRESS,
|
|
|
|
.reg_width = 1,
|
|
|
|
.reg_offset = 0,
|
|
|
|
.reg_shift = 0,
|
2020-02-27 15:21:54 +00:00
|
|
|
.clock = SERIAL_DEFAULT_CLOCK,
|
2018-11-20 21:52:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (!serial_info)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
*serial_info = info;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-01 17:55:16 +00:00
|
|
|
#if CONFIG_IS_ENABLED(OF_CONTROL)
|
2014-09-04 22:27:28 +00:00
|
|
|
static const char * const ansi_colour[] = {
|
|
|
|
"black", "red", "green", "yellow", "blue", "megenta", "cyan",
|
|
|
|
"white",
|
|
|
|
};
|
|
|
|
|
|
|
|
static int sandbox_serial_ofdata_to_platdata(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct sandbox_serial_platdata *plat = dev->platdata;
|
|
|
|
const char *colour;
|
|
|
|
int i;
|
|
|
|
|
2019-09-25 14:55:53 +00:00
|
|
|
if (CONFIG_IS_ENABLED(OF_PLATDATA))
|
|
|
|
return 0;
|
2014-09-04 22:27:28 +00:00
|
|
|
plat->colour = -1;
|
2017-01-17 23:52:55 +00:00
|
|
|
colour = fdt_getprop(gd->fdt_blob, dev_of_offset(dev),
|
2014-09-04 22:27:28 +00:00
|
|
|
"sandbox,text-colour", NULL);
|
|
|
|
if (colour) {
|
|
|
|
for (i = 0; i < ARRAY_SIZE(ansi_colour); i++) {
|
|
|
|
if (!strcmp(colour, ansi_colour[i])) {
|
|
|
|
plat->colour = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-09-04 22:27:27 +00:00
|
|
|
static const struct dm_serial_ops sandbox_serial_ops = {
|
|
|
|
.putc = sandbox_serial_putc,
|
|
|
|
.pending = sandbox_serial_pending,
|
|
|
|
.getc = sandbox_serial_getc,
|
2018-11-20 21:52:32 +00:00
|
|
|
.getconfig = sandbox_serial_getconfig,
|
2018-08-03 13:07:41 +00:00
|
|
|
.setconfig = sandbox_serial_setconfig,
|
2018-11-20 21:52:33 +00:00
|
|
|
.getinfo = sandbox_serial_getinfo,
|
2012-09-14 20:33:21 +00:00
|
|
|
};
|
|
|
|
|
2014-09-04 22:27:27 +00:00
|
|
|
static const struct udevice_id sandbox_serial_ids[] = {
|
|
|
|
{ .compatible = "sandbox,serial" },
|
|
|
|
{ }
|
|
|
|
};
|
2012-09-14 20:33:21 +00:00
|
|
|
|
2020-06-25 04:10:04 +00:00
|
|
|
U_BOOT_DRIVER(sandbox_serial) = {
|
|
|
|
.name = "sandbox_serial",
|
2014-09-04 22:27:27 +00:00
|
|
|
.id = UCLASS_SERIAL,
|
|
|
|
.of_match = sandbox_serial_ids,
|
2014-09-04 22:27:28 +00:00
|
|
|
.ofdata_to_platdata = sandbox_serial_ofdata_to_platdata,
|
|
|
|
.platdata_auto_alloc_size = sizeof(struct sandbox_serial_platdata),
|
|
|
|
.priv_auto_alloc_size = sizeof(struct sandbox_serial_priv),
|
2014-09-04 22:27:27 +00:00
|
|
|
.probe = sandbox_serial_probe,
|
2014-09-04 22:27:28 +00:00
|
|
|
.remove = sandbox_serial_remove,
|
2014-09-04 22:27:27 +00:00
|
|
|
.ops = &sandbox_serial_ops,
|
|
|
|
.flags = DM_FLAG_PRE_RELOC,
|
|
|
|
};
|
|
|
|
|
2020-10-03 17:31:23 +00:00
|
|
|
#if !CONFIG_IS_ENABLED(OF_PLATDATA)
|
2014-09-04 22:27:28 +00:00
|
|
|
static const struct sandbox_serial_platdata platdata_non_fdt = {
|
|
|
|
.colour = -1,
|
|
|
|
};
|
|
|
|
|
2014-09-04 22:27:27 +00:00
|
|
|
U_BOOT_DEVICE(serial_sandbox_non_fdt) = {
|
2020-06-25 04:10:04 +00:00
|
|
|
.name = "sandbox_serial",
|
2014-09-04 22:27:28 +00:00
|
|
|
.platdata = &platdata_non_fdt,
|
2014-09-04 22:27:27 +00:00
|
|
|
};
|
2020-10-03 17:31:23 +00:00
|
|
|
#endif
|
|
|
|
|
2018-10-01 17:55:16 +00:00
|
|
|
#endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
|