ddr.c 47 KB

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