pmc-tegra186.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. */
  13. #define pr_fmt(fmt) "tegra-pmc: " fmt
  14. #include <linux/io.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/reboot.h>
  19. #include <asm/system_misc.h>
  20. #define PMC_CNTRL 0x000
  21. #define PMC_CNTRL_MAIN_RST BIT(4)
  22. #define PMC_RST_STATUS 0x070
  23. #define WAKE_AOWAKE_CTRL 0x4f4
  24. #define WAKE_AOWAKE_CTRL_INTR_POLARITY BIT(0)
  25. #define SCRATCH_SCRATCH0 0x2000
  26. #define SCRATCH_SCRATCH0_MODE_RECOVERY BIT(31)
  27. #define SCRATCH_SCRATCH0_MODE_BOOTLOADER BIT(30)
  28. #define SCRATCH_SCRATCH0_MODE_RCM BIT(1)
  29. #define SCRATCH_SCRATCH0_MODE_MASK (SCRATCH_SCRATCH0_MODE_RECOVERY | \
  30. SCRATCH_SCRATCH0_MODE_BOOTLOADER | \
  31. SCRATCH_SCRATCH0_MODE_RCM)
  32. struct tegra_pmc {
  33. struct device *dev;
  34. void __iomem *regs;
  35. void __iomem *wake;
  36. void __iomem *aotag;
  37. void __iomem *scratch;
  38. void (*system_restart)(enum reboot_mode mode, const char *cmd);
  39. struct notifier_block restart;
  40. };
  41. static int tegra186_pmc_restart_notify(struct notifier_block *nb,
  42. unsigned long action,
  43. void *data)
  44. {
  45. struct tegra_pmc *pmc = container_of(nb, struct tegra_pmc, restart);
  46. const char *cmd = data;
  47. u32 value;
  48. value = readl(pmc->scratch + SCRATCH_SCRATCH0);
  49. value &= ~SCRATCH_SCRATCH0_MODE_MASK;
  50. if (cmd) {
  51. if (strcmp(cmd, "recovery") == 0)
  52. value |= SCRATCH_SCRATCH0_MODE_RECOVERY;
  53. if (strcmp(cmd, "bootloader") == 0)
  54. value |= SCRATCH_SCRATCH0_MODE_BOOTLOADER;
  55. if (strcmp(cmd, "forced-recovery") == 0)
  56. value |= SCRATCH_SCRATCH0_MODE_RCM;
  57. }
  58. writel(value, pmc->scratch + SCRATCH_SCRATCH0);
  59. /*
  60. * If available, call the system restart implementation that was
  61. * registered earlier (typically PSCI).
  62. */
  63. if (pmc->system_restart) {
  64. pmc->system_restart(reboot_mode, cmd);
  65. return NOTIFY_DONE;
  66. }
  67. /* reset everything but SCRATCH0_SCRATCH0 and PMC_RST_STATUS */
  68. value = readl(pmc->regs + PMC_CNTRL);
  69. value |= PMC_CNTRL_MAIN_RST;
  70. writel(value, pmc->regs + PMC_CNTRL);
  71. return NOTIFY_DONE;
  72. }
  73. static int tegra186_pmc_setup(struct tegra_pmc *pmc)
  74. {
  75. struct device_node *np = pmc->dev->of_node;
  76. bool invert;
  77. u32 value;
  78. invert = of_property_read_bool(np, "nvidia,invert-interrupt");
  79. value = readl(pmc->wake + WAKE_AOWAKE_CTRL);
  80. if (invert)
  81. value |= WAKE_AOWAKE_CTRL_INTR_POLARITY;
  82. else
  83. value &= ~WAKE_AOWAKE_CTRL_INTR_POLARITY;
  84. writel(value, pmc->wake + WAKE_AOWAKE_CTRL);
  85. /*
  86. * We need to hook any system restart implementation registered
  87. * previously so we can write SCRATCH_SCRATCH0 before reset.
  88. */
  89. pmc->system_restart = arm_pm_restart;
  90. arm_pm_restart = NULL;
  91. pmc->restart.notifier_call = tegra186_pmc_restart_notify;
  92. pmc->restart.priority = 128;
  93. return register_restart_handler(&pmc->restart);
  94. }
  95. static int tegra186_pmc_probe(struct platform_device *pdev)
  96. {
  97. struct tegra_pmc *pmc;
  98. struct resource *res;
  99. pmc = devm_kzalloc(&pdev->dev, sizeof(*pmc), GFP_KERNEL);
  100. if (!pmc)
  101. return -ENOMEM;
  102. pmc->dev = &pdev->dev;
  103. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pmc");
  104. pmc->regs = devm_ioremap_resource(&pdev->dev, res);
  105. if (IS_ERR(pmc->regs))
  106. return PTR_ERR(pmc->regs);
  107. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "wake");
  108. pmc->wake = devm_ioremap_resource(&pdev->dev, res);
  109. if (IS_ERR(pmc->wake))
  110. return PTR_ERR(pmc->wake);
  111. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "aotag");
  112. pmc->aotag = devm_ioremap_resource(&pdev->dev, res);
  113. if (IS_ERR(pmc->aotag))
  114. return PTR_ERR(pmc->aotag);
  115. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "scratch");
  116. pmc->scratch = devm_ioremap_resource(&pdev->dev, res);
  117. if (IS_ERR(pmc->scratch))
  118. return PTR_ERR(pmc->scratch);
  119. return tegra186_pmc_setup(pmc);
  120. }
  121. static const struct of_device_id tegra186_pmc_of_match[] = {
  122. { .compatible = "nvidia,tegra186-pmc" },
  123. { /* sentinel */ }
  124. };
  125. MODULE_DEVICE_TABLE(of, tegra186_pmc_of_match);
  126. static struct platform_driver tegra186_pmc_driver = {
  127. .driver = {
  128. .name = "tegra186-pmc",
  129. .of_match_table = tegra186_pmc_of_match,
  130. },
  131. .probe = tegra186_pmc_probe,
  132. };
  133. builtin_platform_driver(tegra186_pmc_driver);