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