tegra210-emc-table.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
  4. */
  5. #include <linux/of_reserved_mem.h>
  6. #include "tegra210-emc.h"
  7. #define TEGRA_EMC_MAX_FREQS 16
  8. static int tegra210_emc_table_device_init(struct reserved_mem *rmem,
  9. struct device *dev)
  10. {
  11. struct tegra210_emc *emc = dev_get_drvdata(dev);
  12. struct tegra210_emc_timing *timings;
  13. unsigned int i, count = 0;
  14. timings = memremap(rmem->base, rmem->size, MEMREMAP_WB);
  15. if (!timings) {
  16. dev_err(dev, "failed to map EMC table\n");
  17. return -ENOMEM;
  18. }
  19. for (i = 0; i < TEGRA_EMC_MAX_FREQS; i++) {
  20. if (timings[i].revision == 0)
  21. break;
  22. count++;
  23. }
  24. /* only the nominal and derated tables are expected */
  25. if (emc->derated) {
  26. dev_warn(dev, "excess EMC table '%s'\n", rmem->name);
  27. goto out;
  28. }
  29. if (emc->nominal) {
  30. if (count != emc->num_timings) {
  31. dev_warn(dev, "%u derated vs. %u nominal entries\n",
  32. count, emc->num_timings);
  33. memunmap(timings);
  34. return -EINVAL;
  35. }
  36. emc->derated = timings;
  37. } else {
  38. emc->num_timings = count;
  39. emc->nominal = timings;
  40. }
  41. out:
  42. /* keep track of which table this is */
  43. rmem->priv = timings;
  44. return 0;
  45. }
  46. static void tegra210_emc_table_device_release(struct reserved_mem *rmem,
  47. struct device *dev)
  48. {
  49. struct tegra210_emc_timing *timings = rmem->priv;
  50. struct tegra210_emc *emc = dev_get_drvdata(dev);
  51. if ((emc->nominal && timings != emc->nominal) &&
  52. (emc->derated && timings != emc->derated))
  53. dev_warn(dev, "trying to release unassigned EMC table '%s'\n",
  54. rmem->name);
  55. memunmap(timings);
  56. }
  57. static const struct reserved_mem_ops tegra210_emc_table_ops = {
  58. .device_init = tegra210_emc_table_device_init,
  59. .device_release = tegra210_emc_table_device_release,
  60. };
  61. static int tegra210_emc_table_init(struct reserved_mem *rmem)
  62. {
  63. pr_debug("Tegra210 EMC table at %pa, size %lu bytes\n", &rmem->base,
  64. (unsigned long)rmem->size);
  65. rmem->ops = &tegra210_emc_table_ops;
  66. return 0;
  67. }
  68. RESERVEDMEM_OF_DECLARE(tegra210_emc_table, "nvidia,tegra210-emc-table",
  69. tegra210_emc_table_init);