emif-common.c 37 KB

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