dram.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * (C) Copyright 2009
  3. * Marvell Semiconductor <www.marvell.com>
  4. * Written-by: Prafulla Wadaskar <prafulla@marvell.com>
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <config.h>
  9. #include <common.h>
  10. #include <asm/io.h>
  11. #include <asm/arch/cpu.h>
  12. #include <asm/arch/soc.h>
  13. #if defined(CONFIG_ARCH_MVEBU)
  14. /* Use common XOR definitions for A3x and AXP */
  15. #include "../../../drivers/ddr/marvell/axp/xor.h"
  16. #include "../../../drivers/ddr/marvell/axp/xor_regs.h"
  17. #endif
  18. DECLARE_GLOBAL_DATA_PTR;
  19. struct sdram_bank {
  20. u32 win_bar;
  21. u32 win_sz;
  22. };
  23. struct sdram_addr_dec {
  24. struct sdram_bank sdram_bank[4];
  25. };
  26. #define REG_CPUCS_WIN_ENABLE (1 << 0)
  27. #define REG_CPUCS_WIN_WR_PROTECT (1 << 1)
  28. #define REG_CPUCS_WIN_WIN0_CS(x) (((x) & 0x3) << 2)
  29. #define REG_CPUCS_WIN_SIZE(x) (((x) & 0xff) << 24)
  30. #define SDRAM_SIZE_MAX 0xc0000000
  31. #define SCRUB_MAGIC 0xbeefdead
  32. #define SCRB_XOR_UNIT 0
  33. #define SCRB_XOR_CHAN 1
  34. #define SCRB_XOR_WIN 0
  35. #define XEBARX_BASE_OFFS 16
  36. /*
  37. * mvebu_sdram_bar - reads SDRAM Base Address Register
  38. */
  39. u32 mvebu_sdram_bar(enum memory_bank bank)
  40. {
  41. struct sdram_addr_dec *base =
  42. (struct sdram_addr_dec *)MVEBU_SDRAM_BASE;
  43. u32 result = 0;
  44. u32 enable = 0x01 & readl(&base->sdram_bank[bank].win_sz);
  45. if ((!enable) || (bank > BANK3))
  46. return 0;
  47. result = readl(&base->sdram_bank[bank].win_bar);
  48. return result;
  49. }
  50. /*
  51. * mvebu_sdram_bs_set - writes SDRAM Bank size
  52. */
  53. static void mvebu_sdram_bs_set(enum memory_bank bank, u32 size)
  54. {
  55. struct sdram_addr_dec *base =
  56. (struct sdram_addr_dec *)MVEBU_SDRAM_BASE;
  57. /* Read current register value */
  58. u32 reg = readl(&base->sdram_bank[bank].win_sz);
  59. /* Clear window size */
  60. reg &= ~REG_CPUCS_WIN_SIZE(0xFF);
  61. /* Set new window size */
  62. reg |= REG_CPUCS_WIN_SIZE((size - 1) >> 24);
  63. writel(reg, &base->sdram_bank[bank].win_sz);
  64. }
  65. /*
  66. * mvebu_sdram_bs - reads SDRAM Bank size
  67. */
  68. u32 mvebu_sdram_bs(enum memory_bank bank)
  69. {
  70. struct sdram_addr_dec *base =
  71. (struct sdram_addr_dec *)MVEBU_SDRAM_BASE;
  72. u32 result = 0;
  73. u32 enable = 0x01 & readl(&base->sdram_bank[bank].win_sz);
  74. if ((!enable) || (bank > BANK3))
  75. return 0;
  76. result = 0xff000000 & readl(&base->sdram_bank[bank].win_sz);
  77. result += 0x01000000;
  78. return result;
  79. }
  80. void mvebu_sdram_size_adjust(enum memory_bank bank)
  81. {
  82. u32 size;
  83. /* probe currently equipped RAM size */
  84. size = get_ram_size((void *)mvebu_sdram_bar(bank),
  85. mvebu_sdram_bs(bank));
  86. /* adjust SDRAM window size accordingly */
  87. mvebu_sdram_bs_set(bank, size);
  88. }
  89. #if defined(CONFIG_ARCH_MVEBU)
  90. static u32 xor_ctrl_save;
  91. static u32 xor_base_save;
  92. static u32 xor_mask_save;
  93. static void mv_xor_init2(u32 cs)
  94. {
  95. u32 reg, base, size, base2;
  96. u32 bank_attr[4] = { 0xe00, 0xd00, 0xb00, 0x700 };
  97. xor_ctrl_save = reg_read(XOR_WINDOW_CTRL_REG(SCRB_XOR_UNIT,
  98. SCRB_XOR_CHAN));
  99. xor_base_save = reg_read(XOR_BASE_ADDR_REG(SCRB_XOR_UNIT,
  100. SCRB_XOR_WIN));
  101. xor_mask_save = reg_read(XOR_SIZE_MASK_REG(SCRB_XOR_UNIT,
  102. SCRB_XOR_WIN));
  103. /* Enable Window x for each CS */
  104. reg = 0x1;
  105. reg |= (0x3 << 16);
  106. reg_write(XOR_WINDOW_CTRL_REG(SCRB_XOR_UNIT, SCRB_XOR_CHAN), reg);
  107. base = 0;
  108. size = mvebu_sdram_bs(cs) - 1;
  109. if (size) {
  110. base2 = ((base / (64 << 10)) << XEBARX_BASE_OFFS) |
  111. bank_attr[cs];
  112. reg_write(XOR_BASE_ADDR_REG(SCRB_XOR_UNIT, SCRB_XOR_WIN),
  113. base2);
  114. base += size + 1;
  115. size = (size / (64 << 10)) << 16;
  116. /* Window x - size - 256 MB */
  117. reg_write(XOR_SIZE_MASK_REG(SCRB_XOR_UNIT, SCRB_XOR_WIN), size);
  118. }
  119. mv_xor_hal_init(0);
  120. return;
  121. }
  122. static void mv_xor_finish2(void)
  123. {
  124. reg_write(XOR_WINDOW_CTRL_REG(SCRB_XOR_UNIT, SCRB_XOR_CHAN),
  125. xor_ctrl_save);
  126. reg_write(XOR_BASE_ADDR_REG(SCRB_XOR_UNIT, SCRB_XOR_WIN),
  127. xor_base_save);
  128. reg_write(XOR_SIZE_MASK_REG(SCRB_XOR_UNIT, SCRB_XOR_WIN),
  129. xor_mask_save);
  130. }
  131. static void dram_ecc_scrubbing(void)
  132. {
  133. int cs;
  134. u32 size, temp;
  135. u32 total_mem = 0;
  136. u64 total;
  137. u32 start_addr;
  138. /*
  139. * The DDR training code from the bin_hdr / SPL already
  140. * scrubbed the DDR till 0x1000000. And the main U-Boot
  141. * is loaded to an address < 0x1000000. So we need to
  142. * skip this range to not re-scrub this area again.
  143. */
  144. temp = reg_read(REG_SDRAM_CONFIG_ADDR);
  145. temp |= (1 << REG_SDRAM_CONFIG_IERR_OFFS);
  146. reg_write(REG_SDRAM_CONFIG_ADDR, temp);
  147. for (cs = 0; cs < CONFIG_NR_DRAM_BANKS; cs++) {
  148. size = mvebu_sdram_bs(cs) - 1;
  149. if (size == 0)
  150. continue;
  151. total = (u64)size + 1;
  152. total_mem += (u32)(total / (1 << 30));
  153. start_addr = 0;
  154. mv_xor_init2(cs);
  155. /* Skip first 16 MiB */
  156. if (0 == cs) {
  157. start_addr = 0x1000000;
  158. size -= start_addr;
  159. }
  160. mv_xor_mem_init(SCRB_XOR_CHAN, start_addr, size,
  161. SCRUB_MAGIC, SCRUB_MAGIC);
  162. /* Wait for previous transfer completion */
  163. while (mv_xor_state_get(SCRB_XOR_CHAN) != MV_IDLE)
  164. ;
  165. mv_xor_finish2();
  166. }
  167. temp = reg_read(REG_SDRAM_CONFIG_ADDR);
  168. temp &= ~(1 << REG_SDRAM_CONFIG_IERR_OFFS);
  169. reg_write(REG_SDRAM_CONFIG_ADDR, temp);
  170. }
  171. static int ecc_enabled(void)
  172. {
  173. if (reg_read(REG_SDRAM_CONFIG_ADDR) & (1 << REG_SDRAM_CONFIG_ECC_OFFS))
  174. return 1;
  175. return 0;
  176. }
  177. #else
  178. static void dram_ecc_scrubbing(void)
  179. {
  180. }
  181. static int ecc_enabled(void)
  182. {
  183. return 0;
  184. }
  185. #endif
  186. int dram_init(void)
  187. {
  188. u64 size = 0;
  189. int i;
  190. for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
  191. /*
  192. * It is assumed that all memory banks are consecutive
  193. * and without gaps.
  194. * If the gap is found, ram_size will be reported for
  195. * consecutive memory only
  196. */
  197. if (mvebu_sdram_bar(i) != size)
  198. break;
  199. /*
  200. * Don't report more than 3GiB of SDRAM, otherwise there is no
  201. * address space left for the internal registers etc.
  202. */
  203. size += mvebu_sdram_bs(i);
  204. if (size > SDRAM_SIZE_MAX)
  205. size = SDRAM_SIZE_MAX;
  206. }
  207. for (; i < CONFIG_NR_DRAM_BANKS; i++) {
  208. /* If above loop terminated prematurely, we need to set
  209. * remaining banks' start address & size as 0. Otherwise other
  210. * u-boot functions and Linux kernel gets wrong values which
  211. * could result in crash */
  212. gd->bd->bi_dram[i].start = 0;
  213. gd->bd->bi_dram[i].size = 0;
  214. }
  215. if (ecc_enabled())
  216. dram_ecc_scrubbing();
  217. gd->ram_size = size;
  218. return 0;
  219. }
  220. /*
  221. * If this function is not defined here,
  222. * board.c alters dram bank zero configuration defined above.
  223. */
  224. void dram_init_banksize(void)
  225. {
  226. u64 size = 0;
  227. int i;
  228. for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
  229. gd->bd->bi_dram[i].start = mvebu_sdram_bar(i);
  230. gd->bd->bi_dram[i].size = mvebu_sdram_bs(i);
  231. /* Clip the banksize to 1GiB if it exceeds the max size */
  232. size += gd->bd->bi_dram[i].size;
  233. if (size > SDRAM_SIZE_MAX)
  234. mvebu_sdram_bs_set(i, 0x40000000);
  235. }
  236. }
  237. #if defined(CONFIG_ARCH_MVEBU)
  238. void board_add_ram_info(int use_default)
  239. {
  240. struct sar_freq_modes sar_freq;
  241. get_sar_freq(&sar_freq);
  242. printf(" (%d MHz, ", sar_freq.d_clk);
  243. if (ecc_enabled())
  244. printf("ECC");
  245. else
  246. printf("ECC not");
  247. printf(" enabled)");
  248. }
  249. #endif