emif-common.c 37 KB

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