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_core.h> 33 #include <linux/serial_reg.h> 34 #include <linux/serial.h> 35 #include <linux/serial_8250.h> 36 #include <asm/io.h> 37 #include <asm/serial.h> 38 39 static struct earlycon_device *early_device; 40 41 unsigned int __weak __init serial8250_early_in(struct uart_port *port, int offset) 42 { 43 switch (port->iotype) { 44 case UPIO_MEM: 45 return readb(port->membase + offset); 46 case UPIO_MEM32: 47 return readl(port->membase + (offset << 2)); 48 case UPIO_PORT: 49 return inb(port->iobase + offset); 50 default: 51 return 0; 52 } 53 } 54 55 void __weak __init serial8250_early_out(struct uart_port *port, int offset, int value) 56 { 57 switch (port->iotype) { 58 case UPIO_MEM: 59 writeb(value, port->membase + offset); 60 break; 61 case UPIO_MEM32: 62 writel(value, port->membase + (offset << 2)); 63 break; 64 case UPIO_PORT: 65 outb(value, port->iobase + offset); 66 break; 67 } 68 } 69 70 #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE) 71 72 static void __init wait_for_xmitr(struct uart_port *port) 73 { 74 unsigned int status; 75 76 for (;;) { 77 status = serial8250_early_in(port, UART_LSR); 78 if ((status & BOTH_EMPTY) == BOTH_EMPTY) 79 return; 80 cpu_relax(); 81 } 82 } 83 84 static void __init serial_putc(struct uart_port *port, int c) 85 { 86 wait_for_xmitr(port); 87 serial8250_early_out(port, UART_TX, c); 88 } 89 90 static void __init early_serial8250_write(struct console *console, 91 const char *s, unsigned int count) 92 { 93 struct uart_port *port = &early_device->port; 94 unsigned int ier; 95 96 /* Save the IER and disable interrupts preserving the UUE bit */ 97 ier = serial8250_early_in(port, UART_IER); 98 if (ier) 99 serial8250_early_out(port, UART_IER, ier & UART_IER_UUE); 100 101 uart_console_write(port, s, count, serial_putc); 102 103 /* Wait for transmitter to become empty and restore the IER */ 104 wait_for_xmitr(port); 105 106 if (ier) 107 serial8250_early_out(port, UART_IER, ier); 108 } 109 110 static unsigned int __init probe_baud(struct uart_port *port) 111 { 112 unsigned char lcr, dll, dlm; 113 unsigned int quot; 114 115 lcr = serial8250_early_in(port, UART_LCR); 116 serial8250_early_out(port, UART_LCR, lcr | UART_LCR_DLAB); 117 dll = serial8250_early_in(port, UART_DLL); 118 dlm = serial8250_early_in(port, UART_DLM); 119 serial8250_early_out(port, UART_LCR, lcr); 120 121 quot = (dlm << 8) | dll; 122 return (port->uartclk / 16) / quot; 123 } 124 125 static void __init init_port(struct earlycon_device *device) 126 { 127 struct uart_port *port = &device->port; 128 unsigned int divisor; 129 unsigned char c; 130 unsigned int ier; 131 132 serial8250_early_out(port, UART_LCR, 0x3); /* 8n1 */ 133 ier = serial8250_early_in(port, UART_IER); 134 serial8250_early_out(port, UART_IER, ier & UART_IER_UUE); /* no interrupt */ 135 serial8250_early_out(port, UART_FCR, 0); /* no fifo */ 136 serial8250_early_out(port, UART_MCR, 0x3); /* DTR + RTS */ 137 138 divisor = DIV_ROUND_CLOSEST(port->uartclk, 16 * device->baud); 139 c = serial8250_early_in(port, UART_LCR); 140 serial8250_early_out(port, UART_LCR, c | UART_LCR_DLAB); 141 serial8250_early_out(port, UART_DLL, divisor & 0xff); 142 serial8250_early_out(port, UART_DLM, (divisor >> 8) & 0xff); 143 serial8250_early_out(port, UART_LCR, c & ~UART_LCR_DLAB); 144 } 145 146 static int __init early_serial8250_setup(struct earlycon_device *device, 147 const char *options) 148 { 149 if (!(device->port.membase || device->port.iobase)) 150 return 0; 151 152 if (!device->baud) { 153 device->baud = probe_baud(&device->port); 154 snprintf(device->options, sizeof(device->options), "%u", 155 device->baud); 156 } 157 158 init_port(device); 159 160 early_device = device; 161 device->con->write = early_serial8250_write; 162 return 0; 163 } 164 EARLYCON_DECLARE(uart8250, early_serial8250_setup); 165 EARLYCON_DECLARE(uart, early_serial8250_setup); 166 167 int __init setup_early_serial8250_console(char *cmdline) 168 { 169 char match[] = "uart8250"; 170 171 if (cmdline && cmdline[4] == ',') 172 match[4] = '\0'; 173 174 return setup_earlycon(cmdline, match, early_serial8250_setup); 175 } 176 177 int serial8250_find_port_for_earlycon(void) 178 { 179 struct earlycon_device *device = early_device; 180 struct uart_port *port = device ? &device->port : NULL; 181 int line; 182 int ret; 183 184 if (!port || (!port->membase && !port->iobase)) 185 return -ENODEV; 186 187 line = serial8250_find_port(port); 188 if (line < 0) 189 return -ENODEV; 190 191 ret = update_console_cmdline("uart", 8250, 192 "ttyS", line, device->options); 193 if (ret < 0) 194 ret = update_console_cmdline("uart", 0, 195 "ttyS", line, device->options); 196 197 return ret; 198 } 199