setup-rcar-gen2.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * R-Car Generation 2 support
  4. *
  5. * Copyright (C) 2013 Renesas Solutions Corp.
  6. * Copyright (C) 2013 Magnus Damm
  7. * Copyright (C) 2014 Ulrich Hecht
  8. */
  9. #include <linux/clocksource.h>
  10. #include <linux/device.h>
  11. #include <linux/dma-map-ops.h>
  12. #include <linux/io.h>
  13. #include <linux/kernel.h>
  14. #include <linux/memblock.h>
  15. #include <linux/of.h>
  16. #include <linux/of_clk.h>
  17. #include <linux/of_fdt.h>
  18. #include <linux/psci.h>
  19. #include <asm/mach/arch.h>
  20. #include <asm/secure_cntvoff.h>
  21. #include "common.h"
  22. #include "rcar-gen2.h"
  23. static const struct of_device_id cpg_matches[] __initconst = {
  24. { .compatible = "renesas,r8a7742-cpg-mssr", .data = "extal" },
  25. { .compatible = "renesas,r8a7743-cpg-mssr", .data = "extal" },
  26. { .compatible = "renesas,r8a7744-cpg-mssr", .data = "extal" },
  27. { .compatible = "renesas,r8a7790-cpg-mssr", .data = "extal" },
  28. { .compatible = "renesas,r8a7791-cpg-mssr", .data = "extal" },
  29. { .compatible = "renesas,r8a7793-cpg-mssr", .data = "extal" },
  30. { /* sentinel */ }
  31. };
  32. static unsigned int __init get_extal_freq(void)
  33. {
  34. const struct of_device_id *match;
  35. struct device_node *cpg, *extal;
  36. u32 freq = 20000000;
  37. int idx = 0;
  38. cpg = of_find_matching_node_and_match(NULL, cpg_matches, &match);
  39. if (!cpg)
  40. return freq;
  41. if (match->data)
  42. idx = of_property_match_string(cpg, "clock-names", match->data);
  43. extal = of_parse_phandle(cpg, "clocks", idx);
  44. of_node_put(cpg);
  45. if (!extal)
  46. return freq;
  47. of_property_read_u32(extal, "clock-frequency", &freq);
  48. of_node_put(extal);
  49. return freq;
  50. }
  51. #define CNTCR 0
  52. #define CNTFID0 0x20
  53. static void __init rcar_gen2_timer_init(void)
  54. {
  55. bool need_update = true;
  56. void __iomem *base;
  57. u32 freq;
  58. /*
  59. * If PSCI is available then most likely we are running on PSCI-enabled
  60. * U-Boot which, we assume, has already taken care of resetting CNTVOFF
  61. * and updating counter module before switching to non-secure mode
  62. * and we don't need to.
  63. */
  64. #ifdef CONFIG_ARM_PSCI_FW
  65. if (psci_ops.cpu_on)
  66. need_update = false;
  67. #endif
  68. if (need_update == false)
  69. goto skip_update;
  70. secure_cntvoff_init();
  71. if (of_machine_is_compatible("renesas,r8a7745") ||
  72. of_machine_is_compatible("renesas,r8a77470") ||
  73. of_machine_is_compatible("renesas,r8a7792") ||
  74. of_machine_is_compatible("renesas,r8a7794")) {
  75. freq = 260000000 / 8; /* ZS / 8 */
  76. } else {
  77. /* At Linux boot time the r8a7790 arch timer comes up
  78. * with the counter disabled. Moreover, it may also report
  79. * a potentially incorrect fixed 13 MHz frequency. To be
  80. * correct these registers need to be updated to use the
  81. * frequency EXTAL / 2.
  82. */
  83. freq = get_extal_freq() / 2;
  84. }
  85. /* Remap "armgcnt address map" space */
  86. base = ioremap(0xe6080000, PAGE_SIZE);
  87. /*
  88. * Update the timer if it is either not running, or is not at the
  89. * right frequency. The timer is only configurable in secure mode
  90. * so this avoids an abort if the loader started the timer and
  91. * entered the kernel in non-secure mode.
  92. */
  93. if ((ioread32(base + CNTCR) & 1) == 0 ||
  94. ioread32(base + CNTFID0) != freq) {
  95. /* Update registers with correct frequency */
  96. iowrite32(freq, base + CNTFID0);
  97. asm volatile("mcr p15, 0, %0, c14, c0, 0" : : "r" (freq));
  98. /* make sure arch timer is started by setting bit 0 of CNTCR */
  99. iowrite32(1, base + CNTCR);
  100. }
  101. iounmap(base);
  102. skip_update:
  103. of_clk_init(NULL);
  104. timer_probe();
  105. }
  106. struct memory_reserve_config {
  107. u64 reserved;
  108. u64 base, size;
  109. };
  110. static int __init rcar_gen2_scan_mem(unsigned long node, const char *uname,
  111. int depth, void *data)
  112. {
  113. const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  114. const __be32 *reg, *endp;
  115. int l;
  116. struct memory_reserve_config *mrc = data;
  117. u64 lpae_start = 1ULL << 32;
  118. /* We are scanning "memory" nodes only */
  119. if (type == NULL || strcmp(type, "memory"))
  120. return 0;
  121. reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
  122. if (reg == NULL)
  123. reg = of_get_flat_dt_prop(node, "reg", &l);
  124. if (reg == NULL)
  125. return 0;
  126. endp = reg + (l / sizeof(__be32));
  127. while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
  128. u64 base, size;
  129. base = dt_mem_next_cell(dt_root_addr_cells, &reg);
  130. size = dt_mem_next_cell(dt_root_size_cells, &reg);
  131. if (base >= lpae_start)
  132. continue;
  133. if ((base + size) >= lpae_start)
  134. size = lpae_start - base;
  135. if (size < mrc->reserved)
  136. continue;
  137. if (base < mrc->base)
  138. continue;
  139. /* keep the area at top near the 32-bit legacy limit */
  140. mrc->base = base + size - mrc->reserved;
  141. mrc->size = mrc->reserved;
  142. }
  143. return 0;
  144. }
  145. static void __init rcar_gen2_reserve(void)
  146. {
  147. struct memory_reserve_config mrc;
  148. /* reserve 256 MiB at the top of the physical legacy 32-bit space */
  149. memset(&mrc, 0, sizeof(mrc));
  150. mrc.reserved = SZ_256M;
  151. of_scan_flat_dt(rcar_gen2_scan_mem, &mrc);
  152. #ifdef CONFIG_DMA_CMA
  153. if (mrc.size && memblock_is_region_memory(mrc.base, mrc.size)) {
  154. static struct cma *rcar_gen2_dma_contiguous;
  155. dma_contiguous_reserve_area(mrc.size, mrc.base, 0,
  156. &rcar_gen2_dma_contiguous, true);
  157. }
  158. #endif
  159. }
  160. static const char * const rcar_gen2_boards_compat_dt[] __initconst = {
  161. "renesas,r8a7790",
  162. "renesas,r8a7791",
  163. "renesas,r8a7792",
  164. "renesas,r8a7793",
  165. "renesas,r8a7794",
  166. NULL
  167. };
  168. DT_MACHINE_START(RCAR_GEN2_DT, "Generic R-Car Gen2 (Flattened Device Tree)")
  169. .init_late = shmobile_init_late,
  170. .init_time = rcar_gen2_timer_init,
  171. .reserve = rcar_gen2_reserve,
  172. .dt_compat = rcar_gen2_boards_compat_dt,
  173. MACHINE_END
  174. static const char * const rz_g1_boards_compat_dt[] __initconst = {
  175. "renesas,r8a7742",
  176. "renesas,r8a7743",
  177. "renesas,r8a7744",
  178. "renesas,r8a7745",
  179. "renesas,r8a77470",
  180. NULL
  181. };
  182. DT_MACHINE_START(RZ_G1_DT, "Generic RZ/G1 (Flattened Device Tree)")
  183. .init_late = shmobile_init_late,
  184. .init_time = rcar_gen2_timer_init,
  185. .reserve = rcar_gen2_reserve,
  186. .dt_compat = rz_g1_boards_compat_dt,
  187. MACHINE_END