Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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., |
Yinghai Lu | 18a8bd9 | 2007-07-15 23:37:59 -0700 | [diff] [blame] | 20 | * earlycon=uart8250,io,0x3f8,9600n8 |
| 21 | * earlycon=uart8250,mmio,0xff5e0000,115200n8 |
Samium Gromoff | 1917ac7 | 2010-07-20 15:26:51 -0700 | [diff] [blame] | 22 | * earlycon=uart8250,mmio32,0xff5e0000,115200n8 |
Yinghai Lu | 18a8bd9 | 2007-07-15 23:37:59 -0700 | [diff] [blame] | 23 | * or |
| 24 | * console=uart8250,io,0x3f8,9600n8 |
| 25 | * console=uart8250,mmio,0xff5e0000,115200n8 |
Samium Gromoff | 1917ac7 | 2010-07-20 15:26:51 -0700 | [diff] [blame] | 26 | * console=uart8250,mmio32,0xff5e0000,115200n8 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | */ |
| 28 | |
| 29 | #include <linux/tty.h> |
| 30 | #include <linux/init.h> |
| 31 | #include <linux/console.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <linux/serial_reg.h> |
| 33 | #include <linux/serial.h> |
Yinghai Lu | 18a8bd9 | 2007-07-15 23:37:59 -0700 | [diff] [blame] | 34 | #include <linux/serial_8250.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <asm/io.h> |
| 36 | #include <asm/serial.h> |
| 37 | |
Noam Camus | ed71871 | 2012-11-16 07:03:05 +0200 | [diff] [blame] | 38 | unsigned int __weak __init serial8250_early_in(struct uart_port *port, int offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | { |
Samium Gromoff | 1917ac7 | 2010-07-20 15:26:51 -0700 | [diff] [blame] | 40 | switch (port->iotype) { |
| 41 | case UPIO_MEM: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | return readb(port->membase + offset); |
Samium Gromoff | 1917ac7 | 2010-07-20 15:26:51 -0700 | [diff] [blame] | 43 | case UPIO_MEM32: |
| 44 | return readl(port->membase + (offset << 2)); |
Kevin Cernekee | c627f2c | 2015-04-09 13:05:17 -0700 | [diff] [blame] | 45 | case UPIO_MEM32BE: |
| 46 | return ioread32be(port->membase + (offset << 2)); |
Samium Gromoff | 1917ac7 | 2010-07-20 15:26:51 -0700 | [diff] [blame] | 47 | case UPIO_PORT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | return inb(port->iobase + offset); |
Samium Gromoff | 1917ac7 | 2010-07-20 15:26:51 -0700 | [diff] [blame] | 49 | default: |
| 50 | return 0; |
| 51 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Noam Camus | ed71871 | 2012-11-16 07:03:05 +0200 | [diff] [blame] | 54 | void __weak __init serial8250_early_out(struct uart_port *port, int offset, int value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | { |
Samium Gromoff | 1917ac7 | 2010-07-20 15:26:51 -0700 | [diff] [blame] | 56 | switch (port->iotype) { |
| 57 | case UPIO_MEM: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | writeb(value, port->membase + offset); |
Samium Gromoff | 1917ac7 | 2010-07-20 15:26:51 -0700 | [diff] [blame] | 59 | break; |
| 60 | case UPIO_MEM32: |
| 61 | writel(value, port->membase + (offset << 2)); |
| 62 | break; |
Kevin Cernekee | c627f2c | 2015-04-09 13:05:17 -0700 | [diff] [blame] | 63 | case UPIO_MEM32BE: |
| 64 | iowrite32be(value, port->membase + (offset << 2)); |
| 65 | break; |
Samium Gromoff | 1917ac7 | 2010-07-20 15:26:51 -0700 | [diff] [blame] | 66 | case UPIO_PORT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | outb(value, port->iobase + offset); |
Samium Gromoff | 1917ac7 | 2010-07-20 15:26:51 -0700 | [diff] [blame] | 68 | break; |
| 69 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE) |
| 73 | |
| 74 | static void __init wait_for_xmitr(struct uart_port *port) |
| 75 | { |
| 76 | unsigned int status; |
| 77 | |
| 78 | for (;;) { |
Noam Camus | ed71871 | 2012-11-16 07:03:05 +0200 | [diff] [blame] | 79 | status = serial8250_early_in(port, UART_LSR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | if ((status & BOTH_EMPTY) == BOTH_EMPTY) |
| 81 | return; |
| 82 | cpu_relax(); |
| 83 | } |
| 84 | } |
| 85 | |
Yinghai Lu | bf2cdef | 2007-11-05 14:50:53 -0800 | [diff] [blame] | 86 | static void __init serial_putc(struct uart_port *port, int c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | { |
| 88 | wait_for_xmitr(port); |
Noam Camus | ed71871 | 2012-11-16 07:03:05 +0200 | [diff] [blame] | 89 | serial8250_early_out(port, UART_TX, c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Alan Cox | ce2e204 | 2008-02-08 04:18:49 -0800 | [diff] [blame] | 92 | static void __init early_serial8250_write(struct console *console, |
| 93 | const char *s, unsigned int count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | { |
Peter Hurley | d0d654c | 2015-03-09 16:27:14 -0400 | [diff] [blame] | 95 | struct earlycon_device *device = console->data; |
| 96 | struct uart_port *port = &device->port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | unsigned int ier; |
| 98 | |
Rob Herring | a4c639b | 2015-01-26 22:50:09 -0600 | [diff] [blame] | 99 | /* Save the IER and disable interrupts preserving the UUE bit */ |
Noam Camus | ed71871 | 2012-11-16 07:03:05 +0200 | [diff] [blame] | 100 | ier = serial8250_early_in(port, UART_IER); |
Vineet Gupta | 5567c37 | 2015-01-06 16:59:10 +0530 | [diff] [blame] | 101 | if (ier) |
Rob Herring | a4c639b | 2015-01-26 22:50:09 -0600 | [diff] [blame] | 102 | serial8250_early_out(port, UART_IER, ier & UART_IER_UUE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
Yinghai Lu | bf2cdef | 2007-11-05 14:50:53 -0800 | [diff] [blame] | 104 | uart_console_write(port, s, count, serial_putc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
| 106 | /* Wait for transmitter to become empty and restore the IER */ |
| 107 | wait_for_xmitr(port); |
Vineet Gupta | 5567c37 | 2015-01-06 16:59:10 +0530 | [diff] [blame] | 108 | |
| 109 | if (ier) |
| 110 | serial8250_early_out(port, UART_IER, ier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Rob Herring | d2fd681 | 2014-04-18 17:19:56 -0500 | [diff] [blame] | 113 | static void __init init_port(struct earlycon_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | { |
| 115 | struct uart_port *port = &device->port; |
| 116 | unsigned int divisor; |
| 117 | unsigned char c; |
Rob Herring | a4c639b | 2015-01-26 22:50:09 -0600 | [diff] [blame] | 118 | unsigned int ier; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
Noam Camus | ed71871 | 2012-11-16 07:03:05 +0200 | [diff] [blame] | 120 | serial8250_early_out(port, UART_LCR, 0x3); /* 8n1 */ |
Rob Herring | a4c639b | 2015-01-26 22:50:09 -0600 | [diff] [blame] | 121 | ier = serial8250_early_in(port, UART_IER); |
| 122 | serial8250_early_out(port, UART_IER, ier & UART_IER_UUE); /* no interrupt */ |
Noam Camus | ed71871 | 2012-11-16 07:03:05 +0200 | [diff] [blame] | 123 | serial8250_early_out(port, UART_FCR, 0); /* no fifo */ |
| 124 | serial8250_early_out(port, UART_MCR, 0x3); /* DTR + RTS */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | |
Alexey Brodkin | b15d538 | 2012-10-03 16:27:43 +0400 | [diff] [blame] | 126 | divisor = DIV_ROUND_CLOSEST(port->uartclk, 16 * device->baud); |
Noam Camus | ed71871 | 2012-11-16 07:03:05 +0200 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Eddie Huang | 1c5841e | 2015-04-28 21:40:32 +0800 | [diff] [blame] | 134 | int __init early_serial8250_setup(struct earlycon_device *device, |
Rob Herring | d2fd681 | 2014-04-18 17:19:56 -0500 | [diff] [blame] | 135 | const char *options) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | { |
Rob Herring | d2fd681 | 2014-04-18 17:19:56 -0500 | [diff] [blame] | 137 | if (!(device->port.membase || device->port.iobase)) |
Peter Hurley | cd385e9 | 2015-03-09 16:27:17 -0400 | [diff] [blame] | 138 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
Rob Herring | 60efcf0 | 2014-06-12 12:52:44 -0500 | [diff] [blame] | 140 | if (!device->baud) { |
Peter Hurley | 0e3e143 | 2015-03-09 16:27:16 -0400 | [diff] [blame] | 141 | struct uart_port *port = &device->port; |
| 142 | unsigned int ier; |
| 143 | |
Peter Hurley | 0e3e143 | 2015-03-09 16:27:16 -0400 | [diff] [blame] | 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); |
Rob Herring | d2fd681 | 2014-04-18 17:19:56 -0500 | [diff] [blame] | 149 | |
Rob Herring | d2fd681 | 2014-04-18 17:19:56 -0500 | [diff] [blame] | 150 | device->con->write = early_serial8250_write; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | return 0; |
| 152 | } |
Rob Herring | d2fd681 | 2014-04-18 17:19:56 -0500 | [diff] [blame] | 153 | EARLYCON_DECLARE(uart8250, early_serial8250_setup); |
| 154 | EARLYCON_DECLARE(uart, early_serial8250_setup); |