init.c 4.7 KB

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