setup-rcar-gen2.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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/renesas.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 <asm/mach/arch.h>
  27. #include "common.h"
  28. #include "rcar-gen2.h"
  29. #define MODEMR 0xe6160060
  30. u32 rcar_gen2_read_mode_pins(void)
  31. {
  32. static u32 mode;
  33. static bool mode_valid;
  34. if (!mode_valid) {
  35. void __iomem *modemr = ioremap_nocache(MODEMR, 4);
  36. BUG_ON(!modemr);
  37. mode = ioread32(modemr);
  38. iounmap(modemr);
  39. mode_valid = true;
  40. }
  41. return mode;
  42. }
  43. static unsigned int __init get_extal_freq(void)
  44. {
  45. struct device_node *cpg, *extal;
  46. u32 freq = 20000000;
  47. cpg = of_find_compatible_node(NULL, NULL,
  48. "renesas,rcar-gen2-cpg-clocks");
  49. if (!cpg)
  50. return freq;
  51. extal = of_parse_phandle(cpg, "clocks", 0);
  52. of_node_put(cpg);
  53. if (!extal)
  54. return freq;
  55. of_property_read_u32(extal, "clock-frequency", &freq);
  56. of_node_put(extal);
  57. return freq;
  58. }
  59. #define CNTCR 0
  60. #define CNTFID0 0x20
  61. void __init rcar_gen2_timer_init(void)
  62. {
  63. u32 mode = rcar_gen2_read_mode_pins();
  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. rcar_gen2_clocks_init(mode);
  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. }