1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
2ea65c980SMasahiro Yamada /*
3a74c28a0SMasahiro Yamada * Copyright (C) 2011-2014 Panasonic Corporation
4a74c28a0SMasahiro Yamada * Copyright (C) 2015-2016 Socionext Inc.
5a74c28a0SMasahiro Yamada * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
6ea65c980SMasahiro Yamada */
7ea65c980SMasahiro Yamada
8ea65c980SMasahiro Yamada #include <common.h>
90f4ec05bSMasahiro Yamada #include <linux/errno.h>
10ea65c980SMasahiro Yamada #include <linux/io.h>
11ea65c980SMasahiro Yamada #include <linux/sizes.h>
12ea65c980SMasahiro Yamada #include <asm/processor.h>
13ea65c980SMasahiro Yamada
14ea65c980SMasahiro Yamada #include "../init.h"
156dd34ae4SMasahiro Yamada #include "ddrphy-init.h"
16ea65c980SMasahiro Yamada #include "umc-regs.h"
17ea65c980SMasahiro Yamada
18ea65c980SMasahiro Yamada #define DRAM_CH_NR 2
19ea65c980SMasahiro Yamada
20ea65c980SMasahiro Yamada enum dram_freq {
21ea65c980SMasahiro Yamada DRAM_FREQ_1333M,
22ea65c980SMasahiro Yamada DRAM_FREQ_1600M,
23ea65c980SMasahiro Yamada DRAM_FREQ_NR,
24ea65c980SMasahiro Yamada };
25ea65c980SMasahiro Yamada
26ea65c980SMasahiro Yamada enum dram_size {
27ea65c980SMasahiro Yamada DRAM_SZ_128M,
28ea65c980SMasahiro Yamada DRAM_SZ_256M,
29ea65c980SMasahiro Yamada DRAM_SZ_512M,
30ea65c980SMasahiro Yamada DRAM_SZ_NR,
31ea65c980SMasahiro Yamada };
32ea65c980SMasahiro Yamada
33ea65c980SMasahiro Yamada static u32 umc_cmdctla[DRAM_FREQ_NR] = {0x55990b11, 0x66bb0f17};
34ea65c980SMasahiro Yamada static u32 umc_cmdctla_plus[DRAM_FREQ_NR] = {0x45990b11, 0x46bb0f17};
35ea65c980SMasahiro Yamada static u32 umc_cmdctlb[DRAM_FREQ_NR] = {0x16958944, 0x18c6ab44};
36ea65c980SMasahiro Yamada static u32 umc_cmdctlb_plus[DRAM_FREQ_NR] = {0x16958924, 0x18c6ab24};
37ea65c980SMasahiro Yamada static u32 umc_spcctla[DRAM_FREQ_NR][DRAM_SZ_NR] = {
38ea65c980SMasahiro Yamada {0x00240512, 0x00350512, 0x00000000}, /* no data for 1333MHz,128MB */
39ea65c980SMasahiro Yamada {0x002b0617, 0x003f0617, 0x00670617},
40ea65c980SMasahiro Yamada };
41ea65c980SMasahiro Yamada static u32 umc_spcctlb[DRAM_FREQ_NR] = {0x00ff0006, 0x00ff0008};
42ea65c980SMasahiro Yamada static u32 umc_rdatactl[DRAM_FREQ_NR] = {0x000a00ac, 0x000c00ac};
43ea65c980SMasahiro Yamada
umc_get_rank(int ch)44ea65c980SMasahiro Yamada static int umc_get_rank(int ch)
45ea65c980SMasahiro Yamada {
46ea65c980SMasahiro Yamada return ch; /* ch0: rank0, ch1: rank1 for this SoC */
47ea65c980SMasahiro Yamada }
48ea65c980SMasahiro Yamada
umc_start_ssif(void __iomem * ssif_base)49ea65c980SMasahiro Yamada static void umc_start_ssif(void __iomem *ssif_base)
50ea65c980SMasahiro Yamada {
51ea65c980SMasahiro Yamada writel(0x00000000, ssif_base + 0x0000b004);
52ea65c980SMasahiro Yamada writel(0xffffffff, ssif_base + 0x0000c004);
53ea65c980SMasahiro Yamada writel(0x000fffcf, ssif_base + 0x0000c008);
54ea65c980SMasahiro Yamada writel(0x00000001, ssif_base + 0x0000b000);
55ea65c980SMasahiro Yamada writel(0x00000001, ssif_base + 0x0000c000);
56ea65c980SMasahiro Yamada writel(0x03010101, ssif_base + UMC_MDMCHSEL);
57ea65c980SMasahiro Yamada writel(0x03010100, ssif_base + UMC_DMDCHSEL);
58ea65c980SMasahiro Yamada
59ea65c980SMasahiro Yamada writel(0x00000000, ssif_base + UMC_CLKEN_SSIF_FETCH);
60ea65c980SMasahiro Yamada writel(0x00000000, ssif_base + UMC_CLKEN_SSIF_COMQUE0);
61ea65c980SMasahiro Yamada writel(0x00000000, ssif_base + UMC_CLKEN_SSIF_COMWC0);
62ea65c980SMasahiro Yamada writel(0x00000000, ssif_base + UMC_CLKEN_SSIF_COMRC0);
63ea65c980SMasahiro Yamada writel(0x00000000, ssif_base + UMC_CLKEN_SSIF_COMQUE1);
64ea65c980SMasahiro Yamada writel(0x00000000, ssif_base + UMC_CLKEN_SSIF_COMWC1);
65ea65c980SMasahiro Yamada writel(0x00000000, ssif_base + UMC_CLKEN_SSIF_COMRC1);
66ea65c980SMasahiro Yamada writel(0x00000000, ssif_base + UMC_CLKEN_SSIF_WC);
67ea65c980SMasahiro Yamada writel(0x00000000, ssif_base + UMC_CLKEN_SSIF_RC);
68ea65c980SMasahiro Yamada writel(0x00000000, ssif_base + UMC_CLKEN_SSIF_DST);
69ea65c980SMasahiro Yamada
70ea65c980SMasahiro Yamada writel(0x00000001, ssif_base + UMC_CPURST);
71ea65c980SMasahiro Yamada writel(0x00000001, ssif_base + UMC_IDSRST);
72ea65c980SMasahiro Yamada writel(0x00000001, ssif_base + UMC_IXMRST);
73ea65c980SMasahiro Yamada writel(0x00000001, ssif_base + UMC_MDMRST);
74ea65c980SMasahiro Yamada writel(0x00000001, ssif_base + UMC_MDDRST);
75ea65c980SMasahiro Yamada writel(0x00000001, ssif_base + UMC_SIORST);
76ea65c980SMasahiro Yamada writel(0x00000001, ssif_base + UMC_VIORST);
77ea65c980SMasahiro Yamada writel(0x00000001, ssif_base + UMC_FRCRST);
78ea65c980SMasahiro Yamada writel(0x00000001, ssif_base + UMC_RGLRST);
79ea65c980SMasahiro Yamada writel(0x00000001, ssif_base + UMC_AIORST);
80ea65c980SMasahiro Yamada writel(0x00000001, ssif_base + UMC_DMDRST);
81ea65c980SMasahiro Yamada }
82ea65c980SMasahiro Yamada
umc_dramcont_init(void __iomem * dc_base,void __iomem * ca_base,int freq,unsigned long size,bool ddr3plus)83ea65c980SMasahiro Yamada static int umc_dramcont_init(void __iomem *dc_base, void __iomem *ca_base,
84ea65c980SMasahiro Yamada int freq, unsigned long size, bool ddr3plus)
85ea65c980SMasahiro Yamada {
86ea65c980SMasahiro Yamada enum dram_freq freq_e;
87ea65c980SMasahiro Yamada enum dram_size size_e;
88ea65c980SMasahiro Yamada
89ea65c980SMasahiro Yamada switch (freq) {
90ea65c980SMasahiro Yamada case 1333:
91ea65c980SMasahiro Yamada freq_e = DRAM_FREQ_1333M;
92ea65c980SMasahiro Yamada break;
93ea65c980SMasahiro Yamada case 1600:
94ea65c980SMasahiro Yamada freq_e = DRAM_FREQ_1600M;
95ea65c980SMasahiro Yamada break;
96ea65c980SMasahiro Yamada default:
97ea65c980SMasahiro Yamada pr_err("unsupported DRAM frequency %d MHz\n", freq);
98ea65c980SMasahiro Yamada return -EINVAL;
99ea65c980SMasahiro Yamada }
100ea65c980SMasahiro Yamada
101ea65c980SMasahiro Yamada switch (size) {
102ea65c980SMasahiro Yamada case 0:
103ea65c980SMasahiro Yamada return 0;
104ea65c980SMasahiro Yamada case SZ_128M:
105ea65c980SMasahiro Yamada size_e = DRAM_SZ_128M;
106ea65c980SMasahiro Yamada break;
107ea65c980SMasahiro Yamada case SZ_256M:
108ea65c980SMasahiro Yamada size_e = DRAM_SZ_256M;
109ea65c980SMasahiro Yamada break;
110ea65c980SMasahiro Yamada case SZ_512M:
111ea65c980SMasahiro Yamada size_e = DRAM_SZ_512M;
112ea65c980SMasahiro Yamada break;
113ea65c980SMasahiro Yamada default:
114ea65c980SMasahiro Yamada pr_err("unsupported DRAM size 0x%08lx\n", size);
115ea65c980SMasahiro Yamada return -EINVAL;
116ea65c980SMasahiro Yamada }
117ea65c980SMasahiro Yamada
118ea65c980SMasahiro Yamada writel((ddr3plus ? umc_cmdctla_plus : umc_cmdctla)[freq_e],
119ea65c980SMasahiro Yamada dc_base + UMC_CMDCTLA);
120ea65c980SMasahiro Yamada writel((ddr3plus ? umc_cmdctlb_plus : umc_cmdctlb)[freq_e],
121ea65c980SMasahiro Yamada dc_base + UMC_CMDCTLB);
122ea65c980SMasahiro Yamada writel(umc_spcctla[freq_e][size_e], dc_base + UMC_SPCCTLA);
123ea65c980SMasahiro Yamada writel(umc_spcctlb[freq_e], dc_base + UMC_SPCCTLB);
124ea65c980SMasahiro Yamada writel(umc_rdatactl[freq_e], dc_base + UMC_RDATACTL_D0);
125ea65c980SMasahiro Yamada writel(0x04060806, dc_base + UMC_WDATACTL_D0);
126ea65c980SMasahiro Yamada writel(0x04a02000, dc_base + UMC_DATASET);
127ea65c980SMasahiro Yamada writel(0x00000000, ca_base + 0x2300);
128ea65c980SMasahiro Yamada writel(0x00400020, dc_base + UMC_DCCGCTL);
129ea65c980SMasahiro Yamada writel(0x00000003, dc_base + 0x7000);
130ea65c980SMasahiro Yamada writel(0x0000004f, dc_base + 0x8000);
131ea65c980SMasahiro Yamada writel(0x000000c3, dc_base + 0x8004);
132ea65c980SMasahiro Yamada writel(0x00000077, dc_base + 0x8008);
133ea65c980SMasahiro Yamada writel(0x0000003b, dc_base + UMC_DICGCTLA);
134ea65c980SMasahiro Yamada writel(0x020a0808, dc_base + UMC_DICGCTLB);
135ea65c980SMasahiro Yamada writel(0x00000004, dc_base + UMC_FLOWCTLG);
136ea65c980SMasahiro Yamada writel(0x80000201, ca_base + 0xc20);
137ea65c980SMasahiro Yamada writel(0x0801e01e, dc_base + UMC_FLOWCTLA);
138ea65c980SMasahiro Yamada writel(0x00200000, dc_base + UMC_FLOWCTLB);
139ea65c980SMasahiro Yamada writel(0x00004444, dc_base + UMC_FLOWCTLC);
140ea65c980SMasahiro Yamada writel(0x200a0a00, dc_base + UMC_SPCSETB);
141ea65c980SMasahiro Yamada writel(0x00000000, dc_base + UMC_SPCSETD);
142ea65c980SMasahiro Yamada writel(0x00000520, dc_base + UMC_DFICUPDCTLA);
143ea65c980SMasahiro Yamada
144ea65c980SMasahiro Yamada return 0;
145ea65c980SMasahiro Yamada }
146ea65c980SMasahiro Yamada
umc_ch_init(void __iomem * dc_base,void __iomem * ca_base,int freq,unsigned long size,bool ddr3plus,int ch)147ea65c980SMasahiro Yamada static int umc_ch_init(void __iomem *dc_base, void __iomem *ca_base,
148ea65c980SMasahiro Yamada int freq, unsigned long size, bool ddr3plus, int ch)
149ea65c980SMasahiro Yamada {
150ea65c980SMasahiro Yamada void __iomem *phy_base = dc_base + 0x00001000;
151ea65c980SMasahiro Yamada int ret;
152ea65c980SMasahiro Yamada
153ea65c980SMasahiro Yamada writel(UMC_INITSET_INIT1EN, dc_base + UMC_INITSET);
154a8b66ac8SMasahiro Yamada while (readl(dc_base + UMC_INITSTAT) & UMC_INITSTAT_INIT1ST)
155ea65c980SMasahiro Yamada cpu_relax();
156ea65c980SMasahiro Yamada
157ea65c980SMasahiro Yamada writel(0x00000101, dc_base + UMC_DIOCTLA);
158ea65c980SMasahiro Yamada
1595b660066SMasahiro Yamada ret = uniphier_ld4_ddrphy_init(phy_base, freq, ddr3plus);
160ea65c980SMasahiro Yamada if (ret)
161ea65c980SMasahiro Yamada return ret;
162ea65c980SMasahiro Yamada
163ea65c980SMasahiro Yamada ddrphy_prepare_training(phy_base, umc_get_rank(ch));
164ea65c980SMasahiro Yamada ret = ddrphy_training(phy_base);
165ea65c980SMasahiro Yamada if (ret)
166ea65c980SMasahiro Yamada return ret;
167ea65c980SMasahiro Yamada
168ea65c980SMasahiro Yamada return umc_dramcont_init(dc_base, ca_base, freq, size, ddr3plus);
169ea65c980SMasahiro Yamada }
170ea65c980SMasahiro Yamada
uniphier_sld8_umc_init(const struct uniphier_board_data * bd)1715b660066SMasahiro Yamada int uniphier_sld8_umc_init(const struct uniphier_board_data *bd)
172ea65c980SMasahiro Yamada {
173ea65c980SMasahiro Yamada void __iomem *umc_base = (void __iomem *)0x5b800000;
174ea65c980SMasahiro Yamada void __iomem *ca_base = umc_base + 0x00001000;
175ea65c980SMasahiro Yamada void __iomem *dc_base = umc_base + 0x00400000;
176ea65c980SMasahiro Yamada void __iomem *ssif_base = umc_base;
177ea65c980SMasahiro Yamada int ch, ret;
178ea65c980SMasahiro Yamada
179ea65c980SMasahiro Yamada for (ch = 0; ch < DRAM_CH_NR; ch++) {
180ea65c980SMasahiro Yamada ret = umc_ch_init(dc_base, ca_base, bd->dram_freq,
181ea65c980SMasahiro Yamada bd->dram_ch[ch].size,
182a74c28a0SMasahiro Yamada !!(bd->flags & UNIPHIER_BD_DDR3PLUS), ch);
183ea65c980SMasahiro Yamada if (ret) {
184ea65c980SMasahiro Yamada pr_err("failed to initialize UMC ch%d\n", ch);
185ea65c980SMasahiro Yamada return ret;
186ea65c980SMasahiro Yamada }
187ea65c980SMasahiro Yamada
188ea65c980SMasahiro Yamada ca_base += 0x00001000;
189ea65c980SMasahiro Yamada dc_base += 0x00200000;
190ea65c980SMasahiro Yamada }
191ea65c980SMasahiro Yamada
192ea65c980SMasahiro Yamada umc_start_ssif(ssif_base);
193ea65c980SMasahiro Yamada
194ea65c980SMasahiro Yamada return 0;
195ea65c980SMasahiro Yamada }
196