GNU Linux-libre 4.9.309-gnu1
[releases.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/of_platform.h>
35 #include <linux/platform_device.h>
36 #include <linux/pm_domain.h>
37 #include <linux/reboot.h>
38 #include <linux/reset.h>
39 #include <linux/seq_file.h>
40 #include <linux/slab.h>
41 #include <linux/spinlock.h>
42
43 #include <soc/tegra/common.h>
44 #include <soc/tegra/fuse.h>
45 #include <soc/tegra/pmc.h>
46
47 #define PMC_CNTRL                       0x0
48 #define  PMC_CNTRL_SYSCLK_POLARITY      (1 << 10)  /* sys clk polarity */
49 #define  PMC_CNTRL_SYSCLK_OE            (1 << 11)  /* system clock enable */
50 #define  PMC_CNTRL_SIDE_EFFECT_LP0      (1 << 14)  /* LP0 when CPU pwr gated */
51 #define  PMC_CNTRL_CPU_PWRREQ_POLARITY  (1 << 15)  /* CPU pwr req polarity */
52 #define  PMC_CNTRL_CPU_PWRREQ_OE        (1 << 16)  /* CPU pwr req enable */
53 #define  PMC_CNTRL_INTR_POLARITY        (1 << 17)  /* inverts INTR polarity */
54 #define  PMC_CNTRL_MAIN_RST             (1 <<  4)
55
56 #define DPD_SAMPLE                      0x020
57 #define  DPD_SAMPLE_ENABLE              (1 << 0)
58 #define  DPD_SAMPLE_DISABLE             (0 << 0)
59
60 #define PWRGATE_TOGGLE                  0x30
61 #define  PWRGATE_TOGGLE_START           (1 << 8)
62
63 #define REMOVE_CLAMPING                 0x34
64
65 #define PWRGATE_STATUS                  0x38
66
67 #define PMC_SCRATCH0                    0x50
68 #define  PMC_SCRATCH0_MODE_RECOVERY     (1 << 31)
69 #define  PMC_SCRATCH0_MODE_BOOTLOADER   (1 << 30)
70 #define  PMC_SCRATCH0_MODE_RCM          (1 << 1)
71 #define  PMC_SCRATCH0_MODE_MASK         (PMC_SCRATCH0_MODE_RECOVERY | \
72                                          PMC_SCRATCH0_MODE_BOOTLOADER | \
73                                          PMC_SCRATCH0_MODE_RCM)
74
75 #define PMC_CPUPWRGOOD_TIMER            0xc8
76 #define PMC_CPUPWROFF_TIMER             0xcc
77
78 #define PMC_SCRATCH41                   0x140
79
80 #define PMC_SENSOR_CTRL                 0x1b0
81 #define PMC_SENSOR_CTRL_SCRATCH_WRITE   (1 << 2)
82 #define PMC_SENSOR_CTRL_ENABLE_RST      (1 << 1)
83
84 #define PMC_RST_STATUS                  0x1b4
85 #define  PMC_RST_STATUS_POR             0
86 #define  PMC_RST_STATUS_WATCHDOG        1
87 #define  PMC_RST_STATUS_SENSOR          2
88 #define  PMC_RST_STATUS_SW_MAIN         3
89 #define  PMC_RST_STATUS_LP0             4
90 #define  PMC_RST_STATUS_AOTAG           5
91
92 #define IO_DPD_REQ                      0x1b8
93 #define  IO_DPD_REQ_CODE_IDLE           (0 << 30)
94 #define  IO_DPD_REQ_CODE_OFF            (1 << 30)
95 #define  IO_DPD_REQ_CODE_ON             (2 << 30)
96 #define  IO_DPD_REQ_CODE_MASK           (3 << 30)
97
98 #define IO_DPD_STATUS                   0x1bc
99 #define IO_DPD2_REQ                     0x1c0
100 #define IO_DPD2_STATUS                  0x1c4
101 #define SEL_DPD_TIM                     0x1c8
102
103 #define PMC_SCRATCH54                   0x258
104 #define PMC_SCRATCH54_DATA_SHIFT        8
105 #define PMC_SCRATCH54_ADDR_SHIFT        0
106
107 #define PMC_SCRATCH55                   0x25c
108 #define PMC_SCRATCH55_RESET_TEGRA       (1 << 31)
109 #define PMC_SCRATCH55_CNTRL_ID_SHIFT    27
110 #define PMC_SCRATCH55_PINMUX_SHIFT      24
111 #define PMC_SCRATCH55_16BITOP           (1 << 15)
112 #define PMC_SCRATCH55_CHECKSUM_SHIFT    16
113 #define PMC_SCRATCH55_I2CSLV1_SHIFT     0
114
115 #define GPU_RG_CNTRL                    0x2d4
116
117 struct tegra_powergate {
118         struct generic_pm_domain genpd;
119         struct tegra_pmc *pmc;
120         unsigned int id;
121         struct clk **clks;
122         unsigned int num_clks;
123         struct reset_control **resets;
124         unsigned int num_resets;
125 };
126
127 struct tegra_pmc_soc {
128         unsigned int num_powergates;
129         const char *const *powergates;
130         unsigned int num_cpu_powergates;
131         const u8 *cpu_powergates;
132
133         bool has_tsense_reset;
134         bool has_gpu_clamps;
135 };
136
137 /**
138  * struct tegra_pmc - NVIDIA Tegra PMC
139  * @dev: pointer to PMC device structure
140  * @base: pointer to I/O remapped register region
141  * @clk: pointer to pclk clock
142  * @soc: pointer to SoC data structure
143  * @debugfs: pointer to debugfs entry
144  * @rate: currently configured rate of pclk
145  * @suspend_mode: lowest suspend mode available
146  * @cpu_good_time: CPU power good time (in microseconds)
147  * @cpu_off_time: CPU power off time (in microsecends)
148  * @core_osc_time: core power good OSC time (in microseconds)
149  * @core_pmu_time: core power good PMU time (in microseconds)
150  * @core_off_time: core power off time (in microseconds)
151  * @corereq_high: core power request is active-high
152  * @sysclkreq_high: system clock request is active-high
153  * @combined_req: combined power request for CPU & core
154  * @cpu_pwr_good_en: CPU power good signal is enabled
155  * @lp0_vec_phys: physical base address of the LP0 warm boot code
156  * @lp0_vec_size: size of the LP0 warm boot code
157  * @powergates_available: Bitmap of available power gates
158  * @powergates_lock: mutex for power gate register access
159  */
160 struct tegra_pmc {
161         struct device *dev;
162         void __iomem *base;
163         struct clk *clk;
164         struct dentry *debugfs;
165
166         const struct tegra_pmc_soc *soc;
167
168         unsigned long rate;
169
170         enum tegra_suspend_mode suspend_mode;
171         u32 cpu_good_time;
172         u32 cpu_off_time;
173         u32 core_osc_time;
174         u32 core_pmu_time;
175         u32 core_off_time;
176         bool corereq_high;
177         bool sysclkreq_high;
178         bool combined_req;
179         bool cpu_pwr_good_en;
180         u32 lp0_vec_phys;
181         u32 lp0_vec_size;
182         DECLARE_BITMAP(powergates_available, TEGRA_POWERGATE_MAX);
183
184         struct mutex powergates_lock;
185 };
186
187 static struct tegra_pmc *pmc = &(struct tegra_pmc) {
188         .base = NULL,
189         .suspend_mode = TEGRA_SUSPEND_NONE,
190 };
191
192 static inline struct tegra_powergate *
193 to_powergate(struct generic_pm_domain *domain)
194 {
195         return container_of(domain, struct tegra_powergate, genpd);
196 }
197
198 static u32 tegra_pmc_readl(unsigned long offset)
199 {
200         return readl(pmc->base + offset);
201 }
202
203 static void tegra_pmc_writel(u32 value, unsigned long offset)
204 {
205         writel(value, pmc->base + offset);
206 }
207
208 static inline bool tegra_powergate_state(int id)
209 {
210         if (id == TEGRA_POWERGATE_3D && pmc->soc->has_gpu_clamps)
211                 return (tegra_pmc_readl(GPU_RG_CNTRL) & 0x1) == 0;
212         else
213                 return (tegra_pmc_readl(PWRGATE_STATUS) & BIT(id)) != 0;
214 }
215
216 static inline bool tegra_powergate_is_valid(int id)
217 {
218         return (pmc->soc && pmc->soc->powergates[id]);
219 }
220
221 static inline bool tegra_powergate_is_available(int id)
222 {
223         return test_bit(id, pmc->powergates_available);
224 }
225
226 static int tegra_powergate_lookup(struct tegra_pmc *pmc, const char *name)
227 {
228         unsigned int i;
229
230         if (!pmc || !pmc->soc || !name)
231                 return -EINVAL;
232
233         for (i = 0; i < pmc->soc->num_powergates; i++) {
234                 if (!tegra_powergate_is_valid(i))
235                         continue;
236
237                 if (!strcmp(name, pmc->soc->powergates[i]))
238                         return i;
239         }
240
241         dev_err(pmc->dev, "powergate %s not found\n", name);
242
243         return -ENODEV;
244 }
245
246 /**
247  * tegra_powergate_set() - set the state of a partition
248  * @id: partition ID
249  * @new_state: new state of the partition
250  */
251 static int tegra_powergate_set(unsigned int id, bool new_state)
252 {
253         bool status;
254         int err;
255
256         if (id == TEGRA_POWERGATE_3D && pmc->soc->has_gpu_clamps)
257                 return -EINVAL;
258
259         mutex_lock(&pmc->powergates_lock);
260
261         if (tegra_powergate_state(id) == new_state) {
262                 mutex_unlock(&pmc->powergates_lock);
263                 return 0;
264         }
265
266         tegra_pmc_writel(PWRGATE_TOGGLE_START | id, PWRGATE_TOGGLE);
267
268         err = readx_poll_timeout(tegra_powergate_state, id, status,
269                                  status == new_state, 10, 100000);
270
271         mutex_unlock(&pmc->powergates_lock);
272
273         return err;
274 }
275
276 static int __tegra_powergate_remove_clamping(unsigned int id)
277 {
278         u32 mask;
279
280         mutex_lock(&pmc->powergates_lock);
281
282         /*
283          * On Tegra124 and later, the clamps for the GPU are controlled by a
284          * separate register (with different semantics).
285          */
286         if (id == TEGRA_POWERGATE_3D) {
287                 if (pmc->soc->has_gpu_clamps) {
288                         tegra_pmc_writel(0, GPU_RG_CNTRL);
289                         goto out;
290                 }
291         }
292
293         /*
294          * Tegra 2 has a bug where PCIE and VDE clamping masks are
295          * swapped relatively to the partition ids
296          */
297         if (id == TEGRA_POWERGATE_VDEC)
298                 mask = (1 << TEGRA_POWERGATE_PCIE);
299         else if (id == TEGRA_POWERGATE_PCIE)
300                 mask = (1 << TEGRA_POWERGATE_VDEC);
301         else
302                 mask = (1 << id);
303
304         tegra_pmc_writel(mask, REMOVE_CLAMPING);
305
306 out:
307         mutex_unlock(&pmc->powergates_lock);
308
309         return 0;
310 }
311
312 static void tegra_powergate_disable_clocks(struct tegra_powergate *pg)
313 {
314         unsigned int i;
315
316         for (i = 0; i < pg->num_clks; i++)
317                 clk_disable_unprepare(pg->clks[i]);
318 }
319
320 static int tegra_powergate_enable_clocks(struct tegra_powergate *pg)
321 {
322         unsigned int i;
323         int err;
324
325         for (i = 0; i < pg->num_clks; i++) {
326                 err = clk_prepare_enable(pg->clks[i]);
327                 if (err)
328                         goto out;
329         }
330
331         return 0;
332
333 out:
334         while (i--)
335                 clk_disable_unprepare(pg->clks[i]);
336
337         return err;
338 }
339
340 static int tegra_powergate_reset_assert(struct tegra_powergate *pg)
341 {
342         unsigned int i;
343         int err;
344
345         for (i = 0; i < pg->num_resets; i++) {
346                 err = reset_control_assert(pg->resets[i]);
347                 if (err)
348                         return err;
349         }
350
351         return 0;
352 }
353
354 static int tegra_powergate_reset_deassert(struct tegra_powergate *pg)
355 {
356         unsigned int i;
357         int err;
358
359         for (i = 0; i < pg->num_resets; i++) {
360                 err = reset_control_deassert(pg->resets[i]);
361                 if (err)
362                         return err;
363         }
364
365         return 0;
366 }
367
368 static int tegra_powergate_power_up(struct tegra_powergate *pg,
369                                     bool disable_clocks)
370 {
371         int err;
372
373         err = tegra_powergate_reset_assert(pg);
374         if (err)
375                 return err;
376
377         usleep_range(10, 20);
378
379         err = tegra_powergate_set(pg->id, true);
380         if (err < 0)
381                 return err;
382
383         usleep_range(10, 20);
384
385         err = tegra_powergate_enable_clocks(pg);
386         if (err)
387                 goto powergate_off;
388
389         usleep_range(10, 20);
390
391         err = __tegra_powergate_remove_clamping(pg->id);
392         if (err)
393                 goto disable_clks;
394
395         usleep_range(10, 20);
396
397         err = tegra_powergate_reset_deassert(pg);
398         if (err)
399                 goto disable_clks;
400
401         usleep_range(10, 20);
402
403         if (disable_clocks)
404                 tegra_powergate_disable_clocks(pg);
405
406         return 0;
407
408 disable_clks:
409         tegra_powergate_disable_clocks(pg);
410         usleep_range(10, 20);
411
412 powergate_off:
413         tegra_powergate_set(pg->id, false);
414
415         return err;
416 }
417
418 static int tegra_powergate_power_down(struct tegra_powergate *pg)
419 {
420         int err;
421
422         err = tegra_powergate_enable_clocks(pg);
423         if (err)
424                 return err;
425
426         usleep_range(10, 20);
427
428         err = tegra_powergate_reset_assert(pg);
429         if (err)
430                 goto disable_clks;
431
432         usleep_range(10, 20);
433
434         tegra_powergate_disable_clocks(pg);
435
436         usleep_range(10, 20);
437
438         err = tegra_powergate_set(pg->id, false);
439         if (err)
440                 goto assert_resets;
441
442         return 0;
443
444 assert_resets:
445         tegra_powergate_enable_clocks(pg);
446         usleep_range(10, 20);
447         tegra_powergate_reset_deassert(pg);
448         usleep_range(10, 20);
449
450 disable_clks:
451         tegra_powergate_disable_clocks(pg);
452
453         return err;
454 }
455
456 static int tegra_genpd_power_on(struct generic_pm_domain *domain)
457 {
458         struct tegra_powergate *pg = to_powergate(domain);
459         struct tegra_pmc *pmc = pg->pmc;
460         int err;
461
462         err = tegra_powergate_power_up(pg, true);
463         if (err)
464                 dev_err(pmc->dev, "failed to turn on PM domain %s: %d\n",
465                         pg->genpd.name, err);
466
467         return err;
468 }
469
470 static int tegra_genpd_power_off(struct generic_pm_domain *domain)
471 {
472         struct tegra_powergate *pg = to_powergate(domain);
473         struct tegra_pmc *pmc = pg->pmc;
474         int err;
475
476         err = tegra_powergate_power_down(pg);
477         if (err)
478                 dev_err(pmc->dev, "failed to turn off PM domain %s: %d\n",
479                         pg->genpd.name, err);
480
481         return err;
482 }
483
484 /**
485  * tegra_powergate_power_on() - power on partition
486  * @id: partition ID
487  */
488 int tegra_powergate_power_on(unsigned int id)
489 {
490         if (!tegra_powergate_is_available(id))
491                 return -EINVAL;
492
493         return tegra_powergate_set(id, true);
494 }
495
496 /**
497  * tegra_powergate_power_off() - power off partition
498  * @id: partition ID
499  */
500 int tegra_powergate_power_off(unsigned int id)
501 {
502         if (!tegra_powergate_is_available(id))
503                 return -EINVAL;
504
505         return tegra_powergate_set(id, false);
506 }
507 EXPORT_SYMBOL(tegra_powergate_power_off);
508
509 /**
510  * tegra_powergate_is_powered() - check if partition is powered
511  * @id: partition ID
512  */
513 int tegra_powergate_is_powered(unsigned int id)
514 {
515         if (!tegra_powergate_is_valid(id))
516                 return -EINVAL;
517
518         return tegra_powergate_state(id);
519 }
520
521 /**
522  * tegra_powergate_remove_clamping() - remove power clamps for partition
523  * @id: partition ID
524  */
525 int tegra_powergate_remove_clamping(unsigned int id)
526 {
527         if (!tegra_powergate_is_available(id))
528                 return -EINVAL;
529
530         return __tegra_powergate_remove_clamping(id);
531 }
532 EXPORT_SYMBOL(tegra_powergate_remove_clamping);
533
534 /**
535  * tegra_powergate_sequence_power_up() - power up partition
536  * @id: partition ID
537  * @clk: clock for partition
538  * @rst: reset for partition
539  *
540  * Must be called with clk disabled, and returns with clk enabled.
541  */
542 int tegra_powergate_sequence_power_up(unsigned int id, struct clk *clk,
543                                       struct reset_control *rst)
544 {
545         struct tegra_powergate pg;
546         int err;
547
548         if (!tegra_powergate_is_available(id))
549                 return -EINVAL;
550
551         pg.id = id;
552         pg.clks = &clk;
553         pg.num_clks = 1;
554         pg.resets = &rst;
555         pg.num_resets = 1;
556
557         err = tegra_powergate_power_up(&pg, false);
558         if (err)
559                 pr_err("failed to turn on partition %d: %d\n", id, err);
560
561         return err;
562 }
563 EXPORT_SYMBOL(tegra_powergate_sequence_power_up);
564
565 #ifdef CONFIG_SMP
566 /**
567  * tegra_get_cpu_powergate_id() - convert from CPU ID to partition ID
568  * @cpuid: CPU partition ID
569  *
570  * Returns the partition ID corresponding to the CPU partition ID or a
571  * negative error code on failure.
572  */
573 static int tegra_get_cpu_powergate_id(unsigned int cpuid)
574 {
575         if (pmc->soc && cpuid < pmc->soc->num_cpu_powergates)
576                 return pmc->soc->cpu_powergates[cpuid];
577
578         return -EINVAL;
579 }
580
581 /**
582  * tegra_pmc_cpu_is_powered() - check if CPU partition is powered
583  * @cpuid: CPU partition ID
584  */
585 bool tegra_pmc_cpu_is_powered(unsigned int cpuid)
586 {
587         int id;
588
589         id = tegra_get_cpu_powergate_id(cpuid);
590         if (id < 0)
591                 return false;
592
593         return tegra_powergate_is_powered(id);
594 }
595
596 /**
597  * tegra_pmc_cpu_power_on() - power on CPU partition
598  * @cpuid: CPU partition ID
599  */
600 int tegra_pmc_cpu_power_on(unsigned int cpuid)
601 {
602         int id;
603
604         id = tegra_get_cpu_powergate_id(cpuid);
605         if (id < 0)
606                 return id;
607
608         return tegra_powergate_set(id, true);
609 }
610
611 /**
612  * tegra_pmc_cpu_remove_clamping() - remove power clamps for CPU partition
613  * @cpuid: CPU partition ID
614  */
615 int tegra_pmc_cpu_remove_clamping(unsigned int cpuid)
616 {
617         int id;
618
619         id = tegra_get_cpu_powergate_id(cpuid);
620         if (id < 0)
621                 return id;
622
623         return tegra_powergate_remove_clamping(id);
624 }
625 #endif /* CONFIG_SMP */
626
627 static int tegra_pmc_restart_notify(struct notifier_block *this,
628                                     unsigned long action, void *data)
629 {
630         const char *cmd = data;
631         u32 value;
632
633         value = tegra_pmc_readl(PMC_SCRATCH0);
634         value &= ~PMC_SCRATCH0_MODE_MASK;
635
636         if (cmd) {
637                 if (strcmp(cmd, "recovery") == 0)
638                         value |= PMC_SCRATCH0_MODE_RECOVERY;
639
640                 if (strcmp(cmd, "bootloader") == 0)
641                         value |= PMC_SCRATCH0_MODE_BOOTLOADER;
642
643                 if (strcmp(cmd, "forced-recovery") == 0)
644                         value |= PMC_SCRATCH0_MODE_RCM;
645         }
646
647         tegra_pmc_writel(value, PMC_SCRATCH0);
648
649         /* reset everything but PMC_SCRATCH0 and PMC_RST_STATUS */
650         value = tegra_pmc_readl(PMC_CNTRL);
651         value |= PMC_CNTRL_MAIN_RST;
652         tegra_pmc_writel(value, PMC_CNTRL);
653
654         return NOTIFY_DONE;
655 }
656
657 static struct notifier_block tegra_pmc_restart_handler = {
658         .notifier_call = tegra_pmc_restart_notify,
659         .priority = 128,
660 };
661
662 static int powergate_show(struct seq_file *s, void *data)
663 {
664         unsigned int i;
665         int status;
666
667         seq_printf(s, " powergate powered\n");
668         seq_printf(s, "------------------\n");
669
670         for (i = 0; i < pmc->soc->num_powergates; i++) {
671                 status = tegra_powergate_is_powered(i);
672                 if (status < 0)
673                         continue;
674
675                 seq_printf(s, " %9s %7s\n", pmc->soc->powergates[i],
676                            status ? "yes" : "no");
677         }
678
679         return 0;
680 }
681
682 static int powergate_open(struct inode *inode, struct file *file)
683 {
684         return single_open(file, powergate_show, inode->i_private);
685 }
686
687 static const struct file_operations powergate_fops = {
688         .open = powergate_open,
689         .read = seq_read,
690         .llseek = seq_lseek,
691         .release = single_release,
692 };
693
694 static int tegra_powergate_debugfs_init(void)
695 {
696         pmc->debugfs = debugfs_create_file("powergate", S_IRUGO, NULL, NULL,
697                                            &powergate_fops);
698         if (!pmc->debugfs)
699                 return -ENOMEM;
700
701         return 0;
702 }
703
704 static int tegra_powergate_of_get_clks(struct tegra_powergate *pg,
705                                        struct device_node *np)
706 {
707         struct clk *clk;
708         unsigned int i, count;
709         int err;
710
711         count = of_count_phandle_with_args(np, "clocks", "#clock-cells");
712         if (count == 0)
713                 return -ENODEV;
714
715         pg->clks = kcalloc(count, sizeof(clk), GFP_KERNEL);
716         if (!pg->clks)
717                 return -ENOMEM;
718
719         for (i = 0; i < count; i++) {
720                 pg->clks[i] = of_clk_get(np, i);
721                 if (IS_ERR(pg->clks[i])) {
722                         err = PTR_ERR(pg->clks[i]);
723                         goto err;
724                 }
725         }
726
727         pg->num_clks = count;
728
729         return 0;
730
731 err:
732         while (i--)
733                 clk_put(pg->clks[i]);
734
735         kfree(pg->clks);
736
737         return err;
738 }
739
740 static int tegra_powergate_of_get_resets(struct tegra_powergate *pg,
741                                          struct device_node *np, bool off)
742 {
743         struct reset_control *rst;
744         unsigned int i, count;
745         int err;
746
747         count = of_count_phandle_with_args(np, "resets", "#reset-cells");
748         if (count == 0)
749                 return -ENODEV;
750
751         pg->resets = kcalloc(count, sizeof(rst), GFP_KERNEL);
752         if (!pg->resets)
753                 return -ENOMEM;
754
755         for (i = 0; i < count; i++) {
756                 pg->resets[i] = of_reset_control_get_by_index(np, i);
757                 if (IS_ERR(pg->resets[i])) {
758                         err = PTR_ERR(pg->resets[i]);
759                         goto error;
760                 }
761
762                 if (off)
763                         err = reset_control_assert(pg->resets[i]);
764                 else
765                         err = reset_control_deassert(pg->resets[i]);
766
767                 if (err) {
768                         reset_control_put(pg->resets[i]);
769                         goto error;
770                 }
771         }
772
773         pg->num_resets = count;
774
775         return 0;
776
777 error:
778         while (i--)
779                 reset_control_put(pg->resets[i]);
780
781         kfree(pg->resets);
782
783         return err;
784 }
785
786 static void tegra_powergate_add(struct tegra_pmc *pmc, struct device_node *np)
787 {
788         struct tegra_powergate *pg;
789         int id, err;
790         bool off;
791
792         pg = kzalloc(sizeof(*pg), GFP_KERNEL);
793         if (!pg)
794                 return;
795
796         id = tegra_powergate_lookup(pmc, np->name);
797         if (id < 0) {
798                 dev_err(pmc->dev, "powergate lookup failed for %s: %d\n",
799                         np->name, id);
800                 goto free_mem;
801         }
802
803         /*
804          * Clear the bit for this powergate so it cannot be managed
805          * directly via the legacy APIs for controlling powergates.
806          */
807         clear_bit(id, pmc->powergates_available);
808
809         pg->id = id;
810         pg->genpd.name = np->name;
811         pg->genpd.power_off = tegra_genpd_power_off;
812         pg->genpd.power_on = tegra_genpd_power_on;
813         pg->pmc = pmc;
814
815         off = !tegra_powergate_is_powered(pg->id);
816
817         err = tegra_powergate_of_get_clks(pg, np);
818         if (err < 0) {
819                 dev_err(pmc->dev, "failed to get clocks for %s: %d\n",
820                         np->name, err);
821                 goto set_available;
822         }
823
824         err = tegra_powergate_of_get_resets(pg, np, off);
825         if (err < 0) {
826                 dev_err(pmc->dev, "failed to get resets for %s: %d\n",
827                         np->name, err);
828                 goto remove_clks;
829         }
830
831         if (!IS_ENABLED(CONFIG_PM_GENERIC_DOMAINS))
832                 goto power_on_cleanup;
833
834         /*
835          * FIXME: If XHCI is enabled for Tegra, then power-up the XUSB
836          * host and super-speed partitions. Once the XHCI driver
837          * manages the partitions itself this code can be removed. Note
838          * that we don't register these partitions with the genpd core
839          * to avoid it from powering down the partitions as they appear
840          * to be unused.
841          */
842         if (IS_ENABLED(CONFIG_USB_XHCI_TEGRA) &&
843             (id == TEGRA_POWERGATE_XUSBA || id == TEGRA_POWERGATE_XUSBC))
844                 goto power_on_cleanup;
845
846         pm_genpd_init(&pg->genpd, NULL, off);
847
848         err = of_genpd_add_provider_simple(np, &pg->genpd);
849         if (err < 0) {
850                 dev_err(pmc->dev, "failed to add genpd provider for %s: %d\n",
851                         np->name, err);
852                 goto remove_resets;
853         }
854
855         dev_dbg(pmc->dev, "added power domain %s\n", pg->genpd.name);
856
857         return;
858
859 power_on_cleanup:
860         if (off)
861                 WARN_ON(tegra_powergate_power_up(pg, true));
862
863 remove_resets:
864         while (pg->num_resets--)
865                 reset_control_put(pg->resets[pg->num_resets]);
866
867         kfree(pg->resets);
868
869 remove_clks:
870         while (pg->num_clks--)
871                 clk_put(pg->clks[pg->num_clks]);
872
873         kfree(pg->clks);
874
875 set_available:
876         set_bit(id, pmc->powergates_available);
877
878 free_mem:
879         kfree(pg);
880 }
881
882 static void tegra_powergate_init(struct tegra_pmc *pmc,
883                                  struct device_node *parent)
884 {
885         struct device_node *np, *child;
886         unsigned int i;
887
888         /* Create a bitmap of the available and valid partitions */
889         for (i = 0; i < pmc->soc->num_powergates; i++)
890                 if (pmc->soc->powergates[i])
891                         set_bit(i, pmc->powergates_available);
892
893         np = of_get_child_by_name(parent, "powergates");
894         if (!np)
895                 return;
896
897         for_each_child_of_node(np, child) {
898                 tegra_powergate_add(pmc, child);
899                 of_node_put(child);
900         }
901
902         of_node_put(np);
903 }
904
905 static int tegra_io_rail_prepare(unsigned int id, unsigned long *request,
906                                  unsigned long *status, unsigned int *bit)
907 {
908         unsigned long rate, value;
909
910         *bit = id % 32;
911
912         /*
913          * There are two sets of 30 bits to select IO rails, but bits 30 and
914          * 31 are control bits rather than IO rail selection bits.
915          */
916         if (id > 63 || *bit == 30 || *bit == 31)
917                 return -EINVAL;
918
919         if (id < 32) {
920                 *status = IO_DPD_STATUS;
921                 *request = IO_DPD_REQ;
922         } else {
923                 *status = IO_DPD2_STATUS;
924                 *request = IO_DPD2_REQ;
925         }
926
927         rate = clk_get_rate(pmc->clk);
928
929         tegra_pmc_writel(DPD_SAMPLE_ENABLE, DPD_SAMPLE);
930
931         /* must be at least 200 ns, in APB (PCLK) clock cycles */
932         value = DIV_ROUND_UP(1000000000, rate);
933         value = DIV_ROUND_UP(200, value);
934         tegra_pmc_writel(value, SEL_DPD_TIM);
935
936         return 0;
937 }
938
939 static int tegra_io_rail_poll(unsigned long offset, unsigned long mask,
940                               unsigned long val, unsigned long timeout)
941 {
942         unsigned long value;
943
944         timeout = jiffies + msecs_to_jiffies(timeout);
945
946         while (time_after(timeout, jiffies)) {
947                 value = tegra_pmc_readl(offset);
948                 if ((value & mask) == val)
949                         return 0;
950
951                 usleep_range(250, 1000);
952         }
953
954         return -ETIMEDOUT;
955 }
956
957 static void tegra_io_rail_unprepare(void)
958 {
959         tegra_pmc_writel(DPD_SAMPLE_DISABLE, DPD_SAMPLE);
960 }
961
962 int tegra_io_rail_power_on(unsigned int id)
963 {
964         unsigned long request, status;
965         unsigned int bit;
966         int err;
967
968         mutex_lock(&pmc->powergates_lock);
969
970         err = tegra_io_rail_prepare(id, &request, &status, &bit);
971         if (err)
972                 goto error;
973
974         tegra_pmc_writel(IO_DPD_REQ_CODE_OFF | BIT(bit), request);
975
976         err = tegra_io_rail_poll(status, BIT(bit), 0, 250);
977         if (err) {
978                 pr_info("tegra_io_rail_poll() failed: %d\n", err);
979                 goto error;
980         }
981
982         tegra_io_rail_unprepare();
983
984 error:
985         mutex_unlock(&pmc->powergates_lock);
986
987         return err;
988 }
989 EXPORT_SYMBOL(tegra_io_rail_power_on);
990
991 int tegra_io_rail_power_off(unsigned int id)
992 {
993         unsigned long request, status;
994         unsigned int bit;
995         int err;
996
997         mutex_lock(&pmc->powergates_lock);
998
999         err = tegra_io_rail_prepare(id, &request, &status, &bit);
1000         if (err) {
1001                 pr_info("tegra_io_rail_prepare() failed: %d\n", err);
1002                 goto error;
1003         }
1004
1005         tegra_pmc_writel(IO_DPD_REQ_CODE_ON | BIT(bit), request);
1006
1007         err = tegra_io_rail_poll(status, BIT(bit), BIT(bit), 250);
1008         if (err)
1009                 goto error;
1010
1011         tegra_io_rail_unprepare();
1012
1013 error:
1014         mutex_unlock(&pmc->powergates_lock);
1015
1016         return err;
1017 }
1018 EXPORT_SYMBOL(tegra_io_rail_power_off);
1019
1020 #ifdef CONFIG_PM_SLEEP
1021 enum tegra_suspend_mode tegra_pmc_get_suspend_mode(void)
1022 {
1023         return pmc->suspend_mode;
1024 }
1025
1026 void tegra_pmc_set_suspend_mode(enum tegra_suspend_mode mode)
1027 {
1028         if (mode < TEGRA_SUSPEND_NONE || mode >= TEGRA_MAX_SUSPEND_MODE)
1029                 return;
1030
1031         pmc->suspend_mode = mode;
1032 }
1033
1034 void tegra_pmc_enter_suspend_mode(enum tegra_suspend_mode mode)
1035 {
1036         unsigned long long rate = 0;
1037         u32 value;
1038
1039         switch (mode) {
1040         case TEGRA_SUSPEND_LP1:
1041                 rate = 32768;
1042                 break;
1043
1044         case TEGRA_SUSPEND_LP2:
1045                 rate = clk_get_rate(pmc->clk);
1046                 break;
1047
1048         default:
1049                 break;
1050         }
1051
1052         if (WARN_ON_ONCE(rate == 0))
1053                 rate = 100000000;
1054
1055         if (rate != pmc->rate) {
1056                 u64 ticks;
1057
1058                 ticks = pmc->cpu_good_time * rate + USEC_PER_SEC - 1;
1059                 do_div(ticks, USEC_PER_SEC);
1060                 tegra_pmc_writel(ticks, PMC_CPUPWRGOOD_TIMER);
1061
1062                 ticks = pmc->cpu_off_time * rate + USEC_PER_SEC - 1;
1063                 do_div(ticks, USEC_PER_SEC);
1064                 tegra_pmc_writel(ticks, PMC_CPUPWROFF_TIMER);
1065
1066                 wmb();
1067
1068                 pmc->rate = rate;
1069         }
1070
1071         value = tegra_pmc_readl(PMC_CNTRL);
1072         value &= ~PMC_CNTRL_SIDE_EFFECT_LP0;
1073         value |= PMC_CNTRL_CPU_PWRREQ_OE;
1074         tegra_pmc_writel(value, PMC_CNTRL);
1075 }
1076 #endif
1077
1078 static int tegra_pmc_parse_dt(struct tegra_pmc *pmc, struct device_node *np)
1079 {
1080         u32 value, values[2];
1081
1082         if (of_property_read_u32(np, "nvidia,suspend-mode", &value)) {
1083         } else {
1084                 switch (value) {
1085                 case 0:
1086                         pmc->suspend_mode = TEGRA_SUSPEND_LP0;
1087                         break;
1088
1089                 case 1:
1090                         pmc->suspend_mode = TEGRA_SUSPEND_LP1;
1091                         break;
1092
1093                 case 2:
1094                         pmc->suspend_mode = TEGRA_SUSPEND_LP2;
1095                         break;
1096
1097                 default:
1098                         pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1099                         break;
1100                 }
1101         }
1102
1103         pmc->suspend_mode = tegra_pm_validate_suspend_mode(pmc->suspend_mode);
1104
1105         if (of_property_read_u32(np, "nvidia,cpu-pwr-good-time", &value))
1106                 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1107
1108         pmc->cpu_good_time = value;
1109
1110         if (of_property_read_u32(np, "nvidia,cpu-pwr-off-time", &value))
1111                 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1112
1113         pmc->cpu_off_time = value;
1114
1115         if (of_property_read_u32_array(np, "nvidia,core-pwr-good-time",
1116                                        values, ARRAY_SIZE(values)))
1117                 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1118
1119         pmc->core_osc_time = values[0];
1120         pmc->core_pmu_time = values[1];
1121
1122         if (of_property_read_u32(np, "nvidia,core-pwr-off-time", &value))
1123                 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1124
1125         pmc->core_off_time = value;
1126
1127         pmc->corereq_high = of_property_read_bool(np,
1128                                 "nvidia,core-power-req-active-high");
1129
1130         pmc->sysclkreq_high = of_property_read_bool(np,
1131                                 "nvidia,sys-clock-req-active-high");
1132
1133         pmc->combined_req = of_property_read_bool(np,
1134                                 "nvidia,combined-power-req");
1135
1136         pmc->cpu_pwr_good_en = of_property_read_bool(np,
1137                                 "nvidia,cpu-pwr-good-en");
1138
1139         if (of_property_read_u32_array(np, "nvidia,lp0-vec", values,
1140                                        ARRAY_SIZE(values)))
1141                 if (pmc->suspend_mode == TEGRA_SUSPEND_LP0)
1142                         pmc->suspend_mode = TEGRA_SUSPEND_LP1;
1143
1144         pmc->lp0_vec_phys = values[0];
1145         pmc->lp0_vec_size = values[1];
1146
1147         return 0;
1148 }
1149
1150 static void tegra_pmc_init(struct tegra_pmc *pmc)
1151 {
1152         u32 value;
1153
1154         /* Always enable CPU power request */
1155         value = tegra_pmc_readl(PMC_CNTRL);
1156         value |= PMC_CNTRL_CPU_PWRREQ_OE;
1157         tegra_pmc_writel(value, PMC_CNTRL);
1158
1159         value = tegra_pmc_readl(PMC_CNTRL);
1160
1161         if (pmc->sysclkreq_high)
1162                 value &= ~PMC_CNTRL_SYSCLK_POLARITY;
1163         else
1164                 value |= PMC_CNTRL_SYSCLK_POLARITY;
1165
1166         /* configure the output polarity while the request is tristated */
1167         tegra_pmc_writel(value, PMC_CNTRL);
1168
1169         /* now enable the request */
1170         value = tegra_pmc_readl(PMC_CNTRL);
1171         value |= PMC_CNTRL_SYSCLK_OE;
1172         tegra_pmc_writel(value, PMC_CNTRL);
1173 }
1174
1175 static void tegra_pmc_init_tsense_reset(struct tegra_pmc *pmc)
1176 {
1177         static const char disabled[] = "emergency thermal reset disabled";
1178         u32 pmu_addr, ctrl_id, reg_addr, reg_data, pinmux;
1179         struct device *dev = pmc->dev;
1180         struct device_node *np;
1181         u32 value, checksum;
1182
1183         if (!pmc->soc->has_tsense_reset)
1184                 return;
1185
1186         np = of_get_child_by_name(pmc->dev->of_node, "i2c-thermtrip");
1187         if (!np) {
1188                 dev_warn(dev, "i2c-thermtrip node not found, %s.\n", disabled);
1189                 return;
1190         }
1191
1192         if (of_property_read_u32(np, "nvidia,i2c-controller-id", &ctrl_id)) {
1193                 dev_err(dev, "I2C controller ID missing, %s.\n", disabled);
1194                 goto out;
1195         }
1196
1197         if (of_property_read_u32(np, "nvidia,bus-addr", &pmu_addr)) {
1198                 dev_err(dev, "nvidia,bus-addr missing, %s.\n", disabled);
1199                 goto out;
1200         }
1201
1202         if (of_property_read_u32(np, "nvidia,reg-addr", &reg_addr)) {
1203                 dev_err(dev, "nvidia,reg-addr missing, %s.\n", disabled);
1204                 goto out;
1205         }
1206
1207         if (of_property_read_u32(np, "nvidia,reg-data", &reg_data)) {
1208                 dev_err(dev, "nvidia,reg-data missing, %s.\n", disabled);
1209                 goto out;
1210         }
1211
1212         if (of_property_read_u32(np, "nvidia,pinmux-id", &pinmux))
1213                 pinmux = 0;
1214
1215         value = tegra_pmc_readl(PMC_SENSOR_CTRL);
1216         value |= PMC_SENSOR_CTRL_SCRATCH_WRITE;
1217         tegra_pmc_writel(value, PMC_SENSOR_CTRL);
1218
1219         value = (reg_data << PMC_SCRATCH54_DATA_SHIFT) |
1220                 (reg_addr << PMC_SCRATCH54_ADDR_SHIFT);
1221         tegra_pmc_writel(value, PMC_SCRATCH54);
1222
1223         value = PMC_SCRATCH55_RESET_TEGRA;
1224         value |= ctrl_id << PMC_SCRATCH55_CNTRL_ID_SHIFT;
1225         value |= pinmux << PMC_SCRATCH55_PINMUX_SHIFT;
1226         value |= pmu_addr << PMC_SCRATCH55_I2CSLV1_SHIFT;
1227
1228         /*
1229          * Calculate checksum of SCRATCH54, SCRATCH55 fields. Bits 23:16 will
1230          * contain the checksum and are currently zero, so they are not added.
1231          */
1232         checksum = reg_addr + reg_data + (value & 0xff) + ((value >> 8) & 0xff)
1233                 + ((value >> 24) & 0xff);
1234         checksum &= 0xff;
1235         checksum = 0x100 - checksum;
1236
1237         value |= checksum << PMC_SCRATCH55_CHECKSUM_SHIFT;
1238
1239         tegra_pmc_writel(value, PMC_SCRATCH55);
1240
1241         value = tegra_pmc_readl(PMC_SENSOR_CTRL);
1242         value |= PMC_SENSOR_CTRL_ENABLE_RST;
1243         tegra_pmc_writel(value, PMC_SENSOR_CTRL);
1244
1245         dev_info(pmc->dev, "emergency thermal reset enabled\n");
1246
1247 out:
1248         of_node_put(np);
1249 }
1250
1251 static int tegra_pmc_probe(struct platform_device *pdev)
1252 {
1253         void __iomem *base;
1254         struct resource *res;
1255         int err;
1256
1257         /*
1258          * Early initialisation should have configured an initial
1259          * register mapping and setup the soc data pointer. If these
1260          * are not valid then something went badly wrong!
1261          */
1262         if (WARN_ON(!pmc->base || !pmc->soc))
1263                 return -ENODEV;
1264
1265         err = tegra_pmc_parse_dt(pmc, pdev->dev.of_node);
1266         if (err < 0)
1267                 return err;
1268
1269         /* take over the memory region from the early initialization */
1270         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1271         base = devm_ioremap_resource(&pdev->dev, res);
1272         if (IS_ERR(base))
1273                 return PTR_ERR(base);
1274
1275         pmc->clk = devm_clk_get(&pdev->dev, "pclk");
1276         if (IS_ERR(pmc->clk)) {
1277                 err = PTR_ERR(pmc->clk);
1278                 dev_err(&pdev->dev, "failed to get pclk: %d\n", err);
1279                 return err;
1280         }
1281
1282         pmc->dev = &pdev->dev;
1283
1284         tegra_pmc_init(pmc);
1285
1286         tegra_pmc_init_tsense_reset(pmc);
1287
1288         if (IS_ENABLED(CONFIG_DEBUG_FS)) {
1289                 err = tegra_powergate_debugfs_init();
1290                 if (err < 0)
1291                         return err;
1292         }
1293
1294         err = register_restart_handler(&tegra_pmc_restart_handler);
1295         if (err) {
1296                 debugfs_remove(pmc->debugfs);
1297                 dev_err(&pdev->dev, "unable to register restart handler, %d\n",
1298                         err);
1299                 return err;
1300         }
1301
1302         mutex_lock(&pmc->powergates_lock);
1303         iounmap(pmc->base);
1304         pmc->base = base;
1305         mutex_unlock(&pmc->powergates_lock);
1306
1307         return 0;
1308 }
1309
1310 #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM)
1311 static int tegra_pmc_suspend(struct device *dev)
1312 {
1313         tegra_pmc_writel(virt_to_phys(tegra_resume), PMC_SCRATCH41);
1314
1315         return 0;
1316 }
1317
1318 static int tegra_pmc_resume(struct device *dev)
1319 {
1320         tegra_pmc_writel(0x0, PMC_SCRATCH41);
1321
1322         return 0;
1323 }
1324
1325 static SIMPLE_DEV_PM_OPS(tegra_pmc_pm_ops, tegra_pmc_suspend, tegra_pmc_resume);
1326
1327 #endif
1328
1329 static const char * const tegra20_powergates[] = {
1330         [TEGRA_POWERGATE_CPU] = "cpu",
1331         [TEGRA_POWERGATE_3D] = "3d",
1332         [TEGRA_POWERGATE_VENC] = "venc",
1333         [TEGRA_POWERGATE_VDEC] = "vdec",
1334         [TEGRA_POWERGATE_PCIE] = "pcie",
1335         [TEGRA_POWERGATE_L2] = "l2",
1336         [TEGRA_POWERGATE_MPE] = "mpe",
1337 };
1338
1339 static const struct tegra_pmc_soc tegra20_pmc_soc = {
1340         .num_powergates = ARRAY_SIZE(tegra20_powergates),
1341         .powergates = tegra20_powergates,
1342         .num_cpu_powergates = 0,
1343         .cpu_powergates = NULL,
1344         .has_tsense_reset = false,
1345         .has_gpu_clamps = false,
1346 };
1347
1348 static const char * const tegra30_powergates[] = {
1349         [TEGRA_POWERGATE_CPU] = "cpu0",
1350         [TEGRA_POWERGATE_3D] = "3d0",
1351         [TEGRA_POWERGATE_VENC] = "venc",
1352         [TEGRA_POWERGATE_VDEC] = "vdec",
1353         [TEGRA_POWERGATE_PCIE] = "pcie",
1354         [TEGRA_POWERGATE_L2] = "l2",
1355         [TEGRA_POWERGATE_MPE] = "mpe",
1356         [TEGRA_POWERGATE_HEG] = "heg",
1357         [TEGRA_POWERGATE_SATA] = "sata",
1358         [TEGRA_POWERGATE_CPU1] = "cpu1",
1359         [TEGRA_POWERGATE_CPU2] = "cpu2",
1360         [TEGRA_POWERGATE_CPU3] = "cpu3",
1361         [TEGRA_POWERGATE_CELP] = "celp",
1362         [TEGRA_POWERGATE_3D1] = "3d1",
1363 };
1364
1365 static const u8 tegra30_cpu_powergates[] = {
1366         TEGRA_POWERGATE_CPU,
1367         TEGRA_POWERGATE_CPU1,
1368         TEGRA_POWERGATE_CPU2,
1369         TEGRA_POWERGATE_CPU3,
1370 };
1371
1372 static const struct tegra_pmc_soc tegra30_pmc_soc = {
1373         .num_powergates = ARRAY_SIZE(tegra30_powergates),
1374         .powergates = tegra30_powergates,
1375         .num_cpu_powergates = ARRAY_SIZE(tegra30_cpu_powergates),
1376         .cpu_powergates = tegra30_cpu_powergates,
1377         .has_tsense_reset = true,
1378         .has_gpu_clamps = false,
1379 };
1380
1381 static const char * const tegra114_powergates[] = {
1382         [TEGRA_POWERGATE_CPU] = "crail",
1383         [TEGRA_POWERGATE_3D] = "3d",
1384         [TEGRA_POWERGATE_VENC] = "venc",
1385         [TEGRA_POWERGATE_VDEC] = "vdec",
1386         [TEGRA_POWERGATE_MPE] = "mpe",
1387         [TEGRA_POWERGATE_HEG] = "heg",
1388         [TEGRA_POWERGATE_CPU1] = "cpu1",
1389         [TEGRA_POWERGATE_CPU2] = "cpu2",
1390         [TEGRA_POWERGATE_CPU3] = "cpu3",
1391         [TEGRA_POWERGATE_CELP] = "celp",
1392         [TEGRA_POWERGATE_CPU0] = "cpu0",
1393         [TEGRA_POWERGATE_C0NC] = "c0nc",
1394         [TEGRA_POWERGATE_C1NC] = "c1nc",
1395         [TEGRA_POWERGATE_DIS] = "dis",
1396         [TEGRA_POWERGATE_DISB] = "disb",
1397         [TEGRA_POWERGATE_XUSBA] = "xusba",
1398         [TEGRA_POWERGATE_XUSBB] = "xusbb",
1399         [TEGRA_POWERGATE_XUSBC] = "xusbc",
1400 };
1401
1402 static const u8 tegra114_cpu_powergates[] = {
1403         TEGRA_POWERGATE_CPU0,
1404         TEGRA_POWERGATE_CPU1,
1405         TEGRA_POWERGATE_CPU2,
1406         TEGRA_POWERGATE_CPU3,
1407 };
1408
1409 static const struct tegra_pmc_soc tegra114_pmc_soc = {
1410         .num_powergates = ARRAY_SIZE(tegra114_powergates),
1411         .powergates = tegra114_powergates,
1412         .num_cpu_powergates = ARRAY_SIZE(tegra114_cpu_powergates),
1413         .cpu_powergates = tegra114_cpu_powergates,
1414         .has_tsense_reset = true,
1415         .has_gpu_clamps = false,
1416 };
1417
1418 static const char * const tegra124_powergates[] = {
1419         [TEGRA_POWERGATE_CPU] = "crail",
1420         [TEGRA_POWERGATE_3D] = "3d",
1421         [TEGRA_POWERGATE_VENC] = "venc",
1422         [TEGRA_POWERGATE_PCIE] = "pcie",
1423         [TEGRA_POWERGATE_VDEC] = "vdec",
1424         [TEGRA_POWERGATE_MPE] = "mpe",
1425         [TEGRA_POWERGATE_HEG] = "heg",
1426         [TEGRA_POWERGATE_SATA] = "sata",
1427         [TEGRA_POWERGATE_CPU1] = "cpu1",
1428         [TEGRA_POWERGATE_CPU2] = "cpu2",
1429         [TEGRA_POWERGATE_CPU3] = "cpu3",
1430         [TEGRA_POWERGATE_CELP] = "celp",
1431         [TEGRA_POWERGATE_CPU0] = "cpu0",
1432         [TEGRA_POWERGATE_C0NC] = "c0nc",
1433         [TEGRA_POWERGATE_C1NC] = "c1nc",
1434         [TEGRA_POWERGATE_SOR] = "sor",
1435         [TEGRA_POWERGATE_DIS] = "dis",
1436         [TEGRA_POWERGATE_DISB] = "disb",
1437         [TEGRA_POWERGATE_XUSBA] = "xusba",
1438         [TEGRA_POWERGATE_XUSBB] = "xusbb",
1439         [TEGRA_POWERGATE_XUSBC] = "xusbc",
1440         [TEGRA_POWERGATE_VIC] = "vic",
1441         [TEGRA_POWERGATE_IRAM] = "iram",
1442 };
1443
1444 static const u8 tegra124_cpu_powergates[] = {
1445         TEGRA_POWERGATE_CPU0,
1446         TEGRA_POWERGATE_CPU1,
1447         TEGRA_POWERGATE_CPU2,
1448         TEGRA_POWERGATE_CPU3,
1449 };
1450
1451 static const struct tegra_pmc_soc tegra124_pmc_soc = {
1452         .num_powergates = ARRAY_SIZE(tegra124_powergates),
1453         .powergates = tegra124_powergates,
1454         .num_cpu_powergates = ARRAY_SIZE(tegra124_cpu_powergates),
1455         .cpu_powergates = tegra124_cpu_powergates,
1456         .has_tsense_reset = true,
1457         .has_gpu_clamps = true,
1458 };
1459
1460 static const char * const tegra210_powergates[] = {
1461         [TEGRA_POWERGATE_CPU] = "crail",
1462         [TEGRA_POWERGATE_3D] = "3d",
1463         [TEGRA_POWERGATE_VENC] = "venc",
1464         [TEGRA_POWERGATE_PCIE] = "pcie",
1465         [TEGRA_POWERGATE_MPE] = "mpe",
1466         [TEGRA_POWERGATE_SATA] = "sata",
1467         [TEGRA_POWERGATE_CPU1] = "cpu1",
1468         [TEGRA_POWERGATE_CPU2] = "cpu2",
1469         [TEGRA_POWERGATE_CPU3] = "cpu3",
1470         [TEGRA_POWERGATE_CPU0] = "cpu0",
1471         [TEGRA_POWERGATE_C0NC] = "c0nc",
1472         [TEGRA_POWERGATE_SOR] = "sor",
1473         [TEGRA_POWERGATE_DIS] = "dis",
1474         [TEGRA_POWERGATE_DISB] = "disb",
1475         [TEGRA_POWERGATE_XUSBA] = "xusba",
1476         [TEGRA_POWERGATE_XUSBB] = "xusbb",
1477         [TEGRA_POWERGATE_XUSBC] = "xusbc",
1478         [TEGRA_POWERGATE_VIC] = "vic",
1479         [TEGRA_POWERGATE_IRAM] = "iram",
1480         [TEGRA_POWERGATE_NVDEC] = "nvdec",
1481         [TEGRA_POWERGATE_NVJPG] = "nvjpg",
1482         [TEGRA_POWERGATE_AUD] = "aud",
1483         [TEGRA_POWERGATE_DFD] = "dfd",
1484         [TEGRA_POWERGATE_VE2] = "ve2",
1485 };
1486
1487 static const u8 tegra210_cpu_powergates[] = {
1488         TEGRA_POWERGATE_CPU0,
1489         TEGRA_POWERGATE_CPU1,
1490         TEGRA_POWERGATE_CPU2,
1491         TEGRA_POWERGATE_CPU3,
1492 };
1493
1494 static const struct tegra_pmc_soc tegra210_pmc_soc = {
1495         .num_powergates = ARRAY_SIZE(tegra210_powergates),
1496         .powergates = tegra210_powergates,
1497         .num_cpu_powergates = ARRAY_SIZE(tegra210_cpu_powergates),
1498         .cpu_powergates = tegra210_cpu_powergates,
1499         .has_tsense_reset = true,
1500         .has_gpu_clamps = true,
1501 };
1502
1503 static const struct of_device_id tegra_pmc_match[] = {
1504         { .compatible = "nvidia,tegra210-pmc", .data = &tegra210_pmc_soc },
1505         { .compatible = "nvidia,tegra132-pmc", .data = &tegra124_pmc_soc },
1506         { .compatible = "nvidia,tegra124-pmc", .data = &tegra124_pmc_soc },
1507         { .compatible = "nvidia,tegra114-pmc", .data = &tegra114_pmc_soc },
1508         { .compatible = "nvidia,tegra30-pmc", .data = &tegra30_pmc_soc },
1509         { .compatible = "nvidia,tegra20-pmc", .data = &tegra20_pmc_soc },
1510         { }
1511 };
1512
1513 static struct platform_driver tegra_pmc_driver = {
1514         .driver = {
1515                 .name = "tegra-pmc",
1516                 .suppress_bind_attrs = true,
1517                 .of_match_table = tegra_pmc_match,
1518 #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM)
1519                 .pm = &tegra_pmc_pm_ops,
1520 #endif
1521         },
1522         .probe = tegra_pmc_probe,
1523 };
1524 builtin_platform_driver(tegra_pmc_driver);
1525
1526 /*
1527  * Early initialization to allow access to registers in the very early boot
1528  * process.
1529  */
1530 static int __init tegra_pmc_early_init(void)
1531 {
1532         const struct of_device_id *match;
1533         struct device_node *np;
1534         struct resource regs;
1535         bool invert;
1536         u32 value;
1537
1538         mutex_init(&pmc->powergates_lock);
1539
1540         np = of_find_matching_node_and_match(NULL, tegra_pmc_match, &match);
1541         if (!np) {
1542                 /*
1543                  * Fall back to legacy initialization for 32-bit ARM only. All
1544                  * 64-bit ARM device tree files for Tegra are required to have
1545                  * a PMC node.
1546                  *
1547                  * This is for backwards-compatibility with old device trees
1548                  * that didn't contain a PMC node. Note that in this case the
1549                  * SoC data can't be matched and therefore powergating is
1550                  * disabled.
1551                  */
1552                 if (IS_ENABLED(CONFIG_ARM) && soc_is_tegra()) {
1553                         pr_warn("DT node not found, powergating disabled\n");
1554
1555                         regs.start = 0x7000e400;
1556                         regs.end = 0x7000e7ff;
1557                         regs.flags = IORESOURCE_MEM;
1558
1559                         pr_warn("Using memory region %pR\n", &regs);
1560                 } else {
1561                         /*
1562                          * At this point we're not running on Tegra, so play
1563                          * nice with multi-platform kernels.
1564                          */
1565                         return 0;
1566                 }
1567         } else {
1568                 /*
1569                  * Extract information from the device tree if we've found a
1570                  * matching node.
1571                  */
1572                 if (of_address_to_resource(np, 0, &regs) < 0) {
1573                         pr_err("failed to get PMC registers\n");
1574                         of_node_put(np);
1575                         return -ENXIO;
1576                 }
1577         }
1578
1579         pmc->base = ioremap_nocache(regs.start, resource_size(&regs));
1580         if (!pmc->base) {
1581                 pr_err("failed to map PMC registers\n");
1582                 of_node_put(np);
1583                 return -ENXIO;
1584         }
1585
1586         if (np) {
1587                 pmc->soc = match->data;
1588
1589                 tegra_powergate_init(pmc, np);
1590
1591                 /*
1592                  * Invert the interrupt polarity if a PMC device tree node
1593                  * exists and contains the nvidia,invert-interrupt property.
1594                  */
1595                 invert = of_property_read_bool(np, "nvidia,invert-interrupt");
1596
1597                 value = tegra_pmc_readl(PMC_CNTRL);
1598
1599                 if (invert)
1600                         value |= PMC_CNTRL_INTR_POLARITY;
1601                 else
1602                         value &= ~PMC_CNTRL_INTR_POLARITY;
1603
1604                 tegra_pmc_writel(value, PMC_CNTRL);
1605
1606                 of_node_put(np);
1607         }
1608
1609         return 0;
1610 }
1611 early_initcall(tegra_pmc_early_init);