GNU Linux-libre 4.14.251-gnu1
[releases.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/of.h>
33 #include <linux/of_device.h>
34 #include <linux/serial_reg.h>
35 #include <linux/serial.h>
36 #include <linux/serial_8250.h>
37 #include <asm/io.h>
38 #include <asm/serial.h>
39
40 static unsigned int serial8250_early_in(struct uart_port *port, int offset)
41 {
42         int reg_offset = offset;
43         offset <<= port->regshift;
44
45         switch (port->iotype) {
46         case UPIO_MEM:
47                 return readb(port->membase + offset);
48         case UPIO_MEM16:
49                 return readw(port->membase + offset);
50         case UPIO_MEM32:
51                 return readl(port->membase + offset);
52         case UPIO_MEM32BE:
53                 return ioread32be(port->membase + offset);
54         case UPIO_PORT:
55                 return inb(port->iobase + offset);
56         case UPIO_AU:
57                 return port->serial_in(port, reg_offset);
58         default:
59                 return 0;
60         }
61 }
62
63 static void serial8250_early_out(struct uart_port *port, int offset, int value)
64 {
65         int reg_offset = offset;
66         offset <<= port->regshift;
67
68         switch (port->iotype) {
69         case UPIO_MEM:
70                 writeb(value, port->membase + offset);
71                 break;
72         case UPIO_MEM16:
73                 writew(value, port->membase + offset);
74                 break;
75         case UPIO_MEM32:
76                 writel(value, port->membase + offset);
77                 break;
78         case UPIO_MEM32BE:
79                 iowrite32be(value, port->membase + offset);
80                 break;
81         case UPIO_PORT:
82                 outb(value, port->iobase + offset);
83                 break;
84         case UPIO_AU:
85                 port->serial_out(port, reg_offset, value);
86                 break;
87         }
88 }
89
90 #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
91
92 static void serial_putc(struct uart_port *port, int c)
93 {
94         unsigned int status;
95
96         serial8250_early_out(port, UART_TX, c);
97
98         for (;;) {
99                 status = serial8250_early_in(port, UART_LSR);
100                 if ((status & BOTH_EMPTY) == BOTH_EMPTY)
101                         break;
102                 cpu_relax();
103         }
104 }
105
106 static void early_serial8250_write(struct console *console,
107                                         const char *s, unsigned int count)
108 {
109         struct earlycon_device *device = console->data;
110         struct uart_port *port = &device->port;
111
112         uart_console_write(port, s, count, serial_putc);
113 }
114
115 static void __init init_port(struct earlycon_device *device)
116 {
117         struct uart_port *port = &device->port;
118         unsigned int divisor;
119         unsigned char c;
120         unsigned int ier;
121
122         serial8250_early_out(port, UART_LCR, 0x3);      /* 8n1 */
123         ier = serial8250_early_in(port, UART_IER);
124         serial8250_early_out(port, UART_IER, ier & UART_IER_UUE); /* no interrupt */
125         serial8250_early_out(port, UART_FCR, 0);        /* no fifo */
126         serial8250_early_out(port, UART_MCR, 0x3);      /* DTR + RTS */
127
128         if (port->uartclk && device->baud) {
129                 divisor = DIV_ROUND_CLOSEST(port->uartclk, 16 * device->baud);
130                 c = serial8250_early_in(port, UART_LCR);
131                 serial8250_early_out(port, UART_LCR, c | UART_LCR_DLAB);
132                 serial8250_early_out(port, UART_DLL, divisor & 0xff);
133                 serial8250_early_out(port, UART_DLM, (divisor >> 8) & 0xff);
134                 serial8250_early_out(port, UART_LCR, c & ~UART_LCR_DLAB);
135         }
136 }
137
138 int __init early_serial8250_setup(struct earlycon_device *device,
139                                          const char *options)
140 {
141         if (!(device->port.membase || device->port.iobase))
142                 return -ENODEV;
143
144         if (!device->baud) {
145                 struct uart_port *port = &device->port;
146                 unsigned int ier;
147
148                 /* assume the device was initialized, only mask interrupts */
149                 ier = serial8250_early_in(port, UART_IER);
150                 serial8250_early_out(port, UART_IER, ier & UART_IER_UUE);
151         } else
152                 init_port(device);
153
154         device->con->write = early_serial8250_write;
155         return 0;
156 }
157 EARLYCON_DECLARE(uart8250, early_serial8250_setup);
158 EARLYCON_DECLARE(uart, early_serial8250_setup);
159 OF_EARLYCON_DECLARE(ns16550, "ns16550", early_serial8250_setup);
160 OF_EARLYCON_DECLARE(ns16550a, "ns16550a", early_serial8250_setup);
161 OF_EARLYCON_DECLARE(uart, "nvidia,tegra20-uart", early_serial8250_setup);
162 OF_EARLYCON_DECLARE(uart, "snps,dw-apb-uart", early_serial8250_setup);
163
164 #ifdef CONFIG_SERIAL_8250_OMAP
165
166 static int __init early_omap8250_setup(struct earlycon_device *device,
167                                        const char *options)
168 {
169         struct uart_port *port = &device->port;
170
171         if (!(device->port.membase || device->port.iobase))
172                 return -ENODEV;
173
174         port->regshift = 2;
175         device->con->write = early_serial8250_write;
176         return 0;
177 }
178
179 OF_EARLYCON_DECLARE(omap8250, "ti,omap2-uart", early_omap8250_setup);
180 OF_EARLYCON_DECLARE(omap8250, "ti,omap3-uart", early_omap8250_setup);
181 OF_EARLYCON_DECLARE(omap8250, "ti,omap4-uart", early_omap8250_setup);
182
183 #endif
184
185 #ifdef CONFIG_SERIAL_8250_RT288X
186
187 unsigned int au_serial_in(struct uart_port *p, int offset);
188 void au_serial_out(struct uart_port *p, int offset, int value);
189
190 static int __init early_au_setup(struct earlycon_device *dev, const char *opt)
191 {
192         dev->port.serial_in = au_serial_in;
193         dev->port.serial_out = au_serial_out;
194         dev->port.iotype = UPIO_AU;
195         dev->con->write = early_serial8250_write;
196         return 0;
197 }
198 OF_EARLYCON_DECLARE(palmchip, "ralink,rt2880-uart", early_au_setup);
199
200 #endif