1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (c) 2015 Google, Inc
4 * (C) Copyright 2015
5 * Bernecker & Rainer Industrieelektronik GmbH - http://www.br-automation.com
6 * (C) Copyright 2023 Dzmitry Sankouski <dsankouski@gmail.com>
7 */
8
9#include <charset.h>
10#include <dm.h>
11#include <video.h>
12#include <video_console.h>
13#include <video_font.h>		/* Get font data, width and height */
14#include "vidconsole_internal.h"
15
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);
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;
23	int i, j;
24	int ret;
25
26	start = vid_priv->fb + vid_priv->line_length -
27		(row + 1) * fontdata->height * pbytes;
28	line = start;
29	for (j = 0; j < vid_priv->ysize; j++) {
30		dst = line;
31		for (i = 0; i < fontdata->height; i++)
32			fill_pixel_and_goto_next(&dst, clr, pbytes, pbytes);
33		line += vid_priv->line_length;
34	}
35	ret = vidconsole_sync_copy(dev, start, line);
36	if (ret)
37		return ret;
38
39	return 0;
40}
41
42static int console_move_rows_1(struct udevice *dev, uint rowdst, uint rowsrc,
43				   uint count)
44{
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);
49	void *dst;
50	void *src;
51	int j, ret;
52
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;
57
58	for (j = 0; j < vid_priv->ysize; j++) {
59		ret = vidconsole_memmove(dev, dst, src,
60					fontdata->height * pbytes * count);
61		if (ret)
62			return ret;
63		src += vid_priv->line_length;
64		dst += vid_priv->line_length;
65	}
66
67	return 0;
68}
69
70static int console_putc_xy_1(struct udevice *dev, uint x_frac, uint y, int cp)
71{
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);
78	int x, linenum, ret;
79	void *start, *line;
80	u8 ch = console_utf_to_cp437(cp);
81	uchar *pfont = fontdata->video_fontdata +
82			ch * fontdata->char_pixel_bytes;
83
84	if (x_frac + VID_TO_POS(vc_priv->x_charsize) > vc_priv->xsize_frac)
85		return -EAGAIN;
86	linenum = VID_TO_PIXEL(x_frac) + 1;
87	x = y + 1;
88	start = vid_priv->fb + linenum * vid_priv->line_length - x * pbytes;
89	line = start;
90
91	ret = fill_char_horizontally(pfont, &line, vid_priv, fontdata, FLIPPED_DIRECTION);
92	if (ret)
93		return ret;
94
95	/* 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;
99
100	return VID_TO_POS(fontdata->width);
101}
102
103
104static int console_set_row_2(struct udevice *dev, uint row, int clr)
105{
106	struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
107	struct console_simple_priv *priv = dev_get_priv(dev);
108	struct video_fontdata *fontdata = priv->fontdata;
109	void *start, *line, *dst, *end;
110	int pixels = fontdata->height * vid_priv->xsize;
111	int i, ret;
112	int pbytes = VNBYTES(vid_priv->bpix);
113
114	start = vid_priv->fb + vid_priv->ysize * vid_priv->line_length -
115		(row + 1) * fontdata->height * vid_priv->line_length;
116	line = start;
117	dst = line;
118	for (i = 0; i < pixels; i++)
119		fill_pixel_and_goto_next(&dst, clr, pbytes, pbytes);
120	end = dst;
121	ret = vidconsole_sync_copy(dev, start, end);
122	if (ret)
123		return ret;
124
125	return 0;
126}
127
128static int console_move_rows_2(struct udevice *dev, uint rowdst, uint rowsrc,
129			       uint count)
130{
131	struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
132	struct console_simple_priv *priv = dev_get_priv(dev);
133	struct video_fontdata *fontdata = priv->fontdata;
134	void *dst;
135	void *src;
136	void *end;
137
138	end = vid_priv->fb + vid_priv->ysize * vid_priv->line_length;
139	dst = end - (rowdst + count) * fontdata->height *
140		vid_priv->line_length;
141	src = end - (rowsrc + count) * fontdata->height *
142		vid_priv->line_length;
143	vidconsole_memmove(dev, dst, src,
144			   fontdata->height * vid_priv->line_length * count);
145
146	return 0;
147}
148
149static int console_putc_xy_2(struct udevice *dev, uint x_frac, uint y, int cp)
150{
151	struct vidconsole_priv *vc_priv = dev_get_uclass_priv(dev);
152	struct udevice *vid = dev->parent;
153	struct video_priv *vid_priv = dev_get_uclass_priv(vid);
154	struct console_simple_priv *priv = dev_get_priv(dev);
155	struct video_fontdata *fontdata = priv->fontdata;
156	int pbytes = VNBYTES(vid_priv->bpix);
157	int linenum, x, ret;
158	void *start, *line;
159	u8 ch = console_utf_to_cp437(cp);
160	uchar *pfont = fontdata->video_fontdata +
161			ch * fontdata->char_pixel_bytes;
162
163	if (x_frac + VID_TO_POS(vc_priv->x_charsize) > vc_priv->xsize_frac)
164		return -EAGAIN;
165	linenum = vid_priv->ysize - y - 1;
166	x = vid_priv->xsize - VID_TO_PIXEL(x_frac) - 1;
167	start = vid_priv->fb + linenum * vid_priv->line_length + x * pbytes;
168	line = start;
169
170	ret = fill_char_vertically(pfont, &line, vid_priv, fontdata, FLIPPED_DIRECTION);
171	if (ret)
172		return ret;
173
174	/* Add 4 bytes to allow for the first pixel writen */
175	ret = vidconsole_sync_copy(dev, start + 4, line);
176	if (ret)
177		return ret;
178
179	return VID_TO_POS(fontdata->width);
180}
181
182static int console_set_row_3(struct udevice *dev, uint row, int clr)
183{
184	struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
185	struct console_simple_priv *priv = dev_get_priv(dev);
186	struct video_fontdata *fontdata = priv->fontdata;
187	int pbytes = VNBYTES(vid_priv->bpix);
188	void *start, *dst, *line;
189	int i, j, ret;
190
191	start = vid_priv->fb + row * fontdata->height * pbytes;
192	line = start;
193	for (j = 0; j < vid_priv->ysize; j++) {
194		dst = line;
195		for (i = 0; i < fontdata->height; i++)
196			fill_pixel_and_goto_next(&dst, clr, pbytes, pbytes);
197		line += vid_priv->line_length;
198	}
199	ret = vidconsole_sync_copy(dev, start, line);
200	if (ret)
201		return ret;
202
203	return 0;
204}
205
206static int console_move_rows_3(struct udevice *dev, uint rowdst, uint rowsrc,
207			       uint count)
208{
209	struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
210	struct console_simple_priv *priv = dev_get_priv(dev);
211	struct video_fontdata *fontdata = priv->fontdata;
212	int pbytes = VNBYTES(vid_priv->bpix);
213	void *dst;
214	void *src;
215	int j, ret;
216
217	dst = vid_priv->fb + rowdst * fontdata->height * pbytes;
218	src = vid_priv->fb + rowsrc * fontdata->height * pbytes;
219
220	for (j = 0; j < vid_priv->ysize; j++) {
221		ret = vidconsole_memmove(dev, dst, src,
222					fontdata->height * pbytes * count);
223		if (ret)
224			return ret;
225		src += vid_priv->line_length;
226		dst += vid_priv->line_length;
227	}
228
229	return 0;
230}
231
232static int console_putc_xy_3(struct udevice *dev, uint x_frac, uint y, int cp)
233{
234	struct vidconsole_priv *vc_priv = dev_get_uclass_priv(dev);
235	struct udevice *vid = dev->parent;
236	struct video_priv *vid_priv = dev_get_uclass_priv(vid);
237	struct console_simple_priv *priv = dev_get_priv(dev);
238	struct video_fontdata *fontdata = priv->fontdata;
239	int pbytes = VNBYTES(vid_priv->bpix);
240	int linenum, x, ret;
241	void *start, *line;
242	u8 ch = console_utf_to_cp437(cp);
243	uchar *pfont = fontdata->video_fontdata +
244			ch * fontdata->char_pixel_bytes;
245
246	if (x_frac + VID_TO_POS(vc_priv->x_charsize) > vc_priv->xsize_frac)
247		return -EAGAIN;
248	x = y;
249	linenum = vid_priv->ysize - VID_TO_PIXEL(x_frac) - 1;
250	start = vid_priv->fb + linenum * vid_priv->line_length + y * pbytes;
251	line = start;
252
253	ret = fill_char_horizontally(pfont, &line, vid_priv, fontdata, NORMAL_DIRECTION);
254	if (ret)
255		return ret;
256	/* Add a line to allow for the first pixels writen */
257	ret = vidconsole_sync_copy(dev, start + vid_priv->line_length, line);
258	if (ret)
259		return ret;
260
261	return VID_TO_POS(fontdata->width);
262}
263
264struct vidconsole_ops console_ops_1 = {
265	.putc_xy	= console_putc_xy_1,
266	.move_rows	= console_move_rows_1,
267	.set_row	= console_set_row_1,
268	.get_font_size	= console_simple_get_font_size,
269	.get_font	= console_simple_get_font,
270	.select_font	= console_simple_select_font,
271};
272
273struct vidconsole_ops console_ops_2 = {
274	.putc_xy	= console_putc_xy_2,
275	.move_rows	= console_move_rows_2,
276	.set_row	= console_set_row_2,
277	.get_font_size	= console_simple_get_font_size,
278	.get_font	= console_simple_get_font,
279	.select_font	= console_simple_select_font,
280};
281
282struct vidconsole_ops console_ops_3 = {
283	.putc_xy	= console_putc_xy_3,
284	.move_rows	= console_move_rows_3,
285	.set_row	= console_set_row_3,
286	.get_font_size	= console_simple_get_font_size,
287	.get_font	= console_simple_get_font,
288	.select_font	= console_simple_select_font,
289};
290
291U_BOOT_DRIVER(vidconsole_1) = {
292	.name	= "vidconsole1",
293	.id	= UCLASS_VIDEO_CONSOLE,
294	.ops	= &console_ops_1,
295	.probe	= console_probe,
296	.priv_auto	= sizeof(struct console_simple_priv),
297};
298
299U_BOOT_DRIVER(vidconsole_2) = {
300	.name	= "vidconsole2",
301	.id	= UCLASS_VIDEO_CONSOLE,
302	.ops	= &console_ops_2,
303	.probe	= console_probe,
304	.priv_auto	= sizeof(struct console_simple_priv),
305};
306
307U_BOOT_DRIVER(vidconsole_3) = {
308	.name	= "vidconsole3",
309	.id	= UCLASS_VIDEO_CONSOLE,
310	.ops	= &console_ops_3,
311	.probe	= console_probe,
312	.priv_auto	= sizeof(struct console_simple_priv),
313};
314