blob: 5392ffa603531df00d908bc05fc648a50d5dc6b4 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Masahiro Yamada95e74872016-01-09 01:51:14 +09002/*
Masahiro Yamadae4e789d2017-01-21 18:05:24 +09003 * Copyright (C) 2015-2017 Socionext Inc.
4 * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
Masahiro Yamada95e74872016-01-09 01:51:14 +09005 *
6 * based on commit 21b6e480f92ccc38fe0502e3116411d6509d3bf2 of Diag by:
7 * Copyright (C) 2015 Socionext Inc.
Masahiro Yamada95e74872016-01-09 01:51:14 +09008 */
9
Masahiro Yamada609cd532017-10-13 19:21:55 +090010#include <linux/delay.h>
Masahiro Yamadae4e789d2017-01-21 18:05:24 +090011#include <linux/errno.h>
Masahiro Yamada95e74872016-01-09 01:51:14 +090012#include <linux/io.h>
Masahiro Yamada609cd532017-10-13 19:21:55 +090013#include <linux/printk.h>
Masahiro Yamada95e74872016-01-09 01:51:14 +090014#include <linux/sizes.h>
15#include <asm/processor.h>
Masahiro Yamada609cd532017-10-13 19:21:55 +090016#include <time.h>
Masahiro Yamada95e74872016-01-09 01:51:14 +090017
18#include "../init.h"
19#include "../soc-info.h"
20#include "ddrmphy-regs.h"
Masahiro Yamada99d80982016-01-17 15:03:29 +090021#include "umc-regs.h"
Masahiro Yamada95e74872016-01-09 01:51:14 +090022
Masahiro Yamadaf98edfe2016-02-26 14:21:37 +090023#define DRAM_CH_NR 3
Masahiro Yamada26a2fb02016-02-05 13:21:07 +090024
Masahiro Yamada95e74872016-01-09 01:51:14 +090025enum dram_freq {
Masahiro Yamadaf98edfe2016-02-26 14:21:37 +090026 DRAM_FREQ_1866M,
27 DRAM_FREQ_2133M,
28 DRAM_FREQ_NR,
Masahiro Yamada95e74872016-01-09 01:51:14 +090029};
30
31enum dram_size {
Masahiro Yamadaf98edfe2016-02-26 14:21:37 +090032 DRAM_SZ_256M,
33 DRAM_SZ_512M,
34 DRAM_SZ_NR,
Masahiro Yamada95e74872016-01-09 01:51:14 +090035};
36
Masahiro Yamada7747f792017-01-28 06:53:46 +090037/* PHY */
Masahiro Yamadaf98edfe2016-02-26 14:21:37 +090038static u32 ddrphy_pgcr2[DRAM_FREQ_NR] = {0x00FC7E5D, 0x00FC90AB};
39static u32 ddrphy_ptr0[DRAM_FREQ_NR] = {0x0EA09205, 0x10C0A6C6};
40static u32 ddrphy_ptr1[DRAM_FREQ_NR] = {0x0DAC041B, 0x0FA104B1};
41static u32 ddrphy_ptr3[DRAM_FREQ_NR] = {0x15171e45, 0x18182357};
42static u32 ddrphy_ptr4[DRAM_FREQ_NR] = {0x0e9ad8e9, 0x10b34157};
43static u32 ddrphy_dtpr0[DRAM_FREQ_NR] = {0x35a00d88, 0x39e40e88};
44static u32 ddrphy_dtpr1[DRAM_FREQ_NR] = {0x2288cc2c, 0x228a04d0};
45static u32 ddrphy_dtpr2[DRAM_FREQ_NR] = {0x50005e00, 0x50006a00};
46static u32 ddrphy_dtpr3[DRAM_FREQ_NR] = {0x0010cb49, 0x0010ec89};
47static u32 ddrphy_mr0[DRAM_FREQ_NR] = {0x00000115, 0x00000125};
48static u32 ddrphy_mr2[DRAM_FREQ_NR] = {0x000002a0, 0x000002a8};
Masahiro Yamada95e74872016-01-09 01:51:14 +090049
Masahiro Yamada26a2fb02016-02-05 13:21:07 +090050/* dependent on package and board design */
Masahiro Yamadaf98edfe2016-02-26 14:21:37 +090051static u32 ddrphy_acbdlr0[DRAM_CH_NR] = {0x0000000c, 0x0000000c, 0x00000009};
Masahiro Yamada26a2fb02016-02-05 13:21:07 +090052
Masahiro Yamada95e74872016-01-09 01:51:14 +090053/* DDR multiPHY */
54static inline int ddrphy_get_rank(int dx)
55{
56 return dx / 2;
57}
58
59static void ddrphy_fifo_reset(void __iomem *phy_base)
60{
61 u32 tmp;
62
Masahiro Yamada7747f792017-01-28 06:53:46 +090063 tmp = readl(phy_base + MPHY_PGCR0);
64 tmp &= ~MPHY_PGCR0_PHYFRST;
65 writel(tmp, phy_base + MPHY_PGCR0);
Masahiro Yamada95e74872016-01-09 01:51:14 +090066
67 udelay(1);
68
Masahiro Yamada7747f792017-01-28 06:53:46 +090069 tmp |= MPHY_PGCR0_PHYFRST;
70 writel(tmp, phy_base + MPHY_PGCR0);
Masahiro Yamada95e74872016-01-09 01:51:14 +090071
72 udelay(1);
73}
74
75static void ddrphy_vt_ctrl(void __iomem *phy_base, int enable)
76{
77 u32 tmp;
78
Masahiro Yamada7747f792017-01-28 06:53:46 +090079 tmp = readl(phy_base + MPHY_PGCR1);
Masahiro Yamada95e74872016-01-09 01:51:14 +090080
81 if (enable)
Masahiro Yamada7747f792017-01-28 06:53:46 +090082 tmp &= ~MPHY_PGCR1_INHVT;
Masahiro Yamada95e74872016-01-09 01:51:14 +090083 else
Masahiro Yamada7747f792017-01-28 06:53:46 +090084 tmp |= MPHY_PGCR1_INHVT;
Masahiro Yamada95e74872016-01-09 01:51:14 +090085
Masahiro Yamada7747f792017-01-28 06:53:46 +090086 writel(tmp, phy_base + MPHY_PGCR1);
Masahiro Yamada95e74872016-01-09 01:51:14 +090087
88 if (!enable) {
Masahiro Yamada7747f792017-01-28 06:53:46 +090089 while (!(readl(phy_base + MPHY_PGSR1) & MPHY_PGSR1_VTSTOP))
Masahiro Yamada95e74872016-01-09 01:51:14 +090090 cpu_relax();
91 }
92}
93
94static void ddrphy_dqs_delay_fixup(void __iomem *phy_base, int nr_dx, int step)
95{
96 int dx;
97 u32 lcdlr1, rdqsd;
Masahiro Yamada7747f792017-01-28 06:53:46 +090098 void __iomem *dx_base = phy_base + MPHY_DX_BASE;
Masahiro Yamada95e74872016-01-09 01:51:14 +090099
100 ddrphy_vt_ctrl(phy_base, 0);
101
102 for (dx = 0; dx < nr_dx; dx++) {
Masahiro Yamada7747f792017-01-28 06:53:46 +0900103 lcdlr1 = readl(dx_base + MPHY_DX_LCDLR1);
Masahiro Yamada95e74872016-01-09 01:51:14 +0900104 rdqsd = (lcdlr1 >> 8) & 0xff;
105 rdqsd = clamp(rdqsd + step, 0U, 0xffU);
106 lcdlr1 = (lcdlr1 & ~(0xff << 8)) | (rdqsd << 8);
Masahiro Yamada7747f792017-01-28 06:53:46 +0900107 writel(lcdlr1, dx_base + MPHY_DX_LCDLR1);
108 readl(dx_base + MPHY_DX_LCDLR1); /* relax */
109 dx_base += MPHY_DX_STRIDE;
Masahiro Yamada95e74872016-01-09 01:51:14 +0900110 }
111
112 ddrphy_vt_ctrl(phy_base, 1);
113}
114
115static int ddrphy_get_system_latency(void __iomem *phy_base, int width)
116{
Masahiro Yamada7747f792017-01-28 06:53:46 +0900117 void __iomem *dx_base = phy_base + MPHY_DX_BASE;
Masahiro Yamada95e74872016-01-09 01:51:14 +0900118 const int nr_dx = width / 8;
119 int dx, rank;
120 u32 gtr;
121 int dgsl, dgsl_min = INT_MAX, dgsl_max = 0;
122
123 for (dx = 0; dx < nr_dx; dx++) {
Masahiro Yamada7747f792017-01-28 06:53:46 +0900124 gtr = readl(dx_base + MPHY_DX_GTR);
Masahiro Yamada95e74872016-01-09 01:51:14 +0900125 for (rank = 0; rank < 4; rank++) {
126 dgsl = gtr & 0x7;
127 /* if dgsl is zero, this rank was not trained. skip. */
128 if (dgsl) {
129 dgsl_min = min(dgsl_min, dgsl);
130 dgsl_max = max(dgsl_max, dgsl);
131 }
132 gtr >>= 3;
133 }
Masahiro Yamada7747f792017-01-28 06:53:46 +0900134 dx_base += MPHY_DX_STRIDE;
Masahiro Yamada95e74872016-01-09 01:51:14 +0900135 }
136
137 if (dgsl_min != dgsl_max)
Masahiro Yamada609cd532017-10-13 19:21:55 +0900138 pr_warn("DQS Gateing System Latencies are not all leveled.\n");
Masahiro Yamada95e74872016-01-09 01:51:14 +0900139
140 return dgsl_max;
141}
142
Masahiro Yamada26a2fb02016-02-05 13:21:07 +0900143static void ddrphy_init(void __iomem *phy_base, enum dram_freq freq, int width,
144 int ch)
Masahiro Yamada95e74872016-01-09 01:51:14 +0900145{
146 u32 tmp;
147 void __iomem *zq_base, *dx_base;
148 int zq, dx;
149 int nr_dx;
150
151 nr_dx = width / 8;
152
Masahiro Yamada609cd532017-10-13 19:21:55 +0900153 writel(MPHY_PIR_ZCALBYP, phy_base + MPHY_PIR);
Masahiro Yamada95e74872016-01-09 01:51:14 +0900154 /*
155 * Disable RGLVT bit (Read DQS Gating LCDL Delay VT Compensation)
156 * to avoid read error issue.
157 */
Masahiro Yamada7747f792017-01-28 06:53:46 +0900158 writel(0x07d81e37, phy_base + MPHY_PGCR0);
159 writel(0x0200c4e0, phy_base + MPHY_PGCR1);
Masahiro Yamada95e74872016-01-09 01:51:14 +0900160
161 tmp = ddrphy_pgcr2[freq];
162 if (width >= 32)
Masahiro Yamada7747f792017-01-28 06:53:46 +0900163 tmp |= MPHY_PGCR2_DUALCHN | MPHY_PGCR2_ACPDDC;
164 writel(tmp, phy_base + MPHY_PGCR2);
Masahiro Yamada95e74872016-01-09 01:51:14 +0900165
Masahiro Yamada7747f792017-01-28 06:53:46 +0900166 writel(ddrphy_ptr0[freq], phy_base + MPHY_PTR0);
167 writel(ddrphy_ptr1[freq], phy_base + MPHY_PTR1);
168 writel(0x00083def, phy_base + MPHY_PTR2);
169 writel(ddrphy_ptr3[freq], phy_base + MPHY_PTR3);
170 writel(ddrphy_ptr4[freq], phy_base + MPHY_PTR4);
Masahiro Yamada95e74872016-01-09 01:51:14 +0900171
Masahiro Yamada7747f792017-01-28 06:53:46 +0900172 writel(ddrphy_acbdlr0[ch], phy_base + MPHY_ACBDLR0);
Masahiro Yamada26a2fb02016-02-05 13:21:07 +0900173
Masahiro Yamada7747f792017-01-28 06:53:46 +0900174 writel(0x55555555, phy_base + MPHY_ACIOCR1);
175 writel(0x00000000, phy_base + MPHY_ACIOCR2);
176 writel(0x55555555, phy_base + MPHY_ACIOCR3);
177 writel(0x00000000, phy_base + MPHY_ACIOCR4);
178 writel(0x00000055, phy_base + MPHY_ACIOCR5);
179 writel(0x00181aa4, phy_base + MPHY_DXCCR);
Masahiro Yamada95e74872016-01-09 01:51:14 +0900180
Masahiro Yamada7747f792017-01-28 06:53:46 +0900181 writel(0x0024641e, phy_base + MPHY_DSGCR);
182 writel(0x0000040b, phy_base + MPHY_DCR);
183 writel(ddrphy_dtpr0[freq], phy_base + MPHY_DTPR0);
184 writel(ddrphy_dtpr1[freq], phy_base + MPHY_DTPR1);
185 writel(ddrphy_dtpr2[freq], phy_base + MPHY_DTPR2);
186 writel(ddrphy_dtpr3[freq], phy_base + MPHY_DTPR3);
187 writel(ddrphy_mr0[freq], phy_base + MPHY_MR0);
188 writel(0x00000006, phy_base + MPHY_MR1);
189 writel(ddrphy_mr2[freq], phy_base + MPHY_MR2);
190 writel(0x00000000, phy_base + MPHY_MR3);
Masahiro Yamada95e74872016-01-09 01:51:14 +0900191
192 tmp = 0;
193 for (dx = 0; dx < nr_dx; dx++)
Masahiro Yamada7747f792017-01-28 06:53:46 +0900194 tmp |= BIT(MPHY_DTCR_RANKEN_SHIFT + ddrphy_get_rank(dx));
195 writel(0x90003087 | tmp, phy_base + MPHY_DTCR);
Masahiro Yamada95e74872016-01-09 01:51:14 +0900196
Masahiro Yamada7747f792017-01-28 06:53:46 +0900197 writel(0x00000000, phy_base + MPHY_DTAR0);
198 writel(0x00000008, phy_base + MPHY_DTAR1);
199 writel(0x00000010, phy_base + MPHY_DTAR2);
200 writel(0x00000018, phy_base + MPHY_DTAR3);
201 writel(0xdd22ee11, phy_base + MPHY_DTDR0);
202 writel(0x7788bb44, phy_base + MPHY_DTDR1);
Masahiro Yamada95e74872016-01-09 01:51:14 +0900203
204 /* impedance control settings */
Masahiro Yamada7747f792017-01-28 06:53:46 +0900205 writel(0x04048900, phy_base + MPHY_ZQCR);
Masahiro Yamada95e74872016-01-09 01:51:14 +0900206
Masahiro Yamada7747f792017-01-28 06:53:46 +0900207 zq_base = phy_base + MPHY_ZQ_BASE;
Masahiro Yamada95e74872016-01-09 01:51:14 +0900208 for (zq = 0; zq < 4; zq++) {
209 /*
210 * board-dependent
211 * PXS2: CH0ZQ0=0x5B, CH1ZQ0=0x5B, CH2ZQ0=0x59, others=0x5D
212 */
Masahiro Yamada7747f792017-01-28 06:53:46 +0900213 writel(0x0007BB5D, zq_base + MPHY_ZQ_PR);
214 zq_base += MPHY_ZQ_STRIDE;
Masahiro Yamada95e74872016-01-09 01:51:14 +0900215 }
216
217 /* DATX8 settings */
Masahiro Yamada7747f792017-01-28 06:53:46 +0900218 dx_base = phy_base + MPHY_DX_BASE;
Masahiro Yamada95e74872016-01-09 01:51:14 +0900219 for (dx = 0; dx < 4; dx++) {
Masahiro Yamada7747f792017-01-28 06:53:46 +0900220 tmp = readl(dx_base + MPHY_DX_GCR0);
221 tmp &= ~MPHY_DX_GCR0_WLRKEN_MASK;
222 tmp |= BIT(MPHY_DX_GCR0_WLRKEN_SHIFT + ddrphy_get_rank(dx)) &
223 MPHY_DX_GCR0_WLRKEN_MASK;
224 writel(tmp, dx_base + MPHY_DX_GCR0);
Masahiro Yamada95e74872016-01-09 01:51:14 +0900225
Masahiro Yamada7747f792017-01-28 06:53:46 +0900226 writel(0x00000000, dx_base + MPHY_DX_GCR1);
227 writel(0x00000000, dx_base + MPHY_DX_GCR2);
228 writel(0x00000000, dx_base + MPHY_DX_GCR3);
229 dx_base += MPHY_DX_STRIDE;
Masahiro Yamada95e74872016-01-09 01:51:14 +0900230 }
231
Masahiro Yamada7747f792017-01-28 06:53:46 +0900232 while (!(readl(phy_base + MPHY_PGSR0) & MPHY_PGSR0_IDONE))
Masahiro Yamada95e74872016-01-09 01:51:14 +0900233 cpu_relax();
234
235 ddrphy_dqs_delay_fixup(phy_base, nr_dx, -4);
236}
237
238struct ddrphy_init_sequence {
239 char *description;
240 u32 init_flag;
241 u32 done_flag;
242 u32 err_flag;
243};
244
245static const struct ddrphy_init_sequence impedance_calibration_sequence[] = {
246 {
247 "Impedance Calibration",
Masahiro Yamada7747f792017-01-28 06:53:46 +0900248 MPHY_PIR_ZCAL,
249 MPHY_PGSR0_ZCDONE,
250 MPHY_PGSR0_ZCERR,
Masahiro Yamada95e74872016-01-09 01:51:14 +0900251 },
252 { /* sentinel */ }
253};
254
255static const struct ddrphy_init_sequence dram_init_sequence[] = {
256 {
257 "DRAM Initialization",
Masahiro Yamada7747f792017-01-28 06:53:46 +0900258 MPHY_PIR_DRAMRST | MPHY_PIR_DRAMINIT,
259 MPHY_PGSR0_DIDONE,
Masahiro Yamada95e74872016-01-09 01:51:14 +0900260 0,
261 },
262 { /* sentinel */ }
263};
264
265static const struct ddrphy_init_sequence training_sequence[] = {
266 {
267 "Write Leveling",
Masahiro Yamada7747f792017-01-28 06:53:46 +0900268 MPHY_PIR_WL,
269 MPHY_PGSR0_WLDONE,
270 MPHY_PGSR0_WLERR,
Masahiro Yamada95e74872016-01-09 01:51:14 +0900271 },
272 {
273 "Read DQS Gate Training",
Masahiro Yamada7747f792017-01-28 06:53:46 +0900274 MPHY_PIR_QSGATE,
275 MPHY_PGSR0_QSGDONE,
276 MPHY_PGSR0_QSGERR,
Masahiro Yamada95e74872016-01-09 01:51:14 +0900277 },
278 {
279 "Write Leveling Adjustment",
Masahiro Yamada7747f792017-01-28 06:53:46 +0900280 MPHY_PIR_WLADJ,
281 MPHY_PGSR0_WLADONE,
282 MPHY_PGSR0_WLAERR,
Masahiro Yamada95e74872016-01-09 01:51:14 +0900283 },
284 {
285 "Read Bit Deskew",
Masahiro Yamada7747f792017-01-28 06:53:46 +0900286 MPHY_PIR_RDDSKW,
287 MPHY_PGSR0_RDDONE,
288 MPHY_PGSR0_RDERR,
Masahiro Yamada95e74872016-01-09 01:51:14 +0900289 },
290 {
291 "Write Bit Deskew",
Masahiro Yamada7747f792017-01-28 06:53:46 +0900292 MPHY_PIR_WRDSKW,
293 MPHY_PGSR0_WDDONE,
294 MPHY_PGSR0_WDERR,
Masahiro Yamada95e74872016-01-09 01:51:14 +0900295 },
296 {
297 "Read Eye Training",
Masahiro Yamada7747f792017-01-28 06:53:46 +0900298 MPHY_PIR_RDEYE,
299 MPHY_PGSR0_REDONE,
300 MPHY_PGSR0_REERR,
Masahiro Yamada95e74872016-01-09 01:51:14 +0900301 },
302 {
303 "Write Eye Training",
Masahiro Yamada7747f792017-01-28 06:53:46 +0900304 MPHY_PIR_WREYE,
305 MPHY_PGSR0_WEDONE,
306 MPHY_PGSR0_WEERR,
Masahiro Yamada95e74872016-01-09 01:51:14 +0900307 },
308 { /* sentinel */ }
309};
310
311static int __ddrphy_training(void __iomem *phy_base,
312 const struct ddrphy_init_sequence *seq)
313{
314 const struct ddrphy_init_sequence *s;
315 u32 pgsr0;
Masahiro Yamada7747f792017-01-28 06:53:46 +0900316 u32 init_flag = MPHY_PIR_INIT;
317 u32 done_flag = MPHY_PGSR0_IDONE;
Masahiro Yamada95e74872016-01-09 01:51:14 +0900318 int timeout = 50000; /* 50 msec is long enough */
Masahiro Yamada609cd532017-10-13 19:21:55 +0900319 unsigned long start = 0;
320
321#ifdef DEBUG
322 start = get_timer(0);
Masahiro Yamada95e74872016-01-09 01:51:14 +0900323#endif
324
325 for (s = seq; s->description; s++) {
326 init_flag |= s->init_flag;
327 done_flag |= s->done_flag;
328 }
329
Masahiro Yamada7747f792017-01-28 06:53:46 +0900330 writel(init_flag, phy_base + MPHY_PIR);
Masahiro Yamada95e74872016-01-09 01:51:14 +0900331
332 do {
333 if (--timeout < 0) {
Masahiro Yamada20abe262016-02-26 14:21:36 +0900334 pr_err("%s: error: timeout during DDR training\n",
Masahiro Yamada95e74872016-01-09 01:51:14 +0900335 __func__);
336 return -ETIMEDOUT;
337 }
338 udelay(1);
Masahiro Yamada7747f792017-01-28 06:53:46 +0900339 pgsr0 = readl(phy_base + MPHY_PGSR0);
Masahiro Yamada95e74872016-01-09 01:51:14 +0900340 } while ((pgsr0 & done_flag) != done_flag);
341
342 for (s = seq; s->description; s++) {
343 if (pgsr0 & s->err_flag) {
Masahiro Yamada20abe262016-02-26 14:21:36 +0900344 pr_err("%s: error: %s failed\n", __func__,
Masahiro Yamada95e74872016-01-09 01:51:14 +0900345 s->description);
346 return -EIO;
347 }
348 }
349
Masahiro Yamada609cd532017-10-13 19:21:55 +0900350 pr_debug("DDRPHY training: elapsed time %ld msec\n", get_timer(start));
Masahiro Yamada95e74872016-01-09 01:51:14 +0900351
352 return 0;
353}
354
355static int ddrphy_impedance_calibration(void __iomem *phy_base)
356{
357 int ret;
358 u32 tmp;
359
360 ret = __ddrphy_training(phy_base, impedance_calibration_sequence);
361 if (ret)
362 return ret;
363
364 /*
365 * Because of a hardware bug, IDONE flag is set when the first ZQ block
366 * is calibrated. The flag does not guarantee the completion for all
367 * the ZQ blocks. Wait a little more just in case.
368 */
369 udelay(1);
370
371 /* reflect ZQ settings and enable average algorithm*/
Masahiro Yamada7747f792017-01-28 06:53:46 +0900372 tmp = readl(phy_base + MPHY_ZQCR);
373 tmp |= MPHY_ZQCR_FORCE_ZCAL_VT_UPDATE;
374 writel(tmp, phy_base + MPHY_ZQCR);
375 tmp &= ~MPHY_ZQCR_FORCE_ZCAL_VT_UPDATE;
376 tmp |= MPHY_ZQCR_AVGEN;
377 writel(tmp, phy_base + MPHY_ZQCR);
Masahiro Yamada95e74872016-01-09 01:51:14 +0900378
379 return 0;
380}
381
382static int ddrphy_dram_init(void __iomem *phy_base)
383{
384 return __ddrphy_training(phy_base, dram_init_sequence);
385}
386
387static int ddrphy_training(void __iomem *phy_base)
388{
389 return __ddrphy_training(phy_base, training_sequence);
390}
391
392/* UMC */
Masahiro Yamada7747f792017-01-28 06:53:46 +0900393static u32 umc_cmdctla[DRAM_FREQ_NR] = {0x66DD131D, 0x77EE1722};
394/*
395 * The ch2 is a different generation UMC core.
396 * The register spec is different, unfortunately.
397 */
398static u32 umc_cmdctlb_ch01[DRAM_FREQ_NR] = {0x13E87C44, 0x18F88C44};
399static u32 umc_cmdctlb_ch2[DRAM_FREQ_NR] = {0x19E8DC44, 0x1EF8EC44};
400static u32 umc_spcctla[DRAM_FREQ_NR][DRAM_SZ_NR] = {
401 {0x004A071D, 0x0078071D},
402 {0x0055081E, 0x0089081E},
403};
404
405static u32 umc_spcctlb[] = {0x00FF000A, 0x00FF000B};
406/* The ch2 is different for some reason only hardware guys know... */
407static u32 umc_flowctla_ch01[] = {0x0800001E, 0x08000022};
408static u32 umc_flowctla_ch2[] = {0x0800001E, 0x0800001E};
409
Masahiro Yamada392ecb62016-02-26 14:21:53 +0900410static void umc_set_system_latency(void __iomem *dc_base, int phy_latency)
Masahiro Yamada95e74872016-01-09 01:51:14 +0900411{
412 u32 val;
413 int latency;
414
Masahiro Yamada392ecb62016-02-26 14:21:53 +0900415 val = readl(dc_base + UMC_RDATACTL_D0);
Masahiro Yamada95e74872016-01-09 01:51:14 +0900416 latency = (val & UMC_RDATACTL_RADLTY_MASK) >> UMC_RDATACTL_RADLTY_SHIFT;
417 latency += (val & UMC_RDATACTL_RAD2LTY_MASK) >>
418 UMC_RDATACTL_RAD2LTY_SHIFT;
419 /*
420 * UMC works at the half clock rate of the PHY.
421 * The LSB of latency is ignored
422 */
423 latency += phy_latency & ~1;
424
425 val &= ~(UMC_RDATACTL_RADLTY_MASK | UMC_RDATACTL_RAD2LTY_MASK);
426 if (latency > 0xf) {
427 val |= 0xf << UMC_RDATACTL_RADLTY_SHIFT;
428 val |= (latency - 0xf) << UMC_RDATACTL_RAD2LTY_SHIFT;
429 } else {
430 val |= latency << UMC_RDATACTL_RADLTY_SHIFT;
431 }
432
Masahiro Yamada392ecb62016-02-26 14:21:53 +0900433 writel(val, dc_base + UMC_RDATACTL_D0);
434 writel(val, dc_base + UMC_RDATACTL_D1);
Masahiro Yamada95e74872016-01-09 01:51:14 +0900435
Masahiro Yamada392ecb62016-02-26 14:21:53 +0900436 readl(dc_base + UMC_RDATACTL_D1); /* relax */
Masahiro Yamada95e74872016-01-09 01:51:14 +0900437}
438
439/* enable/disable auto refresh */
Masahiro Yamada5e05c4d2017-06-22 16:42:04 +0900440static void umc_refresh_ctrl(void __iomem *dc_base, int enable)
Masahiro Yamada95e74872016-01-09 01:51:14 +0900441{
442 u32 tmp;
443
Masahiro Yamada392ecb62016-02-26 14:21:53 +0900444 tmp = readl(dc_base + UMC_SPCSETB);
Masahiro Yamada95e74872016-01-09 01:51:14 +0900445 tmp &= ~UMC_SPCSETB_AREFMD_MASK;
446
447 if (enable)
448 tmp |= UMC_SPCSETB_AREFMD_ARB;
449 else
450 tmp |= UMC_SPCSETB_AREFMD_REG;
451
Masahiro Yamada392ecb62016-02-26 14:21:53 +0900452 writel(tmp, dc_base + UMC_SPCSETB);
Masahiro Yamada95e74872016-01-09 01:51:14 +0900453 udelay(1);
454}
455
456static void umc_ud_init(void __iomem *umc_base, int ch)
457{
458 writel(0x00000003, umc_base + UMC_BITPERPIXELMODE_D0);
459
460 if (ch == 2)
461 writel(0x00000033, umc_base + UMC_PAIR1DOFF_D0);
462}
463
Masahiro Yamada392ecb62016-02-26 14:21:53 +0900464static int umc_dc_init(void __iomem *dc_base, enum dram_freq freq,
Masahiro Yamadaf98edfe2016-02-26 14:21:37 +0900465 unsigned long size, int width, int ch)
Masahiro Yamada95e74872016-01-09 01:51:14 +0900466{
Masahiro Yamadaf98edfe2016-02-26 14:21:37 +0900467 enum dram_size size_e;
Masahiro Yamada95e74872016-01-09 01:51:14 +0900468 int latency;
469 u32 val;
470
Masahiro Yamadaf98edfe2016-02-26 14:21:37 +0900471 switch (size) {
472 case 0:
473 return 0;
474 case SZ_256M:
475 size_e = DRAM_SZ_256M;
476 break;
477 case SZ_512M:
478 size_e = DRAM_SZ_512M;
479 break;
480 default:
481 pr_err("unsupported DRAM size 0x%08lx (per 16bit) for ch%d\n",
482 size, ch);
483 return -EINVAL;
484 }
485
Masahiro Yamada392ecb62016-02-26 14:21:53 +0900486 writel(umc_cmdctla[freq], dc_base + UMC_CMDCTLA);
Masahiro Yamada95e74872016-01-09 01:51:14 +0900487
488 writel(ch == 2 ? umc_cmdctlb_ch2[freq] : umc_cmdctlb_ch01[freq],
Masahiro Yamada392ecb62016-02-26 14:21:53 +0900489 dc_base + UMC_CMDCTLB);
Masahiro Yamada95e74872016-01-09 01:51:14 +0900490
Masahiro Yamada392ecb62016-02-26 14:21:53 +0900491 writel(umc_spcctla[freq][size_e], dc_base + UMC_SPCCTLA);
492 writel(umc_spcctlb[freq], dc_base + UMC_SPCCTLB);
Masahiro Yamada95e74872016-01-09 01:51:14 +0900493
494 val = 0x000e000e;
495 latency = 12;
496 /* ES2 inserted one more FF to the logic. */
497 if (uniphier_get_soc_model() >= 2)
498 latency += 2;
499
500 if (latency > 0xf) {
501 val |= 0xf << UMC_RDATACTL_RADLTY_SHIFT;
502 val |= (latency - 0xf) << UMC_RDATACTL_RAD2LTY_SHIFT;
503 } else {
504 val |= latency << UMC_RDATACTL_RADLTY_SHIFT;
505 }
506
Masahiro Yamada392ecb62016-02-26 14:21:53 +0900507 writel(val, dc_base + UMC_RDATACTL_D0);
Masahiro Yamada95e74872016-01-09 01:51:14 +0900508 if (width >= 32)
Masahiro Yamada392ecb62016-02-26 14:21:53 +0900509 writel(val, dc_base + UMC_RDATACTL_D1);
Masahiro Yamada95e74872016-01-09 01:51:14 +0900510
Masahiro Yamada392ecb62016-02-26 14:21:53 +0900511 writel(0x04060A02, dc_base + UMC_WDATACTL_D0);
Masahiro Yamada95e74872016-01-09 01:51:14 +0900512 if (width >= 32)
Masahiro Yamada392ecb62016-02-26 14:21:53 +0900513 writel(0x04060A02, dc_base + UMC_WDATACTL_D1);
514 writel(0x04000000, dc_base + UMC_DATASET);
515 writel(0x00400020, dc_base + UMC_DCCGCTL);
516 writel(0x00000084, dc_base + UMC_FLOWCTLG);
517 writel(0x00000000, dc_base + UMC_ACSSETA);
Masahiro Yamada95e74872016-01-09 01:51:14 +0900518
519 writel(ch == 2 ? umc_flowctla_ch2[freq] : umc_flowctla_ch01[freq],
Masahiro Yamada392ecb62016-02-26 14:21:53 +0900520 dc_base + UMC_FLOWCTLA);
Masahiro Yamada95e74872016-01-09 01:51:14 +0900521
Masahiro Yamada392ecb62016-02-26 14:21:53 +0900522 writel(0x00004400, dc_base + UMC_FLOWCTLC);
523 writel(0x200A0A00, dc_base + UMC_SPCSETB);
524 writel(0x00000520, dc_base + UMC_DFICUPDCTLA);
525 writel(0x0000000D, dc_base + UMC_RESPCTL);
Masahiro Yamada95e74872016-01-09 01:51:14 +0900526
527 if (ch != 2) {
Masahiro Yamada392ecb62016-02-26 14:21:53 +0900528 writel(0x00202000, dc_base + UMC_FLOWCTLB);
529 writel(0xFDBFFFFF, dc_base + UMC_FLOWCTLOB0);
530 writel(0xFFFFFFFF, dc_base + UMC_FLOWCTLOB1);
531 writel(0x00080700, dc_base + UMC_BSICMAPSET);
Masahiro Yamada95e74872016-01-09 01:51:14 +0900532 } else {
Masahiro Yamada392ecb62016-02-26 14:21:53 +0900533 writel(0x00200000, dc_base + UMC_FLOWCTLB);
534 writel(0x00000000, dc_base + UMC_BSICMAPSET);
Masahiro Yamada95e74872016-01-09 01:51:14 +0900535 }
536
Masahiro Yamada392ecb62016-02-26 14:21:53 +0900537 writel(0x00000000, dc_base + UMC_ERRMASKA);
538 writel(0x00000000, dc_base + UMC_ERRMASKB);
Masahiro Yamadaf98edfe2016-02-26 14:21:37 +0900539
540 return 0;
Masahiro Yamada95e74872016-01-09 01:51:14 +0900541}
542
Masahiro Yamadaf98edfe2016-02-26 14:21:37 +0900543static int umc_ch_init(void __iomem *umc_ch_base, enum dram_freq freq,
544 unsigned long size, unsigned int width, int ch)
Masahiro Yamada95e74872016-01-09 01:51:14 +0900545{
Masahiro Yamada392ecb62016-02-26 14:21:53 +0900546 void __iomem *dc_base = umc_ch_base + 0x00011000;
Masahiro Yamadaf98edfe2016-02-26 14:21:37 +0900547 void __iomem *phy_base = umc_ch_base + 0x00030000;
Masahiro Yamada95e74872016-01-09 01:51:14 +0900548 int ret;
549
Masahiro Yamada392ecb62016-02-26 14:21:53 +0900550 writel(0x00000002, dc_base + UMC_INITSET);
551 while (readl(dc_base + UMC_INITSTAT) & BIT(2))
Masahiro Yamada95e74872016-01-09 01:51:14 +0900552 cpu_relax();
553
554 /* deassert PHY reset signals */
555 writel(UMC_DIOCTLA_CTL_NRST | UMC_DIOCTLA_CFG_NRST,
Masahiro Yamada392ecb62016-02-26 14:21:53 +0900556 dc_base + UMC_DIOCTLA);
Masahiro Yamada95e74872016-01-09 01:51:14 +0900557
Masahiro Yamada26a2fb02016-02-05 13:21:07 +0900558 ddrphy_init(phy_base, freq, width, ch);
Masahiro Yamada95e74872016-01-09 01:51:14 +0900559
560 ret = ddrphy_impedance_calibration(phy_base);
561 if (ret)
562 return ret;
563
564 ddrphy_dram_init(phy_base);
565 if (ret)
566 return ret;
567
Masahiro Yamada392ecb62016-02-26 14:21:53 +0900568 ret = umc_dc_init(dc_base, freq, size, width, ch);
Masahiro Yamadaf98edfe2016-02-26 14:21:37 +0900569 if (ret)
570 return ret;
Masahiro Yamada95e74872016-01-09 01:51:14 +0900571
Masahiro Yamadaf98edfe2016-02-26 14:21:37 +0900572 umc_ud_init(umc_ch_base, ch);
Masahiro Yamada95e74872016-01-09 01:51:14 +0900573
Masahiro Yamadaf98edfe2016-02-26 14:21:37 +0900574 ret = ddrphy_training(phy_base);
575 if (ret)
576 return ret;
Masahiro Yamada95e74872016-01-09 01:51:14 +0900577
578 udelay(1);
579
580 /* match the system latency between UMC and PHY */
Masahiro Yamada392ecb62016-02-26 14:21:53 +0900581 umc_set_system_latency(dc_base,
Masahiro Yamada95e74872016-01-09 01:51:14 +0900582 ddrphy_get_system_latency(phy_base, width));
583
584 udelay(1);
585
586 /* stop auto refresh before clearing FIFO in PHY */
Masahiro Yamada392ecb62016-02-26 14:21:53 +0900587 umc_refresh_ctrl(dc_base, 0);
Masahiro Yamada95e74872016-01-09 01:51:14 +0900588 ddrphy_fifo_reset(phy_base);
Masahiro Yamada392ecb62016-02-26 14:21:53 +0900589 umc_refresh_ctrl(dc_base, 1);
Masahiro Yamada95e74872016-01-09 01:51:14 +0900590
591 udelay(10);
592
593 return 0;
594}
595
596static void um_init(void __iomem *um_base)
597{
598 writel(0x000000ff, um_base + UMC_MBUS0);
599 writel(0x000000ff, um_base + UMC_MBUS1);
600 writel(0x000000ff, um_base + UMC_MBUS2);
601 writel(0x000000ff, um_base + UMC_MBUS3);
602}
603
Masahiro Yamada98905692016-03-30 20:17:02 +0900604int uniphier_pxs2_umc_init(const struct uniphier_board_data *bd)
Masahiro Yamada95e74872016-01-09 01:51:14 +0900605{
606 void __iomem *um_base = (void __iomem *)0x5b600000;
Masahiro Yamada11828542016-02-26 14:21:35 +0900607 void __iomem *umc_ch_base = (void __iomem *)0x5b800000;
Masahiro Yamada95e74872016-01-09 01:51:14 +0900608 enum dram_freq freq;
Masahiro Yamada11828542016-02-26 14:21:35 +0900609 int ch, ret;
Masahiro Yamada95e74872016-01-09 01:51:14 +0900610
611 switch (bd->dram_freq) {
612 case 1866:
Masahiro Yamadaf98edfe2016-02-26 14:21:37 +0900613 freq = DRAM_FREQ_1866M;
Masahiro Yamada95e74872016-01-09 01:51:14 +0900614 break;
615 case 2133:
Masahiro Yamadaf98edfe2016-02-26 14:21:37 +0900616 freq = DRAM_FREQ_2133M;
Masahiro Yamada95e74872016-01-09 01:51:14 +0900617 break;
618 default:
Masahiro Yamada20abe262016-02-26 14:21:36 +0900619 pr_err("unsupported DRAM frequency %d MHz\n", bd->dram_freq);
Masahiro Yamada95e74872016-01-09 01:51:14 +0900620 return -EINVAL;
621 }
622
Masahiro Yamadae8e0a5d2017-02-16 15:59:32 +0900623 for (ch = 0; ch < DRAM_CH_NR; ch++) {
Masahiro Yamadaf98edfe2016-02-26 14:21:37 +0900624 unsigned long size = bd->dram_ch[ch].size;
625 unsigned int width = bd->dram_ch[ch].width;
626
Masahiro Yamadae8e0a5d2017-02-16 15:59:32 +0900627 if (size) {
628 ret = umc_ch_init(umc_ch_base, freq,
629 size / (width / 16), width, ch);
630 if (ret) {
631 pr_err("failed to initialize UMC ch%d\n", ch);
632 return ret;
633 }
Masahiro Yamada11828542016-02-26 14:21:35 +0900634 }
Masahiro Yamada95e74872016-01-09 01:51:14 +0900635
Masahiro Yamada11828542016-02-26 14:21:35 +0900636 umc_ch_base += 0x00200000;
Masahiro Yamada95e74872016-01-09 01:51:14 +0900637 }
638
639 um_init(um_base);
640
641 return 0;
642}