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