emif-common.c 42 KB

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