Linux 4.19.133
[linux/fpc-iii.git] / drivers / memory / tegra / mc.c
blob346d8eadb44b1be02f0aca879dac82b16360feeb
1 /*
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.
7 */
9 #include <linux/clk.h>
10 #include <linux/delay.h>
11 #include <linux/interrupt.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/of.h>
15 #include <linux/platform_device.h>
16 #include <linux/slab.h>
17 #include <linux/sort.h>
19 #include <soc/tegra/fuse.h>
21 #include "mc.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 },
55 #endif
56 #ifdef CONFIG_ARCH_TEGRA_3x_SOC
57 { .compatible = "nvidia,tegra30-mc", .data = &tegra30_mc_soc },
58 #endif
59 #ifdef CONFIG_ARCH_TEGRA_114_SOC
60 { .compatible = "nvidia,tegra114-mc", .data = &tegra114_mc_soc },
61 #endif
62 #ifdef CONFIG_ARCH_TEGRA_124_SOC
63 { .compatible = "nvidia,tegra124-mc", .data = &tegra124_mc_soc },
64 #endif
65 #ifdef CONFIG_ARCH_TEGRA_132_SOC
66 { .compatible = "nvidia,tegra132-mc", .data = &tegra132_mc_soc },
67 #endif
68 #ifdef CONFIG_ARCH_TEGRA_210_SOC
69 { .compatible = "nvidia,tegra210-mc", .data = &tegra210_mc_soc },
70 #endif
71 { }
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)
78 unsigned long flags;
79 u32 value;
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);
88 return 0;
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)
100 unsigned long flags;
101 u32 value;
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);
110 return 0;
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,
132 unsigned long id)
134 unsigned int i;
136 for (i = 0; i < mc->soc->num_resets; i++)
137 if (mc->soc->resets[i].id == id)
138 return &mc->soc->resets[i];
140 return NULL;
143 static int tegra_mc_hotreset_assert(struct reset_controller_dev *rcdev,
144 unsigned long id)
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;
149 int retries = 500;
150 int err;
152 rst = tegra_mc_reset_find(mc, id);
153 if (!rst)
154 return -ENODEV;
156 rst_ops = mc->soc->reset_ops;
157 if (!rst_ops)
158 return -ENODEV;
160 if (rst_ops->block_dma) {
161 /* block clients DMA requests */
162 err = rst_ops->block_dma(mc, rst);
163 if (err) {
164 dev_err(mc->dev, "Failed to block %s DMA: %d\n",
165 rst->name, err);
166 return err;
170 if (rst_ops->dma_idling) {
171 /* wait for completion of the outstanding DMA requests */
172 while (!rst_ops->dma_idling(mc, rst)) {
173 if (!retries--) {
174 dev_err(mc->dev, "Failed to flush %s DMA\n",
175 rst->name);
176 return -EBUSY;
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);
186 if (err) {
187 dev_err(mc->dev, "Failed to hot reset %s: %d\n",
188 rst->name, err);
189 return err;
193 return 0;
196 static int tegra_mc_hotreset_deassert(struct reset_controller_dev *rcdev,
197 unsigned long id)
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;
202 int err;
204 rst = tegra_mc_reset_find(mc, id);
205 if (!rst)
206 return -ENODEV;
208 rst_ops = mc->soc->reset_ops;
209 if (!rst_ops)
210 return -ENODEV;
212 if (rst_ops->hotreset_deassert) {
213 /* take out client from hot reset */
214 err = rst_ops->hotreset_deassert(mc, rst);
215 if (err) {
216 dev_err(mc->dev, "Failed to deassert hot reset %s: %d\n",
217 rst->name, err);
218 return err;
222 if (rst_ops->unblock_dma) {
223 /* allow new DMA requests to proceed to arbitration */
224 err = rst_ops->unblock_dma(mc, rst);
225 if (err) {
226 dev_err(mc->dev, "Failed to unblock %s DMA : %d\n",
227 rst->name, err);
228 return err;
232 return 0;
235 static int tegra_mc_hotreset_status(struct reset_controller_dev *rcdev,
236 unsigned long id)
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);
243 if (!rst)
244 return -ENODEV;
246 rst_ops = mc->soc->reset_ops;
247 if (!rst_ops)
248 return -ENODEV;
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)
261 int err;
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);
270 if (err < 0)
271 return err;
273 return 0;
276 static int tegra_mc_setup_latency_allowance(struct tegra_mc *mc)
278 unsigned long long tick;
279 unsigned int i;
280 u32 value;
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;
294 u32 value;
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);
302 return 0;
305 void tegra_mc_write_emem_configuration(struct tegra_mc *mc, unsigned long rate)
307 unsigned int i;
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];
313 break;
317 if (!timing) {
318 dev_err(mc->dev, "no memory timing registered for rate %lu\n",
319 rate);
320 return;
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)
329 u8 dram_count;
331 dram_count = mc_readl(mc, MC_EMEM_ADR_CFG);
332 dram_count &= MC_EMEM_ADR_CFG_EMEM_NUMDEV;
333 dram_count++;
335 return dram_count;
338 static int load_one_timing(struct tegra_mc *mc,
339 struct tegra_mc_timing *timing,
340 struct device_node *node)
342 int err;
343 u32 tmp;
345 err = of_property_read_u32(node, "clock-frequency", &tmp);
346 if (err) {
347 dev_err(mc->dev,
348 "timing %s: failed to read rate\n", node->name);
349 return err;
352 timing->rate = tmp;
353 timing->emem_data = devm_kcalloc(mc->dev, mc->soc->num_emem_regs,
354 sizeof(u32), GFP_KERNEL);
355 if (!timing->emem_data)
356 return -ENOMEM;
358 err = of_property_read_u32_array(node, "nvidia,emem-configuration",
359 timing->emem_data,
360 mc->soc->num_emem_regs);
361 if (err) {
362 dev_err(mc->dev,
363 "timing %s: failed to read EMEM configuration\n",
364 node->name);
365 return err;
368 return 0;
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);
376 int i = 0, err;
378 mc->timings = devm_kcalloc(mc->dev, child_count, sizeof(*timing),
379 GFP_KERNEL);
380 if (!mc->timings)
381 return -ENOMEM;
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);
389 if (err) {
390 of_node_put(child);
391 return err;
395 return 0;
398 static int tegra_mc_setup_timings(struct tegra_mc *mc)
400 struct device_node *node;
401 u32 ram_code, node_ram_code;
402 int err;
404 ram_code = tegra_read_ram_code();
406 mc->num_timings = 0;
408 for_each_child_of_node(mc->dev->of_node, node) {
409 err = of_property_read_u32(node, "nvidia,ram-code",
410 &node_ram_code);
411 if (err || (node_ram_code != ram_code))
412 continue;
414 err = load_timings(mc, node);
415 of_node_put(node);
416 if (err)
417 return err;
418 break;
421 if (mc->num_timings == 0)
422 dev_warn(mc->dev,
423 "no memory timings for RAM code %u registered\n",
424 ram_code);
426 return 0;
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",
435 [10] = "Page fault",
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;
453 unsigned int bit;
455 /* mask all interrupts to avoid flooding */
456 status = mc_readl(mc, MC_INTSTATUS) & mc->soc->intmask;
457 if (!status)
458 return IRQ_NONE;
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;
465 unsigned int i;
466 char perm[7];
467 u8 id, type;
468 u32 value;
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);
476 addr <<= 32;
478 #endif
480 if (value & MC_ERR_STATUS_RW)
481 direction = "write";
482 else
483 direction = "read";
485 if (value & MC_ERR_STATUS_SECURITY)
486 secure = "secure ";
487 else
488 secure = "";
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;
495 break;
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:
505 perm[0] = ' ';
506 perm[1] = '[';
508 if (value & MC_ERR_STATUS_READABLE)
509 perm[2] = 'R';
510 else
511 perm[2] = '-';
513 if (value & MC_ERR_STATUS_WRITABLE)
514 perm[3] = 'W';
515 else
516 perm[3] = '-';
518 if (value & MC_ERR_STATUS_NONSECURE)
519 perm[4] = '-';
520 else
521 perm[4] = 'S';
523 perm[5] = ']';
524 perm[6] = '\0';
525 break;
527 default:
528 perm[0] = '\0';
529 break;
532 value = mc_readl(mc, MC_ERR_ADR);
533 addr |= value;
535 dev_err_ratelimited(mc->dev, "%s: %s%s @%pa: %s (%s%s)\n",
536 client, secure, direction, &addr, error,
537 desc, perm);
540 /* clear interrupts */
541 mc_writel(mc, status, MC_INTSTATUS);
543 return IRQ_HANDLED;
546 static __maybe_unused irqreturn_t tegra20_mc_irq(int irq, void *data)
548 struct tegra_mc *mc = data;
549 unsigned long status;
550 unsigned int bit;
552 /* mask all interrupts to avoid flooding */
553 status = mc_readl(mc, MC_INTSTATUS) & mc->soc->intmask;
554 if (!status)
555 return IRQ_NONE;
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;
561 phys_addr_t addr;
562 u32 value, reg;
563 u8 id, type;
565 switch (BIT(bit)) {
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];
573 if (value & BIT(31))
574 direction = "write";
575 break;
577 case MC_INT_INVALID_GART_PAGE:
578 dev_err_ratelimited(mc->dev, "%s\n", error);
579 continue;
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];
588 secure = "secure ";
590 if (value & BIT(31))
591 direction = "write";
592 break;
594 default:
595 continue;
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,
603 desc);
606 /* clear interrupts */
607 mc_writel(mc, status, MC_INTSTATUS);
609 return IRQ_HANDLED;
612 static int tegra_mc_probe(struct platform_device *pdev)
614 const struct of_device_id *match;
615 struct resource *res;
616 struct tegra_mc *mc;
617 void *isr;
618 int err;
620 match = of_match_node(tegra_mc_of_match, pdev->dev.of_node);
621 if (!match)
622 return -ENODEV;
624 mc = devm_kzalloc(&pdev->dev, sizeof(*mc), GFP_KERNEL);
625 if (!mc)
626 return -ENOMEM;
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 */
634 mc->tick = 30;
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;
649 } else
650 #endif
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);
659 err = tegra_mc_setup_latency_allowance(mc);
660 if (err < 0) {
661 dev_err(&pdev->dev, "failed to setup latency allowance: %d\n",
662 err);
663 return err;
666 isr = tegra_mc_irq;
668 err = tegra_mc_setup_timings(mc);
669 if (err < 0) {
670 dev_err(&pdev->dev, "failed to setup timings: %d\n",
671 err);
672 return err;
676 mc->irq = platform_get_irq(pdev, 0);
677 if (mc->irq < 0) {
678 dev_err(&pdev->dev, "interrupt not specified\n");
679 return mc->irq;
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);
688 if (err < 0) {
689 dev_err(&pdev->dev, "failed to request IRQ#%u: %d\n", mc->irq,
690 err);
691 return err;
694 err = tegra_mc_reset_setup(mc);
695 if (err < 0)
696 dev_err(&pdev->dev, "failed to register reset controller: %d\n",
697 err);
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",
703 PTR_ERR(mc->smmu));
706 return 0;
709 static struct platform_driver tegra_mc_driver = {
710 .driver = {
711 .name = "tegra-mc",
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");