dram_sun4i.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * sunxi DRAM controller initialization
  4. * (C) Copyright 2012 Henrik Nordstrom <henrik@henriknordstrom.net>
  5. * (C) Copyright 2013 Luke Kenneth Casson Leighton <lkcl@lkcl.net>
  6. *
  7. * Based on sun4i Linux kernel sources mach-sunxi/pm/standby/dram*.c
  8. * and earlier U-Boot Allwiner A10 SPL work
  9. *
  10. * (C) Copyright 2007-2012
  11. * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
  12. * Berg Xing <bergxing@allwinnertech.com>
  13. * Tom Cubie <tangliang@allwinnertech.com>
  14. */
  15. /*
  16. * Unfortunately the only documentation we have on the sun7i DRAM
  17. * controller is Allwinner boot0 + boot1 code, and that code uses
  18. * magic numbers & shifts with no explanations. Hence this code is
  19. * rather undocumented and full of magic.
  20. */
  21. #include <common.h>
  22. #include <asm/io.h>
  23. #include <asm/arch/clock.h>
  24. #include <asm/arch/dram.h>
  25. #include <asm/arch/timer.h>
  26. #include <asm/arch/sys_proto.h>
  27. #define CPU_CFG_CHIP_VER(n) ((n) << 6)
  28. #define CPU_CFG_CHIP_VER_MASK CPU_CFG_CHIP_VER(0x3)
  29. #define CPU_CFG_CHIP_REV_A 0x0
  30. #define CPU_CFG_CHIP_REV_C1 0x1
  31. #define CPU_CFG_CHIP_REV_C2 0x2
  32. #define CPU_CFG_CHIP_REV_B 0x3
  33. /*
  34. * Wait up to 1s for mask to be clear in given reg.
  35. */
  36. static inline void await_bits_clear(u32 *reg, u32 mask)
  37. {
  38. mctl_await_completion(reg, mask, 0);
  39. }
  40. /*
  41. * Wait up to 1s for mask to be set in given reg.
  42. */
  43. static inline void await_bits_set(u32 *reg, u32 mask)
  44. {
  45. mctl_await_completion(reg, mask, mask);
  46. }
  47. /*
  48. * This performs the external DRAM reset by driving the RESET pin low and
  49. * then high again. According to the DDR3 spec, the RESET pin needs to be
  50. * kept low for at least 200 us.
  51. */
  52. static void mctl_ddr3_reset(void)
  53. {
  54. struct sunxi_dram_reg *dram =
  55. (struct sunxi_dram_reg *)SUNXI_DRAMC_BASE;
  56. #ifdef CONFIG_MACH_SUN4I
  57. struct sunxi_timer_reg *timer =
  58. (struct sunxi_timer_reg *)SUNXI_TIMER_BASE;
  59. u32 reg_val;
  60. writel(0, &timer->cpu_cfg);
  61. reg_val = readl(&timer->cpu_cfg);
  62. if ((reg_val & CPU_CFG_CHIP_VER_MASK) !=
  63. CPU_CFG_CHIP_VER(CPU_CFG_CHIP_REV_A)) {
  64. setbits_le32(&dram->mcr, DRAM_MCR_RESET);
  65. udelay(200);
  66. clrbits_le32(&dram->mcr, DRAM_MCR_RESET);
  67. } else
  68. #endif
  69. {
  70. clrbits_le32(&dram->mcr, DRAM_MCR_RESET);
  71. udelay(200);
  72. setbits_le32(&dram->mcr, DRAM_MCR_RESET);
  73. }
  74. /* After the RESET pin is de-asserted, the DDR3 spec requires to wait
  75. * for additional 500 us before driving the CKE pin (Clock Enable)
  76. * high. The duration of this delay can be configured in the SDR_IDCR
  77. * (Initialization Delay Configuration Register) and applied
  78. * automatically by the DRAM controller during the DDR3 initialization
  79. * step. But SDR_IDCR has limited range on sun4i/sun5i hardware and
  80. * can't provide sufficient delay at DRAM clock frequencies higher than
  81. * 524 MHz (while Allwinner A13 supports DRAM clock frequency up to
  82. * 533 MHz according to the datasheet). Additionally, there is no
  83. * official documentation for the SDR_IDCR register anywhere, and
  84. * there is always a chance that we are interpreting it wrong.
  85. * Better be safe than sorry, so add an explicit delay here. */
  86. udelay(500);
  87. }
  88. static void mctl_set_drive(void)
  89. {
  90. struct sunxi_dram_reg *dram = (struct sunxi_dram_reg *)SUNXI_DRAMC_BASE;
  91. #ifdef CONFIG_MACH_SUN7I
  92. clrsetbits_le32(&dram->mcr, DRAM_MCR_MODE_NORM(0x3) | (0x3 << 28),
  93. #else
  94. clrsetbits_le32(&dram->mcr, DRAM_MCR_MODE_NORM(0x3),
  95. #endif
  96. DRAM_MCR_MODE_EN(0x3) |
  97. 0xffc);
  98. }
  99. static void mctl_itm_disable(void)
  100. {
  101. struct sunxi_dram_reg *dram = (struct sunxi_dram_reg *)SUNXI_DRAMC_BASE;
  102. clrsetbits_le32(&dram->ccr, DRAM_CCR_INIT, DRAM_CCR_ITM_OFF);
  103. }
  104. static void mctl_itm_enable(void)
  105. {
  106. struct sunxi_dram_reg *dram = (struct sunxi_dram_reg *)SUNXI_DRAMC_BASE;
  107. clrbits_le32(&dram->ccr, DRAM_CCR_ITM_OFF);
  108. }
  109. static void mctl_itm_reset(void)
  110. {
  111. mctl_itm_disable();
  112. udelay(1); /* ITM reset needs a bit of delay */
  113. mctl_itm_enable();
  114. udelay(1);
  115. }
  116. static void mctl_enable_dll0(u32 phase)
  117. {
  118. struct sunxi_dram_reg *dram = (struct sunxi_dram_reg *)SUNXI_DRAMC_BASE;
  119. clrsetbits_le32(&dram->dllcr[0], 0x3f << 6,
  120. ((phase >> 16) & 0x3f) << 6);
  121. clrsetbits_le32(&dram->dllcr[0], DRAM_DLLCR_NRESET, DRAM_DLLCR_DISABLE);
  122. udelay(2);
  123. clrbits_le32(&dram->dllcr[0], DRAM_DLLCR_NRESET | DRAM_DLLCR_DISABLE);
  124. udelay(22);
  125. clrsetbits_le32(&dram->dllcr[0], DRAM_DLLCR_DISABLE, DRAM_DLLCR_NRESET);
  126. udelay(22);
  127. }
  128. /* Get the number of DDR byte lanes */
  129. static u32 mctl_get_number_of_lanes(void)
  130. {
  131. struct sunxi_dram_reg *dram = (struct sunxi_dram_reg *)SUNXI_DRAMC_BASE;
  132. if ((readl(&dram->dcr) & DRAM_DCR_BUS_WIDTH_MASK) ==
  133. DRAM_DCR_BUS_WIDTH(DRAM_DCR_BUS_WIDTH_32BIT))
  134. return 4;
  135. else
  136. return 2;
  137. }
  138. /*
  139. * Note: This differs from pm/standby in that it checks the bus width
  140. */
  141. static void mctl_enable_dllx(u32 phase)
  142. {
  143. struct sunxi_dram_reg *dram = (struct sunxi_dram_reg *)SUNXI_DRAMC_BASE;
  144. u32 i, number_of_lanes;
  145. number_of_lanes = mctl_get_number_of_lanes();
  146. for (i = 1; i <= number_of_lanes; i++) {
  147. clrsetbits_le32(&dram->dllcr[i], 0xf << 14,
  148. (phase & 0xf) << 14);
  149. clrsetbits_le32(&dram->dllcr[i], DRAM_DLLCR_NRESET,
  150. DRAM_DLLCR_DISABLE);
  151. phase >>= 4;
  152. }
  153. udelay(2);
  154. for (i = 1; i <= number_of_lanes; i++)
  155. clrbits_le32(&dram->dllcr[i], DRAM_DLLCR_NRESET |
  156. DRAM_DLLCR_DISABLE);
  157. udelay(22);
  158. for (i = 1; i <= number_of_lanes; i++)
  159. clrsetbits_le32(&dram->dllcr[i], DRAM_DLLCR_DISABLE,
  160. DRAM_DLLCR_NRESET);
  161. udelay(22);
  162. }
  163. static u32 hpcr_value[32] = {
  164. #ifdef CONFIG_MACH_SUN5I
  165. 0, 0, 0, 0,
  166. 0, 0, 0, 0,
  167. 0, 0, 0, 0,
  168. 0, 0, 0, 0,
  169. 0x1031, 0x1031, 0x0735, 0x1035,
  170. 0x1035, 0x0731, 0x1031, 0,
  171. 0x0301, 0x0301, 0x0301, 0x0301,
  172. 0x0301, 0x0301, 0x0301, 0
  173. #endif
  174. #ifdef CONFIG_MACH_SUN4I
  175. 0x0301, 0x0301, 0x0301, 0x0301,
  176. 0x0301, 0x0301, 0, 0,
  177. 0, 0, 0, 0,
  178. 0, 0, 0, 0,
  179. 0x1031, 0x1031, 0x0735, 0x5031,
  180. 0x1035, 0x0731, 0x1031, 0x0735,
  181. 0x1035, 0x1031, 0x0731, 0x1035,
  182. 0x1031, 0x0301, 0x0301, 0x0731
  183. #endif
  184. #ifdef CONFIG_MACH_SUN7I
  185. 0x0301, 0x0301, 0x0301, 0x0301,
  186. 0x0301, 0x0301, 0x0301, 0x0301,
  187. 0, 0, 0, 0,
  188. 0, 0, 0, 0,
  189. 0x1031, 0x1031, 0x0735, 0x1035,
  190. 0x1035, 0x0731, 0x1031, 0x0735,
  191. 0x1035, 0x1031, 0x0731, 0x1035,
  192. 0x0001, 0x1031, 0, 0x1031
  193. /* last row differs from boot0 source table
  194. * 0x1031, 0x0301, 0x0301, 0x0731
  195. * but boot0 code skips #28 and #30, and sets #29 and #31 to the
  196. * value from #28 entry (0x1031)
  197. */
  198. #endif
  199. };
  200. static void mctl_configure_hostport(void)
  201. {
  202. struct sunxi_dram_reg *dram = (struct sunxi_dram_reg *)SUNXI_DRAMC_BASE;
  203. u32 i;
  204. for (i = 0; i < 32; i++)
  205. writel(hpcr_value[i], &dram->hpcr[i]);
  206. }
  207. static void mctl_setup_dram_clock(u32 clk, u32 mbus_clk)
  208. {
  209. u32 reg_val;
  210. struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  211. u32 pll5p_clk, pll6x_clk;
  212. u32 pll5p_div, pll6x_div;
  213. u32 pll5p_rate, pll6x_rate;
  214. /* setup DRAM PLL */
  215. reg_val = readl(&ccm->pll5_cfg);
  216. reg_val &= ~CCM_PLL5_CTRL_M_MASK; /* set M to 0 (x1) */
  217. reg_val &= ~CCM_PLL5_CTRL_K_MASK; /* set K to 0 (x1) */
  218. reg_val &= ~CCM_PLL5_CTRL_N_MASK; /* set N to 0 (x0) */
  219. reg_val &= ~CCM_PLL5_CTRL_P_MASK; /* set P to 0 (x1) */
  220. #ifdef CONFIG_OLD_SUNXI_KERNEL_COMPAT
  221. /* Old kernels are hardcoded to P=1 (divide by 2) */
  222. reg_val |= CCM_PLL5_CTRL_P(1);
  223. #endif
  224. if (clk >= 540 && clk < 552) {
  225. /* dram = 540MHz */
  226. reg_val |= CCM_PLL5_CTRL_M(CCM_PLL5_CTRL_M_X(2));
  227. reg_val |= CCM_PLL5_CTRL_K(CCM_PLL5_CTRL_K_X(3));
  228. reg_val |= CCM_PLL5_CTRL_N(CCM_PLL5_CTRL_N_X(15));
  229. } else if (clk >= 512 && clk < 528) {
  230. /* dram = 512MHz */
  231. reg_val |= CCM_PLL5_CTRL_M(CCM_PLL5_CTRL_M_X(3));
  232. reg_val |= CCM_PLL5_CTRL_K(CCM_PLL5_CTRL_K_X(4));
  233. reg_val |= CCM_PLL5_CTRL_N(CCM_PLL5_CTRL_N_X(16));
  234. } else if (clk >= 496 && clk < 504) {
  235. /* dram = 496MHz */
  236. reg_val |= CCM_PLL5_CTRL_M(CCM_PLL5_CTRL_M_X(3));
  237. reg_val |= CCM_PLL5_CTRL_K(CCM_PLL5_CTRL_K_X(2));
  238. reg_val |= CCM_PLL5_CTRL_N(CCM_PLL5_CTRL_N_X(31));
  239. } else if (clk >= 468 && clk < 480) {
  240. /* dram = 468MHz */
  241. reg_val |= CCM_PLL5_CTRL_M(CCM_PLL5_CTRL_M_X(2));
  242. reg_val |= CCM_PLL5_CTRL_K(CCM_PLL5_CTRL_K_X(3));
  243. reg_val |= CCM_PLL5_CTRL_N(CCM_PLL5_CTRL_N_X(13));
  244. } else if (clk >= 396 && clk < 408) {
  245. /* dram = 396MHz */
  246. reg_val |= CCM_PLL5_CTRL_M(CCM_PLL5_CTRL_M_X(2));
  247. reg_val |= CCM_PLL5_CTRL_K(CCM_PLL5_CTRL_K_X(3));
  248. reg_val |= CCM_PLL5_CTRL_N(CCM_PLL5_CTRL_N_X(11));
  249. } else {
  250. /* any other frequency that is a multiple of 24 */
  251. reg_val |= CCM_PLL5_CTRL_M(CCM_PLL5_CTRL_M_X(2));
  252. reg_val |= CCM_PLL5_CTRL_K(CCM_PLL5_CTRL_K_X(2));
  253. reg_val |= CCM_PLL5_CTRL_N(CCM_PLL5_CTRL_N_X(clk / 24));
  254. }
  255. reg_val &= ~CCM_PLL5_CTRL_VCO_GAIN; /* PLL VCO Gain off */
  256. reg_val |= CCM_PLL5_CTRL_EN; /* PLL On */
  257. writel(reg_val, &ccm->pll5_cfg);
  258. udelay(5500);
  259. setbits_le32(&ccm->pll5_cfg, CCM_PLL5_CTRL_DDR_CLK);
  260. #if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN7I)
  261. /* reset GPS */
  262. clrbits_le32(&ccm->gps_clk_cfg, CCM_GPS_CTRL_RESET | CCM_GPS_CTRL_GATE);
  263. setbits_le32(&ccm->ahb_gate0, CCM_AHB_GATE_GPS);
  264. udelay(1);
  265. clrbits_le32(&ccm->ahb_gate0, CCM_AHB_GATE_GPS);
  266. #endif
  267. /* setup MBUS clock */
  268. if (!mbus_clk)
  269. mbus_clk = 300;
  270. /* PLL5P and PLL6 are the potential clock sources for MBUS */
  271. pll6x_clk = clock_get_pll6() / 1000000;
  272. #ifdef CONFIG_MACH_SUN7I
  273. pll6x_clk *= 2; /* sun7i uses PLL6*2, sun5i uses just PLL6 */
  274. #endif
  275. pll5p_clk = clock_get_pll5p() / 1000000;
  276. pll6x_div = DIV_ROUND_UP(pll6x_clk, mbus_clk);
  277. pll5p_div = DIV_ROUND_UP(pll5p_clk, mbus_clk);
  278. pll6x_rate = pll6x_clk / pll6x_div;
  279. pll5p_rate = pll5p_clk / pll5p_div;
  280. if (pll6x_div <= 16 && pll6x_rate > pll5p_rate) {
  281. /* use PLL6 as the MBUS clock source */
  282. reg_val = CCM_MBUS_CTRL_GATE |
  283. CCM_MBUS_CTRL_CLK_SRC(CCM_MBUS_CTRL_CLK_SRC_PLL6) |
  284. CCM_MBUS_CTRL_N(CCM_MBUS_CTRL_N_X(1)) |
  285. CCM_MBUS_CTRL_M(CCM_MBUS_CTRL_M_X(pll6x_div));
  286. } else if (pll5p_div <= 16) {
  287. /* use PLL5P as the MBUS clock source */
  288. reg_val = CCM_MBUS_CTRL_GATE |
  289. CCM_MBUS_CTRL_CLK_SRC(CCM_MBUS_CTRL_CLK_SRC_PLL5) |
  290. CCM_MBUS_CTRL_N(CCM_MBUS_CTRL_N_X(1)) |
  291. CCM_MBUS_CTRL_M(CCM_MBUS_CTRL_M_X(pll5p_div));
  292. } else {
  293. panic("Bad mbus_clk\n");
  294. }
  295. writel(reg_val, &ccm->mbus_clk_cfg);
  296. /*
  297. * open DRAMC AHB & DLL register clock
  298. * close it first
  299. */
  300. #if defined(CONFIG_MACH_SUN5I) || defined(CONFIG_MACH_SUN7I)
  301. clrbits_le32(&ccm->ahb_gate0, CCM_AHB_GATE_SDRAM | CCM_AHB_GATE_DLL);
  302. #else
  303. clrbits_le32(&ccm->ahb_gate0, CCM_AHB_GATE_SDRAM);
  304. #endif
  305. udelay(22);
  306. /* then open it */
  307. #if defined(CONFIG_MACH_SUN5I) || defined(CONFIG_MACH_SUN7I)
  308. setbits_le32(&ccm->ahb_gate0, CCM_AHB_GATE_SDRAM | CCM_AHB_GATE_DLL);
  309. #else
  310. setbits_le32(&ccm->ahb_gate0, CCM_AHB_GATE_SDRAM);
  311. #endif
  312. udelay(22);
  313. }
  314. /*
  315. * The data from rslrX and rdgrX registers (X=rank) is stored
  316. * in a single 32-bit value using the following format:
  317. * bits [31:26] - DQS gating system latency for byte lane 3
  318. * bits [25:24] - DQS gating phase select for byte lane 3
  319. * bits [23:18] - DQS gating system latency for byte lane 2
  320. * bits [17:16] - DQS gating phase select for byte lane 2
  321. * bits [15:10] - DQS gating system latency for byte lane 1
  322. * bits [ 9:8 ] - DQS gating phase select for byte lane 1
  323. * bits [ 7:2 ] - DQS gating system latency for byte lane 0
  324. * bits [ 1:0 ] - DQS gating phase select for byte lane 0
  325. */
  326. static void mctl_set_dqs_gating_delay(int rank, u32 dqs_gating_delay)
  327. {
  328. struct sunxi_dram_reg *dram = (struct sunxi_dram_reg *)SUNXI_DRAMC_BASE;
  329. u32 lane, number_of_lanes = mctl_get_number_of_lanes();
  330. /* rank0 gating system latency (3 bits per lane: cycles) */
  331. u32 slr = readl(rank == 0 ? &dram->rslr0 : &dram->rslr1);
  332. /* rank0 gating phase select (2 bits per lane: 90, 180, 270, 360) */
  333. u32 dgr = readl(rank == 0 ? &dram->rdgr0 : &dram->rdgr1);
  334. for (lane = 0; lane < number_of_lanes; lane++) {
  335. u32 tmp = dqs_gating_delay >> (lane * 8);
  336. slr &= ~(7 << (lane * 3));
  337. slr |= ((tmp >> 2) & 7) << (lane * 3);
  338. dgr &= ~(3 << (lane * 2));
  339. dgr |= (tmp & 3) << (lane * 2);
  340. }
  341. writel(slr, rank == 0 ? &dram->rslr0 : &dram->rslr1);
  342. writel(dgr, rank == 0 ? &dram->rdgr0 : &dram->rdgr1);
  343. }
  344. static int dramc_scan_readpipe(void)
  345. {
  346. struct sunxi_dram_reg *dram = (struct sunxi_dram_reg *)SUNXI_DRAMC_BASE;
  347. u32 reg_val;
  348. /* data training trigger */
  349. clrbits_le32(&dram->csr, DRAM_CSR_FAILED);
  350. setbits_le32(&dram->ccr, DRAM_CCR_DATA_TRAINING);
  351. /* check whether data training process has completed */
  352. await_bits_clear(&dram->ccr, DRAM_CCR_DATA_TRAINING);
  353. /* check data training result */
  354. reg_val = readl(&dram->csr);
  355. if (reg_val & DRAM_CSR_FAILED)
  356. return -1;
  357. return 0;
  358. }
  359. static void dramc_clock_output_en(u32 on)
  360. {
  361. #if defined(CONFIG_MACH_SUN5I) || defined(CONFIG_MACH_SUN7I)
  362. struct sunxi_dram_reg *dram = (struct sunxi_dram_reg *)SUNXI_DRAMC_BASE;
  363. if (on)
  364. setbits_le32(&dram->mcr, DRAM_MCR_DCLK_OUT);
  365. else
  366. clrbits_le32(&dram->mcr, DRAM_MCR_DCLK_OUT);
  367. #endif
  368. #ifdef CONFIG_MACH_SUN4I
  369. struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  370. if (on)
  371. setbits_le32(&ccm->dram_clk_gate, CCM_DRAM_CTRL_DCLK_OUT);
  372. else
  373. clrbits_le32(&ccm->dram_clk_gate, CCM_DRAM_CTRL_DCLK_OUT);
  374. #endif
  375. }
  376. /* tRFC in nanoseconds for different densities (from the DDR3 spec) */
  377. static const u16 tRFC_DDR3_table[6] = {
  378. /* 256Mb 512Mb 1Gb 2Gb 4Gb 8Gb */
  379. 90, 90, 110, 160, 300, 350
  380. };
  381. static void dramc_set_autorefresh_cycle(u32 clk, u32 density)
  382. {
  383. struct sunxi_dram_reg *dram = (struct sunxi_dram_reg *)SUNXI_DRAMC_BASE;
  384. u32 tRFC, tREFI;
  385. tRFC = (tRFC_DDR3_table[density] * clk + 999) / 1000;
  386. tREFI = (7987 * clk) >> 10; /* <= 7.8us */
  387. writel(DRAM_DRR_TREFI(tREFI) | DRAM_DRR_TRFC(tRFC), &dram->drr);
  388. }
  389. /* Calculate the value for A11, A10, A9 bits in MR0 (write recovery) */
  390. static u32 ddr3_write_recovery(u32 clk)
  391. {
  392. u32 twr_ns = 15; /* DDR3 spec says that it is 15ns for all speed bins */
  393. u32 twr_ck = (twr_ns * clk + 999) / 1000;
  394. if (twr_ck < 5)
  395. return 1;
  396. else if (twr_ck <= 8)
  397. return twr_ck - 4;
  398. else if (twr_ck <= 10)
  399. return 5;
  400. else
  401. return 6;
  402. }
  403. /*
  404. * If the dram->ppwrsctl (SDR_DPCR) register has the lowest bit set to 1, this
  405. * means that DRAM is currently in self-refresh mode and retaining the old
  406. * data. Since we have no idea what to do in this situation yet, just set this
  407. * register to 0 and initialize DRAM in the same way as on any normal reboot
  408. * (discarding whatever was stored there).
  409. *
  410. * Note: on sun7i hardware, the highest 16 bits need to be set to 0x1651 magic
  411. * value for this write operation to have any effect. On sun5i hadware this
  412. * magic value is not necessary. And on sun4i hardware the writes to this
  413. * register seem to have no effect at all.
  414. */
  415. static void mctl_disable_power_save(void)
  416. {
  417. struct sunxi_dram_reg *dram = (struct sunxi_dram_reg *)SUNXI_DRAMC_BASE;
  418. writel(0x16510000, &dram->ppwrsctl);
  419. }
  420. /*
  421. * After the DRAM is powered up or reset, the DDR3 spec requires to wait at
  422. * least 500 us before driving the CKE pin (Clock Enable) high. The dram->idct
  423. * (SDR_IDCR) register appears to configure this delay, which gets applied
  424. * right at the time when the DRAM initialization is activated in the
  425. * 'mctl_ddr3_initialize' function.
  426. */
  427. static void mctl_set_cke_delay(void)
  428. {
  429. struct sunxi_dram_reg *dram = (struct sunxi_dram_reg *)SUNXI_DRAMC_BASE;
  430. /* The CKE delay is represented in DRAM clock cycles, multiplied by N
  431. * (where N=2 for sun4i/sun5i and N=3 for sun7i). Here it is set to
  432. * the maximum possible value 0x1ffff, just like in the Allwinner's
  433. * boot0 bootloader. The resulting delay value is somewhere between
  434. * ~0.4 ms (sun5i with 648 MHz DRAM clock speed) and ~1.1 ms (sun7i
  435. * with 360 MHz DRAM clock speed). */
  436. setbits_le32(&dram->idcr, 0x1ffff);
  437. }
  438. /*
  439. * This triggers the DRAM initialization. It performs sending the mode registers
  440. * to the DRAM among other things. Very likely the ZQCL command is also getting
  441. * executed (to do the initial impedance calibration on the DRAM side of the
  442. * wire). The memory controller and the PHY must be already configured before
  443. * calling this function.
  444. */
  445. static void mctl_ddr3_initialize(void)
  446. {
  447. struct sunxi_dram_reg *dram = (struct sunxi_dram_reg *)SUNXI_DRAMC_BASE;
  448. setbits_le32(&dram->ccr, DRAM_CCR_INIT);
  449. await_bits_clear(&dram->ccr, DRAM_CCR_INIT);
  450. }
  451. /*
  452. * Perform impedance calibration on the DRAM controller side of the wire.
  453. */
  454. static void mctl_set_impedance(u32 zq, bool odt_en)
  455. {
  456. struct sunxi_dram_reg *dram = (struct sunxi_dram_reg *)SUNXI_DRAMC_BASE;
  457. u32 reg_val;
  458. u32 zprog = zq & 0xFF, zdata = (zq >> 8) & 0xFFFFF;
  459. #ifndef CONFIG_MACH_SUN7I
  460. /* Appears that some kind of automatically initiated default
  461. * ZQ calibration is already in progress at this point on sun4i/sun5i
  462. * hardware, but not on sun7i. So it is reasonable to wait for its
  463. * completion before doing anything else. */
  464. await_bits_set(&dram->zqsr, DRAM_ZQSR_ZDONE);
  465. #endif
  466. /* ZQ calibration is not really useful unless ODT is enabled */
  467. if (!odt_en)
  468. return;
  469. #ifdef CONFIG_MACH_SUN7I
  470. /* Enabling ODT in SDR_IOCR on sun7i hardware results in a deadlock
  471. * unless bit 24 is set in SDR_ZQCR1. Not much is known about the
  472. * SDR_ZQCR1 register, but there are hints indicating that it might
  473. * be related to periodic impedance re-calibration. This particular
  474. * magic value is borrowed from the Allwinner boot0 bootloader, and
  475. * using it helps to avoid troubles */
  476. writel((1 << 24) | (1 << 1), &dram->zqcr1);
  477. #endif
  478. /* Needed at least for sun5i, because it does not self clear there */
  479. clrbits_le32(&dram->zqcr0, DRAM_ZQCR0_ZCAL);
  480. if (zdata) {
  481. /* Set the user supplied impedance data */
  482. reg_val = DRAM_ZQCR0_ZDEN | zdata;
  483. writel(reg_val, &dram->zqcr0);
  484. /* no need to wait, this takes effect immediately */
  485. } else {
  486. /* Do the calibration using the external resistor */
  487. reg_val = DRAM_ZQCR0_ZCAL | DRAM_ZQCR0_IMP_DIV(zprog);
  488. writel(reg_val, &dram->zqcr0);
  489. /* Wait for the new impedance configuration to settle */
  490. await_bits_set(&dram->zqsr, DRAM_ZQSR_ZDONE);
  491. }
  492. /* Needed at least for sun5i, because it does not self clear there */
  493. clrbits_le32(&dram->zqcr0, DRAM_ZQCR0_ZCAL);
  494. /* Set I/O configure register */
  495. writel(DRAM_IOCR_ODT_EN, &dram->iocr);
  496. }
  497. static unsigned long dramc_init_helper(struct dram_para *para)
  498. {
  499. struct sunxi_dram_reg *dram = (struct sunxi_dram_reg *)SUNXI_DRAMC_BASE;
  500. u32 reg_val;
  501. u32 density;
  502. int ret_val;
  503. /*
  504. * only single rank DDR3 is supported by this code even though the
  505. * hardware can theoretically support DDR2 and up to two ranks
  506. */
  507. if (para->type != DRAM_MEMORY_TYPE_DDR3 || para->rank_num != 1)
  508. return 0;
  509. /* setup DRAM relative clock */
  510. mctl_setup_dram_clock(para->clock, para->mbus_clock);
  511. /* Disable any pad power save control */
  512. mctl_disable_power_save();
  513. mctl_set_drive();
  514. /* dram clock off */
  515. dramc_clock_output_en(0);
  516. #ifdef CONFIG_MACH_SUN4I
  517. /* select dram controller 1 */
  518. writel(DRAM_CSEL_MAGIC, &dram->csel);
  519. #endif
  520. mctl_itm_disable();
  521. mctl_enable_dll0(para->tpr3);
  522. /* configure external DRAM */
  523. reg_val = DRAM_DCR_TYPE_DDR3;
  524. reg_val |= DRAM_DCR_IO_WIDTH(para->io_width >> 3);
  525. if (para->density == 256)
  526. density = DRAM_DCR_CHIP_DENSITY_256M;
  527. else if (para->density == 512)
  528. density = DRAM_DCR_CHIP_DENSITY_512M;
  529. else if (para->density == 1024)
  530. density = DRAM_DCR_CHIP_DENSITY_1024M;
  531. else if (para->density == 2048)
  532. density = DRAM_DCR_CHIP_DENSITY_2048M;
  533. else if (para->density == 4096)
  534. density = DRAM_DCR_CHIP_DENSITY_4096M;
  535. else if (para->density == 8192)
  536. density = DRAM_DCR_CHIP_DENSITY_8192M;
  537. else
  538. density = DRAM_DCR_CHIP_DENSITY_256M;
  539. reg_val |= DRAM_DCR_CHIP_DENSITY(density);
  540. reg_val |= DRAM_DCR_BUS_WIDTH((para->bus_width >> 3) - 1);
  541. reg_val |= DRAM_DCR_RANK_SEL(para->rank_num - 1);
  542. reg_val |= DRAM_DCR_CMD_RANK_ALL;
  543. reg_val |= DRAM_DCR_MODE(DRAM_DCR_MODE_INTERLEAVE);
  544. writel(reg_val, &dram->dcr);
  545. dramc_clock_output_en(1);
  546. mctl_set_impedance(para->zq, para->odt_en);
  547. mctl_set_cke_delay();
  548. mctl_ddr3_reset();
  549. udelay(1);
  550. await_bits_clear(&dram->ccr, DRAM_CCR_INIT);
  551. mctl_enable_dllx(para->tpr3);
  552. /* set refresh period */
  553. dramc_set_autorefresh_cycle(para->clock, density);
  554. /* set timing parameters */
  555. writel(para->tpr0, &dram->tpr0);
  556. writel(para->tpr1, &dram->tpr1);
  557. writel(para->tpr2, &dram->tpr2);
  558. reg_val = DRAM_MR_BURST_LENGTH(0x0);
  559. #if (defined(CONFIG_MACH_SUN5I) || defined(CONFIG_MACH_SUN7I))
  560. reg_val |= DRAM_MR_POWER_DOWN;
  561. #endif
  562. reg_val |= DRAM_MR_CAS_LAT(para->cas - 4);
  563. reg_val |= DRAM_MR_WRITE_RECOVERY(ddr3_write_recovery(para->clock));
  564. writel(reg_val, &dram->mr);
  565. writel(para->emr1, &dram->emr);
  566. writel(para->emr2, &dram->emr2);
  567. writel(para->emr3, &dram->emr3);
  568. /* disable drift compensation and set passive DQS window mode */
  569. clrsetbits_le32(&dram->ccr, DRAM_CCR_DQS_DRIFT_COMP, DRAM_CCR_DQS_GATE);
  570. #ifdef CONFIG_MACH_SUN7I
  571. /* Command rate timing mode 2T & 1T */
  572. if (para->tpr4 & 0x1)
  573. setbits_le32(&dram->ccr, DRAM_CCR_COMMAND_RATE_1T);
  574. #endif
  575. /* initialize external DRAM */
  576. mctl_ddr3_initialize();
  577. /* scan read pipe value */
  578. mctl_itm_enable();
  579. /* Hardware DQS gate training */
  580. ret_val = dramc_scan_readpipe();
  581. if (ret_val < 0)
  582. return 0;
  583. /* allow to override the DQS training results with a custom delay */
  584. if (para->dqs_gating_delay)
  585. mctl_set_dqs_gating_delay(0, para->dqs_gating_delay);
  586. /* set the DQS gating window type */
  587. if (para->active_windowing)
  588. clrbits_le32(&dram->ccr, DRAM_CCR_DQS_GATE);
  589. else
  590. setbits_le32(&dram->ccr, DRAM_CCR_DQS_GATE);
  591. mctl_itm_reset();
  592. /* configure all host port */
  593. mctl_configure_hostport();
  594. return get_ram_size((long *)PHYS_SDRAM_0, PHYS_SDRAM_0_SIZE);
  595. }
  596. unsigned long dramc_init(struct dram_para *para)
  597. {
  598. unsigned long dram_size, actual_density;
  599. /* If the dram configuration is not provided, use a default */
  600. if (!para)
  601. return 0;
  602. /* if everything is known, then autodetection is not necessary */
  603. if (para->io_width && para->bus_width && para->density)
  604. return dramc_init_helper(para);
  605. /* try to autodetect the DRAM bus width and density */
  606. para->io_width = 16;
  607. para->bus_width = 32;
  608. #if defined(CONFIG_MACH_SUN4I) || defined(CONFIG_MACH_SUN5I)
  609. /* only A0-A14 address lines on A10/A13, limiting max density to 4096 */
  610. para->density = 4096;
  611. #else
  612. /* all A0-A15 address lines on A20, which allow density 8192 */
  613. para->density = 8192;
  614. #endif
  615. dram_size = dramc_init_helper(para);
  616. if (!dram_size) {
  617. /* if 32-bit bus width failed, try 16-bit bus width instead */
  618. para->bus_width = 16;
  619. dram_size = dramc_init_helper(para);
  620. if (!dram_size) {
  621. /* if 16-bit bus width also failed, then bail out */
  622. return dram_size;
  623. }
  624. }
  625. /* check if we need to adjust the density */
  626. actual_density = (dram_size >> 17) * para->io_width / para->bus_width;
  627. if (actual_density != para->density) {
  628. /* update the density and re-initialize DRAM again */
  629. para->density = actual_density;
  630. dram_size = dramc_init_helper(para);
  631. }
  632. return dram_size;
  633. }