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;
71 extern vm_offset_t __startkernel;
83 extern uint32_t *bootinfo;
84 vm_paddr_t ccsrbar_pa;
85 vm_offset_t ccsrbar_va;
86 vm_size_t ccsrbar_size;
88 static int cpu, maxcpu;
90 static int mpc85xx_probe(platform_t);
91 static void mpc85xx_mem_regions(platform_t, struct mem_region *phys,
92 int *physsz, struct mem_region *avail, int *availsz);
93 static u_long mpc85xx_timebase_freq(platform_t, struct cpuref *cpuref);
94 static int mpc85xx_smp_first_cpu(platform_t, struct cpuref *cpuref);
95 static int mpc85xx_smp_next_cpu(platform_t, struct cpuref *cpuref);
96 static int mpc85xx_smp_get_bsp(platform_t, struct cpuref *cpuref);
97 static int mpc85xx_smp_start_cpu(platform_t, struct pcpu *cpu);
98 static void mpc85xx_smp_timebase_sync(platform_t, u_long tb, int ap);
100 static void mpc85xx_reset(platform_t);
102 static platform_method_t mpc85xx_methods[] = {
103 PLATFORMMETHOD(platform_probe, mpc85xx_probe),
104 PLATFORMMETHOD(platform_attach, mpc85xx_attach),
105 PLATFORMMETHOD(platform_mem_regions, mpc85xx_mem_regions),
106 PLATFORMMETHOD(platform_timebase_freq, mpc85xx_timebase_freq),
108 PLATFORMMETHOD(platform_smp_first_cpu, mpc85xx_smp_first_cpu),
109 PLATFORMMETHOD(platform_smp_next_cpu, mpc85xx_smp_next_cpu),
110 PLATFORMMETHOD(platform_smp_get_bsp, mpc85xx_smp_get_bsp),
111 PLATFORMMETHOD(platform_smp_start_cpu, mpc85xx_smp_start_cpu),
112 PLATFORMMETHOD(platform_smp_timebase_sync, mpc85xx_smp_timebase_sync),
114 PLATFORMMETHOD(platform_reset, mpc85xx_reset),
119 DEFINE_CLASS_0(mpc85xx, mpc85xx_platform, mpc85xx_methods, 0);
121 PLATFORM_DEF(mpc85xx_platform);
124 mpc85xx_probe(platform_t plat)
126 u_int pvr = (mfpvr() >> 16) & 0xFFFF;
134 return (BUS_PROBE_DEFAULT);
140 mpc85xx_attach(platform_t plat)
142 phandle_t cpus, child, ccsr;
143 const char *soc_name_guesses[] = {"/soc", "soc", NULL};
145 pcell_t ranges[6], acells, pacells, scells;
146 uint64_t ccsrbar, ccsrsize;
149 if ((cpus = OF_finddevice("/cpus")) != -1) {
150 for (maxcpu = 0, child = OF_child(cpus); child != 0;
151 child = OF_peer(child), maxcpu++)
157 * Locate CCSR region. Irritatingly, there is no way to find it
158 * unless you already know where it is. Try to infer its location
159 * from the device tree.
163 for (name = soc_name_guesses; *name != NULL && ccsr == -1; name++)
164 ccsr = OF_finddevice(*name);
168 /* That didn't work. Search for devices of type "soc" */
169 child = OF_child(OF_peer(0));
170 for (OF_child(child); child != 0; child = OF_peer(child)) {
171 if (OF_getprop(child, "device_type", type, sizeof(type))
175 if (strcmp(type, "soc") == 0) {
183 panic("Could not locate CCSR window!");
185 OF_getprop(ccsr, "#size-cells", &scells, sizeof(scells));
186 OF_getprop(ccsr, "#address-cells", &acells, sizeof(acells));
187 OF_searchprop(OF_parent(ccsr), "#address-cells", &pacells,
189 OF_getprop(ccsr, "ranges", ranges, sizeof(ranges));
190 ccsrbar = ccsrsize = 0;
191 for (i = acells; i < acells + pacells; i++) {
193 ccsrbar |= ranges[i];
195 for (i = acells + pacells; i < acells + pacells + scells; i++) {
197 ccsrsize |= ranges[i];
199 ccsrbar_va = pmap_early_io_map(ccsrbar, ccsrsize);
200 ccsrbar_pa = ccsrbar;
201 ccsrbar_size = ccsrsize;
203 mpc85xx_enable_l3_cache();
209 mpc85xx_mem_regions(platform_t plat, struct mem_region *phys, int *physsz,
210 struct mem_region *avail, int *availsz)
213 ofw_mem_regions(phys, physsz, avail, availsz);
217 mpc85xx_timebase_freq(platform_t plat, struct cpuref *cpuref)
220 phandle_t cpus, child;
223 if (bootinfo != NULL) {
224 if (bootinfo[0] == 1) {
225 /* Backward compatibility. See 8-STABLE. */
226 ticks = bootinfo[3] >> 3;
228 /* Compatibility with Juniper's loader. */
229 ticks = bootinfo[5] >> 3;
234 if ((cpus = OF_finddevice("/cpus")) == -1)
237 if ((child = OF_child(cpus)) == 0)
240 switch (OF_getproplen(child, "timebase-frequency")) {
244 OF_getprop(child, "timebase-frequency", &tbase, sizeof(tbase));
251 OF_getprop(child, "timebase-frequency", &tbase, sizeof(tbase));
260 if (OF_getprop(child, "bus-frequency", (void *)&freq,
268 * Time Base and Decrementer are updated every 8 CCB bus clocks.
269 * HID0[SEL_TBCLK] = 0
271 if (mpc85xx_is_qoriq())
278 panic("Unable to determine timebase frequency!");
284 mpc85xx_smp_first_cpu(platform_t plat, struct cpuref *cpuref)
288 cpuref->cr_cpuid = cpu;
289 cpuref->cr_hwref = cpuref->cr_cpuid;
291 printf("powerpc_smp_first_cpu: cpuid %d\n", cpuref->cr_cpuid);
298 mpc85xx_smp_next_cpu(platform_t plat, struct cpuref *cpuref)
304 cpuref->cr_cpuid = cpu++;
305 cpuref->cr_hwref = cpuref->cr_cpuid;
307 printf("powerpc_smp_next_cpu: cpuid %d\n", cpuref->cr_cpuid);
313 mpc85xx_smp_get_bsp(platform_t plat, struct cpuref *cpuref)
316 cpuref->cr_cpuid = mfspr(SPR_PIR);
317 cpuref->cr_hwref = cpuref->cr_cpuid;
324 mpc85xx_smp_start_cpu_epapr(platform_t plat, struct pcpu *pc)
326 vm_paddr_t rel_pa, bptr;
327 volatile struct cpu_release *rel;
328 vm_offset_t rel_va, rel_page;
332 /* If we're calling this, the node already exists. */
333 node = OF_finddevice("/cpus");
334 for (i = 0, node = OF_child(node); i < pc->pc_cpuid;
335 i++, node = OF_peer(node))
337 if (OF_getencprop(node, "cpu-release-addr", (pcell_t *)&rel_pa,
338 sizeof(rel_pa)) == -1) {
342 rel_page = kva_alloc(PAGE_SIZE);
347 rel_va = rel_page + (rel_pa & PAGE_MASK);
348 pmap_kenter(rel_page, rel_pa & ~PAGE_MASK);
349 rel = (struct cpu_release *)rel_va;
350 bptr = pmap_kextract((uintptr_t)__boot_page);
351 cpu_flush_dcache(__DEVOLATILE(struct cpu_release *,rel), sizeof(*rel));
352 rel->pir = pc->pc_cpuid; __asm __volatile("sync");
353 rel->entry_h = (bptr >> 32);
354 rel->entry_l = bptr; __asm __volatile("sync");
355 cpu_flush_dcache(__DEVOLATILE(struct cpu_release *,rel), sizeof(*rel));
357 printf("Waking up CPU %d via CPU release page %p\n",
360 pmap_kremove(rel_page);
361 kva_free(rel_page, PAGE_SIZE);
368 mpc85xx_smp_start_cpu(platform_t plat, struct pcpu *pc)
379 if (mpc85xx_is_qoriq()) {
380 reg = ccsr_read4(OCP85XX_COREDISR);
381 cpuid = pc->pc_cpuid;
383 if ((reg & (1 << cpuid)) != 0) {
384 printf("%s: CPU %d is disabled!\n", __func__, pc->pc_cpuid);
390 brr = OCP85XX_EEBPCR;
391 cpuid = pc->pc_cpuid + 24;
393 bp_kernload = kernload;
395 * bp_kernload is in the boot page. Sync the cache because ePAPR
396 * booting has the other core(s) already running.
398 cpu_flush_dcache(&bp_kernload, sizeof(bp_kernload));
401 __asm __volatile("msync; isync");
403 /* First try the ePAPR way. */
404 if (mpc85xx_smp_start_cpu_epapr(plat, pc) == 0) {
409 reg = ccsr_read4(brr);
410 if ((reg & (1 << cpuid)) != 0) {
411 printf("SMP: CPU %d already out of hold-off state!\n",
416 /* Flush caches to have our changes hit DRAM. */
417 cpu_flush_dcache(__boot_page, 4096);
419 bptr = pmap_kextract((uintptr_t)__boot_page);
420 KASSERT((bptr & 0xfff) == 0,
421 ("%s: boot page is not aligned (%#jx)", __func__, (uintmax_t)bptr));
422 if (mpc85xx_is_qoriq()) {
424 * Read DDR controller configuration to select proper BPTR target ID.
426 * On P5020 bit 29 of DDR1_CS0_CONFIG enables DDR controllers
427 * interleaving. If this bit is set, we have to use
428 * OCP85XX_TGTIF_RAM_INTL as BPTR target ID. On other QorIQ DPAA SoCs,
429 * this bit is reserved and always 0.
432 reg = ccsr_read4(OCP85XX_DDR1_CS0_CONFIG);
434 tgt = OCP85XX_TGTIF_RAM_INTL;
436 tgt = OCP85XX_TGTIF_RAM1;
439 * Set BSTR to the physical address of the boot page
441 ccsr_write4(OCP85XX_BSTRH, bptr >> 32);
442 ccsr_write4(OCP85XX_BSTRL, bptr);
443 ccsr_write4(OCP85XX_BSTAR, OCP85XX_ENA_MASK |
444 (tgt << OCP85XX_TRGT_SHIFT_QORIQ) | (ffsl(PAGE_SIZE) - 2));
446 /* Read back OCP85XX_BSTAR to synchronize write */
447 ccsr_read4(OCP85XX_BSTAR);
450 * Enable and configure time base on new CPU.
453 /* Set TB clock source to platform clock / 32 */
454 reg = ccsr_read4(CCSR_CTBCKSELR);
455 ccsr_write4(CCSR_CTBCKSELR, reg & ~(1 << pc->pc_cpuid));
458 reg = ccsr_read4(CCSR_CTBENR);
459 ccsr_write4(CCSR_CTBENR, reg | (1 << pc->pc_cpuid));
462 * Set BPTR to the physical address of the boot page
464 bptr = (bptr >> 12) | 0x80000000u;
465 ccsr_write4(OCP85XX_BPTR, bptr);
466 __asm __volatile("isync; msync");
470 * Release AP from hold-off state
472 reg = ccsr_read4(brr);
473 ccsr_write4(brr, reg | (1 << cpuid));
474 __asm __volatile("isync; msync");
478 while (!pc->pc_awake && timeout--)
479 DELAY(1000); /* wait 1ms */
482 * Disable boot page translation so that the 4K page at the default
483 * address (= 0xfffff000) isn't permanently remapped and thus not
487 if (mpc85xx_is_qoriq())
488 ccsr_write4(OCP85XX_BSTAR, 0);
490 ccsr_write4(OCP85XX_BPTR, 0);
491 __asm __volatile("isync; msync");
495 panic("SMP: CPU %d didn't wake up.\n", pc->pc_cpuid);
496 return ((pc->pc_awake) ? 0 : EBUSY);
504 mpc85xx_reset(platform_t plat)
508 * Try the dedicated reset register first.
509 * If the SoC doesn't have one, we'll fall
510 * back to using the debug control register.
512 ccsr_write4(OCP85XX_RSTCR, 2);
514 /* Clear DBCR0, disables debug interrupts and events. */
516 __asm __volatile("isync");
518 /* Enable Debug Interrupts in MSR. */
519 mtmsr(mfmsr() | PSL_DE);
521 /* Enable debug interrupts and issue reset. */
522 mtspr(SPR_DBCR0, mfspr(SPR_DBCR0) | DBCR0_IDM | DBCR0_RST_SYSTEM);
524 printf("Reset failed...\n");
530 mpc85xx_smp_timebase_sync(platform_t plat, u_long tb, int ap)