sdram.c 24 KB

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