1132295Srwatson/*
2132295Srwatson * sun50i H616 DDR3-1333 timings, as programmed by Allwinner's boot0
3132295Srwatson *
4132295Srwatson * The chips are probably able to be driven by a faster clock, but boot0
5132295Srwatson * uses a more conservative timing (as usual).
6132295Srwatson *
7132295Srwatson * (C) Copyright 2020 Jernej Skrabec <jernej.skrabec@siol.net>
8132295Srwatson * Based on H6 DDR3 timings:
9132295Srwatson * (C) Copyright 2018,2019 Arm Ltd.
10132295Srwatson *
11132295Srwatson * SPDX-License-Identifier:	GPL-2.0+
12132295Srwatson */
13132295Srwatson
14132295Srwatson#include <common.h>
15132295Srwatson#include <asm/arch/dram.h>
16132295Srwatson#include <asm/arch/cpu.h>
17132295Srwatson
18132295Srwatsonvoid mctl_set_timing_params(const struct dram_para *para)
19132295Srwatson{
20132295Srwatson	struct sunxi_mctl_ctl_reg * const mctl_ctl =
21132295Srwatson			(struct sunxi_mctl_ctl_reg *)SUNXI_DRAM_CTL0_BASE;
22132295Srwatson
23132295Srwatson	u8 tccd		= 2;			/* JEDEC: 4nCK */
24132295Srwatson	u8 tfaw		= ns_to_t(50);		/* JEDEC: 30 ns w/ 1K pages */
25132295Srwatson	u8 trrd		= max(ns_to_t(6), 4);	/* JEDEC: max(6 ns, 4nCK) */
26132295Srwatson	u8 trcd		= ns_to_t(15);		/* JEDEC: 13.5 ns */
27132295Srwatson	u8 trc		= ns_to_t(53);		/* JEDEC: 49.5 ns */
28132295Srwatson	u8 txp		= max(ns_to_t(6), 3);	/* JEDEC: max(6 ns, 3nCK) */
29132295Srwatson	u8 trtp		= max(ns_to_t(8), 2);	/* JEDEC: max(7.5 ns, 4nCK) */
30132295Srwatson	u8 trp		= ns_to_t(15);		/* JEDEC: >= 13.75 ns */
31168273Sjhb	u8 tras		= ns_to_t(38);		/* JEDEC >= 36 ns, <= 9*trefi */
32132295Srwatson	u16 trefi	= ns_to_t(7800) / 32;	/* JEDEC: 7.8us@Tcase <= 85C */
33132295Srwatson	u16 trfc	= ns_to_t(350);		/* JEDEC: 160 ns for 2Gb */
34132295Srwatson	u16 txsr	= 4;			/* ? */
35136843Srwatson
36132295Srwatson	u8 tmrw		= 0;			/* ? */
37132295Srwatson	u8 tmrd		= 4;			/* JEDEC: 4nCK */
38168273Sjhb	u8 tmod		= max(ns_to_t(15), 12);	/* JEDEC: max(15 ns, 12nCK) */
39132295Srwatson	u8 tcke		= max(ns_to_t(6), 3);	/* JEDEC: max(5.625 ns, 3nCK) */
40132295Srwatson	u8 tcksrx	= max(ns_to_t(10), 4);	/* JEDEC: max(10 ns, 5nCK) */
41132295Srwatson	u8 tcksre	= max(ns_to_t(10), 4);	/* JEDEC: max(10 ns, 5nCK) */
42132295Srwatson	u8 tckesr	= tcke + 1;		/* JEDEC: tCKE(min) + 1nCK */
43132295Srwatson	u8 trasmax	= (para->clk / 2) / 15;	/* JEDEC: tREFI * 9 */
44132295Srwatson	u8 txs		= ns_to_t(360) / 32;	/* JEDEC: max(5nCK,tRFC+10ns) */
45132295Srwatson	u8 txsdll	= 16;			/* JEDEC: 512 nCK */
46168273Sjhb	u8 txsabort	= 4;			/* ? */
47168273Sjhb	u8 txsfast	= 4;			/* ? */
48168273Sjhb	u8 tcl		= 7;			/* JEDEC: CL / 2 => 6 */
49168273Sjhb	u8 tcwl		= 5;			/* JEDEC: 8 */
50168273Sjhb	u8 t_rdata_en	= 9;			/* ? */
51168273Sjhb	u8 t_wr_lat	= 5;			/* ? */
52168273Sjhb
53168273Sjhb	u8 twtp;				/* (WL + BL / 2 + tWR) / 2 */
54132295Srwatson	u8 twr2rd;				/* (WL + BL / 2 + tWTR) / 2 */
55132295Srwatson	u8 trd2wr;				/* (RL + BL / 2 + 2 - WL) / 2 */
56132295Srwatson
57132295Srwatson	if (para->tpr2 & 0x100) {
58132295Srwatson		tcl = 5;
59132295Srwatson		tcwl = 4;
60132295Srwatson		t_rdata_en = 5;
61132295Srwatson		t_wr_lat = 3;
62132295Srwatson	}
63132295Srwatson
64132295Srwatson	twtp   = tcl + 2 + tcwl;
65132295Srwatson	twr2rd = trtp + 2 + tcwl;
66168273Sjhb	trd2wr = tcl + 3 - tcwl;
67132295Srwatson
68168273Sjhb	/* set DRAM timing */
69132295Srwatson	writel((twtp << 24) | (tfaw << 16) | (trasmax << 8) | tras,
70168273Sjhb	       &mctl_ctl->dramtmg[0]);
71137587Snik	writel((txp << 16) | (trtp << 8) | trc, &mctl_ctl->dramtmg[1]);
72132295Srwatson	writel((tcwl << 24) | (tcl << 16) | (trd2wr << 8) | twr2rd,
73132295Srwatson	       &mctl_ctl->dramtmg[2]);
74132295Srwatson	writel((tmrw << 20) | (tmrd << 12) | tmod, &mctl_ctl->dramtmg[3]);
75132295Srwatson	writel((trcd << 24) | (tccd << 16) | (trrd << 8) | trp,
76132295Srwatson	       &mctl_ctl->dramtmg[4]);
77136843Srwatson	writel((tcksrx << 24) | (tcksre << 16) | (tckesr << 8) | tcke,
78136843Srwatson	       &mctl_ctl->dramtmg[5]);
79132295Srwatson	/* Value suggested by ZynqMP manual and used by libdram */
80132295Srwatson	writel((txp + 2) | 0x02020000, &mctl_ctl->dramtmg[6]);
81136843Srwatson	writel((txsfast << 24) | (txsabort << 16) | (txsdll << 8) | txs,
82136843Srwatson	       &mctl_ctl->dramtmg[8]);
83132295Srwatson	writel(0x00020208, &mctl_ctl->dramtmg[9]);
84132295Srwatson	writel(0xE0C05, &mctl_ctl->dramtmg[10]);
85132295Srwatson	writel(0x440C021C, &mctl_ctl->dramtmg[11]);
86132295Srwatson	writel(8, &mctl_ctl->dramtmg[12]);
87132295Srwatson	writel(0xA100002, &mctl_ctl->dramtmg[13]);
88132295Srwatson	writel(txsr, &mctl_ctl->dramtmg[14]);
89132295Srwatson
90136843Srwatson	clrbits_le32(&mctl_ctl->init[0], 3 << 30);
91136843Srwatson	writel(0x420000, &mctl_ctl->init[1]);
92132295Srwatson	writel(5, &mctl_ctl->init[2]);
93136843Srwatson	writel(0x1f140004, &mctl_ctl->init[3]);
94136843Srwatson	writel(0x00200000, &mctl_ctl->init[4]);
95132295Srwatson
96132295Srwatson	writel(0, &mctl_ctl->dfimisc);
97136843Srwatson	clrsetbits_le32(&mctl_ctl->rankctl, 0xff0, 0x660);
98136843Srwatson
99132295Srwatson	/* Configure DFI timing */
100136843Srwatson	writel(t_wr_lat | 0x2000000 | (t_rdata_en << 16) | 0x808000,
101136843Srwatson	       &mctl_ctl->dfitmg0);
102132295Srwatson	writel(0x100202, &mctl_ctl->dfitmg1);
103136843Srwatson
104136843Srwatson	/* set refresh timing */
105136843Srwatson	writel((trefi << 16) | trfc, &mctl_ctl->rfshtmg);
106136843Srwatson}
107132295Srwatson