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