dram_sun8i_a23.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Sun8i platform dram controller init.
  4. *
  5. * (C) Copyright 2014 Hans de Goede <hdegoede@redhat.com>
  6. */
  7. /*
  8. * Note this code uses a lot of magic hex values, that is because this code
  9. * simply replays the init sequence as done by the Allwinner boot0 code, so
  10. * we do not know what these values mean. There are no symbolic constants for
  11. * these magic values, since we do not know how to name them and making up
  12. * names for them is not useful.
  13. *
  14. * The register-layout of the sunxi_mctl_phy_reg-s looks a lot like the one
  15. * found in the TI Keystone2 documentation:
  16. * http://www.ti.com/lit/ug/spruhn7a/spruhn7a.pdf
  17. * "Table4-2 DDR3 PHY Registers"
  18. * This may be used as a (possible) reference for future work / cleanups.
  19. */
  20. #include <common.h>
  21. #include <errno.h>
  22. #include <asm/io.h>
  23. #include <asm/arch/clock.h>
  24. #include <asm/arch/dram.h>
  25. #include <asm/arch/prcm.h>
  26. static const struct dram_para dram_para = {
  27. .clock = CONFIG_DRAM_CLK,
  28. .type = 3,
  29. .zq = CONFIG_DRAM_ZQ,
  30. .odt_en = IS_ENABLED(CONFIG_DRAM_ODT_EN),
  31. .odt_correction = CONFIG_DRAM_ODT_CORRECTION,
  32. .para1 = 0, /* not used (only used when tpr13 bit 31 is set */
  33. .para2 = 0, /* not used (only used when tpr13 bit 31 is set */
  34. .mr0 = 6736,
  35. .mr1 = 4,
  36. .mr2 = 16,
  37. .mr3 = 0,
  38. /* tpr0 - 10 contain timing constants or-ed together in u32 vals */
  39. .tpr0 = 0x2ab83def,
  40. .tpr1 = 0x18082356,
  41. .tpr2 = 0x00034156,
  42. .tpr3 = 0x448c5533,
  43. .tpr4 = 0x08010d00,
  44. .tpr5 = 0x0340b20f,
  45. .tpr6 = 0x20d118cc,
  46. .tpr7 = 0x14062485,
  47. .tpr8 = 0x220d1d52,
  48. .tpr9 = 0x1e078c22,
  49. .tpr10 = 0x3c,
  50. .tpr11 = 0, /* not used */
  51. .tpr12 = 0, /* not used */
  52. .tpr13 = 0x30000,
  53. };
  54. static void mctl_sys_init(void)
  55. {
  56. struct sunxi_ccm_reg * const ccm =
  57. (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  58. /* enable pll5, note the divide by 2 is deliberate! */
  59. clock_set_pll5(dram_para.clock * 1000000 / 2,
  60. dram_para.tpr13 & 0x40000);
  61. /* deassert ahb mctl reset */
  62. setbits_le32(&ccm->ahb_reset0_cfg, 1 << AHB_RESET_OFFSET_MCTL);
  63. /* enable ahb mctl clock */
  64. setbits_le32(&ccm->ahb_gate0, 1 << AHB_GATE_OFFSET_MCTL);
  65. }
  66. static void mctl_apply_odt_correction(u32 *reg, int correction)
  67. {
  68. int val;
  69. val = (readl(reg) >> 8) & 0xff;
  70. val += correction;
  71. /* clamp */
  72. if (val < 0)
  73. val = 0;
  74. else if (val > 255)
  75. val = 255;
  76. clrsetbits_le32(reg, 0xff00, val << 8);
  77. }
  78. static void mctl_init(u32 *bus_width)
  79. {
  80. struct sunxi_ccm_reg * const ccm =
  81. (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  82. struct sunxi_mctl_com_reg * const mctl_com =
  83. (struct sunxi_mctl_com_reg *)SUNXI_DRAM_COM_BASE;
  84. struct sunxi_mctl_ctl_reg * const mctl_ctl =
  85. (struct sunxi_mctl_ctl_reg *)SUNXI_DRAM_CTL0_BASE;
  86. struct sunxi_mctl_phy_reg * const mctl_phy =
  87. (struct sunxi_mctl_phy_reg *)SUNXI_DRAM_PHY0_BASE;
  88. if (dram_para.tpr13 & 0x20)
  89. writel(0x40b, &mctl_phy->dcr);
  90. else
  91. writel(0x1000040b, &mctl_phy->dcr);
  92. if (dram_para.clock >= 480)
  93. writel(0x5c000, &mctl_phy->dllgcr);
  94. else
  95. writel(0xdc000, &mctl_phy->dllgcr);
  96. writel(0x0a003e3f, &mctl_phy->pgcr0);
  97. writel(0x03008421, &mctl_phy->pgcr1);
  98. writel(dram_para.mr0, &mctl_phy->mr0);
  99. writel(dram_para.mr1, &mctl_phy->mr1);
  100. writel(dram_para.mr2, &mctl_phy->mr2);
  101. writel(dram_para.mr3, &mctl_phy->mr3);
  102. if (!(dram_para.tpr13 & 0x10000)) {
  103. clrsetbits_le32(&mctl_phy->dx0gcr, 0x3800, 0x2000);
  104. clrsetbits_le32(&mctl_phy->dx1gcr, 0x3800, 0x2000);
  105. }
  106. /*
  107. * All the masking and shifting below converts what I assume are DDR
  108. * timing constants from Allwinner dram_para tpr format to the actual
  109. * timing registers format.
  110. */
  111. writel((dram_para.tpr0 & 0x000fffff), &mctl_phy->ptr2);
  112. writel((dram_para.tpr1 & 0x1fffffff), &mctl_phy->ptr3);
  113. writel((dram_para.tpr0 & 0x3ff00000) >> 2 |
  114. (dram_para.tpr2 & 0x0003ffff), &mctl_phy->ptr4);
  115. writel(dram_para.tpr3, &mctl_phy->dtpr0);
  116. writel(dram_para.tpr4, &mctl_phy->dtpr2);
  117. writel(0x01000081, &mctl_phy->dtcr);
  118. if (dram_para.clock <= 240 || !dram_para.odt_en) {
  119. clrbits_le32(&mctl_phy->dx0gcr, 0x600);
  120. clrbits_le32(&mctl_phy->dx1gcr, 0x600);
  121. }
  122. if (dram_para.clock <= 240) {
  123. writel(0, &mctl_phy->odtcr);
  124. writel(0, &mctl_ctl->odtmap);
  125. }
  126. writel(((dram_para.tpr5 & 0x0f00) << 12) |
  127. ((dram_para.tpr5 & 0x00f8) << 9) |
  128. ((dram_para.tpr5 & 0x0007) << 8),
  129. &mctl_ctl->rfshctl0);
  130. writel(((dram_para.tpr5 & 0x0003f000) << 12) |
  131. ((dram_para.tpr5 & 0x00fc0000) >> 2) |
  132. ((dram_para.tpr5 & 0x3f000000) >> 16) |
  133. ((dram_para.tpr6 & 0x0000003f) >> 0),
  134. &mctl_ctl->dramtmg0);
  135. writel(((dram_para.tpr6 & 0x000007c0) << 10) |
  136. ((dram_para.tpr6 & 0x0000f800) >> 3) |
  137. ((dram_para.tpr6 & 0x003f0000) >> 16),
  138. &mctl_ctl->dramtmg1);
  139. writel(((dram_para.tpr6 & 0x0fc00000) << 2) |
  140. ((dram_para.tpr7 & 0x0000001f) << 16) |
  141. ((dram_para.tpr7 & 0x000003e0) << 3) |
  142. ((dram_para.tpr7 & 0x0000fc00) >> 10),
  143. &mctl_ctl->dramtmg2);
  144. writel(((dram_para.tpr7 & 0x03ff0000) >> 16) |
  145. ((dram_para.tpr6 & 0xf0000000) >> 16),
  146. &mctl_ctl->dramtmg3);
  147. writel(((dram_para.tpr7 & 0x3c000000) >> 2 ) |
  148. ((dram_para.tpr8 & 0x00000007) << 16) |
  149. ((dram_para.tpr8 & 0x00000038) << 5) |
  150. ((dram_para.tpr8 & 0x000003c0) >> 6),
  151. &mctl_ctl->dramtmg4);
  152. writel(((dram_para.tpr8 & 0x00003c00) << 14) |
  153. ((dram_para.tpr8 & 0x0003c000) << 2) |
  154. ((dram_para.tpr8 & 0x00fc0000) >> 10) |
  155. ((dram_para.tpr8 & 0x0f000000) >> 24),
  156. &mctl_ctl->dramtmg5);
  157. writel(0x00000008, &mctl_ctl->dramtmg8);
  158. writel(((dram_para.tpr8 & 0xf0000000) >> 4) |
  159. ((dram_para.tpr9 & 0x00007c00) << 6) |
  160. ((dram_para.tpr9 & 0x000003e0) << 3) |
  161. ((dram_para.tpr9 & 0x0000001f) >> 0),
  162. &mctl_ctl->pitmg0);
  163. setbits_le32(&mctl_ctl->pitmg1, 0x80000);
  164. writel(((dram_para.tpr9 & 0x003f8000) << 9) | 0x2001,
  165. &mctl_ctl->sched);
  166. writel((dram_para.mr0 << 16) | dram_para.mr1, &mctl_ctl->init3);
  167. writel((dram_para.mr2 << 16) | dram_para.mr3, &mctl_ctl->init4);
  168. writel(0x00000000, &mctl_ctl->pimisc);
  169. writel(0x80000000, &mctl_ctl->upd0);
  170. writel(((dram_para.tpr9 & 0xffc00000) >> 22) |
  171. ((dram_para.tpr10 & 0x00000fff) << 16),
  172. &mctl_ctl->rfshtmg);
  173. if (dram_para.tpr13 & 0x20)
  174. writel(0x01040001, &mctl_ctl->mstr);
  175. else
  176. writel(0x01040401, &mctl_ctl->mstr);
  177. if (!(dram_para.tpr13 & 0x20000)) {
  178. writel(0x00000002, &mctl_ctl->pwrctl);
  179. writel(0x00008001, &mctl_ctl->pwrtmg);
  180. }
  181. writel(0x00000001, &mctl_ctl->rfshctl3);
  182. writel(0x00000001, &mctl_ctl->pimisc);
  183. /* deassert dram_clk_cfg reset */
  184. setbits_le32(&ccm->dram_clk_cfg, CCM_DRAMCLK_CFG_RST);
  185. setbits_le32(&mctl_com->ccr, 0x80000);
  186. /* zq stuff */
  187. writel((dram_para.zq >> 8) & 0xff, &mctl_phy->zqcr1);
  188. writel(0x00000003, &mctl_phy->pir);
  189. udelay(10);
  190. mctl_await_completion(&mctl_phy->pgsr0, 0x09, 0x09);
  191. writel(readl(&mctl_phy->zqsr0) | 0x10000000, &mctl_phy->zqcr2);
  192. writel(dram_para.zq & 0xff, &mctl_phy->zqcr1);
  193. /* A23-v1.0 SDK uses 0xfdf3, A23-v2.0 SDK uses 0x5f3 */
  194. writel(0x000005f3, &mctl_phy->pir);
  195. udelay(10);
  196. mctl_await_completion(&mctl_phy->pgsr0, 0x03, 0x03);
  197. if (readl(&mctl_phy->dx1gsr0) & 0x1000000) {
  198. *bus_width = 8;
  199. writel(0, &mctl_phy->dx1gcr);
  200. writel(dram_para.zq & 0xff, &mctl_phy->zqcr1);
  201. writel(0x5f3, &mctl_phy->pir);
  202. udelay(10000);
  203. setbits_le32(&mctl_ctl->mstr, 0x1000);
  204. } else
  205. *bus_width = 16;
  206. if (dram_para.odt_correction) {
  207. mctl_apply_odt_correction(&mctl_phy->dx0lcdlr1,
  208. dram_para.odt_correction);
  209. mctl_apply_odt_correction(&mctl_phy->dx1lcdlr1,
  210. dram_para.odt_correction);
  211. }
  212. mctl_await_completion(&mctl_ctl->statr, 0x01, 0x01);
  213. writel(0x08003e3f, &mctl_phy->pgcr0);
  214. writel(0x00000000, &mctl_ctl->rfshctl3);
  215. }
  216. unsigned long sunxi_dram_init(void)
  217. {
  218. struct sunxi_mctl_com_reg * const mctl_com =
  219. (struct sunxi_mctl_com_reg *)SUNXI_DRAM_COM_BASE;
  220. const u32 columns = 13;
  221. u32 bus, bus_width, offset, page_size, rows;
  222. mctl_sys_init();
  223. mctl_init(&bus_width);
  224. if (bus_width == 16) {
  225. page_size = 8;
  226. bus = 1;
  227. } else {
  228. page_size = 7;
  229. bus = 0;
  230. }
  231. if (!(dram_para.tpr13 & 0x80000000)) {
  232. /* Detect and set rows */
  233. writel(0x000310f4 | MCTL_CR_PAGE_SIZE(page_size),
  234. &mctl_com->cr);
  235. setbits_le32(&mctl_com->swonr, 0x0003ffff);
  236. for (rows = 11; rows < 16; rows++) {
  237. offset = 1 << (rows + columns + bus);
  238. if (mctl_mem_matches(offset))
  239. break;
  240. }
  241. clrsetbits_le32(&mctl_com->cr, MCTL_CR_ROW_MASK,
  242. MCTL_CR_ROW(rows));
  243. } else {
  244. rows = (dram_para.para1 >> 16) & 0xff;
  245. writel(((dram_para.para2 & 0x000000f0) << 11) |
  246. ((rows - 1) << 4) |
  247. ((dram_para.para1 & 0x0f000000) >> 22) |
  248. 0x31000 | MCTL_CR_PAGE_SIZE(page_size),
  249. &mctl_com->cr);
  250. setbits_le32(&mctl_com->swonr, 0x0003ffff);
  251. }
  252. /* Setup DRAM master priority? If this is left out things still work */
  253. writel(0x00000008, &mctl_com->mcr0_0);
  254. writel(0x0001000d, &mctl_com->mcr1_0);
  255. writel(0x00000004, &mctl_com->mcr0_1);
  256. writel(0x00000080, &mctl_com->mcr1_1);
  257. writel(0x00000004, &mctl_com->mcr0_2);
  258. writel(0x00000019, &mctl_com->mcr1_2);
  259. writel(0x00000004, &mctl_com->mcr0_3);
  260. writel(0x00000080, &mctl_com->mcr1_3);
  261. writel(0x00000004, &mctl_com->mcr0_4);
  262. writel(0x01010040, &mctl_com->mcr1_4);
  263. writel(0x00000004, &mctl_com->mcr0_5);
  264. writel(0x0001002f, &mctl_com->mcr1_5);
  265. writel(0x00000004, &mctl_com->mcr0_6);
  266. writel(0x00010020, &mctl_com->mcr1_6);
  267. writel(0x00000004, &mctl_com->mcr0_7);
  268. writel(0x00010020, &mctl_com->mcr1_7);
  269. writel(0x00000008, &mctl_com->mcr0_8);
  270. writel(0x00000001, &mctl_com->mcr1_8);
  271. writel(0x00000008, &mctl_com->mcr0_9);
  272. writel(0x00000005, &mctl_com->mcr1_9);
  273. writel(0x00000008, &mctl_com->mcr0_10);
  274. writel(0x00000003, &mctl_com->mcr1_10);
  275. writel(0x00000008, &mctl_com->mcr0_11);
  276. writel(0x00000005, &mctl_com->mcr1_11);
  277. writel(0x00000008, &mctl_com->mcr0_12);
  278. writel(0x00000003, &mctl_com->mcr1_12);
  279. writel(0x00000008, &mctl_com->mcr0_13);
  280. writel(0x00000004, &mctl_com->mcr1_13);
  281. writel(0x00000008, &mctl_com->mcr0_14);
  282. writel(0x00000002, &mctl_com->mcr1_14);
  283. writel(0x00000008, &mctl_com->mcr0_15);
  284. writel(0x00000003, &mctl_com->mcr1_15);
  285. writel(0x00010138, &mctl_com->bwcr);
  286. return 1 << (rows + columns + bus);
  287. }