emif-common.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482
  1. /*
  2. * EMIF programming
  3. *
  4. * (C) Copyright 2010
  5. * Texas Instruments, <www.ti.com>
  6. *
  7. * Aneesh V <aneesh@ti.com>
  8. *
  9. * SPDX-License-Identifier: GPL-2.0+
  10. */
  11. #include <common.h>
  12. #include <asm/emif.h>
  13. #include <asm/arch/clock.h>
  14. #include <asm/arch/sys_proto.h>
  15. #include <asm/omap_common.h>
  16. #include <asm/utils.h>
  17. #include <linux/compiler.h>
  18. static int emif1_enabled = -1, emif2_enabled = -1;
  19. void set_lpmode_selfrefresh(u32 base)
  20. {
  21. struct emif_reg_struct *emif = (struct emif_reg_struct *)base;
  22. u32 reg;
  23. reg = readl(&emif->emif_pwr_mgmt_ctrl);
  24. reg &= ~EMIF_REG_LP_MODE_MASK;
  25. reg |= LP_MODE_SELF_REFRESH << EMIF_REG_LP_MODE_SHIFT;
  26. reg &= ~EMIF_REG_SR_TIM_MASK;
  27. writel(reg, &emif->emif_pwr_mgmt_ctrl);
  28. /* dummy read for the new SR_TIM to be loaded */
  29. readl(&emif->emif_pwr_mgmt_ctrl);
  30. }
  31. void force_emif_self_refresh()
  32. {
  33. set_lpmode_selfrefresh(EMIF1_BASE);
  34. set_lpmode_selfrefresh(EMIF2_BASE);
  35. }
  36. inline u32 emif_num(u32 base)
  37. {
  38. if (base == EMIF1_BASE)
  39. return 1;
  40. else if (base == EMIF2_BASE)
  41. return 2;
  42. else
  43. return 0;
  44. }
  45. static inline u32 get_mr(u32 base, u32 cs, u32 mr_addr)
  46. {
  47. u32 mr;
  48. struct emif_reg_struct *emif = (struct emif_reg_struct *)base;
  49. mr_addr |= cs << EMIF_REG_CS_SHIFT;
  50. writel(mr_addr, &emif->emif_lpddr2_mode_reg_cfg);
  51. if (omap_revision() == OMAP4430_ES2_0)
  52. mr = readl(&emif->emif_lpddr2_mode_reg_data_es2);
  53. else
  54. mr = readl(&emif->emif_lpddr2_mode_reg_data);
  55. debug("get_mr: EMIF%d cs %d mr %08x val 0x%x\n", emif_num(base),
  56. cs, mr_addr, mr);
  57. if (((mr & 0x0000ff00) >> 8) == (mr & 0xff) &&
  58. ((mr & 0x00ff0000) >> 16) == (mr & 0xff) &&
  59. ((mr & 0xff000000) >> 24) == (mr & 0xff))
  60. return mr & 0xff;
  61. else
  62. return mr;
  63. }
  64. static inline void set_mr(u32 base, u32 cs, u32 mr_addr, u32 mr_val)
  65. {
  66. struct emif_reg_struct *emif = (struct emif_reg_struct *)base;
  67. mr_addr |= cs << EMIF_REG_CS_SHIFT;
  68. writel(mr_addr, &emif->emif_lpddr2_mode_reg_cfg);
  69. writel(mr_val, &emif->emif_lpddr2_mode_reg_data);
  70. }
  71. void emif_reset_phy(u32 base)
  72. {
  73. struct emif_reg_struct *emif = (struct emif_reg_struct *)base;
  74. u32 iodft;
  75. iodft = readl(&emif->emif_iodft_tlgc);
  76. iodft |= EMIF_REG_RESET_PHY_MASK;
  77. writel(iodft, &emif->emif_iodft_tlgc);
  78. }
  79. static void do_lpddr2_init(u32 base, u32 cs)
  80. {
  81. u32 mr_addr;
  82. const struct lpddr2_mr_regs *mr_regs;
  83. get_lpddr2_mr_regs(&mr_regs);
  84. /* Wait till device auto initialization is complete */
  85. while (get_mr(base, cs, LPDDR2_MR0) & LPDDR2_MR0_DAI_MASK)
  86. ;
  87. set_mr(base, cs, LPDDR2_MR10, mr_regs->mr10);
  88. /*
  89. * tZQINIT = 1 us
  90. * Enough loops assuming a maximum of 2GHz
  91. */
  92. sdelay(2000);
  93. set_mr(base, cs, LPDDR2_MR1, mr_regs->mr1);
  94. set_mr(base, cs, LPDDR2_MR16, mr_regs->mr16);
  95. /*
  96. * Enable refresh along with writing MR2
  97. * Encoding of RL in MR2 is (RL - 2)
  98. */
  99. mr_addr = LPDDR2_MR2 | EMIF_REG_REFRESH_EN_MASK;
  100. set_mr(base, cs, mr_addr, mr_regs->mr2);
  101. if (mr_regs->mr3 > 0)
  102. set_mr(base, cs, LPDDR2_MR3, mr_regs->mr3);
  103. }
  104. static void lpddr2_init(u32 base, const struct emif_regs *regs)
  105. {
  106. struct emif_reg_struct *emif = (struct emif_reg_struct *)base;
  107. /* Not NVM */
  108. clrbits_le32(&emif->emif_lpddr2_nvm_config, EMIF_REG_CS1NVMEN_MASK);
  109. /*
  110. * Keep REG_INITREF_DIS = 1 to prevent re-initialization of SDRAM
  111. * when EMIF_SDRAM_CONFIG register is written
  112. */
  113. setbits_le32(&emif->emif_sdram_ref_ctrl, EMIF_REG_INITREF_DIS_MASK);
  114. /*
  115. * Set the SDRAM_CONFIG and PHY_CTRL for the
  116. * un-locked frequency & default RL
  117. */
  118. writel(regs->sdram_config_init, &emif->emif_sdram_config);
  119. writel(regs->emif_ddr_phy_ctlr_1_init, &emif->emif_ddr_phy_ctrl_1);
  120. do_ext_phy_settings(base, regs);
  121. do_lpddr2_init(base, CS0);
  122. if (regs->sdram_config & EMIF_REG_EBANK_MASK)
  123. do_lpddr2_init(base, CS1);
  124. writel(regs->sdram_config, &emif->emif_sdram_config);
  125. writel(regs->emif_ddr_phy_ctlr_1, &emif->emif_ddr_phy_ctrl_1);
  126. /* Enable refresh now */
  127. clrbits_le32(&emif->emif_sdram_ref_ctrl, EMIF_REG_INITREF_DIS_MASK);
  128. }
  129. __weak void do_ext_phy_settings(u32 base, const struct emif_regs *regs)
  130. {
  131. }
  132. void emif_update_timings(u32 base, const struct emif_regs *regs)
  133. {
  134. struct emif_reg_struct *emif = (struct emif_reg_struct *)base;
  135. if (!is_dra7xx())
  136. writel(regs->ref_ctrl, &emif->emif_sdram_ref_ctrl_shdw);
  137. else
  138. writel(regs->ref_ctrl_final, &emif->emif_sdram_ref_ctrl_shdw);
  139. writel(regs->sdram_tim1, &emif->emif_sdram_tim_1_shdw);
  140. writel(regs->sdram_tim2, &emif->emif_sdram_tim_2_shdw);
  141. writel(regs->sdram_tim3, &emif->emif_sdram_tim_3_shdw);
  142. if (omap_revision() == OMAP4430_ES1_0) {
  143. /* ES1 bug EMIF should be in force idle during freq_update */
  144. writel(0, &emif->emif_pwr_mgmt_ctrl);
  145. } else {
  146. writel(EMIF_PWR_MGMT_CTRL, &emif->emif_pwr_mgmt_ctrl);
  147. writel(EMIF_PWR_MGMT_CTRL_SHDW, &emif->emif_pwr_mgmt_ctrl_shdw);
  148. }
  149. writel(regs->read_idle_ctrl, &emif->emif_read_idlectrl_shdw);
  150. writel(regs->zq_config, &emif->emif_zq_config);
  151. writel(regs->temp_alert_config, &emif->emif_temp_alert_config);
  152. writel(regs->emif_ddr_phy_ctlr_1, &emif->emif_ddr_phy_ctrl_1_shdw);
  153. if ((omap_revision() >= OMAP5430_ES1_0) || is_dra7xx()) {
  154. writel(EMIF_L3_CONFIG_VAL_SYS_10_MPU_5_LL_0,
  155. &emif->emif_l3_config);
  156. } else if (omap_revision() >= OMAP4460_ES1_0) {
  157. writel(EMIF_L3_CONFIG_VAL_SYS_10_MPU_3_LL_0,
  158. &emif->emif_l3_config);
  159. } else {
  160. writel(EMIF_L3_CONFIG_VAL_SYS_10_LL_0,
  161. &emif->emif_l3_config);
  162. }
  163. }
  164. static void omap5_ddr3_leveling(u32 base, const struct emif_regs *regs)
  165. {
  166. struct emif_reg_struct *emif = (struct emif_reg_struct *)base;
  167. /* keep sdram in self-refresh */
  168. writel(((LP_MODE_SELF_REFRESH << EMIF_REG_LP_MODE_SHIFT)
  169. & EMIF_REG_LP_MODE_MASK), &emif->emif_pwr_mgmt_ctrl);
  170. __udelay(130);
  171. /*
  172. * Set invert_clkout (if activated)--DDR_PHYCTRL_1
  173. * Invert clock adds an additional half cycle delay on the
  174. * command interface. The additional half cycle, is usually
  175. * meant to enable leveling in the situation that DQS is later
  176. * than CK on the board.It also helps provide some additional
  177. * margin for leveling.
  178. */
  179. writel(regs->emif_ddr_phy_ctlr_1,
  180. &emif->emif_ddr_phy_ctrl_1);
  181. writel(regs->emif_ddr_phy_ctlr_1,
  182. &emif->emif_ddr_phy_ctrl_1_shdw);
  183. __udelay(130);
  184. writel(((LP_MODE_DISABLE << EMIF_REG_LP_MODE_SHIFT)
  185. & EMIF_REG_LP_MODE_MASK), &emif->emif_pwr_mgmt_ctrl);
  186. /* Launch Full leveling */
  187. writel(DDR3_FULL_LVL, &emif->emif_rd_wr_lvl_ctl);
  188. /* Wait till full leveling is complete */
  189. readl(&emif->emif_rd_wr_lvl_ctl);
  190. __udelay(130);
  191. /* Read data eye leveling no of samples */
  192. config_data_eye_leveling_samples(base);
  193. /*
  194. * Launch 8 incremental WR_LVL- to compensate for
  195. * PHY limitation.
  196. */
  197. writel(0x2 << EMIF_REG_WRLVLINC_INT_SHIFT,
  198. &emif->emif_rd_wr_lvl_ctl);
  199. __udelay(130);
  200. /* Launch Incremental leveling */
  201. writel(DDR3_INC_LVL, &emif->emif_rd_wr_lvl_ctl);
  202. __udelay(130);
  203. }
  204. static void update_hwleveling_output(u32 base, const struct emif_regs *regs)
  205. {
  206. struct emif_reg_struct *emif = (struct emif_reg_struct *)base;
  207. u32 *emif_ext_phy_ctrl_reg, *emif_phy_status;
  208. u32 reg, i, phy;
  209. emif_phy_status = (u32 *)&emif->emif_ddr_phy_status[7];
  210. phy = readl(&emif->emif_ddr_phy_ctrl_1);
  211. /* Update PHY_REG_RDDQS_RATIO */
  212. emif_ext_phy_ctrl_reg = (u32 *)&emif->emif_ddr_ext_phy_ctrl_7;
  213. if (!(phy & EMIF_DDR_PHY_CTRL_1_RDLVL_MASK_MASK))
  214. for (i = 0; i < PHY_RDDQS_RATIO_REGS; i++) {
  215. reg = readl(emif_phy_status++);
  216. writel(reg, emif_ext_phy_ctrl_reg++);
  217. writel(reg, emif_ext_phy_ctrl_reg++);
  218. }
  219. /* Update PHY_REG_FIFO_WE_SLAVE_RATIO */
  220. emif_ext_phy_ctrl_reg = (u32 *)&emif->emif_ddr_ext_phy_ctrl_2;
  221. emif_phy_status = (u32 *)&emif->emif_ddr_phy_status[12];
  222. if (!(phy & EMIF_DDR_PHY_CTRL_1_RDLVLGATE_MASK_MASK))
  223. for (i = 0; i < PHY_FIFO_WE_SLAVE_RATIO_REGS; i++) {
  224. reg = readl(emif_phy_status++);
  225. writel(reg, emif_ext_phy_ctrl_reg++);
  226. writel(reg, emif_ext_phy_ctrl_reg++);
  227. }
  228. /* Update PHY_REG_WR_DQ/DQS_SLAVE_RATIO */
  229. emif_ext_phy_ctrl_reg = (u32 *)&emif->emif_ddr_ext_phy_ctrl_12;
  230. emif_phy_status = (u32 *)&emif->emif_ddr_phy_status[17];
  231. if (!(phy & EMIF_DDR_PHY_CTRL_1_WRLVL_MASK_MASK))
  232. for (i = 0; i < PHY_REG_WR_DQ_SLAVE_RATIO_REGS; i++) {
  233. reg = readl(emif_phy_status++);
  234. writel(reg, emif_ext_phy_ctrl_reg++);
  235. writel(reg, emif_ext_phy_ctrl_reg++);
  236. }
  237. /* Disable Leveling */
  238. writel(regs->emif_ddr_phy_ctlr_1, &emif->emif_ddr_phy_ctrl_1);
  239. writel(regs->emif_ddr_phy_ctlr_1, &emif->emif_ddr_phy_ctrl_1_shdw);
  240. writel(0x0, &emif->emif_rd_wr_lvl_rmp_ctl);
  241. }
  242. static void dra7_ddr3_leveling(u32 base, const struct emif_regs *regs)
  243. {
  244. struct emif_reg_struct *emif = (struct emif_reg_struct *)base;
  245. /* Clear Error Status */
  246. clrsetbits_le32(&emif->emif_ddr_ext_phy_ctrl_36,
  247. EMIF_REG_PHY_FIFO_WE_IN_MISALINED_CLR,
  248. EMIF_REG_PHY_FIFO_WE_IN_MISALINED_CLR);
  249. clrsetbits_le32(&emif->emif_ddr_ext_phy_ctrl_36_shdw,
  250. EMIF_REG_PHY_FIFO_WE_IN_MISALINED_CLR,
  251. EMIF_REG_PHY_FIFO_WE_IN_MISALINED_CLR);
  252. /* Disable refreshed before leveling */
  253. clrsetbits_le32(&emif->emif_sdram_ref_ctrl, EMIF_REG_INITREF_DIS_MASK,
  254. EMIF_REG_INITREF_DIS_MASK);
  255. /* Start Full leveling */
  256. writel(DDR3_FULL_LVL, &emif->emif_rd_wr_lvl_ctl);
  257. __udelay(300);
  258. /* Check for leveling timeout */
  259. if (readl(&emif->emif_status) & EMIF_REG_LEVELING_TO_MASK) {
  260. printf("Leveling timeout on EMIF%d\n", emif_num(base));
  261. return;
  262. }
  263. /* Enable refreshes after leveling */
  264. clrbits_le32(&emif->emif_sdram_ref_ctrl, EMIF_REG_INITREF_DIS_MASK);
  265. debug("HW leveling success\n");
  266. /*
  267. * Update slave ratios in EXT_PHY_CTRLx registers
  268. * as per HW leveling output
  269. */
  270. update_hwleveling_output(base, regs);
  271. }
  272. static void dra7_ddr3_init(u32 base, const struct emif_regs *regs)
  273. {
  274. struct emif_reg_struct *emif = (struct emif_reg_struct *)base;
  275. if (warm_reset()) {
  276. emif_reset_phy(base);
  277. writel(0x0, &emif->emif_pwr_mgmt_ctrl);
  278. }
  279. do_ext_phy_settings(base, regs);
  280. writel(regs->ref_ctrl | EMIF_REG_INITREF_DIS_MASK,
  281. &emif->emif_sdram_ref_ctrl);
  282. /* Update timing registers */
  283. writel(regs->sdram_tim1, &emif->emif_sdram_tim_1);
  284. writel(regs->sdram_tim2, &emif->emif_sdram_tim_2);
  285. writel(regs->sdram_tim3, &emif->emif_sdram_tim_3);
  286. writel(EMIF_L3_CONFIG_VAL_SYS_10_MPU_5_LL_0, &emif->emif_l3_config);
  287. writel(regs->read_idle_ctrl, &emif->emif_read_idlectrl);
  288. writel(regs->zq_config, &emif->emif_zq_config);
  289. writel(regs->temp_alert_config, &emif->emif_temp_alert_config);
  290. writel(regs->emif_rd_wr_lvl_rmp_ctl, &emif->emif_rd_wr_lvl_rmp_ctl);
  291. writel(regs->emif_rd_wr_lvl_ctl, &emif->emif_rd_wr_lvl_ctl);
  292. writel(regs->emif_ddr_phy_ctlr_1_init, &emif->emif_ddr_phy_ctrl_1);
  293. writel(regs->emif_rd_wr_exec_thresh, &emif->emif_rd_wr_exec_thresh);
  294. writel(regs->ref_ctrl, &emif->emif_sdram_ref_ctrl);
  295. writel(regs->sdram_config2, &emif->emif_lpddr2_nvm_config);
  296. writel(regs->sdram_config_init, &emif->emif_sdram_config);
  297. __udelay(1000);
  298. writel(regs->ref_ctrl_final, &emif->emif_sdram_ref_ctrl);
  299. if (regs->emif_rd_wr_lvl_rmp_ctl & EMIF_REG_RDWRLVL_EN_MASK)
  300. dra7_ddr3_leveling(base, regs);
  301. }
  302. static void omap5_ddr3_init(u32 base, const struct emif_regs *regs)
  303. {
  304. struct emif_reg_struct *emif = (struct emif_reg_struct *)base;
  305. writel(regs->ref_ctrl, &emif->emif_sdram_ref_ctrl);
  306. writel(regs->sdram_config_init, &emif->emif_sdram_config);
  307. /*
  308. * Set SDRAM_CONFIG and PHY control registers to locked frequency
  309. * and RL =7. As the default values of the Mode Registers are not
  310. * defined, contents of mode Registers must be fully initialized.
  311. * H/W takes care of this initialization
  312. */
  313. writel(regs->emif_ddr_phy_ctlr_1_init, &emif->emif_ddr_phy_ctrl_1);
  314. /* Update timing registers */
  315. writel(regs->sdram_tim1, &emif->emif_sdram_tim_1);
  316. writel(regs->sdram_tim2, &emif->emif_sdram_tim_2);
  317. writel(regs->sdram_tim3, &emif->emif_sdram_tim_3);
  318. writel(regs->read_idle_ctrl, &emif->emif_read_idlectrl);
  319. writel(regs->sdram_config2, &emif->emif_lpddr2_nvm_config);
  320. writel(regs->sdram_config_init, &emif->emif_sdram_config);
  321. do_ext_phy_settings(base, regs);
  322. writel(regs->emif_rd_wr_lvl_rmp_ctl, &emif->emif_rd_wr_lvl_rmp_ctl);
  323. omap5_ddr3_leveling(base, regs);
  324. }
  325. static void ddr3_init(u32 base, const struct emif_regs *regs)
  326. {
  327. if (is_omap54xx())
  328. omap5_ddr3_init(base, regs);
  329. else
  330. dra7_ddr3_init(base, regs);
  331. }
  332. #ifndef CONFIG_SYS_EMIF_PRECALCULATED_TIMING_REGS
  333. #define print_timing_reg(reg) debug(#reg" - 0x%08x\n", (reg))
  334. /*
  335. * Organization and refresh requirements for LPDDR2 devices of different
  336. * types and densities. Derived from JESD209-2 section 2.4
  337. */
  338. const struct lpddr2_addressing addressing_table[] = {
  339. /* Banks tREFIx10 rowx32,rowx16 colx32,colx16 density */
  340. {BANKS4, T_REFI_15_6, {ROW_12, ROW_12}, {COL_7, COL_8} },/*64M */
  341. {BANKS4, T_REFI_15_6, {ROW_12, ROW_12}, {COL_8, COL_9} },/*128M */
  342. {BANKS4, T_REFI_7_8, {ROW_13, ROW_13}, {COL_8, COL_9} },/*256M */
  343. {BANKS4, T_REFI_7_8, {ROW_13, ROW_13}, {COL_9, COL_10} },/*512M */
  344. {BANKS8, T_REFI_7_8, {ROW_13, ROW_13}, {COL_9, COL_10} },/*1GS4 */
  345. {BANKS8, T_REFI_3_9, {ROW_14, ROW_14}, {COL_9, COL_10} },/*2GS4 */
  346. {BANKS8, T_REFI_3_9, {ROW_14, ROW_14}, {COL_10, COL_11} },/*4G */
  347. {BANKS8, T_REFI_3_9, {ROW_15, ROW_15}, {COL_10, COL_11} },/*8G */
  348. {BANKS4, T_REFI_7_8, {ROW_14, ROW_14}, {COL_9, COL_10} },/*1GS2 */
  349. {BANKS4, T_REFI_3_9, {ROW_15, ROW_15}, {COL_9, COL_10} },/*2GS2 */
  350. };
  351. static const u32 lpddr2_density_2_size_in_mbytes[] = {
  352. 8, /* 64Mb */
  353. 16, /* 128Mb */
  354. 32, /* 256Mb */
  355. 64, /* 512Mb */
  356. 128, /* 1Gb */
  357. 256, /* 2Gb */
  358. 512, /* 4Gb */
  359. 1024, /* 8Gb */
  360. 2048, /* 16Gb */
  361. 4096 /* 32Gb */
  362. };
  363. /*
  364. * Calculate the period of DDR clock from frequency value and set the
  365. * denominator and numerator in global variables for easy access later
  366. */
  367. static void set_ddr_clk_period(u32 freq)
  368. {
  369. /*
  370. * period = 1/freq
  371. * period_in_ns = 10^9/freq
  372. */
  373. *T_num = 1000000000;
  374. *T_den = freq;
  375. cancel_out(T_num, T_den, 200);
  376. }
  377. /*
  378. * Convert time in nano seconds to number of cycles of DDR clock
  379. */
  380. static inline u32 ns_2_cycles(u32 ns)
  381. {
  382. return ((ns * (*T_den)) + (*T_num) - 1) / (*T_num);
  383. }
  384. /*
  385. * ns_2_cycles with the difference that the time passed is 2 times the actual
  386. * value(to avoid fractions). The cycles returned is for the original value of
  387. * the timing parameter
  388. */
  389. static inline u32 ns_x2_2_cycles(u32 ns)
  390. {
  391. return ((ns * (*T_den)) + (*T_num) * 2 - 1) / ((*T_num) * 2);
  392. }
  393. /*
  394. * Find addressing table index based on the device's type(S2 or S4) and
  395. * density
  396. */
  397. s8 addressing_table_index(u8 type, u8 density, u8 width)
  398. {
  399. u8 index;
  400. if ((density > LPDDR2_DENSITY_8Gb) || (width == LPDDR2_IO_WIDTH_8))
  401. return -1;
  402. /*
  403. * Look at the way ADDR_TABLE_INDEX* values have been defined
  404. * in emif.h compared to LPDDR2_DENSITY_* values
  405. * The table is layed out in the increasing order of density
  406. * (ignoring type). The exceptions 1GS2 and 2GS2 have been placed
  407. * at the end
  408. */
  409. if ((type == LPDDR2_TYPE_S2) && (density == LPDDR2_DENSITY_1Gb))
  410. index = ADDR_TABLE_INDEX1GS2;
  411. else if ((type == LPDDR2_TYPE_S2) && (density == LPDDR2_DENSITY_2Gb))
  412. index = ADDR_TABLE_INDEX2GS2;
  413. else
  414. index = density;
  415. debug("emif: addressing table index %d\n", index);
  416. return index;
  417. }
  418. /*
  419. * Find the the right timing table from the array of timing
  420. * tables of the device using DDR clock frequency
  421. */
  422. static const struct lpddr2_ac_timings *get_timings_table(const struct
  423. lpddr2_ac_timings const *const *device_timings,
  424. u32 freq)
  425. {
  426. u32 i, temp, freq_nearest;
  427. const struct lpddr2_ac_timings *timings = 0;
  428. emif_assert(freq <= MAX_LPDDR2_FREQ);
  429. emif_assert(device_timings);
  430. /*
  431. * Start with the maximum allowed frequency - that is always safe
  432. */
  433. freq_nearest = MAX_LPDDR2_FREQ;
  434. /*
  435. * Find the timings table that has the max frequency value:
  436. * i. Above or equal to the DDR frequency - safe
  437. * ii. The lowest that satisfies condition (i) - optimal
  438. */
  439. for (i = 0; (i < MAX_NUM_SPEEDBINS) && device_timings[i]; i++) {
  440. temp = device_timings[i]->max_freq;
  441. if ((temp >= freq) && (temp <= freq_nearest)) {
  442. freq_nearest = temp;
  443. timings = device_timings[i];
  444. }
  445. }
  446. debug("emif: timings table: %d\n", freq_nearest);
  447. return timings;
  448. }
  449. /*
  450. * Finds the value of emif_sdram_config_reg
  451. * All parameters are programmed based on the device on CS0.
  452. * If there is a device on CS1, it will be same as that on CS0 or
  453. * it will be NVM. We don't support NVM yet.
  454. * If cs1_device pointer is NULL it is assumed that there is no device
  455. * on CS1
  456. */
  457. static u32 get_sdram_config_reg(const struct lpddr2_device_details *cs0_device,
  458. const struct lpddr2_device_details *cs1_device,
  459. const struct lpddr2_addressing *addressing,
  460. u8 RL)
  461. {
  462. u32 config_reg = 0;
  463. config_reg |= (cs0_device->type + 4) << EMIF_REG_SDRAM_TYPE_SHIFT;
  464. config_reg |= EMIF_INTERLEAVING_POLICY_MAX_INTERLEAVING <<
  465. EMIF_REG_IBANK_POS_SHIFT;
  466. config_reg |= cs0_device->io_width << EMIF_REG_NARROW_MODE_SHIFT;
  467. config_reg |= RL << EMIF_REG_CL_SHIFT;
  468. config_reg |= addressing->row_sz[cs0_device->io_width] <<
  469. EMIF_REG_ROWSIZE_SHIFT;
  470. config_reg |= addressing->num_banks << EMIF_REG_IBANK_SHIFT;
  471. config_reg |= (cs1_device ? EBANK_CS1_EN : EBANK_CS1_DIS) <<
  472. EMIF_REG_EBANK_SHIFT;
  473. config_reg |= addressing->col_sz[cs0_device->io_width] <<
  474. EMIF_REG_PAGESIZE_SHIFT;
  475. return config_reg;
  476. }
  477. static u32 get_sdram_ref_ctrl(u32 freq,
  478. const struct lpddr2_addressing *addressing)
  479. {
  480. u32 ref_ctrl = 0, val = 0, freq_khz;
  481. freq_khz = freq / 1000;
  482. /*
  483. * refresh rate to be set is 'tREFI * freq in MHz
  484. * division by 10000 to account for khz and x10 in t_REFI_us_x10
  485. */
  486. val = addressing->t_REFI_us_x10 * freq_khz / 10000;
  487. ref_ctrl |= val << EMIF_REG_REFRESH_RATE_SHIFT;
  488. return ref_ctrl;
  489. }
  490. static u32 get_sdram_tim_1_reg(const struct lpddr2_ac_timings *timings,
  491. const struct lpddr2_min_tck *min_tck,
  492. const struct lpddr2_addressing *addressing)
  493. {
  494. u32 tim1 = 0, val = 0;
  495. val = max(min_tck->tWTR, ns_x2_2_cycles(timings->tWTRx2)) - 1;
  496. tim1 |= val << EMIF_REG_T_WTR_SHIFT;
  497. if (addressing->num_banks == BANKS8)
  498. val = (timings->tFAW * (*T_den) + 4 * (*T_num) - 1) /
  499. (4 * (*T_num)) - 1;
  500. else
  501. val = max(min_tck->tRRD, ns_2_cycles(timings->tRRD)) - 1;
  502. tim1 |= val << EMIF_REG_T_RRD_SHIFT;
  503. val = ns_2_cycles(timings->tRASmin + timings->tRPab) - 1;
  504. tim1 |= val << EMIF_REG_T_RC_SHIFT;
  505. val = max(min_tck->tRAS_MIN, ns_2_cycles(timings->tRASmin)) - 1;
  506. tim1 |= val << EMIF_REG_T_RAS_SHIFT;
  507. val = max(min_tck->tWR, ns_2_cycles(timings->tWR)) - 1;
  508. tim1 |= val << EMIF_REG_T_WR_SHIFT;
  509. val = max(min_tck->tRCD, ns_2_cycles(timings->tRCD)) - 1;
  510. tim1 |= val << EMIF_REG_T_RCD_SHIFT;
  511. val = max(min_tck->tRP_AB, ns_2_cycles(timings->tRPab)) - 1;
  512. tim1 |= val << EMIF_REG_T_RP_SHIFT;
  513. return tim1;
  514. }
  515. static u32 get_sdram_tim_2_reg(const struct lpddr2_ac_timings *timings,
  516. const struct lpddr2_min_tck *min_tck)
  517. {
  518. u32 tim2 = 0, val = 0;
  519. val = max(min_tck->tCKE, timings->tCKE) - 1;
  520. tim2 |= val << EMIF_REG_T_CKE_SHIFT;
  521. val = max(min_tck->tRTP, ns_x2_2_cycles(timings->tRTPx2)) - 1;
  522. tim2 |= val << EMIF_REG_T_RTP_SHIFT;
  523. /*
  524. * tXSRD = tRFCab + 10 ns. XSRD and XSNR should have the
  525. * same value
  526. */
  527. val = ns_2_cycles(timings->tXSR) - 1;
  528. tim2 |= val << EMIF_REG_T_XSRD_SHIFT;
  529. tim2 |= val << EMIF_REG_T_XSNR_SHIFT;
  530. val = max(min_tck->tXP, ns_x2_2_cycles(timings->tXPx2)) - 1;
  531. tim2 |= val << EMIF_REG_T_XP_SHIFT;
  532. return tim2;
  533. }
  534. static u32 get_sdram_tim_3_reg(const struct lpddr2_ac_timings *timings,
  535. const struct lpddr2_min_tck *min_tck,
  536. const struct lpddr2_addressing *addressing)
  537. {
  538. u32 tim3 = 0, val = 0;
  539. val = min(timings->tRASmax * 10 / addressing->t_REFI_us_x10 - 1, 0xF);
  540. tim3 |= val << EMIF_REG_T_RAS_MAX_SHIFT;
  541. val = ns_2_cycles(timings->tRFCab) - 1;
  542. tim3 |= val << EMIF_REG_T_RFC_SHIFT;
  543. val = ns_x2_2_cycles(timings->tDQSCKMAXx2) - 1;
  544. tim3 |= val << EMIF_REG_T_TDQSCKMAX_SHIFT;
  545. val = ns_2_cycles(timings->tZQCS) - 1;
  546. tim3 |= val << EMIF_REG_ZQ_ZQCS_SHIFT;
  547. val = max(min_tck->tCKESR, ns_2_cycles(timings->tCKESR)) - 1;
  548. tim3 |= val << EMIF_REG_T_CKESR_SHIFT;
  549. return tim3;
  550. }
  551. static u32 get_zq_config_reg(const struct lpddr2_device_details *cs1_device,
  552. const struct lpddr2_addressing *addressing,
  553. u8 volt_ramp)
  554. {
  555. u32 zq = 0, val = 0;
  556. if (volt_ramp)
  557. val =
  558. EMIF_ZQCS_INTERVAL_DVFS_IN_US * 10 /
  559. addressing->t_REFI_us_x10;
  560. else
  561. val =
  562. EMIF_ZQCS_INTERVAL_NORMAL_IN_US * 10 /
  563. addressing->t_REFI_us_x10;
  564. zq |= val << EMIF_REG_ZQ_REFINTERVAL_SHIFT;
  565. zq |= (REG_ZQ_ZQCL_MULT - 1) << EMIF_REG_ZQ_ZQCL_MULT_SHIFT;
  566. zq |= (REG_ZQ_ZQINIT_MULT - 1) << EMIF_REG_ZQ_ZQINIT_MULT_SHIFT;
  567. zq |= REG_ZQ_SFEXITEN_ENABLE << EMIF_REG_ZQ_SFEXITEN_SHIFT;
  568. /*
  569. * Assuming that two chipselects have a single calibration resistor
  570. * If there are indeed two calibration resistors, then this flag should
  571. * be enabled to take advantage of dual calibration feature.
  572. * This data should ideally come from board files. But considering
  573. * that none of the boards today have calibration resistors per CS,
  574. * it would be an unnecessary overhead.
  575. */
  576. zq |= REG_ZQ_DUALCALEN_DISABLE << EMIF_REG_ZQ_DUALCALEN_SHIFT;
  577. zq |= REG_ZQ_CS0EN_ENABLE << EMIF_REG_ZQ_CS0EN_SHIFT;
  578. zq |= (cs1_device ? 1 : 0) << EMIF_REG_ZQ_CS1EN_SHIFT;
  579. return zq;
  580. }
  581. static u32 get_temp_alert_config(const struct lpddr2_device_details *cs1_device,
  582. const struct lpddr2_addressing *addressing,
  583. u8 is_derated)
  584. {
  585. u32 alert = 0, interval;
  586. interval =
  587. TEMP_ALERT_POLL_INTERVAL_MS * 10000 / addressing->t_REFI_us_x10;
  588. if (is_derated)
  589. interval *= 4;
  590. alert |= interval << EMIF_REG_TA_REFINTERVAL_SHIFT;
  591. alert |= TEMP_ALERT_CONFIG_DEVCT_1 << EMIF_REG_TA_DEVCNT_SHIFT;
  592. alert |= TEMP_ALERT_CONFIG_DEVWDT_32 << EMIF_REG_TA_DEVWDT_SHIFT;
  593. alert |= 1 << EMIF_REG_TA_SFEXITEN_SHIFT;
  594. alert |= 1 << EMIF_REG_TA_CS0EN_SHIFT;
  595. alert |= (cs1_device ? 1 : 0) << EMIF_REG_TA_CS1EN_SHIFT;
  596. return alert;
  597. }
  598. static u32 get_read_idle_ctrl_reg(u8 volt_ramp)
  599. {
  600. u32 idle = 0, val = 0;
  601. if (volt_ramp)
  602. val = ns_2_cycles(READ_IDLE_INTERVAL_DVFS) / 64 - 1;
  603. else
  604. /*Maximum value in normal conditions - suggested by hw team */
  605. val = 0x1FF;
  606. idle |= val << EMIF_REG_READ_IDLE_INTERVAL_SHIFT;
  607. idle |= EMIF_REG_READ_IDLE_LEN_VAL << EMIF_REG_READ_IDLE_LEN_SHIFT;
  608. return idle;
  609. }
  610. static u32 get_ddr_phy_ctrl_1(u32 freq, u8 RL)
  611. {
  612. u32 phy = 0, val = 0;
  613. phy |= (RL + 2) << EMIF_REG_READ_LATENCY_SHIFT;
  614. if (freq <= 100000000)
  615. val = EMIF_DLL_SLAVE_DLY_CTRL_100_MHZ_AND_LESS;
  616. else if (freq <= 200000000)
  617. val = EMIF_DLL_SLAVE_DLY_CTRL_200_MHZ;
  618. else
  619. val = EMIF_DLL_SLAVE_DLY_CTRL_400_MHZ;
  620. phy |= val << EMIF_REG_DLL_SLAVE_DLY_CTRL_SHIFT;
  621. /* Other fields are constant magic values. Hardcode them together */
  622. phy |= EMIF_DDR_PHY_CTRL_1_BASE_VAL <<
  623. EMIF_EMIF_DDR_PHY_CTRL_1_BASE_VAL_SHIFT;
  624. return phy;
  625. }
  626. static u32 get_emif_mem_size(u32 base)
  627. {
  628. u32 size_mbytes = 0, temp;
  629. struct emif_device_details dev_details;
  630. struct lpddr2_device_details cs0_dev_details, cs1_dev_details;
  631. u32 emif_nr = emif_num(base);
  632. emif_reset_phy(base);
  633. dev_details.cs0_device_details = emif_get_device_details(emif_nr, CS0,
  634. &cs0_dev_details);
  635. dev_details.cs1_device_details = emif_get_device_details(emif_nr, CS1,
  636. &cs1_dev_details);
  637. emif_reset_phy(base);
  638. if (dev_details.cs0_device_details) {
  639. temp = dev_details.cs0_device_details->density;
  640. size_mbytes += lpddr2_density_2_size_in_mbytes[temp];
  641. }
  642. if (dev_details.cs1_device_details) {
  643. temp = dev_details.cs1_device_details->density;
  644. size_mbytes += lpddr2_density_2_size_in_mbytes[temp];
  645. }
  646. /* convert to bytes */
  647. return size_mbytes << 20;
  648. }
  649. /* Gets the encoding corresponding to a given DMM section size */
  650. u32 get_dmm_section_size_map(u32 section_size)
  651. {
  652. /*
  653. * Section size mapping:
  654. * 0x0: 16-MiB section
  655. * 0x1: 32-MiB section
  656. * 0x2: 64-MiB section
  657. * 0x3: 128-MiB section
  658. * 0x4: 256-MiB section
  659. * 0x5: 512-MiB section
  660. * 0x6: 1-GiB section
  661. * 0x7: 2-GiB section
  662. */
  663. section_size >>= 24; /* divide by 16 MB */
  664. return log_2_n_round_down(section_size);
  665. }
  666. static void emif_calculate_regs(
  667. const struct emif_device_details *emif_dev_details,
  668. u32 freq, struct emif_regs *regs)
  669. {
  670. u32 temp, sys_freq;
  671. const struct lpddr2_addressing *addressing;
  672. const struct lpddr2_ac_timings *timings;
  673. const struct lpddr2_min_tck *min_tck;
  674. const struct lpddr2_device_details *cs0_dev_details =
  675. emif_dev_details->cs0_device_details;
  676. const struct lpddr2_device_details *cs1_dev_details =
  677. emif_dev_details->cs1_device_details;
  678. const struct lpddr2_device_timings *cs0_dev_timings =
  679. emif_dev_details->cs0_device_timings;
  680. emif_assert(emif_dev_details);
  681. emif_assert(regs);
  682. /*
  683. * You can not have a device on CS1 without one on CS0
  684. * So configuring EMIF without a device on CS0 doesn't
  685. * make sense
  686. */
  687. emif_assert(cs0_dev_details);
  688. emif_assert(cs0_dev_details->type != LPDDR2_TYPE_NVM);
  689. /*
  690. * If there is a device on CS1 it should be same type as CS0
  691. * (or NVM. But NVM is not supported in this driver yet)
  692. */
  693. emif_assert((cs1_dev_details == NULL) ||
  694. (cs1_dev_details->type == LPDDR2_TYPE_NVM) ||
  695. (cs0_dev_details->type == cs1_dev_details->type));
  696. emif_assert(freq <= MAX_LPDDR2_FREQ);
  697. set_ddr_clk_period(freq);
  698. /*
  699. * The device on CS0 is used for all timing calculations
  700. * There is only one set of registers for timings per EMIF. So, if the
  701. * second CS(CS1) has a device, it should have the same timings as the
  702. * device on CS0
  703. */
  704. timings = get_timings_table(cs0_dev_timings->ac_timings, freq);
  705. emif_assert(timings);
  706. min_tck = cs0_dev_timings->min_tck;
  707. temp = addressing_table_index(cs0_dev_details->type,
  708. cs0_dev_details->density,
  709. cs0_dev_details->io_width);
  710. emif_assert((temp >= 0));
  711. addressing = &(addressing_table[temp]);
  712. emif_assert(addressing);
  713. sys_freq = get_sys_clk_freq();
  714. regs->sdram_config_init = get_sdram_config_reg(cs0_dev_details,
  715. cs1_dev_details,
  716. addressing, RL_BOOT);
  717. regs->sdram_config = get_sdram_config_reg(cs0_dev_details,
  718. cs1_dev_details,
  719. addressing, RL_FINAL);
  720. regs->ref_ctrl = get_sdram_ref_ctrl(freq, addressing);
  721. regs->sdram_tim1 = get_sdram_tim_1_reg(timings, min_tck, addressing);
  722. regs->sdram_tim2 = get_sdram_tim_2_reg(timings, min_tck);
  723. regs->sdram_tim3 = get_sdram_tim_3_reg(timings, min_tck, addressing);
  724. regs->read_idle_ctrl = get_read_idle_ctrl_reg(LPDDR2_VOLTAGE_STABLE);
  725. regs->temp_alert_config =
  726. get_temp_alert_config(cs1_dev_details, addressing, 0);
  727. regs->zq_config = get_zq_config_reg(cs1_dev_details, addressing,
  728. LPDDR2_VOLTAGE_STABLE);
  729. regs->emif_ddr_phy_ctlr_1_init =
  730. get_ddr_phy_ctrl_1(sys_freq / 2, RL_BOOT);
  731. regs->emif_ddr_phy_ctlr_1 =
  732. get_ddr_phy_ctrl_1(freq, RL_FINAL);
  733. regs->freq = freq;
  734. print_timing_reg(regs->sdram_config_init);
  735. print_timing_reg(regs->sdram_config);
  736. print_timing_reg(regs->ref_ctrl);
  737. print_timing_reg(regs->sdram_tim1);
  738. print_timing_reg(regs->sdram_tim2);
  739. print_timing_reg(regs->sdram_tim3);
  740. print_timing_reg(regs->read_idle_ctrl);
  741. print_timing_reg(regs->temp_alert_config);
  742. print_timing_reg(regs->zq_config);
  743. print_timing_reg(regs->emif_ddr_phy_ctlr_1);
  744. print_timing_reg(regs->emif_ddr_phy_ctlr_1_init);
  745. }
  746. #endif /* CONFIG_SYS_EMIF_PRECALCULATED_TIMING_REGS */
  747. #ifdef CONFIG_SYS_AUTOMATIC_SDRAM_DETECTION
  748. const char *get_lpddr2_type(u8 type_id)
  749. {
  750. switch (type_id) {
  751. case LPDDR2_TYPE_S4:
  752. return "LPDDR2-S4";
  753. case LPDDR2_TYPE_S2:
  754. return "LPDDR2-S2";
  755. default:
  756. return NULL;
  757. }
  758. }
  759. const char *get_lpddr2_io_width(u8 width_id)
  760. {
  761. switch (width_id) {
  762. case LPDDR2_IO_WIDTH_8:
  763. return "x8";
  764. case LPDDR2_IO_WIDTH_16:
  765. return "x16";
  766. case LPDDR2_IO_WIDTH_32:
  767. return "x32";
  768. default:
  769. return NULL;
  770. }
  771. }
  772. const char *get_lpddr2_manufacturer(u32 manufacturer)
  773. {
  774. switch (manufacturer) {
  775. case LPDDR2_MANUFACTURER_SAMSUNG:
  776. return "Samsung";
  777. case LPDDR2_MANUFACTURER_QIMONDA:
  778. return "Qimonda";
  779. case LPDDR2_MANUFACTURER_ELPIDA:
  780. return "Elpida";
  781. case LPDDR2_MANUFACTURER_ETRON:
  782. return "Etron";
  783. case LPDDR2_MANUFACTURER_NANYA:
  784. return "Nanya";
  785. case LPDDR2_MANUFACTURER_HYNIX:
  786. return "Hynix";
  787. case LPDDR2_MANUFACTURER_MOSEL:
  788. return "Mosel";
  789. case LPDDR2_MANUFACTURER_WINBOND:
  790. return "Winbond";
  791. case LPDDR2_MANUFACTURER_ESMT:
  792. return "ESMT";
  793. case LPDDR2_MANUFACTURER_SPANSION:
  794. return "Spansion";
  795. case LPDDR2_MANUFACTURER_SST:
  796. return "SST";
  797. case LPDDR2_MANUFACTURER_ZMOS:
  798. return "ZMOS";
  799. case LPDDR2_MANUFACTURER_INTEL:
  800. return "Intel";
  801. case LPDDR2_MANUFACTURER_NUMONYX:
  802. return "Numonyx";
  803. case LPDDR2_MANUFACTURER_MICRON:
  804. return "Micron";
  805. default:
  806. return NULL;
  807. }
  808. }
  809. static void display_sdram_details(u32 emif_nr, u32 cs,
  810. struct lpddr2_device_details *device)
  811. {
  812. const char *mfg_str;
  813. const char *type_str;
  814. char density_str[10];
  815. u32 density;
  816. debug("EMIF%d CS%d\t", emif_nr, cs);
  817. if (!device) {
  818. debug("None\n");
  819. return;
  820. }
  821. mfg_str = get_lpddr2_manufacturer(device->manufacturer);
  822. type_str = get_lpddr2_type(device->type);
  823. density = lpddr2_density_2_size_in_mbytes[device->density];
  824. if ((density / 1024 * 1024) == density) {
  825. density /= 1024;
  826. sprintf(density_str, "%d GB", density);
  827. } else
  828. sprintf(density_str, "%d MB", density);
  829. if (mfg_str && type_str)
  830. debug("%s\t\t%s\t%s\n", mfg_str, type_str, density_str);
  831. }
  832. static u8 is_lpddr2_sdram_present(u32 base, u32 cs,
  833. struct lpddr2_device_details *lpddr2_device)
  834. {
  835. u32 mr = 0, temp;
  836. mr = get_mr(base, cs, LPDDR2_MR0);
  837. if (mr > 0xFF) {
  838. /* Mode register value bigger than 8 bit */
  839. return 0;
  840. }
  841. temp = (mr & LPDDR2_MR0_DI_MASK) >> LPDDR2_MR0_DI_SHIFT;
  842. if (temp) {
  843. /* Not SDRAM */
  844. return 0;
  845. }
  846. temp = (mr & LPDDR2_MR0_DNVI_MASK) >> LPDDR2_MR0_DNVI_SHIFT;
  847. if (temp) {
  848. /* DNV supported - But DNV is only supported for NVM */
  849. return 0;
  850. }
  851. mr = get_mr(base, cs, LPDDR2_MR4);
  852. if (mr > 0xFF) {
  853. /* Mode register value bigger than 8 bit */
  854. return 0;
  855. }
  856. mr = get_mr(base, cs, LPDDR2_MR5);
  857. if (mr > 0xFF) {
  858. /* Mode register value bigger than 8 bit */
  859. return 0;
  860. }
  861. if (!get_lpddr2_manufacturer(mr)) {
  862. /* Manufacturer not identified */
  863. return 0;
  864. }
  865. lpddr2_device->manufacturer = mr;
  866. mr = get_mr(base, cs, LPDDR2_MR6);
  867. if (mr >= 0xFF) {
  868. /* Mode register value bigger than 8 bit */
  869. return 0;
  870. }
  871. mr = get_mr(base, cs, LPDDR2_MR7);
  872. if (mr >= 0xFF) {
  873. /* Mode register value bigger than 8 bit */
  874. return 0;
  875. }
  876. mr = get_mr(base, cs, LPDDR2_MR8);
  877. if (mr >= 0xFF) {
  878. /* Mode register value bigger than 8 bit */
  879. return 0;
  880. }
  881. temp = (mr & MR8_TYPE_MASK) >> MR8_TYPE_SHIFT;
  882. if (!get_lpddr2_type(temp)) {
  883. /* Not SDRAM */
  884. return 0;
  885. }
  886. lpddr2_device->type = temp;
  887. temp = (mr & MR8_DENSITY_MASK) >> MR8_DENSITY_SHIFT;
  888. if (temp > LPDDR2_DENSITY_32Gb) {
  889. /* Density not supported */
  890. return 0;
  891. }
  892. lpddr2_device->density = temp;
  893. temp = (mr & MR8_IO_WIDTH_MASK) >> MR8_IO_WIDTH_SHIFT;
  894. if (!get_lpddr2_io_width(temp)) {
  895. /* IO width unsupported value */
  896. return 0;
  897. }
  898. lpddr2_device->io_width = temp;
  899. /*
  900. * If all the above tests pass we should
  901. * have a device on this chip-select
  902. */
  903. return 1;
  904. }
  905. struct lpddr2_device_details *emif_get_device_details(u32 emif_nr, u8 cs,
  906. struct lpddr2_device_details *lpddr2_dev_details)
  907. {
  908. u32 phy;
  909. u32 base = (emif_nr == 1) ? EMIF1_BASE : EMIF2_BASE;
  910. struct emif_reg_struct *emif = (struct emif_reg_struct *)base;
  911. if (!lpddr2_dev_details)
  912. return NULL;
  913. /* Do the minimum init for mode register accesses */
  914. if (!(running_from_sdram() || warm_reset())) {
  915. phy = get_ddr_phy_ctrl_1(get_sys_clk_freq() / 2, RL_BOOT);
  916. writel(phy, &emif->emif_ddr_phy_ctrl_1);
  917. }
  918. if (!(is_lpddr2_sdram_present(base, cs, lpddr2_dev_details)))
  919. return NULL;
  920. display_sdram_details(emif_num(base), cs, lpddr2_dev_details);
  921. return lpddr2_dev_details;
  922. }
  923. #endif /* CONFIG_SYS_AUTOMATIC_SDRAM_DETECTION */
  924. static void do_sdram_init(u32 base)
  925. {
  926. const struct emif_regs *regs;
  927. u32 in_sdram, emif_nr;
  928. debug(">>do_sdram_init() %x\n", base);
  929. in_sdram = running_from_sdram();
  930. emif_nr = (base == EMIF1_BASE) ? 1 : 2;
  931. #ifdef CONFIG_SYS_EMIF_PRECALCULATED_TIMING_REGS
  932. emif_get_reg_dump(emif_nr, &regs);
  933. if (!regs) {
  934. debug("EMIF: reg dump not provided\n");
  935. return;
  936. }
  937. #else
  938. /*
  939. * The user has not provided the register values. We need to
  940. * calculate it based on the timings and the DDR frequency
  941. */
  942. struct emif_device_details dev_details;
  943. struct emif_regs calculated_regs;
  944. /*
  945. * Get device details:
  946. * - Discovered if CONFIG_SYS_AUTOMATIC_SDRAM_DETECTION is set
  947. * - Obtained from user otherwise
  948. */
  949. struct lpddr2_device_details cs0_dev_details, cs1_dev_details;
  950. emif_reset_phy(base);
  951. dev_details.cs0_device_details = emif_get_device_details(emif_nr, CS0,
  952. &cs0_dev_details);
  953. dev_details.cs1_device_details = emif_get_device_details(emif_nr, CS1,
  954. &cs1_dev_details);
  955. emif_reset_phy(base);
  956. /* Return if no devices on this EMIF */
  957. if (!dev_details.cs0_device_details &&
  958. !dev_details.cs1_device_details) {
  959. return;
  960. }
  961. /*
  962. * Get device timings:
  963. * - Default timings specified by JESD209-2 if
  964. * CONFIG_SYS_DEFAULT_LPDDR2_TIMINGS is set
  965. * - Obtained from user otherwise
  966. */
  967. emif_get_device_timings(emif_nr, &dev_details.cs0_device_timings,
  968. &dev_details.cs1_device_timings);
  969. /* Calculate the register values */
  970. emif_calculate_regs(&dev_details, omap_ddr_clk(), &calculated_regs);
  971. regs = &calculated_regs;
  972. #endif /* CONFIG_SYS_EMIF_PRECALCULATED_TIMING_REGS */
  973. /*
  974. * Initializing the LPDDR2 device can not happen from SDRAM.
  975. * Changing the timing registers in EMIF can happen(going from one
  976. * OPP to another)
  977. */
  978. if (!in_sdram && (!warm_reset() || is_dra7xx())) {
  979. if (emif_sdram_type(regs->sdram_config) ==
  980. EMIF_SDRAM_TYPE_LPDDR2)
  981. lpddr2_init(base, regs);
  982. else
  983. ddr3_init(base, regs);
  984. }
  985. if (warm_reset() && (emif_sdram_type(regs->sdram_config) ==
  986. EMIF_SDRAM_TYPE_DDR3) && !is_dra7xx()) {
  987. set_lpmode_selfrefresh(base);
  988. emif_reset_phy(base);
  989. omap5_ddr3_leveling(base, regs);
  990. }
  991. /* Write to the shadow registers */
  992. emif_update_timings(base, regs);
  993. debug("<<do_sdram_init() %x\n", base);
  994. }
  995. void emif_post_init_config(u32 base)
  996. {
  997. struct emif_reg_struct *emif = (struct emif_reg_struct *)base;
  998. u32 omap_rev = omap_revision();
  999. /* reset phy on ES2.0 */
  1000. if (omap_rev == OMAP4430_ES2_0)
  1001. emif_reset_phy(base);
  1002. /* Put EMIF back in smart idle on ES1.0 */
  1003. if (omap_rev == OMAP4430_ES1_0)
  1004. writel(0x80000000, &emif->emif_pwr_mgmt_ctrl);
  1005. }
  1006. void dmm_init(u32 base)
  1007. {
  1008. const struct dmm_lisa_map_regs *lisa_map_regs;
  1009. u32 i, section, valid;
  1010. #ifdef CONFIG_SYS_EMIF_PRECALCULATED_TIMING_REGS
  1011. emif_get_dmm_regs(&lisa_map_regs);
  1012. #else
  1013. u32 emif1_size, emif2_size, mapped_size, section_map = 0;
  1014. u32 section_cnt, sys_addr;
  1015. struct dmm_lisa_map_regs lis_map_regs_calculated = {0};
  1016. mapped_size = 0;
  1017. section_cnt = 3;
  1018. sys_addr = CONFIG_SYS_SDRAM_BASE;
  1019. emif1_size = get_emif_mem_size(EMIF1_BASE);
  1020. emif2_size = get_emif_mem_size(EMIF2_BASE);
  1021. debug("emif1_size 0x%x emif2_size 0x%x\n", emif1_size, emif2_size);
  1022. if (!emif1_size && !emif2_size)
  1023. return;
  1024. /* symmetric interleaved section */
  1025. if (emif1_size && emif2_size) {
  1026. mapped_size = min(emif1_size, emif2_size);
  1027. section_map = DMM_LISA_MAP_INTERLEAVED_BASE_VAL;
  1028. section_map |= 0 << EMIF_SDRC_ADDR_SHIFT;
  1029. /* only MSB */
  1030. section_map |= (sys_addr >> 24) <<
  1031. EMIF_SYS_ADDR_SHIFT;
  1032. section_map |= get_dmm_section_size_map(mapped_size * 2)
  1033. << EMIF_SYS_SIZE_SHIFT;
  1034. lis_map_regs_calculated.dmm_lisa_map_3 = section_map;
  1035. emif1_size -= mapped_size;
  1036. emif2_size -= mapped_size;
  1037. sys_addr += (mapped_size * 2);
  1038. section_cnt--;
  1039. }
  1040. /*
  1041. * Single EMIF section(we can have a maximum of 1 single EMIF
  1042. * section- either EMIF1 or EMIF2 or none, but not both)
  1043. */
  1044. if (emif1_size) {
  1045. section_map = DMM_LISA_MAP_EMIF1_ONLY_BASE_VAL;
  1046. section_map |= get_dmm_section_size_map(emif1_size)
  1047. << EMIF_SYS_SIZE_SHIFT;
  1048. /* only MSB */
  1049. section_map |= (mapped_size >> 24) <<
  1050. EMIF_SDRC_ADDR_SHIFT;
  1051. /* only MSB */
  1052. section_map |= (sys_addr >> 24) << EMIF_SYS_ADDR_SHIFT;
  1053. section_cnt--;
  1054. }
  1055. if (emif2_size) {
  1056. section_map = DMM_LISA_MAP_EMIF2_ONLY_BASE_VAL;
  1057. section_map |= get_dmm_section_size_map(emif2_size) <<
  1058. EMIF_SYS_SIZE_SHIFT;
  1059. /* only MSB */
  1060. section_map |= mapped_size >> 24 << EMIF_SDRC_ADDR_SHIFT;
  1061. /* only MSB */
  1062. section_map |= sys_addr >> 24 << EMIF_SYS_ADDR_SHIFT;
  1063. section_cnt--;
  1064. }
  1065. if (section_cnt == 2) {
  1066. /* Only 1 section - either symmetric or single EMIF */
  1067. lis_map_regs_calculated.dmm_lisa_map_3 = section_map;
  1068. lis_map_regs_calculated.dmm_lisa_map_2 = 0;
  1069. lis_map_regs_calculated.dmm_lisa_map_1 = 0;
  1070. } else {
  1071. /* 2 sections - 1 symmetric, 1 single EMIF */
  1072. lis_map_regs_calculated.dmm_lisa_map_2 = section_map;
  1073. lis_map_regs_calculated.dmm_lisa_map_1 = 0;
  1074. }
  1075. /* TRAP for invalid TILER mappings in section 0 */
  1076. lis_map_regs_calculated.dmm_lisa_map_0 = DMM_LISA_MAP_0_INVAL_ADDR_TRAP;
  1077. if (omap_revision() >= OMAP4460_ES1_0)
  1078. lis_map_regs_calculated.is_ma_present = 1;
  1079. lisa_map_regs = &lis_map_regs_calculated;
  1080. #endif
  1081. struct dmm_lisa_map_regs *hw_lisa_map_regs =
  1082. (struct dmm_lisa_map_regs *)base;
  1083. writel(0, &hw_lisa_map_regs->dmm_lisa_map_3);
  1084. writel(0, &hw_lisa_map_regs->dmm_lisa_map_2);
  1085. writel(0, &hw_lisa_map_regs->dmm_lisa_map_1);
  1086. writel(0, &hw_lisa_map_regs->dmm_lisa_map_0);
  1087. writel(lisa_map_regs->dmm_lisa_map_3,
  1088. &hw_lisa_map_regs->dmm_lisa_map_3);
  1089. writel(lisa_map_regs->dmm_lisa_map_2,
  1090. &hw_lisa_map_regs->dmm_lisa_map_2);
  1091. writel(lisa_map_regs->dmm_lisa_map_1,
  1092. &hw_lisa_map_regs->dmm_lisa_map_1);
  1093. writel(lisa_map_regs->dmm_lisa_map_0,
  1094. &hw_lisa_map_regs->dmm_lisa_map_0);
  1095. if (lisa_map_regs->is_ma_present) {
  1096. hw_lisa_map_regs =
  1097. (struct dmm_lisa_map_regs *)MA_BASE;
  1098. writel(lisa_map_regs->dmm_lisa_map_3,
  1099. &hw_lisa_map_regs->dmm_lisa_map_3);
  1100. writel(lisa_map_regs->dmm_lisa_map_2,
  1101. &hw_lisa_map_regs->dmm_lisa_map_2);
  1102. writel(lisa_map_regs->dmm_lisa_map_1,
  1103. &hw_lisa_map_regs->dmm_lisa_map_1);
  1104. writel(lisa_map_regs->dmm_lisa_map_0,
  1105. &hw_lisa_map_regs->dmm_lisa_map_0);
  1106. setbits_le32(MA_PRIORITY, MA_HIMEM_INTERLEAVE_UN_MASK);
  1107. }
  1108. /*
  1109. * EMIF should be configured only when
  1110. * memory is mapped on it. Using emif1_enabled
  1111. * and emif2_enabled variables for this.
  1112. */
  1113. emif1_enabled = 0;
  1114. emif2_enabled = 0;
  1115. for (i = 0; i < 4; i++) {
  1116. section = __raw_readl(DMM_BASE + i*4);
  1117. valid = (section & EMIF_SDRC_MAP_MASK) >>
  1118. (EMIF_SDRC_MAP_SHIFT);
  1119. if (valid == 3) {
  1120. emif1_enabled = 1;
  1121. emif2_enabled = 1;
  1122. break;
  1123. }
  1124. if (valid == 1)
  1125. emif1_enabled = 1;
  1126. if (valid == 2)
  1127. emif2_enabled = 1;
  1128. }
  1129. }
  1130. static void do_bug0039_workaround(u32 base)
  1131. {
  1132. u32 val, i, clkctrl;
  1133. struct emif_reg_struct *emif_base = (struct emif_reg_struct *)base;
  1134. const struct read_write_regs *bug_00339_regs;
  1135. u32 iterations;
  1136. u32 *phy_status_base = &emif_base->emif_ddr_phy_status[0];
  1137. u32 *phy_ctrl_base = &emif_base->emif_ddr_ext_phy_ctrl_1;
  1138. if (is_dra7xx())
  1139. phy_status_base++;
  1140. bug_00339_regs = get_bug_regs(&iterations);
  1141. /* Put EMIF in to idle */
  1142. clkctrl = __raw_readl((*prcm)->cm_memif_clkstctrl);
  1143. __raw_writel(0x0, (*prcm)->cm_memif_clkstctrl);
  1144. /* Copy the phy status registers in to phy ctrl shadow registers */
  1145. for (i = 0; i < iterations; i++) {
  1146. val = __raw_readl(phy_status_base +
  1147. bug_00339_regs[i].read_reg - 1);
  1148. __raw_writel(val, phy_ctrl_base +
  1149. ((bug_00339_regs[i].write_reg - 1) << 1));
  1150. __raw_writel(val, phy_ctrl_base +
  1151. (bug_00339_regs[i].write_reg << 1) - 1);
  1152. }
  1153. /* Disable leveling */
  1154. writel(0x0, &emif_base->emif_rd_wr_lvl_rmp_ctl);
  1155. __raw_writel(clkctrl, (*prcm)->cm_memif_clkstctrl);
  1156. }
  1157. /*
  1158. * SDRAM initialization:
  1159. * SDRAM initialization has two parts:
  1160. * 1. Configuring the SDRAM device
  1161. * 2. Update the AC timings related parameters in the EMIF module
  1162. * (1) should be done only once and should not be done while we are
  1163. * running from SDRAM.
  1164. * (2) can and should be done more than once if OPP changes.
  1165. * Particularly, this may be needed when we boot without SPL and
  1166. * and using Configuration Header(CH). ROM code supports only at 50% OPP
  1167. * at boot (low power boot). So u-boot has to switch to OPP100 and update
  1168. * the frequency. So,
  1169. * Doing (1) and (2) makes sense - first time initialization
  1170. * Doing (2) and not (1) makes sense - OPP change (when using CH)
  1171. * Doing (1) and not (2) doen't make sense
  1172. * See do_sdram_init() for the details
  1173. */
  1174. void sdram_init(void)
  1175. {
  1176. u32 in_sdram, size_prog, size_detect;
  1177. struct emif_reg_struct *emif = (struct emif_reg_struct *)EMIF1_BASE;
  1178. u32 sdram_type = emif_sdram_type(emif->emif_sdram_config);
  1179. debug(">>sdram_init()\n");
  1180. if (omap_hw_init_context() == OMAP_INIT_CONTEXT_UBOOT_AFTER_SPL)
  1181. return;
  1182. in_sdram = running_from_sdram();
  1183. debug("in_sdram = %d\n", in_sdram);
  1184. if (!in_sdram) {
  1185. if ((sdram_type == EMIF_SDRAM_TYPE_LPDDR2) && !warm_reset())
  1186. bypass_dpll((*prcm)->cm_clkmode_dpll_core);
  1187. else if (sdram_type == EMIF_SDRAM_TYPE_DDR3)
  1188. writel(CM_DLL_CTRL_NO_OVERRIDE, (*prcm)->cm_dll_ctrl);
  1189. }
  1190. if (!in_sdram)
  1191. dmm_init(DMM_BASE);
  1192. if (emif1_enabled)
  1193. do_sdram_init(EMIF1_BASE);
  1194. if (emif2_enabled)
  1195. do_sdram_init(EMIF2_BASE);
  1196. if (!(in_sdram || warm_reset())) {
  1197. if (emif1_enabled)
  1198. emif_post_init_config(EMIF1_BASE);
  1199. if (emif2_enabled)
  1200. emif_post_init_config(EMIF2_BASE);
  1201. }
  1202. /* for the shadow registers to take effect */
  1203. if (sdram_type == EMIF_SDRAM_TYPE_LPDDR2)
  1204. freq_update_core();
  1205. /* Do some testing after the init */
  1206. if (!in_sdram) {
  1207. size_prog = omap_sdram_size();
  1208. size_prog = log_2_n_round_down(size_prog);
  1209. size_prog = (1 << size_prog);
  1210. size_detect = get_ram_size((long *)CONFIG_SYS_SDRAM_BASE,
  1211. size_prog);
  1212. /* Compare with the size programmed */
  1213. if (size_detect != size_prog) {
  1214. printf("SDRAM: identified size not same as expected"
  1215. " size identified: %x expected: %x\n",
  1216. size_detect,
  1217. size_prog);
  1218. } else
  1219. debug("get_ram_size() successful");
  1220. }
  1221. if (sdram_type == EMIF_SDRAM_TYPE_DDR3 &&
  1222. (!in_sdram && !warm_reset()) && (!is_dra7xx())) {
  1223. if (emif1_enabled)
  1224. do_bug0039_workaround(EMIF1_BASE);
  1225. if (emif2_enabled)
  1226. do_bug0039_workaround(EMIF2_BASE);
  1227. }
  1228. debug("<<sdram_init()\n");
  1229. }