]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/lcd_console.c
b7dda7ad85f27592575d60575d8172916aa3e814
[karo-tx-uboot.git] / common / lcd_console.c
1 /*
2  * (C) Copyright 2001-2014
3  * DENX Software Engineering -- wd@denx.de
4  * Compulab Ltd - http://compulab.co.il/
5  *
6  * SPDX-License-Identifier:     GPL-2.0+
7  */
8
9 #include <common.h>
10 #include <lcd.h>
11 #include <video_font.h>         /* Get font data, width and height */
12
13 #define CONSOLE_ROW_SIZE        (VIDEO_FONT_HEIGHT * lcd_line_length)
14 #define CONSOLE_ROW_FIRST       lcd_console_address
15 #define CONSOLE_SIZE            (CONSOLE_ROW_SIZE * console_rows)
16
17 static short console_curr_col;
18 static short console_curr_row;
19 static short console_cols;
20 static short console_rows;
21 static void *lcd_console_address;
22
23 void lcd_init_console(void *address, int rows, int cols)
24 {
25         console_curr_col = 0;
26         console_curr_row = 0;
27         console_cols = cols;
28         console_rows = rows;
29         lcd_console_address = address;
30 }
31
32 void lcd_set_col(short col)
33 {
34         console_curr_col = col;
35 }
36
37 void lcd_set_row(short row)
38 {
39         console_curr_row = row;
40 }
41
42 void lcd_position_cursor(unsigned col, unsigned row)
43 {
44         console_curr_col = min_t(short, col, console_cols - 1);
45         console_curr_row = min_t(short, row, console_rows - 1);
46 }
47
48 int lcd_get_screen_rows(void)
49 {
50         return console_rows;
51 }
52
53 int lcd_get_screen_columns(void)
54 {
55         return console_cols;
56 }
57
58 static void lcd_putc_xy(ushort x, ushort y, char c)
59 {
60         uchar *dest;
61         ushort row;
62         int fg_color = lcd_getfgcolor();
63         int bg_color = lcd_getbgcolor();
64         int i;
65
66         dest = (uchar *)(lcd_console_address +
67                          y * lcd_line_length + x * NBITS(LCD_BPP) / 8);
68
69         for (row = 0; row < VIDEO_FONT_HEIGHT; ++row, dest += lcd_line_length) {
70 #if LCD_BPP == LCD_COLOR16
71                 ushort *d = (ushort *)dest;
72 #elif LCD_BPP == LCD_COLOR32
73                 u32 *d = (u32 *)dest;
74 #else
75                 uchar *d = dest;
76 #endif
77                 uchar bits;
78                 bits = video_fontdata[c * VIDEO_FONT_HEIGHT + row];
79
80                 for (i = 0; i < 8; ++i) {
81                         *d++ = (bits & 0x80) ? fg_color : bg_color;
82                         bits <<= 1;
83                 }
84         }
85 }
86
87 static void console_scrollup(void)
88 {
89         const int rows = CONFIG_CONSOLE_SCROLL_LINES;
90         int bg_color = lcd_getbgcolor();
91
92         /* Copy up rows ignoring those that will be overwritten */
93         memcpy(CONSOLE_ROW_FIRST,
94                lcd_console_address + CONSOLE_ROW_SIZE * rows,
95                CONSOLE_SIZE - CONSOLE_ROW_SIZE * rows);
96
97         /* Clear the last rows */
98 #if (LCD_BPP != LCD_COLOR32)
99         memset(lcd_console_address + CONSOLE_SIZE - CONSOLE_ROW_SIZE * rows,
100                bg_color, CONSOLE_ROW_SIZE * rows);
101 #else
102         u32 *ppix = lcd_console_address +
103                     CONSOLE_SIZE - CONSOLE_ROW_SIZE * rows;
104         u32 i;
105         for (i = 0;
106             i < (CONSOLE_ROW_SIZE * rows) / NBYTES(panel_info.vl_bpix);
107             i++) {
108                 *ppix++ = bg_color;
109         }
110 #endif
111         lcd_sync();
112         console_curr_row -= rows;
113 }
114
115 static inline void console_back(void)
116 {
117         if (--console_curr_col < 0) {
118                 console_curr_col = console_cols - 1;
119                 if (--console_curr_row < 0)
120                         console_curr_row = 0;
121         }
122
123         lcd_putc_xy(console_curr_col * VIDEO_FONT_WIDTH,
124                     console_curr_row * VIDEO_FONT_HEIGHT, ' ');
125 }
126
127 static inline void console_newline(void)
128 {
129         console_curr_col = 0;
130
131         /* Check if we need to scroll the terminal */
132         if (++console_curr_row >= console_rows)
133                 console_scrollup();
134         else
135                 lcd_sync();
136 }
137
138 void lcd_putc(const char c)
139 {
140         if (!lcd_is_enabled) {
141                 serial_putc(c);
142
143                 return;
144         }
145
146         switch (c) {
147         case '\r':
148                 console_curr_col = 0;
149
150                 return;
151         case '\n':
152                 console_newline();
153
154                 return;
155         case '\t':      /* Tab (8 chars alignment) */
156                 console_curr_col +=  8;
157                 console_curr_col &= ~7;
158
159                 if (console_curr_col >= console_cols)
160                         console_newline();
161
162                 return;
163         case '\b':
164                 console_back();
165
166                 return;
167         default:
168                 lcd_putc_xy(console_curr_col * VIDEO_FONT_WIDTH,
169                             console_curr_row * VIDEO_FONT_HEIGHT, c);
170                 if (++console_curr_col >= console_cols)
171                         console_newline();
172         }
173 }
174
175 void lcd_puts(const char *s)
176 {
177         if (!lcd_is_enabled) {
178                 serial_puts(s);
179
180                 return;
181         }
182
183         while (*s)
184                 lcd_putc(*s++);
185
186         lcd_sync();
187 }
188
189 void lcd_printf(const char *fmt, ...)
190 {
191         va_list args;
192         char buf[CONFIG_SYS_PBSIZE];
193
194         va_start(args, fmt);
195         vsprintf(buf, fmt, args);
196         va_end(args);
197
198         lcd_puts(buf);
199 }
200
201 static int do_lcd_setcursor(cmd_tbl_t *cmdtp, int flag, int argc,
202                             char *const argv[])
203 {
204         unsigned int col, row;
205
206         if (argc != 3)
207                 return CMD_RET_USAGE;
208
209         col = simple_strtoul(argv[1], NULL, 10);
210         row = simple_strtoul(argv[2], NULL, 10);
211         lcd_position_cursor(col, row);
212
213         return 0;
214 }
215
216 static int do_lcd_puts(cmd_tbl_t *cmdtp, int flag, int argc,
217                        char *const argv[])
218 {
219         if (argc != 2)
220                 return CMD_RET_USAGE;
221
222         lcd_puts(argv[1]);
223
224         return 0;
225 }
226
227 U_BOOT_CMD(
228         setcurs, 3,     1,      do_lcd_setcursor,
229         "set cursor position within screen",
230         "    <col> <row> in character"
231 );
232
233 U_BOOT_CMD(
234         lcdputs, 2,     1,      do_lcd_puts,
235         "print string on lcd-framebuffer",
236         "    <string>"
237 );
238