1 /*
2 * Copyright (c) 2015 Google, Inc
3 * (C) Copyright 2001-2015
4 * DENX Software Engineering -- wd@denx.de
5 * Compulab Ltd - http://compulab.co.il/
6 * Bernecker & Rainer Industrieelektronik GmbH - http://www.br-automation.com
7 *
8 * SPDX-License-Identifier: GPL-2.0+
9 */
10
11 #include <common.h>
12 #include <dm.h>
13 #include <video.h>
14 #include <video_console.h>
15 #include <video_font.h> /* Get font data, width and height */
16
17 /* By default we scroll by a single line */
18 #ifndef CONFIG_CONSOLE_SCROLL_LINES
19 #define CONFIG_CONSOLE_SCROLL_LINES 1
20 #endif
21
vidconsole_putc_xy(struct udevice * dev,uint x,uint y,char ch)22 int vidconsole_putc_xy(struct udevice *dev, uint x, uint y, char ch)
23 {
24 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
25
26 if (!ops->putc_xy)
27 return -ENOSYS;
28 return ops->putc_xy(dev, x, y, ch);
29 }
30
vidconsole_move_rows(struct udevice * dev,uint rowdst,uint rowsrc,uint count)31 int vidconsole_move_rows(struct udevice *dev, uint rowdst, uint rowsrc,
32 uint count)
33 {
34 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
35
36 if (!ops->move_rows)
37 return -ENOSYS;
38 return ops->move_rows(dev, rowdst, rowsrc, count);
39 }
40
vidconsole_set_row(struct udevice * dev,uint row,int clr)41 int vidconsole_set_row(struct udevice *dev, uint row, int clr)
42 {
43 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
44
45 if (!ops->set_row)
46 return -ENOSYS;
47 return ops->set_row(dev, row, clr);
48 }
49
vidconsole_entry_start(struct udevice * dev)50 static int vidconsole_entry_start(struct udevice *dev)
51 {
52 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
53
54 if (!ops->entry_start)
55 return -ENOSYS;
56 return ops->entry_start(dev);
57 }
58
59 /* Move backwards one space */
vidconsole_back(struct udevice * dev)60 static int vidconsole_back(struct udevice *dev)
61 {
62 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
63 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
64 int ret;
65
66 if (ops->backspace) {
67 ret = ops->backspace(dev);
68 if (ret != -ENOSYS)
69 return ret;
70 }
71
72 priv->xcur_frac -= VID_TO_POS(priv->x_charsize);
73 if (priv->xcur_frac < priv->xstart_frac) {
74 priv->xcur_frac = (priv->cols - 1) *
75 VID_TO_POS(priv->x_charsize);
76 priv->ycur -= priv->y_charsize;
77 if (priv->ycur < 0)
78 priv->ycur = 0;
79 }
80 video_sync(dev->parent);
81
82 return 0;
83 }
84
85 /* Move to a newline, scrolling the display if necessary */
vidconsole_newline(struct udevice * dev)86 static void vidconsole_newline(struct udevice *dev)
87 {
88 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
89 struct udevice *vid_dev = dev->parent;
90 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
91 const int rows = CONFIG_CONSOLE_SCROLL_LINES;
92 int i;
93
94 priv->xcur_frac = priv->xstart_frac;
95 priv->ycur += priv->y_charsize;
96
97 /* Check if we need to scroll the terminal */
98 if ((priv->ycur + priv->y_charsize) / priv->y_charsize > priv->rows) {
99 vidconsole_move_rows(dev, 0, rows, priv->rows - rows);
100 for (i = 0; i < rows; i++)
101 vidconsole_set_row(dev, priv->rows - i - 1,
102 vid_priv->colour_bg);
103 priv->ycur -= rows * priv->y_charsize;
104 }
105 priv->last_ch = 0;
106
107 video_sync(dev->parent);
108 }
109
vidconsole_put_char(struct udevice * dev,char ch)110 int vidconsole_put_char(struct udevice *dev, char ch)
111 {
112 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
113 int ret;
114
115 switch (ch) {
116 case '\a':
117 /* beep */
118 break;
119 case '\r':
120 priv->xcur_frac = priv->xstart_frac;
121 break;
122 case '\n':
123 vidconsole_newline(dev);
124 vidconsole_entry_start(dev);
125 break;
126 case '\t': /* Tab (8 chars alignment) */
127 priv->xcur_frac = ((priv->xcur_frac / priv->tab_width_frac)
128 + 1) * priv->tab_width_frac;
129
130 if (priv->xcur_frac >= priv->xsize_frac)
131 vidconsole_newline(dev);
132 break;
133 case '\b':
134 vidconsole_back(dev);
135 priv->last_ch = 0;
136 break;
137 default:
138 /*
139 * Failure of this function normally indicates an unsupported
140 * colour depth. Check this and return an error to help with
141 * diagnosis.
142 */
143 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
144 if (ret == -EAGAIN) {
145 vidconsole_newline(dev);
146 ret = vidconsole_putc_xy(dev, priv->xcur_frac,
147 priv->ycur, ch);
148 }
149 if (ret < 0)
150 return ret;
151 priv->xcur_frac += ret;
152 priv->last_ch = ch;
153 if (priv->xcur_frac >= priv->xsize_frac)
154 vidconsole_newline(dev);
155 break;
156 }
157
158 return 0;
159 }
160
vidconsole_putc(struct stdio_dev * sdev,const char ch)161 static void vidconsole_putc(struct stdio_dev *sdev, const char ch)
162 {
163 struct udevice *dev = sdev->priv;
164
165 vidconsole_put_char(dev, ch);
166 video_sync(dev->parent);
167 }
168
vidconsole_puts(struct stdio_dev * sdev,const char * s)169 static void vidconsole_puts(struct stdio_dev *sdev, const char *s)
170 {
171 struct udevice *dev = sdev->priv;
172
173 while (*s)
174 vidconsole_put_char(dev, *s++);
175 video_sync(dev->parent);
176 }
177
178 /* Set up the number of rows and colours (rotated drivers override this) */
vidconsole_pre_probe(struct udevice * dev)179 static int vidconsole_pre_probe(struct udevice *dev)
180 {
181 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
182 struct udevice *vid = dev->parent;
183 struct video_priv *vid_priv = dev_get_uclass_priv(vid);
184
185 priv->xsize_frac = VID_TO_POS(vid_priv->xsize);
186
187 return 0;
188 }
189
190 /* Register the device with stdio */
vidconsole_post_probe(struct udevice * dev)191 static int vidconsole_post_probe(struct udevice *dev)
192 {
193 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
194 struct stdio_dev *sdev = &priv->sdev;
195
196 if (!priv->tab_width_frac)
197 priv->tab_width_frac = VID_TO_POS(priv->x_charsize) * 8;
198
199 if (dev->seq) {
200 snprintf(sdev->name, sizeof(sdev->name), "vidconsole%d",
201 dev->seq);
202 } else {
203 strcpy(sdev->name, "vidconsole");
204 }
205
206 sdev->flags = DEV_FLAGS_OUTPUT;
207 sdev->putc = vidconsole_putc;
208 sdev->puts = vidconsole_puts;
209 sdev->priv = dev;
210
211 return stdio_register(sdev);
212 }
213
214 UCLASS_DRIVER(vidconsole) = {
215 .id = UCLASS_VIDEO_CONSOLE,
216 .name = "vidconsole0",
217 .pre_probe = vidconsole_pre_probe,
218 .post_probe = vidconsole_post_probe,
219 .per_device_auto_alloc_size = sizeof(struct vidconsole_priv),
220 };
221
vidconsole_position_cursor(struct udevice * dev,unsigned col,unsigned row)222 void vidconsole_position_cursor(struct udevice *dev, unsigned col, unsigned row)
223 {
224 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
225 struct udevice *vid_dev = dev->parent;
226 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
227
228 priv->xcur_frac = VID_TO_POS(min_t(short, col, vid_priv->xsize - 1));
229 priv->ycur = min_t(short, row, vid_priv->ysize - 1);
230 }
231
do_video_setcursor(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])232 static int do_video_setcursor(cmd_tbl_t *cmdtp, int flag, int argc,
233 char *const argv[])
234 {
235 unsigned int col, row;
236 struct udevice *dev;
237
238 if (argc != 3)
239 return CMD_RET_USAGE;
240
241 if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev))
242 return CMD_RET_FAILURE;
243 col = simple_strtoul(argv[1], NULL, 10);
244 row = simple_strtoul(argv[2], NULL, 10);
245 vidconsole_position_cursor(dev, col, row);
246
247 return 0;
248 }
249
do_video_puts(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])250 static int do_video_puts(cmd_tbl_t *cmdtp, int flag, int argc,
251 char *const argv[])
252 {
253 struct udevice *dev;
254 const char *s;
255
256 if (argc != 2)
257 return CMD_RET_USAGE;
258
259 if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev))
260 return CMD_RET_FAILURE;
261 for (s = argv[1]; *s; s++)
262 vidconsole_put_char(dev, *s);
263 video_sync(dev->parent);
264
265 video_sync(dev->parent);
266
267 return 0;
268 }
269
270 U_BOOT_CMD(
271 setcurs, 3, 1, do_video_setcursor,
272 "set cursor position within screen",
273 " <col> <row> in character"
274 );
275
276 U_BOOT_CMD(
277 lcdputs, 2, 1, do_video_puts,
278 "print string on video framebuffer",
279 " <string>"
280 );
281