Merge branch 'master' of git://git.denx.de/u-boot-samsung
[oweals/u-boot.git] / drivers / serial / sandbox.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2011 The Chromium OS Authors.
4  */
5
6 /*
7  * This provide a test serial port. It provides an emulated serial port where
8  * a test program and read out the serial output and inject serial input for
9  * U-Boot.
10  */
11
12 #include <common.h>
13 #include <console.h>
14 #include <dm.h>
15 #include <fdtdec.h>
16 #include <lcd.h>
17 #include <os.h>
18 #include <serial.h>
19 #include <video.h>
20 #include <linux/compiler.h>
21 #include <asm/state.h>
22
23 DECLARE_GLOBAL_DATA_PTR;
24
25 /*
26  *
27  *   serial_buf: A buffer that holds keyboard characters for the
28  *               Sandbox U-Boot.
29  *
30  * invariants:
31  *   serial_buf_write            == serial_buf_read -> empty buffer
32  *   (serial_buf_write + 1) % 16 == serial_buf_read -> full buffer
33  */
34 static char serial_buf[16];
35 static unsigned int serial_buf_write;
36 static unsigned int serial_buf_read;
37
38 struct sandbox_serial_platdata {
39         int colour;     /* Text colour to use for output, -1 for none */
40 };
41
42 struct sandbox_serial_priv {
43         bool start_of_line;
44 };
45
46 /**
47  * output_ansi_colour() - Output an ANSI colour code
48  *
49  * @colour: Colour to output (0-7)
50  */
51 static void output_ansi_colour(int colour)
52 {
53         char ansi_code[] = "\x1b[1;3Xm";
54
55         ansi_code[5] = '0' + colour;
56         os_write(1, ansi_code, sizeof(ansi_code) - 1);
57 }
58
59 static void output_ansi_reset(void)
60 {
61         os_write(1, "\x1b[0m", 4);
62 }
63
64 static int sandbox_serial_probe(struct udevice *dev)
65 {
66         struct sandbox_state *state = state_get_current();
67         struct sandbox_serial_priv *priv = dev_get_priv(dev);
68
69         if (state->term_raw != STATE_TERM_COOKED)
70                 os_tty_raw(0, state->term_raw == STATE_TERM_RAW_WITH_SIGS);
71         priv->start_of_line = 0;
72
73         if (state->term_raw != STATE_TERM_RAW)
74                 disable_ctrlc(1);
75
76         return 0;
77 }
78
79 static int sandbox_serial_remove(struct udevice *dev)
80 {
81         struct sandbox_serial_platdata *plat = dev->platdata;
82
83         if (plat->colour != -1)
84                 output_ansi_reset();
85
86         return 0;
87 }
88
89 static int sandbox_serial_putc(struct udevice *dev, const char ch)
90 {
91         struct sandbox_serial_priv *priv = dev_get_priv(dev);
92         struct sandbox_serial_platdata *plat = dev->platdata;
93
94         if (priv->start_of_line && plat->colour != -1) {
95                 priv->start_of_line = false;
96                 output_ansi_colour(plat->colour);
97         }
98
99         os_write(1, &ch, 1);
100         if (ch == '\n')
101                 priv->start_of_line = true;
102
103         return 0;
104 }
105
106 static unsigned int increment_buffer_index(unsigned int index)
107 {
108         return (index + 1) % ARRAY_SIZE(serial_buf);
109 }
110
111 static int sandbox_serial_pending(struct udevice *dev, bool input)
112 {
113         const unsigned int next_index =
114                 increment_buffer_index(serial_buf_write);
115         ssize_t count;
116
117         if (!input)
118                 return 0;
119
120         os_usleep(100);
121 #ifndef CONFIG_SPL_BUILD
122         video_sync_all();
123 #endif
124         if (next_index == serial_buf_read)
125                 return 1;       /* buffer full */
126
127         count = os_read_no_block(0, &serial_buf[serial_buf_write], 1);
128         if (count == 1)
129                 serial_buf_write = next_index;
130
131         return serial_buf_write != serial_buf_read;
132 }
133
134 static int sandbox_serial_getc(struct udevice *dev)
135 {
136         int result;
137
138         if (!sandbox_serial_pending(dev, true))
139                 return -EAGAIN; /* buffer empty */
140
141         result = serial_buf[serial_buf_read];
142         serial_buf_read = increment_buffer_index(serial_buf_read);
143         return result;
144 }
145
146 static const char * const ansi_colour[] = {
147         "black", "red", "green", "yellow", "blue", "megenta", "cyan",
148         "white",
149 };
150
151 static int sandbox_serial_ofdata_to_platdata(struct udevice *dev)
152 {
153         struct sandbox_serial_platdata *plat = dev->platdata;
154         const char *colour;
155         int i;
156
157         plat->colour = -1;
158         colour = fdt_getprop(gd->fdt_blob, dev_of_offset(dev),
159                              "sandbox,text-colour", NULL);
160         if (colour) {
161                 for (i = 0; i < ARRAY_SIZE(ansi_colour); i++) {
162                         if (!strcmp(colour, ansi_colour[i])) {
163                                 plat->colour = i;
164                                 break;
165                         }
166                 }
167         }
168
169         return 0;
170 }
171
172 static const struct dm_serial_ops sandbox_serial_ops = {
173         .putc = sandbox_serial_putc,
174         .pending = sandbox_serial_pending,
175         .getc = sandbox_serial_getc,
176 };
177
178 static const struct udevice_id sandbox_serial_ids[] = {
179         { .compatible = "sandbox,serial" },
180         { }
181 };
182
183 U_BOOT_DRIVER(serial_sandbox) = {
184         .name   = "serial_sandbox",
185         .id     = UCLASS_SERIAL,
186         .of_match = sandbox_serial_ids,
187         .ofdata_to_platdata = sandbox_serial_ofdata_to_platdata,
188         .platdata_auto_alloc_size = sizeof(struct sandbox_serial_platdata),
189         .priv_auto_alloc_size = sizeof(struct sandbox_serial_priv),
190         .probe = sandbox_serial_probe,
191         .remove = sandbox_serial_remove,
192         .ops    = &sandbox_serial_ops,
193         .flags = DM_FLAG_PRE_RELOC,
194 };
195
196 static const struct sandbox_serial_platdata platdata_non_fdt = {
197         .colour = -1,
198 };
199
200 U_BOOT_DEVICE(serial_sandbox_non_fdt) = {
201         .name = "serial_sandbox",
202         .platdata = &platdata_non_fdt,
203 };