ddr.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538
  1. /*
  2. * Copyright (C) 2014 Gateworks Corporation
  3. * Author: Tim Harvey <tharvey@gateworks.com>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <linux/types.h>
  9. #include <asm/arch/clock.h>
  10. #include <asm/arch/mx6-ddr.h>
  11. #include <asm/arch/sys_proto.h>
  12. #include <asm/io.h>
  13. #include <asm/types.h>
  14. #include <wait_bit.h>
  15. #if defined(CONFIG_MX6_DDRCAL)
  16. static void reset_read_data_fifos(void)
  17. {
  18. struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
  19. /* Reset data FIFOs twice. */
  20. setbits_le32(&mmdc0->mpdgctrl0, 1 << 31);
  21. wait_for_bit("MMDC", &mmdc0->mpdgctrl0, 1 << 31, 0, 100, 0);
  22. setbits_le32(&mmdc0->mpdgctrl0, 1 << 31);
  23. wait_for_bit("MMDC", &mmdc0->mpdgctrl0, 1 << 31, 0, 100, 0);
  24. }
  25. static void precharge_all(const bool cs0_enable, const bool cs1_enable)
  26. {
  27. struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
  28. /*
  29. * Issue the Precharge-All command to the DDR device for both
  30. * chip selects. Note, CON_REQ bit should also remain set. If
  31. * only using one chip select, then precharge only the desired
  32. * chip select.
  33. */
  34. if (cs0_enable) { /* CS0 */
  35. writel(0x04008050, &mmdc0->mdscr);
  36. wait_for_bit("MMDC", &mmdc0->mdscr, 1 << 14, 1, 100, 0);
  37. }
  38. if (cs1_enable) { /* CS1 */
  39. writel(0x04008058, &mmdc0->mdscr);
  40. wait_for_bit("MMDC", &mmdc0->mdscr, 1 << 14, 1, 100, 0);
  41. }
  42. }
  43. static void force_delay_measurement(int bus_size)
  44. {
  45. struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
  46. struct mmdc_p_regs *mmdc1 = (struct mmdc_p_regs *)MMDC_P1_BASE_ADDR;
  47. writel(0x800, &mmdc0->mpmur0);
  48. if (bus_size == 0x2)
  49. writel(0x800, &mmdc1->mpmur0);
  50. }
  51. static void modify_dg_result(u32 *reg_st0, u32 *reg_st1, u32 *reg_ctrl)
  52. {
  53. u32 dg_tmp_val, dg_dl_abs_offset, dg_hc_del, val_ctrl;
  54. /*
  55. * DQS gating absolute offset should be modified from reflecting
  56. * (HW_DG_LOWx + HW_DG_UPx)/2 to reflecting (HW_DG_UPx - 0x80)
  57. */
  58. val_ctrl = readl(reg_ctrl);
  59. val_ctrl &= 0xf0000000;
  60. dg_tmp_val = ((readl(reg_st0) & 0x07ff0000) >> 16) - 0xc0;
  61. dg_dl_abs_offset = dg_tmp_val & 0x7f;
  62. dg_hc_del = (dg_tmp_val & 0x780) << 1;
  63. val_ctrl |= dg_dl_abs_offset + dg_hc_del;
  64. dg_tmp_val = ((readl(reg_st1) & 0x07ff0000) >> 16) - 0xc0;
  65. dg_dl_abs_offset = dg_tmp_val & 0x7f;
  66. dg_hc_del = (dg_tmp_val & 0x780) << 1;
  67. val_ctrl |= (dg_dl_abs_offset + dg_hc_del) << 16;
  68. writel(val_ctrl, reg_ctrl);
  69. }
  70. int mmdc_do_write_level_calibration(struct mx6_ddr_sysinfo const *sysinfo)
  71. {
  72. struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
  73. struct mmdc_p_regs *mmdc1 = (struct mmdc_p_regs *)MMDC_P1_BASE_ADDR;
  74. u32 esdmisc_val, zq_val;
  75. u32 errors = 0;
  76. u32 ldectrl[4] = {0};
  77. u32 ddr_mr1 = 0x4;
  78. u32 rwalat_max;
  79. /*
  80. * Stash old values in case calibration fails,
  81. * we need to restore them
  82. */
  83. ldectrl[0] = readl(&mmdc0->mpwldectrl0);
  84. ldectrl[1] = readl(&mmdc0->mpwldectrl1);
  85. if (sysinfo->dsize == 2) {
  86. ldectrl[2] = readl(&mmdc1->mpwldectrl0);
  87. ldectrl[3] = readl(&mmdc1->mpwldectrl1);
  88. }
  89. /* disable DDR logic power down timer */
  90. clrbits_le32(&mmdc0->mdpdc, 0xff00);
  91. /* disable Adopt power down timer */
  92. setbits_le32(&mmdc0->mapsr, 0x1);
  93. debug("Starting write leveling calibration.\n");
  94. /*
  95. * 2. disable auto refresh and ZQ calibration
  96. * before proceeding with Write Leveling calibration
  97. */
  98. esdmisc_val = readl(&mmdc0->mdref);
  99. writel(0x0000C000, &mmdc0->mdref);
  100. zq_val = readl(&mmdc0->mpzqhwctrl);
  101. writel(zq_val & ~0x3, &mmdc0->mpzqhwctrl);
  102. /* 3. increase walat and ralat to maximum */
  103. rwalat_max = (1 << 6) | (1 << 7) | (1 << 8) | (1 << 16) | (1 << 17);
  104. setbits_le32(&mmdc0->mdmisc, rwalat_max);
  105. if (sysinfo->dsize == 2)
  106. setbits_le32(&mmdc1->mdmisc, rwalat_max);
  107. /*
  108. * 4 & 5. Configure the external DDR device to enter write-leveling
  109. * mode through Load Mode Register command.
  110. * Register setting:
  111. * Bits[31:16] MR1 value (0x0080 write leveling enable)
  112. * Bit[9] set WL_EN to enable MMDC DQS output
  113. * Bits[6:4] set CMD bits for Load Mode Register programming
  114. * Bits[2:0] set CMD_BA to 0x1 for DDR MR1 programming
  115. */
  116. writel(0x00808231, &mmdc0->mdscr);
  117. /* 6. Activate automatic calibration by setting MPWLGCR[HW_WL_EN] */
  118. writel(0x00000001, &mmdc0->mpwlgcr);
  119. /*
  120. * 7. Upon completion of this process the MMDC de-asserts
  121. * the MPWLGCR[HW_WL_EN]
  122. */
  123. wait_for_bit("MMDC", &mmdc0->mpwlgcr, 1 << 0, 0, 100, 0);
  124. /*
  125. * 8. check for any errors: check both PHYs for x64 configuration,
  126. * if x32, check only PHY0
  127. */
  128. if (readl(&mmdc0->mpwlgcr) & 0x00000F00)
  129. errors |= 1;
  130. if (sysinfo->dsize == 2)
  131. if (readl(&mmdc1->mpwlgcr) & 0x00000F00)
  132. errors |= 2;
  133. debug("Ending write leveling calibration. Error mask: 0x%x\n", errors);
  134. /* check to see if cal failed */
  135. if ((readl(&mmdc0->mpwldectrl0) == 0x001F001F) &&
  136. (readl(&mmdc0->mpwldectrl1) == 0x001F001F) &&
  137. ((sysinfo->dsize < 2) ||
  138. ((readl(&mmdc1->mpwldectrl0) == 0x001F001F) &&
  139. (readl(&mmdc1->mpwldectrl1) == 0x001F001F)))) {
  140. debug("Cal seems to have soft-failed due to memory not supporting write leveling on all channels. Restoring original write leveling values.\n");
  141. writel(ldectrl[0], &mmdc0->mpwldectrl0);
  142. writel(ldectrl[1], &mmdc0->mpwldectrl1);
  143. if (sysinfo->dsize == 2) {
  144. writel(ldectrl[2], &mmdc1->mpwldectrl0);
  145. writel(ldectrl[3], &mmdc1->mpwldectrl1);
  146. }
  147. errors |= 4;
  148. }
  149. /*
  150. * User should issue MRS command to exit write leveling mode
  151. * through Load Mode Register command
  152. * Register setting:
  153. * Bits[31:16] MR1 value "ddr_mr1" value from initialization
  154. * Bit[9] clear WL_EN to disable MMDC DQS output
  155. * Bits[6:4] set CMD bits for Load Mode Register programming
  156. * Bits[2:0] set CMD_BA to 0x1 for DDR MR1 programming
  157. */
  158. writel((ddr_mr1 << 16) + 0x8031, &mmdc0->mdscr);
  159. /* re-enable auto refresh and zq cal */
  160. writel(esdmisc_val, &mmdc0->mdref);
  161. writel(zq_val, &mmdc0->mpzqhwctrl);
  162. debug("\tMMDC_MPWLDECTRL0 after write level cal: 0x%08X\n",
  163. readl(&mmdc0->mpwldectrl0));
  164. debug("\tMMDC_MPWLDECTRL1 after write level cal: 0x%08X\n",
  165. readl(&mmdc0->mpwldectrl1));
  166. if (sysinfo->dsize == 2) {
  167. debug("\tMMDC_MPWLDECTRL0 after write level cal: 0x%08X\n",
  168. readl(&mmdc1->mpwldectrl0));
  169. debug("\tMMDC_MPWLDECTRL1 after write level cal: 0x%08X\n",
  170. readl(&mmdc1->mpwldectrl1));
  171. }
  172. /* We must force a readback of these values, to get them to stick */
  173. readl(&mmdc0->mpwldectrl0);
  174. readl(&mmdc0->mpwldectrl1);
  175. if (sysinfo->dsize == 2) {
  176. readl(&mmdc1->mpwldectrl0);
  177. readl(&mmdc1->mpwldectrl1);
  178. }
  179. /* enable DDR logic power down timer: */
  180. setbits_le32(&mmdc0->mdpdc, 0x00005500);
  181. /* Enable Adopt power down timer: */
  182. clrbits_le32(&mmdc0->mapsr, 0x1);
  183. /* Clear CON_REQ */
  184. writel(0, &mmdc0->mdscr);
  185. return errors;
  186. }
  187. int mmdc_do_dqs_calibration(struct mx6_ddr_sysinfo const *sysinfo)
  188. {
  189. struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
  190. struct mmdc_p_regs *mmdc1 = (struct mmdc_p_regs *)MMDC_P1_BASE_ADDR;
  191. struct mx6dq_iomux_ddr_regs *mx6_ddr_iomux =
  192. (struct mx6dq_iomux_ddr_regs *)MX6DQ_IOM_DDR_BASE;
  193. bool cs0_enable;
  194. bool cs1_enable;
  195. bool cs0_enable_initial;
  196. bool cs1_enable_initial;
  197. u32 esdmisc_val;
  198. u32 temp_ref;
  199. u32 pddword = 0x00ffff00; /* best so far, place into MPPDCMPR1 */
  200. u32 errors = 0;
  201. u32 initdelay = 0x40404040;
  202. /* check to see which chip selects are enabled */
  203. cs0_enable_initial = readl(&mmdc0->mdctl) & 0x80000000;
  204. cs1_enable_initial = readl(&mmdc0->mdctl) & 0x40000000;
  205. /* disable DDR logic power down timer: */
  206. clrbits_le32(&mmdc0->mdpdc, 0xff00);
  207. /* disable Adopt power down timer: */
  208. setbits_le32(&mmdc0->mapsr, 0x1);
  209. /* set DQS pull ups */
  210. setbits_le32(&mx6_ddr_iomux->dram_sdqs0, 0x7000);
  211. setbits_le32(&mx6_ddr_iomux->dram_sdqs1, 0x7000);
  212. setbits_le32(&mx6_ddr_iomux->dram_sdqs2, 0x7000);
  213. setbits_le32(&mx6_ddr_iomux->dram_sdqs3, 0x7000);
  214. setbits_le32(&mx6_ddr_iomux->dram_sdqs4, 0x7000);
  215. setbits_le32(&mx6_ddr_iomux->dram_sdqs5, 0x7000);
  216. setbits_le32(&mx6_ddr_iomux->dram_sdqs6, 0x7000);
  217. setbits_le32(&mx6_ddr_iomux->dram_sdqs7, 0x7000);
  218. /* Save old RALAT and WALAT values */
  219. esdmisc_val = readl(&mmdc0->mdmisc);
  220. setbits_le32(&mmdc0->mdmisc,
  221. (1 << 6) | (1 << 7) | (1 << 8) | (1 << 16) | (1 << 17));
  222. /* Disable auto refresh before proceeding with calibration */
  223. temp_ref = readl(&mmdc0->mdref);
  224. writel(0x0000c000, &mmdc0->mdref);
  225. /*
  226. * Per the ref manual, issue one refresh cycle MDSCR[CMD]= 0x2,
  227. * this also sets the CON_REQ bit.
  228. */
  229. if (cs0_enable_initial)
  230. writel(0x00008020, &mmdc0->mdscr);
  231. if (cs1_enable_initial)
  232. writel(0x00008028, &mmdc0->mdscr);
  233. /* poll to make sure the con_ack bit was asserted */
  234. wait_for_bit("MMDC", &mmdc0->mdscr, 1 << 14, 1, 100, 0);
  235. /*
  236. * Check MDMISC register CALIB_PER_CS to see which CS calibration
  237. * is targeted to (under normal cases, it should be cleared
  238. * as this is the default value, indicating calibration is directed
  239. * to CS0).
  240. * Disable the other chip select not being target for calibration
  241. * to avoid any potential issues. This will get re-enabled at end
  242. * of calibration.
  243. */
  244. if ((readl(&mmdc0->mdmisc) & 0x00100000) == 0)
  245. clrbits_le32(&mmdc0->mdctl, 1 << 30); /* clear SDE_1 */
  246. else
  247. clrbits_le32(&mmdc0->mdctl, 1 << 31); /* clear SDE_0 */
  248. /*
  249. * Check to see which chip selects are now enabled for
  250. * the remainder of the calibration.
  251. */
  252. cs0_enable = readl(&mmdc0->mdctl) & 0x80000000;
  253. cs1_enable = readl(&mmdc0->mdctl) & 0x40000000;
  254. precharge_all(cs0_enable, cs1_enable);
  255. /* Write the pre-defined value into MPPDCMPR1 */
  256. writel(pddword, &mmdc0->mppdcmpr1);
  257. /*
  258. * Issue a write access to the external DDR device by setting
  259. * the bit SW_DUMMY_WR (bit 0) in the MPSWDAR0 and then poll
  260. * this bit until it clears to indicate completion of the write access.
  261. */
  262. setbits_le32(&mmdc0->mpswdar0, 1);
  263. wait_for_bit("MMDC", &mmdc0->mpswdar0, 1 << 0, 0, 100, 0);
  264. /* Set the RD_DL_ABS# bits to their default values
  265. * (will be calibrated later in the read delay-line calibration).
  266. * Both PHYs for x64 configuration, if x32, do only PHY0.
  267. */
  268. writel(initdelay, &mmdc0->mprddlctl);
  269. if (sysinfo->dsize == 0x2)
  270. writel(initdelay, &mmdc1->mprddlctl);
  271. /* Force a measurment, for previous delay setup to take effect. */
  272. force_delay_measurement(sysinfo->dsize);
  273. /*
  274. * ***************************
  275. * Read DQS Gating calibration
  276. * ***************************
  277. */
  278. debug("Starting Read DQS Gating calibration.\n");
  279. /*
  280. * Reset the read data FIFOs (two resets); only need to issue reset
  281. * to PHY0 since in x64 mode, the reset will also go to PHY1.
  282. */
  283. reset_read_data_fifos();
  284. /*
  285. * Start the automatic read DQS gating calibration process by
  286. * asserting MPDGCTRL0[HW_DG_EN] and MPDGCTRL0[DG_CMP_CYC]
  287. * and then poll MPDGCTRL0[HW_DG_EN]] until this bit clears
  288. * to indicate completion.
  289. * Also, ensure that MPDGCTRL0[HW_DG_ERR] is clear to indicate
  290. * no errors were seen during calibration.
  291. */
  292. /*
  293. * Set bit 30: chooses option to wait 32 cycles instead of
  294. * 16 before comparing read data.
  295. */
  296. setbits_le32(&mmdc0->mpdgctrl0, 1 << 30);
  297. if (sysinfo->dsize == 2)
  298. setbits_le32(&mmdc1->mpdgctrl0, 1 << 30);
  299. /* Set bit 28 to start automatic read DQS gating calibration */
  300. setbits_le32(&mmdc0->mpdgctrl0, 5 << 28);
  301. /* Poll for completion. MPDGCTRL0[HW_DG_EN] should be 0 */
  302. wait_for_bit("MMDC", &mmdc0->mpdgctrl0, 1 << 28, 0, 100, 0);
  303. /*
  304. * Check to see if any errors were encountered during calibration
  305. * (check MPDGCTRL0[HW_DG_ERR]).
  306. * Check both PHYs for x64 configuration, if x32, check only PHY0.
  307. */
  308. if (readl(&mmdc0->mpdgctrl0) & 0x00001000)
  309. errors |= 1;
  310. if ((sysinfo->dsize == 0x2) && (readl(&mmdc1->mpdgctrl0) & 0x00001000))
  311. errors |= 2;
  312. /* now disable mpdgctrl0[DG_CMP_CYC] */
  313. clrbits_le32(&mmdc0->mpdgctrl0, 1 << 30);
  314. if (sysinfo->dsize == 2)
  315. clrbits_le32(&mmdc1->mpdgctrl0, 1 << 30);
  316. /*
  317. * DQS gating absolute offset should be modified from
  318. * reflecting (HW_DG_LOWx + HW_DG_UPx)/2 to
  319. * reflecting (HW_DG_UPx - 0x80)
  320. */
  321. modify_dg_result(&mmdc0->mpdghwst0, &mmdc0->mpdghwst1,
  322. &mmdc0->mpdgctrl0);
  323. modify_dg_result(&mmdc0->mpdghwst2, &mmdc0->mpdghwst3,
  324. &mmdc0->mpdgctrl1);
  325. if (sysinfo->dsize == 0x2) {
  326. modify_dg_result(&mmdc1->mpdghwst0, &mmdc1->mpdghwst1,
  327. &mmdc1->mpdgctrl0);
  328. modify_dg_result(&mmdc1->mpdghwst2, &mmdc1->mpdghwst3,
  329. &mmdc1->mpdgctrl1);
  330. }
  331. debug("Ending Read DQS Gating calibration. Error mask: 0x%x\n", errors);
  332. /*
  333. * **********************
  334. * Read Delay calibration
  335. * **********************
  336. */
  337. debug("Starting Read Delay calibration.\n");
  338. reset_read_data_fifos();
  339. /*
  340. * 4. Issue the Precharge-All command to the DDR device for both
  341. * chip selects. If only using one chip select, then precharge
  342. * only the desired chip select.
  343. */
  344. precharge_all(cs0_enable, cs1_enable);
  345. /*
  346. * 9. Read delay-line calibration
  347. * Start the automatic read calibration process by asserting
  348. * MPRDDLHWCTL[HW_RD_DL_EN].
  349. */
  350. writel(0x00000030, &mmdc0->mprddlhwctl);
  351. /*
  352. * 10. poll for completion
  353. * MMDC indicates that the write data calibration had finished by
  354. * setting MPRDDLHWCTL[HW_RD_DL_EN] = 0. Also, ensure that
  355. * no error bits were set.
  356. */
  357. wait_for_bit("MMDC", &mmdc0->mprddlhwctl, 1 << 4, 0, 100, 0);
  358. /* check both PHYs for x64 configuration, if x32, check only PHY0 */
  359. if (readl(&mmdc0->mprddlhwctl) & 0x0000000f)
  360. errors |= 4;
  361. if ((sysinfo->dsize == 0x2) &&
  362. (readl(&mmdc1->mprddlhwctl) & 0x0000000f))
  363. errors |= 8;
  364. debug("Ending Read Delay calibration. Error mask: 0x%x\n", errors);
  365. /*
  366. * ***********************
  367. * Write Delay Calibration
  368. * ***********************
  369. */
  370. debug("Starting Write Delay calibration.\n");
  371. reset_read_data_fifos();
  372. /*
  373. * 4. Issue the Precharge-All command to the DDR device for both
  374. * chip selects. If only using one chip select, then precharge
  375. * only the desired chip select.
  376. */
  377. precharge_all(cs0_enable, cs1_enable);
  378. /*
  379. * 8. Set the WR_DL_ABS# bits to their default values.
  380. * Both PHYs for x64 configuration, if x32, do only PHY0.
  381. */
  382. writel(initdelay, &mmdc0->mpwrdlctl);
  383. if (sysinfo->dsize == 0x2)
  384. writel(initdelay, &mmdc1->mpwrdlctl);
  385. /*
  386. * XXX This isn't in the manual. Force a measurement,
  387. * for previous delay setup to effect.
  388. */
  389. force_delay_measurement(sysinfo->dsize);
  390. /*
  391. * 9. 10. Start the automatic write calibration process
  392. * by asserting MPWRDLHWCTL0[HW_WR_DL_EN].
  393. */
  394. writel(0x00000030, &mmdc0->mpwrdlhwctl);
  395. /*
  396. * Poll for completion.
  397. * MMDC indicates that the write data calibration had finished
  398. * by setting MPWRDLHWCTL[HW_WR_DL_EN] = 0.
  399. * Also, ensure that no error bits were set.
  400. */
  401. wait_for_bit("MMDC", &mmdc0->mpwrdlhwctl, 1 << 4, 0, 100, 0);
  402. /* Check both PHYs for x64 configuration, if x32, check only PHY0 */
  403. if (readl(&mmdc0->mpwrdlhwctl) & 0x0000000f)
  404. errors |= 16;
  405. if ((sysinfo->dsize == 0x2) &&
  406. (readl(&mmdc1->mpwrdlhwctl) & 0x0000000f))
  407. errors |= 32;
  408. debug("Ending Write Delay calibration. Error mask: 0x%x\n", errors);
  409. reset_read_data_fifos();
  410. /* Enable DDR logic power down timer */
  411. setbits_le32(&mmdc0->mdpdc, 0x00005500);
  412. /* Enable Adopt power down timer */
  413. clrbits_le32(&mmdc0->mapsr, 0x1);
  414. /* Restore MDMISC value (RALAT, WALAT) to MMDCP1 */
  415. writel(esdmisc_val, &mmdc0->mdmisc);
  416. /* Clear DQS pull ups */
  417. clrbits_le32(&mx6_ddr_iomux->dram_sdqs0, 0x7000);
  418. clrbits_le32(&mx6_ddr_iomux->dram_sdqs1, 0x7000);
  419. clrbits_le32(&mx6_ddr_iomux->dram_sdqs2, 0x7000);
  420. clrbits_le32(&mx6_ddr_iomux->dram_sdqs3, 0x7000);
  421. clrbits_le32(&mx6_ddr_iomux->dram_sdqs4, 0x7000);
  422. clrbits_le32(&mx6_ddr_iomux->dram_sdqs5, 0x7000);
  423. clrbits_le32(&mx6_ddr_iomux->dram_sdqs6, 0x7000);
  424. clrbits_le32(&mx6_ddr_iomux->dram_sdqs7, 0x7000);
  425. /* Re-enable SDE (chip selects) if they were set initially */
  426. if (cs1_enable_initial)
  427. /* Set SDE_1 */
  428. setbits_le32(&mmdc0->mdctl, 1 << 30);
  429. if (cs0_enable_initial)
  430. /* Set SDE_0 */
  431. setbits_le32(&mmdc0->mdctl, 1 << 31);
  432. /* Re-enable to auto refresh */
  433. writel(temp_ref, &mmdc0->mdref);
  434. /* Clear the MDSCR (including the con_req bit) */
  435. writel(0x0, &mmdc0->mdscr); /* CS0 */
  436. /* Poll to make sure the con_ack bit is clear */
  437. wait_for_bit("MMDC", &mmdc0->mdscr, 1 << 14, 0, 100, 0);
  438. /*
  439. * Print out the registers that were updated as a result
  440. * of the calibration process.
  441. */
  442. debug("MMDC registers updated from calibration\n");
  443. debug("Read DQS gating calibration:\n");
  444. debug("\tMPDGCTRL0 PHY0 = 0x%08X\n", readl(&mmdc0->mpdgctrl0));
  445. debug("\tMPDGCTRL1 PHY0 = 0x%08X\n", readl(&mmdc0->mpdgctrl1));
  446. if (sysinfo->dsize == 2) {
  447. debug("\tMPDGCTRL0 PHY1 = 0x%08X\n", readl(&mmdc1->mpdgctrl0));
  448. debug("\tMPDGCTRL1 PHY1 = 0x%08X\n", readl(&mmdc1->mpdgctrl1));
  449. }
  450. debug("Read calibration:\n");
  451. debug("\tMPRDDLCTL PHY0 = 0x%08X\n", readl(&mmdc0->mprddlctl));
  452. if (sysinfo->dsize == 2)
  453. debug("\tMPRDDLCTL PHY1 = 0x%08X\n", readl(&mmdc1->mprddlctl));
  454. debug("Write calibration:\n");
  455. debug("\tMPWRDLCTL PHY0 = 0x%08X\n", readl(&mmdc0->mpwrdlctl));
  456. if (sysinfo->dsize == 2)
  457. debug("\tMPWRDLCTL PHY1 = 0x%08X\n", readl(&mmdc1->mpwrdlctl));
  458. /*
  459. * Registers below are for debugging purposes. These print out
  460. * the upper and lower boundaries captured during
  461. * read DQS gating calibration.
  462. */
  463. debug("Status registers bounds for read DQS gating:\n");
  464. debug("\tMPDGHWST0 PHY0 = 0x%08x\n", readl(&mmdc0->mpdghwst0));
  465. debug("\tMPDGHWST1 PHY0 = 0x%08x\n", readl(&mmdc0->mpdghwst1));
  466. debug("\tMPDGHWST2 PHY0 = 0x%08x\n", readl(&mmdc0->mpdghwst2));
  467. debug("\tMPDGHWST3 PHY0 = 0x%08x\n", readl(&mmdc0->mpdghwst3));
  468. if (sysinfo->dsize == 2) {
  469. debug("\tMPDGHWST0 PHY1 = 0x%08x\n", readl(&mmdc1->mpdghwst0));
  470. debug("\tMPDGHWST1 PHY1 = 0x%08x\n", readl(&mmdc1->mpdghwst1));
  471. debug("\tMPDGHWST2 PHY1 = 0x%08x\n", readl(&mmdc1->mpdghwst2));
  472. debug("\tMPDGHWST3 PHY1 = 0x%08x\n", readl(&mmdc1->mpdghwst3));
  473. }
  474. debug("Final do_dqs_calibration error mask: 0x%x\n", errors);
  475. return errors;
  476. }
  477. #endif
  478. #if defined(CONFIG_MX6SX)
  479. /* Configure MX6SX mmdc iomux */
  480. void mx6sx_dram_iocfg(unsigned width,
  481. const struct mx6sx_iomux_ddr_regs *ddr,
  482. const struct mx6sx_iomux_grp_regs *grp)
  483. {
  484. struct mx6sx_iomux_ddr_regs *mx6_ddr_iomux;
  485. struct mx6sx_iomux_grp_regs *mx6_grp_iomux;
  486. mx6_ddr_iomux = (struct mx6sx_iomux_ddr_regs *)MX6SX_IOM_DDR_BASE;
  487. mx6_grp_iomux = (struct mx6sx_iomux_grp_regs *)MX6SX_IOM_GRP_BASE;
  488. /* DDR IO TYPE */
  489. writel(grp->grp_ddr_type, &mx6_grp_iomux->grp_ddr_type);
  490. writel(grp->grp_ddrpke, &mx6_grp_iomux->grp_ddrpke);
  491. /* CLOCK */
  492. writel(ddr->dram_sdclk_0, &mx6_ddr_iomux->dram_sdclk_0);
  493. /* ADDRESS */
  494. writel(ddr->dram_cas, &mx6_ddr_iomux->dram_cas);
  495. writel(ddr->dram_ras, &mx6_ddr_iomux->dram_ras);
  496. writel(grp->grp_addds, &mx6_grp_iomux->grp_addds);
  497. /* Control */
  498. writel(ddr->dram_reset, &mx6_ddr_iomux->dram_reset);
  499. writel(ddr->dram_sdba2, &mx6_ddr_iomux->dram_sdba2);
  500. writel(ddr->dram_sdcke0, &mx6_ddr_iomux->dram_sdcke0);
  501. writel(ddr->dram_sdcke1, &mx6_ddr_iomux->dram_sdcke1);
  502. writel(ddr->dram_odt0, &mx6_ddr_iomux->dram_odt0);
  503. writel(ddr->dram_odt1, &mx6_ddr_iomux->dram_odt1);
  504. writel(grp->grp_ctlds, &mx6_grp_iomux->grp_ctlds);
  505. /* Data Strobes */
  506. writel(grp->grp_ddrmode_ctl, &mx6_grp_iomux->grp_ddrmode_ctl);
  507. writel(ddr->dram_sdqs0, &mx6_ddr_iomux->dram_sdqs0);
  508. writel(ddr->dram_sdqs1, &mx6_ddr_iomux->dram_sdqs1);
  509. if (width >= 32) {
  510. writel(ddr->dram_sdqs2, &mx6_ddr_iomux->dram_sdqs2);
  511. writel(ddr->dram_sdqs3, &mx6_ddr_iomux->dram_sdqs3);
  512. }
  513. /* Data */
  514. writel(grp->grp_ddrmode, &mx6_grp_iomux->grp_ddrmode);
  515. writel(grp->grp_b0ds, &mx6_grp_iomux->grp_b0ds);
  516. writel(grp->grp_b1ds, &mx6_grp_iomux->grp_b1ds);
  517. if (width >= 32) {
  518. writel(grp->grp_b2ds, &mx6_grp_iomux->grp_b2ds);
  519. writel(grp->grp_b3ds, &mx6_grp_iomux->grp_b3ds);
  520. }
  521. writel(ddr->dram_dqm0, &mx6_ddr_iomux->dram_dqm0);
  522. writel(ddr->dram_dqm1, &mx6_ddr_iomux->dram_dqm1);
  523. if (width >= 32) {
  524. writel(ddr->dram_dqm2, &mx6_ddr_iomux->dram_dqm2);
  525. writel(ddr->dram_dqm3, &mx6_ddr_iomux->dram_dqm3);
  526. }
  527. }
  528. #endif
  529. #ifdef CONFIG_MX6UL
  530. void mx6ul_dram_iocfg(unsigned width,
  531. const struct mx6ul_iomux_ddr_regs *ddr,
  532. const struct mx6ul_iomux_grp_regs *grp)
  533. {
  534. struct mx6ul_iomux_ddr_regs *mx6_ddr_iomux;
  535. struct mx6ul_iomux_grp_regs *mx6_grp_iomux;
  536. mx6_ddr_iomux = (struct mx6ul_iomux_ddr_regs *)MX6UL_IOM_DDR_BASE;
  537. mx6_grp_iomux = (struct mx6ul_iomux_grp_regs *)MX6UL_IOM_GRP_BASE;
  538. /* DDR IO TYPE */
  539. writel(grp->grp_ddr_type, &mx6_grp_iomux->grp_ddr_type);
  540. writel(grp->grp_ddrpke, &mx6_grp_iomux->grp_ddrpke);
  541. /* CLOCK */
  542. writel(ddr->dram_sdclk_0, &mx6_ddr_iomux->dram_sdclk_0);
  543. /* ADDRESS */
  544. writel(ddr->dram_cas, &mx6_ddr_iomux->dram_cas);
  545. writel(ddr->dram_ras, &mx6_ddr_iomux->dram_ras);
  546. writel(grp->grp_addds, &mx6_grp_iomux->grp_addds);
  547. /* Control */
  548. writel(ddr->dram_reset, &mx6_ddr_iomux->dram_reset);
  549. writel(ddr->dram_sdba2, &mx6_ddr_iomux->dram_sdba2);
  550. writel(ddr->dram_odt0, &mx6_ddr_iomux->dram_odt0);
  551. writel(ddr->dram_odt1, &mx6_ddr_iomux->dram_odt1);
  552. writel(grp->grp_ctlds, &mx6_grp_iomux->grp_ctlds);
  553. /* Data Strobes */
  554. writel(grp->grp_ddrmode_ctl, &mx6_grp_iomux->grp_ddrmode_ctl);
  555. writel(ddr->dram_sdqs0, &mx6_ddr_iomux->dram_sdqs0);
  556. writel(ddr->dram_sdqs1, &mx6_ddr_iomux->dram_sdqs1);
  557. /* Data */
  558. writel(grp->grp_ddrmode, &mx6_grp_iomux->grp_ddrmode);
  559. writel(grp->grp_b0ds, &mx6_grp_iomux->grp_b0ds);
  560. writel(grp->grp_b1ds, &mx6_grp_iomux->grp_b1ds);
  561. writel(ddr->dram_dqm0, &mx6_ddr_iomux->dram_dqm0);
  562. writel(ddr->dram_dqm1, &mx6_ddr_iomux->dram_dqm1);
  563. }
  564. #endif
  565. #if defined(CONFIG_MX6SL)
  566. void mx6sl_dram_iocfg(unsigned width,
  567. const struct mx6sl_iomux_ddr_regs *ddr,
  568. const struct mx6sl_iomux_grp_regs *grp)
  569. {
  570. struct mx6sl_iomux_ddr_regs *mx6_ddr_iomux;
  571. struct mx6sl_iomux_grp_regs *mx6_grp_iomux;
  572. mx6_ddr_iomux = (struct mx6sl_iomux_ddr_regs *)MX6SL_IOM_DDR_BASE;
  573. mx6_grp_iomux = (struct mx6sl_iomux_grp_regs *)MX6SL_IOM_GRP_BASE;
  574. /* DDR IO TYPE */
  575. mx6_grp_iomux->grp_ddr_type = grp->grp_ddr_type;
  576. mx6_grp_iomux->grp_ddrpke = grp->grp_ddrpke;
  577. /* CLOCK */
  578. mx6_ddr_iomux->dram_sdclk_0 = ddr->dram_sdclk_0;
  579. /* ADDRESS */
  580. mx6_ddr_iomux->dram_cas = ddr->dram_cas;
  581. mx6_ddr_iomux->dram_ras = ddr->dram_ras;
  582. mx6_grp_iomux->grp_addds = grp->grp_addds;
  583. /* Control */
  584. mx6_ddr_iomux->dram_reset = ddr->dram_reset;
  585. mx6_ddr_iomux->dram_sdba2 = ddr->dram_sdba2;
  586. mx6_grp_iomux->grp_ctlds = grp->grp_ctlds;
  587. /* Data Strobes */
  588. mx6_grp_iomux->grp_ddrmode_ctl = grp->grp_ddrmode_ctl;
  589. mx6_ddr_iomux->dram_sdqs0 = ddr->dram_sdqs0;
  590. mx6_ddr_iomux->dram_sdqs1 = ddr->dram_sdqs1;
  591. if (width >= 32) {
  592. mx6_ddr_iomux->dram_sdqs2 = ddr->dram_sdqs2;
  593. mx6_ddr_iomux->dram_sdqs3 = ddr->dram_sdqs3;
  594. }
  595. /* Data */
  596. mx6_grp_iomux->grp_ddrmode = grp->grp_ddrmode;
  597. mx6_grp_iomux->grp_b0ds = grp->grp_b0ds;
  598. mx6_grp_iomux->grp_b1ds = grp->grp_b1ds;
  599. if (width >= 32) {
  600. mx6_grp_iomux->grp_b2ds = grp->grp_b2ds;
  601. mx6_grp_iomux->grp_b3ds = grp->grp_b3ds;
  602. }
  603. mx6_ddr_iomux->dram_dqm0 = ddr->dram_dqm0;
  604. mx6_ddr_iomux->dram_dqm1 = ddr->dram_dqm1;
  605. if (width >= 32) {
  606. mx6_ddr_iomux->dram_dqm2 = ddr->dram_dqm2;
  607. mx6_ddr_iomux->dram_dqm3 = ddr->dram_dqm3;
  608. }
  609. }
  610. #endif
  611. #if defined(CONFIG_MX6QDL) || defined(CONFIG_MX6Q) || defined(CONFIG_MX6D)
  612. /* Configure MX6DQ mmdc iomux */
  613. void mx6dq_dram_iocfg(unsigned width,
  614. const struct mx6dq_iomux_ddr_regs *ddr,
  615. const struct mx6dq_iomux_grp_regs *grp)
  616. {
  617. volatile struct mx6dq_iomux_ddr_regs *mx6_ddr_iomux;
  618. volatile struct mx6dq_iomux_grp_regs *mx6_grp_iomux;
  619. mx6_ddr_iomux = (struct mx6dq_iomux_ddr_regs *)MX6DQ_IOM_DDR_BASE;
  620. mx6_grp_iomux = (struct mx6dq_iomux_grp_regs *)MX6DQ_IOM_GRP_BASE;
  621. /* DDR IO Type */
  622. mx6_grp_iomux->grp_ddr_type = grp->grp_ddr_type;
  623. mx6_grp_iomux->grp_ddrpke = grp->grp_ddrpke;
  624. /* Clock */
  625. mx6_ddr_iomux->dram_sdclk_0 = ddr->dram_sdclk_0;
  626. mx6_ddr_iomux->dram_sdclk_1 = ddr->dram_sdclk_1;
  627. /* Address */
  628. mx6_ddr_iomux->dram_cas = ddr->dram_cas;
  629. mx6_ddr_iomux->dram_ras = ddr->dram_ras;
  630. mx6_grp_iomux->grp_addds = grp->grp_addds;
  631. /* Control */
  632. mx6_ddr_iomux->dram_reset = ddr->dram_reset;
  633. mx6_ddr_iomux->dram_sdcke0 = ddr->dram_sdcke0;
  634. mx6_ddr_iomux->dram_sdcke1 = ddr->dram_sdcke1;
  635. mx6_ddr_iomux->dram_sdba2 = ddr->dram_sdba2;
  636. mx6_ddr_iomux->dram_sdodt0 = ddr->dram_sdodt0;
  637. mx6_ddr_iomux->dram_sdodt1 = ddr->dram_sdodt1;
  638. mx6_grp_iomux->grp_ctlds = grp->grp_ctlds;
  639. /* Data Strobes */
  640. mx6_grp_iomux->grp_ddrmode_ctl = grp->grp_ddrmode_ctl;
  641. mx6_ddr_iomux->dram_sdqs0 = ddr->dram_sdqs0;
  642. mx6_ddr_iomux->dram_sdqs1 = ddr->dram_sdqs1;
  643. if (width >= 32) {
  644. mx6_ddr_iomux->dram_sdqs2 = ddr->dram_sdqs2;
  645. mx6_ddr_iomux->dram_sdqs3 = ddr->dram_sdqs3;
  646. }
  647. if (width >= 64) {
  648. mx6_ddr_iomux->dram_sdqs4 = ddr->dram_sdqs4;
  649. mx6_ddr_iomux->dram_sdqs5 = ddr->dram_sdqs5;
  650. mx6_ddr_iomux->dram_sdqs6 = ddr->dram_sdqs6;
  651. mx6_ddr_iomux->dram_sdqs7 = ddr->dram_sdqs7;
  652. }
  653. /* Data */
  654. mx6_grp_iomux->grp_ddrmode = grp->grp_ddrmode;
  655. mx6_grp_iomux->grp_b0ds = grp->grp_b0ds;
  656. mx6_grp_iomux->grp_b1ds = grp->grp_b1ds;
  657. if (width >= 32) {
  658. mx6_grp_iomux->grp_b2ds = grp->grp_b2ds;
  659. mx6_grp_iomux->grp_b3ds = grp->grp_b3ds;
  660. }
  661. if (width >= 64) {
  662. mx6_grp_iomux->grp_b4ds = grp->grp_b4ds;
  663. mx6_grp_iomux->grp_b5ds = grp->grp_b5ds;
  664. mx6_grp_iomux->grp_b6ds = grp->grp_b6ds;
  665. mx6_grp_iomux->grp_b7ds = grp->grp_b7ds;
  666. }
  667. mx6_ddr_iomux->dram_dqm0 = ddr->dram_dqm0;
  668. mx6_ddr_iomux->dram_dqm1 = ddr->dram_dqm1;
  669. if (width >= 32) {
  670. mx6_ddr_iomux->dram_dqm2 = ddr->dram_dqm2;
  671. mx6_ddr_iomux->dram_dqm3 = ddr->dram_dqm3;
  672. }
  673. if (width >= 64) {
  674. mx6_ddr_iomux->dram_dqm4 = ddr->dram_dqm4;
  675. mx6_ddr_iomux->dram_dqm5 = ddr->dram_dqm5;
  676. mx6_ddr_iomux->dram_dqm6 = ddr->dram_dqm6;
  677. mx6_ddr_iomux->dram_dqm7 = ddr->dram_dqm7;
  678. }
  679. }
  680. #endif
  681. #if defined(CONFIG_MX6QDL) || defined(CONFIG_MX6DL) || defined(CONFIG_MX6S)
  682. /* Configure MX6SDL mmdc iomux */
  683. void mx6sdl_dram_iocfg(unsigned width,
  684. const struct mx6sdl_iomux_ddr_regs *ddr,
  685. const struct mx6sdl_iomux_grp_regs *grp)
  686. {
  687. volatile struct mx6sdl_iomux_ddr_regs *mx6_ddr_iomux;
  688. volatile struct mx6sdl_iomux_grp_regs *mx6_grp_iomux;
  689. mx6_ddr_iomux = (struct mx6sdl_iomux_ddr_regs *)MX6SDL_IOM_DDR_BASE;
  690. mx6_grp_iomux = (struct mx6sdl_iomux_grp_regs *)MX6SDL_IOM_GRP_BASE;
  691. /* DDR IO Type */
  692. mx6_grp_iomux->grp_ddr_type = grp->grp_ddr_type;
  693. mx6_grp_iomux->grp_ddrpke = grp->grp_ddrpke;
  694. /* Clock */
  695. mx6_ddr_iomux->dram_sdclk_0 = ddr->dram_sdclk_0;
  696. mx6_ddr_iomux->dram_sdclk_1 = ddr->dram_sdclk_1;
  697. /* Address */
  698. mx6_ddr_iomux->dram_cas = ddr->dram_cas;
  699. mx6_ddr_iomux->dram_ras = ddr->dram_ras;
  700. mx6_grp_iomux->grp_addds = grp->grp_addds;
  701. /* Control */
  702. mx6_ddr_iomux->dram_reset = ddr->dram_reset;
  703. mx6_ddr_iomux->dram_sdcke0 = ddr->dram_sdcke0;
  704. mx6_ddr_iomux->dram_sdcke1 = ddr->dram_sdcke1;
  705. mx6_ddr_iomux->dram_sdba2 = ddr->dram_sdba2;
  706. mx6_ddr_iomux->dram_sdodt0 = ddr->dram_sdodt0;
  707. mx6_ddr_iomux->dram_sdodt1 = ddr->dram_sdodt1;
  708. mx6_grp_iomux->grp_ctlds = grp->grp_ctlds;
  709. /* Data Strobes */
  710. mx6_grp_iomux->grp_ddrmode_ctl = grp->grp_ddrmode_ctl;
  711. mx6_ddr_iomux->dram_sdqs0 = ddr->dram_sdqs0;
  712. mx6_ddr_iomux->dram_sdqs1 = ddr->dram_sdqs1;
  713. if (width >= 32) {
  714. mx6_ddr_iomux->dram_sdqs2 = ddr->dram_sdqs2;
  715. mx6_ddr_iomux->dram_sdqs3 = ddr->dram_sdqs3;
  716. }
  717. if (width >= 64) {
  718. mx6_ddr_iomux->dram_sdqs4 = ddr->dram_sdqs4;
  719. mx6_ddr_iomux->dram_sdqs5 = ddr->dram_sdqs5;
  720. mx6_ddr_iomux->dram_sdqs6 = ddr->dram_sdqs6;
  721. mx6_ddr_iomux->dram_sdqs7 = ddr->dram_sdqs7;
  722. }
  723. /* Data */
  724. mx6_grp_iomux->grp_ddrmode = grp->grp_ddrmode;
  725. mx6_grp_iomux->grp_b0ds = grp->grp_b0ds;
  726. mx6_grp_iomux->grp_b1ds = grp->grp_b1ds;
  727. if (width >= 32) {
  728. mx6_grp_iomux->grp_b2ds = grp->grp_b2ds;
  729. mx6_grp_iomux->grp_b3ds = grp->grp_b3ds;
  730. }
  731. if (width >= 64) {
  732. mx6_grp_iomux->grp_b4ds = grp->grp_b4ds;
  733. mx6_grp_iomux->grp_b5ds = grp->grp_b5ds;
  734. mx6_grp_iomux->grp_b6ds = grp->grp_b6ds;
  735. mx6_grp_iomux->grp_b7ds = grp->grp_b7ds;
  736. }
  737. mx6_ddr_iomux->dram_dqm0 = ddr->dram_dqm0;
  738. mx6_ddr_iomux->dram_dqm1 = ddr->dram_dqm1;
  739. if (width >= 32) {
  740. mx6_ddr_iomux->dram_dqm2 = ddr->dram_dqm2;
  741. mx6_ddr_iomux->dram_dqm3 = ddr->dram_dqm3;
  742. }
  743. if (width >= 64) {
  744. mx6_ddr_iomux->dram_dqm4 = ddr->dram_dqm4;
  745. mx6_ddr_iomux->dram_dqm5 = ddr->dram_dqm5;
  746. mx6_ddr_iomux->dram_dqm6 = ddr->dram_dqm6;
  747. mx6_ddr_iomux->dram_dqm7 = ddr->dram_dqm7;
  748. }
  749. }
  750. #endif
  751. /*
  752. * Configure mx6 mmdc registers based on:
  753. * - board-specific memory configuration
  754. * - board-specific calibration data
  755. * - ddr3/lpddr2 chip details
  756. *
  757. * The various calculations here are derived from the Freescale
  758. * 1. i.Mx6DQSDL DDR3 Script Aid spreadsheet (DOC-94917) designed to generate
  759. * MMDC configuration registers based on memory system and memory chip
  760. * parameters.
  761. *
  762. * 2. i.Mx6SL LPDDR2 Script Aid spreadsheet V0.04 designed to generate MMDC
  763. * configuration registers based on memory system and memory chip
  764. * parameters.
  765. *
  766. * The defaults here are those which were specified in the spreadsheet.
  767. * For details on each register, refer to the IMX6DQRM and/or IMX6SDLRM
  768. * and/or IMX6SLRM section titled MMDC initialization.
  769. */
  770. #define MR(val, ba, cmd, cs1) \
  771. ((val << 16) | (1 << 15) | (cmd << 4) | (cs1 << 3) | ba)
  772. #define MMDC1(entry, value) do { \
  773. if (!is_mx6sx() && !is_mx6ul() && !is_mx6sl()) \
  774. mmdc1->entry = value; \
  775. } while (0)
  776. /*
  777. * According JESD209-2B-LPDDR2: Table 103
  778. * WL: write latency
  779. */
  780. static int lpddr2_wl(uint32_t mem_speed)
  781. {
  782. switch (mem_speed) {
  783. case 1066:
  784. case 933:
  785. return 4;
  786. case 800:
  787. return 3;
  788. case 677:
  789. case 533:
  790. return 2;
  791. case 400:
  792. case 333:
  793. return 1;
  794. default:
  795. puts("invalid memory speed\n");
  796. hang();
  797. }
  798. return 0;
  799. }
  800. /*
  801. * According JESD209-2B-LPDDR2: Table 103
  802. * RL: read latency
  803. */
  804. static int lpddr2_rl(uint32_t mem_speed)
  805. {
  806. switch (mem_speed) {
  807. case 1066:
  808. return 8;
  809. case 933:
  810. return 7;
  811. case 800:
  812. return 6;
  813. case 677:
  814. return 5;
  815. case 533:
  816. return 4;
  817. case 400:
  818. case 333:
  819. return 3;
  820. default:
  821. puts("invalid memory speed\n");
  822. hang();
  823. }
  824. return 0;
  825. }
  826. void mx6_lpddr2_cfg(const struct mx6_ddr_sysinfo *sysinfo,
  827. const struct mx6_mmdc_calibration *calib,
  828. const struct mx6_lpddr2_cfg *lpddr2_cfg)
  829. {
  830. volatile struct mmdc_p_regs *mmdc0;
  831. u32 val;
  832. u8 tcke, tcksrx, tcksre, trrd;
  833. u8 twl, txp, tfaw, tcl;
  834. u16 tras, twr, tmrd, trtp, twtr, trfc, txsr;
  835. u16 trcd_lp, trppb_lp, trpab_lp, trc_lp;
  836. u16 cs0_end;
  837. u8 coladdr;
  838. int clkper; /* clock period in picoseconds */
  839. int clock; /* clock freq in mHz */
  840. int cs;
  841. /* only support 16/32 bits */
  842. if (sysinfo->dsize > 1)
  843. hang();
  844. mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
  845. clock = mxc_get_clock(MXC_DDR_CLK) / 1000000U;
  846. clkper = (1000 * 1000) / clock; /* pico seconds */
  847. twl = lpddr2_wl(lpddr2_cfg->mem_speed) - 1;
  848. /* LPDDR2-S2 and LPDDR2-S4 have the same tRFC value. */
  849. switch (lpddr2_cfg->density) {
  850. case 1:
  851. case 2:
  852. case 4:
  853. trfc = DIV_ROUND_UP(130000, clkper) - 1;
  854. txsr = DIV_ROUND_UP(140000, clkper) - 1;
  855. break;
  856. case 8:
  857. trfc = DIV_ROUND_UP(210000, clkper) - 1;
  858. txsr = DIV_ROUND_UP(220000, clkper) - 1;
  859. break;
  860. default:
  861. /*
  862. * 64Mb, 128Mb, 256Mb, 512Mb are not supported currently.
  863. */
  864. hang();
  865. break;
  866. }
  867. /*
  868. * txpdll, txpr, taonpd and taofpd are not relevant in LPDDR2 mode,
  869. * set them to 0. */
  870. txp = DIV_ROUND_UP(7500, clkper) - 1;
  871. tcke = 3;
  872. if (lpddr2_cfg->mem_speed == 333)
  873. tfaw = DIV_ROUND_UP(60000, clkper) - 1;
  874. else
  875. tfaw = DIV_ROUND_UP(50000, clkper) - 1;
  876. trrd = DIV_ROUND_UP(10000, clkper) - 1;
  877. /* tckesr for LPDDR2 */
  878. tcksre = DIV_ROUND_UP(15000, clkper);
  879. tcksrx = tcksre;
  880. twr = DIV_ROUND_UP(15000, clkper) - 1;
  881. /*
  882. * tMRR: 2, tMRW: 5
  883. * tMRD should be set to max(tMRR, tMRW)
  884. */
  885. tmrd = 5;
  886. tras = DIV_ROUND_UP(lpddr2_cfg->trasmin, clkper / 10) - 1;
  887. /* LPDDR2 mode use tRCD_LP filed in MDCFG3. */
  888. trcd_lp = DIV_ROUND_UP(lpddr2_cfg->trcd_lp, clkper / 10) - 1;
  889. trc_lp = DIV_ROUND_UP(lpddr2_cfg->trasmin + lpddr2_cfg->trppb_lp,
  890. clkper / 10) - 1;
  891. trppb_lp = DIV_ROUND_UP(lpddr2_cfg->trppb_lp, clkper / 10) - 1;
  892. trpab_lp = DIV_ROUND_UP(lpddr2_cfg->trpab_lp, clkper / 10) - 1;
  893. /* To LPDDR2, CL in MDCFG0 refers to RL */
  894. tcl = lpddr2_rl(lpddr2_cfg->mem_speed) - 3;
  895. twtr = DIV_ROUND_UP(7500, clkper) - 1;
  896. trtp = DIV_ROUND_UP(7500, clkper) - 1;
  897. cs0_end = 4 * sysinfo->cs_density - 1;
  898. debug("density:%d Gb (%d Gb per chip)\n",
  899. sysinfo->cs_density, lpddr2_cfg->density);
  900. debug("clock: %dMHz (%d ps)\n", clock, clkper);
  901. debug("memspd:%d\n", lpddr2_cfg->mem_speed);
  902. debug("trcd_lp=%d\n", trcd_lp);
  903. debug("trppb_lp=%d\n", trppb_lp);
  904. debug("trpab_lp=%d\n", trpab_lp);
  905. debug("trc_lp=%d\n", trc_lp);
  906. debug("tcke=%d\n", tcke);
  907. debug("tcksrx=%d\n", tcksrx);
  908. debug("tcksre=%d\n", tcksre);
  909. debug("trfc=%d\n", trfc);
  910. debug("txsr=%d\n", txsr);
  911. debug("txp=%d\n", txp);
  912. debug("tfaw=%d\n", tfaw);
  913. debug("tcl=%d\n", tcl);
  914. debug("tras=%d\n", tras);
  915. debug("twr=%d\n", twr);
  916. debug("tmrd=%d\n", tmrd);
  917. debug("twl=%d\n", twl);
  918. debug("trtp=%d\n", trtp);
  919. debug("twtr=%d\n", twtr);
  920. debug("trrd=%d\n", trrd);
  921. debug("cs0_end=%d\n", cs0_end);
  922. debug("ncs=%d\n", sysinfo->ncs);
  923. /*
  924. * board-specific configuration:
  925. * These values are determined empirically and vary per board layout
  926. */
  927. mmdc0->mpwldectrl0 = calib->p0_mpwldectrl0;
  928. mmdc0->mpwldectrl1 = calib->p0_mpwldectrl1;
  929. mmdc0->mpdgctrl0 = calib->p0_mpdgctrl0;
  930. mmdc0->mpdgctrl1 = calib->p0_mpdgctrl1;
  931. mmdc0->mprddlctl = calib->p0_mprddlctl;
  932. mmdc0->mpwrdlctl = calib->p0_mpwrdlctl;
  933. mmdc0->mpzqlp2ctl = calib->mpzqlp2ctl;
  934. /* Read data DQ Byte0-3 delay */
  935. mmdc0->mprddqby0dl = 0x33333333;
  936. mmdc0->mprddqby1dl = 0x33333333;
  937. if (sysinfo->dsize > 0) {
  938. mmdc0->mprddqby2dl = 0x33333333;
  939. mmdc0->mprddqby3dl = 0x33333333;
  940. }
  941. /* Write data DQ Byte0-3 delay */
  942. mmdc0->mpwrdqby0dl = 0xf3333333;
  943. mmdc0->mpwrdqby1dl = 0xf3333333;
  944. if (sysinfo->dsize > 0) {
  945. mmdc0->mpwrdqby2dl = 0xf3333333;
  946. mmdc0->mpwrdqby3dl = 0xf3333333;
  947. }
  948. /*
  949. * In LPDDR2 mode this register should be cleared,
  950. * so no termination will be activated.
  951. */
  952. mmdc0->mpodtctrl = 0;
  953. /* complete calibration */
  954. val = (1 << 11); /* Force measurement on delay-lines */
  955. mmdc0->mpmur0 = val;
  956. /* Step 1: configuration request */
  957. mmdc0->mdscr = (u32)(1 << 15); /* config request */
  958. /* Step 2: Timing configuration */
  959. mmdc0->mdcfg0 = (trfc << 24) | (txsr << 16) | (txp << 13) |
  960. (tfaw << 4) | tcl;
  961. mmdc0->mdcfg1 = (tras << 16) | (twr << 9) | (tmrd << 5) | twl;
  962. mmdc0->mdcfg2 = (trtp << 6) | (twtr << 3) | trrd;
  963. mmdc0->mdcfg3lp = (trc_lp << 16) | (trcd_lp << 8) |
  964. (trppb_lp << 4) | trpab_lp;
  965. mmdc0->mdotc = 0;
  966. mmdc0->mdasp = cs0_end; /* CS addressing */
  967. /* Step 3: Configure DDR type */
  968. mmdc0->mdmisc = (sysinfo->cs1_mirror << 19) | (sysinfo->walat << 16) |
  969. (sysinfo->bi_on << 12) | (sysinfo->mif3_mode << 9) |
  970. (sysinfo->ralat << 6) | (1 << 3);
  971. /* Step 4: Configure delay while leaving reset */
  972. mmdc0->mdor = (sysinfo->sde_to_rst << 8) |
  973. (sysinfo->rst_to_cke << 0);
  974. /* Step 5: Configure DDR physical parameters (density and burst len) */
  975. coladdr = lpddr2_cfg->coladdr;
  976. if (lpddr2_cfg->coladdr == 8) /* 8-bit COL is 0x3 */
  977. coladdr += 4;
  978. else if (lpddr2_cfg->coladdr == 12) /* 12-bit COL is 0x4 */
  979. coladdr += 1;
  980. mmdc0->mdctl = (lpddr2_cfg->rowaddr - 11) << 24 | /* ROW */
  981. (coladdr - 9) << 20 | /* COL */
  982. (0 << 19) | /* Burst Length = 4 for LPDDR2 */
  983. (sysinfo->dsize << 16); /* DDR data bus size */
  984. /* Step 6: Perform ZQ calibration */
  985. val = 0xa1390003; /* one-time HW ZQ calib */
  986. mmdc0->mpzqhwctrl = val;
  987. /* Step 7: Enable MMDC with desired chip select */
  988. mmdc0->mdctl |= (1 << 31) | /* SDE_0 for CS0 */
  989. ((sysinfo->ncs == 2) ? 1 : 0) << 30; /* SDE_1 for CS1 */
  990. /* Step 8: Write Mode Registers to Init LPDDR2 devices */
  991. for (cs = 0; cs < sysinfo->ncs; cs++) {
  992. /* MR63: reset */
  993. mmdc0->mdscr = MR(63, 0, 3, cs);
  994. /* MR10: calibration,
  995. * 0xff is calibration command after intilization.
  996. */
  997. val = 0xA | (0xff << 8);
  998. mmdc0->mdscr = MR(val, 0, 3, cs);
  999. /* MR1 */
  1000. val = 0x1 | (0x82 << 8);
  1001. mmdc0->mdscr = MR(val, 0, 3, cs);
  1002. /* MR2 */
  1003. val = 0x2 | (0x04 << 8);
  1004. mmdc0->mdscr = MR(val, 0, 3, cs);
  1005. /* MR3 */
  1006. val = 0x3 | (0x02 << 8);
  1007. mmdc0->mdscr = MR(val, 0, 3, cs);
  1008. }
  1009. /* Step 10: Power down control and self-refresh */
  1010. mmdc0->mdpdc = (tcke & 0x7) << 16 |
  1011. 5 << 12 | /* PWDT_1: 256 cycles */
  1012. 5 << 8 | /* PWDT_0: 256 cycles */
  1013. 1 << 6 | /* BOTH_CS_PD */
  1014. (tcksrx & 0x7) << 3 |
  1015. (tcksre & 0x7);
  1016. mmdc0->mapsr = 0x00001006; /* ADOPT power down enabled */
  1017. /* Step 11: Configure ZQ calibration: one-time and periodic 1ms */
  1018. val = 0xa1310003;
  1019. mmdc0->mpzqhwctrl = val;
  1020. /* Step 12: Configure and activate periodic refresh */
  1021. mmdc0->mdref = (sysinfo->refsel << 14) | (sysinfo->refr << 11);
  1022. /* Step 13: Deassert config request - init complete */
  1023. mmdc0->mdscr = 0x00000000;
  1024. /* wait for auto-ZQ calibration to complete */
  1025. mdelay(1);
  1026. }
  1027. void mx6_ddr3_cfg(const struct mx6_ddr_sysinfo *sysinfo,
  1028. const struct mx6_mmdc_calibration *calib,
  1029. const struct mx6_ddr3_cfg *ddr3_cfg)
  1030. {
  1031. volatile struct mmdc_p_regs *mmdc0;
  1032. volatile struct mmdc_p_regs *mmdc1;
  1033. u32 val;
  1034. u8 tcke, tcksrx, tcksre, txpdll, taofpd, taonpd, trrd;
  1035. u8 todtlon, taxpd, tanpd, tcwl, txp, tfaw, tcl;
  1036. u8 todt_idle_off = 0x4; /* from DDR3 Script Aid spreadsheet */
  1037. u16 trcd, trc, tras, twr, tmrd, trtp, trp, twtr, trfc, txs, txpr;
  1038. u16 cs0_end;
  1039. u16 tdllk = 0x1ff; /* DLL locking time: 512 cycles (JEDEC DDR3) */
  1040. u8 coladdr;
  1041. int clkper; /* clock period in picoseconds */
  1042. int clock; /* clock freq in MHz */
  1043. int cs;
  1044. u16 mem_speed = ddr3_cfg->mem_speed;
  1045. mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
  1046. if (!is_mx6sx() && !is_mx6ul() && !is_mx6sl())
  1047. mmdc1 = (struct mmdc_p_regs *)MMDC_P1_BASE_ADDR;
  1048. /* Limit mem_speed for MX6D/MX6Q */
  1049. if (is_mx6dq() || is_mx6dqp()) {
  1050. if (mem_speed > 1066)
  1051. mem_speed = 1066; /* 1066 MT/s */
  1052. tcwl = 4;
  1053. }
  1054. /* Limit mem_speed for MX6S/MX6DL */
  1055. else {
  1056. if (mem_speed > 800)
  1057. mem_speed = 800; /* 800 MT/s */
  1058. tcwl = 3;
  1059. }
  1060. clock = mem_speed / 2;
  1061. /*
  1062. * Data rate of 1066 MT/s requires 533 MHz DDR3 clock, but MX6D/Q supports
  1063. * up to 528 MHz, so reduce the clock to fit chip specs
  1064. */
  1065. if (is_mx6dq() || is_mx6dqp()) {
  1066. if (clock > 528)
  1067. clock = 528; /* 528 MHz */
  1068. }
  1069. clkper = (1000 * 1000) / clock; /* pico seconds */
  1070. todtlon = tcwl;
  1071. taxpd = tcwl;
  1072. tanpd = tcwl;
  1073. switch (ddr3_cfg->density) {
  1074. case 1: /* 1Gb per chip */
  1075. trfc = DIV_ROUND_UP(110000, clkper) - 1;
  1076. txs = DIV_ROUND_UP(120000, clkper) - 1;
  1077. break;
  1078. case 2: /* 2Gb per chip */
  1079. trfc = DIV_ROUND_UP(160000, clkper) - 1;
  1080. txs = DIV_ROUND_UP(170000, clkper) - 1;
  1081. break;
  1082. case 4: /* 4Gb per chip */
  1083. trfc = DIV_ROUND_UP(260000, clkper) - 1;
  1084. txs = DIV_ROUND_UP(270000, clkper) - 1;
  1085. break;
  1086. case 8: /* 8Gb per chip */
  1087. trfc = DIV_ROUND_UP(350000, clkper) - 1;
  1088. txs = DIV_ROUND_UP(360000, clkper) - 1;
  1089. break;
  1090. default:
  1091. /* invalid density */
  1092. puts("invalid chip density\n");
  1093. hang();
  1094. break;
  1095. }
  1096. txpr = txs;
  1097. switch (mem_speed) {
  1098. case 800:
  1099. txp = DIV_ROUND_UP(max(3 * clkper, 7500), clkper) - 1;
  1100. tcke = DIV_ROUND_UP(max(3 * clkper, 7500), clkper) - 1;
  1101. if (ddr3_cfg->pagesz == 1) {
  1102. tfaw = DIV_ROUND_UP(40000, clkper) - 1;
  1103. trrd = DIV_ROUND_UP(max(4 * clkper, 10000), clkper) - 1;
  1104. } else {
  1105. tfaw = DIV_ROUND_UP(50000, clkper) - 1;
  1106. trrd = DIV_ROUND_UP(max(4 * clkper, 10000), clkper) - 1;
  1107. }
  1108. break;
  1109. case 1066:
  1110. txp = DIV_ROUND_UP(max(3 * clkper, 7500), clkper) - 1;
  1111. tcke = DIV_ROUND_UP(max(3 * clkper, 5625), clkper) - 1;
  1112. if (ddr3_cfg->pagesz == 1) {
  1113. tfaw = DIV_ROUND_UP(37500, clkper) - 1;
  1114. trrd = DIV_ROUND_UP(max(4 * clkper, 7500), clkper) - 1;
  1115. } else {
  1116. tfaw = DIV_ROUND_UP(50000, clkper) - 1;
  1117. trrd = DIV_ROUND_UP(max(4 * clkper, 10000), clkper) - 1;
  1118. }
  1119. break;
  1120. default:
  1121. puts("invalid memory speed\n");
  1122. hang();
  1123. break;
  1124. }
  1125. txpdll = DIV_ROUND_UP(max(10 * clkper, 24000), clkper) - 1;
  1126. tcksre = DIV_ROUND_UP(max(5 * clkper, 10000), clkper);
  1127. taonpd = DIV_ROUND_UP(2000, clkper) - 1;
  1128. tcksrx = tcksre;
  1129. taofpd = taonpd;
  1130. twr = DIV_ROUND_UP(15000, clkper) - 1;
  1131. tmrd = DIV_ROUND_UP(max(12 * clkper, 15000), clkper) - 1;
  1132. trc = DIV_ROUND_UP(ddr3_cfg->trcmin, clkper / 10) - 1;
  1133. tras = DIV_ROUND_UP(ddr3_cfg->trasmin, clkper / 10) - 1;
  1134. tcl = DIV_ROUND_UP(ddr3_cfg->trcd, clkper / 10) - 3;
  1135. trp = DIV_ROUND_UP(ddr3_cfg->trcd, clkper / 10) - 1;
  1136. twtr = ROUND(max(4 * clkper, 7500) / clkper, 1) - 1;
  1137. trcd = trp;
  1138. trtp = twtr;
  1139. cs0_end = 4 * sysinfo->cs_density - 1;
  1140. debug("density:%d Gb (%d Gb per chip)\n",
  1141. sysinfo->cs_density, ddr3_cfg->density);
  1142. debug("clock: %dMHz (%d ps)\n", clock, clkper);
  1143. debug("memspd:%d\n", mem_speed);
  1144. debug("tcke=%d\n", tcke);
  1145. debug("tcksrx=%d\n", tcksrx);
  1146. debug("tcksre=%d\n", tcksre);
  1147. debug("taofpd=%d\n", taofpd);
  1148. debug("taonpd=%d\n", taonpd);
  1149. debug("todtlon=%d\n", todtlon);
  1150. debug("tanpd=%d\n", tanpd);
  1151. debug("taxpd=%d\n", taxpd);
  1152. debug("trfc=%d\n", trfc);
  1153. debug("txs=%d\n", txs);
  1154. debug("txp=%d\n", txp);
  1155. debug("txpdll=%d\n", txpdll);
  1156. debug("tfaw=%d\n", tfaw);
  1157. debug("tcl=%d\n", tcl);
  1158. debug("trcd=%d\n", trcd);
  1159. debug("trp=%d\n", trp);
  1160. debug("trc=%d\n", trc);
  1161. debug("tras=%d\n", tras);
  1162. debug("twr=%d\n", twr);
  1163. debug("tmrd=%d\n", tmrd);
  1164. debug("tcwl=%d\n", tcwl);
  1165. debug("tdllk=%d\n", tdllk);
  1166. debug("trtp=%d\n", trtp);
  1167. debug("twtr=%d\n", twtr);
  1168. debug("trrd=%d\n", trrd);
  1169. debug("txpr=%d\n", txpr);
  1170. debug("cs0_end=%d\n", cs0_end);
  1171. debug("ncs=%d\n", sysinfo->ncs);
  1172. debug("Rtt_wr=%d\n", sysinfo->rtt_wr);
  1173. debug("Rtt_nom=%d\n", sysinfo->rtt_nom);
  1174. debug("SRT=%d\n", ddr3_cfg->SRT);
  1175. debug("twr=%d\n", twr);
  1176. /*
  1177. * board-specific configuration:
  1178. * These values are determined empirically and vary per board layout
  1179. * see:
  1180. * appnote, ddr3 spreadsheet
  1181. */
  1182. mmdc0->mpwldectrl0 = calib->p0_mpwldectrl0;
  1183. mmdc0->mpwldectrl1 = calib->p0_mpwldectrl1;
  1184. mmdc0->mpdgctrl0 = calib->p0_mpdgctrl0;
  1185. mmdc0->mpdgctrl1 = calib->p0_mpdgctrl1;
  1186. mmdc0->mprddlctl = calib->p0_mprddlctl;
  1187. mmdc0->mpwrdlctl = calib->p0_mpwrdlctl;
  1188. if (sysinfo->dsize > 1) {
  1189. MMDC1(mpwldectrl0, calib->p1_mpwldectrl0);
  1190. MMDC1(mpwldectrl1, calib->p1_mpwldectrl1);
  1191. MMDC1(mpdgctrl0, calib->p1_mpdgctrl0);
  1192. MMDC1(mpdgctrl1, calib->p1_mpdgctrl1);
  1193. MMDC1(mprddlctl, calib->p1_mprddlctl);
  1194. MMDC1(mpwrdlctl, calib->p1_mpwrdlctl);
  1195. }
  1196. /* Read data DQ Byte0-3 delay */
  1197. mmdc0->mprddqby0dl = 0x33333333;
  1198. mmdc0->mprddqby1dl = 0x33333333;
  1199. if (sysinfo->dsize > 0) {
  1200. mmdc0->mprddqby2dl = 0x33333333;
  1201. mmdc0->mprddqby3dl = 0x33333333;
  1202. }
  1203. if (sysinfo->dsize > 1) {
  1204. MMDC1(mprddqby0dl, 0x33333333);
  1205. MMDC1(mprddqby1dl, 0x33333333);
  1206. MMDC1(mprddqby2dl, 0x33333333);
  1207. MMDC1(mprddqby3dl, 0x33333333);
  1208. }
  1209. /* MMDC Termination: rtt_nom:2 RZQ/2(120ohm), rtt_nom:1 RZQ/4(60ohm) */
  1210. val = (sysinfo->rtt_nom == 2) ? 0x00011117 : 0x00022227;
  1211. mmdc0->mpodtctrl = val;
  1212. if (sysinfo->dsize > 1)
  1213. MMDC1(mpodtctrl, val);
  1214. /* complete calibration */
  1215. val = (1 << 11); /* Force measurement on delay-lines */
  1216. mmdc0->mpmur0 = val;
  1217. if (sysinfo->dsize > 1)
  1218. MMDC1(mpmur0, val);
  1219. /* Step 1: configuration request */
  1220. mmdc0->mdscr = (u32)(1 << 15); /* config request */
  1221. /* Step 2: Timing configuration */
  1222. mmdc0->mdcfg0 = (trfc << 24) | (txs << 16) | (txp << 13) |
  1223. (txpdll << 9) | (tfaw << 4) | tcl;
  1224. mmdc0->mdcfg1 = (trcd << 29) | (trp << 26) | (trc << 21) |
  1225. (tras << 16) | (1 << 15) /* trpa */ |
  1226. (twr << 9) | (tmrd << 5) | tcwl;
  1227. mmdc0->mdcfg2 = (tdllk << 16) | (trtp << 6) | (twtr << 3) | trrd;
  1228. mmdc0->mdotc = (taofpd << 27) | (taonpd << 24) | (tanpd << 20) |
  1229. (taxpd << 16) | (todtlon << 12) | (todt_idle_off << 4);
  1230. mmdc0->mdasp = cs0_end; /* CS addressing */
  1231. /* Step 3: Configure DDR type */
  1232. mmdc0->mdmisc = (sysinfo->cs1_mirror << 19) | (sysinfo->walat << 16) |
  1233. (sysinfo->bi_on << 12) | (sysinfo->mif3_mode << 9) |
  1234. (sysinfo->ralat << 6);
  1235. /* Step 4: Configure delay while leaving reset */
  1236. mmdc0->mdor = (txpr << 16) | (sysinfo->sde_to_rst << 8) |
  1237. (sysinfo->rst_to_cke << 0);
  1238. /* Step 5: Configure DDR physical parameters (density and burst len) */
  1239. coladdr = ddr3_cfg->coladdr;
  1240. if (ddr3_cfg->coladdr == 8) /* 8-bit COL is 0x3 */
  1241. coladdr += 4;
  1242. else if (ddr3_cfg->coladdr == 12) /* 12-bit COL is 0x4 */
  1243. coladdr += 1;
  1244. mmdc0->mdctl = (ddr3_cfg->rowaddr - 11) << 24 | /* ROW */
  1245. (coladdr - 9) << 20 | /* COL */
  1246. (1 << 19) | /* Burst Length = 8 for DDR3 */
  1247. (sysinfo->dsize << 16); /* DDR data bus size */
  1248. /* Step 6: Perform ZQ calibration */
  1249. val = 0xa1390001; /* one-time HW ZQ calib */
  1250. mmdc0->mpzqhwctrl = val;
  1251. if (sysinfo->dsize > 1)
  1252. MMDC1(mpzqhwctrl, val);
  1253. /* Step 7: Enable MMDC with desired chip select */
  1254. mmdc0->mdctl |= (1 << 31) | /* SDE_0 for CS0 */
  1255. ((sysinfo->ncs == 2) ? 1 : 0) << 30; /* SDE_1 for CS1 */
  1256. /* Step 8: Write Mode Registers to Init DDR3 devices */
  1257. for (cs = 0; cs < sysinfo->ncs; cs++) {
  1258. /* MR2 */
  1259. val = (sysinfo->rtt_wr & 3) << 9 | (ddr3_cfg->SRT & 1) << 7 |
  1260. ((tcwl - 3) & 3) << 3;
  1261. debug("MR2 CS%d: 0x%08x\n", cs, (u32)MR(val, 2, 3, cs));
  1262. mmdc0->mdscr = MR(val, 2, 3, cs);
  1263. /* MR3 */
  1264. debug("MR3 CS%d: 0x%08x\n", cs, (u32)MR(0, 3, 3, cs));
  1265. mmdc0->mdscr = MR(0, 3, 3, cs);
  1266. /* MR1 */
  1267. val = ((sysinfo->rtt_nom & 1) ? 1 : 0) << 2 |
  1268. ((sysinfo->rtt_nom & 2) ? 1 : 0) << 6;
  1269. debug("MR1 CS%d: 0x%08x\n", cs, (u32)MR(val, 1, 3, cs));
  1270. mmdc0->mdscr = MR(val, 1, 3, cs);
  1271. /* MR0 */
  1272. val = ((tcl - 1) << 4) | /* CAS */
  1273. (1 << 8) | /* DLL Reset */
  1274. ((twr - 3) << 9) | /* Write Recovery */
  1275. (sysinfo->pd_fast_exit << 12); /* Precharge PD PLL on */
  1276. debug("MR0 CS%d: 0x%08x\n", cs, (u32)MR(val, 0, 3, cs));
  1277. mmdc0->mdscr = MR(val, 0, 3, cs);
  1278. /* ZQ calibration */
  1279. val = (1 << 10);
  1280. mmdc0->mdscr = MR(val, 0, 4, cs);
  1281. }
  1282. /* Step 10: Power down control and self-refresh */
  1283. mmdc0->mdpdc = (tcke & 0x7) << 16 |
  1284. 5 << 12 | /* PWDT_1: 256 cycles */
  1285. 5 << 8 | /* PWDT_0: 256 cycles */
  1286. 1 << 6 | /* BOTH_CS_PD */
  1287. (tcksrx & 0x7) << 3 |
  1288. (tcksre & 0x7);
  1289. if (!sysinfo->pd_fast_exit)
  1290. mmdc0->mdpdc |= (1 << 7); /* SLOW_PD */
  1291. mmdc0->mapsr = 0x00001006; /* ADOPT power down enabled */
  1292. /* Step 11: Configure ZQ calibration: one-time and periodic 1ms */
  1293. val = 0xa1390003;
  1294. mmdc0->mpzqhwctrl = val;
  1295. if (sysinfo->dsize > 1)
  1296. MMDC1(mpzqhwctrl, val);
  1297. /* Step 12: Configure and activate periodic refresh */
  1298. mmdc0->mdref = (sysinfo->refsel << 14) | (sysinfo->refr << 11);
  1299. /* Step 13: Deassert config request - init complete */
  1300. mmdc0->mdscr = 0x00000000;
  1301. /* wait for auto-ZQ calibration to complete */
  1302. mdelay(1);
  1303. }
  1304. void mmdc_read_calibration(struct mx6_ddr_sysinfo const *sysinfo,
  1305. struct mx6_mmdc_calibration *calib)
  1306. {
  1307. struct mmdc_p_regs *mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
  1308. struct mmdc_p_regs *mmdc1 = (struct mmdc_p_regs *)MMDC_P1_BASE_ADDR;
  1309. calib->p0_mpwldectrl0 = readl(&mmdc0->mpwldectrl0);
  1310. calib->p0_mpwldectrl1 = readl(&mmdc0->mpwldectrl1);
  1311. calib->p0_mpdgctrl0 = readl(&mmdc0->mpdgctrl0);
  1312. calib->p0_mpdgctrl1 = readl(&mmdc0->mpdgctrl1);
  1313. calib->p0_mprddlctl = readl(&mmdc0->mprddlctl);
  1314. calib->p0_mpwrdlctl = readl(&mmdc0->mpwrdlctl);
  1315. if (sysinfo->dsize == 2) {
  1316. calib->p1_mpwldectrl0 = readl(&mmdc1->mpwldectrl0);
  1317. calib->p1_mpwldectrl1 = readl(&mmdc1->mpwldectrl1);
  1318. calib->p1_mpdgctrl0 = readl(&mmdc1->mpdgctrl0);
  1319. calib->p1_mpdgctrl1 = readl(&mmdc1->mpdgctrl1);
  1320. calib->p1_mprddlctl = readl(&mmdc1->mprddlctl);
  1321. calib->p1_mpwrdlctl = readl(&mmdc1->mpwrdlctl);
  1322. }
  1323. }
  1324. void mx6_dram_cfg(const struct mx6_ddr_sysinfo *sysinfo,
  1325. const struct mx6_mmdc_calibration *calib,
  1326. const void *ddr_cfg)
  1327. {
  1328. if (sysinfo->ddr_type == DDR_TYPE_DDR3) {
  1329. mx6_ddr3_cfg(sysinfo, calib, ddr_cfg);
  1330. } else if (sysinfo->ddr_type == DDR_TYPE_LPDDR2) {
  1331. mx6_lpddr2_cfg(sysinfo, calib, ddr_cfg);
  1332. } else {
  1333. puts("Unsupported ddr type\n");
  1334. hang();
  1335. }
  1336. }