2 * Copyright (C) 2014 NVIDIA CORPORATION. All rights reserved.
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.
10 #include <linux/delay.h>
11 #include <linux/interrupt.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/slab.h>
17 #include <linux/sort.h>
19 #include <soc/tegra/fuse.h>
23 #define MC_INTSTATUS 0x000
25 #define MC_INTMASK 0x004
27 #define MC_ERR_STATUS 0x08
28 #define MC_ERR_STATUS_TYPE_SHIFT 28
29 #define MC_ERR_STATUS_TYPE_INVALID_SMMU_PAGE (6 << MC_ERR_STATUS_TYPE_SHIFT)
30 #define MC_ERR_STATUS_TYPE_MASK (0x7 << MC_ERR_STATUS_TYPE_SHIFT)
31 #define MC_ERR_STATUS_READABLE (1 << 27)
32 #define MC_ERR_STATUS_WRITABLE (1 << 26)
33 #define MC_ERR_STATUS_NONSECURE (1 << 25)
34 #define MC_ERR_STATUS_ADR_HI_SHIFT 20
35 #define MC_ERR_STATUS_ADR_HI_MASK 0x3
36 #define MC_ERR_STATUS_SECURITY (1 << 17)
37 #define MC_ERR_STATUS_RW (1 << 16)
39 #define MC_ERR_ADR 0x0c
41 #define MC_DECERR_EMEM_OTHERS_STATUS 0x58
42 #define MC_SECURITY_VIOLATION_STATUS 0x74
44 #define MC_EMEM_ARB_CFG 0x90
45 #define MC_EMEM_ARB_CFG_CYCLES_PER_UPDATE(x) (((x) & 0x1ff) << 0)
46 #define MC_EMEM_ARB_CFG_CYCLES_PER_UPDATE_MASK 0x1ff
47 #define MC_EMEM_ARB_MISC0 0xd8
49 #define MC_EMEM_ADR_CFG 0x54
50 #define MC_EMEM_ADR_CFG_EMEM_NUMDEV BIT(0)
52 static const struct of_device_id tegra_mc_of_match
[] = {
53 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
54 { .compatible
= "nvidia,tegra20-mc", .data
= &tegra20_mc_soc
},
56 #ifdef CONFIG_ARCH_TEGRA_3x_SOC
57 { .compatible
= "nvidia,tegra30-mc", .data
= &tegra30_mc_soc
},
59 #ifdef CONFIG_ARCH_TEGRA_114_SOC
60 { .compatible
= "nvidia,tegra114-mc", .data
= &tegra114_mc_soc
},
62 #ifdef CONFIG_ARCH_TEGRA_124_SOC
63 { .compatible
= "nvidia,tegra124-mc", .data
= &tegra124_mc_soc
},
65 #ifdef CONFIG_ARCH_TEGRA_132_SOC
66 { .compatible
= "nvidia,tegra132-mc", .data
= &tegra132_mc_soc
},
68 #ifdef CONFIG_ARCH_TEGRA_210_SOC
69 { .compatible
= "nvidia,tegra210-mc", .data
= &tegra210_mc_soc
},
73 MODULE_DEVICE_TABLE(of
, tegra_mc_of_match
);
75 static int terga_mc_block_dma_common(struct tegra_mc
*mc
,
76 const struct tegra_mc_reset
*rst
)
81 spin_lock_irqsave(&mc
->lock
, flags
);
83 value
= mc_readl(mc
, rst
->control
) | BIT(rst
->bit
);
84 mc_writel(mc
, value
, rst
->control
);
86 spin_unlock_irqrestore(&mc
->lock
, flags
);
91 static bool terga_mc_dma_idling_common(struct tegra_mc
*mc
,
92 const struct tegra_mc_reset
*rst
)
94 return (mc_readl(mc
, rst
->status
) & BIT(rst
->bit
)) != 0;
97 static int terga_mc_unblock_dma_common(struct tegra_mc
*mc
,
98 const struct tegra_mc_reset
*rst
)
103 spin_lock_irqsave(&mc
->lock
, flags
);
105 value
= mc_readl(mc
, rst
->control
) & ~BIT(rst
->bit
);
106 mc_writel(mc
, value
, rst
->control
);
108 spin_unlock_irqrestore(&mc
->lock
, flags
);
113 static int terga_mc_reset_status_common(struct tegra_mc
*mc
,
114 const struct tegra_mc_reset
*rst
)
116 return (mc_readl(mc
, rst
->control
) & BIT(rst
->bit
)) != 0;
119 const struct tegra_mc_reset_ops terga_mc_reset_ops_common
= {
120 .block_dma
= terga_mc_block_dma_common
,
121 .dma_idling
= terga_mc_dma_idling_common
,
122 .unblock_dma
= terga_mc_unblock_dma_common
,
123 .reset_status
= terga_mc_reset_status_common
,
126 static inline struct tegra_mc
*reset_to_mc(struct reset_controller_dev
*rcdev
)
128 return container_of(rcdev
, struct tegra_mc
, reset
);
131 static const struct tegra_mc_reset
*tegra_mc_reset_find(struct tegra_mc
*mc
,
136 for (i
= 0; i
< mc
->soc
->num_resets
; i
++)
137 if (mc
->soc
->resets
[i
].id
== id
)
138 return &mc
->soc
->resets
[i
];
143 static int tegra_mc_hotreset_assert(struct reset_controller_dev
*rcdev
,
146 struct tegra_mc
*mc
= reset_to_mc(rcdev
);
147 const struct tegra_mc_reset_ops
*rst_ops
;
148 const struct tegra_mc_reset
*rst
;
152 rst
= tegra_mc_reset_find(mc
, id
);
156 rst_ops
= mc
->soc
->reset_ops
;
160 if (rst_ops
->block_dma
) {
161 /* block clients DMA requests */
162 err
= rst_ops
->block_dma(mc
, rst
);
164 dev_err(mc
->dev
, "Failed to block %s DMA: %d\n",
170 if (rst_ops
->dma_idling
) {
171 /* wait for completion of the outstanding DMA requests */
172 while (!rst_ops
->dma_idling(mc
, rst
)) {
174 dev_err(mc
->dev
, "Failed to flush %s DMA\n",
179 usleep_range(10, 100);
183 if (rst_ops
->hotreset_assert
) {
184 /* clear clients DMA requests sitting before arbitration */
185 err
= rst_ops
->hotreset_assert(mc
, rst
);
187 dev_err(mc
->dev
, "Failed to hot reset %s: %d\n",
196 static int tegra_mc_hotreset_deassert(struct reset_controller_dev
*rcdev
,
199 struct tegra_mc
*mc
= reset_to_mc(rcdev
);
200 const struct tegra_mc_reset_ops
*rst_ops
;
201 const struct tegra_mc_reset
*rst
;
204 rst
= tegra_mc_reset_find(mc
, id
);
208 rst_ops
= mc
->soc
->reset_ops
;
212 if (rst_ops
->hotreset_deassert
) {
213 /* take out client from hot reset */
214 err
= rst_ops
->hotreset_deassert(mc
, rst
);
216 dev_err(mc
->dev
, "Failed to deassert hot reset %s: %d\n",
222 if (rst_ops
->unblock_dma
) {
223 /* allow new DMA requests to proceed to arbitration */
224 err
= rst_ops
->unblock_dma(mc
, rst
);
226 dev_err(mc
->dev
, "Failed to unblock %s DMA : %d\n",
235 static int tegra_mc_hotreset_status(struct reset_controller_dev
*rcdev
,
238 struct tegra_mc
*mc
= reset_to_mc(rcdev
);
239 const struct tegra_mc_reset_ops
*rst_ops
;
240 const struct tegra_mc_reset
*rst
;
242 rst
= tegra_mc_reset_find(mc
, id
);
246 rst_ops
= mc
->soc
->reset_ops
;
250 return rst_ops
->reset_status(mc
, rst
);
253 static const struct reset_control_ops tegra_mc_reset_ops
= {
254 .assert = tegra_mc_hotreset_assert
,
255 .deassert
= tegra_mc_hotreset_deassert
,
256 .status
= tegra_mc_hotreset_status
,
259 static int tegra_mc_reset_setup(struct tegra_mc
*mc
)
263 mc
->reset
.ops
= &tegra_mc_reset_ops
;
264 mc
->reset
.owner
= THIS_MODULE
;
265 mc
->reset
.of_node
= mc
->dev
->of_node
;
266 mc
->reset
.of_reset_n_cells
= 1;
267 mc
->reset
.nr_resets
= mc
->soc
->num_resets
;
269 err
= reset_controller_register(&mc
->reset
);
276 static int tegra_mc_setup_latency_allowance(struct tegra_mc
*mc
)
278 unsigned long long tick
;
282 /* compute the number of MC clock cycles per tick */
283 tick
= (unsigned long long)mc
->tick
* clk_get_rate(mc
->clk
);
284 do_div(tick
, NSEC_PER_SEC
);
286 value
= readl(mc
->regs
+ MC_EMEM_ARB_CFG
);
287 value
&= ~MC_EMEM_ARB_CFG_CYCLES_PER_UPDATE_MASK
;
288 value
|= MC_EMEM_ARB_CFG_CYCLES_PER_UPDATE(tick
);
289 writel(value
, mc
->regs
+ MC_EMEM_ARB_CFG
);
291 /* write latency allowance defaults */
292 for (i
= 0; i
< mc
->soc
->num_clients
; i
++) {
293 const struct tegra_mc_la
*la
= &mc
->soc
->clients
[i
].la
;
296 value
= readl(mc
->regs
+ la
->reg
);
297 value
&= ~(la
->mask
<< la
->shift
);
298 value
|= (la
->def
& la
->mask
) << la
->shift
;
299 writel(value
, mc
->regs
+ la
->reg
);
305 void tegra_mc_write_emem_configuration(struct tegra_mc
*mc
, unsigned long rate
)
308 struct tegra_mc_timing
*timing
= NULL
;
310 for (i
= 0; i
< mc
->num_timings
; i
++) {
311 if (mc
->timings
[i
].rate
== rate
) {
312 timing
= &mc
->timings
[i
];
318 dev_err(mc
->dev
, "no memory timing registered for rate %lu\n",
323 for (i
= 0; i
< mc
->soc
->num_emem_regs
; ++i
)
324 mc_writel(mc
, timing
->emem_data
[i
], mc
->soc
->emem_regs
[i
]);
327 unsigned int tegra_mc_get_emem_device_count(struct tegra_mc
*mc
)
331 dram_count
= mc_readl(mc
, MC_EMEM_ADR_CFG
);
332 dram_count
&= MC_EMEM_ADR_CFG_EMEM_NUMDEV
;
338 static int load_one_timing(struct tegra_mc
*mc
,
339 struct tegra_mc_timing
*timing
,
340 struct device_node
*node
)
345 err
= of_property_read_u32(node
, "clock-frequency", &tmp
);
348 "timing %s: failed to read rate\n", node
->name
);
353 timing
->emem_data
= devm_kcalloc(mc
->dev
, mc
->soc
->num_emem_regs
,
354 sizeof(u32
), GFP_KERNEL
);
355 if (!timing
->emem_data
)
358 err
= of_property_read_u32_array(node
, "nvidia,emem-configuration",
360 mc
->soc
->num_emem_regs
);
363 "timing %s: failed to read EMEM configuration\n",
371 static int load_timings(struct tegra_mc
*mc
, struct device_node
*node
)
373 struct device_node
*child
;
374 struct tegra_mc_timing
*timing
;
375 int child_count
= of_get_child_count(node
);
378 mc
->timings
= devm_kcalloc(mc
->dev
, child_count
, sizeof(*timing
),
383 mc
->num_timings
= child_count
;
385 for_each_child_of_node(node
, child
) {
386 timing
= &mc
->timings
[i
++];
388 err
= load_one_timing(mc
, timing
, child
);
398 static int tegra_mc_setup_timings(struct tegra_mc
*mc
)
400 struct device_node
*node
;
401 u32 ram_code
, node_ram_code
;
404 ram_code
= tegra_read_ram_code();
408 for_each_child_of_node(mc
->dev
->of_node
, node
) {
409 err
= of_property_read_u32(node
, "nvidia,ram-code",
411 if (err
|| (node_ram_code
!= ram_code
))
414 err
= load_timings(mc
, node
);
421 if (mc
->num_timings
== 0)
423 "no memory timings for RAM code %u registered\n",
429 static const char *const status_names
[32] = {
430 [ 1] = "External interrupt",
431 [ 6] = "EMEM address decode error",
432 [ 7] = "GART page fault",
433 [ 8] = "Security violation",
434 [ 9] = "EMEM arbitration error",
436 [11] = "Invalid APB ASID update",
437 [12] = "VPR violation",
438 [13] = "Secure carveout violation",
439 [16] = "MTS carveout violation",
442 static const char *const error_names
[8] = {
443 [2] = "EMEM decode error",
444 [3] = "TrustZone violation",
445 [4] = "Carveout violation",
446 [6] = "SMMU translation error",
449 static irqreturn_t
tegra_mc_irq(int irq
, void *data
)
451 struct tegra_mc
*mc
= data
;
452 unsigned long status
;
455 /* mask all interrupts to avoid flooding */
456 status
= mc_readl(mc
, MC_INTSTATUS
) & mc
->soc
->intmask
;
460 for_each_set_bit(bit
, &status
, 32) {
461 const char *error
= status_names
[bit
] ?: "unknown";
462 const char *client
= "unknown", *desc
;
463 const char *direction
, *secure
;
464 phys_addr_t addr
= 0;
470 value
= mc_readl(mc
, MC_ERR_STATUS
);
472 #ifdef CONFIG_PHYS_ADDR_T_64BIT
473 if (mc
->soc
->num_address_bits
> 32) {
474 addr
= ((value
>> MC_ERR_STATUS_ADR_HI_SHIFT
) &
475 MC_ERR_STATUS_ADR_HI_MASK
);
480 if (value
& MC_ERR_STATUS_RW
)
485 if (value
& MC_ERR_STATUS_SECURITY
)
490 id
= value
& mc
->soc
->client_id_mask
;
492 for (i
= 0; i
< mc
->soc
->num_clients
; i
++) {
493 if (mc
->soc
->clients
[i
].id
== id
) {
494 client
= mc
->soc
->clients
[i
].name
;
499 type
= (value
& MC_ERR_STATUS_TYPE_MASK
) >>
500 MC_ERR_STATUS_TYPE_SHIFT
;
501 desc
= error_names
[type
];
503 switch (value
& MC_ERR_STATUS_TYPE_MASK
) {
504 case MC_ERR_STATUS_TYPE_INVALID_SMMU_PAGE
:
508 if (value
& MC_ERR_STATUS_READABLE
)
513 if (value
& MC_ERR_STATUS_WRITABLE
)
518 if (value
& MC_ERR_STATUS_NONSECURE
)
532 value
= mc_readl(mc
, MC_ERR_ADR
);
535 dev_err_ratelimited(mc
->dev
, "%s: %s%s @%pa: %s (%s%s)\n",
536 client
, secure
, direction
, &addr
, error
,
540 /* clear interrupts */
541 mc_writel(mc
, status
, MC_INTSTATUS
);
546 static __maybe_unused irqreturn_t
tegra20_mc_irq(int irq
, void *data
)
548 struct tegra_mc
*mc
= data
;
549 unsigned long status
;
552 /* mask all interrupts to avoid flooding */
553 status
= mc_readl(mc
, MC_INTSTATUS
) & mc
->soc
->intmask
;
557 for_each_set_bit(bit
, &status
, 32) {
558 const char *direction
= "read", *secure
= "";
559 const char *error
= status_names
[bit
];
560 const char *client
, *desc
;
566 case MC_INT_DECERR_EMEM
:
567 reg
= MC_DECERR_EMEM_OTHERS_STATUS
;
568 value
= mc_readl(mc
, reg
);
570 id
= value
& mc
->soc
->client_id_mask
;
571 desc
= error_names
[2];
577 case MC_INT_INVALID_GART_PAGE
:
578 dev_err_ratelimited(mc
->dev
, "%s\n", error
);
581 case MC_INT_SECURITY_VIOLATION
:
582 reg
= MC_SECURITY_VIOLATION_STATUS
;
583 value
= mc_readl(mc
, reg
);
585 id
= value
& mc
->soc
->client_id_mask
;
586 type
= (value
& BIT(30)) ? 4 : 3;
587 desc
= error_names
[type
];
598 client
= mc
->soc
->clients
[id
].name
;
599 addr
= mc_readl(mc
, reg
+ sizeof(u32
));
601 dev_err_ratelimited(mc
->dev
, "%s: %s%s @%pa: %s (%s)\n",
602 client
, secure
, direction
, &addr
, error
,
606 /* clear interrupts */
607 mc_writel(mc
, status
, MC_INTSTATUS
);
612 static int tegra_mc_probe(struct platform_device
*pdev
)
614 const struct of_device_id
*match
;
615 struct resource
*res
;
620 match
= of_match_node(tegra_mc_of_match
, pdev
->dev
.of_node
);
624 mc
= devm_kzalloc(&pdev
->dev
, sizeof(*mc
), GFP_KERNEL
);
628 platform_set_drvdata(pdev
, mc
);
629 spin_lock_init(&mc
->lock
);
630 mc
->soc
= match
->data
;
631 mc
->dev
= &pdev
->dev
;
633 /* length of MC tick in nanoseconds */
636 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
637 mc
->regs
= devm_ioremap_resource(&pdev
->dev
, res
);
638 if (IS_ERR(mc
->regs
))
639 return PTR_ERR(mc
->regs
);
641 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
642 if (mc
->soc
== &tegra20_mc_soc
) {
643 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
644 mc
->regs2
= devm_ioremap_resource(&pdev
->dev
, res
);
645 if (IS_ERR(mc
->regs2
))
646 return PTR_ERR(mc
->regs2
);
648 isr
= tegra20_mc_irq
;
652 mc
->clk
= devm_clk_get(&pdev
->dev
, "mc");
653 if (IS_ERR(mc
->clk
)) {
654 dev_err(&pdev
->dev
, "failed to get MC clock: %ld\n",
656 return PTR_ERR(mc
->clk
);
659 err
= tegra_mc_setup_latency_allowance(mc
);
661 dev_err(&pdev
->dev
, "failed to setup latency allowance: %d\n",
668 err
= tegra_mc_setup_timings(mc
);
670 dev_err(&pdev
->dev
, "failed to setup timings: %d\n",
676 mc
->irq
= platform_get_irq(pdev
, 0);
678 dev_err(&pdev
->dev
, "interrupt not specified\n");
682 WARN(!mc
->soc
->client_id_mask
, "Missing client ID mask for this SoC\n");
684 mc_writel(mc
, mc
->soc
->intmask
, MC_INTMASK
);
686 err
= devm_request_irq(&pdev
->dev
, mc
->irq
, isr
, IRQF_SHARED
,
687 dev_name(&pdev
->dev
), mc
);
689 dev_err(&pdev
->dev
, "failed to request IRQ#%u: %d\n", mc
->irq
,
694 err
= tegra_mc_reset_setup(mc
);
696 dev_err(&pdev
->dev
, "failed to register reset controller: %d\n",
699 if (IS_ENABLED(CONFIG_TEGRA_IOMMU_SMMU
)) {
700 mc
->smmu
= tegra_smmu_probe(&pdev
->dev
, mc
->soc
->smmu
, mc
);
701 if (IS_ERR(mc
->smmu
))
702 dev_err(&pdev
->dev
, "failed to probe SMMU: %ld\n",
709 static struct platform_driver tegra_mc_driver
= {
712 .of_match_table
= tegra_mc_of_match
,
713 .suppress_bind_attrs
= true,
715 .prevent_deferred_probe
= true,
716 .probe
= tegra_mc_probe
,
719 static int tegra_mc_init(void)
721 return platform_driver_register(&tegra_mc_driver
);
723 arch_initcall(tegra_mc_init
);
725 MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
726 MODULE_DESCRIPTION("NVIDIA Tegra Memory Controller driver");
727 MODULE_LICENSE("GPL v2");