xref: /freebsd/sys/arm/mv/armadaxp/armadaxp_mp.c (revision cb5ce014d445d3e5a6aa3219687fb59c9482526d)
116694521SOleksandr Tymoshenko /*-
2af3dc4a7SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3af3dc4a7SPedro F. Giffuni  *
416694521SOleksandr Tymoshenko  * Copyright (c) 2011 Semihalf.
516694521SOleksandr Tymoshenko  * All rights reserved.
616694521SOleksandr Tymoshenko  *
716694521SOleksandr Tymoshenko  * Redistribution and use in source and binary forms, with or without
816694521SOleksandr Tymoshenko  * modification, are permitted provided that the following conditions
916694521SOleksandr Tymoshenko  * are met:
1016694521SOleksandr Tymoshenko  * 1. Redistributions of source code must retain the above copyright
1116694521SOleksandr Tymoshenko  *    notice, this list of conditions and the following disclaimer.
1216694521SOleksandr Tymoshenko  * 2. Redistributions in binary form must reproduce the above copyright
1316694521SOleksandr Tymoshenko  *    notice, this list of conditions and the following disclaimer in the
1416694521SOleksandr Tymoshenko  *    documentation and/or other materials provided with the distribution.
1516694521SOleksandr Tymoshenko  *
1616694521SOleksandr Tymoshenko  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1716694521SOleksandr Tymoshenko  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1816694521SOleksandr Tymoshenko  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1916694521SOleksandr Tymoshenko  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2016694521SOleksandr Tymoshenko  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2116694521SOleksandr Tymoshenko  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2216694521SOleksandr Tymoshenko  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2316694521SOleksandr Tymoshenko  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2416694521SOleksandr Tymoshenko  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2516694521SOleksandr Tymoshenko  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2616694521SOleksandr Tymoshenko  * SUCH DAMAGE.
2716694521SOleksandr Tymoshenko  *
2816694521SOleksandr Tymoshenko  * $FreeBSD$
2916694521SOleksandr Tymoshenko  */
3016694521SOleksandr Tymoshenko 
3116694521SOleksandr Tymoshenko #include <sys/param.h>
3216694521SOleksandr Tymoshenko #include <sys/systm.h>
3316694521SOleksandr Tymoshenko #include <sys/bus.h>
3416694521SOleksandr Tymoshenko #include <sys/lock.h>
3516694521SOleksandr Tymoshenko #include <sys/mutex.h>
3616694521SOleksandr Tymoshenko #include <sys/smp.h>
3716694521SOleksandr Tymoshenko 
385c39c3ffSGrzegorz Bernacki #include <vm/vm.h>
395c39c3ffSGrzegorz Bernacki #include <vm/vm_kern.h>
405c39c3ffSGrzegorz Bernacki #include <vm/vm_extern.h>
41087af50aSAndrew Turner #include <vm/pmap.h>
425c39c3ffSGrzegorz Bernacki 
4347fdf913SZbigniew Bodek #include <dev/fdt/fdt_common.h>
4447fdf913SZbigniew Bodek 
45a89156f5SMichal Meloun #include <machine/cpu.h>
4616694521SOleksandr Tymoshenko #include <machine/smp.h>
4716694521SOleksandr Tymoshenko #include <machine/fdt.h>
485c39c3ffSGrzegorz Bernacki #include <machine/armreg.h>
4916694521SOleksandr Tymoshenko 
5016694521SOleksandr Tymoshenko #include <arm/mv/mvwin.h>
5116694521SOleksandr Tymoshenko 
52ccc1e6ebSMarcin Wojtas #include <machine/platformvar.h>
53ccc1e6ebSMarcin Wojtas 
5416694521SOleksandr Tymoshenko #define MV_AXP_CPU_DIVCLK_BASE		(MV_BASE + 0x18700)
5516694521SOleksandr Tymoshenko #define CPU_DIVCLK_CTRL0		0x00
5616694521SOleksandr Tymoshenko #define CPU_DIVCLK_CTRL2_RATIO_FULL0	0x08
5716694521SOleksandr Tymoshenko #define CPU_DIVCLK_CTRL2_RATIO_FULL1	0x0c
585c39c3ffSGrzegorz Bernacki #define CPU_DIVCLK_MASK(x)		(~(0xff << (8 * (x))))
5916694521SOleksandr Tymoshenko 
6016694521SOleksandr Tymoshenko #define CPU_PMU(x)			(MV_BASE + 0x22100 + (0x100 * (x)))
6116694521SOleksandr Tymoshenko #define CPU_PMU_BOOT			0x24
6216694521SOleksandr Tymoshenko 
6316694521SOleksandr Tymoshenko #define MP				(MV_BASE + 0x20800)
6416694521SOleksandr Tymoshenko #define MP_SW_RESET(x)			((x) * 8)
6516694521SOleksandr Tymoshenko 
6616694521SOleksandr Tymoshenko #define CPU_RESUME_CONTROL		(0x20988)
6716694521SOleksandr Tymoshenko 
685c39c3ffSGrzegorz Bernacki void armadaxp_init_coher_fabric(void);
695c39c3ffSGrzegorz Bernacki int platform_get_ncpus(void);
7016694521SOleksandr Tymoshenko 
71ccc1e6ebSMarcin Wojtas void mv_axp_platform_mp_setmaxid(platform_t plat);
72ccc1e6ebSMarcin Wojtas void mv_axp_platform_mp_start_ap(platform_t plat);
73ccc1e6ebSMarcin Wojtas 
7416694521SOleksandr Tymoshenko /* Coherency Fabric registers */
7516694521SOleksandr Tymoshenko static uint32_t
7616694521SOleksandr Tymoshenko read_cpu_clkdiv(uint32_t reg)
7716694521SOleksandr Tymoshenko {
7816694521SOleksandr Tymoshenko 
7916694521SOleksandr Tymoshenko 	return (bus_space_read_4(fdtbus_bs_tag, MV_AXP_CPU_DIVCLK_BASE, reg));
8016694521SOleksandr Tymoshenko }
8116694521SOleksandr Tymoshenko 
8216694521SOleksandr Tymoshenko static void
8316694521SOleksandr Tymoshenko write_cpu_clkdiv(uint32_t reg, uint32_t val)
8416694521SOleksandr Tymoshenko {
8516694521SOleksandr Tymoshenko 
8616694521SOleksandr Tymoshenko 	bus_space_write_4(fdtbus_bs_tag, MV_AXP_CPU_DIVCLK_BASE, reg, val);
8716694521SOleksandr Tymoshenko }
8816694521SOleksandr Tymoshenko 
8916694521SOleksandr Tymoshenko void
90ccc1e6ebSMarcin Wojtas mv_axp_platform_mp_setmaxid(platform_t plat)
9116694521SOleksandr Tymoshenko {
9216694521SOleksandr Tymoshenko 
9327f38a8dSTijl Coosemans 	mp_ncpus = platform_get_ncpus();
9427f38a8dSTijl Coosemans 	mp_maxid = mp_ncpus - 1;
9516694521SOleksandr Tymoshenko }
9616694521SOleksandr Tymoshenko 
9716694521SOleksandr Tymoshenko void mptramp(void);
98d0307444SZbigniew Bodek void mptramp_end(void);
9947fdf913SZbigniew Bodek extern vm_offset_t mptramp_pmu_boot;
10016694521SOleksandr Tymoshenko 
10116694521SOleksandr Tymoshenko void
102ccc1e6ebSMarcin Wojtas mv_axp_platform_mp_start_ap(platform_t plat)
10316694521SOleksandr Tymoshenko {
1045c39c3ffSGrzegorz Bernacki 	uint32_t reg, *src, *dst, cpu_num, div_val, cputype;
10516b2a62eSIan Lepore 	vm_offset_t pmu_boot_off;
1065c39c3ffSGrzegorz Bernacki 	/*
1075c39c3ffSGrzegorz Bernacki 	 * Initialization procedure depends on core revision,
1085c39c3ffSGrzegorz Bernacki 	 * in this step CHIP ID is checked to choose proper procedure
1095c39c3ffSGrzegorz Bernacki 	 */
110*cb5ce014SAndrew Turner 	cputype = cp15_midr_get();
1115c39c3ffSGrzegorz Bernacki 	cputype &= CPU_ID_CPU_MASK;
11216694521SOleksandr Tymoshenko 
11347fdf913SZbigniew Bodek 	/*
11447fdf913SZbigniew Bodek 	 * Set the PA of CPU0 Boot Address Redirect register used in
11547fdf913SZbigniew Bodek 	 * mptramp according to the actual SoC registers' base address.
11647fdf913SZbigniew Bodek 	 */
11747fdf913SZbigniew Bodek 	pmu_boot_off = (CPU_PMU(0) - MV_BASE) + CPU_PMU_BOOT;
11847fdf913SZbigniew Bodek 	mptramp_pmu_boot = fdt_immr_pa + pmu_boot_off;
11916b2a62eSIan Lepore 	dst = pmap_mapdev(0xffff0000, PAGE_SIZE);
120d0307444SZbigniew Bodek 	for (src = (uint32_t *)mptramp; src < (uint32_t *)mptramp_end;
1215c39c3ffSGrzegorz Bernacki 	    src++, dst++) {
1225c39c3ffSGrzegorz Bernacki 		*dst = *src;
1235c39c3ffSGrzegorz Bernacki 	}
12416b2a62eSIan Lepore 	pmap_unmapdev((vm_offset_t)dst, PAGE_SIZE);
1255c39c3ffSGrzegorz Bernacki 	if (cputype == CPU_ID_MV88SV584X_V7) {
1265c39c3ffSGrzegorz Bernacki 		/* Core rev A0 */
1275c39c3ffSGrzegorz Bernacki 		div_val = read_cpu_clkdiv(CPU_DIVCLK_CTRL2_RATIO_FULL1);
1285c39c3ffSGrzegorz Bernacki 		div_val &= 0x3f;
1295c39c3ffSGrzegorz Bernacki 
1305c39c3ffSGrzegorz Bernacki 		for (cpu_num = 1; cpu_num < mp_ncpus; cpu_num++ ) {
1315c39c3ffSGrzegorz Bernacki 			reg = read_cpu_clkdiv(CPU_DIVCLK_CTRL2_RATIO_FULL1);
1325c39c3ffSGrzegorz Bernacki 			reg &= CPU_DIVCLK_MASK(cpu_num);
1335c39c3ffSGrzegorz Bernacki 			reg |= div_val << (cpu_num * 8);
1345c39c3ffSGrzegorz Bernacki 			write_cpu_clkdiv(CPU_DIVCLK_CTRL2_RATIO_FULL1, reg);
1355c39c3ffSGrzegorz Bernacki 		}
1365c39c3ffSGrzegorz Bernacki 	} else {
1375c39c3ffSGrzegorz Bernacki 		/* Core rev Z1 */
1385c39c3ffSGrzegorz Bernacki 		div_val = 0x01;
13916694521SOleksandr Tymoshenko 
14016694521SOleksandr Tymoshenko 		if (mp_ncpus > 1) {
14116694521SOleksandr Tymoshenko 			reg = read_cpu_clkdiv(CPU_DIVCLK_CTRL2_RATIO_FULL0);
1425c39c3ffSGrzegorz Bernacki 			reg &= CPU_DIVCLK_MASK(3);
1435c39c3ffSGrzegorz Bernacki 			reg |= div_val << 24;
14416694521SOleksandr Tymoshenko 			write_cpu_clkdiv(CPU_DIVCLK_CTRL2_RATIO_FULL0, reg);
14516694521SOleksandr Tymoshenko 		}
1465c39c3ffSGrzegorz Bernacki 
1475c39c3ffSGrzegorz Bernacki 		for (cpu_num = 2; cpu_num < mp_ncpus; cpu_num++ ) {
14816694521SOleksandr Tymoshenko 			reg = read_cpu_clkdiv(CPU_DIVCLK_CTRL2_RATIO_FULL1);
1495c39c3ffSGrzegorz Bernacki 			reg &= CPU_DIVCLK_MASK(cpu_num);
1505c39c3ffSGrzegorz Bernacki 			reg |= div_val << (cpu_num * 8);
15116694521SOleksandr Tymoshenko 			write_cpu_clkdiv(CPU_DIVCLK_CTRL2_RATIO_FULL1, reg);
15216694521SOleksandr Tymoshenko 		}
15316694521SOleksandr Tymoshenko 	}
15416694521SOleksandr Tymoshenko 
15516694521SOleksandr Tymoshenko 	reg = read_cpu_clkdiv(CPU_DIVCLK_CTRL0);
15616694521SOleksandr Tymoshenko 	reg |= ((0x1 << (mp_ncpus - 1)) - 1) << 21;
15716694521SOleksandr Tymoshenko 	write_cpu_clkdiv(CPU_DIVCLK_CTRL0, reg);
15816694521SOleksandr Tymoshenko 	reg = read_cpu_clkdiv(CPU_DIVCLK_CTRL0);
15916694521SOleksandr Tymoshenko 	reg |= 0x01000000;
16016694521SOleksandr Tymoshenko 	write_cpu_clkdiv(CPU_DIVCLK_CTRL0, reg);
16116694521SOleksandr Tymoshenko 
16216694521SOleksandr Tymoshenko 	DELAY(100);
16316694521SOleksandr Tymoshenko 	reg &= ~(0xf << 21);
16416694521SOleksandr Tymoshenko 	write_cpu_clkdiv(CPU_DIVCLK_CTRL0, reg);
16516694521SOleksandr Tymoshenko 	DELAY(100);
16616694521SOleksandr Tymoshenko 
16716694521SOleksandr Tymoshenko 	bus_space_write_4(fdtbus_bs_tag, MV_BASE, CPU_RESUME_CONTROL, 0);
16816694521SOleksandr Tymoshenko 
16916694521SOleksandr Tymoshenko 	for (cpu_num = 1; cpu_num < mp_ncpus; cpu_num++ )
17016694521SOleksandr Tymoshenko 		bus_space_write_4(fdtbus_bs_tag, CPU_PMU(cpu_num), CPU_PMU_BOOT,
1719e9b17fbSOlivier Houchard 		    pmap_kextract((vm_offset_t)mpentry));
17216694521SOleksandr Tymoshenko 
173a89156f5SMichal Meloun 	dcache_wbinv_poc_all();
17416694521SOleksandr Tymoshenko 
17516694521SOleksandr Tymoshenko 	for (cpu_num = 1; cpu_num < mp_ncpus; cpu_num++ )
17616694521SOleksandr Tymoshenko 		bus_space_write_4(fdtbus_bs_tag, MP, MP_SW_RESET(cpu_num), 0);
17716694521SOleksandr Tymoshenko 
17816694521SOleksandr Tymoshenko 	/* XXX: Temporary workaround for hangup after releasing AP's */
17916694521SOleksandr Tymoshenko 	wmb();
18016694521SOleksandr Tymoshenko 	DELAY(10);
18116694521SOleksandr Tymoshenko 
1825c39c3ffSGrzegorz Bernacki 	armadaxp_init_coher_fabric();
18316694521SOleksandr Tymoshenko }
184