dfs.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114
  1. /*
  2. * Copyright (c) 2016-2019, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <arch_helpers.h>
  7. #include <common/debug.h>
  8. #include <drivers/delay_timer.h>
  9. #include <lib/mmio.h>
  10. #include <m0_ctl.h>
  11. #include <plat_private.h>
  12. #include "dfs.h"
  13. #include "dram.h"
  14. #include "dram_spec_timing.h"
  15. #include "pmu.h"
  16. #include "soc.h"
  17. #include "string.h"
  18. #define ENPER_CS_TRAINING_FREQ (666)
  19. #define TDFI_LAT_THRESHOLD_FREQ (928)
  20. #define PHY_DLL_BYPASS_FREQ (260)
  21. static const struct pll_div dpll_rates_table[] = {
  22. /* _mhz, _refdiv, _fbdiv, _postdiv1, _postdiv2 */
  23. {.mhz = 928, .refdiv = 1, .fbdiv = 116, .postdiv1 = 3, .postdiv2 = 1},
  24. {.mhz = 800, .refdiv = 1, .fbdiv = 100, .postdiv1 = 3, .postdiv2 = 1},
  25. {.mhz = 732, .refdiv = 1, .fbdiv = 61, .postdiv1 = 2, .postdiv2 = 1},
  26. {.mhz = 666, .refdiv = 1, .fbdiv = 111, .postdiv1 = 4, .postdiv2 = 1},
  27. {.mhz = 600, .refdiv = 1, .fbdiv = 50, .postdiv1 = 2, .postdiv2 = 1},
  28. {.mhz = 528, .refdiv = 1, .fbdiv = 66, .postdiv1 = 3, .postdiv2 = 1},
  29. {.mhz = 400, .refdiv = 1, .fbdiv = 50, .postdiv1 = 3, .postdiv2 = 1},
  30. {.mhz = 300, .refdiv = 1, .fbdiv = 50, .postdiv1 = 4, .postdiv2 = 1},
  31. {.mhz = 200, .refdiv = 1, .fbdiv = 50, .postdiv1 = 3, .postdiv2 = 2},
  32. };
  33. struct rk3399_dram_status {
  34. uint32_t current_index;
  35. uint32_t index_freq[2];
  36. uint32_t boot_freq;
  37. uint32_t low_power_stat;
  38. struct timing_related_config timing_config;
  39. struct drv_odt_lp_config drv_odt_lp_cfg;
  40. };
  41. struct rk3399_saved_status {
  42. uint32_t freq;
  43. uint32_t low_power_stat;
  44. uint32_t odt;
  45. };
  46. static struct rk3399_dram_status rk3399_dram_status;
  47. static struct rk3399_saved_status rk3399_suspend_status;
  48. static uint32_t wrdqs_delay_val[2][2][4];
  49. static uint32_t rddqs_delay_ps;
  50. static struct rk3399_sdram_default_config ddr3_default_config = {
  51. .bl = 8,
  52. .ap = 0,
  53. .burst_ref_cnt = 1,
  54. .zqcsi = 0
  55. };
  56. static struct rk3399_sdram_default_config lpddr3_default_config = {
  57. .bl = 8,
  58. .ap = 0,
  59. .burst_ref_cnt = 1,
  60. .zqcsi = 0
  61. };
  62. static struct rk3399_sdram_default_config lpddr4_default_config = {
  63. .bl = 16,
  64. .ap = 0,
  65. .caodt = 240,
  66. .burst_ref_cnt = 1,
  67. .zqcsi = 0
  68. };
  69. static uint32_t get_cs_die_capability(struct rk3399_sdram_params *ram_config,
  70. uint8_t channel, uint8_t cs)
  71. {
  72. struct rk3399_sdram_channel *ch = &ram_config->ch[channel];
  73. uint32_t bandwidth;
  74. uint32_t die_bandwidth;
  75. uint32_t die;
  76. uint32_t cs_cap;
  77. uint32_t row;
  78. row = cs == 0 ? ch->cs0_row : ch->cs1_row;
  79. bandwidth = 8 * (1 << ch->bw);
  80. die_bandwidth = 8 * (1 << ch->dbw);
  81. die = bandwidth / die_bandwidth;
  82. cs_cap = (1 << (row + ((1 << ch->bk) / 4 + 1) + ch->col +
  83. (bandwidth / 16)));
  84. if (ch->row_3_4)
  85. cs_cap = cs_cap * 3 / 4;
  86. return (cs_cap / die);
  87. }
  88. static void get_dram_drv_odt_val(uint32_t dram_type,
  89. struct drv_odt_lp_config *drv_config)
  90. {
  91. uint32_t tmp;
  92. uint32_t mr1_val, mr3_val, mr11_val;
  93. switch (dram_type) {
  94. case DDR3:
  95. mr1_val = (mmio_read_32(CTL_REG(0, 133)) >> 16) & 0xffff;
  96. tmp = ((mr1_val >> 1) & 1) | ((mr1_val >> 4) & 1);
  97. if (tmp)
  98. drv_config->dram_side_drv = 34;
  99. else
  100. drv_config->dram_side_drv = 40;
  101. tmp = ((mr1_val >> 2) & 1) | ((mr1_val >> 5) & 1) |
  102. ((mr1_val >> 7) & 1);
  103. if (tmp == 0)
  104. drv_config->dram_side_dq_odt = 0;
  105. else if (tmp == 1)
  106. drv_config->dram_side_dq_odt = 60;
  107. else if (tmp == 3)
  108. drv_config->dram_side_dq_odt = 40;
  109. else
  110. drv_config->dram_side_dq_odt = 120;
  111. break;
  112. case LPDDR3:
  113. mr3_val = mmio_read_32(CTL_REG(0, 138)) & 0xf;
  114. mr11_val = (mmio_read_32(CTL_REG(0, 139)) >> 24) & 0x3;
  115. if (mr3_val == 0xb)
  116. drv_config->dram_side_drv = 3448;
  117. else if (mr3_val == 0xa)
  118. drv_config->dram_side_drv = 4048;
  119. else if (mr3_val == 0x9)
  120. drv_config->dram_side_drv = 3440;
  121. else if (mr3_val == 0x4)
  122. drv_config->dram_side_drv = 60;
  123. else if (mr3_val == 0x3)
  124. drv_config->dram_side_drv = 48;
  125. else if (mr3_val == 0x2)
  126. drv_config->dram_side_drv = 40;
  127. else
  128. drv_config->dram_side_drv = 34;
  129. if (mr11_val == 1)
  130. drv_config->dram_side_dq_odt = 60;
  131. else if (mr11_val == 2)
  132. drv_config->dram_side_dq_odt = 120;
  133. else if (mr11_val == 0)
  134. drv_config->dram_side_dq_odt = 0;
  135. else
  136. drv_config->dram_side_dq_odt = 240;
  137. break;
  138. case LPDDR4:
  139. default:
  140. mr3_val = (mmio_read_32(CTL_REG(0, 138)) >> 3) & 0x7;
  141. mr11_val = (mmio_read_32(CTL_REG(0, 139)) >> 24) & 0xff;
  142. if ((mr3_val == 0) || (mr3_val == 7))
  143. drv_config->dram_side_drv = 40;
  144. else
  145. drv_config->dram_side_drv = 240 / mr3_val;
  146. tmp = mr11_val & 0x7;
  147. if ((tmp == 7) || (tmp == 0))
  148. drv_config->dram_side_dq_odt = 0;
  149. else
  150. drv_config->dram_side_dq_odt = 240 / tmp;
  151. tmp = (mr11_val >> 4) & 0x7;
  152. if ((tmp == 7) || (tmp == 0))
  153. drv_config->dram_side_ca_odt = 0;
  154. else
  155. drv_config->dram_side_ca_odt = 240 / tmp;
  156. break;
  157. }
  158. }
  159. static void sdram_timing_cfg_init(struct timing_related_config *ptiming_config,
  160. struct rk3399_sdram_params *sdram_params,
  161. struct drv_odt_lp_config *drv_config)
  162. {
  163. uint32_t i, j;
  164. for (i = 0; i < sdram_params->num_channels; i++) {
  165. ptiming_config->dram_info[i].speed_rate = DDR3_DEFAULT;
  166. ptiming_config->dram_info[i].cs_cnt = sdram_params->ch[i].rank;
  167. for (j = 0; j < sdram_params->ch[i].rank; j++) {
  168. ptiming_config->dram_info[i].per_die_capability[j] =
  169. get_cs_die_capability(sdram_params, i, j);
  170. }
  171. }
  172. ptiming_config->dram_type = sdram_params->dramtype;
  173. ptiming_config->ch_cnt = sdram_params->num_channels;
  174. switch (sdram_params->dramtype) {
  175. case DDR3:
  176. ptiming_config->bl = ddr3_default_config.bl;
  177. ptiming_config->ap = ddr3_default_config.ap;
  178. break;
  179. case LPDDR3:
  180. ptiming_config->bl = lpddr3_default_config.bl;
  181. ptiming_config->ap = lpddr3_default_config.ap;
  182. break;
  183. case LPDDR4:
  184. ptiming_config->bl = lpddr4_default_config.bl;
  185. ptiming_config->ap = lpddr4_default_config.ap;
  186. ptiming_config->rdbi = 0;
  187. ptiming_config->wdbi = 0;
  188. break;
  189. default:
  190. /* Do nothing in default case */
  191. break;
  192. }
  193. ptiming_config->dramds = drv_config->dram_side_drv;
  194. ptiming_config->dramodt = drv_config->dram_side_dq_odt;
  195. ptiming_config->caodt = drv_config->dram_side_ca_odt;
  196. ptiming_config->odt = (mmio_read_32(PHY_REG(0, 5)) >> 16) & 0x1;
  197. }
  198. struct lat_adj_pair {
  199. uint32_t cl;
  200. uint32_t rdlat_adj;
  201. uint32_t cwl;
  202. uint32_t wrlat_adj;
  203. };
  204. const struct lat_adj_pair ddr3_lat_adj[] = {
  205. {6, 5, 5, 4},
  206. {8, 7, 6, 5},
  207. {10, 9, 7, 6},
  208. {11, 9, 8, 7},
  209. {13, 0xb, 9, 8},
  210. {14, 0xb, 0xa, 9}
  211. };
  212. const struct lat_adj_pair lpddr3_lat_adj[] = {
  213. {3, 2, 1, 0},
  214. {6, 5, 3, 2},
  215. {8, 7, 4, 3},
  216. {9, 8, 5, 4},
  217. {10, 9, 6, 5},
  218. {11, 9, 6, 5},
  219. {12, 0xa, 6, 5},
  220. {14, 0xc, 8, 7},
  221. {16, 0xd, 8, 7}
  222. };
  223. const struct lat_adj_pair lpddr4_lat_adj[] = {
  224. {6, 5, 4, 2},
  225. {10, 9, 6, 4},
  226. {14, 0xc, 8, 6},
  227. {20, 0x11, 0xa, 8},
  228. {24, 0x15, 0xc, 0xa},
  229. {28, 0x18, 0xe, 0xc},
  230. {32, 0x1b, 0x10, 0xe},
  231. {36, 0x1e, 0x12, 0x10}
  232. };
  233. static uint32_t get_rdlat_adj(uint32_t dram_type, uint32_t cl)
  234. {
  235. const struct lat_adj_pair *p;
  236. uint32_t cnt;
  237. uint32_t i;
  238. if (dram_type == DDR3) {
  239. p = ddr3_lat_adj;
  240. cnt = ARRAY_SIZE(ddr3_lat_adj);
  241. } else if (dram_type == LPDDR3) {
  242. p = lpddr3_lat_adj;
  243. cnt = ARRAY_SIZE(lpddr3_lat_adj);
  244. } else {
  245. p = lpddr4_lat_adj;
  246. cnt = ARRAY_SIZE(lpddr4_lat_adj);
  247. }
  248. for (i = 0; i < cnt; i++) {
  249. if (cl == p[i].cl)
  250. return p[i].rdlat_adj;
  251. }
  252. /* fail */
  253. return 0xff;
  254. }
  255. static uint32_t get_wrlat_adj(uint32_t dram_type, uint32_t cwl)
  256. {
  257. const struct lat_adj_pair *p;
  258. uint32_t cnt;
  259. uint32_t i;
  260. if (dram_type == DDR3) {
  261. p = ddr3_lat_adj;
  262. cnt = ARRAY_SIZE(ddr3_lat_adj);
  263. } else if (dram_type == LPDDR3) {
  264. p = lpddr3_lat_adj;
  265. cnt = ARRAY_SIZE(lpddr3_lat_adj);
  266. } else {
  267. p = lpddr4_lat_adj;
  268. cnt = ARRAY_SIZE(lpddr4_lat_adj);
  269. }
  270. for (i = 0; i < cnt; i++) {
  271. if (cwl == p[i].cwl)
  272. return p[i].wrlat_adj;
  273. }
  274. /* fail */
  275. return 0xff;
  276. }
  277. #define PI_REGS_DIMM_SUPPORT (0)
  278. #define PI_ADD_LATENCY (0)
  279. #define PI_DOUBLEFREEK (1)
  280. #define PI_PAD_DELAY_PS_VALUE (1000)
  281. #define PI_IE_ENABLE_VALUE (3000)
  282. #define PI_TSEL_ENABLE_VALUE (700)
  283. static uint32_t get_pi_rdlat_adj(struct dram_timing_t *pdram_timing)
  284. {
  285. /*[DLLSUBTYPE2] == "STD_DENALI_HS" */
  286. uint32_t rdlat, delay_adder, ie_enable, hs_offset, tsel_adder,
  287. extra_adder, tsel_enable;
  288. ie_enable = PI_IE_ENABLE_VALUE;
  289. tsel_enable = PI_TSEL_ENABLE_VALUE;
  290. rdlat = pdram_timing->cl + PI_ADD_LATENCY;
  291. delay_adder = ie_enable / (1000000 / pdram_timing->mhz);
  292. if ((ie_enable % (1000000 / pdram_timing->mhz)) != 0)
  293. delay_adder++;
  294. hs_offset = 0;
  295. tsel_adder = 0;
  296. extra_adder = 0;
  297. /* rdlat = rdlat - (PREAMBLE_SUPPORT & 0x1); */
  298. tsel_adder = tsel_enable / (1000000 / pdram_timing->mhz);
  299. if ((tsel_enable % (1000000 / pdram_timing->mhz)) != 0)
  300. tsel_adder++;
  301. delay_adder = delay_adder - 1;
  302. if (tsel_adder > delay_adder)
  303. extra_adder = tsel_adder - delay_adder;
  304. else
  305. extra_adder = 0;
  306. if (PI_REGS_DIMM_SUPPORT && PI_DOUBLEFREEK)
  307. hs_offset = 2;
  308. else
  309. hs_offset = 1;
  310. if (delay_adder > (rdlat - 1 - hs_offset)) {
  311. rdlat = rdlat - tsel_adder;
  312. } else {
  313. if ((rdlat - delay_adder) < 2)
  314. rdlat = 2;
  315. else
  316. rdlat = rdlat - delay_adder - extra_adder;
  317. }
  318. return rdlat;
  319. }
  320. static uint32_t get_pi_wrlat(struct dram_timing_t *pdram_timing,
  321. struct timing_related_config *timing_config)
  322. {
  323. uint32_t tmp;
  324. if (timing_config->dram_type == LPDDR3) {
  325. tmp = pdram_timing->cl;
  326. if (tmp >= 14)
  327. tmp = 8;
  328. else if (tmp >= 10)
  329. tmp = 6;
  330. else if (tmp == 9)
  331. tmp = 5;
  332. else if (tmp == 8)
  333. tmp = 4;
  334. else if (tmp == 6)
  335. tmp = 3;
  336. else
  337. tmp = 1;
  338. } else {
  339. tmp = 1;
  340. }
  341. return tmp;
  342. }
  343. static uint32_t get_pi_wrlat_adj(struct dram_timing_t *pdram_timing,
  344. struct timing_related_config *timing_config)
  345. {
  346. return get_pi_wrlat(pdram_timing, timing_config) + PI_ADD_LATENCY - 1;
  347. }
  348. static uint32_t get_pi_tdfi_phy_rdlat(struct dram_timing_t *pdram_timing,
  349. struct timing_related_config *timing_config)
  350. {
  351. /* [DLLSUBTYPE2] == "STD_DENALI_HS" */
  352. uint32_t cas_lat, delay_adder, ie_enable, hs_offset, ie_delay_adder;
  353. uint32_t mem_delay_ps, round_trip_ps;
  354. uint32_t phy_internal_delay, lpddr_adder, dfi_adder, rdlat_delay;
  355. ie_enable = PI_IE_ENABLE_VALUE;
  356. delay_adder = ie_enable / (1000000 / pdram_timing->mhz);
  357. if ((ie_enable % (1000000 / pdram_timing->mhz)) != 0)
  358. delay_adder++;
  359. delay_adder = delay_adder - 1;
  360. if (PI_REGS_DIMM_SUPPORT && PI_DOUBLEFREEK)
  361. hs_offset = 2;
  362. else
  363. hs_offset = 1;
  364. cas_lat = pdram_timing->cl + PI_ADD_LATENCY;
  365. if (delay_adder > (cas_lat - 1 - hs_offset)) {
  366. ie_delay_adder = 0;
  367. } else {
  368. ie_delay_adder = ie_enable / (1000000 / pdram_timing->mhz);
  369. if ((ie_enable % (1000000 / pdram_timing->mhz)) != 0)
  370. ie_delay_adder++;
  371. }
  372. if (timing_config->dram_type == DDR3) {
  373. mem_delay_ps = 0;
  374. } else if (timing_config->dram_type == LPDDR4) {
  375. mem_delay_ps = 3600;
  376. } else if (timing_config->dram_type == LPDDR3) {
  377. mem_delay_ps = 5500;
  378. } else {
  379. NOTICE("get_pi_tdfi_phy_rdlat:dramtype unsupport\n");
  380. return 0;
  381. }
  382. round_trip_ps = 1100 + 500 + mem_delay_ps + 500 + 600;
  383. delay_adder = round_trip_ps / (1000000 / pdram_timing->mhz);
  384. if ((round_trip_ps % (1000000 / pdram_timing->mhz)) != 0)
  385. delay_adder++;
  386. phy_internal_delay = 5 + 2 + 4;
  387. lpddr_adder = mem_delay_ps / (1000000 / pdram_timing->mhz);
  388. if ((mem_delay_ps % (1000000 / pdram_timing->mhz)) != 0)
  389. lpddr_adder++;
  390. dfi_adder = 0;
  391. phy_internal_delay = phy_internal_delay + 2;
  392. rdlat_delay = delay_adder + phy_internal_delay +
  393. ie_delay_adder + lpddr_adder + dfi_adder;
  394. rdlat_delay = rdlat_delay + 2;
  395. return rdlat_delay;
  396. }
  397. static uint32_t get_pi_todtoff_min(struct dram_timing_t *pdram_timing,
  398. struct timing_related_config *timing_config)
  399. {
  400. uint32_t tmp, todtoff_min_ps;
  401. if (timing_config->dram_type == LPDDR3)
  402. todtoff_min_ps = 2500;
  403. else if (timing_config->dram_type == LPDDR4)
  404. todtoff_min_ps = 1500;
  405. else
  406. todtoff_min_ps = 0;
  407. /* todtoff_min */
  408. tmp = todtoff_min_ps / (1000000 / pdram_timing->mhz);
  409. if ((todtoff_min_ps % (1000000 / pdram_timing->mhz)) != 0)
  410. tmp++;
  411. return tmp;
  412. }
  413. static uint32_t get_pi_todtoff_max(struct dram_timing_t *pdram_timing,
  414. struct timing_related_config *timing_config)
  415. {
  416. uint32_t tmp, todtoff_max_ps;
  417. if ((timing_config->dram_type == LPDDR4)
  418. || (timing_config->dram_type == LPDDR3))
  419. todtoff_max_ps = 3500;
  420. else
  421. todtoff_max_ps = 0;
  422. /* todtoff_max */
  423. tmp = todtoff_max_ps / (1000000 / pdram_timing->mhz);
  424. if ((todtoff_max_ps % (1000000 / pdram_timing->mhz)) != 0)
  425. tmp++;
  426. return tmp;
  427. }
  428. static void gen_rk3399_ctl_params_f0(struct timing_related_config
  429. *timing_config,
  430. struct dram_timing_t *pdram_timing)
  431. {
  432. uint32_t i;
  433. uint32_t tmp, tmp1;
  434. for (i = 0; i < timing_config->ch_cnt; i++) {
  435. if (timing_config->dram_type == DDR3) {
  436. tmp = ((700000 + 10) * timing_config->freq +
  437. 999) / 1000;
  438. tmp += pdram_timing->txsnr + (pdram_timing->tmrd * 3) +
  439. pdram_timing->tmod + pdram_timing->tzqinit;
  440. mmio_write_32(CTL_REG(i, 5), tmp);
  441. mmio_clrsetbits_32(CTL_REG(i, 22), 0xffff,
  442. pdram_timing->tdllk);
  443. mmio_write_32(CTL_REG(i, 32),
  444. (pdram_timing->tmod << 8) |
  445. pdram_timing->tmrd);
  446. mmio_clrsetbits_32(CTL_REG(i, 59), 0xffffu << 16,
  447. (pdram_timing->txsr -
  448. pdram_timing->trcd) << 16);
  449. } else if (timing_config->dram_type == LPDDR4) {
  450. mmio_write_32(CTL_REG(i, 5), pdram_timing->tinit1 +
  451. pdram_timing->tinit3);
  452. mmio_write_32(CTL_REG(i, 32),
  453. (pdram_timing->tmrd << 8) |
  454. pdram_timing->tmrd);
  455. mmio_clrsetbits_32(CTL_REG(i, 59), 0xffffu << 16,
  456. pdram_timing->txsr << 16);
  457. } else {
  458. mmio_write_32(CTL_REG(i, 5), pdram_timing->tinit1);
  459. mmio_write_32(CTL_REG(i, 7), pdram_timing->tinit4);
  460. mmio_write_32(CTL_REG(i, 32),
  461. (pdram_timing->tmrd << 8) |
  462. pdram_timing->tmrd);
  463. mmio_clrsetbits_32(CTL_REG(i, 59), 0xffffu << 16,
  464. pdram_timing->txsr << 16);
  465. }
  466. mmio_write_32(CTL_REG(i, 6), pdram_timing->tinit3);
  467. mmio_write_32(CTL_REG(i, 8), pdram_timing->tinit5);
  468. mmio_clrsetbits_32(CTL_REG(i, 23), (0x7f << 16),
  469. ((pdram_timing->cl * 2) << 16));
  470. mmio_clrsetbits_32(CTL_REG(i, 23), (0x1f << 24),
  471. (pdram_timing->cwl << 24));
  472. mmio_clrsetbits_32(CTL_REG(i, 24), 0x3f, pdram_timing->al);
  473. mmio_clrsetbits_32(CTL_REG(i, 26), 0xffffu << 16,
  474. (pdram_timing->trc << 24) |
  475. (pdram_timing->trrd << 16));
  476. mmio_write_32(CTL_REG(i, 27),
  477. (pdram_timing->tfaw << 24) |
  478. (pdram_timing->trppb << 16) |
  479. (pdram_timing->twtr << 8) |
  480. pdram_timing->tras_min);
  481. mmio_clrsetbits_32(CTL_REG(i, 31), 0xffu << 24,
  482. max(4, pdram_timing->trtp) << 24);
  483. mmio_write_32(CTL_REG(i, 33), (pdram_timing->tcke << 24) |
  484. pdram_timing->tras_max);
  485. mmio_clrsetbits_32(CTL_REG(i, 34), 0xff,
  486. max(1, pdram_timing->tckesr));
  487. mmio_clrsetbits_32(CTL_REG(i, 39),
  488. (0x3f << 16) | (0xff << 8),
  489. (pdram_timing->twr << 16) |
  490. (pdram_timing->trcd << 8));
  491. mmio_clrsetbits_32(CTL_REG(i, 42), 0x1f << 16,
  492. pdram_timing->tmrz << 16);
  493. tmp = pdram_timing->tdal ? pdram_timing->tdal :
  494. (pdram_timing->twr + pdram_timing->trp);
  495. mmio_clrsetbits_32(CTL_REG(i, 44), 0xff, tmp);
  496. mmio_clrsetbits_32(CTL_REG(i, 45), 0xff, pdram_timing->trp);
  497. mmio_write_32(CTL_REG(i, 48),
  498. ((pdram_timing->trefi - 8) << 16) |
  499. pdram_timing->trfc);
  500. mmio_clrsetbits_32(CTL_REG(i, 52), 0xffff, pdram_timing->txp);
  501. mmio_clrsetbits_32(CTL_REG(i, 53), 0xffffu << 16,
  502. pdram_timing->txpdll << 16);
  503. mmio_clrsetbits_32(CTL_REG(i, 55), 0xf << 24,
  504. pdram_timing->tcscke << 24);
  505. mmio_clrsetbits_32(CTL_REG(i, 55), 0xff, pdram_timing->tmrri);
  506. mmio_write_32(CTL_REG(i, 56),
  507. (pdram_timing->tzqcke << 24) |
  508. (pdram_timing->tmrwckel << 16) |
  509. (pdram_timing->tckehcs << 8) |
  510. pdram_timing->tckelcs);
  511. mmio_clrsetbits_32(CTL_REG(i, 60), 0xffff, pdram_timing->txsnr);
  512. mmio_clrsetbits_32(CTL_REG(i, 62), 0xffffu << 16,
  513. (pdram_timing->tckehcmd << 24) |
  514. (pdram_timing->tckelcmd << 16));
  515. mmio_write_32(CTL_REG(i, 63),
  516. (pdram_timing->tckelpd << 24) |
  517. (pdram_timing->tescke << 16) |
  518. (pdram_timing->tsr << 8) |
  519. pdram_timing->tckckel);
  520. mmio_clrsetbits_32(CTL_REG(i, 64), 0xfff,
  521. (pdram_timing->tcmdcke << 8) |
  522. pdram_timing->tcsckeh);
  523. mmio_clrsetbits_32(CTL_REG(i, 92), 0xffff << 8,
  524. (pdram_timing->tcksrx << 16) |
  525. (pdram_timing->tcksre << 8));
  526. mmio_clrsetbits_32(CTL_REG(i, 108), 0x1 << 24,
  527. (timing_config->dllbp << 24));
  528. mmio_clrsetbits_32(CTL_REG(i, 122), 0x3ff << 16,
  529. (pdram_timing->tvrcg_enable << 16));
  530. mmio_write_32(CTL_REG(i, 123), (pdram_timing->tfc_long << 16) |
  531. pdram_timing->tvrcg_disable);
  532. mmio_write_32(CTL_REG(i, 124),
  533. (pdram_timing->tvref_long << 16) |
  534. (pdram_timing->tckfspx << 8) |
  535. pdram_timing->tckfspe);
  536. mmio_write_32(CTL_REG(i, 133), (pdram_timing->mr[1] << 16) |
  537. pdram_timing->mr[0]);
  538. mmio_clrsetbits_32(CTL_REG(i, 134), 0xffff,
  539. pdram_timing->mr[2]);
  540. mmio_clrsetbits_32(CTL_REG(i, 138), 0xffff,
  541. pdram_timing->mr[3]);
  542. mmio_clrsetbits_32(CTL_REG(i, 139), 0xffu << 24,
  543. pdram_timing->mr11 << 24);
  544. mmio_write_32(CTL_REG(i, 147),
  545. (pdram_timing->mr[1] << 16) |
  546. pdram_timing->mr[0]);
  547. mmio_clrsetbits_32(CTL_REG(i, 148), 0xffff,
  548. pdram_timing->mr[2]);
  549. mmio_clrsetbits_32(CTL_REG(i, 152), 0xffff,
  550. pdram_timing->mr[3]);
  551. mmio_clrsetbits_32(CTL_REG(i, 153), 0xffu << 24,
  552. pdram_timing->mr11 << 24);
  553. if (timing_config->dram_type == LPDDR4) {
  554. mmio_clrsetbits_32(CTL_REG(i, 140), 0xffffu << 16,
  555. pdram_timing->mr12 << 16);
  556. mmio_clrsetbits_32(CTL_REG(i, 142), 0xffffu << 16,
  557. pdram_timing->mr14 << 16);
  558. mmio_clrsetbits_32(CTL_REG(i, 145), 0xffffu << 16,
  559. pdram_timing->mr22 << 16);
  560. mmio_clrsetbits_32(CTL_REG(i, 154), 0xffffu << 16,
  561. pdram_timing->mr12 << 16);
  562. mmio_clrsetbits_32(CTL_REG(i, 156), 0xffffu << 16,
  563. pdram_timing->mr14 << 16);
  564. mmio_clrsetbits_32(CTL_REG(i, 159), 0xffffu << 16,
  565. pdram_timing->mr22 << 16);
  566. }
  567. mmio_clrsetbits_32(CTL_REG(i, 179), 0xfff << 8,
  568. pdram_timing->tzqinit << 8);
  569. mmio_write_32(CTL_REG(i, 180), (pdram_timing->tzqcs << 16) |
  570. (pdram_timing->tzqinit / 2));
  571. mmio_write_32(CTL_REG(i, 181), (pdram_timing->tzqlat << 16) |
  572. pdram_timing->tzqcal);
  573. mmio_clrsetbits_32(CTL_REG(i, 212), 0xff << 8,
  574. pdram_timing->todton << 8);
  575. if (timing_config->odt) {
  576. mmio_setbits_32(CTL_REG(i, 213), 1 << 16);
  577. if (timing_config->freq < 400)
  578. tmp = 4 << 24;
  579. else
  580. tmp = 8 << 24;
  581. } else {
  582. mmio_clrbits_32(CTL_REG(i, 213), 1 << 16);
  583. tmp = 2 << 24;
  584. }
  585. mmio_clrsetbits_32(CTL_REG(i, 216), 0x1f << 24, tmp);
  586. mmio_clrsetbits_32(CTL_REG(i, 221), (0x3 << 16) | (0xf << 8),
  587. (pdram_timing->tdqsck << 16) |
  588. (pdram_timing->tdqsck_max << 8));
  589. tmp =
  590. (get_wrlat_adj(timing_config->dram_type, pdram_timing->cwl)
  591. << 8) | get_rdlat_adj(timing_config->dram_type,
  592. pdram_timing->cl);
  593. mmio_clrsetbits_32(CTL_REG(i, 284), 0xffff, tmp);
  594. mmio_clrsetbits_32(CTL_REG(i, 82), 0xffffu << 16,
  595. (4 * pdram_timing->trefi) << 16);
  596. mmio_clrsetbits_32(CTL_REG(i, 83), 0xffff,
  597. (2 * pdram_timing->trefi) & 0xffff);
  598. if ((timing_config->dram_type == LPDDR3) ||
  599. (timing_config->dram_type == LPDDR4)) {
  600. tmp = get_pi_wrlat(pdram_timing, timing_config);
  601. tmp1 = get_pi_todtoff_max(pdram_timing, timing_config);
  602. tmp = (tmp > tmp1) ? (tmp - tmp1) : 0;
  603. } else {
  604. tmp = 0;
  605. }
  606. mmio_clrsetbits_32(CTL_REG(i, 214), 0x3f << 16,
  607. (tmp & 0x3f) << 16);
  608. if ((timing_config->dram_type == LPDDR3) ||
  609. (timing_config->dram_type == LPDDR4)) {
  610. /* min_rl_preamble = cl+TDQSCK_MIN -1 */
  611. tmp = pdram_timing->cl +
  612. get_pi_todtoff_min(pdram_timing, timing_config) - 1;
  613. /* todtoff_max */
  614. tmp1 = get_pi_todtoff_max(pdram_timing, timing_config);
  615. tmp = (tmp > tmp1) ? (tmp - tmp1) : 0;
  616. } else {
  617. tmp = pdram_timing->cl - pdram_timing->cwl;
  618. }
  619. mmio_clrsetbits_32(CTL_REG(i, 215), 0x3f << 8,
  620. (tmp & 0x3f) << 8);
  621. mmio_clrsetbits_32(CTL_REG(i, 275), 0xff << 16,
  622. (get_pi_tdfi_phy_rdlat(pdram_timing,
  623. timing_config) &
  624. 0xff) << 16);
  625. mmio_clrsetbits_32(CTL_REG(i, 277), 0xffff,
  626. (2 * pdram_timing->trefi) & 0xffff);
  627. mmio_clrsetbits_32(CTL_REG(i, 282), 0xffff,
  628. (2 * pdram_timing->trefi) & 0xffff);
  629. mmio_write_32(CTL_REG(i, 283), 20 * pdram_timing->trefi);
  630. /* CTL_308 TDFI_CALVL_CAPTURE_F0:RW:16:10 */
  631. tmp1 = 20000 / (1000000 / pdram_timing->mhz) + 1;
  632. if ((20000 % (1000000 / pdram_timing->mhz)) != 0)
  633. tmp1++;
  634. tmp = (tmp1 >> 1) + (tmp1 % 2) + 5;
  635. mmio_clrsetbits_32(CTL_REG(i, 308), 0x3ff << 16, tmp << 16);
  636. /* CTL_308 TDFI_CALVL_CC_F0:RW:0:10 */
  637. tmp = tmp + 18;
  638. mmio_clrsetbits_32(CTL_REG(i, 308), 0x3ff, tmp);
  639. /* CTL_314 TDFI_WRCSLAT_F0:RW:8:8 */
  640. tmp1 = get_pi_wrlat_adj(pdram_timing, timing_config);
  641. if (timing_config->freq <= TDFI_LAT_THRESHOLD_FREQ) {
  642. if (tmp1 == 0)
  643. tmp = 0;
  644. else if (tmp1 < 5)
  645. tmp = tmp1 - 1;
  646. else
  647. tmp = tmp1 - 5;
  648. } else {
  649. tmp = tmp1 - 2;
  650. }
  651. mmio_clrsetbits_32(CTL_REG(i, 314), 0xff << 8, tmp << 8);
  652. /* CTL_314 TDFI_RDCSLAT_F0:RW:0:8 */
  653. if ((timing_config->freq <= TDFI_LAT_THRESHOLD_FREQ) &&
  654. (pdram_timing->cl >= 5))
  655. tmp = pdram_timing->cl - 5;
  656. else
  657. tmp = pdram_timing->cl - 2;
  658. mmio_clrsetbits_32(CTL_REG(i, 314), 0xff, tmp);
  659. }
  660. }
  661. static void gen_rk3399_ctl_params_f1(struct timing_related_config
  662. *timing_config,
  663. struct dram_timing_t *pdram_timing)
  664. {
  665. uint32_t i;
  666. uint32_t tmp, tmp1;
  667. for (i = 0; i < timing_config->ch_cnt; i++) {
  668. if (timing_config->dram_type == DDR3) {
  669. tmp =
  670. ((700000 + 10) * timing_config->freq + 999) / 1000;
  671. tmp += pdram_timing->txsnr + (pdram_timing->tmrd * 3) +
  672. pdram_timing->tmod + pdram_timing->tzqinit;
  673. mmio_write_32(CTL_REG(i, 9), tmp);
  674. mmio_clrsetbits_32(CTL_REG(i, 22), 0xffffu << 16,
  675. pdram_timing->tdllk << 16);
  676. mmio_clrsetbits_32(CTL_REG(i, 34), 0xffffff00,
  677. (pdram_timing->tmod << 24) |
  678. (pdram_timing->tmrd << 16) |
  679. (pdram_timing->trtp << 8));
  680. mmio_clrsetbits_32(CTL_REG(i, 60), 0xffffu << 16,
  681. (pdram_timing->txsr -
  682. pdram_timing->trcd) << 16);
  683. } else if (timing_config->dram_type == LPDDR4) {
  684. mmio_write_32(CTL_REG(i, 9), pdram_timing->tinit1 +
  685. pdram_timing->tinit3);
  686. mmio_clrsetbits_32(CTL_REG(i, 34), 0xffffff00,
  687. (pdram_timing->tmrd << 24) |
  688. (pdram_timing->tmrd << 16) |
  689. (pdram_timing->trtp << 8));
  690. mmio_clrsetbits_32(CTL_REG(i, 60), 0xffffu << 16,
  691. pdram_timing->txsr << 16);
  692. } else {
  693. mmio_write_32(CTL_REG(i, 9), pdram_timing->tinit1);
  694. mmio_write_32(CTL_REG(i, 11), pdram_timing->tinit4);
  695. mmio_clrsetbits_32(CTL_REG(i, 34), 0xffffff00,
  696. (pdram_timing->tmrd << 24) |
  697. (pdram_timing->tmrd << 16) |
  698. (pdram_timing->trtp << 8));
  699. mmio_clrsetbits_32(CTL_REG(i, 60), 0xffffu << 16,
  700. pdram_timing->txsr << 16);
  701. }
  702. mmio_write_32(CTL_REG(i, 10), pdram_timing->tinit3);
  703. mmio_write_32(CTL_REG(i, 12), pdram_timing->tinit5);
  704. mmio_clrsetbits_32(CTL_REG(i, 24), (0x7f << 8),
  705. ((pdram_timing->cl * 2) << 8));
  706. mmio_clrsetbits_32(CTL_REG(i, 24), (0x1f << 16),
  707. (pdram_timing->cwl << 16));
  708. mmio_clrsetbits_32(CTL_REG(i, 24), 0x3f << 24,
  709. pdram_timing->al << 24);
  710. mmio_clrsetbits_32(CTL_REG(i, 28), 0xffffff00,
  711. (pdram_timing->tras_min << 24) |
  712. (pdram_timing->trc << 16) |
  713. (pdram_timing->trrd << 8));
  714. mmio_clrsetbits_32(CTL_REG(i, 29), 0xffffff,
  715. (pdram_timing->tfaw << 16) |
  716. (pdram_timing->trppb << 8) |
  717. pdram_timing->twtr);
  718. mmio_write_32(CTL_REG(i, 35), (pdram_timing->tcke << 24) |
  719. pdram_timing->tras_max);
  720. mmio_clrsetbits_32(CTL_REG(i, 36), 0xff,
  721. max(1, pdram_timing->tckesr));
  722. mmio_clrsetbits_32(CTL_REG(i, 39), (0xffu << 24),
  723. (pdram_timing->trcd << 24));
  724. mmio_clrsetbits_32(CTL_REG(i, 40), 0x3f, pdram_timing->twr);
  725. mmio_clrsetbits_32(CTL_REG(i, 42), 0x1f << 24,
  726. pdram_timing->tmrz << 24);
  727. tmp = pdram_timing->tdal ? pdram_timing->tdal :
  728. (pdram_timing->twr + pdram_timing->trp);
  729. mmio_clrsetbits_32(CTL_REG(i, 44), 0xff << 8, tmp << 8);
  730. mmio_clrsetbits_32(CTL_REG(i, 45), 0xff << 8,
  731. pdram_timing->trp << 8);
  732. mmio_write_32(CTL_REG(i, 49),
  733. ((pdram_timing->trefi - 8) << 16) |
  734. pdram_timing->trfc);
  735. mmio_clrsetbits_32(CTL_REG(i, 52), 0xffffu << 16,
  736. pdram_timing->txp << 16);
  737. mmio_clrsetbits_32(CTL_REG(i, 54), 0xffff,
  738. pdram_timing->txpdll);
  739. mmio_clrsetbits_32(CTL_REG(i, 55), 0xff << 8,
  740. pdram_timing->tmrri << 8);
  741. mmio_write_32(CTL_REG(i, 57), (pdram_timing->tmrwckel << 24) |
  742. (pdram_timing->tckehcs << 16) |
  743. (pdram_timing->tckelcs << 8) |
  744. pdram_timing->tcscke);
  745. mmio_clrsetbits_32(CTL_REG(i, 58), 0xf, pdram_timing->tzqcke);
  746. mmio_clrsetbits_32(CTL_REG(i, 61), 0xffff, pdram_timing->txsnr);
  747. mmio_clrsetbits_32(CTL_REG(i, 64), 0xffffu << 16,
  748. (pdram_timing->tckehcmd << 24) |
  749. (pdram_timing->tckelcmd << 16));
  750. mmio_write_32(CTL_REG(i, 65), (pdram_timing->tckelpd << 24) |
  751. (pdram_timing->tescke << 16) |
  752. (pdram_timing->tsr << 8) |
  753. pdram_timing->tckckel);
  754. mmio_clrsetbits_32(CTL_REG(i, 66), 0xfff,
  755. (pdram_timing->tcmdcke << 8) |
  756. pdram_timing->tcsckeh);
  757. mmio_clrsetbits_32(CTL_REG(i, 92), (0xffu << 24),
  758. (pdram_timing->tcksre << 24));
  759. mmio_clrsetbits_32(CTL_REG(i, 93), 0xff,
  760. pdram_timing->tcksrx);
  761. mmio_clrsetbits_32(CTL_REG(i, 108), (0x1 << 25),
  762. (timing_config->dllbp << 25));
  763. mmio_write_32(CTL_REG(i, 125),
  764. (pdram_timing->tvrcg_disable << 16) |
  765. pdram_timing->tvrcg_enable);
  766. mmio_write_32(CTL_REG(i, 126), (pdram_timing->tckfspx << 24) |
  767. (pdram_timing->tckfspe << 16) |
  768. pdram_timing->tfc_long);
  769. mmio_clrsetbits_32(CTL_REG(i, 127), 0xffff,
  770. pdram_timing->tvref_long);
  771. mmio_clrsetbits_32(CTL_REG(i, 134), 0xffffu << 16,
  772. pdram_timing->mr[0] << 16);
  773. mmio_write_32(CTL_REG(i, 135), (pdram_timing->mr[2] << 16) |
  774. pdram_timing->mr[1]);
  775. mmio_clrsetbits_32(CTL_REG(i, 138), 0xffffu << 16,
  776. pdram_timing->mr[3] << 16);
  777. mmio_clrsetbits_32(CTL_REG(i, 140), 0xff, pdram_timing->mr11);
  778. mmio_clrsetbits_32(CTL_REG(i, 148), 0xffffu << 16,
  779. pdram_timing->mr[0] << 16);
  780. mmio_write_32(CTL_REG(i, 149), (pdram_timing->mr[2] << 16) |
  781. pdram_timing->mr[1]);
  782. mmio_clrsetbits_32(CTL_REG(i, 152), 0xffffu << 16,
  783. pdram_timing->mr[3] << 16);
  784. mmio_clrsetbits_32(CTL_REG(i, 154), 0xff, pdram_timing->mr11);
  785. if (timing_config->dram_type == LPDDR4) {
  786. mmio_clrsetbits_32(CTL_REG(i, 141), 0xffff,
  787. pdram_timing->mr12);
  788. mmio_clrsetbits_32(CTL_REG(i, 143), 0xffff,
  789. pdram_timing->mr14);
  790. mmio_clrsetbits_32(CTL_REG(i, 146), 0xffff,
  791. pdram_timing->mr22);
  792. mmio_clrsetbits_32(CTL_REG(i, 155), 0xffff,
  793. pdram_timing->mr12);
  794. mmio_clrsetbits_32(CTL_REG(i, 157), 0xffff,
  795. pdram_timing->mr14);
  796. mmio_clrsetbits_32(CTL_REG(i, 160), 0xffff,
  797. pdram_timing->mr22);
  798. }
  799. mmio_write_32(CTL_REG(i, 182),
  800. ((pdram_timing->tzqinit / 2) << 16) |
  801. pdram_timing->tzqinit);
  802. mmio_write_32(CTL_REG(i, 183), (pdram_timing->tzqcal << 16) |
  803. pdram_timing->tzqcs);
  804. mmio_clrsetbits_32(CTL_REG(i, 184), 0x3f, pdram_timing->tzqlat);
  805. mmio_clrsetbits_32(CTL_REG(i, 188), 0xfff,
  806. pdram_timing->tzqreset);
  807. mmio_clrsetbits_32(CTL_REG(i, 212), 0xff << 16,
  808. pdram_timing->todton << 16);
  809. if (timing_config->odt) {
  810. mmio_setbits_32(CTL_REG(i, 213), (1 << 24));
  811. if (timing_config->freq < 400)
  812. tmp = 4 << 24;
  813. else
  814. tmp = 8 << 24;
  815. } else {
  816. mmio_clrbits_32(CTL_REG(i, 213), (1 << 24));
  817. tmp = 2 << 24;
  818. }
  819. mmio_clrsetbits_32(CTL_REG(i, 217), 0x1f << 24, tmp);
  820. mmio_clrsetbits_32(CTL_REG(i, 221), 0xf << 24,
  821. (pdram_timing->tdqsck_max << 24));
  822. mmio_clrsetbits_32(CTL_REG(i, 222), 0x3, pdram_timing->tdqsck);
  823. mmio_clrsetbits_32(CTL_REG(i, 291), 0xffff,
  824. (get_wrlat_adj(timing_config->dram_type,
  825. pdram_timing->cwl) << 8) |
  826. get_rdlat_adj(timing_config->dram_type,
  827. pdram_timing->cl));
  828. mmio_clrsetbits_32(CTL_REG(i, 84), 0xffff,
  829. (4 * pdram_timing->trefi) & 0xffff);
  830. mmio_clrsetbits_32(CTL_REG(i, 84), 0xffffu << 16,
  831. ((2 * pdram_timing->trefi) & 0xffff) << 16);
  832. if ((timing_config->dram_type == LPDDR3) ||
  833. (timing_config->dram_type == LPDDR4)) {
  834. tmp = get_pi_wrlat(pdram_timing, timing_config);
  835. tmp1 = get_pi_todtoff_max(pdram_timing, timing_config);
  836. tmp = (tmp > tmp1) ? (tmp - tmp1) : 0;
  837. } else {
  838. tmp = 0;
  839. }
  840. mmio_clrsetbits_32(CTL_REG(i, 214), 0x3f << 24,
  841. (tmp & 0x3f) << 24);
  842. if ((timing_config->dram_type == LPDDR3) ||
  843. (timing_config->dram_type == LPDDR4)) {
  844. /* min_rl_preamble = cl + TDQSCK_MIN - 1 */
  845. tmp = pdram_timing->cl +
  846. get_pi_todtoff_min(pdram_timing, timing_config);
  847. tmp--;
  848. /* todtoff_max */
  849. tmp1 = get_pi_todtoff_max(pdram_timing, timing_config);
  850. tmp = (tmp > tmp1) ? (tmp - tmp1) : 0;
  851. } else {
  852. tmp = pdram_timing->cl - pdram_timing->cwl;
  853. }
  854. mmio_clrsetbits_32(CTL_REG(i, 215), 0x3f << 16,
  855. (tmp & 0x3f) << 16);
  856. mmio_clrsetbits_32(CTL_REG(i, 275), 0xffu << 24,
  857. (get_pi_tdfi_phy_rdlat(pdram_timing,
  858. timing_config) &
  859. 0xff) << 24);
  860. mmio_clrsetbits_32(CTL_REG(i, 284), 0xffffu << 16,
  861. ((2 * pdram_timing->trefi) & 0xffff) << 16);
  862. mmio_clrsetbits_32(CTL_REG(i, 289), 0xffff,
  863. (2 * pdram_timing->trefi) & 0xffff);
  864. mmio_write_32(CTL_REG(i, 290), 20 * pdram_timing->trefi);
  865. /* CTL_309 TDFI_CALVL_CAPTURE_F1:RW:16:10 */
  866. tmp1 = 20000 / (1000000 / pdram_timing->mhz) + 1;
  867. if ((20000 % (1000000 / pdram_timing->mhz)) != 0)
  868. tmp1++;
  869. tmp = (tmp1 >> 1) + (tmp1 % 2) + 5;
  870. mmio_clrsetbits_32(CTL_REG(i, 309), 0x3ff << 16, tmp << 16);
  871. /* CTL_309 TDFI_CALVL_CC_F1:RW:0:10 */
  872. tmp = tmp + 18;
  873. mmio_clrsetbits_32(CTL_REG(i, 309), 0x3ff, tmp);
  874. /* CTL_314 TDFI_WRCSLAT_F1:RW:24:8 */
  875. tmp1 = get_pi_wrlat_adj(pdram_timing, timing_config);
  876. if (timing_config->freq <= TDFI_LAT_THRESHOLD_FREQ) {
  877. if (tmp1 == 0)
  878. tmp = 0;
  879. else if (tmp1 < 5)
  880. tmp = tmp1 - 1;
  881. else
  882. tmp = tmp1 - 5;
  883. } else {
  884. tmp = tmp1 - 2;
  885. }
  886. mmio_clrsetbits_32(CTL_REG(i, 314), 0xffu << 24, tmp << 24);
  887. /* CTL_314 TDFI_RDCSLAT_F1:RW:16:8 */
  888. if ((timing_config->freq <= TDFI_LAT_THRESHOLD_FREQ) &&
  889. (pdram_timing->cl >= 5))
  890. tmp = pdram_timing->cl - 5;
  891. else
  892. tmp = pdram_timing->cl - 2;
  893. mmio_clrsetbits_32(CTL_REG(i, 314), 0xff << 16, tmp << 16);
  894. }
  895. }
  896. static void gen_rk3399_enable_training(uint32_t ch_cnt, uint32_t nmhz)
  897. {
  898. uint32_t i, tmp;
  899. if (nmhz <= PHY_DLL_BYPASS_FREQ)
  900. tmp = 0;
  901. else
  902. tmp = 1;
  903. for (i = 0; i < ch_cnt; i++) {
  904. mmio_clrsetbits_32(CTL_REG(i, 305), 1 << 16, tmp << 16);
  905. mmio_clrsetbits_32(CTL_REG(i, 71), 1, tmp);
  906. mmio_clrsetbits_32(CTL_REG(i, 70), 1 << 8, 1 << 8);
  907. }
  908. }
  909. static void gen_rk3399_disable_training(uint32_t ch_cnt)
  910. {
  911. uint32_t i;
  912. for (i = 0; i < ch_cnt; i++) {
  913. mmio_clrbits_32(CTL_REG(i, 305), 1 << 16);
  914. mmio_clrbits_32(CTL_REG(i, 71), 1);
  915. mmio_clrbits_32(CTL_REG(i, 70), 1 << 8);
  916. }
  917. }
  918. static void gen_rk3399_ctl_params(struct timing_related_config *timing_config,
  919. struct dram_timing_t *pdram_timing,
  920. uint32_t fn)
  921. {
  922. if (fn == 0)
  923. gen_rk3399_ctl_params_f0(timing_config, pdram_timing);
  924. else
  925. gen_rk3399_ctl_params_f1(timing_config, pdram_timing);
  926. }
  927. static void gen_rk3399_pi_params_f0(struct timing_related_config *timing_config,
  928. struct dram_timing_t *pdram_timing)
  929. {
  930. uint32_t tmp, tmp1, tmp2;
  931. uint32_t i;
  932. for (i = 0; i < timing_config->ch_cnt; i++) {
  933. /* PI_02 PI_TDFI_PHYMSTR_MAX_F0:RW:0:32 */
  934. tmp = 4 * pdram_timing->trefi;
  935. mmio_write_32(PI_REG(i, 2), tmp);
  936. /* PI_03 PI_TDFI_PHYMSTR_RESP_F0:RW:0:16 */
  937. tmp = 2 * pdram_timing->trefi;
  938. mmio_clrsetbits_32(PI_REG(i, 3), 0xffff, tmp);
  939. /* PI_07 PI_TDFI_PHYUPD_RESP_F0:RW:16:16 */
  940. mmio_clrsetbits_32(PI_REG(i, 7), 0xffffu << 16, tmp << 16);
  941. /* PI_42 PI_TDELAY_RDWR_2_BUS_IDLE_F0:RW:0:8 */
  942. if (timing_config->dram_type == LPDDR4)
  943. tmp = 2;
  944. else
  945. tmp = 0;
  946. tmp = (pdram_timing->bl / 2) + 4 +
  947. (get_pi_rdlat_adj(pdram_timing) - 2) + tmp +
  948. get_pi_tdfi_phy_rdlat(pdram_timing, timing_config);
  949. mmio_clrsetbits_32(PI_REG(i, 42), 0xff, tmp);
  950. /* PI_43 PI_WRLAT_F0:RW:0:5 */
  951. if (timing_config->dram_type == LPDDR3) {
  952. tmp = get_pi_wrlat(pdram_timing, timing_config);
  953. mmio_clrsetbits_32(PI_REG(i, 43), 0x1f, tmp);
  954. }
  955. /* PI_43 PI_ADDITIVE_LAT_F0:RW:8:6 */
  956. mmio_clrsetbits_32(PI_REG(i, 43), 0x3f << 8,
  957. PI_ADD_LATENCY << 8);
  958. /* PI_43 PI_CASLAT_LIN_F0:RW:16:7 */
  959. mmio_clrsetbits_32(PI_REG(i, 43), 0x7f << 16,
  960. (pdram_timing->cl * 2) << 16);
  961. /* PI_46 PI_TREF_F0:RW:16:16 */
  962. mmio_clrsetbits_32(PI_REG(i, 46), 0xffffu << 16,
  963. pdram_timing->trefi << 16);
  964. /* PI_46 PI_TRFC_F0:RW:0:10 */
  965. mmio_clrsetbits_32(PI_REG(i, 46), 0x3ff, pdram_timing->trfc);
  966. /* PI_66 PI_TODTL_2CMD_F0:RW:24:8 */
  967. if (timing_config->dram_type == LPDDR3) {
  968. tmp = get_pi_todtoff_max(pdram_timing, timing_config);
  969. mmio_clrsetbits_32(PI_REG(i, 66), 0xffu << 24,
  970. tmp << 24);
  971. }
  972. /* PI_72 PI_WR_TO_ODTH_F0:RW:16:6 */
  973. if ((timing_config->dram_type == LPDDR3) ||
  974. (timing_config->dram_type == LPDDR4)) {
  975. tmp1 = get_pi_wrlat(pdram_timing, timing_config);
  976. tmp2 = get_pi_todtoff_max(pdram_timing, timing_config);
  977. if (tmp1 > tmp2)
  978. tmp = tmp1 - tmp2;
  979. else
  980. tmp = 0;
  981. } else if (timing_config->dram_type == DDR3) {
  982. tmp = 0;
  983. }
  984. mmio_clrsetbits_32(PI_REG(i, 72), 0x3f << 16, tmp << 16);
  985. /* PI_73 PI_RD_TO_ODTH_F0:RW:8:6 */
  986. if ((timing_config->dram_type == LPDDR3) ||
  987. (timing_config->dram_type == LPDDR4)) {
  988. /* min_rl_preamble = cl + TDQSCK_MIN - 1 */
  989. tmp1 = pdram_timing->cl;
  990. tmp1 += get_pi_todtoff_min(pdram_timing, timing_config);
  991. tmp1--;
  992. /* todtoff_max */
  993. tmp2 = get_pi_todtoff_max(pdram_timing, timing_config);
  994. if (tmp1 > tmp2)
  995. tmp = tmp1 - tmp2;
  996. else
  997. tmp = 0;
  998. } else if (timing_config->dram_type == DDR3) {
  999. tmp = pdram_timing->cl - pdram_timing->cwl;
  1000. }
  1001. mmio_clrsetbits_32(PI_REG(i, 73), 0x3f << 8, tmp << 8);
  1002. /* PI_89 PI_RDLAT_ADJ_F0:RW:16:8 */
  1003. tmp = get_pi_rdlat_adj(pdram_timing);
  1004. mmio_clrsetbits_32(PI_REG(i, 89), 0xff << 16, tmp << 16);
  1005. /* PI_90 PI_WRLAT_ADJ_F0:RW:16:8 */
  1006. tmp = get_pi_wrlat_adj(pdram_timing, timing_config);
  1007. mmio_clrsetbits_32(PI_REG(i, 90), 0xff << 16, tmp << 16);
  1008. /* PI_91 PI_TDFI_WRCSLAT_F0:RW:16:8 */
  1009. tmp1 = tmp;
  1010. if (tmp1 == 0)
  1011. tmp = 0;
  1012. else if (tmp1 < 5)
  1013. tmp = tmp1 - 1;
  1014. else
  1015. tmp = tmp1 - 5;
  1016. mmio_clrsetbits_32(PI_REG(i, 91), 0xff << 16, tmp << 16);
  1017. /* PI_95 PI_TDFI_CALVL_CAPTURE_F0:RW:16:10 */
  1018. tmp1 = 20000 / (1000000 / pdram_timing->mhz) + 1;
  1019. if ((20000 % (1000000 / pdram_timing->mhz)) != 0)
  1020. tmp1++;
  1021. tmp = (tmp1 >> 1) + (tmp1 % 2) + 5;
  1022. mmio_clrsetbits_32(PI_REG(i, 95), 0x3ff << 16, tmp << 16);
  1023. /* PI_95 PI_TDFI_CALVL_CC_F0:RW:0:10 */
  1024. mmio_clrsetbits_32(PI_REG(i, 95), 0x3ff, tmp + 18);
  1025. /* PI_102 PI_TMRZ_F0:RW:8:5 */
  1026. mmio_clrsetbits_32(PI_REG(i, 102), 0x1f << 8,
  1027. pdram_timing->tmrz << 8);
  1028. /* PI_111 PI_TDFI_CALVL_STROBE_F0:RW:8:4 */
  1029. tmp1 = 2 * 1000 / (1000000 / pdram_timing->mhz);
  1030. if ((2 * 1000 % (1000000 / pdram_timing->mhz)) != 0)
  1031. tmp1++;
  1032. /* pi_tdfi_calvl_strobe=tds_train+5 */
  1033. tmp = tmp1 + 5;
  1034. mmio_clrsetbits_32(PI_REG(i, 111), 0xf << 8, tmp << 8);
  1035. /* PI_116 PI_TCKEHDQS_F0:RW:16:6 */
  1036. tmp = 10000 / (1000000 / pdram_timing->mhz);
  1037. if ((10000 % (1000000 / pdram_timing->mhz)) != 0)
  1038. tmp++;
  1039. if (pdram_timing->mhz <= 100)
  1040. tmp = tmp + 1;
  1041. else
  1042. tmp = tmp + 8;
  1043. mmio_clrsetbits_32(PI_REG(i, 116), 0x3f << 16, tmp << 16);
  1044. /* PI_125 PI_MR1_DATA_F0_0:RW+:8:16 */
  1045. mmio_clrsetbits_32(PI_REG(i, 125), 0xffff << 8,
  1046. pdram_timing->mr[1] << 8);
  1047. /* PI_133 PI_MR1_DATA_F0_1:RW+:0:16 */
  1048. mmio_clrsetbits_32(PI_REG(i, 133), 0xffff, pdram_timing->mr[1]);
  1049. /* PI_140 PI_MR1_DATA_F0_2:RW+:16:16 */
  1050. mmio_clrsetbits_32(PI_REG(i, 140), 0xffffu << 16,
  1051. pdram_timing->mr[1] << 16);
  1052. /* PI_148 PI_MR1_DATA_F0_3:RW+:0:16 */
  1053. mmio_clrsetbits_32(PI_REG(i, 148), 0xffff, pdram_timing->mr[1]);
  1054. /* PI_126 PI_MR2_DATA_F0_0:RW+:0:16 */
  1055. mmio_clrsetbits_32(PI_REG(i, 126), 0xffff, pdram_timing->mr[2]);
  1056. /* PI_133 PI_MR2_DATA_F0_1:RW+:16:16 */
  1057. mmio_clrsetbits_32(PI_REG(i, 133), 0xffffu << 16,
  1058. pdram_timing->mr[2] << 16);
  1059. /* PI_141 PI_MR2_DATA_F0_2:RW+:0:16 */
  1060. mmio_clrsetbits_32(PI_REG(i, 141), 0xffff, pdram_timing->mr[2]);
  1061. /* PI_148 PI_MR2_DATA_F0_3:RW+:16:16 */
  1062. mmio_clrsetbits_32(PI_REG(i, 148), 0xffffu << 16,
  1063. pdram_timing->mr[2] << 16);
  1064. /* PI_156 PI_TFC_F0:RW:0:10 */
  1065. mmio_clrsetbits_32(PI_REG(i, 156), 0x3ff,
  1066. pdram_timing->tfc_long);
  1067. /* PI_158 PI_TWR_F0:RW:24:6 */
  1068. mmio_clrsetbits_32(PI_REG(i, 158), 0x3f << 24,
  1069. pdram_timing->twr << 24);
  1070. /* PI_158 PI_TWTR_F0:RW:16:6 */
  1071. mmio_clrsetbits_32(PI_REG(i, 158), 0x3f << 16,
  1072. pdram_timing->twtr << 16);
  1073. /* PI_158 PI_TRCD_F0:RW:8:8 */
  1074. mmio_clrsetbits_32(PI_REG(i, 158), 0xff << 8,
  1075. pdram_timing->trcd << 8);
  1076. /* PI_158 PI_TRP_F0:RW:0:8 */
  1077. mmio_clrsetbits_32(PI_REG(i, 158), 0xff, pdram_timing->trp);
  1078. /* PI_157 PI_TRTP_F0:RW:24:8 */
  1079. mmio_clrsetbits_32(PI_REG(i, 157), 0xffu << 24,
  1080. pdram_timing->trtp << 24);
  1081. /* PI_159 PI_TRAS_MIN_F0:RW:24:8 */
  1082. mmio_clrsetbits_32(PI_REG(i, 159), 0xffu << 24,
  1083. pdram_timing->tras_min << 24);
  1084. /* PI_159 PI_TRAS_MAX_F0:RW:0:17 */
  1085. tmp = pdram_timing->tras_max * 99 / 100;
  1086. mmio_clrsetbits_32(PI_REG(i, 159), 0x1ffff, tmp);
  1087. /* PI_160 PI_TMRD_F0:RW:16:6 */
  1088. mmio_clrsetbits_32(PI_REG(i, 160), 0x3f << 16,
  1089. pdram_timing->tmrd << 16);
  1090. /*PI_160 PI_TDQSCK_MAX_F0:RW:0:4 */
  1091. mmio_clrsetbits_32(PI_REG(i, 160), 0xf,
  1092. pdram_timing->tdqsck_max);
  1093. /* PI_187 PI_TDFI_CTRLUPD_MAX_F0:RW:8:16 */
  1094. mmio_clrsetbits_32(PI_REG(i, 187), 0xffff << 8,
  1095. (2 * pdram_timing->trefi) << 8);
  1096. /* PI_188 PI_TDFI_CTRLUPD_INTERVAL_F0:RW:0:32 */
  1097. mmio_clrsetbits_32(PI_REG(i, 188), 0xffffffff,
  1098. 20 * pdram_timing->trefi);
  1099. }
  1100. }
  1101. static void gen_rk3399_pi_params_f1(struct timing_related_config *timing_config,
  1102. struct dram_timing_t *pdram_timing)
  1103. {
  1104. uint32_t tmp, tmp1, tmp2;
  1105. uint32_t i;
  1106. for (i = 0; i < timing_config->ch_cnt; i++) {
  1107. /* PI_04 PI_TDFI_PHYMSTR_MAX_F1:RW:0:32 */
  1108. tmp = 4 * pdram_timing->trefi;
  1109. mmio_write_32(PI_REG(i, 4), tmp);
  1110. /* PI_05 PI_TDFI_PHYMSTR_RESP_F1:RW:0:16 */
  1111. tmp = 2 * pdram_timing->trefi;
  1112. mmio_clrsetbits_32(PI_REG(i, 5), 0xffff, tmp);
  1113. /* PI_12 PI_TDFI_PHYUPD_RESP_F1:RW:0:16 */
  1114. mmio_clrsetbits_32(PI_REG(i, 12), 0xffff, tmp);
  1115. /* PI_42 PI_TDELAY_RDWR_2_BUS_IDLE_F1:RW:8:8 */
  1116. if (timing_config->dram_type == LPDDR4)
  1117. tmp = 2;
  1118. else
  1119. tmp = 0;
  1120. tmp = (pdram_timing->bl / 2) + 4 +
  1121. (get_pi_rdlat_adj(pdram_timing) - 2) + tmp +
  1122. get_pi_tdfi_phy_rdlat(pdram_timing, timing_config);
  1123. mmio_clrsetbits_32(PI_REG(i, 42), 0xff << 8, tmp << 8);
  1124. /* PI_43 PI_WRLAT_F1:RW:24:5 */
  1125. if (timing_config->dram_type == LPDDR3) {
  1126. tmp = get_pi_wrlat(pdram_timing, timing_config);
  1127. mmio_clrsetbits_32(PI_REG(i, 43), 0x1f << 24,
  1128. tmp << 24);
  1129. }
  1130. /* PI_44 PI_ADDITIVE_LAT_F1:RW:0:6 */
  1131. mmio_clrsetbits_32(PI_REG(i, 44), 0x3f, PI_ADD_LATENCY);
  1132. /* PI_44 PI_CASLAT_LIN_F1:RW:8:7:=0x18 */
  1133. mmio_clrsetbits_32(PI_REG(i, 44), 0x7f << 8,
  1134. (pdram_timing->cl * 2) << 8);
  1135. /* PI_47 PI_TREF_F1:RW:16:16 */
  1136. mmio_clrsetbits_32(PI_REG(i, 47), 0xffffu << 16,
  1137. pdram_timing->trefi << 16);
  1138. /* PI_47 PI_TRFC_F1:RW:0:10 */
  1139. mmio_clrsetbits_32(PI_REG(i, 47), 0x3ff, pdram_timing->trfc);
  1140. /* PI_67 PI_TODTL_2CMD_F1:RW:8:8 */
  1141. if (timing_config->dram_type == LPDDR3) {
  1142. tmp = get_pi_todtoff_max(pdram_timing, timing_config);
  1143. mmio_clrsetbits_32(PI_REG(i, 67), 0xff << 8, tmp << 8);
  1144. }
  1145. /* PI_72 PI_WR_TO_ODTH_F1:RW:24:6 */
  1146. if ((timing_config->dram_type == LPDDR3) ||
  1147. (timing_config->dram_type == LPDDR4)) {
  1148. tmp1 = get_pi_wrlat(pdram_timing, timing_config);
  1149. tmp2 = get_pi_todtoff_max(pdram_timing, timing_config);
  1150. if (tmp1 > tmp2)
  1151. tmp = tmp1 - tmp2;
  1152. else
  1153. tmp = 0;
  1154. } else if (timing_config->dram_type == DDR3) {
  1155. tmp = 0;
  1156. }
  1157. mmio_clrsetbits_32(PI_REG(i, 72), 0x3f << 24, tmp << 24);
  1158. /* PI_73 PI_RD_TO_ODTH_F1:RW:16:6 */
  1159. if ((timing_config->dram_type == LPDDR3) ||
  1160. (timing_config->dram_type == LPDDR4)) {
  1161. /* min_rl_preamble = cl + TDQSCK_MIN - 1 */
  1162. tmp1 = pdram_timing->cl +
  1163. get_pi_todtoff_min(pdram_timing, timing_config);
  1164. tmp1--;
  1165. /* todtoff_max */
  1166. tmp2 = get_pi_todtoff_max(pdram_timing, timing_config);
  1167. if (tmp1 > tmp2)
  1168. tmp = tmp1 - tmp2;
  1169. else
  1170. tmp = 0;
  1171. } else if (timing_config->dram_type == DDR3)
  1172. tmp = pdram_timing->cl - pdram_timing->cwl;
  1173. mmio_clrsetbits_32(PI_REG(i, 73), 0x3f << 16, tmp << 16);
  1174. /*P I_89 PI_RDLAT_ADJ_F1:RW:24:8 */
  1175. tmp = get_pi_rdlat_adj(pdram_timing);
  1176. mmio_clrsetbits_32(PI_REG(i, 89), 0xffu << 24, tmp << 24);
  1177. /* PI_90 PI_WRLAT_ADJ_F1:RW:24:8 */
  1178. tmp = get_pi_wrlat_adj(pdram_timing, timing_config);
  1179. mmio_clrsetbits_32(PI_REG(i, 90), 0xffu << 24, tmp << 24);
  1180. /* PI_91 PI_TDFI_WRCSLAT_F1:RW:24:8 */
  1181. tmp1 = tmp;
  1182. if (tmp1 == 0)
  1183. tmp = 0;
  1184. else if (tmp1 < 5)
  1185. tmp = tmp1 - 1;
  1186. else
  1187. tmp = tmp1 - 5;
  1188. mmio_clrsetbits_32(PI_REG(i, 91), 0xffu << 24, tmp << 24);
  1189. /*PI_96 PI_TDFI_CALVL_CAPTURE_F1:RW:16:10 */
  1190. /* tadr=20ns */
  1191. tmp1 = 20000 / (1000000 / pdram_timing->mhz) + 1;
  1192. if ((20000 % (1000000 / pdram_timing->mhz)) != 0)
  1193. tmp1++;
  1194. tmp = (tmp1 >> 1) + (tmp1 % 2) + 5;
  1195. mmio_clrsetbits_32(PI_REG(i, 96), 0x3ff << 16, tmp << 16);
  1196. /* PI_96 PI_TDFI_CALVL_CC_F1:RW:0:10 */
  1197. tmp = tmp + 18;
  1198. mmio_clrsetbits_32(PI_REG(i, 96), 0x3ff, tmp);
  1199. /*PI_103 PI_TMRZ_F1:RW:0:5 */
  1200. mmio_clrsetbits_32(PI_REG(i, 103), 0x1f, pdram_timing->tmrz);
  1201. /*PI_111 PI_TDFI_CALVL_STROBE_F1:RW:16:4 */
  1202. /* tds_train=ceil(2/ns) */
  1203. tmp1 = 2 * 1000 / (1000000 / pdram_timing->mhz);
  1204. if ((2 * 1000 % (1000000 / pdram_timing->mhz)) != 0)
  1205. tmp1++;
  1206. /* pi_tdfi_calvl_strobe=tds_train+5 */
  1207. tmp = tmp1 + 5;
  1208. mmio_clrsetbits_32(PI_REG(i, 111), 0xf << 16,
  1209. tmp << 16);
  1210. /* PI_116 PI_TCKEHDQS_F1:RW:24:6 */
  1211. tmp = 10000 / (1000000 / pdram_timing->mhz);
  1212. if ((10000 % (1000000 / pdram_timing->mhz)) != 0)
  1213. tmp++;
  1214. if (pdram_timing->mhz <= 100)
  1215. tmp = tmp + 1;
  1216. else
  1217. tmp = tmp + 8;
  1218. mmio_clrsetbits_32(PI_REG(i, 116), 0x3f << 24,
  1219. tmp << 24);
  1220. /* PI_128 PI_MR1_DATA_F1_0:RW+:0:16 */
  1221. mmio_clrsetbits_32(PI_REG(i, 128), 0xffff, pdram_timing->mr[1]);
  1222. /* PI_135 PI_MR1_DATA_F1_1:RW+:8:16 */
  1223. mmio_clrsetbits_32(PI_REG(i, 135), 0xffff << 8,
  1224. pdram_timing->mr[1] << 8);
  1225. /* PI_143 PI_MR1_DATA_F1_2:RW+:0:16 */
  1226. mmio_clrsetbits_32(PI_REG(i, 143), 0xffff, pdram_timing->mr[1]);
  1227. /* PI_150 PI_MR1_DATA_F1_3:RW+:8:16 */
  1228. mmio_clrsetbits_32(PI_REG(i, 150), 0xffff << 8,
  1229. pdram_timing->mr[1] << 8);
  1230. /* PI_128 PI_MR2_DATA_F1_0:RW+:16:16 */
  1231. mmio_clrsetbits_32(PI_REG(i, 128), 0xffffu << 16,
  1232. pdram_timing->mr[2] << 16);
  1233. /* PI_136 PI_MR2_DATA_F1_1:RW+:0:16 */
  1234. mmio_clrsetbits_32(PI_REG(i, 136), 0xffff, pdram_timing->mr[2]);
  1235. /* PI_143 PI_MR2_DATA_F1_2:RW+:16:16 */
  1236. mmio_clrsetbits_32(PI_REG(i, 143), 0xffffu << 16,
  1237. pdram_timing->mr[2] << 16);
  1238. /* PI_151 PI_MR2_DATA_F1_3:RW+:0:16 */
  1239. mmio_clrsetbits_32(PI_REG(i, 151), 0xffff, pdram_timing->mr[2]);
  1240. /* PI_156 PI_TFC_F1:RW:16:10 */
  1241. mmio_clrsetbits_32(PI_REG(i, 156), 0x3ff << 16,
  1242. pdram_timing->tfc_long << 16);
  1243. /* PI_162 PI_TWR_F1:RW:8:6 */
  1244. mmio_clrsetbits_32(PI_REG(i, 162), 0x3f << 8,
  1245. pdram_timing->twr << 8);
  1246. /* PI_162 PI_TWTR_F1:RW:0:6 */
  1247. mmio_clrsetbits_32(PI_REG(i, 162), 0x3f, pdram_timing->twtr);
  1248. /* PI_161 PI_TRCD_F1:RW:24:8 */
  1249. mmio_clrsetbits_32(PI_REG(i, 161), 0xffu << 24,
  1250. pdram_timing->trcd << 24);
  1251. /* PI_161 PI_TRP_F1:RW:16:8 */
  1252. mmio_clrsetbits_32(PI_REG(i, 161), 0xff << 16,
  1253. pdram_timing->trp << 16);
  1254. /* PI_161 PI_TRTP_F1:RW:8:8 */
  1255. mmio_clrsetbits_32(PI_REG(i, 161), 0xff << 8,
  1256. pdram_timing->trtp << 8);
  1257. /* PI_163 PI_TRAS_MIN_F1:RW:24:8 */
  1258. mmio_clrsetbits_32(PI_REG(i, 163), 0xffu << 24,
  1259. pdram_timing->tras_min << 24);
  1260. /* PI_163 PI_TRAS_MAX_F1:RW:0:17 */
  1261. mmio_clrsetbits_32(PI_REG(i, 163), 0x1ffff,
  1262. pdram_timing->tras_max * 99 / 100);
  1263. /* PI_164 PI_TMRD_F1:RW:16:6 */
  1264. mmio_clrsetbits_32(PI_REG(i, 164), 0x3f << 16,
  1265. pdram_timing->tmrd << 16);
  1266. /* PI_164 PI_TDQSCK_MAX_F1:RW:0:4 */
  1267. mmio_clrsetbits_32(PI_REG(i, 164), 0xf,
  1268. pdram_timing->tdqsck_max);
  1269. /* PI_189 PI_TDFI_CTRLUPD_MAX_F1:RW:0:16 */
  1270. mmio_clrsetbits_32(PI_REG(i, 189), 0xffff,
  1271. 2 * pdram_timing->trefi);
  1272. /* PI_190 PI_TDFI_CTRLUPD_INTERVAL_F1:RW:0:32 */
  1273. mmio_clrsetbits_32(PI_REG(i, 190), 0xffffffff,
  1274. 20 * pdram_timing->trefi);
  1275. }
  1276. }
  1277. static void gen_rk3399_pi_params(struct timing_related_config *timing_config,
  1278. struct dram_timing_t *pdram_timing,
  1279. uint32_t fn)
  1280. {
  1281. if (fn == 0)
  1282. gen_rk3399_pi_params_f0(timing_config, pdram_timing);
  1283. else
  1284. gen_rk3399_pi_params_f1(timing_config, pdram_timing);
  1285. }
  1286. static void gen_rk3399_set_odt(uint32_t odt_en)
  1287. {
  1288. uint32_t drv_odt_val;
  1289. uint32_t i;
  1290. for (i = 0; i < rk3399_dram_status.timing_config.ch_cnt; i++) {
  1291. drv_odt_val = (odt_en | (0 << 1) | (0 << 2)) << 16;
  1292. mmio_clrsetbits_32(PHY_REG(i, 5), 0x7 << 16, drv_odt_val);
  1293. mmio_clrsetbits_32(PHY_REG(i, 133), 0x7 << 16, drv_odt_val);
  1294. mmio_clrsetbits_32(PHY_REG(i, 261), 0x7 << 16, drv_odt_val);
  1295. mmio_clrsetbits_32(PHY_REG(i, 389), 0x7 << 16, drv_odt_val);
  1296. drv_odt_val = (odt_en | (0 << 1) | (0 << 2)) << 24;
  1297. mmio_clrsetbits_32(PHY_REG(i, 6), 0x7 << 24, drv_odt_val);
  1298. mmio_clrsetbits_32(PHY_REG(i, 134), 0x7 << 24, drv_odt_val);
  1299. mmio_clrsetbits_32(PHY_REG(i, 262), 0x7 << 24, drv_odt_val);
  1300. mmio_clrsetbits_32(PHY_REG(i, 390), 0x7 << 24, drv_odt_val);
  1301. }
  1302. }
  1303. static void gen_rk3399_phy_dll_bypass(uint32_t mhz, uint32_t ch,
  1304. uint32_t index, uint32_t dram_type)
  1305. {
  1306. uint32_t sw_master_mode = 0;
  1307. uint32_t rddqs_gate_delay, rddqs_latency, total_delay;
  1308. uint32_t i;
  1309. if (dram_type == DDR3)
  1310. total_delay = PI_PAD_DELAY_PS_VALUE;
  1311. else if (dram_type == LPDDR3)
  1312. total_delay = PI_PAD_DELAY_PS_VALUE + 2500;
  1313. else
  1314. total_delay = PI_PAD_DELAY_PS_VALUE + 1500;
  1315. /* total_delay + 0.55tck */
  1316. total_delay += (55 * 10000)/mhz;
  1317. rddqs_latency = total_delay * mhz / 1000000;
  1318. total_delay -= rddqs_latency * 1000000 / mhz;
  1319. rddqs_gate_delay = total_delay * 0x200 * mhz / 1000000;
  1320. if (mhz <= PHY_DLL_BYPASS_FREQ) {
  1321. sw_master_mode = 0xc;
  1322. mmio_setbits_32(PHY_REG(ch, 514), 1);
  1323. mmio_setbits_32(PHY_REG(ch, 642), 1);
  1324. mmio_setbits_32(PHY_REG(ch, 770), 1);
  1325. /* setting bypass mode slave delay */
  1326. for (i = 0; i < 4; i++) {
  1327. /* wr dq delay = -180deg + (0x60 / 4) * 20ps */
  1328. mmio_clrsetbits_32(PHY_REG(ch, 1 + 128 * i), 0x7ff << 8,
  1329. 0x4a0 << 8);
  1330. /* rd dqs/dq delay = (0x60 / 4) * 20ps */
  1331. mmio_clrsetbits_32(PHY_REG(ch, 11 + 128 * i), 0x3ff,
  1332. 0xa0);
  1333. /* rd rddqs_gate delay */
  1334. mmio_clrsetbits_32(PHY_REG(ch, 2 + 128 * i), 0x3ff,
  1335. rddqs_gate_delay);
  1336. mmio_clrsetbits_32(PHY_REG(ch, 78 + 128 * i), 0xf,
  1337. rddqs_latency);
  1338. }
  1339. for (i = 0; i < 3; i++)
  1340. /* adr delay */
  1341. mmio_clrsetbits_32(PHY_REG(ch, 513 + 128 * i),
  1342. 0x7ff << 16, 0x80 << 16);
  1343. if ((mmio_read_32(PHY_REG(ch, 86)) & 0xc00) == 0) {
  1344. /*
  1345. * old status is normal mode,
  1346. * and saving the wrdqs slave delay
  1347. */
  1348. for (i = 0; i < 4; i++) {
  1349. /* save and clear wr dqs slave delay */
  1350. wrdqs_delay_val[ch][index][i] = 0x3ff &
  1351. (mmio_read_32(PHY_REG(ch, 63 + i * 128))
  1352. >> 16);
  1353. mmio_clrsetbits_32(PHY_REG(ch, 63 + i * 128),
  1354. 0x03ff << 16, 0 << 16);
  1355. /*
  1356. * in normal mode the cmd may delay 1cycle by
  1357. * wrlvl and in bypass mode making dqs also
  1358. * delay 1cycle.
  1359. */
  1360. mmio_clrsetbits_32(PHY_REG(ch, 78 + i * 128),
  1361. 0x07 << 8, 0x1 << 8);
  1362. }
  1363. }
  1364. } else if (mmio_read_32(PHY_REG(ch, 86)) & 0xc00) {
  1365. /* old status is bypass mode and restore wrlvl resume */
  1366. for (i = 0; i < 4; i++) {
  1367. mmio_clrsetbits_32(PHY_REG(ch, 63 + i * 128),
  1368. 0x03ff << 16,
  1369. (wrdqs_delay_val[ch][index][i] &
  1370. 0x3ff) << 16);
  1371. /* resume phy_write_path_lat_add */
  1372. mmio_clrbits_32(PHY_REG(ch, 78 + i * 128), 0x07 << 8);
  1373. }
  1374. }
  1375. /* phy_sw_master_mode_X PHY_86/214/342/470 4bits offset_8 */
  1376. mmio_clrsetbits_32(PHY_REG(ch, 86), 0xf << 8, sw_master_mode << 8);
  1377. mmio_clrsetbits_32(PHY_REG(ch, 214), 0xf << 8, sw_master_mode << 8);
  1378. mmio_clrsetbits_32(PHY_REG(ch, 342), 0xf << 8, sw_master_mode << 8);
  1379. mmio_clrsetbits_32(PHY_REG(ch, 470), 0xf << 8, sw_master_mode << 8);
  1380. /* phy_adrctl_sw_master_mode PHY_547/675/803 4bits offset_16 */
  1381. mmio_clrsetbits_32(PHY_REG(ch, 547), 0xf << 16, sw_master_mode << 16);
  1382. mmio_clrsetbits_32(PHY_REG(ch, 675), 0xf << 16, sw_master_mode << 16);
  1383. mmio_clrsetbits_32(PHY_REG(ch, 803), 0xf << 16, sw_master_mode << 16);
  1384. }
  1385. static void gen_rk3399_phy_params(struct timing_related_config *timing_config,
  1386. struct drv_odt_lp_config *drv_config,
  1387. struct dram_timing_t *pdram_timing,
  1388. uint32_t fn)
  1389. {
  1390. uint32_t tmp, i, div, j;
  1391. uint32_t mem_delay_ps, pad_delay_ps, total_delay_ps, delay_frac_ps;
  1392. uint32_t trpre_min_ps, gate_delay_ps, gate_delay_frac_ps;
  1393. uint32_t ie_enable, tsel_enable, cas_lat, rddata_en_ie_dly, tsel_adder;
  1394. uint32_t extra_adder, delta, hs_offset;
  1395. for (i = 0; i < timing_config->ch_cnt; i++) {
  1396. pad_delay_ps = PI_PAD_DELAY_PS_VALUE;
  1397. ie_enable = PI_IE_ENABLE_VALUE;
  1398. tsel_enable = PI_TSEL_ENABLE_VALUE;
  1399. mmio_clrsetbits_32(PHY_REG(i, 896), (0x3 << 8) | 1, fn << 8);
  1400. /* PHY_LOW_FREQ_SEL */
  1401. /* DENALI_PHY_913 1bit offset_0 */
  1402. if (timing_config->freq > 400)
  1403. mmio_clrbits_32(PHY_REG(i, 913), 1);
  1404. else
  1405. mmio_setbits_32(PHY_REG(i, 913), 1);
  1406. /* PHY_RPTR_UPDATE_x */
  1407. /* DENALI_PHY_87/215/343/471 4bit offset_16 */
  1408. tmp = 2500 / (1000000 / pdram_timing->mhz) + 3;
  1409. if ((2500 % (1000000 / pdram_timing->mhz)) != 0)
  1410. tmp++;
  1411. mmio_clrsetbits_32(PHY_REG(i, 87), 0xf << 16, tmp << 16);
  1412. mmio_clrsetbits_32(PHY_REG(i, 215), 0xf << 16, tmp << 16);
  1413. mmio_clrsetbits_32(PHY_REG(i, 343), 0xf << 16, tmp << 16);
  1414. mmio_clrsetbits_32(PHY_REG(i, 471), 0xf << 16, tmp << 16);
  1415. /* PHY_PLL_CTRL */
  1416. /* DENALI_PHY_911 13bits offset_0 */
  1417. /* PHY_LP4_BOOT_PLL_CTRL */
  1418. /* DENALI_PHY_919 13bits offset_0 */
  1419. tmp = (1 << 12) | (2 << 7) | (1 << 1);
  1420. mmio_clrsetbits_32(PHY_REG(i, 911), 0x1fff, tmp);
  1421. mmio_clrsetbits_32(PHY_REG(i, 919), 0x1fff, tmp);
  1422. /* PHY_PLL_CTRL_CA */
  1423. /* DENALI_PHY_911 13bits offset_16 */
  1424. /* PHY_LP4_BOOT_PLL_CTRL_CA */
  1425. /* DENALI_PHY_919 13bits offset_16 */
  1426. tmp = (2 << 7) | (1 << 5) | (1 << 1);
  1427. mmio_clrsetbits_32(PHY_REG(i, 911), 0x1fff << 16, tmp << 16);
  1428. mmio_clrsetbits_32(PHY_REG(i, 919), 0x1fff << 16, tmp << 16);
  1429. /* PHY_TCKSRE_WAIT */
  1430. /* DENALI_PHY_922 4bits offset_24 */
  1431. if (pdram_timing->mhz <= 400)
  1432. tmp = 1;
  1433. else if (pdram_timing->mhz <= 800)
  1434. tmp = 3;
  1435. else if (pdram_timing->mhz <= 1000)
  1436. tmp = 4;
  1437. else
  1438. tmp = 5;
  1439. mmio_clrsetbits_32(PHY_REG(i, 922), 0xf << 24, tmp << 24);
  1440. /* PHY_CAL_CLK_SELECT_0:RW8:3 */
  1441. div = pdram_timing->mhz / (2 * 20);
  1442. for (j = 2, tmp = 1; j <= 128; j <<= 1, tmp++) {
  1443. if (div < j)
  1444. break;
  1445. }
  1446. mmio_clrsetbits_32(PHY_REG(i, 947), 0x7 << 8, tmp << 8);
  1447. if (timing_config->dram_type == DDR3) {
  1448. mem_delay_ps = 0;
  1449. trpre_min_ps = 1000;
  1450. } else if (timing_config->dram_type == LPDDR4) {
  1451. mem_delay_ps = 1500;
  1452. trpre_min_ps = 900;
  1453. } else if (timing_config->dram_type == LPDDR3) {
  1454. mem_delay_ps = 2500;
  1455. trpre_min_ps = 900;
  1456. } else {
  1457. ERROR("gen_rk3399_phy_params:dramtype unsupport\n");
  1458. return;
  1459. }
  1460. total_delay_ps = mem_delay_ps + pad_delay_ps;
  1461. delay_frac_ps = 1000 * total_delay_ps /
  1462. (1000000 / pdram_timing->mhz);
  1463. gate_delay_ps = delay_frac_ps + 1000 - (trpre_min_ps / 2);
  1464. gate_delay_frac_ps = gate_delay_ps % 1000;
  1465. tmp = gate_delay_frac_ps * 0x200 / 1000;
  1466. /* PHY_RDDQS_GATE_SLAVE_DELAY */
  1467. /* DENALI_PHY_77/205/333/461 10bits offset_16 */
  1468. mmio_clrsetbits_32(PHY_REG(i, 77), 0x2ff << 16, tmp << 16);
  1469. mmio_clrsetbits_32(PHY_REG(i, 205), 0x2ff << 16, tmp << 16);
  1470. mmio_clrsetbits_32(PHY_REG(i, 333), 0x2ff << 16, tmp << 16);
  1471. mmio_clrsetbits_32(PHY_REG(i, 461), 0x2ff << 16, tmp << 16);
  1472. tmp = gate_delay_ps / 1000;
  1473. /* PHY_LP4_BOOT_RDDQS_LATENCY_ADJUST */
  1474. /* DENALI_PHY_10/138/266/394 4bit offset_0 */
  1475. mmio_clrsetbits_32(PHY_REG(i, 10), 0xf, tmp);
  1476. mmio_clrsetbits_32(PHY_REG(i, 138), 0xf, tmp);
  1477. mmio_clrsetbits_32(PHY_REG(i, 266), 0xf, tmp);
  1478. mmio_clrsetbits_32(PHY_REG(i, 394), 0xf, tmp);
  1479. /* PHY_GTLVL_LAT_ADJ_START */
  1480. /* DENALI_PHY_80/208/336/464 4bits offset_16 */
  1481. tmp = rddqs_delay_ps / (1000000 / pdram_timing->mhz) + 2;
  1482. mmio_clrsetbits_32(PHY_REG(i, 80), 0xf << 16, tmp << 16);
  1483. mmio_clrsetbits_32(PHY_REG(i, 208), 0xf << 16, tmp << 16);
  1484. mmio_clrsetbits_32(PHY_REG(i, 336), 0xf << 16, tmp << 16);
  1485. mmio_clrsetbits_32(PHY_REG(i, 464), 0xf << 16, tmp << 16);
  1486. cas_lat = pdram_timing->cl + PI_ADD_LATENCY;
  1487. rddata_en_ie_dly = ie_enable / (1000000 / pdram_timing->mhz);
  1488. if ((ie_enable % (1000000 / pdram_timing->mhz)) != 0)
  1489. rddata_en_ie_dly++;
  1490. rddata_en_ie_dly = rddata_en_ie_dly - 1;
  1491. tsel_adder = tsel_enable / (1000000 / pdram_timing->mhz);
  1492. if ((tsel_enable % (1000000 / pdram_timing->mhz)) != 0)
  1493. tsel_adder++;
  1494. if (rddata_en_ie_dly > tsel_adder)
  1495. extra_adder = rddata_en_ie_dly - tsel_adder;
  1496. else
  1497. extra_adder = 0;
  1498. delta = cas_lat - rddata_en_ie_dly;
  1499. if (PI_REGS_DIMM_SUPPORT && PI_DOUBLEFREEK)
  1500. hs_offset = 2;
  1501. else
  1502. hs_offset = 1;
  1503. if (rddata_en_ie_dly > (cas_lat - 1 - hs_offset))
  1504. tmp = 0;
  1505. else if ((delta == 2) || (delta == 1))
  1506. tmp = rddata_en_ie_dly - 0 - extra_adder;
  1507. else
  1508. tmp = extra_adder;
  1509. /* PHY_LP4_BOOT_RDDATA_EN_TSEL_DLY */
  1510. /* DENALI_PHY_9/137/265/393 4bit offset_16 */
  1511. mmio_clrsetbits_32(PHY_REG(i, 9), 0xf << 16, tmp << 16);
  1512. mmio_clrsetbits_32(PHY_REG(i, 137), 0xf << 16, tmp << 16);
  1513. mmio_clrsetbits_32(PHY_REG(i, 265), 0xf << 16, tmp << 16);
  1514. mmio_clrsetbits_32(PHY_REG(i, 393), 0xf << 16, tmp << 16);
  1515. /* PHY_RDDATA_EN_TSEL_DLY */
  1516. /* DENALI_PHY_86/214/342/470 4bit offset_0 */
  1517. mmio_clrsetbits_32(PHY_REG(i, 86), 0xf, tmp);
  1518. mmio_clrsetbits_32(PHY_REG(i, 214), 0xf, tmp);
  1519. mmio_clrsetbits_32(PHY_REG(i, 342), 0xf, tmp);
  1520. mmio_clrsetbits_32(PHY_REG(i, 470), 0xf, tmp);
  1521. if (tsel_adder > rddata_en_ie_dly)
  1522. extra_adder = tsel_adder - rddata_en_ie_dly;
  1523. else
  1524. extra_adder = 0;
  1525. if (rddata_en_ie_dly > (cas_lat - 1 - hs_offset))
  1526. tmp = tsel_adder;
  1527. else
  1528. tmp = rddata_en_ie_dly - 0 + extra_adder;
  1529. /* PHY_LP4_BOOT_RDDATA_EN_DLY */
  1530. /* DENALI_PHY_9/137/265/393 4bit offset_8 */
  1531. mmio_clrsetbits_32(PHY_REG(i, 9), 0xf << 8, tmp << 8);
  1532. mmio_clrsetbits_32(PHY_REG(i, 137), 0xf << 8, tmp << 8);
  1533. mmio_clrsetbits_32(PHY_REG(i, 265), 0xf << 8, tmp << 8);
  1534. mmio_clrsetbits_32(PHY_REG(i, 393), 0xf << 8, tmp << 8);
  1535. /* PHY_RDDATA_EN_DLY */
  1536. /* DENALI_PHY_85/213/341/469 4bit offset_24 */
  1537. mmio_clrsetbits_32(PHY_REG(i, 85), 0xf << 24, tmp << 24);
  1538. mmio_clrsetbits_32(PHY_REG(i, 213), 0xf << 24, tmp << 24);
  1539. mmio_clrsetbits_32(PHY_REG(i, 341), 0xf << 24, tmp << 24);
  1540. mmio_clrsetbits_32(PHY_REG(i, 469), 0xf << 24, tmp << 24);
  1541. if (pdram_timing->mhz <= ENPER_CS_TRAINING_FREQ) {
  1542. /*
  1543. * Note:Per-CS Training is not compatible at speeds
  1544. * under 533 MHz. If the PHY is running at a speed
  1545. * less than 533MHz, all phy_per_cs_training_en_X
  1546. * parameters must be cleared to 0.
  1547. */
  1548. /*DENALI_PHY_84/212/340/468 1bit offset_16 */
  1549. mmio_clrbits_32(PHY_REG(i, 84), 0x1 << 16);
  1550. mmio_clrbits_32(PHY_REG(i, 212), 0x1 << 16);
  1551. mmio_clrbits_32(PHY_REG(i, 340), 0x1 << 16);
  1552. mmio_clrbits_32(PHY_REG(i, 468), 0x1 << 16);
  1553. } else {
  1554. mmio_setbits_32(PHY_REG(i, 84), 0x1 << 16);
  1555. mmio_setbits_32(PHY_REG(i, 212), 0x1 << 16);
  1556. mmio_setbits_32(PHY_REG(i, 340), 0x1 << 16);
  1557. mmio_setbits_32(PHY_REG(i, 468), 0x1 << 16);
  1558. }
  1559. gen_rk3399_phy_dll_bypass(pdram_timing->mhz, i, fn,
  1560. timing_config->dram_type);
  1561. }
  1562. }
  1563. static int to_get_clk_index(unsigned int mhz)
  1564. {
  1565. int pll_cnt, i;
  1566. pll_cnt = ARRAY_SIZE(dpll_rates_table);
  1567. /* Assuming rate_table is in descending order */
  1568. for (i = 0; i < pll_cnt; i++) {
  1569. if (mhz >= dpll_rates_table[i].mhz)
  1570. break;
  1571. }
  1572. /* if mhz lower than lowest frequency in table, use lowest frequency */
  1573. if (i == pll_cnt)
  1574. i = pll_cnt - 1;
  1575. return i;
  1576. }
  1577. uint32_t ddr_get_rate(void)
  1578. {
  1579. uint32_t refdiv, postdiv1, fbdiv, postdiv2;
  1580. refdiv = mmio_read_32(CRU_BASE + CRU_PLL_CON(DPLL_ID, 1)) & 0x3f;
  1581. fbdiv = mmio_read_32(CRU_BASE + CRU_PLL_CON(DPLL_ID, 0)) & 0xfff;
  1582. postdiv1 =
  1583. (mmio_read_32(CRU_BASE + CRU_PLL_CON(DPLL_ID, 1)) >> 8) & 0x7;
  1584. postdiv2 =
  1585. (mmio_read_32(CRU_BASE + CRU_PLL_CON(DPLL_ID, 1)) >> 12) & 0x7;
  1586. return (24 / refdiv * fbdiv / postdiv1 / postdiv2) * 1000 * 1000;
  1587. }
  1588. /*
  1589. * return: bit12: channel 1, external self-refresh
  1590. * bit11: channel 1, stdby_mode
  1591. * bit10: channel 1, self-refresh with controller and memory clock gate
  1592. * bit9: channel 1, self-refresh
  1593. * bit8: channel 1, power-down
  1594. *
  1595. * bit4: channel 1, external self-refresh
  1596. * bit3: channel 0, stdby_mode
  1597. * bit2: channel 0, self-refresh with controller and memory clock gate
  1598. * bit1: channel 0, self-refresh
  1599. * bit0: channel 0, power-down
  1600. */
  1601. uint32_t exit_low_power(void)
  1602. {
  1603. uint32_t low_power = 0;
  1604. uint32_t channel_mask;
  1605. uint32_t tmp, i;
  1606. channel_mask = (mmio_read_32(PMUGRF_BASE + PMUGRF_OSREG(2)) >> 28) &
  1607. 0x3;
  1608. for (i = 0; i < 2; i++) {
  1609. if (!(channel_mask & (1 << i)))
  1610. continue;
  1611. /* exit stdby mode */
  1612. mmio_write_32(CIC_BASE + CIC_CTRL1,
  1613. (1 << (i + 16)) | (0 << i));
  1614. /* exit external self-refresh */
  1615. tmp = i ? 12 : 8;
  1616. low_power |= ((mmio_read_32(PMU_BASE + PMU_SFT_CON) >> tmp) &
  1617. 0x1) << (4 + 8 * i);
  1618. mmio_clrbits_32(PMU_BASE + PMU_SFT_CON, 1 << tmp);
  1619. while (!(mmio_read_32(PMU_BASE + PMU_DDR_SREF_ST) & (1 << i)))
  1620. ;
  1621. /* exit auto low-power */
  1622. mmio_clrbits_32(CTL_REG(i, 101), 0x7);
  1623. /* lp_cmd to exit */
  1624. if (((mmio_read_32(CTL_REG(i, 100)) >> 24) & 0x7f) !=
  1625. 0x40) {
  1626. while (mmio_read_32(CTL_REG(i, 200)) & 0x1)
  1627. ;
  1628. mmio_clrsetbits_32(CTL_REG(i, 93), 0xffu << 24,
  1629. 0x69 << 24);
  1630. while (((mmio_read_32(CTL_REG(i, 100)) >> 24) & 0x7f) !=
  1631. 0x40)
  1632. ;
  1633. }
  1634. }
  1635. return low_power;
  1636. }
  1637. void resume_low_power(uint32_t low_power)
  1638. {
  1639. uint32_t channel_mask;
  1640. uint32_t tmp, i, val;
  1641. channel_mask = (mmio_read_32(PMUGRF_BASE + PMUGRF_OSREG(2)) >> 28) &
  1642. 0x3;
  1643. for (i = 0; i < 2; i++) {
  1644. if (!(channel_mask & (1 << i)))
  1645. continue;
  1646. /* resume external self-refresh */
  1647. tmp = i ? 12 : 8;
  1648. val = (low_power >> (4 + 8 * i)) & 0x1;
  1649. mmio_setbits_32(PMU_BASE + PMU_SFT_CON, val << tmp);
  1650. /* resume auto low-power */
  1651. val = (low_power >> (8 * i)) & 0x7;
  1652. mmio_setbits_32(CTL_REG(i, 101), val);
  1653. /* resume stdby mode */
  1654. val = (low_power >> (3 + 8 * i)) & 0x1;
  1655. mmio_write_32(CIC_BASE + CIC_CTRL1,
  1656. (1 << (i + 16)) | (val << i));
  1657. }
  1658. }
  1659. static void dram_low_power_config(void)
  1660. {
  1661. uint32_t tmp, i;
  1662. uint32_t ch_cnt = rk3399_dram_status.timing_config.ch_cnt;
  1663. uint32_t dram_type = rk3399_dram_status.timing_config.dram_type;
  1664. if (dram_type == DDR3)
  1665. tmp = (2 << 16) | (0x7 << 8);
  1666. else
  1667. tmp = (3 << 16) | (0x7 << 8);
  1668. for (i = 0; i < ch_cnt; i++)
  1669. mmio_clrsetbits_32(CTL_REG(i, 101), 0x70f0f, tmp);
  1670. /* standby idle */
  1671. mmio_write_32(CIC_BASE + CIC_CG_WAIT_TH, 0x640008);
  1672. if (ch_cnt == 2) {
  1673. mmio_write_32(GRF_BASE + GRF_DDRC1_CON1,
  1674. (((0x1<<4) | (0x1<<5) | (0x1<<6) |
  1675. (0x1<<7)) << 16) |
  1676. ((0x1<<4) | (0x0<<5) | (0x1<<6) | (0x1<<7)));
  1677. mmio_write_32(CIC_BASE + CIC_CTRL1, 0x002a0028);
  1678. }
  1679. mmio_write_32(GRF_BASE + GRF_DDRC0_CON1,
  1680. (((0x1<<4) | (0x1<<5) | (0x1<<6) | (0x1<<7)) << 16) |
  1681. ((0x1<<4) | (0x0<<5) | (0x1<<6) | (0x1<<7)));
  1682. mmio_write_32(CIC_BASE + CIC_CTRL1, 0x00150014);
  1683. }
  1684. void dram_dfs_init(void)
  1685. {
  1686. uint32_t trefi0, trefi1, boot_freq;
  1687. uint32_t rddqs_adjust, rddqs_slave;
  1688. /* get sdram config for os reg */
  1689. get_dram_drv_odt_val(sdram_config.dramtype,
  1690. &rk3399_dram_status.drv_odt_lp_cfg);
  1691. sdram_timing_cfg_init(&rk3399_dram_status.timing_config,
  1692. &sdram_config,
  1693. &rk3399_dram_status.drv_odt_lp_cfg);
  1694. trefi0 = ((mmio_read_32(CTL_REG(0, 48)) >> 16) & 0xffff) + 8;
  1695. trefi1 = ((mmio_read_32(CTL_REG(0, 49)) >> 16) & 0xffff) + 8;
  1696. rk3399_dram_status.index_freq[0] = trefi0 * 10 / 39;
  1697. rk3399_dram_status.index_freq[1] = trefi1 * 10 / 39;
  1698. rk3399_dram_status.current_index =
  1699. (mmio_read_32(CTL_REG(0, 111)) >> 16) & 0x3;
  1700. if (rk3399_dram_status.timing_config.dram_type == DDR3) {
  1701. rk3399_dram_status.index_freq[0] /= 2;
  1702. rk3399_dram_status.index_freq[1] /= 2;
  1703. }
  1704. boot_freq =
  1705. rk3399_dram_status.index_freq[rk3399_dram_status.current_index];
  1706. boot_freq = dpll_rates_table[to_get_clk_index(boot_freq)].mhz;
  1707. rk3399_dram_status.boot_freq = boot_freq;
  1708. rk3399_dram_status.index_freq[rk3399_dram_status.current_index] =
  1709. boot_freq;
  1710. rk3399_dram_status.index_freq[(rk3399_dram_status.current_index + 1) &
  1711. 0x1] = 0;
  1712. rk3399_dram_status.low_power_stat = 0;
  1713. /*
  1714. * following register decide if NOC stall the access request
  1715. * or return error when NOC being idled. when doing ddr frequency
  1716. * scaling in M0 or DCF, we need to make sure noc stall the access
  1717. * request, if return error cpu may data abort when ddr frequency
  1718. * changing. it don't need to set this register every times,
  1719. * so we init this register in function dram_dfs_init().
  1720. */
  1721. mmio_write_32(GRF_BASE + GRF_SOC_CON(0), 0xffffffff);
  1722. mmio_write_32(GRF_BASE + GRF_SOC_CON(1), 0xffffffff);
  1723. mmio_write_32(GRF_BASE + GRF_SOC_CON(2), 0xffffffff);
  1724. mmio_write_32(GRF_BASE + GRF_SOC_CON(3), 0xffffffff);
  1725. mmio_write_32(GRF_BASE + GRF_SOC_CON(4), 0x70007000);
  1726. /* Disable multicast */
  1727. mmio_clrbits_32(PHY_REG(0, 896), 1);
  1728. mmio_clrbits_32(PHY_REG(1, 896), 1);
  1729. dram_low_power_config();
  1730. /*
  1731. * If boot_freq isn't in the bypass mode, it can get the
  1732. * rddqs_delay_ps from the result of gate training
  1733. */
  1734. if (((mmio_read_32(PHY_REG(0, 86)) >> 8) & 0xf) != 0xc) {
  1735. /*
  1736. * Select PHY's frequency set to current_index
  1737. * index for get the result of gate Training
  1738. * from registers
  1739. */
  1740. mmio_clrsetbits_32(PHY_REG(0, 896), 0x3 << 8,
  1741. rk3399_dram_status.current_index << 8);
  1742. rddqs_slave = (mmio_read_32(PHY_REG(0, 77)) >> 16) & 0x3ff;
  1743. rddqs_slave = rddqs_slave * 1000000 / boot_freq / 512;
  1744. rddqs_adjust = mmio_read_32(PHY_REG(0, 78)) & 0xf;
  1745. rddqs_adjust = rddqs_adjust * 1000000 / boot_freq;
  1746. rddqs_delay_ps = rddqs_slave + rddqs_adjust -
  1747. (1000000 / boot_freq / 2);
  1748. } else {
  1749. rddqs_delay_ps = 3500;
  1750. }
  1751. }
  1752. /*
  1753. * arg0: bit0-7: sr_idle; bit8-15:sr_mc_gate_idle; bit16-31: standby idle
  1754. * arg1: bit0-11: pd_idle; bit 16-27: srpd_lite_idle
  1755. * arg2: bit0: if odt en
  1756. */
  1757. uint32_t dram_set_odt_pd(uint32_t arg0, uint32_t arg1, uint32_t arg2)
  1758. {
  1759. struct drv_odt_lp_config *lp_cfg = &rk3399_dram_status.drv_odt_lp_cfg;
  1760. uint32_t *low_power = &rk3399_dram_status.low_power_stat;
  1761. uint32_t dram_type, ch_count, pd_tmp, sr_tmp, i;
  1762. dram_type = rk3399_dram_status.timing_config.dram_type;
  1763. ch_count = rk3399_dram_status.timing_config.ch_cnt;
  1764. lp_cfg->sr_idle = arg0 & 0xff;
  1765. lp_cfg->sr_mc_gate_idle = (arg0 >> 8) & 0xff;
  1766. lp_cfg->standby_idle = (arg0 >> 16) & 0xffff;
  1767. lp_cfg->pd_idle = arg1 & 0xfff;
  1768. lp_cfg->srpd_lite_idle = (arg1 >> 16) & 0xfff;
  1769. rk3399_dram_status.timing_config.odt = arg2 & 0x1;
  1770. exit_low_power();
  1771. *low_power = 0;
  1772. /* pd_idle en */
  1773. if (lp_cfg->pd_idle)
  1774. *low_power |= ((1 << 0) | (1 << 8));
  1775. /* sr_idle en srpd_lite_idle */
  1776. if (lp_cfg->sr_idle | lp_cfg->srpd_lite_idle)
  1777. *low_power |= ((1 << 1) | (1 << 9));
  1778. /* sr_mc_gate_idle */
  1779. if (lp_cfg->sr_mc_gate_idle)
  1780. *low_power |= ((1 << 2) | (1 << 10));
  1781. /* standbyidle */
  1782. if (lp_cfg->standby_idle) {
  1783. if (rk3399_dram_status.timing_config.ch_cnt == 2)
  1784. *low_power |= ((1 << 3) | (1 << 11));
  1785. else
  1786. *low_power |= (1 << 3);
  1787. }
  1788. pd_tmp = arg1;
  1789. if (dram_type != LPDDR4)
  1790. pd_tmp = arg1 & 0xfff;
  1791. sr_tmp = arg0 & 0xffff;
  1792. for (i = 0; i < ch_count; i++) {
  1793. mmio_write_32(CTL_REG(i, 102), pd_tmp);
  1794. mmio_clrsetbits_32(CTL_REG(i, 103), 0xffff, sr_tmp);
  1795. }
  1796. mmio_write_32(CIC_BASE + CIC_IDLE_TH, (arg0 >> 16) & 0xffff);
  1797. return 0;
  1798. }
  1799. static void m0_configure_ddr(struct pll_div pll_div, uint32_t ddr_index)
  1800. {
  1801. mmio_write_32(M0_PARAM_ADDR + PARAM_DPLL_CON0, FBDIV(pll_div.fbdiv));
  1802. mmio_write_32(M0_PARAM_ADDR + PARAM_DPLL_CON1,
  1803. POSTDIV2(pll_div.postdiv2) | POSTDIV1(pll_div.postdiv1) |
  1804. REFDIV(pll_div.refdiv));
  1805. mmio_write_32(M0_PARAM_ADDR + PARAM_DRAM_FREQ, pll_div.mhz);
  1806. mmio_write_32(M0_PARAM_ADDR + PARAM_FREQ_SELECT, ddr_index << 4);
  1807. dmbst();
  1808. m0_configure_execute_addr(M0_BINCODE_BASE);
  1809. }
  1810. static uint32_t prepare_ddr_timing(uint32_t mhz)
  1811. {
  1812. uint32_t index;
  1813. struct dram_timing_t dram_timing;
  1814. rk3399_dram_status.timing_config.freq = mhz;
  1815. if (mhz < 300)
  1816. rk3399_dram_status.timing_config.dllbp = 1;
  1817. else
  1818. rk3399_dram_status.timing_config.dllbp = 0;
  1819. if (rk3399_dram_status.timing_config.odt == 1)
  1820. gen_rk3399_set_odt(1);
  1821. index = (rk3399_dram_status.current_index + 1) & 0x1;
  1822. /*
  1823. * checking if having available gate traiing timing for
  1824. * target freq.
  1825. */
  1826. dram_get_parameter(&rk3399_dram_status.timing_config, &dram_timing);
  1827. gen_rk3399_ctl_params(&rk3399_dram_status.timing_config,
  1828. &dram_timing, index);
  1829. gen_rk3399_pi_params(&rk3399_dram_status.timing_config,
  1830. &dram_timing, index);
  1831. gen_rk3399_phy_params(&rk3399_dram_status.timing_config,
  1832. &rk3399_dram_status.drv_odt_lp_cfg,
  1833. &dram_timing, index);
  1834. rk3399_dram_status.index_freq[index] = mhz;
  1835. return index;
  1836. }
  1837. uint32_t ddr_set_rate(uint32_t hz)
  1838. {
  1839. uint32_t low_power, index, ddr_index;
  1840. uint32_t mhz = hz / (1000 * 1000);
  1841. if (mhz ==
  1842. rk3399_dram_status.index_freq[rk3399_dram_status.current_index])
  1843. return mhz;
  1844. index = to_get_clk_index(mhz);
  1845. mhz = dpll_rates_table[index].mhz;
  1846. ddr_index = prepare_ddr_timing(mhz);
  1847. gen_rk3399_enable_training(rk3399_dram_status.timing_config.ch_cnt,
  1848. mhz);
  1849. if (ddr_index > 1)
  1850. goto out;
  1851. /*
  1852. * Make sure the clock is enabled. The M0 clocks should be on all of the
  1853. * time during S0.
  1854. */
  1855. m0_configure_ddr(dpll_rates_table[index], ddr_index);
  1856. m0_start();
  1857. m0_wait_done();
  1858. m0_stop();
  1859. if (rk3399_dram_status.timing_config.odt == 0)
  1860. gen_rk3399_set_odt(0);
  1861. rk3399_dram_status.current_index = ddr_index;
  1862. low_power = rk3399_dram_status.low_power_stat;
  1863. resume_low_power(low_power);
  1864. out:
  1865. gen_rk3399_disable_training(rk3399_dram_status.timing_config.ch_cnt);
  1866. return mhz;
  1867. }
  1868. uint32_t ddr_round_rate(uint32_t hz)
  1869. {
  1870. int index;
  1871. uint32_t mhz = hz / (1000 * 1000);
  1872. index = to_get_clk_index(mhz);
  1873. return dpll_rates_table[index].mhz * 1000 * 1000;
  1874. }
  1875. void ddr_prepare_for_sys_suspend(void)
  1876. {
  1877. uint32_t mhz =
  1878. rk3399_dram_status.index_freq[rk3399_dram_status.current_index];
  1879. /*
  1880. * If we're not currently at the boot (assumed highest) frequency, we
  1881. * need to change frequencies to configure out current index.
  1882. */
  1883. rk3399_suspend_status.freq = mhz;
  1884. exit_low_power();
  1885. rk3399_suspend_status.low_power_stat =
  1886. rk3399_dram_status.low_power_stat;
  1887. rk3399_suspend_status.odt = rk3399_dram_status.timing_config.odt;
  1888. rk3399_dram_status.low_power_stat = 0;
  1889. rk3399_dram_status.timing_config.odt = 1;
  1890. if (mhz != rk3399_dram_status.boot_freq)
  1891. ddr_set_rate(rk3399_dram_status.boot_freq * 1000 * 1000);
  1892. /*
  1893. * This will configure the other index to be the same frequency as the
  1894. * current one. We retrain both indices on resume, so both have to be
  1895. * setup for the same frequency.
  1896. */
  1897. prepare_ddr_timing(rk3399_dram_status.boot_freq);
  1898. }
  1899. void ddr_prepare_for_sys_resume(void)
  1900. {
  1901. /* Disable multicast */
  1902. mmio_clrbits_32(PHY_REG(0, 896), 1);
  1903. mmio_clrbits_32(PHY_REG(1, 896), 1);
  1904. /* The suspend code changes the current index, so reset it now. */
  1905. rk3399_dram_status.current_index =
  1906. (mmio_read_32(CTL_REG(0, 111)) >> 16) & 0x3;
  1907. rk3399_dram_status.low_power_stat =
  1908. rk3399_suspend_status.low_power_stat;
  1909. rk3399_dram_status.timing_config.odt = rk3399_suspend_status.odt;
  1910. /*
  1911. * Set the saved frequency from suspend if it's different than the
  1912. * current frequency.
  1913. */
  1914. if (rk3399_suspend_status.freq !=
  1915. rk3399_dram_status.index_freq[rk3399_dram_status.current_index]) {
  1916. ddr_set_rate(rk3399_suspend_status.freq * 1000 * 1000);
  1917. return;
  1918. }
  1919. gen_rk3399_set_odt(rk3399_dram_status.timing_config.odt);
  1920. resume_low_power(rk3399_dram_status.low_power_stat);
  1921. }