xref: /NextBSD/sys/arm/amlogic/aml8726/aml8726_machdep.c (revision 84d351007654069f9643c8e4b4802a7f5f08ee42)
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 #include "opt_global.h"
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include "opt_platform.h"
33 
34 #define _ARM32_BUS_DMA_PRIVATE
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/bus.h>
38 
39 #include <vm/vm.h>
40 #include <vm/pmap.h>
41 
42 #include <machine/bus.h>
43 #include <machine/cpufunc.h>
44 #include <machine/devmap.h>
45 #include <machine/intr.h>
46 #include <machine/machdep.h>
47 #include <machine/platform.h>
48 
49 #include <dev/fdt/fdt_common.h>
50 
51 #include <arm/amlogic/aml8726/aml8726_soc.h>
52 #include <arm/amlogic/aml8726/aml8726_clkmsr.h>
53 
54 #if defined(SOCDEV_PA) && defined(SOCDEV_VA)
55 vm_offset_t aml8726_aobus_kva_base = SOCDEV_VA;
56 #else
57 vm_offset_t aml8726_aobus_kva_base;
58 #endif
59 
60 static void
aml8726_fixup_busfreq()61 aml8726_fixup_busfreq()
62 {
63 	phandle_t node;
64 	pcell_t freq, prop;
65 	ssize_t len;
66 
67 	/*
68 	 * Set the bus-frequency for the SoC simple-bus if it
69 	 * needs updating (meaning the current frequency is zero).
70 	 */
71 
72 	if ((freq = aml8726_clkmsr_bus_frequency()) == 0 ||
73 	    (node = OF_finddevice("/soc")) == 0 ||
74 	    fdt_is_compatible_strict(node, "simple-bus") == 0)
75 		while (1);
76 
77 	freq = cpu_to_fdt32(freq);
78 
79 	len = OF_getencprop(node, "bus-frequency", &prop, sizeof(prop));
80 	if ((len / sizeof(prop)) == 1 && prop == 0)
81 		OF_setprop(node, "bus-frequency", (void *)&freq, sizeof(freq));
82 }
83 
84 vm_offset_t
platform_lastaddr(void)85 platform_lastaddr(void)
86 {
87 
88 	return (arm_devmap_lastaddr());
89 }
90 
91 void
platform_probe_and_attach(void)92 platform_probe_and_attach(void)
93 {
94 }
95 
96 void
platform_gpio_init(void)97 platform_gpio_init(void)
98 {
99 
100 	/*
101 	 * The UART console driver used for debugging early boot code
102 	 * needs to know the virtual base address of the aobus.  It's
103 	 * expected to equal SOCDEV_VA prior to initarm calling setttb
104 	 * ... afterwards it needs to be updated due to the new page
105 	 * tables.
106 	 *
107 	 * This means there's a deadzone in initarm between setttb
108 	 * and platform_gpio_init during which printf can't be used.
109 	 */
110 	aml8726_aobus_kva_base =
111 	    (vm_offset_t)arm_devmap_ptov(0xc8100000, 0x100000);
112 
113 	/*
114 	 * The hardware mux used by clkmsr is unique to the SoC (though
115 	 * currently clk81 is at a fixed location, however that might
116 	 * change in the future).
117 	 */
118 	aml8726_identify_soc();
119 
120 	/*
121 	 * My aml8726-m3 development box which identifies the CPU as
122 	 * a Cortex A9-r2 rev 4 randomly locks up during boot when WFI
123 	 * is used.
124 	 */
125 	switch (aml8726_soc_hw_rev) {
126 	case AML_SOC_HW_REV_M3:
127 		cpufuncs.cf_sleep = (void *)cpufunc_nullop;
128 		break;
129 	default:
130 		break;
131 	}
132 
133 	/*
134 	 * This FDT fixup should arguably be called through fdt_fixup_table,
135 	 * however currently there's no mechanism to specify a fixup which
136 	 * should always be invoked.
137 	 *
138 	 * It needs to be called prior to the console being initialized which
139 	 * is why it's called here, rather than from platform_late_init.
140 	 */
141 	aml8726_fixup_busfreq();
142 }
143 
144 void
platform_late_init(void)145 platform_late_init(void)
146 {
147 }
148 
149 /*
150  * Construct static devmap entries to map out the core
151  * peripherals using 1mb section mappings.
152  */
153 int
platform_devmap_init(void)154 platform_devmap_init(void)
155 {
156 
157 	arm_devmap_add_entry(0xc1100000, 0x200000); /* cbus */
158 	arm_devmap_add_entry(0xc4200000, 0x100000); /* pl310 */
159 	arm_devmap_add_entry(0xc4300000, 0x100000); /* periph */
160 	arm_devmap_add_entry(0xc8000000, 0x100000); /* apbbus */
161 	arm_devmap_add_entry(0xc8100000, 0x100000); /* aobus */
162 	arm_devmap_add_entry(0xc9000000, 0x800000); /* ahbbus */
163 	arm_devmap_add_entry(0xd9000000, 0x100000); /* ahb */
164 	arm_devmap_add_entry(0xda000000, 0x100000); /* secbus */
165 
166 	return (0);
167 }
168 
169 struct arm32_dma_range *
bus_dma_get_range(void)170 bus_dma_get_range(void)
171 {
172 
173 	return (NULL);
174 }
175 
176 int
bus_dma_get_range_nb(void)177 bus_dma_get_range_nb(void)
178 {
179 
180 	return (0);
181 }
182 
183 struct fdt_fixup_entry fdt_fixup_table[] = {
184 	{ NULL, NULL }
185 };
186 
187 #ifndef DEV_GIC
188 static int
fdt_pic_decode_ic(phandle_t node,pcell_t * intr,int * interrupt,int * trig,int * pol)189 fdt_pic_decode_ic(phandle_t node, pcell_t *intr, int *interrupt, int *trig,
190     int *pol)
191 {
192 
193 	/*
194 	 * The single core chips have just an Amlogic PIC.
195 	 */
196 	if (!fdt_is_compatible_strict(node, "amlogic,aml8726-pic"))
197 		return (ENXIO);
198 
199 	*interrupt = fdt32_to_cpu(intr[1]);
200 	*trig = INTR_TRIGGER_EDGE;
201 	*pol = INTR_POLARITY_HIGH;
202 
203 	return (0);
204 }
205 #endif
206 
207 fdt_pic_decode_t fdt_pic_table[] = {
208 #ifdef DEV_GIC
209 	&gic_decode_fdt,
210 #else
211 	&fdt_pic_decode_ic,
212 #endif
213 	NULL
214 };
215