dram_init.c 6.0 KB

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