1
2
3
4
5
6#include <common.h>
7#include <fdtdec.h>
8#include <asm/io.h>
9#include <asm/arch-tegra/ap.h>
10#include <asm/arch-tegra/apb_misc.h>
11#include <asm/arch/clock.h>
12#include <asm/arch/emc.h>
13#include <asm/arch/tegra.h>
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29static const unsigned long emc_reg_addr[TEGRA_EMC_NUM_REGS] = {
30 0x2c,
31 0x30,
32 0x34,
33 0x38,
34 0x3c,
35 0x40,
36 0x44,
37 0x48,
38 0x4c,
39 0x50,
40 0x54,
41 0x58,
42 0x5c,
43 0x60,
44 0x64,
45 0x68,
46 0x6c,
47 0x70,
48 0x74,
49 0x78,
50 0x7c,
51 0x80,
52 0x84,
53 0x88,
54 0x8c,
55 0x90,
56 0x94,
57 0x98,
58 0x9c,
59 0xa0,
60 0xa4,
61 0xa8,
62 0xac,
63 0x114,
64 0xb0,
65 0xb4,
66 0x104,
67 0x2bc,
68 0x2c0,
69 0x2c4,
70 0x2e0,
71 0x2e4,
72 0x2a8,
73 0x2d0,
74 0x2d4,
75 0x2d8,
76};
77
78struct emc_ctlr *emc_get_controller(const void *blob)
79{
80 fdt_addr_t addr;
81 int node;
82
83 node = fdtdec_next_compatible(blob, 0, COMPAT_NVIDIA_TEGRA20_EMC);
84 if (node > 0) {
85 addr = fdtdec_get_addr(blob, node, "reg");
86 if (addr != FDT_ADDR_T_NONE)
87 return (struct emc_ctlr *)addr;
88 }
89 return NULL;
90}
91
92
93enum {
94 ERR_NO_EMC_NODE = -10,
95 ERR_NO_EMC_REG,
96 ERR_NO_FREQ,
97 ERR_FREQ_NOT_FOUND,
98 ERR_BAD_REGS,
99 ERR_NO_RAM_CODE,
100 ERR_RAM_CODE_NOT_FOUND,
101};
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117static int find_emc_tables(const void *blob, int node, int ram_code)
118{
119 int need_ram_code;
120 int depth;
121 int offset;
122
123
124 need_ram_code = fdtdec_get_bool(blob, node, "nvidia,use-ram-code");
125 if (!need_ram_code)
126 return node;
127 if (ram_code == -1) {
128 debug("%s: RAM code required but not supplied\n", __func__);
129 return ERR_NO_RAM_CODE;
130 }
131
132 offset = node;
133 depth = 0;
134 do {
135
136
137
138
139 offset = fdt_next_node(blob, offset, &depth);
140 if (depth <= 0)
141 break;
142
143
144 if (depth != 1)
145 continue;
146 if (strcmp("emc-tables", fdt_get_name(blob, offset, NULL)))
147 continue;
148
149 if (fdtdec_get_int(blob, offset, "nvidia,ram-code", -1)
150 == ram_code)
151 return offset;
152 } while (1);
153
154 debug("%s: Could not find tables for RAM code %d\n", __func__,
155 ram_code);
156 return ERR_RAM_CODE_NOT_FOUND;
157}
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172static int decode_emc(const void *blob, unsigned rate, struct emc_ctlr **emcp,
173 const u32 **tablep)
174{
175 struct apb_misc_pp_ctlr *pp =
176 (struct apb_misc_pp_ctlr *)NV_PA_APB_MISC_BASE;
177 int ram_code;
178 int depth;
179 int node;
180
181 ram_code = (readl(&pp->strapping_opt_a) & RAM_CODE_MASK)
182 >> RAM_CODE_SHIFT;
183
184
185
186
187 rate = rate / 2 / 1000;
188
189 node = fdtdec_next_compatible(blob, 0, COMPAT_NVIDIA_TEGRA20_EMC);
190 if (node < 0) {
191 debug("%s: No EMC node found in FDT\n", __func__);
192 return ERR_NO_EMC_NODE;
193 }
194 *emcp = (struct emc_ctlr *)fdtdec_get_addr(blob, node, "reg");
195 if (*emcp == (struct emc_ctlr *)FDT_ADDR_T_NONE) {
196 debug("%s: No EMC node reg property\n", __func__);
197 return ERR_NO_EMC_REG;
198 }
199
200
201 node = find_emc_tables(blob, node, ram_code & 3);
202 if (node < 0)
203 return node;
204
205 depth = 0;
206 for (;;) {
207 int node_rate;
208
209 node = fdtdec_next_compatible_subnode(blob, node,
210 COMPAT_NVIDIA_TEGRA20_EMC_TABLE, &depth);
211 if (node < 0)
212 break;
213 node_rate = fdtdec_get_int(blob, node, "clock-frequency", -1);
214 if (node_rate == -1) {
215 debug("%s: Missing clock-frequency\n", __func__);
216 return ERR_NO_FREQ;
217 }
218
219 if (node_rate == rate)
220 break;
221 }
222 if (node < 0) {
223 debug("%s: No node found for clock frequency %d\n", __func__,
224 rate);
225 return ERR_FREQ_NOT_FOUND;
226 }
227
228 *tablep = fdtdec_locate_array(blob, node, "nvidia,emc-registers",
229 TEGRA_EMC_NUM_REGS);
230 if (!*tablep) {
231 debug("%s: node '%s' array missing / wrong size\n", __func__,
232 fdt_get_name(blob, node, NULL));
233 return ERR_BAD_REGS;
234 }
235
236
237 return 0;
238}
239
240int tegra_set_emc(const void *blob, unsigned rate)
241{
242 struct emc_ctlr *emc;
243 const u32 *table = NULL;
244 int err, i;
245
246 err = decode_emc(blob, rate, &emc, &table);
247 if (err) {
248 debug("Warning: no valid EMC (%d), memory timings unset\n",
249 err);
250 return err;
251 }
252
253 debug("%s: Table found, setting EMC values as follows:\n", __func__);
254 for (i = 0; i < TEGRA_EMC_NUM_REGS; i++) {
255 u32 value = fdt32_to_cpu(table[i]);
256 u32 addr = (uintptr_t)emc + emc_reg_addr[i];
257
258 debug(" %#x: %#x\n", addr, value);
259 writel(value, addr);
260 }
261
262
263 clock_adjust_periph_pll_div(PERIPH_ID_EMC, CLOCK_ID_MEMORY,
264 clock_get_rate(CLOCK_ID_MEMORY), NULL);
265 debug("EMC clock set to %lu\n",
266 clock_get_periph_rate(PERIPH_ID_EMC, CLOCK_ID_MEMORY));
267
268 return 0;
269}
270