setup-rcar-gen2.c 5.6 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 "common.h"
  29. #include "rcar-gen2.h"
  30. static unsigned int __init get_extal_freq(void)
  31. {
  32. struct device_node *cpg, *extal;
  33. u32 freq = 20000000;
  34. cpg = of_find_compatible_node(NULL, NULL,
  35. "renesas,rcar-gen2-cpg-clocks");
  36. if (!cpg)
  37. return freq;
  38. extal = of_parse_phandle(cpg, "clocks", 0);
  39. of_node_put(cpg);
  40. if (!extal)
  41. return freq;
  42. of_property_read_u32(extal, "clock-frequency", &freq);
  43. of_node_put(extal);
  44. return freq;
  45. }
  46. #define CNTCR 0
  47. #define CNTFID0 0x20
  48. void __init rcar_gen2_timer_init(void)
  49. {
  50. #ifdef CONFIG_ARM_ARCH_TIMER
  51. void __iomem *base;
  52. u32 freq;
  53. if (of_machine_is_compatible("renesas,r8a7792") ||
  54. of_machine_is_compatible("renesas,r8a7794")) {
  55. freq = 260000000 / 8; /* ZS / 8 */
  56. /* CNTVOFF has to be initialized either from non-secure
  57. * Hypervisor mode or secure Monitor mode with SCR.NS==1.
  58. * If TrustZone is enabled then it should be handled by the
  59. * secure code.
  60. */
  61. asm volatile(
  62. " cps 0x16\n"
  63. " mrc p15, 0, r1, c1, c1, 0\n"
  64. " orr r0, r1, #1\n"
  65. " mcr p15, 0, r0, c1, c1, 0\n"
  66. " isb\n"
  67. " mov r0, #0\n"
  68. " mcrr p15, 4, r0, r0, c14\n"
  69. " isb\n"
  70. " mcr p15, 0, r1, c1, c1, 0\n"
  71. " isb\n"
  72. " cps 0x13\n"
  73. : : : "r0", "r1");
  74. } else {
  75. /* At Linux boot time the r8a7790 arch timer comes up
  76. * with the counter disabled. Moreover, it may also report
  77. * a potentially incorrect fixed 13 MHz frequency. To be
  78. * correct these registers need to be updated to use the
  79. * frequency EXTAL / 2.
  80. */
  81. freq = get_extal_freq() / 2;
  82. }
  83. /* Remap "armgcnt address map" space */
  84. base = ioremap(0xe6080000, PAGE_SIZE);
  85. /*
  86. * Update the timer if it is either not running, or is not at the
  87. * right frequency. The timer is only configurable in secure mode
  88. * so this avoids an abort if the loader started the timer and
  89. * entered the kernel in non-secure mode.
  90. */
  91. if ((ioread32(base + CNTCR) & 1) == 0 ||
  92. ioread32(base + CNTFID0) != freq) {
  93. /* Update registers with correct frequency */
  94. iowrite32(freq, base + CNTFID0);
  95. asm volatile("mcr p15, 0, %0, c14, c0, 0" : : "r" (freq));
  96. /* make sure arch timer is started by setting bit 0 of CNTCR */
  97. iowrite32(1, base + CNTCR);
  98. }
  99. iounmap(base);
  100. #endif /* CONFIG_ARM_ARCH_TIMER */
  101. of_clk_init(NULL);
  102. clocksource_probe();
  103. }
  104. struct memory_reserve_config {
  105. u64 reserved;
  106. u64 base, size;
  107. };
  108. static int __init rcar_gen2_scan_mem(unsigned long node, const char *uname,
  109. int depth, void *data)
  110. {
  111. const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  112. const __be32 *reg, *endp;
  113. int l;
  114. struct memory_reserve_config *mrc = data;
  115. u64 lpae_start = 1ULL << 32;
  116. /* We are scanning "memory" nodes only */
  117. if (type == NULL || strcmp(type, "memory"))
  118. return 0;
  119. reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
  120. if (reg == NULL)
  121. reg = of_get_flat_dt_prop(node, "reg", &l);
  122. if (reg == NULL)
  123. return 0;
  124. endp = reg + (l / sizeof(__be32));
  125. while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
  126. u64 base, size;
  127. base = dt_mem_next_cell(dt_root_addr_cells, &reg);
  128. size = dt_mem_next_cell(dt_root_size_cells, &reg);
  129. if (base >= lpae_start)
  130. continue;
  131. if ((base + size) >= lpae_start)
  132. size = lpae_start - base;
  133. if (size < mrc->reserved)
  134. continue;
  135. if (base < mrc->base)
  136. continue;
  137. /* keep the area at top near the 32-bit legacy limit */
  138. mrc->base = base + size - mrc->reserved;
  139. mrc->size = mrc->reserved;
  140. }
  141. return 0;
  142. }
  143. void __init rcar_gen2_reserve(void)
  144. {
  145. struct memory_reserve_config mrc;
  146. /* reserve 256 MiB at the top of the physical legacy 32-bit space */
  147. memset(&mrc, 0, sizeof(mrc));
  148. mrc.reserved = SZ_256M;
  149. of_scan_flat_dt(rcar_gen2_scan_mem, &mrc);
  150. #ifdef CONFIG_DMA_CMA
  151. if (mrc.size && memblock_is_region_memory(mrc.base, mrc.size)) {
  152. static struct cma *rcar_gen2_dma_contiguous;
  153. dma_contiguous_reserve_area(mrc.size, mrc.base, 0,
  154. &rcar_gen2_dma_contiguous, true);
  155. }
  156. #endif
  157. }
  158. static const char * const rcar_gen2_boards_compat_dt[] __initconst = {
  159. /*
  160. * R8A7790 and R8A7791 can't be handled here as long as they need SMP
  161. * initialization fallback.
  162. */
  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_early = shmobile_init_delay,
  170. .init_late = shmobile_init_late,
  171. .init_time = rcar_gen2_timer_init,
  172. .reserve = rcar_gen2_reserve,
  173. .dt_compat = rcar_gen2_boards_compat_dt,
  174. MACHINE_END
  175. static const char * const rz_g1_boards_compat_dt[] __initconst = {
  176. "renesas,r8a7743",
  177. "renesas,r8a7745",
  178. NULL,
  179. };
  180. DT_MACHINE_START(RZ_G1_DT, "Generic RZ/G1 (Flattened Device Tree)")
  181. .init_early = shmobile_init_delay,
  182. .init_late = shmobile_init_late,
  183. .init_time = rcar_gen2_timer_init,
  184. .reserve = rcar_gen2_reserve,
  185. .dt_compat = rz_g1_boards_compat_dt,
  186. MACHINE_END