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>
|
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>
|
2020-10-31 03:38:53 +00:00
|
|
|
#include <asm/global_data.h>
|
2012-09-14 20:33:21 +00:00
|
|
|
#include <linux/compiler.h>
|
2020-12-19 17:39:53 +00:00
|
|
|
#include <asm/serial.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;
|
|
|
|
|
2014-09-04 22:27:28 +00:00
|
|
|
/**
|
|
|
|
* 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);
|
2020-11-09 03:36:50 +00:00
|
|
|
membuff_init(&priv->buf, priv->serial_buf, sizeof(priv->serial_buf));
|
2018-07-03 01:06:49 +00:00
|
|
|
|
2014-09-04 22:27:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sandbox_serial_remove(struct udevice *dev)
|
|
|
|
{
|
2020-12-23 02:30:28 +00:00
|
|
|
struct sandbox_serial_plat *plat = dev_get_plat(dev);
|
2014-09-04 22:27:28 +00:00
|
|
|
|
|
|
|
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);
|
2020-12-23 02:30:28 +00:00
|
|
|
struct sandbox_serial_plat *plat = dev_get_plat(dev);
|
2014-09-04 22:27:28 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2020-11-09 03:36:50 +00:00
|
|
|
struct sandbox_serial_priv *priv = dev_get_priv(dev);
|
2011-10-26 00:21:01 +00:00
|
|
|
ssize_t count;
|
2020-11-09 03:36:50 +00:00
|
|
|
char *data;
|
|
|
|
int avail;
|
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);
|
2020-11-09 03:36:48 +00:00
|
|
|
if (!IS_ENABLED(CONFIG_SPL_BUILD))
|
|
|
|
video_sync_all();
|
2020-11-09 03:36:50 +00:00
|
|
|
avail = membuff_putraw(&priv->buf, 100, false, &data);
|
|
|
|
if (!avail)
|
2013-02-24 17:33:13 +00:00
|
|
|
return 1; /* buffer full */
|
|
|
|
|
2020-11-09 03:36:50 +00:00
|
|
|
count = os_read(0, data, avail);
|
|
|
|
if (count > 0)
|
|
|
|
membuff_putraw(&priv->buf, count, true, &data);
|
2014-09-04 22:27:27 +00:00
|
|
|
|
2020-11-09 03:36:50 +00:00
|
|
|
return membuff_avail(&priv->buf);
|
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
|
|
|
{
|
2020-11-09 03:36:50 +00:00
|
|
|
struct sandbox_serial_priv *priv = dev_get_priv(dev);
|
2013-02-24 17:33:13 +00:00
|
|
|
|
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
|
|
|
|
2020-11-09 03:36:50 +00:00
|
|
|
return membuff_getbyte(&priv->buf);
|
2011-10-03 19:26:46 +00:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2014-09-04 22:27:28 +00:00
|
|
|
static const char * const ansi_colour[] = {
|
|
|
|
"black", "red", "green", "yellow", "blue", "megenta", "cyan",
|
|
|
|
"white",
|
|
|
|
};
|
|
|
|
|
2020-12-03 23:55:21 +00:00
|
|
|
static int sandbox_serial_of_to_plat(struct udevice *dev)
|
2014-09-04 22:27:28 +00:00
|
|
|
{
|
2020-12-23 02:30:28 +00:00
|
|
|
struct sandbox_serial_plat *plat = dev_get_plat(dev);
|
2014-09-04 22:27:28 +00:00
|
|
|
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;
|
2020-11-09 03:36:49 +00:00
|
|
|
colour = dev_read_string(dev, "sandbox,text-colour");
|
2014-09-04 22:27:28 +00:00
|
|
|
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,
|
2020-12-03 23:55:21 +00:00
|
|
|
.of_to_plat = sandbox_serial_of_to_plat,
|
2020-12-03 23:55:23 +00:00
|
|
|
.plat_auto = sizeof(struct sandbox_serial_plat),
|
2020-12-03 23:55:17 +00:00
|
|
|
.priv_auto = 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)
|
2020-12-03 23:55:23 +00:00
|
|
|
static const struct sandbox_serial_plat platdata_non_fdt = {
|
2014-09-04 22:27:28 +00:00
|
|
|
.colour = -1,
|
|
|
|
};
|
|
|
|
|
2020-12-29 03:34:54 +00:00
|
|
|
U_BOOT_DRVINFO(serial_sandbox_non_fdt) = {
|
2020-06-25 04:10:04 +00:00
|
|
|
.name = "sandbox_serial",
|
2020-12-03 23:55:18 +00:00
|
|
|
.plat = &platdata_non_fdt,
|
2014-09-04 22:27:27 +00:00
|
|
|
};
|
2020-10-03 17:31:23 +00:00
|
|
|
#endif
|