xref: /linux/drivers/soc/tegra/pmc.c (revision a9fc2304972b1db28b88af8203dffef23e1e92ba)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * drivers/soc/tegra/pmc.c
4  *
5  * Copyright (c) 2010 Google, Inc
6  * Copyright (c) 2018-2024, NVIDIA CORPORATION. All rights reserved.
7  *
8  * Author:
9  *	Colin Cross <ccross@google.com>
10  */
11 
12 #define pr_fmt(fmt) "tegra-pmc: " fmt
13 
14 #include <linux/arm-smccc.h>
15 #include <linux/clk.h>
16 #include <linux/clk-provider.h>
17 #include <linux/clkdev.h>
18 #include <linux/clk/clk-conf.h>
19 #include <linux/clk/tegra.h>
20 #include <linux/debugfs.h>
21 #include <linux/delay.h>
22 #include <linux/device.h>
23 #include <linux/err.h>
24 #include <linux/export.h>
25 #include <linux/init.h>
26 #include <linux/interrupt.h>
27 #include <linux/io.h>
28 #include <linux/iopoll.h>
29 #include <linux/irqdomain.h>
30 #include <linux/irq.h>
31 #include <linux/kernel.h>
32 #include <linux/of_address.h>
33 #include <linux/of_clk.h>
34 #include <linux/of.h>
35 #include <linux/of_irq.h>
36 #include <linux/of_platform.h>
37 #include <linux/pinctrl/pinconf-generic.h>
38 #include <linux/pinctrl/pinconf.h>
39 #include <linux/pinctrl/pinctrl.h>
40 #include <linux/platform_device.h>
41 #include <linux/pm_domain.h>
42 #include <linux/pm_opp.h>
43 #include <linux/power_supply.h>
44 #include <linux/reboot.h>
45 #include <linux/regmap.h>
46 #include <linux/reset.h>
47 #include <linux/seq_file.h>
48 #include <linux/slab.h>
49 #include <linux/spinlock.h>
50 #include <linux/string_choices.h>
51 #include <linux/syscore_ops.h>
52 
53 #include <soc/tegra/common.h>
54 #include <soc/tegra/fuse.h>
55 #include <soc/tegra/pmc.h>
56 
57 #include <dt-bindings/interrupt-controller/arm-gic.h>
58 #include <dt-bindings/pinctrl/pinctrl-tegra-io-pad.h>
59 #include <dt-bindings/gpio/tegra186-gpio.h>
60 #include <dt-bindings/gpio/tegra194-gpio.h>
61 #include <dt-bindings/gpio/tegra234-gpio.h>
62 #include <dt-bindings/soc/tegra-pmc.h>
63 
64 #define PMC_CNTRL			0x0
65 #define  PMC_CNTRL_INTR_POLARITY	BIT(17) /* inverts INTR polarity */
66 #define  PMC_CNTRL_CPU_PWRREQ_OE	BIT(16) /* CPU pwr req enable */
67 #define  PMC_CNTRL_CPU_PWRREQ_POLARITY	BIT(15) /* CPU pwr req polarity */
68 #define  PMC_CNTRL_SIDE_EFFECT_LP0	BIT(14) /* LP0 when CPU pwr gated */
69 #define  PMC_CNTRL_SYSCLK_OE		BIT(11) /* system clock enable */
70 #define  PMC_CNTRL_SYSCLK_POLARITY	BIT(10) /* sys clk polarity */
71 #define  PMC_CNTRL_PWRREQ_POLARITY	BIT(8)
72 #define  PMC_CNTRL_BLINK_EN		7
73 #define  PMC_CNTRL_MAIN_RST		BIT(4)
74 
75 #define PMC_WAKE_MASK			0x0c
76 #define PMC_WAKE_LEVEL			0x10
77 #define PMC_WAKE_STATUS			0x14
78 #define PMC_SW_WAKE_STATUS		0x18
79 #define PMC_DPD_PADS_ORIDE		0x1c
80 #define  PMC_DPD_PADS_ORIDE_BLINK	20
81 
82 #define DPD_SAMPLE			0x020
83 #define  DPD_SAMPLE_ENABLE		BIT(0)
84 #define  DPD_SAMPLE_DISABLE		(0 << 0)
85 
86 #define PWRGATE_TOGGLE			0x30
87 #define  PWRGATE_TOGGLE_START		BIT(8)
88 
89 #define REMOVE_CLAMPING			0x34
90 
91 #define PWRGATE_STATUS			0x38
92 
93 #define PMC_BLINK_TIMER			0x40
94 #define PMC_IMPL_E_33V_PWR		0x40
95 
96 #define PMC_PWR_DET			0x48
97 
98 #define PMC_SCRATCH0_MODE_RECOVERY	BIT(31)
99 #define PMC_SCRATCH0_MODE_BOOTLOADER	BIT(30)
100 #define PMC_SCRATCH0_MODE_RCM		BIT(1)
101 #define PMC_SCRATCH0_MODE_MASK		(PMC_SCRATCH0_MODE_RECOVERY | \
102 					 PMC_SCRATCH0_MODE_BOOTLOADER | \
103 					 PMC_SCRATCH0_MODE_RCM)
104 
105 #define PMC_CPUPWRGOOD_TIMER		0xc8
106 #define PMC_CPUPWROFF_TIMER		0xcc
107 #define PMC_COREPWRGOOD_TIMER		0x3c
108 #define PMC_COREPWROFF_TIMER		0xe0
109 
110 #define PMC_PWR_DET_VALUE		0xe4
111 
112 #define PMC_USB_DEBOUNCE_DEL		0xec
113 #define PMC_USB_AO			0xf0
114 
115 #define PMC_SCRATCH37			0x130
116 #define PMC_SCRATCH41			0x140
117 
118 #define PMC_WAKE2_MASK			0x160
119 #define PMC_WAKE2_LEVEL			0x164
120 #define PMC_WAKE2_STATUS		0x168
121 #define PMC_SW_WAKE2_STATUS		0x16c
122 
123 #define PMC_CLK_OUT_CNTRL		0x1a8
124 #define  PMC_CLK_OUT_MUX_MASK		GENMASK(1, 0)
125 #define PMC_SENSOR_CTRL			0x1b0
126 #define  PMC_SENSOR_CTRL_SCRATCH_WRITE	BIT(2)
127 #define  PMC_SENSOR_CTRL_ENABLE_RST	BIT(1)
128 
129 #define  PMC_RST_STATUS_POR		0
130 #define  PMC_RST_STATUS_WATCHDOG	1
131 #define  PMC_RST_STATUS_SENSOR		2
132 #define  PMC_RST_STATUS_SW_MAIN		3
133 #define  PMC_RST_STATUS_LP0		4
134 #define  PMC_RST_STATUS_AOTAG		5
135 
136 #define IO_DPD_REQ			0x1b8
137 #define  IO_DPD_REQ_CODE_IDLE		(0U << 30)
138 #define  IO_DPD_REQ_CODE_OFF		(1U << 30)
139 #define  IO_DPD_REQ_CODE_ON		(2U << 30)
140 #define  IO_DPD_REQ_CODE_MASK		(3U << 30)
141 
142 #define IO_DPD_STATUS			0x1bc
143 #define IO_DPD2_REQ			0x1c0
144 #define IO_DPD2_STATUS			0x1c4
145 #define SEL_DPD_TIM			0x1c8
146 
147 #define PMC_UTMIP_UHSIC_TRIGGERS	0x1ec
148 #define PMC_UTMIP_UHSIC_SAVED_STATE	0x1f0
149 
150 #define PMC_UTMIP_TERM_PAD_CFG		0x1f8
151 #define PMC_UTMIP_UHSIC_SLEEP_CFG	0x1fc
152 #define PMC_UTMIP_UHSIC_FAKE		0x218
153 
154 #define PMC_SCRATCH54			0x258
155 #define  PMC_SCRATCH54_DATA_SHIFT	8
156 #define  PMC_SCRATCH54_ADDR_SHIFT	0
157 
158 #define PMC_SCRATCH55			0x25c
159 #define  PMC_SCRATCH55_RESET_TEGRA	BIT(31)
160 #define  PMC_SCRATCH55_CNTRL_ID_SHIFT	27
161 #define  PMC_SCRATCH55_PINMUX_SHIFT	24
162 #define  PMC_SCRATCH55_16BITOP		BIT(15)
163 #define  PMC_SCRATCH55_CHECKSUM_SHIFT	16
164 #define  PMC_SCRATCH55_I2CSLV1_SHIFT	0
165 
166 #define  PMC_UTMIP_UHSIC_LINE_WAKEUP	0x26c
167 
168 #define PMC_UTMIP_BIAS_MASTER_CNTRL	0x270
169 #define PMC_UTMIP_MASTER_CONFIG		0x274
170 #define PMC_UTMIP_UHSIC2_TRIGGERS	0x27c
171 #define PMC_UTMIP_MASTER2_CONFIG	0x29c
172 
173 #define GPU_RG_CNTRL			0x2d4
174 
175 #define PMC_UTMIP_PAD_CFG0		0x4c0
176 #define PMC_UTMIP_UHSIC_SLEEP_CFG1	0x4d0
177 #define PMC_UTMIP_SLEEPWALK_P3		0x4e0
178 /* Tegra186 and later */
179 #define WAKE_AOWAKE_CNTRL(x) (0x000 + ((x) << 2))
180 #define WAKE_AOWAKE_CNTRL_LEVEL (1 << 3)
181 #define WAKE_AOWAKE_CNTRL_SR_CAPTURE_EN (1 << 1)
182 #define WAKE_AOWAKE_MASK_W(x) (0x180 + ((x) << 2))
183 #define WAKE_AOWAKE_MASK_R(x) (0x300 + ((x) << 2))
184 #define WAKE_AOWAKE_STATUS_W(x) (0x30c + ((x) << 2))
185 #define WAKE_AOWAKE_STATUS_R(x) (0x48c + ((x) << 2))
186 #define WAKE_AOWAKE_TIER0_ROUTING(x) (0x4b4 + ((x) << 2))
187 #define WAKE_AOWAKE_TIER1_ROUTING(x) (0x4c0 + ((x) << 2))
188 #define WAKE_AOWAKE_TIER2_ROUTING(x) (0x4cc + ((x) << 2))
189 #define WAKE_AOWAKE_SW_STATUS_W_0	0x49c
190 #define WAKE_AOWAKE_SW_STATUS(x)	(0x4a0 + ((x) << 2))
191 #define WAKE_LATCH_SW			0x498
192 
193 #define WAKE_AOWAKE_CTRL 0x4f4
194 #define  WAKE_AOWAKE_CTRL_INTR_POLARITY BIT(0)
195 
196 #define SW_WAKE_ID		83 /* wake83 */
197 
198 /* for secure PMC */
199 #define TEGRA_SMC_PMC		0xc2fffe00
200 #define  TEGRA_SMC_PMC_READ	0xaa
201 #define  TEGRA_SMC_PMC_WRITE	0xbb
202 
203 struct pmc_clk {
204 	struct clk_hw	hw;
205 	unsigned long	offs;
206 	u32		mux_shift;
207 	u32		force_en_shift;
208 };
209 
210 #define to_pmc_clk(_hw) container_of(_hw, struct pmc_clk, hw)
211 
212 struct pmc_clk_gate {
213 	struct clk_hw	hw;
214 	unsigned long	offs;
215 	u32		shift;
216 };
217 
218 #define to_pmc_clk_gate(_hw) container_of(_hw, struct pmc_clk_gate, hw)
219 
220 struct pmc_clk_init_data {
221 	char *name;
222 	const char *const *parents;
223 	int num_parents;
224 	int clk_id;
225 	u8 mux_shift;
226 	u8 force_en_shift;
227 };
228 
229 static const char * const clk_out1_parents[] = { "osc", "osc_div2",
230 	"osc_div4", "extern1",
231 };
232 
233 static const char * const clk_out2_parents[] = { "osc", "osc_div2",
234 	"osc_div4", "extern2",
235 };
236 
237 static const char * const clk_out3_parents[] = { "osc", "osc_div2",
238 	"osc_div4", "extern3",
239 };
240 
241 static const struct pmc_clk_init_data tegra_pmc_clks_data[] = {
242 	{
243 		.name = "pmc_clk_out_1",
244 		.parents = clk_out1_parents,
245 		.num_parents = ARRAY_SIZE(clk_out1_parents),
246 		.clk_id = TEGRA_PMC_CLK_OUT_1,
247 		.mux_shift = 6,
248 		.force_en_shift = 2,
249 	},
250 	{
251 		.name = "pmc_clk_out_2",
252 		.parents = clk_out2_parents,
253 		.num_parents = ARRAY_SIZE(clk_out2_parents),
254 		.clk_id = TEGRA_PMC_CLK_OUT_2,
255 		.mux_shift = 14,
256 		.force_en_shift = 10,
257 	},
258 	{
259 		.name = "pmc_clk_out_3",
260 		.parents = clk_out3_parents,
261 		.num_parents = ARRAY_SIZE(clk_out3_parents),
262 		.clk_id = TEGRA_PMC_CLK_OUT_3,
263 		.mux_shift = 22,
264 		.force_en_shift = 18,
265 	},
266 };
267 
268 struct tegra_powergate {
269 	struct generic_pm_domain genpd;
270 	struct tegra_pmc *pmc;
271 	unsigned int id;
272 	struct clk **clks;
273 	unsigned int num_clks;
274 	unsigned long *clk_rates;
275 	struct reset_control *reset;
276 };
277 
278 struct tegra_io_pad_soc {
279 	enum tegra_io_pad id;
280 	unsigned int dpd;
281 	unsigned int request;
282 	unsigned int status;
283 	unsigned int voltage;
284 	const char *name;
285 };
286 
287 struct tegra_pmc_regs {
288 	unsigned int scratch0;
289 	unsigned int rst_status;
290 	unsigned int rst_source_shift;
291 	unsigned int rst_source_mask;
292 	unsigned int rst_level_shift;
293 	unsigned int rst_level_mask;
294 };
295 
296 struct tegra_wake_event {
297 	const char *name;
298 	unsigned int id;
299 	unsigned int irq;
300 	struct {
301 		unsigned int instance;
302 		unsigned int pin;
303 	} gpio;
304 };
305 
306 #define TEGRA_WAKE_SIMPLE(_name, _id)			\
307 	{						\
308 		.name = _name,				\
309 		.id = _id,				\
310 		.irq = 0,				\
311 		.gpio = {				\
312 			.instance = UINT_MAX,		\
313 			.pin = UINT_MAX,		\
314 		},					\
315 	}
316 
317 #define TEGRA_WAKE_IRQ(_name, _id, _irq)		\
318 	{						\
319 		.name = _name,				\
320 		.id = _id,				\
321 		.irq = _irq,				\
322 		.gpio = {				\
323 			.instance = UINT_MAX,		\
324 			.pin = UINT_MAX,		\
325 		},					\
326 	}
327 
328 #define TEGRA_WAKE_GPIO(_name, _id, _instance, _pin)	\
329 	{						\
330 		.name = _name,				\
331 		.id = _id,				\
332 		.irq = 0,				\
333 		.gpio = {				\
334 			.instance = _instance,		\
335 			.pin = _pin,			\
336 		},					\
337 	}
338 
339 struct tegra_pmc_soc {
340 	unsigned int num_powergates;
341 	const char *const *powergates;
342 	unsigned int num_cpu_powergates;
343 	const u8 *cpu_powergates;
344 
345 	bool has_tsense_reset;
346 	bool has_gpu_clamps;
347 	bool needs_mbist_war;
348 	bool has_impl_33v_pwr;
349 	bool maybe_tz_only;
350 
351 	const struct tegra_io_pad_soc *io_pads;
352 	unsigned int num_io_pads;
353 
354 	const struct pinctrl_pin_desc *pin_descs;
355 	unsigned int num_pin_descs;
356 
357 	const struct tegra_pmc_regs *regs;
358 	void (*init)(struct tegra_pmc *pmc);
359 	void (*setup_irq_polarity)(struct tegra_pmc *pmc,
360 				   struct device_node *np,
361 				   bool invert);
362 	void (*set_wake_filters)(struct tegra_pmc *pmc);
363 	int (*irq_set_wake)(struct irq_data *data, unsigned int on);
364 	int (*irq_set_type)(struct irq_data *data, unsigned int type);
365 	int (*powergate_set)(struct tegra_pmc *pmc, unsigned int id,
366 			     bool new_state);
367 
368 	const char * const *reset_sources;
369 	unsigned int num_reset_sources;
370 	const char * const *reset_levels;
371 	unsigned int num_reset_levels;
372 
373 	/*
374 	 * These describe events that can wake the system from sleep (i.e.
375 	 * LP0 or SC7). Wakeup from other sleep states (such as LP1 or LP2)
376 	 * are dealt with in the LIC.
377 	 */
378 	const struct tegra_wake_event *wake_events;
379 	unsigned int num_wake_events;
380 	unsigned int max_wake_events;
381 	unsigned int max_wake_vectors;
382 
383 	const struct pmc_clk_init_data *pmc_clks_data;
384 	unsigned int num_pmc_clks;
385 	bool has_blink_output;
386 	bool has_usb_sleepwalk;
387 	bool supports_core_domain;
388 	bool has_single_mmio_aperture;
389 };
390 
391 /**
392  * struct tegra_pmc - NVIDIA Tegra PMC
393  * @dev: pointer to PMC device structure
394  * @base: pointer to I/O remapped register region
395  * @wake: pointer to I/O remapped region for WAKE registers
396  * @aotag: pointer to I/O remapped region for AOTAG registers
397  * @scratch: pointer to I/O remapped region for scratch registers
398  * @clk: pointer to pclk clock
399  * @soc: pointer to SoC data structure
400  * @tz_only: flag specifying if the PMC can only be accessed via TrustZone
401  * @rate: currently configured rate of pclk
402  * @suspend_mode: lowest suspend mode available
403  * @cpu_good_time: CPU power good time (in microseconds)
404  * @cpu_off_time: CPU power off time (in microsecends)
405  * @core_osc_time: core power good OSC time (in microseconds)
406  * @core_pmu_time: core power good PMU time (in microseconds)
407  * @core_off_time: core power off time (in microseconds)
408  * @corereq_high: core power request is active-high
409  * @sysclkreq_high: system clock request is active-high
410  * @combined_req: combined power request for CPU & core
411  * @cpu_pwr_good_en: CPU power good signal is enabled
412  * @lp0_vec_phys: physical base address of the LP0 warm boot code
413  * @lp0_vec_size: size of the LP0 warm boot code
414  * @powergates_available: Bitmap of available power gates
415  * @powergates_lock: mutex for power gate register access
416  * @pctl_dev: pin controller exposed by the PMC
417  * @domain: IRQ domain provided by the PMC
418  * @irq: chip implementation for the IRQ domain
419  * @clk_nb: pclk clock changes handler
420  * @core_domain_state_synced: flag marking the core domain's state as synced
421  * @core_domain_registered: flag marking the core domain as registered
422  * @wake_type_level_map: Bitmap indicating level type for non-dual edge wakes
423  * @wake_type_dual_edge_map: Bitmap indicating if a wake is dual-edge or not
424  * @wake_sw_status_map: Bitmap to hold raw status of wakes without mask
425  * @wake_cntrl_level_map: Bitmap to hold wake levels to be programmed in
426  *     cntrl register associated with each wake during system suspend.
427  */
428 struct tegra_pmc {
429 	struct device *dev;
430 	void __iomem *base;
431 	void __iomem *wake;
432 	void __iomem *aotag;
433 	void __iomem *scratch;
434 	struct clk *clk;
435 
436 	const struct tegra_pmc_soc *soc;
437 	bool tz_only;
438 
439 	unsigned long rate;
440 
441 	enum tegra_suspend_mode suspend_mode;
442 	u32 cpu_good_time;
443 	u32 cpu_off_time;
444 	u32 core_osc_time;
445 	u32 core_pmu_time;
446 	u32 core_off_time;
447 	bool corereq_high;
448 	bool sysclkreq_high;
449 	bool combined_req;
450 	bool cpu_pwr_good_en;
451 	u32 lp0_vec_phys;
452 	u32 lp0_vec_size;
453 	DECLARE_BITMAP(powergates_available, TEGRA_POWERGATE_MAX);
454 
455 	struct mutex powergates_lock;
456 
457 	struct pinctrl_dev *pctl_dev;
458 
459 	struct irq_domain *domain;
460 	struct irq_chip irq;
461 
462 	struct notifier_block clk_nb;
463 
464 	bool core_domain_state_synced;
465 	bool core_domain_registered;
466 
467 	unsigned long *wake_type_level_map;
468 	unsigned long *wake_type_dual_edge_map;
469 	unsigned long *wake_sw_status_map;
470 	unsigned long *wake_cntrl_level_map;
471 	struct syscore_ops syscore;
472 };
473 
474 static struct tegra_pmc *pmc = &(struct tegra_pmc) {
475 	.base = NULL,
476 	.suspend_mode = TEGRA_SUSPEND_NOT_READY,
477 };
478 
479 static inline struct tegra_powergate *
to_powergate(struct generic_pm_domain * domain)480 to_powergate(struct generic_pm_domain *domain)
481 {
482 	return container_of(domain, struct tegra_powergate, genpd);
483 }
484 
tegra_pmc_readl(struct tegra_pmc * pmc,unsigned long offset)485 static u32 tegra_pmc_readl(struct tegra_pmc *pmc, unsigned long offset)
486 {
487 	struct arm_smccc_res res;
488 
489 	if (pmc->tz_only) {
490 		arm_smccc_smc(TEGRA_SMC_PMC, TEGRA_SMC_PMC_READ, offset, 0, 0,
491 			      0, 0, 0, &res);
492 		if (res.a0) {
493 			if (pmc->dev)
494 				dev_warn(pmc->dev, "%s(): SMC failed: %lu\n",
495 					 __func__, res.a0);
496 			else
497 				pr_warn("%s(): SMC failed: %lu\n", __func__,
498 					res.a0);
499 		}
500 
501 		return res.a1;
502 	}
503 
504 	return readl(pmc->base + offset);
505 }
506 
tegra_pmc_writel(struct tegra_pmc * pmc,u32 value,unsigned long offset)507 static void tegra_pmc_writel(struct tegra_pmc *pmc, u32 value,
508 			     unsigned long offset)
509 {
510 	struct arm_smccc_res res;
511 
512 	if (pmc->tz_only) {
513 		arm_smccc_smc(TEGRA_SMC_PMC, TEGRA_SMC_PMC_WRITE, offset,
514 			      value, 0, 0, 0, 0, &res);
515 		if (res.a0) {
516 			if (pmc->dev)
517 				dev_warn(pmc->dev, "%s(): SMC failed: %lu\n",
518 					 __func__, res.a0);
519 			else
520 				pr_warn("%s(): SMC failed: %lu\n", __func__,
521 					res.a0);
522 		}
523 	} else {
524 		writel(value, pmc->base + offset);
525 	}
526 }
527 
tegra_pmc_scratch_readl(struct tegra_pmc * pmc,unsigned long offset)528 static u32 tegra_pmc_scratch_readl(struct tegra_pmc *pmc, unsigned long offset)
529 {
530 	if (pmc->tz_only)
531 		return tegra_pmc_readl(pmc, offset);
532 
533 	return readl(pmc->scratch + offset);
534 }
535 
tegra_pmc_scratch_writel(struct tegra_pmc * pmc,u32 value,unsigned long offset)536 static void tegra_pmc_scratch_writel(struct tegra_pmc *pmc, u32 value,
537 				     unsigned long offset)
538 {
539 	if (pmc->tz_only)
540 		tegra_pmc_writel(pmc, value, offset);
541 	else
542 		writel(value, pmc->scratch + offset);
543 }
544 
545 /*
546  * TODO Figure out a way to call this with the struct tegra_pmc * passed in.
547  * This currently doesn't work because readx_poll_timeout() can only operate
548  * on functions that take a single argument.
549  */
tegra_powergate_state(int id)550 static inline bool tegra_powergate_state(int id)
551 {
552 	if (id == TEGRA_POWERGATE_3D && pmc->soc->has_gpu_clamps)
553 		return (tegra_pmc_readl(pmc, GPU_RG_CNTRL) & 0x1) == 0;
554 	else
555 		return (tegra_pmc_readl(pmc, PWRGATE_STATUS) & BIT(id)) != 0;
556 }
557 
tegra_powergate_is_valid(struct tegra_pmc * pmc,int id)558 static inline bool tegra_powergate_is_valid(struct tegra_pmc *pmc, int id)
559 {
560 	return (pmc->soc && pmc->soc->powergates[id]);
561 }
562 
tegra_powergate_is_available(struct tegra_pmc * pmc,int id)563 static inline bool tegra_powergate_is_available(struct tegra_pmc *pmc, int id)
564 {
565 	return test_bit(id, pmc->powergates_available);
566 }
567 
tegra_powergate_lookup(struct tegra_pmc * pmc,const char * name)568 static int tegra_powergate_lookup(struct tegra_pmc *pmc, const char *name)
569 {
570 	unsigned int i;
571 
572 	if (!pmc || !pmc->soc || !name)
573 		return -EINVAL;
574 
575 	for (i = 0; i < pmc->soc->num_powergates; i++) {
576 		if (!tegra_powergate_is_valid(pmc, i))
577 			continue;
578 
579 		if (!strcmp(name, pmc->soc->powergates[i]))
580 			return i;
581 	}
582 
583 	return -ENODEV;
584 }
585 
tegra20_powergate_set(struct tegra_pmc * pmc,unsigned int id,bool new_state)586 static int tegra20_powergate_set(struct tegra_pmc *pmc, unsigned int id,
587 				 bool new_state)
588 {
589 	unsigned int retries = 100;
590 	bool status;
591 	int ret;
592 
593 	/*
594 	 * As per TRM documentation, the toggle command will be dropped by PMC
595 	 * if there is contention with a HW-initiated toggling (i.e. CPU core
596 	 * power-gated), the command should be retried in that case.
597 	 */
598 	do {
599 		tegra_pmc_writel(pmc, PWRGATE_TOGGLE_START | id, PWRGATE_TOGGLE);
600 
601 		/* wait for PMC to execute the command */
602 		ret = readx_poll_timeout(tegra_powergate_state, id, status,
603 					 status == new_state, 1, 10);
604 	} while (ret == -ETIMEDOUT && retries--);
605 
606 	return ret;
607 }
608 
tegra_powergate_toggle_ready(struct tegra_pmc * pmc)609 static inline bool tegra_powergate_toggle_ready(struct tegra_pmc *pmc)
610 {
611 	return !(tegra_pmc_readl(pmc, PWRGATE_TOGGLE) & PWRGATE_TOGGLE_START);
612 }
613 
tegra114_powergate_set(struct tegra_pmc * pmc,unsigned int id,bool new_state)614 static int tegra114_powergate_set(struct tegra_pmc *pmc, unsigned int id,
615 				  bool new_state)
616 {
617 	bool status;
618 	int err;
619 
620 	/* wait while PMC power gating is contended */
621 	err = readx_poll_timeout(tegra_powergate_toggle_ready, pmc, status,
622 				 status == true, 1, 100);
623 	if (err)
624 		return err;
625 
626 	tegra_pmc_writel(pmc, PWRGATE_TOGGLE_START | id, PWRGATE_TOGGLE);
627 
628 	/* wait for PMC to accept the command */
629 	err = readx_poll_timeout(tegra_powergate_toggle_ready, pmc, status,
630 				 status == true, 1, 100);
631 	if (err)
632 		return err;
633 
634 	/* wait for PMC to execute the command */
635 	err = readx_poll_timeout(tegra_powergate_state, id, status,
636 				 status == new_state, 10, 100000);
637 	if (err)
638 		return err;
639 
640 	return 0;
641 }
642 
643 /**
644  * tegra_powergate_set() - set the state of a partition
645  * @pmc: power management controller
646  * @id: partition ID
647  * @new_state: new state of the partition
648  */
tegra_powergate_set(struct tegra_pmc * pmc,unsigned int id,bool new_state)649 static int tegra_powergate_set(struct tegra_pmc *pmc, unsigned int id,
650 			       bool new_state)
651 {
652 	int err;
653 
654 	if (id == TEGRA_POWERGATE_3D && pmc->soc->has_gpu_clamps)
655 		return -EINVAL;
656 
657 	mutex_lock(&pmc->powergates_lock);
658 
659 	if (tegra_powergate_state(id) == new_state) {
660 		mutex_unlock(&pmc->powergates_lock);
661 		return 0;
662 	}
663 
664 	err = pmc->soc->powergate_set(pmc, id, new_state);
665 
666 	mutex_unlock(&pmc->powergates_lock);
667 
668 	return err;
669 }
670 
__tegra_powergate_remove_clamping(struct tegra_pmc * pmc,unsigned int id)671 static int __tegra_powergate_remove_clamping(struct tegra_pmc *pmc,
672 					     unsigned int id)
673 {
674 	u32 mask;
675 
676 	mutex_lock(&pmc->powergates_lock);
677 
678 	/*
679 	 * On Tegra124 and later, the clamps for the GPU are controlled by a
680 	 * separate register (with different semantics).
681 	 */
682 	if (id == TEGRA_POWERGATE_3D) {
683 		if (pmc->soc->has_gpu_clamps) {
684 			tegra_pmc_writel(pmc, 0, GPU_RG_CNTRL);
685 			goto out;
686 		}
687 	}
688 
689 	/*
690 	 * Tegra 2 has a bug where PCIE and VDE clamping masks are
691 	 * swapped relatively to the partition ids
692 	 */
693 	if (id == TEGRA_POWERGATE_VDEC)
694 		mask = (1 << TEGRA_POWERGATE_PCIE);
695 	else if (id == TEGRA_POWERGATE_PCIE)
696 		mask = (1 << TEGRA_POWERGATE_VDEC);
697 	else
698 		mask = (1 << id);
699 
700 	tegra_pmc_writel(pmc, mask, REMOVE_CLAMPING);
701 
702 out:
703 	mutex_unlock(&pmc->powergates_lock);
704 
705 	return 0;
706 }
707 
tegra_powergate_prepare_clocks(struct tegra_powergate * pg)708 static int tegra_powergate_prepare_clocks(struct tegra_powergate *pg)
709 {
710 	unsigned long safe_rate = 100 * 1000 * 1000;
711 	unsigned int i;
712 	int err;
713 
714 	for (i = 0; i < pg->num_clks; i++) {
715 		pg->clk_rates[i] = clk_get_rate(pg->clks[i]);
716 
717 		if (!pg->clk_rates[i]) {
718 			err = -EINVAL;
719 			goto out;
720 		}
721 
722 		if (pg->clk_rates[i] <= safe_rate)
723 			continue;
724 
725 		/*
726 		 * We don't know whether voltage state is okay for the
727 		 * current clock rate, hence it's better to temporally
728 		 * switch clock to a safe rate which is suitable for
729 		 * all voltages, before enabling the clock.
730 		 */
731 		err = clk_set_rate(pg->clks[i], safe_rate);
732 		if (err)
733 			goto out;
734 	}
735 
736 	return 0;
737 
738 out:
739 	while (i--)
740 		clk_set_rate(pg->clks[i], pg->clk_rates[i]);
741 
742 	return err;
743 }
744 
tegra_powergate_unprepare_clocks(struct tegra_powergate * pg)745 static int tegra_powergate_unprepare_clocks(struct tegra_powergate *pg)
746 {
747 	unsigned int i;
748 	int err;
749 
750 	for (i = 0; i < pg->num_clks; i++) {
751 		err = clk_set_rate(pg->clks[i], pg->clk_rates[i]);
752 		if (err)
753 			return err;
754 	}
755 
756 	return 0;
757 }
758 
tegra_powergate_disable_clocks(struct tegra_powergate * pg)759 static void tegra_powergate_disable_clocks(struct tegra_powergate *pg)
760 {
761 	unsigned int i;
762 
763 	for (i = 0; i < pg->num_clks; i++)
764 		clk_disable_unprepare(pg->clks[i]);
765 }
766 
tegra_powergate_enable_clocks(struct tegra_powergate * pg)767 static int tegra_powergate_enable_clocks(struct tegra_powergate *pg)
768 {
769 	unsigned int i;
770 	int err;
771 
772 	for (i = 0; i < pg->num_clks; i++) {
773 		err = clk_prepare_enable(pg->clks[i]);
774 		if (err)
775 			goto out;
776 	}
777 
778 	return 0;
779 
780 out:
781 	while (i--)
782 		clk_disable_unprepare(pg->clks[i]);
783 
784 	return err;
785 }
786 
tegra_powergate_power_up(struct tegra_powergate * pg,bool disable_clocks)787 static int tegra_powergate_power_up(struct tegra_powergate *pg,
788 				    bool disable_clocks)
789 {
790 	int err;
791 
792 	err = reset_control_assert(pg->reset);
793 	if (err)
794 		return err;
795 
796 	usleep_range(10, 20);
797 
798 	err = tegra_powergate_set(pg->pmc, pg->id, true);
799 	if (err < 0)
800 		return err;
801 
802 	usleep_range(10, 20);
803 
804 	err = tegra_powergate_prepare_clocks(pg);
805 	if (err)
806 		goto powergate_off;
807 
808 	err = tegra_powergate_enable_clocks(pg);
809 	if (err)
810 		goto unprepare_clks;
811 
812 	usleep_range(10, 20);
813 
814 	err = __tegra_powergate_remove_clamping(pg->pmc, pg->id);
815 	if (err)
816 		goto disable_clks;
817 
818 	usleep_range(10, 20);
819 
820 	err = reset_control_deassert(pg->reset);
821 	if (err)
822 		goto disable_clks;
823 
824 	usleep_range(10, 20);
825 
826 	if (pg->pmc->soc->needs_mbist_war)
827 		err = tegra210_clk_handle_mbist_war(pg->id);
828 	if (err)
829 		goto disable_clks;
830 
831 	if (disable_clocks)
832 		tegra_powergate_disable_clocks(pg);
833 
834 	err = tegra_powergate_unprepare_clocks(pg);
835 	if (err)
836 		return err;
837 
838 	return 0;
839 
840 disable_clks:
841 	tegra_powergate_disable_clocks(pg);
842 	usleep_range(10, 20);
843 
844 unprepare_clks:
845 	tegra_powergate_unprepare_clocks(pg);
846 
847 powergate_off:
848 	tegra_powergate_set(pg->pmc, pg->id, false);
849 
850 	return err;
851 }
852 
tegra_powergate_power_down(struct tegra_powergate * pg)853 static int tegra_powergate_power_down(struct tegra_powergate *pg)
854 {
855 	int err;
856 
857 	err = tegra_powergate_prepare_clocks(pg);
858 	if (err)
859 		return err;
860 
861 	err = tegra_powergate_enable_clocks(pg);
862 	if (err)
863 		goto unprepare_clks;
864 
865 	usleep_range(10, 20);
866 
867 	err = reset_control_assert(pg->reset);
868 	if (err)
869 		goto disable_clks;
870 
871 	usleep_range(10, 20);
872 
873 	tegra_powergate_disable_clocks(pg);
874 
875 	usleep_range(10, 20);
876 
877 	err = tegra_powergate_set(pg->pmc, pg->id, false);
878 	if (err)
879 		goto assert_resets;
880 
881 	err = tegra_powergate_unprepare_clocks(pg);
882 	if (err)
883 		return err;
884 
885 	return 0;
886 
887 assert_resets:
888 	tegra_powergate_enable_clocks(pg);
889 	usleep_range(10, 20);
890 	reset_control_deassert(pg->reset);
891 	usleep_range(10, 20);
892 
893 disable_clks:
894 	tegra_powergate_disable_clocks(pg);
895 
896 unprepare_clks:
897 	tegra_powergate_unprepare_clocks(pg);
898 
899 	return err;
900 }
901 
tegra_genpd_power_on(struct generic_pm_domain * domain)902 static int tegra_genpd_power_on(struct generic_pm_domain *domain)
903 {
904 	struct tegra_powergate *pg = to_powergate(domain);
905 	struct device *dev = pg->pmc->dev;
906 	int err;
907 
908 	err = tegra_powergate_power_up(pg, true);
909 	if (err) {
910 		dev_err(dev, "failed to turn on PM domain %s: %d\n",
911 			pg->genpd.name, err);
912 		goto out;
913 	}
914 
915 	reset_control_release(pg->reset);
916 
917 out:
918 	return err;
919 }
920 
tegra_genpd_power_off(struct generic_pm_domain * domain)921 static int tegra_genpd_power_off(struct generic_pm_domain *domain)
922 {
923 	struct tegra_powergate *pg = to_powergate(domain);
924 	struct device *dev = pg->pmc->dev;
925 	int err;
926 
927 	err = reset_control_acquire(pg->reset);
928 	if (err < 0) {
929 		dev_err(dev, "failed to acquire resets for PM domain %s: %d\n",
930 			pg->genpd.name, err);
931 		return err;
932 	}
933 
934 	err = tegra_powergate_power_down(pg);
935 	if (err) {
936 		dev_err(dev, "failed to turn off PM domain %s: %d\n",
937 			pg->genpd.name, err);
938 		reset_control_release(pg->reset);
939 	}
940 
941 	return err;
942 }
943 
944 /**
945  * tegra_powergate_power_on() - power on partition
946  * @id: partition ID
947  */
tegra_powergate_power_on(unsigned int id)948 int tegra_powergate_power_on(unsigned int id)
949 {
950 	if (!tegra_powergate_is_available(pmc, id))
951 		return -EINVAL;
952 
953 	return tegra_powergate_set(pmc, id, true);
954 }
955 EXPORT_SYMBOL(tegra_powergate_power_on);
956 
957 /**
958  * tegra_powergate_power_off() - power off partition
959  * @id: partition ID
960  */
tegra_powergate_power_off(unsigned int id)961 int tegra_powergate_power_off(unsigned int id)
962 {
963 	if (!tegra_powergate_is_available(pmc, id))
964 		return -EINVAL;
965 
966 	return tegra_powergate_set(pmc, id, false);
967 }
968 EXPORT_SYMBOL(tegra_powergate_power_off);
969 
970 /**
971  * tegra_powergate_is_powered() - check if partition is powered
972  * @pmc: power management controller
973  * @id: partition ID
974  */
tegra_powergate_is_powered(struct tegra_pmc * pmc,unsigned int id)975 static int tegra_powergate_is_powered(struct tegra_pmc *pmc, unsigned int id)
976 {
977 	if (!tegra_powergate_is_valid(pmc, id))
978 		return -EINVAL;
979 
980 	return tegra_powergate_state(id);
981 }
982 
983 /**
984  * tegra_powergate_remove_clamping() - remove power clamps for partition
985  * @id: partition ID
986  */
tegra_powergate_remove_clamping(unsigned int id)987 int tegra_powergate_remove_clamping(unsigned int id)
988 {
989 	if (!tegra_powergate_is_available(pmc, id))
990 		return -EINVAL;
991 
992 	return __tegra_powergate_remove_clamping(pmc, id);
993 }
994 EXPORT_SYMBOL(tegra_powergate_remove_clamping);
995 
996 /**
997  * tegra_powergate_sequence_power_up() - power up partition
998  * @id: partition ID
999  * @clk: clock for partition
1000  * @rst: reset for partition
1001  *
1002  * Must be called with clk disabled, and returns with clk enabled.
1003  */
tegra_powergate_sequence_power_up(unsigned int id,struct clk * clk,struct reset_control * rst)1004 int tegra_powergate_sequence_power_up(unsigned int id, struct clk *clk,
1005 				      struct reset_control *rst)
1006 {
1007 	struct tegra_powergate *pg;
1008 	int err;
1009 
1010 	if (!tegra_powergate_is_available(pmc, id))
1011 		return -EINVAL;
1012 
1013 	pg = kzalloc(sizeof(*pg), GFP_KERNEL);
1014 	if (!pg)
1015 		return -ENOMEM;
1016 
1017 	pg->clk_rates = kzalloc(sizeof(*pg->clk_rates), GFP_KERNEL);
1018 	if (!pg->clk_rates) {
1019 		kfree(pg->clks);
1020 		return -ENOMEM;
1021 	}
1022 
1023 	pg->id = id;
1024 	pg->clks = &clk;
1025 	pg->num_clks = 1;
1026 	pg->reset = rst;
1027 	pg->pmc = pmc;
1028 
1029 	err = tegra_powergate_power_up(pg, false);
1030 	if (err)
1031 		dev_err(pmc->dev, "failed to turn on partition %d: %d\n", id,
1032 			err);
1033 
1034 	kfree(pg->clk_rates);
1035 	kfree(pg);
1036 
1037 	return err;
1038 }
1039 EXPORT_SYMBOL(tegra_powergate_sequence_power_up);
1040 
1041 /**
1042  * tegra_get_cpu_powergate_id() - convert from CPU ID to partition ID
1043  * @pmc: power management controller
1044  * @cpuid: CPU partition ID
1045  *
1046  * Returns the partition ID corresponding to the CPU partition ID or a
1047  * negative error code on failure.
1048  */
tegra_get_cpu_powergate_id(struct tegra_pmc * pmc,unsigned int cpuid)1049 static int tegra_get_cpu_powergate_id(struct tegra_pmc *pmc,
1050 				      unsigned int cpuid)
1051 {
1052 	if (pmc->soc && cpuid < pmc->soc->num_cpu_powergates)
1053 		return pmc->soc->cpu_powergates[cpuid];
1054 
1055 	return -EINVAL;
1056 }
1057 
1058 /**
1059  * tegra_pmc_cpu_is_powered() - check if CPU partition is powered
1060  * @cpuid: CPU partition ID
1061  */
tegra_pmc_cpu_is_powered(unsigned int cpuid)1062 bool tegra_pmc_cpu_is_powered(unsigned int cpuid)
1063 {
1064 	int id;
1065 
1066 	id = tegra_get_cpu_powergate_id(pmc, cpuid);
1067 	if (id < 0)
1068 		return false;
1069 
1070 	return tegra_powergate_is_powered(pmc, id);
1071 }
1072 
1073 /**
1074  * tegra_pmc_cpu_power_on() - power on CPU partition
1075  * @cpuid: CPU partition ID
1076  */
tegra_pmc_cpu_power_on(unsigned int cpuid)1077 int tegra_pmc_cpu_power_on(unsigned int cpuid)
1078 {
1079 	int id;
1080 
1081 	id = tegra_get_cpu_powergate_id(pmc, cpuid);
1082 	if (id < 0)
1083 		return id;
1084 
1085 	return tegra_powergate_set(pmc, id, true);
1086 }
1087 
1088 /**
1089  * tegra_pmc_cpu_remove_clamping() - remove power clamps for CPU partition
1090  * @cpuid: CPU partition ID
1091  */
tegra_pmc_cpu_remove_clamping(unsigned int cpuid)1092 int tegra_pmc_cpu_remove_clamping(unsigned int cpuid)
1093 {
1094 	int id;
1095 
1096 	id = tegra_get_cpu_powergate_id(pmc, cpuid);
1097 	if (id < 0)
1098 		return id;
1099 
1100 	return tegra_powergate_remove_clamping(id);
1101 }
1102 
tegra_pmc_program_reboot_reason(const char * cmd)1103 static void tegra_pmc_program_reboot_reason(const char *cmd)
1104 {
1105 	u32 value;
1106 
1107 	value = tegra_pmc_scratch_readl(pmc, pmc->soc->regs->scratch0);
1108 	value &= ~PMC_SCRATCH0_MODE_MASK;
1109 
1110 	if (cmd) {
1111 		if (strcmp(cmd, "recovery") == 0)
1112 			value |= PMC_SCRATCH0_MODE_RECOVERY;
1113 
1114 		if (strcmp(cmd, "bootloader") == 0)
1115 			value |= PMC_SCRATCH0_MODE_BOOTLOADER;
1116 
1117 		if (strcmp(cmd, "forced-recovery") == 0)
1118 			value |= PMC_SCRATCH0_MODE_RCM;
1119 	}
1120 
1121 	tegra_pmc_scratch_writel(pmc, value, pmc->soc->regs->scratch0);
1122 }
1123 
tegra_pmc_reboot_notify(struct notifier_block * this,unsigned long action,void * data)1124 static int tegra_pmc_reboot_notify(struct notifier_block *this,
1125 				   unsigned long action, void *data)
1126 {
1127 	if (action == SYS_RESTART)
1128 		tegra_pmc_program_reboot_reason(data);
1129 
1130 	return NOTIFY_DONE;
1131 }
1132 
1133 static struct notifier_block tegra_pmc_reboot_notifier = {
1134 	.notifier_call = tegra_pmc_reboot_notify,
1135 };
1136 
tegra_pmc_restart(void)1137 static void tegra_pmc_restart(void)
1138 {
1139 	u32 value;
1140 
1141 	/* reset everything but PMC_SCRATCH0 and PMC_RST_STATUS */
1142 	value = tegra_pmc_readl(pmc, PMC_CNTRL);
1143 	value |= PMC_CNTRL_MAIN_RST;
1144 	tegra_pmc_writel(pmc, value, PMC_CNTRL);
1145 }
1146 
tegra_pmc_restart_handler(struct sys_off_data * data)1147 static int tegra_pmc_restart_handler(struct sys_off_data *data)
1148 {
1149 	tegra_pmc_restart();
1150 
1151 	return NOTIFY_DONE;
1152 }
1153 
tegra_pmc_power_off_handler(struct sys_off_data * data)1154 static int tegra_pmc_power_off_handler(struct sys_off_data *data)
1155 {
1156 	/*
1157 	 * Reboot Nexus 7 into special bootloader mode if USB cable is
1158 	 * connected in order to display battery status and power off.
1159 	 */
1160 	if (of_machine_is_compatible("asus,grouper") &&
1161 	    power_supply_is_system_supplied()) {
1162 		const u32 go_to_charger_mode = 0xa5a55a5a;
1163 
1164 		tegra_pmc_writel(pmc, go_to_charger_mode, PMC_SCRATCH37);
1165 		tegra_pmc_restart();
1166 	}
1167 
1168 	return NOTIFY_DONE;
1169 }
1170 
powergate_show(struct seq_file * s,void * data)1171 static int powergate_show(struct seq_file *s, void *data)
1172 {
1173 	unsigned int i;
1174 	int status;
1175 
1176 	seq_printf(s, " powergate powered\n");
1177 	seq_printf(s, "------------------\n");
1178 
1179 	for (i = 0; i < pmc->soc->num_powergates; i++) {
1180 		status = tegra_powergate_is_powered(pmc, i);
1181 		if (status < 0)
1182 			continue;
1183 
1184 		seq_printf(s, " %9s %7s\n", pmc->soc->powergates[i],
1185 			   str_yes_no(status));
1186 	}
1187 
1188 	return 0;
1189 }
1190 
1191 DEFINE_SHOW_ATTRIBUTE(powergate);
1192 
tegra_powergate_of_get_clks(struct tegra_powergate * pg,struct device_node * np)1193 static int tegra_powergate_of_get_clks(struct tegra_powergate *pg,
1194 				       struct device_node *np)
1195 {
1196 	struct clk *clk;
1197 	unsigned int i, count;
1198 	int err;
1199 
1200 	count = of_clk_get_parent_count(np);
1201 	if (count == 0)
1202 		return -ENODEV;
1203 
1204 	pg->clks = kcalloc(count, sizeof(clk), GFP_KERNEL);
1205 	if (!pg->clks)
1206 		return -ENOMEM;
1207 
1208 	pg->clk_rates = kcalloc(count, sizeof(*pg->clk_rates), GFP_KERNEL);
1209 	if (!pg->clk_rates) {
1210 		kfree(pg->clks);
1211 		return -ENOMEM;
1212 	}
1213 
1214 	for (i = 0; i < count; i++) {
1215 		pg->clks[i] = of_clk_get(np, i);
1216 		if (IS_ERR(pg->clks[i])) {
1217 			err = PTR_ERR(pg->clks[i]);
1218 			goto err;
1219 		}
1220 	}
1221 
1222 	pg->num_clks = count;
1223 
1224 	return 0;
1225 
1226 err:
1227 	while (i--)
1228 		clk_put(pg->clks[i]);
1229 
1230 	kfree(pg->clk_rates);
1231 	kfree(pg->clks);
1232 
1233 	return err;
1234 }
1235 
tegra_powergate_of_get_resets(struct tegra_powergate * pg,struct device_node * np,bool off)1236 static int tegra_powergate_of_get_resets(struct tegra_powergate *pg,
1237 					 struct device_node *np, bool off)
1238 {
1239 	struct device *dev = pg->pmc->dev;
1240 	int err;
1241 
1242 	pg->reset = of_reset_control_array_get_exclusive_released(np);
1243 	if (IS_ERR(pg->reset)) {
1244 		err = PTR_ERR(pg->reset);
1245 		dev_err(dev, "failed to get device resets: %d\n", err);
1246 		return err;
1247 	}
1248 
1249 	err = reset_control_acquire(pg->reset);
1250 	if (err < 0) {
1251 		pr_err("failed to acquire resets: %d\n", err);
1252 		goto out;
1253 	}
1254 
1255 	if (off) {
1256 		err = reset_control_assert(pg->reset);
1257 	} else {
1258 		err = reset_control_deassert(pg->reset);
1259 		if (err < 0)
1260 			goto out;
1261 
1262 		reset_control_release(pg->reset);
1263 	}
1264 
1265 out:
1266 	if (err) {
1267 		reset_control_release(pg->reset);
1268 		reset_control_put(pg->reset);
1269 	}
1270 
1271 	return err;
1272 }
1273 
tegra_powergate_add(struct tegra_pmc * pmc,struct device_node * np)1274 static int tegra_powergate_add(struct tegra_pmc *pmc, struct device_node *np)
1275 {
1276 	struct device *dev = pmc->dev;
1277 	struct tegra_powergate *pg;
1278 	int id, err = 0;
1279 	bool off;
1280 
1281 	pg = kzalloc(sizeof(*pg), GFP_KERNEL);
1282 	if (!pg)
1283 		return -ENOMEM;
1284 
1285 	id = tegra_powergate_lookup(pmc, np->name);
1286 	if (id < 0) {
1287 		dev_err(dev, "powergate lookup failed for %pOFn: %d\n", np, id);
1288 		err = -ENODEV;
1289 		goto free_mem;
1290 	}
1291 
1292 	/*
1293 	 * Clear the bit for this powergate so it cannot be managed
1294 	 * directly via the legacy APIs for controlling powergates.
1295 	 */
1296 	clear_bit(id, pmc->powergates_available);
1297 
1298 	pg->id = id;
1299 	pg->genpd.name = np->name;
1300 	pg->genpd.power_off = tegra_genpd_power_off;
1301 	pg->genpd.power_on = tegra_genpd_power_on;
1302 	pg->pmc = pmc;
1303 
1304 	off = !tegra_powergate_is_powered(pmc, pg->id);
1305 
1306 	err = tegra_powergate_of_get_clks(pg, np);
1307 	if (err < 0) {
1308 		dev_err(dev, "failed to get clocks for %pOFn: %d\n", np, err);
1309 		goto set_available;
1310 	}
1311 
1312 	err = tegra_powergate_of_get_resets(pg, np, off);
1313 	if (err < 0) {
1314 		dev_err(dev, "failed to get resets for %pOFn: %d\n", np, err);
1315 		goto remove_clks;
1316 	}
1317 
1318 	if (!IS_ENABLED(CONFIG_PM_GENERIC_DOMAINS)) {
1319 		if (off)
1320 			WARN_ON(tegra_powergate_power_up(pg, true));
1321 
1322 		goto remove_resets;
1323 	}
1324 
1325 	err = pm_genpd_init(&pg->genpd, NULL, off);
1326 	if (err < 0) {
1327 		dev_err(dev, "failed to initialise PM domain %pOFn: %d\n", np,
1328 		       err);
1329 		goto remove_resets;
1330 	}
1331 
1332 	err = of_genpd_add_provider_simple(np, &pg->genpd);
1333 	if (err < 0) {
1334 		dev_err(dev, "failed to add PM domain provider for %pOFn: %d\n",
1335 			np, err);
1336 		goto remove_genpd;
1337 	}
1338 
1339 	dev_dbg(dev, "added PM domain %s\n", pg->genpd.name);
1340 
1341 	return 0;
1342 
1343 remove_genpd:
1344 	pm_genpd_remove(&pg->genpd);
1345 
1346 remove_resets:
1347 	reset_control_put(pg->reset);
1348 
1349 remove_clks:
1350 	while (pg->num_clks--)
1351 		clk_put(pg->clks[pg->num_clks]);
1352 
1353 	kfree(pg->clks);
1354 
1355 set_available:
1356 	set_bit(id, pmc->powergates_available);
1357 
1358 free_mem:
1359 	kfree(pg);
1360 
1361 	return err;
1362 }
1363 
tegra_pmc_core_domain_state_synced(void)1364 bool tegra_pmc_core_domain_state_synced(void)
1365 {
1366 	return pmc->core_domain_state_synced;
1367 }
1368 
1369 static int
tegra_pmc_core_pd_set_performance_state(struct generic_pm_domain * genpd,unsigned int level)1370 tegra_pmc_core_pd_set_performance_state(struct generic_pm_domain *genpd,
1371 					unsigned int level)
1372 {
1373 	struct dev_pm_opp *opp;
1374 	int err;
1375 
1376 	opp = dev_pm_opp_find_level_ceil(&genpd->dev, &level);
1377 	if (IS_ERR(opp)) {
1378 		dev_err(&genpd->dev, "failed to find OPP for level %u: %pe\n",
1379 			level, opp);
1380 		return PTR_ERR(opp);
1381 	}
1382 
1383 	mutex_lock(&pmc->powergates_lock);
1384 	err = dev_pm_opp_set_opp(pmc->dev, opp);
1385 	mutex_unlock(&pmc->powergates_lock);
1386 
1387 	dev_pm_opp_put(opp);
1388 
1389 	if (err) {
1390 		dev_err(&genpd->dev, "failed to set voltage to %duV: %d\n",
1391 			level, err);
1392 		return err;
1393 	}
1394 
1395 	return 0;
1396 }
1397 
tegra_pmc_core_pd_add(struct tegra_pmc * pmc,struct device_node * np)1398 static int tegra_pmc_core_pd_add(struct tegra_pmc *pmc, struct device_node *np)
1399 {
1400 	struct generic_pm_domain *genpd;
1401 	const char *rname[] = { "core", NULL};
1402 	int err;
1403 
1404 	genpd = devm_kzalloc(pmc->dev, sizeof(*genpd), GFP_KERNEL);
1405 	if (!genpd)
1406 		return -ENOMEM;
1407 
1408 	genpd->name = "core";
1409 	genpd->set_performance_state = tegra_pmc_core_pd_set_performance_state;
1410 
1411 	err = devm_pm_opp_set_regulators(pmc->dev, rname);
1412 	if (err)
1413 		return dev_err_probe(pmc->dev, err,
1414 				     "failed to set core OPP regulator\n");
1415 
1416 	err = pm_genpd_init(genpd, NULL, false);
1417 	if (err) {
1418 		dev_err(pmc->dev, "failed to init core genpd: %d\n", err);
1419 		return err;
1420 	}
1421 
1422 	err = of_genpd_add_provider_simple(np, genpd);
1423 	if (err) {
1424 		dev_err(pmc->dev, "failed to add core genpd: %d\n", err);
1425 		goto remove_genpd;
1426 	}
1427 
1428 	pmc->core_domain_registered = true;
1429 
1430 	return 0;
1431 
1432 remove_genpd:
1433 	pm_genpd_remove(genpd);
1434 
1435 	return err;
1436 }
1437 
tegra_powergate_init(struct tegra_pmc * pmc,struct device_node * parent)1438 static int tegra_powergate_init(struct tegra_pmc *pmc,
1439 				struct device_node *parent)
1440 {
1441 	struct of_phandle_args child_args, parent_args;
1442 	struct device_node *np;
1443 	int err = 0;
1444 
1445 	/*
1446 	 * Core power domain is the parent of powergate domains, hence it
1447 	 * should be registered first.
1448 	 */
1449 	np = of_get_child_by_name(parent, "core-domain");
1450 	if (np) {
1451 		err = tegra_pmc_core_pd_add(pmc, np);
1452 		of_node_put(np);
1453 		if (err)
1454 			return err;
1455 	}
1456 
1457 	np = of_get_child_by_name(parent, "powergates");
1458 	if (!np)
1459 		return 0;
1460 
1461 	for_each_child_of_node_scoped(np, child) {
1462 		err = tegra_powergate_add(pmc, child);
1463 		if (err < 0)
1464 			break;
1465 
1466 		if (of_parse_phandle_with_args(child, "power-domains",
1467 					       "#power-domain-cells",
1468 					       0, &parent_args))
1469 			continue;
1470 
1471 		child_args.np = child;
1472 		child_args.args_count = 0;
1473 
1474 		err = of_genpd_add_subdomain(&parent_args, &child_args);
1475 		of_node_put(parent_args.np);
1476 		if (err)
1477 			break;
1478 	}
1479 
1480 	of_node_put(np);
1481 
1482 	return err;
1483 }
1484 
tegra_powergate_remove(struct generic_pm_domain * genpd)1485 static void tegra_powergate_remove(struct generic_pm_domain *genpd)
1486 {
1487 	struct tegra_powergate *pg = to_powergate(genpd);
1488 
1489 	reset_control_put(pg->reset);
1490 
1491 	while (pg->num_clks--)
1492 		clk_put(pg->clks[pg->num_clks]);
1493 
1494 	kfree(pg->clks);
1495 
1496 	set_bit(pg->id, pmc->powergates_available);
1497 
1498 	kfree(pg);
1499 }
1500 
tegra_powergate_remove_all(struct device_node * parent)1501 static void tegra_powergate_remove_all(struct device_node *parent)
1502 {
1503 	struct generic_pm_domain *genpd;
1504 	struct device_node *np, *child;
1505 
1506 	np = of_get_child_by_name(parent, "powergates");
1507 	if (!np)
1508 		return;
1509 
1510 	for_each_child_of_node(np, child) {
1511 		of_genpd_del_provider(child);
1512 
1513 		genpd = of_genpd_remove_last(child);
1514 		if (IS_ERR(genpd))
1515 			continue;
1516 
1517 		tegra_powergate_remove(genpd);
1518 	}
1519 
1520 	of_node_put(np);
1521 
1522 	np = of_get_child_by_name(parent, "core-domain");
1523 	if (np) {
1524 		of_genpd_del_provider(np);
1525 		of_genpd_remove_last(np);
1526 	}
1527 }
1528 
1529 static const struct tegra_io_pad_soc *
tegra_io_pad_find(struct tegra_pmc * pmc,enum tegra_io_pad id)1530 tegra_io_pad_find(struct tegra_pmc *pmc, enum tegra_io_pad id)
1531 {
1532 	unsigned int i;
1533 
1534 	for (i = 0; i < pmc->soc->num_io_pads; i++)
1535 		if (pmc->soc->io_pads[i].id == id)
1536 			return &pmc->soc->io_pads[i];
1537 
1538 	return NULL;
1539 }
1540 
tegra_io_pad_prepare(struct tegra_pmc * pmc,const struct tegra_io_pad_soc * pad,unsigned long * request,unsigned long * status,u32 * mask)1541 static int tegra_io_pad_prepare(struct tegra_pmc *pmc,
1542 				const struct tegra_io_pad_soc *pad,
1543 				unsigned long *request,
1544 				unsigned long *status,
1545 				u32 *mask)
1546 {
1547 	unsigned long rate, value;
1548 
1549 	if (pad->dpd == UINT_MAX)
1550 		return -EINVAL;
1551 
1552 	*request = pad->request;
1553 	*status = pad->status;
1554 	*mask = BIT(pad->dpd);
1555 
1556 	if (pmc->clk) {
1557 		rate = pmc->rate;
1558 		if (!rate) {
1559 			dev_err(pmc->dev, "failed to get clock rate\n");
1560 			return -ENODEV;
1561 		}
1562 
1563 		tegra_pmc_writel(pmc, DPD_SAMPLE_ENABLE, DPD_SAMPLE);
1564 
1565 		/* must be at least 200 ns, in APB (PCLK) clock cycles */
1566 		value = DIV_ROUND_UP(1000000000, rate);
1567 		value = DIV_ROUND_UP(200, value);
1568 		tegra_pmc_writel(pmc, value, SEL_DPD_TIM);
1569 	}
1570 
1571 	return 0;
1572 }
1573 
tegra_io_pad_poll(struct tegra_pmc * pmc,unsigned long offset,u32 mask,u32 val,unsigned long timeout)1574 static int tegra_io_pad_poll(struct tegra_pmc *pmc, unsigned long offset,
1575 			     u32 mask, u32 val, unsigned long timeout)
1576 {
1577 	u32 value;
1578 
1579 	timeout = jiffies + msecs_to_jiffies(timeout);
1580 
1581 	while (time_after(timeout, jiffies)) {
1582 		value = tegra_pmc_readl(pmc, offset);
1583 		if ((value & mask) == val)
1584 			return 0;
1585 
1586 		usleep_range(250, 1000);
1587 	}
1588 
1589 	return -ETIMEDOUT;
1590 }
1591 
tegra_io_pad_unprepare(struct tegra_pmc * pmc)1592 static void tegra_io_pad_unprepare(struct tegra_pmc *pmc)
1593 {
1594 	if (pmc->clk)
1595 		tegra_pmc_writel(pmc, DPD_SAMPLE_DISABLE, DPD_SAMPLE);
1596 }
1597 
1598 /**
1599  * tegra_io_pad_power_enable() - enable power to I/O pad
1600  * @id: Tegra I/O pad ID for which to enable power
1601  *
1602  * Returns: 0 on success or a negative error code on failure.
1603  */
tegra_io_pad_power_enable(enum tegra_io_pad id)1604 int tegra_io_pad_power_enable(enum tegra_io_pad id)
1605 {
1606 	const struct tegra_io_pad_soc *pad;
1607 	unsigned long request, status;
1608 	u32 mask;
1609 	int err;
1610 
1611 	pad = tegra_io_pad_find(pmc, id);
1612 	if (!pad) {
1613 		dev_err(pmc->dev, "invalid I/O pad ID %u\n", id);
1614 		return -ENOENT;
1615 	}
1616 
1617 	mutex_lock(&pmc->powergates_lock);
1618 
1619 	err = tegra_io_pad_prepare(pmc, pad, &request, &status, &mask);
1620 	if (err < 0) {
1621 		dev_err(pmc->dev, "failed to prepare I/O pad: %d\n", err);
1622 		goto unlock;
1623 	}
1624 
1625 	tegra_pmc_writel(pmc, IO_DPD_REQ_CODE_OFF | mask, request);
1626 
1627 	err = tegra_io_pad_poll(pmc, status, mask, 0, 250);
1628 	if (err < 0) {
1629 		dev_err(pmc->dev, "failed to enable I/O pad: %d\n", err);
1630 		goto unlock;
1631 	}
1632 
1633 	tegra_io_pad_unprepare(pmc);
1634 
1635 unlock:
1636 	mutex_unlock(&pmc->powergates_lock);
1637 	return err;
1638 }
1639 EXPORT_SYMBOL(tegra_io_pad_power_enable);
1640 
1641 /**
1642  * tegra_io_pad_power_disable() - disable power to I/O pad
1643  * @id: Tegra I/O pad ID for which to disable power
1644  *
1645  * Returns: 0 on success or a negative error code on failure.
1646  */
tegra_io_pad_power_disable(enum tegra_io_pad id)1647 int tegra_io_pad_power_disable(enum tegra_io_pad id)
1648 {
1649 	const struct tegra_io_pad_soc *pad;
1650 	unsigned long request, status;
1651 	u32 mask;
1652 	int err;
1653 
1654 	pad = tegra_io_pad_find(pmc, id);
1655 	if (!pad) {
1656 		dev_err(pmc->dev, "invalid I/O pad ID %u\n", id);
1657 		return -ENOENT;
1658 	}
1659 
1660 	mutex_lock(&pmc->powergates_lock);
1661 
1662 	err = tegra_io_pad_prepare(pmc, pad, &request, &status, &mask);
1663 	if (err < 0) {
1664 		dev_err(pmc->dev, "failed to prepare I/O pad: %d\n", err);
1665 		goto unlock;
1666 	}
1667 
1668 	tegra_pmc_writel(pmc, IO_DPD_REQ_CODE_ON | mask, request);
1669 
1670 	err = tegra_io_pad_poll(pmc, status, mask, mask, 250);
1671 	if (err < 0) {
1672 		dev_err(pmc->dev, "failed to disable I/O pad: %d\n", err);
1673 		goto unlock;
1674 	}
1675 
1676 	tegra_io_pad_unprepare(pmc);
1677 
1678 unlock:
1679 	mutex_unlock(&pmc->powergates_lock);
1680 	return err;
1681 }
1682 EXPORT_SYMBOL(tegra_io_pad_power_disable);
1683 
tegra_io_pad_is_powered(struct tegra_pmc * pmc,enum tegra_io_pad id)1684 static int tegra_io_pad_is_powered(struct tegra_pmc *pmc, enum tegra_io_pad id)
1685 {
1686 	const struct tegra_io_pad_soc *pad;
1687 	unsigned long status;
1688 	u32 mask, value;
1689 
1690 	pad = tegra_io_pad_find(pmc, id);
1691 	if (!pad) {
1692 		dev_err(pmc->dev, "invalid I/O pad ID %u\n", id);
1693 		return -ENOENT;
1694 	}
1695 
1696 	if (pad->dpd == UINT_MAX)
1697 		return -EINVAL;
1698 
1699 	status = pad->status;
1700 	mask = BIT(pad->dpd);
1701 
1702 	value = tegra_pmc_readl(pmc, status);
1703 
1704 	return !(value & mask);
1705 }
1706 
tegra_io_pad_set_voltage(struct tegra_pmc * pmc,enum tegra_io_pad id,int voltage)1707 static int tegra_io_pad_set_voltage(struct tegra_pmc *pmc, enum tegra_io_pad id,
1708 				    int voltage)
1709 {
1710 	const struct tegra_io_pad_soc *pad;
1711 	u32 value;
1712 
1713 	pad = tegra_io_pad_find(pmc, id);
1714 	if (!pad)
1715 		return -ENOENT;
1716 
1717 	if (pad->voltage == UINT_MAX)
1718 		return -ENOTSUPP;
1719 
1720 	mutex_lock(&pmc->powergates_lock);
1721 
1722 	if (pmc->soc->has_impl_33v_pwr) {
1723 		value = tegra_pmc_readl(pmc, PMC_IMPL_E_33V_PWR);
1724 
1725 		if (voltage == TEGRA_IO_PAD_VOLTAGE_1V8)
1726 			value &= ~BIT(pad->voltage);
1727 		else
1728 			value |= BIT(pad->voltage);
1729 
1730 		tegra_pmc_writel(pmc, value, PMC_IMPL_E_33V_PWR);
1731 	} else {
1732 		/* write-enable PMC_PWR_DET_VALUE[pad->voltage] */
1733 		value = tegra_pmc_readl(pmc, PMC_PWR_DET);
1734 		value |= BIT(pad->voltage);
1735 		tegra_pmc_writel(pmc, value, PMC_PWR_DET);
1736 
1737 		/* update I/O voltage */
1738 		value = tegra_pmc_readl(pmc, PMC_PWR_DET_VALUE);
1739 
1740 		if (voltage == TEGRA_IO_PAD_VOLTAGE_1V8)
1741 			value &= ~BIT(pad->voltage);
1742 		else
1743 			value |= BIT(pad->voltage);
1744 
1745 		tegra_pmc_writel(pmc, value, PMC_PWR_DET_VALUE);
1746 	}
1747 
1748 	mutex_unlock(&pmc->powergates_lock);
1749 
1750 	usleep_range(100, 250);
1751 
1752 	return 0;
1753 }
1754 
tegra_io_pad_get_voltage(struct tegra_pmc * pmc,enum tegra_io_pad id)1755 static int tegra_io_pad_get_voltage(struct tegra_pmc *pmc, enum tegra_io_pad id)
1756 {
1757 	const struct tegra_io_pad_soc *pad;
1758 	u32 value;
1759 
1760 	pad = tegra_io_pad_find(pmc, id);
1761 	if (!pad)
1762 		return -ENOENT;
1763 
1764 	if (pad->voltage == UINT_MAX)
1765 		return -ENOTSUPP;
1766 
1767 	if (pmc->soc->has_impl_33v_pwr)
1768 		value = tegra_pmc_readl(pmc, PMC_IMPL_E_33V_PWR);
1769 	else
1770 		value = tegra_pmc_readl(pmc, PMC_PWR_DET_VALUE);
1771 
1772 	if ((value & BIT(pad->voltage)) == 0)
1773 		return TEGRA_IO_PAD_VOLTAGE_1V8;
1774 
1775 	return TEGRA_IO_PAD_VOLTAGE_3V3;
1776 }
1777 
1778 #ifdef CONFIG_PM_SLEEP
tegra_pmc_get_suspend_mode(void)1779 enum tegra_suspend_mode tegra_pmc_get_suspend_mode(void)
1780 {
1781 	return pmc->suspend_mode;
1782 }
1783 
tegra_pmc_set_suspend_mode(enum tegra_suspend_mode mode)1784 void tegra_pmc_set_suspend_mode(enum tegra_suspend_mode mode)
1785 {
1786 	if (mode < TEGRA_SUSPEND_NONE || mode >= TEGRA_MAX_SUSPEND_MODE)
1787 		return;
1788 
1789 	pmc->suspend_mode = mode;
1790 }
1791 
tegra_pmc_enter_suspend_mode(enum tegra_suspend_mode mode)1792 void tegra_pmc_enter_suspend_mode(enum tegra_suspend_mode mode)
1793 {
1794 	unsigned long long rate = 0;
1795 	u64 ticks;
1796 	u32 value;
1797 
1798 	switch (mode) {
1799 	case TEGRA_SUSPEND_LP1:
1800 		rate = 32768;
1801 		break;
1802 
1803 	case TEGRA_SUSPEND_LP2:
1804 		rate = pmc->rate;
1805 		break;
1806 
1807 	default:
1808 		break;
1809 	}
1810 
1811 	if (WARN_ON_ONCE(rate == 0))
1812 		rate = 100000000;
1813 
1814 	ticks = pmc->cpu_good_time * rate + USEC_PER_SEC - 1;
1815 	do_div(ticks, USEC_PER_SEC);
1816 	tegra_pmc_writel(pmc, ticks, PMC_CPUPWRGOOD_TIMER);
1817 
1818 	ticks = pmc->cpu_off_time * rate + USEC_PER_SEC - 1;
1819 	do_div(ticks, USEC_PER_SEC);
1820 	tegra_pmc_writel(pmc, ticks, PMC_CPUPWROFF_TIMER);
1821 
1822 	value = tegra_pmc_readl(pmc, PMC_CNTRL);
1823 	value &= ~PMC_CNTRL_SIDE_EFFECT_LP0;
1824 	value |= PMC_CNTRL_CPU_PWRREQ_OE;
1825 	tegra_pmc_writel(pmc, value, PMC_CNTRL);
1826 }
1827 #endif
1828 
tegra_pmc_parse_dt(struct tegra_pmc * pmc,struct device_node * np)1829 static int tegra_pmc_parse_dt(struct tegra_pmc *pmc, struct device_node *np)
1830 {
1831 	u32 value, values[2];
1832 
1833 	if (of_property_read_u32(np, "nvidia,suspend-mode", &value)) {
1834 		pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1835 	} else {
1836 		switch (value) {
1837 		case 0:
1838 			pmc->suspend_mode = TEGRA_SUSPEND_LP0;
1839 			break;
1840 
1841 		case 1:
1842 			pmc->suspend_mode = TEGRA_SUSPEND_LP1;
1843 			break;
1844 
1845 		case 2:
1846 			pmc->suspend_mode = TEGRA_SUSPEND_LP2;
1847 			break;
1848 
1849 		default:
1850 			pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1851 			break;
1852 		}
1853 	}
1854 
1855 	pmc->suspend_mode = tegra_pm_validate_suspend_mode(pmc->suspend_mode);
1856 
1857 	if (of_property_read_u32(np, "nvidia,cpu-pwr-good-time", &value))
1858 		pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1859 
1860 	pmc->cpu_good_time = value;
1861 
1862 	if (of_property_read_u32(np, "nvidia,cpu-pwr-off-time", &value))
1863 		pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1864 
1865 	pmc->cpu_off_time = value;
1866 
1867 	if (of_property_read_u32_array(np, "nvidia,core-pwr-good-time",
1868 				       values, ARRAY_SIZE(values)))
1869 		pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1870 
1871 	pmc->core_osc_time = values[0];
1872 	pmc->core_pmu_time = values[1];
1873 
1874 	if (of_property_read_u32(np, "nvidia,core-pwr-off-time", &value))
1875 		pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1876 
1877 	pmc->core_off_time = value;
1878 
1879 	pmc->corereq_high = of_property_read_bool(np,
1880 				"nvidia,core-power-req-active-high");
1881 
1882 	pmc->sysclkreq_high = of_property_read_bool(np,
1883 				"nvidia,sys-clock-req-active-high");
1884 
1885 	pmc->combined_req = of_property_read_bool(np,
1886 				"nvidia,combined-power-req");
1887 
1888 	pmc->cpu_pwr_good_en = of_property_read_bool(np,
1889 				"nvidia,cpu-pwr-good-en");
1890 
1891 	if (of_property_read_u32_array(np, "nvidia,lp0-vec", values,
1892 				       ARRAY_SIZE(values)))
1893 		if (pmc->suspend_mode == TEGRA_SUSPEND_LP0)
1894 			pmc->suspend_mode = TEGRA_SUSPEND_LP1;
1895 
1896 	pmc->lp0_vec_phys = values[0];
1897 	pmc->lp0_vec_size = values[1];
1898 
1899 	return 0;
1900 }
1901 
tegra_pmc_init(struct tegra_pmc * pmc)1902 static int tegra_pmc_init(struct tegra_pmc *pmc)
1903 {
1904 	if (pmc->soc->max_wake_events > 0) {
1905 		pmc->wake_type_level_map = bitmap_zalloc(pmc->soc->max_wake_events, GFP_KERNEL);
1906 		if (!pmc->wake_type_level_map)
1907 			return -ENOMEM;
1908 
1909 		pmc->wake_type_dual_edge_map = bitmap_zalloc(pmc->soc->max_wake_events, GFP_KERNEL);
1910 		if (!pmc->wake_type_dual_edge_map)
1911 			return -ENOMEM;
1912 
1913 		pmc->wake_sw_status_map = bitmap_zalloc(pmc->soc->max_wake_events, GFP_KERNEL);
1914 		if (!pmc->wake_sw_status_map)
1915 			return -ENOMEM;
1916 
1917 		pmc->wake_cntrl_level_map = bitmap_zalloc(pmc->soc->max_wake_events, GFP_KERNEL);
1918 		if (!pmc->wake_cntrl_level_map)
1919 			return -ENOMEM;
1920 	}
1921 
1922 	if (pmc->soc->init)
1923 		pmc->soc->init(pmc);
1924 
1925 	return 0;
1926 }
1927 
tegra_pmc_init_tsense_reset(struct tegra_pmc * pmc)1928 static void tegra_pmc_init_tsense_reset(struct tegra_pmc *pmc)
1929 {
1930 	static const char disabled[] = "emergency thermal reset disabled";
1931 	u32 pmu_addr, ctrl_id, reg_addr, reg_data, pinmux;
1932 	struct device *dev = pmc->dev;
1933 	struct device_node *np;
1934 	u32 value, checksum;
1935 
1936 	if (!pmc->soc->has_tsense_reset)
1937 		return;
1938 
1939 	np = of_get_child_by_name(pmc->dev->of_node, "i2c-thermtrip");
1940 	if (!np) {
1941 		dev_warn(dev, "i2c-thermtrip node not found, %s.\n", disabled);
1942 		return;
1943 	}
1944 
1945 	if (of_property_read_u32(np, "nvidia,i2c-controller-id", &ctrl_id)) {
1946 		dev_err(dev, "I2C controller ID missing, %s.\n", disabled);
1947 		goto out;
1948 	}
1949 
1950 	if (of_property_read_u32(np, "nvidia,bus-addr", &pmu_addr)) {
1951 		dev_err(dev, "nvidia,bus-addr missing, %s.\n", disabled);
1952 		goto out;
1953 	}
1954 
1955 	if (of_property_read_u32(np, "nvidia,reg-addr", &reg_addr)) {
1956 		dev_err(dev, "nvidia,reg-addr missing, %s.\n", disabled);
1957 		goto out;
1958 	}
1959 
1960 	if (of_property_read_u32(np, "nvidia,reg-data", &reg_data)) {
1961 		dev_err(dev, "nvidia,reg-data missing, %s.\n", disabled);
1962 		goto out;
1963 	}
1964 
1965 	if (of_property_read_u32(np, "nvidia,pinmux-id", &pinmux))
1966 		pinmux = 0;
1967 
1968 	value = tegra_pmc_readl(pmc, PMC_SENSOR_CTRL);
1969 	value |= PMC_SENSOR_CTRL_SCRATCH_WRITE;
1970 	tegra_pmc_writel(pmc, value, PMC_SENSOR_CTRL);
1971 
1972 	value = (reg_data << PMC_SCRATCH54_DATA_SHIFT) |
1973 		(reg_addr << PMC_SCRATCH54_ADDR_SHIFT);
1974 	tegra_pmc_writel(pmc, value, PMC_SCRATCH54);
1975 
1976 	value = PMC_SCRATCH55_RESET_TEGRA;
1977 	value |= ctrl_id << PMC_SCRATCH55_CNTRL_ID_SHIFT;
1978 	value |= pinmux << PMC_SCRATCH55_PINMUX_SHIFT;
1979 	value |= pmu_addr << PMC_SCRATCH55_I2CSLV1_SHIFT;
1980 
1981 	/*
1982 	 * Calculate checksum of SCRATCH54, SCRATCH55 fields. Bits 23:16 will
1983 	 * contain the checksum and are currently zero, so they are not added.
1984 	 */
1985 	checksum = reg_addr + reg_data + (value & 0xff) + ((value >> 8) & 0xff)
1986 		+ ((value >> 24) & 0xff);
1987 	checksum &= 0xff;
1988 	checksum = 0x100 - checksum;
1989 
1990 	value |= checksum << PMC_SCRATCH55_CHECKSUM_SHIFT;
1991 
1992 	tegra_pmc_writel(pmc, value, PMC_SCRATCH55);
1993 
1994 	value = tegra_pmc_readl(pmc, PMC_SENSOR_CTRL);
1995 	value |= PMC_SENSOR_CTRL_ENABLE_RST;
1996 	tegra_pmc_writel(pmc, value, PMC_SENSOR_CTRL);
1997 
1998 	dev_info(pmc->dev, "emergency thermal reset enabled\n");
1999 
2000 out:
2001 	of_node_put(np);
2002 }
2003 
tegra_io_pad_pinctrl_get_groups_count(struct pinctrl_dev * pctl_dev)2004 static int tegra_io_pad_pinctrl_get_groups_count(struct pinctrl_dev *pctl_dev)
2005 {
2006 	struct tegra_pmc *pmc = pinctrl_dev_get_drvdata(pctl_dev);
2007 
2008 	return pmc->soc->num_io_pads;
2009 }
2010 
tegra_io_pad_pinctrl_get_group_name(struct pinctrl_dev * pctl,unsigned int group)2011 static const char *tegra_io_pad_pinctrl_get_group_name(struct pinctrl_dev *pctl,
2012 						       unsigned int group)
2013 {
2014 	struct tegra_pmc *pmc = pinctrl_dev_get_drvdata(pctl);
2015 
2016 	return pmc->soc->io_pads[group].name;
2017 }
2018 
tegra_io_pad_pinctrl_get_group_pins(struct pinctrl_dev * pctl_dev,unsigned int group,const unsigned int ** pins,unsigned int * num_pins)2019 static int tegra_io_pad_pinctrl_get_group_pins(struct pinctrl_dev *pctl_dev,
2020 					       unsigned int group,
2021 					       const unsigned int **pins,
2022 					       unsigned int *num_pins)
2023 {
2024 	struct tegra_pmc *pmc = pinctrl_dev_get_drvdata(pctl_dev);
2025 
2026 	*pins = &pmc->soc->io_pads[group].id;
2027 	*num_pins = 1;
2028 
2029 	return 0;
2030 }
2031 
2032 static const struct pinctrl_ops tegra_io_pad_pinctrl_ops = {
2033 	.get_groups_count = tegra_io_pad_pinctrl_get_groups_count,
2034 	.get_group_name = tegra_io_pad_pinctrl_get_group_name,
2035 	.get_group_pins = tegra_io_pad_pinctrl_get_group_pins,
2036 	.dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
2037 	.dt_free_map = pinconf_generic_dt_free_map,
2038 };
2039 
tegra_io_pad_pinconf_get(struct pinctrl_dev * pctl_dev,unsigned int pin,unsigned long * config)2040 static int tegra_io_pad_pinconf_get(struct pinctrl_dev *pctl_dev,
2041 				    unsigned int pin, unsigned long *config)
2042 {
2043 	enum pin_config_param param = pinconf_to_config_param(*config);
2044 	struct tegra_pmc *pmc = pinctrl_dev_get_drvdata(pctl_dev);
2045 	const struct tegra_io_pad_soc *pad;
2046 	int ret;
2047 	u32 arg;
2048 
2049 	pad = tegra_io_pad_find(pmc, pin);
2050 	if (!pad)
2051 		return -EINVAL;
2052 
2053 	switch (param) {
2054 	case PIN_CONFIG_POWER_SOURCE:
2055 		ret = tegra_io_pad_get_voltage(pmc, pad->id);
2056 		if (ret < 0)
2057 			return ret;
2058 
2059 		arg = ret;
2060 		break;
2061 
2062 	case PIN_CONFIG_MODE_LOW_POWER:
2063 		ret = tegra_io_pad_is_powered(pmc, pad->id);
2064 		if (ret < 0)
2065 			return ret;
2066 
2067 		arg = !ret;
2068 		break;
2069 
2070 	default:
2071 		return -EINVAL;
2072 	}
2073 
2074 	*config = pinconf_to_config_packed(param, arg);
2075 
2076 	return 0;
2077 }
2078 
tegra_io_pad_pinconf_set(struct pinctrl_dev * pctl_dev,unsigned int pin,unsigned long * configs,unsigned int num_configs)2079 static int tegra_io_pad_pinconf_set(struct pinctrl_dev *pctl_dev,
2080 				    unsigned int pin, unsigned long *configs,
2081 				    unsigned int num_configs)
2082 {
2083 	struct tegra_pmc *pmc = pinctrl_dev_get_drvdata(pctl_dev);
2084 	const struct tegra_io_pad_soc *pad;
2085 	enum pin_config_param param;
2086 	unsigned int i;
2087 	int err;
2088 	u32 arg;
2089 
2090 	pad = tegra_io_pad_find(pmc, pin);
2091 	if (!pad)
2092 		return -EINVAL;
2093 
2094 	for (i = 0; i < num_configs; ++i) {
2095 		param = pinconf_to_config_param(configs[i]);
2096 		arg = pinconf_to_config_argument(configs[i]);
2097 
2098 		switch (param) {
2099 		case PIN_CONFIG_MODE_LOW_POWER:
2100 			if (arg)
2101 				err = tegra_io_pad_power_disable(pad->id);
2102 			else
2103 				err = tegra_io_pad_power_enable(pad->id);
2104 			if (err)
2105 				return err;
2106 			break;
2107 		case PIN_CONFIG_POWER_SOURCE:
2108 			if (arg != TEGRA_IO_PAD_VOLTAGE_1V8 &&
2109 			    arg != TEGRA_IO_PAD_VOLTAGE_3V3)
2110 				return -EINVAL;
2111 			err = tegra_io_pad_set_voltage(pmc, pad->id, arg);
2112 			if (err)
2113 				return err;
2114 			break;
2115 		default:
2116 			return -EINVAL;
2117 		}
2118 	}
2119 
2120 	return 0;
2121 }
2122 
2123 static const struct pinconf_ops tegra_io_pad_pinconf_ops = {
2124 	.pin_config_get = tegra_io_pad_pinconf_get,
2125 	.pin_config_set = tegra_io_pad_pinconf_set,
2126 	.is_generic = true,
2127 };
2128 
2129 static struct pinctrl_desc tegra_pmc_pctl_desc = {
2130 	.pctlops = &tegra_io_pad_pinctrl_ops,
2131 	.confops = &tegra_io_pad_pinconf_ops,
2132 };
2133 
tegra_pmc_pinctrl_init(struct tegra_pmc * pmc)2134 static int tegra_pmc_pinctrl_init(struct tegra_pmc *pmc)
2135 {
2136 	int err;
2137 
2138 	if (!pmc->soc->num_pin_descs)
2139 		return 0;
2140 
2141 	tegra_pmc_pctl_desc.name = dev_name(pmc->dev);
2142 	tegra_pmc_pctl_desc.pins = pmc->soc->pin_descs;
2143 	tegra_pmc_pctl_desc.npins = pmc->soc->num_pin_descs;
2144 
2145 	pmc->pctl_dev = devm_pinctrl_register(pmc->dev, &tegra_pmc_pctl_desc,
2146 					      pmc);
2147 	if (IS_ERR(pmc->pctl_dev)) {
2148 		err = PTR_ERR(pmc->pctl_dev);
2149 		dev_err(pmc->dev, "failed to register pin controller: %d\n",
2150 			err);
2151 		return err;
2152 	}
2153 
2154 	return 0;
2155 }
2156 
reset_reason_show(struct device * dev,struct device_attribute * attr,char * buf)2157 static ssize_t reset_reason_show(struct device *dev,
2158 				 struct device_attribute *attr, char *buf)
2159 {
2160 	u32 value;
2161 
2162 	value = tegra_pmc_readl(pmc, pmc->soc->regs->rst_status);
2163 	value &= pmc->soc->regs->rst_source_mask;
2164 	value >>= pmc->soc->regs->rst_source_shift;
2165 
2166 	if (WARN_ON(value >= pmc->soc->num_reset_sources))
2167 		return sprintf(buf, "%s\n", "UNKNOWN");
2168 
2169 	return sprintf(buf, "%s\n", pmc->soc->reset_sources[value]);
2170 }
2171 
2172 static DEVICE_ATTR_RO(reset_reason);
2173 
reset_level_show(struct device * dev,struct device_attribute * attr,char * buf)2174 static ssize_t reset_level_show(struct device *dev,
2175 				struct device_attribute *attr, char *buf)
2176 {
2177 	u32 value;
2178 
2179 	value = tegra_pmc_readl(pmc, pmc->soc->regs->rst_status);
2180 	value &= pmc->soc->regs->rst_level_mask;
2181 	value >>= pmc->soc->regs->rst_level_shift;
2182 
2183 	if (WARN_ON(value >= pmc->soc->num_reset_levels))
2184 		return sprintf(buf, "%s\n", "UNKNOWN");
2185 
2186 	return sprintf(buf, "%s\n", pmc->soc->reset_levels[value]);
2187 }
2188 
2189 static DEVICE_ATTR_RO(reset_level);
2190 
tegra_pmc_reset_sysfs_init(struct tegra_pmc * pmc)2191 static void tegra_pmc_reset_sysfs_init(struct tegra_pmc *pmc)
2192 {
2193 	struct device *dev = pmc->dev;
2194 	int err = 0;
2195 
2196 	if (pmc->soc->reset_sources) {
2197 		err = device_create_file(dev, &dev_attr_reset_reason);
2198 		if (err < 0)
2199 			dev_warn(dev,
2200 				 "failed to create attr \"reset_reason\": %d\n",
2201 				 err);
2202 	}
2203 
2204 	if (pmc->soc->reset_levels) {
2205 		err = device_create_file(dev, &dev_attr_reset_level);
2206 		if (err < 0)
2207 			dev_warn(dev,
2208 				 "failed to create attr \"reset_level\": %d\n",
2209 				 err);
2210 	}
2211 }
2212 
tegra_pmc_irq_translate(struct irq_domain * domain,struct irq_fwspec * fwspec,unsigned long * hwirq,unsigned int * type)2213 static int tegra_pmc_irq_translate(struct irq_domain *domain,
2214 				   struct irq_fwspec *fwspec,
2215 				   unsigned long *hwirq,
2216 				   unsigned int *type)
2217 {
2218 	if (WARN_ON(fwspec->param_count < 2))
2219 		return -EINVAL;
2220 
2221 	*hwirq = fwspec->param[0];
2222 	*type = fwspec->param[1];
2223 
2224 	return 0;
2225 }
2226 
tegra_pmc_irq_alloc(struct irq_domain * domain,unsigned int virq,unsigned int num_irqs,void * data)2227 static int tegra_pmc_irq_alloc(struct irq_domain *domain, unsigned int virq,
2228 			       unsigned int num_irqs, void *data)
2229 {
2230 	struct tegra_pmc *pmc = domain->host_data;
2231 	const struct tegra_pmc_soc *soc = pmc->soc;
2232 	struct irq_fwspec *fwspec = data;
2233 	unsigned int i;
2234 	int err = 0;
2235 
2236 	if (WARN_ON(num_irqs > 1))
2237 		return -EINVAL;
2238 
2239 	for (i = 0; i < soc->num_wake_events; i++) {
2240 		const struct tegra_wake_event *event = &soc->wake_events[i];
2241 
2242 		/* IRQ and simple wake events */
2243 		if (fwspec->param_count == 2) {
2244 			struct irq_fwspec spec;
2245 
2246 			if (event->id != fwspec->param[0])
2247 				continue;
2248 
2249 			err = irq_domain_set_hwirq_and_chip(domain, virq,
2250 							    event->id,
2251 							    &pmc->irq, pmc);
2252 			if (err < 0)
2253 				break;
2254 
2255 			/* simple hierarchies stop at the PMC level */
2256 			if (event->irq == 0) {
2257 				err = irq_domain_disconnect_hierarchy(domain->parent, virq);
2258 				break;
2259 			}
2260 
2261 			spec.fwnode = &pmc->dev->of_node->fwnode;
2262 			spec.param_count = 3;
2263 			spec.param[0] = GIC_SPI;
2264 			spec.param[1] = event->irq;
2265 			spec.param[2] = fwspec->param[1];
2266 
2267 			err = irq_domain_alloc_irqs_parent(domain, virq,
2268 							   num_irqs, &spec);
2269 
2270 			break;
2271 		}
2272 
2273 		/* GPIO wake events */
2274 		if (fwspec->param_count == 3) {
2275 			if (event->gpio.instance != fwspec->param[0] ||
2276 			    event->gpio.pin != fwspec->param[1])
2277 				continue;
2278 
2279 			err = irq_domain_set_hwirq_and_chip(domain, virq,
2280 							    event->id,
2281 							    &pmc->irq, pmc);
2282 
2283 			/* GPIO hierarchies stop at the PMC level */
2284 			if (!err && domain->parent)
2285 				err = irq_domain_disconnect_hierarchy(domain->parent,
2286 								      virq);
2287 			break;
2288 		}
2289 	}
2290 
2291 	/* If there is no wake-up event, there is no PMC mapping */
2292 	if (i == soc->num_wake_events)
2293 		err = irq_domain_disconnect_hierarchy(domain, virq);
2294 
2295 	return err;
2296 }
2297 
2298 static const struct irq_domain_ops tegra_pmc_irq_domain_ops = {
2299 	.translate = tegra_pmc_irq_translate,
2300 	.alloc = tegra_pmc_irq_alloc,
2301 };
2302 
tegra210_pmc_irq_set_wake(struct irq_data * data,unsigned int on)2303 static int tegra210_pmc_irq_set_wake(struct irq_data *data, unsigned int on)
2304 {
2305 	struct tegra_pmc *pmc = irq_data_get_irq_chip_data(data);
2306 	unsigned int offset, bit;
2307 	u32 value;
2308 
2309 	offset = data->hwirq / 32;
2310 	bit = data->hwirq % 32;
2311 
2312 	/* clear wake status */
2313 	tegra_pmc_writel(pmc, 0, PMC_SW_WAKE_STATUS);
2314 	tegra_pmc_writel(pmc, 0, PMC_SW_WAKE2_STATUS);
2315 
2316 	tegra_pmc_writel(pmc, 0, PMC_WAKE_STATUS);
2317 	tegra_pmc_writel(pmc, 0, PMC_WAKE2_STATUS);
2318 
2319 	/* enable PMC wake */
2320 	if (data->hwirq >= 32)
2321 		offset = PMC_WAKE2_MASK;
2322 	else
2323 		offset = PMC_WAKE_MASK;
2324 
2325 	value = tegra_pmc_readl(pmc, offset);
2326 
2327 	if (on)
2328 		value |= BIT(bit);
2329 	else
2330 		value &= ~BIT(bit);
2331 
2332 	tegra_pmc_writel(pmc, value, offset);
2333 
2334 	return 0;
2335 }
2336 
tegra210_pmc_irq_set_type(struct irq_data * data,unsigned int type)2337 static int tegra210_pmc_irq_set_type(struct irq_data *data, unsigned int type)
2338 {
2339 	struct tegra_pmc *pmc = irq_data_get_irq_chip_data(data);
2340 	unsigned int offset, bit;
2341 	u32 value;
2342 
2343 	offset = data->hwirq / 32;
2344 	bit = data->hwirq % 32;
2345 
2346 	if (data->hwirq >= 32)
2347 		offset = PMC_WAKE2_LEVEL;
2348 	else
2349 		offset = PMC_WAKE_LEVEL;
2350 
2351 	value = tegra_pmc_readl(pmc, offset);
2352 
2353 	switch (type) {
2354 	case IRQ_TYPE_EDGE_RISING:
2355 	case IRQ_TYPE_LEVEL_HIGH:
2356 		value |= BIT(bit);
2357 		break;
2358 
2359 	case IRQ_TYPE_EDGE_FALLING:
2360 	case IRQ_TYPE_LEVEL_LOW:
2361 		value &= ~BIT(bit);
2362 		break;
2363 
2364 	case IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING:
2365 		value ^= BIT(bit);
2366 		break;
2367 
2368 	default:
2369 		return -EINVAL;
2370 	}
2371 
2372 	tegra_pmc_writel(pmc, value, offset);
2373 
2374 	return 0;
2375 }
2376 
tegra186_pmc_set_wake_filters(struct tegra_pmc * pmc)2377 static void tegra186_pmc_set_wake_filters(struct tegra_pmc *pmc)
2378 {
2379 	u32 value;
2380 
2381 	/* SW Wake (wake83) needs SR_CAPTURE filter to be enabled */
2382 	value = readl(pmc->wake + WAKE_AOWAKE_CNTRL(SW_WAKE_ID));
2383 	value |= WAKE_AOWAKE_CNTRL_SR_CAPTURE_EN;
2384 	writel(value, pmc->wake + WAKE_AOWAKE_CNTRL(SW_WAKE_ID));
2385 	dev_dbg(pmc->dev, "WAKE_AOWAKE_CNTRL_83 = 0x%x\n", value);
2386 }
2387 
tegra186_pmc_irq_set_wake(struct irq_data * data,unsigned int on)2388 static int tegra186_pmc_irq_set_wake(struct irq_data *data, unsigned int on)
2389 {
2390 	struct tegra_pmc *pmc = irq_data_get_irq_chip_data(data);
2391 	unsigned int offset, bit;
2392 	u32 value;
2393 
2394 	offset = data->hwirq / 32;
2395 	bit = data->hwirq % 32;
2396 
2397 	/* clear wake status */
2398 	writel(0x1, pmc->wake + WAKE_AOWAKE_STATUS_W(data->hwirq));
2399 
2400 	/* route wake to tier 2 */
2401 	value = readl(pmc->wake + WAKE_AOWAKE_TIER2_ROUTING(offset));
2402 
2403 	if (!on)
2404 		value &= ~(1 << bit);
2405 	else
2406 		value |= 1 << bit;
2407 
2408 	writel(value, pmc->wake + WAKE_AOWAKE_TIER2_ROUTING(offset));
2409 
2410 	/* enable wakeup event */
2411 	writel(!!on, pmc->wake + WAKE_AOWAKE_MASK_W(data->hwirq));
2412 
2413 	return 0;
2414 }
2415 
tegra186_pmc_irq_set_type(struct irq_data * data,unsigned int type)2416 static int tegra186_pmc_irq_set_type(struct irq_data *data, unsigned int type)
2417 {
2418 	struct tegra_pmc *pmc = irq_data_get_irq_chip_data(data);
2419 	u32 value;
2420 
2421 	value = readl(pmc->wake + WAKE_AOWAKE_CNTRL(data->hwirq));
2422 
2423 	switch (type) {
2424 	case IRQ_TYPE_EDGE_RISING:
2425 	case IRQ_TYPE_LEVEL_HIGH:
2426 		value |= WAKE_AOWAKE_CNTRL_LEVEL;
2427 		set_bit(data->hwirq, pmc->wake_type_level_map);
2428 		clear_bit(data->hwirq, pmc->wake_type_dual_edge_map);
2429 		break;
2430 
2431 	case IRQ_TYPE_EDGE_FALLING:
2432 	case IRQ_TYPE_LEVEL_LOW:
2433 		value &= ~WAKE_AOWAKE_CNTRL_LEVEL;
2434 		clear_bit(data->hwirq, pmc->wake_type_level_map);
2435 		clear_bit(data->hwirq, pmc->wake_type_dual_edge_map);
2436 		break;
2437 
2438 	case IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING:
2439 		value ^= WAKE_AOWAKE_CNTRL_LEVEL;
2440 		clear_bit(data->hwirq, pmc->wake_type_level_map);
2441 		set_bit(data->hwirq, pmc->wake_type_dual_edge_map);
2442 		break;
2443 
2444 	default:
2445 		return -EINVAL;
2446 	}
2447 
2448 	writel(value, pmc->wake + WAKE_AOWAKE_CNTRL(data->hwirq));
2449 
2450 	return 0;
2451 }
2452 
tegra_irq_mask_parent(struct irq_data * data)2453 static void tegra_irq_mask_parent(struct irq_data *data)
2454 {
2455 	if (data->parent_data)
2456 		irq_chip_mask_parent(data);
2457 }
2458 
tegra_irq_unmask_parent(struct irq_data * data)2459 static void tegra_irq_unmask_parent(struct irq_data *data)
2460 {
2461 	if (data->parent_data)
2462 		irq_chip_unmask_parent(data);
2463 }
2464 
tegra_irq_eoi_parent(struct irq_data * data)2465 static void tegra_irq_eoi_parent(struct irq_data *data)
2466 {
2467 	if (data->parent_data)
2468 		irq_chip_eoi_parent(data);
2469 }
2470 
tegra_irq_set_affinity_parent(struct irq_data * data,const struct cpumask * dest,bool force)2471 static int tegra_irq_set_affinity_parent(struct irq_data *data,
2472 					 const struct cpumask *dest,
2473 					 bool force)
2474 {
2475 	if (data->parent_data)
2476 		return irq_chip_set_affinity_parent(data, dest, force);
2477 
2478 	return -EINVAL;
2479 }
2480 
tegra_pmc_irq_init(struct tegra_pmc * pmc)2481 static int tegra_pmc_irq_init(struct tegra_pmc *pmc)
2482 {
2483 	struct irq_domain *parent = NULL;
2484 	struct device_node *np;
2485 
2486 	np = of_irq_find_parent(pmc->dev->of_node);
2487 	if (np) {
2488 		parent = irq_find_host(np);
2489 		of_node_put(np);
2490 	}
2491 
2492 	if (!parent)
2493 		return 0;
2494 
2495 	pmc->irq.name = dev_name(pmc->dev);
2496 	pmc->irq.irq_mask = tegra_irq_mask_parent;
2497 	pmc->irq.irq_unmask = tegra_irq_unmask_parent;
2498 	pmc->irq.irq_eoi = tegra_irq_eoi_parent;
2499 	pmc->irq.irq_set_affinity = tegra_irq_set_affinity_parent;
2500 	pmc->irq.irq_set_type = pmc->soc->irq_set_type;
2501 	pmc->irq.irq_set_wake = pmc->soc->irq_set_wake;
2502 
2503 	pmc->domain = irq_domain_add_hierarchy(parent, 0, 96, pmc->dev->of_node,
2504 					       &tegra_pmc_irq_domain_ops, pmc);
2505 	if (!pmc->domain) {
2506 		dev_err(pmc->dev, "failed to allocate domain\n");
2507 		return -ENOMEM;
2508 	}
2509 
2510 	return 0;
2511 }
2512 
tegra_pmc_clk_notify_cb(struct notifier_block * nb,unsigned long action,void * ptr)2513 static int tegra_pmc_clk_notify_cb(struct notifier_block *nb,
2514 				   unsigned long action, void *ptr)
2515 {
2516 	struct tegra_pmc *pmc = container_of(nb, struct tegra_pmc, clk_nb);
2517 	struct clk_notifier_data *data = ptr;
2518 
2519 	switch (action) {
2520 	case PRE_RATE_CHANGE:
2521 		mutex_lock(&pmc->powergates_lock);
2522 		break;
2523 
2524 	case POST_RATE_CHANGE:
2525 		pmc->rate = data->new_rate;
2526 		fallthrough;
2527 
2528 	case ABORT_RATE_CHANGE:
2529 		mutex_unlock(&pmc->powergates_lock);
2530 		break;
2531 
2532 	default:
2533 		WARN_ON_ONCE(1);
2534 		return notifier_from_errno(-EINVAL);
2535 	}
2536 
2537 	return NOTIFY_OK;
2538 }
2539 
pmc_clk_fence_udelay(u32 offset)2540 static void pmc_clk_fence_udelay(u32 offset)
2541 {
2542 	tegra_pmc_readl(pmc, offset);
2543 	/* pmc clk propagation delay 2 us */
2544 	udelay(2);
2545 }
2546 
pmc_clk_mux_get_parent(struct clk_hw * hw)2547 static u8 pmc_clk_mux_get_parent(struct clk_hw *hw)
2548 {
2549 	struct pmc_clk *clk = to_pmc_clk(hw);
2550 	u32 val;
2551 
2552 	val = tegra_pmc_readl(pmc, clk->offs) >> clk->mux_shift;
2553 	val &= PMC_CLK_OUT_MUX_MASK;
2554 
2555 	return val;
2556 }
2557 
pmc_clk_mux_set_parent(struct clk_hw * hw,u8 index)2558 static int pmc_clk_mux_set_parent(struct clk_hw *hw, u8 index)
2559 {
2560 	struct pmc_clk *clk = to_pmc_clk(hw);
2561 	u32 val;
2562 
2563 	val = tegra_pmc_readl(pmc, clk->offs);
2564 	val &= ~(PMC_CLK_OUT_MUX_MASK << clk->mux_shift);
2565 	val |= index << clk->mux_shift;
2566 	tegra_pmc_writel(pmc, val, clk->offs);
2567 	pmc_clk_fence_udelay(clk->offs);
2568 
2569 	return 0;
2570 }
2571 
pmc_clk_is_enabled(struct clk_hw * hw)2572 static int pmc_clk_is_enabled(struct clk_hw *hw)
2573 {
2574 	struct pmc_clk *clk = to_pmc_clk(hw);
2575 	u32 val;
2576 
2577 	val = tegra_pmc_readl(pmc, clk->offs) & BIT(clk->force_en_shift);
2578 
2579 	return val ? 1 : 0;
2580 }
2581 
pmc_clk_set_state(unsigned long offs,u32 shift,int state)2582 static void pmc_clk_set_state(unsigned long offs, u32 shift, int state)
2583 {
2584 	u32 val;
2585 
2586 	val = tegra_pmc_readl(pmc, offs);
2587 	val = state ? (val | BIT(shift)) : (val & ~BIT(shift));
2588 	tegra_pmc_writel(pmc, val, offs);
2589 	pmc_clk_fence_udelay(offs);
2590 }
2591 
pmc_clk_enable(struct clk_hw * hw)2592 static int pmc_clk_enable(struct clk_hw *hw)
2593 {
2594 	struct pmc_clk *clk = to_pmc_clk(hw);
2595 
2596 	pmc_clk_set_state(clk->offs, clk->force_en_shift, 1);
2597 
2598 	return 0;
2599 }
2600 
pmc_clk_disable(struct clk_hw * hw)2601 static void pmc_clk_disable(struct clk_hw *hw)
2602 {
2603 	struct pmc_clk *clk = to_pmc_clk(hw);
2604 
2605 	pmc_clk_set_state(clk->offs, clk->force_en_shift, 0);
2606 }
2607 
2608 static const struct clk_ops pmc_clk_ops = {
2609 	.get_parent = pmc_clk_mux_get_parent,
2610 	.set_parent = pmc_clk_mux_set_parent,
2611 	.determine_rate = __clk_mux_determine_rate,
2612 	.is_enabled = pmc_clk_is_enabled,
2613 	.enable = pmc_clk_enable,
2614 	.disable = pmc_clk_disable,
2615 };
2616 
2617 static struct clk *
tegra_pmc_clk_out_register(struct tegra_pmc * pmc,const struct pmc_clk_init_data * data,unsigned long offset)2618 tegra_pmc_clk_out_register(struct tegra_pmc *pmc,
2619 			   const struct pmc_clk_init_data *data,
2620 			   unsigned long offset)
2621 {
2622 	struct clk_init_data init;
2623 	struct pmc_clk *pmc_clk;
2624 
2625 	pmc_clk = devm_kzalloc(pmc->dev, sizeof(*pmc_clk), GFP_KERNEL);
2626 	if (!pmc_clk)
2627 		return ERR_PTR(-ENOMEM);
2628 
2629 	init.name = data->name;
2630 	init.ops = &pmc_clk_ops;
2631 	init.parent_names = data->parents;
2632 	init.num_parents = data->num_parents;
2633 	init.flags = CLK_SET_RATE_NO_REPARENT | CLK_SET_RATE_PARENT |
2634 		     CLK_SET_PARENT_GATE;
2635 
2636 	pmc_clk->hw.init = &init;
2637 	pmc_clk->offs = offset;
2638 	pmc_clk->mux_shift = data->mux_shift;
2639 	pmc_clk->force_en_shift = data->force_en_shift;
2640 
2641 	return clk_register(NULL, &pmc_clk->hw);
2642 }
2643 
pmc_clk_gate_is_enabled(struct clk_hw * hw)2644 static int pmc_clk_gate_is_enabled(struct clk_hw *hw)
2645 {
2646 	struct pmc_clk_gate *gate = to_pmc_clk_gate(hw);
2647 
2648 	return tegra_pmc_readl(pmc, gate->offs) & BIT(gate->shift) ? 1 : 0;
2649 }
2650 
pmc_clk_gate_enable(struct clk_hw * hw)2651 static int pmc_clk_gate_enable(struct clk_hw *hw)
2652 {
2653 	struct pmc_clk_gate *gate = to_pmc_clk_gate(hw);
2654 
2655 	pmc_clk_set_state(gate->offs, gate->shift, 1);
2656 
2657 	return 0;
2658 }
2659 
pmc_clk_gate_disable(struct clk_hw * hw)2660 static void pmc_clk_gate_disable(struct clk_hw *hw)
2661 {
2662 	struct pmc_clk_gate *gate = to_pmc_clk_gate(hw);
2663 
2664 	pmc_clk_set_state(gate->offs, gate->shift, 0);
2665 }
2666 
2667 static const struct clk_ops pmc_clk_gate_ops = {
2668 	.is_enabled = pmc_clk_gate_is_enabled,
2669 	.enable = pmc_clk_gate_enable,
2670 	.disable = pmc_clk_gate_disable,
2671 };
2672 
2673 static struct clk *
tegra_pmc_clk_gate_register(struct tegra_pmc * pmc,const char * name,const char * parent_name,unsigned long offset,u32 shift)2674 tegra_pmc_clk_gate_register(struct tegra_pmc *pmc, const char *name,
2675 			    const char *parent_name, unsigned long offset,
2676 			    u32 shift)
2677 {
2678 	struct clk_init_data init;
2679 	struct pmc_clk_gate *gate;
2680 
2681 	gate = devm_kzalloc(pmc->dev, sizeof(*gate), GFP_KERNEL);
2682 	if (!gate)
2683 		return ERR_PTR(-ENOMEM);
2684 
2685 	init.name = name;
2686 	init.ops = &pmc_clk_gate_ops;
2687 	init.parent_names = &parent_name;
2688 	init.num_parents = 1;
2689 	init.flags = 0;
2690 
2691 	gate->hw.init = &init;
2692 	gate->offs = offset;
2693 	gate->shift = shift;
2694 
2695 	return clk_register(NULL, &gate->hw);
2696 }
2697 
tegra_pmc_clock_register(struct tegra_pmc * pmc,struct device_node * np)2698 static void tegra_pmc_clock_register(struct tegra_pmc *pmc,
2699 				     struct device_node *np)
2700 {
2701 	struct clk *clk;
2702 	struct clk_onecell_data *clk_data;
2703 	unsigned int num_clks;
2704 	int i, err;
2705 
2706 	num_clks = pmc->soc->num_pmc_clks;
2707 	if (pmc->soc->has_blink_output)
2708 		num_clks += 1;
2709 
2710 	if (!num_clks)
2711 		return;
2712 
2713 	clk_data = devm_kmalloc(pmc->dev, sizeof(*clk_data), GFP_KERNEL);
2714 	if (!clk_data)
2715 		return;
2716 
2717 	clk_data->clks = devm_kcalloc(pmc->dev, TEGRA_PMC_CLK_MAX,
2718 				      sizeof(*clk_data->clks), GFP_KERNEL);
2719 	if (!clk_data->clks)
2720 		return;
2721 
2722 	clk_data->clk_num = TEGRA_PMC_CLK_MAX;
2723 
2724 	for (i = 0; i < TEGRA_PMC_CLK_MAX; i++)
2725 		clk_data->clks[i] = ERR_PTR(-ENOENT);
2726 
2727 	for (i = 0; i < pmc->soc->num_pmc_clks; i++) {
2728 		const struct pmc_clk_init_data *data;
2729 
2730 		data = pmc->soc->pmc_clks_data + i;
2731 
2732 		clk = tegra_pmc_clk_out_register(pmc, data, PMC_CLK_OUT_CNTRL);
2733 		if (IS_ERR(clk)) {
2734 			dev_warn(pmc->dev, "unable to register clock %s: %d\n",
2735 				 data->name, PTR_ERR_OR_ZERO(clk));
2736 			return;
2737 		}
2738 
2739 		err = clk_register_clkdev(clk, data->name, NULL);
2740 		if (err) {
2741 			dev_warn(pmc->dev,
2742 				 "unable to register %s clock lookup: %d\n",
2743 				 data->name, err);
2744 			return;
2745 		}
2746 
2747 		clk_data->clks[data->clk_id] = clk;
2748 	}
2749 
2750 	if (pmc->soc->has_blink_output) {
2751 		tegra_pmc_writel(pmc, 0x0, PMC_BLINK_TIMER);
2752 		clk = tegra_pmc_clk_gate_register(pmc,
2753 						  "pmc_blink_override",
2754 						  "clk_32k",
2755 						  PMC_DPD_PADS_ORIDE,
2756 						  PMC_DPD_PADS_ORIDE_BLINK);
2757 		if (IS_ERR(clk)) {
2758 			dev_warn(pmc->dev,
2759 				 "unable to register pmc_blink_override: %d\n",
2760 				 PTR_ERR_OR_ZERO(clk));
2761 			return;
2762 		}
2763 
2764 		clk = tegra_pmc_clk_gate_register(pmc, "pmc_blink",
2765 						  "pmc_blink_override",
2766 						  PMC_CNTRL,
2767 						  PMC_CNTRL_BLINK_EN);
2768 		if (IS_ERR(clk)) {
2769 			dev_warn(pmc->dev,
2770 				 "unable to register pmc_blink: %d\n",
2771 				 PTR_ERR_OR_ZERO(clk));
2772 			return;
2773 		}
2774 
2775 		err = clk_register_clkdev(clk, "pmc_blink", NULL);
2776 		if (err) {
2777 			dev_warn(pmc->dev,
2778 				 "unable to register pmc_blink lookup: %d\n",
2779 				 err);
2780 			return;
2781 		}
2782 
2783 		clk_data->clks[TEGRA_PMC_CLK_BLINK] = clk;
2784 	}
2785 
2786 	err = of_clk_add_provider(np, of_clk_src_onecell_get, clk_data);
2787 	if (err)
2788 		dev_warn(pmc->dev, "failed to add pmc clock provider: %d\n",
2789 			 err);
2790 }
2791 
2792 static const struct regmap_range pmc_usb_sleepwalk_ranges[] = {
2793 	regmap_reg_range(PMC_USB_DEBOUNCE_DEL, PMC_USB_AO),
2794 	regmap_reg_range(PMC_UTMIP_UHSIC_TRIGGERS, PMC_UTMIP_UHSIC_SAVED_STATE),
2795 	regmap_reg_range(PMC_UTMIP_TERM_PAD_CFG, PMC_UTMIP_UHSIC_FAKE),
2796 	regmap_reg_range(PMC_UTMIP_UHSIC_LINE_WAKEUP, PMC_UTMIP_UHSIC_LINE_WAKEUP),
2797 	regmap_reg_range(PMC_UTMIP_BIAS_MASTER_CNTRL, PMC_UTMIP_MASTER_CONFIG),
2798 	regmap_reg_range(PMC_UTMIP_UHSIC2_TRIGGERS, PMC_UTMIP_MASTER2_CONFIG),
2799 	regmap_reg_range(PMC_UTMIP_PAD_CFG0, PMC_UTMIP_UHSIC_SLEEP_CFG1),
2800 	regmap_reg_range(PMC_UTMIP_SLEEPWALK_P3, PMC_UTMIP_SLEEPWALK_P3),
2801 };
2802 
2803 static const struct regmap_access_table pmc_usb_sleepwalk_table = {
2804 	.yes_ranges = pmc_usb_sleepwalk_ranges,
2805 	.n_yes_ranges = ARRAY_SIZE(pmc_usb_sleepwalk_ranges),
2806 };
2807 
tegra_pmc_regmap_readl(void * context,unsigned int offset,unsigned int * value)2808 static int tegra_pmc_regmap_readl(void *context, unsigned int offset, unsigned int *value)
2809 {
2810 	struct tegra_pmc *pmc = context;
2811 
2812 	*value = tegra_pmc_readl(pmc, offset);
2813 	return 0;
2814 }
2815 
tegra_pmc_regmap_writel(void * context,unsigned int offset,unsigned int value)2816 static int tegra_pmc_regmap_writel(void *context, unsigned int offset, unsigned int value)
2817 {
2818 	struct tegra_pmc *pmc = context;
2819 
2820 	tegra_pmc_writel(pmc, value, offset);
2821 	return 0;
2822 }
2823 
2824 static const struct regmap_config usb_sleepwalk_regmap_config = {
2825 	.name = "usb_sleepwalk",
2826 	.reg_bits = 32,
2827 	.val_bits = 32,
2828 	.reg_stride = 4,
2829 	.fast_io = true,
2830 	.rd_table = &pmc_usb_sleepwalk_table,
2831 	.wr_table = &pmc_usb_sleepwalk_table,
2832 	.reg_read = tegra_pmc_regmap_readl,
2833 	.reg_write = tegra_pmc_regmap_writel,
2834 };
2835 
tegra_pmc_regmap_init(struct tegra_pmc * pmc)2836 static int tegra_pmc_regmap_init(struct tegra_pmc *pmc)
2837 {
2838 	struct regmap *regmap;
2839 	int err;
2840 
2841 	if (pmc->soc->has_usb_sleepwalk) {
2842 		regmap = devm_regmap_init(pmc->dev, NULL, pmc, &usb_sleepwalk_regmap_config);
2843 		if (IS_ERR(regmap)) {
2844 			err = PTR_ERR(regmap);
2845 			dev_err(pmc->dev, "failed to allocate register map (%d)\n", err);
2846 			return err;
2847 		}
2848 	}
2849 
2850 	return 0;
2851 }
2852 
tegra_pmc_reset_suspend_mode(void * data)2853 static void tegra_pmc_reset_suspend_mode(void *data)
2854 {
2855 	pmc->suspend_mode = TEGRA_SUSPEND_NOT_READY;
2856 }
2857 
tegra_pmc_probe(struct platform_device * pdev)2858 static int tegra_pmc_probe(struct platform_device *pdev)
2859 {
2860 	void __iomem *base;
2861 	struct resource *res;
2862 	int err;
2863 
2864 	/*
2865 	 * Early initialisation should have configured an initial
2866 	 * register mapping and setup the soc data pointer. If these
2867 	 * are not valid then something went badly wrong!
2868 	 */
2869 	if (WARN_ON(!pmc->base || !pmc->soc))
2870 		return -ENODEV;
2871 
2872 	err = tegra_pmc_parse_dt(pmc, pdev->dev.of_node);
2873 	if (err < 0)
2874 		return err;
2875 
2876 	err = devm_add_action_or_reset(&pdev->dev, tegra_pmc_reset_suspend_mode,
2877 				       NULL);
2878 	if (err)
2879 		return err;
2880 
2881 	/* take over the memory region from the early initialization */
2882 	base = devm_platform_ioremap_resource(pdev, 0);
2883 	if (IS_ERR(base))
2884 		return PTR_ERR(base);
2885 
2886 	if (pmc->soc->has_single_mmio_aperture) {
2887 		pmc->wake = base;
2888 		pmc->aotag = base;
2889 		pmc->scratch = base;
2890 	} else {
2891 		pmc->wake = devm_platform_ioremap_resource_byname(pdev, "wake");
2892 		if (IS_ERR(pmc->wake))
2893 			return PTR_ERR(pmc->wake);
2894 
2895 		pmc->aotag = devm_platform_ioremap_resource_byname(pdev, "aotag");
2896 		if (IS_ERR(pmc->aotag))
2897 			return PTR_ERR(pmc->aotag);
2898 
2899 		/* "scratch" is an optional aperture */
2900 		res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
2901 						"scratch");
2902 		if (res) {
2903 			pmc->scratch = devm_ioremap_resource(&pdev->dev, res);
2904 			if (IS_ERR(pmc->scratch))
2905 				return PTR_ERR(pmc->scratch);
2906 		} else {
2907 			pmc->scratch = NULL;
2908 		}
2909 	}
2910 
2911 	pmc->clk = devm_clk_get_optional(&pdev->dev, "pclk");
2912 	if (IS_ERR(pmc->clk))
2913 		return dev_err_probe(&pdev->dev, PTR_ERR(pmc->clk),
2914 				     "failed to get pclk\n");
2915 
2916 	/*
2917 	 * PMC should be last resort for restarting since it soft-resets
2918 	 * CPU without resetting everything else.
2919 	 */
2920 	if (pmc->scratch) {
2921 		err = devm_register_reboot_notifier(&pdev->dev,
2922 						    &tegra_pmc_reboot_notifier);
2923 		if (err) {
2924 			dev_err(&pdev->dev,
2925 				"unable to register reboot notifier, %d\n",
2926 				err);
2927 			return err;
2928 		}
2929 	}
2930 
2931 	err = devm_register_sys_off_handler(&pdev->dev,
2932 					    SYS_OFF_MODE_RESTART,
2933 					    SYS_OFF_PRIO_LOW,
2934 					    tegra_pmc_restart_handler, NULL);
2935 	if (err) {
2936 		dev_err(&pdev->dev, "failed to register sys-off handler: %d\n",
2937 			err);
2938 		return err;
2939 	}
2940 
2941 	/*
2942 	 * PMC should be primary power-off method if it soft-resets CPU,
2943 	 * asking bootloader to shutdown hardware.
2944 	 */
2945 	err = devm_register_sys_off_handler(&pdev->dev,
2946 					    SYS_OFF_MODE_POWER_OFF,
2947 					    SYS_OFF_PRIO_FIRMWARE,
2948 					    tegra_pmc_power_off_handler, NULL);
2949 	if (err) {
2950 		dev_err(&pdev->dev, "failed to register sys-off handler: %d\n",
2951 			err);
2952 		return err;
2953 	}
2954 
2955 	/*
2956 	 * PCLK clock rate can't be retrieved using CLK API because it
2957 	 * causes lockup if CPU enters LP2 idle state from some other
2958 	 * CLK notifier, hence we're caching the rate's value locally.
2959 	 */
2960 	if (pmc->clk) {
2961 		pmc->clk_nb.notifier_call = tegra_pmc_clk_notify_cb;
2962 		err = devm_clk_notifier_register(&pdev->dev, pmc->clk,
2963 						 &pmc->clk_nb);
2964 		if (err) {
2965 			dev_err(&pdev->dev,
2966 				"failed to register clk notifier\n");
2967 			return err;
2968 		}
2969 
2970 		pmc->rate = clk_get_rate(pmc->clk);
2971 	}
2972 
2973 	pmc->dev = &pdev->dev;
2974 
2975 	err = tegra_pmc_init(pmc);
2976 	if (err < 0) {
2977 		dev_err(&pdev->dev, "failed to initialize PMC: %d\n", err);
2978 		return err;
2979 	}
2980 
2981 	tegra_pmc_init_tsense_reset(pmc);
2982 
2983 	tegra_pmc_reset_sysfs_init(pmc);
2984 
2985 	err = tegra_pmc_pinctrl_init(pmc);
2986 	if (err)
2987 		goto cleanup_sysfs;
2988 
2989 	err = tegra_pmc_regmap_init(pmc);
2990 	if (err < 0)
2991 		goto cleanup_sysfs;
2992 
2993 	err = tegra_powergate_init(pmc, pdev->dev.of_node);
2994 	if (err < 0)
2995 		goto cleanup_powergates;
2996 
2997 	err = tegra_pmc_irq_init(pmc);
2998 	if (err < 0)
2999 		goto cleanup_powergates;
3000 
3001 	mutex_lock(&pmc->powergates_lock);
3002 	iounmap(pmc->base);
3003 	pmc->base = base;
3004 	mutex_unlock(&pmc->powergates_lock);
3005 
3006 	tegra_pmc_clock_register(pmc, pdev->dev.of_node);
3007 	platform_set_drvdata(pdev, pmc);
3008 	tegra_pm_init_suspend();
3009 
3010 	/* Some wakes require specific filter configuration */
3011 	if (pmc->soc->set_wake_filters)
3012 		pmc->soc->set_wake_filters(pmc);
3013 
3014 	debugfs_create_file("powergate", 0444, NULL, NULL, &powergate_fops);
3015 
3016 	return 0;
3017 
3018 cleanup_powergates:
3019 	tegra_powergate_remove_all(pdev->dev.of_node);
3020 cleanup_sysfs:
3021 	device_remove_file(&pdev->dev, &dev_attr_reset_reason);
3022 	device_remove_file(&pdev->dev, &dev_attr_reset_level);
3023 
3024 	return err;
3025 }
3026 
3027 /*
3028  * Ensures that sufficient time is passed for a register write to
3029  * serialize into the 32KHz domain.
3030  */
wke_32kwritel(struct tegra_pmc * pmc,u32 value,unsigned int offset)3031 static void wke_32kwritel(struct tegra_pmc *pmc, u32 value, unsigned int offset)
3032 {
3033 	writel(value, pmc->wake + offset);
3034 	udelay(130);
3035 }
3036 
wke_write_wake_level(struct tegra_pmc * pmc,int wake,int level)3037 static void wke_write_wake_level(struct tegra_pmc *pmc, int wake, int level)
3038 {
3039 	unsigned int offset = WAKE_AOWAKE_CNTRL(wake);
3040 	u32 value;
3041 
3042 	value = readl(pmc->wake + offset);
3043 	if (level)
3044 		value |= WAKE_AOWAKE_CNTRL_LEVEL;
3045 	else
3046 		value &= ~WAKE_AOWAKE_CNTRL_LEVEL;
3047 
3048 	writel(value, pmc->wake + offset);
3049 }
3050 
wke_write_wake_levels(struct tegra_pmc * pmc)3051 static void wke_write_wake_levels(struct tegra_pmc *pmc)
3052 {
3053 	unsigned int i;
3054 
3055 	for (i = 0; i < pmc->soc->max_wake_events; i++)
3056 		wke_write_wake_level(pmc, i, test_bit(i, pmc->wake_cntrl_level_map));
3057 }
3058 
wke_clear_sw_wake_status(struct tegra_pmc * pmc)3059 static void wke_clear_sw_wake_status(struct tegra_pmc *pmc)
3060 {
3061 	wke_32kwritel(pmc, 1, WAKE_AOWAKE_SW_STATUS_W_0);
3062 }
3063 
wke_read_sw_wake_status(struct tegra_pmc * pmc)3064 static void wke_read_sw_wake_status(struct tegra_pmc *pmc)
3065 {
3066 	unsigned long status;
3067 	unsigned int wake, i;
3068 
3069 	for (i = 0; i < pmc->soc->max_wake_events; i++)
3070 		wke_write_wake_level(pmc, i, 0);
3071 
3072 	wke_clear_sw_wake_status(pmc);
3073 
3074 	wke_32kwritel(pmc, 1, WAKE_LATCH_SW);
3075 
3076 	/*
3077 	 * WAKE_AOWAKE_SW_STATUS is edge triggered, so in order to
3078 	 * obtain the current status of the input wake signals, change
3079 	 * the polarity of the wake level from 0->1 while latching to force
3080 	 * a positive edge if the sampled signal is '1'.
3081 	 */
3082 	for (i = 0; i < pmc->soc->max_wake_events; i++)
3083 		wke_write_wake_level(pmc, i, 1);
3084 
3085 	/*
3086 	 * Wait for the update to be synced into the 32kHz domain,
3087 	 * and let enough time lapse, so that the wake signals have time to
3088 	 * be sampled.
3089 	 */
3090 	udelay(300);
3091 
3092 	wke_32kwritel(pmc, 0, WAKE_LATCH_SW);
3093 
3094 	bitmap_zero(pmc->wake_sw_status_map, pmc->soc->max_wake_events);
3095 
3096 	for (i = 0; i < pmc->soc->max_wake_vectors; i++) {
3097 		status = readl(pmc->wake + WAKE_AOWAKE_SW_STATUS(i));
3098 
3099 		for_each_set_bit(wake, &status, 32)
3100 			set_bit(wake + (i * 32), pmc->wake_sw_status_map);
3101 	}
3102 }
3103 
wke_clear_wake_status(struct tegra_pmc * pmc)3104 static void wke_clear_wake_status(struct tegra_pmc *pmc)
3105 {
3106 	unsigned long status;
3107 	unsigned int i, wake;
3108 	u32 mask;
3109 
3110 	for (i = 0; i < pmc->soc->max_wake_vectors; i++) {
3111 		mask = readl(pmc->wake + WAKE_AOWAKE_TIER2_ROUTING(i));
3112 		status = readl(pmc->wake + WAKE_AOWAKE_STATUS_R(i)) & mask;
3113 
3114 		for_each_set_bit(wake, &status, 32)
3115 			wke_32kwritel(pmc, 0x1, WAKE_AOWAKE_STATUS_W((i * 32) + wake));
3116 	}
3117 }
3118 
3119 /* translate sc7 wake sources back into IRQs to catch edge triggered wakeups */
tegra186_pmc_process_wake_events(struct tegra_pmc * pmc,unsigned int index,unsigned long status)3120 static void tegra186_pmc_process_wake_events(struct tegra_pmc *pmc, unsigned int index,
3121 					     unsigned long status)
3122 {
3123 	unsigned int wake;
3124 
3125 	dev_dbg(pmc->dev, "Wake[%d:%d]  status=%#lx\n", (index * 32) + 31, index * 32, status);
3126 
3127 	for_each_set_bit(wake, &status, 32) {
3128 		irq_hw_number_t hwirq = wake + 32 * index;
3129 		struct irq_desc *desc;
3130 		unsigned int irq;
3131 
3132 		irq = irq_find_mapping(pmc->domain, hwirq);
3133 
3134 		desc = irq_to_desc(irq);
3135 		if (!desc || !desc->action || !desc->action->name) {
3136 			dev_dbg(pmc->dev, "Resume caused by WAKE%ld, IRQ %d\n", hwirq, irq);
3137 			continue;
3138 		}
3139 
3140 		dev_dbg(pmc->dev, "Resume caused by WAKE%ld, %s\n", hwirq, desc->action->name);
3141 		generic_handle_irq(irq);
3142 	}
3143 }
3144 
tegra186_pmc_wake_syscore_resume(void)3145 static void tegra186_pmc_wake_syscore_resume(void)
3146 {
3147 	u32 status, mask;
3148 	unsigned int i;
3149 
3150 	for (i = 0; i < pmc->soc->max_wake_vectors; i++) {
3151 		mask = readl(pmc->wake + WAKE_AOWAKE_TIER2_ROUTING(i));
3152 		status = readl(pmc->wake + WAKE_AOWAKE_STATUS_R(i)) & mask;
3153 
3154 		tegra186_pmc_process_wake_events(pmc, i, status);
3155 	}
3156 }
3157 
tegra186_pmc_wake_syscore_suspend(void)3158 static int tegra186_pmc_wake_syscore_suspend(void)
3159 {
3160 	wke_read_sw_wake_status(pmc);
3161 
3162 	/* flip the wakeup trigger for dual-edge triggered pads
3163 	 * which are currently asserting as wakeups
3164 	 */
3165 	bitmap_andnot(pmc->wake_cntrl_level_map, pmc->wake_type_dual_edge_map,
3166 		      pmc->wake_sw_status_map, pmc->soc->max_wake_events);
3167 	bitmap_or(pmc->wake_cntrl_level_map, pmc->wake_cntrl_level_map,
3168 		  pmc->wake_type_level_map, pmc->soc->max_wake_events);
3169 
3170 	/* Clear PMC Wake Status registers while going to suspend */
3171 	wke_clear_wake_status(pmc);
3172 	wke_write_wake_levels(pmc);
3173 
3174 	return 0;
3175 }
3176 
3177 #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM)
tegra_pmc_suspend(struct device * dev)3178 static int tegra_pmc_suspend(struct device *dev)
3179 {
3180 	struct tegra_pmc *pmc = dev_get_drvdata(dev);
3181 
3182 	tegra_pmc_writel(pmc, virt_to_phys(tegra_resume), PMC_SCRATCH41);
3183 
3184 	return 0;
3185 }
3186 
tegra_pmc_resume(struct device * dev)3187 static int tegra_pmc_resume(struct device *dev)
3188 {
3189 	struct tegra_pmc *pmc = dev_get_drvdata(dev);
3190 
3191 	tegra_pmc_writel(pmc, 0x0, PMC_SCRATCH41);
3192 
3193 	return 0;
3194 }
3195 
3196 static SIMPLE_DEV_PM_OPS(tegra_pmc_pm_ops, tegra_pmc_suspend, tegra_pmc_resume);
3197 
3198 #endif
3199 
3200 static const char * const tegra20_powergates[] = {
3201 	[TEGRA_POWERGATE_CPU] = "cpu",
3202 	[TEGRA_POWERGATE_3D] = "td",
3203 	[TEGRA_POWERGATE_VENC] = "venc",
3204 	[TEGRA_POWERGATE_VDEC] = "vdec",
3205 	[TEGRA_POWERGATE_PCIE] = "pcie",
3206 	[TEGRA_POWERGATE_L2] = "l2",
3207 	[TEGRA_POWERGATE_MPE] = "mpe",
3208 };
3209 
3210 static const struct tegra_pmc_regs tegra20_pmc_regs = {
3211 	.scratch0 = 0x50,
3212 	.rst_status = 0x1b4,
3213 	.rst_source_shift = 0x0,
3214 	.rst_source_mask = 0x7,
3215 	.rst_level_shift = 0x0,
3216 	.rst_level_mask = 0x0,
3217 };
3218 
tegra20_pmc_init(struct tegra_pmc * pmc)3219 static void tegra20_pmc_init(struct tegra_pmc *pmc)
3220 {
3221 	u32 value, osc, pmu, off;
3222 
3223 	/* Always enable CPU power request */
3224 	value = tegra_pmc_readl(pmc, PMC_CNTRL);
3225 	value |= PMC_CNTRL_CPU_PWRREQ_OE;
3226 	tegra_pmc_writel(pmc, value, PMC_CNTRL);
3227 
3228 	value = tegra_pmc_readl(pmc, PMC_CNTRL);
3229 
3230 	if (pmc->sysclkreq_high)
3231 		value &= ~PMC_CNTRL_SYSCLK_POLARITY;
3232 	else
3233 		value |= PMC_CNTRL_SYSCLK_POLARITY;
3234 
3235 	if (pmc->corereq_high)
3236 		value &= ~PMC_CNTRL_PWRREQ_POLARITY;
3237 	else
3238 		value |= PMC_CNTRL_PWRREQ_POLARITY;
3239 
3240 	/* configure the output polarity while the request is tristated */
3241 	tegra_pmc_writel(pmc, value, PMC_CNTRL);
3242 
3243 	/* now enable the request */
3244 	value = tegra_pmc_readl(pmc, PMC_CNTRL);
3245 	value |= PMC_CNTRL_SYSCLK_OE;
3246 	tegra_pmc_writel(pmc, value, PMC_CNTRL);
3247 
3248 	/* program core timings which are applicable only for suspend state */
3249 	if (pmc->suspend_mode != TEGRA_SUSPEND_NONE) {
3250 		osc = DIV_ROUND_UP(pmc->core_osc_time * 8192, 1000000);
3251 		pmu = DIV_ROUND_UP(pmc->core_pmu_time * 32768, 1000000);
3252 		off = DIV_ROUND_UP(pmc->core_off_time * 32768, 1000000);
3253 		tegra_pmc_writel(pmc, ((osc << 8) & 0xff00) | (pmu & 0xff),
3254 				 PMC_COREPWRGOOD_TIMER);
3255 		tegra_pmc_writel(pmc, off, PMC_COREPWROFF_TIMER);
3256 	}
3257 }
3258 
tegra20_pmc_setup_irq_polarity(struct tegra_pmc * pmc,struct device_node * np,bool invert)3259 static void tegra20_pmc_setup_irq_polarity(struct tegra_pmc *pmc,
3260 					   struct device_node *np,
3261 					   bool invert)
3262 {
3263 	u32 value;
3264 
3265 	value = tegra_pmc_readl(pmc, PMC_CNTRL);
3266 
3267 	if (invert)
3268 		value |= PMC_CNTRL_INTR_POLARITY;
3269 	else
3270 		value &= ~PMC_CNTRL_INTR_POLARITY;
3271 
3272 	tegra_pmc_writel(pmc, value, PMC_CNTRL);
3273 }
3274 
3275 static const struct tegra_pmc_soc tegra20_pmc_soc = {
3276 	.supports_core_domain = true,
3277 	.num_powergates = ARRAY_SIZE(tegra20_powergates),
3278 	.powergates = tegra20_powergates,
3279 	.num_cpu_powergates = 0,
3280 	.cpu_powergates = NULL,
3281 	.has_tsense_reset = false,
3282 	.has_gpu_clamps = false,
3283 	.needs_mbist_war = false,
3284 	.has_impl_33v_pwr = false,
3285 	.maybe_tz_only = false,
3286 	.num_io_pads = 0,
3287 	.io_pads = NULL,
3288 	.num_pin_descs = 0,
3289 	.pin_descs = NULL,
3290 	.regs = &tegra20_pmc_regs,
3291 	.init = tegra20_pmc_init,
3292 	.setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
3293 	.powergate_set = tegra20_powergate_set,
3294 	.reset_sources = NULL,
3295 	.num_reset_sources = 0,
3296 	.reset_levels = NULL,
3297 	.num_reset_levels = 0,
3298 	.pmc_clks_data = NULL,
3299 	.num_pmc_clks = 0,
3300 	.has_blink_output = true,
3301 	.has_usb_sleepwalk = true,
3302 	.has_single_mmio_aperture = true,
3303 };
3304 
3305 static const char * const tegra30_powergates[] = {
3306 	[TEGRA_POWERGATE_CPU] = "cpu0",
3307 	[TEGRA_POWERGATE_3D] = "td",
3308 	[TEGRA_POWERGATE_VENC] = "venc",
3309 	[TEGRA_POWERGATE_VDEC] = "vdec",
3310 	[TEGRA_POWERGATE_PCIE] = "pcie",
3311 	[TEGRA_POWERGATE_L2] = "l2",
3312 	[TEGRA_POWERGATE_MPE] = "mpe",
3313 	[TEGRA_POWERGATE_HEG] = "heg",
3314 	[TEGRA_POWERGATE_SATA] = "sata",
3315 	[TEGRA_POWERGATE_CPU1] = "cpu1",
3316 	[TEGRA_POWERGATE_CPU2] = "cpu2",
3317 	[TEGRA_POWERGATE_CPU3] = "cpu3",
3318 	[TEGRA_POWERGATE_CELP] = "celp",
3319 	[TEGRA_POWERGATE_3D1] = "td2",
3320 };
3321 
3322 static const u8 tegra30_cpu_powergates[] = {
3323 	TEGRA_POWERGATE_CPU,
3324 	TEGRA_POWERGATE_CPU1,
3325 	TEGRA_POWERGATE_CPU2,
3326 	TEGRA_POWERGATE_CPU3,
3327 };
3328 
3329 static const char * const tegra30_reset_sources[] = {
3330 	"POWER_ON_RESET",
3331 	"WATCHDOG",
3332 	"SENSOR",
3333 	"SW_MAIN",
3334 	"LP0"
3335 };
3336 
3337 static const struct tegra_pmc_soc tegra30_pmc_soc = {
3338 	.supports_core_domain = true,
3339 	.num_powergates = ARRAY_SIZE(tegra30_powergates),
3340 	.powergates = tegra30_powergates,
3341 	.num_cpu_powergates = ARRAY_SIZE(tegra30_cpu_powergates),
3342 	.cpu_powergates = tegra30_cpu_powergates,
3343 	.has_tsense_reset = true,
3344 	.has_gpu_clamps = false,
3345 	.needs_mbist_war = false,
3346 	.has_impl_33v_pwr = false,
3347 	.maybe_tz_only = false,
3348 	.num_io_pads = 0,
3349 	.io_pads = NULL,
3350 	.num_pin_descs = 0,
3351 	.pin_descs = NULL,
3352 	.regs = &tegra20_pmc_regs,
3353 	.init = tegra20_pmc_init,
3354 	.setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
3355 	.powergate_set = tegra20_powergate_set,
3356 	.reset_sources = tegra30_reset_sources,
3357 	.num_reset_sources = ARRAY_SIZE(tegra30_reset_sources),
3358 	.reset_levels = NULL,
3359 	.num_reset_levels = 0,
3360 	.pmc_clks_data = tegra_pmc_clks_data,
3361 	.num_pmc_clks = ARRAY_SIZE(tegra_pmc_clks_data),
3362 	.has_blink_output = true,
3363 	.has_usb_sleepwalk = true,
3364 	.has_single_mmio_aperture = true,
3365 };
3366 
3367 static const char * const tegra114_powergates[] = {
3368 	[TEGRA_POWERGATE_CPU] = "crail",
3369 	[TEGRA_POWERGATE_3D] = "td",
3370 	[TEGRA_POWERGATE_VENC] = "venc",
3371 	[TEGRA_POWERGATE_VDEC] = "vdec",
3372 	[TEGRA_POWERGATE_MPE] = "mpe",
3373 	[TEGRA_POWERGATE_HEG] = "heg",
3374 	[TEGRA_POWERGATE_CPU1] = "cpu1",
3375 	[TEGRA_POWERGATE_CPU2] = "cpu2",
3376 	[TEGRA_POWERGATE_CPU3] = "cpu3",
3377 	[TEGRA_POWERGATE_CELP] = "celp",
3378 	[TEGRA_POWERGATE_CPU0] = "cpu0",
3379 	[TEGRA_POWERGATE_C0NC] = "c0nc",
3380 	[TEGRA_POWERGATE_C1NC] = "c1nc",
3381 	[TEGRA_POWERGATE_DIS] = "dis",
3382 	[TEGRA_POWERGATE_DISB] = "disb",
3383 	[TEGRA_POWERGATE_XUSBA] = "xusba",
3384 	[TEGRA_POWERGATE_XUSBB] = "xusbb",
3385 	[TEGRA_POWERGATE_XUSBC] = "xusbc",
3386 };
3387 
3388 static const u8 tegra114_cpu_powergates[] = {
3389 	TEGRA_POWERGATE_CPU0,
3390 	TEGRA_POWERGATE_CPU1,
3391 	TEGRA_POWERGATE_CPU2,
3392 	TEGRA_POWERGATE_CPU3,
3393 };
3394 
3395 static const struct tegra_pmc_soc tegra114_pmc_soc = {
3396 	.supports_core_domain = false,
3397 	.num_powergates = ARRAY_SIZE(tegra114_powergates),
3398 	.powergates = tegra114_powergates,
3399 	.num_cpu_powergates = ARRAY_SIZE(tegra114_cpu_powergates),
3400 	.cpu_powergates = tegra114_cpu_powergates,
3401 	.has_tsense_reset = true,
3402 	.has_gpu_clamps = false,
3403 	.needs_mbist_war = false,
3404 	.has_impl_33v_pwr = false,
3405 	.maybe_tz_only = false,
3406 	.num_io_pads = 0,
3407 	.io_pads = NULL,
3408 	.num_pin_descs = 0,
3409 	.pin_descs = NULL,
3410 	.regs = &tegra20_pmc_regs,
3411 	.init = tegra20_pmc_init,
3412 	.setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
3413 	.powergate_set = tegra114_powergate_set,
3414 	.reset_sources = tegra30_reset_sources,
3415 	.num_reset_sources = ARRAY_SIZE(tegra30_reset_sources),
3416 	.reset_levels = NULL,
3417 	.num_reset_levels = 0,
3418 	.pmc_clks_data = tegra_pmc_clks_data,
3419 	.num_pmc_clks = ARRAY_SIZE(tegra_pmc_clks_data),
3420 	.has_blink_output = true,
3421 	.has_usb_sleepwalk = true,
3422 	.has_single_mmio_aperture = true,
3423 };
3424 
3425 static const char * const tegra124_powergates[] = {
3426 	[TEGRA_POWERGATE_CPU] = "crail",
3427 	[TEGRA_POWERGATE_3D] = "3d",
3428 	[TEGRA_POWERGATE_VENC] = "venc",
3429 	[TEGRA_POWERGATE_PCIE] = "pcie",
3430 	[TEGRA_POWERGATE_VDEC] = "vdec",
3431 	[TEGRA_POWERGATE_MPE] = "mpe",
3432 	[TEGRA_POWERGATE_HEG] = "heg",
3433 	[TEGRA_POWERGATE_SATA] = "sata",
3434 	[TEGRA_POWERGATE_CPU1] = "cpu1",
3435 	[TEGRA_POWERGATE_CPU2] = "cpu2",
3436 	[TEGRA_POWERGATE_CPU3] = "cpu3",
3437 	[TEGRA_POWERGATE_CELP] = "celp",
3438 	[TEGRA_POWERGATE_CPU0] = "cpu0",
3439 	[TEGRA_POWERGATE_C0NC] = "c0nc",
3440 	[TEGRA_POWERGATE_C1NC] = "c1nc",
3441 	[TEGRA_POWERGATE_SOR] = "sor",
3442 	[TEGRA_POWERGATE_DIS] = "dis",
3443 	[TEGRA_POWERGATE_DISB] = "disb",
3444 	[TEGRA_POWERGATE_XUSBA] = "xusba",
3445 	[TEGRA_POWERGATE_XUSBB] = "xusbb",
3446 	[TEGRA_POWERGATE_XUSBC] = "xusbc",
3447 	[TEGRA_POWERGATE_VIC] = "vic",
3448 	[TEGRA_POWERGATE_IRAM] = "iram",
3449 };
3450 
3451 static const u8 tegra124_cpu_powergates[] = {
3452 	TEGRA_POWERGATE_CPU0,
3453 	TEGRA_POWERGATE_CPU1,
3454 	TEGRA_POWERGATE_CPU2,
3455 	TEGRA_POWERGATE_CPU3,
3456 };
3457 
3458 #define TEGRA_IO_PAD(_id, _dpd, _request, _status, _voltage, _name)	\
3459 	((struct tegra_io_pad_soc) {					\
3460 		.id		= (_id),				\
3461 		.dpd		= (_dpd),				\
3462 		.request	= (_request),				\
3463 		.status		= (_status),				\
3464 		.voltage	= (_voltage),				\
3465 		.name		= (_name),				\
3466 	})
3467 
3468 #define TEGRA_IO_PIN_DESC(_id, _name)	\
3469 	((struct pinctrl_pin_desc) {	\
3470 		.number	= (_id),	\
3471 		.name	= (_name),	\
3472 	})
3473 
3474 static const struct tegra_io_pad_soc tegra124_io_pads[] = {
3475 	TEGRA_IO_PAD(TEGRA_IO_PAD_AUDIO, 17, 0x1b8, 0x1bc, UINT_MAX, "audio"),
3476 	TEGRA_IO_PAD(TEGRA_IO_PAD_BB, 15, 0x1b8, 0x1bc, UINT_MAX, "bb"),
3477 	TEGRA_IO_PAD(TEGRA_IO_PAD_CAM, 4, 0x1c0, 0x1c4, UINT_MAX, "cam"),
3478 	TEGRA_IO_PAD(TEGRA_IO_PAD_COMP, 22, 0x1b8, 0x1bc, UINT_MAX, "comp"),
3479 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIA, 0, 0x1b8, 0x1bc, UINT_MAX, "csia"),
3480 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIB, 1, 0x1b8, 0x1bc, UINT_MAX, "csib"),
3481 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIE, 12, 0x1c0, 0x1c4, UINT_MAX, "csie"),
3482 	TEGRA_IO_PAD(TEGRA_IO_PAD_DSI, 2, 0x1b8, 0x1bc, UINT_MAX, "dsi"),
3483 	TEGRA_IO_PAD(TEGRA_IO_PAD_DSIB, 7, 0x1c0, 0x1c4, UINT_MAX, "dsib"),
3484 	TEGRA_IO_PAD(TEGRA_IO_PAD_DSIC, 8, 0x1c0, 0x1c4, UINT_MAX, "dsic"),
3485 	TEGRA_IO_PAD(TEGRA_IO_PAD_DSID, 9, 0x1c0, 0x1c4, UINT_MAX, "dsid"),
3486 	TEGRA_IO_PAD(TEGRA_IO_PAD_HDMI, 28, 0x1b8, 0x1bc, UINT_MAX, "hdmi"),
3487 	TEGRA_IO_PAD(TEGRA_IO_PAD_HSIC, 19, 0x1b8, 0x1bc, UINT_MAX, "hsic"),
3488 	TEGRA_IO_PAD(TEGRA_IO_PAD_HV, 6, 0x1c0, 0x1c4, UINT_MAX, "hv"),
3489 	TEGRA_IO_PAD(TEGRA_IO_PAD_LVDS, 25, 0x1c0, 0x1c4, UINT_MAX, "lvds"),
3490 	TEGRA_IO_PAD(TEGRA_IO_PAD_MIPI_BIAS, 3, 0x1b8, 0x1bc, UINT_MAX, "mipi-bias"),
3491 	TEGRA_IO_PAD(TEGRA_IO_PAD_NAND, 13, 0x1b8, 0x1bc, UINT_MAX, "nand"),
3492 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_BIAS, 4, 0x1b8, 0x1bc, UINT_MAX, "pex-bias"),
3493 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CLK1, 5, 0x1b8, 0x1bc, UINT_MAX, "pex-clk1"),
3494 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CLK2, 6, 0x1b8, 0x1bc, UINT_MAX, "pex-clk2"),
3495 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CNTRL, 0, 0x1c0, 0x1c4, UINT_MAX, "pex-cntrl"),
3496 	TEGRA_IO_PAD(TEGRA_IO_PAD_SDMMC1, 1, 0x1c0, 0x1c4, UINT_MAX, "sdmmc1"),
3497 	TEGRA_IO_PAD(TEGRA_IO_PAD_SDMMC3, 2, 0x1c0, 0x1c4, UINT_MAX, "sdmmc3"),
3498 	TEGRA_IO_PAD(TEGRA_IO_PAD_SDMMC4, 3, 0x1c0, 0x1c4, UINT_MAX, "sdmmc4"),
3499 	TEGRA_IO_PAD(TEGRA_IO_PAD_SYS_DDC, 26, 0x1c0, 0x1c4, UINT_MAX, "sys_ddc"),
3500 	TEGRA_IO_PAD(TEGRA_IO_PAD_UART, 14, 0x1b8, 0x1bc, UINT_MAX, "uart"),
3501 	TEGRA_IO_PAD(TEGRA_IO_PAD_USB0, 9, 0x1b8, 0x1bc, UINT_MAX, "usb0"),
3502 	TEGRA_IO_PAD(TEGRA_IO_PAD_USB1, 10, 0x1b8, 0x1bc, UINT_MAX, "usb1"),
3503 	TEGRA_IO_PAD(TEGRA_IO_PAD_USB2, 11, 0x1b8, 0x1bc, UINT_MAX, "usb2"),
3504 	TEGRA_IO_PAD(TEGRA_IO_PAD_USB_BIAS, 12, 0x1b8, 0x1bc, UINT_MAX, "usb_bias"),
3505 };
3506 
3507 static const struct pinctrl_pin_desc tegra124_pin_descs[] = {
3508 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_AUDIO, "audio"),
3509 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_BB, "bb"),
3510 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CAM, "cam"),
3511 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_COMP, "comp"),
3512 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIA, "csia"),
3513 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIB, "csib"),
3514 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIE, "csie"),
3515 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DSI, "dsi"),
3516 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DSIB, "dsib"),
3517 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DSIC, "dsic"),
3518 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DSID, "dsid"),
3519 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_HDMI, "hdmi"),
3520 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_HSIC, "hsic"),
3521 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_HV, "hv"),
3522 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_LVDS, "lvds"),
3523 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_MIPI_BIAS, "mipi-bias"),
3524 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_NAND, "nand"),
3525 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_BIAS, "pex-bias"),
3526 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CLK1, "pex-clk1"),
3527 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CLK2, "pex-clk2"),
3528 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CNTRL, "pex-cntrl"),
3529 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SDMMC1, "sdmmc1"),
3530 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SDMMC3, "sdmmc3"),
3531 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SDMMC4, "sdmmc4"),
3532 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SYS_DDC, "sys_ddc"),
3533 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_UART, "uart"),
3534 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_USB0, "usb0"),
3535 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_USB1, "usb1"),
3536 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_USB2, "usb2"),
3537 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_USB_BIAS, "usb_bias"),
3538 };
3539 
3540 static const struct tegra_pmc_soc tegra124_pmc_soc = {
3541 	.supports_core_domain = false,
3542 	.num_powergates = ARRAY_SIZE(tegra124_powergates),
3543 	.powergates = tegra124_powergates,
3544 	.num_cpu_powergates = ARRAY_SIZE(tegra124_cpu_powergates),
3545 	.cpu_powergates = tegra124_cpu_powergates,
3546 	.has_tsense_reset = true,
3547 	.has_gpu_clamps = true,
3548 	.needs_mbist_war = false,
3549 	.has_impl_33v_pwr = false,
3550 	.maybe_tz_only = false,
3551 	.num_io_pads = ARRAY_SIZE(tegra124_io_pads),
3552 	.io_pads = tegra124_io_pads,
3553 	.num_pin_descs = ARRAY_SIZE(tegra124_pin_descs),
3554 	.pin_descs = tegra124_pin_descs,
3555 	.regs = &tegra20_pmc_regs,
3556 	.init = tegra20_pmc_init,
3557 	.setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
3558 	.powergate_set = tegra114_powergate_set,
3559 	.reset_sources = tegra30_reset_sources,
3560 	.num_reset_sources = ARRAY_SIZE(tegra30_reset_sources),
3561 	.reset_levels = NULL,
3562 	.num_reset_levels = 0,
3563 	.pmc_clks_data = tegra_pmc_clks_data,
3564 	.num_pmc_clks = ARRAY_SIZE(tegra_pmc_clks_data),
3565 	.has_blink_output = true,
3566 	.has_usb_sleepwalk = true,
3567 	.has_single_mmio_aperture = true,
3568 };
3569 
3570 static const char * const tegra210_powergates[] = {
3571 	[TEGRA_POWERGATE_CPU] = "crail",
3572 	[TEGRA_POWERGATE_3D] = "3d",
3573 	[TEGRA_POWERGATE_VENC] = "venc",
3574 	[TEGRA_POWERGATE_PCIE] = "pcie",
3575 	[TEGRA_POWERGATE_MPE] = "mpe",
3576 	[TEGRA_POWERGATE_SATA] = "sata",
3577 	[TEGRA_POWERGATE_CPU1] = "cpu1",
3578 	[TEGRA_POWERGATE_CPU2] = "cpu2",
3579 	[TEGRA_POWERGATE_CPU3] = "cpu3",
3580 	[TEGRA_POWERGATE_CPU0] = "cpu0",
3581 	[TEGRA_POWERGATE_C0NC] = "c0nc",
3582 	[TEGRA_POWERGATE_SOR] = "sor",
3583 	[TEGRA_POWERGATE_DIS] = "dis",
3584 	[TEGRA_POWERGATE_DISB] = "disb",
3585 	[TEGRA_POWERGATE_XUSBA] = "xusba",
3586 	[TEGRA_POWERGATE_XUSBB] = "xusbb",
3587 	[TEGRA_POWERGATE_XUSBC] = "xusbc",
3588 	[TEGRA_POWERGATE_VIC] = "vic",
3589 	[TEGRA_POWERGATE_IRAM] = "iram",
3590 	[TEGRA_POWERGATE_NVDEC] = "nvdec",
3591 	[TEGRA_POWERGATE_NVJPG] = "nvjpg",
3592 	[TEGRA_POWERGATE_AUD] = "aud",
3593 	[TEGRA_POWERGATE_DFD] = "dfd",
3594 	[TEGRA_POWERGATE_VE2] = "ve2",
3595 };
3596 
3597 static const u8 tegra210_cpu_powergates[] = {
3598 	TEGRA_POWERGATE_CPU0,
3599 	TEGRA_POWERGATE_CPU1,
3600 	TEGRA_POWERGATE_CPU2,
3601 	TEGRA_POWERGATE_CPU3,
3602 };
3603 
3604 static const struct tegra_io_pad_soc tegra210_io_pads[] = {
3605 	TEGRA_IO_PAD(TEGRA_IO_PAD_AUDIO, 17, 0x1b8, 0x1bc, 5, "audio"),
3606 	TEGRA_IO_PAD(TEGRA_IO_PAD_AUDIO_HV, 29, 0x1c0, 0x1c4, 18, "audio-hv"),
3607 	TEGRA_IO_PAD(TEGRA_IO_PAD_CAM, 4, 0x1c0, 0x1c4, 10, "cam"),
3608 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIA, 0, 0x1b8, 0x1bc, UINT_MAX, "csia"),
3609 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIB, 1, 0x1b8, 0x1bc, UINT_MAX, "csib"),
3610 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIC, 10, 0x1c0, 0x1c4, UINT_MAX, "csic"),
3611 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSID, 11, 0x1c0, 0x1c4, UINT_MAX, "csid"),
3612 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIE, 12, 0x1c0, 0x1c4, UINT_MAX, "csie"),
3613 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIF, 13, 0x1c0, 0x1c4, UINT_MAX, "csif"),
3614 	TEGRA_IO_PAD(TEGRA_IO_PAD_DBG, 25, 0x1b8, 0x1bc, 19, "dbg"),
3615 	TEGRA_IO_PAD(TEGRA_IO_PAD_DEBUG_NONAO, 26, 0x1b8, 0x1bc, UINT_MAX, "debug-nonao"),
3616 	TEGRA_IO_PAD(TEGRA_IO_PAD_DMIC, 18, 0x1c0, 0x1c4, 20, "dmic"),
3617 	TEGRA_IO_PAD(TEGRA_IO_PAD_DP, 19, 0x1c0, 0x1c4, UINT_MAX, "dp"),
3618 	TEGRA_IO_PAD(TEGRA_IO_PAD_DSI, 2, 0x1b8, 0x1bc, UINT_MAX, "dsi"),
3619 	TEGRA_IO_PAD(TEGRA_IO_PAD_DSIB, 7, 0x1c0, 0x1c4, UINT_MAX, "dsib"),
3620 	TEGRA_IO_PAD(TEGRA_IO_PAD_DSIC, 8, 0x1c0, 0x1c4, UINT_MAX, "dsic"),
3621 	TEGRA_IO_PAD(TEGRA_IO_PAD_DSID, 9, 0x1c0, 0x1c4, UINT_MAX, "dsid"),
3622 	TEGRA_IO_PAD(TEGRA_IO_PAD_EMMC, 3, 0x1c0, 0x1c4, UINT_MAX, "emmc"),
3623 	TEGRA_IO_PAD(TEGRA_IO_PAD_EMMC2, 5, 0x1c0, 0x1c4, UINT_MAX, "emmc2"),
3624 	TEGRA_IO_PAD(TEGRA_IO_PAD_GPIO, 27, 0x1b8, 0x1bc, 21, "gpio"),
3625 	TEGRA_IO_PAD(TEGRA_IO_PAD_HDMI, 28, 0x1b8, 0x1bc, UINT_MAX, "hdmi"),
3626 	TEGRA_IO_PAD(TEGRA_IO_PAD_HSIC, 19, 0x1b8, 0x1bc, UINT_MAX, "hsic"),
3627 	TEGRA_IO_PAD(TEGRA_IO_PAD_LVDS, 25, 0x1c0, 0x1c4, UINT_MAX, "lvds"),
3628 	TEGRA_IO_PAD(TEGRA_IO_PAD_MIPI_BIAS, 3, 0x1b8, 0x1bc, UINT_MAX, "mipi-bias"),
3629 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_BIAS, 4, 0x1b8, 0x1bc, UINT_MAX, "pex-bias"),
3630 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CLK1, 5, 0x1b8, 0x1bc, UINT_MAX, "pex-clk1"),
3631 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CLK2, 6, 0x1b8, 0x1bc, UINT_MAX, "pex-clk2"),
3632 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CNTRL, UINT_MAX, UINT_MAX, UINT_MAX, 11, "pex-cntrl"),
3633 	TEGRA_IO_PAD(TEGRA_IO_PAD_SDMMC1, 1, 0x1c0, 0x1c4, 12, "sdmmc1"),
3634 	TEGRA_IO_PAD(TEGRA_IO_PAD_SDMMC3, 2, 0x1c0, 0x1c4, 13, "sdmmc3"),
3635 	TEGRA_IO_PAD(TEGRA_IO_PAD_SPI, 14, 0x1c0, 0x1c4, 22, "spi"),
3636 	TEGRA_IO_PAD(TEGRA_IO_PAD_SPI_HV, 15, 0x1c0, 0x1c4, 23, "spi-hv"),
3637 	TEGRA_IO_PAD(TEGRA_IO_PAD_UART, 14, 0x1b8, 0x1bc, 2, "uart"),
3638 	TEGRA_IO_PAD(TEGRA_IO_PAD_USB0, 9, 0x1b8, 0x1bc, UINT_MAX, "usb0"),
3639 	TEGRA_IO_PAD(TEGRA_IO_PAD_USB1, 10, 0x1b8, 0x1bc, UINT_MAX, "usb1"),
3640 	TEGRA_IO_PAD(TEGRA_IO_PAD_USB2, 11, 0x1b8, 0x1bc, UINT_MAX, "usb2"),
3641 	TEGRA_IO_PAD(TEGRA_IO_PAD_USB3, 18, 0x1b8, 0x1bc, UINT_MAX, "usb3"),
3642 	TEGRA_IO_PAD(TEGRA_IO_PAD_USB_BIAS, 12, 0x1b8, 0x1bc, UINT_MAX, "usb-bias"),
3643 };
3644 
3645 static const struct pinctrl_pin_desc tegra210_pin_descs[] = {
3646 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_AUDIO, "audio"),
3647 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_AUDIO_HV, "audio-hv"),
3648 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CAM, "cam"),
3649 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIA, "csia"),
3650 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIB, "csib"),
3651 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIC, "csic"),
3652 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSID, "csid"),
3653 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIE, "csie"),
3654 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIF, "csif"),
3655 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DBG, "dbg"),
3656 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DEBUG_NONAO, "debug-nonao"),
3657 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DMIC, "dmic"),
3658 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DP, "dp"),
3659 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DSI, "dsi"),
3660 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DSIB, "dsib"),
3661 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DSIC, "dsic"),
3662 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DSID, "dsid"),
3663 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_EMMC, "emmc"),
3664 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_EMMC2, "emmc2"),
3665 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_GPIO, "gpio"),
3666 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_HDMI, "hdmi"),
3667 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_HSIC, "hsic"),
3668 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_LVDS, "lvds"),
3669 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_MIPI_BIAS, "mipi-bias"),
3670 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_BIAS, "pex-bias"),
3671 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CLK1, "pex-clk1"),
3672 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CLK2, "pex-clk2"),
3673 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CNTRL, "pex-cntrl"),
3674 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SDMMC1, "sdmmc1"),
3675 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SDMMC3, "sdmmc3"),
3676 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SPI, "spi"),
3677 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SPI_HV, "spi-hv"),
3678 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_UART, "uart"),
3679 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_USB0, "usb0"),
3680 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_USB1, "usb1"),
3681 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_USB2, "usb2"),
3682 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_USB3, "usb3"),
3683 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_USB_BIAS, "usb-bias"),
3684 };
3685 
3686 static const char * const tegra210_reset_sources[] = {
3687 	"POWER_ON_RESET",
3688 	"WATCHDOG",
3689 	"SENSOR",
3690 	"SW_MAIN",
3691 	"LP0",
3692 	"AOTAG"
3693 };
3694 
3695 static const struct tegra_wake_event tegra210_wake_events[] = {
3696 	TEGRA_WAKE_IRQ("rtc", 16, 2),
3697 	TEGRA_WAKE_IRQ("pmu", 51, 86),
3698 };
3699 
3700 static const struct tegra_pmc_soc tegra210_pmc_soc = {
3701 	.supports_core_domain = false,
3702 	.num_powergates = ARRAY_SIZE(tegra210_powergates),
3703 	.powergates = tegra210_powergates,
3704 	.num_cpu_powergates = ARRAY_SIZE(tegra210_cpu_powergates),
3705 	.cpu_powergates = tegra210_cpu_powergates,
3706 	.has_tsense_reset = true,
3707 	.has_gpu_clamps = true,
3708 	.needs_mbist_war = true,
3709 	.has_impl_33v_pwr = false,
3710 	.maybe_tz_only = true,
3711 	.num_io_pads = ARRAY_SIZE(tegra210_io_pads),
3712 	.io_pads = tegra210_io_pads,
3713 	.num_pin_descs = ARRAY_SIZE(tegra210_pin_descs),
3714 	.pin_descs = tegra210_pin_descs,
3715 	.regs = &tegra20_pmc_regs,
3716 	.init = tegra20_pmc_init,
3717 	.setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
3718 	.powergate_set = tegra114_powergate_set,
3719 	.irq_set_wake = tegra210_pmc_irq_set_wake,
3720 	.irq_set_type = tegra210_pmc_irq_set_type,
3721 	.reset_sources = tegra210_reset_sources,
3722 	.num_reset_sources = ARRAY_SIZE(tegra210_reset_sources),
3723 	.reset_levels = NULL,
3724 	.num_reset_levels = 0,
3725 	.num_wake_events = ARRAY_SIZE(tegra210_wake_events),
3726 	.wake_events = tegra210_wake_events,
3727 	.pmc_clks_data = tegra_pmc_clks_data,
3728 	.num_pmc_clks = ARRAY_SIZE(tegra_pmc_clks_data),
3729 	.has_blink_output = true,
3730 	.has_usb_sleepwalk = true,
3731 	.has_single_mmio_aperture = true,
3732 };
3733 
3734 static const struct tegra_io_pad_soc tegra186_io_pads[] = {
3735 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIA, 0, 0x74, 0x78, UINT_MAX, "csia"),
3736 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIB, 1, 0x74, 0x78, UINT_MAX, "csib"),
3737 	TEGRA_IO_PAD(TEGRA_IO_PAD_DSI, 2, 0x74, 0x78, UINT_MAX, "dsi"),
3738 	TEGRA_IO_PAD(TEGRA_IO_PAD_MIPI_BIAS, 3, 0x74, 0x78, UINT_MAX, "mipi-bias"),
3739 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CLK_BIAS, 4, 0x74, 0x78, UINT_MAX, "pex-clk-bias"),
3740 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CLK3, 5, 0x74, 0x78, UINT_MAX, "pex-clk3"),
3741 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CLK2, 6, 0x74, 0x78, UINT_MAX, "pex-clk2"),
3742 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CLK1, 7, 0x74, 0x78, UINT_MAX, "pex-clk1"),
3743 	TEGRA_IO_PAD(TEGRA_IO_PAD_USB0, 9, 0x74, 0x78, UINT_MAX, "usb0"),
3744 	TEGRA_IO_PAD(TEGRA_IO_PAD_USB1, 10, 0x74, 0x78, UINT_MAX, "usb1"),
3745 	TEGRA_IO_PAD(TEGRA_IO_PAD_USB2, 11, 0x74, 0x78, UINT_MAX, "usb2"),
3746 	TEGRA_IO_PAD(TEGRA_IO_PAD_USB_BIAS, 12, 0x74, 0x78, UINT_MAX, "usb-bias"),
3747 	TEGRA_IO_PAD(TEGRA_IO_PAD_UART, 14, 0x74, 0x78, UINT_MAX, "uart"),
3748 	TEGRA_IO_PAD(TEGRA_IO_PAD_AUDIO, 17, 0x74, 0x78, UINT_MAX, "audio"),
3749 	TEGRA_IO_PAD(TEGRA_IO_PAD_HSIC, 19, 0x74, 0x78, UINT_MAX, "hsic"),
3750 	TEGRA_IO_PAD(TEGRA_IO_PAD_DBG, 25, 0x74, 0x78, UINT_MAX, "dbg"),
3751 	TEGRA_IO_PAD(TEGRA_IO_PAD_HDMI_DP0, 28, 0x74, 0x78, UINT_MAX, "hdmi-dp0"),
3752 	TEGRA_IO_PAD(TEGRA_IO_PAD_HDMI_DP1, 29, 0x74, 0x78, UINT_MAX, "hdmi-dp1"),
3753 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CNTRL, 0, 0x7c, 0x80, UINT_MAX, "pex-cntrl"),
3754 	TEGRA_IO_PAD(TEGRA_IO_PAD_SDMMC2_HV, 2, 0x7c, 0x80, 5, "sdmmc2-hv"),
3755 	TEGRA_IO_PAD(TEGRA_IO_PAD_SDMMC4, 4, 0x7c, 0x80, UINT_MAX, "sdmmc4"),
3756 	TEGRA_IO_PAD(TEGRA_IO_PAD_CAM, 6, 0x7c, 0x80, UINT_MAX, "cam"),
3757 	TEGRA_IO_PAD(TEGRA_IO_PAD_DSIB, 8, 0x7c, 0x80, UINT_MAX, "dsib"),
3758 	TEGRA_IO_PAD(TEGRA_IO_PAD_DSIC, 9, 0x7c, 0x80, UINT_MAX, "dsic"),
3759 	TEGRA_IO_PAD(TEGRA_IO_PAD_DSID, 10, 0x7c, 0x80, UINT_MAX, "dsid"),
3760 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIC, 11, 0x7c, 0x80, UINT_MAX, "csic"),
3761 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSID, 12, 0x7c, 0x80, UINT_MAX, "csid"),
3762 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIE, 13, 0x7c, 0x80, UINT_MAX, "csie"),
3763 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIF, 14, 0x7c, 0x80, UINT_MAX, "csif"),
3764 	TEGRA_IO_PAD(TEGRA_IO_PAD_SPI, 15, 0x7c, 0x80, UINT_MAX, "spi"),
3765 	TEGRA_IO_PAD(TEGRA_IO_PAD_UFS, 17, 0x7c, 0x80, UINT_MAX, "ufs"),
3766 	TEGRA_IO_PAD(TEGRA_IO_PAD_DMIC_HV, 20, 0x7c, 0x80, 2, "dmic-hv"),
3767 	TEGRA_IO_PAD(TEGRA_IO_PAD_EDP, 21, 0x7c, 0x80, UINT_MAX, "edp"),
3768 	TEGRA_IO_PAD(TEGRA_IO_PAD_SDMMC1_HV, 23, 0x7c, 0x80, 4, "sdmmc1-hv"),
3769 	TEGRA_IO_PAD(TEGRA_IO_PAD_SDMMC3_HV, 24, 0x7c, 0x80, 6, "sdmmc3-hv"),
3770 	TEGRA_IO_PAD(TEGRA_IO_PAD_CONN, 28, 0x7c, 0x80, UINT_MAX, "conn"),
3771 	TEGRA_IO_PAD(TEGRA_IO_PAD_AUDIO_HV, 29, 0x7c, 0x80, 1, "audio-hv"),
3772 	TEGRA_IO_PAD(TEGRA_IO_PAD_AO_HV, UINT_MAX, UINT_MAX, UINT_MAX, 0, "ao-hv"),
3773 };
3774 
3775 static const struct pinctrl_pin_desc tegra186_pin_descs[] = {
3776 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIA, "csia"),
3777 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIB, "csib"),
3778 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DSI, "dsi"),
3779 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_MIPI_BIAS, "mipi-bias"),
3780 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CLK_BIAS, "pex-clk-bias"),
3781 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CLK3, "pex-clk3"),
3782 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CLK2, "pex-clk2"),
3783 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CLK1, "pex-clk1"),
3784 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_USB0, "usb0"),
3785 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_USB1, "usb1"),
3786 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_USB2, "usb2"),
3787 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_USB_BIAS, "usb-bias"),
3788 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_UART, "uart"),
3789 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_AUDIO, "audio"),
3790 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_HSIC, "hsic"),
3791 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DBG, "dbg"),
3792 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_HDMI_DP0, "hdmi-dp0"),
3793 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_HDMI_DP1, "hdmi-dp1"),
3794 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CNTRL, "pex-cntrl"),
3795 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SDMMC2_HV, "sdmmc2-hv"),
3796 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SDMMC4, "sdmmc4"),
3797 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CAM, "cam"),
3798 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DSIB, "dsib"),
3799 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DSIC, "dsic"),
3800 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DSID, "dsid"),
3801 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIC, "csic"),
3802 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSID, "csid"),
3803 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIE, "csie"),
3804 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIF, "csif"),
3805 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SPI, "spi"),
3806 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_UFS, "ufs"),
3807 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DMIC_HV, "dmic-hv"),
3808 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_EDP, "edp"),
3809 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SDMMC1_HV, "sdmmc1-hv"),
3810 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SDMMC3_HV, "sdmmc3-hv"),
3811 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CONN, "conn"),
3812 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_AUDIO_HV, "audio-hv"),
3813 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_AO_HV, "ao-hv"),
3814 };
3815 
3816 static const struct tegra_pmc_regs tegra186_pmc_regs = {
3817 	.scratch0 = 0x2000,
3818 	.rst_status = 0x70,
3819 	.rst_source_shift = 0x2,
3820 	.rst_source_mask = 0x3c,
3821 	.rst_level_shift = 0x0,
3822 	.rst_level_mask = 0x3,
3823 };
3824 
tegra186_pmc_init(struct tegra_pmc * pmc)3825 static void tegra186_pmc_init(struct tegra_pmc *pmc)
3826 {
3827 	pmc->syscore.suspend = tegra186_pmc_wake_syscore_suspend;
3828 	pmc->syscore.resume = tegra186_pmc_wake_syscore_resume;
3829 
3830 	register_syscore_ops(&pmc->syscore);
3831 }
3832 
tegra186_pmc_setup_irq_polarity(struct tegra_pmc * pmc,struct device_node * np,bool invert)3833 static void tegra186_pmc_setup_irq_polarity(struct tegra_pmc *pmc,
3834 					    struct device_node *np,
3835 					    bool invert)
3836 {
3837 	struct resource regs;
3838 	void __iomem *wake;
3839 	u32 value;
3840 	int index;
3841 
3842 	index = of_property_match_string(np, "reg-names", "wake");
3843 	if (index < 0) {
3844 		dev_err(pmc->dev, "failed to find PMC wake registers\n");
3845 		return;
3846 	}
3847 
3848 	of_address_to_resource(np, index, &regs);
3849 
3850 	wake = ioremap(regs.start, resource_size(&regs));
3851 	if (!wake) {
3852 		dev_err(pmc->dev, "failed to map PMC wake registers\n");
3853 		return;
3854 	}
3855 
3856 	value = readl(wake + WAKE_AOWAKE_CTRL);
3857 
3858 	if (invert)
3859 		value |= WAKE_AOWAKE_CTRL_INTR_POLARITY;
3860 	else
3861 		value &= ~WAKE_AOWAKE_CTRL_INTR_POLARITY;
3862 
3863 	writel(value, wake + WAKE_AOWAKE_CTRL);
3864 
3865 	iounmap(wake);
3866 }
3867 
3868 static const char * const tegra186_reset_sources[] = {
3869 	"SYS_RESET",
3870 	"AOWDT",
3871 	"MCCPLEXWDT",
3872 	"BPMPWDT",
3873 	"SCEWDT",
3874 	"SPEWDT",
3875 	"APEWDT",
3876 	"BCCPLEXWDT",
3877 	"SENSOR",
3878 	"AOTAG",
3879 	"VFSENSOR",
3880 	"SWREST",
3881 	"SC7",
3882 	"HSM",
3883 	"CORESIGHT"
3884 };
3885 
3886 static const char * const tegra186_reset_levels[] = {
3887 	"L0", "L1", "L2", "WARM"
3888 };
3889 
3890 static const struct tegra_wake_event tegra186_wake_events[] = {
3891 	TEGRA_WAKE_IRQ("pmu", 24, 209),
3892 	TEGRA_WAKE_GPIO("power", 29, 1, TEGRA186_AON_GPIO(FF, 0)),
3893 	TEGRA_WAKE_IRQ("rtc", 73, 10),
3894 };
3895 
3896 static const struct tegra_pmc_soc tegra186_pmc_soc = {
3897 	.supports_core_domain = false,
3898 	.num_powergates = 0,
3899 	.powergates = NULL,
3900 	.num_cpu_powergates = 0,
3901 	.cpu_powergates = NULL,
3902 	.has_tsense_reset = false,
3903 	.has_gpu_clamps = false,
3904 	.needs_mbist_war = false,
3905 	.has_impl_33v_pwr = true,
3906 	.maybe_tz_only = false,
3907 	.num_io_pads = ARRAY_SIZE(tegra186_io_pads),
3908 	.io_pads = tegra186_io_pads,
3909 	.num_pin_descs = ARRAY_SIZE(tegra186_pin_descs),
3910 	.pin_descs = tegra186_pin_descs,
3911 	.regs = &tegra186_pmc_regs,
3912 	.init = tegra186_pmc_init,
3913 	.setup_irq_polarity = tegra186_pmc_setup_irq_polarity,
3914 	.set_wake_filters = tegra186_pmc_set_wake_filters,
3915 	.irq_set_wake = tegra186_pmc_irq_set_wake,
3916 	.irq_set_type = tegra186_pmc_irq_set_type,
3917 	.reset_sources = tegra186_reset_sources,
3918 	.num_reset_sources = ARRAY_SIZE(tegra186_reset_sources),
3919 	.reset_levels = tegra186_reset_levels,
3920 	.num_reset_levels = ARRAY_SIZE(tegra186_reset_levels),
3921 	.num_wake_events = ARRAY_SIZE(tegra186_wake_events),
3922 	.wake_events = tegra186_wake_events,
3923 	.max_wake_events = 96,
3924 	.max_wake_vectors = 3,
3925 	.pmc_clks_data = NULL,
3926 	.num_pmc_clks = 0,
3927 	.has_blink_output = false,
3928 	.has_usb_sleepwalk = false,
3929 	.has_single_mmio_aperture = false,
3930 };
3931 
3932 static const struct tegra_io_pad_soc tegra194_io_pads[] = {
3933 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIA, 0, 0x74, 0x78, UINT_MAX, "csia"),
3934 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIB, 1, 0x74, 0x78, UINT_MAX, "csib"),
3935 	TEGRA_IO_PAD(TEGRA_IO_PAD_MIPI_BIAS, 3, 0x74, 0x78, UINT_MAX, "mipi-bias"),
3936 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CLK_BIAS, 4, 0x74, 0x78, UINT_MAX, "pex-clk-bias"),
3937 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CLK3, 5, 0x74, 0x78, UINT_MAX, "pex-clk3"),
3938 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CLK2, 6, 0x74, 0x78, UINT_MAX, "pex-clk2"),
3939 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CLK1, 7, 0x74, 0x78, UINT_MAX, "pex-clk1"),
3940 	TEGRA_IO_PAD(TEGRA_IO_PAD_EQOS, 8, 0x74, 0x78, UINT_MAX, "eqos"),
3941 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CLK_2_BIAS, 9, 0x74, 0x78, UINT_MAX, "pex-clk-2-bias"),
3942 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CLK_2, 10, 0x74, 0x78, UINT_MAX, "pex-clk-2"),
3943 	TEGRA_IO_PAD(TEGRA_IO_PAD_DAP3, 11, 0x74, 0x78, UINT_MAX, "dap3"),
3944 	TEGRA_IO_PAD(TEGRA_IO_PAD_DAP5, 12, 0x74, 0x78, UINT_MAX, "dap5"),
3945 	TEGRA_IO_PAD(TEGRA_IO_PAD_UART, 14, 0x74, 0x78, UINT_MAX, "uart"),
3946 	TEGRA_IO_PAD(TEGRA_IO_PAD_PWR_CTL, 15, 0x74, 0x78, UINT_MAX, "pwr-ctl"),
3947 	TEGRA_IO_PAD(TEGRA_IO_PAD_SOC_GPIO53, 16, 0x74, 0x78, UINT_MAX, "soc-gpio53"),
3948 	TEGRA_IO_PAD(TEGRA_IO_PAD_AUDIO, 17, 0x74, 0x78, UINT_MAX, "audio"),
3949 	TEGRA_IO_PAD(TEGRA_IO_PAD_GP_PWM2, 18, 0x74, 0x78, UINT_MAX, "gp-pwm2"),
3950 	TEGRA_IO_PAD(TEGRA_IO_PAD_GP_PWM3, 19, 0x74, 0x78, UINT_MAX, "gp-pwm3"),
3951 	TEGRA_IO_PAD(TEGRA_IO_PAD_SOC_GPIO12, 20, 0x74, 0x78, UINT_MAX, "soc-gpio12"),
3952 	TEGRA_IO_PAD(TEGRA_IO_PAD_SOC_GPIO13, 21, 0x74, 0x78, UINT_MAX, "soc-gpio13"),
3953 	TEGRA_IO_PAD(TEGRA_IO_PAD_SOC_GPIO10, 22, 0x74, 0x78, UINT_MAX, "soc-gpio10"),
3954 	TEGRA_IO_PAD(TEGRA_IO_PAD_UART4, 23, 0x74, 0x78, UINT_MAX, "uart4"),
3955 	TEGRA_IO_PAD(TEGRA_IO_PAD_UART5, 24, 0x74, 0x78, UINT_MAX, "uart5"),
3956 	TEGRA_IO_PAD(TEGRA_IO_PAD_DBG, 25, 0x74, 0x78, UINT_MAX, "dbg"),
3957 	TEGRA_IO_PAD(TEGRA_IO_PAD_HDMI_DP3, 26, 0x74, 0x78, UINT_MAX, "hdmi-dp3"),
3958 	TEGRA_IO_PAD(TEGRA_IO_PAD_HDMI_DP2, 27, 0x74, 0x78, UINT_MAX, "hdmi-dp2"),
3959 	TEGRA_IO_PAD(TEGRA_IO_PAD_HDMI_DP0, 28, 0x74, 0x78, UINT_MAX, "hdmi-dp0"),
3960 	TEGRA_IO_PAD(TEGRA_IO_PAD_HDMI_DP1, 29, 0x74, 0x78, UINT_MAX, "hdmi-dp1"),
3961 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CNTRL, 0, 0x7c, 0x80, UINT_MAX, "pex-cntrl"),
3962 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CTL2, 1, 0x7c, 0x80, UINT_MAX, "pex-ctl2"),
3963 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_L0_RST, 2, 0x7c, 0x80, UINT_MAX, "pex-l0-rst"),
3964 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_L1_RST, 3, 0x7c, 0x80, UINT_MAX, "pex-l1-rst"),
3965 	TEGRA_IO_PAD(TEGRA_IO_PAD_SDMMC4, 4, 0x7c, 0x80, UINT_MAX, "sdmmc4"),
3966 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_L5_RST, 5, 0x7c, 0x80, UINT_MAX, "pex-l5-rst"),
3967 	TEGRA_IO_PAD(TEGRA_IO_PAD_CAM, 6, 0x7c, 0x80, UINT_MAX, "cam"),
3968 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIC, 11, 0x7c, 0x80, UINT_MAX, "csic"),
3969 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSID, 12, 0x7c, 0x80, UINT_MAX, "csid"),
3970 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIE, 13, 0x7c, 0x80, UINT_MAX, "csie"),
3971 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIF, 14, 0x7c, 0x80, UINT_MAX, "csif"),
3972 	TEGRA_IO_PAD(TEGRA_IO_PAD_SPI, 15, 0x7c, 0x80, UINT_MAX, "spi"),
3973 	TEGRA_IO_PAD(TEGRA_IO_PAD_UFS, 17, 0x7c, 0x80, UINT_MAX, "ufs"),
3974 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIG, 18, 0x7c, 0x80, UINT_MAX, "csig"),
3975 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIH, 19, 0x7c, 0x80, UINT_MAX, "csih"),
3976 	TEGRA_IO_PAD(TEGRA_IO_PAD_EDP, 21, 0x7c, 0x80, UINT_MAX, "edp"),
3977 	TEGRA_IO_PAD(TEGRA_IO_PAD_SDMMC1_HV, 23, 0x7c, 0x80, 4, "sdmmc1-hv"),
3978 	TEGRA_IO_PAD(TEGRA_IO_PAD_SDMMC3_HV, 24, 0x7c, 0x80, 6, "sdmmc3-hv"),
3979 	TEGRA_IO_PAD(TEGRA_IO_PAD_CONN, 28, 0x7c, 0x80, UINT_MAX, "conn"),
3980 	TEGRA_IO_PAD(TEGRA_IO_PAD_AUDIO_HV, 29, 0x7c, 0x80, 1, "audio-hv"),
3981 	TEGRA_IO_PAD(TEGRA_IO_PAD_AO_HV, UINT_MAX, UINT_MAX, UINT_MAX, 0, "ao-hv"),
3982 };
3983 
3984 static const struct pinctrl_pin_desc tegra194_pin_descs[] = {
3985 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIA, "csia"),
3986 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIB, "csib"),
3987 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_MIPI_BIAS, "mipi-bias"),
3988 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CLK_BIAS, "pex-clk-bias"),
3989 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CLK3, "pex-clk3"),
3990 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CLK2, "pex-clk2"),
3991 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CLK1, "pex-clk1"),
3992 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_EQOS, "eqos"),
3993 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CLK_2_BIAS, "pex-clk-2-bias"),
3994 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CLK_2, "pex-clk-2"),
3995 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DAP3, "dap3"),
3996 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DAP5, "dap5"),
3997 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_UART, "uart"),
3998 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PWR_CTL, "pwr-ctl"),
3999 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SOC_GPIO53, "soc-gpio53"),
4000 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_AUDIO, "audio"),
4001 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_GP_PWM2, "gp-pwm2"),
4002 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_GP_PWM3, "gp-pwm3"),
4003 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SOC_GPIO12, "soc-gpio12"),
4004 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SOC_GPIO13, "soc-gpio13"),
4005 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SOC_GPIO10, "soc-gpio10"),
4006 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_UART4, "uart4"),
4007 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_UART5, "uart5"),
4008 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DBG, "dbg"),
4009 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_HDMI_DP3, "hdmi-dp3"),
4010 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_HDMI_DP2, "hdmi-dp2"),
4011 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_HDMI_DP0, "hdmi-dp0"),
4012 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_HDMI_DP1, "hdmi-dp1"),
4013 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CNTRL, "pex-cntrl"),
4014 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CTL2, "pex-ctl2"),
4015 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_L0_RST, "pex-l0-rst"),
4016 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_L1_RST, "pex-l1-rst"),
4017 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SDMMC4, "sdmmc4"),
4018 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_L5_RST, "pex-l5-rst"),
4019 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CAM, "cam"),
4020 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIC, "csic"),
4021 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSID, "csid"),
4022 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIE, "csie"),
4023 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIF, "csif"),
4024 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SPI, "spi"),
4025 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_UFS, "ufs"),
4026 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIG, "csig"),
4027 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIH, "csih"),
4028 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_EDP, "edp"),
4029 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SDMMC1_HV, "sdmmc1-hv"),
4030 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SDMMC3_HV, "sdmmc3-hv"),
4031 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CONN, "conn"),
4032 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_AUDIO_HV, "audio-hv"),
4033 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_AO_HV, "ao-hv"),
4034 };
4035 
4036 static const struct tegra_pmc_regs tegra194_pmc_regs = {
4037 	.scratch0 = 0x2000,
4038 	.rst_status = 0x70,
4039 	.rst_source_shift = 0x2,
4040 	.rst_source_mask = 0x7c,
4041 	.rst_level_shift = 0x0,
4042 	.rst_level_mask = 0x3,
4043 };
4044 
4045 static const char * const tegra194_reset_sources[] = {
4046 	"SYS_RESET_N",
4047 	"AOWDT",
4048 	"BCCPLEXWDT",
4049 	"BPMPWDT",
4050 	"SCEWDT",
4051 	"SPEWDT",
4052 	"APEWDT",
4053 	"LCCPLEXWDT",
4054 	"SENSOR",
4055 	"AOTAG",
4056 	"VFSENSOR",
4057 	"MAINSWRST",
4058 	"SC7",
4059 	"HSM",
4060 	"CSITE",
4061 	"RCEWDT",
4062 	"PVA0WDT",
4063 	"PVA1WDT",
4064 	"L1A_ASYNC",
4065 	"BPMPBOOT",
4066 	"FUSECRC",
4067 };
4068 
4069 static const struct tegra_wake_event tegra194_wake_events[] = {
4070 	TEGRA_WAKE_GPIO("eqos", 20, 0, TEGRA194_MAIN_GPIO(G, 4)),
4071 	TEGRA_WAKE_IRQ("pmu", 24, 209),
4072 	TEGRA_WAKE_GPIO("power", 29, 1, TEGRA194_AON_GPIO(EE, 4)),
4073 	TEGRA_WAKE_IRQ("rtc", 73, 10),
4074 	TEGRA_WAKE_SIMPLE("usb3-port-0", 76),
4075 	TEGRA_WAKE_SIMPLE("usb3-port-1", 77),
4076 	TEGRA_WAKE_SIMPLE("usb3-port-2-3", 78),
4077 	TEGRA_WAKE_SIMPLE("usb2-port-0", 79),
4078 	TEGRA_WAKE_SIMPLE("usb2-port-1", 80),
4079 	TEGRA_WAKE_SIMPLE("usb2-port-2", 81),
4080 	TEGRA_WAKE_SIMPLE("usb2-port-3", 82),
4081 };
4082 
4083 static const struct tegra_pmc_soc tegra194_pmc_soc = {
4084 	.supports_core_domain = false,
4085 	.num_powergates = 0,
4086 	.powergates = NULL,
4087 	.num_cpu_powergates = 0,
4088 	.cpu_powergates = NULL,
4089 	.has_tsense_reset = false,
4090 	.has_gpu_clamps = false,
4091 	.needs_mbist_war = false,
4092 	.has_impl_33v_pwr = true,
4093 	.maybe_tz_only = false,
4094 	.num_io_pads = ARRAY_SIZE(tegra194_io_pads),
4095 	.io_pads = tegra194_io_pads,
4096 	.num_pin_descs = ARRAY_SIZE(tegra194_pin_descs),
4097 	.pin_descs = tegra194_pin_descs,
4098 	.regs = &tegra194_pmc_regs,
4099 	.init = tegra186_pmc_init,
4100 	.setup_irq_polarity = tegra186_pmc_setup_irq_polarity,
4101 	.set_wake_filters = tegra186_pmc_set_wake_filters,
4102 	.irq_set_wake = tegra186_pmc_irq_set_wake,
4103 	.irq_set_type = tegra186_pmc_irq_set_type,
4104 	.reset_sources = tegra194_reset_sources,
4105 	.num_reset_sources = ARRAY_SIZE(tegra194_reset_sources),
4106 	.reset_levels = tegra186_reset_levels,
4107 	.num_reset_levels = ARRAY_SIZE(tegra186_reset_levels),
4108 	.num_wake_events = ARRAY_SIZE(tegra194_wake_events),
4109 	.wake_events = tegra194_wake_events,
4110 	.max_wake_events = 96,
4111 	.max_wake_vectors = 3,
4112 	.pmc_clks_data = NULL,
4113 	.num_pmc_clks = 0,
4114 	.has_blink_output = false,
4115 	.has_usb_sleepwalk = false,
4116 	.has_single_mmio_aperture = false,
4117 };
4118 
4119 static const struct tegra_io_pad_soc tegra234_io_pads[] = {
4120 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIA, 0, 0xe0c0, 0xe0c4, UINT_MAX, "csia"),
4121 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIB, 1, 0xe0c0, 0xe0c4, UINT_MAX, "csib"),
4122 	TEGRA_IO_PAD(TEGRA_IO_PAD_HDMI_DP0, 0, 0xe0d0, 0xe0d4, UINT_MAX, "hdmi-dp0"),
4123 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIC, 2, 0xe0c0, 0xe0c4, UINT_MAX, "csic"),
4124 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSID, 3, 0xe0c0, 0xe0c4, UINT_MAX, "csid"),
4125 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIE, 4, 0xe0c0, 0xe0c4, UINT_MAX, "csie"),
4126 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIF, 5, 0xe0c0, 0xe0c4, UINT_MAX, "csif"),
4127 	TEGRA_IO_PAD(TEGRA_IO_PAD_UFS, 0, 0xe064, 0xe068, UINT_MAX, "ufs"),
4128 	TEGRA_IO_PAD(TEGRA_IO_PAD_EDP, 1, 0xe05c, 0xe060, UINT_MAX, "edp"),
4129 	TEGRA_IO_PAD(TEGRA_IO_PAD_SDMMC1_HV, 0, 0xe054, 0xe058, 4, "sdmmc1-hv"),
4130 	TEGRA_IO_PAD(TEGRA_IO_PAD_SDMMC3_HV, UINT_MAX, UINT_MAX, UINT_MAX, 6, "sdmmc3-hv"),
4131 	TEGRA_IO_PAD(TEGRA_IO_PAD_AUDIO_HV, UINT_MAX, UINT_MAX, UINT_MAX, 1, "audio-hv"),
4132 	TEGRA_IO_PAD(TEGRA_IO_PAD_AO_HV, UINT_MAX, UINT_MAX, UINT_MAX, 0, "ao-hv"),
4133 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIG, 6, 0xe0c0, 0xe0c4, UINT_MAX, "csig"),
4134 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIH, 7, 0xe0c0, 0xe0c4, UINT_MAX, "csih"),
4135 };
4136 
4137 static const struct pinctrl_pin_desc tegra234_pin_descs[] = {
4138 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIA, "csia"),
4139 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIB, "csib"),
4140 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_HDMI_DP0, "hdmi-dp0"),
4141 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIC, "csic"),
4142 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSID, "csid"),
4143 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIE, "csie"),
4144 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIF, "csif"),
4145 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_UFS, "ufs"),
4146 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_EDP, "edp"),
4147 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SDMMC1_HV, "sdmmc1-hv"),
4148 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SDMMC3_HV, "sdmmc3-hv"),
4149 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_AUDIO_HV, "audio-hv"),
4150 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_AO_HV, "ao-hv"),
4151 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIG, "csig"),
4152 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIH, "csih"),
4153 };
4154 
4155 static const struct tegra_pmc_regs tegra234_pmc_regs = {
4156 	.scratch0 = 0x2000,
4157 	.rst_status = 0x70,
4158 	.rst_source_shift = 0x2,
4159 	.rst_source_mask = 0xfc,
4160 	.rst_level_shift = 0x0,
4161 	.rst_level_mask = 0x3,
4162 };
4163 
4164 static const char * const tegra234_reset_sources[] = {
4165 	"SYS_RESET_N",	/* 0x0 */
4166 	"AOWDT",
4167 	"BCCPLEXWDT",
4168 	"BPMPWDT",
4169 	"SCEWDT",
4170 	"SPEWDT",
4171 	"APEWDT",
4172 	"LCCPLEXWDT",
4173 	"SENSOR",	/* 0x8 */
4174 	NULL,
4175 	NULL,
4176 	"MAINSWRST",
4177 	"SC7",
4178 	"HSM",
4179 	NULL,
4180 	"RCEWDT",
4181 	NULL,		/* 0x10 */
4182 	NULL,
4183 	NULL,
4184 	"BPMPBOOT",
4185 	"FUSECRC",
4186 	"DCEWDT",
4187 	"PSCWDT",
4188 	"PSC",
4189 	"CSITE_SW",	/* 0x18 */
4190 	"POD",
4191 	"SCPM",
4192 	"VREFRO_POWERBAD",
4193 	"VMON",
4194 	"FMON",
4195 	"FSI_R5WDT",
4196 	"FSI_THERM",
4197 	"FSI_R52C0WDT",	/* 0x20 */
4198 	"FSI_R52C1WDT",
4199 	"FSI_R52C2WDT",
4200 	"FSI_R52C3WDT",
4201 	"FSI_FMON",
4202 	"FSI_VMON",	/* 0x25 */
4203 };
4204 
4205 static const struct tegra_wake_event tegra234_wake_events[] = {
4206 	TEGRA_WAKE_GPIO("sd-wake", 8, 0, TEGRA234_MAIN_GPIO(G, 7)),
4207 	TEGRA_WAKE_GPIO("eqos", 20, 0, TEGRA234_MAIN_GPIO(G, 4)),
4208 	TEGRA_WAKE_IRQ("pmu", 24, 209),
4209 	TEGRA_WAKE_GPIO("power", 29, 1, TEGRA234_AON_GPIO(EE, 4)),
4210 	TEGRA_WAKE_GPIO("mgbe", 56, 0, TEGRA234_MAIN_GPIO(Y, 3)),
4211 	TEGRA_WAKE_IRQ("rtc", 73, 10),
4212 	TEGRA_WAKE_IRQ("sw-wake", SW_WAKE_ID, 179),
4213 };
4214 
4215 static const struct tegra_pmc_soc tegra234_pmc_soc = {
4216 	.supports_core_domain = false,
4217 	.num_powergates = 0,
4218 	.powergates = NULL,
4219 	.num_cpu_powergates = 0,
4220 	.cpu_powergates = NULL,
4221 	.has_tsense_reset = false,
4222 	.has_gpu_clamps = false,
4223 	.needs_mbist_war = false,
4224 	.has_impl_33v_pwr = true,
4225 	.maybe_tz_only = false,
4226 	.num_io_pads = ARRAY_SIZE(tegra234_io_pads),
4227 	.io_pads = tegra234_io_pads,
4228 	.num_pin_descs = ARRAY_SIZE(tegra234_pin_descs),
4229 	.pin_descs = tegra234_pin_descs,
4230 	.regs = &tegra234_pmc_regs,
4231 	.init = tegra186_pmc_init,
4232 	.setup_irq_polarity = tegra186_pmc_setup_irq_polarity,
4233 	.set_wake_filters = tegra186_pmc_set_wake_filters,
4234 	.irq_set_wake = tegra186_pmc_irq_set_wake,
4235 	.irq_set_type = tegra186_pmc_irq_set_type,
4236 	.reset_sources = tegra234_reset_sources,
4237 	.num_reset_sources = ARRAY_SIZE(tegra234_reset_sources),
4238 	.reset_levels = tegra186_reset_levels,
4239 	.num_reset_levels = ARRAY_SIZE(tegra186_reset_levels),
4240 	.num_wake_events = ARRAY_SIZE(tegra234_wake_events),
4241 	.wake_events = tegra234_wake_events,
4242 	.max_wake_events = 96,
4243 	.max_wake_vectors = 3,
4244 	.pmc_clks_data = NULL,
4245 	.num_pmc_clks = 0,
4246 	.has_blink_output = false,
4247 	.has_single_mmio_aperture = false,
4248 };
4249 
4250 static const struct of_device_id tegra_pmc_match[] = {
4251 	{ .compatible = "nvidia,tegra234-pmc", .data = &tegra234_pmc_soc },
4252 	{ .compatible = "nvidia,tegra194-pmc", .data = &tegra194_pmc_soc },
4253 	{ .compatible = "nvidia,tegra186-pmc", .data = &tegra186_pmc_soc },
4254 	{ .compatible = "nvidia,tegra210-pmc", .data = &tegra210_pmc_soc },
4255 	{ .compatible = "nvidia,tegra132-pmc", .data = &tegra124_pmc_soc },
4256 	{ .compatible = "nvidia,tegra124-pmc", .data = &tegra124_pmc_soc },
4257 	{ .compatible = "nvidia,tegra114-pmc", .data = &tegra114_pmc_soc },
4258 	{ .compatible = "nvidia,tegra30-pmc", .data = &tegra30_pmc_soc },
4259 	{ .compatible = "nvidia,tegra20-pmc", .data = &tegra20_pmc_soc },
4260 	{ }
4261 };
4262 
tegra_pmc_sync_state(struct device * dev)4263 static void tegra_pmc_sync_state(struct device *dev)
4264 {
4265 	int err;
4266 
4267 	/*
4268 	 * Newer device-trees have power domains, but we need to prepare all
4269 	 * device drivers with runtime PM and OPP support first, otherwise
4270 	 * state syncing is unsafe.
4271 	 */
4272 	if (!pmc->soc->supports_core_domain)
4273 		return;
4274 
4275 	/*
4276 	 * Older device-trees don't have core PD, and thus, there are
4277 	 * no dependencies that will block the state syncing. We shouldn't
4278 	 * mark the domain as synced in this case.
4279 	 */
4280 	if (!pmc->core_domain_registered)
4281 		return;
4282 
4283 	pmc->core_domain_state_synced = true;
4284 
4285 	/* this is a no-op if core regulator isn't used */
4286 	mutex_lock(&pmc->powergates_lock);
4287 	err = dev_pm_opp_sync_regulators(dev);
4288 	mutex_unlock(&pmc->powergates_lock);
4289 
4290 	if (err)
4291 		dev_err(dev, "failed to sync regulators: %d\n", err);
4292 }
4293 
4294 static struct platform_driver tegra_pmc_driver = {
4295 	.driver = {
4296 		.name = "tegra-pmc",
4297 		.suppress_bind_attrs = true,
4298 		.of_match_table = tegra_pmc_match,
4299 #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM)
4300 		.pm = &tegra_pmc_pm_ops,
4301 #endif
4302 		.sync_state = tegra_pmc_sync_state,
4303 	},
4304 	.probe = tegra_pmc_probe,
4305 };
4306 builtin_platform_driver(tegra_pmc_driver);
4307 
tegra_pmc_detect_tz_only(struct tegra_pmc * pmc)4308 static bool __init tegra_pmc_detect_tz_only(struct tegra_pmc *pmc)
4309 {
4310 	u32 value, saved;
4311 
4312 	saved = readl(pmc->base + pmc->soc->regs->scratch0);
4313 	value = saved ^ 0xffffffff;
4314 
4315 	if (value == 0xffffffff)
4316 		value = 0xdeadbeef;
4317 
4318 	/* write pattern and read it back */
4319 	writel(value, pmc->base + pmc->soc->regs->scratch0);
4320 	value = readl(pmc->base + pmc->soc->regs->scratch0);
4321 
4322 	/* if we read all-zeroes, access is restricted to TZ only */
4323 	if (value == 0) {
4324 		pr_info("access to PMC is restricted to TZ\n");
4325 		return true;
4326 	}
4327 
4328 	/* restore original value */
4329 	writel(saved, pmc->base + pmc->soc->regs->scratch0);
4330 
4331 	return false;
4332 }
4333 
4334 /*
4335  * Early initialization to allow access to registers in the very early boot
4336  * process.
4337  */
tegra_pmc_early_init(void)4338 static int __init tegra_pmc_early_init(void)
4339 {
4340 	const struct of_device_id *match;
4341 	struct device_node *np;
4342 	struct resource regs;
4343 	unsigned int i;
4344 	bool invert;
4345 
4346 	mutex_init(&pmc->powergates_lock);
4347 
4348 	np = of_find_matching_node_and_match(NULL, tegra_pmc_match, &match);
4349 	if (!np) {
4350 		/*
4351 		 * Fall back to legacy initialization for 32-bit ARM only. All
4352 		 * 64-bit ARM device tree files for Tegra are required to have
4353 		 * a PMC node.
4354 		 *
4355 		 * This is for backwards-compatibility with old device trees
4356 		 * that didn't contain a PMC node. Note that in this case the
4357 		 * SoC data can't be matched and therefore powergating is
4358 		 * disabled.
4359 		 */
4360 		if (IS_ENABLED(CONFIG_ARM) && soc_is_tegra()) {
4361 			pr_warn("DT node not found, powergating disabled\n");
4362 
4363 			regs.start = 0x7000e400;
4364 			regs.end = 0x7000e7ff;
4365 			regs.flags = IORESOURCE_MEM;
4366 
4367 			pr_warn("Using memory region %pR\n", &regs);
4368 		} else {
4369 			/*
4370 			 * At this point we're not running on Tegra, so play
4371 			 * nice with multi-platform kernels.
4372 			 */
4373 			return 0;
4374 		}
4375 	} else {
4376 		/*
4377 		 * Extract information from the device tree if we've found a
4378 		 * matching node.
4379 		 */
4380 		if (of_address_to_resource(np, 0, &regs) < 0) {
4381 			pr_err("failed to get PMC registers\n");
4382 			of_node_put(np);
4383 			return -ENXIO;
4384 		}
4385 	}
4386 
4387 	pmc->base = ioremap(regs.start, resource_size(&regs));
4388 	if (!pmc->base) {
4389 		pr_err("failed to map PMC registers\n");
4390 		of_node_put(np);
4391 		return -ENXIO;
4392 	}
4393 
4394 	if (of_device_is_available(np)) {
4395 		pmc->soc = match->data;
4396 
4397 		if (pmc->soc->maybe_tz_only)
4398 			pmc->tz_only = tegra_pmc_detect_tz_only(pmc);
4399 
4400 		/* Create a bitmap of the available and valid partitions */
4401 		for (i = 0; i < pmc->soc->num_powergates; i++)
4402 			if (pmc->soc->powergates[i])
4403 				set_bit(i, pmc->powergates_available);
4404 
4405 		/*
4406 		 * Invert the interrupt polarity if a PMC device tree node
4407 		 * exists and contains the nvidia,invert-interrupt property.
4408 		 */
4409 		invert = of_property_read_bool(np, "nvidia,invert-interrupt");
4410 
4411 		pmc->soc->setup_irq_polarity(pmc, np, invert);
4412 
4413 		of_node_put(np);
4414 	}
4415 
4416 	return 0;
4417 }
4418 early_initcall(tegra_pmc_early_init);
4419