sdram.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. /*
  2. * Copyright Altera Corporation (C) 2014-2015
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <div64.h>
  8. #include <watchdog.h>
  9. #include <asm/arch/fpga_manager.h>
  10. #include <asm/arch/sdram.h>
  11. #include <asm/arch/system_manager.h>
  12. #include <asm/io.h>
  13. /*
  14. * FIXME: This path is temporary until the SDRAM driver gets
  15. * a proper thorough cleanup.
  16. */
  17. #include "../../../board/altera/socfpga/qts/sdram_config.h"
  18. DECLARE_GLOBAL_DATA_PTR;
  19. struct sdram_prot_rule {
  20. u64 sdram_start; /* SDRAM start address */
  21. u64 sdram_end; /* SDRAM end address */
  22. u32 rule; /* SDRAM protection rule number: 0-19 */
  23. int valid; /* Rule valid or not? 1 - valid, 0 not*/
  24. u32 security;
  25. u32 portmask;
  26. u32 result;
  27. u32 lo_prot_id;
  28. u32 hi_prot_id;
  29. };
  30. static struct socfpga_system_manager *sysmgr_regs =
  31. (struct socfpga_system_manager *)SOCFPGA_SYSMGR_ADDRESS;
  32. static struct socfpga_sdr_ctrl *sdr_ctrl =
  33. (struct socfpga_sdr_ctrl *)SDR_CTRLGRP_ADDRESS;
  34. static struct socfpga_sdram_config {
  35. u32 ctrl_cfg;
  36. u32 dram_timing1;
  37. u32 dram_timing2;
  38. u32 dram_timing3;
  39. u32 dram_timing4;
  40. u32 lowpwr_timing;
  41. u32 dram_addrw;
  42. u32 static_cfg;
  43. u32 fifo_cfg;
  44. u32 mp_weight0;
  45. u32 mp_weight1;
  46. u32 mp_weight2;
  47. u32 mp_weight3;
  48. u32 mp_pacing0;
  49. u32 mp_pacing1;
  50. u32 mp_pacing2;
  51. u32 mp_pacing3;
  52. u32 mp_threshold0;
  53. u32 mp_threshold1;
  54. u32 mp_threshold2;
  55. } sdram_config = {
  56. .ctrl_cfg =
  57. (CONFIG_HPS_SDR_CTRLCFG_CTRLCFG_MEMTYPE <<
  58. SDR_CTRLGRP_CTRLCFG_MEMTYPE_LSB) |
  59. (CONFIG_HPS_SDR_CTRLCFG_CTRLCFG_MEMBL <<
  60. SDR_CTRLGRP_CTRLCFG_MEMBL_LSB) |
  61. (CONFIG_HPS_SDR_CTRLCFG_CTRLCFG_ECCEN <<
  62. SDR_CTRLGRP_CTRLCFG_ECCEN_LSB) |
  63. (CONFIG_HPS_SDR_CTRLCFG_CTRLCFG_ECCCORREN <<
  64. SDR_CTRLGRP_CTRLCFG_ECCCORREN_LSB) |
  65. (CONFIG_HPS_SDR_CTRLCFG_CTRLCFG_REORDEREN <<
  66. SDR_CTRLGRP_CTRLCFG_REORDEREN_LSB) |
  67. (CONFIG_HPS_SDR_CTRLCFG_CTRLCFG_STARVELIMIT <<
  68. SDR_CTRLGRP_CTRLCFG_STARVELIMIT_LSB) |
  69. (CONFIG_HPS_SDR_CTRLCFG_CTRLCFG_DQSTRKEN <<
  70. SDR_CTRLGRP_CTRLCFG_DQSTRKEN_LSB) |
  71. (CONFIG_HPS_SDR_CTRLCFG_CTRLCFG_NODMPINS <<
  72. SDR_CTRLGRP_CTRLCFG_NODMPINS_LSB),
  73. .dram_timing1 =
  74. (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING1_TCWL <<
  75. SDR_CTRLGRP_DRAMTIMING1_TCWL_LSB) |
  76. (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING1_AL <<
  77. SDR_CTRLGRP_DRAMTIMING1_TAL_LSB) |
  78. (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING1_TCL <<
  79. SDR_CTRLGRP_DRAMTIMING1_TCL_LSB) |
  80. (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING1_TRRD <<
  81. SDR_CTRLGRP_DRAMTIMING1_TRRD_LSB) |
  82. (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING1_TFAW <<
  83. SDR_CTRLGRP_DRAMTIMING1_TFAW_LSB) |
  84. (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING1_TRFC <<
  85. SDR_CTRLGRP_DRAMTIMING1_TRFC_LSB),
  86. .dram_timing2 =
  87. (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING2_IF_TREFI <<
  88. SDR_CTRLGRP_DRAMTIMING2_TREFI_LSB) |
  89. (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING2_IF_TRCD <<
  90. SDR_CTRLGRP_DRAMTIMING2_TRCD_LSB) |
  91. (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING2_IF_TRP <<
  92. SDR_CTRLGRP_DRAMTIMING2_TRP_LSB) |
  93. (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING2_IF_TWR <<
  94. SDR_CTRLGRP_DRAMTIMING2_TWR_LSB) |
  95. (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING2_IF_TWTR <<
  96. SDR_CTRLGRP_DRAMTIMING2_TWTR_LSB),
  97. .dram_timing3 =
  98. (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING3_TRTP <<
  99. SDR_CTRLGRP_DRAMTIMING3_TRTP_LSB) |
  100. (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING3_TRAS <<
  101. SDR_CTRLGRP_DRAMTIMING3_TRAS_LSB) |
  102. (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING3_TRC <<
  103. SDR_CTRLGRP_DRAMTIMING3_TRC_LSB) |
  104. (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING3_TMRD <<
  105. SDR_CTRLGRP_DRAMTIMING3_TMRD_LSB) |
  106. (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING3_TCCD <<
  107. SDR_CTRLGRP_DRAMTIMING3_TCCD_LSB),
  108. .dram_timing4 =
  109. (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING4_SELFRFSHEXIT <<
  110. SDR_CTRLGRP_DRAMTIMING4_SELFRFSHEXIT_LSB) |
  111. (CONFIG_HPS_SDR_CTRLCFG_DRAMTIMING4_PWRDOWNEXIT <<
  112. SDR_CTRLGRP_DRAMTIMING4_PWRDOWNEXIT_LSB),
  113. .lowpwr_timing =
  114. (CONFIG_HPS_SDR_CTRLCFG_LOWPWRTIMING_AUTOPDCYCLES <<
  115. SDR_CTRLGRP_LOWPWRTIMING_AUTOPDCYCLES_LSB) |
  116. (CONFIG_HPS_SDR_CTRLCFG_LOWPWRTIMING_CLKDISABLECYCLES <<
  117. SDR_CTRLGRP_LOWPWRTIMING_CLKDISABLECYCLES_LSB),
  118. .dram_addrw =
  119. (CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_COLBITS <<
  120. SDR_CTRLGRP_DRAMADDRW_COLBITS_LSB) |
  121. (CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_BANKBITS <<
  122. SDR_CTRLGRP_DRAMADDRW_BANKBITS_LSB) |
  123. ((CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_CSBITS - 1) <<
  124. SDR_CTRLGRP_DRAMADDRW_CSBITS_LSB),
  125. .static_cfg =
  126. (CONFIG_HPS_SDR_CTRLCFG_STATICCFG_MEMBL <<
  127. SDR_CTRLGRP_STATICCFG_MEMBL_LSB) |
  128. (CONFIG_HPS_SDR_CTRLCFG_STATICCFG_USEECCASDATA <<
  129. SDR_CTRLGRP_STATICCFG_USEECCASDATA_LSB),
  130. .fifo_cfg =
  131. (CONFIG_HPS_SDR_CTRLCFG_FIFOCFG_SYNCMODE <<
  132. SDR_CTRLGRP_FIFOCFG_SYNCMODE_LSB) |
  133. (CONFIG_HPS_SDR_CTRLCFG_FIFOCFG_INCSYNC <<
  134. SDR_CTRLGRP_FIFOCFG_INCSYNC_LSB),
  135. .mp_weight0 =
  136. (CONFIG_HPS_SDR_CTRLCFG_MPWIEIGHT_0_STATICWEIGHT_31_0 <<
  137. SDR_CTRLGRP_MPWEIGHT_MPWEIGHT_0_STATICWEIGHT_31_0_LSB),
  138. .mp_weight1 =
  139. (CONFIG_HPS_SDR_CTRLCFG_MPWIEIGHT_1_STATICWEIGHT_49_32 <<
  140. SDR_CTRLGRP_MPWEIGHT_MPWEIGHT_1_STATICWEIGHT_49_32_LSB) |
  141. (CONFIG_HPS_SDR_CTRLCFG_MPWIEIGHT_1_SUMOFWEIGHT_13_0 <<
  142. SDR_CTRLGRP_MPWEIGHT_MPWEIGHT_1_SUMOFWEIGHTS_13_0_LSB),
  143. .mp_weight2 =
  144. (CONFIG_HPS_SDR_CTRLCFG_MPWIEIGHT_2_SUMOFWEIGHT_45_14 <<
  145. SDR_CTRLGRP_MPWEIGHT_MPWEIGHT_2_SUMOFWEIGHTS_45_14_LSB),
  146. .mp_weight3 =
  147. (CONFIG_HPS_SDR_CTRLCFG_MPWIEIGHT_3_SUMOFWEIGHT_63_46 <<
  148. SDR_CTRLGRP_MPWEIGHT_MPWEIGHT_3_SUMOFWEIGHTS_63_46_LSB),
  149. .mp_pacing0 =
  150. (CONFIG_HPS_SDR_CTRLCFG_MPPACING_0_THRESHOLD1_31_0 <<
  151. SDR_CTRLGRP_MPPACING_MPPACING_0_THRESHOLD1_31_0_LSB),
  152. .mp_pacing1 =
  153. (CONFIG_HPS_SDR_CTRLCFG_MPPACING_1_THRESHOLD1_59_32 <<
  154. SDR_CTRLGRP_MPPACING_MPPACING_1_THRESHOLD1_59_32_LSB) |
  155. (CONFIG_HPS_SDR_CTRLCFG_MPPACING_1_THRESHOLD2_3_0 <<
  156. SDR_CTRLGRP_MPPACING_MPPACING_1_THRESHOLD2_3_0_LSB),
  157. .mp_pacing2 =
  158. (CONFIG_HPS_SDR_CTRLCFG_MPPACING_2_THRESHOLD2_35_4 <<
  159. SDR_CTRLGRP_MPPACING_MPPACING_2_THRESHOLD2_35_4_LSB),
  160. .mp_pacing3 =
  161. (CONFIG_HPS_SDR_CTRLCFG_MPPACING_3_THRESHOLD2_59_36 <<
  162. SDR_CTRLGRP_MPPACING_MPPACING_3_THRESHOLD2_59_36_LSB),
  163. .mp_threshold0 =
  164. (CONFIG_HPS_SDR_CTRLCFG_MPTHRESHOLDRST_0_THRESHOLDRSTCYCLES_31_0 <<
  165. SDR_CTRLGRP_MPTHRESHOLDRST_0_THRESHOLDRSTCYCLES_31_0_LSB),
  166. .mp_threshold1 =
  167. (CONFIG_HPS_SDR_CTRLCFG_MPTHRESHOLDRST_1_THRESHOLDRSTCYCLES_63_32 <<
  168. SDR_CTRLGRP_MPTHRESHOLDRST_1_THRESHOLDRSTCYCLES_63_32_LSB),
  169. .mp_threshold2 =
  170. (CONFIG_HPS_SDR_CTRLCFG_MPTHRESHOLDRST_2_THRESHOLDRSTCYCLES_79_64 <<
  171. SDR_CTRLGRP_MPTHRESHOLDRST_2_THRESHOLDRSTCYCLES_79_64_LSB),
  172. };
  173. /**
  174. * get_errata_rows() - Up the number of DRAM rows to cover entire address space
  175. *
  176. * SDRAM Failure happens when accessing non-existent memory. Artificially
  177. * increase the number of rows so that the memory controller thinks it has
  178. * 4GB of RAM. This function returns such amount of rows.
  179. */
  180. static int get_errata_rows(void)
  181. {
  182. /* Define constant for 4G memory - used for SDRAM errata workaround */
  183. #define MEMSIZE_4G (4ULL * 1024ULL * 1024ULL * 1024ULL)
  184. const unsigned long long memsize = MEMSIZE_4G;
  185. const unsigned int cs = CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_CSBITS;
  186. const unsigned int rows = CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_ROWBITS;
  187. const unsigned int banks = CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_BANKBITS;
  188. const unsigned int cols = CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_COLBITS;
  189. const unsigned int width = 8;
  190. unsigned long long newrows;
  191. int bits, inewrowslog2;
  192. debug("workaround rows - memsize %lld\n", memsize);
  193. debug("workaround rows - cs %d\n", cs);
  194. debug("workaround rows - width %d\n", width);
  195. debug("workaround rows - rows %d\n", rows);
  196. debug("workaround rows - banks %d\n", banks);
  197. debug("workaround rows - cols %d\n", cols);
  198. newrows = lldiv(memsize, cs * (width / 8));
  199. debug("rows workaround - term1 %lld\n", newrows);
  200. newrows = lldiv(newrows, (1 << banks) * (1 << cols));
  201. debug("rows workaround - term2 %lld\n", newrows);
  202. /*
  203. * Compute the hamming weight - same as number of bits set.
  204. * Need to see if result is ordinal power of 2 before
  205. * attempting log2 of result.
  206. */
  207. bits = generic_hweight32(newrows);
  208. debug("rows workaround - bits %d\n", bits);
  209. if (bits != 1) {
  210. printf("SDRAM workaround failed, bits set %d\n", bits);
  211. return rows;
  212. }
  213. if (newrows > UINT_MAX) {
  214. printf("SDRAM workaround rangecheck failed, %lld\n", newrows);
  215. return rows;
  216. }
  217. inewrowslog2 = __ilog2(newrows);
  218. debug("rows workaround - ilog2 %d, %lld\n", inewrowslog2, newrows);
  219. if (inewrowslog2 == -1) {
  220. printf("SDRAM workaround failed, newrows %lld\n", newrows);
  221. return rows;
  222. }
  223. return inewrowslog2;
  224. }
  225. /* SDRAM protection rules vary from 0-19, a total of 20 rules. */
  226. static void sdram_set_rule(struct sdram_prot_rule *prule)
  227. {
  228. uint32_t lo_addr_bits;
  229. uint32_t hi_addr_bits;
  230. int ruleno = prule->rule;
  231. /* Select the rule */
  232. writel(ruleno, &sdr_ctrl->prot_rule_rdwr);
  233. /* Obtain the address bits */
  234. lo_addr_bits = (uint32_t)(((prule->sdram_start) >> 20ULL) & 0xFFF);
  235. hi_addr_bits = (uint32_t)((((prule->sdram_end-1) >> 20ULL)) & 0xFFF);
  236. debug("sdram set rule start %x, %lld\n", lo_addr_bits,
  237. prule->sdram_start);
  238. debug("sdram set rule end %x, %lld\n", hi_addr_bits,
  239. prule->sdram_end);
  240. /* Set rule addresses */
  241. writel(lo_addr_bits | (hi_addr_bits << 12), &sdr_ctrl->prot_rule_addr);
  242. /* Set rule protection ids */
  243. writel(prule->lo_prot_id | (prule->hi_prot_id << 12),
  244. &sdr_ctrl->prot_rule_id);
  245. /* Set the rule data */
  246. writel(prule->security | (prule->valid << 2) |
  247. (prule->portmask << 3) | (prule->result << 13),
  248. &sdr_ctrl->prot_rule_data);
  249. /* write the rule */
  250. writel(ruleno | (1L << 5), &sdr_ctrl->prot_rule_rdwr);
  251. /* Set rule number to 0 by default */
  252. writel(0, &sdr_ctrl->prot_rule_rdwr);
  253. }
  254. static void sdram_get_rule(struct sdram_prot_rule *prule)
  255. {
  256. uint32_t addr;
  257. uint32_t id;
  258. uint32_t data;
  259. int ruleno = prule->rule;
  260. /* Read the rule */
  261. writel(ruleno, &sdr_ctrl->prot_rule_rdwr);
  262. writel(ruleno | (1L << 6), &sdr_ctrl->prot_rule_rdwr);
  263. /* Get the addresses */
  264. addr = readl(&sdr_ctrl->prot_rule_addr);
  265. prule->sdram_start = (addr & 0xFFF) << 20;
  266. prule->sdram_end = ((addr >> 12) & 0xFFF) << 20;
  267. /* Get the configured protection IDs */
  268. id = readl(&sdr_ctrl->prot_rule_id);
  269. prule->lo_prot_id = id & 0xFFF;
  270. prule->hi_prot_id = (id >> 12) & 0xFFF;
  271. /* Get protection data */
  272. data = readl(&sdr_ctrl->prot_rule_data);
  273. prule->security = data & 0x3;
  274. prule->valid = (data >> 2) & 0x1;
  275. prule->portmask = (data >> 3) & 0x3FF;
  276. prule->result = (data >> 13) & 0x1;
  277. }
  278. static void sdram_set_protection_config(uint64_t sdram_start, uint64_t sdram_end)
  279. {
  280. struct sdram_prot_rule rule;
  281. int rules;
  282. /* Start with accepting all SDRAM transaction */
  283. writel(0x0, &sdr_ctrl->protport_default);
  284. /* Clear all protection rules for warm boot case */
  285. memset(&rule, 0, sizeof(struct sdram_prot_rule));
  286. for (rules = 0; rules < 20; rules++) {
  287. rule.rule = rules;
  288. sdram_set_rule(&rule);
  289. }
  290. /* new rule: accept SDRAM */
  291. rule.sdram_start = sdram_start;
  292. rule.sdram_end = sdram_end;
  293. rule.lo_prot_id = 0x0;
  294. rule.hi_prot_id = 0xFFF;
  295. rule.portmask = 0x3FF;
  296. rule.security = 0x3;
  297. rule.result = 0;
  298. rule.valid = 1;
  299. rule.rule = 0;
  300. /* set new rule */
  301. sdram_set_rule(&rule);
  302. /* default rule: reject everything */
  303. writel(0x3ff, &sdr_ctrl->protport_default);
  304. }
  305. static void sdram_dump_protection_config(void)
  306. {
  307. struct sdram_prot_rule rule;
  308. int rules;
  309. debug("SDRAM Prot rule, default %x\n",
  310. readl(&sdr_ctrl->protport_default));
  311. for (rules = 0; rules < 20; rules++) {
  312. sdram_get_rule(&rule);
  313. debug("Rule %d, rules ...\n", rules);
  314. debug(" sdram start %llx\n", rule.sdram_start);
  315. debug(" sdram end %llx\n", rule.sdram_end);
  316. debug(" low prot id %d, hi prot id %d\n",
  317. rule.lo_prot_id,
  318. rule.hi_prot_id);
  319. debug(" portmask %x\n", rule.portmask);
  320. debug(" security %d\n", rule.security);
  321. debug(" result %d\n", rule.result);
  322. debug(" valid %d\n", rule.valid);
  323. }
  324. }
  325. /* Function to write to register and verify the write */
  326. static unsigned sdram_write_verify(unsigned int *addr, unsigned reg_value)
  327. {
  328. #ifndef SDRAM_MMR_SKIP_VERIFY
  329. unsigned reg_value1;
  330. #endif
  331. debug(" Write - Address ");
  332. debug("0x%08x Data 0x%08x\n", (u32)addr, reg_value);
  333. /* Write to register */
  334. writel(reg_value, addr);
  335. #ifndef SDRAM_MMR_SKIP_VERIFY
  336. debug(" Read and verify...");
  337. /* Read back the wrote value */
  338. reg_value1 = readl(addr);
  339. /* Indicate failure if value not matched */
  340. if (reg_value1 != reg_value) {
  341. debug("FAIL - Address 0x%08x Expected 0x%08x Data 0x%08x\n",
  342. (u32)addr, reg_value, reg_value1);
  343. return 1;
  344. }
  345. debug("correct!\n");
  346. #endif /* SDRAM_MMR_SKIP_VERIFY */
  347. return 0;
  348. }
  349. static void set_sdr_ctrlcfg(struct socfpga_sdram_config *cfg)
  350. {
  351. u32 addrorder;
  352. u32 ctrl_cfg = cfg->ctrl_cfg;
  353. debug("\nConfiguring CTRLCFG\n");
  354. /*
  355. * SDRAM Failure When Accessing Non-Existent Memory
  356. * Set the addrorder field of the SDRAM control register
  357. * based on the CSBITs setting.
  358. */
  359. switch (CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_CSBITS) {
  360. case 1:
  361. addrorder = 0; /* chip, row, bank, column */
  362. if (CONFIG_HPS_SDR_CTRLCFG_CTRLCFG_ADDRORDER != 0)
  363. debug("INFO: Changing address order to 0 (chip, row, bank, column)\n");
  364. break;
  365. case 2:
  366. addrorder = 2; /* row, chip, bank, column */
  367. if (CONFIG_HPS_SDR_CTRLCFG_CTRLCFG_ADDRORDER != 2)
  368. debug("INFO: Changing address order to 2 (row, chip, bank, column)\n");
  369. break;
  370. default:
  371. addrorder = CONFIG_HPS_SDR_CTRLCFG_CTRLCFG_ADDRORDER;
  372. break;
  373. }
  374. ctrl_cfg |= addrorder << SDR_CTRLGRP_CTRLCFG_ADDRORDER_LSB;
  375. writel(ctrl_cfg, &sdr_ctrl->ctrl_cfg);
  376. }
  377. static void set_sdr_dram_timing(struct socfpga_sdram_config *cfg)
  378. {
  379. debug("Configuring DRAMTIMING1\n");
  380. writel(cfg->dram_timing1, &sdr_ctrl->dram_timing1);
  381. debug("Configuring DRAMTIMING2\n");
  382. writel(cfg->dram_timing2, &sdr_ctrl->dram_timing2);
  383. debug("Configuring DRAMTIMING3\n");
  384. writel(cfg->dram_timing3, &sdr_ctrl->dram_timing3);
  385. debug("Configuring DRAMTIMING4\n");
  386. writel(cfg->dram_timing4, &sdr_ctrl->dram_timing4);
  387. debug("Configuring LOWPWRTIMING\n");
  388. writel(cfg->lowpwr_timing, &sdr_ctrl->lowpwr_timing);
  389. }
  390. static void set_sdr_addr_rw(struct socfpga_sdram_config *cfg)
  391. {
  392. /*
  393. * SDRAM Failure When Accessing Non-Existent Memory
  394. * Set SDR_CTRLGRP_DRAMADDRW_CSBITS_LSB to
  395. * log2(number of chip select bits). Since there's only
  396. * 1 or 2 chip selects, log2(1) => 0, and log2(2) => 1,
  397. * which is the same as "chip selects" - 1.
  398. */
  399. const int rows = get_errata_rows();
  400. debug("Configuring DRAMADDRW\n");
  401. writel(cfg->dram_addrw | (rows << SDR_CTRLGRP_DRAMADDRW_ROWBITS_LSB),
  402. &sdr_ctrl->dram_addrw);
  403. }
  404. static void set_sdr_static_cfg(struct socfpga_sdram_config *cfg)
  405. {
  406. debug("Configuring STATICCFG\n");
  407. writel(cfg->static_cfg, &sdr_ctrl->static_cfg);
  408. }
  409. static void set_sdr_fifo_cfg(struct socfpga_sdram_config *cfg)
  410. {
  411. debug("Configuring FIFOCFG\n");
  412. writel(cfg->fifo_cfg, &sdr_ctrl->fifo_cfg);
  413. }
  414. static void set_sdr_mp_weight(struct socfpga_sdram_config *cfg)
  415. {
  416. debug("Configuring MPWEIGHT_MPWEIGHT_0\n");
  417. writel(cfg->mp_weight0, &sdr_ctrl->mp_weight0);
  418. writel(cfg->mp_weight1, &sdr_ctrl->mp_weight1);
  419. writel(cfg->mp_weight2, &sdr_ctrl->mp_weight2);
  420. writel(cfg->mp_weight3, &sdr_ctrl->mp_weight3);
  421. }
  422. static void set_sdr_mp_pacing(struct socfpga_sdram_config *cfg)
  423. {
  424. debug("Configuring MPPACING_MPPACING_0\n");
  425. writel(cfg->mp_pacing0, &sdr_ctrl->mp_pacing0);
  426. writel(cfg->mp_pacing1, &sdr_ctrl->mp_pacing1);
  427. writel(cfg->mp_pacing2, &sdr_ctrl->mp_pacing2);
  428. writel(cfg->mp_pacing3, &sdr_ctrl->mp_pacing3);
  429. }
  430. static void set_sdr_mp_threshold(struct socfpga_sdram_config *cfg)
  431. {
  432. debug("Configuring MPTHRESHOLDRST_MPTHRESHOLDRST_0\n");
  433. writel(cfg->mp_threshold0, &sdr_ctrl->mp_threshold0);
  434. writel(cfg->mp_threshold1, &sdr_ctrl->mp_threshold1);
  435. writel(cfg->mp_threshold2, &sdr_ctrl->mp_threshold2);
  436. }
  437. /* Function to initialize SDRAM MMR */
  438. unsigned sdram_mmr_init_full(unsigned int sdr_phy_reg)
  439. {
  440. unsigned long status = 0;
  441. struct socfpga_sdram_config *cfg = &sdram_config;
  442. #if defined(CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_CSBITS) && \
  443. defined(CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_ROWBITS) && \
  444. defined(CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_BANKBITS) && \
  445. defined(CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_COLBITS) && \
  446. defined(CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_ROWBITS)
  447. writel(CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_ROWBITS,
  448. &sysmgr_regs->iswgrp_handoff[4]);
  449. #endif
  450. set_sdr_ctrlcfg(cfg);
  451. set_sdr_dram_timing(cfg);
  452. set_sdr_addr_rw(cfg);
  453. debug("Configuring DRAMIFWIDTH\n");
  454. clrsetbits_le32(&sdr_ctrl->dram_if_width,
  455. SDR_CTRLGRP_DRAMIFWIDTH_IFWIDTH_MASK,
  456. CONFIG_HPS_SDR_CTRLCFG_DRAMIFWIDTH_IFWIDTH <<
  457. SDR_CTRLGRP_DRAMIFWIDTH_IFWIDTH_LSB);
  458. debug("Configuring DRAMDEVWIDTH\n");
  459. clrsetbits_le32(&sdr_ctrl->dram_dev_width,
  460. SDR_CTRLGRP_DRAMDEVWIDTH_DEVWIDTH_MASK,
  461. CONFIG_HPS_SDR_CTRLCFG_DRAMDEVWIDTH_DEVWIDTH <<
  462. SDR_CTRLGRP_DRAMDEVWIDTH_DEVWIDTH_LSB);
  463. debug("Configuring LOWPWREQ\n");
  464. clrsetbits_le32(&sdr_ctrl->lowpwr_eq,
  465. SDR_CTRLGRP_LOWPWREQ_SELFRFSHMASK_MASK,
  466. CONFIG_HPS_SDR_CTRLCFG_LOWPWREQ_SELFRFSHMASK <<
  467. SDR_CTRLGRP_LOWPWREQ_SELFRFSHMASK_LSB);
  468. debug("Configuring DRAMINTR\n");
  469. clrsetbits_le32(&sdr_ctrl->dram_intr, SDR_CTRLGRP_DRAMINTR_INTREN_MASK,
  470. CONFIG_HPS_SDR_CTRLCFG_DRAMINTR_INTREN <<
  471. SDR_CTRLGRP_DRAMINTR_INTREN_LSB);
  472. set_sdr_static_cfg(cfg);
  473. debug("Configuring CTRLWIDTH\n");
  474. clrsetbits_le32(&sdr_ctrl->ctrl_width,
  475. SDR_CTRLGRP_CTRLWIDTH_CTRLWIDTH_MASK,
  476. CONFIG_HPS_SDR_CTRLCFG_CTRLWIDTH_CTRLWIDTH <<
  477. SDR_CTRLGRP_CTRLWIDTH_CTRLWIDTH_LSB);
  478. debug("Configuring PORTCFG\n");
  479. clrsetbits_le32(&sdr_ctrl->port_cfg, SDR_CTRLGRP_PORTCFG_AUTOPCHEN_MASK,
  480. CONFIG_HPS_SDR_CTRLCFG_PORTCFG_AUTOPCHEN <<
  481. SDR_CTRLGRP_PORTCFG_AUTOPCHEN_LSB);
  482. set_sdr_fifo_cfg(cfg);
  483. debug("Configuring MPPRIORITY\n");
  484. clrsetbits_le32(&sdr_ctrl->mp_priority,
  485. SDR_CTRLGRP_MPPRIORITY_USERPRIORITY_MASK,
  486. CONFIG_HPS_SDR_CTRLCFG_MPPRIORITY_USERPRIORITY <<
  487. SDR_CTRLGRP_MPPRIORITY_USERPRIORITY_LSB);
  488. set_sdr_mp_weight(cfg);
  489. set_sdr_mp_pacing(cfg);
  490. set_sdr_mp_threshold(cfg);
  491. debug("Configuring PHYCTRL_PHYCTRL_0\n");
  492. setbits_le32(&sdr_ctrl->phy_ctrl0,
  493. CONFIG_HPS_SDR_CTRLCFG_PHYCTRL_PHYCTRL_0);
  494. debug("Configuring CPORTWIDTH\n");
  495. clrsetbits_le32(&sdr_ctrl->cport_width,
  496. SDR_CTRLGRP_CPORTWIDTH_CMDPORTWIDTH_MASK,
  497. CONFIG_HPS_SDR_CTRLCFG_CPORTWIDTH_CPORTWIDTH <<
  498. SDR_CTRLGRP_CPORTWIDTH_CMDPORTWIDTH_LSB);
  499. debug("Configuring CPORTWMAP\n");
  500. clrsetbits_le32(&sdr_ctrl->cport_wmap,
  501. SDR_CTRLGRP_CPORTWMAP_CPORTWFIFOMAP_MASK,
  502. CONFIG_HPS_SDR_CTRLCFG_CPORTWMAP_CPORTWMAP <<
  503. SDR_CTRLGRP_CPORTWMAP_CPORTWFIFOMAP_LSB);
  504. debug("Configuring CPORTRMAP\n");
  505. clrsetbits_le32(&sdr_ctrl->cport_rmap,
  506. SDR_CTRLGRP_CPORTRMAP_CPORTRFIFOMAP_MASK,
  507. CONFIG_HPS_SDR_CTRLCFG_CPORTRMAP_CPORTRMAP <<
  508. SDR_CTRLGRP_CPORTRMAP_CPORTRFIFOMAP_LSB);
  509. debug("Configuring RFIFOCMAP\n");
  510. clrsetbits_le32(&sdr_ctrl->rfifo_cmap,
  511. SDR_CTRLGRP_RFIFOCMAP_RFIFOCPORTMAP_MASK,
  512. CONFIG_HPS_SDR_CTRLCFG_RFIFOCMAP_RFIFOCMAP <<
  513. SDR_CTRLGRP_RFIFOCMAP_RFIFOCPORTMAP_LSB);
  514. debug("Configuring WFIFOCMAP\n");
  515. clrsetbits_le32(&sdr_ctrl->wfifo_cmap,
  516. SDR_CTRLGRP_WFIFOCMAP_WFIFOCPORTMAP_MASK,
  517. CONFIG_HPS_SDR_CTRLCFG_WFIFOCMAP_WFIFOCMAP <<
  518. SDR_CTRLGRP_WFIFOCMAP_WFIFOCPORTMAP_LSB);
  519. debug("Configuring CPORTRDWR\n");
  520. clrsetbits_le32(&sdr_ctrl->cport_rdwr,
  521. SDR_CTRLGRP_CPORTRDWR_CPORTRDWR_MASK,
  522. CONFIG_HPS_SDR_CTRLCFG_CPORTRDWR_CPORTRDWR <<
  523. SDR_CTRLGRP_CPORTRDWR_CPORTRDWR_LSB);
  524. debug("Configuring DRAMODT\n");
  525. clrsetbits_le32(&sdr_ctrl->dram_odt,
  526. SDR_CTRLGRP_DRAMODT_READ_MASK,
  527. CONFIG_HPS_SDR_CTRLCFG_DRAMODT_READ <<
  528. SDR_CTRLGRP_DRAMODT_READ_LSB);
  529. clrsetbits_le32(&sdr_ctrl->dram_odt,
  530. SDR_CTRLGRP_DRAMODT_WRITE_MASK,
  531. CONFIG_HPS_SDR_CTRLCFG_DRAMODT_WRITE <<
  532. SDR_CTRLGRP_DRAMODT_WRITE_LSB);
  533. /* saving this value to SYSMGR.ISWGRP.HANDOFF.FPGA2SDR */
  534. writel(CONFIG_HPS_SDR_CTRLCFG_FPGAPORTRST,
  535. &sysmgr_regs->iswgrp_handoff[3]);
  536. /* only enable if the FPGA is programmed */
  537. if (fpgamgr_test_fpga_ready()) {
  538. if (sdram_write_verify(&sdr_ctrl->fpgaport_rst,
  539. CONFIG_HPS_SDR_CTRLCFG_FPGAPORTRST) == 1) {
  540. status = 1;
  541. return 1;
  542. }
  543. }
  544. /* Restore the SDR PHY Register if valid */
  545. if (sdr_phy_reg != 0xffffffff)
  546. writel(sdr_phy_reg, &sdr_ctrl->phy_ctrl0);
  547. /***** Final step - apply configuration changes *****/
  548. debug("Configuring STATICCFG_\n");
  549. clrsetbits_le32(&sdr_ctrl->static_cfg, SDR_CTRLGRP_STATICCFG_APPLYCFG_MASK,
  550. 1 << SDR_CTRLGRP_STATICCFG_APPLYCFG_LSB);
  551. sdram_set_protection_config(0, sdram_calculate_size());
  552. sdram_dump_protection_config();
  553. return status;
  554. }
  555. /*
  556. * To calculate SDRAM device size based on SDRAM controller parameters.
  557. * Size is specified in bytes.
  558. *
  559. * NOTE:
  560. * This function is compiled and linked into the preloader and
  561. * Uboot (there may be others). So if this function changes, the Preloader
  562. * and UBoot must be updated simultaneously.
  563. */
  564. unsigned long sdram_calculate_size(void)
  565. {
  566. unsigned long temp;
  567. unsigned long row, bank, col, cs, width;
  568. temp = readl(&sdr_ctrl->dram_addrw);
  569. col = (temp & SDR_CTRLGRP_DRAMADDRW_COLBITS_MASK) >>
  570. SDR_CTRLGRP_DRAMADDRW_COLBITS_LSB;
  571. /* SDRAM Failure When Accessing Non-Existent Memory
  572. * Use ROWBITS from Quartus/QSys to calculate SDRAM size
  573. * since the FB specifies we modify ROWBITs to work around SDRAM
  574. * controller issue.
  575. *
  576. * If the stored handoff value for rows is 0, it probably means
  577. * the preloader is older than UBoot. Use the
  578. * #define from the SOCEDS Tools per Crucible review
  579. * uboot-socfpga-204. Note that this is not a supported
  580. * configuration and is not tested. The customer
  581. * should be using preloader and uboot built from the
  582. * same tag.
  583. */
  584. row = readl(&sysmgr_regs->iswgrp_handoff[4]);
  585. if (row == 0)
  586. row = CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_ROWBITS;
  587. /* If the stored handoff value for rows is greater than
  588. * the field width in the sdr.dramaddrw register then
  589. * something is very wrong. Revert to using the the #define
  590. * value handed off by the SOCEDS tool chain instead of
  591. * using a broken value.
  592. */
  593. if (row > 31)
  594. row = CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_ROWBITS;
  595. bank = (temp & SDR_CTRLGRP_DRAMADDRW_BANKBITS_MASK) >>
  596. SDR_CTRLGRP_DRAMADDRW_BANKBITS_LSB;
  597. /* SDRAM Failure When Accessing Non-Existent Memory
  598. * Use CSBITs from Quartus/QSys to calculate SDRAM size
  599. * since the FB specifies we modify CSBITs to work around SDRAM
  600. * controller issue.
  601. */
  602. cs = (temp & SDR_CTRLGRP_DRAMADDRW_CSBITS_MASK) >>
  603. SDR_CTRLGRP_DRAMADDRW_CSBITS_LSB;
  604. cs += 1;
  605. cs = CONFIG_HPS_SDR_CTRLCFG_DRAMADDRW_CSBITS;
  606. width = readl(&sdr_ctrl->dram_if_width);
  607. /* ECC would not be calculated as its not addressible */
  608. if (width == SDRAM_WIDTH_32BIT_WITH_ECC)
  609. width = 32;
  610. if (width == SDRAM_WIDTH_16BIT_WITH_ECC)
  611. width = 16;
  612. /* calculate the SDRAM size base on this info */
  613. temp = 1 << (row + bank + col);
  614. temp = temp * cs * (width / 8);
  615. debug("sdram_calculate_memory returns %ld\n", temp);
  616. return temp;
  617. }