1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2013 Intel Corporation; author Matt Fleming
6 #include <linux/console.h>
8 #include <linux/font.h>
10 #include <linux/kernel.h>
11 #include <linux/serial_core.h>
12 #include <linux/screen_info.h>
14 #include <asm/early_ioremap.h>
16 static const struct font_desc
*font
;
17 static u32 efi_x
, efi_y
;
19 static pgprot_t fb_prot
;
21 static __ref
void *efi_earlycon_map(unsigned long start
, unsigned long len
)
23 return early_memremap_prot(fb_base
+ start
, len
, pgprot_val(fb_prot
));
26 static __ref
void efi_earlycon_unmap(void *addr
, unsigned long len
)
28 early_memunmap(addr
, len
);
31 static void efi_earlycon_clear_scanline(unsigned int y
)
36 len
= screen_info
.lfb_linelength
;
37 dst
= efi_earlycon_map(y
*len
, len
);
42 efi_earlycon_unmap(dst
, len
);
45 static void efi_earlycon_scroll_up(void)
47 unsigned long *dst
, *src
;
51 len
= screen_info
.lfb_linelength
;
52 height
= screen_info
.lfb_height
;
54 for (i
= 0; i
< height
- font
->height
; i
++) {
55 dst
= efi_earlycon_map(i
*len
, len
);
59 src
= efi_earlycon_map((i
+ font
->height
) * len
, len
);
61 efi_earlycon_unmap(dst
, len
);
65 memmove(dst
, src
, len
);
67 efi_earlycon_unmap(src
, len
);
68 efi_earlycon_unmap(dst
, len
);
72 static void efi_earlycon_write_char(u32
*dst
, unsigned char c
, unsigned int h
)
74 const u32 color_black
= 0x00000000;
75 const u32 color_white
= 0x00ffffff;
80 src
= font
->data
+ c
* font
->height
;
83 for (m
= 0; m
< 8; m
++) {
84 if ((s8
>> (7 - m
)) & 1)
93 efi_earlycon_write(struct console
*con
, const char *str
, unsigned int num
)
95 struct screen_info
*si
;
101 len
= si
->lfb_linelength
;
104 unsigned int linemax
;
105 unsigned int h
, count
= 0;
107 for (s
= str
; *s
&& *s
!= '\n'; s
++) {
113 linemax
= (si
->lfb_width
- efi_x
) / font
->width
;
117 for (h
= 0; h
< font
->height
; h
++) {
120 dst
= efi_earlycon_map((efi_y
+ h
) * len
, len
);
129 efi_earlycon_write_char(dst
+ x
*4, *s
, h
);
134 efi_earlycon_unmap(dst
, len
);
138 efi_x
+= count
* font
->width
;
141 if (num
> 0 && *s
== '\n') {
143 efi_y
+= font
->height
;
148 if (efi_x
+ font
->width
> si
->lfb_width
) {
150 efi_y
+= font
->height
;
153 if (efi_y
+ font
->height
> si
->lfb_height
) {
156 efi_y
-= font
->height
;
157 efi_earlycon_scroll_up();
159 for (i
= 0; i
< font
->height
; i
++)
160 efi_earlycon_clear_scanline(efi_y
+ i
);
165 static int __init
efi_earlycon_setup(struct earlycon_device
*device
,
168 struct screen_info
*si
;
172 if (screen_info
.orig_video_isVGA
!= VIDEO_TYPE_EFI
)
175 fb_base
= screen_info
.lfb_base
;
176 if (screen_info
.capabilities
& VIDEO_CAPABILITY_64BIT_BASE
)
177 fb_base
|= (u64
)screen_info
.ext_lfb_base
<< 32;
179 if (opt
&& !strcmp(opt
, "ram"))
180 fb_prot
= PAGE_KERNEL
;
182 fb_prot
= pgprot_writecombine(PAGE_KERNEL
);
185 xres
= si
->lfb_width
;
186 yres
= si
->lfb_height
;
189 * efi_earlycon_write_char() implicitly assumes a framebuffer with
192 if (si
->lfb_depth
!= 32)
195 font
= get_default_font(xres
, yres
, -1, -1);
199 efi_y
= rounddown(yres
, font
->height
) - font
->height
;
200 for (i
= 0; i
< (yres
- efi_y
) / font
->height
; i
++)
201 efi_earlycon_scroll_up();
203 device
->con
->write
= efi_earlycon_write
;
206 EARLYCON_DECLARE(efifb
, efi_earlycon_setup
);