.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* |
---|
2 | 3 | * 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 | 4 | */ |
---|
8 | 5 | |
---|
9 | 6 | #include <linux/clk.h> |
---|
10 | 7 | #include <linux/delay.h> |
---|
| 8 | +#include <linux/dma-mapping.h> |
---|
11 | 9 | #include <linux/interrupt.h> |
---|
12 | 10 | #include <linux/kernel.h> |
---|
13 | 11 | #include <linux/module.h> |
---|
14 | 12 | #include <linux/of.h> |
---|
| 13 | +#include <linux/of_device.h> |
---|
15 | 14 | #include <linux/platform_device.h> |
---|
16 | 15 | #include <linux/slab.h> |
---|
17 | 16 | #include <linux/sort.h> |
---|
.. | .. |
---|
20 | 19 | |
---|
21 | 20 | #include "mc.h" |
---|
22 | 21 | |
---|
23 | | -#define MC_INTSTATUS 0x000 |
---|
24 | | - |
---|
25 | | -#define MC_INTMASK 0x004 |
---|
26 | | - |
---|
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) |
---|
38 | | - |
---|
39 | | -#define MC_ERR_ADR 0x0c |
---|
40 | | - |
---|
41 | | -#define MC_DECERR_EMEM_OTHERS_STATUS 0x58 |
---|
42 | | -#define MC_SECURITY_VIOLATION_STATUS 0x74 |
---|
43 | | - |
---|
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 |
---|
48 | | - |
---|
49 | | -#define MC_EMEM_ADR_CFG 0x54 |
---|
50 | | -#define MC_EMEM_ADR_CFG_EMEM_NUMDEV BIT(0) |
---|
51 | | - |
---|
52 | 22 | static const struct of_device_id tegra_mc_of_match[] = { |
---|
53 | 23 | #ifdef CONFIG_ARCH_TEGRA_2x_SOC |
---|
54 | | - { .compatible = "nvidia,tegra20-mc", .data = &tegra20_mc_soc }, |
---|
| 24 | + { .compatible = "nvidia,tegra20-mc-gart", .data = &tegra20_mc_soc }, |
---|
55 | 25 | #endif |
---|
56 | 26 | #ifdef CONFIG_ARCH_TEGRA_3x_SOC |
---|
57 | 27 | { .compatible = "nvidia,tegra30-mc", .data = &tegra30_mc_soc }, |
---|
.. | .. |
---|
72 | 42 | }; |
---|
73 | 43 | MODULE_DEVICE_TABLE(of, tegra_mc_of_match); |
---|
74 | 44 | |
---|
75 | | -static int terga_mc_block_dma_common(struct tegra_mc *mc, |
---|
| 45 | +static int tegra_mc_block_dma_common(struct tegra_mc *mc, |
---|
76 | 46 | const struct tegra_mc_reset *rst) |
---|
77 | 47 | { |
---|
78 | 48 | unsigned long flags; |
---|
.. | .. |
---|
88 | 58 | return 0; |
---|
89 | 59 | } |
---|
90 | 60 | |
---|
91 | | -static bool terga_mc_dma_idling_common(struct tegra_mc *mc, |
---|
| 61 | +static bool tegra_mc_dma_idling_common(struct tegra_mc *mc, |
---|
92 | 62 | const struct tegra_mc_reset *rst) |
---|
93 | 63 | { |
---|
94 | 64 | return (mc_readl(mc, rst->status) & BIT(rst->bit)) != 0; |
---|
95 | 65 | } |
---|
96 | 66 | |
---|
97 | | -static int terga_mc_unblock_dma_common(struct tegra_mc *mc, |
---|
| 67 | +static int tegra_mc_unblock_dma_common(struct tegra_mc *mc, |
---|
98 | 68 | const struct tegra_mc_reset *rst) |
---|
99 | 69 | { |
---|
100 | 70 | unsigned long flags; |
---|
.. | .. |
---|
110 | 80 | return 0; |
---|
111 | 81 | } |
---|
112 | 82 | |
---|
113 | | -static int terga_mc_reset_status_common(struct tegra_mc *mc, |
---|
| 83 | +static int tegra_mc_reset_status_common(struct tegra_mc *mc, |
---|
114 | 84 | const struct tegra_mc_reset *rst) |
---|
115 | 85 | { |
---|
116 | 86 | return (mc_readl(mc, rst->control) & BIT(rst->bit)) != 0; |
---|
117 | 87 | } |
---|
118 | 88 | |
---|
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, |
---|
| 89 | +const struct tegra_mc_reset_ops tegra_mc_reset_ops_common = { |
---|
| 90 | + .block_dma = tegra_mc_block_dma_common, |
---|
| 91 | + .dma_idling = tegra_mc_dma_idling_common, |
---|
| 92 | + .unblock_dma = tegra_mc_unblock_dma_common, |
---|
| 93 | + .reset_status = tegra_mc_reset_status_common, |
---|
124 | 94 | }; |
---|
125 | 95 | |
---|
126 | 96 | static inline struct tegra_mc *reset_to_mc(struct reset_controller_dev *rcdev) |
---|
.. | .. |
---|
161 | 131 | /* block clients DMA requests */ |
---|
162 | 132 | err = rst_ops->block_dma(mc, rst); |
---|
163 | 133 | if (err) { |
---|
164 | | - dev_err(mc->dev, "Failed to block %s DMA: %d\n", |
---|
| 134 | + dev_err(mc->dev, "failed to block %s DMA: %d\n", |
---|
165 | 135 | rst->name, err); |
---|
166 | 136 | return err; |
---|
167 | 137 | } |
---|
.. | .. |
---|
171 | 141 | /* wait for completion of the outstanding DMA requests */ |
---|
172 | 142 | while (!rst_ops->dma_idling(mc, rst)) { |
---|
173 | 143 | if (!retries--) { |
---|
174 | | - dev_err(mc->dev, "Failed to flush %s DMA\n", |
---|
| 144 | + dev_err(mc->dev, "failed to flush %s DMA\n", |
---|
175 | 145 | rst->name); |
---|
176 | 146 | return -EBUSY; |
---|
177 | 147 | } |
---|
.. | .. |
---|
184 | 154 | /* clear clients DMA requests sitting before arbitration */ |
---|
185 | 155 | err = rst_ops->hotreset_assert(mc, rst); |
---|
186 | 156 | if (err) { |
---|
187 | | - dev_err(mc->dev, "Failed to hot reset %s: %d\n", |
---|
| 157 | + dev_err(mc->dev, "failed to hot reset %s: %d\n", |
---|
188 | 158 | rst->name, err); |
---|
189 | 159 | return err; |
---|
190 | 160 | } |
---|
.. | .. |
---|
213 | 183 | /* take out client from hot reset */ |
---|
214 | 184 | err = rst_ops->hotreset_deassert(mc, rst); |
---|
215 | 185 | if (err) { |
---|
216 | | - dev_err(mc->dev, "Failed to deassert hot reset %s: %d\n", |
---|
| 186 | + dev_err(mc->dev, "failed to deassert hot reset %s: %d\n", |
---|
217 | 187 | rst->name, err); |
---|
218 | 188 | return err; |
---|
219 | 189 | } |
---|
.. | .. |
---|
223 | 193 | /* allow new DMA requests to proceed to arbitration */ |
---|
224 | 194 | err = rst_ops->unblock_dma(mc, rst); |
---|
225 | 195 | if (err) { |
---|
226 | | - dev_err(mc->dev, "Failed to unblock %s DMA : %d\n", |
---|
| 196 | + dev_err(mc->dev, "failed to unblock %s DMA : %d\n", |
---|
227 | 197 | rst->name, err); |
---|
228 | 198 | return err; |
---|
229 | 199 | } |
---|
.. | .. |
---|
283 | 253 | tick = (unsigned long long)mc->tick * clk_get_rate(mc->clk); |
---|
284 | 254 | do_div(tick, NSEC_PER_SEC); |
---|
285 | 255 | |
---|
286 | | - value = readl(mc->regs + MC_EMEM_ARB_CFG); |
---|
| 256 | + value = mc_readl(mc, MC_EMEM_ARB_CFG); |
---|
287 | 257 | value &= ~MC_EMEM_ARB_CFG_CYCLES_PER_UPDATE_MASK; |
---|
288 | 258 | value |= MC_EMEM_ARB_CFG_CYCLES_PER_UPDATE(tick); |
---|
289 | | - writel(value, mc->regs + MC_EMEM_ARB_CFG); |
---|
| 259 | + mc_writel(mc, value, MC_EMEM_ARB_CFG); |
---|
290 | 260 | |
---|
291 | 261 | /* write latency allowance defaults */ |
---|
292 | 262 | for (i = 0; i < mc->soc->num_clients; i++) { |
---|
293 | 263 | const struct tegra_mc_la *la = &mc->soc->clients[i].la; |
---|
294 | 264 | u32 value; |
---|
295 | 265 | |
---|
296 | | - value = readl(mc->regs + la->reg); |
---|
| 266 | + value = mc_readl(mc, la->reg); |
---|
297 | 267 | value &= ~(la->mask << la->shift); |
---|
298 | 268 | value |= (la->def & la->mask) << la->shift; |
---|
299 | | - writel(value, mc->regs + la->reg); |
---|
| 269 | + mc_writel(mc, value, la->reg); |
---|
300 | 270 | } |
---|
| 271 | + |
---|
| 272 | + /* latch new values */ |
---|
| 273 | + mc_writel(mc, MC_TIMING_UPDATE, MC_TIMING_CONTROL); |
---|
301 | 274 | |
---|
302 | 275 | return 0; |
---|
303 | 276 | } |
---|
304 | 277 | |
---|
305 | | -void tegra_mc_write_emem_configuration(struct tegra_mc *mc, unsigned long rate) |
---|
| 278 | +int tegra_mc_write_emem_configuration(struct tegra_mc *mc, unsigned long rate) |
---|
306 | 279 | { |
---|
307 | 280 | unsigned int i; |
---|
308 | 281 | struct tegra_mc_timing *timing = NULL; |
---|
.. | .. |
---|
317 | 290 | if (!timing) { |
---|
318 | 291 | dev_err(mc->dev, "no memory timing registered for rate %lu\n", |
---|
319 | 292 | rate); |
---|
320 | | - return; |
---|
| 293 | + return -EINVAL; |
---|
321 | 294 | } |
---|
322 | 295 | |
---|
323 | 296 | for (i = 0; i < mc->soc->num_emem_regs; ++i) |
---|
324 | 297 | mc_writel(mc, timing->emem_data[i], mc->soc->emem_regs[i]); |
---|
| 298 | + |
---|
| 299 | + return 0; |
---|
325 | 300 | } |
---|
326 | 301 | |
---|
327 | 302 | unsigned int tegra_mc_get_emem_device_count(struct tegra_mc *mc) |
---|
.. | .. |
---|
345 | 320 | err = of_property_read_u32(node, "clock-frequency", &tmp); |
---|
346 | 321 | if (err) { |
---|
347 | 322 | dev_err(mc->dev, |
---|
348 | | - "timing %s: failed to read rate\n", node->name); |
---|
| 323 | + "timing %pOFn: failed to read rate\n", node); |
---|
349 | 324 | return err; |
---|
350 | 325 | } |
---|
351 | 326 | |
---|
.. | .. |
---|
360 | 335 | mc->soc->num_emem_regs); |
---|
361 | 336 | if (err) { |
---|
362 | 337 | dev_err(mc->dev, |
---|
363 | | - "timing %s: failed to read EMEM configuration\n", |
---|
364 | | - node->name); |
---|
| 338 | + "timing %pOFn: failed to read EMEM configuration\n", |
---|
| 339 | + node); |
---|
365 | 340 | return err; |
---|
366 | 341 | } |
---|
367 | 342 | |
---|
.. | .. |
---|
575 | 550 | break; |
---|
576 | 551 | |
---|
577 | 552 | case MC_INT_INVALID_GART_PAGE: |
---|
578 | | - dev_err_ratelimited(mc->dev, "%s\n", error); |
---|
579 | | - continue; |
---|
| 553 | + reg = MC_GART_ERROR_REQ; |
---|
| 554 | + value = mc_readl(mc, reg); |
---|
| 555 | + |
---|
| 556 | + id = (value >> 1) & mc->soc->client_id_mask; |
---|
| 557 | + desc = error_names[2]; |
---|
| 558 | + |
---|
| 559 | + if (value & BIT(0)) |
---|
| 560 | + direction = "write"; |
---|
| 561 | + break; |
---|
580 | 562 | |
---|
581 | 563 | case MC_INT_SECURITY_VIOLATION: |
---|
582 | 564 | reg = MC_SECURITY_VIOLATION_STATUS; |
---|
.. | .. |
---|
611 | 593 | |
---|
612 | 594 | static int tegra_mc_probe(struct platform_device *pdev) |
---|
613 | 595 | { |
---|
614 | | - const struct of_device_id *match; |
---|
615 | 596 | struct resource *res; |
---|
616 | 597 | struct tegra_mc *mc; |
---|
617 | 598 | void *isr; |
---|
| 599 | + u64 mask; |
---|
618 | 600 | int err; |
---|
619 | | - |
---|
620 | | - match = of_match_node(tegra_mc_of_match, pdev->dev.of_node); |
---|
621 | | - if (!match) |
---|
622 | | - return -ENODEV; |
---|
623 | 601 | |
---|
624 | 602 | mc = devm_kzalloc(&pdev->dev, sizeof(*mc), GFP_KERNEL); |
---|
625 | 603 | if (!mc) |
---|
.. | .. |
---|
627 | 605 | |
---|
628 | 606 | platform_set_drvdata(pdev, mc); |
---|
629 | 607 | spin_lock_init(&mc->lock); |
---|
630 | | - mc->soc = match->data; |
---|
| 608 | + mc->soc = of_device_get_match_data(&pdev->dev); |
---|
631 | 609 | mc->dev = &pdev->dev; |
---|
| 610 | + |
---|
| 611 | + mask = DMA_BIT_MASK(mc->soc->num_address_bits); |
---|
| 612 | + |
---|
| 613 | + err = dma_coerce_mask_and_coherent(&pdev->dev, mask); |
---|
| 614 | + if (err < 0) { |
---|
| 615 | + dev_err(&pdev->dev, "failed to set DMA mask: %d\n", err); |
---|
| 616 | + return err; |
---|
| 617 | + } |
---|
632 | 618 | |
---|
633 | 619 | /* length of MC tick in nanoseconds */ |
---|
634 | 620 | mc->tick = 30; |
---|
.. | .. |
---|
638 | 624 | if (IS_ERR(mc->regs)) |
---|
639 | 625 | return PTR_ERR(mc->regs); |
---|
640 | 626 | |
---|
| 627 | + mc->clk = devm_clk_get(&pdev->dev, "mc"); |
---|
| 628 | + if (IS_ERR(mc->clk)) { |
---|
| 629 | + dev_err(&pdev->dev, "failed to get MC clock: %ld\n", |
---|
| 630 | + PTR_ERR(mc->clk)); |
---|
| 631 | + return PTR_ERR(mc->clk); |
---|
| 632 | + } |
---|
| 633 | + |
---|
641 | 634 | #ifdef CONFIG_ARCH_TEGRA_2x_SOC |
---|
642 | 635 | 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); |
---|
647 | | - |
---|
648 | 636 | isr = tegra20_mc_irq; |
---|
649 | 637 | } else |
---|
650 | 638 | #endif |
---|
651 | 639 | { |
---|
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", |
---|
655 | | - PTR_ERR(mc->clk)); |
---|
656 | | - return PTR_ERR(mc->clk); |
---|
657 | | - } |
---|
| 640 | + /* ensure that debug features are disabled */ |
---|
| 641 | + mc_writel(mc, 0x00000000, MC_TIMING_CONTROL_DBG); |
---|
658 | 642 | |
---|
659 | 643 | err = tegra_mc_setup_latency_allowance(mc); |
---|
660 | 644 | if (err < 0) { |
---|
661 | | - dev_err(&pdev->dev, "failed to setup latency allowance: %d\n", |
---|
| 645 | + dev_err(&pdev->dev, |
---|
| 646 | + "failed to setup latency allowance: %d\n", |
---|
662 | 647 | err); |
---|
663 | 648 | return err; |
---|
664 | 649 | } |
---|
.. | .. |
---|
679 | 664 | return mc->irq; |
---|
680 | 665 | } |
---|
681 | 666 | |
---|
682 | | - WARN(!mc->soc->client_id_mask, "Missing client ID mask for this SoC\n"); |
---|
| 667 | + WARN(!mc->soc->client_id_mask, "missing client ID mask for this SoC\n"); |
---|
683 | 668 | |
---|
684 | 669 | mc_writel(mc, mc->soc->intmask, MC_INTMASK); |
---|
685 | 670 | |
---|
686 | | - err = devm_request_irq(&pdev->dev, mc->irq, isr, IRQF_SHARED, |
---|
| 671 | + err = devm_request_irq(&pdev->dev, mc->irq, isr, 0, |
---|
687 | 672 | dev_name(&pdev->dev), mc); |
---|
688 | 673 | if (err < 0) { |
---|
689 | 674 | dev_err(&pdev->dev, "failed to request IRQ#%u: %d\n", mc->irq, |
---|
.. | .. |
---|
696 | 681 | dev_err(&pdev->dev, "failed to register reset controller: %d\n", |
---|
697 | 682 | err); |
---|
698 | 683 | |
---|
699 | | - if (IS_ENABLED(CONFIG_TEGRA_IOMMU_SMMU)) { |
---|
| 684 | + if (IS_ENABLED(CONFIG_TEGRA_IOMMU_SMMU) && mc->soc->smmu) { |
---|
700 | 685 | mc->smmu = tegra_smmu_probe(&pdev->dev, mc->soc->smmu, mc); |
---|
701 | | - if (IS_ERR(mc->smmu)) |
---|
| 686 | + if (IS_ERR(mc->smmu)) { |
---|
702 | 687 | dev_err(&pdev->dev, "failed to probe SMMU: %ld\n", |
---|
703 | 688 | PTR_ERR(mc->smmu)); |
---|
| 689 | + mc->smmu = NULL; |
---|
| 690 | + } |
---|
| 691 | + } |
---|
| 692 | + |
---|
| 693 | + if (IS_ENABLED(CONFIG_TEGRA_IOMMU_GART) && !mc->soc->smmu) { |
---|
| 694 | + mc->gart = tegra_gart_probe(&pdev->dev, mc); |
---|
| 695 | + if (IS_ERR(mc->gart)) { |
---|
| 696 | + dev_err(&pdev->dev, "failed to probe GART: %ld\n", |
---|
| 697 | + PTR_ERR(mc->gart)); |
---|
| 698 | + mc->gart = NULL; |
---|
| 699 | + } |
---|
704 | 700 | } |
---|
705 | 701 | |
---|
706 | 702 | return 0; |
---|
707 | 703 | } |
---|
708 | 704 | |
---|
| 705 | +static int tegra_mc_suspend(struct device *dev) |
---|
| 706 | +{ |
---|
| 707 | + struct tegra_mc *mc = dev_get_drvdata(dev); |
---|
| 708 | + int err; |
---|
| 709 | + |
---|
| 710 | + if (IS_ENABLED(CONFIG_TEGRA_IOMMU_GART) && mc->gart) { |
---|
| 711 | + err = tegra_gart_suspend(mc->gart); |
---|
| 712 | + if (err) |
---|
| 713 | + return err; |
---|
| 714 | + } |
---|
| 715 | + |
---|
| 716 | + return 0; |
---|
| 717 | +} |
---|
| 718 | + |
---|
| 719 | +static int tegra_mc_resume(struct device *dev) |
---|
| 720 | +{ |
---|
| 721 | + struct tegra_mc *mc = dev_get_drvdata(dev); |
---|
| 722 | + int err; |
---|
| 723 | + |
---|
| 724 | + if (IS_ENABLED(CONFIG_TEGRA_IOMMU_GART) && mc->gart) { |
---|
| 725 | + err = tegra_gart_resume(mc->gart); |
---|
| 726 | + if (err) |
---|
| 727 | + return err; |
---|
| 728 | + } |
---|
| 729 | + |
---|
| 730 | + return 0; |
---|
| 731 | +} |
---|
| 732 | + |
---|
| 733 | +static const struct dev_pm_ops tegra_mc_pm_ops = { |
---|
| 734 | + .suspend = tegra_mc_suspend, |
---|
| 735 | + .resume = tegra_mc_resume, |
---|
| 736 | +}; |
---|
| 737 | + |
---|
709 | 738 | static struct platform_driver tegra_mc_driver = { |
---|
710 | 739 | .driver = { |
---|
711 | 740 | .name = "tegra-mc", |
---|
712 | 741 | .of_match_table = tegra_mc_of_match, |
---|
| 742 | + .pm = &tegra_mc_pm_ops, |
---|
713 | 743 | .suppress_bind_attrs = true, |
---|
714 | 744 | }, |
---|
715 | 745 | .prevent_deferred_probe = true, |
---|