xref: /freebsd/sys/arm/mv/armadaxp/armadaxp_mp.c (revision 9e9b17fba7a9b41a0a26dececeedbffa916c29bb)
116694521SOleksandr Tymoshenko /*-
216694521SOleksandr Tymoshenko  * Copyright (c) 2011 Semihalf.
316694521SOleksandr Tymoshenko  * All rights reserved.
416694521SOleksandr Tymoshenko  *
516694521SOleksandr Tymoshenko  * Redistribution and use in source and binary forms, with or without
616694521SOleksandr Tymoshenko  * modification, are permitted provided that the following conditions
716694521SOleksandr Tymoshenko  * are met:
816694521SOleksandr Tymoshenko  * 1. Redistributions of source code must retain the above copyright
916694521SOleksandr Tymoshenko  *    notice, this list of conditions and the following disclaimer.
1016694521SOleksandr Tymoshenko  * 2. Redistributions in binary form must reproduce the above copyright
1116694521SOleksandr Tymoshenko  *    notice, this list of conditions and the following disclaimer in the
1216694521SOleksandr Tymoshenko  *    documentation and/or other materials provided with the distribution.
1316694521SOleksandr Tymoshenko  *
1416694521SOleksandr Tymoshenko  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1516694521SOleksandr Tymoshenko  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1616694521SOleksandr Tymoshenko  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1716694521SOleksandr Tymoshenko  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1816694521SOleksandr Tymoshenko  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1916694521SOleksandr Tymoshenko  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2016694521SOleksandr Tymoshenko  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2116694521SOleksandr Tymoshenko  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2216694521SOleksandr Tymoshenko  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2316694521SOleksandr Tymoshenko  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2416694521SOleksandr Tymoshenko  * SUCH DAMAGE.
2516694521SOleksandr Tymoshenko  *
2616694521SOleksandr Tymoshenko  * $FreeBSD$
2716694521SOleksandr Tymoshenko  */
2816694521SOleksandr Tymoshenko 
2916694521SOleksandr Tymoshenko #include <sys/param.h>
3016694521SOleksandr Tymoshenko #include <sys/systm.h>
3116694521SOleksandr Tymoshenko #include <sys/bus.h>
3216694521SOleksandr Tymoshenko #include <sys/lock.h>
3316694521SOleksandr Tymoshenko #include <sys/mutex.h>
3416694521SOleksandr Tymoshenko #include <sys/smp.h>
3516694521SOleksandr Tymoshenko 
3616694521SOleksandr Tymoshenko #include <machine/smp.h>
3716694521SOleksandr Tymoshenko #include <machine/fdt.h>
3816694521SOleksandr Tymoshenko 
3916694521SOleksandr Tymoshenko #include <arm/mv/mvwin.h>
4016694521SOleksandr Tymoshenko 
4116694521SOleksandr Tymoshenko static int platform_get_ncpus(void);
4216694521SOleksandr Tymoshenko 
4316694521SOleksandr Tymoshenko #define MV_AXP_CPU_DIVCLK_BASE		(MV_BASE + 0x18700)
4416694521SOleksandr Tymoshenko #define CPU_DIVCLK_CTRL0		0x00
4516694521SOleksandr Tymoshenko #define CPU_DIVCLK_CTRL2_RATIO_FULL0	0x08
4616694521SOleksandr Tymoshenko #define CPU_DIVCLK_CTRL2_RATIO_FULL1	0x0c
4716694521SOleksandr Tymoshenko 
4816694521SOleksandr Tymoshenko #define MV_COHERENCY_FABRIC_BASE	(MV_MBUS_BRIDGE_BASE + 0x200)
4916694521SOleksandr Tymoshenko #define COHER_FABRIC_CTRL		0x00
5016694521SOleksandr Tymoshenko #define COHER_FABRIC_CONF		0x04
5116694521SOleksandr Tymoshenko 
5216694521SOleksandr Tymoshenko #define CPU_PMU(x)			(MV_BASE + 0x22100 + (0x100 * (x)))
5316694521SOleksandr Tymoshenko #define CPU_PMU_BOOT			0x24
5416694521SOleksandr Tymoshenko 
5516694521SOleksandr Tymoshenko #define MP				(MV_BASE + 0x20800)
5616694521SOleksandr Tymoshenko #define MP_SW_RESET(x)			((x) * 8)
5716694521SOleksandr Tymoshenko 
5816694521SOleksandr Tymoshenko #define CPU_RESUME_CONTROL		(0x20988)
5916694521SOleksandr Tymoshenko 
6016694521SOleksandr Tymoshenko /* Coherency Fabric registers */
6116694521SOleksandr Tymoshenko static uint32_t
6216694521SOleksandr Tymoshenko read_coher_fabric(uint32_t reg)
6316694521SOleksandr Tymoshenko {
6416694521SOleksandr Tymoshenko 
6516694521SOleksandr Tymoshenko 	return (bus_space_read_4(fdtbus_bs_tag, MV_COHERENCY_FABRIC_BASE, reg));
6616694521SOleksandr Tymoshenko }
6716694521SOleksandr Tymoshenko 
6816694521SOleksandr Tymoshenko static void
6916694521SOleksandr Tymoshenko write_coher_fabric(uint32_t reg, uint32_t val)
7016694521SOleksandr Tymoshenko {
7116694521SOleksandr Tymoshenko 
7216694521SOleksandr Tymoshenko 	bus_space_write_4(fdtbus_bs_tag, MV_COHERENCY_FABRIC_BASE, reg, val);
7316694521SOleksandr Tymoshenko }
7416694521SOleksandr Tymoshenko 
7516694521SOleksandr Tymoshenko /* Coherency Fabric registers */
7616694521SOleksandr Tymoshenko static uint32_t
7716694521SOleksandr Tymoshenko read_cpu_clkdiv(uint32_t reg)
7816694521SOleksandr Tymoshenko {
7916694521SOleksandr Tymoshenko 
8016694521SOleksandr Tymoshenko 	return (bus_space_read_4(fdtbus_bs_tag, MV_AXP_CPU_DIVCLK_BASE, reg));
8116694521SOleksandr Tymoshenko }
8216694521SOleksandr Tymoshenko 
8316694521SOleksandr Tymoshenko static void
8416694521SOleksandr Tymoshenko write_cpu_clkdiv(uint32_t reg, uint32_t val)
8516694521SOleksandr Tymoshenko {
8616694521SOleksandr Tymoshenko 
8716694521SOleksandr Tymoshenko 	bus_space_write_4(fdtbus_bs_tag, MV_AXP_CPU_DIVCLK_BASE, reg, val);
8816694521SOleksandr Tymoshenko }
8916694521SOleksandr Tymoshenko 
9016694521SOleksandr Tymoshenko void
9116694521SOleksandr Tymoshenko platform_mp_setmaxid(void)
9216694521SOleksandr Tymoshenko {
9316694521SOleksandr Tymoshenko 
9416694521SOleksandr Tymoshenko 	mp_maxid = 3;
9516694521SOleksandr Tymoshenko }
9616694521SOleksandr Tymoshenko 
9716694521SOleksandr Tymoshenko int
9816694521SOleksandr Tymoshenko platform_mp_probe(void)
9916694521SOleksandr Tymoshenko {
10016694521SOleksandr Tymoshenko 
10116694521SOleksandr Tymoshenko 	mp_ncpus = platform_get_ncpus();
10216694521SOleksandr Tymoshenko 
10316694521SOleksandr Tymoshenko 	return (mp_ncpus > 1);
10416694521SOleksandr Tymoshenko }
10516694521SOleksandr Tymoshenko 
10616694521SOleksandr Tymoshenko void
10716694521SOleksandr Tymoshenko platform_mp_init_secondary(void)
10816694521SOleksandr Tymoshenko {
10916694521SOleksandr Tymoshenko }
11016694521SOleksandr Tymoshenko 
11116694521SOleksandr Tymoshenko void mpentry(void);
11216694521SOleksandr Tymoshenko void mptramp(void);
11316694521SOleksandr Tymoshenko 
11416694521SOleksandr Tymoshenko static void
11516694521SOleksandr Tymoshenko initialize_coherency_fabric(void)
11616694521SOleksandr Tymoshenko {
11716694521SOleksandr Tymoshenko 	uint32_t val, cpus, mask;
11816694521SOleksandr Tymoshenko 
11916694521SOleksandr Tymoshenko 	cpus = platform_get_ncpus();
12016694521SOleksandr Tymoshenko 	mask = (1 << cpus) - 1;
12116694521SOleksandr Tymoshenko 	val = read_coher_fabric(COHER_FABRIC_CTRL);
12216694521SOleksandr Tymoshenko 	val |= (mask << 24);
12316694521SOleksandr Tymoshenko 	write_coher_fabric(COHER_FABRIC_CTRL, val);
12416694521SOleksandr Tymoshenko 
12516694521SOleksandr Tymoshenko 	val = read_coher_fabric(COHER_FABRIC_CONF);
12616694521SOleksandr Tymoshenko 	val |= (mask << 24);
12716694521SOleksandr Tymoshenko 	write_coher_fabric(COHER_FABRIC_CONF, val);
12816694521SOleksandr Tymoshenko }
12916694521SOleksandr Tymoshenko 
13016694521SOleksandr Tymoshenko 
13116694521SOleksandr Tymoshenko void
13216694521SOleksandr Tymoshenko platform_mp_start_ap(void)
13316694521SOleksandr Tymoshenko {
13416694521SOleksandr Tymoshenko 	uint32_t reg, *ptr, cpu_num;
13516694521SOleksandr Tymoshenko 
13616694521SOleksandr Tymoshenko 	/* Copy boot code to SRAM */
13716694521SOleksandr Tymoshenko 	*((unsigned int*)(0xf1020240)) = 0xffff0101;
13816694521SOleksandr Tymoshenko 	*((unsigned int*)(0xf1008500)) = 0xffff0003;
13916694521SOleksandr Tymoshenko 
14016694521SOleksandr Tymoshenko 	pmap_kenter_nocache(0x880f0000, 0xffff0000);
14116694521SOleksandr Tymoshenko 	reg = 0x880f0000;
14216694521SOleksandr Tymoshenko 
14316694521SOleksandr Tymoshenko 	for (ptr = (uint32_t *)mptramp; ptr < (uint32_t *)mpentry;
14416694521SOleksandr Tymoshenko 	    ptr++, reg += 4)
14516694521SOleksandr Tymoshenko 		*((uint32_t *)reg) = *ptr;
14616694521SOleksandr Tymoshenko 
14716694521SOleksandr Tymoshenko 	if (mp_ncpus > 1) {
14816694521SOleksandr Tymoshenko 		reg = read_cpu_clkdiv(CPU_DIVCLK_CTRL2_RATIO_FULL0);
14916694521SOleksandr Tymoshenko 		reg &= 0x00ffffff;
15016694521SOleksandr Tymoshenko 		reg |= 0x01000000;
15116694521SOleksandr Tymoshenko 		write_cpu_clkdiv(CPU_DIVCLK_CTRL2_RATIO_FULL0, reg);
15216694521SOleksandr Tymoshenko 	}
15316694521SOleksandr Tymoshenko 	if (mp_ncpus > 2) {
15416694521SOleksandr Tymoshenko 		reg = read_cpu_clkdiv(CPU_DIVCLK_CTRL2_RATIO_FULL1);
15516694521SOleksandr Tymoshenko 		reg &= 0xff00ffff;
15616694521SOleksandr Tymoshenko 		reg |= 0x00010000;
15716694521SOleksandr Tymoshenko 		write_cpu_clkdiv(CPU_DIVCLK_CTRL2_RATIO_FULL1, reg);
15816694521SOleksandr Tymoshenko 	}
15916694521SOleksandr Tymoshenko 	if (mp_ncpus > 3) {
16016694521SOleksandr Tymoshenko 		reg = read_cpu_clkdiv(CPU_DIVCLK_CTRL2_RATIO_FULL1);
16116694521SOleksandr Tymoshenko 		reg &= 0x00ffffff;
16216694521SOleksandr Tymoshenko 		reg |= 0x01000000;
16316694521SOleksandr Tymoshenko 		write_cpu_clkdiv(CPU_DIVCLK_CTRL2_RATIO_FULL1, reg);
16416694521SOleksandr Tymoshenko 	}
16516694521SOleksandr Tymoshenko 
16616694521SOleksandr Tymoshenko 	reg = read_cpu_clkdiv(CPU_DIVCLK_CTRL0);
16716694521SOleksandr Tymoshenko 	reg |= ((0x1 << (mp_ncpus - 1)) - 1) << 21;
16816694521SOleksandr Tymoshenko 	write_cpu_clkdiv(CPU_DIVCLK_CTRL0, reg);
16916694521SOleksandr Tymoshenko 	reg = read_cpu_clkdiv(CPU_DIVCLK_CTRL0);
17016694521SOleksandr Tymoshenko 	reg |= 0x01000000;
17116694521SOleksandr Tymoshenko 	write_cpu_clkdiv(CPU_DIVCLK_CTRL0, reg);
17216694521SOleksandr Tymoshenko 
17316694521SOleksandr Tymoshenko 	DELAY(100);
17416694521SOleksandr Tymoshenko 	reg &= ~(0xf << 21);
17516694521SOleksandr Tymoshenko 	write_cpu_clkdiv(CPU_DIVCLK_CTRL0, reg);
17616694521SOleksandr Tymoshenko 	DELAY(100);
17716694521SOleksandr Tymoshenko 
17816694521SOleksandr Tymoshenko 	bus_space_write_4(fdtbus_bs_tag, MV_BASE, CPU_RESUME_CONTROL, 0);
17916694521SOleksandr Tymoshenko 
18016694521SOleksandr Tymoshenko 	for (cpu_num = 1; cpu_num < mp_ncpus; cpu_num++ )
18116694521SOleksandr Tymoshenko 		bus_space_write_4(fdtbus_bs_tag, CPU_PMU(cpu_num), CPU_PMU_BOOT,
182*9e9b17fbSOlivier Houchard 		    pmap_kextract((vm_offset_t)mpentry));
18316694521SOleksandr Tymoshenko 
18416694521SOleksandr Tymoshenko 	cpu_idcache_wbinv_all();
18516694521SOleksandr Tymoshenko 
18616694521SOleksandr Tymoshenko 	for (cpu_num = 1; cpu_num < mp_ncpus; cpu_num++ )
18716694521SOleksandr Tymoshenko 		bus_space_write_4(fdtbus_bs_tag, MP, MP_SW_RESET(cpu_num), 0);
18816694521SOleksandr Tymoshenko 
18916694521SOleksandr Tymoshenko 	/* XXX: Temporary workaround for hangup after releasing AP's */
19016694521SOleksandr Tymoshenko 	wmb();
19116694521SOleksandr Tymoshenko 	DELAY(10);
19216694521SOleksandr Tymoshenko 
19316694521SOleksandr Tymoshenko 	initialize_coherency_fabric();
19416694521SOleksandr Tymoshenko }
19516694521SOleksandr Tymoshenko 
19616694521SOleksandr Tymoshenko static int
19716694521SOleksandr Tymoshenko platform_get_ncpus(void)
19816694521SOleksandr Tymoshenko {
19916694521SOleksandr Tymoshenko 
20016694521SOleksandr Tymoshenko 	return ((read_coher_fabric(COHER_FABRIC_CONF) & 0xf) + 1);
20116694521SOleksandr Tymoshenko }
20216694521SOleksandr Tymoshenko 
20316694521SOleksandr Tymoshenko void
20416694521SOleksandr Tymoshenko platform_ipi_send(cpuset_t cpus, u_int ipi)
20516694521SOleksandr Tymoshenko {
20616694521SOleksandr Tymoshenko 
20716694521SOleksandr Tymoshenko 	pic_ipi_send(cpus, ipi);
20816694521SOleksandr Tymoshenko }
209