setup-rcar-gen2.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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/shmobile.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. #define CNTCR 0
  44. #define CNTFID0 0x20
  45. void __init rcar_gen2_timer_init(void)
  46. {
  47. u32 mode = rcar_gen2_read_mode_pins();
  48. #ifdef CONFIG_ARM_ARCH_TIMER
  49. void __iomem *base;
  50. int extal_mhz = 0;
  51. u32 freq;
  52. if (of_machine_is_compatible("renesas,r8a7794")) {
  53. freq = 260000000 / 8; /* ZS / 8 */
  54. /* CNTVOFF has to be initialized either from non-secure
  55. * Hypervisor mode or secure Monitor mode with SCR.NS==1.
  56. * If TrustZone is enabled then it should be handled by the
  57. * secure code.
  58. */
  59. asm volatile(
  60. " cps 0x16\n"
  61. " mrc p15, 0, r1, c1, c1, 0\n"
  62. " orr r0, r1, #1\n"
  63. " mcr p15, 0, r0, c1, c1, 0\n"
  64. " isb\n"
  65. " mov r0, #0\n"
  66. " mcrr p15, 4, r0, r0, c14\n"
  67. " isb\n"
  68. " mcr p15, 0, r1, c1, c1, 0\n"
  69. " isb\n"
  70. " cps 0x13\n"
  71. : : : "r0", "r1");
  72. } else {
  73. /* At Linux boot time the r8a7790 arch timer comes up
  74. * with the counter disabled. Moreover, it may also report
  75. * a potentially incorrect fixed 13 MHz frequency. To be
  76. * correct these registers need to be updated to use the
  77. * frequency EXTAL / 2 which can be determined by the MD pins.
  78. */
  79. switch (mode & (MD(14) | MD(13))) {
  80. case 0:
  81. extal_mhz = 15;
  82. break;
  83. case MD(13):
  84. extal_mhz = 20;
  85. break;
  86. case MD(14):
  87. extal_mhz = 26;
  88. break;
  89. case MD(13) | MD(14):
  90. extal_mhz = 30;
  91. break;
  92. }
  93. /* The arch timer frequency equals EXTAL / 2 */
  94. freq = extal_mhz * (1000000 / 2);
  95. }
  96. /* Remap "armgcnt address map" space */
  97. base = ioremap(0xe6080000, PAGE_SIZE);
  98. /*
  99. * Update the timer if it is either not running, or is not at the
  100. * right frequency. The timer is only configurable in secure mode
  101. * so this avoids an abort if the loader started the timer and
  102. * entered the kernel in non-secure mode.
  103. */
  104. if ((ioread32(base + CNTCR) & 1) == 0 ||
  105. ioread32(base + CNTFID0) != freq) {
  106. /* Update registers with correct frequency */
  107. iowrite32(freq, base + CNTFID0);
  108. asm volatile("mcr p15, 0, %0, c14, c0, 0" : : "r" (freq));
  109. /* make sure arch timer is started by setting bit 0 of CNTCR */
  110. iowrite32(1, base + CNTCR);
  111. }
  112. iounmap(base);
  113. #endif /* CONFIG_ARM_ARCH_TIMER */
  114. rcar_gen2_clocks_init(mode);
  115. clocksource_probe();
  116. }
  117. struct memory_reserve_config {
  118. u64 reserved;
  119. u64 base, size;
  120. };
  121. static int __init rcar_gen2_scan_mem(unsigned long node, const char *uname,
  122. int depth, void *data)
  123. {
  124. const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  125. const __be32 *reg, *endp;
  126. int l;
  127. struct memory_reserve_config *mrc = data;
  128. u64 lpae_start = 1ULL << 32;
  129. /* We are scanning "memory" nodes only */
  130. if (type == NULL || strcmp(type, "memory"))
  131. return 0;
  132. reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
  133. if (reg == NULL)
  134. reg = of_get_flat_dt_prop(node, "reg", &l);
  135. if (reg == NULL)
  136. return 0;
  137. endp = reg + (l / sizeof(__be32));
  138. while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
  139. u64 base, size;
  140. base = dt_mem_next_cell(dt_root_addr_cells, &reg);
  141. size = dt_mem_next_cell(dt_root_size_cells, &reg);
  142. if (base >= lpae_start)
  143. continue;
  144. if ((base + size) >= lpae_start)
  145. size = lpae_start - base;
  146. if (size < mrc->reserved)
  147. continue;
  148. if (base < mrc->base)
  149. continue;
  150. /* keep the area at top near the 32-bit legacy limit */
  151. mrc->base = base + size - mrc->reserved;
  152. mrc->size = mrc->reserved;
  153. }
  154. return 0;
  155. }
  156. struct cma *rcar_gen2_dma_contiguous;
  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. dma_contiguous_reserve_area(mrc.size, mrc.base, 0,
  167. &rcar_gen2_dma_contiguous, true);
  168. #endif
  169. }