setup-rcar-gen2.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * R-Car Generation 2 support
  3. *
  4. * Copyright (C) 2013 Renesas Solutions Corp.
  5. * Copyright (C) 2013 Magnus Damm
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; version 2 of the License.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <linux/clk/shmobile.h>
  21. #include <linux/clocksource.h>
  22. #include <linux/device.h>
  23. #include <linux/dma-contiguous.h>
  24. #include <linux/io.h>
  25. #include <linux/kernel.h>
  26. #include <linux/of_fdt.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. #define CNTCR 0
  45. #define CNTFID0 0x20
  46. void __init rcar_gen2_timer_init(void)
  47. {
  48. #if defined(CONFIG_ARM_ARCH_TIMER) || defined(CONFIG_COMMON_CLK)
  49. u32 mode = rcar_gen2_read_mode_pins();
  50. #endif
  51. #ifdef CONFIG_ARM_ARCH_TIMER
  52. void __iomem *base;
  53. int extal_mhz = 0;
  54. u32 freq;
  55. /* At Linux boot time the r8a7790 arch timer comes up
  56. * with the counter disabled. Moreover, it may also report
  57. * a potentially incorrect fixed 13 MHz frequency. To be
  58. * correct these registers need to be updated to use the
  59. * frequency EXTAL / 2 which can be determined by the MD pins.
  60. */
  61. switch (mode & (MD(14) | MD(13))) {
  62. case 0:
  63. extal_mhz = 15;
  64. break;
  65. case MD(13):
  66. extal_mhz = 20;
  67. break;
  68. case MD(14):
  69. extal_mhz = 26;
  70. break;
  71. case MD(13) | MD(14):
  72. extal_mhz = 30;
  73. break;
  74. }
  75. /* The arch timer frequency equals EXTAL / 2 */
  76. freq = extal_mhz * (1000000 / 2);
  77. /* Remap "armgcnt address map" space */
  78. base = ioremap(0xe6080000, PAGE_SIZE);
  79. /*
  80. * Update the timer if it is either not running, or is not at the
  81. * right frequency. The timer is only configurable in secure mode
  82. * so this avoids an abort if the loader started the timer and
  83. * entered the kernel in non-secure mode.
  84. */
  85. if ((ioread32(base + CNTCR) & 1) == 0 ||
  86. ioread32(base + CNTFID0) != freq) {
  87. /* Update registers with correct frequency */
  88. iowrite32(freq, base + CNTFID0);
  89. asm volatile("mcr p15, 0, %0, c14, c0, 0" : : "r" (freq));
  90. /* make sure arch timer is started by setting bit 0 of CNTCR */
  91. iowrite32(1, base + CNTCR);
  92. }
  93. iounmap(base);
  94. #endif /* CONFIG_ARM_ARCH_TIMER */
  95. #ifdef CONFIG_COMMON_CLK
  96. rcar_gen2_clocks_init(mode);
  97. #endif
  98. clocksource_of_init();
  99. }
  100. struct memory_reserve_config {
  101. u64 reserved;
  102. u64 base, size;
  103. };
  104. static int __init rcar_gen2_scan_mem(unsigned long node, const char *uname,
  105. int depth, void *data)
  106. {
  107. const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  108. const __be32 *reg, *endp;
  109. int l;
  110. struct memory_reserve_config *mrc = data;
  111. u64 lpae_start = 1ULL << 32;
  112. /* We are scanning "memory" nodes only */
  113. if (type == NULL || strcmp(type, "memory"))
  114. return 0;
  115. reg = of_get_flat_dt_prop(node, "linux,usable-memory", &l);
  116. if (reg == NULL)
  117. reg = of_get_flat_dt_prop(node, "reg", &l);
  118. if (reg == NULL)
  119. return 0;
  120. endp = reg + (l / sizeof(__be32));
  121. while ((endp - reg) >= (dt_root_addr_cells + dt_root_size_cells)) {
  122. u64 base, size;
  123. base = dt_mem_next_cell(dt_root_addr_cells, &reg);
  124. size = dt_mem_next_cell(dt_root_size_cells, &reg);
  125. if (base >= lpae_start)
  126. continue;
  127. if ((base + size) >= lpae_start)
  128. size = lpae_start - base;
  129. if (size < mrc->reserved)
  130. continue;
  131. if (base < mrc->base)
  132. continue;
  133. /* keep the area at top near the 32-bit legacy limit */
  134. mrc->base = base + size - mrc->reserved;
  135. mrc->size = mrc->reserved;
  136. }
  137. return 0;
  138. }
  139. struct cma *rcar_gen2_dma_contiguous;
  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)
  149. dma_contiguous_reserve_area(mrc.size, mrc.base, 0,
  150. &rcar_gen2_dma_contiguous, true);
  151. #endif
  152. }