ddr.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  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_MX6SX)
  15. /* Configure MX6SX mmdc iomux */
  16. void mx6sx_dram_iocfg(unsigned width,
  17. const struct mx6sx_iomux_ddr_regs *ddr,
  18. const struct mx6sx_iomux_grp_regs *grp)
  19. {
  20. struct mx6sx_iomux_ddr_regs *mx6_ddr_iomux;
  21. struct mx6sx_iomux_grp_regs *mx6_grp_iomux;
  22. mx6_ddr_iomux = (struct mx6sx_iomux_ddr_regs *)MX6SX_IOM_DDR_BASE;
  23. mx6_grp_iomux = (struct mx6sx_iomux_grp_regs *)MX6SX_IOM_GRP_BASE;
  24. /* DDR IO TYPE */
  25. writel(grp->grp_ddr_type, &mx6_grp_iomux->grp_ddr_type);
  26. writel(grp->grp_ddrpke, &mx6_grp_iomux->grp_ddrpke);
  27. /* CLOCK */
  28. writel(ddr->dram_sdclk_0, &mx6_ddr_iomux->dram_sdclk_0);
  29. /* ADDRESS */
  30. writel(ddr->dram_cas, &mx6_ddr_iomux->dram_cas);
  31. writel(ddr->dram_ras, &mx6_ddr_iomux->dram_ras);
  32. writel(grp->grp_addds, &mx6_grp_iomux->grp_addds);
  33. /* Control */
  34. writel(ddr->dram_reset, &mx6_ddr_iomux->dram_reset);
  35. writel(ddr->dram_sdba2, &mx6_ddr_iomux->dram_sdba2);
  36. writel(ddr->dram_sdcke0, &mx6_ddr_iomux->dram_sdcke0);
  37. writel(ddr->dram_sdcke1, &mx6_ddr_iomux->dram_sdcke1);
  38. writel(ddr->dram_odt0, &mx6_ddr_iomux->dram_odt0);
  39. writel(ddr->dram_odt1, &mx6_ddr_iomux->dram_odt1);
  40. writel(grp->grp_ctlds, &mx6_grp_iomux->grp_ctlds);
  41. /* Data Strobes */
  42. writel(grp->grp_ddrmode_ctl, &mx6_grp_iomux->grp_ddrmode_ctl);
  43. writel(ddr->dram_sdqs0, &mx6_ddr_iomux->dram_sdqs0);
  44. writel(ddr->dram_sdqs1, &mx6_ddr_iomux->dram_sdqs1);
  45. if (width >= 32) {
  46. writel(ddr->dram_sdqs2, &mx6_ddr_iomux->dram_sdqs2);
  47. writel(ddr->dram_sdqs3, &mx6_ddr_iomux->dram_sdqs3);
  48. }
  49. /* Data */
  50. writel(grp->grp_ddrmode, &mx6_grp_iomux->grp_ddrmode);
  51. writel(grp->grp_b0ds, &mx6_grp_iomux->grp_b0ds);
  52. writel(grp->grp_b1ds, &mx6_grp_iomux->grp_b1ds);
  53. if (width >= 32) {
  54. writel(grp->grp_b2ds, &mx6_grp_iomux->grp_b2ds);
  55. writel(grp->grp_b3ds, &mx6_grp_iomux->grp_b3ds);
  56. }
  57. writel(ddr->dram_dqm0, &mx6_ddr_iomux->dram_dqm0);
  58. writel(ddr->dram_dqm1, &mx6_ddr_iomux->dram_dqm1);
  59. if (width >= 32) {
  60. writel(ddr->dram_dqm2, &mx6_ddr_iomux->dram_dqm2);
  61. writel(ddr->dram_dqm3, &mx6_ddr_iomux->dram_dqm3);
  62. }
  63. }
  64. #endif
  65. #ifdef CONFIG_MX6UL
  66. void mx6ul_dram_iocfg(unsigned width,
  67. const struct mx6ul_iomux_ddr_regs *ddr,
  68. const struct mx6ul_iomux_grp_regs *grp)
  69. {
  70. struct mx6ul_iomux_ddr_regs *mx6_ddr_iomux;
  71. struct mx6ul_iomux_grp_regs *mx6_grp_iomux;
  72. mx6_ddr_iomux = (struct mx6ul_iomux_ddr_regs *)MX6UL_IOM_DDR_BASE;
  73. mx6_grp_iomux = (struct mx6ul_iomux_grp_regs *)MX6UL_IOM_GRP_BASE;
  74. /* DDR IO TYPE */
  75. writel(grp->grp_ddr_type, &mx6_grp_iomux->grp_ddr_type);
  76. writel(grp->grp_ddrpke, &mx6_grp_iomux->grp_ddrpke);
  77. /* CLOCK */
  78. writel(ddr->dram_sdclk_0, &mx6_ddr_iomux->dram_sdclk_0);
  79. /* ADDRESS */
  80. writel(ddr->dram_cas, &mx6_ddr_iomux->dram_cas);
  81. writel(ddr->dram_ras, &mx6_ddr_iomux->dram_ras);
  82. writel(grp->grp_addds, &mx6_grp_iomux->grp_addds);
  83. /* Control */
  84. writel(ddr->dram_reset, &mx6_ddr_iomux->dram_reset);
  85. writel(ddr->dram_sdba2, &mx6_ddr_iomux->dram_sdba2);
  86. writel(ddr->dram_odt0, &mx6_ddr_iomux->dram_odt0);
  87. writel(ddr->dram_odt1, &mx6_ddr_iomux->dram_odt1);
  88. writel(grp->grp_ctlds, &mx6_grp_iomux->grp_ctlds);
  89. /* Data Strobes */
  90. writel(grp->grp_ddrmode_ctl, &mx6_grp_iomux->grp_ddrmode_ctl);
  91. writel(ddr->dram_sdqs0, &mx6_ddr_iomux->dram_sdqs0);
  92. writel(ddr->dram_sdqs1, &mx6_ddr_iomux->dram_sdqs1);
  93. /* Data */
  94. writel(grp->grp_ddrmode, &mx6_grp_iomux->grp_ddrmode);
  95. writel(grp->grp_b0ds, &mx6_grp_iomux->grp_b0ds);
  96. writel(grp->grp_b1ds, &mx6_grp_iomux->grp_b1ds);
  97. writel(ddr->dram_dqm0, &mx6_ddr_iomux->dram_dqm0);
  98. writel(ddr->dram_dqm1, &mx6_ddr_iomux->dram_dqm1);
  99. }
  100. #endif
  101. #if defined(CONFIG_MX6SL)
  102. void mx6sl_dram_iocfg(unsigned width,
  103. const struct mx6sl_iomux_ddr_regs *ddr,
  104. const struct mx6sl_iomux_grp_regs *grp)
  105. {
  106. struct mx6sl_iomux_ddr_regs *mx6_ddr_iomux;
  107. struct mx6sl_iomux_grp_regs *mx6_grp_iomux;
  108. mx6_ddr_iomux = (struct mx6sl_iomux_ddr_regs *)MX6SL_IOM_DDR_BASE;
  109. mx6_grp_iomux = (struct mx6sl_iomux_grp_regs *)MX6SL_IOM_GRP_BASE;
  110. /* DDR IO TYPE */
  111. mx6_grp_iomux->grp_ddr_type = grp->grp_ddr_type;
  112. mx6_grp_iomux->grp_ddrpke = grp->grp_ddrpke;
  113. /* CLOCK */
  114. mx6_ddr_iomux->dram_sdclk_0 = ddr->dram_sdclk_0;
  115. /* ADDRESS */
  116. mx6_ddr_iomux->dram_cas = ddr->dram_cas;
  117. mx6_ddr_iomux->dram_ras = ddr->dram_ras;
  118. mx6_grp_iomux->grp_addds = grp->grp_addds;
  119. /* Control */
  120. mx6_ddr_iomux->dram_reset = ddr->dram_reset;
  121. mx6_ddr_iomux->dram_sdba2 = ddr->dram_sdba2;
  122. mx6_grp_iomux->grp_ctlds = grp->grp_ctlds;
  123. /* Data Strobes */
  124. mx6_grp_iomux->grp_ddrmode_ctl = grp->grp_ddrmode_ctl;
  125. mx6_ddr_iomux->dram_sdqs0 = ddr->dram_sdqs0;
  126. mx6_ddr_iomux->dram_sdqs1 = ddr->dram_sdqs1;
  127. if (width >= 32) {
  128. mx6_ddr_iomux->dram_sdqs2 = ddr->dram_sdqs2;
  129. mx6_ddr_iomux->dram_sdqs3 = ddr->dram_sdqs3;
  130. }
  131. /* Data */
  132. mx6_grp_iomux->grp_ddrmode = grp->grp_ddrmode;
  133. mx6_grp_iomux->grp_b0ds = grp->grp_b0ds;
  134. mx6_grp_iomux->grp_b1ds = grp->grp_b1ds;
  135. if (width >= 32) {
  136. mx6_grp_iomux->grp_b2ds = grp->grp_b2ds;
  137. mx6_grp_iomux->grp_b3ds = grp->grp_b3ds;
  138. }
  139. mx6_ddr_iomux->dram_dqm0 = ddr->dram_dqm0;
  140. mx6_ddr_iomux->dram_dqm1 = ddr->dram_dqm1;
  141. if (width >= 32) {
  142. mx6_ddr_iomux->dram_dqm2 = ddr->dram_dqm2;
  143. mx6_ddr_iomux->dram_dqm3 = ddr->dram_dqm3;
  144. }
  145. }
  146. #endif
  147. #if defined(CONFIG_MX6QDL) || defined(CONFIG_MX6Q) || defined(CONFIG_MX6D)
  148. /* Configure MX6DQ mmdc iomux */
  149. void mx6dq_dram_iocfg(unsigned width,
  150. const struct mx6dq_iomux_ddr_regs *ddr,
  151. const struct mx6dq_iomux_grp_regs *grp)
  152. {
  153. volatile struct mx6dq_iomux_ddr_regs *mx6_ddr_iomux;
  154. volatile struct mx6dq_iomux_grp_regs *mx6_grp_iomux;
  155. mx6_ddr_iomux = (struct mx6dq_iomux_ddr_regs *)MX6DQ_IOM_DDR_BASE;
  156. mx6_grp_iomux = (struct mx6dq_iomux_grp_regs *)MX6DQ_IOM_GRP_BASE;
  157. /* DDR IO Type */
  158. mx6_grp_iomux->grp_ddr_type = grp->grp_ddr_type;
  159. mx6_grp_iomux->grp_ddrpke = grp->grp_ddrpke;
  160. /* Clock */
  161. mx6_ddr_iomux->dram_sdclk_0 = ddr->dram_sdclk_0;
  162. mx6_ddr_iomux->dram_sdclk_1 = ddr->dram_sdclk_1;
  163. /* Address */
  164. mx6_ddr_iomux->dram_cas = ddr->dram_cas;
  165. mx6_ddr_iomux->dram_ras = ddr->dram_ras;
  166. mx6_grp_iomux->grp_addds = grp->grp_addds;
  167. /* Control */
  168. mx6_ddr_iomux->dram_reset = ddr->dram_reset;
  169. mx6_ddr_iomux->dram_sdcke0 = ddr->dram_sdcke0;
  170. mx6_ddr_iomux->dram_sdcke1 = ddr->dram_sdcke1;
  171. mx6_ddr_iomux->dram_sdba2 = ddr->dram_sdba2;
  172. mx6_ddr_iomux->dram_sdodt0 = ddr->dram_sdodt0;
  173. mx6_ddr_iomux->dram_sdodt1 = ddr->dram_sdodt1;
  174. mx6_grp_iomux->grp_ctlds = grp->grp_ctlds;
  175. /* Data Strobes */
  176. mx6_grp_iomux->grp_ddrmode_ctl = grp->grp_ddrmode_ctl;
  177. mx6_ddr_iomux->dram_sdqs0 = ddr->dram_sdqs0;
  178. mx6_ddr_iomux->dram_sdqs1 = ddr->dram_sdqs1;
  179. if (width >= 32) {
  180. mx6_ddr_iomux->dram_sdqs2 = ddr->dram_sdqs2;
  181. mx6_ddr_iomux->dram_sdqs3 = ddr->dram_sdqs3;
  182. }
  183. if (width >= 64) {
  184. mx6_ddr_iomux->dram_sdqs4 = ddr->dram_sdqs4;
  185. mx6_ddr_iomux->dram_sdqs5 = ddr->dram_sdqs5;
  186. mx6_ddr_iomux->dram_sdqs6 = ddr->dram_sdqs6;
  187. mx6_ddr_iomux->dram_sdqs7 = ddr->dram_sdqs7;
  188. }
  189. /* Data */
  190. mx6_grp_iomux->grp_ddrmode = grp->grp_ddrmode;
  191. mx6_grp_iomux->grp_b0ds = grp->grp_b0ds;
  192. mx6_grp_iomux->grp_b1ds = grp->grp_b1ds;
  193. if (width >= 32) {
  194. mx6_grp_iomux->grp_b2ds = grp->grp_b2ds;
  195. mx6_grp_iomux->grp_b3ds = grp->grp_b3ds;
  196. }
  197. if (width >= 64) {
  198. mx6_grp_iomux->grp_b4ds = grp->grp_b4ds;
  199. mx6_grp_iomux->grp_b5ds = grp->grp_b5ds;
  200. mx6_grp_iomux->grp_b6ds = grp->grp_b6ds;
  201. mx6_grp_iomux->grp_b7ds = grp->grp_b7ds;
  202. }
  203. mx6_ddr_iomux->dram_dqm0 = ddr->dram_dqm0;
  204. mx6_ddr_iomux->dram_dqm1 = ddr->dram_dqm1;
  205. if (width >= 32) {
  206. mx6_ddr_iomux->dram_dqm2 = ddr->dram_dqm2;
  207. mx6_ddr_iomux->dram_dqm3 = ddr->dram_dqm3;
  208. }
  209. if (width >= 64) {
  210. mx6_ddr_iomux->dram_dqm4 = ddr->dram_dqm4;
  211. mx6_ddr_iomux->dram_dqm5 = ddr->dram_dqm5;
  212. mx6_ddr_iomux->dram_dqm6 = ddr->dram_dqm6;
  213. mx6_ddr_iomux->dram_dqm7 = ddr->dram_dqm7;
  214. }
  215. }
  216. #endif
  217. #if defined(CONFIG_MX6QDL) || defined(CONFIG_MX6DL) || defined(CONFIG_MX6S)
  218. /* Configure MX6SDL mmdc iomux */
  219. void mx6sdl_dram_iocfg(unsigned width,
  220. const struct mx6sdl_iomux_ddr_regs *ddr,
  221. const struct mx6sdl_iomux_grp_regs *grp)
  222. {
  223. volatile struct mx6sdl_iomux_ddr_regs *mx6_ddr_iomux;
  224. volatile struct mx6sdl_iomux_grp_regs *mx6_grp_iomux;
  225. mx6_ddr_iomux = (struct mx6sdl_iomux_ddr_regs *)MX6SDL_IOM_DDR_BASE;
  226. mx6_grp_iomux = (struct mx6sdl_iomux_grp_regs *)MX6SDL_IOM_GRP_BASE;
  227. /* DDR IO Type */
  228. mx6_grp_iomux->grp_ddr_type = grp->grp_ddr_type;
  229. mx6_grp_iomux->grp_ddrpke = grp->grp_ddrpke;
  230. /* Clock */
  231. mx6_ddr_iomux->dram_sdclk_0 = ddr->dram_sdclk_0;
  232. mx6_ddr_iomux->dram_sdclk_1 = ddr->dram_sdclk_1;
  233. /* Address */
  234. mx6_ddr_iomux->dram_cas = ddr->dram_cas;
  235. mx6_ddr_iomux->dram_ras = ddr->dram_ras;
  236. mx6_grp_iomux->grp_addds = grp->grp_addds;
  237. /* Control */
  238. mx6_ddr_iomux->dram_reset = ddr->dram_reset;
  239. mx6_ddr_iomux->dram_sdcke0 = ddr->dram_sdcke0;
  240. mx6_ddr_iomux->dram_sdcke1 = ddr->dram_sdcke1;
  241. mx6_ddr_iomux->dram_sdba2 = ddr->dram_sdba2;
  242. mx6_ddr_iomux->dram_sdodt0 = ddr->dram_sdodt0;
  243. mx6_ddr_iomux->dram_sdodt1 = ddr->dram_sdodt1;
  244. mx6_grp_iomux->grp_ctlds = grp->grp_ctlds;
  245. /* Data Strobes */
  246. mx6_grp_iomux->grp_ddrmode_ctl = grp->grp_ddrmode_ctl;
  247. mx6_ddr_iomux->dram_sdqs0 = ddr->dram_sdqs0;
  248. mx6_ddr_iomux->dram_sdqs1 = ddr->dram_sdqs1;
  249. if (width >= 32) {
  250. mx6_ddr_iomux->dram_sdqs2 = ddr->dram_sdqs2;
  251. mx6_ddr_iomux->dram_sdqs3 = ddr->dram_sdqs3;
  252. }
  253. if (width >= 64) {
  254. mx6_ddr_iomux->dram_sdqs4 = ddr->dram_sdqs4;
  255. mx6_ddr_iomux->dram_sdqs5 = ddr->dram_sdqs5;
  256. mx6_ddr_iomux->dram_sdqs6 = ddr->dram_sdqs6;
  257. mx6_ddr_iomux->dram_sdqs7 = ddr->dram_sdqs7;
  258. }
  259. /* Data */
  260. mx6_grp_iomux->grp_ddrmode = grp->grp_ddrmode;
  261. mx6_grp_iomux->grp_b0ds = grp->grp_b0ds;
  262. mx6_grp_iomux->grp_b1ds = grp->grp_b1ds;
  263. if (width >= 32) {
  264. mx6_grp_iomux->grp_b2ds = grp->grp_b2ds;
  265. mx6_grp_iomux->grp_b3ds = grp->grp_b3ds;
  266. }
  267. if (width >= 64) {
  268. mx6_grp_iomux->grp_b4ds = grp->grp_b4ds;
  269. mx6_grp_iomux->grp_b5ds = grp->grp_b5ds;
  270. mx6_grp_iomux->grp_b6ds = grp->grp_b6ds;
  271. mx6_grp_iomux->grp_b7ds = grp->grp_b7ds;
  272. }
  273. mx6_ddr_iomux->dram_dqm0 = ddr->dram_dqm0;
  274. mx6_ddr_iomux->dram_dqm1 = ddr->dram_dqm1;
  275. if (width >= 32) {
  276. mx6_ddr_iomux->dram_dqm2 = ddr->dram_dqm2;
  277. mx6_ddr_iomux->dram_dqm3 = ddr->dram_dqm3;
  278. }
  279. if (width >= 64) {
  280. mx6_ddr_iomux->dram_dqm4 = ddr->dram_dqm4;
  281. mx6_ddr_iomux->dram_dqm5 = ddr->dram_dqm5;
  282. mx6_ddr_iomux->dram_dqm6 = ddr->dram_dqm6;
  283. mx6_ddr_iomux->dram_dqm7 = ddr->dram_dqm7;
  284. }
  285. }
  286. #endif
  287. /*
  288. * Configure mx6 mmdc registers based on:
  289. * - board-specific memory configuration
  290. * - board-specific calibration data
  291. * - ddr3/lpddr2 chip details
  292. *
  293. * The various calculations here are derived from the Freescale
  294. * 1. i.Mx6DQSDL DDR3 Script Aid spreadsheet (DOC-94917) designed to generate
  295. * MMDC configuration registers based on memory system and memory chip
  296. * parameters.
  297. *
  298. * 2. i.Mx6SL LPDDR2 Script Aid spreadsheet V0.04 designed to generate MMDC
  299. * configuration registers based on memory system and memory chip
  300. * parameters.
  301. *
  302. * The defaults here are those which were specified in the spreadsheet.
  303. * For details on each register, refer to the IMX6DQRM and/or IMX6SDLRM
  304. * and/or IMX6SLRM section titled MMDC initialization.
  305. */
  306. #define MR(val, ba, cmd, cs1) \
  307. ((val << 16) | (1 << 15) | (cmd << 4) | (cs1 << 3) | ba)
  308. #define MMDC1(entry, value) do { \
  309. if (!is_cpu_type(MXC_CPU_MX6SX) && !is_cpu_type(MXC_CPU_MX6UL) && \
  310. !is_cpu_type(MXC_CPU_MX6SL)) \
  311. mmdc1->entry = value; \
  312. } while (0)
  313. /*
  314. * According JESD209-2B-LPDDR2: Table 103
  315. * WL: write latency
  316. */
  317. static int lpddr2_wl(uint32_t mem_speed)
  318. {
  319. switch (mem_speed) {
  320. case 1066:
  321. case 933:
  322. return 4;
  323. case 800:
  324. return 3;
  325. case 677:
  326. case 533:
  327. return 2;
  328. case 400:
  329. case 333:
  330. return 1;
  331. default:
  332. puts("invalid memory speed\n");
  333. hang();
  334. }
  335. return 0;
  336. }
  337. /*
  338. * According JESD209-2B-LPDDR2: Table 103
  339. * RL: read latency
  340. */
  341. static int lpddr2_rl(uint32_t mem_speed)
  342. {
  343. switch (mem_speed) {
  344. case 1066:
  345. return 8;
  346. case 933:
  347. return 7;
  348. case 800:
  349. return 6;
  350. case 677:
  351. return 5;
  352. case 533:
  353. return 4;
  354. case 400:
  355. case 333:
  356. return 3;
  357. default:
  358. puts("invalid memory speed\n");
  359. hang();
  360. }
  361. return 0;
  362. }
  363. void mx6_lpddr2_cfg(const struct mx6_ddr_sysinfo *sysinfo,
  364. const struct mx6_mmdc_calibration *calib,
  365. const struct mx6_lpddr2_cfg *lpddr2_cfg)
  366. {
  367. volatile struct mmdc_p_regs *mmdc0;
  368. u32 val;
  369. u8 tcke, tcksrx, tcksre, trrd;
  370. u8 twl, txp, tfaw, tcl;
  371. u16 tras, twr, tmrd, trtp, twtr, trfc, txsr;
  372. u16 trcd_lp, trppb_lp, trpab_lp, trc_lp;
  373. u16 cs0_end;
  374. u8 coladdr;
  375. int clkper; /* clock period in picoseconds */
  376. int clock; /* clock freq in mHz */
  377. int cs;
  378. /* only support 16/32 bits */
  379. if (sysinfo->dsize > 1)
  380. hang();
  381. mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
  382. clock = mxc_get_clock(MXC_DDR_CLK) / 1000000U;
  383. clkper = (1000 * 1000) / clock; /* pico seconds */
  384. twl = lpddr2_wl(lpddr2_cfg->mem_speed) - 1;
  385. /* LPDDR2-S2 and LPDDR2-S4 have the same tRFC value. */
  386. switch (lpddr2_cfg->density) {
  387. case 1:
  388. case 2:
  389. case 4:
  390. trfc = DIV_ROUND_UP(130000, clkper) - 1;
  391. txsr = DIV_ROUND_UP(140000, clkper) - 1;
  392. break;
  393. case 8:
  394. trfc = DIV_ROUND_UP(210000, clkper) - 1;
  395. txsr = DIV_ROUND_UP(220000, clkper) - 1;
  396. break;
  397. default:
  398. /*
  399. * 64Mb, 128Mb, 256Mb, 512Mb are not supported currently.
  400. */
  401. hang();
  402. break;
  403. }
  404. /*
  405. * txpdll, txpr, taonpd and taofpd are not relevant in LPDDR2 mode,
  406. * set them to 0. */
  407. txp = DIV_ROUND_UP(7500, clkper) - 1;
  408. tcke = 3;
  409. if (lpddr2_cfg->mem_speed == 333)
  410. tfaw = DIV_ROUND_UP(60000, clkper) - 1;
  411. else
  412. tfaw = DIV_ROUND_UP(50000, clkper) - 1;
  413. trrd = DIV_ROUND_UP(10000, clkper) - 1;
  414. /* tckesr for LPDDR2 */
  415. tcksre = DIV_ROUND_UP(15000, clkper);
  416. tcksrx = tcksre;
  417. twr = DIV_ROUND_UP(15000, clkper) - 1;
  418. /*
  419. * tMRR: 2, tMRW: 5
  420. * tMRD should be set to max(tMRR, tMRW)
  421. */
  422. tmrd = 5;
  423. tras = DIV_ROUND_UP(lpddr2_cfg->trasmin, clkper / 10) - 1;
  424. /* LPDDR2 mode use tRCD_LP filed in MDCFG3. */
  425. trcd_lp = DIV_ROUND_UP(lpddr2_cfg->trcd_lp, clkper / 10) - 1;
  426. trc_lp = DIV_ROUND_UP(lpddr2_cfg->trasmin + lpddr2_cfg->trppb_lp,
  427. clkper / 10) - 1;
  428. trppb_lp = DIV_ROUND_UP(lpddr2_cfg->trppb_lp, clkper / 10) - 1;
  429. trpab_lp = DIV_ROUND_UP(lpddr2_cfg->trpab_lp, clkper / 10) - 1;
  430. /* To LPDDR2, CL in MDCFG0 refers to RL */
  431. tcl = lpddr2_rl(lpddr2_cfg->mem_speed) - 3;
  432. twtr = DIV_ROUND_UP(7500, clkper) - 1;
  433. trtp = DIV_ROUND_UP(7500, clkper) - 1;
  434. cs0_end = 4 * sysinfo->cs_density - 1;
  435. debug("density:%d Gb (%d Gb per chip)\n",
  436. sysinfo->cs_density, lpddr2_cfg->density);
  437. debug("clock: %dMHz (%d ps)\n", clock, clkper);
  438. debug("memspd:%d\n", lpddr2_cfg->mem_speed);
  439. debug("trcd_lp=%d\n", trcd_lp);
  440. debug("trppb_lp=%d\n", trppb_lp);
  441. debug("trpab_lp=%d\n", trpab_lp);
  442. debug("trc_lp=%d\n", trc_lp);
  443. debug("tcke=%d\n", tcke);
  444. debug("tcksrx=%d\n", tcksrx);
  445. debug("tcksre=%d\n", tcksre);
  446. debug("trfc=%d\n", trfc);
  447. debug("txsr=%d\n", txsr);
  448. debug("txp=%d\n", txp);
  449. debug("tfaw=%d\n", tfaw);
  450. debug("tcl=%d\n", tcl);
  451. debug("tras=%d\n", tras);
  452. debug("twr=%d\n", twr);
  453. debug("tmrd=%d\n", tmrd);
  454. debug("twl=%d\n", twl);
  455. debug("trtp=%d\n", trtp);
  456. debug("twtr=%d\n", twtr);
  457. debug("trrd=%d\n", trrd);
  458. debug("cs0_end=%d\n", cs0_end);
  459. debug("ncs=%d\n", sysinfo->ncs);
  460. /*
  461. * board-specific configuration:
  462. * These values are determined empirically and vary per board layout
  463. */
  464. mmdc0->mpwldectrl0 = calib->p0_mpwldectrl0;
  465. mmdc0->mpwldectrl1 = calib->p0_mpwldectrl1;
  466. mmdc0->mpdgctrl0 = calib->p0_mpdgctrl0;
  467. mmdc0->mpdgctrl1 = calib->p0_mpdgctrl1;
  468. mmdc0->mprddlctl = calib->p0_mprddlctl;
  469. mmdc0->mpwrdlctl = calib->p0_mpwrdlctl;
  470. mmdc0->mpzqlp2ctl = calib->mpzqlp2ctl;
  471. /* Read data DQ Byte0-3 delay */
  472. mmdc0->mprddqby0dl = 0x33333333;
  473. mmdc0->mprddqby1dl = 0x33333333;
  474. if (sysinfo->dsize > 0) {
  475. mmdc0->mprddqby2dl = 0x33333333;
  476. mmdc0->mprddqby3dl = 0x33333333;
  477. }
  478. /* Write data DQ Byte0-3 delay */
  479. mmdc0->mpwrdqby0dl = 0xf3333333;
  480. mmdc0->mpwrdqby1dl = 0xf3333333;
  481. if (sysinfo->dsize > 0) {
  482. mmdc0->mpwrdqby2dl = 0xf3333333;
  483. mmdc0->mpwrdqby3dl = 0xf3333333;
  484. }
  485. /*
  486. * In LPDDR2 mode this register should be cleared,
  487. * so no termination will be activated.
  488. */
  489. mmdc0->mpodtctrl = 0;
  490. /* complete calibration */
  491. val = (1 << 11); /* Force measurement on delay-lines */
  492. mmdc0->mpmur0 = val;
  493. /* Step 1: configuration request */
  494. mmdc0->mdscr = (u32)(1 << 15); /* config request */
  495. /* Step 2: Timing configuration */
  496. mmdc0->mdcfg0 = (trfc << 24) | (txsr << 16) | (txp << 13) |
  497. (tfaw << 4) | tcl;
  498. mmdc0->mdcfg1 = (tras << 16) | (twr << 9) | (tmrd << 5) | twl;
  499. mmdc0->mdcfg2 = (trtp << 6) | (twtr << 3) | trrd;
  500. mmdc0->mdcfg3lp = (trc_lp << 16) | (trcd_lp << 8) |
  501. (trppb_lp << 4) | trpab_lp;
  502. mmdc0->mdotc = 0;
  503. mmdc0->mdasp = cs0_end; /* CS addressing */
  504. /* Step 3: Configure DDR type */
  505. mmdc0->mdmisc = (sysinfo->cs1_mirror << 19) | (sysinfo->walat << 16) |
  506. (sysinfo->bi_on << 12) | (sysinfo->mif3_mode << 9) |
  507. (sysinfo->ralat << 6) | (1 << 3);
  508. /* Step 4: Configure delay while leaving reset */
  509. mmdc0->mdor = (sysinfo->sde_to_rst << 8) |
  510. (sysinfo->rst_to_cke << 0);
  511. /* Step 5: Configure DDR physical parameters (density and burst len) */
  512. coladdr = lpddr2_cfg->coladdr;
  513. if (lpddr2_cfg->coladdr == 8) /* 8-bit COL is 0x3 */
  514. coladdr += 4;
  515. else if (lpddr2_cfg->coladdr == 12) /* 12-bit COL is 0x4 */
  516. coladdr += 1;
  517. mmdc0->mdctl = (lpddr2_cfg->rowaddr - 11) << 24 | /* ROW */
  518. (coladdr - 9) << 20 | /* COL */
  519. (0 << 19) | /* Burst Length = 4 for LPDDR2 */
  520. (sysinfo->dsize << 16); /* DDR data bus size */
  521. /* Step 6: Perform ZQ calibration */
  522. val = 0xa1390003; /* one-time HW ZQ calib */
  523. mmdc0->mpzqhwctrl = val;
  524. /* Step 7: Enable MMDC with desired chip select */
  525. mmdc0->mdctl |= (1 << 31) | /* SDE_0 for CS0 */
  526. ((sysinfo->ncs == 2) ? 1 : 0) << 30; /* SDE_1 for CS1 */
  527. /* Step 8: Write Mode Registers to Init LPDDR2 devices */
  528. for (cs = 0; cs < sysinfo->ncs; cs++) {
  529. /* MR63: reset */
  530. mmdc0->mdscr = MR(63, 0, 3, cs);
  531. /* MR10: calibration,
  532. * 0xff is calibration command after intilization.
  533. */
  534. val = 0xA | (0xff << 8);
  535. mmdc0->mdscr = MR(val, 0, 3, cs);
  536. /* MR1 */
  537. val = 0x1 | (0x82 << 8);
  538. mmdc0->mdscr = MR(val, 0, 3, cs);
  539. /* MR2 */
  540. val = 0x2 | (0x04 << 8);
  541. mmdc0->mdscr = MR(val, 0, 3, cs);
  542. /* MR3 */
  543. val = 0x3 | (0x02 << 8);
  544. mmdc0->mdscr = MR(val, 0, 3, cs);
  545. }
  546. /* Step 10: Power down control and self-refresh */
  547. mmdc0->mdpdc = (tcke & 0x7) << 16 |
  548. 5 << 12 | /* PWDT_1: 256 cycles */
  549. 5 << 8 | /* PWDT_0: 256 cycles */
  550. 1 << 6 | /* BOTH_CS_PD */
  551. (tcksrx & 0x7) << 3 |
  552. (tcksre & 0x7);
  553. mmdc0->mapsr = 0x00001006; /* ADOPT power down enabled */
  554. /* Step 11: Configure ZQ calibration: one-time and periodic 1ms */
  555. val = 0xa1310003;
  556. mmdc0->mpzqhwctrl = val;
  557. /* Step 12: Configure and activate periodic refresh */
  558. mmdc0->mdref = (0 << 14) | /* REF_SEL: Periodic refresh cycle: 64kHz */
  559. (3 << 11); /* REFR: Refresh Rate - 4 refreshes */
  560. /* Step 13: Deassert config request - init complete */
  561. mmdc0->mdscr = 0x00000000;
  562. /* wait for auto-ZQ calibration to complete */
  563. mdelay(1);
  564. }
  565. void mx6_ddr3_cfg(const struct mx6_ddr_sysinfo *sysinfo,
  566. const struct mx6_mmdc_calibration *calib,
  567. const struct mx6_ddr3_cfg *ddr3_cfg)
  568. {
  569. volatile struct mmdc_p_regs *mmdc0;
  570. volatile struct mmdc_p_regs *mmdc1;
  571. u32 val;
  572. u8 tcke, tcksrx, tcksre, txpdll, taofpd, taonpd, trrd;
  573. u8 todtlon, taxpd, tanpd, tcwl, txp, tfaw, tcl;
  574. u8 todt_idle_off = 0x4; /* from DDR3 Script Aid spreadsheet */
  575. u16 trcd, trc, tras, twr, tmrd, trtp, trp, twtr, trfc, txs, txpr;
  576. u16 cs0_end;
  577. u16 tdllk = 0x1ff; /* DLL locking time: 512 cycles (JEDEC DDR3) */
  578. u8 coladdr;
  579. int clkper; /* clock period in picoseconds */
  580. int clock; /* clock freq in MHz */
  581. int cs;
  582. u16 mem_speed = ddr3_cfg->mem_speed;
  583. mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR;
  584. if (!is_cpu_type(MXC_CPU_MX6SX) && !is_cpu_type(MXC_CPU_MX6UL) &&
  585. !is_cpu_type(MXC_CPU_MX6SL))
  586. mmdc1 = (struct mmdc_p_regs *)MMDC_P1_BASE_ADDR;
  587. /* Limit mem_speed for MX6D/MX6Q */
  588. if (is_cpu_type(MXC_CPU_MX6Q) || is_cpu_type(MXC_CPU_MX6D)) {
  589. if (mem_speed > 1066)
  590. mem_speed = 1066; /* 1066 MT/s */
  591. tcwl = 4;
  592. }
  593. /* Limit mem_speed for MX6S/MX6DL */
  594. else {
  595. if (mem_speed > 800)
  596. mem_speed = 800; /* 800 MT/s */
  597. tcwl = 3;
  598. }
  599. clock = mem_speed / 2;
  600. /*
  601. * Data rate of 1066 MT/s requires 533 MHz DDR3 clock, but MX6D/Q supports
  602. * up to 528 MHz, so reduce the clock to fit chip specs
  603. */
  604. if (is_cpu_type(MXC_CPU_MX6Q) || is_cpu_type(MXC_CPU_MX6D)) {
  605. if (clock > 528)
  606. clock = 528; /* 528 MHz */
  607. }
  608. clkper = (1000 * 1000) / clock; /* pico seconds */
  609. todtlon = tcwl;
  610. taxpd = tcwl;
  611. tanpd = tcwl;
  612. switch (ddr3_cfg->density) {
  613. case 1: /* 1Gb per chip */
  614. trfc = DIV_ROUND_UP(110000, clkper) - 1;
  615. txs = DIV_ROUND_UP(120000, clkper) - 1;
  616. break;
  617. case 2: /* 2Gb per chip */
  618. trfc = DIV_ROUND_UP(160000, clkper) - 1;
  619. txs = DIV_ROUND_UP(170000, clkper) - 1;
  620. break;
  621. case 4: /* 4Gb per chip */
  622. trfc = DIV_ROUND_UP(300000, clkper) - 1;
  623. txs = DIV_ROUND_UP(310000, clkper) - 1;
  624. break;
  625. case 8: /* 8Gb per chip */
  626. trfc = DIV_ROUND_UP(350000, clkper) - 1;
  627. txs = DIV_ROUND_UP(360000, clkper) - 1;
  628. break;
  629. default:
  630. /* invalid density */
  631. puts("invalid chip density\n");
  632. hang();
  633. break;
  634. }
  635. txpr = txs;
  636. switch (mem_speed) {
  637. case 800:
  638. txp = DIV_ROUND_UP(max(3 * clkper, 7500), clkper) - 1;
  639. tcke = DIV_ROUND_UP(max(3 * clkper, 7500), clkper) - 1;
  640. if (ddr3_cfg->pagesz == 1) {
  641. tfaw = DIV_ROUND_UP(40000, clkper) - 1;
  642. trrd = DIV_ROUND_UP(max(4 * clkper, 10000), clkper) - 1;
  643. } else {
  644. tfaw = DIV_ROUND_UP(50000, clkper) - 1;
  645. trrd = DIV_ROUND_UP(max(4 * clkper, 10000), clkper) - 1;
  646. }
  647. break;
  648. case 1066:
  649. txp = DIV_ROUND_UP(max(3 * clkper, 7500), clkper) - 1;
  650. tcke = DIV_ROUND_UP(max(3 * clkper, 5625), clkper) - 1;
  651. if (ddr3_cfg->pagesz == 1) {
  652. tfaw = DIV_ROUND_UP(37500, clkper) - 1;
  653. trrd = DIV_ROUND_UP(max(4 * clkper, 7500), clkper) - 1;
  654. } else {
  655. tfaw = DIV_ROUND_UP(50000, clkper) - 1;
  656. trrd = DIV_ROUND_UP(max(4 * clkper, 10000), clkper) - 1;
  657. }
  658. break;
  659. default:
  660. puts("invalid memory speed\n");
  661. hang();
  662. break;
  663. }
  664. txpdll = DIV_ROUND_UP(max(10 * clkper, 24000), clkper) - 1;
  665. tcksre = DIV_ROUND_UP(max(5 * clkper, 10000), clkper);
  666. taonpd = DIV_ROUND_UP(2000, clkper) - 1;
  667. tcksrx = tcksre;
  668. taofpd = taonpd;
  669. twr = DIV_ROUND_UP(15000, clkper) - 1;
  670. tmrd = DIV_ROUND_UP(max(12 * clkper, 15000), clkper) - 1;
  671. trc = DIV_ROUND_UP(ddr3_cfg->trcmin, clkper / 10) - 1;
  672. tras = DIV_ROUND_UP(ddr3_cfg->trasmin, clkper / 10) - 1;
  673. tcl = DIV_ROUND_UP(ddr3_cfg->trcd, clkper / 10) - 3;
  674. trp = DIV_ROUND_UP(ddr3_cfg->trcd, clkper / 10) - 1;
  675. twtr = ROUND(max(4 * clkper, 7500) / clkper, 1) - 1;
  676. trcd = trp;
  677. trtp = twtr;
  678. cs0_end = 4 * sysinfo->cs_density - 1;
  679. debug("density:%d Gb (%d Gb per chip)\n",
  680. sysinfo->cs_density, ddr3_cfg->density);
  681. debug("clock: %dMHz (%d ps)\n", clock, clkper);
  682. debug("memspd:%d\n", mem_speed);
  683. debug("tcke=%d\n", tcke);
  684. debug("tcksrx=%d\n", tcksrx);
  685. debug("tcksre=%d\n", tcksre);
  686. debug("taofpd=%d\n", taofpd);
  687. debug("taonpd=%d\n", taonpd);
  688. debug("todtlon=%d\n", todtlon);
  689. debug("tanpd=%d\n", tanpd);
  690. debug("taxpd=%d\n", taxpd);
  691. debug("trfc=%d\n", trfc);
  692. debug("txs=%d\n", txs);
  693. debug("txp=%d\n", txp);
  694. debug("txpdll=%d\n", txpdll);
  695. debug("tfaw=%d\n", tfaw);
  696. debug("tcl=%d\n", tcl);
  697. debug("trcd=%d\n", trcd);
  698. debug("trp=%d\n", trp);
  699. debug("trc=%d\n", trc);
  700. debug("tras=%d\n", tras);
  701. debug("twr=%d\n", twr);
  702. debug("tmrd=%d\n", tmrd);
  703. debug("tcwl=%d\n", tcwl);
  704. debug("tdllk=%d\n", tdllk);
  705. debug("trtp=%d\n", trtp);
  706. debug("twtr=%d\n", twtr);
  707. debug("trrd=%d\n", trrd);
  708. debug("txpr=%d\n", txpr);
  709. debug("cs0_end=%d\n", cs0_end);
  710. debug("ncs=%d\n", sysinfo->ncs);
  711. debug("Rtt_wr=%d\n", sysinfo->rtt_wr);
  712. debug("Rtt_nom=%d\n", sysinfo->rtt_nom);
  713. debug("SRT=%d\n", ddr3_cfg->SRT);
  714. debug("tcl=%d\n", tcl);
  715. debug("twr=%d\n", twr);
  716. /*
  717. * board-specific configuration:
  718. * These values are determined empirically and vary per board layout
  719. * see:
  720. * appnote, ddr3 spreadsheet
  721. */
  722. mmdc0->mpwldectrl0 = calib->p0_mpwldectrl0;
  723. mmdc0->mpwldectrl1 = calib->p0_mpwldectrl1;
  724. mmdc0->mpdgctrl0 = calib->p0_mpdgctrl0;
  725. mmdc0->mpdgctrl1 = calib->p0_mpdgctrl1;
  726. mmdc0->mprddlctl = calib->p0_mprddlctl;
  727. mmdc0->mpwrdlctl = calib->p0_mpwrdlctl;
  728. if (sysinfo->dsize > 1) {
  729. MMDC1(mpwldectrl0, calib->p1_mpwldectrl0);
  730. MMDC1(mpwldectrl1, calib->p1_mpwldectrl1);
  731. MMDC1(mpdgctrl0, calib->p1_mpdgctrl0);
  732. MMDC1(mpdgctrl1, calib->p1_mpdgctrl1);
  733. MMDC1(mprddlctl, calib->p1_mprddlctl);
  734. MMDC1(mpwrdlctl, calib->p1_mpwrdlctl);
  735. }
  736. /* Read data DQ Byte0-3 delay */
  737. mmdc0->mprddqby0dl = 0x33333333;
  738. mmdc0->mprddqby1dl = 0x33333333;
  739. if (sysinfo->dsize > 0) {
  740. mmdc0->mprddqby2dl = 0x33333333;
  741. mmdc0->mprddqby3dl = 0x33333333;
  742. }
  743. if (sysinfo->dsize > 1) {
  744. MMDC1(mprddqby0dl, 0x33333333);
  745. MMDC1(mprddqby1dl, 0x33333333);
  746. MMDC1(mprddqby2dl, 0x33333333);
  747. MMDC1(mprddqby3dl, 0x33333333);
  748. }
  749. /* MMDC Termination: rtt_nom:2 RZQ/2(120ohm), rtt_nom:1 RZQ/4(60ohm) */
  750. val = (sysinfo->rtt_nom == 2) ? 0x00011117 : 0x00022227;
  751. mmdc0->mpodtctrl = val;
  752. if (sysinfo->dsize > 1)
  753. MMDC1(mpodtctrl, val);
  754. /* complete calibration */
  755. val = (1 << 11); /* Force measurement on delay-lines */
  756. mmdc0->mpmur0 = val;
  757. if (sysinfo->dsize > 1)
  758. MMDC1(mpmur0, val);
  759. /* Step 1: configuration request */
  760. mmdc0->mdscr = (u32)(1 << 15); /* config request */
  761. /* Step 2: Timing configuration */
  762. mmdc0->mdcfg0 = (trfc << 24) | (txs << 16) | (txp << 13) |
  763. (txpdll << 9) | (tfaw << 4) | tcl;
  764. mmdc0->mdcfg1 = (trcd << 29) | (trp << 26) | (trc << 21) |
  765. (tras << 16) | (1 << 15) /* trpa */ |
  766. (twr << 9) | (tmrd << 5) | tcwl;
  767. mmdc0->mdcfg2 = (tdllk << 16) | (trtp << 6) | (twtr << 3) | trrd;
  768. mmdc0->mdotc = (taofpd << 27) | (taonpd << 24) | (tanpd << 20) |
  769. (taxpd << 16) | (todtlon << 12) | (todt_idle_off << 4);
  770. mmdc0->mdasp = cs0_end; /* CS addressing */
  771. /* Step 3: Configure DDR type */
  772. mmdc0->mdmisc = (sysinfo->cs1_mirror << 19) | (sysinfo->walat << 16) |
  773. (sysinfo->bi_on << 12) | (sysinfo->mif3_mode << 9) |
  774. (sysinfo->ralat << 6);
  775. /* Step 4: Configure delay while leaving reset */
  776. mmdc0->mdor = (txpr << 16) | (sysinfo->sde_to_rst << 8) |
  777. (sysinfo->rst_to_cke << 0);
  778. /* Step 5: Configure DDR physical parameters (density and burst len) */
  779. coladdr = ddr3_cfg->coladdr;
  780. if (ddr3_cfg->coladdr == 8) /* 8-bit COL is 0x3 */
  781. coladdr += 4;
  782. else if (ddr3_cfg->coladdr == 12) /* 12-bit COL is 0x4 */
  783. coladdr += 1;
  784. mmdc0->mdctl = (ddr3_cfg->rowaddr - 11) << 24 | /* ROW */
  785. (coladdr - 9) << 20 | /* COL */
  786. (1 << 19) | /* Burst Length = 8 for DDR3 */
  787. (sysinfo->dsize << 16); /* DDR data bus size */
  788. /* Step 6: Perform ZQ calibration */
  789. val = 0xa1390001; /* one-time HW ZQ calib */
  790. mmdc0->mpzqhwctrl = val;
  791. if (sysinfo->dsize > 1)
  792. MMDC1(mpzqhwctrl, val);
  793. /* Step 7: Enable MMDC with desired chip select */
  794. mmdc0->mdctl |= (1 << 31) | /* SDE_0 for CS0 */
  795. ((sysinfo->ncs == 2) ? 1 : 0) << 30; /* SDE_1 for CS1 */
  796. /* Step 8: Write Mode Registers to Init DDR3 devices */
  797. for (cs = 0; cs < sysinfo->ncs; cs++) {
  798. /* MR2 */
  799. val = (sysinfo->rtt_wr & 3) << 9 | (ddr3_cfg->SRT & 1) << 7 |
  800. ((tcwl - 3) & 3) << 3;
  801. debug("MR2 CS%d: 0x%08x\n", cs, (u32)MR(val, 2, 3, cs));
  802. mmdc0->mdscr = MR(val, 2, 3, cs);
  803. /* MR3 */
  804. debug("MR3 CS%d: 0x%08x\n", cs, (u32)MR(0, 3, 3, cs));
  805. mmdc0->mdscr = MR(0, 3, 3, cs);
  806. /* MR1 */
  807. val = ((sysinfo->rtt_nom & 1) ? 1 : 0) << 2 |
  808. ((sysinfo->rtt_nom & 2) ? 1 : 0) << 6;
  809. debug("MR1 CS%d: 0x%08x\n", cs, (u32)MR(val, 1, 3, cs));
  810. mmdc0->mdscr = MR(val, 1, 3, cs);
  811. /* MR0 */
  812. val = ((tcl - 1) << 4) | /* CAS */
  813. (1 << 8) | /* DLL Reset */
  814. ((twr - 3) << 9) | /* Write Recovery */
  815. (sysinfo->pd_fast_exit << 12); /* Precharge PD PLL on */
  816. debug("MR0 CS%d: 0x%08x\n", cs, (u32)MR(val, 0, 3, cs));
  817. mmdc0->mdscr = MR(val, 0, 3, cs);
  818. /* ZQ calibration */
  819. val = (1 << 10);
  820. mmdc0->mdscr = MR(val, 0, 4, cs);
  821. }
  822. /* Step 10: Power down control and self-refresh */
  823. mmdc0->mdpdc = (tcke & 0x7) << 16 |
  824. 5 << 12 | /* PWDT_1: 256 cycles */
  825. 5 << 8 | /* PWDT_0: 256 cycles */
  826. 1 << 6 | /* BOTH_CS_PD */
  827. (tcksrx & 0x7) << 3 |
  828. (tcksre & 0x7);
  829. if (!sysinfo->pd_fast_exit)
  830. mmdc0->mdpdc |= (1 << 7); /* SLOW_PD */
  831. mmdc0->mapsr = 0x00001006; /* ADOPT power down enabled */
  832. /* Step 11: Configure ZQ calibration: one-time and periodic 1ms */
  833. val = 0xa1390003;
  834. mmdc0->mpzqhwctrl = val;
  835. if (sysinfo->dsize > 1)
  836. MMDC1(mpzqhwctrl, val);
  837. /* Step 12: Configure and activate periodic refresh */
  838. mmdc0->mdref = (1 << 14) | /* REF_SEL: Periodic refresh cycle: 32kHz */
  839. (7 << 11); /* REFR: Refresh Rate - 8 refreshes */
  840. /* Step 13: Deassert config request - init complete */
  841. mmdc0->mdscr = 0x00000000;
  842. /* wait for auto-ZQ calibration to complete */
  843. mdelay(1);
  844. }
  845. void mx6_dram_cfg(const struct mx6_ddr_sysinfo *sysinfo,
  846. const struct mx6_mmdc_calibration *calib,
  847. const void *ddr_cfg)
  848. {
  849. if (sysinfo->ddr_type == DDR_TYPE_DDR3) {
  850. mx6_ddr3_cfg(sysinfo, calib, ddr_cfg);
  851. } else if (sysinfo->ddr_type == DDR_TYPE_LPDDR2) {
  852. mx6_lpddr2_cfg(sysinfo, calib, ddr_cfg);
  853. } else {
  854. puts("Unsupported ddr type\n");
  855. hang();
  856. }
  857. }