emif-common.c 36 KB

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