tegra210-emc-cc-r21021.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2014-2020, NVIDIA CORPORATION. All rights reserved.
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/io.h>
  7. #include <linux/clk.h>
  8. #include <linux/delay.h>
  9. #include <linux/of.h>
  10. #include <soc/tegra/mc.h>
  11. #include "tegra210-emc.h"
  12. #include "tegra210-mc.h"
  13. /*
  14. * Enable flags for specifying verbosity.
  15. */
  16. #define INFO (1 << 0)
  17. #define STEPS (1 << 1)
  18. #define SUB_STEPS (1 << 2)
  19. #define PRELOCK (1 << 3)
  20. #define PRELOCK_STEPS (1 << 4)
  21. #define ACTIVE_EN (1 << 5)
  22. #define PRAMP_UP (1 << 6)
  23. #define PRAMP_DN (1 << 7)
  24. #define EMA_WRITES (1 << 10)
  25. #define EMA_UPDATES (1 << 11)
  26. #define PER_TRAIN (1 << 16)
  27. #define CC_PRINT (1 << 17)
  28. #define CCFIFO (1 << 29)
  29. #define REGS (1 << 30)
  30. #define REG_LISTS (1 << 31)
  31. #define emc_dbg(emc, flags, ...) dev_dbg(emc->dev, __VA_ARGS__)
  32. #define DVFS_CLOCK_CHANGE_VERSION 21021
  33. #define EMC_PRELOCK_VERSION 2101
  34. enum {
  35. DVFS_SEQUENCE = 1,
  36. WRITE_TRAINING_SEQUENCE = 2,
  37. PERIODIC_TRAINING_SEQUENCE = 3,
  38. DVFS_PT1 = 10,
  39. DVFS_UPDATE = 11,
  40. TRAINING_PT1 = 12,
  41. TRAINING_UPDATE = 13,
  42. PERIODIC_TRAINING_UPDATE = 14
  43. };
  44. /*
  45. * PTFV defines - basically just indexes into the per table PTFV array.
  46. */
  47. #define PTFV_DQSOSC_MOVAVG_C0D0U0_INDEX 0
  48. #define PTFV_DQSOSC_MOVAVG_C0D0U1_INDEX 1
  49. #define PTFV_DQSOSC_MOVAVG_C0D1U0_INDEX 2
  50. #define PTFV_DQSOSC_MOVAVG_C0D1U1_INDEX 3
  51. #define PTFV_DQSOSC_MOVAVG_C1D0U0_INDEX 4
  52. #define PTFV_DQSOSC_MOVAVG_C1D0U1_INDEX 5
  53. #define PTFV_DQSOSC_MOVAVG_C1D1U0_INDEX 6
  54. #define PTFV_DQSOSC_MOVAVG_C1D1U1_INDEX 7
  55. #define PTFV_DVFS_SAMPLES_INDEX 9
  56. #define PTFV_MOVAVG_WEIGHT_INDEX 10
  57. #define PTFV_CONFIG_CTRL_INDEX 11
  58. #define PTFV_CONFIG_CTRL_USE_PREVIOUS_EMA (1 << 0)
  59. /*
  60. * Do arithmetic in fixed point.
  61. */
  62. #define MOVAVG_PRECISION_FACTOR 100
  63. /*
  64. * The division portion of the average operation.
  65. */
  66. #define __AVERAGE_PTFV(dev) \
  67. ({ next->ptfv_list[(dev)] = \
  68. next->ptfv_list[(dev)] / \
  69. next->ptfv_list[PTFV_DVFS_SAMPLES_INDEX]; })
  70. /*
  71. * Convert val to fixed point and add it to the temporary average.
  72. */
  73. #define __INCREMENT_PTFV(dev, val) \
  74. ({ next->ptfv_list[(dev)] += \
  75. ((val) * MOVAVG_PRECISION_FACTOR); })
  76. /*
  77. * Convert a moving average back to integral form and return the value.
  78. */
  79. #define __MOVAVG_AC(timing, dev) \
  80. ((timing)->ptfv_list[(dev)] / \
  81. MOVAVG_PRECISION_FACTOR)
  82. /* Weighted update. */
  83. #define __WEIGHTED_UPDATE_PTFV(dev, nval) \
  84. do { \
  85. int w = PTFV_MOVAVG_WEIGHT_INDEX; \
  86. int dqs = (dev); \
  87. \
  88. next->ptfv_list[dqs] = \
  89. ((nval * MOVAVG_PRECISION_FACTOR) + \
  90. (next->ptfv_list[dqs] * \
  91. next->ptfv_list[w])) / \
  92. (next->ptfv_list[w] + 1); \
  93. \
  94. emc_dbg(emc, EMA_UPDATES, "%s: (s=%u) EMA: %u\n", \
  95. __stringify(dev), nval, next->ptfv_list[dqs]); \
  96. } while (0)
  97. /* Access a particular average. */
  98. #define __MOVAVG(timing, dev) \
  99. ((timing)->ptfv_list[(dev)])
  100. static bool tegra210_emc_compare_update_delay(struct tegra210_emc_timing *timing,
  101. u32 measured, u32 idx)
  102. {
  103. u32 *curr = &timing->current_dram_clktree[idx];
  104. u32 rate_mhz = timing->rate / 1000;
  105. u32 tmdel;
  106. tmdel = abs(*curr - measured);
  107. if (tmdel * 128 * rate_mhz / 1000000 > timing->tree_margin) {
  108. *curr = measured;
  109. return true;
  110. }
  111. return false;
  112. }
  113. static void tegra210_emc_get_clktree_delay(struct tegra210_emc *emc,
  114. u32 delay[DRAM_CLKTREE_NUM])
  115. {
  116. struct tegra210_emc_timing *curr = emc->last;
  117. u32 rate_mhz = curr->rate / 1000;
  118. u32 msb, lsb, dqsosc, delay_us;
  119. unsigned int c, d, idx;
  120. unsigned long clocks;
  121. clocks = tegra210_emc_actual_osc_clocks(curr->run_clocks);
  122. delay_us = 2 + (clocks / rate_mhz);
  123. tegra210_emc_start_periodic_compensation(emc);
  124. udelay(delay_us);
  125. for (d = 0; d < emc->num_devices; d++) {
  126. /* Read DQSOSC from MRR18/19 */
  127. msb = tegra210_emc_mrr_read(emc, 2 - d, 19);
  128. lsb = tegra210_emc_mrr_read(emc, 2 - d, 18);
  129. for (c = 0; c < emc->num_channels; c++) {
  130. /* C[c]D[d]U[0] */
  131. idx = c * 4 + d * 2;
  132. dqsosc = (msb & 0x00ff) << 8;
  133. dqsosc |= (lsb & 0x00ff) >> 0;
  134. /* Check for unpopulated channels */
  135. if (dqsosc)
  136. delay[idx] = (clocks * 1000000) /
  137. (rate_mhz * 2 * dqsosc);
  138. /* C[c]D[d]U[1] */
  139. idx++;
  140. dqsosc = (msb & 0xff00) << 0;
  141. dqsosc |= (lsb & 0xff00) >> 8;
  142. /* Check for unpopulated channels */
  143. if (dqsosc)
  144. delay[idx] = (clocks * 1000000) /
  145. (rate_mhz * 2 * dqsosc);
  146. msb >>= 16;
  147. lsb >>= 16;
  148. }
  149. }
  150. }
  151. static bool periodic_compensation_handler(struct tegra210_emc *emc, u32 type,
  152. struct tegra210_emc_timing *last,
  153. struct tegra210_emc_timing *next)
  154. {
  155. #define __COPY_EMA(nt, lt, dev) \
  156. ({ __MOVAVG(nt, dev) = __MOVAVG(lt, dev) * \
  157. (nt)->ptfv_list[PTFV_DVFS_SAMPLES_INDEX]; })
  158. u32 i, samples = next->ptfv_list[PTFV_DVFS_SAMPLES_INDEX];
  159. u32 delay[DRAM_CLKTREE_NUM], idx;
  160. bool over = false;
  161. if (!next->periodic_training)
  162. return 0;
  163. if (type == DVFS_SEQUENCE) {
  164. if (last->periodic_training &&
  165. (next->ptfv_list[PTFV_CONFIG_CTRL_INDEX] &
  166. PTFV_CONFIG_CTRL_USE_PREVIOUS_EMA)) {
  167. /*
  168. * If the previous frequency was using periodic
  169. * calibration then we can reuse the previous
  170. * frequencies EMA data.
  171. */
  172. for (idx = 0; idx < DRAM_CLKTREE_NUM; idx++)
  173. __COPY_EMA(next, last, idx);
  174. } else {
  175. /* Reset the EMA.*/
  176. for (idx = 0; idx < DRAM_CLKTREE_NUM; idx++)
  177. __MOVAVG(next, idx) = 0;
  178. for (i = 0; i < samples; i++) {
  179. /* Generate next sample of data. */
  180. tegra210_emc_get_clktree_delay(emc, delay);
  181. for (idx = 0; idx < DRAM_CLKTREE_NUM; idx++)
  182. __INCREMENT_PTFV(idx, delay[idx]);
  183. }
  184. }
  185. for (idx = 0; idx < DRAM_CLKTREE_NUM; idx++) {
  186. /* Do the division part of the moving average */
  187. __AVERAGE_PTFV(idx);
  188. over |= tegra210_emc_compare_update_delay(next,
  189. __MOVAVG_AC(next, idx), idx);
  190. }
  191. }
  192. if (type == PERIODIC_TRAINING_SEQUENCE) {
  193. tegra210_emc_get_clktree_delay(emc, delay);
  194. for (idx = 0; idx < DRAM_CLKTREE_NUM; idx++) {
  195. __WEIGHTED_UPDATE_PTFV(idx, delay[idx]);
  196. over |= tegra210_emc_compare_update_delay(next,
  197. __MOVAVG_AC(next, idx), idx);
  198. }
  199. }
  200. return over;
  201. }
  202. static u32 tegra210_emc_r21021_periodic_compensation(struct tegra210_emc *emc)
  203. {
  204. u32 emc_cfg, emc_cfg_o, emc_cfg_update, value;
  205. static const u32 list[] = {
  206. EMC_PMACRO_OB_DDLL_LONG_DQ_RANK0_0,
  207. EMC_PMACRO_OB_DDLL_LONG_DQ_RANK0_1,
  208. EMC_PMACRO_OB_DDLL_LONG_DQ_RANK0_2,
  209. EMC_PMACRO_OB_DDLL_LONG_DQ_RANK0_3,
  210. EMC_PMACRO_OB_DDLL_LONG_DQ_RANK1_0,
  211. EMC_PMACRO_OB_DDLL_LONG_DQ_RANK1_1,
  212. EMC_PMACRO_OB_DDLL_LONG_DQ_RANK1_2,
  213. EMC_PMACRO_OB_DDLL_LONG_DQ_RANK1_3,
  214. EMC_DATA_BRLSHFT_0,
  215. EMC_DATA_BRLSHFT_1
  216. };
  217. struct tegra210_emc_timing *last = emc->last;
  218. unsigned int items = ARRAY_SIZE(list), i;
  219. if (last->periodic_training) {
  220. emc_dbg(emc, PER_TRAIN, "Periodic training starting\n");
  221. value = emc_readl(emc, EMC_DBG);
  222. emc_cfg_o = emc_readl(emc, EMC_CFG);
  223. emc_cfg = emc_cfg_o & ~(EMC_CFG_DYN_SELF_REF |
  224. EMC_CFG_DRAM_ACPD |
  225. EMC_CFG_DRAM_CLKSTOP_PD);
  226. /*
  227. * 1. Power optimizations should be off.
  228. */
  229. emc_writel(emc, emc_cfg, EMC_CFG);
  230. /* Does emc_timing_update() for above changes. */
  231. tegra210_emc_dll_disable(emc);
  232. for (i = 0; i < emc->num_channels; i++)
  233. tegra210_emc_wait_for_update(emc, i, EMC_EMC_STATUS,
  234. EMC_EMC_STATUS_DRAM_IN_POWERDOWN_MASK,
  235. 0);
  236. for (i = 0; i < emc->num_channels; i++)
  237. tegra210_emc_wait_for_update(emc, i, EMC_EMC_STATUS,
  238. EMC_EMC_STATUS_DRAM_IN_SELF_REFRESH_MASK,
  239. 0);
  240. emc_cfg_update = value = emc_readl(emc, EMC_CFG_UPDATE);
  241. value &= ~EMC_CFG_UPDATE_UPDATE_DLL_IN_UPDATE_MASK;
  242. value |= (2 << EMC_CFG_UPDATE_UPDATE_DLL_IN_UPDATE_SHIFT);
  243. emc_writel(emc, value, EMC_CFG_UPDATE);
  244. /*
  245. * 2. osc kick off - this assumes training and dvfs have set
  246. * correct MR23.
  247. *
  248. * 3. Let dram capture its clock tree delays.
  249. *
  250. * 4. Check delta wrt previous values (save value if margin
  251. * exceeds what is set in table).
  252. */
  253. if (periodic_compensation_handler(emc, PERIODIC_TRAINING_SEQUENCE,
  254. last, last)) {
  255. /*
  256. * 5. Apply compensation w.r.t. trained values (if clock tree
  257. * has drifted more than the set margin).
  258. */
  259. for (i = 0; i < items; i++) {
  260. value = tegra210_emc_compensate(last, list[i]);
  261. emc_dbg(emc, EMA_WRITES, "0x%08x <= 0x%08x\n",
  262. list[i], value);
  263. emc_writel(emc, value, list[i]);
  264. }
  265. }
  266. emc_writel(emc, emc_cfg_o, EMC_CFG);
  267. /*
  268. * 6. Timing update actally applies the new trimmers.
  269. */
  270. tegra210_emc_timing_update(emc);
  271. /* 6.1. Restore the UPDATE_DLL_IN_UPDATE field. */
  272. emc_writel(emc, emc_cfg_update, EMC_CFG_UPDATE);
  273. /* 6.2. Restore the DLL. */
  274. tegra210_emc_dll_enable(emc);
  275. }
  276. return 0;
  277. }
  278. /*
  279. * Do the clock change sequence.
  280. */
  281. static void tegra210_emc_r21021_set_clock(struct tegra210_emc *emc, u32 clksrc)
  282. {
  283. /* state variables */
  284. static bool fsp_for_next_freq;
  285. /* constant configuration parameters */
  286. const bool save_restore_clkstop_pd = true;
  287. const u32 zqcal_before_cc_cutoff = 2400;
  288. const bool cya_allow_ref_cc = false;
  289. const bool cya_issue_pc_ref = false;
  290. const bool opt_cc_short_zcal = true;
  291. const bool ref_b4_sref_en = false;
  292. const u32 tZQCAL_lpddr4 = 1000000;
  293. const bool opt_short_zcal = true;
  294. const bool opt_do_sw_qrst = true;
  295. const u32 opt_dvfs_mode = MAN_SR;
  296. /*
  297. * This is the timing table for the source frequency. It does _not_
  298. * necessarily correspond to the actual timing values in the EMC at the
  299. * moment. If the boot BCT differs from the table then this can happen.
  300. * However, we need it for accessing the dram_timings (which are not
  301. * really registers) array for the current frequency.
  302. */
  303. struct tegra210_emc_timing *fake, *last = emc->last, *next = emc->next;
  304. u32 tRTM, RP_war, R2P_war, TRPab_war, deltaTWATM, W2P_war, tRPST;
  305. u32 mr13_flip_fspwr, mr13_flip_fspop, ramp_up_wait, ramp_down_wait;
  306. u32 zq_wait_long, zq_latch_dvfs_wait_time, tZQCAL_lpddr4_fc_adj;
  307. u32 emc_auto_cal_config, auto_cal_en, emc_cfg, emc_sel_dpd_ctrl;
  308. u32 tFC_lpddr4 = 1000 * next->dram_timings[T_FC_LPDDR4];
  309. u32 bg_reg_mode_change, enable_bglp_reg, enable_bg_reg;
  310. bool opt_zcal_en_cc = false, is_lpddr3 = false;
  311. bool compensate_trimmer_applicable = false;
  312. u32 emc_dbg, emc_cfg_pipe_clk, emc_pin;
  313. u32 src_clk_period, dst_clk_period; /* in picoseconds */
  314. bool shared_zq_resistor = false;
  315. u32 value, dram_type;
  316. u32 opt_dll_mode = 0;
  317. unsigned long delay;
  318. unsigned int i;
  319. emc_dbg(emc, INFO, "Running clock change.\n");
  320. /* XXX fake == last */
  321. fake = tegra210_emc_find_timing(emc, last->rate * 1000UL);
  322. fsp_for_next_freq = !fsp_for_next_freq;
  323. value = emc_readl(emc, EMC_FBIO_CFG5) & EMC_FBIO_CFG5_DRAM_TYPE_MASK;
  324. dram_type = value >> EMC_FBIO_CFG5_DRAM_TYPE_SHIFT;
  325. if (last->burst_regs[EMC_ZCAL_WAIT_CNT_INDEX] & BIT(31))
  326. shared_zq_resistor = true;
  327. if ((next->burst_regs[EMC_ZCAL_INTERVAL_INDEX] != 0 &&
  328. last->burst_regs[EMC_ZCAL_INTERVAL_INDEX] == 0) ||
  329. dram_type == DRAM_TYPE_LPDDR4)
  330. opt_zcal_en_cc = true;
  331. if (dram_type == DRAM_TYPE_DDR3)
  332. opt_dll_mode = tegra210_emc_get_dll_state(next);
  333. if ((next->burst_regs[EMC_FBIO_CFG5_INDEX] & BIT(25)) &&
  334. (dram_type == DRAM_TYPE_LPDDR2))
  335. is_lpddr3 = true;
  336. emc_readl(emc, EMC_CFG);
  337. emc_readl(emc, EMC_AUTO_CAL_CONFIG);
  338. src_clk_period = 1000000000 / last->rate;
  339. dst_clk_period = 1000000000 / next->rate;
  340. if (dst_clk_period <= zqcal_before_cc_cutoff)
  341. tZQCAL_lpddr4_fc_adj = tZQCAL_lpddr4 - tFC_lpddr4;
  342. else
  343. tZQCAL_lpddr4_fc_adj = tZQCAL_lpddr4;
  344. tZQCAL_lpddr4_fc_adj /= dst_clk_period;
  345. emc_dbg = emc_readl(emc, EMC_DBG);
  346. emc_pin = emc_readl(emc, EMC_PIN);
  347. emc_cfg_pipe_clk = emc_readl(emc, EMC_CFG_PIPE_CLK);
  348. emc_cfg = next->burst_regs[EMC_CFG_INDEX];
  349. emc_cfg &= ~(EMC_CFG_DYN_SELF_REF | EMC_CFG_DRAM_ACPD |
  350. EMC_CFG_DRAM_CLKSTOP_SR | EMC_CFG_DRAM_CLKSTOP_PD);
  351. emc_sel_dpd_ctrl = next->emc_sel_dpd_ctrl;
  352. emc_sel_dpd_ctrl &= ~(EMC_SEL_DPD_CTRL_CLK_SEL_DPD_EN |
  353. EMC_SEL_DPD_CTRL_CA_SEL_DPD_EN |
  354. EMC_SEL_DPD_CTRL_RESET_SEL_DPD_EN |
  355. EMC_SEL_DPD_CTRL_ODT_SEL_DPD_EN |
  356. EMC_SEL_DPD_CTRL_DATA_SEL_DPD_EN);
  357. emc_dbg(emc, INFO, "Clock change version: %d\n",
  358. DVFS_CLOCK_CHANGE_VERSION);
  359. emc_dbg(emc, INFO, "DRAM type = %d\n", dram_type);
  360. emc_dbg(emc, INFO, "DRAM dev #: %u\n", emc->num_devices);
  361. emc_dbg(emc, INFO, "Next EMC clksrc: 0x%08x\n", clksrc);
  362. emc_dbg(emc, INFO, "DLL clksrc: 0x%08x\n", next->dll_clk_src);
  363. emc_dbg(emc, INFO, "last rate: %u, next rate %u\n", last->rate,
  364. next->rate);
  365. emc_dbg(emc, INFO, "last period: %u, next period: %u\n",
  366. src_clk_period, dst_clk_period);
  367. emc_dbg(emc, INFO, " shared_zq_resistor: %d\n", !!shared_zq_resistor);
  368. emc_dbg(emc, INFO, " num_channels: %u\n", emc->num_channels);
  369. emc_dbg(emc, INFO, " opt_dll_mode: %d\n", opt_dll_mode);
  370. /*
  371. * Step 1:
  372. * Pre DVFS SW sequence.
  373. */
  374. emc_dbg(emc, STEPS, "Step 1\n");
  375. emc_dbg(emc, STEPS, "Step 1.1: Disable DLL temporarily.\n");
  376. value = emc_readl(emc, EMC_CFG_DIG_DLL);
  377. value &= ~EMC_CFG_DIG_DLL_CFG_DLL_EN;
  378. emc_writel(emc, value, EMC_CFG_DIG_DLL);
  379. tegra210_emc_timing_update(emc);
  380. for (i = 0; i < emc->num_channels; i++)
  381. tegra210_emc_wait_for_update(emc, i, EMC_CFG_DIG_DLL,
  382. EMC_CFG_DIG_DLL_CFG_DLL_EN, 0);
  383. emc_dbg(emc, STEPS, "Step 1.2: Disable AUTOCAL temporarily.\n");
  384. emc_auto_cal_config = next->emc_auto_cal_config;
  385. auto_cal_en = emc_auto_cal_config & EMC_AUTO_CAL_CONFIG_AUTO_CAL_ENABLE;
  386. emc_auto_cal_config &= ~EMC_AUTO_CAL_CONFIG_AUTO_CAL_START;
  387. emc_auto_cal_config |= EMC_AUTO_CAL_CONFIG_AUTO_CAL_MEASURE_STALL;
  388. emc_auto_cal_config |= EMC_AUTO_CAL_CONFIG_AUTO_CAL_UPDATE_STALL;
  389. emc_auto_cal_config |= auto_cal_en;
  390. emc_writel(emc, emc_auto_cal_config, EMC_AUTO_CAL_CONFIG);
  391. emc_readl(emc, EMC_AUTO_CAL_CONFIG); /* Flush write. */
  392. emc_dbg(emc, STEPS, "Step 1.3: Disable other power features.\n");
  393. tegra210_emc_set_shadow_bypass(emc, ACTIVE);
  394. emc_writel(emc, emc_cfg, EMC_CFG);
  395. emc_writel(emc, emc_sel_dpd_ctrl, EMC_SEL_DPD_CTRL);
  396. tegra210_emc_set_shadow_bypass(emc, ASSEMBLY);
  397. if (next->periodic_training) {
  398. tegra210_emc_reset_dram_clktree_values(next);
  399. for (i = 0; i < emc->num_channels; i++)
  400. tegra210_emc_wait_for_update(emc, i, EMC_EMC_STATUS,
  401. EMC_EMC_STATUS_DRAM_IN_POWERDOWN_MASK,
  402. 0);
  403. for (i = 0; i < emc->num_channels; i++)
  404. tegra210_emc_wait_for_update(emc, i, EMC_EMC_STATUS,
  405. EMC_EMC_STATUS_DRAM_IN_SELF_REFRESH_MASK,
  406. 0);
  407. if (periodic_compensation_handler(emc, DVFS_SEQUENCE, fake, next))
  408. compensate_trimmer_applicable = true;
  409. }
  410. emc_writel(emc, EMC_INTSTATUS_CLKCHANGE_COMPLETE, EMC_INTSTATUS);
  411. tegra210_emc_set_shadow_bypass(emc, ACTIVE);
  412. emc_writel(emc, emc_cfg, EMC_CFG);
  413. emc_writel(emc, emc_sel_dpd_ctrl, EMC_SEL_DPD_CTRL);
  414. emc_writel(emc, emc_cfg_pipe_clk | EMC_CFG_PIPE_CLK_CLK_ALWAYS_ON,
  415. EMC_CFG_PIPE_CLK);
  416. emc_writel(emc, next->emc_fdpd_ctrl_cmd_no_ramp &
  417. ~EMC_FDPD_CTRL_CMD_NO_RAMP_CMD_DPD_NO_RAMP_ENABLE,
  418. EMC_FDPD_CTRL_CMD_NO_RAMP);
  419. bg_reg_mode_change =
  420. ((next->burst_regs[EMC_PMACRO_BG_BIAS_CTRL_0_INDEX] &
  421. EMC_PMACRO_BG_BIAS_CTRL_0_BGLP_E_PWRD) ^
  422. (last->burst_regs[EMC_PMACRO_BG_BIAS_CTRL_0_INDEX] &
  423. EMC_PMACRO_BG_BIAS_CTRL_0_BGLP_E_PWRD)) ||
  424. ((next->burst_regs[EMC_PMACRO_BG_BIAS_CTRL_0_INDEX] &
  425. EMC_PMACRO_BG_BIAS_CTRL_0_BG_E_PWRD) ^
  426. (last->burst_regs[EMC_PMACRO_BG_BIAS_CTRL_0_INDEX] &
  427. EMC_PMACRO_BG_BIAS_CTRL_0_BG_E_PWRD));
  428. enable_bglp_reg =
  429. (next->burst_regs[EMC_PMACRO_BG_BIAS_CTRL_0_INDEX] &
  430. EMC_PMACRO_BG_BIAS_CTRL_0_BGLP_E_PWRD) == 0;
  431. enable_bg_reg =
  432. (next->burst_regs[EMC_PMACRO_BG_BIAS_CTRL_0_INDEX] &
  433. EMC_PMACRO_BG_BIAS_CTRL_0_BG_E_PWRD) == 0;
  434. if (bg_reg_mode_change) {
  435. if (enable_bg_reg)
  436. emc_writel(emc, last->burst_regs
  437. [EMC_PMACRO_BG_BIAS_CTRL_0_INDEX] &
  438. ~EMC_PMACRO_BG_BIAS_CTRL_0_BG_E_PWRD,
  439. EMC_PMACRO_BG_BIAS_CTRL_0);
  440. if (enable_bglp_reg)
  441. emc_writel(emc, last->burst_regs
  442. [EMC_PMACRO_BG_BIAS_CTRL_0_INDEX] &
  443. ~EMC_PMACRO_BG_BIAS_CTRL_0_BGLP_E_PWRD,
  444. EMC_PMACRO_BG_BIAS_CTRL_0);
  445. }
  446. /* Check if we need to turn on VREF generator. */
  447. if ((((last->burst_regs[EMC_PMACRO_DATA_PAD_TX_CTRL_INDEX] &
  448. EMC_PMACRO_DATA_PAD_TX_CTRL_DATA_DQ_E_IVREF) == 0) &&
  449. ((next->burst_regs[EMC_PMACRO_DATA_PAD_TX_CTRL_INDEX] &
  450. EMC_PMACRO_DATA_PAD_TX_CTRL_DATA_DQ_E_IVREF) == 1)) ||
  451. (((last->burst_regs[EMC_PMACRO_DATA_PAD_TX_CTRL_INDEX] &
  452. EMC_PMACRO_DATA_PAD_TX_CTRL_DATA_DQS_E_IVREF) == 0) &&
  453. ((next->burst_regs[EMC_PMACRO_DATA_PAD_TX_CTRL_INDEX] &
  454. EMC_PMACRO_DATA_PAD_TX_CTRL_DATA_DQS_E_IVREF) != 0))) {
  455. u32 pad_tx_ctrl =
  456. next->burst_regs[EMC_PMACRO_DATA_PAD_TX_CTRL_INDEX];
  457. u32 last_pad_tx_ctrl =
  458. last->burst_regs[EMC_PMACRO_DATA_PAD_TX_CTRL_INDEX];
  459. u32 next_dq_e_ivref, next_dqs_e_ivref;
  460. next_dqs_e_ivref = pad_tx_ctrl &
  461. EMC_PMACRO_DATA_PAD_TX_CTRL_DATA_DQS_E_IVREF;
  462. next_dq_e_ivref = pad_tx_ctrl &
  463. EMC_PMACRO_DATA_PAD_TX_CTRL_DATA_DQ_E_IVREF;
  464. value = (last_pad_tx_ctrl &
  465. ~EMC_PMACRO_DATA_PAD_TX_CTRL_DATA_DQ_E_IVREF &
  466. ~EMC_PMACRO_DATA_PAD_TX_CTRL_DATA_DQS_E_IVREF) |
  467. next_dq_e_ivref | next_dqs_e_ivref;
  468. emc_writel(emc, value, EMC_PMACRO_DATA_PAD_TX_CTRL);
  469. udelay(1);
  470. } else if (bg_reg_mode_change) {
  471. udelay(1);
  472. }
  473. tegra210_emc_set_shadow_bypass(emc, ASSEMBLY);
  474. /*
  475. * Step 2:
  476. * Prelock the DLL.
  477. */
  478. emc_dbg(emc, STEPS, "Step 2\n");
  479. if (next->burst_regs[EMC_CFG_DIG_DLL_INDEX] &
  480. EMC_CFG_DIG_DLL_CFG_DLL_EN) {
  481. emc_dbg(emc, INFO, "Prelock enabled for target frequency.\n");
  482. value = tegra210_emc_dll_prelock(emc, clksrc);
  483. emc_dbg(emc, INFO, "DLL out: 0x%03x\n", value);
  484. } else {
  485. emc_dbg(emc, INFO, "Disabling DLL for target frequency.\n");
  486. tegra210_emc_dll_disable(emc);
  487. }
  488. /*
  489. * Step 3:
  490. * Prepare autocal for the clock change.
  491. */
  492. emc_dbg(emc, STEPS, "Step 3\n");
  493. tegra210_emc_set_shadow_bypass(emc, ACTIVE);
  494. emc_writel(emc, next->emc_auto_cal_config2, EMC_AUTO_CAL_CONFIG2);
  495. emc_writel(emc, next->emc_auto_cal_config3, EMC_AUTO_CAL_CONFIG3);
  496. emc_writel(emc, next->emc_auto_cal_config4, EMC_AUTO_CAL_CONFIG4);
  497. emc_writel(emc, next->emc_auto_cal_config5, EMC_AUTO_CAL_CONFIG5);
  498. emc_writel(emc, next->emc_auto_cal_config6, EMC_AUTO_CAL_CONFIG6);
  499. emc_writel(emc, next->emc_auto_cal_config7, EMC_AUTO_CAL_CONFIG7);
  500. emc_writel(emc, next->emc_auto_cal_config8, EMC_AUTO_CAL_CONFIG8);
  501. tegra210_emc_set_shadow_bypass(emc, ASSEMBLY);
  502. emc_auto_cal_config |= (EMC_AUTO_CAL_CONFIG_AUTO_CAL_COMPUTE_START |
  503. auto_cal_en);
  504. emc_writel(emc, emc_auto_cal_config, EMC_AUTO_CAL_CONFIG);
  505. /*
  506. * Step 4:
  507. * Update EMC_CFG. (??)
  508. */
  509. emc_dbg(emc, STEPS, "Step 4\n");
  510. if (src_clk_period > 50000 && dram_type == DRAM_TYPE_LPDDR4)
  511. ccfifo_writel(emc, 1, EMC_SELF_REF, 0);
  512. else
  513. emc_writel(emc, next->emc_cfg_2, EMC_CFG_2);
  514. /*
  515. * Step 5:
  516. * Prepare reference variables for ZQCAL regs.
  517. */
  518. emc_dbg(emc, STEPS, "Step 5\n");
  519. if (dram_type == DRAM_TYPE_LPDDR4)
  520. zq_wait_long = max((u32)1, div_o3(1000000, dst_clk_period));
  521. else if (dram_type == DRAM_TYPE_LPDDR2 || is_lpddr3)
  522. zq_wait_long = max(next->min_mrs_wait,
  523. div_o3(360000, dst_clk_period)) + 4;
  524. else if (dram_type == DRAM_TYPE_DDR3)
  525. zq_wait_long = max((u32)256,
  526. div_o3(320000, dst_clk_period) + 2);
  527. else
  528. zq_wait_long = 0;
  529. /*
  530. * Step 6:
  531. * Training code - removed.
  532. */
  533. emc_dbg(emc, STEPS, "Step 6\n");
  534. /*
  535. * Step 7:
  536. * Program FSP reference registers and send MRWs to new FSPWR.
  537. */
  538. emc_dbg(emc, STEPS, "Step 7\n");
  539. emc_dbg(emc, SUB_STEPS, "Step 7.1: Bug 200024907 - Patch RP R2P");
  540. /* WAR 200024907 */
  541. if (dram_type == DRAM_TYPE_LPDDR4) {
  542. u32 nRTP = 16;
  543. if (src_clk_period >= 1000000 / 1866) /* 535.91 ps */
  544. nRTP = 14;
  545. if (src_clk_period >= 1000000 / 1600) /* 625.00 ps */
  546. nRTP = 12;
  547. if (src_clk_period >= 1000000 / 1333) /* 750.19 ps */
  548. nRTP = 10;
  549. if (src_clk_period >= 1000000 / 1066) /* 938.09 ps */
  550. nRTP = 8;
  551. deltaTWATM = max_t(u32, div_o3(7500, src_clk_period), 8);
  552. /*
  553. * Originally there was a + .5 in the tRPST calculation.
  554. * However since we can't do FP in the kernel and the tRTM
  555. * computation was in a floating point ceiling function, adding
  556. * one to tRTP should be ok. There is no other source of non
  557. * integer values, so the result was always going to be
  558. * something for the form: f_ceil(N + .5) = N + 1;
  559. */
  560. tRPST = (last->emc_mrw & 0x80) >> 7;
  561. tRTM = fake->dram_timings[RL] + div_o3(3600, src_clk_period) +
  562. max_t(u32, div_o3(7500, src_clk_period), 8) + tRPST +
  563. 1 + nRTP;
  564. emc_dbg(emc, INFO, "tRTM = %u, EMC_RP = %u\n", tRTM,
  565. next->burst_regs[EMC_RP_INDEX]);
  566. if (last->burst_regs[EMC_RP_INDEX] < tRTM) {
  567. if (tRTM > (last->burst_regs[EMC_R2P_INDEX] +
  568. last->burst_regs[EMC_RP_INDEX])) {
  569. R2P_war = tRTM - last->burst_regs[EMC_RP_INDEX];
  570. RP_war = last->burst_regs[EMC_RP_INDEX];
  571. TRPab_war = last->burst_regs[EMC_TRPAB_INDEX];
  572. if (R2P_war > 63) {
  573. RP_war = R2P_war +
  574. last->burst_regs[EMC_RP_INDEX] - 63;
  575. if (TRPab_war < RP_war)
  576. TRPab_war = RP_war;
  577. R2P_war = 63;
  578. }
  579. } else {
  580. R2P_war = last->burst_regs[EMC_R2P_INDEX];
  581. RP_war = last->burst_regs[EMC_RP_INDEX];
  582. TRPab_war = last->burst_regs[EMC_TRPAB_INDEX];
  583. }
  584. if (RP_war < deltaTWATM) {
  585. W2P_war = last->burst_regs[EMC_W2P_INDEX]
  586. + deltaTWATM - RP_war;
  587. if (W2P_war > 63) {
  588. RP_war = RP_war + W2P_war - 63;
  589. if (TRPab_war < RP_war)
  590. TRPab_war = RP_war;
  591. W2P_war = 63;
  592. }
  593. } else {
  594. W2P_war = last->burst_regs[
  595. EMC_W2P_INDEX];
  596. }
  597. if ((last->burst_regs[EMC_W2P_INDEX] ^ W2P_war) ||
  598. (last->burst_regs[EMC_R2P_INDEX] ^ R2P_war) ||
  599. (last->burst_regs[EMC_RP_INDEX] ^ RP_war) ||
  600. (last->burst_regs[EMC_TRPAB_INDEX] ^ TRPab_war)) {
  601. emc_writel(emc, RP_war, EMC_RP);
  602. emc_writel(emc, R2P_war, EMC_R2P);
  603. emc_writel(emc, W2P_war, EMC_W2P);
  604. emc_writel(emc, TRPab_war, EMC_TRPAB);
  605. }
  606. tegra210_emc_timing_update(emc);
  607. } else {
  608. emc_dbg(emc, INFO, "Skipped WAR\n");
  609. }
  610. }
  611. if (!fsp_for_next_freq) {
  612. mr13_flip_fspwr = (next->emc_mrw3 & 0xffffff3f) | 0x80;
  613. mr13_flip_fspop = (next->emc_mrw3 & 0xffffff3f) | 0x00;
  614. } else {
  615. mr13_flip_fspwr = (next->emc_mrw3 & 0xffffff3f) | 0x40;
  616. mr13_flip_fspop = (next->emc_mrw3 & 0xffffff3f) | 0xc0;
  617. }
  618. if (dram_type == DRAM_TYPE_LPDDR4) {
  619. emc_writel(emc, mr13_flip_fspwr, EMC_MRW3);
  620. emc_writel(emc, next->emc_mrw, EMC_MRW);
  621. emc_writel(emc, next->emc_mrw2, EMC_MRW2);
  622. }
  623. /*
  624. * Step 8:
  625. * Program the shadow registers.
  626. */
  627. emc_dbg(emc, STEPS, "Step 8\n");
  628. emc_dbg(emc, SUB_STEPS, "Writing burst_regs\n");
  629. for (i = 0; i < next->num_burst; i++) {
  630. const u16 *offsets = emc->offsets->burst;
  631. u16 offset;
  632. if (!offsets[i])
  633. continue;
  634. value = next->burst_regs[i];
  635. offset = offsets[i];
  636. if (dram_type != DRAM_TYPE_LPDDR4 &&
  637. (offset == EMC_MRW6 || offset == EMC_MRW7 ||
  638. offset == EMC_MRW8 || offset == EMC_MRW9 ||
  639. offset == EMC_MRW10 || offset == EMC_MRW11 ||
  640. offset == EMC_MRW12 || offset == EMC_MRW13 ||
  641. offset == EMC_MRW14 || offset == EMC_MRW15 ||
  642. offset == EMC_TRAINING_CTRL))
  643. continue;
  644. /* Pain... And suffering. */
  645. if (offset == EMC_CFG) {
  646. value &= ~EMC_CFG_DRAM_ACPD;
  647. value &= ~EMC_CFG_DYN_SELF_REF;
  648. if (dram_type == DRAM_TYPE_LPDDR4) {
  649. value &= ~EMC_CFG_DRAM_CLKSTOP_SR;
  650. value &= ~EMC_CFG_DRAM_CLKSTOP_PD;
  651. }
  652. } else if (offset == EMC_MRS_WAIT_CNT &&
  653. dram_type == DRAM_TYPE_LPDDR2 &&
  654. opt_zcal_en_cc && !opt_cc_short_zcal &&
  655. opt_short_zcal) {
  656. value = (value & ~(EMC_MRS_WAIT_CNT_SHORT_WAIT_MASK <<
  657. EMC_MRS_WAIT_CNT_SHORT_WAIT_SHIFT)) |
  658. ((zq_wait_long & EMC_MRS_WAIT_CNT_SHORT_WAIT_MASK) <<
  659. EMC_MRS_WAIT_CNT_SHORT_WAIT_SHIFT);
  660. } else if (offset == EMC_ZCAL_WAIT_CNT &&
  661. dram_type == DRAM_TYPE_DDR3 && opt_zcal_en_cc &&
  662. !opt_cc_short_zcal && opt_short_zcal) {
  663. value = (value & ~(EMC_ZCAL_WAIT_CNT_ZCAL_WAIT_CNT_MASK <<
  664. EMC_ZCAL_WAIT_CNT_ZCAL_WAIT_CNT_SHIFT)) |
  665. ((zq_wait_long & EMC_ZCAL_WAIT_CNT_ZCAL_WAIT_CNT_MASK) <<
  666. EMC_MRS_WAIT_CNT_SHORT_WAIT_SHIFT);
  667. } else if (offset == EMC_ZCAL_INTERVAL && opt_zcal_en_cc) {
  668. value = 0; /* EMC_ZCAL_INTERVAL reset value. */
  669. } else if (offset == EMC_PMACRO_AUTOCAL_CFG_COMMON) {
  670. value |= EMC_PMACRO_AUTOCAL_CFG_COMMON_E_CAL_BYPASS_DVFS;
  671. } else if (offset == EMC_PMACRO_DATA_PAD_TX_CTRL) {
  672. value &= ~(EMC_PMACRO_DATA_PAD_TX_CTRL_DATA_DQSP_TX_E_DCC |
  673. EMC_PMACRO_DATA_PAD_TX_CTRL_DATA_DQSN_TX_E_DCC |
  674. EMC_PMACRO_DATA_PAD_TX_CTRL_DATA_DQ_TX_E_DCC |
  675. EMC_PMACRO_DATA_PAD_TX_CTRL_DATA_CMD_TX_E_DCC);
  676. } else if (offset == EMC_PMACRO_CMD_PAD_TX_CTRL) {
  677. value |= EMC_PMACRO_CMD_PAD_TX_CTRL_CMD_DQ_TX_DRVFORCEON;
  678. value &= ~(EMC_PMACRO_CMD_PAD_TX_CTRL_CMD_DQSP_TX_E_DCC |
  679. EMC_PMACRO_CMD_PAD_TX_CTRL_CMD_DQSN_TX_E_DCC |
  680. EMC_PMACRO_CMD_PAD_TX_CTRL_CMD_DQ_TX_E_DCC |
  681. EMC_PMACRO_CMD_PAD_TX_CTRL_CMD_CMD_TX_E_DCC);
  682. } else if (offset == EMC_PMACRO_BRICK_CTRL_RFU1) {
  683. value &= 0xf800f800;
  684. } else if (offset == EMC_PMACRO_COMMON_PAD_TX_CTRL) {
  685. value &= 0xfffffff0;
  686. }
  687. emc_writel(emc, value, offset);
  688. }
  689. /* SW addition: do EMC refresh adjustment here. */
  690. tegra210_emc_adjust_timing(emc, next);
  691. if (dram_type == DRAM_TYPE_LPDDR4) {
  692. value = (23 << EMC_MRW_MRW_MA_SHIFT) |
  693. (next->run_clocks & EMC_MRW_MRW_OP_MASK);
  694. emc_writel(emc, value, EMC_MRW);
  695. }
  696. /* Per channel burst registers. */
  697. emc_dbg(emc, SUB_STEPS, "Writing burst_regs_per_ch\n");
  698. for (i = 0; i < next->num_burst_per_ch; i++) {
  699. const struct tegra210_emc_per_channel_regs *burst =
  700. emc->offsets->burst_per_channel;
  701. if (!burst[i].offset)
  702. continue;
  703. if (dram_type != DRAM_TYPE_LPDDR4 &&
  704. (burst[i].offset == EMC_MRW6 ||
  705. burst[i].offset == EMC_MRW7 ||
  706. burst[i].offset == EMC_MRW8 ||
  707. burst[i].offset == EMC_MRW9 ||
  708. burst[i].offset == EMC_MRW10 ||
  709. burst[i].offset == EMC_MRW11 ||
  710. burst[i].offset == EMC_MRW12 ||
  711. burst[i].offset == EMC_MRW13 ||
  712. burst[i].offset == EMC_MRW14 ||
  713. burst[i].offset == EMC_MRW15))
  714. continue;
  715. /* Filter out second channel if not in DUAL_CHANNEL mode. */
  716. if (emc->num_channels < 2 && burst[i].bank >= 1)
  717. continue;
  718. emc_dbg(emc, REG_LISTS, "(%u) 0x%08x => 0x%08x\n", i,
  719. next->burst_reg_per_ch[i], burst[i].offset);
  720. emc_channel_writel(emc, burst[i].bank,
  721. next->burst_reg_per_ch[i],
  722. burst[i].offset);
  723. }
  724. /* Vref regs. */
  725. emc_dbg(emc, SUB_STEPS, "Writing vref_regs\n");
  726. for (i = 0; i < next->vref_num; i++) {
  727. const struct tegra210_emc_per_channel_regs *vref =
  728. emc->offsets->vref_per_channel;
  729. if (!vref[i].offset)
  730. continue;
  731. if (emc->num_channels < 2 && vref[i].bank >= 1)
  732. continue;
  733. emc_dbg(emc, REG_LISTS, "(%u) 0x%08x => 0x%08x\n", i,
  734. next->vref_perch_regs[i], vref[i].offset);
  735. emc_channel_writel(emc, vref[i].bank, next->vref_perch_regs[i],
  736. vref[i].offset);
  737. }
  738. /* Trimmers. */
  739. emc_dbg(emc, SUB_STEPS, "Writing trim_regs\n");
  740. for (i = 0; i < next->num_trim; i++) {
  741. const u16 *offsets = emc->offsets->trim;
  742. if (!offsets[i])
  743. continue;
  744. if (compensate_trimmer_applicable &&
  745. (offsets[i] == EMC_PMACRO_OB_DDLL_LONG_DQ_RANK0_0 ||
  746. offsets[i] == EMC_PMACRO_OB_DDLL_LONG_DQ_RANK0_1 ||
  747. offsets[i] == EMC_PMACRO_OB_DDLL_LONG_DQ_RANK0_2 ||
  748. offsets[i] == EMC_PMACRO_OB_DDLL_LONG_DQ_RANK0_3 ||
  749. offsets[i] == EMC_PMACRO_OB_DDLL_LONG_DQ_RANK1_0 ||
  750. offsets[i] == EMC_PMACRO_OB_DDLL_LONG_DQ_RANK1_1 ||
  751. offsets[i] == EMC_PMACRO_OB_DDLL_LONG_DQ_RANK1_2 ||
  752. offsets[i] == EMC_PMACRO_OB_DDLL_LONG_DQ_RANK1_3 ||
  753. offsets[i] == EMC_DATA_BRLSHFT_0 ||
  754. offsets[i] == EMC_DATA_BRLSHFT_1)) {
  755. value = tegra210_emc_compensate(next, offsets[i]);
  756. emc_dbg(emc, REG_LISTS, "(%u) 0x%08x => 0x%08x\n", i,
  757. value, offsets[i]);
  758. emc_dbg(emc, EMA_WRITES, "0x%08x <= 0x%08x\n",
  759. (u32)(u64)offsets[i], value);
  760. emc_writel(emc, value, offsets[i]);
  761. } else {
  762. emc_dbg(emc, REG_LISTS, "(%u) 0x%08x => 0x%08x\n", i,
  763. next->trim_regs[i], offsets[i]);
  764. emc_writel(emc, next->trim_regs[i], offsets[i]);
  765. }
  766. }
  767. /* Per channel trimmers. */
  768. emc_dbg(emc, SUB_STEPS, "Writing trim_regs_per_ch\n");
  769. for (i = 0; i < next->num_trim_per_ch; i++) {
  770. const struct tegra210_emc_per_channel_regs *trim =
  771. &emc->offsets->trim_per_channel[0];
  772. unsigned int offset;
  773. if (!trim[i].offset)
  774. continue;
  775. if (emc->num_channels < 2 && trim[i].bank >= 1)
  776. continue;
  777. offset = trim[i].offset;
  778. if (compensate_trimmer_applicable &&
  779. (offset == EMC_PMACRO_OB_DDLL_LONG_DQ_RANK0_0 ||
  780. offset == EMC_PMACRO_OB_DDLL_LONG_DQ_RANK0_1 ||
  781. offset == EMC_PMACRO_OB_DDLL_LONG_DQ_RANK0_2 ||
  782. offset == EMC_PMACRO_OB_DDLL_LONG_DQ_RANK0_3 ||
  783. offset == EMC_PMACRO_OB_DDLL_LONG_DQ_RANK1_0 ||
  784. offset == EMC_PMACRO_OB_DDLL_LONG_DQ_RANK1_1 ||
  785. offset == EMC_PMACRO_OB_DDLL_LONG_DQ_RANK1_2 ||
  786. offset == EMC_PMACRO_OB_DDLL_LONG_DQ_RANK1_3 ||
  787. offset == EMC_DATA_BRLSHFT_0 ||
  788. offset == EMC_DATA_BRLSHFT_1)) {
  789. value = tegra210_emc_compensate(next, offset);
  790. emc_dbg(emc, REG_LISTS, "(%u) 0x%08x => 0x%08x\n", i,
  791. value, offset);
  792. emc_dbg(emc, EMA_WRITES, "0x%08x <= 0x%08x\n", offset,
  793. value);
  794. emc_channel_writel(emc, trim[i].bank, value, offset);
  795. } else {
  796. emc_dbg(emc, REG_LISTS, "(%u) 0x%08x => 0x%08x\n", i,
  797. next->trim_perch_regs[i], offset);
  798. emc_channel_writel(emc, trim[i].bank,
  799. next->trim_perch_regs[i], offset);
  800. }
  801. }
  802. emc_dbg(emc, SUB_STEPS, "Writing burst_mc_regs\n");
  803. for (i = 0; i < next->num_mc_regs; i++) {
  804. const u16 *offsets = emc->offsets->burst_mc;
  805. u32 *values = next->burst_mc_regs;
  806. emc_dbg(emc, REG_LISTS, "(%u) 0x%08x => 0x%08x\n", i,
  807. values[i], offsets[i]);
  808. mc_writel(emc->mc, values[i], offsets[i]);
  809. }
  810. /* Registers to be programmed on the faster clock. */
  811. if (next->rate < last->rate) {
  812. const u16 *la = emc->offsets->la_scale;
  813. emc_dbg(emc, SUB_STEPS, "Writing la_scale_regs\n");
  814. for (i = 0; i < next->num_up_down; i++) {
  815. emc_dbg(emc, REG_LISTS, "(%u) 0x%08x => 0x%08x\n", i,
  816. next->la_scale_regs[i], la[i]);
  817. mc_writel(emc->mc, next->la_scale_regs[i], la[i]);
  818. }
  819. }
  820. /* Flush all the burst register writes. */
  821. mc_readl(emc->mc, MC_EMEM_ADR_CFG);
  822. /*
  823. * Step 9:
  824. * LPDDR4 section A.
  825. */
  826. emc_dbg(emc, STEPS, "Step 9\n");
  827. value = next->burst_regs[EMC_ZCAL_WAIT_CNT_INDEX];
  828. value &= ~EMC_ZCAL_WAIT_CNT_ZCAL_WAIT_CNT_MASK;
  829. if (dram_type == DRAM_TYPE_LPDDR4) {
  830. emc_writel(emc, 0, EMC_ZCAL_INTERVAL);
  831. emc_writel(emc, value, EMC_ZCAL_WAIT_CNT);
  832. value = emc_dbg | (EMC_DBG_WRITE_MUX_ACTIVE |
  833. EMC_DBG_WRITE_ACTIVE_ONLY);
  834. emc_writel(emc, value, EMC_DBG);
  835. emc_writel(emc, 0, EMC_ZCAL_INTERVAL);
  836. emc_writel(emc, emc_dbg, EMC_DBG);
  837. }
  838. /*
  839. * Step 10:
  840. * LPDDR4 and DDR3 common section.
  841. */
  842. emc_dbg(emc, STEPS, "Step 10\n");
  843. if (opt_dvfs_mode == MAN_SR || dram_type == DRAM_TYPE_LPDDR4) {
  844. if (dram_type == DRAM_TYPE_LPDDR4)
  845. ccfifo_writel(emc, 0x101, EMC_SELF_REF, 0);
  846. else
  847. ccfifo_writel(emc, 0x1, EMC_SELF_REF, 0);
  848. if (dram_type == DRAM_TYPE_LPDDR4 &&
  849. dst_clk_period <= zqcal_before_cc_cutoff) {
  850. ccfifo_writel(emc, mr13_flip_fspwr ^ 0x40, EMC_MRW3, 0);
  851. ccfifo_writel(emc, (next->burst_regs[EMC_MRW6_INDEX] &
  852. 0xFFFF3F3F) |
  853. (last->burst_regs[EMC_MRW6_INDEX] &
  854. 0x0000C0C0), EMC_MRW6, 0);
  855. ccfifo_writel(emc, (next->burst_regs[EMC_MRW14_INDEX] &
  856. 0xFFFF0707) |
  857. (last->burst_regs[EMC_MRW14_INDEX] &
  858. 0x00003838), EMC_MRW14, 0);
  859. if (emc->num_devices > 1) {
  860. ccfifo_writel(emc,
  861. (next->burst_regs[EMC_MRW7_INDEX] &
  862. 0xFFFF3F3F) |
  863. (last->burst_regs[EMC_MRW7_INDEX] &
  864. 0x0000C0C0), EMC_MRW7, 0);
  865. ccfifo_writel(emc,
  866. (next->burst_regs[EMC_MRW15_INDEX] &
  867. 0xFFFF0707) |
  868. (last->burst_regs[EMC_MRW15_INDEX] &
  869. 0x00003838), EMC_MRW15, 0);
  870. }
  871. if (opt_zcal_en_cc) {
  872. if (emc->num_devices < 2)
  873. ccfifo_writel(emc,
  874. 2UL << EMC_ZQ_CAL_DEV_SEL_SHIFT
  875. | EMC_ZQ_CAL_ZQ_CAL_CMD,
  876. EMC_ZQ_CAL, 0);
  877. else if (shared_zq_resistor)
  878. ccfifo_writel(emc,
  879. 2UL << EMC_ZQ_CAL_DEV_SEL_SHIFT
  880. | EMC_ZQ_CAL_ZQ_CAL_CMD,
  881. EMC_ZQ_CAL, 0);
  882. else
  883. ccfifo_writel(emc,
  884. EMC_ZQ_CAL_ZQ_CAL_CMD,
  885. EMC_ZQ_CAL, 0);
  886. }
  887. }
  888. }
  889. if (dram_type == DRAM_TYPE_LPDDR4) {
  890. value = (1000 * fake->dram_timings[T_RP]) / src_clk_period;
  891. ccfifo_writel(emc, mr13_flip_fspop | 0x8, EMC_MRW3, value);
  892. ccfifo_writel(emc, 0, 0, tFC_lpddr4 / src_clk_period);
  893. }
  894. if (dram_type == DRAM_TYPE_LPDDR4 || opt_dvfs_mode != MAN_SR) {
  895. delay = 30;
  896. if (cya_allow_ref_cc) {
  897. delay += (1000 * fake->dram_timings[T_RP]) /
  898. src_clk_period;
  899. delay += 4000 * fake->dram_timings[T_RFC];
  900. }
  901. ccfifo_writel(emc, emc_pin & ~(EMC_PIN_PIN_CKE_PER_DEV |
  902. EMC_PIN_PIN_CKEB |
  903. EMC_PIN_PIN_CKE),
  904. EMC_PIN, delay);
  905. }
  906. /* calculate reference delay multiplier */
  907. value = 1;
  908. if (ref_b4_sref_en)
  909. value++;
  910. if (cya_allow_ref_cc)
  911. value++;
  912. if (cya_issue_pc_ref)
  913. value++;
  914. if (dram_type != DRAM_TYPE_LPDDR4) {
  915. delay = ((1000 * fake->dram_timings[T_RP] / src_clk_period) +
  916. (1000 * fake->dram_timings[T_RFC] / src_clk_period));
  917. delay = value * delay + 20;
  918. } else {
  919. delay = 0;
  920. }
  921. /*
  922. * Step 11:
  923. * Ramp down.
  924. */
  925. emc_dbg(emc, STEPS, "Step 11\n");
  926. ccfifo_writel(emc, 0x0, EMC_CFG_SYNC, delay);
  927. value = emc_dbg | EMC_DBG_WRITE_MUX_ACTIVE | EMC_DBG_WRITE_ACTIVE_ONLY;
  928. ccfifo_writel(emc, value, EMC_DBG, 0);
  929. ramp_down_wait = tegra210_emc_dvfs_power_ramp_down(emc, src_clk_period,
  930. 0);
  931. /*
  932. * Step 12:
  933. * And finally - trigger the clock change.
  934. */
  935. emc_dbg(emc, STEPS, "Step 12\n");
  936. ccfifo_writel(emc, 1, EMC_STALL_THEN_EXE_AFTER_CLKCHANGE, 0);
  937. value &= ~EMC_DBG_WRITE_ACTIVE_ONLY;
  938. ccfifo_writel(emc, value, EMC_DBG, 0);
  939. /*
  940. * Step 13:
  941. * Ramp up.
  942. */
  943. emc_dbg(emc, STEPS, "Step 13\n");
  944. ramp_up_wait = tegra210_emc_dvfs_power_ramp_up(emc, dst_clk_period, 0);
  945. ccfifo_writel(emc, emc_dbg, EMC_DBG, 0);
  946. /*
  947. * Step 14:
  948. * Bringup CKE pins.
  949. */
  950. emc_dbg(emc, STEPS, "Step 14\n");
  951. if (dram_type == DRAM_TYPE_LPDDR4) {
  952. value = emc_pin | EMC_PIN_PIN_CKE;
  953. if (emc->num_devices <= 1)
  954. value &= ~(EMC_PIN_PIN_CKEB | EMC_PIN_PIN_CKE_PER_DEV);
  955. else
  956. value |= EMC_PIN_PIN_CKEB | EMC_PIN_PIN_CKE_PER_DEV;
  957. ccfifo_writel(emc, value, EMC_PIN, 0);
  958. }
  959. /*
  960. * Step 15: (two step 15s ??)
  961. * Calculate zqlatch wait time; has dependency on ramping times.
  962. */
  963. emc_dbg(emc, STEPS, "Step 15\n");
  964. if (dst_clk_period <= zqcal_before_cc_cutoff) {
  965. s32 t = (s32)(ramp_up_wait + ramp_down_wait) /
  966. (s32)dst_clk_period;
  967. zq_latch_dvfs_wait_time = (s32)tZQCAL_lpddr4_fc_adj - t;
  968. } else {
  969. zq_latch_dvfs_wait_time = tZQCAL_lpddr4_fc_adj -
  970. div_o3(1000 * next->dram_timings[T_PDEX],
  971. dst_clk_period);
  972. }
  973. emc_dbg(emc, INFO, "tZQCAL_lpddr4_fc_adj = %u\n", tZQCAL_lpddr4_fc_adj);
  974. emc_dbg(emc, INFO, "dst_clk_period = %u\n",
  975. dst_clk_period);
  976. emc_dbg(emc, INFO, "next->dram_timings[T_PDEX] = %u\n",
  977. next->dram_timings[T_PDEX]);
  978. emc_dbg(emc, INFO, "zq_latch_dvfs_wait_time = %d\n",
  979. max_t(s32, 0, zq_latch_dvfs_wait_time));
  980. if (dram_type == DRAM_TYPE_LPDDR4 && opt_zcal_en_cc) {
  981. delay = div_o3(1000 * next->dram_timings[T_PDEX],
  982. dst_clk_period);
  983. if (emc->num_devices < 2) {
  984. if (dst_clk_period > zqcal_before_cc_cutoff)
  985. ccfifo_writel(emc,
  986. 2UL << EMC_ZQ_CAL_DEV_SEL_SHIFT |
  987. EMC_ZQ_CAL_ZQ_CAL_CMD, EMC_ZQ_CAL,
  988. delay);
  989. value = (mr13_flip_fspop & 0xfffffff7) | 0x0c000000;
  990. ccfifo_writel(emc, value, EMC_MRW3, delay);
  991. ccfifo_writel(emc, 0, EMC_SELF_REF, 0);
  992. ccfifo_writel(emc, 0, EMC_REF, 0);
  993. ccfifo_writel(emc, 2UL << EMC_ZQ_CAL_DEV_SEL_SHIFT |
  994. EMC_ZQ_CAL_ZQ_LATCH_CMD,
  995. EMC_ZQ_CAL,
  996. max_t(s32, 0, zq_latch_dvfs_wait_time));
  997. } else if (shared_zq_resistor) {
  998. if (dst_clk_period > zqcal_before_cc_cutoff)
  999. ccfifo_writel(emc,
  1000. 2UL << EMC_ZQ_CAL_DEV_SEL_SHIFT |
  1001. EMC_ZQ_CAL_ZQ_CAL_CMD, EMC_ZQ_CAL,
  1002. delay);
  1003. ccfifo_writel(emc, 2UL << EMC_ZQ_CAL_DEV_SEL_SHIFT |
  1004. EMC_ZQ_CAL_ZQ_LATCH_CMD, EMC_ZQ_CAL,
  1005. max_t(s32, 0, zq_latch_dvfs_wait_time) +
  1006. delay);
  1007. ccfifo_writel(emc, 1UL << EMC_ZQ_CAL_DEV_SEL_SHIFT |
  1008. EMC_ZQ_CAL_ZQ_LATCH_CMD,
  1009. EMC_ZQ_CAL, 0);
  1010. value = (mr13_flip_fspop & 0xfffffff7) | 0x0c000000;
  1011. ccfifo_writel(emc, value, EMC_MRW3, 0);
  1012. ccfifo_writel(emc, 0, EMC_SELF_REF, 0);
  1013. ccfifo_writel(emc, 0, EMC_REF, 0);
  1014. ccfifo_writel(emc, 1UL << EMC_ZQ_CAL_DEV_SEL_SHIFT |
  1015. EMC_ZQ_CAL_ZQ_LATCH_CMD, EMC_ZQ_CAL,
  1016. tZQCAL_lpddr4 / dst_clk_period);
  1017. } else {
  1018. if (dst_clk_period > zqcal_before_cc_cutoff)
  1019. ccfifo_writel(emc, EMC_ZQ_CAL_ZQ_CAL_CMD,
  1020. EMC_ZQ_CAL, delay);
  1021. value = (mr13_flip_fspop & 0xfffffff7) | 0x0c000000;
  1022. ccfifo_writel(emc, value, EMC_MRW3, delay);
  1023. ccfifo_writel(emc, 0, EMC_SELF_REF, 0);
  1024. ccfifo_writel(emc, 0, EMC_REF, 0);
  1025. ccfifo_writel(emc, EMC_ZQ_CAL_ZQ_LATCH_CMD, EMC_ZQ_CAL,
  1026. max_t(s32, 0, zq_latch_dvfs_wait_time));
  1027. }
  1028. }
  1029. /* WAR: delay for zqlatch */
  1030. ccfifo_writel(emc, 0, 0, 10);
  1031. /*
  1032. * Step 16:
  1033. * LPDDR4 Conditional Training Kickoff. Removed.
  1034. */
  1035. /*
  1036. * Step 17:
  1037. * MANSR exit self refresh.
  1038. */
  1039. emc_dbg(emc, STEPS, "Step 17\n");
  1040. if (opt_dvfs_mode == MAN_SR && dram_type != DRAM_TYPE_LPDDR4)
  1041. ccfifo_writel(emc, 0, EMC_SELF_REF, 0);
  1042. /*
  1043. * Step 18:
  1044. * Send MRWs to LPDDR3/DDR3.
  1045. */
  1046. emc_dbg(emc, STEPS, "Step 18\n");
  1047. if (dram_type == DRAM_TYPE_LPDDR2) {
  1048. ccfifo_writel(emc, next->emc_mrw2, EMC_MRW2, 0);
  1049. ccfifo_writel(emc, next->emc_mrw, EMC_MRW, 0);
  1050. if (is_lpddr3)
  1051. ccfifo_writel(emc, next->emc_mrw4, EMC_MRW4, 0);
  1052. } else if (dram_type == DRAM_TYPE_DDR3) {
  1053. if (opt_dll_mode)
  1054. ccfifo_writel(emc, next->emc_emrs &
  1055. ~EMC_EMRS_USE_EMRS_LONG_CNT, EMC_EMRS, 0);
  1056. ccfifo_writel(emc, next->emc_emrs2 &
  1057. ~EMC_EMRS2_USE_EMRS2_LONG_CNT, EMC_EMRS2, 0);
  1058. ccfifo_writel(emc, next->emc_mrs |
  1059. EMC_EMRS_USE_EMRS_LONG_CNT, EMC_MRS, 0);
  1060. }
  1061. /*
  1062. * Step 19:
  1063. * ZQCAL for LPDDR3/DDR3
  1064. */
  1065. emc_dbg(emc, STEPS, "Step 19\n");
  1066. if (opt_zcal_en_cc) {
  1067. if (dram_type == DRAM_TYPE_LPDDR2) {
  1068. value = opt_cc_short_zcal ? 90000 : 360000;
  1069. value = div_o3(value, dst_clk_period);
  1070. value = value <<
  1071. EMC_MRS_WAIT_CNT2_MRS_EXT2_WAIT_CNT_SHIFT |
  1072. value <<
  1073. EMC_MRS_WAIT_CNT2_MRS_EXT1_WAIT_CNT_SHIFT;
  1074. ccfifo_writel(emc, value, EMC_MRS_WAIT_CNT2, 0);
  1075. value = opt_cc_short_zcal ? 0x56 : 0xab;
  1076. ccfifo_writel(emc, 2 << EMC_MRW_MRW_DEV_SELECTN_SHIFT |
  1077. EMC_MRW_USE_MRW_EXT_CNT |
  1078. 10 << EMC_MRW_MRW_MA_SHIFT |
  1079. value << EMC_MRW_MRW_OP_SHIFT,
  1080. EMC_MRW, 0);
  1081. if (emc->num_devices > 1) {
  1082. value = 1 << EMC_MRW_MRW_DEV_SELECTN_SHIFT |
  1083. EMC_MRW_USE_MRW_EXT_CNT |
  1084. 10 << EMC_MRW_MRW_MA_SHIFT |
  1085. value << EMC_MRW_MRW_OP_SHIFT;
  1086. ccfifo_writel(emc, value, EMC_MRW, 0);
  1087. }
  1088. } else if (dram_type == DRAM_TYPE_DDR3) {
  1089. value = opt_cc_short_zcal ? 0 : EMC_ZQ_CAL_LONG;
  1090. ccfifo_writel(emc, value |
  1091. 2 << EMC_ZQ_CAL_DEV_SEL_SHIFT |
  1092. EMC_ZQ_CAL_ZQ_CAL_CMD, EMC_ZQ_CAL,
  1093. 0);
  1094. if (emc->num_devices > 1) {
  1095. value = value | 1 << EMC_ZQ_CAL_DEV_SEL_SHIFT |
  1096. EMC_ZQ_CAL_ZQ_CAL_CMD;
  1097. ccfifo_writel(emc, value, EMC_ZQ_CAL, 0);
  1098. }
  1099. }
  1100. }
  1101. if (bg_reg_mode_change) {
  1102. tegra210_emc_set_shadow_bypass(emc, ACTIVE);
  1103. if (ramp_up_wait <= 1250000)
  1104. delay = (1250000 - ramp_up_wait) / dst_clk_period;
  1105. else
  1106. delay = 0;
  1107. ccfifo_writel(emc,
  1108. next->burst_regs[EMC_PMACRO_BG_BIAS_CTRL_0_INDEX],
  1109. EMC_PMACRO_BG_BIAS_CTRL_0, delay);
  1110. tegra210_emc_set_shadow_bypass(emc, ASSEMBLY);
  1111. }
  1112. /*
  1113. * Step 20:
  1114. * Issue ref and optional QRST.
  1115. */
  1116. emc_dbg(emc, STEPS, "Step 20\n");
  1117. if (dram_type != DRAM_TYPE_LPDDR4)
  1118. ccfifo_writel(emc, 0, EMC_REF, 0);
  1119. if (opt_do_sw_qrst) {
  1120. ccfifo_writel(emc, 1, EMC_ISSUE_QRST, 0);
  1121. ccfifo_writel(emc, 0, EMC_ISSUE_QRST, 2);
  1122. }
  1123. /*
  1124. * Step 21:
  1125. * Restore ZCAL and ZCAL interval.
  1126. */
  1127. emc_dbg(emc, STEPS, "Step 21\n");
  1128. if (save_restore_clkstop_pd || opt_zcal_en_cc) {
  1129. ccfifo_writel(emc, emc_dbg | EMC_DBG_WRITE_MUX_ACTIVE,
  1130. EMC_DBG, 0);
  1131. if (opt_zcal_en_cc && dram_type != DRAM_TYPE_LPDDR4)
  1132. ccfifo_writel(emc, next->burst_regs[EMC_ZCAL_INTERVAL_INDEX],
  1133. EMC_ZCAL_INTERVAL, 0);
  1134. if (save_restore_clkstop_pd)
  1135. ccfifo_writel(emc, next->burst_regs[EMC_CFG_INDEX] &
  1136. ~EMC_CFG_DYN_SELF_REF,
  1137. EMC_CFG, 0);
  1138. ccfifo_writel(emc, emc_dbg, EMC_DBG, 0);
  1139. }
  1140. /*
  1141. * Step 22:
  1142. * Restore EMC_CFG_PIPE_CLK.
  1143. */
  1144. emc_dbg(emc, STEPS, "Step 22\n");
  1145. ccfifo_writel(emc, emc_cfg_pipe_clk, EMC_CFG_PIPE_CLK, 0);
  1146. if (bg_reg_mode_change) {
  1147. if (enable_bg_reg)
  1148. emc_writel(emc,
  1149. next->burst_regs[EMC_PMACRO_BG_BIAS_CTRL_0_INDEX] &
  1150. ~EMC_PMACRO_BG_BIAS_CTRL_0_BGLP_E_PWRD,
  1151. EMC_PMACRO_BG_BIAS_CTRL_0);
  1152. else
  1153. emc_writel(emc,
  1154. next->burst_regs[EMC_PMACRO_BG_BIAS_CTRL_0_INDEX] &
  1155. ~EMC_PMACRO_BG_BIAS_CTRL_0_BG_E_PWRD,
  1156. EMC_PMACRO_BG_BIAS_CTRL_0);
  1157. }
  1158. /*
  1159. * Step 23:
  1160. */
  1161. emc_dbg(emc, STEPS, "Step 23\n");
  1162. value = emc_readl(emc, EMC_CFG_DIG_DLL);
  1163. value |= EMC_CFG_DIG_DLL_CFG_DLL_STALL_ALL_TRAFFIC;
  1164. value &= ~EMC_CFG_DIG_DLL_CFG_DLL_STALL_RW_UNTIL_LOCK;
  1165. value &= ~EMC_CFG_DIG_DLL_CFG_DLL_STALL_ALL_UNTIL_LOCK;
  1166. value &= ~EMC_CFG_DIG_DLL_CFG_DLL_EN;
  1167. value = (value & ~EMC_CFG_DIG_DLL_CFG_DLL_MODE_MASK) |
  1168. (2 << EMC_CFG_DIG_DLL_CFG_DLL_MODE_SHIFT);
  1169. emc_writel(emc, value, EMC_CFG_DIG_DLL);
  1170. tegra210_emc_do_clock_change(emc, clksrc);
  1171. /*
  1172. * Step 24:
  1173. * Save training results. Removed.
  1174. */
  1175. /*
  1176. * Step 25:
  1177. * Program MC updown registers.
  1178. */
  1179. emc_dbg(emc, STEPS, "Step 25\n");
  1180. if (next->rate > last->rate) {
  1181. for (i = 0; i < next->num_up_down; i++)
  1182. mc_writel(emc->mc, next->la_scale_regs[i],
  1183. emc->offsets->la_scale[i]);
  1184. tegra210_emc_timing_update(emc);
  1185. }
  1186. /*
  1187. * Step 26:
  1188. * Restore ZCAL registers.
  1189. */
  1190. emc_dbg(emc, STEPS, "Step 26\n");
  1191. if (dram_type == DRAM_TYPE_LPDDR4) {
  1192. tegra210_emc_set_shadow_bypass(emc, ACTIVE);
  1193. emc_writel(emc, next->burst_regs[EMC_ZCAL_WAIT_CNT_INDEX],
  1194. EMC_ZCAL_WAIT_CNT);
  1195. emc_writel(emc, next->burst_regs[EMC_ZCAL_INTERVAL_INDEX],
  1196. EMC_ZCAL_INTERVAL);
  1197. tegra210_emc_set_shadow_bypass(emc, ASSEMBLY);
  1198. }
  1199. if (dram_type != DRAM_TYPE_LPDDR4 && opt_zcal_en_cc &&
  1200. !opt_short_zcal && opt_cc_short_zcal) {
  1201. udelay(2);
  1202. tegra210_emc_set_shadow_bypass(emc, ACTIVE);
  1203. if (dram_type == DRAM_TYPE_LPDDR2)
  1204. emc_writel(emc, next->burst_regs[EMC_MRS_WAIT_CNT_INDEX],
  1205. EMC_MRS_WAIT_CNT);
  1206. else if (dram_type == DRAM_TYPE_DDR3)
  1207. emc_writel(emc, next->burst_regs[EMC_ZCAL_WAIT_CNT_INDEX],
  1208. EMC_ZCAL_WAIT_CNT);
  1209. tegra210_emc_set_shadow_bypass(emc, ASSEMBLY);
  1210. }
  1211. /*
  1212. * Step 27:
  1213. * Restore EMC_CFG, FDPD registers.
  1214. */
  1215. emc_dbg(emc, STEPS, "Step 27\n");
  1216. tegra210_emc_set_shadow_bypass(emc, ACTIVE);
  1217. emc_writel(emc, next->burst_regs[EMC_CFG_INDEX], EMC_CFG);
  1218. tegra210_emc_set_shadow_bypass(emc, ASSEMBLY);
  1219. emc_writel(emc, next->emc_fdpd_ctrl_cmd_no_ramp,
  1220. EMC_FDPD_CTRL_CMD_NO_RAMP);
  1221. emc_writel(emc, next->emc_sel_dpd_ctrl, EMC_SEL_DPD_CTRL);
  1222. /*
  1223. * Step 28:
  1224. * Training recover. Removed.
  1225. */
  1226. emc_dbg(emc, STEPS, "Step 28\n");
  1227. tegra210_emc_set_shadow_bypass(emc, ACTIVE);
  1228. emc_writel(emc,
  1229. next->burst_regs[EMC_PMACRO_AUTOCAL_CFG_COMMON_INDEX],
  1230. EMC_PMACRO_AUTOCAL_CFG_COMMON);
  1231. tegra210_emc_set_shadow_bypass(emc, ASSEMBLY);
  1232. /*
  1233. * Step 29:
  1234. * Power fix WAR.
  1235. */
  1236. emc_dbg(emc, STEPS, "Step 29\n");
  1237. emc_writel(emc, EMC_PMACRO_CFG_PM_GLOBAL_0_DISABLE_CFG_BYTE0 |
  1238. EMC_PMACRO_CFG_PM_GLOBAL_0_DISABLE_CFG_BYTE1 |
  1239. EMC_PMACRO_CFG_PM_GLOBAL_0_DISABLE_CFG_BYTE2 |
  1240. EMC_PMACRO_CFG_PM_GLOBAL_0_DISABLE_CFG_BYTE3 |
  1241. EMC_PMACRO_CFG_PM_GLOBAL_0_DISABLE_CFG_BYTE4 |
  1242. EMC_PMACRO_CFG_PM_GLOBAL_0_DISABLE_CFG_BYTE5 |
  1243. EMC_PMACRO_CFG_PM_GLOBAL_0_DISABLE_CFG_BYTE6 |
  1244. EMC_PMACRO_CFG_PM_GLOBAL_0_DISABLE_CFG_BYTE7,
  1245. EMC_PMACRO_CFG_PM_GLOBAL_0);
  1246. emc_writel(emc, EMC_PMACRO_TRAINING_CTRL_0_CH0_TRAINING_E_WRPTR,
  1247. EMC_PMACRO_TRAINING_CTRL_0);
  1248. emc_writel(emc, EMC_PMACRO_TRAINING_CTRL_1_CH1_TRAINING_E_WRPTR,
  1249. EMC_PMACRO_TRAINING_CTRL_1);
  1250. emc_writel(emc, 0, EMC_PMACRO_CFG_PM_GLOBAL_0);
  1251. /*
  1252. * Step 30:
  1253. * Re-enable autocal.
  1254. */
  1255. emc_dbg(emc, STEPS, "Step 30: Re-enable DLL and AUTOCAL\n");
  1256. if (next->burst_regs[EMC_CFG_DIG_DLL_INDEX] & EMC_CFG_DIG_DLL_CFG_DLL_EN) {
  1257. value = emc_readl(emc, EMC_CFG_DIG_DLL);
  1258. value |= EMC_CFG_DIG_DLL_CFG_DLL_STALL_ALL_TRAFFIC;
  1259. value |= EMC_CFG_DIG_DLL_CFG_DLL_EN;
  1260. value &= ~EMC_CFG_DIG_DLL_CFG_DLL_STALL_RW_UNTIL_LOCK;
  1261. value &= ~EMC_CFG_DIG_DLL_CFG_DLL_STALL_ALL_UNTIL_LOCK;
  1262. value = (value & ~EMC_CFG_DIG_DLL_CFG_DLL_MODE_MASK) |
  1263. (2 << EMC_CFG_DIG_DLL_CFG_DLL_MODE_SHIFT);
  1264. emc_writel(emc, value, EMC_CFG_DIG_DLL);
  1265. tegra210_emc_timing_update(emc);
  1266. }
  1267. emc_writel(emc, next->emc_auto_cal_config, EMC_AUTO_CAL_CONFIG);
  1268. /* Done! Yay. */
  1269. }
  1270. const struct tegra210_emc_sequence tegra210_emc_r21021 = {
  1271. .revision = 0x7,
  1272. .set_clock = tegra210_emc_r21021_set_clock,
  1273. .periodic_compensation = tegra210_emc_r21021_periodic_compensation,
  1274. };