blob: a3f8c6352f83d1ffc18d52d51fa6090f52c9b2ba [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass87aae882016-01-18 19:52:19 -07002/*
3 * Copyright (c) 2015 Google, Inc
4 * (C) Copyright 2015
5 * Bernecker & Rainer Industrieelektronik GmbH - http://www.br-automation.com
Dzmitry Sankouskiaea2d2d2023-03-07 13:21:11 +03006 * (C) Copyright 2023 Dzmitry Sankouski <dsankouski@gmail.com>
Simon Glass87aae882016-01-18 19:52:19 -07007 */
8
Janne Grunau5548c362024-03-16 22:50:19 +01009#include <charset.h>
Simon Glass87aae882016-01-18 19:52:19 -070010#include <dm.h>
11#include <video.h>
12#include <video_console.h>
13#include <video_font.h> /* Get font data, width and height */
Dzmitry Sankouskiaea2d2d2023-03-07 13:21:11 +030014#include "vidconsole_internal.h"
Simon Glass87aae882016-01-18 19:52:19 -070015
16static int console_set_row_1(struct udevice *dev, uint row, int clr)
17{
18 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
Dzmitry Sankouski7fa964a2023-03-07 13:21:14 +030019 struct console_simple_priv *priv = dev_get_priv(dev);
20 struct video_fontdata *fontdata = priv->fontdata;
Simon Glass87aae882016-01-18 19:52:19 -070021 int pbytes = VNBYTES(vid_priv->bpix);
Dzmitry Sankouskiaea2d2d2023-03-07 13:21:11 +030022 void *start, *dst, *line;
Simon Glass87aae882016-01-18 19:52:19 -070023 int i, j;
Simon Glass42301da2020-07-02 21:12:26 -060024 int ret;
Simon Glass87aae882016-01-18 19:52:19 -070025
Simon Glass42301da2020-07-02 21:12:26 -060026 start = vid_priv->fb + vid_priv->line_length -
Dzmitry Sankouski7fa964a2023-03-07 13:21:14 +030027 (row + 1) * fontdata->height * pbytes;
Simon Glass42301da2020-07-02 21:12:26 -060028 line = start;
Simon Glass87aae882016-01-18 19:52:19 -070029 for (j = 0; j < vid_priv->ysize; j++) {
Dzmitry Sankouskiaea2d2d2023-03-07 13:21:11 +030030 dst = line;
Dzmitry Sankouski7fa964a2023-03-07 13:21:14 +030031 for (i = 0; i < fontdata->height; i++)
Dzmitry Sankouskiaea2d2d2023-03-07 13:21:11 +030032 fill_pixel_and_goto_next(&dst, clr, pbytes, pbytes);
Simon Glass87aae882016-01-18 19:52:19 -070033 line += vid_priv->line_length;
34 }
Simon Glass42301da2020-07-02 21:12:26 -060035 ret = vidconsole_sync_copy(dev, start, line);
36 if (ret)
37 return ret;
Simon Glass87aae882016-01-18 19:52:19 -070038
39 return 0;
40}
41
42static int console_move_rows_1(struct udevice *dev, uint rowdst, uint rowsrc,
Dzmitry Sankouskiaea2d2d2023-03-07 13:21:11 +030043 uint count)
Simon Glass87aae882016-01-18 19:52:19 -070044{
45 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
Dzmitry Sankouski7fa964a2023-03-07 13:21:14 +030046 struct console_simple_priv *priv = dev_get_priv(dev);
47 struct video_fontdata *fontdata = priv->fontdata;
Simon Glass80daa3f2020-07-02 21:12:16 -060048 int pbytes = VNBYTES(vid_priv->bpix);
Simon Glass87aae882016-01-18 19:52:19 -070049 void *dst;
50 void *src;
Simon Glass42301da2020-07-02 21:12:26 -060051 int j, ret;
Simon Glass87aae882016-01-18 19:52:19 -070052
53 dst = vid_priv->fb + vid_priv->line_length -
Dzmitry Sankouski7fa964a2023-03-07 13:21:14 +030054 (rowdst + count) * fontdata->height * pbytes;
Simon Glass87aae882016-01-18 19:52:19 -070055 src = vid_priv->fb + vid_priv->line_length -
Dzmitry Sankouski7fa964a2023-03-07 13:21:14 +030056 (rowsrc + count) * fontdata->height * pbytes;
Simon Glass87aae882016-01-18 19:52:19 -070057
58 for (j = 0; j < vid_priv->ysize; j++) {
Simon Glass42301da2020-07-02 21:12:26 -060059 ret = vidconsole_memmove(dev, dst, src,
Dzmitry Sankouski7fa964a2023-03-07 13:21:14 +030060 fontdata->height * pbytes * count);
Simon Glass42301da2020-07-02 21:12:26 -060061 if (ret)
62 return ret;
Simon Glass87aae882016-01-18 19:52:19 -070063 src += vid_priv->line_length;
64 dst += vid_priv->line_length;
65 }
66
67 return 0;
68}
69
Janne Grunau5548c362024-03-16 22:50:19 +010070static int console_putc_xy_1(struct udevice *dev, uint x_frac, uint y, int cp)
Simon Glass87aae882016-01-18 19:52:19 -070071{
Simon Glass52c10c52016-01-14 18:10:37 -070072 struct vidconsole_priv *vc_priv = dev_get_uclass_priv(dev);
Simon Glass87aae882016-01-18 19:52:19 -070073 struct udevice *vid = dev->parent;
74 struct video_priv *vid_priv = dev_get_uclass_priv(vid);
Dzmitry Sankouski7fa964a2023-03-07 13:21:14 +030075 struct console_simple_priv *priv = dev_get_priv(dev);
76 struct video_fontdata *fontdata = priv->fontdata;
Simon Glass87aae882016-01-18 19:52:19 -070077 int pbytes = VNBYTES(vid_priv->bpix);
Dzmitry Sankouskiaea2d2d2023-03-07 13:21:11 +030078 int x, linenum, ret;
Simon Glass42301da2020-07-02 21:12:26 -060079 void *start, *line;
Janne Grunau5548c362024-03-16 22:50:19 +010080 u8 ch = console_utf_to_cp437(cp);
Dzmitry Sankouski7fa964a2023-03-07 13:21:14 +030081 uchar *pfont = fontdata->video_fontdata +
Janne Grunau5548c362024-03-16 22:50:19 +010082 ch * fontdata->char_pixel_bytes;
Simon Glass87aae882016-01-18 19:52:19 -070083
Dzmitry Sankouskiaea2d2d2023-03-07 13:21:11 +030084 if (x_frac + VID_TO_POS(vc_priv->x_charsize) > vc_priv->xsize_frac)
85 return -EAGAIN;
Simon Glass80daa3f2020-07-02 21:12:16 -060086 linenum = VID_TO_PIXEL(x_frac) + 1;
87 x = y + 1;
Simon Glass42301da2020-07-02 21:12:26 -060088 start = vid_priv->fb + linenum * vid_priv->line_length - x * pbytes;
89 line = start;
Simon Glass87aae882016-01-18 19:52:19 -070090
Dzmitry Sankouski7fa964a2023-03-07 13:21:14 +030091 ret = fill_char_horizontally(pfont, &line, vid_priv, fontdata, FLIPPED_DIRECTION);
Dzmitry Sankouskiaea2d2d2023-03-07 13:21:11 +030092 if (ret)
93 return ret;
Simon Glass87aae882016-01-18 19:52:19 -070094
Simon Glass42301da2020-07-02 21:12:26 -060095 /* We draw backwards from 'start, so account for the first line */
96 ret = vidconsole_sync_copy(dev, start - vid_priv->line_length, line);
97 if (ret)
98 return ret;
Simon Glass87aae882016-01-18 19:52:19 -070099
Dzmitry Sankouski7fa964a2023-03-07 13:21:14 +0300100 return VID_TO_POS(fontdata->width);
Simon Glass87aae882016-01-18 19:52:19 -0700101}
102
Simon Glass87aae882016-01-18 19:52:19 -0700103static int console_set_row_2(struct udevice *dev, uint row, int clr)
104{
105 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
Dzmitry Sankouski7fa964a2023-03-07 13:21:14 +0300106 struct console_simple_priv *priv = dev_get_priv(dev);
107 struct video_fontdata *fontdata = priv->fontdata;
Dzmitry Sankouskiaea2d2d2023-03-07 13:21:11 +0300108 void *start, *line, *dst, *end;
Dzmitry Sankouski7fa964a2023-03-07 13:21:14 +0300109 int pixels = fontdata->height * vid_priv->xsize;
Simon Glass42301da2020-07-02 21:12:26 -0600110 int i, ret;
Dzmitry Sankouskiaea2d2d2023-03-07 13:21:11 +0300111 int pbytes = VNBYTES(vid_priv->bpix);
Simon Glass87aae882016-01-18 19:52:19 -0700112
Simon Glass42301da2020-07-02 21:12:26 -0600113 start = vid_priv->fb + vid_priv->ysize * vid_priv->line_length -
Dzmitry Sankouski7fa964a2023-03-07 13:21:14 +0300114 (row + 1) * fontdata->height * vid_priv->line_length;
Simon Glass42301da2020-07-02 21:12:26 -0600115 line = start;
Dzmitry Sankouskiaea2d2d2023-03-07 13:21:11 +0300116 dst = line;
117 for (i = 0; i < pixels; i++)
118 fill_pixel_and_goto_next(&dst, clr, pbytes, pbytes);
119 end = dst;
Simon Glass42301da2020-07-02 21:12:26 -0600120 ret = vidconsole_sync_copy(dev, start, end);
121 if (ret)
122 return ret;
Simon Glass87aae882016-01-18 19:52:19 -0700123
124 return 0;
125}
126
127static int console_move_rows_2(struct udevice *dev, uint rowdst, uint rowsrc,
128 uint count)
129{
130 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
Dzmitry Sankouski7fa964a2023-03-07 13:21:14 +0300131 struct console_simple_priv *priv = dev_get_priv(dev);
132 struct video_fontdata *fontdata = priv->fontdata;
Simon Glass87aae882016-01-18 19:52:19 -0700133 void *dst;
134 void *src;
135 void *end;
136
137 end = vid_priv->fb + vid_priv->ysize * vid_priv->line_length;
Dzmitry Sankouski7fa964a2023-03-07 13:21:14 +0300138 dst = end - (rowdst + count) * fontdata->height *
Simon Glass87aae882016-01-18 19:52:19 -0700139 vid_priv->line_length;
Dzmitry Sankouski7fa964a2023-03-07 13:21:14 +0300140 src = end - (rowsrc + count) * fontdata->height *
Simon Glass87aae882016-01-18 19:52:19 -0700141 vid_priv->line_length;
Simon Glass42301da2020-07-02 21:12:26 -0600142 vidconsole_memmove(dev, dst, src,
Dzmitry Sankouski7fa964a2023-03-07 13:21:14 +0300143 fontdata->height * vid_priv->line_length * count);
Simon Glass87aae882016-01-18 19:52:19 -0700144
145 return 0;
146}
147
Janne Grunau5548c362024-03-16 22:50:19 +0100148static int console_putc_xy_2(struct udevice *dev, uint x_frac, uint y, int cp)
Simon Glass87aae882016-01-18 19:52:19 -0700149{
Simon Glass52c10c52016-01-14 18:10:37 -0700150 struct vidconsole_priv *vc_priv = dev_get_uclass_priv(dev);
Simon Glass87aae882016-01-18 19:52:19 -0700151 struct udevice *vid = dev->parent;
152 struct video_priv *vid_priv = dev_get_uclass_priv(vid);
Dzmitry Sankouski7fa964a2023-03-07 13:21:14 +0300153 struct console_simple_priv *priv = dev_get_priv(dev);
154 struct video_fontdata *fontdata = priv->fontdata;
Simon Glass80daa3f2020-07-02 21:12:16 -0600155 int pbytes = VNBYTES(vid_priv->bpix);
Dzmitry Sankouskiaea2d2d2023-03-07 13:21:11 +0300156 int linenum, x, ret;
Simon Glass42301da2020-07-02 21:12:26 -0600157 void *start, *line;
Janne Grunau5548c362024-03-16 22:50:19 +0100158 u8 ch = console_utf_to_cp437(cp);
Dzmitry Sankouski7fa964a2023-03-07 13:21:14 +0300159 uchar *pfont = fontdata->video_fontdata +
Janne Grunau5548c362024-03-16 22:50:19 +0100160 ch * fontdata->char_pixel_bytes;
Simon Glass87aae882016-01-18 19:52:19 -0700161
Simon Glass52c10c52016-01-14 18:10:37 -0700162 if (x_frac + VID_TO_POS(vc_priv->x_charsize) > vc_priv->xsize_frac)
163 return -EAGAIN;
Simon Glass80daa3f2020-07-02 21:12:16 -0600164 linenum = vid_priv->ysize - y - 1;
Simon Glassf50a6b92020-07-02 21:12:17 -0600165 x = vid_priv->xsize - VID_TO_PIXEL(x_frac) - 1;
Simon Glass42301da2020-07-02 21:12:26 -0600166 start = vid_priv->fb + linenum * vid_priv->line_length + x * pbytes;
167 line = start;
Simon Glass87aae882016-01-18 19:52:19 -0700168
Dzmitry Sankouski7fa964a2023-03-07 13:21:14 +0300169 ret = fill_char_vertically(pfont, &line, vid_priv, fontdata, FLIPPED_DIRECTION);
Dzmitry Sankouskiaea2d2d2023-03-07 13:21:11 +0300170 if (ret)
171 return ret;
Simon Glass87aae882016-01-18 19:52:19 -0700172
Simon Glass42301da2020-07-02 21:12:26 -0600173 /* Add 4 bytes to allow for the first pixel writen */
174 ret = vidconsole_sync_copy(dev, start + 4, line);
175 if (ret)
176 return ret;
Simon Glass87aae882016-01-18 19:52:19 -0700177
Dzmitry Sankouski7fa964a2023-03-07 13:21:14 +0300178 return VID_TO_POS(fontdata->width);
Simon Glass87aae882016-01-18 19:52:19 -0700179}
180
181static int console_set_row_3(struct udevice *dev, uint row, int clr)
182{
183 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
Dzmitry Sankouski7fa964a2023-03-07 13:21:14 +0300184 struct console_simple_priv *priv = dev_get_priv(dev);
185 struct video_fontdata *fontdata = priv->fontdata;
Simon Glass87aae882016-01-18 19:52:19 -0700186 int pbytes = VNBYTES(vid_priv->bpix);
Dzmitry Sankouskiaea2d2d2023-03-07 13:21:11 +0300187 void *start, *dst, *line;
Simon Glass42301da2020-07-02 21:12:26 -0600188 int i, j, ret;
Simon Glass87aae882016-01-18 19:52:19 -0700189
Dzmitry Sankouski7fa964a2023-03-07 13:21:14 +0300190 start = vid_priv->fb + row * fontdata->height * pbytes;
Simon Glass42301da2020-07-02 21:12:26 -0600191 line = start;
Simon Glass87aae882016-01-18 19:52:19 -0700192 for (j = 0; j < vid_priv->ysize; j++) {
Dzmitry Sankouskiaea2d2d2023-03-07 13:21:11 +0300193 dst = line;
Dzmitry Sankouski7fa964a2023-03-07 13:21:14 +0300194 for (i = 0; i < fontdata->height; i++)
Dzmitry Sankouskiaea2d2d2023-03-07 13:21:11 +0300195 fill_pixel_and_goto_next(&dst, clr, pbytes, pbytes);
Simon Glass87aae882016-01-18 19:52:19 -0700196 line += vid_priv->line_length;
197 }
Simon Glass42301da2020-07-02 21:12:26 -0600198 ret = vidconsole_sync_copy(dev, start, line);
199 if (ret)
200 return ret;
Simon Glass87aae882016-01-18 19:52:19 -0700201
202 return 0;
203}
204
205static int console_move_rows_3(struct udevice *dev, uint rowdst, uint rowsrc,
206 uint count)
207{
208 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
Dzmitry Sankouski7fa964a2023-03-07 13:21:14 +0300209 struct console_simple_priv *priv = dev_get_priv(dev);
210 struct video_fontdata *fontdata = priv->fontdata;
Simon Glass80daa3f2020-07-02 21:12:16 -0600211 int pbytes = VNBYTES(vid_priv->bpix);
Simon Glass87aae882016-01-18 19:52:19 -0700212 void *dst;
213 void *src;
Simon Glass42301da2020-07-02 21:12:26 -0600214 int j, ret;
Simon Glass87aae882016-01-18 19:52:19 -0700215
Dzmitry Sankouski7fa964a2023-03-07 13:21:14 +0300216 dst = vid_priv->fb + rowdst * fontdata->height * pbytes;
217 src = vid_priv->fb + rowsrc * fontdata->height * pbytes;
Simon Glass87aae882016-01-18 19:52:19 -0700218
219 for (j = 0; j < vid_priv->ysize; j++) {
Simon Glass42301da2020-07-02 21:12:26 -0600220 ret = vidconsole_memmove(dev, dst, src,
Dzmitry Sankouski7fa964a2023-03-07 13:21:14 +0300221 fontdata->height * pbytes * count);
Simon Glass42301da2020-07-02 21:12:26 -0600222 if (ret)
223 return ret;
Simon Glass87aae882016-01-18 19:52:19 -0700224 src += vid_priv->line_length;
225 dst += vid_priv->line_length;
226 }
227
228 return 0;
229}
230
Janne Grunau5548c362024-03-16 22:50:19 +0100231static int console_putc_xy_3(struct udevice *dev, uint x_frac, uint y, int cp)
Simon Glass87aae882016-01-18 19:52:19 -0700232{
Simon Glass52c10c52016-01-14 18:10:37 -0700233 struct vidconsole_priv *vc_priv = dev_get_uclass_priv(dev);
Simon Glass87aae882016-01-18 19:52:19 -0700234 struct udevice *vid = dev->parent;
235 struct video_priv *vid_priv = dev_get_uclass_priv(vid);
Dzmitry Sankouski7fa964a2023-03-07 13:21:14 +0300236 struct console_simple_priv *priv = dev_get_priv(dev);
237 struct video_fontdata *fontdata = priv->fontdata;
Simon Glass87aae882016-01-18 19:52:19 -0700238 int pbytes = VNBYTES(vid_priv->bpix);
Dzmitry Sankouskiaea2d2d2023-03-07 13:21:11 +0300239 int linenum, x, ret;
Simon Glass42301da2020-07-02 21:12:26 -0600240 void *start, *line;
Janne Grunau5548c362024-03-16 22:50:19 +0100241 u8 ch = console_utf_to_cp437(cp);
Dzmitry Sankouski7fa964a2023-03-07 13:21:14 +0300242 uchar *pfont = fontdata->video_fontdata +
Janne Grunau5548c362024-03-16 22:50:19 +0100243 ch * fontdata->char_pixel_bytes;
Simon Glass87aae882016-01-18 19:52:19 -0700244
Simon Glass52c10c52016-01-14 18:10:37 -0700245 if (x_frac + VID_TO_POS(vc_priv->x_charsize) > vc_priv->xsize_frac)
246 return -EAGAIN;
Dzmitry Sankouskiaea2d2d2023-03-07 13:21:11 +0300247 x = y;
248 linenum = vid_priv->ysize - VID_TO_PIXEL(x_frac) - 1;
249 start = vid_priv->fb + linenum * vid_priv->line_length + y * pbytes;
Simon Glass42301da2020-07-02 21:12:26 -0600250 line = start;
Simon Glass87aae882016-01-18 19:52:19 -0700251
Dzmitry Sankouski7fa964a2023-03-07 13:21:14 +0300252 ret = fill_char_horizontally(pfont, &line, vid_priv, fontdata, NORMAL_DIRECTION);
Dzmitry Sankouskiaea2d2d2023-03-07 13:21:11 +0300253 if (ret)
254 return ret;
Simon Glass42301da2020-07-02 21:12:26 -0600255 /* Add a line to allow for the first pixels writen */
256 ret = vidconsole_sync_copy(dev, start + vid_priv->line_length, line);
257 if (ret)
258 return ret;
Simon Glass87aae882016-01-18 19:52:19 -0700259
Dzmitry Sankouski7fa964a2023-03-07 13:21:14 +0300260 return VID_TO_POS(fontdata->width);
Simon Glass87aae882016-01-18 19:52:19 -0700261}
262
Simon Glass87aae882016-01-18 19:52:19 -0700263struct vidconsole_ops console_ops_1 = {
264 .putc_xy = console_putc_xy_1,
265 .move_rows = console_move_rows_1,
266 .set_row = console_set_row_1,
Dzmitry Sankouskibb165e42023-03-07 13:21:16 +0300267 .get_font_size = console_simple_get_font_size,
268 .get_font = console_simple_get_font,
269 .select_font = console_simple_select_font,
Simon Glass87aae882016-01-18 19:52:19 -0700270};
271
272struct vidconsole_ops console_ops_2 = {
273 .putc_xy = console_putc_xy_2,
274 .move_rows = console_move_rows_2,
275 .set_row = console_set_row_2,
Dzmitry Sankouskibb165e42023-03-07 13:21:16 +0300276 .get_font_size = console_simple_get_font_size,
277 .get_font = console_simple_get_font,
278 .select_font = console_simple_select_font,
Simon Glass87aae882016-01-18 19:52:19 -0700279};
280
281struct vidconsole_ops console_ops_3 = {
282 .putc_xy = console_putc_xy_3,
283 .move_rows = console_move_rows_3,
284 .set_row = console_set_row_3,
Dzmitry Sankouskibb165e42023-03-07 13:21:16 +0300285 .get_font_size = console_simple_get_font_size,
286 .get_font = console_simple_get_font,
287 .select_font = console_simple_select_font,
Simon Glass87aae882016-01-18 19:52:19 -0700288};
289
290U_BOOT_DRIVER(vidconsole_1) = {
291 .name = "vidconsole1",
292 .id = UCLASS_VIDEO_CONSOLE,
293 .ops = &console_ops_1,
Dzmitry Sankouskiaea2d2d2023-03-07 13:21:11 +0300294 .probe = console_probe,
Dzmitry Sankouski7fa964a2023-03-07 13:21:14 +0300295 .priv_auto = sizeof(struct console_simple_priv),
Simon Glass87aae882016-01-18 19:52:19 -0700296};
297
298U_BOOT_DRIVER(vidconsole_2) = {
299 .name = "vidconsole2",
300 .id = UCLASS_VIDEO_CONSOLE,
301 .ops = &console_ops_2,
Dzmitry Sankouskiaea2d2d2023-03-07 13:21:11 +0300302 .probe = console_probe,
Dzmitry Sankouski7fa964a2023-03-07 13:21:14 +0300303 .priv_auto = sizeof(struct console_simple_priv),
Simon Glass87aae882016-01-18 19:52:19 -0700304};
305
306U_BOOT_DRIVER(vidconsole_3) = {
307 .name = "vidconsole3",
308 .id = UCLASS_VIDEO_CONSOLE,
309 .ops = &console_ops_3,
Dzmitry Sankouskiaea2d2d2023-03-07 13:21:11 +0300310 .probe = console_probe,
Dzmitry Sankouski7fa964a2023-03-07 13:21:14 +0300311 .priv_auto = sizeof(struct console_simple_priv),
Simon Glass87aae882016-01-18 19:52:19 -0700312};