h6_ddr3_1333.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * sun50i H6 DDR3-1333 timings, as programmed by Allwinner's boot0
  3. * for some TV boxes with the H6 and DDR3 memory.
  4. *
  5. * The chips are probably able to be driven by a faster clock, but boot0
  6. * uses a more conservative timing (as usual).
  7. *
  8. * (C) Copyright 2018,2019 Arm Ltd.
  9. * based on previous work by:
  10. * (C) Copyright 2017 Icenowy Zheng <icenowy@aosc.io>
  11. *
  12. * References used:
  13. * - JEDEC DDR3 SDRAM standard: JESD79-3F.pdf
  14. * - Samsung K4B2G0446D datasheet
  15. * - ZynqMP UG1087 register DDRC/PHY documentation
  16. *
  17. * Many thanks to Jernej Skrabec for contributing some fixes!
  18. *
  19. * SPDX-License-Identifier: GPL-2.0+
  20. */
  21. #include <common.h>
  22. #include <asm/arch/dram.h>
  23. #include <asm/arch/cpu.h>
  24. /*
  25. * Only the first four are used for DDR3(?)
  26. * MR0: BL8, seq. read burst, no test, fast exit (DLL on), no DLL reset,
  27. * CAS latency (CL): 11, write recovery (WR): 12
  28. * MR1: DLL enabled, output strength RZQ/6, Rtt_norm RZQ/2,
  29. * write levelling disabled, TDQS disabled, output buffer enabled
  30. * MR2: manual full array self refresh, dynamic ODT off,
  31. * CAS write latency (CWL): 8
  32. */
  33. static u32 mr_ddr3[7] = {
  34. 0x00001c70, 0x00000040, 0x00000018, 0x00000000,
  35. 0x00000000, 0x00000400, 0x00000848,
  36. };
  37. /* TODO: flexible timing */
  38. void mctl_set_timing_params(struct dram_para *para)
  39. {
  40. struct sunxi_mctl_ctl_reg * const mctl_ctl =
  41. (struct sunxi_mctl_ctl_reg *)SUNXI_DRAM_CTL0_BASE;
  42. struct sunxi_mctl_phy_reg * const mctl_phy =
  43. (struct sunxi_mctl_phy_reg *)SUNXI_DRAM_PHY0_BASE;
  44. int i;
  45. u8 tccd = 2; /* JEDEC: 4nCK */
  46. u8 tfaw = ns_to_t(50); /* JEDEC: 30 ns w/ 1K pages */
  47. u8 trrd = max(ns_to_t(6), 4); /* JEDEC: max(6 ns, 4nCK) */
  48. u8 trcd = ns_to_t(15); /* JEDEC: 13.5 ns */
  49. u8 trc = ns_to_t(53); /* JEDEC: 49.5 ns */
  50. u8 txp = max(ns_to_t(6), 3); /* JEDEC: max(6 ns, 3nCK) */
  51. u8 twtr = max(ns_to_t(8), 2); /* JEDEC: max(7.5 ns, 4nCK) */
  52. u8 trtp = max(ns_to_t(8), 2); /* JEDEC: max(7.5 ns, 4nCK) */
  53. u8 twr = ns_to_t(15); /* JEDEC: 15 ns */
  54. u8 trp = ns_to_t(15); /* JEDEC: >= 13.75 ns */
  55. u8 tras = ns_to_t(38); /* JEDEC >= 36 ns, <= 9*trefi */
  56. u8 twtr_sa = 2; /* ? */
  57. u8 tcksrea = 4; /* ? */
  58. u16 trefi = ns_to_t(7800) / 32; /* JEDEC: 7.8us@Tcase <= 85C */
  59. u16 trfc = ns_to_t(350); /* JEDEC: 160 ns for 2Gb */
  60. u16 txsr = 4; /* ? */
  61. u8 tmrw = 0; /* ? */
  62. u8 tmrd = 4; /* JEDEC: 4nCK */
  63. u8 tmod = max(ns_to_t(15), 12); /* JEDEC: max(15 ns, 12nCK) */
  64. u8 tcke = max(ns_to_t(6), 3); /* JEDEC: max(5.625 ns, 3nCK) */
  65. u8 tcksrx = max(ns_to_t(10), 5); /* JEDEC: max(10 ns, 5nCK) */
  66. u8 tcksre = max(ns_to_t(10), 5); /* JEDEC: max(10 ns, 5nCK) */
  67. u8 tckesr = tcke + 1; /* JEDEC: tCKE(min) + 1nCK */
  68. u8 trasmax = 24; /* JEDEC: tREFI * 9 */
  69. u8 txs = ns_to_t(360) / 32; /* JEDEC: max(5nCK,tRFC+10ns) */
  70. u8 txsdll = 4; /* JEDEC: 512 nCK */
  71. u8 txsabort = 4; /* ? */
  72. u8 txsfast = 4; /* ? */
  73. u8 tcl = 6; /* JEDEC: CL / 2 => 6 */
  74. u8 tcwl = 4; /* JEDEC: 8 */
  75. u8 t_rdata_en = 7; /* ? */
  76. u32 tdinit0 = (500 * CONFIG_DRAM_CLK) + 1; /* 500us */
  77. u32 tdinit1 = (360 * CONFIG_DRAM_CLK) / 1000 + 1;
  78. u32 tdinit2 = (200 * CONFIG_DRAM_CLK) + 1;
  79. u32 tdinit3 = (1 * CONFIG_DRAM_CLK) + 1; /* 1us */
  80. u8 twtp = tcwl + 2 + twr; /* (WL + BL / 2 + tWR) / 2 */
  81. u8 twr2rd = tcwl + 2 + twtr; /* (WL + BL / 2 + tWTR) / 2 */
  82. u8 trd2wr = 5; /* (RL + BL / 2 + 2 - WL) / 2 */
  83. if (tcl + 1 >= trtp + trp)
  84. trtp = tcl + 2 - trp;
  85. /* set mode registers */
  86. for (i = 0; i < ARRAY_SIZE(mr_ddr3); i++)
  87. writel(mr_ddr3[i], &mctl_phy->mr[i]);
  88. /* set DRAM timing */
  89. writel((twtp << 24) | (tfaw << 16) | (trasmax << 8) | tras,
  90. &mctl_ctl->dramtmg[0]);
  91. writel((txp << 16) | (trtp << 8) | trc, &mctl_ctl->dramtmg[1]);
  92. writel((tcwl << 24) | (tcl << 16) | (trd2wr << 8) | twr2rd,
  93. &mctl_ctl->dramtmg[2]);
  94. writel((tmrw << 20) | (tmrd << 12) | tmod, &mctl_ctl->dramtmg[3]);
  95. writel((trcd << 24) | (tccd << 16) | (trrd << 8) | trp,
  96. &mctl_ctl->dramtmg[4]);
  97. writel((tcksrx << 24) | (tcksre << 16) | (tckesr << 8) | tcke,
  98. &mctl_ctl->dramtmg[5]);
  99. /* Value suggested by ZynqMP manual and used by libdram */
  100. writel((txp + 2) | 0x02020000, &mctl_ctl->dramtmg[6]);
  101. writel((txsfast << 24) | (txsabort << 16) | (txsdll << 8) | txs,
  102. &mctl_ctl->dramtmg[8]);
  103. writel(txsr, &mctl_ctl->dramtmg[14]);
  104. clrsetbits_le32(&mctl_ctl->init[0], (3 << 30), (1 << 30));
  105. writel(0, &mctl_ctl->dfimisc);
  106. clrsetbits_le32(&mctl_ctl->rankctl, 0xff0, 0x660);
  107. /*
  108. * Set timing registers of the PHY.
  109. * Note: the PHY is clocked 2x from the DRAM frequency.
  110. */
  111. writel((trrd << 25) | (tras << 17) | (trp << 9) | (trtp << 1),
  112. &mctl_phy->dtpr[0]);
  113. writel((tfaw << 17) | 0x28000400 | (tmrd << 1), &mctl_phy->dtpr[1]);
  114. writel(((txs << 6) - 1) | (tcke << 17), &mctl_phy->dtpr[2]);
  115. writel(((txsdll << 22) - (0x1 << 16)) | twtr_sa | (tcksrea << 8),
  116. &mctl_phy->dtpr[3]);
  117. writel((txp << 1) | (trfc << 17) | 0x800, &mctl_phy->dtpr[4]);
  118. writel((trc << 17) | (trcd << 9) | (twtr << 1), &mctl_phy->dtpr[5]);
  119. writel(0x0505, &mctl_phy->dtpr[6]);
  120. /* Configure DFI timing */
  121. writel(tcl | 0x2000200 | (t_rdata_en << 16) | 0x808000,
  122. &mctl_ctl->dfitmg0);
  123. writel(0x040201, &mctl_ctl->dfitmg1);
  124. /* Configure PHY timing. Zynq uses different registers. */
  125. writel(tdinit0 | (tdinit1 << 20), &mctl_phy->ptr[3]);
  126. writel(tdinit2 | (tdinit3 << 18), &mctl_phy->ptr[4]);
  127. /* set refresh timing */
  128. writel((trefi << 16) | trfc, &mctl_ctl->rfshtmg);
  129. }