emif-common.c 36 KB

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