blob: 771dda29a0f89e352c0180572bf5a59acb0f824f [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/serial_reg.h>
33#include <linux/serial.h>
Yinghai Lu18a8bd92007-07-15 23:37:59 -070034#include <linux/serial_8250.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/io.h>
36#include <asm/serial.h>
37
Noam Camused718712012-11-16 07:03:05 +020038unsigned int __weak __init serial8250_early_in(struct uart_port *port, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
Samium Gromoff1917ac72010-07-20 15:26:51 -070040 switch (port->iotype) {
41 case UPIO_MEM:
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 return readb(port->membase + offset);
Samium Gromoff1917ac72010-07-20 15:26:51 -070043 case UPIO_MEM32:
44 return readl(port->membase + (offset << 2));
Kevin Cernekeec627f2c2015-04-09 13:05:17 -070045 case UPIO_MEM32BE:
46 return ioread32be(port->membase + (offset << 2));
Samium Gromoff1917ac72010-07-20 15:26:51 -070047 case UPIO_PORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 return inb(port->iobase + offset);
Samium Gromoff1917ac72010-07-20 15:26:51 -070049 default:
50 return 0;
51 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
53
Noam Camused718712012-11-16 07:03:05 +020054void __weak __init serial8250_early_out(struct uart_port *port, int offset, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Samium Gromoff1917ac72010-07-20 15:26:51 -070056 switch (port->iotype) {
57 case UPIO_MEM:
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 writeb(value, port->membase + offset);
Samium Gromoff1917ac72010-07-20 15:26:51 -070059 break;
60 case UPIO_MEM32:
61 writel(value, port->membase + (offset << 2));
62 break;
Kevin Cernekeec627f2c2015-04-09 13:05:17 -070063 case UPIO_MEM32BE:
64 iowrite32be(value, port->membase + (offset << 2));
65 break;
Samium Gromoff1917ac72010-07-20 15:26:51 -070066 case UPIO_PORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 outb(value, port->iobase + offset);
Samium Gromoff1917ac72010-07-20 15:26:51 -070068 break;
69 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070070}
71
72#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
73
74static void __init wait_for_xmitr(struct uart_port *port)
75{
76 unsigned int status;
77
78 for (;;) {
Noam Camused718712012-11-16 07:03:05 +020079 status = serial8250_early_in(port, UART_LSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 if ((status & BOTH_EMPTY) == BOTH_EMPTY)
81 return;
82 cpu_relax();
83 }
84}
85
Yinghai Lubf2cdef2007-11-05 14:50:53 -080086static void __init serial_putc(struct uart_port *port, int c)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
88 wait_for_xmitr(port);
Noam Camused718712012-11-16 07:03:05 +020089 serial8250_early_out(port, UART_TX, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
Alan Coxce2e2042008-02-08 04:18:49 -080092static void __init early_serial8250_write(struct console *console,
93 const char *s, unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Peter Hurleyd0d654c2015-03-09 16:27:14 -040095 struct earlycon_device *device = console->data;
96 struct uart_port *port = &device->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 unsigned int ier;
98
Rob Herringa4c639b2015-01-26 22:50:09 -060099 /* Save the IER and disable interrupts preserving the UUE bit */
Noam Camused718712012-11-16 07:03:05 +0200100 ier = serial8250_early_in(port, UART_IER);
Vineet Gupta5567c372015-01-06 16:59:10 +0530101 if (ier)
Rob Herringa4c639b2015-01-26 22:50:09 -0600102 serial8250_early_out(port, UART_IER, ier & UART_IER_UUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Yinghai Lubf2cdef2007-11-05 14:50:53 -0800104 uart_console_write(port, s, count, serial_putc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 /* Wait for transmitter to become empty and restore the IER */
107 wait_for_xmitr(port);
Vineet Gupta5567c372015-01-06 16:59:10 +0530108
109 if (ier)
110 serial8250_early_out(port, UART_IER, ier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
Rob Herringd2fd6812014-04-18 17:19:56 -0500113static void __init init_port(struct earlycon_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
115 struct uart_port *port = &device->port;
116 unsigned int divisor;
117 unsigned char c;
Rob Herringa4c639b2015-01-26 22:50:09 -0600118 unsigned int ier;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Noam Camused718712012-11-16 07:03:05 +0200120 serial8250_early_out(port, UART_LCR, 0x3); /* 8n1 */
Rob Herringa4c639b2015-01-26 22:50:09 -0600121 ier = serial8250_early_in(port, UART_IER);
122 serial8250_early_out(port, UART_IER, ier & UART_IER_UUE); /* no interrupt */
Noam Camused718712012-11-16 07:03:05 +0200123 serial8250_early_out(port, UART_FCR, 0); /* no fifo */
124 serial8250_early_out(port, UART_MCR, 0x3); /* DTR + RTS */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Alexey Brodkinb15d5382012-10-03 16:27:43 +0400126 divisor = DIV_ROUND_CLOSEST(port->uartclk, 16 * device->baud);
Noam Camused718712012-11-16 07:03:05 +0200127 c = serial8250_early_in(port, UART_LCR);
128 serial8250_early_out(port, UART_LCR, c | UART_LCR_DLAB);
129 serial8250_early_out(port, UART_DLL, divisor & 0xff);
130 serial8250_early_out(port, UART_DLM, (divisor >> 8) & 0xff);
131 serial8250_early_out(port, UART_LCR, c & ~UART_LCR_DLAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
Eddie Huang1c5841e2015-04-28 21:40:32 +0800134int __init early_serial8250_setup(struct earlycon_device *device,
Rob Herringd2fd6812014-04-18 17:19:56 -0500135 const char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Rob Herringd2fd6812014-04-18 17:19:56 -0500137 if (!(device->port.membase || device->port.iobase))
Peter Hurleycd385e92015-03-09 16:27:17 -0400138 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Rob Herring60efcf02014-06-12 12:52:44 -0500140 if (!device->baud) {
Peter Hurley0e3e1432015-03-09 16:27:16 -0400141 struct uart_port *port = &device->port;
142 unsigned int ier;
143
Peter Hurley0e3e1432015-03-09 16:27:16 -0400144 /* assume the device was initialized, only mask interrupts */
145 ier = serial8250_early_in(port, UART_IER);
146 serial8250_early_out(port, UART_IER, ier & UART_IER_UUE);
147 } else
148 init_port(device);
Rob Herringd2fd6812014-04-18 17:19:56 -0500149
Rob Herringd2fd6812014-04-18 17:19:56 -0500150 device->con->write = early_serial8250_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 return 0;
152}
Rob Herringd2fd6812014-04-18 17:19:56 -0500153EARLYCON_DECLARE(uart8250, early_serial8250_setup);
154EARLYCON_DECLARE(uart, early_serial8250_setup);