8250_early.c 5.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Early serial console for 8250/16550 devices
 *
 * (c) Copyright 2004 Hewlett-Packard Development Company, L.P.
 *	Bjorn Helgaas <bjorn.helgaas@hp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * Based on the 8250.c serial driver, Copyright (C) 2001 Russell King,
 * and on early_printk.c by Andi Kleen.
 *
 * This is for use before the serial driver has initialized, in
 * particular, before the UARTs have been discovered and named.
 * Instead of specifying the console device as, e.g., "ttyS0",
 * we locate the device directly by its MMIO or I/O port address.
 *
 * The user can specify the device directly, e.g.,
20 21
 *	earlycon=uart8250,io,0x3f8,9600n8
 *	earlycon=uart8250,mmio,0xff5e0000,115200n8
22
 *	earlycon=uart8250,mmio32,0xff5e0000,115200n8
23 24 25
 * or
 *	console=uart8250,io,0x3f8,9600n8
 *	console=uart8250,mmio,0xff5e0000,115200n8
26
 *	console=uart8250,mmio32,0xff5e0000,115200n8
L
Linus Torvalds 已提交
27 28 29 30 31 32 33 34
 */

#include <linux/tty.h>
#include <linux/init.h>
#include <linux/console.h>
#include <linux/serial_core.h>
#include <linux/serial_reg.h>
#include <linux/serial.h>
35
#include <linux/serial_8250.h>
L
Linus Torvalds 已提交
36 37 38
#include <asm/io.h>
#include <asm/serial.h>

39
unsigned int __weak __init serial8250_early_in(struct uart_port *port, int offset)
L
Linus Torvalds 已提交
40
{
41 42
	switch (port->iotype) {
	case UPIO_MEM:
L
Linus Torvalds 已提交
43
		return readb(port->membase + offset);
44 45 46
	case UPIO_MEM32:
		return readl(port->membase + (offset << 2));
	case UPIO_PORT:
L
Linus Torvalds 已提交
47
		return inb(port->iobase + offset);
48 49 50
	default:
		return 0;
	}
L
Linus Torvalds 已提交
51 52
}

53
void __weak __init serial8250_early_out(struct uart_port *port, int offset, int value)
L
Linus Torvalds 已提交
54
{
55 56
	switch (port->iotype) {
	case UPIO_MEM:
L
Linus Torvalds 已提交
57
		writeb(value, port->membase + offset);
58 59 60 61 62
		break;
	case UPIO_MEM32:
		writel(value, port->membase + (offset << 2));
		break;
	case UPIO_PORT:
L
Linus Torvalds 已提交
63
		outb(value, port->iobase + offset);
64 65
		break;
	}
L
Linus Torvalds 已提交
66 67 68 69 70 71 72 73 74
}

#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)

static void __init wait_for_xmitr(struct uart_port *port)
{
	unsigned int status;

	for (;;) {
75
		status = serial8250_early_in(port, UART_LSR);
L
Linus Torvalds 已提交
76 77 78 79 80 81
		if ((status & BOTH_EMPTY) == BOTH_EMPTY)
			return;
		cpu_relax();
	}
}

82
static void __init serial_putc(struct uart_port *port, int c)
L
Linus Torvalds 已提交
83 84
{
	wait_for_xmitr(port);
85
	serial8250_early_out(port, UART_TX, c);
L
Linus Torvalds 已提交
86 87
}

A
Alan Cox 已提交
88 89
static void __init early_serial8250_write(struct console *console,
					const char *s, unsigned int count)
L
Linus Torvalds 已提交
90
{
91 92
	struct earlycon_device *device = console->data;
	struct uart_port *port = &device->port;
L
Linus Torvalds 已提交
93 94
	unsigned int ier;

95
	/* Save the IER and disable interrupts preserving the UUE bit */
96
	ier = serial8250_early_in(port, UART_IER);
97
	if (ier)
98
		serial8250_early_out(port, UART_IER, ier & UART_IER_UUE);
L
Linus Torvalds 已提交
99

100
	uart_console_write(port, s, count, serial_putc);
L
Linus Torvalds 已提交
101 102 103

	/* Wait for transmitter to become empty and restore the IER */
	wait_for_xmitr(port);
104 105 106

	if (ier)
		serial8250_early_out(port, UART_IER, ier);
L
Linus Torvalds 已提交
107 108 109 110 111 112 113
}

static unsigned int __init probe_baud(struct uart_port *port)
{
	unsigned char lcr, dll, dlm;
	unsigned int quot;

114 115 116 117 118
	lcr = serial8250_early_in(port, UART_LCR);
	serial8250_early_out(port, UART_LCR, lcr | UART_LCR_DLAB);
	dll = serial8250_early_in(port, UART_DLL);
	dlm = serial8250_early_in(port, UART_DLM);
	serial8250_early_out(port, UART_LCR, lcr);
L
Linus Torvalds 已提交
119 120 121 122 123

	quot = (dlm << 8) | dll;
	return (port->uartclk / 16) / quot;
}

124
static void __init init_port(struct earlycon_device *device)
L
Linus Torvalds 已提交
125 126 127 128
{
	struct uart_port *port = &device->port;
	unsigned int divisor;
	unsigned char c;
129
	unsigned int ier;
L
Linus Torvalds 已提交
130

131
	serial8250_early_out(port, UART_LCR, 0x3);	/* 8n1 */
132 133
	ier = serial8250_early_in(port, UART_IER);
	serial8250_early_out(port, UART_IER, ier & UART_IER_UUE); /* no interrupt */
134 135
	serial8250_early_out(port, UART_FCR, 0);	/* no fifo */
	serial8250_early_out(port, UART_MCR, 0x3);	/* DTR + RTS */
L
Linus Torvalds 已提交
136

137
	divisor = DIV_ROUND_CLOSEST(port->uartclk, 16 * device->baud);
138 139 140 141 142
	c = serial8250_early_in(port, UART_LCR);
	serial8250_early_out(port, UART_LCR, c | UART_LCR_DLAB);
	serial8250_early_out(port, UART_DLL, divisor & 0xff);
	serial8250_early_out(port, UART_DLM, (divisor >> 8) & 0xff);
	serial8250_early_out(port, UART_LCR, c & ~UART_LCR_DLAB);
L
Linus Torvalds 已提交
143 144
}

145 146
static int __init early_serial8250_setup(struct earlycon_device *device,
					 const char *options)
L
Linus Torvalds 已提交
147
{
148
	if (!(device->port.membase || device->port.iobase))
149
		return -ENODEV;
L
Linus Torvalds 已提交
150

151
	if (!device->baud) {
152 153 154
		struct uart_port *port = &device->port;
		unsigned int ier;

155
		device->baud = probe_baud(&device->port);
156 157
		snprintf(device->options, sizeof(device->options), "%u",
			 device->baud);
L
Linus Torvalds 已提交
158

159 160 161 162 163
		/* assume the device was initialized, only mask interrupts */
		ier = serial8250_early_in(port, UART_IER);
		serial8250_early_out(port, UART_IER, ier & UART_IER_UUE);
	} else
		init_port(device);
164

165
	device->con->write = early_serial8250_write;
166
	return 0;
L
Linus Torvalds 已提交
167
}
168 169
EARLYCON_DECLARE(uart8250, early_serial8250_setup);
EARLYCON_DECLARE(uart, early_serial8250_setup);
L
Linus Torvalds 已提交
170

171 172 173 174 175 176 177 178 179
int __init setup_early_serial8250_console(char *cmdline)
{
	char match[] = "uart8250";

	if (cmdline && cmdline[4] == ',')
		match[4] = '\0';

	return setup_earlycon(cmdline, match, early_serial8250_setup);
}