xref: /freebsd/sys/arm/qualcomm/ipq4018_machdep.c (revision 6ba2210ee039f2f12878c217bcf058e9c8b26b29)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2021 Adrian Chadd <adrian@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include "opt_platform.h"
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/bus.h>
36 #include <sys/reboot.h>
37 #include <sys/devmap.h>
38 #include <sys/physmem.h>
39 
40 #include <vm/vm.h>
41 
42 #include <machine/bus.h>
43 #include <machine/intr.h>
44 #include <machine/machdep.h>
45 #include <machine/platformvar.h>
46 
47 #include <dev/fdt/fdt_common.h>
48 #include <dev/ofw/openfirm.h>
49 
50 #include <arm/qualcomm/ipq4018_machdep.h>
51 #include <arm/qualcomm/ipq4018_reg.h>
52 
53 #include "platform_if.h"
54 
55 static int
56 ipq4018_attach(platform_t plat)
57 {
58 	return (0);
59 }
60 
61 static void
62 ipq4018_late_init(platform_t plat)
63 {
64 	/*
65 	 * XXX FIXME This is needed because we're not parsing
66 	 * the fdt reserved memory regions in a consistent way
67 	 * between arm/arm64.  Once the reserved region parsing
68 	 * is fixed up this will become unneccessary.
69 	 *
70 	 * These cover the SRAM/TZ regions that are not fully
71 	 * accessible from the OS.  They're in the ipq4018.dtsi
72 	 * tree.
73 	 *
74 	 * Without these, the system fails to boot because we
75 	 * aren't parsing the regions correctly.
76 	 *
77 	 * These will be unnecessary once the parser and setup
78 	 * code is fixed.
79 	 */
80 	physmem_exclude_region(IPQ4018_MEM_SMEM_START,
81 	    IPQ4018_MEM_SMEM_SIZE,
82 	    EXFLAG_NODUMP | EXFLAG_NOALLOC);
83 	physmem_exclude_region(IPQ4018_MEM_TZ_START,
84 	    IPQ4018_MEM_TZ_SIZE,
85 	    EXFLAG_NODUMP | EXFLAG_NOALLOC);
86 }
87 
88 static int
89 ipq4018_devmap_init(platform_t plat)
90 {
91 	/*
92 	 * This covers the boot UART.  Without it we can't boot successfully:
93 	 * there's a mutex uninit panic in subr_vmem.c that occurs when doing
94 	 * a call to pmap_mapdev() when the bus space code is doing its thing.
95 	 */
96 	devmap_add_entry(IPQ4018_MEM_UART1_START, IPQ4018_MEM_UART1_SIZE);
97 	return (0);
98 }
99 
100 static void
101 ipq4018_cpu_reset(platform_t plat)
102 {
103 }
104 
105 /*
106  * Early putc routine for EARLY_PRINTF support.  To use, add to kernel config:
107  *   option SOCDEV_PA=0x07800000
108  *   option SOCDEV_VA=0x07800000
109  *   option EARLY_PRINTF
110  * Resist the temptation to change the #if 0 to #ifdef EARLY_PRINTF here. It
111  * makes sense now, but if multiple SOCs do that it will make early_putc another
112  * duplicate symbol to be eliminated on the path to a generic kernel.
113  */
114 #if 0
115 void
116 qca_msm_early_putc(int c)
117 {
118 	static int is_init = 0;
119 
120 	int limit;
121 /*
122  * This must match what's put into SOCDEV_VA.  You have to change them
123  * both together.
124  *
125  * XXX TODO I should really go and just make UART_BASE here depend upon
126  * SOCDEV_VA so they move together.
127  */
128 #define UART_BASE IPQ4018_MEM_UART1_START
129 	volatile uint32_t * UART_DM_TF0 = (uint32_t *)(UART_BASE + 0x70);
130 	volatile uint32_t * UART_DM_SR = (uint32_t *)(UART_BASE + 0x08);
131 #define UART_DM_SR_TXEMT (1 << 3)
132 #define UART_DM_SR_TXRDY (1 << 2)
133 	volatile uint32_t * UART_DM_ISR = (uint32_t *)(UART_BASE + 0x14);
134 	volatile uint32_t * UART_DM_CR = (uint32_t *)(UART_BASE + 0x10);
135 #define UART_DM_TX_READY (1 << 7)
136 #define UART_DM_CLEAR_TX_READY 0x300
137 	volatile uint32_t * UART_DM_NO_CHARS_FOR_TX = (uint32_t *)(UART_BASE + 0x40);
138 	volatile uint32_t * UART_DM_TFWR = (uint32_t *)(UART_BASE + 0x1c);
139 #define UART_DM_TFW_VALUE 0
140 	volatile uint32_t * UART_DM_IPR = (uint32_t *)(UART_BASE + 0x18);
141 #define  UART_DM_STALE_TIMEOUT_LSB 0xf
142 
143 	if (is_init == 0) {
144 		is_init = 1;
145 		*UART_DM_TFWR = UART_DM_TFW_VALUE;
146 		wmb();
147 		*UART_DM_IPR = UART_DM_STALE_TIMEOUT_LSB;
148 		wmb();
149 	}
150 
151 	/* Wait until TXFIFO is empty via ISR */
152 	limit = 100000;
153 	if ((*UART_DM_SR & UART_DM_SR_TXEMT) == 0) {
154 		while (((*UART_DM_ISR & UART_DM_TX_READY) == 0) && --limit) {
155 			/* Note - can't use DELAY here yet, too early */
156 			rmb();
157 		}
158 		*UART_DM_CR = UART_DM_CLEAR_TX_READY;
159 		wmb();
160 	}
161 
162 	/* FIFO is ready.  Say we're going to write one byte */
163 	*UART_DM_NO_CHARS_FOR_TX = 1;
164 	wmb();
165 
166 	limit = 100000;
167 	while (((*UART_DM_SR & UART_DM_SR_TXRDY) == 0) && --limit) {
168 		/* Note - can't use DELAY here yet, too early */
169 		rmb();
170 	}
171 
172 	/* Put character in first fifo slot */
173 	*UART_DM_TF0 = c;
174 	wmb();
175 }
176 early_putc_t *early_putc = qca_msm_early_putc;
177 #endif
178 
179 static platform_method_t ipq4018_methods[] = {
180 	PLATFORMMETHOD(platform_attach,         ipq4018_attach),
181 	PLATFORMMETHOD(platform_devmap_init,    ipq4018_devmap_init),
182 	PLATFORMMETHOD(platform_late_init,      ipq4018_late_init),
183 	PLATFORMMETHOD(platform_cpu_reset,      ipq4018_cpu_reset),
184 
185 #ifdef SMP
186 	PLATFORMMETHOD(platform_mp_start_ap,    ipq4018_mp_start_ap),
187 	PLATFORMMETHOD(platform_mp_setmaxid,    ipq4018_mp_setmaxid),
188 #endif
189 
190 	PLATFORMMETHOD_END,
191 };
192 
193 FDT_PLATFORM_DEF2(ipq4018, ipq4018_ac58u, "ASUS RT-AC58U", 0,
194     "asus,rt-ac58u", 80);
195