1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Google, Inc
5 * Bernecker & Rainer Industrieelektronik GmbH - http://www.br-automation.com
6 * (C) Copyright 2023 Dzmitry Sankouski <dsankouski@gmail.com>
12 #include <video_console.h>
13 #include <video_font.h> /* Get font data, width and height */
14 #include "vidconsole_internal.h"
16 static int console_set_row_1(struct udevice *dev, uint row, int clr)
18 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
19 struct console_simple_priv *priv = dev_get_priv(dev);
20 struct video_fontdata *fontdata = priv->fontdata;
21 int pbytes = VNBYTES(vid_priv->bpix);
22 void *start, *dst, *line;
26 start = vid_priv->fb + vid_priv->line_length -
27 (row + 1) * fontdata->height * pbytes;
29 for (j = 0; j < vid_priv->ysize; j++) {
31 for (i = 0; i < fontdata->height; i++)
32 fill_pixel_and_goto_next(&dst, clr, pbytes, pbytes);
33 line += vid_priv->line_length;
35 ret = vidconsole_sync_copy(dev, start, line);
42 static int console_move_rows_1(struct udevice *dev, uint rowdst, uint rowsrc,
45 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
46 struct console_simple_priv *priv = dev_get_priv(dev);
47 struct video_fontdata *fontdata = priv->fontdata;
48 int pbytes = VNBYTES(vid_priv->bpix);
53 dst = vid_priv->fb + vid_priv->line_length -
54 (rowdst + count) * fontdata->height * pbytes;
55 src = vid_priv->fb + vid_priv->line_length -
56 (rowsrc + count) * fontdata->height * pbytes;
58 for (j = 0; j < vid_priv->ysize; j++) {
59 ret = vidconsole_memmove(dev, dst, src,
60 fontdata->height * pbytes * count);
63 src += vid_priv->line_length;
64 dst += vid_priv->line_length;
70 static int console_putc_xy_1(struct udevice *dev, uint x_frac, uint y, char ch)
72 struct vidconsole_priv *vc_priv = dev_get_uclass_priv(dev);
73 struct udevice *vid = dev->parent;
74 struct video_priv *vid_priv = dev_get_uclass_priv(vid);
75 struct console_simple_priv *priv = dev_get_priv(dev);
76 struct video_fontdata *fontdata = priv->fontdata;
77 int pbytes = VNBYTES(vid_priv->bpix);
80 uchar *pfont = fontdata->video_fontdata +
81 (u8)ch * fontdata->char_pixel_bytes;
83 if (x_frac + VID_TO_POS(vc_priv->x_charsize) > vc_priv->xsize_frac)
85 linenum = VID_TO_PIXEL(x_frac) + 1;
87 start = vid_priv->fb + linenum * vid_priv->line_length - x * pbytes;
90 ret = fill_char_horizontally(pfont, &line, vid_priv, fontdata, FLIPPED_DIRECTION);
94 /* We draw backwards from 'start, so account for the first line */
95 ret = vidconsole_sync_copy(dev, start - vid_priv->line_length, line);
99 return VID_TO_POS(fontdata->width);
103 static int console_set_row_2(struct udevice *dev, uint row, int clr)
105 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
106 struct console_simple_priv *priv = dev_get_priv(dev);
107 struct video_fontdata *fontdata = priv->fontdata;
108 void *start, *line, *dst, *end;
109 int pixels = fontdata->height * vid_priv->xsize;
111 int pbytes = VNBYTES(vid_priv->bpix);
113 start = vid_priv->fb + vid_priv->ysize * vid_priv->line_length -
114 (row + 1) * fontdata->height * vid_priv->line_length;
117 for (i = 0; i < pixels; i++)
118 fill_pixel_and_goto_next(&dst, clr, pbytes, pbytes);
120 ret = vidconsole_sync_copy(dev, start, end);
127 static int console_move_rows_2(struct udevice *dev, uint rowdst, uint rowsrc,
130 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
131 struct console_simple_priv *priv = dev_get_priv(dev);
132 struct video_fontdata *fontdata = priv->fontdata;
137 end = vid_priv->fb + vid_priv->ysize * vid_priv->line_length;
138 dst = end - (rowdst + count) * fontdata->height *
139 vid_priv->line_length;
140 src = end - (rowsrc + count) * fontdata->height *
141 vid_priv->line_length;
142 vidconsole_memmove(dev, dst, src,
143 fontdata->height * vid_priv->line_length * count);
148 static int console_putc_xy_2(struct udevice *dev, uint x_frac, uint y, char ch)
150 struct vidconsole_priv *vc_priv = dev_get_uclass_priv(dev);
151 struct udevice *vid = dev->parent;
152 struct video_priv *vid_priv = dev_get_uclass_priv(vid);
153 struct console_simple_priv *priv = dev_get_priv(dev);
154 struct video_fontdata *fontdata = priv->fontdata;
155 int pbytes = VNBYTES(vid_priv->bpix);
158 uchar *pfont = fontdata->video_fontdata +
159 (u8)ch * fontdata->char_pixel_bytes;
161 if (x_frac + VID_TO_POS(vc_priv->x_charsize) > vc_priv->xsize_frac)
163 linenum = vid_priv->ysize - y - 1;
164 x = vid_priv->xsize - VID_TO_PIXEL(x_frac) - 1;
165 start = vid_priv->fb + linenum * vid_priv->line_length + x * pbytes;
168 ret = fill_char_vertically(pfont, &line, vid_priv, fontdata, FLIPPED_DIRECTION);
172 /* Add 4 bytes to allow for the first pixel writen */
173 ret = vidconsole_sync_copy(dev, start + 4, line);
177 return VID_TO_POS(fontdata->width);
180 static int console_set_row_3(struct udevice *dev, uint row, int clr)
182 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
183 struct console_simple_priv *priv = dev_get_priv(dev);
184 struct video_fontdata *fontdata = priv->fontdata;
185 int pbytes = VNBYTES(vid_priv->bpix);
186 void *start, *dst, *line;
189 start = vid_priv->fb + row * fontdata->height * pbytes;
191 for (j = 0; j < vid_priv->ysize; j++) {
193 for (i = 0; i < fontdata->height; i++)
194 fill_pixel_and_goto_next(&dst, clr, pbytes, pbytes);
195 line += vid_priv->line_length;
197 ret = vidconsole_sync_copy(dev, start, line);
204 static int console_move_rows_3(struct udevice *dev, uint rowdst, uint rowsrc,
207 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
208 struct console_simple_priv *priv = dev_get_priv(dev);
209 struct video_fontdata *fontdata = priv->fontdata;
210 int pbytes = VNBYTES(vid_priv->bpix);
215 dst = vid_priv->fb + rowdst * fontdata->height * pbytes;
216 src = vid_priv->fb + rowsrc * fontdata->height * pbytes;
218 for (j = 0; j < vid_priv->ysize; j++) {
219 ret = vidconsole_memmove(dev, dst, src,
220 fontdata->height * pbytes * count);
223 src += vid_priv->line_length;
224 dst += vid_priv->line_length;
230 static int console_putc_xy_3(struct udevice *dev, uint x_frac, uint y, char ch)
232 struct vidconsole_priv *vc_priv = dev_get_uclass_priv(dev);
233 struct udevice *vid = dev->parent;
234 struct video_priv *vid_priv = dev_get_uclass_priv(vid);
235 struct console_simple_priv *priv = dev_get_priv(dev);
236 struct video_fontdata *fontdata = priv->fontdata;
237 int pbytes = VNBYTES(vid_priv->bpix);
240 uchar *pfont = fontdata->video_fontdata +
241 (u8)ch * fontdata->char_pixel_bytes;
243 if (x_frac + VID_TO_POS(vc_priv->x_charsize) > vc_priv->xsize_frac)
246 linenum = vid_priv->ysize - VID_TO_PIXEL(x_frac) - 1;
247 start = vid_priv->fb + linenum * vid_priv->line_length + y * pbytes;
250 ret = fill_char_horizontally(pfont, &line, vid_priv, fontdata, NORMAL_DIRECTION);
253 /* Add a line to allow for the first pixels writen */
254 ret = vidconsole_sync_copy(dev, start + vid_priv->line_length, line);
258 return VID_TO_POS(fontdata->width);
261 struct vidconsole_ops console_ops_1 = {
262 .putc_xy = console_putc_xy_1,
263 .move_rows = console_move_rows_1,
264 .set_row = console_set_row_1,
267 struct vidconsole_ops console_ops_2 = {
268 .putc_xy = console_putc_xy_2,
269 .move_rows = console_move_rows_2,
270 .set_row = console_set_row_2,
273 struct vidconsole_ops console_ops_3 = {
274 .putc_xy = console_putc_xy_3,
275 .move_rows = console_move_rows_3,
276 .set_row = console_set_row_3,
279 U_BOOT_DRIVER(vidconsole_1) = {
280 .name = "vidconsole1",
281 .id = UCLASS_VIDEO_CONSOLE,
282 .ops = &console_ops_1,
283 .probe = console_probe,
284 .priv_auto = sizeof(struct console_simple_priv),
287 U_BOOT_DRIVER(vidconsole_2) = {
288 .name = "vidconsole2",
289 .id = UCLASS_VIDEO_CONSOLE,
290 .ops = &console_ops_2,
291 .probe = console_probe,
292 .priv_auto = sizeof(struct console_simple_priv),
295 U_BOOT_DRIVER(vidconsole_3) = {
296 .name = "vidconsole3",
297 .id = UCLASS_VIDEO_CONSOLE,
298 .ops = &console_ops_3,
299 .probe = console_probe,
300 .priv_auto = sizeof(struct console_simple_priv),