1 #include <linux/console.h>
2 #include <linux/kernel.h>
3 #include <linux/init.h>
4 #include <linux/string.h>
5 #include <linux/screen_info.h>
6 #include <linux/usb/ch9.h>
7 #include <linux/pci_regs.h>
8 #include <linux/pci_ids.h>
9 #include <linux/errno.h>
11 #include <asm/processor.h>
12 #include <asm/fcntl.h>
13 #include <asm/setup.h>
14 #include <xen/hvc-console.h>
15 #include <asm/pci-direct.h>
16 #include <asm/fixmap.h>
17 #include <asm/intel-mid.h>
18 #include <asm/pgtable.h>
19 #include <linux/usb/ehci_def.h>
20 #include <linux/efi.h>
22 #include <asm/pci_x86.h>
24 /* Simple VGA output */
25 #define VGABASE (__ISA_IO_base + 0xb8000)
27 static int max_ypos
= 25, max_xpos
= 80;
28 static int current_ypos
= 25, current_xpos
;
30 static void early_vga_write(struct console
*con
, const char *str
, unsigned n
)
35 while ((c
= *str
++) != '\0' && n
-- > 0) {
36 if (current_ypos
>= max_ypos
) {
37 /* scroll 1 line up */
38 for (k
= 1, j
= 0; k
< max_ypos
; k
++, j
++) {
39 for (i
= 0; i
< max_xpos
; i
++) {
40 writew(readw(VGABASE
+2*(max_xpos
*k
+i
)),
41 VGABASE
+ 2*(max_xpos
*j
+ i
));
44 for (i
= 0; i
< max_xpos
; i
++)
45 writew(0x720, VGABASE
+ 2*(max_xpos
*j
+ i
));
46 current_ypos
= max_ypos
-1;
48 #ifdef CONFIG_KGDB_KDB
52 } else if (c
== '\r') {
59 } else if (c
!= '\r') {
60 writew(((0x7 << 8) | (unsigned short) c
),
61 VGABASE
+ 2*(max_xpos
*current_ypos
+
63 if (current_xpos
>= max_xpos
) {
71 static struct console early_vga_console
= {
73 .write
= early_vga_write
,
74 .flags
= CON_PRINTBUFFER
,
78 /* Serial functions loosely based on a similar package from Klaus P. Gerlicher */
80 static unsigned long early_serial_base
= 0x3f8; /* ttyS0 */
86 #define TXR 0 /* Transmit register (WRITE) */
87 #define RXR 0 /* Receive register (READ) */
88 #define IER 1 /* Interrupt Enable */
89 #define IIR 2 /* Interrupt ID */
90 #define FCR 2 /* FIFO control */
91 #define LCR 3 /* Line control */
92 #define MCR 4 /* Modem control */
93 #define LSR 5 /* Line Status */
94 #define MSR 6 /* Modem Status */
95 #define DLL 0 /* Divisor Latch Low */
96 #define DLH 1 /* Divisor latch High */
98 static unsigned int io_serial_in(unsigned long addr
, int offset
)
100 return inb(addr
+ offset
);
103 static void io_serial_out(unsigned long addr
, int offset
, int value
)
105 outb(value
, addr
+ offset
);
108 static unsigned int (*serial_in
)(unsigned long addr
, int offset
) = io_serial_in
;
109 static void (*serial_out
)(unsigned long addr
, int offset
, int value
) = io_serial_out
;
111 static int early_serial_putc(unsigned char ch
)
113 unsigned timeout
= 0xffff;
115 while ((serial_in(early_serial_base
, LSR
) & XMTRDY
) == 0 && --timeout
)
117 serial_out(early_serial_base
, TXR
, ch
);
118 return timeout
? 0 : -1;
121 static void early_serial_write(struct console
*con
, const char *s
, unsigned n
)
123 while (*s
&& n
-- > 0) {
125 early_serial_putc('\r');
126 early_serial_putc(*s
);
131 static __init
void early_serial_hw_init(unsigned divisor
)
135 serial_out(early_serial_base
, LCR
, 0x3); /* 8n1 */
136 serial_out(early_serial_base
, IER
, 0); /* no interrupt */
137 serial_out(early_serial_base
, FCR
, 0); /* no fifo */
138 serial_out(early_serial_base
, MCR
, 0x3); /* DTR + RTS */
140 c
= serial_in(early_serial_base
, LCR
);
141 serial_out(early_serial_base
, LCR
, c
| DLAB
);
142 serial_out(early_serial_base
, DLL
, divisor
& 0xff);
143 serial_out(early_serial_base
, DLH
, (divisor
>> 8) & 0xff);
144 serial_out(early_serial_base
, LCR
, c
& ~DLAB
);
147 #define DEFAULT_BAUD 9600
149 static __init
void early_serial_init(char *s
)
152 unsigned long baud
= DEFAULT_BAUD
;
160 if (!strncmp(s
, "0x", 2)) {
161 early_serial_base
= simple_strtoul(s
, &e
, 16);
163 static const int __initconst bases
[] = { 0x3f8, 0x2f8 };
165 if (!strncmp(s
, "ttyS", 4))
167 port
= simple_strtoul(s
, &e
, 10);
168 if (port
> 1 || s
== e
)
170 early_serial_base
= bases
[port
];
172 s
+= strcspn(s
, ",");
178 if (kstrtoul(s
, 0, &baud
) < 0 || baud
== 0)
182 /* Convert from baud to divisor value */
183 divisor
= 115200 / baud
;
185 /* These will always be IO based ports */
186 serial_in
= io_serial_in
;
187 serial_out
= io_serial_out
;
190 early_serial_hw_init(divisor
);
194 static void mem32_serial_out(unsigned long addr
, int offset
, int value
)
196 u32
*vaddr
= (u32
*)addr
;
197 /* shift implied by pointer type */
198 writel(value
, vaddr
+ offset
);
201 static unsigned int mem32_serial_in(unsigned long addr
, int offset
)
203 u32
*vaddr
= (u32
*)addr
;
204 /* shift implied by pointer type */
205 return readl(vaddr
+ offset
);
209 * early_pci_serial_init()
211 * This function is invoked when the early_printk param starts with "pciserial"
212 * The rest of the param should be ",B:D.F,baud" where B, D & F describe the
213 * location of a PCI device that must be a UART device.
215 static __init
void early_pci_serial_init(char *s
)
218 unsigned long baud
= DEFAULT_BAUD
;
226 * First, part the param to get the BDF values
234 bus
= (u8
)simple_strtoul(s
, &e
, 16);
239 slot
= (u8
)simple_strtoul(s
, &e
, 16);
244 func
= (u8
)simple_strtoul(s
, &e
, 16);
247 /* A baud might be following */
252 * Second, find the device from the BDF
254 cmdreg
= read_pci_config(bus
, slot
, func
, PCI_COMMAND
);
255 classcode
= read_pci_config(bus
, slot
, func
, PCI_CLASS_REVISION
);
256 bar0
= read_pci_config(bus
, slot
, func
, PCI_BASE_ADDRESS_0
);
259 * Verify it is a UART type device
261 if (((classcode
>> 16 != PCI_CLASS_COMMUNICATION_MODEM
) &&
262 (classcode
>> 16 != PCI_CLASS_COMMUNICATION_SERIAL
)) ||
263 (((classcode
>> 8) & 0xff) != 0x02)) /* 16550 I/F at BAR0 */
267 * Determine if it is IO or memory mapped
270 /* it is IO mapped */
271 serial_in
= io_serial_in
;
272 serial_out
= io_serial_out
;
273 early_serial_base
= bar0
&0xfffffffc;
274 write_pci_config(bus
, slot
, func
, PCI_COMMAND
,
275 cmdreg
|PCI_COMMAND_IO
);
277 /* It is memory mapped - assume 32-bit alignment */
278 serial_in
= mem32_serial_in
;
279 serial_out
= mem32_serial_out
;
280 /* WARNING! assuming the address is always in the first 4G */
282 (unsigned long)early_ioremap(bar0
& 0xfffffff0, 0x10);
283 write_pci_config(bus
, slot
, func
, PCI_COMMAND
,
284 cmdreg
|PCI_COMMAND_MEMORY
);
288 * Lastly, initalize the hardware
291 if (strcmp(s
, "nocfg") == 0)
292 /* Sometimes, we want to leave the UART alone
293 * and assume the BIOS has set it up correctly.
294 * "nocfg" tells us this is the case, and we
295 * should do no more setup.
298 if (kstrtoul(s
, 0, &baud
) < 0 || baud
== 0)
302 /* Convert from baud to divisor value */
303 divisor
= 115200 / baud
;
306 early_serial_hw_init(divisor
);
310 static struct console early_serial_console
= {
312 .write
= early_serial_write
,
313 .flags
= CON_PRINTBUFFER
,
317 static inline void early_console_register(struct console
*con
, int keep_early
)
319 if (con
->index
!= -1) {
320 printk(KERN_CRIT
"ERROR: earlyprintk= %s already used\n",
326 early_console
->flags
&= ~CON_BOOT
;
328 early_console
->flags
|= CON_BOOT
;
329 register_console(early_console
);
332 static int __init
setup_early_printk(char *buf
)
342 keep
= (strstr(buf
, "keep") != NULL
);
344 while (*buf
!= '\0') {
345 if (!strncmp(buf
, "serial", 6)) {
347 early_serial_init(buf
);
348 early_console_register(&early_serial_console
, keep
);
349 if (!strncmp(buf
, ",ttyS", 5))
352 if (!strncmp(buf
, "ttyS", 4)) {
353 early_serial_init(buf
+ 4);
354 early_console_register(&early_serial_console
, keep
);
357 if (!strncmp(buf
, "pciserial", 9)) {
358 early_pci_serial_init(buf
+ 9);
359 early_console_register(&early_serial_console
, keep
);
360 buf
+= 9; /* Keep from match the above "serial" */
363 if (!strncmp(buf
, "vga", 3) &&
364 boot_params
.screen_info
.orig_video_isVGA
== 1) {
365 max_xpos
= boot_params
.screen_info
.orig_video_cols
;
366 max_ypos
= boot_params
.screen_info
.orig_video_lines
;
367 current_ypos
= boot_params
.screen_info
.orig_y
;
368 early_console_register(&early_vga_console
, keep
);
370 #ifdef CONFIG_EARLY_PRINTK_DBGP
371 if (!strncmp(buf
, "dbgp", 4) && !early_dbgp_init(buf
+ 4))
372 early_console_register(&early_dbgp_console
, keep
);
374 #ifdef CONFIG_HVC_XEN
375 if (!strncmp(buf
, "xen", 3))
376 early_console_register(&xenboot_console
, keep
);
378 #ifdef CONFIG_EARLY_PRINTK_EFI
379 if (!strncmp(buf
, "efi", 3))
380 early_console_register(&early_efi_console
, keep
);
388 early_param("earlyprintk", setup_early_printk
);