setup-rcar-gen2.c 5.8 KB

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