blob: c27b5b8fe52b4420e2b5b6160d193a758eff8653 [file] [log] [blame]
Simon Glass479349a2011-10-03 19:26:46 +00001/*
2 * Copyright (c) 2011 The Chromium OS Authors.
Simon Glass479349a2011-10-03 19:26:46 +00003 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Simon Glass479349a2011-10-03 19:26:46 +00005 */
6
7/*
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
10 * U-Boot.
11 */
12
13#include <common.h>
Simon Glassb9ddbf42014-02-27 13:26:19 -070014#include <lcd.h>
Simon Glass479349a2011-10-03 19:26:46 +000015#include <os.h>
Marek Vasut289c3002012-09-14 22:33:21 +020016#include <serial.h>
17#include <linux/compiler.h>
Simon Glass479349a2011-10-03 19:26:46 +000018
Taylor Hutt868aa3a2013-02-24 17:33:13 +000019/*
20 *
21 * serial_buf: A buffer that holds keyboard characters for the
22 * Sandbox U-boot.
23 *
24 * invariants:
25 * serial_buf_write == serial_buf_read -> empty buffer
26 * (serial_buf_write + 1) % 16 == serial_buf_read -> full buffer
27 */
28static char serial_buf[16];
29static unsigned int serial_buf_write;
30static unsigned int serial_buf_read;
31
Marek Vasut289c3002012-09-14 22:33:21 +020032static int sandbox_serial_init(void)
Simon Glass479349a2011-10-03 19:26:46 +000033{
Mike Frysingera5baaee2011-10-26 00:21:29 +000034 os_tty_raw(0);
Simon Glass479349a2011-10-03 19:26:46 +000035 return 0;
36}
37
Marek Vasut289c3002012-09-14 22:33:21 +020038static void sandbox_serial_setbrg(void)
Simon Glass479349a2011-10-03 19:26:46 +000039{
40}
41
Marek Vasut289c3002012-09-14 22:33:21 +020042static void sandbox_serial_putc(const char ch)
Simon Glass479349a2011-10-03 19:26:46 +000043{
44 os_write(1, &ch, 1);
45}
46
Marek Vasut289c3002012-09-14 22:33:21 +020047static void sandbox_serial_puts(const char *str)
Simon Glass479349a2011-10-03 19:26:46 +000048{
Mike Frysingerd42e0662011-10-26 00:20:39 +000049 os_write(1, str, strlen(str));
Simon Glass479349a2011-10-03 19:26:46 +000050}
51
Taylor Hutt868aa3a2013-02-24 17:33:13 +000052static unsigned int increment_buffer_index(unsigned int index)
53{
54 return (index + 1) % ARRAY_SIZE(serial_buf);
55}
56
57static int sandbox_serial_tstc(void)
Simon Glass479349a2011-10-03 19:26:46 +000058{
Taylor Hutt868aa3a2013-02-24 17:33:13 +000059 const unsigned int next_index =
60 increment_buffer_index(serial_buf_write);
Mike Frysingerd174fd92011-10-26 00:21:01 +000061 ssize_t count;
Simon Glass479349a2011-10-03 19:26:46 +000062
Taylor Hutt868aa3a2013-02-24 17:33:13 +000063 os_usleep(100);
Simon Glassb9ddbf42014-02-27 13:26:19 -070064#ifdef CONFIG_LCD
65 lcd_sync();
66#endif
Taylor Hutt868aa3a2013-02-24 17:33:13 +000067 if (next_index == serial_buf_read)
68 return 1; /* buffer full */
69
70 count = os_read_no_block(0, &serial_buf[serial_buf_write], 1);
71 if (count == 1)
72 serial_buf_write = next_index;
73 return serial_buf_write != serial_buf_read;
Simon Glass479349a2011-10-03 19:26:46 +000074}
75
Taylor Hutt868aa3a2013-02-24 17:33:13 +000076static int sandbox_serial_getc(void)
Simon Glass479349a2011-10-03 19:26:46 +000077{
Taylor Hutt868aa3a2013-02-24 17:33:13 +000078 int result;
79
80 while (!sandbox_serial_tstc())
81 ; /* buffer empty */
82
83 result = serial_buf[serial_buf_read];
84 serial_buf_read = increment_buffer_index(serial_buf_read);
85 return result;
Simon Glass479349a2011-10-03 19:26:46 +000086}
Marek Vasut289c3002012-09-14 22:33:21 +020087
Marek Vasut289c3002012-09-14 22:33:21 +020088static struct serial_device sandbox_serial_drv = {
89 .name = "sandbox_serial",
90 .start = sandbox_serial_init,
91 .stop = NULL,
92 .setbrg = sandbox_serial_setbrg,
93 .putc = sandbox_serial_putc,
94 .puts = sandbox_serial_puts,
95 .getc = sandbox_serial_getc,
96 .tstc = sandbox_serial_tstc,
97};
98
99void sandbox_serial_initialize(void)
100{
101 serial_register(&sandbox_serial_drv);
102}
103
104__weak struct serial_device *default_serial_console(void)
105{
106 return &sandbox_serial_drv;
107}