1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/console.h>
3 #include <linux/kernel.h>
4 #include <linux/init.h>
5 #include <linux/string.h>
6 #include <linux/screen_info.h>
7 #include <linux/usb/ch9.h>
8 #include <linux/pci_regs.h>
9 #include <linux/pci_ids.h>
10 #include <linux/errno.h>
12 #include <asm/processor.h>
13 #include <asm/fcntl.h>
14 #include <asm/setup.h>
15 #include <xen/hvc-console.h>
16 #include <asm/pci-direct.h>
17 #include <asm/fixmap.h>
18 #include <asm/intel-mid.h>
19 #include <asm/pgtable.h>
20 #include <linux/usb/ehci_def.h>
21 #include <linux/usb/xhci-dbgp.h>
22 #include <linux/efi.h>
24 #include <asm/pci_x86.h>
26 /* Simple VGA output */
27 #define VGABASE (__ISA_IO_base + 0xb8000)
29 static int max_ypos
= 25, max_xpos
= 80;
30 static int current_ypos
= 25, current_xpos
;
32 static void early_vga_write(struct console
*con
, const char *str
, unsigned n
)
37 while ((c
= *str
++) != '\0' && n
-- > 0) {
38 if (current_ypos
>= max_ypos
) {
39 /* scroll 1 line up */
40 for (k
= 1, j
= 0; k
< max_ypos
; k
++, j
++) {
41 for (i
= 0; i
< max_xpos
; i
++) {
42 writew(readw(VGABASE
+2*(max_xpos
*k
+i
)),
43 VGABASE
+ 2*(max_xpos
*j
+ i
));
46 for (i
= 0; i
< max_xpos
; i
++)
47 writew(0x720, VGABASE
+ 2*(max_xpos
*j
+ i
));
48 current_ypos
= max_ypos
-1;
50 #ifdef CONFIG_KGDB_KDB
54 } else if (c
== '\r') {
61 } else if (c
!= '\r') {
62 writew(((0x7 << 8) | (unsigned short) c
),
63 VGABASE
+ 2*(max_xpos
*current_ypos
+
65 if (current_xpos
>= max_xpos
) {
73 static struct console early_vga_console
= {
75 .write
= early_vga_write
,
76 .flags
= CON_PRINTBUFFER
,
80 /* Serial functions loosely based on a similar package from Klaus P. Gerlicher */
82 static unsigned long early_serial_base
= 0x3f8; /* ttyS0 */
88 #define TXR 0 /* Transmit register (WRITE) */
89 #define RXR 0 /* Receive register (READ) */
90 #define IER 1 /* Interrupt Enable */
91 #define IIR 2 /* Interrupt ID */
92 #define FCR 2 /* FIFO control */
93 #define LCR 3 /* Line control */
94 #define MCR 4 /* Modem control */
95 #define LSR 5 /* Line Status */
96 #define MSR 6 /* Modem Status */
97 #define DLL 0 /* Divisor Latch Low */
98 #define DLH 1 /* Divisor latch High */
100 static unsigned int io_serial_in(unsigned long addr
, int offset
)
102 return inb(addr
+ offset
);
105 static void io_serial_out(unsigned long addr
, int offset
, int value
)
107 outb(value
, addr
+ offset
);
110 static unsigned int (*serial_in
)(unsigned long addr
, int offset
) = io_serial_in
;
111 static void (*serial_out
)(unsigned long addr
, int offset
, int value
) = io_serial_out
;
113 static int early_serial_putc(unsigned char ch
)
115 unsigned timeout
= 0xffff;
117 while ((serial_in(early_serial_base
, LSR
) & XMTRDY
) == 0 && --timeout
)
119 serial_out(early_serial_base
, TXR
, ch
);
120 return timeout
? 0 : -1;
123 static void early_serial_write(struct console
*con
, const char *s
, unsigned n
)
125 while (*s
&& n
-- > 0) {
127 early_serial_putc('\r');
128 early_serial_putc(*s
);
133 static __init
void early_serial_hw_init(unsigned divisor
)
137 serial_out(early_serial_base
, LCR
, 0x3); /* 8n1 */
138 serial_out(early_serial_base
, IER
, 0); /* no interrupt */
139 serial_out(early_serial_base
, FCR
, 0); /* no fifo */
140 serial_out(early_serial_base
, MCR
, 0x3); /* DTR + RTS */
142 c
= serial_in(early_serial_base
, LCR
);
143 serial_out(early_serial_base
, LCR
, c
| DLAB
);
144 serial_out(early_serial_base
, DLL
, divisor
& 0xff);
145 serial_out(early_serial_base
, DLH
, (divisor
>> 8) & 0xff);
146 serial_out(early_serial_base
, LCR
, c
& ~DLAB
);
149 #define DEFAULT_BAUD 9600
151 static __init
void early_serial_init(char *s
)
154 unsigned long baud
= DEFAULT_BAUD
;
162 if (!strncmp(s
, "0x", 2)) {
163 early_serial_base
= simple_strtoul(s
, &e
, 16);
165 static const int __initconst bases
[] = { 0x3f8, 0x2f8 };
167 if (!strncmp(s
, "ttyS", 4))
169 port
= simple_strtoul(s
, &e
, 10);
170 if (port
> 1 || s
== e
)
172 early_serial_base
= bases
[port
];
174 s
+= strcspn(s
, ",");
180 baud
= simple_strtoull(s
, &e
, 0);
182 if (baud
== 0 || s
== e
)
186 /* Convert from baud to divisor value */
187 divisor
= 115200 / baud
;
189 /* These will always be IO based ports */
190 serial_in
= io_serial_in
;
191 serial_out
= io_serial_out
;
194 early_serial_hw_init(divisor
);
198 static void mem32_serial_out(unsigned long addr
, int offset
, int value
)
200 u32 __iomem
*vaddr
= (u32 __iomem
*)addr
;
201 /* shift implied by pointer type */
202 writel(value
, vaddr
+ offset
);
205 static unsigned int mem32_serial_in(unsigned long addr
, int offset
)
207 u32 __iomem
*vaddr
= (u32 __iomem
*)addr
;
208 /* shift implied by pointer type */
209 return readl(vaddr
+ offset
);
213 * early_pci_serial_init()
215 * This function is invoked when the early_printk param starts with "pciserial"
216 * The rest of the param should be "[force],B:D.F,baud", where B, D & F describe
217 * the location of a PCI device that must be a UART device. "force" is optional
218 * and overrides the use of an UART device with a wrong PCI class code.
220 static __init
void early_pci_serial_init(char *s
)
223 unsigned long baud
= DEFAULT_BAUD
;
236 /* Force the use of an UART device with wrong class code */
237 if (!strncmp(s
, "force,", 6)) {
243 * Part the param to get the BDF values
245 bus
= (u8
)simple_strtoul(s
, &e
, 16);
250 slot
= (u8
)simple_strtoul(s
, &e
, 16);
255 func
= (u8
)simple_strtoul(s
, &e
, 16);
258 /* A baud might be following */
263 * Find the device from the BDF
265 cmdreg
= read_pci_config(bus
, slot
, func
, PCI_COMMAND
);
266 classcode
= read_pci_config(bus
, slot
, func
, PCI_CLASS_REVISION
);
267 bar0
= read_pci_config(bus
, slot
, func
, PCI_BASE_ADDRESS_0
);
270 * Verify it is a UART type device
272 if (((classcode
>> 16 != PCI_CLASS_COMMUNICATION_MODEM
) &&
273 (classcode
>> 16 != PCI_CLASS_COMMUNICATION_SERIAL
)) ||
274 (((classcode
>> 8) & 0xff) != 0x02)) /* 16550 I/F at BAR0 */ {
280 * Determine if it is IO or memory mapped
283 /* it is IO mapped */
284 serial_in
= io_serial_in
;
285 serial_out
= io_serial_out
;
286 early_serial_base
= bar0
&0xfffffffc;
287 write_pci_config(bus
, slot
, func
, PCI_COMMAND
,
288 cmdreg
|PCI_COMMAND_IO
);
290 /* It is memory mapped - assume 32-bit alignment */
291 serial_in
= mem32_serial_in
;
292 serial_out
= mem32_serial_out
;
293 /* WARNING! assuming the address is always in the first 4G */
295 (unsigned long)early_ioremap(bar0
& 0xfffffff0, 0x10);
296 write_pci_config(bus
, slot
, func
, PCI_COMMAND
,
297 cmdreg
|PCI_COMMAND_MEMORY
);
301 * Initialize the hardware
304 if (strcmp(s
, "nocfg") == 0)
305 /* Sometimes, we want to leave the UART alone
306 * and assume the BIOS has set it up correctly.
307 * "nocfg" tells us this is the case, and we
308 * should do no more setup.
311 if (kstrtoul(s
, 0, &baud
) < 0 || baud
== 0)
315 /* Convert from baud to divisor value */
316 divisor
= 115200 / baud
;
319 early_serial_hw_init(divisor
);
323 static struct console early_serial_console
= {
325 .write
= early_serial_write
,
326 .flags
= CON_PRINTBUFFER
,
330 static void early_console_register(struct console
*con
, int keep_early
)
332 if (con
->index
!= -1) {
333 printk(KERN_CRIT
"ERROR: earlyprintk= %s already used\n",
339 early_console
->flags
&= ~CON_BOOT
;
341 early_console
->flags
|= CON_BOOT
;
342 register_console(early_console
);
345 static int __init
setup_early_printk(char *buf
)
355 keep
= (strstr(buf
, "keep") != NULL
);
357 while (*buf
!= '\0') {
358 if (!strncmp(buf
, "serial", 6)) {
360 early_serial_init(buf
);
361 early_console_register(&early_serial_console
, keep
);
362 if (!strncmp(buf
, ",ttyS", 5))
365 if (!strncmp(buf
, "ttyS", 4)) {
366 early_serial_init(buf
+ 4);
367 early_console_register(&early_serial_console
, keep
);
370 if (!strncmp(buf
, "pciserial", 9)) {
371 early_pci_serial_init(buf
+ 9);
372 early_console_register(&early_serial_console
, keep
);
373 buf
+= 9; /* Keep from match the above "serial" */
376 if (!strncmp(buf
, "vga", 3) &&
377 boot_params
.screen_info
.orig_video_isVGA
== 1) {
378 max_xpos
= boot_params
.screen_info
.orig_video_cols
;
379 max_ypos
= boot_params
.screen_info
.orig_video_lines
;
380 current_ypos
= boot_params
.screen_info
.orig_y
;
381 early_console_register(&early_vga_console
, keep
);
383 #ifdef CONFIG_EARLY_PRINTK_DBGP
384 if (!strncmp(buf
, "dbgp", 4) && !early_dbgp_init(buf
+ 4))
385 early_console_register(&early_dbgp_console
, keep
);
387 #ifdef CONFIG_HVC_XEN
388 if (!strncmp(buf
, "xen", 3))
389 early_console_register(&xenboot_console
, keep
);
391 #ifdef CONFIG_EARLY_PRINTK_USB_XDBC
392 if (!strncmp(buf
, "xdbc", 4))
393 early_xdbc_parse_parameter(buf
+ 4);
401 early_param("earlyprintk", setup_early_printk
);