arm: mach-omap2: Move common image process functions out of board files
[platform/kernel/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 <os.h>
16 #include <serial.h>
17 #include <video.h>
18 #include <asm/global_data.h>
19 #include <linux/compiler.h>
20 #include <asm/serial.h>
21 #include <asm/state.h>
22
23 DECLARE_GLOBAL_DATA_PTR;
24
25 static size_t _sandbox_serial_written = 1;
26 static bool sandbox_serial_enabled = true;
27
28 size_t sandbox_serial_written(void)
29 {
30         return _sandbox_serial_written;
31 }
32
33 void sandbox_serial_endisable(bool enabled)
34 {
35         sandbox_serial_enabled = enabled;
36 }
37
38 /**
39  * output_ansi_colour() - Output an ANSI colour code
40  *
41  * @colour: Colour to output (0-7)
42  */
43 static void output_ansi_colour(int colour)
44 {
45         char ansi_code[] = "\x1b[1;3Xm";
46
47         ansi_code[5] = '0' + colour;
48         os_write(1, ansi_code, sizeof(ansi_code) - 1);
49 }
50
51 static void output_ansi_reset(void)
52 {
53         os_write(1, "\x1b[0m", 4);
54 }
55
56 static int sandbox_serial_probe(struct udevice *dev)
57 {
58         struct sandbox_state *state = state_get_current();
59         struct sandbox_serial_priv *priv = dev_get_priv(dev);
60
61         if (state->term_raw != STATE_TERM_COOKED)
62                 os_tty_raw(0, state->term_raw == STATE_TERM_RAW_WITH_SIGS);
63         priv->start_of_line = 0;
64
65         if (state->term_raw != STATE_TERM_RAW)
66                 disable_ctrlc(1);
67         membuff_init(&priv->buf, priv->serial_buf, sizeof(priv->serial_buf));
68
69         return 0;
70 }
71
72 static int sandbox_serial_remove(struct udevice *dev)
73 {
74         struct sandbox_serial_plat *plat = dev_get_plat(dev);
75
76         if (plat->colour != -1)
77                 output_ansi_reset();
78
79         return 0;
80 }
81
82 static void sandbox_print_color(struct udevice *dev)
83 {
84         struct sandbox_serial_priv *priv = dev_get_priv(dev);
85         struct sandbox_serial_plat *plat = dev_get_plat(dev);
86
87         /* With of-platdata we don't real the colour correctly, so disable it */
88         if (!CONFIG_IS_ENABLED(OF_PLATDATA) && priv->start_of_line &&
89             plat->colour != -1) {
90                 priv->start_of_line = false;
91                 output_ansi_colour(plat->colour);
92         }
93 }
94
95 static int sandbox_serial_putc(struct udevice *dev, const char ch)
96 {
97         struct sandbox_serial_priv *priv = dev_get_priv(dev);
98
99         if (ch == '\n')
100                 priv->start_of_line = true;
101
102         if (sandbox_serial_enabled) {
103                 sandbox_print_color(dev);
104                 os_write(1, &ch, 1);
105         }
106         _sandbox_serial_written += 1;
107         return 0;
108 }
109
110 static ssize_t sandbox_serial_puts(struct udevice *dev, const char *s,
111                                    size_t len)
112 {
113         struct sandbox_serial_priv *priv = dev_get_priv(dev);
114         ssize_t ret;
115
116         if (len && s[len - 1] == '\n')
117                 priv->start_of_line = true;
118
119         if (sandbox_serial_enabled) {
120                 sandbox_print_color(dev);
121                 ret = os_write(1, s, len);
122                 if (ret < 0)
123                         return ret;
124         } else {
125                 ret = len;
126         }
127         _sandbox_serial_written += ret;
128         return ret;
129 }
130
131 static int sandbox_serial_pending(struct udevice *dev, bool input)
132 {
133         struct sandbox_serial_priv *priv = dev_get_priv(dev);
134         ssize_t count;
135         char *data;
136         int avail;
137
138         if (!input)
139                 return 0;
140
141         os_usleep(100);
142         if (IS_ENABLED(CONFIG_VIDEO) && !IS_ENABLED(CONFIG_SPL_BUILD))
143                 video_sync_all();
144         avail = membuff_putraw(&priv->buf, 100, false, &data);
145         if (!avail)
146                 return 1;       /* buffer full */
147
148         count = os_read(0, data, avail);
149         if (count > 0)
150                 membuff_putraw(&priv->buf, count, true, &data);
151
152         return membuff_avail(&priv->buf);
153 }
154
155 static int sandbox_serial_getc(struct udevice *dev)
156 {
157         struct sandbox_serial_priv *priv = dev_get_priv(dev);
158
159         if (!sandbox_serial_pending(dev, true))
160                 return -EAGAIN; /* buffer empty */
161
162         return membuff_getbyte(&priv->buf);
163 }
164
165 #ifdef CONFIG_DEBUG_UART_SANDBOX
166
167 #include <debug_uart.h>
168
169 static inline void _debug_uart_init(void)
170 {
171 }
172
173 static inline void _debug_uart_putc(int ch)
174 {
175         os_putc(ch);
176 }
177
178 DEBUG_UART_FUNCS
179
180 #endif /* CONFIG_DEBUG_UART_SANDBOX */
181
182 static int sandbox_serial_getconfig(struct udevice *dev, uint *serial_config)
183 {
184         uint config = SERIAL_DEFAULT_CONFIG;
185
186         if (!serial_config)
187                 return -EINVAL;
188
189         *serial_config = config;
190
191         return 0;
192 }
193
194 static int sandbox_serial_setconfig(struct udevice *dev, uint serial_config)
195 {
196         u8 parity = SERIAL_GET_PARITY(serial_config);
197         u8 bits = SERIAL_GET_BITS(serial_config);
198         u8 stop = SERIAL_GET_STOP(serial_config);
199
200         if (bits != SERIAL_8_BITS || stop != SERIAL_ONE_STOP ||
201             parity != SERIAL_PAR_NONE)
202                 return -ENOTSUPP; /* not supported in driver*/
203
204         return 0;
205 }
206
207 static int sandbox_serial_getinfo(struct udevice *dev,
208                                   struct serial_device_info *serial_info)
209 {
210         struct serial_device_info info = {
211                 .type = SERIAL_CHIP_UNKNOWN,
212                 .addr_space = SERIAL_ADDRESS_SPACE_IO,
213                 .addr = SERIAL_DEFAULT_ADDRESS,
214                 .reg_width = 1,
215                 .reg_offset = 0,
216                 .reg_shift = 0,
217                 .clock = SERIAL_DEFAULT_CLOCK,
218         };
219
220         if (!serial_info)
221                 return -EINVAL;
222
223         *serial_info = info;
224
225         return 0;
226 }
227
228 static const char * const ansi_colour[] = {
229         "black", "red", "green", "yellow", "blue", "megenta", "cyan",
230         "white",
231 };
232
233 static int sandbox_serial_of_to_plat(struct udevice *dev)
234 {
235         struct sandbox_serial_plat *plat = dev_get_plat(dev);
236         const char *colour;
237         int i;
238
239         if (CONFIG_IS_ENABLED(OF_PLATDATA))
240                 return 0;
241         plat->colour = -1;
242         colour = dev_read_string(dev, "sandbox,text-colour");
243         if (colour) {
244                 for (i = 0; i < ARRAY_SIZE(ansi_colour); i++) {
245                         if (!strcmp(colour, ansi_colour[i])) {
246                                 plat->colour = i;
247                                 break;
248                         }
249                 }
250         }
251
252         return 0;
253 }
254
255 static const struct dm_serial_ops sandbox_serial_ops = {
256         .putc = sandbox_serial_putc,
257         .puts = sandbox_serial_puts,
258         .pending = sandbox_serial_pending,
259         .getc = sandbox_serial_getc,
260         .getconfig = sandbox_serial_getconfig,
261         .setconfig = sandbox_serial_setconfig,
262         .getinfo = sandbox_serial_getinfo,
263 };
264
265 static const struct udevice_id sandbox_serial_ids[] = {
266         { .compatible = "sandbox,serial" },
267         { }
268 };
269
270 U_BOOT_DRIVER(sandbox_serial) = {
271         .name   = "sandbox_serial",
272         .id     = UCLASS_SERIAL,
273         .of_match = sandbox_serial_ids,
274         .of_to_plat = sandbox_serial_of_to_plat,
275         .plat_auto      = sizeof(struct sandbox_serial_plat),
276         .priv_auto      = sizeof(struct sandbox_serial_priv),
277         .probe = sandbox_serial_probe,
278         .remove = sandbox_serial_remove,
279         .ops    = &sandbox_serial_ops,
280         .flags = DM_FLAG_PRE_RELOC,
281 };
282
283 #if CONFIG_IS_ENABLED(OF_REAL)
284 static const struct sandbox_serial_plat platdata_non_fdt = {
285         .colour = -1,
286 };
287
288 U_BOOT_DRVINFO(serial_sandbox_non_fdt) = {
289         .name = "sandbox_serial",
290         .plat = &platdata_non_fdt,
291 };
292 #endif