ddr.c 46 KB

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