Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (c) 2020, NVIDIA CORPORATION.  All rights reserved.
0004  */
0005 
0006 #include <linux/of_reserved_mem.h>
0007 
0008 #include "tegra210-emc.h"
0009 
0010 #define TEGRA_EMC_MAX_FREQS     16
0011 
0012 static int tegra210_emc_table_device_init(struct reserved_mem *rmem,
0013                       struct device *dev)
0014 {
0015     struct tegra210_emc *emc = dev_get_drvdata(dev);
0016     struct tegra210_emc_timing *timings;
0017     unsigned int i, count = 0;
0018 
0019     timings = memremap(rmem->base, rmem->size, MEMREMAP_WB);
0020     if (!timings) {
0021         dev_err(dev, "failed to map EMC table\n");
0022         return -ENOMEM;
0023     }
0024 
0025     count = 0;
0026 
0027     for (i = 0; i < TEGRA_EMC_MAX_FREQS; i++) {
0028         if (timings[i].revision == 0)
0029             break;
0030 
0031         count++;
0032     }
0033 
0034     /* only the nominal and derated tables are expected */
0035     if (emc->derated) {
0036         dev_warn(dev, "excess EMC table '%s'\n", rmem->name);
0037         goto out;
0038     }
0039 
0040     if (emc->nominal) {
0041         if (count != emc->num_timings) {
0042             dev_warn(dev, "%u derated vs. %u nominal entries\n",
0043                  count, emc->num_timings);
0044             memunmap(timings);
0045             return -EINVAL;
0046         }
0047 
0048         emc->derated = timings;
0049     } else {
0050         emc->num_timings = count;
0051         emc->nominal = timings;
0052     }
0053 
0054 out:
0055     /* keep track of which table this is */
0056     rmem->priv = timings;
0057 
0058     return 0;
0059 }
0060 
0061 static void tegra210_emc_table_device_release(struct reserved_mem *rmem,
0062                           struct device *dev)
0063 {
0064     struct tegra210_emc_timing *timings = rmem->priv;
0065     struct tegra210_emc *emc = dev_get_drvdata(dev);
0066 
0067     if ((emc->nominal && timings != emc->nominal) &&
0068         (emc->derated && timings != emc->derated))
0069         dev_warn(dev, "trying to release unassigned EMC table '%s'\n",
0070              rmem->name);
0071 
0072     memunmap(timings);
0073 }
0074 
0075 static const struct reserved_mem_ops tegra210_emc_table_ops = {
0076     .device_init = tegra210_emc_table_device_init,
0077     .device_release = tegra210_emc_table_device_release,
0078 };
0079 
0080 static int tegra210_emc_table_init(struct reserved_mem *rmem)
0081 {
0082     pr_debug("Tegra210 EMC table at %pa, size %lu bytes\n", &rmem->base,
0083          (unsigned long)rmem->size);
0084 
0085     rmem->ops = &tegra210_emc_table_ops;
0086 
0087     return 0;
0088 }
0089 RESERVEDMEM_OF_DECLARE(tegra210_emc_table, "nvidia,tegra210-emc-table",
0090                tegra210_emc_table_init);