emif-common.c 36 KB

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