blob: 4858b8a99d3b4fcdbd4702c08a89442c6fb795fa [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Early serial console for 8250/16550 devices
3 *
4 * (c) Copyright 2004 Hewlett-Packard Development Company, L.P.
5 * Bjorn Helgaas <bjorn.helgaas@hp.com>
6 *
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.
10 *
11 * Based on the 8250.c serial driver, Copyright (C) 2001 Russell King,
12 * and on early_printk.c by Andi Kleen.
13 *
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.
18 *
19 * The user can specify the device directly, e.g.,
Yinghai Lu18a8bd92007-07-15 23:37:59 -070020 * earlycon=uart8250,io,0x3f8,9600n8
21 * earlycon=uart8250,mmio,0xff5e0000,115200n8
Samium Gromoff1917ac72010-07-20 15:26:51 -070022 * earlycon=uart8250,mmio32,0xff5e0000,115200n8
Yinghai Lu18a8bd92007-07-15 23:37:59 -070023 * or
24 * console=uart8250,io,0x3f8,9600n8
25 * console=uart8250,mmio,0xff5e0000,115200n8
Samium Gromoff1917ac72010-07-20 15:26:51 -070026 * console=uart8250,mmio32,0xff5e0000,115200n8
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 */
28
29#include <linux/tty.h>
30#include <linux/init.h>
31#include <linux/console.h>
32#include <linux/serial_core.h>
33#include <linux/serial_reg.h>
34#include <linux/serial.h>
Yinghai Lu18a8bd92007-07-15 23:37:59 -070035#include <linux/serial_8250.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/io.h>
37#include <asm/serial.h>
38
Rob Herringd2fd6812014-04-18 17:19:56 -050039static struct earlycon_device *early_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Noam Camused718712012-11-16 07:03:05 +020041unsigned int __weak __init serial8250_early_in(struct uart_port *port, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
Samium Gromoff1917ac72010-07-20 15:26:51 -070043 switch (port->iotype) {
44 case UPIO_MEM:
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 return readb(port->membase + offset);
Samium Gromoff1917ac72010-07-20 15:26:51 -070046 case UPIO_MEM32:
47 return readl(port->membase + (offset << 2));
48 case UPIO_PORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 return inb(port->iobase + offset);
Samium Gromoff1917ac72010-07-20 15:26:51 -070050 default:
51 return 0;
52 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070053}
54
Noam Camused718712012-11-16 07:03:05 +020055void __weak __init serial8250_early_out(struct uart_port *port, int offset, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Samium Gromoff1917ac72010-07-20 15:26:51 -070057 switch (port->iotype) {
58 case UPIO_MEM:
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 writeb(value, port->membase + offset);
Samium Gromoff1917ac72010-07-20 15:26:51 -070060 break;
61 case UPIO_MEM32:
62 writel(value, port->membase + (offset << 2));
63 break;
64 case UPIO_PORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 outb(value, port->iobase + offset);
Samium Gromoff1917ac72010-07-20 15:26:51 -070066 break;
67 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
69
70#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
71
72static void __init wait_for_xmitr(struct uart_port *port)
73{
74 unsigned int status;
75
76 for (;;) {
Noam Camused718712012-11-16 07:03:05 +020077 status = serial8250_early_in(port, UART_LSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 if ((status & BOTH_EMPTY) == BOTH_EMPTY)
79 return;
80 cpu_relax();
81 }
82}
83
Yinghai Lubf2cdef2007-11-05 14:50:53 -080084static void __init serial_putc(struct uart_port *port, int c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
86 wait_for_xmitr(port);
Noam Camused718712012-11-16 07:03:05 +020087 serial8250_early_out(port, UART_TX, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
Alan Coxce2e2042008-02-08 04:18:49 -080090static void __init early_serial8250_write(struct console *console,
91 const char *s, unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Rob Herringd2fd6812014-04-18 17:19:56 -050093 struct uart_port *port = &early_device->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 unsigned int ier;
95
96 /* Save the IER and disable interrupts */
Noam Camused718712012-11-16 07:03:05 +020097 ier = serial8250_early_in(port, UART_IER);
98 serial8250_early_out(port, UART_IER, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Yinghai Lubf2cdef2007-11-05 14:50:53 -0800100 uart_console_write(port, s, count, serial_putc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102 /* Wait for transmitter to become empty and restore the IER */
103 wait_for_xmitr(port);
Noam Camused718712012-11-16 07:03:05 +0200104 serial8250_early_out(port, UART_IER, ier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
107static unsigned int __init probe_baud(struct uart_port *port)
108{
109 unsigned char lcr, dll, dlm;
110 unsigned int quot;
111
Noam Camused718712012-11-16 07:03:05 +0200112 lcr = serial8250_early_in(port, UART_LCR);
113 serial8250_early_out(port, UART_LCR, lcr | UART_LCR_DLAB);
114 dll = serial8250_early_in(port, UART_DLL);
115 dlm = serial8250_early_in(port, UART_DLM);
116 serial8250_early_out(port, UART_LCR, lcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 quot = (dlm << 8) | dll;
119 return (port->uartclk / 16) / quot;
120}
121
Rob Herringd2fd6812014-04-18 17:19:56 -0500122static void __init init_port(struct earlycon_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124 struct uart_port *port = &device->port;
125 unsigned int divisor;
126 unsigned char c;
127
Noam Camused718712012-11-16 07:03:05 +0200128 serial8250_early_out(port, UART_LCR, 0x3); /* 8n1 */
129 serial8250_early_out(port, UART_IER, 0); /* no interrupt */
130 serial8250_early_out(port, UART_FCR, 0); /* no fifo */
131 serial8250_early_out(port, UART_MCR, 0x3); /* DTR + RTS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Alexey Brodkinb15d5382012-10-03 16:27:43 +0400133 divisor = DIV_ROUND_CLOSEST(port->uartclk, 16 * device->baud);
Noam Camused718712012-11-16 07:03:05 +0200134 c = serial8250_early_in(port, UART_LCR);
135 serial8250_early_out(port, UART_LCR, c | UART_LCR_DLAB);
136 serial8250_early_out(port, UART_DLL, divisor & 0xff);
137 serial8250_early_out(port, UART_DLM, (divisor >> 8) & 0xff);
138 serial8250_early_out(port, UART_LCR, c & ~UART_LCR_DLAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
Rob Herringd2fd6812014-04-18 17:19:56 -0500141static int __init early_serial8250_setup(struct earlycon_device *device,
142 const char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Rob Herringd2fd6812014-04-18 17:19:56 -0500144 if (!(device->port.membase || device->port.iobase))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return 0;
146
Rob Herring60efcf02014-06-12 12:52:44 -0500147 if (!device->baud) {
Rob Herringd2fd6812014-04-18 17:19:56 -0500148 device->baud = probe_baud(&device->port);
Rob Herring60efcf02014-06-12 12:52:44 -0500149 snprintf(device->options, sizeof(device->options), "%u",
150 device->baud);
151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 init_port(device);
Rob Herringd2fd6812014-04-18 17:19:56 -0500154
155 early_device = device;
156 device->con->write = early_serial8250_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return 0;
158}
Rob Herringd2fd6812014-04-18 17:19:56 -0500159EARLYCON_DECLARE(uart8250, early_serial8250_setup);
160EARLYCON_DECLARE(uart, early_serial8250_setup);
Yinghai Lu18a8bd92007-07-15 23:37:59 -0700161
Rob Herringfe1cf8a2014-04-30 19:48:28 -0500162int __init setup_early_serial8250_console(char *cmdline)
163{
164 char match[] = "uart8250";
165
166 if (cmdline && cmdline[4] == ',')
167 match[4] = '\0';
168
169 return setup_earlycon(cmdline, match, early_serial8250_setup);
170}
171
Daniel Ritzb6b1d872007-08-03 16:07:43 +0200172int serial8250_find_port_for_earlycon(void)
Yinghai Lu18a8bd92007-07-15 23:37:59 -0700173{
Rob Herringd2fd6812014-04-18 17:19:56 -0500174 struct earlycon_device *device = early_device;
175 struct uart_port *port = device ? &device->port : NULL;
Yinghai Lu18a8bd92007-07-15 23:37:59 -0700176 int line;
177 int ret;
178
Rob Herringd2fd6812014-04-18 17:19:56 -0500179 if (!port || (!port->membase && !port->iobase))
Yinghai Lu18a8bd92007-07-15 23:37:59 -0700180 return -ENODEV;
181
182 line = serial8250_find_port(port);
183 if (line < 0)
184 return -ENODEV;
185
186 ret = update_console_cmdline("uart", 8250,
187 "ttyS", line, device->options);
188 if (ret < 0)
189 ret = update_console_cmdline("uart", 0,
190 "ttyS", line, device->options);
191
192 return ret;
193}