1 /*
2  * (C) Copyright 2007-2012
3  * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
4  * Berg Xing <bergxing@allwinnertech.com>
5  * Tom Cubie <tangliang@allwinnertech.com>
6  *
7  * Sunxi platform dram register definition.
8  *
9  * SPDX-License-Identifier:	GPL-2.0+
10  */
11 
12 #ifndef _SUNXI_DRAM_H
13 #define _SUNXI_DRAM_H
14 
15 #include <linux/types.h>
16 
17 struct sunxi_dram_reg {
18 	u32 ccr;		/* 0x00 controller configuration register */
19 	u32 dcr;		/* 0x04 dram configuration register */
20 	u32 iocr;		/* 0x08 i/o configuration register */
21 	u32 csr;		/* 0x0c controller status register */
22 	u32 drr;		/* 0x10 dram refresh register */
23 	u32 tpr0;		/* 0x14 dram timing parameters register 0 */
24 	u32 tpr1;		/* 0x18 dram timing parameters register 1 */
25 	u32 tpr2;		/* 0x1c dram timing parameters register 2 */
26 	u32 gdllcr;		/* 0x20 global dll control register */
27 	u8 res0[0x28];
28 	u32 rslr0;		/* 0x4c rank system latency register */
29 	u32 rslr1;		/* 0x50 rank system latency register */
30 	u8 res1[0x8];
31 	u32 rdgr0;		/* 0x5c rank dqs gating register */
32 	u32 rdgr1;		/* 0x60 rank dqs gating register */
33 	u8 res2[0x34];
34 	u32 odtcr;		/* 0x98 odt configuration register */
35 	u32 dtr0;		/* 0x9c data training register 0 */
36 	u32 dtr1;		/* 0xa0 data training register 1 */
37 	u32 dtar;		/* 0xa4 data training address register */
38 	u32 zqcr0;		/* 0xa8 zq control register 0 */
39 	u32 zqcr1;		/* 0xac zq control register 1 */
40 	u32 zqsr;		/* 0xb0 zq status register */
41 	u32 idcr;		/* 0xb4 initializaton delay configure reg */
42 	u8 res3[0x138];
43 	u32 mr;			/* 0x1f0 mode register */
44 	u32 emr;		/* 0x1f4 extended mode register */
45 	u32 emr2;		/* 0x1f8 extended mode register */
46 	u32 emr3;		/* 0x1fc extended mode register */
47 	u32 dllctr;		/* 0x200 dll control register */
48 	u32 dllcr[5];		/* 0x204 dll control register 0(byte 0) */
49 	/* 0x208 dll control register 1(byte 1) */
50 	/* 0x20c dll control register 2(byte 2) */
51 	/* 0x210 dll control register 3(byte 3) */
52 	/* 0x214 dll control register 4(byte 4) */
53 	u32 dqtr0;		/* 0x218 dq timing register */
54 	u32 dqtr1;		/* 0x21c dq timing register */
55 	u32 dqtr2;		/* 0x220 dq timing register */
56 	u32 dqtr3;		/* 0x224 dq timing register */
57 	u32 dqstr;		/* 0x228 dqs timing register */
58 	u32 dqsbtr;		/* 0x22c dqsb timing register */
59 	u32 mcr;		/* 0x230 mode configure register */
60 	u8 res[0x8];
61 	u32 ppwrsctl;		/* 0x23c pad power save control */
62 	u32 apr;		/* 0x240 arbiter period register */
63 	u32 pldtr;		/* 0x244 priority level data threshold reg */
64 	u8 res5[0x8];
65 	u32 hpcr[32];		/* 0x250 host port configure register */
66 	u8 res6[0x10];
67 	u32 csel;		/* 0x2e0 controller select register */
68 };
69 
70 struct dram_para {
71 	u32 clock;
72 	u32 mbus_clock;
73 	u32 type;
74 	u32 rank_num;
75 	u32 density;
76 	u32 io_width;
77 	u32 bus_width;
78 	u32 cas;
79 	u32 zq;
80 	u32 odt_en;
81 	u32 size;
82 	u32 tpr0;
83 	u32 tpr1;
84 	u32 tpr2;
85 	u32 tpr3;
86 	u32 tpr4;
87 	u32 tpr5;
88 	u32 emr1;
89 	u32 emr2;
90 	u32 emr3;
91 	u32 dqs_gating_delay;
92 	u32 active_windowing;
93 };
94 
95 #define DRAM_CCR_COMMAND_RATE_1T (0x1 << 5)
96 #define DRAM_CCR_DQS_GATE (0x1 << 14)
97 #define DRAM_CCR_DQS_DRIFT_COMP (0x1 << 17)
98 #define DRAM_CCR_ITM_OFF (0x1 << 28)
99 #define DRAM_CCR_DATA_TRAINING (0x1 << 30)
100 #define DRAM_CCR_INIT (0x1 << 31)
101 
102 #define DRAM_MEMORY_TYPE_DDR1 1
103 #define DRAM_MEMORY_TYPE_DDR2 2
104 #define DRAM_MEMORY_TYPE_DDR3 3
105 #define DRAM_MEMORY_TYPE_LPDDR2 4
106 #define DRAM_MEMORY_TYPE_LPDDR 5
107 #define DRAM_DCR_TYPE (0x1 << 0)
108 #define DRAM_DCR_TYPE_DDR2 0x0
109 #define DRAM_DCR_TYPE_DDR3 0x1
110 #define DRAM_DCR_IO_WIDTH(n) (((n) & 0x3) << 1)
111 #define DRAM_DCR_IO_WIDTH_MASK DRAM_DCR_IO_WIDTH(0x3)
112 #define DRAM_DCR_IO_WIDTH_8BIT 0x0
113 #define DRAM_DCR_IO_WIDTH_16BIT 0x1
114 #define DRAM_DCR_CHIP_DENSITY(n) (((n) & 0x7) << 3)
115 #define DRAM_DCR_CHIP_DENSITY_MASK DRAM_DCR_CHIP_DENSITY(0x7)
116 #define DRAM_DCR_CHIP_DENSITY_256M 0x0
117 #define DRAM_DCR_CHIP_DENSITY_512M 0x1
118 #define DRAM_DCR_CHIP_DENSITY_1024M 0x2
119 #define DRAM_DCR_CHIP_DENSITY_2048M 0x3
120 #define DRAM_DCR_CHIP_DENSITY_4096M 0x4
121 #define DRAM_DCR_CHIP_DENSITY_8192M 0x5
122 #define DRAM_DCR_BUS_WIDTH(n) (((n) & 0x7) << 6)
123 #define DRAM_DCR_BUS_WIDTH_MASK DRAM_DCR_BUS_WIDTH(0x7)
124 #define DRAM_DCR_BUS_WIDTH_32BIT 0x3
125 #define DRAM_DCR_BUS_WIDTH_16BIT 0x1
126 #define DRAM_DCR_BUS_WIDTH_8BIT 0x0
127 #define DRAM_DCR_RANK_SEL(n) (((n) & 0x3) << 10)
128 #define DRAM_DCR_RANK_SEL_MASK DRAM_DCR_CMD_RANK(0x3)
129 #define DRAM_DCR_CMD_RANK_ALL (0x1 << 12)
130 #define DRAM_DCR_MODE(n) (((n) & 0x3) << 13)
131 #define DRAM_DCR_MODE_MASK DRAM_DCR_MODE(0x3)
132 #define DRAM_DCR_MODE_SEQ 0x0
133 #define DRAM_DCR_MODE_INTERLEAVE 0x1
134 
135 #define DRAM_CSR_DTERR  (0x1 << 20)
136 #define DRAM_CSR_DTIERR (0x1 << 21)
137 #define DRAM_CSR_FAILED (DRAM_CSR_DTERR | DRAM_CSR_DTIERR)
138 
139 #define DRAM_DRR_TRFC(n) ((n) & 0xff)
140 #define DRAM_DRR_TREFI(n) (((n) & 0xffff) << 8)
141 #define DRAM_DRR_BURST(n) ((((n) - 1) & 0xf) << 24)
142 
143 #define DRAM_MCR_MODE_NORM(n) (((n) & 0x3) << 0)
144 #define DRAM_MCR_MODE_NORM_MASK DRAM_MCR_MOD_NORM(0x3)
145 #define DRAM_MCR_MODE_DQ_OUT(n) (((n) & 0x3) << 2)
146 #define DRAM_MCR_MODE_DQ_OUT_MASK DRAM_MCR_MODE_DQ_OUT(0x3)
147 #define DRAM_MCR_MODE_ADDR_OUT(n) (((n) & 0x3) << 4)
148 #define DRAM_MCR_MODE_ADDR_OUT_MASK DRAM_MCR_MODE_ADDR_OUT(0x3)
149 #define DRAM_MCR_MODE_DQ_IN_OUT(n) (((n) & 0x3) << 6)
150 #define DRAM_MCR_MODE_DQ_IN_OUT_MASK DRAM_MCR_MODE_DQ_IN_OUT(0x3)
151 #define DRAM_MCR_MODE_DQ_TURNON_DELAY(n) (((n) & 0x7) << 8)
152 #define DRAM_MCR_MODE_DQ_TURNON_DELAY_MASK DRAM_MCR_MODE_DQ_TURNON_DELAY(0x7)
153 #define DRAM_MCR_MODE_ADDR_IN (0x1 << 11)
154 #define DRAM_MCR_RESET (0x1 << 12)
155 #define DRAM_MCR_MODE_EN(n) (((n) & 0x3) << 13)
156 #define DRAM_MCR_MODE_EN_MASK DRAM_MCR_MOD_EN(0x3)
157 #define DRAM_MCR_DCLK_OUT (0x1 << 16)
158 
159 #define DRAM_DLLCR_NRESET (0x1 << 30)
160 #define DRAM_DLLCR_DISABLE (0x1 << 31)
161 
162 #define DRAM_ZQCR0_IMP_DIV(n) (((n) & 0xff) << 20)
163 #define DRAM_ZQCR0_IMP_DIV_MASK DRAM_ZQCR0_IMP_DIV(0xff)
164 #define DRAM_ZQCR0_ZCAL (1 << 31) /* Starts ZQ calibration when set to 1 */
165 #define DRAM_ZQCR0_ZDEN (1 << 28) /* Uses ZDATA instead of doing calibration */
166 
167 #define DRAM_ZQSR_ZDONE (1 << 31) /* ZQ calibration completion flag */
168 
169 #define DRAM_IOCR_ODT_EN(n) ((((n) & 0x3) << 30) | ((n) & 0x3) << 0)
170 #define DRAM_IOCR_ODT_EN_MASK DRAM_IOCR_ODT_EN(0x3)
171 
172 #define DRAM_MR_BURST_LENGTH(n) (((n) & 0x7) << 0)
173 #define DRAM_MR_BURST_LENGTH_MASK DRAM_MR_BURST_LENGTH(0x7)
174 #define DRAM_MR_CAS_LAT(n) (((n) & 0x7) << 4)
175 #define DRAM_MR_CAS_LAT_MASK DRAM_MR_CAS_LAT(0x7)
176 #define DRAM_MR_WRITE_RECOVERY(n) (((n) & 0x7) << 9)
177 #define DRAM_MR_WRITE_RECOVERY_MASK DRAM_MR_WRITE_RECOVERY(0x7)
178 #define DRAM_MR_POWER_DOWN (0x1 << 12)
179 
180 #define DRAM_CSEL_MAGIC 0x16237495
181 
182 unsigned long sunxi_dram_init(void);
183 unsigned long dramc_init(struct dram_para *para);
184 
185 #endif /* _SUNXI_DRAM_H */
186