blob: 4e3a886816a4d7f62dd34bed70195579623dbeef [file] [log] [blame]
Thierry Reding89184652014-04-16 09:24:44 +02001/*
2 * Copyright (C) 2014 NVIDIA CORPORATION. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8
9#include <linux/clk.h>
10#include <linux/interrupt.h>
11#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/of.h>
14#include <linux/platform_device.h>
15#include <linux/slab.h>
Mikko Perttunen3d9dd6f2015-03-12 15:48:02 +010016#include <linux/sort.h>
17
18#include <soc/tegra/fuse.h>
Thierry Reding89184652014-04-16 09:24:44 +020019
20#include "mc.h"
21
22#define MC_INTSTATUS 0x000
23#define MC_INT_DECERR_MTS (1 << 16)
24#define MC_INT_SECERR_SEC (1 << 13)
25#define MC_INT_DECERR_VPR (1 << 12)
26#define MC_INT_INVALID_APB_ASID_UPDATE (1 << 11)
27#define MC_INT_INVALID_SMMU_PAGE (1 << 10)
28#define MC_INT_ARBITRATION_EMEM (1 << 9)
29#define MC_INT_SECURITY_VIOLATION (1 << 8)
30#define MC_INT_DECERR_EMEM (1 << 6)
31
32#define MC_INTMASK 0x004
33
34#define MC_ERR_STATUS 0x08
35#define MC_ERR_STATUS_TYPE_SHIFT 28
36#define MC_ERR_STATUS_TYPE_INVALID_SMMU_PAGE (6 << MC_ERR_STATUS_TYPE_SHIFT)
37#define MC_ERR_STATUS_TYPE_MASK (0x7 << MC_ERR_STATUS_TYPE_SHIFT)
38#define MC_ERR_STATUS_READABLE (1 << 27)
39#define MC_ERR_STATUS_WRITABLE (1 << 26)
40#define MC_ERR_STATUS_NONSECURE (1 << 25)
41#define MC_ERR_STATUS_ADR_HI_SHIFT 20
42#define MC_ERR_STATUS_ADR_HI_MASK 0x3
43#define MC_ERR_STATUS_SECURITY (1 << 17)
44#define MC_ERR_STATUS_RW (1 << 16)
45#define MC_ERR_STATUS_CLIENT_MASK 0x7f
46
47#define MC_ERR_ADR 0x0c
48
49#define MC_EMEM_ARB_CFG 0x90
50#define MC_EMEM_ARB_CFG_CYCLES_PER_UPDATE(x) (((x) & 0x1ff) << 0)
51#define MC_EMEM_ARB_CFG_CYCLES_PER_UPDATE_MASK 0x1ff
52#define MC_EMEM_ARB_MISC0 0xd8
53
Mikko Perttunen3d9dd6f2015-03-12 15:48:02 +010054#define MC_EMEM_ADR_CFG 0x54
55#define MC_EMEM_ADR_CFG_EMEM_NUMDEV BIT(0)
56
Thierry Reding89184652014-04-16 09:24:44 +020057static const struct of_device_id tegra_mc_of_match[] = {
58#ifdef CONFIG_ARCH_TEGRA_3x_SOC
59 { .compatible = "nvidia,tegra30-mc", .data = &tegra30_mc_soc },
60#endif
61#ifdef CONFIG_ARCH_TEGRA_114_SOC
62 { .compatible = "nvidia,tegra114-mc", .data = &tegra114_mc_soc },
63#endif
64#ifdef CONFIG_ARCH_TEGRA_124_SOC
65 { .compatible = "nvidia,tegra124-mc", .data = &tegra124_mc_soc },
66#endif
67 { }
68};
69MODULE_DEVICE_TABLE(of, tegra_mc_of_match);
70
71static int tegra_mc_setup_latency_allowance(struct tegra_mc *mc)
72{
73 unsigned long long tick;
74 unsigned int i;
75 u32 value;
76
77 /* compute the number of MC clock cycles per tick */
78 tick = mc->tick * clk_get_rate(mc->clk);
79 do_div(tick, NSEC_PER_SEC);
80
81 value = readl(mc->regs + MC_EMEM_ARB_CFG);
82 value &= ~MC_EMEM_ARB_CFG_CYCLES_PER_UPDATE_MASK;
83 value |= MC_EMEM_ARB_CFG_CYCLES_PER_UPDATE(tick);
84 writel(value, mc->regs + MC_EMEM_ARB_CFG);
85
86 /* write latency allowance defaults */
87 for (i = 0; i < mc->soc->num_clients; i++) {
88 const struct tegra_mc_la *la = &mc->soc->clients[i].la;
89 u32 value;
90
91 value = readl(mc->regs + la->reg);
92 value &= ~(la->mask << la->shift);
93 value |= (la->def & la->mask) << la->shift;
94 writel(value, mc->regs + la->reg);
95 }
96
97 return 0;
98}
99
Mikko Perttunen3d9dd6f2015-03-12 15:48:02 +0100100void tegra_mc_write_emem_configuration(struct tegra_mc *mc, unsigned long rate)
101{
102 unsigned int i;
103 struct tegra_mc_timing *timing = NULL;
104
105 for (i = 0; i < mc->num_timings; i++) {
106 if (mc->timings[i].rate == rate) {
107 timing = &mc->timings[i];
108 break;
109 }
110 }
111
112 if (!timing) {
113 dev_err(mc->dev, "no memory timing registered for rate %lu\n",
114 rate);
115 return;
116 }
117
118 for (i = 0; i < mc->soc->num_emem_regs; ++i)
119 mc_writel(mc, timing->emem_data[i], mc->soc->emem_regs[i]);
120}
121
122unsigned int tegra_mc_get_emem_device_count(struct tegra_mc *mc)
123{
124 u8 dram_count;
125
126 dram_count = mc_readl(mc, MC_EMEM_ADR_CFG);
127 dram_count &= MC_EMEM_ADR_CFG_EMEM_NUMDEV;
128 dram_count++;
129
130 return dram_count;
131}
132
133static int load_one_timing(struct tegra_mc *mc,
134 struct tegra_mc_timing *timing,
135 struct device_node *node)
136{
137 int err;
138 u32 tmp;
139
140 err = of_property_read_u32(node, "clock-frequency", &tmp);
141 if (err) {
142 dev_err(mc->dev,
143 "timing %s: failed to read rate\n", node->name);
144 return err;
145 }
146
147 timing->rate = tmp;
148 timing->emem_data = devm_kcalloc(mc->dev, mc->soc->num_emem_regs,
149 sizeof(u32), GFP_KERNEL);
150 if (!timing->emem_data)
151 return -ENOMEM;
152
153 err = of_property_read_u32_array(node, "nvidia,emem-configuration",
154 timing->emem_data,
155 mc->soc->num_emem_regs);
156 if (err) {
157 dev_err(mc->dev,
158 "timing %s: failed to read EMEM configuration\n",
159 node->name);
160 return err;
161 }
162
163 return 0;
164}
165
166static int load_timings(struct tegra_mc *mc, struct device_node *node)
167{
168 struct device_node *child;
169 struct tegra_mc_timing *timing;
170 int child_count = of_get_child_count(node);
171 int i = 0, err;
172
173 mc->timings = devm_kcalloc(mc->dev, child_count, sizeof(*timing),
174 GFP_KERNEL);
175 if (!mc->timings)
176 return -ENOMEM;
177
178 mc->num_timings = child_count;
179
180 for_each_child_of_node(node, child) {
181 timing = &mc->timings[i++];
182
183 err = load_one_timing(mc, timing, child);
184 if (err)
185 return err;
186 }
187
188 return 0;
189}
190
191static int tegra_mc_setup_timings(struct tegra_mc *mc)
192{
193 struct device_node *node;
194 u32 ram_code, node_ram_code;
195 int err;
196
197 ram_code = tegra_read_ram_code();
198
199 mc->num_timings = 0;
200
201 for_each_child_of_node(mc->dev->of_node, node) {
202 err = of_property_read_u32(node, "nvidia,ram-code",
203 &node_ram_code);
204 if (err || (node_ram_code != ram_code)) {
205 of_node_put(node);
206 continue;
207 }
208
209 err = load_timings(mc, node);
210 if (err)
211 return err;
212 of_node_put(node);
213 break;
214 }
215
216 if (mc->num_timings == 0)
217 dev_warn(mc->dev,
218 "no memory timings for RAM code %u registered\n",
219 ram_code);
220
221 return 0;
222}
223
Thierry Reding89184652014-04-16 09:24:44 +0200224static const char *const status_names[32] = {
225 [ 1] = "External interrupt",
226 [ 6] = "EMEM address decode error",
227 [ 8] = "Security violation",
228 [ 9] = "EMEM arbitration error",
229 [10] = "Page fault",
230 [11] = "Invalid APB ASID update",
231 [12] = "VPR violation",
232 [13] = "Secure carveout violation",
233 [16] = "MTS carveout violation",
234};
235
236static const char *const error_names[8] = {
237 [2] = "EMEM decode error",
238 [3] = "TrustZone violation",
239 [4] = "Carveout violation",
240 [6] = "SMMU translation error",
241};
242
243static irqreturn_t tegra_mc_irq(int irq, void *data)
244{
245 struct tegra_mc *mc = data;
246 unsigned long status, mask;
247 unsigned int bit;
248
249 /* mask all interrupts to avoid flooding */
250 status = mc_readl(mc, MC_INTSTATUS);
251 mask = mc_readl(mc, MC_INTMASK);
252
253 for_each_set_bit(bit, &status, 32) {
254 const char *error = status_names[bit] ?: "unknown";
255 const char *client = "unknown", *desc;
256 const char *direction, *secure;
257 phys_addr_t addr = 0;
258 unsigned int i;
259 char perm[7];
260 u8 id, type;
261 u32 value;
262
263 value = mc_readl(mc, MC_ERR_STATUS);
264
265#ifdef CONFIG_PHYS_ADDR_T_64BIT
266 if (mc->soc->num_address_bits > 32) {
267 addr = ((value >> MC_ERR_STATUS_ADR_HI_SHIFT) &
268 MC_ERR_STATUS_ADR_HI_MASK);
269 addr <<= 32;
270 }
271#endif
272
273 if (value & MC_ERR_STATUS_RW)
274 direction = "write";
275 else
276 direction = "read";
277
278 if (value & MC_ERR_STATUS_SECURITY)
279 secure = "secure ";
280 else
281 secure = "";
282
283 id = value & MC_ERR_STATUS_CLIENT_MASK;
284
285 for (i = 0; i < mc->soc->num_clients; i++) {
286 if (mc->soc->clients[i].id == id) {
287 client = mc->soc->clients[i].name;
288 break;
289 }
290 }
291
292 type = (value & MC_ERR_STATUS_TYPE_MASK) >>
293 MC_ERR_STATUS_TYPE_SHIFT;
294 desc = error_names[type];
295
296 switch (value & MC_ERR_STATUS_TYPE_MASK) {
297 case MC_ERR_STATUS_TYPE_INVALID_SMMU_PAGE:
298 perm[0] = ' ';
299 perm[1] = '[';
300
301 if (value & MC_ERR_STATUS_READABLE)
302 perm[2] = 'R';
303 else
304 perm[2] = '-';
305
306 if (value & MC_ERR_STATUS_WRITABLE)
307 perm[3] = 'W';
308 else
309 perm[3] = '-';
310
311 if (value & MC_ERR_STATUS_NONSECURE)
312 perm[4] = '-';
313 else
314 perm[4] = 'S';
315
316 perm[5] = ']';
317 perm[6] = '\0';
318 break;
319
320 default:
321 perm[0] = '\0';
322 break;
323 }
324
325 value = mc_readl(mc, MC_ERR_ADR);
326 addr |= value;
327
328 dev_err_ratelimited(mc->dev, "%s: %s%s @%pa: %s (%s%s)\n",
329 client, secure, direction, &addr, error,
330 desc, perm);
331 }
332
333 /* clear interrupts */
334 mc_writel(mc, status, MC_INTSTATUS);
335
336 return IRQ_HANDLED;
337}
338
339static int tegra_mc_probe(struct platform_device *pdev)
340{
341 const struct of_device_id *match;
342 struct resource *res;
343 struct tegra_mc *mc;
344 u32 value;
345 int err;
346
347 match = of_match_node(tegra_mc_of_match, pdev->dev.of_node);
348 if (!match)
349 return -ENODEV;
350
351 mc = devm_kzalloc(&pdev->dev, sizeof(*mc), GFP_KERNEL);
352 if (!mc)
353 return -ENOMEM;
354
355 platform_set_drvdata(pdev, mc);
356 mc->soc = match->data;
357 mc->dev = &pdev->dev;
358
359 /* length of MC tick in nanoseconds */
360 mc->tick = 30;
361
362 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
363 mc->regs = devm_ioremap_resource(&pdev->dev, res);
364 if (IS_ERR(mc->regs))
365 return PTR_ERR(mc->regs);
366
367 mc->clk = devm_clk_get(&pdev->dev, "mc");
368 if (IS_ERR(mc->clk)) {
369 dev_err(&pdev->dev, "failed to get MC clock: %ld\n",
370 PTR_ERR(mc->clk));
371 return PTR_ERR(mc->clk);
372 }
373
374 err = tegra_mc_setup_latency_allowance(mc);
375 if (err < 0) {
376 dev_err(&pdev->dev, "failed to setup latency allowance: %d\n",
377 err);
378 return err;
379 }
380
Mikko Perttunen3d9dd6f2015-03-12 15:48:02 +0100381 err = tegra_mc_setup_timings(mc);
382 if (err < 0) {
383 dev_err(&pdev->dev, "failed to setup timings: %d\n", err);
384 return err;
385 }
386
Thierry Reding89184652014-04-16 09:24:44 +0200387 if (IS_ENABLED(CONFIG_TEGRA_IOMMU_SMMU)) {
388 mc->smmu = tegra_smmu_probe(&pdev->dev, mc->soc->smmu, mc);
389 if (IS_ERR(mc->smmu)) {
390 dev_err(&pdev->dev, "failed to probe SMMU: %ld\n",
391 PTR_ERR(mc->smmu));
392 return PTR_ERR(mc->smmu);
393 }
394 }
395
396 mc->irq = platform_get_irq(pdev, 0);
397 if (mc->irq < 0) {
398 dev_err(&pdev->dev, "interrupt not specified\n");
399 return mc->irq;
400 }
401
402 err = devm_request_irq(&pdev->dev, mc->irq, tegra_mc_irq, IRQF_SHARED,
403 dev_name(&pdev->dev), mc);
404 if (err < 0) {
405 dev_err(&pdev->dev, "failed to request IRQ#%u: %d\n", mc->irq,
406 err);
407 return err;
408 }
409
410 value = MC_INT_DECERR_MTS | MC_INT_SECERR_SEC | MC_INT_DECERR_VPR |
411 MC_INT_INVALID_APB_ASID_UPDATE | MC_INT_INVALID_SMMU_PAGE |
412 MC_INT_ARBITRATION_EMEM | MC_INT_SECURITY_VIOLATION |
413 MC_INT_DECERR_EMEM;
414 mc_writel(mc, value, MC_INTMASK);
415
416 return 0;
417}
418
419static struct platform_driver tegra_mc_driver = {
420 .driver = {
421 .name = "tegra-mc",
422 .of_match_table = tegra_mc_of_match,
423 .suppress_bind_attrs = true,
424 },
425 .prevent_deferred_probe = true,
426 .probe = tegra_mc_probe,
427};
428
429static int tegra_mc_init(void)
430{
431 return platform_driver_register(&tegra_mc_driver);
432}
433arch_initcall(tegra_mc_init);
434
435MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
436MODULE_DESCRIPTION("NVIDIA Tegra Memory Controller driver");
437MODULE_LICENSE("GPL v2");