dram.c 7.5 KB

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