2 * drivers/soc/tegra/pmc.c
4 * Copyright (c) 2010 Google, Inc
7 * Colin Cross <ccross@google.com>
9 * This software is licensed under the terms of the GNU General Public
10 * License version 2, as published by the Free Software Foundation, and
11 * may be copied, distributed, and modified under those terms.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
20 #define pr_fmt(fmt) "tegra-pmc: " fmt
22 #include <linux/kernel.h>
23 #include <linux/clk.h>
24 #include <linux/clk/tegra.h>
25 #include <linux/debugfs.h>
26 #include <linux/delay.h>
27 #include <linux/err.h>
28 #include <linux/export.h>
29 #include <linux/init.h>
31 #include <linux/iopoll.h>
33 #include <linux/of_address.h>
34 #include <linux/of_clk.h>
35 #include <linux/of_platform.h>
36 #include <linux/pinctrl/pinctrl.h>
37 #include <linux/pinctrl/pinconf.h>
38 #include <linux/pinctrl/pinconf-generic.h>
39 #include <linux/platform_device.h>
40 #include <linux/pm_domain.h>
41 #include <linux/reboot.h>
42 #include <linux/reset.h>
43 #include <linux/seq_file.h>
44 #include <linux/slab.h>
45 #include <linux/spinlock.h>
47 #include <soc/tegra/common.h>
48 #include <soc/tegra/fuse.h>
49 #include <soc/tegra/pmc.h>
51 #include <dt-bindings/pinctrl/pinctrl-tegra-io-pad.h>
54 #define PMC_CNTRL_INTR_POLARITY BIT(17) /* inverts INTR polarity */
55 #define PMC_CNTRL_CPU_PWRREQ_OE BIT(16) /* CPU pwr req enable */
56 #define PMC_CNTRL_CPU_PWRREQ_POLARITY BIT(15) /* CPU pwr req polarity */
57 #define PMC_CNTRL_SIDE_EFFECT_LP0 BIT(14) /* LP0 when CPU pwr gated */
58 #define PMC_CNTRL_SYSCLK_OE BIT(11) /* system clock enable */
59 #define PMC_CNTRL_SYSCLK_POLARITY BIT(10) /* sys clk polarity */
60 #define PMC_CNTRL_MAIN_RST BIT(4)
62 #define DPD_SAMPLE 0x020
63 #define DPD_SAMPLE_ENABLE BIT(0)
64 #define DPD_SAMPLE_DISABLE (0 << 0)
66 #define PWRGATE_TOGGLE 0x30
67 #define PWRGATE_TOGGLE_START BIT(8)
69 #define REMOVE_CLAMPING 0x34
71 #define PWRGATE_STATUS 0x38
73 #define PMC_IMPL_E_33V_PWR 0x40
75 #define PMC_PWR_DET 0x48
77 #define PMC_SCRATCH0_MODE_RECOVERY BIT(31)
78 #define PMC_SCRATCH0_MODE_BOOTLOADER BIT(30)
79 #define PMC_SCRATCH0_MODE_RCM BIT(1)
80 #define PMC_SCRATCH0_MODE_MASK (PMC_SCRATCH0_MODE_RECOVERY | \
81 PMC_SCRATCH0_MODE_BOOTLOADER | \
82 PMC_SCRATCH0_MODE_RCM)
84 #define PMC_CPUPWRGOOD_TIMER 0xc8
85 #define PMC_CPUPWROFF_TIMER 0xcc
87 #define PMC_PWR_DET_VALUE 0xe4
89 #define PMC_SCRATCH41 0x140
91 #define PMC_SENSOR_CTRL 0x1b0
92 #define PMC_SENSOR_CTRL_SCRATCH_WRITE BIT(2)
93 #define PMC_SENSOR_CTRL_ENABLE_RST BIT(1)
95 #define PMC_RST_STATUS 0x1b4
96 #define PMC_RST_STATUS_POR 0
97 #define PMC_RST_STATUS_WATCHDOG 1
98 #define PMC_RST_STATUS_SENSOR 2
99 #define PMC_RST_STATUS_SW_MAIN 3
100 #define PMC_RST_STATUS_LP0 4
101 #define PMC_RST_STATUS_AOTAG 5
103 #define IO_DPD_REQ 0x1b8
104 #define IO_DPD_REQ_CODE_IDLE (0U << 30)
105 #define IO_DPD_REQ_CODE_OFF (1U << 30)
106 #define IO_DPD_REQ_CODE_ON (2U << 30)
107 #define IO_DPD_REQ_CODE_MASK (3U << 30)
109 #define IO_DPD_STATUS 0x1bc
110 #define IO_DPD2_REQ 0x1c0
111 #define IO_DPD2_STATUS 0x1c4
112 #define SEL_DPD_TIM 0x1c8
114 #define PMC_SCRATCH54 0x258
115 #define PMC_SCRATCH54_DATA_SHIFT 8
116 #define PMC_SCRATCH54_ADDR_SHIFT 0
118 #define PMC_SCRATCH55 0x25c
119 #define PMC_SCRATCH55_RESET_TEGRA BIT(31)
120 #define PMC_SCRATCH55_CNTRL_ID_SHIFT 27
121 #define PMC_SCRATCH55_PINMUX_SHIFT 24
122 #define PMC_SCRATCH55_16BITOP BIT(15)
123 #define PMC_SCRATCH55_CHECKSUM_SHIFT 16
124 #define PMC_SCRATCH55_I2CSLV1_SHIFT 0
126 #define GPU_RG_CNTRL 0x2d4
128 /* Tegra186 and later */
129 #define WAKE_AOWAKE_CTRL 0x4f4
130 #define WAKE_AOWAKE_CTRL_INTR_POLARITY BIT(0)
132 struct tegra_powergate {
133 struct generic_pm_domain genpd;
134 struct tegra_pmc *pmc;
137 unsigned int num_clks;
138 struct reset_control *reset;
141 struct tegra_io_pad_soc {
142 enum tegra_io_pad id;
144 unsigned int voltage;
148 struct tegra_pmc_regs {
149 unsigned int scratch0;
150 unsigned int dpd_req;
151 unsigned int dpd_status;
152 unsigned int dpd2_req;
153 unsigned int dpd2_status;
156 struct tegra_pmc_soc {
157 unsigned int num_powergates;
158 const char *const *powergates;
159 unsigned int num_cpu_powergates;
160 const u8 *cpu_powergates;
162 bool has_tsense_reset;
164 bool needs_mbist_war;
165 bool has_impl_33v_pwr;
167 const struct tegra_io_pad_soc *io_pads;
168 unsigned int num_io_pads;
170 const struct pinctrl_pin_desc *pin_descs;
171 unsigned int num_pin_descs;
173 const struct tegra_pmc_regs *regs;
174 void (*init)(struct tegra_pmc *pmc);
175 void (*setup_irq_polarity)(struct tegra_pmc *pmc,
176 struct device_node *np,
181 * struct tegra_pmc - NVIDIA Tegra PMC
182 * @dev: pointer to PMC device structure
183 * @base: pointer to I/O remapped register region
184 * @clk: pointer to pclk clock
185 * @soc: pointer to SoC data structure
186 * @debugfs: pointer to debugfs entry
187 * @rate: currently configured rate of pclk
188 * @suspend_mode: lowest suspend mode available
189 * @cpu_good_time: CPU power good time (in microseconds)
190 * @cpu_off_time: CPU power off time (in microsecends)
191 * @core_osc_time: core power good OSC time (in microseconds)
192 * @core_pmu_time: core power good PMU time (in microseconds)
193 * @core_off_time: core power off time (in microseconds)
194 * @corereq_high: core power request is active-high
195 * @sysclkreq_high: system clock request is active-high
196 * @combined_req: combined power request for CPU & core
197 * @cpu_pwr_good_en: CPU power good signal is enabled
198 * @lp0_vec_phys: physical base address of the LP0 warm boot code
199 * @lp0_vec_size: size of the LP0 warm boot code
200 * @powergates_available: Bitmap of available power gates
201 * @powergates_lock: mutex for power gate register access
208 void __iomem *scratch;
210 struct dentry *debugfs;
212 const struct tegra_pmc_soc *soc;
216 enum tegra_suspend_mode suspend_mode;
225 bool cpu_pwr_good_en;
228 DECLARE_BITMAP(powergates_available, TEGRA_POWERGATE_MAX);
230 struct mutex powergates_lock;
232 struct pinctrl_dev *pctl_dev;
235 static struct tegra_pmc *pmc = &(struct tegra_pmc) {
237 .suspend_mode = TEGRA_SUSPEND_NONE,
240 static inline struct tegra_powergate *
241 to_powergate(struct generic_pm_domain *domain)
243 return container_of(domain, struct tegra_powergate, genpd);
246 static u32 tegra_pmc_readl(unsigned long offset)
248 return readl(pmc->base + offset);
251 static void tegra_pmc_writel(u32 value, unsigned long offset)
253 writel(value, pmc->base + offset);
256 static inline bool tegra_powergate_state(int id)
258 if (id == TEGRA_POWERGATE_3D && pmc->soc->has_gpu_clamps)
259 return (tegra_pmc_readl(GPU_RG_CNTRL) & 0x1) == 0;
261 return (tegra_pmc_readl(PWRGATE_STATUS) & BIT(id)) != 0;
264 static inline bool tegra_powergate_is_valid(int id)
266 return (pmc->soc && pmc->soc->powergates[id]);
269 static inline bool tegra_powergate_is_available(int id)
271 return test_bit(id, pmc->powergates_available);
274 static int tegra_powergate_lookup(struct tegra_pmc *pmc, const char *name)
278 if (!pmc || !pmc->soc || !name)
281 for (i = 0; i < pmc->soc->num_powergates; i++) {
282 if (!tegra_powergate_is_valid(i))
285 if (!strcmp(name, pmc->soc->powergates[i]))
293 * tegra_powergate_set() - set the state of a partition
295 * @new_state: new state of the partition
297 static int tegra_powergate_set(unsigned int id, bool new_state)
302 if (id == TEGRA_POWERGATE_3D && pmc->soc->has_gpu_clamps)
305 mutex_lock(&pmc->powergates_lock);
307 if (tegra_powergate_state(id) == new_state) {
308 mutex_unlock(&pmc->powergates_lock);
312 tegra_pmc_writel(PWRGATE_TOGGLE_START | id, PWRGATE_TOGGLE);
314 err = readx_poll_timeout(tegra_powergate_state, id, status,
315 status == new_state, 10, 100000);
317 mutex_unlock(&pmc->powergates_lock);
322 static int __tegra_powergate_remove_clamping(unsigned int id)
326 mutex_lock(&pmc->powergates_lock);
329 * On Tegra124 and later, the clamps for the GPU are controlled by a
330 * separate register (with different semantics).
332 if (id == TEGRA_POWERGATE_3D) {
333 if (pmc->soc->has_gpu_clamps) {
334 tegra_pmc_writel(0, GPU_RG_CNTRL);
340 * Tegra 2 has a bug where PCIE and VDE clamping masks are
341 * swapped relatively to the partition ids
343 if (id == TEGRA_POWERGATE_VDEC)
344 mask = (1 << TEGRA_POWERGATE_PCIE);
345 else if (id == TEGRA_POWERGATE_PCIE)
346 mask = (1 << TEGRA_POWERGATE_VDEC);
350 tegra_pmc_writel(mask, REMOVE_CLAMPING);
353 mutex_unlock(&pmc->powergates_lock);
358 static void tegra_powergate_disable_clocks(struct tegra_powergate *pg)
362 for (i = 0; i < pg->num_clks; i++)
363 clk_disable_unprepare(pg->clks[i]);
366 static int tegra_powergate_enable_clocks(struct tegra_powergate *pg)
371 for (i = 0; i < pg->num_clks; i++) {
372 err = clk_prepare_enable(pg->clks[i]);
381 clk_disable_unprepare(pg->clks[i]);
386 int __weak tegra210_clk_handle_mbist_war(unsigned int id)
391 static int tegra_powergate_power_up(struct tegra_powergate *pg,
396 err = reset_control_assert(pg->reset);
400 usleep_range(10, 20);
402 err = tegra_powergate_set(pg->id, true);
406 usleep_range(10, 20);
408 err = tegra_powergate_enable_clocks(pg);
412 usleep_range(10, 20);
414 err = __tegra_powergate_remove_clamping(pg->id);
418 usleep_range(10, 20);
420 err = reset_control_deassert(pg->reset);
424 usleep_range(10, 20);
426 if (pg->pmc->soc->needs_mbist_war)
427 err = tegra210_clk_handle_mbist_war(pg->id);
432 tegra_powergate_disable_clocks(pg);
437 tegra_powergate_disable_clocks(pg);
438 usleep_range(10, 20);
441 tegra_powergate_set(pg->id, false);
446 static int tegra_powergate_power_down(struct tegra_powergate *pg)
450 err = tegra_powergate_enable_clocks(pg);
454 usleep_range(10, 20);
456 err = reset_control_assert(pg->reset);
460 usleep_range(10, 20);
462 tegra_powergate_disable_clocks(pg);
464 usleep_range(10, 20);
466 err = tegra_powergate_set(pg->id, false);
473 tegra_powergate_enable_clocks(pg);
474 usleep_range(10, 20);
475 reset_control_deassert(pg->reset);
476 usleep_range(10, 20);
479 tegra_powergate_disable_clocks(pg);
484 static int tegra_genpd_power_on(struct generic_pm_domain *domain)
486 struct tegra_powergate *pg = to_powergate(domain);
489 err = tegra_powergate_power_up(pg, true);
491 pr_err("failed to turn on PM domain %s: %d\n", pg->genpd.name,
497 static int tegra_genpd_power_off(struct generic_pm_domain *domain)
499 struct tegra_powergate *pg = to_powergate(domain);
502 err = tegra_powergate_power_down(pg);
504 pr_err("failed to turn off PM domain %s: %d\n",
505 pg->genpd.name, err);
511 * tegra_powergate_power_on() - power on partition
514 int tegra_powergate_power_on(unsigned int id)
516 if (!tegra_powergate_is_available(id))
519 return tegra_powergate_set(id, true);
523 * tegra_powergate_power_off() - power off partition
526 int tegra_powergate_power_off(unsigned int id)
528 if (!tegra_powergate_is_available(id))
531 return tegra_powergate_set(id, false);
533 EXPORT_SYMBOL(tegra_powergate_power_off);
536 * tegra_powergate_is_powered() - check if partition is powered
539 int tegra_powergate_is_powered(unsigned int id)
543 if (!tegra_powergate_is_valid(id))
546 mutex_lock(&pmc->powergates_lock);
547 status = tegra_powergate_state(id);
548 mutex_unlock(&pmc->powergates_lock);
554 * tegra_powergate_remove_clamping() - remove power clamps for partition
557 int tegra_powergate_remove_clamping(unsigned int id)
559 if (!tegra_powergate_is_available(id))
562 return __tegra_powergate_remove_clamping(id);
564 EXPORT_SYMBOL(tegra_powergate_remove_clamping);
567 * tegra_powergate_sequence_power_up() - power up partition
569 * @clk: clock for partition
570 * @rst: reset for partition
572 * Must be called with clk disabled, and returns with clk enabled.
574 int tegra_powergate_sequence_power_up(unsigned int id, struct clk *clk,
575 struct reset_control *rst)
577 struct tegra_powergate *pg;
580 if (!tegra_powergate_is_available(id))
583 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
593 err = tegra_powergate_power_up(pg, false);
595 pr_err("failed to turn on partition %d: %d\n", id, err);
601 EXPORT_SYMBOL(tegra_powergate_sequence_power_up);
605 * tegra_get_cpu_powergate_id() - convert from CPU ID to partition ID
606 * @cpuid: CPU partition ID
608 * Returns the partition ID corresponding to the CPU partition ID or a
609 * negative error code on failure.
611 static int tegra_get_cpu_powergate_id(unsigned int cpuid)
613 if (pmc->soc && cpuid < pmc->soc->num_cpu_powergates)
614 return pmc->soc->cpu_powergates[cpuid];
620 * tegra_pmc_cpu_is_powered() - check if CPU partition is powered
621 * @cpuid: CPU partition ID
623 bool tegra_pmc_cpu_is_powered(unsigned int cpuid)
627 id = tegra_get_cpu_powergate_id(cpuid);
631 return tegra_powergate_is_powered(id);
635 * tegra_pmc_cpu_power_on() - power on CPU partition
636 * @cpuid: CPU partition ID
638 int tegra_pmc_cpu_power_on(unsigned int cpuid)
642 id = tegra_get_cpu_powergate_id(cpuid);
646 return tegra_powergate_set(id, true);
650 * tegra_pmc_cpu_remove_clamping() - remove power clamps for CPU partition
651 * @cpuid: CPU partition ID
653 int tegra_pmc_cpu_remove_clamping(unsigned int cpuid)
657 id = tegra_get_cpu_powergate_id(cpuid);
661 return tegra_powergate_remove_clamping(id);
663 #endif /* CONFIG_SMP */
665 static int tegra_pmc_restart_notify(struct notifier_block *this,
666 unsigned long action, void *data)
668 const char *cmd = data;
671 value = readl(pmc->scratch + pmc->soc->regs->scratch0);
672 value &= ~PMC_SCRATCH0_MODE_MASK;
675 if (strcmp(cmd, "recovery") == 0)
676 value |= PMC_SCRATCH0_MODE_RECOVERY;
678 if (strcmp(cmd, "bootloader") == 0)
679 value |= PMC_SCRATCH0_MODE_BOOTLOADER;
681 if (strcmp(cmd, "forced-recovery") == 0)
682 value |= PMC_SCRATCH0_MODE_RCM;
685 writel(value, pmc->scratch + pmc->soc->regs->scratch0);
687 /* reset everything but PMC_SCRATCH0 and PMC_RST_STATUS */
688 value = tegra_pmc_readl(PMC_CNTRL);
689 value |= PMC_CNTRL_MAIN_RST;
690 tegra_pmc_writel(value, PMC_CNTRL);
695 static struct notifier_block tegra_pmc_restart_handler = {
696 .notifier_call = tegra_pmc_restart_notify,
700 static int powergate_show(struct seq_file *s, void *data)
705 seq_printf(s, " powergate powered\n");
706 seq_printf(s, "------------------\n");
708 for (i = 0; i < pmc->soc->num_powergates; i++) {
709 status = tegra_powergate_is_powered(i);
713 seq_printf(s, " %9s %7s\n", pmc->soc->powergates[i],
714 status ? "yes" : "no");
720 static int powergate_open(struct inode *inode, struct file *file)
722 return single_open(file, powergate_show, inode->i_private);
725 static const struct file_operations powergate_fops = {
726 .open = powergate_open,
729 .release = single_release,
732 static int tegra_powergate_debugfs_init(void)
734 pmc->debugfs = debugfs_create_file("powergate", S_IRUGO, NULL, NULL,
742 static int tegra_powergate_of_get_clks(struct tegra_powergate *pg,
743 struct device_node *np)
746 unsigned int i, count;
749 count = of_clk_get_parent_count(np);
753 pg->clks = kcalloc(count, sizeof(clk), GFP_KERNEL);
757 for (i = 0; i < count; i++) {
758 pg->clks[i] = of_clk_get(np, i);
759 if (IS_ERR(pg->clks[i])) {
760 err = PTR_ERR(pg->clks[i]);
765 pg->num_clks = count;
771 clk_put(pg->clks[i]);
778 static int tegra_powergate_of_get_resets(struct tegra_powergate *pg,
779 struct device_node *np, bool off)
783 pg->reset = of_reset_control_array_get_exclusive(np);
784 if (IS_ERR(pg->reset)) {
785 err = PTR_ERR(pg->reset);
786 pr_err("failed to get device resets: %d\n", err);
791 err = reset_control_assert(pg->reset);
793 err = reset_control_deassert(pg->reset);
796 reset_control_put(pg->reset);
801 static void tegra_powergate_add(struct tegra_pmc *pmc, struct device_node *np)
803 struct tegra_powergate *pg;
807 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
811 id = tegra_powergate_lookup(pmc, np->name);
813 pr_err("powergate lookup failed for %pOFn: %d\n", np, id);
818 * Clear the bit for this powergate so it cannot be managed
819 * directly via the legacy APIs for controlling powergates.
821 clear_bit(id, pmc->powergates_available);
824 pg->genpd.name = np->name;
825 pg->genpd.power_off = tegra_genpd_power_off;
826 pg->genpd.power_on = tegra_genpd_power_on;
829 off = !tegra_powergate_is_powered(pg->id);
831 err = tegra_powergate_of_get_clks(pg, np);
833 pr_err("failed to get clocks for %pOFn: %d\n", np, err);
837 err = tegra_powergate_of_get_resets(pg, np, off);
839 pr_err("failed to get resets for %pOFn: %d\n", np, err);
843 if (!IS_ENABLED(CONFIG_PM_GENERIC_DOMAINS)) {
845 WARN_ON(tegra_powergate_power_up(pg, true));
851 * FIXME: If XHCI is enabled for Tegra, then power-up the XUSB
852 * host and super-speed partitions. Once the XHCI driver
853 * manages the partitions itself this code can be removed. Note
854 * that we don't register these partitions with the genpd core
855 * to avoid it from powering down the partitions as they appear
858 if (IS_ENABLED(CONFIG_USB_XHCI_TEGRA) &&
859 (id == TEGRA_POWERGATE_XUSBA || id == TEGRA_POWERGATE_XUSBC)) {
861 WARN_ON(tegra_powergate_power_up(pg, true));
866 err = pm_genpd_init(&pg->genpd, NULL, off);
868 pr_err("failed to initialise PM domain %pOFn: %d\n", np,
873 err = of_genpd_add_provider_simple(np, &pg->genpd);
875 pr_err("failed to add PM domain provider for %pOFn: %d\n",
880 pr_debug("added PM domain %s\n", pg->genpd.name);
885 pm_genpd_remove(&pg->genpd);
888 reset_control_put(pg->reset);
891 while (pg->num_clks--)
892 clk_put(pg->clks[pg->num_clks]);
897 set_bit(id, pmc->powergates_available);
903 static void tegra_powergate_init(struct tegra_pmc *pmc,
904 struct device_node *parent)
906 struct device_node *np, *child;
909 /* Create a bitmap of the available and valid partitions */
910 for (i = 0; i < pmc->soc->num_powergates; i++)
911 if (pmc->soc->powergates[i])
912 set_bit(i, pmc->powergates_available);
914 np = of_get_child_by_name(parent, "powergates");
918 for_each_child_of_node(np, child)
919 tegra_powergate_add(pmc, child);
924 static const struct tegra_io_pad_soc *
925 tegra_io_pad_find(struct tegra_pmc *pmc, enum tegra_io_pad id)
929 for (i = 0; i < pmc->soc->num_io_pads; i++)
930 if (pmc->soc->io_pads[i].id == id)
931 return &pmc->soc->io_pads[i];
936 static int tegra_io_pad_get_dpd_register_bit(enum tegra_io_pad id,
937 unsigned long *request,
938 unsigned long *status,
941 const struct tegra_io_pad_soc *pad;
943 pad = tegra_io_pad_find(pmc, id);
945 pr_err("invalid I/O pad ID %u\n", id);
949 if (pad->dpd == UINT_MAX)
952 *mask = BIT(pad->dpd % 32);
955 *status = pmc->soc->regs->dpd_status;
956 *request = pmc->soc->regs->dpd_req;
958 *status = pmc->soc->regs->dpd2_status;
959 *request = pmc->soc->regs->dpd2_req;
965 static int tegra_io_pad_prepare(enum tegra_io_pad id, unsigned long *request,
966 unsigned long *status, u32 *mask)
968 unsigned long rate, value;
971 err = tegra_io_pad_get_dpd_register_bit(id, request, status, mask);
976 rate = clk_get_rate(pmc->clk);
978 pr_err("failed to get clock rate\n");
982 tegra_pmc_writel(DPD_SAMPLE_ENABLE, DPD_SAMPLE);
984 /* must be at least 200 ns, in APB (PCLK) clock cycles */
985 value = DIV_ROUND_UP(1000000000, rate);
986 value = DIV_ROUND_UP(200, value);
987 tegra_pmc_writel(value, SEL_DPD_TIM);
993 static int tegra_io_pad_poll(unsigned long offset, u32 mask,
994 u32 val, unsigned long timeout)
998 timeout = jiffies + msecs_to_jiffies(timeout);
1000 while (time_after(timeout, jiffies)) {
1001 value = tegra_pmc_readl(offset);
1002 if ((value & mask) == val)
1005 usleep_range(250, 1000);
1011 static void tegra_io_pad_unprepare(void)
1014 tegra_pmc_writel(DPD_SAMPLE_DISABLE, DPD_SAMPLE);
1018 * tegra_io_pad_power_enable() - enable power to I/O pad
1019 * @id: Tegra I/O pad ID for which to enable power
1021 * Returns: 0 on success or a negative error code on failure.
1023 int tegra_io_pad_power_enable(enum tegra_io_pad id)
1025 unsigned long request, status;
1029 mutex_lock(&pmc->powergates_lock);
1031 err = tegra_io_pad_prepare(id, &request, &status, &mask);
1033 pr_err("failed to prepare I/O pad: %d\n", err);
1037 tegra_pmc_writel(IO_DPD_REQ_CODE_OFF | mask, request);
1039 err = tegra_io_pad_poll(status, mask, 0, 250);
1041 pr_err("failed to enable I/O pad: %d\n", err);
1045 tegra_io_pad_unprepare();
1048 mutex_unlock(&pmc->powergates_lock);
1051 EXPORT_SYMBOL(tegra_io_pad_power_enable);
1054 * tegra_io_pad_power_disable() - disable power to I/O pad
1055 * @id: Tegra I/O pad ID for which to disable power
1057 * Returns: 0 on success or a negative error code on failure.
1059 int tegra_io_pad_power_disable(enum tegra_io_pad id)
1061 unsigned long request, status;
1065 mutex_lock(&pmc->powergates_lock);
1067 err = tegra_io_pad_prepare(id, &request, &status, &mask);
1069 pr_err("failed to prepare I/O pad: %d\n", err);
1073 tegra_pmc_writel(IO_DPD_REQ_CODE_ON | mask, request);
1075 err = tegra_io_pad_poll(status, mask, mask, 250);
1077 pr_err("failed to disable I/O pad: %d\n", err);
1081 tegra_io_pad_unprepare();
1084 mutex_unlock(&pmc->powergates_lock);
1087 EXPORT_SYMBOL(tegra_io_pad_power_disable);
1089 static int tegra_io_pad_is_powered(enum tegra_io_pad id)
1091 unsigned long request, status;
1095 err = tegra_io_pad_get_dpd_register_bit(id, &request, &status, &mask);
1099 value = tegra_pmc_readl(status);
1101 return !(value & mask);
1104 static int tegra_io_pad_set_voltage(enum tegra_io_pad id, int voltage)
1106 const struct tegra_io_pad_soc *pad;
1109 pad = tegra_io_pad_find(pmc, id);
1113 if (pad->voltage == UINT_MAX)
1116 mutex_lock(&pmc->powergates_lock);
1118 if (pmc->soc->has_impl_33v_pwr) {
1119 value = tegra_pmc_readl(PMC_IMPL_E_33V_PWR);
1121 if (voltage == TEGRA_IO_PAD_VOLTAGE_1V8)
1122 value &= ~BIT(pad->voltage);
1124 value |= BIT(pad->voltage);
1126 tegra_pmc_writel(value, PMC_IMPL_E_33V_PWR);
1128 /* write-enable PMC_PWR_DET_VALUE[pad->voltage] */
1129 value = tegra_pmc_readl(PMC_PWR_DET);
1130 value |= BIT(pad->voltage);
1131 tegra_pmc_writel(value, PMC_PWR_DET);
1133 /* update I/O voltage */
1134 value = tegra_pmc_readl(PMC_PWR_DET_VALUE);
1136 if (voltage == TEGRA_IO_PAD_VOLTAGE_1V8)
1137 value &= ~BIT(pad->voltage);
1139 value |= BIT(pad->voltage);
1141 tegra_pmc_writel(value, PMC_PWR_DET_VALUE);
1144 mutex_unlock(&pmc->powergates_lock);
1146 usleep_range(100, 250);
1151 static int tegra_io_pad_get_voltage(enum tegra_io_pad id)
1153 const struct tegra_io_pad_soc *pad;
1156 pad = tegra_io_pad_find(pmc, id);
1160 if (pad->voltage == UINT_MAX)
1163 if (pmc->soc->has_impl_33v_pwr)
1164 value = tegra_pmc_readl(PMC_IMPL_E_33V_PWR);
1166 value = tegra_pmc_readl(PMC_PWR_DET_VALUE);
1168 if ((value & BIT(pad->voltage)) == 0)
1169 return TEGRA_IO_PAD_VOLTAGE_1V8;
1171 return TEGRA_IO_PAD_VOLTAGE_3V3;
1175 * tegra_io_rail_power_on() - enable power to I/O rail
1176 * @id: Tegra I/O pad ID for which to enable power
1178 * See also: tegra_io_pad_power_enable()
1180 int tegra_io_rail_power_on(unsigned int id)
1182 return tegra_io_pad_power_enable(id);
1184 EXPORT_SYMBOL(tegra_io_rail_power_on);
1187 * tegra_io_rail_power_off() - disable power to I/O rail
1188 * @id: Tegra I/O pad ID for which to disable power
1190 * See also: tegra_io_pad_power_disable()
1192 int tegra_io_rail_power_off(unsigned int id)
1194 return tegra_io_pad_power_disable(id);
1196 EXPORT_SYMBOL(tegra_io_rail_power_off);
1198 #ifdef CONFIG_PM_SLEEP
1199 enum tegra_suspend_mode tegra_pmc_get_suspend_mode(void)
1201 return pmc->suspend_mode;
1204 void tegra_pmc_set_suspend_mode(enum tegra_suspend_mode mode)
1206 if (mode < TEGRA_SUSPEND_NONE || mode >= TEGRA_MAX_SUSPEND_MODE)
1209 pmc->suspend_mode = mode;
1212 void tegra_pmc_enter_suspend_mode(enum tegra_suspend_mode mode)
1214 unsigned long long rate = 0;
1218 case TEGRA_SUSPEND_LP1:
1222 case TEGRA_SUSPEND_LP2:
1223 rate = clk_get_rate(pmc->clk);
1230 if (WARN_ON_ONCE(rate == 0))
1233 if (rate != pmc->rate) {
1236 ticks = pmc->cpu_good_time * rate + USEC_PER_SEC - 1;
1237 do_div(ticks, USEC_PER_SEC);
1238 tegra_pmc_writel(ticks, PMC_CPUPWRGOOD_TIMER);
1240 ticks = pmc->cpu_off_time * rate + USEC_PER_SEC - 1;
1241 do_div(ticks, USEC_PER_SEC);
1242 tegra_pmc_writel(ticks, PMC_CPUPWROFF_TIMER);
1249 value = tegra_pmc_readl(PMC_CNTRL);
1250 value &= ~PMC_CNTRL_SIDE_EFFECT_LP0;
1251 value |= PMC_CNTRL_CPU_PWRREQ_OE;
1252 tegra_pmc_writel(value, PMC_CNTRL);
1256 static int tegra_pmc_parse_dt(struct tegra_pmc *pmc, struct device_node *np)
1258 u32 value, values[2];
1260 if (of_property_read_u32(np, "nvidia,suspend-mode", &value)) {
1264 pmc->suspend_mode = TEGRA_SUSPEND_LP0;
1268 pmc->suspend_mode = TEGRA_SUSPEND_LP1;
1272 pmc->suspend_mode = TEGRA_SUSPEND_LP2;
1276 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1281 pmc->suspend_mode = tegra_pm_validate_suspend_mode(pmc->suspend_mode);
1283 if (of_property_read_u32(np, "nvidia,cpu-pwr-good-time", &value))
1284 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1286 pmc->cpu_good_time = value;
1288 if (of_property_read_u32(np, "nvidia,cpu-pwr-off-time", &value))
1289 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1291 pmc->cpu_off_time = value;
1293 if (of_property_read_u32_array(np, "nvidia,core-pwr-good-time",
1294 values, ARRAY_SIZE(values)))
1295 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1297 pmc->core_osc_time = values[0];
1298 pmc->core_pmu_time = values[1];
1300 if (of_property_read_u32(np, "nvidia,core-pwr-off-time", &value))
1301 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1303 pmc->core_off_time = value;
1305 pmc->corereq_high = of_property_read_bool(np,
1306 "nvidia,core-power-req-active-high");
1308 pmc->sysclkreq_high = of_property_read_bool(np,
1309 "nvidia,sys-clock-req-active-high");
1311 pmc->combined_req = of_property_read_bool(np,
1312 "nvidia,combined-power-req");
1314 pmc->cpu_pwr_good_en = of_property_read_bool(np,
1315 "nvidia,cpu-pwr-good-en");
1317 if (of_property_read_u32_array(np, "nvidia,lp0-vec", values,
1318 ARRAY_SIZE(values)))
1319 if (pmc->suspend_mode == TEGRA_SUSPEND_LP0)
1320 pmc->suspend_mode = TEGRA_SUSPEND_LP1;
1322 pmc->lp0_vec_phys = values[0];
1323 pmc->lp0_vec_size = values[1];
1328 static void tegra_pmc_init(struct tegra_pmc *pmc)
1331 pmc->soc->init(pmc);
1334 static void tegra_pmc_init_tsense_reset(struct tegra_pmc *pmc)
1336 static const char disabled[] = "emergency thermal reset disabled";
1337 u32 pmu_addr, ctrl_id, reg_addr, reg_data, pinmux;
1338 struct device *dev = pmc->dev;
1339 struct device_node *np;
1340 u32 value, checksum;
1342 if (!pmc->soc->has_tsense_reset)
1345 np = of_get_child_by_name(pmc->dev->of_node, "i2c-thermtrip");
1347 dev_warn(dev, "i2c-thermtrip node not found, %s.\n", disabled);
1351 if (of_property_read_u32(np, "nvidia,i2c-controller-id", &ctrl_id)) {
1352 dev_err(dev, "I2C controller ID missing, %s.\n", disabled);
1356 if (of_property_read_u32(np, "nvidia,bus-addr", &pmu_addr)) {
1357 dev_err(dev, "nvidia,bus-addr missing, %s.\n", disabled);
1361 if (of_property_read_u32(np, "nvidia,reg-addr", ®_addr)) {
1362 dev_err(dev, "nvidia,reg-addr missing, %s.\n", disabled);
1366 if (of_property_read_u32(np, "nvidia,reg-data", ®_data)) {
1367 dev_err(dev, "nvidia,reg-data missing, %s.\n", disabled);
1371 if (of_property_read_u32(np, "nvidia,pinmux-id", &pinmux))
1374 value = tegra_pmc_readl(PMC_SENSOR_CTRL);
1375 value |= PMC_SENSOR_CTRL_SCRATCH_WRITE;
1376 tegra_pmc_writel(value, PMC_SENSOR_CTRL);
1378 value = (reg_data << PMC_SCRATCH54_DATA_SHIFT) |
1379 (reg_addr << PMC_SCRATCH54_ADDR_SHIFT);
1380 tegra_pmc_writel(value, PMC_SCRATCH54);
1382 value = PMC_SCRATCH55_RESET_TEGRA;
1383 value |= ctrl_id << PMC_SCRATCH55_CNTRL_ID_SHIFT;
1384 value |= pinmux << PMC_SCRATCH55_PINMUX_SHIFT;
1385 value |= pmu_addr << PMC_SCRATCH55_I2CSLV1_SHIFT;
1388 * Calculate checksum of SCRATCH54, SCRATCH55 fields. Bits 23:16 will
1389 * contain the checksum and are currently zero, so they are not added.
1391 checksum = reg_addr + reg_data + (value & 0xff) + ((value >> 8) & 0xff)
1392 + ((value >> 24) & 0xff);
1394 checksum = 0x100 - checksum;
1396 value |= checksum << PMC_SCRATCH55_CHECKSUM_SHIFT;
1398 tegra_pmc_writel(value, PMC_SCRATCH55);
1400 value = tegra_pmc_readl(PMC_SENSOR_CTRL);
1401 value |= PMC_SENSOR_CTRL_ENABLE_RST;
1402 tegra_pmc_writel(value, PMC_SENSOR_CTRL);
1404 dev_info(pmc->dev, "emergency thermal reset enabled\n");
1410 static int tegra_io_pad_pinctrl_get_groups_count(struct pinctrl_dev *pctl_dev)
1412 return pmc->soc->num_io_pads;
1415 static const char *tegra_io_pad_pinctrl_get_group_name(
1416 struct pinctrl_dev *pctl, unsigned int group)
1418 return pmc->soc->io_pads[group].name;
1421 static int tegra_io_pad_pinctrl_get_group_pins(struct pinctrl_dev *pctl_dev,
1423 const unsigned int **pins,
1424 unsigned int *num_pins)
1426 *pins = &pmc->soc->io_pads[group].id;
1431 static const struct pinctrl_ops tegra_io_pad_pinctrl_ops = {
1432 .get_groups_count = tegra_io_pad_pinctrl_get_groups_count,
1433 .get_group_name = tegra_io_pad_pinctrl_get_group_name,
1434 .get_group_pins = tegra_io_pad_pinctrl_get_group_pins,
1435 .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
1436 .dt_free_map = pinconf_generic_dt_free_map,
1439 static int tegra_io_pad_pinconf_get(struct pinctrl_dev *pctl_dev,
1440 unsigned int pin, unsigned long *config)
1442 const struct tegra_io_pad_soc *pad = tegra_io_pad_find(pmc, pin);
1443 enum pin_config_param param = pinconf_to_config_param(*config);
1451 case PIN_CONFIG_POWER_SOURCE:
1452 ret = tegra_io_pad_get_voltage(pad->id);
1457 case PIN_CONFIG_LOW_POWER_MODE:
1458 ret = tegra_io_pad_is_powered(pad->id);
1467 *config = pinconf_to_config_packed(param, arg);
1472 static int tegra_io_pad_pinconf_set(struct pinctrl_dev *pctl_dev,
1473 unsigned int pin, unsigned long *configs,
1474 unsigned int num_configs)
1476 const struct tegra_io_pad_soc *pad = tegra_io_pad_find(pmc, pin);
1477 enum pin_config_param param;
1485 for (i = 0; i < num_configs; ++i) {
1486 param = pinconf_to_config_param(configs[i]);
1487 arg = pinconf_to_config_argument(configs[i]);
1490 case PIN_CONFIG_LOW_POWER_MODE:
1492 err = tegra_io_pad_power_disable(pad->id);
1494 err = tegra_io_pad_power_enable(pad->id);
1498 case PIN_CONFIG_POWER_SOURCE:
1499 if (arg != TEGRA_IO_PAD_VOLTAGE_1V8 &&
1500 arg != TEGRA_IO_PAD_VOLTAGE_3V3)
1502 err = tegra_io_pad_set_voltage(pad->id, arg);
1514 static const struct pinconf_ops tegra_io_pad_pinconf_ops = {
1515 .pin_config_get = tegra_io_pad_pinconf_get,
1516 .pin_config_set = tegra_io_pad_pinconf_set,
1520 static struct pinctrl_desc tegra_pmc_pctl_desc = {
1521 .pctlops = &tegra_io_pad_pinctrl_ops,
1522 .confops = &tegra_io_pad_pinconf_ops,
1525 static int tegra_pmc_pinctrl_init(struct tegra_pmc *pmc)
1529 if (!pmc->soc->num_pin_descs)
1532 tegra_pmc_pctl_desc.name = dev_name(pmc->dev);
1533 tegra_pmc_pctl_desc.pins = pmc->soc->pin_descs;
1534 tegra_pmc_pctl_desc.npins = pmc->soc->num_pin_descs;
1536 pmc->pctl_dev = devm_pinctrl_register(pmc->dev, &tegra_pmc_pctl_desc,
1538 if (IS_ERR(pmc->pctl_dev)) {
1539 err = PTR_ERR(pmc->pctl_dev);
1540 dev_err(pmc->dev, "unable to register pinctrl, %d\n", err);
1546 static int tegra_pmc_probe(struct platform_device *pdev)
1549 struct resource *res;
1553 * Early initialisation should have configured an initial
1554 * register mapping and setup the soc data pointer. If these
1555 * are not valid then something went badly wrong!
1557 if (WARN_ON(!pmc->base || !pmc->soc))
1560 err = tegra_pmc_parse_dt(pmc, pdev->dev.of_node);
1564 /* take over the memory region from the early initialization */
1565 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1566 base = devm_ioremap_resource(&pdev->dev, res);
1568 return PTR_ERR(base);
1570 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "wake");
1572 pmc->wake = devm_ioremap_resource(&pdev->dev, res);
1573 if (IS_ERR(pmc->wake))
1574 return PTR_ERR(pmc->wake);
1579 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "aotag");
1581 pmc->aotag = devm_ioremap_resource(&pdev->dev, res);
1582 if (IS_ERR(pmc->aotag))
1583 return PTR_ERR(pmc->aotag);
1588 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "scratch");
1590 pmc->scratch = devm_ioremap_resource(&pdev->dev, res);
1591 if (IS_ERR(pmc->scratch))
1592 return PTR_ERR(pmc->scratch);
1594 pmc->scratch = base;
1597 pmc->clk = devm_clk_get(&pdev->dev, "pclk");
1598 if (IS_ERR(pmc->clk)) {
1599 err = PTR_ERR(pmc->clk);
1601 if (err != -ENOENT) {
1602 dev_err(&pdev->dev, "failed to get pclk: %d\n", err);
1609 pmc->dev = &pdev->dev;
1611 tegra_pmc_init(pmc);
1613 tegra_pmc_init_tsense_reset(pmc);
1615 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
1616 err = tegra_powergate_debugfs_init();
1621 err = register_restart_handler(&tegra_pmc_restart_handler);
1623 dev_err(&pdev->dev, "unable to register restart handler, %d\n",
1625 goto cleanup_debugfs;
1628 err = tegra_pmc_pinctrl_init(pmc);
1630 goto cleanup_restart_handler;
1632 mutex_lock(&pmc->powergates_lock);
1635 mutex_unlock(&pmc->powergates_lock);
1639 cleanup_restart_handler:
1640 unregister_restart_handler(&tegra_pmc_restart_handler);
1642 debugfs_remove(pmc->debugfs);
1646 #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM)
1647 static int tegra_pmc_suspend(struct device *dev)
1649 tegra_pmc_writel(virt_to_phys(tegra_resume), PMC_SCRATCH41);
1654 static int tegra_pmc_resume(struct device *dev)
1656 tegra_pmc_writel(0x0, PMC_SCRATCH41);
1661 static SIMPLE_DEV_PM_OPS(tegra_pmc_pm_ops, tegra_pmc_suspend, tegra_pmc_resume);
1665 static const char * const tegra20_powergates[] = {
1666 [TEGRA_POWERGATE_CPU] = "cpu",
1667 [TEGRA_POWERGATE_3D] = "3d",
1668 [TEGRA_POWERGATE_VENC] = "venc",
1669 [TEGRA_POWERGATE_VDEC] = "vdec",
1670 [TEGRA_POWERGATE_PCIE] = "pcie",
1671 [TEGRA_POWERGATE_L2] = "l2",
1672 [TEGRA_POWERGATE_MPE] = "mpe",
1675 static const struct tegra_pmc_regs tegra20_pmc_regs = {
1678 .dpd_status = 0x1bc,
1680 .dpd2_status = 0x1c4,
1683 static void tegra20_pmc_init(struct tegra_pmc *pmc)
1687 /* Always enable CPU power request */
1688 value = tegra_pmc_readl(PMC_CNTRL);
1689 value |= PMC_CNTRL_CPU_PWRREQ_OE;
1690 tegra_pmc_writel(value, PMC_CNTRL);
1692 value = tegra_pmc_readl(PMC_CNTRL);
1694 if (pmc->sysclkreq_high)
1695 value &= ~PMC_CNTRL_SYSCLK_POLARITY;
1697 value |= PMC_CNTRL_SYSCLK_POLARITY;
1699 /* configure the output polarity while the request is tristated */
1700 tegra_pmc_writel(value, PMC_CNTRL);
1702 /* now enable the request */
1703 value = tegra_pmc_readl(PMC_CNTRL);
1704 value |= PMC_CNTRL_SYSCLK_OE;
1705 tegra_pmc_writel(value, PMC_CNTRL);
1708 static void tegra20_pmc_setup_irq_polarity(struct tegra_pmc *pmc,
1709 struct device_node *np,
1714 value = tegra_pmc_readl(PMC_CNTRL);
1717 value |= PMC_CNTRL_INTR_POLARITY;
1719 value &= ~PMC_CNTRL_INTR_POLARITY;
1721 tegra_pmc_writel(value, PMC_CNTRL);
1724 static const struct tegra_pmc_soc tegra20_pmc_soc = {
1725 .num_powergates = ARRAY_SIZE(tegra20_powergates),
1726 .powergates = tegra20_powergates,
1727 .num_cpu_powergates = 0,
1728 .cpu_powergates = NULL,
1729 .has_tsense_reset = false,
1730 .has_gpu_clamps = false,
1735 .regs = &tegra20_pmc_regs,
1736 .init = tegra20_pmc_init,
1737 .setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
1740 static const char * const tegra30_powergates[] = {
1741 [TEGRA_POWERGATE_CPU] = "cpu0",
1742 [TEGRA_POWERGATE_3D] = "3d0",
1743 [TEGRA_POWERGATE_VENC] = "venc",
1744 [TEGRA_POWERGATE_VDEC] = "vdec",
1745 [TEGRA_POWERGATE_PCIE] = "pcie",
1746 [TEGRA_POWERGATE_L2] = "l2",
1747 [TEGRA_POWERGATE_MPE] = "mpe",
1748 [TEGRA_POWERGATE_HEG] = "heg",
1749 [TEGRA_POWERGATE_SATA] = "sata",
1750 [TEGRA_POWERGATE_CPU1] = "cpu1",
1751 [TEGRA_POWERGATE_CPU2] = "cpu2",
1752 [TEGRA_POWERGATE_CPU3] = "cpu3",
1753 [TEGRA_POWERGATE_CELP] = "celp",
1754 [TEGRA_POWERGATE_3D1] = "3d1",
1757 static const u8 tegra30_cpu_powergates[] = {
1758 TEGRA_POWERGATE_CPU,
1759 TEGRA_POWERGATE_CPU1,
1760 TEGRA_POWERGATE_CPU2,
1761 TEGRA_POWERGATE_CPU3,
1764 static const struct tegra_pmc_soc tegra30_pmc_soc = {
1765 .num_powergates = ARRAY_SIZE(tegra30_powergates),
1766 .powergates = tegra30_powergates,
1767 .num_cpu_powergates = ARRAY_SIZE(tegra30_cpu_powergates),
1768 .cpu_powergates = tegra30_cpu_powergates,
1769 .has_tsense_reset = true,
1770 .has_gpu_clamps = false,
1771 .has_impl_33v_pwr = false,
1776 .regs = &tegra20_pmc_regs,
1777 .init = tegra20_pmc_init,
1778 .setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
1781 static const char * const tegra114_powergates[] = {
1782 [TEGRA_POWERGATE_CPU] = "crail",
1783 [TEGRA_POWERGATE_3D] = "3d",
1784 [TEGRA_POWERGATE_VENC] = "venc",
1785 [TEGRA_POWERGATE_VDEC] = "vdec",
1786 [TEGRA_POWERGATE_MPE] = "mpe",
1787 [TEGRA_POWERGATE_HEG] = "heg",
1788 [TEGRA_POWERGATE_CPU1] = "cpu1",
1789 [TEGRA_POWERGATE_CPU2] = "cpu2",
1790 [TEGRA_POWERGATE_CPU3] = "cpu3",
1791 [TEGRA_POWERGATE_CELP] = "celp",
1792 [TEGRA_POWERGATE_CPU0] = "cpu0",
1793 [TEGRA_POWERGATE_C0NC] = "c0nc",
1794 [TEGRA_POWERGATE_C1NC] = "c1nc",
1795 [TEGRA_POWERGATE_DIS] = "dis",
1796 [TEGRA_POWERGATE_DISB] = "disb",
1797 [TEGRA_POWERGATE_XUSBA] = "xusba",
1798 [TEGRA_POWERGATE_XUSBB] = "xusbb",
1799 [TEGRA_POWERGATE_XUSBC] = "xusbc",
1802 static const u8 tegra114_cpu_powergates[] = {
1803 TEGRA_POWERGATE_CPU0,
1804 TEGRA_POWERGATE_CPU1,
1805 TEGRA_POWERGATE_CPU2,
1806 TEGRA_POWERGATE_CPU3,
1809 static const struct tegra_pmc_soc tegra114_pmc_soc = {
1810 .num_powergates = ARRAY_SIZE(tegra114_powergates),
1811 .powergates = tegra114_powergates,
1812 .num_cpu_powergates = ARRAY_SIZE(tegra114_cpu_powergates),
1813 .cpu_powergates = tegra114_cpu_powergates,
1814 .has_tsense_reset = true,
1815 .has_gpu_clamps = false,
1816 .has_impl_33v_pwr = false,
1821 .regs = &tegra20_pmc_regs,
1822 .init = tegra20_pmc_init,
1823 .setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
1826 static const char * const tegra124_powergates[] = {
1827 [TEGRA_POWERGATE_CPU] = "crail",
1828 [TEGRA_POWERGATE_3D] = "3d",
1829 [TEGRA_POWERGATE_VENC] = "venc",
1830 [TEGRA_POWERGATE_PCIE] = "pcie",
1831 [TEGRA_POWERGATE_VDEC] = "vdec",
1832 [TEGRA_POWERGATE_MPE] = "mpe",
1833 [TEGRA_POWERGATE_HEG] = "heg",
1834 [TEGRA_POWERGATE_SATA] = "sata",
1835 [TEGRA_POWERGATE_CPU1] = "cpu1",
1836 [TEGRA_POWERGATE_CPU2] = "cpu2",
1837 [TEGRA_POWERGATE_CPU3] = "cpu3",
1838 [TEGRA_POWERGATE_CELP] = "celp",
1839 [TEGRA_POWERGATE_CPU0] = "cpu0",
1840 [TEGRA_POWERGATE_C0NC] = "c0nc",
1841 [TEGRA_POWERGATE_C1NC] = "c1nc",
1842 [TEGRA_POWERGATE_SOR] = "sor",
1843 [TEGRA_POWERGATE_DIS] = "dis",
1844 [TEGRA_POWERGATE_DISB] = "disb",
1845 [TEGRA_POWERGATE_XUSBA] = "xusba",
1846 [TEGRA_POWERGATE_XUSBB] = "xusbb",
1847 [TEGRA_POWERGATE_XUSBC] = "xusbc",
1848 [TEGRA_POWERGATE_VIC] = "vic",
1849 [TEGRA_POWERGATE_IRAM] = "iram",
1852 static const u8 tegra124_cpu_powergates[] = {
1853 TEGRA_POWERGATE_CPU0,
1854 TEGRA_POWERGATE_CPU1,
1855 TEGRA_POWERGATE_CPU2,
1856 TEGRA_POWERGATE_CPU3,
1859 #define TEGRA_IO_PAD(_id, _dpd, _voltage, _name) \
1860 ((struct tegra_io_pad_soc) { \
1863 .voltage = (_voltage), \
1867 #define TEGRA_IO_PIN_DESC(_id, _dpd, _voltage, _name) \
1868 ((struct pinctrl_pin_desc) { \
1873 #define TEGRA124_IO_PAD_TABLE(_pad) \
1874 /* .id .dpd .voltage .name */ \
1875 _pad(TEGRA_IO_PAD_AUDIO, 17, UINT_MAX, "audio"), \
1876 _pad(TEGRA_IO_PAD_BB, 15, UINT_MAX, "bb"), \
1877 _pad(TEGRA_IO_PAD_CAM, 36, UINT_MAX, "cam"), \
1878 _pad(TEGRA_IO_PAD_COMP, 22, UINT_MAX, "comp"), \
1879 _pad(TEGRA_IO_PAD_CSIA, 0, UINT_MAX, "csia"), \
1880 _pad(TEGRA_IO_PAD_CSIB, 1, UINT_MAX, "csb"), \
1881 _pad(TEGRA_IO_PAD_CSIE, 44, UINT_MAX, "cse"), \
1882 _pad(TEGRA_IO_PAD_DSI, 2, UINT_MAX, "dsi"), \
1883 _pad(TEGRA_IO_PAD_DSIB, 39, UINT_MAX, "dsib"), \
1884 _pad(TEGRA_IO_PAD_DSIC, 40, UINT_MAX, "dsic"), \
1885 _pad(TEGRA_IO_PAD_DSID, 41, UINT_MAX, "dsid"), \
1886 _pad(TEGRA_IO_PAD_HDMI, 28, UINT_MAX, "hdmi"), \
1887 _pad(TEGRA_IO_PAD_HSIC, 19, UINT_MAX, "hsic"), \
1888 _pad(TEGRA_IO_PAD_HV, 38, UINT_MAX, "hv"), \
1889 _pad(TEGRA_IO_PAD_LVDS, 57, UINT_MAX, "lvds"), \
1890 _pad(TEGRA_IO_PAD_MIPI_BIAS, 3, UINT_MAX, "mipi-bias"), \
1891 _pad(TEGRA_IO_PAD_NAND, 13, UINT_MAX, "nand"), \
1892 _pad(TEGRA_IO_PAD_PEX_BIAS, 4, UINT_MAX, "pex-bias"), \
1893 _pad(TEGRA_IO_PAD_PEX_CLK1, 5, UINT_MAX, "pex-clk1"), \
1894 _pad(TEGRA_IO_PAD_PEX_CLK2, 6, UINT_MAX, "pex-clk2"), \
1895 _pad(TEGRA_IO_PAD_PEX_CNTRL, 32, UINT_MAX, "pex-cntrl"), \
1896 _pad(TEGRA_IO_PAD_SDMMC1, 33, UINT_MAX, "sdmmc1"), \
1897 _pad(TEGRA_IO_PAD_SDMMC3, 34, UINT_MAX, "sdmmc3"), \
1898 _pad(TEGRA_IO_PAD_SDMMC4, 35, UINT_MAX, "sdmmc4"), \
1899 _pad(TEGRA_IO_PAD_SYS_DDC, 58, UINT_MAX, "sys_ddc"), \
1900 _pad(TEGRA_IO_PAD_UART, 14, UINT_MAX, "uart"), \
1901 _pad(TEGRA_IO_PAD_USB0, 9, UINT_MAX, "usb0"), \
1902 _pad(TEGRA_IO_PAD_USB1, 10, UINT_MAX, "usb1"), \
1903 _pad(TEGRA_IO_PAD_USB2, 11, UINT_MAX, "usb2"), \
1904 _pad(TEGRA_IO_PAD_USB_BIAS, 12, UINT_MAX, "usb_bias")
1906 static const struct tegra_io_pad_soc tegra124_io_pads[] = {
1907 TEGRA124_IO_PAD_TABLE(TEGRA_IO_PAD)
1910 static const struct pinctrl_pin_desc tegra124_pin_descs[] = {
1911 TEGRA124_IO_PAD_TABLE(TEGRA_IO_PIN_DESC)
1914 static const struct tegra_pmc_soc tegra124_pmc_soc = {
1915 .num_powergates = ARRAY_SIZE(tegra124_powergates),
1916 .powergates = tegra124_powergates,
1917 .num_cpu_powergates = ARRAY_SIZE(tegra124_cpu_powergates),
1918 .cpu_powergates = tegra124_cpu_powergates,
1919 .has_tsense_reset = true,
1920 .has_gpu_clamps = true,
1921 .has_impl_33v_pwr = false,
1922 .num_io_pads = ARRAY_SIZE(tegra124_io_pads),
1923 .io_pads = tegra124_io_pads,
1924 .num_pin_descs = ARRAY_SIZE(tegra124_pin_descs),
1925 .pin_descs = tegra124_pin_descs,
1926 .regs = &tegra20_pmc_regs,
1927 .init = tegra20_pmc_init,
1928 .setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
1931 static const char * const tegra210_powergates[] = {
1932 [TEGRA_POWERGATE_CPU] = "crail",
1933 [TEGRA_POWERGATE_3D] = "3d",
1934 [TEGRA_POWERGATE_VENC] = "venc",
1935 [TEGRA_POWERGATE_PCIE] = "pcie",
1936 [TEGRA_POWERGATE_MPE] = "mpe",
1937 [TEGRA_POWERGATE_SATA] = "sata",
1938 [TEGRA_POWERGATE_CPU1] = "cpu1",
1939 [TEGRA_POWERGATE_CPU2] = "cpu2",
1940 [TEGRA_POWERGATE_CPU3] = "cpu3",
1941 [TEGRA_POWERGATE_CPU0] = "cpu0",
1942 [TEGRA_POWERGATE_C0NC] = "c0nc",
1943 [TEGRA_POWERGATE_SOR] = "sor",
1944 [TEGRA_POWERGATE_DIS] = "dis",
1945 [TEGRA_POWERGATE_DISB] = "disb",
1946 [TEGRA_POWERGATE_XUSBA] = "xusba",
1947 [TEGRA_POWERGATE_XUSBB] = "xusbb",
1948 [TEGRA_POWERGATE_XUSBC] = "xusbc",
1949 [TEGRA_POWERGATE_VIC] = "vic",
1950 [TEGRA_POWERGATE_IRAM] = "iram",
1951 [TEGRA_POWERGATE_NVDEC] = "nvdec",
1952 [TEGRA_POWERGATE_NVJPG] = "nvjpg",
1953 [TEGRA_POWERGATE_AUD] = "aud",
1954 [TEGRA_POWERGATE_DFD] = "dfd",
1955 [TEGRA_POWERGATE_VE2] = "ve2",
1958 static const u8 tegra210_cpu_powergates[] = {
1959 TEGRA_POWERGATE_CPU0,
1960 TEGRA_POWERGATE_CPU1,
1961 TEGRA_POWERGATE_CPU2,
1962 TEGRA_POWERGATE_CPU3,
1965 #define TEGRA210_IO_PAD_TABLE(_pad) \
1966 /* .id .dpd .voltage .name */ \
1967 _pad(TEGRA_IO_PAD_AUDIO, 17, 5, "audio"), \
1968 _pad(TEGRA_IO_PAD_AUDIO_HV, 61, 18, "audio-hv"), \
1969 _pad(TEGRA_IO_PAD_CAM, 36, 10, "cam"), \
1970 _pad(TEGRA_IO_PAD_CSIA, 0, UINT_MAX, "csia"), \
1971 _pad(TEGRA_IO_PAD_CSIB, 1, UINT_MAX, "csib"), \
1972 _pad(TEGRA_IO_PAD_CSIC, 42, UINT_MAX, "csic"), \
1973 _pad(TEGRA_IO_PAD_CSID, 43, UINT_MAX, "csid"), \
1974 _pad(TEGRA_IO_PAD_CSIE, 44, UINT_MAX, "csie"), \
1975 _pad(TEGRA_IO_PAD_CSIF, 45, UINT_MAX, "csif"), \
1976 _pad(TEGRA_IO_PAD_DBG, 25, 19, "dbg"), \
1977 _pad(TEGRA_IO_PAD_DEBUG_NONAO, 26, UINT_MAX, "debug-nonao"), \
1978 _pad(TEGRA_IO_PAD_DMIC, 50, 20, "dmic"), \
1979 _pad(TEGRA_IO_PAD_DP, 51, UINT_MAX, "dp"), \
1980 _pad(TEGRA_IO_PAD_DSI, 2, UINT_MAX, "dsi"), \
1981 _pad(TEGRA_IO_PAD_DSIB, 39, UINT_MAX, "dsib"), \
1982 _pad(TEGRA_IO_PAD_DSIC, 40, UINT_MAX, "dsic"), \
1983 _pad(TEGRA_IO_PAD_DSID, 41, UINT_MAX, "dsid"), \
1984 _pad(TEGRA_IO_PAD_EMMC, 35, UINT_MAX, "emmc"), \
1985 _pad(TEGRA_IO_PAD_EMMC2, 37, UINT_MAX, "emmc2"), \
1986 _pad(TEGRA_IO_PAD_GPIO, 27, 21, "gpio"), \
1987 _pad(TEGRA_IO_PAD_HDMI, 28, UINT_MAX, "hdmi"), \
1988 _pad(TEGRA_IO_PAD_HSIC, 19, UINT_MAX, "hsic"), \
1989 _pad(TEGRA_IO_PAD_LVDS, 57, UINT_MAX, "lvds"), \
1990 _pad(TEGRA_IO_PAD_MIPI_BIAS, 3, UINT_MAX, "mipi-bias"), \
1991 _pad(TEGRA_IO_PAD_PEX_BIAS, 4, UINT_MAX, "pex-bias"), \
1992 _pad(TEGRA_IO_PAD_PEX_CLK1, 5, UINT_MAX, "pex-clk1"), \
1993 _pad(TEGRA_IO_PAD_PEX_CLK2, 6, UINT_MAX, "pex-clk2"), \
1994 _pad(TEGRA_IO_PAD_PEX_CNTRL, UINT_MAX, 11, "pex-cntrl"), \
1995 _pad(TEGRA_IO_PAD_SDMMC1, 33, 12, "sdmmc1"), \
1996 _pad(TEGRA_IO_PAD_SDMMC3, 34, 13, "sdmmc3"), \
1997 _pad(TEGRA_IO_PAD_SPI, 46, 22, "spi"), \
1998 _pad(TEGRA_IO_PAD_SPI_HV, 47, 23, "spi-hv"), \
1999 _pad(TEGRA_IO_PAD_UART, 14, 2, "uart"), \
2000 _pad(TEGRA_IO_PAD_USB0, 9, UINT_MAX, "usb0"), \
2001 _pad(TEGRA_IO_PAD_USB1, 10, UINT_MAX, "usb1"), \
2002 _pad(TEGRA_IO_PAD_USB2, 11, UINT_MAX, "usb2"), \
2003 _pad(TEGRA_IO_PAD_USB3, 18, UINT_MAX, "usb3"), \
2004 _pad(TEGRA_IO_PAD_USB_BIAS, 12, UINT_MAX, "usb-bias")
2006 static const struct tegra_io_pad_soc tegra210_io_pads[] = {
2007 TEGRA210_IO_PAD_TABLE(TEGRA_IO_PAD)
2010 static const struct pinctrl_pin_desc tegra210_pin_descs[] = {
2011 TEGRA210_IO_PAD_TABLE(TEGRA_IO_PIN_DESC)
2014 static const struct tegra_pmc_soc tegra210_pmc_soc = {
2015 .num_powergates = ARRAY_SIZE(tegra210_powergates),
2016 .powergates = tegra210_powergates,
2017 .num_cpu_powergates = ARRAY_SIZE(tegra210_cpu_powergates),
2018 .cpu_powergates = tegra210_cpu_powergates,
2019 .has_tsense_reset = true,
2020 .has_gpu_clamps = true,
2021 .has_impl_33v_pwr = false,
2022 .needs_mbist_war = true,
2023 .num_io_pads = ARRAY_SIZE(tegra210_io_pads),
2024 .io_pads = tegra210_io_pads,
2025 .num_pin_descs = ARRAY_SIZE(tegra210_pin_descs),
2026 .pin_descs = tegra210_pin_descs,
2027 .regs = &tegra20_pmc_regs,
2028 .init = tegra20_pmc_init,
2029 .setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
2032 #define TEGRA186_IO_PAD_TABLE(_pad) \
2033 /* .id .dpd .voltage .name */ \
2034 _pad(TEGRA_IO_PAD_CSIA, 0, UINT_MAX, "csia"), \
2035 _pad(TEGRA_IO_PAD_CSIB, 1, UINT_MAX, "csib"), \
2036 _pad(TEGRA_IO_PAD_DSI, 2, UINT_MAX, "dsi"), \
2037 _pad(TEGRA_IO_PAD_MIPI_BIAS, 3, UINT_MAX, "mipi-bias"), \
2038 _pad(TEGRA_IO_PAD_PEX_CLK_BIAS, 4, UINT_MAX, "pex-clk-bias"), \
2039 _pad(TEGRA_IO_PAD_PEX_CLK3, 5, UINT_MAX, "pex-clk3"), \
2040 _pad(TEGRA_IO_PAD_PEX_CLK2, 6, UINT_MAX, "pex-clk2"), \
2041 _pad(TEGRA_IO_PAD_PEX_CLK1, 7, UINT_MAX, "pex-clk1"), \
2042 _pad(TEGRA_IO_PAD_USB0, 9, UINT_MAX, "usb0"), \
2043 _pad(TEGRA_IO_PAD_USB1, 10, UINT_MAX, "usb1"), \
2044 _pad(TEGRA_IO_PAD_USB2, 11, UINT_MAX, "usb2"), \
2045 _pad(TEGRA_IO_PAD_USB_BIAS, 12, UINT_MAX, "usb-bias"), \
2046 _pad(TEGRA_IO_PAD_UART, 14, UINT_MAX, "uart"), \
2047 _pad(TEGRA_IO_PAD_AUDIO, 17, UINT_MAX, "audio"), \
2048 _pad(TEGRA_IO_PAD_HSIC, 19, UINT_MAX, "hsic"), \
2049 _pad(TEGRA_IO_PAD_DBG, 25, UINT_MAX, "dbg"), \
2050 _pad(TEGRA_IO_PAD_HDMI_DP0, 28, UINT_MAX, "hdmi-dp0"), \
2051 _pad(TEGRA_IO_PAD_HDMI_DP1, 29, UINT_MAX, "hdmi-dp1"), \
2052 _pad(TEGRA_IO_PAD_PEX_CNTRL, 32, UINT_MAX, "pex-cntrl"), \
2053 _pad(TEGRA_IO_PAD_SDMMC2_HV, 34, 5, "sdmmc2-hv"), \
2054 _pad(TEGRA_IO_PAD_SDMMC4, 36, UINT_MAX, "sdmmc4"), \
2055 _pad(TEGRA_IO_PAD_CAM, 38, UINT_MAX, "cam"), \
2056 _pad(TEGRA_IO_PAD_DSIB, 40, UINT_MAX, "dsib"), \
2057 _pad(TEGRA_IO_PAD_DSIC, 41, UINT_MAX, "dsic"), \
2058 _pad(TEGRA_IO_PAD_DSID, 42, UINT_MAX, "dsid"), \
2059 _pad(TEGRA_IO_PAD_CSIC, 43, UINT_MAX, "csic"), \
2060 _pad(TEGRA_IO_PAD_CSID, 44, UINT_MAX, "csid"), \
2061 _pad(TEGRA_IO_PAD_CSIE, 45, UINT_MAX, "csie"), \
2062 _pad(TEGRA_IO_PAD_CSIF, 46, UINT_MAX, "csif"), \
2063 _pad(TEGRA_IO_PAD_SPI, 47, UINT_MAX, "spi"), \
2064 _pad(TEGRA_IO_PAD_UFS, 49, UINT_MAX, "ufs"), \
2065 _pad(TEGRA_IO_PAD_DMIC_HV, 52, 2, "dmic-hv"), \
2066 _pad(TEGRA_IO_PAD_EDP, 53, UINT_MAX, "edp"), \
2067 _pad(TEGRA_IO_PAD_SDMMC1_HV, 55, 4, "sdmmc1-hv"), \
2068 _pad(TEGRA_IO_PAD_SDMMC3_HV, 56, 6, "sdmmc3-hv"), \
2069 _pad(TEGRA_IO_PAD_CONN, 60, UINT_MAX, "conn"), \
2070 _pad(TEGRA_IO_PAD_AUDIO_HV, 61, 1, "audio-hv"), \
2071 _pad(TEGRA_IO_PAD_AO_HV, UINT_MAX, 0, "ao-hv")
2073 static const struct tegra_io_pad_soc tegra186_io_pads[] = {
2074 TEGRA186_IO_PAD_TABLE(TEGRA_IO_PAD)
2077 static const struct pinctrl_pin_desc tegra186_pin_descs[] = {
2078 TEGRA186_IO_PAD_TABLE(TEGRA_IO_PIN_DESC)
2081 static const struct tegra_pmc_regs tegra186_pmc_regs = {
2086 .dpd2_status = 0x80,
2089 static void tegra186_pmc_setup_irq_polarity(struct tegra_pmc *pmc,
2090 struct device_node *np,
2093 struct resource regs;
2098 index = of_property_match_string(np, "reg-names", "wake");
2100 pr_err("failed to find PMC wake registers\n");
2104 of_address_to_resource(np, index, ®s);
2106 wake = ioremap_nocache(regs.start, resource_size(®s));
2108 pr_err("failed to map PMC wake registers\n");
2112 value = readl(wake + WAKE_AOWAKE_CTRL);
2115 value |= WAKE_AOWAKE_CTRL_INTR_POLARITY;
2117 value &= ~WAKE_AOWAKE_CTRL_INTR_POLARITY;
2119 writel(value, wake + WAKE_AOWAKE_CTRL);
2124 static const struct tegra_pmc_soc tegra186_pmc_soc = {
2125 .num_powergates = 0,
2127 .num_cpu_powergates = 0,
2128 .cpu_powergates = NULL,
2129 .has_tsense_reset = false,
2130 .has_gpu_clamps = false,
2131 .has_impl_33v_pwr = true,
2132 .num_io_pads = ARRAY_SIZE(tegra186_io_pads),
2133 .io_pads = tegra186_io_pads,
2134 .num_pin_descs = ARRAY_SIZE(tegra186_pin_descs),
2135 .pin_descs = tegra186_pin_descs,
2136 .regs = &tegra186_pmc_regs,
2138 .setup_irq_polarity = tegra186_pmc_setup_irq_polarity,
2141 static const struct of_device_id tegra_pmc_match[] = {
2142 { .compatible = "nvidia,tegra194-pmc", .data = &tegra186_pmc_soc },
2143 { .compatible = "nvidia,tegra186-pmc", .data = &tegra186_pmc_soc },
2144 { .compatible = "nvidia,tegra210-pmc", .data = &tegra210_pmc_soc },
2145 { .compatible = "nvidia,tegra132-pmc", .data = &tegra124_pmc_soc },
2146 { .compatible = "nvidia,tegra124-pmc", .data = &tegra124_pmc_soc },
2147 { .compatible = "nvidia,tegra114-pmc", .data = &tegra114_pmc_soc },
2148 { .compatible = "nvidia,tegra30-pmc", .data = &tegra30_pmc_soc },
2149 { .compatible = "nvidia,tegra20-pmc", .data = &tegra20_pmc_soc },
2153 static struct platform_driver tegra_pmc_driver = {
2155 .name = "tegra-pmc",
2156 .suppress_bind_attrs = true,
2157 .of_match_table = tegra_pmc_match,
2158 #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM)
2159 .pm = &tegra_pmc_pm_ops,
2162 .probe = tegra_pmc_probe,
2164 builtin_platform_driver(tegra_pmc_driver);
2167 * Early initialization to allow access to registers in the very early boot
2170 static int __init tegra_pmc_early_init(void)
2172 const struct of_device_id *match;
2173 struct device_node *np;
2174 struct resource regs;
2177 mutex_init(&pmc->powergates_lock);
2179 np = of_find_matching_node_and_match(NULL, tegra_pmc_match, &match);
2182 * Fall back to legacy initialization for 32-bit ARM only. All
2183 * 64-bit ARM device tree files for Tegra are required to have
2186 * This is for backwards-compatibility with old device trees
2187 * that didn't contain a PMC node. Note that in this case the
2188 * SoC data can't be matched and therefore powergating is
2191 if (IS_ENABLED(CONFIG_ARM) && soc_is_tegra()) {
2192 pr_warn("DT node not found, powergating disabled\n");
2194 regs.start = 0x7000e400;
2195 regs.end = 0x7000e7ff;
2196 regs.flags = IORESOURCE_MEM;
2198 pr_warn("Using memory region %pR\n", ®s);
2201 * At this point we're not running on Tegra, so play
2202 * nice with multi-platform kernels.
2208 * Extract information from the device tree if we've found a
2211 if (of_address_to_resource(np, 0, ®s) < 0) {
2212 pr_err("failed to get PMC registers\n");
2218 pmc->base = ioremap_nocache(regs.start, resource_size(®s));
2220 pr_err("failed to map PMC registers\n");
2226 pmc->soc = match->data;
2228 tegra_powergate_init(pmc, np);
2231 * Invert the interrupt polarity if a PMC device tree node
2232 * exists and contains the nvidia,invert-interrupt property.
2234 invert = of_property_read_bool(np, "nvidia,invert-interrupt");
2236 pmc->soc->setup_irq_polarity(pmc, np, invert);
2243 early_initcall(tegra_pmc_early_init);