blob: 22c28a4ace4dae9a7bc5f3b10fb3625e8468a93c [file] [log] [blame]
Thierry Reding72323982014-07-11 13:19:06 +02001/*
2 * drivers/soc/tegra/pmc.c
3 *
4 * Copyright (c) 2010 Google, Inc
Sandipan Patra5f84bb12018-10-24 12:38:00 +05305 * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
Thierry Reding72323982014-07-11 13:19:06 +02006 *
7 * Author:
8 * Colin Cross <ccross@google.com>
9 *
10 * This software is licensed under the terms of the GNU General Public
11 * License version 2, as published by the Free Software Foundation, and
12 * may be copied, distributed, and modified under those terms.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 */
20
Thierry Reding7d71e9032015-04-29 12:42:28 +020021#define pr_fmt(fmt) "tegra-pmc: " fmt
22
Thierry Reding72323982014-07-11 13:19:06 +020023#include <linux/kernel.h>
24#include <linux/clk.h>
25#include <linux/clk/tegra.h>
26#include <linux/debugfs.h>
27#include <linux/delay.h>
28#include <linux/err.h>
29#include <linux/export.h>
30#include <linux/init.h>
31#include <linux/io.h>
Jon Hunter0a2d87e2016-02-26 15:48:40 +000032#include <linux/iopoll.h>
Thierry Reding72323982014-07-11 13:19:06 +020033#include <linux/of.h>
34#include <linux/of_address.h>
Geert Uytterhoeven3fd01212018-04-18 16:50:04 +020035#include <linux/of_clk.h>
Jon Huntera3804512016-03-30 10:15:15 +010036#include <linux/of_platform.h>
Aapo Vienamo4a37f112018-08-10 21:08:12 +030037#include <linux/pinctrl/pinctrl.h>
38#include <linux/pinctrl/pinconf.h>
39#include <linux/pinctrl/pinconf-generic.h>
Thierry Reding72323982014-07-11 13:19:06 +020040#include <linux/platform_device.h>
Jon Huntera3804512016-03-30 10:15:15 +010041#include <linux/pm_domain.h>
Thierry Reding72323982014-07-11 13:19:06 +020042#include <linux/reboot.h>
43#include <linux/reset.h>
44#include <linux/seq_file.h>
Jon Huntera3804512016-03-30 10:15:15 +010045#include <linux/slab.h>
Thierry Reding72323982014-07-11 13:19:06 +020046#include <linux/spinlock.h>
47
48#include <soc/tegra/common.h>
49#include <soc/tegra/fuse.h>
50#include <soc/tegra/pmc.h>
51
Aapo Vienamofccf0f72018-08-10 21:08:11 +030052#include <dt-bindings/pinctrl/pinctrl-tegra-io-pad.h>
53
Thierry Reding72323982014-07-11 13:19:06 +020054#define PMC_CNTRL 0x0
Laxman Dewangan6c0bd212016-06-17 18:36:12 +053055#define PMC_CNTRL_INTR_POLARITY BIT(17) /* inverts INTR polarity */
Thierry Reding95b780b2016-10-10 13:13:36 +020056#define PMC_CNTRL_CPU_PWRREQ_OE BIT(16) /* CPU pwr req enable */
57#define PMC_CNTRL_CPU_PWRREQ_POLARITY BIT(15) /* CPU pwr req polarity */
58#define PMC_CNTRL_SIDE_EFFECT_LP0 BIT(14) /* LP0 when CPU pwr gated */
59#define PMC_CNTRL_SYSCLK_OE BIT(11) /* system clock enable */
60#define PMC_CNTRL_SYSCLK_POLARITY BIT(10) /* sys clk polarity */
61#define PMC_CNTRL_MAIN_RST BIT(4)
Thierry Reding72323982014-07-11 13:19:06 +020062
63#define DPD_SAMPLE 0x020
Laxman Dewangan6c0bd212016-06-17 18:36:12 +053064#define DPD_SAMPLE_ENABLE BIT(0)
Thierry Reding72323982014-07-11 13:19:06 +020065#define DPD_SAMPLE_DISABLE (0 << 0)
66
67#define PWRGATE_TOGGLE 0x30
Laxman Dewangan6c0bd212016-06-17 18:36:12 +053068#define PWRGATE_TOGGLE_START BIT(8)
Thierry Reding72323982014-07-11 13:19:06 +020069
70#define REMOVE_CLAMPING 0x34
71
72#define PWRGATE_STATUS 0x38
73
Aapo Vienamo13136a42018-08-10 21:08:07 +030074#define PMC_IMPL_E_33V_PWR 0x40
75
Laxman Dewangan21b49912016-10-10 15:14:34 +020076#define PMC_PWR_DET 0x48
77
Thierry Reding5be22552017-08-30 12:32:58 +020078#define PMC_SCRATCH0_MODE_RECOVERY BIT(31)
79#define PMC_SCRATCH0_MODE_BOOTLOADER BIT(30)
80#define PMC_SCRATCH0_MODE_RCM BIT(1)
81#define PMC_SCRATCH0_MODE_MASK (PMC_SCRATCH0_MODE_RECOVERY | \
Thierry Reding72323982014-07-11 13:19:06 +020082 PMC_SCRATCH0_MODE_BOOTLOADER | \
83 PMC_SCRATCH0_MODE_RCM)
84
85#define PMC_CPUPWRGOOD_TIMER 0xc8
86#define PMC_CPUPWROFF_TIMER 0xcc
87
Laxman Dewangan21b49912016-10-10 15:14:34 +020088#define PMC_PWR_DET_VALUE 0xe4
89
Thierry Reding72323982014-07-11 13:19:06 +020090#define PMC_SCRATCH41 0x140
91
Mikko Perttunen3568df32015-01-06 12:52:58 +020092#define PMC_SENSOR_CTRL 0x1b0
Laxman Dewangan6c0bd212016-06-17 18:36:12 +053093#define PMC_SENSOR_CTRL_SCRATCH_WRITE BIT(2)
94#define PMC_SENSOR_CTRL_ENABLE_RST BIT(1)
Mikko Perttunen3568df32015-01-06 12:52:58 +020095
Thierry Redingf5353c62015-12-30 17:13:29 +010096#define PMC_RST_STATUS_POR 0
97#define PMC_RST_STATUS_WATCHDOG 1
98#define PMC_RST_STATUS_SENSOR 2
99#define PMC_RST_STATUS_SW_MAIN 3
100#define PMC_RST_STATUS_LP0 4
101#define PMC_RST_STATUS_AOTAG 5
102
Thierry Reding72323982014-07-11 13:19:06 +0200103#define IO_DPD_REQ 0x1b8
Laxman Dewangan6c0bd212016-06-17 18:36:12 +0530104#define IO_DPD_REQ_CODE_IDLE (0U << 30)
105#define IO_DPD_REQ_CODE_OFF (1U << 30)
106#define IO_DPD_REQ_CODE_ON (2U << 30)
107#define IO_DPD_REQ_CODE_MASK (3U << 30)
Thierry Reding72323982014-07-11 13:19:06 +0200108
109#define IO_DPD_STATUS 0x1bc
110#define IO_DPD2_REQ 0x1c0
111#define IO_DPD2_STATUS 0x1c4
112#define SEL_DPD_TIM 0x1c8
113
Mikko Perttunen3568df32015-01-06 12:52:58 +0200114#define PMC_SCRATCH54 0x258
Laxman Dewangan6c0bd212016-06-17 18:36:12 +0530115#define PMC_SCRATCH54_DATA_SHIFT 8
116#define PMC_SCRATCH54_ADDR_SHIFT 0
Mikko Perttunen3568df32015-01-06 12:52:58 +0200117
118#define PMC_SCRATCH55 0x25c
Laxman Dewangan6c0bd212016-06-17 18:36:12 +0530119#define PMC_SCRATCH55_RESET_TEGRA BIT(31)
120#define PMC_SCRATCH55_CNTRL_ID_SHIFT 27
121#define PMC_SCRATCH55_PINMUX_SHIFT 24
122#define PMC_SCRATCH55_16BITOP BIT(15)
123#define PMC_SCRATCH55_CHECKSUM_SHIFT 16
124#define PMC_SCRATCH55_I2CSLV1_SHIFT 0
Mikko Perttunen3568df32015-01-06 12:52:58 +0200125
Thierry Reding72323982014-07-11 13:19:06 +0200126#define GPU_RG_CNTRL 0x2d4
127
Thierry Redingc641ec62017-08-30 12:42:34 +0200128/* Tegra186 and later */
129#define WAKE_AOWAKE_CTRL 0x4f4
130#define WAKE_AOWAKE_CTRL_INTR_POLARITY BIT(0)
131
Jon Huntera3804512016-03-30 10:15:15 +0100132struct tegra_powergate {
133 struct generic_pm_domain genpd;
134 struct tegra_pmc *pmc;
135 unsigned int id;
136 struct clk **clks;
137 unsigned int num_clks;
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200138 struct reset_control *reset;
Jon Huntera3804512016-03-30 10:15:15 +0100139};
140
Laxman Dewangan21b49912016-10-10 15:14:34 +0200141struct tegra_io_pad_soc {
142 enum tegra_io_pad id;
143 unsigned int dpd;
144 unsigned int voltage;
Aapo Vienamo437c4f22018-08-10 21:08:10 +0300145 const char *name;
Laxman Dewangan21b49912016-10-10 15:14:34 +0200146};
147
Thierry Reding5be22552017-08-30 12:32:58 +0200148struct tegra_pmc_regs {
149 unsigned int scratch0;
150 unsigned int dpd_req;
151 unsigned int dpd_status;
152 unsigned int dpd2_req;
153 unsigned int dpd2_status;
Sandipan Patra5f84bb12018-10-24 12:38:00 +0530154 unsigned int rst_status;
155 unsigned int rst_source_shift;
156 unsigned int rst_source_mask;
157 unsigned int rst_level_shift;
158 unsigned int rst_level_mask;
Thierry Reding5be22552017-08-30 12:32:58 +0200159};
160
Thierry Reding72323982014-07-11 13:19:06 +0200161struct tegra_pmc_soc {
162 unsigned int num_powergates;
163 const char *const *powergates;
164 unsigned int num_cpu_powergates;
165 const u8 *cpu_powergates;
Thierry Redinga9a40a42015-01-09 11:15:33 +0100166
Mikko Perttunen3568df32015-01-06 12:52:58 +0200167 bool has_tsense_reset;
Thierry Redinga9a40a42015-01-09 11:15:33 +0100168 bool has_gpu_clamps;
Peter De Schrijvera263394a2018-01-25 16:00:13 +0200169 bool needs_mbist_war;
Aapo Vienamo13136a42018-08-10 21:08:07 +0300170 bool has_impl_33v_pwr;
Laxman Dewangan21b49912016-10-10 15:14:34 +0200171
172 const struct tegra_io_pad_soc *io_pads;
173 unsigned int num_io_pads;
Thierry Reding5be22552017-08-30 12:32:58 +0200174
Aapo Vienamo4a37f112018-08-10 21:08:12 +0300175 const struct pinctrl_pin_desc *pin_descs;
176 unsigned int num_pin_descs;
177
Thierry Reding5be22552017-08-30 12:32:58 +0200178 const struct tegra_pmc_regs *regs;
179 void (*init)(struct tegra_pmc *pmc);
180 void (*setup_irq_polarity)(struct tegra_pmc *pmc,
181 struct device_node *np,
182 bool invert);
Sandipan Patra5f84bb12018-10-24 12:38:00 +0530183
184 const char * const *reset_sources;
185 unsigned int num_reset_sources;
186 const char * const *reset_levels;
187 unsigned int num_reset_levels;
188};
189
190static const char * const tegra186_reset_sources[] = {
191 "SYS_RESET",
192 "AOWDT",
193 "MCCPLEXWDT",
194 "BPMPWDT",
195 "SCEWDT",
196 "SPEWDT",
197 "APEWDT",
198 "BCCPLEXWDT",
199 "SENSOR",
200 "AOTAG",
201 "VFSENSOR",
202 "SWREST",
203 "SC7",
204 "HSM",
205 "CORESIGHT"
206};
207
208static const char * const tegra186_reset_levels[] = {
209 "L0", "L1", "L2", "WARM"
210};
211
212static const char * const tegra30_reset_sources[] = {
213 "POWER_ON_RESET",
214 "WATCHDOG",
215 "SENSOR",
216 "SW_MAIN",
217 "LP0",
218 "AOTAG"
Thierry Reding72323982014-07-11 13:19:06 +0200219};
220
221/**
222 * struct tegra_pmc - NVIDIA Tegra PMC
Jon Hunter35b67292015-12-04 14:57:03 +0000223 * @dev: pointer to PMC device structure
Thierry Reding72323982014-07-11 13:19:06 +0200224 * @base: pointer to I/O remapped register region
225 * @clk: pointer to pclk clock
Jon Hunter35b67292015-12-04 14:57:03 +0000226 * @soc: pointer to SoC data structure
Jon Hunter3195ac62015-12-04 14:57:05 +0000227 * @debugfs: pointer to debugfs entry
Thierry Reding72323982014-07-11 13:19:06 +0200228 * @rate: currently configured rate of pclk
229 * @suspend_mode: lowest suspend mode available
230 * @cpu_good_time: CPU power good time (in microseconds)
231 * @cpu_off_time: CPU power off time (in microsecends)
232 * @core_osc_time: core power good OSC time (in microseconds)
233 * @core_pmu_time: core power good PMU time (in microseconds)
234 * @core_off_time: core power off time (in microseconds)
235 * @corereq_high: core power request is active-high
236 * @sysclkreq_high: system clock request is active-high
237 * @combined_req: combined power request for CPU & core
238 * @cpu_pwr_good_en: CPU power good signal is enabled
239 * @lp0_vec_phys: physical base address of the LP0 warm boot code
240 * @lp0_vec_size: size of the LP0 warm boot code
Jon Huntera3804512016-03-30 10:15:15 +0100241 * @powergates_available: Bitmap of available power gates
Thierry Reding72323982014-07-11 13:19:06 +0200242 * @powergates_lock: mutex for power gate register access
243 */
244struct tegra_pmc {
Mikko Perttunen3568df32015-01-06 12:52:58 +0200245 struct device *dev;
Thierry Reding72323982014-07-11 13:19:06 +0200246 void __iomem *base;
Thierry Redingc641ec62017-08-30 12:42:34 +0200247 void __iomem *wake;
248 void __iomem *aotag;
Thierry Reding5be22552017-08-30 12:32:58 +0200249 void __iomem *scratch;
Thierry Reding72323982014-07-11 13:19:06 +0200250 struct clk *clk;
Jon Hunter3195ac62015-12-04 14:57:05 +0000251 struct dentry *debugfs;
Thierry Reding72323982014-07-11 13:19:06 +0200252
253 const struct tegra_pmc_soc *soc;
254
255 unsigned long rate;
256
257 enum tegra_suspend_mode suspend_mode;
258 u32 cpu_good_time;
259 u32 cpu_off_time;
260 u32 core_osc_time;
261 u32 core_pmu_time;
262 u32 core_off_time;
263 bool corereq_high;
264 bool sysclkreq_high;
265 bool combined_req;
266 bool cpu_pwr_good_en;
267 u32 lp0_vec_phys;
268 u32 lp0_vec_size;
Jon Huntera3804512016-03-30 10:15:15 +0100269 DECLARE_BITMAP(powergates_available, TEGRA_POWERGATE_MAX);
Thierry Reding72323982014-07-11 13:19:06 +0200270
271 struct mutex powergates_lock;
Aapo Vienamo4a37f112018-08-10 21:08:12 +0300272
273 struct pinctrl_dev *pctl_dev;
Thierry Reding72323982014-07-11 13:19:06 +0200274};
275
276static struct tegra_pmc *pmc = &(struct tegra_pmc) {
277 .base = NULL,
278 .suspend_mode = TEGRA_SUSPEND_NONE,
279};
280
Jon Huntera3804512016-03-30 10:15:15 +0100281static inline struct tegra_powergate *
282to_powergate(struct generic_pm_domain *domain)
283{
284 return container_of(domain, struct tegra_powergate, genpd);
285}
286
Thierry Reding72323982014-07-11 13:19:06 +0200287static u32 tegra_pmc_readl(unsigned long offset)
288{
289 return readl(pmc->base + offset);
290}
291
292static void tegra_pmc_writel(u32 value, unsigned long offset)
293{
294 writel(value, pmc->base + offset);
295}
296
Jon Hunter0ecf2d32016-02-11 18:03:23 +0000297static inline bool tegra_powergate_state(int id)
298{
Jon Hunterbc9af232016-02-15 12:38:11 +0000299 if (id == TEGRA_POWERGATE_3D && pmc->soc->has_gpu_clamps)
300 return (tegra_pmc_readl(GPU_RG_CNTRL) & 0x1) == 0;
301 else
302 return (tegra_pmc_readl(PWRGATE_STATUS) & BIT(id)) != 0;
Jon Hunter0ecf2d32016-02-11 18:03:23 +0000303}
304
Jon Hunter0a243bd2016-02-11 18:03:24 +0000305static inline bool tegra_powergate_is_valid(int id)
306{
307 return (pmc->soc && pmc->soc->powergates[id]);
308}
309
Jon Huntera3804512016-03-30 10:15:15 +0100310static inline bool tegra_powergate_is_available(int id)
311{
312 return test_bit(id, pmc->powergates_available);
313}
314
315static int tegra_powergate_lookup(struct tegra_pmc *pmc, const char *name)
316{
317 unsigned int i;
318
319 if (!pmc || !pmc->soc || !name)
320 return -EINVAL;
321
322 for (i = 0; i < pmc->soc->num_powergates; i++) {
323 if (!tegra_powergate_is_valid(i))
324 continue;
325
326 if (!strcmp(name, pmc->soc->powergates[i]))
327 return i;
328 }
329
Jon Huntera3804512016-03-30 10:15:15 +0100330 return -ENODEV;
331}
332
Thierry Reding72323982014-07-11 13:19:06 +0200333/**
334 * tegra_powergate_set() - set the state of a partition
335 * @id: partition ID
336 * @new_state: new state of the partition
337 */
Jon Hunter70293ed2016-02-11 18:03:22 +0000338static int tegra_powergate_set(unsigned int id, bool new_state)
Thierry Reding72323982014-07-11 13:19:06 +0200339{
Jon Hunter0a2d87e2016-02-26 15:48:40 +0000340 bool status;
341 int err;
342
Jon Hunterbc9af232016-02-15 12:38:11 +0000343 if (id == TEGRA_POWERGATE_3D && pmc->soc->has_gpu_clamps)
344 return -EINVAL;
345
Thierry Reding72323982014-07-11 13:19:06 +0200346 mutex_lock(&pmc->powergates_lock);
347
Jon Hunter0ecf2d32016-02-11 18:03:23 +0000348 if (tegra_powergate_state(id) == new_state) {
Thierry Reding72323982014-07-11 13:19:06 +0200349 mutex_unlock(&pmc->powergates_lock);
350 return 0;
351 }
352
353 tegra_pmc_writel(PWRGATE_TOGGLE_START | id, PWRGATE_TOGGLE);
354
Jon Hunter0a2d87e2016-02-26 15:48:40 +0000355 err = readx_poll_timeout(tegra_powergate_state, id, status,
356 status == new_state, 10, 100000);
357
Thierry Reding72323982014-07-11 13:19:06 +0200358 mutex_unlock(&pmc->powergates_lock);
359
Jon Hunter0a2d87e2016-02-26 15:48:40 +0000360 return err;
Thierry Reding72323982014-07-11 13:19:06 +0200361}
362
Jon Huntera3804512016-03-30 10:15:15 +0100363static int __tegra_powergate_remove_clamping(unsigned int id)
Thierry Reding72323982014-07-11 13:19:06 +0200364{
365 u32 mask;
366
Jon Huntere8cf6612016-02-11 18:03:21 +0000367 mutex_lock(&pmc->powergates_lock);
368
Thierry Reding72323982014-07-11 13:19:06 +0200369 /*
Thierry Redinga9a40a42015-01-09 11:15:33 +0100370 * On Tegra124 and later, the clamps for the GPU are controlled by a
371 * separate register (with different semantics).
Thierry Reding72323982014-07-11 13:19:06 +0200372 */
Thierry Redinga9a40a42015-01-09 11:15:33 +0100373 if (id == TEGRA_POWERGATE_3D) {
374 if (pmc->soc->has_gpu_clamps) {
Thierry Reding72323982014-07-11 13:19:06 +0200375 tegra_pmc_writel(0, GPU_RG_CNTRL);
Jon Huntere8cf6612016-02-11 18:03:21 +0000376 goto out;
Thierry Reding72323982014-07-11 13:19:06 +0200377 }
378 }
379
380 /*
381 * Tegra 2 has a bug where PCIE and VDE clamping masks are
382 * swapped relatively to the partition ids
383 */
384 if (id == TEGRA_POWERGATE_VDEC)
385 mask = (1 << TEGRA_POWERGATE_PCIE);
386 else if (id == TEGRA_POWERGATE_PCIE)
387 mask = (1 << TEGRA_POWERGATE_VDEC);
388 else
389 mask = (1 << id);
390
391 tegra_pmc_writel(mask, REMOVE_CLAMPING);
392
Jon Huntere8cf6612016-02-11 18:03:21 +0000393out:
394 mutex_unlock(&pmc->powergates_lock);
395
Thierry Reding72323982014-07-11 13:19:06 +0200396 return 0;
397}
Jon Huntera3804512016-03-30 10:15:15 +0100398
399static void tegra_powergate_disable_clocks(struct tegra_powergate *pg)
400{
401 unsigned int i;
402
403 for (i = 0; i < pg->num_clks; i++)
404 clk_disable_unprepare(pg->clks[i]);
405}
406
407static int tegra_powergate_enable_clocks(struct tegra_powergate *pg)
408{
409 unsigned int i;
410 int err;
411
412 for (i = 0; i < pg->num_clks; i++) {
413 err = clk_prepare_enable(pg->clks[i]);
414 if (err)
415 goto out;
416 }
417
418 return 0;
419
420out:
421 while (i--)
422 clk_disable_unprepare(pg->clks[i]);
423
424 return err;
425}
426
Peter De Schrijvera263394a2018-01-25 16:00:13 +0200427int __weak tegra210_clk_handle_mbist_war(unsigned int id)
428{
429 return 0;
430}
431
Jon Huntera3804512016-03-30 10:15:15 +0100432static int tegra_powergate_power_up(struct tegra_powergate *pg,
433 bool disable_clocks)
434{
435 int err;
436
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200437 err = reset_control_assert(pg->reset);
Jon Huntera3804512016-03-30 10:15:15 +0100438 if (err)
439 return err;
440
441 usleep_range(10, 20);
442
443 err = tegra_powergate_set(pg->id, true);
444 if (err < 0)
445 return err;
446
447 usleep_range(10, 20);
448
449 err = tegra_powergate_enable_clocks(pg);
450 if (err)
451 goto disable_clks;
452
453 usleep_range(10, 20);
454
455 err = __tegra_powergate_remove_clamping(pg->id);
456 if (err)
457 goto disable_clks;
458
459 usleep_range(10, 20);
460
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200461 err = reset_control_deassert(pg->reset);
Jon Huntera3804512016-03-30 10:15:15 +0100462 if (err)
463 goto powergate_off;
464
465 usleep_range(10, 20);
466
Peter De Schrijvera263394a2018-01-25 16:00:13 +0200467 if (pg->pmc->soc->needs_mbist_war)
468 err = tegra210_clk_handle_mbist_war(pg->id);
469 if (err)
470 goto disable_clks;
471
Jon Huntera3804512016-03-30 10:15:15 +0100472 if (disable_clocks)
473 tegra_powergate_disable_clocks(pg);
474
475 return 0;
476
477disable_clks:
478 tegra_powergate_disable_clocks(pg);
479 usleep_range(10, 20);
Thierry Redingda8f4b42016-06-30 12:12:55 +0200480
Jon Huntera3804512016-03-30 10:15:15 +0100481powergate_off:
482 tegra_powergate_set(pg->id, false);
483
484 return err;
485}
486
487static int tegra_powergate_power_down(struct tegra_powergate *pg)
488{
489 int err;
490
491 err = tegra_powergate_enable_clocks(pg);
492 if (err)
493 return err;
494
495 usleep_range(10, 20);
496
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200497 err = reset_control_assert(pg->reset);
Jon Huntera3804512016-03-30 10:15:15 +0100498 if (err)
499 goto disable_clks;
500
501 usleep_range(10, 20);
502
503 tegra_powergate_disable_clocks(pg);
504
505 usleep_range(10, 20);
506
507 err = tegra_powergate_set(pg->id, false);
508 if (err)
509 goto assert_resets;
510
511 return 0;
512
513assert_resets:
514 tegra_powergate_enable_clocks(pg);
515 usleep_range(10, 20);
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200516 reset_control_deassert(pg->reset);
Jon Huntera3804512016-03-30 10:15:15 +0100517 usleep_range(10, 20);
Thierry Redingda8f4b42016-06-30 12:12:55 +0200518
Jon Huntera3804512016-03-30 10:15:15 +0100519disable_clks:
520 tegra_powergate_disable_clocks(pg);
521
522 return err;
523}
524
525static int tegra_genpd_power_on(struct generic_pm_domain *domain)
526{
527 struct tegra_powergate *pg = to_powergate(domain);
Jon Huntera3804512016-03-30 10:15:15 +0100528 int err;
529
530 err = tegra_powergate_power_up(pg, true);
531 if (err)
Thierry Reding54e24722016-11-08 10:58:32 +0100532 pr_err("failed to turn on PM domain %s: %d\n", pg->genpd.name,
533 err);
Jon Huntera3804512016-03-30 10:15:15 +0100534
535 return err;
536}
537
538static int tegra_genpd_power_off(struct generic_pm_domain *domain)
539{
540 struct tegra_powergate *pg = to_powergate(domain);
Jon Huntera3804512016-03-30 10:15:15 +0100541 int err;
542
543 err = tegra_powergate_power_down(pg);
544 if (err)
Thierry Reding54e24722016-11-08 10:58:32 +0100545 pr_err("failed to turn off PM domain %s: %d\n",
546 pg->genpd.name, err);
Jon Huntera3804512016-03-30 10:15:15 +0100547
548 return err;
549}
550
551/**
552 * tegra_powergate_power_on() - power on partition
553 * @id: partition ID
554 */
555int tegra_powergate_power_on(unsigned int id)
556{
557 if (!tegra_powergate_is_available(id))
558 return -EINVAL;
559
560 return tegra_powergate_set(id, true);
561}
562
563/**
564 * tegra_powergate_power_off() - power off partition
565 * @id: partition ID
566 */
567int tegra_powergate_power_off(unsigned int id)
568{
569 if (!tegra_powergate_is_available(id))
570 return -EINVAL;
571
572 return tegra_powergate_set(id, false);
573}
574EXPORT_SYMBOL(tegra_powergate_power_off);
575
576/**
577 * tegra_powergate_is_powered() - check if partition is powered
578 * @id: partition ID
579 */
580int tegra_powergate_is_powered(unsigned int id)
581{
582 int status;
583
584 if (!tegra_powergate_is_valid(id))
585 return -EINVAL;
586
587 mutex_lock(&pmc->powergates_lock);
588 status = tegra_powergate_state(id);
589 mutex_unlock(&pmc->powergates_lock);
590
591 return status;
592}
593
594/**
595 * tegra_powergate_remove_clamping() - remove power clamps for partition
596 * @id: partition ID
597 */
598int tegra_powergate_remove_clamping(unsigned int id)
599{
600 if (!tegra_powergate_is_available(id))
601 return -EINVAL;
602
603 return __tegra_powergate_remove_clamping(id);
604}
Thierry Reding72323982014-07-11 13:19:06 +0200605EXPORT_SYMBOL(tegra_powergate_remove_clamping);
606
607/**
608 * tegra_powergate_sequence_power_up() - power up partition
609 * @id: partition ID
610 * @clk: clock for partition
611 * @rst: reset for partition
612 *
613 * Must be called with clk disabled, and returns with clk enabled.
614 */
Jon Hunter70293ed2016-02-11 18:03:22 +0000615int tegra_powergate_sequence_power_up(unsigned int id, struct clk *clk,
Thierry Reding72323982014-07-11 13:19:06 +0200616 struct reset_control *rst)
617{
Viresh Kumar495ac332018-05-03 13:56:17 +0530618 struct tegra_powergate *pg;
Jon Huntera3804512016-03-30 10:15:15 +0100619 int err;
Thierry Reding72323982014-07-11 13:19:06 +0200620
Jon Hunter403db2d2016-06-28 11:38:23 +0100621 if (!tegra_powergate_is_available(id))
622 return -EINVAL;
623
Viresh Kumar495ac332018-05-03 13:56:17 +0530624 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
625 if (!pg)
626 return -ENOMEM;
Thierry Reding72323982014-07-11 13:19:06 +0200627
Viresh Kumar495ac332018-05-03 13:56:17 +0530628 pg->id = id;
629 pg->clks = &clk;
630 pg->num_clks = 1;
631 pg->reset = rst;
632 pg->pmc = pmc;
633
634 err = tegra_powergate_power_up(pg, false);
Jon Huntera3804512016-03-30 10:15:15 +0100635 if (err)
636 pr_err("failed to turn on partition %d: %d\n", id, err);
Thierry Reding72323982014-07-11 13:19:06 +0200637
Viresh Kumar495ac332018-05-03 13:56:17 +0530638 kfree(pg);
639
Jon Huntera3804512016-03-30 10:15:15 +0100640 return err;
Thierry Reding72323982014-07-11 13:19:06 +0200641}
642EXPORT_SYMBOL(tegra_powergate_sequence_power_up);
643
644#ifdef CONFIG_SMP
645/**
646 * tegra_get_cpu_powergate_id() - convert from CPU ID to partition ID
647 * @cpuid: CPU partition ID
648 *
649 * Returns the partition ID corresponding to the CPU partition ID or a
650 * negative error code on failure.
651 */
Jon Hunter70293ed2016-02-11 18:03:22 +0000652static int tegra_get_cpu_powergate_id(unsigned int cpuid)
Thierry Reding72323982014-07-11 13:19:06 +0200653{
Jon Hunter70293ed2016-02-11 18:03:22 +0000654 if (pmc->soc && cpuid < pmc->soc->num_cpu_powergates)
Thierry Reding72323982014-07-11 13:19:06 +0200655 return pmc->soc->cpu_powergates[cpuid];
656
657 return -EINVAL;
658}
659
660/**
661 * tegra_pmc_cpu_is_powered() - check if CPU partition is powered
662 * @cpuid: CPU partition ID
663 */
Jon Hunter70293ed2016-02-11 18:03:22 +0000664bool tegra_pmc_cpu_is_powered(unsigned int cpuid)
Thierry Reding72323982014-07-11 13:19:06 +0200665{
666 int id;
667
668 id = tegra_get_cpu_powergate_id(cpuid);
669 if (id < 0)
670 return false;
671
672 return tegra_powergate_is_powered(id);
673}
674
675/**
676 * tegra_pmc_cpu_power_on() - power on CPU partition
677 * @cpuid: CPU partition ID
678 */
Jon Hunter70293ed2016-02-11 18:03:22 +0000679int tegra_pmc_cpu_power_on(unsigned int cpuid)
Thierry Reding72323982014-07-11 13:19:06 +0200680{
681 int id;
682
683 id = tegra_get_cpu_powergate_id(cpuid);
684 if (id < 0)
685 return id;
686
687 return tegra_powergate_set(id, true);
688}
689
690/**
691 * tegra_pmc_cpu_remove_clamping() - remove power clamps for CPU partition
692 * @cpuid: CPU partition ID
693 */
Jon Hunter70293ed2016-02-11 18:03:22 +0000694int tegra_pmc_cpu_remove_clamping(unsigned int cpuid)
Thierry Reding72323982014-07-11 13:19:06 +0200695{
696 int id;
697
698 id = tegra_get_cpu_powergate_id(cpuid);
699 if (id < 0)
700 return id;
701
702 return tegra_powergate_remove_clamping(id);
703}
704#endif /* CONFIG_SMP */
705
David Riley78921582015-03-18 10:52:25 +0100706static int tegra_pmc_restart_notify(struct notifier_block *this,
707 unsigned long action, void *data)
Thierry Reding72323982014-07-11 13:19:06 +0200708{
David Riley78921582015-03-18 10:52:25 +0100709 const char *cmd = data;
Thierry Reding72323982014-07-11 13:19:06 +0200710 u32 value;
711
Thierry Reding5be22552017-08-30 12:32:58 +0200712 value = readl(pmc->scratch + pmc->soc->regs->scratch0);
Thierry Reding72323982014-07-11 13:19:06 +0200713 value &= ~PMC_SCRATCH0_MODE_MASK;
714
715 if (cmd) {
716 if (strcmp(cmd, "recovery") == 0)
717 value |= PMC_SCRATCH0_MODE_RECOVERY;
718
719 if (strcmp(cmd, "bootloader") == 0)
720 value |= PMC_SCRATCH0_MODE_BOOTLOADER;
721
722 if (strcmp(cmd, "forced-recovery") == 0)
723 value |= PMC_SCRATCH0_MODE_RCM;
724 }
725
Thierry Reding5be22552017-08-30 12:32:58 +0200726 writel(value, pmc->scratch + pmc->soc->regs->scratch0);
Thierry Reding72323982014-07-11 13:19:06 +0200727
Thierry Redingf5353c62015-12-30 17:13:29 +0100728 /* reset everything but PMC_SCRATCH0 and PMC_RST_STATUS */
729 value = tegra_pmc_readl(PMC_CNTRL);
730 value |= PMC_CNTRL_MAIN_RST;
731 tegra_pmc_writel(value, PMC_CNTRL);
David Riley78921582015-03-18 10:52:25 +0100732
733 return NOTIFY_DONE;
Thierry Reding72323982014-07-11 13:19:06 +0200734}
735
David Riley78921582015-03-18 10:52:25 +0100736static struct notifier_block tegra_pmc_restart_handler = {
737 .notifier_call = tegra_pmc_restart_notify,
738 .priority = 128,
739};
740
Thierry Reding72323982014-07-11 13:19:06 +0200741static int powergate_show(struct seq_file *s, void *data)
742{
743 unsigned int i;
Jon Hunterc3ea2972016-02-11 18:03:25 +0000744 int status;
Thierry Reding72323982014-07-11 13:19:06 +0200745
746 seq_printf(s, " powergate powered\n");
747 seq_printf(s, "------------------\n");
748
749 for (i = 0; i < pmc->soc->num_powergates; i++) {
Jon Hunterc3ea2972016-02-11 18:03:25 +0000750 status = tegra_powergate_is_powered(i);
751 if (status < 0)
Thierry Reding72323982014-07-11 13:19:06 +0200752 continue;
753
754 seq_printf(s, " %9s %7s\n", pmc->soc->powergates[i],
Jon Hunterc3ea2972016-02-11 18:03:25 +0000755 status ? "yes" : "no");
Thierry Reding72323982014-07-11 13:19:06 +0200756 }
757
758 return 0;
759}
760
761static int powergate_open(struct inode *inode, struct file *file)
762{
763 return single_open(file, powergate_show, inode->i_private);
764}
765
766static const struct file_operations powergate_fops = {
767 .open = powergate_open,
768 .read = seq_read,
769 .llseek = seq_lseek,
770 .release = single_release,
771};
772
773static int tegra_powergate_debugfs_init(void)
774{
Jon Hunter3195ac62015-12-04 14:57:05 +0000775 pmc->debugfs = debugfs_create_file("powergate", S_IRUGO, NULL, NULL,
776 &powergate_fops);
777 if (!pmc->debugfs)
Thierry Reding72323982014-07-11 13:19:06 +0200778 return -ENOMEM;
779
780 return 0;
781}
782
Jon Huntera3804512016-03-30 10:15:15 +0100783static int tegra_powergate_of_get_clks(struct tegra_powergate *pg,
784 struct device_node *np)
785{
786 struct clk *clk;
787 unsigned int i, count;
788 int err;
789
Geert Uytterhoeven3fd01212018-04-18 16:50:04 +0200790 count = of_clk_get_parent_count(np);
Jon Huntera3804512016-03-30 10:15:15 +0100791 if (count == 0)
792 return -ENODEV;
793
794 pg->clks = kcalloc(count, sizeof(clk), GFP_KERNEL);
795 if (!pg->clks)
796 return -ENOMEM;
797
798 for (i = 0; i < count; i++) {
799 pg->clks[i] = of_clk_get(np, i);
800 if (IS_ERR(pg->clks[i])) {
801 err = PTR_ERR(pg->clks[i]);
802 goto err;
803 }
804 }
805
806 pg->num_clks = count;
807
808 return 0;
809
810err:
811 while (i--)
812 clk_put(pg->clks[i]);
Thierry Redingda8f4b42016-06-30 12:12:55 +0200813
Jon Huntera3804512016-03-30 10:15:15 +0100814 kfree(pg->clks);
815
816 return err;
817}
818
819static int tegra_powergate_of_get_resets(struct tegra_powergate *pg,
Jon Hunter05cfb982016-06-29 10:17:47 +0100820 struct device_node *np, bool off)
Jon Huntera3804512016-03-30 10:15:15 +0100821{
Jon Huntera3804512016-03-30 10:15:15 +0100822 int err;
823
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200824 pg->reset = of_reset_control_array_get_exclusive(np);
825 if (IS_ERR(pg->reset)) {
826 err = PTR_ERR(pg->reset);
827 pr_err("failed to get device resets: %d\n", err);
828 return err;
Jon Huntera3804512016-03-30 10:15:15 +0100829 }
830
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200831 if (off)
832 err = reset_control_assert(pg->reset);
833 else
834 err = reset_control_deassert(pg->reset);
Jon Huntera3804512016-03-30 10:15:15 +0100835
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200836 if (err)
837 reset_control_put(pg->reset);
Jon Huntera3804512016-03-30 10:15:15 +0100838
839 return err;
840}
841
842static void tegra_powergate_add(struct tegra_pmc *pmc, struct device_node *np)
843{
844 struct tegra_powergate *pg;
Jon Hunterc2710ac2016-06-30 11:56:24 +0100845 int id, err;
Jon Huntera3804512016-03-30 10:15:15 +0100846 bool off;
Jon Huntera3804512016-03-30 10:15:15 +0100847
848 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
849 if (!pg)
Jon Hunterc2710ac2016-06-30 11:56:24 +0100850 return;
Jon Huntera3804512016-03-30 10:15:15 +0100851
852 id = tegra_powergate_lookup(pmc, np->name);
Jon Hunterc2710ac2016-06-30 11:56:24 +0100853 if (id < 0) {
Rob Herringdc37a252018-08-27 20:02:33 -0500854 pr_err("powergate lookup failed for %pOFn: %d\n", np, id);
Jon Huntera3804512016-03-30 10:15:15 +0100855 goto free_mem;
Jon Hunterc2710ac2016-06-30 11:56:24 +0100856 }
Jon Huntera3804512016-03-30 10:15:15 +0100857
858 /*
859 * Clear the bit for this powergate so it cannot be managed
860 * directly via the legacy APIs for controlling powergates.
861 */
862 clear_bit(id, pmc->powergates_available);
863
864 pg->id = id;
865 pg->genpd.name = np->name;
866 pg->genpd.power_off = tegra_genpd_power_off;
867 pg->genpd.power_on = tegra_genpd_power_on;
868 pg->pmc = pmc;
869
Jon Hunter05cfb982016-06-29 10:17:47 +0100870 off = !tegra_powergate_is_powered(pg->id);
871
Jon Hunterc2710ac2016-06-30 11:56:24 +0100872 err = tegra_powergate_of_get_clks(pg, np);
873 if (err < 0) {
Rob Herringdc37a252018-08-27 20:02:33 -0500874 pr_err("failed to get clocks for %pOFn: %d\n", np, err);
Jon Huntera3804512016-03-30 10:15:15 +0100875 goto set_available;
Jon Hunterc2710ac2016-06-30 11:56:24 +0100876 }
Jon Huntera3804512016-03-30 10:15:15 +0100877
Jon Hunterc2710ac2016-06-30 11:56:24 +0100878 err = tegra_powergate_of_get_resets(pg, np, off);
879 if (err < 0) {
Rob Herringdc37a252018-08-27 20:02:33 -0500880 pr_err("failed to get resets for %pOFn: %d\n", np, err);
Jon Huntera3804512016-03-30 10:15:15 +0100881 goto remove_clks;
Jon Hunterc2710ac2016-06-30 11:56:24 +0100882 }
Jon Huntera3804512016-03-30 10:15:15 +0100883
Jon Hunter0b137342016-10-22 20:23:56 +0100884 if (!IS_ENABLED(CONFIG_PM_GENERIC_DOMAINS)) {
885 if (off)
886 WARN_ON(tegra_powergate_power_up(pg, true));
887
888 goto remove_resets;
889 }
Jon Huntere2d17962016-06-30 11:56:25 +0100890
Jon Huntercd5ceda2016-10-22 20:23:55 +0100891 err = pm_genpd_init(&pg->genpd, NULL, off);
892 if (err < 0) {
Rob Herringdc37a252018-08-27 20:02:33 -0500893 pr_err("failed to initialise PM domain %pOFn: %d\n", np,
Jon Huntercd5ceda2016-10-22 20:23:55 +0100894 err);
895 goto remove_resets;
896 }
Jon Huntera3804512016-03-30 10:15:15 +0100897
Jon Hunterc2710ac2016-06-30 11:56:24 +0100898 err = of_genpd_add_provider_simple(np, &pg->genpd);
899 if (err < 0) {
Rob Herringdc37a252018-08-27 20:02:33 -0500900 pr_err("failed to add PM domain provider for %pOFn: %d\n",
901 np, err);
Jon Hunter0b137342016-10-22 20:23:56 +0100902 goto remove_genpd;
Jon Hunterc2710ac2016-06-30 11:56:24 +0100903 }
Jon Huntera3804512016-03-30 10:15:15 +0100904
Thierry Reding45221122016-11-08 11:05:03 +0100905 pr_debug("added PM domain %s\n", pg->genpd.name);
Jon Huntera3804512016-03-30 10:15:15 +0100906
907 return;
908
Jon Hunter0b137342016-10-22 20:23:56 +0100909remove_genpd:
910 pm_genpd_remove(&pg->genpd);
Jon Huntere2d17962016-06-30 11:56:25 +0100911
Jon Huntera3804512016-03-30 10:15:15 +0100912remove_resets:
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200913 reset_control_put(pg->reset);
Jon Huntera3804512016-03-30 10:15:15 +0100914
915remove_clks:
916 while (pg->num_clks--)
917 clk_put(pg->clks[pg->num_clks]);
Thierry Redingda8f4b42016-06-30 12:12:55 +0200918
Jon Huntera3804512016-03-30 10:15:15 +0100919 kfree(pg->clks);
920
921set_available:
922 set_bit(id, pmc->powergates_available);
923
924free_mem:
925 kfree(pg);
Jon Huntera3804512016-03-30 10:15:15 +0100926}
927
Jon Huntere2d17962016-06-30 11:56:25 +0100928static void tegra_powergate_init(struct tegra_pmc *pmc,
929 struct device_node *parent)
Jon Huntera3804512016-03-30 10:15:15 +0100930{
931 struct device_node *np, *child;
Jon Huntere2d17962016-06-30 11:56:25 +0100932 unsigned int i;
Jon Huntera3804512016-03-30 10:15:15 +0100933
Jon Huntere2d17962016-06-30 11:56:25 +0100934 /* Create a bitmap of the available and valid partitions */
935 for (i = 0; i < pmc->soc->num_powergates; i++)
936 if (pmc->soc->powergates[i])
937 set_bit(i, pmc->powergates_available);
938
939 np = of_get_child_by_name(parent, "powergates");
Jon Huntera3804512016-03-30 10:15:15 +0100940 if (!np)
941 return;
942
Tuomas Tynkkynen0c106e52017-07-29 02:58:43 +0300943 for_each_child_of_node(np, child)
Jon Huntera3804512016-03-30 10:15:15 +0100944 tegra_powergate_add(pmc, child);
Jon Huntera3804512016-03-30 10:15:15 +0100945
946 of_node_put(np);
947}
948
Laxman Dewangan21b49912016-10-10 15:14:34 +0200949static const struct tegra_io_pad_soc *
950tegra_io_pad_find(struct tegra_pmc *pmc, enum tegra_io_pad id)
Thierry Reding72323982014-07-11 13:19:06 +0200951{
Laxman Dewangan21b49912016-10-10 15:14:34 +0200952 unsigned int i;
953
954 for (i = 0; i < pmc->soc->num_io_pads; i++)
955 if (pmc->soc->io_pads[i].id == id)
956 return &pmc->soc->io_pads[i];
957
958 return NULL;
959}
960
Aapo Vienamo00ead3c2018-08-10 21:08:08 +0300961static int tegra_io_pad_get_dpd_register_bit(enum tegra_io_pad id,
962 unsigned long *request,
963 unsigned long *status,
964 u32 *mask)
Laxman Dewangan21b49912016-10-10 15:14:34 +0200965{
966 const struct tegra_io_pad_soc *pad;
Thierry Reding72323982014-07-11 13:19:06 +0200967
Laxman Dewangan21b49912016-10-10 15:14:34 +0200968 pad = tegra_io_pad_find(pmc, id);
Thierry Reding54e24722016-11-08 10:58:32 +0100969 if (!pad) {
970 pr_err("invalid I/O pad ID %u\n", id);
Laxman Dewangan21b49912016-10-10 15:14:34 +0200971 return -ENOENT;
Thierry Reding54e24722016-11-08 10:58:32 +0100972 }
Thierry Reding72323982014-07-11 13:19:06 +0200973
Laxman Dewangan21b49912016-10-10 15:14:34 +0200974 if (pad->dpd == UINT_MAX)
975 return -ENOTSUPP;
Thierry Reding72323982014-07-11 13:19:06 +0200976
Jon Hunter27b12b4e2016-10-22 20:23:53 +0100977 *mask = BIT(pad->dpd % 32);
Laxman Dewangan21b49912016-10-10 15:14:34 +0200978
979 if (pad->dpd < 32) {
Thierry Reding5be22552017-08-30 12:32:58 +0200980 *status = pmc->soc->regs->dpd_status;
981 *request = pmc->soc->regs->dpd_req;
Thierry Reding72323982014-07-11 13:19:06 +0200982 } else {
Thierry Reding5be22552017-08-30 12:32:58 +0200983 *status = pmc->soc->regs->dpd2_status;
984 *request = pmc->soc->regs->dpd2_req;
Thierry Reding72323982014-07-11 13:19:06 +0200985 }
986
Aapo Vienamo00ead3c2018-08-10 21:08:08 +0300987 return 0;
988}
989
990static int tegra_io_pad_prepare(enum tegra_io_pad id, unsigned long *request,
991 unsigned long *status, u32 *mask)
992{
993 unsigned long rate, value;
994 int err;
995
996 err = tegra_io_pad_get_dpd_register_bit(id, request, status, mask);
997 if (err)
998 return err;
999
Thierry Reding5be22552017-08-30 12:32:58 +02001000 if (pmc->clk) {
1001 rate = clk_get_rate(pmc->clk);
1002 if (!rate) {
1003 pr_err("failed to get clock rate\n");
1004 return -ENODEV;
1005 }
1006
1007 tegra_pmc_writel(DPD_SAMPLE_ENABLE, DPD_SAMPLE);
1008
1009 /* must be at least 200 ns, in APB (PCLK) clock cycles */
1010 value = DIV_ROUND_UP(1000000000, rate);
1011 value = DIV_ROUND_UP(200, value);
1012 tegra_pmc_writel(value, SEL_DPD_TIM);
Thierry Reding54e24722016-11-08 10:58:32 +01001013 }
Thierry Reding72323982014-07-11 13:19:06 +02001014
Thierry Reding72323982014-07-11 13:19:06 +02001015 return 0;
1016}
1017
Laxman Dewangan21b49912016-10-10 15:14:34 +02001018static int tegra_io_pad_poll(unsigned long offset, u32 mask,
1019 u32 val, unsigned long timeout)
Thierry Reding72323982014-07-11 13:19:06 +02001020{
Laxman Dewangan84cf85e2016-06-17 18:36:13 +05301021 u32 value;
Thierry Reding72323982014-07-11 13:19:06 +02001022
1023 timeout = jiffies + msecs_to_jiffies(timeout);
1024
1025 while (time_after(timeout, jiffies)) {
1026 value = tegra_pmc_readl(offset);
1027 if ((value & mask) == val)
1028 return 0;
1029
1030 usleep_range(250, 1000);
1031 }
1032
1033 return -ETIMEDOUT;
1034}
1035
Laxman Dewangan21b49912016-10-10 15:14:34 +02001036static void tegra_io_pad_unprepare(void)
Thierry Reding72323982014-07-11 13:19:06 +02001037{
Thierry Reding5be22552017-08-30 12:32:58 +02001038 if (pmc->clk)
1039 tegra_pmc_writel(DPD_SAMPLE_DISABLE, DPD_SAMPLE);
Thierry Reding72323982014-07-11 13:19:06 +02001040}
1041
Laxman Dewangan21b49912016-10-10 15:14:34 +02001042/**
1043 * tegra_io_pad_power_enable() - enable power to I/O pad
1044 * @id: Tegra I/O pad ID for which to enable power
1045 *
1046 * Returns: 0 on success or a negative error code on failure.
1047 */
1048int tegra_io_pad_power_enable(enum tegra_io_pad id)
Thierry Reding72323982014-07-11 13:19:06 +02001049{
Vince Hsua9ccc1232016-08-11 09:13:36 +08001050 unsigned long request, status;
Jon Hunter27b12b4e2016-10-22 20:23:53 +01001051 u32 mask;
Thierry Reding72323982014-07-11 13:19:06 +02001052 int err;
1053
Jon Huntere8cf6612016-02-11 18:03:21 +00001054 mutex_lock(&pmc->powergates_lock);
1055
Jon Hunter27b12b4e2016-10-22 20:23:53 +01001056 err = tegra_io_pad_prepare(id, &request, &status, &mask);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001057 if (err < 0) {
Thierry Reding54e24722016-11-08 10:58:32 +01001058 pr_err("failed to prepare I/O pad: %d\n", err);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001059 goto unlock;
1060 }
Thierry Reding72323982014-07-11 13:19:06 +02001061
Jon Hunter27b12b4e2016-10-22 20:23:53 +01001062 tegra_pmc_writel(IO_DPD_REQ_CODE_OFF | mask, request);
Thierry Reding72323982014-07-11 13:19:06 +02001063
Jon Hunter27b12b4e2016-10-22 20:23:53 +01001064 err = tegra_io_pad_poll(status, mask, 0, 250);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001065 if (err < 0) {
Thierry Reding54e24722016-11-08 10:58:32 +01001066 pr_err("failed to enable I/O pad: %d\n", err);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001067 goto unlock;
Thierry Reding592431b2015-08-04 15:25:03 +02001068 }
Thierry Reding72323982014-07-11 13:19:06 +02001069
Laxman Dewangan21b49912016-10-10 15:14:34 +02001070 tegra_io_pad_unprepare();
Thierry Reding72323982014-07-11 13:19:06 +02001071
Laxman Dewangan21b49912016-10-10 15:14:34 +02001072unlock:
Jon Huntere8cf6612016-02-11 18:03:21 +00001073 mutex_unlock(&pmc->powergates_lock);
Jon Huntere8cf6612016-02-11 18:03:21 +00001074 return err;
Thierry Reding72323982014-07-11 13:19:06 +02001075}
Laxman Dewangan21b49912016-10-10 15:14:34 +02001076EXPORT_SYMBOL(tegra_io_pad_power_enable);
Thierry Reding72323982014-07-11 13:19:06 +02001077
Laxman Dewangan21b49912016-10-10 15:14:34 +02001078/**
1079 * tegra_io_pad_power_disable() - disable power to I/O pad
1080 * @id: Tegra I/O pad ID for which to disable power
1081 *
1082 * Returns: 0 on success or a negative error code on failure.
1083 */
1084int tegra_io_pad_power_disable(enum tegra_io_pad id)
Thierry Reding72323982014-07-11 13:19:06 +02001085{
Vince Hsua9ccc1232016-08-11 09:13:36 +08001086 unsigned long request, status;
Jon Hunter27b12b4e2016-10-22 20:23:53 +01001087 u32 mask;
Thierry Reding72323982014-07-11 13:19:06 +02001088 int err;
1089
Jon Huntere8cf6612016-02-11 18:03:21 +00001090 mutex_lock(&pmc->powergates_lock);
1091
Jon Hunter27b12b4e2016-10-22 20:23:53 +01001092 err = tegra_io_pad_prepare(id, &request, &status, &mask);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001093 if (err < 0) {
Thierry Reding54e24722016-11-08 10:58:32 +01001094 pr_err("failed to prepare I/O pad: %d\n", err);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001095 goto unlock;
Thierry Reding592431b2015-08-04 15:25:03 +02001096 }
Thierry Reding72323982014-07-11 13:19:06 +02001097
Jon Hunter27b12b4e2016-10-22 20:23:53 +01001098 tegra_pmc_writel(IO_DPD_REQ_CODE_ON | mask, request);
Thierry Reding72323982014-07-11 13:19:06 +02001099
Jon Hunter27b12b4e2016-10-22 20:23:53 +01001100 err = tegra_io_pad_poll(status, mask, mask, 250);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001101 if (err < 0) {
Thierry Reding54e24722016-11-08 10:58:32 +01001102 pr_err("failed to disable I/O pad: %d\n", err);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001103 goto unlock;
1104 }
Thierry Reding72323982014-07-11 13:19:06 +02001105
Laxman Dewangan21b49912016-10-10 15:14:34 +02001106 tegra_io_pad_unprepare();
Thierry Reding72323982014-07-11 13:19:06 +02001107
Laxman Dewangan21b49912016-10-10 15:14:34 +02001108unlock:
1109 mutex_unlock(&pmc->powergates_lock);
1110 return err;
1111}
1112EXPORT_SYMBOL(tegra_io_pad_power_disable);
1113
Aapo Vienamof142b9d2018-08-10 21:08:09 +03001114static int tegra_io_pad_is_powered(enum tegra_io_pad id)
1115{
1116 unsigned long request, status;
1117 u32 mask, value;
1118 int err;
1119
1120 err = tegra_io_pad_get_dpd_register_bit(id, &request, &status, &mask);
1121 if (err)
1122 return err;
1123
1124 value = tegra_pmc_readl(status);
1125
1126 return !(value & mask);
1127}
1128
Aapo Vienamofccf0f72018-08-10 21:08:11 +03001129static int tegra_io_pad_set_voltage(enum tegra_io_pad id, int voltage)
Laxman Dewangan21b49912016-10-10 15:14:34 +02001130{
1131 const struct tegra_io_pad_soc *pad;
1132 u32 value;
1133
1134 pad = tegra_io_pad_find(pmc, id);
1135 if (!pad)
1136 return -ENOENT;
1137
1138 if (pad->voltage == UINT_MAX)
1139 return -ENOTSUPP;
1140
1141 mutex_lock(&pmc->powergates_lock);
1142
Aapo Vienamo13136a42018-08-10 21:08:07 +03001143 if (pmc->soc->has_impl_33v_pwr) {
1144 value = tegra_pmc_readl(PMC_IMPL_E_33V_PWR);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001145
Aapo Vienamofccf0f72018-08-10 21:08:11 +03001146 if (voltage == TEGRA_IO_PAD_VOLTAGE_1V8)
Aapo Vienamo13136a42018-08-10 21:08:07 +03001147 value &= ~BIT(pad->voltage);
1148 else
1149 value |= BIT(pad->voltage);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001150
Aapo Vienamo13136a42018-08-10 21:08:07 +03001151 tegra_pmc_writel(value, PMC_IMPL_E_33V_PWR);
1152 } else {
1153 /* write-enable PMC_PWR_DET_VALUE[pad->voltage] */
1154 value = tegra_pmc_readl(PMC_PWR_DET);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001155 value |= BIT(pad->voltage);
Aapo Vienamo13136a42018-08-10 21:08:07 +03001156 tegra_pmc_writel(value, PMC_PWR_DET);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001157
Aapo Vienamo13136a42018-08-10 21:08:07 +03001158 /* update I/O voltage */
1159 value = tegra_pmc_readl(PMC_PWR_DET_VALUE);
1160
Aapo Vienamofccf0f72018-08-10 21:08:11 +03001161 if (voltage == TEGRA_IO_PAD_VOLTAGE_1V8)
Aapo Vienamo13136a42018-08-10 21:08:07 +03001162 value &= ~BIT(pad->voltage);
1163 else
1164 value |= BIT(pad->voltage);
1165
1166 tegra_pmc_writel(value, PMC_PWR_DET_VALUE);
1167 }
Laxman Dewangan21b49912016-10-10 15:14:34 +02001168
Jon Huntere8cf6612016-02-11 18:03:21 +00001169 mutex_unlock(&pmc->powergates_lock);
1170
Laxman Dewangan21b49912016-10-10 15:14:34 +02001171 usleep_range(100, 250);
1172
1173 return 0;
1174}
Laxman Dewangan21b49912016-10-10 15:14:34 +02001175
Aapo Vienamofccf0f72018-08-10 21:08:11 +03001176static int tegra_io_pad_get_voltage(enum tegra_io_pad id)
Laxman Dewangan21b49912016-10-10 15:14:34 +02001177{
1178 const struct tegra_io_pad_soc *pad;
1179 u32 value;
1180
1181 pad = tegra_io_pad_find(pmc, id);
1182 if (!pad)
1183 return -ENOENT;
1184
1185 if (pad->voltage == UINT_MAX)
1186 return -ENOTSUPP;
1187
Aapo Vienamo13136a42018-08-10 21:08:07 +03001188 if (pmc->soc->has_impl_33v_pwr)
1189 value = tegra_pmc_readl(PMC_IMPL_E_33V_PWR);
1190 else
1191 value = tegra_pmc_readl(PMC_PWR_DET_VALUE);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001192
1193 if ((value & BIT(pad->voltage)) == 0)
Aapo Vienamofccf0f72018-08-10 21:08:11 +03001194 return TEGRA_IO_PAD_VOLTAGE_1V8;
Laxman Dewangan21b49912016-10-10 15:14:34 +02001195
Aapo Vienamofccf0f72018-08-10 21:08:11 +03001196 return TEGRA_IO_PAD_VOLTAGE_3V3;
Laxman Dewangan21b49912016-10-10 15:14:34 +02001197}
Laxman Dewangan21b49912016-10-10 15:14:34 +02001198
1199/**
1200 * tegra_io_rail_power_on() - enable power to I/O rail
1201 * @id: Tegra I/O pad ID for which to enable power
1202 *
1203 * See also: tegra_io_pad_power_enable()
1204 */
1205int tegra_io_rail_power_on(unsigned int id)
1206{
1207 return tegra_io_pad_power_enable(id);
1208}
1209EXPORT_SYMBOL(tegra_io_rail_power_on);
1210
1211/**
1212 * tegra_io_rail_power_off() - disable power to I/O rail
1213 * @id: Tegra I/O pad ID for which to disable power
1214 *
1215 * See also: tegra_io_pad_power_disable()
1216 */
1217int tegra_io_rail_power_off(unsigned int id)
1218{
1219 return tegra_io_pad_power_disable(id);
Thierry Reding72323982014-07-11 13:19:06 +02001220}
1221EXPORT_SYMBOL(tegra_io_rail_power_off);
1222
1223#ifdef CONFIG_PM_SLEEP
1224enum tegra_suspend_mode tegra_pmc_get_suspend_mode(void)
1225{
1226 return pmc->suspend_mode;
1227}
1228
1229void tegra_pmc_set_suspend_mode(enum tegra_suspend_mode mode)
1230{
1231 if (mode < TEGRA_SUSPEND_NONE || mode >= TEGRA_MAX_SUSPEND_MODE)
1232 return;
1233
1234 pmc->suspend_mode = mode;
1235}
1236
1237void tegra_pmc_enter_suspend_mode(enum tegra_suspend_mode mode)
1238{
1239 unsigned long long rate = 0;
1240 u32 value;
1241
1242 switch (mode) {
1243 case TEGRA_SUSPEND_LP1:
1244 rate = 32768;
1245 break;
1246
1247 case TEGRA_SUSPEND_LP2:
1248 rate = clk_get_rate(pmc->clk);
1249 break;
1250
1251 default:
1252 break;
1253 }
1254
1255 if (WARN_ON_ONCE(rate == 0))
1256 rate = 100000000;
1257
1258 if (rate != pmc->rate) {
1259 u64 ticks;
1260
1261 ticks = pmc->cpu_good_time * rate + USEC_PER_SEC - 1;
1262 do_div(ticks, USEC_PER_SEC);
1263 tegra_pmc_writel(ticks, PMC_CPUPWRGOOD_TIMER);
1264
1265 ticks = pmc->cpu_off_time * rate + USEC_PER_SEC - 1;
1266 do_div(ticks, USEC_PER_SEC);
1267 tegra_pmc_writel(ticks, PMC_CPUPWROFF_TIMER);
1268
1269 wmb();
1270
1271 pmc->rate = rate;
1272 }
1273
1274 value = tegra_pmc_readl(PMC_CNTRL);
1275 value &= ~PMC_CNTRL_SIDE_EFFECT_LP0;
1276 value |= PMC_CNTRL_CPU_PWRREQ_OE;
1277 tegra_pmc_writel(value, PMC_CNTRL);
1278}
1279#endif
1280
1281static int tegra_pmc_parse_dt(struct tegra_pmc *pmc, struct device_node *np)
1282{
1283 u32 value, values[2];
1284
1285 if (of_property_read_u32(np, "nvidia,suspend-mode", &value)) {
1286 } else {
1287 switch (value) {
1288 case 0:
1289 pmc->suspend_mode = TEGRA_SUSPEND_LP0;
1290 break;
1291
1292 case 1:
1293 pmc->suspend_mode = TEGRA_SUSPEND_LP1;
1294 break;
1295
1296 case 2:
1297 pmc->suspend_mode = TEGRA_SUSPEND_LP2;
1298 break;
1299
1300 default:
1301 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1302 break;
1303 }
1304 }
1305
1306 pmc->suspend_mode = tegra_pm_validate_suspend_mode(pmc->suspend_mode);
1307
1308 if (of_property_read_u32(np, "nvidia,cpu-pwr-good-time", &value))
1309 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1310
1311 pmc->cpu_good_time = value;
1312
1313 if (of_property_read_u32(np, "nvidia,cpu-pwr-off-time", &value))
1314 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1315
1316 pmc->cpu_off_time = value;
1317
1318 if (of_property_read_u32_array(np, "nvidia,core-pwr-good-time",
1319 values, ARRAY_SIZE(values)))
1320 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1321
1322 pmc->core_osc_time = values[0];
1323 pmc->core_pmu_time = values[1];
1324
1325 if (of_property_read_u32(np, "nvidia,core-pwr-off-time", &value))
1326 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1327
1328 pmc->core_off_time = value;
1329
1330 pmc->corereq_high = of_property_read_bool(np,
1331 "nvidia,core-power-req-active-high");
1332
1333 pmc->sysclkreq_high = of_property_read_bool(np,
1334 "nvidia,sys-clock-req-active-high");
1335
1336 pmc->combined_req = of_property_read_bool(np,
1337 "nvidia,combined-power-req");
1338
1339 pmc->cpu_pwr_good_en = of_property_read_bool(np,
1340 "nvidia,cpu-pwr-good-en");
1341
1342 if (of_property_read_u32_array(np, "nvidia,lp0-vec", values,
1343 ARRAY_SIZE(values)))
1344 if (pmc->suspend_mode == TEGRA_SUSPEND_LP0)
1345 pmc->suspend_mode = TEGRA_SUSPEND_LP1;
1346
1347 pmc->lp0_vec_phys = values[0];
1348 pmc->lp0_vec_size = values[1];
1349
1350 return 0;
1351}
1352
1353static void tegra_pmc_init(struct tegra_pmc *pmc)
1354{
Thierry Reding5be22552017-08-30 12:32:58 +02001355 if (pmc->soc->init)
1356 pmc->soc->init(pmc);
Thierry Reding72323982014-07-11 13:19:06 +02001357}
1358
Jon Hunter1e52efdf2015-12-04 14:57:04 +00001359static void tegra_pmc_init_tsense_reset(struct tegra_pmc *pmc)
Mikko Perttunen3568df32015-01-06 12:52:58 +02001360{
1361 static const char disabled[] = "emergency thermal reset disabled";
1362 u32 pmu_addr, ctrl_id, reg_addr, reg_data, pinmux;
1363 struct device *dev = pmc->dev;
1364 struct device_node *np;
1365 u32 value, checksum;
1366
1367 if (!pmc->soc->has_tsense_reset)
Thierry Reding95169cd2015-07-09 09:59:55 +02001368 return;
Mikko Perttunen3568df32015-01-06 12:52:58 +02001369
Johan Hovold1dc6bd52017-11-15 10:44:58 +01001370 np = of_get_child_by_name(pmc->dev->of_node, "i2c-thermtrip");
Mikko Perttunen3568df32015-01-06 12:52:58 +02001371 if (!np) {
1372 dev_warn(dev, "i2c-thermtrip node not found, %s.\n", disabled);
Thierry Reding95169cd2015-07-09 09:59:55 +02001373 return;
Mikko Perttunen3568df32015-01-06 12:52:58 +02001374 }
1375
1376 if (of_property_read_u32(np, "nvidia,i2c-controller-id", &ctrl_id)) {
1377 dev_err(dev, "I2C controller ID missing, %s.\n", disabled);
1378 goto out;
1379 }
1380
1381 if (of_property_read_u32(np, "nvidia,bus-addr", &pmu_addr)) {
1382 dev_err(dev, "nvidia,bus-addr missing, %s.\n", disabled);
1383 goto out;
1384 }
1385
1386 if (of_property_read_u32(np, "nvidia,reg-addr", &reg_addr)) {
1387 dev_err(dev, "nvidia,reg-addr missing, %s.\n", disabled);
1388 goto out;
1389 }
1390
1391 if (of_property_read_u32(np, "nvidia,reg-data", &reg_data)) {
1392 dev_err(dev, "nvidia,reg-data missing, %s.\n", disabled);
1393 goto out;
1394 }
1395
1396 if (of_property_read_u32(np, "nvidia,pinmux-id", &pinmux))
1397 pinmux = 0;
1398
1399 value = tegra_pmc_readl(PMC_SENSOR_CTRL);
1400 value |= PMC_SENSOR_CTRL_SCRATCH_WRITE;
1401 tegra_pmc_writel(value, PMC_SENSOR_CTRL);
1402
1403 value = (reg_data << PMC_SCRATCH54_DATA_SHIFT) |
1404 (reg_addr << PMC_SCRATCH54_ADDR_SHIFT);
1405 tegra_pmc_writel(value, PMC_SCRATCH54);
1406
1407 value = PMC_SCRATCH55_RESET_TEGRA;
1408 value |= ctrl_id << PMC_SCRATCH55_CNTRL_ID_SHIFT;
1409 value |= pinmux << PMC_SCRATCH55_PINMUX_SHIFT;
1410 value |= pmu_addr << PMC_SCRATCH55_I2CSLV1_SHIFT;
1411
1412 /*
1413 * Calculate checksum of SCRATCH54, SCRATCH55 fields. Bits 23:16 will
1414 * contain the checksum and are currently zero, so they are not added.
1415 */
1416 checksum = reg_addr + reg_data + (value & 0xff) + ((value >> 8) & 0xff)
1417 + ((value >> 24) & 0xff);
1418 checksum &= 0xff;
1419 checksum = 0x100 - checksum;
1420
1421 value |= checksum << PMC_SCRATCH55_CHECKSUM_SHIFT;
1422
1423 tegra_pmc_writel(value, PMC_SCRATCH55);
1424
1425 value = tegra_pmc_readl(PMC_SENSOR_CTRL);
1426 value |= PMC_SENSOR_CTRL_ENABLE_RST;
1427 tegra_pmc_writel(value, PMC_SENSOR_CTRL);
1428
1429 dev_info(pmc->dev, "emergency thermal reset enabled\n");
1430
1431out:
1432 of_node_put(np);
Mikko Perttunen3568df32015-01-06 12:52:58 +02001433}
1434
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001435static int tegra_io_pad_pinctrl_get_groups_count(struct pinctrl_dev *pctl_dev)
1436{
1437 return pmc->soc->num_io_pads;
1438}
1439
1440static const char *tegra_io_pad_pinctrl_get_group_name(
1441 struct pinctrl_dev *pctl, unsigned int group)
1442{
1443 return pmc->soc->io_pads[group].name;
1444}
1445
1446static int tegra_io_pad_pinctrl_get_group_pins(struct pinctrl_dev *pctl_dev,
1447 unsigned int group,
1448 const unsigned int **pins,
1449 unsigned int *num_pins)
1450{
1451 *pins = &pmc->soc->io_pads[group].id;
1452 *num_pins = 1;
1453 return 0;
1454}
1455
1456static const struct pinctrl_ops tegra_io_pad_pinctrl_ops = {
1457 .get_groups_count = tegra_io_pad_pinctrl_get_groups_count,
1458 .get_group_name = tegra_io_pad_pinctrl_get_group_name,
1459 .get_group_pins = tegra_io_pad_pinctrl_get_group_pins,
1460 .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
1461 .dt_free_map = pinconf_generic_dt_free_map,
1462};
1463
1464static int tegra_io_pad_pinconf_get(struct pinctrl_dev *pctl_dev,
1465 unsigned int pin, unsigned long *config)
1466{
1467 const struct tegra_io_pad_soc *pad = tegra_io_pad_find(pmc, pin);
1468 enum pin_config_param param = pinconf_to_config_param(*config);
1469 int ret;
1470 u32 arg;
1471
1472 if (!pad)
1473 return -EINVAL;
1474
1475 switch (param) {
1476 case PIN_CONFIG_POWER_SOURCE:
1477 ret = tegra_io_pad_get_voltage(pad->id);
1478 if (ret < 0)
1479 return ret;
1480 arg = ret;
1481 break;
1482 case PIN_CONFIG_LOW_POWER_MODE:
1483 ret = tegra_io_pad_is_powered(pad->id);
1484 if (ret < 0)
1485 return ret;
1486 arg = !ret;
1487 break;
1488 default:
1489 return -EINVAL;
1490 }
1491
1492 *config = pinconf_to_config_packed(param, arg);
1493
1494 return 0;
1495}
1496
1497static int tegra_io_pad_pinconf_set(struct pinctrl_dev *pctl_dev,
1498 unsigned int pin, unsigned long *configs,
1499 unsigned int num_configs)
1500{
1501 const struct tegra_io_pad_soc *pad = tegra_io_pad_find(pmc, pin);
1502 enum pin_config_param param;
1503 unsigned int i;
1504 int err;
1505 u32 arg;
1506
1507 if (!pad)
1508 return -EINVAL;
1509
1510 for (i = 0; i < num_configs; ++i) {
1511 param = pinconf_to_config_param(configs[i]);
1512 arg = pinconf_to_config_argument(configs[i]);
1513
1514 switch (param) {
1515 case PIN_CONFIG_LOW_POWER_MODE:
1516 if (arg)
1517 err = tegra_io_pad_power_disable(pad->id);
1518 else
1519 err = tegra_io_pad_power_enable(pad->id);
1520 if (err)
1521 return err;
1522 break;
1523 case PIN_CONFIG_POWER_SOURCE:
1524 if (arg != TEGRA_IO_PAD_VOLTAGE_1V8 &&
1525 arg != TEGRA_IO_PAD_VOLTAGE_3V3)
1526 return -EINVAL;
1527 err = tegra_io_pad_set_voltage(pad->id, arg);
1528 if (err)
1529 return err;
1530 break;
1531 default:
1532 return -EINVAL;
1533 }
1534 }
1535
1536 return 0;
1537}
1538
1539static const struct pinconf_ops tegra_io_pad_pinconf_ops = {
1540 .pin_config_get = tegra_io_pad_pinconf_get,
1541 .pin_config_set = tegra_io_pad_pinconf_set,
1542 .is_generic = true,
1543};
1544
1545static struct pinctrl_desc tegra_pmc_pctl_desc = {
1546 .pctlops = &tegra_io_pad_pinctrl_ops,
1547 .confops = &tegra_io_pad_pinconf_ops,
1548};
1549
1550static int tegra_pmc_pinctrl_init(struct tegra_pmc *pmc)
1551{
1552 int err = 0;
1553
1554 if (!pmc->soc->num_pin_descs)
1555 return 0;
1556
1557 tegra_pmc_pctl_desc.name = dev_name(pmc->dev);
1558 tegra_pmc_pctl_desc.pins = pmc->soc->pin_descs;
1559 tegra_pmc_pctl_desc.npins = pmc->soc->num_pin_descs;
1560
1561 pmc->pctl_dev = devm_pinctrl_register(pmc->dev, &tegra_pmc_pctl_desc,
1562 pmc);
1563 if (IS_ERR(pmc->pctl_dev)) {
1564 err = PTR_ERR(pmc->pctl_dev);
1565 dev_err(pmc->dev, "unable to register pinctrl, %d\n", err);
1566 }
1567
1568 return err;
1569}
1570
Sandipan Patra5f84bb12018-10-24 12:38:00 +05301571static ssize_t reset_reason_show(struct device *dev,
1572 struct device_attribute *attr, char *buf)
1573{
1574 u32 value, rst_src;
1575
1576 value = tegra_pmc_readl(pmc->soc->regs->rst_status);
1577 rst_src = (value & pmc->soc->regs->rst_source_mask) >>
1578 pmc->soc->regs->rst_source_shift;
1579
1580 return sprintf(buf, "%s\n", pmc->soc->reset_sources[rst_src]);
1581}
1582
1583static DEVICE_ATTR_RO(reset_reason);
1584
1585static ssize_t reset_level_show(struct device *dev,
1586 struct device_attribute *attr, char *buf)
1587{
1588 u32 value, rst_lvl;
1589
1590 value = tegra_pmc_readl(pmc->soc->regs->rst_status);
1591 rst_lvl = (value & pmc->soc->regs->rst_level_mask) >>
1592 pmc->soc->regs->rst_level_shift;
1593
1594 return sprintf(buf, "%s\n", pmc->soc->reset_levels[rst_lvl]);
1595}
1596
1597static DEVICE_ATTR_RO(reset_level);
1598
1599static void tegra_pmc_reset_sysfs_init(struct tegra_pmc *pmc)
1600{
1601 struct device *dev = pmc->dev;
1602 int err = 0;
1603
1604 if (pmc->soc->reset_sources) {
1605 err = device_create_file(dev, &dev_attr_reset_reason);
1606 if (err < 0)
1607 dev_warn(dev,
1608 "failed to create attr \"reset_reason\": %d\n",
1609 err);
1610 }
1611
1612 if (pmc->soc->reset_levels) {
1613 err = device_create_file(dev, &dev_attr_reset_level);
1614 if (err < 0)
1615 dev_warn(dev,
1616 "failed to create attr \"reset_level\": %d\n",
1617 err);
1618 }
1619}
1620
Thierry Reding72323982014-07-11 13:19:06 +02001621static int tegra_pmc_probe(struct platform_device *pdev)
1622{
Jon Huntere8cf6612016-02-11 18:03:21 +00001623 void __iomem *base;
Thierry Reding72323982014-07-11 13:19:06 +02001624 struct resource *res;
1625 int err;
1626
Jon Huntera83f1fc2016-06-28 11:38:28 +01001627 /*
1628 * Early initialisation should have configured an initial
1629 * register mapping and setup the soc data pointer. If these
1630 * are not valid then something went badly wrong!
1631 */
1632 if (WARN_ON(!pmc->base || !pmc->soc))
1633 return -ENODEV;
1634
Thierry Reding72323982014-07-11 13:19:06 +02001635 err = tegra_pmc_parse_dt(pmc, pdev->dev.of_node);
1636 if (err < 0)
1637 return err;
1638
1639 /* take over the memory region from the early initialization */
1640 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Jon Hunter0259f522016-02-11 18:03:20 +00001641 base = devm_ioremap_resource(&pdev->dev, res);
1642 if (IS_ERR(base))
1643 return PTR_ERR(base);
Thierry Reding72323982014-07-11 13:19:06 +02001644
Thierry Redingc641ec62017-08-30 12:42:34 +02001645 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "wake");
1646 if (res) {
1647 pmc->wake = devm_ioremap_resource(&pdev->dev, res);
1648 if (IS_ERR(pmc->wake))
1649 return PTR_ERR(pmc->wake);
1650 } else {
1651 pmc->wake = base;
1652 }
1653
1654 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "aotag");
1655 if (res) {
1656 pmc->aotag = devm_ioremap_resource(&pdev->dev, res);
1657 if (IS_ERR(pmc->aotag))
1658 return PTR_ERR(pmc->aotag);
1659 } else {
1660 pmc->aotag = base;
1661 }
1662
1663 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "scratch");
1664 if (res) {
1665 pmc->scratch = devm_ioremap_resource(&pdev->dev, res);
1666 if (IS_ERR(pmc->scratch))
1667 return PTR_ERR(pmc->scratch);
1668 } else {
1669 pmc->scratch = base;
1670 }
Thierry Reding5be22552017-08-30 12:32:58 +02001671
Thierry Reding72323982014-07-11 13:19:06 +02001672 pmc->clk = devm_clk_get(&pdev->dev, "pclk");
1673 if (IS_ERR(pmc->clk)) {
1674 err = PTR_ERR(pmc->clk);
Thierry Reding5be22552017-08-30 12:32:58 +02001675
1676 if (err != -ENOENT) {
1677 dev_err(&pdev->dev, "failed to get pclk: %d\n", err);
1678 return err;
1679 }
1680
1681 pmc->clk = NULL;
Thierry Reding72323982014-07-11 13:19:06 +02001682 }
1683
Mikko Perttunen3568df32015-01-06 12:52:58 +02001684 pmc->dev = &pdev->dev;
1685
Thierry Reding72323982014-07-11 13:19:06 +02001686 tegra_pmc_init(pmc);
1687
Mikko Perttunen3568df32015-01-06 12:52:58 +02001688 tegra_pmc_init_tsense_reset(pmc);
1689
Sandipan Patra5f84bb12018-10-24 12:38:00 +05301690 tegra_pmc_reset_sysfs_init(pmc);
1691
Thierry Reding72323982014-07-11 13:19:06 +02001692 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
1693 err = tegra_powergate_debugfs_init();
1694 if (err < 0)
1695 return err;
1696 }
1697
David Riley78921582015-03-18 10:52:25 +01001698 err = register_restart_handler(&tegra_pmc_restart_handler);
1699 if (err) {
1700 dev_err(&pdev->dev, "unable to register restart handler, %d\n",
1701 err);
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001702 goto cleanup_debugfs;
David Riley78921582015-03-18 10:52:25 +01001703 }
1704
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001705 err = tegra_pmc_pinctrl_init(pmc);
1706 if (err)
1707 goto cleanup_restart_handler;
1708
Jon Huntere8cf6612016-02-11 18:03:21 +00001709 mutex_lock(&pmc->powergates_lock);
1710 iounmap(pmc->base);
Jon Hunter0259f522016-02-11 18:03:20 +00001711 pmc->base = base;
Jon Huntere8cf6612016-02-11 18:03:21 +00001712 mutex_unlock(&pmc->powergates_lock);
Jon Hunter0259f522016-02-11 18:03:20 +00001713
Thierry Reding72323982014-07-11 13:19:06 +02001714 return 0;
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001715
1716cleanup_restart_handler:
1717 unregister_restart_handler(&tegra_pmc_restart_handler);
1718cleanup_debugfs:
1719 debugfs_remove(pmc->debugfs);
1720 return err;
Thierry Reding72323982014-07-11 13:19:06 +02001721}
1722
Paul Walmsley2b20b612014-12-09 22:36:50 +00001723#if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM)
Thierry Reding72323982014-07-11 13:19:06 +02001724static int tegra_pmc_suspend(struct device *dev)
1725{
1726 tegra_pmc_writel(virt_to_phys(tegra_resume), PMC_SCRATCH41);
1727
1728 return 0;
1729}
1730
1731static int tegra_pmc_resume(struct device *dev)
1732{
1733 tegra_pmc_writel(0x0, PMC_SCRATCH41);
1734
1735 return 0;
1736}
Thierry Reding72323982014-07-11 13:19:06 +02001737
1738static SIMPLE_DEV_PM_OPS(tegra_pmc_pm_ops, tegra_pmc_suspend, tegra_pmc_resume);
1739
Paul Walmsley2b20b612014-12-09 22:36:50 +00001740#endif
1741
Thierry Reding72323982014-07-11 13:19:06 +02001742static const char * const tegra20_powergates[] = {
1743 [TEGRA_POWERGATE_CPU] = "cpu",
1744 [TEGRA_POWERGATE_3D] = "3d",
1745 [TEGRA_POWERGATE_VENC] = "venc",
1746 [TEGRA_POWERGATE_VDEC] = "vdec",
1747 [TEGRA_POWERGATE_PCIE] = "pcie",
1748 [TEGRA_POWERGATE_L2] = "l2",
1749 [TEGRA_POWERGATE_MPE] = "mpe",
1750};
1751
Thierry Reding5be22552017-08-30 12:32:58 +02001752static const struct tegra_pmc_regs tegra20_pmc_regs = {
1753 .scratch0 = 0x50,
1754 .dpd_req = 0x1b8,
1755 .dpd_status = 0x1bc,
1756 .dpd2_req = 0x1c0,
1757 .dpd2_status = 0x1c4,
Sandipan Patra5f84bb12018-10-24 12:38:00 +05301758 .rst_status = 0x1b4,
1759 .rst_source_shift = 0x0,
1760 .rst_source_mask = 0x7,
1761 .rst_level_shift = 0x0,
1762 .rst_level_mask = 0x0,
Thierry Reding5be22552017-08-30 12:32:58 +02001763};
1764
1765static void tegra20_pmc_init(struct tegra_pmc *pmc)
1766{
1767 u32 value;
1768
1769 /* Always enable CPU power request */
1770 value = tegra_pmc_readl(PMC_CNTRL);
1771 value |= PMC_CNTRL_CPU_PWRREQ_OE;
1772 tegra_pmc_writel(value, PMC_CNTRL);
1773
1774 value = tegra_pmc_readl(PMC_CNTRL);
1775
1776 if (pmc->sysclkreq_high)
1777 value &= ~PMC_CNTRL_SYSCLK_POLARITY;
1778 else
1779 value |= PMC_CNTRL_SYSCLK_POLARITY;
1780
1781 /* configure the output polarity while the request is tristated */
1782 tegra_pmc_writel(value, PMC_CNTRL);
1783
1784 /* now enable the request */
1785 value = tegra_pmc_readl(PMC_CNTRL);
1786 value |= PMC_CNTRL_SYSCLK_OE;
1787 tegra_pmc_writel(value, PMC_CNTRL);
1788}
1789
1790static void tegra20_pmc_setup_irq_polarity(struct tegra_pmc *pmc,
1791 struct device_node *np,
1792 bool invert)
1793{
1794 u32 value;
1795
1796 value = tegra_pmc_readl(PMC_CNTRL);
1797
1798 if (invert)
1799 value |= PMC_CNTRL_INTR_POLARITY;
1800 else
1801 value &= ~PMC_CNTRL_INTR_POLARITY;
1802
1803 tegra_pmc_writel(value, PMC_CNTRL);
1804}
1805
Thierry Reding72323982014-07-11 13:19:06 +02001806static const struct tegra_pmc_soc tegra20_pmc_soc = {
1807 .num_powergates = ARRAY_SIZE(tegra20_powergates),
1808 .powergates = tegra20_powergates,
1809 .num_cpu_powergates = 0,
1810 .cpu_powergates = NULL,
Mikko Perttunen3568df32015-01-06 12:52:58 +02001811 .has_tsense_reset = false,
Thierry Redinga9a40a42015-01-09 11:15:33 +01001812 .has_gpu_clamps = false,
Thierry Reding5be22552017-08-30 12:32:58 +02001813 .num_io_pads = 0,
1814 .io_pads = NULL,
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001815 .num_pin_descs = 0,
1816 .pin_descs = NULL,
Thierry Reding5be22552017-08-30 12:32:58 +02001817 .regs = &tegra20_pmc_regs,
1818 .init = tegra20_pmc_init,
1819 .setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
Sandipan Patra5f84bb12018-10-24 12:38:00 +05301820 .reset_sources = NULL,
1821 .num_reset_sources = 0,
1822 .reset_levels = NULL,
1823 .num_reset_levels = 0,
Thierry Reding72323982014-07-11 13:19:06 +02001824};
1825
1826static const char * const tegra30_powergates[] = {
1827 [TEGRA_POWERGATE_CPU] = "cpu0",
1828 [TEGRA_POWERGATE_3D] = "3d0",
1829 [TEGRA_POWERGATE_VENC] = "venc",
1830 [TEGRA_POWERGATE_VDEC] = "vdec",
1831 [TEGRA_POWERGATE_PCIE] = "pcie",
1832 [TEGRA_POWERGATE_L2] = "l2",
1833 [TEGRA_POWERGATE_MPE] = "mpe",
1834 [TEGRA_POWERGATE_HEG] = "heg",
1835 [TEGRA_POWERGATE_SATA] = "sata",
1836 [TEGRA_POWERGATE_CPU1] = "cpu1",
1837 [TEGRA_POWERGATE_CPU2] = "cpu2",
1838 [TEGRA_POWERGATE_CPU3] = "cpu3",
1839 [TEGRA_POWERGATE_CELP] = "celp",
1840 [TEGRA_POWERGATE_3D1] = "3d1",
1841};
1842
1843static const u8 tegra30_cpu_powergates[] = {
1844 TEGRA_POWERGATE_CPU,
1845 TEGRA_POWERGATE_CPU1,
1846 TEGRA_POWERGATE_CPU2,
1847 TEGRA_POWERGATE_CPU3,
1848};
1849
1850static const struct tegra_pmc_soc tegra30_pmc_soc = {
1851 .num_powergates = ARRAY_SIZE(tegra30_powergates),
1852 .powergates = tegra30_powergates,
1853 .num_cpu_powergates = ARRAY_SIZE(tegra30_cpu_powergates),
1854 .cpu_powergates = tegra30_cpu_powergates,
Mikko Perttunen3568df32015-01-06 12:52:58 +02001855 .has_tsense_reset = true,
Thierry Redinga9a40a42015-01-09 11:15:33 +01001856 .has_gpu_clamps = false,
Aapo Vienamo13136a42018-08-10 21:08:07 +03001857 .has_impl_33v_pwr = false,
Thierry Reding5be22552017-08-30 12:32:58 +02001858 .num_io_pads = 0,
1859 .io_pads = NULL,
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001860 .num_pin_descs = 0,
1861 .pin_descs = NULL,
Thierry Reding5be22552017-08-30 12:32:58 +02001862 .regs = &tegra20_pmc_regs,
1863 .init = tegra20_pmc_init,
1864 .setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
Sandipan Patra5f84bb12018-10-24 12:38:00 +05301865 .reset_sources = tegra30_reset_sources,
1866 .num_reset_sources = 5,
1867 .reset_levels = NULL,
1868 .num_reset_levels = 0,
Thierry Reding72323982014-07-11 13:19:06 +02001869};
1870
1871static const char * const tegra114_powergates[] = {
1872 [TEGRA_POWERGATE_CPU] = "crail",
1873 [TEGRA_POWERGATE_3D] = "3d",
1874 [TEGRA_POWERGATE_VENC] = "venc",
1875 [TEGRA_POWERGATE_VDEC] = "vdec",
1876 [TEGRA_POWERGATE_MPE] = "mpe",
1877 [TEGRA_POWERGATE_HEG] = "heg",
1878 [TEGRA_POWERGATE_CPU1] = "cpu1",
1879 [TEGRA_POWERGATE_CPU2] = "cpu2",
1880 [TEGRA_POWERGATE_CPU3] = "cpu3",
1881 [TEGRA_POWERGATE_CELP] = "celp",
1882 [TEGRA_POWERGATE_CPU0] = "cpu0",
1883 [TEGRA_POWERGATE_C0NC] = "c0nc",
1884 [TEGRA_POWERGATE_C1NC] = "c1nc",
1885 [TEGRA_POWERGATE_DIS] = "dis",
1886 [TEGRA_POWERGATE_DISB] = "disb",
1887 [TEGRA_POWERGATE_XUSBA] = "xusba",
1888 [TEGRA_POWERGATE_XUSBB] = "xusbb",
1889 [TEGRA_POWERGATE_XUSBC] = "xusbc",
1890};
1891
1892static const u8 tegra114_cpu_powergates[] = {
1893 TEGRA_POWERGATE_CPU0,
1894 TEGRA_POWERGATE_CPU1,
1895 TEGRA_POWERGATE_CPU2,
1896 TEGRA_POWERGATE_CPU3,
1897};
1898
1899static const struct tegra_pmc_soc tegra114_pmc_soc = {
1900 .num_powergates = ARRAY_SIZE(tegra114_powergates),
1901 .powergates = tegra114_powergates,
1902 .num_cpu_powergates = ARRAY_SIZE(tegra114_cpu_powergates),
1903 .cpu_powergates = tegra114_cpu_powergates,
Mikko Perttunen3568df32015-01-06 12:52:58 +02001904 .has_tsense_reset = true,
Thierry Redinga9a40a42015-01-09 11:15:33 +01001905 .has_gpu_clamps = false,
Aapo Vienamo13136a42018-08-10 21:08:07 +03001906 .has_impl_33v_pwr = false,
Thierry Reding5be22552017-08-30 12:32:58 +02001907 .num_io_pads = 0,
1908 .io_pads = NULL,
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001909 .num_pin_descs = 0,
1910 .pin_descs = NULL,
Thierry Reding5be22552017-08-30 12:32:58 +02001911 .regs = &tegra20_pmc_regs,
1912 .init = tegra20_pmc_init,
1913 .setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
Sandipan Patra5f84bb12018-10-24 12:38:00 +05301914 .reset_sources = tegra30_reset_sources,
1915 .num_reset_sources = 5,
1916 .reset_levels = NULL,
1917 .num_reset_levels = 0,
Thierry Reding72323982014-07-11 13:19:06 +02001918};
1919
1920static const char * const tegra124_powergates[] = {
1921 [TEGRA_POWERGATE_CPU] = "crail",
1922 [TEGRA_POWERGATE_3D] = "3d",
1923 [TEGRA_POWERGATE_VENC] = "venc",
1924 [TEGRA_POWERGATE_PCIE] = "pcie",
1925 [TEGRA_POWERGATE_VDEC] = "vdec",
Thierry Reding72323982014-07-11 13:19:06 +02001926 [TEGRA_POWERGATE_MPE] = "mpe",
1927 [TEGRA_POWERGATE_HEG] = "heg",
1928 [TEGRA_POWERGATE_SATA] = "sata",
1929 [TEGRA_POWERGATE_CPU1] = "cpu1",
1930 [TEGRA_POWERGATE_CPU2] = "cpu2",
1931 [TEGRA_POWERGATE_CPU3] = "cpu3",
1932 [TEGRA_POWERGATE_CELP] = "celp",
1933 [TEGRA_POWERGATE_CPU0] = "cpu0",
1934 [TEGRA_POWERGATE_C0NC] = "c0nc",
1935 [TEGRA_POWERGATE_C1NC] = "c1nc",
1936 [TEGRA_POWERGATE_SOR] = "sor",
1937 [TEGRA_POWERGATE_DIS] = "dis",
1938 [TEGRA_POWERGATE_DISB] = "disb",
1939 [TEGRA_POWERGATE_XUSBA] = "xusba",
1940 [TEGRA_POWERGATE_XUSBB] = "xusbb",
1941 [TEGRA_POWERGATE_XUSBC] = "xusbc",
1942 [TEGRA_POWERGATE_VIC] = "vic",
1943 [TEGRA_POWERGATE_IRAM] = "iram",
1944};
1945
1946static const u8 tegra124_cpu_powergates[] = {
1947 TEGRA_POWERGATE_CPU0,
1948 TEGRA_POWERGATE_CPU1,
1949 TEGRA_POWERGATE_CPU2,
1950 TEGRA_POWERGATE_CPU3,
1951};
1952
Aapo Vienamo437c4f22018-08-10 21:08:10 +03001953#define TEGRA_IO_PAD(_id, _dpd, _voltage, _name) \
1954 ((struct tegra_io_pad_soc) { \
1955 .id = (_id), \
1956 .dpd = (_dpd), \
1957 .voltage = (_voltage), \
1958 .name = (_name), \
1959 })
1960
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001961#define TEGRA_IO_PIN_DESC(_id, _dpd, _voltage, _name) \
1962 ((struct pinctrl_pin_desc) { \
1963 .number = (_id), \
1964 .name = (_name) \
1965 })
1966
Aapo Vienamo437c4f22018-08-10 21:08:10 +03001967#define TEGRA124_IO_PAD_TABLE(_pad) \
1968 /* .id .dpd .voltage .name */ \
1969 _pad(TEGRA_IO_PAD_AUDIO, 17, UINT_MAX, "audio"), \
1970 _pad(TEGRA_IO_PAD_BB, 15, UINT_MAX, "bb"), \
1971 _pad(TEGRA_IO_PAD_CAM, 36, UINT_MAX, "cam"), \
1972 _pad(TEGRA_IO_PAD_COMP, 22, UINT_MAX, "comp"), \
1973 _pad(TEGRA_IO_PAD_CSIA, 0, UINT_MAX, "csia"), \
1974 _pad(TEGRA_IO_PAD_CSIB, 1, UINT_MAX, "csb"), \
1975 _pad(TEGRA_IO_PAD_CSIE, 44, UINT_MAX, "cse"), \
1976 _pad(TEGRA_IO_PAD_DSI, 2, UINT_MAX, "dsi"), \
1977 _pad(TEGRA_IO_PAD_DSIB, 39, UINT_MAX, "dsib"), \
1978 _pad(TEGRA_IO_PAD_DSIC, 40, UINT_MAX, "dsic"), \
1979 _pad(TEGRA_IO_PAD_DSID, 41, UINT_MAX, "dsid"), \
1980 _pad(TEGRA_IO_PAD_HDMI, 28, UINT_MAX, "hdmi"), \
1981 _pad(TEGRA_IO_PAD_HSIC, 19, UINT_MAX, "hsic"), \
1982 _pad(TEGRA_IO_PAD_HV, 38, UINT_MAX, "hv"), \
1983 _pad(TEGRA_IO_PAD_LVDS, 57, UINT_MAX, "lvds"), \
1984 _pad(TEGRA_IO_PAD_MIPI_BIAS, 3, UINT_MAX, "mipi-bias"), \
1985 _pad(TEGRA_IO_PAD_NAND, 13, UINT_MAX, "nand"), \
1986 _pad(TEGRA_IO_PAD_PEX_BIAS, 4, UINT_MAX, "pex-bias"), \
1987 _pad(TEGRA_IO_PAD_PEX_CLK1, 5, UINT_MAX, "pex-clk1"), \
1988 _pad(TEGRA_IO_PAD_PEX_CLK2, 6, UINT_MAX, "pex-clk2"), \
1989 _pad(TEGRA_IO_PAD_PEX_CNTRL, 32, UINT_MAX, "pex-cntrl"), \
1990 _pad(TEGRA_IO_PAD_SDMMC1, 33, UINT_MAX, "sdmmc1"), \
1991 _pad(TEGRA_IO_PAD_SDMMC3, 34, UINT_MAX, "sdmmc3"), \
1992 _pad(TEGRA_IO_PAD_SDMMC4, 35, UINT_MAX, "sdmmc4"), \
1993 _pad(TEGRA_IO_PAD_SYS_DDC, 58, UINT_MAX, "sys_ddc"), \
1994 _pad(TEGRA_IO_PAD_UART, 14, UINT_MAX, "uart"), \
1995 _pad(TEGRA_IO_PAD_USB0, 9, UINT_MAX, "usb0"), \
1996 _pad(TEGRA_IO_PAD_USB1, 10, UINT_MAX, "usb1"), \
1997 _pad(TEGRA_IO_PAD_USB2, 11, UINT_MAX, "usb2"), \
1998 _pad(TEGRA_IO_PAD_USB_BIAS, 12, UINT_MAX, "usb_bias")
1999
Laxman Dewangan21b49912016-10-10 15:14:34 +02002000static const struct tegra_io_pad_soc tegra124_io_pads[] = {
Aapo Vienamo437c4f22018-08-10 21:08:10 +03002001 TEGRA124_IO_PAD_TABLE(TEGRA_IO_PAD)
Laxman Dewangan21b49912016-10-10 15:14:34 +02002002};
2003
Aapo Vienamo4a37f112018-08-10 21:08:12 +03002004static const struct pinctrl_pin_desc tegra124_pin_descs[] = {
2005 TEGRA124_IO_PAD_TABLE(TEGRA_IO_PIN_DESC)
Thierry Reding72323982014-07-11 13:19:06 +02002006};
2007
2008static const struct tegra_pmc_soc tegra124_pmc_soc = {
2009 .num_powergates = ARRAY_SIZE(tegra124_powergates),
2010 .powergates = tegra124_powergates,
2011 .num_cpu_powergates = ARRAY_SIZE(tegra124_cpu_powergates),
2012 .cpu_powergates = tegra124_cpu_powergates,
Mikko Perttunen3568df32015-01-06 12:52:58 +02002013 .has_tsense_reset = true,
Thierry Redinga9a40a42015-01-09 11:15:33 +01002014 .has_gpu_clamps = true,
Aapo Vienamo13136a42018-08-10 21:08:07 +03002015 .has_impl_33v_pwr = false,
Laxman Dewangan21b49912016-10-10 15:14:34 +02002016 .num_io_pads = ARRAY_SIZE(tegra124_io_pads),
2017 .io_pads = tegra124_io_pads,
Aapo Vienamo4a37f112018-08-10 21:08:12 +03002018 .num_pin_descs = ARRAY_SIZE(tegra124_pin_descs),
2019 .pin_descs = tegra124_pin_descs,
Thierry Reding5be22552017-08-30 12:32:58 +02002020 .regs = &tegra20_pmc_regs,
2021 .init = tegra20_pmc_init,
2022 .setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
Sandipan Patra5f84bb12018-10-24 12:38:00 +05302023 .reset_sources = tegra30_reset_sources,
2024 .num_reset_sources = 5,
2025 .reset_levels = NULL,
2026 .num_reset_levels = 0,
Thierry Reding72323982014-07-11 13:19:06 +02002027};
2028
Thierry Redingc2fe4692015-03-23 11:31:29 +01002029static const char * const tegra210_powergates[] = {
2030 [TEGRA_POWERGATE_CPU] = "crail",
2031 [TEGRA_POWERGATE_3D] = "3d",
2032 [TEGRA_POWERGATE_VENC] = "venc",
2033 [TEGRA_POWERGATE_PCIE] = "pcie",
Thierry Redingc2fe4692015-03-23 11:31:29 +01002034 [TEGRA_POWERGATE_MPE] = "mpe",
Thierry Redingc2fe4692015-03-23 11:31:29 +01002035 [TEGRA_POWERGATE_SATA] = "sata",
2036 [TEGRA_POWERGATE_CPU1] = "cpu1",
2037 [TEGRA_POWERGATE_CPU2] = "cpu2",
2038 [TEGRA_POWERGATE_CPU3] = "cpu3",
Thierry Redingc2fe4692015-03-23 11:31:29 +01002039 [TEGRA_POWERGATE_CPU0] = "cpu0",
2040 [TEGRA_POWERGATE_C0NC] = "c0nc",
Thierry Redingc2fe4692015-03-23 11:31:29 +01002041 [TEGRA_POWERGATE_SOR] = "sor",
2042 [TEGRA_POWERGATE_DIS] = "dis",
2043 [TEGRA_POWERGATE_DISB] = "disb",
2044 [TEGRA_POWERGATE_XUSBA] = "xusba",
2045 [TEGRA_POWERGATE_XUSBB] = "xusbb",
2046 [TEGRA_POWERGATE_XUSBC] = "xusbc",
2047 [TEGRA_POWERGATE_VIC] = "vic",
2048 [TEGRA_POWERGATE_IRAM] = "iram",
2049 [TEGRA_POWERGATE_NVDEC] = "nvdec",
2050 [TEGRA_POWERGATE_NVJPG] = "nvjpg",
2051 [TEGRA_POWERGATE_AUD] = "aud",
2052 [TEGRA_POWERGATE_DFD] = "dfd",
2053 [TEGRA_POWERGATE_VE2] = "ve2",
2054};
2055
2056static const u8 tegra210_cpu_powergates[] = {
2057 TEGRA_POWERGATE_CPU0,
2058 TEGRA_POWERGATE_CPU1,
2059 TEGRA_POWERGATE_CPU2,
2060 TEGRA_POWERGATE_CPU3,
2061};
2062
Aapo Vienamo437c4f22018-08-10 21:08:10 +03002063#define TEGRA210_IO_PAD_TABLE(_pad) \
2064 /* .id .dpd .voltage .name */ \
2065 _pad(TEGRA_IO_PAD_AUDIO, 17, 5, "audio"), \
2066 _pad(TEGRA_IO_PAD_AUDIO_HV, 61, 18, "audio-hv"), \
2067 _pad(TEGRA_IO_PAD_CAM, 36, 10, "cam"), \
2068 _pad(TEGRA_IO_PAD_CSIA, 0, UINT_MAX, "csia"), \
2069 _pad(TEGRA_IO_PAD_CSIB, 1, UINT_MAX, "csib"), \
2070 _pad(TEGRA_IO_PAD_CSIC, 42, UINT_MAX, "csic"), \
2071 _pad(TEGRA_IO_PAD_CSID, 43, UINT_MAX, "csid"), \
2072 _pad(TEGRA_IO_PAD_CSIE, 44, UINT_MAX, "csie"), \
2073 _pad(TEGRA_IO_PAD_CSIF, 45, UINT_MAX, "csif"), \
2074 _pad(TEGRA_IO_PAD_DBG, 25, 19, "dbg"), \
2075 _pad(TEGRA_IO_PAD_DEBUG_NONAO, 26, UINT_MAX, "debug-nonao"), \
2076 _pad(TEGRA_IO_PAD_DMIC, 50, 20, "dmic"), \
2077 _pad(TEGRA_IO_PAD_DP, 51, UINT_MAX, "dp"), \
2078 _pad(TEGRA_IO_PAD_DSI, 2, UINT_MAX, "dsi"), \
2079 _pad(TEGRA_IO_PAD_DSIB, 39, UINT_MAX, "dsib"), \
2080 _pad(TEGRA_IO_PAD_DSIC, 40, UINT_MAX, "dsic"), \
2081 _pad(TEGRA_IO_PAD_DSID, 41, UINT_MAX, "dsid"), \
2082 _pad(TEGRA_IO_PAD_EMMC, 35, UINT_MAX, "emmc"), \
2083 _pad(TEGRA_IO_PAD_EMMC2, 37, UINT_MAX, "emmc2"), \
2084 _pad(TEGRA_IO_PAD_GPIO, 27, 21, "gpio"), \
2085 _pad(TEGRA_IO_PAD_HDMI, 28, UINT_MAX, "hdmi"), \
2086 _pad(TEGRA_IO_PAD_HSIC, 19, UINT_MAX, "hsic"), \
2087 _pad(TEGRA_IO_PAD_LVDS, 57, UINT_MAX, "lvds"), \
2088 _pad(TEGRA_IO_PAD_MIPI_BIAS, 3, UINT_MAX, "mipi-bias"), \
2089 _pad(TEGRA_IO_PAD_PEX_BIAS, 4, UINT_MAX, "pex-bias"), \
2090 _pad(TEGRA_IO_PAD_PEX_CLK1, 5, UINT_MAX, "pex-clk1"), \
2091 _pad(TEGRA_IO_PAD_PEX_CLK2, 6, UINT_MAX, "pex-clk2"), \
2092 _pad(TEGRA_IO_PAD_PEX_CNTRL, UINT_MAX, 11, "pex-cntrl"), \
2093 _pad(TEGRA_IO_PAD_SDMMC1, 33, 12, "sdmmc1"), \
2094 _pad(TEGRA_IO_PAD_SDMMC3, 34, 13, "sdmmc3"), \
2095 _pad(TEGRA_IO_PAD_SPI, 46, 22, "spi"), \
2096 _pad(TEGRA_IO_PAD_SPI_HV, 47, 23, "spi-hv"), \
2097 _pad(TEGRA_IO_PAD_UART, 14, 2, "uart"), \
2098 _pad(TEGRA_IO_PAD_USB0, 9, UINT_MAX, "usb0"), \
2099 _pad(TEGRA_IO_PAD_USB1, 10, UINT_MAX, "usb1"), \
2100 _pad(TEGRA_IO_PAD_USB2, 11, UINT_MAX, "usb2"), \
2101 _pad(TEGRA_IO_PAD_USB3, 18, UINT_MAX, "usb3"), \
2102 _pad(TEGRA_IO_PAD_USB_BIAS, 12, UINT_MAX, "usb-bias")
2103
Laxman Dewangan21b49912016-10-10 15:14:34 +02002104static const struct tegra_io_pad_soc tegra210_io_pads[] = {
Aapo Vienamo437c4f22018-08-10 21:08:10 +03002105 TEGRA210_IO_PAD_TABLE(TEGRA_IO_PAD)
Laxman Dewangan21b49912016-10-10 15:14:34 +02002106};
2107
Aapo Vienamo4a37f112018-08-10 21:08:12 +03002108static const struct pinctrl_pin_desc tegra210_pin_descs[] = {
2109 TEGRA210_IO_PAD_TABLE(TEGRA_IO_PIN_DESC)
Thierry Redingc2fe4692015-03-23 11:31:29 +01002110};
2111
2112static const struct tegra_pmc_soc tegra210_pmc_soc = {
2113 .num_powergates = ARRAY_SIZE(tegra210_powergates),
2114 .powergates = tegra210_powergates,
2115 .num_cpu_powergates = ARRAY_SIZE(tegra210_cpu_powergates),
2116 .cpu_powergates = tegra210_cpu_powergates,
2117 .has_tsense_reset = true,
2118 .has_gpu_clamps = true,
Aapo Vienamo13136a42018-08-10 21:08:07 +03002119 .has_impl_33v_pwr = false,
Peter De Schrijvera263394a2018-01-25 16:00:13 +02002120 .needs_mbist_war = true,
Laxman Dewangan21b49912016-10-10 15:14:34 +02002121 .num_io_pads = ARRAY_SIZE(tegra210_io_pads),
2122 .io_pads = tegra210_io_pads,
Aapo Vienamo4a37f112018-08-10 21:08:12 +03002123 .num_pin_descs = ARRAY_SIZE(tegra210_pin_descs),
2124 .pin_descs = tegra210_pin_descs,
Thierry Reding5be22552017-08-30 12:32:58 +02002125 .regs = &tegra20_pmc_regs,
2126 .init = tegra20_pmc_init,
2127 .setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
Sandipan Patra5f84bb12018-10-24 12:38:00 +05302128 .reset_sources = tegra30_reset_sources,
2129 .num_reset_sources = 5,
2130 .reset_levels = NULL,
2131 .num_reset_levels = 0,
Thierry Redingc2fe4692015-03-23 11:31:29 +01002132};
2133
Aapo Vienamo437c4f22018-08-10 21:08:10 +03002134#define TEGRA186_IO_PAD_TABLE(_pad) \
2135 /* .id .dpd .voltage .name */ \
2136 _pad(TEGRA_IO_PAD_CSIA, 0, UINT_MAX, "csia"), \
2137 _pad(TEGRA_IO_PAD_CSIB, 1, UINT_MAX, "csib"), \
2138 _pad(TEGRA_IO_PAD_DSI, 2, UINT_MAX, "dsi"), \
2139 _pad(TEGRA_IO_PAD_MIPI_BIAS, 3, UINT_MAX, "mipi-bias"), \
2140 _pad(TEGRA_IO_PAD_PEX_CLK_BIAS, 4, UINT_MAX, "pex-clk-bias"), \
2141 _pad(TEGRA_IO_PAD_PEX_CLK3, 5, UINT_MAX, "pex-clk3"), \
2142 _pad(TEGRA_IO_PAD_PEX_CLK2, 6, UINT_MAX, "pex-clk2"), \
2143 _pad(TEGRA_IO_PAD_PEX_CLK1, 7, UINT_MAX, "pex-clk1"), \
2144 _pad(TEGRA_IO_PAD_USB0, 9, UINT_MAX, "usb0"), \
2145 _pad(TEGRA_IO_PAD_USB1, 10, UINT_MAX, "usb1"), \
2146 _pad(TEGRA_IO_PAD_USB2, 11, UINT_MAX, "usb2"), \
2147 _pad(TEGRA_IO_PAD_USB_BIAS, 12, UINT_MAX, "usb-bias"), \
2148 _pad(TEGRA_IO_PAD_UART, 14, UINT_MAX, "uart"), \
2149 _pad(TEGRA_IO_PAD_AUDIO, 17, UINT_MAX, "audio"), \
2150 _pad(TEGRA_IO_PAD_HSIC, 19, UINT_MAX, "hsic"), \
2151 _pad(TEGRA_IO_PAD_DBG, 25, UINT_MAX, "dbg"), \
2152 _pad(TEGRA_IO_PAD_HDMI_DP0, 28, UINT_MAX, "hdmi-dp0"), \
2153 _pad(TEGRA_IO_PAD_HDMI_DP1, 29, UINT_MAX, "hdmi-dp1"), \
2154 _pad(TEGRA_IO_PAD_PEX_CNTRL, 32, UINT_MAX, "pex-cntrl"), \
2155 _pad(TEGRA_IO_PAD_SDMMC2_HV, 34, 5, "sdmmc2-hv"), \
2156 _pad(TEGRA_IO_PAD_SDMMC4, 36, UINT_MAX, "sdmmc4"), \
2157 _pad(TEGRA_IO_PAD_CAM, 38, UINT_MAX, "cam"), \
2158 _pad(TEGRA_IO_PAD_DSIB, 40, UINT_MAX, "dsib"), \
2159 _pad(TEGRA_IO_PAD_DSIC, 41, UINT_MAX, "dsic"), \
2160 _pad(TEGRA_IO_PAD_DSID, 42, UINT_MAX, "dsid"), \
2161 _pad(TEGRA_IO_PAD_CSIC, 43, UINT_MAX, "csic"), \
2162 _pad(TEGRA_IO_PAD_CSID, 44, UINT_MAX, "csid"), \
2163 _pad(TEGRA_IO_PAD_CSIE, 45, UINT_MAX, "csie"), \
2164 _pad(TEGRA_IO_PAD_CSIF, 46, UINT_MAX, "csif"), \
2165 _pad(TEGRA_IO_PAD_SPI, 47, UINT_MAX, "spi"), \
2166 _pad(TEGRA_IO_PAD_UFS, 49, UINT_MAX, "ufs"), \
2167 _pad(TEGRA_IO_PAD_DMIC_HV, 52, 2, "dmic-hv"), \
2168 _pad(TEGRA_IO_PAD_EDP, 53, UINT_MAX, "edp"), \
2169 _pad(TEGRA_IO_PAD_SDMMC1_HV, 55, 4, "sdmmc1-hv"), \
2170 _pad(TEGRA_IO_PAD_SDMMC3_HV, 56, 6, "sdmmc3-hv"), \
2171 _pad(TEGRA_IO_PAD_CONN, 60, UINT_MAX, "conn"), \
2172 _pad(TEGRA_IO_PAD_AUDIO_HV, 61, 1, "audio-hv"), \
2173 _pad(TEGRA_IO_PAD_AO_HV, UINT_MAX, 0, "ao-hv")
2174
Thierry Redingc641ec62017-08-30 12:42:34 +02002175static const struct tegra_io_pad_soc tegra186_io_pads[] = {
Aapo Vienamo437c4f22018-08-10 21:08:10 +03002176 TEGRA186_IO_PAD_TABLE(TEGRA_IO_PAD)
Thierry Redingc641ec62017-08-30 12:42:34 +02002177};
2178
Aapo Vienamo4a37f112018-08-10 21:08:12 +03002179static const struct pinctrl_pin_desc tegra186_pin_descs[] = {
2180 TEGRA186_IO_PAD_TABLE(TEGRA_IO_PIN_DESC)
Thierry Redingc641ec62017-08-30 12:42:34 +02002181};
2182
2183static const struct tegra_pmc_regs tegra186_pmc_regs = {
2184 .scratch0 = 0x2000,
2185 .dpd_req = 0x74,
2186 .dpd_status = 0x78,
2187 .dpd2_req = 0x7c,
2188 .dpd2_status = 0x80,
Sandipan Patra5f84bb12018-10-24 12:38:00 +05302189 .rst_status = 0x70,
2190 .rst_source_shift = 0x2,
2191 .rst_source_mask = 0x3C,
2192 .rst_level_shift = 0x0,
2193 .rst_level_mask = 0x3,
Thierry Redingc641ec62017-08-30 12:42:34 +02002194};
2195
2196static void tegra186_pmc_setup_irq_polarity(struct tegra_pmc *pmc,
2197 struct device_node *np,
2198 bool invert)
2199{
2200 struct resource regs;
2201 void __iomem *wake;
2202 u32 value;
2203 int index;
2204
2205 index = of_property_match_string(np, "reg-names", "wake");
2206 if (index < 0) {
2207 pr_err("failed to find PMC wake registers\n");
2208 return;
2209 }
2210
2211 of_address_to_resource(np, index, &regs);
2212
2213 wake = ioremap_nocache(regs.start, resource_size(&regs));
2214 if (!wake) {
2215 pr_err("failed to map PMC wake registers\n");
2216 return;
2217 }
2218
2219 value = readl(wake + WAKE_AOWAKE_CTRL);
2220
2221 if (invert)
2222 value |= WAKE_AOWAKE_CTRL_INTR_POLARITY;
2223 else
2224 value &= ~WAKE_AOWAKE_CTRL_INTR_POLARITY;
2225
2226 writel(value, wake + WAKE_AOWAKE_CTRL);
2227
2228 iounmap(wake);
2229}
2230
2231static const struct tegra_pmc_soc tegra186_pmc_soc = {
2232 .num_powergates = 0,
2233 .powergates = NULL,
2234 .num_cpu_powergates = 0,
2235 .cpu_powergates = NULL,
2236 .has_tsense_reset = false,
2237 .has_gpu_clamps = false,
Aapo Vienamo13136a42018-08-10 21:08:07 +03002238 .has_impl_33v_pwr = true,
Thierry Redingc641ec62017-08-30 12:42:34 +02002239 .num_io_pads = ARRAY_SIZE(tegra186_io_pads),
2240 .io_pads = tegra186_io_pads,
Aapo Vienamo4a37f112018-08-10 21:08:12 +03002241 .num_pin_descs = ARRAY_SIZE(tegra186_pin_descs),
2242 .pin_descs = tegra186_pin_descs,
Thierry Redingc641ec62017-08-30 12:42:34 +02002243 .regs = &tegra186_pmc_regs,
2244 .init = NULL,
2245 .setup_irq_polarity = tegra186_pmc_setup_irq_polarity,
Sandipan Patra5f84bb12018-10-24 12:38:00 +05302246 .reset_sources = tegra186_reset_sources,
2247 .num_reset_sources = 14,
2248 .reset_levels = tegra186_reset_levels,
2249 .num_reset_levels = 3,
Thierry Redingc641ec62017-08-30 12:42:34 +02002250};
2251
Thierry Reding72323982014-07-11 13:19:06 +02002252static const struct of_device_id tegra_pmc_match[] = {
Mikko Perttunen56327f52018-02-20 13:58:08 +02002253 { .compatible = "nvidia,tegra194-pmc", .data = &tegra186_pmc_soc },
Thierry Redingc641ec62017-08-30 12:42:34 +02002254 { .compatible = "nvidia,tegra186-pmc", .data = &tegra186_pmc_soc },
Thierry Redingc2fe4692015-03-23 11:31:29 +01002255 { .compatible = "nvidia,tegra210-pmc", .data = &tegra210_pmc_soc },
Thierry Reding7d71e9032015-04-29 12:42:28 +02002256 { .compatible = "nvidia,tegra132-pmc", .data = &tegra124_pmc_soc },
Thierry Reding72323982014-07-11 13:19:06 +02002257 { .compatible = "nvidia,tegra124-pmc", .data = &tegra124_pmc_soc },
2258 { .compatible = "nvidia,tegra114-pmc", .data = &tegra114_pmc_soc },
2259 { .compatible = "nvidia,tegra30-pmc", .data = &tegra30_pmc_soc },
2260 { .compatible = "nvidia,tegra20-pmc", .data = &tegra20_pmc_soc },
2261 { }
2262};
2263
2264static struct platform_driver tegra_pmc_driver = {
2265 .driver = {
2266 .name = "tegra-pmc",
2267 .suppress_bind_attrs = true,
2268 .of_match_table = tegra_pmc_match,
Paul Walmsley2b20b612014-12-09 22:36:50 +00002269#if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM)
Thierry Reding72323982014-07-11 13:19:06 +02002270 .pm = &tegra_pmc_pm_ops,
Paul Walmsley2b20b612014-12-09 22:36:50 +00002271#endif
Thierry Reding72323982014-07-11 13:19:06 +02002272 },
2273 .probe = tegra_pmc_probe,
2274};
Paul Gortmaker7d4d9ed2015-05-01 20:10:57 -04002275builtin_platform_driver(tegra_pmc_driver);
Thierry Reding72323982014-07-11 13:19:06 +02002276
2277/*
2278 * Early initialization to allow access to registers in the very early boot
2279 * process.
2280 */
2281static int __init tegra_pmc_early_init(void)
2282{
2283 const struct of_device_id *match;
2284 struct device_node *np;
2285 struct resource regs;
2286 bool invert;
Thierry Reding72323982014-07-11 13:19:06 +02002287
Jon Hunter61fd2842016-06-28 11:38:26 +01002288 mutex_init(&pmc->powergates_lock);
2289
Thierry Reding72323982014-07-11 13:19:06 +02002290 np = of_find_matching_node_and_match(NULL, tegra_pmc_match, &match);
2291 if (!np) {
Thierry Reding7d71e9032015-04-29 12:42:28 +02002292 /*
2293 * Fall back to legacy initialization for 32-bit ARM only. All
2294 * 64-bit ARM device tree files for Tegra are required to have
2295 * a PMC node.
2296 *
2297 * This is for backwards-compatibility with old device trees
2298 * that didn't contain a PMC node. Note that in this case the
2299 * SoC data can't be matched and therefore powergating is
2300 * disabled.
2301 */
2302 if (IS_ENABLED(CONFIG_ARM) && soc_is_tegra()) {
2303 pr_warn("DT node not found, powergating disabled\n");
Thierry Reding72323982014-07-11 13:19:06 +02002304
Thierry Reding7d71e9032015-04-29 12:42:28 +02002305 regs.start = 0x7000e400;
2306 regs.end = 0x7000e7ff;
2307 regs.flags = IORESOURCE_MEM;
Thierry Reding72323982014-07-11 13:19:06 +02002308
Thierry Reding7d71e9032015-04-29 12:42:28 +02002309 pr_warn("Using memory region %pR\n", &regs);
2310 } else {
2311 /*
2312 * At this point we're not running on Tegra, so play
2313 * nice with multi-platform kernels.
2314 */
2315 return 0;
2316 }
Thierry Reding72323982014-07-11 13:19:06 +02002317 } else {
Thierry Reding7d71e9032015-04-29 12:42:28 +02002318 /*
2319 * Extract information from the device tree if we've found a
2320 * matching node.
2321 */
2322 if (of_address_to_resource(np, 0, &regs) < 0) {
2323 pr_err("failed to get PMC registers\n");
Jon Hunterb69a6252016-06-28 11:38:27 +01002324 of_node_put(np);
Thierry Reding7d71e9032015-04-29 12:42:28 +02002325 return -ENXIO;
2326 }
Thierry Reding72323982014-07-11 13:19:06 +02002327 }
2328
2329 pmc->base = ioremap_nocache(regs.start, resource_size(&regs));
2330 if (!pmc->base) {
2331 pr_err("failed to map PMC registers\n");
Jon Hunterb69a6252016-06-28 11:38:27 +01002332 of_node_put(np);
Thierry Reding72323982014-07-11 13:19:06 +02002333 return -ENXIO;
2334 }
2335
Jon Hunter11131892016-06-28 11:38:24 +01002336 if (np) {
Jon Hunter718a2422016-06-28 11:38:25 +01002337 pmc->soc = match->data;
2338
Jon Huntere2d17962016-06-30 11:56:25 +01002339 tegra_powergate_init(pmc, np);
Thierry Reding72323982014-07-11 13:19:06 +02002340
Jon Hunter11131892016-06-28 11:38:24 +01002341 /*
2342 * Invert the interrupt polarity if a PMC device tree node
2343 * exists and contains the nvidia,invert-interrupt property.
2344 */
2345 invert = of_property_read_bool(np, "nvidia,invert-interrupt");
Thierry Reding72323982014-07-11 13:19:06 +02002346
Thierry Reding5be22552017-08-30 12:32:58 +02002347 pmc->soc->setup_irq_polarity(pmc, np, invert);
Jon Hunterb69a6252016-06-28 11:38:27 +01002348
2349 of_node_put(np);
Jon Hunter11131892016-06-28 11:38:24 +01002350 }
Thierry Reding72323982014-07-11 13:19:06 +02002351
2352 return 0;
2353}
2354early_initcall(tegra_pmc_early_init);