dmc_init_ddr3.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. /*
  2. * DDR3 mem setup file for board based on EXYNOS5
  3. *
  4. * Copyright (C) 2012 Samsung Electronics
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <config.h>
  9. #include <asm/io.h>
  10. #include <asm/arch/clock.h>
  11. #include <asm/arch/cpu.h>
  12. #include <asm/arch/dmc.h>
  13. #include <asm/arch/power.h>
  14. #include "common_setup.h"
  15. #include "exynos5_setup.h"
  16. #include "clock_init.h"
  17. #define TIMEOUT 10000
  18. #ifdef CONFIG_EXYNOS5250
  19. static void reset_phy_ctrl(void)
  20. {
  21. struct exynos5_clock *clk =
  22. (struct exynos5_clock *)samsung_get_base_clock();
  23. writel(DDR3PHY_CTRL_PHY_RESET_OFF, &clk->lpddr3phy_ctrl);
  24. writel(DDR3PHY_CTRL_PHY_RESET, &clk->lpddr3phy_ctrl);
  25. }
  26. int ddr3_mem_ctrl_init(struct mem_timings *mem, int reset)
  27. {
  28. unsigned int val;
  29. struct exynos5_phy_control *phy0_ctrl, *phy1_ctrl;
  30. struct exynos5_dmc *dmc;
  31. int i;
  32. phy0_ctrl = (struct exynos5_phy_control *)samsung_get_base_dmc_phy();
  33. phy1_ctrl = (struct exynos5_phy_control *)(samsung_get_base_dmc_phy()
  34. + DMC_OFFSET);
  35. dmc = (struct exynos5_dmc *)samsung_get_base_dmc_ctrl();
  36. if (reset)
  37. reset_phy_ctrl();
  38. /* Set Impedance Output Driver */
  39. val = (mem->impedance << CA_CK_DRVR_DS_OFFSET) |
  40. (mem->impedance << CA_CKE_DRVR_DS_OFFSET) |
  41. (mem->impedance << CA_CS_DRVR_DS_OFFSET) |
  42. (mem->impedance << CA_ADR_DRVR_DS_OFFSET);
  43. writel(val, &phy0_ctrl->phy_con39);
  44. writel(val, &phy1_ctrl->phy_con39);
  45. /* Set Read Latency and Burst Length for PHY0 and PHY1 */
  46. val = (mem->ctrl_bstlen << PHY_CON42_CTRL_BSTLEN_SHIFT) |
  47. (mem->ctrl_rdlat << PHY_CON42_CTRL_RDLAT_SHIFT);
  48. writel(val, &phy0_ctrl->phy_con42);
  49. writel(val, &phy1_ctrl->phy_con42);
  50. /* ZQ Calibration */
  51. if (dmc_config_zq(mem, &phy0_ctrl->phy_con16, &phy1_ctrl->phy_con16,
  52. &phy0_ctrl->phy_con17, &phy1_ctrl->phy_con17))
  53. return SETUP_ERR_ZQ_CALIBRATION_FAILURE;
  54. /* DQ Signal */
  55. writel(mem->phy0_pulld_dqs, &phy0_ctrl->phy_con14);
  56. writel(mem->phy1_pulld_dqs, &phy1_ctrl->phy_con14);
  57. writel(mem->concontrol | (mem->rd_fetch << CONCONTROL_RD_FETCH_SHIFT)
  58. | (mem->dfi_init_start << CONCONTROL_DFI_INIT_START_SHIFT),
  59. &dmc->concontrol);
  60. update_reset_dll(&dmc->phycontrol0, DDR_MODE_DDR3);
  61. /* DQS Signal */
  62. writel(mem->phy0_dqs, &phy0_ctrl->phy_con4);
  63. writel(mem->phy1_dqs, &phy1_ctrl->phy_con4);
  64. writel(mem->phy0_dq, &phy0_ctrl->phy_con6);
  65. writel(mem->phy1_dq, &phy1_ctrl->phy_con6);
  66. writel(mem->phy0_tFS, &phy0_ctrl->phy_con10);
  67. writel(mem->phy1_tFS, &phy1_ctrl->phy_con10);
  68. val = (mem->ctrl_start_point << PHY_CON12_CTRL_START_POINT_SHIFT) |
  69. (mem->ctrl_inc << PHY_CON12_CTRL_INC_SHIFT) |
  70. (mem->ctrl_dll_on << PHY_CON12_CTRL_DLL_ON_SHIFT) |
  71. (mem->ctrl_ref << PHY_CON12_CTRL_REF_SHIFT);
  72. writel(val, &phy0_ctrl->phy_con12);
  73. writel(val, &phy1_ctrl->phy_con12);
  74. /* Start DLL locking */
  75. writel(val | (mem->ctrl_start << PHY_CON12_CTRL_START_SHIFT),
  76. &phy0_ctrl->phy_con12);
  77. writel(val | (mem->ctrl_start << PHY_CON12_CTRL_START_SHIFT),
  78. &phy1_ctrl->phy_con12);
  79. update_reset_dll(&dmc->phycontrol0, DDR_MODE_DDR3);
  80. writel(mem->concontrol | (mem->rd_fetch << CONCONTROL_RD_FETCH_SHIFT),
  81. &dmc->concontrol);
  82. /* Memory Channel Inteleaving Size */
  83. writel(mem->iv_size, &dmc->ivcontrol);
  84. writel(mem->memconfig, &dmc->memconfig0);
  85. writel(mem->memconfig, &dmc->memconfig1);
  86. writel(mem->membaseconfig0, &dmc->membaseconfig0);
  87. writel(mem->membaseconfig1, &dmc->membaseconfig1);
  88. /* Precharge Configuration */
  89. writel(mem->prechconfig_tp_cnt << PRECHCONFIG_TP_CNT_SHIFT,
  90. &dmc->prechconfig);
  91. /* Power Down mode Configuration */
  92. writel(mem->dpwrdn_cyc << PWRDNCONFIG_DPWRDN_CYC_SHIFT |
  93. mem->dsref_cyc << PWRDNCONFIG_DSREF_CYC_SHIFT,
  94. &dmc->pwrdnconfig);
  95. /* TimingRow, TimingData, TimingPower and Timingaref
  96. * values as per Memory AC parameters
  97. */
  98. writel(mem->timing_ref, &dmc->timingref);
  99. writel(mem->timing_row, &dmc->timingrow);
  100. writel(mem->timing_data, &dmc->timingdata);
  101. writel(mem->timing_power, &dmc->timingpower);
  102. /* Send PALL command */
  103. dmc_config_prech(mem, &dmc->directcmd);
  104. /* Send NOP, MRS and ZQINIT commands */
  105. dmc_config_mrs(mem, &dmc->directcmd);
  106. if (mem->gate_leveling_enable) {
  107. val = PHY_CON0_RESET_VAL;
  108. val |= P0_CMD_EN;
  109. writel(val, &phy0_ctrl->phy_con0);
  110. writel(val, &phy1_ctrl->phy_con0);
  111. val = PHY_CON2_RESET_VAL;
  112. val |= INIT_DESKEW_EN;
  113. writel(val, &phy0_ctrl->phy_con2);
  114. writel(val, &phy1_ctrl->phy_con2);
  115. val = PHY_CON0_RESET_VAL;
  116. val |= P0_CMD_EN;
  117. val |= BYTE_RDLVL_EN;
  118. writel(val, &phy0_ctrl->phy_con0);
  119. writel(val, &phy1_ctrl->phy_con0);
  120. val = (mem->ctrl_start_point <<
  121. PHY_CON12_CTRL_START_POINT_SHIFT) |
  122. (mem->ctrl_inc << PHY_CON12_CTRL_INC_SHIFT) |
  123. (mem->ctrl_force << PHY_CON12_CTRL_FORCE_SHIFT) |
  124. (mem->ctrl_start << PHY_CON12_CTRL_START_SHIFT) |
  125. (mem->ctrl_ref << PHY_CON12_CTRL_REF_SHIFT);
  126. writel(val, &phy0_ctrl->phy_con12);
  127. writel(val, &phy1_ctrl->phy_con12);
  128. val = PHY_CON2_RESET_VAL;
  129. val |= INIT_DESKEW_EN;
  130. val |= RDLVL_GATE_EN;
  131. writel(val, &phy0_ctrl->phy_con2);
  132. writel(val, &phy1_ctrl->phy_con2);
  133. val = PHY_CON0_RESET_VAL;
  134. val |= P0_CMD_EN;
  135. val |= BYTE_RDLVL_EN;
  136. val |= CTRL_SHGATE;
  137. writel(val, &phy0_ctrl->phy_con0);
  138. writel(val, &phy1_ctrl->phy_con0);
  139. val = PHY_CON1_RESET_VAL;
  140. val &= ~(CTRL_GATEDURADJ_MASK);
  141. writel(val, &phy0_ctrl->phy_con1);
  142. writel(val, &phy1_ctrl->phy_con1);
  143. writel(CTRL_RDLVL_GATE_ENABLE, &dmc->rdlvl_config);
  144. i = TIMEOUT;
  145. while ((readl(&dmc->phystatus) &
  146. (RDLVL_COMPLETE_CHO | RDLVL_COMPLETE_CH1)) !=
  147. (RDLVL_COMPLETE_CHO | RDLVL_COMPLETE_CH1) && i > 0) {
  148. /*
  149. * TODO(waihong): Comment on how long this take to
  150. * timeout
  151. */
  152. sdelay(100);
  153. i--;
  154. }
  155. if (!i)
  156. return SETUP_ERR_RDLV_COMPLETE_TIMEOUT;
  157. writel(CTRL_RDLVL_GATE_DISABLE, &dmc->rdlvl_config);
  158. writel(0, &phy0_ctrl->phy_con14);
  159. writel(0, &phy1_ctrl->phy_con14);
  160. val = (mem->ctrl_start_point <<
  161. PHY_CON12_CTRL_START_POINT_SHIFT) |
  162. (mem->ctrl_inc << PHY_CON12_CTRL_INC_SHIFT) |
  163. (mem->ctrl_force << PHY_CON12_CTRL_FORCE_SHIFT) |
  164. (mem->ctrl_start << PHY_CON12_CTRL_START_SHIFT) |
  165. (mem->ctrl_dll_on << PHY_CON12_CTRL_DLL_ON_SHIFT) |
  166. (mem->ctrl_ref << PHY_CON12_CTRL_REF_SHIFT);
  167. writel(val, &phy0_ctrl->phy_con12);
  168. writel(val, &phy1_ctrl->phy_con12);
  169. update_reset_dll(&dmc->phycontrol0, DDR_MODE_DDR3);
  170. }
  171. /* Send PALL command */
  172. dmc_config_prech(mem, &dmc->directcmd);
  173. writel(mem->memcontrol, &dmc->memcontrol);
  174. /* Set DMC Concontrol and enable auto-refresh counter */
  175. writel(mem->concontrol | (mem->rd_fetch << CONCONTROL_RD_FETCH_SHIFT)
  176. | (mem->aref_en << CONCONTROL_AREF_EN_SHIFT), &dmc->concontrol);
  177. return 0;
  178. }
  179. #endif
  180. #ifdef CONFIG_EXYNOS5420
  181. int ddr3_mem_ctrl_init(struct mem_timings *mem, int reset)
  182. {
  183. struct exynos5420_clock *clk =
  184. (struct exynos5420_clock *)samsung_get_base_clock();
  185. struct exynos5420_power *power =
  186. (struct exynos5420_power *)samsung_get_base_power();
  187. struct exynos5420_phy_control *phy0_ctrl, *phy1_ctrl;
  188. struct exynos5420_dmc *drex0, *drex1;
  189. struct exynos5420_tzasc *tzasc0, *tzasc1;
  190. uint32_t val, n_lock_r, n_lock_w_phy0, n_lock_w_phy1;
  191. uint32_t lock0_info, lock1_info;
  192. int chip;
  193. int i;
  194. phy0_ctrl = (struct exynos5420_phy_control *)samsung_get_base_dmc_phy();
  195. phy1_ctrl = (struct exynos5420_phy_control *)(samsung_get_base_dmc_phy()
  196. + DMC_OFFSET);
  197. drex0 = (struct exynos5420_dmc *)samsung_get_base_dmc_ctrl();
  198. drex1 = (struct exynos5420_dmc *)(samsung_get_base_dmc_ctrl()
  199. + DMC_OFFSET);
  200. tzasc0 = (struct exynos5420_tzasc *)samsung_get_base_dmc_tzasc();
  201. tzasc1 = (struct exynos5420_tzasc *)(samsung_get_base_dmc_tzasc()
  202. + DMC_OFFSET);
  203. /* Enable PAUSE for DREX */
  204. setbits_le32(&clk->pause, ENABLE_BIT);
  205. /* Enable BYPASS mode */
  206. setbits_le32(&clk->bpll_con1, BYPASS_EN);
  207. writel(MUX_BPLL_SEL_FOUTBPLL, &clk->src_cdrex);
  208. do {
  209. val = readl(&clk->mux_stat_cdrex);
  210. val &= BPLL_SEL_MASK;
  211. } while (val != FOUTBPLL);
  212. clrbits_le32(&clk->bpll_con1, BYPASS_EN);
  213. /* Specify the DDR memory type as DDR3 */
  214. val = readl(&phy0_ctrl->phy_con0);
  215. val &= ~(PHY_CON0_CTRL_DDR_MODE_MASK << PHY_CON0_CTRL_DDR_MODE_SHIFT);
  216. val |= (DDR_MODE_DDR3 << PHY_CON0_CTRL_DDR_MODE_SHIFT);
  217. writel(val, &phy0_ctrl->phy_con0);
  218. val = readl(&phy1_ctrl->phy_con0);
  219. val &= ~(PHY_CON0_CTRL_DDR_MODE_MASK << PHY_CON0_CTRL_DDR_MODE_SHIFT);
  220. val |= (DDR_MODE_DDR3 << PHY_CON0_CTRL_DDR_MODE_SHIFT);
  221. writel(val, &phy1_ctrl->phy_con0);
  222. /* Set Read Latency and Burst Length for PHY0 and PHY1 */
  223. val = (mem->ctrl_bstlen << PHY_CON42_CTRL_BSTLEN_SHIFT) |
  224. (mem->ctrl_rdlat << PHY_CON42_CTRL_RDLAT_SHIFT);
  225. writel(val, &phy0_ctrl->phy_con42);
  226. writel(val, &phy1_ctrl->phy_con42);
  227. val = readl(&phy0_ctrl->phy_con26);
  228. val &= ~(T_WRDATA_EN_MASK << T_WRDATA_EN_OFFSET);
  229. val |= (T_WRDATA_EN_DDR3 << T_WRDATA_EN_OFFSET);
  230. writel(val, &phy0_ctrl->phy_con26);
  231. val = readl(&phy1_ctrl->phy_con26);
  232. val &= ~(T_WRDATA_EN_MASK << T_WRDATA_EN_OFFSET);
  233. val |= (T_WRDATA_EN_DDR3 << T_WRDATA_EN_OFFSET);
  234. writel(val, &phy1_ctrl->phy_con26);
  235. /*
  236. * Set Driver strength for CK, CKE, CS & CA to 0x7
  237. * Set Driver strength for Data Slice 0~3 to 0x7
  238. */
  239. val = (0x7 << CA_CK_DRVR_DS_OFFSET) | (0x7 << CA_CKE_DRVR_DS_OFFSET) |
  240. (0x7 << CA_CS_DRVR_DS_OFFSET) | (0x7 << CA_ADR_DRVR_DS_OFFSET);
  241. val |= (0x7 << DA_3_DS_OFFSET) | (0x7 << DA_2_DS_OFFSET) |
  242. (0x7 << DA_1_DS_OFFSET) | (0x7 << DA_0_DS_OFFSET);
  243. writel(val, &phy0_ctrl->phy_con39);
  244. writel(val, &phy1_ctrl->phy_con39);
  245. /* ZQ Calibration */
  246. if (dmc_config_zq(mem, &phy0_ctrl->phy_con16, &phy1_ctrl->phy_con16,
  247. &phy0_ctrl->phy_con17, &phy1_ctrl->phy_con17))
  248. return SETUP_ERR_ZQ_CALIBRATION_FAILURE;
  249. clrbits_le32(&phy0_ctrl->phy_con16, ZQ_CLK_DIV_EN);
  250. clrbits_le32(&phy1_ctrl->phy_con16, ZQ_CLK_DIV_EN);
  251. /* DQ Signal */
  252. val = readl(&phy0_ctrl->phy_con14);
  253. val |= mem->phy0_pulld_dqs;
  254. writel(val, &phy0_ctrl->phy_con14);
  255. val = readl(&phy1_ctrl->phy_con14);
  256. val |= mem->phy1_pulld_dqs;
  257. writel(val, &phy1_ctrl->phy_con14);
  258. val = MEM_TERM_EN | PHY_TERM_EN;
  259. writel(val, &drex0->phycontrol0);
  260. writel(val, &drex1->phycontrol0);
  261. writel(mem->concontrol |
  262. (mem->dfi_init_start << CONCONTROL_DFI_INIT_START_SHIFT) |
  263. (mem->rd_fetch << CONCONTROL_RD_FETCH_SHIFT),
  264. &drex0->concontrol);
  265. writel(mem->concontrol |
  266. (mem->dfi_init_start << CONCONTROL_DFI_INIT_START_SHIFT) |
  267. (mem->rd_fetch << CONCONTROL_RD_FETCH_SHIFT),
  268. &drex1->concontrol);
  269. do {
  270. val = readl(&drex0->phystatus);
  271. } while ((val & DFI_INIT_COMPLETE) != DFI_INIT_COMPLETE);
  272. do {
  273. val = readl(&drex1->phystatus);
  274. } while ((val & DFI_INIT_COMPLETE) != DFI_INIT_COMPLETE);
  275. clrbits_le32(&drex0->concontrol, DFI_INIT_START);
  276. clrbits_le32(&drex1->concontrol, DFI_INIT_START);
  277. update_reset_dll(&drex0->phycontrol0, DDR_MODE_DDR3);
  278. update_reset_dll(&drex1->phycontrol0, DDR_MODE_DDR3);
  279. /*
  280. * Set Base Address:
  281. * 0x2000_0000 ~ 0x5FFF_FFFF
  282. * 0x6000_0000 ~ 0x9FFF_FFFF
  283. */
  284. /* MEMBASECONFIG0 */
  285. val = DMC_MEMBASECONFIGX_CHIP_BASE(DMC_CHIP_BASE_0) |
  286. DMC_MEMBASECONFIGX_CHIP_MASK(DMC_CHIP_MASK);
  287. writel(val, &tzasc0->membaseconfig0);
  288. writel(val, &tzasc1->membaseconfig0);
  289. /* MEMBASECONFIG1 */
  290. val = DMC_MEMBASECONFIGX_CHIP_BASE(DMC_CHIP_BASE_1) |
  291. DMC_MEMBASECONFIGX_CHIP_MASK(DMC_CHIP_MASK);
  292. writel(val, &tzasc0->membaseconfig1);
  293. writel(val, &tzasc1->membaseconfig1);
  294. /*
  295. * Memory Channel Inteleaving Size
  296. * Ares Channel interleaving = 128 bytes
  297. */
  298. /* MEMCONFIG0/1 */
  299. writel(mem->memconfig, &tzasc0->memconfig0);
  300. writel(mem->memconfig, &tzasc1->memconfig0);
  301. writel(mem->memconfig, &tzasc0->memconfig1);
  302. writel(mem->memconfig, &tzasc1->memconfig1);
  303. /* Precharge Configuration */
  304. writel(mem->prechconfig_tp_cnt << PRECHCONFIG_TP_CNT_SHIFT,
  305. &drex0->prechconfig0);
  306. writel(mem->prechconfig_tp_cnt << PRECHCONFIG_TP_CNT_SHIFT,
  307. &drex1->prechconfig0);
  308. /*
  309. * TimingRow, TimingData, TimingPower and Timingaref
  310. * values as per Memory AC parameters
  311. */
  312. writel(mem->timing_ref, &drex0->timingref);
  313. writel(mem->timing_ref, &drex1->timingref);
  314. writel(mem->timing_row, &drex0->timingrow0);
  315. writel(mem->timing_row, &drex1->timingrow0);
  316. writel(mem->timing_data, &drex0->timingdata0);
  317. writel(mem->timing_data, &drex1->timingdata0);
  318. writel(mem->timing_power, &drex0->timingpower0);
  319. writel(mem->timing_power, &drex1->timingpower0);
  320. if (reset) {
  321. /*
  322. * Send NOP, MRS and ZQINIT commands
  323. * Sending MRS command will reset the DRAM. We should not be
  324. * reseting the DRAM after resume, this will lead to memory
  325. * corruption as DRAM content is lost after DRAM reset
  326. */
  327. dmc_config_mrs(mem, &drex0->directcmd);
  328. dmc_config_mrs(mem, &drex1->directcmd);
  329. }
  330. /*
  331. * Get PHY_CON13 from both phys. Gate CLKM around reading since
  332. * PHY_CON13 is glitchy when CLKM is running. We're paranoid and
  333. * wait until we get a "fine lock", though a coarse lock is probably
  334. * OK (we only use the coarse numbers below). We try to gate the
  335. * clock for as short a time as possible in case SDRAM is somehow
  336. * sensitive. sdelay(10) in the loop is arbitrary to make sure
  337. * there is some time for PHY_CON13 to get updated. In practice
  338. * no delay appears to be needed.
  339. */
  340. val = readl(&clk->gate_bus_cdrex);
  341. while (true) {
  342. writel(val & ~0x1, &clk->gate_bus_cdrex);
  343. lock0_info = readl(&phy0_ctrl->phy_con13);
  344. writel(val, &clk->gate_bus_cdrex);
  345. if ((lock0_info & CTRL_FINE_LOCKED) == CTRL_FINE_LOCKED)
  346. break;
  347. sdelay(10);
  348. }
  349. while (true) {
  350. writel(val & ~0x2, &clk->gate_bus_cdrex);
  351. lock1_info = readl(&phy1_ctrl->phy_con13);
  352. writel(val, &clk->gate_bus_cdrex);
  353. if ((lock1_info & CTRL_FINE_LOCKED) == CTRL_FINE_LOCKED)
  354. break;
  355. sdelay(10);
  356. }
  357. if (!reset) {
  358. /*
  359. * During Suspend-Resume & S/W-Reset, as soon as PMU releases
  360. * pad retention, CKE goes high. This causes memory contents
  361. * not to be retained during DRAM initialization. Therfore,
  362. * there is a new control register(0x100431e8[28]) which lets us
  363. * release pad retention and retain the memory content until the
  364. * initialization is complete.
  365. */
  366. writel(PAD_RETENTION_DRAM_COREBLK_VAL,
  367. &power->pad_retention_dram_coreblk_option);
  368. do {
  369. val = readl(&power->pad_retention_dram_status);
  370. } while (val != 0x1);
  371. /*
  372. * CKE PAD retention disables DRAM self-refresh mode.
  373. * Send auto refresh command for DRAM refresh.
  374. */
  375. for (i = 0; i < 128; i++) {
  376. for (chip = 0; chip < mem->chips_to_configure; chip++) {
  377. writel(DIRECT_CMD_REFA |
  378. (chip << DIRECT_CMD_CHIP_SHIFT),
  379. &drex0->directcmd);
  380. writel(DIRECT_CMD_REFA |
  381. (chip << DIRECT_CMD_CHIP_SHIFT),
  382. &drex1->directcmd);
  383. }
  384. }
  385. }
  386. if (mem->gate_leveling_enable) {
  387. writel(PHY_CON0_RESET_VAL, &phy0_ctrl->phy_con0);
  388. writel(PHY_CON0_RESET_VAL, &phy1_ctrl->phy_con0);
  389. setbits_le32(&phy0_ctrl->phy_con0, P0_CMD_EN);
  390. setbits_le32(&phy1_ctrl->phy_con0, P0_CMD_EN);
  391. val = PHY_CON2_RESET_VAL;
  392. val |= INIT_DESKEW_EN;
  393. writel(val, &phy0_ctrl->phy_con2);
  394. writel(val, &phy1_ctrl->phy_con2);
  395. val = readl(&phy0_ctrl->phy_con1);
  396. val |= (RDLVL_PASS_ADJ_VAL << RDLVL_PASS_ADJ_OFFSET);
  397. writel(val, &phy0_ctrl->phy_con1);
  398. val = readl(&phy1_ctrl->phy_con1);
  399. val |= (RDLVL_PASS_ADJ_VAL << RDLVL_PASS_ADJ_OFFSET);
  400. writel(val, &phy1_ctrl->phy_con1);
  401. n_lock_w_phy0 = (lock0_info & CTRL_LOCK_COARSE_MASK) >> 2;
  402. n_lock_r = readl(&phy0_ctrl->phy_con12);
  403. n_lock_r &= ~CTRL_DLL_ON;
  404. n_lock_r |= n_lock_w_phy0;
  405. writel(n_lock_r, &phy0_ctrl->phy_con12);
  406. n_lock_w_phy1 = (lock1_info & CTRL_LOCK_COARSE_MASK) >> 2;
  407. n_lock_r = readl(&phy1_ctrl->phy_con12);
  408. n_lock_r &= ~CTRL_DLL_ON;
  409. n_lock_r |= n_lock_w_phy1;
  410. writel(n_lock_r, &phy1_ctrl->phy_con12);
  411. val = (0x3 << DIRECT_CMD_BANK_SHIFT) | 0x4;
  412. for (chip = 0; chip < mem->chips_to_configure; chip++) {
  413. writel(val | (chip << DIRECT_CMD_CHIP_SHIFT),
  414. &drex0->directcmd);
  415. writel(val | (chip << DIRECT_CMD_CHIP_SHIFT),
  416. &drex1->directcmd);
  417. }
  418. setbits_le32(&phy0_ctrl->phy_con2, RDLVL_GATE_EN);
  419. setbits_le32(&phy1_ctrl->phy_con2, RDLVL_GATE_EN);
  420. setbits_le32(&phy0_ctrl->phy_con0, CTRL_SHGATE);
  421. setbits_le32(&phy1_ctrl->phy_con0, CTRL_SHGATE);
  422. val = readl(&phy0_ctrl->phy_con1);
  423. val &= ~(CTRL_GATEDURADJ_MASK);
  424. writel(val, &phy0_ctrl->phy_con1);
  425. val = readl(&phy1_ctrl->phy_con1);
  426. val &= ~(CTRL_GATEDURADJ_MASK);
  427. writel(val, &phy1_ctrl->phy_con1);
  428. writel(CTRL_RDLVL_GATE_ENABLE, &drex0->rdlvl_config);
  429. i = TIMEOUT;
  430. while (((readl(&drex0->phystatus) & RDLVL_COMPLETE_CHO) !=
  431. RDLVL_COMPLETE_CHO) && (i > 0)) {
  432. /*
  433. * TODO(waihong): Comment on how long this take to
  434. * timeout
  435. */
  436. sdelay(100);
  437. i--;
  438. }
  439. if (!i)
  440. return SETUP_ERR_RDLV_COMPLETE_TIMEOUT;
  441. writel(CTRL_RDLVL_GATE_DISABLE, &drex0->rdlvl_config);
  442. writel(CTRL_RDLVL_GATE_ENABLE, &drex1->rdlvl_config);
  443. i = TIMEOUT;
  444. while (((readl(&drex1->phystatus) & RDLVL_COMPLETE_CHO) !=
  445. RDLVL_COMPLETE_CHO) && (i > 0)) {
  446. /*
  447. * TODO(waihong): Comment on how long this take to
  448. * timeout
  449. */
  450. sdelay(100);
  451. i--;
  452. }
  453. if (!i)
  454. return SETUP_ERR_RDLV_COMPLETE_TIMEOUT;
  455. writel(CTRL_RDLVL_GATE_DISABLE, &drex1->rdlvl_config);
  456. writel(0, &phy0_ctrl->phy_con14);
  457. writel(0, &phy1_ctrl->phy_con14);
  458. val = (0x3 << DIRECT_CMD_BANK_SHIFT);
  459. for (chip = 0; chip < mem->chips_to_configure; chip++) {
  460. writel(val | (chip << DIRECT_CMD_CHIP_SHIFT),
  461. &drex0->directcmd);
  462. writel(val | (chip << DIRECT_CMD_CHIP_SHIFT),
  463. &drex1->directcmd);
  464. }
  465. /* Common Settings for Leveling */
  466. val = PHY_CON12_RESET_VAL;
  467. writel((val + n_lock_w_phy0), &phy0_ctrl->phy_con12);
  468. writel((val + n_lock_w_phy1), &phy1_ctrl->phy_con12);
  469. setbits_le32(&phy0_ctrl->phy_con2, DLL_DESKEW_EN);
  470. setbits_le32(&phy1_ctrl->phy_con2, DLL_DESKEW_EN);
  471. }
  472. /* Send PALL command */
  473. dmc_config_prech(mem, &drex0->directcmd);
  474. dmc_config_prech(mem, &drex1->directcmd);
  475. writel(mem->memcontrol, &drex0->memcontrol);
  476. writel(mem->memcontrol, &drex1->memcontrol);
  477. /*
  478. * Set DMC Concontrol: Enable auto-refresh counter, provide
  479. * read data fetch cycles and enable DREX auto set powerdown
  480. * for input buffer of I/O in none read memory state.
  481. */
  482. writel(mem->concontrol | (mem->aref_en << CONCONTROL_AREF_EN_SHIFT) |
  483. (mem->rd_fetch << CONCONTROL_RD_FETCH_SHIFT)|
  484. DMC_CONCONTROL_IO_PD_CON(0x2),
  485. &drex0->concontrol);
  486. writel(mem->concontrol | (mem->aref_en << CONCONTROL_AREF_EN_SHIFT) |
  487. (mem->rd_fetch << CONCONTROL_RD_FETCH_SHIFT)|
  488. DMC_CONCONTROL_IO_PD_CON(0x2),
  489. &drex1->concontrol);
  490. /*
  491. * Enable Clock Gating Control for DMC
  492. * this saves around 25 mw dmc power as compared to the power
  493. * consumption without these bits enabled
  494. */
  495. setbits_le32(&drex0->cgcontrol, DMC_INTERNAL_CG);
  496. setbits_le32(&drex1->cgcontrol, DMC_INTERNAL_CG);
  497. return 0;
  498. }
  499. #endif