xref: /linux/drivers/soc/tegra/pmc.c (revision 2bd1bea5fa6aa79bc563a57919730eb809651b28)
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_create_hierarchy(parent, 0, 96,
2504 						  of_fwnode_handle(pmc->dev->of_node),
2505 						  &tegra_pmc_irq_domain_ops, pmc);
2506 	if (!pmc->domain) {
2507 		dev_err(pmc->dev, "failed to allocate domain\n");
2508 		return -ENOMEM;
2509 	}
2510 
2511 	return 0;
2512 }
2513 
tegra_pmc_clk_notify_cb(struct notifier_block * nb,unsigned long action,void * ptr)2514 static int tegra_pmc_clk_notify_cb(struct notifier_block *nb,
2515 				   unsigned long action, void *ptr)
2516 {
2517 	struct tegra_pmc *pmc = container_of(nb, struct tegra_pmc, clk_nb);
2518 	struct clk_notifier_data *data = ptr;
2519 
2520 	switch (action) {
2521 	case PRE_RATE_CHANGE:
2522 		mutex_lock(&pmc->powergates_lock);
2523 		break;
2524 
2525 	case POST_RATE_CHANGE:
2526 		pmc->rate = data->new_rate;
2527 		fallthrough;
2528 
2529 	case ABORT_RATE_CHANGE:
2530 		mutex_unlock(&pmc->powergates_lock);
2531 		break;
2532 
2533 	default:
2534 		WARN_ON_ONCE(1);
2535 		return notifier_from_errno(-EINVAL);
2536 	}
2537 
2538 	return NOTIFY_OK;
2539 }
2540 
pmc_clk_fence_udelay(u32 offset)2541 static void pmc_clk_fence_udelay(u32 offset)
2542 {
2543 	tegra_pmc_readl(pmc, offset);
2544 	/* pmc clk propagation delay 2 us */
2545 	udelay(2);
2546 }
2547 
pmc_clk_mux_get_parent(struct clk_hw * hw)2548 static u8 pmc_clk_mux_get_parent(struct clk_hw *hw)
2549 {
2550 	struct pmc_clk *clk = to_pmc_clk(hw);
2551 	u32 val;
2552 
2553 	val = tegra_pmc_readl(pmc, clk->offs) >> clk->mux_shift;
2554 	val &= PMC_CLK_OUT_MUX_MASK;
2555 
2556 	return val;
2557 }
2558 
pmc_clk_mux_set_parent(struct clk_hw * hw,u8 index)2559 static int pmc_clk_mux_set_parent(struct clk_hw *hw, u8 index)
2560 {
2561 	struct pmc_clk *clk = to_pmc_clk(hw);
2562 	u32 val;
2563 
2564 	val = tegra_pmc_readl(pmc, clk->offs);
2565 	val &= ~(PMC_CLK_OUT_MUX_MASK << clk->mux_shift);
2566 	val |= index << clk->mux_shift;
2567 	tegra_pmc_writel(pmc, val, clk->offs);
2568 	pmc_clk_fence_udelay(clk->offs);
2569 
2570 	return 0;
2571 }
2572 
pmc_clk_is_enabled(struct clk_hw * hw)2573 static int pmc_clk_is_enabled(struct clk_hw *hw)
2574 {
2575 	struct pmc_clk *clk = to_pmc_clk(hw);
2576 	u32 val;
2577 
2578 	val = tegra_pmc_readl(pmc, clk->offs) & BIT(clk->force_en_shift);
2579 
2580 	return val ? 1 : 0;
2581 }
2582 
pmc_clk_set_state(unsigned long offs,u32 shift,int state)2583 static void pmc_clk_set_state(unsigned long offs, u32 shift, int state)
2584 {
2585 	u32 val;
2586 
2587 	val = tegra_pmc_readl(pmc, offs);
2588 	val = state ? (val | BIT(shift)) : (val & ~BIT(shift));
2589 	tegra_pmc_writel(pmc, val, offs);
2590 	pmc_clk_fence_udelay(offs);
2591 }
2592 
pmc_clk_enable(struct clk_hw * hw)2593 static int pmc_clk_enable(struct clk_hw *hw)
2594 {
2595 	struct pmc_clk *clk = to_pmc_clk(hw);
2596 
2597 	pmc_clk_set_state(clk->offs, clk->force_en_shift, 1);
2598 
2599 	return 0;
2600 }
2601 
pmc_clk_disable(struct clk_hw * hw)2602 static void pmc_clk_disable(struct clk_hw *hw)
2603 {
2604 	struct pmc_clk *clk = to_pmc_clk(hw);
2605 
2606 	pmc_clk_set_state(clk->offs, clk->force_en_shift, 0);
2607 }
2608 
2609 static const struct clk_ops pmc_clk_ops = {
2610 	.get_parent = pmc_clk_mux_get_parent,
2611 	.set_parent = pmc_clk_mux_set_parent,
2612 	.determine_rate = __clk_mux_determine_rate,
2613 	.is_enabled = pmc_clk_is_enabled,
2614 	.enable = pmc_clk_enable,
2615 	.disable = pmc_clk_disable,
2616 };
2617 
2618 static struct clk *
tegra_pmc_clk_out_register(struct tegra_pmc * pmc,const struct pmc_clk_init_data * data,unsigned long offset)2619 tegra_pmc_clk_out_register(struct tegra_pmc *pmc,
2620 			   const struct pmc_clk_init_data *data,
2621 			   unsigned long offset)
2622 {
2623 	struct clk_init_data init;
2624 	struct pmc_clk *pmc_clk;
2625 
2626 	pmc_clk = devm_kzalloc(pmc->dev, sizeof(*pmc_clk), GFP_KERNEL);
2627 	if (!pmc_clk)
2628 		return ERR_PTR(-ENOMEM);
2629 
2630 	init.name = data->name;
2631 	init.ops = &pmc_clk_ops;
2632 	init.parent_names = data->parents;
2633 	init.num_parents = data->num_parents;
2634 	init.flags = CLK_SET_RATE_NO_REPARENT | CLK_SET_RATE_PARENT |
2635 		     CLK_SET_PARENT_GATE;
2636 
2637 	pmc_clk->hw.init = &init;
2638 	pmc_clk->offs = offset;
2639 	pmc_clk->mux_shift = data->mux_shift;
2640 	pmc_clk->force_en_shift = data->force_en_shift;
2641 
2642 	return clk_register(NULL, &pmc_clk->hw);
2643 }
2644 
pmc_clk_gate_is_enabled(struct clk_hw * hw)2645 static int pmc_clk_gate_is_enabled(struct clk_hw *hw)
2646 {
2647 	struct pmc_clk_gate *gate = to_pmc_clk_gate(hw);
2648 
2649 	return tegra_pmc_readl(pmc, gate->offs) & BIT(gate->shift) ? 1 : 0;
2650 }
2651 
pmc_clk_gate_enable(struct clk_hw * hw)2652 static int pmc_clk_gate_enable(struct clk_hw *hw)
2653 {
2654 	struct pmc_clk_gate *gate = to_pmc_clk_gate(hw);
2655 
2656 	pmc_clk_set_state(gate->offs, gate->shift, 1);
2657 
2658 	return 0;
2659 }
2660 
pmc_clk_gate_disable(struct clk_hw * hw)2661 static void pmc_clk_gate_disable(struct clk_hw *hw)
2662 {
2663 	struct pmc_clk_gate *gate = to_pmc_clk_gate(hw);
2664 
2665 	pmc_clk_set_state(gate->offs, gate->shift, 0);
2666 }
2667 
2668 static const struct clk_ops pmc_clk_gate_ops = {
2669 	.is_enabled = pmc_clk_gate_is_enabled,
2670 	.enable = pmc_clk_gate_enable,
2671 	.disable = pmc_clk_gate_disable,
2672 };
2673 
2674 static struct clk *
tegra_pmc_clk_gate_register(struct tegra_pmc * pmc,const char * name,const char * parent_name,unsigned long offset,u32 shift)2675 tegra_pmc_clk_gate_register(struct tegra_pmc *pmc, const char *name,
2676 			    const char *parent_name, unsigned long offset,
2677 			    u32 shift)
2678 {
2679 	struct clk_init_data init;
2680 	struct pmc_clk_gate *gate;
2681 
2682 	gate = devm_kzalloc(pmc->dev, sizeof(*gate), GFP_KERNEL);
2683 	if (!gate)
2684 		return ERR_PTR(-ENOMEM);
2685 
2686 	init.name = name;
2687 	init.ops = &pmc_clk_gate_ops;
2688 	init.parent_names = &parent_name;
2689 	init.num_parents = 1;
2690 	init.flags = 0;
2691 
2692 	gate->hw.init = &init;
2693 	gate->offs = offset;
2694 	gate->shift = shift;
2695 
2696 	return clk_register(NULL, &gate->hw);
2697 }
2698 
tegra_pmc_clock_register(struct tegra_pmc * pmc,struct device_node * np)2699 static void tegra_pmc_clock_register(struct tegra_pmc *pmc,
2700 				     struct device_node *np)
2701 {
2702 	struct clk *clk;
2703 	struct clk_onecell_data *clk_data;
2704 	unsigned int num_clks;
2705 	int i, err;
2706 
2707 	num_clks = pmc->soc->num_pmc_clks;
2708 	if (pmc->soc->has_blink_output)
2709 		num_clks += 1;
2710 
2711 	if (!num_clks)
2712 		return;
2713 
2714 	clk_data = devm_kmalloc(pmc->dev, sizeof(*clk_data), GFP_KERNEL);
2715 	if (!clk_data)
2716 		return;
2717 
2718 	clk_data->clks = devm_kcalloc(pmc->dev, TEGRA_PMC_CLK_MAX,
2719 				      sizeof(*clk_data->clks), GFP_KERNEL);
2720 	if (!clk_data->clks)
2721 		return;
2722 
2723 	clk_data->clk_num = TEGRA_PMC_CLK_MAX;
2724 
2725 	for (i = 0; i < TEGRA_PMC_CLK_MAX; i++)
2726 		clk_data->clks[i] = ERR_PTR(-ENOENT);
2727 
2728 	for (i = 0; i < pmc->soc->num_pmc_clks; i++) {
2729 		const struct pmc_clk_init_data *data;
2730 
2731 		data = pmc->soc->pmc_clks_data + i;
2732 
2733 		clk = tegra_pmc_clk_out_register(pmc, data, PMC_CLK_OUT_CNTRL);
2734 		if (IS_ERR(clk)) {
2735 			dev_warn(pmc->dev, "unable to register clock %s: %d\n",
2736 				 data->name, PTR_ERR_OR_ZERO(clk));
2737 			return;
2738 		}
2739 
2740 		err = clk_register_clkdev(clk, data->name, NULL);
2741 		if (err) {
2742 			dev_warn(pmc->dev,
2743 				 "unable to register %s clock lookup: %d\n",
2744 				 data->name, err);
2745 			return;
2746 		}
2747 
2748 		clk_data->clks[data->clk_id] = clk;
2749 	}
2750 
2751 	if (pmc->soc->has_blink_output) {
2752 		tegra_pmc_writel(pmc, 0x0, PMC_BLINK_TIMER);
2753 		clk = tegra_pmc_clk_gate_register(pmc,
2754 						  "pmc_blink_override",
2755 						  "clk_32k",
2756 						  PMC_DPD_PADS_ORIDE,
2757 						  PMC_DPD_PADS_ORIDE_BLINK);
2758 		if (IS_ERR(clk)) {
2759 			dev_warn(pmc->dev,
2760 				 "unable to register pmc_blink_override: %d\n",
2761 				 PTR_ERR_OR_ZERO(clk));
2762 			return;
2763 		}
2764 
2765 		clk = tegra_pmc_clk_gate_register(pmc, "pmc_blink",
2766 						  "pmc_blink_override",
2767 						  PMC_CNTRL,
2768 						  PMC_CNTRL_BLINK_EN);
2769 		if (IS_ERR(clk)) {
2770 			dev_warn(pmc->dev,
2771 				 "unable to register pmc_blink: %d\n",
2772 				 PTR_ERR_OR_ZERO(clk));
2773 			return;
2774 		}
2775 
2776 		err = clk_register_clkdev(clk, "pmc_blink", NULL);
2777 		if (err) {
2778 			dev_warn(pmc->dev,
2779 				 "unable to register pmc_blink lookup: %d\n",
2780 				 err);
2781 			return;
2782 		}
2783 
2784 		clk_data->clks[TEGRA_PMC_CLK_BLINK] = clk;
2785 	}
2786 
2787 	err = of_clk_add_provider(np, of_clk_src_onecell_get, clk_data);
2788 	if (err)
2789 		dev_warn(pmc->dev, "failed to add pmc clock provider: %d\n",
2790 			 err);
2791 }
2792 
2793 static const struct regmap_range pmc_usb_sleepwalk_ranges[] = {
2794 	regmap_reg_range(PMC_USB_DEBOUNCE_DEL, PMC_USB_AO),
2795 	regmap_reg_range(PMC_UTMIP_UHSIC_TRIGGERS, PMC_UTMIP_UHSIC_SAVED_STATE),
2796 	regmap_reg_range(PMC_UTMIP_TERM_PAD_CFG, PMC_UTMIP_UHSIC_FAKE),
2797 	regmap_reg_range(PMC_UTMIP_UHSIC_LINE_WAKEUP, PMC_UTMIP_UHSIC_LINE_WAKEUP),
2798 	regmap_reg_range(PMC_UTMIP_BIAS_MASTER_CNTRL, PMC_UTMIP_MASTER_CONFIG),
2799 	regmap_reg_range(PMC_UTMIP_UHSIC2_TRIGGERS, PMC_UTMIP_MASTER2_CONFIG),
2800 	regmap_reg_range(PMC_UTMIP_PAD_CFG0, PMC_UTMIP_UHSIC_SLEEP_CFG1),
2801 	regmap_reg_range(PMC_UTMIP_SLEEPWALK_P3, PMC_UTMIP_SLEEPWALK_P3),
2802 };
2803 
2804 static const struct regmap_access_table pmc_usb_sleepwalk_table = {
2805 	.yes_ranges = pmc_usb_sleepwalk_ranges,
2806 	.n_yes_ranges = ARRAY_SIZE(pmc_usb_sleepwalk_ranges),
2807 };
2808 
tegra_pmc_regmap_readl(void * context,unsigned int offset,unsigned int * value)2809 static int tegra_pmc_regmap_readl(void *context, unsigned int offset, unsigned int *value)
2810 {
2811 	struct tegra_pmc *pmc = context;
2812 
2813 	*value = tegra_pmc_readl(pmc, offset);
2814 	return 0;
2815 }
2816 
tegra_pmc_regmap_writel(void * context,unsigned int offset,unsigned int value)2817 static int tegra_pmc_regmap_writel(void *context, unsigned int offset, unsigned int value)
2818 {
2819 	struct tegra_pmc *pmc = context;
2820 
2821 	tegra_pmc_writel(pmc, value, offset);
2822 	return 0;
2823 }
2824 
2825 static const struct regmap_config usb_sleepwalk_regmap_config = {
2826 	.name = "usb_sleepwalk",
2827 	.reg_bits = 32,
2828 	.val_bits = 32,
2829 	.reg_stride = 4,
2830 	.fast_io = true,
2831 	.rd_table = &pmc_usb_sleepwalk_table,
2832 	.wr_table = &pmc_usb_sleepwalk_table,
2833 	.reg_read = tegra_pmc_regmap_readl,
2834 	.reg_write = tegra_pmc_regmap_writel,
2835 };
2836 
tegra_pmc_regmap_init(struct tegra_pmc * pmc)2837 static int tegra_pmc_regmap_init(struct tegra_pmc *pmc)
2838 {
2839 	struct regmap *regmap;
2840 	int err;
2841 
2842 	if (pmc->soc->has_usb_sleepwalk) {
2843 		regmap = devm_regmap_init(pmc->dev, NULL, pmc, &usb_sleepwalk_regmap_config);
2844 		if (IS_ERR(regmap)) {
2845 			err = PTR_ERR(regmap);
2846 			dev_err(pmc->dev, "failed to allocate register map (%d)\n", err);
2847 			return err;
2848 		}
2849 	}
2850 
2851 	return 0;
2852 }
2853 
tegra_pmc_reset_suspend_mode(void * data)2854 static void tegra_pmc_reset_suspend_mode(void *data)
2855 {
2856 	pmc->suspend_mode = TEGRA_SUSPEND_NOT_READY;
2857 }
2858 
tegra_pmc_probe(struct platform_device * pdev)2859 static int tegra_pmc_probe(struct platform_device *pdev)
2860 {
2861 	void __iomem *base;
2862 	struct resource *res;
2863 	int err;
2864 
2865 	/*
2866 	 * Early initialisation should have configured an initial
2867 	 * register mapping and setup the soc data pointer. If these
2868 	 * are not valid then something went badly wrong!
2869 	 */
2870 	if (WARN_ON(!pmc->base || !pmc->soc))
2871 		return -ENODEV;
2872 
2873 	err = tegra_pmc_parse_dt(pmc, pdev->dev.of_node);
2874 	if (err < 0)
2875 		return err;
2876 
2877 	err = devm_add_action_or_reset(&pdev->dev, tegra_pmc_reset_suspend_mode,
2878 				       NULL);
2879 	if (err)
2880 		return err;
2881 
2882 	/* take over the memory region from the early initialization */
2883 	base = devm_platform_ioremap_resource(pdev, 0);
2884 	if (IS_ERR(base))
2885 		return PTR_ERR(base);
2886 
2887 	if (pmc->soc->has_single_mmio_aperture) {
2888 		pmc->wake = base;
2889 		pmc->aotag = base;
2890 		pmc->scratch = base;
2891 	} else {
2892 		pmc->wake = devm_platform_ioremap_resource_byname(pdev, "wake");
2893 		if (IS_ERR(pmc->wake))
2894 			return PTR_ERR(pmc->wake);
2895 
2896 		pmc->aotag = devm_platform_ioremap_resource_byname(pdev, "aotag");
2897 		if (IS_ERR(pmc->aotag))
2898 			return PTR_ERR(pmc->aotag);
2899 
2900 		/* "scratch" is an optional aperture */
2901 		res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
2902 						"scratch");
2903 		if (res) {
2904 			pmc->scratch = devm_ioremap_resource(&pdev->dev, res);
2905 			if (IS_ERR(pmc->scratch))
2906 				return PTR_ERR(pmc->scratch);
2907 		} else {
2908 			pmc->scratch = NULL;
2909 		}
2910 	}
2911 
2912 	pmc->clk = devm_clk_get_optional(&pdev->dev, "pclk");
2913 	if (IS_ERR(pmc->clk))
2914 		return dev_err_probe(&pdev->dev, PTR_ERR(pmc->clk),
2915 				     "failed to get pclk\n");
2916 
2917 	/*
2918 	 * PMC should be last resort for restarting since it soft-resets
2919 	 * CPU without resetting everything else.
2920 	 */
2921 	if (pmc->scratch) {
2922 		err = devm_register_reboot_notifier(&pdev->dev,
2923 						    &tegra_pmc_reboot_notifier);
2924 		if (err) {
2925 			dev_err(&pdev->dev,
2926 				"unable to register reboot notifier, %d\n",
2927 				err);
2928 			return err;
2929 		}
2930 	}
2931 
2932 	err = devm_register_sys_off_handler(&pdev->dev,
2933 					    SYS_OFF_MODE_RESTART,
2934 					    SYS_OFF_PRIO_LOW,
2935 					    tegra_pmc_restart_handler, NULL);
2936 	if (err) {
2937 		dev_err(&pdev->dev, "failed to register sys-off handler: %d\n",
2938 			err);
2939 		return err;
2940 	}
2941 
2942 	/*
2943 	 * PMC should be primary power-off method if it soft-resets CPU,
2944 	 * asking bootloader to shutdown hardware.
2945 	 */
2946 	err = devm_register_sys_off_handler(&pdev->dev,
2947 					    SYS_OFF_MODE_POWER_OFF,
2948 					    SYS_OFF_PRIO_FIRMWARE,
2949 					    tegra_pmc_power_off_handler, NULL);
2950 	if (err) {
2951 		dev_err(&pdev->dev, "failed to register sys-off handler: %d\n",
2952 			err);
2953 		return err;
2954 	}
2955 
2956 	/*
2957 	 * PCLK clock rate can't be retrieved using CLK API because it
2958 	 * causes lockup if CPU enters LP2 idle state from some other
2959 	 * CLK notifier, hence we're caching the rate's value locally.
2960 	 */
2961 	if (pmc->clk) {
2962 		pmc->clk_nb.notifier_call = tegra_pmc_clk_notify_cb;
2963 		err = devm_clk_notifier_register(&pdev->dev, pmc->clk,
2964 						 &pmc->clk_nb);
2965 		if (err) {
2966 			dev_err(&pdev->dev,
2967 				"failed to register clk notifier\n");
2968 			return err;
2969 		}
2970 
2971 		pmc->rate = clk_get_rate(pmc->clk);
2972 	}
2973 
2974 	pmc->dev = &pdev->dev;
2975 
2976 	err = tegra_pmc_init(pmc);
2977 	if (err < 0) {
2978 		dev_err(&pdev->dev, "failed to initialize PMC: %d\n", err);
2979 		return err;
2980 	}
2981 
2982 	tegra_pmc_init_tsense_reset(pmc);
2983 
2984 	tegra_pmc_reset_sysfs_init(pmc);
2985 
2986 	err = tegra_pmc_pinctrl_init(pmc);
2987 	if (err)
2988 		goto cleanup_sysfs;
2989 
2990 	err = tegra_pmc_regmap_init(pmc);
2991 	if (err < 0)
2992 		goto cleanup_sysfs;
2993 
2994 	err = tegra_powergate_init(pmc, pdev->dev.of_node);
2995 	if (err < 0)
2996 		goto cleanup_powergates;
2997 
2998 	err = tegra_pmc_irq_init(pmc);
2999 	if (err < 0)
3000 		goto cleanup_powergates;
3001 
3002 	mutex_lock(&pmc->powergates_lock);
3003 	iounmap(pmc->base);
3004 	pmc->base = base;
3005 	mutex_unlock(&pmc->powergates_lock);
3006 
3007 	tegra_pmc_clock_register(pmc, pdev->dev.of_node);
3008 	platform_set_drvdata(pdev, pmc);
3009 	tegra_pm_init_suspend();
3010 
3011 	/* Some wakes require specific filter configuration */
3012 	if (pmc->soc->set_wake_filters)
3013 		pmc->soc->set_wake_filters(pmc);
3014 
3015 	debugfs_create_file("powergate", 0444, NULL, NULL, &powergate_fops);
3016 
3017 	return 0;
3018 
3019 cleanup_powergates:
3020 	tegra_powergate_remove_all(pdev->dev.of_node);
3021 cleanup_sysfs:
3022 	device_remove_file(&pdev->dev, &dev_attr_reset_reason);
3023 	device_remove_file(&pdev->dev, &dev_attr_reset_level);
3024 
3025 	return err;
3026 }
3027 
3028 /*
3029  * Ensures that sufficient time is passed for a register write to
3030  * serialize into the 32KHz domain.
3031  */
wke_32kwritel(struct tegra_pmc * pmc,u32 value,unsigned int offset)3032 static void wke_32kwritel(struct tegra_pmc *pmc, u32 value, unsigned int offset)
3033 {
3034 	writel(value, pmc->wake + offset);
3035 	udelay(130);
3036 }
3037 
wke_write_wake_level(struct tegra_pmc * pmc,int wake,int level)3038 static void wke_write_wake_level(struct tegra_pmc *pmc, int wake, int level)
3039 {
3040 	unsigned int offset = WAKE_AOWAKE_CNTRL(wake);
3041 	u32 value;
3042 
3043 	value = readl(pmc->wake + offset);
3044 	if (level)
3045 		value |= WAKE_AOWAKE_CNTRL_LEVEL;
3046 	else
3047 		value &= ~WAKE_AOWAKE_CNTRL_LEVEL;
3048 
3049 	writel(value, pmc->wake + offset);
3050 }
3051 
wke_write_wake_levels(struct tegra_pmc * pmc)3052 static void wke_write_wake_levels(struct tegra_pmc *pmc)
3053 {
3054 	unsigned int i;
3055 
3056 	for (i = 0; i < pmc->soc->max_wake_events; i++)
3057 		wke_write_wake_level(pmc, i, test_bit(i, pmc->wake_cntrl_level_map));
3058 }
3059 
wke_clear_sw_wake_status(struct tegra_pmc * pmc)3060 static void wke_clear_sw_wake_status(struct tegra_pmc *pmc)
3061 {
3062 	wke_32kwritel(pmc, 1, WAKE_AOWAKE_SW_STATUS_W_0);
3063 }
3064 
wke_read_sw_wake_status(struct tegra_pmc * pmc)3065 static void wke_read_sw_wake_status(struct tegra_pmc *pmc)
3066 {
3067 	unsigned long status;
3068 	unsigned int wake, i;
3069 
3070 	for (i = 0; i < pmc->soc->max_wake_events; i++)
3071 		wke_write_wake_level(pmc, i, 0);
3072 
3073 	wke_clear_sw_wake_status(pmc);
3074 
3075 	wke_32kwritel(pmc, 1, WAKE_LATCH_SW);
3076 
3077 	/*
3078 	 * WAKE_AOWAKE_SW_STATUS is edge triggered, so in order to
3079 	 * obtain the current status of the input wake signals, change
3080 	 * the polarity of the wake level from 0->1 while latching to force
3081 	 * a positive edge if the sampled signal is '1'.
3082 	 */
3083 	for (i = 0; i < pmc->soc->max_wake_events; i++)
3084 		wke_write_wake_level(pmc, i, 1);
3085 
3086 	/*
3087 	 * Wait for the update to be synced into the 32kHz domain,
3088 	 * and let enough time lapse, so that the wake signals have time to
3089 	 * be sampled.
3090 	 */
3091 	udelay(300);
3092 
3093 	wke_32kwritel(pmc, 0, WAKE_LATCH_SW);
3094 
3095 	bitmap_zero(pmc->wake_sw_status_map, pmc->soc->max_wake_events);
3096 
3097 	for (i = 0; i < pmc->soc->max_wake_vectors; i++) {
3098 		status = readl(pmc->wake + WAKE_AOWAKE_SW_STATUS(i));
3099 
3100 		for_each_set_bit(wake, &status, 32)
3101 			set_bit(wake + (i * 32), pmc->wake_sw_status_map);
3102 	}
3103 }
3104 
wke_clear_wake_status(struct tegra_pmc * pmc)3105 static void wke_clear_wake_status(struct tegra_pmc *pmc)
3106 {
3107 	unsigned long status;
3108 	unsigned int i, wake;
3109 	u32 mask;
3110 
3111 	for (i = 0; i < pmc->soc->max_wake_vectors; i++) {
3112 		mask = readl(pmc->wake + WAKE_AOWAKE_TIER2_ROUTING(i));
3113 		status = readl(pmc->wake + WAKE_AOWAKE_STATUS_R(i)) & mask;
3114 
3115 		for_each_set_bit(wake, &status, 32)
3116 			wke_32kwritel(pmc, 0x1, WAKE_AOWAKE_STATUS_W((i * 32) + wake));
3117 	}
3118 }
3119 
3120 /* 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)3121 static void tegra186_pmc_process_wake_events(struct tegra_pmc *pmc, unsigned int index,
3122 					     unsigned long status)
3123 {
3124 	unsigned int wake;
3125 
3126 	dev_dbg(pmc->dev, "Wake[%d:%d]  status=%#lx\n", (index * 32) + 31, index * 32, status);
3127 
3128 	for_each_set_bit(wake, &status, 32) {
3129 		irq_hw_number_t hwirq = wake + 32 * index;
3130 		struct irq_desc *desc;
3131 		unsigned int irq;
3132 
3133 		irq = irq_find_mapping(pmc->domain, hwirq);
3134 
3135 		desc = irq_to_desc(irq);
3136 		if (!desc || !desc->action || !desc->action->name) {
3137 			dev_dbg(pmc->dev, "Resume caused by WAKE%ld, IRQ %d\n", hwirq, irq);
3138 			continue;
3139 		}
3140 
3141 		dev_dbg(pmc->dev, "Resume caused by WAKE%ld, %s\n", hwirq, desc->action->name);
3142 		generic_handle_irq(irq);
3143 	}
3144 }
3145 
tegra186_pmc_wake_syscore_resume(void)3146 static void tegra186_pmc_wake_syscore_resume(void)
3147 {
3148 	u32 status, mask;
3149 	unsigned int i;
3150 
3151 	for (i = 0; i < pmc->soc->max_wake_vectors; i++) {
3152 		mask = readl(pmc->wake + WAKE_AOWAKE_TIER2_ROUTING(i));
3153 		status = readl(pmc->wake + WAKE_AOWAKE_STATUS_R(i)) & mask;
3154 
3155 		tegra186_pmc_process_wake_events(pmc, i, status);
3156 	}
3157 }
3158 
tegra186_pmc_wake_syscore_suspend(void)3159 static int tegra186_pmc_wake_syscore_suspend(void)
3160 {
3161 	wke_read_sw_wake_status(pmc);
3162 
3163 	/* flip the wakeup trigger for dual-edge triggered pads
3164 	 * which are currently asserting as wakeups
3165 	 */
3166 	bitmap_andnot(pmc->wake_cntrl_level_map, pmc->wake_type_dual_edge_map,
3167 		      pmc->wake_sw_status_map, pmc->soc->max_wake_events);
3168 	bitmap_or(pmc->wake_cntrl_level_map, pmc->wake_cntrl_level_map,
3169 		  pmc->wake_type_level_map, pmc->soc->max_wake_events);
3170 
3171 	/* Clear PMC Wake Status registers while going to suspend */
3172 	wke_clear_wake_status(pmc);
3173 	wke_write_wake_levels(pmc);
3174 
3175 	return 0;
3176 }
3177 
3178 #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM)
tegra_pmc_suspend(struct device * dev)3179 static int tegra_pmc_suspend(struct device *dev)
3180 {
3181 	struct tegra_pmc *pmc = dev_get_drvdata(dev);
3182 
3183 	tegra_pmc_writel(pmc, virt_to_phys(tegra_resume), PMC_SCRATCH41);
3184 
3185 	return 0;
3186 }
3187 
tegra_pmc_resume(struct device * dev)3188 static int tegra_pmc_resume(struct device *dev)
3189 {
3190 	struct tegra_pmc *pmc = dev_get_drvdata(dev);
3191 
3192 	tegra_pmc_writel(pmc, 0x0, PMC_SCRATCH41);
3193 
3194 	return 0;
3195 }
3196 
3197 static SIMPLE_DEV_PM_OPS(tegra_pmc_pm_ops, tegra_pmc_suspend, tegra_pmc_resume);
3198 
3199 #endif
3200 
3201 static const char * const tegra20_powergates[] = {
3202 	[TEGRA_POWERGATE_CPU] = "cpu",
3203 	[TEGRA_POWERGATE_3D] = "td",
3204 	[TEGRA_POWERGATE_VENC] = "venc",
3205 	[TEGRA_POWERGATE_VDEC] = "vdec",
3206 	[TEGRA_POWERGATE_PCIE] = "pcie",
3207 	[TEGRA_POWERGATE_L2] = "l2",
3208 	[TEGRA_POWERGATE_MPE] = "mpe",
3209 };
3210 
3211 static const struct tegra_pmc_regs tegra20_pmc_regs = {
3212 	.scratch0 = 0x50,
3213 	.rst_status = 0x1b4,
3214 	.rst_source_shift = 0x0,
3215 	.rst_source_mask = 0x7,
3216 	.rst_level_shift = 0x0,
3217 	.rst_level_mask = 0x0,
3218 };
3219 
tegra20_pmc_init(struct tegra_pmc * pmc)3220 static void tegra20_pmc_init(struct tegra_pmc *pmc)
3221 {
3222 	u32 value, osc, pmu, off;
3223 
3224 	/* Always enable CPU power request */
3225 	value = tegra_pmc_readl(pmc, PMC_CNTRL);
3226 	value |= PMC_CNTRL_CPU_PWRREQ_OE;
3227 	tegra_pmc_writel(pmc, value, PMC_CNTRL);
3228 
3229 	value = tegra_pmc_readl(pmc, PMC_CNTRL);
3230 
3231 	if (pmc->sysclkreq_high)
3232 		value &= ~PMC_CNTRL_SYSCLK_POLARITY;
3233 	else
3234 		value |= PMC_CNTRL_SYSCLK_POLARITY;
3235 
3236 	if (pmc->corereq_high)
3237 		value &= ~PMC_CNTRL_PWRREQ_POLARITY;
3238 	else
3239 		value |= PMC_CNTRL_PWRREQ_POLARITY;
3240 
3241 	/* configure the output polarity while the request is tristated */
3242 	tegra_pmc_writel(pmc, value, PMC_CNTRL);
3243 
3244 	/* now enable the request */
3245 	value = tegra_pmc_readl(pmc, PMC_CNTRL);
3246 	value |= PMC_CNTRL_SYSCLK_OE;
3247 	tegra_pmc_writel(pmc, value, PMC_CNTRL);
3248 
3249 	/* program core timings which are applicable only for suspend state */
3250 	if (pmc->suspend_mode != TEGRA_SUSPEND_NONE) {
3251 		osc = DIV_ROUND_UP(pmc->core_osc_time * 8192, 1000000);
3252 		pmu = DIV_ROUND_UP(pmc->core_pmu_time * 32768, 1000000);
3253 		off = DIV_ROUND_UP(pmc->core_off_time * 32768, 1000000);
3254 		tegra_pmc_writel(pmc, ((osc << 8) & 0xff00) | (pmu & 0xff),
3255 				 PMC_COREPWRGOOD_TIMER);
3256 		tegra_pmc_writel(pmc, off, PMC_COREPWROFF_TIMER);
3257 	}
3258 }
3259 
tegra20_pmc_setup_irq_polarity(struct tegra_pmc * pmc,struct device_node * np,bool invert)3260 static void tegra20_pmc_setup_irq_polarity(struct tegra_pmc *pmc,
3261 					   struct device_node *np,
3262 					   bool invert)
3263 {
3264 	u32 value;
3265 
3266 	value = tegra_pmc_readl(pmc, PMC_CNTRL);
3267 
3268 	if (invert)
3269 		value |= PMC_CNTRL_INTR_POLARITY;
3270 	else
3271 		value &= ~PMC_CNTRL_INTR_POLARITY;
3272 
3273 	tegra_pmc_writel(pmc, value, PMC_CNTRL);
3274 }
3275 
3276 static const struct tegra_pmc_soc tegra20_pmc_soc = {
3277 	.supports_core_domain = true,
3278 	.num_powergates = ARRAY_SIZE(tegra20_powergates),
3279 	.powergates = tegra20_powergates,
3280 	.num_cpu_powergates = 0,
3281 	.cpu_powergates = NULL,
3282 	.has_tsense_reset = false,
3283 	.has_gpu_clamps = false,
3284 	.needs_mbist_war = false,
3285 	.has_impl_33v_pwr = false,
3286 	.maybe_tz_only = false,
3287 	.num_io_pads = 0,
3288 	.io_pads = NULL,
3289 	.num_pin_descs = 0,
3290 	.pin_descs = NULL,
3291 	.regs = &tegra20_pmc_regs,
3292 	.init = tegra20_pmc_init,
3293 	.setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
3294 	.powergate_set = tegra20_powergate_set,
3295 	.reset_sources = NULL,
3296 	.num_reset_sources = 0,
3297 	.reset_levels = NULL,
3298 	.num_reset_levels = 0,
3299 	.pmc_clks_data = NULL,
3300 	.num_pmc_clks = 0,
3301 	.has_blink_output = true,
3302 	.has_usb_sleepwalk = true,
3303 	.has_single_mmio_aperture = true,
3304 };
3305 
3306 static const char * const tegra30_powergates[] = {
3307 	[TEGRA_POWERGATE_CPU] = "cpu0",
3308 	[TEGRA_POWERGATE_3D] = "td",
3309 	[TEGRA_POWERGATE_VENC] = "venc",
3310 	[TEGRA_POWERGATE_VDEC] = "vdec",
3311 	[TEGRA_POWERGATE_PCIE] = "pcie",
3312 	[TEGRA_POWERGATE_L2] = "l2",
3313 	[TEGRA_POWERGATE_MPE] = "mpe",
3314 	[TEGRA_POWERGATE_HEG] = "heg",
3315 	[TEGRA_POWERGATE_SATA] = "sata",
3316 	[TEGRA_POWERGATE_CPU1] = "cpu1",
3317 	[TEGRA_POWERGATE_CPU2] = "cpu2",
3318 	[TEGRA_POWERGATE_CPU3] = "cpu3",
3319 	[TEGRA_POWERGATE_CELP] = "celp",
3320 	[TEGRA_POWERGATE_3D1] = "td2",
3321 };
3322 
3323 static const u8 tegra30_cpu_powergates[] = {
3324 	TEGRA_POWERGATE_CPU,
3325 	TEGRA_POWERGATE_CPU1,
3326 	TEGRA_POWERGATE_CPU2,
3327 	TEGRA_POWERGATE_CPU3,
3328 };
3329 
3330 static const char * const tegra30_reset_sources[] = {
3331 	"POWER_ON_RESET",
3332 	"WATCHDOG",
3333 	"SENSOR",
3334 	"SW_MAIN",
3335 	"LP0"
3336 };
3337 
3338 static const struct tegra_pmc_soc tegra30_pmc_soc = {
3339 	.supports_core_domain = true,
3340 	.num_powergates = ARRAY_SIZE(tegra30_powergates),
3341 	.powergates = tegra30_powergates,
3342 	.num_cpu_powergates = ARRAY_SIZE(tegra30_cpu_powergates),
3343 	.cpu_powergates = tegra30_cpu_powergates,
3344 	.has_tsense_reset = true,
3345 	.has_gpu_clamps = false,
3346 	.needs_mbist_war = false,
3347 	.has_impl_33v_pwr = false,
3348 	.maybe_tz_only = false,
3349 	.num_io_pads = 0,
3350 	.io_pads = NULL,
3351 	.num_pin_descs = 0,
3352 	.pin_descs = NULL,
3353 	.regs = &tegra20_pmc_regs,
3354 	.init = tegra20_pmc_init,
3355 	.setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
3356 	.powergate_set = tegra20_powergate_set,
3357 	.reset_sources = tegra30_reset_sources,
3358 	.num_reset_sources = ARRAY_SIZE(tegra30_reset_sources),
3359 	.reset_levels = NULL,
3360 	.num_reset_levels = 0,
3361 	.pmc_clks_data = tegra_pmc_clks_data,
3362 	.num_pmc_clks = ARRAY_SIZE(tegra_pmc_clks_data),
3363 	.has_blink_output = true,
3364 	.has_usb_sleepwalk = true,
3365 	.has_single_mmio_aperture = true,
3366 };
3367 
3368 static const char * const tegra114_powergates[] = {
3369 	[TEGRA_POWERGATE_CPU] = "crail",
3370 	[TEGRA_POWERGATE_3D] = "td",
3371 	[TEGRA_POWERGATE_VENC] = "venc",
3372 	[TEGRA_POWERGATE_VDEC] = "vdec",
3373 	[TEGRA_POWERGATE_MPE] = "mpe",
3374 	[TEGRA_POWERGATE_HEG] = "heg",
3375 	[TEGRA_POWERGATE_CPU1] = "cpu1",
3376 	[TEGRA_POWERGATE_CPU2] = "cpu2",
3377 	[TEGRA_POWERGATE_CPU3] = "cpu3",
3378 	[TEGRA_POWERGATE_CELP] = "celp",
3379 	[TEGRA_POWERGATE_CPU0] = "cpu0",
3380 	[TEGRA_POWERGATE_C0NC] = "c0nc",
3381 	[TEGRA_POWERGATE_C1NC] = "c1nc",
3382 	[TEGRA_POWERGATE_DIS] = "dis",
3383 	[TEGRA_POWERGATE_DISB] = "disb",
3384 	[TEGRA_POWERGATE_XUSBA] = "xusba",
3385 	[TEGRA_POWERGATE_XUSBB] = "xusbb",
3386 	[TEGRA_POWERGATE_XUSBC] = "xusbc",
3387 };
3388 
3389 static const u8 tegra114_cpu_powergates[] = {
3390 	TEGRA_POWERGATE_CPU0,
3391 	TEGRA_POWERGATE_CPU1,
3392 	TEGRA_POWERGATE_CPU2,
3393 	TEGRA_POWERGATE_CPU3,
3394 };
3395 
3396 static const struct tegra_pmc_soc tegra114_pmc_soc = {
3397 	.supports_core_domain = false,
3398 	.num_powergates = ARRAY_SIZE(tegra114_powergates),
3399 	.powergates = tegra114_powergates,
3400 	.num_cpu_powergates = ARRAY_SIZE(tegra114_cpu_powergates),
3401 	.cpu_powergates = tegra114_cpu_powergates,
3402 	.has_tsense_reset = true,
3403 	.has_gpu_clamps = false,
3404 	.needs_mbist_war = false,
3405 	.has_impl_33v_pwr = false,
3406 	.maybe_tz_only = false,
3407 	.num_io_pads = 0,
3408 	.io_pads = NULL,
3409 	.num_pin_descs = 0,
3410 	.pin_descs = NULL,
3411 	.regs = &tegra20_pmc_regs,
3412 	.init = tegra20_pmc_init,
3413 	.setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
3414 	.powergate_set = tegra114_powergate_set,
3415 	.reset_sources = tegra30_reset_sources,
3416 	.num_reset_sources = ARRAY_SIZE(tegra30_reset_sources),
3417 	.reset_levels = NULL,
3418 	.num_reset_levels = 0,
3419 	.pmc_clks_data = tegra_pmc_clks_data,
3420 	.num_pmc_clks = ARRAY_SIZE(tegra_pmc_clks_data),
3421 	.has_blink_output = true,
3422 	.has_usb_sleepwalk = true,
3423 	.has_single_mmio_aperture = true,
3424 };
3425 
3426 static const char * const tegra124_powergates[] = {
3427 	[TEGRA_POWERGATE_CPU] = "crail",
3428 	[TEGRA_POWERGATE_3D] = "3d",
3429 	[TEGRA_POWERGATE_VENC] = "venc",
3430 	[TEGRA_POWERGATE_PCIE] = "pcie",
3431 	[TEGRA_POWERGATE_VDEC] = "vdec",
3432 	[TEGRA_POWERGATE_MPE] = "mpe",
3433 	[TEGRA_POWERGATE_HEG] = "heg",
3434 	[TEGRA_POWERGATE_SATA] = "sata",
3435 	[TEGRA_POWERGATE_CPU1] = "cpu1",
3436 	[TEGRA_POWERGATE_CPU2] = "cpu2",
3437 	[TEGRA_POWERGATE_CPU3] = "cpu3",
3438 	[TEGRA_POWERGATE_CELP] = "celp",
3439 	[TEGRA_POWERGATE_CPU0] = "cpu0",
3440 	[TEGRA_POWERGATE_C0NC] = "c0nc",
3441 	[TEGRA_POWERGATE_C1NC] = "c1nc",
3442 	[TEGRA_POWERGATE_SOR] = "sor",
3443 	[TEGRA_POWERGATE_DIS] = "dis",
3444 	[TEGRA_POWERGATE_DISB] = "disb",
3445 	[TEGRA_POWERGATE_XUSBA] = "xusba",
3446 	[TEGRA_POWERGATE_XUSBB] = "xusbb",
3447 	[TEGRA_POWERGATE_XUSBC] = "xusbc",
3448 	[TEGRA_POWERGATE_VIC] = "vic",
3449 	[TEGRA_POWERGATE_IRAM] = "iram",
3450 };
3451 
3452 static const u8 tegra124_cpu_powergates[] = {
3453 	TEGRA_POWERGATE_CPU0,
3454 	TEGRA_POWERGATE_CPU1,
3455 	TEGRA_POWERGATE_CPU2,
3456 	TEGRA_POWERGATE_CPU3,
3457 };
3458 
3459 #define TEGRA_IO_PAD(_id, _dpd, _request, _status, _voltage, _name)	\
3460 	((struct tegra_io_pad_soc) {					\
3461 		.id		= (_id),				\
3462 		.dpd		= (_dpd),				\
3463 		.request	= (_request),				\
3464 		.status		= (_status),				\
3465 		.voltage	= (_voltage),				\
3466 		.name		= (_name),				\
3467 	})
3468 
3469 #define TEGRA_IO_PIN_DESC(_id, _name)	\
3470 	((struct pinctrl_pin_desc) {	\
3471 		.number	= (_id),	\
3472 		.name	= (_name),	\
3473 	})
3474 
3475 static const struct tegra_io_pad_soc tegra124_io_pads[] = {
3476 	TEGRA_IO_PAD(TEGRA_IO_PAD_AUDIO, 17, 0x1b8, 0x1bc, UINT_MAX, "audio"),
3477 	TEGRA_IO_PAD(TEGRA_IO_PAD_BB, 15, 0x1b8, 0x1bc, UINT_MAX, "bb"),
3478 	TEGRA_IO_PAD(TEGRA_IO_PAD_CAM, 4, 0x1c0, 0x1c4, UINT_MAX, "cam"),
3479 	TEGRA_IO_PAD(TEGRA_IO_PAD_COMP, 22, 0x1b8, 0x1bc, UINT_MAX, "comp"),
3480 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIA, 0, 0x1b8, 0x1bc, UINT_MAX, "csia"),
3481 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIB, 1, 0x1b8, 0x1bc, UINT_MAX, "csib"),
3482 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIE, 12, 0x1c0, 0x1c4, UINT_MAX, "csie"),
3483 	TEGRA_IO_PAD(TEGRA_IO_PAD_DSI, 2, 0x1b8, 0x1bc, UINT_MAX, "dsi"),
3484 	TEGRA_IO_PAD(TEGRA_IO_PAD_DSIB, 7, 0x1c0, 0x1c4, UINT_MAX, "dsib"),
3485 	TEGRA_IO_PAD(TEGRA_IO_PAD_DSIC, 8, 0x1c0, 0x1c4, UINT_MAX, "dsic"),
3486 	TEGRA_IO_PAD(TEGRA_IO_PAD_DSID, 9, 0x1c0, 0x1c4, UINT_MAX, "dsid"),
3487 	TEGRA_IO_PAD(TEGRA_IO_PAD_HDMI, 28, 0x1b8, 0x1bc, UINT_MAX, "hdmi"),
3488 	TEGRA_IO_PAD(TEGRA_IO_PAD_HSIC, 19, 0x1b8, 0x1bc, UINT_MAX, "hsic"),
3489 	TEGRA_IO_PAD(TEGRA_IO_PAD_HV, 6, 0x1c0, 0x1c4, UINT_MAX, "hv"),
3490 	TEGRA_IO_PAD(TEGRA_IO_PAD_LVDS, 25, 0x1c0, 0x1c4, UINT_MAX, "lvds"),
3491 	TEGRA_IO_PAD(TEGRA_IO_PAD_MIPI_BIAS, 3, 0x1b8, 0x1bc, UINT_MAX, "mipi-bias"),
3492 	TEGRA_IO_PAD(TEGRA_IO_PAD_NAND, 13, 0x1b8, 0x1bc, UINT_MAX, "nand"),
3493 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_BIAS, 4, 0x1b8, 0x1bc, UINT_MAX, "pex-bias"),
3494 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CLK1, 5, 0x1b8, 0x1bc, UINT_MAX, "pex-clk1"),
3495 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CLK2, 6, 0x1b8, 0x1bc, UINT_MAX, "pex-clk2"),
3496 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CNTRL, 0, 0x1c0, 0x1c4, UINT_MAX, "pex-cntrl"),
3497 	TEGRA_IO_PAD(TEGRA_IO_PAD_SDMMC1, 1, 0x1c0, 0x1c4, UINT_MAX, "sdmmc1"),
3498 	TEGRA_IO_PAD(TEGRA_IO_PAD_SDMMC3, 2, 0x1c0, 0x1c4, UINT_MAX, "sdmmc3"),
3499 	TEGRA_IO_PAD(TEGRA_IO_PAD_SDMMC4, 3, 0x1c0, 0x1c4, UINT_MAX, "sdmmc4"),
3500 	TEGRA_IO_PAD(TEGRA_IO_PAD_SYS_DDC, 26, 0x1c0, 0x1c4, UINT_MAX, "sys_ddc"),
3501 	TEGRA_IO_PAD(TEGRA_IO_PAD_UART, 14, 0x1b8, 0x1bc, UINT_MAX, "uart"),
3502 	TEGRA_IO_PAD(TEGRA_IO_PAD_USB0, 9, 0x1b8, 0x1bc, UINT_MAX, "usb0"),
3503 	TEGRA_IO_PAD(TEGRA_IO_PAD_USB1, 10, 0x1b8, 0x1bc, UINT_MAX, "usb1"),
3504 	TEGRA_IO_PAD(TEGRA_IO_PAD_USB2, 11, 0x1b8, 0x1bc, UINT_MAX, "usb2"),
3505 	TEGRA_IO_PAD(TEGRA_IO_PAD_USB_BIAS, 12, 0x1b8, 0x1bc, UINT_MAX, "usb_bias"),
3506 };
3507 
3508 static const struct pinctrl_pin_desc tegra124_pin_descs[] = {
3509 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_AUDIO, "audio"),
3510 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_BB, "bb"),
3511 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CAM, "cam"),
3512 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_COMP, "comp"),
3513 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIA, "csia"),
3514 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIB, "csib"),
3515 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIE, "csie"),
3516 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DSI, "dsi"),
3517 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DSIB, "dsib"),
3518 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DSIC, "dsic"),
3519 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DSID, "dsid"),
3520 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_HDMI, "hdmi"),
3521 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_HSIC, "hsic"),
3522 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_HV, "hv"),
3523 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_LVDS, "lvds"),
3524 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_MIPI_BIAS, "mipi-bias"),
3525 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_NAND, "nand"),
3526 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_BIAS, "pex-bias"),
3527 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CLK1, "pex-clk1"),
3528 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CLK2, "pex-clk2"),
3529 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CNTRL, "pex-cntrl"),
3530 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SDMMC1, "sdmmc1"),
3531 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SDMMC3, "sdmmc3"),
3532 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SDMMC4, "sdmmc4"),
3533 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SYS_DDC, "sys_ddc"),
3534 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_UART, "uart"),
3535 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_USB0, "usb0"),
3536 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_USB1, "usb1"),
3537 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_USB2, "usb2"),
3538 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_USB_BIAS, "usb_bias"),
3539 };
3540 
3541 static const struct tegra_pmc_soc tegra124_pmc_soc = {
3542 	.supports_core_domain = false,
3543 	.num_powergates = ARRAY_SIZE(tegra124_powergates),
3544 	.powergates = tegra124_powergates,
3545 	.num_cpu_powergates = ARRAY_SIZE(tegra124_cpu_powergates),
3546 	.cpu_powergates = tegra124_cpu_powergates,
3547 	.has_tsense_reset = true,
3548 	.has_gpu_clamps = true,
3549 	.needs_mbist_war = false,
3550 	.has_impl_33v_pwr = false,
3551 	.maybe_tz_only = false,
3552 	.num_io_pads = ARRAY_SIZE(tegra124_io_pads),
3553 	.io_pads = tegra124_io_pads,
3554 	.num_pin_descs = ARRAY_SIZE(tegra124_pin_descs),
3555 	.pin_descs = tegra124_pin_descs,
3556 	.regs = &tegra20_pmc_regs,
3557 	.init = tegra20_pmc_init,
3558 	.setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
3559 	.powergate_set = tegra114_powergate_set,
3560 	.reset_sources = tegra30_reset_sources,
3561 	.num_reset_sources = ARRAY_SIZE(tegra30_reset_sources),
3562 	.reset_levels = NULL,
3563 	.num_reset_levels = 0,
3564 	.pmc_clks_data = tegra_pmc_clks_data,
3565 	.num_pmc_clks = ARRAY_SIZE(tegra_pmc_clks_data),
3566 	.has_blink_output = true,
3567 	.has_usb_sleepwalk = true,
3568 	.has_single_mmio_aperture = true,
3569 };
3570 
3571 static const char * const tegra210_powergates[] = {
3572 	[TEGRA_POWERGATE_CPU] = "crail",
3573 	[TEGRA_POWERGATE_3D] = "3d",
3574 	[TEGRA_POWERGATE_VENC] = "venc",
3575 	[TEGRA_POWERGATE_PCIE] = "pcie",
3576 	[TEGRA_POWERGATE_MPE] = "mpe",
3577 	[TEGRA_POWERGATE_SATA] = "sata",
3578 	[TEGRA_POWERGATE_CPU1] = "cpu1",
3579 	[TEGRA_POWERGATE_CPU2] = "cpu2",
3580 	[TEGRA_POWERGATE_CPU3] = "cpu3",
3581 	[TEGRA_POWERGATE_CPU0] = "cpu0",
3582 	[TEGRA_POWERGATE_C0NC] = "c0nc",
3583 	[TEGRA_POWERGATE_SOR] = "sor",
3584 	[TEGRA_POWERGATE_DIS] = "dis",
3585 	[TEGRA_POWERGATE_DISB] = "disb",
3586 	[TEGRA_POWERGATE_XUSBA] = "xusba",
3587 	[TEGRA_POWERGATE_XUSBB] = "xusbb",
3588 	[TEGRA_POWERGATE_XUSBC] = "xusbc",
3589 	[TEGRA_POWERGATE_VIC] = "vic",
3590 	[TEGRA_POWERGATE_IRAM] = "iram",
3591 	[TEGRA_POWERGATE_NVDEC] = "nvdec",
3592 	[TEGRA_POWERGATE_NVJPG] = "nvjpg",
3593 	[TEGRA_POWERGATE_AUD] = "aud",
3594 	[TEGRA_POWERGATE_DFD] = "dfd",
3595 	[TEGRA_POWERGATE_VE2] = "ve2",
3596 };
3597 
3598 static const u8 tegra210_cpu_powergates[] = {
3599 	TEGRA_POWERGATE_CPU0,
3600 	TEGRA_POWERGATE_CPU1,
3601 	TEGRA_POWERGATE_CPU2,
3602 	TEGRA_POWERGATE_CPU3,
3603 };
3604 
3605 static const struct tegra_io_pad_soc tegra210_io_pads[] = {
3606 	TEGRA_IO_PAD(TEGRA_IO_PAD_AUDIO, 17, 0x1b8, 0x1bc, 5, "audio"),
3607 	TEGRA_IO_PAD(TEGRA_IO_PAD_AUDIO_HV, 29, 0x1c0, 0x1c4, 18, "audio-hv"),
3608 	TEGRA_IO_PAD(TEGRA_IO_PAD_CAM, 4, 0x1c0, 0x1c4, 10, "cam"),
3609 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIA, 0, 0x1b8, 0x1bc, UINT_MAX, "csia"),
3610 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIB, 1, 0x1b8, 0x1bc, UINT_MAX, "csib"),
3611 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIC, 10, 0x1c0, 0x1c4, UINT_MAX, "csic"),
3612 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSID, 11, 0x1c0, 0x1c4, UINT_MAX, "csid"),
3613 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIE, 12, 0x1c0, 0x1c4, UINT_MAX, "csie"),
3614 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIF, 13, 0x1c0, 0x1c4, UINT_MAX, "csif"),
3615 	TEGRA_IO_PAD(TEGRA_IO_PAD_DBG, 25, 0x1b8, 0x1bc, 19, "dbg"),
3616 	TEGRA_IO_PAD(TEGRA_IO_PAD_DEBUG_NONAO, 26, 0x1b8, 0x1bc, UINT_MAX, "debug-nonao"),
3617 	TEGRA_IO_PAD(TEGRA_IO_PAD_DMIC, 18, 0x1c0, 0x1c4, 20, "dmic"),
3618 	TEGRA_IO_PAD(TEGRA_IO_PAD_DP, 19, 0x1c0, 0x1c4, UINT_MAX, "dp"),
3619 	TEGRA_IO_PAD(TEGRA_IO_PAD_DSI, 2, 0x1b8, 0x1bc, UINT_MAX, "dsi"),
3620 	TEGRA_IO_PAD(TEGRA_IO_PAD_DSIB, 7, 0x1c0, 0x1c4, UINT_MAX, "dsib"),
3621 	TEGRA_IO_PAD(TEGRA_IO_PAD_DSIC, 8, 0x1c0, 0x1c4, UINT_MAX, "dsic"),
3622 	TEGRA_IO_PAD(TEGRA_IO_PAD_DSID, 9, 0x1c0, 0x1c4, UINT_MAX, "dsid"),
3623 	TEGRA_IO_PAD(TEGRA_IO_PAD_EMMC, 3, 0x1c0, 0x1c4, UINT_MAX, "emmc"),
3624 	TEGRA_IO_PAD(TEGRA_IO_PAD_EMMC2, 5, 0x1c0, 0x1c4, UINT_MAX, "emmc2"),
3625 	TEGRA_IO_PAD(TEGRA_IO_PAD_GPIO, 27, 0x1b8, 0x1bc, 21, "gpio"),
3626 	TEGRA_IO_PAD(TEGRA_IO_PAD_HDMI, 28, 0x1b8, 0x1bc, UINT_MAX, "hdmi"),
3627 	TEGRA_IO_PAD(TEGRA_IO_PAD_HSIC, 19, 0x1b8, 0x1bc, UINT_MAX, "hsic"),
3628 	TEGRA_IO_PAD(TEGRA_IO_PAD_LVDS, 25, 0x1c0, 0x1c4, UINT_MAX, "lvds"),
3629 	TEGRA_IO_PAD(TEGRA_IO_PAD_MIPI_BIAS, 3, 0x1b8, 0x1bc, UINT_MAX, "mipi-bias"),
3630 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_BIAS, 4, 0x1b8, 0x1bc, UINT_MAX, "pex-bias"),
3631 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CLK1, 5, 0x1b8, 0x1bc, UINT_MAX, "pex-clk1"),
3632 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CLK2, 6, 0x1b8, 0x1bc, UINT_MAX, "pex-clk2"),
3633 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CNTRL, UINT_MAX, UINT_MAX, UINT_MAX, 11, "pex-cntrl"),
3634 	TEGRA_IO_PAD(TEGRA_IO_PAD_SDMMC1, 1, 0x1c0, 0x1c4, 12, "sdmmc1"),
3635 	TEGRA_IO_PAD(TEGRA_IO_PAD_SDMMC3, 2, 0x1c0, 0x1c4, 13, "sdmmc3"),
3636 	TEGRA_IO_PAD(TEGRA_IO_PAD_SPI, 14, 0x1c0, 0x1c4, 22, "spi"),
3637 	TEGRA_IO_PAD(TEGRA_IO_PAD_SPI_HV, 15, 0x1c0, 0x1c4, 23, "spi-hv"),
3638 	TEGRA_IO_PAD(TEGRA_IO_PAD_UART, 14, 0x1b8, 0x1bc, 2, "uart"),
3639 	TEGRA_IO_PAD(TEGRA_IO_PAD_USB0, 9, 0x1b8, 0x1bc, UINT_MAX, "usb0"),
3640 	TEGRA_IO_PAD(TEGRA_IO_PAD_USB1, 10, 0x1b8, 0x1bc, UINT_MAX, "usb1"),
3641 	TEGRA_IO_PAD(TEGRA_IO_PAD_USB2, 11, 0x1b8, 0x1bc, UINT_MAX, "usb2"),
3642 	TEGRA_IO_PAD(TEGRA_IO_PAD_USB3, 18, 0x1b8, 0x1bc, UINT_MAX, "usb3"),
3643 	TEGRA_IO_PAD(TEGRA_IO_PAD_USB_BIAS, 12, 0x1b8, 0x1bc, UINT_MAX, "usb-bias"),
3644 };
3645 
3646 static const struct pinctrl_pin_desc tegra210_pin_descs[] = {
3647 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_AUDIO, "audio"),
3648 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_AUDIO_HV, "audio-hv"),
3649 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CAM, "cam"),
3650 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIA, "csia"),
3651 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIB, "csib"),
3652 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIC, "csic"),
3653 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSID, "csid"),
3654 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIE, "csie"),
3655 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIF, "csif"),
3656 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DBG, "dbg"),
3657 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DEBUG_NONAO, "debug-nonao"),
3658 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DMIC, "dmic"),
3659 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DP, "dp"),
3660 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DSI, "dsi"),
3661 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DSIB, "dsib"),
3662 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DSIC, "dsic"),
3663 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DSID, "dsid"),
3664 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_EMMC, "emmc"),
3665 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_EMMC2, "emmc2"),
3666 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_GPIO, "gpio"),
3667 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_HDMI, "hdmi"),
3668 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_HSIC, "hsic"),
3669 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_LVDS, "lvds"),
3670 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_MIPI_BIAS, "mipi-bias"),
3671 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_BIAS, "pex-bias"),
3672 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CLK1, "pex-clk1"),
3673 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CLK2, "pex-clk2"),
3674 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CNTRL, "pex-cntrl"),
3675 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SDMMC1, "sdmmc1"),
3676 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SDMMC3, "sdmmc3"),
3677 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SPI, "spi"),
3678 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SPI_HV, "spi-hv"),
3679 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_UART, "uart"),
3680 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_USB0, "usb0"),
3681 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_USB1, "usb1"),
3682 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_USB2, "usb2"),
3683 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_USB3, "usb3"),
3684 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_USB_BIAS, "usb-bias"),
3685 };
3686 
3687 static const char * const tegra210_reset_sources[] = {
3688 	"POWER_ON_RESET",
3689 	"WATCHDOG",
3690 	"SENSOR",
3691 	"SW_MAIN",
3692 	"LP0",
3693 	"AOTAG"
3694 };
3695 
3696 static const struct tegra_wake_event tegra210_wake_events[] = {
3697 	TEGRA_WAKE_IRQ("rtc", 16, 2),
3698 	TEGRA_WAKE_IRQ("pmu", 51, 86),
3699 };
3700 
3701 static const struct tegra_pmc_soc tegra210_pmc_soc = {
3702 	.supports_core_domain = false,
3703 	.num_powergates = ARRAY_SIZE(tegra210_powergates),
3704 	.powergates = tegra210_powergates,
3705 	.num_cpu_powergates = ARRAY_SIZE(tegra210_cpu_powergates),
3706 	.cpu_powergates = tegra210_cpu_powergates,
3707 	.has_tsense_reset = true,
3708 	.has_gpu_clamps = true,
3709 	.needs_mbist_war = true,
3710 	.has_impl_33v_pwr = false,
3711 	.maybe_tz_only = true,
3712 	.num_io_pads = ARRAY_SIZE(tegra210_io_pads),
3713 	.io_pads = tegra210_io_pads,
3714 	.num_pin_descs = ARRAY_SIZE(tegra210_pin_descs),
3715 	.pin_descs = tegra210_pin_descs,
3716 	.regs = &tegra20_pmc_regs,
3717 	.init = tegra20_pmc_init,
3718 	.setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
3719 	.powergate_set = tegra114_powergate_set,
3720 	.irq_set_wake = tegra210_pmc_irq_set_wake,
3721 	.irq_set_type = tegra210_pmc_irq_set_type,
3722 	.reset_sources = tegra210_reset_sources,
3723 	.num_reset_sources = ARRAY_SIZE(tegra210_reset_sources),
3724 	.reset_levels = NULL,
3725 	.num_reset_levels = 0,
3726 	.num_wake_events = ARRAY_SIZE(tegra210_wake_events),
3727 	.wake_events = tegra210_wake_events,
3728 	.pmc_clks_data = tegra_pmc_clks_data,
3729 	.num_pmc_clks = ARRAY_SIZE(tegra_pmc_clks_data),
3730 	.has_blink_output = true,
3731 	.has_usb_sleepwalk = true,
3732 	.has_single_mmio_aperture = true,
3733 };
3734 
3735 static const struct tegra_io_pad_soc tegra186_io_pads[] = {
3736 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIA, 0, 0x74, 0x78, UINT_MAX, "csia"),
3737 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIB, 1, 0x74, 0x78, UINT_MAX, "csib"),
3738 	TEGRA_IO_PAD(TEGRA_IO_PAD_DSI, 2, 0x74, 0x78, UINT_MAX, "dsi"),
3739 	TEGRA_IO_PAD(TEGRA_IO_PAD_MIPI_BIAS, 3, 0x74, 0x78, UINT_MAX, "mipi-bias"),
3740 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CLK_BIAS, 4, 0x74, 0x78, UINT_MAX, "pex-clk-bias"),
3741 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CLK3, 5, 0x74, 0x78, UINT_MAX, "pex-clk3"),
3742 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CLK2, 6, 0x74, 0x78, UINT_MAX, "pex-clk2"),
3743 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CLK1, 7, 0x74, 0x78, UINT_MAX, "pex-clk1"),
3744 	TEGRA_IO_PAD(TEGRA_IO_PAD_USB0, 9, 0x74, 0x78, UINT_MAX, "usb0"),
3745 	TEGRA_IO_PAD(TEGRA_IO_PAD_USB1, 10, 0x74, 0x78, UINT_MAX, "usb1"),
3746 	TEGRA_IO_PAD(TEGRA_IO_PAD_USB2, 11, 0x74, 0x78, UINT_MAX, "usb2"),
3747 	TEGRA_IO_PAD(TEGRA_IO_PAD_USB_BIAS, 12, 0x74, 0x78, UINT_MAX, "usb-bias"),
3748 	TEGRA_IO_PAD(TEGRA_IO_PAD_UART, 14, 0x74, 0x78, UINT_MAX, "uart"),
3749 	TEGRA_IO_PAD(TEGRA_IO_PAD_AUDIO, 17, 0x74, 0x78, UINT_MAX, "audio"),
3750 	TEGRA_IO_PAD(TEGRA_IO_PAD_HSIC, 19, 0x74, 0x78, UINT_MAX, "hsic"),
3751 	TEGRA_IO_PAD(TEGRA_IO_PAD_DBG, 25, 0x74, 0x78, UINT_MAX, "dbg"),
3752 	TEGRA_IO_PAD(TEGRA_IO_PAD_HDMI_DP0, 28, 0x74, 0x78, UINT_MAX, "hdmi-dp0"),
3753 	TEGRA_IO_PAD(TEGRA_IO_PAD_HDMI_DP1, 29, 0x74, 0x78, UINT_MAX, "hdmi-dp1"),
3754 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CNTRL, 0, 0x7c, 0x80, UINT_MAX, "pex-cntrl"),
3755 	TEGRA_IO_PAD(TEGRA_IO_PAD_SDMMC2_HV, 2, 0x7c, 0x80, 5, "sdmmc2-hv"),
3756 	TEGRA_IO_PAD(TEGRA_IO_PAD_SDMMC4, 4, 0x7c, 0x80, UINT_MAX, "sdmmc4"),
3757 	TEGRA_IO_PAD(TEGRA_IO_PAD_CAM, 6, 0x7c, 0x80, UINT_MAX, "cam"),
3758 	TEGRA_IO_PAD(TEGRA_IO_PAD_DSIB, 8, 0x7c, 0x80, UINT_MAX, "dsib"),
3759 	TEGRA_IO_PAD(TEGRA_IO_PAD_DSIC, 9, 0x7c, 0x80, UINT_MAX, "dsic"),
3760 	TEGRA_IO_PAD(TEGRA_IO_PAD_DSID, 10, 0x7c, 0x80, UINT_MAX, "dsid"),
3761 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIC, 11, 0x7c, 0x80, UINT_MAX, "csic"),
3762 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSID, 12, 0x7c, 0x80, UINT_MAX, "csid"),
3763 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIE, 13, 0x7c, 0x80, UINT_MAX, "csie"),
3764 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIF, 14, 0x7c, 0x80, UINT_MAX, "csif"),
3765 	TEGRA_IO_PAD(TEGRA_IO_PAD_SPI, 15, 0x7c, 0x80, UINT_MAX, "spi"),
3766 	TEGRA_IO_PAD(TEGRA_IO_PAD_UFS, 17, 0x7c, 0x80, UINT_MAX, "ufs"),
3767 	TEGRA_IO_PAD(TEGRA_IO_PAD_DMIC_HV, 20, 0x7c, 0x80, 2, "dmic-hv"),
3768 	TEGRA_IO_PAD(TEGRA_IO_PAD_EDP, 21, 0x7c, 0x80, UINT_MAX, "edp"),
3769 	TEGRA_IO_PAD(TEGRA_IO_PAD_SDMMC1_HV, 23, 0x7c, 0x80, 4, "sdmmc1-hv"),
3770 	TEGRA_IO_PAD(TEGRA_IO_PAD_SDMMC3_HV, 24, 0x7c, 0x80, 6, "sdmmc3-hv"),
3771 	TEGRA_IO_PAD(TEGRA_IO_PAD_CONN, 28, 0x7c, 0x80, UINT_MAX, "conn"),
3772 	TEGRA_IO_PAD(TEGRA_IO_PAD_AUDIO_HV, 29, 0x7c, 0x80, 1, "audio-hv"),
3773 	TEGRA_IO_PAD(TEGRA_IO_PAD_AO_HV, UINT_MAX, UINT_MAX, UINT_MAX, 0, "ao-hv"),
3774 };
3775 
3776 static const struct pinctrl_pin_desc tegra186_pin_descs[] = {
3777 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIA, "csia"),
3778 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIB, "csib"),
3779 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DSI, "dsi"),
3780 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_MIPI_BIAS, "mipi-bias"),
3781 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CLK_BIAS, "pex-clk-bias"),
3782 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CLK3, "pex-clk3"),
3783 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CLK2, "pex-clk2"),
3784 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CLK1, "pex-clk1"),
3785 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_USB0, "usb0"),
3786 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_USB1, "usb1"),
3787 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_USB2, "usb2"),
3788 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_USB_BIAS, "usb-bias"),
3789 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_UART, "uart"),
3790 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_AUDIO, "audio"),
3791 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_HSIC, "hsic"),
3792 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DBG, "dbg"),
3793 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_HDMI_DP0, "hdmi-dp0"),
3794 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_HDMI_DP1, "hdmi-dp1"),
3795 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CNTRL, "pex-cntrl"),
3796 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SDMMC2_HV, "sdmmc2-hv"),
3797 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SDMMC4, "sdmmc4"),
3798 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CAM, "cam"),
3799 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DSIB, "dsib"),
3800 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DSIC, "dsic"),
3801 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DSID, "dsid"),
3802 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIC, "csic"),
3803 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSID, "csid"),
3804 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIE, "csie"),
3805 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIF, "csif"),
3806 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SPI, "spi"),
3807 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_UFS, "ufs"),
3808 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DMIC_HV, "dmic-hv"),
3809 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_EDP, "edp"),
3810 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SDMMC1_HV, "sdmmc1-hv"),
3811 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SDMMC3_HV, "sdmmc3-hv"),
3812 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CONN, "conn"),
3813 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_AUDIO_HV, "audio-hv"),
3814 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_AO_HV, "ao-hv"),
3815 };
3816 
3817 static const struct tegra_pmc_regs tegra186_pmc_regs = {
3818 	.scratch0 = 0x2000,
3819 	.rst_status = 0x70,
3820 	.rst_source_shift = 0x2,
3821 	.rst_source_mask = 0x3c,
3822 	.rst_level_shift = 0x0,
3823 	.rst_level_mask = 0x3,
3824 };
3825 
tegra186_pmc_init(struct tegra_pmc * pmc)3826 static void tegra186_pmc_init(struct tegra_pmc *pmc)
3827 {
3828 	pmc->syscore.suspend = tegra186_pmc_wake_syscore_suspend;
3829 	pmc->syscore.resume = tegra186_pmc_wake_syscore_resume;
3830 
3831 	register_syscore_ops(&pmc->syscore);
3832 }
3833 
tegra186_pmc_setup_irq_polarity(struct tegra_pmc * pmc,struct device_node * np,bool invert)3834 static void tegra186_pmc_setup_irq_polarity(struct tegra_pmc *pmc,
3835 					    struct device_node *np,
3836 					    bool invert)
3837 {
3838 	struct resource regs;
3839 	void __iomem *wake;
3840 	u32 value;
3841 	int index;
3842 
3843 	index = of_property_match_string(np, "reg-names", "wake");
3844 	if (index < 0) {
3845 		dev_err(pmc->dev, "failed to find PMC wake registers\n");
3846 		return;
3847 	}
3848 
3849 	of_address_to_resource(np, index, &regs);
3850 
3851 	wake = ioremap(regs.start, resource_size(&regs));
3852 	if (!wake) {
3853 		dev_err(pmc->dev, "failed to map PMC wake registers\n");
3854 		return;
3855 	}
3856 
3857 	value = readl(wake + WAKE_AOWAKE_CTRL);
3858 
3859 	if (invert)
3860 		value |= WAKE_AOWAKE_CTRL_INTR_POLARITY;
3861 	else
3862 		value &= ~WAKE_AOWAKE_CTRL_INTR_POLARITY;
3863 
3864 	writel(value, wake + WAKE_AOWAKE_CTRL);
3865 
3866 	iounmap(wake);
3867 }
3868 
3869 static const char * const tegra186_reset_sources[] = {
3870 	"SYS_RESET",
3871 	"AOWDT",
3872 	"MCCPLEXWDT",
3873 	"BPMPWDT",
3874 	"SCEWDT",
3875 	"SPEWDT",
3876 	"APEWDT",
3877 	"BCCPLEXWDT",
3878 	"SENSOR",
3879 	"AOTAG",
3880 	"VFSENSOR",
3881 	"SWREST",
3882 	"SC7",
3883 	"HSM",
3884 	"CORESIGHT"
3885 };
3886 
3887 static const char * const tegra186_reset_levels[] = {
3888 	"L0", "L1", "L2", "WARM"
3889 };
3890 
3891 static const struct tegra_wake_event tegra186_wake_events[] = {
3892 	TEGRA_WAKE_IRQ("pmu", 24, 209),
3893 	TEGRA_WAKE_GPIO("power", 29, 1, TEGRA186_AON_GPIO(FF, 0)),
3894 	TEGRA_WAKE_IRQ("rtc", 73, 10),
3895 };
3896 
3897 static const struct tegra_pmc_soc tegra186_pmc_soc = {
3898 	.supports_core_domain = false,
3899 	.num_powergates = 0,
3900 	.powergates = NULL,
3901 	.num_cpu_powergates = 0,
3902 	.cpu_powergates = NULL,
3903 	.has_tsense_reset = false,
3904 	.has_gpu_clamps = false,
3905 	.needs_mbist_war = false,
3906 	.has_impl_33v_pwr = true,
3907 	.maybe_tz_only = false,
3908 	.num_io_pads = ARRAY_SIZE(tegra186_io_pads),
3909 	.io_pads = tegra186_io_pads,
3910 	.num_pin_descs = ARRAY_SIZE(tegra186_pin_descs),
3911 	.pin_descs = tegra186_pin_descs,
3912 	.regs = &tegra186_pmc_regs,
3913 	.init = tegra186_pmc_init,
3914 	.setup_irq_polarity = tegra186_pmc_setup_irq_polarity,
3915 	.set_wake_filters = tegra186_pmc_set_wake_filters,
3916 	.irq_set_wake = tegra186_pmc_irq_set_wake,
3917 	.irq_set_type = tegra186_pmc_irq_set_type,
3918 	.reset_sources = tegra186_reset_sources,
3919 	.num_reset_sources = ARRAY_SIZE(tegra186_reset_sources),
3920 	.reset_levels = tegra186_reset_levels,
3921 	.num_reset_levels = ARRAY_SIZE(tegra186_reset_levels),
3922 	.num_wake_events = ARRAY_SIZE(tegra186_wake_events),
3923 	.wake_events = tegra186_wake_events,
3924 	.max_wake_events = 96,
3925 	.max_wake_vectors = 3,
3926 	.pmc_clks_data = NULL,
3927 	.num_pmc_clks = 0,
3928 	.has_blink_output = false,
3929 	.has_usb_sleepwalk = false,
3930 	.has_single_mmio_aperture = false,
3931 };
3932 
3933 static const struct tegra_io_pad_soc tegra194_io_pads[] = {
3934 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIA, 0, 0x74, 0x78, UINT_MAX, "csia"),
3935 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIB, 1, 0x74, 0x78, UINT_MAX, "csib"),
3936 	TEGRA_IO_PAD(TEGRA_IO_PAD_MIPI_BIAS, 3, 0x74, 0x78, UINT_MAX, "mipi-bias"),
3937 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CLK_BIAS, 4, 0x74, 0x78, UINT_MAX, "pex-clk-bias"),
3938 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CLK3, 5, 0x74, 0x78, UINT_MAX, "pex-clk3"),
3939 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CLK2, 6, 0x74, 0x78, UINT_MAX, "pex-clk2"),
3940 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CLK1, 7, 0x74, 0x78, UINT_MAX, "pex-clk1"),
3941 	TEGRA_IO_PAD(TEGRA_IO_PAD_EQOS, 8, 0x74, 0x78, UINT_MAX, "eqos"),
3942 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CLK_2_BIAS, 9, 0x74, 0x78, UINT_MAX, "pex-clk-2-bias"),
3943 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CLK_2, 10, 0x74, 0x78, UINT_MAX, "pex-clk-2"),
3944 	TEGRA_IO_PAD(TEGRA_IO_PAD_DAP3, 11, 0x74, 0x78, UINT_MAX, "dap3"),
3945 	TEGRA_IO_PAD(TEGRA_IO_PAD_DAP5, 12, 0x74, 0x78, UINT_MAX, "dap5"),
3946 	TEGRA_IO_PAD(TEGRA_IO_PAD_UART, 14, 0x74, 0x78, UINT_MAX, "uart"),
3947 	TEGRA_IO_PAD(TEGRA_IO_PAD_PWR_CTL, 15, 0x74, 0x78, UINT_MAX, "pwr-ctl"),
3948 	TEGRA_IO_PAD(TEGRA_IO_PAD_SOC_GPIO53, 16, 0x74, 0x78, UINT_MAX, "soc-gpio53"),
3949 	TEGRA_IO_PAD(TEGRA_IO_PAD_AUDIO, 17, 0x74, 0x78, UINT_MAX, "audio"),
3950 	TEGRA_IO_PAD(TEGRA_IO_PAD_GP_PWM2, 18, 0x74, 0x78, UINT_MAX, "gp-pwm2"),
3951 	TEGRA_IO_PAD(TEGRA_IO_PAD_GP_PWM3, 19, 0x74, 0x78, UINT_MAX, "gp-pwm3"),
3952 	TEGRA_IO_PAD(TEGRA_IO_PAD_SOC_GPIO12, 20, 0x74, 0x78, UINT_MAX, "soc-gpio12"),
3953 	TEGRA_IO_PAD(TEGRA_IO_PAD_SOC_GPIO13, 21, 0x74, 0x78, UINT_MAX, "soc-gpio13"),
3954 	TEGRA_IO_PAD(TEGRA_IO_PAD_SOC_GPIO10, 22, 0x74, 0x78, UINT_MAX, "soc-gpio10"),
3955 	TEGRA_IO_PAD(TEGRA_IO_PAD_UART4, 23, 0x74, 0x78, UINT_MAX, "uart4"),
3956 	TEGRA_IO_PAD(TEGRA_IO_PAD_UART5, 24, 0x74, 0x78, UINT_MAX, "uart5"),
3957 	TEGRA_IO_PAD(TEGRA_IO_PAD_DBG, 25, 0x74, 0x78, UINT_MAX, "dbg"),
3958 	TEGRA_IO_PAD(TEGRA_IO_PAD_HDMI_DP3, 26, 0x74, 0x78, UINT_MAX, "hdmi-dp3"),
3959 	TEGRA_IO_PAD(TEGRA_IO_PAD_HDMI_DP2, 27, 0x74, 0x78, UINT_MAX, "hdmi-dp2"),
3960 	TEGRA_IO_PAD(TEGRA_IO_PAD_HDMI_DP0, 28, 0x74, 0x78, UINT_MAX, "hdmi-dp0"),
3961 	TEGRA_IO_PAD(TEGRA_IO_PAD_HDMI_DP1, 29, 0x74, 0x78, UINT_MAX, "hdmi-dp1"),
3962 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CNTRL, 0, 0x7c, 0x80, UINT_MAX, "pex-cntrl"),
3963 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_CTL2, 1, 0x7c, 0x80, UINT_MAX, "pex-ctl2"),
3964 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_L0_RST, 2, 0x7c, 0x80, UINT_MAX, "pex-l0-rst"),
3965 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_L1_RST, 3, 0x7c, 0x80, UINT_MAX, "pex-l1-rst"),
3966 	TEGRA_IO_PAD(TEGRA_IO_PAD_SDMMC4, 4, 0x7c, 0x80, UINT_MAX, "sdmmc4"),
3967 	TEGRA_IO_PAD(TEGRA_IO_PAD_PEX_L5_RST, 5, 0x7c, 0x80, UINT_MAX, "pex-l5-rst"),
3968 	TEGRA_IO_PAD(TEGRA_IO_PAD_CAM, 6, 0x7c, 0x80, UINT_MAX, "cam"),
3969 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIC, 11, 0x7c, 0x80, UINT_MAX, "csic"),
3970 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSID, 12, 0x7c, 0x80, UINT_MAX, "csid"),
3971 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIE, 13, 0x7c, 0x80, UINT_MAX, "csie"),
3972 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIF, 14, 0x7c, 0x80, UINT_MAX, "csif"),
3973 	TEGRA_IO_PAD(TEGRA_IO_PAD_SPI, 15, 0x7c, 0x80, UINT_MAX, "spi"),
3974 	TEGRA_IO_PAD(TEGRA_IO_PAD_UFS, 17, 0x7c, 0x80, UINT_MAX, "ufs"),
3975 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIG, 18, 0x7c, 0x80, UINT_MAX, "csig"),
3976 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIH, 19, 0x7c, 0x80, UINT_MAX, "csih"),
3977 	TEGRA_IO_PAD(TEGRA_IO_PAD_EDP, 21, 0x7c, 0x80, UINT_MAX, "edp"),
3978 	TEGRA_IO_PAD(TEGRA_IO_PAD_SDMMC1_HV, 23, 0x7c, 0x80, 4, "sdmmc1-hv"),
3979 	TEGRA_IO_PAD(TEGRA_IO_PAD_SDMMC3_HV, 24, 0x7c, 0x80, 6, "sdmmc3-hv"),
3980 	TEGRA_IO_PAD(TEGRA_IO_PAD_CONN, 28, 0x7c, 0x80, UINT_MAX, "conn"),
3981 	TEGRA_IO_PAD(TEGRA_IO_PAD_AUDIO_HV, 29, 0x7c, 0x80, 1, "audio-hv"),
3982 	TEGRA_IO_PAD(TEGRA_IO_PAD_AO_HV, UINT_MAX, UINT_MAX, UINT_MAX, 0, "ao-hv"),
3983 };
3984 
3985 static const struct pinctrl_pin_desc tegra194_pin_descs[] = {
3986 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIA, "csia"),
3987 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIB, "csib"),
3988 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_MIPI_BIAS, "mipi-bias"),
3989 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CLK_BIAS, "pex-clk-bias"),
3990 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CLK3, "pex-clk3"),
3991 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CLK2, "pex-clk2"),
3992 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CLK1, "pex-clk1"),
3993 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_EQOS, "eqos"),
3994 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CLK_2_BIAS, "pex-clk-2-bias"),
3995 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CLK_2, "pex-clk-2"),
3996 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DAP3, "dap3"),
3997 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DAP5, "dap5"),
3998 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_UART, "uart"),
3999 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PWR_CTL, "pwr-ctl"),
4000 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SOC_GPIO53, "soc-gpio53"),
4001 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_AUDIO, "audio"),
4002 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_GP_PWM2, "gp-pwm2"),
4003 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_GP_PWM3, "gp-pwm3"),
4004 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SOC_GPIO12, "soc-gpio12"),
4005 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SOC_GPIO13, "soc-gpio13"),
4006 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SOC_GPIO10, "soc-gpio10"),
4007 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_UART4, "uart4"),
4008 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_UART5, "uart5"),
4009 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_DBG, "dbg"),
4010 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_HDMI_DP3, "hdmi-dp3"),
4011 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_HDMI_DP2, "hdmi-dp2"),
4012 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_HDMI_DP0, "hdmi-dp0"),
4013 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_HDMI_DP1, "hdmi-dp1"),
4014 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CNTRL, "pex-cntrl"),
4015 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_CTL2, "pex-ctl2"),
4016 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_L0_RST, "pex-l0-rst"),
4017 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_L1_RST, "pex-l1-rst"),
4018 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SDMMC4, "sdmmc4"),
4019 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_PEX_L5_RST, "pex-l5-rst"),
4020 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CAM, "cam"),
4021 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIC, "csic"),
4022 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSID, "csid"),
4023 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIE, "csie"),
4024 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIF, "csif"),
4025 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SPI, "spi"),
4026 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_UFS, "ufs"),
4027 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIG, "csig"),
4028 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIH, "csih"),
4029 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_EDP, "edp"),
4030 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SDMMC1_HV, "sdmmc1-hv"),
4031 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SDMMC3_HV, "sdmmc3-hv"),
4032 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CONN, "conn"),
4033 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_AUDIO_HV, "audio-hv"),
4034 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_AO_HV, "ao-hv"),
4035 };
4036 
4037 static const struct tegra_pmc_regs tegra194_pmc_regs = {
4038 	.scratch0 = 0x2000,
4039 	.rst_status = 0x70,
4040 	.rst_source_shift = 0x2,
4041 	.rst_source_mask = 0x7c,
4042 	.rst_level_shift = 0x0,
4043 	.rst_level_mask = 0x3,
4044 };
4045 
4046 static const char * const tegra194_reset_sources[] = {
4047 	"SYS_RESET_N",
4048 	"AOWDT",
4049 	"BCCPLEXWDT",
4050 	"BPMPWDT",
4051 	"SCEWDT",
4052 	"SPEWDT",
4053 	"APEWDT",
4054 	"LCCPLEXWDT",
4055 	"SENSOR",
4056 	"AOTAG",
4057 	"VFSENSOR",
4058 	"MAINSWRST",
4059 	"SC7",
4060 	"HSM",
4061 	"CSITE",
4062 	"RCEWDT",
4063 	"PVA0WDT",
4064 	"PVA1WDT",
4065 	"L1A_ASYNC",
4066 	"BPMPBOOT",
4067 	"FUSECRC",
4068 };
4069 
4070 static const struct tegra_wake_event tegra194_wake_events[] = {
4071 	TEGRA_WAKE_GPIO("eqos", 20, 0, TEGRA194_MAIN_GPIO(G, 4)),
4072 	TEGRA_WAKE_IRQ("pmu", 24, 209),
4073 	TEGRA_WAKE_GPIO("power", 29, 1, TEGRA194_AON_GPIO(EE, 4)),
4074 	TEGRA_WAKE_IRQ("rtc", 73, 10),
4075 	TEGRA_WAKE_SIMPLE("usb3-port-0", 76),
4076 	TEGRA_WAKE_SIMPLE("usb3-port-1", 77),
4077 	TEGRA_WAKE_SIMPLE("usb3-port-2-3", 78),
4078 	TEGRA_WAKE_SIMPLE("usb2-port-0", 79),
4079 	TEGRA_WAKE_SIMPLE("usb2-port-1", 80),
4080 	TEGRA_WAKE_SIMPLE("usb2-port-2", 81),
4081 	TEGRA_WAKE_SIMPLE("usb2-port-3", 82),
4082 };
4083 
4084 static const struct tegra_pmc_soc tegra194_pmc_soc = {
4085 	.supports_core_domain = false,
4086 	.num_powergates = 0,
4087 	.powergates = NULL,
4088 	.num_cpu_powergates = 0,
4089 	.cpu_powergates = NULL,
4090 	.has_tsense_reset = false,
4091 	.has_gpu_clamps = false,
4092 	.needs_mbist_war = false,
4093 	.has_impl_33v_pwr = true,
4094 	.maybe_tz_only = false,
4095 	.num_io_pads = ARRAY_SIZE(tegra194_io_pads),
4096 	.io_pads = tegra194_io_pads,
4097 	.num_pin_descs = ARRAY_SIZE(tegra194_pin_descs),
4098 	.pin_descs = tegra194_pin_descs,
4099 	.regs = &tegra194_pmc_regs,
4100 	.init = tegra186_pmc_init,
4101 	.setup_irq_polarity = tegra186_pmc_setup_irq_polarity,
4102 	.set_wake_filters = tegra186_pmc_set_wake_filters,
4103 	.irq_set_wake = tegra186_pmc_irq_set_wake,
4104 	.irq_set_type = tegra186_pmc_irq_set_type,
4105 	.reset_sources = tegra194_reset_sources,
4106 	.num_reset_sources = ARRAY_SIZE(tegra194_reset_sources),
4107 	.reset_levels = tegra186_reset_levels,
4108 	.num_reset_levels = ARRAY_SIZE(tegra186_reset_levels),
4109 	.num_wake_events = ARRAY_SIZE(tegra194_wake_events),
4110 	.wake_events = tegra194_wake_events,
4111 	.max_wake_events = 96,
4112 	.max_wake_vectors = 3,
4113 	.pmc_clks_data = NULL,
4114 	.num_pmc_clks = 0,
4115 	.has_blink_output = false,
4116 	.has_usb_sleepwalk = false,
4117 	.has_single_mmio_aperture = false,
4118 };
4119 
4120 static const struct tegra_io_pad_soc tegra234_io_pads[] = {
4121 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIA, 0, 0xe0c0, 0xe0c4, UINT_MAX, "csia"),
4122 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIB, 1, 0xe0c0, 0xe0c4, UINT_MAX, "csib"),
4123 	TEGRA_IO_PAD(TEGRA_IO_PAD_HDMI_DP0, 0, 0xe0d0, 0xe0d4, UINT_MAX, "hdmi-dp0"),
4124 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIC, 2, 0xe0c0, 0xe0c4, UINT_MAX, "csic"),
4125 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSID, 3, 0xe0c0, 0xe0c4, UINT_MAX, "csid"),
4126 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIE, 4, 0xe0c0, 0xe0c4, UINT_MAX, "csie"),
4127 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIF, 5, 0xe0c0, 0xe0c4, UINT_MAX, "csif"),
4128 	TEGRA_IO_PAD(TEGRA_IO_PAD_UFS, 0, 0xe064, 0xe068, UINT_MAX, "ufs"),
4129 	TEGRA_IO_PAD(TEGRA_IO_PAD_EDP, 1, 0xe05c, 0xe060, UINT_MAX, "edp"),
4130 	TEGRA_IO_PAD(TEGRA_IO_PAD_SDMMC1_HV, 0, 0xe054, 0xe058, 4, "sdmmc1-hv"),
4131 	TEGRA_IO_PAD(TEGRA_IO_PAD_SDMMC3_HV, UINT_MAX, UINT_MAX, UINT_MAX, 6, "sdmmc3-hv"),
4132 	TEGRA_IO_PAD(TEGRA_IO_PAD_AUDIO_HV, UINT_MAX, UINT_MAX, UINT_MAX, 1, "audio-hv"),
4133 	TEGRA_IO_PAD(TEGRA_IO_PAD_AO_HV, UINT_MAX, UINT_MAX, UINT_MAX, 0, "ao-hv"),
4134 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIG, 6, 0xe0c0, 0xe0c4, UINT_MAX, "csig"),
4135 	TEGRA_IO_PAD(TEGRA_IO_PAD_CSIH, 7, 0xe0c0, 0xe0c4, UINT_MAX, "csih"),
4136 };
4137 
4138 static const struct pinctrl_pin_desc tegra234_pin_descs[] = {
4139 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIA, "csia"),
4140 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIB, "csib"),
4141 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_HDMI_DP0, "hdmi-dp0"),
4142 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIC, "csic"),
4143 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSID, "csid"),
4144 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIE, "csie"),
4145 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIF, "csif"),
4146 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_UFS, "ufs"),
4147 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_EDP, "edp"),
4148 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SDMMC1_HV, "sdmmc1-hv"),
4149 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_SDMMC3_HV, "sdmmc3-hv"),
4150 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_AUDIO_HV, "audio-hv"),
4151 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_AO_HV, "ao-hv"),
4152 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIG, "csig"),
4153 	TEGRA_IO_PIN_DESC(TEGRA_IO_PAD_CSIH, "csih"),
4154 };
4155 
4156 static const struct tegra_pmc_regs tegra234_pmc_regs = {
4157 	.scratch0 = 0x2000,
4158 	.rst_status = 0x70,
4159 	.rst_source_shift = 0x2,
4160 	.rst_source_mask = 0xfc,
4161 	.rst_level_shift = 0x0,
4162 	.rst_level_mask = 0x3,
4163 };
4164 
4165 static const char * const tegra234_reset_sources[] = {
4166 	"SYS_RESET_N",	/* 0x0 */
4167 	"AOWDT",
4168 	"BCCPLEXWDT",
4169 	"BPMPWDT",
4170 	"SCEWDT",
4171 	"SPEWDT",
4172 	"APEWDT",
4173 	"LCCPLEXWDT",
4174 	"SENSOR",	/* 0x8 */
4175 	NULL,
4176 	NULL,
4177 	"MAINSWRST",
4178 	"SC7",
4179 	"HSM",
4180 	NULL,
4181 	"RCEWDT",
4182 	NULL,		/* 0x10 */
4183 	NULL,
4184 	NULL,
4185 	"BPMPBOOT",
4186 	"FUSECRC",
4187 	"DCEWDT",
4188 	"PSCWDT",
4189 	"PSC",
4190 	"CSITE_SW",	/* 0x18 */
4191 	"POD",
4192 	"SCPM",
4193 	"VREFRO_POWERBAD",
4194 	"VMON",
4195 	"FMON",
4196 	"FSI_R5WDT",
4197 	"FSI_THERM",
4198 	"FSI_R52C0WDT",	/* 0x20 */
4199 	"FSI_R52C1WDT",
4200 	"FSI_R52C2WDT",
4201 	"FSI_R52C3WDT",
4202 	"FSI_FMON",
4203 	"FSI_VMON",	/* 0x25 */
4204 };
4205 
4206 static const struct tegra_wake_event tegra234_wake_events[] = {
4207 	TEGRA_WAKE_GPIO("sd-wake", 8, 0, TEGRA234_MAIN_GPIO(G, 7)),
4208 	TEGRA_WAKE_GPIO("eqos", 20, 0, TEGRA234_MAIN_GPIO(G, 4)),
4209 	TEGRA_WAKE_IRQ("pmu", 24, 209),
4210 	TEGRA_WAKE_GPIO("power", 29, 1, TEGRA234_AON_GPIO(EE, 4)),
4211 	TEGRA_WAKE_GPIO("mgbe", 56, 0, TEGRA234_MAIN_GPIO(Y, 3)),
4212 	TEGRA_WAKE_IRQ("rtc", 73, 10),
4213 	TEGRA_WAKE_IRQ("sw-wake", SW_WAKE_ID, 179),
4214 };
4215 
4216 static const struct tegra_pmc_soc tegra234_pmc_soc = {
4217 	.supports_core_domain = false,
4218 	.num_powergates = 0,
4219 	.powergates = NULL,
4220 	.num_cpu_powergates = 0,
4221 	.cpu_powergates = NULL,
4222 	.has_tsense_reset = false,
4223 	.has_gpu_clamps = false,
4224 	.needs_mbist_war = false,
4225 	.has_impl_33v_pwr = true,
4226 	.maybe_tz_only = false,
4227 	.num_io_pads = ARRAY_SIZE(tegra234_io_pads),
4228 	.io_pads = tegra234_io_pads,
4229 	.num_pin_descs = ARRAY_SIZE(tegra234_pin_descs),
4230 	.pin_descs = tegra234_pin_descs,
4231 	.regs = &tegra234_pmc_regs,
4232 	.init = tegra186_pmc_init,
4233 	.setup_irq_polarity = tegra186_pmc_setup_irq_polarity,
4234 	.set_wake_filters = tegra186_pmc_set_wake_filters,
4235 	.irq_set_wake = tegra186_pmc_irq_set_wake,
4236 	.irq_set_type = tegra186_pmc_irq_set_type,
4237 	.reset_sources = tegra234_reset_sources,
4238 	.num_reset_sources = ARRAY_SIZE(tegra234_reset_sources),
4239 	.reset_levels = tegra186_reset_levels,
4240 	.num_reset_levels = ARRAY_SIZE(tegra186_reset_levels),
4241 	.num_wake_events = ARRAY_SIZE(tegra234_wake_events),
4242 	.wake_events = tegra234_wake_events,
4243 	.max_wake_events = 96,
4244 	.max_wake_vectors = 3,
4245 	.pmc_clks_data = NULL,
4246 	.num_pmc_clks = 0,
4247 	.has_blink_output = false,
4248 	.has_single_mmio_aperture = false,
4249 };
4250 
4251 static const struct of_device_id tegra_pmc_match[] = {
4252 	{ .compatible = "nvidia,tegra234-pmc", .data = &tegra234_pmc_soc },
4253 	{ .compatible = "nvidia,tegra194-pmc", .data = &tegra194_pmc_soc },
4254 	{ .compatible = "nvidia,tegra186-pmc", .data = &tegra186_pmc_soc },
4255 	{ .compatible = "nvidia,tegra210-pmc", .data = &tegra210_pmc_soc },
4256 	{ .compatible = "nvidia,tegra132-pmc", .data = &tegra124_pmc_soc },
4257 	{ .compatible = "nvidia,tegra124-pmc", .data = &tegra124_pmc_soc },
4258 	{ .compatible = "nvidia,tegra114-pmc", .data = &tegra114_pmc_soc },
4259 	{ .compatible = "nvidia,tegra30-pmc", .data = &tegra30_pmc_soc },
4260 	{ .compatible = "nvidia,tegra20-pmc", .data = &tegra20_pmc_soc },
4261 	{ }
4262 };
4263 
tegra_pmc_sync_state(struct device * dev)4264 static void tegra_pmc_sync_state(struct device *dev)
4265 {
4266 	int err;
4267 
4268 	/*
4269 	 * Newer device-trees have power domains, but we need to prepare all
4270 	 * device drivers with runtime PM and OPP support first, otherwise
4271 	 * state syncing is unsafe.
4272 	 */
4273 	if (!pmc->soc->supports_core_domain)
4274 		return;
4275 
4276 	/*
4277 	 * Older device-trees don't have core PD, and thus, there are
4278 	 * no dependencies that will block the state syncing. We shouldn't
4279 	 * mark the domain as synced in this case.
4280 	 */
4281 	if (!pmc->core_domain_registered)
4282 		return;
4283 
4284 	pmc->core_domain_state_synced = true;
4285 
4286 	/* this is a no-op if core regulator isn't used */
4287 	mutex_lock(&pmc->powergates_lock);
4288 	err = dev_pm_opp_sync_regulators(dev);
4289 	mutex_unlock(&pmc->powergates_lock);
4290 
4291 	if (err)
4292 		dev_err(dev, "failed to sync regulators: %d\n", err);
4293 }
4294 
4295 static struct platform_driver tegra_pmc_driver = {
4296 	.driver = {
4297 		.name = "tegra-pmc",
4298 		.suppress_bind_attrs = true,
4299 		.of_match_table = tegra_pmc_match,
4300 #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM)
4301 		.pm = &tegra_pmc_pm_ops,
4302 #endif
4303 		.sync_state = tegra_pmc_sync_state,
4304 	},
4305 	.probe = tegra_pmc_probe,
4306 };
4307 builtin_platform_driver(tegra_pmc_driver);
4308 
tegra_pmc_detect_tz_only(struct tegra_pmc * pmc)4309 static bool __init tegra_pmc_detect_tz_only(struct tegra_pmc *pmc)
4310 {
4311 	u32 value, saved;
4312 
4313 	saved = readl(pmc->base + pmc->soc->regs->scratch0);
4314 	value = saved ^ 0xffffffff;
4315 
4316 	if (value == 0xffffffff)
4317 		value = 0xdeadbeef;
4318 
4319 	/* write pattern and read it back */
4320 	writel(value, pmc->base + pmc->soc->regs->scratch0);
4321 	value = readl(pmc->base + pmc->soc->regs->scratch0);
4322 
4323 	/* if we read all-zeroes, access is restricted to TZ only */
4324 	if (value == 0) {
4325 		pr_info("access to PMC is restricted to TZ\n");
4326 		return true;
4327 	}
4328 
4329 	/* restore original value */
4330 	writel(saved, pmc->base + pmc->soc->regs->scratch0);
4331 
4332 	return false;
4333 }
4334 
4335 /*
4336  * Early initialization to allow access to registers in the very early boot
4337  * process.
4338  */
tegra_pmc_early_init(void)4339 static int __init tegra_pmc_early_init(void)
4340 {
4341 	const struct of_device_id *match;
4342 	struct device_node *np;
4343 	struct resource regs;
4344 	unsigned int i;
4345 	bool invert;
4346 
4347 	mutex_init(&pmc->powergates_lock);
4348 
4349 	np = of_find_matching_node_and_match(NULL, tegra_pmc_match, &match);
4350 	if (!np) {
4351 		/*
4352 		 * Fall back to legacy initialization for 32-bit ARM only. All
4353 		 * 64-bit ARM device tree files for Tegra are required to have
4354 		 * a PMC node.
4355 		 *
4356 		 * This is for backwards-compatibility with old device trees
4357 		 * that didn't contain a PMC node. Note that in this case the
4358 		 * SoC data can't be matched and therefore powergating is
4359 		 * disabled.
4360 		 */
4361 		if (IS_ENABLED(CONFIG_ARM) && soc_is_tegra()) {
4362 			pr_warn("DT node not found, powergating disabled\n");
4363 
4364 			regs.start = 0x7000e400;
4365 			regs.end = 0x7000e7ff;
4366 			regs.flags = IORESOURCE_MEM;
4367 
4368 			pr_warn("Using memory region %pR\n", &regs);
4369 		} else {
4370 			/*
4371 			 * At this point we're not running on Tegra, so play
4372 			 * nice with multi-platform kernels.
4373 			 */
4374 			return 0;
4375 		}
4376 	} else {
4377 		/*
4378 		 * Extract information from the device tree if we've found a
4379 		 * matching node.
4380 		 */
4381 		if (of_address_to_resource(np, 0, &regs) < 0) {
4382 			pr_err("failed to get PMC registers\n");
4383 			of_node_put(np);
4384 			return -ENXIO;
4385 		}
4386 	}
4387 
4388 	pmc->base = ioremap(regs.start, resource_size(&regs));
4389 	if (!pmc->base) {
4390 		pr_err("failed to map PMC registers\n");
4391 		of_node_put(np);
4392 		return -ENXIO;
4393 	}
4394 
4395 	if (of_device_is_available(np)) {
4396 		pmc->soc = match->data;
4397 
4398 		if (pmc->soc->maybe_tz_only)
4399 			pmc->tz_only = tegra_pmc_detect_tz_only(pmc);
4400 
4401 		/* Create a bitmap of the available and valid partitions */
4402 		for (i = 0; i < pmc->soc->num_powergates; i++)
4403 			if (pmc->soc->powergates[i])
4404 				set_bit(i, pmc->powergates_available);
4405 
4406 		/*
4407 		 * Invert the interrupt polarity if a PMC device tree node
4408 		 * exists and contains the nvidia,invert-interrupt property.
4409 		 */
4410 		invert = of_property_read_bool(np, "nvidia,invert-interrupt");
4411 
4412 		pmc->soc->setup_irq_polarity(pmc, np, invert);
4413 
4414 		of_node_put(np);
4415 	}
4416 
4417 	return 0;
4418 }
4419 early_initcall(tegra_pmc_early_init);
4420