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