blob: 0c5f79528e5f7f202518d020a12729e869104554 [file] [log] [blame]
Thierry Reding72323982014-07-11 13:19:06 +02001/*
2 * drivers/soc/tegra/pmc.c
3 *
4 * Copyright (c) 2010 Google, Inc
Sandipan Patra5f84bb12018-10-24 12:38:00 +05305 * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
Thierry Reding72323982014-07-11 13:19:06 +02006 *
7 * Author:
8 * Colin Cross <ccross@google.com>
9 *
10 * This software is licensed under the terms of the GNU General Public
11 * License version 2, as published by the Free Software Foundation, and
12 * may be copied, distributed, and modified under those terms.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 */
20
Thierry Reding7d71e9032015-04-29 12:42:28 +020021#define pr_fmt(fmt) "tegra-pmc: " fmt
22
Mikko Perttunene247dea2019-01-25 11:22:55 +010023#include <linux/arm-smccc.h>
Thierry Reding72323982014-07-11 13:19:06 +020024#include <linux/clk.h>
25#include <linux/clk/tegra.h>
26#include <linux/debugfs.h>
27#include <linux/delay.h>
28#include <linux/err.h>
29#include <linux/export.h>
30#include <linux/init.h>
31#include <linux/io.h>
Jon Hunter0a2d87e2016-02-26 15:48:40 +000032#include <linux/iopoll.h>
Thierry Reding19906e62018-09-17 15:08:17 +020033#include <linux/irqdomain.h>
Thierry Reding4659db52019-01-25 11:22:49 +010034#include <linux/irq.h>
35#include <linux/kernel.h>
Thierry Reding72323982014-07-11 13:19:06 +020036#include <linux/of_address.h>
Geert Uytterhoeven3fd01212018-04-18 16:50:04 +020037#include <linux/of_clk.h>
Thierry Reding4659db52019-01-25 11:22:49 +010038#include <linux/of.h>
Thierry Reding19906e62018-09-17 15:08:17 +020039#include <linux/of_irq.h>
Jon Huntera3804512016-03-30 10:15:15 +010040#include <linux/of_platform.h>
Aapo Vienamo4a37f112018-08-10 21:08:12 +030041#include <linux/pinctrl/pinconf-generic.h>
Thierry Reding4659db52019-01-25 11:22:49 +010042#include <linux/pinctrl/pinconf.h>
43#include <linux/pinctrl/pinctrl.h>
Thierry Reding72323982014-07-11 13:19:06 +020044#include <linux/platform_device.h>
Jon Huntera3804512016-03-30 10:15:15 +010045#include <linux/pm_domain.h>
Thierry Reding72323982014-07-11 13:19:06 +020046#include <linux/reboot.h>
47#include <linux/reset.h>
48#include <linux/seq_file.h>
Jon Huntera3804512016-03-30 10:15:15 +010049#include <linux/slab.h>
Thierry Reding72323982014-07-11 13:19:06 +020050#include <linux/spinlock.h>
51
52#include <soc/tegra/common.h>
53#include <soc/tegra/fuse.h>
54#include <soc/tegra/pmc.h>
55
Thierry Reding19906e62018-09-17 15:08:17 +020056#include <dt-bindings/interrupt-controller/arm-gic.h>
Aapo Vienamofccf0f72018-08-10 21:08:11 +030057#include <dt-bindings/pinctrl/pinctrl-tegra-io-pad.h>
Thierry Redinge59333c2018-09-19 18:41:59 +020058#include <dt-bindings/gpio/tegra186-gpio.h>
Thierry Redinge3e403c2018-09-19 18:42:37 +020059#include <dt-bindings/gpio/tegra194-gpio.h>
Aapo Vienamofccf0f72018-08-10 21:08:11 +030060
Thierry Reding72323982014-07-11 13:19:06 +020061#define PMC_CNTRL 0x0
Laxman Dewangan6c0bd212016-06-17 18:36:12 +053062#define PMC_CNTRL_INTR_POLARITY BIT(17) /* inverts INTR polarity */
Thierry Reding95b780b2016-10-10 13:13:36 +020063#define PMC_CNTRL_CPU_PWRREQ_OE BIT(16) /* CPU pwr req enable */
64#define PMC_CNTRL_CPU_PWRREQ_POLARITY BIT(15) /* CPU pwr req polarity */
65#define PMC_CNTRL_SIDE_EFFECT_LP0 BIT(14) /* LP0 when CPU pwr gated */
66#define PMC_CNTRL_SYSCLK_OE BIT(11) /* system clock enable */
67#define PMC_CNTRL_SYSCLK_POLARITY BIT(10) /* sys clk polarity */
68#define PMC_CNTRL_MAIN_RST BIT(4)
Thierry Reding72323982014-07-11 13:19:06 +020069
70#define DPD_SAMPLE 0x020
Laxman Dewangan6c0bd212016-06-17 18:36:12 +053071#define DPD_SAMPLE_ENABLE BIT(0)
Thierry Reding72323982014-07-11 13:19:06 +020072#define DPD_SAMPLE_DISABLE (0 << 0)
73
74#define PWRGATE_TOGGLE 0x30
Laxman Dewangan6c0bd212016-06-17 18:36:12 +053075#define PWRGATE_TOGGLE_START BIT(8)
Thierry Reding72323982014-07-11 13:19:06 +020076
77#define REMOVE_CLAMPING 0x34
78
79#define PWRGATE_STATUS 0x38
80
Aapo Vienamo13136a42018-08-10 21:08:07 +030081#define PMC_IMPL_E_33V_PWR 0x40
82
Laxman Dewangan21b49912016-10-10 15:14:34 +020083#define PMC_PWR_DET 0x48
84
Thierry Reding5be22552017-08-30 12:32:58 +020085#define PMC_SCRATCH0_MODE_RECOVERY BIT(31)
86#define PMC_SCRATCH0_MODE_BOOTLOADER BIT(30)
87#define PMC_SCRATCH0_MODE_RCM BIT(1)
88#define PMC_SCRATCH0_MODE_MASK (PMC_SCRATCH0_MODE_RECOVERY | \
Thierry Reding72323982014-07-11 13:19:06 +020089 PMC_SCRATCH0_MODE_BOOTLOADER | \
90 PMC_SCRATCH0_MODE_RCM)
91
92#define PMC_CPUPWRGOOD_TIMER 0xc8
93#define PMC_CPUPWROFF_TIMER 0xcc
94
Laxman Dewangan21b49912016-10-10 15:14:34 +020095#define PMC_PWR_DET_VALUE 0xe4
96
Thierry Reding72323982014-07-11 13:19:06 +020097#define PMC_SCRATCH41 0x140
98
Mikko Perttunen3568df32015-01-06 12:52:58 +020099#define PMC_SENSOR_CTRL 0x1b0
Laxman Dewangan6c0bd212016-06-17 18:36:12 +0530100#define PMC_SENSOR_CTRL_SCRATCH_WRITE BIT(2)
101#define PMC_SENSOR_CTRL_ENABLE_RST BIT(1)
Mikko Perttunen3568df32015-01-06 12:52:58 +0200102
Thierry Redingf5353c62015-12-30 17:13:29 +0100103#define PMC_RST_STATUS_POR 0
104#define PMC_RST_STATUS_WATCHDOG 1
105#define PMC_RST_STATUS_SENSOR 2
106#define PMC_RST_STATUS_SW_MAIN 3
107#define PMC_RST_STATUS_LP0 4
108#define PMC_RST_STATUS_AOTAG 5
109
Thierry Reding72323982014-07-11 13:19:06 +0200110#define IO_DPD_REQ 0x1b8
Laxman Dewangan6c0bd212016-06-17 18:36:12 +0530111#define IO_DPD_REQ_CODE_IDLE (0U << 30)
112#define IO_DPD_REQ_CODE_OFF (1U << 30)
113#define IO_DPD_REQ_CODE_ON (2U << 30)
114#define IO_DPD_REQ_CODE_MASK (3U << 30)
Thierry Reding72323982014-07-11 13:19:06 +0200115
116#define IO_DPD_STATUS 0x1bc
117#define IO_DPD2_REQ 0x1c0
118#define IO_DPD2_STATUS 0x1c4
119#define SEL_DPD_TIM 0x1c8
120
Mikko Perttunen3568df32015-01-06 12:52:58 +0200121#define PMC_SCRATCH54 0x258
Laxman Dewangan6c0bd212016-06-17 18:36:12 +0530122#define PMC_SCRATCH54_DATA_SHIFT 8
123#define PMC_SCRATCH54_ADDR_SHIFT 0
Mikko Perttunen3568df32015-01-06 12:52:58 +0200124
125#define PMC_SCRATCH55 0x25c
Laxman Dewangan6c0bd212016-06-17 18:36:12 +0530126#define PMC_SCRATCH55_RESET_TEGRA BIT(31)
127#define PMC_SCRATCH55_CNTRL_ID_SHIFT 27
128#define PMC_SCRATCH55_PINMUX_SHIFT 24
129#define PMC_SCRATCH55_16BITOP BIT(15)
130#define PMC_SCRATCH55_CHECKSUM_SHIFT 16
131#define PMC_SCRATCH55_I2CSLV1_SHIFT 0
Mikko Perttunen3568df32015-01-06 12:52:58 +0200132
Thierry Reding72323982014-07-11 13:19:06 +0200133#define GPU_RG_CNTRL 0x2d4
134
Thierry Redingc641ec62017-08-30 12:42:34 +0200135/* Tegra186 and later */
Thierry Reding19906e62018-09-17 15:08:17 +0200136#define WAKE_AOWAKE_CNTRL(x) (0x000 + ((x) << 2))
137#define WAKE_AOWAKE_CNTRL_LEVEL (1 << 3)
138#define WAKE_AOWAKE_MASK_W(x) (0x180 + ((x) << 2))
139#define WAKE_AOWAKE_MASK_R(x) (0x300 + ((x) << 2))
140#define WAKE_AOWAKE_STATUS_W(x) (0x30c + ((x) << 2))
141#define WAKE_AOWAKE_STATUS_R(x) (0x48c + ((x) << 2))
142#define WAKE_AOWAKE_TIER0_ROUTING(x) (0x4b4 + ((x) << 2))
143#define WAKE_AOWAKE_TIER1_ROUTING(x) (0x4c0 + ((x) << 2))
144#define WAKE_AOWAKE_TIER2_ROUTING(x) (0x4cc + ((x) << 2))
145
Thierry Redingc641ec62017-08-30 12:42:34 +0200146#define WAKE_AOWAKE_CTRL 0x4f4
147#define WAKE_AOWAKE_CTRL_INTR_POLARITY BIT(0)
148
Mikko Perttunene247dea2019-01-25 11:22:55 +0100149/* for secure PMC */
150#define TEGRA_SMC_PMC 0xc2fffe00
151#define TEGRA_SMC_PMC_READ 0xaa
152#define TEGRA_SMC_PMC_WRITE 0xbb
153
Jon Huntera3804512016-03-30 10:15:15 +0100154struct tegra_powergate {
155 struct generic_pm_domain genpd;
156 struct tegra_pmc *pmc;
157 unsigned int id;
158 struct clk **clks;
159 unsigned int num_clks;
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200160 struct reset_control *reset;
Jon Huntera3804512016-03-30 10:15:15 +0100161};
162
Laxman Dewangan21b49912016-10-10 15:14:34 +0200163struct tegra_io_pad_soc {
164 enum tegra_io_pad id;
165 unsigned int dpd;
166 unsigned int voltage;
Aapo Vienamo437c4f22018-08-10 21:08:10 +0300167 const char *name;
Laxman Dewangan21b49912016-10-10 15:14:34 +0200168};
169
Thierry Reding5be22552017-08-30 12:32:58 +0200170struct tegra_pmc_regs {
171 unsigned int scratch0;
172 unsigned int dpd_req;
173 unsigned int dpd_status;
174 unsigned int dpd2_req;
175 unsigned int dpd2_status;
Sandipan Patra5f84bb12018-10-24 12:38:00 +0530176 unsigned int rst_status;
177 unsigned int rst_source_shift;
178 unsigned int rst_source_mask;
179 unsigned int rst_level_shift;
180 unsigned int rst_level_mask;
Thierry Reding5be22552017-08-30 12:32:58 +0200181};
182
Thierry Reding19906e62018-09-17 15:08:17 +0200183struct tegra_wake_event {
184 const char *name;
185 unsigned int id;
186 unsigned int irq;
187 struct {
188 unsigned int instance;
189 unsigned int pin;
190 } gpio;
191};
192
193#define TEGRA_WAKE_IRQ(_name, _id, _irq) \
194 { \
195 .name = _name, \
196 .id = _id, \
197 .irq = _irq, \
198 .gpio = { \
199 .instance = UINT_MAX, \
200 .pin = UINT_MAX, \
201 }, \
202 }
203
204#define TEGRA_WAKE_GPIO(_name, _id, _instance, _pin) \
205 { \
206 .name = _name, \
207 .id = _id, \
208 .irq = 0, \
209 .gpio = { \
210 .instance = _instance, \
211 .pin = _pin, \
212 }, \
213 }
214
Thierry Reding72323982014-07-11 13:19:06 +0200215struct tegra_pmc_soc {
216 unsigned int num_powergates;
217 const char *const *powergates;
218 unsigned int num_cpu_powergates;
219 const u8 *cpu_powergates;
Thierry Redinga9a40a42015-01-09 11:15:33 +0100220
Mikko Perttunen3568df32015-01-06 12:52:58 +0200221 bool has_tsense_reset;
Thierry Redinga9a40a42015-01-09 11:15:33 +0100222 bool has_gpu_clamps;
Peter De Schrijvera263394a2018-01-25 16:00:13 +0200223 bool needs_mbist_war;
Aapo Vienamo13136a42018-08-10 21:08:07 +0300224 bool has_impl_33v_pwr;
Mikko Perttunene247dea2019-01-25 11:22:55 +0100225 bool maybe_tz_only;
Laxman Dewangan21b49912016-10-10 15:14:34 +0200226
227 const struct tegra_io_pad_soc *io_pads;
228 unsigned int num_io_pads;
Thierry Reding5be22552017-08-30 12:32:58 +0200229
Aapo Vienamo4a37f112018-08-10 21:08:12 +0300230 const struct pinctrl_pin_desc *pin_descs;
231 unsigned int num_pin_descs;
232
Thierry Reding5be22552017-08-30 12:32:58 +0200233 const struct tegra_pmc_regs *regs;
234 void (*init)(struct tegra_pmc *pmc);
235 void (*setup_irq_polarity)(struct tegra_pmc *pmc,
236 struct device_node *np,
237 bool invert);
Sandipan Patra5f84bb12018-10-24 12:38:00 +0530238
239 const char * const *reset_sources;
240 unsigned int num_reset_sources;
241 const char * const *reset_levels;
242 unsigned int num_reset_levels;
Thierry Reding19906e62018-09-17 15:08:17 +0200243
244 const struct tegra_wake_event *wake_events;
245 unsigned int num_wake_events;
Sandipan Patra5f84bb12018-10-24 12:38:00 +0530246};
247
248static const char * const tegra186_reset_sources[] = {
249 "SYS_RESET",
250 "AOWDT",
251 "MCCPLEXWDT",
252 "BPMPWDT",
253 "SCEWDT",
254 "SPEWDT",
255 "APEWDT",
256 "BCCPLEXWDT",
257 "SENSOR",
258 "AOTAG",
259 "VFSENSOR",
260 "SWREST",
261 "SC7",
262 "HSM",
263 "CORESIGHT"
264};
265
266static const char * const tegra186_reset_levels[] = {
267 "L0", "L1", "L2", "WARM"
268};
269
270static const char * const tegra30_reset_sources[] = {
271 "POWER_ON_RESET",
272 "WATCHDOG",
273 "SENSOR",
274 "SW_MAIN",
275 "LP0",
276 "AOTAG"
Thierry Reding72323982014-07-11 13:19:06 +0200277};
278
279/**
280 * struct tegra_pmc - NVIDIA Tegra PMC
Jon Hunter35b67292015-12-04 14:57:03 +0000281 * @dev: pointer to PMC device structure
Thierry Reding72323982014-07-11 13:19:06 +0200282 * @base: pointer to I/O remapped register region
Thierry Redingbbe5af62019-01-25 11:22:50 +0100283 * @wake: pointer to I/O remapped region for WAKE registers
284 * @aotag: pointer to I/O remapped region for AOTAG registers
285 * @scratch: pointer to I/O remapped region for scratch registers
Thierry Reding72323982014-07-11 13:19:06 +0200286 * @clk: pointer to pclk clock
Jon Hunter35b67292015-12-04 14:57:03 +0000287 * @soc: pointer to SoC data structure
Mikko Perttunene247dea2019-01-25 11:22:55 +0100288 * @tz_only: flag specifying if the PMC can only be accessed via TrustZone
Jon Hunter3195ac62015-12-04 14:57:05 +0000289 * @debugfs: pointer to debugfs entry
Thierry Reding72323982014-07-11 13:19:06 +0200290 * @rate: currently configured rate of pclk
291 * @suspend_mode: lowest suspend mode available
292 * @cpu_good_time: CPU power good time (in microseconds)
293 * @cpu_off_time: CPU power off time (in microsecends)
294 * @core_osc_time: core power good OSC time (in microseconds)
295 * @core_pmu_time: core power good PMU time (in microseconds)
296 * @core_off_time: core power off time (in microseconds)
297 * @corereq_high: core power request is active-high
298 * @sysclkreq_high: system clock request is active-high
299 * @combined_req: combined power request for CPU & core
300 * @cpu_pwr_good_en: CPU power good signal is enabled
301 * @lp0_vec_phys: physical base address of the LP0 warm boot code
302 * @lp0_vec_size: size of the LP0 warm boot code
Jon Huntera3804512016-03-30 10:15:15 +0100303 * @powergates_available: Bitmap of available power gates
Thierry Reding72323982014-07-11 13:19:06 +0200304 * @powergates_lock: mutex for power gate register access
Thierry Redingbbe5af62019-01-25 11:22:50 +0100305 * @pctl_dev: pin controller exposed by the PMC
306 * @domain: IRQ domain provided by the PMC
307 * @irq: chip implementation for the IRQ domain
Thierry Reding72323982014-07-11 13:19:06 +0200308 */
309struct tegra_pmc {
Mikko Perttunen3568df32015-01-06 12:52:58 +0200310 struct device *dev;
Thierry Reding72323982014-07-11 13:19:06 +0200311 void __iomem *base;
Thierry Redingc641ec62017-08-30 12:42:34 +0200312 void __iomem *wake;
313 void __iomem *aotag;
Thierry Reding5be22552017-08-30 12:32:58 +0200314 void __iomem *scratch;
Thierry Reding72323982014-07-11 13:19:06 +0200315 struct clk *clk;
Jon Hunter3195ac62015-12-04 14:57:05 +0000316 struct dentry *debugfs;
Thierry Reding72323982014-07-11 13:19:06 +0200317
318 const struct tegra_pmc_soc *soc;
Mikko Perttunene247dea2019-01-25 11:22:55 +0100319 bool tz_only;
Thierry Reding72323982014-07-11 13:19:06 +0200320
321 unsigned long rate;
322
323 enum tegra_suspend_mode suspend_mode;
324 u32 cpu_good_time;
325 u32 cpu_off_time;
326 u32 core_osc_time;
327 u32 core_pmu_time;
328 u32 core_off_time;
329 bool corereq_high;
330 bool sysclkreq_high;
331 bool combined_req;
332 bool cpu_pwr_good_en;
333 u32 lp0_vec_phys;
334 u32 lp0_vec_size;
Jon Huntera3804512016-03-30 10:15:15 +0100335 DECLARE_BITMAP(powergates_available, TEGRA_POWERGATE_MAX);
Thierry Reding72323982014-07-11 13:19:06 +0200336
337 struct mutex powergates_lock;
Aapo Vienamo4a37f112018-08-10 21:08:12 +0300338
339 struct pinctrl_dev *pctl_dev;
Thierry Reding19906e62018-09-17 15:08:17 +0200340
341 struct irq_domain *domain;
342 struct irq_chip irq;
Thierry Reding72323982014-07-11 13:19:06 +0200343};
344
345static struct tegra_pmc *pmc = &(struct tegra_pmc) {
346 .base = NULL,
347 .suspend_mode = TEGRA_SUSPEND_NONE,
348};
349
Jon Huntera3804512016-03-30 10:15:15 +0100350static inline struct tegra_powergate *
351to_powergate(struct generic_pm_domain *domain)
352{
353 return container_of(domain, struct tegra_powergate, genpd);
354}
355
Thierry Reding589997a2019-01-25 11:22:52 +0100356static u32 tegra_pmc_readl(struct tegra_pmc *pmc, unsigned long offset)
Thierry Reding72323982014-07-11 13:19:06 +0200357{
Mikko Perttunene247dea2019-01-25 11:22:55 +0100358 struct arm_smccc_res res;
359
360 if (pmc->tz_only) {
361 arm_smccc_smc(TEGRA_SMC_PMC, TEGRA_SMC_PMC_READ, offset, 0, 0,
362 0, 0, 0, &res);
363 if (res.a0) {
364 if (pmc->dev)
365 dev_warn(pmc->dev, "%s(): SMC failed: %lu\n",
366 __func__, res.a0);
367 else
368 pr_warn("%s(): SMC failed: %lu\n", __func__,
369 res.a0);
370 }
371
372 return res.a1;
373 }
374
Thierry Reding72323982014-07-11 13:19:06 +0200375 return readl(pmc->base + offset);
376}
377
Thierry Reding589997a2019-01-25 11:22:52 +0100378static void tegra_pmc_writel(struct tegra_pmc *pmc, u32 value,
379 unsigned long offset)
Thierry Reding72323982014-07-11 13:19:06 +0200380{
Mikko Perttunene247dea2019-01-25 11:22:55 +0100381 struct arm_smccc_res res;
382
383 if (pmc->tz_only) {
384 arm_smccc_smc(TEGRA_SMC_PMC, TEGRA_SMC_PMC_WRITE, offset,
385 value, 0, 0, 0, 0, &res);
386 if (res.a0) {
387 if (pmc->dev)
388 dev_warn(pmc->dev, "%s(): SMC failed: %lu\n",
389 __func__, res.a0);
390 else
391 pr_warn("%s(): SMC failed: %lu\n", __func__,
392 res.a0);
393 }
394 } else {
395 writel(value, pmc->base + offset);
396 }
397}
398
399static u32 tegra_pmc_scratch_readl(struct tegra_pmc *pmc, unsigned long offset)
400{
401 if (pmc->tz_only)
402 return tegra_pmc_readl(pmc, offset);
403
404 return readl(pmc->scratch + offset);
405}
406
407static void tegra_pmc_scratch_writel(struct tegra_pmc *pmc, u32 value,
408 unsigned long offset)
409{
410 if (pmc->tz_only)
411 tegra_pmc_writel(pmc, value, offset);
412 else
413 writel(value, pmc->scratch + offset);
Thierry Reding72323982014-07-11 13:19:06 +0200414}
415
Thierry Reding589997a2019-01-25 11:22:52 +0100416/*
417 * TODO Figure out a way to call this with the struct tegra_pmc * passed in.
418 * This currently doesn't work because readx_poll_timeout() can only operate
419 * on functions that take a single argument.
420 */
Jon Hunter0ecf2d32016-02-11 18:03:23 +0000421static inline bool tegra_powergate_state(int id)
422{
Jon Hunterbc9af232016-02-15 12:38:11 +0000423 if (id == TEGRA_POWERGATE_3D && pmc->soc->has_gpu_clamps)
Thierry Reding589997a2019-01-25 11:22:52 +0100424 return (tegra_pmc_readl(pmc, GPU_RG_CNTRL) & 0x1) == 0;
Jon Hunterbc9af232016-02-15 12:38:11 +0000425 else
Thierry Reding589997a2019-01-25 11:22:52 +0100426 return (tegra_pmc_readl(pmc, PWRGATE_STATUS) & BIT(id)) != 0;
Jon Hunter0ecf2d32016-02-11 18:03:23 +0000427}
428
Thierry Reding589997a2019-01-25 11:22:52 +0100429static inline bool tegra_powergate_is_valid(struct tegra_pmc *pmc, int id)
Jon Hunter0a243bd2016-02-11 18:03:24 +0000430{
431 return (pmc->soc && pmc->soc->powergates[id]);
432}
433
Thierry Reding589997a2019-01-25 11:22:52 +0100434static inline bool tegra_powergate_is_available(struct tegra_pmc *pmc, int id)
Jon Huntera3804512016-03-30 10:15:15 +0100435{
436 return test_bit(id, pmc->powergates_available);
437}
438
439static int tegra_powergate_lookup(struct tegra_pmc *pmc, const char *name)
440{
441 unsigned int i;
442
443 if (!pmc || !pmc->soc || !name)
444 return -EINVAL;
445
446 for (i = 0; i < pmc->soc->num_powergates; i++) {
Thierry Reding589997a2019-01-25 11:22:52 +0100447 if (!tegra_powergate_is_valid(pmc, i))
Jon Huntera3804512016-03-30 10:15:15 +0100448 continue;
449
450 if (!strcmp(name, pmc->soc->powergates[i]))
451 return i;
452 }
453
Jon Huntera3804512016-03-30 10:15:15 +0100454 return -ENODEV;
455}
456
Thierry Reding72323982014-07-11 13:19:06 +0200457/**
458 * tegra_powergate_set() - set the state of a partition
Thierry Reding589997a2019-01-25 11:22:52 +0100459 * @pmc: power management controller
Thierry Reding72323982014-07-11 13:19:06 +0200460 * @id: partition ID
461 * @new_state: new state of the partition
462 */
Thierry Reding589997a2019-01-25 11:22:52 +0100463static int tegra_powergate_set(struct tegra_pmc *pmc, unsigned int id,
464 bool new_state)
Thierry Reding72323982014-07-11 13:19:06 +0200465{
Jon Hunter0a2d87e2016-02-26 15:48:40 +0000466 bool status;
467 int err;
468
Jon Hunterbc9af232016-02-15 12:38:11 +0000469 if (id == TEGRA_POWERGATE_3D && pmc->soc->has_gpu_clamps)
470 return -EINVAL;
471
Thierry Reding72323982014-07-11 13:19:06 +0200472 mutex_lock(&pmc->powergates_lock);
473
Jon Hunter0ecf2d32016-02-11 18:03:23 +0000474 if (tegra_powergate_state(id) == new_state) {
Thierry Reding72323982014-07-11 13:19:06 +0200475 mutex_unlock(&pmc->powergates_lock);
476 return 0;
477 }
478
Thierry Reding589997a2019-01-25 11:22:52 +0100479 tegra_pmc_writel(pmc, PWRGATE_TOGGLE_START | id, PWRGATE_TOGGLE);
Thierry Reding72323982014-07-11 13:19:06 +0200480
Jon Hunter0a2d87e2016-02-26 15:48:40 +0000481 err = readx_poll_timeout(tegra_powergate_state, id, status,
482 status == new_state, 10, 100000);
483
Thierry Reding72323982014-07-11 13:19:06 +0200484 mutex_unlock(&pmc->powergates_lock);
485
Jon Hunter0a2d87e2016-02-26 15:48:40 +0000486 return err;
Thierry Reding72323982014-07-11 13:19:06 +0200487}
488
Thierry Reding589997a2019-01-25 11:22:52 +0100489static int __tegra_powergate_remove_clamping(struct tegra_pmc *pmc,
490 unsigned int id)
Thierry Reding72323982014-07-11 13:19:06 +0200491{
492 u32 mask;
493
Jon Huntere8cf6612016-02-11 18:03:21 +0000494 mutex_lock(&pmc->powergates_lock);
495
Thierry Reding72323982014-07-11 13:19:06 +0200496 /*
Thierry Redinga9a40a42015-01-09 11:15:33 +0100497 * On Tegra124 and later, the clamps for the GPU are controlled by a
498 * separate register (with different semantics).
Thierry Reding72323982014-07-11 13:19:06 +0200499 */
Thierry Redinga9a40a42015-01-09 11:15:33 +0100500 if (id == TEGRA_POWERGATE_3D) {
501 if (pmc->soc->has_gpu_clamps) {
Thierry Reding589997a2019-01-25 11:22:52 +0100502 tegra_pmc_writel(pmc, 0, GPU_RG_CNTRL);
Jon Huntere8cf6612016-02-11 18:03:21 +0000503 goto out;
Thierry Reding72323982014-07-11 13:19:06 +0200504 }
505 }
506
507 /*
508 * Tegra 2 has a bug where PCIE and VDE clamping masks are
509 * swapped relatively to the partition ids
510 */
511 if (id == TEGRA_POWERGATE_VDEC)
512 mask = (1 << TEGRA_POWERGATE_PCIE);
513 else if (id == TEGRA_POWERGATE_PCIE)
514 mask = (1 << TEGRA_POWERGATE_VDEC);
515 else
516 mask = (1 << id);
517
Thierry Reding589997a2019-01-25 11:22:52 +0100518 tegra_pmc_writel(pmc, mask, REMOVE_CLAMPING);
Thierry Reding72323982014-07-11 13:19:06 +0200519
Jon Huntere8cf6612016-02-11 18:03:21 +0000520out:
521 mutex_unlock(&pmc->powergates_lock);
522
Thierry Reding72323982014-07-11 13:19:06 +0200523 return 0;
524}
Jon Huntera3804512016-03-30 10:15:15 +0100525
526static void tegra_powergate_disable_clocks(struct tegra_powergate *pg)
527{
528 unsigned int i;
529
530 for (i = 0; i < pg->num_clks; i++)
531 clk_disable_unprepare(pg->clks[i]);
532}
533
534static int tegra_powergate_enable_clocks(struct tegra_powergate *pg)
535{
536 unsigned int i;
537 int err;
538
539 for (i = 0; i < pg->num_clks; i++) {
540 err = clk_prepare_enable(pg->clks[i]);
541 if (err)
542 goto out;
543 }
544
545 return 0;
546
547out:
548 while (i--)
549 clk_disable_unprepare(pg->clks[i]);
550
551 return err;
552}
553
Peter De Schrijvera263394a2018-01-25 16:00:13 +0200554int __weak tegra210_clk_handle_mbist_war(unsigned int id)
555{
556 return 0;
557}
558
Jon Huntera3804512016-03-30 10:15:15 +0100559static int tegra_powergate_power_up(struct tegra_powergate *pg,
560 bool disable_clocks)
561{
562 int err;
563
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200564 err = reset_control_assert(pg->reset);
Jon Huntera3804512016-03-30 10:15:15 +0100565 if (err)
566 return err;
567
568 usleep_range(10, 20);
569
Thierry Reding589997a2019-01-25 11:22:52 +0100570 err = tegra_powergate_set(pg->pmc, pg->id, true);
Jon Huntera3804512016-03-30 10:15:15 +0100571 if (err < 0)
572 return err;
573
574 usleep_range(10, 20);
575
576 err = tegra_powergate_enable_clocks(pg);
577 if (err)
578 goto disable_clks;
579
580 usleep_range(10, 20);
581
Thierry Reding589997a2019-01-25 11:22:52 +0100582 err = __tegra_powergate_remove_clamping(pg->pmc, pg->id);
Jon Huntera3804512016-03-30 10:15:15 +0100583 if (err)
584 goto disable_clks;
585
586 usleep_range(10, 20);
587
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200588 err = reset_control_deassert(pg->reset);
Jon Huntera3804512016-03-30 10:15:15 +0100589 if (err)
590 goto powergate_off;
591
592 usleep_range(10, 20);
593
Peter De Schrijvera263394a2018-01-25 16:00:13 +0200594 if (pg->pmc->soc->needs_mbist_war)
595 err = tegra210_clk_handle_mbist_war(pg->id);
596 if (err)
597 goto disable_clks;
598
Jon Huntera3804512016-03-30 10:15:15 +0100599 if (disable_clocks)
600 tegra_powergate_disable_clocks(pg);
601
602 return 0;
603
604disable_clks:
605 tegra_powergate_disable_clocks(pg);
606 usleep_range(10, 20);
Thierry Redingda8f4b42016-06-30 12:12:55 +0200607
Jon Huntera3804512016-03-30 10:15:15 +0100608powergate_off:
Thierry Reding589997a2019-01-25 11:22:52 +0100609 tegra_powergate_set(pg->pmc, pg->id, false);
Jon Huntera3804512016-03-30 10:15:15 +0100610
611 return err;
612}
613
614static int tegra_powergate_power_down(struct tegra_powergate *pg)
615{
616 int err;
617
618 err = tegra_powergate_enable_clocks(pg);
619 if (err)
620 return err;
621
622 usleep_range(10, 20);
623
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200624 err = reset_control_assert(pg->reset);
Jon Huntera3804512016-03-30 10:15:15 +0100625 if (err)
626 goto disable_clks;
627
628 usleep_range(10, 20);
629
630 tegra_powergate_disable_clocks(pg);
631
632 usleep_range(10, 20);
633
Thierry Reding589997a2019-01-25 11:22:52 +0100634 err = tegra_powergate_set(pg->pmc, pg->id, false);
Jon Huntera3804512016-03-30 10:15:15 +0100635 if (err)
636 goto assert_resets;
637
638 return 0;
639
640assert_resets:
641 tegra_powergate_enable_clocks(pg);
642 usleep_range(10, 20);
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200643 reset_control_deassert(pg->reset);
Jon Huntera3804512016-03-30 10:15:15 +0100644 usleep_range(10, 20);
Thierry Redingda8f4b42016-06-30 12:12:55 +0200645
Jon Huntera3804512016-03-30 10:15:15 +0100646disable_clks:
647 tegra_powergate_disable_clocks(pg);
648
649 return err;
650}
651
652static int tegra_genpd_power_on(struct generic_pm_domain *domain)
653{
654 struct tegra_powergate *pg = to_powergate(domain);
Thierry Reding589997a2019-01-25 11:22:52 +0100655 struct device *dev = pg->pmc->dev;
Jon Huntera3804512016-03-30 10:15:15 +0100656 int err;
657
658 err = tegra_powergate_power_up(pg, true);
Thierry Reding7fe57192019-02-18 17:36:43 +0100659 if (err) {
Thierry Reding589997a2019-01-25 11:22:52 +0100660 dev_err(dev, "failed to turn on PM domain %s: %d\n",
661 pg->genpd.name, err);
Thierry Reding7fe57192019-02-18 17:36:43 +0100662 goto out;
663 }
Jon Huntera3804512016-03-30 10:15:15 +0100664
Thierry Reding7fe57192019-02-18 17:36:43 +0100665 reset_control_release(pg->reset);
666
667out:
Jon Huntera3804512016-03-30 10:15:15 +0100668 return err;
669}
670
671static int tegra_genpd_power_off(struct generic_pm_domain *domain)
672{
673 struct tegra_powergate *pg = to_powergate(domain);
Thierry Reding589997a2019-01-25 11:22:52 +0100674 struct device *dev = pg->pmc->dev;
Jon Huntera3804512016-03-30 10:15:15 +0100675 int err;
676
Thierry Reding7fe57192019-02-18 17:36:43 +0100677 err = reset_control_acquire(pg->reset);
678 if (err < 0) {
679 pr_err("failed to acquire resets: %d\n", err);
680 return err;
681 }
682
Jon Huntera3804512016-03-30 10:15:15 +0100683 err = tegra_powergate_power_down(pg);
Thierry Reding7fe57192019-02-18 17:36:43 +0100684 if (err) {
Thierry Reding589997a2019-01-25 11:22:52 +0100685 dev_err(dev, "failed to turn off PM domain %s: %d\n",
686 pg->genpd.name, err);
Thierry Reding7fe57192019-02-18 17:36:43 +0100687 reset_control_release(pg->reset);
688 }
Jon Huntera3804512016-03-30 10:15:15 +0100689
690 return err;
691}
692
693/**
694 * tegra_powergate_power_on() - power on partition
695 * @id: partition ID
696 */
697int tegra_powergate_power_on(unsigned int id)
698{
Thierry Reding589997a2019-01-25 11:22:52 +0100699 if (!tegra_powergate_is_available(pmc, id))
Jon Huntera3804512016-03-30 10:15:15 +0100700 return -EINVAL;
701
Thierry Reding589997a2019-01-25 11:22:52 +0100702 return tegra_powergate_set(pmc, id, true);
Jon Huntera3804512016-03-30 10:15:15 +0100703}
704
705/**
706 * tegra_powergate_power_off() - power off partition
707 * @id: partition ID
708 */
709int tegra_powergate_power_off(unsigned int id)
710{
Thierry Reding589997a2019-01-25 11:22:52 +0100711 if (!tegra_powergate_is_available(pmc, id))
Jon Huntera3804512016-03-30 10:15:15 +0100712 return -EINVAL;
713
Thierry Reding589997a2019-01-25 11:22:52 +0100714 return tegra_powergate_set(pmc, id, false);
Jon Huntera3804512016-03-30 10:15:15 +0100715}
716EXPORT_SYMBOL(tegra_powergate_power_off);
717
718/**
719 * tegra_powergate_is_powered() - check if partition is powered
Thierry Reding589997a2019-01-25 11:22:52 +0100720 * @pmc: power management controller
Jon Huntera3804512016-03-30 10:15:15 +0100721 * @id: partition ID
722 */
Thierry Reding589997a2019-01-25 11:22:52 +0100723static int tegra_powergate_is_powered(struct tegra_pmc *pmc, unsigned int id)
Jon Huntera3804512016-03-30 10:15:15 +0100724{
Thierry Reding589997a2019-01-25 11:22:52 +0100725 if (!tegra_powergate_is_valid(pmc, id))
Jon Huntera3804512016-03-30 10:15:15 +0100726 return -EINVAL;
727
Dmitry Osipenkob6e1fd12018-10-21 21:36:14 +0300728 return tegra_powergate_state(id);
Jon Huntera3804512016-03-30 10:15:15 +0100729}
730
731/**
732 * tegra_powergate_remove_clamping() - remove power clamps for partition
733 * @id: partition ID
734 */
735int tegra_powergate_remove_clamping(unsigned int id)
736{
Thierry Reding589997a2019-01-25 11:22:52 +0100737 if (!tegra_powergate_is_available(pmc, id))
Jon Huntera3804512016-03-30 10:15:15 +0100738 return -EINVAL;
739
Thierry Reding589997a2019-01-25 11:22:52 +0100740 return __tegra_powergate_remove_clamping(pmc, id);
Jon Huntera3804512016-03-30 10:15:15 +0100741}
Thierry Reding72323982014-07-11 13:19:06 +0200742EXPORT_SYMBOL(tegra_powergate_remove_clamping);
743
744/**
745 * tegra_powergate_sequence_power_up() - power up partition
746 * @id: partition ID
747 * @clk: clock for partition
748 * @rst: reset for partition
749 *
750 * Must be called with clk disabled, and returns with clk enabled.
751 */
Jon Hunter70293ed2016-02-11 18:03:22 +0000752int tegra_powergate_sequence_power_up(unsigned int id, struct clk *clk,
Thierry Reding72323982014-07-11 13:19:06 +0200753 struct reset_control *rst)
754{
Viresh Kumar495ac332018-05-03 13:56:17 +0530755 struct tegra_powergate *pg;
Jon Huntera3804512016-03-30 10:15:15 +0100756 int err;
Thierry Reding72323982014-07-11 13:19:06 +0200757
Thierry Reding589997a2019-01-25 11:22:52 +0100758 if (!tegra_powergate_is_available(pmc, id))
Jon Hunter403db2d2016-06-28 11:38:23 +0100759 return -EINVAL;
760
Viresh Kumar495ac332018-05-03 13:56:17 +0530761 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
762 if (!pg)
763 return -ENOMEM;
Thierry Reding72323982014-07-11 13:19:06 +0200764
Viresh Kumar495ac332018-05-03 13:56:17 +0530765 pg->id = id;
766 pg->clks = &clk;
767 pg->num_clks = 1;
768 pg->reset = rst;
769 pg->pmc = pmc;
770
771 err = tegra_powergate_power_up(pg, false);
Jon Huntera3804512016-03-30 10:15:15 +0100772 if (err)
Thierry Reding589997a2019-01-25 11:22:52 +0100773 dev_err(pmc->dev, "failed to turn on partition %d: %d\n", id,
774 err);
Thierry Reding72323982014-07-11 13:19:06 +0200775
Viresh Kumar495ac332018-05-03 13:56:17 +0530776 kfree(pg);
777
Jon Huntera3804512016-03-30 10:15:15 +0100778 return err;
Thierry Reding72323982014-07-11 13:19:06 +0200779}
780EXPORT_SYMBOL(tegra_powergate_sequence_power_up);
781
Thierry Reding72323982014-07-11 13:19:06 +0200782/**
783 * tegra_get_cpu_powergate_id() - convert from CPU ID to partition ID
Thierry Reding589997a2019-01-25 11:22:52 +0100784 * @pmc: power management controller
Thierry Reding72323982014-07-11 13:19:06 +0200785 * @cpuid: CPU partition ID
786 *
787 * Returns the partition ID corresponding to the CPU partition ID or a
788 * negative error code on failure.
789 */
Thierry Reding589997a2019-01-25 11:22:52 +0100790static int tegra_get_cpu_powergate_id(struct tegra_pmc *pmc,
791 unsigned int cpuid)
Thierry Reding72323982014-07-11 13:19:06 +0200792{
Jon Hunter70293ed2016-02-11 18:03:22 +0000793 if (pmc->soc && cpuid < pmc->soc->num_cpu_powergates)
Thierry Reding72323982014-07-11 13:19:06 +0200794 return pmc->soc->cpu_powergates[cpuid];
795
796 return -EINVAL;
797}
798
799/**
800 * tegra_pmc_cpu_is_powered() - check if CPU partition is powered
801 * @cpuid: CPU partition ID
802 */
Jon Hunter70293ed2016-02-11 18:03:22 +0000803bool tegra_pmc_cpu_is_powered(unsigned int cpuid)
Thierry Reding72323982014-07-11 13:19:06 +0200804{
805 int id;
806
Thierry Reding589997a2019-01-25 11:22:52 +0100807 id = tegra_get_cpu_powergate_id(pmc, cpuid);
Thierry Reding72323982014-07-11 13:19:06 +0200808 if (id < 0)
809 return false;
810
Thierry Reding589997a2019-01-25 11:22:52 +0100811 return tegra_powergate_is_powered(pmc, id);
Thierry Reding72323982014-07-11 13:19:06 +0200812}
813
814/**
815 * tegra_pmc_cpu_power_on() - power on CPU partition
816 * @cpuid: CPU partition ID
817 */
Jon Hunter70293ed2016-02-11 18:03:22 +0000818int tegra_pmc_cpu_power_on(unsigned int cpuid)
Thierry Reding72323982014-07-11 13:19:06 +0200819{
820 int id;
821
Thierry Reding589997a2019-01-25 11:22:52 +0100822 id = tegra_get_cpu_powergate_id(pmc, cpuid);
Thierry Reding72323982014-07-11 13:19:06 +0200823 if (id < 0)
824 return id;
825
Thierry Reding589997a2019-01-25 11:22:52 +0100826 return tegra_powergate_set(pmc, id, true);
Thierry Reding72323982014-07-11 13:19:06 +0200827}
828
829/**
830 * tegra_pmc_cpu_remove_clamping() - remove power clamps for CPU partition
831 * @cpuid: CPU partition ID
832 */
Jon Hunter70293ed2016-02-11 18:03:22 +0000833int tegra_pmc_cpu_remove_clamping(unsigned int cpuid)
Thierry Reding72323982014-07-11 13:19:06 +0200834{
835 int id;
836
Thierry Reding589997a2019-01-25 11:22:52 +0100837 id = tegra_get_cpu_powergate_id(pmc, cpuid);
Thierry Reding72323982014-07-11 13:19:06 +0200838 if (id < 0)
839 return id;
840
841 return tegra_powergate_remove_clamping(id);
842}
Thierry Reding72323982014-07-11 13:19:06 +0200843
David Riley78921582015-03-18 10:52:25 +0100844static int tegra_pmc_restart_notify(struct notifier_block *this,
845 unsigned long action, void *data)
Thierry Reding72323982014-07-11 13:19:06 +0200846{
David Riley78921582015-03-18 10:52:25 +0100847 const char *cmd = data;
Thierry Reding72323982014-07-11 13:19:06 +0200848 u32 value;
849
Mikko Perttunene247dea2019-01-25 11:22:55 +0100850 value = tegra_pmc_scratch_readl(pmc, pmc->soc->regs->scratch0);
Thierry Reding72323982014-07-11 13:19:06 +0200851 value &= ~PMC_SCRATCH0_MODE_MASK;
852
853 if (cmd) {
854 if (strcmp(cmd, "recovery") == 0)
855 value |= PMC_SCRATCH0_MODE_RECOVERY;
856
857 if (strcmp(cmd, "bootloader") == 0)
858 value |= PMC_SCRATCH0_MODE_BOOTLOADER;
859
860 if (strcmp(cmd, "forced-recovery") == 0)
861 value |= PMC_SCRATCH0_MODE_RCM;
862 }
863
Mikko Perttunene247dea2019-01-25 11:22:55 +0100864 tegra_pmc_scratch_writel(pmc, value, pmc->soc->regs->scratch0);
Thierry Reding72323982014-07-11 13:19:06 +0200865
Thierry Redingf5353c62015-12-30 17:13:29 +0100866 /* reset everything but PMC_SCRATCH0 and PMC_RST_STATUS */
Thierry Reding589997a2019-01-25 11:22:52 +0100867 value = tegra_pmc_readl(pmc, PMC_CNTRL);
Thierry Redingf5353c62015-12-30 17:13:29 +0100868 value |= PMC_CNTRL_MAIN_RST;
Thierry Reding589997a2019-01-25 11:22:52 +0100869 tegra_pmc_writel(pmc, value, PMC_CNTRL);
David Riley78921582015-03-18 10:52:25 +0100870
871 return NOTIFY_DONE;
Thierry Reding72323982014-07-11 13:19:06 +0200872}
873
David Riley78921582015-03-18 10:52:25 +0100874static struct notifier_block tegra_pmc_restart_handler = {
875 .notifier_call = tegra_pmc_restart_notify,
876 .priority = 128,
877};
878
Thierry Reding72323982014-07-11 13:19:06 +0200879static int powergate_show(struct seq_file *s, void *data)
880{
881 unsigned int i;
Jon Hunterc3ea2972016-02-11 18:03:25 +0000882 int status;
Thierry Reding72323982014-07-11 13:19:06 +0200883
884 seq_printf(s, " powergate powered\n");
885 seq_printf(s, "------------------\n");
886
887 for (i = 0; i < pmc->soc->num_powergates; i++) {
Thierry Reding589997a2019-01-25 11:22:52 +0100888 status = tegra_powergate_is_powered(pmc, i);
Jon Hunterc3ea2972016-02-11 18:03:25 +0000889 if (status < 0)
Thierry Reding72323982014-07-11 13:19:06 +0200890 continue;
891
892 seq_printf(s, " %9s %7s\n", pmc->soc->powergates[i],
Jon Hunterc3ea2972016-02-11 18:03:25 +0000893 status ? "yes" : "no");
Thierry Reding72323982014-07-11 13:19:06 +0200894 }
895
896 return 0;
897}
898
Yangtao Li57ba33d2018-11-22 08:12:07 -0500899DEFINE_SHOW_ATTRIBUTE(powergate);
Thierry Reding72323982014-07-11 13:19:06 +0200900
901static int tegra_powergate_debugfs_init(void)
902{
Jon Hunter3195ac62015-12-04 14:57:05 +0000903 pmc->debugfs = debugfs_create_file("powergate", S_IRUGO, NULL, NULL,
904 &powergate_fops);
905 if (!pmc->debugfs)
Thierry Reding72323982014-07-11 13:19:06 +0200906 return -ENOMEM;
907
908 return 0;
909}
910
Jon Huntera3804512016-03-30 10:15:15 +0100911static int tegra_powergate_of_get_clks(struct tegra_powergate *pg,
912 struct device_node *np)
913{
914 struct clk *clk;
915 unsigned int i, count;
916 int err;
917
Geert Uytterhoeven3fd01212018-04-18 16:50:04 +0200918 count = of_clk_get_parent_count(np);
Jon Huntera3804512016-03-30 10:15:15 +0100919 if (count == 0)
920 return -ENODEV;
921
922 pg->clks = kcalloc(count, sizeof(clk), GFP_KERNEL);
923 if (!pg->clks)
924 return -ENOMEM;
925
926 for (i = 0; i < count; i++) {
927 pg->clks[i] = of_clk_get(np, i);
928 if (IS_ERR(pg->clks[i])) {
929 err = PTR_ERR(pg->clks[i]);
930 goto err;
931 }
932 }
933
934 pg->num_clks = count;
935
936 return 0;
937
938err:
939 while (i--)
940 clk_put(pg->clks[i]);
Thierry Redingda8f4b42016-06-30 12:12:55 +0200941
Jon Huntera3804512016-03-30 10:15:15 +0100942 kfree(pg->clks);
943
944 return err;
945}
946
947static int tegra_powergate_of_get_resets(struct tegra_powergate *pg,
Jon Hunter05cfb982016-06-29 10:17:47 +0100948 struct device_node *np, bool off)
Jon Huntera3804512016-03-30 10:15:15 +0100949{
Thierry Reding589997a2019-01-25 11:22:52 +0100950 struct device *dev = pg->pmc->dev;
Jon Huntera3804512016-03-30 10:15:15 +0100951 int err;
952
Thierry Reding7fe57192019-02-18 17:36:43 +0100953 pg->reset = of_reset_control_array_get_exclusive_released(np);
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200954 if (IS_ERR(pg->reset)) {
955 err = PTR_ERR(pg->reset);
Thierry Reding589997a2019-01-25 11:22:52 +0100956 dev_err(dev, "failed to get device resets: %d\n", err);
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200957 return err;
Jon Huntera3804512016-03-30 10:15:15 +0100958 }
959
Thierry Reding7fe57192019-02-18 17:36:43 +0100960 err = reset_control_acquire(pg->reset);
961 if (err < 0) {
962 pr_err("failed to acquire resets: %d\n", err);
963 goto out;
964 }
Jon Huntera3804512016-03-30 10:15:15 +0100965
Thierry Reding7fe57192019-02-18 17:36:43 +0100966 if (off) {
967 err = reset_control_assert(pg->reset);
968 } else {
969 err = reset_control_deassert(pg->reset);
970 if (err < 0)
971 goto out;
972
973 reset_control_release(pg->reset);
974 }
975
976out:
977 if (err) {
978 reset_control_release(pg->reset);
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200979 reset_control_put(pg->reset);
Thierry Reding7fe57192019-02-18 17:36:43 +0100980 }
Jon Huntera3804512016-03-30 10:15:15 +0100981
982 return err;
983}
984
985static void tegra_powergate_add(struct tegra_pmc *pmc, struct device_node *np)
986{
Thierry Reding589997a2019-01-25 11:22:52 +0100987 struct device *dev = pmc->dev;
Jon Huntera3804512016-03-30 10:15:15 +0100988 struct tegra_powergate *pg;
Jon Hunterc2710ac2016-06-30 11:56:24 +0100989 int id, err;
Jon Huntera3804512016-03-30 10:15:15 +0100990 bool off;
Jon Huntera3804512016-03-30 10:15:15 +0100991
992 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
993 if (!pg)
Jon Hunterc2710ac2016-06-30 11:56:24 +0100994 return;
Jon Huntera3804512016-03-30 10:15:15 +0100995
996 id = tegra_powergate_lookup(pmc, np->name);
Jon Hunterc2710ac2016-06-30 11:56:24 +0100997 if (id < 0) {
Thierry Reding589997a2019-01-25 11:22:52 +0100998 dev_err(dev, "powergate lookup failed for %pOFn: %d\n", np, id);
Jon Huntera3804512016-03-30 10:15:15 +0100999 goto free_mem;
Jon Hunterc2710ac2016-06-30 11:56:24 +01001000 }
Jon Huntera3804512016-03-30 10:15:15 +01001001
1002 /*
1003 * Clear the bit for this powergate so it cannot be managed
1004 * directly via the legacy APIs for controlling powergates.
1005 */
1006 clear_bit(id, pmc->powergates_available);
1007
1008 pg->id = id;
1009 pg->genpd.name = np->name;
1010 pg->genpd.power_off = tegra_genpd_power_off;
1011 pg->genpd.power_on = tegra_genpd_power_on;
1012 pg->pmc = pmc;
1013
Thierry Reding589997a2019-01-25 11:22:52 +01001014 off = !tegra_powergate_is_powered(pmc, pg->id);
Jon Hunter05cfb982016-06-29 10:17:47 +01001015
Jon Hunterc2710ac2016-06-30 11:56:24 +01001016 err = tegra_powergate_of_get_clks(pg, np);
1017 if (err < 0) {
Thierry Reding589997a2019-01-25 11:22:52 +01001018 dev_err(dev, "failed to get clocks for %pOFn: %d\n", np, err);
Jon Huntera3804512016-03-30 10:15:15 +01001019 goto set_available;
Jon Hunterc2710ac2016-06-30 11:56:24 +01001020 }
Jon Huntera3804512016-03-30 10:15:15 +01001021
Jon Hunterc2710ac2016-06-30 11:56:24 +01001022 err = tegra_powergate_of_get_resets(pg, np, off);
1023 if (err < 0) {
Thierry Reding589997a2019-01-25 11:22:52 +01001024 dev_err(dev, "failed to get resets for %pOFn: %d\n", np, err);
Jon Huntera3804512016-03-30 10:15:15 +01001025 goto remove_clks;
Jon Hunterc2710ac2016-06-30 11:56:24 +01001026 }
Jon Huntera3804512016-03-30 10:15:15 +01001027
Jon Hunter0b137342016-10-22 20:23:56 +01001028 if (!IS_ENABLED(CONFIG_PM_GENERIC_DOMAINS)) {
1029 if (off)
1030 WARN_ON(tegra_powergate_power_up(pg, true));
1031
1032 goto remove_resets;
1033 }
Jon Huntere2d17962016-06-30 11:56:25 +01001034
Jon Huntercd5ceda2016-10-22 20:23:55 +01001035 err = pm_genpd_init(&pg->genpd, NULL, off);
1036 if (err < 0) {
Thierry Reding589997a2019-01-25 11:22:52 +01001037 dev_err(dev, "failed to initialise PM domain %pOFn: %d\n", np,
Jon Huntercd5ceda2016-10-22 20:23:55 +01001038 err);
1039 goto remove_resets;
1040 }
Jon Huntera3804512016-03-30 10:15:15 +01001041
Jon Hunterc2710ac2016-06-30 11:56:24 +01001042 err = of_genpd_add_provider_simple(np, &pg->genpd);
1043 if (err < 0) {
Thierry Reding589997a2019-01-25 11:22:52 +01001044 dev_err(dev, "failed to add PM domain provider for %pOFn: %d\n",
1045 np, err);
Jon Hunter0b137342016-10-22 20:23:56 +01001046 goto remove_genpd;
Jon Hunterc2710ac2016-06-30 11:56:24 +01001047 }
Jon Huntera3804512016-03-30 10:15:15 +01001048
Thierry Reding589997a2019-01-25 11:22:52 +01001049 dev_dbg(dev, "added PM domain %s\n", pg->genpd.name);
Jon Huntera3804512016-03-30 10:15:15 +01001050
1051 return;
1052
Jon Hunter0b137342016-10-22 20:23:56 +01001053remove_genpd:
1054 pm_genpd_remove(&pg->genpd);
Jon Huntere2d17962016-06-30 11:56:25 +01001055
Jon Huntera3804512016-03-30 10:15:15 +01001056remove_resets:
Vivek Gautam4c817cc2017-07-19 17:59:08 +02001057 reset_control_put(pg->reset);
Jon Huntera3804512016-03-30 10:15:15 +01001058
1059remove_clks:
1060 while (pg->num_clks--)
1061 clk_put(pg->clks[pg->num_clks]);
Thierry Redingda8f4b42016-06-30 12:12:55 +02001062
Jon Huntera3804512016-03-30 10:15:15 +01001063 kfree(pg->clks);
1064
1065set_available:
1066 set_bit(id, pmc->powergates_available);
1067
1068free_mem:
1069 kfree(pg);
Jon Huntera3804512016-03-30 10:15:15 +01001070}
1071
Jon Huntere2d17962016-06-30 11:56:25 +01001072static void tegra_powergate_init(struct tegra_pmc *pmc,
1073 struct device_node *parent)
Jon Huntera3804512016-03-30 10:15:15 +01001074{
1075 struct device_node *np, *child;
Jon Huntere2d17962016-06-30 11:56:25 +01001076 unsigned int i;
Jon Huntera3804512016-03-30 10:15:15 +01001077
Jon Huntere2d17962016-06-30 11:56:25 +01001078 /* Create a bitmap of the available and valid partitions */
1079 for (i = 0; i < pmc->soc->num_powergates; i++)
1080 if (pmc->soc->powergates[i])
1081 set_bit(i, pmc->powergates_available);
1082
1083 np = of_get_child_by_name(parent, "powergates");
Jon Huntera3804512016-03-30 10:15:15 +01001084 if (!np)
1085 return;
1086
Tuomas Tynkkynen0c106e52017-07-29 02:58:43 +03001087 for_each_child_of_node(np, child)
Jon Huntera3804512016-03-30 10:15:15 +01001088 tegra_powergate_add(pmc, child);
Jon Huntera3804512016-03-30 10:15:15 +01001089
1090 of_node_put(np);
1091}
1092
Laxman Dewangan21b49912016-10-10 15:14:34 +02001093static const struct tegra_io_pad_soc *
1094tegra_io_pad_find(struct tegra_pmc *pmc, enum tegra_io_pad id)
Thierry Reding72323982014-07-11 13:19:06 +02001095{
Laxman Dewangan21b49912016-10-10 15:14:34 +02001096 unsigned int i;
1097
1098 for (i = 0; i < pmc->soc->num_io_pads; i++)
1099 if (pmc->soc->io_pads[i].id == id)
1100 return &pmc->soc->io_pads[i];
1101
1102 return NULL;
1103}
1104
Thierry Reding589997a2019-01-25 11:22:52 +01001105static int tegra_io_pad_get_dpd_register_bit(struct tegra_pmc *pmc,
1106 enum tegra_io_pad id,
Aapo Vienamo00ead3c2018-08-10 21:08:08 +03001107 unsigned long *request,
1108 unsigned long *status,
1109 u32 *mask)
Laxman Dewangan21b49912016-10-10 15:14:34 +02001110{
1111 const struct tegra_io_pad_soc *pad;
Thierry Reding72323982014-07-11 13:19:06 +02001112
Laxman Dewangan21b49912016-10-10 15:14:34 +02001113 pad = tegra_io_pad_find(pmc, id);
Thierry Reding54e24722016-11-08 10:58:32 +01001114 if (!pad) {
Thierry Reding589997a2019-01-25 11:22:52 +01001115 dev_err(pmc->dev, "invalid I/O pad ID %u\n", id);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001116 return -ENOENT;
Thierry Reding54e24722016-11-08 10:58:32 +01001117 }
Thierry Reding72323982014-07-11 13:19:06 +02001118
Laxman Dewangan21b49912016-10-10 15:14:34 +02001119 if (pad->dpd == UINT_MAX)
1120 return -ENOTSUPP;
Thierry Reding72323982014-07-11 13:19:06 +02001121
Jon Hunter27b12b4e2016-10-22 20:23:53 +01001122 *mask = BIT(pad->dpd % 32);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001123
1124 if (pad->dpd < 32) {
Thierry Reding5be22552017-08-30 12:32:58 +02001125 *status = pmc->soc->regs->dpd_status;
1126 *request = pmc->soc->regs->dpd_req;
Thierry Reding72323982014-07-11 13:19:06 +02001127 } else {
Thierry Reding5be22552017-08-30 12:32:58 +02001128 *status = pmc->soc->regs->dpd2_status;
1129 *request = pmc->soc->regs->dpd2_req;
Thierry Reding72323982014-07-11 13:19:06 +02001130 }
1131
Aapo Vienamo00ead3c2018-08-10 21:08:08 +03001132 return 0;
1133}
1134
Thierry Reding589997a2019-01-25 11:22:52 +01001135static int tegra_io_pad_prepare(struct tegra_pmc *pmc, enum tegra_io_pad id,
1136 unsigned long *request, unsigned long *status,
1137 u32 *mask)
Aapo Vienamo00ead3c2018-08-10 21:08:08 +03001138{
1139 unsigned long rate, value;
1140 int err;
1141
Thierry Reding589997a2019-01-25 11:22:52 +01001142 err = tegra_io_pad_get_dpd_register_bit(pmc, id, request, status, mask);
Aapo Vienamo00ead3c2018-08-10 21:08:08 +03001143 if (err)
1144 return err;
1145
Thierry Reding5be22552017-08-30 12:32:58 +02001146 if (pmc->clk) {
1147 rate = clk_get_rate(pmc->clk);
1148 if (!rate) {
Thierry Reding589997a2019-01-25 11:22:52 +01001149 dev_err(pmc->dev, "failed to get clock rate\n");
Thierry Reding5be22552017-08-30 12:32:58 +02001150 return -ENODEV;
1151 }
1152
Thierry Reding589997a2019-01-25 11:22:52 +01001153 tegra_pmc_writel(pmc, DPD_SAMPLE_ENABLE, DPD_SAMPLE);
Thierry Reding5be22552017-08-30 12:32:58 +02001154
1155 /* must be at least 200 ns, in APB (PCLK) clock cycles */
1156 value = DIV_ROUND_UP(1000000000, rate);
1157 value = DIV_ROUND_UP(200, value);
Thierry Reding589997a2019-01-25 11:22:52 +01001158 tegra_pmc_writel(pmc, value, SEL_DPD_TIM);
Thierry Reding54e24722016-11-08 10:58:32 +01001159 }
Thierry Reding72323982014-07-11 13:19:06 +02001160
Thierry Reding72323982014-07-11 13:19:06 +02001161 return 0;
1162}
1163
Thierry Reding589997a2019-01-25 11:22:52 +01001164static int tegra_io_pad_poll(struct tegra_pmc *pmc, unsigned long offset,
1165 u32 mask, u32 val, unsigned long timeout)
Thierry Reding72323982014-07-11 13:19:06 +02001166{
Laxman Dewangan84cf85e2016-06-17 18:36:13 +05301167 u32 value;
Thierry Reding72323982014-07-11 13:19:06 +02001168
1169 timeout = jiffies + msecs_to_jiffies(timeout);
1170
1171 while (time_after(timeout, jiffies)) {
Thierry Reding589997a2019-01-25 11:22:52 +01001172 value = tegra_pmc_readl(pmc, offset);
Thierry Reding72323982014-07-11 13:19:06 +02001173 if ((value & mask) == val)
1174 return 0;
1175
1176 usleep_range(250, 1000);
1177 }
1178
1179 return -ETIMEDOUT;
1180}
1181
Thierry Reding589997a2019-01-25 11:22:52 +01001182static void tegra_io_pad_unprepare(struct tegra_pmc *pmc)
Thierry Reding72323982014-07-11 13:19:06 +02001183{
Thierry Reding5be22552017-08-30 12:32:58 +02001184 if (pmc->clk)
Thierry Reding589997a2019-01-25 11:22:52 +01001185 tegra_pmc_writel(pmc, DPD_SAMPLE_DISABLE, DPD_SAMPLE);
Thierry Reding72323982014-07-11 13:19:06 +02001186}
1187
Laxman Dewangan21b49912016-10-10 15:14:34 +02001188/**
1189 * tegra_io_pad_power_enable() - enable power to I/O pad
1190 * @id: Tegra I/O pad ID for which to enable power
1191 *
1192 * Returns: 0 on success or a negative error code on failure.
1193 */
1194int tegra_io_pad_power_enable(enum tegra_io_pad id)
Thierry Reding72323982014-07-11 13:19:06 +02001195{
Vince Hsua9ccc1232016-08-11 09:13:36 +08001196 unsigned long request, status;
Jon Hunter27b12b4e2016-10-22 20:23:53 +01001197 u32 mask;
Thierry Reding72323982014-07-11 13:19:06 +02001198 int err;
1199
Jon Huntere8cf6612016-02-11 18:03:21 +00001200 mutex_lock(&pmc->powergates_lock);
1201
Thierry Reding589997a2019-01-25 11:22:52 +01001202 err = tegra_io_pad_prepare(pmc, id, &request, &status, &mask);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001203 if (err < 0) {
Thierry Reding589997a2019-01-25 11:22:52 +01001204 dev_err(pmc->dev, "failed to prepare I/O pad: %d\n", err);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001205 goto unlock;
1206 }
Thierry Reding72323982014-07-11 13:19:06 +02001207
Thierry Reding589997a2019-01-25 11:22:52 +01001208 tegra_pmc_writel(pmc, IO_DPD_REQ_CODE_OFF | mask, request);
Thierry Reding72323982014-07-11 13:19:06 +02001209
Thierry Reding589997a2019-01-25 11:22:52 +01001210 err = tegra_io_pad_poll(pmc, status, mask, 0, 250);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001211 if (err < 0) {
Thierry Reding589997a2019-01-25 11:22:52 +01001212 dev_err(pmc->dev, "failed to enable I/O pad: %d\n", err);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001213 goto unlock;
Thierry Reding592431b2015-08-04 15:25:03 +02001214 }
Thierry Reding72323982014-07-11 13:19:06 +02001215
Thierry Reding589997a2019-01-25 11:22:52 +01001216 tegra_io_pad_unprepare(pmc);
Thierry Reding72323982014-07-11 13:19:06 +02001217
Laxman Dewangan21b49912016-10-10 15:14:34 +02001218unlock:
Jon Huntere8cf6612016-02-11 18:03:21 +00001219 mutex_unlock(&pmc->powergates_lock);
Jon Huntere8cf6612016-02-11 18:03:21 +00001220 return err;
Thierry Reding72323982014-07-11 13:19:06 +02001221}
Laxman Dewangan21b49912016-10-10 15:14:34 +02001222EXPORT_SYMBOL(tegra_io_pad_power_enable);
Thierry Reding72323982014-07-11 13:19:06 +02001223
Laxman Dewangan21b49912016-10-10 15:14:34 +02001224/**
1225 * tegra_io_pad_power_disable() - disable power to I/O pad
1226 * @id: Tegra I/O pad ID for which to disable power
1227 *
1228 * Returns: 0 on success or a negative error code on failure.
1229 */
1230int tegra_io_pad_power_disable(enum tegra_io_pad id)
Thierry Reding72323982014-07-11 13:19:06 +02001231{
Vince Hsua9ccc1232016-08-11 09:13:36 +08001232 unsigned long request, status;
Jon Hunter27b12b4e2016-10-22 20:23:53 +01001233 u32 mask;
Thierry Reding72323982014-07-11 13:19:06 +02001234 int err;
1235
Jon Huntere8cf6612016-02-11 18:03:21 +00001236 mutex_lock(&pmc->powergates_lock);
1237
Thierry Reding589997a2019-01-25 11:22:52 +01001238 err = tegra_io_pad_prepare(pmc, id, &request, &status, &mask);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001239 if (err < 0) {
Thierry Reding589997a2019-01-25 11:22:52 +01001240 dev_err(pmc->dev, "failed to prepare I/O pad: %d\n", err);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001241 goto unlock;
Thierry Reding592431b2015-08-04 15:25:03 +02001242 }
Thierry Reding72323982014-07-11 13:19:06 +02001243
Thierry Reding589997a2019-01-25 11:22:52 +01001244 tegra_pmc_writel(pmc, IO_DPD_REQ_CODE_ON | mask, request);
Thierry Reding72323982014-07-11 13:19:06 +02001245
Thierry Reding589997a2019-01-25 11:22:52 +01001246 err = tegra_io_pad_poll(pmc, status, mask, mask, 250);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001247 if (err < 0) {
Thierry Reding589997a2019-01-25 11:22:52 +01001248 dev_err(pmc->dev, "failed to disable I/O pad: %d\n", err);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001249 goto unlock;
1250 }
Thierry Reding72323982014-07-11 13:19:06 +02001251
Thierry Reding589997a2019-01-25 11:22:52 +01001252 tegra_io_pad_unprepare(pmc);
Thierry Reding72323982014-07-11 13:19:06 +02001253
Laxman Dewangan21b49912016-10-10 15:14:34 +02001254unlock:
1255 mutex_unlock(&pmc->powergates_lock);
1256 return err;
1257}
1258EXPORT_SYMBOL(tegra_io_pad_power_disable);
1259
Thierry Reding589997a2019-01-25 11:22:52 +01001260static int tegra_io_pad_is_powered(struct tegra_pmc *pmc, enum tegra_io_pad id)
Aapo Vienamof142b9d2018-08-10 21:08:09 +03001261{
1262 unsigned long request, status;
1263 u32 mask, value;
1264 int err;
1265
Thierry Reding589997a2019-01-25 11:22:52 +01001266 err = tegra_io_pad_get_dpd_register_bit(pmc, id, &request, &status,
1267 &mask);
Aapo Vienamof142b9d2018-08-10 21:08:09 +03001268 if (err)
1269 return err;
1270
Thierry Reding589997a2019-01-25 11:22:52 +01001271 value = tegra_pmc_readl(pmc, status);
Aapo Vienamof142b9d2018-08-10 21:08:09 +03001272
1273 return !(value & mask);
1274}
1275
Thierry Reding589997a2019-01-25 11:22:52 +01001276static int tegra_io_pad_set_voltage(struct tegra_pmc *pmc, enum tegra_io_pad id,
1277 int voltage)
Laxman Dewangan21b49912016-10-10 15:14:34 +02001278{
1279 const struct tegra_io_pad_soc *pad;
1280 u32 value;
1281
1282 pad = tegra_io_pad_find(pmc, id);
1283 if (!pad)
1284 return -ENOENT;
1285
1286 if (pad->voltage == UINT_MAX)
1287 return -ENOTSUPP;
1288
1289 mutex_lock(&pmc->powergates_lock);
1290
Aapo Vienamo13136a42018-08-10 21:08:07 +03001291 if (pmc->soc->has_impl_33v_pwr) {
Thierry Reding589997a2019-01-25 11:22:52 +01001292 value = tegra_pmc_readl(pmc, PMC_IMPL_E_33V_PWR);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001293
Aapo Vienamofccf0f72018-08-10 21:08:11 +03001294 if (voltage == TEGRA_IO_PAD_VOLTAGE_1V8)
Aapo Vienamo13136a42018-08-10 21:08:07 +03001295 value &= ~BIT(pad->voltage);
1296 else
1297 value |= BIT(pad->voltage);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001298
Thierry Reding589997a2019-01-25 11:22:52 +01001299 tegra_pmc_writel(pmc, value, PMC_IMPL_E_33V_PWR);
Aapo Vienamo13136a42018-08-10 21:08:07 +03001300 } else {
1301 /* write-enable PMC_PWR_DET_VALUE[pad->voltage] */
Thierry Reding589997a2019-01-25 11:22:52 +01001302 value = tegra_pmc_readl(pmc, PMC_PWR_DET);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001303 value |= BIT(pad->voltage);
Thierry Reding589997a2019-01-25 11:22:52 +01001304 tegra_pmc_writel(pmc, value, PMC_PWR_DET);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001305
Aapo Vienamo13136a42018-08-10 21:08:07 +03001306 /* update I/O voltage */
Thierry Reding589997a2019-01-25 11:22:52 +01001307 value = tegra_pmc_readl(pmc, PMC_PWR_DET_VALUE);
Aapo Vienamo13136a42018-08-10 21:08:07 +03001308
Aapo Vienamofccf0f72018-08-10 21:08:11 +03001309 if (voltage == TEGRA_IO_PAD_VOLTAGE_1V8)
Aapo Vienamo13136a42018-08-10 21:08:07 +03001310 value &= ~BIT(pad->voltage);
1311 else
1312 value |= BIT(pad->voltage);
1313
Thierry Reding589997a2019-01-25 11:22:52 +01001314 tegra_pmc_writel(pmc, value, PMC_PWR_DET_VALUE);
Aapo Vienamo13136a42018-08-10 21:08:07 +03001315 }
Laxman Dewangan21b49912016-10-10 15:14:34 +02001316
Jon Huntere8cf6612016-02-11 18:03:21 +00001317 mutex_unlock(&pmc->powergates_lock);
1318
Laxman Dewangan21b49912016-10-10 15:14:34 +02001319 usleep_range(100, 250);
1320
1321 return 0;
1322}
Laxman Dewangan21b49912016-10-10 15:14:34 +02001323
Thierry Reding589997a2019-01-25 11:22:52 +01001324static int tegra_io_pad_get_voltage(struct tegra_pmc *pmc, enum tegra_io_pad id)
Laxman Dewangan21b49912016-10-10 15:14:34 +02001325{
1326 const struct tegra_io_pad_soc *pad;
1327 u32 value;
1328
1329 pad = tegra_io_pad_find(pmc, id);
1330 if (!pad)
1331 return -ENOENT;
1332
1333 if (pad->voltage == UINT_MAX)
1334 return -ENOTSUPP;
1335
Aapo Vienamo13136a42018-08-10 21:08:07 +03001336 if (pmc->soc->has_impl_33v_pwr)
Thierry Reding589997a2019-01-25 11:22:52 +01001337 value = tegra_pmc_readl(pmc, PMC_IMPL_E_33V_PWR);
Aapo Vienamo13136a42018-08-10 21:08:07 +03001338 else
Thierry Reding589997a2019-01-25 11:22:52 +01001339 value = tegra_pmc_readl(pmc, PMC_PWR_DET_VALUE);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001340
1341 if ((value & BIT(pad->voltage)) == 0)
Aapo Vienamofccf0f72018-08-10 21:08:11 +03001342 return TEGRA_IO_PAD_VOLTAGE_1V8;
Laxman Dewangan21b49912016-10-10 15:14:34 +02001343
Aapo Vienamofccf0f72018-08-10 21:08:11 +03001344 return TEGRA_IO_PAD_VOLTAGE_3V3;
Laxman Dewangan21b49912016-10-10 15:14:34 +02001345}
Laxman Dewangan21b49912016-10-10 15:14:34 +02001346
1347/**
1348 * tegra_io_rail_power_on() - enable power to I/O rail
1349 * @id: Tegra I/O pad ID for which to enable power
1350 *
1351 * See also: tegra_io_pad_power_enable()
1352 */
1353int tegra_io_rail_power_on(unsigned int id)
1354{
1355 return tegra_io_pad_power_enable(id);
1356}
1357EXPORT_SYMBOL(tegra_io_rail_power_on);
1358
1359/**
1360 * tegra_io_rail_power_off() - disable power to I/O rail
1361 * @id: Tegra I/O pad ID for which to disable power
1362 *
1363 * See also: tegra_io_pad_power_disable()
1364 */
1365int tegra_io_rail_power_off(unsigned int id)
1366{
1367 return tegra_io_pad_power_disable(id);
Thierry Reding72323982014-07-11 13:19:06 +02001368}
1369EXPORT_SYMBOL(tegra_io_rail_power_off);
1370
1371#ifdef CONFIG_PM_SLEEP
1372enum tegra_suspend_mode tegra_pmc_get_suspend_mode(void)
1373{
1374 return pmc->suspend_mode;
1375}
1376
1377void tegra_pmc_set_suspend_mode(enum tegra_suspend_mode mode)
1378{
1379 if (mode < TEGRA_SUSPEND_NONE || mode >= TEGRA_MAX_SUSPEND_MODE)
1380 return;
1381
1382 pmc->suspend_mode = mode;
1383}
1384
1385void tegra_pmc_enter_suspend_mode(enum tegra_suspend_mode mode)
1386{
1387 unsigned long long rate = 0;
1388 u32 value;
1389
1390 switch (mode) {
1391 case TEGRA_SUSPEND_LP1:
1392 rate = 32768;
1393 break;
1394
1395 case TEGRA_SUSPEND_LP2:
1396 rate = clk_get_rate(pmc->clk);
1397 break;
1398
1399 default:
1400 break;
1401 }
1402
1403 if (WARN_ON_ONCE(rate == 0))
1404 rate = 100000000;
1405
1406 if (rate != pmc->rate) {
1407 u64 ticks;
1408
1409 ticks = pmc->cpu_good_time * rate + USEC_PER_SEC - 1;
1410 do_div(ticks, USEC_PER_SEC);
Thierry Reding589997a2019-01-25 11:22:52 +01001411 tegra_pmc_writel(pmc, ticks, PMC_CPUPWRGOOD_TIMER);
Thierry Reding72323982014-07-11 13:19:06 +02001412
1413 ticks = pmc->cpu_off_time * rate + USEC_PER_SEC - 1;
1414 do_div(ticks, USEC_PER_SEC);
Thierry Reding589997a2019-01-25 11:22:52 +01001415 tegra_pmc_writel(pmc, ticks, PMC_CPUPWROFF_TIMER);
Thierry Reding72323982014-07-11 13:19:06 +02001416
1417 wmb();
1418
1419 pmc->rate = rate;
1420 }
1421
Thierry Reding589997a2019-01-25 11:22:52 +01001422 value = tegra_pmc_readl(pmc, PMC_CNTRL);
Thierry Reding72323982014-07-11 13:19:06 +02001423 value &= ~PMC_CNTRL_SIDE_EFFECT_LP0;
1424 value |= PMC_CNTRL_CPU_PWRREQ_OE;
Thierry Reding589997a2019-01-25 11:22:52 +01001425 tegra_pmc_writel(pmc, value, PMC_CNTRL);
Thierry Reding72323982014-07-11 13:19:06 +02001426}
1427#endif
1428
1429static int tegra_pmc_parse_dt(struct tegra_pmc *pmc, struct device_node *np)
1430{
1431 u32 value, values[2];
1432
1433 if (of_property_read_u32(np, "nvidia,suspend-mode", &value)) {
1434 } else {
1435 switch (value) {
1436 case 0:
1437 pmc->suspend_mode = TEGRA_SUSPEND_LP0;
1438 break;
1439
1440 case 1:
1441 pmc->suspend_mode = TEGRA_SUSPEND_LP1;
1442 break;
1443
1444 case 2:
1445 pmc->suspend_mode = TEGRA_SUSPEND_LP2;
1446 break;
1447
1448 default:
1449 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1450 break;
1451 }
1452 }
1453
1454 pmc->suspend_mode = tegra_pm_validate_suspend_mode(pmc->suspend_mode);
1455
1456 if (of_property_read_u32(np, "nvidia,cpu-pwr-good-time", &value))
1457 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1458
1459 pmc->cpu_good_time = value;
1460
1461 if (of_property_read_u32(np, "nvidia,cpu-pwr-off-time", &value))
1462 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1463
1464 pmc->cpu_off_time = value;
1465
1466 if (of_property_read_u32_array(np, "nvidia,core-pwr-good-time",
1467 values, ARRAY_SIZE(values)))
1468 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1469
1470 pmc->core_osc_time = values[0];
1471 pmc->core_pmu_time = values[1];
1472
1473 if (of_property_read_u32(np, "nvidia,core-pwr-off-time", &value))
1474 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1475
1476 pmc->core_off_time = value;
1477
1478 pmc->corereq_high = of_property_read_bool(np,
1479 "nvidia,core-power-req-active-high");
1480
1481 pmc->sysclkreq_high = of_property_read_bool(np,
1482 "nvidia,sys-clock-req-active-high");
1483
1484 pmc->combined_req = of_property_read_bool(np,
1485 "nvidia,combined-power-req");
1486
1487 pmc->cpu_pwr_good_en = of_property_read_bool(np,
1488 "nvidia,cpu-pwr-good-en");
1489
1490 if (of_property_read_u32_array(np, "nvidia,lp0-vec", values,
1491 ARRAY_SIZE(values)))
1492 if (pmc->suspend_mode == TEGRA_SUSPEND_LP0)
1493 pmc->suspend_mode = TEGRA_SUSPEND_LP1;
1494
1495 pmc->lp0_vec_phys = values[0];
1496 pmc->lp0_vec_size = values[1];
1497
1498 return 0;
1499}
1500
1501static void tegra_pmc_init(struct tegra_pmc *pmc)
1502{
Thierry Reding5be22552017-08-30 12:32:58 +02001503 if (pmc->soc->init)
1504 pmc->soc->init(pmc);
Thierry Reding72323982014-07-11 13:19:06 +02001505}
1506
Jon Hunter1e52efdf2015-12-04 14:57:04 +00001507static void tegra_pmc_init_tsense_reset(struct tegra_pmc *pmc)
Mikko Perttunen3568df32015-01-06 12:52:58 +02001508{
1509 static const char disabled[] = "emergency thermal reset disabled";
1510 u32 pmu_addr, ctrl_id, reg_addr, reg_data, pinmux;
1511 struct device *dev = pmc->dev;
1512 struct device_node *np;
1513 u32 value, checksum;
1514
1515 if (!pmc->soc->has_tsense_reset)
Thierry Reding95169cd2015-07-09 09:59:55 +02001516 return;
Mikko Perttunen3568df32015-01-06 12:52:58 +02001517
Johan Hovold1dc6bd52017-11-15 10:44:58 +01001518 np = of_get_child_by_name(pmc->dev->of_node, "i2c-thermtrip");
Mikko Perttunen3568df32015-01-06 12:52:58 +02001519 if (!np) {
1520 dev_warn(dev, "i2c-thermtrip node not found, %s.\n", disabled);
Thierry Reding95169cd2015-07-09 09:59:55 +02001521 return;
Mikko Perttunen3568df32015-01-06 12:52:58 +02001522 }
1523
1524 if (of_property_read_u32(np, "nvidia,i2c-controller-id", &ctrl_id)) {
1525 dev_err(dev, "I2C controller ID missing, %s.\n", disabled);
1526 goto out;
1527 }
1528
1529 if (of_property_read_u32(np, "nvidia,bus-addr", &pmu_addr)) {
1530 dev_err(dev, "nvidia,bus-addr missing, %s.\n", disabled);
1531 goto out;
1532 }
1533
1534 if (of_property_read_u32(np, "nvidia,reg-addr", &reg_addr)) {
1535 dev_err(dev, "nvidia,reg-addr missing, %s.\n", disabled);
1536 goto out;
1537 }
1538
1539 if (of_property_read_u32(np, "nvidia,reg-data", &reg_data)) {
1540 dev_err(dev, "nvidia,reg-data missing, %s.\n", disabled);
1541 goto out;
1542 }
1543
1544 if (of_property_read_u32(np, "nvidia,pinmux-id", &pinmux))
1545 pinmux = 0;
1546
Thierry Reding589997a2019-01-25 11:22:52 +01001547 value = tegra_pmc_readl(pmc, PMC_SENSOR_CTRL);
Mikko Perttunen3568df32015-01-06 12:52:58 +02001548 value |= PMC_SENSOR_CTRL_SCRATCH_WRITE;
Thierry Reding589997a2019-01-25 11:22:52 +01001549 tegra_pmc_writel(pmc, value, PMC_SENSOR_CTRL);
Mikko Perttunen3568df32015-01-06 12:52:58 +02001550
1551 value = (reg_data << PMC_SCRATCH54_DATA_SHIFT) |
1552 (reg_addr << PMC_SCRATCH54_ADDR_SHIFT);
Thierry Reding589997a2019-01-25 11:22:52 +01001553 tegra_pmc_writel(pmc, value, PMC_SCRATCH54);
Mikko Perttunen3568df32015-01-06 12:52:58 +02001554
1555 value = PMC_SCRATCH55_RESET_TEGRA;
1556 value |= ctrl_id << PMC_SCRATCH55_CNTRL_ID_SHIFT;
1557 value |= pinmux << PMC_SCRATCH55_PINMUX_SHIFT;
1558 value |= pmu_addr << PMC_SCRATCH55_I2CSLV1_SHIFT;
1559
1560 /*
1561 * Calculate checksum of SCRATCH54, SCRATCH55 fields. Bits 23:16 will
1562 * contain the checksum and are currently zero, so they are not added.
1563 */
1564 checksum = reg_addr + reg_data + (value & 0xff) + ((value >> 8) & 0xff)
1565 + ((value >> 24) & 0xff);
1566 checksum &= 0xff;
1567 checksum = 0x100 - checksum;
1568
1569 value |= checksum << PMC_SCRATCH55_CHECKSUM_SHIFT;
1570
Thierry Reding589997a2019-01-25 11:22:52 +01001571 tegra_pmc_writel(pmc, value, PMC_SCRATCH55);
Mikko Perttunen3568df32015-01-06 12:52:58 +02001572
Thierry Reding589997a2019-01-25 11:22:52 +01001573 value = tegra_pmc_readl(pmc, PMC_SENSOR_CTRL);
Mikko Perttunen3568df32015-01-06 12:52:58 +02001574 value |= PMC_SENSOR_CTRL_ENABLE_RST;
Thierry Reding589997a2019-01-25 11:22:52 +01001575 tegra_pmc_writel(pmc, value, PMC_SENSOR_CTRL);
Mikko Perttunen3568df32015-01-06 12:52:58 +02001576
1577 dev_info(pmc->dev, "emergency thermal reset enabled\n");
1578
1579out:
1580 of_node_put(np);
Mikko Perttunen3568df32015-01-06 12:52:58 +02001581}
1582
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001583static int tegra_io_pad_pinctrl_get_groups_count(struct pinctrl_dev *pctl_dev)
1584{
Thierry Reding589997a2019-01-25 11:22:52 +01001585 struct tegra_pmc *pmc = pinctrl_dev_get_drvdata(pctl_dev);
1586
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001587 return pmc->soc->num_io_pads;
1588}
1589
Thierry Redingf1d91292019-01-25 11:22:53 +01001590static const char *tegra_io_pad_pinctrl_get_group_name(struct pinctrl_dev *pctl,
1591 unsigned int group)
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001592{
Thierry Reding589997a2019-01-25 11:22:52 +01001593 struct tegra_pmc *pmc = pinctrl_dev_get_drvdata(pctl);
1594
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001595 return pmc->soc->io_pads[group].name;
1596}
1597
1598static int tegra_io_pad_pinctrl_get_group_pins(struct pinctrl_dev *pctl_dev,
1599 unsigned int group,
1600 const unsigned int **pins,
1601 unsigned int *num_pins)
1602{
Thierry Reding589997a2019-01-25 11:22:52 +01001603 struct tegra_pmc *pmc = pinctrl_dev_get_drvdata(pctl_dev);
1604
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001605 *pins = &pmc->soc->io_pads[group].id;
1606 *num_pins = 1;
Thierry Redingf1d91292019-01-25 11:22:53 +01001607
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001608 return 0;
1609}
1610
1611static const struct pinctrl_ops tegra_io_pad_pinctrl_ops = {
1612 .get_groups_count = tegra_io_pad_pinctrl_get_groups_count,
1613 .get_group_name = tegra_io_pad_pinctrl_get_group_name,
1614 .get_group_pins = tegra_io_pad_pinctrl_get_group_pins,
1615 .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
1616 .dt_free_map = pinconf_generic_dt_free_map,
1617};
1618
1619static int tegra_io_pad_pinconf_get(struct pinctrl_dev *pctl_dev,
1620 unsigned int pin, unsigned long *config)
1621{
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001622 enum pin_config_param param = pinconf_to_config_param(*config);
Thierry Reding589997a2019-01-25 11:22:52 +01001623 struct tegra_pmc *pmc = pinctrl_dev_get_drvdata(pctl_dev);
1624 const struct tegra_io_pad_soc *pad;
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001625 int ret;
1626 u32 arg;
1627
Thierry Reding589997a2019-01-25 11:22:52 +01001628 pad = tegra_io_pad_find(pmc, pin);
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001629 if (!pad)
1630 return -EINVAL;
1631
1632 switch (param) {
1633 case PIN_CONFIG_POWER_SOURCE:
Thierry Reding589997a2019-01-25 11:22:52 +01001634 ret = tegra_io_pad_get_voltage(pmc, pad->id);
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001635 if (ret < 0)
1636 return ret;
Thierry Redingf1d91292019-01-25 11:22:53 +01001637
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001638 arg = ret;
1639 break;
Thierry Redingf1d91292019-01-25 11:22:53 +01001640
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001641 case PIN_CONFIG_LOW_POWER_MODE:
Thierry Reding589997a2019-01-25 11:22:52 +01001642 ret = tegra_io_pad_is_powered(pmc, pad->id);
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001643 if (ret < 0)
1644 return ret;
Thierry Redingf1d91292019-01-25 11:22:53 +01001645
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001646 arg = !ret;
1647 break;
Thierry Redingf1d91292019-01-25 11:22:53 +01001648
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001649 default:
1650 return -EINVAL;
1651 }
1652
1653 *config = pinconf_to_config_packed(param, arg);
1654
1655 return 0;
1656}
1657
1658static int tegra_io_pad_pinconf_set(struct pinctrl_dev *pctl_dev,
1659 unsigned int pin, unsigned long *configs,
1660 unsigned int num_configs)
1661{
Thierry Reding589997a2019-01-25 11:22:52 +01001662 struct tegra_pmc *pmc = pinctrl_dev_get_drvdata(pctl_dev);
1663 const struct tegra_io_pad_soc *pad;
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001664 enum pin_config_param param;
1665 unsigned int i;
1666 int err;
1667 u32 arg;
1668
Thierry Reding589997a2019-01-25 11:22:52 +01001669 pad = tegra_io_pad_find(pmc, pin);
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001670 if (!pad)
1671 return -EINVAL;
1672
1673 for (i = 0; i < num_configs; ++i) {
1674 param = pinconf_to_config_param(configs[i]);
1675 arg = pinconf_to_config_argument(configs[i]);
1676
1677 switch (param) {
1678 case PIN_CONFIG_LOW_POWER_MODE:
1679 if (arg)
1680 err = tegra_io_pad_power_disable(pad->id);
1681 else
1682 err = tegra_io_pad_power_enable(pad->id);
1683 if (err)
1684 return err;
1685 break;
1686 case PIN_CONFIG_POWER_SOURCE:
1687 if (arg != TEGRA_IO_PAD_VOLTAGE_1V8 &&
1688 arg != TEGRA_IO_PAD_VOLTAGE_3V3)
1689 return -EINVAL;
Thierry Reding589997a2019-01-25 11:22:52 +01001690 err = tegra_io_pad_set_voltage(pmc, pad->id, arg);
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001691 if (err)
1692 return err;
1693 break;
1694 default:
1695 return -EINVAL;
1696 }
1697 }
1698
1699 return 0;
1700}
1701
1702static const struct pinconf_ops tegra_io_pad_pinconf_ops = {
1703 .pin_config_get = tegra_io_pad_pinconf_get,
1704 .pin_config_set = tegra_io_pad_pinconf_set,
1705 .is_generic = true,
1706};
1707
1708static struct pinctrl_desc tegra_pmc_pctl_desc = {
1709 .pctlops = &tegra_io_pad_pinctrl_ops,
1710 .confops = &tegra_io_pad_pinconf_ops,
1711};
1712
1713static int tegra_pmc_pinctrl_init(struct tegra_pmc *pmc)
1714{
Thierry Redingf1d91292019-01-25 11:22:53 +01001715 int err;
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001716
1717 if (!pmc->soc->num_pin_descs)
1718 return 0;
1719
1720 tegra_pmc_pctl_desc.name = dev_name(pmc->dev);
1721 tegra_pmc_pctl_desc.pins = pmc->soc->pin_descs;
1722 tegra_pmc_pctl_desc.npins = pmc->soc->num_pin_descs;
1723
1724 pmc->pctl_dev = devm_pinctrl_register(pmc->dev, &tegra_pmc_pctl_desc,
1725 pmc);
1726 if (IS_ERR(pmc->pctl_dev)) {
1727 err = PTR_ERR(pmc->pctl_dev);
Thierry Redingf1d91292019-01-25 11:22:53 +01001728 dev_err(pmc->dev, "failed to register pin controller: %d\n",
1729 err);
1730 return err;
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001731 }
1732
Thierry Redingf1d91292019-01-25 11:22:53 +01001733 return 0;
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001734}
1735
Sandipan Patra5f84bb12018-10-24 12:38:00 +05301736static ssize_t reset_reason_show(struct device *dev,
Thierry Redingf1d91292019-01-25 11:22:53 +01001737 struct device_attribute *attr, char *buf)
Sandipan Patra5f84bb12018-10-24 12:38:00 +05301738{
1739 u32 value, rst_src;
1740
Thierry Reding589997a2019-01-25 11:22:52 +01001741 value = tegra_pmc_readl(pmc, pmc->soc->regs->rst_status);
Sandipan Patra5f84bb12018-10-24 12:38:00 +05301742 rst_src = (value & pmc->soc->regs->rst_source_mask) >>
1743 pmc->soc->regs->rst_source_shift;
1744
1745 return sprintf(buf, "%s\n", pmc->soc->reset_sources[rst_src]);
1746}
1747
1748static DEVICE_ATTR_RO(reset_reason);
1749
1750static ssize_t reset_level_show(struct device *dev,
Thierry Redingf1d91292019-01-25 11:22:53 +01001751 struct device_attribute *attr, char *buf)
Sandipan Patra5f84bb12018-10-24 12:38:00 +05301752{
1753 u32 value, rst_lvl;
1754
Thierry Reding589997a2019-01-25 11:22:52 +01001755 value = tegra_pmc_readl(pmc, pmc->soc->regs->rst_status);
Sandipan Patra5f84bb12018-10-24 12:38:00 +05301756 rst_lvl = (value & pmc->soc->regs->rst_level_mask) >>
1757 pmc->soc->regs->rst_level_shift;
1758
1759 return sprintf(buf, "%s\n", pmc->soc->reset_levels[rst_lvl]);
1760}
1761
1762static DEVICE_ATTR_RO(reset_level);
1763
1764static void tegra_pmc_reset_sysfs_init(struct tegra_pmc *pmc)
1765{
1766 struct device *dev = pmc->dev;
1767 int err = 0;
1768
1769 if (pmc->soc->reset_sources) {
1770 err = device_create_file(dev, &dev_attr_reset_reason);
1771 if (err < 0)
1772 dev_warn(dev,
Thierry Redingf1d91292019-01-25 11:22:53 +01001773 "failed to create attr \"reset_reason\": %d\n",
1774 err);
Sandipan Patra5f84bb12018-10-24 12:38:00 +05301775 }
1776
1777 if (pmc->soc->reset_levels) {
1778 err = device_create_file(dev, &dev_attr_reset_level);
1779 if (err < 0)
1780 dev_warn(dev,
Thierry Redingf1d91292019-01-25 11:22:53 +01001781 "failed to create attr \"reset_level\": %d\n",
1782 err);
Sandipan Patra5f84bb12018-10-24 12:38:00 +05301783 }
1784}
1785
Thierry Reding19906e62018-09-17 15:08:17 +02001786static int tegra_pmc_irq_translate(struct irq_domain *domain,
1787 struct irq_fwspec *fwspec,
1788 unsigned long *hwirq,
1789 unsigned int *type)
1790{
1791 if (WARN_ON(fwspec->param_count < 2))
1792 return -EINVAL;
1793
1794 *hwirq = fwspec->param[0];
1795 *type = fwspec->param[1];
1796
1797 return 0;
1798}
1799
1800static int tegra_pmc_irq_alloc(struct irq_domain *domain, unsigned int virq,
1801 unsigned int num_irqs, void *data)
1802{
1803 struct tegra_pmc *pmc = domain->host_data;
1804 const struct tegra_pmc_soc *soc = pmc->soc;
1805 struct irq_fwspec *fwspec = data;
1806 unsigned int i;
1807 int err = 0;
1808
1809 for (i = 0; i < soc->num_wake_events; i++) {
1810 const struct tegra_wake_event *event = &soc->wake_events[i];
1811
1812 if (fwspec->param_count == 2) {
1813 struct irq_fwspec spec;
1814
1815 if (event->id != fwspec->param[0])
1816 continue;
1817
1818 err = irq_domain_set_hwirq_and_chip(domain, virq,
1819 event->id,
1820 &pmc->irq, pmc);
1821 if (err < 0)
1822 break;
1823
1824 spec.fwnode = &pmc->dev->of_node->fwnode;
1825 spec.param_count = 3;
1826 spec.param[0] = GIC_SPI;
1827 spec.param[1] = event->irq;
1828 spec.param[2] = fwspec->param[1];
1829
1830 err = irq_domain_alloc_irqs_parent(domain, virq,
1831 num_irqs, &spec);
1832
1833 break;
1834 }
1835
1836 if (fwspec->param_count == 3) {
1837 if (event->gpio.instance != fwspec->param[0] ||
1838 event->gpio.pin != fwspec->param[1])
1839 continue;
1840
1841 err = irq_domain_set_hwirq_and_chip(domain, virq,
1842 event->id,
1843 &pmc->irq, pmc);
1844
1845 break;
1846 }
1847 }
1848
1849 if (i == soc->num_wake_events)
1850 err = irq_domain_set_hwirq_and_chip(domain, virq, ULONG_MAX,
1851 &pmc->irq, pmc);
1852
1853 return err;
1854}
1855
1856static const struct irq_domain_ops tegra_pmc_irq_domain_ops = {
1857 .translate = tegra_pmc_irq_translate,
1858 .alloc = tegra_pmc_irq_alloc,
1859};
1860
1861static int tegra_pmc_irq_set_wake(struct irq_data *data, unsigned int on)
1862{
1863 struct tegra_pmc *pmc = irq_data_get_irq_chip_data(data);
1864 unsigned int offset, bit;
1865 u32 value;
1866
1867 offset = data->hwirq / 32;
1868 bit = data->hwirq % 32;
1869
1870 /* clear wake status */
1871 writel(0x1, pmc->wake + WAKE_AOWAKE_STATUS_W(data->hwirq));
1872
1873 /* route wake to tier 2 */
1874 value = readl(pmc->wake + WAKE_AOWAKE_TIER2_ROUTING(offset));
1875
1876 if (!on)
1877 value &= ~(1 << bit);
1878 else
1879 value |= 1 << bit;
1880
1881 writel(value, pmc->wake + WAKE_AOWAKE_TIER2_ROUTING(offset));
1882
1883 /* enable wakeup event */
1884 writel(!!on, pmc->wake + WAKE_AOWAKE_MASK_W(data->hwirq));
1885
1886 return 0;
1887}
1888
1889static int tegra_pmc_irq_set_type(struct irq_data *data, unsigned int type)
1890{
1891 struct tegra_pmc *pmc = irq_data_get_irq_chip_data(data);
1892 u32 value;
1893
1894 if (data->hwirq == ULONG_MAX)
1895 return 0;
1896
1897 value = readl(pmc->wake + WAKE_AOWAKE_CNTRL(data->hwirq));
1898
1899 switch (type) {
1900 case IRQ_TYPE_EDGE_RISING:
1901 case IRQ_TYPE_LEVEL_HIGH:
1902 value |= WAKE_AOWAKE_CNTRL_LEVEL;
1903 break;
1904
1905 case IRQ_TYPE_EDGE_FALLING:
1906 case IRQ_TYPE_LEVEL_LOW:
1907 value &= ~WAKE_AOWAKE_CNTRL_LEVEL;
1908 break;
1909
1910 case IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING:
1911 value ^= WAKE_AOWAKE_CNTRL_LEVEL;
1912 break;
1913
1914 default:
1915 return -EINVAL;
1916 }
1917
1918 writel(value, pmc->wake + WAKE_AOWAKE_CNTRL(data->hwirq));
1919
1920 return 0;
1921}
1922
1923static int tegra_pmc_irq_init(struct tegra_pmc *pmc)
1924{
1925 struct irq_domain *parent = NULL;
1926 struct device_node *np;
1927
1928 np = of_irq_find_parent(pmc->dev->of_node);
1929 if (np) {
1930 parent = irq_find_host(np);
1931 of_node_put(np);
1932 }
1933
1934 if (!parent)
1935 return 0;
1936
1937 pmc->irq.name = dev_name(pmc->dev);
1938 pmc->irq.irq_mask = irq_chip_mask_parent;
1939 pmc->irq.irq_unmask = irq_chip_unmask_parent;
1940 pmc->irq.irq_eoi = irq_chip_eoi_parent;
1941 pmc->irq.irq_set_affinity = irq_chip_set_affinity_parent;
1942 pmc->irq.irq_set_type = tegra_pmc_irq_set_type;
1943 pmc->irq.irq_set_wake = tegra_pmc_irq_set_wake;
1944
1945 pmc->domain = irq_domain_add_hierarchy(parent, 0, 96, pmc->dev->of_node,
1946 &tegra_pmc_irq_domain_ops, pmc);
1947 if (!pmc->domain) {
1948 dev_err(pmc->dev, "failed to allocate domain\n");
1949 return -ENOMEM;
1950 }
1951
1952 return 0;
1953}
1954
Thierry Reding72323982014-07-11 13:19:06 +02001955static int tegra_pmc_probe(struct platform_device *pdev)
1956{
Jon Huntere8cf6612016-02-11 18:03:21 +00001957 void __iomem *base;
Thierry Reding72323982014-07-11 13:19:06 +02001958 struct resource *res;
1959 int err;
1960
Jon Huntera83f1fc2016-06-28 11:38:28 +01001961 /*
1962 * Early initialisation should have configured an initial
1963 * register mapping and setup the soc data pointer. If these
1964 * are not valid then something went badly wrong!
1965 */
1966 if (WARN_ON(!pmc->base || !pmc->soc))
1967 return -ENODEV;
1968
Thierry Reding72323982014-07-11 13:19:06 +02001969 err = tegra_pmc_parse_dt(pmc, pdev->dev.of_node);
1970 if (err < 0)
1971 return err;
1972
1973 /* take over the memory region from the early initialization */
1974 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Jon Hunter0259f522016-02-11 18:03:20 +00001975 base = devm_ioremap_resource(&pdev->dev, res);
1976 if (IS_ERR(base))
1977 return PTR_ERR(base);
Thierry Reding72323982014-07-11 13:19:06 +02001978
Thierry Redingc641ec62017-08-30 12:42:34 +02001979 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "wake");
1980 if (res) {
1981 pmc->wake = devm_ioremap_resource(&pdev->dev, res);
1982 if (IS_ERR(pmc->wake))
1983 return PTR_ERR(pmc->wake);
1984 } else {
1985 pmc->wake = base;
1986 }
1987
1988 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "aotag");
1989 if (res) {
1990 pmc->aotag = devm_ioremap_resource(&pdev->dev, res);
1991 if (IS_ERR(pmc->aotag))
1992 return PTR_ERR(pmc->aotag);
1993 } else {
1994 pmc->aotag = base;
1995 }
1996
1997 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "scratch");
1998 if (res) {
1999 pmc->scratch = devm_ioremap_resource(&pdev->dev, res);
2000 if (IS_ERR(pmc->scratch))
2001 return PTR_ERR(pmc->scratch);
2002 } else {
2003 pmc->scratch = base;
2004 }
Thierry Reding5be22552017-08-30 12:32:58 +02002005
Thierry Reding72323982014-07-11 13:19:06 +02002006 pmc->clk = devm_clk_get(&pdev->dev, "pclk");
2007 if (IS_ERR(pmc->clk)) {
2008 err = PTR_ERR(pmc->clk);
Thierry Reding5be22552017-08-30 12:32:58 +02002009
2010 if (err != -ENOENT) {
2011 dev_err(&pdev->dev, "failed to get pclk: %d\n", err);
2012 return err;
2013 }
2014
2015 pmc->clk = NULL;
Thierry Reding72323982014-07-11 13:19:06 +02002016 }
2017
Mikko Perttunen3568df32015-01-06 12:52:58 +02002018 pmc->dev = &pdev->dev;
2019
Thierry Reding72323982014-07-11 13:19:06 +02002020 tegra_pmc_init(pmc);
2021
Mikko Perttunen3568df32015-01-06 12:52:58 +02002022 tegra_pmc_init_tsense_reset(pmc);
2023
Sandipan Patra5f84bb12018-10-24 12:38:00 +05302024 tegra_pmc_reset_sysfs_init(pmc);
2025
Thierry Reding72323982014-07-11 13:19:06 +02002026 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
2027 err = tegra_powergate_debugfs_init();
2028 if (err < 0)
2029 return err;
2030 }
2031
David Riley78921582015-03-18 10:52:25 +01002032 err = register_restart_handler(&tegra_pmc_restart_handler);
2033 if (err) {
2034 dev_err(&pdev->dev, "unable to register restart handler, %d\n",
2035 err);
Aapo Vienamo4a37f112018-08-10 21:08:12 +03002036 goto cleanup_debugfs;
David Riley78921582015-03-18 10:52:25 +01002037 }
2038
Aapo Vienamo4a37f112018-08-10 21:08:12 +03002039 err = tegra_pmc_pinctrl_init(pmc);
2040 if (err)
2041 goto cleanup_restart_handler;
2042
Thierry Reding19906e62018-09-17 15:08:17 +02002043 err = tegra_pmc_irq_init(pmc);
2044 if (err < 0)
2045 goto cleanup_restart_handler;
2046
Jon Huntere8cf6612016-02-11 18:03:21 +00002047 mutex_lock(&pmc->powergates_lock);
2048 iounmap(pmc->base);
Jon Hunter0259f522016-02-11 18:03:20 +00002049 pmc->base = base;
Jon Huntere8cf6612016-02-11 18:03:21 +00002050 mutex_unlock(&pmc->powergates_lock);
Jon Hunter0259f522016-02-11 18:03:20 +00002051
Thierry Reding589997a2019-01-25 11:22:52 +01002052 platform_set_drvdata(pdev, pmc);
2053
Thierry Reding72323982014-07-11 13:19:06 +02002054 return 0;
Aapo Vienamo4a37f112018-08-10 21:08:12 +03002055
2056cleanup_restart_handler:
2057 unregister_restart_handler(&tegra_pmc_restart_handler);
2058cleanup_debugfs:
2059 debugfs_remove(pmc->debugfs);
2060 return err;
Thierry Reding72323982014-07-11 13:19:06 +02002061}
2062
Paul Walmsley2b20b612014-12-09 22:36:50 +00002063#if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM)
Thierry Reding72323982014-07-11 13:19:06 +02002064static int tegra_pmc_suspend(struct device *dev)
2065{
Thierry Reding589997a2019-01-25 11:22:52 +01002066 struct tegra_pmc *pmc = dev_get_drvdata(dev);
2067
2068 tegra_pmc_writel(pmc, virt_to_phys(tegra_resume), PMC_SCRATCH41);
Thierry Reding72323982014-07-11 13:19:06 +02002069
2070 return 0;
2071}
2072
2073static int tegra_pmc_resume(struct device *dev)
2074{
Thierry Reding589997a2019-01-25 11:22:52 +01002075 struct tegra_pmc *pmc = dev_get_drvdata(dev);
2076
2077 tegra_pmc_writel(pmc, 0x0, PMC_SCRATCH41);
Thierry Reding72323982014-07-11 13:19:06 +02002078
2079 return 0;
2080}
Thierry Reding72323982014-07-11 13:19:06 +02002081
2082static SIMPLE_DEV_PM_OPS(tegra_pmc_pm_ops, tegra_pmc_suspend, tegra_pmc_resume);
2083
Paul Walmsley2b20b612014-12-09 22:36:50 +00002084#endif
2085
Thierry Reding72323982014-07-11 13:19:06 +02002086static const char * const tegra20_powergates[] = {
2087 [TEGRA_POWERGATE_CPU] = "cpu",
2088 [TEGRA_POWERGATE_3D] = "3d",
2089 [TEGRA_POWERGATE_VENC] = "venc",
2090 [TEGRA_POWERGATE_VDEC] = "vdec",
2091 [TEGRA_POWERGATE_PCIE] = "pcie",
2092 [TEGRA_POWERGATE_L2] = "l2",
2093 [TEGRA_POWERGATE_MPE] = "mpe",
2094};
2095
Thierry Reding5be22552017-08-30 12:32:58 +02002096static const struct tegra_pmc_regs tegra20_pmc_regs = {
2097 .scratch0 = 0x50,
2098 .dpd_req = 0x1b8,
2099 .dpd_status = 0x1bc,
2100 .dpd2_req = 0x1c0,
2101 .dpd2_status = 0x1c4,
Sandipan Patra5f84bb12018-10-24 12:38:00 +05302102 .rst_status = 0x1b4,
2103 .rst_source_shift = 0x0,
2104 .rst_source_mask = 0x7,
2105 .rst_level_shift = 0x0,
2106 .rst_level_mask = 0x0,
Thierry Reding5be22552017-08-30 12:32:58 +02002107};
2108
2109static void tegra20_pmc_init(struct tegra_pmc *pmc)
2110{
2111 u32 value;
2112
2113 /* Always enable CPU power request */
Thierry Reding589997a2019-01-25 11:22:52 +01002114 value = tegra_pmc_readl(pmc, PMC_CNTRL);
Thierry Reding5be22552017-08-30 12:32:58 +02002115 value |= PMC_CNTRL_CPU_PWRREQ_OE;
Thierry Reding589997a2019-01-25 11:22:52 +01002116 tegra_pmc_writel(pmc, value, PMC_CNTRL);
Thierry Reding5be22552017-08-30 12:32:58 +02002117
Thierry Reding589997a2019-01-25 11:22:52 +01002118 value = tegra_pmc_readl(pmc, PMC_CNTRL);
Thierry Reding5be22552017-08-30 12:32:58 +02002119
2120 if (pmc->sysclkreq_high)
2121 value &= ~PMC_CNTRL_SYSCLK_POLARITY;
2122 else
2123 value |= PMC_CNTRL_SYSCLK_POLARITY;
2124
2125 /* configure the output polarity while the request is tristated */
Thierry Reding589997a2019-01-25 11:22:52 +01002126 tegra_pmc_writel(pmc, value, PMC_CNTRL);
Thierry Reding5be22552017-08-30 12:32:58 +02002127
2128 /* now enable the request */
Thierry Reding589997a2019-01-25 11:22:52 +01002129 value = tegra_pmc_readl(pmc, PMC_CNTRL);
Thierry Reding5be22552017-08-30 12:32:58 +02002130 value |= PMC_CNTRL_SYSCLK_OE;
Thierry Reding589997a2019-01-25 11:22:52 +01002131 tegra_pmc_writel(pmc, value, PMC_CNTRL);
Thierry Reding5be22552017-08-30 12:32:58 +02002132}
2133
2134static void tegra20_pmc_setup_irq_polarity(struct tegra_pmc *pmc,
2135 struct device_node *np,
2136 bool invert)
2137{
2138 u32 value;
2139
Thierry Reding589997a2019-01-25 11:22:52 +01002140 value = tegra_pmc_readl(pmc, PMC_CNTRL);
Thierry Reding5be22552017-08-30 12:32:58 +02002141
2142 if (invert)
2143 value |= PMC_CNTRL_INTR_POLARITY;
2144 else
2145 value &= ~PMC_CNTRL_INTR_POLARITY;
2146
Thierry Reding589997a2019-01-25 11:22:52 +01002147 tegra_pmc_writel(pmc, value, PMC_CNTRL);
Thierry Reding5be22552017-08-30 12:32:58 +02002148}
2149
Thierry Reding72323982014-07-11 13:19:06 +02002150static const struct tegra_pmc_soc tegra20_pmc_soc = {
2151 .num_powergates = ARRAY_SIZE(tegra20_powergates),
2152 .powergates = tegra20_powergates,
2153 .num_cpu_powergates = 0,
2154 .cpu_powergates = NULL,
Mikko Perttunen3568df32015-01-06 12:52:58 +02002155 .has_tsense_reset = false,
Thierry Redinga9a40a42015-01-09 11:15:33 +01002156 .has_gpu_clamps = false,
Thierry Redingfa3bc042019-01-25 11:22:54 +01002157 .needs_mbist_war = false,
2158 .has_impl_33v_pwr = false,
Mikko Perttunene247dea2019-01-25 11:22:55 +01002159 .maybe_tz_only = false,
Thierry Reding5be22552017-08-30 12:32:58 +02002160 .num_io_pads = 0,
2161 .io_pads = NULL,
Aapo Vienamo4a37f112018-08-10 21:08:12 +03002162 .num_pin_descs = 0,
2163 .pin_descs = NULL,
Thierry Reding5be22552017-08-30 12:32:58 +02002164 .regs = &tegra20_pmc_regs,
2165 .init = tegra20_pmc_init,
2166 .setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
Sandipan Patra5f84bb12018-10-24 12:38:00 +05302167 .reset_sources = NULL,
2168 .num_reset_sources = 0,
2169 .reset_levels = NULL,
2170 .num_reset_levels = 0,
Thierry Reding72323982014-07-11 13:19:06 +02002171};
2172
2173static const char * const tegra30_powergates[] = {
2174 [TEGRA_POWERGATE_CPU] = "cpu0",
2175 [TEGRA_POWERGATE_3D] = "3d0",
2176 [TEGRA_POWERGATE_VENC] = "venc",
2177 [TEGRA_POWERGATE_VDEC] = "vdec",
2178 [TEGRA_POWERGATE_PCIE] = "pcie",
2179 [TEGRA_POWERGATE_L2] = "l2",
2180 [TEGRA_POWERGATE_MPE] = "mpe",
2181 [TEGRA_POWERGATE_HEG] = "heg",
2182 [TEGRA_POWERGATE_SATA] = "sata",
2183 [TEGRA_POWERGATE_CPU1] = "cpu1",
2184 [TEGRA_POWERGATE_CPU2] = "cpu2",
2185 [TEGRA_POWERGATE_CPU3] = "cpu3",
2186 [TEGRA_POWERGATE_CELP] = "celp",
2187 [TEGRA_POWERGATE_3D1] = "3d1",
2188};
2189
2190static const u8 tegra30_cpu_powergates[] = {
2191 TEGRA_POWERGATE_CPU,
2192 TEGRA_POWERGATE_CPU1,
2193 TEGRA_POWERGATE_CPU2,
2194 TEGRA_POWERGATE_CPU3,
2195};
2196
2197static const struct tegra_pmc_soc tegra30_pmc_soc = {
2198 .num_powergates = ARRAY_SIZE(tegra30_powergates),
2199 .powergates = tegra30_powergates,
2200 .num_cpu_powergates = ARRAY_SIZE(tegra30_cpu_powergates),
2201 .cpu_powergates = tegra30_cpu_powergates,
Mikko Perttunen3568df32015-01-06 12:52:58 +02002202 .has_tsense_reset = true,
Thierry Redinga9a40a42015-01-09 11:15:33 +01002203 .has_gpu_clamps = false,
Thierry Redingfa3bc042019-01-25 11:22:54 +01002204 .needs_mbist_war = false,
Aapo Vienamo13136a42018-08-10 21:08:07 +03002205 .has_impl_33v_pwr = false,
Mikko Perttunene247dea2019-01-25 11:22:55 +01002206 .maybe_tz_only = false,
Thierry Reding5be22552017-08-30 12:32:58 +02002207 .num_io_pads = 0,
2208 .io_pads = NULL,
Aapo Vienamo4a37f112018-08-10 21:08:12 +03002209 .num_pin_descs = 0,
2210 .pin_descs = NULL,
Thierry Reding5be22552017-08-30 12:32:58 +02002211 .regs = &tegra20_pmc_regs,
2212 .init = tegra20_pmc_init,
2213 .setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
Sandipan Patra5f84bb12018-10-24 12:38:00 +05302214 .reset_sources = tegra30_reset_sources,
2215 .num_reset_sources = 5,
2216 .reset_levels = NULL,
2217 .num_reset_levels = 0,
Thierry Reding72323982014-07-11 13:19:06 +02002218};
2219
2220static const char * const tegra114_powergates[] = {
2221 [TEGRA_POWERGATE_CPU] = "crail",
2222 [TEGRA_POWERGATE_3D] = "3d",
2223 [TEGRA_POWERGATE_VENC] = "venc",
2224 [TEGRA_POWERGATE_VDEC] = "vdec",
2225 [TEGRA_POWERGATE_MPE] = "mpe",
2226 [TEGRA_POWERGATE_HEG] = "heg",
2227 [TEGRA_POWERGATE_CPU1] = "cpu1",
2228 [TEGRA_POWERGATE_CPU2] = "cpu2",
2229 [TEGRA_POWERGATE_CPU3] = "cpu3",
2230 [TEGRA_POWERGATE_CELP] = "celp",
2231 [TEGRA_POWERGATE_CPU0] = "cpu0",
2232 [TEGRA_POWERGATE_C0NC] = "c0nc",
2233 [TEGRA_POWERGATE_C1NC] = "c1nc",
2234 [TEGRA_POWERGATE_DIS] = "dis",
2235 [TEGRA_POWERGATE_DISB] = "disb",
2236 [TEGRA_POWERGATE_XUSBA] = "xusba",
2237 [TEGRA_POWERGATE_XUSBB] = "xusbb",
2238 [TEGRA_POWERGATE_XUSBC] = "xusbc",
2239};
2240
2241static const u8 tegra114_cpu_powergates[] = {
2242 TEGRA_POWERGATE_CPU0,
2243 TEGRA_POWERGATE_CPU1,
2244 TEGRA_POWERGATE_CPU2,
2245 TEGRA_POWERGATE_CPU3,
2246};
2247
2248static const struct tegra_pmc_soc tegra114_pmc_soc = {
2249 .num_powergates = ARRAY_SIZE(tegra114_powergates),
2250 .powergates = tegra114_powergates,
2251 .num_cpu_powergates = ARRAY_SIZE(tegra114_cpu_powergates),
2252 .cpu_powergates = tegra114_cpu_powergates,
Mikko Perttunen3568df32015-01-06 12:52:58 +02002253 .has_tsense_reset = true,
Thierry Redinga9a40a42015-01-09 11:15:33 +01002254 .has_gpu_clamps = false,
Thierry Redingfa3bc042019-01-25 11:22:54 +01002255 .needs_mbist_war = false,
Aapo Vienamo13136a42018-08-10 21:08:07 +03002256 .has_impl_33v_pwr = false,
Mikko Perttunene247dea2019-01-25 11:22:55 +01002257 .maybe_tz_only = false,
Thierry Reding5be22552017-08-30 12:32:58 +02002258 .num_io_pads = 0,
2259 .io_pads = NULL,
Aapo Vienamo4a37f112018-08-10 21:08:12 +03002260 .num_pin_descs = 0,
2261 .pin_descs = NULL,
Thierry Reding5be22552017-08-30 12:32:58 +02002262 .regs = &tegra20_pmc_regs,
2263 .init = tegra20_pmc_init,
2264 .setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
Sandipan Patra5f84bb12018-10-24 12:38:00 +05302265 .reset_sources = tegra30_reset_sources,
2266 .num_reset_sources = 5,
2267 .reset_levels = NULL,
2268 .num_reset_levels = 0,
Thierry Reding72323982014-07-11 13:19:06 +02002269};
2270
2271static const char * const tegra124_powergates[] = {
2272 [TEGRA_POWERGATE_CPU] = "crail",
2273 [TEGRA_POWERGATE_3D] = "3d",
2274 [TEGRA_POWERGATE_VENC] = "venc",
2275 [TEGRA_POWERGATE_PCIE] = "pcie",
2276 [TEGRA_POWERGATE_VDEC] = "vdec",
Thierry Reding72323982014-07-11 13:19:06 +02002277 [TEGRA_POWERGATE_MPE] = "mpe",
2278 [TEGRA_POWERGATE_HEG] = "heg",
2279 [TEGRA_POWERGATE_SATA] = "sata",
2280 [TEGRA_POWERGATE_CPU1] = "cpu1",
2281 [TEGRA_POWERGATE_CPU2] = "cpu2",
2282 [TEGRA_POWERGATE_CPU3] = "cpu3",
2283 [TEGRA_POWERGATE_CELP] = "celp",
2284 [TEGRA_POWERGATE_CPU0] = "cpu0",
2285 [TEGRA_POWERGATE_C0NC] = "c0nc",
2286 [TEGRA_POWERGATE_C1NC] = "c1nc",
2287 [TEGRA_POWERGATE_SOR] = "sor",
2288 [TEGRA_POWERGATE_DIS] = "dis",
2289 [TEGRA_POWERGATE_DISB] = "disb",
2290 [TEGRA_POWERGATE_XUSBA] = "xusba",
2291 [TEGRA_POWERGATE_XUSBB] = "xusbb",
2292 [TEGRA_POWERGATE_XUSBC] = "xusbc",
2293 [TEGRA_POWERGATE_VIC] = "vic",
2294 [TEGRA_POWERGATE_IRAM] = "iram",
2295};
2296
2297static const u8 tegra124_cpu_powergates[] = {
2298 TEGRA_POWERGATE_CPU0,
2299 TEGRA_POWERGATE_CPU1,
2300 TEGRA_POWERGATE_CPU2,
2301 TEGRA_POWERGATE_CPU3,
2302};
2303
Aapo Vienamo437c4f22018-08-10 21:08:10 +03002304#define TEGRA_IO_PAD(_id, _dpd, _voltage, _name) \
2305 ((struct tegra_io_pad_soc) { \
2306 .id = (_id), \
2307 .dpd = (_dpd), \
2308 .voltage = (_voltage), \
2309 .name = (_name), \
2310 })
2311
Aapo Vienamo4a37f112018-08-10 21:08:12 +03002312#define TEGRA_IO_PIN_DESC(_id, _dpd, _voltage, _name) \
2313 ((struct pinctrl_pin_desc) { \
2314 .number = (_id), \
2315 .name = (_name) \
2316 })
2317
Aapo Vienamo437c4f22018-08-10 21:08:10 +03002318#define TEGRA124_IO_PAD_TABLE(_pad) \
2319 /* .id .dpd .voltage .name */ \
2320 _pad(TEGRA_IO_PAD_AUDIO, 17, UINT_MAX, "audio"), \
2321 _pad(TEGRA_IO_PAD_BB, 15, UINT_MAX, "bb"), \
2322 _pad(TEGRA_IO_PAD_CAM, 36, UINT_MAX, "cam"), \
2323 _pad(TEGRA_IO_PAD_COMP, 22, UINT_MAX, "comp"), \
2324 _pad(TEGRA_IO_PAD_CSIA, 0, UINT_MAX, "csia"), \
2325 _pad(TEGRA_IO_PAD_CSIB, 1, UINT_MAX, "csb"), \
2326 _pad(TEGRA_IO_PAD_CSIE, 44, UINT_MAX, "cse"), \
2327 _pad(TEGRA_IO_PAD_DSI, 2, UINT_MAX, "dsi"), \
2328 _pad(TEGRA_IO_PAD_DSIB, 39, UINT_MAX, "dsib"), \
2329 _pad(TEGRA_IO_PAD_DSIC, 40, UINT_MAX, "dsic"), \
2330 _pad(TEGRA_IO_PAD_DSID, 41, UINT_MAX, "dsid"), \
2331 _pad(TEGRA_IO_PAD_HDMI, 28, UINT_MAX, "hdmi"), \
2332 _pad(TEGRA_IO_PAD_HSIC, 19, UINT_MAX, "hsic"), \
2333 _pad(TEGRA_IO_PAD_HV, 38, UINT_MAX, "hv"), \
2334 _pad(TEGRA_IO_PAD_LVDS, 57, UINT_MAX, "lvds"), \
2335 _pad(TEGRA_IO_PAD_MIPI_BIAS, 3, UINT_MAX, "mipi-bias"), \
2336 _pad(TEGRA_IO_PAD_NAND, 13, UINT_MAX, "nand"), \
2337 _pad(TEGRA_IO_PAD_PEX_BIAS, 4, UINT_MAX, "pex-bias"), \
2338 _pad(TEGRA_IO_PAD_PEX_CLK1, 5, UINT_MAX, "pex-clk1"), \
2339 _pad(TEGRA_IO_PAD_PEX_CLK2, 6, UINT_MAX, "pex-clk2"), \
2340 _pad(TEGRA_IO_PAD_PEX_CNTRL, 32, UINT_MAX, "pex-cntrl"), \
2341 _pad(TEGRA_IO_PAD_SDMMC1, 33, UINT_MAX, "sdmmc1"), \
2342 _pad(TEGRA_IO_PAD_SDMMC3, 34, UINT_MAX, "sdmmc3"), \
2343 _pad(TEGRA_IO_PAD_SDMMC4, 35, UINT_MAX, "sdmmc4"), \
2344 _pad(TEGRA_IO_PAD_SYS_DDC, 58, UINT_MAX, "sys_ddc"), \
2345 _pad(TEGRA_IO_PAD_UART, 14, UINT_MAX, "uart"), \
2346 _pad(TEGRA_IO_PAD_USB0, 9, UINT_MAX, "usb0"), \
2347 _pad(TEGRA_IO_PAD_USB1, 10, UINT_MAX, "usb1"), \
2348 _pad(TEGRA_IO_PAD_USB2, 11, UINT_MAX, "usb2"), \
2349 _pad(TEGRA_IO_PAD_USB_BIAS, 12, UINT_MAX, "usb_bias")
2350
Laxman Dewangan21b49912016-10-10 15:14:34 +02002351static const struct tegra_io_pad_soc tegra124_io_pads[] = {
Aapo Vienamo437c4f22018-08-10 21:08:10 +03002352 TEGRA124_IO_PAD_TABLE(TEGRA_IO_PAD)
Laxman Dewangan21b49912016-10-10 15:14:34 +02002353};
2354
Aapo Vienamo4a37f112018-08-10 21:08:12 +03002355static const struct pinctrl_pin_desc tegra124_pin_descs[] = {
2356 TEGRA124_IO_PAD_TABLE(TEGRA_IO_PIN_DESC)
Thierry Reding72323982014-07-11 13:19:06 +02002357};
2358
2359static const struct tegra_pmc_soc tegra124_pmc_soc = {
2360 .num_powergates = ARRAY_SIZE(tegra124_powergates),
2361 .powergates = tegra124_powergates,
2362 .num_cpu_powergates = ARRAY_SIZE(tegra124_cpu_powergates),
2363 .cpu_powergates = tegra124_cpu_powergates,
Mikko Perttunen3568df32015-01-06 12:52:58 +02002364 .has_tsense_reset = true,
Thierry Redinga9a40a42015-01-09 11:15:33 +01002365 .has_gpu_clamps = true,
Thierry Redingfa3bc042019-01-25 11:22:54 +01002366 .needs_mbist_war = false,
Aapo Vienamo13136a42018-08-10 21:08:07 +03002367 .has_impl_33v_pwr = false,
Mikko Perttunene247dea2019-01-25 11:22:55 +01002368 .maybe_tz_only = false,
Laxman Dewangan21b49912016-10-10 15:14:34 +02002369 .num_io_pads = ARRAY_SIZE(tegra124_io_pads),
2370 .io_pads = tegra124_io_pads,
Aapo Vienamo4a37f112018-08-10 21:08:12 +03002371 .num_pin_descs = ARRAY_SIZE(tegra124_pin_descs),
2372 .pin_descs = tegra124_pin_descs,
Thierry Reding5be22552017-08-30 12:32:58 +02002373 .regs = &tegra20_pmc_regs,
2374 .init = tegra20_pmc_init,
2375 .setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
Sandipan Patra5f84bb12018-10-24 12:38:00 +05302376 .reset_sources = tegra30_reset_sources,
2377 .num_reset_sources = 5,
2378 .reset_levels = NULL,
2379 .num_reset_levels = 0,
Thierry Reding72323982014-07-11 13:19:06 +02002380};
2381
Thierry Redingc2fe4692015-03-23 11:31:29 +01002382static const char * const tegra210_powergates[] = {
2383 [TEGRA_POWERGATE_CPU] = "crail",
2384 [TEGRA_POWERGATE_3D] = "3d",
2385 [TEGRA_POWERGATE_VENC] = "venc",
2386 [TEGRA_POWERGATE_PCIE] = "pcie",
Thierry Redingc2fe4692015-03-23 11:31:29 +01002387 [TEGRA_POWERGATE_MPE] = "mpe",
Thierry Redingc2fe4692015-03-23 11:31:29 +01002388 [TEGRA_POWERGATE_SATA] = "sata",
2389 [TEGRA_POWERGATE_CPU1] = "cpu1",
2390 [TEGRA_POWERGATE_CPU2] = "cpu2",
2391 [TEGRA_POWERGATE_CPU3] = "cpu3",
Thierry Redingc2fe4692015-03-23 11:31:29 +01002392 [TEGRA_POWERGATE_CPU0] = "cpu0",
2393 [TEGRA_POWERGATE_C0NC] = "c0nc",
Thierry Redingc2fe4692015-03-23 11:31:29 +01002394 [TEGRA_POWERGATE_SOR] = "sor",
2395 [TEGRA_POWERGATE_DIS] = "dis",
2396 [TEGRA_POWERGATE_DISB] = "disb",
2397 [TEGRA_POWERGATE_XUSBA] = "xusba",
2398 [TEGRA_POWERGATE_XUSBB] = "xusbb",
2399 [TEGRA_POWERGATE_XUSBC] = "xusbc",
2400 [TEGRA_POWERGATE_VIC] = "vic",
2401 [TEGRA_POWERGATE_IRAM] = "iram",
2402 [TEGRA_POWERGATE_NVDEC] = "nvdec",
2403 [TEGRA_POWERGATE_NVJPG] = "nvjpg",
2404 [TEGRA_POWERGATE_AUD] = "aud",
2405 [TEGRA_POWERGATE_DFD] = "dfd",
2406 [TEGRA_POWERGATE_VE2] = "ve2",
2407};
2408
2409static const u8 tegra210_cpu_powergates[] = {
2410 TEGRA_POWERGATE_CPU0,
2411 TEGRA_POWERGATE_CPU1,
2412 TEGRA_POWERGATE_CPU2,
2413 TEGRA_POWERGATE_CPU3,
2414};
2415
Aapo Vienamo437c4f22018-08-10 21:08:10 +03002416#define TEGRA210_IO_PAD_TABLE(_pad) \
2417 /* .id .dpd .voltage .name */ \
2418 _pad(TEGRA_IO_PAD_AUDIO, 17, 5, "audio"), \
2419 _pad(TEGRA_IO_PAD_AUDIO_HV, 61, 18, "audio-hv"), \
2420 _pad(TEGRA_IO_PAD_CAM, 36, 10, "cam"), \
2421 _pad(TEGRA_IO_PAD_CSIA, 0, UINT_MAX, "csia"), \
2422 _pad(TEGRA_IO_PAD_CSIB, 1, UINT_MAX, "csib"), \
2423 _pad(TEGRA_IO_PAD_CSIC, 42, UINT_MAX, "csic"), \
2424 _pad(TEGRA_IO_PAD_CSID, 43, UINT_MAX, "csid"), \
2425 _pad(TEGRA_IO_PAD_CSIE, 44, UINT_MAX, "csie"), \
2426 _pad(TEGRA_IO_PAD_CSIF, 45, UINT_MAX, "csif"), \
2427 _pad(TEGRA_IO_PAD_DBG, 25, 19, "dbg"), \
2428 _pad(TEGRA_IO_PAD_DEBUG_NONAO, 26, UINT_MAX, "debug-nonao"), \
2429 _pad(TEGRA_IO_PAD_DMIC, 50, 20, "dmic"), \
2430 _pad(TEGRA_IO_PAD_DP, 51, UINT_MAX, "dp"), \
2431 _pad(TEGRA_IO_PAD_DSI, 2, UINT_MAX, "dsi"), \
2432 _pad(TEGRA_IO_PAD_DSIB, 39, UINT_MAX, "dsib"), \
2433 _pad(TEGRA_IO_PAD_DSIC, 40, UINT_MAX, "dsic"), \
2434 _pad(TEGRA_IO_PAD_DSID, 41, UINT_MAX, "dsid"), \
2435 _pad(TEGRA_IO_PAD_EMMC, 35, UINT_MAX, "emmc"), \
2436 _pad(TEGRA_IO_PAD_EMMC2, 37, UINT_MAX, "emmc2"), \
2437 _pad(TEGRA_IO_PAD_GPIO, 27, 21, "gpio"), \
2438 _pad(TEGRA_IO_PAD_HDMI, 28, UINT_MAX, "hdmi"), \
2439 _pad(TEGRA_IO_PAD_HSIC, 19, UINT_MAX, "hsic"), \
2440 _pad(TEGRA_IO_PAD_LVDS, 57, UINT_MAX, "lvds"), \
2441 _pad(TEGRA_IO_PAD_MIPI_BIAS, 3, UINT_MAX, "mipi-bias"), \
2442 _pad(TEGRA_IO_PAD_PEX_BIAS, 4, UINT_MAX, "pex-bias"), \
2443 _pad(TEGRA_IO_PAD_PEX_CLK1, 5, UINT_MAX, "pex-clk1"), \
2444 _pad(TEGRA_IO_PAD_PEX_CLK2, 6, UINT_MAX, "pex-clk2"), \
2445 _pad(TEGRA_IO_PAD_PEX_CNTRL, UINT_MAX, 11, "pex-cntrl"), \
2446 _pad(TEGRA_IO_PAD_SDMMC1, 33, 12, "sdmmc1"), \
2447 _pad(TEGRA_IO_PAD_SDMMC3, 34, 13, "sdmmc3"), \
2448 _pad(TEGRA_IO_PAD_SPI, 46, 22, "spi"), \
2449 _pad(TEGRA_IO_PAD_SPI_HV, 47, 23, "spi-hv"), \
2450 _pad(TEGRA_IO_PAD_UART, 14, 2, "uart"), \
2451 _pad(TEGRA_IO_PAD_USB0, 9, UINT_MAX, "usb0"), \
2452 _pad(TEGRA_IO_PAD_USB1, 10, UINT_MAX, "usb1"), \
2453 _pad(TEGRA_IO_PAD_USB2, 11, UINT_MAX, "usb2"), \
2454 _pad(TEGRA_IO_PAD_USB3, 18, UINT_MAX, "usb3"), \
2455 _pad(TEGRA_IO_PAD_USB_BIAS, 12, UINT_MAX, "usb-bias")
2456
Laxman Dewangan21b49912016-10-10 15:14:34 +02002457static const struct tegra_io_pad_soc tegra210_io_pads[] = {
Aapo Vienamo437c4f22018-08-10 21:08:10 +03002458 TEGRA210_IO_PAD_TABLE(TEGRA_IO_PAD)
Laxman Dewangan21b49912016-10-10 15:14:34 +02002459};
2460
Aapo Vienamo4a37f112018-08-10 21:08:12 +03002461static const struct pinctrl_pin_desc tegra210_pin_descs[] = {
2462 TEGRA210_IO_PAD_TABLE(TEGRA_IO_PIN_DESC)
Thierry Redingc2fe4692015-03-23 11:31:29 +01002463};
2464
2465static const struct tegra_pmc_soc tegra210_pmc_soc = {
2466 .num_powergates = ARRAY_SIZE(tegra210_powergates),
2467 .powergates = tegra210_powergates,
2468 .num_cpu_powergates = ARRAY_SIZE(tegra210_cpu_powergates),
2469 .cpu_powergates = tegra210_cpu_powergates,
2470 .has_tsense_reset = true,
2471 .has_gpu_clamps = true,
Peter De Schrijvera263394a2018-01-25 16:00:13 +02002472 .needs_mbist_war = true,
Thierry Redingfa3bc042019-01-25 11:22:54 +01002473 .has_impl_33v_pwr = false,
Mikko Perttunene247dea2019-01-25 11:22:55 +01002474 .maybe_tz_only = true,
Laxman Dewangan21b49912016-10-10 15:14:34 +02002475 .num_io_pads = ARRAY_SIZE(tegra210_io_pads),
2476 .io_pads = tegra210_io_pads,
Aapo Vienamo4a37f112018-08-10 21:08:12 +03002477 .num_pin_descs = ARRAY_SIZE(tegra210_pin_descs),
2478 .pin_descs = tegra210_pin_descs,
Thierry Reding5be22552017-08-30 12:32:58 +02002479 .regs = &tegra20_pmc_regs,
2480 .init = tegra20_pmc_init,
2481 .setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
Sandipan Patra5f84bb12018-10-24 12:38:00 +05302482 .reset_sources = tegra30_reset_sources,
2483 .num_reset_sources = 5,
2484 .reset_levels = NULL,
2485 .num_reset_levels = 0,
Thierry Redingc2fe4692015-03-23 11:31:29 +01002486};
2487
Aapo Vienamo437c4f22018-08-10 21:08:10 +03002488#define TEGRA186_IO_PAD_TABLE(_pad) \
2489 /* .id .dpd .voltage .name */ \
2490 _pad(TEGRA_IO_PAD_CSIA, 0, UINT_MAX, "csia"), \
2491 _pad(TEGRA_IO_PAD_CSIB, 1, UINT_MAX, "csib"), \
2492 _pad(TEGRA_IO_PAD_DSI, 2, UINT_MAX, "dsi"), \
2493 _pad(TEGRA_IO_PAD_MIPI_BIAS, 3, UINT_MAX, "mipi-bias"), \
2494 _pad(TEGRA_IO_PAD_PEX_CLK_BIAS, 4, UINT_MAX, "pex-clk-bias"), \
2495 _pad(TEGRA_IO_PAD_PEX_CLK3, 5, UINT_MAX, "pex-clk3"), \
2496 _pad(TEGRA_IO_PAD_PEX_CLK2, 6, UINT_MAX, "pex-clk2"), \
2497 _pad(TEGRA_IO_PAD_PEX_CLK1, 7, UINT_MAX, "pex-clk1"), \
2498 _pad(TEGRA_IO_PAD_USB0, 9, UINT_MAX, "usb0"), \
2499 _pad(TEGRA_IO_PAD_USB1, 10, UINT_MAX, "usb1"), \
2500 _pad(TEGRA_IO_PAD_USB2, 11, UINT_MAX, "usb2"), \
2501 _pad(TEGRA_IO_PAD_USB_BIAS, 12, UINT_MAX, "usb-bias"), \
2502 _pad(TEGRA_IO_PAD_UART, 14, UINT_MAX, "uart"), \
2503 _pad(TEGRA_IO_PAD_AUDIO, 17, UINT_MAX, "audio"), \
2504 _pad(TEGRA_IO_PAD_HSIC, 19, UINT_MAX, "hsic"), \
2505 _pad(TEGRA_IO_PAD_DBG, 25, UINT_MAX, "dbg"), \
2506 _pad(TEGRA_IO_PAD_HDMI_DP0, 28, UINT_MAX, "hdmi-dp0"), \
2507 _pad(TEGRA_IO_PAD_HDMI_DP1, 29, UINT_MAX, "hdmi-dp1"), \
2508 _pad(TEGRA_IO_PAD_PEX_CNTRL, 32, UINT_MAX, "pex-cntrl"), \
2509 _pad(TEGRA_IO_PAD_SDMMC2_HV, 34, 5, "sdmmc2-hv"), \
2510 _pad(TEGRA_IO_PAD_SDMMC4, 36, UINT_MAX, "sdmmc4"), \
2511 _pad(TEGRA_IO_PAD_CAM, 38, UINT_MAX, "cam"), \
2512 _pad(TEGRA_IO_PAD_DSIB, 40, UINT_MAX, "dsib"), \
2513 _pad(TEGRA_IO_PAD_DSIC, 41, UINT_MAX, "dsic"), \
2514 _pad(TEGRA_IO_PAD_DSID, 42, UINT_MAX, "dsid"), \
2515 _pad(TEGRA_IO_PAD_CSIC, 43, UINT_MAX, "csic"), \
2516 _pad(TEGRA_IO_PAD_CSID, 44, UINT_MAX, "csid"), \
2517 _pad(TEGRA_IO_PAD_CSIE, 45, UINT_MAX, "csie"), \
2518 _pad(TEGRA_IO_PAD_CSIF, 46, UINT_MAX, "csif"), \
2519 _pad(TEGRA_IO_PAD_SPI, 47, UINT_MAX, "spi"), \
2520 _pad(TEGRA_IO_PAD_UFS, 49, UINT_MAX, "ufs"), \
2521 _pad(TEGRA_IO_PAD_DMIC_HV, 52, 2, "dmic-hv"), \
2522 _pad(TEGRA_IO_PAD_EDP, 53, UINT_MAX, "edp"), \
2523 _pad(TEGRA_IO_PAD_SDMMC1_HV, 55, 4, "sdmmc1-hv"), \
2524 _pad(TEGRA_IO_PAD_SDMMC3_HV, 56, 6, "sdmmc3-hv"), \
2525 _pad(TEGRA_IO_PAD_CONN, 60, UINT_MAX, "conn"), \
2526 _pad(TEGRA_IO_PAD_AUDIO_HV, 61, 1, "audio-hv"), \
2527 _pad(TEGRA_IO_PAD_AO_HV, UINT_MAX, 0, "ao-hv")
2528
Thierry Redingc641ec62017-08-30 12:42:34 +02002529static const struct tegra_io_pad_soc tegra186_io_pads[] = {
Aapo Vienamo437c4f22018-08-10 21:08:10 +03002530 TEGRA186_IO_PAD_TABLE(TEGRA_IO_PAD)
Thierry Redingc641ec62017-08-30 12:42:34 +02002531};
2532
Aapo Vienamo4a37f112018-08-10 21:08:12 +03002533static const struct pinctrl_pin_desc tegra186_pin_descs[] = {
2534 TEGRA186_IO_PAD_TABLE(TEGRA_IO_PIN_DESC)
Thierry Redingc641ec62017-08-30 12:42:34 +02002535};
2536
2537static const struct tegra_pmc_regs tegra186_pmc_regs = {
2538 .scratch0 = 0x2000,
2539 .dpd_req = 0x74,
2540 .dpd_status = 0x78,
2541 .dpd2_req = 0x7c,
2542 .dpd2_status = 0x80,
Sandipan Patra5f84bb12018-10-24 12:38:00 +05302543 .rst_status = 0x70,
2544 .rst_source_shift = 0x2,
2545 .rst_source_mask = 0x3C,
2546 .rst_level_shift = 0x0,
2547 .rst_level_mask = 0x3,
Thierry Redingc641ec62017-08-30 12:42:34 +02002548};
2549
2550static void tegra186_pmc_setup_irq_polarity(struct tegra_pmc *pmc,
2551 struct device_node *np,
2552 bool invert)
2553{
2554 struct resource regs;
2555 void __iomem *wake;
2556 u32 value;
2557 int index;
2558
2559 index = of_property_match_string(np, "reg-names", "wake");
2560 if (index < 0) {
Thierry Reding589997a2019-01-25 11:22:52 +01002561 dev_err(pmc->dev, "failed to find PMC wake registers\n");
Thierry Redingc641ec62017-08-30 12:42:34 +02002562 return;
2563 }
2564
2565 of_address_to_resource(np, index, &regs);
2566
2567 wake = ioremap_nocache(regs.start, resource_size(&regs));
2568 if (!wake) {
Thierry Reding589997a2019-01-25 11:22:52 +01002569 dev_err(pmc->dev, "failed to map PMC wake registers\n");
Thierry Redingc641ec62017-08-30 12:42:34 +02002570 return;
2571 }
2572
2573 value = readl(wake + WAKE_AOWAKE_CTRL);
2574
2575 if (invert)
2576 value |= WAKE_AOWAKE_CTRL_INTR_POLARITY;
2577 else
2578 value &= ~WAKE_AOWAKE_CTRL_INTR_POLARITY;
2579
2580 writel(value, wake + WAKE_AOWAKE_CTRL);
2581
2582 iounmap(wake);
2583}
2584
Thierry Redinge59333c2018-09-19 18:41:59 +02002585static const struct tegra_wake_event tegra186_wake_events[] = {
Thierry Reding532700e2019-01-23 12:38:43 +01002586 TEGRA_WAKE_GPIO("power", 29, 1, TEGRA186_AON_GPIO(FF, 0)),
Thierry Redinge59333c2018-09-19 18:41:59 +02002587 TEGRA_WAKE_IRQ("rtc", 73, 10),
2588};
2589
Thierry Redingc641ec62017-08-30 12:42:34 +02002590static const struct tegra_pmc_soc tegra186_pmc_soc = {
2591 .num_powergates = 0,
2592 .powergates = NULL,
2593 .num_cpu_powergates = 0,
2594 .cpu_powergates = NULL,
2595 .has_tsense_reset = false,
2596 .has_gpu_clamps = false,
Thierry Redingfa3bc042019-01-25 11:22:54 +01002597 .needs_mbist_war = false,
Aapo Vienamo13136a42018-08-10 21:08:07 +03002598 .has_impl_33v_pwr = true,
Mikko Perttunene247dea2019-01-25 11:22:55 +01002599 .maybe_tz_only = false,
Thierry Redingc641ec62017-08-30 12:42:34 +02002600 .num_io_pads = ARRAY_SIZE(tegra186_io_pads),
2601 .io_pads = tegra186_io_pads,
Aapo Vienamo4a37f112018-08-10 21:08:12 +03002602 .num_pin_descs = ARRAY_SIZE(tegra186_pin_descs),
2603 .pin_descs = tegra186_pin_descs,
Thierry Redingc641ec62017-08-30 12:42:34 +02002604 .regs = &tegra186_pmc_regs,
2605 .init = NULL,
2606 .setup_irq_polarity = tegra186_pmc_setup_irq_polarity,
Sandipan Patra5f84bb12018-10-24 12:38:00 +05302607 .reset_sources = tegra186_reset_sources,
2608 .num_reset_sources = 14,
2609 .reset_levels = tegra186_reset_levels,
2610 .num_reset_levels = 3,
Thierry Redinge59333c2018-09-19 18:41:59 +02002611 .num_wake_events = ARRAY_SIZE(tegra186_wake_events),
2612 .wake_events = tegra186_wake_events,
Thierry Redingc641ec62017-08-30 12:42:34 +02002613};
2614
Thierry Redingeac9c482018-01-25 14:43:45 +02002615static const struct tegra_io_pad_soc tegra194_io_pads[] = {
2616 { .id = TEGRA_IO_PAD_CSIA, .dpd = 0, .voltage = UINT_MAX },
2617 { .id = TEGRA_IO_PAD_CSIB, .dpd = 1, .voltage = UINT_MAX },
2618 { .id = TEGRA_IO_PAD_MIPI_BIAS, .dpd = 3, .voltage = UINT_MAX },
2619 { .id = TEGRA_IO_PAD_PEX_CLK_BIAS, .dpd = 4, .voltage = UINT_MAX },
2620 { .id = TEGRA_IO_PAD_PEX_CLK3, .dpd = 5, .voltage = UINT_MAX },
2621 { .id = TEGRA_IO_PAD_PEX_CLK2, .dpd = 6, .voltage = UINT_MAX },
2622 { .id = TEGRA_IO_PAD_PEX_CLK1, .dpd = 7, .voltage = UINT_MAX },
2623 { .id = TEGRA_IO_PAD_EQOS, .dpd = 8, .voltage = UINT_MAX },
2624 { .id = TEGRA_IO_PAD_PEX_CLK2_BIAS, .dpd = 9, .voltage = UINT_MAX },
2625 { .id = TEGRA_IO_PAD_PEX_CLK2, .dpd = 10, .voltage = UINT_MAX },
2626 { .id = TEGRA_IO_PAD_DAP3, .dpd = 11, .voltage = UINT_MAX },
2627 { .id = TEGRA_IO_PAD_DAP5, .dpd = 12, .voltage = UINT_MAX },
2628 { .id = TEGRA_IO_PAD_UART, .dpd = 14, .voltage = UINT_MAX },
2629 { .id = TEGRA_IO_PAD_PWR_CTL, .dpd = 15, .voltage = UINT_MAX },
2630 { .id = TEGRA_IO_PAD_SOC_GPIO53, .dpd = 16, .voltage = UINT_MAX },
2631 { .id = TEGRA_IO_PAD_AUDIO, .dpd = 17, .voltage = UINT_MAX },
2632 { .id = TEGRA_IO_PAD_GP_PWM2, .dpd = 18, .voltage = UINT_MAX },
2633 { .id = TEGRA_IO_PAD_GP_PWM3, .dpd = 19, .voltage = UINT_MAX },
2634 { .id = TEGRA_IO_PAD_SOC_GPIO12, .dpd = 20, .voltage = UINT_MAX },
2635 { .id = TEGRA_IO_PAD_SOC_GPIO13, .dpd = 21, .voltage = UINT_MAX },
2636 { .id = TEGRA_IO_PAD_SOC_GPIO10, .dpd = 22, .voltage = UINT_MAX },
2637 { .id = TEGRA_IO_PAD_UART4, .dpd = 23, .voltage = UINT_MAX },
2638 { .id = TEGRA_IO_PAD_UART5, .dpd = 24, .voltage = UINT_MAX },
2639 { .id = TEGRA_IO_PAD_DBG, .dpd = 25, .voltage = UINT_MAX },
2640 { .id = TEGRA_IO_PAD_HDMI_DP3, .dpd = 26, .voltage = UINT_MAX },
2641 { .id = TEGRA_IO_PAD_HDMI_DP2, .dpd = 27, .voltage = UINT_MAX },
2642 { .id = TEGRA_IO_PAD_HDMI_DP0, .dpd = 28, .voltage = UINT_MAX },
2643 { .id = TEGRA_IO_PAD_HDMI_DP1, .dpd = 29, .voltage = UINT_MAX },
2644 { .id = TEGRA_IO_PAD_PEX_CNTRL, .dpd = 32, .voltage = UINT_MAX },
2645 { .id = TEGRA_IO_PAD_PEX_CTL2, .dpd = 33, .voltage = UINT_MAX },
2646 { .id = TEGRA_IO_PAD_PEX_L0_RST_N, .dpd = 34, .voltage = UINT_MAX },
2647 { .id = TEGRA_IO_PAD_PEX_L1_RST_N, .dpd = 35, .voltage = UINT_MAX },
2648 { .id = TEGRA_IO_PAD_SDMMC4, .dpd = 36, .voltage = UINT_MAX },
2649 { .id = TEGRA_IO_PAD_PEX_L5_RST_N, .dpd = 37, .voltage = UINT_MAX },
2650 { .id = TEGRA_IO_PAD_CSIC, .dpd = 43, .voltage = UINT_MAX },
2651 { .id = TEGRA_IO_PAD_CSID, .dpd = 44, .voltage = UINT_MAX },
2652 { .id = TEGRA_IO_PAD_CSIE, .dpd = 45, .voltage = UINT_MAX },
2653 { .id = TEGRA_IO_PAD_CSIF, .dpd = 46, .voltage = UINT_MAX },
2654 { .id = TEGRA_IO_PAD_SPI, .dpd = 47, .voltage = UINT_MAX },
2655 { .id = TEGRA_IO_PAD_UFS, .dpd = 49, .voltage = UINT_MAX },
2656 { .id = TEGRA_IO_PAD_CSIG, .dpd = 50, .voltage = UINT_MAX },
2657 { .id = TEGRA_IO_PAD_CSIH, .dpd = 51, .voltage = UINT_MAX },
2658 { .id = TEGRA_IO_PAD_EDP, .dpd = 53, .voltage = UINT_MAX },
2659 { .id = TEGRA_IO_PAD_SDMMC1_HV, .dpd = 55, .voltage = UINT_MAX },
2660 { .id = TEGRA_IO_PAD_SDMMC3_HV, .dpd = 56, .voltage = UINT_MAX },
2661 { .id = TEGRA_IO_PAD_CONN, .dpd = 60, .voltage = UINT_MAX },
2662 { .id = TEGRA_IO_PAD_AUDIO_HV, .dpd = 61, .voltage = UINT_MAX },
2663};
2664
Thierry Redinge3e403c2018-09-19 18:42:37 +02002665static const struct tegra_wake_event tegra194_wake_events[] = {
2666 TEGRA_WAKE_GPIO("power", 29, 1, TEGRA194_AON_GPIO(EE, 4)),
2667 TEGRA_WAKE_IRQ("rtc", 73, 10),
2668};
2669
Thierry Redingeac9c482018-01-25 14:43:45 +02002670static const struct tegra_pmc_soc tegra194_pmc_soc = {
2671 .num_powergates = 0,
2672 .powergates = NULL,
2673 .num_cpu_powergates = 0,
2674 .cpu_powergates = NULL,
2675 .has_tsense_reset = false,
2676 .has_gpu_clamps = false,
Thierry Redingfa3bc042019-01-25 11:22:54 +01002677 .needs_mbist_war = false,
2678 .has_impl_33v_pwr = false,
Mikko Perttunene247dea2019-01-25 11:22:55 +01002679 .maybe_tz_only = false,
Thierry Redingeac9c482018-01-25 14:43:45 +02002680 .num_io_pads = ARRAY_SIZE(tegra194_io_pads),
2681 .io_pads = tegra194_io_pads,
2682 .regs = &tegra186_pmc_regs,
2683 .init = NULL,
2684 .setup_irq_polarity = tegra186_pmc_setup_irq_polarity,
Thierry Redinge3e403c2018-09-19 18:42:37 +02002685 .num_wake_events = ARRAY_SIZE(tegra194_wake_events),
2686 .wake_events = tegra194_wake_events,
Thierry Reding72323982014-07-11 13:19:06 +02002687};
2688
2689static const struct of_device_id tegra_pmc_match[] = {
Thierry Redingeac9c482018-01-25 14:43:45 +02002690 { .compatible = "nvidia,tegra194-pmc", .data = &tegra194_pmc_soc },
Thierry Redingc641ec62017-08-30 12:42:34 +02002691 { .compatible = "nvidia,tegra186-pmc", .data = &tegra186_pmc_soc },
Thierry Redingc2fe4692015-03-23 11:31:29 +01002692 { .compatible = "nvidia,tegra210-pmc", .data = &tegra210_pmc_soc },
Thierry Reding7d71e9032015-04-29 12:42:28 +02002693 { .compatible = "nvidia,tegra132-pmc", .data = &tegra124_pmc_soc },
Thierry Reding72323982014-07-11 13:19:06 +02002694 { .compatible = "nvidia,tegra124-pmc", .data = &tegra124_pmc_soc },
2695 { .compatible = "nvidia,tegra114-pmc", .data = &tegra114_pmc_soc },
2696 { .compatible = "nvidia,tegra30-pmc", .data = &tegra30_pmc_soc },
2697 { .compatible = "nvidia,tegra20-pmc", .data = &tegra20_pmc_soc },
2698 { }
2699};
2700
2701static struct platform_driver tegra_pmc_driver = {
2702 .driver = {
2703 .name = "tegra-pmc",
2704 .suppress_bind_attrs = true,
2705 .of_match_table = tegra_pmc_match,
Paul Walmsley2b20b612014-12-09 22:36:50 +00002706#if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM)
Thierry Reding72323982014-07-11 13:19:06 +02002707 .pm = &tegra_pmc_pm_ops,
Paul Walmsley2b20b612014-12-09 22:36:50 +00002708#endif
Thierry Reding72323982014-07-11 13:19:06 +02002709 },
2710 .probe = tegra_pmc_probe,
2711};
Paul Gortmaker7d4d9ed2015-05-01 20:10:57 -04002712builtin_platform_driver(tegra_pmc_driver);
Thierry Reding72323982014-07-11 13:19:06 +02002713
Mikko Perttunene247dea2019-01-25 11:22:55 +01002714static bool __init tegra_pmc_detect_tz_only(struct tegra_pmc *pmc)
2715{
2716 u32 value, saved;
2717
2718 saved = readl(pmc->base + pmc->soc->regs->scratch0);
2719 value = saved ^ 0xffffffff;
2720
2721 if (value == 0xffffffff)
2722 value = 0xdeadbeef;
2723
2724 /* write pattern and read it back */
2725 writel(value, pmc->base + pmc->soc->regs->scratch0);
2726 value = readl(pmc->base + pmc->soc->regs->scratch0);
2727
2728 /* if we read all-zeroes, access is restricted to TZ only */
2729 if (value == 0) {
2730 pr_info("access to PMC is restricted to TZ\n");
2731 return true;
2732 }
2733
2734 /* restore original value */
2735 writel(saved, pmc->base + pmc->soc->regs->scratch0);
2736
2737 return false;
2738}
2739
Thierry Reding72323982014-07-11 13:19:06 +02002740/*
2741 * Early initialization to allow access to registers in the very early boot
2742 * process.
2743 */
2744static int __init tegra_pmc_early_init(void)
2745{
2746 const struct of_device_id *match;
2747 struct device_node *np;
2748 struct resource regs;
2749 bool invert;
Thierry Reding72323982014-07-11 13:19:06 +02002750
Jon Hunter61fd2842016-06-28 11:38:26 +01002751 mutex_init(&pmc->powergates_lock);
2752
Thierry Reding72323982014-07-11 13:19:06 +02002753 np = of_find_matching_node_and_match(NULL, tegra_pmc_match, &match);
2754 if (!np) {
Thierry Reding7d71e9032015-04-29 12:42:28 +02002755 /*
2756 * Fall back to legacy initialization for 32-bit ARM only. All
2757 * 64-bit ARM device tree files for Tegra are required to have
2758 * a PMC node.
2759 *
2760 * This is for backwards-compatibility with old device trees
2761 * that didn't contain a PMC node. Note that in this case the
2762 * SoC data can't be matched and therefore powergating is
2763 * disabled.
2764 */
2765 if (IS_ENABLED(CONFIG_ARM) && soc_is_tegra()) {
2766 pr_warn("DT node not found, powergating disabled\n");
Thierry Reding72323982014-07-11 13:19:06 +02002767
Thierry Reding7d71e9032015-04-29 12:42:28 +02002768 regs.start = 0x7000e400;
2769 regs.end = 0x7000e7ff;
2770 regs.flags = IORESOURCE_MEM;
Thierry Reding72323982014-07-11 13:19:06 +02002771
Thierry Reding7d71e9032015-04-29 12:42:28 +02002772 pr_warn("Using memory region %pR\n", &regs);
2773 } else {
2774 /*
2775 * At this point we're not running on Tegra, so play
2776 * nice with multi-platform kernels.
2777 */
2778 return 0;
2779 }
Thierry Reding72323982014-07-11 13:19:06 +02002780 } else {
Thierry Reding7d71e9032015-04-29 12:42:28 +02002781 /*
2782 * Extract information from the device tree if we've found a
2783 * matching node.
2784 */
2785 if (of_address_to_resource(np, 0, &regs) < 0) {
2786 pr_err("failed to get PMC registers\n");
Jon Hunterb69a6252016-06-28 11:38:27 +01002787 of_node_put(np);
Thierry Reding7d71e9032015-04-29 12:42:28 +02002788 return -ENXIO;
2789 }
Thierry Reding72323982014-07-11 13:19:06 +02002790 }
2791
2792 pmc->base = ioremap_nocache(regs.start, resource_size(&regs));
2793 if (!pmc->base) {
2794 pr_err("failed to map PMC registers\n");
Jon Hunterb69a6252016-06-28 11:38:27 +01002795 of_node_put(np);
Thierry Reding72323982014-07-11 13:19:06 +02002796 return -ENXIO;
2797 }
2798
Jon Hunter11131892016-06-28 11:38:24 +01002799 if (np) {
Jon Hunter718a2422016-06-28 11:38:25 +01002800 pmc->soc = match->data;
2801
Mikko Perttunene247dea2019-01-25 11:22:55 +01002802 if (pmc->soc->maybe_tz_only)
2803 pmc->tz_only = tegra_pmc_detect_tz_only(pmc);
2804
Jon Huntere2d17962016-06-30 11:56:25 +01002805 tegra_powergate_init(pmc, np);
Thierry Reding72323982014-07-11 13:19:06 +02002806
Jon Hunter11131892016-06-28 11:38:24 +01002807 /*
2808 * Invert the interrupt polarity if a PMC device tree node
2809 * exists and contains the nvidia,invert-interrupt property.
2810 */
2811 invert = of_property_read_bool(np, "nvidia,invert-interrupt");
Thierry Reding72323982014-07-11 13:19:06 +02002812
Thierry Reding5be22552017-08-30 12:32:58 +02002813 pmc->soc->setup_irq_polarity(pmc, np, invert);
Jon Hunterb69a6252016-06-28 11:38:27 +01002814
2815 of_node_put(np);
Jon Hunter11131892016-06-28 11:38:24 +01002816 }
Thierry Reding72323982014-07-11 13:19:06 +02002817
2818 return 0;
2819}
2820early_initcall(tegra_pmc_early_init);