sdram.c 22 KB

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