Home | History | Annotate | Line # | Download | only in samsung
exynos_platform.c revision 1.22
      1 /* $NetBSD: exynos_platform.c,v 1.22 2019/01/04 15:57:03 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2017 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include "opt_arm_debug.h"
     30 #include "opt_console.h"
     31 #include "opt_exynos.h"
     32 #include "opt_multiprocessor.h"
     33 #include "opt_console.h"
     34 
     35 #include "ukbd.h"
     36 
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: exynos_platform.c,v 1.22 2019/01/04 15:57:03 jmcneill Exp $");
     39 
     40 /* XXXJDM
     41  * Booting a CA7 core on Exynos5422 is currently broken, disable starting CA7 secondaries.
     42  */
     43 #define	EXYNOS5422_DISABLE_CA7_CLUSTER
     44 
     45 #include <sys/param.h>
     46 #include <sys/bus.h>
     47 #include <sys/cpu.h>
     48 #include <sys/device.h>
     49 #include <sys/termios.h>
     50 
     51 #include <dev/fdt/fdtvar.h>
     52 
     53 #include <uvm/uvm_extern.h>
     54 
     55 #include <machine/bootconfig.h>
     56 #include <arm/cpufunc.h>
     57 
     58 #include <arm/samsung/exynos_reg.h>
     59 #include <arm/samsung/exynos_var.h>
     60 #include <arm/samsung/mct_var.h>
     61 #include <arm/samsung/sscom_reg.h>
     62 
     63 #include <evbarm/exynos/platform.h>
     64 #include <evbarm/fdt/machdep.h>
     65 
     66 #include <arm/fdt/arm_fdtvar.h>
     67 
     68 #include <libfdt.h>
     69 
     70 void exynos_platform_early_putchar(char);
     71 
     72 #define	EXYNOS5800_PMU_BASE		0x10040000
     73 #define	EXYNOS5800_PMU_SIZE		0x20000
     74 #define	 EXYNOS5800_PMU_SWRESET			0x0400
     75 #define	  EXYNOS5800_PMU_KFC_ETM_RESET(n)	__BIT(20 + (n))
     76 #define	  EXYNOS5800_PMU_KFC_CORE_RESET(n)	__BIT(8 + (n))
     77 #define	 EXYNOS5800_PMU_SPARE2			0x0908
     78 #define	 EXYNOS5800_PMU_SPARE3			0x090c
     79 #define	  EXYNOS5800_PMU_SWRESET_KFC_SEL	0x3
     80 #define	 EXYNOS5800_PMU_CORE_CONFIG(n)		(0x2000 + 0x80 * (n))
     81 #define	 EXYNOS5800_PMU_CORE_STATUS(n)		(0x2004 + 0x80 * (n))
     82 #define	  EXYNOS5800_PMU_CORE_POWER_EN		0x3
     83 #define	 EXYNOS5800_PMU_COMMON_CONFIG(n)	(0x2500 + 0x80 * (n))
     84 #define	  EXYNOS5800_PMU_COMMON_POWER_EN	0x3
     85 #define	 EXYNOS5800_PMU_COMMON_OPTION(n)	(0x2508 + 0x80 * (n))
     86 #define	  EXYNOS5800_PMU_USE_L2_COMMON_UP_STATE		__BIT(30)
     87 #define	  EXYNOS5800_PMU_USE_ARM_CORE_DOWN_STATE	__BIT(29)
     88 #define	  EXYNOS5800_PMU_AUTO_CORE_DOWN			__BIT(9)
     89 
     90 #define	EXYNOS5800_SYSRAM_BASE		0x02073000
     91 #define	EXYNOS5800_SYSRAM_SIZE		0x1000
     92 #define	 EXYNOS5800_SYSRAM_HOTPLUG		0x001c
     93 
     94 static void
     95 exynos5800_mpstart(void)
     96 {
     97 #if defined(MULTIPROCESSOR)
     98 	bus_space_tag_t bst = &armv7_generic_bs_tag;
     99 	bus_space_handle_t pmu_bsh, sysram_bsh;
    100 	uint64_t mpidr, bp_mpidr;
    101 	uint32_t val, started = 0;
    102 	u_int cpuindex, n;
    103 	int child;
    104 
    105 	bus_space_map(bst, EXYNOS5800_PMU_BASE, EXYNOS5800_PMU_SIZE, 0, &pmu_bsh);
    106 	bus_space_map(bst, EXYNOS5800_SYSRAM_BASE, EXYNOS5800_SYSRAM_SIZE, 0, &sysram_bsh);
    107 
    108 	const int cpus = OF_finddevice("/cpus");
    109 	if (cpus == -1) {
    110 		aprint_error("%s: no /cpus node found\n", __func__);
    111 		return;
    112 	}
    113 
    114 	/* MPIDR affinity levels of boot processor. */
    115 	bp_mpidr = cpu_mpidr_aff_read();
    116 
    117 	/* Setup KFC reset */
    118 	bus_space_write_4(bst, pmu_bsh, EXYNOS5800_PMU_SPARE3, EXYNOS5800_PMU_SWRESET_KFC_SEL);
    119 
    120 	const uint32_t option = EXYNOS5800_PMU_USE_L2_COMMON_UP_STATE |
    121 	    EXYNOS5800_PMU_USE_ARM_CORE_DOWN_STATE |
    122 	    EXYNOS5800_PMU_AUTO_CORE_DOWN;
    123 	val = bus_space_read_4(bst, pmu_bsh, EXYNOS5800_PMU_COMMON_OPTION(0));
    124 	bus_space_write_4(bst, pmu_bsh, EXYNOS5800_PMU_COMMON_OPTION(0), val | option);
    125 	val = bus_space_read_4(bst, pmu_bsh, EXYNOS5800_PMU_COMMON_OPTION(1));
    126 	bus_space_write_4(bst, pmu_bsh, EXYNOS5800_PMU_COMMON_OPTION(1), val | option);
    127 
    128 	bus_space_write_4(bst, sysram_bsh, EXYNOS5800_SYSRAM_HOTPLUG, KERN_VTOPHYS((vaddr_t)cpu_mpstart));
    129 	arm_dsb();
    130 
    131 	/* Power on clusters */
    132 	bus_space_write_4(bst, pmu_bsh, EXYNOS5800_PMU_COMMON_CONFIG(0),
    133 	    EXYNOS5800_PMU_COMMON_POWER_EN);
    134 	bus_space_write_4(bst, pmu_bsh, EXYNOS5800_PMU_COMMON_CONFIG(1),
    135 	    EXYNOS5800_PMU_COMMON_POWER_EN);
    136 
    137 	/* Boot APs */
    138 	cpuindex = 1;
    139 	for (child = OF_child(cpus); child; child = OF_peer(child)) {
    140 		if (fdtbus_get_reg64(child, 0, &mpidr, NULL) != 0)
    141 			continue;
    142 
    143 		if (mpidr == bp_mpidr)
    144 			continue;	/* BP already started */
    145 
    146 		const u_int cluster = __SHIFTOUT(mpidr, MPIDR_AFF1);
    147 		const u_int aff0 = __SHIFTOUT(mpidr, MPIDR_AFF0);
    148 		const u_int cpu = cluster * 4 + aff0;
    149 
    150 #if defined(EXYNOS5422_DISABLE_CA7_CLUSTER)
    151 		if (cluster == 1)
    152 			continue;
    153 #endif
    154 
    155 		val = bus_space_read_4(bst, pmu_bsh, EXYNOS5800_PMU_CORE_STATUS(cpu));
    156 		bus_space_write_4(bst, pmu_bsh, EXYNOS5800_PMU_CORE_CONFIG(cpu),
    157 		    EXYNOS5800_PMU_CORE_POWER_EN);
    158 
    159 		for (n = 0x100000; n > 0; n--) {
    160 			val = bus_space_read_4(bst, pmu_bsh, EXYNOS5800_PMU_CORE_STATUS(cpu));
    161 			if ((val & EXYNOS5800_PMU_CORE_POWER_EN) == EXYNOS5800_PMU_CORE_POWER_EN) {
    162 				started |= __BIT(cpuindex);
    163 				break;
    164 			}
    165 		}
    166 		if (n == 0)
    167 			aprint_error("cpu%d: WARNING: AP failed to power on\n", cpuindex);
    168 
    169 		if (cluster == 1 && __SHIFTOUT(bp_mpidr, MPIDR_AFF1) == 1) {
    170 			while (bus_space_read_4(bst, pmu_bsh, EXYNOS5800_PMU_SPARE2) == 0)
    171 				;
    172 			bus_space_write_4(bst, pmu_bsh, EXYNOS5800_PMU_SWRESET,
    173 			    EXYNOS5800_PMU_KFC_CORE_RESET(aff0) |
    174 			    EXYNOS5800_PMU_KFC_ETM_RESET(aff0));
    175 		}
    176 
    177 		/* Wait for AP to start */
    178 		for (n = 0x100000; n > 0; n--) {
    179 			membar_consumer();
    180 			if (arm_cpu_hatched & __BIT(cpuindex))
    181 				break;
    182 		}
    183 		if (n == 0)
    184 			aprint_error("cpu%d: WARNING: AP failed to start\n", cpuindex);
    185 
    186 		cpuindex++;
    187 	}
    188 
    189 	bus_space_unmap(bst, sysram_bsh, EXYNOS5800_SYSRAM_SIZE);
    190 	bus_space_unmap(bst, pmu_bsh, EXYNOS5800_PMU_SIZE);
    191 #endif
    192 }
    193 
    194 static struct of_compat_data mp_compat_data[] = {
    195 	{ "samsung,exynos5800",		(uintptr_t)exynos5800_mpstart },
    196 	{ NULL }
    197 };
    198 
    199 static void
    200 exynos_platform_mpstart(void)
    201 {
    202 
    203 	void (*mp_start)(void) = NULL;
    204 
    205 	const struct of_compat_data *cd = of_search_compatible(OF_finddevice("/"), mp_compat_data);
    206 	if (cd)
    207 		mp_start = (void (*)(void))cd->data;
    208 
    209 	if (mp_start)
    210 		mp_start();
    211 }
    212 
    213 static void
    214 exynos_platform_init_attach_args(struct fdt_attach_args *faa)
    215 {
    216 	extern struct bus_space armv7_generic_bs_tag;
    217 	extern struct bus_space armv7_generic_a4x_bs_tag;
    218 	extern struct arm32_bus_dma_tag arm_generic_dma_tag;
    219 
    220 	faa->faa_bst = &armv7_generic_bs_tag;
    221 	faa->faa_a4x_bst = &armv7_generic_a4x_bs_tag;
    222 	faa->faa_dmat = &arm_generic_dma_tag;
    223 }
    224 
    225 void
    226 exynos_platform_early_putchar(char c)
    227 {
    228 #ifdef CONSADDR
    229 #define CONSADDR_VA	(CONSADDR - EXYNOS_CORE_PBASE + EXYNOS_CORE_VBASE)
    230 
    231 	volatile uint32_t *uartaddr = cpu_earlydevice_va_p() ?
    232 	    (volatile uint32_t *)CONSADDR_VA :
    233 	    (volatile uint32_t *)CONSADDR;
    234 
    235 	while ((uartaddr[SSCOM_UFSTAT / 4] & UFSTAT_TXFULL) != 0)
    236 		;
    237 
    238 	uartaddr[SSCOM_UTXH / 4] = c;
    239 #endif
    240 }
    241 
    242 static void
    243 exynos_platform_device_register(device_t self, void *aux)
    244 {
    245 	exynos_device_register(self, aux);
    246 }
    247 
    248 static void
    249 exynos5_platform_reset(void)
    250 {
    251 	bus_space_tag_t bst = &armv7_generic_bs_tag;
    252 	bus_space_handle_t bsh;
    253 
    254 	bus_space_map(bst, EXYNOS5800_PMU_BASE + EXYNOS5800_PMU_SWRESET, 4, 0, &bsh);
    255 	bus_space_write_4(bst, bsh, 0, 1);
    256 }
    257 
    258 static u_int
    259 exynos_platform_uart_freq(void)
    260 {
    261 	return EXYNOS_UART_FREQ;
    262 }
    263 
    264 
    265 #if defined(SOC_EXYNOS4)
    266 static const struct pmap_devmap *
    267 exynos4_platform_devmap(void)
    268 {
    269 	static const struct pmap_devmap devmap[] = {
    270 		DEVMAP_ENTRY(EXYNOS_CORE_VBASE,
    271 			     EXYNOS_CORE_PBASE,
    272 			     EXYNOS4_CORE_SIZE),
    273 		DEVMAP_ENTRY(EXYNOS4_AUDIOCORE_VBASE,
    274 			     EXYNOS4_AUDIOCORE_PBASE,
    275 			     EXYNOS4_AUDIOCORE_SIZE),
    276 		DEVMAP_ENTRY_END
    277 	};
    278 
    279 	return devmap;
    280 }
    281 
    282 static void
    283 exynos4_platform_bootstrap(void)
    284 {
    285 
    286 	exynos_bootstrap(4);
    287 
    288 #if defined(MULTIPROCESSOR)
    289 	arm_cpu_max = 1 + __SHIFTOUT(armreg_l2ctrl_read(), L2CTRL_NUMCPU);
    290 #endif
    291 }
    292 
    293 static const struct arm_platform exynos4_platform = {
    294 	.ap_devmap = exynos4_platform_devmap,
    295 //	.ap_mpstart = exynos4_mpstart,
    296 	.ap_bootstrap = exynos4_platform_bootstrap,
    297 	.ap_init_attach_args = exynos_platform_init_attach_args,
    298 	.ap_device_register = exynos_platform_device_register,
    299 	.ap_reset = exynos5_platform_reset,
    300 	.ap_delay = mct_delay,
    301 	.ap_uart_freq = exynos_platform_uart_freq,
    302 };
    303 
    304 ARM_PLATFORM(exynos4, "samsung,exynos4", &exynos4_platform);
    305 #endif
    306 
    307 
    308 #if defined(SOC_EXYNOS5)
    309 static const struct pmap_devmap *
    310 exynos5_platform_devmap(void)
    311 {
    312 	static const struct pmap_devmap devmap[] = {
    313 		DEVMAP_ENTRY(EXYNOS_CORE_VBASE,
    314 			     EXYNOS_CORE_PBASE,
    315 			     EXYNOS5_CORE_SIZE),
    316 		DEVMAP_ENTRY(EXYNOS5_AUDIOCORE_VBASE,
    317 			     EXYNOS5_AUDIOCORE_PBASE,
    318 			     EXYNOS5_AUDIOCORE_SIZE),
    319 		DEVMAP_ENTRY(EXYNOS5_SYSRAM_VBASE,
    320 			     EXYNOS5_SYSRAM_PBASE,
    321 			     EXYNOS5_SYSRAM_SIZE),
    322 		DEVMAP_ENTRY_END
    323 	};
    324 
    325 	return devmap;
    326 }
    327 
    328 static void
    329 exynos5_platform_bootstrap(void)
    330 {
    331 
    332 	exynos_bootstrap(5);
    333 
    334 #if defined(MULTIPROCESSOR) && defined(EXYNOS5422_DISABLE_CA7_CLUSTER)
    335 	const struct of_compat_data *cd = of_search_compatible(OF_finddevice("/"), mp_compat_data);
    336 	if (cd && cd->data == (uintptr_t)exynos5800_mpstart) {
    337 		void *fdt_data = __UNCONST(fdtbus_get_data());
    338 		int cpu_off, cpus_off, len;
    339 
    340 		cpus_off = fdt_path_offset(fdt_data, "/cpus");
    341 		if (cpus_off < 0)
    342 			return;
    343 
    344 		fdt_for_each_subnode(cpu_off, fdt_data, cpus_off) {
    345 			const void *prop = fdt_getprop(fdt_data, cpu_off, "reg", &len);
    346 			if (len != 4)
    347 				continue;
    348 			const uint32_t mpidr = be32dec(prop);
    349 			if (mpidr != cpu_mpidr_aff_read() && __SHIFTOUT(mpidr, MPIDR_AFF1) == 1)
    350 				fdt_setprop_string(fdt_data, cpu_off, "status", "fail");
    351 		}
    352 	}
    353 #endif
    354 
    355 	arm_fdt_cpu_bootstrap();
    356 }
    357 
    358 static const struct arm_platform exynos5_platform = {
    359 	.ap_devmap = exynos5_platform_devmap,
    360 	.ap_bootstrap = exynos5_platform_bootstrap,
    361 	.ap_mpstart = exynos_platform_mpstart,
    362 	.ap_init_attach_args = exynos_platform_init_attach_args,
    363 	.ap_device_register = exynos_platform_device_register,
    364 	.ap_reset = exynos5_platform_reset,
    365 	.ap_delay = mct_delay,
    366 	.ap_uart_freq = exynos_platform_uart_freq,
    367 };
    368 
    369 ARM_PLATFORM(exynos5, "samsung,exynos5", &exynos5_platform);
    370 #endif
    371