1eed0eabdSPaul Burton /* 2eed0eabdSPaul Burton * Copyright (C) 2016 Imagination Technologies 348c834beSPaul Burton * Author: Paul Burton <paul.burton@mips.com> 4eed0eabdSPaul Burton * 5eed0eabdSPaul Burton * This program is free software; you can redistribute it and/or modify it 6eed0eabdSPaul Burton * under the terms of the GNU General Public License as published by the 7eed0eabdSPaul Burton * Free Software Foundation; either version 2 of the License, or (at your 8eed0eabdSPaul Burton * option) any later version. 9eed0eabdSPaul Burton */ 10eed0eabdSPaul Burton 11eed0eabdSPaul Burton #include <linux/clk.h> 12eed0eabdSPaul Burton #include <linux/clk-provider.h> 13eed0eabdSPaul Burton #include <linux/clocksource.h> 14eed0eabdSPaul Burton #include <linux/init.h> 15eed0eabdSPaul Burton #include <linux/irqchip.h> 16eed0eabdSPaul Burton #include <linux/of_fdt.h> 17eed0eabdSPaul Burton #include <linux/of_platform.h> 18eed0eabdSPaul Burton 19b47e9c62SPaul Burton #include <asm/bootinfo.h> 20eed0eabdSPaul Burton #include <asm/fw/fw.h> 21eed0eabdSPaul Burton #include <asm/irq_cpu.h> 22eed0eabdSPaul Burton #include <asm/machine.h> 23eed0eabdSPaul Burton #include <asm/mips-cpc.h> 24eed0eabdSPaul Burton #include <asm/prom.h> 25eed0eabdSPaul Burton #include <asm/smp-ops.h> 26eed0eabdSPaul Burton #include <asm/time.h> 27eed0eabdSPaul Burton 28eed0eabdSPaul Burton static __initdata const void *fdt; 29eed0eabdSPaul Burton static __initdata const struct mips_machine *mach; 30eed0eabdSPaul Burton static __initdata const void *mach_match_data; 31eed0eabdSPaul Burton 32eed0eabdSPaul Burton void __init prom_init(void) 33eed0eabdSPaul Burton { 349a59061cSMatt Redfearn plat_get_fdt(); 359a59061cSMatt Redfearn BUG_ON(!fdt); 369a59061cSMatt Redfearn } 379a59061cSMatt Redfearn 389a59061cSMatt Redfearn void __init *plat_get_fdt(void) 399a59061cSMatt Redfearn { 40eed0eabdSPaul Burton const struct mips_machine *check_mach; 41eed0eabdSPaul Burton const struct of_device_id *match; 42eed0eabdSPaul Burton 439a59061cSMatt Redfearn if (fdt) 449a59061cSMatt Redfearn /* Already set up */ 459a59061cSMatt Redfearn return (void *)fdt; 469a59061cSMatt Redfearn 47eed0eabdSPaul Burton if ((fw_arg0 == -2) && !fdt_check_header((void *)fw_arg1)) { 48eed0eabdSPaul Burton /* 49eed0eabdSPaul Burton * We booted using the UHI boot protocol, so we have been 50eed0eabdSPaul Burton * provided with the appropriate device tree for the board. 51eed0eabdSPaul Burton * Make use of it & search for any machine struct based upon 52eed0eabdSPaul Burton * the root compatible string. 53eed0eabdSPaul Burton */ 54eed0eabdSPaul Burton fdt = (void *)fw_arg1; 55eed0eabdSPaul Burton 56eed0eabdSPaul Burton for_each_mips_machine(check_mach) { 57eed0eabdSPaul Burton match = mips_machine_is_compatible(check_mach, fdt); 58eed0eabdSPaul Burton if (match) { 59eed0eabdSPaul Burton mach = check_mach; 60eed0eabdSPaul Burton mach_match_data = match->data; 61eed0eabdSPaul Burton break; 62eed0eabdSPaul Burton } 63eed0eabdSPaul Burton } 64eed0eabdSPaul Burton } else if (IS_ENABLED(CONFIG_LEGACY_BOARDS)) { 65eed0eabdSPaul Burton /* 66eed0eabdSPaul Burton * We weren't booted using the UHI boot protocol, but do 67eed0eabdSPaul Burton * support some number of boards with legacy boot protocols. 68eed0eabdSPaul Burton * Attempt to find the right one. 69eed0eabdSPaul Burton */ 70eed0eabdSPaul Burton for_each_mips_machine(check_mach) { 71eed0eabdSPaul Burton if (!check_mach->detect) 72eed0eabdSPaul Burton continue; 73eed0eabdSPaul Burton 74eed0eabdSPaul Burton if (!check_mach->detect()) 75eed0eabdSPaul Burton continue; 76eed0eabdSPaul Burton 77eed0eabdSPaul Burton mach = check_mach; 78eed0eabdSPaul Burton } 79eed0eabdSPaul Burton 80eed0eabdSPaul Burton /* 81eed0eabdSPaul Burton * If we don't recognise the machine then we can't continue, so 82eed0eabdSPaul Burton * die here. 83eed0eabdSPaul Burton */ 84eed0eabdSPaul Burton BUG_ON(!mach); 85eed0eabdSPaul Burton 86eed0eabdSPaul Burton /* Retrieve the machine's FDT */ 87eed0eabdSPaul Burton fdt = mach->fdt; 88eed0eabdSPaul Burton } 89eed0eabdSPaul Burton return (void *)fdt; 90eed0eabdSPaul Burton } 91eed0eabdSPaul Burton 92b47e9c62SPaul Burton #ifdef CONFIG_RELOCATABLE 93b47e9c62SPaul Burton 940063fdedSMarcin Nowakowski void __init plat_fdt_relocated(void *new_location) 950063fdedSMarcin Nowakowski { 960063fdedSMarcin Nowakowski /* 970063fdedSMarcin Nowakowski * reset fdt as the cached value would point to the location 980063fdedSMarcin Nowakowski * before relocations happened and update the location argument 990063fdedSMarcin Nowakowski * if it was passed using UHI 1000063fdedSMarcin Nowakowski */ 1010063fdedSMarcin Nowakowski fdt = NULL; 1020063fdedSMarcin Nowakowski 1030063fdedSMarcin Nowakowski if (fw_arg0 == -2) 1040063fdedSMarcin Nowakowski fw_arg1 = (unsigned long)new_location; 1050063fdedSMarcin Nowakowski } 1060063fdedSMarcin Nowakowski 107b47e9c62SPaul Burton #endif /* CONFIG_RELOCATABLE */ 108b47e9c62SPaul Burton 109eed0eabdSPaul Burton void __init plat_mem_setup(void) 110eed0eabdSPaul Burton { 111eed0eabdSPaul Burton if (mach && mach->fixup_fdt) 112eed0eabdSPaul Burton fdt = mach->fixup_fdt(fdt, mach_match_data); 113eed0eabdSPaul Burton 114eed0eabdSPaul Burton strlcpy(arcs_cmdline, boot_command_line, COMMAND_LINE_SIZE); 115eed0eabdSPaul Burton __dt_setup_arch((void *)fdt); 116eed0eabdSPaul Burton } 117eed0eabdSPaul Burton 118eed0eabdSPaul Burton void __init device_tree_init(void) 119eed0eabdSPaul Burton { 120eed0eabdSPaul Burton int err; 121eed0eabdSPaul Burton 122eed0eabdSPaul Burton unflatten_and_copy_device_tree(); 123eed0eabdSPaul Burton mips_cpc_probe(); 124eed0eabdSPaul Burton 125eed0eabdSPaul Burton err = register_cps_smp_ops(); 126eed0eabdSPaul Burton if (err) 127eed0eabdSPaul Burton err = register_up_smp_ops(); 128eed0eabdSPaul Burton } 129eed0eabdSPaul Burton 130e889dfcaSPaul Burton int __init apply_mips_fdt_fixups(void *fdt_out, size_t fdt_out_size, 131e889dfcaSPaul Burton const void *fdt_in, 132e889dfcaSPaul Burton const struct mips_fdt_fixup *fixups) 133e889dfcaSPaul Burton { 134e889dfcaSPaul Burton int err; 135e889dfcaSPaul Burton 136e889dfcaSPaul Burton err = fdt_open_into(fdt_in, fdt_out, fdt_out_size); 137e889dfcaSPaul Burton if (err) { 138e889dfcaSPaul Burton pr_err("Failed to open FDT\n"); 139e889dfcaSPaul Burton return err; 140e889dfcaSPaul Burton } 141e889dfcaSPaul Burton 142e889dfcaSPaul Burton for (; fixups->apply; fixups++) { 143e889dfcaSPaul Burton err = fixups->apply(fdt_out); 144e889dfcaSPaul Burton if (err) { 145e889dfcaSPaul Burton pr_err("Failed to apply FDT fixup \"%s\"\n", 146e889dfcaSPaul Burton fixups->description); 147e889dfcaSPaul Burton return err; 148e889dfcaSPaul Burton } 149e889dfcaSPaul Burton } 150e889dfcaSPaul Burton 151e889dfcaSPaul Burton err = fdt_pack(fdt_out); 152e889dfcaSPaul Burton if (err) 153e889dfcaSPaul Burton pr_err("Failed to pack FDT\n"); 154e889dfcaSPaul Burton return err; 155e889dfcaSPaul Burton } 156e889dfcaSPaul Burton 157eed0eabdSPaul Burton void __init plat_time_init(void) 158eed0eabdSPaul Burton { 159eed0eabdSPaul Burton struct device_node *np; 160eed0eabdSPaul Burton struct clk *clk; 161eed0eabdSPaul Burton 162eed0eabdSPaul Burton of_clk_init(NULL); 163eed0eabdSPaul Burton 164eed0eabdSPaul Burton if (!cpu_has_counter) { 165eed0eabdSPaul Burton mips_hpt_frequency = 0; 166eed0eabdSPaul Burton } else if (mach && mach->measure_hpt_freq) { 167eed0eabdSPaul Burton mips_hpt_frequency = mach->measure_hpt_freq(); 168eed0eabdSPaul Burton } else { 169eed0eabdSPaul Burton np = of_get_cpu_node(0, NULL); 170eed0eabdSPaul Burton if (!np) { 171eed0eabdSPaul Burton pr_err("Failed to get CPU node\n"); 172eed0eabdSPaul Burton return; 173eed0eabdSPaul Burton } 174eed0eabdSPaul Burton 175eed0eabdSPaul Burton clk = of_clk_get(np, 0); 176eed0eabdSPaul Burton if (IS_ERR(clk)) { 177eed0eabdSPaul Burton pr_err("Failed to get CPU clock: %ld\n", PTR_ERR(clk)); 178eed0eabdSPaul Burton return; 179eed0eabdSPaul Burton } 180eed0eabdSPaul Burton 181eed0eabdSPaul Burton mips_hpt_frequency = clk_get_rate(clk); 182eed0eabdSPaul Burton clk_put(clk); 183eed0eabdSPaul Burton 184eed0eabdSPaul Burton switch (boot_cpu_type()) { 185eed0eabdSPaul Burton case CPU_20KC: 186eed0eabdSPaul Burton case CPU_25KF: 187eed0eabdSPaul Burton /* The counter runs at the CPU clock rate */ 188eed0eabdSPaul Burton break; 189eed0eabdSPaul Burton default: 190eed0eabdSPaul Burton /* The counter runs at half the CPU clock rate */ 191eed0eabdSPaul Burton mips_hpt_frequency /= 2; 192eed0eabdSPaul Burton break; 193eed0eabdSPaul Burton } 194eed0eabdSPaul Burton } 195eed0eabdSPaul Burton 196ba5d08c0SDaniel Lezcano timer_probe(); 197eed0eabdSPaul Burton } 198eed0eabdSPaul Burton 199eed0eabdSPaul Burton void __init arch_init_irq(void) 200eed0eabdSPaul Burton { 201eed0eabdSPaul Burton struct device_node *intc_node; 202eed0eabdSPaul Burton 203eed0eabdSPaul Burton intc_node = of_find_compatible_node(NULL, NULL, 204eed0eabdSPaul Burton "mti,cpu-interrupt-controller"); 205eed0eabdSPaul Burton if (!cpu_has_veic && !intc_node) 206eed0eabdSPaul Burton mips_cpu_irq_init(); 207eed0eabdSPaul Burton 208eed0eabdSPaul Burton irqchip_init(); 209eed0eabdSPaul Burton } 210eed0eabdSPaul Burton 211eed0eabdSPaul Burton static int __init publish_devices(void) 212eed0eabdSPaul Burton { 213eed0eabdSPaul Burton if (!of_have_populated_dt()) 214eed0eabdSPaul Burton panic("Device-tree not present"); 215eed0eabdSPaul Burton 216eed0eabdSPaul Burton if (of_platform_populate(NULL, of_default_bus_match_table, NULL, NULL)) 217eed0eabdSPaul Burton panic("Failed to populate DT"); 218eed0eabdSPaul Burton 219eed0eabdSPaul Burton return 0; 220eed0eabdSPaul Burton } 221eed0eabdSPaul Burton arch_initcall(publish_devices); 222eed0eabdSPaul Burton 223eed0eabdSPaul Burton void __init prom_free_prom_memory(void) 224eed0eabdSPaul Burton { 225eed0eabdSPaul Burton } 226