]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/amlogic/aml8726/aml8726_uart_console.c
Merge OpenSSL 1.0.2m.
[FreeBSD/FreeBSD.git] / sys / arm / amlogic / aml8726 / aml8726_uart_console.c
1 /*-
2  * Copyright 2013-2015 John Wehle <john@feith.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 /*
28  * Amlogic aml8726 UART console driver.
29  *
30  * This is only necessary to use when debugging early boot code.
31  * The standard uart driver is available for use later in the boot.
32  *
33  * It's assumed the SoC uart is mapped into AML_UART_KVM_BASE meaning
34  * when using EARLY_PRINTF you'll need to define SOCDEV_VA to be
35  * 0xd8100000 and SOCDEV_PA to be 0xc8100000 in your config file.
36  */
37
38 #include "opt_global.h"
39
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/bus.h>
46 #include <sys/conf.h>
47 #include <sys/cons.h>
48 #include <sys/consio.h>
49 #include <sys/kernel.h>
50 #include <sys/sysctl.h>
51
52 #include <machine/bus.h>
53 #include <machine/cpu.h>
54
55 #include <arm/amlogic/aml8726/aml8726_machdep.h>
56 #include <arm/amlogic/aml8726/aml8726_uart.h>
57
58 #define AML_UART_KVM_BASE       (aml8726_aobus_kva_base + 0x130 * 4)
59
60 static uint32_t
61 ub_getreg(uint32_t off)
62 {
63
64         return *((volatile uint32_t *)(AML_UART_KVM_BASE + off));
65 }
66
67 static void
68 ub_setreg(uint32_t off, uint32_t val)
69 {
70
71         *((volatile uint32_t *)(AML_UART_KVM_BASE + off)) = val;
72 }
73
74 static void
75 uart_cnprobe(struct consdev *cp)
76 {
77
78         sprintf(cp->cn_name, "uart");
79         cp->cn_pri = CN_REMOTE;
80 }
81
82 static void
83 uart_cngrab(struct consdev *cp)
84 {
85 }
86
87 static void
88 uart_cnungrab(struct consdev *cp)
89 {
90 }
91
92 static void
93 uart_cninit(struct consdev *cp)
94 {
95         uint32_t cr;
96         uint32_t mr;
97
98 #ifdef EARLY_PRINTF
99         if (early_putc != NULL) {
100                 printf("Early printf yielding control to the real console.\n");
101                 early_putc = NULL;
102         }
103
104         /*
105          * Give pending characters a chance to drain.
106          */
107         DELAY(4000);
108 #endif
109
110         cr = ub_getreg(AML_UART_CONTROL_REG);
111         /* Disable all interrupt sources. */
112         cr &= ~(AML_UART_CONTROL_TX_INT_EN | AML_UART_CONTROL_RX_INT_EN);
113         /* Reset the transmitter and receiver. */
114         cr |= (AML_UART_CONTROL_TX_RST | AML_UART_CONTROL_RX_RST);
115         /* Use two wire mode. */
116         cr |= AML_UART_CONTROL_TWO_WIRE_EN;
117         /* Enable the transmitter and receiver. */
118         cr |= (AML_UART_CONTROL_TX_EN | AML_UART_CONTROL_RX_EN);
119         ub_setreg(AML_UART_CONTROL_REG, cr);
120
121         /* Clear RX FIFO level for generating interrupts. */
122         mr = ub_getreg(AML_UART_MISC_REG);
123         mr &= ~AML_UART_MISC_RECV_IRQ_CNT_MASK;
124         ub_setreg(AML_UART_MISC_REG, mr);
125
126         /* Ensure the reset bits are clear. */
127         cr &= ~(AML_UART_CONTROL_TX_RST | AML_UART_CONTROL_RX_RST);
128         ub_setreg(AML_UART_CONTROL_REG, cr);
129 }
130
131 static void
132 uart_cnterm(struct consdev * cp)
133 {
134 }
135
136 static void
137 uart_cnputc(struct consdev *cp, int c)
138 {
139
140         while ((ub_getreg(AML_UART_STATUS_REG) &
141             AML_UART_STATUS_TX_FIFO_FULL) != 0)
142                 cpu_spinwait();
143
144         ub_setreg(AML_UART_WFIFO_REG, c);
145 }
146
147 static int
148 uart_cngetc(struct consdev * cp)
149 {
150         int c;
151
152         if ((ub_getreg(AML_UART_STATUS_REG) &
153             AML_UART_STATUS_RX_FIFO_EMPTY) != 0)
154                 return (-1);
155
156         c = ub_getreg(AML_UART_RFIFO_REG) & 0xff;
157
158         return (c);
159 }
160
161 CONSOLE_DRIVER(uart);
162
163 #ifdef EARLY_PRINTF
164
165 #if !(defined(SOCDEV_PA) && defined(SOCDEV_VA))
166 #error SOCDEV_PA and SOCDEV_VA must be defined.
167 #endif
168
169 static void
170 eputc(int c)
171 {
172
173         if (c == '\n')
174                 eputc('\r');
175
176         uart_cnputc(NULL, c);
177 }
178
179 early_putc_t *early_putc = eputc;
180 #endif