]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/tty/serial/8250/8250_early.c
serial: 8250: remove the redundant include
[karo-tx-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_reg.h>
33 #include <linux/serial.h>
34 #include <linux/serial_8250.h>
35 #include <asm/io.h>
36 #include <asm/serial.h>
37
38 unsigned 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_PORT:
46                 return inb(port->iobase + offset);
47         default:
48                 return 0;
49         }
50 }
51
52 void __weak __init serial8250_early_out(struct uart_port *port, int offset, int value)
53 {
54         switch (port->iotype) {
55         case UPIO_MEM:
56                 writeb(value, port->membase + offset);
57                 break;
58         case UPIO_MEM32:
59                 writel(value, port->membase + (offset << 2));
60                 break;
61         case UPIO_PORT:
62                 outb(value, port->iobase + offset);
63                 break;
64         }
65 }
66
67 #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
68
69 static void __init wait_for_xmitr(struct uart_port *port)
70 {
71         unsigned int status;
72
73         for (;;) {
74                 status = serial8250_early_in(port, UART_LSR);
75                 if ((status & BOTH_EMPTY) == BOTH_EMPTY)
76                         return;
77                 cpu_relax();
78         }
79 }
80
81 static void __init serial_putc(struct uart_port *port, int c)
82 {
83         wait_for_xmitr(port);
84         serial8250_early_out(port, UART_TX, c);
85 }
86
87 static void __init early_serial8250_write(struct console *console,
88                                         const char *s, unsigned int count)
89 {
90         struct earlycon_device *device = console->data;
91         struct uart_port *port = &device->port;
92         unsigned int ier;
93
94         /* Save the IER and disable interrupts preserving the UUE bit */
95         ier = serial8250_early_in(port, UART_IER);
96         if (ier)
97                 serial8250_early_out(port, UART_IER, ier & UART_IER_UUE);
98
99         uart_console_write(port, s, count, serial_putc);
100
101         /* Wait for transmitter to become empty and restore the IER */
102         wait_for_xmitr(port);
103
104         if (ier)
105                 serial8250_early_out(port, UART_IER, ier);
106 }
107
108 static unsigned int __init probe_baud(struct uart_port *port)
109 {
110         unsigned char lcr, dll, dlm;
111         unsigned int quot;
112
113         lcr = serial8250_early_in(port, UART_LCR);
114         serial8250_early_out(port, UART_LCR, lcr | UART_LCR_DLAB);
115         dll = serial8250_early_in(port, UART_DLL);
116         dlm = serial8250_early_in(port, UART_DLM);
117         serial8250_early_out(port, UART_LCR, lcr);
118
119         quot = (dlm << 8) | dll;
120         return (port->uartclk / 16) / quot;
121 }
122
123 static void __init init_port(struct earlycon_device *device)
124 {
125         struct uart_port *port = &device->port;
126         unsigned int divisor;
127         unsigned char c;
128         unsigned int ier;
129
130         serial8250_early_out(port, UART_LCR, 0x3);      /* 8n1 */
131         ier = serial8250_early_in(port, UART_IER);
132         serial8250_early_out(port, UART_IER, ier & UART_IER_UUE); /* no interrupt */
133         serial8250_early_out(port, UART_FCR, 0);        /* no fifo */
134         serial8250_early_out(port, UART_MCR, 0x3);      /* DTR + RTS */
135
136         divisor = DIV_ROUND_CLOSEST(port->uartclk, 16 * device->baud);
137         c = serial8250_early_in(port, UART_LCR);
138         serial8250_early_out(port, UART_LCR, c | UART_LCR_DLAB);
139         serial8250_early_out(port, UART_DLL, divisor & 0xff);
140         serial8250_early_out(port, UART_DLM, (divisor >> 8) & 0xff);
141         serial8250_early_out(port, UART_LCR, c & ~UART_LCR_DLAB);
142 }
143
144 static int __init early_serial8250_setup(struct earlycon_device *device,
145                                          const char *options)
146 {
147         if (!(device->port.membase || device->port.iobase))
148                 return -ENODEV;
149
150         if (!device->baud) {
151                 struct uart_port *port = &device->port;
152                 unsigned int ier;
153
154                 device->baud = probe_baud(&device->port);
155                 snprintf(device->options, sizeof(device->options), "%u",
156                          device->baud);
157
158                 /* assume the device was initialized, only mask interrupts */
159                 ier = serial8250_early_in(port, UART_IER);
160                 serial8250_early_out(port, UART_IER, ier & UART_IER_UUE);
161         } else
162                 init_port(device);
163
164         device->con->write = early_serial8250_write;
165         return 0;
166 }
167 EARLYCON_DECLARE(uart8250, early_serial8250_setup);
168 EARLYCON_DECLARE(uart, early_serial8250_setup);