emif-common.c 37 KB

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