2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2008-2012 Semihalf.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "opt_platform.h"
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
41 #include <machine/bus.h>
42 #include <machine/cpu.h>
43 #include <machine/hid.h>
44 #include <machine/_inttypes.h>
45 #include <machine/machdep.h>
46 #include <machine/md_var.h>
47 #include <machine/platform.h>
48 #include <machine/platformvar.h>
49 #include <machine/smp.h>
50 #include <machine/spr.h>
51 #include <machine/vmparam.h>
53 #include <dev/fdt/fdt_common.h>
54 #include <dev/ofw/ofw_bus.h>
55 #include <dev/ofw/ofw_bus_subr.h>
56 #include <dev/ofw/openfirm.h>
60 #include <vm/vm_extern.h>
62 #include <powerpc/mpc85xx/mpc85xx.h>
64 #include "platform_if.h"
68 extern vm_paddr_t kernload; /* Kernel physical load address */
69 extern uint8_t __boot_page[]; /* Boot page body */
70 extern uint32_t bp_kernload;
82 extern uint32_t *bootinfo;
83 vm_paddr_t ccsrbar_pa;
84 vm_offset_t ccsrbar_va;
85 vm_size_t ccsrbar_size;
87 static int cpu, maxcpu;
89 static int mpc85xx_probe(platform_t);
90 static void mpc85xx_mem_regions(platform_t, struct mem_region *phys,
91 int *physsz, struct mem_region *avail, int *availsz);
92 static u_long mpc85xx_timebase_freq(platform_t, struct cpuref *cpuref);
93 static int mpc85xx_smp_first_cpu(platform_t, struct cpuref *cpuref);
94 static int mpc85xx_smp_next_cpu(platform_t, struct cpuref *cpuref);
95 static int mpc85xx_smp_get_bsp(platform_t, struct cpuref *cpuref);
96 static int mpc85xx_smp_start_cpu(platform_t, struct pcpu *cpu);
97 static void mpc85xx_smp_timebase_sync(platform_t, u_long tb, int ap);
98 static void mpc85xx_idle(platform_t, int cpu);
99 static int mpc85xx_idle_wakeup(platform_t plat, int cpu);
101 static void mpc85xx_reset(platform_t);
103 static platform_method_t mpc85xx_methods[] = {
104 PLATFORMMETHOD(platform_probe, mpc85xx_probe),
105 PLATFORMMETHOD(platform_attach, mpc85xx_attach),
106 PLATFORMMETHOD(platform_mem_regions, mpc85xx_mem_regions),
107 PLATFORMMETHOD(platform_timebase_freq, mpc85xx_timebase_freq),
109 PLATFORMMETHOD(platform_smp_first_cpu, mpc85xx_smp_first_cpu),
110 PLATFORMMETHOD(platform_smp_next_cpu, mpc85xx_smp_next_cpu),
111 PLATFORMMETHOD(platform_smp_get_bsp, mpc85xx_smp_get_bsp),
112 PLATFORMMETHOD(platform_smp_start_cpu, mpc85xx_smp_start_cpu),
113 PLATFORMMETHOD(platform_smp_timebase_sync, mpc85xx_smp_timebase_sync),
115 PLATFORMMETHOD(platform_reset, mpc85xx_reset),
116 PLATFORMMETHOD(platform_idle, mpc85xx_idle),
117 PLATFORMMETHOD(platform_idle_wakeup, mpc85xx_idle_wakeup),
122 DEFINE_CLASS_0(mpc85xx, mpc85xx_platform, mpc85xx_methods, 0);
124 PLATFORM_DEF(mpc85xx_platform);
127 mpc85xx_probe(platform_t plat)
129 u_int pvr = (mfpvr() >> 16) & 0xFFFF;
137 return (BUS_PROBE_DEFAULT);
143 mpc85xx_attach(platform_t plat)
145 phandle_t cpus, child, ccsr;
146 const char *soc_name_guesses[] = {"/soc", "soc", NULL};
148 pcell_t ranges[6], acells, pacells, scells;
149 uint64_t ccsrbar, ccsrsize;
152 if ((cpus = OF_finddevice("/cpus")) != -1) {
153 for (maxcpu = 0, child = OF_child(cpus); child != 0;
154 child = OF_peer(child), maxcpu++)
160 * Locate CCSR region. Irritatingly, there is no way to find it
161 * unless you already know where it is. Try to infer its location
162 * from the device tree.
166 for (name = soc_name_guesses; *name != NULL && ccsr == -1; name++)
167 ccsr = OF_finddevice(*name);
171 /* That didn't work. Search for devices of type "soc" */
172 child = OF_child(OF_peer(0));
173 for (OF_child(child); child != 0; child = OF_peer(child)) {
174 if (OF_getprop(child, "device_type", type, sizeof(type))
178 if (strcmp(type, "soc") == 0) {
186 panic("Could not locate CCSR window!");
188 OF_getprop(ccsr, "#size-cells", &scells, sizeof(scells));
189 OF_getprop(ccsr, "#address-cells", &acells, sizeof(acells));
190 OF_searchprop(OF_parent(ccsr), "#address-cells", &pacells,
192 OF_getprop(ccsr, "ranges", ranges, sizeof(ranges));
193 ccsrbar = ccsrsize = 0;
194 for (i = acells; i < acells + pacells; i++) {
196 ccsrbar |= ranges[i];
198 for (i = acells + pacells; i < acells + pacells + scells; i++) {
200 ccsrsize |= ranges[i];
202 ccsrbar_va = pmap_early_io_map(ccsrbar, ccsrsize);
203 ccsrbar_pa = ccsrbar;
204 ccsrbar_size = ccsrsize;
207 mpc85xx_fix_errata(ccsrbar_va);
209 mpc85xx_enable_l3_cache();
215 mpc85xx_mem_regions(platform_t plat, struct mem_region *phys, int *physsz,
216 struct mem_region *avail, int *availsz)
219 ofw_mem_regions(phys, physsz, avail, availsz);
223 mpc85xx_timebase_freq(platform_t plat, struct cpuref *cpuref)
226 phandle_t cpus, child;
229 if (bootinfo != NULL) {
230 if (bootinfo[0] == 1) {
231 /* Backward compatibility. See 8-STABLE. */
232 ticks = bootinfo[3] >> 3;
234 /* Compatibility with Juniper's loader. */
235 ticks = bootinfo[5] >> 3;
240 if ((cpus = OF_finddevice("/cpus")) == -1)
243 if ((child = OF_child(cpus)) == 0)
246 switch (OF_getproplen(child, "timebase-frequency")) {
250 OF_getprop(child, "timebase-frequency", &tbase, sizeof(tbase));
257 OF_getprop(child, "timebase-frequency", &tbase, sizeof(tbase));
266 if (OF_getprop(child, "bus-frequency", (void *)&freq,
274 * Time Base and Decrementer are updated every 8 CCB bus clocks.
275 * HID0[SEL_TBCLK] = 0
277 if (mpc85xx_is_qoriq())
284 panic("Unable to determine timebase frequency!");
290 mpc85xx_smp_first_cpu(platform_t plat, struct cpuref *cpuref)
294 cpuref->cr_cpuid = cpu;
295 cpuref->cr_hwref = cpuref->cr_cpuid;
297 printf("powerpc_smp_first_cpu: cpuid %d\n", cpuref->cr_cpuid);
304 mpc85xx_smp_next_cpu(platform_t plat, struct cpuref *cpuref)
310 cpuref->cr_cpuid = cpu++;
311 cpuref->cr_hwref = cpuref->cr_cpuid;
313 printf("powerpc_smp_next_cpu: cpuid %d\n", cpuref->cr_cpuid);
319 mpc85xx_smp_get_bsp(platform_t plat, struct cpuref *cpuref)
322 cpuref->cr_cpuid = mfspr(SPR_PIR);
323 cpuref->cr_hwref = cpuref->cr_cpuid;
330 mpc85xx_smp_start_cpu_epapr(platform_t plat, struct pcpu *pc)
332 vm_paddr_t rel_pa, bptr;
333 volatile struct cpu_release *rel;
334 vm_offset_t rel_va, rel_page;
338 /* If we're calling this, the node already exists. */
339 node = OF_finddevice("/cpus");
340 for (i = 0, node = OF_child(node); i < pc->pc_cpuid;
341 i++, node = OF_peer(node))
343 if (OF_getencprop(node, "cpu-release-addr", (pcell_t *)&rel_pa,
344 sizeof(rel_pa)) == -1) {
348 rel_page = kva_alloc(PAGE_SIZE);
353 rel_va = rel_page + (rel_pa & PAGE_MASK);
354 pmap_kenter(rel_page, rel_pa & ~PAGE_MASK);
355 rel = (struct cpu_release *)rel_va;
356 bptr = ((vm_paddr_t)(uintptr_t)__boot_page - KERNBASE) + kernload;
357 cpu_flush_dcache(__DEVOLATILE(struct cpu_release *,rel), sizeof(*rel));
358 rel->pir = pc->pc_cpuid; __asm __volatile("sync");
359 rel->entry_h = (bptr >> 32);
360 rel->entry_l = bptr; __asm __volatile("sync");
361 cpu_flush_dcache(__DEVOLATILE(struct cpu_release *,rel), sizeof(*rel));
363 printf("Waking up CPU %d via CPU release page %p\n",
366 pmap_kremove(rel_page);
367 kva_free(rel_page, PAGE_SIZE);
374 mpc85xx_smp_start_cpu(platform_t plat, struct pcpu *pc)
385 if (mpc85xx_is_qoriq()) {
386 reg = ccsr_read4(OCP85XX_COREDISR);
387 cpuid = pc->pc_cpuid;
389 if ((reg & (1 << cpuid)) != 0) {
390 printf("%s: CPU %d is disabled!\n", __func__, pc->pc_cpuid);
396 brr = OCP85XX_EEBPCR;
397 cpuid = pc->pc_cpuid + 24;
399 bp_kernload = kernload;
401 * bp_kernload is in the boot page. Sync the cache because ePAPR
402 * booting has the other core(s) already running.
404 cpu_flush_dcache(&bp_kernload, sizeof(bp_kernload));
407 __asm __volatile("msync; isync");
409 /* First try the ePAPR way. */
410 if (mpc85xx_smp_start_cpu_epapr(plat, pc) == 0) {
415 reg = ccsr_read4(brr);
416 if ((reg & (1 << cpuid)) != 0) {
417 printf("SMP: CPU %d already out of hold-off state!\n",
422 /* Flush caches to have our changes hit DRAM. */
423 cpu_flush_dcache(__boot_page, 4096);
425 bptr = ((vm_paddr_t)(uintptr_t)__boot_page - KERNBASE) + kernload;
426 KASSERT((bptr & 0xfff) == 0,
427 ("%s: boot page is not aligned (%#jx)", __func__, (uintmax_t)bptr));
428 if (mpc85xx_is_qoriq()) {
430 * Read DDR controller configuration to select proper BPTR target ID.
432 * On P5020 bit 29 of DDR1_CS0_CONFIG enables DDR controllers
433 * interleaving. If this bit is set, we have to use
434 * OCP85XX_TGTIF_RAM_INTL as BPTR target ID. On other QorIQ DPAA SoCs,
435 * this bit is reserved and always 0.
438 reg = ccsr_read4(OCP85XX_DDR1_CS0_CONFIG);
440 tgt = OCP85XX_TGTIF_RAM_INTL;
442 tgt = OCP85XX_TGTIF_RAM1;
445 * Set BSTR to the physical address of the boot page
447 ccsr_write4(OCP85XX_BSTRH, bptr >> 32);
448 ccsr_write4(OCP85XX_BSTRL, bptr);
449 ccsr_write4(OCP85XX_BSTAR, OCP85XX_ENA_MASK |
450 (tgt << OCP85XX_TRGT_SHIFT_QORIQ) | (ffsl(PAGE_SIZE) - 2));
452 /* Read back OCP85XX_BSTAR to synchronize write */
453 ccsr_read4(OCP85XX_BSTAR);
456 * Enable and configure time base on new CPU.
459 /* Set TB clock source to platform clock / 32 */
460 reg = ccsr_read4(CCSR_CTBCKSELR);
461 ccsr_write4(CCSR_CTBCKSELR, reg & ~(1 << pc->pc_cpuid));
464 reg = ccsr_read4(CCSR_CTBENR);
465 ccsr_write4(CCSR_CTBENR, reg | (1 << pc->pc_cpuid));
468 * Set BPTR to the physical address of the boot page
470 bptr = (bptr >> 12) | 0x80000000u;
471 ccsr_write4(OCP85XX_BPTR, bptr);
472 __asm __volatile("isync; msync");
476 * Release AP from hold-off state
478 reg = ccsr_read4(brr);
479 ccsr_write4(brr, reg | (1 << cpuid));
480 __asm __volatile("isync; msync");
484 while (!pc->pc_awake && timeout--)
485 DELAY(1000); /* wait 1ms */
488 * Disable boot page translation so that the 4K page at the default
489 * address (= 0xfffff000) isn't permanently remapped and thus not
493 if (mpc85xx_is_qoriq())
494 ccsr_write4(OCP85XX_BSTAR, 0);
496 ccsr_write4(OCP85XX_BPTR, 0);
497 __asm __volatile("isync; msync");
501 panic("SMP: CPU %d didn't wake up.\n", pc->pc_cpuid);
502 return ((pc->pc_awake) ? 0 : EBUSY);
510 mpc85xx_reset(platform_t plat)
514 * Try the dedicated reset register first.
515 * If the SoC doesn't have one, we'll fall
516 * back to using the debug control register.
518 ccsr_write4(OCP85XX_RSTCR, 2);
520 /* Clear DBCR0, disables debug interrupts and events. */
522 __asm __volatile("isync");
524 /* Enable Debug Interrupts in MSR. */
525 mtmsr(mfmsr() | PSL_DE);
527 /* Enable debug interrupts and issue reset. */
528 mtspr(SPR_DBCR0, mfspr(SPR_DBCR0) | DBCR0_IDM | DBCR0_RST_SYSTEM);
530 printf("Reset failed...\n");
536 mpc85xx_smp_timebase_sync(platform_t plat, u_long tb, int ap)
543 mpc85xx_idle(platform_t plat, int cpu)
547 if (mpc85xx_is_qoriq()) {
549 * Base binutils doesn't know what the 'wait' instruction is, so
550 * use the opcode encoding here.
552 __asm __volatile("wrteei 1; .long 0x7c00007c");
555 /* Freescale E500 core RM section 6.4.1. */
556 __asm __volatile("msync; mtmsr %0; isync" ::
562 mpc85xx_idle_wakeup(platform_t plat, int cpu)