setup-rcar-gen2.c 5.9 KB

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