xref: /freebsd/sys/arm/mv/armadaxp/armadaxp_mp.c (revision 16694521fe80f54bf8f37334fe9a54bc5c953a6a)
1*16694521SOleksandr Tymoshenko /*-
2*16694521SOleksandr Tymoshenko  * Copyright (c) 2011 Semihalf.
3*16694521SOleksandr Tymoshenko  * All rights reserved.
4*16694521SOleksandr Tymoshenko  *
5*16694521SOleksandr Tymoshenko  * Redistribution and use in source and binary forms, with or without
6*16694521SOleksandr Tymoshenko  * modification, are permitted provided that the following conditions
7*16694521SOleksandr Tymoshenko  * are met:
8*16694521SOleksandr Tymoshenko  * 1. Redistributions of source code must retain the above copyright
9*16694521SOleksandr Tymoshenko  *    notice, this list of conditions and the following disclaimer.
10*16694521SOleksandr Tymoshenko  * 2. Redistributions in binary form must reproduce the above copyright
11*16694521SOleksandr Tymoshenko  *    notice, this list of conditions and the following disclaimer in the
12*16694521SOleksandr Tymoshenko  *    documentation and/or other materials provided with the distribution.
13*16694521SOleksandr Tymoshenko  *
14*16694521SOleksandr Tymoshenko  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*16694521SOleksandr Tymoshenko  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*16694521SOleksandr Tymoshenko  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*16694521SOleksandr Tymoshenko  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*16694521SOleksandr Tymoshenko  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*16694521SOleksandr Tymoshenko  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*16694521SOleksandr Tymoshenko  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*16694521SOleksandr Tymoshenko  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*16694521SOleksandr Tymoshenko  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*16694521SOleksandr Tymoshenko  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*16694521SOleksandr Tymoshenko  * SUCH DAMAGE.
25*16694521SOleksandr Tymoshenko  *
26*16694521SOleksandr Tymoshenko  * $FreeBSD$
27*16694521SOleksandr Tymoshenko  */
28*16694521SOleksandr Tymoshenko 
29*16694521SOleksandr Tymoshenko #include <sys/param.h>
30*16694521SOleksandr Tymoshenko #include <sys/systm.h>
31*16694521SOleksandr Tymoshenko #include <sys/bus.h>
32*16694521SOleksandr Tymoshenko #include <sys/lock.h>
33*16694521SOleksandr Tymoshenko #include <sys/mutex.h>
34*16694521SOleksandr Tymoshenko #include <sys/smp.h>
35*16694521SOleksandr Tymoshenko 
36*16694521SOleksandr Tymoshenko #include <machine/smp.h>
37*16694521SOleksandr Tymoshenko #include <machine/fdt.h>
38*16694521SOleksandr Tymoshenko 
39*16694521SOleksandr Tymoshenko #include <arm/mv/mvwin.h>
40*16694521SOleksandr Tymoshenko 
41*16694521SOleksandr Tymoshenko static int platform_get_ncpus(void);
42*16694521SOleksandr Tymoshenko 
43*16694521SOleksandr Tymoshenko #define MV_AXP_CPU_DIVCLK_BASE		(MV_BASE + 0x18700)
44*16694521SOleksandr Tymoshenko #define CPU_DIVCLK_CTRL0		0x00
45*16694521SOleksandr Tymoshenko #define CPU_DIVCLK_CTRL2_RATIO_FULL0	0x08
46*16694521SOleksandr Tymoshenko #define CPU_DIVCLK_CTRL2_RATIO_FULL1	0x0c
47*16694521SOleksandr Tymoshenko 
48*16694521SOleksandr Tymoshenko #define MV_COHERENCY_FABRIC_BASE	(MV_MBUS_BRIDGE_BASE + 0x200)
49*16694521SOleksandr Tymoshenko #define COHER_FABRIC_CTRL		0x00
50*16694521SOleksandr Tymoshenko #define COHER_FABRIC_CONF		0x04
51*16694521SOleksandr Tymoshenko 
52*16694521SOleksandr Tymoshenko #define CPU_PMU(x)			(MV_BASE + 0x22100 + (0x100 * (x)))
53*16694521SOleksandr Tymoshenko #define CPU_PMU_BOOT			0x24
54*16694521SOleksandr Tymoshenko 
55*16694521SOleksandr Tymoshenko #define MP				(MV_BASE + 0x20800)
56*16694521SOleksandr Tymoshenko #define MP_SW_RESET(x)			((x) * 8)
57*16694521SOleksandr Tymoshenko 
58*16694521SOleksandr Tymoshenko #define CPU_RESUME_CONTROL		(0x20988)
59*16694521SOleksandr Tymoshenko 
60*16694521SOleksandr Tymoshenko /* Coherency Fabric registers */
61*16694521SOleksandr Tymoshenko static uint32_t
62*16694521SOleksandr Tymoshenko read_coher_fabric(uint32_t reg)
63*16694521SOleksandr Tymoshenko {
64*16694521SOleksandr Tymoshenko 
65*16694521SOleksandr Tymoshenko 	return (bus_space_read_4(fdtbus_bs_tag, MV_COHERENCY_FABRIC_BASE, reg));
66*16694521SOleksandr Tymoshenko }
67*16694521SOleksandr Tymoshenko 
68*16694521SOleksandr Tymoshenko static void
69*16694521SOleksandr Tymoshenko write_coher_fabric(uint32_t reg, uint32_t val)
70*16694521SOleksandr Tymoshenko {
71*16694521SOleksandr Tymoshenko 
72*16694521SOleksandr Tymoshenko 	bus_space_write_4(fdtbus_bs_tag, MV_COHERENCY_FABRIC_BASE, reg, val);
73*16694521SOleksandr Tymoshenko }
74*16694521SOleksandr Tymoshenko 
75*16694521SOleksandr Tymoshenko /* Coherency Fabric registers */
76*16694521SOleksandr Tymoshenko static uint32_t
77*16694521SOleksandr Tymoshenko read_cpu_clkdiv(uint32_t reg)
78*16694521SOleksandr Tymoshenko {
79*16694521SOleksandr Tymoshenko 
80*16694521SOleksandr Tymoshenko 	return (bus_space_read_4(fdtbus_bs_tag, MV_AXP_CPU_DIVCLK_BASE, reg));
81*16694521SOleksandr Tymoshenko }
82*16694521SOleksandr Tymoshenko 
83*16694521SOleksandr Tymoshenko static void
84*16694521SOleksandr Tymoshenko write_cpu_clkdiv(uint32_t reg, uint32_t val)
85*16694521SOleksandr Tymoshenko {
86*16694521SOleksandr Tymoshenko 
87*16694521SOleksandr Tymoshenko 	bus_space_write_4(fdtbus_bs_tag, MV_AXP_CPU_DIVCLK_BASE, reg, val);
88*16694521SOleksandr Tymoshenko }
89*16694521SOleksandr Tymoshenko 
90*16694521SOleksandr Tymoshenko void
91*16694521SOleksandr Tymoshenko platform_mp_setmaxid(void)
92*16694521SOleksandr Tymoshenko {
93*16694521SOleksandr Tymoshenko 
94*16694521SOleksandr Tymoshenko 	mp_maxid = 3;
95*16694521SOleksandr Tymoshenko }
96*16694521SOleksandr Tymoshenko 
97*16694521SOleksandr Tymoshenko int
98*16694521SOleksandr Tymoshenko platform_mp_probe(void)
99*16694521SOleksandr Tymoshenko {
100*16694521SOleksandr Tymoshenko 
101*16694521SOleksandr Tymoshenko 	mp_ncpus = platform_get_ncpus();
102*16694521SOleksandr Tymoshenko 
103*16694521SOleksandr Tymoshenko 	return (mp_ncpus > 1);
104*16694521SOleksandr Tymoshenko }
105*16694521SOleksandr Tymoshenko 
106*16694521SOleksandr Tymoshenko void
107*16694521SOleksandr Tymoshenko platform_mp_init_secondary(void)
108*16694521SOleksandr Tymoshenko {
109*16694521SOleksandr Tymoshenko }
110*16694521SOleksandr Tymoshenko 
111*16694521SOleksandr Tymoshenko void mpentry(void);
112*16694521SOleksandr Tymoshenko void mptramp(void);
113*16694521SOleksandr Tymoshenko 
114*16694521SOleksandr Tymoshenko static void
115*16694521SOleksandr Tymoshenko initialize_coherency_fabric(void)
116*16694521SOleksandr Tymoshenko {
117*16694521SOleksandr Tymoshenko 	uint32_t val, cpus, mask;
118*16694521SOleksandr Tymoshenko 
119*16694521SOleksandr Tymoshenko 	cpus = platform_get_ncpus();
120*16694521SOleksandr Tymoshenko 	mask = (1 << cpus) - 1;
121*16694521SOleksandr Tymoshenko 	val = read_coher_fabric(COHER_FABRIC_CTRL);
122*16694521SOleksandr Tymoshenko 	val |= (mask << 24);
123*16694521SOleksandr Tymoshenko 	write_coher_fabric(COHER_FABRIC_CTRL, val);
124*16694521SOleksandr Tymoshenko 
125*16694521SOleksandr Tymoshenko 	val = read_coher_fabric(COHER_FABRIC_CONF);
126*16694521SOleksandr Tymoshenko 	val |= (mask << 24);
127*16694521SOleksandr Tymoshenko 	write_coher_fabric(COHER_FABRIC_CONF, val);
128*16694521SOleksandr Tymoshenko }
129*16694521SOleksandr Tymoshenko 
130*16694521SOleksandr Tymoshenko 
131*16694521SOleksandr Tymoshenko void
132*16694521SOleksandr Tymoshenko platform_mp_start_ap(void)
133*16694521SOleksandr Tymoshenko {
134*16694521SOleksandr Tymoshenko 	uint32_t reg, *ptr, cpu_num;
135*16694521SOleksandr Tymoshenko 
136*16694521SOleksandr Tymoshenko 	/* Copy boot code to SRAM */
137*16694521SOleksandr Tymoshenko 	*((unsigned int*)(0xf1020240)) = 0xffff0101;
138*16694521SOleksandr Tymoshenko 	*((unsigned int*)(0xf1008500)) = 0xffff0003;
139*16694521SOleksandr Tymoshenko 
140*16694521SOleksandr Tymoshenko 	pmap_kenter_nocache(0x880f0000, 0xffff0000);
141*16694521SOleksandr Tymoshenko 	reg = 0x880f0000;
142*16694521SOleksandr Tymoshenko 
143*16694521SOleksandr Tymoshenko 	for (ptr = (uint32_t *)mptramp; ptr < (uint32_t *)mpentry;
144*16694521SOleksandr Tymoshenko 	    ptr++, reg += 4)
145*16694521SOleksandr Tymoshenko 		*((uint32_t *)reg) = *ptr;
146*16694521SOleksandr Tymoshenko 
147*16694521SOleksandr Tymoshenko 	if (mp_ncpus > 1) {
148*16694521SOleksandr Tymoshenko 		reg = read_cpu_clkdiv(CPU_DIVCLK_CTRL2_RATIO_FULL0);
149*16694521SOleksandr Tymoshenko 		reg &= 0x00ffffff;
150*16694521SOleksandr Tymoshenko 		reg |= 0x01000000;
151*16694521SOleksandr Tymoshenko 		write_cpu_clkdiv(CPU_DIVCLK_CTRL2_RATIO_FULL0, reg);
152*16694521SOleksandr Tymoshenko 	}
153*16694521SOleksandr Tymoshenko 	if (mp_ncpus > 2) {
154*16694521SOleksandr Tymoshenko 		reg = read_cpu_clkdiv(CPU_DIVCLK_CTRL2_RATIO_FULL1);
155*16694521SOleksandr Tymoshenko 		reg &= 0xff00ffff;
156*16694521SOleksandr Tymoshenko 		reg |= 0x00010000;
157*16694521SOleksandr Tymoshenko 		write_cpu_clkdiv(CPU_DIVCLK_CTRL2_RATIO_FULL1, reg);
158*16694521SOleksandr Tymoshenko 	}
159*16694521SOleksandr Tymoshenko 	if (mp_ncpus > 3) {
160*16694521SOleksandr Tymoshenko 		reg = read_cpu_clkdiv(CPU_DIVCLK_CTRL2_RATIO_FULL1);
161*16694521SOleksandr Tymoshenko 		reg &= 0x00ffffff;
162*16694521SOleksandr Tymoshenko 		reg |= 0x01000000;
163*16694521SOleksandr Tymoshenko 		write_cpu_clkdiv(CPU_DIVCLK_CTRL2_RATIO_FULL1, reg);
164*16694521SOleksandr Tymoshenko 	}
165*16694521SOleksandr Tymoshenko 
166*16694521SOleksandr Tymoshenko 	reg = read_cpu_clkdiv(CPU_DIVCLK_CTRL0);
167*16694521SOleksandr Tymoshenko 	reg |= ((0x1 << (mp_ncpus - 1)) - 1) << 21;
168*16694521SOleksandr Tymoshenko 	write_cpu_clkdiv(CPU_DIVCLK_CTRL0, reg);
169*16694521SOleksandr Tymoshenko 	reg = read_cpu_clkdiv(CPU_DIVCLK_CTRL0);
170*16694521SOleksandr Tymoshenko 	reg |= 0x01000000;
171*16694521SOleksandr Tymoshenko 	write_cpu_clkdiv(CPU_DIVCLK_CTRL0, reg);
172*16694521SOleksandr Tymoshenko 
173*16694521SOleksandr Tymoshenko 	DELAY(100);
174*16694521SOleksandr Tymoshenko 	reg &= ~(0xf << 21);
175*16694521SOleksandr Tymoshenko 	write_cpu_clkdiv(CPU_DIVCLK_CTRL0, reg);
176*16694521SOleksandr Tymoshenko 	DELAY(100);
177*16694521SOleksandr Tymoshenko 
178*16694521SOleksandr Tymoshenko 	bus_space_write_4(fdtbus_bs_tag, MV_BASE, CPU_RESUME_CONTROL, 0);
179*16694521SOleksandr Tymoshenko 
180*16694521SOleksandr Tymoshenko 	for (cpu_num = 1; cpu_num < mp_ncpus; cpu_num++ )
181*16694521SOleksandr Tymoshenko 		bus_space_write_4(fdtbus_bs_tag, CPU_PMU(cpu_num), CPU_PMU_BOOT,
182*16694521SOleksandr Tymoshenko 		    pmap_kextract(mpentry));
183*16694521SOleksandr Tymoshenko 
184*16694521SOleksandr Tymoshenko 	cpu_idcache_wbinv_all();
185*16694521SOleksandr Tymoshenko 
186*16694521SOleksandr Tymoshenko 	for (cpu_num = 1; cpu_num < mp_ncpus; cpu_num++ )
187*16694521SOleksandr Tymoshenko 		bus_space_write_4(fdtbus_bs_tag, MP, MP_SW_RESET(cpu_num), 0);
188*16694521SOleksandr Tymoshenko 
189*16694521SOleksandr Tymoshenko 	/* XXX: Temporary workaround for hangup after releasing AP's */
190*16694521SOleksandr Tymoshenko 	wmb();
191*16694521SOleksandr Tymoshenko 	DELAY(10);
192*16694521SOleksandr Tymoshenko 
193*16694521SOleksandr Tymoshenko 	initialize_coherency_fabric();
194*16694521SOleksandr Tymoshenko }
195*16694521SOleksandr Tymoshenko 
196*16694521SOleksandr Tymoshenko static int
197*16694521SOleksandr Tymoshenko platform_get_ncpus(void)
198*16694521SOleksandr Tymoshenko {
199*16694521SOleksandr Tymoshenko 
200*16694521SOleksandr Tymoshenko 	return ((read_coher_fabric(COHER_FABRIC_CONF) & 0xf) + 1);
201*16694521SOleksandr Tymoshenko }
202*16694521SOleksandr Tymoshenko 
203*16694521SOleksandr Tymoshenko void
204*16694521SOleksandr Tymoshenko platform_ipi_send(cpuset_t cpus, u_int ipi)
205*16694521SOleksandr Tymoshenko {
206*16694521SOleksandr Tymoshenko 
207*16694521SOleksandr Tymoshenko 	pic_ipi_send(cpus, ipi);
208*16694521SOleksandr Tymoshenko }
209