xref: /openbmc/linux/drivers/video/console/sticon.c (revision 017cdefe)
1 /*
2  *  linux/drivers/video/console/sticon.c - console driver using HP's STI firmware
3  *
4  *	Copyright (C) 2000 Philipp Rumpf <prumpf@tux.org>
5  *	Copyright (C) 2002-2020 Helge Deller <deller@gmx.de>
6  *
7  *  Based on linux/drivers/video/vgacon.c and linux/drivers/video/fbcon.c,
8  *  which were
9  *
10  *	Created 28 Sep 1997 by Geert Uytterhoeven
11  *	Rewritten by Martin Mares <mj@ucw.cz>, July 1998
12  *	Copyright (C) 1991, 1992  Linus Torvalds
13  *			    1995  Jay Estabrook
14  *	Copyright (C) 1995 Geert Uytterhoeven
15  *	Copyright (C) 1993 Bjoern Brauel
16  *			   Roman Hodek
17  *	Copyright (C) 1993 Hamish Macdonald
18  *			   Greg Harp
19  *	Copyright (C) 1994 David Carter [carter@compsci.bristol.ac.uk]
20  *
21  *	      with work by William Rucklidge (wjr@cs.cornell.edu)
22  *			   Geert Uytterhoeven
23  *			   Jes Sorensen (jds@kom.auc.dk)
24  *			   Martin Apel
25  *	      with work by Guenther Kelleter
26  *			   Martin Schaller
27  *			   Andreas Schwab
28  *			   Emmanuel Marty (core@ggi-project.org)
29  *			   Jakub Jelinek (jj@ultra.linux.cz)
30  *			   Martin Mares <mj@ucw.cz>
31  *
32  *  This file is subject to the terms and conditions of the GNU General Public
33  *  License.  See the file COPYING in the main directory of this archive for
34  *  more details.
35  *
36  */
37 
38 #include <linux/init.h>
39 #include <linux/kernel.h>
40 #include <linux/console.h>
41 #include <linux/errno.h>
42 #include <linux/vt_kern.h>
43 #include <linux/kd.h>
44 #include <linux/selection.h>
45 #include <linux/module.h>
46 #include <linux/slab.h>
47 #include <linux/font.h>
48 #include <linux/crc32.h>
49 #include <linux/fb.h>
50 
51 #include <asm/io.h>
52 
53 #include "../fbdev/sticore.h"
54 
55 /* switching to graphics mode */
56 #define BLANK 0
57 static int vga_is_gfx;
58 
59 #define STI_DEF_FONT	sticon_sti->font
60 
61 /* borrowed from fbcon.c */
62 #define FNTREFCOUNT(fd)	(fd->refcount)
63 #define FNTCRC(fd)	(fd->crc)
64 static struct sti_cooked_font *font_data[MAX_NR_CONSOLES];
65 
66 /* this is the sti_struct used for this console */
67 static struct sti_struct *sticon_sti;
68 
69 static const char *sticon_startup(void)
70 {
71     return "STI console";
72 }
73 
74 static void sticon_putc(struct vc_data *conp, int c, int ypos, int xpos)
75 {
76     if (vga_is_gfx || console_blanked)
77 	    return;
78 
79     if (conp->vc_mode != KD_TEXT)
80     	    return;
81 
82     sti_putc(sticon_sti, c, ypos, xpos, font_data[conp->vc_num]);
83 }
84 
85 static void sticon_putcs(struct vc_data *conp, const unsigned short *s,
86 			 int count, int ypos, int xpos)
87 {
88     if (vga_is_gfx || console_blanked)
89 	    return;
90 
91     if (conp->vc_mode != KD_TEXT)
92     	    return;
93 
94     while (count--) {
95 	sti_putc(sticon_sti, scr_readw(s++), ypos, xpos++,
96 		 font_data[conp->vc_num]);
97     }
98 }
99 
100 static void sticon_cursor(struct vc_data *conp, int mode)
101 {
102     unsigned short car1;
103 
104     /* no cursor update if screen is blanked */
105     if (vga_is_gfx || console_blanked)
106 	return;
107 
108     car1 = conp->vc_screenbuf[conp->state.x + conp->state.y * conp->vc_cols];
109     switch (mode) {
110     case CM_ERASE:
111 	sti_putc(sticon_sti, car1, conp->state.y, conp->state.x,
112 		 font_data[conp->vc_num]);
113 	break;
114     case CM_MOVE:
115     case CM_DRAW:
116 	switch (CUR_SIZE(conp->vc_cursor_type)) {
117 	case CUR_UNDERLINE:
118 	case CUR_LOWER_THIRD:
119 	case CUR_LOWER_HALF:
120 	case CUR_TWO_THIRDS:
121 	case CUR_BLOCK:
122 	    sti_putc(sticon_sti, (car1 & 255) + (0 << 8) + (7 << 11),
123 		     conp->state.y, conp->state.x, font_data[conp->vc_num]);
124 	    break;
125 	}
126 	break;
127     }
128 }
129 
130 static bool sticon_scroll(struct vc_data *conp, unsigned int t,
131 		unsigned int b, enum con_scroll dir, unsigned int count)
132 {
133     struct sti_struct *sti = sticon_sti;
134 
135     if (vga_is_gfx)
136         return false;
137 
138     sticon_cursor(conp, CM_ERASE);
139 
140     switch (dir) {
141     case SM_UP:
142 	sti_bmove(sti, t + count, 0, t, 0, b - t - count, conp->vc_cols,
143 		  font_data[conp->vc_num]);
144 	sti_clear(sti, b - count, 0, count, conp->vc_cols,
145 		  conp->vc_video_erase_char, font_data[conp->vc_num]);
146 	break;
147 
148     case SM_DOWN:
149 	sti_bmove(sti, t, 0, t + count, 0, b - t - count, conp->vc_cols,
150 		  font_data[conp->vc_num]);
151 	sti_clear(sti, t, 0, count, conp->vc_cols,
152 		  conp->vc_video_erase_char, font_data[conp->vc_num]);
153 	break;
154     }
155 
156     return false;
157 }
158 
159 static int sticon_set_def_font(int unit, struct console_font *op)
160 {
161 	if (font_data[unit] != STI_DEF_FONT) {
162 		if (--FNTREFCOUNT(font_data[unit]) == 0) {
163 			kfree(font_data[unit]->raw_ptr);
164 			kfree(font_data[unit]);
165 		}
166 		font_data[unit] = STI_DEF_FONT;
167 	}
168 
169 	return 0;
170 }
171 
172 static int sticon_set_font(struct vc_data *vc, struct console_font *op,
173 			   unsigned int vpitch)
174 {
175 	struct sti_struct *sti = sticon_sti;
176 	int vc_cols, vc_rows, vc_old_cols, vc_old_rows;
177 	int unit = vc->vc_num;
178 	int w = op->width;
179 	int h = op->height;
180 	int size, i, bpc, pitch;
181 	struct sti_rom_font *new_font;
182 	struct sti_cooked_font *cooked_font;
183 	unsigned char *data = op->data, *p;
184 
185 	if ((w < 6) || (h < 6) || (w > 32) || (h > 32) || (vpitch != 32)
186 	    || (op->charcount != 256 && op->charcount != 512))
187 		return -EINVAL;
188 	pitch = ALIGN(w, 8) / 8;
189 	bpc = pitch * h;
190 	size = bpc * op->charcount;
191 
192 	new_font = kmalloc(sizeof(*new_font) + size, STI_LOWMEM);
193 	if (!new_font)
194 		return -ENOMEM;
195 
196 	new_font->first_char = 0;
197 	new_font->last_char = op->charcount - 1;
198 	new_font->width = w;
199 	new_font->height = h;
200 	new_font->font_type = STI_FONT_HPROMAN8;
201 	new_font->bytes_per_char = bpc;
202 	new_font->underline_height = 0;
203 	new_font->underline_pos = 0;
204 
205 	cooked_font = kzalloc(sizeof(*cooked_font), GFP_KERNEL);
206 	if (!cooked_font) {
207 		kfree(new_font);
208 		return -ENOMEM;
209 	}
210 	cooked_font->raw = new_font;
211 	cooked_font->raw_ptr = new_font;
212 	cooked_font->width = w;
213 	cooked_font->height = h;
214 	FNTREFCOUNT(cooked_font) = 0;	/* usage counter */
215 
216 	p = (unsigned char *) new_font;
217 	p += sizeof(*new_font);
218 	for (i = 0; i < op->charcount; i++) {
219 		memcpy(p, data, bpc);
220 		data += pitch*32;
221 		p += bpc;
222 	}
223 	FNTCRC(cooked_font) = crc32(0, new_font, size + sizeof(*new_font));
224 	sti_font_convert_bytemode(sti, cooked_font);
225 	new_font = cooked_font->raw_ptr;
226 
227 	/* check if font is already used by other console */
228 	for (i = 0; i < MAX_NR_CONSOLES; i++) {
229 		if (font_data[i] != STI_DEF_FONT
230 		    && (FNTCRC(font_data[i]) == FNTCRC(cooked_font))) {
231 			kfree(new_font);
232 			kfree(cooked_font);
233 			/* current font is the same as the new one */
234 			if (i == unit)
235 				return 0;
236 			cooked_font = font_data[i];
237 			new_font = cooked_font->raw_ptr;
238 			break;
239 		}
240 	}
241 
242 	/* clear screen with old font: we now may have less rows */
243 	vc_old_rows = vc->vc_rows;
244 	vc_old_cols = vc->vc_cols;
245 	sti_clear(sticon_sti, 0, 0, vc_old_rows, vc_old_cols,
246 		  vc->vc_video_erase_char, font_data[vc->vc_num]);
247 
248 	/* delete old font in case it is a user font */
249 	sticon_set_def_font(unit, NULL);
250 
251 	FNTREFCOUNT(cooked_font)++;
252 	font_data[unit] = cooked_font;
253 
254 	vc_cols = sti_onscreen_x(sti) / cooked_font->width;
255 	vc_rows = sti_onscreen_y(sti) / cooked_font->height;
256 	vc_resize(vc, vc_cols, vc_rows);
257 
258 	/* need to repaint screen if cols & rows are same as old font */
259 	if (vc_cols == vc_old_cols && vc_rows == vc_old_rows)
260 		update_screen(vc);
261 
262 	return 0;
263 }
264 
265 static int sticon_font_default(struct vc_data *vc, struct console_font *op, char *name)
266 {
267 	return sticon_set_def_font(vc->vc_num, op);
268 }
269 
270 static int sticon_font_set(struct vc_data *vc, struct console_font *font,
271 			   unsigned int vpitch, unsigned int flags)
272 {
273 	return sticon_set_font(vc, font, vpitch);
274 }
275 
276 static void sticon_init(struct vc_data *c, int init)
277 {
278     struct sti_struct *sti = sticon_sti;
279     int vc_cols, vc_rows;
280 
281     sti_set(sti, 0, 0, sti_onscreen_y(sti), sti_onscreen_x(sti), 0);
282     vc_cols = sti_onscreen_x(sti) / sti->font->width;
283     vc_rows = sti_onscreen_y(sti) / sti->font->height;
284     c->vc_can_do_color = 1;
285 
286     if (init) {
287 	c->vc_cols = vc_cols;
288 	c->vc_rows = vc_rows;
289     } else {
290 	vc_resize(c, vc_cols, vc_rows);
291     }
292 }
293 
294 static void sticon_deinit(struct vc_data *c)
295 {
296     int i;
297 
298     /* free memory used by user font */
299     for (i = 0; i < MAX_NR_CONSOLES; i++)
300 	sticon_set_def_font(i, NULL);
301 }
302 
303 static void sticon_clear(struct vc_data *conp, int sy, int sx, int height,
304 			 int width)
305 {
306     if (!height || !width)
307 	return;
308 
309     sti_clear(sticon_sti, sy, sx, height, width,
310 	      conp->vc_video_erase_char, font_data[conp->vc_num]);
311 }
312 
313 static int sticon_switch(struct vc_data *conp)
314 {
315     return 1;	/* needs refreshing */
316 }
317 
318 static int sticon_blank(struct vc_data *c, int blank, int mode_switch)
319 {
320     if (blank == 0) {
321 	if (mode_switch)
322 	    vga_is_gfx = 0;
323 	return 1;
324     }
325     sti_clear(sticon_sti, 0, 0, c->vc_rows, c->vc_cols, BLANK,
326 	      font_data[c->vc_num]);
327     if (mode_switch)
328 	vga_is_gfx = 1;
329     return 1;
330 }
331 
332 static u8 sticon_build_attr(struct vc_data *conp, u8 color,
333 			    enum vc_intensity intens,
334 			    bool blink, bool underline, bool reverse,
335 			    bool italic)
336 {
337 	u8 fg = color & 7;
338 	u8 bg = (color & 0x70) >> 4;
339 
340 	if (reverse)
341 		return (fg << 3) | bg;
342 	else
343 		return (bg << 3) | fg;
344 }
345 
346 static void sticon_invert_region(struct vc_data *conp, u16 *p, int count)
347 {
348     int col = 1; /* vga_can_do_color; */
349 
350     while (count--) {
351 	u16 a = scr_readw(p);
352 
353 	if (col)
354 		a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) | (((a) & 0x0700) << 4);
355 	else
356 		a = ((a & 0x0700) == 0x0100) ? 0x7000 : 0x7700;
357 
358 	scr_writew(a, p++);
359     }
360 }
361 
362 static const struct consw sti_con = {
363 	.owner			= THIS_MODULE,
364 	.con_startup		= sticon_startup,
365 	.con_init		= sticon_init,
366 	.con_deinit		= sticon_deinit,
367 	.con_clear		= sticon_clear,
368 	.con_putc		= sticon_putc,
369 	.con_putcs		= sticon_putcs,
370 	.con_cursor		= sticon_cursor,
371 	.con_scroll		= sticon_scroll,
372 	.con_switch		= sticon_switch,
373 	.con_blank		= sticon_blank,
374 	.con_font_set		= sticon_font_set,
375 	.con_font_default	= sticon_font_default,
376 	.con_build_attr		= sticon_build_attr,
377 	.con_invert_region	= sticon_invert_region,
378 };
379 
380 
381 
382 static int __init sticonsole_init(void)
383 {
384     int err, i;
385 
386     /* already initialized ? */
387     if (sticon_sti)
388 	 return 0;
389 
390     sticon_sti = sti_get_rom(0);
391     if (!sticon_sti)
392 	return -ENODEV;
393 
394     for (i = 0; i < MAX_NR_CONSOLES; i++)
395 	font_data[i] = STI_DEF_FONT;
396 
397     pr_info("sticon: Initializing STI text console on %s at [%s]\n",
398 	sticon_sti->sti_data->inq_outptr.dev_name,
399 	sticon_sti->pa_path);
400     console_lock();
401     err = do_take_over_console(&sti_con, 0, MAX_NR_CONSOLES - 1,
402 		PAGE0->mem_cons.cl_class != CL_DUPLEX);
403     console_unlock();
404 
405     return err;
406 }
407 
408 module_init(sticonsole_init);
409 MODULE_LICENSE("GPL");
410