emif.c 36 KB

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