emif-common.c 36 KB

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