xref: /freebsd/sys/arm/broadcom/bcm2835/bcm2835_vcbus.c (revision 324cdd9320f58837c2fbaa7f6ceb9ea5c33d5b2a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2019 Kyle Evans <kevans@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  * $FreeBSD$
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 /*
34  * This file contains facilities for runtime determination of address space
35  * mappings for use in DMA/mailbox interactions.  This is only used for the
36  * arm64 SoC because the 32-bit SoC used the same mappings.
37  */
38 #if defined (__aarch64__)
39 #include "opt_soc.h"
40 #endif
41 
42 #include <sys/types.h>
43 #include <sys/systm.h>
44 
45 #include <dev/ofw/openfirm.h>
46 #include <dev/ofw/ofw_bus.h>
47 #include <dev/ofw/ofw_bus_subr.h>
48 
49 #include <machine/bus.h>
50 
51 #include <arm/broadcom/bcm2835/bcm2835_vcbus.h>
52 
53 /*
54  * This structure describes mappings that need to take place when transforming
55  * ARM core addresses into vcbus addresses for use with the DMA/mailbox
56  * interfaces.  Currently, we only deal with peripheral/SDRAM address spaces
57  * here.
58  *
59  * The SDRAM address space is consistently mapped starting at 0 and extends to
60  * the size of the installed SDRAM.
61  *
62  * Peripherals are mapped further up at spots that vary per-SOC.
63  */
64 struct bcm283x_memory_mapping {
65 	vm_paddr_t	armc_start;
66 	vm_paddr_t	armc_size;
67 	vm_paddr_t	vcbus_start;
68 };
69 
70 #if defined(SOC_BCM2835) || defined(SOC_BCM2836)
71 static struct bcm283x_memory_mapping bcm2835_memmap[] = {
72 	{
73 		/* SDRAM */
74 		.armc_start = 0x00000000,
75 		.armc_size = BCM2835_ARM_IO_BASE,
76 		.vcbus_start = BCM2835_VCBUS_SDRAM_BASE,
77 	},
78 	{
79 		/* Peripherals */
80 		.armc_start = BCM2835_ARM_IO_BASE,
81 		.armc_size  = BCM28XX_ARM_IO_SIZE,
82 		.vcbus_start = BCM2835_VCBUS_IO_BASE,
83 	},
84 	{ 0, 0, 0 },
85 };
86 #endif
87 
88 #ifdef SOC_BRCM_BCM2837
89 static struct bcm283x_memory_mapping bcm2837_memmap[] = {
90 	{
91 		/* SDRAM */
92 		.armc_start = 0x00000000,
93 		.armc_size = BCM2837_ARM_IO_BASE,
94 		.vcbus_start = BCM2837_VCBUS_SDRAM_BASE,
95 	},
96 	{
97 		/* Peripherals */
98 		.armc_start = BCM2837_ARM_IO_BASE,
99 		.armc_size  = BCM28XX_ARM_IO_SIZE,
100 		.vcbus_start = BCM2837_VCBUS_IO_BASE,
101 	},
102 	{ 0, 0, 0 },
103 };
104 #endif
105 
106 #ifdef SOC_BRCM_BCM2838
107 
108 /*
109  * The BCM2838 supports up to 4GB of SDRAM, but unfortunately we can still only
110  * map the first 1GB into the "legacy master view" (vcbus) address space.  Thus,
111  * peripherals can still only access the lower end of SDRAM.  For this reason,
112  * we also capture the main-peripheral busdma restriction below.
113  */
114 static struct bcm283x_memory_mapping bcm2838_memmap[] = {
115 	{
116 		/* SDRAM */
117 		.armc_start = 0x00000000,
118 		.armc_size = 0x40000000,
119 		.vcbus_start = BCM2838_VCBUS_SDRAM_BASE,
120 	},
121 	{
122 		/* Main peripherals */
123 		.armc_start = BCM2838_ARM_IO_BASE,
124 		.armc_size = BCM28XX_ARM_IO_SIZE,
125 		.vcbus_start = BCM2838_VCBUS_IO_BASE,
126 	},
127 	{ 0, 0, 0 },
128 };
129 #endif
130 
131 static struct bcm283x_memory_soc_cfg {
132 	struct bcm283x_memory_mapping	*memmap;
133 	const char			*soc_compat;
134 	bus_addr_t			 busdma_lowaddr;
135 } bcm283x_memory_configs[] = {
136 #ifdef SOC_BCM2835
137 	{
138 		.memmap = bcm2835_memmap,
139 		.soc_compat = "brcm,bcm2835",
140 		.busdma_lowaddr = BUS_SPACE_MAXADDR_32BIT,
141 	},
142 #endif
143 #ifdef SOC_BCM2836
144 	{
145 		.memmap = bcm2835_memmap,
146 		.soc_compat = "brcm,bcm2836",
147 		.busdma_lowaddr = BUS_SPACE_MAXADDR_32BIT,
148 	},
149 
150 #endif
151 #ifdef SOC_BRCM_BCM2837
152 	{
153 		.memmap = bcm2837_memmap,
154 		.soc_compat = "brcm,bcm2837",
155 		.busdma_lowaddr = BUS_SPACE_MAXADDR_32BIT,
156 	},
157 #endif
158 #ifdef SOC_BRCM_BCM2838
159 	{
160 		.memmap = bcm2838_memmap,
161 		.soc_compat = "brcm,bcm2711",
162 		.busdma_lowaddr = BCM2838_PERIPH_MAXADDR,
163 	},
164 	{
165 		.memmap = bcm2838_memmap,
166 		.soc_compat = "brcm,bcm2838",
167 		.busdma_lowaddr = BCM2838_PERIPH_MAXADDR,
168 	},
169 #endif
170 };
171 
172 static struct bcm283x_memory_soc_cfg *booted_soc_memcfg;
173 
174 static struct bcm283x_memory_soc_cfg *
175 bcm283x_get_current_memcfg(void)
176 {
177 	phandle_t root;
178 	int i;
179 
180 	/* We'll cache it once we decide, because it won't change per-boot. */
181 	if (booted_soc_memcfg != NULL)
182 		return (booted_soc_memcfg);
183 
184 	KASSERT(nitems(bcm283x_memory_configs) != 0,
185 	    ("No SOC memory configurations enabled!"));
186 
187 	root = OF_finddevice("/");
188 	for (i = 0; i < nitems(bcm283x_memory_configs); ++i) {
189 		booted_soc_memcfg = &bcm283x_memory_configs[i];
190 		printf("Checking root against %s\n",
191 		booted_soc_memcfg->soc_compat);
192 		if (ofw_bus_node_is_compatible(root,
193 		    booted_soc_memcfg->soc_compat))
194 			return (booted_soc_memcfg);
195 	}
196 
197 	/*
198 	 * The kernel doesn't fit the board; we can't really make a reasonable
199 	 * guess, as these SOC are different enough that something will blow up
200 	 * later.
201 	 */
202 	panic("No suitable SOC memory configuration found.");
203 }
204 
205 #define	BCM283X_MEMMAP_ISTERM(ent)	\
206     ((ent)->armc_start == 0 && (ent)->armc_size == 0 && \
207     (ent)->vcbus_start == 0)
208 
209 vm_paddr_t
210 bcm283x_armc_to_vcbus(vm_paddr_t pa)
211 {
212 	struct bcm283x_memory_soc_cfg *cfg;
213 	struct bcm283x_memory_mapping *map, *ment;
214 
215 	/* Guaranteed not NULL if we haven't panicked yet. */
216 	cfg = bcm283x_get_current_memcfg();
217 	map = cfg->memmap;
218 	for (ment = map; !BCM283X_MEMMAP_ISTERM(ment); ++ment) {
219 		if (pa >= ment->armc_start &&
220 		    pa < ment->armc_start + ment->armc_size) {
221 			return (pa - ment->armc_start) + ment->vcbus_start;
222 		}
223 	}
224 
225 	/*
226 	 * Assume 1:1 mapping for anything else, but complain about it on
227 	 * verbose boots.
228 	 */
229 	if (bootverbose)
230 		printf("bcm283x_vcbus: No armc -> vcbus mapping found: %jx\n",
231 		    (uintmax_t)pa);
232 	return (pa);
233 }
234 
235 vm_paddr_t
236 bcm283x_vcbus_to_armc(vm_paddr_t vca)
237 {
238 	struct bcm283x_memory_soc_cfg *cfg;
239 	struct bcm283x_memory_mapping *map, *ment;
240 
241 	/* Guaranteed not NULL if we haven't panicked yet. */
242 	cfg = bcm283x_get_current_memcfg();
243 	map = cfg->memmap;
244 	for (ment = map; !BCM283X_MEMMAP_ISTERM(ment); ++ment) {
245 		if (vca >= ment->vcbus_start &&
246 		    vca < ment->vcbus_start + ment->armc_size) {
247 			return (vca - ment->vcbus_start) + ment->armc_start;
248 		}
249 	}
250 
251 	/*
252 	 * Assume 1:1 mapping for anything else, but complain about it on
253 	 * verbose boots.
254 	 */
255 	if (bootverbose)
256 		printf("bcm283x_vcbus: No vcbus -> armc mapping found: %jx\n",
257 		    (uintmax_t)vca);
258 	return (vca);
259 }
260 
261 bus_addr_t
262 bcm283x_dmabus_peripheral_lowaddr(void)
263 {
264 	struct bcm283x_memory_soc_cfg *cfg;
265 
266 	cfg = bcm283x_get_current_memcfg();
267 	return (cfg->busdma_lowaddr);
268 }
269