blob: c27b5b8fe52b4420e2b5b6160d193a758eff8653 [file] [log] [blame]
Simon Glass7accb6e2011-10-03 19:26:46 +00001/*
2 * Copyright (c) 2011 The Chromium OS Authors.
Simon Glass7accb6e2011-10-03 19:26:46 +00003 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Simon Glass7accb6e2011-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 Glass7d95f2a2014-02-27 13:26:19 -070014#include <lcd.h>
Simon Glass7accb6e2011-10-03 19:26:46 +000015#include <os.h>
Marek Vasutcef46b72012-09-14 22:33:21 +020016#include <serial.h>
17#include <linux/compiler.h>
Simon Glass7accb6e2011-10-03 19:26:46 +000018
Taylor Hutte1015502013-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 Vasutcef46b72012-09-14 22:33:21 +020032static int sandbox_serial_init(void)
Simon Glass7accb6e2011-10-03 19:26:46 +000033{
Mike Frysingerab06a752011-10-26 00:21:29 +000034 os_tty_raw(0);
Simon Glass7accb6e2011-10-03 19:26:46 +000035 return 0;
36}
37
Marek Vasutcef46b72012-09-14 22:33:21 +020038static void sandbox_serial_setbrg(void)
Simon Glass7accb6e2011-10-03 19:26:46 +000039{
40}
41
Marek Vasutcef46b72012-09-14 22:33:21 +020042static void sandbox_serial_putc(const char ch)
Simon Glass7accb6e2011-10-03 19:26:46 +000043{
44 os_write(1, &ch, 1);
45}
46
Marek Vasutcef46b72012-09-14 22:33:21 +020047static void sandbox_serial_puts(const char *str)
Simon Glass7accb6e2011-10-03 19:26:46 +000048{
Mike Frysinger5778d542011-10-26 00:20:39 +000049 os_write(1, str, strlen(str));
Simon Glass7accb6e2011-10-03 19:26:46 +000050}
51
Taylor Hutte1015502013-02-24 17:33:13 +000052static unsigned int increment_buffer_index(unsigned int index)
Simon Glass7accb6e2011-10-03 19:26:46 +000053{
Taylor Hutte1015502013-02-24 17:33:13 +000054 return (index + 1) % ARRAY_SIZE(serial_buf);
Simon Glass7accb6e2011-10-03 19:26:46 +000055}
56
Marek Vasutcef46b72012-09-14 22:33:21 +020057static int sandbox_serial_tstc(void)
Simon Glass7accb6e2011-10-03 19:26:46 +000058{
Taylor Hutte1015502013-02-24 17:33:13 +000059 const unsigned int next_index =
60 increment_buffer_index(serial_buf_write);
61 ssize_t count;
62
63 os_usleep(100);
Simon Glass7d95f2a2014-02-27 13:26:19 -070064#ifdef CONFIG_LCD
65 lcd_sync();
66#endif
Taylor Hutte1015502013-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;
74}
75
76static int sandbox_serial_getc(void)
77{
78 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 Glass7accb6e2011-10-03 19:26:46 +000086}
Marek Vasutcef46b72012-09-14 22:33:21 +020087
Marek Vasutcef46b72012-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}