blob: 08966c26d65cb959c379162c8905667584c22e0c [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>
34#include <linux/platform_device.h>
35#include <linux/reboot.h>
36#include <linux/reset.h>
37#include <linux/seq_file.h>
38#include <linux/spinlock.h>
39
40#include <soc/tegra/common.h>
41#include <soc/tegra/fuse.h>
42#include <soc/tegra/pmc.h>
43
44#define PMC_CNTRL 0x0
45#define PMC_CNTRL_SYSCLK_POLARITY (1 << 10) /* sys clk polarity */
46#define PMC_CNTRL_SYSCLK_OE (1 << 11) /* system clock enable */
47#define PMC_CNTRL_SIDE_EFFECT_LP0 (1 << 14) /* LP0 when CPU pwr gated */
48#define PMC_CNTRL_CPU_PWRREQ_POLARITY (1 << 15) /* CPU pwr req polarity */
49#define PMC_CNTRL_CPU_PWRREQ_OE (1 << 16) /* CPU pwr req enable */
50#define PMC_CNTRL_INTR_POLARITY (1 << 17) /* inverts INTR polarity */
51
52#define DPD_SAMPLE 0x020
53#define DPD_SAMPLE_ENABLE (1 << 0)
54#define DPD_SAMPLE_DISABLE (0 << 0)
55
56#define PWRGATE_TOGGLE 0x30
57#define PWRGATE_TOGGLE_START (1 << 8)
58
59#define REMOVE_CLAMPING 0x34
60
61#define PWRGATE_STATUS 0x38
62
63#define PMC_SCRATCH0 0x50
64#define PMC_SCRATCH0_MODE_RECOVERY (1 << 31)
65#define PMC_SCRATCH0_MODE_BOOTLOADER (1 << 30)
66#define PMC_SCRATCH0_MODE_RCM (1 << 1)
67#define PMC_SCRATCH0_MODE_MASK (PMC_SCRATCH0_MODE_RECOVERY | \
68 PMC_SCRATCH0_MODE_BOOTLOADER | \
69 PMC_SCRATCH0_MODE_RCM)
70
71#define PMC_CPUPWRGOOD_TIMER 0xc8
72#define PMC_CPUPWROFF_TIMER 0xcc
73
74#define PMC_SCRATCH41 0x140
75
Mikko Perttunen3568df32015-01-06 12:52:58 +020076#define PMC_SENSOR_CTRL 0x1b0
77#define PMC_SENSOR_CTRL_SCRATCH_WRITE (1 << 2)
78#define PMC_SENSOR_CTRL_ENABLE_RST (1 << 1)
79
Thierry Reding72323982014-07-11 13:19:06 +020080#define IO_DPD_REQ 0x1b8
81#define IO_DPD_REQ_CODE_IDLE (0 << 30)
82#define IO_DPD_REQ_CODE_OFF (1 << 30)
83#define IO_DPD_REQ_CODE_ON (2 << 30)
84#define IO_DPD_REQ_CODE_MASK (3 << 30)
85
86#define IO_DPD_STATUS 0x1bc
87#define IO_DPD2_REQ 0x1c0
88#define IO_DPD2_STATUS 0x1c4
89#define SEL_DPD_TIM 0x1c8
90
Mikko Perttunen3568df32015-01-06 12:52:58 +020091#define PMC_SCRATCH54 0x258
92#define PMC_SCRATCH54_DATA_SHIFT 8
93#define PMC_SCRATCH54_ADDR_SHIFT 0
94
95#define PMC_SCRATCH55 0x25c
96#define PMC_SCRATCH55_RESET_TEGRA (1 << 31)
97#define PMC_SCRATCH55_CNTRL_ID_SHIFT 27
98#define PMC_SCRATCH55_PINMUX_SHIFT 24
99#define PMC_SCRATCH55_16BITOP (1 << 15)
100#define PMC_SCRATCH55_CHECKSUM_SHIFT 16
101#define PMC_SCRATCH55_I2CSLV1_SHIFT 0
102
Thierry Reding72323982014-07-11 13:19:06 +0200103#define GPU_RG_CNTRL 0x2d4
104
105struct tegra_pmc_soc {
106 unsigned int num_powergates;
107 const char *const *powergates;
108 unsigned int num_cpu_powergates;
109 const u8 *cpu_powergates;
Thierry Redinga9a40a42015-01-09 11:15:33 +0100110
Mikko Perttunen3568df32015-01-06 12:52:58 +0200111 bool has_tsense_reset;
Thierry Redinga9a40a42015-01-09 11:15:33 +0100112 bool has_gpu_clamps;
Thierry Reding72323982014-07-11 13:19:06 +0200113};
114
115/**
116 * struct tegra_pmc - NVIDIA Tegra PMC
Jon Hunter35b67292015-12-04 14:57:03 +0000117 * @dev: pointer to PMC device structure
Thierry Reding72323982014-07-11 13:19:06 +0200118 * @base: pointer to I/O remapped register region
119 * @clk: pointer to pclk clock
Jon Hunter35b67292015-12-04 14:57:03 +0000120 * @soc: pointer to SoC data structure
Jon Hunter3195ac62015-12-04 14:57:05 +0000121 * @debugfs: pointer to debugfs entry
Thierry Reding72323982014-07-11 13:19:06 +0200122 * @rate: currently configured rate of pclk
123 * @suspend_mode: lowest suspend mode available
124 * @cpu_good_time: CPU power good time (in microseconds)
125 * @cpu_off_time: CPU power off time (in microsecends)
126 * @core_osc_time: core power good OSC time (in microseconds)
127 * @core_pmu_time: core power good PMU time (in microseconds)
128 * @core_off_time: core power off time (in microseconds)
129 * @corereq_high: core power request is active-high
130 * @sysclkreq_high: system clock request is active-high
131 * @combined_req: combined power request for CPU & core
132 * @cpu_pwr_good_en: CPU power good signal is enabled
133 * @lp0_vec_phys: physical base address of the LP0 warm boot code
134 * @lp0_vec_size: size of the LP0 warm boot code
135 * @powergates_lock: mutex for power gate register access
136 */
137struct tegra_pmc {
Mikko Perttunen3568df32015-01-06 12:52:58 +0200138 struct device *dev;
Thierry Reding72323982014-07-11 13:19:06 +0200139 void __iomem *base;
140 struct clk *clk;
Jon Hunter3195ac62015-12-04 14:57:05 +0000141 struct dentry *debugfs;
Thierry Reding72323982014-07-11 13:19:06 +0200142
143 const struct tegra_pmc_soc *soc;
144
145 unsigned long rate;
146
147 enum tegra_suspend_mode suspend_mode;
148 u32 cpu_good_time;
149 u32 cpu_off_time;
150 u32 core_osc_time;
151 u32 core_pmu_time;
152 u32 core_off_time;
153 bool corereq_high;
154 bool sysclkreq_high;
155 bool combined_req;
156 bool cpu_pwr_good_en;
157 u32 lp0_vec_phys;
158 u32 lp0_vec_size;
159
160 struct mutex powergates_lock;
161};
162
163static struct tegra_pmc *pmc = &(struct tegra_pmc) {
164 .base = NULL,
165 .suspend_mode = TEGRA_SUSPEND_NONE,
166};
167
168static u32 tegra_pmc_readl(unsigned long offset)
169{
170 return readl(pmc->base + offset);
171}
172
173static void tegra_pmc_writel(u32 value, unsigned long offset)
174{
175 writel(value, pmc->base + offset);
176}
177
Jon Hunter0ecf2d32016-02-11 18:03:23 +0000178static inline bool tegra_powergate_state(int id)
179{
Jon Hunterbc9af232016-02-15 12:38:11 +0000180 if (id == TEGRA_POWERGATE_3D && pmc->soc->has_gpu_clamps)
181 return (tegra_pmc_readl(GPU_RG_CNTRL) & 0x1) == 0;
182 else
183 return (tegra_pmc_readl(PWRGATE_STATUS) & BIT(id)) != 0;
Jon Hunter0ecf2d32016-02-11 18:03:23 +0000184}
185
Jon Hunter0a243bd2016-02-11 18:03:24 +0000186static inline bool tegra_powergate_is_valid(int id)
187{
188 return (pmc->soc && pmc->soc->powergates[id]);
189}
190
Thierry Reding72323982014-07-11 13:19:06 +0200191/**
192 * tegra_powergate_set() - set the state of a partition
193 * @id: partition ID
194 * @new_state: new state of the partition
195 */
Jon Hunter70293ed2016-02-11 18:03:22 +0000196static int tegra_powergate_set(unsigned int id, bool new_state)
Thierry Reding72323982014-07-11 13:19:06 +0200197{
Jon Hunter0a2d87e2016-02-26 15:48:40 +0000198 bool status;
199 int err;
200
Jon Hunterbc9af232016-02-15 12:38:11 +0000201 if (id == TEGRA_POWERGATE_3D && pmc->soc->has_gpu_clamps)
202 return -EINVAL;
203
Thierry Reding72323982014-07-11 13:19:06 +0200204 mutex_lock(&pmc->powergates_lock);
205
Jon Hunter0ecf2d32016-02-11 18:03:23 +0000206 if (tegra_powergate_state(id) == new_state) {
Thierry Reding72323982014-07-11 13:19:06 +0200207 mutex_unlock(&pmc->powergates_lock);
208 return 0;
209 }
210
211 tegra_pmc_writel(PWRGATE_TOGGLE_START | id, PWRGATE_TOGGLE);
212
Jon Hunter0a2d87e2016-02-26 15:48:40 +0000213 err = readx_poll_timeout(tegra_powergate_state, id, status,
214 status == new_state, 10, 100000);
215
Thierry Reding72323982014-07-11 13:19:06 +0200216 mutex_unlock(&pmc->powergates_lock);
217
Jon Hunter0a2d87e2016-02-26 15:48:40 +0000218 return err;
Thierry Reding72323982014-07-11 13:19:06 +0200219}
220
221/**
222 * tegra_powergate_power_on() - power on partition
223 * @id: partition ID
224 */
Jon Hunter70293ed2016-02-11 18:03:22 +0000225int tegra_powergate_power_on(unsigned int id)
Thierry Reding72323982014-07-11 13:19:06 +0200226{
Jon Hunter0a243bd2016-02-11 18:03:24 +0000227 if (!tegra_powergate_is_valid(id))
Thierry Reding72323982014-07-11 13:19:06 +0200228 return -EINVAL;
229
230 return tegra_powergate_set(id, true);
231}
232
233/**
234 * tegra_powergate_power_off() - power off partition
235 * @id: partition ID
236 */
Jon Hunter70293ed2016-02-11 18:03:22 +0000237int tegra_powergate_power_off(unsigned int id)
Thierry Reding72323982014-07-11 13:19:06 +0200238{
Jon Hunter0a243bd2016-02-11 18:03:24 +0000239 if (!tegra_powergate_is_valid(id))
Thierry Reding72323982014-07-11 13:19:06 +0200240 return -EINVAL;
241
242 return tegra_powergate_set(id, false);
243}
244EXPORT_SYMBOL(tegra_powergate_power_off);
245
246/**
247 * tegra_powergate_is_powered() - check if partition is powered
248 * @id: partition ID
249 */
Jon Hunter70293ed2016-02-11 18:03:22 +0000250int tegra_powergate_is_powered(unsigned int id)
Thierry Reding72323982014-07-11 13:19:06 +0200251{
Jon Hunter0ecf2d32016-02-11 18:03:23 +0000252 int status;
Thierry Reding72323982014-07-11 13:19:06 +0200253
Jon Hunter0a243bd2016-02-11 18:03:24 +0000254 if (!tegra_powergate_is_valid(id))
Thierry Reding72323982014-07-11 13:19:06 +0200255 return -EINVAL;
256
Jon Huntere8cf6612016-02-11 18:03:21 +0000257 mutex_lock(&pmc->powergates_lock);
Jon Hunter0ecf2d32016-02-11 18:03:23 +0000258 status = tegra_powergate_state(id);
Jon Huntere8cf6612016-02-11 18:03:21 +0000259 mutex_unlock(&pmc->powergates_lock);
260
Jon Hunter0ecf2d32016-02-11 18:03:23 +0000261 return status;
Thierry Reding72323982014-07-11 13:19:06 +0200262}
263
264/**
265 * tegra_powergate_remove_clamping() - remove power clamps for partition
266 * @id: partition ID
267 */
Jon Hunter70293ed2016-02-11 18:03:22 +0000268int tegra_powergate_remove_clamping(unsigned int id)
Thierry Reding72323982014-07-11 13:19:06 +0200269{
270 u32 mask;
271
Jon Hunter0a243bd2016-02-11 18:03:24 +0000272 if (!tegra_powergate_is_valid(id))
Thierry Reding72323982014-07-11 13:19:06 +0200273 return -EINVAL;
274
Jon Huntere8cf6612016-02-11 18:03:21 +0000275 mutex_lock(&pmc->powergates_lock);
276
Thierry Reding72323982014-07-11 13:19:06 +0200277 /*
Thierry Redinga9a40a42015-01-09 11:15:33 +0100278 * On Tegra124 and later, the clamps for the GPU are controlled by a
279 * separate register (with different semantics).
Thierry Reding72323982014-07-11 13:19:06 +0200280 */
Thierry Redinga9a40a42015-01-09 11:15:33 +0100281 if (id == TEGRA_POWERGATE_3D) {
282 if (pmc->soc->has_gpu_clamps) {
Thierry Reding72323982014-07-11 13:19:06 +0200283 tegra_pmc_writel(0, GPU_RG_CNTRL);
Jon Huntere8cf6612016-02-11 18:03:21 +0000284 goto out;
Thierry Reding72323982014-07-11 13:19:06 +0200285 }
286 }
287
288 /*
289 * Tegra 2 has a bug where PCIE and VDE clamping masks are
290 * swapped relatively to the partition ids
291 */
292 if (id == TEGRA_POWERGATE_VDEC)
293 mask = (1 << TEGRA_POWERGATE_PCIE);
294 else if (id == TEGRA_POWERGATE_PCIE)
295 mask = (1 << TEGRA_POWERGATE_VDEC);
296 else
297 mask = (1 << id);
298
299 tegra_pmc_writel(mask, REMOVE_CLAMPING);
300
Jon Huntere8cf6612016-02-11 18:03:21 +0000301out:
302 mutex_unlock(&pmc->powergates_lock);
303
Thierry Reding72323982014-07-11 13:19:06 +0200304 return 0;
305}
306EXPORT_SYMBOL(tegra_powergate_remove_clamping);
307
308/**
309 * tegra_powergate_sequence_power_up() - power up partition
310 * @id: partition ID
311 * @clk: clock for partition
312 * @rst: reset for partition
313 *
314 * Must be called with clk disabled, and returns with clk enabled.
315 */
Jon Hunter70293ed2016-02-11 18:03:22 +0000316int tegra_powergate_sequence_power_up(unsigned int id, struct clk *clk,
Thierry Reding72323982014-07-11 13:19:06 +0200317 struct reset_control *rst)
318{
319 int ret;
320
321 reset_control_assert(rst);
322
323 ret = tegra_powergate_power_on(id);
324 if (ret)
325 goto err_power;
326
327 ret = clk_prepare_enable(clk);
328 if (ret)
329 goto err_clk;
330
331 usleep_range(10, 20);
332
333 ret = tegra_powergate_remove_clamping(id);
334 if (ret)
335 goto err_clamp;
336
337 usleep_range(10, 20);
338 reset_control_deassert(rst);
339
340 return 0;
341
342err_clamp:
343 clk_disable_unprepare(clk);
344err_clk:
345 tegra_powergate_power_off(id);
346err_power:
347 return ret;
348}
349EXPORT_SYMBOL(tegra_powergate_sequence_power_up);
350
351#ifdef CONFIG_SMP
352/**
353 * tegra_get_cpu_powergate_id() - convert from CPU ID to partition ID
354 * @cpuid: CPU partition ID
355 *
356 * Returns the partition ID corresponding to the CPU partition ID or a
357 * negative error code on failure.
358 */
Jon Hunter70293ed2016-02-11 18:03:22 +0000359static int tegra_get_cpu_powergate_id(unsigned int cpuid)
Thierry Reding72323982014-07-11 13:19:06 +0200360{
Jon Hunter70293ed2016-02-11 18:03:22 +0000361 if (pmc->soc && cpuid < pmc->soc->num_cpu_powergates)
Thierry Reding72323982014-07-11 13:19:06 +0200362 return pmc->soc->cpu_powergates[cpuid];
363
364 return -EINVAL;
365}
366
367/**
368 * tegra_pmc_cpu_is_powered() - check if CPU partition is powered
369 * @cpuid: CPU partition ID
370 */
Jon Hunter70293ed2016-02-11 18:03:22 +0000371bool tegra_pmc_cpu_is_powered(unsigned int cpuid)
Thierry Reding72323982014-07-11 13:19:06 +0200372{
373 int id;
374
375 id = tegra_get_cpu_powergate_id(cpuid);
376 if (id < 0)
377 return false;
378
379 return tegra_powergate_is_powered(id);
380}
381
382/**
383 * tegra_pmc_cpu_power_on() - power on CPU partition
384 * @cpuid: CPU partition ID
385 */
Jon Hunter70293ed2016-02-11 18:03:22 +0000386int tegra_pmc_cpu_power_on(unsigned int cpuid)
Thierry Reding72323982014-07-11 13:19:06 +0200387{
388 int id;
389
390 id = tegra_get_cpu_powergate_id(cpuid);
391 if (id < 0)
392 return id;
393
394 return tegra_powergate_set(id, true);
395}
396
397/**
398 * tegra_pmc_cpu_remove_clamping() - remove power clamps for CPU partition
399 * @cpuid: CPU partition ID
400 */
Jon Hunter70293ed2016-02-11 18:03:22 +0000401int tegra_pmc_cpu_remove_clamping(unsigned int cpuid)
Thierry Reding72323982014-07-11 13:19:06 +0200402{
403 int id;
404
405 id = tegra_get_cpu_powergate_id(cpuid);
406 if (id < 0)
407 return id;
408
409 return tegra_powergate_remove_clamping(id);
410}
411#endif /* CONFIG_SMP */
412
David Riley78921582015-03-18 10:52:25 +0100413static int tegra_pmc_restart_notify(struct notifier_block *this,
414 unsigned long action, void *data)
Thierry Reding72323982014-07-11 13:19:06 +0200415{
David Riley78921582015-03-18 10:52:25 +0100416 const char *cmd = data;
Thierry Reding72323982014-07-11 13:19:06 +0200417 u32 value;
418
419 value = tegra_pmc_readl(PMC_SCRATCH0);
420 value &= ~PMC_SCRATCH0_MODE_MASK;
421
422 if (cmd) {
423 if (strcmp(cmd, "recovery") == 0)
424 value |= PMC_SCRATCH0_MODE_RECOVERY;
425
426 if (strcmp(cmd, "bootloader") == 0)
427 value |= PMC_SCRATCH0_MODE_BOOTLOADER;
428
429 if (strcmp(cmd, "forced-recovery") == 0)
430 value |= PMC_SCRATCH0_MODE_RCM;
431 }
432
433 tegra_pmc_writel(value, PMC_SCRATCH0);
434
435 value = tegra_pmc_readl(0);
436 value |= 0x10;
437 tegra_pmc_writel(value, 0);
David Riley78921582015-03-18 10:52:25 +0100438
439 return NOTIFY_DONE;
Thierry Reding72323982014-07-11 13:19:06 +0200440}
441
David Riley78921582015-03-18 10:52:25 +0100442static struct notifier_block tegra_pmc_restart_handler = {
443 .notifier_call = tegra_pmc_restart_notify,
444 .priority = 128,
445};
446
Thierry Reding72323982014-07-11 13:19:06 +0200447static int powergate_show(struct seq_file *s, void *data)
448{
449 unsigned int i;
Jon Hunterc3ea2972016-02-11 18:03:25 +0000450 int status;
Thierry Reding72323982014-07-11 13:19:06 +0200451
452 seq_printf(s, " powergate powered\n");
453 seq_printf(s, "------------------\n");
454
455 for (i = 0; i < pmc->soc->num_powergates; i++) {
Jon Hunterc3ea2972016-02-11 18:03:25 +0000456 status = tegra_powergate_is_powered(i);
457 if (status < 0)
Thierry Reding72323982014-07-11 13:19:06 +0200458 continue;
459
460 seq_printf(s, " %9s %7s\n", pmc->soc->powergates[i],
Jon Hunterc3ea2972016-02-11 18:03:25 +0000461 status ? "yes" : "no");
Thierry Reding72323982014-07-11 13:19:06 +0200462 }
463
464 return 0;
465}
466
467static int powergate_open(struct inode *inode, struct file *file)
468{
469 return single_open(file, powergate_show, inode->i_private);
470}
471
472static const struct file_operations powergate_fops = {
473 .open = powergate_open,
474 .read = seq_read,
475 .llseek = seq_lseek,
476 .release = single_release,
477};
478
479static int tegra_powergate_debugfs_init(void)
480{
Jon Hunter3195ac62015-12-04 14:57:05 +0000481 pmc->debugfs = debugfs_create_file("powergate", S_IRUGO, NULL, NULL,
482 &powergate_fops);
483 if (!pmc->debugfs)
Thierry Reding72323982014-07-11 13:19:06 +0200484 return -ENOMEM;
485
486 return 0;
487}
488
Jon Hunter70293ed2016-02-11 18:03:22 +0000489static int tegra_io_rail_prepare(unsigned int id, unsigned long *request,
Thierry Reding72323982014-07-11 13:19:06 +0200490 unsigned long *status, unsigned int *bit)
491{
492 unsigned long rate, value;
Thierry Reding72323982014-07-11 13:19:06 +0200493
494 *bit = id % 32;
495
496 /*
497 * There are two sets of 30 bits to select IO rails, but bits 30 and
498 * 31 are control bits rather than IO rail selection bits.
499 */
500 if (id > 63 || *bit == 30 || *bit == 31)
501 return -EINVAL;
502
503 if (id < 32) {
504 *status = IO_DPD_STATUS;
505 *request = IO_DPD_REQ;
506 } else {
507 *status = IO_DPD2_STATUS;
508 *request = IO_DPD2_REQ;
509 }
510
Thierry Reding592431b2015-08-04 15:25:03 +0200511 rate = clk_get_rate(pmc->clk);
Thierry Reding72323982014-07-11 13:19:06 +0200512
513 tegra_pmc_writel(DPD_SAMPLE_ENABLE, DPD_SAMPLE);
514
515 /* must be at least 200 ns, in APB (PCLK) clock cycles */
516 value = DIV_ROUND_UP(1000000000, rate);
517 value = DIV_ROUND_UP(200, value);
518 tegra_pmc_writel(value, SEL_DPD_TIM);
519
520 return 0;
521}
522
523static int tegra_io_rail_poll(unsigned long offset, unsigned long mask,
524 unsigned long val, unsigned long timeout)
525{
526 unsigned long value;
527
528 timeout = jiffies + msecs_to_jiffies(timeout);
529
530 while (time_after(timeout, jiffies)) {
531 value = tegra_pmc_readl(offset);
532 if ((value & mask) == val)
533 return 0;
534
535 usleep_range(250, 1000);
536 }
537
538 return -ETIMEDOUT;
539}
540
541static void tegra_io_rail_unprepare(void)
542{
543 tegra_pmc_writel(DPD_SAMPLE_DISABLE, DPD_SAMPLE);
544}
545
Jon Hunter70293ed2016-02-11 18:03:22 +0000546int tegra_io_rail_power_on(unsigned int id)
Thierry Reding72323982014-07-11 13:19:06 +0200547{
548 unsigned long request, status, value;
549 unsigned int bit, mask;
550 int err;
551
Jon Huntere8cf6612016-02-11 18:03:21 +0000552 mutex_lock(&pmc->powergates_lock);
553
Thierry Reding72323982014-07-11 13:19:06 +0200554 err = tegra_io_rail_prepare(id, &request, &status, &bit);
Jon Huntere8cf6612016-02-11 18:03:21 +0000555 if (err)
556 goto error;
Thierry Reding72323982014-07-11 13:19:06 +0200557
558 mask = 1 << bit;
559
560 value = tegra_pmc_readl(request);
561 value |= mask;
562 value &= ~IO_DPD_REQ_CODE_MASK;
563 value |= IO_DPD_REQ_CODE_OFF;
564 tegra_pmc_writel(value, request);
565
566 err = tegra_io_rail_poll(status, mask, 0, 250);
Jon Huntere8cf6612016-02-11 18:03:21 +0000567 if (err) {
Thierry Reding592431b2015-08-04 15:25:03 +0200568 pr_info("tegra_io_rail_poll() failed: %d\n", err);
Jon Huntere8cf6612016-02-11 18:03:21 +0000569 goto error;
Thierry Reding592431b2015-08-04 15:25:03 +0200570 }
Thierry Reding72323982014-07-11 13:19:06 +0200571
572 tegra_io_rail_unprepare();
573
Jon Huntere8cf6612016-02-11 18:03:21 +0000574error:
575 mutex_unlock(&pmc->powergates_lock);
576
577 return err;
Thierry Reding72323982014-07-11 13:19:06 +0200578}
579EXPORT_SYMBOL(tegra_io_rail_power_on);
580
Jon Hunter70293ed2016-02-11 18:03:22 +0000581int tegra_io_rail_power_off(unsigned int id)
Thierry Reding72323982014-07-11 13:19:06 +0200582{
583 unsigned long request, status, value;
584 unsigned int bit, mask;
585 int err;
586
Jon Huntere8cf6612016-02-11 18:03:21 +0000587 mutex_lock(&pmc->powergates_lock);
588
Thierry Reding72323982014-07-11 13:19:06 +0200589 err = tegra_io_rail_prepare(id, &request, &status, &bit);
Jon Huntere8cf6612016-02-11 18:03:21 +0000590 if (err) {
Thierry Reding592431b2015-08-04 15:25:03 +0200591 pr_info("tegra_io_rail_prepare() failed: %d\n", err);
Jon Huntere8cf6612016-02-11 18:03:21 +0000592 goto error;
Thierry Reding592431b2015-08-04 15:25:03 +0200593 }
Thierry Reding72323982014-07-11 13:19:06 +0200594
595 mask = 1 << bit;
596
597 value = tegra_pmc_readl(request);
598 value |= mask;
599 value &= ~IO_DPD_REQ_CODE_MASK;
600 value |= IO_DPD_REQ_CODE_ON;
601 tegra_pmc_writel(value, request);
602
603 err = tegra_io_rail_poll(status, mask, mask, 250);
Jon Huntere8cf6612016-02-11 18:03:21 +0000604 if (err)
605 goto error;
Thierry Reding72323982014-07-11 13:19:06 +0200606
607 tegra_io_rail_unprepare();
608
Jon Huntere8cf6612016-02-11 18:03:21 +0000609error:
610 mutex_unlock(&pmc->powergates_lock);
611
612 return err;
Thierry Reding72323982014-07-11 13:19:06 +0200613}
614EXPORT_SYMBOL(tegra_io_rail_power_off);
615
616#ifdef CONFIG_PM_SLEEP
617enum tegra_suspend_mode tegra_pmc_get_suspend_mode(void)
618{
619 return pmc->suspend_mode;
620}
621
622void tegra_pmc_set_suspend_mode(enum tegra_suspend_mode mode)
623{
624 if (mode < TEGRA_SUSPEND_NONE || mode >= TEGRA_MAX_SUSPEND_MODE)
625 return;
626
627 pmc->suspend_mode = mode;
628}
629
630void tegra_pmc_enter_suspend_mode(enum tegra_suspend_mode mode)
631{
632 unsigned long long rate = 0;
633 u32 value;
634
635 switch (mode) {
636 case TEGRA_SUSPEND_LP1:
637 rate = 32768;
638 break;
639
640 case TEGRA_SUSPEND_LP2:
641 rate = clk_get_rate(pmc->clk);
642 break;
643
644 default:
645 break;
646 }
647
648 if (WARN_ON_ONCE(rate == 0))
649 rate = 100000000;
650
651 if (rate != pmc->rate) {
652 u64 ticks;
653
654 ticks = pmc->cpu_good_time * rate + USEC_PER_SEC - 1;
655 do_div(ticks, USEC_PER_SEC);
656 tegra_pmc_writel(ticks, PMC_CPUPWRGOOD_TIMER);
657
658 ticks = pmc->cpu_off_time * rate + USEC_PER_SEC - 1;
659 do_div(ticks, USEC_PER_SEC);
660 tegra_pmc_writel(ticks, PMC_CPUPWROFF_TIMER);
661
662 wmb();
663
664 pmc->rate = rate;
665 }
666
667 value = tegra_pmc_readl(PMC_CNTRL);
668 value &= ~PMC_CNTRL_SIDE_EFFECT_LP0;
669 value |= PMC_CNTRL_CPU_PWRREQ_OE;
670 tegra_pmc_writel(value, PMC_CNTRL);
671}
672#endif
673
674static int tegra_pmc_parse_dt(struct tegra_pmc *pmc, struct device_node *np)
675{
676 u32 value, values[2];
677
678 if (of_property_read_u32(np, "nvidia,suspend-mode", &value)) {
679 } else {
680 switch (value) {
681 case 0:
682 pmc->suspend_mode = TEGRA_SUSPEND_LP0;
683 break;
684
685 case 1:
686 pmc->suspend_mode = TEGRA_SUSPEND_LP1;
687 break;
688
689 case 2:
690 pmc->suspend_mode = TEGRA_SUSPEND_LP2;
691 break;
692
693 default:
694 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
695 break;
696 }
697 }
698
699 pmc->suspend_mode = tegra_pm_validate_suspend_mode(pmc->suspend_mode);
700
701 if (of_property_read_u32(np, "nvidia,cpu-pwr-good-time", &value))
702 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
703
704 pmc->cpu_good_time = value;
705
706 if (of_property_read_u32(np, "nvidia,cpu-pwr-off-time", &value))
707 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
708
709 pmc->cpu_off_time = value;
710
711 if (of_property_read_u32_array(np, "nvidia,core-pwr-good-time",
712 values, ARRAY_SIZE(values)))
713 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
714
715 pmc->core_osc_time = values[0];
716 pmc->core_pmu_time = values[1];
717
718 if (of_property_read_u32(np, "nvidia,core-pwr-off-time", &value))
719 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
720
721 pmc->core_off_time = value;
722
723 pmc->corereq_high = of_property_read_bool(np,
724 "nvidia,core-power-req-active-high");
725
726 pmc->sysclkreq_high = of_property_read_bool(np,
727 "nvidia,sys-clock-req-active-high");
728
729 pmc->combined_req = of_property_read_bool(np,
730 "nvidia,combined-power-req");
731
732 pmc->cpu_pwr_good_en = of_property_read_bool(np,
733 "nvidia,cpu-pwr-good-en");
734
735 if (of_property_read_u32_array(np, "nvidia,lp0-vec", values,
736 ARRAY_SIZE(values)))
737 if (pmc->suspend_mode == TEGRA_SUSPEND_LP0)
738 pmc->suspend_mode = TEGRA_SUSPEND_LP1;
739
740 pmc->lp0_vec_phys = values[0];
741 pmc->lp0_vec_size = values[1];
742
743 return 0;
744}
745
746static void tegra_pmc_init(struct tegra_pmc *pmc)
747{
748 u32 value;
749
750 /* Always enable CPU power request */
751 value = tegra_pmc_readl(PMC_CNTRL);
752 value |= PMC_CNTRL_CPU_PWRREQ_OE;
753 tegra_pmc_writel(value, PMC_CNTRL);
754
755 value = tegra_pmc_readl(PMC_CNTRL);
756
757 if (pmc->sysclkreq_high)
758 value &= ~PMC_CNTRL_SYSCLK_POLARITY;
759 else
760 value |= PMC_CNTRL_SYSCLK_POLARITY;
761
762 /* configure the output polarity while the request is tristated */
763 tegra_pmc_writel(value, PMC_CNTRL);
764
765 /* now enable the request */
766 value = tegra_pmc_readl(PMC_CNTRL);
767 value |= PMC_CNTRL_SYSCLK_OE;
768 tegra_pmc_writel(value, PMC_CNTRL);
769}
770
Jon Hunter1e52efdf2015-12-04 14:57:04 +0000771static void tegra_pmc_init_tsense_reset(struct tegra_pmc *pmc)
Mikko Perttunen3568df32015-01-06 12:52:58 +0200772{
773 static const char disabled[] = "emergency thermal reset disabled";
774 u32 pmu_addr, ctrl_id, reg_addr, reg_data, pinmux;
775 struct device *dev = pmc->dev;
776 struct device_node *np;
777 u32 value, checksum;
778
779 if (!pmc->soc->has_tsense_reset)
Thierry Reding95169cd2015-07-09 09:59:55 +0200780 return;
Mikko Perttunen3568df32015-01-06 12:52:58 +0200781
782 np = of_find_node_by_name(pmc->dev->of_node, "i2c-thermtrip");
783 if (!np) {
784 dev_warn(dev, "i2c-thermtrip node not found, %s.\n", disabled);
Thierry Reding95169cd2015-07-09 09:59:55 +0200785 return;
Mikko Perttunen3568df32015-01-06 12:52:58 +0200786 }
787
788 if (of_property_read_u32(np, "nvidia,i2c-controller-id", &ctrl_id)) {
789 dev_err(dev, "I2C controller ID missing, %s.\n", disabled);
790 goto out;
791 }
792
793 if (of_property_read_u32(np, "nvidia,bus-addr", &pmu_addr)) {
794 dev_err(dev, "nvidia,bus-addr missing, %s.\n", disabled);
795 goto out;
796 }
797
798 if (of_property_read_u32(np, "nvidia,reg-addr", &reg_addr)) {
799 dev_err(dev, "nvidia,reg-addr missing, %s.\n", disabled);
800 goto out;
801 }
802
803 if (of_property_read_u32(np, "nvidia,reg-data", &reg_data)) {
804 dev_err(dev, "nvidia,reg-data missing, %s.\n", disabled);
805 goto out;
806 }
807
808 if (of_property_read_u32(np, "nvidia,pinmux-id", &pinmux))
809 pinmux = 0;
810
811 value = tegra_pmc_readl(PMC_SENSOR_CTRL);
812 value |= PMC_SENSOR_CTRL_SCRATCH_WRITE;
813 tegra_pmc_writel(value, PMC_SENSOR_CTRL);
814
815 value = (reg_data << PMC_SCRATCH54_DATA_SHIFT) |
816 (reg_addr << PMC_SCRATCH54_ADDR_SHIFT);
817 tegra_pmc_writel(value, PMC_SCRATCH54);
818
819 value = PMC_SCRATCH55_RESET_TEGRA;
820 value |= ctrl_id << PMC_SCRATCH55_CNTRL_ID_SHIFT;
821 value |= pinmux << PMC_SCRATCH55_PINMUX_SHIFT;
822 value |= pmu_addr << PMC_SCRATCH55_I2CSLV1_SHIFT;
823
824 /*
825 * Calculate checksum of SCRATCH54, SCRATCH55 fields. Bits 23:16 will
826 * contain the checksum and are currently zero, so they are not added.
827 */
828 checksum = reg_addr + reg_data + (value & 0xff) + ((value >> 8) & 0xff)
829 + ((value >> 24) & 0xff);
830 checksum &= 0xff;
831 checksum = 0x100 - checksum;
832
833 value |= checksum << PMC_SCRATCH55_CHECKSUM_SHIFT;
834
835 tegra_pmc_writel(value, PMC_SCRATCH55);
836
837 value = tegra_pmc_readl(PMC_SENSOR_CTRL);
838 value |= PMC_SENSOR_CTRL_ENABLE_RST;
839 tegra_pmc_writel(value, PMC_SENSOR_CTRL);
840
841 dev_info(pmc->dev, "emergency thermal reset enabled\n");
842
843out:
844 of_node_put(np);
Mikko Perttunen3568df32015-01-06 12:52:58 +0200845}
846
Thierry Reding72323982014-07-11 13:19:06 +0200847static int tegra_pmc_probe(struct platform_device *pdev)
848{
Jon Huntere8cf6612016-02-11 18:03:21 +0000849 void __iomem *base;
Thierry Reding72323982014-07-11 13:19:06 +0200850 struct resource *res;
851 int err;
852
853 err = tegra_pmc_parse_dt(pmc, pdev->dev.of_node);
854 if (err < 0)
855 return err;
856
857 /* take over the memory region from the early initialization */
858 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Jon Hunter0259f522016-02-11 18:03:20 +0000859 base = devm_ioremap_resource(&pdev->dev, res);
860 if (IS_ERR(base))
861 return PTR_ERR(base);
Thierry Reding72323982014-07-11 13:19:06 +0200862
863 pmc->clk = devm_clk_get(&pdev->dev, "pclk");
864 if (IS_ERR(pmc->clk)) {
865 err = PTR_ERR(pmc->clk);
866 dev_err(&pdev->dev, "failed to get pclk: %d\n", err);
867 return err;
868 }
869
Mikko Perttunen3568df32015-01-06 12:52:58 +0200870 pmc->dev = &pdev->dev;
871
Thierry Reding72323982014-07-11 13:19:06 +0200872 tegra_pmc_init(pmc);
873
Mikko Perttunen3568df32015-01-06 12:52:58 +0200874 tegra_pmc_init_tsense_reset(pmc);
875
Thierry Reding72323982014-07-11 13:19:06 +0200876 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
877 err = tegra_powergate_debugfs_init();
878 if (err < 0)
879 return err;
880 }
881
David Riley78921582015-03-18 10:52:25 +0100882 err = register_restart_handler(&tegra_pmc_restart_handler);
883 if (err) {
Jon Hunter3195ac62015-12-04 14:57:05 +0000884 debugfs_remove(pmc->debugfs);
David Riley78921582015-03-18 10:52:25 +0100885 dev_err(&pdev->dev, "unable to register restart handler, %d\n",
886 err);
887 return err;
888 }
889
Jon Huntere8cf6612016-02-11 18:03:21 +0000890 mutex_lock(&pmc->powergates_lock);
891 iounmap(pmc->base);
Jon Hunter0259f522016-02-11 18:03:20 +0000892 pmc->base = base;
Jon Huntere8cf6612016-02-11 18:03:21 +0000893 mutex_unlock(&pmc->powergates_lock);
Jon Hunter0259f522016-02-11 18:03:20 +0000894
Thierry Reding72323982014-07-11 13:19:06 +0200895 return 0;
896}
897
Paul Walmsley2b20b612014-12-09 22:36:50 +0000898#if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM)
Thierry Reding72323982014-07-11 13:19:06 +0200899static int tegra_pmc_suspend(struct device *dev)
900{
901 tegra_pmc_writel(virt_to_phys(tegra_resume), PMC_SCRATCH41);
902
903 return 0;
904}
905
906static int tegra_pmc_resume(struct device *dev)
907{
908 tegra_pmc_writel(0x0, PMC_SCRATCH41);
909
910 return 0;
911}
Thierry Reding72323982014-07-11 13:19:06 +0200912
913static SIMPLE_DEV_PM_OPS(tegra_pmc_pm_ops, tegra_pmc_suspend, tegra_pmc_resume);
914
Paul Walmsley2b20b612014-12-09 22:36:50 +0000915#endif
916
Thierry Reding72323982014-07-11 13:19:06 +0200917static const char * const tegra20_powergates[] = {
918 [TEGRA_POWERGATE_CPU] = "cpu",
919 [TEGRA_POWERGATE_3D] = "3d",
920 [TEGRA_POWERGATE_VENC] = "venc",
921 [TEGRA_POWERGATE_VDEC] = "vdec",
922 [TEGRA_POWERGATE_PCIE] = "pcie",
923 [TEGRA_POWERGATE_L2] = "l2",
924 [TEGRA_POWERGATE_MPE] = "mpe",
925};
926
927static const struct tegra_pmc_soc tegra20_pmc_soc = {
928 .num_powergates = ARRAY_SIZE(tegra20_powergates),
929 .powergates = tegra20_powergates,
930 .num_cpu_powergates = 0,
931 .cpu_powergates = NULL,
Mikko Perttunen3568df32015-01-06 12:52:58 +0200932 .has_tsense_reset = false,
Thierry Redinga9a40a42015-01-09 11:15:33 +0100933 .has_gpu_clamps = false,
Thierry Reding72323982014-07-11 13:19:06 +0200934};
935
936static const char * const tegra30_powergates[] = {
937 [TEGRA_POWERGATE_CPU] = "cpu0",
938 [TEGRA_POWERGATE_3D] = "3d0",
939 [TEGRA_POWERGATE_VENC] = "venc",
940 [TEGRA_POWERGATE_VDEC] = "vdec",
941 [TEGRA_POWERGATE_PCIE] = "pcie",
942 [TEGRA_POWERGATE_L2] = "l2",
943 [TEGRA_POWERGATE_MPE] = "mpe",
944 [TEGRA_POWERGATE_HEG] = "heg",
945 [TEGRA_POWERGATE_SATA] = "sata",
946 [TEGRA_POWERGATE_CPU1] = "cpu1",
947 [TEGRA_POWERGATE_CPU2] = "cpu2",
948 [TEGRA_POWERGATE_CPU3] = "cpu3",
949 [TEGRA_POWERGATE_CELP] = "celp",
950 [TEGRA_POWERGATE_3D1] = "3d1",
951};
952
953static const u8 tegra30_cpu_powergates[] = {
954 TEGRA_POWERGATE_CPU,
955 TEGRA_POWERGATE_CPU1,
956 TEGRA_POWERGATE_CPU2,
957 TEGRA_POWERGATE_CPU3,
958};
959
960static const struct tegra_pmc_soc tegra30_pmc_soc = {
961 .num_powergates = ARRAY_SIZE(tegra30_powergates),
962 .powergates = tegra30_powergates,
963 .num_cpu_powergates = ARRAY_SIZE(tegra30_cpu_powergates),
964 .cpu_powergates = tegra30_cpu_powergates,
Mikko Perttunen3568df32015-01-06 12:52:58 +0200965 .has_tsense_reset = true,
Thierry Redinga9a40a42015-01-09 11:15:33 +0100966 .has_gpu_clamps = false,
Thierry Reding72323982014-07-11 13:19:06 +0200967};
968
969static const char * const tegra114_powergates[] = {
970 [TEGRA_POWERGATE_CPU] = "crail",
971 [TEGRA_POWERGATE_3D] = "3d",
972 [TEGRA_POWERGATE_VENC] = "venc",
973 [TEGRA_POWERGATE_VDEC] = "vdec",
974 [TEGRA_POWERGATE_MPE] = "mpe",
975 [TEGRA_POWERGATE_HEG] = "heg",
976 [TEGRA_POWERGATE_CPU1] = "cpu1",
977 [TEGRA_POWERGATE_CPU2] = "cpu2",
978 [TEGRA_POWERGATE_CPU3] = "cpu3",
979 [TEGRA_POWERGATE_CELP] = "celp",
980 [TEGRA_POWERGATE_CPU0] = "cpu0",
981 [TEGRA_POWERGATE_C0NC] = "c0nc",
982 [TEGRA_POWERGATE_C1NC] = "c1nc",
983 [TEGRA_POWERGATE_DIS] = "dis",
984 [TEGRA_POWERGATE_DISB] = "disb",
985 [TEGRA_POWERGATE_XUSBA] = "xusba",
986 [TEGRA_POWERGATE_XUSBB] = "xusbb",
987 [TEGRA_POWERGATE_XUSBC] = "xusbc",
988};
989
990static const u8 tegra114_cpu_powergates[] = {
991 TEGRA_POWERGATE_CPU0,
992 TEGRA_POWERGATE_CPU1,
993 TEGRA_POWERGATE_CPU2,
994 TEGRA_POWERGATE_CPU3,
995};
996
997static const struct tegra_pmc_soc tegra114_pmc_soc = {
998 .num_powergates = ARRAY_SIZE(tegra114_powergates),
999 .powergates = tegra114_powergates,
1000 .num_cpu_powergates = ARRAY_SIZE(tegra114_cpu_powergates),
1001 .cpu_powergates = tegra114_cpu_powergates,
Mikko Perttunen3568df32015-01-06 12:52:58 +02001002 .has_tsense_reset = true,
Thierry Redinga9a40a42015-01-09 11:15:33 +01001003 .has_gpu_clamps = false,
Thierry Reding72323982014-07-11 13:19:06 +02001004};
1005
1006static const char * const tegra124_powergates[] = {
1007 [TEGRA_POWERGATE_CPU] = "crail",
1008 [TEGRA_POWERGATE_3D] = "3d",
1009 [TEGRA_POWERGATE_VENC] = "venc",
1010 [TEGRA_POWERGATE_PCIE] = "pcie",
1011 [TEGRA_POWERGATE_VDEC] = "vdec",
Thierry Reding72323982014-07-11 13:19:06 +02001012 [TEGRA_POWERGATE_MPE] = "mpe",
1013 [TEGRA_POWERGATE_HEG] = "heg",
1014 [TEGRA_POWERGATE_SATA] = "sata",
1015 [TEGRA_POWERGATE_CPU1] = "cpu1",
1016 [TEGRA_POWERGATE_CPU2] = "cpu2",
1017 [TEGRA_POWERGATE_CPU3] = "cpu3",
1018 [TEGRA_POWERGATE_CELP] = "celp",
1019 [TEGRA_POWERGATE_CPU0] = "cpu0",
1020 [TEGRA_POWERGATE_C0NC] = "c0nc",
1021 [TEGRA_POWERGATE_C1NC] = "c1nc",
1022 [TEGRA_POWERGATE_SOR] = "sor",
1023 [TEGRA_POWERGATE_DIS] = "dis",
1024 [TEGRA_POWERGATE_DISB] = "disb",
1025 [TEGRA_POWERGATE_XUSBA] = "xusba",
1026 [TEGRA_POWERGATE_XUSBB] = "xusbb",
1027 [TEGRA_POWERGATE_XUSBC] = "xusbc",
1028 [TEGRA_POWERGATE_VIC] = "vic",
1029 [TEGRA_POWERGATE_IRAM] = "iram",
1030};
1031
1032static const u8 tegra124_cpu_powergates[] = {
1033 TEGRA_POWERGATE_CPU0,
1034 TEGRA_POWERGATE_CPU1,
1035 TEGRA_POWERGATE_CPU2,
1036 TEGRA_POWERGATE_CPU3,
1037};
1038
1039static const struct tegra_pmc_soc tegra124_pmc_soc = {
1040 .num_powergates = ARRAY_SIZE(tegra124_powergates),
1041 .powergates = tegra124_powergates,
1042 .num_cpu_powergates = ARRAY_SIZE(tegra124_cpu_powergates),
1043 .cpu_powergates = tegra124_cpu_powergates,
Mikko Perttunen3568df32015-01-06 12:52:58 +02001044 .has_tsense_reset = true,
Thierry Redinga9a40a42015-01-09 11:15:33 +01001045 .has_gpu_clamps = true,
Thierry Reding72323982014-07-11 13:19:06 +02001046};
1047
Thierry Redingc2fe4692015-03-23 11:31:29 +01001048static const char * const tegra210_powergates[] = {
1049 [TEGRA_POWERGATE_CPU] = "crail",
1050 [TEGRA_POWERGATE_3D] = "3d",
1051 [TEGRA_POWERGATE_VENC] = "venc",
1052 [TEGRA_POWERGATE_PCIE] = "pcie",
Thierry Redingc2fe4692015-03-23 11:31:29 +01001053 [TEGRA_POWERGATE_MPE] = "mpe",
Thierry Redingc2fe4692015-03-23 11:31:29 +01001054 [TEGRA_POWERGATE_SATA] = "sata",
1055 [TEGRA_POWERGATE_CPU1] = "cpu1",
1056 [TEGRA_POWERGATE_CPU2] = "cpu2",
1057 [TEGRA_POWERGATE_CPU3] = "cpu3",
Thierry Redingc2fe4692015-03-23 11:31:29 +01001058 [TEGRA_POWERGATE_CPU0] = "cpu0",
1059 [TEGRA_POWERGATE_C0NC] = "c0nc",
Thierry Redingc2fe4692015-03-23 11:31:29 +01001060 [TEGRA_POWERGATE_SOR] = "sor",
1061 [TEGRA_POWERGATE_DIS] = "dis",
1062 [TEGRA_POWERGATE_DISB] = "disb",
1063 [TEGRA_POWERGATE_XUSBA] = "xusba",
1064 [TEGRA_POWERGATE_XUSBB] = "xusbb",
1065 [TEGRA_POWERGATE_XUSBC] = "xusbc",
1066 [TEGRA_POWERGATE_VIC] = "vic",
1067 [TEGRA_POWERGATE_IRAM] = "iram",
1068 [TEGRA_POWERGATE_NVDEC] = "nvdec",
1069 [TEGRA_POWERGATE_NVJPG] = "nvjpg",
1070 [TEGRA_POWERGATE_AUD] = "aud",
1071 [TEGRA_POWERGATE_DFD] = "dfd",
1072 [TEGRA_POWERGATE_VE2] = "ve2",
1073};
1074
1075static const u8 tegra210_cpu_powergates[] = {
1076 TEGRA_POWERGATE_CPU0,
1077 TEGRA_POWERGATE_CPU1,
1078 TEGRA_POWERGATE_CPU2,
1079 TEGRA_POWERGATE_CPU3,
1080};
1081
1082static const struct tegra_pmc_soc tegra210_pmc_soc = {
1083 .num_powergates = ARRAY_SIZE(tegra210_powergates),
1084 .powergates = tegra210_powergates,
1085 .num_cpu_powergates = ARRAY_SIZE(tegra210_cpu_powergates),
1086 .cpu_powergates = tegra210_cpu_powergates,
1087 .has_tsense_reset = true,
1088 .has_gpu_clamps = true,
1089};
1090
Thierry Reding72323982014-07-11 13:19:06 +02001091static const struct of_device_id tegra_pmc_match[] = {
Thierry Redingc2fe4692015-03-23 11:31:29 +01001092 { .compatible = "nvidia,tegra210-pmc", .data = &tegra210_pmc_soc },
Thierry Reding7d71e9032015-04-29 12:42:28 +02001093 { .compatible = "nvidia,tegra132-pmc", .data = &tegra124_pmc_soc },
Thierry Reding72323982014-07-11 13:19:06 +02001094 { .compatible = "nvidia,tegra124-pmc", .data = &tegra124_pmc_soc },
1095 { .compatible = "nvidia,tegra114-pmc", .data = &tegra114_pmc_soc },
1096 { .compatible = "nvidia,tegra30-pmc", .data = &tegra30_pmc_soc },
1097 { .compatible = "nvidia,tegra20-pmc", .data = &tegra20_pmc_soc },
1098 { }
1099};
1100
1101static struct platform_driver tegra_pmc_driver = {
1102 .driver = {
1103 .name = "tegra-pmc",
1104 .suppress_bind_attrs = true,
1105 .of_match_table = tegra_pmc_match,
Paul Walmsley2b20b612014-12-09 22:36:50 +00001106#if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM)
Thierry Reding72323982014-07-11 13:19:06 +02001107 .pm = &tegra_pmc_pm_ops,
Paul Walmsley2b20b612014-12-09 22:36:50 +00001108#endif
Thierry Reding72323982014-07-11 13:19:06 +02001109 },
1110 .probe = tegra_pmc_probe,
1111};
Paul Gortmaker7d4d9ed2015-05-01 20:10:57 -04001112builtin_platform_driver(tegra_pmc_driver);
Thierry Reding72323982014-07-11 13:19:06 +02001113
1114/*
1115 * Early initialization to allow access to registers in the very early boot
1116 * process.
1117 */
1118static int __init tegra_pmc_early_init(void)
1119{
1120 const struct of_device_id *match;
1121 struct device_node *np;
1122 struct resource regs;
1123 bool invert;
1124 u32 value;
1125
Thierry Reding72323982014-07-11 13:19:06 +02001126 np = of_find_matching_node_and_match(NULL, tegra_pmc_match, &match);
1127 if (!np) {
Thierry Reding7d71e9032015-04-29 12:42:28 +02001128 /*
1129 * Fall back to legacy initialization for 32-bit ARM only. All
1130 * 64-bit ARM device tree files for Tegra are required to have
1131 * a PMC node.
1132 *
1133 * This is for backwards-compatibility with old device trees
1134 * that didn't contain a PMC node. Note that in this case the
1135 * SoC data can't be matched and therefore powergating is
1136 * disabled.
1137 */
1138 if (IS_ENABLED(CONFIG_ARM) && soc_is_tegra()) {
1139 pr_warn("DT node not found, powergating disabled\n");
Thierry Reding72323982014-07-11 13:19:06 +02001140
Thierry Reding7d71e9032015-04-29 12:42:28 +02001141 regs.start = 0x7000e400;
1142 regs.end = 0x7000e7ff;
1143 regs.flags = IORESOURCE_MEM;
Thierry Reding72323982014-07-11 13:19:06 +02001144
Thierry Reding7d71e9032015-04-29 12:42:28 +02001145 pr_warn("Using memory region %pR\n", &regs);
1146 } else {
1147 /*
1148 * At this point we're not running on Tegra, so play
1149 * nice with multi-platform kernels.
1150 */
1151 return 0;
1152 }
Thierry Reding72323982014-07-11 13:19:06 +02001153 } else {
Thierry Reding7d71e9032015-04-29 12:42:28 +02001154 /*
1155 * Extract information from the device tree if we've found a
1156 * matching node.
1157 */
1158 if (of_address_to_resource(np, 0, &regs) < 0) {
1159 pr_err("failed to get PMC registers\n");
1160 return -ENXIO;
1161 }
Thierry Reding72323982014-07-11 13:19:06 +02001162
Thierry Reding7d71e9032015-04-29 12:42:28 +02001163 pmc->soc = match->data;
Thierry Reding72323982014-07-11 13:19:06 +02001164 }
1165
1166 pmc->base = ioremap_nocache(regs.start, resource_size(&regs));
1167 if (!pmc->base) {
1168 pr_err("failed to map PMC registers\n");
1169 return -ENXIO;
1170 }
1171
1172 mutex_init(&pmc->powergates_lock);
1173
Thierry Reding7d71e9032015-04-29 12:42:28 +02001174 /*
1175 * Invert the interrupt polarity if a PMC device tree node exists and
1176 * contains the nvidia,invert-interrupt property.
1177 */
Thierry Reding72323982014-07-11 13:19:06 +02001178 invert = of_property_read_bool(np, "nvidia,invert-interrupt");
1179
1180 value = tegra_pmc_readl(PMC_CNTRL);
1181
1182 if (invert)
1183 value |= PMC_CNTRL_INTR_POLARITY;
1184 else
1185 value &= ~PMC_CNTRL_INTR_POLARITY;
1186
1187 tegra_pmc_writel(value, PMC_CNTRL);
1188
1189 return 0;
1190}
1191early_initcall(tegra_pmc_early_init);