ddr.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516
  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_MX6QDL) || defined(CONFIG_MX6Q) || defined(CONFIG_MX6D)
  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 mx6_dram_cfg(const struct mx6_ddr_sysinfo *sysinfo,
  1305. const struct mx6_mmdc_calibration *calib,
  1306. const void *ddr_cfg)
  1307. {
  1308. if (sysinfo->ddr_type == DDR_TYPE_DDR3) {
  1309. mx6_ddr3_cfg(sysinfo, calib, ddr_cfg);
  1310. } else if (sysinfo->ddr_type == DDR_TYPE_LPDDR2) {
  1311. mx6_lpddr2_cfg(sysinfo, calib, ddr_cfg);
  1312. } else {
  1313. puts("Unsupported ddr type\n");
  1314. hang();
  1315. }
  1316. }