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