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 */ 5928bb01e5SRafal Jaworowski extern uint32_t kernload; /* Kernel physical load address */ 6028bb01e5SRafal Jaworowski #endif 6128bb01e5SRafal Jaworowski 62e3d41006SMarcel Moolenaar extern uint32_t *bootinfo; 63e3d41006SMarcel Moolenaar 642b7b2d79SRafal Jaworowski static int cpu, maxcpu; 65b40ce02aSNathan Whitehorn 66b40ce02aSNathan Whitehorn static int bare_probe(platform_t); 67b40ce02aSNathan Whitehorn static void bare_mem_regions(platform_t, struct mem_region **phys, int *physsz, 68b40ce02aSNathan Whitehorn struct mem_region **avail, int *availsz); 69b40ce02aSNathan Whitehorn static u_long bare_timebase_freq(platform_t, struct cpuref *cpuref); 70b40ce02aSNathan Whitehorn static int bare_smp_first_cpu(platform_t, struct cpuref *cpuref); 71b40ce02aSNathan Whitehorn static int bare_smp_next_cpu(platform_t, struct cpuref *cpuref); 72b40ce02aSNathan Whitehorn static int bare_smp_get_bsp(platform_t, struct cpuref *cpuref); 73b40ce02aSNathan Whitehorn static int bare_smp_start_cpu(platform_t, struct pcpu *cpu); 74b40ce02aSNathan Whitehorn 75b2a237beSNathan Whitehorn static void e500_reset(platform_t); 76b2a237beSNathan Whitehorn 77b40ce02aSNathan Whitehorn static platform_method_t bare_methods[] = { 78b40ce02aSNathan Whitehorn PLATFORMMETHOD(platform_probe, bare_probe), 79b40ce02aSNathan Whitehorn PLATFORMMETHOD(platform_mem_regions, bare_mem_regions), 80b40ce02aSNathan Whitehorn PLATFORMMETHOD(platform_timebase_freq, bare_timebase_freq), 81b40ce02aSNathan Whitehorn 82b40ce02aSNathan Whitehorn PLATFORMMETHOD(platform_smp_first_cpu, bare_smp_first_cpu), 83b40ce02aSNathan Whitehorn PLATFORMMETHOD(platform_smp_next_cpu, bare_smp_next_cpu), 84b40ce02aSNathan Whitehorn PLATFORMMETHOD(platform_smp_get_bsp, bare_smp_get_bsp), 85b40ce02aSNathan Whitehorn PLATFORMMETHOD(platform_smp_start_cpu, bare_smp_start_cpu), 86b40ce02aSNathan Whitehorn 87707c2fb9SAlexander Motin PLATFORMMETHOD(platform_reset, e500_reset), 88b2a237beSNathan Whitehorn 89b40ce02aSNathan Whitehorn { 0, 0 } 90b40ce02aSNathan Whitehorn }; 91b40ce02aSNathan Whitehorn 92b40ce02aSNathan Whitehorn static platform_def_t bare_platform = { 93b40ce02aSNathan Whitehorn "bare metal", 94b40ce02aSNathan Whitehorn bare_methods, 95b40ce02aSNathan Whitehorn 0 96b40ce02aSNathan Whitehorn }; 97b40ce02aSNathan Whitehorn 98b40ce02aSNathan Whitehorn PLATFORM_DEF(bare_platform); 99b40ce02aSNathan Whitehorn 100b40ce02aSNathan Whitehorn static int 101b40ce02aSNathan Whitehorn bare_probe(platform_t plat) 102b40ce02aSNathan Whitehorn { 103d1d3233eSRafal Jaworowski uint32_t ver, sr; 104d1d3233eSRafal Jaworowski int i, law_max, tgt; 1052b7b2d79SRafal Jaworowski 1062b7b2d79SRafal Jaworowski ver = SVR_VER(mfspr(SPR_SVR)); 1072b7b2d79SRafal Jaworowski if (ver == SVR_MPC8572E || ver == SVR_MPC8572) 1082b7b2d79SRafal Jaworowski maxcpu = 2; 1092b7b2d79SRafal Jaworowski else 1102b7b2d79SRafal Jaworowski maxcpu = 1; 111b40ce02aSNathan Whitehorn 112d1d3233eSRafal Jaworowski /* 113d1d3233eSRafal Jaworowski * Clear local access windows. Skip DRAM entries, so we don't shoot 114d1d3233eSRafal Jaworowski * ourselves in the foot. 115d1d3233eSRafal Jaworowski */ 116d1d3233eSRafal Jaworowski law_max = law_getmax(); 117d1d3233eSRafal Jaworowski for (i = 0; i < law_max; i++) { 118d1d3233eSRafal Jaworowski sr = ccsr_read4(OCP85XX_LAWSR(i)); 119d1d3233eSRafal Jaworowski if ((sr & 0x80000000) == 0) 120d1d3233eSRafal Jaworowski continue; 121d1d3233eSRafal Jaworowski tgt = (sr & 0x01f00000) >> 20; 122d1d3233eSRafal Jaworowski if (tgt == OCP85XX_TGTIF_RAM1 || tgt == OCP85XX_TGTIF_RAM2 || 123d1d3233eSRafal Jaworowski tgt == OCP85XX_TGTIF_RAM_INTL) 124d1d3233eSRafal Jaworowski continue; 125d1d3233eSRafal Jaworowski 126d1d3233eSRafal Jaworowski ccsr_write4(OCP85XX_LAWSR(i), sr & 0x7fffffff); 127d1d3233eSRafal Jaworowski } 128d1d3233eSRafal Jaworowski 129b40ce02aSNathan Whitehorn return (BUS_PROBE_GENERIC); 130b40ce02aSNathan Whitehorn } 131b40ce02aSNathan Whitehorn 132b40ce02aSNathan Whitehorn #define MEM_REGIONS 8 133b40ce02aSNathan Whitehorn static struct mem_region avail_regions[MEM_REGIONS]; 134b40ce02aSNathan Whitehorn 135b40ce02aSNathan Whitehorn void 136b40ce02aSNathan Whitehorn bare_mem_regions(platform_t plat, struct mem_region **phys, int *physsz, 137b40ce02aSNathan Whitehorn struct mem_region **avail, int *availsz) 138b40ce02aSNathan Whitehorn { 139d1d3233eSRafal Jaworowski uint32_t memsize; 140d1d3233eSRafal Jaworowski int i, rv; 141b40ce02aSNathan Whitehorn 142d1d3233eSRafal Jaworowski rv = fdt_get_mem_regions(avail_regions, availsz, &memsize); 143d1d3233eSRafal Jaworowski 144d1d3233eSRafal Jaworowski if (rv != 0) 145d1d3233eSRafal Jaworowski return; 146d1d3233eSRafal Jaworowski 147d1d3233eSRafal Jaworowski for (i = 0; i < *availsz; i++) { 148d1d3233eSRafal Jaworowski if (avail_regions[i].mr_start < 1048576) { 149d1d3233eSRafal Jaworowski avail_regions[i].mr_size = 150d1d3233eSRafal Jaworowski avail_regions[i].mr_size - 151d1d3233eSRafal Jaworowski (1048576 - avail_regions[i].mr_start); 152b40ce02aSNathan Whitehorn avail_regions[i].mr_start = 1048576; 153b40ce02aSNathan Whitehorn } 154b40ce02aSNathan Whitehorn } 155b40ce02aSNathan Whitehorn *avail = avail_regions; 156b40ce02aSNathan Whitehorn 157b40ce02aSNathan Whitehorn /* On the bare metal platform phys == avail memory */ 158b40ce02aSNathan Whitehorn *physsz = *availsz; 159b40ce02aSNathan Whitehorn *phys = *avail; 160b40ce02aSNathan Whitehorn } 161b40ce02aSNathan Whitehorn 162b40ce02aSNathan Whitehorn static u_long 163b40ce02aSNathan Whitehorn bare_timebase_freq(platform_t plat, struct cpuref *cpuref) 164b40ce02aSNathan Whitehorn { 165e3d41006SMarcel Moolenaar u_long ticks; 166d1d3233eSRafal Jaworowski phandle_t cpus, child; 167d1d3233eSRafal Jaworowski pcell_t freq; 168b40ce02aSNathan Whitehorn 169*7512c508SMarcel Moolenaar if (bootinfo != NULL) { 170e3d41006SMarcel Moolenaar /* Backward compatibility. See 8-STABLE. */ 171e3d41006SMarcel Moolenaar ticks = bootinfo[3] >> 3; 172*7512c508SMarcel Moolenaar } else 173*7512c508SMarcel Moolenaar ticks = 0; 174e3d41006SMarcel Moolenaar 175d1d3233eSRafal Jaworowski if ((cpus = OF_finddevice("/cpus")) == 0) 176d1d3233eSRafal Jaworowski goto out; 177d1d3233eSRafal Jaworowski 178d1d3233eSRafal Jaworowski if ((child = OF_child(cpus)) == 0) 179d1d3233eSRafal Jaworowski goto out; 180d1d3233eSRafal Jaworowski 181e3d41006SMarcel Moolenaar freq = 0; 182d1d3233eSRafal Jaworowski if (OF_getprop(child, "bus-frequency", (void *)&freq, 183d1d3233eSRafal Jaworowski sizeof(freq)) <= 0) 184d1d3233eSRafal Jaworowski goto out; 185e3d41006SMarcel Moolenaar 186b40ce02aSNathan Whitehorn /* 187b40ce02aSNathan Whitehorn * Time Base and Decrementer are updated every 8 CCB bus clocks. 188b40ce02aSNathan Whitehorn * HID0[SEL_TBCLK] = 0 189b40ce02aSNathan Whitehorn */ 190e3d41006SMarcel Moolenaar if (freq != 0) 191d1d3233eSRafal Jaworowski ticks = freq / 8; 192e3d41006SMarcel Moolenaar 193d1d3233eSRafal Jaworowski out: 194b40ce02aSNathan Whitehorn if (ticks <= 0) 195b40ce02aSNathan Whitehorn panic("Unable to determine timebase frequency!"); 196b40ce02aSNathan Whitehorn 197b40ce02aSNathan Whitehorn return (ticks); 198b40ce02aSNathan Whitehorn } 199b40ce02aSNathan Whitehorn 200b40ce02aSNathan Whitehorn static int 201b40ce02aSNathan Whitehorn bare_smp_first_cpu(platform_t plat, struct cpuref *cpuref) 202b40ce02aSNathan Whitehorn { 203b40ce02aSNathan Whitehorn 204b40ce02aSNathan Whitehorn cpu = 0; 205b40ce02aSNathan Whitehorn cpuref->cr_cpuid = cpu; 206b40ce02aSNathan Whitehorn cpuref->cr_hwref = cpuref->cr_cpuid; 207b40ce02aSNathan Whitehorn if (bootverbose) 208b40ce02aSNathan Whitehorn printf("powerpc_smp_first_cpu: cpuid %d\n", cpuref->cr_cpuid); 209b40ce02aSNathan Whitehorn cpu++; 210b40ce02aSNathan Whitehorn 211b40ce02aSNathan Whitehorn return (0); 212b40ce02aSNathan Whitehorn } 213b40ce02aSNathan Whitehorn 214b40ce02aSNathan Whitehorn static int 215b40ce02aSNathan Whitehorn bare_smp_next_cpu(platform_t plat, struct cpuref *cpuref) 216b40ce02aSNathan Whitehorn { 217b40ce02aSNathan Whitehorn 2182b7b2d79SRafal Jaworowski if (cpu >= maxcpu) 219b40ce02aSNathan Whitehorn return (ENOENT); 220b40ce02aSNathan Whitehorn 221b40ce02aSNathan Whitehorn cpuref->cr_cpuid = cpu++; 222b40ce02aSNathan Whitehorn cpuref->cr_hwref = cpuref->cr_cpuid; 223b40ce02aSNathan Whitehorn if (bootverbose) 224b40ce02aSNathan Whitehorn printf("powerpc_smp_next_cpu: cpuid %d\n", cpuref->cr_cpuid); 225b40ce02aSNathan Whitehorn 226b40ce02aSNathan Whitehorn return (0); 227b40ce02aSNathan Whitehorn } 228b40ce02aSNathan Whitehorn 229b40ce02aSNathan Whitehorn static int 230b40ce02aSNathan Whitehorn bare_smp_get_bsp(platform_t plat, struct cpuref *cpuref) 231b40ce02aSNathan Whitehorn { 232b40ce02aSNathan Whitehorn 233b40ce02aSNathan Whitehorn cpuref->cr_cpuid = mfspr(SPR_PIR); 234b40ce02aSNathan Whitehorn cpuref->cr_hwref = cpuref->cr_cpuid; 235b40ce02aSNathan Whitehorn 236b40ce02aSNathan Whitehorn return (0); 237b40ce02aSNathan Whitehorn } 238b40ce02aSNathan Whitehorn 239b40ce02aSNathan Whitehorn static int 240b40ce02aSNathan Whitehorn bare_smp_start_cpu(platform_t plat, struct pcpu *pc) 241b40ce02aSNathan Whitehorn { 24228bb01e5SRafal Jaworowski #ifdef SMP 24328bb01e5SRafal Jaworowski uint32_t bptr, eebpcr; 24428bb01e5SRafal Jaworowski int timeout; 245b40ce02aSNathan Whitehorn 24628bb01e5SRafal Jaworowski eebpcr = ccsr_read4(OCP85XX_EEBPCR); 247c98b3586SAttilio Rao if ((eebpcr & (pc->pc_cpumask << 24)) != 0) { 24828bb01e5SRafal Jaworowski printf("%s: CPU=%d already out of hold-off state!\n", 24928bb01e5SRafal Jaworowski __func__, pc->pc_cpuid); 25028bb01e5SRafal Jaworowski return (ENXIO); 25128bb01e5SRafal Jaworowski } 25228bb01e5SRafal Jaworowski 25328bb01e5SRafal Jaworowski ap_pcpu = pc; 25428bb01e5SRafal Jaworowski __asm __volatile("msync; isync"); 25528bb01e5SRafal Jaworowski 25628bb01e5SRafal Jaworowski /* 25728bb01e5SRafal Jaworowski * Set BPTR to the physical address of the boot page 25828bb01e5SRafal Jaworowski */ 25928bb01e5SRafal Jaworowski bptr = ((uint32_t)__boot_page - KERNBASE) + kernload; 26028bb01e5SRafal Jaworowski ccsr_write4(OCP85XX_BPTR, (bptr >> 12) | 0x80000000); 26128bb01e5SRafal Jaworowski 26228bb01e5SRafal Jaworowski /* 26328bb01e5SRafal Jaworowski * Release AP from hold-off state 26428bb01e5SRafal Jaworowski */ 265c98b3586SAttilio Rao eebpcr |= (pc->pc_cpumask << 24); 26628bb01e5SRafal Jaworowski ccsr_write4(OCP85XX_EEBPCR, eebpcr); 26728bb01e5SRafal Jaworowski __asm __volatile("isync; msync"); 26828bb01e5SRafal Jaworowski 26928bb01e5SRafal Jaworowski timeout = 500; 27028bb01e5SRafal Jaworowski while (!pc->pc_awake && timeout--) 27128bb01e5SRafal Jaworowski DELAY(1000); /* wait 1ms */ 27228bb01e5SRafal Jaworowski 27328bb01e5SRafal Jaworowski return ((pc->pc_awake) ? 0 : EBUSY); 27428bb01e5SRafal Jaworowski #else 275b40ce02aSNathan Whitehorn /* No SMP support */ 276b40ce02aSNathan Whitehorn return (ENXIO); 27728bb01e5SRafal Jaworowski #endif 278b40ce02aSNathan Whitehorn } 279b2a237beSNathan Whitehorn 280b2a237beSNathan Whitehorn static void 281b2a237beSNathan Whitehorn e500_reset(platform_t plat) 282b2a237beSNathan Whitehorn { 283b2a237beSNathan Whitehorn uint32_t ver = SVR_VER(mfspr(SPR_SVR)); 284b2a237beSNathan Whitehorn 285b2a237beSNathan Whitehorn if (ver == SVR_MPC8572E || ver == SVR_MPC8572 || 286b2a237beSNathan Whitehorn ver == SVR_MPC8548E || ver == SVR_MPC8548) 287b2a237beSNathan Whitehorn /* Systems with dedicated reset register */ 288b2a237beSNathan Whitehorn ccsr_write4(OCP85XX_RSTCR, 2); 289b2a237beSNathan Whitehorn else { 290b2a237beSNathan Whitehorn /* Clear DBCR0, disables debug interrupts and events. */ 291b2a237beSNathan Whitehorn mtspr(SPR_DBCR0, 0); 292b2a237beSNathan Whitehorn __asm __volatile("isync"); 293b2a237beSNathan Whitehorn 294b2a237beSNathan Whitehorn /* Enable Debug Interrupts in MSR. */ 295b2a237beSNathan Whitehorn mtmsr(mfmsr() | PSL_DE); 296b2a237beSNathan Whitehorn 297b2a237beSNathan Whitehorn /* Enable debug interrupts and issue reset. */ 298b2a237beSNathan Whitehorn mtspr(SPR_DBCR0, mfspr(SPR_DBCR0) | DBCR0_IDM | 299b2a237beSNathan Whitehorn DBCR0_RST_SYSTEM); 300b2a237beSNathan Whitehorn } 301b2a237beSNathan Whitehorn 302b2a237beSNathan Whitehorn printf("Reset failed...\n"); 303b2a237beSNathan Whitehorn while (1); 304b2a237beSNathan Whitehorn } 305b2a237beSNathan Whitehorn 306