xref: /freebsd/sys/arm/broadcom/bcm2835/bcm2835_vcbus.c (revision 8b238f4126d32df3e70056bc32536b7248ebffa0)
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 #ifdef SOC_BCM2835
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_BCM2836
89 static struct bcm283x_memory_mapping bcm2836_memmap[] = {
90 	{
91 		/* SDRAM */
92 		.armc_start = 0x00000000,
93 		.armc_size = BCM2836_ARM_IO_BASE,
94 		.vcbus_start = BCM2836_VCBUS_SDRAM_BASE,
95 	},
96 	{
97 		/* Peripherals */
98 		.armc_start = BCM2836_ARM_IO_BASE,
99 		.armc_size  = BCM28XX_ARM_IO_SIZE,
100 		.vcbus_start = BCM2836_VCBUS_IO_BASE,
101 	},
102 	{ 0, 0, 0 },
103 };
104 #endif
105 
106 #ifdef SOC_BRCM_BCM2837
107 static struct bcm283x_memory_mapping bcm2837_memmap[] = {
108 	{
109 		/* SDRAM */
110 		.armc_start = 0x00000000,
111 		.armc_size = BCM2837_ARM_IO_BASE,
112 		.vcbus_start = BCM2837_VCBUS_SDRAM_BASE,
113 	},
114 	{
115 		/* Peripherals */
116 		.armc_start = BCM2837_ARM_IO_BASE,
117 		.armc_size  = BCM28XX_ARM_IO_SIZE,
118 		.vcbus_start = BCM2837_VCBUS_IO_BASE,
119 	},
120 	{ 0, 0, 0 },
121 };
122 #endif
123 
124 #ifdef SOC_BRCM_BCM2838
125 
126 /*
127  * The BCM2838 supports up to 4GB of SDRAM, but unfortunately we can still only
128  * map the first 1GB into the "legacy master view" (vcbus) address space.  Thus,
129  * peripherals can still only access the lower end of SDRAM.  For this reason,
130  * we also capture the main-peripheral busdma restriction below.
131  */
132 static struct bcm283x_memory_mapping bcm2838_memmap[] = {
133 	{
134 		/* SDRAM */
135 		.armc_start = 0x00000000,
136 		.armc_size = 0x40000000,
137 		.vcbus_start = BCM2838_VCBUS_SDRAM_BASE,
138 	},
139 	{
140 		/* Main peripherals */
141 		.armc_start = BCM2838_ARM_IO_BASE,
142 		.armc_size = BCM28XX_ARM_IO_SIZE,
143 		.vcbus_start = BCM2838_VCBUS_IO_BASE,
144 	},
145 	{ 0, 0, 0 },
146 };
147 #endif
148 
149 static struct bcm283x_memory_soc_cfg {
150 	struct bcm283x_memory_mapping	*memmap;
151 	const char			*soc_compat;
152 	bus_addr_t			 busdma_lowaddr;
153 } bcm283x_memory_configs[] = {
154 #ifdef SOC_BCM2835
155 	{
156 		.memmap = bcm2835_memmap,
157 		.soc_compat = "brcm,bcm2835",
158 		.busdma_lowaddr = BUS_SPACE_MAXADDR_32BIT,
159 	},
160 #endif
161 #ifdef SOC_BCM2836
162 	{
163 		.memmap = bcm2836_memmap,
164 		.soc_compat = "brcm,bcm2836",
165 		.busdma_lowaddr = BUS_SPACE_MAXADDR_32BIT,
166 	},
167 
168 #endif
169 #ifdef SOC_BRCM_BCM2837
170 	{
171 		.memmap = bcm2837_memmap,
172 		.soc_compat = "brcm,bcm2837",
173 		.busdma_lowaddr = BUS_SPACE_MAXADDR_32BIT,
174 	},
175 #endif
176 #ifdef SOC_BRCM_BCM2838
177 	{
178 		.memmap = bcm2838_memmap,
179 		.soc_compat = "brcm,bcm2711",
180 		.busdma_lowaddr = BCM2838_PERIPH_MAXADDR,
181 	},
182 	{
183 		.memmap = bcm2838_memmap,
184 		.soc_compat = "brcm,bcm2838",
185 		.busdma_lowaddr = BCM2838_PERIPH_MAXADDR,
186 	},
187 #endif
188 };
189 
190 static struct bcm283x_memory_soc_cfg *booted_soc_memcfg;
191 
192 static struct bcm283x_memory_soc_cfg *
193 bcm283x_get_current_memcfg(void)
194 {
195 	phandle_t root;
196 	int i;
197 
198 	/* We'll cache it once we decide, because it won't change per-boot. */
199 	if (booted_soc_memcfg != NULL)
200 		return (booted_soc_memcfg);
201 
202 	KASSERT(nitems(bcm283x_memory_configs) != 0,
203 	    ("No SOC memory configurations enabled!"));
204 
205 	root = OF_finddevice("/");
206 	for (i = 0; i < nitems(bcm283x_memory_configs); ++i) {
207 		booted_soc_memcfg = &bcm283x_memory_configs[i];
208 		printf("Checking root against %s\n",
209 		booted_soc_memcfg->soc_compat);
210 		if (ofw_bus_node_is_compatible(root,
211 		    booted_soc_memcfg->soc_compat))
212 			return (booted_soc_memcfg);
213 	}
214 
215 	/*
216 	 * The kernel doesn't fit the board; we can't really make a reasonable
217 	 * guess, as these SOC are different enough that something will blow up
218 	 * later.
219 	 */
220 	panic("No suitable SOC memory configuration found.");
221 }
222 
223 #define	BCM283X_MEMMAP_ISTERM(ent)	\
224     ((ent)->armc_start == 0 && (ent)->armc_size == 0 && \
225     (ent)->vcbus_start == 0)
226 
227 vm_paddr_t
228 bcm283x_armc_to_vcbus(vm_paddr_t pa)
229 {
230 	struct bcm283x_memory_soc_cfg *cfg;
231 	struct bcm283x_memory_mapping *map, *ment;
232 
233 	/* Guaranteed not NULL if we haven't panicked yet. */
234 	cfg = bcm283x_get_current_memcfg();
235 	map = cfg->memmap;
236 	for (ment = map; !BCM283X_MEMMAP_ISTERM(ment); ++ment) {
237 		if (pa >= ment->armc_start &&
238 		    pa < ment->armc_start + ment->armc_size) {
239 			return (pa - ment->armc_start) + ment->vcbus_start;
240 		}
241 	}
242 
243 	/*
244 	 * Assume 1:1 mapping for anything else, but complain about it on
245 	 * verbose boots.
246 	 */
247 	if (bootverbose)
248 		printf("bcm283x_vcbus: No armc -> vcbus mapping found: %jx\n",
249 		    (uintmax_t)pa);
250 	return (pa);
251 }
252 
253 vm_paddr_t
254 bcm283x_vcbus_to_armc(vm_paddr_t vca)
255 {
256 	struct bcm283x_memory_soc_cfg *cfg;
257 	struct bcm283x_memory_mapping *map, *ment;
258 
259 	/* Guaranteed not NULL if we haven't panicked yet. */
260 	cfg = bcm283x_get_current_memcfg();
261 	map = cfg->memmap;
262 	for (ment = map; !BCM283X_MEMMAP_ISTERM(ment); ++ment) {
263 		if (vca >= ment->vcbus_start &&
264 		    vca < ment->vcbus_start + ment->armc_size) {
265 			return (vca - ment->vcbus_start) + ment->armc_start;
266 		}
267 	}
268 
269 	/*
270 	 * Assume 1:1 mapping for anything else, but complain about it on
271 	 * verbose boots.
272 	 */
273 	if (bootverbose)
274 		printf("bcm283x_vcbus: No vcbus -> armc mapping found: %jx\n",
275 		    (uintmax_t)vca);
276 	return (vca);
277 }
278 
279 bus_addr_t
280 bcm283x_dmabus_peripheral_lowaddr(void)
281 {
282 	struct bcm283x_memory_soc_cfg *cfg;
283 
284 	cfg = bcm283x_get_current_memcfg();
285 	return (cfg->busdma_lowaddr);
286 }
287