emif-common.c 36 KB

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