dram_init.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2012-2015 Panasonic Corporation
  4. * Copyright (C) 2015-2017 Socionext Inc.
  5. * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
  6. */
  7. #include <common.h>
  8. #include <fdt_support.h>
  9. #include <fdtdec.h>
  10. #include <linux/errno.h>
  11. #include <linux/kernel.h>
  12. #include <linux/printk.h>
  13. #include <linux/sizes.h>
  14. #include <asm/global_data.h>
  15. #include "sg-regs.h"
  16. #include "soc-info.h"
  17. DECLARE_GLOBAL_DATA_PTR;
  18. struct uniphier_memif_data {
  19. unsigned int soc_id;
  20. unsigned long sparse_ch1_base;
  21. int have_ch2;
  22. };
  23. static const struct uniphier_memif_data uniphier_memif_data[] = {
  24. {
  25. .soc_id = UNIPHIER_LD4_ID,
  26. .sparse_ch1_base = 0xc0000000,
  27. },
  28. {
  29. .soc_id = UNIPHIER_PRO4_ID,
  30. .sparse_ch1_base = 0xa0000000,
  31. },
  32. {
  33. .soc_id = UNIPHIER_SLD8_ID,
  34. .sparse_ch1_base = 0xc0000000,
  35. },
  36. {
  37. .soc_id = UNIPHIER_PRO5_ID,
  38. .sparse_ch1_base = 0xc0000000,
  39. },
  40. {
  41. .soc_id = UNIPHIER_PXS2_ID,
  42. .sparse_ch1_base = 0xc0000000,
  43. .have_ch2 = 1,
  44. },
  45. {
  46. .soc_id = UNIPHIER_LD6B_ID,
  47. .sparse_ch1_base = 0xc0000000,
  48. .have_ch2 = 1,
  49. },
  50. {
  51. .soc_id = UNIPHIER_LD11_ID,
  52. .sparse_ch1_base = 0xc0000000,
  53. },
  54. {
  55. .soc_id = UNIPHIER_LD20_ID,
  56. .sparse_ch1_base = 0xc0000000,
  57. .have_ch2 = 1,
  58. },
  59. {
  60. .soc_id = UNIPHIER_PXS3_ID,
  61. .sparse_ch1_base = 0xc0000000,
  62. .have_ch2 = 1,
  63. },
  64. };
  65. UNIPHIER_DEFINE_SOCDATA_FUNC(uniphier_get_memif_data, uniphier_memif_data)
  66. struct uniphier_dram_map {
  67. unsigned long base;
  68. unsigned long size;
  69. };
  70. static int uniphier_memconf_decode(struct uniphier_dram_map *dram_map)
  71. {
  72. const struct uniphier_memif_data *data;
  73. unsigned long size;
  74. u32 val;
  75. data = uniphier_get_memif_data();
  76. if (!data) {
  77. pr_err("unsupported SoC\n");
  78. return -EINVAL;
  79. }
  80. val = readl(SG_MEMCONF);
  81. /* set up ch0 */
  82. dram_map[0].base = CONFIG_SYS_SDRAM_BASE;
  83. switch (val & SG_MEMCONF_CH0_SZ_MASK) {
  84. case SG_MEMCONF_CH0_SZ_64M:
  85. size = SZ_64M;
  86. break;
  87. case SG_MEMCONF_CH0_SZ_128M:
  88. size = SZ_128M;
  89. break;
  90. case SG_MEMCONF_CH0_SZ_256M:
  91. size = SZ_256M;
  92. break;
  93. case SG_MEMCONF_CH0_SZ_512M:
  94. size = SZ_512M;
  95. break;
  96. case SG_MEMCONF_CH0_SZ_1G:
  97. size = SZ_1G;
  98. break;
  99. default:
  100. pr_err("error: invalid value is set to MEMCONF ch0 size\n");
  101. return -EINVAL;
  102. }
  103. if ((val & SG_MEMCONF_CH0_NUM_MASK) == SG_MEMCONF_CH0_NUM_2)
  104. size *= 2;
  105. dram_map[0].size = size;
  106. /* set up ch1 */
  107. dram_map[1].base = dram_map[0].base + size;
  108. if (val & SG_MEMCONF_SPARSEMEM) {
  109. if (dram_map[1].base > data->sparse_ch1_base) {
  110. pr_warn("Sparse mem is enabled, but ch0 and ch1 overlap\n");
  111. pr_warn("Only ch0 is available\n");
  112. dram_map[1].base = 0;
  113. return 0;
  114. }
  115. dram_map[1].base = data->sparse_ch1_base;
  116. }
  117. switch (val & SG_MEMCONF_CH1_SZ_MASK) {
  118. case SG_MEMCONF_CH1_SZ_64M:
  119. size = SZ_64M;
  120. break;
  121. case SG_MEMCONF_CH1_SZ_128M:
  122. size = SZ_128M;
  123. break;
  124. case SG_MEMCONF_CH1_SZ_256M:
  125. size = SZ_256M;
  126. break;
  127. case SG_MEMCONF_CH1_SZ_512M:
  128. size = SZ_512M;
  129. break;
  130. case SG_MEMCONF_CH1_SZ_1G:
  131. size = SZ_1G;
  132. break;
  133. default:
  134. pr_err("error: invalid value is set to MEMCONF ch1 size\n");
  135. return -EINVAL;
  136. }
  137. if ((val & SG_MEMCONF_CH1_NUM_MASK) == SG_MEMCONF_CH1_NUM_2)
  138. size *= 2;
  139. dram_map[1].size = size;
  140. if (!data->have_ch2 || val & SG_MEMCONF_CH2_DISABLE)
  141. return 0;
  142. /* set up ch2 */
  143. dram_map[2].base = dram_map[1].base + size;
  144. switch (val & SG_MEMCONF_CH2_SZ_MASK) {
  145. case SG_MEMCONF_CH2_SZ_64M:
  146. size = SZ_64M;
  147. break;
  148. case SG_MEMCONF_CH2_SZ_128M:
  149. size = SZ_128M;
  150. break;
  151. case SG_MEMCONF_CH2_SZ_256M:
  152. size = SZ_256M;
  153. break;
  154. case SG_MEMCONF_CH2_SZ_512M:
  155. size = SZ_512M;
  156. break;
  157. case SG_MEMCONF_CH2_SZ_1G:
  158. size = SZ_1G;
  159. break;
  160. default:
  161. pr_err("error: invalid value is set to MEMCONF ch2 size\n");
  162. return -EINVAL;
  163. }
  164. if ((val & SG_MEMCONF_CH2_NUM_MASK) == SG_MEMCONF_CH2_NUM_2)
  165. size *= 2;
  166. dram_map[2].size = size;
  167. return 0;
  168. }
  169. int dram_init(void)
  170. {
  171. struct uniphier_dram_map dram_map[3] = {};
  172. int ret, i;
  173. gd->ram_size = 0;
  174. ret = uniphier_memconf_decode(dram_map);
  175. if (ret)
  176. return ret;
  177. for (i = 0; i < ARRAY_SIZE(dram_map); i++) {
  178. unsigned long max_size;
  179. if (!dram_map[i].size)
  180. break;
  181. /*
  182. * U-Boot relocates itself to the tail of the memory region,
  183. * but it does not expect sparse memory. We use the first
  184. * contiguous chunk here.
  185. */
  186. if (i > 0 && dram_map[i - 1].base + dram_map[i - 1].size <
  187. dram_map[i].base)
  188. break;
  189. /*
  190. * Do not use memory that exceeds 32bit address range. U-Boot
  191. * relocates itself to the end of the effectively available RAM.
  192. * This could be a problem for DMA engines that do not support
  193. * 64bit address (SDMA of SDHCI, UniPhier AV-ether, etc.)
  194. */
  195. if (dram_map[i].base >= 1ULL << 32)
  196. break;
  197. max_size = (1ULL << 32) - dram_map[i].base;
  198. if (dram_map[i].size > max_size) {
  199. gd->ram_size += max_size;
  200. break;
  201. }
  202. gd->ram_size += dram_map[i].size;
  203. }
  204. /*
  205. * LD20 uses the last 64 byte for each channel for dynamic
  206. * DDR PHY training
  207. */
  208. if (uniphier_get_soc_id() == UNIPHIER_LD20_ID)
  209. gd->ram_size -= 64;
  210. return 0;
  211. }
  212. int dram_init_banksize(void)
  213. {
  214. struct uniphier_dram_map dram_map[3] = {};
  215. int i;
  216. uniphier_memconf_decode(dram_map);
  217. for (i = 0; i < ARRAY_SIZE(dram_map); i++) {
  218. if (i >= ARRAY_SIZE(gd->bd->bi_dram))
  219. break;
  220. gd->bd->bi_dram[i].start = dram_map[i].base;
  221. gd->bd->bi_dram[i].size = dram_map[i].size;
  222. }
  223. return 0;
  224. }
  225. #ifdef CONFIG_OF_BOARD_SETUP
  226. /*
  227. * The DRAM PHY requires 64 byte scratch area in each DRAM channel
  228. * for its dynamic PHY training feature.
  229. */
  230. int ft_board_setup(void *fdt, bd_t *bd)
  231. {
  232. unsigned long rsv_addr;
  233. const unsigned long rsv_size = 64;
  234. int i, ret;
  235. if (uniphier_get_soc_id() != UNIPHIER_LD20_ID)
  236. return 0;
  237. for (i = 0; i < ARRAY_SIZE(gd->bd->bi_dram); i++) {
  238. if (!gd->bd->bi_dram[i].size)
  239. continue;
  240. rsv_addr = gd->bd->bi_dram[i].start + gd->bd->bi_dram[i].size;
  241. rsv_addr -= rsv_size;
  242. ret = fdt_add_mem_rsv(fdt, rsv_addr, rsv_size);
  243. if (ret)
  244. return -ENOSPC;
  245. pr_notice(" Reserved memory region for DRAM PHY training: addr=%lx size=%lx\n",
  246. rsv_addr, rsv_size);
  247. }
  248. return 0;
  249. }
  250. #endif