1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Google, Inc
5 * Bernecker & Rainer Industrieelektronik GmbH - http://www.br-automation.com
11 #include <video_console.h>
12 #include <video_font.h> /* Get font data, width and height */
14 static int console_set_row_1(struct udevice *dev, uint row, int clr)
16 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
17 int pbytes = VNBYTES(vid_priv->bpix);
22 start = vid_priv->fb + vid_priv->line_length -
23 (row + 1) * VIDEO_FONT_HEIGHT * pbytes;
25 for (j = 0; j < vid_priv->ysize; j++) {
26 switch (vid_priv->bpix) {
28 if (IS_ENABLED(CONFIG_VIDEO_BPP8)) {
31 for (i = 0; i < VIDEO_FONT_HEIGHT; i++)
36 if (IS_ENABLED(CONFIG_VIDEO_BPP16)) {
39 for (i = 0; i < VIDEO_FONT_HEIGHT; i++)
44 if (IS_ENABLED(CONFIG_VIDEO_BPP32)) {
47 for (i = 0; i < VIDEO_FONT_HEIGHT; i++)
54 line += vid_priv->line_length;
56 ret = vidconsole_sync_copy(dev, start, line);
63 static int console_move_rows_1(struct udevice *dev, uint rowdst, uint rowsrc,
66 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
67 int pbytes = VNBYTES(vid_priv->bpix);
72 dst = vid_priv->fb + vid_priv->line_length -
73 (rowdst + count) * VIDEO_FONT_HEIGHT * pbytes;
74 src = vid_priv->fb + vid_priv->line_length -
75 (rowsrc + count) * VIDEO_FONT_HEIGHT * pbytes;
77 for (j = 0; j < vid_priv->ysize; j++) {
78 ret = vidconsole_memmove(dev, dst, src,
79 VIDEO_FONT_HEIGHT * pbytes * count);
82 src += vid_priv->line_length;
83 dst += vid_priv->line_length;
89 static int console_putc_xy_1(struct udevice *dev, uint x_frac, uint y, char ch)
91 struct vidconsole_priv *vc_priv = dev_get_uclass_priv(dev);
92 struct udevice *vid = dev->parent;
93 struct video_priv *vid_priv = dev_get_uclass_priv(vid);
94 uchar *pfont = video_fontdata + (u8)ch * VIDEO_FONT_HEIGHT;
95 int pbytes = VNBYTES(vid_priv->bpix);
96 int i, col, x, linenum, ret;
100 linenum = VID_TO_PIXEL(x_frac) + 1;
102 start = vid_priv->fb + linenum * vid_priv->line_length - x * pbytes;
104 if (x_frac + VID_TO_POS(vc_priv->x_charsize) > vc_priv->xsize_frac)
107 for (col = 0; col < VIDEO_FONT_HEIGHT; col++) {
108 switch (vid_priv->bpix) {
110 if (IS_ENABLED(CONFIG_VIDEO_BPP8)) {
113 for (i = 0; i < VIDEO_FONT_HEIGHT; i++) {
114 *dst-- = (pfont[i] & mask) ?
115 vid_priv->colour_fg :
121 if (IS_ENABLED(CONFIG_VIDEO_BPP16)) {
122 uint16_t *dst = line;
124 for (i = 0; i < VIDEO_FONT_HEIGHT; i++) {
125 *dst-- = (pfont[i] & mask) ?
126 vid_priv->colour_fg :
132 if (IS_ENABLED(CONFIG_VIDEO_BPP32)) {
133 uint32_t *dst = line;
135 for (i = 0; i < VIDEO_FONT_HEIGHT; i++) {
136 *dst-- = (pfont[i] & mask) ?
137 vid_priv->colour_fg :
145 line += vid_priv->line_length;
148 /* We draw backwards from 'start, so account for the first line */
149 ret = vidconsole_sync_copy(dev, start - vid_priv->line_length, line);
153 return VID_TO_POS(VIDEO_FONT_WIDTH);
157 static int console_set_row_2(struct udevice *dev, uint row, int clr)
159 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
160 void *start, *line, *end;
161 int pixels = VIDEO_FONT_HEIGHT * vid_priv->xsize;
164 start = vid_priv->fb + vid_priv->ysize * vid_priv->line_length -
165 (row + 1) * VIDEO_FONT_HEIGHT * vid_priv->line_length;
167 switch (vid_priv->bpix) {
169 if (IS_ENABLED(CONFIG_VIDEO_BPP8)) {
172 for (i = 0; i < pixels; i++)
178 if (IS_ENABLED(CONFIG_VIDEO_BPP16)) {
179 uint16_t *dst = line;
181 for (i = 0; i < pixels; i++)
187 if (IS_ENABLED(CONFIG_VIDEO_BPP32)) {
188 uint32_t *dst = line;
190 for (i = 0; i < pixels; i++)
198 ret = vidconsole_sync_copy(dev, start, end);
205 static int console_move_rows_2(struct udevice *dev, uint rowdst, uint rowsrc,
208 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
213 end = vid_priv->fb + vid_priv->ysize * vid_priv->line_length;
214 dst = end - (rowdst + count) * VIDEO_FONT_HEIGHT *
215 vid_priv->line_length;
216 src = end - (rowsrc + count) * VIDEO_FONT_HEIGHT *
217 vid_priv->line_length;
218 vidconsole_memmove(dev, dst, src,
219 VIDEO_FONT_HEIGHT * vid_priv->line_length * count);
224 static int console_putc_xy_2(struct udevice *dev, uint x_frac, uint y, char ch)
226 struct vidconsole_priv *vc_priv = dev_get_uclass_priv(dev);
227 struct udevice *vid = dev->parent;
228 struct video_priv *vid_priv = dev_get_uclass_priv(vid);
229 int pbytes = VNBYTES(vid_priv->bpix);
230 int i, row, x, linenum, ret;
233 if (x_frac + VID_TO_POS(vc_priv->x_charsize) > vc_priv->xsize_frac)
235 linenum = vid_priv->ysize - y - 1;
236 x = vid_priv->xsize - VID_TO_PIXEL(x_frac) - 1;
237 start = vid_priv->fb + linenum * vid_priv->line_length + x * pbytes;
240 for (row = 0; row < VIDEO_FONT_HEIGHT; row++) {
241 unsigned int idx = (u8)ch * VIDEO_FONT_HEIGHT + row;
242 uchar bits = video_fontdata[idx];
244 switch (vid_priv->bpix) {
246 if (IS_ENABLED(CONFIG_VIDEO_BPP8)) {
249 for (i = 0; i < VIDEO_FONT_WIDTH; i++) {
250 *dst-- = (bits & 0x80) ?
251 vid_priv->colour_fg :
258 if (IS_ENABLED(CONFIG_VIDEO_BPP16)) {
259 uint16_t *dst = line;
261 for (i = 0; i < VIDEO_FONT_WIDTH; i++) {
262 *dst-- = (bits & 0x80) ?
263 vid_priv->colour_fg :
270 if (IS_ENABLED(CONFIG_VIDEO_BPP32)) {
271 uint32_t *dst = line;
273 for (i = 0; i < VIDEO_FONT_WIDTH; i++) {
274 *dst-- = (bits & 0x80) ?
275 vid_priv->colour_fg :
284 line -= vid_priv->line_length;
286 /* Add 4 bytes to allow for the first pixel writen */
287 ret = vidconsole_sync_copy(dev, start + 4, line);
291 return VID_TO_POS(VIDEO_FONT_WIDTH);
294 static int console_set_row_3(struct udevice *dev, uint row, int clr)
296 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
297 int pbytes = VNBYTES(vid_priv->bpix);
301 start = vid_priv->fb + row * VIDEO_FONT_HEIGHT * pbytes;
303 for (j = 0; j < vid_priv->ysize; j++) {
304 switch (vid_priv->bpix) {
306 if (IS_ENABLED(CONFIG_VIDEO_BPP8)) {
309 for (i = 0; i < VIDEO_FONT_HEIGHT; i++)
314 if (IS_ENABLED(CONFIG_VIDEO_BPP16)) {
315 uint16_t *dst = line;
317 for (i = 0; i < VIDEO_FONT_HEIGHT; i++)
322 if (IS_ENABLED(CONFIG_VIDEO_BPP32)) {
323 uint32_t *dst = line;
325 for (i = 0; i < VIDEO_FONT_HEIGHT; i++)
332 line += vid_priv->line_length;
334 ret = vidconsole_sync_copy(dev, start, line);
341 static int console_move_rows_3(struct udevice *dev, uint rowdst, uint rowsrc,
344 struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
345 int pbytes = VNBYTES(vid_priv->bpix);
350 dst = vid_priv->fb + rowdst * VIDEO_FONT_HEIGHT * pbytes;
351 src = vid_priv->fb + rowsrc * VIDEO_FONT_HEIGHT * pbytes;
353 for (j = 0; j < vid_priv->ysize; j++) {
354 ret = vidconsole_memmove(dev, dst, src,
355 VIDEO_FONT_HEIGHT * pbytes * count);
358 src += vid_priv->line_length;
359 dst += vid_priv->line_length;
365 static int console_putc_xy_3(struct udevice *dev, uint x_frac, uint y, char ch)
367 struct vidconsole_priv *vc_priv = dev_get_uclass_priv(dev);
368 struct udevice *vid = dev->parent;
369 struct video_priv *vid_priv = dev_get_uclass_priv(vid);
370 uchar *pfont = video_fontdata + (u8)ch * VIDEO_FONT_HEIGHT;
371 int pbytes = VNBYTES(vid_priv->bpix);
376 if (x_frac + VID_TO_POS(vc_priv->x_charsize) > vc_priv->xsize_frac)
378 x = vid_priv->ysize - VID_TO_PIXEL(x_frac) - 1;
379 start = vid_priv->fb + x * vid_priv->line_length + y * pbytes;
381 for (col = 0; col < VIDEO_FONT_HEIGHT; col++) {
382 switch (vid_priv->bpix) {
384 if (IS_ENABLED(CONFIG_VIDEO_BPP8)) {
387 for (i = 0; i < VIDEO_FONT_HEIGHT; i++) {
388 *dst++ = (pfont[i] & mask) ?
389 vid_priv->colour_fg :
395 if (IS_ENABLED(CONFIG_VIDEO_BPP16)) {
396 uint16_t *dst = line;
398 for (i = 0; i < VIDEO_FONT_HEIGHT; i++) {
399 *dst++ = (pfont[i] & mask) ?
400 vid_priv->colour_fg :
406 if (IS_ENABLED(CONFIG_VIDEO_BPP32)) {
407 uint32_t *dst = line;
409 for (i = 0; i < VIDEO_FONT_HEIGHT; i++) {
410 *dst++ = (pfont[i] & mask) ?
411 vid_priv->colour_fg :
419 line -= vid_priv->line_length;
422 /* Add a line to allow for the first pixels writen */
423 ret = vidconsole_sync_copy(dev, start + vid_priv->line_length, line);
427 return VID_TO_POS(VIDEO_FONT_WIDTH);
431 static int console_probe_2(struct udevice *dev)
433 struct vidconsole_priv *vc_priv = dev_get_uclass_priv(dev);
434 struct udevice *vid_dev = dev->parent;
435 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
437 vc_priv->x_charsize = VIDEO_FONT_WIDTH;
438 vc_priv->y_charsize = VIDEO_FONT_HEIGHT;
439 vc_priv->cols = vid_priv->xsize / VIDEO_FONT_WIDTH;
440 vc_priv->rows = vid_priv->ysize / VIDEO_FONT_HEIGHT;
445 static int console_probe_1_3(struct udevice *dev)
447 struct vidconsole_priv *vc_priv = dev_get_uclass_priv(dev);
448 struct udevice *vid_dev = dev->parent;
449 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
451 vc_priv->x_charsize = VIDEO_FONT_WIDTH;
452 vc_priv->y_charsize = VIDEO_FONT_HEIGHT;
453 vc_priv->cols = vid_priv->ysize / VIDEO_FONT_WIDTH;
454 vc_priv->rows = vid_priv->xsize / VIDEO_FONT_HEIGHT;
455 vc_priv->xsize_frac = VID_TO_POS(vid_priv->ysize);
460 struct vidconsole_ops console_ops_1 = {
461 .putc_xy = console_putc_xy_1,
462 .move_rows = console_move_rows_1,
463 .set_row = console_set_row_1,
466 struct vidconsole_ops console_ops_2 = {
467 .putc_xy = console_putc_xy_2,
468 .move_rows = console_move_rows_2,
469 .set_row = console_set_row_2,
472 struct vidconsole_ops console_ops_3 = {
473 .putc_xy = console_putc_xy_3,
474 .move_rows = console_move_rows_3,
475 .set_row = console_set_row_3,
478 U_BOOT_DRIVER(vidconsole_1) = {
479 .name = "vidconsole1",
480 .id = UCLASS_VIDEO_CONSOLE,
481 .ops = &console_ops_1,
482 .probe = console_probe_1_3,
485 U_BOOT_DRIVER(vidconsole_2) = {
486 .name = "vidconsole2",
487 .id = UCLASS_VIDEO_CONSOLE,
488 .ops = &console_ops_2,
489 .probe = console_probe_2,
492 U_BOOT_DRIVER(vidconsole_3) = {
493 .name = "vidconsole3",
494 .id = UCLASS_VIDEO_CONSOLE,
495 .ops = &console_ops_3,
496 .probe = console_probe_1_3,