sdram.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /*
  2. * Copyright Altera Corporation (C) 2014-2015
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <errno.h>
  8. #include <div64.h>
  9. #include <watchdog.h>
  10. #include <asm/arch/fpga_manager.h>
  11. #include <asm/arch/sdram.h>
  12. #include <asm/arch/system_manager.h>
  13. #include <asm/io.h>
  14. DECLARE_GLOBAL_DATA_PTR;
  15. struct sdram_prot_rule {
  16. u64 sdram_start; /* SDRAM start address */
  17. u64 sdram_end; /* SDRAM end address */
  18. u32 rule; /* SDRAM protection rule number: 0-19 */
  19. int valid; /* Rule valid or not? 1 - valid, 0 not*/
  20. u32 security;
  21. u32 portmask;
  22. u32 result;
  23. u32 lo_prot_id;
  24. u32 hi_prot_id;
  25. };
  26. static struct socfpga_system_manager *sysmgr_regs =
  27. (struct socfpga_system_manager *)SOCFPGA_SYSMGR_ADDRESS;
  28. static struct socfpga_sdr_ctrl *sdr_ctrl =
  29. (struct socfpga_sdr_ctrl *)SDR_CTRLGRP_ADDRESS;
  30. /**
  31. * get_errata_rows() - Up the number of DRAM rows to cover entire address space
  32. * @cfg: SDRAM controller configuration data
  33. *
  34. * SDRAM Failure happens when accessing non-existent memory. Artificially
  35. * increase the number of rows so that the memory controller thinks it has
  36. * 4GB of RAM. This function returns such amount of rows.
  37. */
  38. static int get_errata_rows(const struct socfpga_sdram_config *cfg)
  39. {
  40. /* Define constant for 4G memory - used for SDRAM errata workaround */
  41. #define MEMSIZE_4G (4ULL * 1024ULL * 1024ULL * 1024ULL)
  42. const unsigned long long memsize = MEMSIZE_4G;
  43. const unsigned int cs =
  44. ((cfg->dram_addrw & SDR_CTRLGRP_DRAMADDRW_CSBITS_MASK) >>
  45. SDR_CTRLGRP_DRAMADDRW_CSBITS_LSB) + 1;
  46. const unsigned int rows =
  47. (cfg->dram_addrw & SDR_CTRLGRP_DRAMADDRW_ROWBITS_MASK) >>
  48. SDR_CTRLGRP_DRAMADDRW_ROWBITS_LSB;
  49. const unsigned int banks =
  50. (cfg->dram_addrw & SDR_CTRLGRP_DRAMADDRW_BANKBITS_MASK) >>
  51. SDR_CTRLGRP_DRAMADDRW_BANKBITS_LSB;
  52. const unsigned int cols =
  53. (cfg->dram_addrw & SDR_CTRLGRP_DRAMADDRW_COLBITS_MASK) >>
  54. SDR_CTRLGRP_DRAMADDRW_COLBITS_LSB;
  55. const unsigned int width = 8;
  56. unsigned long long newrows;
  57. int bits, inewrowslog2;
  58. debug("workaround rows - memsize %lld\n", memsize);
  59. debug("workaround rows - cs %d\n", cs);
  60. debug("workaround rows - width %d\n", width);
  61. debug("workaround rows - rows %d\n", rows);
  62. debug("workaround rows - banks %d\n", banks);
  63. debug("workaround rows - cols %d\n", cols);
  64. newrows = lldiv(memsize, cs * (width / 8));
  65. debug("rows workaround - term1 %lld\n", newrows);
  66. newrows = lldiv(newrows, (1 << banks) * (1 << cols));
  67. debug("rows workaround - term2 %lld\n", newrows);
  68. /*
  69. * Compute the hamming weight - same as number of bits set.
  70. * Need to see if result is ordinal power of 2 before
  71. * attempting log2 of result.
  72. */
  73. bits = generic_hweight32(newrows);
  74. debug("rows workaround - bits %d\n", bits);
  75. if (bits != 1) {
  76. printf("SDRAM workaround failed, bits set %d\n", bits);
  77. return rows;
  78. }
  79. if (newrows > UINT_MAX) {
  80. printf("SDRAM workaround rangecheck failed, %lld\n", newrows);
  81. return rows;
  82. }
  83. inewrowslog2 = __ilog2(newrows);
  84. debug("rows workaround - ilog2 %d, %lld\n", inewrowslog2, newrows);
  85. if (inewrowslog2 == -1) {
  86. printf("SDRAM workaround failed, newrows %lld\n", newrows);
  87. return rows;
  88. }
  89. return inewrowslog2;
  90. }
  91. /* SDRAM protection rules vary from 0-19, a total of 20 rules. */
  92. static void sdram_set_rule(struct sdram_prot_rule *prule)
  93. {
  94. uint32_t lo_addr_bits;
  95. uint32_t hi_addr_bits;
  96. int ruleno = prule->rule;
  97. /* Select the rule */
  98. writel(ruleno, &sdr_ctrl->prot_rule_rdwr);
  99. /* Obtain the address bits */
  100. lo_addr_bits = (uint32_t)(((prule->sdram_start) >> 20ULL) & 0xFFF);
  101. hi_addr_bits = (uint32_t)((((prule->sdram_end-1) >> 20ULL)) & 0xFFF);
  102. debug("sdram set rule start %x, %lld\n", lo_addr_bits,
  103. prule->sdram_start);
  104. debug("sdram set rule end %x, %lld\n", hi_addr_bits,
  105. prule->sdram_end);
  106. /* Set rule addresses */
  107. writel(lo_addr_bits | (hi_addr_bits << 12), &sdr_ctrl->prot_rule_addr);
  108. /* Set rule protection ids */
  109. writel(prule->lo_prot_id | (prule->hi_prot_id << 12),
  110. &sdr_ctrl->prot_rule_id);
  111. /* Set the rule data */
  112. writel(prule->security | (prule->valid << 2) |
  113. (prule->portmask << 3) | (prule->result << 13),
  114. &sdr_ctrl->prot_rule_data);
  115. /* write the rule */
  116. writel(ruleno | (1L << 5), &sdr_ctrl->prot_rule_rdwr);
  117. /* Set rule number to 0 by default */
  118. writel(0, &sdr_ctrl->prot_rule_rdwr);
  119. }
  120. static void sdram_get_rule(struct sdram_prot_rule *prule)
  121. {
  122. uint32_t addr;
  123. uint32_t id;
  124. uint32_t data;
  125. int ruleno = prule->rule;
  126. /* Read the rule */
  127. writel(ruleno, &sdr_ctrl->prot_rule_rdwr);
  128. writel(ruleno | (1L << 6), &sdr_ctrl->prot_rule_rdwr);
  129. /* Get the addresses */
  130. addr = readl(&sdr_ctrl->prot_rule_addr);
  131. prule->sdram_start = (addr & 0xFFF) << 20;
  132. prule->sdram_end = ((addr >> 12) & 0xFFF) << 20;
  133. /* Get the configured protection IDs */
  134. id = readl(&sdr_ctrl->prot_rule_id);
  135. prule->lo_prot_id = id & 0xFFF;
  136. prule->hi_prot_id = (id >> 12) & 0xFFF;
  137. /* Get protection data */
  138. data = readl(&sdr_ctrl->prot_rule_data);
  139. prule->security = data & 0x3;
  140. prule->valid = (data >> 2) & 0x1;
  141. prule->portmask = (data >> 3) & 0x3FF;
  142. prule->result = (data >> 13) & 0x1;
  143. }
  144. static void sdram_set_protection_config(uint64_t sdram_start, uint64_t sdram_end)
  145. {
  146. struct sdram_prot_rule rule;
  147. int rules;
  148. /* Start with accepting all SDRAM transaction */
  149. writel(0x0, &sdr_ctrl->protport_default);
  150. /* Clear all protection rules for warm boot case */
  151. memset(&rule, 0, sizeof(struct sdram_prot_rule));
  152. for (rules = 0; rules < 20; rules++) {
  153. rule.rule = rules;
  154. sdram_set_rule(&rule);
  155. }
  156. /* new rule: accept SDRAM */
  157. rule.sdram_start = sdram_start;
  158. rule.sdram_end = sdram_end;
  159. rule.lo_prot_id = 0x0;
  160. rule.hi_prot_id = 0xFFF;
  161. rule.portmask = 0x3FF;
  162. rule.security = 0x3;
  163. rule.result = 0;
  164. rule.valid = 1;
  165. rule.rule = 0;
  166. /* set new rule */
  167. sdram_set_rule(&rule);
  168. /* default rule: reject everything */
  169. writel(0x3ff, &sdr_ctrl->protport_default);
  170. }
  171. static void sdram_dump_protection_config(void)
  172. {
  173. struct sdram_prot_rule rule;
  174. int rules;
  175. debug("SDRAM Prot rule, default %x\n",
  176. readl(&sdr_ctrl->protport_default));
  177. for (rules = 0; rules < 20; rules++) {
  178. sdram_get_rule(&rule);
  179. debug("Rule %d, rules ...\n", rules);
  180. debug(" sdram start %llx\n", rule.sdram_start);
  181. debug(" sdram end %llx\n", rule.sdram_end);
  182. debug(" low prot id %d, hi prot id %d\n",
  183. rule.lo_prot_id,
  184. rule.hi_prot_id);
  185. debug(" portmask %x\n", rule.portmask);
  186. debug(" security %d\n", rule.security);
  187. debug(" result %d\n", rule.result);
  188. debug(" valid %d\n", rule.valid);
  189. }
  190. }
  191. /* Function to write to register and verify the write */
  192. static unsigned sdram_write_verify(unsigned int *addr, unsigned reg_value)
  193. {
  194. #ifndef SDRAM_MMR_SKIP_VERIFY
  195. unsigned reg_value1;
  196. #endif
  197. debug(" Write - Address ");
  198. debug("0x%08x Data 0x%08x\n", (u32)addr, reg_value);
  199. /* Write to register */
  200. writel(reg_value, addr);
  201. #ifndef SDRAM_MMR_SKIP_VERIFY
  202. debug(" Read and verify...");
  203. /* Read back the wrote value */
  204. reg_value1 = readl(addr);
  205. /* Indicate failure if value not matched */
  206. if (reg_value1 != reg_value) {
  207. debug("FAIL - Address 0x%08x Expected 0x%08x Data 0x%08x\n",
  208. (u32)addr, reg_value, reg_value1);
  209. return 1;
  210. }
  211. debug("correct!\n");
  212. #endif /* SDRAM_MMR_SKIP_VERIFY */
  213. return 0;
  214. }
  215. static u32 sdr_get_ctrlcfg(const struct socfpga_sdram_config *cfg)
  216. {
  217. const u32 csbits =
  218. ((cfg->dram_addrw & SDR_CTRLGRP_DRAMADDRW_CSBITS_MASK) >>
  219. SDR_CTRLGRP_DRAMADDRW_CSBITS_LSB) + 1;
  220. u32 addrorder =
  221. (cfg->ctrl_cfg & SDR_CTRLGRP_CTRLCFG_ADDRORDER_MASK) >>
  222. SDR_CTRLGRP_CTRLCFG_ADDRORDER_LSB;
  223. u32 ctrl_cfg = cfg->ctrl_cfg;
  224. /*
  225. * SDRAM Failure When Accessing Non-Existent Memory
  226. * Set the addrorder field of the SDRAM control register
  227. * based on the CSBITs setting.
  228. */
  229. if (csbits == 1) {
  230. if (addrorder != 0)
  231. debug("INFO: Changing address order to 0 (chip, row, bank, column)\n");
  232. addrorder = 0;
  233. } else if (csbits == 2) {
  234. if (addrorder != 2)
  235. debug("INFO: Changing address order to 2 (row, chip, bank, column)\n");
  236. addrorder = 2;
  237. }
  238. ctrl_cfg &= ~SDR_CTRLGRP_CTRLCFG_ADDRORDER_MASK;
  239. ctrl_cfg |= addrorder << SDR_CTRLGRP_CTRLCFG_ADDRORDER_LSB;
  240. return ctrl_cfg;
  241. }
  242. static u32 sdr_get_addr_rw(const struct socfpga_sdram_config *cfg)
  243. {
  244. /*
  245. * SDRAM Failure When Accessing Non-Existent Memory
  246. * Set SDR_CTRLGRP_DRAMADDRW_CSBITS_LSB to
  247. * log2(number of chip select bits). Since there's only
  248. * 1 or 2 chip selects, log2(1) => 0, and log2(2) => 1,
  249. * which is the same as "chip selects" - 1.
  250. */
  251. const int rows = get_errata_rows(cfg);
  252. u32 dram_addrw = cfg->dram_addrw & ~SDR_CTRLGRP_DRAMADDRW_ROWBITS_MASK;
  253. return dram_addrw | (rows << SDR_CTRLGRP_DRAMADDRW_ROWBITS_LSB);
  254. }
  255. /**
  256. * sdr_load_regs() - Load SDRAM controller registers
  257. * @cfg: SDRAM controller configuration data
  258. *
  259. * This function loads the register values into the SDRAM controller block.
  260. */
  261. static void sdr_load_regs(const struct socfpga_sdram_config *cfg)
  262. {
  263. const u32 ctrl_cfg = sdr_get_ctrlcfg(cfg);
  264. const u32 dram_addrw = sdr_get_addr_rw(cfg);
  265. debug("\nConfiguring CTRLCFG\n");
  266. writel(ctrl_cfg, &sdr_ctrl->ctrl_cfg);
  267. debug("Configuring DRAMTIMING1\n");
  268. writel(cfg->dram_timing1, &sdr_ctrl->dram_timing1);
  269. debug("Configuring DRAMTIMING2\n");
  270. writel(cfg->dram_timing2, &sdr_ctrl->dram_timing2);
  271. debug("Configuring DRAMTIMING3\n");
  272. writel(cfg->dram_timing3, &sdr_ctrl->dram_timing3);
  273. debug("Configuring DRAMTIMING4\n");
  274. writel(cfg->dram_timing4, &sdr_ctrl->dram_timing4);
  275. debug("Configuring LOWPWRTIMING\n");
  276. writel(cfg->lowpwr_timing, &sdr_ctrl->lowpwr_timing);
  277. debug("Configuring DRAMADDRW\n");
  278. writel(dram_addrw, &sdr_ctrl->dram_addrw);
  279. debug("Configuring DRAMIFWIDTH\n");
  280. writel(cfg->dram_if_width, &sdr_ctrl->dram_if_width);
  281. debug("Configuring DRAMDEVWIDTH\n");
  282. writel(cfg->dram_dev_width, &sdr_ctrl->dram_dev_width);
  283. debug("Configuring LOWPWREQ\n");
  284. writel(cfg->lowpwr_eq, &sdr_ctrl->lowpwr_eq);
  285. debug("Configuring DRAMINTR\n");
  286. writel(cfg->dram_intr, &sdr_ctrl->dram_intr);
  287. debug("Configuring STATICCFG\n");
  288. writel(cfg->static_cfg, &sdr_ctrl->static_cfg);
  289. debug("Configuring CTRLWIDTH\n");
  290. writel(cfg->ctrl_width, &sdr_ctrl->ctrl_width);
  291. debug("Configuring PORTCFG\n");
  292. writel(cfg->port_cfg, &sdr_ctrl->port_cfg);
  293. debug("Configuring FIFOCFG\n");
  294. writel(cfg->fifo_cfg, &sdr_ctrl->fifo_cfg);
  295. debug("Configuring MPPRIORITY\n");
  296. writel(cfg->mp_priority, &sdr_ctrl->mp_priority);
  297. debug("Configuring MPWEIGHT_MPWEIGHT_0\n");
  298. writel(cfg->mp_weight0, &sdr_ctrl->mp_weight0);
  299. writel(cfg->mp_weight1, &sdr_ctrl->mp_weight1);
  300. writel(cfg->mp_weight2, &sdr_ctrl->mp_weight2);
  301. writel(cfg->mp_weight3, &sdr_ctrl->mp_weight3);
  302. debug("Configuring MPPACING_MPPACING_0\n");
  303. writel(cfg->mp_pacing0, &sdr_ctrl->mp_pacing0);
  304. writel(cfg->mp_pacing1, &sdr_ctrl->mp_pacing1);
  305. writel(cfg->mp_pacing2, &sdr_ctrl->mp_pacing2);
  306. writel(cfg->mp_pacing3, &sdr_ctrl->mp_pacing3);
  307. debug("Configuring MPTHRESHOLDRST_MPTHRESHOLDRST_0\n");
  308. writel(cfg->mp_threshold0, &sdr_ctrl->mp_threshold0);
  309. writel(cfg->mp_threshold1, &sdr_ctrl->mp_threshold1);
  310. writel(cfg->mp_threshold2, &sdr_ctrl->mp_threshold2);
  311. debug("Configuring PHYCTRL_PHYCTRL_0\n");
  312. writel(cfg->phy_ctrl0, &sdr_ctrl->phy_ctrl0);
  313. debug("Configuring CPORTWIDTH\n");
  314. writel(cfg->cport_width, &sdr_ctrl->cport_width);
  315. debug("Configuring CPORTWMAP\n");
  316. writel(cfg->cport_wmap, &sdr_ctrl->cport_wmap);
  317. debug("Configuring CPORTRMAP\n");
  318. writel(cfg->cport_rmap, &sdr_ctrl->cport_rmap);
  319. debug("Configuring RFIFOCMAP\n");
  320. writel(cfg->rfifo_cmap, &sdr_ctrl->rfifo_cmap);
  321. debug("Configuring WFIFOCMAP\n");
  322. writel(cfg->wfifo_cmap, &sdr_ctrl->wfifo_cmap);
  323. debug("Configuring CPORTRDWR\n");
  324. writel(cfg->cport_rdwr, &sdr_ctrl->cport_rdwr);
  325. debug("Configuring DRAMODT\n");
  326. writel(cfg->dram_odt, &sdr_ctrl->dram_odt);
  327. }
  328. /**
  329. * sdram_mmr_init_full() - Function to initialize SDRAM MMR
  330. * @sdr_phy_reg: Value of the PHY control register 0
  331. *
  332. * Initialize the SDRAM MMR.
  333. */
  334. int sdram_mmr_init_full(unsigned int sdr_phy_reg)
  335. {
  336. unsigned long status = 0;
  337. const struct socfpga_sdram_config *cfg = socfpga_get_sdram_config();
  338. const unsigned int rows =
  339. (cfg->dram_addrw & SDR_CTRLGRP_DRAMADDRW_ROWBITS_MASK) >>
  340. SDR_CTRLGRP_DRAMADDRW_ROWBITS_LSB;
  341. writel(rows, &sysmgr_regs->iswgrp_handoff[4]);
  342. sdr_load_regs(cfg);
  343. /* saving this value to SYSMGR.ISWGRP.HANDOFF.FPGA2SDR */
  344. writel(cfg->fpgaport_rst, &sysmgr_regs->iswgrp_handoff[3]);
  345. /* only enable if the FPGA is programmed */
  346. if (fpgamgr_test_fpga_ready()) {
  347. if (sdram_write_verify(&sdr_ctrl->fpgaport_rst,
  348. cfg->fpgaport_rst) == 1) {
  349. status = 1;
  350. return 1;
  351. }
  352. }
  353. /* Restore the SDR PHY Register if valid */
  354. if (sdr_phy_reg != 0xffffffff)
  355. writel(sdr_phy_reg, &sdr_ctrl->phy_ctrl0);
  356. /* Final step - apply configuration changes */
  357. debug("Configuring STATICCFG\n");
  358. clrsetbits_le32(&sdr_ctrl->static_cfg,
  359. SDR_CTRLGRP_STATICCFG_APPLYCFG_MASK,
  360. 1 << SDR_CTRLGRP_STATICCFG_APPLYCFG_LSB);
  361. sdram_set_protection_config(0, sdram_calculate_size());
  362. sdram_dump_protection_config();
  363. return status;
  364. }
  365. /*
  366. * To calculate SDRAM device size based on SDRAM controller parameters.
  367. * Size is specified in bytes.
  368. *
  369. * NOTE:
  370. * This function is compiled and linked into the preloader and
  371. * Uboot (there may be others). So if this function changes, the Preloader
  372. * and UBoot must be updated simultaneously.
  373. */
  374. unsigned long sdram_calculate_size(void)
  375. {
  376. unsigned long temp;
  377. unsigned long row, bank, col, cs, width;
  378. const struct socfpga_sdram_config *cfg = socfpga_get_sdram_config();
  379. const unsigned int csbits =
  380. ((cfg->dram_addrw & SDR_CTRLGRP_DRAMADDRW_CSBITS_MASK) >>
  381. SDR_CTRLGRP_DRAMADDRW_CSBITS_LSB) + 1;
  382. const unsigned int rowbits =
  383. (cfg->dram_addrw & SDR_CTRLGRP_DRAMADDRW_ROWBITS_MASK) >>
  384. SDR_CTRLGRP_DRAMADDRW_ROWBITS_LSB;
  385. temp = readl(&sdr_ctrl->dram_addrw);
  386. col = (temp & SDR_CTRLGRP_DRAMADDRW_COLBITS_MASK) >>
  387. SDR_CTRLGRP_DRAMADDRW_COLBITS_LSB;
  388. /* SDRAM Failure When Accessing Non-Existent Memory
  389. * Use ROWBITS from Quartus/QSys to calculate SDRAM size
  390. * since the FB specifies we modify ROWBITs to work around SDRAM
  391. * controller issue.
  392. *
  393. * If the stored handoff value for rows is 0, it probably means
  394. * the preloader is older than UBoot. Use the
  395. * #define from the SOCEDS Tools per Crucible review
  396. * uboot-socfpga-204. Note that this is not a supported
  397. * configuration and is not tested. The customer
  398. * should be using preloader and uboot built from the
  399. * same tag.
  400. */
  401. row = readl(&sysmgr_regs->iswgrp_handoff[4]);
  402. if (row == 0)
  403. row = rowbits;
  404. /* If the stored handoff value for rows is greater than
  405. * the field width in the sdr.dramaddrw register then
  406. * something is very wrong. Revert to using the the #define
  407. * value handed off by the SOCEDS tool chain instead of
  408. * using a broken value.
  409. */
  410. if (row > 31)
  411. row = rowbits;
  412. bank = (temp & SDR_CTRLGRP_DRAMADDRW_BANKBITS_MASK) >>
  413. SDR_CTRLGRP_DRAMADDRW_BANKBITS_LSB;
  414. /* SDRAM Failure When Accessing Non-Existent Memory
  415. * Use CSBITs from Quartus/QSys to calculate SDRAM size
  416. * since the FB specifies we modify CSBITs to work around SDRAM
  417. * controller issue.
  418. */
  419. cs = (temp & SDR_CTRLGRP_DRAMADDRW_CSBITS_MASK) >>
  420. SDR_CTRLGRP_DRAMADDRW_CSBITS_LSB;
  421. cs += 1;
  422. cs = csbits;
  423. width = readl(&sdr_ctrl->dram_if_width);
  424. /* ECC would not be calculated as its not addressible */
  425. if (width == SDRAM_WIDTH_32BIT_WITH_ECC)
  426. width = 32;
  427. if (width == SDRAM_WIDTH_16BIT_WITH_ECC)
  428. width = 16;
  429. /* calculate the SDRAM size base on this info */
  430. temp = 1 << (row + bank + col);
  431. temp = temp * cs * (width / 8);
  432. debug("sdram_calculate_memory returns %ld\n", temp);
  433. return temp;
  434. }