setup-rcar-gen2.c 5.3 KB

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