blob: c04ff5eb7cade4ecf55703a3d6f9e5665c678079 [file] [log] [blame]
Thierry Reding72323982014-07-11 13:19:06 +02001/*
2 * drivers/soc/tegra/pmc.c
3 *
4 * Copyright (c) 2010 Google, Inc
5 *
6 * Author:
7 * Colin Cross <ccross@google.com>
8 *
9 * This software is licensed under the terms of the GNU General Public
10 * License version 2, as published by the Free Software Foundation, and
11 * may be copied, distributed, and modified under those terms.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 */
19
Thierry Reding7d71e9032015-04-29 12:42:28 +020020#define pr_fmt(fmt) "tegra-pmc: " fmt
21
Thierry Reding72323982014-07-11 13:19:06 +020022#include <linux/kernel.h>
23#include <linux/clk.h>
24#include <linux/clk/tegra.h>
25#include <linux/debugfs.h>
26#include <linux/delay.h>
27#include <linux/err.h>
28#include <linux/export.h>
29#include <linux/init.h>
30#include <linux/io.h>
Jon Hunter0a2d87e2016-02-26 15:48:40 +000031#include <linux/iopoll.h>
Thierry Reding72323982014-07-11 13:19:06 +020032#include <linux/of.h>
33#include <linux/of_address.h>
Geert Uytterhoeven3fd01212018-04-18 16:50:04 +020034#include <linux/of_clk.h>
Jon Huntera3804512016-03-30 10:15:15 +010035#include <linux/of_platform.h>
Thierry Reding72323982014-07-11 13:19:06 +020036#include <linux/platform_device.h>
Jon Huntera3804512016-03-30 10:15:15 +010037#include <linux/pm_domain.h>
Thierry Reding72323982014-07-11 13:19:06 +020038#include <linux/reboot.h>
39#include <linux/reset.h>
40#include <linux/seq_file.h>
Jon Huntera3804512016-03-30 10:15:15 +010041#include <linux/slab.h>
Thierry Reding72323982014-07-11 13:19:06 +020042#include <linux/spinlock.h>
43
44#include <soc/tegra/common.h>
45#include <soc/tegra/fuse.h>
46#include <soc/tegra/pmc.h>
47
48#define PMC_CNTRL 0x0
Laxman Dewangan6c0bd212016-06-17 18:36:12 +053049#define PMC_CNTRL_INTR_POLARITY BIT(17) /* inverts INTR polarity */
Thierry Reding95b780b2016-10-10 13:13:36 +020050#define PMC_CNTRL_CPU_PWRREQ_OE BIT(16) /* CPU pwr req enable */
51#define PMC_CNTRL_CPU_PWRREQ_POLARITY BIT(15) /* CPU pwr req polarity */
52#define PMC_CNTRL_SIDE_EFFECT_LP0 BIT(14) /* LP0 when CPU pwr gated */
53#define PMC_CNTRL_SYSCLK_OE BIT(11) /* system clock enable */
54#define PMC_CNTRL_SYSCLK_POLARITY BIT(10) /* sys clk polarity */
55#define PMC_CNTRL_MAIN_RST BIT(4)
Thierry Reding72323982014-07-11 13:19:06 +020056
57#define DPD_SAMPLE 0x020
Laxman Dewangan6c0bd212016-06-17 18:36:12 +053058#define DPD_SAMPLE_ENABLE BIT(0)
Thierry Reding72323982014-07-11 13:19:06 +020059#define DPD_SAMPLE_DISABLE (0 << 0)
60
61#define PWRGATE_TOGGLE 0x30
Laxman Dewangan6c0bd212016-06-17 18:36:12 +053062#define PWRGATE_TOGGLE_START BIT(8)
Thierry Reding72323982014-07-11 13:19:06 +020063
64#define REMOVE_CLAMPING 0x34
65
66#define PWRGATE_STATUS 0x38
67
Aapo Vienamo13136a42018-08-10 21:08:07 +030068#define PMC_IMPL_E_33V_PWR 0x40
69
Laxman Dewangan21b49912016-10-10 15:14:34 +020070#define PMC_PWR_DET 0x48
71
Thierry Reding5be22552017-08-30 12:32:58 +020072#define PMC_SCRATCH0_MODE_RECOVERY BIT(31)
73#define PMC_SCRATCH0_MODE_BOOTLOADER BIT(30)
74#define PMC_SCRATCH0_MODE_RCM BIT(1)
75#define PMC_SCRATCH0_MODE_MASK (PMC_SCRATCH0_MODE_RECOVERY | \
Thierry Reding72323982014-07-11 13:19:06 +020076 PMC_SCRATCH0_MODE_BOOTLOADER | \
77 PMC_SCRATCH0_MODE_RCM)
78
79#define PMC_CPUPWRGOOD_TIMER 0xc8
80#define PMC_CPUPWROFF_TIMER 0xcc
81
Laxman Dewangan21b49912016-10-10 15:14:34 +020082#define PMC_PWR_DET_VALUE 0xe4
83
Thierry Reding72323982014-07-11 13:19:06 +020084#define PMC_SCRATCH41 0x140
85
Mikko Perttunen3568df32015-01-06 12:52:58 +020086#define PMC_SENSOR_CTRL 0x1b0
Laxman Dewangan6c0bd212016-06-17 18:36:12 +053087#define PMC_SENSOR_CTRL_SCRATCH_WRITE BIT(2)
88#define PMC_SENSOR_CTRL_ENABLE_RST BIT(1)
Mikko Perttunen3568df32015-01-06 12:52:58 +020089
Thierry Redingf5353c62015-12-30 17:13:29 +010090#define PMC_RST_STATUS 0x1b4
91#define PMC_RST_STATUS_POR 0
92#define PMC_RST_STATUS_WATCHDOG 1
93#define PMC_RST_STATUS_SENSOR 2
94#define PMC_RST_STATUS_SW_MAIN 3
95#define PMC_RST_STATUS_LP0 4
96#define PMC_RST_STATUS_AOTAG 5
97
Thierry Reding72323982014-07-11 13:19:06 +020098#define IO_DPD_REQ 0x1b8
Laxman Dewangan6c0bd212016-06-17 18:36:12 +053099#define IO_DPD_REQ_CODE_IDLE (0U << 30)
100#define IO_DPD_REQ_CODE_OFF (1U << 30)
101#define IO_DPD_REQ_CODE_ON (2U << 30)
102#define IO_DPD_REQ_CODE_MASK (3U << 30)
Thierry Reding72323982014-07-11 13:19:06 +0200103
104#define IO_DPD_STATUS 0x1bc
105#define IO_DPD2_REQ 0x1c0
106#define IO_DPD2_STATUS 0x1c4
107#define SEL_DPD_TIM 0x1c8
108
Mikko Perttunen3568df32015-01-06 12:52:58 +0200109#define PMC_SCRATCH54 0x258
Laxman Dewangan6c0bd212016-06-17 18:36:12 +0530110#define PMC_SCRATCH54_DATA_SHIFT 8
111#define PMC_SCRATCH54_ADDR_SHIFT 0
Mikko Perttunen3568df32015-01-06 12:52:58 +0200112
113#define PMC_SCRATCH55 0x25c
Laxman Dewangan6c0bd212016-06-17 18:36:12 +0530114#define PMC_SCRATCH55_RESET_TEGRA BIT(31)
115#define PMC_SCRATCH55_CNTRL_ID_SHIFT 27
116#define PMC_SCRATCH55_PINMUX_SHIFT 24
117#define PMC_SCRATCH55_16BITOP BIT(15)
118#define PMC_SCRATCH55_CHECKSUM_SHIFT 16
119#define PMC_SCRATCH55_I2CSLV1_SHIFT 0
Mikko Perttunen3568df32015-01-06 12:52:58 +0200120
Thierry Reding72323982014-07-11 13:19:06 +0200121#define GPU_RG_CNTRL 0x2d4
122
Thierry Redingc641ec62017-08-30 12:42:34 +0200123/* Tegra186 and later */
124#define WAKE_AOWAKE_CTRL 0x4f4
125#define WAKE_AOWAKE_CTRL_INTR_POLARITY BIT(0)
126
Jon Huntera3804512016-03-30 10:15:15 +0100127struct tegra_powergate {
128 struct generic_pm_domain genpd;
129 struct tegra_pmc *pmc;
130 unsigned int id;
131 struct clk **clks;
132 unsigned int num_clks;
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200133 struct reset_control *reset;
Jon Huntera3804512016-03-30 10:15:15 +0100134};
135
Laxman Dewangan21b49912016-10-10 15:14:34 +0200136struct tegra_io_pad_soc {
137 enum tegra_io_pad id;
138 unsigned int dpd;
139 unsigned int voltage;
140};
141
Thierry Reding5be22552017-08-30 12:32:58 +0200142struct tegra_pmc_regs {
143 unsigned int scratch0;
144 unsigned int dpd_req;
145 unsigned int dpd_status;
146 unsigned int dpd2_req;
147 unsigned int dpd2_status;
148};
149
Thierry Reding72323982014-07-11 13:19:06 +0200150struct tegra_pmc_soc {
151 unsigned int num_powergates;
152 const char *const *powergates;
153 unsigned int num_cpu_powergates;
154 const u8 *cpu_powergates;
Thierry Redinga9a40a42015-01-09 11:15:33 +0100155
Mikko Perttunen3568df32015-01-06 12:52:58 +0200156 bool has_tsense_reset;
Thierry Redinga9a40a42015-01-09 11:15:33 +0100157 bool has_gpu_clamps;
Peter De Schrijvera263394a2018-01-25 16:00:13 +0200158 bool needs_mbist_war;
Aapo Vienamo13136a42018-08-10 21:08:07 +0300159 bool has_impl_33v_pwr;
Laxman Dewangan21b49912016-10-10 15:14:34 +0200160
161 const struct tegra_io_pad_soc *io_pads;
162 unsigned int num_io_pads;
Thierry Reding5be22552017-08-30 12:32:58 +0200163
164 const struct tegra_pmc_regs *regs;
165 void (*init)(struct tegra_pmc *pmc);
166 void (*setup_irq_polarity)(struct tegra_pmc *pmc,
167 struct device_node *np,
168 bool invert);
Thierry Reding72323982014-07-11 13:19:06 +0200169};
170
171/**
172 * struct tegra_pmc - NVIDIA Tegra PMC
Jon Hunter35b67292015-12-04 14:57:03 +0000173 * @dev: pointer to PMC device structure
Thierry Reding72323982014-07-11 13:19:06 +0200174 * @base: pointer to I/O remapped register region
175 * @clk: pointer to pclk clock
Jon Hunter35b67292015-12-04 14:57:03 +0000176 * @soc: pointer to SoC data structure
Jon Hunter3195ac62015-12-04 14:57:05 +0000177 * @debugfs: pointer to debugfs entry
Thierry Reding72323982014-07-11 13:19:06 +0200178 * @rate: currently configured rate of pclk
179 * @suspend_mode: lowest suspend mode available
180 * @cpu_good_time: CPU power good time (in microseconds)
181 * @cpu_off_time: CPU power off time (in microsecends)
182 * @core_osc_time: core power good OSC time (in microseconds)
183 * @core_pmu_time: core power good PMU time (in microseconds)
184 * @core_off_time: core power off time (in microseconds)
185 * @corereq_high: core power request is active-high
186 * @sysclkreq_high: system clock request is active-high
187 * @combined_req: combined power request for CPU & core
188 * @cpu_pwr_good_en: CPU power good signal is enabled
189 * @lp0_vec_phys: physical base address of the LP0 warm boot code
190 * @lp0_vec_size: size of the LP0 warm boot code
Jon Huntera3804512016-03-30 10:15:15 +0100191 * @powergates_available: Bitmap of available power gates
Thierry Reding72323982014-07-11 13:19:06 +0200192 * @powergates_lock: mutex for power gate register access
193 */
194struct tegra_pmc {
Mikko Perttunen3568df32015-01-06 12:52:58 +0200195 struct device *dev;
Thierry Reding72323982014-07-11 13:19:06 +0200196 void __iomem *base;
Thierry Redingc641ec62017-08-30 12:42:34 +0200197 void __iomem *wake;
198 void __iomem *aotag;
Thierry Reding5be22552017-08-30 12:32:58 +0200199 void __iomem *scratch;
Thierry Reding72323982014-07-11 13:19:06 +0200200 struct clk *clk;
Jon Hunter3195ac62015-12-04 14:57:05 +0000201 struct dentry *debugfs;
Thierry Reding72323982014-07-11 13:19:06 +0200202
203 const struct tegra_pmc_soc *soc;
204
205 unsigned long rate;
206
207 enum tegra_suspend_mode suspend_mode;
208 u32 cpu_good_time;
209 u32 cpu_off_time;
210 u32 core_osc_time;
211 u32 core_pmu_time;
212 u32 core_off_time;
213 bool corereq_high;
214 bool sysclkreq_high;
215 bool combined_req;
216 bool cpu_pwr_good_en;
217 u32 lp0_vec_phys;
218 u32 lp0_vec_size;
Jon Huntera3804512016-03-30 10:15:15 +0100219 DECLARE_BITMAP(powergates_available, TEGRA_POWERGATE_MAX);
Thierry Reding72323982014-07-11 13:19:06 +0200220
221 struct mutex powergates_lock;
222};
223
224static struct tegra_pmc *pmc = &(struct tegra_pmc) {
225 .base = NULL,
226 .suspend_mode = TEGRA_SUSPEND_NONE,
227};
228
Jon Huntera3804512016-03-30 10:15:15 +0100229static inline struct tegra_powergate *
230to_powergate(struct generic_pm_domain *domain)
231{
232 return container_of(domain, struct tegra_powergate, genpd);
233}
234
Thierry Reding72323982014-07-11 13:19:06 +0200235static u32 tegra_pmc_readl(unsigned long offset)
236{
237 return readl(pmc->base + offset);
238}
239
240static void tegra_pmc_writel(u32 value, unsigned long offset)
241{
242 writel(value, pmc->base + offset);
243}
244
Jon Hunter0ecf2d32016-02-11 18:03:23 +0000245static inline bool tegra_powergate_state(int id)
246{
Jon Hunterbc9af232016-02-15 12:38:11 +0000247 if (id == TEGRA_POWERGATE_3D && pmc->soc->has_gpu_clamps)
248 return (tegra_pmc_readl(GPU_RG_CNTRL) & 0x1) == 0;
249 else
250 return (tegra_pmc_readl(PWRGATE_STATUS) & BIT(id)) != 0;
Jon Hunter0ecf2d32016-02-11 18:03:23 +0000251}
252
Jon Hunter0a243bd2016-02-11 18:03:24 +0000253static inline bool tegra_powergate_is_valid(int id)
254{
255 return (pmc->soc && pmc->soc->powergates[id]);
256}
257
Jon Huntera3804512016-03-30 10:15:15 +0100258static inline bool tegra_powergate_is_available(int id)
259{
260 return test_bit(id, pmc->powergates_available);
261}
262
263static int tegra_powergate_lookup(struct tegra_pmc *pmc, const char *name)
264{
265 unsigned int i;
266
267 if (!pmc || !pmc->soc || !name)
268 return -EINVAL;
269
270 for (i = 0; i < pmc->soc->num_powergates; i++) {
271 if (!tegra_powergate_is_valid(i))
272 continue;
273
274 if (!strcmp(name, pmc->soc->powergates[i]))
275 return i;
276 }
277
Jon Huntera3804512016-03-30 10:15:15 +0100278 return -ENODEV;
279}
280
Thierry Reding72323982014-07-11 13:19:06 +0200281/**
282 * tegra_powergate_set() - set the state of a partition
283 * @id: partition ID
284 * @new_state: new state of the partition
285 */
Jon Hunter70293ed2016-02-11 18:03:22 +0000286static int tegra_powergate_set(unsigned int id, bool new_state)
Thierry Reding72323982014-07-11 13:19:06 +0200287{
Jon Hunter0a2d87e2016-02-26 15:48:40 +0000288 bool status;
289 int err;
290
Jon Hunterbc9af232016-02-15 12:38:11 +0000291 if (id == TEGRA_POWERGATE_3D && pmc->soc->has_gpu_clamps)
292 return -EINVAL;
293
Thierry Reding72323982014-07-11 13:19:06 +0200294 mutex_lock(&pmc->powergates_lock);
295
Jon Hunter0ecf2d32016-02-11 18:03:23 +0000296 if (tegra_powergate_state(id) == new_state) {
Thierry Reding72323982014-07-11 13:19:06 +0200297 mutex_unlock(&pmc->powergates_lock);
298 return 0;
299 }
300
301 tegra_pmc_writel(PWRGATE_TOGGLE_START | id, PWRGATE_TOGGLE);
302
Jon Hunter0a2d87e2016-02-26 15:48:40 +0000303 err = readx_poll_timeout(tegra_powergate_state, id, status,
304 status == new_state, 10, 100000);
305
Thierry Reding72323982014-07-11 13:19:06 +0200306 mutex_unlock(&pmc->powergates_lock);
307
Jon Hunter0a2d87e2016-02-26 15:48:40 +0000308 return err;
Thierry Reding72323982014-07-11 13:19:06 +0200309}
310
Jon Huntera3804512016-03-30 10:15:15 +0100311static int __tegra_powergate_remove_clamping(unsigned int id)
Thierry Reding72323982014-07-11 13:19:06 +0200312{
313 u32 mask;
314
Jon Huntere8cf6612016-02-11 18:03:21 +0000315 mutex_lock(&pmc->powergates_lock);
316
Thierry Reding72323982014-07-11 13:19:06 +0200317 /*
Thierry Redinga9a40a42015-01-09 11:15:33 +0100318 * On Tegra124 and later, the clamps for the GPU are controlled by a
319 * separate register (with different semantics).
Thierry Reding72323982014-07-11 13:19:06 +0200320 */
Thierry Redinga9a40a42015-01-09 11:15:33 +0100321 if (id == TEGRA_POWERGATE_3D) {
322 if (pmc->soc->has_gpu_clamps) {
Thierry Reding72323982014-07-11 13:19:06 +0200323 tegra_pmc_writel(0, GPU_RG_CNTRL);
Jon Huntere8cf6612016-02-11 18:03:21 +0000324 goto out;
Thierry Reding72323982014-07-11 13:19:06 +0200325 }
326 }
327
328 /*
329 * Tegra 2 has a bug where PCIE and VDE clamping masks are
330 * swapped relatively to the partition ids
331 */
332 if (id == TEGRA_POWERGATE_VDEC)
333 mask = (1 << TEGRA_POWERGATE_PCIE);
334 else if (id == TEGRA_POWERGATE_PCIE)
335 mask = (1 << TEGRA_POWERGATE_VDEC);
336 else
337 mask = (1 << id);
338
339 tegra_pmc_writel(mask, REMOVE_CLAMPING);
340
Jon Huntere8cf6612016-02-11 18:03:21 +0000341out:
342 mutex_unlock(&pmc->powergates_lock);
343
Thierry Reding72323982014-07-11 13:19:06 +0200344 return 0;
345}
Jon Huntera3804512016-03-30 10:15:15 +0100346
347static void tegra_powergate_disable_clocks(struct tegra_powergate *pg)
348{
349 unsigned int i;
350
351 for (i = 0; i < pg->num_clks; i++)
352 clk_disable_unprepare(pg->clks[i]);
353}
354
355static int tegra_powergate_enable_clocks(struct tegra_powergate *pg)
356{
357 unsigned int i;
358 int err;
359
360 for (i = 0; i < pg->num_clks; i++) {
361 err = clk_prepare_enable(pg->clks[i]);
362 if (err)
363 goto out;
364 }
365
366 return 0;
367
368out:
369 while (i--)
370 clk_disable_unprepare(pg->clks[i]);
371
372 return err;
373}
374
Peter De Schrijvera263394a2018-01-25 16:00:13 +0200375int __weak tegra210_clk_handle_mbist_war(unsigned int id)
376{
377 return 0;
378}
379
Jon Huntera3804512016-03-30 10:15:15 +0100380static int tegra_powergate_power_up(struct tegra_powergate *pg,
381 bool disable_clocks)
382{
383 int err;
384
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200385 err = reset_control_assert(pg->reset);
Jon Huntera3804512016-03-30 10:15:15 +0100386 if (err)
387 return err;
388
389 usleep_range(10, 20);
390
391 err = tegra_powergate_set(pg->id, true);
392 if (err < 0)
393 return err;
394
395 usleep_range(10, 20);
396
397 err = tegra_powergate_enable_clocks(pg);
398 if (err)
399 goto disable_clks;
400
401 usleep_range(10, 20);
402
403 err = __tegra_powergate_remove_clamping(pg->id);
404 if (err)
405 goto disable_clks;
406
407 usleep_range(10, 20);
408
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200409 err = reset_control_deassert(pg->reset);
Jon Huntera3804512016-03-30 10:15:15 +0100410 if (err)
411 goto powergate_off;
412
413 usleep_range(10, 20);
414
Peter De Schrijvera263394a2018-01-25 16:00:13 +0200415 if (pg->pmc->soc->needs_mbist_war)
416 err = tegra210_clk_handle_mbist_war(pg->id);
417 if (err)
418 goto disable_clks;
419
Jon Huntera3804512016-03-30 10:15:15 +0100420 if (disable_clocks)
421 tegra_powergate_disable_clocks(pg);
422
423 return 0;
424
425disable_clks:
426 tegra_powergate_disable_clocks(pg);
427 usleep_range(10, 20);
Thierry Redingda8f4b42016-06-30 12:12:55 +0200428
Jon Huntera3804512016-03-30 10:15:15 +0100429powergate_off:
430 tegra_powergate_set(pg->id, false);
431
432 return err;
433}
434
435static int tegra_powergate_power_down(struct tegra_powergate *pg)
436{
437 int err;
438
439 err = tegra_powergate_enable_clocks(pg);
440 if (err)
441 return err;
442
443 usleep_range(10, 20);
444
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200445 err = reset_control_assert(pg->reset);
Jon Huntera3804512016-03-30 10:15:15 +0100446 if (err)
447 goto disable_clks;
448
449 usleep_range(10, 20);
450
451 tegra_powergate_disable_clocks(pg);
452
453 usleep_range(10, 20);
454
455 err = tegra_powergate_set(pg->id, false);
456 if (err)
457 goto assert_resets;
458
459 return 0;
460
461assert_resets:
462 tegra_powergate_enable_clocks(pg);
463 usleep_range(10, 20);
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200464 reset_control_deassert(pg->reset);
Jon Huntera3804512016-03-30 10:15:15 +0100465 usleep_range(10, 20);
Thierry Redingda8f4b42016-06-30 12:12:55 +0200466
Jon Huntera3804512016-03-30 10:15:15 +0100467disable_clks:
468 tegra_powergate_disable_clocks(pg);
469
470 return err;
471}
472
473static int tegra_genpd_power_on(struct generic_pm_domain *domain)
474{
475 struct tegra_powergate *pg = to_powergate(domain);
Jon Huntera3804512016-03-30 10:15:15 +0100476 int err;
477
478 err = tegra_powergate_power_up(pg, true);
479 if (err)
Thierry Reding54e24722016-11-08 10:58:32 +0100480 pr_err("failed to turn on PM domain %s: %d\n", pg->genpd.name,
481 err);
Jon Huntera3804512016-03-30 10:15:15 +0100482
483 return err;
484}
485
486static int tegra_genpd_power_off(struct generic_pm_domain *domain)
487{
488 struct tegra_powergate *pg = to_powergate(domain);
Jon Huntera3804512016-03-30 10:15:15 +0100489 int err;
490
491 err = tegra_powergate_power_down(pg);
492 if (err)
Thierry Reding54e24722016-11-08 10:58:32 +0100493 pr_err("failed to turn off PM domain %s: %d\n",
494 pg->genpd.name, err);
Jon Huntera3804512016-03-30 10:15:15 +0100495
496 return err;
497}
498
499/**
500 * tegra_powergate_power_on() - power on partition
501 * @id: partition ID
502 */
503int tegra_powergate_power_on(unsigned int id)
504{
505 if (!tegra_powergate_is_available(id))
506 return -EINVAL;
507
508 return tegra_powergate_set(id, true);
509}
510
511/**
512 * tegra_powergate_power_off() - power off partition
513 * @id: partition ID
514 */
515int tegra_powergate_power_off(unsigned int id)
516{
517 if (!tegra_powergate_is_available(id))
518 return -EINVAL;
519
520 return tegra_powergate_set(id, false);
521}
522EXPORT_SYMBOL(tegra_powergate_power_off);
523
524/**
525 * tegra_powergate_is_powered() - check if partition is powered
526 * @id: partition ID
527 */
528int tegra_powergate_is_powered(unsigned int id)
529{
530 int status;
531
532 if (!tegra_powergate_is_valid(id))
533 return -EINVAL;
534
535 mutex_lock(&pmc->powergates_lock);
536 status = tegra_powergate_state(id);
537 mutex_unlock(&pmc->powergates_lock);
538
539 return status;
540}
541
542/**
543 * tegra_powergate_remove_clamping() - remove power clamps for partition
544 * @id: partition ID
545 */
546int tegra_powergate_remove_clamping(unsigned int id)
547{
548 if (!tegra_powergate_is_available(id))
549 return -EINVAL;
550
551 return __tegra_powergate_remove_clamping(id);
552}
Thierry Reding72323982014-07-11 13:19:06 +0200553EXPORT_SYMBOL(tegra_powergate_remove_clamping);
554
555/**
556 * tegra_powergate_sequence_power_up() - power up partition
557 * @id: partition ID
558 * @clk: clock for partition
559 * @rst: reset for partition
560 *
561 * Must be called with clk disabled, and returns with clk enabled.
562 */
Jon Hunter70293ed2016-02-11 18:03:22 +0000563int tegra_powergate_sequence_power_up(unsigned int id, struct clk *clk,
Thierry Reding72323982014-07-11 13:19:06 +0200564 struct reset_control *rst)
565{
Viresh Kumar495ac332018-05-03 13:56:17 +0530566 struct tegra_powergate *pg;
Jon Huntera3804512016-03-30 10:15:15 +0100567 int err;
Thierry Reding72323982014-07-11 13:19:06 +0200568
Jon Hunter403db2d2016-06-28 11:38:23 +0100569 if (!tegra_powergate_is_available(id))
570 return -EINVAL;
571
Viresh Kumar495ac332018-05-03 13:56:17 +0530572 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
573 if (!pg)
574 return -ENOMEM;
Thierry Reding72323982014-07-11 13:19:06 +0200575
Viresh Kumar495ac332018-05-03 13:56:17 +0530576 pg->id = id;
577 pg->clks = &clk;
578 pg->num_clks = 1;
579 pg->reset = rst;
580 pg->pmc = pmc;
581
582 err = tegra_powergate_power_up(pg, false);
Jon Huntera3804512016-03-30 10:15:15 +0100583 if (err)
584 pr_err("failed to turn on partition %d: %d\n", id, err);
Thierry Reding72323982014-07-11 13:19:06 +0200585
Viresh Kumar495ac332018-05-03 13:56:17 +0530586 kfree(pg);
587
Jon Huntera3804512016-03-30 10:15:15 +0100588 return err;
Thierry Reding72323982014-07-11 13:19:06 +0200589}
590EXPORT_SYMBOL(tegra_powergate_sequence_power_up);
591
592#ifdef CONFIG_SMP
593/**
594 * tegra_get_cpu_powergate_id() - convert from CPU ID to partition ID
595 * @cpuid: CPU partition ID
596 *
597 * Returns the partition ID corresponding to the CPU partition ID or a
598 * negative error code on failure.
599 */
Jon Hunter70293ed2016-02-11 18:03:22 +0000600static int tegra_get_cpu_powergate_id(unsigned int cpuid)
Thierry Reding72323982014-07-11 13:19:06 +0200601{
Jon Hunter70293ed2016-02-11 18:03:22 +0000602 if (pmc->soc && cpuid < pmc->soc->num_cpu_powergates)
Thierry Reding72323982014-07-11 13:19:06 +0200603 return pmc->soc->cpu_powergates[cpuid];
604
605 return -EINVAL;
606}
607
608/**
609 * tegra_pmc_cpu_is_powered() - check if CPU partition is powered
610 * @cpuid: CPU partition ID
611 */
Jon Hunter70293ed2016-02-11 18:03:22 +0000612bool tegra_pmc_cpu_is_powered(unsigned int cpuid)
Thierry Reding72323982014-07-11 13:19:06 +0200613{
614 int id;
615
616 id = tegra_get_cpu_powergate_id(cpuid);
617 if (id < 0)
618 return false;
619
620 return tegra_powergate_is_powered(id);
621}
622
623/**
624 * tegra_pmc_cpu_power_on() - power on CPU partition
625 * @cpuid: CPU partition ID
626 */
Jon Hunter70293ed2016-02-11 18:03:22 +0000627int tegra_pmc_cpu_power_on(unsigned int cpuid)
Thierry Reding72323982014-07-11 13:19:06 +0200628{
629 int id;
630
631 id = tegra_get_cpu_powergate_id(cpuid);
632 if (id < 0)
633 return id;
634
635 return tegra_powergate_set(id, true);
636}
637
638/**
639 * tegra_pmc_cpu_remove_clamping() - remove power clamps for CPU partition
640 * @cpuid: CPU partition ID
641 */
Jon Hunter70293ed2016-02-11 18:03:22 +0000642int tegra_pmc_cpu_remove_clamping(unsigned int cpuid)
Thierry Reding72323982014-07-11 13:19:06 +0200643{
644 int id;
645
646 id = tegra_get_cpu_powergate_id(cpuid);
647 if (id < 0)
648 return id;
649
650 return tegra_powergate_remove_clamping(id);
651}
652#endif /* CONFIG_SMP */
653
David Riley78921582015-03-18 10:52:25 +0100654static int tegra_pmc_restart_notify(struct notifier_block *this,
655 unsigned long action, void *data)
Thierry Reding72323982014-07-11 13:19:06 +0200656{
David Riley78921582015-03-18 10:52:25 +0100657 const char *cmd = data;
Thierry Reding72323982014-07-11 13:19:06 +0200658 u32 value;
659
Thierry Reding5be22552017-08-30 12:32:58 +0200660 value = readl(pmc->scratch + pmc->soc->regs->scratch0);
Thierry Reding72323982014-07-11 13:19:06 +0200661 value &= ~PMC_SCRATCH0_MODE_MASK;
662
663 if (cmd) {
664 if (strcmp(cmd, "recovery") == 0)
665 value |= PMC_SCRATCH0_MODE_RECOVERY;
666
667 if (strcmp(cmd, "bootloader") == 0)
668 value |= PMC_SCRATCH0_MODE_BOOTLOADER;
669
670 if (strcmp(cmd, "forced-recovery") == 0)
671 value |= PMC_SCRATCH0_MODE_RCM;
672 }
673
Thierry Reding5be22552017-08-30 12:32:58 +0200674 writel(value, pmc->scratch + pmc->soc->regs->scratch0);
Thierry Reding72323982014-07-11 13:19:06 +0200675
Thierry Redingf5353c62015-12-30 17:13:29 +0100676 /* reset everything but PMC_SCRATCH0 and PMC_RST_STATUS */
677 value = tegra_pmc_readl(PMC_CNTRL);
678 value |= PMC_CNTRL_MAIN_RST;
679 tegra_pmc_writel(value, PMC_CNTRL);
David Riley78921582015-03-18 10:52:25 +0100680
681 return NOTIFY_DONE;
Thierry Reding72323982014-07-11 13:19:06 +0200682}
683
David Riley78921582015-03-18 10:52:25 +0100684static struct notifier_block tegra_pmc_restart_handler = {
685 .notifier_call = tegra_pmc_restart_notify,
686 .priority = 128,
687};
688
Thierry Reding72323982014-07-11 13:19:06 +0200689static int powergate_show(struct seq_file *s, void *data)
690{
691 unsigned int i;
Jon Hunterc3ea2972016-02-11 18:03:25 +0000692 int status;
Thierry Reding72323982014-07-11 13:19:06 +0200693
694 seq_printf(s, " powergate powered\n");
695 seq_printf(s, "------------------\n");
696
697 for (i = 0; i < pmc->soc->num_powergates; i++) {
Jon Hunterc3ea2972016-02-11 18:03:25 +0000698 status = tegra_powergate_is_powered(i);
699 if (status < 0)
Thierry Reding72323982014-07-11 13:19:06 +0200700 continue;
701
702 seq_printf(s, " %9s %7s\n", pmc->soc->powergates[i],
Jon Hunterc3ea2972016-02-11 18:03:25 +0000703 status ? "yes" : "no");
Thierry Reding72323982014-07-11 13:19:06 +0200704 }
705
706 return 0;
707}
708
709static int powergate_open(struct inode *inode, struct file *file)
710{
711 return single_open(file, powergate_show, inode->i_private);
712}
713
714static const struct file_operations powergate_fops = {
715 .open = powergate_open,
716 .read = seq_read,
717 .llseek = seq_lseek,
718 .release = single_release,
719};
720
721static int tegra_powergate_debugfs_init(void)
722{
Jon Hunter3195ac62015-12-04 14:57:05 +0000723 pmc->debugfs = debugfs_create_file("powergate", S_IRUGO, NULL, NULL,
724 &powergate_fops);
725 if (!pmc->debugfs)
Thierry Reding72323982014-07-11 13:19:06 +0200726 return -ENOMEM;
727
728 return 0;
729}
730
Jon Huntera3804512016-03-30 10:15:15 +0100731static int tegra_powergate_of_get_clks(struct tegra_powergate *pg,
732 struct device_node *np)
733{
734 struct clk *clk;
735 unsigned int i, count;
736 int err;
737
Geert Uytterhoeven3fd01212018-04-18 16:50:04 +0200738 count = of_clk_get_parent_count(np);
Jon Huntera3804512016-03-30 10:15:15 +0100739 if (count == 0)
740 return -ENODEV;
741
742 pg->clks = kcalloc(count, sizeof(clk), GFP_KERNEL);
743 if (!pg->clks)
744 return -ENOMEM;
745
746 for (i = 0; i < count; i++) {
747 pg->clks[i] = of_clk_get(np, i);
748 if (IS_ERR(pg->clks[i])) {
749 err = PTR_ERR(pg->clks[i]);
750 goto err;
751 }
752 }
753
754 pg->num_clks = count;
755
756 return 0;
757
758err:
759 while (i--)
760 clk_put(pg->clks[i]);
Thierry Redingda8f4b42016-06-30 12:12:55 +0200761
Jon Huntera3804512016-03-30 10:15:15 +0100762 kfree(pg->clks);
763
764 return err;
765}
766
767static int tegra_powergate_of_get_resets(struct tegra_powergate *pg,
Jon Hunter05cfb982016-06-29 10:17:47 +0100768 struct device_node *np, bool off)
Jon Huntera3804512016-03-30 10:15:15 +0100769{
Jon Huntera3804512016-03-30 10:15:15 +0100770 int err;
771
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200772 pg->reset = of_reset_control_array_get_exclusive(np);
773 if (IS_ERR(pg->reset)) {
774 err = PTR_ERR(pg->reset);
775 pr_err("failed to get device resets: %d\n", err);
776 return err;
Jon Huntera3804512016-03-30 10:15:15 +0100777 }
778
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200779 if (off)
780 err = reset_control_assert(pg->reset);
781 else
782 err = reset_control_deassert(pg->reset);
Jon Huntera3804512016-03-30 10:15:15 +0100783
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200784 if (err)
785 reset_control_put(pg->reset);
Jon Huntera3804512016-03-30 10:15:15 +0100786
787 return err;
788}
789
790static void tegra_powergate_add(struct tegra_pmc *pmc, struct device_node *np)
791{
792 struct tegra_powergate *pg;
Jon Hunterc2710ac2016-06-30 11:56:24 +0100793 int id, err;
Jon Huntera3804512016-03-30 10:15:15 +0100794 bool off;
Jon Huntera3804512016-03-30 10:15:15 +0100795
796 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
797 if (!pg)
Jon Hunterc2710ac2016-06-30 11:56:24 +0100798 return;
Jon Huntera3804512016-03-30 10:15:15 +0100799
800 id = tegra_powergate_lookup(pmc, np->name);
Jon Hunterc2710ac2016-06-30 11:56:24 +0100801 if (id < 0) {
Thierry Reding54e24722016-11-08 10:58:32 +0100802 pr_err("powergate lookup failed for %s: %d\n", np->name, id);
Jon Huntera3804512016-03-30 10:15:15 +0100803 goto free_mem;
Jon Hunterc2710ac2016-06-30 11:56:24 +0100804 }
Jon Huntera3804512016-03-30 10:15:15 +0100805
806 /*
807 * Clear the bit for this powergate so it cannot be managed
808 * directly via the legacy APIs for controlling powergates.
809 */
810 clear_bit(id, pmc->powergates_available);
811
812 pg->id = id;
813 pg->genpd.name = np->name;
814 pg->genpd.power_off = tegra_genpd_power_off;
815 pg->genpd.power_on = tegra_genpd_power_on;
816 pg->pmc = pmc;
817
Jon Hunter05cfb982016-06-29 10:17:47 +0100818 off = !tegra_powergate_is_powered(pg->id);
819
Jon Hunterc2710ac2016-06-30 11:56:24 +0100820 err = tegra_powergate_of_get_clks(pg, np);
821 if (err < 0) {
Thierry Reding54e24722016-11-08 10:58:32 +0100822 pr_err("failed to get clocks for %s: %d\n", np->name, err);
Jon Huntera3804512016-03-30 10:15:15 +0100823 goto set_available;
Jon Hunterc2710ac2016-06-30 11:56:24 +0100824 }
Jon Huntera3804512016-03-30 10:15:15 +0100825
Jon Hunterc2710ac2016-06-30 11:56:24 +0100826 err = tegra_powergate_of_get_resets(pg, np, off);
827 if (err < 0) {
Thierry Reding54e24722016-11-08 10:58:32 +0100828 pr_err("failed to get resets for %s: %d\n", np->name, err);
Jon Huntera3804512016-03-30 10:15:15 +0100829 goto remove_clks;
Jon Hunterc2710ac2016-06-30 11:56:24 +0100830 }
Jon Huntera3804512016-03-30 10:15:15 +0100831
Jon Hunter0b137342016-10-22 20:23:56 +0100832 if (!IS_ENABLED(CONFIG_PM_GENERIC_DOMAINS)) {
833 if (off)
834 WARN_ON(tegra_powergate_power_up(pg, true));
835
836 goto remove_resets;
837 }
Jon Huntere2d17962016-06-30 11:56:25 +0100838
Jon Hunter8df12742016-06-30 11:56:26 +0100839 /*
840 * FIXME: If XHCI is enabled for Tegra, then power-up the XUSB
841 * host and super-speed partitions. Once the XHCI driver
842 * manages the partitions itself this code can be removed. Note
843 * that we don't register these partitions with the genpd core
844 * to avoid it from powering down the partitions as they appear
845 * to be unused.
846 */
847 if (IS_ENABLED(CONFIG_USB_XHCI_TEGRA) &&
Jon Hunter0b137342016-10-22 20:23:56 +0100848 (id == TEGRA_POWERGATE_XUSBA || id == TEGRA_POWERGATE_XUSBC)) {
849 if (off)
850 WARN_ON(tegra_powergate_power_up(pg, true));
851
852 goto remove_resets;
853 }
Jon Hunter8df12742016-06-30 11:56:26 +0100854
Jon Huntercd5ceda2016-10-22 20:23:55 +0100855 err = pm_genpd_init(&pg->genpd, NULL, off);
856 if (err < 0) {
Thierry Reding45221122016-11-08 11:05:03 +0100857 pr_err("failed to initialise PM domain %s: %d\n", np->name,
Jon Huntercd5ceda2016-10-22 20:23:55 +0100858 err);
859 goto remove_resets;
860 }
Jon Huntera3804512016-03-30 10:15:15 +0100861
Jon Hunterc2710ac2016-06-30 11:56:24 +0100862 err = of_genpd_add_provider_simple(np, &pg->genpd);
863 if (err < 0) {
Thierry Reding45221122016-11-08 11:05:03 +0100864 pr_err("failed to add PM domain provider for %s: %d\n",
865 np->name, err);
Jon Hunter0b137342016-10-22 20:23:56 +0100866 goto remove_genpd;
Jon Hunterc2710ac2016-06-30 11:56:24 +0100867 }
Jon Huntera3804512016-03-30 10:15:15 +0100868
Thierry Reding45221122016-11-08 11:05:03 +0100869 pr_debug("added PM domain %s\n", pg->genpd.name);
Jon Huntera3804512016-03-30 10:15:15 +0100870
871 return;
872
Jon Hunter0b137342016-10-22 20:23:56 +0100873remove_genpd:
874 pm_genpd_remove(&pg->genpd);
Jon Huntere2d17962016-06-30 11:56:25 +0100875
Jon Huntera3804512016-03-30 10:15:15 +0100876remove_resets:
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200877 reset_control_put(pg->reset);
Jon Huntera3804512016-03-30 10:15:15 +0100878
879remove_clks:
880 while (pg->num_clks--)
881 clk_put(pg->clks[pg->num_clks]);
Thierry Redingda8f4b42016-06-30 12:12:55 +0200882
Jon Huntera3804512016-03-30 10:15:15 +0100883 kfree(pg->clks);
884
885set_available:
886 set_bit(id, pmc->powergates_available);
887
888free_mem:
889 kfree(pg);
Jon Huntera3804512016-03-30 10:15:15 +0100890}
891
Jon Huntere2d17962016-06-30 11:56:25 +0100892static void tegra_powergate_init(struct tegra_pmc *pmc,
893 struct device_node *parent)
Jon Huntera3804512016-03-30 10:15:15 +0100894{
895 struct device_node *np, *child;
Jon Huntere2d17962016-06-30 11:56:25 +0100896 unsigned int i;
Jon Huntera3804512016-03-30 10:15:15 +0100897
Jon Huntere2d17962016-06-30 11:56:25 +0100898 /* Create a bitmap of the available and valid partitions */
899 for (i = 0; i < pmc->soc->num_powergates; i++)
900 if (pmc->soc->powergates[i])
901 set_bit(i, pmc->powergates_available);
902
903 np = of_get_child_by_name(parent, "powergates");
Jon Huntera3804512016-03-30 10:15:15 +0100904 if (!np)
905 return;
906
Tuomas Tynkkynen0c106e52017-07-29 02:58:43 +0300907 for_each_child_of_node(np, child)
Jon Huntera3804512016-03-30 10:15:15 +0100908 tegra_powergate_add(pmc, child);
Jon Huntera3804512016-03-30 10:15:15 +0100909
910 of_node_put(np);
911}
912
Laxman Dewangan21b49912016-10-10 15:14:34 +0200913static const struct tegra_io_pad_soc *
914tegra_io_pad_find(struct tegra_pmc *pmc, enum tegra_io_pad id)
Thierry Reding72323982014-07-11 13:19:06 +0200915{
Laxman Dewangan21b49912016-10-10 15:14:34 +0200916 unsigned int i;
917
918 for (i = 0; i < pmc->soc->num_io_pads; i++)
919 if (pmc->soc->io_pads[i].id == id)
920 return &pmc->soc->io_pads[i];
921
922 return NULL;
923}
924
Aapo Vienamo00ead3c2018-08-10 21:08:08 +0300925static int tegra_io_pad_get_dpd_register_bit(enum tegra_io_pad id,
926 unsigned long *request,
927 unsigned long *status,
928 u32 *mask)
Laxman Dewangan21b49912016-10-10 15:14:34 +0200929{
930 const struct tegra_io_pad_soc *pad;
Thierry Reding72323982014-07-11 13:19:06 +0200931
Laxman Dewangan21b49912016-10-10 15:14:34 +0200932 pad = tegra_io_pad_find(pmc, id);
Thierry Reding54e24722016-11-08 10:58:32 +0100933 if (!pad) {
934 pr_err("invalid I/O pad ID %u\n", id);
Laxman Dewangan21b49912016-10-10 15:14:34 +0200935 return -ENOENT;
Thierry Reding54e24722016-11-08 10:58:32 +0100936 }
Thierry Reding72323982014-07-11 13:19:06 +0200937
Laxman Dewangan21b49912016-10-10 15:14:34 +0200938 if (pad->dpd == UINT_MAX)
939 return -ENOTSUPP;
Thierry Reding72323982014-07-11 13:19:06 +0200940
Jon Hunter27b12b4e2016-10-22 20:23:53 +0100941 *mask = BIT(pad->dpd % 32);
Laxman Dewangan21b49912016-10-10 15:14:34 +0200942
943 if (pad->dpd < 32) {
Thierry Reding5be22552017-08-30 12:32:58 +0200944 *status = pmc->soc->regs->dpd_status;
945 *request = pmc->soc->regs->dpd_req;
Thierry Reding72323982014-07-11 13:19:06 +0200946 } else {
Thierry Reding5be22552017-08-30 12:32:58 +0200947 *status = pmc->soc->regs->dpd2_status;
948 *request = pmc->soc->regs->dpd2_req;
Thierry Reding72323982014-07-11 13:19:06 +0200949 }
950
Aapo Vienamo00ead3c2018-08-10 21:08:08 +0300951 return 0;
952}
953
954static int tegra_io_pad_prepare(enum tegra_io_pad id, unsigned long *request,
955 unsigned long *status, u32 *mask)
956{
957 unsigned long rate, value;
958 int err;
959
960 err = tegra_io_pad_get_dpd_register_bit(id, request, status, mask);
961 if (err)
962 return err;
963
Thierry Reding5be22552017-08-30 12:32:58 +0200964 if (pmc->clk) {
965 rate = clk_get_rate(pmc->clk);
966 if (!rate) {
967 pr_err("failed to get clock rate\n");
968 return -ENODEV;
969 }
970
971 tegra_pmc_writel(DPD_SAMPLE_ENABLE, DPD_SAMPLE);
972
973 /* must be at least 200 ns, in APB (PCLK) clock cycles */
974 value = DIV_ROUND_UP(1000000000, rate);
975 value = DIV_ROUND_UP(200, value);
976 tegra_pmc_writel(value, SEL_DPD_TIM);
Thierry Reding54e24722016-11-08 10:58:32 +0100977 }
Thierry Reding72323982014-07-11 13:19:06 +0200978
Thierry Reding72323982014-07-11 13:19:06 +0200979 return 0;
980}
981
Laxman Dewangan21b49912016-10-10 15:14:34 +0200982static int tegra_io_pad_poll(unsigned long offset, u32 mask,
983 u32 val, unsigned long timeout)
Thierry Reding72323982014-07-11 13:19:06 +0200984{
Laxman Dewangan84cf85e2016-06-17 18:36:13 +0530985 u32 value;
Thierry Reding72323982014-07-11 13:19:06 +0200986
987 timeout = jiffies + msecs_to_jiffies(timeout);
988
989 while (time_after(timeout, jiffies)) {
990 value = tegra_pmc_readl(offset);
991 if ((value & mask) == val)
992 return 0;
993
994 usleep_range(250, 1000);
995 }
996
997 return -ETIMEDOUT;
998}
999
Laxman Dewangan21b49912016-10-10 15:14:34 +02001000static void tegra_io_pad_unprepare(void)
Thierry Reding72323982014-07-11 13:19:06 +02001001{
Thierry Reding5be22552017-08-30 12:32:58 +02001002 if (pmc->clk)
1003 tegra_pmc_writel(DPD_SAMPLE_DISABLE, DPD_SAMPLE);
Thierry Reding72323982014-07-11 13:19:06 +02001004}
1005
Laxman Dewangan21b49912016-10-10 15:14:34 +02001006/**
1007 * tegra_io_pad_power_enable() - enable power to I/O pad
1008 * @id: Tegra I/O pad ID for which to enable power
1009 *
1010 * Returns: 0 on success or a negative error code on failure.
1011 */
1012int tegra_io_pad_power_enable(enum tegra_io_pad id)
Thierry Reding72323982014-07-11 13:19:06 +02001013{
Vince Hsua9ccc1232016-08-11 09:13:36 +08001014 unsigned long request, status;
Jon Hunter27b12b4e2016-10-22 20:23:53 +01001015 u32 mask;
Thierry Reding72323982014-07-11 13:19:06 +02001016 int err;
1017
Jon Huntere8cf6612016-02-11 18:03:21 +00001018 mutex_lock(&pmc->powergates_lock);
1019
Jon Hunter27b12b4e2016-10-22 20:23:53 +01001020 err = tegra_io_pad_prepare(id, &request, &status, &mask);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001021 if (err < 0) {
Thierry Reding54e24722016-11-08 10:58:32 +01001022 pr_err("failed to prepare I/O pad: %d\n", err);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001023 goto unlock;
1024 }
Thierry Reding72323982014-07-11 13:19:06 +02001025
Jon Hunter27b12b4e2016-10-22 20:23:53 +01001026 tegra_pmc_writel(IO_DPD_REQ_CODE_OFF | mask, request);
Thierry Reding72323982014-07-11 13:19:06 +02001027
Jon Hunter27b12b4e2016-10-22 20:23:53 +01001028 err = tegra_io_pad_poll(status, mask, 0, 250);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001029 if (err < 0) {
Thierry Reding54e24722016-11-08 10:58:32 +01001030 pr_err("failed to enable I/O pad: %d\n", err);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001031 goto unlock;
Thierry Reding592431b2015-08-04 15:25:03 +02001032 }
Thierry Reding72323982014-07-11 13:19:06 +02001033
Laxman Dewangan21b49912016-10-10 15:14:34 +02001034 tegra_io_pad_unprepare();
Thierry Reding72323982014-07-11 13:19:06 +02001035
Laxman Dewangan21b49912016-10-10 15:14:34 +02001036unlock:
Jon Huntere8cf6612016-02-11 18:03:21 +00001037 mutex_unlock(&pmc->powergates_lock);
Jon Huntere8cf6612016-02-11 18:03:21 +00001038 return err;
Thierry Reding72323982014-07-11 13:19:06 +02001039}
Laxman Dewangan21b49912016-10-10 15:14:34 +02001040EXPORT_SYMBOL(tegra_io_pad_power_enable);
Thierry Reding72323982014-07-11 13:19:06 +02001041
Laxman Dewangan21b49912016-10-10 15:14:34 +02001042/**
1043 * tegra_io_pad_power_disable() - disable power to I/O pad
1044 * @id: Tegra I/O pad ID for which to disable power
1045 *
1046 * Returns: 0 on success or a negative error code on failure.
1047 */
1048int tegra_io_pad_power_disable(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;
Thierry Reding592431b2015-08-04 15:25:03 +02001060 }
Thierry Reding72323982014-07-11 13:19:06 +02001061
Jon Hunter27b12b4e2016-10-22 20:23:53 +01001062 tegra_pmc_writel(IO_DPD_REQ_CODE_ON | 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, mask, 250);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001065 if (err < 0) {
Thierry Reding54e24722016-11-08 10:58:32 +01001066 pr_err("failed to disable I/O pad: %d\n", err);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001067 goto unlock;
1068 }
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:
1073 mutex_unlock(&pmc->powergates_lock);
1074 return err;
1075}
1076EXPORT_SYMBOL(tegra_io_pad_power_disable);
1077
1078int tegra_io_pad_set_voltage(enum tegra_io_pad id,
1079 enum tegra_io_pad_voltage voltage)
1080{
1081 const struct tegra_io_pad_soc *pad;
1082 u32 value;
1083
1084 pad = tegra_io_pad_find(pmc, id);
1085 if (!pad)
1086 return -ENOENT;
1087
1088 if (pad->voltage == UINT_MAX)
1089 return -ENOTSUPP;
1090
1091 mutex_lock(&pmc->powergates_lock);
1092
Aapo Vienamo13136a42018-08-10 21:08:07 +03001093 if (pmc->soc->has_impl_33v_pwr) {
1094 value = tegra_pmc_readl(PMC_IMPL_E_33V_PWR);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001095
Aapo Vienamo13136a42018-08-10 21:08:07 +03001096 if (voltage == TEGRA_IO_PAD_1800000UV)
1097 value &= ~BIT(pad->voltage);
1098 else
1099 value |= BIT(pad->voltage);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001100
Aapo Vienamo13136a42018-08-10 21:08:07 +03001101 tegra_pmc_writel(value, PMC_IMPL_E_33V_PWR);
1102 } else {
1103 /* write-enable PMC_PWR_DET_VALUE[pad->voltage] */
1104 value = tegra_pmc_readl(PMC_PWR_DET);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001105 value |= BIT(pad->voltage);
Aapo Vienamo13136a42018-08-10 21:08:07 +03001106 tegra_pmc_writel(value, PMC_PWR_DET);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001107
Aapo Vienamo13136a42018-08-10 21:08:07 +03001108 /* update I/O voltage */
1109 value = tegra_pmc_readl(PMC_PWR_DET_VALUE);
1110
1111 if (voltage == TEGRA_IO_PAD_1800000UV)
1112 value &= ~BIT(pad->voltage);
1113 else
1114 value |= BIT(pad->voltage);
1115
1116 tegra_pmc_writel(value, PMC_PWR_DET_VALUE);
1117 }
Laxman Dewangan21b49912016-10-10 15:14:34 +02001118
Jon Huntere8cf6612016-02-11 18:03:21 +00001119 mutex_unlock(&pmc->powergates_lock);
1120
Laxman Dewangan21b49912016-10-10 15:14:34 +02001121 usleep_range(100, 250);
1122
1123 return 0;
1124}
1125EXPORT_SYMBOL(tegra_io_pad_set_voltage);
1126
1127int tegra_io_pad_get_voltage(enum tegra_io_pad id)
1128{
1129 const struct tegra_io_pad_soc *pad;
1130 u32 value;
1131
1132 pad = tegra_io_pad_find(pmc, id);
1133 if (!pad)
1134 return -ENOENT;
1135
1136 if (pad->voltage == UINT_MAX)
1137 return -ENOTSUPP;
1138
Aapo Vienamo13136a42018-08-10 21:08:07 +03001139 if (pmc->soc->has_impl_33v_pwr)
1140 value = tegra_pmc_readl(PMC_IMPL_E_33V_PWR);
1141 else
1142 value = tegra_pmc_readl(PMC_PWR_DET_VALUE);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001143
1144 if ((value & BIT(pad->voltage)) == 0)
1145 return TEGRA_IO_PAD_1800000UV;
1146
1147 return TEGRA_IO_PAD_3300000UV;
1148}
1149EXPORT_SYMBOL(tegra_io_pad_get_voltage);
1150
1151/**
1152 * tegra_io_rail_power_on() - enable power to I/O rail
1153 * @id: Tegra I/O pad ID for which to enable power
1154 *
1155 * See also: tegra_io_pad_power_enable()
1156 */
1157int tegra_io_rail_power_on(unsigned int id)
1158{
1159 return tegra_io_pad_power_enable(id);
1160}
1161EXPORT_SYMBOL(tegra_io_rail_power_on);
1162
1163/**
1164 * tegra_io_rail_power_off() - disable power to I/O rail
1165 * @id: Tegra I/O pad ID for which to disable power
1166 *
1167 * See also: tegra_io_pad_power_disable()
1168 */
1169int tegra_io_rail_power_off(unsigned int id)
1170{
1171 return tegra_io_pad_power_disable(id);
Thierry Reding72323982014-07-11 13:19:06 +02001172}
1173EXPORT_SYMBOL(tegra_io_rail_power_off);
1174
1175#ifdef CONFIG_PM_SLEEP
1176enum tegra_suspend_mode tegra_pmc_get_suspend_mode(void)
1177{
1178 return pmc->suspend_mode;
1179}
1180
1181void tegra_pmc_set_suspend_mode(enum tegra_suspend_mode mode)
1182{
1183 if (mode < TEGRA_SUSPEND_NONE || mode >= TEGRA_MAX_SUSPEND_MODE)
1184 return;
1185
1186 pmc->suspend_mode = mode;
1187}
1188
1189void tegra_pmc_enter_suspend_mode(enum tegra_suspend_mode mode)
1190{
1191 unsigned long long rate = 0;
1192 u32 value;
1193
1194 switch (mode) {
1195 case TEGRA_SUSPEND_LP1:
1196 rate = 32768;
1197 break;
1198
1199 case TEGRA_SUSPEND_LP2:
1200 rate = clk_get_rate(pmc->clk);
1201 break;
1202
1203 default:
1204 break;
1205 }
1206
1207 if (WARN_ON_ONCE(rate == 0))
1208 rate = 100000000;
1209
1210 if (rate != pmc->rate) {
1211 u64 ticks;
1212
1213 ticks = pmc->cpu_good_time * rate + USEC_PER_SEC - 1;
1214 do_div(ticks, USEC_PER_SEC);
1215 tegra_pmc_writel(ticks, PMC_CPUPWRGOOD_TIMER);
1216
1217 ticks = pmc->cpu_off_time * rate + USEC_PER_SEC - 1;
1218 do_div(ticks, USEC_PER_SEC);
1219 tegra_pmc_writel(ticks, PMC_CPUPWROFF_TIMER);
1220
1221 wmb();
1222
1223 pmc->rate = rate;
1224 }
1225
1226 value = tegra_pmc_readl(PMC_CNTRL);
1227 value &= ~PMC_CNTRL_SIDE_EFFECT_LP0;
1228 value |= PMC_CNTRL_CPU_PWRREQ_OE;
1229 tegra_pmc_writel(value, PMC_CNTRL);
1230}
1231#endif
1232
1233static int tegra_pmc_parse_dt(struct tegra_pmc *pmc, struct device_node *np)
1234{
1235 u32 value, values[2];
1236
1237 if (of_property_read_u32(np, "nvidia,suspend-mode", &value)) {
1238 } else {
1239 switch (value) {
1240 case 0:
1241 pmc->suspend_mode = TEGRA_SUSPEND_LP0;
1242 break;
1243
1244 case 1:
1245 pmc->suspend_mode = TEGRA_SUSPEND_LP1;
1246 break;
1247
1248 case 2:
1249 pmc->suspend_mode = TEGRA_SUSPEND_LP2;
1250 break;
1251
1252 default:
1253 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1254 break;
1255 }
1256 }
1257
1258 pmc->suspend_mode = tegra_pm_validate_suspend_mode(pmc->suspend_mode);
1259
1260 if (of_property_read_u32(np, "nvidia,cpu-pwr-good-time", &value))
1261 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1262
1263 pmc->cpu_good_time = value;
1264
1265 if (of_property_read_u32(np, "nvidia,cpu-pwr-off-time", &value))
1266 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1267
1268 pmc->cpu_off_time = value;
1269
1270 if (of_property_read_u32_array(np, "nvidia,core-pwr-good-time",
1271 values, ARRAY_SIZE(values)))
1272 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1273
1274 pmc->core_osc_time = values[0];
1275 pmc->core_pmu_time = values[1];
1276
1277 if (of_property_read_u32(np, "nvidia,core-pwr-off-time", &value))
1278 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1279
1280 pmc->core_off_time = value;
1281
1282 pmc->corereq_high = of_property_read_bool(np,
1283 "nvidia,core-power-req-active-high");
1284
1285 pmc->sysclkreq_high = of_property_read_bool(np,
1286 "nvidia,sys-clock-req-active-high");
1287
1288 pmc->combined_req = of_property_read_bool(np,
1289 "nvidia,combined-power-req");
1290
1291 pmc->cpu_pwr_good_en = of_property_read_bool(np,
1292 "nvidia,cpu-pwr-good-en");
1293
1294 if (of_property_read_u32_array(np, "nvidia,lp0-vec", values,
1295 ARRAY_SIZE(values)))
1296 if (pmc->suspend_mode == TEGRA_SUSPEND_LP0)
1297 pmc->suspend_mode = TEGRA_SUSPEND_LP1;
1298
1299 pmc->lp0_vec_phys = values[0];
1300 pmc->lp0_vec_size = values[1];
1301
1302 return 0;
1303}
1304
1305static void tegra_pmc_init(struct tegra_pmc *pmc)
1306{
Thierry Reding5be22552017-08-30 12:32:58 +02001307 if (pmc->soc->init)
1308 pmc->soc->init(pmc);
Thierry Reding72323982014-07-11 13:19:06 +02001309}
1310
Jon Hunter1e52efdf2015-12-04 14:57:04 +00001311static void tegra_pmc_init_tsense_reset(struct tegra_pmc *pmc)
Mikko Perttunen3568df32015-01-06 12:52:58 +02001312{
1313 static const char disabled[] = "emergency thermal reset disabled";
1314 u32 pmu_addr, ctrl_id, reg_addr, reg_data, pinmux;
1315 struct device *dev = pmc->dev;
1316 struct device_node *np;
1317 u32 value, checksum;
1318
1319 if (!pmc->soc->has_tsense_reset)
Thierry Reding95169cd2015-07-09 09:59:55 +02001320 return;
Mikko Perttunen3568df32015-01-06 12:52:58 +02001321
Johan Hovold1dc6bd52017-11-15 10:44:58 +01001322 np = of_get_child_by_name(pmc->dev->of_node, "i2c-thermtrip");
Mikko Perttunen3568df32015-01-06 12:52:58 +02001323 if (!np) {
1324 dev_warn(dev, "i2c-thermtrip node not found, %s.\n", disabled);
Thierry Reding95169cd2015-07-09 09:59:55 +02001325 return;
Mikko Perttunen3568df32015-01-06 12:52:58 +02001326 }
1327
1328 if (of_property_read_u32(np, "nvidia,i2c-controller-id", &ctrl_id)) {
1329 dev_err(dev, "I2C controller ID missing, %s.\n", disabled);
1330 goto out;
1331 }
1332
1333 if (of_property_read_u32(np, "nvidia,bus-addr", &pmu_addr)) {
1334 dev_err(dev, "nvidia,bus-addr missing, %s.\n", disabled);
1335 goto out;
1336 }
1337
1338 if (of_property_read_u32(np, "nvidia,reg-addr", &reg_addr)) {
1339 dev_err(dev, "nvidia,reg-addr missing, %s.\n", disabled);
1340 goto out;
1341 }
1342
1343 if (of_property_read_u32(np, "nvidia,reg-data", &reg_data)) {
1344 dev_err(dev, "nvidia,reg-data missing, %s.\n", disabled);
1345 goto out;
1346 }
1347
1348 if (of_property_read_u32(np, "nvidia,pinmux-id", &pinmux))
1349 pinmux = 0;
1350
1351 value = tegra_pmc_readl(PMC_SENSOR_CTRL);
1352 value |= PMC_SENSOR_CTRL_SCRATCH_WRITE;
1353 tegra_pmc_writel(value, PMC_SENSOR_CTRL);
1354
1355 value = (reg_data << PMC_SCRATCH54_DATA_SHIFT) |
1356 (reg_addr << PMC_SCRATCH54_ADDR_SHIFT);
1357 tegra_pmc_writel(value, PMC_SCRATCH54);
1358
1359 value = PMC_SCRATCH55_RESET_TEGRA;
1360 value |= ctrl_id << PMC_SCRATCH55_CNTRL_ID_SHIFT;
1361 value |= pinmux << PMC_SCRATCH55_PINMUX_SHIFT;
1362 value |= pmu_addr << PMC_SCRATCH55_I2CSLV1_SHIFT;
1363
1364 /*
1365 * Calculate checksum of SCRATCH54, SCRATCH55 fields. Bits 23:16 will
1366 * contain the checksum and are currently zero, so they are not added.
1367 */
1368 checksum = reg_addr + reg_data + (value & 0xff) + ((value >> 8) & 0xff)
1369 + ((value >> 24) & 0xff);
1370 checksum &= 0xff;
1371 checksum = 0x100 - checksum;
1372
1373 value |= checksum << PMC_SCRATCH55_CHECKSUM_SHIFT;
1374
1375 tegra_pmc_writel(value, PMC_SCRATCH55);
1376
1377 value = tegra_pmc_readl(PMC_SENSOR_CTRL);
1378 value |= PMC_SENSOR_CTRL_ENABLE_RST;
1379 tegra_pmc_writel(value, PMC_SENSOR_CTRL);
1380
1381 dev_info(pmc->dev, "emergency thermal reset enabled\n");
1382
1383out:
1384 of_node_put(np);
Mikko Perttunen3568df32015-01-06 12:52:58 +02001385}
1386
Thierry Reding72323982014-07-11 13:19:06 +02001387static int tegra_pmc_probe(struct platform_device *pdev)
1388{
Jon Huntere8cf6612016-02-11 18:03:21 +00001389 void __iomem *base;
Thierry Reding72323982014-07-11 13:19:06 +02001390 struct resource *res;
1391 int err;
1392
Jon Huntera83f1fc2016-06-28 11:38:28 +01001393 /*
1394 * Early initialisation should have configured an initial
1395 * register mapping and setup the soc data pointer. If these
1396 * are not valid then something went badly wrong!
1397 */
1398 if (WARN_ON(!pmc->base || !pmc->soc))
1399 return -ENODEV;
1400
Thierry Reding72323982014-07-11 13:19:06 +02001401 err = tegra_pmc_parse_dt(pmc, pdev->dev.of_node);
1402 if (err < 0)
1403 return err;
1404
1405 /* take over the memory region from the early initialization */
1406 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Jon Hunter0259f522016-02-11 18:03:20 +00001407 base = devm_ioremap_resource(&pdev->dev, res);
1408 if (IS_ERR(base))
1409 return PTR_ERR(base);
Thierry Reding72323982014-07-11 13:19:06 +02001410
Thierry Redingc641ec62017-08-30 12:42:34 +02001411 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "wake");
1412 if (res) {
1413 pmc->wake = devm_ioremap_resource(&pdev->dev, res);
1414 if (IS_ERR(pmc->wake))
1415 return PTR_ERR(pmc->wake);
1416 } else {
1417 pmc->wake = base;
1418 }
1419
1420 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "aotag");
1421 if (res) {
1422 pmc->aotag = devm_ioremap_resource(&pdev->dev, res);
1423 if (IS_ERR(pmc->aotag))
1424 return PTR_ERR(pmc->aotag);
1425 } else {
1426 pmc->aotag = base;
1427 }
1428
1429 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "scratch");
1430 if (res) {
1431 pmc->scratch = devm_ioremap_resource(&pdev->dev, res);
1432 if (IS_ERR(pmc->scratch))
1433 return PTR_ERR(pmc->scratch);
1434 } else {
1435 pmc->scratch = base;
1436 }
Thierry Reding5be22552017-08-30 12:32:58 +02001437
Thierry Reding72323982014-07-11 13:19:06 +02001438 pmc->clk = devm_clk_get(&pdev->dev, "pclk");
1439 if (IS_ERR(pmc->clk)) {
1440 err = PTR_ERR(pmc->clk);
Thierry Reding5be22552017-08-30 12:32:58 +02001441
1442 if (err != -ENOENT) {
1443 dev_err(&pdev->dev, "failed to get pclk: %d\n", err);
1444 return err;
1445 }
1446
1447 pmc->clk = NULL;
Thierry Reding72323982014-07-11 13:19:06 +02001448 }
1449
Mikko Perttunen3568df32015-01-06 12:52:58 +02001450 pmc->dev = &pdev->dev;
1451
Thierry Reding72323982014-07-11 13:19:06 +02001452 tegra_pmc_init(pmc);
1453
Mikko Perttunen3568df32015-01-06 12:52:58 +02001454 tegra_pmc_init_tsense_reset(pmc);
1455
Thierry Reding72323982014-07-11 13:19:06 +02001456 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
1457 err = tegra_powergate_debugfs_init();
1458 if (err < 0)
1459 return err;
1460 }
1461
David Riley78921582015-03-18 10:52:25 +01001462 err = register_restart_handler(&tegra_pmc_restart_handler);
1463 if (err) {
Jon Hunter3195ac62015-12-04 14:57:05 +00001464 debugfs_remove(pmc->debugfs);
David Riley78921582015-03-18 10:52:25 +01001465 dev_err(&pdev->dev, "unable to register restart handler, %d\n",
1466 err);
1467 return err;
1468 }
1469
Jon Huntere8cf6612016-02-11 18:03:21 +00001470 mutex_lock(&pmc->powergates_lock);
1471 iounmap(pmc->base);
Jon Hunter0259f522016-02-11 18:03:20 +00001472 pmc->base = base;
Jon Huntere8cf6612016-02-11 18:03:21 +00001473 mutex_unlock(&pmc->powergates_lock);
Jon Hunter0259f522016-02-11 18:03:20 +00001474
Thierry Reding72323982014-07-11 13:19:06 +02001475 return 0;
1476}
1477
Paul Walmsley2b20b612014-12-09 22:36:50 +00001478#if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM)
Thierry Reding72323982014-07-11 13:19:06 +02001479static int tegra_pmc_suspend(struct device *dev)
1480{
1481 tegra_pmc_writel(virt_to_phys(tegra_resume), PMC_SCRATCH41);
1482
1483 return 0;
1484}
1485
1486static int tegra_pmc_resume(struct device *dev)
1487{
1488 tegra_pmc_writel(0x0, PMC_SCRATCH41);
1489
1490 return 0;
1491}
Thierry Reding72323982014-07-11 13:19:06 +02001492
1493static SIMPLE_DEV_PM_OPS(tegra_pmc_pm_ops, tegra_pmc_suspend, tegra_pmc_resume);
1494
Paul Walmsley2b20b612014-12-09 22:36:50 +00001495#endif
1496
Thierry Reding72323982014-07-11 13:19:06 +02001497static const char * const tegra20_powergates[] = {
1498 [TEGRA_POWERGATE_CPU] = "cpu",
1499 [TEGRA_POWERGATE_3D] = "3d",
1500 [TEGRA_POWERGATE_VENC] = "venc",
1501 [TEGRA_POWERGATE_VDEC] = "vdec",
1502 [TEGRA_POWERGATE_PCIE] = "pcie",
1503 [TEGRA_POWERGATE_L2] = "l2",
1504 [TEGRA_POWERGATE_MPE] = "mpe",
1505};
1506
Thierry Reding5be22552017-08-30 12:32:58 +02001507static const struct tegra_pmc_regs tegra20_pmc_regs = {
1508 .scratch0 = 0x50,
1509 .dpd_req = 0x1b8,
1510 .dpd_status = 0x1bc,
1511 .dpd2_req = 0x1c0,
1512 .dpd2_status = 0x1c4,
1513};
1514
1515static void tegra20_pmc_init(struct tegra_pmc *pmc)
1516{
1517 u32 value;
1518
1519 /* Always enable CPU power request */
1520 value = tegra_pmc_readl(PMC_CNTRL);
1521 value |= PMC_CNTRL_CPU_PWRREQ_OE;
1522 tegra_pmc_writel(value, PMC_CNTRL);
1523
1524 value = tegra_pmc_readl(PMC_CNTRL);
1525
1526 if (pmc->sysclkreq_high)
1527 value &= ~PMC_CNTRL_SYSCLK_POLARITY;
1528 else
1529 value |= PMC_CNTRL_SYSCLK_POLARITY;
1530
1531 /* configure the output polarity while the request is tristated */
1532 tegra_pmc_writel(value, PMC_CNTRL);
1533
1534 /* now enable the request */
1535 value = tegra_pmc_readl(PMC_CNTRL);
1536 value |= PMC_CNTRL_SYSCLK_OE;
1537 tegra_pmc_writel(value, PMC_CNTRL);
1538}
1539
1540static void tegra20_pmc_setup_irq_polarity(struct tegra_pmc *pmc,
1541 struct device_node *np,
1542 bool invert)
1543{
1544 u32 value;
1545
1546 value = tegra_pmc_readl(PMC_CNTRL);
1547
1548 if (invert)
1549 value |= PMC_CNTRL_INTR_POLARITY;
1550 else
1551 value &= ~PMC_CNTRL_INTR_POLARITY;
1552
1553 tegra_pmc_writel(value, PMC_CNTRL);
1554}
1555
Thierry Reding72323982014-07-11 13:19:06 +02001556static const struct tegra_pmc_soc tegra20_pmc_soc = {
1557 .num_powergates = ARRAY_SIZE(tegra20_powergates),
1558 .powergates = tegra20_powergates,
1559 .num_cpu_powergates = 0,
1560 .cpu_powergates = NULL,
Mikko Perttunen3568df32015-01-06 12:52:58 +02001561 .has_tsense_reset = false,
Thierry Redinga9a40a42015-01-09 11:15:33 +01001562 .has_gpu_clamps = false,
Thierry Reding5be22552017-08-30 12:32:58 +02001563 .num_io_pads = 0,
1564 .io_pads = NULL,
1565 .regs = &tegra20_pmc_regs,
1566 .init = tegra20_pmc_init,
1567 .setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
Thierry Reding72323982014-07-11 13:19:06 +02001568};
1569
1570static const char * const tegra30_powergates[] = {
1571 [TEGRA_POWERGATE_CPU] = "cpu0",
1572 [TEGRA_POWERGATE_3D] = "3d0",
1573 [TEGRA_POWERGATE_VENC] = "venc",
1574 [TEGRA_POWERGATE_VDEC] = "vdec",
1575 [TEGRA_POWERGATE_PCIE] = "pcie",
1576 [TEGRA_POWERGATE_L2] = "l2",
1577 [TEGRA_POWERGATE_MPE] = "mpe",
1578 [TEGRA_POWERGATE_HEG] = "heg",
1579 [TEGRA_POWERGATE_SATA] = "sata",
1580 [TEGRA_POWERGATE_CPU1] = "cpu1",
1581 [TEGRA_POWERGATE_CPU2] = "cpu2",
1582 [TEGRA_POWERGATE_CPU3] = "cpu3",
1583 [TEGRA_POWERGATE_CELP] = "celp",
1584 [TEGRA_POWERGATE_3D1] = "3d1",
1585};
1586
1587static const u8 tegra30_cpu_powergates[] = {
1588 TEGRA_POWERGATE_CPU,
1589 TEGRA_POWERGATE_CPU1,
1590 TEGRA_POWERGATE_CPU2,
1591 TEGRA_POWERGATE_CPU3,
1592};
1593
1594static const struct tegra_pmc_soc tegra30_pmc_soc = {
1595 .num_powergates = ARRAY_SIZE(tegra30_powergates),
1596 .powergates = tegra30_powergates,
1597 .num_cpu_powergates = ARRAY_SIZE(tegra30_cpu_powergates),
1598 .cpu_powergates = tegra30_cpu_powergates,
Mikko Perttunen3568df32015-01-06 12:52:58 +02001599 .has_tsense_reset = true,
Thierry Redinga9a40a42015-01-09 11:15:33 +01001600 .has_gpu_clamps = false,
Aapo Vienamo13136a42018-08-10 21:08:07 +03001601 .has_impl_33v_pwr = false,
Thierry Reding5be22552017-08-30 12:32:58 +02001602 .num_io_pads = 0,
1603 .io_pads = NULL,
1604 .regs = &tegra20_pmc_regs,
1605 .init = tegra20_pmc_init,
1606 .setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
Thierry Reding72323982014-07-11 13:19:06 +02001607};
1608
1609static const char * const tegra114_powergates[] = {
1610 [TEGRA_POWERGATE_CPU] = "crail",
1611 [TEGRA_POWERGATE_3D] = "3d",
1612 [TEGRA_POWERGATE_VENC] = "venc",
1613 [TEGRA_POWERGATE_VDEC] = "vdec",
1614 [TEGRA_POWERGATE_MPE] = "mpe",
1615 [TEGRA_POWERGATE_HEG] = "heg",
1616 [TEGRA_POWERGATE_CPU1] = "cpu1",
1617 [TEGRA_POWERGATE_CPU2] = "cpu2",
1618 [TEGRA_POWERGATE_CPU3] = "cpu3",
1619 [TEGRA_POWERGATE_CELP] = "celp",
1620 [TEGRA_POWERGATE_CPU0] = "cpu0",
1621 [TEGRA_POWERGATE_C0NC] = "c0nc",
1622 [TEGRA_POWERGATE_C1NC] = "c1nc",
1623 [TEGRA_POWERGATE_DIS] = "dis",
1624 [TEGRA_POWERGATE_DISB] = "disb",
1625 [TEGRA_POWERGATE_XUSBA] = "xusba",
1626 [TEGRA_POWERGATE_XUSBB] = "xusbb",
1627 [TEGRA_POWERGATE_XUSBC] = "xusbc",
1628};
1629
1630static const u8 tegra114_cpu_powergates[] = {
1631 TEGRA_POWERGATE_CPU0,
1632 TEGRA_POWERGATE_CPU1,
1633 TEGRA_POWERGATE_CPU2,
1634 TEGRA_POWERGATE_CPU3,
1635};
1636
1637static const struct tegra_pmc_soc tegra114_pmc_soc = {
1638 .num_powergates = ARRAY_SIZE(tegra114_powergates),
1639 .powergates = tegra114_powergates,
1640 .num_cpu_powergates = ARRAY_SIZE(tegra114_cpu_powergates),
1641 .cpu_powergates = tegra114_cpu_powergates,
Mikko Perttunen3568df32015-01-06 12:52:58 +02001642 .has_tsense_reset = true,
Thierry Redinga9a40a42015-01-09 11:15:33 +01001643 .has_gpu_clamps = false,
Aapo Vienamo13136a42018-08-10 21:08:07 +03001644 .has_impl_33v_pwr = false,
Thierry Reding5be22552017-08-30 12:32:58 +02001645 .num_io_pads = 0,
1646 .io_pads = NULL,
1647 .regs = &tegra20_pmc_regs,
1648 .init = tegra20_pmc_init,
1649 .setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
Thierry Reding72323982014-07-11 13:19:06 +02001650};
1651
1652static const char * const tegra124_powergates[] = {
1653 [TEGRA_POWERGATE_CPU] = "crail",
1654 [TEGRA_POWERGATE_3D] = "3d",
1655 [TEGRA_POWERGATE_VENC] = "venc",
1656 [TEGRA_POWERGATE_PCIE] = "pcie",
1657 [TEGRA_POWERGATE_VDEC] = "vdec",
Thierry Reding72323982014-07-11 13:19:06 +02001658 [TEGRA_POWERGATE_MPE] = "mpe",
1659 [TEGRA_POWERGATE_HEG] = "heg",
1660 [TEGRA_POWERGATE_SATA] = "sata",
1661 [TEGRA_POWERGATE_CPU1] = "cpu1",
1662 [TEGRA_POWERGATE_CPU2] = "cpu2",
1663 [TEGRA_POWERGATE_CPU3] = "cpu3",
1664 [TEGRA_POWERGATE_CELP] = "celp",
1665 [TEGRA_POWERGATE_CPU0] = "cpu0",
1666 [TEGRA_POWERGATE_C0NC] = "c0nc",
1667 [TEGRA_POWERGATE_C1NC] = "c1nc",
1668 [TEGRA_POWERGATE_SOR] = "sor",
1669 [TEGRA_POWERGATE_DIS] = "dis",
1670 [TEGRA_POWERGATE_DISB] = "disb",
1671 [TEGRA_POWERGATE_XUSBA] = "xusba",
1672 [TEGRA_POWERGATE_XUSBB] = "xusbb",
1673 [TEGRA_POWERGATE_XUSBC] = "xusbc",
1674 [TEGRA_POWERGATE_VIC] = "vic",
1675 [TEGRA_POWERGATE_IRAM] = "iram",
1676};
1677
1678static const u8 tegra124_cpu_powergates[] = {
1679 TEGRA_POWERGATE_CPU0,
1680 TEGRA_POWERGATE_CPU1,
1681 TEGRA_POWERGATE_CPU2,
1682 TEGRA_POWERGATE_CPU3,
1683};
1684
Laxman Dewangan21b49912016-10-10 15:14:34 +02001685static const struct tegra_io_pad_soc tegra124_io_pads[] = {
1686 { .id = TEGRA_IO_PAD_AUDIO, .dpd = 17, .voltage = UINT_MAX },
1687 { .id = TEGRA_IO_PAD_BB, .dpd = 15, .voltage = UINT_MAX },
1688 { .id = TEGRA_IO_PAD_CAM, .dpd = 36, .voltage = UINT_MAX },
1689 { .id = TEGRA_IO_PAD_COMP, .dpd = 22, .voltage = UINT_MAX },
1690 { .id = TEGRA_IO_PAD_CSIA, .dpd = 0, .voltage = UINT_MAX },
1691 { .id = TEGRA_IO_PAD_CSIB, .dpd = 1, .voltage = UINT_MAX },
1692 { .id = TEGRA_IO_PAD_CSIE, .dpd = 44, .voltage = UINT_MAX },
1693 { .id = TEGRA_IO_PAD_DSI, .dpd = 2, .voltage = UINT_MAX },
1694 { .id = TEGRA_IO_PAD_DSIB, .dpd = 39, .voltage = UINT_MAX },
1695 { .id = TEGRA_IO_PAD_DSIC, .dpd = 40, .voltage = UINT_MAX },
1696 { .id = TEGRA_IO_PAD_DSID, .dpd = 41, .voltage = UINT_MAX },
1697 { .id = TEGRA_IO_PAD_HDMI, .dpd = 28, .voltage = UINT_MAX },
1698 { .id = TEGRA_IO_PAD_HSIC, .dpd = 19, .voltage = UINT_MAX },
1699 { .id = TEGRA_IO_PAD_HV, .dpd = 38, .voltage = UINT_MAX },
1700 { .id = TEGRA_IO_PAD_LVDS, .dpd = 57, .voltage = UINT_MAX },
1701 { .id = TEGRA_IO_PAD_MIPI_BIAS, .dpd = 3, .voltage = UINT_MAX },
1702 { .id = TEGRA_IO_PAD_NAND, .dpd = 13, .voltage = UINT_MAX },
1703 { .id = TEGRA_IO_PAD_PEX_BIAS, .dpd = 4, .voltage = UINT_MAX },
1704 { .id = TEGRA_IO_PAD_PEX_CLK1, .dpd = 5, .voltage = UINT_MAX },
1705 { .id = TEGRA_IO_PAD_PEX_CLK2, .dpd = 6, .voltage = UINT_MAX },
1706 { .id = TEGRA_IO_PAD_PEX_CNTRL, .dpd = 32, .voltage = UINT_MAX },
1707 { .id = TEGRA_IO_PAD_SDMMC1, .dpd = 33, .voltage = UINT_MAX },
1708 { .id = TEGRA_IO_PAD_SDMMC3, .dpd = 34, .voltage = UINT_MAX },
1709 { .id = TEGRA_IO_PAD_SDMMC4, .dpd = 35, .voltage = UINT_MAX },
1710 { .id = TEGRA_IO_PAD_SYS_DDC, .dpd = 58, .voltage = UINT_MAX },
1711 { .id = TEGRA_IO_PAD_UART, .dpd = 14, .voltage = UINT_MAX },
1712 { .id = TEGRA_IO_PAD_USB0, .dpd = 9, .voltage = UINT_MAX },
1713 { .id = TEGRA_IO_PAD_USB1, .dpd = 10, .voltage = UINT_MAX },
1714 { .id = TEGRA_IO_PAD_USB2, .dpd = 11, .voltage = UINT_MAX },
1715 { .id = TEGRA_IO_PAD_USB_BIAS, .dpd = 12, .voltage = UINT_MAX },
1716};
1717
Thierry Reding72323982014-07-11 13:19:06 +02001718static const struct tegra_pmc_soc tegra124_pmc_soc = {
1719 .num_powergates = ARRAY_SIZE(tegra124_powergates),
1720 .powergates = tegra124_powergates,
1721 .num_cpu_powergates = ARRAY_SIZE(tegra124_cpu_powergates),
1722 .cpu_powergates = tegra124_cpu_powergates,
Mikko Perttunen3568df32015-01-06 12:52:58 +02001723 .has_tsense_reset = true,
Thierry Redinga9a40a42015-01-09 11:15:33 +01001724 .has_gpu_clamps = true,
Aapo Vienamo13136a42018-08-10 21:08:07 +03001725 .has_impl_33v_pwr = false,
Laxman Dewangan21b49912016-10-10 15:14:34 +02001726 .num_io_pads = ARRAY_SIZE(tegra124_io_pads),
1727 .io_pads = tegra124_io_pads,
Thierry Reding5be22552017-08-30 12:32:58 +02001728 .regs = &tegra20_pmc_regs,
1729 .init = tegra20_pmc_init,
1730 .setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
Thierry Reding72323982014-07-11 13:19:06 +02001731};
1732
Thierry Redingc2fe4692015-03-23 11:31:29 +01001733static const char * const tegra210_powergates[] = {
1734 [TEGRA_POWERGATE_CPU] = "crail",
1735 [TEGRA_POWERGATE_3D] = "3d",
1736 [TEGRA_POWERGATE_VENC] = "venc",
1737 [TEGRA_POWERGATE_PCIE] = "pcie",
Thierry Redingc2fe4692015-03-23 11:31:29 +01001738 [TEGRA_POWERGATE_MPE] = "mpe",
Thierry Redingc2fe4692015-03-23 11:31:29 +01001739 [TEGRA_POWERGATE_SATA] = "sata",
1740 [TEGRA_POWERGATE_CPU1] = "cpu1",
1741 [TEGRA_POWERGATE_CPU2] = "cpu2",
1742 [TEGRA_POWERGATE_CPU3] = "cpu3",
Thierry Redingc2fe4692015-03-23 11:31:29 +01001743 [TEGRA_POWERGATE_CPU0] = "cpu0",
1744 [TEGRA_POWERGATE_C0NC] = "c0nc",
Thierry Redingc2fe4692015-03-23 11:31:29 +01001745 [TEGRA_POWERGATE_SOR] = "sor",
1746 [TEGRA_POWERGATE_DIS] = "dis",
1747 [TEGRA_POWERGATE_DISB] = "disb",
1748 [TEGRA_POWERGATE_XUSBA] = "xusba",
1749 [TEGRA_POWERGATE_XUSBB] = "xusbb",
1750 [TEGRA_POWERGATE_XUSBC] = "xusbc",
1751 [TEGRA_POWERGATE_VIC] = "vic",
1752 [TEGRA_POWERGATE_IRAM] = "iram",
1753 [TEGRA_POWERGATE_NVDEC] = "nvdec",
1754 [TEGRA_POWERGATE_NVJPG] = "nvjpg",
1755 [TEGRA_POWERGATE_AUD] = "aud",
1756 [TEGRA_POWERGATE_DFD] = "dfd",
1757 [TEGRA_POWERGATE_VE2] = "ve2",
1758};
1759
1760static const u8 tegra210_cpu_powergates[] = {
1761 TEGRA_POWERGATE_CPU0,
1762 TEGRA_POWERGATE_CPU1,
1763 TEGRA_POWERGATE_CPU2,
1764 TEGRA_POWERGATE_CPU3,
1765};
1766
Laxman Dewangan21b49912016-10-10 15:14:34 +02001767static const struct tegra_io_pad_soc tegra210_io_pads[] = {
1768 { .id = TEGRA_IO_PAD_AUDIO, .dpd = 17, .voltage = 5 },
1769 { .id = TEGRA_IO_PAD_AUDIO_HV, .dpd = 61, .voltage = 18 },
1770 { .id = TEGRA_IO_PAD_CAM, .dpd = 36, .voltage = 10 },
1771 { .id = TEGRA_IO_PAD_CSIA, .dpd = 0, .voltage = UINT_MAX },
1772 { .id = TEGRA_IO_PAD_CSIB, .dpd = 1, .voltage = UINT_MAX },
1773 { .id = TEGRA_IO_PAD_CSIC, .dpd = 42, .voltage = UINT_MAX },
1774 { .id = TEGRA_IO_PAD_CSID, .dpd = 43, .voltage = UINT_MAX },
1775 { .id = TEGRA_IO_PAD_CSIE, .dpd = 44, .voltage = UINT_MAX },
1776 { .id = TEGRA_IO_PAD_CSIF, .dpd = 45, .voltage = UINT_MAX },
1777 { .id = TEGRA_IO_PAD_DBG, .dpd = 25, .voltage = 19 },
1778 { .id = TEGRA_IO_PAD_DEBUG_NONAO, .dpd = 26, .voltage = UINT_MAX },
1779 { .id = TEGRA_IO_PAD_DMIC, .dpd = 50, .voltage = 20 },
1780 { .id = TEGRA_IO_PAD_DP, .dpd = 51, .voltage = UINT_MAX },
1781 { .id = TEGRA_IO_PAD_DSI, .dpd = 2, .voltage = UINT_MAX },
1782 { .id = TEGRA_IO_PAD_DSIB, .dpd = 39, .voltage = UINT_MAX },
1783 { .id = TEGRA_IO_PAD_DSIC, .dpd = 40, .voltage = UINT_MAX },
1784 { .id = TEGRA_IO_PAD_DSID, .dpd = 41, .voltage = UINT_MAX },
1785 { .id = TEGRA_IO_PAD_EMMC, .dpd = 35, .voltage = UINT_MAX },
1786 { .id = TEGRA_IO_PAD_EMMC2, .dpd = 37, .voltage = UINT_MAX },
1787 { .id = TEGRA_IO_PAD_GPIO, .dpd = 27, .voltage = 21 },
1788 { .id = TEGRA_IO_PAD_HDMI, .dpd = 28, .voltage = UINT_MAX },
1789 { .id = TEGRA_IO_PAD_HSIC, .dpd = 19, .voltage = UINT_MAX },
1790 { .id = TEGRA_IO_PAD_LVDS, .dpd = 57, .voltage = UINT_MAX },
1791 { .id = TEGRA_IO_PAD_MIPI_BIAS, .dpd = 3, .voltage = UINT_MAX },
1792 { .id = TEGRA_IO_PAD_PEX_BIAS, .dpd = 4, .voltage = UINT_MAX },
1793 { .id = TEGRA_IO_PAD_PEX_CLK1, .dpd = 5, .voltage = UINT_MAX },
1794 { .id = TEGRA_IO_PAD_PEX_CLK2, .dpd = 6, .voltage = UINT_MAX },
1795 { .id = TEGRA_IO_PAD_PEX_CNTRL, .dpd = UINT_MAX, .voltage = 11 },
1796 { .id = TEGRA_IO_PAD_SDMMC1, .dpd = 33, .voltage = 12 },
1797 { .id = TEGRA_IO_PAD_SDMMC3, .dpd = 34, .voltage = 13 },
1798 { .id = TEGRA_IO_PAD_SPI, .dpd = 46, .voltage = 22 },
1799 { .id = TEGRA_IO_PAD_SPI_HV, .dpd = 47, .voltage = 23 },
1800 { .id = TEGRA_IO_PAD_UART, .dpd = 14, .voltage = 2 },
1801 { .id = TEGRA_IO_PAD_USB0, .dpd = 9, .voltage = UINT_MAX },
1802 { .id = TEGRA_IO_PAD_USB1, .dpd = 10, .voltage = UINT_MAX },
1803 { .id = TEGRA_IO_PAD_USB2, .dpd = 11, .voltage = UINT_MAX },
1804 { .id = TEGRA_IO_PAD_USB3, .dpd = 18, .voltage = UINT_MAX },
1805 { .id = TEGRA_IO_PAD_USB_BIAS, .dpd = 12, .voltage = UINT_MAX },
1806};
1807
Thierry Redingc2fe4692015-03-23 11:31:29 +01001808static const struct tegra_pmc_soc tegra210_pmc_soc = {
1809 .num_powergates = ARRAY_SIZE(tegra210_powergates),
1810 .powergates = tegra210_powergates,
1811 .num_cpu_powergates = ARRAY_SIZE(tegra210_cpu_powergates),
1812 .cpu_powergates = tegra210_cpu_powergates,
1813 .has_tsense_reset = true,
1814 .has_gpu_clamps = true,
Aapo Vienamo13136a42018-08-10 21:08:07 +03001815 .has_impl_33v_pwr = false,
Peter De Schrijvera263394a2018-01-25 16:00:13 +02001816 .needs_mbist_war = true,
Laxman Dewangan21b49912016-10-10 15:14:34 +02001817 .num_io_pads = ARRAY_SIZE(tegra210_io_pads),
1818 .io_pads = tegra210_io_pads,
Thierry Reding5be22552017-08-30 12:32:58 +02001819 .regs = &tegra20_pmc_regs,
1820 .init = tegra20_pmc_init,
1821 .setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
Thierry Redingc2fe4692015-03-23 11:31:29 +01001822};
1823
Thierry Redingc641ec62017-08-30 12:42:34 +02001824static const struct tegra_io_pad_soc tegra186_io_pads[] = {
1825 { .id = TEGRA_IO_PAD_CSIA, .dpd = 0, .voltage = UINT_MAX },
1826 { .id = TEGRA_IO_PAD_CSIB, .dpd = 1, .voltage = UINT_MAX },
1827 { .id = TEGRA_IO_PAD_DSI, .dpd = 2, .voltage = UINT_MAX },
1828 { .id = TEGRA_IO_PAD_MIPI_BIAS, .dpd = 3, .voltage = UINT_MAX },
1829 { .id = TEGRA_IO_PAD_PEX_CLK_BIAS, .dpd = 4, .voltage = UINT_MAX },
1830 { .id = TEGRA_IO_PAD_PEX_CLK3, .dpd = 5, .voltage = UINT_MAX },
1831 { .id = TEGRA_IO_PAD_PEX_CLK2, .dpd = 6, .voltage = UINT_MAX },
1832 { .id = TEGRA_IO_PAD_PEX_CLK1, .dpd = 7, .voltage = UINT_MAX },
1833 { .id = TEGRA_IO_PAD_USB0, .dpd = 9, .voltage = UINT_MAX },
1834 { .id = TEGRA_IO_PAD_USB1, .dpd = 10, .voltage = UINT_MAX },
1835 { .id = TEGRA_IO_PAD_USB2, .dpd = 11, .voltage = UINT_MAX },
1836 { .id = TEGRA_IO_PAD_USB_BIAS, .dpd = 12, .voltage = UINT_MAX },
1837 { .id = TEGRA_IO_PAD_UART, .dpd = 14, .voltage = UINT_MAX },
1838 { .id = TEGRA_IO_PAD_AUDIO, .dpd = 17, .voltage = UINT_MAX },
1839 { .id = TEGRA_IO_PAD_HSIC, .dpd = 19, .voltage = UINT_MAX },
1840 { .id = TEGRA_IO_PAD_DBG, .dpd = 25, .voltage = UINT_MAX },
1841 { .id = TEGRA_IO_PAD_HDMI_DP0, .dpd = 28, .voltage = UINT_MAX },
1842 { .id = TEGRA_IO_PAD_HDMI_DP1, .dpd = 29, .voltage = UINT_MAX },
1843 { .id = TEGRA_IO_PAD_PEX_CNTRL, .dpd = 32, .voltage = UINT_MAX },
Aapo Vienamo13136a42018-08-10 21:08:07 +03001844 { .id = TEGRA_IO_PAD_SDMMC2_HV, .dpd = 34, .voltage = 5 },
Thierry Redingc641ec62017-08-30 12:42:34 +02001845 { .id = TEGRA_IO_PAD_SDMMC4, .dpd = 36, .voltage = UINT_MAX },
1846 { .id = TEGRA_IO_PAD_CAM, .dpd = 38, .voltage = UINT_MAX },
1847 { .id = TEGRA_IO_PAD_DSIB, .dpd = 40, .voltage = UINT_MAX },
1848 { .id = TEGRA_IO_PAD_DSIC, .dpd = 41, .voltage = UINT_MAX },
1849 { .id = TEGRA_IO_PAD_DSID, .dpd = 42, .voltage = UINT_MAX },
1850 { .id = TEGRA_IO_PAD_CSIC, .dpd = 43, .voltage = UINT_MAX },
1851 { .id = TEGRA_IO_PAD_CSID, .dpd = 44, .voltage = UINT_MAX },
1852 { .id = TEGRA_IO_PAD_CSIE, .dpd = 45, .voltage = UINT_MAX },
1853 { .id = TEGRA_IO_PAD_CSIF, .dpd = 46, .voltage = UINT_MAX },
1854 { .id = TEGRA_IO_PAD_SPI, .dpd = 47, .voltage = UINT_MAX },
1855 { .id = TEGRA_IO_PAD_UFS, .dpd = 49, .voltage = UINT_MAX },
Aapo Vienamo13136a42018-08-10 21:08:07 +03001856 { .id = TEGRA_IO_PAD_DMIC_HV, .dpd = 52, .voltage = 2 },
Thierry Redingc641ec62017-08-30 12:42:34 +02001857 { .id = TEGRA_IO_PAD_EDP, .dpd = 53, .voltage = UINT_MAX },
Aapo Vienamo13136a42018-08-10 21:08:07 +03001858 { .id = TEGRA_IO_PAD_SDMMC1_HV, .dpd = 55, .voltage = 4 },
1859 { .id = TEGRA_IO_PAD_SDMMC3_HV, .dpd = 56, .voltage = 6 },
Thierry Redingc641ec62017-08-30 12:42:34 +02001860 { .id = TEGRA_IO_PAD_CONN, .dpd = 60, .voltage = UINT_MAX },
Aapo Vienamo13136a42018-08-10 21:08:07 +03001861 { .id = TEGRA_IO_PAD_AUDIO_HV, .dpd = 61, .voltage = 1 },
1862 { .id = TEGRA_IO_PAD_AO_HV, .dpd = UINT_MAX, .voltage = 0 },
Thierry Redingc641ec62017-08-30 12:42:34 +02001863};
1864
1865static const struct tegra_pmc_regs tegra186_pmc_regs = {
1866 .scratch0 = 0x2000,
1867 .dpd_req = 0x74,
1868 .dpd_status = 0x78,
1869 .dpd2_req = 0x7c,
1870 .dpd2_status = 0x80,
1871};
1872
1873static void tegra186_pmc_setup_irq_polarity(struct tegra_pmc *pmc,
1874 struct device_node *np,
1875 bool invert)
1876{
1877 struct resource regs;
1878 void __iomem *wake;
1879 u32 value;
1880 int index;
1881
1882 index = of_property_match_string(np, "reg-names", "wake");
1883 if (index < 0) {
1884 pr_err("failed to find PMC wake registers\n");
1885 return;
1886 }
1887
1888 of_address_to_resource(np, index, &regs);
1889
1890 wake = ioremap_nocache(regs.start, resource_size(&regs));
1891 if (!wake) {
1892 pr_err("failed to map PMC wake registers\n");
1893 return;
1894 }
1895
1896 value = readl(wake + WAKE_AOWAKE_CTRL);
1897
1898 if (invert)
1899 value |= WAKE_AOWAKE_CTRL_INTR_POLARITY;
1900 else
1901 value &= ~WAKE_AOWAKE_CTRL_INTR_POLARITY;
1902
1903 writel(value, wake + WAKE_AOWAKE_CTRL);
1904
1905 iounmap(wake);
1906}
1907
1908static const struct tegra_pmc_soc tegra186_pmc_soc = {
1909 .num_powergates = 0,
1910 .powergates = NULL,
1911 .num_cpu_powergates = 0,
1912 .cpu_powergates = NULL,
1913 .has_tsense_reset = false,
1914 .has_gpu_clamps = false,
Aapo Vienamo13136a42018-08-10 21:08:07 +03001915 .has_impl_33v_pwr = true,
Thierry Redingc641ec62017-08-30 12:42:34 +02001916 .num_io_pads = ARRAY_SIZE(tegra186_io_pads),
1917 .io_pads = tegra186_io_pads,
1918 .regs = &tegra186_pmc_regs,
1919 .init = NULL,
1920 .setup_irq_polarity = tegra186_pmc_setup_irq_polarity,
1921};
1922
Thierry Reding72323982014-07-11 13:19:06 +02001923static const struct of_device_id tegra_pmc_match[] = {
Mikko Perttunen56327f52018-02-20 13:58:08 +02001924 { .compatible = "nvidia,tegra194-pmc", .data = &tegra186_pmc_soc },
Thierry Redingc641ec62017-08-30 12:42:34 +02001925 { .compatible = "nvidia,tegra186-pmc", .data = &tegra186_pmc_soc },
Thierry Redingc2fe4692015-03-23 11:31:29 +01001926 { .compatible = "nvidia,tegra210-pmc", .data = &tegra210_pmc_soc },
Thierry Reding7d71e9032015-04-29 12:42:28 +02001927 { .compatible = "nvidia,tegra132-pmc", .data = &tegra124_pmc_soc },
Thierry Reding72323982014-07-11 13:19:06 +02001928 { .compatible = "nvidia,tegra124-pmc", .data = &tegra124_pmc_soc },
1929 { .compatible = "nvidia,tegra114-pmc", .data = &tegra114_pmc_soc },
1930 { .compatible = "nvidia,tegra30-pmc", .data = &tegra30_pmc_soc },
1931 { .compatible = "nvidia,tegra20-pmc", .data = &tegra20_pmc_soc },
1932 { }
1933};
1934
1935static struct platform_driver tegra_pmc_driver = {
1936 .driver = {
1937 .name = "tegra-pmc",
1938 .suppress_bind_attrs = true,
1939 .of_match_table = tegra_pmc_match,
Paul Walmsley2b20b612014-12-09 22:36:50 +00001940#if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM)
Thierry Reding72323982014-07-11 13:19:06 +02001941 .pm = &tegra_pmc_pm_ops,
Paul Walmsley2b20b612014-12-09 22:36:50 +00001942#endif
Thierry Reding72323982014-07-11 13:19:06 +02001943 },
1944 .probe = tegra_pmc_probe,
1945};
Paul Gortmaker7d4d9ed2015-05-01 20:10:57 -04001946builtin_platform_driver(tegra_pmc_driver);
Thierry Reding72323982014-07-11 13:19:06 +02001947
1948/*
1949 * Early initialization to allow access to registers in the very early boot
1950 * process.
1951 */
1952static int __init tegra_pmc_early_init(void)
1953{
1954 const struct of_device_id *match;
1955 struct device_node *np;
1956 struct resource regs;
1957 bool invert;
Thierry Reding72323982014-07-11 13:19:06 +02001958
Jon Hunter61fd2842016-06-28 11:38:26 +01001959 mutex_init(&pmc->powergates_lock);
1960
Thierry Reding72323982014-07-11 13:19:06 +02001961 np = of_find_matching_node_and_match(NULL, tegra_pmc_match, &match);
1962 if (!np) {
Thierry Reding7d71e9032015-04-29 12:42:28 +02001963 /*
1964 * Fall back to legacy initialization for 32-bit ARM only. All
1965 * 64-bit ARM device tree files for Tegra are required to have
1966 * a PMC node.
1967 *
1968 * This is for backwards-compatibility with old device trees
1969 * that didn't contain a PMC node. Note that in this case the
1970 * SoC data can't be matched and therefore powergating is
1971 * disabled.
1972 */
1973 if (IS_ENABLED(CONFIG_ARM) && soc_is_tegra()) {
1974 pr_warn("DT node not found, powergating disabled\n");
Thierry Reding72323982014-07-11 13:19:06 +02001975
Thierry Reding7d71e9032015-04-29 12:42:28 +02001976 regs.start = 0x7000e400;
1977 regs.end = 0x7000e7ff;
1978 regs.flags = IORESOURCE_MEM;
Thierry Reding72323982014-07-11 13:19:06 +02001979
Thierry Reding7d71e9032015-04-29 12:42:28 +02001980 pr_warn("Using memory region %pR\n", &regs);
1981 } else {
1982 /*
1983 * At this point we're not running on Tegra, so play
1984 * nice with multi-platform kernels.
1985 */
1986 return 0;
1987 }
Thierry Reding72323982014-07-11 13:19:06 +02001988 } else {
Thierry Reding7d71e9032015-04-29 12:42:28 +02001989 /*
1990 * Extract information from the device tree if we've found a
1991 * matching node.
1992 */
1993 if (of_address_to_resource(np, 0, &regs) < 0) {
1994 pr_err("failed to get PMC registers\n");
Jon Hunterb69a6252016-06-28 11:38:27 +01001995 of_node_put(np);
Thierry Reding7d71e9032015-04-29 12:42:28 +02001996 return -ENXIO;
1997 }
Thierry Reding72323982014-07-11 13:19:06 +02001998 }
1999
2000 pmc->base = ioremap_nocache(regs.start, resource_size(&regs));
2001 if (!pmc->base) {
2002 pr_err("failed to map PMC registers\n");
Jon Hunterb69a6252016-06-28 11:38:27 +01002003 of_node_put(np);
Thierry Reding72323982014-07-11 13:19:06 +02002004 return -ENXIO;
2005 }
2006
Jon Hunter11131892016-06-28 11:38:24 +01002007 if (np) {
Jon Hunter718a2422016-06-28 11:38:25 +01002008 pmc->soc = match->data;
2009
Jon Huntere2d17962016-06-30 11:56:25 +01002010 tegra_powergate_init(pmc, np);
Thierry Reding72323982014-07-11 13:19:06 +02002011
Jon Hunter11131892016-06-28 11:38:24 +01002012 /*
2013 * Invert the interrupt polarity if a PMC device tree node
2014 * exists and contains the nvidia,invert-interrupt property.
2015 */
2016 invert = of_property_read_bool(np, "nvidia,invert-interrupt");
Thierry Reding72323982014-07-11 13:19:06 +02002017
Thierry Reding5be22552017-08-30 12:32:58 +02002018 pmc->soc->setup_irq_polarity(pmc, np, invert);
Jon Hunterb69a6252016-06-28 11:38:27 +01002019
2020 of_node_put(np);
Jon Hunter11131892016-06-28 11:38:24 +01002021 }
Thierry Reding72323982014-07-11 13:19:06 +02002022
2023 return 0;
2024}
2025early_initcall(tegra_pmc_early_init);