2
* Tegra30 Memory Controller
4
* Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
6
* This program is free software; you can redistribute it and/or modify it
7
* under the terms and conditions of the GNU General Public License,
8
* version 2, as published by the Free Software Foundation.
10
* This program is distributed in the hope it will be useful, but WITHOUT
11
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15
* You should have received a copy of the GNU General Public License along with
16
* this program; if not, write to the Free Software Foundation, Inc.,
17
* 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
20
#include <linux/kernel.h>
21
#include <linux/module.h>
22
#include <linux/ratelimit.h>
23
#include <linux/platform_device.h>
24
#include <linux/interrupt.h>
27
#define DRV_NAME "tegra30-mc"
29
#define MC_INTSTATUS 0x0
30
#define MC_INTMASK 0x4
32
#define MC_INT_ERR_SHIFT 6
33
#define MC_INT_ERR_MASK (0x1f << MC_INT_ERR_SHIFT)
34
#define MC_INT_DECERR_EMEM BIT(MC_INT_ERR_SHIFT)
35
#define MC_INT_SECURITY_VIOLATION BIT(MC_INT_ERR_SHIFT + 2)
36
#define MC_INT_ARBITRATION_EMEM BIT(MC_INT_ERR_SHIFT + 3)
37
#define MC_INT_INVALID_SMMU_PAGE BIT(MC_INT_ERR_SHIFT + 4)
39
#define MC_ERR_STATUS 0x8
40
#define MC_ERR_ADR 0xc
42
#define MC_ERR_TYPE_SHIFT 28
43
#define MC_ERR_TYPE_MASK (7 << MC_ERR_TYPE_SHIFT)
44
#define MC_ERR_TYPE_DECERR_EMEM 2
45
#define MC_ERR_TYPE_SECURITY_TRUSTZONE 3
46
#define MC_ERR_TYPE_SECURITY_CARVEOUT 4
47
#define MC_ERR_TYPE_INVALID_SMMU_PAGE 6
49
#define MC_ERR_INVALID_SMMU_PAGE_SHIFT 25
50
#define MC_ERR_INVALID_SMMU_PAGE_MASK (7 << MC_ERR_INVALID_SMMU_PAGE_SHIFT)
51
#define MC_ERR_RW_SHIFT 16
52
#define MC_ERR_RW BIT(MC_ERR_RW_SHIFT)
53
#define MC_ERR_SECURITY BIT(MC_ERR_RW_SHIFT + 1)
55
#define SECURITY_VIOLATION_TYPE BIT(30) /* 0=TRUSTZONE, 1=CARVEOUT */
57
#define MC_EMEM_ARB_CFG 0x90
58
#define MC_EMEM_ARB_OUTSTANDING_REQ 0x94
59
#define MC_EMEM_ARB_TIMING_RCD 0x98
60
#define MC_EMEM_ARB_TIMING_RP 0x9c
61
#define MC_EMEM_ARB_TIMING_RC 0xa0
62
#define MC_EMEM_ARB_TIMING_RAS 0xa4
63
#define MC_EMEM_ARB_TIMING_FAW 0xa8
64
#define MC_EMEM_ARB_TIMING_RRD 0xac
65
#define MC_EMEM_ARB_TIMING_RAP2PRE 0xb0
66
#define MC_EMEM_ARB_TIMING_WAP2PRE 0xb4
67
#define MC_EMEM_ARB_TIMING_R2R 0xb8
68
#define MC_EMEM_ARB_TIMING_W2W 0xbc
69
#define MC_EMEM_ARB_TIMING_R2W 0xc0
70
#define MC_EMEM_ARB_TIMING_W2R 0xc4
72
#define MC_EMEM_ARB_DA_TURNS 0xd0
73
#define MC_EMEM_ARB_DA_COVERS 0xd4
74
#define MC_EMEM_ARB_MISC0 0xd8
75
#define MC_EMEM_ARB_MISC1 0xdc
77
#define MC_EMEM_ARB_RING3_THROTTLE 0xe4
78
#define MC_EMEM_ARB_OVERRIDE 0xe8
80
#define MC_TIMING_CONTROL 0xfc
82
#define MC_CLIENT_ID_MASK 0x7f
84
#define NUM_MC_REG_BANKS 4
87
void __iomem *regs[NUM_MC_REG_BANKS];
92
static inline u32 mc_readl(struct tegra30_mc *mc, u32 offs)
97
val = readl(mc->regs[0] + offs);
99
val = readl(mc->regs[1] + offs - 0x3c);
101
val = readl(mc->regs[2] + offs - 0x200);
103
val = readl(mc->regs[3] + offs - 0x284);
108
static inline void mc_writel(struct tegra30_mc *mc, u32 val, u32 offs)
111
writel(val, mc->regs[0] + offs);
115
writel(val, mc->regs[1] + offs - 0x3c);
119
writel(val, mc->regs[2] + offs - 0x200);
123
writel(val, mc->regs[3] + offs - 0x284);
128
static const char * const tegra30_mc_client[] = {
197
static void tegra30_mc_decode(struct tegra30_mc *mc, int n)
200
const char * const mc_int_err[] = {
204
"MC_ARBITRATION_EMEM",
207
const char * const err_type[] = {
211
"SECURITY_TRUSTZONE",
218
int cid, perm, type, idx;
219
const char *client = "Unknown";
221
idx = n - MC_INT_ERR_SHIFT;
222
if ((idx < 0) || (idx >= ARRAY_SIZE(mc_int_err)) || (idx == 1)) {
223
dev_err_ratelimited(mc->dev, "Unknown interrupt status %08lx\n",
228
err = readl(mc + MC_ERR_STATUS);
230
type = (err & MC_ERR_TYPE_MASK) >> MC_ERR_TYPE_SHIFT;
231
perm = (err & MC_ERR_INVALID_SMMU_PAGE_MASK) >>
232
MC_ERR_INVALID_SMMU_PAGE_SHIFT;
233
if (type == MC_ERR_TYPE_INVALID_SMMU_PAGE)
234
sprintf(attr, "%c-%c-%c",
235
(perm & BIT(2)) ? 'R' : '-',
236
(perm & BIT(1)) ? 'W' : '-',
237
(perm & BIT(0)) ? 'S' : '-');
241
cid = err & MC_CLIENT_ID_MASK;
242
if (cid < ARRAY_SIZE(tegra30_mc_client))
243
client = tegra30_mc_client[cid];
245
addr = readl(mc + MC_ERR_ADR);
247
dev_err_ratelimited(mc->dev, "%s (0x%08x): 0x%08x %s (%s %s %s %s)\n",
248
mc_int_err[idx], err, addr, client,
249
(err & MC_ERR_SECURITY) ? "secure" : "non-secure",
250
(err & MC_ERR_RW) ? "write" : "read",
251
err_type[type], attr);
254
static const u32 tegra30_mc_ctx[] = {
256
MC_EMEM_ARB_OUTSTANDING_REQ,
257
MC_EMEM_ARB_TIMING_RCD,
258
MC_EMEM_ARB_TIMING_RP,
259
MC_EMEM_ARB_TIMING_RC,
260
MC_EMEM_ARB_TIMING_RAS,
261
MC_EMEM_ARB_TIMING_FAW,
262
MC_EMEM_ARB_TIMING_RRD,
263
MC_EMEM_ARB_TIMING_RAP2PRE,
264
MC_EMEM_ARB_TIMING_WAP2PRE,
265
MC_EMEM_ARB_TIMING_R2R,
266
MC_EMEM_ARB_TIMING_W2W,
267
MC_EMEM_ARB_TIMING_R2W,
268
MC_EMEM_ARB_TIMING_W2R,
269
MC_EMEM_ARB_DA_TURNS,
270
MC_EMEM_ARB_DA_COVERS,
273
MC_EMEM_ARB_RING3_THROTTLE,
274
MC_EMEM_ARB_OVERRIDE,
278
static int tegra30_mc_suspend(struct device *dev)
281
struct tegra30_mc *mc = dev_get_drvdata(dev);
283
for (i = 0; i < ARRAY_SIZE(tegra30_mc_ctx); i++)
284
mc->ctx[i] = mc_readl(mc, tegra30_mc_ctx[i]);
288
static int tegra30_mc_resume(struct device *dev)
291
struct tegra30_mc *mc = dev_get_drvdata(dev);
293
for (i = 0; i < ARRAY_SIZE(tegra30_mc_ctx); i++)
294
mc_writel(mc, mc->ctx[i], tegra30_mc_ctx[i]);
296
mc_writel(mc, 1, MC_TIMING_CONTROL);
297
/* Read-back to ensure that write reached */
298
mc_readl(mc, MC_TIMING_CONTROL);
302
static UNIVERSAL_DEV_PM_OPS(tegra30_mc_pm,
304
tegra30_mc_resume, NULL);
306
static const struct of_device_id tegra30_mc_of_match[] __devinitconst = {
307
{ .compatible = "nvidia,tegra30-mc", },
311
static irqreturn_t tegra30_mc_isr(int irq, void *data)
314
struct tegra30_mc *mc = data;
316
stat = mc_readl(mc, MC_INTSTATUS);
317
mask = mc_readl(mc, MC_INTMASK);
321
while ((bit = ffs(mask)) != 0)
322
tegra30_mc_decode(mc, bit - 1);
323
mc_writel(mc, stat, MC_INTSTATUS);
327
static int __devinit tegra30_mc_probe(struct platform_device *pdev)
329
struct resource *irq;
330
struct tegra30_mc *mc;
335
bytes = sizeof(*mc) + sizeof(u32) * ARRAY_SIZE(tegra30_mc_ctx);
336
mc = devm_kzalloc(&pdev->dev, bytes, GFP_KERNEL);
339
mc->dev = &pdev->dev;
341
for (i = 0; i < ARRAY_SIZE(mc->regs); i++) {
342
struct resource *res;
344
res = platform_get_resource(pdev, IORESOURCE_MEM, i);
347
mc->regs[i] = devm_request_and_ioremap(&pdev->dev, res);
352
irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
355
err = devm_request_irq(&pdev->dev, irq->start, tegra30_mc_isr,
356
IRQF_SHARED, dev_name(&pdev->dev), mc);
360
platform_set_drvdata(pdev, mc);
362
intmask = MC_INT_INVALID_SMMU_PAGE |
363
MC_INT_DECERR_EMEM | MC_INT_SECURITY_VIOLATION;
364
mc_writel(mc, intmask, MC_INTMASK);
368
static struct platform_driver tegra30_mc_driver = {
369
.probe = tegra30_mc_probe,
372
.owner = THIS_MODULE,
373
.of_match_table = tegra30_mc_of_match,
374
.pm = &tegra30_mc_pm,
377
module_platform_driver(tegra30_mc_driver);
379
MODULE_AUTHOR("Hiroshi DOYU <hdoyu@nvidia.com>");
380
MODULE_DESCRIPTION("Tegra30 MC driver");
381
MODULE_LICENSE("GPL v2");
382
MODULE_ALIAS("platform:" DRV_NAME);