soc/tegra: pmc: Wait for powergate state to change
[cascardo/linux.git] / drivers / soc / tegra / pmc.c
1 /*
2  * drivers/soc/tegra/pmc.c
3  *
4  * Copyright (c) 2010 Google, Inc
5  *
6  * Author:
7  *      Colin Cross <ccross@google.com>
8  *
9  * This software is licensed under the terms of the GNU General Public
10  * License version 2, as published by the Free Software Foundation, and
11  * may be copied, distributed, and modified under those terms.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  */
19
20 #define pr_fmt(fmt) "tegra-pmc: " fmt
21
22 #include <linux/kernel.h>
23 #include <linux/clk.h>
24 #include <linux/clk/tegra.h>
25 #include <linux/debugfs.h>
26 #include <linux/delay.h>
27 #include <linux/err.h>
28 #include <linux/export.h>
29 #include <linux/init.h>
30 #include <linux/io.h>
31 #include <linux/iopoll.h>
32 #include <linux/of.h>
33 #include <linux/of_address.h>
34 #include <linux/platform_device.h>
35 #include <linux/reboot.h>
36 #include <linux/reset.h>
37 #include <linux/seq_file.h>
38 #include <linux/spinlock.h>
39
40 #include <soc/tegra/common.h>
41 #include <soc/tegra/fuse.h>
42 #include <soc/tegra/pmc.h>
43
44 #define PMC_CNTRL                       0x0
45 #define  PMC_CNTRL_SYSCLK_POLARITY      (1 << 10)  /* sys clk polarity */
46 #define  PMC_CNTRL_SYSCLK_OE            (1 << 11)  /* system clock enable */
47 #define  PMC_CNTRL_SIDE_EFFECT_LP0      (1 << 14)  /* LP0 when CPU pwr gated */
48 #define  PMC_CNTRL_CPU_PWRREQ_POLARITY  (1 << 15)  /* CPU pwr req polarity */
49 #define  PMC_CNTRL_CPU_PWRREQ_OE        (1 << 16)  /* CPU pwr req enable */
50 #define  PMC_CNTRL_INTR_POLARITY        (1 << 17)  /* inverts INTR polarity */
51
52 #define DPD_SAMPLE                      0x020
53 #define  DPD_SAMPLE_ENABLE              (1 << 0)
54 #define  DPD_SAMPLE_DISABLE             (0 << 0)
55
56 #define PWRGATE_TOGGLE                  0x30
57 #define  PWRGATE_TOGGLE_START           (1 << 8)
58
59 #define REMOVE_CLAMPING                 0x34
60
61 #define PWRGATE_STATUS                  0x38
62
63 #define PMC_SCRATCH0                    0x50
64 #define  PMC_SCRATCH0_MODE_RECOVERY     (1 << 31)
65 #define  PMC_SCRATCH0_MODE_BOOTLOADER   (1 << 30)
66 #define  PMC_SCRATCH0_MODE_RCM          (1 << 1)
67 #define  PMC_SCRATCH0_MODE_MASK         (PMC_SCRATCH0_MODE_RECOVERY | \
68                                          PMC_SCRATCH0_MODE_BOOTLOADER | \
69                                          PMC_SCRATCH0_MODE_RCM)
70
71 #define PMC_CPUPWRGOOD_TIMER            0xc8
72 #define PMC_CPUPWROFF_TIMER             0xcc
73
74 #define PMC_SCRATCH41                   0x140
75
76 #define PMC_SENSOR_CTRL                 0x1b0
77 #define PMC_SENSOR_CTRL_SCRATCH_WRITE   (1 << 2)
78 #define PMC_SENSOR_CTRL_ENABLE_RST      (1 << 1)
79
80 #define IO_DPD_REQ                      0x1b8
81 #define  IO_DPD_REQ_CODE_IDLE           (0 << 30)
82 #define  IO_DPD_REQ_CODE_OFF            (1 << 30)
83 #define  IO_DPD_REQ_CODE_ON             (2 << 30)
84 #define  IO_DPD_REQ_CODE_MASK           (3 << 30)
85
86 #define IO_DPD_STATUS                   0x1bc
87 #define IO_DPD2_REQ                     0x1c0
88 #define IO_DPD2_STATUS                  0x1c4
89 #define SEL_DPD_TIM                     0x1c8
90
91 #define PMC_SCRATCH54                   0x258
92 #define PMC_SCRATCH54_DATA_SHIFT        8
93 #define PMC_SCRATCH54_ADDR_SHIFT        0
94
95 #define PMC_SCRATCH55                   0x25c
96 #define PMC_SCRATCH55_RESET_TEGRA       (1 << 31)
97 #define PMC_SCRATCH55_CNTRL_ID_SHIFT    27
98 #define PMC_SCRATCH55_PINMUX_SHIFT      24
99 #define PMC_SCRATCH55_16BITOP           (1 << 15)
100 #define PMC_SCRATCH55_CHECKSUM_SHIFT    16
101 #define PMC_SCRATCH55_I2CSLV1_SHIFT     0
102
103 #define GPU_RG_CNTRL                    0x2d4
104
105 struct tegra_pmc_soc {
106         unsigned int num_powergates;
107         const char *const *powergates;
108         unsigned int num_cpu_powergates;
109         const u8 *cpu_powergates;
110
111         bool has_tsense_reset;
112         bool has_gpu_clamps;
113 };
114
115 /**
116  * struct tegra_pmc - NVIDIA Tegra PMC
117  * @dev: pointer to PMC device structure
118  * @base: pointer to I/O remapped register region
119  * @clk: pointer to pclk clock
120  * @soc: pointer to SoC data structure
121  * @debugfs: pointer to debugfs entry
122  * @rate: currently configured rate of pclk
123  * @suspend_mode: lowest suspend mode available
124  * @cpu_good_time: CPU power good time (in microseconds)
125  * @cpu_off_time: CPU power off time (in microsecends)
126  * @core_osc_time: core power good OSC time (in microseconds)
127  * @core_pmu_time: core power good PMU time (in microseconds)
128  * @core_off_time: core power off time (in microseconds)
129  * @corereq_high: core power request is active-high
130  * @sysclkreq_high: system clock request is active-high
131  * @combined_req: combined power request for CPU & core
132  * @cpu_pwr_good_en: CPU power good signal is enabled
133  * @lp0_vec_phys: physical base address of the LP0 warm boot code
134  * @lp0_vec_size: size of the LP0 warm boot code
135  * @powergates_lock: mutex for power gate register access
136  */
137 struct tegra_pmc {
138         struct device *dev;
139         void __iomem *base;
140         struct clk *clk;
141         struct dentry *debugfs;
142
143         const struct tegra_pmc_soc *soc;
144
145         unsigned long rate;
146
147         enum tegra_suspend_mode suspend_mode;
148         u32 cpu_good_time;
149         u32 cpu_off_time;
150         u32 core_osc_time;
151         u32 core_pmu_time;
152         u32 core_off_time;
153         bool corereq_high;
154         bool sysclkreq_high;
155         bool combined_req;
156         bool cpu_pwr_good_en;
157         u32 lp0_vec_phys;
158         u32 lp0_vec_size;
159
160         struct mutex powergates_lock;
161 };
162
163 static struct tegra_pmc *pmc = &(struct tegra_pmc) {
164         .base = NULL,
165         .suspend_mode = TEGRA_SUSPEND_NONE,
166 };
167
168 static u32 tegra_pmc_readl(unsigned long offset)
169 {
170         return readl(pmc->base + offset);
171 }
172
173 static void tegra_pmc_writel(u32 value, unsigned long offset)
174 {
175         writel(value, pmc->base + offset);
176 }
177
178 static inline bool tegra_powergate_state(int id)
179 {
180         if (id == TEGRA_POWERGATE_3D && pmc->soc->has_gpu_clamps)
181                 return (tegra_pmc_readl(GPU_RG_CNTRL) & 0x1) == 0;
182         else
183                 return (tegra_pmc_readl(PWRGATE_STATUS) & BIT(id)) != 0;
184 }
185
186 static inline bool tegra_powergate_is_valid(int id)
187 {
188         return (pmc->soc && pmc->soc->powergates[id]);
189 }
190
191 /**
192  * tegra_powergate_set() - set the state of a partition
193  * @id: partition ID
194  * @new_state: new state of the partition
195  */
196 static int tegra_powergate_set(unsigned int id, bool new_state)
197 {
198         bool status;
199         int err;
200
201         if (id == TEGRA_POWERGATE_3D && pmc->soc->has_gpu_clamps)
202                 return -EINVAL;
203
204         mutex_lock(&pmc->powergates_lock);
205
206         if (tegra_powergate_state(id) == new_state) {
207                 mutex_unlock(&pmc->powergates_lock);
208                 return 0;
209         }
210
211         tegra_pmc_writel(PWRGATE_TOGGLE_START | id, PWRGATE_TOGGLE);
212
213         err = readx_poll_timeout(tegra_powergate_state, id, status,
214                                  status == new_state, 10, 100000);
215
216         mutex_unlock(&pmc->powergates_lock);
217
218         return err;
219 }
220
221 /**
222  * tegra_powergate_power_on() - power on partition
223  * @id: partition ID
224  */
225 int tegra_powergate_power_on(unsigned int id)
226 {
227         if (!tegra_powergate_is_valid(id))
228                 return -EINVAL;
229
230         return tegra_powergate_set(id, true);
231 }
232
233 /**
234  * tegra_powergate_power_off() - power off partition
235  * @id: partition ID
236  */
237 int tegra_powergate_power_off(unsigned int id)
238 {
239         if (!tegra_powergate_is_valid(id))
240                 return -EINVAL;
241
242         return tegra_powergate_set(id, false);
243 }
244 EXPORT_SYMBOL(tegra_powergate_power_off);
245
246 /**
247  * tegra_powergate_is_powered() - check if partition is powered
248  * @id: partition ID
249  */
250 int tegra_powergate_is_powered(unsigned int id)
251 {
252         int status;
253
254         if (!tegra_powergate_is_valid(id))
255                 return -EINVAL;
256
257         mutex_lock(&pmc->powergates_lock);
258         status = tegra_powergate_state(id);
259         mutex_unlock(&pmc->powergates_lock);
260
261         return status;
262 }
263
264 /**
265  * tegra_powergate_remove_clamping() - remove power clamps for partition
266  * @id: partition ID
267  */
268 int tegra_powergate_remove_clamping(unsigned int id)
269 {
270         u32 mask;
271
272         if (!tegra_powergate_is_valid(id))
273                 return -EINVAL;
274
275         mutex_lock(&pmc->powergates_lock);
276
277         /*
278          * On Tegra124 and later, the clamps for the GPU are controlled by a
279          * separate register (with different semantics).
280          */
281         if (id == TEGRA_POWERGATE_3D) {
282                 if (pmc->soc->has_gpu_clamps) {
283                         tegra_pmc_writel(0, GPU_RG_CNTRL);
284                         goto out;
285                 }
286         }
287
288         /*
289          * Tegra 2 has a bug where PCIE and VDE clamping masks are
290          * swapped relatively to the partition ids
291          */
292         if (id == TEGRA_POWERGATE_VDEC)
293                 mask = (1 << TEGRA_POWERGATE_PCIE);
294         else if (id == TEGRA_POWERGATE_PCIE)
295                 mask = (1 << TEGRA_POWERGATE_VDEC);
296         else
297                 mask = (1 << id);
298
299         tegra_pmc_writel(mask, REMOVE_CLAMPING);
300
301 out:
302         mutex_unlock(&pmc->powergates_lock);
303
304         return 0;
305 }
306 EXPORT_SYMBOL(tegra_powergate_remove_clamping);
307
308 /**
309  * tegra_powergate_sequence_power_up() - power up partition
310  * @id: partition ID
311  * @clk: clock for partition
312  * @rst: reset for partition
313  *
314  * Must be called with clk disabled, and returns with clk enabled.
315  */
316 int tegra_powergate_sequence_power_up(unsigned int id, struct clk *clk,
317                                       struct reset_control *rst)
318 {
319         int ret;
320
321         reset_control_assert(rst);
322
323         ret = tegra_powergate_power_on(id);
324         if (ret)
325                 goto err_power;
326
327         ret = clk_prepare_enable(clk);
328         if (ret)
329                 goto err_clk;
330
331         usleep_range(10, 20);
332
333         ret = tegra_powergate_remove_clamping(id);
334         if (ret)
335                 goto err_clamp;
336
337         usleep_range(10, 20);
338         reset_control_deassert(rst);
339
340         return 0;
341
342 err_clamp:
343         clk_disable_unprepare(clk);
344 err_clk:
345         tegra_powergate_power_off(id);
346 err_power:
347         return ret;
348 }
349 EXPORT_SYMBOL(tegra_powergate_sequence_power_up);
350
351 #ifdef CONFIG_SMP
352 /**
353  * tegra_get_cpu_powergate_id() - convert from CPU ID to partition ID
354  * @cpuid: CPU partition ID
355  *
356  * Returns the partition ID corresponding to the CPU partition ID or a
357  * negative error code on failure.
358  */
359 static int tegra_get_cpu_powergate_id(unsigned int cpuid)
360 {
361         if (pmc->soc && cpuid < pmc->soc->num_cpu_powergates)
362                 return pmc->soc->cpu_powergates[cpuid];
363
364         return -EINVAL;
365 }
366
367 /**
368  * tegra_pmc_cpu_is_powered() - check if CPU partition is powered
369  * @cpuid: CPU partition ID
370  */
371 bool tegra_pmc_cpu_is_powered(unsigned int cpuid)
372 {
373         int id;
374
375         id = tegra_get_cpu_powergate_id(cpuid);
376         if (id < 0)
377                 return false;
378
379         return tegra_powergate_is_powered(id);
380 }
381
382 /**
383  * tegra_pmc_cpu_power_on() - power on CPU partition
384  * @cpuid: CPU partition ID
385  */
386 int tegra_pmc_cpu_power_on(unsigned int cpuid)
387 {
388         int id;
389
390         id = tegra_get_cpu_powergate_id(cpuid);
391         if (id < 0)
392                 return id;
393
394         return tegra_powergate_set(id, true);
395 }
396
397 /**
398  * tegra_pmc_cpu_remove_clamping() - remove power clamps for CPU partition
399  * @cpuid: CPU partition ID
400  */
401 int tegra_pmc_cpu_remove_clamping(unsigned int cpuid)
402 {
403         int id;
404
405         id = tegra_get_cpu_powergate_id(cpuid);
406         if (id < 0)
407                 return id;
408
409         return tegra_powergate_remove_clamping(id);
410 }
411 #endif /* CONFIG_SMP */
412
413 static int tegra_pmc_restart_notify(struct notifier_block *this,
414                                     unsigned long action, void *data)
415 {
416         const char *cmd = data;
417         u32 value;
418
419         value = tegra_pmc_readl(PMC_SCRATCH0);
420         value &= ~PMC_SCRATCH0_MODE_MASK;
421
422         if (cmd) {
423                 if (strcmp(cmd, "recovery") == 0)
424                         value |= PMC_SCRATCH0_MODE_RECOVERY;
425
426                 if (strcmp(cmd, "bootloader") == 0)
427                         value |= PMC_SCRATCH0_MODE_BOOTLOADER;
428
429                 if (strcmp(cmd, "forced-recovery") == 0)
430                         value |= PMC_SCRATCH0_MODE_RCM;
431         }
432
433         tegra_pmc_writel(value, PMC_SCRATCH0);
434
435         value = tegra_pmc_readl(0);
436         value |= 0x10;
437         tegra_pmc_writel(value, 0);
438
439         return NOTIFY_DONE;
440 }
441
442 static struct notifier_block tegra_pmc_restart_handler = {
443         .notifier_call = tegra_pmc_restart_notify,
444         .priority = 128,
445 };
446
447 static int powergate_show(struct seq_file *s, void *data)
448 {
449         unsigned int i;
450         int status;
451
452         seq_printf(s, " powergate powered\n");
453         seq_printf(s, "------------------\n");
454
455         for (i = 0; i < pmc->soc->num_powergates; i++) {
456                 status = tegra_powergate_is_powered(i);
457                 if (status < 0)
458                         continue;
459
460                 seq_printf(s, " %9s %7s\n", pmc->soc->powergates[i],
461                            status ? "yes" : "no");
462         }
463
464         return 0;
465 }
466
467 static int powergate_open(struct inode *inode, struct file *file)
468 {
469         return single_open(file, powergate_show, inode->i_private);
470 }
471
472 static const struct file_operations powergate_fops = {
473         .open = powergate_open,
474         .read = seq_read,
475         .llseek = seq_lseek,
476         .release = single_release,
477 };
478
479 static int tegra_powergate_debugfs_init(void)
480 {
481         pmc->debugfs = debugfs_create_file("powergate", S_IRUGO, NULL, NULL,
482                                            &powergate_fops);
483         if (!pmc->debugfs)
484                 return -ENOMEM;
485
486         return 0;
487 }
488
489 static int tegra_io_rail_prepare(unsigned int id, unsigned long *request,
490                                  unsigned long *status, unsigned int *bit)
491 {
492         unsigned long rate, value;
493
494         *bit = id % 32;
495
496         /*
497          * There are two sets of 30 bits to select IO rails, but bits 30 and
498          * 31 are control bits rather than IO rail selection bits.
499          */
500         if (id > 63 || *bit == 30 || *bit == 31)
501                 return -EINVAL;
502
503         if (id < 32) {
504                 *status = IO_DPD_STATUS;
505                 *request = IO_DPD_REQ;
506         } else {
507                 *status = IO_DPD2_STATUS;
508                 *request = IO_DPD2_REQ;
509         }
510
511         rate = clk_get_rate(pmc->clk);
512
513         tegra_pmc_writel(DPD_SAMPLE_ENABLE, DPD_SAMPLE);
514
515         /* must be at least 200 ns, in APB (PCLK) clock cycles */
516         value = DIV_ROUND_UP(1000000000, rate);
517         value = DIV_ROUND_UP(200, value);
518         tegra_pmc_writel(value, SEL_DPD_TIM);
519
520         return 0;
521 }
522
523 static int tegra_io_rail_poll(unsigned long offset, unsigned long mask,
524                               unsigned long val, unsigned long timeout)
525 {
526         unsigned long value;
527
528         timeout = jiffies + msecs_to_jiffies(timeout);
529
530         while (time_after(timeout, jiffies)) {
531                 value = tegra_pmc_readl(offset);
532                 if ((value & mask) == val)
533                         return 0;
534
535                 usleep_range(250, 1000);
536         }
537
538         return -ETIMEDOUT;
539 }
540
541 static void tegra_io_rail_unprepare(void)
542 {
543         tegra_pmc_writel(DPD_SAMPLE_DISABLE, DPD_SAMPLE);
544 }
545
546 int tegra_io_rail_power_on(unsigned int id)
547 {
548         unsigned long request, status, value;
549         unsigned int bit, mask;
550         int err;
551
552         mutex_lock(&pmc->powergates_lock);
553
554         err = tegra_io_rail_prepare(id, &request, &status, &bit);
555         if (err)
556                 goto error;
557
558         mask = 1 << bit;
559
560         value = tegra_pmc_readl(request);
561         value |= mask;
562         value &= ~IO_DPD_REQ_CODE_MASK;
563         value |= IO_DPD_REQ_CODE_OFF;
564         tegra_pmc_writel(value, request);
565
566         err = tegra_io_rail_poll(status, mask, 0, 250);
567         if (err) {
568                 pr_info("tegra_io_rail_poll() failed: %d\n", err);
569                 goto error;
570         }
571
572         tegra_io_rail_unprepare();
573
574 error:
575         mutex_unlock(&pmc->powergates_lock);
576
577         return err;
578 }
579 EXPORT_SYMBOL(tegra_io_rail_power_on);
580
581 int tegra_io_rail_power_off(unsigned int id)
582 {
583         unsigned long request, status, value;
584         unsigned int bit, mask;
585         int err;
586
587         mutex_lock(&pmc->powergates_lock);
588
589         err = tegra_io_rail_prepare(id, &request, &status, &bit);
590         if (err) {
591                 pr_info("tegra_io_rail_prepare() failed: %d\n", err);
592                 goto error;
593         }
594
595         mask = 1 << bit;
596
597         value = tegra_pmc_readl(request);
598         value |= mask;
599         value &= ~IO_DPD_REQ_CODE_MASK;
600         value |= IO_DPD_REQ_CODE_ON;
601         tegra_pmc_writel(value, request);
602
603         err = tegra_io_rail_poll(status, mask, mask, 250);
604         if (err)
605                 goto error;
606
607         tegra_io_rail_unprepare();
608
609 error:
610         mutex_unlock(&pmc->powergates_lock);
611
612         return err;
613 }
614 EXPORT_SYMBOL(tegra_io_rail_power_off);
615
616 #ifdef CONFIG_PM_SLEEP
617 enum tegra_suspend_mode tegra_pmc_get_suspend_mode(void)
618 {
619         return pmc->suspend_mode;
620 }
621
622 void tegra_pmc_set_suspend_mode(enum tegra_suspend_mode mode)
623 {
624         if (mode < TEGRA_SUSPEND_NONE || mode >= TEGRA_MAX_SUSPEND_MODE)
625                 return;
626
627         pmc->suspend_mode = mode;
628 }
629
630 void tegra_pmc_enter_suspend_mode(enum tegra_suspend_mode mode)
631 {
632         unsigned long long rate = 0;
633         u32 value;
634
635         switch (mode) {
636         case TEGRA_SUSPEND_LP1:
637                 rate = 32768;
638                 break;
639
640         case TEGRA_SUSPEND_LP2:
641                 rate = clk_get_rate(pmc->clk);
642                 break;
643
644         default:
645                 break;
646         }
647
648         if (WARN_ON_ONCE(rate == 0))
649                 rate = 100000000;
650
651         if (rate != pmc->rate) {
652                 u64 ticks;
653
654                 ticks = pmc->cpu_good_time * rate + USEC_PER_SEC - 1;
655                 do_div(ticks, USEC_PER_SEC);
656                 tegra_pmc_writel(ticks, PMC_CPUPWRGOOD_TIMER);
657
658                 ticks = pmc->cpu_off_time * rate + USEC_PER_SEC - 1;
659                 do_div(ticks, USEC_PER_SEC);
660                 tegra_pmc_writel(ticks, PMC_CPUPWROFF_TIMER);
661
662                 wmb();
663
664                 pmc->rate = rate;
665         }
666
667         value = tegra_pmc_readl(PMC_CNTRL);
668         value &= ~PMC_CNTRL_SIDE_EFFECT_LP0;
669         value |= PMC_CNTRL_CPU_PWRREQ_OE;
670         tegra_pmc_writel(value, PMC_CNTRL);
671 }
672 #endif
673
674 static int tegra_pmc_parse_dt(struct tegra_pmc *pmc, struct device_node *np)
675 {
676         u32 value, values[2];
677
678         if (of_property_read_u32(np, "nvidia,suspend-mode", &value)) {
679         } else {
680                 switch (value) {
681                 case 0:
682                         pmc->suspend_mode = TEGRA_SUSPEND_LP0;
683                         break;
684
685                 case 1:
686                         pmc->suspend_mode = TEGRA_SUSPEND_LP1;
687                         break;
688
689                 case 2:
690                         pmc->suspend_mode = TEGRA_SUSPEND_LP2;
691                         break;
692
693                 default:
694                         pmc->suspend_mode = TEGRA_SUSPEND_NONE;
695                         break;
696                 }
697         }
698
699         pmc->suspend_mode = tegra_pm_validate_suspend_mode(pmc->suspend_mode);
700
701         if (of_property_read_u32(np, "nvidia,cpu-pwr-good-time", &value))
702                 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
703
704         pmc->cpu_good_time = value;
705
706         if (of_property_read_u32(np, "nvidia,cpu-pwr-off-time", &value))
707                 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
708
709         pmc->cpu_off_time = value;
710
711         if (of_property_read_u32_array(np, "nvidia,core-pwr-good-time",
712                                        values, ARRAY_SIZE(values)))
713                 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
714
715         pmc->core_osc_time = values[0];
716         pmc->core_pmu_time = values[1];
717
718         if (of_property_read_u32(np, "nvidia,core-pwr-off-time", &value))
719                 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
720
721         pmc->core_off_time = value;
722
723         pmc->corereq_high = of_property_read_bool(np,
724                                 "nvidia,core-power-req-active-high");
725
726         pmc->sysclkreq_high = of_property_read_bool(np,
727                                 "nvidia,sys-clock-req-active-high");
728
729         pmc->combined_req = of_property_read_bool(np,
730                                 "nvidia,combined-power-req");
731
732         pmc->cpu_pwr_good_en = of_property_read_bool(np,
733                                 "nvidia,cpu-pwr-good-en");
734
735         if (of_property_read_u32_array(np, "nvidia,lp0-vec", values,
736                                        ARRAY_SIZE(values)))
737                 if (pmc->suspend_mode == TEGRA_SUSPEND_LP0)
738                         pmc->suspend_mode = TEGRA_SUSPEND_LP1;
739
740         pmc->lp0_vec_phys = values[0];
741         pmc->lp0_vec_size = values[1];
742
743         return 0;
744 }
745
746 static void tegra_pmc_init(struct tegra_pmc *pmc)
747 {
748         u32 value;
749
750         /* Always enable CPU power request */
751         value = tegra_pmc_readl(PMC_CNTRL);
752         value |= PMC_CNTRL_CPU_PWRREQ_OE;
753         tegra_pmc_writel(value, PMC_CNTRL);
754
755         value = tegra_pmc_readl(PMC_CNTRL);
756
757         if (pmc->sysclkreq_high)
758                 value &= ~PMC_CNTRL_SYSCLK_POLARITY;
759         else
760                 value |= PMC_CNTRL_SYSCLK_POLARITY;
761
762         /* configure the output polarity while the request is tristated */
763         tegra_pmc_writel(value, PMC_CNTRL);
764
765         /* now enable the request */
766         value = tegra_pmc_readl(PMC_CNTRL);
767         value |= PMC_CNTRL_SYSCLK_OE;
768         tegra_pmc_writel(value, PMC_CNTRL);
769 }
770
771 static void tegra_pmc_init_tsense_reset(struct tegra_pmc *pmc)
772 {
773         static const char disabled[] = "emergency thermal reset disabled";
774         u32 pmu_addr, ctrl_id, reg_addr, reg_data, pinmux;
775         struct device *dev = pmc->dev;
776         struct device_node *np;
777         u32 value, checksum;
778
779         if (!pmc->soc->has_tsense_reset)
780                 return;
781
782         np = of_find_node_by_name(pmc->dev->of_node, "i2c-thermtrip");
783         if (!np) {
784                 dev_warn(dev, "i2c-thermtrip node not found, %s.\n", disabled);
785                 return;
786         }
787
788         if (of_property_read_u32(np, "nvidia,i2c-controller-id", &ctrl_id)) {
789                 dev_err(dev, "I2C controller ID missing, %s.\n", disabled);
790                 goto out;
791         }
792
793         if (of_property_read_u32(np, "nvidia,bus-addr", &pmu_addr)) {
794                 dev_err(dev, "nvidia,bus-addr missing, %s.\n", disabled);
795                 goto out;
796         }
797
798         if (of_property_read_u32(np, "nvidia,reg-addr", &reg_addr)) {
799                 dev_err(dev, "nvidia,reg-addr missing, %s.\n", disabled);
800                 goto out;
801         }
802
803         if (of_property_read_u32(np, "nvidia,reg-data", &reg_data)) {
804                 dev_err(dev, "nvidia,reg-data missing, %s.\n", disabled);
805                 goto out;
806         }
807
808         if (of_property_read_u32(np, "nvidia,pinmux-id", &pinmux))
809                 pinmux = 0;
810
811         value = tegra_pmc_readl(PMC_SENSOR_CTRL);
812         value |= PMC_SENSOR_CTRL_SCRATCH_WRITE;
813         tegra_pmc_writel(value, PMC_SENSOR_CTRL);
814
815         value = (reg_data << PMC_SCRATCH54_DATA_SHIFT) |
816                 (reg_addr << PMC_SCRATCH54_ADDR_SHIFT);
817         tegra_pmc_writel(value, PMC_SCRATCH54);
818
819         value = PMC_SCRATCH55_RESET_TEGRA;
820         value |= ctrl_id << PMC_SCRATCH55_CNTRL_ID_SHIFT;
821         value |= pinmux << PMC_SCRATCH55_PINMUX_SHIFT;
822         value |= pmu_addr << PMC_SCRATCH55_I2CSLV1_SHIFT;
823
824         /*
825          * Calculate checksum of SCRATCH54, SCRATCH55 fields. Bits 23:16 will
826          * contain the checksum and are currently zero, so they are not added.
827          */
828         checksum = reg_addr + reg_data + (value & 0xff) + ((value >> 8) & 0xff)
829                 + ((value >> 24) & 0xff);
830         checksum &= 0xff;
831         checksum = 0x100 - checksum;
832
833         value |= checksum << PMC_SCRATCH55_CHECKSUM_SHIFT;
834
835         tegra_pmc_writel(value, PMC_SCRATCH55);
836
837         value = tegra_pmc_readl(PMC_SENSOR_CTRL);
838         value |= PMC_SENSOR_CTRL_ENABLE_RST;
839         tegra_pmc_writel(value, PMC_SENSOR_CTRL);
840
841         dev_info(pmc->dev, "emergency thermal reset enabled\n");
842
843 out:
844         of_node_put(np);
845 }
846
847 static int tegra_pmc_probe(struct platform_device *pdev)
848 {
849         void __iomem *base;
850         struct resource *res;
851         int err;
852
853         err = tegra_pmc_parse_dt(pmc, pdev->dev.of_node);
854         if (err < 0)
855                 return err;
856
857         /* take over the memory region from the early initialization */
858         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
859         base = devm_ioremap_resource(&pdev->dev, res);
860         if (IS_ERR(base))
861                 return PTR_ERR(base);
862
863         pmc->clk = devm_clk_get(&pdev->dev, "pclk");
864         if (IS_ERR(pmc->clk)) {
865                 err = PTR_ERR(pmc->clk);
866                 dev_err(&pdev->dev, "failed to get pclk: %d\n", err);
867                 return err;
868         }
869
870         pmc->dev = &pdev->dev;
871
872         tegra_pmc_init(pmc);
873
874         tegra_pmc_init_tsense_reset(pmc);
875
876         if (IS_ENABLED(CONFIG_DEBUG_FS)) {
877                 err = tegra_powergate_debugfs_init();
878                 if (err < 0)
879                         return err;
880         }
881
882         err = register_restart_handler(&tegra_pmc_restart_handler);
883         if (err) {
884                 debugfs_remove(pmc->debugfs);
885                 dev_err(&pdev->dev, "unable to register restart handler, %d\n",
886                         err);
887                 return err;
888         }
889
890         mutex_lock(&pmc->powergates_lock);
891         iounmap(pmc->base);
892         pmc->base = base;
893         mutex_unlock(&pmc->powergates_lock);
894
895         return 0;
896 }
897
898 #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM)
899 static int tegra_pmc_suspend(struct device *dev)
900 {
901         tegra_pmc_writel(virt_to_phys(tegra_resume), PMC_SCRATCH41);
902
903         return 0;
904 }
905
906 static int tegra_pmc_resume(struct device *dev)
907 {
908         tegra_pmc_writel(0x0, PMC_SCRATCH41);
909
910         return 0;
911 }
912
913 static SIMPLE_DEV_PM_OPS(tegra_pmc_pm_ops, tegra_pmc_suspend, tegra_pmc_resume);
914
915 #endif
916
917 static const char * const tegra20_powergates[] = {
918         [TEGRA_POWERGATE_CPU] = "cpu",
919         [TEGRA_POWERGATE_3D] = "3d",
920         [TEGRA_POWERGATE_VENC] = "venc",
921         [TEGRA_POWERGATE_VDEC] = "vdec",
922         [TEGRA_POWERGATE_PCIE] = "pcie",
923         [TEGRA_POWERGATE_L2] = "l2",
924         [TEGRA_POWERGATE_MPE] = "mpe",
925 };
926
927 static const struct tegra_pmc_soc tegra20_pmc_soc = {
928         .num_powergates = ARRAY_SIZE(tegra20_powergates),
929         .powergates = tegra20_powergates,
930         .num_cpu_powergates = 0,
931         .cpu_powergates = NULL,
932         .has_tsense_reset = false,
933         .has_gpu_clamps = false,
934 };
935
936 static const char * const tegra30_powergates[] = {
937         [TEGRA_POWERGATE_CPU] = "cpu0",
938         [TEGRA_POWERGATE_3D] = "3d0",
939         [TEGRA_POWERGATE_VENC] = "venc",
940         [TEGRA_POWERGATE_VDEC] = "vdec",
941         [TEGRA_POWERGATE_PCIE] = "pcie",
942         [TEGRA_POWERGATE_L2] = "l2",
943         [TEGRA_POWERGATE_MPE] = "mpe",
944         [TEGRA_POWERGATE_HEG] = "heg",
945         [TEGRA_POWERGATE_SATA] = "sata",
946         [TEGRA_POWERGATE_CPU1] = "cpu1",
947         [TEGRA_POWERGATE_CPU2] = "cpu2",
948         [TEGRA_POWERGATE_CPU3] = "cpu3",
949         [TEGRA_POWERGATE_CELP] = "celp",
950         [TEGRA_POWERGATE_3D1] = "3d1",
951 };
952
953 static const u8 tegra30_cpu_powergates[] = {
954         TEGRA_POWERGATE_CPU,
955         TEGRA_POWERGATE_CPU1,
956         TEGRA_POWERGATE_CPU2,
957         TEGRA_POWERGATE_CPU3,
958 };
959
960 static const struct tegra_pmc_soc tegra30_pmc_soc = {
961         .num_powergates = ARRAY_SIZE(tegra30_powergates),
962         .powergates = tegra30_powergates,
963         .num_cpu_powergates = ARRAY_SIZE(tegra30_cpu_powergates),
964         .cpu_powergates = tegra30_cpu_powergates,
965         .has_tsense_reset = true,
966         .has_gpu_clamps = false,
967 };
968
969 static const char * const tegra114_powergates[] = {
970         [TEGRA_POWERGATE_CPU] = "crail",
971         [TEGRA_POWERGATE_3D] = "3d",
972         [TEGRA_POWERGATE_VENC] = "venc",
973         [TEGRA_POWERGATE_VDEC] = "vdec",
974         [TEGRA_POWERGATE_MPE] = "mpe",
975         [TEGRA_POWERGATE_HEG] = "heg",
976         [TEGRA_POWERGATE_CPU1] = "cpu1",
977         [TEGRA_POWERGATE_CPU2] = "cpu2",
978         [TEGRA_POWERGATE_CPU3] = "cpu3",
979         [TEGRA_POWERGATE_CELP] = "celp",
980         [TEGRA_POWERGATE_CPU0] = "cpu0",
981         [TEGRA_POWERGATE_C0NC] = "c0nc",
982         [TEGRA_POWERGATE_C1NC] = "c1nc",
983         [TEGRA_POWERGATE_DIS] = "dis",
984         [TEGRA_POWERGATE_DISB] = "disb",
985         [TEGRA_POWERGATE_XUSBA] = "xusba",
986         [TEGRA_POWERGATE_XUSBB] = "xusbb",
987         [TEGRA_POWERGATE_XUSBC] = "xusbc",
988 };
989
990 static const u8 tegra114_cpu_powergates[] = {
991         TEGRA_POWERGATE_CPU0,
992         TEGRA_POWERGATE_CPU1,
993         TEGRA_POWERGATE_CPU2,
994         TEGRA_POWERGATE_CPU3,
995 };
996
997 static const struct tegra_pmc_soc tegra114_pmc_soc = {
998         .num_powergates = ARRAY_SIZE(tegra114_powergates),
999         .powergates = tegra114_powergates,
1000         .num_cpu_powergates = ARRAY_SIZE(tegra114_cpu_powergates),
1001         .cpu_powergates = tegra114_cpu_powergates,
1002         .has_tsense_reset = true,
1003         .has_gpu_clamps = false,
1004 };
1005
1006 static const char * const tegra124_powergates[] = {
1007         [TEGRA_POWERGATE_CPU] = "crail",
1008         [TEGRA_POWERGATE_3D] = "3d",
1009         [TEGRA_POWERGATE_VENC] = "venc",
1010         [TEGRA_POWERGATE_PCIE] = "pcie",
1011         [TEGRA_POWERGATE_VDEC] = "vdec",
1012         [TEGRA_POWERGATE_MPE] = "mpe",
1013         [TEGRA_POWERGATE_HEG] = "heg",
1014         [TEGRA_POWERGATE_SATA] = "sata",
1015         [TEGRA_POWERGATE_CPU1] = "cpu1",
1016         [TEGRA_POWERGATE_CPU2] = "cpu2",
1017         [TEGRA_POWERGATE_CPU3] = "cpu3",
1018         [TEGRA_POWERGATE_CELP] = "celp",
1019         [TEGRA_POWERGATE_CPU0] = "cpu0",
1020         [TEGRA_POWERGATE_C0NC] = "c0nc",
1021         [TEGRA_POWERGATE_C1NC] = "c1nc",
1022         [TEGRA_POWERGATE_SOR] = "sor",
1023         [TEGRA_POWERGATE_DIS] = "dis",
1024         [TEGRA_POWERGATE_DISB] = "disb",
1025         [TEGRA_POWERGATE_XUSBA] = "xusba",
1026         [TEGRA_POWERGATE_XUSBB] = "xusbb",
1027         [TEGRA_POWERGATE_XUSBC] = "xusbc",
1028         [TEGRA_POWERGATE_VIC] = "vic",
1029         [TEGRA_POWERGATE_IRAM] = "iram",
1030 };
1031
1032 static const u8 tegra124_cpu_powergates[] = {
1033         TEGRA_POWERGATE_CPU0,
1034         TEGRA_POWERGATE_CPU1,
1035         TEGRA_POWERGATE_CPU2,
1036         TEGRA_POWERGATE_CPU3,
1037 };
1038
1039 static const struct tegra_pmc_soc tegra124_pmc_soc = {
1040         .num_powergates = ARRAY_SIZE(tegra124_powergates),
1041         .powergates = tegra124_powergates,
1042         .num_cpu_powergates = ARRAY_SIZE(tegra124_cpu_powergates),
1043         .cpu_powergates = tegra124_cpu_powergates,
1044         .has_tsense_reset = true,
1045         .has_gpu_clamps = true,
1046 };
1047
1048 static const char * const tegra210_powergates[] = {
1049         [TEGRA_POWERGATE_CPU] = "crail",
1050         [TEGRA_POWERGATE_3D] = "3d",
1051         [TEGRA_POWERGATE_VENC] = "venc",
1052         [TEGRA_POWERGATE_PCIE] = "pcie",
1053         [TEGRA_POWERGATE_MPE] = "mpe",
1054         [TEGRA_POWERGATE_SATA] = "sata",
1055         [TEGRA_POWERGATE_CPU1] = "cpu1",
1056         [TEGRA_POWERGATE_CPU2] = "cpu2",
1057         [TEGRA_POWERGATE_CPU3] = "cpu3",
1058         [TEGRA_POWERGATE_CPU0] = "cpu0",
1059         [TEGRA_POWERGATE_C0NC] = "c0nc",
1060         [TEGRA_POWERGATE_SOR] = "sor",
1061         [TEGRA_POWERGATE_DIS] = "dis",
1062         [TEGRA_POWERGATE_DISB] = "disb",
1063         [TEGRA_POWERGATE_XUSBA] = "xusba",
1064         [TEGRA_POWERGATE_XUSBB] = "xusbb",
1065         [TEGRA_POWERGATE_XUSBC] = "xusbc",
1066         [TEGRA_POWERGATE_VIC] = "vic",
1067         [TEGRA_POWERGATE_IRAM] = "iram",
1068         [TEGRA_POWERGATE_NVDEC] = "nvdec",
1069         [TEGRA_POWERGATE_NVJPG] = "nvjpg",
1070         [TEGRA_POWERGATE_AUD] = "aud",
1071         [TEGRA_POWERGATE_DFD] = "dfd",
1072         [TEGRA_POWERGATE_VE2] = "ve2",
1073 };
1074
1075 static const u8 tegra210_cpu_powergates[] = {
1076         TEGRA_POWERGATE_CPU0,
1077         TEGRA_POWERGATE_CPU1,
1078         TEGRA_POWERGATE_CPU2,
1079         TEGRA_POWERGATE_CPU3,
1080 };
1081
1082 static const struct tegra_pmc_soc tegra210_pmc_soc = {
1083         .num_powergates = ARRAY_SIZE(tegra210_powergates),
1084         .powergates = tegra210_powergates,
1085         .num_cpu_powergates = ARRAY_SIZE(tegra210_cpu_powergates),
1086         .cpu_powergates = tegra210_cpu_powergates,
1087         .has_tsense_reset = true,
1088         .has_gpu_clamps = true,
1089 };
1090
1091 static const struct of_device_id tegra_pmc_match[] = {
1092         { .compatible = "nvidia,tegra210-pmc", .data = &tegra210_pmc_soc },
1093         { .compatible = "nvidia,tegra132-pmc", .data = &tegra124_pmc_soc },
1094         { .compatible = "nvidia,tegra124-pmc", .data = &tegra124_pmc_soc },
1095         { .compatible = "nvidia,tegra114-pmc", .data = &tegra114_pmc_soc },
1096         { .compatible = "nvidia,tegra30-pmc", .data = &tegra30_pmc_soc },
1097         { .compatible = "nvidia,tegra20-pmc", .data = &tegra20_pmc_soc },
1098         { }
1099 };
1100
1101 static struct platform_driver tegra_pmc_driver = {
1102         .driver = {
1103                 .name = "tegra-pmc",
1104                 .suppress_bind_attrs = true,
1105                 .of_match_table = tegra_pmc_match,
1106 #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM)
1107                 .pm = &tegra_pmc_pm_ops,
1108 #endif
1109         },
1110         .probe = tegra_pmc_probe,
1111 };
1112 builtin_platform_driver(tegra_pmc_driver);
1113
1114 /*
1115  * Early initialization to allow access to registers in the very early boot
1116  * process.
1117  */
1118 static int __init tegra_pmc_early_init(void)
1119 {
1120         const struct of_device_id *match;
1121         struct device_node *np;
1122         struct resource regs;
1123         bool invert;
1124         u32 value;
1125
1126         np = of_find_matching_node_and_match(NULL, tegra_pmc_match, &match);
1127         if (!np) {
1128                 /*
1129                  * Fall back to legacy initialization for 32-bit ARM only. All
1130                  * 64-bit ARM device tree files for Tegra are required to have
1131                  * a PMC node.
1132                  *
1133                  * This is for backwards-compatibility with old device trees
1134                  * that didn't contain a PMC node. Note that in this case the
1135                  * SoC data can't be matched and therefore powergating is
1136                  * disabled.
1137                  */
1138                 if (IS_ENABLED(CONFIG_ARM) && soc_is_tegra()) {
1139                         pr_warn("DT node not found, powergating disabled\n");
1140
1141                         regs.start = 0x7000e400;
1142                         regs.end = 0x7000e7ff;
1143                         regs.flags = IORESOURCE_MEM;
1144
1145                         pr_warn("Using memory region %pR\n", &regs);
1146                 } else {
1147                         /*
1148                          * At this point we're not running on Tegra, so play
1149                          * nice with multi-platform kernels.
1150                          */
1151                         return 0;
1152                 }
1153         } else {
1154                 /*
1155                  * Extract information from the device tree if we've found a
1156                  * matching node.
1157                  */
1158                 if (of_address_to_resource(np, 0, &regs) < 0) {
1159                         pr_err("failed to get PMC registers\n");
1160                         return -ENXIO;
1161                 }
1162
1163                 pmc->soc = match->data;
1164         }
1165
1166         pmc->base = ioremap_nocache(regs.start, resource_size(&regs));
1167         if (!pmc->base) {
1168                 pr_err("failed to map PMC registers\n");
1169                 return -ENXIO;
1170         }
1171
1172         mutex_init(&pmc->powergates_lock);
1173
1174         /*
1175          * Invert the interrupt polarity if a PMC device tree node exists and
1176          * contains the nvidia,invert-interrupt property.
1177          */
1178         invert = of_property_read_bool(np, "nvidia,invert-interrupt");
1179
1180         value = tegra_pmc_readl(PMC_CNTRL);
1181
1182         if (invert)
1183                 value |= PMC_CNTRL_INTR_POLARITY;
1184         else
1185                 value &= ~PMC_CNTRL_INTR_POLARITY;
1186
1187         tegra_pmc_writel(value, PMC_CNTRL);
1188
1189         return 0;
1190 }
1191 early_initcall(tegra_pmc_early_init);