1/* 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., 20 * earlycon=uart8250,io,0x3f8,9600n8 21 * earlycon=uart8250,mmio,0xff5e0000,115200n8 22 * earlycon=uart8250,mmio32,0xff5e0000,115200n8 23 * or 24 * console=uart8250,io,0x3f8,9600n8 25 * console=uart8250,mmio,0xff5e0000,115200n8 26 * console=uart8250,mmio32,0xff5e0000,115200n8 27 */ 28 29#include <linux/tty.h> 30#include <linux/init.h> 31#include <linux/console.h> 32#include <linux/serial_reg.h> 33#include <linux/serial.h> 34#include <linux/serial_8250.h> 35#include <asm/io.h> 36#include <asm/serial.h> 37 38unsigned int __weak __init serial8250_early_in(struct uart_port *port, int offset) 39{ 40 switch (port->iotype) { 41 case UPIO_MEM: 42 return readb(port->membase + offset); 43 case UPIO_MEM32: 44 return readl(port->membase + (offset << 2)); 45 case UPIO_MEM32BE: 46 return ioread32be(port->membase + (offset << 2)); 47 case UPIO_PORT: 48 return inb(port->iobase + offset); 49 default: 50 return 0; 51 } 52} 53 54void __weak __init serial8250_early_out(struct uart_port *port, int offset, int value) 55{ 56 switch (port->iotype) { 57 case UPIO_MEM: 58 writeb(value, port->membase + offset); 59 break; 60 case UPIO_MEM32: 61 writel(value, port->membase + (offset << 2)); 62 break; 63 case UPIO_MEM32BE: 64 iowrite32be(value, port->membase + (offset << 2)); 65 break; 66 case UPIO_PORT: 67 outb(value, port->iobase + offset); 68 break; 69 } 70} 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 (;;) { 79 status = serial8250_early_in(port, UART_LSR); 80 if ((status & BOTH_EMPTY) == BOTH_EMPTY) 81 return; 82 cpu_relax(); 83 } 84} 85 86static void __init serial_putc(struct uart_port *port, int c) 87{ 88 wait_for_xmitr(port); 89 serial8250_early_out(port, UART_TX, c); 90} 91 92static void __init early_serial8250_write(struct console *console, 93 const char *s, unsigned int count) 94{ 95 struct earlycon_device *device = console->data; 96 struct uart_port *port = &device->port; 97 unsigned int ier; 98 99 /* Save the IER and disable interrupts preserving the UUE bit */ 100 ier = serial8250_early_in(port, UART_IER); 101 if (ier) 102 serial8250_early_out(port, UART_IER, ier & UART_IER_UUE); 103 104 uart_console_write(port, s, count, serial_putc); 105 106 /* Wait for transmitter to become empty and restore the IER */ 107 wait_for_xmitr(port); 108 109 if (ier) 110 serial8250_early_out(port, UART_IER, ier); 111} 112 113static void __init init_port(struct earlycon_device *device) 114{ 115 struct uart_port *port = &device->port; 116 unsigned int divisor; 117 unsigned char c; 118 unsigned int ier; 119 120 serial8250_early_out(port, UART_LCR, 0x3); /* 8n1 */ 121 ier = serial8250_early_in(port, UART_IER); 122 serial8250_early_out(port, UART_IER, ier & UART_IER_UUE); /* no interrupt */ 123 serial8250_early_out(port, UART_FCR, 0); /* no fifo */ 124 serial8250_early_out(port, UART_MCR, 0x3); /* DTR + RTS */ 125 126 divisor = DIV_ROUND_CLOSEST(port->uartclk, 16 * device->baud); 127 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); 132} 133 134static int __init early_serial8250_setup(struct earlycon_device *device, 135 const char *options) 136{ 137 if (!(device->port.membase || device->port.iobase)) 138 return -ENODEV; 139 140 if (!device->baud) { 141 struct uart_port *port = &device->port; 142 unsigned int ier; 143 144 /* 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); 149 150 device->con->write = early_serial8250_write; 151 return 0; 152} 153EARLYCON_DECLARE(uart8250, early_serial8250_setup); 154EARLYCON_DECLARE(uart, early_serial8250_setup); 155