2 * Copyright 2013-2015 John Wehle <john@feith.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
28 * Amlogic aml8726 UART console driver.
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.
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.
38 #include "opt_global.h"
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 #include <sys/param.h>
44 #include <sys/systm.h>
48 #include <sys/consio.h>
49 #include <sys/kernel.h>
50 #include <sys/sysctl.h>
52 #include <machine/bus.h>
53 #include <machine/cpu.h>
55 #include <arm/amlogic/aml8726/aml8726_machdep.h>
56 #include <arm/amlogic/aml8726/aml8726_uart.h>
58 #define AML_UART_KVM_BASE (aml8726_aobus_kva_base + 0x130 * 4)
61 ub_getreg(uint32_t off)
64 return *((volatile uint32_t *)(AML_UART_KVM_BASE + off));
68 ub_setreg(uint32_t off, uint32_t val)
71 *((volatile uint32_t *)(AML_UART_KVM_BASE + off)) = val;
75 uart_cnprobe(struct consdev *cp)
78 sprintf(cp->cn_name, "uart");
79 cp->cn_pri = CN_REMOTE;
83 uart_cngrab(struct consdev *cp)
88 uart_cnungrab(struct consdev *cp)
93 uart_cninit(struct consdev *cp)
99 if (early_putc != NULL) {
100 printf("Early printf yielding control to the real console.\n");
105 * Give pending characters a chance to drain.
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);
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);
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);
132 uart_cnterm(struct consdev * cp)
137 uart_cnputc(struct consdev *cp, int c)
140 while ((ub_getreg(AML_UART_STATUS_REG) &
141 AML_UART_STATUS_TX_FIFO_FULL) != 0)
144 ub_setreg(AML_UART_WFIFO_REG, c);
148 uart_cngetc(struct consdev * cp)
152 if ((ub_getreg(AML_UART_STATUS_REG) &
153 AML_UART_STATUS_RX_FIFO_EMPTY) != 0)
156 c = ub_getreg(AML_UART_RFIFO_REG) & 0xff;
161 CONSOLE_DRIVER(uart);
165 #if !(defined(SOCDEV_PA) && defined(SOCDEV_VA))
166 #error SOCDEV_PA and SOCDEV_VA must be defined.
176 uart_cnputc(NULL, c);
179 early_putc_t *early_putc = eputc;