dram_sunxi_dw.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * sun8i H3 platform dram controller init
  4. *
  5. * (C) Copyright 2007-2015 Allwinner Technology Co.
  6. * Jerry Wang <wangflord@allwinnertech.com>
  7. * (C) Copyright 2015 Vishnu Patekar <vishnupatekar0510@gmail.com>
  8. * (C) Copyright 2015 Hans de Goede <hdegoede@redhat.com>
  9. * (C) Copyright 2015 Jens Kuske <jenskuske@gmail.com>
  10. */
  11. #include <common.h>
  12. #include <asm/io.h>
  13. #include <asm/arch/clock.h>
  14. #include <asm/arch/dram.h>
  15. #include <asm/arch/cpu.h>
  16. #include <linux/kconfig.h>
  17. static void mctl_phy_init(u32 val)
  18. {
  19. struct sunxi_mctl_ctl_reg * const mctl_ctl =
  20. (struct sunxi_mctl_ctl_reg *)SUNXI_DRAM_CTL0_BASE;
  21. writel(val | PIR_INIT, &mctl_ctl->pir);
  22. mctl_await_completion(&mctl_ctl->pgsr[0], PGSR_INIT_DONE, 0x1);
  23. }
  24. static void mctl_set_bit_delays(struct dram_para *para)
  25. {
  26. struct sunxi_mctl_ctl_reg * const mctl_ctl =
  27. (struct sunxi_mctl_ctl_reg *)SUNXI_DRAM_CTL0_BASE;
  28. int i, j;
  29. clrbits_le32(&mctl_ctl->pgcr[0], 1 << 26);
  30. for (i = 0; i < NR_OF_BYTE_LANES; i++)
  31. for (j = 0; j < LINES_PER_BYTE_LANE; j++)
  32. writel(DXBDLR_WRITE_DELAY(para->dx_write_delays[i][j]) |
  33. DXBDLR_READ_DELAY(para->dx_read_delays[i][j]),
  34. &mctl_ctl->dx[i].bdlr[j]);
  35. for (i = 0; i < 31; i++)
  36. writel(ACBDLR_WRITE_DELAY(para->ac_delays[i]),
  37. &mctl_ctl->acbdlr[i]);
  38. #ifdef CONFIG_MACH_SUN8I_R40
  39. /* DQSn, DMn, DQn output enable bit delay */
  40. for (i = 0; i < 4; i++)
  41. writel(0x6 << 24, &mctl_ctl->dx[i].sdlr);
  42. #endif
  43. setbits_le32(&mctl_ctl->pgcr[0], 1 << 26);
  44. }
  45. enum {
  46. MBUS_PORT_CPU = 0,
  47. MBUS_PORT_GPU = 1,
  48. MBUS_PORT_UNUSED = 2,
  49. MBUS_PORT_DMA = 3,
  50. MBUS_PORT_VE = 4,
  51. MBUS_PORT_CSI = 5,
  52. MBUS_PORT_NAND = 6,
  53. MBUS_PORT_SS = 7,
  54. MBUS_PORT_TS = 8,
  55. MBUS_PORT_DI = 9,
  56. MBUS_PORT_DE = 10,
  57. MBUS_PORT_DE_CFD = 11,
  58. MBUS_PORT_UNKNOWN1 = 12,
  59. MBUS_PORT_UNKNOWN2 = 13,
  60. MBUS_PORT_UNKNOWN3 = 14,
  61. };
  62. enum {
  63. MBUS_QOS_LOWEST = 0,
  64. MBUS_QOS_LOW,
  65. MBUS_QOS_HIGH,
  66. MBUS_QOS_HIGHEST
  67. };
  68. inline void mbus_configure_port(u8 port,
  69. bool bwlimit,
  70. bool priority,
  71. u8 qos, /* MBUS_QOS_LOWEST .. MBUS_QOS_HIGEST */
  72. u8 waittime, /* 0 .. 0xf */
  73. u8 acs, /* 0 .. 0xff */
  74. u16 bwl0, /* 0 .. 0xffff, bandwidth limit in MB/s */
  75. u16 bwl1,
  76. u16 bwl2)
  77. {
  78. struct sunxi_mctl_com_reg * const mctl_com =
  79. (struct sunxi_mctl_com_reg *)SUNXI_DRAM_COM_BASE;
  80. const u32 cfg0 = ( (bwlimit ? (1 << 0) : 0)
  81. | (priority ? (1 << 1) : 0)
  82. | ((qos & 0x3) << 2)
  83. | ((waittime & 0xf) << 4)
  84. | ((acs & 0xff) << 8)
  85. | (bwl0 << 16) );
  86. const u32 cfg1 = ((u32)bwl2 << 16) | (bwl1 & 0xffff);
  87. debug("MBUS port %d cfg0 %08x cfg1 %08x\n", port, cfg0, cfg1);
  88. writel(cfg0, &mctl_com->mcr[port][0]);
  89. writel(cfg1, &mctl_com->mcr[port][1]);
  90. }
  91. #define MBUS_CONF(port, bwlimit, qos, acs, bwl0, bwl1, bwl2) \
  92. mbus_configure_port(MBUS_PORT_ ## port, bwlimit, false, \
  93. MBUS_QOS_ ## qos, 0, acs, bwl0, bwl1, bwl2)
  94. static void mctl_set_master_priority_h3(void)
  95. {
  96. struct sunxi_mctl_com_reg * const mctl_com =
  97. (struct sunxi_mctl_com_reg *)SUNXI_DRAM_COM_BASE;
  98. /* enable bandwidth limit windows and set windows size 1us */
  99. writel((1 << 16) | (400 << 0), &mctl_com->bwcr);
  100. /* set cpu high priority */
  101. writel(0x00000001, &mctl_com->mapr);
  102. MBUS_CONF( CPU, true, HIGHEST, 0, 512, 256, 128);
  103. MBUS_CONF( GPU, true, HIGH, 0, 1536, 1024, 256);
  104. MBUS_CONF(UNUSED, true, HIGHEST, 0, 512, 256, 96);
  105. MBUS_CONF( DMA, true, HIGHEST, 0, 256, 128, 32);
  106. MBUS_CONF( VE, true, HIGH, 0, 1792, 1600, 256);
  107. MBUS_CONF( CSI, true, HIGHEST, 0, 256, 128, 32);
  108. MBUS_CONF( NAND, true, HIGH, 0, 256, 128, 64);
  109. MBUS_CONF( SS, true, HIGHEST, 0, 256, 128, 64);
  110. MBUS_CONF( TS, true, HIGHEST, 0, 256, 128, 64);
  111. MBUS_CONF( DI, true, HIGH, 0, 1024, 256, 64);
  112. MBUS_CONF( DE, true, HIGHEST, 3, 8192, 6120, 1024);
  113. MBUS_CONF(DE_CFD, true, HIGH, 0, 1024, 288, 64);
  114. }
  115. static void mctl_set_master_priority_a64(void)
  116. {
  117. struct sunxi_mctl_com_reg * const mctl_com =
  118. (struct sunxi_mctl_com_reg *)SUNXI_DRAM_COM_BASE;
  119. /* enable bandwidth limit windows and set windows size 1us */
  120. writel(399, &mctl_com->tmr);
  121. writel((1 << 16), &mctl_com->bwcr);
  122. /* Port 2 is reserved per Allwinner's linux-3.10 source, yet they
  123. * initialise it */
  124. MBUS_CONF( CPU, true, HIGHEST, 0, 160, 100, 80);
  125. MBUS_CONF( GPU, false, HIGH, 0, 1536, 1400, 256);
  126. MBUS_CONF(UNUSED, true, HIGHEST, 0, 512, 256, 96);
  127. MBUS_CONF( DMA, true, HIGH, 0, 256, 80, 100);
  128. MBUS_CONF( VE, true, HIGH, 0, 1792, 1600, 256);
  129. MBUS_CONF( CSI, true, HIGH, 0, 256, 128, 0);
  130. MBUS_CONF( NAND, true, HIGH, 0, 256, 128, 64);
  131. MBUS_CONF( SS, true, HIGHEST, 0, 256, 128, 64);
  132. MBUS_CONF( TS, true, HIGHEST, 0, 256, 128, 64);
  133. MBUS_CONF( DI, true, HIGH, 0, 1024, 256, 64);
  134. MBUS_CONF( DE, true, HIGH, 2, 8192, 6144, 2048);
  135. MBUS_CONF(DE_CFD, true, HIGH, 0, 1280, 144, 64);
  136. writel(0x81000004, &mctl_com->mdfs_bwlr[2]);
  137. }
  138. static void mctl_set_master_priority_h5(void)
  139. {
  140. struct sunxi_mctl_com_reg * const mctl_com =
  141. (struct sunxi_mctl_com_reg *)SUNXI_DRAM_COM_BASE;
  142. /* enable bandwidth limit windows and set windows size 1us */
  143. writel(399, &mctl_com->tmr);
  144. writel((1 << 16), &mctl_com->bwcr);
  145. /* set cpu high priority */
  146. writel(0x00000001, &mctl_com->mapr);
  147. /* Port 2 is reserved per Allwinner's linux-3.10 source, yet
  148. * they initialise it */
  149. MBUS_CONF( CPU, true, HIGHEST, 0, 300, 260, 150);
  150. MBUS_CONF( GPU, true, HIGHEST, 0, 600, 400, 200);
  151. MBUS_CONF(UNUSED, true, HIGHEST, 0, 512, 256, 96);
  152. MBUS_CONF( DMA, true, HIGHEST, 0, 256, 128, 32);
  153. MBUS_CONF( VE, true, HIGHEST, 0, 1900, 1500, 1000);
  154. MBUS_CONF( CSI, true, HIGHEST, 0, 150, 120, 100);
  155. MBUS_CONF( NAND, true, HIGH, 0, 256, 128, 64);
  156. MBUS_CONF( SS, true, HIGHEST, 0, 256, 128, 64);
  157. MBUS_CONF( TS, true, HIGHEST, 0, 256, 128, 64);
  158. MBUS_CONF( DI, true, HIGH, 0, 1024, 256, 64);
  159. MBUS_CONF( DE, true, HIGHEST, 3, 3400, 2400, 1024);
  160. MBUS_CONF(DE_CFD, true, HIGHEST, 0, 600, 400, 200);
  161. }
  162. static void mctl_set_master_priority_r40(void)
  163. {
  164. struct sunxi_mctl_com_reg * const mctl_com =
  165. (struct sunxi_mctl_com_reg *)SUNXI_DRAM_COM_BASE;
  166. /* enable bandwidth limit windows and set windows size 1us */
  167. writel(399, &mctl_com->tmr);
  168. writel((1 << 16), &mctl_com->bwcr);
  169. /* set cpu high priority */
  170. writel(0x00000001, &mctl_com->mapr);
  171. /* Port 2 is reserved per Allwinner's linux-3.10 source, yet
  172. * they initialise it */
  173. MBUS_CONF( CPU, true, HIGHEST, 0, 300, 260, 150);
  174. MBUS_CONF( GPU, true, HIGHEST, 0, 600, 400, 200);
  175. MBUS_CONF( UNUSED, true, HIGHEST, 0, 512, 256, 96);
  176. MBUS_CONF( DMA, true, HIGHEST, 0, 256, 128, 32);
  177. MBUS_CONF( VE, true, HIGHEST, 0, 1900, 1500, 1000);
  178. MBUS_CONF( CSI, true, HIGHEST, 0, 150, 120, 100);
  179. MBUS_CONF( NAND, true, HIGH, 0, 256, 128, 64);
  180. MBUS_CONF( SS, true, HIGHEST, 0, 256, 128, 64);
  181. MBUS_CONF( TS, true, HIGHEST, 0, 256, 128, 64);
  182. MBUS_CONF( DI, true, HIGH, 0, 1024, 256, 64);
  183. /*
  184. * The port names are probably wrong, but no correct sources
  185. * are available.
  186. */
  187. MBUS_CONF( DE, true, HIGH, 0, 128, 48, 0);
  188. MBUS_CONF( DE_CFD, true, HIGH, 0, 384, 256, 0);
  189. MBUS_CONF(UNKNOWN1, true, HIGHEST, 0, 512, 384, 256);
  190. MBUS_CONF(UNKNOWN2, true, HIGHEST, 2, 8192, 6144, 1024);
  191. MBUS_CONF(UNKNOWN3, true, HIGH, 0, 1280, 144, 64);
  192. }
  193. static void mctl_set_master_priority(uint16_t socid)
  194. {
  195. switch (socid) {
  196. case SOCID_H3:
  197. mctl_set_master_priority_h3();
  198. return;
  199. case SOCID_A64:
  200. mctl_set_master_priority_a64();
  201. return;
  202. case SOCID_H5:
  203. mctl_set_master_priority_h5();
  204. return;
  205. case SOCID_R40:
  206. mctl_set_master_priority_r40();
  207. return;
  208. }
  209. }
  210. static u32 bin_to_mgray(int val)
  211. {
  212. static const u8 lookup_table[32] = {
  213. 0x00, 0x01, 0x02, 0x03, 0x06, 0x07, 0x04, 0x05,
  214. 0x0c, 0x0d, 0x0e, 0x0f, 0x0a, 0x0b, 0x08, 0x09,
  215. 0x18, 0x19, 0x1a, 0x1b, 0x1e, 0x1f, 0x1c, 0x1d,
  216. 0x14, 0x15, 0x16, 0x17, 0x12, 0x13, 0x10, 0x11,
  217. };
  218. return lookup_table[clamp(val, 0, 31)];
  219. }
  220. static int mgray_to_bin(u32 val)
  221. {
  222. static const u8 lookup_table[32] = {
  223. 0x00, 0x01, 0x02, 0x03, 0x06, 0x07, 0x04, 0x05,
  224. 0x0e, 0x0f, 0x0c, 0x0d, 0x08, 0x09, 0x0a, 0x0b,
  225. 0x1e, 0x1f, 0x1c, 0x1d, 0x18, 0x19, 0x1a, 0x1b,
  226. 0x10, 0x11, 0x12, 0x13, 0x16, 0x17, 0x14, 0x15,
  227. };
  228. return lookup_table[val & 0x1f];
  229. }
  230. static void mctl_h3_zq_calibration_quirk(struct dram_para *para)
  231. {
  232. struct sunxi_mctl_ctl_reg * const mctl_ctl =
  233. (struct sunxi_mctl_ctl_reg *)SUNXI_DRAM_CTL0_BASE;
  234. int zq_count;
  235. #if defined CONFIG_SUNXI_DRAM_DW_16BIT
  236. zq_count = 4;
  237. #else
  238. zq_count = 6;
  239. #endif
  240. if ((readl(SUNXI_SRAMC_BASE + 0x24) & 0xff) == 0 &&
  241. (readl(SUNXI_SRAMC_BASE + 0xf0) & 0x1) == 0) {
  242. u32 reg_val;
  243. clrsetbits_le32(&mctl_ctl->zqcr, 0xffff,
  244. CONFIG_DRAM_ZQ & 0xffff);
  245. writel(PIR_CLRSR, &mctl_ctl->pir);
  246. mctl_phy_init(PIR_ZCAL);
  247. reg_val = readl(&mctl_ctl->zqdr[0]);
  248. reg_val &= (0x1f << 16) | (0x1f << 0);
  249. reg_val |= reg_val << 8;
  250. writel(reg_val, &mctl_ctl->zqdr[0]);
  251. reg_val = readl(&mctl_ctl->zqdr[1]);
  252. reg_val &= (0x1f << 16) | (0x1f << 0);
  253. reg_val |= reg_val << 8;
  254. writel(reg_val, &mctl_ctl->zqdr[1]);
  255. writel(reg_val, &mctl_ctl->zqdr[2]);
  256. } else {
  257. int i;
  258. u16 zq_val[6];
  259. u8 val;
  260. writel(0x0a0a0a0a, &mctl_ctl->zqdr[2]);
  261. for (i = 0; i < zq_count; i++) {
  262. u8 zq = (CONFIG_DRAM_ZQ >> (i * 4)) & 0xf;
  263. writel((zq << 20) | (zq << 16) | (zq << 12) |
  264. (zq << 8) | (zq << 4) | (zq << 0),
  265. &mctl_ctl->zqcr);
  266. writel(PIR_CLRSR, &mctl_ctl->pir);
  267. mctl_phy_init(PIR_ZCAL);
  268. zq_val[i] = readl(&mctl_ctl->zqdr[0]) & 0xff;
  269. writel(REPEAT_BYTE(zq_val[i]), &mctl_ctl->zqdr[2]);
  270. writel(PIR_CLRSR, &mctl_ctl->pir);
  271. mctl_phy_init(PIR_ZCAL);
  272. val = readl(&mctl_ctl->zqdr[0]) >> 24;
  273. zq_val[i] |= bin_to_mgray(mgray_to_bin(val) - 1) << 8;
  274. }
  275. writel((zq_val[1] << 16) | zq_val[0], &mctl_ctl->zqdr[0]);
  276. writel((zq_val[3] << 16) | zq_val[2], &mctl_ctl->zqdr[1]);
  277. if (zq_count > 4)
  278. writel((zq_val[5] << 16) | zq_val[4],
  279. &mctl_ctl->zqdr[2]);
  280. }
  281. }
  282. static void mctl_set_cr(uint16_t socid, struct dram_para *para)
  283. {
  284. struct sunxi_mctl_com_reg * const mctl_com =
  285. (struct sunxi_mctl_com_reg *)SUNXI_DRAM_COM_BASE;
  286. writel(MCTL_CR_BL8 | MCTL_CR_INTERLEAVED |
  287. #if defined CONFIG_SUNXI_DRAM_DDR3
  288. MCTL_CR_DDR3 | MCTL_CR_2T |
  289. #elif defined CONFIG_SUNXI_DRAM_DDR2
  290. MCTL_CR_DDR2 | MCTL_CR_2T |
  291. #elif defined CONFIG_SUNXI_DRAM_LPDDR3
  292. MCTL_CR_LPDDR3 | MCTL_CR_1T |
  293. #else
  294. #error Unsupported DRAM type!
  295. #endif
  296. (para->bank_bits == 3 ? MCTL_CR_EIGHT_BANKS : MCTL_CR_FOUR_BANKS) |
  297. MCTL_CR_BUS_FULL_WIDTH(para->bus_full_width) |
  298. (para->dual_rank ? MCTL_CR_DUAL_RANK : MCTL_CR_SINGLE_RANK) |
  299. MCTL_CR_PAGE_SIZE(para->page_size) |
  300. MCTL_CR_ROW_BITS(para->row_bits), &mctl_com->cr);
  301. if (socid == SOCID_R40) {
  302. if (para->dual_rank)
  303. panic("Dual rank memory not supported\n");
  304. /* Mux pin to A15 address line for single rank memory. */
  305. setbits_le32(&mctl_com->cr_r1, MCTL_CR_R1_MUX_A15);
  306. }
  307. }
  308. static void mctl_sys_init(uint16_t socid, struct dram_para *para)
  309. {
  310. struct sunxi_ccm_reg * const ccm =
  311. (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  312. struct sunxi_mctl_ctl_reg * const mctl_ctl =
  313. (struct sunxi_mctl_ctl_reg *)SUNXI_DRAM_CTL0_BASE;
  314. clrbits_le32(&ccm->mbus0_clk_cfg, MBUS_CLK_GATE);
  315. clrbits_le32(&ccm->mbus_reset, CCM_MBUS_RESET_RESET);
  316. clrbits_le32(&ccm->ahb_gate0, 1 << AHB_GATE_OFFSET_MCTL);
  317. clrbits_le32(&ccm->ahb_reset0_cfg, 1 << AHB_RESET_OFFSET_MCTL);
  318. clrbits_le32(&ccm->pll5_cfg, CCM_PLL5_CTRL_EN);
  319. if (socid == SOCID_A64 || socid == SOCID_R40)
  320. clrbits_le32(&ccm->pll11_cfg, CCM_PLL11_CTRL_EN);
  321. udelay(10);
  322. clrbits_le32(&ccm->dram_clk_cfg, CCM_DRAMCLK_CFG_RST);
  323. udelay(1000);
  324. if (socid == SOCID_A64 || socid == SOCID_R40) {
  325. clock_set_pll11(CONFIG_DRAM_CLK * 2 * 1000000, false);
  326. clrsetbits_le32(&ccm->dram_clk_cfg,
  327. CCM_DRAMCLK_CFG_DIV_MASK |
  328. CCM_DRAMCLK_CFG_SRC_MASK,
  329. CCM_DRAMCLK_CFG_DIV(1) |
  330. CCM_DRAMCLK_CFG_SRC_PLL11 |
  331. CCM_DRAMCLK_CFG_UPD);
  332. } else if (socid == SOCID_H3 || socid == SOCID_H5) {
  333. clock_set_pll5(CONFIG_DRAM_CLK * 2 * 1000000, false);
  334. clrsetbits_le32(&ccm->dram_clk_cfg,
  335. CCM_DRAMCLK_CFG_DIV_MASK |
  336. CCM_DRAMCLK_CFG_SRC_MASK,
  337. CCM_DRAMCLK_CFG_DIV(1) |
  338. CCM_DRAMCLK_CFG_SRC_PLL5 |
  339. CCM_DRAMCLK_CFG_UPD);
  340. }
  341. mctl_await_completion(&ccm->dram_clk_cfg, CCM_DRAMCLK_CFG_UPD, 0);
  342. setbits_le32(&ccm->ahb_reset0_cfg, 1 << AHB_RESET_OFFSET_MCTL);
  343. setbits_le32(&ccm->ahb_gate0, 1 << AHB_GATE_OFFSET_MCTL);
  344. setbits_le32(&ccm->mbus_reset, CCM_MBUS_RESET_RESET);
  345. setbits_le32(&ccm->mbus0_clk_cfg, MBUS_CLK_GATE);
  346. setbits_le32(&ccm->dram_clk_cfg, CCM_DRAMCLK_CFG_RST);
  347. udelay(10);
  348. writel(socid == SOCID_H5 ? 0x8000 : 0xc00e, &mctl_ctl->clken);
  349. udelay(500);
  350. }
  351. /* These are more guessed based on some Allwinner code. */
  352. #define DX_GCR_ODT_DYNAMIC (0x0 << 4)
  353. #define DX_GCR_ODT_ALWAYS_ON (0x1 << 4)
  354. #define DX_GCR_ODT_OFF (0x2 << 4)
  355. static int mctl_channel_init(uint16_t socid, struct dram_para *para)
  356. {
  357. struct sunxi_mctl_com_reg * const mctl_com =
  358. (struct sunxi_mctl_com_reg *)SUNXI_DRAM_COM_BASE;
  359. struct sunxi_mctl_ctl_reg * const mctl_ctl =
  360. (struct sunxi_mctl_ctl_reg *)SUNXI_DRAM_CTL0_BASE;
  361. unsigned int i;
  362. mctl_set_cr(socid, para);
  363. mctl_set_timing_params(socid, para);
  364. mctl_set_master_priority(socid);
  365. /* setting VTC, default disable all VT */
  366. clrbits_le32(&mctl_ctl->pgcr[0], (1 << 30) | 0x3f);
  367. if (socid == SOCID_H5)
  368. setbits_le32(&mctl_ctl->pgcr[1], (1 << 24) | (1 << 26));
  369. else
  370. clrsetbits_le32(&mctl_ctl->pgcr[1], 1 << 24, 1 << 26);
  371. /* increase DFI_PHY_UPD clock */
  372. writel(PROTECT_MAGIC, &mctl_com->protect);
  373. udelay(100);
  374. clrsetbits_le32(&mctl_ctl->upd2, 0xfff << 16, 0x50 << 16);
  375. writel(0x0, &mctl_com->protect);
  376. udelay(100);
  377. /* set dramc odt */
  378. for (i = 0; i < 4; i++) {
  379. u32 clearmask = (0x3 << 4) | (0x1 << 1) | (0x3 << 2) |
  380. (0x3 << 12) | (0x3 << 14);
  381. u32 setmask = IS_ENABLED(CONFIG_DRAM_ODT_EN) ?
  382. DX_GCR_ODT_DYNAMIC : DX_GCR_ODT_OFF;
  383. if (socid == SOCID_H5) {
  384. clearmask |= 0x2 << 8;
  385. setmask |= 0x4 << 8;
  386. }
  387. clrsetbits_le32(&mctl_ctl->dx[i].gcr, clearmask, setmask);
  388. }
  389. /* AC PDR should always ON */
  390. clrsetbits_le32(&mctl_ctl->aciocr, socid == SOCID_H5 ? (0x1 << 11) : 0,
  391. 0x1 << 1);
  392. /* set DQS auto gating PD mode */
  393. setbits_le32(&mctl_ctl->pgcr[2], 0x3 << 6);
  394. if (socid == SOCID_H3) {
  395. /* dx ddr_clk & hdr_clk dynamic mode */
  396. clrbits_le32(&mctl_ctl->pgcr[0], (0x3 << 14) | (0x3 << 12));
  397. /* dphy & aphy phase select 270 degree */
  398. clrsetbits_le32(&mctl_ctl->pgcr[2], (0x3 << 10) | (0x3 << 8),
  399. (0x1 << 10) | (0x2 << 8));
  400. } else if (socid == SOCID_A64 || socid == SOCID_H5) {
  401. /* dphy & aphy phase select ? */
  402. clrsetbits_le32(&mctl_ctl->pgcr[2], (0x3 << 10) | (0x3 << 8),
  403. (0x0 << 10) | (0x3 << 8));
  404. } else if (socid == SOCID_R40) {
  405. /* dx ddr_clk & hdr_clk dynamic mode (tpr13[9] == 0) */
  406. clrbits_le32(&mctl_ctl->pgcr[0], (0x3 << 14) | (0x3 << 12));
  407. /* dphy & aphy phase select ? */
  408. clrsetbits_le32(&mctl_ctl->pgcr[2], (0x3 << 10) | (0x3 << 8),
  409. (0x0 << 10) | (0x3 << 8));
  410. }
  411. /* set half DQ */
  412. if (!para->bus_full_width) {
  413. #if defined CONFIG_SUNXI_DRAM_DW_32BIT
  414. writel(0x0, &mctl_ctl->dx[2].gcr);
  415. writel(0x0, &mctl_ctl->dx[3].gcr);
  416. #elif defined CONFIG_SUNXI_DRAM_DW_16BIT
  417. writel(0x0, &mctl_ctl->dx[1].gcr);
  418. #else
  419. #error Unsupported DRAM bus width!
  420. #endif
  421. }
  422. /* data training configuration */
  423. clrsetbits_le32(&mctl_ctl->dtcr, 0xf << 24,
  424. (para->dual_rank ? 0x3 : 0x1) << 24);
  425. mctl_set_bit_delays(para);
  426. udelay(50);
  427. if (socid == SOCID_H3) {
  428. mctl_h3_zq_calibration_quirk(para);
  429. mctl_phy_init(PIR_PLLINIT | PIR_DCAL | PIR_PHYRST |
  430. PIR_DRAMRST | PIR_DRAMINIT | PIR_QSGATE);
  431. } else if (socid == SOCID_A64 || socid == SOCID_H5) {
  432. clrsetbits_le32(&mctl_ctl->zqcr, 0xffffff, CONFIG_DRAM_ZQ);
  433. mctl_phy_init(PIR_ZCAL | PIR_PLLINIT | PIR_DCAL | PIR_PHYRST |
  434. PIR_DRAMRST | PIR_DRAMINIT | PIR_QSGATE);
  435. /* no PIR_QSGATE for H5 ???? */
  436. } else if (socid == SOCID_R40) {
  437. clrsetbits_le32(&mctl_ctl->zqcr, 0xffffff, CONFIG_DRAM_ZQ);
  438. mctl_phy_init(PIR_ZCAL | PIR_PLLINIT | PIR_DCAL | PIR_PHYRST |
  439. PIR_DRAMRST | PIR_DRAMINIT);
  440. }
  441. /* detect ranks and bus width */
  442. if (readl(&mctl_ctl->pgsr[0]) & (0xfe << 20)) {
  443. /* only one rank */
  444. if (((readl(&mctl_ctl->dx[0].gsr[0]) >> 24) & 0x2)
  445. #if defined CONFIG_SUNXI_DRAM_DW_32BIT
  446. || ((readl(&mctl_ctl->dx[1].gsr[0]) >> 24) & 0x2)
  447. #endif
  448. ) {
  449. clrsetbits_le32(&mctl_ctl->dtcr, 0xf << 24, 0x1 << 24);
  450. para->dual_rank = 0;
  451. }
  452. /* only half DQ width */
  453. #if defined CONFIG_SUNXI_DRAM_DW_32BIT
  454. if (((readl(&mctl_ctl->dx[2].gsr[0]) >> 24) & 0x1) ||
  455. ((readl(&mctl_ctl->dx[3].gsr[0]) >> 24) & 0x1)) {
  456. writel(0x0, &mctl_ctl->dx[2].gcr);
  457. writel(0x0, &mctl_ctl->dx[3].gcr);
  458. para->bus_full_width = 0;
  459. }
  460. #elif defined CONFIG_SUNXI_DRAM_DW_16BIT
  461. if ((readl(&mctl_ctl->dx[1].gsr[0]) >> 24) & 0x1) {
  462. writel(0x0, &mctl_ctl->dx[1].gcr);
  463. para->bus_full_width = 0;
  464. }
  465. #endif
  466. mctl_set_cr(socid, para);
  467. udelay(20);
  468. /* re-train */
  469. mctl_phy_init(PIR_QSGATE);
  470. if (readl(&mctl_ctl->pgsr[0]) & (0xfe << 20))
  471. return 1;
  472. }
  473. /* check the dramc status */
  474. mctl_await_completion(&mctl_ctl->statr, 0x1, 0x1);
  475. /* liuke added for refresh debug */
  476. setbits_le32(&mctl_ctl->rfshctl0, 0x1 << 31);
  477. udelay(10);
  478. clrbits_le32(&mctl_ctl->rfshctl0, 0x1 << 31);
  479. udelay(10);
  480. /* set PGCR3, CKE polarity */
  481. if (socid == SOCID_H3)
  482. writel(0x00aa0060, &mctl_ctl->pgcr[3]);
  483. else if (socid == SOCID_A64 || socid == SOCID_H5 || socid == SOCID_R40)
  484. writel(0xc0aa0060, &mctl_ctl->pgcr[3]);
  485. /* power down zq calibration module for power save */
  486. setbits_le32(&mctl_ctl->zqcr, ZQCR_PWRDOWN);
  487. /* enable master access */
  488. writel(0xffffffff, &mctl_com->maer);
  489. return 0;
  490. }
  491. static void mctl_auto_detect_dram_size(uint16_t socid, struct dram_para *para)
  492. {
  493. /* detect row address bits */
  494. para->page_size = 512;
  495. para->row_bits = 16;
  496. para->bank_bits = 2;
  497. mctl_set_cr(socid, para);
  498. for (para->row_bits = 11; para->row_bits < 16; para->row_bits++)
  499. if (mctl_mem_matches((1 << (para->row_bits + para->bank_bits)) * para->page_size))
  500. break;
  501. /* detect bank address bits */
  502. para->bank_bits = 3;
  503. mctl_set_cr(socid, para);
  504. for (para->bank_bits = 2; para->bank_bits < 3; para->bank_bits++)
  505. if (mctl_mem_matches((1 << para->bank_bits) * para->page_size))
  506. break;
  507. /* detect page size */
  508. para->page_size = 8192;
  509. mctl_set_cr(socid, para);
  510. for (para->page_size = 512; para->page_size < 8192; para->page_size *= 2)
  511. if (mctl_mem_matches(para->page_size))
  512. break;
  513. }
  514. /*
  515. * The actual values used here are taken from Allwinner provided boot0
  516. * binaries, though they are probably board specific, so would likely benefit
  517. * from invidual tuning for each board. Apparently a lot of boards copy from
  518. * some Allwinner reference design, so we go with those generic values for now
  519. * in the hope that they are reasonable for most (all?) boards.
  520. */
  521. #define SUN8I_H3_DX_READ_DELAYS \
  522. {{ 18, 18, 18, 18, 18, 18, 18, 18, 18, 0, 0 }, \
  523. { 14, 14, 14, 14, 14, 14, 14, 14, 14, 0, 0 }, \
  524. { 18, 18, 18, 18, 18, 18, 18, 18, 18, 0, 0 }, \
  525. { 14, 14, 14, 14, 14, 14, 14, 14, 14, 0, 0 }}
  526. #define SUN8I_H3_DX_WRITE_DELAYS \
  527. {{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10 }, \
  528. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10 }, \
  529. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10 }, \
  530. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 6 }}
  531. #define SUN8I_H3_AC_DELAYS \
  532. { 0, 0, 0, 0, 0, 0, 0, 0, \
  533. 0, 0, 0, 0, 0, 0, 0, 0, \
  534. 0, 0, 0, 0, 0, 0, 0, 0, \
  535. 0, 0, 0, 0, 0, 0, 0 }
  536. #define SUN8I_R40_DX_READ_DELAYS \
  537. {{ 14, 14, 14, 14, 14, 14, 14, 14, 14, 0, 0 }, \
  538. { 14, 14, 14, 14, 14, 14, 14, 14, 14, 0, 0 }, \
  539. { 14, 14, 14, 14, 14, 14, 14, 14, 14, 0, 0 }, \
  540. { 14, 14, 14, 14, 14, 14, 14, 14, 14, 0, 0 } }
  541. #define SUN8I_R40_DX_WRITE_DELAYS \
  542. {{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0 }, \
  543. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0 }, \
  544. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0 }, \
  545. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0 } }
  546. #define SUN8I_R40_AC_DELAYS \
  547. { 0, 0, 3, 0, 0, 0, 0, 0, \
  548. 0, 0, 0, 0, 0, 0, 0, 0, \
  549. 0, 0, 0, 0, 0, 0, 0, 0, \
  550. 0, 0, 0, 0, 0, 0, 0 }
  551. #define SUN50I_A64_DX_READ_DELAYS \
  552. {{ 16, 16, 16, 16, 17, 16, 16, 17, 16, 1, 0 }, \
  553. { 17, 17, 17, 17, 17, 17, 17, 17, 17, 1, 0 }, \
  554. { 16, 17, 17, 16, 16, 16, 16, 16, 16, 0, 0 }, \
  555. { 17, 17, 17, 17, 17, 17, 17, 17, 17, 1, 0 }}
  556. #define SUN50I_A64_DX_WRITE_DELAYS \
  557. {{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 15 }, \
  558. { 0, 0, 0, 0, 1, 1, 1, 1, 0, 10, 10 }, \
  559. { 1, 0, 1, 1, 1, 1, 1, 1, 0, 11, 11 }, \
  560. { 1, 0, 0, 1, 1, 1, 1, 1, 0, 12, 12 }}
  561. #define SUN50I_A64_AC_DELAYS \
  562. { 5, 5, 13, 10, 2, 5, 3, 3, \
  563. 0, 3, 3, 3, 1, 0, 0, 0, \
  564. 3, 4, 0, 3, 4, 1, 4, 0, \
  565. 1, 1, 0, 1, 13, 5, 4 }
  566. #define SUN8I_H5_DX_READ_DELAYS \
  567. {{ 14, 15, 17, 17, 17, 17, 17, 18, 17, 3, 3 }, \
  568. { 21, 21, 12, 22, 21, 21, 21, 21, 21, 3, 3 }, \
  569. { 16, 19, 19, 17, 22, 22, 21, 22, 19, 3, 3 }, \
  570. { 21, 21, 22, 22, 20, 21, 19, 19, 19, 3, 3 } }
  571. #define SUN8I_H5_DX_WRITE_DELAYS \
  572. {{ 1, 2, 3, 4, 3, 4, 4, 4, 6, 6, 6 }, \
  573. { 6, 6, 6, 5, 5, 5, 5, 5, 6, 6, 6 }, \
  574. { 0, 2, 4, 2, 6, 5, 5, 5, 6, 6, 6 }, \
  575. { 3, 3, 3, 2, 2, 1, 1, 1, 4, 4, 4 } }
  576. #define SUN8I_H5_AC_DELAYS \
  577. { 0, 0, 5, 5, 0, 0, 0, 0, \
  578. 0, 0, 0, 0, 3, 3, 3, 3, \
  579. 3, 3, 3, 3, 3, 3, 3, 3, \
  580. 3, 3, 3, 3, 2, 0, 0 }
  581. unsigned long sunxi_dram_init(void)
  582. {
  583. struct sunxi_mctl_com_reg * const mctl_com =
  584. (struct sunxi_mctl_com_reg *)SUNXI_DRAM_COM_BASE;
  585. struct sunxi_mctl_ctl_reg * const mctl_ctl =
  586. (struct sunxi_mctl_ctl_reg *)SUNXI_DRAM_CTL0_BASE;
  587. struct dram_para para = {
  588. .dual_rank = 1,
  589. .bus_full_width = 1,
  590. .row_bits = 15,
  591. .bank_bits = 3,
  592. .page_size = 4096,
  593. #if defined(CONFIG_MACH_SUN8I_H3)
  594. .dx_read_delays = SUN8I_H3_DX_READ_DELAYS,
  595. .dx_write_delays = SUN8I_H3_DX_WRITE_DELAYS,
  596. .ac_delays = SUN8I_H3_AC_DELAYS,
  597. #elif defined(CONFIG_MACH_SUN8I_R40)
  598. .dx_read_delays = SUN8I_R40_DX_READ_DELAYS,
  599. .dx_write_delays = SUN8I_R40_DX_WRITE_DELAYS,
  600. .ac_delays = SUN8I_R40_AC_DELAYS,
  601. #elif defined(CONFIG_MACH_SUN50I)
  602. .dx_read_delays = SUN50I_A64_DX_READ_DELAYS,
  603. .dx_write_delays = SUN50I_A64_DX_WRITE_DELAYS,
  604. .ac_delays = SUN50I_A64_AC_DELAYS,
  605. #elif defined(CONFIG_MACH_SUN50I_H5)
  606. .dx_read_delays = SUN8I_H5_DX_READ_DELAYS,
  607. .dx_write_delays = SUN8I_H5_DX_WRITE_DELAYS,
  608. .ac_delays = SUN8I_H5_AC_DELAYS,
  609. #endif
  610. };
  611. /*
  612. * Let the compiler optimize alternatives away by passing this value into
  613. * the static functions. This saves us #ifdefs, but still keeps the binary
  614. * small.
  615. */
  616. #if defined(CONFIG_MACH_SUN8I_H3)
  617. uint16_t socid = SOCID_H3;
  618. #elif defined(CONFIG_MACH_SUN8I_R40)
  619. uint16_t socid = SOCID_R40;
  620. /* Currently we cannot support R40 with dual rank memory */
  621. para.dual_rank = 0;
  622. #elif defined(CONFIG_MACH_SUN8I_V3S)
  623. /* TODO: set delays and mbus priority for V3s */
  624. uint16_t socid = SOCID_H3;
  625. #elif defined(CONFIG_MACH_SUN50I)
  626. uint16_t socid = SOCID_A64;
  627. #elif defined(CONFIG_MACH_SUN50I_H5)
  628. uint16_t socid = SOCID_H5;
  629. #endif
  630. mctl_sys_init(socid, &para);
  631. if (mctl_channel_init(socid, &para))
  632. return 0;
  633. if (para.dual_rank)
  634. writel(0x00000303, &mctl_ctl->odtmap);
  635. else
  636. writel(0x00000201, &mctl_ctl->odtmap);
  637. udelay(1);
  638. /* odt delay */
  639. if (socid == SOCID_H3)
  640. writel(0x0c000400, &mctl_ctl->odtcfg);
  641. if (socid == SOCID_A64 || socid == SOCID_H5 || socid == SOCID_R40) {
  642. /* VTF enable (tpr13[8] == 1) */
  643. setbits_le32(&mctl_ctl->vtfcr,
  644. (socid != SOCID_A64 ? 3 : 2) << 8);
  645. /* DQ hold disable (tpr13[26] == 1) */
  646. clrbits_le32(&mctl_ctl->pgcr[2], (1 << 13));
  647. }
  648. /* clear credit value */
  649. setbits_le32(&mctl_com->cccr, 1 << 31);
  650. udelay(10);
  651. mctl_auto_detect_dram_size(socid, &para);
  652. mctl_set_cr(socid, &para);
  653. return (1UL << (para.row_bits + para.bank_bits)) * para.page_size *
  654. (para.dual_rank ? 2 : 1);
  655. }