setup-rcar-gen2.c 5.2 KB

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