ddr.c 45 KB

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