2 * Early serial console for 8250/16550 devices
4 * (c) Copyright 2004 Hewlett-Packard Development Company, L.P.
5 * Bjorn Helgaas <bjorn.helgaas@hp.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * Based on the 8250.c serial driver, Copyright (C) 2001 Russell King,
12 * and on early_printk.c by Andi Kleen.
14 * This is for use before the serial driver has initialized, in
15 * particular, before the UARTs have been discovered and named.
16 * Instead of specifying the console device as, e.g., "ttyS0",
17 * we locate the device directly by its MMIO or I/O port address.
19 * The user can specify the device directly, e.g.,
20 * earlycon=uart8250,io,0x3f8,9600n8
21 * earlycon=uart8250,mmio,0xff5e0000,115200n8
22 * earlycon=uart8250,mmio32,0xff5e0000,115200n8
24 * console=uart8250,io,0x3f8,9600n8
25 * console=uart8250,mmio,0xff5e0000,115200n8
26 * console=uart8250,mmio32,0xff5e0000,115200n8
29 #include <linux/tty.h>
30 #include <linux/init.h>
31 #include <linux/console.h>
33 #include <linux/of_device.h>
34 #include <linux/serial_reg.h>
35 #include <linux/serial.h>
36 #include <linux/serial_8250.h>
38 #include <asm/serial.h>
40 static unsigned int __init serial8250_early_in(struct uart_port *port, int offset)
42 switch (port->iotype) {
44 return readb(port->membase + offset);
46 return readw(port->membase + (offset << 1));
48 return readl(port->membase + (offset << 2));
50 return ioread32be(port->membase + (offset << 2));
52 return inb(port->iobase + offset);
58 static void __init serial8250_early_out(struct uart_port *port, int offset, int value)
60 switch (port->iotype) {
62 writeb(value, port->membase + offset);
65 writew(value, port->membase + (offset << 1));
68 writel(value, port->membase + (offset << 2));
71 iowrite32be(value, port->membase + (offset << 2));
74 outb(value, port->iobase + offset);
79 #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
81 static void __init serial_putc(struct uart_port *port, int c)
85 serial8250_early_out(port, UART_TX, c);
88 status = serial8250_early_in(port, UART_LSR);
89 if ((status & BOTH_EMPTY) == BOTH_EMPTY)
95 static void __init early_serial8250_write(struct console *console,
96 const char *s, unsigned int count)
98 struct earlycon_device *device = console->data;
99 struct uart_port *port = &device->port;
101 uart_console_write(port, s, count, serial_putc);
104 static void __init init_port(struct earlycon_device *device)
106 struct uart_port *port = &device->port;
107 unsigned int divisor;
111 serial8250_early_out(port, UART_LCR, 0x3); /* 8n1 */
112 ier = serial8250_early_in(port, UART_IER);
113 serial8250_early_out(port, UART_IER, ier & UART_IER_UUE); /* no interrupt */
114 serial8250_early_out(port, UART_FCR, 0); /* no fifo */
115 serial8250_early_out(port, UART_MCR, 0x3); /* DTR + RTS */
117 divisor = DIV_ROUND_CLOSEST(port->uartclk, 16 * device->baud);
118 c = serial8250_early_in(port, UART_LCR);
119 serial8250_early_out(port, UART_LCR, c | UART_LCR_DLAB);
120 serial8250_early_out(port, UART_DLL, divisor & 0xff);
121 serial8250_early_out(port, UART_DLM, (divisor >> 8) & 0xff);
122 serial8250_early_out(port, UART_LCR, c & ~UART_LCR_DLAB);
125 int __init early_serial8250_setup(struct earlycon_device *device,
128 if (!(device->port.membase || device->port.iobase))
132 struct uart_port *port = &device->port;
135 /* assume the device was initialized, only mask interrupts */
136 ier = serial8250_early_in(port, UART_IER);
137 serial8250_early_out(port, UART_IER, ier & UART_IER_UUE);
141 device->con->write = early_serial8250_write;
144 EARLYCON_DECLARE(uart8250, early_serial8250_setup);
145 EARLYCON_DECLARE(uart, early_serial8250_setup);
146 OF_EARLYCON_DECLARE(ns16550, "ns16550", early_serial8250_setup);
147 OF_EARLYCON_DECLARE(ns16550a, "ns16550a", early_serial8250_setup);