blob: d6ec2e5b906e0b3e704cdc821be94b313c43eb43 [file] [log] [blame]
Thierry Reding72323982014-07-11 13:19:06 +02001/*
2 * drivers/soc/tegra/pmc.c
3 *
4 * Copyright (c) 2010 Google, Inc
Sandipan Patra5f84bb12018-10-24 12:38:00 +05305 * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
Thierry Reding72323982014-07-11 13:19:06 +02006 *
7 * Author:
8 * Colin Cross <ccross@google.com>
9 *
10 * This software is licensed under the terms of the GNU General Public
11 * License version 2, as published by the Free Software Foundation, and
12 * may be copied, distributed, and modified under those terms.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 */
20
Thierry Reding7d71e9032015-04-29 12:42:28 +020021#define pr_fmt(fmt) "tegra-pmc: " fmt
22
Thierry Reding72323982014-07-11 13:19:06 +020023#include <linux/kernel.h>
24#include <linux/clk.h>
25#include <linux/clk/tegra.h>
26#include <linux/debugfs.h>
27#include <linux/delay.h>
28#include <linux/err.h>
29#include <linux/export.h>
30#include <linux/init.h>
31#include <linux/io.h>
Jon Hunter0a2d87e2016-02-26 15:48:40 +000032#include <linux/iopoll.h>
Thierry Reding19906e62018-09-17 15:08:17 +020033#include <linux/irq.h>
34#include <linux/irqdomain.h>
Thierry Reding72323982014-07-11 13:19:06 +020035#include <linux/of.h>
36#include <linux/of_address.h>
Geert Uytterhoeven3fd01212018-04-18 16:50:04 +020037#include <linux/of_clk.h>
Thierry Reding19906e62018-09-17 15:08:17 +020038#include <linux/of_irq.h>
Jon Huntera3804512016-03-30 10:15:15 +010039#include <linux/of_platform.h>
Aapo Vienamo4a37f112018-08-10 21:08:12 +030040#include <linux/pinctrl/pinctrl.h>
41#include <linux/pinctrl/pinconf.h>
42#include <linux/pinctrl/pinconf-generic.h>
Thierry Reding72323982014-07-11 13:19:06 +020043#include <linux/platform_device.h>
Jon Huntera3804512016-03-30 10:15:15 +010044#include <linux/pm_domain.h>
Thierry Reding72323982014-07-11 13:19:06 +020045#include <linux/reboot.h>
46#include <linux/reset.h>
47#include <linux/seq_file.h>
Jon Huntera3804512016-03-30 10:15:15 +010048#include <linux/slab.h>
Thierry Reding72323982014-07-11 13:19:06 +020049#include <linux/spinlock.h>
50
51#include <soc/tegra/common.h>
52#include <soc/tegra/fuse.h>
53#include <soc/tegra/pmc.h>
54
Thierry Reding19906e62018-09-17 15:08:17 +020055#include <dt-bindings/interrupt-controller/arm-gic.h>
Aapo Vienamofccf0f72018-08-10 21:08:11 +030056#include <dt-bindings/pinctrl/pinctrl-tegra-io-pad.h>
Thierry Redinge59333c2018-09-19 18:41:59 +020057#include <dt-bindings/gpio/tegra186-gpio.h>
Aapo Vienamofccf0f72018-08-10 21:08:11 +030058
Thierry Reding72323982014-07-11 13:19:06 +020059#define PMC_CNTRL 0x0
Laxman Dewangan6c0bd212016-06-17 18:36:12 +053060#define PMC_CNTRL_INTR_POLARITY BIT(17) /* inverts INTR polarity */
Thierry Reding95b780b2016-10-10 13:13:36 +020061#define PMC_CNTRL_CPU_PWRREQ_OE BIT(16) /* CPU pwr req enable */
62#define PMC_CNTRL_CPU_PWRREQ_POLARITY BIT(15) /* CPU pwr req polarity */
63#define PMC_CNTRL_SIDE_EFFECT_LP0 BIT(14) /* LP0 when CPU pwr gated */
64#define PMC_CNTRL_SYSCLK_OE BIT(11) /* system clock enable */
65#define PMC_CNTRL_SYSCLK_POLARITY BIT(10) /* sys clk polarity */
66#define PMC_CNTRL_MAIN_RST BIT(4)
Thierry Reding72323982014-07-11 13:19:06 +020067
68#define DPD_SAMPLE 0x020
Laxman Dewangan6c0bd212016-06-17 18:36:12 +053069#define DPD_SAMPLE_ENABLE BIT(0)
Thierry Reding72323982014-07-11 13:19:06 +020070#define DPD_SAMPLE_DISABLE (0 << 0)
71
72#define PWRGATE_TOGGLE 0x30
Laxman Dewangan6c0bd212016-06-17 18:36:12 +053073#define PWRGATE_TOGGLE_START BIT(8)
Thierry Reding72323982014-07-11 13:19:06 +020074
75#define REMOVE_CLAMPING 0x34
76
77#define PWRGATE_STATUS 0x38
78
Aapo Vienamo13136a42018-08-10 21:08:07 +030079#define PMC_IMPL_E_33V_PWR 0x40
80
Laxman Dewangan21b49912016-10-10 15:14:34 +020081#define PMC_PWR_DET 0x48
82
Thierry Reding5be22552017-08-30 12:32:58 +020083#define PMC_SCRATCH0_MODE_RECOVERY BIT(31)
84#define PMC_SCRATCH0_MODE_BOOTLOADER BIT(30)
85#define PMC_SCRATCH0_MODE_RCM BIT(1)
86#define PMC_SCRATCH0_MODE_MASK (PMC_SCRATCH0_MODE_RECOVERY | \
Thierry Reding72323982014-07-11 13:19:06 +020087 PMC_SCRATCH0_MODE_BOOTLOADER | \
88 PMC_SCRATCH0_MODE_RCM)
89
90#define PMC_CPUPWRGOOD_TIMER 0xc8
91#define PMC_CPUPWROFF_TIMER 0xcc
92
Laxman Dewangan21b49912016-10-10 15:14:34 +020093#define PMC_PWR_DET_VALUE 0xe4
94
Thierry Reding72323982014-07-11 13:19:06 +020095#define PMC_SCRATCH41 0x140
96
Mikko Perttunen3568df32015-01-06 12:52:58 +020097#define PMC_SENSOR_CTRL 0x1b0
Laxman Dewangan6c0bd212016-06-17 18:36:12 +053098#define PMC_SENSOR_CTRL_SCRATCH_WRITE BIT(2)
99#define PMC_SENSOR_CTRL_ENABLE_RST BIT(1)
Mikko Perttunen3568df32015-01-06 12:52:58 +0200100
Thierry Redingf5353c62015-12-30 17:13:29 +0100101#define PMC_RST_STATUS_POR 0
102#define PMC_RST_STATUS_WATCHDOG 1
103#define PMC_RST_STATUS_SENSOR 2
104#define PMC_RST_STATUS_SW_MAIN 3
105#define PMC_RST_STATUS_LP0 4
106#define PMC_RST_STATUS_AOTAG 5
107
Thierry Reding72323982014-07-11 13:19:06 +0200108#define IO_DPD_REQ 0x1b8
Laxman Dewangan6c0bd212016-06-17 18:36:12 +0530109#define IO_DPD_REQ_CODE_IDLE (0U << 30)
110#define IO_DPD_REQ_CODE_OFF (1U << 30)
111#define IO_DPD_REQ_CODE_ON (2U << 30)
112#define IO_DPD_REQ_CODE_MASK (3U << 30)
Thierry Reding72323982014-07-11 13:19:06 +0200113
114#define IO_DPD_STATUS 0x1bc
115#define IO_DPD2_REQ 0x1c0
116#define IO_DPD2_STATUS 0x1c4
117#define SEL_DPD_TIM 0x1c8
118
Mikko Perttunen3568df32015-01-06 12:52:58 +0200119#define PMC_SCRATCH54 0x258
Laxman Dewangan6c0bd212016-06-17 18:36:12 +0530120#define PMC_SCRATCH54_DATA_SHIFT 8
121#define PMC_SCRATCH54_ADDR_SHIFT 0
Mikko Perttunen3568df32015-01-06 12:52:58 +0200122
123#define PMC_SCRATCH55 0x25c
Laxman Dewangan6c0bd212016-06-17 18:36:12 +0530124#define PMC_SCRATCH55_RESET_TEGRA BIT(31)
125#define PMC_SCRATCH55_CNTRL_ID_SHIFT 27
126#define PMC_SCRATCH55_PINMUX_SHIFT 24
127#define PMC_SCRATCH55_16BITOP BIT(15)
128#define PMC_SCRATCH55_CHECKSUM_SHIFT 16
129#define PMC_SCRATCH55_I2CSLV1_SHIFT 0
Mikko Perttunen3568df32015-01-06 12:52:58 +0200130
Thierry Reding72323982014-07-11 13:19:06 +0200131#define GPU_RG_CNTRL 0x2d4
132
Thierry Redingc641ec62017-08-30 12:42:34 +0200133/* Tegra186 and later */
Thierry Reding19906e62018-09-17 15:08:17 +0200134#define WAKE_AOWAKE_CNTRL(x) (0x000 + ((x) << 2))
135#define WAKE_AOWAKE_CNTRL_LEVEL (1 << 3)
136#define WAKE_AOWAKE_MASK_W(x) (0x180 + ((x) << 2))
137#define WAKE_AOWAKE_MASK_R(x) (0x300 + ((x) << 2))
138#define WAKE_AOWAKE_STATUS_W(x) (0x30c + ((x) << 2))
139#define WAKE_AOWAKE_STATUS_R(x) (0x48c + ((x) << 2))
140#define WAKE_AOWAKE_TIER0_ROUTING(x) (0x4b4 + ((x) << 2))
141#define WAKE_AOWAKE_TIER1_ROUTING(x) (0x4c0 + ((x) << 2))
142#define WAKE_AOWAKE_TIER2_ROUTING(x) (0x4cc + ((x) << 2))
143
Thierry Redingc641ec62017-08-30 12:42:34 +0200144#define WAKE_AOWAKE_CTRL 0x4f4
145#define WAKE_AOWAKE_CTRL_INTR_POLARITY BIT(0)
146
Jon Huntera3804512016-03-30 10:15:15 +0100147struct tegra_powergate {
148 struct generic_pm_domain genpd;
149 struct tegra_pmc *pmc;
150 unsigned int id;
151 struct clk **clks;
152 unsigned int num_clks;
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200153 struct reset_control *reset;
Jon Huntera3804512016-03-30 10:15:15 +0100154};
155
Laxman Dewangan21b49912016-10-10 15:14:34 +0200156struct tegra_io_pad_soc {
157 enum tegra_io_pad id;
158 unsigned int dpd;
159 unsigned int voltage;
Aapo Vienamo437c4f22018-08-10 21:08:10 +0300160 const char *name;
Laxman Dewangan21b49912016-10-10 15:14:34 +0200161};
162
Thierry Reding5be22552017-08-30 12:32:58 +0200163struct tegra_pmc_regs {
164 unsigned int scratch0;
165 unsigned int dpd_req;
166 unsigned int dpd_status;
167 unsigned int dpd2_req;
168 unsigned int dpd2_status;
Sandipan Patra5f84bb12018-10-24 12:38:00 +0530169 unsigned int rst_status;
170 unsigned int rst_source_shift;
171 unsigned int rst_source_mask;
172 unsigned int rst_level_shift;
173 unsigned int rst_level_mask;
Thierry Reding5be22552017-08-30 12:32:58 +0200174};
175
Thierry Reding19906e62018-09-17 15:08:17 +0200176struct tegra_wake_event {
177 const char *name;
178 unsigned int id;
179 unsigned int irq;
180 struct {
181 unsigned int instance;
182 unsigned int pin;
183 } gpio;
184};
185
186#define TEGRA_WAKE_IRQ(_name, _id, _irq) \
187 { \
188 .name = _name, \
189 .id = _id, \
190 .irq = _irq, \
191 .gpio = { \
192 .instance = UINT_MAX, \
193 .pin = UINT_MAX, \
194 }, \
195 }
196
197#define TEGRA_WAKE_GPIO(_name, _id, _instance, _pin) \
198 { \
199 .name = _name, \
200 .id = _id, \
201 .irq = 0, \
202 .gpio = { \
203 .instance = _instance, \
204 .pin = _pin, \
205 }, \
206 }
207
Thierry Reding72323982014-07-11 13:19:06 +0200208struct tegra_pmc_soc {
209 unsigned int num_powergates;
210 const char *const *powergates;
211 unsigned int num_cpu_powergates;
212 const u8 *cpu_powergates;
Thierry Redinga9a40a42015-01-09 11:15:33 +0100213
Mikko Perttunen3568df32015-01-06 12:52:58 +0200214 bool has_tsense_reset;
Thierry Redinga9a40a42015-01-09 11:15:33 +0100215 bool has_gpu_clamps;
Peter De Schrijvera263394a2018-01-25 16:00:13 +0200216 bool needs_mbist_war;
Aapo Vienamo13136a42018-08-10 21:08:07 +0300217 bool has_impl_33v_pwr;
Laxman Dewangan21b49912016-10-10 15:14:34 +0200218
219 const struct tegra_io_pad_soc *io_pads;
220 unsigned int num_io_pads;
Thierry Reding5be22552017-08-30 12:32:58 +0200221
Aapo Vienamo4a37f112018-08-10 21:08:12 +0300222 const struct pinctrl_pin_desc *pin_descs;
223 unsigned int num_pin_descs;
224
Thierry Reding5be22552017-08-30 12:32:58 +0200225 const struct tegra_pmc_regs *regs;
226 void (*init)(struct tegra_pmc *pmc);
227 void (*setup_irq_polarity)(struct tegra_pmc *pmc,
228 struct device_node *np,
229 bool invert);
Sandipan Patra5f84bb12018-10-24 12:38:00 +0530230
231 const char * const *reset_sources;
232 unsigned int num_reset_sources;
233 const char * const *reset_levels;
234 unsigned int num_reset_levels;
Thierry Reding19906e62018-09-17 15:08:17 +0200235
236 const struct tegra_wake_event *wake_events;
237 unsigned int num_wake_events;
Sandipan Patra5f84bb12018-10-24 12:38:00 +0530238};
239
240static const char * const tegra186_reset_sources[] = {
241 "SYS_RESET",
242 "AOWDT",
243 "MCCPLEXWDT",
244 "BPMPWDT",
245 "SCEWDT",
246 "SPEWDT",
247 "APEWDT",
248 "BCCPLEXWDT",
249 "SENSOR",
250 "AOTAG",
251 "VFSENSOR",
252 "SWREST",
253 "SC7",
254 "HSM",
255 "CORESIGHT"
256};
257
258static const char * const tegra186_reset_levels[] = {
259 "L0", "L1", "L2", "WARM"
260};
261
262static const char * const tegra30_reset_sources[] = {
263 "POWER_ON_RESET",
264 "WATCHDOG",
265 "SENSOR",
266 "SW_MAIN",
267 "LP0",
268 "AOTAG"
Thierry Reding72323982014-07-11 13:19:06 +0200269};
270
271/**
272 * struct tegra_pmc - NVIDIA Tegra PMC
Jon Hunter35b67292015-12-04 14:57:03 +0000273 * @dev: pointer to PMC device structure
Thierry Reding72323982014-07-11 13:19:06 +0200274 * @base: pointer to I/O remapped register region
275 * @clk: pointer to pclk clock
Jon Hunter35b67292015-12-04 14:57:03 +0000276 * @soc: pointer to SoC data structure
Jon Hunter3195ac62015-12-04 14:57:05 +0000277 * @debugfs: pointer to debugfs entry
Thierry Reding72323982014-07-11 13:19:06 +0200278 * @rate: currently configured rate of pclk
279 * @suspend_mode: lowest suspend mode available
280 * @cpu_good_time: CPU power good time (in microseconds)
281 * @cpu_off_time: CPU power off time (in microsecends)
282 * @core_osc_time: core power good OSC time (in microseconds)
283 * @core_pmu_time: core power good PMU time (in microseconds)
284 * @core_off_time: core power off time (in microseconds)
285 * @corereq_high: core power request is active-high
286 * @sysclkreq_high: system clock request is active-high
287 * @combined_req: combined power request for CPU & core
288 * @cpu_pwr_good_en: CPU power good signal is enabled
289 * @lp0_vec_phys: physical base address of the LP0 warm boot code
290 * @lp0_vec_size: size of the LP0 warm boot code
Jon Huntera3804512016-03-30 10:15:15 +0100291 * @powergates_available: Bitmap of available power gates
Thierry Reding72323982014-07-11 13:19:06 +0200292 * @powergates_lock: mutex for power gate register access
293 */
294struct tegra_pmc {
Mikko Perttunen3568df32015-01-06 12:52:58 +0200295 struct device *dev;
Thierry Reding72323982014-07-11 13:19:06 +0200296 void __iomem *base;
Thierry Redingc641ec62017-08-30 12:42:34 +0200297 void __iomem *wake;
298 void __iomem *aotag;
Thierry Reding5be22552017-08-30 12:32:58 +0200299 void __iomem *scratch;
Thierry Reding72323982014-07-11 13:19:06 +0200300 struct clk *clk;
Jon Hunter3195ac62015-12-04 14:57:05 +0000301 struct dentry *debugfs;
Thierry Reding72323982014-07-11 13:19:06 +0200302
303 const struct tegra_pmc_soc *soc;
304
305 unsigned long rate;
306
307 enum tegra_suspend_mode suspend_mode;
308 u32 cpu_good_time;
309 u32 cpu_off_time;
310 u32 core_osc_time;
311 u32 core_pmu_time;
312 u32 core_off_time;
313 bool corereq_high;
314 bool sysclkreq_high;
315 bool combined_req;
316 bool cpu_pwr_good_en;
317 u32 lp0_vec_phys;
318 u32 lp0_vec_size;
Jon Huntera3804512016-03-30 10:15:15 +0100319 DECLARE_BITMAP(powergates_available, TEGRA_POWERGATE_MAX);
Thierry Reding72323982014-07-11 13:19:06 +0200320
321 struct mutex powergates_lock;
Aapo Vienamo4a37f112018-08-10 21:08:12 +0300322
323 struct pinctrl_dev *pctl_dev;
Thierry Reding19906e62018-09-17 15:08:17 +0200324
325 struct irq_domain *domain;
326 struct irq_chip irq;
Thierry Reding72323982014-07-11 13:19:06 +0200327};
328
329static struct tegra_pmc *pmc = &(struct tegra_pmc) {
330 .base = NULL,
331 .suspend_mode = TEGRA_SUSPEND_NONE,
332};
333
Jon Huntera3804512016-03-30 10:15:15 +0100334static inline struct tegra_powergate *
335to_powergate(struct generic_pm_domain *domain)
336{
337 return container_of(domain, struct tegra_powergate, genpd);
338}
339
Thierry Reding72323982014-07-11 13:19:06 +0200340static u32 tegra_pmc_readl(unsigned long offset)
341{
342 return readl(pmc->base + offset);
343}
344
345static void tegra_pmc_writel(u32 value, unsigned long offset)
346{
347 writel(value, pmc->base + offset);
348}
349
Jon Hunter0ecf2d32016-02-11 18:03:23 +0000350static inline bool tegra_powergate_state(int id)
351{
Jon Hunterbc9af232016-02-15 12:38:11 +0000352 if (id == TEGRA_POWERGATE_3D && pmc->soc->has_gpu_clamps)
353 return (tegra_pmc_readl(GPU_RG_CNTRL) & 0x1) == 0;
354 else
355 return (tegra_pmc_readl(PWRGATE_STATUS) & BIT(id)) != 0;
Jon Hunter0ecf2d32016-02-11 18:03:23 +0000356}
357
Jon Hunter0a243bd2016-02-11 18:03:24 +0000358static inline bool tegra_powergate_is_valid(int id)
359{
360 return (pmc->soc && pmc->soc->powergates[id]);
361}
362
Jon Huntera3804512016-03-30 10:15:15 +0100363static inline bool tegra_powergate_is_available(int id)
364{
365 return test_bit(id, pmc->powergates_available);
366}
367
368static int tegra_powergate_lookup(struct tegra_pmc *pmc, const char *name)
369{
370 unsigned int i;
371
372 if (!pmc || !pmc->soc || !name)
373 return -EINVAL;
374
375 for (i = 0; i < pmc->soc->num_powergates; i++) {
376 if (!tegra_powergate_is_valid(i))
377 continue;
378
379 if (!strcmp(name, pmc->soc->powergates[i]))
380 return i;
381 }
382
Jon Huntera3804512016-03-30 10:15:15 +0100383 return -ENODEV;
384}
385
Thierry Reding72323982014-07-11 13:19:06 +0200386/**
387 * tegra_powergate_set() - set the state of a partition
388 * @id: partition ID
389 * @new_state: new state of the partition
390 */
Jon Hunter70293ed2016-02-11 18:03:22 +0000391static int tegra_powergate_set(unsigned int id, bool new_state)
Thierry Reding72323982014-07-11 13:19:06 +0200392{
Jon Hunter0a2d87e2016-02-26 15:48:40 +0000393 bool status;
394 int err;
395
Jon Hunterbc9af232016-02-15 12:38:11 +0000396 if (id == TEGRA_POWERGATE_3D && pmc->soc->has_gpu_clamps)
397 return -EINVAL;
398
Thierry Reding72323982014-07-11 13:19:06 +0200399 mutex_lock(&pmc->powergates_lock);
400
Jon Hunter0ecf2d32016-02-11 18:03:23 +0000401 if (tegra_powergate_state(id) == new_state) {
Thierry Reding72323982014-07-11 13:19:06 +0200402 mutex_unlock(&pmc->powergates_lock);
403 return 0;
404 }
405
406 tegra_pmc_writel(PWRGATE_TOGGLE_START | id, PWRGATE_TOGGLE);
407
Jon Hunter0a2d87e2016-02-26 15:48:40 +0000408 err = readx_poll_timeout(tegra_powergate_state, id, status,
409 status == new_state, 10, 100000);
410
Thierry Reding72323982014-07-11 13:19:06 +0200411 mutex_unlock(&pmc->powergates_lock);
412
Jon Hunter0a2d87e2016-02-26 15:48:40 +0000413 return err;
Thierry Reding72323982014-07-11 13:19:06 +0200414}
415
Jon Huntera3804512016-03-30 10:15:15 +0100416static int __tegra_powergate_remove_clamping(unsigned int id)
Thierry Reding72323982014-07-11 13:19:06 +0200417{
418 u32 mask;
419
Jon Huntere8cf6612016-02-11 18:03:21 +0000420 mutex_lock(&pmc->powergates_lock);
421
Thierry Reding72323982014-07-11 13:19:06 +0200422 /*
Thierry Redinga9a40a42015-01-09 11:15:33 +0100423 * On Tegra124 and later, the clamps for the GPU are controlled by a
424 * separate register (with different semantics).
Thierry Reding72323982014-07-11 13:19:06 +0200425 */
Thierry Redinga9a40a42015-01-09 11:15:33 +0100426 if (id == TEGRA_POWERGATE_3D) {
427 if (pmc->soc->has_gpu_clamps) {
Thierry Reding72323982014-07-11 13:19:06 +0200428 tegra_pmc_writel(0, GPU_RG_CNTRL);
Jon Huntere8cf6612016-02-11 18:03:21 +0000429 goto out;
Thierry Reding72323982014-07-11 13:19:06 +0200430 }
431 }
432
433 /*
434 * Tegra 2 has a bug where PCIE and VDE clamping masks are
435 * swapped relatively to the partition ids
436 */
437 if (id == TEGRA_POWERGATE_VDEC)
438 mask = (1 << TEGRA_POWERGATE_PCIE);
439 else if (id == TEGRA_POWERGATE_PCIE)
440 mask = (1 << TEGRA_POWERGATE_VDEC);
441 else
442 mask = (1 << id);
443
444 tegra_pmc_writel(mask, REMOVE_CLAMPING);
445
Jon Huntere8cf6612016-02-11 18:03:21 +0000446out:
447 mutex_unlock(&pmc->powergates_lock);
448
Thierry Reding72323982014-07-11 13:19:06 +0200449 return 0;
450}
Jon Huntera3804512016-03-30 10:15:15 +0100451
452static void tegra_powergate_disable_clocks(struct tegra_powergate *pg)
453{
454 unsigned int i;
455
456 for (i = 0; i < pg->num_clks; i++)
457 clk_disable_unprepare(pg->clks[i]);
458}
459
460static int tegra_powergate_enable_clocks(struct tegra_powergate *pg)
461{
462 unsigned int i;
463 int err;
464
465 for (i = 0; i < pg->num_clks; i++) {
466 err = clk_prepare_enable(pg->clks[i]);
467 if (err)
468 goto out;
469 }
470
471 return 0;
472
473out:
474 while (i--)
475 clk_disable_unprepare(pg->clks[i]);
476
477 return err;
478}
479
Peter De Schrijvera263394a2018-01-25 16:00:13 +0200480int __weak tegra210_clk_handle_mbist_war(unsigned int id)
481{
482 return 0;
483}
484
Jon Huntera3804512016-03-30 10:15:15 +0100485static int tegra_powergate_power_up(struct tegra_powergate *pg,
486 bool disable_clocks)
487{
488 int err;
489
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200490 err = reset_control_assert(pg->reset);
Jon Huntera3804512016-03-30 10:15:15 +0100491 if (err)
492 return err;
493
494 usleep_range(10, 20);
495
496 err = tegra_powergate_set(pg->id, true);
497 if (err < 0)
498 return err;
499
500 usleep_range(10, 20);
501
502 err = tegra_powergate_enable_clocks(pg);
503 if (err)
504 goto disable_clks;
505
506 usleep_range(10, 20);
507
508 err = __tegra_powergate_remove_clamping(pg->id);
509 if (err)
510 goto disable_clks;
511
512 usleep_range(10, 20);
513
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200514 err = reset_control_deassert(pg->reset);
Jon Huntera3804512016-03-30 10:15:15 +0100515 if (err)
516 goto powergate_off;
517
518 usleep_range(10, 20);
519
Peter De Schrijvera263394a2018-01-25 16:00:13 +0200520 if (pg->pmc->soc->needs_mbist_war)
521 err = tegra210_clk_handle_mbist_war(pg->id);
522 if (err)
523 goto disable_clks;
524
Jon Huntera3804512016-03-30 10:15:15 +0100525 if (disable_clocks)
526 tegra_powergate_disable_clocks(pg);
527
528 return 0;
529
530disable_clks:
531 tegra_powergate_disable_clocks(pg);
532 usleep_range(10, 20);
Thierry Redingda8f4b42016-06-30 12:12:55 +0200533
Jon Huntera3804512016-03-30 10:15:15 +0100534powergate_off:
535 tegra_powergate_set(pg->id, false);
536
537 return err;
538}
539
540static int tegra_powergate_power_down(struct tegra_powergate *pg)
541{
542 int err;
543
544 err = tegra_powergate_enable_clocks(pg);
545 if (err)
546 return err;
547
548 usleep_range(10, 20);
549
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200550 err = reset_control_assert(pg->reset);
Jon Huntera3804512016-03-30 10:15:15 +0100551 if (err)
552 goto disable_clks;
553
554 usleep_range(10, 20);
555
556 tegra_powergate_disable_clocks(pg);
557
558 usleep_range(10, 20);
559
560 err = tegra_powergate_set(pg->id, false);
561 if (err)
562 goto assert_resets;
563
564 return 0;
565
566assert_resets:
567 tegra_powergate_enable_clocks(pg);
568 usleep_range(10, 20);
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200569 reset_control_deassert(pg->reset);
Jon Huntera3804512016-03-30 10:15:15 +0100570 usleep_range(10, 20);
Thierry Redingda8f4b42016-06-30 12:12:55 +0200571
Jon Huntera3804512016-03-30 10:15:15 +0100572disable_clks:
573 tegra_powergate_disable_clocks(pg);
574
575 return err;
576}
577
578static int tegra_genpd_power_on(struct generic_pm_domain *domain)
579{
580 struct tegra_powergate *pg = to_powergate(domain);
Jon Huntera3804512016-03-30 10:15:15 +0100581 int err;
582
583 err = tegra_powergate_power_up(pg, true);
584 if (err)
Thierry Reding54e24722016-11-08 10:58:32 +0100585 pr_err("failed to turn on PM domain %s: %d\n", pg->genpd.name,
586 err);
Jon Huntera3804512016-03-30 10:15:15 +0100587
588 return err;
589}
590
591static int tegra_genpd_power_off(struct generic_pm_domain *domain)
592{
593 struct tegra_powergate *pg = to_powergate(domain);
Jon Huntera3804512016-03-30 10:15:15 +0100594 int err;
595
596 err = tegra_powergate_power_down(pg);
597 if (err)
Thierry Reding54e24722016-11-08 10:58:32 +0100598 pr_err("failed to turn off PM domain %s: %d\n",
599 pg->genpd.name, err);
Jon Huntera3804512016-03-30 10:15:15 +0100600
601 return err;
602}
603
604/**
605 * tegra_powergate_power_on() - power on partition
606 * @id: partition ID
607 */
608int tegra_powergate_power_on(unsigned int id)
609{
610 if (!tegra_powergate_is_available(id))
611 return -EINVAL;
612
613 return tegra_powergate_set(id, true);
614}
615
616/**
617 * tegra_powergate_power_off() - power off partition
618 * @id: partition ID
619 */
620int tegra_powergate_power_off(unsigned int id)
621{
622 if (!tegra_powergate_is_available(id))
623 return -EINVAL;
624
625 return tegra_powergate_set(id, false);
626}
627EXPORT_SYMBOL(tegra_powergate_power_off);
628
629/**
630 * tegra_powergate_is_powered() - check if partition is powered
631 * @id: partition ID
632 */
633int tegra_powergate_is_powered(unsigned int id)
634{
Jon Huntera3804512016-03-30 10:15:15 +0100635 if (!tegra_powergate_is_valid(id))
636 return -EINVAL;
637
Dmitry Osipenkob6e1fd12018-10-21 21:36:14 +0300638 return tegra_powergate_state(id);
Jon Huntera3804512016-03-30 10:15:15 +0100639}
640
641/**
642 * tegra_powergate_remove_clamping() - remove power clamps for partition
643 * @id: partition ID
644 */
645int tegra_powergate_remove_clamping(unsigned int id)
646{
647 if (!tegra_powergate_is_available(id))
648 return -EINVAL;
649
650 return __tegra_powergate_remove_clamping(id);
651}
Thierry Reding72323982014-07-11 13:19:06 +0200652EXPORT_SYMBOL(tegra_powergate_remove_clamping);
653
654/**
655 * tegra_powergate_sequence_power_up() - power up partition
656 * @id: partition ID
657 * @clk: clock for partition
658 * @rst: reset for partition
659 *
660 * Must be called with clk disabled, and returns with clk enabled.
661 */
Jon Hunter70293ed2016-02-11 18:03:22 +0000662int tegra_powergate_sequence_power_up(unsigned int id, struct clk *clk,
Thierry Reding72323982014-07-11 13:19:06 +0200663 struct reset_control *rst)
664{
Viresh Kumar495ac332018-05-03 13:56:17 +0530665 struct tegra_powergate *pg;
Jon Huntera3804512016-03-30 10:15:15 +0100666 int err;
Thierry Reding72323982014-07-11 13:19:06 +0200667
Jon Hunter403db2d2016-06-28 11:38:23 +0100668 if (!tegra_powergate_is_available(id))
669 return -EINVAL;
670
Viresh Kumar495ac332018-05-03 13:56:17 +0530671 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
672 if (!pg)
673 return -ENOMEM;
Thierry Reding72323982014-07-11 13:19:06 +0200674
Viresh Kumar495ac332018-05-03 13:56:17 +0530675 pg->id = id;
676 pg->clks = &clk;
677 pg->num_clks = 1;
678 pg->reset = rst;
679 pg->pmc = pmc;
680
681 err = tegra_powergate_power_up(pg, false);
Jon Huntera3804512016-03-30 10:15:15 +0100682 if (err)
683 pr_err("failed to turn on partition %d: %d\n", id, err);
Thierry Reding72323982014-07-11 13:19:06 +0200684
Viresh Kumar495ac332018-05-03 13:56:17 +0530685 kfree(pg);
686
Jon Huntera3804512016-03-30 10:15:15 +0100687 return err;
Thierry Reding72323982014-07-11 13:19:06 +0200688}
689EXPORT_SYMBOL(tegra_powergate_sequence_power_up);
690
691#ifdef CONFIG_SMP
692/**
693 * tegra_get_cpu_powergate_id() - convert from CPU ID to partition ID
694 * @cpuid: CPU partition ID
695 *
696 * Returns the partition ID corresponding to the CPU partition ID or a
697 * negative error code on failure.
698 */
Jon Hunter70293ed2016-02-11 18:03:22 +0000699static int tegra_get_cpu_powergate_id(unsigned int cpuid)
Thierry Reding72323982014-07-11 13:19:06 +0200700{
Jon Hunter70293ed2016-02-11 18:03:22 +0000701 if (pmc->soc && cpuid < pmc->soc->num_cpu_powergates)
Thierry Reding72323982014-07-11 13:19:06 +0200702 return pmc->soc->cpu_powergates[cpuid];
703
704 return -EINVAL;
705}
706
707/**
708 * tegra_pmc_cpu_is_powered() - check if CPU partition is powered
709 * @cpuid: CPU partition ID
710 */
Jon Hunter70293ed2016-02-11 18:03:22 +0000711bool tegra_pmc_cpu_is_powered(unsigned int cpuid)
Thierry Reding72323982014-07-11 13:19:06 +0200712{
713 int id;
714
715 id = tegra_get_cpu_powergate_id(cpuid);
716 if (id < 0)
717 return false;
718
719 return tegra_powergate_is_powered(id);
720}
721
722/**
723 * tegra_pmc_cpu_power_on() - power on CPU partition
724 * @cpuid: CPU partition ID
725 */
Jon Hunter70293ed2016-02-11 18:03:22 +0000726int tegra_pmc_cpu_power_on(unsigned int cpuid)
Thierry Reding72323982014-07-11 13:19:06 +0200727{
728 int id;
729
730 id = tegra_get_cpu_powergate_id(cpuid);
731 if (id < 0)
732 return id;
733
734 return tegra_powergate_set(id, true);
735}
736
737/**
738 * tegra_pmc_cpu_remove_clamping() - remove power clamps for CPU partition
739 * @cpuid: CPU partition ID
740 */
Jon Hunter70293ed2016-02-11 18:03:22 +0000741int tegra_pmc_cpu_remove_clamping(unsigned int cpuid)
Thierry Reding72323982014-07-11 13:19:06 +0200742{
743 int id;
744
745 id = tegra_get_cpu_powergate_id(cpuid);
746 if (id < 0)
747 return id;
748
749 return tegra_powergate_remove_clamping(id);
750}
751#endif /* CONFIG_SMP */
752
David Riley78921582015-03-18 10:52:25 +0100753static int tegra_pmc_restart_notify(struct notifier_block *this,
754 unsigned long action, void *data)
Thierry Reding72323982014-07-11 13:19:06 +0200755{
David Riley78921582015-03-18 10:52:25 +0100756 const char *cmd = data;
Thierry Reding72323982014-07-11 13:19:06 +0200757 u32 value;
758
Thierry Reding5be22552017-08-30 12:32:58 +0200759 value = readl(pmc->scratch + pmc->soc->regs->scratch0);
Thierry Reding72323982014-07-11 13:19:06 +0200760 value &= ~PMC_SCRATCH0_MODE_MASK;
761
762 if (cmd) {
763 if (strcmp(cmd, "recovery") == 0)
764 value |= PMC_SCRATCH0_MODE_RECOVERY;
765
766 if (strcmp(cmd, "bootloader") == 0)
767 value |= PMC_SCRATCH0_MODE_BOOTLOADER;
768
769 if (strcmp(cmd, "forced-recovery") == 0)
770 value |= PMC_SCRATCH0_MODE_RCM;
771 }
772
Thierry Reding5be22552017-08-30 12:32:58 +0200773 writel(value, pmc->scratch + pmc->soc->regs->scratch0);
Thierry Reding72323982014-07-11 13:19:06 +0200774
Thierry Redingf5353c62015-12-30 17:13:29 +0100775 /* reset everything but PMC_SCRATCH0 and PMC_RST_STATUS */
776 value = tegra_pmc_readl(PMC_CNTRL);
777 value |= PMC_CNTRL_MAIN_RST;
778 tegra_pmc_writel(value, PMC_CNTRL);
David Riley78921582015-03-18 10:52:25 +0100779
780 return NOTIFY_DONE;
Thierry Reding72323982014-07-11 13:19:06 +0200781}
782
David Riley78921582015-03-18 10:52:25 +0100783static struct notifier_block tegra_pmc_restart_handler = {
784 .notifier_call = tegra_pmc_restart_notify,
785 .priority = 128,
786};
787
Thierry Reding72323982014-07-11 13:19:06 +0200788static int powergate_show(struct seq_file *s, void *data)
789{
790 unsigned int i;
Jon Hunterc3ea2972016-02-11 18:03:25 +0000791 int status;
Thierry Reding72323982014-07-11 13:19:06 +0200792
793 seq_printf(s, " powergate powered\n");
794 seq_printf(s, "------------------\n");
795
796 for (i = 0; i < pmc->soc->num_powergates; i++) {
Jon Hunterc3ea2972016-02-11 18:03:25 +0000797 status = tegra_powergate_is_powered(i);
798 if (status < 0)
Thierry Reding72323982014-07-11 13:19:06 +0200799 continue;
800
801 seq_printf(s, " %9s %7s\n", pmc->soc->powergates[i],
Jon Hunterc3ea2972016-02-11 18:03:25 +0000802 status ? "yes" : "no");
Thierry Reding72323982014-07-11 13:19:06 +0200803 }
804
805 return 0;
806}
807
Yangtao Li57ba33d2018-11-22 08:12:07 -0500808DEFINE_SHOW_ATTRIBUTE(powergate);
Thierry Reding72323982014-07-11 13:19:06 +0200809
810static int tegra_powergate_debugfs_init(void)
811{
Jon Hunter3195ac62015-12-04 14:57:05 +0000812 pmc->debugfs = debugfs_create_file("powergate", S_IRUGO, NULL, NULL,
813 &powergate_fops);
814 if (!pmc->debugfs)
Thierry Reding72323982014-07-11 13:19:06 +0200815 return -ENOMEM;
816
817 return 0;
818}
819
Jon Huntera3804512016-03-30 10:15:15 +0100820static int tegra_powergate_of_get_clks(struct tegra_powergate *pg,
821 struct device_node *np)
822{
823 struct clk *clk;
824 unsigned int i, count;
825 int err;
826
Geert Uytterhoeven3fd01212018-04-18 16:50:04 +0200827 count = of_clk_get_parent_count(np);
Jon Huntera3804512016-03-30 10:15:15 +0100828 if (count == 0)
829 return -ENODEV;
830
831 pg->clks = kcalloc(count, sizeof(clk), GFP_KERNEL);
832 if (!pg->clks)
833 return -ENOMEM;
834
835 for (i = 0; i < count; i++) {
836 pg->clks[i] = of_clk_get(np, i);
837 if (IS_ERR(pg->clks[i])) {
838 err = PTR_ERR(pg->clks[i]);
839 goto err;
840 }
841 }
842
843 pg->num_clks = count;
844
845 return 0;
846
847err:
848 while (i--)
849 clk_put(pg->clks[i]);
Thierry Redingda8f4b42016-06-30 12:12:55 +0200850
Jon Huntera3804512016-03-30 10:15:15 +0100851 kfree(pg->clks);
852
853 return err;
854}
855
856static int tegra_powergate_of_get_resets(struct tegra_powergate *pg,
Jon Hunter05cfb982016-06-29 10:17:47 +0100857 struct device_node *np, bool off)
Jon Huntera3804512016-03-30 10:15:15 +0100858{
Jon Huntera3804512016-03-30 10:15:15 +0100859 int err;
860
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200861 pg->reset = of_reset_control_array_get_exclusive(np);
862 if (IS_ERR(pg->reset)) {
863 err = PTR_ERR(pg->reset);
864 pr_err("failed to get device resets: %d\n", err);
865 return err;
Jon Huntera3804512016-03-30 10:15:15 +0100866 }
867
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200868 if (off)
869 err = reset_control_assert(pg->reset);
870 else
871 err = reset_control_deassert(pg->reset);
Jon Huntera3804512016-03-30 10:15:15 +0100872
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200873 if (err)
874 reset_control_put(pg->reset);
Jon Huntera3804512016-03-30 10:15:15 +0100875
876 return err;
877}
878
879static void tegra_powergate_add(struct tegra_pmc *pmc, struct device_node *np)
880{
881 struct tegra_powergate *pg;
Jon Hunterc2710ac2016-06-30 11:56:24 +0100882 int id, err;
Jon Huntera3804512016-03-30 10:15:15 +0100883 bool off;
Jon Huntera3804512016-03-30 10:15:15 +0100884
885 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
886 if (!pg)
Jon Hunterc2710ac2016-06-30 11:56:24 +0100887 return;
Jon Huntera3804512016-03-30 10:15:15 +0100888
889 id = tegra_powergate_lookup(pmc, np->name);
Jon Hunterc2710ac2016-06-30 11:56:24 +0100890 if (id < 0) {
Rob Herringdc37a252018-08-27 20:02:33 -0500891 pr_err("powergate lookup failed for %pOFn: %d\n", np, id);
Jon Huntera3804512016-03-30 10:15:15 +0100892 goto free_mem;
Jon Hunterc2710ac2016-06-30 11:56:24 +0100893 }
Jon Huntera3804512016-03-30 10:15:15 +0100894
895 /*
896 * Clear the bit for this powergate so it cannot be managed
897 * directly via the legacy APIs for controlling powergates.
898 */
899 clear_bit(id, pmc->powergates_available);
900
901 pg->id = id;
902 pg->genpd.name = np->name;
903 pg->genpd.power_off = tegra_genpd_power_off;
904 pg->genpd.power_on = tegra_genpd_power_on;
905 pg->pmc = pmc;
906
Jon Hunter05cfb982016-06-29 10:17:47 +0100907 off = !tegra_powergate_is_powered(pg->id);
908
Jon Hunterc2710ac2016-06-30 11:56:24 +0100909 err = tegra_powergate_of_get_clks(pg, np);
910 if (err < 0) {
Rob Herringdc37a252018-08-27 20:02:33 -0500911 pr_err("failed to get clocks for %pOFn: %d\n", np, err);
Jon Huntera3804512016-03-30 10:15:15 +0100912 goto set_available;
Jon Hunterc2710ac2016-06-30 11:56:24 +0100913 }
Jon Huntera3804512016-03-30 10:15:15 +0100914
Jon Hunterc2710ac2016-06-30 11:56:24 +0100915 err = tegra_powergate_of_get_resets(pg, np, off);
916 if (err < 0) {
Rob Herringdc37a252018-08-27 20:02:33 -0500917 pr_err("failed to get resets for %pOFn: %d\n", np, err);
Jon Huntera3804512016-03-30 10:15:15 +0100918 goto remove_clks;
Jon Hunterc2710ac2016-06-30 11:56:24 +0100919 }
Jon Huntera3804512016-03-30 10:15:15 +0100920
Jon Hunter0b137342016-10-22 20:23:56 +0100921 if (!IS_ENABLED(CONFIG_PM_GENERIC_DOMAINS)) {
922 if (off)
923 WARN_ON(tegra_powergate_power_up(pg, true));
924
925 goto remove_resets;
926 }
Jon Huntere2d17962016-06-30 11:56:25 +0100927
Jon Huntercd5ceda2016-10-22 20:23:55 +0100928 err = pm_genpd_init(&pg->genpd, NULL, off);
929 if (err < 0) {
Rob Herringdc37a252018-08-27 20:02:33 -0500930 pr_err("failed to initialise PM domain %pOFn: %d\n", np,
Jon Huntercd5ceda2016-10-22 20:23:55 +0100931 err);
932 goto remove_resets;
933 }
Jon Huntera3804512016-03-30 10:15:15 +0100934
Jon Hunterc2710ac2016-06-30 11:56:24 +0100935 err = of_genpd_add_provider_simple(np, &pg->genpd);
936 if (err < 0) {
Rob Herringdc37a252018-08-27 20:02:33 -0500937 pr_err("failed to add PM domain provider for %pOFn: %d\n",
938 np, err);
Jon Hunter0b137342016-10-22 20:23:56 +0100939 goto remove_genpd;
Jon Hunterc2710ac2016-06-30 11:56:24 +0100940 }
Jon Huntera3804512016-03-30 10:15:15 +0100941
Thierry Reding45221122016-11-08 11:05:03 +0100942 pr_debug("added PM domain %s\n", pg->genpd.name);
Jon Huntera3804512016-03-30 10:15:15 +0100943
944 return;
945
Jon Hunter0b137342016-10-22 20:23:56 +0100946remove_genpd:
947 pm_genpd_remove(&pg->genpd);
Jon Huntere2d17962016-06-30 11:56:25 +0100948
Jon Huntera3804512016-03-30 10:15:15 +0100949remove_resets:
Vivek Gautam4c817cc2017-07-19 17:59:08 +0200950 reset_control_put(pg->reset);
Jon Huntera3804512016-03-30 10:15:15 +0100951
952remove_clks:
953 while (pg->num_clks--)
954 clk_put(pg->clks[pg->num_clks]);
Thierry Redingda8f4b42016-06-30 12:12:55 +0200955
Jon Huntera3804512016-03-30 10:15:15 +0100956 kfree(pg->clks);
957
958set_available:
959 set_bit(id, pmc->powergates_available);
960
961free_mem:
962 kfree(pg);
Jon Huntera3804512016-03-30 10:15:15 +0100963}
964
Jon Huntere2d17962016-06-30 11:56:25 +0100965static void tegra_powergate_init(struct tegra_pmc *pmc,
966 struct device_node *parent)
Jon Huntera3804512016-03-30 10:15:15 +0100967{
968 struct device_node *np, *child;
Jon Huntere2d17962016-06-30 11:56:25 +0100969 unsigned int i;
Jon Huntera3804512016-03-30 10:15:15 +0100970
Jon Huntere2d17962016-06-30 11:56:25 +0100971 /* Create a bitmap of the available and valid partitions */
972 for (i = 0; i < pmc->soc->num_powergates; i++)
973 if (pmc->soc->powergates[i])
974 set_bit(i, pmc->powergates_available);
975
976 np = of_get_child_by_name(parent, "powergates");
Jon Huntera3804512016-03-30 10:15:15 +0100977 if (!np)
978 return;
979
Tuomas Tynkkynen0c106e52017-07-29 02:58:43 +0300980 for_each_child_of_node(np, child)
Jon Huntera3804512016-03-30 10:15:15 +0100981 tegra_powergate_add(pmc, child);
Jon Huntera3804512016-03-30 10:15:15 +0100982
983 of_node_put(np);
984}
985
Laxman Dewangan21b49912016-10-10 15:14:34 +0200986static const struct tegra_io_pad_soc *
987tegra_io_pad_find(struct tegra_pmc *pmc, enum tegra_io_pad id)
Thierry Reding72323982014-07-11 13:19:06 +0200988{
Laxman Dewangan21b49912016-10-10 15:14:34 +0200989 unsigned int i;
990
991 for (i = 0; i < pmc->soc->num_io_pads; i++)
992 if (pmc->soc->io_pads[i].id == id)
993 return &pmc->soc->io_pads[i];
994
995 return NULL;
996}
997
Aapo Vienamo00ead3c2018-08-10 21:08:08 +0300998static int tegra_io_pad_get_dpd_register_bit(enum tegra_io_pad id,
999 unsigned long *request,
1000 unsigned long *status,
1001 u32 *mask)
Laxman Dewangan21b49912016-10-10 15:14:34 +02001002{
1003 const struct tegra_io_pad_soc *pad;
Thierry Reding72323982014-07-11 13:19:06 +02001004
Laxman Dewangan21b49912016-10-10 15:14:34 +02001005 pad = tegra_io_pad_find(pmc, id);
Thierry Reding54e24722016-11-08 10:58:32 +01001006 if (!pad) {
1007 pr_err("invalid I/O pad ID %u\n", id);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001008 return -ENOENT;
Thierry Reding54e24722016-11-08 10:58:32 +01001009 }
Thierry Reding72323982014-07-11 13:19:06 +02001010
Laxman Dewangan21b49912016-10-10 15:14:34 +02001011 if (pad->dpd == UINT_MAX)
1012 return -ENOTSUPP;
Thierry Reding72323982014-07-11 13:19:06 +02001013
Jon Hunter27b12b4e2016-10-22 20:23:53 +01001014 *mask = BIT(pad->dpd % 32);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001015
1016 if (pad->dpd < 32) {
Thierry Reding5be22552017-08-30 12:32:58 +02001017 *status = pmc->soc->regs->dpd_status;
1018 *request = pmc->soc->regs->dpd_req;
Thierry Reding72323982014-07-11 13:19:06 +02001019 } else {
Thierry Reding5be22552017-08-30 12:32:58 +02001020 *status = pmc->soc->regs->dpd2_status;
1021 *request = pmc->soc->regs->dpd2_req;
Thierry Reding72323982014-07-11 13:19:06 +02001022 }
1023
Aapo Vienamo00ead3c2018-08-10 21:08:08 +03001024 return 0;
1025}
1026
1027static int tegra_io_pad_prepare(enum tegra_io_pad id, unsigned long *request,
1028 unsigned long *status, u32 *mask)
1029{
1030 unsigned long rate, value;
1031 int err;
1032
1033 err = tegra_io_pad_get_dpd_register_bit(id, request, status, mask);
1034 if (err)
1035 return err;
1036
Thierry Reding5be22552017-08-30 12:32:58 +02001037 if (pmc->clk) {
1038 rate = clk_get_rate(pmc->clk);
1039 if (!rate) {
1040 pr_err("failed to get clock rate\n");
1041 return -ENODEV;
1042 }
1043
1044 tegra_pmc_writel(DPD_SAMPLE_ENABLE, DPD_SAMPLE);
1045
1046 /* must be at least 200 ns, in APB (PCLK) clock cycles */
1047 value = DIV_ROUND_UP(1000000000, rate);
1048 value = DIV_ROUND_UP(200, value);
1049 tegra_pmc_writel(value, SEL_DPD_TIM);
Thierry Reding54e24722016-11-08 10:58:32 +01001050 }
Thierry Reding72323982014-07-11 13:19:06 +02001051
Thierry Reding72323982014-07-11 13:19:06 +02001052 return 0;
1053}
1054
Laxman Dewangan21b49912016-10-10 15:14:34 +02001055static int tegra_io_pad_poll(unsigned long offset, u32 mask,
1056 u32 val, unsigned long timeout)
Thierry Reding72323982014-07-11 13:19:06 +02001057{
Laxman Dewangan84cf85e2016-06-17 18:36:13 +05301058 u32 value;
Thierry Reding72323982014-07-11 13:19:06 +02001059
1060 timeout = jiffies + msecs_to_jiffies(timeout);
1061
1062 while (time_after(timeout, jiffies)) {
1063 value = tegra_pmc_readl(offset);
1064 if ((value & mask) == val)
1065 return 0;
1066
1067 usleep_range(250, 1000);
1068 }
1069
1070 return -ETIMEDOUT;
1071}
1072
Laxman Dewangan21b49912016-10-10 15:14:34 +02001073static void tegra_io_pad_unprepare(void)
Thierry Reding72323982014-07-11 13:19:06 +02001074{
Thierry Reding5be22552017-08-30 12:32:58 +02001075 if (pmc->clk)
1076 tegra_pmc_writel(DPD_SAMPLE_DISABLE, DPD_SAMPLE);
Thierry Reding72323982014-07-11 13:19:06 +02001077}
1078
Laxman Dewangan21b49912016-10-10 15:14:34 +02001079/**
1080 * tegra_io_pad_power_enable() - enable power to I/O pad
1081 * @id: Tegra I/O pad ID for which to enable power
1082 *
1083 * Returns: 0 on success or a negative error code on failure.
1084 */
1085int tegra_io_pad_power_enable(enum tegra_io_pad id)
Thierry Reding72323982014-07-11 13:19:06 +02001086{
Vince Hsua9ccc1232016-08-11 09:13:36 +08001087 unsigned long request, status;
Jon Hunter27b12b4e2016-10-22 20:23:53 +01001088 u32 mask;
Thierry Reding72323982014-07-11 13:19:06 +02001089 int err;
1090
Jon Huntere8cf6612016-02-11 18:03:21 +00001091 mutex_lock(&pmc->powergates_lock);
1092
Jon Hunter27b12b4e2016-10-22 20:23:53 +01001093 err = tegra_io_pad_prepare(id, &request, &status, &mask);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001094 if (err < 0) {
Thierry Reding54e24722016-11-08 10:58:32 +01001095 pr_err("failed to prepare I/O pad: %d\n", err);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001096 goto unlock;
1097 }
Thierry Reding72323982014-07-11 13:19:06 +02001098
Jon Hunter27b12b4e2016-10-22 20:23:53 +01001099 tegra_pmc_writel(IO_DPD_REQ_CODE_OFF | mask, request);
Thierry Reding72323982014-07-11 13:19:06 +02001100
Jon Hunter27b12b4e2016-10-22 20:23:53 +01001101 err = tegra_io_pad_poll(status, mask, 0, 250);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001102 if (err < 0) {
Thierry Reding54e24722016-11-08 10:58:32 +01001103 pr_err("failed to enable I/O pad: %d\n", err);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001104 goto unlock;
Thierry Reding592431b2015-08-04 15:25:03 +02001105 }
Thierry Reding72323982014-07-11 13:19:06 +02001106
Laxman Dewangan21b49912016-10-10 15:14:34 +02001107 tegra_io_pad_unprepare();
Thierry Reding72323982014-07-11 13:19:06 +02001108
Laxman Dewangan21b49912016-10-10 15:14:34 +02001109unlock:
Jon Huntere8cf6612016-02-11 18:03:21 +00001110 mutex_unlock(&pmc->powergates_lock);
Jon Huntere8cf6612016-02-11 18:03:21 +00001111 return err;
Thierry Reding72323982014-07-11 13:19:06 +02001112}
Laxman Dewangan21b49912016-10-10 15:14:34 +02001113EXPORT_SYMBOL(tegra_io_pad_power_enable);
Thierry Reding72323982014-07-11 13:19:06 +02001114
Laxman Dewangan21b49912016-10-10 15:14:34 +02001115/**
1116 * tegra_io_pad_power_disable() - disable power to I/O pad
1117 * @id: Tegra I/O pad ID for which to disable power
1118 *
1119 * Returns: 0 on success or a negative error code on failure.
1120 */
1121int tegra_io_pad_power_disable(enum tegra_io_pad id)
Thierry Reding72323982014-07-11 13:19:06 +02001122{
Vince Hsua9ccc1232016-08-11 09:13:36 +08001123 unsigned long request, status;
Jon Hunter27b12b4e2016-10-22 20:23:53 +01001124 u32 mask;
Thierry Reding72323982014-07-11 13:19:06 +02001125 int err;
1126
Jon Huntere8cf6612016-02-11 18:03:21 +00001127 mutex_lock(&pmc->powergates_lock);
1128
Jon Hunter27b12b4e2016-10-22 20:23:53 +01001129 err = tegra_io_pad_prepare(id, &request, &status, &mask);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001130 if (err < 0) {
Thierry Reding54e24722016-11-08 10:58:32 +01001131 pr_err("failed to prepare I/O pad: %d\n", err);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001132 goto unlock;
Thierry Reding592431b2015-08-04 15:25:03 +02001133 }
Thierry Reding72323982014-07-11 13:19:06 +02001134
Jon Hunter27b12b4e2016-10-22 20:23:53 +01001135 tegra_pmc_writel(IO_DPD_REQ_CODE_ON | mask, request);
Thierry Reding72323982014-07-11 13:19:06 +02001136
Jon Hunter27b12b4e2016-10-22 20:23:53 +01001137 err = tegra_io_pad_poll(status, mask, mask, 250);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001138 if (err < 0) {
Thierry Reding54e24722016-11-08 10:58:32 +01001139 pr_err("failed to disable I/O pad: %d\n", err);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001140 goto unlock;
1141 }
Thierry Reding72323982014-07-11 13:19:06 +02001142
Laxman Dewangan21b49912016-10-10 15:14:34 +02001143 tegra_io_pad_unprepare();
Thierry Reding72323982014-07-11 13:19:06 +02001144
Laxman Dewangan21b49912016-10-10 15:14:34 +02001145unlock:
1146 mutex_unlock(&pmc->powergates_lock);
1147 return err;
1148}
1149EXPORT_SYMBOL(tegra_io_pad_power_disable);
1150
Aapo Vienamof142b9d2018-08-10 21:08:09 +03001151static int tegra_io_pad_is_powered(enum tegra_io_pad id)
1152{
1153 unsigned long request, status;
1154 u32 mask, value;
1155 int err;
1156
1157 err = tegra_io_pad_get_dpd_register_bit(id, &request, &status, &mask);
1158 if (err)
1159 return err;
1160
1161 value = tegra_pmc_readl(status);
1162
1163 return !(value & mask);
1164}
1165
Aapo Vienamofccf0f72018-08-10 21:08:11 +03001166static int tegra_io_pad_set_voltage(enum tegra_io_pad id, int voltage)
Laxman Dewangan21b49912016-10-10 15:14:34 +02001167{
1168 const struct tegra_io_pad_soc *pad;
1169 u32 value;
1170
1171 pad = tegra_io_pad_find(pmc, id);
1172 if (!pad)
1173 return -ENOENT;
1174
1175 if (pad->voltage == UINT_MAX)
1176 return -ENOTSUPP;
1177
1178 mutex_lock(&pmc->powergates_lock);
1179
Aapo Vienamo13136a42018-08-10 21:08:07 +03001180 if (pmc->soc->has_impl_33v_pwr) {
1181 value = tegra_pmc_readl(PMC_IMPL_E_33V_PWR);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001182
Aapo Vienamofccf0f72018-08-10 21:08:11 +03001183 if (voltage == TEGRA_IO_PAD_VOLTAGE_1V8)
Aapo Vienamo13136a42018-08-10 21:08:07 +03001184 value &= ~BIT(pad->voltage);
1185 else
1186 value |= BIT(pad->voltage);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001187
Aapo Vienamo13136a42018-08-10 21:08:07 +03001188 tegra_pmc_writel(value, PMC_IMPL_E_33V_PWR);
1189 } else {
1190 /* write-enable PMC_PWR_DET_VALUE[pad->voltage] */
1191 value = tegra_pmc_readl(PMC_PWR_DET);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001192 value |= BIT(pad->voltage);
Aapo Vienamo13136a42018-08-10 21:08:07 +03001193 tegra_pmc_writel(value, PMC_PWR_DET);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001194
Aapo Vienamo13136a42018-08-10 21:08:07 +03001195 /* update I/O voltage */
1196 value = tegra_pmc_readl(PMC_PWR_DET_VALUE);
1197
Aapo Vienamofccf0f72018-08-10 21:08:11 +03001198 if (voltage == TEGRA_IO_PAD_VOLTAGE_1V8)
Aapo Vienamo13136a42018-08-10 21:08:07 +03001199 value &= ~BIT(pad->voltage);
1200 else
1201 value |= BIT(pad->voltage);
1202
1203 tegra_pmc_writel(value, PMC_PWR_DET_VALUE);
1204 }
Laxman Dewangan21b49912016-10-10 15:14:34 +02001205
Jon Huntere8cf6612016-02-11 18:03:21 +00001206 mutex_unlock(&pmc->powergates_lock);
1207
Laxman Dewangan21b49912016-10-10 15:14:34 +02001208 usleep_range(100, 250);
1209
1210 return 0;
1211}
Laxman Dewangan21b49912016-10-10 15:14:34 +02001212
Aapo Vienamofccf0f72018-08-10 21:08:11 +03001213static int tegra_io_pad_get_voltage(enum tegra_io_pad id)
Laxman Dewangan21b49912016-10-10 15:14:34 +02001214{
1215 const struct tegra_io_pad_soc *pad;
1216 u32 value;
1217
1218 pad = tegra_io_pad_find(pmc, id);
1219 if (!pad)
1220 return -ENOENT;
1221
1222 if (pad->voltage == UINT_MAX)
1223 return -ENOTSUPP;
1224
Aapo Vienamo13136a42018-08-10 21:08:07 +03001225 if (pmc->soc->has_impl_33v_pwr)
1226 value = tegra_pmc_readl(PMC_IMPL_E_33V_PWR);
1227 else
1228 value = tegra_pmc_readl(PMC_PWR_DET_VALUE);
Laxman Dewangan21b49912016-10-10 15:14:34 +02001229
1230 if ((value & BIT(pad->voltage)) == 0)
Aapo Vienamofccf0f72018-08-10 21:08:11 +03001231 return TEGRA_IO_PAD_VOLTAGE_1V8;
Laxman Dewangan21b49912016-10-10 15:14:34 +02001232
Aapo Vienamofccf0f72018-08-10 21:08:11 +03001233 return TEGRA_IO_PAD_VOLTAGE_3V3;
Laxman Dewangan21b49912016-10-10 15:14:34 +02001234}
Laxman Dewangan21b49912016-10-10 15:14:34 +02001235
1236/**
1237 * tegra_io_rail_power_on() - enable power to I/O rail
1238 * @id: Tegra I/O pad ID for which to enable power
1239 *
1240 * See also: tegra_io_pad_power_enable()
1241 */
1242int tegra_io_rail_power_on(unsigned int id)
1243{
1244 return tegra_io_pad_power_enable(id);
1245}
1246EXPORT_SYMBOL(tegra_io_rail_power_on);
1247
1248/**
1249 * tegra_io_rail_power_off() - disable power to I/O rail
1250 * @id: Tegra I/O pad ID for which to disable power
1251 *
1252 * See also: tegra_io_pad_power_disable()
1253 */
1254int tegra_io_rail_power_off(unsigned int id)
1255{
1256 return tegra_io_pad_power_disable(id);
Thierry Reding72323982014-07-11 13:19:06 +02001257}
1258EXPORT_SYMBOL(tegra_io_rail_power_off);
1259
1260#ifdef CONFIG_PM_SLEEP
1261enum tegra_suspend_mode tegra_pmc_get_suspend_mode(void)
1262{
1263 return pmc->suspend_mode;
1264}
1265
1266void tegra_pmc_set_suspend_mode(enum tegra_suspend_mode mode)
1267{
1268 if (mode < TEGRA_SUSPEND_NONE || mode >= TEGRA_MAX_SUSPEND_MODE)
1269 return;
1270
1271 pmc->suspend_mode = mode;
1272}
1273
1274void tegra_pmc_enter_suspend_mode(enum tegra_suspend_mode mode)
1275{
1276 unsigned long long rate = 0;
1277 u32 value;
1278
1279 switch (mode) {
1280 case TEGRA_SUSPEND_LP1:
1281 rate = 32768;
1282 break;
1283
1284 case TEGRA_SUSPEND_LP2:
1285 rate = clk_get_rate(pmc->clk);
1286 break;
1287
1288 default:
1289 break;
1290 }
1291
1292 if (WARN_ON_ONCE(rate == 0))
1293 rate = 100000000;
1294
1295 if (rate != pmc->rate) {
1296 u64 ticks;
1297
1298 ticks = pmc->cpu_good_time * rate + USEC_PER_SEC - 1;
1299 do_div(ticks, USEC_PER_SEC);
1300 tegra_pmc_writel(ticks, PMC_CPUPWRGOOD_TIMER);
1301
1302 ticks = pmc->cpu_off_time * rate + USEC_PER_SEC - 1;
1303 do_div(ticks, USEC_PER_SEC);
1304 tegra_pmc_writel(ticks, PMC_CPUPWROFF_TIMER);
1305
1306 wmb();
1307
1308 pmc->rate = rate;
1309 }
1310
1311 value = tegra_pmc_readl(PMC_CNTRL);
1312 value &= ~PMC_CNTRL_SIDE_EFFECT_LP0;
1313 value |= PMC_CNTRL_CPU_PWRREQ_OE;
1314 tegra_pmc_writel(value, PMC_CNTRL);
1315}
1316#endif
1317
1318static int tegra_pmc_parse_dt(struct tegra_pmc *pmc, struct device_node *np)
1319{
1320 u32 value, values[2];
1321
1322 if (of_property_read_u32(np, "nvidia,suspend-mode", &value)) {
1323 } else {
1324 switch (value) {
1325 case 0:
1326 pmc->suspend_mode = TEGRA_SUSPEND_LP0;
1327 break;
1328
1329 case 1:
1330 pmc->suspend_mode = TEGRA_SUSPEND_LP1;
1331 break;
1332
1333 case 2:
1334 pmc->suspend_mode = TEGRA_SUSPEND_LP2;
1335 break;
1336
1337 default:
1338 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1339 break;
1340 }
1341 }
1342
1343 pmc->suspend_mode = tegra_pm_validate_suspend_mode(pmc->suspend_mode);
1344
1345 if (of_property_read_u32(np, "nvidia,cpu-pwr-good-time", &value))
1346 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1347
1348 pmc->cpu_good_time = value;
1349
1350 if (of_property_read_u32(np, "nvidia,cpu-pwr-off-time", &value))
1351 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1352
1353 pmc->cpu_off_time = value;
1354
1355 if (of_property_read_u32_array(np, "nvidia,core-pwr-good-time",
1356 values, ARRAY_SIZE(values)))
1357 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1358
1359 pmc->core_osc_time = values[0];
1360 pmc->core_pmu_time = values[1];
1361
1362 if (of_property_read_u32(np, "nvidia,core-pwr-off-time", &value))
1363 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1364
1365 pmc->core_off_time = value;
1366
1367 pmc->corereq_high = of_property_read_bool(np,
1368 "nvidia,core-power-req-active-high");
1369
1370 pmc->sysclkreq_high = of_property_read_bool(np,
1371 "nvidia,sys-clock-req-active-high");
1372
1373 pmc->combined_req = of_property_read_bool(np,
1374 "nvidia,combined-power-req");
1375
1376 pmc->cpu_pwr_good_en = of_property_read_bool(np,
1377 "nvidia,cpu-pwr-good-en");
1378
1379 if (of_property_read_u32_array(np, "nvidia,lp0-vec", values,
1380 ARRAY_SIZE(values)))
1381 if (pmc->suspend_mode == TEGRA_SUSPEND_LP0)
1382 pmc->suspend_mode = TEGRA_SUSPEND_LP1;
1383
1384 pmc->lp0_vec_phys = values[0];
1385 pmc->lp0_vec_size = values[1];
1386
1387 return 0;
1388}
1389
1390static void tegra_pmc_init(struct tegra_pmc *pmc)
1391{
Thierry Reding5be22552017-08-30 12:32:58 +02001392 if (pmc->soc->init)
1393 pmc->soc->init(pmc);
Thierry Reding72323982014-07-11 13:19:06 +02001394}
1395
Jon Hunter1e52efdf2015-12-04 14:57:04 +00001396static void tegra_pmc_init_tsense_reset(struct tegra_pmc *pmc)
Mikko Perttunen3568df32015-01-06 12:52:58 +02001397{
1398 static const char disabled[] = "emergency thermal reset disabled";
1399 u32 pmu_addr, ctrl_id, reg_addr, reg_data, pinmux;
1400 struct device *dev = pmc->dev;
1401 struct device_node *np;
1402 u32 value, checksum;
1403
1404 if (!pmc->soc->has_tsense_reset)
Thierry Reding95169cd2015-07-09 09:59:55 +02001405 return;
Mikko Perttunen3568df32015-01-06 12:52:58 +02001406
Johan Hovold1dc6bd52017-11-15 10:44:58 +01001407 np = of_get_child_by_name(pmc->dev->of_node, "i2c-thermtrip");
Mikko Perttunen3568df32015-01-06 12:52:58 +02001408 if (!np) {
1409 dev_warn(dev, "i2c-thermtrip node not found, %s.\n", disabled);
Thierry Reding95169cd2015-07-09 09:59:55 +02001410 return;
Mikko Perttunen3568df32015-01-06 12:52:58 +02001411 }
1412
1413 if (of_property_read_u32(np, "nvidia,i2c-controller-id", &ctrl_id)) {
1414 dev_err(dev, "I2C controller ID missing, %s.\n", disabled);
1415 goto out;
1416 }
1417
1418 if (of_property_read_u32(np, "nvidia,bus-addr", &pmu_addr)) {
1419 dev_err(dev, "nvidia,bus-addr missing, %s.\n", disabled);
1420 goto out;
1421 }
1422
1423 if (of_property_read_u32(np, "nvidia,reg-addr", &reg_addr)) {
1424 dev_err(dev, "nvidia,reg-addr missing, %s.\n", disabled);
1425 goto out;
1426 }
1427
1428 if (of_property_read_u32(np, "nvidia,reg-data", &reg_data)) {
1429 dev_err(dev, "nvidia,reg-data missing, %s.\n", disabled);
1430 goto out;
1431 }
1432
1433 if (of_property_read_u32(np, "nvidia,pinmux-id", &pinmux))
1434 pinmux = 0;
1435
1436 value = tegra_pmc_readl(PMC_SENSOR_CTRL);
1437 value |= PMC_SENSOR_CTRL_SCRATCH_WRITE;
1438 tegra_pmc_writel(value, PMC_SENSOR_CTRL);
1439
1440 value = (reg_data << PMC_SCRATCH54_DATA_SHIFT) |
1441 (reg_addr << PMC_SCRATCH54_ADDR_SHIFT);
1442 tegra_pmc_writel(value, PMC_SCRATCH54);
1443
1444 value = PMC_SCRATCH55_RESET_TEGRA;
1445 value |= ctrl_id << PMC_SCRATCH55_CNTRL_ID_SHIFT;
1446 value |= pinmux << PMC_SCRATCH55_PINMUX_SHIFT;
1447 value |= pmu_addr << PMC_SCRATCH55_I2CSLV1_SHIFT;
1448
1449 /*
1450 * Calculate checksum of SCRATCH54, SCRATCH55 fields. Bits 23:16 will
1451 * contain the checksum and are currently zero, so they are not added.
1452 */
1453 checksum = reg_addr + reg_data + (value & 0xff) + ((value >> 8) & 0xff)
1454 + ((value >> 24) & 0xff);
1455 checksum &= 0xff;
1456 checksum = 0x100 - checksum;
1457
1458 value |= checksum << PMC_SCRATCH55_CHECKSUM_SHIFT;
1459
1460 tegra_pmc_writel(value, PMC_SCRATCH55);
1461
1462 value = tegra_pmc_readl(PMC_SENSOR_CTRL);
1463 value |= PMC_SENSOR_CTRL_ENABLE_RST;
1464 tegra_pmc_writel(value, PMC_SENSOR_CTRL);
1465
1466 dev_info(pmc->dev, "emergency thermal reset enabled\n");
1467
1468out:
1469 of_node_put(np);
Mikko Perttunen3568df32015-01-06 12:52:58 +02001470}
1471
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001472static int tegra_io_pad_pinctrl_get_groups_count(struct pinctrl_dev *pctl_dev)
1473{
1474 return pmc->soc->num_io_pads;
1475}
1476
1477static const char *tegra_io_pad_pinctrl_get_group_name(
1478 struct pinctrl_dev *pctl, unsigned int group)
1479{
1480 return pmc->soc->io_pads[group].name;
1481}
1482
1483static int tegra_io_pad_pinctrl_get_group_pins(struct pinctrl_dev *pctl_dev,
1484 unsigned int group,
1485 const unsigned int **pins,
1486 unsigned int *num_pins)
1487{
1488 *pins = &pmc->soc->io_pads[group].id;
1489 *num_pins = 1;
1490 return 0;
1491}
1492
1493static const struct pinctrl_ops tegra_io_pad_pinctrl_ops = {
1494 .get_groups_count = tegra_io_pad_pinctrl_get_groups_count,
1495 .get_group_name = tegra_io_pad_pinctrl_get_group_name,
1496 .get_group_pins = tegra_io_pad_pinctrl_get_group_pins,
1497 .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
1498 .dt_free_map = pinconf_generic_dt_free_map,
1499};
1500
1501static int tegra_io_pad_pinconf_get(struct pinctrl_dev *pctl_dev,
1502 unsigned int pin, unsigned long *config)
1503{
1504 const struct tegra_io_pad_soc *pad = tegra_io_pad_find(pmc, pin);
1505 enum pin_config_param param = pinconf_to_config_param(*config);
1506 int ret;
1507 u32 arg;
1508
1509 if (!pad)
1510 return -EINVAL;
1511
1512 switch (param) {
1513 case PIN_CONFIG_POWER_SOURCE:
1514 ret = tegra_io_pad_get_voltage(pad->id);
1515 if (ret < 0)
1516 return ret;
1517 arg = ret;
1518 break;
1519 case PIN_CONFIG_LOW_POWER_MODE:
1520 ret = tegra_io_pad_is_powered(pad->id);
1521 if (ret < 0)
1522 return ret;
1523 arg = !ret;
1524 break;
1525 default:
1526 return -EINVAL;
1527 }
1528
1529 *config = pinconf_to_config_packed(param, arg);
1530
1531 return 0;
1532}
1533
1534static int tegra_io_pad_pinconf_set(struct pinctrl_dev *pctl_dev,
1535 unsigned int pin, unsigned long *configs,
1536 unsigned int num_configs)
1537{
1538 const struct tegra_io_pad_soc *pad = tegra_io_pad_find(pmc, pin);
1539 enum pin_config_param param;
1540 unsigned int i;
1541 int err;
1542 u32 arg;
1543
1544 if (!pad)
1545 return -EINVAL;
1546
1547 for (i = 0; i < num_configs; ++i) {
1548 param = pinconf_to_config_param(configs[i]);
1549 arg = pinconf_to_config_argument(configs[i]);
1550
1551 switch (param) {
1552 case PIN_CONFIG_LOW_POWER_MODE:
1553 if (arg)
1554 err = tegra_io_pad_power_disable(pad->id);
1555 else
1556 err = tegra_io_pad_power_enable(pad->id);
1557 if (err)
1558 return err;
1559 break;
1560 case PIN_CONFIG_POWER_SOURCE:
1561 if (arg != TEGRA_IO_PAD_VOLTAGE_1V8 &&
1562 arg != TEGRA_IO_PAD_VOLTAGE_3V3)
1563 return -EINVAL;
1564 err = tegra_io_pad_set_voltage(pad->id, arg);
1565 if (err)
1566 return err;
1567 break;
1568 default:
1569 return -EINVAL;
1570 }
1571 }
1572
1573 return 0;
1574}
1575
1576static const struct pinconf_ops tegra_io_pad_pinconf_ops = {
1577 .pin_config_get = tegra_io_pad_pinconf_get,
1578 .pin_config_set = tegra_io_pad_pinconf_set,
1579 .is_generic = true,
1580};
1581
1582static struct pinctrl_desc tegra_pmc_pctl_desc = {
1583 .pctlops = &tegra_io_pad_pinctrl_ops,
1584 .confops = &tegra_io_pad_pinconf_ops,
1585};
1586
1587static int tegra_pmc_pinctrl_init(struct tegra_pmc *pmc)
1588{
1589 int err = 0;
1590
1591 if (!pmc->soc->num_pin_descs)
1592 return 0;
1593
1594 tegra_pmc_pctl_desc.name = dev_name(pmc->dev);
1595 tegra_pmc_pctl_desc.pins = pmc->soc->pin_descs;
1596 tegra_pmc_pctl_desc.npins = pmc->soc->num_pin_descs;
1597
1598 pmc->pctl_dev = devm_pinctrl_register(pmc->dev, &tegra_pmc_pctl_desc,
1599 pmc);
1600 if (IS_ERR(pmc->pctl_dev)) {
1601 err = PTR_ERR(pmc->pctl_dev);
1602 dev_err(pmc->dev, "unable to register pinctrl, %d\n", err);
1603 }
1604
1605 return err;
1606}
1607
Sandipan Patra5f84bb12018-10-24 12:38:00 +05301608static ssize_t reset_reason_show(struct device *dev,
1609 struct device_attribute *attr, char *buf)
1610{
1611 u32 value, rst_src;
1612
1613 value = tegra_pmc_readl(pmc->soc->regs->rst_status);
1614 rst_src = (value & pmc->soc->regs->rst_source_mask) >>
1615 pmc->soc->regs->rst_source_shift;
1616
1617 return sprintf(buf, "%s\n", pmc->soc->reset_sources[rst_src]);
1618}
1619
1620static DEVICE_ATTR_RO(reset_reason);
1621
1622static ssize_t reset_level_show(struct device *dev,
1623 struct device_attribute *attr, char *buf)
1624{
1625 u32 value, rst_lvl;
1626
1627 value = tegra_pmc_readl(pmc->soc->regs->rst_status);
1628 rst_lvl = (value & pmc->soc->regs->rst_level_mask) >>
1629 pmc->soc->regs->rst_level_shift;
1630
1631 return sprintf(buf, "%s\n", pmc->soc->reset_levels[rst_lvl]);
1632}
1633
1634static DEVICE_ATTR_RO(reset_level);
1635
1636static void tegra_pmc_reset_sysfs_init(struct tegra_pmc *pmc)
1637{
1638 struct device *dev = pmc->dev;
1639 int err = 0;
1640
1641 if (pmc->soc->reset_sources) {
1642 err = device_create_file(dev, &dev_attr_reset_reason);
1643 if (err < 0)
1644 dev_warn(dev,
1645 "failed to create attr \"reset_reason\": %d\n",
1646 err);
1647 }
1648
1649 if (pmc->soc->reset_levels) {
1650 err = device_create_file(dev, &dev_attr_reset_level);
1651 if (err < 0)
1652 dev_warn(dev,
1653 "failed to create attr \"reset_level\": %d\n",
1654 err);
1655 }
1656}
1657
Thierry Reding19906e62018-09-17 15:08:17 +02001658static int tegra_pmc_irq_translate(struct irq_domain *domain,
1659 struct irq_fwspec *fwspec,
1660 unsigned long *hwirq,
1661 unsigned int *type)
1662{
1663 if (WARN_ON(fwspec->param_count < 2))
1664 return -EINVAL;
1665
1666 *hwirq = fwspec->param[0];
1667 *type = fwspec->param[1];
1668
1669 return 0;
1670}
1671
1672static int tegra_pmc_irq_alloc(struct irq_domain *domain, unsigned int virq,
1673 unsigned int num_irqs, void *data)
1674{
1675 struct tegra_pmc *pmc = domain->host_data;
1676 const struct tegra_pmc_soc *soc = pmc->soc;
1677 struct irq_fwspec *fwspec = data;
1678 unsigned int i;
1679 int err = 0;
1680
1681 for (i = 0; i < soc->num_wake_events; i++) {
1682 const struct tegra_wake_event *event = &soc->wake_events[i];
1683
1684 if (fwspec->param_count == 2) {
1685 struct irq_fwspec spec;
1686
1687 if (event->id != fwspec->param[0])
1688 continue;
1689
1690 err = irq_domain_set_hwirq_and_chip(domain, virq,
1691 event->id,
1692 &pmc->irq, pmc);
1693 if (err < 0)
1694 break;
1695
1696 spec.fwnode = &pmc->dev->of_node->fwnode;
1697 spec.param_count = 3;
1698 spec.param[0] = GIC_SPI;
1699 spec.param[1] = event->irq;
1700 spec.param[2] = fwspec->param[1];
1701
1702 err = irq_domain_alloc_irqs_parent(domain, virq,
1703 num_irqs, &spec);
1704
1705 break;
1706 }
1707
1708 if (fwspec->param_count == 3) {
1709 if (event->gpio.instance != fwspec->param[0] ||
1710 event->gpio.pin != fwspec->param[1])
1711 continue;
1712
1713 err = irq_domain_set_hwirq_and_chip(domain, virq,
1714 event->id,
1715 &pmc->irq, pmc);
1716
1717 break;
1718 }
1719 }
1720
1721 if (i == soc->num_wake_events)
1722 err = irq_domain_set_hwirq_and_chip(domain, virq, ULONG_MAX,
1723 &pmc->irq, pmc);
1724
1725 return err;
1726}
1727
1728static const struct irq_domain_ops tegra_pmc_irq_domain_ops = {
1729 .translate = tegra_pmc_irq_translate,
1730 .alloc = tegra_pmc_irq_alloc,
1731};
1732
1733static int tegra_pmc_irq_set_wake(struct irq_data *data, unsigned int on)
1734{
1735 struct tegra_pmc *pmc = irq_data_get_irq_chip_data(data);
1736 unsigned int offset, bit;
1737 u32 value;
1738
1739 offset = data->hwirq / 32;
1740 bit = data->hwirq % 32;
1741
1742 /* clear wake status */
1743 writel(0x1, pmc->wake + WAKE_AOWAKE_STATUS_W(data->hwirq));
1744
1745 /* route wake to tier 2 */
1746 value = readl(pmc->wake + WAKE_AOWAKE_TIER2_ROUTING(offset));
1747
1748 if (!on)
1749 value &= ~(1 << bit);
1750 else
1751 value |= 1 << bit;
1752
1753 writel(value, pmc->wake + WAKE_AOWAKE_TIER2_ROUTING(offset));
1754
1755 /* enable wakeup event */
1756 writel(!!on, pmc->wake + WAKE_AOWAKE_MASK_W(data->hwirq));
1757
1758 return 0;
1759}
1760
1761static int tegra_pmc_irq_set_type(struct irq_data *data, unsigned int type)
1762{
1763 struct tegra_pmc *pmc = irq_data_get_irq_chip_data(data);
1764 u32 value;
1765
1766 if (data->hwirq == ULONG_MAX)
1767 return 0;
1768
1769 value = readl(pmc->wake + WAKE_AOWAKE_CNTRL(data->hwirq));
1770
1771 switch (type) {
1772 case IRQ_TYPE_EDGE_RISING:
1773 case IRQ_TYPE_LEVEL_HIGH:
1774 value |= WAKE_AOWAKE_CNTRL_LEVEL;
1775 break;
1776
1777 case IRQ_TYPE_EDGE_FALLING:
1778 case IRQ_TYPE_LEVEL_LOW:
1779 value &= ~WAKE_AOWAKE_CNTRL_LEVEL;
1780 break;
1781
1782 case IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING:
1783 value ^= WAKE_AOWAKE_CNTRL_LEVEL;
1784 break;
1785
1786 default:
1787 return -EINVAL;
1788 }
1789
1790 writel(value, pmc->wake + WAKE_AOWAKE_CNTRL(data->hwirq));
1791
1792 return 0;
1793}
1794
1795static int tegra_pmc_irq_init(struct tegra_pmc *pmc)
1796{
1797 struct irq_domain *parent = NULL;
1798 struct device_node *np;
1799
1800 np = of_irq_find_parent(pmc->dev->of_node);
1801 if (np) {
1802 parent = irq_find_host(np);
1803 of_node_put(np);
1804 }
1805
1806 if (!parent)
1807 return 0;
1808
1809 pmc->irq.name = dev_name(pmc->dev);
1810 pmc->irq.irq_mask = irq_chip_mask_parent;
1811 pmc->irq.irq_unmask = irq_chip_unmask_parent;
1812 pmc->irq.irq_eoi = irq_chip_eoi_parent;
1813 pmc->irq.irq_set_affinity = irq_chip_set_affinity_parent;
1814 pmc->irq.irq_set_type = tegra_pmc_irq_set_type;
1815 pmc->irq.irq_set_wake = tegra_pmc_irq_set_wake;
1816
1817 pmc->domain = irq_domain_add_hierarchy(parent, 0, 96, pmc->dev->of_node,
1818 &tegra_pmc_irq_domain_ops, pmc);
1819 if (!pmc->domain) {
1820 dev_err(pmc->dev, "failed to allocate domain\n");
1821 return -ENOMEM;
1822 }
1823
1824 return 0;
1825}
1826
Thierry Reding72323982014-07-11 13:19:06 +02001827static int tegra_pmc_probe(struct platform_device *pdev)
1828{
Jon Huntere8cf6612016-02-11 18:03:21 +00001829 void __iomem *base;
Thierry Reding72323982014-07-11 13:19:06 +02001830 struct resource *res;
1831 int err;
1832
Jon Huntera83f1fc2016-06-28 11:38:28 +01001833 /*
1834 * Early initialisation should have configured an initial
1835 * register mapping and setup the soc data pointer. If these
1836 * are not valid then something went badly wrong!
1837 */
1838 if (WARN_ON(!pmc->base || !pmc->soc))
1839 return -ENODEV;
1840
Thierry Reding72323982014-07-11 13:19:06 +02001841 err = tegra_pmc_parse_dt(pmc, pdev->dev.of_node);
1842 if (err < 0)
1843 return err;
1844
1845 /* take over the memory region from the early initialization */
1846 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Jon Hunter0259f522016-02-11 18:03:20 +00001847 base = devm_ioremap_resource(&pdev->dev, res);
1848 if (IS_ERR(base))
1849 return PTR_ERR(base);
Thierry Reding72323982014-07-11 13:19:06 +02001850
Thierry Redingc641ec62017-08-30 12:42:34 +02001851 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "wake");
1852 if (res) {
1853 pmc->wake = devm_ioremap_resource(&pdev->dev, res);
1854 if (IS_ERR(pmc->wake))
1855 return PTR_ERR(pmc->wake);
1856 } else {
1857 pmc->wake = base;
1858 }
1859
1860 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "aotag");
1861 if (res) {
1862 pmc->aotag = devm_ioremap_resource(&pdev->dev, res);
1863 if (IS_ERR(pmc->aotag))
1864 return PTR_ERR(pmc->aotag);
1865 } else {
1866 pmc->aotag = base;
1867 }
1868
1869 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "scratch");
1870 if (res) {
1871 pmc->scratch = devm_ioremap_resource(&pdev->dev, res);
1872 if (IS_ERR(pmc->scratch))
1873 return PTR_ERR(pmc->scratch);
1874 } else {
1875 pmc->scratch = base;
1876 }
Thierry Reding5be22552017-08-30 12:32:58 +02001877
Thierry Reding72323982014-07-11 13:19:06 +02001878 pmc->clk = devm_clk_get(&pdev->dev, "pclk");
1879 if (IS_ERR(pmc->clk)) {
1880 err = PTR_ERR(pmc->clk);
Thierry Reding5be22552017-08-30 12:32:58 +02001881
1882 if (err != -ENOENT) {
1883 dev_err(&pdev->dev, "failed to get pclk: %d\n", err);
1884 return err;
1885 }
1886
1887 pmc->clk = NULL;
Thierry Reding72323982014-07-11 13:19:06 +02001888 }
1889
Mikko Perttunen3568df32015-01-06 12:52:58 +02001890 pmc->dev = &pdev->dev;
1891
Thierry Reding72323982014-07-11 13:19:06 +02001892 tegra_pmc_init(pmc);
1893
Mikko Perttunen3568df32015-01-06 12:52:58 +02001894 tegra_pmc_init_tsense_reset(pmc);
1895
Sandipan Patra5f84bb12018-10-24 12:38:00 +05301896 tegra_pmc_reset_sysfs_init(pmc);
1897
Thierry Reding72323982014-07-11 13:19:06 +02001898 if (IS_ENABLED(CONFIG_DEBUG_FS)) {
1899 err = tegra_powergate_debugfs_init();
1900 if (err < 0)
1901 return err;
1902 }
1903
David Riley78921582015-03-18 10:52:25 +01001904 err = register_restart_handler(&tegra_pmc_restart_handler);
1905 if (err) {
1906 dev_err(&pdev->dev, "unable to register restart handler, %d\n",
1907 err);
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001908 goto cleanup_debugfs;
David Riley78921582015-03-18 10:52:25 +01001909 }
1910
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001911 err = tegra_pmc_pinctrl_init(pmc);
1912 if (err)
1913 goto cleanup_restart_handler;
1914
Thierry Reding19906e62018-09-17 15:08:17 +02001915 err = tegra_pmc_irq_init(pmc);
1916 if (err < 0)
1917 goto cleanup_restart_handler;
1918
Jon Huntere8cf6612016-02-11 18:03:21 +00001919 mutex_lock(&pmc->powergates_lock);
1920 iounmap(pmc->base);
Jon Hunter0259f522016-02-11 18:03:20 +00001921 pmc->base = base;
Jon Huntere8cf6612016-02-11 18:03:21 +00001922 mutex_unlock(&pmc->powergates_lock);
Jon Hunter0259f522016-02-11 18:03:20 +00001923
Thierry Reding72323982014-07-11 13:19:06 +02001924 return 0;
Aapo Vienamo4a37f112018-08-10 21:08:12 +03001925
1926cleanup_restart_handler:
1927 unregister_restart_handler(&tegra_pmc_restart_handler);
1928cleanup_debugfs:
1929 debugfs_remove(pmc->debugfs);
1930 return err;
Thierry Reding72323982014-07-11 13:19:06 +02001931}
1932
Paul Walmsley2b20b612014-12-09 22:36:50 +00001933#if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM)
Thierry Reding72323982014-07-11 13:19:06 +02001934static int tegra_pmc_suspend(struct device *dev)
1935{
1936 tegra_pmc_writel(virt_to_phys(tegra_resume), PMC_SCRATCH41);
1937
1938 return 0;
1939}
1940
1941static int tegra_pmc_resume(struct device *dev)
1942{
1943 tegra_pmc_writel(0x0, PMC_SCRATCH41);
1944
1945 return 0;
1946}
Thierry Reding72323982014-07-11 13:19:06 +02001947
1948static SIMPLE_DEV_PM_OPS(tegra_pmc_pm_ops, tegra_pmc_suspend, tegra_pmc_resume);
1949
Paul Walmsley2b20b612014-12-09 22:36:50 +00001950#endif
1951
Thierry Reding72323982014-07-11 13:19:06 +02001952static const char * const tegra20_powergates[] = {
1953 [TEGRA_POWERGATE_CPU] = "cpu",
1954 [TEGRA_POWERGATE_3D] = "3d",
1955 [TEGRA_POWERGATE_VENC] = "venc",
1956 [TEGRA_POWERGATE_VDEC] = "vdec",
1957 [TEGRA_POWERGATE_PCIE] = "pcie",
1958 [TEGRA_POWERGATE_L2] = "l2",
1959 [TEGRA_POWERGATE_MPE] = "mpe",
1960};
1961
Thierry Reding5be22552017-08-30 12:32:58 +02001962static const struct tegra_pmc_regs tegra20_pmc_regs = {
1963 .scratch0 = 0x50,
1964 .dpd_req = 0x1b8,
1965 .dpd_status = 0x1bc,
1966 .dpd2_req = 0x1c0,
1967 .dpd2_status = 0x1c4,
Sandipan Patra5f84bb12018-10-24 12:38:00 +05301968 .rst_status = 0x1b4,
1969 .rst_source_shift = 0x0,
1970 .rst_source_mask = 0x7,
1971 .rst_level_shift = 0x0,
1972 .rst_level_mask = 0x0,
Thierry Reding5be22552017-08-30 12:32:58 +02001973};
1974
1975static void tegra20_pmc_init(struct tegra_pmc *pmc)
1976{
1977 u32 value;
1978
1979 /* Always enable CPU power request */
1980 value = tegra_pmc_readl(PMC_CNTRL);
1981 value |= PMC_CNTRL_CPU_PWRREQ_OE;
1982 tegra_pmc_writel(value, PMC_CNTRL);
1983
1984 value = tegra_pmc_readl(PMC_CNTRL);
1985
1986 if (pmc->sysclkreq_high)
1987 value &= ~PMC_CNTRL_SYSCLK_POLARITY;
1988 else
1989 value |= PMC_CNTRL_SYSCLK_POLARITY;
1990
1991 /* configure the output polarity while the request is tristated */
1992 tegra_pmc_writel(value, PMC_CNTRL);
1993
1994 /* now enable the request */
1995 value = tegra_pmc_readl(PMC_CNTRL);
1996 value |= PMC_CNTRL_SYSCLK_OE;
1997 tegra_pmc_writel(value, PMC_CNTRL);
1998}
1999
2000static void tegra20_pmc_setup_irq_polarity(struct tegra_pmc *pmc,
2001 struct device_node *np,
2002 bool invert)
2003{
2004 u32 value;
2005
2006 value = tegra_pmc_readl(PMC_CNTRL);
2007
2008 if (invert)
2009 value |= PMC_CNTRL_INTR_POLARITY;
2010 else
2011 value &= ~PMC_CNTRL_INTR_POLARITY;
2012
2013 tegra_pmc_writel(value, PMC_CNTRL);
2014}
2015
Thierry Reding72323982014-07-11 13:19:06 +02002016static const struct tegra_pmc_soc tegra20_pmc_soc = {
2017 .num_powergates = ARRAY_SIZE(tegra20_powergates),
2018 .powergates = tegra20_powergates,
2019 .num_cpu_powergates = 0,
2020 .cpu_powergates = NULL,
Mikko Perttunen3568df32015-01-06 12:52:58 +02002021 .has_tsense_reset = false,
Thierry Redinga9a40a42015-01-09 11:15:33 +01002022 .has_gpu_clamps = false,
Thierry Reding5be22552017-08-30 12:32:58 +02002023 .num_io_pads = 0,
2024 .io_pads = NULL,
Aapo Vienamo4a37f112018-08-10 21:08:12 +03002025 .num_pin_descs = 0,
2026 .pin_descs = NULL,
Thierry Reding5be22552017-08-30 12:32:58 +02002027 .regs = &tegra20_pmc_regs,
2028 .init = tegra20_pmc_init,
2029 .setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
Sandipan Patra5f84bb12018-10-24 12:38:00 +05302030 .reset_sources = NULL,
2031 .num_reset_sources = 0,
2032 .reset_levels = NULL,
2033 .num_reset_levels = 0,
Thierry Reding72323982014-07-11 13:19:06 +02002034};
2035
2036static const char * const tegra30_powergates[] = {
2037 [TEGRA_POWERGATE_CPU] = "cpu0",
2038 [TEGRA_POWERGATE_3D] = "3d0",
2039 [TEGRA_POWERGATE_VENC] = "venc",
2040 [TEGRA_POWERGATE_VDEC] = "vdec",
2041 [TEGRA_POWERGATE_PCIE] = "pcie",
2042 [TEGRA_POWERGATE_L2] = "l2",
2043 [TEGRA_POWERGATE_MPE] = "mpe",
2044 [TEGRA_POWERGATE_HEG] = "heg",
2045 [TEGRA_POWERGATE_SATA] = "sata",
2046 [TEGRA_POWERGATE_CPU1] = "cpu1",
2047 [TEGRA_POWERGATE_CPU2] = "cpu2",
2048 [TEGRA_POWERGATE_CPU3] = "cpu3",
2049 [TEGRA_POWERGATE_CELP] = "celp",
2050 [TEGRA_POWERGATE_3D1] = "3d1",
2051};
2052
2053static const u8 tegra30_cpu_powergates[] = {
2054 TEGRA_POWERGATE_CPU,
2055 TEGRA_POWERGATE_CPU1,
2056 TEGRA_POWERGATE_CPU2,
2057 TEGRA_POWERGATE_CPU3,
2058};
2059
2060static const struct tegra_pmc_soc tegra30_pmc_soc = {
2061 .num_powergates = ARRAY_SIZE(tegra30_powergates),
2062 .powergates = tegra30_powergates,
2063 .num_cpu_powergates = ARRAY_SIZE(tegra30_cpu_powergates),
2064 .cpu_powergates = tegra30_cpu_powergates,
Mikko Perttunen3568df32015-01-06 12:52:58 +02002065 .has_tsense_reset = true,
Thierry Redinga9a40a42015-01-09 11:15:33 +01002066 .has_gpu_clamps = false,
Aapo Vienamo13136a42018-08-10 21:08:07 +03002067 .has_impl_33v_pwr = false,
Thierry Reding5be22552017-08-30 12:32:58 +02002068 .num_io_pads = 0,
2069 .io_pads = NULL,
Aapo Vienamo4a37f112018-08-10 21:08:12 +03002070 .num_pin_descs = 0,
2071 .pin_descs = NULL,
Thierry Reding5be22552017-08-30 12:32:58 +02002072 .regs = &tegra20_pmc_regs,
2073 .init = tegra20_pmc_init,
2074 .setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
Sandipan Patra5f84bb12018-10-24 12:38:00 +05302075 .reset_sources = tegra30_reset_sources,
2076 .num_reset_sources = 5,
2077 .reset_levels = NULL,
2078 .num_reset_levels = 0,
Thierry Reding72323982014-07-11 13:19:06 +02002079};
2080
2081static const char * const tegra114_powergates[] = {
2082 [TEGRA_POWERGATE_CPU] = "crail",
2083 [TEGRA_POWERGATE_3D] = "3d",
2084 [TEGRA_POWERGATE_VENC] = "venc",
2085 [TEGRA_POWERGATE_VDEC] = "vdec",
2086 [TEGRA_POWERGATE_MPE] = "mpe",
2087 [TEGRA_POWERGATE_HEG] = "heg",
2088 [TEGRA_POWERGATE_CPU1] = "cpu1",
2089 [TEGRA_POWERGATE_CPU2] = "cpu2",
2090 [TEGRA_POWERGATE_CPU3] = "cpu3",
2091 [TEGRA_POWERGATE_CELP] = "celp",
2092 [TEGRA_POWERGATE_CPU0] = "cpu0",
2093 [TEGRA_POWERGATE_C0NC] = "c0nc",
2094 [TEGRA_POWERGATE_C1NC] = "c1nc",
2095 [TEGRA_POWERGATE_DIS] = "dis",
2096 [TEGRA_POWERGATE_DISB] = "disb",
2097 [TEGRA_POWERGATE_XUSBA] = "xusba",
2098 [TEGRA_POWERGATE_XUSBB] = "xusbb",
2099 [TEGRA_POWERGATE_XUSBC] = "xusbc",
2100};
2101
2102static const u8 tegra114_cpu_powergates[] = {
2103 TEGRA_POWERGATE_CPU0,
2104 TEGRA_POWERGATE_CPU1,
2105 TEGRA_POWERGATE_CPU2,
2106 TEGRA_POWERGATE_CPU3,
2107};
2108
2109static const struct tegra_pmc_soc tegra114_pmc_soc = {
2110 .num_powergates = ARRAY_SIZE(tegra114_powergates),
2111 .powergates = tegra114_powergates,
2112 .num_cpu_powergates = ARRAY_SIZE(tegra114_cpu_powergates),
2113 .cpu_powergates = tegra114_cpu_powergates,
Mikko Perttunen3568df32015-01-06 12:52:58 +02002114 .has_tsense_reset = true,
Thierry Redinga9a40a42015-01-09 11:15:33 +01002115 .has_gpu_clamps = false,
Aapo Vienamo13136a42018-08-10 21:08:07 +03002116 .has_impl_33v_pwr = false,
Thierry Reding5be22552017-08-30 12:32:58 +02002117 .num_io_pads = 0,
2118 .io_pads = NULL,
Aapo Vienamo4a37f112018-08-10 21:08:12 +03002119 .num_pin_descs = 0,
2120 .pin_descs = NULL,
Thierry Reding5be22552017-08-30 12:32:58 +02002121 .regs = &tegra20_pmc_regs,
2122 .init = tegra20_pmc_init,
2123 .setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
Sandipan Patra5f84bb12018-10-24 12:38:00 +05302124 .reset_sources = tegra30_reset_sources,
2125 .num_reset_sources = 5,
2126 .reset_levels = NULL,
2127 .num_reset_levels = 0,
Thierry Reding72323982014-07-11 13:19:06 +02002128};
2129
2130static const char * const tegra124_powergates[] = {
2131 [TEGRA_POWERGATE_CPU] = "crail",
2132 [TEGRA_POWERGATE_3D] = "3d",
2133 [TEGRA_POWERGATE_VENC] = "venc",
2134 [TEGRA_POWERGATE_PCIE] = "pcie",
2135 [TEGRA_POWERGATE_VDEC] = "vdec",
Thierry Reding72323982014-07-11 13:19:06 +02002136 [TEGRA_POWERGATE_MPE] = "mpe",
2137 [TEGRA_POWERGATE_HEG] = "heg",
2138 [TEGRA_POWERGATE_SATA] = "sata",
2139 [TEGRA_POWERGATE_CPU1] = "cpu1",
2140 [TEGRA_POWERGATE_CPU2] = "cpu2",
2141 [TEGRA_POWERGATE_CPU3] = "cpu3",
2142 [TEGRA_POWERGATE_CELP] = "celp",
2143 [TEGRA_POWERGATE_CPU0] = "cpu0",
2144 [TEGRA_POWERGATE_C0NC] = "c0nc",
2145 [TEGRA_POWERGATE_C1NC] = "c1nc",
2146 [TEGRA_POWERGATE_SOR] = "sor",
2147 [TEGRA_POWERGATE_DIS] = "dis",
2148 [TEGRA_POWERGATE_DISB] = "disb",
2149 [TEGRA_POWERGATE_XUSBA] = "xusba",
2150 [TEGRA_POWERGATE_XUSBB] = "xusbb",
2151 [TEGRA_POWERGATE_XUSBC] = "xusbc",
2152 [TEGRA_POWERGATE_VIC] = "vic",
2153 [TEGRA_POWERGATE_IRAM] = "iram",
2154};
2155
2156static const u8 tegra124_cpu_powergates[] = {
2157 TEGRA_POWERGATE_CPU0,
2158 TEGRA_POWERGATE_CPU1,
2159 TEGRA_POWERGATE_CPU2,
2160 TEGRA_POWERGATE_CPU3,
2161};
2162
Aapo Vienamo437c4f22018-08-10 21:08:10 +03002163#define TEGRA_IO_PAD(_id, _dpd, _voltage, _name) \
2164 ((struct tegra_io_pad_soc) { \
2165 .id = (_id), \
2166 .dpd = (_dpd), \
2167 .voltage = (_voltage), \
2168 .name = (_name), \
2169 })
2170
Aapo Vienamo4a37f112018-08-10 21:08:12 +03002171#define TEGRA_IO_PIN_DESC(_id, _dpd, _voltage, _name) \
2172 ((struct pinctrl_pin_desc) { \
2173 .number = (_id), \
2174 .name = (_name) \
2175 })
2176
Aapo Vienamo437c4f22018-08-10 21:08:10 +03002177#define TEGRA124_IO_PAD_TABLE(_pad) \
2178 /* .id .dpd .voltage .name */ \
2179 _pad(TEGRA_IO_PAD_AUDIO, 17, UINT_MAX, "audio"), \
2180 _pad(TEGRA_IO_PAD_BB, 15, UINT_MAX, "bb"), \
2181 _pad(TEGRA_IO_PAD_CAM, 36, UINT_MAX, "cam"), \
2182 _pad(TEGRA_IO_PAD_COMP, 22, UINT_MAX, "comp"), \
2183 _pad(TEGRA_IO_PAD_CSIA, 0, UINT_MAX, "csia"), \
2184 _pad(TEGRA_IO_PAD_CSIB, 1, UINT_MAX, "csb"), \
2185 _pad(TEGRA_IO_PAD_CSIE, 44, UINT_MAX, "cse"), \
2186 _pad(TEGRA_IO_PAD_DSI, 2, UINT_MAX, "dsi"), \
2187 _pad(TEGRA_IO_PAD_DSIB, 39, UINT_MAX, "dsib"), \
2188 _pad(TEGRA_IO_PAD_DSIC, 40, UINT_MAX, "dsic"), \
2189 _pad(TEGRA_IO_PAD_DSID, 41, UINT_MAX, "dsid"), \
2190 _pad(TEGRA_IO_PAD_HDMI, 28, UINT_MAX, "hdmi"), \
2191 _pad(TEGRA_IO_PAD_HSIC, 19, UINT_MAX, "hsic"), \
2192 _pad(TEGRA_IO_PAD_HV, 38, UINT_MAX, "hv"), \
2193 _pad(TEGRA_IO_PAD_LVDS, 57, UINT_MAX, "lvds"), \
2194 _pad(TEGRA_IO_PAD_MIPI_BIAS, 3, UINT_MAX, "mipi-bias"), \
2195 _pad(TEGRA_IO_PAD_NAND, 13, UINT_MAX, "nand"), \
2196 _pad(TEGRA_IO_PAD_PEX_BIAS, 4, UINT_MAX, "pex-bias"), \
2197 _pad(TEGRA_IO_PAD_PEX_CLK1, 5, UINT_MAX, "pex-clk1"), \
2198 _pad(TEGRA_IO_PAD_PEX_CLK2, 6, UINT_MAX, "pex-clk2"), \
2199 _pad(TEGRA_IO_PAD_PEX_CNTRL, 32, UINT_MAX, "pex-cntrl"), \
2200 _pad(TEGRA_IO_PAD_SDMMC1, 33, UINT_MAX, "sdmmc1"), \
2201 _pad(TEGRA_IO_PAD_SDMMC3, 34, UINT_MAX, "sdmmc3"), \
2202 _pad(TEGRA_IO_PAD_SDMMC4, 35, UINT_MAX, "sdmmc4"), \
2203 _pad(TEGRA_IO_PAD_SYS_DDC, 58, UINT_MAX, "sys_ddc"), \
2204 _pad(TEGRA_IO_PAD_UART, 14, UINT_MAX, "uart"), \
2205 _pad(TEGRA_IO_PAD_USB0, 9, UINT_MAX, "usb0"), \
2206 _pad(TEGRA_IO_PAD_USB1, 10, UINT_MAX, "usb1"), \
2207 _pad(TEGRA_IO_PAD_USB2, 11, UINT_MAX, "usb2"), \
2208 _pad(TEGRA_IO_PAD_USB_BIAS, 12, UINT_MAX, "usb_bias")
2209
Laxman Dewangan21b49912016-10-10 15:14:34 +02002210static const struct tegra_io_pad_soc tegra124_io_pads[] = {
Aapo Vienamo437c4f22018-08-10 21:08:10 +03002211 TEGRA124_IO_PAD_TABLE(TEGRA_IO_PAD)
Laxman Dewangan21b49912016-10-10 15:14:34 +02002212};
2213
Aapo Vienamo4a37f112018-08-10 21:08:12 +03002214static const struct pinctrl_pin_desc tegra124_pin_descs[] = {
2215 TEGRA124_IO_PAD_TABLE(TEGRA_IO_PIN_DESC)
Thierry Reding72323982014-07-11 13:19:06 +02002216};
2217
2218static const struct tegra_pmc_soc tegra124_pmc_soc = {
2219 .num_powergates = ARRAY_SIZE(tegra124_powergates),
2220 .powergates = tegra124_powergates,
2221 .num_cpu_powergates = ARRAY_SIZE(tegra124_cpu_powergates),
2222 .cpu_powergates = tegra124_cpu_powergates,
Mikko Perttunen3568df32015-01-06 12:52:58 +02002223 .has_tsense_reset = true,
Thierry Redinga9a40a42015-01-09 11:15:33 +01002224 .has_gpu_clamps = true,
Aapo Vienamo13136a42018-08-10 21:08:07 +03002225 .has_impl_33v_pwr = false,
Laxman Dewangan21b49912016-10-10 15:14:34 +02002226 .num_io_pads = ARRAY_SIZE(tegra124_io_pads),
2227 .io_pads = tegra124_io_pads,
Aapo Vienamo4a37f112018-08-10 21:08:12 +03002228 .num_pin_descs = ARRAY_SIZE(tegra124_pin_descs),
2229 .pin_descs = tegra124_pin_descs,
Thierry Reding5be22552017-08-30 12:32:58 +02002230 .regs = &tegra20_pmc_regs,
2231 .init = tegra20_pmc_init,
2232 .setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
Sandipan Patra5f84bb12018-10-24 12:38:00 +05302233 .reset_sources = tegra30_reset_sources,
2234 .num_reset_sources = 5,
2235 .reset_levels = NULL,
2236 .num_reset_levels = 0,
Thierry Reding72323982014-07-11 13:19:06 +02002237};
2238
Thierry Redingc2fe4692015-03-23 11:31:29 +01002239static const char * const tegra210_powergates[] = {
2240 [TEGRA_POWERGATE_CPU] = "crail",
2241 [TEGRA_POWERGATE_3D] = "3d",
2242 [TEGRA_POWERGATE_VENC] = "venc",
2243 [TEGRA_POWERGATE_PCIE] = "pcie",
Thierry Redingc2fe4692015-03-23 11:31:29 +01002244 [TEGRA_POWERGATE_MPE] = "mpe",
Thierry Redingc2fe4692015-03-23 11:31:29 +01002245 [TEGRA_POWERGATE_SATA] = "sata",
2246 [TEGRA_POWERGATE_CPU1] = "cpu1",
2247 [TEGRA_POWERGATE_CPU2] = "cpu2",
2248 [TEGRA_POWERGATE_CPU3] = "cpu3",
Thierry Redingc2fe4692015-03-23 11:31:29 +01002249 [TEGRA_POWERGATE_CPU0] = "cpu0",
2250 [TEGRA_POWERGATE_C0NC] = "c0nc",
Thierry Redingc2fe4692015-03-23 11:31:29 +01002251 [TEGRA_POWERGATE_SOR] = "sor",
2252 [TEGRA_POWERGATE_DIS] = "dis",
2253 [TEGRA_POWERGATE_DISB] = "disb",
2254 [TEGRA_POWERGATE_XUSBA] = "xusba",
2255 [TEGRA_POWERGATE_XUSBB] = "xusbb",
2256 [TEGRA_POWERGATE_XUSBC] = "xusbc",
2257 [TEGRA_POWERGATE_VIC] = "vic",
2258 [TEGRA_POWERGATE_IRAM] = "iram",
2259 [TEGRA_POWERGATE_NVDEC] = "nvdec",
2260 [TEGRA_POWERGATE_NVJPG] = "nvjpg",
2261 [TEGRA_POWERGATE_AUD] = "aud",
2262 [TEGRA_POWERGATE_DFD] = "dfd",
2263 [TEGRA_POWERGATE_VE2] = "ve2",
2264};
2265
2266static const u8 tegra210_cpu_powergates[] = {
2267 TEGRA_POWERGATE_CPU0,
2268 TEGRA_POWERGATE_CPU1,
2269 TEGRA_POWERGATE_CPU2,
2270 TEGRA_POWERGATE_CPU3,
2271};
2272
Aapo Vienamo437c4f22018-08-10 21:08:10 +03002273#define TEGRA210_IO_PAD_TABLE(_pad) \
2274 /* .id .dpd .voltage .name */ \
2275 _pad(TEGRA_IO_PAD_AUDIO, 17, 5, "audio"), \
2276 _pad(TEGRA_IO_PAD_AUDIO_HV, 61, 18, "audio-hv"), \
2277 _pad(TEGRA_IO_PAD_CAM, 36, 10, "cam"), \
2278 _pad(TEGRA_IO_PAD_CSIA, 0, UINT_MAX, "csia"), \
2279 _pad(TEGRA_IO_PAD_CSIB, 1, UINT_MAX, "csib"), \
2280 _pad(TEGRA_IO_PAD_CSIC, 42, UINT_MAX, "csic"), \
2281 _pad(TEGRA_IO_PAD_CSID, 43, UINT_MAX, "csid"), \
2282 _pad(TEGRA_IO_PAD_CSIE, 44, UINT_MAX, "csie"), \
2283 _pad(TEGRA_IO_PAD_CSIF, 45, UINT_MAX, "csif"), \
2284 _pad(TEGRA_IO_PAD_DBG, 25, 19, "dbg"), \
2285 _pad(TEGRA_IO_PAD_DEBUG_NONAO, 26, UINT_MAX, "debug-nonao"), \
2286 _pad(TEGRA_IO_PAD_DMIC, 50, 20, "dmic"), \
2287 _pad(TEGRA_IO_PAD_DP, 51, UINT_MAX, "dp"), \
2288 _pad(TEGRA_IO_PAD_DSI, 2, UINT_MAX, "dsi"), \
2289 _pad(TEGRA_IO_PAD_DSIB, 39, UINT_MAX, "dsib"), \
2290 _pad(TEGRA_IO_PAD_DSIC, 40, UINT_MAX, "dsic"), \
2291 _pad(TEGRA_IO_PAD_DSID, 41, UINT_MAX, "dsid"), \
2292 _pad(TEGRA_IO_PAD_EMMC, 35, UINT_MAX, "emmc"), \
2293 _pad(TEGRA_IO_PAD_EMMC2, 37, UINT_MAX, "emmc2"), \
2294 _pad(TEGRA_IO_PAD_GPIO, 27, 21, "gpio"), \
2295 _pad(TEGRA_IO_PAD_HDMI, 28, UINT_MAX, "hdmi"), \
2296 _pad(TEGRA_IO_PAD_HSIC, 19, UINT_MAX, "hsic"), \
2297 _pad(TEGRA_IO_PAD_LVDS, 57, UINT_MAX, "lvds"), \
2298 _pad(TEGRA_IO_PAD_MIPI_BIAS, 3, UINT_MAX, "mipi-bias"), \
2299 _pad(TEGRA_IO_PAD_PEX_BIAS, 4, UINT_MAX, "pex-bias"), \
2300 _pad(TEGRA_IO_PAD_PEX_CLK1, 5, UINT_MAX, "pex-clk1"), \
2301 _pad(TEGRA_IO_PAD_PEX_CLK2, 6, UINT_MAX, "pex-clk2"), \
2302 _pad(TEGRA_IO_PAD_PEX_CNTRL, UINT_MAX, 11, "pex-cntrl"), \
2303 _pad(TEGRA_IO_PAD_SDMMC1, 33, 12, "sdmmc1"), \
2304 _pad(TEGRA_IO_PAD_SDMMC3, 34, 13, "sdmmc3"), \
2305 _pad(TEGRA_IO_PAD_SPI, 46, 22, "spi"), \
2306 _pad(TEGRA_IO_PAD_SPI_HV, 47, 23, "spi-hv"), \
2307 _pad(TEGRA_IO_PAD_UART, 14, 2, "uart"), \
2308 _pad(TEGRA_IO_PAD_USB0, 9, UINT_MAX, "usb0"), \
2309 _pad(TEGRA_IO_PAD_USB1, 10, UINT_MAX, "usb1"), \
2310 _pad(TEGRA_IO_PAD_USB2, 11, UINT_MAX, "usb2"), \
2311 _pad(TEGRA_IO_PAD_USB3, 18, UINT_MAX, "usb3"), \
2312 _pad(TEGRA_IO_PAD_USB_BIAS, 12, UINT_MAX, "usb-bias")
2313
Laxman Dewangan21b49912016-10-10 15:14:34 +02002314static const struct tegra_io_pad_soc tegra210_io_pads[] = {
Aapo Vienamo437c4f22018-08-10 21:08:10 +03002315 TEGRA210_IO_PAD_TABLE(TEGRA_IO_PAD)
Laxman Dewangan21b49912016-10-10 15:14:34 +02002316};
2317
Aapo Vienamo4a37f112018-08-10 21:08:12 +03002318static const struct pinctrl_pin_desc tegra210_pin_descs[] = {
2319 TEGRA210_IO_PAD_TABLE(TEGRA_IO_PIN_DESC)
Thierry Redingc2fe4692015-03-23 11:31:29 +01002320};
2321
2322static const struct tegra_pmc_soc tegra210_pmc_soc = {
2323 .num_powergates = ARRAY_SIZE(tegra210_powergates),
2324 .powergates = tegra210_powergates,
2325 .num_cpu_powergates = ARRAY_SIZE(tegra210_cpu_powergates),
2326 .cpu_powergates = tegra210_cpu_powergates,
2327 .has_tsense_reset = true,
2328 .has_gpu_clamps = true,
Aapo Vienamo13136a42018-08-10 21:08:07 +03002329 .has_impl_33v_pwr = false,
Peter De Schrijvera263394a2018-01-25 16:00:13 +02002330 .needs_mbist_war = true,
Laxman Dewangan21b49912016-10-10 15:14:34 +02002331 .num_io_pads = ARRAY_SIZE(tegra210_io_pads),
2332 .io_pads = tegra210_io_pads,
Aapo Vienamo4a37f112018-08-10 21:08:12 +03002333 .num_pin_descs = ARRAY_SIZE(tegra210_pin_descs),
2334 .pin_descs = tegra210_pin_descs,
Thierry Reding5be22552017-08-30 12:32:58 +02002335 .regs = &tegra20_pmc_regs,
2336 .init = tegra20_pmc_init,
2337 .setup_irq_polarity = tegra20_pmc_setup_irq_polarity,
Sandipan Patra5f84bb12018-10-24 12:38:00 +05302338 .reset_sources = tegra30_reset_sources,
2339 .num_reset_sources = 5,
2340 .reset_levels = NULL,
2341 .num_reset_levels = 0,
Thierry Redingc2fe4692015-03-23 11:31:29 +01002342};
2343
Aapo Vienamo437c4f22018-08-10 21:08:10 +03002344#define TEGRA186_IO_PAD_TABLE(_pad) \
2345 /* .id .dpd .voltage .name */ \
2346 _pad(TEGRA_IO_PAD_CSIA, 0, UINT_MAX, "csia"), \
2347 _pad(TEGRA_IO_PAD_CSIB, 1, UINT_MAX, "csib"), \
2348 _pad(TEGRA_IO_PAD_DSI, 2, UINT_MAX, "dsi"), \
2349 _pad(TEGRA_IO_PAD_MIPI_BIAS, 3, UINT_MAX, "mipi-bias"), \
2350 _pad(TEGRA_IO_PAD_PEX_CLK_BIAS, 4, UINT_MAX, "pex-clk-bias"), \
2351 _pad(TEGRA_IO_PAD_PEX_CLK3, 5, UINT_MAX, "pex-clk3"), \
2352 _pad(TEGRA_IO_PAD_PEX_CLK2, 6, UINT_MAX, "pex-clk2"), \
2353 _pad(TEGRA_IO_PAD_PEX_CLK1, 7, UINT_MAX, "pex-clk1"), \
2354 _pad(TEGRA_IO_PAD_USB0, 9, UINT_MAX, "usb0"), \
2355 _pad(TEGRA_IO_PAD_USB1, 10, UINT_MAX, "usb1"), \
2356 _pad(TEGRA_IO_PAD_USB2, 11, UINT_MAX, "usb2"), \
2357 _pad(TEGRA_IO_PAD_USB_BIAS, 12, UINT_MAX, "usb-bias"), \
2358 _pad(TEGRA_IO_PAD_UART, 14, UINT_MAX, "uart"), \
2359 _pad(TEGRA_IO_PAD_AUDIO, 17, UINT_MAX, "audio"), \
2360 _pad(TEGRA_IO_PAD_HSIC, 19, UINT_MAX, "hsic"), \
2361 _pad(TEGRA_IO_PAD_DBG, 25, UINT_MAX, "dbg"), \
2362 _pad(TEGRA_IO_PAD_HDMI_DP0, 28, UINT_MAX, "hdmi-dp0"), \
2363 _pad(TEGRA_IO_PAD_HDMI_DP1, 29, UINT_MAX, "hdmi-dp1"), \
2364 _pad(TEGRA_IO_PAD_PEX_CNTRL, 32, UINT_MAX, "pex-cntrl"), \
2365 _pad(TEGRA_IO_PAD_SDMMC2_HV, 34, 5, "sdmmc2-hv"), \
2366 _pad(TEGRA_IO_PAD_SDMMC4, 36, UINT_MAX, "sdmmc4"), \
2367 _pad(TEGRA_IO_PAD_CAM, 38, UINT_MAX, "cam"), \
2368 _pad(TEGRA_IO_PAD_DSIB, 40, UINT_MAX, "dsib"), \
2369 _pad(TEGRA_IO_PAD_DSIC, 41, UINT_MAX, "dsic"), \
2370 _pad(TEGRA_IO_PAD_DSID, 42, UINT_MAX, "dsid"), \
2371 _pad(TEGRA_IO_PAD_CSIC, 43, UINT_MAX, "csic"), \
2372 _pad(TEGRA_IO_PAD_CSID, 44, UINT_MAX, "csid"), \
2373 _pad(TEGRA_IO_PAD_CSIE, 45, UINT_MAX, "csie"), \
2374 _pad(TEGRA_IO_PAD_CSIF, 46, UINT_MAX, "csif"), \
2375 _pad(TEGRA_IO_PAD_SPI, 47, UINT_MAX, "spi"), \
2376 _pad(TEGRA_IO_PAD_UFS, 49, UINT_MAX, "ufs"), \
2377 _pad(TEGRA_IO_PAD_DMIC_HV, 52, 2, "dmic-hv"), \
2378 _pad(TEGRA_IO_PAD_EDP, 53, UINT_MAX, "edp"), \
2379 _pad(TEGRA_IO_PAD_SDMMC1_HV, 55, 4, "sdmmc1-hv"), \
2380 _pad(TEGRA_IO_PAD_SDMMC3_HV, 56, 6, "sdmmc3-hv"), \
2381 _pad(TEGRA_IO_PAD_CONN, 60, UINT_MAX, "conn"), \
2382 _pad(TEGRA_IO_PAD_AUDIO_HV, 61, 1, "audio-hv"), \
2383 _pad(TEGRA_IO_PAD_AO_HV, UINT_MAX, 0, "ao-hv")
2384
Thierry Redingc641ec62017-08-30 12:42:34 +02002385static const struct tegra_io_pad_soc tegra186_io_pads[] = {
Aapo Vienamo437c4f22018-08-10 21:08:10 +03002386 TEGRA186_IO_PAD_TABLE(TEGRA_IO_PAD)
Thierry Redingc641ec62017-08-30 12:42:34 +02002387};
2388
Aapo Vienamo4a37f112018-08-10 21:08:12 +03002389static const struct pinctrl_pin_desc tegra186_pin_descs[] = {
2390 TEGRA186_IO_PAD_TABLE(TEGRA_IO_PIN_DESC)
Thierry Redingc641ec62017-08-30 12:42:34 +02002391};
2392
2393static const struct tegra_pmc_regs tegra186_pmc_regs = {
2394 .scratch0 = 0x2000,
2395 .dpd_req = 0x74,
2396 .dpd_status = 0x78,
2397 .dpd2_req = 0x7c,
2398 .dpd2_status = 0x80,
Sandipan Patra5f84bb12018-10-24 12:38:00 +05302399 .rst_status = 0x70,
2400 .rst_source_shift = 0x2,
2401 .rst_source_mask = 0x3C,
2402 .rst_level_shift = 0x0,
2403 .rst_level_mask = 0x3,
Thierry Redingc641ec62017-08-30 12:42:34 +02002404};
2405
2406static void tegra186_pmc_setup_irq_polarity(struct tegra_pmc *pmc,
2407 struct device_node *np,
2408 bool invert)
2409{
2410 struct resource regs;
2411 void __iomem *wake;
2412 u32 value;
2413 int index;
2414
2415 index = of_property_match_string(np, "reg-names", "wake");
2416 if (index < 0) {
2417 pr_err("failed to find PMC wake registers\n");
2418 return;
2419 }
2420
2421 of_address_to_resource(np, index, &regs);
2422
2423 wake = ioremap_nocache(regs.start, resource_size(&regs));
2424 if (!wake) {
2425 pr_err("failed to map PMC wake registers\n");
2426 return;
2427 }
2428
2429 value = readl(wake + WAKE_AOWAKE_CTRL);
2430
2431 if (invert)
2432 value |= WAKE_AOWAKE_CTRL_INTR_POLARITY;
2433 else
2434 value &= ~WAKE_AOWAKE_CTRL_INTR_POLARITY;
2435
2436 writel(value, wake + WAKE_AOWAKE_CTRL);
2437
2438 iounmap(wake);
2439}
2440
Thierry Redinge59333c2018-09-19 18:41:59 +02002441static const struct tegra_wake_event tegra186_wake_events[] = {
2442 TEGRA_WAKE_GPIO("power", 29, 1, TEGRA_AON_GPIO(FF, 0)),
2443 TEGRA_WAKE_IRQ("rtc", 73, 10),
2444};
2445
Thierry Redingc641ec62017-08-30 12:42:34 +02002446static const struct tegra_pmc_soc tegra186_pmc_soc = {
2447 .num_powergates = 0,
2448 .powergates = NULL,
2449 .num_cpu_powergates = 0,
2450 .cpu_powergates = NULL,
2451 .has_tsense_reset = false,
2452 .has_gpu_clamps = false,
Aapo Vienamo13136a42018-08-10 21:08:07 +03002453 .has_impl_33v_pwr = true,
Thierry Redingc641ec62017-08-30 12:42:34 +02002454 .num_io_pads = ARRAY_SIZE(tegra186_io_pads),
2455 .io_pads = tegra186_io_pads,
Aapo Vienamo4a37f112018-08-10 21:08:12 +03002456 .num_pin_descs = ARRAY_SIZE(tegra186_pin_descs),
2457 .pin_descs = tegra186_pin_descs,
Thierry Redingc641ec62017-08-30 12:42:34 +02002458 .regs = &tegra186_pmc_regs,
2459 .init = NULL,
2460 .setup_irq_polarity = tegra186_pmc_setup_irq_polarity,
Sandipan Patra5f84bb12018-10-24 12:38:00 +05302461 .reset_sources = tegra186_reset_sources,
2462 .num_reset_sources = 14,
2463 .reset_levels = tegra186_reset_levels,
2464 .num_reset_levels = 3,
Thierry Redinge59333c2018-09-19 18:41:59 +02002465 .num_wake_events = ARRAY_SIZE(tegra186_wake_events),
2466 .wake_events = tegra186_wake_events,
Thierry Redingc641ec62017-08-30 12:42:34 +02002467};
2468
Thierry Redingeac9c482018-01-25 14:43:45 +02002469static const struct tegra_io_pad_soc tegra194_io_pads[] = {
2470 { .id = TEGRA_IO_PAD_CSIA, .dpd = 0, .voltage = UINT_MAX },
2471 { .id = TEGRA_IO_PAD_CSIB, .dpd = 1, .voltage = UINT_MAX },
2472 { .id = TEGRA_IO_PAD_MIPI_BIAS, .dpd = 3, .voltage = UINT_MAX },
2473 { .id = TEGRA_IO_PAD_PEX_CLK_BIAS, .dpd = 4, .voltage = UINT_MAX },
2474 { .id = TEGRA_IO_PAD_PEX_CLK3, .dpd = 5, .voltage = UINT_MAX },
2475 { .id = TEGRA_IO_PAD_PEX_CLK2, .dpd = 6, .voltage = UINT_MAX },
2476 { .id = TEGRA_IO_PAD_PEX_CLK1, .dpd = 7, .voltage = UINT_MAX },
2477 { .id = TEGRA_IO_PAD_EQOS, .dpd = 8, .voltage = UINT_MAX },
2478 { .id = TEGRA_IO_PAD_PEX_CLK2_BIAS, .dpd = 9, .voltage = UINT_MAX },
2479 { .id = TEGRA_IO_PAD_PEX_CLK2, .dpd = 10, .voltage = UINT_MAX },
2480 { .id = TEGRA_IO_PAD_DAP3, .dpd = 11, .voltage = UINT_MAX },
2481 { .id = TEGRA_IO_PAD_DAP5, .dpd = 12, .voltage = UINT_MAX },
2482 { .id = TEGRA_IO_PAD_UART, .dpd = 14, .voltage = UINT_MAX },
2483 { .id = TEGRA_IO_PAD_PWR_CTL, .dpd = 15, .voltage = UINT_MAX },
2484 { .id = TEGRA_IO_PAD_SOC_GPIO53, .dpd = 16, .voltage = UINT_MAX },
2485 { .id = TEGRA_IO_PAD_AUDIO, .dpd = 17, .voltage = UINT_MAX },
2486 { .id = TEGRA_IO_PAD_GP_PWM2, .dpd = 18, .voltage = UINT_MAX },
2487 { .id = TEGRA_IO_PAD_GP_PWM3, .dpd = 19, .voltage = UINT_MAX },
2488 { .id = TEGRA_IO_PAD_SOC_GPIO12, .dpd = 20, .voltage = UINT_MAX },
2489 { .id = TEGRA_IO_PAD_SOC_GPIO13, .dpd = 21, .voltage = UINT_MAX },
2490 { .id = TEGRA_IO_PAD_SOC_GPIO10, .dpd = 22, .voltage = UINT_MAX },
2491 { .id = TEGRA_IO_PAD_UART4, .dpd = 23, .voltage = UINT_MAX },
2492 { .id = TEGRA_IO_PAD_UART5, .dpd = 24, .voltage = UINT_MAX },
2493 { .id = TEGRA_IO_PAD_DBG, .dpd = 25, .voltage = UINT_MAX },
2494 { .id = TEGRA_IO_PAD_HDMI_DP3, .dpd = 26, .voltage = UINT_MAX },
2495 { .id = TEGRA_IO_PAD_HDMI_DP2, .dpd = 27, .voltage = UINT_MAX },
2496 { .id = TEGRA_IO_PAD_HDMI_DP0, .dpd = 28, .voltage = UINT_MAX },
2497 { .id = TEGRA_IO_PAD_HDMI_DP1, .dpd = 29, .voltage = UINT_MAX },
2498 { .id = TEGRA_IO_PAD_PEX_CNTRL, .dpd = 32, .voltage = UINT_MAX },
2499 { .id = TEGRA_IO_PAD_PEX_CTL2, .dpd = 33, .voltage = UINT_MAX },
2500 { .id = TEGRA_IO_PAD_PEX_L0_RST_N, .dpd = 34, .voltage = UINT_MAX },
2501 { .id = TEGRA_IO_PAD_PEX_L1_RST_N, .dpd = 35, .voltage = UINT_MAX },
2502 { .id = TEGRA_IO_PAD_SDMMC4, .dpd = 36, .voltage = UINT_MAX },
2503 { .id = TEGRA_IO_PAD_PEX_L5_RST_N, .dpd = 37, .voltage = UINT_MAX },
2504 { .id = TEGRA_IO_PAD_CSIC, .dpd = 43, .voltage = UINT_MAX },
2505 { .id = TEGRA_IO_PAD_CSID, .dpd = 44, .voltage = UINT_MAX },
2506 { .id = TEGRA_IO_PAD_CSIE, .dpd = 45, .voltage = UINT_MAX },
2507 { .id = TEGRA_IO_PAD_CSIF, .dpd = 46, .voltage = UINT_MAX },
2508 { .id = TEGRA_IO_PAD_SPI, .dpd = 47, .voltage = UINT_MAX },
2509 { .id = TEGRA_IO_PAD_UFS, .dpd = 49, .voltage = UINT_MAX },
2510 { .id = TEGRA_IO_PAD_CSIG, .dpd = 50, .voltage = UINT_MAX },
2511 { .id = TEGRA_IO_PAD_CSIH, .dpd = 51, .voltage = UINT_MAX },
2512 { .id = TEGRA_IO_PAD_EDP, .dpd = 53, .voltage = UINT_MAX },
2513 { .id = TEGRA_IO_PAD_SDMMC1_HV, .dpd = 55, .voltage = UINT_MAX },
2514 { .id = TEGRA_IO_PAD_SDMMC3_HV, .dpd = 56, .voltage = UINT_MAX },
2515 { .id = TEGRA_IO_PAD_CONN, .dpd = 60, .voltage = UINT_MAX },
2516 { .id = TEGRA_IO_PAD_AUDIO_HV, .dpd = 61, .voltage = UINT_MAX },
2517};
2518
2519static const struct tegra_pmc_soc tegra194_pmc_soc = {
2520 .num_powergates = 0,
2521 .powergates = NULL,
2522 .num_cpu_powergates = 0,
2523 .cpu_powergates = NULL,
2524 .has_tsense_reset = false,
2525 .has_gpu_clamps = false,
2526 .num_io_pads = ARRAY_SIZE(tegra194_io_pads),
2527 .io_pads = tegra194_io_pads,
2528 .regs = &tegra186_pmc_regs,
2529 .init = NULL,
2530 .setup_irq_polarity = tegra186_pmc_setup_irq_polarity,
2531};
2532
Thierry Reding72323982014-07-11 13:19:06 +02002533static const struct of_device_id tegra_pmc_match[] = {
Thierry Redingeac9c482018-01-25 14:43:45 +02002534 { .compatible = "nvidia,tegra194-pmc", .data = &tegra194_pmc_soc },
Thierry Redingc641ec62017-08-30 12:42:34 +02002535 { .compatible = "nvidia,tegra186-pmc", .data = &tegra186_pmc_soc },
Thierry Redingc2fe4692015-03-23 11:31:29 +01002536 { .compatible = "nvidia,tegra210-pmc", .data = &tegra210_pmc_soc },
Thierry Reding7d71e9032015-04-29 12:42:28 +02002537 { .compatible = "nvidia,tegra132-pmc", .data = &tegra124_pmc_soc },
Thierry Reding72323982014-07-11 13:19:06 +02002538 { .compatible = "nvidia,tegra124-pmc", .data = &tegra124_pmc_soc },
2539 { .compatible = "nvidia,tegra114-pmc", .data = &tegra114_pmc_soc },
2540 { .compatible = "nvidia,tegra30-pmc", .data = &tegra30_pmc_soc },
2541 { .compatible = "nvidia,tegra20-pmc", .data = &tegra20_pmc_soc },
2542 { }
2543};
2544
2545static struct platform_driver tegra_pmc_driver = {
2546 .driver = {
2547 .name = "tegra-pmc",
2548 .suppress_bind_attrs = true,
2549 .of_match_table = tegra_pmc_match,
Paul Walmsley2b20b612014-12-09 22:36:50 +00002550#if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM)
Thierry Reding72323982014-07-11 13:19:06 +02002551 .pm = &tegra_pmc_pm_ops,
Paul Walmsley2b20b612014-12-09 22:36:50 +00002552#endif
Thierry Reding72323982014-07-11 13:19:06 +02002553 },
2554 .probe = tegra_pmc_probe,
2555};
Paul Gortmaker7d4d9ed2015-05-01 20:10:57 -04002556builtin_platform_driver(tegra_pmc_driver);
Thierry Reding72323982014-07-11 13:19:06 +02002557
2558/*
2559 * Early initialization to allow access to registers in the very early boot
2560 * process.
2561 */
2562static int __init tegra_pmc_early_init(void)
2563{
2564 const struct of_device_id *match;
2565 struct device_node *np;
2566 struct resource regs;
2567 bool invert;
Thierry Reding72323982014-07-11 13:19:06 +02002568
Jon Hunter61fd2842016-06-28 11:38:26 +01002569 mutex_init(&pmc->powergates_lock);
2570
Thierry Reding72323982014-07-11 13:19:06 +02002571 np = of_find_matching_node_and_match(NULL, tegra_pmc_match, &match);
2572 if (!np) {
Thierry Reding7d71e9032015-04-29 12:42:28 +02002573 /*
2574 * Fall back to legacy initialization for 32-bit ARM only. All
2575 * 64-bit ARM device tree files for Tegra are required to have
2576 * a PMC node.
2577 *
2578 * This is for backwards-compatibility with old device trees
2579 * that didn't contain a PMC node. Note that in this case the
2580 * SoC data can't be matched and therefore powergating is
2581 * disabled.
2582 */
2583 if (IS_ENABLED(CONFIG_ARM) && soc_is_tegra()) {
2584 pr_warn("DT node not found, powergating disabled\n");
Thierry Reding72323982014-07-11 13:19:06 +02002585
Thierry Reding7d71e9032015-04-29 12:42:28 +02002586 regs.start = 0x7000e400;
2587 regs.end = 0x7000e7ff;
2588 regs.flags = IORESOURCE_MEM;
Thierry Reding72323982014-07-11 13:19:06 +02002589
Thierry Reding7d71e9032015-04-29 12:42:28 +02002590 pr_warn("Using memory region %pR\n", &regs);
2591 } else {
2592 /*
2593 * At this point we're not running on Tegra, so play
2594 * nice with multi-platform kernels.
2595 */
2596 return 0;
2597 }
Thierry Reding72323982014-07-11 13:19:06 +02002598 } else {
Thierry Reding7d71e9032015-04-29 12:42:28 +02002599 /*
2600 * Extract information from the device tree if we've found a
2601 * matching node.
2602 */
2603 if (of_address_to_resource(np, 0, &regs) < 0) {
2604 pr_err("failed to get PMC registers\n");
Jon Hunterb69a6252016-06-28 11:38:27 +01002605 of_node_put(np);
Thierry Reding7d71e9032015-04-29 12:42:28 +02002606 return -ENXIO;
2607 }
Thierry Reding72323982014-07-11 13:19:06 +02002608 }
2609
2610 pmc->base = ioremap_nocache(regs.start, resource_size(&regs));
2611 if (!pmc->base) {
2612 pr_err("failed to map PMC registers\n");
Jon Hunterb69a6252016-06-28 11:38:27 +01002613 of_node_put(np);
Thierry Reding72323982014-07-11 13:19:06 +02002614 return -ENXIO;
2615 }
2616
Jon Hunter11131892016-06-28 11:38:24 +01002617 if (np) {
Jon Hunter718a2422016-06-28 11:38:25 +01002618 pmc->soc = match->data;
2619
Jon Huntere2d17962016-06-30 11:56:25 +01002620 tegra_powergate_init(pmc, np);
Thierry Reding72323982014-07-11 13:19:06 +02002621
Jon Hunter11131892016-06-28 11:38:24 +01002622 /*
2623 * Invert the interrupt polarity if a PMC device tree node
2624 * exists and contains the nvidia,invert-interrupt property.
2625 */
2626 invert = of_property_read_bool(np, "nvidia,invert-interrupt");
Thierry Reding72323982014-07-11 13:19:06 +02002627
Thierry Reding5be22552017-08-30 12:32:58 +02002628 pmc->soc->setup_irq_polarity(pmc, np, invert);
Jon Hunterb69a6252016-06-28 11:38:27 +01002629
2630 of_node_put(np);
Jon Hunter11131892016-06-28 11:38:24 +01002631 }
Thierry Reding72323982014-07-11 13:19:06 +02002632
2633 return 0;
2634}
2635early_initcall(tegra_pmc_early_init);