1b40ce02aSNathan Whitehorn /*- 2b40ce02aSNathan Whitehorn * Copyright (c) 2008-2009 Semihalf, Rafal Jaworowski 3b40ce02aSNathan Whitehorn * All rights reserved. 4b40ce02aSNathan Whitehorn * 5b40ce02aSNathan Whitehorn * Redistribution and use in source and binary forms, with or without 6b40ce02aSNathan Whitehorn * modification, are permitted provided that the following conditions 7b40ce02aSNathan Whitehorn * are met: 8b40ce02aSNathan Whitehorn * 9b40ce02aSNathan Whitehorn * 1. Redistributions of source code must retain the above copyright 10b40ce02aSNathan Whitehorn * notice, this list of conditions and the following disclaimer. 11b40ce02aSNathan Whitehorn * 2. Redistributions in binary form must reproduce the above copyright 12b40ce02aSNathan Whitehorn * notice, this list of conditions and the following disclaimer in the 13b40ce02aSNathan Whitehorn * documentation and/or other materials provided with the distribution. 14b40ce02aSNathan Whitehorn * 15b40ce02aSNathan Whitehorn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16b40ce02aSNathan Whitehorn * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17b40ce02aSNathan Whitehorn * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18b40ce02aSNathan Whitehorn * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19b40ce02aSNathan Whitehorn * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20b40ce02aSNathan Whitehorn * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21b40ce02aSNathan Whitehorn * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22b40ce02aSNathan Whitehorn * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23b40ce02aSNathan Whitehorn * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24b40ce02aSNathan Whitehorn * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25b40ce02aSNathan Whitehorn */ 26b40ce02aSNathan Whitehorn 27b40ce02aSNathan Whitehorn #include <sys/cdefs.h> 28b40ce02aSNathan Whitehorn __FBSDID("$FreeBSD$"); 29b40ce02aSNathan Whitehorn 30b40ce02aSNathan Whitehorn #include <sys/param.h> 31b40ce02aSNathan Whitehorn #include <sys/systm.h> 32b40ce02aSNathan Whitehorn #include <sys/kernel.h> 33b40ce02aSNathan Whitehorn #include <sys/bus.h> 34b40ce02aSNathan Whitehorn #include <sys/pcpu.h> 35b40ce02aSNathan Whitehorn #include <sys/proc.h> 36b40ce02aSNathan Whitehorn #include <sys/smp.h> 37b40ce02aSNathan Whitehorn 38b40ce02aSNathan Whitehorn #include <machine/bus.h> 39b40ce02aSNathan Whitehorn #include <machine/cpu.h> 40b40ce02aSNathan Whitehorn #include <machine/hid.h> 41b40ce02aSNathan Whitehorn #include <machine/platform.h> 42b40ce02aSNathan Whitehorn #include <machine/platformvar.h> 43b40ce02aSNathan Whitehorn #include <machine/smp.h> 44b40ce02aSNathan Whitehorn #include <machine/spr.h> 45b40ce02aSNathan Whitehorn #include <machine/vmparam.h> 46b40ce02aSNathan Whitehorn 47d1d3233eSRafal Jaworowski #include <dev/fdt/fdt_common.h> 48d1d3233eSRafal Jaworowski #include <dev/ofw/ofw_bus.h> 49d1d3233eSRafal Jaworowski #include <dev/ofw/ofw_bus_subr.h> 50d1d3233eSRafal Jaworowski #include <dev/ofw/openfirm.h> 51d1d3233eSRafal Jaworowski 52b40ce02aSNathan Whitehorn #include <powerpc/mpc85xx/mpc85xx.h> 53b40ce02aSNathan Whitehorn 54b40ce02aSNathan Whitehorn #include "platform_if.h" 55b40ce02aSNathan Whitehorn 5628bb01e5SRafal Jaworowski #ifdef SMP 5728bb01e5SRafal Jaworowski extern void *ap_pcpu; 5828bb01e5SRafal Jaworowski extern uint8_t __boot_page[]; /* Boot page body */ 59a45d9127SMarcel Moolenaar extern uint32_t bp_kernload; /* Kernel physical load address */ 60a45d9127SMarcel Moolenaar extern uint32_t bp_trace; /* AP boot trace field */ 6128bb01e5SRafal Jaworowski #endif 6228bb01e5SRafal Jaworowski 63e3d41006SMarcel Moolenaar extern uint32_t *bootinfo; 64e3d41006SMarcel Moolenaar 652b7b2d79SRafal Jaworowski static int cpu, maxcpu; 66b40ce02aSNathan Whitehorn 67b40ce02aSNathan Whitehorn static int bare_probe(platform_t); 68b40ce02aSNathan Whitehorn static void bare_mem_regions(platform_t, struct mem_region **phys, int *physsz, 69b40ce02aSNathan Whitehorn struct mem_region **avail, int *availsz); 70b40ce02aSNathan Whitehorn static u_long bare_timebase_freq(platform_t, struct cpuref *cpuref); 71b40ce02aSNathan Whitehorn static int bare_smp_first_cpu(platform_t, struct cpuref *cpuref); 72b40ce02aSNathan Whitehorn static int bare_smp_next_cpu(platform_t, struct cpuref *cpuref); 73b40ce02aSNathan Whitehorn static int bare_smp_get_bsp(platform_t, struct cpuref *cpuref); 74b40ce02aSNathan Whitehorn static int bare_smp_start_cpu(platform_t, struct pcpu *cpu); 75b40ce02aSNathan Whitehorn 76*2f6bd241SRafal Jaworowski static void booke_reset(platform_t); 77b2a237beSNathan Whitehorn 78b40ce02aSNathan Whitehorn static platform_method_t bare_methods[] = { 79b40ce02aSNathan Whitehorn PLATFORMMETHOD(platform_probe, bare_probe), 80b40ce02aSNathan Whitehorn PLATFORMMETHOD(platform_mem_regions, bare_mem_regions), 81b40ce02aSNathan Whitehorn PLATFORMMETHOD(platform_timebase_freq, bare_timebase_freq), 82b40ce02aSNathan Whitehorn 83b40ce02aSNathan Whitehorn PLATFORMMETHOD(platform_smp_first_cpu, bare_smp_first_cpu), 84b40ce02aSNathan Whitehorn PLATFORMMETHOD(platform_smp_next_cpu, bare_smp_next_cpu), 85b40ce02aSNathan Whitehorn PLATFORMMETHOD(platform_smp_get_bsp, bare_smp_get_bsp), 86b40ce02aSNathan Whitehorn PLATFORMMETHOD(platform_smp_start_cpu, bare_smp_start_cpu), 87b40ce02aSNathan Whitehorn 88*2f6bd241SRafal Jaworowski PLATFORMMETHOD(platform_reset, booke_reset), 89b2a237beSNathan Whitehorn 90b40ce02aSNathan Whitehorn { 0, 0 } 91b40ce02aSNathan Whitehorn }; 92b40ce02aSNathan Whitehorn 93b40ce02aSNathan Whitehorn static platform_def_t bare_platform = { 94b40ce02aSNathan Whitehorn "bare metal", 95b40ce02aSNathan Whitehorn bare_methods, 96b40ce02aSNathan Whitehorn 0 97b40ce02aSNathan Whitehorn }; 98b40ce02aSNathan Whitehorn 99b40ce02aSNathan Whitehorn PLATFORM_DEF(bare_platform); 100b40ce02aSNathan Whitehorn 101b40ce02aSNathan Whitehorn static int 102b40ce02aSNathan Whitehorn bare_probe(platform_t plat) 103b40ce02aSNathan Whitehorn { 104d1d3233eSRafal Jaworowski uint32_t ver, sr; 105d1d3233eSRafal Jaworowski int i, law_max, tgt; 1062b7b2d79SRafal Jaworowski 1072b7b2d79SRafal Jaworowski ver = SVR_VER(mfspr(SPR_SVR)); 108c7df91afSAttilio Rao switch (ver & ~0x0008) { /* Mask Security Enabled bit */ 109c7df91afSAttilio Rao case SVR_P4080: 110c7df91afSAttilio Rao maxcpu = 8; 111c7df91afSAttilio Rao break; 112c7df91afSAttilio Rao case SVR_P4040: 113c7df91afSAttilio Rao maxcpu = 4; 114c7df91afSAttilio Rao break; 115c7df91afSAttilio Rao case SVR_MPC8572: 116c7df91afSAttilio Rao case SVR_P1020: 117c7df91afSAttilio Rao case SVR_P2020: 1182b7b2d79SRafal Jaworowski maxcpu = 2; 119c7df91afSAttilio Rao break; 120c7df91afSAttilio Rao default: 1212b7b2d79SRafal Jaworowski maxcpu = 1; 122c7df91afSAttilio Rao break; 123c7df91afSAttilio Rao } 124b40ce02aSNathan Whitehorn 125d1d3233eSRafal Jaworowski /* 126d1d3233eSRafal Jaworowski * Clear local access windows. Skip DRAM entries, so we don't shoot 127d1d3233eSRafal Jaworowski * ourselves in the foot. 128d1d3233eSRafal Jaworowski */ 129d1d3233eSRafal Jaworowski law_max = law_getmax(); 130d1d3233eSRafal Jaworowski for (i = 0; i < law_max; i++) { 131d1d3233eSRafal Jaworowski sr = ccsr_read4(OCP85XX_LAWSR(i)); 132d1d3233eSRafal Jaworowski if ((sr & 0x80000000) == 0) 133d1d3233eSRafal Jaworowski continue; 134d1d3233eSRafal Jaworowski tgt = (sr & 0x01f00000) >> 20; 135d1d3233eSRafal Jaworowski if (tgt == OCP85XX_TGTIF_RAM1 || tgt == OCP85XX_TGTIF_RAM2 || 136d1d3233eSRafal Jaworowski tgt == OCP85XX_TGTIF_RAM_INTL) 137d1d3233eSRafal Jaworowski continue; 138d1d3233eSRafal Jaworowski 139d1d3233eSRafal Jaworowski ccsr_write4(OCP85XX_LAWSR(i), sr & 0x7fffffff); 140d1d3233eSRafal Jaworowski } 141d1d3233eSRafal Jaworowski 142b40ce02aSNathan Whitehorn return (BUS_PROBE_GENERIC); 143b40ce02aSNathan Whitehorn } 144b40ce02aSNathan Whitehorn 145b40ce02aSNathan Whitehorn #define MEM_REGIONS 8 146b40ce02aSNathan Whitehorn static struct mem_region avail_regions[MEM_REGIONS]; 147b40ce02aSNathan Whitehorn 148b40ce02aSNathan Whitehorn void 149b40ce02aSNathan Whitehorn bare_mem_regions(platform_t plat, struct mem_region **phys, int *physsz, 150b40ce02aSNathan Whitehorn struct mem_region **avail, int *availsz) 151b40ce02aSNathan Whitehorn { 152d1d3233eSRafal Jaworowski uint32_t memsize; 153d1d3233eSRafal Jaworowski int i, rv; 154b40ce02aSNathan Whitehorn 155d1d3233eSRafal Jaworowski rv = fdt_get_mem_regions(avail_regions, availsz, &memsize); 156d1d3233eSRafal Jaworowski 157d1d3233eSRafal Jaworowski if (rv != 0) 158d1d3233eSRafal Jaworowski return; 159d1d3233eSRafal Jaworowski 160d1d3233eSRafal Jaworowski for (i = 0; i < *availsz; i++) { 161d1d3233eSRafal Jaworowski if (avail_regions[i].mr_start < 1048576) { 162d1d3233eSRafal Jaworowski avail_regions[i].mr_size = 163d1d3233eSRafal Jaworowski avail_regions[i].mr_size - 164d1d3233eSRafal Jaworowski (1048576 - avail_regions[i].mr_start); 165b40ce02aSNathan Whitehorn avail_regions[i].mr_start = 1048576; 166b40ce02aSNathan Whitehorn } 167b40ce02aSNathan Whitehorn } 168b40ce02aSNathan Whitehorn *avail = avail_regions; 169b40ce02aSNathan Whitehorn 170b40ce02aSNathan Whitehorn /* On the bare metal platform phys == avail memory */ 171b40ce02aSNathan Whitehorn *physsz = *availsz; 172b40ce02aSNathan Whitehorn *phys = *avail; 173b40ce02aSNathan Whitehorn } 174b40ce02aSNathan Whitehorn 175b40ce02aSNathan Whitehorn static u_long 176b40ce02aSNathan Whitehorn bare_timebase_freq(platform_t plat, struct cpuref *cpuref) 177b40ce02aSNathan Whitehorn { 178e3d41006SMarcel Moolenaar u_long ticks; 179d1d3233eSRafal Jaworowski phandle_t cpus, child; 180d1d3233eSRafal Jaworowski pcell_t freq; 181b40ce02aSNathan Whitehorn 1825ce36fdbSMarcel Moolenaar if (bootinfo != NULL) { 1832b5bf115SMarcel Moolenaar if (bootinfo[0] == 1) { 184e3d41006SMarcel Moolenaar /* Backward compatibility. See 8-STABLE. */ 185e3d41006SMarcel Moolenaar ticks = bootinfo[3] >> 3; 1862b5bf115SMarcel Moolenaar } else { 1875ce36fdbSMarcel Moolenaar /* Compatibility with Juniper's loader. */ 1882b5bf115SMarcel Moolenaar ticks = bootinfo[5] >> 3; 1895ce36fdbSMarcel Moolenaar } 1907512c508SMarcel Moolenaar } else 1917512c508SMarcel Moolenaar ticks = 0; 192e3d41006SMarcel Moolenaar 19307042befSJayachandran C. if ((cpus = OF_finddevice("/cpus")) == -1) 194d1d3233eSRafal Jaworowski goto out; 195d1d3233eSRafal Jaworowski 196d1d3233eSRafal Jaworowski if ((child = OF_child(cpus)) == 0) 197d1d3233eSRafal Jaworowski goto out; 198d1d3233eSRafal Jaworowski 199e3d41006SMarcel Moolenaar freq = 0; 200d1d3233eSRafal Jaworowski if (OF_getprop(child, "bus-frequency", (void *)&freq, 201d1d3233eSRafal Jaworowski sizeof(freq)) <= 0) 202d1d3233eSRafal Jaworowski goto out; 203e3d41006SMarcel Moolenaar 204b40ce02aSNathan Whitehorn /* 205b40ce02aSNathan Whitehorn * Time Base and Decrementer are updated every 8 CCB bus clocks. 206b40ce02aSNathan Whitehorn * HID0[SEL_TBCLK] = 0 207b40ce02aSNathan Whitehorn */ 208e3d41006SMarcel Moolenaar if (freq != 0) 209d1d3233eSRafal Jaworowski ticks = freq / 8; 210e3d41006SMarcel Moolenaar 211d1d3233eSRafal Jaworowski out: 212b40ce02aSNathan Whitehorn if (ticks <= 0) 213b40ce02aSNathan Whitehorn panic("Unable to determine timebase frequency!"); 214b40ce02aSNathan Whitehorn 215b40ce02aSNathan Whitehorn return (ticks); 216b40ce02aSNathan Whitehorn } 217b40ce02aSNathan Whitehorn 218b40ce02aSNathan Whitehorn static int 219b40ce02aSNathan Whitehorn bare_smp_first_cpu(platform_t plat, struct cpuref *cpuref) 220b40ce02aSNathan Whitehorn { 221b40ce02aSNathan Whitehorn 222b40ce02aSNathan Whitehorn cpu = 0; 223b40ce02aSNathan Whitehorn cpuref->cr_cpuid = cpu; 224b40ce02aSNathan Whitehorn cpuref->cr_hwref = cpuref->cr_cpuid; 225b40ce02aSNathan Whitehorn if (bootverbose) 226b40ce02aSNathan Whitehorn printf("powerpc_smp_first_cpu: cpuid %d\n", cpuref->cr_cpuid); 227b40ce02aSNathan Whitehorn cpu++; 228b40ce02aSNathan Whitehorn 229b40ce02aSNathan Whitehorn return (0); 230b40ce02aSNathan Whitehorn } 231b40ce02aSNathan Whitehorn 232b40ce02aSNathan Whitehorn static int 233b40ce02aSNathan Whitehorn bare_smp_next_cpu(platform_t plat, struct cpuref *cpuref) 234b40ce02aSNathan Whitehorn { 235b40ce02aSNathan Whitehorn 2362b7b2d79SRafal Jaworowski if (cpu >= maxcpu) 237b40ce02aSNathan Whitehorn return (ENOENT); 238b40ce02aSNathan Whitehorn 239b40ce02aSNathan Whitehorn cpuref->cr_cpuid = cpu++; 240b40ce02aSNathan Whitehorn cpuref->cr_hwref = cpuref->cr_cpuid; 241b40ce02aSNathan Whitehorn if (bootverbose) 242b40ce02aSNathan Whitehorn printf("powerpc_smp_next_cpu: cpuid %d\n", cpuref->cr_cpuid); 243b40ce02aSNathan Whitehorn 244b40ce02aSNathan Whitehorn return (0); 245b40ce02aSNathan Whitehorn } 246b40ce02aSNathan Whitehorn 247b40ce02aSNathan Whitehorn static int 248b40ce02aSNathan Whitehorn bare_smp_get_bsp(platform_t plat, struct cpuref *cpuref) 249b40ce02aSNathan Whitehorn { 250b40ce02aSNathan Whitehorn 251b40ce02aSNathan Whitehorn cpuref->cr_cpuid = mfspr(SPR_PIR); 252b40ce02aSNathan Whitehorn cpuref->cr_hwref = cpuref->cr_cpuid; 253b40ce02aSNathan Whitehorn 254b40ce02aSNathan Whitehorn return (0); 255b40ce02aSNathan Whitehorn } 256b40ce02aSNathan Whitehorn 257b40ce02aSNathan Whitehorn static int 258b40ce02aSNathan Whitehorn bare_smp_start_cpu(platform_t plat, struct pcpu *pc) 259b40ce02aSNathan Whitehorn { 26028bb01e5SRafal Jaworowski #ifdef SMP 26128bb01e5SRafal Jaworowski uint32_t bptr, eebpcr; 26228bb01e5SRafal Jaworowski int timeout; 263b40ce02aSNathan Whitehorn 26428bb01e5SRafal Jaworowski eebpcr = ccsr_read4(OCP85XX_EEBPCR); 26520bf92c2SAttilio Rao if ((eebpcr & (1 << (pc->pc_cpuid + 24))) != 0) { 266a45d9127SMarcel Moolenaar printf("SMP: CPU %d already out of hold-off state!\n", 267a45d9127SMarcel Moolenaar pc->pc_cpuid); 26828bb01e5SRafal Jaworowski return (ENXIO); 26928bb01e5SRafal Jaworowski } 27028bb01e5SRafal Jaworowski 27128bb01e5SRafal Jaworowski ap_pcpu = pc; 27228bb01e5SRafal Jaworowski __asm __volatile("msync; isync"); 27328bb01e5SRafal Jaworowski 27428bb01e5SRafal Jaworowski /* 27528bb01e5SRafal Jaworowski * Set BPTR to the physical address of the boot page 27628bb01e5SRafal Jaworowski */ 277a45d9127SMarcel Moolenaar bptr = ((uint32_t)__boot_page - KERNBASE) + bp_kernload; 27828bb01e5SRafal Jaworowski ccsr_write4(OCP85XX_BPTR, (bptr >> 12) | 0x80000000); 27928bb01e5SRafal Jaworowski 28028bb01e5SRafal Jaworowski /* 28128bb01e5SRafal Jaworowski * Release AP from hold-off state 28228bb01e5SRafal Jaworowski */ 283a45d9127SMarcel Moolenaar bp_trace = 0; 28420bf92c2SAttilio Rao eebpcr |= (1 << (pc->pc_cpuid + 24)); 28528bb01e5SRafal Jaworowski ccsr_write4(OCP85XX_EEBPCR, eebpcr); 28628bb01e5SRafal Jaworowski __asm __volatile("isync; msync"); 28728bb01e5SRafal Jaworowski 28828bb01e5SRafal Jaworowski timeout = 500; 28928bb01e5SRafal Jaworowski while (!pc->pc_awake && timeout--) 29028bb01e5SRafal Jaworowski DELAY(1000); /* wait 1ms */ 29128bb01e5SRafal Jaworowski 292a45d9127SMarcel Moolenaar /* 293a45d9127SMarcel Moolenaar * Disable boot page translation so that the 4K page at the default 294a45d9127SMarcel Moolenaar * address (= 0xfffff000) isn't permanently remapped and thus not 295a45d9127SMarcel Moolenaar * usable otherwise. 296a45d9127SMarcel Moolenaar */ 297a45d9127SMarcel Moolenaar ccsr_write4(OCP85XX_BPTR, 0); 298a45d9127SMarcel Moolenaar 299a45d9127SMarcel Moolenaar if (!pc->pc_awake) 300a45d9127SMarcel Moolenaar printf("SMP: CPU %d didn't wake up (trace code %#x).\n", 301a45d9127SMarcel Moolenaar pc->pc_awake, bp_trace); 30228bb01e5SRafal Jaworowski return ((pc->pc_awake) ? 0 : EBUSY); 30328bb01e5SRafal Jaworowski #else 304b40ce02aSNathan Whitehorn /* No SMP support */ 305b40ce02aSNathan Whitehorn return (ENXIO); 30628bb01e5SRafal Jaworowski #endif 307b40ce02aSNathan Whitehorn } 308b2a237beSNathan Whitehorn 309b2a237beSNathan Whitehorn static void 310*2f6bd241SRafal Jaworowski booke_reset(platform_t plat) 311b2a237beSNathan Whitehorn { 312b2a237beSNathan Whitehorn 3137faf44baSMarcel Moolenaar /* 3147faf44baSMarcel Moolenaar * Try the dedicated reset register first. 3157faf44baSMarcel Moolenaar * If the SoC doesn't have one, we'll fall 3167faf44baSMarcel Moolenaar * back to using the debug control register. 3177faf44baSMarcel Moolenaar */ 318b2a237beSNathan Whitehorn ccsr_write4(OCP85XX_RSTCR, 2); 3197faf44baSMarcel Moolenaar 320b2a237beSNathan Whitehorn /* Clear DBCR0, disables debug interrupts and events. */ 321b2a237beSNathan Whitehorn mtspr(SPR_DBCR0, 0); 322b2a237beSNathan Whitehorn __asm __volatile("isync"); 323b2a237beSNathan Whitehorn 324b2a237beSNathan Whitehorn /* Enable Debug Interrupts in MSR. */ 325b2a237beSNathan Whitehorn mtmsr(mfmsr() | PSL_DE); 326b2a237beSNathan Whitehorn 327b2a237beSNathan Whitehorn /* Enable debug interrupts and issue reset. */ 3287faf44baSMarcel Moolenaar mtspr(SPR_DBCR0, mfspr(SPR_DBCR0) | DBCR0_IDM | DBCR0_RST_SYSTEM); 329b2a237beSNathan Whitehorn 330b2a237beSNathan Whitehorn printf("Reset failed...\n"); 331*2f6bd241SRafal Jaworowski while (1) 332*2f6bd241SRafal Jaworowski ; 333b2a237beSNathan Whitehorn } 334b2a237beSNathan Whitehorn 335