blob: a6b7d40d962c6b0195b421f5d9f484dbe69217ab [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass83510762016-01-18 19:52:17 -07002/*
3 * Copyright (c) 2015 Google, Inc
4 * (C) Copyright 2001-2015
5 * DENX Software Engineering -- wd@denx.de
6 * Compulab Ltd - http://compulab.co.il/
7 * Bernecker & Rainer Industrieelektronik GmbH - http://www.br-automation.com
Simon Glass83510762016-01-18 19:52:17 -07008 */
9
10#include <common.h>
Simon Glass09140112020-05-10 11:40:03 -060011#include <command.h>
Simon Glass8b763df2020-07-02 21:12:14 -060012#include <console.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
Simon Glass83510762016-01-18 19:52:17 -070014#include <dm.h>
15#include <video.h>
16#include <video_console.h>
Heinrich Schuchardt5fba5322018-03-02 20:50:17 +010017#include <video_font.h> /* Bitmap font for code page 437 */
Simon Glass8b763df2020-07-02 21:12:14 -060018#include <linux/ctype.h>
Simon Glass83510762016-01-18 19:52:17 -070019
Heinrich Schuchardt5c30fbb2018-02-08 21:47:11 +010020/*
21 * Structure to describe a console color
22 */
23struct vid_rgb {
24 u32 r;
25 u32 g;
26 u32 b;
27};
28
Simon Glass83510762016-01-18 19:52:17 -070029/* By default we scroll by a single line */
30#ifndef CONFIG_CONSOLE_SCROLL_LINES
31#define CONFIG_CONSOLE_SCROLL_LINES 1
32#endif
33
34int vidconsole_putc_xy(struct udevice *dev, uint x, uint y, char ch)
35{
36 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
37
38 if (!ops->putc_xy)
39 return -ENOSYS;
40 return ops->putc_xy(dev, x, y, ch);
41}
42
43int vidconsole_move_rows(struct udevice *dev, uint rowdst, uint rowsrc,
44 uint count)
45{
46 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
47
48 if (!ops->move_rows)
49 return -ENOSYS;
50 return ops->move_rows(dev, rowdst, rowsrc, count);
51}
52
53int vidconsole_set_row(struct udevice *dev, uint row, int clr)
54{
55 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
56
57 if (!ops->set_row)
58 return -ENOSYS;
59 return ops->set_row(dev, row, clr);
60}
61
Simon Glass58c733a2016-01-14 18:10:40 -070062static int vidconsole_entry_start(struct udevice *dev)
63{
64 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
65
66 if (!ops->entry_start)
67 return -ENOSYS;
68 return ops->entry_start(dev);
69}
70
Simon Glass83510762016-01-18 19:52:17 -070071/* Move backwards one space */
Simon Glass7b9f7e42016-01-14 18:10:41 -070072static int vidconsole_back(struct udevice *dev)
Simon Glass83510762016-01-18 19:52:17 -070073{
74 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
Simon Glass7b9f7e42016-01-14 18:10:41 -070075 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
76 int ret;
77
78 if (ops->backspace) {
79 ret = ops->backspace(dev);
80 if (ret != -ENOSYS)
81 return ret;
82 }
Simon Glass83510762016-01-18 19:52:17 -070083
Simon Glassf2661782016-01-14 18:10:37 -070084 priv->xcur_frac -= VID_TO_POS(priv->x_charsize);
Simon Glassc5b77d02016-01-14 18:10:39 -070085 if (priv->xcur_frac < priv->xstart_frac) {
Simon Glassf2661782016-01-14 18:10:37 -070086 priv->xcur_frac = (priv->cols - 1) *
87 VID_TO_POS(priv->x_charsize);
88 priv->ycur -= priv->y_charsize;
89 if (priv->ycur < 0)
90 priv->ycur = 0;
Simon Glass83510762016-01-18 19:52:17 -070091 }
Simon Glass55d39912018-10-01 11:55:14 -060092 video_sync(dev->parent, false);
Simon Glass7b9f7e42016-01-14 18:10:41 -070093
94 return 0;
Simon Glass83510762016-01-18 19:52:17 -070095}
96
97/* Move to a newline, scrolling the display if necessary */
98static void vidconsole_newline(struct udevice *dev)
99{
100 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
101 struct udevice *vid_dev = dev->parent;
102 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
103 const int rows = CONFIG_CONSOLE_SCROLL_LINES;
104 int i;
105
Simon Glassc5b77d02016-01-14 18:10:39 -0700106 priv->xcur_frac = priv->xstart_frac;
Simon Glassf2661782016-01-14 18:10:37 -0700107 priv->ycur += priv->y_charsize;
Simon Glass83510762016-01-18 19:52:17 -0700108
109 /* Check if we need to scroll the terminal */
Simon Glassf2661782016-01-14 18:10:37 -0700110 if ((priv->ycur + priv->y_charsize) / priv->y_charsize > priv->rows) {
Simon Glass83510762016-01-18 19:52:17 -0700111 vidconsole_move_rows(dev, 0, rows, priv->rows - rows);
112 for (i = 0; i < rows; i++)
113 vidconsole_set_row(dev, priv->rows - i - 1,
114 vid_priv->colour_bg);
Simon Glassf2661782016-01-14 18:10:37 -0700115 priv->ycur -= rows * priv->y_charsize;
Simon Glass83510762016-01-18 19:52:17 -0700116 }
Simon Glass58c733a2016-01-14 18:10:40 -0700117 priv->last_ch = 0;
118
Simon Glass55d39912018-10-01 11:55:14 -0600119 video_sync(dev->parent, false);
Simon Glass83510762016-01-18 19:52:17 -0700120}
121
Heinrich Schuchardt5c30fbb2018-02-08 21:47:11 +0100122static const struct vid_rgb colors[VID_COLOR_COUNT] = {
Rob Clark703d8852017-09-13 18:12:22 -0400123 { 0x00, 0x00, 0x00 }, /* black */
Heinrich Schuchardt9ffa4d12018-02-08 21:47:12 +0100124 { 0xc0, 0x00, 0x00 }, /* red */
125 { 0x00, 0xc0, 0x00 }, /* green */
126 { 0xc0, 0x60, 0x00 }, /* brown */
127 { 0x00, 0x00, 0xc0 }, /* blue */
128 { 0xc0, 0x00, 0xc0 }, /* magenta */
129 { 0x00, 0xc0, 0xc0 }, /* cyan */
130 { 0xc0, 0xc0, 0xc0 }, /* light gray */
131 { 0x80, 0x80, 0x80 }, /* gray */
132 { 0xff, 0x00, 0x00 }, /* bright red */
133 { 0x00, 0xff, 0x00 }, /* bright green */
Rob Clark703d8852017-09-13 18:12:22 -0400134 { 0xff, 0xff, 0x00 }, /* yellow */
Heinrich Schuchardt9ffa4d12018-02-08 21:47:12 +0100135 { 0x00, 0x00, 0xff }, /* bright blue */
136 { 0xff, 0x00, 0xff }, /* bright magenta */
137 { 0x00, 0xff, 0xff }, /* bright cyan */
Rob Clark703d8852017-09-13 18:12:22 -0400138 { 0xff, 0xff, 0xff }, /* white */
139};
140
Heinrich Schuchardt5c30fbb2018-02-08 21:47:11 +0100141u32 vid_console_color(struct video_priv *priv, unsigned int idx)
Rob Clark703d8852017-09-13 18:12:22 -0400142{
143 switch (priv->bpix) {
144 case VIDEO_BPP16:
Simon Glass775d3322019-12-20 18:10:36 -0700145 if (CONFIG_IS_ENABLED(VIDEO_BPP16)) {
146 return ((colors[idx].r >> 3) << 11) |
147 ((colors[idx].g >> 2) << 5) |
148 ((colors[idx].b >> 3) << 0);
149 }
Anatolij Gustschinab6ea932020-01-06 23:00:38 +0100150 break;
Rob Clark703d8852017-09-13 18:12:22 -0400151 case VIDEO_BPP32:
Simon Glass775d3322019-12-20 18:10:36 -0700152 if (CONFIG_IS_ENABLED(VIDEO_BPP32)) {
153 return (colors[idx].r << 16) |
154 (colors[idx].g << 8) |
155 (colors[idx].b << 0);
156 }
Anatolij Gustschinab6ea932020-01-06 23:00:38 +0100157 break;
Rob Clark703d8852017-09-13 18:12:22 -0400158 default:
Anatolij Gustschinab6ea932020-01-06 23:00:38 +0100159 break;
Rob Clark703d8852017-09-13 18:12:22 -0400160 }
Anatolij Gustschinab6ea932020-01-06 23:00:38 +0100161
162 /*
163 * For unknown bit arrangements just support
164 * black and white.
165 */
166 if (idx)
167 return 0xffffff; /* white */
168
169 return 0x000000; /* black */
Rob Clark703d8852017-09-13 18:12:22 -0400170}
171
Rob Clarka085aa12017-09-13 18:12:21 -0400172static char *parsenum(char *s, int *num)
173{
174 char *end;
175 *num = simple_strtol(s, &end, 10);
176 return end;
177}
178
Heinrich Schuchardt662f3812018-09-19 21:31:48 +0200179/**
180 * set_cursor_position() - set cursor position
181 *
182 * @priv: private data of the video console
183 * @row: new row
184 * @col: new column
185 */
186static void set_cursor_position(struct vidconsole_priv *priv, int row, int col)
187{
188 /*
189 * Ensure we stay in the bounds of the screen.
190 */
191 if (row >= priv->rows)
192 row = priv->rows - 1;
193 if (col >= priv->cols)
194 col = priv->cols - 1;
195
196 priv->ycur = row * priv->y_charsize;
197 priv->xcur_frac = priv->xstart_frac +
198 VID_TO_POS(col * priv->x_charsize);
199}
200
201/**
202 * get_cursor_position() - get cursor position
203 *
204 * @priv: private data of the video console
205 * @row: row
206 * @col: column
207 */
208static void get_cursor_position(struct vidconsole_priv *priv,
209 int *row, int *col)
210{
211 *row = priv->ycur / priv->y_charsize;
212 *col = VID_TO_PIXEL(priv->xcur_frac - priv->xstart_frac) /
213 priv->x_charsize;
214}
215
Rob Clarka085aa12017-09-13 18:12:21 -0400216/*
217 * Process a character while accumulating an escape string. Chars are
218 * accumulated into escape_buf until the end of escape sequence is
219 * found, at which point the sequence is parsed and processed.
220 */
221static void vidconsole_escape_char(struct udevice *dev, char ch)
222{
223 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
224
225 if (!IS_ENABLED(CONFIG_VIDEO_ANSI))
226 goto error;
227
228 /* Sanity checking for bogus ESC sequences: */
229 if (priv->escape_len >= sizeof(priv->escape_buf))
230 goto error;
Heinrich Schuchardt662f3812018-09-19 21:31:48 +0200231 if (priv->escape_len == 0) {
232 switch (ch) {
233 case '7':
234 /* Save cursor position */
235 get_cursor_position(priv, &priv->row_saved,
236 &priv->col_saved);
237 priv->escape = 0;
238
239 return;
240 case '8': {
241 /* Restore cursor position */
242 int row = priv->row_saved;
243 int col = priv->col_saved;
244
245 set_cursor_position(priv, row, col);
246 priv->escape = 0;
247 return;
248 }
249 case '[':
250 break;
251 default:
252 goto error;
253 }
254 }
Rob Clarka085aa12017-09-13 18:12:21 -0400255
256 priv->escape_buf[priv->escape_len++] = ch;
257
258 /*
259 * Escape sequences are terminated by a letter, so keep
260 * accumulating until we get one:
261 */
262 if (!isalpha(ch))
263 return;
264
265 /*
266 * clear escape mode first, otherwise things will get highly
267 * surprising if you hit any debug prints that come back to
268 * this console.
269 */
270 priv->escape = 0;
271
272 switch (ch) {
Andre Przywara29c158b2019-03-23 01:29:57 +0000273 case 'A':
274 case 'B':
275 case 'C':
276 case 'D':
277 case 'E':
278 case 'F': {
279 int row, col, num;
280 char *s = priv->escape_buf;
281
282 /*
283 * Cursor up/down: [%dA, [%dB, [%dE, [%dF
284 * Cursor left/right: [%dD, [%dC
285 */
286 s++; /* [ */
287 s = parsenum(s, &num);
288 if (num == 0) /* No digit in sequence ... */
289 num = 1; /* ... means "move by 1". */
290
291 get_cursor_position(priv, &row, &col);
292 if (ch == 'A' || ch == 'F')
293 row -= num;
294 if (ch == 'C')
295 col += num;
296 if (ch == 'D')
297 col -= num;
298 if (ch == 'B' || ch == 'E')
299 row += num;
300 if (ch == 'E' || ch == 'F')
301 col = 0;
302 if (col < 0)
303 col = 0;
304 if (row < 0)
305 row = 0;
306 /* Right and bottom overflows are handled in the callee. */
307 set_cursor_position(priv, row, col);
308 break;
309 }
Rob Clarka085aa12017-09-13 18:12:21 -0400310 case 'H':
311 case 'f': {
312 int row, col;
313 char *s = priv->escape_buf;
314
315 /*
316 * Set cursor position: [%d;%df or [%d;%dH
317 */
318 s++; /* [ */
319 s = parsenum(s, &row);
320 s++; /* ; */
321 s = parsenum(s, &col);
322
Heinrich Schuchardt118f0202018-11-10 19:55:48 +0100323 /*
324 * Video origin is [0, 0], terminal origin is [1, 1].
325 */
326 if (row)
327 --row;
328 if (col)
329 --col;
330
Heinrich Schuchardt662f3812018-09-19 21:31:48 +0200331 set_cursor_position(priv, row, col);
Rob Clarka085aa12017-09-13 18:12:21 -0400332
333 break;
334 }
335 case 'J': {
336 int mode;
337
338 /*
339 * Clear part/all screen:
340 * [J or [0J - clear screen from cursor down
341 * [1J - clear screen from cursor up
342 * [2J - clear entire screen
343 *
344 * TODO we really only handle entire-screen case, others
345 * probably require some additions to video-uclass (and
346 * are not really needed yet by efi_console)
347 */
348 parsenum(priv->escape_buf + 1, &mode);
349
350 if (mode == 2) {
351 video_clear(dev->parent);
Simon Glass55d39912018-10-01 11:55:14 -0600352 video_sync(dev->parent, false);
Rob Clarka085aa12017-09-13 18:12:21 -0400353 priv->ycur = 0;
354 priv->xcur_frac = priv->xstart_frac;
355 } else {
356 debug("unsupported clear mode: %d\n", mode);
357 }
358 break;
359 }
Andre Przywara44222942019-03-23 01:29:58 +0000360 case 'K': {
361 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
362 int mode;
363
364 /*
365 * Clear (parts of) current line
366 * [0K - clear line to end
367 * [2K - clear entire line
368 */
369 parsenum(priv->escape_buf + 1, &mode);
370
371 if (mode == 2) {
372 int row, col;
373
374 get_cursor_position(priv, &row, &col);
375 vidconsole_set_row(dev, row, vid_priv->colour_bg);
376 }
377 break;
378 }
Rob Clark703d8852017-09-13 18:12:22 -0400379 case 'm': {
380 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
381 char *s = priv->escape_buf;
382 char *end = &priv->escape_buf[priv->escape_len];
383
384 /*
385 * Set graphics mode: [%d;...;%dm
386 *
387 * Currently only supports the color attributes:
388 *
389 * Foreground Colors:
390 *
391 * 30 Black
392 * 31 Red
393 * 32 Green
394 * 33 Yellow
395 * 34 Blue
396 * 35 Magenta
397 * 36 Cyan
398 * 37 White
399 *
400 * Background Colors:
401 *
402 * 40 Black
403 * 41 Red
404 * 42 Green
405 * 43 Yellow
406 * 44 Blue
407 * 45 Magenta
408 * 46 Cyan
409 * 47 White
410 */
411
412 s++; /* [ */
413 while (s < end) {
414 int val;
415
416 s = parsenum(s, &val);
417 s++;
418
419 switch (val) {
Heinrich Schuchardt9ffa4d12018-02-08 21:47:12 +0100420 case 0:
421 /* all attributes off */
Simon Glassb9f210a2018-11-06 15:21:36 -0700422 video_set_default_colors(dev->parent, false);
Heinrich Schuchardt9ffa4d12018-02-08 21:47:12 +0100423 break;
424 case 1:
425 /* bold */
426 vid_priv->fg_col_idx |= 8;
427 vid_priv->colour_fg = vid_console_color(
428 vid_priv, vid_priv->fg_col_idx);
429 break;
Andre Przywaraeabb0722019-03-23 01:29:56 +0000430 case 7:
431 /* reverse video */
432 vid_priv->colour_fg = vid_console_color(
433 vid_priv, vid_priv->bg_col_idx);
434 vid_priv->colour_bg = vid_console_color(
435 vid_priv, vid_priv->fg_col_idx);
436 break;
Rob Clark703d8852017-09-13 18:12:22 -0400437 case 30 ... 37:
Heinrich Schuchardt5c30fbb2018-02-08 21:47:11 +0100438 /* foreground color */
Heinrich Schuchardt9ffa4d12018-02-08 21:47:12 +0100439 vid_priv->fg_col_idx &= ~7;
440 vid_priv->fg_col_idx |= val - 30;
Heinrich Schuchardt5c30fbb2018-02-08 21:47:11 +0100441 vid_priv->colour_fg = vid_console_color(
Heinrich Schuchardt9ffa4d12018-02-08 21:47:12 +0100442 vid_priv, vid_priv->fg_col_idx);
Rob Clark703d8852017-09-13 18:12:22 -0400443 break;
444 case 40 ... 47:
Andre Przywaraeabb0722019-03-23 01:29:56 +0000445 /* background color, also mask the bold bit */
446 vid_priv->bg_col_idx &= ~0xf;
447 vid_priv->bg_col_idx |= val - 40;
Heinrich Schuchardt5c30fbb2018-02-08 21:47:11 +0100448 vid_priv->colour_bg = vid_console_color(
Andre Przywaraeabb0722019-03-23 01:29:56 +0000449 vid_priv, vid_priv->bg_col_idx);
Rob Clark703d8852017-09-13 18:12:22 -0400450 break;
451 default:
Heinrich Schuchardt5c30fbb2018-02-08 21:47:11 +0100452 /* ignore unsupported SGR parameter */
Rob Clark703d8852017-09-13 18:12:22 -0400453 break;
454 }
455 }
456
457 break;
458 }
Rob Clarka085aa12017-09-13 18:12:21 -0400459 default:
460 debug("unrecognized escape sequence: %*s\n",
461 priv->escape_len, priv->escape_buf);
462 }
463
464 return;
465
466error:
467 /* something went wrong, just revert to normal mode: */
468 priv->escape = 0;
469}
470
Andre Przywara7035ec32019-03-23 01:29:59 +0000471/* Put that actual character on the screen (using the CP437 code page). */
472static int vidconsole_output_glyph(struct udevice *dev, char ch)
473{
474 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
475 int ret;
476
477 /*
478 * Failure of this function normally indicates an unsupported
479 * colour depth. Check this and return an error to help with
480 * diagnosis.
481 */
482 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
483 if (ret == -EAGAIN) {
484 vidconsole_newline(dev);
485 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
486 }
487 if (ret < 0)
488 return ret;
489 priv->xcur_frac += ret;
490 priv->last_ch = ch;
491 if (priv->xcur_frac >= priv->xsize_frac)
492 vidconsole_newline(dev);
493
494 return 0;
495}
496
Simon Glass83510762016-01-18 19:52:17 -0700497int vidconsole_put_char(struct udevice *dev, char ch)
498{
499 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
500 int ret;
501
Rob Clarka085aa12017-09-13 18:12:21 -0400502 if (priv->escape) {
503 vidconsole_escape_char(dev, ch);
504 return 0;
505 }
506
Simon Glass83510762016-01-18 19:52:17 -0700507 switch (ch) {
Rob Clarka085aa12017-09-13 18:12:21 -0400508 case '\x1b':
509 priv->escape_len = 0;
510 priv->escape = 1;
511 break;
Simon Glass5508f102016-01-14 18:10:38 -0700512 case '\a':
513 /* beep */
514 break;
Simon Glass83510762016-01-18 19:52:17 -0700515 case '\r':
Simon Glassc5b77d02016-01-14 18:10:39 -0700516 priv->xcur_frac = priv->xstart_frac;
Simon Glass83510762016-01-18 19:52:17 -0700517 break;
518 case '\n':
519 vidconsole_newline(dev);
Simon Glass58c733a2016-01-14 18:10:40 -0700520 vidconsole_entry_start(dev);
Simon Glass83510762016-01-18 19:52:17 -0700521 break;
522 case '\t': /* Tab (8 chars alignment) */
Simon Glassf2661782016-01-14 18:10:37 -0700523 priv->xcur_frac = ((priv->xcur_frac / priv->tab_width_frac)
524 + 1) * priv->tab_width_frac;
Simon Glass83510762016-01-18 19:52:17 -0700525
Simon Glassf2661782016-01-14 18:10:37 -0700526 if (priv->xcur_frac >= priv->xsize_frac)
Simon Glass83510762016-01-18 19:52:17 -0700527 vidconsole_newline(dev);
528 break;
529 case '\b':
530 vidconsole_back(dev);
Simon Glass58c733a2016-01-14 18:10:40 -0700531 priv->last_ch = 0;
Simon Glass83510762016-01-18 19:52:17 -0700532 break;
533 default:
Andre Przywara7035ec32019-03-23 01:29:59 +0000534 ret = vidconsole_output_glyph(dev, ch);
Simon Glassf2661782016-01-14 18:10:37 -0700535 if (ret < 0)
Simon Glass83510762016-01-18 19:52:17 -0700536 return ret;
Simon Glass83510762016-01-18 19:52:17 -0700537 break;
538 }
539
540 return 0;
541}
542
Marek Vasute63168a2019-05-17 20:22:31 +0200543int vidconsole_put_string(struct udevice *dev, const char *str)
544{
545 const char *s;
546 int ret;
547
548 for (s = str; *s; s++) {
549 ret = vidconsole_put_char(dev, *s);
550 if (ret)
551 return ret;
552 }
553
554 return 0;
555}
556
Simon Glass83510762016-01-18 19:52:17 -0700557static void vidconsole_putc(struct stdio_dev *sdev, const char ch)
558{
559 struct udevice *dev = sdev->priv;
Simon Glass8b763df2020-07-02 21:12:14 -0600560 int ret;
Simon Glass83510762016-01-18 19:52:17 -0700561
Simon Glass8b763df2020-07-02 21:12:14 -0600562 ret = vidconsole_put_char(dev, ch);
563 if (ret) {
564#ifdef DEBUG
565 console_puts_select_stderr(true, "[vc err: putc]");
566#endif
567 }
Simon Glass55d39912018-10-01 11:55:14 -0600568 video_sync(dev->parent, false);
Simon Glass83510762016-01-18 19:52:17 -0700569}
570
571static void vidconsole_puts(struct stdio_dev *sdev, const char *s)
572{
573 struct udevice *dev = sdev->priv;
Simon Glass8b763df2020-07-02 21:12:14 -0600574 int ret;
Simon Glass83510762016-01-18 19:52:17 -0700575
Simon Glass8b763df2020-07-02 21:12:14 -0600576 ret = vidconsole_put_string(dev, s);
577 if (ret) {
578#ifdef DEBUG
579 char str[30];
580
581 snprintf(str, sizeof(str), "[vc err: puts %d]", ret);
582 console_puts_select_stderr(true, str);
583#endif
584 }
Simon Glass55d39912018-10-01 11:55:14 -0600585 video_sync(dev->parent, false);
Simon Glass83510762016-01-18 19:52:17 -0700586}
587
588/* Set up the number of rows and colours (rotated drivers override this) */
589static int vidconsole_pre_probe(struct udevice *dev)
590{
591 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
592 struct udevice *vid = dev->parent;
593 struct video_priv *vid_priv = dev_get_uclass_priv(vid);
594
Simon Glassf2661782016-01-14 18:10:37 -0700595 priv->xsize_frac = VID_TO_POS(vid_priv->xsize);
Simon Glass83510762016-01-18 19:52:17 -0700596
597 return 0;
598}
599
600/* Register the device with stdio */
601static int vidconsole_post_probe(struct udevice *dev)
602{
603 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
604 struct stdio_dev *sdev = &priv->sdev;
Simon Glass83510762016-01-18 19:52:17 -0700605
Simon Glassf2661782016-01-14 18:10:37 -0700606 if (!priv->tab_width_frac)
607 priv->tab_width_frac = VID_TO_POS(priv->x_charsize) * 8;
608
Simon Glassf1a12472016-01-21 19:44:51 -0700609 if (dev->seq) {
610 snprintf(sdev->name, sizeof(sdev->name), "vidconsole%d",
611 dev->seq);
612 } else {
613 strcpy(sdev->name, "vidconsole");
614 }
Simon Glassf2661782016-01-14 18:10:37 -0700615
Simon Glass83510762016-01-18 19:52:17 -0700616 sdev->flags = DEV_FLAGS_OUTPUT;
617 sdev->putc = vidconsole_putc;
618 sdev->puts = vidconsole_puts;
619 sdev->priv = dev;
Simon Glass83510762016-01-18 19:52:17 -0700620
Masahiro Yamada720873b2016-09-06 22:17:33 +0900621 return stdio_register(sdev);
Simon Glass83510762016-01-18 19:52:17 -0700622}
623
624UCLASS_DRIVER(vidconsole) = {
625 .id = UCLASS_VIDEO_CONSOLE,
626 .name = "vidconsole0",
627 .pre_probe = vidconsole_pre_probe,
628 .post_probe = vidconsole_post_probe,
Simon Glass41575d82020-12-03 16:55:17 -0700629 .per_device_auto = sizeof(struct vidconsole_priv),
Simon Glass83510762016-01-18 19:52:17 -0700630};
631
Simon Glass8c0b5d22020-07-02 21:12:23 -0600632#ifdef CONFIG_VIDEO_COPY
633int vidconsole_sync_copy(struct udevice *dev, void *from, void *to)
634{
635 struct udevice *vid = dev_get_parent(dev);
636
637 return video_sync_copy(vid, from, to);
638}
639
640int vidconsole_memmove(struct udevice *dev, void *dst, const void *src,
641 int size)
642{
643 memmove(dst, src, size);
644 return vidconsole_sync_copy(dev, dst, dst + size);
645}
646#endif
647
Anatolij Gustschin39b95552020-05-25 21:47:19 +0200648#if CONFIG_IS_ENABLED(CMD_VIDCONSOLE)
Simon Glass83510762016-01-18 19:52:17 -0700649void vidconsole_position_cursor(struct udevice *dev, unsigned col, unsigned row)
650{
651 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
Simon Glassf2661782016-01-14 18:10:37 -0700652 struct udevice *vid_dev = dev->parent;
653 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
Simon Glass83510762016-01-18 19:52:17 -0700654
Simon Glass9949ee82018-10-01 12:22:47 -0600655 col *= priv->x_charsize;
656 row *= priv->y_charsize;
Simon Glassf2661782016-01-14 18:10:37 -0700657 priv->xcur_frac = VID_TO_POS(min_t(short, col, vid_priv->xsize - 1));
Ye Li8cee2002020-06-10 02:52:21 -0700658 priv->xstart_frac = priv->xcur_frac;
Simon Glassf2661782016-01-14 18:10:37 -0700659 priv->ycur = min_t(short, row, vid_priv->ysize - 1);
Simon Glass83510762016-01-18 19:52:17 -0700660}
661
Simon Glass09140112020-05-10 11:40:03 -0600662static int do_video_setcursor(struct cmd_tbl *cmdtp, int flag, int argc,
Simon Glass83510762016-01-18 19:52:17 -0700663 char *const argv[])
664{
665 unsigned int col, row;
666 struct udevice *dev;
667
668 if (argc != 3)
669 return CMD_RET_USAGE;
670
Simon Glass3f603cb2016-02-11 13:23:26 -0700671 if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev))
Simon Glass83510762016-01-18 19:52:17 -0700672 return CMD_RET_FAILURE;
673 col = simple_strtoul(argv[1], NULL, 10);
674 row = simple_strtoul(argv[2], NULL, 10);
675 vidconsole_position_cursor(dev, col, row);
676
677 return 0;
678}
679
Simon Glass09140112020-05-10 11:40:03 -0600680static int do_video_puts(struct cmd_tbl *cmdtp, int flag, int argc,
Simon Glass83510762016-01-18 19:52:17 -0700681 char *const argv[])
682{
683 struct udevice *dev;
684 const char *s;
685
686 if (argc != 2)
687 return CMD_RET_USAGE;
688
Simon Glass3f603cb2016-02-11 13:23:26 -0700689 if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev))
Simon Glass83510762016-01-18 19:52:17 -0700690 return CMD_RET_FAILURE;
691 for (s = argv[1]; *s; s++)
692 vidconsole_put_char(dev, *s);
693
Simon Glass55d39912018-10-01 11:55:14 -0600694 video_sync(dev->parent, false);
Rob Clark889808d2017-09-13 18:12:20 -0400695
Simon Glass83510762016-01-18 19:52:17 -0700696 return 0;
697}
698
699U_BOOT_CMD(
700 setcurs, 3, 1, do_video_setcursor,
701 "set cursor position within screen",
702 " <col> <row> in character"
703);
704
705U_BOOT_CMD(
706 lcdputs, 2, 1, do_video_puts,
707 "print string on video framebuffer",
708 " <string>"
709);
Anatolij Gustschin39b95552020-05-25 21:47:19 +0200710#endif /* CONFIG_IS_ENABLED(CMD_VIDCONSOLE) */